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