expr.h (prepare_operand): Move to...
[gcc.git] / gcc / optabs.c
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "diagnostic-core.h"
28
29 /* Include insn-config.h before expr.h so that HAVE_conditional_move
30 is properly defined. */
31 #include "insn-config.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "except.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "libfuncs.h"
41 #include "recog.h"
42 #include "reload.h"
43 #include "ggc.h"
44 #include "basic-block.h"
45 #include "target.h"
46
47 struct target_optabs default_target_optabs;
48 struct target_libfuncs default_target_libfuncs;
49 #if SWITCHABLE_TARGET
50 struct target_optabs *this_target_optabs = &default_target_optabs;
51 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
52 #endif
53
54 #define libfunc_hash \
55 (this_target_libfuncs->x_libfunc_hash)
56
57 /* Contains the optab used for each rtx code. */
58 optab code_to_optab[NUM_RTX_CODE + 1];
59
60 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
61 enum machine_mode *);
62 static rtx expand_unop_direct (enum machine_mode, optab, rtx, rtx, int);
63
64 /* Debug facility for use in GDB. */
65 void debug_optab_libfuncs (void);
66
67 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
68 #if ENABLE_DECIMAL_BID_FORMAT
69 #define DECIMAL_PREFIX "bid_"
70 #else
71 #define DECIMAL_PREFIX "dpd_"
72 #endif
73 \f
74 /* Used for libfunc_hash. */
75
76 static hashval_t
77 hash_libfunc (const void *p)
78 {
79 const struct libfunc_entry *const e = (const struct libfunc_entry *) p;
80
81 return (((int) e->mode1 + (int) e->mode2 * NUM_MACHINE_MODES)
82 ^ e->optab);
83 }
84
85 /* Used for libfunc_hash. */
86
87 static int
88 eq_libfunc (const void *p, const void *q)
89 {
90 const struct libfunc_entry *const e1 = (const struct libfunc_entry *) p;
91 const struct libfunc_entry *const e2 = (const struct libfunc_entry *) q;
92
93 return (e1->optab == e2->optab
94 && e1->mode1 == e2->mode1
95 && e1->mode2 == e2->mode2);
96 }
97
98 /* Return libfunc corresponding operation defined by OPTAB converting
99 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
100 if no libfunc is available. */
101 rtx
102 convert_optab_libfunc (convert_optab optab, enum machine_mode mode1,
103 enum machine_mode mode2)
104 {
105 struct libfunc_entry e;
106 struct libfunc_entry **slot;
107
108 e.optab = (size_t) (optab - &convert_optab_table[0]);
109 e.mode1 = mode1;
110 e.mode2 = mode2;
111 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
112 if (!slot)
113 {
114 if (optab->libcall_gen)
115 {
116 optab->libcall_gen (optab, optab->libcall_basename, mode1, mode2);
117 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
118 if (slot)
119 return (*slot)->libfunc;
120 else
121 return NULL;
122 }
123 return NULL;
124 }
125 return (*slot)->libfunc;
126 }
127
128 /* Return libfunc corresponding operation defined by OPTAB in MODE.
129 Trigger lazy initialization if needed, return NULL if no libfunc is
130 available. */
131 rtx
132 optab_libfunc (optab optab, enum machine_mode mode)
133 {
134 struct libfunc_entry e;
135 struct libfunc_entry **slot;
136
137 e.optab = (size_t) (optab - &optab_table[0]);
138 e.mode1 = mode;
139 e.mode2 = VOIDmode;
140 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
141 if (!slot)
142 {
143 if (optab->libcall_gen)
144 {
145 optab->libcall_gen (optab, optab->libcall_basename,
146 optab->libcall_suffix, mode);
147 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash,
148 &e, NO_INSERT);
149 if (slot)
150 return (*slot)->libfunc;
151 else
152 return NULL;
153 }
154 return NULL;
155 }
156 return (*slot)->libfunc;
157 }
158
159 \f
160 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
161 the result of operation CODE applied to OP0 (and OP1 if it is a binary
162 operation).
163
164 If the last insn does not set TARGET, don't do anything, but return 1.
165
166 If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
167 don't add the REG_EQUAL note but return 0. Our caller can then try
168 again, ensuring that TARGET is not one of the operands. */
169
170 static int
171 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
172 {
173 rtx last_insn, insn, set;
174 rtx note;
175
176 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
177
178 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
179 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
180 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
181 && GET_RTX_CLASS (code) != RTX_COMPARE
182 && GET_RTX_CLASS (code) != RTX_UNARY)
183 return 1;
184
185 if (GET_CODE (target) == ZERO_EXTRACT)
186 return 1;
187
188 for (last_insn = insns;
189 NEXT_INSN (last_insn) != NULL_RTX;
190 last_insn = NEXT_INSN (last_insn))
191 ;
192
193 set = single_set (last_insn);
194 if (set == NULL_RTX)
195 return 1;
196
197 if (! rtx_equal_p (SET_DEST (set), target)
198 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
199 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
200 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
201 return 1;
202
203 /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
204 besides the last insn. */
205 if (reg_overlap_mentioned_p (target, op0)
206 || (op1 && reg_overlap_mentioned_p (target, op1)))
207 {
208 insn = PREV_INSN (last_insn);
209 while (insn != NULL_RTX)
210 {
211 if (reg_set_p (target, insn))
212 return 0;
213
214 insn = PREV_INSN (insn);
215 }
216 }
217
218 if (GET_RTX_CLASS (code) == RTX_UNARY)
219 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
220 else
221 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
222
223 set_unique_reg_note (last_insn, REG_EQUAL, note);
224
225 return 1;
226 }
227 \f
228 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
229 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
230 not actually do a sign-extend or zero-extend, but can leave the
231 higher-order bits of the result rtx undefined, for example, in the case
232 of logical operations, but not right shifts. */
233
234 static rtx
235 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
236 int unsignedp, int no_extend)
237 {
238 rtx result;
239
240 /* If we don't have to extend and this is a constant, return it. */
241 if (no_extend && GET_MODE (op) == VOIDmode)
242 return op;
243
244 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
245 extend since it will be more efficient to do so unless the signedness of
246 a promoted object differs from our extension. */
247 if (! no_extend
248 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
249 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
250 return convert_modes (mode, oldmode, op, unsignedp);
251
252 /* If MODE is no wider than a single word, we return a paradoxical
253 SUBREG. */
254 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
255 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
256
257 /* Otherwise, get an object of MODE, clobber it, and set the low-order
258 part to OP. */
259
260 result = gen_reg_rtx (mode);
261 emit_clobber (result);
262 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
263 return result;
264 }
265 \f
266 /* Return the optab used for computing the operation given by the tree code,
267 CODE and the tree EXP. This function is not always usable (for example, it
268 cannot give complete results for multiplication or division) but probably
269 ought to be relied on more widely throughout the expander. */
270 optab
271 optab_for_tree_code (enum tree_code code, const_tree type,
272 enum optab_subtype subtype)
273 {
274 bool trapv;
275 switch (code)
276 {
277 case BIT_AND_EXPR:
278 return and_optab;
279
280 case BIT_IOR_EXPR:
281 return ior_optab;
282
283 case BIT_NOT_EXPR:
284 return one_cmpl_optab;
285
286 case BIT_XOR_EXPR:
287 return xor_optab;
288
289 case TRUNC_MOD_EXPR:
290 case CEIL_MOD_EXPR:
291 case FLOOR_MOD_EXPR:
292 case ROUND_MOD_EXPR:
293 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
294
295 case RDIV_EXPR:
296 case TRUNC_DIV_EXPR:
297 case CEIL_DIV_EXPR:
298 case FLOOR_DIV_EXPR:
299 case ROUND_DIV_EXPR:
300 case EXACT_DIV_EXPR:
301 if (TYPE_SATURATING(type))
302 return TYPE_UNSIGNED(type) ? usdiv_optab : ssdiv_optab;
303 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
304
305 case LSHIFT_EXPR:
306 if (TREE_CODE (type) == VECTOR_TYPE)
307 {
308 if (subtype == optab_vector)
309 return TYPE_SATURATING (type) ? NULL : vashl_optab;
310
311 gcc_assert (subtype == optab_scalar);
312 }
313 if (TYPE_SATURATING(type))
314 return TYPE_UNSIGNED(type) ? usashl_optab : ssashl_optab;
315 return ashl_optab;
316
317 case RSHIFT_EXPR:
318 if (TREE_CODE (type) == VECTOR_TYPE)
319 {
320 if (subtype == optab_vector)
321 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
322
323 gcc_assert (subtype == optab_scalar);
324 }
325 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
326
327 case LROTATE_EXPR:
328 if (TREE_CODE (type) == VECTOR_TYPE)
329 {
330 if (subtype == optab_vector)
331 return vrotl_optab;
332
333 gcc_assert (subtype == optab_scalar);
334 }
335 return rotl_optab;
336
337 case RROTATE_EXPR:
338 if (TREE_CODE (type) == VECTOR_TYPE)
339 {
340 if (subtype == optab_vector)
341 return vrotr_optab;
342
343 gcc_assert (subtype == optab_scalar);
344 }
345 return rotr_optab;
346
347 case MAX_EXPR:
348 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
349
350 case MIN_EXPR:
351 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
352
353 case REALIGN_LOAD_EXPR:
354 return vec_realign_load_optab;
355
356 case WIDEN_SUM_EXPR:
357 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
358
359 case DOT_PROD_EXPR:
360 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
361
362 case WIDEN_MULT_PLUS_EXPR:
363 return (TYPE_UNSIGNED (type)
364 ? (TYPE_SATURATING (type)
365 ? usmadd_widen_optab : umadd_widen_optab)
366 : (TYPE_SATURATING (type)
367 ? ssmadd_widen_optab : smadd_widen_optab));
368
369 case WIDEN_MULT_MINUS_EXPR:
370 return (TYPE_UNSIGNED (type)
371 ? (TYPE_SATURATING (type)
372 ? usmsub_widen_optab : umsub_widen_optab)
373 : (TYPE_SATURATING (type)
374 ? ssmsub_widen_optab : smsub_widen_optab));
375
376 case FMA_EXPR:
377 return fma_optab;
378
379 case REDUC_MAX_EXPR:
380 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
381
382 case REDUC_MIN_EXPR:
383 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
384
385 case REDUC_PLUS_EXPR:
386 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
387
388 case VEC_LSHIFT_EXPR:
389 return vec_shl_optab;
390
391 case VEC_RSHIFT_EXPR:
392 return vec_shr_optab;
393
394 case VEC_WIDEN_MULT_HI_EXPR:
395 return TYPE_UNSIGNED (type) ?
396 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
397
398 case VEC_WIDEN_MULT_LO_EXPR:
399 return TYPE_UNSIGNED (type) ?
400 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
401
402 case VEC_UNPACK_HI_EXPR:
403 return TYPE_UNSIGNED (type) ?
404 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
405
406 case VEC_UNPACK_LO_EXPR:
407 return TYPE_UNSIGNED (type) ?
408 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
409
410 case VEC_UNPACK_FLOAT_HI_EXPR:
411 /* The signedness is determined from input operand. */
412 return TYPE_UNSIGNED (type) ?
413 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
414
415 case VEC_UNPACK_FLOAT_LO_EXPR:
416 /* The signedness is determined from input operand. */
417 return TYPE_UNSIGNED (type) ?
418 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
419
420 case VEC_PACK_TRUNC_EXPR:
421 return vec_pack_trunc_optab;
422
423 case VEC_PACK_SAT_EXPR:
424 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
425
426 case VEC_PACK_FIX_TRUNC_EXPR:
427 /* The signedness is determined from output operand. */
428 return TYPE_UNSIGNED (type) ?
429 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
430
431 default:
432 break;
433 }
434
435 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
436 switch (code)
437 {
438 case POINTER_PLUS_EXPR:
439 case PLUS_EXPR:
440 if (TYPE_SATURATING(type))
441 return TYPE_UNSIGNED(type) ? usadd_optab : ssadd_optab;
442 return trapv ? addv_optab : add_optab;
443
444 case MINUS_EXPR:
445 if (TYPE_SATURATING(type))
446 return TYPE_UNSIGNED(type) ? ussub_optab : sssub_optab;
447 return trapv ? subv_optab : sub_optab;
448
449 case MULT_EXPR:
450 if (TYPE_SATURATING(type))
451 return TYPE_UNSIGNED(type) ? usmul_optab : ssmul_optab;
452 return trapv ? smulv_optab : smul_optab;
453
454 case NEGATE_EXPR:
455 if (TYPE_SATURATING(type))
456 return TYPE_UNSIGNED(type) ? usneg_optab : ssneg_optab;
457 return trapv ? negv_optab : neg_optab;
458
459 case ABS_EXPR:
460 return trapv ? absv_optab : abs_optab;
461
462 case VEC_EXTRACT_EVEN_EXPR:
463 return vec_extract_even_optab;
464
465 case VEC_EXTRACT_ODD_EXPR:
466 return vec_extract_odd_optab;
467
468 case VEC_INTERLEAVE_HIGH_EXPR:
469 return vec_interleave_high_optab;
470
471 case VEC_INTERLEAVE_LOW_EXPR:
472 return vec_interleave_low_optab;
473
474 default:
475 return NULL;
476 }
477 }
478 \f
479
480 /* Expand vector widening operations.
481
482 There are two different classes of operations handled here:
483 1) Operations whose result is wider than all the arguments to the operation.
484 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
485 In this case OP0 and optionally OP1 would be initialized,
486 but WIDE_OP wouldn't (not relevant for this case).
487 2) Operations whose result is of the same size as the last argument to the
488 operation, but wider than all the other arguments to the operation.
489 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
490 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
491
492 E.g, when called to expand the following operations, this is how
493 the arguments will be initialized:
494 nops OP0 OP1 WIDE_OP
495 widening-sum 2 oprnd0 - oprnd1
496 widening-dot-product 3 oprnd0 oprnd1 oprnd2
497 widening-mult 2 oprnd0 oprnd1 -
498 type-promotion (vec-unpack) 1 oprnd0 - - */
499
500 rtx
501 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
502 rtx target, int unsignedp)
503 {
504 tree oprnd0, oprnd1, oprnd2;
505 enum machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
506 optab widen_pattern_optab;
507 int icode;
508 enum machine_mode xmode0, xmode1 = VOIDmode, wxmode = VOIDmode;
509 rtx temp;
510 rtx pat;
511 rtx xop0, xop1, wxop;
512 int nops = TREE_CODE_LENGTH (ops->code);
513
514 oprnd0 = ops->op0;
515 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
516 widen_pattern_optab =
517 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
518 if (ops->code == WIDEN_MULT_PLUS_EXPR
519 || ops->code == WIDEN_MULT_MINUS_EXPR)
520 icode = (int) optab_handler (widen_pattern_optab,
521 TYPE_MODE (TREE_TYPE (ops->op2)));
522 else
523 icode = (int) optab_handler (widen_pattern_optab, tmode0);
524 gcc_assert (icode != CODE_FOR_nothing);
525 xmode0 = insn_data[icode].operand[1].mode;
526
527 if (nops >= 2)
528 {
529 oprnd1 = ops->op1;
530 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
531 xmode1 = insn_data[icode].operand[2].mode;
532 }
533
534 /* The last operand is of a wider mode than the rest of the operands. */
535 if (nops == 2)
536 {
537 wmode = tmode1;
538 wxmode = xmode1;
539 }
540 else if (nops == 3)
541 {
542 gcc_assert (tmode1 == tmode0);
543 gcc_assert (op1);
544 oprnd2 = ops->op2;
545 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
546 wxmode = insn_data[icode].operand[3].mode;
547 }
548
549 if (!wide_op)
550 wmode = wxmode = insn_data[icode].operand[0].mode;
551
552 if (!target
553 || ! (*insn_data[icode].operand[0].predicate) (target, wmode))
554 temp = gen_reg_rtx (wmode);
555 else
556 temp = target;
557
558 xop0 = op0;
559 xop1 = op1;
560 wxop = wide_op;
561
562 /* In case the insn wants input operands in modes different from
563 those of the actual operands, convert the operands. It would
564 seem that we don't need to convert CONST_INTs, but we do, so
565 that they're properly zero-extended, sign-extended or truncated
566 for their mode. */
567
568 if (GET_MODE (op0) != xmode0 && xmode0 != VOIDmode)
569 xop0 = convert_modes (xmode0,
570 GET_MODE (op0) != VOIDmode
571 ? GET_MODE (op0)
572 : tmode0,
573 xop0, unsignedp);
574
575 if (op1)
576 if (GET_MODE (op1) != xmode1 && xmode1 != VOIDmode)
577 xop1 = convert_modes (xmode1,
578 GET_MODE (op1) != VOIDmode
579 ? GET_MODE (op1)
580 : tmode1,
581 xop1, unsignedp);
582
583 if (wide_op)
584 if (GET_MODE (wide_op) != wxmode && wxmode != VOIDmode)
585 wxop = convert_modes (wxmode,
586 GET_MODE (wide_op) != VOIDmode
587 ? GET_MODE (wide_op)
588 : wmode,
589 wxop, unsignedp);
590
591 /* Now, if insn's predicates don't allow our operands, put them into
592 pseudo regs. */
593
594 if (! (*insn_data[icode].operand[1].predicate) (xop0, xmode0)
595 && xmode0 != VOIDmode)
596 xop0 = copy_to_mode_reg (xmode0, xop0);
597
598 if (op1)
599 {
600 if (! (*insn_data[icode].operand[2].predicate) (xop1, xmode1)
601 && xmode1 != VOIDmode)
602 xop1 = copy_to_mode_reg (xmode1, xop1);
603
604 if (wide_op)
605 {
606 if (! (*insn_data[icode].operand[3].predicate) (wxop, wxmode)
607 && wxmode != VOIDmode)
608 wxop = copy_to_mode_reg (wxmode, wxop);
609
610 pat = GEN_FCN (icode) (temp, xop0, xop1, wxop);
611 }
612 else
613 pat = GEN_FCN (icode) (temp, xop0, xop1);
614 }
615 else
616 {
617 if (wide_op)
618 {
619 if (! (*insn_data[icode].operand[2].predicate) (wxop, wxmode)
620 && wxmode != VOIDmode)
621 wxop = copy_to_mode_reg (wxmode, wxop);
622
623 pat = GEN_FCN (icode) (temp, xop0, wxop);
624 }
625 else
626 pat = GEN_FCN (icode) (temp, xop0);
627 }
628
629 emit_insn (pat);
630 return temp;
631 }
632
633 /* Generate code to perform an operation specified by TERNARY_OPTAB
634 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
635
636 UNSIGNEDP is for the case where we have to widen the operands
637 to perform the operation. It says to use zero-extension.
638
639 If TARGET is nonzero, the value
640 is generated there, if it is convenient to do so.
641 In all cases an rtx is returned for the locus of the value;
642 this may or may not be TARGET. */
643
644 rtx
645 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
646 rtx op1, rtx op2, rtx target, int unsignedp)
647 {
648 int icode = (int) optab_handler (ternary_optab, mode);
649 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
650 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
651 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
652 rtx temp;
653 rtx pat;
654 rtx xop0 = op0, xop1 = op1, xop2 = op2;
655
656 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
657
658 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
659 temp = gen_reg_rtx (mode);
660 else
661 temp = target;
662
663 /* In case the insn wants input operands in modes different from
664 those of the actual operands, convert the operands. It would
665 seem that we don't need to convert CONST_INTs, but we do, so
666 that they're properly zero-extended, sign-extended or truncated
667 for their mode. */
668
669 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
670 xop0 = convert_modes (mode0,
671 GET_MODE (op0) != VOIDmode
672 ? GET_MODE (op0)
673 : mode,
674 xop0, unsignedp);
675
676 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
677 xop1 = convert_modes (mode1,
678 GET_MODE (op1) != VOIDmode
679 ? GET_MODE (op1)
680 : mode,
681 xop1, unsignedp);
682
683 if (GET_MODE (op2) != mode2 && mode2 != VOIDmode)
684 xop2 = convert_modes (mode2,
685 GET_MODE (op2) != VOIDmode
686 ? GET_MODE (op2)
687 : mode,
688 xop2, unsignedp);
689
690 /* Now, if insn's predicates don't allow our operands, put them into
691 pseudo regs. */
692
693 if (!insn_data[icode].operand[1].predicate (xop0, mode0)
694 && mode0 != VOIDmode)
695 xop0 = copy_to_mode_reg (mode0, xop0);
696
697 if (!insn_data[icode].operand[2].predicate (xop1, mode1)
698 && mode1 != VOIDmode)
699 xop1 = copy_to_mode_reg (mode1, xop1);
700
701 if (!insn_data[icode].operand[3].predicate (xop2, mode2)
702 && mode2 != VOIDmode)
703 xop2 = copy_to_mode_reg (mode2, xop2);
704
705 pat = GEN_FCN (icode) (temp, xop0, xop1, xop2);
706
707 emit_insn (pat);
708 return temp;
709 }
710
711
712 /* Like expand_binop, but return a constant rtx if the result can be
713 calculated at compile time. The arguments and return value are
714 otherwise the same as for expand_binop. */
715
716 static rtx
717 simplify_expand_binop (enum machine_mode mode, optab binoptab,
718 rtx op0, rtx op1, rtx target, int unsignedp,
719 enum optab_methods methods)
720 {
721 if (CONSTANT_P (op0) && CONSTANT_P (op1))
722 {
723 rtx x = simplify_binary_operation (binoptab->code, mode, op0, op1);
724
725 if (x)
726 return x;
727 }
728
729 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
730 }
731
732 /* Like simplify_expand_binop, but always put the result in TARGET.
733 Return true if the expansion succeeded. */
734
735 bool
736 force_expand_binop (enum machine_mode mode, optab binoptab,
737 rtx op0, rtx op1, rtx target, int unsignedp,
738 enum optab_methods methods)
739 {
740 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
741 target, unsignedp, methods);
742 if (x == 0)
743 return false;
744 if (x != target)
745 emit_move_insn (target, x);
746 return true;
747 }
748
749 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
750
751 rtx
752 expand_vec_shift_expr (sepops ops, rtx target)
753 {
754 enum insn_code icode;
755 rtx rtx_op1, rtx_op2;
756 enum machine_mode mode1;
757 enum machine_mode mode2;
758 enum machine_mode mode = TYPE_MODE (ops->type);
759 tree vec_oprnd = ops->op0;
760 tree shift_oprnd = ops->op1;
761 optab shift_optab;
762 rtx pat;
763
764 switch (ops->code)
765 {
766 case VEC_RSHIFT_EXPR:
767 shift_optab = vec_shr_optab;
768 break;
769 case VEC_LSHIFT_EXPR:
770 shift_optab = vec_shl_optab;
771 break;
772 default:
773 gcc_unreachable ();
774 }
775
776 icode = optab_handler (shift_optab, mode);
777 gcc_assert (icode != CODE_FOR_nothing);
778
779 mode1 = insn_data[icode].operand[1].mode;
780 mode2 = insn_data[icode].operand[2].mode;
781
782 rtx_op1 = expand_normal (vec_oprnd);
783 if (!(*insn_data[icode].operand[1].predicate) (rtx_op1, mode1)
784 && mode1 != VOIDmode)
785 rtx_op1 = force_reg (mode1, rtx_op1);
786
787 rtx_op2 = expand_normal (shift_oprnd);
788 if (!(*insn_data[icode].operand[2].predicate) (rtx_op2, mode2)
789 && mode2 != VOIDmode)
790 rtx_op2 = force_reg (mode2, rtx_op2);
791
792 if (!target
793 || ! (*insn_data[icode].operand[0].predicate) (target, mode))
794 target = gen_reg_rtx (mode);
795
796 /* Emit instruction */
797 pat = GEN_FCN (icode) (target, rtx_op1, rtx_op2);
798 gcc_assert (pat);
799 emit_insn (pat);
800
801 return target;
802 }
803
804 /* This subroutine of expand_doubleword_shift handles the cases in which
805 the effective shift value is >= BITS_PER_WORD. The arguments and return
806 value are the same as for the parent routine, except that SUPERWORD_OP1
807 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
808 INTO_TARGET may be null if the caller has decided to calculate it. */
809
810 static bool
811 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
812 rtx outof_target, rtx into_target,
813 int unsignedp, enum optab_methods methods)
814 {
815 if (into_target != 0)
816 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
817 into_target, unsignedp, methods))
818 return false;
819
820 if (outof_target != 0)
821 {
822 /* For a signed right shift, we must fill OUTOF_TARGET with copies
823 of the sign bit, otherwise we must fill it with zeros. */
824 if (binoptab != ashr_optab)
825 emit_move_insn (outof_target, CONST0_RTX (word_mode));
826 else
827 if (!force_expand_binop (word_mode, binoptab,
828 outof_input, GEN_INT (BITS_PER_WORD - 1),
829 outof_target, unsignedp, methods))
830 return false;
831 }
832 return true;
833 }
834
835 /* This subroutine of expand_doubleword_shift handles the cases in which
836 the effective shift value is < BITS_PER_WORD. The arguments and return
837 value are the same as for the parent routine. */
838
839 static bool
840 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
841 rtx outof_input, rtx into_input, rtx op1,
842 rtx outof_target, rtx into_target,
843 int unsignedp, enum optab_methods methods,
844 unsigned HOST_WIDE_INT shift_mask)
845 {
846 optab reverse_unsigned_shift, unsigned_shift;
847 rtx tmp, carries;
848
849 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
850 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
851
852 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
853 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
854 the opposite direction to BINOPTAB. */
855 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
856 {
857 carries = outof_input;
858 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
859 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
860 0, true, methods);
861 }
862 else
863 {
864 /* We must avoid shifting by BITS_PER_WORD bits since that is either
865 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
866 has unknown behavior. Do a single shift first, then shift by the
867 remainder. It's OK to use ~OP1 as the remainder if shift counts
868 are truncated to the mode size. */
869 carries = expand_binop (word_mode, reverse_unsigned_shift,
870 outof_input, const1_rtx, 0, unsignedp, methods);
871 if (shift_mask == BITS_PER_WORD - 1)
872 {
873 tmp = immed_double_const (-1, -1, op1_mode);
874 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
875 0, true, methods);
876 }
877 else
878 {
879 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
880 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
881 0, true, methods);
882 }
883 }
884 if (tmp == 0 || carries == 0)
885 return false;
886 carries = expand_binop (word_mode, reverse_unsigned_shift,
887 carries, tmp, 0, unsignedp, methods);
888 if (carries == 0)
889 return false;
890
891 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
892 so the result can go directly into INTO_TARGET if convenient. */
893 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
894 into_target, unsignedp, methods);
895 if (tmp == 0)
896 return false;
897
898 /* Now OR in the bits carried over from OUTOF_INPUT. */
899 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
900 into_target, unsignedp, methods))
901 return false;
902
903 /* Use a standard word_mode shift for the out-of half. */
904 if (outof_target != 0)
905 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
906 outof_target, unsignedp, methods))
907 return false;
908
909 return true;
910 }
911
912
913 #ifdef HAVE_conditional_move
914 /* Try implementing expand_doubleword_shift using conditional moves.
915 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
916 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
917 are the shift counts to use in the former and latter case. All other
918 arguments are the same as the parent routine. */
919
920 static bool
921 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
922 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
923 rtx outof_input, rtx into_input,
924 rtx subword_op1, rtx superword_op1,
925 rtx outof_target, rtx into_target,
926 int unsignedp, enum optab_methods methods,
927 unsigned HOST_WIDE_INT shift_mask)
928 {
929 rtx outof_superword, into_superword;
930
931 /* Put the superword version of the output into OUTOF_SUPERWORD and
932 INTO_SUPERWORD. */
933 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
934 if (outof_target != 0 && subword_op1 == superword_op1)
935 {
936 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
937 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
938 into_superword = outof_target;
939 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
940 outof_superword, 0, unsignedp, methods))
941 return false;
942 }
943 else
944 {
945 into_superword = gen_reg_rtx (word_mode);
946 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
947 outof_superword, into_superword,
948 unsignedp, methods))
949 return false;
950 }
951
952 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
953 if (!expand_subword_shift (op1_mode, binoptab,
954 outof_input, into_input, subword_op1,
955 outof_target, into_target,
956 unsignedp, methods, shift_mask))
957 return false;
958
959 /* Select between them. Do the INTO half first because INTO_SUPERWORD
960 might be the current value of OUTOF_TARGET. */
961 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
962 into_target, into_superword, word_mode, false))
963 return false;
964
965 if (outof_target != 0)
966 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
967 outof_target, outof_superword,
968 word_mode, false))
969 return false;
970
971 return true;
972 }
973 #endif
974
975 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
976 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
977 input operand; the shift moves bits in the direction OUTOF_INPUT->
978 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
979 of the target. OP1 is the shift count and OP1_MODE is its mode.
980 If OP1 is constant, it will have been truncated as appropriate
981 and is known to be nonzero.
982
983 If SHIFT_MASK is zero, the result of word shifts is undefined when the
984 shift count is outside the range [0, BITS_PER_WORD). This routine must
985 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
986
987 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
988 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
989 fill with zeros or sign bits as appropriate.
990
991 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
992 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
993 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
994 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
995 are undefined.
996
997 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
998 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
999 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
1000 function wants to calculate it itself.
1001
1002 Return true if the shift could be successfully synthesized. */
1003
1004 static bool
1005 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
1006 rtx outof_input, rtx into_input, rtx op1,
1007 rtx outof_target, rtx into_target,
1008 int unsignedp, enum optab_methods methods,
1009 unsigned HOST_WIDE_INT shift_mask)
1010 {
1011 rtx superword_op1, tmp, cmp1, cmp2;
1012 rtx subword_label, done_label;
1013 enum rtx_code cmp_code;
1014
1015 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1016 fill the result with sign or zero bits as appropriate. If so, the value
1017 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1018 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1019 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1020
1021 This isn't worthwhile for constant shifts since the optimizers will
1022 cope better with in-range shift counts. */
1023 if (shift_mask >= BITS_PER_WORD
1024 && outof_target != 0
1025 && !CONSTANT_P (op1))
1026 {
1027 if (!expand_doubleword_shift (op1_mode, binoptab,
1028 outof_input, into_input, op1,
1029 0, into_target,
1030 unsignedp, methods, shift_mask))
1031 return false;
1032 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1033 outof_target, unsignedp, methods))
1034 return false;
1035 return true;
1036 }
1037
1038 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1039 is true when the effective shift value is less than BITS_PER_WORD.
1040 Set SUPERWORD_OP1 to the shift count that should be used to shift
1041 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1042 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
1043 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1044 {
1045 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1046 is a subword shift count. */
1047 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1048 0, true, methods);
1049 cmp2 = CONST0_RTX (op1_mode);
1050 cmp_code = EQ;
1051 superword_op1 = op1;
1052 }
1053 else
1054 {
1055 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1056 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1057 0, true, methods);
1058 cmp2 = CONST0_RTX (op1_mode);
1059 cmp_code = LT;
1060 superword_op1 = cmp1;
1061 }
1062 if (cmp1 == 0)
1063 return false;
1064
1065 /* If we can compute the condition at compile time, pick the
1066 appropriate subroutine. */
1067 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1068 if (tmp != 0 && CONST_INT_P (tmp))
1069 {
1070 if (tmp == const0_rtx)
1071 return expand_superword_shift (binoptab, outof_input, superword_op1,
1072 outof_target, into_target,
1073 unsignedp, methods);
1074 else
1075 return expand_subword_shift (op1_mode, binoptab,
1076 outof_input, into_input, op1,
1077 outof_target, into_target,
1078 unsignedp, methods, shift_mask);
1079 }
1080
1081 #ifdef HAVE_conditional_move
1082 /* Try using conditional moves to generate straight-line code. */
1083 {
1084 rtx start = get_last_insn ();
1085 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1086 cmp_code, cmp1, cmp2,
1087 outof_input, into_input,
1088 op1, superword_op1,
1089 outof_target, into_target,
1090 unsignedp, methods, shift_mask))
1091 return true;
1092 delete_insns_since (start);
1093 }
1094 #endif
1095
1096 /* As a last resort, use branches to select the correct alternative. */
1097 subword_label = gen_label_rtx ();
1098 done_label = gen_label_rtx ();
1099
1100 NO_DEFER_POP;
1101 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1102 0, 0, subword_label, -1);
1103 OK_DEFER_POP;
1104
1105 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1106 outof_target, into_target,
1107 unsignedp, methods))
1108 return false;
1109
1110 emit_jump_insn (gen_jump (done_label));
1111 emit_barrier ();
1112 emit_label (subword_label);
1113
1114 if (!expand_subword_shift (op1_mode, binoptab,
1115 outof_input, into_input, op1,
1116 outof_target, into_target,
1117 unsignedp, methods, shift_mask))
1118 return false;
1119
1120 emit_label (done_label);
1121 return true;
1122 }
1123 \f
1124 /* Subroutine of expand_binop. Perform a double word multiplication of
1125 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1126 as the target's word_mode. This function return NULL_RTX if anything
1127 goes wrong, in which case it may have already emitted instructions
1128 which need to be deleted.
1129
1130 If we want to multiply two two-word values and have normal and widening
1131 multiplies of single-word values, we can do this with three smaller
1132 multiplications.
1133
1134 The multiplication proceeds as follows:
1135 _______________________
1136 [__op0_high_|__op0_low__]
1137 _______________________
1138 * [__op1_high_|__op1_low__]
1139 _______________________________________________
1140 _______________________
1141 (1) [__op0_low__*__op1_low__]
1142 _______________________
1143 (2a) [__op0_low__*__op1_high_]
1144 _______________________
1145 (2b) [__op0_high_*__op1_low__]
1146 _______________________
1147 (3) [__op0_high_*__op1_high_]
1148
1149
1150 This gives a 4-word result. Since we are only interested in the
1151 lower 2 words, partial result (3) and the upper words of (2a) and
1152 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1153 calculated using non-widening multiplication.
1154
1155 (1), however, needs to be calculated with an unsigned widening
1156 multiplication. If this operation is not directly supported we
1157 try using a signed widening multiplication and adjust the result.
1158 This adjustment works as follows:
1159
1160 If both operands are positive then no adjustment is needed.
1161
1162 If the operands have different signs, for example op0_low < 0 and
1163 op1_low >= 0, the instruction treats the most significant bit of
1164 op0_low as a sign bit instead of a bit with significance
1165 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1166 with 2**BITS_PER_WORD - op0_low, and two's complements the
1167 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1168 the result.
1169
1170 Similarly, if both operands are negative, we need to add
1171 (op0_low + op1_low) * 2**BITS_PER_WORD.
1172
1173 We use a trick to adjust quickly. We logically shift op0_low right
1174 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1175 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1176 logical shift exists, we do an arithmetic right shift and subtract
1177 the 0 or -1. */
1178
1179 static rtx
1180 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1181 bool umulp, enum optab_methods methods)
1182 {
1183 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1184 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1185 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1186 rtx product, adjust, product_high, temp;
1187
1188 rtx op0_high = operand_subword_force (op0, high, mode);
1189 rtx op0_low = operand_subword_force (op0, low, mode);
1190 rtx op1_high = operand_subword_force (op1, high, mode);
1191 rtx op1_low = operand_subword_force (op1, low, mode);
1192
1193 /* If we're using an unsigned multiply to directly compute the product
1194 of the low-order words of the operands and perform any required
1195 adjustments of the operands, we begin by trying two more multiplications
1196 and then computing the appropriate sum.
1197
1198 We have checked above that the required addition is provided.
1199 Full-word addition will normally always succeed, especially if
1200 it is provided at all, so we don't worry about its failure. The
1201 multiplication may well fail, however, so we do handle that. */
1202
1203 if (!umulp)
1204 {
1205 /* ??? This could be done with emit_store_flag where available. */
1206 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1207 NULL_RTX, 1, methods);
1208 if (temp)
1209 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1210 NULL_RTX, 0, OPTAB_DIRECT);
1211 else
1212 {
1213 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1214 NULL_RTX, 0, methods);
1215 if (!temp)
1216 return NULL_RTX;
1217 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1218 NULL_RTX, 0, OPTAB_DIRECT);
1219 }
1220
1221 if (!op0_high)
1222 return NULL_RTX;
1223 }
1224
1225 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1226 NULL_RTX, 0, OPTAB_DIRECT);
1227 if (!adjust)
1228 return NULL_RTX;
1229
1230 /* OP0_HIGH should now be dead. */
1231
1232 if (!umulp)
1233 {
1234 /* ??? This could be done with emit_store_flag where available. */
1235 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1236 NULL_RTX, 1, methods);
1237 if (temp)
1238 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1239 NULL_RTX, 0, OPTAB_DIRECT);
1240 else
1241 {
1242 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1243 NULL_RTX, 0, methods);
1244 if (!temp)
1245 return NULL_RTX;
1246 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1247 NULL_RTX, 0, OPTAB_DIRECT);
1248 }
1249
1250 if (!op1_high)
1251 return NULL_RTX;
1252 }
1253
1254 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1255 NULL_RTX, 0, OPTAB_DIRECT);
1256 if (!temp)
1257 return NULL_RTX;
1258
1259 /* OP1_HIGH should now be dead. */
1260
1261 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1262 NULL_RTX, 0, OPTAB_DIRECT);
1263
1264 if (target && !REG_P (target))
1265 target = NULL_RTX;
1266
1267 if (umulp)
1268 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1269 target, 1, OPTAB_DIRECT);
1270 else
1271 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1272 target, 1, OPTAB_DIRECT);
1273
1274 if (!product)
1275 return NULL_RTX;
1276
1277 product_high = operand_subword (product, high, 1, mode);
1278 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1279 NULL_RTX, 0, OPTAB_DIRECT);
1280 emit_move_insn (product_high, adjust);
1281 return product;
1282 }
1283 \f
1284 /* Wrapper around expand_binop which takes an rtx code to specify
1285 the operation to perform, not an optab pointer. All other
1286 arguments are the same. */
1287 rtx
1288 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1289 rtx op1, rtx target, int unsignedp,
1290 enum optab_methods methods)
1291 {
1292 optab binop = code_to_optab[(int) code];
1293 gcc_assert (binop);
1294
1295 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1296 }
1297
1298 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1299 binop. Order them according to commutative_operand_precedence and, if
1300 possible, try to put TARGET or a pseudo first. */
1301 static bool
1302 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1303 {
1304 int op0_prec = commutative_operand_precedence (op0);
1305 int op1_prec = commutative_operand_precedence (op1);
1306
1307 if (op0_prec < op1_prec)
1308 return true;
1309
1310 if (op0_prec > op1_prec)
1311 return false;
1312
1313 /* With equal precedence, both orders are ok, but it is better if the
1314 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1315 if (target == 0 || REG_P (target))
1316 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1317 else
1318 return rtx_equal_p (op1, target);
1319 }
1320
1321 /* Return true if BINOPTAB implements a shift operation. */
1322
1323 static bool
1324 shift_optab_p (optab binoptab)
1325 {
1326 switch (binoptab->code)
1327 {
1328 case ASHIFT:
1329 case SS_ASHIFT:
1330 case US_ASHIFT:
1331 case ASHIFTRT:
1332 case LSHIFTRT:
1333 case ROTATE:
1334 case ROTATERT:
1335 return true;
1336
1337 default:
1338 return false;
1339 }
1340 }
1341
1342 /* Return true if BINOPTAB implements a commutative binary operation. */
1343
1344 static bool
1345 commutative_optab_p (optab binoptab)
1346 {
1347 return (GET_RTX_CLASS (binoptab->code) == RTX_COMM_ARITH
1348 || binoptab == smul_widen_optab
1349 || binoptab == umul_widen_optab
1350 || binoptab == smul_highpart_optab
1351 || binoptab == umul_highpart_optab);
1352 }
1353
1354 /* X is to be used in mode MODE as an operand to BINOPTAB. If we're
1355 optimizing, and if the operand is a constant that costs more than
1356 1 instruction, force the constant into a register and return that
1357 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1358
1359 static rtx
1360 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1361 rtx x, bool unsignedp)
1362 {
1363 bool speed = optimize_insn_for_speed_p ();
1364
1365 if (mode != VOIDmode
1366 && optimize
1367 && CONSTANT_P (x)
1368 && rtx_cost (x, binoptab->code, speed) > rtx_cost (x, SET, speed))
1369 {
1370 if (CONST_INT_P (x))
1371 {
1372 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1373 if (intval != INTVAL (x))
1374 x = GEN_INT (intval);
1375 }
1376 else
1377 x = convert_modes (mode, VOIDmode, x, unsignedp);
1378 x = force_reg (mode, x);
1379 }
1380 return x;
1381 }
1382
1383 /* Helper function for expand_binop: handle the case where there
1384 is an insn that directly implements the indicated operation.
1385 Returns null if this is not possible. */
1386 static rtx
1387 expand_binop_directly (enum machine_mode mode, optab binoptab,
1388 rtx op0, rtx op1,
1389 rtx target, int unsignedp, enum optab_methods methods,
1390 rtx last)
1391 {
1392 int icode = (int) optab_handler (binoptab, mode);
1393 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
1394 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
1395 enum machine_mode tmp_mode;
1396 bool commutative_p;
1397 rtx pat;
1398 rtx xop0 = op0, xop1 = op1;
1399 rtx temp;
1400 rtx swap;
1401
1402 if (target)
1403 temp = target;
1404 else
1405 temp = gen_reg_rtx (mode);
1406
1407 /* If it is a commutative operator and the modes would match
1408 if we would swap the operands, we can save the conversions. */
1409 commutative_p = commutative_optab_p (binoptab);
1410 if (commutative_p
1411 && GET_MODE (xop0) != mode0 && GET_MODE (xop1) != mode1
1412 && GET_MODE (xop0) == mode1 && GET_MODE (xop1) == mode1)
1413 {
1414 swap = xop0;
1415 xop0 = xop1;
1416 xop1 = swap;
1417 }
1418
1419 /* If we are optimizing, force expensive constants into a register. */
1420 xop0 = avoid_expensive_constant (mode0, binoptab, xop0, unsignedp);
1421 if (!shift_optab_p (binoptab))
1422 xop1 = avoid_expensive_constant (mode1, binoptab, xop1, unsignedp);
1423
1424 /* In case the insn wants input operands in modes different from
1425 those of the actual operands, convert the operands. It would
1426 seem that we don't need to convert CONST_INTs, but we do, so
1427 that they're properly zero-extended, sign-extended or truncated
1428 for their mode. */
1429
1430 if (GET_MODE (xop0) != mode0 && mode0 != VOIDmode)
1431 xop0 = convert_modes (mode0,
1432 GET_MODE (xop0) != VOIDmode
1433 ? GET_MODE (xop0)
1434 : mode,
1435 xop0, unsignedp);
1436
1437 if (GET_MODE (xop1) != mode1 && mode1 != VOIDmode)
1438 xop1 = convert_modes (mode1,
1439 GET_MODE (xop1) != VOIDmode
1440 ? GET_MODE (xop1)
1441 : mode,
1442 xop1, unsignedp);
1443
1444 /* If operation is commutative,
1445 try to make the first operand a register.
1446 Even better, try to make it the same as the target.
1447 Also try to make the last operand a constant. */
1448 if (commutative_p
1449 && swap_commutative_operands_with_target (target, xop0, xop1))
1450 {
1451 swap = xop1;
1452 xop1 = xop0;
1453 xop0 = swap;
1454 }
1455
1456 /* Now, if insn's predicates don't allow our operands, put them into
1457 pseudo regs. */
1458
1459 if (!insn_data[icode].operand[1].predicate (xop0, mode0)
1460 && mode0 != VOIDmode)
1461 xop0 = copy_to_mode_reg (mode0, xop0);
1462
1463 if (!insn_data[icode].operand[2].predicate (xop1, mode1)
1464 && mode1 != VOIDmode)
1465 xop1 = copy_to_mode_reg (mode1, xop1);
1466
1467 if (binoptab == vec_pack_trunc_optab
1468 || binoptab == vec_pack_usat_optab
1469 || binoptab == vec_pack_ssat_optab
1470 || binoptab == vec_pack_ufix_trunc_optab
1471 || binoptab == vec_pack_sfix_trunc_optab)
1472 {
1473 /* The mode of the result is different then the mode of the
1474 arguments. */
1475 tmp_mode = insn_data[icode].operand[0].mode;
1476 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1477 return 0;
1478 }
1479 else
1480 tmp_mode = mode;
1481
1482 if (!insn_data[icode].operand[0].predicate (temp, tmp_mode))
1483 temp = gen_reg_rtx (tmp_mode);
1484
1485 pat = GEN_FCN (icode) (temp, xop0, xop1);
1486 if (pat)
1487 {
1488 /* If PAT is composed of more than one insn, try to add an appropriate
1489 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1490 operand, call expand_binop again, this time without a target. */
1491 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1492 && ! add_equal_note (pat, temp, binoptab->code, xop0, xop1))
1493 {
1494 delete_insns_since (last);
1495 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1496 unsignedp, methods);
1497 }
1498
1499 emit_insn (pat);
1500 return temp;
1501 }
1502
1503 delete_insns_since (last);
1504 return NULL_RTX;
1505 }
1506
1507 /* Generate code to perform an operation specified by BINOPTAB
1508 on operands OP0 and OP1, with result having machine-mode MODE.
1509
1510 UNSIGNEDP is for the case where we have to widen the operands
1511 to perform the operation. It says to use zero-extension.
1512
1513 If TARGET is nonzero, the value
1514 is generated there, if it is convenient to do so.
1515 In all cases an rtx is returned for the locus of the value;
1516 this may or may not be TARGET. */
1517
1518 rtx
1519 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1520 rtx target, int unsignedp, enum optab_methods methods)
1521 {
1522 enum optab_methods next_methods
1523 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1524 ? OPTAB_WIDEN : methods);
1525 enum mode_class mclass;
1526 enum machine_mode wider_mode;
1527 rtx libfunc;
1528 rtx temp;
1529 rtx entry_last = get_last_insn ();
1530 rtx last;
1531
1532 mclass = GET_MODE_CLASS (mode);
1533
1534 /* If subtracting an integer constant, convert this into an addition of
1535 the negated constant. */
1536
1537 if (binoptab == sub_optab && CONST_INT_P (op1))
1538 {
1539 op1 = negate_rtx (mode, op1);
1540 binoptab = add_optab;
1541 }
1542
1543 /* Record where to delete back to if we backtrack. */
1544 last = get_last_insn ();
1545
1546 /* If we can do it with a three-operand insn, do so. */
1547
1548 if (methods != OPTAB_MUST_WIDEN
1549 && optab_handler (binoptab, mode) != CODE_FOR_nothing)
1550 {
1551 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1552 unsignedp, methods, last);
1553 if (temp)
1554 return temp;
1555 }
1556
1557 /* If we were trying to rotate, and that didn't work, try rotating
1558 the other direction before falling back to shifts and bitwise-or. */
1559 if (((binoptab == rotl_optab
1560 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1561 || (binoptab == rotr_optab
1562 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1563 && mclass == MODE_INT)
1564 {
1565 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1566 rtx newop1;
1567 unsigned int bits = GET_MODE_BITSIZE (mode);
1568
1569 if (CONST_INT_P (op1))
1570 newop1 = GEN_INT (bits - INTVAL (op1));
1571 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1572 newop1 = negate_rtx (GET_MODE (op1), op1);
1573 else
1574 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1575 GEN_INT (bits), op1,
1576 NULL_RTX, unsignedp, OPTAB_DIRECT);
1577
1578 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1579 target, unsignedp, methods, last);
1580 if (temp)
1581 return temp;
1582 }
1583
1584 /* If this is a multiply, see if we can do a widening operation that
1585 takes operands of this mode and makes a wider mode. */
1586
1587 if (binoptab == smul_optab
1588 && GET_MODE_WIDER_MODE (mode) != VOIDmode
1589 && (optab_handler ((unsignedp ? umul_widen_optab : smul_widen_optab),
1590 GET_MODE_WIDER_MODE (mode))
1591 != CODE_FOR_nothing))
1592 {
1593 temp = expand_binop (GET_MODE_WIDER_MODE (mode),
1594 unsignedp ? umul_widen_optab : smul_widen_optab,
1595 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1596
1597 if (temp != 0)
1598 {
1599 if (GET_MODE_CLASS (mode) == MODE_INT
1600 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1601 GET_MODE_BITSIZE (GET_MODE (temp))))
1602 return gen_lowpart (mode, temp);
1603 else
1604 return convert_to_mode (mode, temp, unsignedp);
1605 }
1606 }
1607
1608 /* Look for a wider mode of the same class for which we think we
1609 can open-code the operation. Check for a widening multiply at the
1610 wider mode as well. */
1611
1612 if (CLASS_HAS_WIDER_MODES_P (mclass)
1613 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1614 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1615 wider_mode != VOIDmode;
1616 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1617 {
1618 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1619 || (binoptab == smul_optab
1620 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1621 && (optab_handler ((unsignedp ? umul_widen_optab
1622 : smul_widen_optab),
1623 GET_MODE_WIDER_MODE (wider_mode))
1624 != CODE_FOR_nothing)))
1625 {
1626 rtx xop0 = op0, xop1 = op1;
1627 int no_extend = 0;
1628
1629 /* For certain integer operations, we need not actually extend
1630 the narrow operands, as long as we will truncate
1631 the results to the same narrowness. */
1632
1633 if ((binoptab == ior_optab || binoptab == and_optab
1634 || binoptab == xor_optab
1635 || binoptab == add_optab || binoptab == sub_optab
1636 || binoptab == smul_optab || binoptab == ashl_optab)
1637 && mclass == MODE_INT)
1638 {
1639 no_extend = 1;
1640 xop0 = avoid_expensive_constant (mode, binoptab,
1641 xop0, unsignedp);
1642 if (binoptab != ashl_optab)
1643 xop1 = avoid_expensive_constant (mode, binoptab,
1644 xop1, unsignedp);
1645 }
1646
1647 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1648
1649 /* The second operand of a shift must always be extended. */
1650 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1651 no_extend && binoptab != ashl_optab);
1652
1653 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1654 unsignedp, OPTAB_DIRECT);
1655 if (temp)
1656 {
1657 if (mclass != MODE_INT
1658 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1659 GET_MODE_BITSIZE (wider_mode)))
1660 {
1661 if (target == 0)
1662 target = gen_reg_rtx (mode);
1663 convert_move (target, temp, 0);
1664 return target;
1665 }
1666 else
1667 return gen_lowpart (mode, temp);
1668 }
1669 else
1670 delete_insns_since (last);
1671 }
1672 }
1673
1674 /* If operation is commutative,
1675 try to make the first operand a register.
1676 Even better, try to make it the same as the target.
1677 Also try to make the last operand a constant. */
1678 if (commutative_optab_p (binoptab)
1679 && swap_commutative_operands_with_target (target, op0, op1))
1680 {
1681 temp = op1;
1682 op1 = op0;
1683 op0 = temp;
1684 }
1685
1686 /* These can be done a word at a time. */
1687 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1688 && mclass == MODE_INT
1689 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1690 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1691 {
1692 int i;
1693 rtx insns;
1694
1695 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1696 won't be accurate, so use a new target. */
1697 if (target == 0 || target == op0 || target == op1)
1698 target = gen_reg_rtx (mode);
1699
1700 start_sequence ();
1701
1702 /* Do the actual arithmetic. */
1703 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1704 {
1705 rtx target_piece = operand_subword (target, i, 1, mode);
1706 rtx x = expand_binop (word_mode, binoptab,
1707 operand_subword_force (op0, i, mode),
1708 operand_subword_force (op1, i, mode),
1709 target_piece, unsignedp, next_methods);
1710
1711 if (x == 0)
1712 break;
1713
1714 if (target_piece != x)
1715 emit_move_insn (target_piece, x);
1716 }
1717
1718 insns = get_insns ();
1719 end_sequence ();
1720
1721 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1722 {
1723 emit_insn (insns);
1724 return target;
1725 }
1726 }
1727
1728 /* Synthesize double word shifts from single word shifts. */
1729 if ((binoptab == lshr_optab || binoptab == ashl_optab
1730 || binoptab == ashr_optab)
1731 && mclass == MODE_INT
1732 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1733 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1734 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1735 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1736 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1737 {
1738 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1739 enum machine_mode op1_mode;
1740
1741 double_shift_mask = targetm.shift_truncation_mask (mode);
1742 shift_mask = targetm.shift_truncation_mask (word_mode);
1743 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1744
1745 /* Apply the truncation to constant shifts. */
1746 if (double_shift_mask > 0 && CONST_INT_P (op1))
1747 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1748
1749 if (op1 == CONST0_RTX (op1_mode))
1750 return op0;
1751
1752 /* Make sure that this is a combination that expand_doubleword_shift
1753 can handle. See the comments there for details. */
1754 if (double_shift_mask == 0
1755 || (shift_mask == BITS_PER_WORD - 1
1756 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1757 {
1758 rtx insns;
1759 rtx into_target, outof_target;
1760 rtx into_input, outof_input;
1761 int left_shift, outof_word;
1762
1763 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1764 won't be accurate, so use a new target. */
1765 if (target == 0 || target == op0 || target == op1)
1766 target = gen_reg_rtx (mode);
1767
1768 start_sequence ();
1769
1770 /* OUTOF_* is the word we are shifting bits away from, and
1771 INTO_* is the word that we are shifting bits towards, thus
1772 they differ depending on the direction of the shift and
1773 WORDS_BIG_ENDIAN. */
1774
1775 left_shift = binoptab == ashl_optab;
1776 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1777
1778 outof_target = operand_subword (target, outof_word, 1, mode);
1779 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1780
1781 outof_input = operand_subword_force (op0, outof_word, mode);
1782 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1783
1784 if (expand_doubleword_shift (op1_mode, binoptab,
1785 outof_input, into_input, op1,
1786 outof_target, into_target,
1787 unsignedp, next_methods, shift_mask))
1788 {
1789 insns = get_insns ();
1790 end_sequence ();
1791
1792 emit_insn (insns);
1793 return target;
1794 }
1795 end_sequence ();
1796 }
1797 }
1798
1799 /* Synthesize double word rotates from single word shifts. */
1800 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1801 && mclass == MODE_INT
1802 && CONST_INT_P (op1)
1803 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1804 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1805 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1806 {
1807 rtx insns;
1808 rtx into_target, outof_target;
1809 rtx into_input, outof_input;
1810 rtx inter;
1811 int shift_count, left_shift, outof_word;
1812
1813 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1814 won't be accurate, so use a new target. Do this also if target is not
1815 a REG, first because having a register instead may open optimization
1816 opportunities, and second because if target and op0 happen to be MEMs
1817 designating the same location, we would risk clobbering it too early
1818 in the code sequence we generate below. */
1819 if (target == 0 || target == op0 || target == op1 || ! REG_P (target))
1820 target = gen_reg_rtx (mode);
1821
1822 start_sequence ();
1823
1824 shift_count = INTVAL (op1);
1825
1826 /* OUTOF_* is the word we are shifting bits away from, and
1827 INTO_* is the word that we are shifting bits towards, thus
1828 they differ depending on the direction of the shift and
1829 WORDS_BIG_ENDIAN. */
1830
1831 left_shift = (binoptab == rotl_optab);
1832 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1833
1834 outof_target = operand_subword (target, outof_word, 1, mode);
1835 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1836
1837 outof_input = operand_subword_force (op0, outof_word, mode);
1838 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1839
1840 if (shift_count == BITS_PER_WORD)
1841 {
1842 /* This is just a word swap. */
1843 emit_move_insn (outof_target, into_input);
1844 emit_move_insn (into_target, outof_input);
1845 inter = const0_rtx;
1846 }
1847 else
1848 {
1849 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1850 rtx first_shift_count, second_shift_count;
1851 optab reverse_unsigned_shift, unsigned_shift;
1852
1853 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1854 ? lshr_optab : ashl_optab);
1855
1856 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1857 ? ashl_optab : lshr_optab);
1858
1859 if (shift_count > BITS_PER_WORD)
1860 {
1861 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1862 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1863 }
1864 else
1865 {
1866 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1867 second_shift_count = GEN_INT (shift_count);
1868 }
1869
1870 into_temp1 = expand_binop (word_mode, unsigned_shift,
1871 outof_input, first_shift_count,
1872 NULL_RTX, unsignedp, next_methods);
1873 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1874 into_input, second_shift_count,
1875 NULL_RTX, unsignedp, next_methods);
1876
1877 if (into_temp1 != 0 && into_temp2 != 0)
1878 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1879 into_target, unsignedp, next_methods);
1880 else
1881 inter = 0;
1882
1883 if (inter != 0 && inter != into_target)
1884 emit_move_insn (into_target, inter);
1885
1886 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1887 into_input, first_shift_count,
1888 NULL_RTX, unsignedp, next_methods);
1889 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1890 outof_input, second_shift_count,
1891 NULL_RTX, unsignedp, next_methods);
1892
1893 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1894 inter = expand_binop (word_mode, ior_optab,
1895 outof_temp1, outof_temp2,
1896 outof_target, unsignedp, next_methods);
1897
1898 if (inter != 0 && inter != outof_target)
1899 emit_move_insn (outof_target, inter);
1900 }
1901
1902 insns = get_insns ();
1903 end_sequence ();
1904
1905 if (inter != 0)
1906 {
1907 emit_insn (insns);
1908 return target;
1909 }
1910 }
1911
1912 /* These can be done a word at a time by propagating carries. */
1913 if ((binoptab == add_optab || binoptab == sub_optab)
1914 && mclass == MODE_INT
1915 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1916 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1917 {
1918 unsigned int i;
1919 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1920 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1921 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1922 rtx xop0, xop1, xtarget;
1923
1924 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1925 value is one of those, use it. Otherwise, use 1 since it is the
1926 one easiest to get. */
1927 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1928 int normalizep = STORE_FLAG_VALUE;
1929 #else
1930 int normalizep = 1;
1931 #endif
1932
1933 /* Prepare the operands. */
1934 xop0 = force_reg (mode, op0);
1935 xop1 = force_reg (mode, op1);
1936
1937 xtarget = gen_reg_rtx (mode);
1938
1939 if (target == 0 || !REG_P (target))
1940 target = xtarget;
1941
1942 /* Indicate for flow that the entire target reg is being set. */
1943 if (REG_P (target))
1944 emit_clobber (xtarget);
1945
1946 /* Do the actual arithmetic. */
1947 for (i = 0; i < nwords; i++)
1948 {
1949 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1950 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1951 rtx op0_piece = operand_subword_force (xop0, index, mode);
1952 rtx op1_piece = operand_subword_force (xop1, index, mode);
1953 rtx x;
1954
1955 /* Main add/subtract of the input operands. */
1956 x = expand_binop (word_mode, binoptab,
1957 op0_piece, op1_piece,
1958 target_piece, unsignedp, next_methods);
1959 if (x == 0)
1960 break;
1961
1962 if (i + 1 < nwords)
1963 {
1964 /* Store carry from main add/subtract. */
1965 carry_out = gen_reg_rtx (word_mode);
1966 carry_out = emit_store_flag_force (carry_out,
1967 (binoptab == add_optab
1968 ? LT : GT),
1969 x, op0_piece,
1970 word_mode, 1, normalizep);
1971 }
1972
1973 if (i > 0)
1974 {
1975 rtx newx;
1976
1977 /* Add/subtract previous carry to main result. */
1978 newx = expand_binop (word_mode,
1979 normalizep == 1 ? binoptab : otheroptab,
1980 x, carry_in,
1981 NULL_RTX, 1, next_methods);
1982
1983 if (i + 1 < nwords)
1984 {
1985 /* Get out carry from adding/subtracting carry in. */
1986 rtx carry_tmp = gen_reg_rtx (word_mode);
1987 carry_tmp = emit_store_flag_force (carry_tmp,
1988 (binoptab == add_optab
1989 ? LT : GT),
1990 newx, x,
1991 word_mode, 1, normalizep);
1992
1993 /* Logical-ior the two poss. carry together. */
1994 carry_out = expand_binop (word_mode, ior_optab,
1995 carry_out, carry_tmp,
1996 carry_out, 0, next_methods);
1997 if (carry_out == 0)
1998 break;
1999 }
2000 emit_move_insn (target_piece, newx);
2001 }
2002 else
2003 {
2004 if (x != target_piece)
2005 emit_move_insn (target_piece, x);
2006 }
2007
2008 carry_in = carry_out;
2009 }
2010
2011 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2012 {
2013 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2014 || ! rtx_equal_p (target, xtarget))
2015 {
2016 rtx temp = emit_move_insn (target, xtarget);
2017
2018 set_unique_reg_note (temp,
2019 REG_EQUAL,
2020 gen_rtx_fmt_ee (binoptab->code, mode,
2021 copy_rtx (xop0),
2022 copy_rtx (xop1)));
2023 }
2024 else
2025 target = xtarget;
2026
2027 return target;
2028 }
2029
2030 else
2031 delete_insns_since (last);
2032 }
2033
2034 /* Attempt to synthesize double word multiplies using a sequence of word
2035 mode multiplications. We first attempt to generate a sequence using a
2036 more efficient unsigned widening multiply, and if that fails we then
2037 try using a signed widening multiply. */
2038
2039 if (binoptab == smul_optab
2040 && mclass == MODE_INT
2041 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2042 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2043 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2044 {
2045 rtx product = NULL_RTX;
2046
2047 if (optab_handler (umul_widen_optab, mode) != CODE_FOR_nothing)
2048 {
2049 product = expand_doubleword_mult (mode, op0, op1, target,
2050 true, methods);
2051 if (!product)
2052 delete_insns_since (last);
2053 }
2054
2055 if (product == NULL_RTX
2056 && optab_handler (smul_widen_optab, mode) != CODE_FOR_nothing)
2057 {
2058 product = expand_doubleword_mult (mode, op0, op1, target,
2059 false, methods);
2060 if (!product)
2061 delete_insns_since (last);
2062 }
2063
2064 if (product != NULL_RTX)
2065 {
2066 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2067 {
2068 temp = emit_move_insn (target ? target : product, product);
2069 set_unique_reg_note (temp,
2070 REG_EQUAL,
2071 gen_rtx_fmt_ee (MULT, mode,
2072 copy_rtx (op0),
2073 copy_rtx (op1)));
2074 }
2075 return product;
2076 }
2077 }
2078
2079 /* It can't be open-coded in this mode.
2080 Use a library call if one is available and caller says that's ok. */
2081
2082 libfunc = optab_libfunc (binoptab, mode);
2083 if (libfunc
2084 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2085 {
2086 rtx insns;
2087 rtx op1x = op1;
2088 enum machine_mode op1_mode = mode;
2089 rtx value;
2090
2091 start_sequence ();
2092
2093 if (shift_optab_p (binoptab))
2094 {
2095 op1_mode = targetm.libgcc_shift_count_mode ();
2096 /* Specify unsigned here,
2097 since negative shift counts are meaningless. */
2098 op1x = convert_to_mode (op1_mode, op1, 1);
2099 }
2100
2101 if (GET_MODE (op0) != VOIDmode
2102 && GET_MODE (op0) != mode)
2103 op0 = convert_to_mode (mode, op0, unsignedp);
2104
2105 /* Pass 1 for NO_QUEUE so we don't lose any increments
2106 if the libcall is cse'd or moved. */
2107 value = emit_library_call_value (libfunc,
2108 NULL_RTX, LCT_CONST, mode, 2,
2109 op0, mode, op1x, op1_mode);
2110
2111 insns = get_insns ();
2112 end_sequence ();
2113
2114 target = gen_reg_rtx (mode);
2115 emit_libcall_block (insns, target, value,
2116 gen_rtx_fmt_ee (binoptab->code, mode, op0, op1));
2117
2118 return target;
2119 }
2120
2121 delete_insns_since (last);
2122
2123 /* It can't be done in this mode. Can we do it in a wider mode? */
2124
2125 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2126 || methods == OPTAB_MUST_WIDEN))
2127 {
2128 /* Caller says, don't even try. */
2129 delete_insns_since (entry_last);
2130 return 0;
2131 }
2132
2133 /* Compute the value of METHODS to pass to recursive calls.
2134 Don't allow widening to be tried recursively. */
2135
2136 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2137
2138 /* Look for a wider mode of the same class for which it appears we can do
2139 the operation. */
2140
2141 if (CLASS_HAS_WIDER_MODES_P (mclass))
2142 {
2143 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2144 wider_mode != VOIDmode;
2145 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2146 {
2147 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
2148 || (methods == OPTAB_LIB
2149 && optab_libfunc (binoptab, wider_mode)))
2150 {
2151 rtx xop0 = op0, xop1 = op1;
2152 int no_extend = 0;
2153
2154 /* For certain integer operations, we need not actually extend
2155 the narrow operands, as long as we will truncate
2156 the results to the same narrowness. */
2157
2158 if ((binoptab == ior_optab || binoptab == and_optab
2159 || binoptab == xor_optab
2160 || binoptab == add_optab || binoptab == sub_optab
2161 || binoptab == smul_optab || binoptab == ashl_optab)
2162 && mclass == MODE_INT)
2163 no_extend = 1;
2164
2165 xop0 = widen_operand (xop0, wider_mode, mode,
2166 unsignedp, no_extend);
2167
2168 /* The second operand of a shift must always be extended. */
2169 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2170 no_extend && binoptab != ashl_optab);
2171
2172 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2173 unsignedp, methods);
2174 if (temp)
2175 {
2176 if (mclass != MODE_INT
2177 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
2178 GET_MODE_BITSIZE (wider_mode)))
2179 {
2180 if (target == 0)
2181 target = gen_reg_rtx (mode);
2182 convert_move (target, temp, 0);
2183 return target;
2184 }
2185 else
2186 return gen_lowpart (mode, temp);
2187 }
2188 else
2189 delete_insns_since (last);
2190 }
2191 }
2192 }
2193
2194 delete_insns_since (entry_last);
2195 return 0;
2196 }
2197 \f
2198 /* Expand a binary operator which has both signed and unsigned forms.
2199 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2200 signed operations.
2201
2202 If we widen unsigned operands, we may use a signed wider operation instead
2203 of an unsigned wider operation, since the result would be the same. */
2204
2205 rtx
2206 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2207 rtx op0, rtx op1, rtx target, int unsignedp,
2208 enum optab_methods methods)
2209 {
2210 rtx temp;
2211 optab direct_optab = unsignedp ? uoptab : soptab;
2212 struct optab_d wide_soptab;
2213
2214 /* Do it without widening, if possible. */
2215 temp = expand_binop (mode, direct_optab, op0, op1, target,
2216 unsignedp, OPTAB_DIRECT);
2217 if (temp || methods == OPTAB_DIRECT)
2218 return temp;
2219
2220 /* Try widening to a signed int. Make a fake signed optab that
2221 hides any signed insn for direct use. */
2222 wide_soptab = *soptab;
2223 set_optab_handler (&wide_soptab, mode, CODE_FOR_nothing);
2224 /* We don't want to generate new hash table entries from this fake
2225 optab. */
2226 wide_soptab.libcall_gen = NULL;
2227
2228 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2229 unsignedp, OPTAB_WIDEN);
2230
2231 /* For unsigned operands, try widening to an unsigned int. */
2232 if (temp == 0 && unsignedp)
2233 temp = expand_binop (mode, uoptab, op0, op1, target,
2234 unsignedp, OPTAB_WIDEN);
2235 if (temp || methods == OPTAB_WIDEN)
2236 return temp;
2237
2238 /* Use the right width libcall if that exists. */
2239 temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
2240 if (temp || methods == OPTAB_LIB)
2241 return temp;
2242
2243 /* Must widen and use a libcall, use either signed or unsigned. */
2244 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2245 unsignedp, methods);
2246 if (temp != 0)
2247 return temp;
2248 if (unsignedp)
2249 return expand_binop (mode, uoptab, op0, op1, target,
2250 unsignedp, methods);
2251 return 0;
2252 }
2253 \f
2254 /* Generate code to perform an operation specified by UNOPPTAB
2255 on operand OP0, with two results to TARG0 and TARG1.
2256 We assume that the order of the operands for the instruction
2257 is TARG0, TARG1, OP0.
2258
2259 Either TARG0 or TARG1 may be zero, but what that means is that
2260 the result is not actually wanted. We will generate it into
2261 a dummy pseudo-reg and discard it. They may not both be zero.
2262
2263 Returns 1 if this operation can be performed; 0 if not. */
2264
2265 int
2266 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2267 int unsignedp)
2268 {
2269 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2270 enum mode_class mclass;
2271 enum machine_mode wider_mode;
2272 rtx entry_last = get_last_insn ();
2273 rtx last;
2274
2275 mclass = GET_MODE_CLASS (mode);
2276
2277 if (!targ0)
2278 targ0 = gen_reg_rtx (mode);
2279 if (!targ1)
2280 targ1 = gen_reg_rtx (mode);
2281
2282 /* Record where to go back to if we fail. */
2283 last = get_last_insn ();
2284
2285 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2286 {
2287 int icode = (int) optab_handler (unoptab, mode);
2288 enum machine_mode mode0 = insn_data[icode].operand[2].mode;
2289 rtx pat;
2290 rtx xop0 = op0;
2291
2292 if (GET_MODE (xop0) != VOIDmode
2293 && GET_MODE (xop0) != mode0)
2294 xop0 = convert_to_mode (mode0, xop0, unsignedp);
2295
2296 /* Now, if insn doesn't accept these operands, put them into pseudos. */
2297 if (!insn_data[icode].operand[2].predicate (xop0, mode0))
2298 xop0 = copy_to_mode_reg (mode0, xop0);
2299
2300 /* We could handle this, but we should always be called with a pseudo
2301 for our targets and all insns should take them as outputs. */
2302 gcc_assert (insn_data[icode].operand[0].predicate (targ0, mode));
2303 gcc_assert (insn_data[icode].operand[1].predicate (targ1, mode));
2304
2305 pat = GEN_FCN (icode) (targ0, targ1, xop0);
2306 if (pat)
2307 {
2308 emit_insn (pat);
2309 return 1;
2310 }
2311 else
2312 delete_insns_since (last);
2313 }
2314
2315 /* It can't be done in this mode. Can we do it in a wider mode? */
2316
2317 if (CLASS_HAS_WIDER_MODES_P (mclass))
2318 {
2319 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2320 wider_mode != VOIDmode;
2321 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2322 {
2323 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2324 {
2325 rtx t0 = gen_reg_rtx (wider_mode);
2326 rtx t1 = gen_reg_rtx (wider_mode);
2327 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2328
2329 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2330 {
2331 convert_move (targ0, t0, unsignedp);
2332 convert_move (targ1, t1, unsignedp);
2333 return 1;
2334 }
2335 else
2336 delete_insns_since (last);
2337 }
2338 }
2339 }
2340
2341 delete_insns_since (entry_last);
2342 return 0;
2343 }
2344 \f
2345 /* Generate code to perform an operation specified by BINOPTAB
2346 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2347 We assume that the order of the operands for the instruction
2348 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2349 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2350
2351 Either TARG0 or TARG1 may be zero, but what that means is that
2352 the result is not actually wanted. We will generate it into
2353 a dummy pseudo-reg and discard it. They may not both be zero.
2354
2355 Returns 1 if this operation can be performed; 0 if not. */
2356
2357 int
2358 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2359 int unsignedp)
2360 {
2361 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2362 enum mode_class mclass;
2363 enum machine_mode wider_mode;
2364 rtx entry_last = get_last_insn ();
2365 rtx last;
2366
2367 mclass = GET_MODE_CLASS (mode);
2368
2369 if (!targ0)
2370 targ0 = gen_reg_rtx (mode);
2371 if (!targ1)
2372 targ1 = gen_reg_rtx (mode);
2373
2374 /* Record where to go back to if we fail. */
2375 last = get_last_insn ();
2376
2377 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2378 {
2379 int icode = (int) optab_handler (binoptab, mode);
2380 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2381 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2382 rtx pat;
2383 rtx xop0 = op0, xop1 = op1;
2384
2385 /* If we are optimizing, force expensive constants into a register. */
2386 xop0 = avoid_expensive_constant (mode0, binoptab, xop0, unsignedp);
2387 xop1 = avoid_expensive_constant (mode1, binoptab, xop1, unsignedp);
2388
2389 /* In case the insn wants input operands in modes different from
2390 those of the actual operands, convert the operands. It would
2391 seem that we don't need to convert CONST_INTs, but we do, so
2392 that they're properly zero-extended, sign-extended or truncated
2393 for their mode. */
2394
2395 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
2396 xop0 = convert_modes (mode0,
2397 GET_MODE (op0) != VOIDmode
2398 ? GET_MODE (op0)
2399 : mode,
2400 xop0, unsignedp);
2401
2402 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
2403 xop1 = convert_modes (mode1,
2404 GET_MODE (op1) != VOIDmode
2405 ? GET_MODE (op1)
2406 : mode,
2407 xop1, unsignedp);
2408
2409 /* Now, if insn doesn't accept these operands, put them into pseudos. */
2410 if (!insn_data[icode].operand[1].predicate (xop0, mode0))
2411 xop0 = copy_to_mode_reg (mode0, xop0);
2412
2413 if (!insn_data[icode].operand[2].predicate (xop1, mode1))
2414 xop1 = copy_to_mode_reg (mode1, xop1);
2415
2416 /* We could handle this, but we should always be called with a pseudo
2417 for our targets and all insns should take them as outputs. */
2418 gcc_assert (insn_data[icode].operand[0].predicate (targ0, mode));
2419 gcc_assert (insn_data[icode].operand[3].predicate (targ1, mode));
2420
2421 pat = GEN_FCN (icode) (targ0, xop0, xop1, targ1);
2422 if (pat)
2423 {
2424 emit_insn (pat);
2425 return 1;
2426 }
2427 else
2428 delete_insns_since (last);
2429 }
2430
2431 /* It can't be done in this mode. Can we do it in a wider mode? */
2432
2433 if (CLASS_HAS_WIDER_MODES_P (mclass))
2434 {
2435 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2436 wider_mode != VOIDmode;
2437 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2438 {
2439 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2440 {
2441 rtx t0 = gen_reg_rtx (wider_mode);
2442 rtx t1 = gen_reg_rtx (wider_mode);
2443 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2444 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2445
2446 if (expand_twoval_binop (binoptab, cop0, cop1,
2447 t0, t1, unsignedp))
2448 {
2449 convert_move (targ0, t0, unsignedp);
2450 convert_move (targ1, t1, unsignedp);
2451 return 1;
2452 }
2453 else
2454 delete_insns_since (last);
2455 }
2456 }
2457 }
2458
2459 delete_insns_since (entry_last);
2460 return 0;
2461 }
2462
2463 /* Expand the two-valued library call indicated by BINOPTAB, but
2464 preserve only one of the values. If TARG0 is non-NULL, the first
2465 value is placed into TARG0; otherwise the second value is placed
2466 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2467 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2468 This routine assumes that the value returned by the library call is
2469 as if the return value was of an integral mode twice as wide as the
2470 mode of OP0. Returns 1 if the call was successful. */
2471
2472 bool
2473 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2474 rtx targ0, rtx targ1, enum rtx_code code)
2475 {
2476 enum machine_mode mode;
2477 enum machine_mode libval_mode;
2478 rtx libval;
2479 rtx insns;
2480 rtx libfunc;
2481
2482 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2483 gcc_assert (!targ0 != !targ1);
2484
2485 mode = GET_MODE (op0);
2486 libfunc = optab_libfunc (binoptab, mode);
2487 if (!libfunc)
2488 return false;
2489
2490 /* The value returned by the library function will have twice as
2491 many bits as the nominal MODE. */
2492 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2493 MODE_INT);
2494 start_sequence ();
2495 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2496 libval_mode, 2,
2497 op0, mode,
2498 op1, mode);
2499 /* Get the part of VAL containing the value that we want. */
2500 libval = simplify_gen_subreg (mode, libval, libval_mode,
2501 targ0 ? 0 : GET_MODE_SIZE (mode));
2502 insns = get_insns ();
2503 end_sequence ();
2504 /* Move the into the desired location. */
2505 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2506 gen_rtx_fmt_ee (code, mode, op0, op1));
2507
2508 return true;
2509 }
2510
2511 \f
2512 /* Wrapper around expand_unop which takes an rtx code to specify
2513 the operation to perform, not an optab pointer. All other
2514 arguments are the same. */
2515 rtx
2516 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2517 rtx target, int unsignedp)
2518 {
2519 optab unop = code_to_optab[(int) code];
2520 gcc_assert (unop);
2521
2522 return expand_unop (mode, unop, op0, target, unsignedp);
2523 }
2524
2525 /* Try calculating
2526 (clz:narrow x)
2527 as
2528 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)). */
2529 static rtx
2530 widen_clz (enum machine_mode mode, rtx op0, rtx target)
2531 {
2532 enum mode_class mclass = GET_MODE_CLASS (mode);
2533 if (CLASS_HAS_WIDER_MODES_P (mclass))
2534 {
2535 enum machine_mode wider_mode;
2536 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2537 wider_mode != VOIDmode;
2538 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2539 {
2540 if (optab_handler (clz_optab, wider_mode) != CODE_FOR_nothing)
2541 {
2542 rtx xop0, temp, last;
2543
2544 last = get_last_insn ();
2545
2546 if (target == 0)
2547 target = gen_reg_rtx (mode);
2548 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2549 temp = expand_unop (wider_mode, clz_optab, xop0, NULL_RTX, true);
2550 if (temp != 0)
2551 temp = expand_binop (wider_mode, sub_optab, temp,
2552 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2553 - GET_MODE_BITSIZE (mode)),
2554 target, true, OPTAB_DIRECT);
2555 if (temp == 0)
2556 delete_insns_since (last);
2557
2558 return temp;
2559 }
2560 }
2561 }
2562 return 0;
2563 }
2564
2565 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2566 quantities, choosing which based on whether the high word is nonzero. */
2567 static rtx
2568 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2569 {
2570 rtx xop0 = force_reg (mode, op0);
2571 rtx subhi = gen_highpart (word_mode, xop0);
2572 rtx sublo = gen_lowpart (word_mode, xop0);
2573 rtx hi0_label = gen_label_rtx ();
2574 rtx after_label = gen_label_rtx ();
2575 rtx seq, temp, result;
2576
2577 /* If we were not given a target, use a word_mode register, not a
2578 'mode' register. The result will fit, and nobody is expecting
2579 anything bigger (the return type of __builtin_clz* is int). */
2580 if (!target)
2581 target = gen_reg_rtx (word_mode);
2582
2583 /* In any case, write to a word_mode scratch in both branches of the
2584 conditional, so we can ensure there is a single move insn setting
2585 'target' to tag a REG_EQUAL note on. */
2586 result = gen_reg_rtx (word_mode);
2587
2588 start_sequence ();
2589
2590 /* If the high word is not equal to zero,
2591 then clz of the full value is clz of the high word. */
2592 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2593 word_mode, true, hi0_label);
2594
2595 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2596 if (!temp)
2597 goto fail;
2598
2599 if (temp != result)
2600 convert_move (result, temp, true);
2601
2602 emit_jump_insn (gen_jump (after_label));
2603 emit_barrier ();
2604
2605 /* Else clz of the full value is clz of the low word plus the number
2606 of bits in the high word. */
2607 emit_label (hi0_label);
2608
2609 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2610 if (!temp)
2611 goto fail;
2612 temp = expand_binop (word_mode, add_optab, temp,
2613 GEN_INT (GET_MODE_BITSIZE (word_mode)),
2614 result, true, OPTAB_DIRECT);
2615 if (!temp)
2616 goto fail;
2617 if (temp != result)
2618 convert_move (result, temp, true);
2619
2620 emit_label (after_label);
2621 convert_move (target, result, true);
2622
2623 seq = get_insns ();
2624 end_sequence ();
2625
2626 add_equal_note (seq, target, CLZ, xop0, 0);
2627 emit_insn (seq);
2628 return target;
2629
2630 fail:
2631 end_sequence ();
2632 return 0;
2633 }
2634
2635 /* Try calculating
2636 (bswap:narrow x)
2637 as
2638 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2639 static rtx
2640 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2641 {
2642 enum mode_class mclass = GET_MODE_CLASS (mode);
2643 enum machine_mode wider_mode;
2644 rtx x, last;
2645
2646 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2647 return NULL_RTX;
2648
2649 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2650 wider_mode != VOIDmode;
2651 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2652 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2653 goto found;
2654 return NULL_RTX;
2655
2656 found:
2657 last = get_last_insn ();
2658
2659 x = widen_operand (op0, wider_mode, mode, true, true);
2660 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2661
2662 if (x != 0)
2663 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2664 size_int (GET_MODE_BITSIZE (wider_mode)
2665 - GET_MODE_BITSIZE (mode)),
2666 NULL_RTX, true);
2667
2668 if (x != 0)
2669 {
2670 if (target == 0)
2671 target = gen_reg_rtx (mode);
2672 emit_move_insn (target, gen_lowpart (mode, x));
2673 }
2674 else
2675 delete_insns_since (last);
2676
2677 return target;
2678 }
2679
2680 /* Try calculating bswap as two bswaps of two word-sized operands. */
2681
2682 static rtx
2683 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2684 {
2685 rtx t0, t1;
2686
2687 t1 = expand_unop (word_mode, bswap_optab,
2688 operand_subword_force (op, 0, mode), NULL_RTX, true);
2689 t0 = expand_unop (word_mode, bswap_optab,
2690 operand_subword_force (op, 1, mode), NULL_RTX, true);
2691
2692 if (target == 0)
2693 target = gen_reg_rtx (mode);
2694 if (REG_P (target))
2695 emit_clobber (target);
2696 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2697 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2698
2699 return target;
2700 }
2701
2702 /* Try calculating (parity x) as (and (popcount x) 1), where
2703 popcount can also be done in a wider mode. */
2704 static rtx
2705 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2706 {
2707 enum mode_class mclass = GET_MODE_CLASS (mode);
2708 if (CLASS_HAS_WIDER_MODES_P (mclass))
2709 {
2710 enum machine_mode wider_mode;
2711 for (wider_mode = mode; wider_mode != VOIDmode;
2712 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2713 {
2714 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2715 {
2716 rtx xop0, temp, last;
2717
2718 last = get_last_insn ();
2719
2720 if (target == 0)
2721 target = gen_reg_rtx (mode);
2722 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2723 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2724 true);
2725 if (temp != 0)
2726 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2727 target, true, OPTAB_DIRECT);
2728 if (temp == 0)
2729 delete_insns_since (last);
2730
2731 return temp;
2732 }
2733 }
2734 }
2735 return 0;
2736 }
2737
2738 /* Try calculating ctz(x) as K - clz(x & -x) ,
2739 where K is GET_MODE_BITSIZE(mode) - 1.
2740
2741 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2742 don't have to worry about what the hardware does in that case. (If
2743 the clz instruction produces the usual value at 0, which is K, the
2744 result of this code sequence will be -1; expand_ffs, below, relies
2745 on this. It might be nice to have it be K instead, for consistency
2746 with the (very few) processors that provide a ctz with a defined
2747 value, but that would take one more instruction, and it would be
2748 less convenient for expand_ffs anyway. */
2749
2750 static rtx
2751 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2752 {
2753 rtx seq, temp;
2754
2755 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2756 return 0;
2757
2758 start_sequence ();
2759
2760 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2761 if (temp)
2762 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2763 true, OPTAB_DIRECT);
2764 if (temp)
2765 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2766 if (temp)
2767 temp = expand_binop (mode, sub_optab, GEN_INT (GET_MODE_BITSIZE (mode) - 1),
2768 temp, target,
2769 true, OPTAB_DIRECT);
2770 if (temp == 0)
2771 {
2772 end_sequence ();
2773 return 0;
2774 }
2775
2776 seq = get_insns ();
2777 end_sequence ();
2778
2779 add_equal_note (seq, temp, CTZ, op0, 0);
2780 emit_insn (seq);
2781 return temp;
2782 }
2783
2784
2785 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2786 else with the sequence used by expand_clz.
2787
2788 The ffs builtin promises to return zero for a zero value and ctz/clz
2789 may have an undefined value in that case. If they do not give us a
2790 convenient value, we have to generate a test and branch. */
2791 static rtx
2792 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2793 {
2794 HOST_WIDE_INT val = 0;
2795 bool defined_at_zero = false;
2796 rtx temp, seq;
2797
2798 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2799 {
2800 start_sequence ();
2801
2802 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2803 if (!temp)
2804 goto fail;
2805
2806 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2807 }
2808 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2809 {
2810 start_sequence ();
2811 temp = expand_ctz (mode, op0, 0);
2812 if (!temp)
2813 goto fail;
2814
2815 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2816 {
2817 defined_at_zero = true;
2818 val = (GET_MODE_BITSIZE (mode) - 1) - val;
2819 }
2820 }
2821 else
2822 return 0;
2823
2824 if (defined_at_zero && val == -1)
2825 /* No correction needed at zero. */;
2826 else
2827 {
2828 /* We don't try to do anything clever with the situation found
2829 on some processors (eg Alpha) where ctz(0:mode) ==
2830 bitsize(mode). If someone can think of a way to send N to -1
2831 and leave alone all values in the range 0..N-1 (where N is a
2832 power of two), cheaper than this test-and-branch, please add it.
2833
2834 The test-and-branch is done after the operation itself, in case
2835 the operation sets condition codes that can be recycled for this.
2836 (This is true on i386, for instance.) */
2837
2838 rtx nonzero_label = gen_label_rtx ();
2839 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2840 mode, true, nonzero_label);
2841
2842 convert_move (temp, GEN_INT (-1), false);
2843 emit_label (nonzero_label);
2844 }
2845
2846 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2847 to produce a value in the range 0..bitsize. */
2848 temp = expand_binop (mode, add_optab, temp, GEN_INT (1),
2849 target, false, OPTAB_DIRECT);
2850 if (!temp)
2851 goto fail;
2852
2853 seq = get_insns ();
2854 end_sequence ();
2855
2856 add_equal_note (seq, temp, FFS, op0, 0);
2857 emit_insn (seq);
2858 return temp;
2859
2860 fail:
2861 end_sequence ();
2862 return 0;
2863 }
2864
2865 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2866 conditions, VAL may already be a SUBREG against which we cannot generate
2867 a further SUBREG. In this case, we expect forcing the value into a
2868 register will work around the situation. */
2869
2870 static rtx
2871 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2872 enum machine_mode imode)
2873 {
2874 rtx ret;
2875 ret = lowpart_subreg (omode, val, imode);
2876 if (ret == NULL)
2877 {
2878 val = force_reg (imode, val);
2879 ret = lowpart_subreg (omode, val, imode);
2880 gcc_assert (ret != NULL);
2881 }
2882 return ret;
2883 }
2884
2885 /* Expand a floating point absolute value or negation operation via a
2886 logical operation on the sign bit. */
2887
2888 static rtx
2889 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2890 rtx op0, rtx target)
2891 {
2892 const struct real_format *fmt;
2893 int bitpos, word, nwords, i;
2894 enum machine_mode imode;
2895 double_int mask;
2896 rtx temp, insns;
2897
2898 /* The format has to have a simple sign bit. */
2899 fmt = REAL_MODE_FORMAT (mode);
2900 if (fmt == NULL)
2901 return NULL_RTX;
2902
2903 bitpos = fmt->signbit_rw;
2904 if (bitpos < 0)
2905 return NULL_RTX;
2906
2907 /* Don't create negative zeros if the format doesn't support them. */
2908 if (code == NEG && !fmt->has_signed_zero)
2909 return NULL_RTX;
2910
2911 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2912 {
2913 imode = int_mode_for_mode (mode);
2914 if (imode == BLKmode)
2915 return NULL_RTX;
2916 word = 0;
2917 nwords = 1;
2918 }
2919 else
2920 {
2921 imode = word_mode;
2922
2923 if (FLOAT_WORDS_BIG_ENDIAN)
2924 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2925 else
2926 word = bitpos / BITS_PER_WORD;
2927 bitpos = bitpos % BITS_PER_WORD;
2928 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2929 }
2930
2931 mask = double_int_setbit (double_int_zero, bitpos);
2932 if (code == ABS)
2933 mask = double_int_not (mask);
2934
2935 if (target == 0 || target == op0)
2936 target = gen_reg_rtx (mode);
2937
2938 if (nwords > 1)
2939 {
2940 start_sequence ();
2941
2942 for (i = 0; i < nwords; ++i)
2943 {
2944 rtx targ_piece = operand_subword (target, i, 1, mode);
2945 rtx op0_piece = operand_subword_force (op0, i, mode);
2946
2947 if (i == word)
2948 {
2949 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2950 op0_piece,
2951 immed_double_int_const (mask, imode),
2952 targ_piece, 1, OPTAB_LIB_WIDEN);
2953 if (temp != targ_piece)
2954 emit_move_insn (targ_piece, temp);
2955 }
2956 else
2957 emit_move_insn (targ_piece, op0_piece);
2958 }
2959
2960 insns = get_insns ();
2961 end_sequence ();
2962
2963 emit_insn (insns);
2964 }
2965 else
2966 {
2967 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2968 gen_lowpart (imode, op0),
2969 immed_double_int_const (mask, imode),
2970 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2971 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2972
2973 set_unique_reg_note (get_last_insn (), REG_EQUAL,
2974 gen_rtx_fmt_e (code, mode, copy_rtx (op0)));
2975 }
2976
2977 return target;
2978 }
2979
2980 /* As expand_unop, but will fail rather than attempt the operation in a
2981 different mode or with a libcall. */
2982 static rtx
2983 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2984 int unsignedp)
2985 {
2986 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2987 {
2988 int icode = (int) optab_handler (unoptab, mode);
2989 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2990 rtx xop0 = op0;
2991 rtx last = get_last_insn ();
2992 rtx pat, temp;
2993
2994 if (target)
2995 temp = target;
2996 else
2997 temp = gen_reg_rtx (mode);
2998
2999 if (GET_MODE (xop0) != VOIDmode
3000 && GET_MODE (xop0) != mode0)
3001 xop0 = convert_to_mode (mode0, xop0, unsignedp);
3002
3003 /* Now, if insn doesn't accept our operand, put it into a pseudo. */
3004
3005 if (!insn_data[icode].operand[1].predicate (xop0, mode0))
3006 xop0 = copy_to_mode_reg (mode0, xop0);
3007
3008 if (!insn_data[icode].operand[0].predicate (temp, mode))
3009 temp = gen_reg_rtx (mode);
3010
3011 pat = GEN_FCN (icode) (temp, xop0);
3012 if (pat)
3013 {
3014 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3015 && ! add_equal_note (pat, temp, unoptab->code, xop0, NULL_RTX))
3016 {
3017 delete_insns_since (last);
3018 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3019 }
3020
3021 emit_insn (pat);
3022
3023 return temp;
3024 }
3025 else
3026 delete_insns_since (last);
3027 }
3028 return 0;
3029 }
3030
3031 /* Generate code to perform an operation specified by UNOPTAB
3032 on operand OP0, with result having machine-mode MODE.
3033
3034 UNSIGNEDP is for the case where we have to widen the operands
3035 to perform the operation. It says to use zero-extension.
3036
3037 If TARGET is nonzero, the value
3038 is generated there, if it is convenient to do so.
3039 In all cases an rtx is returned for the locus of the value;
3040 this may or may not be TARGET. */
3041
3042 rtx
3043 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3044 int unsignedp)
3045 {
3046 enum mode_class mclass = GET_MODE_CLASS (mode);
3047 enum machine_mode wider_mode;
3048 rtx temp;
3049 rtx libfunc;
3050
3051 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3052 if (temp)
3053 return temp;
3054
3055 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3056
3057 /* Widening (or narrowing) clz needs special treatment. */
3058 if (unoptab == clz_optab)
3059 {
3060 temp = widen_clz (mode, op0, target);
3061 if (temp)
3062 return temp;
3063
3064 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3065 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3066 {
3067 temp = expand_doubleword_clz (mode, op0, target);
3068 if (temp)
3069 return temp;
3070 }
3071
3072 goto try_libcall;
3073 }
3074
3075 /* Widening (or narrowing) bswap needs special treatment. */
3076 if (unoptab == bswap_optab)
3077 {
3078 temp = widen_bswap (mode, op0, target);
3079 if (temp)
3080 return temp;
3081
3082 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3083 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3084 {
3085 temp = expand_doubleword_bswap (mode, op0, target);
3086 if (temp)
3087 return temp;
3088 }
3089
3090 goto try_libcall;
3091 }
3092
3093 if (CLASS_HAS_WIDER_MODES_P (mclass))
3094 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3095 wider_mode != VOIDmode;
3096 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3097 {
3098 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3099 {
3100 rtx xop0 = op0;
3101 rtx last = get_last_insn ();
3102
3103 /* For certain operations, we need not actually extend
3104 the narrow operand, as long as we will truncate the
3105 results to the same narrowness. */
3106
3107 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3108 (unoptab == neg_optab
3109 || unoptab == one_cmpl_optab)
3110 && mclass == MODE_INT);
3111
3112 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3113 unsignedp);
3114
3115 if (temp)
3116 {
3117 if (mclass != MODE_INT
3118 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3119 GET_MODE_BITSIZE (wider_mode)))
3120 {
3121 if (target == 0)
3122 target = gen_reg_rtx (mode);
3123 convert_move (target, temp, 0);
3124 return target;
3125 }
3126 else
3127 return gen_lowpart (mode, temp);
3128 }
3129 else
3130 delete_insns_since (last);
3131 }
3132 }
3133
3134 /* These can be done a word at a time. */
3135 if (unoptab == one_cmpl_optab
3136 && mclass == MODE_INT
3137 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3138 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3139 {
3140 int i;
3141 rtx insns;
3142
3143 if (target == 0 || target == op0)
3144 target = gen_reg_rtx (mode);
3145
3146 start_sequence ();
3147
3148 /* Do the actual arithmetic. */
3149 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3150 {
3151 rtx target_piece = operand_subword (target, i, 1, mode);
3152 rtx x = expand_unop (word_mode, unoptab,
3153 operand_subword_force (op0, i, mode),
3154 target_piece, unsignedp);
3155
3156 if (target_piece != x)
3157 emit_move_insn (target_piece, x);
3158 }
3159
3160 insns = get_insns ();
3161 end_sequence ();
3162
3163 emit_insn (insns);
3164 return target;
3165 }
3166
3167 if (unoptab->code == NEG)
3168 {
3169 /* Try negating floating point values by flipping the sign bit. */
3170 if (SCALAR_FLOAT_MODE_P (mode))
3171 {
3172 temp = expand_absneg_bit (NEG, mode, op0, target);
3173 if (temp)
3174 return temp;
3175 }
3176
3177 /* If there is no negation pattern, and we have no negative zero,
3178 try subtracting from zero. */
3179 if (!HONOR_SIGNED_ZEROS (mode))
3180 {
3181 temp = expand_binop (mode, (unoptab == negv_optab
3182 ? subv_optab : sub_optab),
3183 CONST0_RTX (mode), op0, target,
3184 unsignedp, OPTAB_DIRECT);
3185 if (temp)
3186 return temp;
3187 }
3188 }
3189
3190 /* Try calculating parity (x) as popcount (x) % 2. */
3191 if (unoptab == parity_optab)
3192 {
3193 temp = expand_parity (mode, op0, target);
3194 if (temp)
3195 return temp;
3196 }
3197
3198 /* Try implementing ffs (x) in terms of clz (x). */
3199 if (unoptab == ffs_optab)
3200 {
3201 temp = expand_ffs (mode, op0, target);
3202 if (temp)
3203 return temp;
3204 }
3205
3206 /* Try implementing ctz (x) in terms of clz (x). */
3207 if (unoptab == ctz_optab)
3208 {
3209 temp = expand_ctz (mode, op0, target);
3210 if (temp)
3211 return temp;
3212 }
3213
3214 try_libcall:
3215 /* Now try a library call in this mode. */
3216 libfunc = optab_libfunc (unoptab, mode);
3217 if (libfunc)
3218 {
3219 rtx insns;
3220 rtx value;
3221 rtx eq_value;
3222 enum machine_mode outmode = mode;
3223
3224 /* All of these functions return small values. Thus we choose to
3225 have them return something that isn't a double-word. */
3226 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3227 || unoptab == popcount_optab || unoptab == parity_optab)
3228 outmode
3229 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3230 optab_libfunc (unoptab, mode)));
3231
3232 start_sequence ();
3233
3234 /* Pass 1 for NO_QUEUE so we don't lose any increments
3235 if the libcall is cse'd or moved. */
3236 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3237 1, op0, mode);
3238 insns = get_insns ();
3239 end_sequence ();
3240
3241 target = gen_reg_rtx (outmode);
3242 eq_value = gen_rtx_fmt_e (unoptab->code, mode, op0);
3243 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3244 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3245 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3246 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3247 emit_libcall_block (insns, target, value, eq_value);
3248
3249 return target;
3250 }
3251
3252 /* It can't be done in this mode. Can we do it in a wider mode? */
3253
3254 if (CLASS_HAS_WIDER_MODES_P (mclass))
3255 {
3256 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3257 wider_mode != VOIDmode;
3258 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3259 {
3260 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3261 || optab_libfunc (unoptab, wider_mode))
3262 {
3263 rtx xop0 = op0;
3264 rtx last = get_last_insn ();
3265
3266 /* For certain operations, we need not actually extend
3267 the narrow operand, as long as we will truncate the
3268 results to the same narrowness. */
3269
3270 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3271 (unoptab == neg_optab
3272 || unoptab == one_cmpl_optab)
3273 && mclass == MODE_INT);
3274
3275 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3276 unsignedp);
3277
3278 /* If we are generating clz using wider mode, adjust the
3279 result. */
3280 if (unoptab == clz_optab && temp != 0)
3281 temp = expand_binop (wider_mode, sub_optab, temp,
3282 GEN_INT (GET_MODE_BITSIZE (wider_mode)
3283 - GET_MODE_BITSIZE (mode)),
3284 target, true, OPTAB_DIRECT);
3285
3286 if (temp)
3287 {
3288 if (mclass != MODE_INT)
3289 {
3290 if (target == 0)
3291 target = gen_reg_rtx (mode);
3292 convert_move (target, temp, 0);
3293 return target;
3294 }
3295 else
3296 return gen_lowpart (mode, temp);
3297 }
3298 else
3299 delete_insns_since (last);
3300 }
3301 }
3302 }
3303
3304 /* One final attempt at implementing negation via subtraction,
3305 this time allowing widening of the operand. */
3306 if (unoptab->code == NEG && !HONOR_SIGNED_ZEROS (mode))
3307 {
3308 rtx temp;
3309 temp = expand_binop (mode,
3310 unoptab == negv_optab ? subv_optab : sub_optab,
3311 CONST0_RTX (mode), op0,
3312 target, unsignedp, OPTAB_LIB_WIDEN);
3313 if (temp)
3314 return temp;
3315 }
3316
3317 return 0;
3318 }
3319 \f
3320 /* Emit code to compute the absolute value of OP0, with result to
3321 TARGET if convenient. (TARGET may be 0.) The return value says
3322 where the result actually is to be found.
3323
3324 MODE is the mode of the operand; the mode of the result is
3325 different but can be deduced from MODE.
3326
3327 */
3328
3329 rtx
3330 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3331 int result_unsignedp)
3332 {
3333 rtx temp;
3334
3335 if (! flag_trapv)
3336 result_unsignedp = 1;
3337
3338 /* First try to do it with a special abs instruction. */
3339 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3340 op0, target, 0);
3341 if (temp != 0)
3342 return temp;
3343
3344 /* For floating point modes, try clearing the sign bit. */
3345 if (SCALAR_FLOAT_MODE_P (mode))
3346 {
3347 temp = expand_absneg_bit (ABS, mode, op0, target);
3348 if (temp)
3349 return temp;
3350 }
3351
3352 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3353 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3354 && !HONOR_SIGNED_ZEROS (mode))
3355 {
3356 rtx last = get_last_insn ();
3357
3358 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
3359 if (temp != 0)
3360 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3361 OPTAB_WIDEN);
3362
3363 if (temp != 0)
3364 return temp;
3365
3366 delete_insns_since (last);
3367 }
3368
3369 /* If this machine has expensive jumps, we can do integer absolute
3370 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3371 where W is the width of MODE. */
3372
3373 if (GET_MODE_CLASS (mode) == MODE_INT
3374 && BRANCH_COST (optimize_insn_for_speed_p (),
3375 false) >= 2)
3376 {
3377 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3378 size_int (GET_MODE_BITSIZE (mode) - 1),
3379 NULL_RTX, 0);
3380
3381 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3382 OPTAB_LIB_WIDEN);
3383 if (temp != 0)
3384 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3385 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3386
3387 if (temp != 0)
3388 return temp;
3389 }
3390
3391 return NULL_RTX;
3392 }
3393
3394 rtx
3395 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3396 int result_unsignedp, int safe)
3397 {
3398 rtx temp, op1;
3399
3400 if (! flag_trapv)
3401 result_unsignedp = 1;
3402
3403 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3404 if (temp != 0)
3405 return temp;
3406
3407 /* If that does not win, use conditional jump and negate. */
3408
3409 /* It is safe to use the target if it is the same
3410 as the source if this is also a pseudo register */
3411 if (op0 == target && REG_P (op0)
3412 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3413 safe = 1;
3414
3415 op1 = gen_label_rtx ();
3416 if (target == 0 || ! safe
3417 || GET_MODE (target) != mode
3418 || (MEM_P (target) && MEM_VOLATILE_P (target))
3419 || (REG_P (target)
3420 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3421 target = gen_reg_rtx (mode);
3422
3423 emit_move_insn (target, op0);
3424 NO_DEFER_POP;
3425
3426 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3427 NULL_RTX, NULL_RTX, op1, -1);
3428
3429 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3430 target, target, 0);
3431 if (op0 != target)
3432 emit_move_insn (target, op0);
3433 emit_label (op1);
3434 OK_DEFER_POP;
3435 return target;
3436 }
3437
3438 /* Emit code to compute the one's complement absolute value of OP0
3439 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3440 (TARGET may be NULL_RTX.) The return value says where the result
3441 actually is to be found.
3442
3443 MODE is the mode of the operand; the mode of the result is
3444 different but can be deduced from MODE. */
3445
3446 rtx
3447 expand_one_cmpl_abs_nojump (enum machine_mode mode, rtx op0, rtx target)
3448 {
3449 rtx temp;
3450
3451 /* Not applicable for floating point modes. */
3452 if (FLOAT_MODE_P (mode))
3453 return NULL_RTX;
3454
3455 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3456 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3457 {
3458 rtx last = get_last_insn ();
3459
3460 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3461 if (temp != 0)
3462 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3463 OPTAB_WIDEN);
3464
3465 if (temp != 0)
3466 return temp;
3467
3468 delete_insns_since (last);
3469 }
3470
3471 /* If this machine has expensive jumps, we can do one's complement
3472 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3473
3474 if (GET_MODE_CLASS (mode) == MODE_INT
3475 && BRANCH_COST (optimize_insn_for_speed_p (),
3476 false) >= 2)
3477 {
3478 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3479 size_int (GET_MODE_BITSIZE (mode) - 1),
3480 NULL_RTX, 0);
3481
3482 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3483 OPTAB_LIB_WIDEN);
3484
3485 if (temp != 0)
3486 return temp;
3487 }
3488
3489 return NULL_RTX;
3490 }
3491
3492 /* A subroutine of expand_copysign, perform the copysign operation using the
3493 abs and neg primitives advertised to exist on the target. The assumption
3494 is that we have a split register file, and leaving op0 in fp registers,
3495 and not playing with subregs so much, will help the register allocator. */
3496
3497 static rtx
3498 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3499 int bitpos, bool op0_is_abs)
3500 {
3501 enum machine_mode imode;
3502 int icode;
3503 rtx sign, label;
3504
3505 if (target == op1)
3506 target = NULL_RTX;
3507
3508 /* Check if the back end provides an insn that handles signbit for the
3509 argument's mode. */
3510 icode = (int) optab_handler (signbit_optab, mode);
3511 if (icode != CODE_FOR_nothing)
3512 {
3513 imode = insn_data[icode].operand[0].mode;
3514 sign = gen_reg_rtx (imode);
3515 emit_unop_insn (icode, sign, op1, UNKNOWN);
3516 }
3517 else
3518 {
3519 double_int mask;
3520
3521 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3522 {
3523 imode = int_mode_for_mode (mode);
3524 if (imode == BLKmode)
3525 return NULL_RTX;
3526 op1 = gen_lowpart (imode, op1);
3527 }
3528 else
3529 {
3530 int word;
3531
3532 imode = word_mode;
3533 if (FLOAT_WORDS_BIG_ENDIAN)
3534 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3535 else
3536 word = bitpos / BITS_PER_WORD;
3537 bitpos = bitpos % BITS_PER_WORD;
3538 op1 = operand_subword_force (op1, word, mode);
3539 }
3540
3541 mask = double_int_setbit (double_int_zero, bitpos);
3542
3543 sign = expand_binop (imode, and_optab, op1,
3544 immed_double_int_const (mask, imode),
3545 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3546 }
3547
3548 if (!op0_is_abs)
3549 {
3550 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3551 if (op0 == NULL)
3552 return NULL_RTX;
3553 target = op0;
3554 }
3555 else
3556 {
3557 if (target == NULL_RTX)
3558 target = copy_to_reg (op0);
3559 else
3560 emit_move_insn (target, op0);
3561 }
3562
3563 label = gen_label_rtx ();
3564 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3565
3566 if (GET_CODE (op0) == CONST_DOUBLE)
3567 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3568 else
3569 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3570 if (op0 != target)
3571 emit_move_insn (target, op0);
3572
3573 emit_label (label);
3574
3575 return target;
3576 }
3577
3578
3579 /* A subroutine of expand_copysign, perform the entire copysign operation
3580 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3581 is true if op0 is known to have its sign bit clear. */
3582
3583 static rtx
3584 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3585 int bitpos, bool op0_is_abs)
3586 {
3587 enum machine_mode imode;
3588 double_int mask;
3589 int word, nwords, i;
3590 rtx temp, insns;
3591
3592 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3593 {
3594 imode = int_mode_for_mode (mode);
3595 if (imode == BLKmode)
3596 return NULL_RTX;
3597 word = 0;
3598 nwords = 1;
3599 }
3600 else
3601 {
3602 imode = word_mode;
3603
3604 if (FLOAT_WORDS_BIG_ENDIAN)
3605 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3606 else
3607 word = bitpos / BITS_PER_WORD;
3608 bitpos = bitpos % BITS_PER_WORD;
3609 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3610 }
3611
3612 mask = double_int_setbit (double_int_zero, bitpos);
3613
3614 if (target == 0 || target == op0 || target == op1)
3615 target = gen_reg_rtx (mode);
3616
3617 if (nwords > 1)
3618 {
3619 start_sequence ();
3620
3621 for (i = 0; i < nwords; ++i)
3622 {
3623 rtx targ_piece = operand_subword (target, i, 1, mode);
3624 rtx op0_piece = operand_subword_force (op0, i, mode);
3625
3626 if (i == word)
3627 {
3628 if (!op0_is_abs)
3629 op0_piece
3630 = expand_binop (imode, and_optab, op0_piece,
3631 immed_double_int_const (double_int_not (mask),
3632 imode),
3633 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3634
3635 op1 = expand_binop (imode, and_optab,
3636 operand_subword_force (op1, i, mode),
3637 immed_double_int_const (mask, imode),
3638 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3639
3640 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3641 targ_piece, 1, OPTAB_LIB_WIDEN);
3642 if (temp != targ_piece)
3643 emit_move_insn (targ_piece, temp);
3644 }
3645 else
3646 emit_move_insn (targ_piece, op0_piece);
3647 }
3648
3649 insns = get_insns ();
3650 end_sequence ();
3651
3652 emit_insn (insns);
3653 }
3654 else
3655 {
3656 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3657 immed_double_int_const (mask, imode),
3658 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3659
3660 op0 = gen_lowpart (imode, op0);
3661 if (!op0_is_abs)
3662 op0 = expand_binop (imode, and_optab, op0,
3663 immed_double_int_const (double_int_not (mask),
3664 imode),
3665 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3666
3667 temp = expand_binop (imode, ior_optab, op0, op1,
3668 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3669 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3670 }
3671
3672 return target;
3673 }
3674
3675 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3676 scalar floating point mode. Return NULL if we do not know how to
3677 expand the operation inline. */
3678
3679 rtx
3680 expand_copysign (rtx op0, rtx op1, rtx target)
3681 {
3682 enum machine_mode mode = GET_MODE (op0);
3683 const struct real_format *fmt;
3684 bool op0_is_abs;
3685 rtx temp;
3686
3687 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3688 gcc_assert (GET_MODE (op1) == mode);
3689
3690 /* First try to do it with a special instruction. */
3691 temp = expand_binop (mode, copysign_optab, op0, op1,
3692 target, 0, OPTAB_DIRECT);
3693 if (temp)
3694 return temp;
3695
3696 fmt = REAL_MODE_FORMAT (mode);
3697 if (fmt == NULL || !fmt->has_signed_zero)
3698 return NULL_RTX;
3699
3700 op0_is_abs = false;
3701 if (GET_CODE (op0) == CONST_DOUBLE)
3702 {
3703 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3704 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3705 op0_is_abs = true;
3706 }
3707
3708 if (fmt->signbit_ro >= 0
3709 && (GET_CODE (op0) == CONST_DOUBLE
3710 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3711 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3712 {
3713 temp = expand_copysign_absneg (mode, op0, op1, target,
3714 fmt->signbit_ro, op0_is_abs);
3715 if (temp)
3716 return temp;
3717 }
3718
3719 if (fmt->signbit_rw < 0)
3720 return NULL_RTX;
3721 return expand_copysign_bit (mode, op0, op1, target,
3722 fmt->signbit_rw, op0_is_abs);
3723 }
3724 \f
3725 /* Generate an instruction whose insn-code is INSN_CODE,
3726 with two operands: an output TARGET and an input OP0.
3727 TARGET *must* be nonzero, and the output is always stored there.
3728 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3729 the value that is stored into TARGET.
3730
3731 Return false if expansion failed. */
3732
3733 bool
3734 maybe_emit_unop_insn (int icode, rtx target, rtx op0, enum rtx_code code)
3735 {
3736 rtx temp;
3737 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3738 rtx pat;
3739 rtx last = get_last_insn ();
3740
3741 temp = target;
3742
3743 /* Now, if insn does not accept our operands, put them into pseudos. */
3744
3745 if (!insn_data[icode].operand[1].predicate (op0, mode0))
3746 op0 = copy_to_mode_reg (mode0, op0);
3747
3748 if (!insn_data[icode].operand[0].predicate (temp, GET_MODE (temp)))
3749 temp = gen_reg_rtx (GET_MODE (temp));
3750
3751 pat = GEN_FCN (icode) (temp, op0);
3752 if (!pat)
3753 {
3754 delete_insns_since (last);
3755 return false;
3756 }
3757
3758 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3759 add_equal_note (pat, temp, code, op0, NULL_RTX);
3760
3761 emit_insn (pat);
3762
3763 if (temp != target)
3764 emit_move_insn (target, temp);
3765 return true;
3766 }
3767 /* Generate an instruction whose insn-code is INSN_CODE,
3768 with two operands: an output TARGET and an input OP0.
3769 TARGET *must* be nonzero, and the output is always stored there.
3770 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3771 the value that is stored into TARGET. */
3772
3773 void
3774 emit_unop_insn (int icode, rtx target, rtx op0, enum rtx_code code)
3775 {
3776 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3777 gcc_assert (ok);
3778 }
3779 \f
3780 struct no_conflict_data
3781 {
3782 rtx target, first, insn;
3783 bool must_stay;
3784 };
3785
3786 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3787 the currently examined clobber / store has to stay in the list of
3788 insns that constitute the actual libcall block. */
3789 static void
3790 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3791 {
3792 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3793
3794 /* If this inns directly contributes to setting the target, it must stay. */
3795 if (reg_overlap_mentioned_p (p->target, dest))
3796 p->must_stay = true;
3797 /* If we haven't committed to keeping any other insns in the list yet,
3798 there is nothing more to check. */
3799 else if (p->insn == p->first)
3800 return;
3801 /* If this insn sets / clobbers a register that feeds one of the insns
3802 already in the list, this insn has to stay too. */
3803 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3804 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3805 || reg_used_between_p (dest, p->first, p->insn)
3806 /* Likewise if this insn depends on a register set by a previous
3807 insn in the list, or if it sets a result (presumably a hard
3808 register) that is set or clobbered by a previous insn.
3809 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3810 SET_DEST perform the former check on the address, and the latter
3811 check on the MEM. */
3812 || (GET_CODE (set) == SET
3813 && (modified_in_p (SET_SRC (set), p->first)
3814 || modified_in_p (SET_DEST (set), p->first)
3815 || modified_between_p (SET_SRC (set), p->first, p->insn)
3816 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3817 p->must_stay = true;
3818 }
3819
3820 \f
3821 /* Emit code to make a call to a constant function or a library call.
3822
3823 INSNS is a list containing all insns emitted in the call.
3824 These insns leave the result in RESULT. Our block is to copy RESULT
3825 to TARGET, which is logically equivalent to EQUIV.
3826
3827 We first emit any insns that set a pseudo on the assumption that these are
3828 loading constants into registers; doing so allows them to be safely cse'ed
3829 between blocks. Then we emit all the other insns in the block, followed by
3830 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3831 note with an operand of EQUIV. */
3832
3833 void
3834 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3835 {
3836 rtx final_dest = target;
3837 rtx next, last, insn;
3838
3839 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3840 into a MEM later. Protect the libcall block from this change. */
3841 if (! REG_P (target) || REG_USERVAR_P (target))
3842 target = gen_reg_rtx (GET_MODE (target));
3843
3844 /* If we're using non-call exceptions, a libcall corresponding to an
3845 operation that may trap may also trap. */
3846 /* ??? See the comment in front of make_reg_eh_region_note. */
3847 if (cfun->can_throw_non_call_exceptions && may_trap_p (equiv))
3848 {
3849 for (insn = insns; insn; insn = NEXT_INSN (insn))
3850 if (CALL_P (insn))
3851 {
3852 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3853 if (note)
3854 {
3855 int lp_nr = INTVAL (XEXP (note, 0));
3856 if (lp_nr == 0 || lp_nr == INT_MIN)
3857 remove_note (insn, note);
3858 }
3859 }
3860 }
3861 else
3862 {
3863 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3864 reg note to indicate that this call cannot throw or execute a nonlocal
3865 goto (unless there is already a REG_EH_REGION note, in which case
3866 we update it). */
3867 for (insn = insns; insn; insn = NEXT_INSN (insn))
3868 if (CALL_P (insn))
3869 make_reg_eh_region_note_nothrow_nononlocal (insn);
3870 }
3871
3872 /* First emit all insns that set pseudos. Remove them from the list as
3873 we go. Avoid insns that set pseudos which were referenced in previous
3874 insns. These can be generated by move_by_pieces, for example,
3875 to update an address. Similarly, avoid insns that reference things
3876 set in previous insns. */
3877
3878 for (insn = insns; insn; insn = next)
3879 {
3880 rtx set = single_set (insn);
3881
3882 next = NEXT_INSN (insn);
3883
3884 if (set != 0 && REG_P (SET_DEST (set))
3885 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3886 {
3887 struct no_conflict_data data;
3888
3889 data.target = const0_rtx;
3890 data.first = insns;
3891 data.insn = insn;
3892 data.must_stay = 0;
3893 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3894 if (! data.must_stay)
3895 {
3896 if (PREV_INSN (insn))
3897 NEXT_INSN (PREV_INSN (insn)) = next;
3898 else
3899 insns = next;
3900
3901 if (next)
3902 PREV_INSN (next) = PREV_INSN (insn);
3903
3904 add_insn (insn);
3905 }
3906 }
3907
3908 /* Some ports use a loop to copy large arguments onto the stack.
3909 Don't move anything outside such a loop. */
3910 if (LABEL_P (insn))
3911 break;
3912 }
3913
3914 /* Write the remaining insns followed by the final copy. */
3915 for (insn = insns; insn; insn = next)
3916 {
3917 next = NEXT_INSN (insn);
3918
3919 add_insn (insn);
3920 }
3921
3922 last = emit_move_insn (target, result);
3923 if (optab_handler (mov_optab, GET_MODE (target)) != CODE_FOR_nothing)
3924 set_unique_reg_note (last, REG_EQUAL, copy_rtx (equiv));
3925
3926 if (final_dest != target)
3927 emit_move_insn (final_dest, target);
3928 }
3929 \f
3930 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3931 PURPOSE describes how this comparison will be used. CODE is the rtx
3932 comparison code we will be using.
3933
3934 ??? Actually, CODE is slightly weaker than that. A target is still
3935 required to implement all of the normal bcc operations, but not
3936 required to implement all (or any) of the unordered bcc operations. */
3937
3938 int
3939 can_compare_p (enum rtx_code code, enum machine_mode mode,
3940 enum can_compare_purpose purpose)
3941 {
3942 rtx test;
3943 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3944 do
3945 {
3946 enum insn_code icode;
3947
3948 if (purpose == ccp_jump
3949 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
3950 && insn_operand_matches (icode, 0, test))
3951 return 1;
3952 if (purpose == ccp_store_flag
3953 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
3954 && insn_operand_matches (icode, 1, test))
3955 return 1;
3956 if (purpose == ccp_cmov
3957 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
3958 return 1;
3959
3960 mode = GET_MODE_WIDER_MODE (mode);
3961 PUT_MODE (test, mode);
3962 }
3963 while (mode != VOIDmode);
3964
3965 return 0;
3966 }
3967
3968 /* This function is called when we are going to emit a compare instruction that
3969 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
3970
3971 *PMODE is the mode of the inputs (in case they are const_int).
3972 *PUNSIGNEDP nonzero says that the operands are unsigned;
3973 this matters if they need to be widened (as given by METHODS).
3974
3975 If they have mode BLKmode, then SIZE specifies the size of both operands.
3976
3977 This function performs all the setup necessary so that the caller only has
3978 to emit a single comparison insn. This setup can involve doing a BLKmode
3979 comparison or emitting a library call to perform the comparison if no insn
3980 is available to handle it.
3981 The values which are passed in through pointers can be modified; the caller
3982 should perform the comparison on the modified values. Constant
3983 comparisons must have already been folded. */
3984
3985 static void
3986 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3987 int unsignedp, enum optab_methods methods,
3988 rtx *ptest, enum machine_mode *pmode)
3989 {
3990 enum machine_mode mode = *pmode;
3991 rtx libfunc, test;
3992 enum machine_mode cmp_mode;
3993 enum mode_class mclass;
3994
3995 /* The other methods are not needed. */
3996 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
3997 || methods == OPTAB_LIB_WIDEN);
3998
3999 /* If we are optimizing, force expensive constants into a register. */
4000 if (CONSTANT_P (x) && optimize
4001 && (rtx_cost (x, COMPARE, optimize_insn_for_speed_p ())
4002 > COSTS_N_INSNS (1)))
4003 x = force_reg (mode, x);
4004
4005 if (CONSTANT_P (y) && optimize
4006 && (rtx_cost (y, COMPARE, optimize_insn_for_speed_p ())
4007 > COSTS_N_INSNS (1)))
4008 y = force_reg (mode, y);
4009
4010 #ifdef HAVE_cc0
4011 /* Make sure if we have a canonical comparison. The RTL
4012 documentation states that canonical comparisons are required only
4013 for targets which have cc0. */
4014 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4015 #endif
4016
4017 /* Don't let both operands fail to indicate the mode. */
4018 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4019 x = force_reg (mode, x);
4020 if (mode == VOIDmode)
4021 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4022
4023 /* Handle all BLKmode compares. */
4024
4025 if (mode == BLKmode)
4026 {
4027 enum machine_mode result_mode;
4028 enum insn_code cmp_code;
4029 tree length_type;
4030 rtx libfunc;
4031 rtx result;
4032 rtx opalign
4033 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4034
4035 gcc_assert (size);
4036
4037 /* Try to use a memory block compare insn - either cmpstr
4038 or cmpmem will do. */
4039 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4040 cmp_mode != VOIDmode;
4041 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4042 {
4043 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4044 if (cmp_code == CODE_FOR_nothing)
4045 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4046 if (cmp_code == CODE_FOR_nothing)
4047 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4048 if (cmp_code == CODE_FOR_nothing)
4049 continue;
4050
4051 /* Must make sure the size fits the insn's mode. */
4052 if ((CONST_INT_P (size)
4053 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4054 || (GET_MODE_BITSIZE (GET_MODE (size))
4055 > GET_MODE_BITSIZE (cmp_mode)))
4056 continue;
4057
4058 result_mode = insn_data[cmp_code].operand[0].mode;
4059 result = gen_reg_rtx (result_mode);
4060 size = convert_to_mode (cmp_mode, size, 1);
4061 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4062
4063 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4064 *pmode = result_mode;
4065 return;
4066 }
4067
4068 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4069 goto fail;
4070
4071 /* Otherwise call a library function, memcmp. */
4072 libfunc = memcmp_libfunc;
4073 length_type = sizetype;
4074 result_mode = TYPE_MODE (integer_type_node);
4075 cmp_mode = TYPE_MODE (length_type);
4076 size = convert_to_mode (TYPE_MODE (length_type), size,
4077 TYPE_UNSIGNED (length_type));
4078
4079 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4080 result_mode, 3,
4081 XEXP (x, 0), Pmode,
4082 XEXP (y, 0), Pmode,
4083 size, cmp_mode);
4084
4085 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4086 *pmode = result_mode;
4087 return;
4088 }
4089
4090 /* Don't allow operands to the compare to trap, as that can put the
4091 compare and branch in different basic blocks. */
4092 if (cfun->can_throw_non_call_exceptions)
4093 {
4094 if (may_trap_p (x))
4095 x = force_reg (mode, x);
4096 if (may_trap_p (y))
4097 y = force_reg (mode, y);
4098 }
4099
4100 if (GET_MODE_CLASS (mode) == MODE_CC)
4101 {
4102 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
4103 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4104 return;
4105 }
4106
4107 mclass = GET_MODE_CLASS (mode);
4108 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4109 cmp_mode = mode;
4110 do
4111 {
4112 enum insn_code icode;
4113 icode = optab_handler (cbranch_optab, cmp_mode);
4114 if (icode != CODE_FOR_nothing
4115 && insn_operand_matches (icode, 0, test))
4116 {
4117 rtx last = get_last_insn ();
4118 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4119 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4120 if (op0 && op1
4121 && insn_operand_matches (icode, 1, op0)
4122 && insn_operand_matches (icode, 2, op1))
4123 {
4124 XEXP (test, 0) = op0;
4125 XEXP (test, 1) = op1;
4126 *ptest = test;
4127 *pmode = cmp_mode;
4128 return;
4129 }
4130 delete_insns_since (last);
4131 }
4132
4133 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4134 break;
4135 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4136 }
4137 while (cmp_mode != VOIDmode);
4138
4139 if (methods != OPTAB_LIB_WIDEN)
4140 goto fail;
4141
4142 if (!SCALAR_FLOAT_MODE_P (mode))
4143 {
4144 rtx result;
4145
4146 /* Handle a libcall just for the mode we are using. */
4147 libfunc = optab_libfunc (cmp_optab, mode);
4148 gcc_assert (libfunc);
4149
4150 /* If we want unsigned, and this mode has a distinct unsigned
4151 comparison routine, use that. */
4152 if (unsignedp)
4153 {
4154 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4155 if (ulibfunc)
4156 libfunc = ulibfunc;
4157 }
4158
4159 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4160 targetm.libgcc_cmp_return_mode (),
4161 2, x, mode, y, mode);
4162
4163 /* There are two kinds of comparison routines. Biased routines
4164 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4165 of gcc expect that the comparison operation is equivalent
4166 to the modified comparison. For signed comparisons compare the
4167 result against 1 in the biased case, and zero in the unbiased
4168 case. For unsigned comparisons always compare against 1 after
4169 biasing the unbiased result by adding 1. This gives us a way to
4170 represent LTU. */
4171 x = result;
4172 y = const1_rtx;
4173
4174 if (!TARGET_LIB_INT_CMP_BIASED)
4175 {
4176 if (unsignedp)
4177 x = plus_constant (result, 1);
4178 else
4179 y = const0_rtx;
4180 }
4181
4182 *pmode = word_mode;
4183 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4184 ptest, pmode);
4185 }
4186 else
4187 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4188
4189 return;
4190
4191 fail:
4192 *ptest = NULL_RTX;
4193 }
4194
4195 /* Before emitting an insn with code ICODE, make sure that X, which is going
4196 to be used for operand OPNUM of the insn, is converted from mode MODE to
4197 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4198 that it is accepted by the operand predicate. Return the new value. */
4199
4200 rtx
4201 prepare_operand (enum insn_code icode, rtx x, int opnum, enum machine_mode mode,
4202 enum machine_mode wider_mode, int unsignedp)
4203 {
4204 if (mode != wider_mode)
4205 x = convert_modes (wider_mode, mode, x, unsignedp);
4206
4207 if (!insn_operand_matches (icode, opnum, x))
4208 {
4209 if (reload_completed)
4210 return NULL_RTX;
4211 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
4212 }
4213
4214 return x;
4215 }
4216
4217 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4218 we can do the branch. */
4219
4220 static void
4221 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label)
4222 {
4223 enum machine_mode optab_mode;
4224 enum mode_class mclass;
4225 enum insn_code icode;
4226
4227 mclass = GET_MODE_CLASS (mode);
4228 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4229 icode = optab_handler (cbranch_optab, optab_mode);
4230
4231 gcc_assert (icode != CODE_FOR_nothing);
4232 gcc_assert (insn_operand_matches (icode, 0, test));
4233 emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0), XEXP (test, 1), label));
4234 }
4235
4236 /* Generate code to compare X with Y so that the condition codes are
4237 set and to jump to LABEL if the condition is true. If X is a
4238 constant and Y is not a constant, then the comparison is swapped to
4239 ensure that the comparison RTL has the canonical form.
4240
4241 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4242 need to be widened. UNSIGNEDP is also used to select the proper
4243 branch condition code.
4244
4245 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4246
4247 MODE is the mode of the inputs (in case they are const_int).
4248
4249 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4250 It will be potentially converted into an unsigned variant based on
4251 UNSIGNEDP to select a proper jump instruction. */
4252
4253 void
4254 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4255 enum machine_mode mode, int unsignedp, rtx label)
4256 {
4257 rtx op0 = x, op1 = y;
4258 rtx test;
4259
4260 /* Swap operands and condition to ensure canonical RTL. */
4261 if (swap_commutative_operands_p (x, y)
4262 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4263 {
4264 op0 = y, op1 = x;
4265 comparison = swap_condition (comparison);
4266 }
4267
4268 /* If OP0 is still a constant, then both X and Y must be constants
4269 or the opposite comparison is not supported. Force X into a register
4270 to create canonical RTL. */
4271 if (CONSTANT_P (op0))
4272 op0 = force_reg (mode, op0);
4273
4274 if (unsignedp)
4275 comparison = unsigned_condition (comparison);
4276
4277 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4278 &test, &mode);
4279 emit_cmp_and_jump_insn_1 (test, mode, label);
4280 }
4281
4282 \f
4283 /* Emit a library call comparison between floating point X and Y.
4284 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4285
4286 static void
4287 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4288 rtx *ptest, enum machine_mode *pmode)
4289 {
4290 enum rtx_code swapped = swap_condition (comparison);
4291 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4292 enum machine_mode orig_mode = GET_MODE (x);
4293 enum machine_mode mode, cmp_mode;
4294 rtx true_rtx, false_rtx;
4295 rtx value, target, insns, equiv;
4296 rtx libfunc = 0;
4297 bool reversed_p = false;
4298 cmp_mode = targetm.libgcc_cmp_return_mode ();
4299
4300 for (mode = orig_mode;
4301 mode != VOIDmode;
4302 mode = GET_MODE_WIDER_MODE (mode))
4303 {
4304 if (code_to_optab[comparison]
4305 && (libfunc = optab_libfunc (code_to_optab[comparison], mode)))
4306 break;
4307
4308 if (code_to_optab[swapped]
4309 && (libfunc = optab_libfunc (code_to_optab[swapped], mode)))
4310 {
4311 rtx tmp;
4312 tmp = x; x = y; y = tmp;
4313 comparison = swapped;
4314 break;
4315 }
4316
4317 if (code_to_optab[reversed]
4318 && (libfunc = optab_libfunc (code_to_optab[reversed], mode)))
4319 {
4320 comparison = reversed;
4321 reversed_p = true;
4322 break;
4323 }
4324 }
4325
4326 gcc_assert (mode != VOIDmode);
4327
4328 if (mode != orig_mode)
4329 {
4330 x = convert_to_mode (mode, x, 0);
4331 y = convert_to_mode (mode, y, 0);
4332 }
4333
4334 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4335 the RTL. The allows the RTL optimizers to delete the libcall if the
4336 condition can be determined at compile-time. */
4337 if (comparison == UNORDERED
4338 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4339 {
4340 true_rtx = const_true_rtx;
4341 false_rtx = const0_rtx;
4342 }
4343 else
4344 {
4345 switch (comparison)
4346 {
4347 case EQ:
4348 true_rtx = const0_rtx;
4349 false_rtx = const_true_rtx;
4350 break;
4351
4352 case NE:
4353 true_rtx = const_true_rtx;
4354 false_rtx = const0_rtx;
4355 break;
4356
4357 case GT:
4358 true_rtx = const1_rtx;
4359 false_rtx = const0_rtx;
4360 break;
4361
4362 case GE:
4363 true_rtx = const0_rtx;
4364 false_rtx = constm1_rtx;
4365 break;
4366
4367 case LT:
4368 true_rtx = constm1_rtx;
4369 false_rtx = const0_rtx;
4370 break;
4371
4372 case LE:
4373 true_rtx = const0_rtx;
4374 false_rtx = const1_rtx;
4375 break;
4376
4377 default:
4378 gcc_unreachable ();
4379 }
4380 }
4381
4382 if (comparison == UNORDERED)
4383 {
4384 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4385 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4386 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4387 temp, const_true_rtx, equiv);
4388 }
4389 else
4390 {
4391 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4392 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4393 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4394 equiv, true_rtx, false_rtx);
4395 }
4396
4397 start_sequence ();
4398 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4399 cmp_mode, 2, x, mode, y, mode);
4400 insns = get_insns ();
4401 end_sequence ();
4402
4403 target = gen_reg_rtx (cmp_mode);
4404 emit_libcall_block (insns, target, value, equiv);
4405
4406 if (comparison == UNORDERED
4407 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4408 || reversed_p)
4409 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4410 else
4411 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4412
4413 *pmode = cmp_mode;
4414 }
4415 \f
4416 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4417
4418 void
4419 emit_indirect_jump (rtx loc)
4420 {
4421 if (!insn_data[(int) CODE_FOR_indirect_jump].operand[0].predicate
4422 (loc, Pmode))
4423 loc = copy_to_mode_reg (Pmode, loc);
4424
4425 emit_jump_insn (gen_indirect_jump (loc));
4426 emit_barrier ();
4427 }
4428 \f
4429 #ifdef HAVE_conditional_move
4430
4431 /* Emit a conditional move instruction if the machine supports one for that
4432 condition and machine mode.
4433
4434 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4435 the mode to use should they be constants. If it is VOIDmode, they cannot
4436 both be constants.
4437
4438 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4439 should be stored there. MODE is the mode to use should they be constants.
4440 If it is VOIDmode, they cannot both be constants.
4441
4442 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4443 is not supported. */
4444
4445 rtx
4446 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4447 enum machine_mode cmode, rtx op2, rtx op3,
4448 enum machine_mode mode, int unsignedp)
4449 {
4450 rtx tem, subtarget, comparison, insn;
4451 enum insn_code icode;
4452 enum rtx_code reversed;
4453
4454 /* If one operand is constant, make it the second one. Only do this
4455 if the other operand is not constant as well. */
4456
4457 if (swap_commutative_operands_p (op0, op1))
4458 {
4459 tem = op0;
4460 op0 = op1;
4461 op1 = tem;
4462 code = swap_condition (code);
4463 }
4464
4465 /* get_condition will prefer to generate LT and GT even if the old
4466 comparison was against zero, so undo that canonicalization here since
4467 comparisons against zero are cheaper. */
4468 if (code == LT && op1 == const1_rtx)
4469 code = LE, op1 = const0_rtx;
4470 else if (code == GT && op1 == constm1_rtx)
4471 code = GE, op1 = const0_rtx;
4472
4473 if (cmode == VOIDmode)
4474 cmode = GET_MODE (op0);
4475
4476 if (swap_commutative_operands_p (op2, op3)
4477 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4478 != UNKNOWN))
4479 {
4480 tem = op2;
4481 op2 = op3;
4482 op3 = tem;
4483 code = reversed;
4484 }
4485
4486 if (mode == VOIDmode)
4487 mode = GET_MODE (op2);
4488
4489 icode = direct_optab_handler (movcc_optab, mode);
4490
4491 if (icode == CODE_FOR_nothing)
4492 return 0;
4493
4494 if (!target)
4495 target = gen_reg_rtx (mode);
4496
4497 subtarget = target;
4498
4499 /* If the insn doesn't accept these operands, put them in pseudos. */
4500
4501 if (!insn_data[icode].operand[0].predicate
4502 (subtarget, insn_data[icode].operand[0].mode))
4503 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
4504
4505 if (!insn_data[icode].operand[2].predicate
4506 (op2, insn_data[icode].operand[2].mode))
4507 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
4508
4509 if (!insn_data[icode].operand[3].predicate
4510 (op3, insn_data[icode].operand[3].mode))
4511 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
4512
4513 /* Everything should now be in the suitable form. */
4514
4515 code = unsignedp ? unsigned_condition (code) : code;
4516 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4517
4518 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4519 return NULL and let the caller figure out how best to deal with this
4520 situation. */
4521 if (!COMPARISON_P (comparison))
4522 return NULL_RTX;
4523
4524 do_pending_stack_adjust ();
4525 start_sequence ();
4526 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4527 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4528 &comparison, &cmode);
4529 if (!comparison)
4530 insn = NULL_RTX;
4531 else
4532 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
4533
4534 /* If that failed, then give up. */
4535 if (insn == 0)
4536 {
4537 end_sequence ();
4538 return 0;
4539 }
4540
4541 emit_insn (insn);
4542 insn = get_insns ();
4543 end_sequence ();
4544 emit_insn (insn);
4545 if (subtarget != target)
4546 convert_move (target, subtarget, 0);
4547
4548 return target;
4549 }
4550
4551 /* Return nonzero if a conditional move of mode MODE is supported.
4552
4553 This function is for combine so it can tell whether an insn that looks
4554 like a conditional move is actually supported by the hardware. If we
4555 guess wrong we lose a bit on optimization, but that's it. */
4556 /* ??? sparc64 supports conditionally moving integers values based on fp
4557 comparisons, and vice versa. How do we handle them? */
4558
4559 int
4560 can_conditionally_move_p (enum machine_mode mode)
4561 {
4562 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4563 return 1;
4564
4565 return 0;
4566 }
4567
4568 #endif /* HAVE_conditional_move */
4569
4570 /* Emit a conditional addition instruction if the machine supports one for that
4571 condition and machine mode.
4572
4573 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4574 the mode to use should they be constants. If it is VOIDmode, they cannot
4575 both be constants.
4576
4577 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
4578 should be stored there. MODE is the mode to use should they be constants.
4579 If it is VOIDmode, they cannot both be constants.
4580
4581 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4582 is not supported. */
4583
4584 rtx
4585 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4586 enum machine_mode cmode, rtx op2, rtx op3,
4587 enum machine_mode mode, int unsignedp)
4588 {
4589 rtx tem, subtarget, comparison, insn;
4590 enum insn_code icode;
4591 enum rtx_code reversed;
4592
4593 /* If one operand is constant, make it the second one. Only do this
4594 if the other operand is not constant as well. */
4595
4596 if (swap_commutative_operands_p (op0, op1))
4597 {
4598 tem = op0;
4599 op0 = op1;
4600 op1 = tem;
4601 code = swap_condition (code);
4602 }
4603
4604 /* get_condition will prefer to generate LT and GT even if the old
4605 comparison was against zero, so undo that canonicalization here since
4606 comparisons against zero are cheaper. */
4607 if (code == LT && op1 == const1_rtx)
4608 code = LE, op1 = const0_rtx;
4609 else if (code == GT && op1 == constm1_rtx)
4610 code = GE, op1 = const0_rtx;
4611
4612 if (cmode == VOIDmode)
4613 cmode = GET_MODE (op0);
4614
4615 if (swap_commutative_operands_p (op2, op3)
4616 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4617 != UNKNOWN))
4618 {
4619 tem = op2;
4620 op2 = op3;
4621 op3 = tem;
4622 code = reversed;
4623 }
4624
4625 if (mode == VOIDmode)
4626 mode = GET_MODE (op2);
4627
4628 icode = optab_handler (addcc_optab, mode);
4629
4630 if (icode == CODE_FOR_nothing)
4631 return 0;
4632
4633 if (!target)
4634 target = gen_reg_rtx (mode);
4635
4636 /* If the insn doesn't accept these operands, put them in pseudos. */
4637
4638 if (!insn_data[icode].operand[0].predicate
4639 (target, insn_data[icode].operand[0].mode))
4640 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
4641 else
4642 subtarget = target;
4643
4644 if (!insn_data[icode].operand[2].predicate
4645 (op2, insn_data[icode].operand[2].mode))
4646 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
4647
4648 if (!insn_data[icode].operand[3].predicate
4649 (op3, insn_data[icode].operand[3].mode))
4650 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
4651
4652 /* Everything should now be in the suitable form. */
4653
4654 code = unsignedp ? unsigned_condition (code) : code;
4655 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4656
4657 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4658 return NULL and let the caller figure out how best to deal with this
4659 situation. */
4660 if (!COMPARISON_P (comparison))
4661 return NULL_RTX;
4662
4663 do_pending_stack_adjust ();
4664 start_sequence ();
4665 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4666 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4667 &comparison, &cmode);
4668 if (!comparison)
4669 insn = NULL_RTX;
4670 else
4671 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
4672
4673 /* If that failed, then give up. */
4674 if (insn == 0)
4675 {
4676 end_sequence ();
4677 return 0;
4678 }
4679
4680 emit_insn (insn);
4681 insn = get_insns ();
4682 end_sequence ();
4683 emit_insn (insn);
4684 if (subtarget != target)
4685 convert_move (target, subtarget, 0);
4686
4687 return target;
4688 }
4689 \f
4690 /* These functions attempt to generate an insn body, rather than
4691 emitting the insn, but if the gen function already emits them, we
4692 make no attempt to turn them back into naked patterns. */
4693
4694 /* Generate and return an insn body to add Y to X. */
4695
4696 rtx
4697 gen_add2_insn (rtx x, rtx y)
4698 {
4699 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4700
4701 gcc_assert (insn_operand_matches (icode, 0, x));
4702 gcc_assert (insn_operand_matches (icode, 1, x));
4703 gcc_assert (insn_operand_matches (icode, 2, y));
4704
4705 return GEN_FCN (icode) (x, x, y);
4706 }
4707
4708 /* Generate and return an insn body to add r1 and c,
4709 storing the result in r0. */
4710
4711 rtx
4712 gen_add3_insn (rtx r0, rtx r1, rtx c)
4713 {
4714 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4715
4716 if (icode == CODE_FOR_nothing
4717 || !insn_operand_matches (icode, 0, r0)
4718 || !insn_operand_matches (icode, 1, r1)
4719 || !insn_operand_matches (icode, 2, c))
4720 return NULL_RTX;
4721
4722 return GEN_FCN (icode) (r0, r1, c);
4723 }
4724
4725 int
4726 have_add2_insn (rtx x, rtx y)
4727 {
4728 enum insn_code icode;
4729
4730 gcc_assert (GET_MODE (x) != VOIDmode);
4731
4732 icode = optab_handler (add_optab, GET_MODE (x));
4733
4734 if (icode == CODE_FOR_nothing)
4735 return 0;
4736
4737 if (!insn_operand_matches (icode, 0, x)
4738 || !insn_operand_matches (icode, 1, x)
4739 || !insn_operand_matches (icode, 2, y))
4740 return 0;
4741
4742 return 1;
4743 }
4744
4745 /* Generate and return an insn body to subtract Y from X. */
4746
4747 rtx
4748 gen_sub2_insn (rtx x, rtx y)
4749 {
4750 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4751
4752 gcc_assert (insn_operand_matches (icode, 0, x));
4753 gcc_assert (insn_operand_matches (icode, 1, x));
4754 gcc_assert (insn_operand_matches (icode, 2, y));
4755
4756 return GEN_FCN (icode) (x, x, y);
4757 }
4758
4759 /* Generate and return an insn body to subtract r1 and c,
4760 storing the result in r0. */
4761
4762 rtx
4763 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4764 {
4765 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4766
4767 if (icode == CODE_FOR_nothing
4768 || !insn_operand_matches (icode, 0, r0)
4769 || !insn_operand_matches (icode, 1, r1)
4770 || !insn_operand_matches (icode, 2, c))
4771 return NULL_RTX;
4772
4773 return GEN_FCN (icode) (r0, r1, c);
4774 }
4775
4776 int
4777 have_sub2_insn (rtx x, rtx y)
4778 {
4779 enum insn_code icode;
4780
4781 gcc_assert (GET_MODE (x) != VOIDmode);
4782
4783 icode = optab_handler (sub_optab, GET_MODE (x));
4784
4785 if (icode == CODE_FOR_nothing)
4786 return 0;
4787
4788 if (!insn_operand_matches (icode, 0, x)
4789 || !insn_operand_matches (icode, 1, x)
4790 || !insn_operand_matches (icode, 2, y))
4791 return 0;
4792
4793 return 1;
4794 }
4795
4796 /* Generate the body of an instruction to copy Y into X.
4797 It may be a list of insns, if one insn isn't enough. */
4798
4799 rtx
4800 gen_move_insn (rtx x, rtx y)
4801 {
4802 rtx seq;
4803
4804 start_sequence ();
4805 emit_move_insn_1 (x, y);
4806 seq = get_insns ();
4807 end_sequence ();
4808 return seq;
4809 }
4810 \f
4811 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4812 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4813 no such operation exists, CODE_FOR_nothing will be returned. */
4814
4815 enum insn_code
4816 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4817 int unsignedp)
4818 {
4819 convert_optab tab;
4820 #ifdef HAVE_ptr_extend
4821 if (unsignedp < 0)
4822 return CODE_FOR_ptr_extend;
4823 #endif
4824
4825 tab = unsignedp ? zext_optab : sext_optab;
4826 return convert_optab_handler (tab, to_mode, from_mode);
4827 }
4828
4829 /* Generate the body of an insn to extend Y (with mode MFROM)
4830 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4831
4832 rtx
4833 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4834 enum machine_mode mfrom, int unsignedp)
4835 {
4836 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4837 return GEN_FCN (icode) (x, y);
4838 }
4839 \f
4840 /* can_fix_p and can_float_p say whether the target machine
4841 can directly convert a given fixed point type to
4842 a given floating point type, or vice versa.
4843 The returned value is the CODE_FOR_... value to use,
4844 or CODE_FOR_nothing if these modes cannot be directly converted.
4845
4846 *TRUNCP_PTR is set to 1 if it is necessary to output
4847 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4848
4849 static enum insn_code
4850 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4851 int unsignedp, int *truncp_ptr)
4852 {
4853 convert_optab tab;
4854 enum insn_code icode;
4855
4856 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4857 icode = convert_optab_handler (tab, fixmode, fltmode);
4858 if (icode != CODE_FOR_nothing)
4859 {
4860 *truncp_ptr = 0;
4861 return icode;
4862 }
4863
4864 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4865 for this to work. We need to rework the fix* and ftrunc* patterns
4866 and documentation. */
4867 tab = unsignedp ? ufix_optab : sfix_optab;
4868 icode = convert_optab_handler (tab, fixmode, fltmode);
4869 if (icode != CODE_FOR_nothing
4870 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4871 {
4872 *truncp_ptr = 1;
4873 return icode;
4874 }
4875
4876 *truncp_ptr = 0;
4877 return CODE_FOR_nothing;
4878 }
4879
4880 static enum insn_code
4881 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4882 int unsignedp)
4883 {
4884 convert_optab tab;
4885
4886 tab = unsignedp ? ufloat_optab : sfloat_optab;
4887 return convert_optab_handler (tab, fltmode, fixmode);
4888 }
4889 \f
4890 /* Generate code to convert FROM to floating point
4891 and store in TO. FROM must be fixed point and not VOIDmode.
4892 UNSIGNEDP nonzero means regard FROM as unsigned.
4893 Normally this is done by correcting the final value
4894 if it is negative. */
4895
4896 void
4897 expand_float (rtx to, rtx from, int unsignedp)
4898 {
4899 enum insn_code icode;
4900 rtx target = to;
4901 enum machine_mode fmode, imode;
4902 bool can_do_signed = false;
4903
4904 /* Crash now, because we won't be able to decide which mode to use. */
4905 gcc_assert (GET_MODE (from) != VOIDmode);
4906
4907 /* Look for an insn to do the conversion. Do it in the specified
4908 modes if possible; otherwise convert either input, output or both to
4909 wider mode. If the integer mode is wider than the mode of FROM,
4910 we can do the conversion signed even if the input is unsigned. */
4911
4912 for (fmode = GET_MODE (to); fmode != VOIDmode;
4913 fmode = GET_MODE_WIDER_MODE (fmode))
4914 for (imode = GET_MODE (from); imode != VOIDmode;
4915 imode = GET_MODE_WIDER_MODE (imode))
4916 {
4917 int doing_unsigned = unsignedp;
4918
4919 if (fmode != GET_MODE (to)
4920 && significand_size (fmode) < GET_MODE_BITSIZE (GET_MODE (from)))
4921 continue;
4922
4923 icode = can_float_p (fmode, imode, unsignedp);
4924 if (icode == CODE_FOR_nothing && unsignedp)
4925 {
4926 enum insn_code scode = can_float_p (fmode, imode, 0);
4927 if (scode != CODE_FOR_nothing)
4928 can_do_signed = true;
4929 if (imode != GET_MODE (from))
4930 icode = scode, doing_unsigned = 0;
4931 }
4932
4933 if (icode != CODE_FOR_nothing)
4934 {
4935 if (imode != GET_MODE (from))
4936 from = convert_to_mode (imode, from, unsignedp);
4937
4938 if (fmode != GET_MODE (to))
4939 target = gen_reg_rtx (fmode);
4940
4941 emit_unop_insn (icode, target, from,
4942 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4943
4944 if (target != to)
4945 convert_move (to, target, 0);
4946 return;
4947 }
4948 }
4949
4950 /* Unsigned integer, and no way to convert directly. Convert as signed,
4951 then unconditionally adjust the result. */
4952 if (unsignedp && can_do_signed)
4953 {
4954 rtx label = gen_label_rtx ();
4955 rtx temp;
4956 REAL_VALUE_TYPE offset;
4957
4958 /* Look for a usable floating mode FMODE wider than the source and at
4959 least as wide as the target. Using FMODE will avoid rounding woes
4960 with unsigned values greater than the signed maximum value. */
4961
4962 for (fmode = GET_MODE (to); fmode != VOIDmode;
4963 fmode = GET_MODE_WIDER_MODE (fmode))
4964 if (GET_MODE_BITSIZE (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4965 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4966 break;
4967
4968 if (fmode == VOIDmode)
4969 {
4970 /* There is no such mode. Pretend the target is wide enough. */
4971 fmode = GET_MODE (to);
4972
4973 /* Avoid double-rounding when TO is narrower than FROM. */
4974 if ((significand_size (fmode) + 1)
4975 < GET_MODE_BITSIZE (GET_MODE (from)))
4976 {
4977 rtx temp1;
4978 rtx neglabel = gen_label_rtx ();
4979
4980 /* Don't use TARGET if it isn't a register, is a hard register,
4981 or is the wrong mode. */
4982 if (!REG_P (target)
4983 || REGNO (target) < FIRST_PSEUDO_REGISTER
4984 || GET_MODE (target) != fmode)
4985 target = gen_reg_rtx (fmode);
4986
4987 imode = GET_MODE (from);
4988 do_pending_stack_adjust ();
4989
4990 /* Test whether the sign bit is set. */
4991 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4992 0, neglabel);
4993
4994 /* The sign bit is not set. Convert as signed. */
4995 expand_float (target, from, 0);
4996 emit_jump_insn (gen_jump (label));
4997 emit_barrier ();
4998
4999 /* The sign bit is set.
5000 Convert to a usable (positive signed) value by shifting right
5001 one bit, while remembering if a nonzero bit was shifted
5002 out; i.e., compute (from & 1) | (from >> 1). */
5003
5004 emit_label (neglabel);
5005 temp = expand_binop (imode, and_optab, from, const1_rtx,
5006 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5007 temp1 = expand_shift (RSHIFT_EXPR, imode, from, integer_one_node,
5008 NULL_RTX, 1);
5009 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5010 OPTAB_LIB_WIDEN);
5011 expand_float (target, temp, 0);
5012
5013 /* Multiply by 2 to undo the shift above. */
5014 temp = expand_binop (fmode, add_optab, target, target,
5015 target, 0, OPTAB_LIB_WIDEN);
5016 if (temp != target)
5017 emit_move_insn (target, temp);
5018
5019 do_pending_stack_adjust ();
5020 emit_label (label);
5021 goto done;
5022 }
5023 }
5024
5025 /* If we are about to do some arithmetic to correct for an
5026 unsigned operand, do it in a pseudo-register. */
5027
5028 if (GET_MODE (to) != fmode
5029 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5030 target = gen_reg_rtx (fmode);
5031
5032 /* Convert as signed integer to floating. */
5033 expand_float (target, from, 0);
5034
5035 /* If FROM is negative (and therefore TO is negative),
5036 correct its value by 2**bitwidth. */
5037
5038 do_pending_stack_adjust ();
5039 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5040 0, label);
5041
5042
5043 real_2expN (&offset, GET_MODE_BITSIZE (GET_MODE (from)), fmode);
5044 temp = expand_binop (fmode, add_optab, target,
5045 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5046 target, 0, OPTAB_LIB_WIDEN);
5047 if (temp != target)
5048 emit_move_insn (target, temp);
5049
5050 do_pending_stack_adjust ();
5051 emit_label (label);
5052 goto done;
5053 }
5054
5055 /* No hardware instruction available; call a library routine. */
5056 {
5057 rtx libfunc;
5058 rtx insns;
5059 rtx value;
5060 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5061
5062 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
5063 from = convert_to_mode (SImode, from, unsignedp);
5064
5065 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5066 gcc_assert (libfunc);
5067
5068 start_sequence ();
5069
5070 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5071 GET_MODE (to), 1, from,
5072 GET_MODE (from));
5073 insns = get_insns ();
5074 end_sequence ();
5075
5076 emit_libcall_block (insns, target, value,
5077 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5078 GET_MODE (to), from));
5079 }
5080
5081 done:
5082
5083 /* Copy result to requested destination
5084 if we have been computing in a temp location. */
5085
5086 if (target != to)
5087 {
5088 if (GET_MODE (target) == GET_MODE (to))
5089 emit_move_insn (to, target);
5090 else
5091 convert_move (to, target, 0);
5092 }
5093 }
5094 \f
5095 /* Generate code to convert FROM to fixed point and store in TO. FROM
5096 must be floating point. */
5097
5098 void
5099 expand_fix (rtx to, rtx from, int unsignedp)
5100 {
5101 enum insn_code icode;
5102 rtx target = to;
5103 enum machine_mode fmode, imode;
5104 int must_trunc = 0;
5105
5106 /* We first try to find a pair of modes, one real and one integer, at
5107 least as wide as FROM and TO, respectively, in which we can open-code
5108 this conversion. If the integer mode is wider than the mode of TO,
5109 we can do the conversion either signed or unsigned. */
5110
5111 for (fmode = GET_MODE (from); fmode != VOIDmode;
5112 fmode = GET_MODE_WIDER_MODE (fmode))
5113 for (imode = GET_MODE (to); imode != VOIDmode;
5114 imode = GET_MODE_WIDER_MODE (imode))
5115 {
5116 int doing_unsigned = unsignedp;
5117
5118 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5119 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5120 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5121
5122 if (icode != CODE_FOR_nothing)
5123 {
5124 rtx last = get_last_insn ();
5125 if (fmode != GET_MODE (from))
5126 from = convert_to_mode (fmode, from, 0);
5127
5128 if (must_trunc)
5129 {
5130 rtx temp = gen_reg_rtx (GET_MODE (from));
5131 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5132 temp, 0);
5133 }
5134
5135 if (imode != GET_MODE (to))
5136 target = gen_reg_rtx (imode);
5137
5138 if (maybe_emit_unop_insn (icode, target, from,
5139 doing_unsigned ? UNSIGNED_FIX : FIX))
5140 {
5141 if (target != to)
5142 convert_move (to, target, unsignedp);
5143 return;
5144 }
5145 delete_insns_since (last);
5146 }
5147 }
5148
5149 /* For an unsigned conversion, there is one more way to do it.
5150 If we have a signed conversion, we generate code that compares
5151 the real value to the largest representable positive number. If if
5152 is smaller, the conversion is done normally. Otherwise, subtract
5153 one plus the highest signed number, convert, and add it back.
5154
5155 We only need to check all real modes, since we know we didn't find
5156 anything with a wider integer mode.
5157
5158 This code used to extend FP value into mode wider than the destination.
5159 This is needed for decimal float modes which cannot accurately
5160 represent one plus the highest signed number of the same size, but
5161 not for binary modes. Consider, for instance conversion from SFmode
5162 into DImode.
5163
5164 The hot path through the code is dealing with inputs smaller than 2^63
5165 and doing just the conversion, so there is no bits to lose.
5166
5167 In the other path we know the value is positive in the range 2^63..2^64-1
5168 inclusive. (as for other input overflow happens and result is undefined)
5169 So we know that the most important bit set in mantissa corresponds to
5170 2^63. The subtraction of 2^63 should not generate any rounding as it
5171 simply clears out that bit. The rest is trivial. */
5172
5173 if (unsignedp && GET_MODE_BITSIZE (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5174 for (fmode = GET_MODE (from); fmode != VOIDmode;
5175 fmode = GET_MODE_WIDER_MODE (fmode))
5176 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5177 && (!DECIMAL_FLOAT_MODE_P (fmode)
5178 || GET_MODE_BITSIZE (fmode) > GET_MODE_BITSIZE (GET_MODE (to))))
5179 {
5180 int bitsize;
5181 REAL_VALUE_TYPE offset;
5182 rtx limit, lab1, lab2, insn;
5183
5184 bitsize = GET_MODE_BITSIZE (GET_MODE (to));
5185 real_2expN (&offset, bitsize - 1, fmode);
5186 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5187 lab1 = gen_label_rtx ();
5188 lab2 = gen_label_rtx ();
5189
5190 if (fmode != GET_MODE (from))
5191 from = convert_to_mode (fmode, from, 0);
5192
5193 /* See if we need to do the subtraction. */
5194 do_pending_stack_adjust ();
5195 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5196 0, lab1);
5197
5198 /* If not, do the signed "fix" and branch around fixup code. */
5199 expand_fix (to, from, 0);
5200 emit_jump_insn (gen_jump (lab2));
5201 emit_barrier ();
5202
5203 /* Otherwise, subtract 2**(N-1), convert to signed number,
5204 then add 2**(N-1). Do the addition using XOR since this
5205 will often generate better code. */
5206 emit_label (lab1);
5207 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5208 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5209 expand_fix (to, target, 0);
5210 target = expand_binop (GET_MODE (to), xor_optab, to,
5211 gen_int_mode
5212 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5213 GET_MODE (to)),
5214 to, 1, OPTAB_LIB_WIDEN);
5215
5216 if (target != to)
5217 emit_move_insn (to, target);
5218
5219 emit_label (lab2);
5220
5221 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5222 {
5223 /* Make a place for a REG_NOTE and add it. */
5224 insn = emit_move_insn (to, to);
5225 set_unique_reg_note (insn,
5226 REG_EQUAL,
5227 gen_rtx_fmt_e (UNSIGNED_FIX,
5228 GET_MODE (to),
5229 copy_rtx (from)));
5230 }
5231
5232 return;
5233 }
5234
5235 /* We can't do it with an insn, so use a library call. But first ensure
5236 that the mode of TO is at least as wide as SImode, since those are the
5237 only library calls we know about. */
5238
5239 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5240 {
5241 target = gen_reg_rtx (SImode);
5242
5243 expand_fix (target, from, unsignedp);
5244 }
5245 else
5246 {
5247 rtx insns;
5248 rtx value;
5249 rtx libfunc;
5250
5251 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5252 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5253 gcc_assert (libfunc);
5254
5255 start_sequence ();
5256
5257 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5258 GET_MODE (to), 1, from,
5259 GET_MODE (from));
5260 insns = get_insns ();
5261 end_sequence ();
5262
5263 emit_libcall_block (insns, target, value,
5264 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5265 GET_MODE (to), from));
5266 }
5267
5268 if (target != to)
5269 {
5270 if (GET_MODE (to) == GET_MODE (target))
5271 emit_move_insn (to, target);
5272 else
5273 convert_move (to, target, 0);
5274 }
5275 }
5276
5277 /* Generate code to convert FROM or TO a fixed-point.
5278 If UINTP is true, either TO or FROM is an unsigned integer.
5279 If SATP is true, we need to saturate the result. */
5280
5281 void
5282 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5283 {
5284 enum machine_mode to_mode = GET_MODE (to);
5285 enum machine_mode from_mode = GET_MODE (from);
5286 convert_optab tab;
5287 enum rtx_code this_code;
5288 enum insn_code code;
5289 rtx insns, value;
5290 rtx libfunc;
5291
5292 if (to_mode == from_mode)
5293 {
5294 emit_move_insn (to, from);
5295 return;
5296 }
5297
5298 if (uintp)
5299 {
5300 tab = satp ? satfractuns_optab : fractuns_optab;
5301 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5302 }
5303 else
5304 {
5305 tab = satp ? satfract_optab : fract_optab;
5306 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5307 }
5308 code = convert_optab_handler (tab, to_mode, from_mode);
5309 if (code != CODE_FOR_nothing)
5310 {
5311 emit_unop_insn (code, to, from, this_code);
5312 return;
5313 }
5314
5315 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5316 gcc_assert (libfunc);
5317
5318 start_sequence ();
5319 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5320 1, from, from_mode);
5321 insns = get_insns ();
5322 end_sequence ();
5323
5324 emit_libcall_block (insns, to, value,
5325 gen_rtx_fmt_e (tab->code, to_mode, from));
5326 }
5327
5328 /* Generate code to convert FROM to fixed point and store in TO. FROM
5329 must be floating point, TO must be signed. Use the conversion optab
5330 TAB to do the conversion. */
5331
5332 bool
5333 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5334 {
5335 enum insn_code icode;
5336 rtx target = to;
5337 enum machine_mode fmode, imode;
5338
5339 /* We first try to find a pair of modes, one real and one integer, at
5340 least as wide as FROM and TO, respectively, in which we can open-code
5341 this conversion. If the integer mode is wider than the mode of TO,
5342 we can do the conversion either signed or unsigned. */
5343
5344 for (fmode = GET_MODE (from); fmode != VOIDmode;
5345 fmode = GET_MODE_WIDER_MODE (fmode))
5346 for (imode = GET_MODE (to); imode != VOIDmode;
5347 imode = GET_MODE_WIDER_MODE (imode))
5348 {
5349 icode = convert_optab_handler (tab, imode, fmode);
5350 if (icode != CODE_FOR_nothing)
5351 {
5352 rtx last = get_last_insn ();
5353 if (fmode != GET_MODE (from))
5354 from = convert_to_mode (fmode, from, 0);
5355
5356 if (imode != GET_MODE (to))
5357 target = gen_reg_rtx (imode);
5358
5359 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5360 {
5361 delete_insns_since (last);
5362 continue;
5363 }
5364 if (target != to)
5365 convert_move (to, target, 0);
5366 return true;
5367 }
5368 }
5369
5370 return false;
5371 }
5372 \f
5373 /* Report whether we have an instruction to perform the operation
5374 specified by CODE on operands of mode MODE. */
5375 int
5376 have_insn_for (enum rtx_code code, enum machine_mode mode)
5377 {
5378 return (code_to_optab[(int) code] != 0
5379 && (optab_handler (code_to_optab[(int) code], mode)
5380 != CODE_FOR_nothing));
5381 }
5382
5383 /* Set all insn_code fields to CODE_FOR_nothing. */
5384
5385 static void
5386 init_insn_codes (void)
5387 {
5388 memset (optab_table, 0, sizeof (optab_table));
5389 memset (convert_optab_table, 0, sizeof (convert_optab_table));
5390 memset (direct_optab_table, 0, sizeof (direct_optab_table));
5391 }
5392
5393 /* Initialize OP's code to CODE, and write it into the code_to_optab table. */
5394 static inline void
5395 init_optab (optab op, enum rtx_code code)
5396 {
5397 op->code = code;
5398 code_to_optab[(int) code] = op;
5399 }
5400
5401 /* Same, but fill in its code as CODE, and do _not_ write it into
5402 the code_to_optab table. */
5403 static inline void
5404 init_optabv (optab op, enum rtx_code code)
5405 {
5406 op->code = code;
5407 }
5408
5409 /* Conversion optabs never go in the code_to_optab table. */
5410 static void
5411 init_convert_optab (convert_optab op, enum rtx_code code)
5412 {
5413 op->code = code;
5414 }
5415
5416 /* Initialize the libfunc fields of an entire group of entries in some
5417 optab. Each entry is set equal to a string consisting of a leading
5418 pair of underscores followed by a generic operation name followed by
5419 a mode name (downshifted to lowercase) followed by a single character
5420 representing the number of operands for the given operation (which is
5421 usually one of the characters '2', '3', or '4').
5422
5423 OPTABLE is the table in which libfunc fields are to be initialized.
5424 OPNAME is the generic (string) name of the operation.
5425 SUFFIX is the character which specifies the number of operands for
5426 the given generic operation.
5427 MODE is the mode to generate for.
5428 */
5429
5430 static void
5431 gen_libfunc (optab optable, const char *opname, int suffix, enum machine_mode mode)
5432 {
5433 unsigned opname_len = strlen (opname);
5434 const char *mname = GET_MODE_NAME (mode);
5435 unsigned mname_len = strlen (mname);
5436 char *libfunc_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5437 char *p;
5438 const char *q;
5439
5440 p = libfunc_name;
5441 *p++ = '_';
5442 *p++ = '_';
5443 for (q = opname; *q; )
5444 *p++ = *q++;
5445 for (q = mname; *q; q++)
5446 *p++ = TOLOWER (*q);
5447 *p++ = suffix;
5448 *p = '\0';
5449
5450 set_optab_libfunc (optable, mode,
5451 ggc_alloc_string (libfunc_name, p - libfunc_name));
5452 }
5453
5454 /* Like gen_libfunc, but verify that integer operation is involved. */
5455
5456 static void
5457 gen_int_libfunc (optab optable, const char *opname, char suffix,
5458 enum machine_mode mode)
5459 {
5460 int maxsize = 2 * BITS_PER_WORD;
5461
5462 if (GET_MODE_CLASS (mode) != MODE_INT)
5463 return;
5464 if (maxsize < LONG_LONG_TYPE_SIZE)
5465 maxsize = LONG_LONG_TYPE_SIZE;
5466 if (GET_MODE_CLASS (mode) != MODE_INT
5467 || mode < word_mode || GET_MODE_BITSIZE (mode) > maxsize)
5468 return;
5469 gen_libfunc (optable, opname, suffix, mode);
5470 }
5471
5472 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5473
5474 static void
5475 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5476 enum machine_mode mode)
5477 {
5478 char *dec_opname;
5479
5480 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5481 gen_libfunc (optable, opname, suffix, mode);
5482 if (DECIMAL_FLOAT_MODE_P (mode))
5483 {
5484 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5485 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5486 depending on the low level floating format used. */
5487 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5488 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5489 gen_libfunc (optable, dec_opname, suffix, mode);
5490 }
5491 }
5492
5493 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5494
5495 static void
5496 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5497 enum machine_mode mode)
5498 {
5499 if (!ALL_FIXED_POINT_MODE_P (mode))
5500 return;
5501 gen_libfunc (optable, opname, suffix, mode);
5502 }
5503
5504 /* Like gen_libfunc, but verify that signed fixed-point operation is
5505 involved. */
5506
5507 static void
5508 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5509 enum machine_mode mode)
5510 {
5511 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5512 return;
5513 gen_libfunc (optable, opname, suffix, mode);
5514 }
5515
5516 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5517 involved. */
5518
5519 static void
5520 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5521 enum machine_mode mode)
5522 {
5523 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5524 return;
5525 gen_libfunc (optable, opname, suffix, mode);
5526 }
5527
5528 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5529
5530 static void
5531 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5532 enum machine_mode mode)
5533 {
5534 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5535 gen_fp_libfunc (optable, name, suffix, mode);
5536 if (INTEGRAL_MODE_P (mode))
5537 gen_int_libfunc (optable, name, suffix, mode);
5538 }
5539
5540 /* Like gen_libfunc, but verify that FP or INT operation is involved
5541 and add 'v' suffix for integer operation. */
5542
5543 static void
5544 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5545 enum machine_mode mode)
5546 {
5547 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5548 gen_fp_libfunc (optable, name, suffix, mode);
5549 if (GET_MODE_CLASS (mode) == MODE_INT)
5550 {
5551 int len = strlen (name);
5552 char *v_name = XALLOCAVEC (char, len + 2);
5553 strcpy (v_name, name);
5554 v_name[len] = 'v';
5555 v_name[len + 1] = 0;
5556 gen_int_libfunc (optable, v_name, suffix, mode);
5557 }
5558 }
5559
5560 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5561 involved. */
5562
5563 static void
5564 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5565 enum machine_mode mode)
5566 {
5567 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5568 gen_fp_libfunc (optable, name, suffix, mode);
5569 if (INTEGRAL_MODE_P (mode))
5570 gen_int_libfunc (optable, name, suffix, mode);
5571 if (ALL_FIXED_POINT_MODE_P (mode))
5572 gen_fixed_libfunc (optable, name, suffix, mode);
5573 }
5574
5575 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5576 involved. */
5577
5578 static void
5579 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5580 enum machine_mode mode)
5581 {
5582 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5583 gen_fp_libfunc (optable, name, suffix, mode);
5584 if (INTEGRAL_MODE_P (mode))
5585 gen_int_libfunc (optable, name, suffix, mode);
5586 if (SIGNED_FIXED_POINT_MODE_P (mode))
5587 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5588 }
5589
5590 /* Like gen_libfunc, but verify that INT or FIXED operation is
5591 involved. */
5592
5593 static void
5594 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5595 enum machine_mode mode)
5596 {
5597 if (INTEGRAL_MODE_P (mode))
5598 gen_int_libfunc (optable, name, suffix, mode);
5599 if (ALL_FIXED_POINT_MODE_P (mode))
5600 gen_fixed_libfunc (optable, name, suffix, mode);
5601 }
5602
5603 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5604 involved. */
5605
5606 static void
5607 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5608 enum machine_mode mode)
5609 {
5610 if (INTEGRAL_MODE_P (mode))
5611 gen_int_libfunc (optable, name, suffix, mode);
5612 if (SIGNED_FIXED_POINT_MODE_P (mode))
5613 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5614 }
5615
5616 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5617 involved. */
5618
5619 static void
5620 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5621 enum machine_mode mode)
5622 {
5623 if (INTEGRAL_MODE_P (mode))
5624 gen_int_libfunc (optable, name, suffix, mode);
5625 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5626 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5627 }
5628
5629 /* Initialize the libfunc fields of an entire group of entries of an
5630 inter-mode-class conversion optab. The string formation rules are
5631 similar to the ones for init_libfuncs, above, but instead of having
5632 a mode name and an operand count these functions have two mode names
5633 and no operand count. */
5634
5635 static void
5636 gen_interclass_conv_libfunc (convert_optab tab,
5637 const char *opname,
5638 enum machine_mode tmode,
5639 enum machine_mode fmode)
5640 {
5641 size_t opname_len = strlen (opname);
5642 size_t mname_len = 0;
5643
5644 const char *fname, *tname;
5645 const char *q;
5646 char *libfunc_name, *suffix;
5647 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5648 char *p;
5649
5650 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5651 depends on which underlying decimal floating point format is used. */
5652 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5653
5654 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5655
5656 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5657 nondec_name[0] = '_';
5658 nondec_name[1] = '_';
5659 memcpy (&nondec_name[2], opname, opname_len);
5660 nondec_suffix = nondec_name + opname_len + 2;
5661
5662 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5663 dec_name[0] = '_';
5664 dec_name[1] = '_';
5665 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5666 memcpy (&dec_name[2+dec_len], opname, opname_len);
5667 dec_suffix = dec_name + dec_len + opname_len + 2;
5668
5669 fname = GET_MODE_NAME (fmode);
5670 tname = GET_MODE_NAME (tmode);
5671
5672 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5673 {
5674 libfunc_name = dec_name;
5675 suffix = dec_suffix;
5676 }
5677 else
5678 {
5679 libfunc_name = nondec_name;
5680 suffix = nondec_suffix;
5681 }
5682
5683 p = suffix;
5684 for (q = fname; *q; p++, q++)
5685 *p = TOLOWER (*q);
5686 for (q = tname; *q; p++, q++)
5687 *p = TOLOWER (*q);
5688
5689 *p = '\0';
5690
5691 set_conv_libfunc (tab, tmode, fmode,
5692 ggc_alloc_string (libfunc_name, p - libfunc_name));
5693 }
5694
5695 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5696 int->fp conversion. */
5697
5698 static void
5699 gen_int_to_fp_conv_libfunc (convert_optab tab,
5700 const char *opname,
5701 enum machine_mode tmode,
5702 enum machine_mode fmode)
5703 {
5704 if (GET_MODE_CLASS (fmode) != MODE_INT)
5705 return;
5706 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5707 return;
5708 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5709 }
5710
5711 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5712 naming scheme. */
5713
5714 static void
5715 gen_ufloat_conv_libfunc (convert_optab tab,
5716 const char *opname ATTRIBUTE_UNUSED,
5717 enum machine_mode tmode,
5718 enum machine_mode fmode)
5719 {
5720 if (DECIMAL_FLOAT_MODE_P (tmode))
5721 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5722 else
5723 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5724 }
5725
5726 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5727 fp->int conversion. */
5728
5729 static void
5730 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5731 const char *opname,
5732 enum machine_mode tmode,
5733 enum machine_mode fmode)
5734 {
5735 if (GET_MODE_CLASS (fmode) != MODE_INT)
5736 return;
5737 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5738 return;
5739 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5740 }
5741
5742 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5743 fp->int conversion with no decimal floating point involved. */
5744
5745 static void
5746 gen_fp_to_int_conv_libfunc (convert_optab tab,
5747 const char *opname,
5748 enum machine_mode tmode,
5749 enum machine_mode fmode)
5750 {
5751 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5752 return;
5753 if (GET_MODE_CLASS (tmode) != MODE_INT)
5754 return;
5755 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5756 }
5757
5758 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5759 The string formation rules are
5760 similar to the ones for init_libfunc, above. */
5761
5762 static void
5763 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5764 enum machine_mode tmode, enum machine_mode fmode)
5765 {
5766 size_t opname_len = strlen (opname);
5767 size_t mname_len = 0;
5768
5769 const char *fname, *tname;
5770 const char *q;
5771 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5772 char *libfunc_name, *suffix;
5773 char *p;
5774
5775 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5776 depends on which underlying decimal floating point format is used. */
5777 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5778
5779 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5780
5781 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5782 nondec_name[0] = '_';
5783 nondec_name[1] = '_';
5784 memcpy (&nondec_name[2], opname, opname_len);
5785 nondec_suffix = nondec_name + opname_len + 2;
5786
5787 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5788 dec_name[0] = '_';
5789 dec_name[1] = '_';
5790 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5791 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5792 dec_suffix = dec_name + dec_len + opname_len + 2;
5793
5794 fname = GET_MODE_NAME (fmode);
5795 tname = GET_MODE_NAME (tmode);
5796
5797 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5798 {
5799 libfunc_name = dec_name;
5800 suffix = dec_suffix;
5801 }
5802 else
5803 {
5804 libfunc_name = nondec_name;
5805 suffix = nondec_suffix;
5806 }
5807
5808 p = suffix;
5809 for (q = fname; *q; p++, q++)
5810 *p = TOLOWER (*q);
5811 for (q = tname; *q; p++, q++)
5812 *p = TOLOWER (*q);
5813
5814 *p++ = '2';
5815 *p = '\0';
5816
5817 set_conv_libfunc (tab, tmode, fmode,
5818 ggc_alloc_string (libfunc_name, p - libfunc_name));
5819 }
5820
5821 /* Pick proper libcall for trunc_optab. We need to chose if we do
5822 truncation or extension and interclass or intraclass. */
5823
5824 static void
5825 gen_trunc_conv_libfunc (convert_optab tab,
5826 const char *opname,
5827 enum machine_mode tmode,
5828 enum machine_mode fmode)
5829 {
5830 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5831 return;
5832 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5833 return;
5834 if (tmode == fmode)
5835 return;
5836
5837 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5838 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5839 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5840
5841 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5842 return;
5843
5844 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5845 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5846 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5847 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5848 }
5849
5850 /* Pick proper libcall for extend_optab. We need to chose if we do
5851 truncation or extension and interclass or intraclass. */
5852
5853 static void
5854 gen_extend_conv_libfunc (convert_optab tab,
5855 const char *opname ATTRIBUTE_UNUSED,
5856 enum machine_mode tmode,
5857 enum machine_mode fmode)
5858 {
5859 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5860 return;
5861 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5862 return;
5863 if (tmode == fmode)
5864 return;
5865
5866 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5867 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5868 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5869
5870 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5871 return;
5872
5873 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5874 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5875 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5876 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5877 }
5878
5879 /* Pick proper libcall for fract_optab. We need to chose if we do
5880 interclass or intraclass. */
5881
5882 static void
5883 gen_fract_conv_libfunc (convert_optab tab,
5884 const char *opname,
5885 enum machine_mode tmode,
5886 enum machine_mode fmode)
5887 {
5888 if (tmode == fmode)
5889 return;
5890 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5891 return;
5892
5893 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5894 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5895 else
5896 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5897 }
5898
5899 /* Pick proper libcall for fractuns_optab. */
5900
5901 static void
5902 gen_fractuns_conv_libfunc (convert_optab tab,
5903 const char *opname,
5904 enum machine_mode tmode,
5905 enum machine_mode fmode)
5906 {
5907 if (tmode == fmode)
5908 return;
5909 /* One mode must be a fixed-point mode, and the other must be an integer
5910 mode. */
5911 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
5912 || (ALL_FIXED_POINT_MODE_P (fmode)
5913 && GET_MODE_CLASS (tmode) == MODE_INT)))
5914 return;
5915
5916 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5917 }
5918
5919 /* Pick proper libcall for satfract_optab. We need to chose if we do
5920 interclass or intraclass. */
5921
5922 static void
5923 gen_satfract_conv_libfunc (convert_optab tab,
5924 const char *opname,
5925 enum machine_mode tmode,
5926 enum machine_mode fmode)
5927 {
5928 if (tmode == fmode)
5929 return;
5930 /* TMODE must be a fixed-point mode. */
5931 if (!ALL_FIXED_POINT_MODE_P (tmode))
5932 return;
5933
5934 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5935 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5936 else
5937 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5938 }
5939
5940 /* Pick proper libcall for satfractuns_optab. */
5941
5942 static void
5943 gen_satfractuns_conv_libfunc (convert_optab tab,
5944 const char *opname,
5945 enum machine_mode tmode,
5946 enum machine_mode fmode)
5947 {
5948 if (tmode == fmode)
5949 return;
5950 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
5951 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
5952 return;
5953
5954 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5955 }
5956
5957 /* A table of previously-created libfuncs, hashed by name. */
5958 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
5959
5960 /* Hashtable callbacks for libfunc_decls. */
5961
5962 static hashval_t
5963 libfunc_decl_hash (const void *entry)
5964 {
5965 return IDENTIFIER_HASH_VALUE (DECL_NAME ((const_tree) entry));
5966 }
5967
5968 static int
5969 libfunc_decl_eq (const void *entry1, const void *entry2)
5970 {
5971 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
5972 }
5973
5974 /* Build a decl for a libfunc named NAME. */
5975
5976 tree
5977 build_libfunc_function (const char *name)
5978 {
5979 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
5980 get_identifier (name),
5981 build_function_type (integer_type_node, NULL_TREE));
5982 /* ??? We don't have any type information except for this is
5983 a function. Pretend this is "int foo()". */
5984 DECL_ARTIFICIAL (decl) = 1;
5985 DECL_EXTERNAL (decl) = 1;
5986 TREE_PUBLIC (decl) = 1;
5987 gcc_assert (DECL_ASSEMBLER_NAME (decl));
5988
5989 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
5990 are the flags assigned by targetm.encode_section_info. */
5991 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
5992
5993 return decl;
5994 }
5995
5996 rtx
5997 init_one_libfunc (const char *name)
5998 {
5999 tree id, decl;
6000 void **slot;
6001 hashval_t hash;
6002
6003 if (libfunc_decls == NULL)
6004 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
6005 libfunc_decl_eq, NULL);
6006
6007 /* See if we have already created a libfunc decl for this function. */
6008 id = get_identifier (name);
6009 hash = IDENTIFIER_HASH_VALUE (id);
6010 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
6011 decl = (tree) *slot;
6012 if (decl == NULL)
6013 {
6014 /* Create a new decl, so that it can be passed to
6015 targetm.encode_section_info. */
6016 decl = build_libfunc_function (name);
6017 *slot = decl;
6018 }
6019 return XEXP (DECL_RTL (decl), 0);
6020 }
6021
6022 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6023
6024 rtx
6025 set_user_assembler_libfunc (const char *name, const char *asmspec)
6026 {
6027 tree id, decl;
6028 void **slot;
6029 hashval_t hash;
6030
6031 id = get_identifier (name);
6032 hash = IDENTIFIER_HASH_VALUE (id);
6033 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
6034 gcc_assert (slot);
6035 decl = (tree) *slot;
6036 set_user_assembler_name (decl, asmspec);
6037 return XEXP (DECL_RTL (decl), 0);
6038 }
6039
6040 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6041 MODE to NAME, which should be either 0 or a string constant. */
6042 void
6043 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
6044 {
6045 rtx val;
6046 struct libfunc_entry e;
6047 struct libfunc_entry **slot;
6048 e.optab = (size_t) (optable - &optab_table[0]);
6049 e.mode1 = mode;
6050 e.mode2 = VOIDmode;
6051
6052 if (name)
6053 val = init_one_libfunc (name);
6054 else
6055 val = 0;
6056 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6057 if (*slot == NULL)
6058 *slot = ggc_alloc_libfunc_entry ();
6059 (*slot)->optab = (size_t) (optable - &optab_table[0]);
6060 (*slot)->mode1 = mode;
6061 (*slot)->mode2 = VOIDmode;
6062 (*slot)->libfunc = val;
6063 }
6064
6065 /* Call this to reset the function entry for one conversion optab
6066 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6067 either 0 or a string constant. */
6068 void
6069 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
6070 enum machine_mode fmode, const char *name)
6071 {
6072 rtx val;
6073 struct libfunc_entry e;
6074 struct libfunc_entry **slot;
6075 e.optab = (size_t) (optable - &convert_optab_table[0]);
6076 e.mode1 = tmode;
6077 e.mode2 = fmode;
6078
6079 if (name)
6080 val = init_one_libfunc (name);
6081 else
6082 val = 0;
6083 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6084 if (*slot == NULL)
6085 *slot = ggc_alloc_libfunc_entry ();
6086 (*slot)->optab = (size_t) (optable - &convert_optab_table[0]);
6087 (*slot)->mode1 = tmode;
6088 (*slot)->mode2 = fmode;
6089 (*slot)->libfunc = val;
6090 }
6091
6092 /* Call this to initialize the contents of the optabs
6093 appropriately for the current target machine. */
6094
6095 void
6096 init_optabs (void)
6097 {
6098 if (libfunc_hash)
6099 {
6100 htab_empty (libfunc_hash);
6101 /* We statically initialize the insn_codes with the equivalent of
6102 CODE_FOR_nothing. Repeat the process if reinitialising. */
6103 init_insn_codes ();
6104 }
6105 else
6106 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
6107
6108 init_optab (add_optab, PLUS);
6109 init_optabv (addv_optab, PLUS);
6110 init_optab (sub_optab, MINUS);
6111 init_optabv (subv_optab, MINUS);
6112 init_optab (ssadd_optab, SS_PLUS);
6113 init_optab (usadd_optab, US_PLUS);
6114 init_optab (sssub_optab, SS_MINUS);
6115 init_optab (ussub_optab, US_MINUS);
6116 init_optab (smul_optab, MULT);
6117 init_optab (ssmul_optab, SS_MULT);
6118 init_optab (usmul_optab, US_MULT);
6119 init_optabv (smulv_optab, MULT);
6120 init_optab (smul_highpart_optab, UNKNOWN);
6121 init_optab (umul_highpart_optab, UNKNOWN);
6122 init_optab (smul_widen_optab, UNKNOWN);
6123 init_optab (umul_widen_optab, UNKNOWN);
6124 init_optab (usmul_widen_optab, UNKNOWN);
6125 init_optab (smadd_widen_optab, UNKNOWN);
6126 init_optab (umadd_widen_optab, UNKNOWN);
6127 init_optab (ssmadd_widen_optab, UNKNOWN);
6128 init_optab (usmadd_widen_optab, UNKNOWN);
6129 init_optab (smsub_widen_optab, UNKNOWN);
6130 init_optab (umsub_widen_optab, UNKNOWN);
6131 init_optab (ssmsub_widen_optab, UNKNOWN);
6132 init_optab (usmsub_widen_optab, UNKNOWN);
6133 init_optab (sdiv_optab, DIV);
6134 init_optab (ssdiv_optab, SS_DIV);
6135 init_optab (usdiv_optab, US_DIV);
6136 init_optabv (sdivv_optab, DIV);
6137 init_optab (sdivmod_optab, UNKNOWN);
6138 init_optab (udiv_optab, UDIV);
6139 init_optab (udivmod_optab, UNKNOWN);
6140 init_optab (smod_optab, MOD);
6141 init_optab (umod_optab, UMOD);
6142 init_optab (fmod_optab, UNKNOWN);
6143 init_optab (remainder_optab, UNKNOWN);
6144 init_optab (ftrunc_optab, UNKNOWN);
6145 init_optab (and_optab, AND);
6146 init_optab (ior_optab, IOR);
6147 init_optab (xor_optab, XOR);
6148 init_optab (ashl_optab, ASHIFT);
6149 init_optab (ssashl_optab, SS_ASHIFT);
6150 init_optab (usashl_optab, US_ASHIFT);
6151 init_optab (ashr_optab, ASHIFTRT);
6152 init_optab (lshr_optab, LSHIFTRT);
6153 init_optab (rotl_optab, ROTATE);
6154 init_optab (rotr_optab, ROTATERT);
6155 init_optab (smin_optab, SMIN);
6156 init_optab (smax_optab, SMAX);
6157 init_optab (umin_optab, UMIN);
6158 init_optab (umax_optab, UMAX);
6159 init_optab (pow_optab, UNKNOWN);
6160 init_optab (atan2_optab, UNKNOWN);
6161 init_optab (fma_optab, FMA);
6162 init_optab (fms_optab, UNKNOWN);
6163 init_optab (fnma_optab, UNKNOWN);
6164 init_optab (fnms_optab, UNKNOWN);
6165
6166 /* These three have codes assigned exclusively for the sake of
6167 have_insn_for. */
6168 init_optab (mov_optab, SET);
6169 init_optab (movstrict_optab, STRICT_LOW_PART);
6170 init_optab (cbranch_optab, COMPARE);
6171
6172 init_optab (cmov_optab, UNKNOWN);
6173 init_optab (cstore_optab, UNKNOWN);
6174 init_optab (ctrap_optab, UNKNOWN);
6175
6176 init_optab (storent_optab, UNKNOWN);
6177
6178 init_optab (cmp_optab, UNKNOWN);
6179 init_optab (ucmp_optab, UNKNOWN);
6180
6181 init_optab (eq_optab, EQ);
6182 init_optab (ne_optab, NE);
6183 init_optab (gt_optab, GT);
6184 init_optab (ge_optab, GE);
6185 init_optab (lt_optab, LT);
6186 init_optab (le_optab, LE);
6187 init_optab (unord_optab, UNORDERED);
6188
6189 init_optab (neg_optab, NEG);
6190 init_optab (ssneg_optab, SS_NEG);
6191 init_optab (usneg_optab, US_NEG);
6192 init_optabv (negv_optab, NEG);
6193 init_optab (abs_optab, ABS);
6194 init_optabv (absv_optab, ABS);
6195 init_optab (addcc_optab, UNKNOWN);
6196 init_optab (one_cmpl_optab, NOT);
6197 init_optab (bswap_optab, BSWAP);
6198 init_optab (ffs_optab, FFS);
6199 init_optab (clz_optab, CLZ);
6200 init_optab (ctz_optab, CTZ);
6201 init_optab (popcount_optab, POPCOUNT);
6202 init_optab (parity_optab, PARITY);
6203 init_optab (sqrt_optab, SQRT);
6204 init_optab (floor_optab, UNKNOWN);
6205 init_optab (ceil_optab, UNKNOWN);
6206 init_optab (round_optab, UNKNOWN);
6207 init_optab (btrunc_optab, UNKNOWN);
6208 init_optab (nearbyint_optab, UNKNOWN);
6209 init_optab (rint_optab, UNKNOWN);
6210 init_optab (sincos_optab, UNKNOWN);
6211 init_optab (sin_optab, UNKNOWN);
6212 init_optab (asin_optab, UNKNOWN);
6213 init_optab (cos_optab, UNKNOWN);
6214 init_optab (acos_optab, UNKNOWN);
6215 init_optab (exp_optab, UNKNOWN);
6216 init_optab (exp10_optab, UNKNOWN);
6217 init_optab (exp2_optab, UNKNOWN);
6218 init_optab (expm1_optab, UNKNOWN);
6219 init_optab (ldexp_optab, UNKNOWN);
6220 init_optab (scalb_optab, UNKNOWN);
6221 init_optab (significand_optab, UNKNOWN);
6222 init_optab (logb_optab, UNKNOWN);
6223 init_optab (ilogb_optab, UNKNOWN);
6224 init_optab (log_optab, UNKNOWN);
6225 init_optab (log10_optab, UNKNOWN);
6226 init_optab (log2_optab, UNKNOWN);
6227 init_optab (log1p_optab, UNKNOWN);
6228 init_optab (tan_optab, UNKNOWN);
6229 init_optab (atan_optab, UNKNOWN);
6230 init_optab (copysign_optab, UNKNOWN);
6231 init_optab (signbit_optab, UNKNOWN);
6232
6233 init_optab (isinf_optab, UNKNOWN);
6234
6235 init_optab (strlen_optab, UNKNOWN);
6236 init_optab (push_optab, UNKNOWN);
6237
6238 init_optab (reduc_smax_optab, UNKNOWN);
6239 init_optab (reduc_umax_optab, UNKNOWN);
6240 init_optab (reduc_smin_optab, UNKNOWN);
6241 init_optab (reduc_umin_optab, UNKNOWN);
6242 init_optab (reduc_splus_optab, UNKNOWN);
6243 init_optab (reduc_uplus_optab, UNKNOWN);
6244
6245 init_optab (ssum_widen_optab, UNKNOWN);
6246 init_optab (usum_widen_optab, UNKNOWN);
6247 init_optab (sdot_prod_optab, UNKNOWN);
6248 init_optab (udot_prod_optab, UNKNOWN);
6249
6250 init_optab (vec_extract_optab, UNKNOWN);
6251 init_optab (vec_extract_even_optab, UNKNOWN);
6252 init_optab (vec_extract_odd_optab, UNKNOWN);
6253 init_optab (vec_interleave_high_optab, UNKNOWN);
6254 init_optab (vec_interleave_low_optab, UNKNOWN);
6255 init_optab (vec_set_optab, UNKNOWN);
6256 init_optab (vec_init_optab, UNKNOWN);
6257 init_optab (vec_shl_optab, UNKNOWN);
6258 init_optab (vec_shr_optab, UNKNOWN);
6259 init_optab (vec_realign_load_optab, UNKNOWN);
6260 init_optab (movmisalign_optab, UNKNOWN);
6261 init_optab (vec_widen_umult_hi_optab, UNKNOWN);
6262 init_optab (vec_widen_umult_lo_optab, UNKNOWN);
6263 init_optab (vec_widen_smult_hi_optab, UNKNOWN);
6264 init_optab (vec_widen_smult_lo_optab, UNKNOWN);
6265 init_optab (vec_unpacks_hi_optab, UNKNOWN);
6266 init_optab (vec_unpacks_lo_optab, UNKNOWN);
6267 init_optab (vec_unpacku_hi_optab, UNKNOWN);
6268 init_optab (vec_unpacku_lo_optab, UNKNOWN);
6269 init_optab (vec_unpacks_float_hi_optab, UNKNOWN);
6270 init_optab (vec_unpacks_float_lo_optab, UNKNOWN);
6271 init_optab (vec_unpacku_float_hi_optab, UNKNOWN);
6272 init_optab (vec_unpacku_float_lo_optab, UNKNOWN);
6273 init_optab (vec_pack_trunc_optab, UNKNOWN);
6274 init_optab (vec_pack_usat_optab, UNKNOWN);
6275 init_optab (vec_pack_ssat_optab, UNKNOWN);
6276 init_optab (vec_pack_ufix_trunc_optab, UNKNOWN);
6277 init_optab (vec_pack_sfix_trunc_optab, UNKNOWN);
6278
6279 init_optab (powi_optab, UNKNOWN);
6280
6281 /* Conversions. */
6282 init_convert_optab (sext_optab, SIGN_EXTEND);
6283 init_convert_optab (zext_optab, ZERO_EXTEND);
6284 init_convert_optab (trunc_optab, TRUNCATE);
6285 init_convert_optab (sfix_optab, FIX);
6286 init_convert_optab (ufix_optab, UNSIGNED_FIX);
6287 init_convert_optab (sfixtrunc_optab, UNKNOWN);
6288 init_convert_optab (ufixtrunc_optab, UNKNOWN);
6289 init_convert_optab (sfloat_optab, FLOAT);
6290 init_convert_optab (ufloat_optab, UNSIGNED_FLOAT);
6291 init_convert_optab (lrint_optab, UNKNOWN);
6292 init_convert_optab (lround_optab, UNKNOWN);
6293 init_convert_optab (lfloor_optab, UNKNOWN);
6294 init_convert_optab (lceil_optab, UNKNOWN);
6295
6296 init_convert_optab (fract_optab, FRACT_CONVERT);
6297 init_convert_optab (fractuns_optab, UNSIGNED_FRACT_CONVERT);
6298 init_convert_optab (satfract_optab, SAT_FRACT);
6299 init_convert_optab (satfractuns_optab, UNSIGNED_SAT_FRACT);
6300
6301 /* Fill in the optabs with the insns we support. */
6302 init_all_optabs ();
6303
6304 /* Initialize the optabs with the names of the library functions. */
6305 add_optab->libcall_basename = "add";
6306 add_optab->libcall_suffix = '3';
6307 add_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6308 addv_optab->libcall_basename = "add";
6309 addv_optab->libcall_suffix = '3';
6310 addv_optab->libcall_gen = gen_intv_fp_libfunc;
6311 ssadd_optab->libcall_basename = "ssadd";
6312 ssadd_optab->libcall_suffix = '3';
6313 ssadd_optab->libcall_gen = gen_signed_fixed_libfunc;
6314 usadd_optab->libcall_basename = "usadd";
6315 usadd_optab->libcall_suffix = '3';
6316 usadd_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6317 sub_optab->libcall_basename = "sub";
6318 sub_optab->libcall_suffix = '3';
6319 sub_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6320 subv_optab->libcall_basename = "sub";
6321 subv_optab->libcall_suffix = '3';
6322 subv_optab->libcall_gen = gen_intv_fp_libfunc;
6323 sssub_optab->libcall_basename = "sssub";
6324 sssub_optab->libcall_suffix = '3';
6325 sssub_optab->libcall_gen = gen_signed_fixed_libfunc;
6326 ussub_optab->libcall_basename = "ussub";
6327 ussub_optab->libcall_suffix = '3';
6328 ussub_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6329 smul_optab->libcall_basename = "mul";
6330 smul_optab->libcall_suffix = '3';
6331 smul_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6332 smulv_optab->libcall_basename = "mul";
6333 smulv_optab->libcall_suffix = '3';
6334 smulv_optab->libcall_gen = gen_intv_fp_libfunc;
6335 ssmul_optab->libcall_basename = "ssmul";
6336 ssmul_optab->libcall_suffix = '3';
6337 ssmul_optab->libcall_gen = gen_signed_fixed_libfunc;
6338 usmul_optab->libcall_basename = "usmul";
6339 usmul_optab->libcall_suffix = '3';
6340 usmul_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6341 sdiv_optab->libcall_basename = "div";
6342 sdiv_optab->libcall_suffix = '3';
6343 sdiv_optab->libcall_gen = gen_int_fp_signed_fixed_libfunc;
6344 sdivv_optab->libcall_basename = "divv";
6345 sdivv_optab->libcall_suffix = '3';
6346 sdivv_optab->libcall_gen = gen_int_libfunc;
6347 ssdiv_optab->libcall_basename = "ssdiv";
6348 ssdiv_optab->libcall_suffix = '3';
6349 ssdiv_optab->libcall_gen = gen_signed_fixed_libfunc;
6350 udiv_optab->libcall_basename = "udiv";
6351 udiv_optab->libcall_suffix = '3';
6352 udiv_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6353 usdiv_optab->libcall_basename = "usdiv";
6354 usdiv_optab->libcall_suffix = '3';
6355 usdiv_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6356 sdivmod_optab->libcall_basename = "divmod";
6357 sdivmod_optab->libcall_suffix = '4';
6358 sdivmod_optab->libcall_gen = gen_int_libfunc;
6359 udivmod_optab->libcall_basename = "udivmod";
6360 udivmod_optab->libcall_suffix = '4';
6361 udivmod_optab->libcall_gen = gen_int_libfunc;
6362 smod_optab->libcall_basename = "mod";
6363 smod_optab->libcall_suffix = '3';
6364 smod_optab->libcall_gen = gen_int_libfunc;
6365 umod_optab->libcall_basename = "umod";
6366 umod_optab->libcall_suffix = '3';
6367 umod_optab->libcall_gen = gen_int_libfunc;
6368 ftrunc_optab->libcall_basename = "ftrunc";
6369 ftrunc_optab->libcall_suffix = '2';
6370 ftrunc_optab->libcall_gen = gen_fp_libfunc;
6371 and_optab->libcall_basename = "and";
6372 and_optab->libcall_suffix = '3';
6373 and_optab->libcall_gen = gen_int_libfunc;
6374 ior_optab->libcall_basename = "ior";
6375 ior_optab->libcall_suffix = '3';
6376 ior_optab->libcall_gen = gen_int_libfunc;
6377 xor_optab->libcall_basename = "xor";
6378 xor_optab->libcall_suffix = '3';
6379 xor_optab->libcall_gen = gen_int_libfunc;
6380 ashl_optab->libcall_basename = "ashl";
6381 ashl_optab->libcall_suffix = '3';
6382 ashl_optab->libcall_gen = gen_int_fixed_libfunc;
6383 ssashl_optab->libcall_basename = "ssashl";
6384 ssashl_optab->libcall_suffix = '3';
6385 ssashl_optab->libcall_gen = gen_signed_fixed_libfunc;
6386 usashl_optab->libcall_basename = "usashl";
6387 usashl_optab->libcall_suffix = '3';
6388 usashl_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6389 ashr_optab->libcall_basename = "ashr";
6390 ashr_optab->libcall_suffix = '3';
6391 ashr_optab->libcall_gen = gen_int_signed_fixed_libfunc;
6392 lshr_optab->libcall_basename = "lshr";
6393 lshr_optab->libcall_suffix = '3';
6394 lshr_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6395 smin_optab->libcall_basename = "min";
6396 smin_optab->libcall_suffix = '3';
6397 smin_optab->libcall_gen = gen_int_fp_libfunc;
6398 smax_optab->libcall_basename = "max";
6399 smax_optab->libcall_suffix = '3';
6400 smax_optab->libcall_gen = gen_int_fp_libfunc;
6401 umin_optab->libcall_basename = "umin";
6402 umin_optab->libcall_suffix = '3';
6403 umin_optab->libcall_gen = gen_int_libfunc;
6404 umax_optab->libcall_basename = "umax";
6405 umax_optab->libcall_suffix = '3';
6406 umax_optab->libcall_gen = gen_int_libfunc;
6407 neg_optab->libcall_basename = "neg";
6408 neg_optab->libcall_suffix = '2';
6409 neg_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6410 ssneg_optab->libcall_basename = "ssneg";
6411 ssneg_optab->libcall_suffix = '2';
6412 ssneg_optab->libcall_gen = gen_signed_fixed_libfunc;
6413 usneg_optab->libcall_basename = "usneg";
6414 usneg_optab->libcall_suffix = '2';
6415 usneg_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6416 negv_optab->libcall_basename = "neg";
6417 negv_optab->libcall_suffix = '2';
6418 negv_optab->libcall_gen = gen_intv_fp_libfunc;
6419 one_cmpl_optab->libcall_basename = "one_cmpl";
6420 one_cmpl_optab->libcall_suffix = '2';
6421 one_cmpl_optab->libcall_gen = gen_int_libfunc;
6422 ffs_optab->libcall_basename = "ffs";
6423 ffs_optab->libcall_suffix = '2';
6424 ffs_optab->libcall_gen = gen_int_libfunc;
6425 clz_optab->libcall_basename = "clz";
6426 clz_optab->libcall_suffix = '2';
6427 clz_optab->libcall_gen = gen_int_libfunc;
6428 ctz_optab->libcall_basename = "ctz";
6429 ctz_optab->libcall_suffix = '2';
6430 ctz_optab->libcall_gen = gen_int_libfunc;
6431 popcount_optab->libcall_basename = "popcount";
6432 popcount_optab->libcall_suffix = '2';
6433 popcount_optab->libcall_gen = gen_int_libfunc;
6434 parity_optab->libcall_basename = "parity";
6435 parity_optab->libcall_suffix = '2';
6436 parity_optab->libcall_gen = gen_int_libfunc;
6437
6438 /* Comparison libcalls for integers MUST come in pairs,
6439 signed/unsigned. */
6440 cmp_optab->libcall_basename = "cmp";
6441 cmp_optab->libcall_suffix = '2';
6442 cmp_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6443 ucmp_optab->libcall_basename = "ucmp";
6444 ucmp_optab->libcall_suffix = '2';
6445 ucmp_optab->libcall_gen = gen_int_libfunc;
6446
6447 /* EQ etc are floating point only. */
6448 eq_optab->libcall_basename = "eq";
6449 eq_optab->libcall_suffix = '2';
6450 eq_optab->libcall_gen = gen_fp_libfunc;
6451 ne_optab->libcall_basename = "ne";
6452 ne_optab->libcall_suffix = '2';
6453 ne_optab->libcall_gen = gen_fp_libfunc;
6454 gt_optab->libcall_basename = "gt";
6455 gt_optab->libcall_suffix = '2';
6456 gt_optab->libcall_gen = gen_fp_libfunc;
6457 ge_optab->libcall_basename = "ge";
6458 ge_optab->libcall_suffix = '2';
6459 ge_optab->libcall_gen = gen_fp_libfunc;
6460 lt_optab->libcall_basename = "lt";
6461 lt_optab->libcall_suffix = '2';
6462 lt_optab->libcall_gen = gen_fp_libfunc;
6463 le_optab->libcall_basename = "le";
6464 le_optab->libcall_suffix = '2';
6465 le_optab->libcall_gen = gen_fp_libfunc;
6466 unord_optab->libcall_basename = "unord";
6467 unord_optab->libcall_suffix = '2';
6468 unord_optab->libcall_gen = gen_fp_libfunc;
6469
6470 powi_optab->libcall_basename = "powi";
6471 powi_optab->libcall_suffix = '2';
6472 powi_optab->libcall_gen = gen_fp_libfunc;
6473
6474 /* Conversions. */
6475 sfloat_optab->libcall_basename = "float";
6476 sfloat_optab->libcall_gen = gen_int_to_fp_conv_libfunc;
6477 ufloat_optab->libcall_gen = gen_ufloat_conv_libfunc;
6478 sfix_optab->libcall_basename = "fix";
6479 sfix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6480 ufix_optab->libcall_basename = "fixuns";
6481 ufix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6482 lrint_optab->libcall_basename = "lrint";
6483 lrint_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6484 lround_optab->libcall_basename = "lround";
6485 lround_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6486 lfloor_optab->libcall_basename = "lfloor";
6487 lfloor_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6488 lceil_optab->libcall_basename = "lceil";
6489 lceil_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6490
6491 /* trunc_optab is also used for FLOAT_EXTEND. */
6492 sext_optab->libcall_basename = "extend";
6493 sext_optab->libcall_gen = gen_extend_conv_libfunc;
6494 trunc_optab->libcall_basename = "trunc";
6495 trunc_optab->libcall_gen = gen_trunc_conv_libfunc;
6496
6497 /* Conversions for fixed-point modes and other modes. */
6498 fract_optab->libcall_basename = "fract";
6499 fract_optab->libcall_gen = gen_fract_conv_libfunc;
6500 satfract_optab->libcall_basename = "satfract";
6501 satfract_optab->libcall_gen = gen_satfract_conv_libfunc;
6502 fractuns_optab->libcall_basename = "fractuns";
6503 fractuns_optab->libcall_gen = gen_fractuns_conv_libfunc;
6504 satfractuns_optab->libcall_basename = "satfractuns";
6505 satfractuns_optab->libcall_gen = gen_satfractuns_conv_libfunc;
6506
6507 /* The ffs function operates on `int'. Fall back on it if we do not
6508 have a libgcc2 function for that width. */
6509 if (INT_TYPE_SIZE < BITS_PER_WORD)
6510 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6511 "ffs");
6512
6513 /* Explicitly initialize the bswap libfuncs since we need them to be
6514 valid for things other than word_mode. */
6515 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6516 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6517
6518 /* Use cabs for double complex abs, since systems generally have cabs.
6519 Don't define any libcall for float complex, so that cabs will be used. */
6520 if (complex_double_type_node)
6521 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node), "cabs");
6522
6523 abort_libfunc = init_one_libfunc ("abort");
6524 memcpy_libfunc = init_one_libfunc ("memcpy");
6525 memmove_libfunc = init_one_libfunc ("memmove");
6526 memcmp_libfunc = init_one_libfunc ("memcmp");
6527 memset_libfunc = init_one_libfunc ("memset");
6528 setbits_libfunc = init_one_libfunc ("__setbits");
6529
6530 #ifndef DONT_USE_BUILTIN_SETJMP
6531 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6532 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6533 #else
6534 setjmp_libfunc = init_one_libfunc ("setjmp");
6535 longjmp_libfunc = init_one_libfunc ("longjmp");
6536 #endif
6537 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6538 unwind_sjlj_unregister_libfunc
6539 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6540
6541 /* For function entry/exit instrumentation. */
6542 profile_function_entry_libfunc
6543 = init_one_libfunc ("__cyg_profile_func_enter");
6544 profile_function_exit_libfunc
6545 = init_one_libfunc ("__cyg_profile_func_exit");
6546
6547 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6548
6549 /* Allow the target to add more libcalls or rename some, etc. */
6550 targetm.init_libfuncs ();
6551 }
6552
6553 /* Print information about the current contents of the optabs on
6554 STDERR. */
6555
6556 DEBUG_FUNCTION void
6557 debug_optab_libfuncs (void)
6558 {
6559 int i;
6560 int j;
6561 int k;
6562
6563 /* Dump the arithmetic optabs. */
6564 for (i = 0; i != (int) OTI_MAX; i++)
6565 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6566 {
6567 optab o;
6568 rtx l;
6569
6570 o = &optab_table[i];
6571 l = optab_libfunc (o, (enum machine_mode) j);
6572 if (l)
6573 {
6574 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6575 fprintf (stderr, "%s\t%s:\t%s\n",
6576 GET_RTX_NAME (o->code),
6577 GET_MODE_NAME (j),
6578 XSTR (l, 0));
6579 }
6580 }
6581
6582 /* Dump the conversion optabs. */
6583 for (i = 0; i < (int) COI_MAX; ++i)
6584 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6585 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6586 {
6587 convert_optab o;
6588 rtx l;
6589
6590 o = &convert_optab_table[i];
6591 l = convert_optab_libfunc (o, (enum machine_mode) j,
6592 (enum machine_mode) k);
6593 if (l)
6594 {
6595 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6596 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6597 GET_RTX_NAME (o->code),
6598 GET_MODE_NAME (j),
6599 GET_MODE_NAME (k),
6600 XSTR (l, 0));
6601 }
6602 }
6603 }
6604
6605 \f
6606 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6607 CODE. Return 0 on failure. */
6608
6609 rtx
6610 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6611 {
6612 enum machine_mode mode = GET_MODE (op1);
6613 enum insn_code icode;
6614 rtx insn;
6615 rtx trap_rtx;
6616
6617 if (mode == VOIDmode)
6618 return 0;
6619
6620 icode = optab_handler (ctrap_optab, mode);
6621 if (icode == CODE_FOR_nothing)
6622 return 0;
6623
6624 /* Some targets only accept a zero trap code. */
6625 if (!insn_operand_matches (icode, 3, tcode))
6626 return 0;
6627
6628 do_pending_stack_adjust ();
6629 start_sequence ();
6630 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6631 &trap_rtx, &mode);
6632 if (!trap_rtx)
6633 insn = NULL_RTX;
6634 else
6635 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6636 tcode);
6637
6638 /* If that failed, then give up. */
6639 if (insn == 0)
6640 {
6641 end_sequence ();
6642 return 0;
6643 }
6644
6645 emit_insn (insn);
6646 insn = get_insns ();
6647 end_sequence ();
6648 return insn;
6649 }
6650
6651 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6652 or unsigned operation code. */
6653
6654 static enum rtx_code
6655 get_rtx_code (enum tree_code tcode, bool unsignedp)
6656 {
6657 enum rtx_code code;
6658 switch (tcode)
6659 {
6660 case EQ_EXPR:
6661 code = EQ;
6662 break;
6663 case NE_EXPR:
6664 code = NE;
6665 break;
6666 case LT_EXPR:
6667 code = unsignedp ? LTU : LT;
6668 break;
6669 case LE_EXPR:
6670 code = unsignedp ? LEU : LE;
6671 break;
6672 case GT_EXPR:
6673 code = unsignedp ? GTU : GT;
6674 break;
6675 case GE_EXPR:
6676 code = unsignedp ? GEU : GE;
6677 break;
6678
6679 case UNORDERED_EXPR:
6680 code = UNORDERED;
6681 break;
6682 case ORDERED_EXPR:
6683 code = ORDERED;
6684 break;
6685 case UNLT_EXPR:
6686 code = UNLT;
6687 break;
6688 case UNLE_EXPR:
6689 code = UNLE;
6690 break;
6691 case UNGT_EXPR:
6692 code = UNGT;
6693 break;
6694 case UNGE_EXPR:
6695 code = UNGE;
6696 break;
6697 case UNEQ_EXPR:
6698 code = UNEQ;
6699 break;
6700 case LTGT_EXPR:
6701 code = LTGT;
6702 break;
6703
6704 default:
6705 gcc_unreachable ();
6706 }
6707 return code;
6708 }
6709
6710 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6711 unsigned operators. Do not generate compare instruction. */
6712
6713 static rtx
6714 vector_compare_rtx (tree cond, bool unsignedp, enum insn_code icode)
6715 {
6716 enum rtx_code rcode;
6717 tree t_op0, t_op1;
6718 rtx rtx_op0, rtx_op1;
6719
6720 /* This is unlikely. While generating VEC_COND_EXPR, auto vectorizer
6721 ensures that condition is a relational operation. */
6722 gcc_assert (COMPARISON_CLASS_P (cond));
6723
6724 rcode = get_rtx_code (TREE_CODE (cond), unsignedp);
6725 t_op0 = TREE_OPERAND (cond, 0);
6726 t_op1 = TREE_OPERAND (cond, 1);
6727
6728 /* Expand operands. */
6729 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6730 EXPAND_STACK_PARM);
6731 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6732 EXPAND_STACK_PARM);
6733
6734 if (!insn_data[icode].operand[4].predicate (rtx_op0, GET_MODE (rtx_op0))
6735 && GET_MODE (rtx_op0) != VOIDmode)
6736 rtx_op0 = force_reg (GET_MODE (rtx_op0), rtx_op0);
6737
6738 if (!insn_data[icode].operand[5].predicate (rtx_op1, GET_MODE (rtx_op1))
6739 && GET_MODE (rtx_op1) != VOIDmode)
6740 rtx_op1 = force_reg (GET_MODE (rtx_op1), rtx_op1);
6741
6742 return gen_rtx_fmt_ee (rcode, VOIDmode, rtx_op0, rtx_op1);
6743 }
6744
6745 /* Return insn code for TYPE, the type of a VEC_COND_EXPR. */
6746
6747 static inline enum insn_code
6748 get_vcond_icode (tree type, enum machine_mode mode)
6749 {
6750 enum insn_code icode = CODE_FOR_nothing;
6751
6752 if (TYPE_UNSIGNED (type))
6753 icode = direct_optab_handler (vcondu_optab, mode);
6754 else
6755 icode = direct_optab_handler (vcond_optab, mode);
6756 return icode;
6757 }
6758
6759 /* Return TRUE iff, appropriate vector insns are available
6760 for vector cond expr with type TYPE in VMODE mode. */
6761
6762 bool
6763 expand_vec_cond_expr_p (tree type, enum machine_mode vmode)
6764 {
6765 if (get_vcond_icode (type, vmode) == CODE_FOR_nothing)
6766 return false;
6767 return true;
6768 }
6769
6770 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6771 three operands. */
6772
6773 rtx
6774 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6775 rtx target)
6776 {
6777 enum insn_code icode;
6778 rtx comparison, rtx_op1, rtx_op2, cc_op0, cc_op1;
6779 enum machine_mode mode = TYPE_MODE (vec_cond_type);
6780 bool unsignedp = TYPE_UNSIGNED (vec_cond_type);
6781
6782 icode = get_vcond_icode (vec_cond_type, mode);
6783 if (icode == CODE_FOR_nothing)
6784 return 0;
6785
6786 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
6787 target = gen_reg_rtx (mode);
6788
6789 /* Get comparison rtx. First expand both cond expr operands. */
6790 comparison = vector_compare_rtx (op0,
6791 unsignedp, icode);
6792 cc_op0 = XEXP (comparison, 0);
6793 cc_op1 = XEXP (comparison, 1);
6794 /* Expand both operands and force them in reg, if required. */
6795 rtx_op1 = expand_normal (op1);
6796 if (!insn_data[icode].operand[1].predicate (rtx_op1, mode)
6797 && mode != VOIDmode)
6798 rtx_op1 = force_reg (mode, rtx_op1);
6799
6800 rtx_op2 = expand_normal (op2);
6801 if (!insn_data[icode].operand[2].predicate (rtx_op2, mode)
6802 && mode != VOIDmode)
6803 rtx_op2 = force_reg (mode, rtx_op2);
6804
6805 /* Emit instruction! */
6806 emit_insn (GEN_FCN (icode) (target, rtx_op1, rtx_op2,
6807 comparison, cc_op0, cc_op1));
6808
6809 return target;
6810 }
6811
6812 \f
6813 /* This is an internal subroutine of the other compare_and_swap expanders.
6814 MEM, OLD_VAL and NEW_VAL are as you'd expect for a compare-and-swap
6815 operation. TARGET is an optional place to store the value result of
6816 the operation. ICODE is the particular instruction to expand. Return
6817 the result of the operation. */
6818
6819 static rtx
6820 expand_val_compare_and_swap_1 (rtx mem, rtx old_val, rtx new_val,
6821 rtx target, enum insn_code icode)
6822 {
6823 enum machine_mode mode = GET_MODE (mem);
6824 rtx insn;
6825
6826 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
6827 target = gen_reg_rtx (mode);
6828
6829 if (GET_MODE (old_val) != VOIDmode && GET_MODE (old_val) != mode)
6830 old_val = convert_modes (mode, GET_MODE (old_val), old_val, 1);
6831 if (!insn_data[icode].operand[2].predicate (old_val, mode))
6832 old_val = force_reg (mode, old_val);
6833
6834 if (GET_MODE (new_val) != VOIDmode && GET_MODE (new_val) != mode)
6835 new_val = convert_modes (mode, GET_MODE (new_val), new_val, 1);
6836 if (!insn_data[icode].operand[3].predicate (new_val, mode))
6837 new_val = force_reg (mode, new_val);
6838
6839 insn = GEN_FCN (icode) (target, mem, old_val, new_val);
6840 if (insn == NULL_RTX)
6841 return NULL_RTX;
6842 emit_insn (insn);
6843
6844 return target;
6845 }
6846
6847 /* Expand a compare-and-swap operation and return its value. */
6848
6849 rtx
6850 expand_val_compare_and_swap (rtx mem, rtx old_val, rtx new_val, rtx target)
6851 {
6852 enum machine_mode mode = GET_MODE (mem);
6853 enum insn_code icode
6854 = direct_optab_handler (sync_compare_and_swap_optab, mode);
6855
6856 if (icode == CODE_FOR_nothing)
6857 return NULL_RTX;
6858
6859 return expand_val_compare_and_swap_1 (mem, old_val, new_val, target, icode);
6860 }
6861
6862 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
6863 pattern. */
6864
6865 static void
6866 find_cc_set (rtx x, const_rtx pat, void *data)
6867 {
6868 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
6869 && GET_CODE (pat) == SET)
6870 {
6871 rtx *p_cc_reg = (rtx *) data;
6872 gcc_assert (!*p_cc_reg);
6873 *p_cc_reg = x;
6874 }
6875 }
6876
6877 /* Expand a compare-and-swap operation and store true into the result if
6878 the operation was successful and false otherwise. Return the result.
6879 Unlike other routines, TARGET is not optional. */
6880
6881 rtx
6882 expand_bool_compare_and_swap (rtx mem, rtx old_val, rtx new_val, rtx target)
6883 {
6884 enum machine_mode mode = GET_MODE (mem);
6885 enum insn_code icode;
6886 rtx subtarget, seq, cc_reg;
6887
6888 /* If the target supports a compare-and-swap pattern that simultaneously
6889 sets some flag for success, then use it. Otherwise use the regular
6890 compare-and-swap and follow that immediately with a compare insn. */
6891 icode = direct_optab_handler (sync_compare_and_swap_optab, mode);
6892 if (icode == CODE_FOR_nothing)
6893 return NULL_RTX;
6894
6895 do_pending_stack_adjust ();
6896 do
6897 {
6898 start_sequence ();
6899 subtarget = expand_val_compare_and_swap_1 (mem, old_val, new_val,
6900 NULL_RTX, icode);
6901 cc_reg = NULL_RTX;
6902 if (subtarget == NULL_RTX)
6903 {
6904 end_sequence ();
6905 return NULL_RTX;
6906 }
6907
6908 if (have_insn_for (COMPARE, CCmode))
6909 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6910 seq = get_insns ();
6911 end_sequence ();
6912
6913 /* We might be comparing against an old value. Try again. :-( */
6914 if (!cc_reg && MEM_P (old_val))
6915 {
6916 seq = NULL_RTX;
6917 old_val = force_reg (mode, old_val);
6918 }
6919 }
6920 while (!seq);
6921
6922 emit_insn (seq);
6923 if (cc_reg)
6924 return emit_store_flag_force (target, EQ, cc_reg, const0_rtx, VOIDmode, 0, 1);
6925 else
6926 return emit_store_flag_force (target, EQ, subtarget, old_val, VOIDmode, 1, 1);
6927 }
6928
6929 /* This is a helper function for the other atomic operations. This function
6930 emits a loop that contains SEQ that iterates until a compare-and-swap
6931 operation at the end succeeds. MEM is the memory to be modified. SEQ is
6932 a set of instructions that takes a value from OLD_REG as an input and
6933 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
6934 set to the current contents of MEM. After SEQ, a compare-and-swap will
6935 attempt to update MEM with NEW_REG. The function returns true when the
6936 loop was generated successfully. */
6937
6938 static bool
6939 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
6940 {
6941 enum machine_mode mode = GET_MODE (mem);
6942 enum insn_code icode;
6943 rtx label, cmp_reg, subtarget, cc_reg;
6944
6945 /* The loop we want to generate looks like
6946
6947 cmp_reg = mem;
6948 label:
6949 old_reg = cmp_reg;
6950 seq;
6951 cmp_reg = compare-and-swap(mem, old_reg, new_reg)
6952 if (cmp_reg != old_reg)
6953 goto label;
6954
6955 Note that we only do the plain load from memory once. Subsequent
6956 iterations use the value loaded by the compare-and-swap pattern. */
6957
6958 label = gen_label_rtx ();
6959 cmp_reg = gen_reg_rtx (mode);
6960
6961 emit_move_insn (cmp_reg, mem);
6962 emit_label (label);
6963 emit_move_insn (old_reg, cmp_reg);
6964 if (seq)
6965 emit_insn (seq);
6966
6967 /* If the target supports a compare-and-swap pattern that simultaneously
6968 sets some flag for success, then use it. Otherwise use the regular
6969 compare-and-swap and follow that immediately with a compare insn. */
6970 icode = direct_optab_handler (sync_compare_and_swap_optab, mode);
6971 if (icode == CODE_FOR_nothing)
6972 return false;
6973
6974 subtarget = expand_val_compare_and_swap_1 (mem, old_reg, new_reg,
6975 cmp_reg, icode);
6976 if (subtarget == NULL_RTX)
6977 return false;
6978
6979 cc_reg = NULL_RTX;
6980 if (have_insn_for (COMPARE, CCmode))
6981 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6982 if (cc_reg)
6983 {
6984 cmp_reg = cc_reg;
6985 old_reg = const0_rtx;
6986 }
6987 else
6988 {
6989 if (subtarget != cmp_reg)
6990 emit_move_insn (cmp_reg, subtarget);
6991 }
6992
6993 /* ??? Mark this jump predicted not taken? */
6994 emit_cmp_and_jump_insns (cmp_reg, old_reg, NE, const0_rtx, GET_MODE (cmp_reg), 1,
6995 label);
6996 return true;
6997 }
6998
6999 /* This function generates the atomic operation MEM CODE= VAL. In this
7000 case, we do not care about any resulting value. Returns NULL if we
7001 cannot generate the operation. */
7002
7003 rtx
7004 expand_sync_operation (rtx mem, rtx val, enum rtx_code code)
7005 {
7006 enum machine_mode mode = GET_MODE (mem);
7007 enum insn_code icode;
7008 rtx insn;
7009
7010 /* Look to see if the target supports the operation directly. */
7011 switch (code)
7012 {
7013 case PLUS:
7014 icode = direct_optab_handler (sync_add_optab, mode);
7015 break;
7016 case IOR:
7017 icode = direct_optab_handler (sync_ior_optab, mode);
7018 break;
7019 case XOR:
7020 icode = direct_optab_handler (sync_xor_optab, mode);
7021 break;
7022 case AND:
7023 icode = direct_optab_handler (sync_and_optab, mode);
7024 break;
7025 case NOT:
7026 icode = direct_optab_handler (sync_nand_optab, mode);
7027 break;
7028
7029 case MINUS:
7030 icode = direct_optab_handler (sync_sub_optab, mode);
7031 if (icode == CODE_FOR_nothing || CONST_INT_P (val))
7032 {
7033 icode = direct_optab_handler (sync_add_optab, mode);
7034 if (icode != CODE_FOR_nothing)
7035 {
7036 val = expand_simple_unop (mode, NEG, val, NULL_RTX, 1);
7037 code = PLUS;
7038 }
7039 }
7040 break;
7041
7042 default:
7043 gcc_unreachable ();
7044 }
7045
7046 /* Generate the direct operation, if present. */
7047 if (icode != CODE_FOR_nothing)
7048 {
7049 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7050 val = convert_modes (mode, GET_MODE (val), val, 1);
7051 if (!insn_data[icode].operand[1].predicate (val, mode))
7052 val = force_reg (mode, val);
7053
7054 insn = GEN_FCN (icode) (mem, val);
7055 if (insn)
7056 {
7057 emit_insn (insn);
7058 return const0_rtx;
7059 }
7060 }
7061
7062 /* Failing that, generate a compare-and-swap loop in which we perform the
7063 operation with normal arithmetic instructions. */
7064 if (direct_optab_handler (sync_compare_and_swap_optab, mode)
7065 != CODE_FOR_nothing)
7066 {
7067 rtx t0 = gen_reg_rtx (mode), t1;
7068
7069 start_sequence ();
7070
7071 t1 = t0;
7072 if (code == NOT)
7073 {
7074 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
7075 true, OPTAB_LIB_WIDEN);
7076 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
7077 }
7078 else
7079 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX,
7080 true, OPTAB_LIB_WIDEN);
7081 insn = get_insns ();
7082 end_sequence ();
7083
7084 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7085 return const0_rtx;
7086 }
7087
7088 return NULL_RTX;
7089 }
7090
7091 /* This function generates the atomic operation MEM CODE= VAL. In this
7092 case, we do care about the resulting value: if AFTER is true then
7093 return the value MEM holds after the operation, if AFTER is false
7094 then return the value MEM holds before the operation. TARGET is an
7095 optional place for the result value to be stored. */
7096
7097 rtx
7098 expand_sync_fetch_operation (rtx mem, rtx val, enum rtx_code code,
7099 bool after, rtx target)
7100 {
7101 enum machine_mode mode = GET_MODE (mem);
7102 enum insn_code old_code, new_code, icode;
7103 bool compensate;
7104 rtx insn;
7105
7106 /* Look to see if the target supports the operation directly. */
7107 switch (code)
7108 {
7109 case PLUS:
7110 old_code = direct_optab_handler (sync_old_add_optab, mode);
7111 new_code = direct_optab_handler (sync_new_add_optab, mode);
7112 break;
7113 case IOR:
7114 old_code = direct_optab_handler (sync_old_ior_optab, mode);
7115 new_code = direct_optab_handler (sync_new_ior_optab, mode);
7116 break;
7117 case XOR:
7118 old_code = direct_optab_handler (sync_old_xor_optab, mode);
7119 new_code = direct_optab_handler (sync_new_xor_optab, mode);
7120 break;
7121 case AND:
7122 old_code = direct_optab_handler (sync_old_and_optab, mode);
7123 new_code = direct_optab_handler (sync_new_and_optab, mode);
7124 break;
7125 case NOT:
7126 old_code = direct_optab_handler (sync_old_nand_optab, mode);
7127 new_code = direct_optab_handler (sync_new_nand_optab, mode);
7128 break;
7129
7130 case MINUS:
7131 old_code = direct_optab_handler (sync_old_sub_optab, mode);
7132 new_code = direct_optab_handler (sync_new_sub_optab, mode);
7133 if ((old_code == CODE_FOR_nothing && new_code == CODE_FOR_nothing)
7134 || CONST_INT_P (val))
7135 {
7136 old_code = direct_optab_handler (sync_old_add_optab, mode);
7137 new_code = direct_optab_handler (sync_new_add_optab, mode);
7138 if (old_code != CODE_FOR_nothing || new_code != CODE_FOR_nothing)
7139 {
7140 val = expand_simple_unop (mode, NEG, val, NULL_RTX, 1);
7141 code = PLUS;
7142 }
7143 }
7144 break;
7145
7146 default:
7147 gcc_unreachable ();
7148 }
7149
7150 /* If the target does supports the proper new/old operation, great. But
7151 if we only support the opposite old/new operation, check to see if we
7152 can compensate. In the case in which the old value is supported, then
7153 we can always perform the operation again with normal arithmetic. In
7154 the case in which the new value is supported, then we can only handle
7155 this in the case the operation is reversible. */
7156 compensate = false;
7157 if (after)
7158 {
7159 icode = new_code;
7160 if (icode == CODE_FOR_nothing)
7161 {
7162 icode = old_code;
7163 if (icode != CODE_FOR_nothing)
7164 compensate = true;
7165 }
7166 }
7167 else
7168 {
7169 icode = old_code;
7170 if (icode == CODE_FOR_nothing
7171 && (code == PLUS || code == MINUS || code == XOR))
7172 {
7173 icode = new_code;
7174 if (icode != CODE_FOR_nothing)
7175 compensate = true;
7176 }
7177 }
7178
7179 /* If we found something supported, great. */
7180 if (icode != CODE_FOR_nothing)
7181 {
7182 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
7183 target = gen_reg_rtx (mode);
7184
7185 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7186 val = convert_modes (mode, GET_MODE (val), val, 1);
7187 if (!insn_data[icode].operand[2].predicate (val, mode))
7188 val = force_reg (mode, val);
7189
7190 insn = GEN_FCN (icode) (target, mem, val);
7191 if (insn)
7192 {
7193 emit_insn (insn);
7194
7195 /* If we need to compensate for using an operation with the
7196 wrong return value, do so now. */
7197 if (compensate)
7198 {
7199 if (!after)
7200 {
7201 if (code == PLUS)
7202 code = MINUS;
7203 else if (code == MINUS)
7204 code = PLUS;
7205 }
7206
7207 if (code == NOT)
7208 {
7209 target = expand_simple_binop (mode, AND, target, val,
7210 NULL_RTX, true,
7211 OPTAB_LIB_WIDEN);
7212 target = expand_simple_unop (mode, code, target,
7213 NULL_RTX, true);
7214 }
7215 else
7216 target = expand_simple_binop (mode, code, target, val,
7217 NULL_RTX, true,
7218 OPTAB_LIB_WIDEN);
7219 }
7220
7221 return target;
7222 }
7223 }
7224
7225 /* Failing that, generate a compare-and-swap loop in which we perform the
7226 operation with normal arithmetic instructions. */
7227 if (direct_optab_handler (sync_compare_and_swap_optab, mode)
7228 != CODE_FOR_nothing)
7229 {
7230 rtx t0 = gen_reg_rtx (mode), t1;
7231
7232 if (!target || !register_operand (target, mode))
7233 target = gen_reg_rtx (mode);
7234
7235 start_sequence ();
7236
7237 if (!after)
7238 emit_move_insn (target, t0);
7239 t1 = t0;
7240 if (code == NOT)
7241 {
7242 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
7243 true, OPTAB_LIB_WIDEN);
7244 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
7245 }
7246 else
7247 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX,
7248 true, OPTAB_LIB_WIDEN);
7249 if (after)
7250 emit_move_insn (target, t1);
7251
7252 insn = get_insns ();
7253 end_sequence ();
7254
7255 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7256 return target;
7257 }
7258
7259 return NULL_RTX;
7260 }
7261
7262 /* This function expands a test-and-set operation. Ideally we atomically
7263 store VAL in MEM and return the previous value in MEM. Some targets
7264 may not support this operation and only support VAL with the constant 1;
7265 in this case while the return value will be 0/1, but the exact value
7266 stored in MEM is target defined. TARGET is an option place to stick
7267 the return value. */
7268
7269 rtx
7270 expand_sync_lock_test_and_set (rtx mem, rtx val, rtx target)
7271 {
7272 enum machine_mode mode = GET_MODE (mem);
7273 enum insn_code icode;
7274 rtx insn;
7275
7276 /* If the target supports the test-and-set directly, great. */
7277 icode = direct_optab_handler (sync_lock_test_and_set_optab, mode);
7278 if (icode != CODE_FOR_nothing)
7279 {
7280 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
7281 target = gen_reg_rtx (mode);
7282
7283 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7284 val = convert_modes (mode, GET_MODE (val), val, 1);
7285 if (!insn_data[icode].operand[2].predicate (val, mode))
7286 val = force_reg (mode, val);
7287
7288 insn = GEN_FCN (icode) (target, mem, val);
7289 if (insn)
7290 {
7291 emit_insn (insn);
7292 return target;
7293 }
7294 }
7295
7296 /* Otherwise, use a compare-and-swap loop for the exchange. */
7297 if (direct_optab_handler (sync_compare_and_swap_optab, mode)
7298 != CODE_FOR_nothing)
7299 {
7300 if (!target || !register_operand (target, mode))
7301 target = gen_reg_rtx (mode);
7302 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7303 val = convert_modes (mode, GET_MODE (val), val, 1);
7304 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7305 return target;
7306 }
7307
7308 return NULL_RTX;
7309 }
7310 \f
7311 /* Return true if OPERAND is suitable for operand number OPNO of
7312 instruction ICODE. */
7313
7314 bool
7315 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
7316 {
7317 return (!insn_data[(int) icode].operand[opno].predicate
7318 || (insn_data[(int) icode].operand[opno].predicate
7319 (operand, insn_data[(int) icode].operand[opno].mode)));
7320 }
7321
7322 #include "gt-optabs.h"