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