[20/77] Replace MODE_INT checks with is_int_mode
[official-gcc.git] / gcc / optabs.c
blob65a098eb90e88a979e4f5c2e6daeadd5afd13753
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 scalar_int_mode int_mode;
1117 rtx libfunc;
1118 rtx temp;
1119 rtx_insn *entry_last = get_last_insn ();
1120 rtx_insn *last;
1122 mclass = GET_MODE_CLASS (mode);
1124 /* If subtracting an integer constant, convert this into an addition of
1125 the negated constant. */
1127 if (binoptab == sub_optab && CONST_INT_P (op1))
1129 op1 = negate_rtx (mode, op1);
1130 binoptab = add_optab;
1132 /* For shifts, constant invalid op1 might be expanded from different
1133 mode than MODE. As those are invalid, force them to a register
1134 to avoid further problems during expansion. */
1135 else if (CONST_INT_P (op1)
1136 && shift_optab_p (binoptab)
1137 && UINTVAL (op1) >= GET_MODE_BITSIZE (GET_MODE_INNER (mode)))
1139 op1 = gen_int_mode (INTVAL (op1), GET_MODE_INNER (mode));
1140 op1 = force_reg (GET_MODE_INNER (mode), op1);
1143 /* Record where to delete back to if we backtrack. */
1144 last = get_last_insn ();
1146 /* If we can do it with a three-operand insn, do so. */
1148 if (methods != OPTAB_MUST_WIDEN
1149 && find_widening_optab_handler (binoptab, mode,
1150 widened_mode (mode, op0, op1), 1)
1151 != CODE_FOR_nothing)
1153 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1154 unsignedp, methods, last);
1155 if (temp)
1156 return temp;
1159 /* If we were trying to rotate, and that didn't work, try rotating
1160 the other direction before falling back to shifts and bitwise-or. */
1161 if (((binoptab == rotl_optab
1162 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1163 || (binoptab == rotr_optab
1164 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1165 && is_int_mode (mode, &int_mode))
1167 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1168 rtx newop1;
1169 unsigned int bits = GET_MODE_PRECISION (int_mode);
1171 if (CONST_INT_P (op1))
1172 newop1 = GEN_INT (bits - INTVAL (op1));
1173 else if (targetm.shift_truncation_mask (int_mode) == bits - 1)
1174 newop1 = negate_rtx (GET_MODE (op1), op1);
1175 else
1176 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1177 gen_int_mode (bits, GET_MODE (op1)), op1,
1178 NULL_RTX, unsignedp, OPTAB_DIRECT);
1180 temp = expand_binop_directly (int_mode, otheroptab, op0, newop1,
1181 target, unsignedp, methods, last);
1182 if (temp)
1183 return temp;
1186 /* If this is a multiply, see if we can do a widening operation that
1187 takes operands of this mode and makes a wider mode. */
1189 if (binoptab == smul_optab
1190 && GET_MODE_2XWIDER_MODE (mode).exists (&wider_mode)
1191 && (convert_optab_handler ((unsignedp
1192 ? umul_widen_optab
1193 : smul_widen_optab),
1194 wider_mode, mode) != CODE_FOR_nothing))
1196 temp = expand_binop (wider_mode,
1197 unsignedp ? umul_widen_optab : smul_widen_optab,
1198 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1200 if (temp != 0)
1202 if (GET_MODE_CLASS (mode) == MODE_INT
1203 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1204 return gen_lowpart (mode, temp);
1205 else
1206 return convert_to_mode (mode, temp, unsignedp);
1210 /* If this is a vector shift by a scalar, see if we can do a vector
1211 shift by a vector. If so, broadcast the scalar into a vector. */
1212 if (mclass == MODE_VECTOR_INT)
1214 optab otheroptab = unknown_optab;
1216 if (binoptab == ashl_optab)
1217 otheroptab = vashl_optab;
1218 else if (binoptab == ashr_optab)
1219 otheroptab = vashr_optab;
1220 else if (binoptab == lshr_optab)
1221 otheroptab = vlshr_optab;
1222 else if (binoptab == rotl_optab)
1223 otheroptab = vrotl_optab;
1224 else if (binoptab == rotr_optab)
1225 otheroptab = vrotr_optab;
1227 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1229 /* The scalar may have been extended to be too wide. Truncate
1230 it back to the proper size to fit in the broadcast vector. */
1231 machine_mode inner_mode = GET_MODE_INNER (mode);
1232 if (!CONST_INT_P (op1)
1233 && (GET_MODE_BITSIZE (inner_mode)
1234 < GET_MODE_BITSIZE (GET_MODE (op1))))
1235 op1 = force_reg (inner_mode,
1236 simplify_gen_unary (TRUNCATE, inner_mode, op1,
1237 GET_MODE (op1)));
1238 rtx vop1 = expand_vector_broadcast (mode, op1);
1239 if (vop1)
1241 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1242 target, unsignedp, methods, last);
1243 if (temp)
1244 return temp;
1249 /* Look for a wider mode of the same class for which we think we
1250 can open-code the operation. Check for a widening multiply at the
1251 wider mode as well. */
1253 if (CLASS_HAS_WIDER_MODES_P (mclass)
1254 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1255 FOR_EACH_WIDER_MODE (wider_mode, mode)
1257 machine_mode next_mode;
1258 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1259 || (binoptab == smul_optab
1260 && GET_MODE_WIDER_MODE (wider_mode).exists (&next_mode)
1261 && (find_widening_optab_handler ((unsignedp
1262 ? umul_widen_optab
1263 : smul_widen_optab),
1264 next_mode, mode, 0)
1265 != CODE_FOR_nothing)))
1267 rtx xop0 = op0, xop1 = op1;
1268 int no_extend = 0;
1270 /* For certain integer operations, we need not actually extend
1271 the narrow operands, as long as we will truncate
1272 the results to the same narrowness. */
1274 if ((binoptab == ior_optab || binoptab == and_optab
1275 || binoptab == xor_optab
1276 || binoptab == add_optab || binoptab == sub_optab
1277 || binoptab == smul_optab || binoptab == ashl_optab)
1278 && mclass == MODE_INT)
1280 no_extend = 1;
1281 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1282 xop0, unsignedp);
1283 if (binoptab != ashl_optab)
1284 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1285 xop1, unsignedp);
1288 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1290 /* The second operand of a shift must always be extended. */
1291 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1292 no_extend && binoptab != ashl_optab);
1294 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1295 unsignedp, OPTAB_DIRECT);
1296 if (temp)
1298 if (mclass != MODE_INT
1299 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1301 if (target == 0)
1302 target = gen_reg_rtx (mode);
1303 convert_move (target, temp, 0);
1304 return target;
1306 else
1307 return gen_lowpart (mode, temp);
1309 else
1310 delete_insns_since (last);
1314 /* If operation is commutative,
1315 try to make the first operand a register.
1316 Even better, try to make it the same as the target.
1317 Also try to make the last operand a constant. */
1318 if (commutative_optab_p (binoptab)
1319 && swap_commutative_operands_with_target (target, op0, op1))
1320 std::swap (op0, op1);
1322 /* These can be done a word at a time. */
1323 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1324 && is_int_mode (mode, &int_mode)
1325 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
1326 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1328 int i;
1329 rtx_insn *insns;
1331 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1332 won't be accurate, so use a new target. */
1333 if (target == 0
1334 || target == op0
1335 || target == op1
1336 || !valid_multiword_target_p (target))
1337 target = gen_reg_rtx (int_mode);
1339 start_sequence ();
1341 /* Do the actual arithmetic. */
1342 for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
1344 rtx target_piece = operand_subword (target, i, 1, int_mode);
1345 rtx x = expand_binop (word_mode, binoptab,
1346 operand_subword_force (op0, i, int_mode),
1347 operand_subword_force (op1, i, int_mode),
1348 target_piece, unsignedp, next_methods);
1350 if (x == 0)
1351 break;
1353 if (target_piece != x)
1354 emit_move_insn (target_piece, x);
1357 insns = get_insns ();
1358 end_sequence ();
1360 if (i == GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD)
1362 emit_insn (insns);
1363 return target;
1367 /* Synthesize double word shifts from single word shifts. */
1368 if ((binoptab == lshr_optab || binoptab == ashl_optab
1369 || binoptab == ashr_optab)
1370 && is_int_mode (mode, &int_mode)
1371 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1372 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
1373 && GET_MODE_PRECISION (int_mode) == GET_MODE_BITSIZE (int_mode)
1374 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1375 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1376 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1378 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1379 machine_mode op1_mode;
1381 double_shift_mask = targetm.shift_truncation_mask (int_mode);
1382 shift_mask = targetm.shift_truncation_mask (word_mode);
1383 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1385 /* Apply the truncation to constant shifts. */
1386 if (double_shift_mask > 0 && CONST_INT_P (op1))
1387 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1389 if (op1 == CONST0_RTX (op1_mode))
1390 return op0;
1392 /* Make sure that this is a combination that expand_doubleword_shift
1393 can handle. See the comments there for details. */
1394 if (double_shift_mask == 0
1395 || (shift_mask == BITS_PER_WORD - 1
1396 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1398 rtx_insn *insns;
1399 rtx into_target, outof_target;
1400 rtx into_input, outof_input;
1401 int left_shift, outof_word;
1403 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1404 won't be accurate, so use a new target. */
1405 if (target == 0
1406 || target == op0
1407 || target == op1
1408 || !valid_multiword_target_p (target))
1409 target = gen_reg_rtx (int_mode);
1411 start_sequence ();
1413 /* OUTOF_* is the word we are shifting bits away from, and
1414 INTO_* is the word that we are shifting bits towards, thus
1415 they differ depending on the direction of the shift and
1416 WORDS_BIG_ENDIAN. */
1418 left_shift = binoptab == ashl_optab;
1419 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1421 outof_target = operand_subword (target, outof_word, 1, int_mode);
1422 into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
1424 outof_input = operand_subword_force (op0, outof_word, int_mode);
1425 into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
1427 if (expand_doubleword_shift (op1_mode, binoptab,
1428 outof_input, into_input, op1,
1429 outof_target, into_target,
1430 unsignedp, next_methods, shift_mask))
1432 insns = get_insns ();
1433 end_sequence ();
1435 emit_insn (insns);
1436 return target;
1438 end_sequence ();
1442 /* Synthesize double word rotates from single word shifts. */
1443 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1444 && is_int_mode (mode, &int_mode)
1445 && CONST_INT_P (op1)
1446 && GET_MODE_PRECISION (int_mode) == 2 * BITS_PER_WORD
1447 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1448 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1450 rtx_insn *insns;
1451 rtx into_target, outof_target;
1452 rtx into_input, outof_input;
1453 rtx inter;
1454 int shift_count, left_shift, outof_word;
1456 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1457 won't be accurate, so use a new target. Do this also if target is not
1458 a REG, first because having a register instead may open optimization
1459 opportunities, and second because if target and op0 happen to be MEMs
1460 designating the same location, we would risk clobbering it too early
1461 in the code sequence we generate below. */
1462 if (target == 0
1463 || target == op0
1464 || target == op1
1465 || !REG_P (target)
1466 || !valid_multiword_target_p (target))
1467 target = gen_reg_rtx (int_mode);
1469 start_sequence ();
1471 shift_count = INTVAL (op1);
1473 /* OUTOF_* is the word we are shifting bits away from, and
1474 INTO_* is the word that we are shifting bits towards, thus
1475 they differ depending on the direction of the shift and
1476 WORDS_BIG_ENDIAN. */
1478 left_shift = (binoptab == rotl_optab);
1479 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1481 outof_target = operand_subword (target, outof_word, 1, int_mode);
1482 into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
1484 outof_input = operand_subword_force (op0, outof_word, int_mode);
1485 into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
1487 if (shift_count == BITS_PER_WORD)
1489 /* This is just a word swap. */
1490 emit_move_insn (outof_target, into_input);
1491 emit_move_insn (into_target, outof_input);
1492 inter = const0_rtx;
1494 else
1496 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1497 rtx first_shift_count, second_shift_count;
1498 optab reverse_unsigned_shift, unsigned_shift;
1500 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1501 ? lshr_optab : ashl_optab);
1503 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1504 ? ashl_optab : lshr_optab);
1506 if (shift_count > BITS_PER_WORD)
1508 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1509 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1511 else
1513 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1514 second_shift_count = GEN_INT (shift_count);
1517 into_temp1 = expand_binop (word_mode, unsigned_shift,
1518 outof_input, first_shift_count,
1519 NULL_RTX, unsignedp, next_methods);
1520 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1521 into_input, second_shift_count,
1522 NULL_RTX, unsignedp, next_methods);
1524 if (into_temp1 != 0 && into_temp2 != 0)
1525 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1526 into_target, unsignedp, next_methods);
1527 else
1528 inter = 0;
1530 if (inter != 0 && inter != into_target)
1531 emit_move_insn (into_target, inter);
1533 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1534 into_input, first_shift_count,
1535 NULL_RTX, unsignedp, next_methods);
1536 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1537 outof_input, second_shift_count,
1538 NULL_RTX, unsignedp, next_methods);
1540 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1541 inter = expand_binop (word_mode, ior_optab,
1542 outof_temp1, outof_temp2,
1543 outof_target, unsignedp, next_methods);
1545 if (inter != 0 && inter != outof_target)
1546 emit_move_insn (outof_target, inter);
1549 insns = get_insns ();
1550 end_sequence ();
1552 if (inter != 0)
1554 emit_insn (insns);
1555 return target;
1559 /* These can be done a word at a time by propagating carries. */
1560 if ((binoptab == add_optab || binoptab == sub_optab)
1561 && is_int_mode (mode, &int_mode)
1562 && GET_MODE_SIZE (int_mode) >= 2 * UNITS_PER_WORD
1563 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1565 unsigned int i;
1566 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1567 const unsigned int nwords = GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD;
1568 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1569 rtx xop0, xop1, xtarget;
1571 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1572 value is one of those, use it. Otherwise, use 1 since it is the
1573 one easiest to get. */
1574 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1575 int normalizep = STORE_FLAG_VALUE;
1576 #else
1577 int normalizep = 1;
1578 #endif
1580 /* Prepare the operands. */
1581 xop0 = force_reg (int_mode, op0);
1582 xop1 = force_reg (int_mode, op1);
1584 xtarget = gen_reg_rtx (int_mode);
1586 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1587 target = xtarget;
1589 /* Indicate for flow that the entire target reg is being set. */
1590 if (REG_P (target))
1591 emit_clobber (xtarget);
1593 /* Do the actual arithmetic. */
1594 for (i = 0; i < nwords; i++)
1596 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1597 rtx target_piece = operand_subword (xtarget, index, 1, int_mode);
1598 rtx op0_piece = operand_subword_force (xop0, index, int_mode);
1599 rtx op1_piece = operand_subword_force (xop1, index, int_mode);
1600 rtx x;
1602 /* Main add/subtract of the input operands. */
1603 x = expand_binop (word_mode, binoptab,
1604 op0_piece, op1_piece,
1605 target_piece, unsignedp, next_methods);
1606 if (x == 0)
1607 break;
1609 if (i + 1 < nwords)
1611 /* Store carry from main add/subtract. */
1612 carry_out = gen_reg_rtx (word_mode);
1613 carry_out = emit_store_flag_force (carry_out,
1614 (binoptab == add_optab
1615 ? LT : GT),
1616 x, op0_piece,
1617 word_mode, 1, normalizep);
1620 if (i > 0)
1622 rtx newx;
1624 /* Add/subtract previous carry to main result. */
1625 newx = expand_binop (word_mode,
1626 normalizep == 1 ? binoptab : otheroptab,
1627 x, carry_in,
1628 NULL_RTX, 1, next_methods);
1630 if (i + 1 < nwords)
1632 /* Get out carry from adding/subtracting carry in. */
1633 rtx carry_tmp = gen_reg_rtx (word_mode);
1634 carry_tmp = emit_store_flag_force (carry_tmp,
1635 (binoptab == add_optab
1636 ? LT : GT),
1637 newx, x,
1638 word_mode, 1, normalizep);
1640 /* Logical-ior the two poss. carry together. */
1641 carry_out = expand_binop (word_mode, ior_optab,
1642 carry_out, carry_tmp,
1643 carry_out, 0, next_methods);
1644 if (carry_out == 0)
1645 break;
1647 emit_move_insn (target_piece, newx);
1649 else
1651 if (x != target_piece)
1652 emit_move_insn (target_piece, x);
1655 carry_in = carry_out;
1658 if (i == GET_MODE_BITSIZE (int_mode) / (unsigned) BITS_PER_WORD)
1660 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing
1661 || ! rtx_equal_p (target, xtarget))
1663 rtx_insn *temp = emit_move_insn (target, xtarget);
1665 set_dst_reg_note (temp, REG_EQUAL,
1666 gen_rtx_fmt_ee (optab_to_code (binoptab),
1667 int_mode, copy_rtx (xop0),
1668 copy_rtx (xop1)),
1669 target);
1671 else
1672 target = xtarget;
1674 return target;
1677 else
1678 delete_insns_since (last);
1681 /* Attempt to synthesize double word multiplies using a sequence of word
1682 mode multiplications. We first attempt to generate a sequence using a
1683 more efficient unsigned widening multiply, and if that fails we then
1684 try using a signed widening multiply. */
1686 if (binoptab == smul_optab
1687 && is_int_mode (mode, &int_mode)
1688 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
1689 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
1690 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
1692 rtx product = NULL_RTX;
1693 if (widening_optab_handler (umul_widen_optab, int_mode, word_mode)
1694 != CODE_FOR_nothing)
1696 product = expand_doubleword_mult (int_mode, op0, op1, target,
1697 true, methods);
1698 if (!product)
1699 delete_insns_since (last);
1702 if (product == NULL_RTX
1703 && (widening_optab_handler (smul_widen_optab, int_mode, word_mode)
1704 != CODE_FOR_nothing))
1706 product = expand_doubleword_mult (int_mode, op0, op1, target,
1707 false, methods);
1708 if (!product)
1709 delete_insns_since (last);
1712 if (product != NULL_RTX)
1714 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
1716 rtx_insn *move = emit_move_insn (target ? target : product,
1717 product);
1718 set_dst_reg_note (move,
1719 REG_EQUAL,
1720 gen_rtx_fmt_ee (MULT, int_mode,
1721 copy_rtx (op0),
1722 copy_rtx (op1)),
1723 target ? target : product);
1725 return product;
1729 /* It can't be open-coded in this mode.
1730 Use a library call if one is available and caller says that's ok. */
1732 libfunc = optab_libfunc (binoptab, mode);
1733 if (libfunc
1734 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
1736 rtx_insn *insns;
1737 rtx op1x = op1;
1738 machine_mode op1_mode = mode;
1739 rtx value;
1741 start_sequence ();
1743 if (shift_optab_p (binoptab))
1745 op1_mode = targetm.libgcc_shift_count_mode ();
1746 /* Specify unsigned here,
1747 since negative shift counts are meaningless. */
1748 op1x = convert_to_mode (op1_mode, op1, 1);
1751 if (GET_MODE (op0) != VOIDmode
1752 && GET_MODE (op0) != mode)
1753 op0 = convert_to_mode (mode, op0, unsignedp);
1755 /* Pass 1 for NO_QUEUE so we don't lose any increments
1756 if the libcall is cse'd or moved. */
1757 value = emit_library_call_value (libfunc,
1758 NULL_RTX, LCT_CONST, mode, 2,
1759 op0, mode, op1x, op1_mode);
1761 insns = get_insns ();
1762 end_sequence ();
1764 bool trapv = trapv_binoptab_p (binoptab);
1765 target = gen_reg_rtx (mode);
1766 emit_libcall_block_1 (insns, target, value,
1767 trapv ? NULL_RTX
1768 : gen_rtx_fmt_ee (optab_to_code (binoptab),
1769 mode, op0, op1), trapv);
1771 return target;
1774 delete_insns_since (last);
1776 /* It can't be done in this mode. Can we do it in a wider mode? */
1778 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
1779 || methods == OPTAB_MUST_WIDEN))
1781 /* Caller says, don't even try. */
1782 delete_insns_since (entry_last);
1783 return 0;
1786 /* Compute the value of METHODS to pass to recursive calls.
1787 Don't allow widening to be tried recursively. */
1789 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
1791 /* Look for a wider mode of the same class for which it appears we can do
1792 the operation. */
1794 if (CLASS_HAS_WIDER_MODES_P (mclass))
1796 FOR_EACH_WIDER_MODE (wider_mode, mode)
1798 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
1799 != CODE_FOR_nothing
1800 || (methods == OPTAB_LIB
1801 && optab_libfunc (binoptab, wider_mode)))
1803 rtx xop0 = op0, xop1 = op1;
1804 int no_extend = 0;
1806 /* For certain integer operations, we need not actually extend
1807 the narrow operands, as long as we will truncate
1808 the results to the same narrowness. */
1810 if ((binoptab == ior_optab || binoptab == and_optab
1811 || binoptab == xor_optab
1812 || binoptab == add_optab || binoptab == sub_optab
1813 || binoptab == smul_optab || binoptab == ashl_optab)
1814 && mclass == MODE_INT)
1815 no_extend = 1;
1817 xop0 = widen_operand (xop0, wider_mode, mode,
1818 unsignedp, no_extend);
1820 /* The second operand of a shift must always be extended. */
1821 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1822 no_extend && binoptab != ashl_optab);
1824 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1825 unsignedp, methods);
1826 if (temp)
1828 if (mclass != MODE_INT
1829 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1831 if (target == 0)
1832 target = gen_reg_rtx (mode);
1833 convert_move (target, temp, 0);
1834 return target;
1836 else
1837 return gen_lowpart (mode, temp);
1839 else
1840 delete_insns_since (last);
1845 delete_insns_since (entry_last);
1846 return 0;
1849 /* Expand a binary operator which has both signed and unsigned forms.
1850 UOPTAB is the optab for unsigned operations, and SOPTAB is for
1851 signed operations.
1853 If we widen unsigned operands, we may use a signed wider operation instead
1854 of an unsigned wider operation, since the result would be the same. */
1857 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
1858 rtx op0, rtx op1, rtx target, int unsignedp,
1859 enum optab_methods methods)
1861 rtx temp;
1862 optab direct_optab = unsignedp ? uoptab : soptab;
1863 bool save_enable;
1865 /* Do it without widening, if possible. */
1866 temp = expand_binop (mode, direct_optab, op0, op1, target,
1867 unsignedp, OPTAB_DIRECT);
1868 if (temp || methods == OPTAB_DIRECT)
1869 return temp;
1871 /* Try widening to a signed int. Disable any direct use of any
1872 signed insn in the current mode. */
1873 save_enable = swap_optab_enable (soptab, mode, false);
1875 temp = expand_binop (mode, soptab, op0, op1, target,
1876 unsignedp, OPTAB_WIDEN);
1878 /* For unsigned operands, try widening to an unsigned int. */
1879 if (!temp && unsignedp)
1880 temp = expand_binop (mode, uoptab, op0, op1, target,
1881 unsignedp, OPTAB_WIDEN);
1882 if (temp || methods == OPTAB_WIDEN)
1883 goto egress;
1885 /* Use the right width libcall if that exists. */
1886 temp = expand_binop (mode, direct_optab, op0, op1, target,
1887 unsignedp, OPTAB_LIB);
1888 if (temp || methods == OPTAB_LIB)
1889 goto egress;
1891 /* Must widen and use a libcall, use either signed or unsigned. */
1892 temp = expand_binop (mode, soptab, op0, op1, target,
1893 unsignedp, methods);
1894 if (!temp && unsignedp)
1895 temp = expand_binop (mode, uoptab, op0, op1, target,
1896 unsignedp, methods);
1898 egress:
1899 /* Undo the fiddling above. */
1900 if (save_enable)
1901 swap_optab_enable (soptab, mode, true);
1902 return temp;
1905 /* Generate code to perform an operation specified by UNOPPTAB
1906 on operand OP0, with two results to TARG0 and TARG1.
1907 We assume that the order of the operands for the instruction
1908 is TARG0, TARG1, OP0.
1910 Either TARG0 or TARG1 may be zero, but what that means is that
1911 the result is not actually wanted. We will generate it into
1912 a dummy pseudo-reg and discard it. They may not both be zero.
1914 Returns 1 if this operation can be performed; 0 if not. */
1917 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
1918 int unsignedp)
1920 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1921 enum mode_class mclass;
1922 machine_mode wider_mode;
1923 rtx_insn *entry_last = get_last_insn ();
1924 rtx_insn *last;
1926 mclass = GET_MODE_CLASS (mode);
1928 if (!targ0)
1929 targ0 = gen_reg_rtx (mode);
1930 if (!targ1)
1931 targ1 = gen_reg_rtx (mode);
1933 /* Record where to go back to if we fail. */
1934 last = get_last_insn ();
1936 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
1938 struct expand_operand ops[3];
1939 enum insn_code icode = optab_handler (unoptab, mode);
1941 create_fixed_operand (&ops[0], targ0);
1942 create_fixed_operand (&ops[1], targ1);
1943 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
1944 if (maybe_expand_insn (icode, 3, ops))
1945 return 1;
1948 /* It can't be done in this mode. Can we do it in a wider mode? */
1950 if (CLASS_HAS_WIDER_MODES_P (mclass))
1952 FOR_EACH_WIDER_MODE (wider_mode, mode)
1954 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
1956 rtx t0 = gen_reg_rtx (wider_mode);
1957 rtx t1 = gen_reg_rtx (wider_mode);
1958 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1960 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
1962 convert_move (targ0, t0, unsignedp);
1963 convert_move (targ1, t1, unsignedp);
1964 return 1;
1966 else
1967 delete_insns_since (last);
1972 delete_insns_since (entry_last);
1973 return 0;
1976 /* Generate code to perform an operation specified by BINOPTAB
1977 on operands OP0 and OP1, with two results to TARG1 and TARG2.
1978 We assume that the order of the operands for the instruction
1979 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
1980 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
1982 Either TARG0 or TARG1 may be zero, but what that means is that
1983 the result is not actually wanted. We will generate it into
1984 a dummy pseudo-reg and discard it. They may not both be zero.
1986 Returns 1 if this operation can be performed; 0 if not. */
1989 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
1990 int unsignedp)
1992 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1993 enum mode_class mclass;
1994 machine_mode wider_mode;
1995 rtx_insn *entry_last = get_last_insn ();
1996 rtx_insn *last;
1998 mclass = GET_MODE_CLASS (mode);
2000 if (!targ0)
2001 targ0 = gen_reg_rtx (mode);
2002 if (!targ1)
2003 targ1 = gen_reg_rtx (mode);
2005 /* Record where to go back to if we fail. */
2006 last = get_last_insn ();
2008 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2010 struct expand_operand ops[4];
2011 enum insn_code icode = optab_handler (binoptab, mode);
2012 machine_mode mode0 = insn_data[icode].operand[1].mode;
2013 machine_mode mode1 = insn_data[icode].operand[2].mode;
2014 rtx xop0 = op0, xop1 = op1;
2016 /* If we are optimizing, force expensive constants into a register. */
2017 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2018 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2020 create_fixed_operand (&ops[0], targ0);
2021 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2022 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2023 create_fixed_operand (&ops[3], targ1);
2024 if (maybe_expand_insn (icode, 4, ops))
2025 return 1;
2026 delete_insns_since (last);
2029 /* It can't be done in this mode. Can we do it in a wider mode? */
2031 if (CLASS_HAS_WIDER_MODES_P (mclass))
2033 FOR_EACH_WIDER_MODE (wider_mode, mode)
2035 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2037 rtx t0 = gen_reg_rtx (wider_mode);
2038 rtx t1 = gen_reg_rtx (wider_mode);
2039 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2040 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2042 if (expand_twoval_binop (binoptab, cop0, cop1,
2043 t0, t1, unsignedp))
2045 convert_move (targ0, t0, unsignedp);
2046 convert_move (targ1, t1, unsignedp);
2047 return 1;
2049 else
2050 delete_insns_since (last);
2055 delete_insns_since (entry_last);
2056 return 0;
2059 /* Expand the two-valued library call indicated by BINOPTAB, but
2060 preserve only one of the values. If TARG0 is non-NULL, the first
2061 value is placed into TARG0; otherwise the second value is placed
2062 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2063 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2064 This routine assumes that the value returned by the library call is
2065 as if the return value was of an integral mode twice as wide as the
2066 mode of OP0. Returns 1 if the call was successful. */
2068 bool
2069 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2070 rtx targ0, rtx targ1, enum rtx_code code)
2072 machine_mode mode;
2073 machine_mode libval_mode;
2074 rtx libval;
2075 rtx_insn *insns;
2076 rtx libfunc;
2078 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2079 gcc_assert (!targ0 != !targ1);
2081 mode = GET_MODE (op0);
2082 libfunc = optab_libfunc (binoptab, mode);
2083 if (!libfunc)
2084 return false;
2086 /* The value returned by the library function will have twice as
2087 many bits as the nominal MODE. */
2088 libval_mode = smallest_int_mode_for_size (2 * GET_MODE_BITSIZE (mode));
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 if (!int_mode_for_mode (mode).exists (&imode))
2580 return NULL_RTX;
2581 word = 0;
2582 nwords = 1;
2584 else
2586 imode = word_mode;
2588 if (FLOAT_WORDS_BIG_ENDIAN)
2589 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2590 else
2591 word = bitpos / BITS_PER_WORD;
2592 bitpos = bitpos % BITS_PER_WORD;
2593 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2596 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2597 if (code == ABS)
2598 mask = ~mask;
2600 if (target == 0
2601 || target == op0
2602 || (nwords > 1 && !valid_multiword_target_p (target)))
2603 target = gen_reg_rtx (mode);
2605 if (nwords > 1)
2607 start_sequence ();
2609 for (i = 0; i < nwords; ++i)
2611 rtx targ_piece = operand_subword (target, i, 1, mode);
2612 rtx op0_piece = operand_subword_force (op0, i, mode);
2614 if (i == word)
2616 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2617 op0_piece,
2618 immed_wide_int_const (mask, imode),
2619 targ_piece, 1, OPTAB_LIB_WIDEN);
2620 if (temp != targ_piece)
2621 emit_move_insn (targ_piece, temp);
2623 else
2624 emit_move_insn (targ_piece, op0_piece);
2627 insns = get_insns ();
2628 end_sequence ();
2630 emit_insn (insns);
2632 else
2634 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2635 gen_lowpart (imode, op0),
2636 immed_wide_int_const (mask, imode),
2637 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2638 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2640 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2641 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2642 target);
2645 return target;
2648 /* As expand_unop, but will fail rather than attempt the operation in a
2649 different mode or with a libcall. */
2650 static rtx
2651 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
2652 int unsignedp)
2654 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2656 struct expand_operand ops[2];
2657 enum insn_code icode = optab_handler (unoptab, mode);
2658 rtx_insn *last = get_last_insn ();
2659 rtx_insn *pat;
2661 create_output_operand (&ops[0], target, mode);
2662 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2663 pat = maybe_gen_insn (icode, 2, ops);
2664 if (pat)
2666 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2667 && ! add_equal_note (pat, ops[0].value,
2668 optab_to_code (unoptab),
2669 ops[1].value, NULL_RTX))
2671 delete_insns_since (last);
2672 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2675 emit_insn (pat);
2677 return ops[0].value;
2680 return 0;
2683 /* Generate code to perform an operation specified by UNOPTAB
2684 on operand OP0, with result having machine-mode MODE.
2686 UNSIGNEDP is for the case where we have to widen the operands
2687 to perform the operation. It says to use zero-extension.
2689 If TARGET is nonzero, the value
2690 is generated there, if it is convenient to do so.
2691 In all cases an rtx is returned for the locus of the value;
2692 this may or may not be TARGET. */
2695 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
2696 int unsignedp)
2698 enum mode_class mclass = GET_MODE_CLASS (mode);
2699 machine_mode wider_mode;
2700 scalar_int_mode int_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 && is_int_mode (mode, &int_mode)
2859 && GET_MODE_SIZE (int_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 (int_mode);
2868 start_sequence ();
2870 /* Do the actual arithmetic. */
2871 for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
2873 rtx target_piece = operand_subword (target, i, 1, int_mode);
2874 rtx x = expand_unop (word_mode, unoptab,
2875 operand_subword_force (op0, i, int_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 scalar_int_mode int_mode;
3122 if (is_int_mode (mode, &int_mode)
3123 && BRANCH_COST (optimize_insn_for_speed_p (),
3124 false) >= 2)
3126 rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
3127 GET_MODE_PRECISION (int_mode) - 1,
3128 NULL_RTX, 0);
3130 temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
3131 OPTAB_LIB_WIDEN);
3132 if (temp != 0)
3133 temp = expand_binop (int_mode,
3134 result_unsignedp ? sub_optab : subv_optab,
3135 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3137 if (temp != 0)
3138 return temp;
3141 return NULL_RTX;
3145 expand_abs (machine_mode mode, rtx op0, rtx target,
3146 int result_unsignedp, int safe)
3148 rtx temp;
3149 rtx_code_label *op1;
3151 if (GET_MODE_CLASS (mode) != MODE_INT
3152 || ! flag_trapv)
3153 result_unsignedp = 1;
3155 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3156 if (temp != 0)
3157 return temp;
3159 /* If that does not win, use conditional jump and negate. */
3161 /* It is safe to use the target if it is the same
3162 as the source if this is also a pseudo register */
3163 if (op0 == target && REG_P (op0)
3164 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3165 safe = 1;
3167 op1 = gen_label_rtx ();
3168 if (target == 0 || ! safe
3169 || GET_MODE (target) != mode
3170 || (MEM_P (target) && MEM_VOLATILE_P (target))
3171 || (REG_P (target)
3172 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3173 target = gen_reg_rtx (mode);
3175 emit_move_insn (target, op0);
3176 NO_DEFER_POP;
3178 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3179 NULL_RTX, NULL, op1,
3180 profile_probability::uninitialized ());
3182 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3183 target, target, 0);
3184 if (op0 != target)
3185 emit_move_insn (target, op0);
3186 emit_label (op1);
3187 OK_DEFER_POP;
3188 return target;
3191 /* Emit code to compute the one's complement absolute value of OP0
3192 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3193 (TARGET may be NULL_RTX.) The return value says where the result
3194 actually is to be found.
3196 MODE is the mode of the operand; the mode of the result is
3197 different but can be deduced from MODE. */
3200 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3202 rtx temp;
3204 /* Not applicable for floating point modes. */
3205 if (FLOAT_MODE_P (mode))
3206 return NULL_RTX;
3208 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3209 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3211 rtx_insn *last = get_last_insn ();
3213 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3214 if (temp != 0)
3215 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3216 OPTAB_WIDEN);
3218 if (temp != 0)
3219 return temp;
3221 delete_insns_since (last);
3224 /* If this machine has expensive jumps, we can do one's complement
3225 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3227 scalar_int_mode int_mode;
3228 if (is_int_mode (mode, &int_mode)
3229 && BRANCH_COST (optimize_insn_for_speed_p (),
3230 false) >= 2)
3232 rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
3233 GET_MODE_PRECISION (int_mode) - 1,
3234 NULL_RTX, 0);
3236 temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
3237 OPTAB_LIB_WIDEN);
3239 if (temp != 0)
3240 return temp;
3243 return NULL_RTX;
3246 /* A subroutine of expand_copysign, perform the copysign operation using the
3247 abs and neg primitives advertised to exist on the target. The assumption
3248 is that we have a split register file, and leaving op0 in fp registers,
3249 and not playing with subregs so much, will help the register allocator. */
3251 static rtx
3252 expand_copysign_absneg (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3253 int bitpos, bool op0_is_abs)
3255 machine_mode imode;
3256 enum insn_code icode;
3257 rtx sign;
3258 rtx_code_label *label;
3260 if (target == op1)
3261 target = NULL_RTX;
3263 /* Check if the back end provides an insn that handles signbit for the
3264 argument's mode. */
3265 icode = optab_handler (signbit_optab, mode);
3266 if (icode != CODE_FOR_nothing)
3268 imode = insn_data[(int) icode].operand[0].mode;
3269 sign = gen_reg_rtx (imode);
3270 emit_unop_insn (icode, sign, op1, UNKNOWN);
3272 else
3274 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3276 if (!int_mode_for_mode (mode).exists (&imode))
3277 return NULL_RTX;
3278 op1 = gen_lowpart (imode, op1);
3280 else
3282 int word;
3284 imode = word_mode;
3285 if (FLOAT_WORDS_BIG_ENDIAN)
3286 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3287 else
3288 word = bitpos / BITS_PER_WORD;
3289 bitpos = bitpos % BITS_PER_WORD;
3290 op1 = operand_subword_force (op1, word, mode);
3293 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3294 sign = expand_binop (imode, and_optab, op1,
3295 immed_wide_int_const (mask, imode),
3296 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3299 if (!op0_is_abs)
3301 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3302 if (op0 == NULL)
3303 return NULL_RTX;
3304 target = op0;
3306 else
3308 if (target == NULL_RTX)
3309 target = copy_to_reg (op0);
3310 else
3311 emit_move_insn (target, op0);
3314 label = gen_label_rtx ();
3315 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3317 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3318 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3319 else
3320 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3321 if (op0 != target)
3322 emit_move_insn (target, op0);
3324 emit_label (label);
3326 return target;
3330 /* A subroutine of expand_copysign, perform the entire copysign operation
3331 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3332 is true if op0 is known to have its sign bit clear. */
3334 static rtx
3335 expand_copysign_bit (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3336 int bitpos, bool op0_is_abs)
3338 scalar_int_mode imode;
3339 int word, nwords, i;
3340 rtx temp;
3341 rtx_insn *insns;
3343 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3345 if (!int_mode_for_mode (mode).exists (&imode))
3346 return NULL_RTX;
3347 word = 0;
3348 nwords = 1;
3350 else
3352 imode = word_mode;
3354 if (FLOAT_WORDS_BIG_ENDIAN)
3355 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3356 else
3357 word = bitpos / BITS_PER_WORD;
3358 bitpos = bitpos % BITS_PER_WORD;
3359 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3362 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3364 if (target == 0
3365 || target == op0
3366 || target == op1
3367 || (nwords > 1 && !valid_multiword_target_p (target)))
3368 target = gen_reg_rtx (mode);
3370 if (nwords > 1)
3372 start_sequence ();
3374 for (i = 0; i < nwords; ++i)
3376 rtx targ_piece = operand_subword (target, i, 1, mode);
3377 rtx op0_piece = operand_subword_force (op0, i, mode);
3379 if (i == word)
3381 if (!op0_is_abs)
3382 op0_piece
3383 = expand_binop (imode, and_optab, op0_piece,
3384 immed_wide_int_const (~mask, imode),
3385 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3386 op1 = expand_binop (imode, and_optab,
3387 operand_subword_force (op1, i, mode),
3388 immed_wide_int_const (mask, imode),
3389 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3391 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3392 targ_piece, 1, OPTAB_LIB_WIDEN);
3393 if (temp != targ_piece)
3394 emit_move_insn (targ_piece, temp);
3396 else
3397 emit_move_insn (targ_piece, op0_piece);
3400 insns = get_insns ();
3401 end_sequence ();
3403 emit_insn (insns);
3405 else
3407 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3408 immed_wide_int_const (mask, imode),
3409 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3411 op0 = gen_lowpart (imode, op0);
3412 if (!op0_is_abs)
3413 op0 = expand_binop (imode, and_optab, op0,
3414 immed_wide_int_const (~mask, imode),
3415 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3417 temp = expand_binop (imode, ior_optab, op0, op1,
3418 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3419 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3422 return target;
3425 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3426 scalar floating point mode. Return NULL if we do not know how to
3427 expand the operation inline. */
3430 expand_copysign (rtx op0, rtx op1, rtx target)
3432 scalar_float_mode mode;
3433 const struct real_format *fmt;
3434 bool op0_is_abs;
3435 rtx temp;
3437 mode = as_a <scalar_float_mode> (GET_MODE (op0));
3438 gcc_assert (GET_MODE (op1) == mode);
3440 /* First try to do it with a special instruction. */
3441 temp = expand_binop (mode, copysign_optab, op0, op1,
3442 target, 0, OPTAB_DIRECT);
3443 if (temp)
3444 return temp;
3446 fmt = REAL_MODE_FORMAT (mode);
3447 if (fmt == NULL || !fmt->has_signed_zero)
3448 return NULL_RTX;
3450 op0_is_abs = false;
3451 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3453 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3454 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3455 op0_is_abs = true;
3458 if (fmt->signbit_ro >= 0
3459 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3460 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3461 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3463 temp = expand_copysign_absneg (mode, op0, op1, target,
3464 fmt->signbit_ro, op0_is_abs);
3465 if (temp)
3466 return temp;
3469 if (fmt->signbit_rw < 0)
3470 return NULL_RTX;
3471 return expand_copysign_bit (mode, op0, op1, target,
3472 fmt->signbit_rw, op0_is_abs);
3475 /* Generate an instruction whose insn-code is INSN_CODE,
3476 with two operands: an output TARGET and an input OP0.
3477 TARGET *must* be nonzero, and the output is always stored there.
3478 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3479 the value that is stored into TARGET.
3481 Return false if expansion failed. */
3483 bool
3484 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3485 enum rtx_code code)
3487 struct expand_operand ops[2];
3488 rtx_insn *pat;
3490 create_output_operand (&ops[0], target, GET_MODE (target));
3491 create_input_operand (&ops[1], op0, GET_MODE (op0));
3492 pat = maybe_gen_insn (icode, 2, ops);
3493 if (!pat)
3494 return false;
3496 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3497 && code != UNKNOWN)
3498 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3500 emit_insn (pat);
3502 if (ops[0].value != target)
3503 emit_move_insn (target, ops[0].value);
3504 return true;
3506 /* Generate an instruction whose insn-code is INSN_CODE,
3507 with two operands: an output TARGET and an input OP0.
3508 TARGET *must* be nonzero, and the output is always stored there.
3509 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3510 the value that is stored into TARGET. */
3512 void
3513 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3515 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3516 gcc_assert (ok);
3519 struct no_conflict_data
3521 rtx target;
3522 rtx_insn *first, *insn;
3523 bool must_stay;
3526 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3527 the currently examined clobber / store has to stay in the list of
3528 insns that constitute the actual libcall block. */
3529 static void
3530 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3532 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3534 /* If this inns directly contributes to setting the target, it must stay. */
3535 if (reg_overlap_mentioned_p (p->target, dest))
3536 p->must_stay = true;
3537 /* If we haven't committed to keeping any other insns in the list yet,
3538 there is nothing more to check. */
3539 else if (p->insn == p->first)
3540 return;
3541 /* If this insn sets / clobbers a register that feeds one of the insns
3542 already in the list, this insn has to stay too. */
3543 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3544 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3545 || reg_used_between_p (dest, p->first, p->insn)
3546 /* Likewise if this insn depends on a register set by a previous
3547 insn in the list, or if it sets a result (presumably a hard
3548 register) that is set or clobbered by a previous insn.
3549 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3550 SET_DEST perform the former check on the address, and the latter
3551 check on the MEM. */
3552 || (GET_CODE (set) == SET
3553 && (modified_in_p (SET_SRC (set), p->first)
3554 || modified_in_p (SET_DEST (set), p->first)
3555 || modified_between_p (SET_SRC (set), p->first, p->insn)
3556 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3557 p->must_stay = true;
3561 /* Emit code to make a call to a constant function or a library call.
3563 INSNS is a list containing all insns emitted in the call.
3564 These insns leave the result in RESULT. Our block is to copy RESULT
3565 to TARGET, which is logically equivalent to EQUIV.
3567 We first emit any insns that set a pseudo on the assumption that these are
3568 loading constants into registers; doing so allows them to be safely cse'ed
3569 between blocks. Then we emit all the other insns in the block, followed by
3570 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3571 note with an operand of EQUIV. */
3573 static void
3574 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3575 bool equiv_may_trap)
3577 rtx final_dest = target;
3578 rtx_insn *next, *last, *insn;
3580 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3581 into a MEM later. Protect the libcall block from this change. */
3582 if (! REG_P (target) || REG_USERVAR_P (target))
3583 target = gen_reg_rtx (GET_MODE (target));
3585 /* If we're using non-call exceptions, a libcall corresponding to an
3586 operation that may trap may also trap. */
3587 /* ??? See the comment in front of make_reg_eh_region_note. */
3588 if (cfun->can_throw_non_call_exceptions
3589 && (equiv_may_trap || may_trap_p (equiv)))
3591 for (insn = insns; insn; insn = NEXT_INSN (insn))
3592 if (CALL_P (insn))
3594 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3595 if (note)
3597 int lp_nr = INTVAL (XEXP (note, 0));
3598 if (lp_nr == 0 || lp_nr == INT_MIN)
3599 remove_note (insn, note);
3603 else
3605 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3606 reg note to indicate that this call cannot throw or execute a nonlocal
3607 goto (unless there is already a REG_EH_REGION note, in which case
3608 we update it). */
3609 for (insn = insns; insn; insn = NEXT_INSN (insn))
3610 if (CALL_P (insn))
3611 make_reg_eh_region_note_nothrow_nononlocal (insn);
3614 /* First emit all insns that set pseudos. Remove them from the list as
3615 we go. Avoid insns that set pseudos which were referenced in previous
3616 insns. These can be generated by move_by_pieces, for example,
3617 to update an address. Similarly, avoid insns that reference things
3618 set in previous insns. */
3620 for (insn = insns; insn; insn = next)
3622 rtx set = single_set (insn);
3624 next = NEXT_INSN (insn);
3626 if (set != 0 && REG_P (SET_DEST (set))
3627 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3629 struct no_conflict_data data;
3631 data.target = const0_rtx;
3632 data.first = insns;
3633 data.insn = insn;
3634 data.must_stay = 0;
3635 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3636 if (! data.must_stay)
3638 if (PREV_INSN (insn))
3639 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3640 else
3641 insns = next;
3643 if (next)
3644 SET_PREV_INSN (next) = PREV_INSN (insn);
3646 add_insn (insn);
3650 /* Some ports use a loop to copy large arguments onto the stack.
3651 Don't move anything outside such a loop. */
3652 if (LABEL_P (insn))
3653 break;
3656 /* Write the remaining insns followed by the final copy. */
3657 for (insn = insns; insn; insn = next)
3659 next = NEXT_INSN (insn);
3661 add_insn (insn);
3664 last = emit_move_insn (target, result);
3665 if (equiv)
3666 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3668 if (final_dest != target)
3669 emit_move_insn (final_dest, target);
3672 void
3673 emit_libcall_block (rtx_insn *insns, rtx target, rtx result, rtx equiv)
3675 emit_libcall_block_1 (insns, target, result, equiv, false);
3678 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3679 PURPOSE describes how this comparison will be used. CODE is the rtx
3680 comparison code we will be using.
3682 ??? Actually, CODE is slightly weaker than that. A target is still
3683 required to implement all of the normal bcc operations, but not
3684 required to implement all (or any) of the unordered bcc operations. */
3687 can_compare_p (enum rtx_code code, machine_mode mode,
3688 enum can_compare_purpose purpose)
3690 rtx test;
3691 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3694 enum insn_code icode;
3696 if (purpose == ccp_jump
3697 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
3698 && insn_operand_matches (icode, 0, test))
3699 return 1;
3700 if (purpose == ccp_store_flag
3701 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
3702 && insn_operand_matches (icode, 1, test))
3703 return 1;
3704 if (purpose == ccp_cmov
3705 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
3706 return 1;
3708 mode = GET_MODE_WIDER_MODE (mode).else_void ();
3709 PUT_MODE (test, mode);
3711 while (mode != VOIDmode);
3713 return 0;
3716 /* This function is called when we are going to emit a compare instruction that
3717 compares the values found in X and Y, using the rtl operator COMPARISON.
3719 If they have mode BLKmode, then SIZE specifies the size of both operands.
3721 UNSIGNEDP nonzero says that the operands are unsigned;
3722 this matters if they need to be widened (as given by METHODS).
3724 *PTEST is where the resulting comparison RTX is returned or NULL_RTX
3725 if we failed to produce one.
3727 *PMODE is the mode of the inputs (in case they are const_int).
3729 This function performs all the setup necessary so that the caller only has
3730 to emit a single comparison insn. This setup can involve doing a BLKmode
3731 comparison or emitting a library call to perform the comparison if no insn
3732 is available to handle it.
3733 The values which are passed in through pointers can be modified; the caller
3734 should perform the comparison on the modified values. Constant
3735 comparisons must have already been folded. */
3737 static void
3738 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3739 int unsignedp, enum optab_methods methods,
3740 rtx *ptest, machine_mode *pmode)
3742 machine_mode mode = *pmode;
3743 rtx libfunc, test;
3744 machine_mode cmp_mode;
3745 enum mode_class mclass;
3747 /* The other methods are not needed. */
3748 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
3749 || methods == OPTAB_LIB_WIDEN);
3751 /* If we are optimizing, force expensive constants into a register. */
3752 if (CONSTANT_P (x) && optimize
3753 && (rtx_cost (x, mode, COMPARE, 0, optimize_insn_for_speed_p ())
3754 > COSTS_N_INSNS (1)))
3755 x = force_reg (mode, x);
3757 if (CONSTANT_P (y) && optimize
3758 && (rtx_cost (y, mode, COMPARE, 1, optimize_insn_for_speed_p ())
3759 > COSTS_N_INSNS (1)))
3760 y = force_reg (mode, y);
3762 #if HAVE_cc0
3763 /* Make sure if we have a canonical comparison. The RTL
3764 documentation states that canonical comparisons are required only
3765 for targets which have cc0. */
3766 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
3767 #endif
3769 /* Don't let both operands fail to indicate the mode. */
3770 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
3771 x = force_reg (mode, x);
3772 if (mode == VOIDmode)
3773 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
3775 /* Handle all BLKmode compares. */
3777 if (mode == BLKmode)
3779 machine_mode result_mode;
3780 enum insn_code cmp_code;
3781 rtx result;
3782 rtx opalign
3783 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
3785 gcc_assert (size);
3787 /* Try to use a memory block compare insn - either cmpstr
3788 or cmpmem will do. */
3789 FOR_EACH_MODE_IN_CLASS (cmp_mode, MODE_INT)
3791 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
3792 if (cmp_code == CODE_FOR_nothing)
3793 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
3794 if (cmp_code == CODE_FOR_nothing)
3795 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
3796 if (cmp_code == CODE_FOR_nothing)
3797 continue;
3799 /* Must make sure the size fits the insn's mode. */
3800 if ((CONST_INT_P (size)
3801 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
3802 || (GET_MODE_BITSIZE (GET_MODE (size))
3803 > GET_MODE_BITSIZE (cmp_mode)))
3804 continue;
3806 result_mode = insn_data[cmp_code].operand[0].mode;
3807 result = gen_reg_rtx (result_mode);
3808 size = convert_to_mode (cmp_mode, size, 1);
3809 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
3811 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
3812 *pmode = result_mode;
3813 return;
3816 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
3817 goto fail;
3819 /* Otherwise call a library function. */
3820 result = emit_block_comp_via_libcall (XEXP (x, 0), XEXP (y, 0), size);
3822 x = result;
3823 y = const0_rtx;
3824 mode = TYPE_MODE (integer_type_node);
3825 methods = OPTAB_LIB_WIDEN;
3826 unsignedp = false;
3829 /* Don't allow operands to the compare to trap, as that can put the
3830 compare and branch in different basic blocks. */
3831 if (cfun->can_throw_non_call_exceptions)
3833 if (may_trap_p (x))
3834 x = copy_to_reg (x);
3835 if (may_trap_p (y))
3836 y = copy_to_reg (y);
3839 if (GET_MODE_CLASS (mode) == MODE_CC)
3841 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
3842 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3843 gcc_assert (icode != CODE_FOR_nothing
3844 && insn_operand_matches (icode, 0, test));
3845 *ptest = test;
3846 return;
3849 mclass = GET_MODE_CLASS (mode);
3850 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3851 FOR_EACH_MODE_FROM (cmp_mode, mode)
3853 enum insn_code icode;
3854 icode = optab_handler (cbranch_optab, cmp_mode);
3855 if (icode != CODE_FOR_nothing
3856 && insn_operand_matches (icode, 0, test))
3858 rtx_insn *last = get_last_insn ();
3859 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
3860 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
3861 if (op0 && op1
3862 && insn_operand_matches (icode, 1, op0)
3863 && insn_operand_matches (icode, 2, op1))
3865 XEXP (test, 0) = op0;
3866 XEXP (test, 1) = op1;
3867 *ptest = test;
3868 *pmode = cmp_mode;
3869 return;
3871 delete_insns_since (last);
3874 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
3875 break;
3878 if (methods != OPTAB_LIB_WIDEN)
3879 goto fail;
3881 if (!SCALAR_FLOAT_MODE_P (mode))
3883 rtx result;
3884 machine_mode ret_mode;
3886 /* Handle a libcall just for the mode we are using. */
3887 libfunc = optab_libfunc (cmp_optab, mode);
3888 gcc_assert (libfunc);
3890 /* If we want unsigned, and this mode has a distinct unsigned
3891 comparison routine, use that. */
3892 if (unsignedp)
3894 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
3895 if (ulibfunc)
3896 libfunc = ulibfunc;
3899 ret_mode = targetm.libgcc_cmp_return_mode ();
3900 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3901 ret_mode, 2, x, mode, y, mode);
3903 /* There are two kinds of comparison routines. Biased routines
3904 return 0/1/2, and unbiased routines return -1/0/1. Other parts
3905 of gcc expect that the comparison operation is equivalent
3906 to the modified comparison. For signed comparisons compare the
3907 result against 1 in the biased case, and zero in the unbiased
3908 case. For unsigned comparisons always compare against 1 after
3909 biasing the unbiased result by adding 1. This gives us a way to
3910 represent LTU.
3911 The comparisons in the fixed-point helper library are always
3912 biased. */
3913 x = result;
3914 y = const1_rtx;
3916 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
3918 if (unsignedp)
3919 x = plus_constant (ret_mode, result, 1);
3920 else
3921 y = const0_rtx;
3924 *pmode = ret_mode;
3925 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
3926 ptest, pmode);
3928 else
3929 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
3931 return;
3933 fail:
3934 *ptest = NULL_RTX;
3937 /* Before emitting an insn with code ICODE, make sure that X, which is going
3938 to be used for operand OPNUM of the insn, is converted from mode MODE to
3939 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
3940 that it is accepted by the operand predicate. Return the new value. */
3943 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
3944 machine_mode wider_mode, int unsignedp)
3946 if (mode != wider_mode)
3947 x = convert_modes (wider_mode, mode, x, unsignedp);
3949 if (!insn_operand_matches (icode, opnum, x))
3951 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
3952 if (reload_completed)
3953 return NULL_RTX;
3954 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
3955 return NULL_RTX;
3956 x = copy_to_mode_reg (op_mode, x);
3959 return x;
3962 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
3963 we can do the branch. */
3965 static void
3966 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label,
3967 profile_probability prob)
3969 machine_mode optab_mode;
3970 enum mode_class mclass;
3971 enum insn_code icode;
3972 rtx_insn *insn;
3974 mclass = GET_MODE_CLASS (mode);
3975 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
3976 icode = optab_handler (cbranch_optab, optab_mode);
3978 gcc_assert (icode != CODE_FOR_nothing);
3979 gcc_assert (insn_operand_matches (icode, 0, test));
3980 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
3981 XEXP (test, 1), label));
3982 if (prob.initialized_p ()
3983 && profile_status_for_fn (cfun) != PROFILE_ABSENT
3984 && insn
3985 && JUMP_P (insn)
3986 && any_condjump_p (insn)
3987 && !find_reg_note (insn, REG_BR_PROB, 0))
3988 add_reg_br_prob_note (insn, prob);
3991 /* Generate code to compare X with Y so that the condition codes are
3992 set and to jump to LABEL if the condition is true. If X is a
3993 constant and Y is not a constant, then the comparison is swapped to
3994 ensure that the comparison RTL has the canonical form.
3996 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
3997 need to be widened. UNSIGNEDP is also used to select the proper
3998 branch condition code.
4000 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4002 MODE is the mode of the inputs (in case they are const_int).
4004 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4005 It will be potentially converted into an unsigned variant based on
4006 UNSIGNEDP to select a proper jump instruction.
4008 PROB is the probability of jumping to LABEL. */
4010 void
4011 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4012 machine_mode mode, int unsignedp, rtx label,
4013 profile_probability prob)
4015 rtx op0 = x, op1 = y;
4016 rtx test;
4018 /* Swap operands and condition to ensure canonical RTL. */
4019 if (swap_commutative_operands_p (x, y)
4020 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4022 op0 = y, op1 = x;
4023 comparison = swap_condition (comparison);
4026 /* If OP0 is still a constant, then both X and Y must be constants
4027 or the opposite comparison is not supported. Force X into a register
4028 to create canonical RTL. */
4029 if (CONSTANT_P (op0))
4030 op0 = force_reg (mode, op0);
4032 if (unsignedp)
4033 comparison = unsigned_condition (comparison);
4035 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4036 &test, &mode);
4037 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4041 /* Emit a library call comparison between floating point X and Y.
4042 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4044 static void
4045 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4046 rtx *ptest, machine_mode *pmode)
4048 enum rtx_code swapped = swap_condition (comparison);
4049 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4050 machine_mode orig_mode = GET_MODE (x);
4051 machine_mode mode, cmp_mode;
4052 rtx true_rtx, false_rtx;
4053 rtx value, target, equiv;
4054 rtx_insn *insns;
4055 rtx libfunc = 0;
4056 bool reversed_p = false;
4057 cmp_mode = targetm.libgcc_cmp_return_mode ();
4059 FOR_EACH_MODE_FROM (mode, orig_mode)
4061 if (code_to_optab (comparison)
4062 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4063 break;
4065 if (code_to_optab (swapped)
4066 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4068 std::swap (x, y);
4069 comparison = swapped;
4070 break;
4073 if (code_to_optab (reversed)
4074 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4076 comparison = reversed;
4077 reversed_p = true;
4078 break;
4082 gcc_assert (mode != VOIDmode);
4084 if (mode != orig_mode)
4086 x = convert_to_mode (mode, x, 0);
4087 y = convert_to_mode (mode, y, 0);
4090 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4091 the RTL. The allows the RTL optimizers to delete the libcall if the
4092 condition can be determined at compile-time. */
4093 if (comparison == UNORDERED
4094 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4096 true_rtx = const_true_rtx;
4097 false_rtx = const0_rtx;
4099 else
4101 switch (comparison)
4103 case EQ:
4104 true_rtx = const0_rtx;
4105 false_rtx = const_true_rtx;
4106 break;
4108 case NE:
4109 true_rtx = const_true_rtx;
4110 false_rtx = const0_rtx;
4111 break;
4113 case GT:
4114 true_rtx = const1_rtx;
4115 false_rtx = const0_rtx;
4116 break;
4118 case GE:
4119 true_rtx = const0_rtx;
4120 false_rtx = constm1_rtx;
4121 break;
4123 case LT:
4124 true_rtx = constm1_rtx;
4125 false_rtx = const0_rtx;
4126 break;
4128 case LE:
4129 true_rtx = const0_rtx;
4130 false_rtx = const1_rtx;
4131 break;
4133 default:
4134 gcc_unreachable ();
4138 if (comparison == UNORDERED)
4140 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4141 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4142 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4143 temp, const_true_rtx, equiv);
4145 else
4147 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4148 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4149 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4150 equiv, true_rtx, false_rtx);
4153 start_sequence ();
4154 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4155 cmp_mode, 2, x, mode, y, mode);
4156 insns = get_insns ();
4157 end_sequence ();
4159 target = gen_reg_rtx (cmp_mode);
4160 emit_libcall_block (insns, target, value, equiv);
4162 if (comparison == UNORDERED
4163 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4164 || reversed_p)
4165 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4166 else
4167 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4169 *pmode = cmp_mode;
4172 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4174 void
4175 emit_indirect_jump (rtx loc)
4177 if (!targetm.have_indirect_jump ())
4178 sorry ("indirect jumps are not available on this target");
4179 else
4181 struct expand_operand ops[1];
4182 create_address_operand (&ops[0], loc);
4183 expand_jump_insn (targetm.code_for_indirect_jump, 1, ops);
4184 emit_barrier ();
4189 /* Emit a conditional move instruction if the machine supports one for that
4190 condition and machine mode.
4192 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4193 the mode to use should they be constants. If it is VOIDmode, they cannot
4194 both be constants.
4196 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4197 should be stored there. MODE is the mode to use should they be constants.
4198 If it is VOIDmode, they cannot both be constants.
4200 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4201 is not supported. */
4204 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4205 machine_mode cmode, rtx op2, rtx op3,
4206 machine_mode mode, int unsignedp)
4208 rtx comparison;
4209 rtx_insn *last;
4210 enum insn_code icode;
4211 enum rtx_code reversed;
4213 /* If the two source operands are identical, that's just a move. */
4215 if (rtx_equal_p (op2, op3))
4217 if (!target)
4218 target = gen_reg_rtx (mode);
4220 emit_move_insn (target, op3);
4221 return target;
4224 /* If one operand is constant, make it the second one. Only do this
4225 if the other operand is not constant as well. */
4227 if (swap_commutative_operands_p (op0, op1))
4229 std::swap (op0, op1);
4230 code = swap_condition (code);
4233 /* get_condition will prefer to generate LT and GT even if the old
4234 comparison was against zero, so undo that canonicalization here since
4235 comparisons against zero are cheaper. */
4236 if (code == LT && op1 == const1_rtx)
4237 code = LE, op1 = const0_rtx;
4238 else if (code == GT && op1 == constm1_rtx)
4239 code = GE, op1 = const0_rtx;
4241 if (cmode == VOIDmode)
4242 cmode = GET_MODE (op0);
4244 enum rtx_code orig_code = code;
4245 bool swapped = false;
4246 if (swap_commutative_operands_p (op2, op3)
4247 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4248 != UNKNOWN))
4250 std::swap (op2, op3);
4251 code = reversed;
4252 swapped = true;
4255 if (mode == VOIDmode)
4256 mode = GET_MODE (op2);
4258 icode = direct_optab_handler (movcc_optab, mode);
4260 if (icode == CODE_FOR_nothing)
4261 return NULL_RTX;
4263 if (!target)
4264 target = gen_reg_rtx (mode);
4266 for (int pass = 0; ; pass++)
4268 code = unsignedp ? unsigned_condition (code) : code;
4269 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4271 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4272 punt and let the caller figure out how best to deal with this
4273 situation. */
4274 if (COMPARISON_P (comparison))
4276 saved_pending_stack_adjust save;
4277 save_pending_stack_adjust (&save);
4278 last = get_last_insn ();
4279 do_pending_stack_adjust ();
4280 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4281 GET_CODE (comparison), NULL_RTX, unsignedp,
4282 OPTAB_WIDEN, &comparison, &cmode);
4283 if (comparison)
4285 struct expand_operand ops[4];
4287 create_output_operand (&ops[0], target, mode);
4288 create_fixed_operand (&ops[1], comparison);
4289 create_input_operand (&ops[2], op2, mode);
4290 create_input_operand (&ops[3], op3, mode);
4291 if (maybe_expand_insn (icode, 4, ops))
4293 if (ops[0].value != target)
4294 convert_move (target, ops[0].value, false);
4295 return target;
4298 delete_insns_since (last);
4299 restore_pending_stack_adjust (&save);
4302 if (pass == 1)
4303 return NULL_RTX;
4305 /* If the preferred op2/op3 order is not usable, retry with other
4306 operand order, perhaps it will expand successfully. */
4307 if (swapped)
4308 code = orig_code;
4309 else if ((reversed = reversed_comparison_code_parts (orig_code, op0, op1,
4310 NULL))
4311 != UNKNOWN)
4312 code = reversed;
4313 else
4314 return NULL_RTX;
4315 std::swap (op2, op3);
4320 /* Emit a conditional negate or bitwise complement using the
4321 negcc or notcc optabs if available. Return NULL_RTX if such operations
4322 are not available. Otherwise return the RTX holding the result.
4323 TARGET is the desired destination of the result. COMP is the comparison
4324 on which to negate. If COND is true move into TARGET the negation
4325 or bitwise complement of OP1. Otherwise move OP2 into TARGET.
4326 CODE is either NEG or NOT. MODE is the machine mode in which the
4327 operation is performed. */
4330 emit_conditional_neg_or_complement (rtx target, rtx_code code,
4331 machine_mode mode, rtx cond, rtx op1,
4332 rtx op2)
4334 optab op = unknown_optab;
4335 if (code == NEG)
4336 op = negcc_optab;
4337 else if (code == NOT)
4338 op = notcc_optab;
4339 else
4340 gcc_unreachable ();
4342 insn_code icode = direct_optab_handler (op, mode);
4344 if (icode == CODE_FOR_nothing)
4345 return NULL_RTX;
4347 if (!target)
4348 target = gen_reg_rtx (mode);
4350 rtx_insn *last = get_last_insn ();
4351 struct expand_operand ops[4];
4353 create_output_operand (&ops[0], target, mode);
4354 create_fixed_operand (&ops[1], cond);
4355 create_input_operand (&ops[2], op1, mode);
4356 create_input_operand (&ops[3], op2, mode);
4358 if (maybe_expand_insn (icode, 4, ops))
4360 if (ops[0].value != target)
4361 convert_move (target, ops[0].value, false);
4363 return target;
4365 delete_insns_since (last);
4366 return NULL_RTX;
4369 /* Emit a conditional addition instruction if the machine supports one for that
4370 condition and machine mode.
4372 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4373 the mode to use should they be constants. If it is VOIDmode, they cannot
4374 both be constants.
4376 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4377 should be stored there. MODE is the mode to use should they be constants.
4378 If it is VOIDmode, they cannot both be constants.
4380 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4381 is not supported. */
4384 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4385 machine_mode cmode, rtx op2, rtx op3,
4386 machine_mode mode, int unsignedp)
4388 rtx comparison;
4389 rtx_insn *last;
4390 enum insn_code icode;
4392 /* If one operand is constant, make it the second one. Only do this
4393 if the other operand is not constant as well. */
4395 if (swap_commutative_operands_p (op0, op1))
4397 std::swap (op0, op1);
4398 code = swap_condition (code);
4401 /* get_condition will prefer to generate LT and GT even if the old
4402 comparison was against zero, so undo that canonicalization here since
4403 comparisons against zero are cheaper. */
4404 if (code == LT && op1 == const1_rtx)
4405 code = LE, op1 = const0_rtx;
4406 else if (code == GT && op1 == constm1_rtx)
4407 code = GE, op1 = const0_rtx;
4409 if (cmode == VOIDmode)
4410 cmode = GET_MODE (op0);
4412 if (mode == VOIDmode)
4413 mode = GET_MODE (op2);
4415 icode = optab_handler (addcc_optab, mode);
4417 if (icode == CODE_FOR_nothing)
4418 return 0;
4420 if (!target)
4421 target = gen_reg_rtx (mode);
4423 code = unsignedp ? unsigned_condition (code) : code;
4424 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4426 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4427 return NULL and let the caller figure out how best to deal with this
4428 situation. */
4429 if (!COMPARISON_P (comparison))
4430 return NULL_RTX;
4432 do_pending_stack_adjust ();
4433 last = get_last_insn ();
4434 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4435 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4436 &comparison, &cmode);
4437 if (comparison)
4439 struct expand_operand ops[4];
4441 create_output_operand (&ops[0], target, mode);
4442 create_fixed_operand (&ops[1], comparison);
4443 create_input_operand (&ops[2], op2, mode);
4444 create_input_operand (&ops[3], op3, mode);
4445 if (maybe_expand_insn (icode, 4, ops))
4447 if (ops[0].value != target)
4448 convert_move (target, ops[0].value, false);
4449 return target;
4452 delete_insns_since (last);
4453 return NULL_RTX;
4456 /* These functions attempt to generate an insn body, rather than
4457 emitting the insn, but if the gen function already emits them, we
4458 make no attempt to turn them back into naked patterns. */
4460 /* Generate and return an insn body to add Y to X. */
4462 rtx_insn *
4463 gen_add2_insn (rtx x, rtx y)
4465 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4467 gcc_assert (insn_operand_matches (icode, 0, x));
4468 gcc_assert (insn_operand_matches (icode, 1, x));
4469 gcc_assert (insn_operand_matches (icode, 2, y));
4471 return GEN_FCN (icode) (x, x, y);
4474 /* Generate and return an insn body to add r1 and c,
4475 storing the result in r0. */
4477 rtx_insn *
4478 gen_add3_insn (rtx r0, rtx r1, rtx c)
4480 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4482 if (icode == CODE_FOR_nothing
4483 || !insn_operand_matches (icode, 0, r0)
4484 || !insn_operand_matches (icode, 1, r1)
4485 || !insn_operand_matches (icode, 2, c))
4486 return NULL;
4488 return GEN_FCN (icode) (r0, r1, c);
4492 have_add2_insn (rtx x, rtx y)
4494 enum insn_code icode;
4496 gcc_assert (GET_MODE (x) != VOIDmode);
4498 icode = optab_handler (add_optab, GET_MODE (x));
4500 if (icode == CODE_FOR_nothing)
4501 return 0;
4503 if (!insn_operand_matches (icode, 0, x)
4504 || !insn_operand_matches (icode, 1, x)
4505 || !insn_operand_matches (icode, 2, y))
4506 return 0;
4508 return 1;
4511 /* Generate and return an insn body to add Y to X. */
4513 rtx_insn *
4514 gen_addptr3_insn (rtx x, rtx y, rtx z)
4516 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4518 gcc_assert (insn_operand_matches (icode, 0, x));
4519 gcc_assert (insn_operand_matches (icode, 1, y));
4520 gcc_assert (insn_operand_matches (icode, 2, z));
4522 return GEN_FCN (icode) (x, y, z);
4525 /* Return true if the target implements an addptr pattern and X, Y,
4526 and Z are valid for the pattern predicates. */
4529 have_addptr3_insn (rtx x, rtx y, rtx z)
4531 enum insn_code icode;
4533 gcc_assert (GET_MODE (x) != VOIDmode);
4535 icode = optab_handler (addptr3_optab, GET_MODE (x));
4537 if (icode == CODE_FOR_nothing)
4538 return 0;
4540 if (!insn_operand_matches (icode, 0, x)
4541 || !insn_operand_matches (icode, 1, y)
4542 || !insn_operand_matches (icode, 2, z))
4543 return 0;
4545 return 1;
4548 /* Generate and return an insn body to subtract Y from X. */
4550 rtx_insn *
4551 gen_sub2_insn (rtx x, rtx y)
4553 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4555 gcc_assert (insn_operand_matches (icode, 0, x));
4556 gcc_assert (insn_operand_matches (icode, 1, x));
4557 gcc_assert (insn_operand_matches (icode, 2, y));
4559 return GEN_FCN (icode) (x, x, y);
4562 /* Generate and return an insn body to subtract r1 and c,
4563 storing the result in r0. */
4565 rtx_insn *
4566 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4568 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4570 if (icode == CODE_FOR_nothing
4571 || !insn_operand_matches (icode, 0, r0)
4572 || !insn_operand_matches (icode, 1, r1)
4573 || !insn_operand_matches (icode, 2, c))
4574 return NULL;
4576 return GEN_FCN (icode) (r0, r1, c);
4580 have_sub2_insn (rtx x, rtx y)
4582 enum insn_code icode;
4584 gcc_assert (GET_MODE (x) != VOIDmode);
4586 icode = optab_handler (sub_optab, GET_MODE (x));
4588 if (icode == CODE_FOR_nothing)
4589 return 0;
4591 if (!insn_operand_matches (icode, 0, x)
4592 || !insn_operand_matches (icode, 1, x)
4593 || !insn_operand_matches (icode, 2, y))
4594 return 0;
4596 return 1;
4599 /* Generate the body of an insn to extend Y (with mode MFROM)
4600 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4602 rtx_insn *
4603 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4604 machine_mode mfrom, int unsignedp)
4606 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4607 return GEN_FCN (icode) (x, y);
4610 /* Generate code to convert FROM to floating point
4611 and store in TO. FROM must be fixed point and not VOIDmode.
4612 UNSIGNEDP nonzero means regard FROM as unsigned.
4613 Normally this is done by correcting the final value
4614 if it is negative. */
4616 void
4617 expand_float (rtx to, rtx from, int unsignedp)
4619 enum insn_code icode;
4620 rtx target = to;
4621 machine_mode fmode, imode;
4622 bool can_do_signed = false;
4624 /* Crash now, because we won't be able to decide which mode to use. */
4625 gcc_assert (GET_MODE (from) != VOIDmode);
4627 /* Look for an insn to do the conversion. Do it in the specified
4628 modes if possible; otherwise convert either input, output or both to
4629 wider mode. If the integer mode is wider than the mode of FROM,
4630 we can do the conversion signed even if the input is unsigned. */
4632 FOR_EACH_MODE_FROM (fmode, GET_MODE (to))
4633 FOR_EACH_MODE_FROM (imode, GET_MODE (from))
4635 int doing_unsigned = unsignedp;
4637 if (fmode != GET_MODE (to)
4638 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
4639 continue;
4641 icode = can_float_p (fmode, imode, unsignedp);
4642 if (icode == CODE_FOR_nothing && unsignedp)
4644 enum insn_code scode = can_float_p (fmode, imode, 0);
4645 if (scode != CODE_FOR_nothing)
4646 can_do_signed = true;
4647 if (imode != GET_MODE (from))
4648 icode = scode, doing_unsigned = 0;
4651 if (icode != CODE_FOR_nothing)
4653 if (imode != GET_MODE (from))
4654 from = convert_to_mode (imode, from, unsignedp);
4656 if (fmode != GET_MODE (to))
4657 target = gen_reg_rtx (fmode);
4659 emit_unop_insn (icode, target, from,
4660 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4662 if (target != to)
4663 convert_move (to, target, 0);
4664 return;
4668 /* Unsigned integer, and no way to convert directly. Convert as signed,
4669 then unconditionally adjust the result. */
4670 if (unsignedp && can_do_signed)
4672 rtx_code_label *label = gen_label_rtx ();
4673 rtx temp;
4674 REAL_VALUE_TYPE offset;
4676 /* Look for a usable floating mode FMODE wider than the source and at
4677 least as wide as the target. Using FMODE will avoid rounding woes
4678 with unsigned values greater than the signed maximum value. */
4680 FOR_EACH_MODE_FROM (fmode, GET_MODE (to))
4681 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4682 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4683 break;
4685 if (fmode == VOIDmode)
4687 /* There is no such mode. Pretend the target is wide enough. */
4688 fmode = GET_MODE (to);
4690 /* Avoid double-rounding when TO is narrower than FROM. */
4691 if ((significand_size (fmode) + 1)
4692 < GET_MODE_PRECISION (GET_MODE (from)))
4694 rtx temp1;
4695 rtx_code_label *neglabel = gen_label_rtx ();
4697 /* Don't use TARGET if it isn't a register, is a hard register,
4698 or is the wrong mode. */
4699 if (!REG_P (target)
4700 || REGNO (target) < FIRST_PSEUDO_REGISTER
4701 || GET_MODE (target) != fmode)
4702 target = gen_reg_rtx (fmode);
4704 imode = GET_MODE (from);
4705 do_pending_stack_adjust ();
4707 /* Test whether the sign bit is set. */
4708 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4709 0, neglabel);
4711 /* The sign bit is not set. Convert as signed. */
4712 expand_float (target, from, 0);
4713 emit_jump_insn (targetm.gen_jump (label));
4714 emit_barrier ();
4716 /* The sign bit is set.
4717 Convert to a usable (positive signed) value by shifting right
4718 one bit, while remembering if a nonzero bit was shifted
4719 out; i.e., compute (from & 1) | (from >> 1). */
4721 emit_label (neglabel);
4722 temp = expand_binop (imode, and_optab, from, const1_rtx,
4723 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4724 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
4725 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4726 OPTAB_LIB_WIDEN);
4727 expand_float (target, temp, 0);
4729 /* Multiply by 2 to undo the shift above. */
4730 temp = expand_binop (fmode, add_optab, target, target,
4731 target, 0, OPTAB_LIB_WIDEN);
4732 if (temp != target)
4733 emit_move_insn (target, temp);
4735 do_pending_stack_adjust ();
4736 emit_label (label);
4737 goto done;
4741 /* If we are about to do some arithmetic to correct for an
4742 unsigned operand, do it in a pseudo-register. */
4744 if (GET_MODE (to) != fmode
4745 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
4746 target = gen_reg_rtx (fmode);
4748 /* Convert as signed integer to floating. */
4749 expand_float (target, from, 0);
4751 /* If FROM is negative (and therefore TO is negative),
4752 correct its value by 2**bitwidth. */
4754 do_pending_stack_adjust ();
4755 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
4756 0, label);
4759 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
4760 temp = expand_binop (fmode, add_optab, target,
4761 const_double_from_real_value (offset, fmode),
4762 target, 0, OPTAB_LIB_WIDEN);
4763 if (temp != target)
4764 emit_move_insn (target, temp);
4766 do_pending_stack_adjust ();
4767 emit_label (label);
4768 goto done;
4771 /* No hardware instruction available; call a library routine. */
4773 rtx libfunc;
4774 rtx_insn *insns;
4775 rtx value;
4776 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
4778 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
4779 from = convert_to_mode (SImode, from, unsignedp);
4781 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
4782 gcc_assert (libfunc);
4784 start_sequence ();
4786 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4787 GET_MODE (to), 1, from,
4788 GET_MODE (from));
4789 insns = get_insns ();
4790 end_sequence ();
4792 emit_libcall_block (insns, target, value,
4793 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
4794 GET_MODE (to), from));
4797 done:
4799 /* Copy result to requested destination
4800 if we have been computing in a temp location. */
4802 if (target != to)
4804 if (GET_MODE (target) == GET_MODE (to))
4805 emit_move_insn (to, target);
4806 else
4807 convert_move (to, target, 0);
4811 /* Generate code to convert FROM to fixed point and store in TO. FROM
4812 must be floating point. */
4814 void
4815 expand_fix (rtx to, rtx from, int unsignedp)
4817 enum insn_code icode;
4818 rtx target = to;
4819 machine_mode fmode, imode;
4820 bool must_trunc = false;
4822 /* We first try to find a pair of modes, one real and one integer, at
4823 least as wide as FROM and TO, respectively, in which we can open-code
4824 this conversion. If the integer mode is wider than the mode of TO,
4825 we can do the conversion either signed or unsigned. */
4827 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
4828 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
4830 int doing_unsigned = unsignedp;
4832 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
4833 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
4834 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
4836 if (icode != CODE_FOR_nothing)
4838 rtx_insn *last = get_last_insn ();
4839 if (fmode != GET_MODE (from))
4840 from = convert_to_mode (fmode, from, 0);
4842 if (must_trunc)
4844 rtx temp = gen_reg_rtx (GET_MODE (from));
4845 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
4846 temp, 0);
4849 if (imode != GET_MODE (to))
4850 target = gen_reg_rtx (imode);
4852 if (maybe_emit_unop_insn (icode, target, from,
4853 doing_unsigned ? UNSIGNED_FIX : FIX))
4855 if (target != to)
4856 convert_move (to, target, unsignedp);
4857 return;
4859 delete_insns_since (last);
4863 /* For an unsigned conversion, there is one more way to do it.
4864 If we have a signed conversion, we generate code that compares
4865 the real value to the largest representable positive number. If if
4866 is smaller, the conversion is done normally. Otherwise, subtract
4867 one plus the highest signed number, convert, and add it back.
4869 We only need to check all real modes, since we know we didn't find
4870 anything with a wider integer mode.
4872 This code used to extend FP value into mode wider than the destination.
4873 This is needed for decimal float modes which cannot accurately
4874 represent one plus the highest signed number of the same size, but
4875 not for binary modes. Consider, for instance conversion from SFmode
4876 into DImode.
4878 The hot path through the code is dealing with inputs smaller than 2^63
4879 and doing just the conversion, so there is no bits to lose.
4881 In the other path we know the value is positive in the range 2^63..2^64-1
4882 inclusive. (as for other input overflow happens and result is undefined)
4883 So we know that the most important bit set in mantissa corresponds to
4884 2^63. The subtraction of 2^63 should not generate any rounding as it
4885 simply clears out that bit. The rest is trivial. */
4887 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
4888 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
4889 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
4890 && (!DECIMAL_FLOAT_MODE_P (fmode)
4891 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
4893 int bitsize;
4894 REAL_VALUE_TYPE offset;
4895 rtx limit;
4896 rtx_code_label *lab1, *lab2;
4897 rtx_insn *insn;
4899 bitsize = GET_MODE_PRECISION (GET_MODE (to));
4900 real_2expN (&offset, bitsize - 1, fmode);
4901 limit = const_double_from_real_value (offset, fmode);
4902 lab1 = gen_label_rtx ();
4903 lab2 = gen_label_rtx ();
4905 if (fmode != GET_MODE (from))
4906 from = convert_to_mode (fmode, from, 0);
4908 /* See if we need to do the subtraction. */
4909 do_pending_stack_adjust ();
4910 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
4911 0, lab1);
4913 /* If not, do the signed "fix" and branch around fixup code. */
4914 expand_fix (to, from, 0);
4915 emit_jump_insn (targetm.gen_jump (lab2));
4916 emit_barrier ();
4918 /* Otherwise, subtract 2**(N-1), convert to signed number,
4919 then add 2**(N-1). Do the addition using XOR since this
4920 will often generate better code. */
4921 emit_label (lab1);
4922 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
4923 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4924 expand_fix (to, target, 0);
4925 target = expand_binop (GET_MODE (to), xor_optab, to,
4926 gen_int_mode
4927 (HOST_WIDE_INT_1 << (bitsize - 1),
4928 GET_MODE (to)),
4929 to, 1, OPTAB_LIB_WIDEN);
4931 if (target != to)
4932 emit_move_insn (to, target);
4934 emit_label (lab2);
4936 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
4938 /* Make a place for a REG_NOTE and add it. */
4939 insn = emit_move_insn (to, to);
4940 set_dst_reg_note (insn, REG_EQUAL,
4941 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
4942 copy_rtx (from)),
4943 to);
4946 return;
4949 /* We can't do it with an insn, so use a library call. But first ensure
4950 that the mode of TO is at least as wide as SImode, since those are the
4951 only library calls we know about. */
4953 if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
4955 target = gen_reg_rtx (SImode);
4957 expand_fix (target, from, unsignedp);
4959 else
4961 rtx_insn *insns;
4962 rtx value;
4963 rtx libfunc;
4965 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
4966 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
4967 gcc_assert (libfunc);
4969 start_sequence ();
4971 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4972 GET_MODE (to), 1, from,
4973 GET_MODE (from));
4974 insns = get_insns ();
4975 end_sequence ();
4977 emit_libcall_block (insns, target, value,
4978 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
4979 GET_MODE (to), from));
4982 if (target != to)
4984 if (GET_MODE (to) == GET_MODE (target))
4985 emit_move_insn (to, target);
4986 else
4987 convert_move (to, target, 0);
4992 /* Promote integer arguments for a libcall if necessary.
4993 emit_library_call_value cannot do the promotion because it does not
4994 know if it should do a signed or unsigned promotion. This is because
4995 there are no tree types defined for libcalls. */
4997 static rtx
4998 prepare_libcall_arg (rtx arg, int uintp)
5000 machine_mode mode = GET_MODE (arg);
5001 machine_mode arg_mode;
5002 if (SCALAR_INT_MODE_P (mode))
5004 /* If we need to promote the integer function argument we need to do
5005 it here instead of inside emit_library_call_value because in
5006 emit_library_call_value we don't know if we should do a signed or
5007 unsigned promotion. */
5009 int unsigned_p = 0;
5010 arg_mode = promote_function_mode (NULL_TREE, mode,
5011 &unsigned_p, NULL_TREE, 0);
5012 if (arg_mode != mode)
5013 return convert_to_mode (arg_mode, arg, uintp);
5015 return arg;
5018 /* Generate code to convert FROM or TO a fixed-point.
5019 If UINTP is true, either TO or FROM is an unsigned integer.
5020 If SATP is true, we need to saturate the result. */
5022 void
5023 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5025 machine_mode to_mode = GET_MODE (to);
5026 machine_mode from_mode = GET_MODE (from);
5027 convert_optab tab;
5028 enum rtx_code this_code;
5029 enum insn_code code;
5030 rtx_insn *insns;
5031 rtx value;
5032 rtx libfunc;
5034 if (to_mode == from_mode)
5036 emit_move_insn (to, from);
5037 return;
5040 if (uintp)
5042 tab = satp ? satfractuns_optab : fractuns_optab;
5043 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5045 else
5047 tab = satp ? satfract_optab : fract_optab;
5048 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5050 code = convert_optab_handler (tab, to_mode, from_mode);
5051 if (code != CODE_FOR_nothing)
5053 emit_unop_insn (code, to, from, this_code);
5054 return;
5057 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5058 gcc_assert (libfunc);
5060 from = prepare_libcall_arg (from, uintp);
5061 from_mode = GET_MODE (from);
5063 start_sequence ();
5064 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5065 1, from, from_mode);
5066 insns = get_insns ();
5067 end_sequence ();
5069 emit_libcall_block (insns, to, value,
5070 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5073 /* Generate code to convert FROM to fixed point and store in TO. FROM
5074 must be floating point, TO must be signed. Use the conversion optab
5075 TAB to do the conversion. */
5077 bool
5078 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5080 enum insn_code icode;
5081 rtx target = to;
5082 machine_mode fmode, imode;
5084 /* We first try to find a pair of modes, one real and one integer, at
5085 least as wide as FROM and TO, respectively, in which we can open-code
5086 this conversion. If the integer mode is wider than the mode of TO,
5087 we can do the conversion either signed or unsigned. */
5089 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
5090 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
5092 icode = convert_optab_handler (tab, imode, fmode);
5093 if (icode != CODE_FOR_nothing)
5095 rtx_insn *last = get_last_insn ();
5096 if (fmode != GET_MODE (from))
5097 from = convert_to_mode (fmode, from, 0);
5099 if (imode != GET_MODE (to))
5100 target = gen_reg_rtx (imode);
5102 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5104 delete_insns_since (last);
5105 continue;
5107 if (target != to)
5108 convert_move (to, target, 0);
5109 return true;
5113 return false;
5116 /* Report whether we have an instruction to perform the operation
5117 specified by CODE on operands of mode MODE. */
5119 have_insn_for (enum rtx_code code, machine_mode mode)
5121 return (code_to_optab (code)
5122 && (optab_handler (code_to_optab (code), mode)
5123 != CODE_FOR_nothing));
5126 /* Print information about the current contents of the optabs on
5127 STDERR. */
5129 DEBUG_FUNCTION void
5130 debug_optab_libfuncs (void)
5132 int i, j, k;
5134 /* Dump the arithmetic optabs. */
5135 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
5136 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5138 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
5139 if (l)
5141 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5142 fprintf (stderr, "%s\t%s:\t%s\n",
5143 GET_RTX_NAME (optab_to_code ((optab) i)),
5144 GET_MODE_NAME (j),
5145 XSTR (l, 0));
5149 /* Dump the conversion optabs. */
5150 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
5151 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5152 for (k = 0; k < NUM_MACHINE_MODES; ++k)
5154 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
5155 (machine_mode) k);
5156 if (l)
5158 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5159 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
5160 GET_RTX_NAME (optab_to_code ((optab) i)),
5161 GET_MODE_NAME (j),
5162 GET_MODE_NAME (k),
5163 XSTR (l, 0));
5168 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5169 CODE. Return 0 on failure. */
5171 rtx_insn *
5172 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
5174 machine_mode mode = GET_MODE (op1);
5175 enum insn_code icode;
5176 rtx_insn *insn;
5177 rtx trap_rtx;
5179 if (mode == VOIDmode)
5180 return 0;
5182 icode = optab_handler (ctrap_optab, mode);
5183 if (icode == CODE_FOR_nothing)
5184 return 0;
5186 /* Some targets only accept a zero trap code. */
5187 if (!insn_operand_matches (icode, 3, tcode))
5188 return 0;
5190 do_pending_stack_adjust ();
5191 start_sequence ();
5192 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
5193 &trap_rtx, &mode);
5194 if (!trap_rtx)
5195 insn = NULL;
5196 else
5197 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
5198 tcode);
5200 /* If that failed, then give up. */
5201 if (insn == 0)
5203 end_sequence ();
5204 return 0;
5207 emit_insn (insn);
5208 insn = get_insns ();
5209 end_sequence ();
5210 return insn;
5213 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
5214 or unsigned operation code. */
5216 enum rtx_code
5217 get_rtx_code (enum tree_code tcode, bool unsignedp)
5219 enum rtx_code code;
5220 switch (tcode)
5222 case EQ_EXPR:
5223 code = EQ;
5224 break;
5225 case NE_EXPR:
5226 code = NE;
5227 break;
5228 case LT_EXPR:
5229 code = unsignedp ? LTU : LT;
5230 break;
5231 case LE_EXPR:
5232 code = unsignedp ? LEU : LE;
5233 break;
5234 case GT_EXPR:
5235 code = unsignedp ? GTU : GT;
5236 break;
5237 case GE_EXPR:
5238 code = unsignedp ? GEU : GE;
5239 break;
5241 case UNORDERED_EXPR:
5242 code = UNORDERED;
5243 break;
5244 case ORDERED_EXPR:
5245 code = ORDERED;
5246 break;
5247 case UNLT_EXPR:
5248 code = UNLT;
5249 break;
5250 case UNLE_EXPR:
5251 code = UNLE;
5252 break;
5253 case UNGT_EXPR:
5254 code = UNGT;
5255 break;
5256 case UNGE_EXPR:
5257 code = UNGE;
5258 break;
5259 case UNEQ_EXPR:
5260 code = UNEQ;
5261 break;
5262 case LTGT_EXPR:
5263 code = LTGT;
5264 break;
5266 case BIT_AND_EXPR:
5267 code = AND;
5268 break;
5270 case BIT_IOR_EXPR:
5271 code = IOR;
5272 break;
5274 default:
5275 gcc_unreachable ();
5277 return code;
5280 /* Return a comparison rtx of mode CMP_MODE for COND. Use UNSIGNEDP to
5281 select signed or unsigned operators. OPNO holds the index of the
5282 first comparison operand for insn ICODE. Do not generate the
5283 compare instruction itself. */
5285 static rtx
5286 vector_compare_rtx (machine_mode cmp_mode, enum tree_code tcode,
5287 tree t_op0, tree t_op1, bool unsignedp,
5288 enum insn_code icode, unsigned int opno)
5290 struct expand_operand ops[2];
5291 rtx rtx_op0, rtx_op1;
5292 machine_mode m0, m1;
5293 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
5295 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
5297 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
5298 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
5299 cases, use the original mode. */
5300 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
5301 EXPAND_STACK_PARM);
5302 m0 = GET_MODE (rtx_op0);
5303 if (m0 == VOIDmode)
5304 m0 = TYPE_MODE (TREE_TYPE (t_op0));
5306 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
5307 EXPAND_STACK_PARM);
5308 m1 = GET_MODE (rtx_op1);
5309 if (m1 == VOIDmode)
5310 m1 = TYPE_MODE (TREE_TYPE (t_op1));
5312 create_input_operand (&ops[0], rtx_op0, m0);
5313 create_input_operand (&ops[1], rtx_op1, m1);
5314 if (!maybe_legitimize_operands (icode, opno, 2, ops))
5315 gcc_unreachable ();
5316 return gen_rtx_fmt_ee (rcode, cmp_mode, ops[0].value, ops[1].value);
5319 /* Checks if vec_perm mask SEL is a constant equivalent to a shift of the first
5320 vec_perm operand, assuming the second operand is a constant vector of zeroes.
5321 Return the shift distance in bits if so, or NULL_RTX if the vec_perm is not a
5322 shift. */
5323 static rtx
5324 shift_amt_for_vec_perm_mask (rtx sel)
5326 unsigned int i, first, nelt = GET_MODE_NUNITS (GET_MODE (sel));
5327 unsigned int bitsize = GET_MODE_UNIT_BITSIZE (GET_MODE (sel));
5329 if (GET_CODE (sel) != CONST_VECTOR)
5330 return NULL_RTX;
5332 first = INTVAL (CONST_VECTOR_ELT (sel, 0));
5333 if (first >= nelt)
5334 return NULL_RTX;
5335 for (i = 1; i < nelt; i++)
5337 int idx = INTVAL (CONST_VECTOR_ELT (sel, i));
5338 unsigned int expected = i + first;
5339 /* Indices into the second vector are all equivalent. */
5340 if (idx < 0 || (MIN (nelt, (unsigned) idx) != MIN (nelt, expected)))
5341 return NULL_RTX;
5344 return GEN_INT (first * bitsize);
5347 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
5349 static rtx
5350 expand_vec_perm_1 (enum insn_code icode, rtx target,
5351 rtx v0, rtx v1, rtx sel)
5353 machine_mode tmode = GET_MODE (target);
5354 machine_mode smode = GET_MODE (sel);
5355 struct expand_operand ops[4];
5357 create_output_operand (&ops[0], target, tmode);
5358 create_input_operand (&ops[3], sel, smode);
5360 /* Make an effort to preserve v0 == v1. The target expander is able to
5361 rely on this to determine if we're permuting a single input operand. */
5362 if (rtx_equal_p (v0, v1))
5364 if (!insn_operand_matches (icode, 1, v0))
5365 v0 = force_reg (tmode, v0);
5366 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
5367 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
5369 create_fixed_operand (&ops[1], v0);
5370 create_fixed_operand (&ops[2], v0);
5372 else
5374 create_input_operand (&ops[1], v0, tmode);
5375 create_input_operand (&ops[2], v1, tmode);
5378 if (maybe_expand_insn (icode, 4, ops))
5379 return ops[0].value;
5380 return NULL_RTX;
5383 /* Generate instructions for vec_perm optab given its mode
5384 and three operands. */
5387 expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
5389 enum insn_code icode;
5390 machine_mode qimode;
5391 unsigned int i, w, e, u;
5392 rtx tmp, sel_qi = NULL;
5393 rtvec vec;
5395 if (!target || GET_MODE (target) != mode)
5396 target = gen_reg_rtx (mode);
5398 w = GET_MODE_SIZE (mode);
5399 e = GET_MODE_NUNITS (mode);
5400 u = GET_MODE_UNIT_SIZE (mode);
5402 /* Set QIMODE to a different vector mode with byte elements.
5403 If no such mode, or if MODE already has byte elements, use VOIDmode. */
5404 qimode = VOIDmode;
5405 if (GET_MODE_INNER (mode) != QImode)
5407 qimode = mode_for_vector (QImode, w);
5408 if (!VECTOR_MODE_P (qimode))
5409 qimode = VOIDmode;
5412 /* If the input is a constant, expand it specially. */
5413 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
5414 if (GET_CODE (sel) == CONST_VECTOR)
5416 /* See if this can be handled with a vec_shr. We only do this if the
5417 second vector is all zeroes. */
5418 enum insn_code shift_code = optab_handler (vec_shr_optab, mode);
5419 enum insn_code shift_code_qi = ((qimode != VOIDmode && qimode != mode)
5420 ? optab_handler (vec_shr_optab, qimode)
5421 : CODE_FOR_nothing);
5422 rtx shift_amt = NULL_RTX;
5423 if (v1 == CONST0_RTX (GET_MODE (v1))
5424 && (shift_code != CODE_FOR_nothing
5425 || shift_code_qi != CODE_FOR_nothing))
5427 shift_amt = shift_amt_for_vec_perm_mask (sel);
5428 if (shift_amt)
5430 struct expand_operand ops[3];
5431 if (shift_code != CODE_FOR_nothing)
5433 create_output_operand (&ops[0], target, mode);
5434 create_input_operand (&ops[1], v0, mode);
5435 create_convert_operand_from_type (&ops[2], shift_amt,
5436 sizetype);
5437 if (maybe_expand_insn (shift_code, 3, ops))
5438 return ops[0].value;
5440 if (shift_code_qi != CODE_FOR_nothing)
5442 tmp = gen_reg_rtx (qimode);
5443 create_output_operand (&ops[0], tmp, qimode);
5444 create_input_operand (&ops[1], gen_lowpart (qimode, v0),
5445 qimode);
5446 create_convert_operand_from_type (&ops[2], shift_amt,
5447 sizetype);
5448 if (maybe_expand_insn (shift_code_qi, 3, ops))
5449 return gen_lowpart (mode, ops[0].value);
5454 icode = direct_optab_handler (vec_perm_const_optab, mode);
5455 if (icode != CODE_FOR_nothing)
5457 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
5458 if (tmp)
5459 return tmp;
5462 /* Fall back to a constant byte-based permutation. */
5463 if (qimode != VOIDmode)
5465 vec = rtvec_alloc (w);
5466 for (i = 0; i < e; ++i)
5468 unsigned int j, this_e;
5470 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
5471 this_e &= 2 * e - 1;
5472 this_e *= u;
5474 for (j = 0; j < u; ++j)
5475 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
5477 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
5479 icode = direct_optab_handler (vec_perm_const_optab, qimode);
5480 if (icode != CODE_FOR_nothing)
5482 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
5483 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
5484 gen_lowpart (qimode, v1), sel_qi);
5485 if (tmp)
5486 return gen_lowpart (mode, tmp);
5491 /* Otherwise expand as a fully variable permuation. */
5492 icode = direct_optab_handler (vec_perm_optab, mode);
5493 if (icode != CODE_FOR_nothing)
5495 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
5496 if (tmp)
5497 return tmp;
5500 /* As a special case to aid several targets, lower the element-based
5501 permutation to a byte-based permutation and try again. */
5502 if (qimode == VOIDmode)
5503 return NULL_RTX;
5504 icode = direct_optab_handler (vec_perm_optab, qimode);
5505 if (icode == CODE_FOR_nothing)
5506 return NULL_RTX;
5508 if (sel_qi == NULL)
5510 /* Multiply each element by its byte size. */
5511 machine_mode selmode = GET_MODE (sel);
5512 if (u == 2)
5513 sel = expand_simple_binop (selmode, PLUS, sel, sel,
5514 NULL, 0, OPTAB_DIRECT);
5515 else
5516 sel = expand_simple_binop (selmode, ASHIFT, sel,
5517 GEN_INT (exact_log2 (u)),
5518 NULL, 0, OPTAB_DIRECT);
5519 gcc_assert (sel != NULL);
5521 /* Broadcast the low byte each element into each of its bytes. */
5522 vec = rtvec_alloc (w);
5523 for (i = 0; i < w; ++i)
5525 int this_e = i / u * u;
5526 if (BYTES_BIG_ENDIAN)
5527 this_e += u - 1;
5528 RTVEC_ELT (vec, i) = GEN_INT (this_e);
5530 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
5531 sel = gen_lowpart (qimode, sel);
5532 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
5533 gcc_assert (sel != NULL);
5535 /* Add the byte offset to each byte element. */
5536 /* Note that the definition of the indicies here is memory ordering,
5537 so there should be no difference between big and little endian. */
5538 vec = rtvec_alloc (w);
5539 for (i = 0; i < w; ++i)
5540 RTVEC_ELT (vec, i) = GEN_INT (i % u);
5541 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
5542 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
5543 sel, 0, OPTAB_DIRECT);
5544 gcc_assert (sel_qi != NULL);
5547 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
5548 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
5549 gen_lowpart (qimode, v1), sel_qi);
5550 if (tmp)
5551 tmp = gen_lowpart (mode, tmp);
5552 return tmp;
5555 /* Generate insns for a VEC_COND_EXPR with mask, given its TYPE and its
5556 three operands. */
5559 expand_vec_cond_mask_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
5560 rtx target)
5562 struct expand_operand ops[4];
5563 machine_mode mode = TYPE_MODE (vec_cond_type);
5564 machine_mode mask_mode = TYPE_MODE (TREE_TYPE (op0));
5565 enum insn_code icode = get_vcond_mask_icode (mode, mask_mode);
5566 rtx mask, rtx_op1, rtx_op2;
5568 if (icode == CODE_FOR_nothing)
5569 return 0;
5571 mask = expand_normal (op0);
5572 rtx_op1 = expand_normal (op1);
5573 rtx_op2 = expand_normal (op2);
5575 mask = force_reg (mask_mode, mask);
5576 rtx_op1 = force_reg (GET_MODE (rtx_op1), rtx_op1);
5578 create_output_operand (&ops[0], target, mode);
5579 create_input_operand (&ops[1], rtx_op1, mode);
5580 create_input_operand (&ops[2], rtx_op2, mode);
5581 create_input_operand (&ops[3], mask, mask_mode);
5582 expand_insn (icode, 4, ops);
5584 return ops[0].value;
5587 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
5588 three operands. */
5591 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
5592 rtx target)
5594 struct expand_operand ops[6];
5595 enum insn_code icode;
5596 rtx comparison, rtx_op1, rtx_op2;
5597 machine_mode mode = TYPE_MODE (vec_cond_type);
5598 machine_mode cmp_op_mode;
5599 bool unsignedp;
5600 tree op0a, op0b;
5601 enum tree_code tcode;
5603 if (COMPARISON_CLASS_P (op0))
5605 op0a = TREE_OPERAND (op0, 0);
5606 op0b = TREE_OPERAND (op0, 1);
5607 tcode = TREE_CODE (op0);
5609 else
5611 gcc_assert (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (op0)));
5612 if (get_vcond_mask_icode (mode, TYPE_MODE (TREE_TYPE (op0)))
5613 != CODE_FOR_nothing)
5614 return expand_vec_cond_mask_expr (vec_cond_type, op0, op1,
5615 op2, target);
5616 /* Fake op0 < 0. */
5617 else
5619 gcc_assert (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (op0)))
5620 == MODE_VECTOR_INT);
5621 op0a = op0;
5622 op0b = build_zero_cst (TREE_TYPE (op0));
5623 tcode = LT_EXPR;
5626 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
5627 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
5630 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
5631 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
5633 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
5634 if (icode == CODE_FOR_nothing)
5636 if (tcode == EQ_EXPR || tcode == NE_EXPR)
5637 icode = get_vcond_eq_icode (mode, cmp_op_mode);
5638 if (icode == CODE_FOR_nothing)
5639 return 0;
5642 comparison = vector_compare_rtx (VOIDmode, tcode, op0a, op0b, unsignedp,
5643 icode, 4);
5644 rtx_op1 = expand_normal (op1);
5645 rtx_op2 = expand_normal (op2);
5647 create_output_operand (&ops[0], target, mode);
5648 create_input_operand (&ops[1], rtx_op1, mode);
5649 create_input_operand (&ops[2], rtx_op2, mode);
5650 create_fixed_operand (&ops[3], comparison);
5651 create_fixed_operand (&ops[4], XEXP (comparison, 0));
5652 create_fixed_operand (&ops[5], XEXP (comparison, 1));
5653 expand_insn (icode, 6, ops);
5654 return ops[0].value;
5657 /* Generate insns for a vector comparison into a mask. */
5660 expand_vec_cmp_expr (tree type, tree exp, rtx target)
5662 struct expand_operand ops[4];
5663 enum insn_code icode;
5664 rtx comparison;
5665 machine_mode mask_mode = TYPE_MODE (type);
5666 machine_mode vmode;
5667 bool unsignedp;
5668 tree op0a, op0b;
5669 enum tree_code tcode;
5671 op0a = TREE_OPERAND (exp, 0);
5672 op0b = TREE_OPERAND (exp, 1);
5673 tcode = TREE_CODE (exp);
5675 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
5676 vmode = TYPE_MODE (TREE_TYPE (op0a));
5678 icode = get_vec_cmp_icode (vmode, mask_mode, unsignedp);
5679 if (icode == CODE_FOR_nothing)
5681 if (tcode == EQ_EXPR || tcode == NE_EXPR)
5682 icode = get_vec_cmp_eq_icode (vmode, mask_mode);
5683 if (icode == CODE_FOR_nothing)
5684 return 0;
5687 comparison = vector_compare_rtx (mask_mode, tcode, op0a, op0b,
5688 unsignedp, icode, 2);
5689 create_output_operand (&ops[0], target, mask_mode);
5690 create_fixed_operand (&ops[1], comparison);
5691 create_fixed_operand (&ops[2], XEXP (comparison, 0));
5692 create_fixed_operand (&ops[3], XEXP (comparison, 1));
5693 expand_insn (icode, 4, ops);
5694 return ops[0].value;
5697 /* Expand a highpart multiply. */
5700 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
5701 rtx target, bool uns_p)
5703 struct expand_operand eops[3];
5704 enum insn_code icode;
5705 int method, i, nunits;
5706 machine_mode wmode;
5707 rtx m1, m2, perm;
5708 optab tab1, tab2;
5709 rtvec v;
5711 method = can_mult_highpart_p (mode, uns_p);
5712 switch (method)
5714 case 0:
5715 return NULL_RTX;
5716 case 1:
5717 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
5718 return expand_binop (mode, tab1, op0, op1, target, uns_p,
5719 OPTAB_LIB_WIDEN);
5720 case 2:
5721 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
5722 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
5723 break;
5724 case 3:
5725 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
5726 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
5727 if (BYTES_BIG_ENDIAN)
5728 std::swap (tab1, tab2);
5729 break;
5730 default:
5731 gcc_unreachable ();
5734 icode = optab_handler (tab1, mode);
5735 nunits = GET_MODE_NUNITS (mode);
5736 wmode = insn_data[icode].operand[0].mode;
5737 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
5738 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
5740 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
5741 create_input_operand (&eops[1], op0, mode);
5742 create_input_operand (&eops[2], op1, mode);
5743 expand_insn (icode, 3, eops);
5744 m1 = gen_lowpart (mode, eops[0].value);
5746 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
5747 create_input_operand (&eops[1], op0, mode);
5748 create_input_operand (&eops[2], op1, mode);
5749 expand_insn (optab_handler (tab2, mode), 3, eops);
5750 m2 = gen_lowpart (mode, eops[0].value);
5752 v = rtvec_alloc (nunits);
5753 if (method == 2)
5755 for (i = 0; i < nunits; ++i)
5756 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
5757 + ((i & 1) ? nunits : 0));
5759 else
5761 for (i = 0; i < nunits; ++i)
5762 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
5764 perm = gen_rtx_CONST_VECTOR (mode, v);
5766 return expand_vec_perm (mode, m1, m2, perm, target);
5769 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
5770 pattern. */
5772 static void
5773 find_cc_set (rtx x, const_rtx pat, void *data)
5775 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
5776 && GET_CODE (pat) == SET)
5778 rtx *p_cc_reg = (rtx *) data;
5779 gcc_assert (!*p_cc_reg);
5780 *p_cc_reg = x;
5784 /* This is a helper function for the other atomic operations. This function
5785 emits a loop that contains SEQ that iterates until a compare-and-swap
5786 operation at the end succeeds. MEM is the memory to be modified. SEQ is
5787 a set of instructions that takes a value from OLD_REG as an input and
5788 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
5789 set to the current contents of MEM. After SEQ, a compare-and-swap will
5790 attempt to update MEM with NEW_REG. The function returns true when the
5791 loop was generated successfully. */
5793 static bool
5794 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
5796 machine_mode mode = GET_MODE (mem);
5797 rtx_code_label *label;
5798 rtx cmp_reg, success, oldval;
5800 /* The loop we want to generate looks like
5802 cmp_reg = mem;
5803 label:
5804 old_reg = cmp_reg;
5805 seq;
5806 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
5807 if (success)
5808 goto label;
5810 Note that we only do the plain load from memory once. Subsequent
5811 iterations use the value loaded by the compare-and-swap pattern. */
5813 label = gen_label_rtx ();
5814 cmp_reg = gen_reg_rtx (mode);
5816 emit_move_insn (cmp_reg, mem);
5817 emit_label (label);
5818 emit_move_insn (old_reg, cmp_reg);
5819 if (seq)
5820 emit_insn (seq);
5822 success = NULL_RTX;
5823 oldval = cmp_reg;
5824 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
5825 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
5826 MEMMODEL_RELAXED))
5827 return false;
5829 if (oldval != cmp_reg)
5830 emit_move_insn (cmp_reg, oldval);
5832 /* Mark this jump predicted not taken. */
5833 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
5834 GET_MODE (success), 1, label,
5835 profile_probability::guessed_never ());
5836 return true;
5840 /* This function tries to emit an atomic_exchange intruction. VAL is written
5841 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
5842 using TARGET if possible. */
5844 static rtx
5845 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
5847 machine_mode mode = GET_MODE (mem);
5848 enum insn_code icode;
5850 /* If the target supports the exchange directly, great. */
5851 icode = direct_optab_handler (atomic_exchange_optab, mode);
5852 if (icode != CODE_FOR_nothing)
5854 struct expand_operand ops[4];
5856 create_output_operand (&ops[0], target, mode);
5857 create_fixed_operand (&ops[1], mem);
5858 create_input_operand (&ops[2], val, mode);
5859 create_integer_operand (&ops[3], model);
5860 if (maybe_expand_insn (icode, 4, ops))
5861 return ops[0].value;
5864 return NULL_RTX;
5867 /* This function tries to implement an atomic exchange operation using
5868 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
5869 The previous contents of *MEM are returned, using TARGET if possible.
5870 Since this instructionn is an acquire barrier only, stronger memory
5871 models may require additional barriers to be emitted. */
5873 static rtx
5874 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
5875 enum memmodel model)
5877 machine_mode mode = GET_MODE (mem);
5878 enum insn_code icode;
5879 rtx_insn *last_insn = get_last_insn ();
5881 icode = optab_handler (sync_lock_test_and_set_optab, mode);
5883 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
5884 exists, and the memory model is stronger than acquire, add a release
5885 barrier before the instruction. */
5887 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
5888 expand_mem_thread_fence (model);
5890 if (icode != CODE_FOR_nothing)
5892 struct expand_operand ops[3];
5893 create_output_operand (&ops[0], target, mode);
5894 create_fixed_operand (&ops[1], mem);
5895 create_input_operand (&ops[2], val, mode);
5896 if (maybe_expand_insn (icode, 3, ops))
5897 return ops[0].value;
5900 /* If an external test-and-set libcall is provided, use that instead of
5901 any external compare-and-swap that we might get from the compare-and-
5902 swap-loop expansion later. */
5903 if (!can_compare_and_swap_p (mode, false))
5905 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
5906 if (libfunc != NULL)
5908 rtx addr;
5910 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
5911 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
5912 mode, 2, addr, ptr_mode,
5913 val, mode);
5917 /* If the test_and_set can't be emitted, eliminate any barrier that might
5918 have been emitted. */
5919 delete_insns_since (last_insn);
5920 return NULL_RTX;
5923 /* This function tries to implement an atomic exchange operation using a
5924 compare_and_swap loop. VAL is written to *MEM. The previous contents of
5925 *MEM are returned, using TARGET if possible. No memory model is required
5926 since a compare_and_swap loop is seq-cst. */
5928 static rtx
5929 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
5931 machine_mode mode = GET_MODE (mem);
5933 if (can_compare_and_swap_p (mode, true))
5935 if (!target || !register_operand (target, mode))
5936 target = gen_reg_rtx (mode);
5937 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
5938 return target;
5941 return NULL_RTX;
5944 /* This function tries to implement an atomic test-and-set operation
5945 using the atomic_test_and_set instruction pattern. A boolean value
5946 is returned from the operation, using TARGET if possible. */
5948 static rtx
5949 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
5951 machine_mode pat_bool_mode;
5952 struct expand_operand ops[3];
5954 if (!targetm.have_atomic_test_and_set ())
5955 return NULL_RTX;
5957 /* While we always get QImode from __atomic_test_and_set, we get
5958 other memory modes from __sync_lock_test_and_set. Note that we
5959 use no endian adjustment here. This matches the 4.6 behavior
5960 in the Sparc backend. */
5961 enum insn_code icode = targetm.code_for_atomic_test_and_set;
5962 gcc_checking_assert (insn_data[icode].operand[1].mode == QImode);
5963 if (GET_MODE (mem) != QImode)
5964 mem = adjust_address_nv (mem, QImode, 0);
5966 pat_bool_mode = insn_data[icode].operand[0].mode;
5967 create_output_operand (&ops[0], target, pat_bool_mode);
5968 create_fixed_operand (&ops[1], mem);
5969 create_integer_operand (&ops[2], model);
5971 if (maybe_expand_insn (icode, 3, ops))
5972 return ops[0].value;
5973 return NULL_RTX;
5976 /* This function expands the legacy _sync_lock test_and_set operation which is
5977 generally an atomic exchange. Some limited targets only allow the
5978 constant 1 to be stored. This is an ACQUIRE operation.
5980 TARGET is an optional place to stick the return value.
5981 MEM is where VAL is stored. */
5984 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
5986 rtx ret;
5988 /* Try an atomic_exchange first. */
5989 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
5990 if (ret)
5991 return ret;
5993 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
5994 MEMMODEL_SYNC_ACQUIRE);
5995 if (ret)
5996 return ret;
5998 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
5999 if (ret)
6000 return ret;
6002 /* If there are no other options, try atomic_test_and_set if the value
6003 being stored is 1. */
6004 if (val == const1_rtx)
6005 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
6007 return ret;
6010 /* This function expands the atomic test_and_set operation:
6011 atomically store a boolean TRUE into MEM and return the previous value.
6013 MEMMODEL is the memory model variant to use.
6014 TARGET is an optional place to stick the return value. */
6017 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6019 machine_mode mode = GET_MODE (mem);
6020 rtx ret, trueval, subtarget;
6022 ret = maybe_emit_atomic_test_and_set (target, mem, model);
6023 if (ret)
6024 return ret;
6026 /* Be binary compatible with non-default settings of trueval, and different
6027 cpu revisions. E.g. one revision may have atomic-test-and-set, but
6028 another only has atomic-exchange. */
6029 if (targetm.atomic_test_and_set_trueval == 1)
6031 trueval = const1_rtx;
6032 subtarget = target ? target : gen_reg_rtx (mode);
6034 else
6036 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
6037 subtarget = gen_reg_rtx (mode);
6040 /* Try the atomic-exchange optab... */
6041 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
6043 /* ... then an atomic-compare-and-swap loop ... */
6044 if (!ret)
6045 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
6047 /* ... before trying the vaguely defined legacy lock_test_and_set. */
6048 if (!ret)
6049 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
6051 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
6052 things with the value 1. Thus we try again without trueval. */
6053 if (!ret && targetm.atomic_test_and_set_trueval != 1)
6054 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
6056 /* Failing all else, assume a single threaded environment and simply
6057 perform the operation. */
6058 if (!ret)
6060 /* If the result is ignored skip the move to target. */
6061 if (subtarget != const0_rtx)
6062 emit_move_insn (subtarget, mem);
6064 emit_move_insn (mem, trueval);
6065 ret = subtarget;
6068 /* Recall that have to return a boolean value; rectify if trueval
6069 is not exactly one. */
6070 if (targetm.atomic_test_and_set_trueval != 1)
6071 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
6073 return ret;
6076 /* This function expands the atomic exchange operation:
6077 atomically store VAL in MEM and return the previous value in MEM.
6079 MEMMODEL is the memory model variant to use.
6080 TARGET is an optional place to stick the return value. */
6083 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6085 machine_mode mode = GET_MODE (mem);
6086 rtx ret;
6088 /* If loads are not atomic for the required size and we are not called to
6089 provide a __sync builtin, do not do anything so that we stay consistent
6090 with atomic loads of the same size. */
6091 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6092 return NULL_RTX;
6094 ret = maybe_emit_atomic_exchange (target, mem, val, model);
6096 /* Next try a compare-and-swap loop for the exchange. */
6097 if (!ret)
6098 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6100 return ret;
6103 /* This function expands the atomic compare exchange operation:
6105 *PTARGET_BOOL is an optional place to store the boolean success/failure.
6106 *PTARGET_OVAL is an optional place to store the old value from memory.
6107 Both target parameters may be NULL or const0_rtx to indicate that we do
6108 not care about that return value. Both target parameters are updated on
6109 success to the actual location of the corresponding result.
6111 MEMMODEL is the memory model variant to use.
6113 The return value of the function is true for success. */
6115 bool
6116 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
6117 rtx mem, rtx expected, rtx desired,
6118 bool is_weak, enum memmodel succ_model,
6119 enum memmodel fail_model)
6121 machine_mode mode = GET_MODE (mem);
6122 struct expand_operand ops[8];
6123 enum insn_code icode;
6124 rtx target_oval, target_bool = NULL_RTX;
6125 rtx libfunc;
6127 /* If loads are not atomic for the required size and we are not called to
6128 provide a __sync builtin, do not do anything so that we stay consistent
6129 with atomic loads of the same size. */
6130 if (!can_atomic_load_p (mode) && !is_mm_sync (succ_model))
6131 return false;
6133 /* Load expected into a register for the compare and swap. */
6134 if (MEM_P (expected))
6135 expected = copy_to_reg (expected);
6137 /* Make sure we always have some place to put the return oldval.
6138 Further, make sure that place is distinct from the input expected,
6139 just in case we need that path down below. */
6140 if (ptarget_oval && *ptarget_oval == const0_rtx)
6141 ptarget_oval = NULL;
6143 if (ptarget_oval == NULL
6144 || (target_oval = *ptarget_oval) == NULL
6145 || reg_overlap_mentioned_p (expected, target_oval))
6146 target_oval = gen_reg_rtx (mode);
6148 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
6149 if (icode != CODE_FOR_nothing)
6151 machine_mode bool_mode = insn_data[icode].operand[0].mode;
6153 if (ptarget_bool && *ptarget_bool == const0_rtx)
6154 ptarget_bool = NULL;
6156 /* Make sure we always have a place for the bool operand. */
6157 if (ptarget_bool == NULL
6158 || (target_bool = *ptarget_bool) == NULL
6159 || GET_MODE (target_bool) != bool_mode)
6160 target_bool = gen_reg_rtx (bool_mode);
6162 /* Emit the compare_and_swap. */
6163 create_output_operand (&ops[0], target_bool, bool_mode);
6164 create_output_operand (&ops[1], target_oval, mode);
6165 create_fixed_operand (&ops[2], mem);
6166 create_input_operand (&ops[3], expected, mode);
6167 create_input_operand (&ops[4], desired, mode);
6168 create_integer_operand (&ops[5], is_weak);
6169 create_integer_operand (&ops[6], succ_model);
6170 create_integer_operand (&ops[7], fail_model);
6171 if (maybe_expand_insn (icode, 8, ops))
6173 /* Return success/failure. */
6174 target_bool = ops[0].value;
6175 target_oval = ops[1].value;
6176 goto success;
6180 /* Otherwise fall back to the original __sync_val_compare_and_swap
6181 which is always seq-cst. */
6182 icode = optab_handler (sync_compare_and_swap_optab, mode);
6183 if (icode != CODE_FOR_nothing)
6185 rtx cc_reg;
6187 create_output_operand (&ops[0], target_oval, mode);
6188 create_fixed_operand (&ops[1], mem);
6189 create_input_operand (&ops[2], expected, mode);
6190 create_input_operand (&ops[3], desired, mode);
6191 if (!maybe_expand_insn (icode, 4, ops))
6192 return false;
6194 target_oval = ops[0].value;
6196 /* If the caller isn't interested in the boolean return value,
6197 skip the computation of it. */
6198 if (ptarget_bool == NULL)
6199 goto success;
6201 /* Otherwise, work out if the compare-and-swap succeeded. */
6202 cc_reg = NULL_RTX;
6203 if (have_insn_for (COMPARE, CCmode))
6204 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6205 if (cc_reg)
6207 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
6208 const0_rtx, VOIDmode, 0, 1);
6209 goto success;
6211 goto success_bool_from_val;
6214 /* Also check for library support for __sync_val_compare_and_swap. */
6215 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
6216 if (libfunc != NULL)
6218 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6219 rtx target = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6220 mode, 3, addr, ptr_mode,
6221 expected, mode, desired, mode);
6222 emit_move_insn (target_oval, target);
6224 /* Compute the boolean return value only if requested. */
6225 if (ptarget_bool)
6226 goto success_bool_from_val;
6227 else
6228 goto success;
6231 /* Failure. */
6232 return false;
6234 success_bool_from_val:
6235 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
6236 expected, VOIDmode, 1, 1);
6237 success:
6238 /* Make sure that the oval output winds up where the caller asked. */
6239 if (ptarget_oval)
6240 *ptarget_oval = target_oval;
6241 if (ptarget_bool)
6242 *ptarget_bool = target_bool;
6243 return true;
6246 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
6248 static void
6249 expand_asm_memory_barrier (void)
6251 rtx asm_op, clob;
6253 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
6254 rtvec_alloc (0), rtvec_alloc (0),
6255 rtvec_alloc (0), UNKNOWN_LOCATION);
6256 MEM_VOLATILE_P (asm_op) = 1;
6258 clob = gen_rtx_SCRATCH (VOIDmode);
6259 clob = gen_rtx_MEM (BLKmode, clob);
6260 clob = gen_rtx_CLOBBER (VOIDmode, clob);
6262 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
6265 /* This routine will either emit the mem_thread_fence pattern or issue a
6266 sync_synchronize to generate a fence for memory model MEMMODEL. */
6268 void
6269 expand_mem_thread_fence (enum memmodel model)
6271 if (is_mm_relaxed (model))
6272 return;
6273 if (targetm.have_mem_thread_fence ())
6275 emit_insn (targetm.gen_mem_thread_fence (GEN_INT (model)));
6276 expand_asm_memory_barrier ();
6278 else if (targetm.have_memory_barrier ())
6279 emit_insn (targetm.gen_memory_barrier ());
6280 else if (synchronize_libfunc != NULL_RTX)
6281 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
6282 else
6283 expand_asm_memory_barrier ();
6286 /* This routine will either emit the mem_signal_fence pattern or issue a
6287 sync_synchronize to generate a fence for memory model MEMMODEL. */
6289 void
6290 expand_mem_signal_fence (enum memmodel model)
6292 if (targetm.have_mem_signal_fence ())
6293 emit_insn (targetm.gen_mem_signal_fence (GEN_INT (model)));
6294 else if (!is_mm_relaxed (model))
6296 /* By default targets are coherent between a thread and the signal
6297 handler running on the same thread. Thus this really becomes a
6298 compiler barrier, in that stores must not be sunk past
6299 (or raised above) a given point. */
6300 expand_asm_memory_barrier ();
6304 /* This function expands the atomic load operation:
6305 return the atomically loaded value in MEM.
6307 MEMMODEL is the memory model variant to use.
6308 TARGET is an option place to stick the return value. */
6311 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
6313 machine_mode mode = GET_MODE (mem);
6314 enum insn_code icode;
6316 /* If the target supports the load directly, great. */
6317 icode = direct_optab_handler (atomic_load_optab, mode);
6318 if (icode != CODE_FOR_nothing)
6320 struct expand_operand ops[3];
6322 create_output_operand (&ops[0], target, mode);
6323 create_fixed_operand (&ops[1], mem);
6324 create_integer_operand (&ops[2], model);
6325 if (maybe_expand_insn (icode, 3, ops))
6326 return ops[0].value;
6329 /* If the size of the object is greater than word size on this target,
6330 then we assume that a load will not be atomic. We could try to
6331 emulate a load with a compare-and-swap operation, but the store that
6332 doing this could result in would be incorrect if this is a volatile
6333 atomic load or targetting read-only-mapped memory. */
6334 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
6335 /* If there is no atomic load, leave the library call. */
6336 return NULL_RTX;
6338 /* Otherwise assume loads are atomic, and emit the proper barriers. */
6339 if (!target || target == const0_rtx)
6340 target = gen_reg_rtx (mode);
6342 /* For SEQ_CST, emit a barrier before the load. */
6343 if (is_mm_seq_cst (model))
6344 expand_mem_thread_fence (model);
6346 emit_move_insn (target, mem);
6348 /* Emit the appropriate barrier after the load. */
6349 expand_mem_thread_fence (model);
6351 return target;
6354 /* This function expands the atomic store operation:
6355 Atomically store VAL in MEM.
6356 MEMMODEL is the memory model variant to use.
6357 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
6358 function returns const0_rtx if a pattern was emitted. */
6361 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
6363 machine_mode mode = GET_MODE (mem);
6364 enum insn_code icode;
6365 struct expand_operand ops[3];
6367 /* If the target supports the store directly, great. */
6368 icode = direct_optab_handler (atomic_store_optab, mode);
6369 if (icode != CODE_FOR_nothing)
6371 create_fixed_operand (&ops[0], mem);
6372 create_input_operand (&ops[1], val, mode);
6373 create_integer_operand (&ops[2], model);
6374 if (maybe_expand_insn (icode, 3, ops))
6375 return const0_rtx;
6378 /* If using __sync_lock_release is a viable alternative, try it.
6379 Note that this will not be set to true if we are expanding a generic
6380 __atomic_store_n. */
6381 if (use_release)
6383 icode = direct_optab_handler (sync_lock_release_optab, mode);
6384 if (icode != CODE_FOR_nothing)
6386 create_fixed_operand (&ops[0], mem);
6387 create_input_operand (&ops[1], const0_rtx, mode);
6388 if (maybe_expand_insn (icode, 2, ops))
6390 /* lock_release is only a release barrier. */
6391 if (is_mm_seq_cst (model))
6392 expand_mem_thread_fence (model);
6393 return const0_rtx;
6398 /* If the size of the object is greater than word size on this target,
6399 a default store will not be atomic. */
6400 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
6402 /* If loads are atomic or we are called to provide a __sync builtin,
6403 we can try a atomic_exchange and throw away the result. Otherwise,
6404 don't do anything so that we do not create an inconsistency between
6405 loads and stores. */
6406 if (can_atomic_load_p (mode) || is_mm_sync (model))
6408 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
6409 if (!target)
6410 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem,
6411 val);
6412 if (target)
6413 return const0_rtx;
6415 return NULL_RTX;
6418 /* Otherwise assume stores are atomic, and emit the proper barriers. */
6419 expand_mem_thread_fence (model);
6421 emit_move_insn (mem, val);
6423 /* For SEQ_CST, also emit a barrier after the store. */
6424 if (is_mm_seq_cst (model))
6425 expand_mem_thread_fence (model);
6427 return const0_rtx;
6431 /* Structure containing the pointers and values required to process the
6432 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
6434 struct atomic_op_functions
6436 direct_optab mem_fetch_before;
6437 direct_optab mem_fetch_after;
6438 direct_optab mem_no_result;
6439 optab fetch_before;
6440 optab fetch_after;
6441 direct_optab no_result;
6442 enum rtx_code reverse_code;
6446 /* Fill in structure pointed to by OP with the various optab entries for an
6447 operation of type CODE. */
6449 static void
6450 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
6452 gcc_assert (op!= NULL);
6454 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
6455 in the source code during compilation, and the optab entries are not
6456 computable until runtime. Fill in the values at runtime. */
6457 switch (code)
6459 case PLUS:
6460 op->mem_fetch_before = atomic_fetch_add_optab;
6461 op->mem_fetch_after = atomic_add_fetch_optab;
6462 op->mem_no_result = atomic_add_optab;
6463 op->fetch_before = sync_old_add_optab;
6464 op->fetch_after = sync_new_add_optab;
6465 op->no_result = sync_add_optab;
6466 op->reverse_code = MINUS;
6467 break;
6468 case MINUS:
6469 op->mem_fetch_before = atomic_fetch_sub_optab;
6470 op->mem_fetch_after = atomic_sub_fetch_optab;
6471 op->mem_no_result = atomic_sub_optab;
6472 op->fetch_before = sync_old_sub_optab;
6473 op->fetch_after = sync_new_sub_optab;
6474 op->no_result = sync_sub_optab;
6475 op->reverse_code = PLUS;
6476 break;
6477 case XOR:
6478 op->mem_fetch_before = atomic_fetch_xor_optab;
6479 op->mem_fetch_after = atomic_xor_fetch_optab;
6480 op->mem_no_result = atomic_xor_optab;
6481 op->fetch_before = sync_old_xor_optab;
6482 op->fetch_after = sync_new_xor_optab;
6483 op->no_result = sync_xor_optab;
6484 op->reverse_code = XOR;
6485 break;
6486 case AND:
6487 op->mem_fetch_before = atomic_fetch_and_optab;
6488 op->mem_fetch_after = atomic_and_fetch_optab;
6489 op->mem_no_result = atomic_and_optab;
6490 op->fetch_before = sync_old_and_optab;
6491 op->fetch_after = sync_new_and_optab;
6492 op->no_result = sync_and_optab;
6493 op->reverse_code = UNKNOWN;
6494 break;
6495 case IOR:
6496 op->mem_fetch_before = atomic_fetch_or_optab;
6497 op->mem_fetch_after = atomic_or_fetch_optab;
6498 op->mem_no_result = atomic_or_optab;
6499 op->fetch_before = sync_old_ior_optab;
6500 op->fetch_after = sync_new_ior_optab;
6501 op->no_result = sync_ior_optab;
6502 op->reverse_code = UNKNOWN;
6503 break;
6504 case NOT:
6505 op->mem_fetch_before = atomic_fetch_nand_optab;
6506 op->mem_fetch_after = atomic_nand_fetch_optab;
6507 op->mem_no_result = atomic_nand_optab;
6508 op->fetch_before = sync_old_nand_optab;
6509 op->fetch_after = sync_new_nand_optab;
6510 op->no_result = sync_nand_optab;
6511 op->reverse_code = UNKNOWN;
6512 break;
6513 default:
6514 gcc_unreachable ();
6518 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
6519 using memory order MODEL. If AFTER is true the operation needs to return
6520 the value of *MEM after the operation, otherwise the previous value.
6521 TARGET is an optional place to place the result. The result is unused if
6522 it is const0_rtx.
6523 Return the result if there is a better sequence, otherwise NULL_RTX. */
6525 static rtx
6526 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
6527 enum memmodel model, bool after)
6529 /* If the value is prefetched, or not used, it may be possible to replace
6530 the sequence with a native exchange operation. */
6531 if (!after || target == const0_rtx)
6533 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
6534 if (code == AND && val == const0_rtx)
6536 if (target == const0_rtx)
6537 target = gen_reg_rtx (GET_MODE (mem));
6538 return maybe_emit_atomic_exchange (target, mem, val, model);
6541 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
6542 if (code == IOR && val == constm1_rtx)
6544 if (target == const0_rtx)
6545 target = gen_reg_rtx (GET_MODE (mem));
6546 return maybe_emit_atomic_exchange (target, mem, val, model);
6550 return NULL_RTX;
6553 /* Try to emit an instruction for a specific operation varaition.
6554 OPTAB contains the OP functions.
6555 TARGET is an optional place to return the result. const0_rtx means unused.
6556 MEM is the memory location to operate on.
6557 VAL is the value to use in the operation.
6558 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
6559 MODEL is the memory model, if used.
6560 AFTER is true if the returned result is the value after the operation. */
6562 static rtx
6563 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
6564 rtx val, bool use_memmodel, enum memmodel model, bool after)
6566 machine_mode mode = GET_MODE (mem);
6567 struct expand_operand ops[4];
6568 enum insn_code icode;
6569 int op_counter = 0;
6570 int num_ops;
6572 /* Check to see if there is a result returned. */
6573 if (target == const0_rtx)
6575 if (use_memmodel)
6577 icode = direct_optab_handler (optab->mem_no_result, mode);
6578 create_integer_operand (&ops[2], model);
6579 num_ops = 3;
6581 else
6583 icode = direct_optab_handler (optab->no_result, mode);
6584 num_ops = 2;
6587 /* Otherwise, we need to generate a result. */
6588 else
6590 if (use_memmodel)
6592 icode = direct_optab_handler (after ? optab->mem_fetch_after
6593 : optab->mem_fetch_before, mode);
6594 create_integer_operand (&ops[3], model);
6595 num_ops = 4;
6597 else
6599 icode = optab_handler (after ? optab->fetch_after
6600 : optab->fetch_before, mode);
6601 num_ops = 3;
6603 create_output_operand (&ops[op_counter++], target, mode);
6605 if (icode == CODE_FOR_nothing)
6606 return NULL_RTX;
6608 create_fixed_operand (&ops[op_counter++], mem);
6609 /* VAL may have been promoted to a wider mode. Shrink it if so. */
6610 create_convert_operand_to (&ops[op_counter++], val, mode, true);
6612 if (maybe_expand_insn (icode, num_ops, ops))
6613 return (target == const0_rtx ? const0_rtx : ops[0].value);
6615 return NULL_RTX;
6619 /* This function expands an atomic fetch_OP or OP_fetch operation:
6620 TARGET is an option place to stick the return value. const0_rtx indicates
6621 the result is unused.
6622 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6623 CODE is the operation being performed (OP)
6624 MEMMODEL is the memory model variant to use.
6625 AFTER is true to return the result of the operation (OP_fetch).
6626 AFTER is false to return the value before the operation (fetch_OP).
6628 This function will *only* generate instructions if there is a direct
6629 optab. No compare and swap loops or libcalls will be generated. */
6631 static rtx
6632 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
6633 enum rtx_code code, enum memmodel model,
6634 bool after)
6636 machine_mode mode = GET_MODE (mem);
6637 struct atomic_op_functions optab;
6638 rtx result;
6639 bool unused_result = (target == const0_rtx);
6641 get_atomic_op_for_code (&optab, code);
6643 /* Check to see if there are any better instructions. */
6644 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
6645 if (result)
6646 return result;
6648 /* Check for the case where the result isn't used and try those patterns. */
6649 if (unused_result)
6651 /* Try the memory model variant first. */
6652 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
6653 if (result)
6654 return result;
6656 /* Next try the old style withuot a memory model. */
6657 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
6658 if (result)
6659 return result;
6661 /* There is no no-result pattern, so try patterns with a result. */
6662 target = NULL_RTX;
6665 /* Try the __atomic version. */
6666 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
6667 if (result)
6668 return result;
6670 /* Try the older __sync version. */
6671 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
6672 if (result)
6673 return result;
6675 /* If the fetch value can be calculated from the other variation of fetch,
6676 try that operation. */
6677 if (after || unused_result || optab.reverse_code != UNKNOWN)
6679 /* Try the __atomic version, then the older __sync version. */
6680 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
6681 if (!result)
6682 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
6684 if (result)
6686 /* If the result isn't used, no need to do compensation code. */
6687 if (unused_result)
6688 return result;
6690 /* Issue compensation code. Fetch_after == fetch_before OP val.
6691 Fetch_before == after REVERSE_OP val. */
6692 if (!after)
6693 code = optab.reverse_code;
6694 if (code == NOT)
6696 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
6697 true, OPTAB_LIB_WIDEN);
6698 result = expand_simple_unop (mode, NOT, result, target, true);
6700 else
6701 result = expand_simple_binop (mode, code, result, val, target,
6702 true, OPTAB_LIB_WIDEN);
6703 return result;
6707 /* No direct opcode can be generated. */
6708 return NULL_RTX;
6713 /* This function expands an atomic fetch_OP or OP_fetch operation:
6714 TARGET is an option place to stick the return value. const0_rtx indicates
6715 the result is unused.
6716 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6717 CODE is the operation being performed (OP)
6718 MEMMODEL is the memory model variant to use.
6719 AFTER is true to return the result of the operation (OP_fetch).
6720 AFTER is false to return the value before the operation (fetch_OP). */
6722 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
6723 enum memmodel model, bool after)
6725 machine_mode mode = GET_MODE (mem);
6726 rtx result;
6727 bool unused_result = (target == const0_rtx);
6729 /* If loads are not atomic for the required size and we are not called to
6730 provide a __sync builtin, do not do anything so that we stay consistent
6731 with atomic loads of the same size. */
6732 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6733 return NULL_RTX;
6735 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
6736 after);
6738 if (result)
6739 return result;
6741 /* Add/sub can be implemented by doing the reverse operation with -(val). */
6742 if (code == PLUS || code == MINUS)
6744 rtx tmp;
6745 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
6747 start_sequence ();
6748 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
6749 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
6750 model, after);
6751 if (result)
6753 /* PLUS worked so emit the insns and return. */
6754 tmp = get_insns ();
6755 end_sequence ();
6756 emit_insn (tmp);
6757 return result;
6760 /* PLUS did not work, so throw away the negation code and continue. */
6761 end_sequence ();
6764 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
6765 if (!can_compare_and_swap_p (mode, false))
6767 rtx libfunc;
6768 bool fixup = false;
6769 enum rtx_code orig_code = code;
6770 struct atomic_op_functions optab;
6772 get_atomic_op_for_code (&optab, code);
6773 libfunc = optab_libfunc (after ? optab.fetch_after
6774 : optab.fetch_before, mode);
6775 if (libfunc == NULL
6776 && (after || unused_result || optab.reverse_code != UNKNOWN))
6778 fixup = true;
6779 if (!after)
6780 code = optab.reverse_code;
6781 libfunc = optab_libfunc (after ? optab.fetch_before
6782 : optab.fetch_after, mode);
6784 if (libfunc != NULL)
6786 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6787 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
6788 2, addr, ptr_mode, val, mode);
6790 if (!unused_result && fixup)
6791 result = expand_simple_binop (mode, code, result, val, target,
6792 true, OPTAB_LIB_WIDEN);
6793 return result;
6796 /* We need the original code for any further attempts. */
6797 code = orig_code;
6800 /* If nothing else has succeeded, default to a compare and swap loop. */
6801 if (can_compare_and_swap_p (mode, true))
6803 rtx_insn *insn;
6804 rtx t0 = gen_reg_rtx (mode), t1;
6806 start_sequence ();
6808 /* If the result is used, get a register for it. */
6809 if (!unused_result)
6811 if (!target || !register_operand (target, mode))
6812 target = gen_reg_rtx (mode);
6813 /* If fetch_before, copy the value now. */
6814 if (!after)
6815 emit_move_insn (target, t0);
6817 else
6818 target = const0_rtx;
6820 t1 = t0;
6821 if (code == NOT)
6823 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
6824 true, OPTAB_LIB_WIDEN);
6825 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
6827 else
6828 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
6829 OPTAB_LIB_WIDEN);
6831 /* For after, copy the value now. */
6832 if (!unused_result && after)
6833 emit_move_insn (target, t1);
6834 insn = get_insns ();
6835 end_sequence ();
6837 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
6838 return target;
6841 return NULL_RTX;
6844 /* Return true if OPERAND is suitable for operand number OPNO of
6845 instruction ICODE. */
6847 bool
6848 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
6850 return (!insn_data[(int) icode].operand[opno].predicate
6851 || (insn_data[(int) icode].operand[opno].predicate
6852 (operand, insn_data[(int) icode].operand[opno].mode)));
6855 /* TARGET is a target of a multiword operation that we are going to
6856 implement as a series of word-mode operations. Return true if
6857 TARGET is suitable for this purpose. */
6859 bool
6860 valid_multiword_target_p (rtx target)
6862 machine_mode mode;
6863 int i;
6865 mode = GET_MODE (target);
6866 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
6867 if (!validate_subreg (word_mode, mode, target, i))
6868 return false;
6869 return true;
6872 /* Like maybe_legitimize_operand, but do not change the code of the
6873 current rtx value. */
6875 static bool
6876 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
6877 struct expand_operand *op)
6879 /* See if the operand matches in its current form. */
6880 if (insn_operand_matches (icode, opno, op->value))
6881 return true;
6883 /* If the operand is a memory whose address has no side effects,
6884 try forcing the address into a non-virtual pseudo register.
6885 The check for side effects is important because copy_to_mode_reg
6886 cannot handle things like auto-modified addresses. */
6887 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
6889 rtx addr, mem;
6891 mem = op->value;
6892 addr = XEXP (mem, 0);
6893 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
6894 && !side_effects_p (addr))
6896 rtx_insn *last;
6897 machine_mode mode;
6899 last = get_last_insn ();
6900 mode = get_address_mode (mem);
6901 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
6902 if (insn_operand_matches (icode, opno, mem))
6904 op->value = mem;
6905 return true;
6907 delete_insns_since (last);
6911 return false;
6914 /* Try to make OP match operand OPNO of instruction ICODE. Return true
6915 on success, storing the new operand value back in OP. */
6917 static bool
6918 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
6919 struct expand_operand *op)
6921 machine_mode mode, imode;
6922 bool old_volatile_ok, result;
6924 mode = op->mode;
6925 switch (op->type)
6927 case EXPAND_FIXED:
6928 old_volatile_ok = volatile_ok;
6929 volatile_ok = true;
6930 result = maybe_legitimize_operand_same_code (icode, opno, op);
6931 volatile_ok = old_volatile_ok;
6932 return result;
6934 case EXPAND_OUTPUT:
6935 gcc_assert (mode != VOIDmode);
6936 if (op->value
6937 && op->value != const0_rtx
6938 && GET_MODE (op->value) == mode
6939 && maybe_legitimize_operand_same_code (icode, opno, op))
6940 return true;
6942 op->value = gen_reg_rtx (mode);
6943 op->target = 0;
6944 break;
6946 case EXPAND_INPUT:
6947 input:
6948 gcc_assert (mode != VOIDmode);
6949 gcc_assert (GET_MODE (op->value) == VOIDmode
6950 || GET_MODE (op->value) == mode);
6951 if (maybe_legitimize_operand_same_code (icode, opno, op))
6952 return true;
6954 op->value = copy_to_mode_reg (mode, op->value);
6955 break;
6957 case EXPAND_CONVERT_TO:
6958 gcc_assert (mode != VOIDmode);
6959 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
6960 goto input;
6962 case EXPAND_CONVERT_FROM:
6963 if (GET_MODE (op->value) != VOIDmode)
6964 mode = GET_MODE (op->value);
6965 else
6966 /* The caller must tell us what mode this value has. */
6967 gcc_assert (mode != VOIDmode);
6969 imode = insn_data[(int) icode].operand[opno].mode;
6970 if (imode != VOIDmode && imode != mode)
6972 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
6973 mode = imode;
6975 goto input;
6977 case EXPAND_ADDRESS:
6978 gcc_assert (mode != VOIDmode);
6979 op->value = convert_memory_address (mode, op->value);
6980 goto input;
6982 case EXPAND_INTEGER:
6983 mode = insn_data[(int) icode].operand[opno].mode;
6984 if (mode != VOIDmode && const_int_operand (op->value, mode))
6985 goto input;
6986 break;
6988 return insn_operand_matches (icode, opno, op->value);
6991 /* Make OP describe an input operand that should have the same value
6992 as VALUE, after any mode conversion that the target might request.
6993 TYPE is the type of VALUE. */
6995 void
6996 create_convert_operand_from_type (struct expand_operand *op,
6997 rtx value, tree type)
6999 create_convert_operand_from (op, value, TYPE_MODE (type),
7000 TYPE_UNSIGNED (type));
7003 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
7004 of instruction ICODE. Return true on success, leaving the new operand
7005 values in the OPS themselves. Emit no code on failure. */
7007 bool
7008 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
7009 unsigned int nops, struct expand_operand *ops)
7011 rtx_insn *last;
7012 unsigned int i;
7014 last = get_last_insn ();
7015 for (i = 0; i < nops; i++)
7016 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
7018 delete_insns_since (last);
7019 return false;
7021 return true;
7024 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
7025 as its operands. Return the instruction pattern on success,
7026 and emit any necessary set-up code. Return null and emit no
7027 code on failure. */
7029 rtx_insn *
7030 maybe_gen_insn (enum insn_code icode, unsigned int nops,
7031 struct expand_operand *ops)
7033 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
7034 if (!maybe_legitimize_operands (icode, 0, nops, ops))
7035 return NULL;
7037 switch (nops)
7039 case 1:
7040 return GEN_FCN (icode) (ops[0].value);
7041 case 2:
7042 return GEN_FCN (icode) (ops[0].value, ops[1].value);
7043 case 3:
7044 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
7045 case 4:
7046 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7047 ops[3].value);
7048 case 5:
7049 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7050 ops[3].value, ops[4].value);
7051 case 6:
7052 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7053 ops[3].value, ops[4].value, ops[5].value);
7054 case 7:
7055 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7056 ops[3].value, ops[4].value, ops[5].value,
7057 ops[6].value);
7058 case 8:
7059 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7060 ops[3].value, ops[4].value, ops[5].value,
7061 ops[6].value, ops[7].value);
7062 case 9:
7063 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7064 ops[3].value, ops[4].value, ops[5].value,
7065 ops[6].value, ops[7].value, ops[8].value);
7067 gcc_unreachable ();
7070 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
7071 as its operands. Return true on success and emit no code on failure. */
7073 bool
7074 maybe_expand_insn (enum insn_code icode, unsigned int nops,
7075 struct expand_operand *ops)
7077 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7078 if (pat)
7080 emit_insn (pat);
7081 return true;
7083 return false;
7086 /* Like maybe_expand_insn, but for jumps. */
7088 bool
7089 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
7090 struct expand_operand *ops)
7092 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7093 if (pat)
7095 emit_jump_insn (pat);
7096 return true;
7098 return false;
7101 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
7102 as its operands. */
7104 void
7105 expand_insn (enum insn_code icode, unsigned int nops,
7106 struct expand_operand *ops)
7108 if (!maybe_expand_insn (icode, nops, ops))
7109 gcc_unreachable ();
7112 /* Like expand_insn, but for jumps. */
7114 void
7115 expand_jump_insn (enum insn_code icode, unsigned int nops,
7116 struct expand_operand *ops)
7118 if (!maybe_expand_jump_insn (icode, nops, ops))
7119 gcc_unreachable ();