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