[13/77] Make floatn_mode return an opt_scalar_float_mode
[official-gcc.git] / gcc / optabs.c
blob5c9cd34a26e6e32c50ae72306aefa35b9793e0ef
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "memmodel.h"
29 #include "predict.h"
30 #include "tm_p.h"
31 #include "expmed.h"
32 #include "optabs.h"
33 #include "emit-rtl.h"
34 #include "recog.h"
35 #include "diagnostic-core.h"
37 /* Include insn-config.h before expr.h so that HAVE_conditional_move
38 is properly defined. */
39 #include "stor-layout.h"
40 #include "except.h"
41 #include "dojump.h"
42 #include "explow.h"
43 #include "expr.h"
44 #include "optabs-tree.h"
45 #include "libfuncs.h"
47 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
48 machine_mode *);
49 static rtx expand_unop_direct (machine_mode, optab, rtx, rtx, int);
50 static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
52 /* Debug facility for use in GDB. */
53 void debug_optab_libfuncs (void);
55 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
56 the result of operation CODE applied to OP0 (and OP1 if it is a binary
57 operation).
59 If the last insn does not set TARGET, don't do anything, but return 1.
61 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
62 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
63 try again, ensuring that TARGET is not one of the operands. */
65 static int
66 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
68 rtx_insn *last_insn;
69 rtx set;
70 rtx note;
72 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
74 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
75 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
76 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
77 && GET_RTX_CLASS (code) != RTX_COMPARE
78 && GET_RTX_CLASS (code) != RTX_UNARY)
79 return 1;
81 if (GET_CODE (target) == ZERO_EXTRACT)
82 return 1;
84 for (last_insn = insns;
85 NEXT_INSN (last_insn) != NULL_RTX;
86 last_insn = NEXT_INSN (last_insn))
89 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
90 a value changing in the insn, so the note would be invalid for CSE. */
91 if (reg_overlap_mentioned_p (target, op0)
92 || (op1 && reg_overlap_mentioned_p (target, op1)))
94 if (MEM_P (target)
95 && (rtx_equal_p (target, op0)
96 || (op1 && rtx_equal_p (target, op1))))
98 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
99 over expanding it as temp = MEM op X, MEM = temp. If the target
100 supports MEM = MEM op X instructions, it is sometimes too hard
101 to reconstruct that form later, especially if X is also a memory,
102 and due to multiple occurrences of addresses the address might
103 be forced into register unnecessarily.
104 Note that not emitting the REG_EQUIV note might inhibit
105 CSE in some cases. */
106 set = single_set (last_insn);
107 if (set
108 && GET_CODE (SET_SRC (set)) == code
109 && MEM_P (SET_DEST (set))
110 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
111 || (op1 && rtx_equal_p (SET_DEST (set),
112 XEXP (SET_SRC (set), 1)))))
113 return 1;
115 return 0;
118 set = set_for_reg_notes (last_insn);
119 if (set == NULL_RTX)
120 return 1;
122 if (! rtx_equal_p (SET_DEST (set), target)
123 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
124 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
125 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
126 return 1;
128 if (GET_RTX_CLASS (code) == RTX_UNARY)
129 switch (code)
131 case FFS:
132 case CLZ:
133 case CTZ:
134 case CLRSB:
135 case POPCOUNT:
136 case PARITY:
137 case BSWAP:
138 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
140 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
141 if (GET_MODE_SIZE (GET_MODE (op0))
142 > GET_MODE_SIZE (GET_MODE (target)))
143 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
144 note, GET_MODE (op0));
145 else
146 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
147 note, GET_MODE (op0));
148 break;
150 /* FALLTHRU */
151 default:
152 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
153 break;
155 else
156 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
158 set_unique_reg_note (last_insn, REG_EQUAL, note);
160 return 1;
163 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
164 for a widening operation would be. In most cases this would be OP0, but if
165 that's a constant it'll be VOIDmode, which isn't useful. */
167 static machine_mode
168 widened_mode (machine_mode to_mode, rtx op0, rtx op1)
170 machine_mode m0 = GET_MODE (op0);
171 machine_mode m1 = GET_MODE (op1);
172 machine_mode result;
174 if (m0 == VOIDmode && m1 == VOIDmode)
175 return to_mode;
176 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
177 result = m1;
178 else
179 result = m0;
181 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
182 return to_mode;
184 return result;
187 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
188 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
189 not actually do a sign-extend or zero-extend, but can leave the
190 higher-order bits of the result rtx undefined, for example, in the case
191 of logical operations, but not right shifts. */
193 static rtx
194 widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
195 int unsignedp, int no_extend)
197 rtx result;
199 /* If we don't have to extend and this is a constant, return it. */
200 if (no_extend && GET_MODE (op) == VOIDmode)
201 return op;
203 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
204 extend since it will be more efficient to do so unless the signedness of
205 a promoted object differs from our extension. */
206 if (! no_extend
207 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
208 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
209 return convert_modes (mode, oldmode, op, unsignedp);
211 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
212 SUBREG. */
213 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
214 return gen_lowpart (mode, force_reg (GET_MODE (op), op));
216 /* Otherwise, get an object of MODE, clobber it, and set the low-order
217 part to OP. */
219 result = gen_reg_rtx (mode);
220 emit_clobber (result);
221 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
222 return result;
225 /* Expand vector widening operations.
227 There are two different classes of operations handled here:
228 1) Operations whose result is wider than all the arguments to the operation.
229 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
230 In this case OP0 and optionally OP1 would be initialized,
231 but WIDE_OP wouldn't (not relevant for this case).
232 2) Operations whose result is of the same size as the last argument to the
233 operation, but wider than all the other arguments to the operation.
234 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
235 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
237 E.g, when called to expand the following operations, this is how
238 the arguments will be initialized:
239 nops OP0 OP1 WIDE_OP
240 widening-sum 2 oprnd0 - oprnd1
241 widening-dot-product 3 oprnd0 oprnd1 oprnd2
242 widening-mult 2 oprnd0 oprnd1 -
243 type-promotion (vec-unpack) 1 oprnd0 - - */
246 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
247 rtx target, int unsignedp)
249 struct expand_operand eops[4];
250 tree oprnd0, oprnd1, oprnd2;
251 machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
252 optab widen_pattern_optab;
253 enum insn_code icode;
254 int nops = TREE_CODE_LENGTH (ops->code);
255 int op;
257 oprnd0 = ops->op0;
258 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
259 widen_pattern_optab =
260 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
261 if (ops->code == WIDEN_MULT_PLUS_EXPR
262 || ops->code == WIDEN_MULT_MINUS_EXPR)
263 icode = find_widening_optab_handler (widen_pattern_optab,
264 TYPE_MODE (TREE_TYPE (ops->op2)),
265 tmode0, 0);
266 else
267 icode = optab_handler (widen_pattern_optab, tmode0);
268 gcc_assert (icode != CODE_FOR_nothing);
270 if (nops >= 2)
272 oprnd1 = ops->op1;
273 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
276 /* The last operand is of a wider mode than the rest of the operands. */
277 if (nops == 2)
278 wmode = tmode1;
279 else if (nops == 3)
281 gcc_assert (tmode1 == tmode0);
282 gcc_assert (op1);
283 oprnd2 = ops->op2;
284 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
287 op = 0;
288 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
289 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
290 if (op1)
291 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
292 if (wide_op)
293 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
294 expand_insn (icode, op, eops);
295 return eops[0].value;
298 /* Generate code to perform an operation specified by TERNARY_OPTAB
299 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
301 UNSIGNEDP is for the case where we have to widen the operands
302 to perform the operation. It says to use zero-extension.
304 If TARGET is nonzero, the value
305 is generated there, if it is convenient to do so.
306 In all cases an rtx is returned for the locus of the value;
307 this may or may not be TARGET. */
310 expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
311 rtx op1, rtx op2, rtx target, int unsignedp)
313 struct expand_operand ops[4];
314 enum insn_code icode = optab_handler (ternary_optab, mode);
316 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
318 create_output_operand (&ops[0], target, mode);
319 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
320 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
321 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
322 expand_insn (icode, 4, ops);
323 return ops[0].value;
327 /* Like expand_binop, but return a constant rtx if the result can be
328 calculated at compile time. The arguments and return value are
329 otherwise the same as for expand_binop. */
332 simplify_expand_binop (machine_mode mode, optab binoptab,
333 rtx op0, rtx op1, rtx target, int unsignedp,
334 enum optab_methods methods)
336 if (CONSTANT_P (op0) && CONSTANT_P (op1))
338 rtx x = simplify_binary_operation (optab_to_code (binoptab),
339 mode, op0, op1);
340 if (x)
341 return x;
344 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
347 /* Like simplify_expand_binop, but always put the result in TARGET.
348 Return true if the expansion succeeded. */
350 bool
351 force_expand_binop (machine_mode mode, optab binoptab,
352 rtx op0, rtx op1, rtx target, int unsignedp,
353 enum optab_methods methods)
355 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
356 target, unsignedp, methods);
357 if (x == 0)
358 return false;
359 if (x != target)
360 emit_move_insn (target, x);
361 return true;
364 /* Create a new vector value in VMODE with all elements set to OP. The
365 mode of OP must be the element mode of VMODE. If OP is a constant,
366 then the return value will be a constant. */
368 static rtx
369 expand_vector_broadcast (machine_mode vmode, rtx op)
371 enum insn_code icode;
372 rtvec vec;
373 rtx ret;
374 int i, n;
376 gcc_checking_assert (VECTOR_MODE_P (vmode));
378 n = GET_MODE_NUNITS (vmode);
379 vec = rtvec_alloc (n);
380 for (i = 0; i < n; ++i)
381 RTVEC_ELT (vec, i) = op;
383 if (CONSTANT_P (op))
384 return gen_rtx_CONST_VECTOR (vmode, vec);
386 /* ??? If the target doesn't have a vec_init, then we have no easy way
387 of performing this operation. Most of this sort of generic support
388 is hidden away in the vector lowering support in gimple. */
389 icode = convert_optab_handler (vec_init_optab, vmode,
390 GET_MODE_INNER (vmode));
391 if (icode == CODE_FOR_nothing)
392 return NULL;
394 ret = gen_reg_rtx (vmode);
395 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
397 return ret;
400 /* This subroutine of expand_doubleword_shift handles the cases in which
401 the effective shift value is >= BITS_PER_WORD. The arguments and return
402 value are the same as for the parent routine, except that SUPERWORD_OP1
403 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
404 INTO_TARGET may be null if the caller has decided to calculate it. */
406 static bool
407 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
408 rtx outof_target, rtx into_target,
409 int unsignedp, enum optab_methods methods)
411 if (into_target != 0)
412 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
413 into_target, unsignedp, methods))
414 return false;
416 if (outof_target != 0)
418 /* For a signed right shift, we must fill OUTOF_TARGET with copies
419 of the sign bit, otherwise we must fill it with zeros. */
420 if (binoptab != ashr_optab)
421 emit_move_insn (outof_target, CONST0_RTX (word_mode));
422 else
423 if (!force_expand_binop (word_mode, binoptab,
424 outof_input, GEN_INT (BITS_PER_WORD - 1),
425 outof_target, unsignedp, methods))
426 return false;
428 return true;
431 /* This subroutine of expand_doubleword_shift handles the cases in which
432 the effective shift value is < BITS_PER_WORD. The arguments and return
433 value are the same as for the parent routine. */
435 static bool
436 expand_subword_shift (machine_mode op1_mode, optab binoptab,
437 rtx outof_input, rtx into_input, rtx op1,
438 rtx outof_target, rtx into_target,
439 int unsignedp, enum optab_methods methods,
440 unsigned HOST_WIDE_INT shift_mask)
442 optab reverse_unsigned_shift, unsigned_shift;
443 rtx tmp, carries;
445 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
446 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
448 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
449 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
450 the opposite direction to BINOPTAB. */
451 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
453 carries = outof_input;
454 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
455 op1_mode), op1_mode);
456 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
457 0, true, methods);
459 else
461 /* We must avoid shifting by BITS_PER_WORD bits since that is either
462 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
463 has unknown behavior. Do a single shift first, then shift by the
464 remainder. It's OK to use ~OP1 as the remainder if shift counts
465 are truncated to the mode size. */
466 carries = expand_binop (word_mode, reverse_unsigned_shift,
467 outof_input, const1_rtx, 0, unsignedp, methods);
468 if (shift_mask == BITS_PER_WORD - 1)
470 tmp = immed_wide_int_const
471 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
472 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
473 0, true, methods);
475 else
477 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
478 op1_mode), op1_mode);
479 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
480 0, true, methods);
483 if (tmp == 0 || carries == 0)
484 return false;
485 carries = expand_binop (word_mode, reverse_unsigned_shift,
486 carries, tmp, 0, unsignedp, methods);
487 if (carries == 0)
488 return false;
490 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
491 so the result can go directly into INTO_TARGET if convenient. */
492 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
493 into_target, unsignedp, methods);
494 if (tmp == 0)
495 return false;
497 /* Now OR in the bits carried over from OUTOF_INPUT. */
498 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
499 into_target, unsignedp, methods))
500 return false;
502 /* Use a standard word_mode shift for the out-of half. */
503 if (outof_target != 0)
504 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
505 outof_target, unsignedp, methods))
506 return false;
508 return true;
512 /* Try implementing expand_doubleword_shift using conditional moves.
513 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
514 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
515 are the shift counts to use in the former and latter case. All other
516 arguments are the same as the parent routine. */
518 static bool
519 expand_doubleword_shift_condmove (machine_mode op1_mode, optab binoptab,
520 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
521 rtx outof_input, rtx into_input,
522 rtx subword_op1, rtx superword_op1,
523 rtx outof_target, rtx into_target,
524 int unsignedp, enum optab_methods methods,
525 unsigned HOST_WIDE_INT shift_mask)
527 rtx outof_superword, into_superword;
529 /* Put the superword version of the output into OUTOF_SUPERWORD and
530 INTO_SUPERWORD. */
531 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
532 if (outof_target != 0 && subword_op1 == superword_op1)
534 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
535 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
536 into_superword = outof_target;
537 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
538 outof_superword, 0, unsignedp, methods))
539 return false;
541 else
543 into_superword = gen_reg_rtx (word_mode);
544 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
545 outof_superword, into_superword,
546 unsignedp, methods))
547 return false;
550 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
551 if (!expand_subword_shift (op1_mode, binoptab,
552 outof_input, into_input, subword_op1,
553 outof_target, into_target,
554 unsignedp, methods, shift_mask))
555 return false;
557 /* Select between them. Do the INTO half first because INTO_SUPERWORD
558 might be the current value of OUTOF_TARGET. */
559 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
560 into_target, into_superword, word_mode, false))
561 return false;
563 if (outof_target != 0)
564 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
565 outof_target, outof_superword,
566 word_mode, false))
567 return false;
569 return true;
572 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
573 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
574 input operand; the shift moves bits in the direction OUTOF_INPUT->
575 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
576 of the target. OP1 is the shift count and OP1_MODE is its mode.
577 If OP1 is constant, it will have been truncated as appropriate
578 and is known to be nonzero.
580 If SHIFT_MASK is zero, the result of word shifts is undefined when the
581 shift count is outside the range [0, BITS_PER_WORD). This routine must
582 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
584 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
585 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
586 fill with zeros or sign bits as appropriate.
588 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
589 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
590 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
591 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
592 are undefined.
594 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
595 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
596 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
597 function wants to calculate it itself.
599 Return true if the shift could be successfully synthesized. */
601 static bool
602 expand_doubleword_shift (machine_mode op1_mode, optab binoptab,
603 rtx outof_input, rtx into_input, rtx op1,
604 rtx outof_target, rtx into_target,
605 int unsignedp, enum optab_methods methods,
606 unsigned HOST_WIDE_INT shift_mask)
608 rtx superword_op1, tmp, cmp1, cmp2;
609 enum rtx_code cmp_code;
611 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
612 fill the result with sign or zero bits as appropriate. If so, the value
613 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
614 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
615 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
617 This isn't worthwhile for constant shifts since the optimizers will
618 cope better with in-range shift counts. */
619 if (shift_mask >= BITS_PER_WORD
620 && outof_target != 0
621 && !CONSTANT_P (op1))
623 if (!expand_doubleword_shift (op1_mode, binoptab,
624 outof_input, into_input, op1,
625 0, into_target,
626 unsignedp, methods, shift_mask))
627 return false;
628 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
629 outof_target, unsignedp, methods))
630 return false;
631 return true;
634 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
635 is true when the effective shift value is less than BITS_PER_WORD.
636 Set SUPERWORD_OP1 to the shift count that should be used to shift
637 OUTOF_INPUT into INTO_TARGET when the condition is false. */
638 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
639 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
641 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
642 is a subword shift count. */
643 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
644 0, true, methods);
645 cmp2 = CONST0_RTX (op1_mode);
646 cmp_code = EQ;
647 superword_op1 = op1;
649 else
651 /* Set CMP1 to OP1 - BITS_PER_WORD. */
652 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
653 0, true, methods);
654 cmp2 = CONST0_RTX (op1_mode);
655 cmp_code = LT;
656 superword_op1 = cmp1;
658 if (cmp1 == 0)
659 return false;
661 /* If we can compute the condition at compile time, pick the
662 appropriate subroutine. */
663 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
664 if (tmp != 0 && CONST_INT_P (tmp))
666 if (tmp == const0_rtx)
667 return expand_superword_shift (binoptab, outof_input, superword_op1,
668 outof_target, into_target,
669 unsignedp, methods);
670 else
671 return expand_subword_shift (op1_mode, binoptab,
672 outof_input, into_input, op1,
673 outof_target, into_target,
674 unsignedp, methods, shift_mask);
677 /* Try using conditional moves to generate straight-line code. */
678 if (HAVE_conditional_move)
680 rtx_insn *start = get_last_insn ();
681 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
682 cmp_code, cmp1, cmp2,
683 outof_input, into_input,
684 op1, superword_op1,
685 outof_target, into_target,
686 unsignedp, methods, shift_mask))
687 return true;
688 delete_insns_since (start);
691 /* As a last resort, use branches to select the correct alternative. */
692 rtx_code_label *subword_label = gen_label_rtx ();
693 rtx_code_label *done_label = gen_label_rtx ();
695 NO_DEFER_POP;
696 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
697 0, 0, subword_label,
698 profile_probability::uninitialized ());
699 OK_DEFER_POP;
701 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
702 outof_target, into_target,
703 unsignedp, methods))
704 return false;
706 emit_jump_insn (targetm.gen_jump (done_label));
707 emit_barrier ();
708 emit_label (subword_label);
710 if (!expand_subword_shift (op1_mode, binoptab,
711 outof_input, into_input, op1,
712 outof_target, into_target,
713 unsignedp, methods, shift_mask))
714 return false;
716 emit_label (done_label);
717 return true;
720 /* Subroutine of expand_binop. Perform a double word multiplication of
721 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
722 as the target's word_mode. This function return NULL_RTX if anything
723 goes wrong, in which case it may have already emitted instructions
724 which need to be deleted.
726 If we want to multiply two two-word values and have normal and widening
727 multiplies of single-word values, we can do this with three smaller
728 multiplications.
730 The multiplication proceeds as follows:
731 _______________________
732 [__op0_high_|__op0_low__]
733 _______________________
734 * [__op1_high_|__op1_low__]
735 _______________________________________________
736 _______________________
737 (1) [__op0_low__*__op1_low__]
738 _______________________
739 (2a) [__op0_low__*__op1_high_]
740 _______________________
741 (2b) [__op0_high_*__op1_low__]
742 _______________________
743 (3) [__op0_high_*__op1_high_]
746 This gives a 4-word result. Since we are only interested in the
747 lower 2 words, partial result (3) and the upper words of (2a) and
748 (2b) don't need to be calculated. Hence (2a) and (2b) can be
749 calculated using non-widening multiplication.
751 (1), however, needs to be calculated with an unsigned widening
752 multiplication. If this operation is not directly supported we
753 try using a signed widening multiplication and adjust the result.
754 This adjustment works as follows:
756 If both operands are positive then no adjustment is needed.
758 If the operands have different signs, for example op0_low < 0 and
759 op1_low >= 0, the instruction treats the most significant bit of
760 op0_low as a sign bit instead of a bit with significance
761 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
762 with 2**BITS_PER_WORD - op0_low, and two's complements the
763 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
764 the result.
766 Similarly, if both operands are negative, we need to add
767 (op0_low + op1_low) * 2**BITS_PER_WORD.
769 We use a trick to adjust quickly. We logically shift op0_low right
770 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
771 op0_high (op1_high) before it is used to calculate 2b (2a). If no
772 logical shift exists, we do an arithmetic right shift and subtract
773 the 0 or -1. */
775 static rtx
776 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
777 bool umulp, enum optab_methods methods)
779 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
780 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
781 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
782 rtx product, adjust, product_high, temp;
784 rtx op0_high = operand_subword_force (op0, high, mode);
785 rtx op0_low = operand_subword_force (op0, low, mode);
786 rtx op1_high = operand_subword_force (op1, high, mode);
787 rtx op1_low = operand_subword_force (op1, low, mode);
789 /* If we're using an unsigned multiply to directly compute the product
790 of the low-order words of the operands and perform any required
791 adjustments of the operands, we begin by trying two more multiplications
792 and then computing the appropriate sum.
794 We have checked above that the required addition is provided.
795 Full-word addition will normally always succeed, especially if
796 it is provided at all, so we don't worry about its failure. The
797 multiplication may well fail, however, so we do handle that. */
799 if (!umulp)
801 /* ??? This could be done with emit_store_flag where available. */
802 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
803 NULL_RTX, 1, methods);
804 if (temp)
805 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
806 NULL_RTX, 0, OPTAB_DIRECT);
807 else
809 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
810 NULL_RTX, 0, methods);
811 if (!temp)
812 return NULL_RTX;
813 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
814 NULL_RTX, 0, OPTAB_DIRECT);
817 if (!op0_high)
818 return NULL_RTX;
821 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
822 NULL_RTX, 0, OPTAB_DIRECT);
823 if (!adjust)
824 return NULL_RTX;
826 /* OP0_HIGH should now be dead. */
828 if (!umulp)
830 /* ??? This could be done with emit_store_flag where available. */
831 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
832 NULL_RTX, 1, methods);
833 if (temp)
834 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
835 NULL_RTX, 0, OPTAB_DIRECT);
836 else
838 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
839 NULL_RTX, 0, methods);
840 if (!temp)
841 return NULL_RTX;
842 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
843 NULL_RTX, 0, OPTAB_DIRECT);
846 if (!op1_high)
847 return NULL_RTX;
850 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
851 NULL_RTX, 0, OPTAB_DIRECT);
852 if (!temp)
853 return NULL_RTX;
855 /* OP1_HIGH should now be dead. */
857 adjust = expand_binop (word_mode, add_optab, adjust, temp,
858 NULL_RTX, 0, OPTAB_DIRECT);
860 if (target && !REG_P (target))
861 target = NULL_RTX;
863 if (umulp)
864 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
865 target, 1, OPTAB_DIRECT);
866 else
867 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
868 target, 1, OPTAB_DIRECT);
870 if (!product)
871 return NULL_RTX;
873 product_high = operand_subword (product, high, 1, mode);
874 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
875 NULL_RTX, 0, OPTAB_DIRECT);
876 emit_move_insn (product_high, adjust);
877 return product;
880 /* Wrapper around expand_binop which takes an rtx code to specify
881 the operation to perform, not an optab pointer. All other
882 arguments are the same. */
884 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
885 rtx op1, rtx target, int unsignedp,
886 enum optab_methods methods)
888 optab binop = code_to_optab (code);
889 gcc_assert (binop);
891 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
894 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
895 binop. Order them according to commutative_operand_precedence and, if
896 possible, try to put TARGET or a pseudo first. */
897 static bool
898 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
900 int op0_prec = commutative_operand_precedence (op0);
901 int op1_prec = commutative_operand_precedence (op1);
903 if (op0_prec < op1_prec)
904 return true;
906 if (op0_prec > op1_prec)
907 return false;
909 /* With equal precedence, both orders are ok, but it is better if the
910 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
911 if (target == 0 || REG_P (target))
912 return (REG_P (op1) && !REG_P (op0)) || target == op1;
913 else
914 return rtx_equal_p (op1, target);
917 /* Return true if BINOPTAB implements a shift operation. */
919 static bool
920 shift_optab_p (optab binoptab)
922 switch (optab_to_code (binoptab))
924 case ASHIFT:
925 case SS_ASHIFT:
926 case US_ASHIFT:
927 case ASHIFTRT:
928 case LSHIFTRT:
929 case ROTATE:
930 case ROTATERT:
931 return true;
933 default:
934 return false;
938 /* Return true if BINOPTAB implements a commutative binary operation. */
940 static bool
941 commutative_optab_p (optab binoptab)
943 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
944 || binoptab == smul_widen_optab
945 || binoptab == umul_widen_optab
946 || binoptab == smul_highpart_optab
947 || binoptab == umul_highpart_optab);
950 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
951 optimizing, and if the operand is a constant that costs more than
952 1 instruction, force the constant into a register and return that
953 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
955 static rtx
956 avoid_expensive_constant (machine_mode mode, optab binoptab,
957 int opn, rtx x, bool unsignedp)
959 bool speed = optimize_insn_for_speed_p ();
961 if (mode != VOIDmode
962 && optimize
963 && CONSTANT_P (x)
964 && (rtx_cost (x, mode, optab_to_code (binoptab), opn, speed)
965 > set_src_cost (x, mode, speed)))
967 if (CONST_INT_P (x))
969 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
970 if (intval != INTVAL (x))
971 x = GEN_INT (intval);
973 else
974 x = convert_modes (mode, VOIDmode, x, unsignedp);
975 x = force_reg (mode, x);
977 return x;
980 /* Helper function for expand_binop: handle the case where there
981 is an insn that directly implements the indicated operation.
982 Returns null if this is not possible. */
983 static rtx
984 expand_binop_directly (machine_mode mode, optab binoptab,
985 rtx op0, rtx op1,
986 rtx target, int unsignedp, enum optab_methods methods,
987 rtx_insn *last)
989 machine_mode from_mode = widened_mode (mode, op0, op1);
990 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
991 from_mode, 1);
992 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
993 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
994 machine_mode mode0, mode1, tmp_mode;
995 struct expand_operand ops[3];
996 bool commutative_p;
997 rtx_insn *pat;
998 rtx xop0 = op0, xop1 = op1;
999 bool canonicalize_op1 = false;
1001 /* If it is a commutative operator and the modes would match
1002 if we would swap the operands, we can save the conversions. */
1003 commutative_p = commutative_optab_p (binoptab);
1004 if (commutative_p
1005 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1006 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1007 std::swap (xop0, xop1);
1009 /* If we are optimizing, force expensive constants into a register. */
1010 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1011 if (!shift_optab_p (binoptab))
1012 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1013 else
1014 /* Shifts and rotates often use a different mode for op1 from op0;
1015 for VOIDmode constants we don't know the mode, so force it
1016 to be canonicalized using convert_modes. */
1017 canonicalize_op1 = true;
1019 /* In case the insn wants input operands in modes different from
1020 those of the actual operands, convert the operands. It would
1021 seem that we don't need to convert CONST_INTs, but we do, so
1022 that they're properly zero-extended, sign-extended or truncated
1023 for their mode. */
1025 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1026 if (xmode0 != VOIDmode && xmode0 != mode0)
1028 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1029 mode0 = xmode0;
1032 mode1 = ((GET_MODE (xop1) != VOIDmode || canonicalize_op1)
1033 ? GET_MODE (xop1) : mode);
1034 if (xmode1 != VOIDmode && xmode1 != mode1)
1036 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1037 mode1 = xmode1;
1040 /* If operation is commutative,
1041 try to make the first operand a register.
1042 Even better, try to make it the same as the target.
1043 Also try to make the last operand a constant. */
1044 if (commutative_p
1045 && swap_commutative_operands_with_target (target, xop0, xop1))
1046 std::swap (xop0, xop1);
1048 /* Now, if insn's predicates don't allow our operands, put them into
1049 pseudo regs. */
1051 if (binoptab == vec_pack_trunc_optab
1052 || binoptab == vec_pack_usat_optab
1053 || binoptab == vec_pack_ssat_optab
1054 || binoptab == vec_pack_ufix_trunc_optab
1055 || binoptab == vec_pack_sfix_trunc_optab)
1057 /* The mode of the result is different then the mode of the
1058 arguments. */
1059 tmp_mode = insn_data[(int) icode].operand[0].mode;
1060 if (VECTOR_MODE_P (mode)
1061 && GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1063 delete_insns_since (last);
1064 return NULL_RTX;
1067 else
1068 tmp_mode = mode;
1070 create_output_operand (&ops[0], target, tmp_mode);
1071 create_input_operand (&ops[1], xop0, mode0);
1072 create_input_operand (&ops[2], xop1, mode1);
1073 pat = maybe_gen_insn (icode, 3, ops);
1074 if (pat)
1076 /* If PAT is composed of more than one insn, try to add an appropriate
1077 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1078 operand, call expand_binop again, this time without a target. */
1079 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1080 && ! add_equal_note (pat, ops[0].value,
1081 optab_to_code (binoptab),
1082 ops[1].value, ops[2].value))
1084 delete_insns_since (last);
1085 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1086 unsignedp, methods);
1089 emit_insn (pat);
1090 return ops[0].value;
1092 delete_insns_since (last);
1093 return NULL_RTX;
1096 /* Generate code to perform an operation specified by BINOPTAB
1097 on operands OP0 and OP1, with result having machine-mode MODE.
1099 UNSIGNEDP is for the case where we have to widen the operands
1100 to perform the operation. It says to use zero-extension.
1102 If TARGET is nonzero, the value
1103 is generated there, if it is convenient to do so.
1104 In all cases an rtx is returned for the locus of the value;
1105 this may or may not be TARGET. */
1108 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1109 rtx target, int unsignedp, enum optab_methods methods)
1111 enum optab_methods next_methods
1112 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1113 ? OPTAB_WIDEN : methods);
1114 enum mode_class mclass;
1115 machine_mode wider_mode;
1116 rtx libfunc;
1117 rtx temp;
1118 rtx_insn *entry_last = get_last_insn ();
1119 rtx_insn *last;
1121 mclass = GET_MODE_CLASS (mode);
1123 /* If subtracting an integer constant, convert this into an addition of
1124 the negated constant. */
1126 if (binoptab == sub_optab && CONST_INT_P (op1))
1128 op1 = negate_rtx (mode, op1);
1129 binoptab = add_optab;
1131 /* For shifts, constant invalid op1 might be expanded from different
1132 mode than MODE. As those are invalid, force them to a register
1133 to avoid further problems during expansion. */
1134 else if (CONST_INT_P (op1)
1135 && shift_optab_p (binoptab)
1136 && UINTVAL (op1) >= GET_MODE_BITSIZE (GET_MODE_INNER (mode)))
1138 op1 = gen_int_mode (INTVAL (op1), GET_MODE_INNER (mode));
1139 op1 = force_reg (GET_MODE_INNER (mode), op1);
1142 /* Record where to delete back to if we backtrack. */
1143 last = get_last_insn ();
1145 /* If we can do it with a three-operand insn, do so. */
1147 if (methods != OPTAB_MUST_WIDEN
1148 && find_widening_optab_handler (binoptab, mode,
1149 widened_mode (mode, op0, op1), 1)
1150 != CODE_FOR_nothing)
1152 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1153 unsignedp, methods, last);
1154 if (temp)
1155 return temp;
1158 /* If we were trying to rotate, and that didn't work, try rotating
1159 the other direction before falling back to shifts and bitwise-or. */
1160 if (((binoptab == rotl_optab
1161 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1162 || (binoptab == rotr_optab
1163 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1164 && mclass == MODE_INT)
1166 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1167 rtx newop1;
1168 unsigned int bits = GET_MODE_PRECISION (mode);
1170 if (CONST_INT_P (op1))
1171 newop1 = GEN_INT (bits - INTVAL (op1));
1172 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1173 newop1 = negate_rtx (GET_MODE (op1), op1);
1174 else
1175 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1176 gen_int_mode (bits, GET_MODE (op1)), op1,
1177 NULL_RTX, unsignedp, OPTAB_DIRECT);
1179 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1180 target, unsignedp, methods, last);
1181 if (temp)
1182 return temp;
1185 /* If this is a multiply, see if we can do a widening operation that
1186 takes operands of this mode and makes a wider mode. */
1188 if (binoptab == smul_optab
1189 && GET_MODE_2XWIDER_MODE (mode).exists (&wider_mode)
1190 && (convert_optab_handler ((unsignedp
1191 ? umul_widen_optab
1192 : smul_widen_optab),
1193 wider_mode, mode) != CODE_FOR_nothing))
1195 temp = expand_binop (wider_mode,
1196 unsignedp ? umul_widen_optab : smul_widen_optab,
1197 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1199 if (temp != 0)
1201 if (GET_MODE_CLASS (mode) == MODE_INT
1202 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1203 return gen_lowpart (mode, temp);
1204 else
1205 return convert_to_mode (mode, temp, unsignedp);
1209 /* If this is a vector shift by a scalar, see if we can do a vector
1210 shift by a vector. If so, broadcast the scalar into a vector. */
1211 if (mclass == MODE_VECTOR_INT)
1213 optab otheroptab = unknown_optab;
1215 if (binoptab == ashl_optab)
1216 otheroptab = vashl_optab;
1217 else if (binoptab == ashr_optab)
1218 otheroptab = vashr_optab;
1219 else if (binoptab == lshr_optab)
1220 otheroptab = vlshr_optab;
1221 else if (binoptab == rotl_optab)
1222 otheroptab = vrotl_optab;
1223 else if (binoptab == rotr_optab)
1224 otheroptab = vrotr_optab;
1226 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1228 /* The scalar may have been extended to be too wide. Truncate
1229 it back to the proper size to fit in the broadcast vector. */
1230 machine_mode inner_mode = GET_MODE_INNER (mode);
1231 if (!CONST_INT_P (op1)
1232 && (GET_MODE_BITSIZE (inner_mode)
1233 < GET_MODE_BITSIZE (GET_MODE (op1))))
1234 op1 = force_reg (inner_mode,
1235 simplify_gen_unary (TRUNCATE, inner_mode, op1,
1236 GET_MODE (op1)));
1237 rtx vop1 = expand_vector_broadcast (mode, op1);
1238 if (vop1)
1240 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1241 target, unsignedp, methods, last);
1242 if (temp)
1243 return temp;
1248 /* Look for a wider mode of the same class for which we think we
1249 can open-code the operation. Check for a widening multiply at the
1250 wider mode as well. */
1252 if (CLASS_HAS_WIDER_MODES_P (mclass)
1253 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1254 FOR_EACH_WIDER_MODE (wider_mode, mode)
1256 machine_mode next_mode;
1257 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1258 || (binoptab == smul_optab
1259 && GET_MODE_WIDER_MODE (wider_mode).exists (&next_mode)
1260 && (find_widening_optab_handler ((unsignedp
1261 ? umul_widen_optab
1262 : smul_widen_optab),
1263 next_mode, mode, 0)
1264 != CODE_FOR_nothing)))
1266 rtx xop0 = op0, xop1 = op1;
1267 int no_extend = 0;
1269 /* For certain integer operations, we need not actually extend
1270 the narrow operands, as long as we will truncate
1271 the results to the same narrowness. */
1273 if ((binoptab == ior_optab || binoptab == and_optab
1274 || binoptab == xor_optab
1275 || binoptab == add_optab || binoptab == sub_optab
1276 || binoptab == smul_optab || binoptab == ashl_optab)
1277 && mclass == MODE_INT)
1279 no_extend = 1;
1280 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1281 xop0, unsignedp);
1282 if (binoptab != ashl_optab)
1283 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1284 xop1, unsignedp);
1287 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1289 /* The second operand of a shift must always be extended. */
1290 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1291 no_extend && binoptab != ashl_optab);
1293 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1294 unsignedp, OPTAB_DIRECT);
1295 if (temp)
1297 if (mclass != MODE_INT
1298 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1300 if (target == 0)
1301 target = gen_reg_rtx (mode);
1302 convert_move (target, temp, 0);
1303 return target;
1305 else
1306 return gen_lowpart (mode, temp);
1308 else
1309 delete_insns_since (last);
1313 /* If operation is commutative,
1314 try to make the first operand a register.
1315 Even better, try to make it the same as the target.
1316 Also try to make the last operand a constant. */
1317 if (commutative_optab_p (binoptab)
1318 && swap_commutative_operands_with_target (target, op0, op1))
1319 std::swap (op0, op1);
1321 /* These can be done a word at a time. */
1322 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1323 && mclass == MODE_INT
1324 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1325 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1327 int i;
1328 rtx_insn *insns;
1330 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1331 won't be accurate, so use a new target. */
1332 if (target == 0
1333 || target == op0
1334 || target == op1
1335 || !valid_multiword_target_p (target))
1336 target = gen_reg_rtx (mode);
1338 start_sequence ();
1340 /* Do the actual arithmetic. */
1341 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1343 rtx target_piece = operand_subword (target, i, 1, mode);
1344 rtx x = expand_binop (word_mode, binoptab,
1345 operand_subword_force (op0, i, mode),
1346 operand_subword_force (op1, i, mode),
1347 target_piece, unsignedp, next_methods);
1349 if (x == 0)
1350 break;
1352 if (target_piece != x)
1353 emit_move_insn (target_piece, x);
1356 insns = get_insns ();
1357 end_sequence ();
1359 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1361 emit_insn (insns);
1362 return target;
1366 /* Synthesize double word shifts from single word shifts. */
1367 if ((binoptab == lshr_optab || binoptab == ashl_optab
1368 || binoptab == ashr_optab)
1369 && mclass == MODE_INT
1370 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1371 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1372 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1373 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1374 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1375 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1377 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1378 machine_mode op1_mode;
1380 double_shift_mask = targetm.shift_truncation_mask (mode);
1381 shift_mask = targetm.shift_truncation_mask (word_mode);
1382 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1384 /* Apply the truncation to constant shifts. */
1385 if (double_shift_mask > 0 && CONST_INT_P (op1))
1386 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1388 if (op1 == CONST0_RTX (op1_mode))
1389 return op0;
1391 /* Make sure that this is a combination that expand_doubleword_shift
1392 can handle. See the comments there for details. */
1393 if (double_shift_mask == 0
1394 || (shift_mask == BITS_PER_WORD - 1
1395 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1397 rtx_insn *insns;
1398 rtx into_target, outof_target;
1399 rtx into_input, outof_input;
1400 int left_shift, outof_word;
1402 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1403 won't be accurate, so use a new target. */
1404 if (target == 0
1405 || target == op0
1406 || target == op1
1407 || !valid_multiword_target_p (target))
1408 target = gen_reg_rtx (mode);
1410 start_sequence ();
1412 /* OUTOF_* is the word we are shifting bits away from, and
1413 INTO_* is the word that we are shifting bits towards, thus
1414 they differ depending on the direction of the shift and
1415 WORDS_BIG_ENDIAN. */
1417 left_shift = binoptab == ashl_optab;
1418 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1420 outof_target = operand_subword (target, outof_word, 1, mode);
1421 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1423 outof_input = operand_subword_force (op0, outof_word, mode);
1424 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1426 if (expand_doubleword_shift (op1_mode, binoptab,
1427 outof_input, into_input, op1,
1428 outof_target, into_target,
1429 unsignedp, next_methods, shift_mask))
1431 insns = get_insns ();
1432 end_sequence ();
1434 emit_insn (insns);
1435 return target;
1437 end_sequence ();
1441 /* Synthesize double word rotates from single word shifts. */
1442 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1443 && mclass == MODE_INT
1444 && CONST_INT_P (op1)
1445 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1446 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1447 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1449 rtx_insn *insns;
1450 rtx into_target, outof_target;
1451 rtx into_input, outof_input;
1452 rtx inter;
1453 int shift_count, left_shift, outof_word;
1455 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1456 won't be accurate, so use a new target. Do this also if target is not
1457 a REG, first because having a register instead may open optimization
1458 opportunities, and second because if target and op0 happen to be MEMs
1459 designating the same location, we would risk clobbering it too early
1460 in the code sequence we generate below. */
1461 if (target == 0
1462 || target == op0
1463 || target == op1
1464 || !REG_P (target)
1465 || !valid_multiword_target_p (target))
1466 target = gen_reg_rtx (mode);
1468 start_sequence ();
1470 shift_count = INTVAL (op1);
1472 /* OUTOF_* is the word we are shifting bits away from, and
1473 INTO_* is the word that we are shifting bits towards, thus
1474 they differ depending on the direction of the shift and
1475 WORDS_BIG_ENDIAN. */
1477 left_shift = (binoptab == rotl_optab);
1478 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1480 outof_target = operand_subword (target, outof_word, 1, mode);
1481 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1483 outof_input = operand_subword_force (op0, outof_word, mode);
1484 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1486 if (shift_count == BITS_PER_WORD)
1488 /* This is just a word swap. */
1489 emit_move_insn (outof_target, into_input);
1490 emit_move_insn (into_target, outof_input);
1491 inter = const0_rtx;
1493 else
1495 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1496 rtx first_shift_count, second_shift_count;
1497 optab reverse_unsigned_shift, unsigned_shift;
1499 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1500 ? lshr_optab : ashl_optab);
1502 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1503 ? ashl_optab : lshr_optab);
1505 if (shift_count > BITS_PER_WORD)
1507 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1508 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1510 else
1512 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1513 second_shift_count = GEN_INT (shift_count);
1516 into_temp1 = expand_binop (word_mode, unsigned_shift,
1517 outof_input, first_shift_count,
1518 NULL_RTX, unsignedp, next_methods);
1519 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1520 into_input, second_shift_count,
1521 NULL_RTX, unsignedp, next_methods);
1523 if (into_temp1 != 0 && into_temp2 != 0)
1524 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1525 into_target, unsignedp, next_methods);
1526 else
1527 inter = 0;
1529 if (inter != 0 && inter != into_target)
1530 emit_move_insn (into_target, inter);
1532 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1533 into_input, first_shift_count,
1534 NULL_RTX, unsignedp, next_methods);
1535 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1536 outof_input, second_shift_count,
1537 NULL_RTX, unsignedp, next_methods);
1539 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1540 inter = expand_binop (word_mode, ior_optab,
1541 outof_temp1, outof_temp2,
1542 outof_target, unsignedp, next_methods);
1544 if (inter != 0 && inter != outof_target)
1545 emit_move_insn (outof_target, inter);
1548 insns = get_insns ();
1549 end_sequence ();
1551 if (inter != 0)
1553 emit_insn (insns);
1554 return target;
1558 /* These can be done a word at a time by propagating carries. */
1559 if ((binoptab == add_optab || binoptab == sub_optab)
1560 && mclass == MODE_INT
1561 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1562 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1564 unsigned int i;
1565 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1566 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1567 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1568 rtx xop0, xop1, xtarget;
1570 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1571 value is one of those, use it. Otherwise, use 1 since it is the
1572 one easiest to get. */
1573 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1574 int normalizep = STORE_FLAG_VALUE;
1575 #else
1576 int normalizep = 1;
1577 #endif
1579 /* Prepare the operands. */
1580 xop0 = force_reg (mode, op0);
1581 xop1 = force_reg (mode, op1);
1583 xtarget = gen_reg_rtx (mode);
1585 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1586 target = xtarget;
1588 /* Indicate for flow that the entire target reg is being set. */
1589 if (REG_P (target))
1590 emit_clobber (xtarget);
1592 /* Do the actual arithmetic. */
1593 for (i = 0; i < nwords; i++)
1595 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1596 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1597 rtx op0_piece = operand_subword_force (xop0, index, mode);
1598 rtx op1_piece = operand_subword_force (xop1, index, mode);
1599 rtx x;
1601 /* Main add/subtract of the input operands. */
1602 x = expand_binop (word_mode, binoptab,
1603 op0_piece, op1_piece,
1604 target_piece, unsignedp, next_methods);
1605 if (x == 0)
1606 break;
1608 if (i + 1 < nwords)
1610 /* Store carry from main add/subtract. */
1611 carry_out = gen_reg_rtx (word_mode);
1612 carry_out = emit_store_flag_force (carry_out,
1613 (binoptab == add_optab
1614 ? LT : GT),
1615 x, op0_piece,
1616 word_mode, 1, normalizep);
1619 if (i > 0)
1621 rtx newx;
1623 /* Add/subtract previous carry to main result. */
1624 newx = expand_binop (word_mode,
1625 normalizep == 1 ? binoptab : otheroptab,
1626 x, carry_in,
1627 NULL_RTX, 1, next_methods);
1629 if (i + 1 < nwords)
1631 /* Get out carry from adding/subtracting carry in. */
1632 rtx carry_tmp = gen_reg_rtx (word_mode);
1633 carry_tmp = emit_store_flag_force (carry_tmp,
1634 (binoptab == add_optab
1635 ? LT : GT),
1636 newx, x,
1637 word_mode, 1, normalizep);
1639 /* Logical-ior the two poss. carry together. */
1640 carry_out = expand_binop (word_mode, ior_optab,
1641 carry_out, carry_tmp,
1642 carry_out, 0, next_methods);
1643 if (carry_out == 0)
1644 break;
1646 emit_move_insn (target_piece, newx);
1648 else
1650 if (x != target_piece)
1651 emit_move_insn (target_piece, x);
1654 carry_in = carry_out;
1657 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
1659 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
1660 || ! rtx_equal_p (target, xtarget))
1662 rtx_insn *temp = emit_move_insn (target, xtarget);
1664 set_dst_reg_note (temp, REG_EQUAL,
1665 gen_rtx_fmt_ee (optab_to_code (binoptab),
1666 mode, copy_rtx (xop0),
1667 copy_rtx (xop1)),
1668 target);
1670 else
1671 target = xtarget;
1673 return target;
1676 else
1677 delete_insns_since (last);
1680 /* Attempt to synthesize double word multiplies using a sequence of word
1681 mode multiplications. We first attempt to generate a sequence using a
1682 more efficient unsigned widening multiply, and if that fails we then
1683 try using a signed widening multiply. */
1685 if (binoptab == smul_optab
1686 && mclass == MODE_INT
1687 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1688 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
1689 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
1691 rtx product = NULL_RTX;
1692 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
1693 != CODE_FOR_nothing)
1695 product = expand_doubleword_mult (mode, op0, op1, target,
1696 true, methods);
1697 if (!product)
1698 delete_insns_since (last);
1701 if (product == NULL_RTX
1702 && widening_optab_handler (smul_widen_optab, mode, word_mode)
1703 != CODE_FOR_nothing)
1705 product = expand_doubleword_mult (mode, op0, op1, target,
1706 false, methods);
1707 if (!product)
1708 delete_insns_since (last);
1711 if (product != NULL_RTX)
1713 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
1715 rtx_insn *move = emit_move_insn (target ? target : product,
1716 product);
1717 set_dst_reg_note (move,
1718 REG_EQUAL,
1719 gen_rtx_fmt_ee (MULT, mode,
1720 copy_rtx (op0),
1721 copy_rtx (op1)),
1722 target ? target : product);
1724 return product;
1728 /* It can't be open-coded in this mode.
1729 Use a library call if one is available and caller says that's ok. */
1731 libfunc = optab_libfunc (binoptab, mode);
1732 if (libfunc
1733 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
1735 rtx_insn *insns;
1736 rtx op1x = op1;
1737 machine_mode op1_mode = mode;
1738 rtx value;
1740 start_sequence ();
1742 if (shift_optab_p (binoptab))
1744 op1_mode = targetm.libgcc_shift_count_mode ();
1745 /* Specify unsigned here,
1746 since negative shift counts are meaningless. */
1747 op1x = convert_to_mode (op1_mode, op1, 1);
1750 if (GET_MODE (op0) != VOIDmode
1751 && GET_MODE (op0) != mode)
1752 op0 = convert_to_mode (mode, op0, unsignedp);
1754 /* Pass 1 for NO_QUEUE so we don't lose any increments
1755 if the libcall is cse'd or moved. */
1756 value = emit_library_call_value (libfunc,
1757 NULL_RTX, LCT_CONST, mode, 2,
1758 op0, mode, op1x, op1_mode);
1760 insns = get_insns ();
1761 end_sequence ();
1763 bool trapv = trapv_binoptab_p (binoptab);
1764 target = gen_reg_rtx (mode);
1765 emit_libcall_block_1 (insns, target, value,
1766 trapv ? NULL_RTX
1767 : gen_rtx_fmt_ee (optab_to_code (binoptab),
1768 mode, op0, op1), trapv);
1770 return target;
1773 delete_insns_since (last);
1775 /* It can't be done in this mode. Can we do it in a wider mode? */
1777 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
1778 || methods == OPTAB_MUST_WIDEN))
1780 /* Caller says, don't even try. */
1781 delete_insns_since (entry_last);
1782 return 0;
1785 /* Compute the value of METHODS to pass to recursive calls.
1786 Don't allow widening to be tried recursively. */
1788 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
1790 /* Look for a wider mode of the same class for which it appears we can do
1791 the operation. */
1793 if (CLASS_HAS_WIDER_MODES_P (mclass))
1795 FOR_EACH_WIDER_MODE (wider_mode, mode)
1797 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
1798 != CODE_FOR_nothing
1799 || (methods == OPTAB_LIB
1800 && optab_libfunc (binoptab, wider_mode)))
1802 rtx xop0 = op0, xop1 = op1;
1803 int no_extend = 0;
1805 /* For certain integer operations, we need not actually extend
1806 the narrow operands, as long as we will truncate
1807 the results to the same narrowness. */
1809 if ((binoptab == ior_optab || binoptab == and_optab
1810 || binoptab == xor_optab
1811 || binoptab == add_optab || binoptab == sub_optab
1812 || binoptab == smul_optab || binoptab == ashl_optab)
1813 && mclass == MODE_INT)
1814 no_extend = 1;
1816 xop0 = widen_operand (xop0, wider_mode, mode,
1817 unsignedp, no_extend);
1819 /* The second operand of a shift must always be extended. */
1820 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1821 no_extend && binoptab != ashl_optab);
1823 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1824 unsignedp, methods);
1825 if (temp)
1827 if (mclass != MODE_INT
1828 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1830 if (target == 0)
1831 target = gen_reg_rtx (mode);
1832 convert_move (target, temp, 0);
1833 return target;
1835 else
1836 return gen_lowpart (mode, temp);
1838 else
1839 delete_insns_since (last);
1844 delete_insns_since (entry_last);
1845 return 0;
1848 /* Expand a binary operator which has both signed and unsigned forms.
1849 UOPTAB is the optab for unsigned operations, and SOPTAB is for
1850 signed operations.
1852 If we widen unsigned operands, we may use a signed wider operation instead
1853 of an unsigned wider operation, since the result would be the same. */
1856 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
1857 rtx op0, rtx op1, rtx target, int unsignedp,
1858 enum optab_methods methods)
1860 rtx temp;
1861 optab direct_optab = unsignedp ? uoptab : soptab;
1862 bool save_enable;
1864 /* Do it without widening, if possible. */
1865 temp = expand_binop (mode, direct_optab, op0, op1, target,
1866 unsignedp, OPTAB_DIRECT);
1867 if (temp || methods == OPTAB_DIRECT)
1868 return temp;
1870 /* Try widening to a signed int. Disable any direct use of any
1871 signed insn in the current mode. */
1872 save_enable = swap_optab_enable (soptab, mode, false);
1874 temp = expand_binop (mode, soptab, op0, op1, target,
1875 unsignedp, OPTAB_WIDEN);
1877 /* For unsigned operands, try widening to an unsigned int. */
1878 if (!temp && unsignedp)
1879 temp = expand_binop (mode, uoptab, op0, op1, target,
1880 unsignedp, OPTAB_WIDEN);
1881 if (temp || methods == OPTAB_WIDEN)
1882 goto egress;
1884 /* Use the right width libcall if that exists. */
1885 temp = expand_binop (mode, direct_optab, op0, op1, target,
1886 unsignedp, OPTAB_LIB);
1887 if (temp || methods == OPTAB_LIB)
1888 goto egress;
1890 /* Must widen and use a libcall, use either signed or unsigned. */
1891 temp = expand_binop (mode, soptab, op0, op1, target,
1892 unsignedp, methods);
1893 if (!temp && unsignedp)
1894 temp = expand_binop (mode, uoptab, op0, op1, target,
1895 unsignedp, methods);
1897 egress:
1898 /* Undo the fiddling above. */
1899 if (save_enable)
1900 swap_optab_enable (soptab, mode, true);
1901 return temp;
1904 /* Generate code to perform an operation specified by UNOPPTAB
1905 on operand OP0, with two results to TARG0 and TARG1.
1906 We assume that the order of the operands for the instruction
1907 is TARG0, TARG1, OP0.
1909 Either TARG0 or TARG1 may be zero, but what that means is that
1910 the result is not actually wanted. We will generate it into
1911 a dummy pseudo-reg and discard it. They may not both be zero.
1913 Returns 1 if this operation can be performed; 0 if not. */
1916 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
1917 int unsignedp)
1919 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1920 enum mode_class mclass;
1921 machine_mode wider_mode;
1922 rtx_insn *entry_last = get_last_insn ();
1923 rtx_insn *last;
1925 mclass = GET_MODE_CLASS (mode);
1927 if (!targ0)
1928 targ0 = gen_reg_rtx (mode);
1929 if (!targ1)
1930 targ1 = gen_reg_rtx (mode);
1932 /* Record where to go back to if we fail. */
1933 last = get_last_insn ();
1935 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
1937 struct expand_operand ops[3];
1938 enum insn_code icode = optab_handler (unoptab, mode);
1940 create_fixed_operand (&ops[0], targ0);
1941 create_fixed_operand (&ops[1], targ1);
1942 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
1943 if (maybe_expand_insn (icode, 3, ops))
1944 return 1;
1947 /* It can't be done in this mode. Can we do it in a wider mode? */
1949 if (CLASS_HAS_WIDER_MODES_P (mclass))
1951 FOR_EACH_WIDER_MODE (wider_mode, mode)
1953 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
1955 rtx t0 = gen_reg_rtx (wider_mode);
1956 rtx t1 = gen_reg_rtx (wider_mode);
1957 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1959 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
1961 convert_move (targ0, t0, unsignedp);
1962 convert_move (targ1, t1, unsignedp);
1963 return 1;
1965 else
1966 delete_insns_since (last);
1971 delete_insns_since (entry_last);
1972 return 0;
1975 /* Generate code to perform an operation specified by BINOPTAB
1976 on operands OP0 and OP1, with two results to TARG1 and TARG2.
1977 We assume that the order of the operands for the instruction
1978 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
1979 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
1981 Either TARG0 or TARG1 may be zero, but what that means is that
1982 the result is not actually wanted. We will generate it into
1983 a dummy pseudo-reg and discard it. They may not both be zero.
1985 Returns 1 if this operation can be performed; 0 if not. */
1988 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
1989 int unsignedp)
1991 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1992 enum mode_class mclass;
1993 machine_mode wider_mode;
1994 rtx_insn *entry_last = get_last_insn ();
1995 rtx_insn *last;
1997 mclass = GET_MODE_CLASS (mode);
1999 if (!targ0)
2000 targ0 = gen_reg_rtx (mode);
2001 if (!targ1)
2002 targ1 = gen_reg_rtx (mode);
2004 /* Record where to go back to if we fail. */
2005 last = get_last_insn ();
2007 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2009 struct expand_operand ops[4];
2010 enum insn_code icode = optab_handler (binoptab, mode);
2011 machine_mode mode0 = insn_data[icode].operand[1].mode;
2012 machine_mode mode1 = insn_data[icode].operand[2].mode;
2013 rtx xop0 = op0, xop1 = op1;
2015 /* If we are optimizing, force expensive constants into a register. */
2016 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2017 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2019 create_fixed_operand (&ops[0], targ0);
2020 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2021 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2022 create_fixed_operand (&ops[3], targ1);
2023 if (maybe_expand_insn (icode, 4, ops))
2024 return 1;
2025 delete_insns_since (last);
2028 /* It can't be done in this mode. Can we do it in a wider mode? */
2030 if (CLASS_HAS_WIDER_MODES_P (mclass))
2032 FOR_EACH_WIDER_MODE (wider_mode, mode)
2034 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2036 rtx t0 = gen_reg_rtx (wider_mode);
2037 rtx t1 = gen_reg_rtx (wider_mode);
2038 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2039 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2041 if (expand_twoval_binop (binoptab, cop0, cop1,
2042 t0, t1, unsignedp))
2044 convert_move (targ0, t0, unsignedp);
2045 convert_move (targ1, t1, unsignedp);
2046 return 1;
2048 else
2049 delete_insns_since (last);
2054 delete_insns_since (entry_last);
2055 return 0;
2058 /* Expand the two-valued library call indicated by BINOPTAB, but
2059 preserve only one of the values. If TARG0 is non-NULL, the first
2060 value is placed into TARG0; otherwise the second value is placed
2061 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2062 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2063 This routine assumes that the value returned by the library call is
2064 as if the return value was of an integral mode twice as wide as the
2065 mode of OP0. Returns 1 if the call was successful. */
2067 bool
2068 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2069 rtx targ0, rtx targ1, enum rtx_code code)
2071 machine_mode mode;
2072 machine_mode libval_mode;
2073 rtx libval;
2074 rtx_insn *insns;
2075 rtx libfunc;
2077 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2078 gcc_assert (!targ0 != !targ1);
2080 mode = GET_MODE (op0);
2081 libfunc = optab_libfunc (binoptab, mode);
2082 if (!libfunc)
2083 return false;
2085 /* The value returned by the library function will have twice as
2086 many bits as the nominal MODE. */
2087 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2088 MODE_INT);
2089 start_sequence ();
2090 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2091 libval_mode, 2,
2092 op0, mode,
2093 op1, mode);
2094 /* Get the part of VAL containing the value that we want. */
2095 libval = simplify_gen_subreg (mode, libval, libval_mode,
2096 targ0 ? 0 : GET_MODE_SIZE (mode));
2097 insns = get_insns ();
2098 end_sequence ();
2099 /* Move the into the desired location. */
2100 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2101 gen_rtx_fmt_ee (code, mode, op0, op1));
2103 return true;
2107 /* Wrapper around expand_unop which takes an rtx code to specify
2108 the operation to perform, not an optab pointer. All other
2109 arguments are the same. */
2111 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2112 rtx target, int unsignedp)
2114 optab unop = code_to_optab (code);
2115 gcc_assert (unop);
2117 return expand_unop (mode, unop, op0, target, unsignedp);
2120 /* Try calculating
2121 (clz:narrow x)
2123 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2125 A similar operation can be used for clrsb. UNOPTAB says which operation
2126 we are trying to expand. */
2127 static rtx
2128 widen_leading (machine_mode mode, rtx op0, rtx target, optab unoptab)
2130 enum mode_class mclass = GET_MODE_CLASS (mode);
2131 if (CLASS_HAS_WIDER_MODES_P (mclass))
2133 machine_mode wider_mode;
2134 FOR_EACH_WIDER_MODE (wider_mode, mode)
2136 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2138 rtx xop0, temp;
2139 rtx_insn *last;
2141 last = get_last_insn ();
2143 if (target == 0)
2144 target = gen_reg_rtx (mode);
2145 xop0 = widen_operand (op0, wider_mode, mode,
2146 unoptab != clrsb_optab, false);
2147 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2148 unoptab != clrsb_optab);
2149 if (temp != 0)
2150 temp = expand_binop
2151 (wider_mode, sub_optab, temp,
2152 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2153 - GET_MODE_PRECISION (mode),
2154 wider_mode),
2155 target, true, OPTAB_DIRECT);
2156 if (temp == 0)
2157 delete_insns_since (last);
2159 return temp;
2163 return 0;
2166 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2167 quantities, choosing which based on whether the high word is nonzero. */
2168 static rtx
2169 expand_doubleword_clz (machine_mode mode, rtx op0, rtx target)
2171 rtx xop0 = force_reg (mode, op0);
2172 rtx subhi = gen_highpart (word_mode, xop0);
2173 rtx sublo = gen_lowpart (word_mode, xop0);
2174 rtx_code_label *hi0_label = gen_label_rtx ();
2175 rtx_code_label *after_label = gen_label_rtx ();
2176 rtx_insn *seq;
2177 rtx temp, result;
2179 /* If we were not given a target, use a word_mode register, not a
2180 'mode' register. The result will fit, and nobody is expecting
2181 anything bigger (the return type of __builtin_clz* is int). */
2182 if (!target)
2183 target = gen_reg_rtx (word_mode);
2185 /* In any case, write to a word_mode scratch in both branches of the
2186 conditional, so we can ensure there is a single move insn setting
2187 'target' to tag a REG_EQUAL note on. */
2188 result = gen_reg_rtx (word_mode);
2190 start_sequence ();
2192 /* If the high word is not equal to zero,
2193 then clz of the full value is clz of the high word. */
2194 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2195 word_mode, true, hi0_label);
2197 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2198 if (!temp)
2199 goto fail;
2201 if (temp != result)
2202 convert_move (result, temp, true);
2204 emit_jump_insn (targetm.gen_jump (after_label));
2205 emit_barrier ();
2207 /* Else clz of the full value is clz of the low word plus the number
2208 of bits in the high word. */
2209 emit_label (hi0_label);
2211 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2212 if (!temp)
2213 goto fail;
2214 temp = expand_binop (word_mode, add_optab, temp,
2215 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2216 result, true, OPTAB_DIRECT);
2217 if (!temp)
2218 goto fail;
2219 if (temp != result)
2220 convert_move (result, temp, true);
2222 emit_label (after_label);
2223 convert_move (target, result, true);
2225 seq = get_insns ();
2226 end_sequence ();
2228 add_equal_note (seq, target, CLZ, xop0, 0);
2229 emit_insn (seq);
2230 return target;
2232 fail:
2233 end_sequence ();
2234 return 0;
2237 /* Try calculating popcount of a double-word quantity as two popcount's of
2238 word-sized quantities and summing up the results. */
2239 static rtx
2240 expand_doubleword_popcount (machine_mode mode, rtx op0, rtx target)
2242 rtx t0, t1, t;
2243 rtx_insn *seq;
2245 start_sequence ();
2247 t0 = expand_unop_direct (word_mode, popcount_optab,
2248 operand_subword_force (op0, 0, mode), NULL_RTX,
2249 true);
2250 t1 = expand_unop_direct (word_mode, popcount_optab,
2251 operand_subword_force (op0, 1, mode), NULL_RTX,
2252 true);
2253 if (!t0 || !t1)
2255 end_sequence ();
2256 return NULL_RTX;
2259 /* If we were not given a target, use a word_mode register, not a
2260 'mode' register. The result will fit, and nobody is expecting
2261 anything bigger (the return type of __builtin_popcount* is int). */
2262 if (!target)
2263 target = gen_reg_rtx (word_mode);
2265 t = expand_binop (word_mode, add_optab, t0, t1, target, 0, OPTAB_DIRECT);
2267 seq = get_insns ();
2268 end_sequence ();
2270 add_equal_note (seq, t, POPCOUNT, op0, 0);
2271 emit_insn (seq);
2272 return t;
2275 /* Try calculating
2276 (parity:wide x)
2278 (parity:narrow (low (x) ^ high (x))) */
2279 static rtx
2280 expand_doubleword_parity (machine_mode mode, rtx op0, rtx target)
2282 rtx t = expand_binop (word_mode, xor_optab,
2283 operand_subword_force (op0, 0, mode),
2284 operand_subword_force (op0, 1, mode),
2285 NULL_RTX, 0, OPTAB_DIRECT);
2286 return expand_unop (word_mode, parity_optab, t, target, true);
2289 /* Try calculating
2290 (bswap:narrow x)
2292 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2293 static rtx
2294 widen_bswap (machine_mode mode, rtx op0, rtx target)
2296 enum mode_class mclass = GET_MODE_CLASS (mode);
2297 machine_mode wider_mode;
2298 rtx x;
2299 rtx_insn *last;
2301 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2302 return NULL_RTX;
2304 FOR_EACH_WIDER_MODE (wider_mode, mode)
2305 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2306 goto found;
2307 return NULL_RTX;
2309 found:
2310 last = get_last_insn ();
2312 x = widen_operand (op0, wider_mode, mode, true, true);
2313 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2315 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2316 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2317 if (x != 0)
2318 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2319 GET_MODE_BITSIZE (wider_mode)
2320 - GET_MODE_BITSIZE (mode),
2321 NULL_RTX, true);
2323 if (x != 0)
2325 if (target == 0)
2326 target = gen_reg_rtx (mode);
2327 emit_move_insn (target, gen_lowpart (mode, x));
2329 else
2330 delete_insns_since (last);
2332 return target;
2335 /* Try calculating bswap as two bswaps of two word-sized operands. */
2337 static rtx
2338 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2340 rtx t0, t1;
2342 t1 = expand_unop (word_mode, bswap_optab,
2343 operand_subword_force (op, 0, mode), NULL_RTX, true);
2344 t0 = expand_unop (word_mode, bswap_optab,
2345 operand_subword_force (op, 1, mode), NULL_RTX, true);
2347 if (target == 0 || !valid_multiword_target_p (target))
2348 target = gen_reg_rtx (mode);
2349 if (REG_P (target))
2350 emit_clobber (target);
2351 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2352 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2354 return target;
2357 /* Try calculating (parity x) as (and (popcount x) 1), where
2358 popcount can also be done in a wider mode. */
2359 static rtx
2360 expand_parity (machine_mode mode, rtx op0, rtx target)
2362 enum mode_class mclass = GET_MODE_CLASS (mode);
2363 if (CLASS_HAS_WIDER_MODES_P (mclass))
2365 machine_mode wider_mode;
2366 FOR_EACH_MODE_FROM (wider_mode, mode)
2368 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2370 rtx xop0, temp;
2371 rtx_insn *last;
2373 last = get_last_insn ();
2375 if (target == 0 || GET_MODE (target) != wider_mode)
2376 target = gen_reg_rtx (wider_mode);
2378 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2379 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2380 true);
2381 if (temp != 0)
2382 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2383 target, true, OPTAB_DIRECT);
2385 if (temp)
2387 if (mclass != MODE_INT
2388 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2389 return convert_to_mode (mode, temp, 0);
2390 else
2391 return gen_lowpart (mode, temp);
2393 else
2394 delete_insns_since (last);
2398 return 0;
2401 /* Try calculating ctz(x) as K - clz(x & -x) ,
2402 where K is GET_MODE_PRECISION(mode) - 1.
2404 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2405 don't have to worry about what the hardware does in that case. (If
2406 the clz instruction produces the usual value at 0, which is K, the
2407 result of this code sequence will be -1; expand_ffs, below, relies
2408 on this. It might be nice to have it be K instead, for consistency
2409 with the (very few) processors that provide a ctz with a defined
2410 value, but that would take one more instruction, and it would be
2411 less convenient for expand_ffs anyway. */
2413 static rtx
2414 expand_ctz (machine_mode mode, rtx op0, rtx target)
2416 rtx_insn *seq;
2417 rtx temp;
2419 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2420 return 0;
2422 start_sequence ();
2424 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2425 if (temp)
2426 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2427 true, OPTAB_DIRECT);
2428 if (temp)
2429 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2430 if (temp)
2431 temp = expand_binop (mode, sub_optab,
2432 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2433 temp, target,
2434 true, OPTAB_DIRECT);
2435 if (temp == 0)
2437 end_sequence ();
2438 return 0;
2441 seq = get_insns ();
2442 end_sequence ();
2444 add_equal_note (seq, temp, CTZ, op0, 0);
2445 emit_insn (seq);
2446 return temp;
2450 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2451 else with the sequence used by expand_clz.
2453 The ffs builtin promises to return zero for a zero value and ctz/clz
2454 may have an undefined value in that case. If they do not give us a
2455 convenient value, we have to generate a test and branch. */
2456 static rtx
2457 expand_ffs (machine_mode mode, rtx op0, rtx target)
2459 HOST_WIDE_INT val = 0;
2460 bool defined_at_zero = false;
2461 rtx temp;
2462 rtx_insn *seq;
2464 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2466 start_sequence ();
2468 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2469 if (!temp)
2470 goto fail;
2472 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2474 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2476 start_sequence ();
2477 temp = expand_ctz (mode, op0, 0);
2478 if (!temp)
2479 goto fail;
2481 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2483 defined_at_zero = true;
2484 val = (GET_MODE_PRECISION (mode) - 1) - val;
2487 else
2488 return 0;
2490 if (defined_at_zero && val == -1)
2491 /* No correction needed at zero. */;
2492 else
2494 /* We don't try to do anything clever with the situation found
2495 on some processors (eg Alpha) where ctz(0:mode) ==
2496 bitsize(mode). If someone can think of a way to send N to -1
2497 and leave alone all values in the range 0..N-1 (where N is a
2498 power of two), cheaper than this test-and-branch, please add it.
2500 The test-and-branch is done after the operation itself, in case
2501 the operation sets condition codes that can be recycled for this.
2502 (This is true on i386, for instance.) */
2504 rtx_code_label *nonzero_label = gen_label_rtx ();
2505 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2506 mode, true, nonzero_label);
2508 convert_move (temp, GEN_INT (-1), false);
2509 emit_label (nonzero_label);
2512 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2513 to produce a value in the range 0..bitsize. */
2514 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2515 target, false, OPTAB_DIRECT);
2516 if (!temp)
2517 goto fail;
2519 seq = get_insns ();
2520 end_sequence ();
2522 add_equal_note (seq, temp, FFS, op0, 0);
2523 emit_insn (seq);
2524 return temp;
2526 fail:
2527 end_sequence ();
2528 return 0;
2531 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2532 conditions, VAL may already be a SUBREG against which we cannot generate
2533 a further SUBREG. In this case, we expect forcing the value into a
2534 register will work around the situation. */
2536 static rtx
2537 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
2538 machine_mode imode)
2540 rtx ret;
2541 ret = lowpart_subreg (omode, val, imode);
2542 if (ret == NULL)
2544 val = force_reg (imode, val);
2545 ret = lowpart_subreg (omode, val, imode);
2546 gcc_assert (ret != NULL);
2548 return ret;
2551 /* Expand a floating point absolute value or negation operation via a
2552 logical operation on the sign bit. */
2554 static rtx
2555 expand_absneg_bit (enum rtx_code code, scalar_float_mode mode,
2556 rtx op0, rtx target)
2558 const struct real_format *fmt;
2559 int bitpos, word, nwords, i;
2560 machine_mode imode;
2561 rtx temp;
2562 rtx_insn *insns;
2564 /* The format has to have a simple sign bit. */
2565 fmt = REAL_MODE_FORMAT (mode);
2566 if (fmt == NULL)
2567 return NULL_RTX;
2569 bitpos = fmt->signbit_rw;
2570 if (bitpos < 0)
2571 return NULL_RTX;
2573 /* Don't create negative zeros if the format doesn't support them. */
2574 if (code == NEG && !fmt->has_signed_zero)
2575 return NULL_RTX;
2577 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2579 imode = int_mode_for_mode (mode);
2580 if (imode == BLKmode)
2581 return NULL_RTX;
2582 word = 0;
2583 nwords = 1;
2585 else
2587 imode = word_mode;
2589 if (FLOAT_WORDS_BIG_ENDIAN)
2590 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2591 else
2592 word = bitpos / BITS_PER_WORD;
2593 bitpos = bitpos % BITS_PER_WORD;
2594 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2597 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2598 if (code == ABS)
2599 mask = ~mask;
2601 if (target == 0
2602 || target == op0
2603 || (nwords > 1 && !valid_multiword_target_p (target)))
2604 target = gen_reg_rtx (mode);
2606 if (nwords > 1)
2608 start_sequence ();
2610 for (i = 0; i < nwords; ++i)
2612 rtx targ_piece = operand_subword (target, i, 1, mode);
2613 rtx op0_piece = operand_subword_force (op0, i, mode);
2615 if (i == word)
2617 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2618 op0_piece,
2619 immed_wide_int_const (mask, imode),
2620 targ_piece, 1, OPTAB_LIB_WIDEN);
2621 if (temp != targ_piece)
2622 emit_move_insn (targ_piece, temp);
2624 else
2625 emit_move_insn (targ_piece, op0_piece);
2628 insns = get_insns ();
2629 end_sequence ();
2631 emit_insn (insns);
2633 else
2635 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2636 gen_lowpart (imode, op0),
2637 immed_wide_int_const (mask, imode),
2638 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2639 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2641 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2642 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2643 target);
2646 return target;
2649 /* As expand_unop, but will fail rather than attempt the operation in a
2650 different mode or with a libcall. */
2651 static rtx
2652 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
2653 int unsignedp)
2655 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2657 struct expand_operand ops[2];
2658 enum insn_code icode = optab_handler (unoptab, mode);
2659 rtx_insn *last = get_last_insn ();
2660 rtx_insn *pat;
2662 create_output_operand (&ops[0], target, mode);
2663 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2664 pat = maybe_gen_insn (icode, 2, ops);
2665 if (pat)
2667 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2668 && ! add_equal_note (pat, ops[0].value,
2669 optab_to_code (unoptab),
2670 ops[1].value, NULL_RTX))
2672 delete_insns_since (last);
2673 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2676 emit_insn (pat);
2678 return ops[0].value;
2681 return 0;
2684 /* Generate code to perform an operation specified by UNOPTAB
2685 on operand OP0, with result having machine-mode MODE.
2687 UNSIGNEDP is for the case where we have to widen the operands
2688 to perform the operation. It says to use zero-extension.
2690 If TARGET is nonzero, the value
2691 is generated there, if it is convenient to do so.
2692 In all cases an rtx is returned for the locus of the value;
2693 this may or may not be TARGET. */
2696 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
2697 int unsignedp)
2699 enum mode_class mclass = GET_MODE_CLASS (mode);
2700 machine_mode wider_mode;
2701 scalar_float_mode float_mode;
2702 rtx temp;
2703 rtx libfunc;
2705 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
2706 if (temp)
2707 return temp;
2709 /* It can't be done in this mode. Can we open-code it in a wider mode? */
2711 /* Widening (or narrowing) clz needs special treatment. */
2712 if (unoptab == clz_optab)
2714 temp = widen_leading (mode, op0, target, unoptab);
2715 if (temp)
2716 return temp;
2718 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2719 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2721 temp = expand_doubleword_clz (mode, op0, target);
2722 if (temp)
2723 return temp;
2726 goto try_libcall;
2729 if (unoptab == clrsb_optab)
2731 temp = widen_leading (mode, op0, target, unoptab);
2732 if (temp)
2733 return temp;
2734 goto try_libcall;
2737 if (unoptab == popcount_optab
2738 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2739 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing
2740 && optimize_insn_for_speed_p ())
2742 temp = expand_doubleword_popcount (mode, op0, target);
2743 if (temp)
2744 return temp;
2747 if (unoptab == parity_optab
2748 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2749 && (optab_handler (unoptab, word_mode) != CODE_FOR_nothing
2750 || optab_handler (popcount_optab, word_mode) != CODE_FOR_nothing)
2751 && optimize_insn_for_speed_p ())
2753 temp = expand_doubleword_parity (mode, op0, target);
2754 if (temp)
2755 return temp;
2758 /* Widening (or narrowing) bswap needs special treatment. */
2759 if (unoptab == bswap_optab)
2761 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
2762 or ROTATERT. First try these directly; if this fails, then try the
2763 obvious pair of shifts with allowed widening, as this will probably
2764 be always more efficient than the other fallback methods. */
2765 if (mode == HImode)
2767 rtx_insn *last;
2768 rtx temp1, temp2;
2770 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
2772 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
2773 unsignedp, OPTAB_DIRECT);
2774 if (temp)
2775 return temp;
2778 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
2780 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
2781 unsignedp, OPTAB_DIRECT);
2782 if (temp)
2783 return temp;
2786 last = get_last_insn ();
2788 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
2789 unsignedp, OPTAB_WIDEN);
2790 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
2791 unsignedp, OPTAB_WIDEN);
2792 if (temp1 && temp2)
2794 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
2795 unsignedp, OPTAB_WIDEN);
2796 if (temp)
2797 return temp;
2800 delete_insns_since (last);
2803 temp = widen_bswap (mode, op0, target);
2804 if (temp)
2805 return temp;
2807 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2808 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2810 temp = expand_doubleword_bswap (mode, op0, target);
2811 if (temp)
2812 return temp;
2815 goto try_libcall;
2818 if (CLASS_HAS_WIDER_MODES_P (mclass))
2819 FOR_EACH_WIDER_MODE (wider_mode, mode)
2821 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2823 rtx xop0 = op0;
2824 rtx_insn *last = get_last_insn ();
2826 /* For certain operations, we need not actually extend
2827 the narrow operand, as long as we will truncate the
2828 results to the same narrowness. */
2830 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2831 (unoptab == neg_optab
2832 || unoptab == one_cmpl_optab)
2833 && mclass == MODE_INT);
2835 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2836 unsignedp);
2838 if (temp)
2840 if (mclass != MODE_INT
2841 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2843 if (target == 0)
2844 target = gen_reg_rtx (mode);
2845 convert_move (target, temp, 0);
2846 return target;
2848 else
2849 return gen_lowpart (mode, temp);
2851 else
2852 delete_insns_since (last);
2856 /* These can be done a word at a time. */
2857 if (unoptab == one_cmpl_optab
2858 && mclass == MODE_INT
2859 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
2860 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2862 int i;
2863 rtx_insn *insns;
2865 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
2866 target = gen_reg_rtx (mode);
2868 start_sequence ();
2870 /* Do the actual arithmetic. */
2871 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
2873 rtx target_piece = operand_subword (target, i, 1, mode);
2874 rtx x = expand_unop (word_mode, unoptab,
2875 operand_subword_force (op0, i, mode),
2876 target_piece, unsignedp);
2878 if (target_piece != x)
2879 emit_move_insn (target_piece, x);
2882 insns = get_insns ();
2883 end_sequence ();
2885 emit_insn (insns);
2886 return target;
2889 if (optab_to_code (unoptab) == NEG)
2891 /* Try negating floating point values by flipping the sign bit. */
2892 if (is_a <scalar_float_mode> (mode, &float_mode))
2894 temp = expand_absneg_bit (NEG, float_mode, op0, target);
2895 if (temp)
2896 return temp;
2899 /* If there is no negation pattern, and we have no negative zero,
2900 try subtracting from zero. */
2901 if (!HONOR_SIGNED_ZEROS (mode))
2903 temp = expand_binop (mode, (unoptab == negv_optab
2904 ? subv_optab : sub_optab),
2905 CONST0_RTX (mode), op0, target,
2906 unsignedp, OPTAB_DIRECT);
2907 if (temp)
2908 return temp;
2912 /* Try calculating parity (x) as popcount (x) % 2. */
2913 if (unoptab == parity_optab)
2915 temp = expand_parity (mode, op0, target);
2916 if (temp)
2917 return temp;
2920 /* Try implementing ffs (x) in terms of clz (x). */
2921 if (unoptab == ffs_optab)
2923 temp = expand_ffs (mode, op0, target);
2924 if (temp)
2925 return temp;
2928 /* Try implementing ctz (x) in terms of clz (x). */
2929 if (unoptab == ctz_optab)
2931 temp = expand_ctz (mode, op0, target);
2932 if (temp)
2933 return temp;
2936 try_libcall:
2937 /* Now try a library call in this mode. */
2938 libfunc = optab_libfunc (unoptab, mode);
2939 if (libfunc)
2941 rtx_insn *insns;
2942 rtx value;
2943 rtx eq_value;
2944 machine_mode outmode = mode;
2946 /* All of these functions return small values. Thus we choose to
2947 have them return something that isn't a double-word. */
2948 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
2949 || unoptab == clrsb_optab || unoptab == popcount_optab
2950 || unoptab == parity_optab)
2951 outmode
2952 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
2953 optab_libfunc (unoptab, mode)));
2955 start_sequence ();
2957 /* Pass 1 for NO_QUEUE so we don't lose any increments
2958 if the libcall is cse'd or moved. */
2959 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
2960 1, op0, mode);
2961 insns = get_insns ();
2962 end_sequence ();
2964 target = gen_reg_rtx (outmode);
2965 bool trapv = trapv_unoptab_p (unoptab);
2966 if (trapv)
2967 eq_value = NULL_RTX;
2968 else
2970 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
2971 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
2972 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
2973 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
2974 eq_value = simplify_gen_unary (ZERO_EXTEND,
2975 outmode, eq_value, mode);
2977 emit_libcall_block_1 (insns, target, value, eq_value, trapv);
2979 return target;
2982 /* It can't be done in this mode. Can we do it in a wider mode? */
2984 if (CLASS_HAS_WIDER_MODES_P (mclass))
2986 FOR_EACH_WIDER_MODE (wider_mode, mode)
2988 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
2989 || optab_libfunc (unoptab, wider_mode))
2991 rtx xop0 = op0;
2992 rtx_insn *last = get_last_insn ();
2994 /* For certain operations, we need not actually extend
2995 the narrow operand, as long as we will truncate the
2996 results to the same narrowness. */
2997 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2998 (unoptab == neg_optab
2999 || unoptab == one_cmpl_optab
3000 || unoptab == bswap_optab)
3001 && mclass == MODE_INT);
3003 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3004 unsignedp);
3006 /* If we are generating clz using wider mode, adjust the
3007 result. Similarly for clrsb. */
3008 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3009 && temp != 0)
3010 temp = expand_binop
3011 (wider_mode, sub_optab, temp,
3012 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3013 - GET_MODE_PRECISION (mode),
3014 wider_mode),
3015 target, true, OPTAB_DIRECT);
3017 /* Likewise for bswap. */
3018 if (unoptab == bswap_optab && temp != 0)
3020 gcc_assert (GET_MODE_PRECISION (wider_mode)
3021 == GET_MODE_BITSIZE (wider_mode)
3022 && GET_MODE_PRECISION (mode)
3023 == GET_MODE_BITSIZE (mode));
3025 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3026 GET_MODE_BITSIZE (wider_mode)
3027 - GET_MODE_BITSIZE (mode),
3028 NULL_RTX, true);
3031 if (temp)
3033 if (mclass != MODE_INT)
3035 if (target == 0)
3036 target = gen_reg_rtx (mode);
3037 convert_move (target, temp, 0);
3038 return target;
3040 else
3041 return gen_lowpart (mode, temp);
3043 else
3044 delete_insns_since (last);
3049 /* One final attempt at implementing negation via subtraction,
3050 this time allowing widening of the operand. */
3051 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3053 rtx temp;
3054 temp = expand_binop (mode,
3055 unoptab == negv_optab ? subv_optab : sub_optab,
3056 CONST0_RTX (mode), op0,
3057 target, unsignedp, OPTAB_LIB_WIDEN);
3058 if (temp)
3059 return temp;
3062 return 0;
3065 /* Emit code to compute the absolute value of OP0, with result to
3066 TARGET if convenient. (TARGET may be 0.) The return value says
3067 where the result actually is to be found.
3069 MODE is the mode of the operand; the mode of the result is
3070 different but can be deduced from MODE.
3075 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3076 int result_unsignedp)
3078 rtx temp;
3080 if (GET_MODE_CLASS (mode) != MODE_INT
3081 || ! flag_trapv)
3082 result_unsignedp = 1;
3084 /* First try to do it with a special abs instruction. */
3085 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3086 op0, target, 0);
3087 if (temp != 0)
3088 return temp;
3090 /* For floating point modes, try clearing the sign bit. */
3091 scalar_float_mode float_mode;
3092 if (is_a <scalar_float_mode> (mode, &float_mode))
3094 temp = expand_absneg_bit (ABS, float_mode, op0, target);
3095 if (temp)
3096 return temp;
3099 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3100 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3101 && !HONOR_SIGNED_ZEROS (mode))
3103 rtx_insn *last = get_last_insn ();
3105 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3106 op0, NULL_RTX, 0);
3107 if (temp != 0)
3108 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3109 OPTAB_WIDEN);
3111 if (temp != 0)
3112 return temp;
3114 delete_insns_since (last);
3117 /* If this machine has expensive jumps, we can do integer absolute
3118 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3119 where W is the width of MODE. */
3121 if (GET_MODE_CLASS (mode) == MODE_INT
3122 && BRANCH_COST (optimize_insn_for_speed_p (),
3123 false) >= 2)
3125 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3126 GET_MODE_PRECISION (mode) - 1,
3127 NULL_RTX, 0);
3129 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3130 OPTAB_LIB_WIDEN);
3131 if (temp != 0)
3132 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3133 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3135 if (temp != 0)
3136 return temp;
3139 return NULL_RTX;
3143 expand_abs (machine_mode mode, rtx op0, rtx target,
3144 int result_unsignedp, int safe)
3146 rtx temp;
3147 rtx_code_label *op1;
3149 if (GET_MODE_CLASS (mode) != MODE_INT
3150 || ! flag_trapv)
3151 result_unsignedp = 1;
3153 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3154 if (temp != 0)
3155 return temp;
3157 /* If that does not win, use conditional jump and negate. */
3159 /* It is safe to use the target if it is the same
3160 as the source if this is also a pseudo register */
3161 if (op0 == target && REG_P (op0)
3162 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3163 safe = 1;
3165 op1 = gen_label_rtx ();
3166 if (target == 0 || ! safe
3167 || GET_MODE (target) != mode
3168 || (MEM_P (target) && MEM_VOLATILE_P (target))
3169 || (REG_P (target)
3170 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3171 target = gen_reg_rtx (mode);
3173 emit_move_insn (target, op0);
3174 NO_DEFER_POP;
3176 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3177 NULL_RTX, NULL, op1,
3178 profile_probability::uninitialized ());
3180 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3181 target, target, 0);
3182 if (op0 != target)
3183 emit_move_insn (target, op0);
3184 emit_label (op1);
3185 OK_DEFER_POP;
3186 return target;
3189 /* Emit code to compute the one's complement absolute value of OP0
3190 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3191 (TARGET may be NULL_RTX.) The return value says where the result
3192 actually is to be found.
3194 MODE is the mode of the operand; the mode of the result is
3195 different but can be deduced from MODE. */
3198 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3200 rtx temp;
3202 /* Not applicable for floating point modes. */
3203 if (FLOAT_MODE_P (mode))
3204 return NULL_RTX;
3206 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3207 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3209 rtx_insn *last = get_last_insn ();
3211 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3212 if (temp != 0)
3213 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3214 OPTAB_WIDEN);
3216 if (temp != 0)
3217 return temp;
3219 delete_insns_since (last);
3222 /* If this machine has expensive jumps, we can do one's complement
3223 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3225 if (GET_MODE_CLASS (mode) == MODE_INT
3226 && BRANCH_COST (optimize_insn_for_speed_p (),
3227 false) >= 2)
3229 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3230 GET_MODE_PRECISION (mode) - 1,
3231 NULL_RTX, 0);
3233 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3234 OPTAB_LIB_WIDEN);
3236 if (temp != 0)
3237 return temp;
3240 return NULL_RTX;
3243 /* A subroutine of expand_copysign, perform the copysign operation using the
3244 abs and neg primitives advertised to exist on the target. The assumption
3245 is that we have a split register file, and leaving op0 in fp registers,
3246 and not playing with subregs so much, will help the register allocator. */
3248 static rtx
3249 expand_copysign_absneg (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3250 int bitpos, bool op0_is_abs)
3252 machine_mode imode;
3253 enum insn_code icode;
3254 rtx sign;
3255 rtx_code_label *label;
3257 if (target == op1)
3258 target = NULL_RTX;
3260 /* Check if the back end provides an insn that handles signbit for the
3261 argument's mode. */
3262 icode = optab_handler (signbit_optab, mode);
3263 if (icode != CODE_FOR_nothing)
3265 imode = insn_data[(int) icode].operand[0].mode;
3266 sign = gen_reg_rtx (imode);
3267 emit_unop_insn (icode, sign, op1, UNKNOWN);
3269 else
3271 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3273 imode = int_mode_for_mode (mode);
3274 if (imode == BLKmode)
3275 return NULL_RTX;
3276 op1 = gen_lowpart (imode, op1);
3278 else
3280 int word;
3282 imode = word_mode;
3283 if (FLOAT_WORDS_BIG_ENDIAN)
3284 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3285 else
3286 word = bitpos / BITS_PER_WORD;
3287 bitpos = bitpos % BITS_PER_WORD;
3288 op1 = operand_subword_force (op1, word, mode);
3291 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3292 sign = expand_binop (imode, and_optab, op1,
3293 immed_wide_int_const (mask, imode),
3294 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3297 if (!op0_is_abs)
3299 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3300 if (op0 == NULL)
3301 return NULL_RTX;
3302 target = op0;
3304 else
3306 if (target == NULL_RTX)
3307 target = copy_to_reg (op0);
3308 else
3309 emit_move_insn (target, op0);
3312 label = gen_label_rtx ();
3313 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3315 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3316 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3317 else
3318 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3319 if (op0 != target)
3320 emit_move_insn (target, op0);
3322 emit_label (label);
3324 return target;
3328 /* A subroutine of expand_copysign, perform the entire copysign operation
3329 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3330 is true if op0 is known to have its sign bit clear. */
3332 static rtx
3333 expand_copysign_bit (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3334 int bitpos, bool op0_is_abs)
3336 machine_mode imode;
3337 int word, nwords, i;
3338 rtx temp;
3339 rtx_insn *insns;
3341 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3343 imode = int_mode_for_mode (mode);
3344 if (imode == BLKmode)
3345 return NULL_RTX;
3346 word = 0;
3347 nwords = 1;
3349 else
3351 imode = word_mode;
3353 if (FLOAT_WORDS_BIG_ENDIAN)
3354 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3355 else
3356 word = bitpos / BITS_PER_WORD;
3357 bitpos = bitpos % BITS_PER_WORD;
3358 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3361 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3363 if (target == 0
3364 || target == op0
3365 || target == op1
3366 || (nwords > 1 && !valid_multiword_target_p (target)))
3367 target = gen_reg_rtx (mode);
3369 if (nwords > 1)
3371 start_sequence ();
3373 for (i = 0; i < nwords; ++i)
3375 rtx targ_piece = operand_subword (target, i, 1, mode);
3376 rtx op0_piece = operand_subword_force (op0, i, mode);
3378 if (i == word)
3380 if (!op0_is_abs)
3381 op0_piece
3382 = expand_binop (imode, and_optab, op0_piece,
3383 immed_wide_int_const (~mask, imode),
3384 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3385 op1 = expand_binop (imode, and_optab,
3386 operand_subword_force (op1, i, mode),
3387 immed_wide_int_const (mask, imode),
3388 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3390 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3391 targ_piece, 1, OPTAB_LIB_WIDEN);
3392 if (temp != targ_piece)
3393 emit_move_insn (targ_piece, temp);
3395 else
3396 emit_move_insn (targ_piece, op0_piece);
3399 insns = get_insns ();
3400 end_sequence ();
3402 emit_insn (insns);
3404 else
3406 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3407 immed_wide_int_const (mask, imode),
3408 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3410 op0 = gen_lowpart (imode, op0);
3411 if (!op0_is_abs)
3412 op0 = expand_binop (imode, and_optab, op0,
3413 immed_wide_int_const (~mask, imode),
3414 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3416 temp = expand_binop (imode, ior_optab, op0, op1,
3417 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3418 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3421 return target;
3424 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3425 scalar floating point mode. Return NULL if we do not know how to
3426 expand the operation inline. */
3429 expand_copysign (rtx op0, rtx op1, rtx target)
3431 scalar_float_mode mode;
3432 const struct real_format *fmt;
3433 bool op0_is_abs;
3434 rtx temp;
3436 mode = as_a <scalar_float_mode> (GET_MODE (op0));
3437 gcc_assert (GET_MODE (op1) == mode);
3439 /* First try to do it with a special instruction. */
3440 temp = expand_binop (mode, copysign_optab, op0, op1,
3441 target, 0, OPTAB_DIRECT);
3442 if (temp)
3443 return temp;
3445 fmt = REAL_MODE_FORMAT (mode);
3446 if (fmt == NULL || !fmt->has_signed_zero)
3447 return NULL_RTX;
3449 op0_is_abs = false;
3450 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3452 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3453 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3454 op0_is_abs = true;
3457 if (fmt->signbit_ro >= 0
3458 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3459 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3460 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3462 temp = expand_copysign_absneg (mode, op0, op1, target,
3463 fmt->signbit_ro, op0_is_abs);
3464 if (temp)
3465 return temp;
3468 if (fmt->signbit_rw < 0)
3469 return NULL_RTX;
3470 return expand_copysign_bit (mode, op0, op1, target,
3471 fmt->signbit_rw, op0_is_abs);
3474 /* Generate an instruction whose insn-code is INSN_CODE,
3475 with two operands: an output TARGET and an input OP0.
3476 TARGET *must* be nonzero, and the output is always stored there.
3477 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3478 the value that is stored into TARGET.
3480 Return false if expansion failed. */
3482 bool
3483 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3484 enum rtx_code code)
3486 struct expand_operand ops[2];
3487 rtx_insn *pat;
3489 create_output_operand (&ops[0], target, GET_MODE (target));
3490 create_input_operand (&ops[1], op0, GET_MODE (op0));
3491 pat = maybe_gen_insn (icode, 2, ops);
3492 if (!pat)
3493 return false;
3495 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3496 && code != UNKNOWN)
3497 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3499 emit_insn (pat);
3501 if (ops[0].value != target)
3502 emit_move_insn (target, ops[0].value);
3503 return true;
3505 /* Generate an instruction whose insn-code is INSN_CODE,
3506 with two operands: an output TARGET and an input OP0.
3507 TARGET *must* be nonzero, and the output is always stored there.
3508 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3509 the value that is stored into TARGET. */
3511 void
3512 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3514 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3515 gcc_assert (ok);
3518 struct no_conflict_data
3520 rtx target;
3521 rtx_insn *first, *insn;
3522 bool must_stay;
3525 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3526 the currently examined clobber / store has to stay in the list of
3527 insns that constitute the actual libcall block. */
3528 static void
3529 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3531 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3533 /* If this inns directly contributes to setting the target, it must stay. */
3534 if (reg_overlap_mentioned_p (p->target, dest))
3535 p->must_stay = true;
3536 /* If we haven't committed to keeping any other insns in the list yet,
3537 there is nothing more to check. */
3538 else if (p->insn == p->first)
3539 return;
3540 /* If this insn sets / clobbers a register that feeds one of the insns
3541 already in the list, this insn has to stay too. */
3542 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3543 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3544 || reg_used_between_p (dest, p->first, p->insn)
3545 /* Likewise if this insn depends on a register set by a previous
3546 insn in the list, or if it sets a result (presumably a hard
3547 register) that is set or clobbered by a previous insn.
3548 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3549 SET_DEST perform the former check on the address, and the latter
3550 check on the MEM. */
3551 || (GET_CODE (set) == SET
3552 && (modified_in_p (SET_SRC (set), p->first)
3553 || modified_in_p (SET_DEST (set), p->first)
3554 || modified_between_p (SET_SRC (set), p->first, p->insn)
3555 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3556 p->must_stay = true;
3560 /* Emit code to make a call to a constant function or a library call.
3562 INSNS is a list containing all insns emitted in the call.
3563 These insns leave the result in RESULT. Our block is to copy RESULT
3564 to TARGET, which is logically equivalent to EQUIV.
3566 We first emit any insns that set a pseudo on the assumption that these are
3567 loading constants into registers; doing so allows them to be safely cse'ed
3568 between blocks. Then we emit all the other insns in the block, followed by
3569 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3570 note with an operand of EQUIV. */
3572 static void
3573 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3574 bool equiv_may_trap)
3576 rtx final_dest = target;
3577 rtx_insn *next, *last, *insn;
3579 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3580 into a MEM later. Protect the libcall block from this change. */
3581 if (! REG_P (target) || REG_USERVAR_P (target))
3582 target = gen_reg_rtx (GET_MODE (target));
3584 /* If we're using non-call exceptions, a libcall corresponding to an
3585 operation that may trap may also trap. */
3586 /* ??? See the comment in front of make_reg_eh_region_note. */
3587 if (cfun->can_throw_non_call_exceptions
3588 && (equiv_may_trap || may_trap_p (equiv)))
3590 for (insn = insns; insn; insn = NEXT_INSN (insn))
3591 if (CALL_P (insn))
3593 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3594 if (note)
3596 int lp_nr = INTVAL (XEXP (note, 0));
3597 if (lp_nr == 0 || lp_nr == INT_MIN)
3598 remove_note (insn, note);
3602 else
3604 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3605 reg note to indicate that this call cannot throw or execute a nonlocal
3606 goto (unless there is already a REG_EH_REGION note, in which case
3607 we update it). */
3608 for (insn = insns; insn; insn = NEXT_INSN (insn))
3609 if (CALL_P (insn))
3610 make_reg_eh_region_note_nothrow_nononlocal (insn);
3613 /* First emit all insns that set pseudos. Remove them from the list as
3614 we go. Avoid insns that set pseudos which were referenced in previous
3615 insns. These can be generated by move_by_pieces, for example,
3616 to update an address. Similarly, avoid insns that reference things
3617 set in previous insns. */
3619 for (insn = insns; insn; insn = next)
3621 rtx set = single_set (insn);
3623 next = NEXT_INSN (insn);
3625 if (set != 0 && REG_P (SET_DEST (set))
3626 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3628 struct no_conflict_data data;
3630 data.target = const0_rtx;
3631 data.first = insns;
3632 data.insn = insn;
3633 data.must_stay = 0;
3634 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3635 if (! data.must_stay)
3637 if (PREV_INSN (insn))
3638 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3639 else
3640 insns = next;
3642 if (next)
3643 SET_PREV_INSN (next) = PREV_INSN (insn);
3645 add_insn (insn);
3649 /* Some ports use a loop to copy large arguments onto the stack.
3650 Don't move anything outside such a loop. */
3651 if (LABEL_P (insn))
3652 break;
3655 /* Write the remaining insns followed by the final copy. */
3656 for (insn = insns; insn; insn = next)
3658 next = NEXT_INSN (insn);
3660 add_insn (insn);
3663 last = emit_move_insn (target, result);
3664 if (equiv)
3665 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3667 if (final_dest != target)
3668 emit_move_insn (final_dest, target);
3671 void
3672 emit_libcall_block (rtx_insn *insns, rtx target, rtx result, rtx equiv)
3674 emit_libcall_block_1 (insns, target, result, equiv, false);
3677 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3678 PURPOSE describes how this comparison will be used. CODE is the rtx
3679 comparison code we will be using.
3681 ??? Actually, CODE is slightly weaker than that. A target is still
3682 required to implement all of the normal bcc operations, but not
3683 required to implement all (or any) of the unordered bcc operations. */
3686 can_compare_p (enum rtx_code code, machine_mode mode,
3687 enum can_compare_purpose purpose)
3689 rtx test;
3690 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3693 enum insn_code icode;
3695 if (purpose == ccp_jump
3696 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
3697 && insn_operand_matches (icode, 0, test))
3698 return 1;
3699 if (purpose == ccp_store_flag
3700 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
3701 && insn_operand_matches (icode, 1, test))
3702 return 1;
3703 if (purpose == ccp_cmov
3704 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
3705 return 1;
3707 mode = GET_MODE_WIDER_MODE (mode).else_void ();
3708 PUT_MODE (test, mode);
3710 while (mode != VOIDmode);
3712 return 0;
3715 /* This function is called when we are going to emit a compare instruction that
3716 compares the values found in X and Y, using the rtl operator COMPARISON.
3718 If they have mode BLKmode, then SIZE specifies the size of both operands.
3720 UNSIGNEDP nonzero says that the operands are unsigned;
3721 this matters if they need to be widened (as given by METHODS).
3723 *PTEST is where the resulting comparison RTX is returned or NULL_RTX
3724 if we failed to produce one.
3726 *PMODE is the mode of the inputs (in case they are const_int).
3728 This function performs all the setup necessary so that the caller only has
3729 to emit a single comparison insn. This setup can involve doing a BLKmode
3730 comparison or emitting a library call to perform the comparison if no insn
3731 is available to handle it.
3732 The values which are passed in through pointers can be modified; the caller
3733 should perform the comparison on the modified values. Constant
3734 comparisons must have already been folded. */
3736 static void
3737 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3738 int unsignedp, enum optab_methods methods,
3739 rtx *ptest, machine_mode *pmode)
3741 machine_mode mode = *pmode;
3742 rtx libfunc, test;
3743 machine_mode cmp_mode;
3744 enum mode_class mclass;
3746 /* The other methods are not needed. */
3747 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
3748 || methods == OPTAB_LIB_WIDEN);
3750 /* If we are optimizing, force expensive constants into a register. */
3751 if (CONSTANT_P (x) && optimize
3752 && (rtx_cost (x, mode, COMPARE, 0, optimize_insn_for_speed_p ())
3753 > COSTS_N_INSNS (1)))
3754 x = force_reg (mode, x);
3756 if (CONSTANT_P (y) && optimize
3757 && (rtx_cost (y, mode, COMPARE, 1, optimize_insn_for_speed_p ())
3758 > COSTS_N_INSNS (1)))
3759 y = force_reg (mode, y);
3761 #if HAVE_cc0
3762 /* Make sure if we have a canonical comparison. The RTL
3763 documentation states that canonical comparisons are required only
3764 for targets which have cc0. */
3765 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
3766 #endif
3768 /* Don't let both operands fail to indicate the mode. */
3769 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
3770 x = force_reg (mode, x);
3771 if (mode == VOIDmode)
3772 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
3774 /* Handle all BLKmode compares. */
3776 if (mode == BLKmode)
3778 machine_mode result_mode;
3779 enum insn_code cmp_code;
3780 rtx result;
3781 rtx opalign
3782 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
3784 gcc_assert (size);
3786 /* Try to use a memory block compare insn - either cmpstr
3787 or cmpmem will do. */
3788 FOR_EACH_MODE_IN_CLASS (cmp_mode, MODE_INT)
3790 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
3791 if (cmp_code == CODE_FOR_nothing)
3792 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
3793 if (cmp_code == CODE_FOR_nothing)
3794 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
3795 if (cmp_code == CODE_FOR_nothing)
3796 continue;
3798 /* Must make sure the size fits the insn's mode. */
3799 if ((CONST_INT_P (size)
3800 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
3801 || (GET_MODE_BITSIZE (GET_MODE (size))
3802 > GET_MODE_BITSIZE (cmp_mode)))
3803 continue;
3805 result_mode = insn_data[cmp_code].operand[0].mode;
3806 result = gen_reg_rtx (result_mode);
3807 size = convert_to_mode (cmp_mode, size, 1);
3808 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
3810 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
3811 *pmode = result_mode;
3812 return;
3815 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
3816 goto fail;
3818 /* Otherwise call a library function. */
3819 result = emit_block_comp_via_libcall (XEXP (x, 0), XEXP (y, 0), size);
3821 x = result;
3822 y = const0_rtx;
3823 mode = TYPE_MODE (integer_type_node);
3824 methods = OPTAB_LIB_WIDEN;
3825 unsignedp = false;
3828 /* Don't allow operands to the compare to trap, as that can put the
3829 compare and branch in different basic blocks. */
3830 if (cfun->can_throw_non_call_exceptions)
3832 if (may_trap_p (x))
3833 x = copy_to_reg (x);
3834 if (may_trap_p (y))
3835 y = copy_to_reg (y);
3838 if (GET_MODE_CLASS (mode) == MODE_CC)
3840 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
3841 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3842 gcc_assert (icode != CODE_FOR_nothing
3843 && insn_operand_matches (icode, 0, test));
3844 *ptest = test;
3845 return;
3848 mclass = GET_MODE_CLASS (mode);
3849 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3850 FOR_EACH_MODE_FROM (cmp_mode, mode)
3852 enum insn_code icode;
3853 icode = optab_handler (cbranch_optab, cmp_mode);
3854 if (icode != CODE_FOR_nothing
3855 && insn_operand_matches (icode, 0, test))
3857 rtx_insn *last = get_last_insn ();
3858 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
3859 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
3860 if (op0 && op1
3861 && insn_operand_matches (icode, 1, op0)
3862 && insn_operand_matches (icode, 2, op1))
3864 XEXP (test, 0) = op0;
3865 XEXP (test, 1) = op1;
3866 *ptest = test;
3867 *pmode = cmp_mode;
3868 return;
3870 delete_insns_since (last);
3873 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
3874 break;
3877 if (methods != OPTAB_LIB_WIDEN)
3878 goto fail;
3880 if (!SCALAR_FLOAT_MODE_P (mode))
3882 rtx result;
3883 machine_mode ret_mode;
3885 /* Handle a libcall just for the mode we are using. */
3886 libfunc = optab_libfunc (cmp_optab, mode);
3887 gcc_assert (libfunc);
3889 /* If we want unsigned, and this mode has a distinct unsigned
3890 comparison routine, use that. */
3891 if (unsignedp)
3893 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
3894 if (ulibfunc)
3895 libfunc = ulibfunc;
3898 ret_mode = targetm.libgcc_cmp_return_mode ();
3899 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3900 ret_mode, 2, x, mode, y, mode);
3902 /* There are two kinds of comparison routines. Biased routines
3903 return 0/1/2, and unbiased routines return -1/0/1. Other parts
3904 of gcc expect that the comparison operation is equivalent
3905 to the modified comparison. For signed comparisons compare the
3906 result against 1 in the biased case, and zero in the unbiased
3907 case. For unsigned comparisons always compare against 1 after
3908 biasing the unbiased result by adding 1. This gives us a way to
3909 represent LTU.
3910 The comparisons in the fixed-point helper library are always
3911 biased. */
3912 x = result;
3913 y = const1_rtx;
3915 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
3917 if (unsignedp)
3918 x = plus_constant (ret_mode, result, 1);
3919 else
3920 y = const0_rtx;
3923 *pmode = ret_mode;
3924 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
3925 ptest, pmode);
3927 else
3928 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
3930 return;
3932 fail:
3933 *ptest = NULL_RTX;
3936 /* Before emitting an insn with code ICODE, make sure that X, which is going
3937 to be used for operand OPNUM of the insn, is converted from mode MODE to
3938 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
3939 that it is accepted by the operand predicate. Return the new value. */
3942 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
3943 machine_mode wider_mode, int unsignedp)
3945 if (mode != wider_mode)
3946 x = convert_modes (wider_mode, mode, x, unsignedp);
3948 if (!insn_operand_matches (icode, opnum, x))
3950 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
3951 if (reload_completed)
3952 return NULL_RTX;
3953 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
3954 return NULL_RTX;
3955 x = copy_to_mode_reg (op_mode, x);
3958 return x;
3961 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
3962 we can do the branch. */
3964 static void
3965 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label,
3966 profile_probability prob)
3968 machine_mode optab_mode;
3969 enum mode_class mclass;
3970 enum insn_code icode;
3971 rtx_insn *insn;
3973 mclass = GET_MODE_CLASS (mode);
3974 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
3975 icode = optab_handler (cbranch_optab, optab_mode);
3977 gcc_assert (icode != CODE_FOR_nothing);
3978 gcc_assert (insn_operand_matches (icode, 0, test));
3979 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
3980 XEXP (test, 1), label));
3981 if (prob.initialized_p ()
3982 && profile_status_for_fn (cfun) != PROFILE_ABSENT
3983 && insn
3984 && JUMP_P (insn)
3985 && any_condjump_p (insn)
3986 && !find_reg_note (insn, REG_BR_PROB, 0))
3987 add_reg_br_prob_note (insn, prob);
3990 /* Generate code to compare X with Y so that the condition codes are
3991 set and to jump to LABEL if the condition is true. If X is a
3992 constant and Y is not a constant, then the comparison is swapped to
3993 ensure that the comparison RTL has the canonical form.
3995 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
3996 need to be widened. UNSIGNEDP is also used to select the proper
3997 branch condition code.
3999 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4001 MODE is the mode of the inputs (in case they are const_int).
4003 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4004 It will be potentially converted into an unsigned variant based on
4005 UNSIGNEDP to select a proper jump instruction.
4007 PROB is the probability of jumping to LABEL. */
4009 void
4010 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4011 machine_mode mode, int unsignedp, rtx label,
4012 profile_probability prob)
4014 rtx op0 = x, op1 = y;
4015 rtx test;
4017 /* Swap operands and condition to ensure canonical RTL. */
4018 if (swap_commutative_operands_p (x, y)
4019 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4021 op0 = y, op1 = x;
4022 comparison = swap_condition (comparison);
4025 /* If OP0 is still a constant, then both X and Y must be constants
4026 or the opposite comparison is not supported. Force X into a register
4027 to create canonical RTL. */
4028 if (CONSTANT_P (op0))
4029 op0 = force_reg (mode, op0);
4031 if (unsignedp)
4032 comparison = unsigned_condition (comparison);
4034 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4035 &test, &mode);
4036 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4040 /* Emit a library call comparison between floating point X and Y.
4041 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4043 static void
4044 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4045 rtx *ptest, machine_mode *pmode)
4047 enum rtx_code swapped = swap_condition (comparison);
4048 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4049 machine_mode orig_mode = GET_MODE (x);
4050 machine_mode mode, cmp_mode;
4051 rtx true_rtx, false_rtx;
4052 rtx value, target, equiv;
4053 rtx_insn *insns;
4054 rtx libfunc = 0;
4055 bool reversed_p = false;
4056 cmp_mode = targetm.libgcc_cmp_return_mode ();
4058 FOR_EACH_MODE_FROM (mode, orig_mode)
4060 if (code_to_optab (comparison)
4061 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4062 break;
4064 if (code_to_optab (swapped)
4065 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4067 std::swap (x, y);
4068 comparison = swapped;
4069 break;
4072 if (code_to_optab (reversed)
4073 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4075 comparison = reversed;
4076 reversed_p = true;
4077 break;
4081 gcc_assert (mode != VOIDmode);
4083 if (mode != orig_mode)
4085 x = convert_to_mode (mode, x, 0);
4086 y = convert_to_mode (mode, y, 0);
4089 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4090 the RTL. The allows the RTL optimizers to delete the libcall if the
4091 condition can be determined at compile-time. */
4092 if (comparison == UNORDERED
4093 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4095 true_rtx = const_true_rtx;
4096 false_rtx = const0_rtx;
4098 else
4100 switch (comparison)
4102 case EQ:
4103 true_rtx = const0_rtx;
4104 false_rtx = const_true_rtx;
4105 break;
4107 case NE:
4108 true_rtx = const_true_rtx;
4109 false_rtx = const0_rtx;
4110 break;
4112 case GT:
4113 true_rtx = const1_rtx;
4114 false_rtx = const0_rtx;
4115 break;
4117 case GE:
4118 true_rtx = const0_rtx;
4119 false_rtx = constm1_rtx;
4120 break;
4122 case LT:
4123 true_rtx = constm1_rtx;
4124 false_rtx = const0_rtx;
4125 break;
4127 case LE:
4128 true_rtx = const0_rtx;
4129 false_rtx = const1_rtx;
4130 break;
4132 default:
4133 gcc_unreachable ();
4137 if (comparison == UNORDERED)
4139 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4140 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4141 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4142 temp, const_true_rtx, equiv);
4144 else
4146 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4147 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4148 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4149 equiv, true_rtx, false_rtx);
4152 start_sequence ();
4153 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4154 cmp_mode, 2, x, mode, y, mode);
4155 insns = get_insns ();
4156 end_sequence ();
4158 target = gen_reg_rtx (cmp_mode);
4159 emit_libcall_block (insns, target, value, equiv);
4161 if (comparison == UNORDERED
4162 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4163 || reversed_p)
4164 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4165 else
4166 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4168 *pmode = cmp_mode;
4171 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4173 void
4174 emit_indirect_jump (rtx loc)
4176 if (!targetm.have_indirect_jump ())
4177 sorry ("indirect jumps are not available on this target");
4178 else
4180 struct expand_operand ops[1];
4181 create_address_operand (&ops[0], loc);
4182 expand_jump_insn (targetm.code_for_indirect_jump, 1, ops);
4183 emit_barrier ();
4188 /* Emit a conditional move instruction if the machine supports one for that
4189 condition and machine mode.
4191 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4192 the mode to use should they be constants. If it is VOIDmode, they cannot
4193 both be constants.
4195 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4196 should be stored there. MODE is the mode to use should they be constants.
4197 If it is VOIDmode, they cannot both be constants.
4199 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4200 is not supported. */
4203 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4204 machine_mode cmode, rtx op2, rtx op3,
4205 machine_mode mode, int unsignedp)
4207 rtx comparison;
4208 rtx_insn *last;
4209 enum insn_code icode;
4210 enum rtx_code reversed;
4212 /* If the two source operands are identical, that's just a move. */
4214 if (rtx_equal_p (op2, op3))
4216 if (!target)
4217 target = gen_reg_rtx (mode);
4219 emit_move_insn (target, op3);
4220 return target;
4223 /* If one operand is constant, make it the second one. Only do this
4224 if the other operand is not constant as well. */
4226 if (swap_commutative_operands_p (op0, op1))
4228 std::swap (op0, op1);
4229 code = swap_condition (code);
4232 /* get_condition will prefer to generate LT and GT even if the old
4233 comparison was against zero, so undo that canonicalization here since
4234 comparisons against zero are cheaper. */
4235 if (code == LT && op1 == const1_rtx)
4236 code = LE, op1 = const0_rtx;
4237 else if (code == GT && op1 == constm1_rtx)
4238 code = GE, op1 = const0_rtx;
4240 if (cmode == VOIDmode)
4241 cmode = GET_MODE (op0);
4243 enum rtx_code orig_code = code;
4244 bool swapped = false;
4245 if (swap_commutative_operands_p (op2, op3)
4246 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4247 != UNKNOWN))
4249 std::swap (op2, op3);
4250 code = reversed;
4251 swapped = true;
4254 if (mode == VOIDmode)
4255 mode = GET_MODE (op2);
4257 icode = direct_optab_handler (movcc_optab, mode);
4259 if (icode == CODE_FOR_nothing)
4260 return NULL_RTX;
4262 if (!target)
4263 target = gen_reg_rtx (mode);
4265 for (int pass = 0; ; pass++)
4267 code = unsignedp ? unsigned_condition (code) : code;
4268 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4270 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4271 punt and let the caller figure out how best to deal with this
4272 situation. */
4273 if (COMPARISON_P (comparison))
4275 saved_pending_stack_adjust save;
4276 save_pending_stack_adjust (&save);
4277 last = get_last_insn ();
4278 do_pending_stack_adjust ();
4279 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4280 GET_CODE (comparison), NULL_RTX, unsignedp,
4281 OPTAB_WIDEN, &comparison, &cmode);
4282 if (comparison)
4284 struct expand_operand ops[4];
4286 create_output_operand (&ops[0], target, mode);
4287 create_fixed_operand (&ops[1], comparison);
4288 create_input_operand (&ops[2], op2, mode);
4289 create_input_operand (&ops[3], op3, mode);
4290 if (maybe_expand_insn (icode, 4, ops))
4292 if (ops[0].value != target)
4293 convert_move (target, ops[0].value, false);
4294 return target;
4297 delete_insns_since (last);
4298 restore_pending_stack_adjust (&save);
4301 if (pass == 1)
4302 return NULL_RTX;
4304 /* If the preferred op2/op3 order is not usable, retry with other
4305 operand order, perhaps it will expand successfully. */
4306 if (swapped)
4307 code = orig_code;
4308 else if ((reversed = reversed_comparison_code_parts (orig_code, op0, op1,
4309 NULL))
4310 != UNKNOWN)
4311 code = reversed;
4312 else
4313 return NULL_RTX;
4314 std::swap (op2, op3);
4319 /* Emit a conditional negate or bitwise complement using the
4320 negcc or notcc optabs if available. Return NULL_RTX if such operations
4321 are not available. Otherwise return the RTX holding the result.
4322 TARGET is the desired destination of the result. COMP is the comparison
4323 on which to negate. If COND is true move into TARGET the negation
4324 or bitwise complement of OP1. Otherwise move OP2 into TARGET.
4325 CODE is either NEG or NOT. MODE is the machine mode in which the
4326 operation is performed. */
4329 emit_conditional_neg_or_complement (rtx target, rtx_code code,
4330 machine_mode mode, rtx cond, rtx op1,
4331 rtx op2)
4333 optab op = unknown_optab;
4334 if (code == NEG)
4335 op = negcc_optab;
4336 else if (code == NOT)
4337 op = notcc_optab;
4338 else
4339 gcc_unreachable ();
4341 insn_code icode = direct_optab_handler (op, mode);
4343 if (icode == CODE_FOR_nothing)
4344 return NULL_RTX;
4346 if (!target)
4347 target = gen_reg_rtx (mode);
4349 rtx_insn *last = get_last_insn ();
4350 struct expand_operand ops[4];
4352 create_output_operand (&ops[0], target, mode);
4353 create_fixed_operand (&ops[1], cond);
4354 create_input_operand (&ops[2], op1, mode);
4355 create_input_operand (&ops[3], op2, mode);
4357 if (maybe_expand_insn (icode, 4, ops))
4359 if (ops[0].value != target)
4360 convert_move (target, ops[0].value, false);
4362 return target;
4364 delete_insns_since (last);
4365 return NULL_RTX;
4368 /* Emit a conditional addition instruction if the machine supports one for that
4369 condition and machine mode.
4371 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4372 the mode to use should they be constants. If it is VOIDmode, they cannot
4373 both be constants.
4375 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4376 should be stored there. MODE is the mode to use should they be constants.
4377 If it is VOIDmode, they cannot both be constants.
4379 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4380 is not supported. */
4383 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4384 machine_mode cmode, rtx op2, rtx op3,
4385 machine_mode mode, int unsignedp)
4387 rtx comparison;
4388 rtx_insn *last;
4389 enum insn_code icode;
4391 /* If one operand is constant, make it the second one. Only do this
4392 if the other operand is not constant as well. */
4394 if (swap_commutative_operands_p (op0, op1))
4396 std::swap (op0, op1);
4397 code = swap_condition (code);
4400 /* get_condition will prefer to generate LT and GT even if the old
4401 comparison was against zero, so undo that canonicalization here since
4402 comparisons against zero are cheaper. */
4403 if (code == LT && op1 == const1_rtx)
4404 code = LE, op1 = const0_rtx;
4405 else if (code == GT && op1 == constm1_rtx)
4406 code = GE, op1 = const0_rtx;
4408 if (cmode == VOIDmode)
4409 cmode = GET_MODE (op0);
4411 if (mode == VOIDmode)
4412 mode = GET_MODE (op2);
4414 icode = optab_handler (addcc_optab, mode);
4416 if (icode == CODE_FOR_nothing)
4417 return 0;
4419 if (!target)
4420 target = gen_reg_rtx (mode);
4422 code = unsignedp ? unsigned_condition (code) : code;
4423 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4425 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4426 return NULL and let the caller figure out how best to deal with this
4427 situation. */
4428 if (!COMPARISON_P (comparison))
4429 return NULL_RTX;
4431 do_pending_stack_adjust ();
4432 last = get_last_insn ();
4433 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4434 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4435 &comparison, &cmode);
4436 if (comparison)
4438 struct expand_operand ops[4];
4440 create_output_operand (&ops[0], target, mode);
4441 create_fixed_operand (&ops[1], comparison);
4442 create_input_operand (&ops[2], op2, mode);
4443 create_input_operand (&ops[3], op3, mode);
4444 if (maybe_expand_insn (icode, 4, ops))
4446 if (ops[0].value != target)
4447 convert_move (target, ops[0].value, false);
4448 return target;
4451 delete_insns_since (last);
4452 return NULL_RTX;
4455 /* These functions attempt to generate an insn body, rather than
4456 emitting the insn, but if the gen function already emits them, we
4457 make no attempt to turn them back into naked patterns. */
4459 /* Generate and return an insn body to add Y to X. */
4461 rtx_insn *
4462 gen_add2_insn (rtx x, rtx y)
4464 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4466 gcc_assert (insn_operand_matches (icode, 0, x));
4467 gcc_assert (insn_operand_matches (icode, 1, x));
4468 gcc_assert (insn_operand_matches (icode, 2, y));
4470 return GEN_FCN (icode) (x, x, y);
4473 /* Generate and return an insn body to add r1 and c,
4474 storing the result in r0. */
4476 rtx_insn *
4477 gen_add3_insn (rtx r0, rtx r1, rtx c)
4479 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4481 if (icode == CODE_FOR_nothing
4482 || !insn_operand_matches (icode, 0, r0)
4483 || !insn_operand_matches (icode, 1, r1)
4484 || !insn_operand_matches (icode, 2, c))
4485 return NULL;
4487 return GEN_FCN (icode) (r0, r1, c);
4491 have_add2_insn (rtx x, rtx y)
4493 enum insn_code icode;
4495 gcc_assert (GET_MODE (x) != VOIDmode);
4497 icode = optab_handler (add_optab, GET_MODE (x));
4499 if (icode == CODE_FOR_nothing)
4500 return 0;
4502 if (!insn_operand_matches (icode, 0, x)
4503 || !insn_operand_matches (icode, 1, x)
4504 || !insn_operand_matches (icode, 2, y))
4505 return 0;
4507 return 1;
4510 /* Generate and return an insn body to add Y to X. */
4512 rtx_insn *
4513 gen_addptr3_insn (rtx x, rtx y, rtx z)
4515 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4517 gcc_assert (insn_operand_matches (icode, 0, x));
4518 gcc_assert (insn_operand_matches (icode, 1, y));
4519 gcc_assert (insn_operand_matches (icode, 2, z));
4521 return GEN_FCN (icode) (x, y, z);
4524 /* Return true if the target implements an addptr pattern and X, Y,
4525 and Z are valid for the pattern predicates. */
4528 have_addptr3_insn (rtx x, rtx y, rtx z)
4530 enum insn_code icode;
4532 gcc_assert (GET_MODE (x) != VOIDmode);
4534 icode = optab_handler (addptr3_optab, GET_MODE (x));
4536 if (icode == CODE_FOR_nothing)
4537 return 0;
4539 if (!insn_operand_matches (icode, 0, x)
4540 || !insn_operand_matches (icode, 1, y)
4541 || !insn_operand_matches (icode, 2, z))
4542 return 0;
4544 return 1;
4547 /* Generate and return an insn body to subtract Y from X. */
4549 rtx_insn *
4550 gen_sub2_insn (rtx x, rtx y)
4552 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4554 gcc_assert (insn_operand_matches (icode, 0, x));
4555 gcc_assert (insn_operand_matches (icode, 1, x));
4556 gcc_assert (insn_operand_matches (icode, 2, y));
4558 return GEN_FCN (icode) (x, x, y);
4561 /* Generate and return an insn body to subtract r1 and c,
4562 storing the result in r0. */
4564 rtx_insn *
4565 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4567 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4569 if (icode == CODE_FOR_nothing
4570 || !insn_operand_matches (icode, 0, r0)
4571 || !insn_operand_matches (icode, 1, r1)
4572 || !insn_operand_matches (icode, 2, c))
4573 return NULL;
4575 return GEN_FCN (icode) (r0, r1, c);
4579 have_sub2_insn (rtx x, rtx y)
4581 enum insn_code icode;
4583 gcc_assert (GET_MODE (x) != VOIDmode);
4585 icode = optab_handler (sub_optab, GET_MODE (x));
4587 if (icode == CODE_FOR_nothing)
4588 return 0;
4590 if (!insn_operand_matches (icode, 0, x)
4591 || !insn_operand_matches (icode, 1, x)
4592 || !insn_operand_matches (icode, 2, y))
4593 return 0;
4595 return 1;
4598 /* Generate the body of an insn to extend Y (with mode MFROM)
4599 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4601 rtx_insn *
4602 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4603 machine_mode mfrom, int unsignedp)
4605 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4606 return GEN_FCN (icode) (x, y);
4609 /* Generate code to convert FROM to floating point
4610 and store in TO. FROM must be fixed point and not VOIDmode.
4611 UNSIGNEDP nonzero means regard FROM as unsigned.
4612 Normally this is done by correcting the final value
4613 if it is negative. */
4615 void
4616 expand_float (rtx to, rtx from, int unsignedp)
4618 enum insn_code icode;
4619 rtx target = to;
4620 machine_mode fmode, imode;
4621 bool can_do_signed = false;
4623 /* Crash now, because we won't be able to decide which mode to use. */
4624 gcc_assert (GET_MODE (from) != VOIDmode);
4626 /* Look for an insn to do the conversion. Do it in the specified
4627 modes if possible; otherwise convert either input, output or both to
4628 wider mode. If the integer mode is wider than the mode of FROM,
4629 we can do the conversion signed even if the input is unsigned. */
4631 FOR_EACH_MODE_FROM (fmode, GET_MODE (to))
4632 FOR_EACH_MODE_FROM (imode, GET_MODE (from))
4634 int doing_unsigned = unsignedp;
4636 if (fmode != GET_MODE (to)
4637 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
4638 continue;
4640 icode = can_float_p (fmode, imode, unsignedp);
4641 if (icode == CODE_FOR_nothing && unsignedp)
4643 enum insn_code scode = can_float_p (fmode, imode, 0);
4644 if (scode != CODE_FOR_nothing)
4645 can_do_signed = true;
4646 if (imode != GET_MODE (from))
4647 icode = scode, doing_unsigned = 0;
4650 if (icode != CODE_FOR_nothing)
4652 if (imode != GET_MODE (from))
4653 from = convert_to_mode (imode, from, unsignedp);
4655 if (fmode != GET_MODE (to))
4656 target = gen_reg_rtx (fmode);
4658 emit_unop_insn (icode, target, from,
4659 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4661 if (target != to)
4662 convert_move (to, target, 0);
4663 return;
4667 /* Unsigned integer, and no way to convert directly. Convert as signed,
4668 then unconditionally adjust the result. */
4669 if (unsignedp && can_do_signed)
4671 rtx_code_label *label = gen_label_rtx ();
4672 rtx temp;
4673 REAL_VALUE_TYPE offset;
4675 /* Look for a usable floating mode FMODE wider than the source and at
4676 least as wide as the target. Using FMODE will avoid rounding woes
4677 with unsigned values greater than the signed maximum value. */
4679 FOR_EACH_MODE_FROM (fmode, GET_MODE (to))
4680 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4681 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4682 break;
4684 if (fmode == VOIDmode)
4686 /* There is no such mode. Pretend the target is wide enough. */
4687 fmode = GET_MODE (to);
4689 /* Avoid double-rounding when TO is narrower than FROM. */
4690 if ((significand_size (fmode) + 1)
4691 < GET_MODE_PRECISION (GET_MODE (from)))
4693 rtx temp1;
4694 rtx_code_label *neglabel = gen_label_rtx ();
4696 /* Don't use TARGET if it isn't a register, is a hard register,
4697 or is the wrong mode. */
4698 if (!REG_P (target)
4699 || REGNO (target) < FIRST_PSEUDO_REGISTER
4700 || GET_MODE (target) != fmode)
4701 target = gen_reg_rtx (fmode);
4703 imode = GET_MODE (from);
4704 do_pending_stack_adjust ();
4706 /* Test whether the sign bit is set. */
4707 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4708 0, neglabel);
4710 /* The sign bit is not set. Convert as signed. */
4711 expand_float (target, from, 0);
4712 emit_jump_insn (targetm.gen_jump (label));
4713 emit_barrier ();
4715 /* The sign bit is set.
4716 Convert to a usable (positive signed) value by shifting right
4717 one bit, while remembering if a nonzero bit was shifted
4718 out; i.e., compute (from & 1) | (from >> 1). */
4720 emit_label (neglabel);
4721 temp = expand_binop (imode, and_optab, from, const1_rtx,
4722 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4723 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
4724 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4725 OPTAB_LIB_WIDEN);
4726 expand_float (target, temp, 0);
4728 /* Multiply by 2 to undo the shift above. */
4729 temp = expand_binop (fmode, add_optab, target, target,
4730 target, 0, OPTAB_LIB_WIDEN);
4731 if (temp != target)
4732 emit_move_insn (target, temp);
4734 do_pending_stack_adjust ();
4735 emit_label (label);
4736 goto done;
4740 /* If we are about to do some arithmetic to correct for an
4741 unsigned operand, do it in a pseudo-register. */
4743 if (GET_MODE (to) != fmode
4744 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
4745 target = gen_reg_rtx (fmode);
4747 /* Convert as signed integer to floating. */
4748 expand_float (target, from, 0);
4750 /* If FROM is negative (and therefore TO is negative),
4751 correct its value by 2**bitwidth. */
4753 do_pending_stack_adjust ();
4754 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
4755 0, label);
4758 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
4759 temp = expand_binop (fmode, add_optab, target,
4760 const_double_from_real_value (offset, fmode),
4761 target, 0, OPTAB_LIB_WIDEN);
4762 if (temp != target)
4763 emit_move_insn (target, temp);
4765 do_pending_stack_adjust ();
4766 emit_label (label);
4767 goto done;
4770 /* No hardware instruction available; call a library routine. */
4772 rtx libfunc;
4773 rtx_insn *insns;
4774 rtx value;
4775 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
4777 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
4778 from = convert_to_mode (SImode, from, unsignedp);
4780 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
4781 gcc_assert (libfunc);
4783 start_sequence ();
4785 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4786 GET_MODE (to), 1, from,
4787 GET_MODE (from));
4788 insns = get_insns ();
4789 end_sequence ();
4791 emit_libcall_block (insns, target, value,
4792 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
4793 GET_MODE (to), from));
4796 done:
4798 /* Copy result to requested destination
4799 if we have been computing in a temp location. */
4801 if (target != to)
4803 if (GET_MODE (target) == GET_MODE (to))
4804 emit_move_insn (to, target);
4805 else
4806 convert_move (to, target, 0);
4810 /* Generate code to convert FROM to fixed point and store in TO. FROM
4811 must be floating point. */
4813 void
4814 expand_fix (rtx to, rtx from, int unsignedp)
4816 enum insn_code icode;
4817 rtx target = to;
4818 machine_mode fmode, imode;
4819 bool must_trunc = false;
4821 /* We first try to find a pair of modes, one real and one integer, at
4822 least as wide as FROM and TO, respectively, in which we can open-code
4823 this conversion. If the integer mode is wider than the mode of TO,
4824 we can do the conversion either signed or unsigned. */
4826 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
4827 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
4829 int doing_unsigned = unsignedp;
4831 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
4832 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
4833 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
4835 if (icode != CODE_FOR_nothing)
4837 rtx_insn *last = get_last_insn ();
4838 if (fmode != GET_MODE (from))
4839 from = convert_to_mode (fmode, from, 0);
4841 if (must_trunc)
4843 rtx temp = gen_reg_rtx (GET_MODE (from));
4844 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
4845 temp, 0);
4848 if (imode != GET_MODE (to))
4849 target = gen_reg_rtx (imode);
4851 if (maybe_emit_unop_insn (icode, target, from,
4852 doing_unsigned ? UNSIGNED_FIX : FIX))
4854 if (target != to)
4855 convert_move (to, target, unsignedp);
4856 return;
4858 delete_insns_since (last);
4862 /* For an unsigned conversion, there is one more way to do it.
4863 If we have a signed conversion, we generate code that compares
4864 the real value to the largest representable positive number. If if
4865 is smaller, the conversion is done normally. Otherwise, subtract
4866 one plus the highest signed number, convert, and add it back.
4868 We only need to check all real modes, since we know we didn't find
4869 anything with a wider integer mode.
4871 This code used to extend FP value into mode wider than the destination.
4872 This is needed for decimal float modes which cannot accurately
4873 represent one plus the highest signed number of the same size, but
4874 not for binary modes. Consider, for instance conversion from SFmode
4875 into DImode.
4877 The hot path through the code is dealing with inputs smaller than 2^63
4878 and doing just the conversion, so there is no bits to lose.
4880 In the other path we know the value is positive in the range 2^63..2^64-1
4881 inclusive. (as for other input overflow happens and result is undefined)
4882 So we know that the most important bit set in mantissa corresponds to
4883 2^63. The subtraction of 2^63 should not generate any rounding as it
4884 simply clears out that bit. The rest is trivial. */
4886 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
4887 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
4888 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
4889 && (!DECIMAL_FLOAT_MODE_P (fmode)
4890 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
4892 int bitsize;
4893 REAL_VALUE_TYPE offset;
4894 rtx limit;
4895 rtx_code_label *lab1, *lab2;
4896 rtx_insn *insn;
4898 bitsize = GET_MODE_PRECISION (GET_MODE (to));
4899 real_2expN (&offset, bitsize - 1, fmode);
4900 limit = const_double_from_real_value (offset, fmode);
4901 lab1 = gen_label_rtx ();
4902 lab2 = gen_label_rtx ();
4904 if (fmode != GET_MODE (from))
4905 from = convert_to_mode (fmode, from, 0);
4907 /* See if we need to do the subtraction. */
4908 do_pending_stack_adjust ();
4909 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
4910 0, lab1);
4912 /* If not, do the signed "fix" and branch around fixup code. */
4913 expand_fix (to, from, 0);
4914 emit_jump_insn (targetm.gen_jump (lab2));
4915 emit_barrier ();
4917 /* Otherwise, subtract 2**(N-1), convert to signed number,
4918 then add 2**(N-1). Do the addition using XOR since this
4919 will often generate better code. */
4920 emit_label (lab1);
4921 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
4922 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4923 expand_fix (to, target, 0);
4924 target = expand_binop (GET_MODE (to), xor_optab, to,
4925 gen_int_mode
4926 (HOST_WIDE_INT_1 << (bitsize - 1),
4927 GET_MODE (to)),
4928 to, 1, OPTAB_LIB_WIDEN);
4930 if (target != to)
4931 emit_move_insn (to, target);
4933 emit_label (lab2);
4935 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
4937 /* Make a place for a REG_NOTE and add it. */
4938 insn = emit_move_insn (to, to);
4939 set_dst_reg_note (insn, REG_EQUAL,
4940 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
4941 copy_rtx (from)),
4942 to);
4945 return;
4948 /* We can't do it with an insn, so use a library call. But first ensure
4949 that the mode of TO is at least as wide as SImode, since those are the
4950 only library calls we know about. */
4952 if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
4954 target = gen_reg_rtx (SImode);
4956 expand_fix (target, from, unsignedp);
4958 else
4960 rtx_insn *insns;
4961 rtx value;
4962 rtx libfunc;
4964 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
4965 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
4966 gcc_assert (libfunc);
4968 start_sequence ();
4970 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4971 GET_MODE (to), 1, from,
4972 GET_MODE (from));
4973 insns = get_insns ();
4974 end_sequence ();
4976 emit_libcall_block (insns, target, value,
4977 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
4978 GET_MODE (to), from));
4981 if (target != to)
4983 if (GET_MODE (to) == GET_MODE (target))
4984 emit_move_insn (to, target);
4985 else
4986 convert_move (to, target, 0);
4991 /* Promote integer arguments for a libcall if necessary.
4992 emit_library_call_value cannot do the promotion because it does not
4993 know if it should do a signed or unsigned promotion. This is because
4994 there are no tree types defined for libcalls. */
4996 static rtx
4997 prepare_libcall_arg (rtx arg, int uintp)
4999 machine_mode mode = GET_MODE (arg);
5000 machine_mode arg_mode;
5001 if (SCALAR_INT_MODE_P (mode))
5003 /* If we need to promote the integer function argument we need to do
5004 it here instead of inside emit_library_call_value because in
5005 emit_library_call_value we don't know if we should do a signed or
5006 unsigned promotion. */
5008 int unsigned_p = 0;
5009 arg_mode = promote_function_mode (NULL_TREE, mode,
5010 &unsigned_p, NULL_TREE, 0);
5011 if (arg_mode != mode)
5012 return convert_to_mode (arg_mode, arg, uintp);
5014 return arg;
5017 /* Generate code to convert FROM or TO a fixed-point.
5018 If UINTP is true, either TO or FROM is an unsigned integer.
5019 If SATP is true, we need to saturate the result. */
5021 void
5022 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5024 machine_mode to_mode = GET_MODE (to);
5025 machine_mode from_mode = GET_MODE (from);
5026 convert_optab tab;
5027 enum rtx_code this_code;
5028 enum insn_code code;
5029 rtx_insn *insns;
5030 rtx value;
5031 rtx libfunc;
5033 if (to_mode == from_mode)
5035 emit_move_insn (to, from);
5036 return;
5039 if (uintp)
5041 tab = satp ? satfractuns_optab : fractuns_optab;
5042 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5044 else
5046 tab = satp ? satfract_optab : fract_optab;
5047 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5049 code = convert_optab_handler (tab, to_mode, from_mode);
5050 if (code != CODE_FOR_nothing)
5052 emit_unop_insn (code, to, from, this_code);
5053 return;
5056 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5057 gcc_assert (libfunc);
5059 from = prepare_libcall_arg (from, uintp);
5060 from_mode = GET_MODE (from);
5062 start_sequence ();
5063 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5064 1, from, from_mode);
5065 insns = get_insns ();
5066 end_sequence ();
5068 emit_libcall_block (insns, to, value,
5069 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5072 /* Generate code to convert FROM to fixed point and store in TO. FROM
5073 must be floating point, TO must be signed. Use the conversion optab
5074 TAB to do the conversion. */
5076 bool
5077 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5079 enum insn_code icode;
5080 rtx target = to;
5081 machine_mode fmode, imode;
5083 /* We first try to find a pair of modes, one real and one integer, at
5084 least as wide as FROM and TO, respectively, in which we can open-code
5085 this conversion. If the integer mode is wider than the mode of TO,
5086 we can do the conversion either signed or unsigned. */
5088 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
5089 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
5091 icode = convert_optab_handler (tab, imode, fmode);
5092 if (icode != CODE_FOR_nothing)
5094 rtx_insn *last = get_last_insn ();
5095 if (fmode != GET_MODE (from))
5096 from = convert_to_mode (fmode, from, 0);
5098 if (imode != GET_MODE (to))
5099 target = gen_reg_rtx (imode);
5101 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5103 delete_insns_since (last);
5104 continue;
5106 if (target != to)
5107 convert_move (to, target, 0);
5108 return true;
5112 return false;
5115 /* Report whether we have an instruction to perform the operation
5116 specified by CODE on operands of mode MODE. */
5118 have_insn_for (enum rtx_code code, machine_mode mode)
5120 return (code_to_optab (code)
5121 && (optab_handler (code_to_optab (code), mode)
5122 != CODE_FOR_nothing));
5125 /* Print information about the current contents of the optabs on
5126 STDERR. */
5128 DEBUG_FUNCTION void
5129 debug_optab_libfuncs (void)
5131 int i, j, k;
5133 /* Dump the arithmetic optabs. */
5134 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
5135 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5137 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
5138 if (l)
5140 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5141 fprintf (stderr, "%s\t%s:\t%s\n",
5142 GET_RTX_NAME (optab_to_code ((optab) i)),
5143 GET_MODE_NAME (j),
5144 XSTR (l, 0));
5148 /* Dump the conversion optabs. */
5149 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
5150 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5151 for (k = 0; k < NUM_MACHINE_MODES; ++k)
5153 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
5154 (machine_mode) k);
5155 if (l)
5157 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5158 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
5159 GET_RTX_NAME (optab_to_code ((optab) i)),
5160 GET_MODE_NAME (j),
5161 GET_MODE_NAME (k),
5162 XSTR (l, 0));
5167 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5168 CODE. Return 0 on failure. */
5170 rtx_insn *
5171 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
5173 machine_mode mode = GET_MODE (op1);
5174 enum insn_code icode;
5175 rtx_insn *insn;
5176 rtx trap_rtx;
5178 if (mode == VOIDmode)
5179 return 0;
5181 icode = optab_handler (ctrap_optab, mode);
5182 if (icode == CODE_FOR_nothing)
5183 return 0;
5185 /* Some targets only accept a zero trap code. */
5186 if (!insn_operand_matches (icode, 3, tcode))
5187 return 0;
5189 do_pending_stack_adjust ();
5190 start_sequence ();
5191 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
5192 &trap_rtx, &mode);
5193 if (!trap_rtx)
5194 insn = NULL;
5195 else
5196 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
5197 tcode);
5199 /* If that failed, then give up. */
5200 if (insn == 0)
5202 end_sequence ();
5203 return 0;
5206 emit_insn (insn);
5207 insn = get_insns ();
5208 end_sequence ();
5209 return insn;
5212 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
5213 or unsigned operation code. */
5215 enum rtx_code
5216 get_rtx_code (enum tree_code tcode, bool unsignedp)
5218 enum rtx_code code;
5219 switch (tcode)
5221 case EQ_EXPR:
5222 code = EQ;
5223 break;
5224 case NE_EXPR:
5225 code = NE;
5226 break;
5227 case LT_EXPR:
5228 code = unsignedp ? LTU : LT;
5229 break;
5230 case LE_EXPR:
5231 code = unsignedp ? LEU : LE;
5232 break;
5233 case GT_EXPR:
5234 code = unsignedp ? GTU : GT;
5235 break;
5236 case GE_EXPR:
5237 code = unsignedp ? GEU : GE;
5238 break;
5240 case UNORDERED_EXPR:
5241 code = UNORDERED;
5242 break;
5243 case ORDERED_EXPR:
5244 code = ORDERED;
5245 break;
5246 case UNLT_EXPR:
5247 code = UNLT;
5248 break;
5249 case UNLE_EXPR:
5250 code = UNLE;
5251 break;
5252 case UNGT_EXPR:
5253 code = UNGT;
5254 break;
5255 case UNGE_EXPR:
5256 code = UNGE;
5257 break;
5258 case UNEQ_EXPR:
5259 code = UNEQ;
5260 break;
5261 case LTGT_EXPR:
5262 code = LTGT;
5263 break;
5265 case BIT_AND_EXPR:
5266 code = AND;
5267 break;
5269 case BIT_IOR_EXPR:
5270 code = IOR;
5271 break;
5273 default:
5274 gcc_unreachable ();
5276 return code;
5279 /* Return a comparison rtx of mode CMP_MODE for COND. Use UNSIGNEDP to
5280 select signed or unsigned operators. OPNO holds the index of the
5281 first comparison operand for insn ICODE. Do not generate the
5282 compare instruction itself. */
5284 static rtx
5285 vector_compare_rtx (machine_mode cmp_mode, enum tree_code tcode,
5286 tree t_op0, tree t_op1, bool unsignedp,
5287 enum insn_code icode, unsigned int opno)
5289 struct expand_operand ops[2];
5290 rtx rtx_op0, rtx_op1;
5291 machine_mode m0, m1;
5292 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
5294 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
5296 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
5297 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
5298 cases, use the original mode. */
5299 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
5300 EXPAND_STACK_PARM);
5301 m0 = GET_MODE (rtx_op0);
5302 if (m0 == VOIDmode)
5303 m0 = TYPE_MODE (TREE_TYPE (t_op0));
5305 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
5306 EXPAND_STACK_PARM);
5307 m1 = GET_MODE (rtx_op1);
5308 if (m1 == VOIDmode)
5309 m1 = TYPE_MODE (TREE_TYPE (t_op1));
5311 create_input_operand (&ops[0], rtx_op0, m0);
5312 create_input_operand (&ops[1], rtx_op1, m1);
5313 if (!maybe_legitimize_operands (icode, opno, 2, ops))
5314 gcc_unreachable ();
5315 return gen_rtx_fmt_ee (rcode, cmp_mode, ops[0].value, ops[1].value);
5318 /* Checks if vec_perm mask SEL is a constant equivalent to a shift of the first
5319 vec_perm operand, assuming the second operand is a constant vector of zeroes.
5320 Return the shift distance in bits if so, or NULL_RTX if the vec_perm is not a
5321 shift. */
5322 static rtx
5323 shift_amt_for_vec_perm_mask (rtx sel)
5325 unsigned int i, first, nelt = GET_MODE_NUNITS (GET_MODE (sel));
5326 unsigned int bitsize = GET_MODE_UNIT_BITSIZE (GET_MODE (sel));
5328 if (GET_CODE (sel) != CONST_VECTOR)
5329 return NULL_RTX;
5331 first = INTVAL (CONST_VECTOR_ELT (sel, 0));
5332 if (first >= nelt)
5333 return NULL_RTX;
5334 for (i = 1; i < nelt; i++)
5336 int idx = INTVAL (CONST_VECTOR_ELT (sel, i));
5337 unsigned int expected = i + first;
5338 /* Indices into the second vector are all equivalent. */
5339 if (idx < 0 || (MIN (nelt, (unsigned) idx) != MIN (nelt, expected)))
5340 return NULL_RTX;
5343 return GEN_INT (first * bitsize);
5346 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
5348 static rtx
5349 expand_vec_perm_1 (enum insn_code icode, rtx target,
5350 rtx v0, rtx v1, rtx sel)
5352 machine_mode tmode = GET_MODE (target);
5353 machine_mode smode = GET_MODE (sel);
5354 struct expand_operand ops[4];
5356 create_output_operand (&ops[0], target, tmode);
5357 create_input_operand (&ops[3], sel, smode);
5359 /* Make an effort to preserve v0 == v1. The target expander is able to
5360 rely on this to determine if we're permuting a single input operand. */
5361 if (rtx_equal_p (v0, v1))
5363 if (!insn_operand_matches (icode, 1, v0))
5364 v0 = force_reg (tmode, v0);
5365 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
5366 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
5368 create_fixed_operand (&ops[1], v0);
5369 create_fixed_operand (&ops[2], v0);
5371 else
5373 create_input_operand (&ops[1], v0, tmode);
5374 create_input_operand (&ops[2], v1, tmode);
5377 if (maybe_expand_insn (icode, 4, ops))
5378 return ops[0].value;
5379 return NULL_RTX;
5382 /* Generate instructions for vec_perm optab given its mode
5383 and three operands. */
5386 expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
5388 enum insn_code icode;
5389 machine_mode qimode;
5390 unsigned int i, w, e, u;
5391 rtx tmp, sel_qi = NULL;
5392 rtvec vec;
5394 if (!target || GET_MODE (target) != mode)
5395 target = gen_reg_rtx (mode);
5397 w = GET_MODE_SIZE (mode);
5398 e = GET_MODE_NUNITS (mode);
5399 u = GET_MODE_UNIT_SIZE (mode);
5401 /* Set QIMODE to a different vector mode with byte elements.
5402 If no such mode, or if MODE already has byte elements, use VOIDmode. */
5403 qimode = VOIDmode;
5404 if (GET_MODE_INNER (mode) != QImode)
5406 qimode = mode_for_vector (QImode, w);
5407 if (!VECTOR_MODE_P (qimode))
5408 qimode = VOIDmode;
5411 /* If the input is a constant, expand it specially. */
5412 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
5413 if (GET_CODE (sel) == CONST_VECTOR)
5415 /* See if this can be handled with a vec_shr. We only do this if the
5416 second vector is all zeroes. */
5417 enum insn_code shift_code = optab_handler (vec_shr_optab, mode);
5418 enum insn_code shift_code_qi = ((qimode != VOIDmode && qimode != mode)
5419 ? optab_handler (vec_shr_optab, qimode)
5420 : CODE_FOR_nothing);
5421 rtx shift_amt = NULL_RTX;
5422 if (v1 == CONST0_RTX (GET_MODE (v1))
5423 && (shift_code != CODE_FOR_nothing
5424 || shift_code_qi != CODE_FOR_nothing))
5426 shift_amt = shift_amt_for_vec_perm_mask (sel);
5427 if (shift_amt)
5429 struct expand_operand ops[3];
5430 if (shift_code != CODE_FOR_nothing)
5432 create_output_operand (&ops[0], target, mode);
5433 create_input_operand (&ops[1], v0, mode);
5434 create_convert_operand_from_type (&ops[2], shift_amt,
5435 sizetype);
5436 if (maybe_expand_insn (shift_code, 3, ops))
5437 return ops[0].value;
5439 if (shift_code_qi != CODE_FOR_nothing)
5441 tmp = gen_reg_rtx (qimode);
5442 create_output_operand (&ops[0], tmp, qimode);
5443 create_input_operand (&ops[1], gen_lowpart (qimode, v0),
5444 qimode);
5445 create_convert_operand_from_type (&ops[2], shift_amt,
5446 sizetype);
5447 if (maybe_expand_insn (shift_code_qi, 3, ops))
5448 return gen_lowpart (mode, ops[0].value);
5453 icode = direct_optab_handler (vec_perm_const_optab, mode);
5454 if (icode != CODE_FOR_nothing)
5456 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
5457 if (tmp)
5458 return tmp;
5461 /* Fall back to a constant byte-based permutation. */
5462 if (qimode != VOIDmode)
5464 vec = rtvec_alloc (w);
5465 for (i = 0; i < e; ++i)
5467 unsigned int j, this_e;
5469 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
5470 this_e &= 2 * e - 1;
5471 this_e *= u;
5473 for (j = 0; j < u; ++j)
5474 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
5476 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
5478 icode = direct_optab_handler (vec_perm_const_optab, qimode);
5479 if (icode != CODE_FOR_nothing)
5481 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
5482 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
5483 gen_lowpart (qimode, v1), sel_qi);
5484 if (tmp)
5485 return gen_lowpart (mode, tmp);
5490 /* Otherwise expand as a fully variable permuation. */
5491 icode = direct_optab_handler (vec_perm_optab, mode);
5492 if (icode != CODE_FOR_nothing)
5494 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
5495 if (tmp)
5496 return tmp;
5499 /* As a special case to aid several targets, lower the element-based
5500 permutation to a byte-based permutation and try again. */
5501 if (qimode == VOIDmode)
5502 return NULL_RTX;
5503 icode = direct_optab_handler (vec_perm_optab, qimode);
5504 if (icode == CODE_FOR_nothing)
5505 return NULL_RTX;
5507 if (sel_qi == NULL)
5509 /* Multiply each element by its byte size. */
5510 machine_mode selmode = GET_MODE (sel);
5511 if (u == 2)
5512 sel = expand_simple_binop (selmode, PLUS, sel, sel,
5513 NULL, 0, OPTAB_DIRECT);
5514 else
5515 sel = expand_simple_binop (selmode, ASHIFT, sel,
5516 GEN_INT (exact_log2 (u)),
5517 NULL, 0, OPTAB_DIRECT);
5518 gcc_assert (sel != NULL);
5520 /* Broadcast the low byte each element into each of its bytes. */
5521 vec = rtvec_alloc (w);
5522 for (i = 0; i < w; ++i)
5524 int this_e = i / u * u;
5525 if (BYTES_BIG_ENDIAN)
5526 this_e += u - 1;
5527 RTVEC_ELT (vec, i) = GEN_INT (this_e);
5529 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
5530 sel = gen_lowpart (qimode, sel);
5531 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
5532 gcc_assert (sel != NULL);
5534 /* Add the byte offset to each byte element. */
5535 /* Note that the definition of the indicies here is memory ordering,
5536 so there should be no difference between big and little endian. */
5537 vec = rtvec_alloc (w);
5538 for (i = 0; i < w; ++i)
5539 RTVEC_ELT (vec, i) = GEN_INT (i % u);
5540 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
5541 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
5542 sel, 0, OPTAB_DIRECT);
5543 gcc_assert (sel_qi != NULL);
5546 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
5547 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
5548 gen_lowpart (qimode, v1), sel_qi);
5549 if (tmp)
5550 tmp = gen_lowpart (mode, tmp);
5551 return tmp;
5554 /* Generate insns for a VEC_COND_EXPR with mask, given its TYPE and its
5555 three operands. */
5558 expand_vec_cond_mask_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
5559 rtx target)
5561 struct expand_operand ops[4];
5562 machine_mode mode = TYPE_MODE (vec_cond_type);
5563 machine_mode mask_mode = TYPE_MODE (TREE_TYPE (op0));
5564 enum insn_code icode = get_vcond_mask_icode (mode, mask_mode);
5565 rtx mask, rtx_op1, rtx_op2;
5567 if (icode == CODE_FOR_nothing)
5568 return 0;
5570 mask = expand_normal (op0);
5571 rtx_op1 = expand_normal (op1);
5572 rtx_op2 = expand_normal (op2);
5574 mask = force_reg (mask_mode, mask);
5575 rtx_op1 = force_reg (GET_MODE (rtx_op1), rtx_op1);
5577 create_output_operand (&ops[0], target, mode);
5578 create_input_operand (&ops[1], rtx_op1, mode);
5579 create_input_operand (&ops[2], rtx_op2, mode);
5580 create_input_operand (&ops[3], mask, mask_mode);
5581 expand_insn (icode, 4, ops);
5583 return ops[0].value;
5586 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
5587 three operands. */
5590 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
5591 rtx target)
5593 struct expand_operand ops[6];
5594 enum insn_code icode;
5595 rtx comparison, rtx_op1, rtx_op2;
5596 machine_mode mode = TYPE_MODE (vec_cond_type);
5597 machine_mode cmp_op_mode;
5598 bool unsignedp;
5599 tree op0a, op0b;
5600 enum tree_code tcode;
5602 if (COMPARISON_CLASS_P (op0))
5604 op0a = TREE_OPERAND (op0, 0);
5605 op0b = TREE_OPERAND (op0, 1);
5606 tcode = TREE_CODE (op0);
5608 else
5610 gcc_assert (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (op0)));
5611 if (get_vcond_mask_icode (mode, TYPE_MODE (TREE_TYPE (op0)))
5612 != CODE_FOR_nothing)
5613 return expand_vec_cond_mask_expr (vec_cond_type, op0, op1,
5614 op2, target);
5615 /* Fake op0 < 0. */
5616 else
5618 gcc_assert (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (op0)))
5619 == MODE_VECTOR_INT);
5620 op0a = op0;
5621 op0b = build_zero_cst (TREE_TYPE (op0));
5622 tcode = LT_EXPR;
5625 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
5626 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
5629 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
5630 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
5632 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
5633 if (icode == CODE_FOR_nothing)
5635 if (tcode == EQ_EXPR || tcode == NE_EXPR)
5636 icode = get_vcond_eq_icode (mode, cmp_op_mode);
5637 if (icode == CODE_FOR_nothing)
5638 return 0;
5641 comparison = vector_compare_rtx (VOIDmode, tcode, op0a, op0b, unsignedp,
5642 icode, 4);
5643 rtx_op1 = expand_normal (op1);
5644 rtx_op2 = expand_normal (op2);
5646 create_output_operand (&ops[0], target, mode);
5647 create_input_operand (&ops[1], rtx_op1, mode);
5648 create_input_operand (&ops[2], rtx_op2, mode);
5649 create_fixed_operand (&ops[3], comparison);
5650 create_fixed_operand (&ops[4], XEXP (comparison, 0));
5651 create_fixed_operand (&ops[5], XEXP (comparison, 1));
5652 expand_insn (icode, 6, ops);
5653 return ops[0].value;
5656 /* Generate insns for a vector comparison into a mask. */
5659 expand_vec_cmp_expr (tree type, tree exp, rtx target)
5661 struct expand_operand ops[4];
5662 enum insn_code icode;
5663 rtx comparison;
5664 machine_mode mask_mode = TYPE_MODE (type);
5665 machine_mode vmode;
5666 bool unsignedp;
5667 tree op0a, op0b;
5668 enum tree_code tcode;
5670 op0a = TREE_OPERAND (exp, 0);
5671 op0b = TREE_OPERAND (exp, 1);
5672 tcode = TREE_CODE (exp);
5674 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
5675 vmode = TYPE_MODE (TREE_TYPE (op0a));
5677 icode = get_vec_cmp_icode (vmode, mask_mode, unsignedp);
5678 if (icode == CODE_FOR_nothing)
5680 if (tcode == EQ_EXPR || tcode == NE_EXPR)
5681 icode = get_vec_cmp_eq_icode (vmode, mask_mode);
5682 if (icode == CODE_FOR_nothing)
5683 return 0;
5686 comparison = vector_compare_rtx (mask_mode, tcode, op0a, op0b,
5687 unsignedp, icode, 2);
5688 create_output_operand (&ops[0], target, mask_mode);
5689 create_fixed_operand (&ops[1], comparison);
5690 create_fixed_operand (&ops[2], XEXP (comparison, 0));
5691 create_fixed_operand (&ops[3], XEXP (comparison, 1));
5692 expand_insn (icode, 4, ops);
5693 return ops[0].value;
5696 /* Expand a highpart multiply. */
5699 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
5700 rtx target, bool uns_p)
5702 struct expand_operand eops[3];
5703 enum insn_code icode;
5704 int method, i, nunits;
5705 machine_mode wmode;
5706 rtx m1, m2, perm;
5707 optab tab1, tab2;
5708 rtvec v;
5710 method = can_mult_highpart_p (mode, uns_p);
5711 switch (method)
5713 case 0:
5714 return NULL_RTX;
5715 case 1:
5716 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
5717 return expand_binop (mode, tab1, op0, op1, target, uns_p,
5718 OPTAB_LIB_WIDEN);
5719 case 2:
5720 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
5721 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
5722 break;
5723 case 3:
5724 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
5725 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
5726 if (BYTES_BIG_ENDIAN)
5727 std::swap (tab1, tab2);
5728 break;
5729 default:
5730 gcc_unreachable ();
5733 icode = optab_handler (tab1, mode);
5734 nunits = GET_MODE_NUNITS (mode);
5735 wmode = insn_data[icode].operand[0].mode;
5736 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
5737 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
5739 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
5740 create_input_operand (&eops[1], op0, mode);
5741 create_input_operand (&eops[2], op1, mode);
5742 expand_insn (icode, 3, eops);
5743 m1 = gen_lowpart (mode, eops[0].value);
5745 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
5746 create_input_operand (&eops[1], op0, mode);
5747 create_input_operand (&eops[2], op1, mode);
5748 expand_insn (optab_handler (tab2, mode), 3, eops);
5749 m2 = gen_lowpart (mode, eops[0].value);
5751 v = rtvec_alloc (nunits);
5752 if (method == 2)
5754 for (i = 0; i < nunits; ++i)
5755 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
5756 + ((i & 1) ? nunits : 0));
5758 else
5760 for (i = 0; i < nunits; ++i)
5761 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
5763 perm = gen_rtx_CONST_VECTOR (mode, v);
5765 return expand_vec_perm (mode, m1, m2, perm, target);
5768 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
5769 pattern. */
5771 static void
5772 find_cc_set (rtx x, const_rtx pat, void *data)
5774 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
5775 && GET_CODE (pat) == SET)
5777 rtx *p_cc_reg = (rtx *) data;
5778 gcc_assert (!*p_cc_reg);
5779 *p_cc_reg = x;
5783 /* This is a helper function for the other atomic operations. This function
5784 emits a loop that contains SEQ that iterates until a compare-and-swap
5785 operation at the end succeeds. MEM is the memory to be modified. SEQ is
5786 a set of instructions that takes a value from OLD_REG as an input and
5787 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
5788 set to the current contents of MEM. After SEQ, a compare-and-swap will
5789 attempt to update MEM with NEW_REG. The function returns true when the
5790 loop was generated successfully. */
5792 static bool
5793 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
5795 machine_mode mode = GET_MODE (mem);
5796 rtx_code_label *label;
5797 rtx cmp_reg, success, oldval;
5799 /* The loop we want to generate looks like
5801 cmp_reg = mem;
5802 label:
5803 old_reg = cmp_reg;
5804 seq;
5805 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
5806 if (success)
5807 goto label;
5809 Note that we only do the plain load from memory once. Subsequent
5810 iterations use the value loaded by the compare-and-swap pattern. */
5812 label = gen_label_rtx ();
5813 cmp_reg = gen_reg_rtx (mode);
5815 emit_move_insn (cmp_reg, mem);
5816 emit_label (label);
5817 emit_move_insn (old_reg, cmp_reg);
5818 if (seq)
5819 emit_insn (seq);
5821 success = NULL_RTX;
5822 oldval = cmp_reg;
5823 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
5824 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
5825 MEMMODEL_RELAXED))
5826 return false;
5828 if (oldval != cmp_reg)
5829 emit_move_insn (cmp_reg, oldval);
5831 /* Mark this jump predicted not taken. */
5832 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
5833 GET_MODE (success), 1, label,
5834 profile_probability::guessed_never ());
5835 return true;
5839 /* This function tries to emit an atomic_exchange intruction. VAL is written
5840 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
5841 using TARGET if possible. */
5843 static rtx
5844 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
5846 machine_mode mode = GET_MODE (mem);
5847 enum insn_code icode;
5849 /* If the target supports the exchange directly, great. */
5850 icode = direct_optab_handler (atomic_exchange_optab, mode);
5851 if (icode != CODE_FOR_nothing)
5853 struct expand_operand ops[4];
5855 create_output_operand (&ops[0], target, mode);
5856 create_fixed_operand (&ops[1], mem);
5857 create_input_operand (&ops[2], val, mode);
5858 create_integer_operand (&ops[3], model);
5859 if (maybe_expand_insn (icode, 4, ops))
5860 return ops[0].value;
5863 return NULL_RTX;
5866 /* This function tries to implement an atomic exchange operation using
5867 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
5868 The previous contents of *MEM are returned, using TARGET if possible.
5869 Since this instructionn is an acquire barrier only, stronger memory
5870 models may require additional barriers to be emitted. */
5872 static rtx
5873 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
5874 enum memmodel model)
5876 machine_mode mode = GET_MODE (mem);
5877 enum insn_code icode;
5878 rtx_insn *last_insn = get_last_insn ();
5880 icode = optab_handler (sync_lock_test_and_set_optab, mode);
5882 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
5883 exists, and the memory model is stronger than acquire, add a release
5884 barrier before the instruction. */
5886 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
5887 expand_mem_thread_fence (model);
5889 if (icode != CODE_FOR_nothing)
5891 struct expand_operand ops[3];
5892 create_output_operand (&ops[0], target, mode);
5893 create_fixed_operand (&ops[1], mem);
5894 create_input_operand (&ops[2], val, mode);
5895 if (maybe_expand_insn (icode, 3, ops))
5896 return ops[0].value;
5899 /* If an external test-and-set libcall is provided, use that instead of
5900 any external compare-and-swap that we might get from the compare-and-
5901 swap-loop expansion later. */
5902 if (!can_compare_and_swap_p (mode, false))
5904 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
5905 if (libfunc != NULL)
5907 rtx addr;
5909 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
5910 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
5911 mode, 2, addr, ptr_mode,
5912 val, mode);
5916 /* If the test_and_set can't be emitted, eliminate any barrier that might
5917 have been emitted. */
5918 delete_insns_since (last_insn);
5919 return NULL_RTX;
5922 /* This function tries to implement an atomic exchange operation using a
5923 compare_and_swap loop. VAL is written to *MEM. The previous contents of
5924 *MEM are returned, using TARGET if possible. No memory model is required
5925 since a compare_and_swap loop is seq-cst. */
5927 static rtx
5928 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
5930 machine_mode mode = GET_MODE (mem);
5932 if (can_compare_and_swap_p (mode, true))
5934 if (!target || !register_operand (target, mode))
5935 target = gen_reg_rtx (mode);
5936 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
5937 return target;
5940 return NULL_RTX;
5943 /* This function tries to implement an atomic test-and-set operation
5944 using the atomic_test_and_set instruction pattern. A boolean value
5945 is returned from the operation, using TARGET if possible. */
5947 static rtx
5948 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
5950 machine_mode pat_bool_mode;
5951 struct expand_operand ops[3];
5953 if (!targetm.have_atomic_test_and_set ())
5954 return NULL_RTX;
5956 /* While we always get QImode from __atomic_test_and_set, we get
5957 other memory modes from __sync_lock_test_and_set. Note that we
5958 use no endian adjustment here. This matches the 4.6 behavior
5959 in the Sparc backend. */
5960 enum insn_code icode = targetm.code_for_atomic_test_and_set;
5961 gcc_checking_assert (insn_data[icode].operand[1].mode == QImode);
5962 if (GET_MODE (mem) != QImode)
5963 mem = adjust_address_nv (mem, QImode, 0);
5965 pat_bool_mode = insn_data[icode].operand[0].mode;
5966 create_output_operand (&ops[0], target, pat_bool_mode);
5967 create_fixed_operand (&ops[1], mem);
5968 create_integer_operand (&ops[2], model);
5970 if (maybe_expand_insn (icode, 3, ops))
5971 return ops[0].value;
5972 return NULL_RTX;
5975 /* This function expands the legacy _sync_lock test_and_set operation which is
5976 generally an atomic exchange. Some limited targets only allow the
5977 constant 1 to be stored. This is an ACQUIRE operation.
5979 TARGET is an optional place to stick the return value.
5980 MEM is where VAL is stored. */
5983 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
5985 rtx ret;
5987 /* Try an atomic_exchange first. */
5988 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
5989 if (ret)
5990 return ret;
5992 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
5993 MEMMODEL_SYNC_ACQUIRE);
5994 if (ret)
5995 return ret;
5997 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
5998 if (ret)
5999 return ret;
6001 /* If there are no other options, try atomic_test_and_set if the value
6002 being stored is 1. */
6003 if (val == const1_rtx)
6004 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
6006 return ret;
6009 /* This function expands the atomic test_and_set operation:
6010 atomically store a boolean TRUE into MEM and return the previous value.
6012 MEMMODEL is the memory model variant to use.
6013 TARGET is an optional place to stick the return value. */
6016 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6018 machine_mode mode = GET_MODE (mem);
6019 rtx ret, trueval, subtarget;
6021 ret = maybe_emit_atomic_test_and_set (target, mem, model);
6022 if (ret)
6023 return ret;
6025 /* Be binary compatible with non-default settings of trueval, and different
6026 cpu revisions. E.g. one revision may have atomic-test-and-set, but
6027 another only has atomic-exchange. */
6028 if (targetm.atomic_test_and_set_trueval == 1)
6030 trueval = const1_rtx;
6031 subtarget = target ? target : gen_reg_rtx (mode);
6033 else
6035 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
6036 subtarget = gen_reg_rtx (mode);
6039 /* Try the atomic-exchange optab... */
6040 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
6042 /* ... then an atomic-compare-and-swap loop ... */
6043 if (!ret)
6044 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
6046 /* ... before trying the vaguely defined legacy lock_test_and_set. */
6047 if (!ret)
6048 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
6050 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
6051 things with the value 1. Thus we try again without trueval. */
6052 if (!ret && targetm.atomic_test_and_set_trueval != 1)
6053 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
6055 /* Failing all else, assume a single threaded environment and simply
6056 perform the operation. */
6057 if (!ret)
6059 /* If the result is ignored skip the move to target. */
6060 if (subtarget != const0_rtx)
6061 emit_move_insn (subtarget, mem);
6063 emit_move_insn (mem, trueval);
6064 ret = subtarget;
6067 /* Recall that have to return a boolean value; rectify if trueval
6068 is not exactly one. */
6069 if (targetm.atomic_test_and_set_trueval != 1)
6070 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
6072 return ret;
6075 /* This function expands the atomic exchange operation:
6076 atomically store VAL in MEM and return the previous value in MEM.
6078 MEMMODEL is the memory model variant to use.
6079 TARGET is an optional place to stick the return value. */
6082 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6084 machine_mode mode = GET_MODE (mem);
6085 rtx ret;
6087 /* If loads are not atomic for the required size and we are not called to
6088 provide a __sync builtin, do not do anything so that we stay consistent
6089 with atomic loads of the same size. */
6090 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6091 return NULL_RTX;
6093 ret = maybe_emit_atomic_exchange (target, mem, val, model);
6095 /* Next try a compare-and-swap loop for the exchange. */
6096 if (!ret)
6097 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6099 return ret;
6102 /* This function expands the atomic compare exchange operation:
6104 *PTARGET_BOOL is an optional place to store the boolean success/failure.
6105 *PTARGET_OVAL is an optional place to store the old value from memory.
6106 Both target parameters may be NULL or const0_rtx to indicate that we do
6107 not care about that return value. Both target parameters are updated on
6108 success to the actual location of the corresponding result.
6110 MEMMODEL is the memory model variant to use.
6112 The return value of the function is true for success. */
6114 bool
6115 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
6116 rtx mem, rtx expected, rtx desired,
6117 bool is_weak, enum memmodel succ_model,
6118 enum memmodel fail_model)
6120 machine_mode mode = GET_MODE (mem);
6121 struct expand_operand ops[8];
6122 enum insn_code icode;
6123 rtx target_oval, target_bool = NULL_RTX;
6124 rtx libfunc;
6126 /* If loads are not atomic for the required size and we are not called to
6127 provide a __sync builtin, do not do anything so that we stay consistent
6128 with atomic loads of the same size. */
6129 if (!can_atomic_load_p (mode) && !is_mm_sync (succ_model))
6130 return false;
6132 /* Load expected into a register for the compare and swap. */
6133 if (MEM_P (expected))
6134 expected = copy_to_reg (expected);
6136 /* Make sure we always have some place to put the return oldval.
6137 Further, make sure that place is distinct from the input expected,
6138 just in case we need that path down below. */
6139 if (ptarget_oval && *ptarget_oval == const0_rtx)
6140 ptarget_oval = NULL;
6142 if (ptarget_oval == NULL
6143 || (target_oval = *ptarget_oval) == NULL
6144 || reg_overlap_mentioned_p (expected, target_oval))
6145 target_oval = gen_reg_rtx (mode);
6147 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
6148 if (icode != CODE_FOR_nothing)
6150 machine_mode bool_mode = insn_data[icode].operand[0].mode;
6152 if (ptarget_bool && *ptarget_bool == const0_rtx)
6153 ptarget_bool = NULL;
6155 /* Make sure we always have a place for the bool operand. */
6156 if (ptarget_bool == NULL
6157 || (target_bool = *ptarget_bool) == NULL
6158 || GET_MODE (target_bool) != bool_mode)
6159 target_bool = gen_reg_rtx (bool_mode);
6161 /* Emit the compare_and_swap. */
6162 create_output_operand (&ops[0], target_bool, bool_mode);
6163 create_output_operand (&ops[1], target_oval, mode);
6164 create_fixed_operand (&ops[2], mem);
6165 create_input_operand (&ops[3], expected, mode);
6166 create_input_operand (&ops[4], desired, mode);
6167 create_integer_operand (&ops[5], is_weak);
6168 create_integer_operand (&ops[6], succ_model);
6169 create_integer_operand (&ops[7], fail_model);
6170 if (maybe_expand_insn (icode, 8, ops))
6172 /* Return success/failure. */
6173 target_bool = ops[0].value;
6174 target_oval = ops[1].value;
6175 goto success;
6179 /* Otherwise fall back to the original __sync_val_compare_and_swap
6180 which is always seq-cst. */
6181 icode = optab_handler (sync_compare_and_swap_optab, mode);
6182 if (icode != CODE_FOR_nothing)
6184 rtx cc_reg;
6186 create_output_operand (&ops[0], target_oval, mode);
6187 create_fixed_operand (&ops[1], mem);
6188 create_input_operand (&ops[2], expected, mode);
6189 create_input_operand (&ops[3], desired, mode);
6190 if (!maybe_expand_insn (icode, 4, ops))
6191 return false;
6193 target_oval = ops[0].value;
6195 /* If the caller isn't interested in the boolean return value,
6196 skip the computation of it. */
6197 if (ptarget_bool == NULL)
6198 goto success;
6200 /* Otherwise, work out if the compare-and-swap succeeded. */
6201 cc_reg = NULL_RTX;
6202 if (have_insn_for (COMPARE, CCmode))
6203 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6204 if (cc_reg)
6206 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
6207 const0_rtx, VOIDmode, 0, 1);
6208 goto success;
6210 goto success_bool_from_val;
6213 /* Also check for library support for __sync_val_compare_and_swap. */
6214 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
6215 if (libfunc != NULL)
6217 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6218 rtx target = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6219 mode, 3, addr, ptr_mode,
6220 expected, mode, desired, mode);
6221 emit_move_insn (target_oval, target);
6223 /* Compute the boolean return value only if requested. */
6224 if (ptarget_bool)
6225 goto success_bool_from_val;
6226 else
6227 goto success;
6230 /* Failure. */
6231 return false;
6233 success_bool_from_val:
6234 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
6235 expected, VOIDmode, 1, 1);
6236 success:
6237 /* Make sure that the oval output winds up where the caller asked. */
6238 if (ptarget_oval)
6239 *ptarget_oval = target_oval;
6240 if (ptarget_bool)
6241 *ptarget_bool = target_bool;
6242 return true;
6245 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
6247 static void
6248 expand_asm_memory_barrier (void)
6250 rtx asm_op, clob;
6252 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
6253 rtvec_alloc (0), rtvec_alloc (0),
6254 rtvec_alloc (0), UNKNOWN_LOCATION);
6255 MEM_VOLATILE_P (asm_op) = 1;
6257 clob = gen_rtx_SCRATCH (VOIDmode);
6258 clob = gen_rtx_MEM (BLKmode, clob);
6259 clob = gen_rtx_CLOBBER (VOIDmode, clob);
6261 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
6264 /* This routine will either emit the mem_thread_fence pattern or issue a
6265 sync_synchronize to generate a fence for memory model MEMMODEL. */
6267 void
6268 expand_mem_thread_fence (enum memmodel model)
6270 if (is_mm_relaxed (model))
6271 return;
6272 if (targetm.have_mem_thread_fence ())
6274 emit_insn (targetm.gen_mem_thread_fence (GEN_INT (model)));
6275 expand_asm_memory_barrier ();
6277 else if (targetm.have_memory_barrier ())
6278 emit_insn (targetm.gen_memory_barrier ());
6279 else if (synchronize_libfunc != NULL_RTX)
6280 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
6281 else
6282 expand_asm_memory_barrier ();
6285 /* This routine will either emit the mem_signal_fence pattern or issue a
6286 sync_synchronize to generate a fence for memory model MEMMODEL. */
6288 void
6289 expand_mem_signal_fence (enum memmodel model)
6291 if (targetm.have_mem_signal_fence ())
6292 emit_insn (targetm.gen_mem_signal_fence (GEN_INT (model)));
6293 else if (!is_mm_relaxed (model))
6295 /* By default targets are coherent between a thread and the signal
6296 handler running on the same thread. Thus this really becomes a
6297 compiler barrier, in that stores must not be sunk past
6298 (or raised above) a given point. */
6299 expand_asm_memory_barrier ();
6303 /* This function expands the atomic load operation:
6304 return the atomically loaded value in MEM.
6306 MEMMODEL is the memory model variant to use.
6307 TARGET is an option place to stick the return value. */
6310 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
6312 machine_mode mode = GET_MODE (mem);
6313 enum insn_code icode;
6315 /* If the target supports the load directly, great. */
6316 icode = direct_optab_handler (atomic_load_optab, mode);
6317 if (icode != CODE_FOR_nothing)
6319 struct expand_operand ops[3];
6321 create_output_operand (&ops[0], target, mode);
6322 create_fixed_operand (&ops[1], mem);
6323 create_integer_operand (&ops[2], model);
6324 if (maybe_expand_insn (icode, 3, ops))
6325 return ops[0].value;
6328 /* If the size of the object is greater than word size on this target,
6329 then we assume that a load will not be atomic. We could try to
6330 emulate a load with a compare-and-swap operation, but the store that
6331 doing this could result in would be incorrect if this is a volatile
6332 atomic load or targetting read-only-mapped memory. */
6333 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
6334 /* If there is no atomic load, leave the library call. */
6335 return NULL_RTX;
6337 /* Otherwise assume loads are atomic, and emit the proper barriers. */
6338 if (!target || target == const0_rtx)
6339 target = gen_reg_rtx (mode);
6341 /* For SEQ_CST, emit a barrier before the load. */
6342 if (is_mm_seq_cst (model))
6343 expand_mem_thread_fence (model);
6345 emit_move_insn (target, mem);
6347 /* Emit the appropriate barrier after the load. */
6348 expand_mem_thread_fence (model);
6350 return target;
6353 /* This function expands the atomic store operation:
6354 Atomically store VAL in MEM.
6355 MEMMODEL is the memory model variant to use.
6356 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
6357 function returns const0_rtx if a pattern was emitted. */
6360 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
6362 machine_mode mode = GET_MODE (mem);
6363 enum insn_code icode;
6364 struct expand_operand ops[3];
6366 /* If the target supports the store directly, great. */
6367 icode = direct_optab_handler (atomic_store_optab, mode);
6368 if (icode != CODE_FOR_nothing)
6370 create_fixed_operand (&ops[0], mem);
6371 create_input_operand (&ops[1], val, mode);
6372 create_integer_operand (&ops[2], model);
6373 if (maybe_expand_insn (icode, 3, ops))
6374 return const0_rtx;
6377 /* If using __sync_lock_release is a viable alternative, try it.
6378 Note that this will not be set to true if we are expanding a generic
6379 __atomic_store_n. */
6380 if (use_release)
6382 icode = direct_optab_handler (sync_lock_release_optab, mode);
6383 if (icode != CODE_FOR_nothing)
6385 create_fixed_operand (&ops[0], mem);
6386 create_input_operand (&ops[1], const0_rtx, mode);
6387 if (maybe_expand_insn (icode, 2, ops))
6389 /* lock_release is only a release barrier. */
6390 if (is_mm_seq_cst (model))
6391 expand_mem_thread_fence (model);
6392 return const0_rtx;
6397 /* If the size of the object is greater than word size on this target,
6398 a default store will not be atomic. */
6399 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
6401 /* If loads are atomic or we are called to provide a __sync builtin,
6402 we can try a atomic_exchange and throw away the result. Otherwise,
6403 don't do anything so that we do not create an inconsistency between
6404 loads and stores. */
6405 if (can_atomic_load_p (mode) || is_mm_sync (model))
6407 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
6408 if (!target)
6409 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem,
6410 val);
6411 if (target)
6412 return const0_rtx;
6414 return NULL_RTX;
6417 /* Otherwise assume stores are atomic, and emit the proper barriers. */
6418 expand_mem_thread_fence (model);
6420 emit_move_insn (mem, val);
6422 /* For SEQ_CST, also emit a barrier after the store. */
6423 if (is_mm_seq_cst (model))
6424 expand_mem_thread_fence (model);
6426 return const0_rtx;
6430 /* Structure containing the pointers and values required to process the
6431 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
6433 struct atomic_op_functions
6435 direct_optab mem_fetch_before;
6436 direct_optab mem_fetch_after;
6437 direct_optab mem_no_result;
6438 optab fetch_before;
6439 optab fetch_after;
6440 direct_optab no_result;
6441 enum rtx_code reverse_code;
6445 /* Fill in structure pointed to by OP with the various optab entries for an
6446 operation of type CODE. */
6448 static void
6449 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
6451 gcc_assert (op!= NULL);
6453 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
6454 in the source code during compilation, and the optab entries are not
6455 computable until runtime. Fill in the values at runtime. */
6456 switch (code)
6458 case PLUS:
6459 op->mem_fetch_before = atomic_fetch_add_optab;
6460 op->mem_fetch_after = atomic_add_fetch_optab;
6461 op->mem_no_result = atomic_add_optab;
6462 op->fetch_before = sync_old_add_optab;
6463 op->fetch_after = sync_new_add_optab;
6464 op->no_result = sync_add_optab;
6465 op->reverse_code = MINUS;
6466 break;
6467 case MINUS:
6468 op->mem_fetch_before = atomic_fetch_sub_optab;
6469 op->mem_fetch_after = atomic_sub_fetch_optab;
6470 op->mem_no_result = atomic_sub_optab;
6471 op->fetch_before = sync_old_sub_optab;
6472 op->fetch_after = sync_new_sub_optab;
6473 op->no_result = sync_sub_optab;
6474 op->reverse_code = PLUS;
6475 break;
6476 case XOR:
6477 op->mem_fetch_before = atomic_fetch_xor_optab;
6478 op->mem_fetch_after = atomic_xor_fetch_optab;
6479 op->mem_no_result = atomic_xor_optab;
6480 op->fetch_before = sync_old_xor_optab;
6481 op->fetch_after = sync_new_xor_optab;
6482 op->no_result = sync_xor_optab;
6483 op->reverse_code = XOR;
6484 break;
6485 case AND:
6486 op->mem_fetch_before = atomic_fetch_and_optab;
6487 op->mem_fetch_after = atomic_and_fetch_optab;
6488 op->mem_no_result = atomic_and_optab;
6489 op->fetch_before = sync_old_and_optab;
6490 op->fetch_after = sync_new_and_optab;
6491 op->no_result = sync_and_optab;
6492 op->reverse_code = UNKNOWN;
6493 break;
6494 case IOR:
6495 op->mem_fetch_before = atomic_fetch_or_optab;
6496 op->mem_fetch_after = atomic_or_fetch_optab;
6497 op->mem_no_result = atomic_or_optab;
6498 op->fetch_before = sync_old_ior_optab;
6499 op->fetch_after = sync_new_ior_optab;
6500 op->no_result = sync_ior_optab;
6501 op->reverse_code = UNKNOWN;
6502 break;
6503 case NOT:
6504 op->mem_fetch_before = atomic_fetch_nand_optab;
6505 op->mem_fetch_after = atomic_nand_fetch_optab;
6506 op->mem_no_result = atomic_nand_optab;
6507 op->fetch_before = sync_old_nand_optab;
6508 op->fetch_after = sync_new_nand_optab;
6509 op->no_result = sync_nand_optab;
6510 op->reverse_code = UNKNOWN;
6511 break;
6512 default:
6513 gcc_unreachable ();
6517 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
6518 using memory order MODEL. If AFTER is true the operation needs to return
6519 the value of *MEM after the operation, otherwise the previous value.
6520 TARGET is an optional place to place the result. The result is unused if
6521 it is const0_rtx.
6522 Return the result if there is a better sequence, otherwise NULL_RTX. */
6524 static rtx
6525 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
6526 enum memmodel model, bool after)
6528 /* If the value is prefetched, or not used, it may be possible to replace
6529 the sequence with a native exchange operation. */
6530 if (!after || target == const0_rtx)
6532 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
6533 if (code == AND && val == const0_rtx)
6535 if (target == const0_rtx)
6536 target = gen_reg_rtx (GET_MODE (mem));
6537 return maybe_emit_atomic_exchange (target, mem, val, model);
6540 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
6541 if (code == IOR && val == constm1_rtx)
6543 if (target == const0_rtx)
6544 target = gen_reg_rtx (GET_MODE (mem));
6545 return maybe_emit_atomic_exchange (target, mem, val, model);
6549 return NULL_RTX;
6552 /* Try to emit an instruction for a specific operation varaition.
6553 OPTAB contains the OP functions.
6554 TARGET is an optional place to return the result. const0_rtx means unused.
6555 MEM is the memory location to operate on.
6556 VAL is the value to use in the operation.
6557 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
6558 MODEL is the memory model, if used.
6559 AFTER is true if the returned result is the value after the operation. */
6561 static rtx
6562 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
6563 rtx val, bool use_memmodel, enum memmodel model, bool after)
6565 machine_mode mode = GET_MODE (mem);
6566 struct expand_operand ops[4];
6567 enum insn_code icode;
6568 int op_counter = 0;
6569 int num_ops;
6571 /* Check to see if there is a result returned. */
6572 if (target == const0_rtx)
6574 if (use_memmodel)
6576 icode = direct_optab_handler (optab->mem_no_result, mode);
6577 create_integer_operand (&ops[2], model);
6578 num_ops = 3;
6580 else
6582 icode = direct_optab_handler (optab->no_result, mode);
6583 num_ops = 2;
6586 /* Otherwise, we need to generate a result. */
6587 else
6589 if (use_memmodel)
6591 icode = direct_optab_handler (after ? optab->mem_fetch_after
6592 : optab->mem_fetch_before, mode);
6593 create_integer_operand (&ops[3], model);
6594 num_ops = 4;
6596 else
6598 icode = optab_handler (after ? optab->fetch_after
6599 : optab->fetch_before, mode);
6600 num_ops = 3;
6602 create_output_operand (&ops[op_counter++], target, mode);
6604 if (icode == CODE_FOR_nothing)
6605 return NULL_RTX;
6607 create_fixed_operand (&ops[op_counter++], mem);
6608 /* VAL may have been promoted to a wider mode. Shrink it if so. */
6609 create_convert_operand_to (&ops[op_counter++], val, mode, true);
6611 if (maybe_expand_insn (icode, num_ops, ops))
6612 return (target == const0_rtx ? const0_rtx : ops[0].value);
6614 return NULL_RTX;
6618 /* This function expands an atomic fetch_OP or OP_fetch operation:
6619 TARGET is an option place to stick the return value. const0_rtx indicates
6620 the result is unused.
6621 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6622 CODE is the operation being performed (OP)
6623 MEMMODEL is the memory model variant to use.
6624 AFTER is true to return the result of the operation (OP_fetch).
6625 AFTER is false to return the value before the operation (fetch_OP).
6627 This function will *only* generate instructions if there is a direct
6628 optab. No compare and swap loops or libcalls will be generated. */
6630 static rtx
6631 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
6632 enum rtx_code code, enum memmodel model,
6633 bool after)
6635 machine_mode mode = GET_MODE (mem);
6636 struct atomic_op_functions optab;
6637 rtx result;
6638 bool unused_result = (target == const0_rtx);
6640 get_atomic_op_for_code (&optab, code);
6642 /* Check to see if there are any better instructions. */
6643 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
6644 if (result)
6645 return result;
6647 /* Check for the case where the result isn't used and try those patterns. */
6648 if (unused_result)
6650 /* Try the memory model variant first. */
6651 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
6652 if (result)
6653 return result;
6655 /* Next try the old style withuot a memory model. */
6656 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
6657 if (result)
6658 return result;
6660 /* There is no no-result pattern, so try patterns with a result. */
6661 target = NULL_RTX;
6664 /* Try the __atomic version. */
6665 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
6666 if (result)
6667 return result;
6669 /* Try the older __sync version. */
6670 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
6671 if (result)
6672 return result;
6674 /* If the fetch value can be calculated from the other variation of fetch,
6675 try that operation. */
6676 if (after || unused_result || optab.reverse_code != UNKNOWN)
6678 /* Try the __atomic version, then the older __sync version. */
6679 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
6680 if (!result)
6681 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
6683 if (result)
6685 /* If the result isn't used, no need to do compensation code. */
6686 if (unused_result)
6687 return result;
6689 /* Issue compensation code. Fetch_after == fetch_before OP val.
6690 Fetch_before == after REVERSE_OP val. */
6691 if (!after)
6692 code = optab.reverse_code;
6693 if (code == NOT)
6695 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
6696 true, OPTAB_LIB_WIDEN);
6697 result = expand_simple_unop (mode, NOT, result, target, true);
6699 else
6700 result = expand_simple_binop (mode, code, result, val, target,
6701 true, OPTAB_LIB_WIDEN);
6702 return result;
6706 /* No direct opcode can be generated. */
6707 return NULL_RTX;
6712 /* This function expands an atomic fetch_OP or OP_fetch operation:
6713 TARGET is an option place to stick the return value. const0_rtx indicates
6714 the result is unused.
6715 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6716 CODE is the operation being performed (OP)
6717 MEMMODEL is the memory model variant to use.
6718 AFTER is true to return the result of the operation (OP_fetch).
6719 AFTER is false to return the value before the operation (fetch_OP). */
6721 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
6722 enum memmodel model, bool after)
6724 machine_mode mode = GET_MODE (mem);
6725 rtx result;
6726 bool unused_result = (target == const0_rtx);
6728 /* If loads are not atomic for the required size and we are not called to
6729 provide a __sync builtin, do not do anything so that we stay consistent
6730 with atomic loads of the same size. */
6731 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6732 return NULL_RTX;
6734 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
6735 after);
6737 if (result)
6738 return result;
6740 /* Add/sub can be implemented by doing the reverse operation with -(val). */
6741 if (code == PLUS || code == MINUS)
6743 rtx tmp;
6744 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
6746 start_sequence ();
6747 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
6748 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
6749 model, after);
6750 if (result)
6752 /* PLUS worked so emit the insns and return. */
6753 tmp = get_insns ();
6754 end_sequence ();
6755 emit_insn (tmp);
6756 return result;
6759 /* PLUS did not work, so throw away the negation code and continue. */
6760 end_sequence ();
6763 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
6764 if (!can_compare_and_swap_p (mode, false))
6766 rtx libfunc;
6767 bool fixup = false;
6768 enum rtx_code orig_code = code;
6769 struct atomic_op_functions optab;
6771 get_atomic_op_for_code (&optab, code);
6772 libfunc = optab_libfunc (after ? optab.fetch_after
6773 : optab.fetch_before, mode);
6774 if (libfunc == NULL
6775 && (after || unused_result || optab.reverse_code != UNKNOWN))
6777 fixup = true;
6778 if (!after)
6779 code = optab.reverse_code;
6780 libfunc = optab_libfunc (after ? optab.fetch_before
6781 : optab.fetch_after, mode);
6783 if (libfunc != NULL)
6785 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6786 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
6787 2, addr, ptr_mode, val, mode);
6789 if (!unused_result && fixup)
6790 result = expand_simple_binop (mode, code, result, val, target,
6791 true, OPTAB_LIB_WIDEN);
6792 return result;
6795 /* We need the original code for any further attempts. */
6796 code = orig_code;
6799 /* If nothing else has succeeded, default to a compare and swap loop. */
6800 if (can_compare_and_swap_p (mode, true))
6802 rtx_insn *insn;
6803 rtx t0 = gen_reg_rtx (mode), t1;
6805 start_sequence ();
6807 /* If the result is used, get a register for it. */
6808 if (!unused_result)
6810 if (!target || !register_operand (target, mode))
6811 target = gen_reg_rtx (mode);
6812 /* If fetch_before, copy the value now. */
6813 if (!after)
6814 emit_move_insn (target, t0);
6816 else
6817 target = const0_rtx;
6819 t1 = t0;
6820 if (code == NOT)
6822 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
6823 true, OPTAB_LIB_WIDEN);
6824 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
6826 else
6827 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
6828 OPTAB_LIB_WIDEN);
6830 /* For after, copy the value now. */
6831 if (!unused_result && after)
6832 emit_move_insn (target, t1);
6833 insn = get_insns ();
6834 end_sequence ();
6836 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
6837 return target;
6840 return NULL_RTX;
6843 /* Return true if OPERAND is suitable for operand number OPNO of
6844 instruction ICODE. */
6846 bool
6847 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
6849 return (!insn_data[(int) icode].operand[opno].predicate
6850 || (insn_data[(int) icode].operand[opno].predicate
6851 (operand, insn_data[(int) icode].operand[opno].mode)));
6854 /* TARGET is a target of a multiword operation that we are going to
6855 implement as a series of word-mode operations. Return true if
6856 TARGET is suitable for this purpose. */
6858 bool
6859 valid_multiword_target_p (rtx target)
6861 machine_mode mode;
6862 int i;
6864 mode = GET_MODE (target);
6865 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
6866 if (!validate_subreg (word_mode, mode, target, i))
6867 return false;
6868 return true;
6871 /* Like maybe_legitimize_operand, but do not change the code of the
6872 current rtx value. */
6874 static bool
6875 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
6876 struct expand_operand *op)
6878 /* See if the operand matches in its current form. */
6879 if (insn_operand_matches (icode, opno, op->value))
6880 return true;
6882 /* If the operand is a memory whose address has no side effects,
6883 try forcing the address into a non-virtual pseudo register.
6884 The check for side effects is important because copy_to_mode_reg
6885 cannot handle things like auto-modified addresses. */
6886 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
6888 rtx addr, mem;
6890 mem = op->value;
6891 addr = XEXP (mem, 0);
6892 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
6893 && !side_effects_p (addr))
6895 rtx_insn *last;
6896 machine_mode mode;
6898 last = get_last_insn ();
6899 mode = get_address_mode (mem);
6900 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
6901 if (insn_operand_matches (icode, opno, mem))
6903 op->value = mem;
6904 return true;
6906 delete_insns_since (last);
6910 return false;
6913 /* Try to make OP match operand OPNO of instruction ICODE. Return true
6914 on success, storing the new operand value back in OP. */
6916 static bool
6917 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
6918 struct expand_operand *op)
6920 machine_mode mode, imode;
6921 bool old_volatile_ok, result;
6923 mode = op->mode;
6924 switch (op->type)
6926 case EXPAND_FIXED:
6927 old_volatile_ok = volatile_ok;
6928 volatile_ok = true;
6929 result = maybe_legitimize_operand_same_code (icode, opno, op);
6930 volatile_ok = old_volatile_ok;
6931 return result;
6933 case EXPAND_OUTPUT:
6934 gcc_assert (mode != VOIDmode);
6935 if (op->value
6936 && op->value != const0_rtx
6937 && GET_MODE (op->value) == mode
6938 && maybe_legitimize_operand_same_code (icode, opno, op))
6939 return true;
6941 op->value = gen_reg_rtx (mode);
6942 op->target = 0;
6943 break;
6945 case EXPAND_INPUT:
6946 input:
6947 gcc_assert (mode != VOIDmode);
6948 gcc_assert (GET_MODE (op->value) == VOIDmode
6949 || GET_MODE (op->value) == mode);
6950 if (maybe_legitimize_operand_same_code (icode, opno, op))
6951 return true;
6953 op->value = copy_to_mode_reg (mode, op->value);
6954 break;
6956 case EXPAND_CONVERT_TO:
6957 gcc_assert (mode != VOIDmode);
6958 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
6959 goto input;
6961 case EXPAND_CONVERT_FROM:
6962 if (GET_MODE (op->value) != VOIDmode)
6963 mode = GET_MODE (op->value);
6964 else
6965 /* The caller must tell us what mode this value has. */
6966 gcc_assert (mode != VOIDmode);
6968 imode = insn_data[(int) icode].operand[opno].mode;
6969 if (imode != VOIDmode && imode != mode)
6971 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
6972 mode = imode;
6974 goto input;
6976 case EXPAND_ADDRESS:
6977 gcc_assert (mode != VOIDmode);
6978 op->value = convert_memory_address (mode, op->value);
6979 goto input;
6981 case EXPAND_INTEGER:
6982 mode = insn_data[(int) icode].operand[opno].mode;
6983 if (mode != VOIDmode && const_int_operand (op->value, mode))
6984 goto input;
6985 break;
6987 return insn_operand_matches (icode, opno, op->value);
6990 /* Make OP describe an input operand that should have the same value
6991 as VALUE, after any mode conversion that the target might request.
6992 TYPE is the type of VALUE. */
6994 void
6995 create_convert_operand_from_type (struct expand_operand *op,
6996 rtx value, tree type)
6998 create_convert_operand_from (op, value, TYPE_MODE (type),
6999 TYPE_UNSIGNED (type));
7002 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
7003 of instruction ICODE. Return true on success, leaving the new operand
7004 values in the OPS themselves. Emit no code on failure. */
7006 bool
7007 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
7008 unsigned int nops, struct expand_operand *ops)
7010 rtx_insn *last;
7011 unsigned int i;
7013 last = get_last_insn ();
7014 for (i = 0; i < nops; i++)
7015 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
7017 delete_insns_since (last);
7018 return false;
7020 return true;
7023 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
7024 as its operands. Return the instruction pattern on success,
7025 and emit any necessary set-up code. Return null and emit no
7026 code on failure. */
7028 rtx_insn *
7029 maybe_gen_insn (enum insn_code icode, unsigned int nops,
7030 struct expand_operand *ops)
7032 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
7033 if (!maybe_legitimize_operands (icode, 0, nops, ops))
7034 return NULL;
7036 switch (nops)
7038 case 1:
7039 return GEN_FCN (icode) (ops[0].value);
7040 case 2:
7041 return GEN_FCN (icode) (ops[0].value, ops[1].value);
7042 case 3:
7043 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
7044 case 4:
7045 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7046 ops[3].value);
7047 case 5:
7048 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7049 ops[3].value, ops[4].value);
7050 case 6:
7051 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7052 ops[3].value, ops[4].value, ops[5].value);
7053 case 7:
7054 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7055 ops[3].value, ops[4].value, ops[5].value,
7056 ops[6].value);
7057 case 8:
7058 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7059 ops[3].value, ops[4].value, ops[5].value,
7060 ops[6].value, ops[7].value);
7061 case 9:
7062 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7063 ops[3].value, ops[4].value, ops[5].value,
7064 ops[6].value, ops[7].value, ops[8].value);
7066 gcc_unreachable ();
7069 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
7070 as its operands. Return true on success and emit no code on failure. */
7072 bool
7073 maybe_expand_insn (enum insn_code icode, unsigned int nops,
7074 struct expand_operand *ops)
7076 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7077 if (pat)
7079 emit_insn (pat);
7080 return true;
7082 return false;
7085 /* Like maybe_expand_insn, but for jumps. */
7087 bool
7088 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
7089 struct expand_operand *ops)
7091 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7092 if (pat)
7094 emit_jump_insn (pat);
7095 return true;
7097 return false;
7100 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
7101 as its operands. */
7103 void
7104 expand_insn (enum insn_code icode, unsigned int nops,
7105 struct expand_operand *ops)
7107 if (!maybe_expand_insn (icode, nops, ops))
7108 gcc_unreachable ();
7111 /* Like expand_insn, but for jumps. */
7113 void
7114 expand_jump_insn (enum insn_code icode, unsigned int nops,
7115 struct expand_operand *ops)
7117 if (!maybe_expand_jump_insn (icode, nops, ops))
7118 gcc_unreachable ();