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