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