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