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