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