Use std::swap instead of explicit swaps
[gcc.git] / gcc / optabs.c
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "diagnostic-core.h"
26
27 /* Include insn-config.h before expr.h so that HAVE_conditional_move
28 is properly defined. */
29 #include "insn-config.h"
30 #include "rtl.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "vec.h"
34 #include "double-int.h"
35 #include "input.h"
36 #include "alias.h"
37 #include "symtab.h"
38 #include "wide-int.h"
39 #include "inchash.h"
40 #include "tree.h"
41 #include "tree-hasher.h"
42 #include "stor-layout.h"
43 #include "stringpool.h"
44 #include "varasm.h"
45 #include "tm_p.h"
46 #include "flags.h"
47 #include "hard-reg-set.h"
48 #include "function.h"
49 #include "except.h"
50 #include "hashtab.h"
51 #include "statistics.h"
52 #include "real.h"
53 #include "fixed-value.h"
54 #include "expmed.h"
55 #include "dojump.h"
56 #include "explow.h"
57 #include "calls.h"
58 #include "emit-rtl.h"
59 #include "stmt.h"
60 #include "expr.h"
61 #include "insn-codes.h"
62 #include "optabs.h"
63 #include "libfuncs.h"
64 #include "recog.h"
65 #include "reload.h"
66 #include "ggc.h"
67 #include "predict.h"
68 #include "dominance.h"
69 #include "cfg.h"
70 #include "basic-block.h"
71 #include "target.h"
72
73 struct target_optabs default_target_optabs;
74 struct target_libfuncs default_target_libfuncs;
75 struct target_optabs *this_fn_optabs = &default_target_optabs;
76 #if SWITCHABLE_TARGET
77 struct target_optabs *this_target_optabs = &default_target_optabs;
78 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
79 #endif
80
81 #define libfunc_hash \
82 (this_target_libfuncs->x_libfunc_hash)
83
84 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
85 machine_mode *);
86 static rtx expand_unop_direct (machine_mode, optab, rtx, rtx, int);
87 static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
88
89 /* Debug facility for use in GDB. */
90 void debug_optab_libfuncs (void);
91
92 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
93 #if ENABLE_DECIMAL_BID_FORMAT
94 #define DECIMAL_PREFIX "bid_"
95 #else
96 #define DECIMAL_PREFIX "dpd_"
97 #endif
98 \f
99 /* Used for libfunc_hash. */
100
101 hashval_t
102 libfunc_hasher::hash (libfunc_entry *e)
103 {
104 return ((e->mode1 + e->mode2 * NUM_MACHINE_MODES) ^ e->op);
105 }
106
107 /* Used for libfunc_hash. */
108
109 bool
110 libfunc_hasher::equal (libfunc_entry *e1, libfunc_entry *e2)
111 {
112 return e1->op == e2->op && e1->mode1 == e2->mode1 && e1->mode2 == e2->mode2;
113 }
114
115 /* Return libfunc corresponding operation defined by OPTAB converting
116 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
117 if no libfunc is available. */
118 rtx
119 convert_optab_libfunc (convert_optab optab, machine_mode mode1,
120 machine_mode mode2)
121 {
122 struct libfunc_entry e;
123 struct libfunc_entry **slot;
124
125 /* ??? This ought to be an assert, but not all of the places
126 that we expand optabs know about the optabs that got moved
127 to being direct. */
128 if (!(optab >= FIRST_CONV_OPTAB && optab <= LAST_CONVLIB_OPTAB))
129 return NULL_RTX;
130
131 e.op = optab;
132 e.mode1 = mode1;
133 e.mode2 = mode2;
134 slot = libfunc_hash->find_slot (&e, NO_INSERT);
135 if (!slot)
136 {
137 const struct convert_optab_libcall_d *d
138 = &convlib_def[optab - FIRST_CONV_OPTAB];
139
140 if (d->libcall_gen == NULL)
141 return NULL;
142
143 d->libcall_gen (optab, d->libcall_basename, mode1, mode2);
144 slot = libfunc_hash->find_slot (&e, NO_INSERT);
145 if (!slot)
146 return NULL;
147 }
148 return (*slot)->libfunc;
149 }
150
151 /* Return libfunc corresponding operation defined by OPTAB in MODE.
152 Trigger lazy initialization if needed, return NULL if no libfunc is
153 available. */
154 rtx
155 optab_libfunc (optab optab, machine_mode mode)
156 {
157 struct libfunc_entry e;
158 struct libfunc_entry **slot;
159
160 /* ??? This ought to be an assert, but not all of the places
161 that we expand optabs know about the optabs that got moved
162 to being direct. */
163 if (!(optab >= FIRST_NORM_OPTAB && optab <= LAST_NORMLIB_OPTAB))
164 return NULL_RTX;
165
166 e.op = optab;
167 e.mode1 = mode;
168 e.mode2 = VOIDmode;
169 slot = libfunc_hash->find_slot (&e, NO_INSERT);
170 if (!slot)
171 {
172 const struct optab_libcall_d *d
173 = &normlib_def[optab - FIRST_NORM_OPTAB];
174
175 if (d->libcall_gen == NULL)
176 return NULL;
177
178 d->libcall_gen (optab, d->libcall_basename, d->libcall_suffix, mode);
179 slot = libfunc_hash->find_slot (&e, NO_INSERT);
180 if (!slot)
181 return NULL;
182 }
183 return (*slot)->libfunc;
184 }
185
186 \f
187 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
188 the result of operation CODE applied to OP0 (and OP1 if it is a binary
189 operation).
190
191 If the last insn does not set TARGET, don't do anything, but return 1.
192
193 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
194 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
195 try again, ensuring that TARGET is not one of the operands. */
196
197 static int
198 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
199 {
200 rtx_insn *last_insn;
201 rtx set;
202 rtx note;
203
204 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
205
206 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
207 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
208 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
209 && GET_RTX_CLASS (code) != RTX_COMPARE
210 && GET_RTX_CLASS (code) != RTX_UNARY)
211 return 1;
212
213 if (GET_CODE (target) == ZERO_EXTRACT)
214 return 1;
215
216 for (last_insn = insns;
217 NEXT_INSN (last_insn) != NULL_RTX;
218 last_insn = NEXT_INSN (last_insn))
219 ;
220
221 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
222 a value changing in the insn, so the note would be invalid for CSE. */
223 if (reg_overlap_mentioned_p (target, op0)
224 || (op1 && reg_overlap_mentioned_p (target, op1)))
225 {
226 if (MEM_P (target)
227 && (rtx_equal_p (target, op0)
228 || (op1 && rtx_equal_p (target, op1))))
229 {
230 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
231 over expanding it as temp = MEM op X, MEM = temp. If the target
232 supports MEM = MEM op X instructions, it is sometimes too hard
233 to reconstruct that form later, especially if X is also a memory,
234 and due to multiple occurrences of addresses the address might
235 be forced into register unnecessarily.
236 Note that not emitting the REG_EQUIV note might inhibit
237 CSE in some cases. */
238 set = single_set (last_insn);
239 if (set
240 && GET_CODE (SET_SRC (set)) == code
241 && MEM_P (SET_DEST (set))
242 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
243 || (op1 && rtx_equal_p (SET_DEST (set),
244 XEXP (SET_SRC (set), 1)))))
245 return 1;
246 }
247 return 0;
248 }
249
250 set = set_for_reg_notes (last_insn);
251 if (set == NULL_RTX)
252 return 1;
253
254 if (! rtx_equal_p (SET_DEST (set), target)
255 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
256 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
257 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
258 return 1;
259
260 if (GET_RTX_CLASS (code) == RTX_UNARY)
261 switch (code)
262 {
263 case FFS:
264 case CLZ:
265 case CTZ:
266 case CLRSB:
267 case POPCOUNT:
268 case PARITY:
269 case BSWAP:
270 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
271 {
272 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
273 if (GET_MODE_SIZE (GET_MODE (op0))
274 > GET_MODE_SIZE (GET_MODE (target)))
275 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
276 note, GET_MODE (op0));
277 else
278 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
279 note, GET_MODE (op0));
280 break;
281 }
282 /* FALLTHRU */
283 default:
284 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
285 break;
286 }
287 else
288 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
289
290 set_unique_reg_note (last_insn, REG_EQUAL, note);
291
292 return 1;
293 }
294 \f
295 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
296 for a widening operation would be. In most cases this would be OP0, but if
297 that's a constant it'll be VOIDmode, which isn't useful. */
298
299 static machine_mode
300 widened_mode (machine_mode to_mode, rtx op0, rtx op1)
301 {
302 machine_mode m0 = GET_MODE (op0);
303 machine_mode m1 = GET_MODE (op1);
304 machine_mode result;
305
306 if (m0 == VOIDmode && m1 == VOIDmode)
307 return to_mode;
308 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
309 result = m1;
310 else
311 result = m0;
312
313 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
314 return to_mode;
315
316 return result;
317 }
318 \f
319 /* Like optab_handler, but for widening_operations that have a
320 TO_MODE and a FROM_MODE. */
321
322 enum insn_code
323 widening_optab_handler (optab op, machine_mode to_mode,
324 machine_mode from_mode)
325 {
326 unsigned scode = (op << 16) | to_mode;
327 if (to_mode != from_mode && from_mode != VOIDmode)
328 {
329 /* ??? Why does find_widening_optab_handler_and_mode attempt to
330 widen things that can't be widened? E.g. add_optab... */
331 if (op > LAST_CONV_OPTAB)
332 return CODE_FOR_nothing;
333 scode |= from_mode << 8;
334 }
335 return raw_optab_handler (scode);
336 }
337
338 /* Find a widening optab even if it doesn't widen as much as we want.
339 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
340 direct HI->SI insn, then return SI->DI, if that exists.
341 If PERMIT_NON_WIDENING is non-zero then this can be used with
342 non-widening optabs also. */
343
344 enum insn_code
345 find_widening_optab_handler_and_mode (optab op, machine_mode to_mode,
346 machine_mode from_mode,
347 int permit_non_widening,
348 machine_mode *found_mode)
349 {
350 for (; (permit_non_widening || from_mode != to_mode)
351 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
352 && from_mode != VOIDmode;
353 from_mode = GET_MODE_WIDER_MODE (from_mode))
354 {
355 enum insn_code handler = widening_optab_handler (op, to_mode,
356 from_mode);
357
358 if (handler != CODE_FOR_nothing)
359 {
360 if (found_mode)
361 *found_mode = from_mode;
362 return handler;
363 }
364 }
365
366 return CODE_FOR_nothing;
367 }
368 \f
369 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
370 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
371 not actually do a sign-extend or zero-extend, but can leave the
372 higher-order bits of the result rtx undefined, for example, in the case
373 of logical operations, but not right shifts. */
374
375 static rtx
376 widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
377 int unsignedp, int no_extend)
378 {
379 rtx result;
380
381 /* If we don't have to extend and this is a constant, return it. */
382 if (no_extend && GET_MODE (op) == VOIDmode)
383 return op;
384
385 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
386 extend since it will be more efficient to do so unless the signedness of
387 a promoted object differs from our extension. */
388 if (! no_extend
389 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
390 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
391 return convert_modes (mode, oldmode, op, unsignedp);
392
393 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
394 SUBREG. */
395 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
396 return gen_lowpart (mode, force_reg (GET_MODE (op), op));
397
398 /* Otherwise, get an object of MODE, clobber it, and set the low-order
399 part to OP. */
400
401 result = gen_reg_rtx (mode);
402 emit_clobber (result);
403 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
404 return result;
405 }
406 \f
407 /* Return the optab used for computing the operation given by the tree code,
408 CODE and the tree EXP. This function is not always usable (for example, it
409 cannot give complete results for multiplication or division) but probably
410 ought to be relied on more widely throughout the expander. */
411 optab
412 optab_for_tree_code (enum tree_code code, const_tree type,
413 enum optab_subtype subtype)
414 {
415 bool trapv;
416 switch (code)
417 {
418 case BIT_AND_EXPR:
419 return and_optab;
420
421 case BIT_IOR_EXPR:
422 return ior_optab;
423
424 case BIT_NOT_EXPR:
425 return one_cmpl_optab;
426
427 case BIT_XOR_EXPR:
428 return xor_optab;
429
430 case MULT_HIGHPART_EXPR:
431 return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
432
433 case TRUNC_MOD_EXPR:
434 case CEIL_MOD_EXPR:
435 case FLOOR_MOD_EXPR:
436 case ROUND_MOD_EXPR:
437 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
438
439 case RDIV_EXPR:
440 case TRUNC_DIV_EXPR:
441 case CEIL_DIV_EXPR:
442 case FLOOR_DIV_EXPR:
443 case ROUND_DIV_EXPR:
444 case EXACT_DIV_EXPR:
445 if (TYPE_SATURATING (type))
446 return TYPE_UNSIGNED (type) ? usdiv_optab : ssdiv_optab;
447 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
448
449 case LSHIFT_EXPR:
450 if (TREE_CODE (type) == VECTOR_TYPE)
451 {
452 if (subtype == optab_vector)
453 return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
454
455 gcc_assert (subtype == optab_scalar);
456 }
457 if (TYPE_SATURATING (type))
458 return TYPE_UNSIGNED (type) ? usashl_optab : ssashl_optab;
459 return ashl_optab;
460
461 case RSHIFT_EXPR:
462 if (TREE_CODE (type) == VECTOR_TYPE)
463 {
464 if (subtype == optab_vector)
465 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
466
467 gcc_assert (subtype == optab_scalar);
468 }
469 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
470
471 case LROTATE_EXPR:
472 if (TREE_CODE (type) == VECTOR_TYPE)
473 {
474 if (subtype == optab_vector)
475 return vrotl_optab;
476
477 gcc_assert (subtype == optab_scalar);
478 }
479 return rotl_optab;
480
481 case RROTATE_EXPR:
482 if (TREE_CODE (type) == VECTOR_TYPE)
483 {
484 if (subtype == optab_vector)
485 return vrotr_optab;
486
487 gcc_assert (subtype == optab_scalar);
488 }
489 return rotr_optab;
490
491 case MAX_EXPR:
492 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
493
494 case MIN_EXPR:
495 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
496
497 case REALIGN_LOAD_EXPR:
498 return vec_realign_load_optab;
499
500 case WIDEN_SUM_EXPR:
501 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
502
503 case DOT_PROD_EXPR:
504 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
505
506 case SAD_EXPR:
507 return TYPE_UNSIGNED (type) ? usad_optab : ssad_optab;
508
509 case WIDEN_MULT_PLUS_EXPR:
510 return (TYPE_UNSIGNED (type)
511 ? (TYPE_SATURATING (type)
512 ? usmadd_widen_optab : umadd_widen_optab)
513 : (TYPE_SATURATING (type)
514 ? ssmadd_widen_optab : smadd_widen_optab));
515
516 case WIDEN_MULT_MINUS_EXPR:
517 return (TYPE_UNSIGNED (type)
518 ? (TYPE_SATURATING (type)
519 ? usmsub_widen_optab : umsub_widen_optab)
520 : (TYPE_SATURATING (type)
521 ? ssmsub_widen_optab : smsub_widen_optab));
522
523 case FMA_EXPR:
524 return fma_optab;
525
526 case REDUC_MAX_EXPR:
527 return TYPE_UNSIGNED (type)
528 ? reduc_umax_scal_optab : reduc_smax_scal_optab;
529
530 case REDUC_MIN_EXPR:
531 return TYPE_UNSIGNED (type)
532 ? reduc_umin_scal_optab : reduc_smin_scal_optab;
533
534 case REDUC_PLUS_EXPR:
535 return reduc_plus_scal_optab;
536
537 case VEC_WIDEN_MULT_HI_EXPR:
538 return TYPE_UNSIGNED (type) ?
539 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
540
541 case VEC_WIDEN_MULT_LO_EXPR:
542 return TYPE_UNSIGNED (type) ?
543 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
544
545 case VEC_WIDEN_MULT_EVEN_EXPR:
546 return TYPE_UNSIGNED (type) ?
547 vec_widen_umult_even_optab : vec_widen_smult_even_optab;
548
549 case VEC_WIDEN_MULT_ODD_EXPR:
550 return TYPE_UNSIGNED (type) ?
551 vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
552
553 case VEC_WIDEN_LSHIFT_HI_EXPR:
554 return TYPE_UNSIGNED (type) ?
555 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
556
557 case VEC_WIDEN_LSHIFT_LO_EXPR:
558 return TYPE_UNSIGNED (type) ?
559 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
560
561 case VEC_UNPACK_HI_EXPR:
562 return TYPE_UNSIGNED (type) ?
563 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
564
565 case VEC_UNPACK_LO_EXPR:
566 return TYPE_UNSIGNED (type) ?
567 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
568
569 case VEC_UNPACK_FLOAT_HI_EXPR:
570 /* The signedness is determined from input operand. */
571 return TYPE_UNSIGNED (type) ?
572 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
573
574 case VEC_UNPACK_FLOAT_LO_EXPR:
575 /* The signedness is determined from input operand. */
576 return TYPE_UNSIGNED (type) ?
577 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
578
579 case VEC_PACK_TRUNC_EXPR:
580 return vec_pack_trunc_optab;
581
582 case VEC_PACK_SAT_EXPR:
583 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
584
585 case VEC_PACK_FIX_TRUNC_EXPR:
586 /* The signedness is determined from output operand. */
587 return TYPE_UNSIGNED (type) ?
588 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
589
590 default:
591 break;
592 }
593
594 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
595 switch (code)
596 {
597 case POINTER_PLUS_EXPR:
598 case PLUS_EXPR:
599 if (TYPE_SATURATING (type))
600 return TYPE_UNSIGNED (type) ? usadd_optab : ssadd_optab;
601 return trapv ? addv_optab : add_optab;
602
603 case MINUS_EXPR:
604 if (TYPE_SATURATING (type))
605 return TYPE_UNSIGNED (type) ? ussub_optab : sssub_optab;
606 return trapv ? subv_optab : sub_optab;
607
608 case MULT_EXPR:
609 if (TYPE_SATURATING (type))
610 return TYPE_UNSIGNED (type) ? usmul_optab : ssmul_optab;
611 return trapv ? smulv_optab : smul_optab;
612
613 case NEGATE_EXPR:
614 if (TYPE_SATURATING (type))
615 return TYPE_UNSIGNED (type) ? usneg_optab : ssneg_optab;
616 return trapv ? negv_optab : neg_optab;
617
618 case ABS_EXPR:
619 return trapv ? absv_optab : abs_optab;
620
621 default:
622 return unknown_optab;
623 }
624 }
625
626 /* Given optab UNOPTAB that reduces a vector to a scalar, find instead the old
627 optab that produces a vector with the reduction result in one element,
628 for a tree with type TYPE. */
629
630 optab
631 scalar_reduc_to_vector (optab unoptab, const_tree type)
632 {
633 switch (unoptab)
634 {
635 case reduc_plus_scal_optab:
636 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
637
638 case reduc_smin_scal_optab: return reduc_smin_optab;
639 case reduc_umin_scal_optab: return reduc_umin_optab;
640 case reduc_smax_scal_optab: return reduc_smax_optab;
641 case reduc_umax_scal_optab: return reduc_umax_optab;
642 default: return unknown_optab;
643 }
644 }
645
646 /* Expand vector widening operations.
647
648 There are two different classes of operations handled here:
649 1) Operations whose result is wider than all the arguments to the operation.
650 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
651 In this case OP0 and optionally OP1 would be initialized,
652 but WIDE_OP wouldn't (not relevant for this case).
653 2) Operations whose result is of the same size as the last argument to the
654 operation, but wider than all the other arguments to the operation.
655 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
656 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
657
658 E.g, when called to expand the following operations, this is how
659 the arguments will be initialized:
660 nops OP0 OP1 WIDE_OP
661 widening-sum 2 oprnd0 - oprnd1
662 widening-dot-product 3 oprnd0 oprnd1 oprnd2
663 widening-mult 2 oprnd0 oprnd1 -
664 type-promotion (vec-unpack) 1 oprnd0 - - */
665
666 rtx
667 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
668 rtx target, int unsignedp)
669 {
670 struct expand_operand eops[4];
671 tree oprnd0, oprnd1, oprnd2;
672 machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
673 optab widen_pattern_optab;
674 enum insn_code icode;
675 int nops = TREE_CODE_LENGTH (ops->code);
676 int op;
677
678 oprnd0 = ops->op0;
679 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
680 widen_pattern_optab =
681 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
682 if (ops->code == WIDEN_MULT_PLUS_EXPR
683 || ops->code == WIDEN_MULT_MINUS_EXPR)
684 icode = find_widening_optab_handler (widen_pattern_optab,
685 TYPE_MODE (TREE_TYPE (ops->op2)),
686 tmode0, 0);
687 else
688 icode = optab_handler (widen_pattern_optab, tmode0);
689 gcc_assert (icode != CODE_FOR_nothing);
690
691 if (nops >= 2)
692 {
693 oprnd1 = ops->op1;
694 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
695 }
696
697 /* The last operand is of a wider mode than the rest of the operands. */
698 if (nops == 2)
699 wmode = tmode1;
700 else if (nops == 3)
701 {
702 gcc_assert (tmode1 == tmode0);
703 gcc_assert (op1);
704 oprnd2 = ops->op2;
705 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
706 }
707
708 op = 0;
709 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
710 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
711 if (op1)
712 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
713 if (wide_op)
714 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
715 expand_insn (icode, op, eops);
716 return eops[0].value;
717 }
718
719 /* Generate code to perform an operation specified by TERNARY_OPTAB
720 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
721
722 UNSIGNEDP is for the case where we have to widen the operands
723 to perform the operation. It says to use zero-extension.
724
725 If TARGET is nonzero, the value
726 is generated there, if it is convenient to do so.
727 In all cases an rtx is returned for the locus of the value;
728 this may or may not be TARGET. */
729
730 rtx
731 expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
732 rtx op1, rtx op2, rtx target, int unsignedp)
733 {
734 struct expand_operand ops[4];
735 enum insn_code icode = optab_handler (ternary_optab, mode);
736
737 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
738
739 create_output_operand (&ops[0], target, mode);
740 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
741 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
742 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
743 expand_insn (icode, 4, ops);
744 return ops[0].value;
745 }
746
747
748 /* Like expand_binop, but return a constant rtx if the result can be
749 calculated at compile time. The arguments and return value are
750 otherwise the same as for expand_binop. */
751
752 rtx
753 simplify_expand_binop (machine_mode mode, optab binoptab,
754 rtx op0, rtx op1, rtx target, int unsignedp,
755 enum optab_methods methods)
756 {
757 if (CONSTANT_P (op0) && CONSTANT_P (op1))
758 {
759 rtx x = simplify_binary_operation (optab_to_code (binoptab),
760 mode, op0, op1);
761 if (x)
762 return x;
763 }
764
765 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
766 }
767
768 /* Like simplify_expand_binop, but always put the result in TARGET.
769 Return true if the expansion succeeded. */
770
771 bool
772 force_expand_binop (machine_mode mode, optab binoptab,
773 rtx op0, rtx op1, rtx target, int unsignedp,
774 enum optab_methods methods)
775 {
776 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
777 target, unsignedp, methods);
778 if (x == 0)
779 return false;
780 if (x != target)
781 emit_move_insn (target, x);
782 return true;
783 }
784
785 /* Create a new vector value in VMODE with all elements set to OP. The
786 mode of OP must be the element mode of VMODE. If OP is a constant,
787 then the return value will be a constant. */
788
789 static rtx
790 expand_vector_broadcast (machine_mode vmode, rtx op)
791 {
792 enum insn_code icode;
793 rtvec vec;
794 rtx ret;
795 int i, n;
796
797 gcc_checking_assert (VECTOR_MODE_P (vmode));
798
799 n = GET_MODE_NUNITS (vmode);
800 vec = rtvec_alloc (n);
801 for (i = 0; i < n; ++i)
802 RTVEC_ELT (vec, i) = op;
803
804 if (CONSTANT_P (op))
805 return gen_rtx_CONST_VECTOR (vmode, vec);
806
807 /* ??? If the target doesn't have a vec_init, then we have no easy way
808 of performing this operation. Most of this sort of generic support
809 is hidden away in the vector lowering support in gimple. */
810 icode = optab_handler (vec_init_optab, vmode);
811 if (icode == CODE_FOR_nothing)
812 return NULL;
813
814 ret = gen_reg_rtx (vmode);
815 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
816
817 return ret;
818 }
819
820 /* This subroutine of expand_doubleword_shift handles the cases in which
821 the effective shift value is >= BITS_PER_WORD. The arguments and return
822 value are the same as for the parent routine, except that SUPERWORD_OP1
823 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
824 INTO_TARGET may be null if the caller has decided to calculate it. */
825
826 static bool
827 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
828 rtx outof_target, rtx into_target,
829 int unsignedp, enum optab_methods methods)
830 {
831 if (into_target != 0)
832 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
833 into_target, unsignedp, methods))
834 return false;
835
836 if (outof_target != 0)
837 {
838 /* For a signed right shift, we must fill OUTOF_TARGET with copies
839 of the sign bit, otherwise we must fill it with zeros. */
840 if (binoptab != ashr_optab)
841 emit_move_insn (outof_target, CONST0_RTX (word_mode));
842 else
843 if (!force_expand_binop (word_mode, binoptab,
844 outof_input, GEN_INT (BITS_PER_WORD - 1),
845 outof_target, unsignedp, methods))
846 return false;
847 }
848 return true;
849 }
850
851 /* This subroutine of expand_doubleword_shift handles the cases in which
852 the effective shift value is < BITS_PER_WORD. The arguments and return
853 value are the same as for the parent routine. */
854
855 static bool
856 expand_subword_shift (machine_mode op1_mode, optab binoptab,
857 rtx outof_input, rtx into_input, rtx op1,
858 rtx outof_target, rtx into_target,
859 int unsignedp, enum optab_methods methods,
860 unsigned HOST_WIDE_INT shift_mask)
861 {
862 optab reverse_unsigned_shift, unsigned_shift;
863 rtx tmp, carries;
864
865 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
866 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
867
868 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
869 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
870 the opposite direction to BINOPTAB. */
871 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
872 {
873 carries = outof_input;
874 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
875 op1_mode), op1_mode);
876 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
877 0, true, methods);
878 }
879 else
880 {
881 /* We must avoid shifting by BITS_PER_WORD bits since that is either
882 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
883 has unknown behavior. Do a single shift first, then shift by the
884 remainder. It's OK to use ~OP1 as the remainder if shift counts
885 are truncated to the mode size. */
886 carries = expand_binop (word_mode, reverse_unsigned_shift,
887 outof_input, const1_rtx, 0, unsignedp, methods);
888 if (shift_mask == BITS_PER_WORD - 1)
889 {
890 tmp = immed_wide_int_const
891 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
892 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
893 0, true, methods);
894 }
895 else
896 {
897 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
898 op1_mode), op1_mode);
899 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
900 0, true, methods);
901 }
902 }
903 if (tmp == 0 || carries == 0)
904 return false;
905 carries = expand_binop (word_mode, reverse_unsigned_shift,
906 carries, tmp, 0, unsignedp, methods);
907 if (carries == 0)
908 return false;
909
910 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
911 so the result can go directly into INTO_TARGET if convenient. */
912 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
913 into_target, unsignedp, methods);
914 if (tmp == 0)
915 return false;
916
917 /* Now OR in the bits carried over from OUTOF_INPUT. */
918 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
919 into_target, unsignedp, methods))
920 return false;
921
922 /* Use a standard word_mode shift for the out-of half. */
923 if (outof_target != 0)
924 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
925 outof_target, unsignedp, methods))
926 return false;
927
928 return true;
929 }
930
931
932 #ifdef HAVE_conditional_move
933 /* Try implementing expand_doubleword_shift using conditional moves.
934 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
935 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
936 are the shift counts to use in the former and latter case. All other
937 arguments are the same as the parent routine. */
938
939 static bool
940 expand_doubleword_shift_condmove (machine_mode op1_mode, optab binoptab,
941 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
942 rtx outof_input, rtx into_input,
943 rtx subword_op1, rtx superword_op1,
944 rtx outof_target, rtx into_target,
945 int unsignedp, enum optab_methods methods,
946 unsigned HOST_WIDE_INT shift_mask)
947 {
948 rtx outof_superword, into_superword;
949
950 /* Put the superword version of the output into OUTOF_SUPERWORD and
951 INTO_SUPERWORD. */
952 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
953 if (outof_target != 0 && subword_op1 == superword_op1)
954 {
955 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
956 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
957 into_superword = outof_target;
958 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
959 outof_superword, 0, unsignedp, methods))
960 return false;
961 }
962 else
963 {
964 into_superword = gen_reg_rtx (word_mode);
965 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
966 outof_superword, into_superword,
967 unsignedp, methods))
968 return false;
969 }
970
971 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
972 if (!expand_subword_shift (op1_mode, binoptab,
973 outof_input, into_input, subword_op1,
974 outof_target, into_target,
975 unsignedp, methods, shift_mask))
976 return false;
977
978 /* Select between them. Do the INTO half first because INTO_SUPERWORD
979 might be the current value of OUTOF_TARGET. */
980 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
981 into_target, into_superword, word_mode, false))
982 return false;
983
984 if (outof_target != 0)
985 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
986 outof_target, outof_superword,
987 word_mode, false))
988 return false;
989
990 return true;
991 }
992 #endif
993
994 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
995 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
996 input operand; the shift moves bits in the direction OUTOF_INPUT->
997 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
998 of the target. OP1 is the shift count and OP1_MODE is its mode.
999 If OP1 is constant, it will have been truncated as appropriate
1000 and is known to be nonzero.
1001
1002 If SHIFT_MASK is zero, the result of word shifts is undefined when the
1003 shift count is outside the range [0, BITS_PER_WORD). This routine must
1004 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
1005
1006 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
1007 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
1008 fill with zeros or sign bits as appropriate.
1009
1010 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
1011 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
1012 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
1013 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
1014 are undefined.
1015
1016 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
1017 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
1018 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
1019 function wants to calculate it itself.
1020
1021 Return true if the shift could be successfully synthesized. */
1022
1023 static bool
1024 expand_doubleword_shift (machine_mode op1_mode, optab binoptab,
1025 rtx outof_input, rtx into_input, rtx op1,
1026 rtx outof_target, rtx into_target,
1027 int unsignedp, enum optab_methods methods,
1028 unsigned HOST_WIDE_INT shift_mask)
1029 {
1030 rtx superword_op1, tmp, cmp1, cmp2;
1031 enum rtx_code cmp_code;
1032
1033 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1034 fill the result with sign or zero bits as appropriate. If so, the value
1035 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1036 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1037 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1038
1039 This isn't worthwhile for constant shifts since the optimizers will
1040 cope better with in-range shift counts. */
1041 if (shift_mask >= BITS_PER_WORD
1042 && outof_target != 0
1043 && !CONSTANT_P (op1))
1044 {
1045 if (!expand_doubleword_shift (op1_mode, binoptab,
1046 outof_input, into_input, op1,
1047 0, into_target,
1048 unsignedp, methods, shift_mask))
1049 return false;
1050 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1051 outof_target, unsignedp, methods))
1052 return false;
1053 return true;
1054 }
1055
1056 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1057 is true when the effective shift value is less than BITS_PER_WORD.
1058 Set SUPERWORD_OP1 to the shift count that should be used to shift
1059 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1060 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
1061 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1062 {
1063 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1064 is a subword shift count. */
1065 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1066 0, true, methods);
1067 cmp2 = CONST0_RTX (op1_mode);
1068 cmp_code = EQ;
1069 superword_op1 = op1;
1070 }
1071 else
1072 {
1073 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1074 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1075 0, true, methods);
1076 cmp2 = CONST0_RTX (op1_mode);
1077 cmp_code = LT;
1078 superword_op1 = cmp1;
1079 }
1080 if (cmp1 == 0)
1081 return false;
1082
1083 /* If we can compute the condition at compile time, pick the
1084 appropriate subroutine. */
1085 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1086 if (tmp != 0 && CONST_INT_P (tmp))
1087 {
1088 if (tmp == const0_rtx)
1089 return expand_superword_shift (binoptab, outof_input, superword_op1,
1090 outof_target, into_target,
1091 unsignedp, methods);
1092 else
1093 return expand_subword_shift (op1_mode, binoptab,
1094 outof_input, into_input, op1,
1095 outof_target, into_target,
1096 unsignedp, methods, shift_mask);
1097 }
1098
1099 #ifdef HAVE_conditional_move
1100 /* Try using conditional moves to generate straight-line code. */
1101 {
1102 rtx_insn *start = get_last_insn ();
1103 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1104 cmp_code, cmp1, cmp2,
1105 outof_input, into_input,
1106 op1, superword_op1,
1107 outof_target, into_target,
1108 unsignedp, methods, shift_mask))
1109 return true;
1110 delete_insns_since (start);
1111 }
1112 #endif
1113
1114 /* As a last resort, use branches to select the correct alternative. */
1115 rtx_code_label *subword_label = gen_label_rtx ();
1116 rtx_code_label *done_label = gen_label_rtx ();
1117
1118 NO_DEFER_POP;
1119 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1120 0, 0, subword_label, -1);
1121 OK_DEFER_POP;
1122
1123 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1124 outof_target, into_target,
1125 unsignedp, methods))
1126 return false;
1127
1128 emit_jump_insn (gen_jump (done_label));
1129 emit_barrier ();
1130 emit_label (subword_label);
1131
1132 if (!expand_subword_shift (op1_mode, binoptab,
1133 outof_input, into_input, op1,
1134 outof_target, into_target,
1135 unsignedp, methods, shift_mask))
1136 return false;
1137
1138 emit_label (done_label);
1139 return true;
1140 }
1141 \f
1142 /* Subroutine of expand_binop. Perform a double word multiplication of
1143 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1144 as the target's word_mode. This function return NULL_RTX if anything
1145 goes wrong, in which case it may have already emitted instructions
1146 which need to be deleted.
1147
1148 If we want to multiply two two-word values and have normal and widening
1149 multiplies of single-word values, we can do this with three smaller
1150 multiplications.
1151
1152 The multiplication proceeds as follows:
1153 _______________________
1154 [__op0_high_|__op0_low__]
1155 _______________________
1156 * [__op1_high_|__op1_low__]
1157 _______________________________________________
1158 _______________________
1159 (1) [__op0_low__*__op1_low__]
1160 _______________________
1161 (2a) [__op0_low__*__op1_high_]
1162 _______________________
1163 (2b) [__op0_high_*__op1_low__]
1164 _______________________
1165 (3) [__op0_high_*__op1_high_]
1166
1167
1168 This gives a 4-word result. Since we are only interested in the
1169 lower 2 words, partial result (3) and the upper words of (2a) and
1170 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1171 calculated using non-widening multiplication.
1172
1173 (1), however, needs to be calculated with an unsigned widening
1174 multiplication. If this operation is not directly supported we
1175 try using a signed widening multiplication and adjust the result.
1176 This adjustment works as follows:
1177
1178 If both operands are positive then no adjustment is needed.
1179
1180 If the operands have different signs, for example op0_low < 0 and
1181 op1_low >= 0, the instruction treats the most significant bit of
1182 op0_low as a sign bit instead of a bit with significance
1183 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1184 with 2**BITS_PER_WORD - op0_low, and two's complements the
1185 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1186 the result.
1187
1188 Similarly, if both operands are negative, we need to add
1189 (op0_low + op1_low) * 2**BITS_PER_WORD.
1190
1191 We use a trick to adjust quickly. We logically shift op0_low right
1192 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1193 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1194 logical shift exists, we do an arithmetic right shift and subtract
1195 the 0 or -1. */
1196
1197 static rtx
1198 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
1199 bool umulp, enum optab_methods methods)
1200 {
1201 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1202 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1203 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1204 rtx product, adjust, product_high, temp;
1205
1206 rtx op0_high = operand_subword_force (op0, high, mode);
1207 rtx op0_low = operand_subword_force (op0, low, mode);
1208 rtx op1_high = operand_subword_force (op1, high, mode);
1209 rtx op1_low = operand_subword_force (op1, low, mode);
1210
1211 /* If we're using an unsigned multiply to directly compute the product
1212 of the low-order words of the operands and perform any required
1213 adjustments of the operands, we begin by trying two more multiplications
1214 and then computing the appropriate sum.
1215
1216 We have checked above that the required addition is provided.
1217 Full-word addition will normally always succeed, especially if
1218 it is provided at all, so we don't worry about its failure. The
1219 multiplication may well fail, however, so we do handle that. */
1220
1221 if (!umulp)
1222 {
1223 /* ??? This could be done with emit_store_flag where available. */
1224 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1225 NULL_RTX, 1, methods);
1226 if (temp)
1227 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1228 NULL_RTX, 0, OPTAB_DIRECT);
1229 else
1230 {
1231 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1232 NULL_RTX, 0, methods);
1233 if (!temp)
1234 return NULL_RTX;
1235 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1236 NULL_RTX, 0, OPTAB_DIRECT);
1237 }
1238
1239 if (!op0_high)
1240 return NULL_RTX;
1241 }
1242
1243 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1244 NULL_RTX, 0, OPTAB_DIRECT);
1245 if (!adjust)
1246 return NULL_RTX;
1247
1248 /* OP0_HIGH should now be dead. */
1249
1250 if (!umulp)
1251 {
1252 /* ??? This could be done with emit_store_flag where available. */
1253 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1254 NULL_RTX, 1, methods);
1255 if (temp)
1256 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1257 NULL_RTX, 0, OPTAB_DIRECT);
1258 else
1259 {
1260 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1261 NULL_RTX, 0, methods);
1262 if (!temp)
1263 return NULL_RTX;
1264 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1265 NULL_RTX, 0, OPTAB_DIRECT);
1266 }
1267
1268 if (!op1_high)
1269 return NULL_RTX;
1270 }
1271
1272 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1273 NULL_RTX, 0, OPTAB_DIRECT);
1274 if (!temp)
1275 return NULL_RTX;
1276
1277 /* OP1_HIGH should now be dead. */
1278
1279 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1280 NULL_RTX, 0, OPTAB_DIRECT);
1281
1282 if (target && !REG_P (target))
1283 target = NULL_RTX;
1284
1285 if (umulp)
1286 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1287 target, 1, OPTAB_DIRECT);
1288 else
1289 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1290 target, 1, OPTAB_DIRECT);
1291
1292 if (!product)
1293 return NULL_RTX;
1294
1295 product_high = operand_subword (product, high, 1, mode);
1296 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1297 NULL_RTX, 0, OPTAB_DIRECT);
1298 emit_move_insn (product_high, adjust);
1299 return product;
1300 }
1301 \f
1302 /* Wrapper around expand_binop which takes an rtx code to specify
1303 the operation to perform, not an optab pointer. All other
1304 arguments are the same. */
1305 rtx
1306 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
1307 rtx op1, rtx target, int unsignedp,
1308 enum optab_methods methods)
1309 {
1310 optab binop = code_to_optab (code);
1311 gcc_assert (binop);
1312
1313 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1314 }
1315
1316 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1317 binop. Order them according to commutative_operand_precedence and, if
1318 possible, try to put TARGET or a pseudo first. */
1319 static bool
1320 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1321 {
1322 int op0_prec = commutative_operand_precedence (op0);
1323 int op1_prec = commutative_operand_precedence (op1);
1324
1325 if (op0_prec < op1_prec)
1326 return true;
1327
1328 if (op0_prec > op1_prec)
1329 return false;
1330
1331 /* With equal precedence, both orders are ok, but it is better if the
1332 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1333 if (target == 0 || REG_P (target))
1334 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1335 else
1336 return rtx_equal_p (op1, target);
1337 }
1338
1339 /* Return true if BINOPTAB implements a shift operation. */
1340
1341 static bool
1342 shift_optab_p (optab binoptab)
1343 {
1344 switch (optab_to_code (binoptab))
1345 {
1346 case ASHIFT:
1347 case SS_ASHIFT:
1348 case US_ASHIFT:
1349 case ASHIFTRT:
1350 case LSHIFTRT:
1351 case ROTATE:
1352 case ROTATERT:
1353 return true;
1354
1355 default:
1356 return false;
1357 }
1358 }
1359
1360 /* Return true if BINOPTAB implements a commutative binary operation. */
1361
1362 static bool
1363 commutative_optab_p (optab binoptab)
1364 {
1365 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1366 || binoptab == smul_widen_optab
1367 || binoptab == umul_widen_optab
1368 || binoptab == smul_highpart_optab
1369 || binoptab == umul_highpart_optab);
1370 }
1371
1372 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1373 optimizing, and if the operand is a constant that costs more than
1374 1 instruction, force the constant into a register and return that
1375 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1376
1377 static rtx
1378 avoid_expensive_constant (machine_mode mode, optab binoptab,
1379 int opn, rtx x, bool unsignedp)
1380 {
1381 bool speed = optimize_insn_for_speed_p ();
1382
1383 if (mode != VOIDmode
1384 && optimize
1385 && CONSTANT_P (x)
1386 && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
1387 > set_src_cost (x, speed)))
1388 {
1389 if (CONST_INT_P (x))
1390 {
1391 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1392 if (intval != INTVAL (x))
1393 x = GEN_INT (intval);
1394 }
1395 else
1396 x = convert_modes (mode, VOIDmode, x, unsignedp);
1397 x = force_reg (mode, x);
1398 }
1399 return x;
1400 }
1401
1402 /* Helper function for expand_binop: handle the case where there
1403 is an insn that directly implements the indicated operation.
1404 Returns null if this is not possible. */
1405 static rtx
1406 expand_binop_directly (machine_mode mode, optab binoptab,
1407 rtx op0, rtx op1,
1408 rtx target, int unsignedp, enum optab_methods methods,
1409 rtx_insn *last)
1410 {
1411 machine_mode from_mode = widened_mode (mode, op0, op1);
1412 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1413 from_mode, 1);
1414 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1415 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1416 machine_mode mode0, mode1, tmp_mode;
1417 struct expand_operand ops[3];
1418 bool commutative_p;
1419 rtx pat;
1420 rtx xop0 = op0, xop1 = op1;
1421
1422 /* If it is a commutative operator and the modes would match
1423 if we would swap the operands, we can save the conversions. */
1424 commutative_p = commutative_optab_p (binoptab);
1425 if (commutative_p
1426 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1427 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1428 std::swap (xop0, xop1);
1429
1430 /* If we are optimizing, force expensive constants into a register. */
1431 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1432 if (!shift_optab_p (binoptab))
1433 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1434
1435 /* In case the insn wants input operands in modes different from
1436 those of the actual operands, convert the operands. It would
1437 seem that we don't need to convert CONST_INTs, but we do, so
1438 that they're properly zero-extended, sign-extended or truncated
1439 for their mode. */
1440
1441 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1442 if (xmode0 != VOIDmode && xmode0 != mode0)
1443 {
1444 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1445 mode0 = xmode0;
1446 }
1447
1448 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1449 if (xmode1 != VOIDmode && xmode1 != mode1)
1450 {
1451 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1452 mode1 = xmode1;
1453 }
1454
1455 /* If operation is commutative,
1456 try to make the first operand a register.
1457 Even better, try to make it the same as the target.
1458 Also try to make the last operand a constant. */
1459 if (commutative_p
1460 && swap_commutative_operands_with_target (target, xop0, xop1))
1461 std::swap (xop0, xop1);
1462
1463 /* Now, if insn's predicates don't allow our operands, put them into
1464 pseudo regs. */
1465
1466 if (binoptab == vec_pack_trunc_optab
1467 || binoptab == vec_pack_usat_optab
1468 || binoptab == vec_pack_ssat_optab
1469 || binoptab == vec_pack_ufix_trunc_optab
1470 || binoptab == vec_pack_sfix_trunc_optab)
1471 {
1472 /* The mode of the result is different then the mode of the
1473 arguments. */
1474 tmp_mode = insn_data[(int) icode].operand[0].mode;
1475 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1476 {
1477 delete_insns_since (last);
1478 return NULL_RTX;
1479 }
1480 }
1481 else
1482 tmp_mode = mode;
1483
1484 create_output_operand (&ops[0], target, tmp_mode);
1485 create_input_operand (&ops[1], xop0, mode0);
1486 create_input_operand (&ops[2], xop1, mode1);
1487 pat = maybe_gen_insn (icode, 3, ops);
1488 if (pat)
1489 {
1490 /* If PAT is composed of more than one insn, try to add an appropriate
1491 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1492 operand, call expand_binop again, this time without a target. */
1493 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
1494 && ! add_equal_note (as_a <rtx_insn *> (pat), ops[0].value,
1495 optab_to_code (binoptab),
1496 ops[1].value, ops[2].value))
1497 {
1498 delete_insns_since (last);
1499 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1500 unsignedp, methods);
1501 }
1502
1503 emit_insn (pat);
1504 return ops[0].value;
1505 }
1506 delete_insns_since (last);
1507 return NULL_RTX;
1508 }
1509
1510 /* Generate code to perform an operation specified by BINOPTAB
1511 on operands OP0 and OP1, with result having machine-mode MODE.
1512
1513 UNSIGNEDP is for the case where we have to widen the operands
1514 to perform the operation. It says to use zero-extension.
1515
1516 If TARGET is nonzero, the value
1517 is generated there, if it is convenient to do so.
1518 In all cases an rtx is returned for the locus of the value;
1519 this may or may not be TARGET. */
1520
1521 rtx
1522 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1523 rtx target, int unsignedp, enum optab_methods methods)
1524 {
1525 enum optab_methods next_methods
1526 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1527 ? OPTAB_WIDEN : methods);
1528 enum mode_class mclass;
1529 machine_mode wider_mode;
1530 rtx libfunc;
1531 rtx temp;
1532 rtx_insn *entry_last = get_last_insn ();
1533 rtx_insn *last;
1534
1535 mclass = GET_MODE_CLASS (mode);
1536
1537 /* If subtracting an integer constant, convert this into an addition of
1538 the negated constant. */
1539
1540 if (binoptab == sub_optab && CONST_INT_P (op1))
1541 {
1542 op1 = negate_rtx (mode, op1);
1543 binoptab = add_optab;
1544 }
1545
1546 /* Record where to delete back to if we backtrack. */
1547 last = get_last_insn ();
1548
1549 /* If we can do it with a three-operand insn, do so. */
1550
1551 if (methods != OPTAB_MUST_WIDEN
1552 && find_widening_optab_handler (binoptab, mode,
1553 widened_mode (mode, op0, op1), 1)
1554 != CODE_FOR_nothing)
1555 {
1556 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1557 unsignedp, methods, last);
1558 if (temp)
1559 return temp;
1560 }
1561
1562 /* If we were trying to rotate, and that didn't work, try rotating
1563 the other direction before falling back to shifts and bitwise-or. */
1564 if (((binoptab == rotl_optab
1565 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1566 || (binoptab == rotr_optab
1567 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1568 && mclass == MODE_INT)
1569 {
1570 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1571 rtx newop1;
1572 unsigned int bits = GET_MODE_PRECISION (mode);
1573
1574 if (CONST_INT_P (op1))
1575 newop1 = GEN_INT (bits - INTVAL (op1));
1576 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1577 newop1 = negate_rtx (GET_MODE (op1), op1);
1578 else
1579 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1580 gen_int_mode (bits, GET_MODE (op1)), op1,
1581 NULL_RTX, unsignedp, OPTAB_DIRECT);
1582
1583 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1584 target, unsignedp, methods, last);
1585 if (temp)
1586 return temp;
1587 }
1588
1589 /* If this is a multiply, see if we can do a widening operation that
1590 takes operands of this mode and makes a wider mode. */
1591
1592 if (binoptab == smul_optab
1593 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1594 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1595 : smul_widen_optab),
1596 GET_MODE_2XWIDER_MODE (mode), mode)
1597 != CODE_FOR_nothing))
1598 {
1599 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1600 unsignedp ? umul_widen_optab : smul_widen_optab,
1601 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1602
1603 if (temp != 0)
1604 {
1605 if (GET_MODE_CLASS (mode) == MODE_INT
1606 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1607 return gen_lowpart (mode, temp);
1608 else
1609 return convert_to_mode (mode, temp, unsignedp);
1610 }
1611 }
1612
1613 /* If this is a vector shift by a scalar, see if we can do a vector
1614 shift by a vector. If so, broadcast the scalar into a vector. */
1615 if (mclass == MODE_VECTOR_INT)
1616 {
1617 optab otheroptab = unknown_optab;
1618
1619 if (binoptab == ashl_optab)
1620 otheroptab = vashl_optab;
1621 else if (binoptab == ashr_optab)
1622 otheroptab = vashr_optab;
1623 else if (binoptab == lshr_optab)
1624 otheroptab = vlshr_optab;
1625 else if (binoptab == rotl_optab)
1626 otheroptab = vrotl_optab;
1627 else if (binoptab == rotr_optab)
1628 otheroptab = vrotr_optab;
1629
1630 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1631 {
1632 rtx vop1 = expand_vector_broadcast (mode, op1);
1633 if (vop1)
1634 {
1635 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1636 target, unsignedp, methods, last);
1637 if (temp)
1638 return temp;
1639 }
1640 }
1641 }
1642
1643 /* Look for a wider mode of the same class for which we think we
1644 can open-code the operation. Check for a widening multiply at the
1645 wider mode as well. */
1646
1647 if (CLASS_HAS_WIDER_MODES_P (mclass)
1648 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1649 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1650 wider_mode != VOIDmode;
1651 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1652 {
1653 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1654 || (binoptab == smul_optab
1655 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1656 && (find_widening_optab_handler ((unsignedp
1657 ? umul_widen_optab
1658 : smul_widen_optab),
1659 GET_MODE_WIDER_MODE (wider_mode),
1660 mode, 0)
1661 != CODE_FOR_nothing)))
1662 {
1663 rtx xop0 = op0, xop1 = op1;
1664 int no_extend = 0;
1665
1666 /* For certain integer operations, we need not actually extend
1667 the narrow operands, as long as we will truncate
1668 the results to the same narrowness. */
1669
1670 if ((binoptab == ior_optab || binoptab == and_optab
1671 || binoptab == xor_optab
1672 || binoptab == add_optab || binoptab == sub_optab
1673 || binoptab == smul_optab || binoptab == ashl_optab)
1674 && mclass == MODE_INT)
1675 {
1676 no_extend = 1;
1677 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1678 xop0, unsignedp);
1679 if (binoptab != ashl_optab)
1680 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1681 xop1, unsignedp);
1682 }
1683
1684 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1685
1686 /* The second operand of a shift must always be extended. */
1687 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1688 no_extend && binoptab != ashl_optab);
1689
1690 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1691 unsignedp, OPTAB_DIRECT);
1692 if (temp)
1693 {
1694 if (mclass != MODE_INT
1695 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1696 {
1697 if (target == 0)
1698 target = gen_reg_rtx (mode);
1699 convert_move (target, temp, 0);
1700 return target;
1701 }
1702 else
1703 return gen_lowpart (mode, temp);
1704 }
1705 else
1706 delete_insns_since (last);
1707 }
1708 }
1709
1710 /* If operation is commutative,
1711 try to make the first operand a register.
1712 Even better, try to make it the same as the target.
1713 Also try to make the last operand a constant. */
1714 if (commutative_optab_p (binoptab)
1715 && swap_commutative_operands_with_target (target, op0, op1))
1716 std::swap (op0, op1);
1717
1718 /* These can be done a word at a time. */
1719 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1720 && mclass == MODE_INT
1721 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1722 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1723 {
1724 int i;
1725 rtx_insn *insns;
1726
1727 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1728 won't be accurate, so use a new target. */
1729 if (target == 0
1730 || target == op0
1731 || target == op1
1732 || !valid_multiword_target_p (target))
1733 target = gen_reg_rtx (mode);
1734
1735 start_sequence ();
1736
1737 /* Do the actual arithmetic. */
1738 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1739 {
1740 rtx target_piece = operand_subword (target, i, 1, mode);
1741 rtx x = expand_binop (word_mode, binoptab,
1742 operand_subword_force (op0, i, mode),
1743 operand_subword_force (op1, i, mode),
1744 target_piece, unsignedp, next_methods);
1745
1746 if (x == 0)
1747 break;
1748
1749 if (target_piece != x)
1750 emit_move_insn (target_piece, x);
1751 }
1752
1753 insns = get_insns ();
1754 end_sequence ();
1755
1756 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1757 {
1758 emit_insn (insns);
1759 return target;
1760 }
1761 }
1762
1763 /* Synthesize double word shifts from single word shifts. */
1764 if ((binoptab == lshr_optab || binoptab == ashl_optab
1765 || binoptab == ashr_optab)
1766 && mclass == MODE_INT
1767 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1768 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1769 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1770 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1771 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1772 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1773 {
1774 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1775 machine_mode op1_mode;
1776
1777 double_shift_mask = targetm.shift_truncation_mask (mode);
1778 shift_mask = targetm.shift_truncation_mask (word_mode);
1779 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1780
1781 /* Apply the truncation to constant shifts. */
1782 if (double_shift_mask > 0 && CONST_INT_P (op1))
1783 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1784
1785 if (op1 == CONST0_RTX (op1_mode))
1786 return op0;
1787
1788 /* Make sure that this is a combination that expand_doubleword_shift
1789 can handle. See the comments there for details. */
1790 if (double_shift_mask == 0
1791 || (shift_mask == BITS_PER_WORD - 1
1792 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1793 {
1794 rtx_insn *insns;
1795 rtx into_target, outof_target;
1796 rtx into_input, outof_input;
1797 int left_shift, outof_word;
1798
1799 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1800 won't be accurate, so use a new target. */
1801 if (target == 0
1802 || target == op0
1803 || target == op1
1804 || !valid_multiword_target_p (target))
1805 target = gen_reg_rtx (mode);
1806
1807 start_sequence ();
1808
1809 /* OUTOF_* is the word we are shifting bits away from, and
1810 INTO_* is the word that we are shifting bits towards, thus
1811 they differ depending on the direction of the shift and
1812 WORDS_BIG_ENDIAN. */
1813
1814 left_shift = binoptab == ashl_optab;
1815 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1816
1817 outof_target = operand_subword (target, outof_word, 1, mode);
1818 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1819
1820 outof_input = operand_subword_force (op0, outof_word, mode);
1821 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1822
1823 if (expand_doubleword_shift (op1_mode, binoptab,
1824 outof_input, into_input, op1,
1825 outof_target, into_target,
1826 unsignedp, next_methods, shift_mask))
1827 {
1828 insns = get_insns ();
1829 end_sequence ();
1830
1831 emit_insn (insns);
1832 return target;
1833 }
1834 end_sequence ();
1835 }
1836 }
1837
1838 /* Synthesize double word rotates from single word shifts. */
1839 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1840 && mclass == MODE_INT
1841 && CONST_INT_P (op1)
1842 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1843 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1844 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1845 {
1846 rtx_insn *insns;
1847 rtx into_target, outof_target;
1848 rtx into_input, outof_input;
1849 rtx inter;
1850 int shift_count, left_shift, outof_word;
1851
1852 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1853 won't be accurate, so use a new target. Do this also if target is not
1854 a REG, first because having a register instead may open optimization
1855 opportunities, and second because if target and op0 happen to be MEMs
1856 designating the same location, we would risk clobbering it too early
1857 in the code sequence we generate below. */
1858 if (target == 0
1859 || target == op0
1860 || target == op1
1861 || !REG_P (target)
1862 || !valid_multiword_target_p (target))
1863 target = gen_reg_rtx (mode);
1864
1865 start_sequence ();
1866
1867 shift_count = INTVAL (op1);
1868
1869 /* OUTOF_* is the word we are shifting bits away from, and
1870 INTO_* is the word that we are shifting bits towards, thus
1871 they differ depending on the direction of the shift and
1872 WORDS_BIG_ENDIAN. */
1873
1874 left_shift = (binoptab == rotl_optab);
1875 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1876
1877 outof_target = operand_subword (target, outof_word, 1, mode);
1878 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1879
1880 outof_input = operand_subword_force (op0, outof_word, mode);
1881 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1882
1883 if (shift_count == BITS_PER_WORD)
1884 {
1885 /* This is just a word swap. */
1886 emit_move_insn (outof_target, into_input);
1887 emit_move_insn (into_target, outof_input);
1888 inter = const0_rtx;
1889 }
1890 else
1891 {
1892 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1893 rtx first_shift_count, second_shift_count;
1894 optab reverse_unsigned_shift, unsigned_shift;
1895
1896 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1897 ? lshr_optab : ashl_optab);
1898
1899 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1900 ? ashl_optab : lshr_optab);
1901
1902 if (shift_count > BITS_PER_WORD)
1903 {
1904 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1905 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1906 }
1907 else
1908 {
1909 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1910 second_shift_count = GEN_INT (shift_count);
1911 }
1912
1913 into_temp1 = expand_binop (word_mode, unsigned_shift,
1914 outof_input, first_shift_count,
1915 NULL_RTX, unsignedp, next_methods);
1916 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1917 into_input, second_shift_count,
1918 NULL_RTX, unsignedp, next_methods);
1919
1920 if (into_temp1 != 0 && into_temp2 != 0)
1921 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1922 into_target, unsignedp, next_methods);
1923 else
1924 inter = 0;
1925
1926 if (inter != 0 && inter != into_target)
1927 emit_move_insn (into_target, inter);
1928
1929 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1930 into_input, first_shift_count,
1931 NULL_RTX, unsignedp, next_methods);
1932 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1933 outof_input, second_shift_count,
1934 NULL_RTX, unsignedp, next_methods);
1935
1936 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1937 inter = expand_binop (word_mode, ior_optab,
1938 outof_temp1, outof_temp2,
1939 outof_target, unsignedp, next_methods);
1940
1941 if (inter != 0 && inter != outof_target)
1942 emit_move_insn (outof_target, inter);
1943 }
1944
1945 insns = get_insns ();
1946 end_sequence ();
1947
1948 if (inter != 0)
1949 {
1950 emit_insn (insns);
1951 return target;
1952 }
1953 }
1954
1955 /* These can be done a word at a time by propagating carries. */
1956 if ((binoptab == add_optab || binoptab == sub_optab)
1957 && mclass == MODE_INT
1958 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1959 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1960 {
1961 unsigned int i;
1962 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1963 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1964 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1965 rtx xop0, xop1, xtarget;
1966
1967 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1968 value is one of those, use it. Otherwise, use 1 since it is the
1969 one easiest to get. */
1970 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1971 int normalizep = STORE_FLAG_VALUE;
1972 #else
1973 int normalizep = 1;
1974 #endif
1975
1976 /* Prepare the operands. */
1977 xop0 = force_reg (mode, op0);
1978 xop1 = force_reg (mode, op1);
1979
1980 xtarget = gen_reg_rtx (mode);
1981
1982 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1983 target = xtarget;
1984
1985 /* Indicate for flow that the entire target reg is being set. */
1986 if (REG_P (target))
1987 emit_clobber (xtarget);
1988
1989 /* Do the actual arithmetic. */
1990 for (i = 0; i < nwords; i++)
1991 {
1992 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1993 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1994 rtx op0_piece = operand_subword_force (xop0, index, mode);
1995 rtx op1_piece = operand_subword_force (xop1, index, mode);
1996 rtx x;
1997
1998 /* Main add/subtract of the input operands. */
1999 x = expand_binop (word_mode, binoptab,
2000 op0_piece, op1_piece,
2001 target_piece, unsignedp, next_methods);
2002 if (x == 0)
2003 break;
2004
2005 if (i + 1 < nwords)
2006 {
2007 /* Store carry from main add/subtract. */
2008 carry_out = gen_reg_rtx (word_mode);
2009 carry_out = emit_store_flag_force (carry_out,
2010 (binoptab == add_optab
2011 ? LT : GT),
2012 x, op0_piece,
2013 word_mode, 1, normalizep);
2014 }
2015
2016 if (i > 0)
2017 {
2018 rtx newx;
2019
2020 /* Add/subtract previous carry to main result. */
2021 newx = expand_binop (word_mode,
2022 normalizep == 1 ? binoptab : otheroptab,
2023 x, carry_in,
2024 NULL_RTX, 1, next_methods);
2025
2026 if (i + 1 < nwords)
2027 {
2028 /* Get out carry from adding/subtracting carry in. */
2029 rtx carry_tmp = gen_reg_rtx (word_mode);
2030 carry_tmp = emit_store_flag_force (carry_tmp,
2031 (binoptab == add_optab
2032 ? LT : GT),
2033 newx, x,
2034 word_mode, 1, normalizep);
2035
2036 /* Logical-ior the two poss. carry together. */
2037 carry_out = expand_binop (word_mode, ior_optab,
2038 carry_out, carry_tmp,
2039 carry_out, 0, next_methods);
2040 if (carry_out == 0)
2041 break;
2042 }
2043 emit_move_insn (target_piece, newx);
2044 }
2045 else
2046 {
2047 if (x != target_piece)
2048 emit_move_insn (target_piece, x);
2049 }
2050
2051 carry_in = carry_out;
2052 }
2053
2054 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2055 {
2056 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2057 || ! rtx_equal_p (target, xtarget))
2058 {
2059 rtx temp = emit_move_insn (target, xtarget);
2060
2061 set_dst_reg_note (temp, REG_EQUAL,
2062 gen_rtx_fmt_ee (optab_to_code (binoptab),
2063 mode, copy_rtx (xop0),
2064 copy_rtx (xop1)),
2065 target);
2066 }
2067 else
2068 target = xtarget;
2069
2070 return target;
2071 }
2072
2073 else
2074 delete_insns_since (last);
2075 }
2076
2077 /* Attempt to synthesize double word multiplies using a sequence of word
2078 mode multiplications. We first attempt to generate a sequence using a
2079 more efficient unsigned widening multiply, and if that fails we then
2080 try using a signed widening multiply. */
2081
2082 if (binoptab == smul_optab
2083 && mclass == MODE_INT
2084 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2085 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2086 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2087 {
2088 rtx product = NULL_RTX;
2089 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2090 != CODE_FOR_nothing)
2091 {
2092 product = expand_doubleword_mult (mode, op0, op1, target,
2093 true, methods);
2094 if (!product)
2095 delete_insns_since (last);
2096 }
2097
2098 if (product == NULL_RTX
2099 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2100 != CODE_FOR_nothing)
2101 {
2102 product = expand_doubleword_mult (mode, op0, op1, target,
2103 false, methods);
2104 if (!product)
2105 delete_insns_since (last);
2106 }
2107
2108 if (product != NULL_RTX)
2109 {
2110 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2111 {
2112 temp = emit_move_insn (target ? target : product, product);
2113 set_dst_reg_note (temp,
2114 REG_EQUAL,
2115 gen_rtx_fmt_ee (MULT, mode,
2116 copy_rtx (op0),
2117 copy_rtx (op1)),
2118 target ? target : product);
2119 }
2120 return product;
2121 }
2122 }
2123
2124 /* It can't be open-coded in this mode.
2125 Use a library call if one is available and caller says that's ok. */
2126
2127 libfunc = optab_libfunc (binoptab, mode);
2128 if (libfunc
2129 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2130 {
2131 rtx_insn *insns;
2132 rtx op1x = op1;
2133 machine_mode op1_mode = mode;
2134 rtx value;
2135
2136 start_sequence ();
2137
2138 if (shift_optab_p (binoptab))
2139 {
2140 op1_mode = targetm.libgcc_shift_count_mode ();
2141 /* Specify unsigned here,
2142 since negative shift counts are meaningless. */
2143 op1x = convert_to_mode (op1_mode, op1, 1);
2144 }
2145
2146 if (GET_MODE (op0) != VOIDmode
2147 && GET_MODE (op0) != mode)
2148 op0 = convert_to_mode (mode, op0, unsignedp);
2149
2150 /* Pass 1 for NO_QUEUE so we don't lose any increments
2151 if the libcall is cse'd or moved. */
2152 value = emit_library_call_value (libfunc,
2153 NULL_RTX, LCT_CONST, mode, 2,
2154 op0, mode, op1x, op1_mode);
2155
2156 insns = get_insns ();
2157 end_sequence ();
2158
2159 target = gen_reg_rtx (mode);
2160 emit_libcall_block_1 (insns, target, value,
2161 gen_rtx_fmt_ee (optab_to_code (binoptab),
2162 mode, op0, op1),
2163 trapv_binoptab_p (binoptab));
2164
2165 return target;
2166 }
2167
2168 delete_insns_since (last);
2169
2170 /* It can't be done in this mode. Can we do it in a wider mode? */
2171
2172 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2173 || methods == OPTAB_MUST_WIDEN))
2174 {
2175 /* Caller says, don't even try. */
2176 delete_insns_since (entry_last);
2177 return 0;
2178 }
2179
2180 /* Compute the value of METHODS to pass to recursive calls.
2181 Don't allow widening to be tried recursively. */
2182
2183 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2184
2185 /* Look for a wider mode of the same class for which it appears we can do
2186 the operation. */
2187
2188 if (CLASS_HAS_WIDER_MODES_P (mclass))
2189 {
2190 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2191 wider_mode != VOIDmode;
2192 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2193 {
2194 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2195 != CODE_FOR_nothing
2196 || (methods == OPTAB_LIB
2197 && optab_libfunc (binoptab, wider_mode)))
2198 {
2199 rtx xop0 = op0, xop1 = op1;
2200 int no_extend = 0;
2201
2202 /* For certain integer operations, we need not actually extend
2203 the narrow operands, as long as we will truncate
2204 the results to the same narrowness. */
2205
2206 if ((binoptab == ior_optab || binoptab == and_optab
2207 || binoptab == xor_optab
2208 || binoptab == add_optab || binoptab == sub_optab
2209 || binoptab == smul_optab || binoptab == ashl_optab)
2210 && mclass == MODE_INT)
2211 no_extend = 1;
2212
2213 xop0 = widen_operand (xop0, wider_mode, mode,
2214 unsignedp, no_extend);
2215
2216 /* The second operand of a shift must always be extended. */
2217 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2218 no_extend && binoptab != ashl_optab);
2219
2220 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2221 unsignedp, methods);
2222 if (temp)
2223 {
2224 if (mclass != MODE_INT
2225 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2226 {
2227 if (target == 0)
2228 target = gen_reg_rtx (mode);
2229 convert_move (target, temp, 0);
2230 return target;
2231 }
2232 else
2233 return gen_lowpart (mode, temp);
2234 }
2235 else
2236 delete_insns_since (last);
2237 }
2238 }
2239 }
2240
2241 delete_insns_since (entry_last);
2242 return 0;
2243 }
2244 \f
2245 /* Expand a binary operator which has both signed and unsigned forms.
2246 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2247 signed operations.
2248
2249 If we widen unsigned operands, we may use a signed wider operation instead
2250 of an unsigned wider operation, since the result would be the same. */
2251
2252 rtx
2253 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
2254 rtx op0, rtx op1, rtx target, int unsignedp,
2255 enum optab_methods methods)
2256 {
2257 rtx temp;
2258 optab direct_optab = unsignedp ? uoptab : soptab;
2259 bool save_enable;
2260
2261 /* Do it without widening, if possible. */
2262 temp = expand_binop (mode, direct_optab, op0, op1, target,
2263 unsignedp, OPTAB_DIRECT);
2264 if (temp || methods == OPTAB_DIRECT)
2265 return temp;
2266
2267 /* Try widening to a signed int. Disable any direct use of any
2268 signed insn in the current mode. */
2269 save_enable = swap_optab_enable (soptab, mode, false);
2270
2271 temp = expand_binop (mode, soptab, op0, op1, target,
2272 unsignedp, OPTAB_WIDEN);
2273
2274 /* For unsigned operands, try widening to an unsigned int. */
2275 if (!temp && unsignedp)
2276 temp = expand_binop (mode, uoptab, op0, op1, target,
2277 unsignedp, OPTAB_WIDEN);
2278 if (temp || methods == OPTAB_WIDEN)
2279 goto egress;
2280
2281 /* Use the right width libcall if that exists. */
2282 temp = expand_binop (mode, direct_optab, op0, op1, target,
2283 unsignedp, OPTAB_LIB);
2284 if (temp || methods == OPTAB_LIB)
2285 goto egress;
2286
2287 /* Must widen and use a libcall, use either signed or unsigned. */
2288 temp = expand_binop (mode, soptab, op0, op1, target,
2289 unsignedp, methods);
2290 if (!temp && unsignedp)
2291 temp = expand_binop (mode, uoptab, op0, op1, target,
2292 unsignedp, methods);
2293
2294 egress:
2295 /* Undo the fiddling above. */
2296 if (save_enable)
2297 swap_optab_enable (soptab, mode, true);
2298 return temp;
2299 }
2300 \f
2301 /* Generate code to perform an operation specified by UNOPPTAB
2302 on operand OP0, with two results to TARG0 and TARG1.
2303 We assume that the order of the operands for the instruction
2304 is TARG0, TARG1, OP0.
2305
2306 Either TARG0 or TARG1 may be zero, but what that means is that
2307 the result is not actually wanted. We will generate it into
2308 a dummy pseudo-reg and discard it. They may not both be zero.
2309
2310 Returns 1 if this operation can be performed; 0 if not. */
2311
2312 int
2313 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2314 int unsignedp)
2315 {
2316 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2317 enum mode_class mclass;
2318 machine_mode wider_mode;
2319 rtx_insn *entry_last = get_last_insn ();
2320 rtx_insn *last;
2321
2322 mclass = GET_MODE_CLASS (mode);
2323
2324 if (!targ0)
2325 targ0 = gen_reg_rtx (mode);
2326 if (!targ1)
2327 targ1 = gen_reg_rtx (mode);
2328
2329 /* Record where to go back to if we fail. */
2330 last = get_last_insn ();
2331
2332 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2333 {
2334 struct expand_operand ops[3];
2335 enum insn_code icode = optab_handler (unoptab, mode);
2336
2337 create_fixed_operand (&ops[0], targ0);
2338 create_fixed_operand (&ops[1], targ1);
2339 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2340 if (maybe_expand_insn (icode, 3, ops))
2341 return 1;
2342 }
2343
2344 /* It can't be done in this mode. Can we do it in a wider mode? */
2345
2346 if (CLASS_HAS_WIDER_MODES_P (mclass))
2347 {
2348 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2349 wider_mode != VOIDmode;
2350 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2351 {
2352 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2353 {
2354 rtx t0 = gen_reg_rtx (wider_mode);
2355 rtx t1 = gen_reg_rtx (wider_mode);
2356 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2357
2358 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2359 {
2360 convert_move (targ0, t0, unsignedp);
2361 convert_move (targ1, t1, unsignedp);
2362 return 1;
2363 }
2364 else
2365 delete_insns_since (last);
2366 }
2367 }
2368 }
2369
2370 delete_insns_since (entry_last);
2371 return 0;
2372 }
2373 \f
2374 /* Generate code to perform an operation specified by BINOPTAB
2375 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2376 We assume that the order of the operands for the instruction
2377 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2378 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2379
2380 Either TARG0 or TARG1 may be zero, but what that means is that
2381 the result is not actually wanted. We will generate it into
2382 a dummy pseudo-reg and discard it. They may not both be zero.
2383
2384 Returns 1 if this operation can be performed; 0 if not. */
2385
2386 int
2387 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2388 int unsignedp)
2389 {
2390 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2391 enum mode_class mclass;
2392 machine_mode wider_mode;
2393 rtx_insn *entry_last = get_last_insn ();
2394 rtx_insn *last;
2395
2396 mclass = GET_MODE_CLASS (mode);
2397
2398 if (!targ0)
2399 targ0 = gen_reg_rtx (mode);
2400 if (!targ1)
2401 targ1 = gen_reg_rtx (mode);
2402
2403 /* Record where to go back to if we fail. */
2404 last = get_last_insn ();
2405
2406 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2407 {
2408 struct expand_operand ops[4];
2409 enum insn_code icode = optab_handler (binoptab, mode);
2410 machine_mode mode0 = insn_data[icode].operand[1].mode;
2411 machine_mode mode1 = insn_data[icode].operand[2].mode;
2412 rtx xop0 = op0, xop1 = op1;
2413
2414 /* If we are optimizing, force expensive constants into a register. */
2415 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2416 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2417
2418 create_fixed_operand (&ops[0], targ0);
2419 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2420 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2421 create_fixed_operand (&ops[3], targ1);
2422 if (maybe_expand_insn (icode, 4, ops))
2423 return 1;
2424 delete_insns_since (last);
2425 }
2426
2427 /* It can't be done in this mode. Can we do it in a wider mode? */
2428
2429 if (CLASS_HAS_WIDER_MODES_P (mclass))
2430 {
2431 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2432 wider_mode != VOIDmode;
2433 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2434 {
2435 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2436 {
2437 rtx t0 = gen_reg_rtx (wider_mode);
2438 rtx t1 = gen_reg_rtx (wider_mode);
2439 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2440 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2441
2442 if (expand_twoval_binop (binoptab, cop0, cop1,
2443 t0, t1, unsignedp))
2444 {
2445 convert_move (targ0, t0, unsignedp);
2446 convert_move (targ1, t1, unsignedp);
2447 return 1;
2448 }
2449 else
2450 delete_insns_since (last);
2451 }
2452 }
2453 }
2454
2455 delete_insns_since (entry_last);
2456 return 0;
2457 }
2458
2459 /* Expand the two-valued library call indicated by BINOPTAB, but
2460 preserve only one of the values. If TARG0 is non-NULL, the first
2461 value is placed into TARG0; otherwise the second value is placed
2462 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2463 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2464 This routine assumes that the value returned by the library call is
2465 as if the return value was of an integral mode twice as wide as the
2466 mode of OP0. Returns 1 if the call was successful. */
2467
2468 bool
2469 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2470 rtx targ0, rtx targ1, enum rtx_code code)
2471 {
2472 machine_mode mode;
2473 machine_mode libval_mode;
2474 rtx libval;
2475 rtx_insn *insns;
2476 rtx libfunc;
2477
2478 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2479 gcc_assert (!targ0 != !targ1);
2480
2481 mode = GET_MODE (op0);
2482 libfunc = optab_libfunc (binoptab, mode);
2483 if (!libfunc)
2484 return false;
2485
2486 /* The value returned by the library function will have twice as
2487 many bits as the nominal MODE. */
2488 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2489 MODE_INT);
2490 start_sequence ();
2491 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2492 libval_mode, 2,
2493 op0, mode,
2494 op1, mode);
2495 /* Get the part of VAL containing the value that we want. */
2496 libval = simplify_gen_subreg (mode, libval, libval_mode,
2497 targ0 ? 0 : GET_MODE_SIZE (mode));
2498 insns = get_insns ();
2499 end_sequence ();
2500 /* Move the into the desired location. */
2501 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2502 gen_rtx_fmt_ee (code, mode, op0, op1));
2503
2504 return true;
2505 }
2506
2507 \f
2508 /* Wrapper around expand_unop which takes an rtx code to specify
2509 the operation to perform, not an optab pointer. All other
2510 arguments are the same. */
2511 rtx
2512 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2513 rtx target, int unsignedp)
2514 {
2515 optab unop = code_to_optab (code);
2516 gcc_assert (unop);
2517
2518 return expand_unop (mode, unop, op0, target, unsignedp);
2519 }
2520
2521 /* Try calculating
2522 (clz:narrow x)
2523 as
2524 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2525
2526 A similar operation can be used for clrsb. UNOPTAB says which operation
2527 we are trying to expand. */
2528 static rtx
2529 widen_leading (machine_mode mode, rtx op0, rtx target, optab unoptab)
2530 {
2531 enum mode_class mclass = GET_MODE_CLASS (mode);
2532 if (CLASS_HAS_WIDER_MODES_P (mclass))
2533 {
2534 machine_mode wider_mode;
2535 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2536 wider_mode != VOIDmode;
2537 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2538 {
2539 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2540 {
2541 rtx xop0, temp;
2542 rtx_insn *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,
2549 unoptab != clrsb_optab, false);
2550 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2551 unoptab != clrsb_optab);
2552 if (temp != 0)
2553 temp = expand_binop
2554 (wider_mode, sub_optab, temp,
2555 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2556 - GET_MODE_PRECISION (mode),
2557 wider_mode),
2558 target, true, OPTAB_DIRECT);
2559 if (temp == 0)
2560 delete_insns_since (last);
2561
2562 return temp;
2563 }
2564 }
2565 }
2566 return 0;
2567 }
2568
2569 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2570 quantities, choosing which based on whether the high word is nonzero. */
2571 static rtx
2572 expand_doubleword_clz (machine_mode mode, rtx op0, rtx target)
2573 {
2574 rtx xop0 = force_reg (mode, op0);
2575 rtx subhi = gen_highpart (word_mode, xop0);
2576 rtx sublo = gen_lowpart (word_mode, xop0);
2577 rtx_code_label *hi0_label = gen_label_rtx ();
2578 rtx_code_label *after_label = gen_label_rtx ();
2579 rtx_insn *seq;
2580 rtx temp, result;
2581
2582 /* If we were not given a target, use a word_mode register, not a
2583 'mode' register. The result will fit, and nobody is expecting
2584 anything bigger (the return type of __builtin_clz* is int). */
2585 if (!target)
2586 target = gen_reg_rtx (word_mode);
2587
2588 /* In any case, write to a word_mode scratch in both branches of the
2589 conditional, so we can ensure there is a single move insn setting
2590 'target' to tag a REG_EQUAL note on. */
2591 result = gen_reg_rtx (word_mode);
2592
2593 start_sequence ();
2594
2595 /* If the high word is not equal to zero,
2596 then clz of the full value is clz of the high word. */
2597 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2598 word_mode, true, hi0_label);
2599
2600 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2601 if (!temp)
2602 goto fail;
2603
2604 if (temp != result)
2605 convert_move (result, temp, true);
2606
2607 emit_jump_insn (gen_jump (after_label));
2608 emit_barrier ();
2609
2610 /* Else clz of the full value is clz of the low word plus the number
2611 of bits in the high word. */
2612 emit_label (hi0_label);
2613
2614 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2615 if (!temp)
2616 goto fail;
2617 temp = expand_binop (word_mode, add_optab, temp,
2618 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2619 result, true, OPTAB_DIRECT);
2620 if (!temp)
2621 goto fail;
2622 if (temp != result)
2623 convert_move (result, temp, true);
2624
2625 emit_label (after_label);
2626 convert_move (target, result, true);
2627
2628 seq = get_insns ();
2629 end_sequence ();
2630
2631 add_equal_note (seq, target, CLZ, xop0, 0);
2632 emit_insn (seq);
2633 return target;
2634
2635 fail:
2636 end_sequence ();
2637 return 0;
2638 }
2639
2640 /* Try calculating
2641 (bswap:narrow x)
2642 as
2643 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2644 static rtx
2645 widen_bswap (machine_mode mode, rtx op0, rtx target)
2646 {
2647 enum mode_class mclass = GET_MODE_CLASS (mode);
2648 machine_mode wider_mode;
2649 rtx x;
2650 rtx_insn *last;
2651
2652 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2653 return NULL_RTX;
2654
2655 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2656 wider_mode != VOIDmode;
2657 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2658 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2659 goto found;
2660 return NULL_RTX;
2661
2662 found:
2663 last = get_last_insn ();
2664
2665 x = widen_operand (op0, wider_mode, mode, true, true);
2666 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2667
2668 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2669 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2670 if (x != 0)
2671 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2672 GET_MODE_BITSIZE (wider_mode)
2673 - GET_MODE_BITSIZE (mode),
2674 NULL_RTX, true);
2675
2676 if (x != 0)
2677 {
2678 if (target == 0)
2679 target = gen_reg_rtx (mode);
2680 emit_move_insn (target, gen_lowpart (mode, x));
2681 }
2682 else
2683 delete_insns_since (last);
2684
2685 return target;
2686 }
2687
2688 /* Try calculating bswap as two bswaps of two word-sized operands. */
2689
2690 static rtx
2691 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2692 {
2693 rtx t0, t1;
2694
2695 t1 = expand_unop (word_mode, bswap_optab,
2696 operand_subword_force (op, 0, mode), NULL_RTX, true);
2697 t0 = expand_unop (word_mode, bswap_optab,
2698 operand_subword_force (op, 1, mode), NULL_RTX, true);
2699
2700 if (target == 0 || !valid_multiword_target_p (target))
2701 target = gen_reg_rtx (mode);
2702 if (REG_P (target))
2703 emit_clobber (target);
2704 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2705 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2706
2707 return target;
2708 }
2709
2710 /* Try calculating (parity x) as (and (popcount x) 1), where
2711 popcount can also be done in a wider mode. */
2712 static rtx
2713 expand_parity (machine_mode mode, rtx op0, rtx target)
2714 {
2715 enum mode_class mclass = GET_MODE_CLASS (mode);
2716 if (CLASS_HAS_WIDER_MODES_P (mclass))
2717 {
2718 machine_mode wider_mode;
2719 for (wider_mode = mode; wider_mode != VOIDmode;
2720 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2721 {
2722 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2723 {
2724 rtx xop0, temp;
2725 rtx_insn *last;
2726
2727 last = get_last_insn ();
2728
2729 if (target == 0)
2730 target = gen_reg_rtx (mode);
2731 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2732 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2733 true);
2734 if (temp != 0)
2735 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2736 target, true, OPTAB_DIRECT);
2737 if (temp == 0)
2738 delete_insns_since (last);
2739
2740 return temp;
2741 }
2742 }
2743 }
2744 return 0;
2745 }
2746
2747 /* Try calculating ctz(x) as K - clz(x & -x) ,
2748 where K is GET_MODE_PRECISION(mode) - 1.
2749
2750 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2751 don't have to worry about what the hardware does in that case. (If
2752 the clz instruction produces the usual value at 0, which is K, the
2753 result of this code sequence will be -1; expand_ffs, below, relies
2754 on this. It might be nice to have it be K instead, for consistency
2755 with the (very few) processors that provide a ctz with a defined
2756 value, but that would take one more instruction, and it would be
2757 less convenient for expand_ffs anyway. */
2758
2759 static rtx
2760 expand_ctz (machine_mode mode, rtx op0, rtx target)
2761 {
2762 rtx_insn *seq;
2763 rtx temp;
2764
2765 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2766 return 0;
2767
2768 start_sequence ();
2769
2770 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2771 if (temp)
2772 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2773 true, OPTAB_DIRECT);
2774 if (temp)
2775 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2776 if (temp)
2777 temp = expand_binop (mode, sub_optab,
2778 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2779 temp, target,
2780 true, OPTAB_DIRECT);
2781 if (temp == 0)
2782 {
2783 end_sequence ();
2784 return 0;
2785 }
2786
2787 seq = get_insns ();
2788 end_sequence ();
2789
2790 add_equal_note (seq, temp, CTZ, op0, 0);
2791 emit_insn (seq);
2792 return temp;
2793 }
2794
2795
2796 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2797 else with the sequence used by expand_clz.
2798
2799 The ffs builtin promises to return zero for a zero value and ctz/clz
2800 may have an undefined value in that case. If they do not give us a
2801 convenient value, we have to generate a test and branch. */
2802 static rtx
2803 expand_ffs (machine_mode mode, rtx op0, rtx target)
2804 {
2805 HOST_WIDE_INT val = 0;
2806 bool defined_at_zero = false;
2807 rtx temp;
2808 rtx_insn *seq;
2809
2810 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2811 {
2812 start_sequence ();
2813
2814 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2815 if (!temp)
2816 goto fail;
2817
2818 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2819 }
2820 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2821 {
2822 start_sequence ();
2823 temp = expand_ctz (mode, op0, 0);
2824 if (!temp)
2825 goto fail;
2826
2827 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2828 {
2829 defined_at_zero = true;
2830 val = (GET_MODE_PRECISION (mode) - 1) - val;
2831 }
2832 }
2833 else
2834 return 0;
2835
2836 if (defined_at_zero && val == -1)
2837 /* No correction needed at zero. */;
2838 else
2839 {
2840 /* We don't try to do anything clever with the situation found
2841 on some processors (eg Alpha) where ctz(0:mode) ==
2842 bitsize(mode). If someone can think of a way to send N to -1
2843 and leave alone all values in the range 0..N-1 (where N is a
2844 power of two), cheaper than this test-and-branch, please add it.
2845
2846 The test-and-branch is done after the operation itself, in case
2847 the operation sets condition codes that can be recycled for this.
2848 (This is true on i386, for instance.) */
2849
2850 rtx_code_label *nonzero_label = gen_label_rtx ();
2851 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2852 mode, true, nonzero_label);
2853
2854 convert_move (temp, GEN_INT (-1), false);
2855 emit_label (nonzero_label);
2856 }
2857
2858 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2859 to produce a value in the range 0..bitsize. */
2860 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2861 target, false, OPTAB_DIRECT);
2862 if (!temp)
2863 goto fail;
2864
2865 seq = get_insns ();
2866 end_sequence ();
2867
2868 add_equal_note (seq, temp, FFS, op0, 0);
2869 emit_insn (seq);
2870 return temp;
2871
2872 fail:
2873 end_sequence ();
2874 return 0;
2875 }
2876
2877 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2878 conditions, VAL may already be a SUBREG against which we cannot generate
2879 a further SUBREG. In this case, we expect forcing the value into a
2880 register will work around the situation. */
2881
2882 static rtx
2883 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
2884 machine_mode imode)
2885 {
2886 rtx ret;
2887 ret = lowpart_subreg (omode, val, imode);
2888 if (ret == NULL)
2889 {
2890 val = force_reg (imode, val);
2891 ret = lowpart_subreg (omode, val, imode);
2892 gcc_assert (ret != NULL);
2893 }
2894 return ret;
2895 }
2896
2897 /* Expand a floating point absolute value or negation operation via a
2898 logical operation on the sign bit. */
2899
2900 static rtx
2901 expand_absneg_bit (enum rtx_code code, machine_mode mode,
2902 rtx op0, rtx target)
2903 {
2904 const struct real_format *fmt;
2905 int bitpos, word, nwords, i;
2906 machine_mode imode;
2907 rtx temp;
2908 rtx_insn *insns;
2909
2910 /* The format has to have a simple sign bit. */
2911 fmt = REAL_MODE_FORMAT (mode);
2912 if (fmt == NULL)
2913 return NULL_RTX;
2914
2915 bitpos = fmt->signbit_rw;
2916 if (bitpos < 0)
2917 return NULL_RTX;
2918
2919 /* Don't create negative zeros if the format doesn't support them. */
2920 if (code == NEG && !fmt->has_signed_zero)
2921 return NULL_RTX;
2922
2923 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2924 {
2925 imode = int_mode_for_mode (mode);
2926 if (imode == BLKmode)
2927 return NULL_RTX;
2928 word = 0;
2929 nwords = 1;
2930 }
2931 else
2932 {
2933 imode = word_mode;
2934
2935 if (FLOAT_WORDS_BIG_ENDIAN)
2936 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2937 else
2938 word = bitpos / BITS_PER_WORD;
2939 bitpos = bitpos % BITS_PER_WORD;
2940 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2941 }
2942
2943 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2944 if (code == ABS)
2945 mask = ~mask;
2946
2947 if (target == 0
2948 || target == op0
2949 || (nwords > 1 && !valid_multiword_target_p (target)))
2950 target = gen_reg_rtx (mode);
2951
2952 if (nwords > 1)
2953 {
2954 start_sequence ();
2955
2956 for (i = 0; i < nwords; ++i)
2957 {
2958 rtx targ_piece = operand_subword (target, i, 1, mode);
2959 rtx op0_piece = operand_subword_force (op0, i, mode);
2960
2961 if (i == word)
2962 {
2963 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2964 op0_piece,
2965 immed_wide_int_const (mask, imode),
2966 targ_piece, 1, OPTAB_LIB_WIDEN);
2967 if (temp != targ_piece)
2968 emit_move_insn (targ_piece, temp);
2969 }
2970 else
2971 emit_move_insn (targ_piece, op0_piece);
2972 }
2973
2974 insns = get_insns ();
2975 end_sequence ();
2976
2977 emit_insn (insns);
2978 }
2979 else
2980 {
2981 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2982 gen_lowpart (imode, op0),
2983 immed_wide_int_const (mask, imode),
2984 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2985 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2986
2987 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2988 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2989 target);
2990 }
2991
2992 return target;
2993 }
2994
2995 /* As expand_unop, but will fail rather than attempt the operation in a
2996 different mode or with a libcall. */
2997 static rtx
2998 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
2999 int unsignedp)
3000 {
3001 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
3002 {
3003 struct expand_operand ops[2];
3004 enum insn_code icode = optab_handler (unoptab, mode);
3005 rtx_insn *last = get_last_insn ();
3006 rtx pat;
3007
3008 create_output_operand (&ops[0], target, mode);
3009 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
3010 pat = maybe_gen_insn (icode, 2, ops);
3011 if (pat)
3012 {
3013 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
3014 && ! add_equal_note (as_a <rtx_insn *> (pat), ops[0].value,
3015 optab_to_code (unoptab),
3016 ops[1].value, NULL_RTX))
3017 {
3018 delete_insns_since (last);
3019 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3020 }
3021
3022 emit_insn (pat);
3023
3024 return ops[0].value;
3025 }
3026 }
3027 return 0;
3028 }
3029
3030 /* Generate code to perform an operation specified by UNOPTAB
3031 on operand OP0, with result having machine-mode MODE.
3032
3033 UNSIGNEDP is for the case where we have to widen the operands
3034 to perform the operation. It says to use zero-extension.
3035
3036 If TARGET is nonzero, the value
3037 is generated there, if it is convenient to do so.
3038 In all cases an rtx is returned for the locus of the value;
3039 this may or may not be TARGET. */
3040
3041 rtx
3042 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
3043 int unsignedp)
3044 {
3045 enum mode_class mclass = GET_MODE_CLASS (mode);
3046 machine_mode wider_mode;
3047 rtx temp;
3048 rtx libfunc;
3049
3050 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3051 if (temp)
3052 return temp;
3053
3054 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3055
3056 /* Widening (or narrowing) clz needs special treatment. */
3057 if (unoptab == clz_optab)
3058 {
3059 temp = widen_leading (mode, op0, target, unoptab);
3060 if (temp)
3061 return temp;
3062
3063 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3064 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3065 {
3066 temp = expand_doubleword_clz (mode, op0, target);
3067 if (temp)
3068 return temp;
3069 }
3070
3071 goto try_libcall;
3072 }
3073
3074 if (unoptab == clrsb_optab)
3075 {
3076 temp = widen_leading (mode, op0, target, unoptab);
3077 if (temp)
3078 return temp;
3079 goto try_libcall;
3080 }
3081
3082 /* Widening (or narrowing) bswap needs special treatment. */
3083 if (unoptab == bswap_optab)
3084 {
3085 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3086 or ROTATERT. First try these directly; if this fails, then try the
3087 obvious pair of shifts with allowed widening, as this will probably
3088 be always more efficient than the other fallback methods. */
3089 if (mode == HImode)
3090 {
3091 rtx_insn *last;
3092 rtx temp1, temp2;
3093
3094 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3095 {
3096 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3097 unsignedp, OPTAB_DIRECT);
3098 if (temp)
3099 return temp;
3100 }
3101
3102 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3103 {
3104 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3105 unsignedp, OPTAB_DIRECT);
3106 if (temp)
3107 return temp;
3108 }
3109
3110 last = get_last_insn ();
3111
3112 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3113 unsignedp, OPTAB_WIDEN);
3114 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3115 unsignedp, OPTAB_WIDEN);
3116 if (temp1 && temp2)
3117 {
3118 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3119 unsignedp, OPTAB_WIDEN);
3120 if (temp)
3121 return temp;
3122 }
3123
3124 delete_insns_since (last);
3125 }
3126
3127 temp = widen_bswap (mode, op0, target);
3128 if (temp)
3129 return temp;
3130
3131 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3132 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3133 {
3134 temp = expand_doubleword_bswap (mode, op0, target);
3135 if (temp)
3136 return temp;
3137 }
3138
3139 goto try_libcall;
3140 }
3141
3142 if (CLASS_HAS_WIDER_MODES_P (mclass))
3143 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3144 wider_mode != VOIDmode;
3145 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3146 {
3147 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3148 {
3149 rtx xop0 = op0;
3150 rtx_insn *last = get_last_insn ();
3151
3152 /* For certain operations, we need not actually extend
3153 the narrow operand, as long as we will truncate the
3154 results to the same narrowness. */
3155
3156 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3157 (unoptab == neg_optab
3158 || unoptab == one_cmpl_optab)
3159 && mclass == MODE_INT);
3160
3161 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3162 unsignedp);
3163
3164 if (temp)
3165 {
3166 if (mclass != MODE_INT
3167 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3168 {
3169 if (target == 0)
3170 target = gen_reg_rtx (mode);
3171 convert_move (target, temp, 0);
3172 return target;
3173 }
3174 else
3175 return gen_lowpart (mode, temp);
3176 }
3177 else
3178 delete_insns_since (last);
3179 }
3180 }
3181
3182 /* These can be done a word at a time. */
3183 if (unoptab == one_cmpl_optab
3184 && mclass == MODE_INT
3185 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3186 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3187 {
3188 int i;
3189 rtx_insn *insns;
3190
3191 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3192 target = gen_reg_rtx (mode);
3193
3194 start_sequence ();
3195
3196 /* Do the actual arithmetic. */
3197 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3198 {
3199 rtx target_piece = operand_subword (target, i, 1, mode);
3200 rtx x = expand_unop (word_mode, unoptab,
3201 operand_subword_force (op0, i, mode),
3202 target_piece, unsignedp);
3203
3204 if (target_piece != x)
3205 emit_move_insn (target_piece, x);
3206 }
3207
3208 insns = get_insns ();
3209 end_sequence ();
3210
3211 emit_insn (insns);
3212 return target;
3213 }
3214
3215 if (optab_to_code (unoptab) == NEG)
3216 {
3217 /* Try negating floating point values by flipping the sign bit. */
3218 if (SCALAR_FLOAT_MODE_P (mode))
3219 {
3220 temp = expand_absneg_bit (NEG, mode, op0, target);
3221 if (temp)
3222 return temp;
3223 }
3224
3225 /* If there is no negation pattern, and we have no negative zero,
3226 try subtracting from zero. */
3227 if (!HONOR_SIGNED_ZEROS (mode))
3228 {
3229 temp = expand_binop (mode, (unoptab == negv_optab
3230 ? subv_optab : sub_optab),
3231 CONST0_RTX (mode), op0, target,
3232 unsignedp, OPTAB_DIRECT);
3233 if (temp)
3234 return temp;
3235 }
3236 }
3237
3238 /* Try calculating parity (x) as popcount (x) % 2. */
3239 if (unoptab == parity_optab)
3240 {
3241 temp = expand_parity (mode, op0, target);
3242 if (temp)
3243 return temp;
3244 }
3245
3246 /* Try implementing ffs (x) in terms of clz (x). */
3247 if (unoptab == ffs_optab)
3248 {
3249 temp = expand_ffs (mode, op0, target);
3250 if (temp)
3251 return temp;
3252 }
3253
3254 /* Try implementing ctz (x) in terms of clz (x). */
3255 if (unoptab == ctz_optab)
3256 {
3257 temp = expand_ctz (mode, op0, target);
3258 if (temp)
3259 return temp;
3260 }
3261
3262 try_libcall:
3263 /* Now try a library call in this mode. */
3264 libfunc = optab_libfunc (unoptab, mode);
3265 if (libfunc)
3266 {
3267 rtx_insn *insns;
3268 rtx value;
3269 rtx eq_value;
3270 machine_mode outmode = mode;
3271
3272 /* All of these functions return small values. Thus we choose to
3273 have them return something that isn't a double-word. */
3274 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3275 || unoptab == clrsb_optab || unoptab == popcount_optab
3276 || unoptab == parity_optab)
3277 outmode
3278 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3279 optab_libfunc (unoptab, mode)));
3280
3281 start_sequence ();
3282
3283 /* Pass 1 for NO_QUEUE so we don't lose any increments
3284 if the libcall is cse'd or moved. */
3285 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3286 1, op0, mode);
3287 insns = get_insns ();
3288 end_sequence ();
3289
3290 target = gen_reg_rtx (outmode);
3291 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3292 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3293 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3294 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3295 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3296 emit_libcall_block_1 (insns, target, value, eq_value,
3297 trapv_unoptab_p (unoptab));
3298
3299 return target;
3300 }
3301
3302 /* It can't be done in this mode. Can we do it in a wider mode? */
3303
3304 if (CLASS_HAS_WIDER_MODES_P (mclass))
3305 {
3306 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3307 wider_mode != VOIDmode;
3308 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3309 {
3310 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3311 || optab_libfunc (unoptab, wider_mode))
3312 {
3313 rtx xop0 = op0;
3314 rtx_insn *last = get_last_insn ();
3315
3316 /* For certain operations, we need not actually extend
3317 the narrow operand, as long as we will truncate the
3318 results to the same narrowness. */
3319 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3320 (unoptab == neg_optab
3321 || unoptab == one_cmpl_optab
3322 || unoptab == bswap_optab)
3323 && mclass == MODE_INT);
3324
3325 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3326 unsignedp);
3327
3328 /* If we are generating clz using wider mode, adjust the
3329 result. Similarly for clrsb. */
3330 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3331 && temp != 0)
3332 temp = expand_binop
3333 (wider_mode, sub_optab, temp,
3334 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3335 - GET_MODE_PRECISION (mode),
3336 wider_mode),
3337 target, true, OPTAB_DIRECT);
3338
3339 /* Likewise for bswap. */
3340 if (unoptab == bswap_optab && temp != 0)
3341 {
3342 gcc_assert (GET_MODE_PRECISION (wider_mode)
3343 == GET_MODE_BITSIZE (wider_mode)
3344 && GET_MODE_PRECISION (mode)
3345 == GET_MODE_BITSIZE (mode));
3346
3347 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3348 GET_MODE_BITSIZE (wider_mode)
3349 - GET_MODE_BITSIZE (mode),
3350 NULL_RTX, true);
3351 }
3352
3353 if (temp)
3354 {
3355 if (mclass != MODE_INT)
3356 {
3357 if (target == 0)
3358 target = gen_reg_rtx (mode);
3359 convert_move (target, temp, 0);
3360 return target;
3361 }
3362 else
3363 return gen_lowpart (mode, temp);
3364 }
3365 else
3366 delete_insns_since (last);
3367 }
3368 }
3369 }
3370
3371 /* One final attempt at implementing negation via subtraction,
3372 this time allowing widening of the operand. */
3373 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3374 {
3375 rtx temp;
3376 temp = expand_binop (mode,
3377 unoptab == negv_optab ? subv_optab : sub_optab,
3378 CONST0_RTX (mode), op0,
3379 target, unsignedp, OPTAB_LIB_WIDEN);
3380 if (temp)
3381 return temp;
3382 }
3383
3384 return 0;
3385 }
3386 \f
3387 /* Emit code to compute the absolute value of OP0, with result to
3388 TARGET if convenient. (TARGET may be 0.) The return value says
3389 where the result actually is to be found.
3390
3391 MODE is the mode of the operand; the mode of the result is
3392 different but can be deduced from MODE.
3393
3394 */
3395
3396 rtx
3397 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3398 int result_unsignedp)
3399 {
3400 rtx temp;
3401
3402 if (GET_MODE_CLASS (mode) != MODE_INT
3403 || ! flag_trapv)
3404 result_unsignedp = 1;
3405
3406 /* First try to do it with a special abs instruction. */
3407 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3408 op0, target, 0);
3409 if (temp != 0)
3410 return temp;
3411
3412 /* For floating point modes, try clearing the sign bit. */
3413 if (SCALAR_FLOAT_MODE_P (mode))
3414 {
3415 temp = expand_absneg_bit (ABS, mode, op0, target);
3416 if (temp)
3417 return temp;
3418 }
3419
3420 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3421 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3422 && !HONOR_SIGNED_ZEROS (mode))
3423 {
3424 rtx_insn *last = get_last_insn ();
3425
3426 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3427 op0, NULL_RTX, 0);
3428 if (temp != 0)
3429 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3430 OPTAB_WIDEN);
3431
3432 if (temp != 0)
3433 return temp;
3434
3435 delete_insns_since (last);
3436 }
3437
3438 /* If this machine has expensive jumps, we can do integer absolute
3439 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3440 where W is the width of MODE. */
3441
3442 if (GET_MODE_CLASS (mode) == MODE_INT
3443 && BRANCH_COST (optimize_insn_for_speed_p (),
3444 false) >= 2)
3445 {
3446 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3447 GET_MODE_PRECISION (mode) - 1,
3448 NULL_RTX, 0);
3449
3450 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3451 OPTAB_LIB_WIDEN);
3452 if (temp != 0)
3453 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3454 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3455
3456 if (temp != 0)
3457 return temp;
3458 }
3459
3460 return NULL_RTX;
3461 }
3462
3463 rtx
3464 expand_abs (machine_mode mode, rtx op0, rtx target,
3465 int result_unsignedp, int safe)
3466 {
3467 rtx temp;
3468 rtx_code_label *op1;
3469
3470 if (GET_MODE_CLASS (mode) != MODE_INT
3471 || ! flag_trapv)
3472 result_unsignedp = 1;
3473
3474 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3475 if (temp != 0)
3476 return temp;
3477
3478 /* If that does not win, use conditional jump and negate. */
3479
3480 /* It is safe to use the target if it is the same
3481 as the source if this is also a pseudo register */
3482 if (op0 == target && REG_P (op0)
3483 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3484 safe = 1;
3485
3486 op1 = gen_label_rtx ();
3487 if (target == 0 || ! safe
3488 || GET_MODE (target) != mode
3489 || (MEM_P (target) && MEM_VOLATILE_P (target))
3490 || (REG_P (target)
3491 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3492 target = gen_reg_rtx (mode);
3493
3494 emit_move_insn (target, op0);
3495 NO_DEFER_POP;
3496
3497 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3498 NULL_RTX, NULL_RTX, op1, -1);
3499
3500 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3501 target, target, 0);
3502 if (op0 != target)
3503 emit_move_insn (target, op0);
3504 emit_label (op1);
3505 OK_DEFER_POP;
3506 return target;
3507 }
3508
3509 /* Emit code to compute the one's complement absolute value of OP0
3510 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3511 (TARGET may be NULL_RTX.) The return value says where the result
3512 actually is to be found.
3513
3514 MODE is the mode of the operand; the mode of the result is
3515 different but can be deduced from MODE. */
3516
3517 rtx
3518 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3519 {
3520 rtx temp;
3521
3522 /* Not applicable for floating point modes. */
3523 if (FLOAT_MODE_P (mode))
3524 return NULL_RTX;
3525
3526 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3527 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3528 {
3529 rtx_insn *last = get_last_insn ();
3530
3531 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3532 if (temp != 0)
3533 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3534 OPTAB_WIDEN);
3535
3536 if (temp != 0)
3537 return temp;
3538
3539 delete_insns_since (last);
3540 }
3541
3542 /* If this machine has expensive jumps, we can do one's complement
3543 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3544
3545 if (GET_MODE_CLASS (mode) == MODE_INT
3546 && BRANCH_COST (optimize_insn_for_speed_p (),
3547 false) >= 2)
3548 {
3549 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3550 GET_MODE_PRECISION (mode) - 1,
3551 NULL_RTX, 0);
3552
3553 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3554 OPTAB_LIB_WIDEN);
3555
3556 if (temp != 0)
3557 return temp;
3558 }
3559
3560 return NULL_RTX;
3561 }
3562
3563 /* A subroutine of expand_copysign, perform the copysign operation using the
3564 abs and neg primitives advertised to exist on the target. The assumption
3565 is that we have a split register file, and leaving op0 in fp registers,
3566 and not playing with subregs so much, will help the register allocator. */
3567
3568 static rtx
3569 expand_copysign_absneg (machine_mode mode, rtx op0, rtx op1, rtx target,
3570 int bitpos, bool op0_is_abs)
3571 {
3572 machine_mode imode;
3573 enum insn_code icode;
3574 rtx sign;
3575 rtx_code_label *label;
3576
3577 if (target == op1)
3578 target = NULL_RTX;
3579
3580 /* Check if the back end provides an insn that handles signbit for the
3581 argument's mode. */
3582 icode = optab_handler (signbit_optab, mode);
3583 if (icode != CODE_FOR_nothing)
3584 {
3585 imode = insn_data[(int) icode].operand[0].mode;
3586 sign = gen_reg_rtx (imode);
3587 emit_unop_insn (icode, sign, op1, UNKNOWN);
3588 }
3589 else
3590 {
3591 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3592 {
3593 imode = int_mode_for_mode (mode);
3594 if (imode == BLKmode)
3595 return NULL_RTX;
3596 op1 = gen_lowpart (imode, op1);
3597 }
3598 else
3599 {
3600 int word;
3601
3602 imode = word_mode;
3603 if (FLOAT_WORDS_BIG_ENDIAN)
3604 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3605 else
3606 word = bitpos / BITS_PER_WORD;
3607 bitpos = bitpos % BITS_PER_WORD;
3608 op1 = operand_subword_force (op1, word, mode);
3609 }
3610
3611 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3612 sign = expand_binop (imode, and_optab, op1,
3613 immed_wide_int_const (mask, imode),
3614 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3615 }
3616
3617 if (!op0_is_abs)
3618 {
3619 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3620 if (op0 == NULL)
3621 return NULL_RTX;
3622 target = op0;
3623 }
3624 else
3625 {
3626 if (target == NULL_RTX)
3627 target = copy_to_reg (op0);
3628 else
3629 emit_move_insn (target, op0);
3630 }
3631
3632 label = gen_label_rtx ();
3633 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3634
3635 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3636 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3637 else
3638 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3639 if (op0 != target)
3640 emit_move_insn (target, op0);
3641
3642 emit_label (label);
3643
3644 return target;
3645 }
3646
3647
3648 /* A subroutine of expand_copysign, perform the entire copysign operation
3649 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3650 is true if op0 is known to have its sign bit clear. */
3651
3652 static rtx
3653 expand_copysign_bit (machine_mode mode, rtx op0, rtx op1, rtx target,
3654 int bitpos, bool op0_is_abs)
3655 {
3656 machine_mode imode;
3657 int word, nwords, i;
3658 rtx temp;
3659 rtx_insn *insns;
3660
3661 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3662 {
3663 imode = int_mode_for_mode (mode);
3664 if (imode == BLKmode)
3665 return NULL_RTX;
3666 word = 0;
3667 nwords = 1;
3668 }
3669 else
3670 {
3671 imode = word_mode;
3672
3673 if (FLOAT_WORDS_BIG_ENDIAN)
3674 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3675 else
3676 word = bitpos / BITS_PER_WORD;
3677 bitpos = bitpos % BITS_PER_WORD;
3678 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3679 }
3680
3681 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3682
3683 if (target == 0
3684 || target == op0
3685 || target == op1
3686 || (nwords > 1 && !valid_multiword_target_p (target)))
3687 target = gen_reg_rtx (mode);
3688
3689 if (nwords > 1)
3690 {
3691 start_sequence ();
3692
3693 for (i = 0; i < nwords; ++i)
3694 {
3695 rtx targ_piece = operand_subword (target, i, 1, mode);
3696 rtx op0_piece = operand_subword_force (op0, i, mode);
3697
3698 if (i == word)
3699 {
3700 if (!op0_is_abs)
3701 op0_piece
3702 = expand_binop (imode, and_optab, op0_piece,
3703 immed_wide_int_const (~mask, imode),
3704 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3705 op1 = expand_binop (imode, and_optab,
3706 operand_subword_force (op1, i, mode),
3707 immed_wide_int_const (mask, imode),
3708 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3709
3710 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3711 targ_piece, 1, OPTAB_LIB_WIDEN);
3712 if (temp != targ_piece)
3713 emit_move_insn (targ_piece, temp);
3714 }
3715 else
3716 emit_move_insn (targ_piece, op0_piece);
3717 }
3718
3719 insns = get_insns ();
3720 end_sequence ();
3721
3722 emit_insn (insns);
3723 }
3724 else
3725 {
3726 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3727 immed_wide_int_const (mask, imode),
3728 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3729
3730 op0 = gen_lowpart (imode, op0);
3731 if (!op0_is_abs)
3732 op0 = expand_binop (imode, and_optab, op0,
3733 immed_wide_int_const (~mask, imode),
3734 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3735
3736 temp = expand_binop (imode, ior_optab, op0, op1,
3737 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3738 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3739 }
3740
3741 return target;
3742 }
3743
3744 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3745 scalar floating point mode. Return NULL if we do not know how to
3746 expand the operation inline. */
3747
3748 rtx
3749 expand_copysign (rtx op0, rtx op1, rtx target)
3750 {
3751 machine_mode mode = GET_MODE (op0);
3752 const struct real_format *fmt;
3753 bool op0_is_abs;
3754 rtx temp;
3755
3756 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3757 gcc_assert (GET_MODE (op1) == mode);
3758
3759 /* First try to do it with a special instruction. */
3760 temp = expand_binop (mode, copysign_optab, op0, op1,
3761 target, 0, OPTAB_DIRECT);
3762 if (temp)
3763 return temp;
3764
3765 fmt = REAL_MODE_FORMAT (mode);
3766 if (fmt == NULL || !fmt->has_signed_zero)
3767 return NULL_RTX;
3768
3769 op0_is_abs = false;
3770 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3771 {
3772 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3773 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3774 op0_is_abs = true;
3775 }
3776
3777 if (fmt->signbit_ro >= 0
3778 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3779 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3780 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3781 {
3782 temp = expand_copysign_absneg (mode, op0, op1, target,
3783 fmt->signbit_ro, op0_is_abs);
3784 if (temp)
3785 return temp;
3786 }
3787
3788 if (fmt->signbit_rw < 0)
3789 return NULL_RTX;
3790 return expand_copysign_bit (mode, op0, op1, target,
3791 fmt->signbit_rw, op0_is_abs);
3792 }
3793 \f
3794 /* Generate an instruction whose insn-code is INSN_CODE,
3795 with two operands: an output TARGET and an input OP0.
3796 TARGET *must* be nonzero, and the output is always stored there.
3797 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3798 the value that is stored into TARGET.
3799
3800 Return false if expansion failed. */
3801
3802 bool
3803 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3804 enum rtx_code code)
3805 {
3806 struct expand_operand ops[2];
3807 rtx pat;
3808
3809 create_output_operand (&ops[0], target, GET_MODE (target));
3810 create_input_operand (&ops[1], op0, GET_MODE (op0));
3811 pat = maybe_gen_insn (icode, 2, ops);
3812 if (!pat)
3813 return false;
3814
3815 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
3816 && code != UNKNOWN)
3817 add_equal_note (as_a <rtx_insn *> (pat), ops[0].value, code, ops[1].value,
3818 NULL_RTX);
3819
3820 emit_insn (pat);
3821
3822 if (ops[0].value != target)
3823 emit_move_insn (target, ops[0].value);
3824 return true;
3825 }
3826 /* Generate an instruction whose insn-code is INSN_CODE,
3827 with two operands: an output TARGET and an input OP0.
3828 TARGET *must* be nonzero, and the output is always stored there.
3829 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3830 the value that is stored into TARGET. */
3831
3832 void
3833 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3834 {
3835 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3836 gcc_assert (ok);
3837 }
3838 \f
3839 struct no_conflict_data
3840 {
3841 rtx target;
3842 rtx_insn *first, *insn;
3843 bool must_stay;
3844 };
3845
3846 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3847 the currently examined clobber / store has to stay in the list of
3848 insns that constitute the actual libcall block. */
3849 static void
3850 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3851 {
3852 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3853
3854 /* If this inns directly contributes to setting the target, it must stay. */
3855 if (reg_overlap_mentioned_p (p->target, dest))
3856 p->must_stay = true;
3857 /* If we haven't committed to keeping any other insns in the list yet,
3858 there is nothing more to check. */
3859 else if (p->insn == p->first)
3860 return;
3861 /* If this insn sets / clobbers a register that feeds one of the insns
3862 already in the list, this insn has to stay too. */
3863 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3864 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3865 || reg_used_between_p (dest, p->first, p->insn)
3866 /* Likewise if this insn depends on a register set by a previous
3867 insn in the list, or if it sets a result (presumably a hard
3868 register) that is set or clobbered by a previous insn.
3869 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3870 SET_DEST perform the former check on the address, and the latter
3871 check on the MEM. */
3872 || (GET_CODE (set) == SET
3873 && (modified_in_p (SET_SRC (set), p->first)
3874 || modified_in_p (SET_DEST (set), p->first)
3875 || modified_between_p (SET_SRC (set), p->first, p->insn)
3876 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3877 p->must_stay = true;
3878 }
3879
3880 \f
3881 /* Emit code to make a call to a constant function or a library call.
3882
3883 INSNS is a list containing all insns emitted in the call.
3884 These insns leave the result in RESULT. Our block is to copy RESULT
3885 to TARGET, which is logically equivalent to EQUIV.
3886
3887 We first emit any insns that set a pseudo on the assumption that these are
3888 loading constants into registers; doing so allows them to be safely cse'ed
3889 between blocks. Then we emit all the other insns in the block, followed by
3890 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3891 note with an operand of EQUIV. */
3892
3893 static void
3894 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3895 bool equiv_may_trap)
3896 {
3897 rtx final_dest = target;
3898 rtx_insn *next, *last, *insn;
3899
3900 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3901 into a MEM later. Protect the libcall block from this change. */
3902 if (! REG_P (target) || REG_USERVAR_P (target))
3903 target = gen_reg_rtx (GET_MODE (target));
3904
3905 /* If we're using non-call exceptions, a libcall corresponding to an
3906 operation that may trap may also trap. */
3907 /* ??? See the comment in front of make_reg_eh_region_note. */
3908 if (cfun->can_throw_non_call_exceptions
3909 && (equiv_may_trap || may_trap_p (equiv)))
3910 {
3911 for (insn = insns; insn; insn = NEXT_INSN (insn))
3912 if (CALL_P (insn))
3913 {
3914 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3915 if (note)
3916 {
3917 int lp_nr = INTVAL (XEXP (note, 0));
3918 if (lp_nr == 0 || lp_nr == INT_MIN)
3919 remove_note (insn, note);
3920 }
3921 }
3922 }
3923 else
3924 {
3925 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3926 reg note to indicate that this call cannot throw or execute a nonlocal
3927 goto (unless there is already a REG_EH_REGION note, in which case
3928 we update it). */
3929 for (insn = insns; insn; insn = NEXT_INSN (insn))
3930 if (CALL_P (insn))
3931 make_reg_eh_region_note_nothrow_nononlocal (insn);
3932 }
3933
3934 /* First emit all insns that set pseudos. Remove them from the list as
3935 we go. Avoid insns that set pseudos which were referenced in previous
3936 insns. These can be generated by move_by_pieces, for example,
3937 to update an address. Similarly, avoid insns that reference things
3938 set in previous insns. */
3939
3940 for (insn = insns; insn; insn = next)
3941 {
3942 rtx set = single_set (insn);
3943
3944 next = NEXT_INSN (insn);
3945
3946 if (set != 0 && REG_P (SET_DEST (set))
3947 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3948 {
3949 struct no_conflict_data data;
3950
3951 data.target = const0_rtx;
3952 data.first = insns;
3953 data.insn = insn;
3954 data.must_stay = 0;
3955 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3956 if (! data.must_stay)
3957 {
3958 if (PREV_INSN (insn))
3959 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3960 else
3961 insns = next;
3962
3963 if (next)
3964 SET_PREV_INSN (next) = PREV_INSN (insn);
3965
3966 add_insn (insn);
3967 }
3968 }
3969
3970 /* Some ports use a loop to copy large arguments onto the stack.
3971 Don't move anything outside such a loop. */
3972 if (LABEL_P (insn))
3973 break;
3974 }
3975
3976 /* Write the remaining insns followed by the final copy. */
3977 for (insn = insns; insn; insn = next)
3978 {
3979 next = NEXT_INSN (insn);
3980
3981 add_insn (insn);
3982 }
3983
3984 last = emit_move_insn (target, result);
3985 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3986
3987 if (final_dest != target)
3988 emit_move_insn (final_dest, target);
3989 }
3990
3991 void
3992 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3993 {
3994 emit_libcall_block_1 (safe_as_a <rtx_insn *> (insns),
3995 target, result, equiv, false);
3996 }
3997 \f
3998 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3999 PURPOSE describes how this comparison will be used. CODE is the rtx
4000 comparison code we will be using.
4001
4002 ??? Actually, CODE is slightly weaker than that. A target is still
4003 required to implement all of the normal bcc operations, but not
4004 required to implement all (or any) of the unordered bcc operations. */
4005
4006 int
4007 can_compare_p (enum rtx_code code, machine_mode mode,
4008 enum can_compare_purpose purpose)
4009 {
4010 rtx test;
4011 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
4012 do
4013 {
4014 enum insn_code icode;
4015
4016 if (purpose == ccp_jump
4017 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4018 && insn_operand_matches (icode, 0, test))
4019 return 1;
4020 if (purpose == ccp_store_flag
4021 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4022 && insn_operand_matches (icode, 1, test))
4023 return 1;
4024 if (purpose == ccp_cmov
4025 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4026 return 1;
4027
4028 mode = GET_MODE_WIDER_MODE (mode);
4029 PUT_MODE (test, mode);
4030 }
4031 while (mode != VOIDmode);
4032
4033 return 0;
4034 }
4035
4036 /* This function is called when we are going to emit a compare instruction that
4037 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
4038
4039 *PMODE is the mode of the inputs (in case they are const_int).
4040 *PUNSIGNEDP nonzero says that the operands are unsigned;
4041 this matters if they need to be widened (as given by METHODS).
4042
4043 If they have mode BLKmode, then SIZE specifies the size of both operands.
4044
4045 This function performs all the setup necessary so that the caller only has
4046 to emit a single comparison insn. This setup can involve doing a BLKmode
4047 comparison or emitting a library call to perform the comparison if no insn
4048 is available to handle it.
4049 The values which are passed in through pointers can be modified; the caller
4050 should perform the comparison on the modified values. Constant
4051 comparisons must have already been folded. */
4052
4053 static void
4054 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4055 int unsignedp, enum optab_methods methods,
4056 rtx *ptest, machine_mode *pmode)
4057 {
4058 machine_mode mode = *pmode;
4059 rtx libfunc, test;
4060 machine_mode cmp_mode;
4061 enum mode_class mclass;
4062
4063 /* The other methods are not needed. */
4064 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4065 || methods == OPTAB_LIB_WIDEN);
4066
4067 /* If we are optimizing, force expensive constants into a register. */
4068 if (CONSTANT_P (x) && optimize
4069 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4070 > COSTS_N_INSNS (1)))
4071 x = force_reg (mode, x);
4072
4073 if (CONSTANT_P (y) && optimize
4074 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4075 > COSTS_N_INSNS (1)))
4076 y = force_reg (mode, y);
4077
4078 #if HAVE_cc0
4079 /* Make sure if we have a canonical comparison. The RTL
4080 documentation states that canonical comparisons are required only
4081 for targets which have cc0. */
4082 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4083 #endif
4084
4085 /* Don't let both operands fail to indicate the mode. */
4086 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4087 x = force_reg (mode, x);
4088 if (mode == VOIDmode)
4089 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4090
4091 /* Handle all BLKmode compares. */
4092
4093 if (mode == BLKmode)
4094 {
4095 machine_mode result_mode;
4096 enum insn_code cmp_code;
4097 tree length_type;
4098 rtx libfunc;
4099 rtx result;
4100 rtx opalign
4101 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4102
4103 gcc_assert (size);
4104
4105 /* Try to use a memory block compare insn - either cmpstr
4106 or cmpmem will do. */
4107 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4108 cmp_mode != VOIDmode;
4109 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4110 {
4111 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4112 if (cmp_code == CODE_FOR_nothing)
4113 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4114 if (cmp_code == CODE_FOR_nothing)
4115 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4116 if (cmp_code == CODE_FOR_nothing)
4117 continue;
4118
4119 /* Must make sure the size fits the insn's mode. */
4120 if ((CONST_INT_P (size)
4121 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4122 || (GET_MODE_BITSIZE (GET_MODE (size))
4123 > GET_MODE_BITSIZE (cmp_mode)))
4124 continue;
4125
4126 result_mode = insn_data[cmp_code].operand[0].mode;
4127 result = gen_reg_rtx (result_mode);
4128 size = convert_to_mode (cmp_mode, size, 1);
4129 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4130
4131 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4132 *pmode = result_mode;
4133 return;
4134 }
4135
4136 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4137 goto fail;
4138
4139 /* Otherwise call a library function, memcmp. */
4140 libfunc = memcmp_libfunc;
4141 length_type = sizetype;
4142 result_mode = TYPE_MODE (integer_type_node);
4143 cmp_mode = TYPE_MODE (length_type);
4144 size = convert_to_mode (TYPE_MODE (length_type), size,
4145 TYPE_UNSIGNED (length_type));
4146
4147 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4148 result_mode, 3,
4149 XEXP (x, 0), Pmode,
4150 XEXP (y, 0), Pmode,
4151 size, cmp_mode);
4152 x = result;
4153 y = const0_rtx;
4154 mode = result_mode;
4155 methods = OPTAB_LIB_WIDEN;
4156 unsignedp = false;
4157 }
4158
4159 /* Don't allow operands to the compare to trap, as that can put the
4160 compare and branch in different basic blocks. */
4161 if (cfun->can_throw_non_call_exceptions)
4162 {
4163 if (may_trap_p (x))
4164 x = force_reg (mode, x);
4165 if (may_trap_p (y))
4166 y = force_reg (mode, y);
4167 }
4168
4169 if (GET_MODE_CLASS (mode) == MODE_CC)
4170 {
4171 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
4172 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4173 gcc_assert (icode != CODE_FOR_nothing
4174 && insn_operand_matches (icode, 0, test));
4175 *ptest = test;
4176 return;
4177 }
4178
4179 mclass = GET_MODE_CLASS (mode);
4180 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4181 cmp_mode = mode;
4182 do
4183 {
4184 enum insn_code icode;
4185 icode = optab_handler (cbranch_optab, cmp_mode);
4186 if (icode != CODE_FOR_nothing
4187 && insn_operand_matches (icode, 0, test))
4188 {
4189 rtx_insn *last = get_last_insn ();
4190 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4191 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4192 if (op0 && op1
4193 && insn_operand_matches (icode, 1, op0)
4194 && insn_operand_matches (icode, 2, op1))
4195 {
4196 XEXP (test, 0) = op0;
4197 XEXP (test, 1) = op1;
4198 *ptest = test;
4199 *pmode = cmp_mode;
4200 return;
4201 }
4202 delete_insns_since (last);
4203 }
4204
4205 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4206 break;
4207 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4208 }
4209 while (cmp_mode != VOIDmode);
4210
4211 if (methods != OPTAB_LIB_WIDEN)
4212 goto fail;
4213
4214 if (!SCALAR_FLOAT_MODE_P (mode))
4215 {
4216 rtx result;
4217 machine_mode ret_mode;
4218
4219 /* Handle a libcall just for the mode we are using. */
4220 libfunc = optab_libfunc (cmp_optab, mode);
4221 gcc_assert (libfunc);
4222
4223 /* If we want unsigned, and this mode has a distinct unsigned
4224 comparison routine, use that. */
4225 if (unsignedp)
4226 {
4227 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4228 if (ulibfunc)
4229 libfunc = ulibfunc;
4230 }
4231
4232 ret_mode = targetm.libgcc_cmp_return_mode ();
4233 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4234 ret_mode, 2, x, mode, y, mode);
4235
4236 /* There are two kinds of comparison routines. Biased routines
4237 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4238 of gcc expect that the comparison operation is equivalent
4239 to the modified comparison. For signed comparisons compare the
4240 result against 1 in the biased case, and zero in the unbiased
4241 case. For unsigned comparisons always compare against 1 after
4242 biasing the unbiased result by adding 1. This gives us a way to
4243 represent LTU.
4244 The comparisons in the fixed-point helper library are always
4245 biased. */
4246 x = result;
4247 y = const1_rtx;
4248
4249 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4250 {
4251 if (unsignedp)
4252 x = plus_constant (ret_mode, result, 1);
4253 else
4254 y = const0_rtx;
4255 }
4256
4257 *pmode = ret_mode;
4258 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4259 ptest, pmode);
4260 }
4261 else
4262 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4263
4264 return;
4265
4266 fail:
4267 *ptest = NULL_RTX;
4268 }
4269
4270 /* Before emitting an insn with code ICODE, make sure that X, which is going
4271 to be used for operand OPNUM of the insn, is converted from mode MODE to
4272 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4273 that it is accepted by the operand predicate. Return the new value. */
4274
4275 rtx
4276 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
4277 machine_mode wider_mode, int unsignedp)
4278 {
4279 if (mode != wider_mode)
4280 x = convert_modes (wider_mode, mode, x, unsignedp);
4281
4282 if (!insn_operand_matches (icode, opnum, x))
4283 {
4284 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
4285 if (reload_completed)
4286 return NULL_RTX;
4287 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
4288 return NULL_RTX;
4289 x = copy_to_mode_reg (op_mode, x);
4290 }
4291
4292 return x;
4293 }
4294
4295 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4296 we can do the branch. */
4297
4298 static void
4299 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label, int prob)
4300 {
4301 machine_mode optab_mode;
4302 enum mode_class mclass;
4303 enum insn_code icode;
4304 rtx_insn *insn;
4305
4306 mclass = GET_MODE_CLASS (mode);
4307 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4308 icode = optab_handler (cbranch_optab, optab_mode);
4309
4310 gcc_assert (icode != CODE_FOR_nothing);
4311 gcc_assert (insn_operand_matches (icode, 0, test));
4312 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4313 XEXP (test, 1), label));
4314 if (prob != -1
4315 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4316 && insn
4317 && JUMP_P (insn)
4318 && any_condjump_p (insn)
4319 && !find_reg_note (insn, REG_BR_PROB, 0))
4320 add_int_reg_note (insn, REG_BR_PROB, prob);
4321 }
4322
4323 /* Generate code to compare X with Y so that the condition codes are
4324 set and to jump to LABEL if the condition is true. If X is a
4325 constant and Y is not a constant, then the comparison is swapped to
4326 ensure that the comparison RTL has the canonical form.
4327
4328 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4329 need to be widened. UNSIGNEDP is also used to select the proper
4330 branch condition code.
4331
4332 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4333
4334 MODE is the mode of the inputs (in case they are const_int).
4335
4336 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4337 It will be potentially converted into an unsigned variant based on
4338 UNSIGNEDP to select a proper jump instruction.
4339
4340 PROB is the probability of jumping to LABEL. */
4341
4342 void
4343 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4344 machine_mode mode, int unsignedp, rtx label,
4345 int prob)
4346 {
4347 rtx op0 = x, op1 = y;
4348 rtx test;
4349
4350 /* Swap operands and condition to ensure canonical RTL. */
4351 if (swap_commutative_operands_p (x, y)
4352 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4353 {
4354 op0 = y, op1 = x;
4355 comparison = swap_condition (comparison);
4356 }
4357
4358 /* If OP0 is still a constant, then both X and Y must be constants
4359 or the opposite comparison is not supported. Force X into a register
4360 to create canonical RTL. */
4361 if (CONSTANT_P (op0))
4362 op0 = force_reg (mode, op0);
4363
4364 if (unsignedp)
4365 comparison = unsigned_condition (comparison);
4366
4367 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4368 &test, &mode);
4369 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4370 }
4371
4372 \f
4373 /* Emit a library call comparison between floating point X and Y.
4374 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4375
4376 static void
4377 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4378 rtx *ptest, machine_mode *pmode)
4379 {
4380 enum rtx_code swapped = swap_condition (comparison);
4381 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4382 machine_mode orig_mode = GET_MODE (x);
4383 machine_mode mode, cmp_mode;
4384 rtx true_rtx, false_rtx;
4385 rtx value, target, equiv;
4386 rtx_insn *insns;
4387 rtx libfunc = 0;
4388 bool reversed_p = false;
4389 cmp_mode = targetm.libgcc_cmp_return_mode ();
4390
4391 for (mode = orig_mode;
4392 mode != VOIDmode;
4393 mode = GET_MODE_WIDER_MODE (mode))
4394 {
4395 if (code_to_optab (comparison)
4396 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4397 break;
4398
4399 if (code_to_optab (swapped)
4400 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4401 {
4402 rtx tmp;
4403 tmp = x; x = y; y = tmp;
4404 comparison = swapped;
4405 break;
4406 }
4407
4408 if (code_to_optab (reversed)
4409 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4410 {
4411 comparison = reversed;
4412 reversed_p = true;
4413 break;
4414 }
4415 }
4416
4417 gcc_assert (mode != VOIDmode);
4418
4419 if (mode != orig_mode)
4420 {
4421 x = convert_to_mode (mode, x, 0);
4422 y = convert_to_mode (mode, y, 0);
4423 }
4424
4425 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4426 the RTL. The allows the RTL optimizers to delete the libcall if the
4427 condition can be determined at compile-time. */
4428 if (comparison == UNORDERED
4429 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4430 {
4431 true_rtx = const_true_rtx;
4432 false_rtx = const0_rtx;
4433 }
4434 else
4435 {
4436 switch (comparison)
4437 {
4438 case EQ:
4439 true_rtx = const0_rtx;
4440 false_rtx = const_true_rtx;
4441 break;
4442
4443 case NE:
4444 true_rtx = const_true_rtx;
4445 false_rtx = const0_rtx;
4446 break;
4447
4448 case GT:
4449 true_rtx = const1_rtx;
4450 false_rtx = const0_rtx;
4451 break;
4452
4453 case GE:
4454 true_rtx = const0_rtx;
4455 false_rtx = constm1_rtx;
4456 break;
4457
4458 case LT:
4459 true_rtx = constm1_rtx;
4460 false_rtx = const0_rtx;
4461 break;
4462
4463 case LE:
4464 true_rtx = const0_rtx;
4465 false_rtx = const1_rtx;
4466 break;
4467
4468 default:
4469 gcc_unreachable ();
4470 }
4471 }
4472
4473 if (comparison == UNORDERED)
4474 {
4475 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4476 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4477 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4478 temp, const_true_rtx, equiv);
4479 }
4480 else
4481 {
4482 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4483 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4484 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4485 equiv, true_rtx, false_rtx);
4486 }
4487
4488 start_sequence ();
4489 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4490 cmp_mode, 2, x, mode, y, mode);
4491 insns = get_insns ();
4492 end_sequence ();
4493
4494 target = gen_reg_rtx (cmp_mode);
4495 emit_libcall_block (insns, target, value, equiv);
4496
4497 if (comparison == UNORDERED
4498 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4499 || reversed_p)
4500 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4501 else
4502 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4503
4504 *pmode = cmp_mode;
4505 }
4506 \f
4507 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4508
4509 void
4510 emit_indirect_jump (rtx loc ATTRIBUTE_UNUSED)
4511 {
4512 #ifndef HAVE_indirect_jump
4513 sorry ("indirect jumps are not available on this target");
4514 #else
4515 struct expand_operand ops[1];
4516 create_address_operand (&ops[0], loc);
4517 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4518 emit_barrier ();
4519 #endif
4520 }
4521 \f
4522 #ifdef HAVE_conditional_move
4523
4524 /* Emit a conditional move instruction if the machine supports one for that
4525 condition and machine mode.
4526
4527 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4528 the mode to use should they be constants. If it is VOIDmode, they cannot
4529 both be constants.
4530
4531 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4532 should be stored there. MODE is the mode to use should they be constants.
4533 If it is VOIDmode, they cannot both be constants.
4534
4535 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4536 is not supported. */
4537
4538 rtx
4539 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4540 machine_mode cmode, rtx op2, rtx op3,
4541 machine_mode mode, int unsignedp)
4542 {
4543 rtx comparison;
4544 rtx_insn *last;
4545 enum insn_code icode;
4546 enum rtx_code reversed;
4547
4548 /* If one operand is constant, make it the second one. Only do this
4549 if the other operand is not constant as well. */
4550
4551 if (swap_commutative_operands_p (op0, op1))
4552 {
4553 std::swap (op0, op1);
4554 code = swap_condition (code);
4555 }
4556
4557 /* get_condition will prefer to generate LT and GT even if the old
4558 comparison was against zero, so undo that canonicalization here since
4559 comparisons against zero are cheaper. */
4560 if (code == LT && op1 == const1_rtx)
4561 code = LE, op1 = const0_rtx;
4562 else if (code == GT && op1 == constm1_rtx)
4563 code = GE, op1 = const0_rtx;
4564
4565 if (cmode == VOIDmode)
4566 cmode = GET_MODE (op0);
4567
4568 if (swap_commutative_operands_p (op2, op3)
4569 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4570 != UNKNOWN))
4571 {
4572 std::swap (op2, op3);
4573 code = reversed;
4574 }
4575
4576 if (mode == VOIDmode)
4577 mode = GET_MODE (op2);
4578
4579 icode = direct_optab_handler (movcc_optab, mode);
4580
4581 if (icode == CODE_FOR_nothing)
4582 return 0;
4583
4584 if (!target)
4585 target = gen_reg_rtx (mode);
4586
4587 code = unsignedp ? unsigned_condition (code) : code;
4588 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4589
4590 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4591 return NULL and let the caller figure out how best to deal with this
4592 situation. */
4593 if (!COMPARISON_P (comparison))
4594 return NULL_RTX;
4595
4596 saved_pending_stack_adjust save;
4597 save_pending_stack_adjust (&save);
4598 last = get_last_insn ();
4599 do_pending_stack_adjust ();
4600 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4601 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4602 &comparison, &cmode);
4603 if (comparison)
4604 {
4605 struct expand_operand ops[4];
4606
4607 create_output_operand (&ops[0], target, mode);
4608 create_fixed_operand (&ops[1], comparison);
4609 create_input_operand (&ops[2], op2, mode);
4610 create_input_operand (&ops[3], op3, mode);
4611 if (maybe_expand_insn (icode, 4, ops))
4612 {
4613 if (ops[0].value != target)
4614 convert_move (target, ops[0].value, false);
4615 return target;
4616 }
4617 }
4618 delete_insns_since (last);
4619 restore_pending_stack_adjust (&save);
4620 return NULL_RTX;
4621 }
4622
4623 /* Return nonzero if a conditional move of mode MODE is supported.
4624
4625 This function is for combine so it can tell whether an insn that looks
4626 like a conditional move is actually supported by the hardware. If we
4627 guess wrong we lose a bit on optimization, but that's it. */
4628 /* ??? sparc64 supports conditionally moving integers values based on fp
4629 comparisons, and vice versa. How do we handle them? */
4630
4631 int
4632 can_conditionally_move_p (machine_mode mode)
4633 {
4634 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4635 return 1;
4636
4637 return 0;
4638 }
4639
4640 #endif /* HAVE_conditional_move */
4641
4642 /* Emit a conditional addition instruction if the machine supports one for that
4643 condition and machine mode.
4644
4645 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4646 the mode to use should they be constants. If it is VOIDmode, they cannot
4647 both be constants.
4648
4649 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4650 should be stored there. MODE is the mode to use should they be constants.
4651 If it is VOIDmode, they cannot both be constants.
4652
4653 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4654 is not supported. */
4655
4656 rtx
4657 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4658 machine_mode cmode, rtx op2, rtx op3,
4659 machine_mode mode, int unsignedp)
4660 {
4661 rtx comparison;
4662 rtx_insn *last;
4663 enum insn_code icode;
4664
4665 /* If one operand is constant, make it the second one. Only do this
4666 if the other operand is not constant as well. */
4667
4668 if (swap_commutative_operands_p (op0, op1))
4669 {
4670 std::swap (op0, op1);
4671 code = swap_condition (code);
4672 }
4673
4674 /* get_condition will prefer to generate LT and GT even if the old
4675 comparison was against zero, so undo that canonicalization here since
4676 comparisons against zero are cheaper. */
4677 if (code == LT && op1 == const1_rtx)
4678 code = LE, op1 = const0_rtx;
4679 else if (code == GT && op1 == constm1_rtx)
4680 code = GE, op1 = const0_rtx;
4681
4682 if (cmode == VOIDmode)
4683 cmode = GET_MODE (op0);
4684
4685 if (mode == VOIDmode)
4686 mode = GET_MODE (op2);
4687
4688 icode = optab_handler (addcc_optab, mode);
4689
4690 if (icode == CODE_FOR_nothing)
4691 return 0;
4692
4693 if (!target)
4694 target = gen_reg_rtx (mode);
4695
4696 code = unsignedp ? unsigned_condition (code) : code;
4697 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4698
4699 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4700 return NULL and let the caller figure out how best to deal with this
4701 situation. */
4702 if (!COMPARISON_P (comparison))
4703 return NULL_RTX;
4704
4705 do_pending_stack_adjust ();
4706 last = get_last_insn ();
4707 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4708 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4709 &comparison, &cmode);
4710 if (comparison)
4711 {
4712 struct expand_operand ops[4];
4713
4714 create_output_operand (&ops[0], target, mode);
4715 create_fixed_operand (&ops[1], comparison);
4716 create_input_operand (&ops[2], op2, mode);
4717 create_input_operand (&ops[3], op3, mode);
4718 if (maybe_expand_insn (icode, 4, ops))
4719 {
4720 if (ops[0].value != target)
4721 convert_move (target, ops[0].value, false);
4722 return target;
4723 }
4724 }
4725 delete_insns_since (last);
4726 return NULL_RTX;
4727 }
4728 \f
4729 /* These functions attempt to generate an insn body, rather than
4730 emitting the insn, but if the gen function already emits them, we
4731 make no attempt to turn them back into naked patterns. */
4732
4733 /* Generate and return an insn body to add Y to X. */
4734
4735 rtx
4736 gen_add2_insn (rtx x, rtx y)
4737 {
4738 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4739
4740 gcc_assert (insn_operand_matches (icode, 0, x));
4741 gcc_assert (insn_operand_matches (icode, 1, x));
4742 gcc_assert (insn_operand_matches (icode, 2, y));
4743
4744 return GEN_FCN (icode) (x, x, y);
4745 }
4746
4747 /* Generate and return an insn body to add r1 and c,
4748 storing the result in r0. */
4749
4750 rtx
4751 gen_add3_insn (rtx r0, rtx r1, rtx c)
4752 {
4753 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4754
4755 if (icode == CODE_FOR_nothing
4756 || !insn_operand_matches (icode, 0, r0)
4757 || !insn_operand_matches (icode, 1, r1)
4758 || !insn_operand_matches (icode, 2, c))
4759 return NULL_RTX;
4760
4761 return GEN_FCN (icode) (r0, r1, c);
4762 }
4763
4764 int
4765 have_add2_insn (rtx x, rtx y)
4766 {
4767 enum insn_code icode;
4768
4769 gcc_assert (GET_MODE (x) != VOIDmode);
4770
4771 icode = optab_handler (add_optab, GET_MODE (x));
4772
4773 if (icode == CODE_FOR_nothing)
4774 return 0;
4775
4776 if (!insn_operand_matches (icode, 0, x)
4777 || !insn_operand_matches (icode, 1, x)
4778 || !insn_operand_matches (icode, 2, y))
4779 return 0;
4780
4781 return 1;
4782 }
4783
4784 /* Generate and return an insn body to add Y to X. */
4785
4786 rtx
4787 gen_addptr3_insn (rtx x, rtx y, rtx z)
4788 {
4789 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4790
4791 gcc_assert (insn_operand_matches (icode, 0, x));
4792 gcc_assert (insn_operand_matches (icode, 1, y));
4793 gcc_assert (insn_operand_matches (icode, 2, z));
4794
4795 return GEN_FCN (icode) (x, y, z);
4796 }
4797
4798 /* Return true if the target implements an addptr pattern and X, Y,
4799 and Z are valid for the pattern predicates. */
4800
4801 int
4802 have_addptr3_insn (rtx x, rtx y, rtx z)
4803 {
4804 enum insn_code icode;
4805
4806 gcc_assert (GET_MODE (x) != VOIDmode);
4807
4808 icode = optab_handler (addptr3_optab, GET_MODE (x));
4809
4810 if (icode == CODE_FOR_nothing)
4811 return 0;
4812
4813 if (!insn_operand_matches (icode, 0, x)
4814 || !insn_operand_matches (icode, 1, y)
4815 || !insn_operand_matches (icode, 2, z))
4816 return 0;
4817
4818 return 1;
4819 }
4820
4821 /* Generate and return an insn body to subtract Y from X. */
4822
4823 rtx
4824 gen_sub2_insn (rtx x, rtx y)
4825 {
4826 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4827
4828 gcc_assert (insn_operand_matches (icode, 0, x));
4829 gcc_assert (insn_operand_matches (icode, 1, x));
4830 gcc_assert (insn_operand_matches (icode, 2, y));
4831
4832 return GEN_FCN (icode) (x, x, y);
4833 }
4834
4835 /* Generate and return an insn body to subtract r1 and c,
4836 storing the result in r0. */
4837
4838 rtx
4839 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4840 {
4841 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4842
4843 if (icode == CODE_FOR_nothing
4844 || !insn_operand_matches (icode, 0, r0)
4845 || !insn_operand_matches (icode, 1, r1)
4846 || !insn_operand_matches (icode, 2, c))
4847 return NULL_RTX;
4848
4849 return GEN_FCN (icode) (r0, r1, c);
4850 }
4851
4852 int
4853 have_sub2_insn (rtx x, rtx y)
4854 {
4855 enum insn_code icode;
4856
4857 gcc_assert (GET_MODE (x) != VOIDmode);
4858
4859 icode = optab_handler (sub_optab, GET_MODE (x));
4860
4861 if (icode == CODE_FOR_nothing)
4862 return 0;
4863
4864 if (!insn_operand_matches (icode, 0, x)
4865 || !insn_operand_matches (icode, 1, x)
4866 || !insn_operand_matches (icode, 2, y))
4867 return 0;
4868
4869 return 1;
4870 }
4871 \f
4872 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4873 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4874 no such operation exists, CODE_FOR_nothing will be returned. */
4875
4876 enum insn_code
4877 can_extend_p (machine_mode to_mode, machine_mode from_mode,
4878 int unsignedp)
4879 {
4880 convert_optab tab;
4881 #ifdef HAVE_ptr_extend
4882 if (unsignedp < 0)
4883 return CODE_FOR_ptr_extend;
4884 #endif
4885
4886 tab = unsignedp ? zext_optab : sext_optab;
4887 return convert_optab_handler (tab, to_mode, from_mode);
4888 }
4889
4890 /* Generate the body of an insn to extend Y (with mode MFROM)
4891 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4892
4893 rtx
4894 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4895 machine_mode mfrom, int unsignedp)
4896 {
4897 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4898 return GEN_FCN (icode) (x, y);
4899 }
4900 \f
4901 /* can_fix_p and can_float_p say whether the target machine
4902 can directly convert a given fixed point type to
4903 a given floating point type, or vice versa.
4904 The returned value is the CODE_FOR_... value to use,
4905 or CODE_FOR_nothing if these modes cannot be directly converted.
4906
4907 *TRUNCP_PTR is set to 1 if it is necessary to output
4908 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4909
4910 static enum insn_code
4911 can_fix_p (machine_mode fixmode, machine_mode fltmode,
4912 int unsignedp, int *truncp_ptr)
4913 {
4914 convert_optab tab;
4915 enum insn_code icode;
4916
4917 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4918 icode = convert_optab_handler (tab, fixmode, fltmode);
4919 if (icode != CODE_FOR_nothing)
4920 {
4921 *truncp_ptr = 0;
4922 return icode;
4923 }
4924
4925 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4926 for this to work. We need to rework the fix* and ftrunc* patterns
4927 and documentation. */
4928 tab = unsignedp ? ufix_optab : sfix_optab;
4929 icode = convert_optab_handler (tab, fixmode, fltmode);
4930 if (icode != CODE_FOR_nothing
4931 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4932 {
4933 *truncp_ptr = 1;
4934 return icode;
4935 }
4936
4937 *truncp_ptr = 0;
4938 return CODE_FOR_nothing;
4939 }
4940
4941 enum insn_code
4942 can_float_p (machine_mode fltmode, machine_mode fixmode,
4943 int unsignedp)
4944 {
4945 convert_optab tab;
4946
4947 tab = unsignedp ? ufloat_optab : sfloat_optab;
4948 return convert_optab_handler (tab, fltmode, fixmode);
4949 }
4950
4951 /* Function supportable_convert_operation
4952
4953 Check whether an operation represented by the code CODE is a
4954 convert operation that is supported by the target platform in
4955 vector form (i.e., when operating on arguments of type VECTYPE_IN
4956 producing a result of type VECTYPE_OUT).
4957
4958 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4959 This function checks if these operations are supported
4960 by the target platform either directly (via vector tree-codes), or via
4961 target builtins.
4962
4963 Output:
4964 - CODE1 is code of vector operation to be used when
4965 vectorizing the operation, if available.
4966 - DECL is decl of target builtin functions to be used
4967 when vectorizing the operation, if available. In this case,
4968 CODE1 is CALL_EXPR. */
4969
4970 bool
4971 supportable_convert_operation (enum tree_code code,
4972 tree vectype_out, tree vectype_in,
4973 tree *decl, enum tree_code *code1)
4974 {
4975 machine_mode m1,m2;
4976 int truncp;
4977
4978 m1 = TYPE_MODE (vectype_out);
4979 m2 = TYPE_MODE (vectype_in);
4980
4981 /* First check if we can done conversion directly. */
4982 if ((code == FIX_TRUNC_EXPR
4983 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4984 != CODE_FOR_nothing)
4985 || (code == FLOAT_EXPR
4986 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4987 != CODE_FOR_nothing))
4988 {
4989 *code1 = code;
4990 return true;
4991 }
4992
4993 /* Now check for builtin. */
4994 if (targetm.vectorize.builtin_conversion
4995 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
4996 {
4997 *code1 = CALL_EXPR;
4998 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
4999 return true;
5000 }
5001 return false;
5002 }
5003
5004 \f
5005 /* Generate code to convert FROM to floating point
5006 and store in TO. FROM must be fixed point and not VOIDmode.
5007 UNSIGNEDP nonzero means regard FROM as unsigned.
5008 Normally this is done by correcting the final value
5009 if it is negative. */
5010
5011 void
5012 expand_float (rtx to, rtx from, int unsignedp)
5013 {
5014 enum insn_code icode;
5015 rtx target = to;
5016 machine_mode fmode, imode;
5017 bool can_do_signed = false;
5018
5019 /* Crash now, because we won't be able to decide which mode to use. */
5020 gcc_assert (GET_MODE (from) != VOIDmode);
5021
5022 /* Look for an insn to do the conversion. Do it in the specified
5023 modes if possible; otherwise convert either input, output or both to
5024 wider mode. If the integer mode is wider than the mode of FROM,
5025 we can do the conversion signed even if the input is unsigned. */
5026
5027 for (fmode = GET_MODE (to); fmode != VOIDmode;
5028 fmode = GET_MODE_WIDER_MODE (fmode))
5029 for (imode = GET_MODE (from); imode != VOIDmode;
5030 imode = GET_MODE_WIDER_MODE (imode))
5031 {
5032 int doing_unsigned = unsignedp;
5033
5034 if (fmode != GET_MODE (to)
5035 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
5036 continue;
5037
5038 icode = can_float_p (fmode, imode, unsignedp);
5039 if (icode == CODE_FOR_nothing && unsignedp)
5040 {
5041 enum insn_code scode = can_float_p (fmode, imode, 0);
5042 if (scode != CODE_FOR_nothing)
5043 can_do_signed = true;
5044 if (imode != GET_MODE (from))
5045 icode = scode, doing_unsigned = 0;
5046 }
5047
5048 if (icode != CODE_FOR_nothing)
5049 {
5050 if (imode != GET_MODE (from))
5051 from = convert_to_mode (imode, from, unsignedp);
5052
5053 if (fmode != GET_MODE (to))
5054 target = gen_reg_rtx (fmode);
5055
5056 emit_unop_insn (icode, target, from,
5057 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5058
5059 if (target != to)
5060 convert_move (to, target, 0);
5061 return;
5062 }
5063 }
5064
5065 /* Unsigned integer, and no way to convert directly. Convert as signed,
5066 then unconditionally adjust the result. */
5067 if (unsignedp && can_do_signed)
5068 {
5069 rtx_code_label *label = gen_label_rtx ();
5070 rtx temp;
5071 REAL_VALUE_TYPE offset;
5072
5073 /* Look for a usable floating mode FMODE wider than the source and at
5074 least as wide as the target. Using FMODE will avoid rounding woes
5075 with unsigned values greater than the signed maximum value. */
5076
5077 for (fmode = GET_MODE (to); fmode != VOIDmode;
5078 fmode = GET_MODE_WIDER_MODE (fmode))
5079 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5080 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5081 break;
5082
5083 if (fmode == VOIDmode)
5084 {
5085 /* There is no such mode. Pretend the target is wide enough. */
5086 fmode = GET_MODE (to);
5087
5088 /* Avoid double-rounding when TO is narrower than FROM. */
5089 if ((significand_size (fmode) + 1)
5090 < GET_MODE_PRECISION (GET_MODE (from)))
5091 {
5092 rtx temp1;
5093 rtx_code_label *neglabel = gen_label_rtx ();
5094
5095 /* Don't use TARGET if it isn't a register, is a hard register,
5096 or is the wrong mode. */
5097 if (!REG_P (target)
5098 || REGNO (target) < FIRST_PSEUDO_REGISTER
5099 || GET_MODE (target) != fmode)
5100 target = gen_reg_rtx (fmode);
5101
5102 imode = GET_MODE (from);
5103 do_pending_stack_adjust ();
5104
5105 /* Test whether the sign bit is set. */
5106 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5107 0, neglabel);
5108
5109 /* The sign bit is not set. Convert as signed. */
5110 expand_float (target, from, 0);
5111 emit_jump_insn (gen_jump (label));
5112 emit_barrier ();
5113
5114 /* The sign bit is set.
5115 Convert to a usable (positive signed) value by shifting right
5116 one bit, while remembering if a nonzero bit was shifted
5117 out; i.e., compute (from & 1) | (from >> 1). */
5118
5119 emit_label (neglabel);
5120 temp = expand_binop (imode, and_optab, from, const1_rtx,
5121 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5122 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5123 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5124 OPTAB_LIB_WIDEN);
5125 expand_float (target, temp, 0);
5126
5127 /* Multiply by 2 to undo the shift above. */
5128 temp = expand_binop (fmode, add_optab, target, target,
5129 target, 0, OPTAB_LIB_WIDEN);
5130 if (temp != target)
5131 emit_move_insn (target, temp);
5132
5133 do_pending_stack_adjust ();
5134 emit_label (label);
5135 goto done;
5136 }
5137 }
5138
5139 /* If we are about to do some arithmetic to correct for an
5140 unsigned operand, do it in a pseudo-register. */
5141
5142 if (GET_MODE (to) != fmode
5143 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5144 target = gen_reg_rtx (fmode);
5145
5146 /* Convert as signed integer to floating. */
5147 expand_float (target, from, 0);
5148
5149 /* If FROM is negative (and therefore TO is negative),
5150 correct its value by 2**bitwidth. */
5151
5152 do_pending_stack_adjust ();
5153 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5154 0, label);
5155
5156
5157 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5158 temp = expand_binop (fmode, add_optab, target,
5159 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5160 target, 0, OPTAB_LIB_WIDEN);
5161 if (temp != target)
5162 emit_move_insn (target, temp);
5163
5164 do_pending_stack_adjust ();
5165 emit_label (label);
5166 goto done;
5167 }
5168
5169 /* No hardware instruction available; call a library routine. */
5170 {
5171 rtx libfunc;
5172 rtx_insn *insns;
5173 rtx value;
5174 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5175
5176 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
5177 from = convert_to_mode (SImode, from, unsignedp);
5178
5179 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5180 gcc_assert (libfunc);
5181
5182 start_sequence ();
5183
5184 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5185 GET_MODE (to), 1, from,
5186 GET_MODE (from));
5187 insns = get_insns ();
5188 end_sequence ();
5189
5190 emit_libcall_block (insns, target, value,
5191 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5192 GET_MODE (to), from));
5193 }
5194
5195 done:
5196
5197 /* Copy result to requested destination
5198 if we have been computing in a temp location. */
5199
5200 if (target != to)
5201 {
5202 if (GET_MODE (target) == GET_MODE (to))
5203 emit_move_insn (to, target);
5204 else
5205 convert_move (to, target, 0);
5206 }
5207 }
5208 \f
5209 /* Generate code to convert FROM to fixed point and store in TO. FROM
5210 must be floating point. */
5211
5212 void
5213 expand_fix (rtx to, rtx from, int unsignedp)
5214 {
5215 enum insn_code icode;
5216 rtx target = to;
5217 machine_mode fmode, imode;
5218 int must_trunc = 0;
5219
5220 /* We first try to find a pair of modes, one real and one integer, at
5221 least as wide as FROM and TO, respectively, in which we can open-code
5222 this conversion. If the integer mode is wider than the mode of TO,
5223 we can do the conversion either signed or unsigned. */
5224
5225 for (fmode = GET_MODE (from); fmode != VOIDmode;
5226 fmode = GET_MODE_WIDER_MODE (fmode))
5227 for (imode = GET_MODE (to); imode != VOIDmode;
5228 imode = GET_MODE_WIDER_MODE (imode))
5229 {
5230 int doing_unsigned = unsignedp;
5231
5232 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5233 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5234 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5235
5236 if (icode != CODE_FOR_nothing)
5237 {
5238 rtx_insn *last = get_last_insn ();
5239 if (fmode != GET_MODE (from))
5240 from = convert_to_mode (fmode, from, 0);
5241
5242 if (must_trunc)
5243 {
5244 rtx temp = gen_reg_rtx (GET_MODE (from));
5245 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5246 temp, 0);
5247 }
5248
5249 if (imode != GET_MODE (to))
5250 target = gen_reg_rtx (imode);
5251
5252 if (maybe_emit_unop_insn (icode, target, from,
5253 doing_unsigned ? UNSIGNED_FIX : FIX))
5254 {
5255 if (target != to)
5256 convert_move (to, target, unsignedp);
5257 return;
5258 }
5259 delete_insns_since (last);
5260 }
5261 }
5262
5263 /* For an unsigned conversion, there is one more way to do it.
5264 If we have a signed conversion, we generate code that compares
5265 the real value to the largest representable positive number. If if
5266 is smaller, the conversion is done normally. Otherwise, subtract
5267 one plus the highest signed number, convert, and add it back.
5268
5269 We only need to check all real modes, since we know we didn't find
5270 anything with a wider integer mode.
5271
5272 This code used to extend FP value into mode wider than the destination.
5273 This is needed for decimal float modes which cannot accurately
5274 represent one plus the highest signed number of the same size, but
5275 not for binary modes. Consider, for instance conversion from SFmode
5276 into DImode.
5277
5278 The hot path through the code is dealing with inputs smaller than 2^63
5279 and doing just the conversion, so there is no bits to lose.
5280
5281 In the other path we know the value is positive in the range 2^63..2^64-1
5282 inclusive. (as for other input overflow happens and result is undefined)
5283 So we know that the most important bit set in mantissa corresponds to
5284 2^63. The subtraction of 2^63 should not generate any rounding as it
5285 simply clears out that bit. The rest is trivial. */
5286
5287 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5288 for (fmode = GET_MODE (from); fmode != VOIDmode;
5289 fmode = GET_MODE_WIDER_MODE (fmode))
5290 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5291 && (!DECIMAL_FLOAT_MODE_P (fmode)
5292 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5293 {
5294 int bitsize;
5295 REAL_VALUE_TYPE offset;
5296 rtx limit;
5297 rtx_code_label *lab1, *lab2;
5298 rtx_insn *insn;
5299
5300 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5301 real_2expN (&offset, bitsize - 1, fmode);
5302 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5303 lab1 = gen_label_rtx ();
5304 lab2 = gen_label_rtx ();
5305
5306 if (fmode != GET_MODE (from))
5307 from = convert_to_mode (fmode, from, 0);
5308
5309 /* See if we need to do the subtraction. */
5310 do_pending_stack_adjust ();
5311 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5312 0, lab1);
5313
5314 /* If not, do the signed "fix" and branch around fixup code. */
5315 expand_fix (to, from, 0);
5316 emit_jump_insn (gen_jump (lab2));
5317 emit_barrier ();
5318
5319 /* Otherwise, subtract 2**(N-1), convert to signed number,
5320 then add 2**(N-1). Do the addition using XOR since this
5321 will often generate better code. */
5322 emit_label (lab1);
5323 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5324 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5325 expand_fix (to, target, 0);
5326 target = expand_binop (GET_MODE (to), xor_optab, to,
5327 gen_int_mode
5328 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5329 GET_MODE (to)),
5330 to, 1, OPTAB_LIB_WIDEN);
5331
5332 if (target != to)
5333 emit_move_insn (to, target);
5334
5335 emit_label (lab2);
5336
5337 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5338 {
5339 /* Make a place for a REG_NOTE and add it. */
5340 insn = emit_move_insn (to, to);
5341 set_dst_reg_note (insn, REG_EQUAL,
5342 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5343 copy_rtx (from)),
5344 to);
5345 }
5346
5347 return;
5348 }
5349
5350 /* We can't do it with an insn, so use a library call. But first ensure
5351 that the mode of TO is at least as wide as SImode, since those are the
5352 only library calls we know about. */
5353
5354 if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
5355 {
5356 target = gen_reg_rtx (SImode);
5357
5358 expand_fix (target, from, unsignedp);
5359 }
5360 else
5361 {
5362 rtx_insn *insns;
5363 rtx value;
5364 rtx libfunc;
5365
5366 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5367 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5368 gcc_assert (libfunc);
5369
5370 start_sequence ();
5371
5372 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5373 GET_MODE (to), 1, from,
5374 GET_MODE (from));
5375 insns = get_insns ();
5376 end_sequence ();
5377
5378 emit_libcall_block (insns, target, value,
5379 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5380 GET_MODE (to), from));
5381 }
5382
5383 if (target != to)
5384 {
5385 if (GET_MODE (to) == GET_MODE (target))
5386 emit_move_insn (to, target);
5387 else
5388 convert_move (to, target, 0);
5389 }
5390 }
5391
5392 /* Generate code to convert FROM or TO a fixed-point.
5393 If UINTP is true, either TO or FROM is an unsigned integer.
5394 If SATP is true, we need to saturate the result. */
5395
5396 void
5397 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5398 {
5399 machine_mode to_mode = GET_MODE (to);
5400 machine_mode from_mode = GET_MODE (from);
5401 convert_optab tab;
5402 enum rtx_code this_code;
5403 enum insn_code code;
5404 rtx_insn *insns;
5405 rtx value;
5406 rtx libfunc;
5407
5408 if (to_mode == from_mode)
5409 {
5410 emit_move_insn (to, from);
5411 return;
5412 }
5413
5414 if (uintp)
5415 {
5416 tab = satp ? satfractuns_optab : fractuns_optab;
5417 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5418 }
5419 else
5420 {
5421 tab = satp ? satfract_optab : fract_optab;
5422 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5423 }
5424 code = convert_optab_handler (tab, to_mode, from_mode);
5425 if (code != CODE_FOR_nothing)
5426 {
5427 emit_unop_insn (code, to, from, this_code);
5428 return;
5429 }
5430
5431 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5432 gcc_assert (libfunc);
5433
5434 start_sequence ();
5435 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5436 1, from, from_mode);
5437 insns = get_insns ();
5438 end_sequence ();
5439
5440 emit_libcall_block (insns, to, value,
5441 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5442 }
5443
5444 /* Generate code to convert FROM to fixed point and store in TO. FROM
5445 must be floating point, TO must be signed. Use the conversion optab
5446 TAB to do the conversion. */
5447
5448 bool
5449 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5450 {
5451 enum insn_code icode;
5452 rtx target = to;
5453 machine_mode fmode, imode;
5454
5455 /* We first try to find a pair of modes, one real and one integer, at
5456 least as wide as FROM and TO, respectively, in which we can open-code
5457 this conversion. If the integer mode is wider than the mode of TO,
5458 we can do the conversion either signed or unsigned. */
5459
5460 for (fmode = GET_MODE (from); fmode != VOIDmode;
5461 fmode = GET_MODE_WIDER_MODE (fmode))
5462 for (imode = GET_MODE (to); imode != VOIDmode;
5463 imode = GET_MODE_WIDER_MODE (imode))
5464 {
5465 icode = convert_optab_handler (tab, imode, fmode);
5466 if (icode != CODE_FOR_nothing)
5467 {
5468 rtx_insn *last = get_last_insn ();
5469 if (fmode != GET_MODE (from))
5470 from = convert_to_mode (fmode, from, 0);
5471
5472 if (imode != GET_MODE (to))
5473 target = gen_reg_rtx (imode);
5474
5475 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5476 {
5477 delete_insns_since (last);
5478 continue;
5479 }
5480 if (target != to)
5481 convert_move (to, target, 0);
5482 return true;
5483 }
5484 }
5485
5486 return false;
5487 }
5488 \f
5489 /* Report whether we have an instruction to perform the operation
5490 specified by CODE on operands of mode MODE. */
5491 int
5492 have_insn_for (enum rtx_code code, machine_mode mode)
5493 {
5494 return (code_to_optab (code)
5495 && (optab_handler (code_to_optab (code), mode)
5496 != CODE_FOR_nothing));
5497 }
5498
5499 /* Initialize the libfunc fields of an entire group of entries in some
5500 optab. Each entry is set equal to a string consisting of a leading
5501 pair of underscores followed by a generic operation name followed by
5502 a mode name (downshifted to lowercase) followed by a single character
5503 representing the number of operands for the given operation (which is
5504 usually one of the characters '2', '3', or '4').
5505
5506 OPTABLE is the table in which libfunc fields are to be initialized.
5507 OPNAME is the generic (string) name of the operation.
5508 SUFFIX is the character which specifies the number of operands for
5509 the given generic operation.
5510 MODE is the mode to generate for.
5511 */
5512
5513 static void
5514 gen_libfunc (optab optable, const char *opname, int suffix,
5515 machine_mode mode)
5516 {
5517 unsigned opname_len = strlen (opname);
5518 const char *mname = GET_MODE_NAME (mode);
5519 unsigned mname_len = strlen (mname);
5520 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5521 int len = prefix_len + opname_len + mname_len + 1 + 1;
5522 char *libfunc_name = XALLOCAVEC (char, len);
5523 char *p;
5524 const char *q;
5525
5526 p = libfunc_name;
5527 *p++ = '_';
5528 *p++ = '_';
5529 if (targetm.libfunc_gnu_prefix)
5530 {
5531 *p++ = 'g';
5532 *p++ = 'n';
5533 *p++ = 'u';
5534 *p++ = '_';
5535 }
5536 for (q = opname; *q; )
5537 *p++ = *q++;
5538 for (q = mname; *q; q++)
5539 *p++ = TOLOWER (*q);
5540 *p++ = suffix;
5541 *p = '\0';
5542
5543 set_optab_libfunc (optable, mode,
5544 ggc_alloc_string (libfunc_name, p - libfunc_name));
5545 }
5546
5547 /* Like gen_libfunc, but verify that integer operation is involved. */
5548
5549 void
5550 gen_int_libfunc (optab optable, const char *opname, char suffix,
5551 machine_mode mode)
5552 {
5553 int maxsize = 2 * BITS_PER_WORD;
5554 int minsize = BITS_PER_WORD;
5555
5556 if (GET_MODE_CLASS (mode) != MODE_INT)
5557 return;
5558 if (maxsize < LONG_LONG_TYPE_SIZE)
5559 maxsize = LONG_LONG_TYPE_SIZE;
5560 if (minsize > INT_TYPE_SIZE
5561 && (trapv_binoptab_p (optable)
5562 || trapv_unoptab_p (optable)))
5563 minsize = INT_TYPE_SIZE;
5564 if (GET_MODE_BITSIZE (mode) < minsize
5565 || GET_MODE_BITSIZE (mode) > maxsize)
5566 return;
5567 gen_libfunc (optable, opname, suffix, mode);
5568 }
5569
5570 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5571
5572 void
5573 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5574 machine_mode mode)
5575 {
5576 char *dec_opname;
5577
5578 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5579 gen_libfunc (optable, opname, suffix, mode);
5580 if (DECIMAL_FLOAT_MODE_P (mode))
5581 {
5582 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5583 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5584 depending on the low level floating format used. */
5585 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5586 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5587 gen_libfunc (optable, dec_opname, suffix, mode);
5588 }
5589 }
5590
5591 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5592
5593 void
5594 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5595 machine_mode mode)
5596 {
5597 if (!ALL_FIXED_POINT_MODE_P (mode))
5598 return;
5599 gen_libfunc (optable, opname, suffix, mode);
5600 }
5601
5602 /* Like gen_libfunc, but verify that signed fixed-point operation is
5603 involved. */
5604
5605 void
5606 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5607 machine_mode mode)
5608 {
5609 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5610 return;
5611 gen_libfunc (optable, opname, suffix, mode);
5612 }
5613
5614 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5615 involved. */
5616
5617 void
5618 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5619 machine_mode mode)
5620 {
5621 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5622 return;
5623 gen_libfunc (optable, opname, suffix, mode);
5624 }
5625
5626 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5627
5628 void
5629 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5630 machine_mode mode)
5631 {
5632 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5633 gen_fp_libfunc (optable, name, suffix, mode);
5634 if (INTEGRAL_MODE_P (mode))
5635 gen_int_libfunc (optable, name, suffix, mode);
5636 }
5637
5638 /* Like gen_libfunc, but verify that FP or INT operation is involved
5639 and add 'v' suffix for integer operation. */
5640
5641 void
5642 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5643 machine_mode mode)
5644 {
5645 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5646 gen_fp_libfunc (optable, name, suffix, mode);
5647 if (GET_MODE_CLASS (mode) == MODE_INT)
5648 {
5649 int len = strlen (name);
5650 char *v_name = XALLOCAVEC (char, len + 2);
5651 strcpy (v_name, name);
5652 v_name[len] = 'v';
5653 v_name[len + 1] = 0;
5654 gen_int_libfunc (optable, v_name, suffix, mode);
5655 }
5656 }
5657
5658 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5659 involved. */
5660
5661 void
5662 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5663 machine_mode mode)
5664 {
5665 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5666 gen_fp_libfunc (optable, name, suffix, mode);
5667 if (INTEGRAL_MODE_P (mode))
5668 gen_int_libfunc (optable, name, suffix, mode);
5669 if (ALL_FIXED_POINT_MODE_P (mode))
5670 gen_fixed_libfunc (optable, name, suffix, mode);
5671 }
5672
5673 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5674 involved. */
5675
5676 void
5677 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5678 machine_mode mode)
5679 {
5680 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5681 gen_fp_libfunc (optable, name, suffix, mode);
5682 if (INTEGRAL_MODE_P (mode))
5683 gen_int_libfunc (optable, name, suffix, mode);
5684 if (SIGNED_FIXED_POINT_MODE_P (mode))
5685 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5686 }
5687
5688 /* Like gen_libfunc, but verify that INT or FIXED operation is
5689 involved. */
5690
5691 void
5692 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5693 machine_mode mode)
5694 {
5695 if (INTEGRAL_MODE_P (mode))
5696 gen_int_libfunc (optable, name, suffix, mode);
5697 if (ALL_FIXED_POINT_MODE_P (mode))
5698 gen_fixed_libfunc (optable, name, suffix, mode);
5699 }
5700
5701 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5702 involved. */
5703
5704 void
5705 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5706 machine_mode mode)
5707 {
5708 if (INTEGRAL_MODE_P (mode))
5709 gen_int_libfunc (optable, name, suffix, mode);
5710 if (SIGNED_FIXED_POINT_MODE_P (mode))
5711 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5712 }
5713
5714 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5715 involved. */
5716
5717 void
5718 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5719 machine_mode mode)
5720 {
5721 if (INTEGRAL_MODE_P (mode))
5722 gen_int_libfunc (optable, name, suffix, mode);
5723 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5724 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5725 }
5726
5727 /* Initialize the libfunc fields of an entire group of entries of an
5728 inter-mode-class conversion optab. The string formation rules are
5729 similar to the ones for init_libfuncs, above, but instead of having
5730 a mode name and an operand count these functions have two mode names
5731 and no operand count. */
5732
5733 void
5734 gen_interclass_conv_libfunc (convert_optab tab,
5735 const char *opname,
5736 machine_mode tmode,
5737 machine_mode fmode)
5738 {
5739 size_t opname_len = strlen (opname);
5740 size_t mname_len = 0;
5741
5742 const char *fname, *tname;
5743 const char *q;
5744 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5745 char *libfunc_name, *suffix;
5746 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5747 char *p;
5748
5749 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5750 depends on which underlying decimal floating point format is used. */
5751 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5752
5753 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5754
5755 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5756 nondec_name[0] = '_';
5757 nondec_name[1] = '_';
5758 if (targetm.libfunc_gnu_prefix)
5759 {
5760 nondec_name[2] = 'g';
5761 nondec_name[3] = 'n';
5762 nondec_name[4] = 'u';
5763 nondec_name[5] = '_';
5764 }
5765
5766 memcpy (&nondec_name[prefix_len], opname, opname_len);
5767 nondec_suffix = nondec_name + opname_len + prefix_len;
5768
5769 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5770 dec_name[0] = '_';
5771 dec_name[1] = '_';
5772 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5773 memcpy (&dec_name[2+dec_len], opname, opname_len);
5774 dec_suffix = dec_name + dec_len + opname_len + 2;
5775
5776 fname = GET_MODE_NAME (fmode);
5777 tname = GET_MODE_NAME (tmode);
5778
5779 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5780 {
5781 libfunc_name = dec_name;
5782 suffix = dec_suffix;
5783 }
5784 else
5785 {
5786 libfunc_name = nondec_name;
5787 suffix = nondec_suffix;
5788 }
5789
5790 p = suffix;
5791 for (q = fname; *q; p++, q++)
5792 *p = TOLOWER (*q);
5793 for (q = tname; *q; p++, q++)
5794 *p = TOLOWER (*q);
5795
5796 *p = '\0';
5797
5798 set_conv_libfunc (tab, tmode, fmode,
5799 ggc_alloc_string (libfunc_name, p - libfunc_name));
5800 }
5801
5802 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5803 int->fp conversion. */
5804
5805 void
5806 gen_int_to_fp_conv_libfunc (convert_optab tab,
5807 const char *opname,
5808 machine_mode tmode,
5809 machine_mode fmode)
5810 {
5811 if (GET_MODE_CLASS (fmode) != MODE_INT)
5812 return;
5813 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5814 return;
5815 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5816 }
5817
5818 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5819 naming scheme. */
5820
5821 void
5822 gen_ufloat_conv_libfunc (convert_optab tab,
5823 const char *opname ATTRIBUTE_UNUSED,
5824 machine_mode tmode,
5825 machine_mode fmode)
5826 {
5827 if (DECIMAL_FLOAT_MODE_P (tmode))
5828 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5829 else
5830 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5831 }
5832
5833 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5834 fp->int conversion. */
5835
5836 void
5837 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5838 const char *opname,
5839 machine_mode tmode,
5840 machine_mode fmode)
5841 {
5842 if (GET_MODE_CLASS (fmode) != MODE_INT)
5843 return;
5844 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5845 return;
5846 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5847 }
5848
5849 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5850 fp->int conversion with no decimal floating point involved. */
5851
5852 void
5853 gen_fp_to_int_conv_libfunc (convert_optab tab,
5854 const char *opname,
5855 machine_mode tmode,
5856 machine_mode fmode)
5857 {
5858 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5859 return;
5860 if (GET_MODE_CLASS (tmode) != MODE_INT)
5861 return;
5862 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5863 }
5864
5865 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5866 The string formation rules are
5867 similar to the ones for init_libfunc, above. */
5868
5869 void
5870 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5871 machine_mode tmode, machine_mode fmode)
5872 {
5873 size_t opname_len = strlen (opname);
5874 size_t mname_len = 0;
5875
5876 const char *fname, *tname;
5877 const char *q;
5878 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5879 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5880 char *libfunc_name, *suffix;
5881 char *p;
5882
5883 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5884 depends on which underlying decimal floating point format is used. */
5885 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5886
5887 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5888
5889 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5890 nondec_name[0] = '_';
5891 nondec_name[1] = '_';
5892 if (targetm.libfunc_gnu_prefix)
5893 {
5894 nondec_name[2] = 'g';
5895 nondec_name[3] = 'n';
5896 nondec_name[4] = 'u';
5897 nondec_name[5] = '_';
5898 }
5899 memcpy (&nondec_name[prefix_len], opname, opname_len);
5900 nondec_suffix = nondec_name + opname_len + prefix_len;
5901
5902 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5903 dec_name[0] = '_';
5904 dec_name[1] = '_';
5905 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5906 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5907 dec_suffix = dec_name + dec_len + opname_len + 2;
5908
5909 fname = GET_MODE_NAME (fmode);
5910 tname = GET_MODE_NAME (tmode);
5911
5912 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5913 {
5914 libfunc_name = dec_name;
5915 suffix = dec_suffix;
5916 }
5917 else
5918 {
5919 libfunc_name = nondec_name;
5920 suffix = nondec_suffix;
5921 }
5922
5923 p = suffix;
5924 for (q = fname; *q; p++, q++)
5925 *p = TOLOWER (*q);
5926 for (q = tname; *q; p++, q++)
5927 *p = TOLOWER (*q);
5928
5929 *p++ = '2';
5930 *p = '\0';
5931
5932 set_conv_libfunc (tab, tmode, fmode,
5933 ggc_alloc_string (libfunc_name, p - libfunc_name));
5934 }
5935
5936 /* Pick proper libcall for trunc_optab. We need to chose if we do
5937 truncation or extension and interclass or intraclass. */
5938
5939 void
5940 gen_trunc_conv_libfunc (convert_optab tab,
5941 const char *opname,
5942 machine_mode tmode,
5943 machine_mode fmode)
5944 {
5945 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5946 return;
5947 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5948 return;
5949 if (tmode == fmode)
5950 return;
5951
5952 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5953 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5954 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5955
5956 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5957 return;
5958
5959 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5960 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5961 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5962 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5963 }
5964
5965 /* Pick proper libcall for extend_optab. We need to chose if we do
5966 truncation or extension and interclass or intraclass. */
5967
5968 void
5969 gen_extend_conv_libfunc (convert_optab tab,
5970 const char *opname ATTRIBUTE_UNUSED,
5971 machine_mode tmode,
5972 machine_mode fmode)
5973 {
5974 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5975 return;
5976 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5977 return;
5978 if (tmode == fmode)
5979 return;
5980
5981 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5982 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5983 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5984
5985 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5986 return;
5987
5988 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5989 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5990 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5991 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5992 }
5993
5994 /* Pick proper libcall for fract_optab. We need to chose if we do
5995 interclass or intraclass. */
5996
5997 void
5998 gen_fract_conv_libfunc (convert_optab tab,
5999 const char *opname,
6000 machine_mode tmode,
6001 machine_mode fmode)
6002 {
6003 if (tmode == fmode)
6004 return;
6005 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
6006 return;
6007
6008 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6009 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6010 else
6011 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6012 }
6013
6014 /* Pick proper libcall for fractuns_optab. */
6015
6016 void
6017 gen_fractuns_conv_libfunc (convert_optab tab,
6018 const char *opname,
6019 machine_mode tmode,
6020 machine_mode fmode)
6021 {
6022 if (tmode == fmode)
6023 return;
6024 /* One mode must be a fixed-point mode, and the other must be an integer
6025 mode. */
6026 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
6027 || (ALL_FIXED_POINT_MODE_P (fmode)
6028 && GET_MODE_CLASS (tmode) == MODE_INT)))
6029 return;
6030
6031 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6032 }
6033
6034 /* Pick proper libcall for satfract_optab. We need to chose if we do
6035 interclass or intraclass. */
6036
6037 void
6038 gen_satfract_conv_libfunc (convert_optab tab,
6039 const char *opname,
6040 machine_mode tmode,
6041 machine_mode fmode)
6042 {
6043 if (tmode == fmode)
6044 return;
6045 /* TMODE must be a fixed-point mode. */
6046 if (!ALL_FIXED_POINT_MODE_P (tmode))
6047 return;
6048
6049 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6050 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6051 else
6052 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6053 }
6054
6055 /* Pick proper libcall for satfractuns_optab. */
6056
6057 void
6058 gen_satfractuns_conv_libfunc (convert_optab tab,
6059 const char *opname,
6060 machine_mode tmode,
6061 machine_mode fmode)
6062 {
6063 if (tmode == fmode)
6064 return;
6065 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6066 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6067 return;
6068
6069 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6070 }
6071
6072 /* Hashtable callbacks for libfunc_decls. */
6073
6074 struct libfunc_decl_hasher : ggc_hasher<tree>
6075 {
6076 static hashval_t
6077 hash (tree entry)
6078 {
6079 return IDENTIFIER_HASH_VALUE (DECL_NAME (entry));
6080 }
6081
6082 static bool
6083 equal (tree decl, tree name)
6084 {
6085 return DECL_NAME (decl) == name;
6086 }
6087 };
6088
6089 /* A table of previously-created libfuncs, hashed by name. */
6090 static GTY (()) hash_table<libfunc_decl_hasher> *libfunc_decls;
6091
6092 /* Build a decl for a libfunc named NAME. */
6093
6094 tree
6095 build_libfunc_function (const char *name)
6096 {
6097 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6098 get_identifier (name),
6099 build_function_type (integer_type_node, NULL_TREE));
6100 /* ??? We don't have any type information except for this is
6101 a function. Pretend this is "int foo()". */
6102 DECL_ARTIFICIAL (decl) = 1;
6103 DECL_EXTERNAL (decl) = 1;
6104 TREE_PUBLIC (decl) = 1;
6105 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6106
6107 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6108 are the flags assigned by targetm.encode_section_info. */
6109 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6110
6111 return decl;
6112 }
6113
6114 rtx
6115 init_one_libfunc (const char *name)
6116 {
6117 tree id, decl;
6118 hashval_t hash;
6119
6120 if (libfunc_decls == NULL)
6121 libfunc_decls = hash_table<libfunc_decl_hasher>::create_ggc (37);
6122
6123 /* See if we have already created a libfunc decl for this function. */
6124 id = get_identifier (name);
6125 hash = IDENTIFIER_HASH_VALUE (id);
6126 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, INSERT);
6127 decl = *slot;
6128 if (decl == NULL)
6129 {
6130 /* Create a new decl, so that it can be passed to
6131 targetm.encode_section_info. */
6132 decl = build_libfunc_function (name);
6133 *slot = decl;
6134 }
6135 return XEXP (DECL_RTL (decl), 0);
6136 }
6137
6138 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6139
6140 rtx
6141 set_user_assembler_libfunc (const char *name, const char *asmspec)
6142 {
6143 tree id, decl;
6144 hashval_t hash;
6145
6146 id = get_identifier (name);
6147 hash = IDENTIFIER_HASH_VALUE (id);
6148 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, NO_INSERT);
6149 gcc_assert (slot);
6150 decl = (tree) *slot;
6151 set_user_assembler_name (decl, asmspec);
6152 return XEXP (DECL_RTL (decl), 0);
6153 }
6154
6155 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6156 MODE to NAME, which should be either 0 or a string constant. */
6157 void
6158 set_optab_libfunc (optab op, machine_mode mode, const char *name)
6159 {
6160 rtx val;
6161 struct libfunc_entry e;
6162 struct libfunc_entry **slot;
6163
6164 e.op = op;
6165 e.mode1 = mode;
6166 e.mode2 = VOIDmode;
6167
6168 if (name)
6169 val = init_one_libfunc (name);
6170 else
6171 val = 0;
6172 slot = libfunc_hash->find_slot (&e, INSERT);
6173 if (*slot == NULL)
6174 *slot = ggc_alloc<libfunc_entry> ();
6175 (*slot)->op = op;
6176 (*slot)->mode1 = mode;
6177 (*slot)->mode2 = VOIDmode;
6178 (*slot)->libfunc = val;
6179 }
6180
6181 /* Call this to reset the function entry for one conversion optab
6182 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6183 either 0 or a string constant. */
6184 void
6185 set_conv_libfunc (convert_optab optab, machine_mode tmode,
6186 machine_mode fmode, const char *name)
6187 {
6188 rtx val;
6189 struct libfunc_entry e;
6190 struct libfunc_entry **slot;
6191
6192 e.op = optab;
6193 e.mode1 = tmode;
6194 e.mode2 = fmode;
6195
6196 if (name)
6197 val = init_one_libfunc (name);
6198 else
6199 val = 0;
6200 slot = libfunc_hash->find_slot (&e, INSERT);
6201 if (*slot == NULL)
6202 *slot = ggc_alloc<libfunc_entry> ();
6203 (*slot)->op = optab;
6204 (*slot)->mode1 = tmode;
6205 (*slot)->mode2 = fmode;
6206 (*slot)->libfunc = val;
6207 }
6208
6209 /* Call this to initialize the contents of the optabs
6210 appropriately for the current target machine. */
6211
6212 void
6213 init_optabs (void)
6214 {
6215 if (libfunc_hash)
6216 libfunc_hash->empty ();
6217 else
6218 libfunc_hash = hash_table<libfunc_hasher>::create_ggc (10);
6219
6220 /* Fill in the optabs with the insns we support. */
6221 init_all_optabs (this_fn_optabs);
6222
6223 /* The ffs function operates on `int'. Fall back on it if we do not
6224 have a libgcc2 function for that width. */
6225 if (INT_TYPE_SIZE < BITS_PER_WORD)
6226 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6227 "ffs");
6228
6229 /* Explicitly initialize the bswap libfuncs since we need them to be
6230 valid for things other than word_mode. */
6231 if (targetm.libfunc_gnu_prefix)
6232 {
6233 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6234 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6235 }
6236 else
6237 {
6238 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6239 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6240 }
6241
6242 /* Use cabs for double complex abs, since systems generally have cabs.
6243 Don't define any libcall for float complex, so that cabs will be used. */
6244 if (complex_double_type_node)
6245 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6246 "cabs");
6247
6248 abort_libfunc = init_one_libfunc ("abort");
6249 memcpy_libfunc = init_one_libfunc ("memcpy");
6250 memmove_libfunc = init_one_libfunc ("memmove");
6251 memcmp_libfunc = init_one_libfunc ("memcmp");
6252 memset_libfunc = init_one_libfunc ("memset");
6253 setbits_libfunc = init_one_libfunc ("__setbits");
6254
6255 #ifndef DONT_USE_BUILTIN_SETJMP
6256 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6257 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6258 #else
6259 setjmp_libfunc = init_one_libfunc ("setjmp");
6260 longjmp_libfunc = init_one_libfunc ("longjmp");
6261 #endif
6262 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6263 unwind_sjlj_unregister_libfunc
6264 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6265
6266 /* For function entry/exit instrumentation. */
6267 profile_function_entry_libfunc
6268 = init_one_libfunc ("__cyg_profile_func_enter");
6269 profile_function_exit_libfunc
6270 = init_one_libfunc ("__cyg_profile_func_exit");
6271
6272 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6273
6274 /* Allow the target to add more libcalls or rename some, etc. */
6275 targetm.init_libfuncs ();
6276 }
6277
6278 /* Use the current target and options to initialize
6279 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
6280
6281 void
6282 init_tree_optimization_optabs (tree optnode)
6283 {
6284 /* Quick exit if we have already computed optabs for this target. */
6285 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
6286 return;
6287
6288 /* Forget any previous information and set up for the current target. */
6289 TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
6290 struct target_optabs *tmp_optabs = (struct target_optabs *)
6291 TREE_OPTIMIZATION_OPTABS (optnode);
6292 if (tmp_optabs)
6293 memset (tmp_optabs, 0, sizeof (struct target_optabs));
6294 else
6295 tmp_optabs = ggc_alloc<target_optabs> ();
6296
6297 /* Generate a new set of optabs into tmp_optabs. */
6298 init_all_optabs (tmp_optabs);
6299
6300 /* If the optabs changed, record it. */
6301 if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
6302 TREE_OPTIMIZATION_OPTABS (optnode) = tmp_optabs;
6303 else
6304 {
6305 TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
6306 ggc_free (tmp_optabs);
6307 }
6308 }
6309
6310 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6311 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6312
6313 static void
6314 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6315 {
6316 machine_mode mode;
6317 char buf[64];
6318 size_t len = strlen (base);
6319 int i;
6320
6321 gcc_assert (max <= 8);
6322 gcc_assert (len + 3 < sizeof (buf));
6323
6324 memcpy (buf, base, len);
6325 buf[len] = '_';
6326 buf[len + 1] = '0';
6327 buf[len + 2] = '\0';
6328
6329 mode = QImode;
6330 for (i = 1; i <= max; i *= 2)
6331 {
6332 buf[len + 1] = '0' + i;
6333 set_optab_libfunc (tab, mode, buf);
6334 mode = GET_MODE_2XWIDER_MODE (mode);
6335 }
6336 }
6337
6338 void
6339 init_sync_libfuncs (int max)
6340 {
6341 if (!flag_sync_libcalls)
6342 return;
6343
6344 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6345 "__sync_val_compare_and_swap", max);
6346 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6347 "__sync_lock_test_and_set", max);
6348
6349 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6350 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6351 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6352 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6353 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6354 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6355
6356 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6357 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6358 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6359 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6360 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6361 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6362 }
6363
6364 /* Print information about the current contents of the optabs on
6365 STDERR. */
6366
6367 DEBUG_FUNCTION void
6368 debug_optab_libfuncs (void)
6369 {
6370 int i, j, k;
6371
6372 /* Dump the arithmetic optabs. */
6373 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6374 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6375 {
6376 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
6377 if (l)
6378 {
6379 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6380 fprintf (stderr, "%s\t%s:\t%s\n",
6381 GET_RTX_NAME (optab_to_code ((optab) i)),
6382 GET_MODE_NAME (j),
6383 XSTR (l, 0));
6384 }
6385 }
6386
6387 /* Dump the conversion optabs. */
6388 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6389 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6390 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6391 {
6392 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
6393 (machine_mode) k);
6394 if (l)
6395 {
6396 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6397 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6398 GET_RTX_NAME (optab_to_code ((optab) i)),
6399 GET_MODE_NAME (j),
6400 GET_MODE_NAME (k),
6401 XSTR (l, 0));
6402 }
6403 }
6404 }
6405
6406 \f
6407 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6408 CODE. Return 0 on failure. */
6409
6410 rtx
6411 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6412 {
6413 machine_mode mode = GET_MODE (op1);
6414 enum insn_code icode;
6415 rtx insn;
6416 rtx trap_rtx;
6417
6418 if (mode == VOIDmode)
6419 return 0;
6420
6421 icode = optab_handler (ctrap_optab, mode);
6422 if (icode == CODE_FOR_nothing)
6423 return 0;
6424
6425 /* Some targets only accept a zero trap code. */
6426 if (!insn_operand_matches (icode, 3, tcode))
6427 return 0;
6428
6429 do_pending_stack_adjust ();
6430 start_sequence ();
6431 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6432 &trap_rtx, &mode);
6433 if (!trap_rtx)
6434 insn = NULL_RTX;
6435 else
6436 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6437 tcode);
6438
6439 /* If that failed, then give up. */
6440 if (insn == 0)
6441 {
6442 end_sequence ();
6443 return 0;
6444 }
6445
6446 emit_insn (insn);
6447 insn = get_insns ();
6448 end_sequence ();
6449 return insn;
6450 }
6451
6452 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6453 or unsigned operation code. */
6454
6455 enum rtx_code
6456 get_rtx_code (enum tree_code tcode, bool unsignedp)
6457 {
6458 enum rtx_code code;
6459 switch (tcode)
6460 {
6461 case EQ_EXPR:
6462 code = EQ;
6463 break;
6464 case NE_EXPR:
6465 code = NE;
6466 break;
6467 case LT_EXPR:
6468 code = unsignedp ? LTU : LT;
6469 break;
6470 case LE_EXPR:
6471 code = unsignedp ? LEU : LE;
6472 break;
6473 case GT_EXPR:
6474 code = unsignedp ? GTU : GT;
6475 break;
6476 case GE_EXPR:
6477 code = unsignedp ? GEU : GE;
6478 break;
6479
6480 case UNORDERED_EXPR:
6481 code = UNORDERED;
6482 break;
6483 case ORDERED_EXPR:
6484 code = ORDERED;
6485 break;
6486 case UNLT_EXPR:
6487 code = UNLT;
6488 break;
6489 case UNLE_EXPR:
6490 code = UNLE;
6491 break;
6492 case UNGT_EXPR:
6493 code = UNGT;
6494 break;
6495 case UNGE_EXPR:
6496 code = UNGE;
6497 break;
6498 case UNEQ_EXPR:
6499 code = UNEQ;
6500 break;
6501 case LTGT_EXPR:
6502 code = LTGT;
6503 break;
6504
6505 case BIT_AND_EXPR:
6506 code = AND;
6507 break;
6508
6509 case BIT_IOR_EXPR:
6510 code = IOR;
6511 break;
6512
6513 default:
6514 gcc_unreachable ();
6515 }
6516 return code;
6517 }
6518
6519 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6520 unsigned operators. Do not generate compare instruction. */
6521
6522 static rtx
6523 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6524 bool unsignedp, enum insn_code icode)
6525 {
6526 struct expand_operand ops[2];
6527 rtx rtx_op0, rtx_op1;
6528 machine_mode m0, m1;
6529 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6530
6531 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6532
6533 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
6534 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
6535 cases, use the original mode. */
6536 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6537 EXPAND_STACK_PARM);
6538 m0 = GET_MODE (rtx_op0);
6539 if (m0 == VOIDmode)
6540 m0 = TYPE_MODE (TREE_TYPE (t_op0));
6541
6542 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6543 EXPAND_STACK_PARM);
6544 m1 = GET_MODE (rtx_op1);
6545 if (m1 == VOIDmode)
6546 m1 = TYPE_MODE (TREE_TYPE (t_op1));
6547
6548 create_input_operand (&ops[0], rtx_op0, m0);
6549 create_input_operand (&ops[1], rtx_op1, m1);
6550 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6551 gcc_unreachable ();
6552 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6553 }
6554
6555 /* Return true if VEC_PERM_EXPR of arbitrary input vectors can be expanded using
6556 SIMD extensions of the CPU. SEL may be NULL, which stands for an unknown
6557 constant. Note that additional permutations representing whole-vector shifts
6558 may also be handled via the vec_shr optab, but only where the second input
6559 vector is entirely constant zeroes; this case is not dealt with here. */
6560
6561 bool
6562 can_vec_perm_p (machine_mode mode, bool variable,
6563 const unsigned char *sel)
6564 {
6565 machine_mode qimode;
6566
6567 /* If the target doesn't implement a vector mode for the vector type,
6568 then no operations are supported. */
6569 if (!VECTOR_MODE_P (mode))
6570 return false;
6571
6572 if (!variable)
6573 {
6574 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6575 && (sel == NULL
6576 || targetm.vectorize.vec_perm_const_ok == NULL
6577 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6578 return true;
6579 }
6580
6581 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6582 return true;
6583
6584 /* We allow fallback to a QI vector mode, and adjust the mask. */
6585 if (GET_MODE_INNER (mode) == QImode)
6586 return false;
6587 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6588 if (!VECTOR_MODE_P (qimode))
6589 return false;
6590
6591 /* ??? For completeness, we ought to check the QImode version of
6592 vec_perm_const_optab. But all users of this implicit lowering
6593 feature implement the variable vec_perm_optab. */
6594 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6595 return false;
6596
6597 /* In order to support the lowering of variable permutations,
6598 we need to support shifts and adds. */
6599 if (variable)
6600 {
6601 if (GET_MODE_UNIT_SIZE (mode) > 2
6602 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6603 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6604 return false;
6605 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6606 return false;
6607 }
6608
6609 return true;
6610 }
6611
6612 /* Checks if vec_perm mask SEL is a constant equivalent to a shift of the first
6613 vec_perm operand, assuming the second operand is a constant vector of zeroes.
6614 Return the shift distance in bits if so, or NULL_RTX if the vec_perm is not a
6615 shift. */
6616 static rtx
6617 shift_amt_for_vec_perm_mask (rtx sel)
6618 {
6619 unsigned int i, first, nelt = GET_MODE_NUNITS (GET_MODE (sel));
6620 unsigned int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (sel)));
6621
6622 if (GET_CODE (sel) != CONST_VECTOR)
6623 return NULL_RTX;
6624
6625 first = INTVAL (CONST_VECTOR_ELT (sel, 0));
6626 if (first >= 2*nelt)
6627 return NULL_RTX;
6628 for (i = 1; i < nelt; i++)
6629 {
6630 int idx = INTVAL (CONST_VECTOR_ELT (sel, i));
6631 unsigned int expected = (i + first) & (2 * nelt - 1);
6632 /* Indices into the second vector are all equivalent. */
6633 if (idx < 0 || (MIN (nelt, (unsigned) idx) != MIN (nelt, expected)))
6634 return NULL_RTX;
6635 }
6636
6637 return GEN_INT (first * bitsize);
6638 }
6639
6640 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6641
6642 static rtx
6643 expand_vec_perm_1 (enum insn_code icode, rtx target,
6644 rtx v0, rtx v1, rtx sel)
6645 {
6646 machine_mode tmode = GET_MODE (target);
6647 machine_mode smode = GET_MODE (sel);
6648 struct expand_operand ops[4];
6649
6650 create_output_operand (&ops[0], target, tmode);
6651 create_input_operand (&ops[3], sel, smode);
6652
6653 /* Make an effort to preserve v0 == v1. The target expander is able to
6654 rely on this to determine if we're permuting a single input operand. */
6655 if (rtx_equal_p (v0, v1))
6656 {
6657 if (!insn_operand_matches (icode, 1, v0))
6658 v0 = force_reg (tmode, v0);
6659 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6660 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6661
6662 create_fixed_operand (&ops[1], v0);
6663 create_fixed_operand (&ops[2], v0);
6664 }
6665 else
6666 {
6667 create_input_operand (&ops[1], v0, tmode);
6668 /* See if this can be handled with a vec_shr. We only do this if the
6669 second vector is all zeroes. */
6670 enum insn_code shift_code = optab_handler (vec_shr_optab, GET_MODE (v0));
6671 if (v1 == CONST0_RTX (GET_MODE (v1)) && shift_code)
6672 if (rtx shift_amt = shift_amt_for_vec_perm_mask (sel))
6673 {
6674 create_convert_operand_from_type (&ops[2], shift_amt,
6675 sizetype_tab[(int) stk_sizetype]);
6676 if (maybe_expand_insn (shift_code, 3, ops))
6677 return ops[0].value;
6678 }
6679 create_input_operand (&ops[2], v1, tmode);
6680 }
6681
6682 if (maybe_expand_insn (icode, 4, ops))
6683 return ops[0].value;
6684 return NULL_RTX;
6685 }
6686
6687 /* Generate instructions for vec_perm optab given its mode
6688 and three operands. */
6689
6690 rtx
6691 expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6692 {
6693 enum insn_code icode;
6694 machine_mode qimode;
6695 unsigned int i, w, e, u;
6696 rtx tmp, sel_qi = NULL;
6697 rtvec vec;
6698
6699 if (!target || GET_MODE (target) != mode)
6700 target = gen_reg_rtx (mode);
6701
6702 w = GET_MODE_SIZE (mode);
6703 e = GET_MODE_NUNITS (mode);
6704 u = GET_MODE_UNIT_SIZE (mode);
6705
6706 /* Set QIMODE to a different vector mode with byte elements.
6707 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6708 qimode = VOIDmode;
6709 if (GET_MODE_INNER (mode) != QImode)
6710 {
6711 qimode = mode_for_vector (QImode, w);
6712 if (!VECTOR_MODE_P (qimode))
6713 qimode = VOIDmode;
6714 }
6715
6716 /* If the input is a constant, expand it specially. */
6717 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6718 if (GET_CODE (sel) == CONST_VECTOR)
6719 {
6720 icode = direct_optab_handler (vec_perm_const_optab, mode);
6721 if (icode != CODE_FOR_nothing)
6722 {
6723 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6724 if (tmp)
6725 return tmp;
6726 }
6727
6728 /* Fall back to a constant byte-based permutation. */
6729 if (qimode != VOIDmode)
6730 {
6731 vec = rtvec_alloc (w);
6732 for (i = 0; i < e; ++i)
6733 {
6734 unsigned int j, this_e;
6735
6736 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6737 this_e &= 2 * e - 1;
6738 this_e *= u;
6739
6740 for (j = 0; j < u; ++j)
6741 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6742 }
6743 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6744
6745 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6746 if (icode != CODE_FOR_nothing)
6747 {
6748 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6749 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6750 gen_lowpart (qimode, v1), sel_qi);
6751 if (tmp)
6752 return gen_lowpart (mode, tmp);
6753 }
6754 }
6755 }
6756
6757 /* Otherwise expand as a fully variable permuation. */
6758 icode = direct_optab_handler (vec_perm_optab, mode);
6759 if (icode != CODE_FOR_nothing)
6760 {
6761 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6762 if (tmp)
6763 return tmp;
6764 }
6765
6766 /* As a special case to aid several targets, lower the element-based
6767 permutation to a byte-based permutation and try again. */
6768 if (qimode == VOIDmode)
6769 return NULL_RTX;
6770 icode = direct_optab_handler (vec_perm_optab, qimode);
6771 if (icode == CODE_FOR_nothing)
6772 return NULL_RTX;
6773
6774 if (sel_qi == NULL)
6775 {
6776 /* Multiply each element by its byte size. */
6777 machine_mode selmode = GET_MODE (sel);
6778 if (u == 2)
6779 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6780 sel, 0, OPTAB_DIRECT);
6781 else
6782 sel = expand_simple_binop (selmode, ASHIFT, sel,
6783 GEN_INT (exact_log2 (u)),
6784 sel, 0, OPTAB_DIRECT);
6785 gcc_assert (sel != NULL);
6786
6787 /* Broadcast the low byte each element into each of its bytes. */
6788 vec = rtvec_alloc (w);
6789 for (i = 0; i < w; ++i)
6790 {
6791 int this_e = i / u * u;
6792 if (BYTES_BIG_ENDIAN)
6793 this_e += u - 1;
6794 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6795 }
6796 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6797 sel = gen_lowpart (qimode, sel);
6798 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6799 gcc_assert (sel != NULL);
6800
6801 /* Add the byte offset to each byte element. */
6802 /* Note that the definition of the indicies here is memory ordering,
6803 so there should be no difference between big and little endian. */
6804 vec = rtvec_alloc (w);
6805 for (i = 0; i < w; ++i)
6806 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6807 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6808 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6809 sel, 0, OPTAB_DIRECT);
6810 gcc_assert (sel_qi != NULL);
6811 }
6812
6813 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6814 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6815 gen_lowpart (qimode, v1), sel_qi);
6816 if (tmp)
6817 tmp = gen_lowpart (mode, tmp);
6818 return tmp;
6819 }
6820
6821 /* Return insn code for a conditional operator with a comparison in
6822 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6823
6824 static inline enum insn_code
6825 get_vcond_icode (machine_mode vmode, machine_mode cmode, bool uns)
6826 {
6827 enum insn_code icode = CODE_FOR_nothing;
6828 if (uns)
6829 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6830 else
6831 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6832 return icode;
6833 }
6834
6835 /* Return TRUE iff, appropriate vector insns are available
6836 for vector cond expr with vector type VALUE_TYPE and a comparison
6837 with operand vector types in CMP_OP_TYPE. */
6838
6839 bool
6840 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6841 {
6842 machine_mode value_mode = TYPE_MODE (value_type);
6843 machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6844 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6845 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6846 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6847 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6848 return false;
6849 return true;
6850 }
6851
6852 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6853 three operands. */
6854
6855 rtx
6856 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6857 rtx target)
6858 {
6859 struct expand_operand ops[6];
6860 enum insn_code icode;
6861 rtx comparison, rtx_op1, rtx_op2;
6862 machine_mode mode = TYPE_MODE (vec_cond_type);
6863 machine_mode cmp_op_mode;
6864 bool unsignedp;
6865 tree op0a, op0b;
6866 enum tree_code tcode;
6867
6868 if (COMPARISON_CLASS_P (op0))
6869 {
6870 op0a = TREE_OPERAND (op0, 0);
6871 op0b = TREE_OPERAND (op0, 1);
6872 tcode = TREE_CODE (op0);
6873 }
6874 else
6875 {
6876 /* Fake op0 < 0. */
6877 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6878 op0a = op0;
6879 op0b = build_zero_cst (TREE_TYPE (op0));
6880 tcode = LT_EXPR;
6881 }
6882 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6883 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6884
6885
6886 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6887 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6888
6889 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6890 if (icode == CODE_FOR_nothing)
6891 return 0;
6892
6893 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6894 rtx_op1 = expand_normal (op1);
6895 rtx_op2 = expand_normal (op2);
6896
6897 create_output_operand (&ops[0], target, mode);
6898 create_input_operand (&ops[1], rtx_op1, mode);
6899 create_input_operand (&ops[2], rtx_op2, mode);
6900 create_fixed_operand (&ops[3], comparison);
6901 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6902 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6903 expand_insn (icode, 6, ops);
6904 return ops[0].value;
6905 }
6906
6907 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6908 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6909 2 for even/odd widening, and 3 for hi/lo widening. */
6910
6911 int
6912 can_mult_highpart_p (machine_mode mode, bool uns_p)
6913 {
6914 optab op;
6915 unsigned char *sel;
6916 unsigned i, nunits;
6917
6918 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6919 if (optab_handler (op, mode) != CODE_FOR_nothing)
6920 return 1;
6921
6922 /* If the mode is an integral vector, synth from widening operations. */
6923 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6924 return 0;
6925
6926 nunits = GET_MODE_NUNITS (mode);
6927 sel = XALLOCAVEC (unsigned char, nunits);
6928
6929 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6930 if (optab_handler (op, mode) != CODE_FOR_nothing)
6931 {
6932 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6933 if (optab_handler (op, mode) != CODE_FOR_nothing)
6934 {
6935 for (i = 0; i < nunits; ++i)
6936 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6937 if (can_vec_perm_p (mode, false, sel))
6938 return 2;
6939 }
6940 }
6941
6942 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6943 if (optab_handler (op, mode) != CODE_FOR_nothing)
6944 {
6945 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6946 if (optab_handler (op, mode) != CODE_FOR_nothing)
6947 {
6948 for (i = 0; i < nunits; ++i)
6949 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6950 if (can_vec_perm_p (mode, false, sel))
6951 return 3;
6952 }
6953 }
6954
6955 return 0;
6956 }
6957
6958 /* Expand a highpart multiply. */
6959
6960 rtx
6961 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
6962 rtx target, bool uns_p)
6963 {
6964 struct expand_operand eops[3];
6965 enum insn_code icode;
6966 int method, i, nunits;
6967 machine_mode wmode;
6968 rtx m1, m2, perm;
6969 optab tab1, tab2;
6970 rtvec v;
6971
6972 method = can_mult_highpart_p (mode, uns_p);
6973 switch (method)
6974 {
6975 case 0:
6976 return NULL_RTX;
6977 case 1:
6978 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6979 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6980 OPTAB_LIB_WIDEN);
6981 case 2:
6982 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6983 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6984 break;
6985 case 3:
6986 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6987 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6988 if (BYTES_BIG_ENDIAN)
6989 {
6990 optab t = tab1;
6991 tab1 = tab2;
6992 tab2 = t;
6993 }
6994 break;
6995 default:
6996 gcc_unreachable ();
6997 }
6998
6999 icode = optab_handler (tab1, mode);
7000 nunits = GET_MODE_NUNITS (mode);
7001 wmode = insn_data[icode].operand[0].mode;
7002 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
7003 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
7004
7005 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
7006 create_input_operand (&eops[1], op0, mode);
7007 create_input_operand (&eops[2], op1, mode);
7008 expand_insn (icode, 3, eops);
7009 m1 = gen_lowpart (mode, eops[0].value);
7010
7011 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
7012 create_input_operand (&eops[1], op0, mode);
7013 create_input_operand (&eops[2], op1, mode);
7014 expand_insn (optab_handler (tab2, mode), 3, eops);
7015 m2 = gen_lowpart (mode, eops[0].value);
7016
7017 v = rtvec_alloc (nunits);
7018 if (method == 2)
7019 {
7020 for (i = 0; i < nunits; ++i)
7021 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
7022 + ((i & 1) ? nunits : 0));
7023 }
7024 else
7025 {
7026 for (i = 0; i < nunits; ++i)
7027 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
7028 }
7029 perm = gen_rtx_CONST_VECTOR (mode, v);
7030
7031 return expand_vec_perm (mode, m1, m2, perm, target);
7032 }
7033
7034 /* Return true if target supports vector masked load/store for mode. */
7035 bool
7036 can_vec_mask_load_store_p (machine_mode mode, bool is_load)
7037 {
7038 optab op = is_load ? maskload_optab : maskstore_optab;
7039 machine_mode vmode;
7040 unsigned int vector_sizes;
7041
7042 /* If mode is vector mode, check it directly. */
7043 if (VECTOR_MODE_P (mode))
7044 return optab_handler (op, mode) != CODE_FOR_nothing;
7045
7046 /* Otherwise, return true if there is some vector mode with
7047 the mask load/store supported. */
7048
7049 /* See if there is any chance the mask load or store might be
7050 vectorized. If not, punt. */
7051 vmode = targetm.vectorize.preferred_simd_mode (mode);
7052 if (!VECTOR_MODE_P (vmode))
7053 return false;
7054
7055 if (optab_handler (op, vmode) != CODE_FOR_nothing)
7056 return true;
7057
7058 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
7059 while (vector_sizes != 0)
7060 {
7061 unsigned int cur = 1 << floor_log2 (vector_sizes);
7062 vector_sizes &= ~cur;
7063 if (cur <= GET_MODE_SIZE (mode))
7064 continue;
7065 vmode = mode_for_vector (mode, cur / GET_MODE_SIZE (mode));
7066 if (VECTOR_MODE_P (vmode)
7067 && optab_handler (op, vmode) != CODE_FOR_nothing)
7068 return true;
7069 }
7070 return false;
7071 }
7072 \f
7073 /* Return true if there is a compare_and_swap pattern. */
7074
7075 bool
7076 can_compare_and_swap_p (machine_mode mode, bool allow_libcall)
7077 {
7078 enum insn_code icode;
7079
7080 /* Check for __atomic_compare_and_swap. */
7081 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7082 if (icode != CODE_FOR_nothing)
7083 return true;
7084
7085 /* Check for __sync_compare_and_swap. */
7086 icode = optab_handler (sync_compare_and_swap_optab, mode);
7087 if (icode != CODE_FOR_nothing)
7088 return true;
7089 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7090 return true;
7091
7092 /* No inline compare and swap. */
7093 return false;
7094 }
7095
7096 /* Return true if an atomic exchange can be performed. */
7097
7098 bool
7099 can_atomic_exchange_p (machine_mode mode, bool allow_libcall)
7100 {
7101 enum insn_code icode;
7102
7103 /* Check for __atomic_exchange. */
7104 icode = direct_optab_handler (atomic_exchange_optab, mode);
7105 if (icode != CODE_FOR_nothing)
7106 return true;
7107
7108 /* Don't check __sync_test_and_set, as on some platforms that
7109 has reduced functionality. Targets that really do support
7110 a proper exchange should simply be updated to the __atomics. */
7111
7112 return can_compare_and_swap_p (mode, allow_libcall);
7113 }
7114
7115
7116 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7117 pattern. */
7118
7119 static void
7120 find_cc_set (rtx x, const_rtx pat, void *data)
7121 {
7122 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7123 && GET_CODE (pat) == SET)
7124 {
7125 rtx *p_cc_reg = (rtx *) data;
7126 gcc_assert (!*p_cc_reg);
7127 *p_cc_reg = x;
7128 }
7129 }
7130
7131 /* This is a helper function for the other atomic operations. This function
7132 emits a loop that contains SEQ that iterates until a compare-and-swap
7133 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7134 a set of instructions that takes a value from OLD_REG as an input and
7135 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7136 set to the current contents of MEM. After SEQ, a compare-and-swap will
7137 attempt to update MEM with NEW_REG. The function returns true when the
7138 loop was generated successfully. */
7139
7140 static bool
7141 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7142 {
7143 machine_mode mode = GET_MODE (mem);
7144 rtx_code_label *label;
7145 rtx cmp_reg, success, oldval;
7146
7147 /* The loop we want to generate looks like
7148
7149 cmp_reg = mem;
7150 label:
7151 old_reg = cmp_reg;
7152 seq;
7153 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7154 if (success)
7155 goto label;
7156
7157 Note that we only do the plain load from memory once. Subsequent
7158 iterations use the value loaded by the compare-and-swap pattern. */
7159
7160 label = gen_label_rtx ();
7161 cmp_reg = gen_reg_rtx (mode);
7162
7163 emit_move_insn (cmp_reg, mem);
7164 emit_label (label);
7165 emit_move_insn (old_reg, cmp_reg);
7166 if (seq)
7167 emit_insn (seq);
7168
7169 success = NULL_RTX;
7170 oldval = cmp_reg;
7171 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7172 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
7173 MEMMODEL_RELAXED))
7174 return false;
7175
7176 if (oldval != cmp_reg)
7177 emit_move_insn (cmp_reg, oldval);
7178
7179 /* Mark this jump predicted not taken. */
7180 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7181 GET_MODE (success), 1, label, 0);
7182 return true;
7183 }
7184
7185
7186 /* This function tries to emit an atomic_exchange intruction. VAL is written
7187 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7188 using TARGET if possible. */
7189
7190 static rtx
7191 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7192 {
7193 machine_mode mode = GET_MODE (mem);
7194 enum insn_code icode;
7195
7196 /* If the target supports the exchange directly, great. */
7197 icode = direct_optab_handler (atomic_exchange_optab, mode);
7198 if (icode != CODE_FOR_nothing)
7199 {
7200 struct expand_operand ops[4];
7201
7202 create_output_operand (&ops[0], target, mode);
7203 create_fixed_operand (&ops[1], mem);
7204 create_input_operand (&ops[2], val, mode);
7205 create_integer_operand (&ops[3], model);
7206 if (maybe_expand_insn (icode, 4, ops))
7207 return ops[0].value;
7208 }
7209
7210 return NULL_RTX;
7211 }
7212
7213 /* This function tries to implement an atomic exchange operation using
7214 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7215 The previous contents of *MEM are returned, using TARGET if possible.
7216 Since this instructionn is an acquire barrier only, stronger memory
7217 models may require additional barriers to be emitted. */
7218
7219 static rtx
7220 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7221 enum memmodel model)
7222 {
7223 machine_mode mode = GET_MODE (mem);
7224 enum insn_code icode;
7225 rtx_insn *last_insn = get_last_insn ();
7226
7227 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7228
7229 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7230 exists, and the memory model is stronger than acquire, add a release
7231 barrier before the instruction. */
7232
7233 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
7234 expand_mem_thread_fence (model);
7235
7236 if (icode != CODE_FOR_nothing)
7237 {
7238 struct expand_operand ops[3];
7239 create_output_operand (&ops[0], target, mode);
7240 create_fixed_operand (&ops[1], mem);
7241 create_input_operand (&ops[2], val, mode);
7242 if (maybe_expand_insn (icode, 3, ops))
7243 return ops[0].value;
7244 }
7245
7246 /* If an external test-and-set libcall is provided, use that instead of
7247 any external compare-and-swap that we might get from the compare-and-
7248 swap-loop expansion later. */
7249 if (!can_compare_and_swap_p (mode, false))
7250 {
7251 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7252 if (libfunc != NULL)
7253 {
7254 rtx addr;
7255
7256 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7257 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7258 mode, 2, addr, ptr_mode,
7259 val, mode);
7260 }
7261 }
7262
7263 /* If the test_and_set can't be emitted, eliminate any barrier that might
7264 have been emitted. */
7265 delete_insns_since (last_insn);
7266 return NULL_RTX;
7267 }
7268
7269 /* This function tries to implement an atomic exchange operation using a
7270 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7271 *MEM are returned, using TARGET if possible. No memory model is required
7272 since a compare_and_swap loop is seq-cst. */
7273
7274 static rtx
7275 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7276 {
7277 machine_mode mode = GET_MODE (mem);
7278
7279 if (can_compare_and_swap_p (mode, true))
7280 {
7281 if (!target || !register_operand (target, mode))
7282 target = gen_reg_rtx (mode);
7283 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7284 return target;
7285 }
7286
7287 return NULL_RTX;
7288 }
7289
7290 /* This function tries to implement an atomic test-and-set operation
7291 using the atomic_test_and_set instruction pattern. A boolean value
7292 is returned from the operation, using TARGET if possible. */
7293
7294 #ifndef HAVE_atomic_test_and_set
7295 #define HAVE_atomic_test_and_set 0
7296 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7297 #endif
7298
7299 static rtx
7300 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7301 {
7302 machine_mode pat_bool_mode;
7303 struct expand_operand ops[3];
7304
7305 if (!HAVE_atomic_test_and_set)
7306 return NULL_RTX;
7307
7308 /* While we always get QImode from __atomic_test_and_set, we get
7309 other memory modes from __sync_lock_test_and_set. Note that we
7310 use no endian adjustment here. This matches the 4.6 behavior
7311 in the Sparc backend. */
7312 gcc_checking_assert
7313 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7314 if (GET_MODE (mem) != QImode)
7315 mem = adjust_address_nv (mem, QImode, 0);
7316
7317 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7318 create_output_operand (&ops[0], target, pat_bool_mode);
7319 create_fixed_operand (&ops[1], mem);
7320 create_integer_operand (&ops[2], model);
7321
7322 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7323 return ops[0].value;
7324 return NULL_RTX;
7325 }
7326
7327 /* This function expands the legacy _sync_lock test_and_set operation which is
7328 generally an atomic exchange. Some limited targets only allow the
7329 constant 1 to be stored. This is an ACQUIRE operation.
7330
7331 TARGET is an optional place to stick the return value.
7332 MEM is where VAL is stored. */
7333
7334 rtx
7335 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7336 {
7337 rtx ret;
7338
7339 /* Try an atomic_exchange first. */
7340 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
7341 if (ret)
7342 return ret;
7343
7344 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
7345 MEMMODEL_SYNC_ACQUIRE);
7346 if (ret)
7347 return ret;
7348
7349 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7350 if (ret)
7351 return ret;
7352
7353 /* If there are no other options, try atomic_test_and_set if the value
7354 being stored is 1. */
7355 if (val == const1_rtx)
7356 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
7357
7358 return ret;
7359 }
7360
7361 /* This function expands the atomic test_and_set operation:
7362 atomically store a boolean TRUE into MEM and return the previous value.
7363
7364 MEMMODEL is the memory model variant to use.
7365 TARGET is an optional place to stick the return value. */
7366
7367 rtx
7368 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7369 {
7370 machine_mode mode = GET_MODE (mem);
7371 rtx ret, trueval, subtarget;
7372
7373 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7374 if (ret)
7375 return ret;
7376
7377 /* Be binary compatible with non-default settings of trueval, and different
7378 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7379 another only has atomic-exchange. */
7380 if (targetm.atomic_test_and_set_trueval == 1)
7381 {
7382 trueval = const1_rtx;
7383 subtarget = target ? target : gen_reg_rtx (mode);
7384 }
7385 else
7386 {
7387 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7388 subtarget = gen_reg_rtx (mode);
7389 }
7390
7391 /* Try the atomic-exchange optab... */
7392 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7393
7394 /* ... then an atomic-compare-and-swap loop ... */
7395 if (!ret)
7396 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7397
7398 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7399 if (!ret)
7400 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7401
7402 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7403 things with the value 1. Thus we try again without trueval. */
7404 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7405 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7406
7407 /* Failing all else, assume a single threaded environment and simply
7408 perform the operation. */
7409 if (!ret)
7410 {
7411 /* If the result is ignored skip the move to target. */
7412 if (subtarget != const0_rtx)
7413 emit_move_insn (subtarget, mem);
7414
7415 emit_move_insn (mem, trueval);
7416 ret = subtarget;
7417 }
7418
7419 /* Recall that have to return a boolean value; rectify if trueval
7420 is not exactly one. */
7421 if (targetm.atomic_test_and_set_trueval != 1)
7422 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7423
7424 return ret;
7425 }
7426
7427 /* This function expands the atomic exchange operation:
7428 atomically store VAL in MEM and return the previous value in MEM.
7429
7430 MEMMODEL is the memory model variant to use.
7431 TARGET is an optional place to stick the return value. */
7432
7433 rtx
7434 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7435 {
7436 rtx ret;
7437
7438 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7439
7440 /* Next try a compare-and-swap loop for the exchange. */
7441 if (!ret)
7442 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7443
7444 return ret;
7445 }
7446
7447 /* This function expands the atomic compare exchange operation:
7448
7449 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7450 *PTARGET_OVAL is an optional place to store the old value from memory.
7451 Both target parameters may be NULL to indicate that we do not care about
7452 that return value. Both target parameters are updated on success to
7453 the actual location of the corresponding result.
7454
7455 MEMMODEL is the memory model variant to use.
7456
7457 The return value of the function is true for success. */
7458
7459 bool
7460 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7461 rtx mem, rtx expected, rtx desired,
7462 bool is_weak, enum memmodel succ_model,
7463 enum memmodel fail_model)
7464 {
7465 machine_mode mode = GET_MODE (mem);
7466 struct expand_operand ops[8];
7467 enum insn_code icode;
7468 rtx target_oval, target_bool = NULL_RTX;
7469 rtx libfunc;
7470
7471 /* Load expected into a register for the compare and swap. */
7472 if (MEM_P (expected))
7473 expected = copy_to_reg (expected);
7474
7475 /* Make sure we always have some place to put the return oldval.
7476 Further, make sure that place is distinct from the input expected,
7477 just in case we need that path down below. */
7478 if (ptarget_oval == NULL
7479 || (target_oval = *ptarget_oval) == NULL
7480 || reg_overlap_mentioned_p (expected, target_oval))
7481 target_oval = gen_reg_rtx (mode);
7482
7483 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7484 if (icode != CODE_FOR_nothing)
7485 {
7486 machine_mode bool_mode = insn_data[icode].operand[0].mode;
7487
7488 /* Make sure we always have a place for the bool operand. */
7489 if (ptarget_bool == NULL
7490 || (target_bool = *ptarget_bool) == NULL
7491 || GET_MODE (target_bool) != bool_mode)
7492 target_bool = gen_reg_rtx (bool_mode);
7493
7494 /* Emit the compare_and_swap. */
7495 create_output_operand (&ops[0], target_bool, bool_mode);
7496 create_output_operand (&ops[1], target_oval, mode);
7497 create_fixed_operand (&ops[2], mem);
7498 create_input_operand (&ops[3], expected, mode);
7499 create_input_operand (&ops[4], desired, mode);
7500 create_integer_operand (&ops[5], is_weak);
7501 create_integer_operand (&ops[6], succ_model);
7502 create_integer_operand (&ops[7], fail_model);
7503 if (maybe_expand_insn (icode, 8, ops))
7504 {
7505 /* Return success/failure. */
7506 target_bool = ops[0].value;
7507 target_oval = ops[1].value;
7508 goto success;
7509 }
7510 }
7511
7512 /* Otherwise fall back to the original __sync_val_compare_and_swap
7513 which is always seq-cst. */
7514 icode = optab_handler (sync_compare_and_swap_optab, mode);
7515 if (icode != CODE_FOR_nothing)
7516 {
7517 rtx cc_reg;
7518
7519 create_output_operand (&ops[0], target_oval, mode);
7520 create_fixed_operand (&ops[1], mem);
7521 create_input_operand (&ops[2], expected, mode);
7522 create_input_operand (&ops[3], desired, mode);
7523 if (!maybe_expand_insn (icode, 4, ops))
7524 return false;
7525
7526 target_oval = ops[0].value;
7527
7528 /* If the caller isn't interested in the boolean return value,
7529 skip the computation of it. */
7530 if (ptarget_bool == NULL)
7531 goto success;
7532
7533 /* Otherwise, work out if the compare-and-swap succeeded. */
7534 cc_reg = NULL_RTX;
7535 if (have_insn_for (COMPARE, CCmode))
7536 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7537 if (cc_reg)
7538 {
7539 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7540 const0_rtx, VOIDmode, 0, 1);
7541 goto success;
7542 }
7543 goto success_bool_from_val;
7544 }
7545
7546 /* Also check for library support for __sync_val_compare_and_swap. */
7547 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7548 if (libfunc != NULL)
7549 {
7550 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7551 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7552 mode, 3, addr, ptr_mode,
7553 expected, mode, desired, mode);
7554
7555 /* Compute the boolean return value only if requested. */
7556 if (ptarget_bool)
7557 goto success_bool_from_val;
7558 else
7559 goto success;
7560 }
7561
7562 /* Failure. */
7563 return false;
7564
7565 success_bool_from_val:
7566 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7567 expected, VOIDmode, 1, 1);
7568 success:
7569 /* Make sure that the oval output winds up where the caller asked. */
7570 if (ptarget_oval)
7571 *ptarget_oval = target_oval;
7572 if (ptarget_bool)
7573 *ptarget_bool = target_bool;
7574 return true;
7575 }
7576
7577 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7578
7579 static void
7580 expand_asm_memory_barrier (void)
7581 {
7582 rtx asm_op, clob;
7583
7584 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7585 rtvec_alloc (0), rtvec_alloc (0),
7586 rtvec_alloc (0), UNKNOWN_LOCATION);
7587 MEM_VOLATILE_P (asm_op) = 1;
7588
7589 clob = gen_rtx_SCRATCH (VOIDmode);
7590 clob = gen_rtx_MEM (BLKmode, clob);
7591 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7592
7593 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7594 }
7595
7596 /* This routine will either emit the mem_thread_fence pattern or issue a
7597 sync_synchronize to generate a fence for memory model MEMMODEL. */
7598
7599 #ifndef HAVE_mem_thread_fence
7600 # define HAVE_mem_thread_fence 0
7601 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
7602 #endif
7603 #ifndef HAVE_memory_barrier
7604 # define HAVE_memory_barrier 0
7605 # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
7606 #endif
7607
7608 void
7609 expand_mem_thread_fence (enum memmodel model)
7610 {
7611 if (HAVE_mem_thread_fence)
7612 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7613 else if (!is_mm_relaxed (model))
7614 {
7615 if (HAVE_memory_barrier)
7616 emit_insn (gen_memory_barrier ());
7617 else if (synchronize_libfunc != NULL_RTX)
7618 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7619 else
7620 expand_asm_memory_barrier ();
7621 }
7622 }
7623
7624 /* This routine will either emit the mem_signal_fence pattern or issue a
7625 sync_synchronize to generate a fence for memory model MEMMODEL. */
7626
7627 #ifndef HAVE_mem_signal_fence
7628 # define HAVE_mem_signal_fence 0
7629 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
7630 #endif
7631
7632 void
7633 expand_mem_signal_fence (enum memmodel model)
7634 {
7635 if (HAVE_mem_signal_fence)
7636 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7637 else if (!is_mm_relaxed (model))
7638 {
7639 /* By default targets are coherent between a thread and the signal
7640 handler running on the same thread. Thus this really becomes a
7641 compiler barrier, in that stores must not be sunk past
7642 (or raised above) a given point. */
7643 expand_asm_memory_barrier ();
7644 }
7645 }
7646
7647 /* This function expands the atomic load operation:
7648 return the atomically loaded value in MEM.
7649
7650 MEMMODEL is the memory model variant to use.
7651 TARGET is an option place to stick the return value. */
7652
7653 rtx
7654 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7655 {
7656 machine_mode mode = GET_MODE (mem);
7657 enum insn_code icode;
7658
7659 /* If the target supports the load directly, great. */
7660 icode = direct_optab_handler (atomic_load_optab, mode);
7661 if (icode != CODE_FOR_nothing)
7662 {
7663 struct expand_operand ops[3];
7664
7665 create_output_operand (&ops[0], target, mode);
7666 create_fixed_operand (&ops[1], mem);
7667 create_integer_operand (&ops[2], model);
7668 if (maybe_expand_insn (icode, 3, ops))
7669 return ops[0].value;
7670 }
7671
7672 /* If the size of the object is greater than word size on this target,
7673 then we assume that a load will not be atomic. */
7674 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7675 {
7676 /* Issue val = compare_and_swap (mem, 0, 0).
7677 This may cause the occasional harmless store of 0 when the value is
7678 already 0, but it seems to be OK according to the standards guys. */
7679 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7680 const0_rtx, false, model, model))
7681 return target;
7682 else
7683 /* Otherwise there is no atomic load, leave the library call. */
7684 return NULL_RTX;
7685 }
7686
7687 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7688 if (!target || target == const0_rtx)
7689 target = gen_reg_rtx (mode);
7690
7691 /* For SEQ_CST, emit a barrier before the load. */
7692 if (is_mm_seq_cst (model))
7693 expand_mem_thread_fence (model);
7694
7695 emit_move_insn (target, mem);
7696
7697 /* Emit the appropriate barrier after the load. */
7698 expand_mem_thread_fence (model);
7699
7700 return target;
7701 }
7702
7703 /* This function expands the atomic store operation:
7704 Atomically store VAL in MEM.
7705 MEMMODEL is the memory model variant to use.
7706 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7707 function returns const0_rtx if a pattern was emitted. */
7708
7709 rtx
7710 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7711 {
7712 machine_mode mode = GET_MODE (mem);
7713 enum insn_code icode;
7714 struct expand_operand ops[3];
7715
7716 /* If the target supports the store directly, great. */
7717 icode = direct_optab_handler (atomic_store_optab, mode);
7718 if (icode != CODE_FOR_nothing)
7719 {
7720 create_fixed_operand (&ops[0], mem);
7721 create_input_operand (&ops[1], val, mode);
7722 create_integer_operand (&ops[2], model);
7723 if (maybe_expand_insn (icode, 3, ops))
7724 return const0_rtx;
7725 }
7726
7727 /* If using __sync_lock_release is a viable alternative, try it. */
7728 if (use_release)
7729 {
7730 icode = direct_optab_handler (sync_lock_release_optab, mode);
7731 if (icode != CODE_FOR_nothing)
7732 {
7733 create_fixed_operand (&ops[0], mem);
7734 create_input_operand (&ops[1], const0_rtx, mode);
7735 if (maybe_expand_insn (icode, 2, ops))
7736 {
7737 /* lock_release is only a release barrier. */
7738 if (is_mm_seq_cst (model))
7739 expand_mem_thread_fence (model);
7740 return const0_rtx;
7741 }
7742 }
7743 }
7744
7745 /* If the size of the object is greater than word size on this target,
7746 a default store will not be atomic, Try a mem_exchange and throw away
7747 the result. If that doesn't work, don't do anything. */
7748 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7749 {
7750 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7751 if (!target)
7752 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7753 if (target)
7754 return const0_rtx;
7755 else
7756 return NULL_RTX;
7757 }
7758
7759 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7760 expand_mem_thread_fence (model);
7761
7762 emit_move_insn (mem, val);
7763
7764 /* For SEQ_CST, also emit a barrier after the store. */
7765 if (is_mm_seq_cst (model))
7766 expand_mem_thread_fence (model);
7767
7768 return const0_rtx;
7769 }
7770
7771
7772 /* Structure containing the pointers and values required to process the
7773 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7774
7775 struct atomic_op_functions
7776 {
7777 direct_optab mem_fetch_before;
7778 direct_optab mem_fetch_after;
7779 direct_optab mem_no_result;
7780 optab fetch_before;
7781 optab fetch_after;
7782 direct_optab no_result;
7783 enum rtx_code reverse_code;
7784 };
7785
7786
7787 /* Fill in structure pointed to by OP with the various optab entries for an
7788 operation of type CODE. */
7789
7790 static void
7791 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7792 {
7793 gcc_assert (op!= NULL);
7794
7795 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7796 in the source code during compilation, and the optab entries are not
7797 computable until runtime. Fill in the values at runtime. */
7798 switch (code)
7799 {
7800 case PLUS:
7801 op->mem_fetch_before = atomic_fetch_add_optab;
7802 op->mem_fetch_after = atomic_add_fetch_optab;
7803 op->mem_no_result = atomic_add_optab;
7804 op->fetch_before = sync_old_add_optab;
7805 op->fetch_after = sync_new_add_optab;
7806 op->no_result = sync_add_optab;
7807 op->reverse_code = MINUS;
7808 break;
7809 case MINUS:
7810 op->mem_fetch_before = atomic_fetch_sub_optab;
7811 op->mem_fetch_after = atomic_sub_fetch_optab;
7812 op->mem_no_result = atomic_sub_optab;
7813 op->fetch_before = sync_old_sub_optab;
7814 op->fetch_after = sync_new_sub_optab;
7815 op->no_result = sync_sub_optab;
7816 op->reverse_code = PLUS;
7817 break;
7818 case XOR:
7819 op->mem_fetch_before = atomic_fetch_xor_optab;
7820 op->mem_fetch_after = atomic_xor_fetch_optab;
7821 op->mem_no_result = atomic_xor_optab;
7822 op->fetch_before = sync_old_xor_optab;
7823 op->fetch_after = sync_new_xor_optab;
7824 op->no_result = sync_xor_optab;
7825 op->reverse_code = XOR;
7826 break;
7827 case AND:
7828 op->mem_fetch_before = atomic_fetch_and_optab;
7829 op->mem_fetch_after = atomic_and_fetch_optab;
7830 op->mem_no_result = atomic_and_optab;
7831 op->fetch_before = sync_old_and_optab;
7832 op->fetch_after = sync_new_and_optab;
7833 op->no_result = sync_and_optab;
7834 op->reverse_code = UNKNOWN;
7835 break;
7836 case IOR:
7837 op->mem_fetch_before = atomic_fetch_or_optab;
7838 op->mem_fetch_after = atomic_or_fetch_optab;
7839 op->mem_no_result = atomic_or_optab;
7840 op->fetch_before = sync_old_ior_optab;
7841 op->fetch_after = sync_new_ior_optab;
7842 op->no_result = sync_ior_optab;
7843 op->reverse_code = UNKNOWN;
7844 break;
7845 case NOT:
7846 op->mem_fetch_before = atomic_fetch_nand_optab;
7847 op->mem_fetch_after = atomic_nand_fetch_optab;
7848 op->mem_no_result = atomic_nand_optab;
7849 op->fetch_before = sync_old_nand_optab;
7850 op->fetch_after = sync_new_nand_optab;
7851 op->no_result = sync_nand_optab;
7852 op->reverse_code = UNKNOWN;
7853 break;
7854 default:
7855 gcc_unreachable ();
7856 }
7857 }
7858
7859 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7860 using memory order MODEL. If AFTER is true the operation needs to return
7861 the value of *MEM after the operation, otherwise the previous value.
7862 TARGET is an optional place to place the result. The result is unused if
7863 it is const0_rtx.
7864 Return the result if there is a better sequence, otherwise NULL_RTX. */
7865
7866 static rtx
7867 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7868 enum memmodel model, bool after)
7869 {
7870 /* If the value is prefetched, or not used, it may be possible to replace
7871 the sequence with a native exchange operation. */
7872 if (!after || target == const0_rtx)
7873 {
7874 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7875 if (code == AND && val == const0_rtx)
7876 {
7877 if (target == const0_rtx)
7878 target = gen_reg_rtx (GET_MODE (mem));
7879 return maybe_emit_atomic_exchange (target, mem, val, model);
7880 }
7881
7882 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7883 if (code == IOR && val == constm1_rtx)
7884 {
7885 if (target == const0_rtx)
7886 target = gen_reg_rtx (GET_MODE (mem));
7887 return maybe_emit_atomic_exchange (target, mem, val, model);
7888 }
7889 }
7890
7891 return NULL_RTX;
7892 }
7893
7894 /* Try to emit an instruction for a specific operation varaition.
7895 OPTAB contains the OP functions.
7896 TARGET is an optional place to return the result. const0_rtx means unused.
7897 MEM is the memory location to operate on.
7898 VAL is the value to use in the operation.
7899 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7900 MODEL is the memory model, if used.
7901 AFTER is true if the returned result is the value after the operation. */
7902
7903 static rtx
7904 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7905 rtx val, bool use_memmodel, enum memmodel model, bool after)
7906 {
7907 machine_mode mode = GET_MODE (mem);
7908 struct expand_operand ops[4];
7909 enum insn_code icode;
7910 int op_counter = 0;
7911 int num_ops;
7912
7913 /* Check to see if there is a result returned. */
7914 if (target == const0_rtx)
7915 {
7916 if (use_memmodel)
7917 {
7918 icode = direct_optab_handler (optab->mem_no_result, mode);
7919 create_integer_operand (&ops[2], model);
7920 num_ops = 3;
7921 }
7922 else
7923 {
7924 icode = direct_optab_handler (optab->no_result, mode);
7925 num_ops = 2;
7926 }
7927 }
7928 /* Otherwise, we need to generate a result. */
7929 else
7930 {
7931 if (use_memmodel)
7932 {
7933 icode = direct_optab_handler (after ? optab->mem_fetch_after
7934 : optab->mem_fetch_before, mode);
7935 create_integer_operand (&ops[3], model);
7936 num_ops = 4;
7937 }
7938 else
7939 {
7940 icode = optab_handler (after ? optab->fetch_after
7941 : optab->fetch_before, mode);
7942 num_ops = 3;
7943 }
7944 create_output_operand (&ops[op_counter++], target, mode);
7945 }
7946 if (icode == CODE_FOR_nothing)
7947 return NULL_RTX;
7948
7949 create_fixed_operand (&ops[op_counter++], mem);
7950 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7951 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7952
7953 if (maybe_expand_insn (icode, num_ops, ops))
7954 return (target == const0_rtx ? const0_rtx : ops[0].value);
7955
7956 return NULL_RTX;
7957 }
7958
7959
7960 /* This function expands an atomic fetch_OP or OP_fetch operation:
7961 TARGET is an option place to stick the return value. const0_rtx indicates
7962 the result is unused.
7963 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7964 CODE is the operation being performed (OP)
7965 MEMMODEL is the memory model variant to use.
7966 AFTER is true to return the result of the operation (OP_fetch).
7967 AFTER is false to return the value before the operation (fetch_OP).
7968
7969 This function will *only* generate instructions if there is a direct
7970 optab. No compare and swap loops or libcalls will be generated. */
7971
7972 static rtx
7973 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7974 enum rtx_code code, enum memmodel model,
7975 bool after)
7976 {
7977 machine_mode mode = GET_MODE (mem);
7978 struct atomic_op_functions optab;
7979 rtx result;
7980 bool unused_result = (target == const0_rtx);
7981
7982 get_atomic_op_for_code (&optab, code);
7983
7984 /* Check to see if there are any better instructions. */
7985 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7986 if (result)
7987 return result;
7988
7989 /* Check for the case where the result isn't used and try those patterns. */
7990 if (unused_result)
7991 {
7992 /* Try the memory model variant first. */
7993 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7994 if (result)
7995 return result;
7996
7997 /* Next try the old style withuot a memory model. */
7998 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7999 if (result)
8000 return result;
8001
8002 /* There is no no-result pattern, so try patterns with a result. */
8003 target = NULL_RTX;
8004 }
8005
8006 /* Try the __atomic version. */
8007 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
8008 if (result)
8009 return result;
8010
8011 /* Try the older __sync version. */
8012 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
8013 if (result)
8014 return result;
8015
8016 /* If the fetch value can be calculated from the other variation of fetch,
8017 try that operation. */
8018 if (after || unused_result || optab.reverse_code != UNKNOWN)
8019 {
8020 /* Try the __atomic version, then the older __sync version. */
8021 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
8022 if (!result)
8023 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
8024
8025 if (result)
8026 {
8027 /* If the result isn't used, no need to do compensation code. */
8028 if (unused_result)
8029 return result;
8030
8031 /* Issue compensation code. Fetch_after == fetch_before OP val.
8032 Fetch_before == after REVERSE_OP val. */
8033 if (!after)
8034 code = optab.reverse_code;
8035 if (code == NOT)
8036 {
8037 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
8038 true, OPTAB_LIB_WIDEN);
8039 result = expand_simple_unop (mode, NOT, result, target, true);
8040 }
8041 else
8042 result = expand_simple_binop (mode, code, result, val, target,
8043 true, OPTAB_LIB_WIDEN);
8044 return result;
8045 }
8046 }
8047
8048 /* No direct opcode can be generated. */
8049 return NULL_RTX;
8050 }
8051
8052
8053
8054 /* This function expands an atomic fetch_OP or OP_fetch operation:
8055 TARGET is an option place to stick the return value. const0_rtx indicates
8056 the result is unused.
8057 atomically fetch MEM, perform the operation with VAL and return it to MEM.
8058 CODE is the operation being performed (OP)
8059 MEMMODEL is the memory model variant to use.
8060 AFTER is true to return the result of the operation (OP_fetch).
8061 AFTER is false to return the value before the operation (fetch_OP). */
8062 rtx
8063 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
8064 enum memmodel model, bool after)
8065 {
8066 machine_mode mode = GET_MODE (mem);
8067 rtx result;
8068 bool unused_result = (target == const0_rtx);
8069
8070 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
8071 after);
8072
8073 if (result)
8074 return result;
8075
8076 /* Add/sub can be implemented by doing the reverse operation with -(val). */
8077 if (code == PLUS || code == MINUS)
8078 {
8079 rtx tmp;
8080 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
8081
8082 start_sequence ();
8083 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
8084 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
8085 model, after);
8086 if (result)
8087 {
8088 /* PLUS worked so emit the insns and return. */
8089 tmp = get_insns ();
8090 end_sequence ();
8091 emit_insn (tmp);
8092 return result;
8093 }
8094
8095 /* PLUS did not work, so throw away the negation code and continue. */
8096 end_sequence ();
8097 }
8098
8099 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8100 if (!can_compare_and_swap_p (mode, false))
8101 {
8102 rtx libfunc;
8103 bool fixup = false;
8104 enum rtx_code orig_code = code;
8105 struct atomic_op_functions optab;
8106
8107 get_atomic_op_for_code (&optab, code);
8108 libfunc = optab_libfunc (after ? optab.fetch_after
8109 : optab.fetch_before, mode);
8110 if (libfunc == NULL
8111 && (after || unused_result || optab.reverse_code != UNKNOWN))
8112 {
8113 fixup = true;
8114 if (!after)
8115 code = optab.reverse_code;
8116 libfunc = optab_libfunc (after ? optab.fetch_before
8117 : optab.fetch_after, mode);
8118 }
8119 if (libfunc != NULL)
8120 {
8121 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8122 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8123 2, addr, ptr_mode, val, mode);
8124
8125 if (!unused_result && fixup)
8126 result = expand_simple_binop (mode, code, result, val, target,
8127 true, OPTAB_LIB_WIDEN);
8128 return result;
8129 }
8130
8131 /* We need the original code for any further attempts. */
8132 code = orig_code;
8133 }
8134
8135 /* If nothing else has succeeded, default to a compare and swap loop. */
8136 if (can_compare_and_swap_p (mode, true))
8137 {
8138 rtx_insn *insn;
8139 rtx t0 = gen_reg_rtx (mode), t1;
8140
8141 start_sequence ();
8142
8143 /* If the result is used, get a register for it. */
8144 if (!unused_result)
8145 {
8146 if (!target || !register_operand (target, mode))
8147 target = gen_reg_rtx (mode);
8148 /* If fetch_before, copy the value now. */
8149 if (!after)
8150 emit_move_insn (target, t0);
8151 }
8152 else
8153 target = const0_rtx;
8154
8155 t1 = t0;
8156 if (code == NOT)
8157 {
8158 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8159 true, OPTAB_LIB_WIDEN);
8160 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8161 }
8162 else
8163 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8164 OPTAB_LIB_WIDEN);
8165
8166 /* For after, copy the value now. */
8167 if (!unused_result && after)
8168 emit_move_insn (target, t1);
8169 insn = get_insns ();
8170 end_sequence ();
8171
8172 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8173 return target;
8174 }
8175
8176 return NULL_RTX;
8177 }
8178 \f
8179 /* Return true if OPERAND is suitable for operand number OPNO of
8180 instruction ICODE. */
8181
8182 bool
8183 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8184 {
8185 return (!insn_data[(int) icode].operand[opno].predicate
8186 || (insn_data[(int) icode].operand[opno].predicate
8187 (operand, insn_data[(int) icode].operand[opno].mode)));
8188 }
8189 \f
8190 /* TARGET is a target of a multiword operation that we are going to
8191 implement as a series of word-mode operations. Return true if
8192 TARGET is suitable for this purpose. */
8193
8194 bool
8195 valid_multiword_target_p (rtx target)
8196 {
8197 machine_mode mode;
8198 int i;
8199
8200 mode = GET_MODE (target);
8201 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8202 if (!validate_subreg (word_mode, mode, target, i))
8203 return false;
8204 return true;
8205 }
8206
8207 /* Like maybe_legitimize_operand, but do not change the code of the
8208 current rtx value. */
8209
8210 static bool
8211 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8212 struct expand_operand *op)
8213 {
8214 /* See if the operand matches in its current form. */
8215 if (insn_operand_matches (icode, opno, op->value))
8216 return true;
8217
8218 /* If the operand is a memory whose address has no side effects,
8219 try forcing the address into a non-virtual pseudo register.
8220 The check for side effects is important because copy_to_mode_reg
8221 cannot handle things like auto-modified addresses. */
8222 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8223 {
8224 rtx addr, mem;
8225
8226 mem = op->value;
8227 addr = XEXP (mem, 0);
8228 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8229 && !side_effects_p (addr))
8230 {
8231 rtx_insn *last;
8232 machine_mode mode;
8233
8234 last = get_last_insn ();
8235 mode = get_address_mode (mem);
8236 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8237 if (insn_operand_matches (icode, opno, mem))
8238 {
8239 op->value = mem;
8240 return true;
8241 }
8242 delete_insns_since (last);
8243 }
8244 }
8245
8246 return false;
8247 }
8248
8249 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8250 on success, storing the new operand value back in OP. */
8251
8252 static bool
8253 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8254 struct expand_operand *op)
8255 {
8256 machine_mode mode, imode;
8257 bool old_volatile_ok, result;
8258
8259 mode = op->mode;
8260 switch (op->type)
8261 {
8262 case EXPAND_FIXED:
8263 old_volatile_ok = volatile_ok;
8264 volatile_ok = true;
8265 result = maybe_legitimize_operand_same_code (icode, opno, op);
8266 volatile_ok = old_volatile_ok;
8267 return result;
8268
8269 case EXPAND_OUTPUT:
8270 gcc_assert (mode != VOIDmode);
8271 if (op->value
8272 && op->value != const0_rtx
8273 && GET_MODE (op->value) == mode
8274 && maybe_legitimize_operand_same_code (icode, opno, op))
8275 return true;
8276
8277 op->value = gen_reg_rtx (mode);
8278 break;
8279
8280 case EXPAND_INPUT:
8281 input:
8282 gcc_assert (mode != VOIDmode);
8283 gcc_assert (GET_MODE (op->value) == VOIDmode
8284 || GET_MODE (op->value) == mode);
8285 if (maybe_legitimize_operand_same_code (icode, opno, op))
8286 return true;
8287
8288 op->value = copy_to_mode_reg (mode, op->value);
8289 break;
8290
8291 case EXPAND_CONVERT_TO:
8292 gcc_assert (mode != VOIDmode);
8293 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8294 goto input;
8295
8296 case EXPAND_CONVERT_FROM:
8297 if (GET_MODE (op->value) != VOIDmode)
8298 mode = GET_MODE (op->value);
8299 else
8300 /* The caller must tell us what mode this value has. */
8301 gcc_assert (mode != VOIDmode);
8302
8303 imode = insn_data[(int) icode].operand[opno].mode;
8304 if (imode != VOIDmode && imode != mode)
8305 {
8306 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8307 mode = imode;
8308 }
8309 goto input;
8310
8311 case EXPAND_ADDRESS:
8312 gcc_assert (mode != VOIDmode);
8313 op->value = convert_memory_address (mode, op->value);
8314 goto input;
8315
8316 case EXPAND_INTEGER:
8317 mode = insn_data[(int) icode].operand[opno].mode;
8318 if (mode != VOIDmode && const_int_operand (op->value, mode))
8319 goto input;
8320 break;
8321 }
8322 return insn_operand_matches (icode, opno, op->value);
8323 }
8324
8325 /* Make OP describe an input operand that should have the same value
8326 as VALUE, after any mode conversion that the target might request.
8327 TYPE is the type of VALUE. */
8328
8329 void
8330 create_convert_operand_from_type (struct expand_operand *op,
8331 rtx value, tree type)
8332 {
8333 create_convert_operand_from (op, value, TYPE_MODE (type),
8334 TYPE_UNSIGNED (type));
8335 }
8336
8337 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8338 of instruction ICODE. Return true on success, leaving the new operand
8339 values in the OPS themselves. Emit no code on failure. */
8340
8341 bool
8342 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8343 unsigned int nops, struct expand_operand *ops)
8344 {
8345 rtx_insn *last;
8346 unsigned int i;
8347
8348 last = get_last_insn ();
8349 for (i = 0; i < nops; i++)
8350 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8351 {
8352 delete_insns_since (last);
8353 return false;
8354 }
8355 return true;
8356 }
8357
8358 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8359 as its operands. Return the instruction pattern on success,
8360 and emit any necessary set-up code. Return null and emit no
8361 code on failure. */
8362
8363 rtx
8364 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8365 struct expand_operand *ops)
8366 {
8367 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8368 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8369 return NULL_RTX;
8370
8371 switch (nops)
8372 {
8373 case 1:
8374 return GEN_FCN (icode) (ops[0].value);
8375 case 2:
8376 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8377 case 3:
8378 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8379 case 4:
8380 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8381 ops[3].value);
8382 case 5:
8383 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8384 ops[3].value, ops[4].value);
8385 case 6:
8386 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8387 ops[3].value, ops[4].value, ops[5].value);
8388 case 7:
8389 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8390 ops[3].value, ops[4].value, ops[5].value,
8391 ops[6].value);
8392 case 8:
8393 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8394 ops[3].value, ops[4].value, ops[5].value,
8395 ops[6].value, ops[7].value);
8396 case 9:
8397 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8398 ops[3].value, ops[4].value, ops[5].value,
8399 ops[6].value, ops[7].value, ops[8].value);
8400 }
8401 gcc_unreachable ();
8402 }
8403
8404 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8405 as its operands. Return true on success and emit no code on failure. */
8406
8407 bool
8408 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8409 struct expand_operand *ops)
8410 {
8411 rtx pat = maybe_gen_insn (icode, nops, ops);
8412 if (pat)
8413 {
8414 emit_insn (pat);
8415 return true;
8416 }
8417 return false;
8418 }
8419
8420 /* Like maybe_expand_insn, but for jumps. */
8421
8422 bool
8423 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8424 struct expand_operand *ops)
8425 {
8426 rtx pat = maybe_gen_insn (icode, nops, ops);
8427 if (pat)
8428 {
8429 emit_jump_insn (pat);
8430 return true;
8431 }
8432 return false;
8433 }
8434
8435 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8436 as its operands. */
8437
8438 void
8439 expand_insn (enum insn_code icode, unsigned int nops,
8440 struct expand_operand *ops)
8441 {
8442 if (!maybe_expand_insn (icode, nops, ops))
8443 gcc_unreachable ();
8444 }
8445
8446 /* Like expand_insn, but for jumps. */
8447
8448 void
8449 expand_jump_insn (enum insn_code icode, unsigned int nops,
8450 struct expand_operand *ops)
8451 {
8452 if (!maybe_expand_jump_insn (icode, nops, ops))
8453 gcc_unreachable ();
8454 }
8455
8456 /* Reduce conditional compilation elsewhere. */
8457 #ifndef HAVE_insv
8458 #define HAVE_insv 0
8459 #define CODE_FOR_insv CODE_FOR_nothing
8460 #endif
8461 #ifndef HAVE_extv
8462 #define HAVE_extv 0
8463 #define CODE_FOR_extv CODE_FOR_nothing
8464 #endif
8465 #ifndef HAVE_extzv
8466 #define HAVE_extzv 0
8467 #define CODE_FOR_extzv CODE_FOR_nothing
8468 #endif
8469
8470 /* Enumerates the possible types of structure operand to an
8471 extraction_insn. */
8472 enum extraction_type { ET_unaligned_mem, ET_reg };
8473
8474 /* Check whether insv, extv or extzv pattern ICODE can be used for an
8475 insertion or extraction of type TYPE on a structure of mode MODE.
8476 Return true if so and fill in *INSN accordingly. STRUCT_OP is the
8477 operand number of the structure (the first sign_extract or zero_extract
8478 operand) and FIELD_OP is the operand number of the field (the other
8479 side of the set from the sign_extract or zero_extract). */
8480
8481 static bool
8482 get_traditional_extraction_insn (extraction_insn *insn,
8483 enum extraction_type type,
8484 machine_mode mode,
8485 enum insn_code icode,
8486 int struct_op, int field_op)
8487 {
8488 const struct insn_data_d *data = &insn_data[icode];
8489
8490 machine_mode struct_mode = data->operand[struct_op].mode;
8491 if (struct_mode == VOIDmode)
8492 struct_mode = word_mode;
8493 if (mode != struct_mode)
8494 return false;
8495
8496 machine_mode field_mode = data->operand[field_op].mode;
8497 if (field_mode == VOIDmode)
8498 field_mode = word_mode;
8499
8500 machine_mode pos_mode = data->operand[struct_op + 2].mode;
8501 if (pos_mode == VOIDmode)
8502 pos_mode = word_mode;
8503
8504 insn->icode = icode;
8505 insn->field_mode = field_mode;
8506 insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
8507 insn->pos_mode = pos_mode;
8508 return true;
8509 }
8510
8511 /* Return true if an optab exists to perform an insertion or extraction
8512 of type TYPE in mode MODE. Describe the instruction in *INSN if so.
8513
8514 REG_OPTAB is the optab to use for register structures and
8515 MISALIGN_OPTAB is the optab to use for misaligned memory structures.
8516 POS_OP is the operand number of the bit position. */
8517
8518 static bool
8519 get_optab_extraction_insn (struct extraction_insn *insn,
8520 enum extraction_type type,
8521 machine_mode mode, direct_optab reg_optab,
8522 direct_optab misalign_optab, int pos_op)
8523 {
8524 direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
8525 enum insn_code icode = direct_optab_handler (optab, mode);
8526 if (icode == CODE_FOR_nothing)
8527 return false;
8528
8529 const struct insn_data_d *data = &insn_data[icode];
8530
8531 insn->icode = icode;
8532 insn->field_mode = mode;
8533 insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
8534 insn->pos_mode = data->operand[pos_op].mode;
8535 if (insn->pos_mode == VOIDmode)
8536 insn->pos_mode = word_mode;
8537 return true;
8538 }
8539
8540 /* Return true if an instruction exists to perform an insertion or
8541 extraction (PATTERN says which) of type TYPE in mode MODE.
8542 Describe the instruction in *INSN if so. */
8543
8544 static bool
8545 get_extraction_insn (extraction_insn *insn,
8546 enum extraction_pattern pattern,
8547 enum extraction_type type,
8548 machine_mode mode)
8549 {
8550 switch (pattern)
8551 {
8552 case EP_insv:
8553 if (HAVE_insv
8554 && get_traditional_extraction_insn (insn, type, mode,
8555 CODE_FOR_insv, 0, 3))
8556 return true;
8557 return get_optab_extraction_insn (insn, type, mode, insv_optab,
8558 insvmisalign_optab, 2);
8559
8560 case EP_extv:
8561 if (HAVE_extv
8562 && get_traditional_extraction_insn (insn, type, mode,
8563 CODE_FOR_extv, 1, 0))
8564 return true;
8565 return get_optab_extraction_insn (insn, type, mode, extv_optab,
8566 extvmisalign_optab, 3);
8567
8568 case EP_extzv:
8569 if (HAVE_extzv
8570 && get_traditional_extraction_insn (insn, type, mode,
8571 CODE_FOR_extzv, 1, 0))
8572 return true;
8573 return get_optab_extraction_insn (insn, type, mode, extzv_optab,
8574 extzvmisalign_optab, 3);
8575
8576 default:
8577 gcc_unreachable ();
8578 }
8579 }
8580
8581 /* Return true if an instruction exists to access a field of mode
8582 FIELDMODE in a structure that has STRUCT_BITS significant bits.
8583 Describe the "best" such instruction in *INSN if so. PATTERN and
8584 TYPE describe the type of insertion or extraction we want to perform.
8585
8586 For an insertion, the number of significant structure bits includes
8587 all bits of the target. For an extraction, it need only include the
8588 most significant bit of the field. Larger widths are acceptable
8589 in both cases. */
8590
8591 static bool
8592 get_best_extraction_insn (extraction_insn *insn,
8593 enum extraction_pattern pattern,
8594 enum extraction_type type,
8595 unsigned HOST_WIDE_INT struct_bits,
8596 machine_mode field_mode)
8597 {
8598 machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
8599 while (mode != VOIDmode)
8600 {
8601 if (get_extraction_insn (insn, pattern, type, mode))
8602 {
8603 while (mode != VOIDmode
8604 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
8605 && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
8606 field_mode))
8607 {
8608 get_extraction_insn (insn, pattern, type, mode);
8609 mode = GET_MODE_WIDER_MODE (mode);
8610 }
8611 return true;
8612 }
8613 mode = GET_MODE_WIDER_MODE (mode);
8614 }
8615 return false;
8616 }
8617
8618 /* Return true if an instruction exists to access a field of mode
8619 FIELDMODE in a register structure that has STRUCT_BITS significant bits.
8620 Describe the "best" such instruction in *INSN if so. PATTERN describes
8621 the type of insertion or extraction we want to perform.
8622
8623 For an insertion, the number of significant structure bits includes
8624 all bits of the target. For an extraction, it need only include the
8625 most significant bit of the field. Larger widths are acceptable
8626 in both cases. */
8627
8628 bool
8629 get_best_reg_extraction_insn (extraction_insn *insn,
8630 enum extraction_pattern pattern,
8631 unsigned HOST_WIDE_INT struct_bits,
8632 machine_mode field_mode)
8633 {
8634 return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
8635 field_mode);
8636 }
8637
8638 /* Return true if an instruction exists to access a field of BITSIZE
8639 bits starting BITNUM bits into a memory structure. Describe the
8640 "best" such instruction in *INSN if so. PATTERN describes the type
8641 of insertion or extraction we want to perform and FIELDMODE is the
8642 natural mode of the extracted field.
8643
8644 The instructions considered here only access bytes that overlap
8645 the bitfield; they do not touch any surrounding bytes. */
8646
8647 bool
8648 get_best_mem_extraction_insn (extraction_insn *insn,
8649 enum extraction_pattern pattern,
8650 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
8651 machine_mode field_mode)
8652 {
8653 unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
8654 + bitsize
8655 + BITS_PER_UNIT - 1);
8656 struct_bits -= struct_bits % BITS_PER_UNIT;
8657 return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
8658 struct_bits, field_mode);
8659 }
8660
8661 /* Determine whether "1 << x" is relatively cheap in word_mode. */
8662
8663 bool
8664 lshift_cheap_p (bool speed_p)
8665 {
8666 /* FIXME: This should be made target dependent via this "this_target"
8667 mechanism, similar to e.g. can_copy_init_p in gcse.c. */
8668 static bool init[2] = { false, false };
8669 static bool cheap[2] = { true, true };
8670
8671 /* If the targer has no lshift in word_mode, the operation will most
8672 probably not be cheap. ??? Does GCC even work for such targets? */
8673 if (optab_handler (ashl_optab, word_mode) == CODE_FOR_nothing)
8674 return false;
8675
8676 if (!init[speed_p])
8677 {
8678 rtx reg = gen_raw_REG (word_mode, 10000);
8679 int cost = set_src_cost (gen_rtx_ASHIFT (word_mode, const1_rtx, reg),
8680 speed_p);
8681 cheap[speed_p] = cost < COSTS_N_INSNS (3);
8682 init[speed_p] = true;
8683 }
8684
8685 return cheap[speed_p];
8686 }
8687
8688 #include "gt-optabs.h"