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