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