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