1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2020 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
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
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/>. */
23 #include "coretypes.h"
35 #include "diagnostic-core.h"
36 #include "rtx-vector-builder.h"
38 /* Include insn-config.h before expr.h so that HAVE_conditional_move
39 is properly defined. */
40 #include "stor-layout.h"
45 #include "optabs-tree.h"
48 static void prepare_float_lib_cmp (rtx
, rtx
, enum rtx_code
, rtx
*,
50 static rtx
expand_unop_direct (machine_mode
, optab
, rtx
, rtx
, int);
51 static void emit_libcall_block_1 (rtx_insn
*, rtx
, rtx
, rtx
, bool);
53 /* Debug facility for use in GDB. */
54 void debug_optab_libfuncs (void);
56 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
57 the result of operation CODE applied to OP0 (and OP1 if it is a binary
58 operation). OP0_MODE is OP0's mode.
60 If the last insn does not set TARGET, don't do anything, but return 1.
62 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
63 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
64 try again, ensuring that TARGET is not one of the operands. */
67 add_equal_note (rtx_insn
*insns
, rtx target
, enum rtx_code code
, rtx op0
,
68 rtx op1
, machine_mode op0_mode
)
74 gcc_assert (insns
&& INSN_P (insns
) && NEXT_INSN (insns
));
76 if (GET_RTX_CLASS (code
) != RTX_COMM_ARITH
77 && GET_RTX_CLASS (code
) != RTX_BIN_ARITH
78 && GET_RTX_CLASS (code
) != RTX_COMM_COMPARE
79 && GET_RTX_CLASS (code
) != RTX_COMPARE
80 && GET_RTX_CLASS (code
) != RTX_UNARY
)
83 if (GET_CODE (target
) == ZERO_EXTRACT
)
86 for (last_insn
= insns
;
87 NEXT_INSN (last_insn
) != NULL_RTX
;
88 last_insn
= NEXT_INSN (last_insn
))
91 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
92 a value changing in the insn, so the note would be invalid for CSE. */
93 if (reg_overlap_mentioned_p (target
, op0
)
94 || (op1
&& reg_overlap_mentioned_p (target
, op1
)))
97 && (rtx_equal_p (target
, op0
)
98 || (op1
&& rtx_equal_p (target
, op1
))))
100 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
101 over expanding it as temp = MEM op X, MEM = temp. If the target
102 supports MEM = MEM op X instructions, it is sometimes too hard
103 to reconstruct that form later, especially if X is also a memory,
104 and due to multiple occurrences of addresses the address might
105 be forced into register unnecessarily.
106 Note that not emitting the REG_EQUIV note might inhibit
107 CSE in some cases. */
108 set
= single_set (last_insn
);
110 && GET_CODE (SET_SRC (set
)) == code
111 && MEM_P (SET_DEST (set
))
112 && (rtx_equal_p (SET_DEST (set
), XEXP (SET_SRC (set
), 0))
113 || (op1
&& rtx_equal_p (SET_DEST (set
),
114 XEXP (SET_SRC (set
), 1)))))
120 set
= set_for_reg_notes (last_insn
);
124 if (! rtx_equal_p (SET_DEST (set
), target
)
125 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
126 && (GET_CODE (SET_DEST (set
)) != STRICT_LOW_PART
127 || ! rtx_equal_p (XEXP (SET_DEST (set
), 0), target
)))
130 if (GET_RTX_CLASS (code
) == RTX_UNARY
)
140 if (op0_mode
!= VOIDmode
&& GET_MODE (target
) != op0_mode
)
142 note
= gen_rtx_fmt_e (code
, op0_mode
, copy_rtx (op0
));
143 if (GET_MODE_UNIT_SIZE (op0_mode
)
144 > GET_MODE_UNIT_SIZE (GET_MODE (target
)))
145 note
= simplify_gen_unary (TRUNCATE
, GET_MODE (target
),
148 note
= simplify_gen_unary (ZERO_EXTEND
, GET_MODE (target
),
154 note
= gen_rtx_fmt_e (code
, GET_MODE (target
), copy_rtx (op0
));
158 note
= gen_rtx_fmt_ee (code
, GET_MODE (target
), copy_rtx (op0
), copy_rtx (op1
));
160 set_unique_reg_note (last_insn
, REG_EQUAL
, note
);
165 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
166 for a widening operation would be. In most cases this would be OP0, but if
167 that's a constant it'll be VOIDmode, which isn't useful. */
170 widened_mode (machine_mode to_mode
, rtx op0
, rtx op1
)
172 machine_mode m0
= GET_MODE (op0
);
173 machine_mode m1
= GET_MODE (op1
);
176 if (m0
== VOIDmode
&& m1
== VOIDmode
)
178 else if (m0
== VOIDmode
|| GET_MODE_UNIT_SIZE (m0
) < GET_MODE_UNIT_SIZE (m1
))
183 if (GET_MODE_UNIT_SIZE (result
) > GET_MODE_UNIT_SIZE (to_mode
))
189 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
190 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
191 not actually do a sign-extend or zero-extend, but can leave the
192 higher-order bits of the result rtx undefined, for example, in the case
193 of logical operations, but not right shifts. */
196 widen_operand (rtx op
, machine_mode mode
, machine_mode oldmode
,
197 int unsignedp
, int no_extend
)
200 scalar_int_mode int_mode
;
202 /* If we don't have to extend and this is a constant, return it. */
203 if (no_extend
&& GET_MODE (op
) == VOIDmode
)
206 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
207 extend since it will be more efficient to do so unless the signedness of
208 a promoted object differs from our extension. */
210 || !is_a
<scalar_int_mode
> (mode
, &int_mode
)
211 || (GET_CODE (op
) == SUBREG
&& SUBREG_PROMOTED_VAR_P (op
)
212 && SUBREG_CHECK_PROMOTED_SIGN (op
, unsignedp
)))
213 return convert_modes (mode
, oldmode
, op
, unsignedp
);
215 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
217 if (GET_MODE_SIZE (int_mode
) <= UNITS_PER_WORD
)
218 return gen_lowpart (int_mode
, force_reg (GET_MODE (op
), op
));
220 /* Otherwise, get an object of MODE, clobber it, and set the low-order
223 result
= gen_reg_rtx (int_mode
);
224 emit_clobber (result
);
225 emit_move_insn (gen_lowpart (GET_MODE (op
), result
), op
);
229 /* Expand vector widening operations.
231 There are two different classes of operations handled here:
232 1) Operations whose result is wider than all the arguments to the operation.
233 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
234 In this case OP0 and optionally OP1 would be initialized,
235 but WIDE_OP wouldn't (not relevant for this case).
236 2) Operations whose result is of the same size as the last argument to the
237 operation, but wider than all the other arguments to the operation.
238 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
239 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
241 E.g, when called to expand the following operations, this is how
242 the arguments will be initialized:
244 widening-sum 2 oprnd0 - oprnd1
245 widening-dot-product 3 oprnd0 oprnd1 oprnd2
246 widening-mult 2 oprnd0 oprnd1 -
247 type-promotion (vec-unpack) 1 oprnd0 - - */
250 expand_widen_pattern_expr (sepops ops
, rtx op0
, rtx op1
, rtx wide_op
,
251 rtx target
, int unsignedp
)
253 class expand_operand eops
[4];
254 tree oprnd0
, oprnd1
, oprnd2
;
255 machine_mode wmode
= VOIDmode
, tmode0
, tmode1
= VOIDmode
;
256 optab widen_pattern_optab
;
257 enum insn_code icode
;
258 int nops
= TREE_CODE_LENGTH (ops
->code
);
263 tmode0
= TYPE_MODE (TREE_TYPE (oprnd0
));
264 if (ops
->code
== VEC_UNPACK_FIX_TRUNC_HI_EXPR
265 || ops
->code
== VEC_UNPACK_FIX_TRUNC_LO_EXPR
)
266 /* The sign is from the result type rather than operand's type
269 = optab_for_tree_code (ops
->code
, ops
->type
, optab_default
);
270 else if ((ops
->code
== VEC_UNPACK_HI_EXPR
271 || ops
->code
== VEC_UNPACK_LO_EXPR
)
272 && VECTOR_BOOLEAN_TYPE_P (ops
->type
)
273 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (oprnd0
))
274 && TYPE_MODE (ops
->type
) == TYPE_MODE (TREE_TYPE (oprnd0
))
275 && SCALAR_INT_MODE_P (TYPE_MODE (ops
->type
)))
277 /* For VEC_UNPACK_{LO,HI}_EXPR if the mode of op0 and result is
278 the same scalar mode for VECTOR_BOOLEAN_TYPE_P vectors, use
279 vec_unpacks_sbool_{lo,hi}_optab, so that we can pass in
280 the pattern number of elements in the wider vector. */
282 = (ops
->code
== VEC_UNPACK_HI_EXPR
283 ? vec_unpacks_sbool_hi_optab
: vec_unpacks_sbool_lo_optab
);
288 = optab_for_tree_code (ops
->code
, TREE_TYPE (oprnd0
), optab_default
);
289 if (ops
->code
== WIDEN_MULT_PLUS_EXPR
290 || ops
->code
== WIDEN_MULT_MINUS_EXPR
)
291 icode
= find_widening_optab_handler (widen_pattern_optab
,
292 TYPE_MODE (TREE_TYPE (ops
->op2
)),
295 icode
= optab_handler (widen_pattern_optab
, tmode0
);
296 gcc_assert (icode
!= CODE_FOR_nothing
);
301 tmode1
= TYPE_MODE (TREE_TYPE (oprnd1
));
306 op1
= GEN_INT (TYPE_VECTOR_SUBPARTS (TREE_TYPE (oprnd0
)).to_constant ());
310 /* The last operand is of a wider mode than the rest of the operands. */
315 gcc_assert (tmode1
== tmode0
);
318 wmode
= TYPE_MODE (TREE_TYPE (oprnd2
));
322 create_output_operand (&eops
[op
++], target
, TYPE_MODE (ops
->type
));
323 create_convert_operand_from (&eops
[op
++], op0
, tmode0
, unsignedp
);
325 create_convert_operand_from (&eops
[op
++], op1
, tmode1
, unsignedp
);
327 create_convert_operand_from (&eops
[op
++], wide_op
, wmode
, unsignedp
);
328 expand_insn (icode
, op
, eops
);
329 return eops
[0].value
;
332 /* Generate code to perform an operation specified by TERNARY_OPTAB
333 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
335 UNSIGNEDP is for the case where we have to widen the operands
336 to perform the operation. It says to use zero-extension.
338 If TARGET is nonzero, the value
339 is generated there, if it is convenient to do so.
340 In all cases an rtx is returned for the locus of the value;
341 this may or may not be TARGET. */
344 expand_ternary_op (machine_mode mode
, optab ternary_optab
, rtx op0
,
345 rtx op1
, rtx op2
, rtx target
, int unsignedp
)
347 class expand_operand ops
[4];
348 enum insn_code icode
= optab_handler (ternary_optab
, mode
);
350 gcc_assert (optab_handler (ternary_optab
, mode
) != CODE_FOR_nothing
);
352 create_output_operand (&ops
[0], target
, mode
);
353 create_convert_operand_from (&ops
[1], op0
, mode
, unsignedp
);
354 create_convert_operand_from (&ops
[2], op1
, mode
, unsignedp
);
355 create_convert_operand_from (&ops
[3], op2
, mode
, unsignedp
);
356 expand_insn (icode
, 4, ops
);
361 /* Like expand_binop, but return a constant rtx if the result can be
362 calculated at compile time. The arguments and return value are
363 otherwise the same as for expand_binop. */
366 simplify_expand_binop (machine_mode mode
, optab binoptab
,
367 rtx op0
, rtx op1
, rtx target
, int unsignedp
,
368 enum optab_methods methods
)
370 if (CONSTANT_P (op0
) && CONSTANT_P (op1
))
372 rtx x
= simplify_binary_operation (optab_to_code (binoptab
),
378 return expand_binop (mode
, binoptab
, op0
, op1
, target
, unsignedp
, methods
);
381 /* Like simplify_expand_binop, but always put the result in TARGET.
382 Return true if the expansion succeeded. */
385 force_expand_binop (machine_mode mode
, optab binoptab
,
386 rtx op0
, rtx op1
, rtx target
, int unsignedp
,
387 enum optab_methods methods
)
389 rtx x
= simplify_expand_binop (mode
, binoptab
, op0
, op1
,
390 target
, unsignedp
, methods
);
394 emit_move_insn (target
, x
);
398 /* Create a new vector value in VMODE with all elements set to OP. The
399 mode of OP must be the element mode of VMODE. If OP is a constant,
400 then the return value will be a constant. */
403 expand_vector_broadcast (machine_mode vmode
, rtx op
)
408 gcc_checking_assert (VECTOR_MODE_P (vmode
));
410 if (valid_for_const_vector_p (vmode
, op
))
411 return gen_const_vec_duplicate (vmode
, op
);
413 insn_code icode
= optab_handler (vec_duplicate_optab
, vmode
);
414 if (icode
!= CODE_FOR_nothing
)
416 class expand_operand ops
[2];
417 create_output_operand (&ops
[0], NULL_RTX
, vmode
);
418 create_input_operand (&ops
[1], op
, GET_MODE (op
));
419 expand_insn (icode
, 2, ops
);
423 if (!GET_MODE_NUNITS (vmode
).is_constant (&n
))
426 /* ??? If the target doesn't have a vec_init, then we have no easy way
427 of performing this operation. Most of this sort of generic support
428 is hidden away in the vector lowering support in gimple. */
429 icode
= convert_optab_handler (vec_init_optab
, vmode
,
430 GET_MODE_INNER (vmode
));
431 if (icode
== CODE_FOR_nothing
)
434 vec
= rtvec_alloc (n
);
435 for (int i
= 0; i
< n
; ++i
)
436 RTVEC_ELT (vec
, i
) = op
;
437 rtx ret
= gen_reg_rtx (vmode
);
438 emit_insn (GEN_FCN (icode
) (ret
, gen_rtx_PARALLEL (vmode
, vec
)));
443 /* This subroutine of expand_doubleword_shift handles the cases in which
444 the effective shift value is >= BITS_PER_WORD. The arguments and return
445 value are the same as for the parent routine, except that SUPERWORD_OP1
446 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
447 INTO_TARGET may be null if the caller has decided to calculate it. */
450 expand_superword_shift (optab binoptab
, rtx outof_input
, rtx superword_op1
,
451 rtx outof_target
, rtx into_target
,
452 int unsignedp
, enum optab_methods methods
)
454 if (into_target
!= 0)
455 if (!force_expand_binop (word_mode
, binoptab
, outof_input
, superword_op1
,
456 into_target
, unsignedp
, methods
))
459 if (outof_target
!= 0)
461 /* For a signed right shift, we must fill OUTOF_TARGET with copies
462 of the sign bit, otherwise we must fill it with zeros. */
463 if (binoptab
!= ashr_optab
)
464 emit_move_insn (outof_target
, CONST0_RTX (word_mode
));
466 if (!force_expand_binop (word_mode
, binoptab
, outof_input
,
467 gen_int_shift_amount (word_mode
,
469 outof_target
, unsignedp
, methods
))
475 /* This subroutine of expand_doubleword_shift handles the cases in which
476 the effective shift value is < BITS_PER_WORD. The arguments and return
477 value are the same as for the parent routine. */
480 expand_subword_shift (scalar_int_mode op1_mode
, optab binoptab
,
481 rtx outof_input
, rtx into_input
, rtx op1
,
482 rtx outof_target
, rtx into_target
,
483 int unsignedp
, enum optab_methods methods
,
484 unsigned HOST_WIDE_INT shift_mask
)
486 optab reverse_unsigned_shift
, unsigned_shift
;
489 reverse_unsigned_shift
= (binoptab
== ashl_optab
? lshr_optab
: ashl_optab
);
490 unsigned_shift
= (binoptab
== ashl_optab
? ashl_optab
: lshr_optab
);
492 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
493 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
494 the opposite direction to BINOPTAB. */
495 if (CONSTANT_P (op1
) || shift_mask
>= BITS_PER_WORD
)
497 carries
= outof_input
;
498 tmp
= immed_wide_int_const (wi::shwi (BITS_PER_WORD
,
499 op1_mode
), op1_mode
);
500 tmp
= simplify_expand_binop (op1_mode
, sub_optab
, tmp
, op1
,
505 /* We must avoid shifting by BITS_PER_WORD bits since that is either
506 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
507 has unknown behavior. Do a single shift first, then shift by the
508 remainder. It's OK to use ~OP1 as the remainder if shift counts
509 are truncated to the mode size. */
510 carries
= expand_binop (word_mode
, reverse_unsigned_shift
,
511 outof_input
, const1_rtx
, 0, unsignedp
, methods
);
512 if (shift_mask
== BITS_PER_WORD
- 1)
514 tmp
= immed_wide_int_const
515 (wi::minus_one (GET_MODE_PRECISION (op1_mode
)), op1_mode
);
516 tmp
= simplify_expand_binop (op1_mode
, xor_optab
, op1
, tmp
,
521 tmp
= immed_wide_int_const (wi::shwi (BITS_PER_WORD
- 1,
522 op1_mode
), op1_mode
);
523 tmp
= simplify_expand_binop (op1_mode
, sub_optab
, tmp
, op1
,
527 if (tmp
== 0 || carries
== 0)
529 carries
= expand_binop (word_mode
, reverse_unsigned_shift
,
530 carries
, tmp
, 0, unsignedp
, methods
);
534 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
535 so the result can go directly into INTO_TARGET if convenient. */
536 tmp
= expand_binop (word_mode
, unsigned_shift
, into_input
, op1
,
537 into_target
, unsignedp
, methods
);
541 /* Now OR in the bits carried over from OUTOF_INPUT. */
542 if (!force_expand_binop (word_mode
, ior_optab
, tmp
, carries
,
543 into_target
, unsignedp
, methods
))
546 /* Use a standard word_mode shift for the out-of half. */
547 if (outof_target
!= 0)
548 if (!force_expand_binop (word_mode
, binoptab
, outof_input
, op1
,
549 outof_target
, unsignedp
, methods
))
556 /* Try implementing expand_doubleword_shift using conditional moves.
557 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
558 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
559 are the shift counts to use in the former and latter case. All other
560 arguments are the same as the parent routine. */
563 expand_doubleword_shift_condmove (scalar_int_mode op1_mode
, optab binoptab
,
564 enum rtx_code cmp_code
, rtx cmp1
, rtx cmp2
,
565 rtx outof_input
, rtx into_input
,
566 rtx subword_op1
, rtx superword_op1
,
567 rtx outof_target
, rtx into_target
,
568 int unsignedp
, enum optab_methods methods
,
569 unsigned HOST_WIDE_INT shift_mask
)
571 rtx outof_superword
, into_superword
;
573 /* Put the superword version of the output into OUTOF_SUPERWORD and
575 outof_superword
= outof_target
!= 0 ? gen_reg_rtx (word_mode
) : 0;
576 if (outof_target
!= 0 && subword_op1
== superword_op1
)
578 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
579 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
580 into_superword
= outof_target
;
581 if (!expand_superword_shift (binoptab
, outof_input
, superword_op1
,
582 outof_superword
, 0, unsignedp
, methods
))
587 into_superword
= gen_reg_rtx (word_mode
);
588 if (!expand_superword_shift (binoptab
, outof_input
, superword_op1
,
589 outof_superword
, into_superword
,
594 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
595 if (!expand_subword_shift (op1_mode
, binoptab
,
596 outof_input
, into_input
, subword_op1
,
597 outof_target
, into_target
,
598 unsignedp
, methods
, shift_mask
))
601 /* Select between them. Do the INTO half first because INTO_SUPERWORD
602 might be the current value of OUTOF_TARGET. */
603 if (!emit_conditional_move (into_target
, cmp_code
, cmp1
, cmp2
, op1_mode
,
604 into_target
, into_superword
, word_mode
, false))
607 if (outof_target
!= 0)
608 if (!emit_conditional_move (outof_target
, cmp_code
, cmp1
, cmp2
, op1_mode
,
609 outof_target
, outof_superword
,
616 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
617 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
618 input operand; the shift moves bits in the direction OUTOF_INPUT->
619 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
620 of the target. OP1 is the shift count and OP1_MODE is its mode.
621 If OP1 is constant, it will have been truncated as appropriate
622 and is known to be nonzero.
624 If SHIFT_MASK is zero, the result of word shifts is undefined when the
625 shift count is outside the range [0, BITS_PER_WORD). This routine must
626 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
628 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
629 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
630 fill with zeros or sign bits as appropriate.
632 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
633 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
634 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
635 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
638 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
639 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
640 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
641 function wants to calculate it itself.
643 Return true if the shift could be successfully synthesized. */
646 expand_doubleword_shift (scalar_int_mode op1_mode
, optab binoptab
,
647 rtx outof_input
, rtx into_input
, rtx op1
,
648 rtx outof_target
, rtx into_target
,
649 int unsignedp
, enum optab_methods methods
,
650 unsigned HOST_WIDE_INT shift_mask
)
652 rtx superword_op1
, tmp
, cmp1
, cmp2
;
653 enum rtx_code cmp_code
;
655 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
656 fill the result with sign or zero bits as appropriate. If so, the value
657 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
658 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
659 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
661 This isn't worthwhile for constant shifts since the optimizers will
662 cope better with in-range shift counts. */
663 if (shift_mask
>= BITS_PER_WORD
665 && !CONSTANT_P (op1
))
667 if (!expand_doubleword_shift (op1_mode
, binoptab
,
668 outof_input
, into_input
, op1
,
670 unsignedp
, methods
, shift_mask
))
672 if (!force_expand_binop (word_mode
, binoptab
, outof_input
, op1
,
673 outof_target
, unsignedp
, methods
))
678 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
679 is true when the effective shift value is less than BITS_PER_WORD.
680 Set SUPERWORD_OP1 to the shift count that should be used to shift
681 OUTOF_INPUT into INTO_TARGET when the condition is false. */
682 tmp
= immed_wide_int_const (wi::shwi (BITS_PER_WORD
, op1_mode
), op1_mode
);
683 if (!CONSTANT_P (op1
) && shift_mask
== BITS_PER_WORD
- 1)
685 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
686 is a subword shift count. */
687 cmp1
= simplify_expand_binop (op1_mode
, and_optab
, op1
, tmp
,
689 cmp2
= CONST0_RTX (op1_mode
);
695 /* Set CMP1 to OP1 - BITS_PER_WORD. */
696 cmp1
= simplify_expand_binop (op1_mode
, sub_optab
, op1
, tmp
,
698 cmp2
= CONST0_RTX (op1_mode
);
700 superword_op1
= cmp1
;
705 /* If we can compute the condition at compile time, pick the
706 appropriate subroutine. */
707 tmp
= simplify_relational_operation (cmp_code
, SImode
, op1_mode
, cmp1
, cmp2
);
708 if (tmp
!= 0 && CONST_INT_P (tmp
))
710 if (tmp
== const0_rtx
)
711 return expand_superword_shift (binoptab
, outof_input
, superword_op1
,
712 outof_target
, into_target
,
715 return expand_subword_shift (op1_mode
, binoptab
,
716 outof_input
, into_input
, op1
,
717 outof_target
, into_target
,
718 unsignedp
, methods
, shift_mask
);
721 /* Try using conditional moves to generate straight-line code. */
722 if (HAVE_conditional_move
)
724 rtx_insn
*start
= get_last_insn ();
725 if (expand_doubleword_shift_condmove (op1_mode
, binoptab
,
726 cmp_code
, cmp1
, cmp2
,
727 outof_input
, into_input
,
729 outof_target
, into_target
,
730 unsignedp
, methods
, shift_mask
))
732 delete_insns_since (start
);
735 /* As a last resort, use branches to select the correct alternative. */
736 rtx_code_label
*subword_label
= gen_label_rtx ();
737 rtx_code_label
*done_label
= gen_label_rtx ();
740 do_compare_rtx_and_jump (cmp1
, cmp2
, cmp_code
, false, op1_mode
,
742 profile_probability::uninitialized ());
745 if (!expand_superword_shift (binoptab
, outof_input
, superword_op1
,
746 outof_target
, into_target
,
750 emit_jump_insn (targetm
.gen_jump (done_label
));
752 emit_label (subword_label
);
754 if (!expand_subword_shift (op1_mode
, binoptab
,
755 outof_input
, into_input
, op1
,
756 outof_target
, into_target
,
757 unsignedp
, methods
, shift_mask
))
760 emit_label (done_label
);
764 /* Subroutine of expand_binop. Perform a double word multiplication of
765 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
766 as the target's word_mode. This function return NULL_RTX if anything
767 goes wrong, in which case it may have already emitted instructions
768 which need to be deleted.
770 If we want to multiply two two-word values and have normal and widening
771 multiplies of single-word values, we can do this with three smaller
774 The multiplication proceeds as follows:
775 _______________________
776 [__op0_high_|__op0_low__]
777 _______________________
778 * [__op1_high_|__op1_low__]
779 _______________________________________________
780 _______________________
781 (1) [__op0_low__*__op1_low__]
782 _______________________
783 (2a) [__op0_low__*__op1_high_]
784 _______________________
785 (2b) [__op0_high_*__op1_low__]
786 _______________________
787 (3) [__op0_high_*__op1_high_]
790 This gives a 4-word result. Since we are only interested in the
791 lower 2 words, partial result (3) and the upper words of (2a) and
792 (2b) don't need to be calculated. Hence (2a) and (2b) can be
793 calculated using non-widening multiplication.
795 (1), however, needs to be calculated with an unsigned widening
796 multiplication. If this operation is not directly supported we
797 try using a signed widening multiplication and adjust the result.
798 This adjustment works as follows:
800 If both operands are positive then no adjustment is needed.
802 If the operands have different signs, for example op0_low < 0 and
803 op1_low >= 0, the instruction treats the most significant bit of
804 op0_low as a sign bit instead of a bit with significance
805 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
806 with 2**BITS_PER_WORD - op0_low, and two's complements the
807 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
810 Similarly, if both operands are negative, we need to add
811 (op0_low + op1_low) * 2**BITS_PER_WORD.
813 We use a trick to adjust quickly. We logically shift op0_low right
814 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
815 op0_high (op1_high) before it is used to calculate 2b (2a). If no
816 logical shift exists, we do an arithmetic right shift and subtract
820 expand_doubleword_mult (machine_mode mode
, rtx op0
, rtx op1
, rtx target
,
821 bool umulp
, enum optab_methods methods
)
823 int low
= (WORDS_BIG_ENDIAN
? 1 : 0);
824 int high
= (WORDS_BIG_ENDIAN
? 0 : 1);
825 rtx wordm1
= (umulp
? NULL_RTX
826 : gen_int_shift_amount (word_mode
, BITS_PER_WORD
- 1));
827 rtx product
, adjust
, product_high
, temp
;
829 rtx op0_high
= operand_subword_force (op0
, high
, mode
);
830 rtx op0_low
= operand_subword_force (op0
, low
, mode
);
831 rtx op1_high
= operand_subword_force (op1
, high
, mode
);
832 rtx op1_low
= operand_subword_force (op1
, low
, mode
);
834 /* If we're using an unsigned multiply to directly compute the product
835 of the low-order words of the operands and perform any required
836 adjustments of the operands, we begin by trying two more multiplications
837 and then computing the appropriate sum.
839 We have checked above that the required addition is provided.
840 Full-word addition will normally always succeed, especially if
841 it is provided at all, so we don't worry about its failure. The
842 multiplication may well fail, however, so we do handle that. */
846 /* ??? This could be done with emit_store_flag where available. */
847 temp
= expand_binop (word_mode
, lshr_optab
, op0_low
, wordm1
,
848 NULL_RTX
, 1, methods
);
850 op0_high
= expand_binop (word_mode
, add_optab
, op0_high
, temp
,
851 NULL_RTX
, 0, OPTAB_DIRECT
);
854 temp
= expand_binop (word_mode
, ashr_optab
, op0_low
, wordm1
,
855 NULL_RTX
, 0, methods
);
858 op0_high
= expand_binop (word_mode
, sub_optab
, op0_high
, temp
,
859 NULL_RTX
, 0, OPTAB_DIRECT
);
866 adjust
= expand_binop (word_mode
, smul_optab
, op0_high
, op1_low
,
867 NULL_RTX
, 0, OPTAB_DIRECT
);
871 /* OP0_HIGH should now be dead. */
875 /* ??? This could be done with emit_store_flag where available. */
876 temp
= expand_binop (word_mode
, lshr_optab
, op1_low
, wordm1
,
877 NULL_RTX
, 1, methods
);
879 op1_high
= expand_binop (word_mode
, add_optab
, op1_high
, temp
,
880 NULL_RTX
, 0, OPTAB_DIRECT
);
883 temp
= expand_binop (word_mode
, ashr_optab
, op1_low
, wordm1
,
884 NULL_RTX
, 0, methods
);
887 op1_high
= expand_binop (word_mode
, sub_optab
, op1_high
, temp
,
888 NULL_RTX
, 0, OPTAB_DIRECT
);
895 temp
= expand_binop (word_mode
, smul_optab
, op1_high
, op0_low
,
896 NULL_RTX
, 0, OPTAB_DIRECT
);
900 /* OP1_HIGH should now be dead. */
902 adjust
= expand_binop (word_mode
, add_optab
, adjust
, temp
,
903 NULL_RTX
, 0, OPTAB_DIRECT
);
905 if (target
&& !REG_P (target
))
908 /* *_widen_optab needs to determine operand mode, make sure at least
909 one operand has non-VOID mode. */
910 if (GET_MODE (op0_low
) == VOIDmode
&& GET_MODE (op1_low
) == VOIDmode
)
911 op0_low
= force_reg (word_mode
, op0_low
);
914 product
= expand_binop (mode
, umul_widen_optab
, op0_low
, op1_low
,
915 target
, 1, OPTAB_DIRECT
);
917 product
= expand_binop (mode
, smul_widen_optab
, op0_low
, op1_low
,
918 target
, 1, OPTAB_DIRECT
);
923 product_high
= operand_subword (product
, high
, 1, mode
);
924 adjust
= expand_binop (word_mode
, add_optab
, product_high
, adjust
,
925 NULL_RTX
, 0, OPTAB_DIRECT
);
926 emit_move_insn (product_high
, adjust
);
930 /* Wrapper around expand_binop which takes an rtx code to specify
931 the operation to perform, not an optab pointer. All other
932 arguments are the same. */
934 expand_simple_binop (machine_mode mode
, enum rtx_code code
, rtx op0
,
935 rtx op1
, rtx target
, int unsignedp
,
936 enum optab_methods methods
)
938 optab binop
= code_to_optab (code
);
941 return expand_binop (mode
, binop
, op0
, op1
, target
, unsignedp
, methods
);
944 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
945 binop. Order them according to commutative_operand_precedence and, if
946 possible, try to put TARGET or a pseudo first. */
948 swap_commutative_operands_with_target (rtx target
, rtx op0
, rtx op1
)
950 int op0_prec
= commutative_operand_precedence (op0
);
951 int op1_prec
= commutative_operand_precedence (op1
);
953 if (op0_prec
< op1_prec
)
956 if (op0_prec
> op1_prec
)
959 /* With equal precedence, both orders are ok, but it is better if the
960 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
961 if (target
== 0 || REG_P (target
))
962 return (REG_P (op1
) && !REG_P (op0
)) || target
== op1
;
964 return rtx_equal_p (op1
, target
);
967 /* Return true if BINOPTAB implements a shift operation. */
970 shift_optab_p (optab binoptab
)
972 switch (optab_to_code (binoptab
))
988 /* Return true if BINOPTAB implements a commutative binary operation. */
991 commutative_optab_p (optab binoptab
)
993 return (GET_RTX_CLASS (optab_to_code (binoptab
)) == RTX_COMM_ARITH
994 || binoptab
== smul_widen_optab
995 || binoptab
== umul_widen_optab
996 || binoptab
== smul_highpart_optab
997 || binoptab
== umul_highpart_optab
);
1000 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1001 optimizing, and if the operand is a constant that costs more than
1002 1 instruction, force the constant into a register and return that
1003 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1006 avoid_expensive_constant (machine_mode mode
, optab binoptab
,
1007 int opn
, rtx x
, bool unsignedp
)
1009 bool speed
= optimize_insn_for_speed_p ();
1011 if (mode
!= VOIDmode
1014 && (rtx_cost (x
, mode
, optab_to_code (binoptab
), opn
, speed
)
1015 > set_src_cost (x
, mode
, speed
)))
1017 if (CONST_INT_P (x
))
1019 HOST_WIDE_INT intval
= trunc_int_for_mode (INTVAL (x
), mode
);
1020 if (intval
!= INTVAL (x
))
1021 x
= GEN_INT (intval
);
1024 x
= convert_modes (mode
, VOIDmode
, x
, unsignedp
);
1025 x
= force_reg (mode
, x
);
1030 /* Helper function for expand_binop: handle the case where there
1031 is an insn ICODE that directly implements the indicated operation.
1032 Returns null if this is not possible. */
1034 expand_binop_directly (enum insn_code icode
, machine_mode mode
, optab binoptab
,
1036 rtx target
, int unsignedp
, enum optab_methods methods
,
1039 machine_mode xmode0
= insn_data
[(int) icode
].operand
[1].mode
;
1040 machine_mode xmode1
= insn_data
[(int) icode
].operand
[2].mode
;
1041 machine_mode mode0
, mode1
, tmp_mode
;
1042 class expand_operand ops
[3];
1045 rtx xop0
= op0
, xop1
= op1
;
1046 bool canonicalize_op1
= false;
1048 /* If it is a commutative operator and the modes would match
1049 if we would swap the operands, we can save the conversions. */
1050 commutative_p
= commutative_optab_p (binoptab
);
1052 && GET_MODE (xop0
) != xmode0
&& GET_MODE (xop1
) != xmode1
1053 && GET_MODE (xop0
) == xmode1
&& GET_MODE (xop1
) == xmode0
)
1054 std::swap (xop0
, xop1
);
1056 /* If we are optimizing, force expensive constants into a register. */
1057 xop0
= avoid_expensive_constant (xmode0
, binoptab
, 0, xop0
, unsignedp
);
1058 if (!shift_optab_p (binoptab
))
1059 xop1
= avoid_expensive_constant (xmode1
, binoptab
, 1, xop1
, unsignedp
);
1061 /* Shifts and rotates often use a different mode for op1 from op0;
1062 for VOIDmode constants we don't know the mode, so force it
1063 to be canonicalized using convert_modes. */
1064 canonicalize_op1
= true;
1066 /* In case the insn wants input operands in modes different from
1067 those of the actual operands, convert the operands. It would
1068 seem that we don't need to convert CONST_INTs, but we do, so
1069 that they're properly zero-extended, sign-extended or truncated
1072 mode0
= GET_MODE (xop0
) != VOIDmode
? GET_MODE (xop0
) : mode
;
1073 if (xmode0
!= VOIDmode
&& xmode0
!= mode0
)
1075 xop0
= convert_modes (xmode0
, mode0
, xop0
, unsignedp
);
1079 mode1
= ((GET_MODE (xop1
) != VOIDmode
|| canonicalize_op1
)
1080 ? GET_MODE (xop1
) : mode
);
1081 if (xmode1
!= VOIDmode
&& xmode1
!= mode1
)
1083 xop1
= convert_modes (xmode1
, mode1
, xop1
, unsignedp
);
1087 /* If operation is commutative,
1088 try to make the first operand a register.
1089 Even better, try to make it the same as the target.
1090 Also try to make the last operand a constant. */
1092 && swap_commutative_operands_with_target (target
, xop0
, xop1
))
1093 std::swap (xop0
, xop1
);
1095 /* Now, if insn's predicates don't allow our operands, put them into
1098 if (binoptab
== vec_pack_trunc_optab
1099 || binoptab
== vec_pack_usat_optab
1100 || binoptab
== vec_pack_ssat_optab
1101 || binoptab
== vec_pack_ufix_trunc_optab
1102 || binoptab
== vec_pack_sfix_trunc_optab
1103 || binoptab
== vec_packu_float_optab
1104 || binoptab
== vec_packs_float_optab
)
1106 /* The mode of the result is different then the mode of the
1108 tmp_mode
= insn_data
[(int) icode
].operand
[0].mode
;
1109 if (VECTOR_MODE_P (mode
)
1110 && maybe_ne (GET_MODE_NUNITS (tmp_mode
), 2 * GET_MODE_NUNITS (mode
)))
1112 delete_insns_since (last
);
1119 create_output_operand (&ops
[0], target
, tmp_mode
);
1120 create_input_operand (&ops
[1], xop0
, mode0
);
1121 create_input_operand (&ops
[2], xop1
, mode1
);
1122 pat
= maybe_gen_insn (icode
, 3, ops
);
1125 /* If PAT is composed of more than one insn, try to add an appropriate
1126 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1127 operand, call expand_binop again, this time without a target. */
1128 if (INSN_P (pat
) && NEXT_INSN (pat
) != NULL_RTX
1129 && ! add_equal_note (pat
, ops
[0].value
,
1130 optab_to_code (binoptab
),
1131 ops
[1].value
, ops
[2].value
, mode0
))
1133 delete_insns_since (last
);
1134 return expand_binop (mode
, binoptab
, op0
, op1
, NULL_RTX
,
1135 unsignedp
, methods
);
1139 return ops
[0].value
;
1141 delete_insns_since (last
);
1145 /* Generate code to perform an operation specified by BINOPTAB
1146 on operands OP0 and OP1, with result having machine-mode MODE.
1148 UNSIGNEDP is for the case where we have to widen the operands
1149 to perform the operation. It says to use zero-extension.
1151 If TARGET is nonzero, the value
1152 is generated there, if it is convenient to do so.
1153 In all cases an rtx is returned for the locus of the value;
1154 this may or may not be TARGET. */
1157 expand_binop (machine_mode mode
, optab binoptab
, rtx op0
, rtx op1
,
1158 rtx target
, int unsignedp
, enum optab_methods methods
)
1160 enum optab_methods next_methods
1161 = (methods
== OPTAB_LIB
|| methods
== OPTAB_LIB_WIDEN
1162 ? OPTAB_WIDEN
: methods
);
1163 enum mode_class mclass
;
1164 enum insn_code icode
;
1165 machine_mode wider_mode
;
1166 scalar_int_mode int_mode
;
1169 rtx_insn
*entry_last
= get_last_insn ();
1172 mclass
= GET_MODE_CLASS (mode
);
1174 /* If subtracting an integer constant, convert this into an addition of
1175 the negated constant. */
1177 if (binoptab
== sub_optab
&& CONST_INT_P (op1
))
1179 op1
= negate_rtx (mode
, op1
);
1180 binoptab
= add_optab
;
1182 /* For shifts, constant invalid op1 might be expanded from different
1183 mode than MODE. As those are invalid, force them to a register
1184 to avoid further problems during expansion. */
1185 else if (CONST_INT_P (op1
)
1186 && shift_optab_p (binoptab
)
1187 && UINTVAL (op1
) >= GET_MODE_BITSIZE (GET_MODE_INNER (mode
)))
1189 op1
= gen_int_mode (INTVAL (op1
), GET_MODE_INNER (mode
));
1190 op1
= force_reg (GET_MODE_INNER (mode
), op1
);
1193 /* Record where to delete back to if we backtrack. */
1194 last
= get_last_insn ();
1196 /* If we can do it with a three-operand insn, do so. */
1198 if (methods
!= OPTAB_MUST_WIDEN
)
1200 if (convert_optab_p (binoptab
))
1202 machine_mode from_mode
= widened_mode (mode
, op0
, op1
);
1203 icode
= find_widening_optab_handler (binoptab
, mode
, from_mode
);
1206 icode
= optab_handler (binoptab
, mode
);
1207 if (icode
!= CODE_FOR_nothing
)
1209 temp
= expand_binop_directly (icode
, mode
, binoptab
, op0
, op1
,
1210 target
, unsignedp
, methods
, last
);
1216 /* If we were trying to rotate, and that didn't work, try rotating
1217 the other direction before falling back to shifts and bitwise-or. */
1218 if (((binoptab
== rotl_optab
1219 && (icode
= optab_handler (rotr_optab
, mode
)) != CODE_FOR_nothing
)
1220 || (binoptab
== rotr_optab
1221 && (icode
= optab_handler (rotl_optab
, mode
)) != CODE_FOR_nothing
))
1222 && is_int_mode (mode
, &int_mode
))
1224 optab otheroptab
= (binoptab
== rotl_optab
? rotr_optab
: rotl_optab
);
1226 unsigned int bits
= GET_MODE_PRECISION (int_mode
);
1228 if (CONST_INT_P (op1
))
1229 newop1
= gen_int_shift_amount (int_mode
, bits
- INTVAL (op1
));
1230 else if (targetm
.shift_truncation_mask (int_mode
) == bits
- 1)
1231 newop1
= negate_rtx (GET_MODE (op1
), op1
);
1233 newop1
= expand_binop (GET_MODE (op1
), sub_optab
,
1234 gen_int_mode (bits
, GET_MODE (op1
)), op1
,
1235 NULL_RTX
, unsignedp
, OPTAB_DIRECT
);
1237 temp
= expand_binop_directly (icode
, int_mode
, otheroptab
, op0
, newop1
,
1238 target
, unsignedp
, methods
, last
);
1243 /* If this is a multiply, see if we can do a widening operation that
1244 takes operands of this mode and makes a wider mode. */
1246 if (binoptab
== smul_optab
1247 && GET_MODE_2XWIDER_MODE (mode
).exists (&wider_mode
)
1248 && (convert_optab_handler ((unsignedp
1250 : smul_widen_optab
),
1251 wider_mode
, mode
) != CODE_FOR_nothing
))
1253 /* *_widen_optab needs to determine operand mode, make sure at least
1254 one operand has non-VOID mode. */
1255 if (GET_MODE (op0
) == VOIDmode
&& GET_MODE (op1
) == VOIDmode
)
1256 op0
= force_reg (mode
, op0
);
1257 temp
= expand_binop (wider_mode
,
1258 unsignedp
? umul_widen_optab
: smul_widen_optab
,
1259 op0
, op1
, NULL_RTX
, unsignedp
, OPTAB_DIRECT
);
1263 if (GET_MODE_CLASS (mode
) == MODE_INT
1264 && TRULY_NOOP_TRUNCATION_MODES_P (mode
, GET_MODE (temp
)))
1265 return gen_lowpart (mode
, temp
);
1267 return convert_to_mode (mode
, temp
, unsignedp
);
1271 /* If this is a vector shift by a scalar, see if we can do a vector
1272 shift by a vector. If so, broadcast the scalar into a vector. */
1273 if (mclass
== MODE_VECTOR_INT
)
1275 optab otheroptab
= unknown_optab
;
1277 if (binoptab
== ashl_optab
)
1278 otheroptab
= vashl_optab
;
1279 else if (binoptab
== ashr_optab
)
1280 otheroptab
= vashr_optab
;
1281 else if (binoptab
== lshr_optab
)
1282 otheroptab
= vlshr_optab
;
1283 else if (binoptab
== rotl_optab
)
1284 otheroptab
= vrotl_optab
;
1285 else if (binoptab
== rotr_optab
)
1286 otheroptab
= vrotr_optab
;
1289 && (icode
= optab_handler (otheroptab
, mode
)) != CODE_FOR_nothing
)
1291 /* The scalar may have been extended to be too wide. Truncate
1292 it back to the proper size to fit in the broadcast vector. */
1293 scalar_mode inner_mode
= GET_MODE_INNER (mode
);
1294 if (!CONST_INT_P (op1
)
1295 && (GET_MODE_BITSIZE (as_a
<scalar_int_mode
> (GET_MODE (op1
)))
1296 > GET_MODE_BITSIZE (inner_mode
)))
1297 op1
= force_reg (inner_mode
,
1298 simplify_gen_unary (TRUNCATE
, inner_mode
, op1
,
1300 rtx vop1
= expand_vector_broadcast (mode
, op1
);
1303 temp
= expand_binop_directly (icode
, mode
, otheroptab
, op0
, vop1
,
1304 target
, unsignedp
, methods
, last
);
1311 /* Look for a wider mode of the same class for which we think we
1312 can open-code the operation. Check for a widening multiply at the
1313 wider mode as well. */
1315 if (CLASS_HAS_WIDER_MODES_P (mclass
)
1316 && methods
!= OPTAB_DIRECT
&& methods
!= OPTAB_LIB
)
1317 FOR_EACH_WIDER_MODE (wider_mode
, mode
)
1319 machine_mode next_mode
;
1320 if (optab_handler (binoptab
, wider_mode
) != CODE_FOR_nothing
1321 || (binoptab
== smul_optab
1322 && GET_MODE_WIDER_MODE (wider_mode
).exists (&next_mode
)
1323 && (find_widening_optab_handler ((unsignedp
1325 : smul_widen_optab
),
1327 != CODE_FOR_nothing
)))
1329 rtx xop0
= op0
, xop1
= op1
;
1332 /* For certain integer operations, we need not actually extend
1333 the narrow operands, as long as we will truncate
1334 the results to the same narrowness. */
1336 if ((binoptab
== ior_optab
|| binoptab
== and_optab
1337 || binoptab
== xor_optab
1338 || binoptab
== add_optab
|| binoptab
== sub_optab
1339 || binoptab
== smul_optab
|| binoptab
== ashl_optab
)
1340 && mclass
== MODE_INT
)
1343 xop0
= avoid_expensive_constant (mode
, binoptab
, 0,
1345 if (binoptab
!= ashl_optab
)
1346 xop1
= avoid_expensive_constant (mode
, binoptab
, 1,
1350 xop0
= widen_operand (xop0
, wider_mode
, mode
, unsignedp
, no_extend
);
1352 /* The second operand of a shift must always be extended. */
1353 xop1
= widen_operand (xop1
, wider_mode
, mode
, unsignedp
,
1354 no_extend
&& binoptab
!= ashl_optab
);
1356 temp
= expand_binop (wider_mode
, binoptab
, xop0
, xop1
, NULL_RTX
,
1357 unsignedp
, OPTAB_DIRECT
);
1360 if (mclass
!= MODE_INT
1361 || !TRULY_NOOP_TRUNCATION_MODES_P (mode
, wider_mode
))
1364 target
= gen_reg_rtx (mode
);
1365 convert_move (target
, temp
, 0);
1369 return gen_lowpart (mode
, temp
);
1372 delete_insns_since (last
);
1376 /* If operation is commutative,
1377 try to make the first operand a register.
1378 Even better, try to make it the same as the target.
1379 Also try to make the last operand a constant. */
1380 if (commutative_optab_p (binoptab
)
1381 && swap_commutative_operands_with_target (target
, op0
, op1
))
1382 std::swap (op0
, op1
);
1384 /* These can be done a word at a time. */
1385 if ((binoptab
== and_optab
|| binoptab
== ior_optab
|| binoptab
== xor_optab
)
1386 && is_int_mode (mode
, &int_mode
)
1387 && GET_MODE_SIZE (int_mode
) > UNITS_PER_WORD
1388 && optab_handler (binoptab
, word_mode
) != CODE_FOR_nothing
)
1393 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1394 won't be accurate, so use a new target. */
1398 || reg_overlap_mentioned_p (target
, op0
)
1399 || reg_overlap_mentioned_p (target
, op1
)
1400 || !valid_multiword_target_p (target
))
1401 target
= gen_reg_rtx (int_mode
);
1405 /* Do the actual arithmetic. */
1406 machine_mode op0_mode
= GET_MODE (op0
);
1407 machine_mode op1_mode
= GET_MODE (op1
);
1408 if (op0_mode
== VOIDmode
)
1409 op0_mode
= int_mode
;
1410 if (op1_mode
== VOIDmode
)
1411 op1_mode
= int_mode
;
1412 for (i
= 0; i
< GET_MODE_BITSIZE (int_mode
) / BITS_PER_WORD
; i
++)
1414 rtx target_piece
= operand_subword (target
, i
, 1, int_mode
);
1415 rtx x
= expand_binop (word_mode
, binoptab
,
1416 operand_subword_force (op0
, i
, op0_mode
),
1417 operand_subword_force (op1
, i
, op1_mode
),
1418 target_piece
, unsignedp
, next_methods
);
1423 if (target_piece
!= x
)
1424 emit_move_insn (target_piece
, x
);
1427 insns
= get_insns ();
1430 if (i
== GET_MODE_BITSIZE (int_mode
) / BITS_PER_WORD
)
1437 /* Synthesize double word shifts from single word shifts. */
1438 if ((binoptab
== lshr_optab
|| binoptab
== ashl_optab
1439 || binoptab
== ashr_optab
)
1440 && is_int_mode (mode
, &int_mode
)
1441 && (CONST_INT_P (op1
) || optimize_insn_for_speed_p ())
1442 && GET_MODE_SIZE (int_mode
) == 2 * UNITS_PER_WORD
1443 && GET_MODE_PRECISION (int_mode
) == GET_MODE_BITSIZE (int_mode
)
1444 && optab_handler (binoptab
, word_mode
) != CODE_FOR_nothing
1445 && optab_handler (ashl_optab
, word_mode
) != CODE_FOR_nothing
1446 && optab_handler (lshr_optab
, word_mode
) != CODE_FOR_nothing
)
1448 unsigned HOST_WIDE_INT shift_mask
, double_shift_mask
;
1449 scalar_int_mode op1_mode
;
1451 double_shift_mask
= targetm
.shift_truncation_mask (int_mode
);
1452 shift_mask
= targetm
.shift_truncation_mask (word_mode
);
1453 op1_mode
= (GET_MODE (op1
) != VOIDmode
1454 ? as_a
<scalar_int_mode
> (GET_MODE (op1
))
1457 /* Apply the truncation to constant shifts. */
1458 if (double_shift_mask
> 0 && CONST_INT_P (op1
))
1459 op1
= gen_int_mode (INTVAL (op1
) & double_shift_mask
, op1_mode
);
1461 if (op1
== CONST0_RTX (op1_mode
))
1464 /* Make sure that this is a combination that expand_doubleword_shift
1465 can handle. See the comments there for details. */
1466 if (double_shift_mask
== 0
1467 || (shift_mask
== BITS_PER_WORD
- 1
1468 && double_shift_mask
== BITS_PER_WORD
* 2 - 1))
1471 rtx into_target
, outof_target
;
1472 rtx into_input
, outof_input
;
1473 int left_shift
, outof_word
;
1475 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1476 won't be accurate, so use a new target. */
1480 || reg_overlap_mentioned_p (target
, op0
)
1481 || reg_overlap_mentioned_p (target
, op1
)
1482 || !valid_multiword_target_p (target
))
1483 target
= gen_reg_rtx (int_mode
);
1487 /* OUTOF_* is the word we are shifting bits away from, and
1488 INTO_* is the word that we are shifting bits towards, thus
1489 they differ depending on the direction of the shift and
1490 WORDS_BIG_ENDIAN. */
1492 left_shift
= binoptab
== ashl_optab
;
1493 outof_word
= left_shift
^ ! WORDS_BIG_ENDIAN
;
1495 outof_target
= operand_subword (target
, outof_word
, 1, int_mode
);
1496 into_target
= operand_subword (target
, 1 - outof_word
, 1, int_mode
);
1498 outof_input
= operand_subword_force (op0
, outof_word
, int_mode
);
1499 into_input
= operand_subword_force (op0
, 1 - outof_word
, int_mode
);
1501 if (expand_doubleword_shift (op1_mode
, binoptab
,
1502 outof_input
, into_input
, op1
,
1503 outof_target
, into_target
,
1504 unsignedp
, next_methods
, shift_mask
))
1506 insns
= get_insns ();
1516 /* Synthesize double word rotates from single word shifts. */
1517 if ((binoptab
== rotl_optab
|| binoptab
== rotr_optab
)
1518 && is_int_mode (mode
, &int_mode
)
1519 && CONST_INT_P (op1
)
1520 && GET_MODE_PRECISION (int_mode
) == 2 * BITS_PER_WORD
1521 && optab_handler (ashl_optab
, word_mode
) != CODE_FOR_nothing
1522 && optab_handler (lshr_optab
, word_mode
) != CODE_FOR_nothing
)
1525 rtx into_target
, outof_target
;
1526 rtx into_input
, outof_input
;
1528 int shift_count
, left_shift
, outof_word
;
1530 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1531 won't be accurate, so use a new target. Do this also if target is not
1532 a REG, first because having a register instead may open optimization
1533 opportunities, and second because if target and op0 happen to be MEMs
1534 designating the same location, we would risk clobbering it too early
1535 in the code sequence we generate below. */
1540 || reg_overlap_mentioned_p (target
, op0
)
1541 || reg_overlap_mentioned_p (target
, op1
)
1542 || !valid_multiword_target_p (target
))
1543 target
= gen_reg_rtx (int_mode
);
1547 shift_count
= INTVAL (op1
);
1549 /* OUTOF_* is the word we are shifting bits away from, and
1550 INTO_* is the word that we are shifting bits towards, thus
1551 they differ depending on the direction of the shift and
1552 WORDS_BIG_ENDIAN. */
1554 left_shift
= (binoptab
== rotl_optab
);
1555 outof_word
= left_shift
^ ! WORDS_BIG_ENDIAN
;
1557 outof_target
= operand_subword (target
, outof_word
, 1, int_mode
);
1558 into_target
= operand_subword (target
, 1 - outof_word
, 1, int_mode
);
1560 outof_input
= operand_subword_force (op0
, outof_word
, int_mode
);
1561 into_input
= operand_subword_force (op0
, 1 - outof_word
, int_mode
);
1563 if (shift_count
== BITS_PER_WORD
)
1565 /* This is just a word swap. */
1566 emit_move_insn (outof_target
, into_input
);
1567 emit_move_insn (into_target
, outof_input
);
1572 rtx into_temp1
, into_temp2
, outof_temp1
, outof_temp2
;
1573 HOST_WIDE_INT first_shift_count
, second_shift_count
;
1574 optab reverse_unsigned_shift
, unsigned_shift
;
1576 reverse_unsigned_shift
= (left_shift
^ (shift_count
< BITS_PER_WORD
)
1577 ? lshr_optab
: ashl_optab
);
1579 unsigned_shift
= (left_shift
^ (shift_count
< BITS_PER_WORD
)
1580 ? ashl_optab
: lshr_optab
);
1582 if (shift_count
> BITS_PER_WORD
)
1584 first_shift_count
= shift_count
- BITS_PER_WORD
;
1585 second_shift_count
= 2 * BITS_PER_WORD
- shift_count
;
1589 first_shift_count
= BITS_PER_WORD
- shift_count
;
1590 second_shift_count
= shift_count
;
1592 rtx first_shift_count_rtx
1593 = gen_int_shift_amount (word_mode
, first_shift_count
);
1594 rtx second_shift_count_rtx
1595 = gen_int_shift_amount (word_mode
, second_shift_count
);
1597 into_temp1
= expand_binop (word_mode
, unsigned_shift
,
1598 outof_input
, first_shift_count_rtx
,
1599 NULL_RTX
, unsignedp
, next_methods
);
1600 into_temp2
= expand_binop (word_mode
, reverse_unsigned_shift
,
1601 into_input
, second_shift_count_rtx
,
1602 NULL_RTX
, unsignedp
, next_methods
);
1604 if (into_temp1
!= 0 && into_temp2
!= 0)
1605 inter
= expand_binop (word_mode
, ior_optab
, into_temp1
, into_temp2
,
1606 into_target
, unsignedp
, next_methods
);
1610 if (inter
!= 0 && inter
!= into_target
)
1611 emit_move_insn (into_target
, inter
);
1613 outof_temp1
= expand_binop (word_mode
, unsigned_shift
,
1614 into_input
, first_shift_count_rtx
,
1615 NULL_RTX
, unsignedp
, next_methods
);
1616 outof_temp2
= expand_binop (word_mode
, reverse_unsigned_shift
,
1617 outof_input
, second_shift_count_rtx
,
1618 NULL_RTX
, unsignedp
, next_methods
);
1620 if (inter
!= 0 && outof_temp1
!= 0 && outof_temp2
!= 0)
1621 inter
= expand_binop (word_mode
, ior_optab
,
1622 outof_temp1
, outof_temp2
,
1623 outof_target
, unsignedp
, next_methods
);
1625 if (inter
!= 0 && inter
!= outof_target
)
1626 emit_move_insn (outof_target
, inter
);
1629 insns
= get_insns ();
1639 /* These can be done a word at a time by propagating carries. */
1640 if ((binoptab
== add_optab
|| binoptab
== sub_optab
)
1641 && is_int_mode (mode
, &int_mode
)
1642 && GET_MODE_SIZE (int_mode
) >= 2 * UNITS_PER_WORD
1643 && optab_handler (binoptab
, word_mode
) != CODE_FOR_nothing
)
1646 optab otheroptab
= binoptab
== add_optab
? sub_optab
: add_optab
;
1647 const unsigned int nwords
= GET_MODE_BITSIZE (int_mode
) / BITS_PER_WORD
;
1648 rtx carry_in
= NULL_RTX
, carry_out
= NULL_RTX
;
1649 rtx xop0
, xop1
, xtarget
;
1651 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1652 value is one of those, use it. Otherwise, use 1 since it is the
1653 one easiest to get. */
1654 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1655 int normalizep
= STORE_FLAG_VALUE
;
1660 /* Prepare the operands. */
1661 xop0
= force_reg (int_mode
, op0
);
1662 xop1
= force_reg (int_mode
, op1
);
1664 xtarget
= gen_reg_rtx (int_mode
);
1666 if (target
== 0 || !REG_P (target
) || !valid_multiword_target_p (target
))
1669 /* Indicate for flow that the entire target reg is being set. */
1671 emit_clobber (xtarget
);
1673 /* Do the actual arithmetic. */
1674 for (i
= 0; i
< nwords
; i
++)
1676 int index
= (WORDS_BIG_ENDIAN
? nwords
- i
- 1 : i
);
1677 rtx target_piece
= operand_subword (xtarget
, index
, 1, int_mode
);
1678 rtx op0_piece
= operand_subword_force (xop0
, index
, int_mode
);
1679 rtx op1_piece
= operand_subword_force (xop1
, index
, int_mode
);
1682 /* Main add/subtract of the input operands. */
1683 x
= expand_binop (word_mode
, binoptab
,
1684 op0_piece
, op1_piece
,
1685 target_piece
, unsignedp
, next_methods
);
1691 /* Store carry from main add/subtract. */
1692 carry_out
= gen_reg_rtx (word_mode
);
1693 carry_out
= emit_store_flag_force (carry_out
,
1694 (binoptab
== add_optab
1697 word_mode
, 1, normalizep
);
1704 /* Add/subtract previous carry to main result. */
1705 newx
= expand_binop (word_mode
,
1706 normalizep
== 1 ? binoptab
: otheroptab
,
1708 NULL_RTX
, 1, next_methods
);
1712 /* Get out carry from adding/subtracting carry in. */
1713 rtx carry_tmp
= gen_reg_rtx (word_mode
);
1714 carry_tmp
= emit_store_flag_force (carry_tmp
,
1715 (binoptab
== add_optab
1718 word_mode
, 1, normalizep
);
1720 /* Logical-ior the two poss. carry together. */
1721 carry_out
= expand_binop (word_mode
, ior_optab
,
1722 carry_out
, carry_tmp
,
1723 carry_out
, 0, next_methods
);
1727 emit_move_insn (target_piece
, newx
);
1731 if (x
!= target_piece
)
1732 emit_move_insn (target_piece
, x
);
1735 carry_in
= carry_out
;
1738 if (i
== GET_MODE_BITSIZE (int_mode
) / (unsigned) BITS_PER_WORD
)
1740 if (optab_handler (mov_optab
, int_mode
) != CODE_FOR_nothing
1741 || ! rtx_equal_p (target
, xtarget
))
1743 rtx_insn
*temp
= emit_move_insn (target
, xtarget
);
1745 set_dst_reg_note (temp
, REG_EQUAL
,
1746 gen_rtx_fmt_ee (optab_to_code (binoptab
),
1747 int_mode
, copy_rtx (xop0
),
1758 delete_insns_since (last
);
1761 /* Attempt to synthesize double word multiplies using a sequence of word
1762 mode multiplications. We first attempt to generate a sequence using a
1763 more efficient unsigned widening multiply, and if that fails we then
1764 try using a signed widening multiply. */
1766 if (binoptab
== smul_optab
1767 && is_int_mode (mode
, &int_mode
)
1768 && GET_MODE_SIZE (int_mode
) == 2 * UNITS_PER_WORD
1769 && optab_handler (smul_optab
, word_mode
) != CODE_FOR_nothing
1770 && optab_handler (add_optab
, word_mode
) != CODE_FOR_nothing
)
1772 rtx product
= NULL_RTX
;
1773 if (convert_optab_handler (umul_widen_optab
, int_mode
, word_mode
)
1774 != CODE_FOR_nothing
)
1776 product
= expand_doubleword_mult (int_mode
, op0
, op1
, target
,
1779 delete_insns_since (last
);
1782 if (product
== NULL_RTX
1783 && (convert_optab_handler (smul_widen_optab
, int_mode
, word_mode
)
1784 != CODE_FOR_nothing
))
1786 product
= expand_doubleword_mult (int_mode
, op0
, op1
, target
,
1789 delete_insns_since (last
);
1792 if (product
!= NULL_RTX
)
1794 if (optab_handler (mov_optab
, int_mode
) != CODE_FOR_nothing
)
1796 rtx_insn
*move
= emit_move_insn (target
? target
: product
,
1798 set_dst_reg_note (move
,
1800 gen_rtx_fmt_ee (MULT
, int_mode
,
1803 target
? target
: product
);
1809 /* It can't be open-coded in this mode.
1810 Use a library call if one is available and caller says that's ok. */
1812 libfunc
= optab_libfunc (binoptab
, mode
);
1814 && (methods
== OPTAB_LIB
|| methods
== OPTAB_LIB_WIDEN
))
1818 machine_mode op1_mode
= mode
;
1823 if (shift_optab_p (binoptab
))
1825 op1_mode
= targetm
.libgcc_shift_count_mode ();
1826 /* Specify unsigned here,
1827 since negative shift counts are meaningless. */
1828 op1x
= convert_to_mode (op1_mode
, op1
, 1);
1831 if (GET_MODE (op0
) != VOIDmode
1832 && GET_MODE (op0
) != mode
)
1833 op0
= convert_to_mode (mode
, op0
, unsignedp
);
1835 /* Pass 1 for NO_QUEUE so we don't lose any increments
1836 if the libcall is cse'd or moved. */
1837 value
= emit_library_call_value (libfunc
,
1838 NULL_RTX
, LCT_CONST
, mode
,
1839 op0
, mode
, op1x
, op1_mode
);
1841 insns
= get_insns ();
1844 bool trapv
= trapv_binoptab_p (binoptab
);
1845 target
= gen_reg_rtx (mode
);
1846 emit_libcall_block_1 (insns
, target
, value
,
1848 : gen_rtx_fmt_ee (optab_to_code (binoptab
),
1849 mode
, op0
, op1
), trapv
);
1854 delete_insns_since (last
);
1856 /* It can't be done in this mode. Can we do it in a wider mode? */
1858 if (! (methods
== OPTAB_WIDEN
|| methods
== OPTAB_LIB_WIDEN
1859 || methods
== OPTAB_MUST_WIDEN
))
1861 /* Caller says, don't even try. */
1862 delete_insns_since (entry_last
);
1866 /* Compute the value of METHODS to pass to recursive calls.
1867 Don't allow widening to be tried recursively. */
1869 methods
= (methods
== OPTAB_LIB_WIDEN
? OPTAB_LIB
: OPTAB_DIRECT
);
1871 /* Look for a wider mode of the same class for which it appears we can do
1874 if (CLASS_HAS_WIDER_MODES_P (mclass
))
1876 /* This code doesn't make sense for conversion optabs, since we
1877 wouldn't then want to extend the operands to be the same size
1879 gcc_assert (!convert_optab_p (binoptab
));
1880 FOR_EACH_WIDER_MODE (wider_mode
, mode
)
1882 if (optab_handler (binoptab
, wider_mode
)
1883 || (methods
== OPTAB_LIB
1884 && optab_libfunc (binoptab
, wider_mode
)))
1886 rtx xop0
= op0
, xop1
= op1
;
1889 /* For certain integer operations, we need not actually extend
1890 the narrow operands, as long as we will truncate
1891 the results to the same narrowness. */
1893 if ((binoptab
== ior_optab
|| binoptab
== and_optab
1894 || binoptab
== xor_optab
1895 || binoptab
== add_optab
|| binoptab
== sub_optab
1896 || binoptab
== smul_optab
|| binoptab
== ashl_optab
)
1897 && mclass
== MODE_INT
)
1900 xop0
= widen_operand (xop0
, wider_mode
, mode
,
1901 unsignedp
, no_extend
);
1903 /* The second operand of a shift must always be extended. */
1904 xop1
= widen_operand (xop1
, wider_mode
, mode
, unsignedp
,
1905 no_extend
&& binoptab
!= ashl_optab
);
1907 temp
= expand_binop (wider_mode
, binoptab
, xop0
, xop1
, NULL_RTX
,
1908 unsignedp
, methods
);
1911 if (mclass
!= MODE_INT
1912 || !TRULY_NOOP_TRUNCATION_MODES_P (mode
, wider_mode
))
1915 target
= gen_reg_rtx (mode
);
1916 convert_move (target
, temp
, 0);
1920 return gen_lowpart (mode
, temp
);
1923 delete_insns_since (last
);
1928 delete_insns_since (entry_last
);
1932 /* Expand a binary operator which has both signed and unsigned forms.
1933 UOPTAB is the optab for unsigned operations, and SOPTAB is for
1936 If we widen unsigned operands, we may use a signed wider operation instead
1937 of an unsigned wider operation, since the result would be the same. */
1940 sign_expand_binop (machine_mode mode
, optab uoptab
, optab soptab
,
1941 rtx op0
, rtx op1
, rtx target
, int unsignedp
,
1942 enum optab_methods methods
)
1945 optab direct_optab
= unsignedp
? uoptab
: soptab
;
1948 /* Do it without widening, if possible. */
1949 temp
= expand_binop (mode
, direct_optab
, op0
, op1
, target
,
1950 unsignedp
, OPTAB_DIRECT
);
1951 if (temp
|| methods
== OPTAB_DIRECT
)
1954 /* Try widening to a signed int. Disable any direct use of any
1955 signed insn in the current mode. */
1956 save_enable
= swap_optab_enable (soptab
, mode
, false);
1958 temp
= expand_binop (mode
, soptab
, op0
, op1
, target
,
1959 unsignedp
, OPTAB_WIDEN
);
1961 /* For unsigned operands, try widening to an unsigned int. */
1962 if (!temp
&& unsignedp
)
1963 temp
= expand_binop (mode
, uoptab
, op0
, op1
, target
,
1964 unsignedp
, OPTAB_WIDEN
);
1965 if (temp
|| methods
== OPTAB_WIDEN
)
1968 /* Use the right width libcall if that exists. */
1969 temp
= expand_binop (mode
, direct_optab
, op0
, op1
, target
,
1970 unsignedp
, OPTAB_LIB
);
1971 if (temp
|| methods
== OPTAB_LIB
)
1974 /* Must widen and use a libcall, use either signed or unsigned. */
1975 temp
= expand_binop (mode
, soptab
, op0
, op1
, target
,
1976 unsignedp
, methods
);
1977 if (!temp
&& unsignedp
)
1978 temp
= expand_binop (mode
, uoptab
, op0
, op1
, target
,
1979 unsignedp
, methods
);
1982 /* Undo the fiddling above. */
1984 swap_optab_enable (soptab
, mode
, true);
1988 /* Generate code to perform an operation specified by UNOPPTAB
1989 on operand OP0, with two results to TARG0 and TARG1.
1990 We assume that the order of the operands for the instruction
1991 is TARG0, TARG1, OP0.
1993 Either TARG0 or TARG1 may be zero, but what that means is that
1994 the result is not actually wanted. We will generate it into
1995 a dummy pseudo-reg and discard it. They may not both be zero.
1997 Returns 1 if this operation can be performed; 0 if not. */
2000 expand_twoval_unop (optab unoptab
, rtx op0
, rtx targ0
, rtx targ1
,
2003 machine_mode mode
= GET_MODE (targ0
? targ0
: targ1
);
2004 enum mode_class mclass
;
2005 machine_mode wider_mode
;
2006 rtx_insn
*entry_last
= get_last_insn ();
2009 mclass
= GET_MODE_CLASS (mode
);
2012 targ0
= gen_reg_rtx (mode
);
2014 targ1
= gen_reg_rtx (mode
);
2016 /* Record where to go back to if we fail. */
2017 last
= get_last_insn ();
2019 if (optab_handler (unoptab
, mode
) != CODE_FOR_nothing
)
2021 class expand_operand ops
[3];
2022 enum insn_code icode
= optab_handler (unoptab
, mode
);
2024 create_fixed_operand (&ops
[0], targ0
);
2025 create_fixed_operand (&ops
[1], targ1
);
2026 create_convert_operand_from (&ops
[2], op0
, mode
, unsignedp
);
2027 if (maybe_expand_insn (icode
, 3, ops
))
2031 /* It can't be done in this mode. Can we do it in a wider mode? */
2033 if (CLASS_HAS_WIDER_MODES_P (mclass
))
2035 FOR_EACH_WIDER_MODE (wider_mode
, mode
)
2037 if (optab_handler (unoptab
, wider_mode
) != CODE_FOR_nothing
)
2039 rtx t0
= gen_reg_rtx (wider_mode
);
2040 rtx t1
= gen_reg_rtx (wider_mode
);
2041 rtx cop0
= convert_modes (wider_mode
, mode
, op0
, unsignedp
);
2043 if (expand_twoval_unop (unoptab
, cop0
, t0
, t1
, unsignedp
))
2045 convert_move (targ0
, t0
, unsignedp
);
2046 convert_move (targ1
, t1
, unsignedp
);
2050 delete_insns_since (last
);
2055 delete_insns_since (entry_last
);
2059 /* Generate code to perform an operation specified by BINOPTAB
2060 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2061 We assume that the order of the operands for the instruction
2062 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2063 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2065 Either TARG0 or TARG1 may be zero, but what that means is that
2066 the result is not actually wanted. We will generate it into
2067 a dummy pseudo-reg and discard it. They may not both be zero.
2069 Returns 1 if this operation can be performed; 0 if not. */
2072 expand_twoval_binop (optab binoptab
, rtx op0
, rtx op1
, rtx targ0
, rtx targ1
,
2075 machine_mode mode
= GET_MODE (targ0
? targ0
: targ1
);
2076 enum mode_class mclass
;
2077 machine_mode wider_mode
;
2078 rtx_insn
*entry_last
= get_last_insn ();
2081 mclass
= GET_MODE_CLASS (mode
);
2084 targ0
= gen_reg_rtx (mode
);
2086 targ1
= gen_reg_rtx (mode
);
2088 /* Record where to go back to if we fail. */
2089 last
= get_last_insn ();
2091 if (optab_handler (binoptab
, mode
) != CODE_FOR_nothing
)
2093 class expand_operand ops
[4];
2094 enum insn_code icode
= optab_handler (binoptab
, mode
);
2095 machine_mode mode0
= insn_data
[icode
].operand
[1].mode
;
2096 machine_mode mode1
= insn_data
[icode
].operand
[2].mode
;
2097 rtx xop0
= op0
, xop1
= op1
;
2099 /* If we are optimizing, force expensive constants into a register. */
2100 xop0
= avoid_expensive_constant (mode0
, binoptab
, 0, xop0
, unsignedp
);
2101 xop1
= avoid_expensive_constant (mode1
, binoptab
, 1, xop1
, unsignedp
);
2103 create_fixed_operand (&ops
[0], targ0
);
2104 create_convert_operand_from (&ops
[1], xop0
, mode
, unsignedp
);
2105 create_convert_operand_from (&ops
[2], xop1
, mode
, unsignedp
);
2106 create_fixed_operand (&ops
[3], targ1
);
2107 if (maybe_expand_insn (icode
, 4, ops
))
2109 delete_insns_since (last
);
2112 /* It can't be done in this mode. Can we do it in a wider mode? */
2114 if (CLASS_HAS_WIDER_MODES_P (mclass
))
2116 FOR_EACH_WIDER_MODE (wider_mode
, mode
)
2118 if (optab_handler (binoptab
, wider_mode
) != CODE_FOR_nothing
)
2120 rtx t0
= gen_reg_rtx (wider_mode
);
2121 rtx t1
= gen_reg_rtx (wider_mode
);
2122 rtx cop0
= convert_modes (wider_mode
, mode
, op0
, unsignedp
);
2123 rtx cop1
= convert_modes (wider_mode
, mode
, op1
, unsignedp
);
2125 if (expand_twoval_binop (binoptab
, cop0
, cop1
,
2128 convert_move (targ0
, t0
, unsignedp
);
2129 convert_move (targ1
, t1
, unsignedp
);
2133 delete_insns_since (last
);
2138 delete_insns_since (entry_last
);
2142 /* Expand the two-valued library call indicated by BINOPTAB, but
2143 preserve only one of the values. If TARG0 is non-NULL, the first
2144 value is placed into TARG0; otherwise the second value is placed
2145 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2146 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2147 This routine assumes that the value returned by the library call is
2148 as if the return value was of an integral mode twice as wide as the
2149 mode of OP0. Returns 1 if the call was successful. */
2152 expand_twoval_binop_libfunc (optab binoptab
, rtx op0
, rtx op1
,
2153 rtx targ0
, rtx targ1
, enum rtx_code code
)
2156 machine_mode libval_mode
;
2161 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2162 gcc_assert (!targ0
!= !targ1
);
2164 mode
= GET_MODE (op0
);
2165 libfunc
= optab_libfunc (binoptab
, mode
);
2169 /* The value returned by the library function will have twice as
2170 many bits as the nominal MODE. */
2171 libval_mode
= smallest_int_mode_for_size (2 * GET_MODE_BITSIZE (mode
));
2173 libval
= emit_library_call_value (libfunc
, NULL_RTX
, LCT_CONST
,
2177 /* Get the part of VAL containing the value that we want. */
2178 libval
= simplify_gen_subreg (mode
, libval
, libval_mode
,
2179 targ0
? 0 : GET_MODE_SIZE (mode
));
2180 insns
= get_insns ();
2182 /* Move the into the desired location. */
2183 emit_libcall_block (insns
, targ0
? targ0
: targ1
, libval
,
2184 gen_rtx_fmt_ee (code
, mode
, op0
, op1
));
2190 /* Wrapper around expand_unop which takes an rtx code to specify
2191 the operation to perform, not an optab pointer. All other
2192 arguments are the same. */
2194 expand_simple_unop (machine_mode mode
, enum rtx_code code
, rtx op0
,
2195 rtx target
, int unsignedp
)
2197 optab unop
= code_to_optab (code
);
2200 return expand_unop (mode
, unop
, op0
, target
, unsignedp
);
2206 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2208 A similar operation can be used for clrsb. UNOPTAB says which operation
2209 we are trying to expand. */
2211 widen_leading (scalar_int_mode mode
, rtx op0
, rtx target
, optab unoptab
)
2213 opt_scalar_int_mode wider_mode_iter
;
2214 FOR_EACH_WIDER_MODE (wider_mode_iter
, mode
)
2216 scalar_int_mode wider_mode
= wider_mode_iter
.require ();
2217 if (optab_handler (unoptab
, wider_mode
) != CODE_FOR_nothing
)
2222 last
= get_last_insn ();
2225 target
= gen_reg_rtx (mode
);
2226 xop0
= widen_operand (op0
, wider_mode
, mode
,
2227 unoptab
!= clrsb_optab
, false);
2228 temp
= expand_unop (wider_mode
, unoptab
, xop0
, NULL_RTX
,
2229 unoptab
!= clrsb_optab
);
2232 (wider_mode
, sub_optab
, temp
,
2233 gen_int_mode (GET_MODE_PRECISION (wider_mode
)
2234 - GET_MODE_PRECISION (mode
),
2236 target
, true, OPTAB_DIRECT
);
2238 delete_insns_since (last
);
2246 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2247 quantities, choosing which based on whether the high word is nonzero. */
2249 expand_doubleword_clz (scalar_int_mode mode
, rtx op0
, rtx target
)
2251 rtx xop0
= force_reg (mode
, op0
);
2252 rtx subhi
= gen_highpart (word_mode
, xop0
);
2253 rtx sublo
= gen_lowpart (word_mode
, xop0
);
2254 rtx_code_label
*hi0_label
= gen_label_rtx ();
2255 rtx_code_label
*after_label
= gen_label_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_clz* is int). */
2263 target
= gen_reg_rtx (word_mode
);
2265 /* In any case, write to a word_mode scratch in both branches of the
2266 conditional, so we can ensure there is a single move insn setting
2267 'target' to tag a REG_EQUAL note on. */
2268 result
= gen_reg_rtx (word_mode
);
2272 /* If the high word is not equal to zero,
2273 then clz of the full value is clz of the high word. */
2274 emit_cmp_and_jump_insns (subhi
, CONST0_RTX (word_mode
), EQ
, 0,
2275 word_mode
, true, hi0_label
);
2277 temp
= expand_unop_direct (word_mode
, clz_optab
, subhi
, result
, true);
2282 convert_move (result
, temp
, true);
2284 emit_jump_insn (targetm
.gen_jump (after_label
));
2287 /* Else clz of the full value is clz of the low word plus the number
2288 of bits in the high word. */
2289 emit_label (hi0_label
);
2291 temp
= expand_unop_direct (word_mode
, clz_optab
, sublo
, 0, true);
2294 temp
= expand_binop (word_mode
, add_optab
, temp
,
2295 gen_int_mode (GET_MODE_BITSIZE (word_mode
), word_mode
),
2296 result
, true, OPTAB_DIRECT
);
2300 convert_move (result
, temp
, true);
2302 emit_label (after_label
);
2303 convert_move (target
, result
, true);
2308 add_equal_note (seq
, target
, CLZ
, xop0
, NULL_RTX
, mode
);
2317 /* Try calculating popcount of a double-word quantity as two popcount's of
2318 word-sized quantities and summing up the results. */
2320 expand_doubleword_popcount (scalar_int_mode mode
, rtx op0
, rtx target
)
2327 t0
= expand_unop_direct (word_mode
, popcount_optab
,
2328 operand_subword_force (op0
, 0, mode
), NULL_RTX
,
2330 t1
= expand_unop_direct (word_mode
, popcount_optab
,
2331 operand_subword_force (op0
, 1, mode
), NULL_RTX
,
2339 /* If we were not given a target, use a word_mode register, not a
2340 'mode' register. The result will fit, and nobody is expecting
2341 anything bigger (the return type of __builtin_popcount* is int). */
2343 target
= gen_reg_rtx (word_mode
);
2345 t
= expand_binop (word_mode
, add_optab
, t0
, t1
, target
, 0, OPTAB_DIRECT
);
2350 add_equal_note (seq
, t
, POPCOUNT
, op0
, NULL_RTX
, mode
);
2358 (parity:narrow (low (x) ^ high (x))) */
2360 expand_doubleword_parity (scalar_int_mode mode
, rtx op0
, rtx target
)
2362 rtx t
= expand_binop (word_mode
, xor_optab
,
2363 operand_subword_force (op0
, 0, mode
),
2364 operand_subword_force (op0
, 1, mode
),
2365 NULL_RTX
, 0, OPTAB_DIRECT
);
2366 return expand_unop (word_mode
, parity_optab
, t
, target
, true);
2372 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2374 widen_bswap (scalar_int_mode mode
, rtx op0
, rtx target
)
2378 opt_scalar_int_mode wider_mode_iter
;
2380 FOR_EACH_WIDER_MODE (wider_mode_iter
, mode
)
2381 if (optab_handler (bswap_optab
, wider_mode_iter
.require ())
2382 != CODE_FOR_nothing
)
2385 if (!wider_mode_iter
.exists ())
2388 scalar_int_mode wider_mode
= wider_mode_iter
.require ();
2389 last
= get_last_insn ();
2391 x
= widen_operand (op0
, wider_mode
, mode
, true, true);
2392 x
= expand_unop (wider_mode
, bswap_optab
, x
, NULL_RTX
, true);
2394 gcc_assert (GET_MODE_PRECISION (wider_mode
) == GET_MODE_BITSIZE (wider_mode
)
2395 && GET_MODE_PRECISION (mode
) == GET_MODE_BITSIZE (mode
));
2397 x
= expand_shift (RSHIFT_EXPR
, wider_mode
, x
,
2398 GET_MODE_BITSIZE (wider_mode
)
2399 - GET_MODE_BITSIZE (mode
),
2405 target
= gen_reg_rtx (mode
);
2406 emit_move_insn (target
, gen_lowpart (mode
, x
));
2409 delete_insns_since (last
);
2414 /* Try calculating bswap as two bswaps of two word-sized operands. */
2417 expand_doubleword_bswap (machine_mode mode
, rtx op
, rtx target
)
2421 t1
= expand_unop (word_mode
, bswap_optab
,
2422 operand_subword_force (op
, 0, mode
), NULL_RTX
, true);
2423 t0
= expand_unop (word_mode
, bswap_optab
,
2424 operand_subword_force (op
, 1, mode
), NULL_RTX
, true);
2426 if (target
== 0 || !valid_multiword_target_p (target
))
2427 target
= gen_reg_rtx (mode
);
2429 emit_clobber (target
);
2430 emit_move_insn (operand_subword (target
, 0, 1, mode
), t0
);
2431 emit_move_insn (operand_subword (target
, 1, 1, mode
), t1
);
2436 /* Try calculating (parity x) as (and (popcount x) 1), where
2437 popcount can also be done in a wider mode. */
2439 expand_parity (scalar_int_mode mode
, rtx op0
, rtx target
)
2441 enum mode_class mclass
= GET_MODE_CLASS (mode
);
2442 opt_scalar_int_mode wider_mode_iter
;
2443 FOR_EACH_MODE_FROM (wider_mode_iter
, mode
)
2445 scalar_int_mode wider_mode
= wider_mode_iter
.require ();
2446 if (optab_handler (popcount_optab
, wider_mode
) != CODE_FOR_nothing
)
2451 last
= get_last_insn ();
2453 if (target
== 0 || GET_MODE (target
) != wider_mode
)
2454 target
= gen_reg_rtx (wider_mode
);
2456 xop0
= widen_operand (op0
, wider_mode
, mode
, true, false);
2457 temp
= expand_unop (wider_mode
, popcount_optab
, xop0
, NULL_RTX
,
2460 temp
= expand_binop (wider_mode
, and_optab
, temp
, const1_rtx
,
2461 target
, true, OPTAB_DIRECT
);
2465 if (mclass
!= MODE_INT
2466 || !TRULY_NOOP_TRUNCATION_MODES_P (mode
, wider_mode
))
2467 return convert_to_mode (mode
, temp
, 0);
2469 return gen_lowpart (mode
, temp
);
2472 delete_insns_since (last
);
2478 /* Try calculating ctz(x) as K - clz(x & -x) ,
2479 where K is GET_MODE_PRECISION(mode) - 1.
2481 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2482 don't have to worry about what the hardware does in that case. (If
2483 the clz instruction produces the usual value at 0, which is K, the
2484 result of this code sequence will be -1; expand_ffs, below, relies
2485 on this. It might be nice to have it be K instead, for consistency
2486 with the (very few) processors that provide a ctz with a defined
2487 value, but that would take one more instruction, and it would be
2488 less convenient for expand_ffs anyway. */
2491 expand_ctz (scalar_int_mode mode
, rtx op0
, rtx target
)
2496 if (optab_handler (clz_optab
, mode
) == CODE_FOR_nothing
)
2501 temp
= expand_unop_direct (mode
, neg_optab
, op0
, NULL_RTX
, true);
2503 temp
= expand_binop (mode
, and_optab
, op0
, temp
, NULL_RTX
,
2504 true, OPTAB_DIRECT
);
2506 temp
= expand_unop_direct (mode
, clz_optab
, temp
, NULL_RTX
, true);
2508 temp
= expand_binop (mode
, sub_optab
,
2509 gen_int_mode (GET_MODE_PRECISION (mode
) - 1, mode
),
2511 true, OPTAB_DIRECT
);
2521 add_equal_note (seq
, temp
, CTZ
, op0
, NULL_RTX
, mode
);
2527 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2528 else with the sequence used by expand_clz.
2530 The ffs builtin promises to return zero for a zero value and ctz/clz
2531 may have an undefined value in that case. If they do not give us a
2532 convenient value, we have to generate a test and branch. */
2534 expand_ffs (scalar_int_mode mode
, rtx op0
, rtx target
)
2536 HOST_WIDE_INT val
= 0;
2537 bool defined_at_zero
= false;
2541 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
)
2545 temp
= expand_unop_direct (mode
, ctz_optab
, op0
, 0, true);
2549 defined_at_zero
= (CTZ_DEFINED_VALUE_AT_ZERO (mode
, val
) == 2);
2551 else if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
)
2554 temp
= expand_ctz (mode
, op0
, 0);
2558 if (CLZ_DEFINED_VALUE_AT_ZERO (mode
, val
) == 2)
2560 defined_at_zero
= true;
2561 val
= (GET_MODE_PRECISION (mode
) - 1) - val
;
2567 if (defined_at_zero
&& val
== -1)
2568 /* No correction needed at zero. */;
2571 /* We don't try to do anything clever with the situation found
2572 on some processors (eg Alpha) where ctz(0:mode) ==
2573 bitsize(mode). If someone can think of a way to send N to -1
2574 and leave alone all values in the range 0..N-1 (where N is a
2575 power of two), cheaper than this test-and-branch, please add it.
2577 The test-and-branch is done after the operation itself, in case
2578 the operation sets condition codes that can be recycled for this.
2579 (This is true on i386, for instance.) */
2581 rtx_code_label
*nonzero_label
= gen_label_rtx ();
2582 emit_cmp_and_jump_insns (op0
, CONST0_RTX (mode
), NE
, 0,
2583 mode
, true, nonzero_label
);
2585 convert_move (temp
, GEN_INT (-1), false);
2586 emit_label (nonzero_label
);
2589 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2590 to produce a value in the range 0..bitsize. */
2591 temp
= expand_binop (mode
, add_optab
, temp
, gen_int_mode (1, mode
),
2592 target
, false, OPTAB_DIRECT
);
2599 add_equal_note (seq
, temp
, FFS
, op0
, NULL_RTX
, mode
);
2608 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2609 conditions, VAL may already be a SUBREG against which we cannot generate
2610 a further SUBREG. In this case, we expect forcing the value into a
2611 register will work around the situation. */
2614 lowpart_subreg_maybe_copy (machine_mode omode
, rtx val
,
2618 ret
= lowpart_subreg (omode
, val
, imode
);
2621 val
= force_reg (imode
, val
);
2622 ret
= lowpart_subreg (omode
, val
, imode
);
2623 gcc_assert (ret
!= NULL
);
2628 /* Expand a floating point absolute value or negation operation via a
2629 logical operation on the sign bit. */
2632 expand_absneg_bit (enum rtx_code code
, scalar_float_mode mode
,
2633 rtx op0
, rtx target
)
2635 const struct real_format
*fmt
;
2636 int bitpos
, word
, nwords
, i
;
2637 scalar_int_mode imode
;
2641 /* The format has to have a simple sign bit. */
2642 fmt
= REAL_MODE_FORMAT (mode
);
2646 bitpos
= fmt
->signbit_rw
;
2650 /* Don't create negative zeros if the format doesn't support them. */
2651 if (code
== NEG
&& !fmt
->has_signed_zero
)
2654 if (GET_MODE_SIZE (mode
) <= UNITS_PER_WORD
)
2656 if (!int_mode_for_mode (mode
).exists (&imode
))
2665 if (FLOAT_WORDS_BIG_ENDIAN
)
2666 word
= (GET_MODE_BITSIZE (mode
) - bitpos
) / BITS_PER_WORD
;
2668 word
= bitpos
/ BITS_PER_WORD
;
2669 bitpos
= bitpos
% BITS_PER_WORD
;
2670 nwords
= (GET_MODE_BITSIZE (mode
) + BITS_PER_WORD
- 1) / BITS_PER_WORD
;
2673 wide_int mask
= wi::set_bit_in_zero (bitpos
, GET_MODE_PRECISION (imode
));
2679 || reg_overlap_mentioned_p (target
, op0
)
2680 || (nwords
> 1 && !valid_multiword_target_p (target
)))
2681 target
= gen_reg_rtx (mode
);
2687 for (i
= 0; i
< nwords
; ++i
)
2689 rtx targ_piece
= operand_subword (target
, i
, 1, mode
);
2690 rtx op0_piece
= operand_subword_force (op0
, i
, mode
);
2694 temp
= expand_binop (imode
, code
== ABS
? and_optab
: xor_optab
,
2696 immed_wide_int_const (mask
, imode
),
2697 targ_piece
, 1, OPTAB_LIB_WIDEN
);
2698 if (temp
!= targ_piece
)
2699 emit_move_insn (targ_piece
, temp
);
2702 emit_move_insn (targ_piece
, op0_piece
);
2705 insns
= get_insns ();
2712 temp
= expand_binop (imode
, code
== ABS
? and_optab
: xor_optab
,
2713 gen_lowpart (imode
, op0
),
2714 immed_wide_int_const (mask
, imode
),
2715 gen_lowpart (imode
, target
), 1, OPTAB_LIB_WIDEN
);
2716 target
= lowpart_subreg_maybe_copy (mode
, temp
, imode
);
2718 set_dst_reg_note (get_last_insn (), REG_EQUAL
,
2719 gen_rtx_fmt_e (code
, mode
, copy_rtx (op0
)),
2726 /* As expand_unop, but will fail rather than attempt the operation in a
2727 different mode or with a libcall. */
2729 expand_unop_direct (machine_mode mode
, optab unoptab
, rtx op0
, rtx target
,
2732 if (optab_handler (unoptab
, mode
) != CODE_FOR_nothing
)
2734 class expand_operand ops
[2];
2735 enum insn_code icode
= optab_handler (unoptab
, mode
);
2736 rtx_insn
*last
= get_last_insn ();
2739 create_output_operand (&ops
[0], target
, mode
);
2740 create_convert_operand_from (&ops
[1], op0
, mode
, unsignedp
);
2741 pat
= maybe_gen_insn (icode
, 2, ops
);
2744 if (INSN_P (pat
) && NEXT_INSN (pat
) != NULL_RTX
2745 && ! add_equal_note (pat
, ops
[0].value
,
2746 optab_to_code (unoptab
),
2747 ops
[1].value
, NULL_RTX
, mode
))
2749 delete_insns_since (last
);
2750 return expand_unop (mode
, unoptab
, op0
, NULL_RTX
, unsignedp
);
2755 return ops
[0].value
;
2761 /* Generate code to perform an operation specified by UNOPTAB
2762 on operand OP0, with result having machine-mode MODE.
2764 UNSIGNEDP is for the case where we have to widen the operands
2765 to perform the operation. It says to use zero-extension.
2767 If TARGET is nonzero, the value
2768 is generated there, if it is convenient to do so.
2769 In all cases an rtx is returned for the locus of the value;
2770 this may or may not be TARGET. */
2773 expand_unop (machine_mode mode
, optab unoptab
, rtx op0
, rtx target
,
2776 enum mode_class mclass
= GET_MODE_CLASS (mode
);
2777 machine_mode wider_mode
;
2778 scalar_int_mode int_mode
;
2779 scalar_float_mode float_mode
;
2783 temp
= expand_unop_direct (mode
, unoptab
, op0
, target
, unsignedp
);
2787 /* It can't be done in this mode. Can we open-code it in a wider mode? */
2789 /* Widening (or narrowing) clz needs special treatment. */
2790 if (unoptab
== clz_optab
)
2792 if (is_a
<scalar_int_mode
> (mode
, &int_mode
))
2794 temp
= widen_leading (int_mode
, op0
, target
, unoptab
);
2798 if (GET_MODE_SIZE (int_mode
) == 2 * UNITS_PER_WORD
2799 && optab_handler (unoptab
, word_mode
) != CODE_FOR_nothing
)
2801 temp
= expand_doubleword_clz (int_mode
, op0
, target
);
2810 if (unoptab
== clrsb_optab
)
2812 if (is_a
<scalar_int_mode
> (mode
, &int_mode
))
2814 temp
= widen_leading (int_mode
, op0
, target
, unoptab
);
2821 if (unoptab
== popcount_optab
2822 && is_a
<scalar_int_mode
> (mode
, &int_mode
)
2823 && GET_MODE_SIZE (int_mode
) == 2 * UNITS_PER_WORD
2824 && optab_handler (unoptab
, word_mode
) != CODE_FOR_nothing
2825 && optimize_insn_for_speed_p ())
2827 temp
= expand_doubleword_popcount (int_mode
, op0
, target
);
2832 if (unoptab
== parity_optab
2833 && is_a
<scalar_int_mode
> (mode
, &int_mode
)
2834 && GET_MODE_SIZE (int_mode
) == 2 * UNITS_PER_WORD
2835 && (optab_handler (unoptab
, word_mode
) != CODE_FOR_nothing
2836 || optab_handler (popcount_optab
, word_mode
) != CODE_FOR_nothing
)
2837 && optimize_insn_for_speed_p ())
2839 temp
= expand_doubleword_parity (int_mode
, op0
, target
);
2844 /* Widening (or narrowing) bswap needs special treatment. */
2845 if (unoptab
== bswap_optab
)
2847 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
2848 or ROTATERT. First try these directly; if this fails, then try the
2849 obvious pair of shifts with allowed widening, as this will probably
2850 be always more efficient than the other fallback methods. */
2856 if (optab_handler (rotl_optab
, mode
) != CODE_FOR_nothing
)
2858 temp
= expand_binop (mode
, rotl_optab
, op0
,
2859 gen_int_shift_amount (mode
, 8),
2860 target
, unsignedp
, OPTAB_DIRECT
);
2865 if (optab_handler (rotr_optab
, mode
) != CODE_FOR_nothing
)
2867 temp
= expand_binop (mode
, rotr_optab
, op0
,
2868 gen_int_shift_amount (mode
, 8),
2869 target
, unsignedp
, OPTAB_DIRECT
);
2874 last
= get_last_insn ();
2876 temp1
= expand_binop (mode
, ashl_optab
, op0
,
2877 gen_int_shift_amount (mode
, 8), NULL_RTX
,
2878 unsignedp
, OPTAB_WIDEN
);
2879 temp2
= expand_binop (mode
, lshr_optab
, op0
,
2880 gen_int_shift_amount (mode
, 8), NULL_RTX
,
2881 unsignedp
, OPTAB_WIDEN
);
2884 temp
= expand_binop (mode
, ior_optab
, temp1
, temp2
, target
,
2885 unsignedp
, OPTAB_WIDEN
);
2890 delete_insns_since (last
);
2893 if (is_a
<scalar_int_mode
> (mode
, &int_mode
))
2895 temp
= widen_bswap (int_mode
, op0
, target
);
2899 /* We do not provide a 128-bit bswap in libgcc so force the use of
2900 a double bswap for 64-bit targets. */
2901 if (GET_MODE_SIZE (int_mode
) == 2 * UNITS_PER_WORD
2902 && (UNITS_PER_WORD
== 8
2903 || optab_handler (unoptab
, word_mode
) != CODE_FOR_nothing
))
2905 temp
= expand_doubleword_bswap (mode
, op0
, target
);
2914 if (CLASS_HAS_WIDER_MODES_P (mclass
))
2915 FOR_EACH_WIDER_MODE (wider_mode
, mode
)
2917 if (optab_handler (unoptab
, wider_mode
) != CODE_FOR_nothing
)
2920 rtx_insn
*last
= get_last_insn ();
2922 /* For certain operations, we need not actually extend
2923 the narrow operand, as long as we will truncate the
2924 results to the same narrowness. */
2926 xop0
= widen_operand (xop0
, wider_mode
, mode
, unsignedp
,
2927 (unoptab
== neg_optab
2928 || unoptab
== one_cmpl_optab
)
2929 && mclass
== MODE_INT
);
2931 temp
= expand_unop (wider_mode
, unoptab
, xop0
, NULL_RTX
,
2936 if (mclass
!= MODE_INT
2937 || !TRULY_NOOP_TRUNCATION_MODES_P (mode
, wider_mode
))
2940 target
= gen_reg_rtx (mode
);
2941 convert_move (target
, temp
, 0);
2945 return gen_lowpart (mode
, temp
);
2948 delete_insns_since (last
);
2952 /* These can be done a word at a time. */
2953 if (unoptab
== one_cmpl_optab
2954 && is_int_mode (mode
, &int_mode
)
2955 && GET_MODE_SIZE (int_mode
) > UNITS_PER_WORD
2956 && optab_handler (unoptab
, word_mode
) != CODE_FOR_nothing
)
2963 || reg_overlap_mentioned_p (target
, op0
)
2964 || !valid_multiword_target_p (target
))
2965 target
= gen_reg_rtx (int_mode
);
2969 /* Do the actual arithmetic. */
2970 for (i
= 0; i
< GET_MODE_BITSIZE (int_mode
) / BITS_PER_WORD
; i
++)
2972 rtx target_piece
= operand_subword (target
, i
, 1, int_mode
);
2973 rtx x
= expand_unop (word_mode
, unoptab
,
2974 operand_subword_force (op0
, i
, int_mode
),
2975 target_piece
, unsignedp
);
2977 if (target_piece
!= x
)
2978 emit_move_insn (target_piece
, x
);
2981 insns
= get_insns ();
2988 /* Emit ~op0 as op0 ^ -1. */
2989 if (unoptab
== one_cmpl_optab
2990 && (SCALAR_INT_MODE_P (mode
) || GET_MODE_CLASS (mode
) == MODE_VECTOR_INT
)
2991 && optab_handler (xor_optab
, mode
) != CODE_FOR_nothing
)
2993 temp
= expand_binop (mode
, xor_optab
, op0
, CONSTM1_RTX (mode
),
2994 target
, unsignedp
, OPTAB_DIRECT
);
2999 if (optab_to_code (unoptab
) == NEG
)
3001 /* Try negating floating point values by flipping the sign bit. */
3002 if (is_a
<scalar_float_mode
> (mode
, &float_mode
))
3004 temp
= expand_absneg_bit (NEG
, float_mode
, op0
, target
);
3009 /* If there is no negation pattern, and we have no negative zero,
3010 try subtracting from zero. */
3011 if (!HONOR_SIGNED_ZEROS (mode
))
3013 temp
= expand_binop (mode
, (unoptab
== negv_optab
3014 ? subv_optab
: sub_optab
),
3015 CONST0_RTX (mode
), op0
, target
,
3016 unsignedp
, OPTAB_DIRECT
);
3022 /* Try calculating parity (x) as popcount (x) % 2. */
3023 if (unoptab
== parity_optab
&& is_a
<scalar_int_mode
> (mode
, &int_mode
))
3025 temp
= expand_parity (int_mode
, op0
, target
);
3030 /* Try implementing ffs (x) in terms of clz (x). */
3031 if (unoptab
== ffs_optab
&& is_a
<scalar_int_mode
> (mode
, &int_mode
))
3033 temp
= expand_ffs (int_mode
, op0
, target
);
3038 /* Try implementing ctz (x) in terms of clz (x). */
3039 if (unoptab
== ctz_optab
&& is_a
<scalar_int_mode
> (mode
, &int_mode
))
3041 temp
= expand_ctz (int_mode
, op0
, target
);
3047 /* Now try a library call in this mode. */
3048 libfunc
= optab_libfunc (unoptab
, mode
);
3054 machine_mode outmode
= mode
;
3056 /* All of these functions return small values. Thus we choose to
3057 have them return something that isn't a double-word. */
3058 if (unoptab
== ffs_optab
|| unoptab
== clz_optab
|| unoptab
== ctz_optab
3059 || unoptab
== clrsb_optab
|| unoptab
== popcount_optab
3060 || unoptab
== parity_optab
)
3062 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node
),
3063 optab_libfunc (unoptab
, mode
)));
3067 /* Pass 1 for NO_QUEUE so we don't lose any increments
3068 if the libcall is cse'd or moved. */
3069 value
= emit_library_call_value (libfunc
, NULL_RTX
, LCT_CONST
, outmode
,
3071 insns
= get_insns ();
3074 target
= gen_reg_rtx (outmode
);
3075 bool trapv
= trapv_unoptab_p (unoptab
);
3077 eq_value
= NULL_RTX
;
3080 eq_value
= gen_rtx_fmt_e (optab_to_code (unoptab
), mode
, op0
);
3081 if (GET_MODE_UNIT_SIZE (outmode
) < GET_MODE_UNIT_SIZE (mode
))
3082 eq_value
= simplify_gen_unary (TRUNCATE
, outmode
, eq_value
, mode
);
3083 else if (GET_MODE_UNIT_SIZE (outmode
) > GET_MODE_UNIT_SIZE (mode
))
3084 eq_value
= simplify_gen_unary (ZERO_EXTEND
,
3085 outmode
, eq_value
, mode
);
3087 emit_libcall_block_1 (insns
, target
, value
, eq_value
, trapv
);
3092 /* It can't be done in this mode. Can we do it in a wider mode? */
3094 if (CLASS_HAS_WIDER_MODES_P (mclass
))
3096 FOR_EACH_WIDER_MODE (wider_mode
, mode
)
3098 if (optab_handler (unoptab
, wider_mode
) != CODE_FOR_nothing
3099 || optab_libfunc (unoptab
, wider_mode
))
3102 rtx_insn
*last
= get_last_insn ();
3104 /* For certain operations, we need not actually extend
3105 the narrow operand, as long as we will truncate the
3106 results to the same narrowness. */
3107 xop0
= widen_operand (xop0
, wider_mode
, mode
, unsignedp
,
3108 (unoptab
== neg_optab
3109 || unoptab
== one_cmpl_optab
3110 || unoptab
== bswap_optab
)
3111 && mclass
== MODE_INT
);
3113 temp
= expand_unop (wider_mode
, unoptab
, xop0
, NULL_RTX
,
3116 /* If we are generating clz using wider mode, adjust the
3117 result. Similarly for clrsb. */
3118 if ((unoptab
== clz_optab
|| unoptab
== clrsb_optab
)
3121 scalar_int_mode wider_int_mode
3122 = as_a
<scalar_int_mode
> (wider_mode
);
3123 int_mode
= as_a
<scalar_int_mode
> (mode
);
3125 (wider_mode
, sub_optab
, temp
,
3126 gen_int_mode (GET_MODE_PRECISION (wider_int_mode
)
3127 - GET_MODE_PRECISION (int_mode
),
3129 target
, true, OPTAB_DIRECT
);
3132 /* Likewise for bswap. */
3133 if (unoptab
== bswap_optab
&& temp
!= 0)
3135 scalar_int_mode wider_int_mode
3136 = as_a
<scalar_int_mode
> (wider_mode
);
3137 int_mode
= as_a
<scalar_int_mode
> (mode
);
3138 gcc_assert (GET_MODE_PRECISION (wider_int_mode
)
3139 == GET_MODE_BITSIZE (wider_int_mode
)
3140 && GET_MODE_PRECISION (int_mode
)
3141 == GET_MODE_BITSIZE (int_mode
));
3143 temp
= expand_shift (RSHIFT_EXPR
, wider_int_mode
, temp
,
3144 GET_MODE_BITSIZE (wider_int_mode
)
3145 - GET_MODE_BITSIZE (int_mode
),
3151 if (mclass
!= MODE_INT
)
3154 target
= gen_reg_rtx (mode
);
3155 convert_move (target
, temp
, 0);
3159 return gen_lowpart (mode
, temp
);
3162 delete_insns_since (last
);
3167 /* One final attempt at implementing negation via subtraction,
3168 this time allowing widening of the operand. */
3169 if (optab_to_code (unoptab
) == NEG
&& !HONOR_SIGNED_ZEROS (mode
))
3172 temp
= expand_binop (mode
,
3173 unoptab
== negv_optab
? subv_optab
: sub_optab
,
3174 CONST0_RTX (mode
), op0
,
3175 target
, unsignedp
, OPTAB_LIB_WIDEN
);
3183 /* Emit code to compute the absolute value of OP0, with result to
3184 TARGET if convenient. (TARGET may be 0.) The return value says
3185 where the result actually is to be found.
3187 MODE is the mode of the operand; the mode of the result is
3188 different but can be deduced from MODE.
3193 expand_abs_nojump (machine_mode mode
, rtx op0
, rtx target
,
3194 int result_unsignedp
)
3198 if (GET_MODE_CLASS (mode
) != MODE_INT
3200 result_unsignedp
= 1;
3202 /* First try to do it with a special abs instruction. */
3203 temp
= expand_unop (mode
, result_unsignedp
? abs_optab
: absv_optab
,
3208 /* For floating point modes, try clearing the sign bit. */
3209 scalar_float_mode float_mode
;
3210 if (is_a
<scalar_float_mode
> (mode
, &float_mode
))
3212 temp
= expand_absneg_bit (ABS
, float_mode
, op0
, target
);
3217 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3218 if (optab_handler (smax_optab
, mode
) != CODE_FOR_nothing
3219 && !HONOR_SIGNED_ZEROS (mode
))
3221 rtx_insn
*last
= get_last_insn ();
3223 temp
= expand_unop (mode
, result_unsignedp
? neg_optab
: negv_optab
,
3226 temp
= expand_binop (mode
, smax_optab
, op0
, temp
, target
, 0,
3232 delete_insns_since (last
);
3235 /* If this machine has expensive jumps, we can do integer absolute
3236 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3237 where W is the width of MODE. */
3239 scalar_int_mode int_mode
;
3240 if (is_int_mode (mode
, &int_mode
)
3241 && BRANCH_COST (optimize_insn_for_speed_p (),
3244 rtx extended
= expand_shift (RSHIFT_EXPR
, int_mode
, op0
,
3245 GET_MODE_PRECISION (int_mode
) - 1,
3248 temp
= expand_binop (int_mode
, xor_optab
, extended
, op0
, target
, 0,
3251 temp
= expand_binop (int_mode
,
3252 result_unsignedp
? sub_optab
: subv_optab
,
3253 temp
, extended
, target
, 0, OPTAB_LIB_WIDEN
);
3263 expand_abs (machine_mode mode
, rtx op0
, rtx target
,
3264 int result_unsignedp
, int safe
)
3267 rtx_code_label
*op1
;
3269 if (GET_MODE_CLASS (mode
) != MODE_INT
3271 result_unsignedp
= 1;
3273 temp
= expand_abs_nojump (mode
, op0
, target
, result_unsignedp
);
3277 /* If that does not win, use conditional jump and negate. */
3279 /* It is safe to use the target if it is the same
3280 as the source if this is also a pseudo register */
3281 if (op0
== target
&& REG_P (op0
)
3282 && REGNO (op0
) >= FIRST_PSEUDO_REGISTER
)
3285 op1
= gen_label_rtx ();
3286 if (target
== 0 || ! safe
3287 || GET_MODE (target
) != mode
3288 || (MEM_P (target
) && MEM_VOLATILE_P (target
))
3290 && REGNO (target
) < FIRST_PSEUDO_REGISTER
))
3291 target
= gen_reg_rtx (mode
);
3293 emit_move_insn (target
, op0
);
3296 do_compare_rtx_and_jump (target
, CONST0_RTX (mode
), GE
, 0, mode
,
3297 NULL_RTX
, NULL
, op1
,
3298 profile_probability::uninitialized ());
3300 op0
= expand_unop (mode
, result_unsignedp
? neg_optab
: negv_optab
,
3303 emit_move_insn (target
, op0
);
3309 /* Emit code to compute the one's complement absolute value of OP0
3310 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3311 (TARGET may be NULL_RTX.) The return value says where the result
3312 actually is to be found.
3314 MODE is the mode of the operand; the mode of the result is
3315 different but can be deduced from MODE. */
3318 expand_one_cmpl_abs_nojump (machine_mode mode
, rtx op0
, rtx target
)
3322 /* Not applicable for floating point modes. */
3323 if (FLOAT_MODE_P (mode
))
3326 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3327 if (optab_handler (smax_optab
, mode
) != CODE_FOR_nothing
)
3329 rtx_insn
*last
= get_last_insn ();
3331 temp
= expand_unop (mode
, one_cmpl_optab
, op0
, NULL_RTX
, 0);
3333 temp
= expand_binop (mode
, smax_optab
, op0
, temp
, target
, 0,
3339 delete_insns_since (last
);
3342 /* If this machine has expensive jumps, we can do one's complement
3343 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3345 scalar_int_mode int_mode
;
3346 if (is_int_mode (mode
, &int_mode
)
3347 && BRANCH_COST (optimize_insn_for_speed_p (),
3350 rtx extended
= expand_shift (RSHIFT_EXPR
, int_mode
, op0
,
3351 GET_MODE_PRECISION (int_mode
) - 1,
3354 temp
= expand_binop (int_mode
, xor_optab
, extended
, op0
, target
, 0,
3364 /* A subroutine of expand_copysign, perform the copysign operation using the
3365 abs and neg primitives advertised to exist on the target. The assumption
3366 is that we have a split register file, and leaving op0 in fp registers,
3367 and not playing with subregs so much, will help the register allocator. */
3370 expand_copysign_absneg (scalar_float_mode mode
, rtx op0
, rtx op1
, rtx target
,
3371 int bitpos
, bool op0_is_abs
)
3373 scalar_int_mode imode
;
3374 enum insn_code icode
;
3376 rtx_code_label
*label
;
3381 /* Check if the back end provides an insn that handles signbit for the
3383 icode
= optab_handler (signbit_optab
, mode
);
3384 if (icode
!= CODE_FOR_nothing
)
3386 imode
= as_a
<scalar_int_mode
> (insn_data
[(int) icode
].operand
[0].mode
);
3387 sign
= gen_reg_rtx (imode
);
3388 emit_unop_insn (icode
, sign
, op1
, UNKNOWN
);
3392 if (GET_MODE_SIZE (mode
) <= UNITS_PER_WORD
)
3394 if (!int_mode_for_mode (mode
).exists (&imode
))
3396 op1
= gen_lowpart (imode
, op1
);
3403 if (FLOAT_WORDS_BIG_ENDIAN
)
3404 word
= (GET_MODE_BITSIZE (mode
) - bitpos
) / BITS_PER_WORD
;
3406 word
= bitpos
/ BITS_PER_WORD
;
3407 bitpos
= bitpos
% BITS_PER_WORD
;
3408 op1
= operand_subword_force (op1
, word
, mode
);
3411 wide_int mask
= wi::set_bit_in_zero (bitpos
, GET_MODE_PRECISION (imode
));
3412 sign
= expand_binop (imode
, and_optab
, op1
,
3413 immed_wide_int_const (mask
, imode
),
3414 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3419 op0
= expand_unop (mode
, abs_optab
, op0
, target
, 0);
3426 if (target
== NULL_RTX
)
3427 target
= copy_to_reg (op0
);
3429 emit_move_insn (target
, op0
);
3432 label
= gen_label_rtx ();
3433 emit_cmp_and_jump_insns (sign
, const0_rtx
, EQ
, NULL_RTX
, imode
, 1, label
);
3435 if (CONST_DOUBLE_AS_FLOAT_P (op0
))
3436 op0
= simplify_unary_operation (NEG
, mode
, op0
, mode
);
3438 op0
= expand_unop (mode
, neg_optab
, op0
, target
, 0);
3440 emit_move_insn (target
, op0
);
3448 /* A subroutine of expand_copysign, perform the entire copysign operation
3449 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3450 is true if op0 is known to have its sign bit clear. */
3453 expand_copysign_bit (scalar_float_mode mode
, rtx op0
, rtx op1
, rtx target
,
3454 int bitpos
, bool op0_is_abs
)
3456 scalar_int_mode imode
;
3457 int word
, nwords
, i
;
3461 if (GET_MODE_SIZE (mode
) <= UNITS_PER_WORD
)
3463 if (!int_mode_for_mode (mode
).exists (&imode
))
3472 if (FLOAT_WORDS_BIG_ENDIAN
)
3473 word
= (GET_MODE_BITSIZE (mode
) - bitpos
) / BITS_PER_WORD
;
3475 word
= bitpos
/ BITS_PER_WORD
;
3476 bitpos
= bitpos
% BITS_PER_WORD
;
3477 nwords
= (GET_MODE_BITSIZE (mode
) + BITS_PER_WORD
- 1) / BITS_PER_WORD
;
3480 wide_int mask
= wi::set_bit_in_zero (bitpos
, GET_MODE_PRECISION (imode
));
3485 || reg_overlap_mentioned_p (target
, op0
)
3486 || reg_overlap_mentioned_p (target
, op1
)
3487 || (nwords
> 1 && !valid_multiword_target_p (target
)))
3488 target
= gen_reg_rtx (mode
);
3494 for (i
= 0; i
< nwords
; ++i
)
3496 rtx targ_piece
= operand_subword (target
, i
, 1, mode
);
3497 rtx op0_piece
= operand_subword_force (op0
, i
, mode
);
3503 = expand_binop (imode
, and_optab
, op0_piece
,
3504 immed_wide_int_const (~mask
, imode
),
3505 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3506 op1
= expand_binop (imode
, and_optab
,
3507 operand_subword_force (op1
, i
, mode
),
3508 immed_wide_int_const (mask
, imode
),
3509 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3511 temp
= expand_binop (imode
, ior_optab
, op0_piece
, op1
,
3512 targ_piece
, 1, OPTAB_LIB_WIDEN
);
3513 if (temp
!= targ_piece
)
3514 emit_move_insn (targ_piece
, temp
);
3517 emit_move_insn (targ_piece
, op0_piece
);
3520 insns
= get_insns ();
3527 op1
= expand_binop (imode
, and_optab
, gen_lowpart (imode
, op1
),
3528 immed_wide_int_const (mask
, imode
),
3529 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3531 op0
= gen_lowpart (imode
, op0
);
3533 op0
= expand_binop (imode
, and_optab
, op0
,
3534 immed_wide_int_const (~mask
, imode
),
3535 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3537 temp
= expand_binop (imode
, ior_optab
, op0
, op1
,
3538 gen_lowpart (imode
, target
), 1, OPTAB_LIB_WIDEN
);
3539 target
= lowpart_subreg_maybe_copy (mode
, temp
, imode
);
3545 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3546 scalar floating point mode. Return NULL if we do not know how to
3547 expand the operation inline. */
3550 expand_copysign (rtx op0
, rtx op1
, rtx target
)
3552 scalar_float_mode mode
;
3553 const struct real_format
*fmt
;
3557 mode
= as_a
<scalar_float_mode
> (GET_MODE (op0
));
3558 gcc_assert (GET_MODE (op1
) == mode
);
3560 /* First try to do it with a special instruction. */
3561 temp
= expand_binop (mode
, copysign_optab
, op0
, op1
,
3562 target
, 0, OPTAB_DIRECT
);
3566 fmt
= REAL_MODE_FORMAT (mode
);
3567 if (fmt
== NULL
|| !fmt
->has_signed_zero
)
3571 if (CONST_DOUBLE_AS_FLOAT_P (op0
))
3573 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0
)))
3574 op0
= simplify_unary_operation (ABS
, mode
, op0
, mode
);
3578 if (fmt
->signbit_ro
>= 0
3579 && (CONST_DOUBLE_AS_FLOAT_P (op0
)
3580 || (optab_handler (neg_optab
, mode
) != CODE_FOR_nothing
3581 && optab_handler (abs_optab
, mode
) != CODE_FOR_nothing
)))
3583 temp
= expand_copysign_absneg (mode
, op0
, op1
, target
,
3584 fmt
->signbit_ro
, op0_is_abs
);
3589 if (fmt
->signbit_rw
< 0)
3591 return expand_copysign_bit (mode
, op0
, op1
, target
,
3592 fmt
->signbit_rw
, op0_is_abs
);
3595 /* Generate an instruction whose insn-code is INSN_CODE,
3596 with two operands: an output TARGET and an input OP0.
3597 TARGET *must* be nonzero, and the output is always stored there.
3598 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3599 the value that is stored into TARGET.
3601 Return false if expansion failed. */
3604 maybe_emit_unop_insn (enum insn_code icode
, rtx target
, rtx op0
,
3607 class expand_operand ops
[2];
3610 create_output_operand (&ops
[0], target
, GET_MODE (target
));
3611 create_input_operand (&ops
[1], op0
, GET_MODE (op0
));
3612 pat
= maybe_gen_insn (icode
, 2, ops
);
3616 if (INSN_P (pat
) && NEXT_INSN (pat
) != NULL_RTX
3618 add_equal_note (pat
, ops
[0].value
, code
, ops
[1].value
, NULL_RTX
,
3623 if (ops
[0].value
!= target
)
3624 emit_move_insn (target
, ops
[0].value
);
3627 /* Generate an instruction whose insn-code is INSN_CODE,
3628 with two operands: an output TARGET and an input OP0.
3629 TARGET *must* be nonzero, and the output is always stored there.
3630 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3631 the value that is stored into TARGET. */
3634 emit_unop_insn (enum insn_code icode
, rtx target
, rtx op0
, enum rtx_code code
)
3636 bool ok
= maybe_emit_unop_insn (icode
, target
, op0
, code
);
3640 struct no_conflict_data
3643 rtx_insn
*first
, *insn
;
3647 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3648 the currently examined clobber / store has to stay in the list of
3649 insns that constitute the actual libcall block. */
3651 no_conflict_move_test (rtx dest
, const_rtx set
, void *p0
)
3653 struct no_conflict_data
*p
= (struct no_conflict_data
*) p0
;
3655 /* If this inns directly contributes to setting the target, it must stay. */
3656 if (reg_overlap_mentioned_p (p
->target
, dest
))
3657 p
->must_stay
= true;
3658 /* If we haven't committed to keeping any other insns in the list yet,
3659 there is nothing more to check. */
3660 else if (p
->insn
== p
->first
)
3662 /* If this insn sets / clobbers a register that feeds one of the insns
3663 already in the list, this insn has to stay too. */
3664 else if (reg_overlap_mentioned_p (dest
, PATTERN (p
->first
))
3665 || (CALL_P (p
->first
) && (find_reg_fusage (p
->first
, USE
, dest
)))
3666 || reg_used_between_p (dest
, p
->first
, p
->insn
)
3667 /* Likewise if this insn depends on a register set by a previous
3668 insn in the list, or if it sets a result (presumably a hard
3669 register) that is set or clobbered by a previous insn.
3670 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3671 SET_DEST perform the former check on the address, and the latter
3672 check on the MEM. */
3673 || (GET_CODE (set
) == SET
3674 && (modified_in_p (SET_SRC (set
), p
->first
)
3675 || modified_in_p (SET_DEST (set
), p
->first
)
3676 || modified_between_p (SET_SRC (set
), p
->first
, p
->insn
)
3677 || modified_between_p (SET_DEST (set
), p
->first
, p
->insn
))))
3678 p
->must_stay
= true;
3682 /* Emit code to make a call to a constant function or a library call.
3684 INSNS is a list containing all insns emitted in the call.
3685 These insns leave the result in RESULT. Our block is to copy RESULT
3686 to TARGET, which is logically equivalent to EQUIV.
3688 We first emit any insns that set a pseudo on the assumption that these are
3689 loading constants into registers; doing so allows them to be safely cse'ed
3690 between blocks. Then we emit all the other insns in the block, followed by
3691 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3692 note with an operand of EQUIV. */
3695 emit_libcall_block_1 (rtx_insn
*insns
, rtx target
, rtx result
, rtx equiv
,
3696 bool equiv_may_trap
)
3698 rtx final_dest
= target
;
3699 rtx_insn
*next
, *last
, *insn
;
3701 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3702 into a MEM later. Protect the libcall block from this change. */
3703 if (! REG_P (target
) || REG_USERVAR_P (target
))
3704 target
= gen_reg_rtx (GET_MODE (target
));
3706 /* If we're using non-call exceptions, a libcall corresponding to an
3707 operation that may trap may also trap. */
3708 /* ??? See the comment in front of make_reg_eh_region_note. */
3709 if (cfun
->can_throw_non_call_exceptions
3710 && (equiv_may_trap
|| may_trap_p (equiv
)))
3712 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
3715 rtx note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
3718 int lp_nr
= INTVAL (XEXP (note
, 0));
3719 if (lp_nr
== 0 || lp_nr
== INT_MIN
)
3720 remove_note (insn
, note
);
3726 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3727 reg note to indicate that this call cannot throw or execute a nonlocal
3728 goto (unless there is already a REG_EH_REGION note, in which case
3730 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
3732 make_reg_eh_region_note_nothrow_nononlocal (insn
);
3735 /* First emit all insns that set pseudos. Remove them from the list as
3736 we go. Avoid insns that set pseudos which were referenced in previous
3737 insns. These can be generated by move_by_pieces, for example,
3738 to update an address. Similarly, avoid insns that reference things
3739 set in previous insns. */
3741 for (insn
= insns
; insn
; insn
= next
)
3743 rtx set
= single_set (insn
);
3745 next
= NEXT_INSN (insn
);
3747 if (set
!= 0 && REG_P (SET_DEST (set
))
3748 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
)
3750 struct no_conflict_data data
;
3752 data
.target
= const0_rtx
;
3756 note_stores (insn
, no_conflict_move_test
, &data
);
3757 if (! data
.must_stay
)
3759 if (PREV_INSN (insn
))
3760 SET_NEXT_INSN (PREV_INSN (insn
)) = next
;
3765 SET_PREV_INSN (next
) = PREV_INSN (insn
);
3771 /* Some ports use a loop to copy large arguments onto the stack.
3772 Don't move anything outside such a loop. */
3777 /* Write the remaining insns followed by the final copy. */
3778 for (insn
= insns
; insn
; insn
= next
)
3780 next
= NEXT_INSN (insn
);
3785 last
= emit_move_insn (target
, result
);
3787 set_dst_reg_note (last
, REG_EQUAL
, copy_rtx (equiv
), target
);
3789 if (final_dest
!= target
)
3790 emit_move_insn (final_dest
, target
);
3794 emit_libcall_block (rtx_insn
*insns
, rtx target
, rtx result
, rtx equiv
)
3796 emit_libcall_block_1 (insns
, target
, result
, equiv
, false);
3799 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3800 PURPOSE describes how this comparison will be used. CODE is the rtx
3801 comparison code we will be using.
3803 ??? Actually, CODE is slightly weaker than that. A target is still
3804 required to implement all of the normal bcc operations, but not
3805 required to implement all (or any) of the unordered bcc operations. */
3808 can_compare_p (enum rtx_code code
, machine_mode mode
,
3809 enum can_compare_purpose purpose
)
3812 test
= gen_rtx_fmt_ee (code
, mode
, const0_rtx
, const0_rtx
);
3815 enum insn_code icode
;
3817 if (purpose
== ccp_jump
3818 && (icode
= optab_handler (cbranch_optab
, mode
)) != CODE_FOR_nothing
3819 && insn_operand_matches (icode
, 0, test
))
3821 if (purpose
== ccp_store_flag
3822 && (icode
= optab_handler (cstore_optab
, mode
)) != CODE_FOR_nothing
3823 && insn_operand_matches (icode
, 1, test
))
3825 if (purpose
== ccp_cmov
3826 && optab_handler (cmov_optab
, mode
) != CODE_FOR_nothing
)
3829 mode
= GET_MODE_WIDER_MODE (mode
).else_void ();
3830 PUT_MODE (test
, mode
);
3832 while (mode
!= VOIDmode
);
3837 /* Return whether the backend can emit a vector comparison for code CODE,
3838 comparing operands of mode CMP_OP_MODE and producing a result with
3842 can_vcond_compare_p (enum rtx_code code
, machine_mode value_mode
,
3843 machine_mode cmp_op_mode
)
3845 enum insn_code icode
;
3846 bool unsigned_p
= (code
== LTU
|| code
== LEU
|| code
== GTU
|| code
== GEU
);
3847 rtx reg1
= alloca_raw_REG (cmp_op_mode
, LAST_VIRTUAL_REGISTER
+ 1);
3848 rtx reg2
= alloca_raw_REG (cmp_op_mode
, LAST_VIRTUAL_REGISTER
+ 2);
3849 rtx test
= alloca_rtx_fmt_ee (code
, value_mode
, reg1
, reg2
);
3851 return (icode
= get_vcond_icode (value_mode
, cmp_op_mode
, unsigned_p
))
3853 && insn_operand_matches (icode
, 3, test
);
3856 /* Return whether the backend can emit vector set instructions for inserting
3857 element into vector at variable index position. */
3860 can_vec_set_var_idx_p (machine_mode vec_mode
)
3862 if (!VECTOR_MODE_P (vec_mode
))
3865 machine_mode inner_mode
= GET_MODE_INNER (vec_mode
);
3866 rtx reg1
= alloca_raw_REG (vec_mode
, LAST_VIRTUAL_REGISTER
+ 1);
3867 rtx reg2
= alloca_raw_REG (inner_mode
, LAST_VIRTUAL_REGISTER
+ 2);
3868 rtx reg3
= alloca_raw_REG (VOIDmode
, LAST_VIRTUAL_REGISTER
+ 3);
3870 enum insn_code icode
= optab_handler (vec_set_optab
, vec_mode
);
3872 return icode
!= CODE_FOR_nothing
&& insn_operand_matches (icode
, 0, reg1
)
3873 && insn_operand_matches (icode
, 1, reg2
)
3874 && insn_operand_matches (icode
, 2, reg3
);
3877 /* This function is called when we are going to emit a compare instruction that
3878 compares the values found in X and Y, using the rtl operator COMPARISON.
3880 If they have mode BLKmode, then SIZE specifies the size of both operands.
3882 UNSIGNEDP nonzero says that the operands are unsigned;
3883 this matters if they need to be widened (as given by METHODS).
3885 *PTEST is where the resulting comparison RTX is returned or NULL_RTX
3886 if we failed to produce one.
3888 *PMODE is the mode of the inputs (in case they are const_int).
3890 This function performs all the setup necessary so that the caller only has
3891 to emit a single comparison insn. This setup can involve doing a BLKmode
3892 comparison or emitting a library call to perform the comparison if no insn
3893 is available to handle it.
3894 The values which are passed in through pointers can be modified; the caller
3895 should perform the comparison on the modified values. Constant
3896 comparisons must have already been folded. */
3899 prepare_cmp_insn (rtx x
, rtx y
, enum rtx_code comparison
, rtx size
,
3900 int unsignedp
, enum optab_methods methods
,
3901 rtx
*ptest
, machine_mode
*pmode
)
3903 machine_mode mode
= *pmode
;
3905 machine_mode cmp_mode
;
3906 enum mode_class mclass
;
3908 /* The other methods are not needed. */
3909 gcc_assert (methods
== OPTAB_DIRECT
|| methods
== OPTAB_WIDEN
3910 || methods
== OPTAB_LIB_WIDEN
);
3912 if (CONST_SCALAR_INT_P (y
))
3913 canonicalize_comparison (mode
, &comparison
, &y
);
3915 /* If we are optimizing, force expensive constants into a register. */
3916 if (CONSTANT_P (x
) && optimize
3917 && (rtx_cost (x
, mode
, COMPARE
, 0, optimize_insn_for_speed_p ())
3918 > COSTS_N_INSNS (1)))
3919 x
= force_reg (mode
, x
);
3921 if (CONSTANT_P (y
) && optimize
3922 && (rtx_cost (y
, mode
, COMPARE
, 1, optimize_insn_for_speed_p ())
3923 > COSTS_N_INSNS (1)))
3924 y
= force_reg (mode
, y
);
3927 /* Make sure if we have a canonical comparison. The RTL
3928 documentation states that canonical comparisons are required only
3929 for targets which have cc0. */
3930 gcc_assert (!CONSTANT_P (x
) || CONSTANT_P (y
));
3933 /* Don't let both operands fail to indicate the mode. */
3934 if (GET_MODE (x
) == VOIDmode
&& GET_MODE (y
) == VOIDmode
)
3935 x
= force_reg (mode
, x
);
3936 if (mode
== VOIDmode
)
3937 mode
= GET_MODE (x
) != VOIDmode
? GET_MODE (x
) : GET_MODE (y
);
3939 /* Handle all BLKmode compares. */
3941 if (mode
== BLKmode
)
3943 machine_mode result_mode
;
3944 enum insn_code cmp_code
;
3947 = GEN_INT (MIN (MEM_ALIGN (x
), MEM_ALIGN (y
)) / BITS_PER_UNIT
);
3951 /* Try to use a memory block compare insn - either cmpstr
3952 or cmpmem will do. */
3953 opt_scalar_int_mode cmp_mode_iter
;
3954 FOR_EACH_MODE_IN_CLASS (cmp_mode_iter
, MODE_INT
)
3956 scalar_int_mode cmp_mode
= cmp_mode_iter
.require ();
3957 cmp_code
= direct_optab_handler (cmpmem_optab
, cmp_mode
);
3958 if (cmp_code
== CODE_FOR_nothing
)
3959 cmp_code
= direct_optab_handler (cmpstr_optab
, cmp_mode
);
3960 if (cmp_code
== CODE_FOR_nothing
)
3961 cmp_code
= direct_optab_handler (cmpstrn_optab
, cmp_mode
);
3962 if (cmp_code
== CODE_FOR_nothing
)
3965 /* Must make sure the size fits the insn's mode. */
3966 if (CONST_INT_P (size
)
3967 ? UINTVAL (size
) > GET_MODE_MASK (cmp_mode
)
3968 : (GET_MODE_BITSIZE (as_a
<scalar_int_mode
> (GET_MODE (size
)))
3969 > GET_MODE_BITSIZE (cmp_mode
)))
3972 result_mode
= insn_data
[cmp_code
].operand
[0].mode
;
3973 result
= gen_reg_rtx (result_mode
);
3974 size
= convert_to_mode (cmp_mode
, size
, 1);
3975 emit_insn (GEN_FCN (cmp_code
) (result
, x
, y
, size
, opalign
));
3977 *ptest
= gen_rtx_fmt_ee (comparison
, VOIDmode
, result
, const0_rtx
);
3978 *pmode
= result_mode
;
3982 if (methods
!= OPTAB_LIB
&& methods
!= OPTAB_LIB_WIDEN
)
3985 /* Otherwise call a library function. */
3986 result
= emit_block_comp_via_libcall (x
, y
, size
);
3990 mode
= TYPE_MODE (integer_type_node
);
3991 methods
= OPTAB_LIB_WIDEN
;
3995 /* Don't allow operands to the compare to trap, as that can put the
3996 compare and branch in different basic blocks. */
3997 if (cfun
->can_throw_non_call_exceptions
)
4000 x
= copy_to_reg (x
);
4002 y
= copy_to_reg (y
);
4005 if (GET_MODE_CLASS (mode
) == MODE_CC
)
4007 enum insn_code icode
= optab_handler (cbranch_optab
, CCmode
);
4008 test
= gen_rtx_fmt_ee (comparison
, VOIDmode
, x
, y
);
4009 gcc_assert (icode
!= CODE_FOR_nothing
4010 && insn_operand_matches (icode
, 0, test
));
4015 mclass
= GET_MODE_CLASS (mode
);
4016 test
= gen_rtx_fmt_ee (comparison
, VOIDmode
, x
, y
);
4017 FOR_EACH_MODE_FROM (cmp_mode
, mode
)
4019 enum insn_code icode
;
4020 icode
= optab_handler (cbranch_optab
, cmp_mode
);
4021 if (icode
!= CODE_FOR_nothing
4022 && insn_operand_matches (icode
, 0, test
))
4024 rtx_insn
*last
= get_last_insn ();
4025 rtx op0
= prepare_operand (icode
, x
, 1, mode
, cmp_mode
, unsignedp
);
4026 rtx op1
= prepare_operand (icode
, y
, 2, mode
, cmp_mode
, unsignedp
);
4028 && insn_operand_matches (icode
, 1, op0
)
4029 && insn_operand_matches (icode
, 2, op1
))
4031 XEXP (test
, 0) = op0
;
4032 XEXP (test
, 1) = op1
;
4037 delete_insns_since (last
);
4040 if (methods
== OPTAB_DIRECT
|| !CLASS_HAS_WIDER_MODES_P (mclass
))
4044 if (methods
!= OPTAB_LIB_WIDEN
)
4047 if (SCALAR_FLOAT_MODE_P (mode
))
4049 /* Small trick if UNORDERED isn't implemented by the hardware. */
4050 if (comparison
== UNORDERED
&& rtx_equal_p (x
, y
))
4052 prepare_cmp_insn (x
, y
, UNLT
, NULL_RTX
, unsignedp
, OPTAB_WIDEN
,
4058 prepare_float_lib_cmp (x
, y
, comparison
, ptest
, pmode
);
4063 machine_mode ret_mode
;
4065 /* Handle a libcall just for the mode we are using. */
4066 libfunc
= optab_libfunc (cmp_optab
, mode
);
4067 gcc_assert (libfunc
);
4069 /* If we want unsigned, and this mode has a distinct unsigned
4070 comparison routine, use that. */
4073 rtx ulibfunc
= optab_libfunc (ucmp_optab
, mode
);
4078 ret_mode
= targetm
.libgcc_cmp_return_mode ();
4079 result
= emit_library_call_value (libfunc
, NULL_RTX
, LCT_CONST
,
4080 ret_mode
, x
, mode
, y
, mode
);
4082 /* There are two kinds of comparison routines. Biased routines
4083 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4084 of gcc expect that the comparison operation is equivalent
4085 to the modified comparison. For signed comparisons compare the
4086 result against 1 in the biased case, and zero in the unbiased
4087 case. For unsigned comparisons always compare against 1 after
4088 biasing the unbiased result by adding 1. This gives us a way to
4090 The comparisons in the fixed-point helper library are always
4095 if (!TARGET_LIB_INT_CMP_BIASED
&& !ALL_FIXED_POINT_MODE_P (mode
))
4098 x
= plus_constant (ret_mode
, result
, 1);
4104 prepare_cmp_insn (x
, y
, comparison
, NULL_RTX
, unsignedp
, methods
,
4114 /* Before emitting an insn with code ICODE, make sure that X, which is going
4115 to be used for operand OPNUM of the insn, is converted from mode MODE to
4116 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4117 that it is accepted by the operand predicate. Return the new value. */
4120 prepare_operand (enum insn_code icode
, rtx x
, int opnum
, machine_mode mode
,
4121 machine_mode wider_mode
, int unsignedp
)
4123 if (mode
!= wider_mode
)
4124 x
= convert_modes (wider_mode
, mode
, x
, unsignedp
);
4126 if (!insn_operand_matches (icode
, opnum
, x
))
4128 machine_mode op_mode
= insn_data
[(int) icode
].operand
[opnum
].mode
;
4129 if (reload_completed
)
4131 if (GET_MODE (x
) != op_mode
&& GET_MODE (x
) != VOIDmode
)
4133 x
= copy_to_mode_reg (op_mode
, x
);
4139 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4140 we can do the branch. */
4143 emit_cmp_and_jump_insn_1 (rtx test
, machine_mode mode
, rtx label
,
4144 profile_probability prob
)
4146 machine_mode optab_mode
;
4147 enum mode_class mclass
;
4148 enum insn_code icode
;
4151 mclass
= GET_MODE_CLASS (mode
);
4152 optab_mode
= (mclass
== MODE_CC
) ? CCmode
: mode
;
4153 icode
= optab_handler (cbranch_optab
, optab_mode
);
4155 gcc_assert (icode
!= CODE_FOR_nothing
);
4156 gcc_assert (insn_operand_matches (icode
, 0, test
));
4157 insn
= emit_jump_insn (GEN_FCN (icode
) (test
, XEXP (test
, 0),
4158 XEXP (test
, 1), label
));
4159 if (prob
.initialized_p ()
4160 && profile_status_for_fn (cfun
) != PROFILE_ABSENT
4163 && any_condjump_p (insn
)
4164 && !find_reg_note (insn
, REG_BR_PROB
, 0))
4165 add_reg_br_prob_note (insn
, prob
);
4168 /* Generate code to compare X with Y so that the condition codes are
4169 set and to jump to LABEL if the condition is true. If X is a
4170 constant and Y is not a constant, then the comparison is swapped to
4171 ensure that the comparison RTL has the canonical form.
4173 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4174 need to be widened. UNSIGNEDP is also used to select the proper
4175 branch condition code.
4177 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4179 MODE is the mode of the inputs (in case they are const_int).
4181 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4182 It will be potentially converted into an unsigned variant based on
4183 UNSIGNEDP to select a proper jump instruction.
4185 PROB is the probability of jumping to LABEL. */
4188 emit_cmp_and_jump_insns (rtx x
, rtx y
, enum rtx_code comparison
, rtx size
,
4189 machine_mode mode
, int unsignedp
, rtx label
,
4190 profile_probability prob
)
4192 rtx op0
= x
, op1
= y
;
4195 /* Swap operands and condition to ensure canonical RTL. */
4196 if (swap_commutative_operands_p (x
, y
)
4197 && can_compare_p (swap_condition (comparison
), mode
, ccp_jump
))
4200 comparison
= swap_condition (comparison
);
4203 /* If OP0 is still a constant, then both X and Y must be constants
4204 or the opposite comparison is not supported. Force X into a register
4205 to create canonical RTL. */
4206 if (CONSTANT_P (op0
))
4207 op0
= force_reg (mode
, op0
);
4210 comparison
= unsigned_condition (comparison
);
4212 prepare_cmp_insn (op0
, op1
, comparison
, size
, unsignedp
, OPTAB_LIB_WIDEN
,
4214 emit_cmp_and_jump_insn_1 (test
, mode
, label
, prob
);
4218 /* Emit a library call comparison between floating point X and Y.
4219 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4222 prepare_float_lib_cmp (rtx x
, rtx y
, enum rtx_code comparison
,
4223 rtx
*ptest
, machine_mode
*pmode
)
4225 enum rtx_code swapped
= swap_condition (comparison
);
4226 enum rtx_code reversed
= reverse_condition_maybe_unordered (comparison
);
4227 machine_mode orig_mode
= GET_MODE (x
);
4229 rtx true_rtx
, false_rtx
;
4230 rtx value
, target
, equiv
;
4233 bool reversed_p
= false;
4234 scalar_int_mode cmp_mode
= targetm
.libgcc_cmp_return_mode ();
4236 FOR_EACH_MODE_FROM (mode
, orig_mode
)
4238 if (code_to_optab (comparison
)
4239 && (libfunc
= optab_libfunc (code_to_optab (comparison
), mode
)))
4242 if (code_to_optab (swapped
)
4243 && (libfunc
= optab_libfunc (code_to_optab (swapped
), mode
)))
4246 comparison
= swapped
;
4250 if (code_to_optab (reversed
)
4251 && (libfunc
= optab_libfunc (code_to_optab (reversed
), mode
)))
4253 comparison
= reversed
;
4259 gcc_assert (mode
!= VOIDmode
);
4261 if (mode
!= orig_mode
)
4263 x
= convert_to_mode (mode
, x
, 0);
4264 y
= convert_to_mode (mode
, y
, 0);
4267 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4268 the RTL. The allows the RTL optimizers to delete the libcall if the
4269 condition can be determined at compile-time. */
4270 if (comparison
== UNORDERED
4271 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode
, comparison
))
4273 true_rtx
= const_true_rtx
;
4274 false_rtx
= const0_rtx
;
4281 true_rtx
= const0_rtx
;
4282 false_rtx
= const_true_rtx
;
4286 true_rtx
= const_true_rtx
;
4287 false_rtx
= const0_rtx
;
4291 true_rtx
= const1_rtx
;
4292 false_rtx
= const0_rtx
;
4296 true_rtx
= const0_rtx
;
4297 false_rtx
= constm1_rtx
;
4301 true_rtx
= constm1_rtx
;
4302 false_rtx
= const0_rtx
;
4306 true_rtx
= const0_rtx
;
4307 false_rtx
= const1_rtx
;
4315 if (comparison
== UNORDERED
)
4317 rtx temp
= simplify_gen_relational (NE
, cmp_mode
, mode
, x
, x
);
4318 equiv
= simplify_gen_relational (NE
, cmp_mode
, mode
, y
, y
);
4319 equiv
= simplify_gen_ternary (IF_THEN_ELSE
, cmp_mode
, cmp_mode
,
4320 temp
, const_true_rtx
, equiv
);
4324 equiv
= simplify_gen_relational (comparison
, cmp_mode
, mode
, x
, y
);
4325 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode
, comparison
))
4326 equiv
= simplify_gen_ternary (IF_THEN_ELSE
, cmp_mode
, cmp_mode
,
4327 equiv
, true_rtx
, false_rtx
);
4331 value
= emit_library_call_value (libfunc
, NULL_RTX
, LCT_CONST
,
4332 cmp_mode
, x
, mode
, y
, mode
);
4333 insns
= get_insns ();
4336 target
= gen_reg_rtx (cmp_mode
);
4337 emit_libcall_block (insns
, target
, value
, equiv
);
4339 if (comparison
== UNORDERED
4340 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode
, comparison
)
4342 *ptest
= gen_rtx_fmt_ee (reversed_p
? EQ
: NE
, VOIDmode
, target
, false_rtx
);
4344 *ptest
= gen_rtx_fmt_ee (comparison
, VOIDmode
, target
, const0_rtx
);
4349 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4352 emit_indirect_jump (rtx loc
)
4354 if (!targetm
.have_indirect_jump ())
4355 sorry ("indirect jumps are not available on this target");
4358 class expand_operand ops
[1];
4359 create_address_operand (&ops
[0], loc
);
4360 expand_jump_insn (targetm
.code_for_indirect_jump
, 1, ops
);
4366 /* Emit a conditional move instruction if the machine supports one for that
4367 condition and machine mode.
4369 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4370 the mode to use should they be constants. If it is VOIDmode, they cannot
4373 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4374 should be stored there. MODE is the mode to use should they be constants.
4375 If it is VOIDmode, they cannot both be constants.
4377 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4378 is not supported. */
4381 emit_conditional_move (rtx target
, enum rtx_code code
, rtx op0
, rtx op1
,
4382 machine_mode cmode
, rtx op2
, rtx op3
,
4383 machine_mode mode
, int unsignedp
)
4387 enum insn_code icode
;
4388 enum rtx_code reversed
;
4390 /* If the two source operands are identical, that's just a move. */
4392 if (rtx_equal_p (op2
, op3
))
4395 target
= gen_reg_rtx (mode
);
4397 emit_move_insn (target
, op3
);
4401 /* If one operand is constant, make it the second one. Only do this
4402 if the other operand is not constant as well. */
4404 if (swap_commutative_operands_p (op0
, op1
))
4406 std::swap (op0
, op1
);
4407 code
= swap_condition (code
);
4410 /* get_condition will prefer to generate LT and GT even if the old
4411 comparison was against zero, so undo that canonicalization here since
4412 comparisons against zero are cheaper. */
4413 if (code
== LT
&& op1
== const1_rtx
)
4414 code
= LE
, op1
= const0_rtx
;
4415 else if (code
== GT
&& op1
== constm1_rtx
)
4416 code
= GE
, op1
= const0_rtx
;
4418 if (cmode
== VOIDmode
)
4419 cmode
= GET_MODE (op0
);
4421 enum rtx_code orig_code
= code
;
4422 bool swapped
= false;
4423 if (swap_commutative_operands_p (op2
, op3
)
4424 && ((reversed
= reversed_comparison_code_parts (code
, op0
, op1
, NULL
))
4427 std::swap (op2
, op3
);
4432 if (mode
== VOIDmode
)
4433 mode
= GET_MODE (op2
);
4435 icode
= direct_optab_handler (movcc_optab
, mode
);
4437 if (icode
== CODE_FOR_nothing
)
4441 target
= gen_reg_rtx (mode
);
4443 for (int pass
= 0; ; pass
++)
4445 code
= unsignedp
? unsigned_condition (code
) : code
;
4446 comparison
= simplify_gen_relational (code
, VOIDmode
, cmode
, op0
, op1
);
4448 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4449 punt and let the caller figure out how best to deal with this
4451 if (COMPARISON_P (comparison
))
4453 saved_pending_stack_adjust save
;
4454 save_pending_stack_adjust (&save
);
4455 last
= get_last_insn ();
4456 do_pending_stack_adjust ();
4457 machine_mode cmpmode
= cmode
;
4458 prepare_cmp_insn (XEXP (comparison
, 0), XEXP (comparison
, 1),
4459 GET_CODE (comparison
), NULL_RTX
, unsignedp
,
4460 OPTAB_WIDEN
, &comparison
, &cmpmode
);
4463 class expand_operand ops
[4];
4465 create_output_operand (&ops
[0], target
, mode
);
4466 create_fixed_operand (&ops
[1], comparison
);
4467 create_input_operand (&ops
[2], op2
, mode
);
4468 create_input_operand (&ops
[3], op3
, mode
);
4469 if (maybe_expand_insn (icode
, 4, ops
))
4471 if (ops
[0].value
!= target
)
4472 convert_move (target
, ops
[0].value
, false);
4476 delete_insns_since (last
);
4477 restore_pending_stack_adjust (&save
);
4483 /* If the preferred op2/op3 order is not usable, retry with other
4484 operand order, perhaps it will expand successfully. */
4487 else if ((reversed
= reversed_comparison_code_parts (orig_code
, op0
, op1
,
4493 std::swap (op2
, op3
);
4498 /* Emit a conditional negate or bitwise complement using the
4499 negcc or notcc optabs if available. Return NULL_RTX if such operations
4500 are not available. Otherwise return the RTX holding the result.
4501 TARGET is the desired destination of the result. COMP is the comparison
4502 on which to negate. If COND is true move into TARGET the negation
4503 or bitwise complement of OP1. Otherwise move OP2 into TARGET.
4504 CODE is either NEG or NOT. MODE is the machine mode in which the
4505 operation is performed. */
4508 emit_conditional_neg_or_complement (rtx target
, rtx_code code
,
4509 machine_mode mode
, rtx cond
, rtx op1
,
4512 optab op
= unknown_optab
;
4515 else if (code
== NOT
)
4520 insn_code icode
= direct_optab_handler (op
, mode
);
4522 if (icode
== CODE_FOR_nothing
)
4526 target
= gen_reg_rtx (mode
);
4528 rtx_insn
*last
= get_last_insn ();
4529 class expand_operand ops
[4];
4531 create_output_operand (&ops
[0], target
, mode
);
4532 create_fixed_operand (&ops
[1], cond
);
4533 create_input_operand (&ops
[2], op1
, mode
);
4534 create_input_operand (&ops
[3], op2
, mode
);
4536 if (maybe_expand_insn (icode
, 4, ops
))
4538 if (ops
[0].value
!= target
)
4539 convert_move (target
, ops
[0].value
, false);
4543 delete_insns_since (last
);
4547 /* Emit a conditional addition instruction if the machine supports one for that
4548 condition and machine mode.
4550 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4551 the mode to use should they be constants. If it is VOIDmode, they cannot
4554 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4555 should be stored there. MODE is the mode to use should they be constants.
4556 If it is VOIDmode, they cannot both be constants.
4558 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4559 is not supported. */
4562 emit_conditional_add (rtx target
, enum rtx_code code
, rtx op0
, rtx op1
,
4563 machine_mode cmode
, rtx op2
, rtx op3
,
4564 machine_mode mode
, int unsignedp
)
4568 enum insn_code icode
;
4570 /* If one operand is constant, make it the second one. Only do this
4571 if the other operand is not constant as well. */
4573 if (swap_commutative_operands_p (op0
, op1
))
4575 std::swap (op0
, op1
);
4576 code
= swap_condition (code
);
4579 /* get_condition will prefer to generate LT and GT even if the old
4580 comparison was against zero, so undo that canonicalization here since
4581 comparisons against zero are cheaper. */
4582 if (code
== LT
&& op1
== const1_rtx
)
4583 code
= LE
, op1
= const0_rtx
;
4584 else if (code
== GT
&& op1
== constm1_rtx
)
4585 code
= GE
, op1
= const0_rtx
;
4587 if (cmode
== VOIDmode
)
4588 cmode
= GET_MODE (op0
);
4590 if (mode
== VOIDmode
)
4591 mode
= GET_MODE (op2
);
4593 icode
= optab_handler (addcc_optab
, mode
);
4595 if (icode
== CODE_FOR_nothing
)
4599 target
= gen_reg_rtx (mode
);
4601 code
= unsignedp
? unsigned_condition (code
) : code
;
4602 comparison
= simplify_gen_relational (code
, VOIDmode
, cmode
, op0
, op1
);
4604 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4605 return NULL and let the caller figure out how best to deal with this
4607 if (!COMPARISON_P (comparison
))
4610 do_pending_stack_adjust ();
4611 last
= get_last_insn ();
4612 prepare_cmp_insn (XEXP (comparison
, 0), XEXP (comparison
, 1),
4613 GET_CODE (comparison
), NULL_RTX
, unsignedp
, OPTAB_WIDEN
,
4614 &comparison
, &cmode
);
4617 class expand_operand ops
[4];
4619 create_output_operand (&ops
[0], target
, mode
);
4620 create_fixed_operand (&ops
[1], comparison
);
4621 create_input_operand (&ops
[2], op2
, mode
);
4622 create_input_operand (&ops
[3], op3
, mode
);
4623 if (maybe_expand_insn (icode
, 4, ops
))
4625 if (ops
[0].value
!= target
)
4626 convert_move (target
, ops
[0].value
, false);
4630 delete_insns_since (last
);
4634 /* These functions attempt to generate an insn body, rather than
4635 emitting the insn, but if the gen function already emits them, we
4636 make no attempt to turn them back into naked patterns. */
4638 /* Generate and return an insn body to add Y to X. */
4641 gen_add2_insn (rtx x
, rtx y
)
4643 enum insn_code icode
= optab_handler (add_optab
, GET_MODE (x
));
4645 gcc_assert (insn_operand_matches (icode
, 0, x
));
4646 gcc_assert (insn_operand_matches (icode
, 1, x
));
4647 gcc_assert (insn_operand_matches (icode
, 2, y
));
4649 return GEN_FCN (icode
) (x
, x
, y
);
4652 /* Generate and return an insn body to add r1 and c,
4653 storing the result in r0. */
4656 gen_add3_insn (rtx r0
, rtx r1
, rtx c
)
4658 enum insn_code icode
= optab_handler (add_optab
, GET_MODE (r0
));
4660 if (icode
== CODE_FOR_nothing
4661 || !insn_operand_matches (icode
, 0, r0
)
4662 || !insn_operand_matches (icode
, 1, r1
)
4663 || !insn_operand_matches (icode
, 2, c
))
4666 return GEN_FCN (icode
) (r0
, r1
, c
);
4670 have_add2_insn (rtx x
, rtx y
)
4672 enum insn_code icode
;
4674 gcc_assert (GET_MODE (x
) != VOIDmode
);
4676 icode
= optab_handler (add_optab
, GET_MODE (x
));
4678 if (icode
== CODE_FOR_nothing
)
4681 if (!insn_operand_matches (icode
, 0, x
)
4682 || !insn_operand_matches (icode
, 1, x
)
4683 || !insn_operand_matches (icode
, 2, y
))
4689 /* Generate and return an insn body to add Y to X. */
4692 gen_addptr3_insn (rtx x
, rtx y
, rtx z
)
4694 enum insn_code icode
= optab_handler (addptr3_optab
, GET_MODE (x
));
4696 gcc_assert (insn_operand_matches (icode
, 0, x
));
4697 gcc_assert (insn_operand_matches (icode
, 1, y
));
4698 gcc_assert (insn_operand_matches (icode
, 2, z
));
4700 return GEN_FCN (icode
) (x
, y
, z
);
4703 /* Return true if the target implements an addptr pattern and X, Y,
4704 and Z are valid for the pattern predicates. */
4707 have_addptr3_insn (rtx x
, rtx y
, rtx z
)
4709 enum insn_code icode
;
4711 gcc_assert (GET_MODE (x
) != VOIDmode
);
4713 icode
= optab_handler (addptr3_optab
, GET_MODE (x
));
4715 if (icode
== CODE_FOR_nothing
)
4718 if (!insn_operand_matches (icode
, 0, x
)
4719 || !insn_operand_matches (icode
, 1, y
)
4720 || !insn_operand_matches (icode
, 2, z
))
4726 /* Generate and return an insn body to subtract Y from X. */
4729 gen_sub2_insn (rtx x
, rtx y
)
4731 enum insn_code icode
= optab_handler (sub_optab
, GET_MODE (x
));
4733 gcc_assert (insn_operand_matches (icode
, 0, x
));
4734 gcc_assert (insn_operand_matches (icode
, 1, x
));
4735 gcc_assert (insn_operand_matches (icode
, 2, y
));
4737 return GEN_FCN (icode
) (x
, x
, y
);
4740 /* Generate and return an insn body to subtract r1 and c,
4741 storing the result in r0. */
4744 gen_sub3_insn (rtx r0
, rtx r1
, rtx c
)
4746 enum insn_code icode
= optab_handler (sub_optab
, GET_MODE (r0
));
4748 if (icode
== CODE_FOR_nothing
4749 || !insn_operand_matches (icode
, 0, r0
)
4750 || !insn_operand_matches (icode
, 1, r1
)
4751 || !insn_operand_matches (icode
, 2, c
))
4754 return GEN_FCN (icode
) (r0
, r1
, c
);
4758 have_sub2_insn (rtx x
, rtx y
)
4760 enum insn_code icode
;
4762 gcc_assert (GET_MODE (x
) != VOIDmode
);
4764 icode
= optab_handler (sub_optab
, GET_MODE (x
));
4766 if (icode
== CODE_FOR_nothing
)
4769 if (!insn_operand_matches (icode
, 0, x
)
4770 || !insn_operand_matches (icode
, 1, x
)
4771 || !insn_operand_matches (icode
, 2, y
))
4777 /* Generate the body of an insn to extend Y (with mode MFROM)
4778 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4781 gen_extend_insn (rtx x
, rtx y
, machine_mode mto
,
4782 machine_mode mfrom
, int unsignedp
)
4784 enum insn_code icode
= can_extend_p (mto
, mfrom
, unsignedp
);
4785 return GEN_FCN (icode
) (x
, y
);
4788 /* Generate code to convert FROM to floating point
4789 and store in TO. FROM must be fixed point and not VOIDmode.
4790 UNSIGNEDP nonzero means regard FROM as unsigned.
4791 Normally this is done by correcting the final value
4792 if it is negative. */
4795 expand_float (rtx to
, rtx from
, int unsignedp
)
4797 enum insn_code icode
;
4799 scalar_mode from_mode
, to_mode
;
4800 machine_mode fmode
, imode
;
4801 bool can_do_signed
= false;
4803 /* Crash now, because we won't be able to decide which mode to use. */
4804 gcc_assert (GET_MODE (from
) != VOIDmode
);
4806 /* Look for an insn to do the conversion. Do it in the specified
4807 modes if possible; otherwise convert either input, output or both to
4808 wider mode. If the integer mode is wider than the mode of FROM,
4809 we can do the conversion signed even if the input is unsigned. */
4811 FOR_EACH_MODE_FROM (fmode
, GET_MODE (to
))
4812 FOR_EACH_MODE_FROM (imode
, GET_MODE (from
))
4814 int doing_unsigned
= unsignedp
;
4816 if (fmode
!= GET_MODE (to
)
4817 && (significand_size (fmode
)
4818 < GET_MODE_UNIT_PRECISION (GET_MODE (from
))))
4821 icode
= can_float_p (fmode
, imode
, unsignedp
);
4822 if (icode
== CODE_FOR_nothing
&& unsignedp
)
4824 enum insn_code scode
= can_float_p (fmode
, imode
, 0);
4825 if (scode
!= CODE_FOR_nothing
)
4826 can_do_signed
= true;
4827 if (imode
!= GET_MODE (from
))
4828 icode
= scode
, doing_unsigned
= 0;
4831 if (icode
!= CODE_FOR_nothing
)
4833 if (imode
!= GET_MODE (from
))
4834 from
= convert_to_mode (imode
, from
, unsignedp
);
4836 if (fmode
!= GET_MODE (to
))
4837 target
= gen_reg_rtx (fmode
);
4839 emit_unop_insn (icode
, target
, from
,
4840 doing_unsigned
? UNSIGNED_FLOAT
: FLOAT
);
4843 convert_move (to
, target
, 0);
4848 /* Unsigned integer, and no way to convert directly. Convert as signed,
4849 then unconditionally adjust the result. */
4852 && is_a
<scalar_mode
> (GET_MODE (to
), &to_mode
)
4853 && is_a
<scalar_mode
> (GET_MODE (from
), &from_mode
))
4855 opt_scalar_mode fmode_iter
;
4856 rtx_code_label
*label
= gen_label_rtx ();
4858 REAL_VALUE_TYPE offset
;
4860 /* Look for a usable floating mode FMODE wider than the source and at
4861 least as wide as the target. Using FMODE will avoid rounding woes
4862 with unsigned values greater than the signed maximum value. */
4864 FOR_EACH_MODE_FROM (fmode_iter
, to_mode
)
4866 scalar_mode fmode
= fmode_iter
.require ();
4867 if (GET_MODE_PRECISION (from_mode
) < GET_MODE_BITSIZE (fmode
)
4868 && can_float_p (fmode
, from_mode
, 0) != CODE_FOR_nothing
)
4872 if (!fmode_iter
.exists (&fmode
))
4874 /* There is no such mode. Pretend the target is wide enough. */
4877 /* Avoid double-rounding when TO is narrower than FROM. */
4878 if ((significand_size (fmode
) + 1)
4879 < GET_MODE_PRECISION (from_mode
))
4882 rtx_code_label
*neglabel
= gen_label_rtx ();
4884 /* Don't use TARGET if it isn't a register, is a hard register,
4885 or is the wrong mode. */
4887 || REGNO (target
) < FIRST_PSEUDO_REGISTER
4888 || GET_MODE (target
) != fmode
)
4889 target
= gen_reg_rtx (fmode
);
4892 do_pending_stack_adjust ();
4894 /* Test whether the sign bit is set. */
4895 emit_cmp_and_jump_insns (from
, const0_rtx
, LT
, NULL_RTX
, imode
,
4898 /* The sign bit is not set. Convert as signed. */
4899 expand_float (target
, from
, 0);
4900 emit_jump_insn (targetm
.gen_jump (label
));
4903 /* The sign bit is set.
4904 Convert to a usable (positive signed) value by shifting right
4905 one bit, while remembering if a nonzero bit was shifted
4906 out; i.e., compute (from & 1) | (from >> 1). */
4908 emit_label (neglabel
);
4909 temp
= expand_binop (imode
, and_optab
, from
, const1_rtx
,
4910 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4911 temp1
= expand_shift (RSHIFT_EXPR
, imode
, from
, 1, NULL_RTX
, 1);
4912 temp
= expand_binop (imode
, ior_optab
, temp
, temp1
, temp
, 1,
4914 expand_float (target
, temp
, 0);
4916 /* Multiply by 2 to undo the shift above. */
4917 temp
= expand_binop (fmode
, add_optab
, target
, target
,
4918 target
, 0, OPTAB_LIB_WIDEN
);
4920 emit_move_insn (target
, temp
);
4922 do_pending_stack_adjust ();
4928 /* If we are about to do some arithmetic to correct for an
4929 unsigned operand, do it in a pseudo-register. */
4931 if (to_mode
!= fmode
4932 || !REG_P (to
) || REGNO (to
) < FIRST_PSEUDO_REGISTER
)
4933 target
= gen_reg_rtx (fmode
);
4935 /* Convert as signed integer to floating. */
4936 expand_float (target
, from
, 0);
4938 /* If FROM is negative (and therefore TO is negative),
4939 correct its value by 2**bitwidth. */
4941 do_pending_stack_adjust ();
4942 emit_cmp_and_jump_insns (from
, const0_rtx
, GE
, NULL_RTX
, from_mode
,
4946 real_2expN (&offset
, GET_MODE_PRECISION (from_mode
), fmode
);
4947 temp
= expand_binop (fmode
, add_optab
, target
,
4948 const_double_from_real_value (offset
, fmode
),
4949 target
, 0, OPTAB_LIB_WIDEN
);
4951 emit_move_insn (target
, temp
);
4953 do_pending_stack_adjust ();
4958 /* No hardware instruction available; call a library routine. */
4963 convert_optab tab
= unsignedp
? ufloat_optab
: sfloat_optab
;
4965 if (is_narrower_int_mode (GET_MODE (from
), SImode
))
4966 from
= convert_to_mode (SImode
, from
, unsignedp
);
4968 libfunc
= convert_optab_libfunc (tab
, GET_MODE (to
), GET_MODE (from
));
4969 gcc_assert (libfunc
);
4973 value
= emit_library_call_value (libfunc
, NULL_RTX
, LCT_CONST
,
4974 GET_MODE (to
), from
, GET_MODE (from
));
4975 insns
= get_insns ();
4978 emit_libcall_block (insns
, target
, value
,
4979 gen_rtx_fmt_e (unsignedp
? UNSIGNED_FLOAT
: FLOAT
,
4980 GET_MODE (to
), from
));
4985 /* Copy result to requested destination
4986 if we have been computing in a temp location. */
4990 if (GET_MODE (target
) == GET_MODE (to
))
4991 emit_move_insn (to
, target
);
4993 convert_move (to
, target
, 0);
4997 /* Generate code to convert FROM to fixed point and store in TO. FROM
4998 must be floating point. */
5001 expand_fix (rtx to
, rtx from
, int unsignedp
)
5003 enum insn_code icode
;
5005 machine_mode fmode
, imode
;
5006 opt_scalar_mode fmode_iter
;
5007 bool must_trunc
= false;
5009 /* We first try to find a pair of modes, one real and one integer, at
5010 least as wide as FROM and TO, respectively, in which we can open-code
5011 this conversion. If the integer mode is wider than the mode of TO,
5012 we can do the conversion either signed or unsigned. */
5014 FOR_EACH_MODE_FROM (fmode
, GET_MODE (from
))
5015 FOR_EACH_MODE_FROM (imode
, GET_MODE (to
))
5017 int doing_unsigned
= unsignedp
;
5019 icode
= can_fix_p (imode
, fmode
, unsignedp
, &must_trunc
);
5020 if (icode
== CODE_FOR_nothing
&& imode
!= GET_MODE (to
) && unsignedp
)
5021 icode
= can_fix_p (imode
, fmode
, 0, &must_trunc
), doing_unsigned
= 0;
5023 if (icode
!= CODE_FOR_nothing
)
5025 rtx_insn
*last
= get_last_insn ();
5026 if (fmode
!= GET_MODE (from
))
5027 from
= convert_to_mode (fmode
, from
, 0);
5031 rtx temp
= gen_reg_rtx (GET_MODE (from
));
5032 from
= expand_unop (GET_MODE (from
), ftrunc_optab
, from
,
5036 if (imode
!= GET_MODE (to
))
5037 target
= gen_reg_rtx (imode
);
5039 if (maybe_emit_unop_insn (icode
, target
, from
,
5040 doing_unsigned
? UNSIGNED_FIX
: FIX
))
5043 convert_move (to
, target
, unsignedp
);
5046 delete_insns_since (last
);
5050 /* For an unsigned conversion, there is one more way to do it.
5051 If we have a signed conversion, we generate code that compares
5052 the real value to the largest representable positive number. If if
5053 is smaller, the conversion is done normally. Otherwise, subtract
5054 one plus the highest signed number, convert, and add it back.
5056 We only need to check all real modes, since we know we didn't find
5057 anything with a wider integer mode.
5059 This code used to extend FP value into mode wider than the destination.
5060 This is needed for decimal float modes which cannot accurately
5061 represent one plus the highest signed number of the same size, but
5062 not for binary modes. Consider, for instance conversion from SFmode
5065 The hot path through the code is dealing with inputs smaller than 2^63
5066 and doing just the conversion, so there is no bits to lose.
5068 In the other path we know the value is positive in the range 2^63..2^64-1
5069 inclusive. (as for other input overflow happens and result is undefined)
5070 So we know that the most important bit set in mantissa corresponds to
5071 2^63. The subtraction of 2^63 should not generate any rounding as it
5072 simply clears out that bit. The rest is trivial. */
5074 scalar_int_mode to_mode
;
5076 && is_a
<scalar_int_mode
> (GET_MODE (to
), &to_mode
)
5077 && HWI_COMPUTABLE_MODE_P (to_mode
))
5078 FOR_EACH_MODE_FROM (fmode_iter
, as_a
<scalar_mode
> (GET_MODE (from
)))
5080 scalar_mode fmode
= fmode_iter
.require ();
5081 if (CODE_FOR_nothing
!= can_fix_p (to_mode
, fmode
,
5083 && (!DECIMAL_FLOAT_MODE_P (fmode
)
5084 || (GET_MODE_BITSIZE (fmode
) > GET_MODE_PRECISION (to_mode
))))
5087 REAL_VALUE_TYPE offset
;
5089 rtx_code_label
*lab1
, *lab2
;
5092 bitsize
= GET_MODE_PRECISION (to_mode
);
5093 real_2expN (&offset
, bitsize
- 1, fmode
);
5094 limit
= const_double_from_real_value (offset
, fmode
);
5095 lab1
= gen_label_rtx ();
5096 lab2
= gen_label_rtx ();
5098 if (fmode
!= GET_MODE (from
))
5099 from
= convert_to_mode (fmode
, from
, 0);
5101 /* See if we need to do the subtraction. */
5102 do_pending_stack_adjust ();
5103 emit_cmp_and_jump_insns (from
, limit
, GE
, NULL_RTX
,
5104 GET_MODE (from
), 0, lab1
);
5106 /* If not, do the signed "fix" and branch around fixup code. */
5107 expand_fix (to
, from
, 0);
5108 emit_jump_insn (targetm
.gen_jump (lab2
));
5111 /* Otherwise, subtract 2**(N-1), convert to signed number,
5112 then add 2**(N-1). Do the addition using XOR since this
5113 will often generate better code. */
5115 target
= expand_binop (GET_MODE (from
), sub_optab
, from
, limit
,
5116 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
5117 expand_fix (to
, target
, 0);
5118 target
= expand_binop (to_mode
, xor_optab
, to
,
5120 (HOST_WIDE_INT_1
<< (bitsize
- 1),
5122 to
, 1, OPTAB_LIB_WIDEN
);
5125 emit_move_insn (to
, target
);
5129 if (optab_handler (mov_optab
, to_mode
) != CODE_FOR_nothing
)
5131 /* Make a place for a REG_NOTE and add it. */
5132 insn
= emit_move_insn (to
, to
);
5133 set_dst_reg_note (insn
, REG_EQUAL
,
5134 gen_rtx_fmt_e (UNSIGNED_FIX
, to_mode
,
5143 /* We can't do it with an insn, so use a library call. But first ensure
5144 that the mode of TO is at least as wide as SImode, since those are the
5145 only library calls we know about. */
5147 if (is_narrower_int_mode (GET_MODE (to
), SImode
))
5149 target
= gen_reg_rtx (SImode
);
5151 expand_fix (target
, from
, unsignedp
);
5159 convert_optab tab
= unsignedp
? ufix_optab
: sfix_optab
;
5160 libfunc
= convert_optab_libfunc (tab
, GET_MODE (to
), GET_MODE (from
));
5161 gcc_assert (libfunc
);
5165 value
= emit_library_call_value (libfunc
, NULL_RTX
, LCT_CONST
,
5166 GET_MODE (to
), from
, GET_MODE (from
));
5167 insns
= get_insns ();
5170 emit_libcall_block (insns
, target
, value
,
5171 gen_rtx_fmt_e (unsignedp
? UNSIGNED_FIX
: FIX
,
5172 GET_MODE (to
), from
));
5177 if (GET_MODE (to
) == GET_MODE (target
))
5178 emit_move_insn (to
, target
);
5180 convert_move (to
, target
, 0);
5185 /* Promote integer arguments for a libcall if necessary.
5186 emit_library_call_value cannot do the promotion because it does not
5187 know if it should do a signed or unsigned promotion. This is because
5188 there are no tree types defined for libcalls. */
5191 prepare_libcall_arg (rtx arg
, int uintp
)
5193 scalar_int_mode mode
;
5194 machine_mode arg_mode
;
5195 if (is_a
<scalar_int_mode
> (GET_MODE (arg
), &mode
))
5197 /* If we need to promote the integer function argument we need to do
5198 it here instead of inside emit_library_call_value because in
5199 emit_library_call_value we don't know if we should do a signed or
5200 unsigned promotion. */
5203 arg_mode
= promote_function_mode (NULL_TREE
, mode
,
5204 &unsigned_p
, NULL_TREE
, 0);
5205 if (arg_mode
!= mode
)
5206 return convert_to_mode (arg_mode
, arg
, uintp
);
5211 /* Generate code to convert FROM or TO a fixed-point.
5212 If UINTP is true, either TO or FROM is an unsigned integer.
5213 If SATP is true, we need to saturate the result. */
5216 expand_fixed_convert (rtx to
, rtx from
, int uintp
, int satp
)
5218 machine_mode to_mode
= GET_MODE (to
);
5219 machine_mode from_mode
= GET_MODE (from
);
5221 enum rtx_code this_code
;
5222 enum insn_code code
;
5227 if (to_mode
== from_mode
)
5229 emit_move_insn (to
, from
);
5235 tab
= satp
? satfractuns_optab
: fractuns_optab
;
5236 this_code
= satp
? UNSIGNED_SAT_FRACT
: UNSIGNED_FRACT_CONVERT
;
5240 tab
= satp
? satfract_optab
: fract_optab
;
5241 this_code
= satp
? SAT_FRACT
: FRACT_CONVERT
;
5243 code
= convert_optab_handler (tab
, to_mode
, from_mode
);
5244 if (code
!= CODE_FOR_nothing
)
5246 emit_unop_insn (code
, to
, from
, this_code
);
5250 libfunc
= convert_optab_libfunc (tab
, to_mode
, from_mode
);
5251 gcc_assert (libfunc
);
5253 from
= prepare_libcall_arg (from
, uintp
);
5254 from_mode
= GET_MODE (from
);
5257 value
= emit_library_call_value (libfunc
, NULL_RTX
, LCT_CONST
, to_mode
,
5259 insns
= get_insns ();
5262 emit_libcall_block (insns
, to
, value
,
5263 gen_rtx_fmt_e (optab_to_code (tab
), to_mode
, from
));
5266 /* Generate code to convert FROM to fixed point and store in TO. FROM
5267 must be floating point, TO must be signed. Use the conversion optab
5268 TAB to do the conversion. */
5271 expand_sfix_optab (rtx to
, rtx from
, convert_optab tab
)
5273 enum insn_code icode
;
5275 machine_mode fmode
, imode
;
5277 /* We first try to find a pair of modes, one real and one integer, at
5278 least as wide as FROM and TO, respectively, in which we can open-code
5279 this conversion. If the integer mode is wider than the mode of TO,
5280 we can do the conversion either signed or unsigned. */
5282 FOR_EACH_MODE_FROM (fmode
, GET_MODE (from
))
5283 FOR_EACH_MODE_FROM (imode
, GET_MODE (to
))
5285 icode
= convert_optab_handler (tab
, imode
, fmode
);
5286 if (icode
!= CODE_FOR_nothing
)
5288 rtx_insn
*last
= get_last_insn ();
5289 if (fmode
!= GET_MODE (from
))
5290 from
= convert_to_mode (fmode
, from
, 0);
5292 if (imode
!= GET_MODE (to
))
5293 target
= gen_reg_rtx (imode
);
5295 if (!maybe_emit_unop_insn (icode
, target
, from
, UNKNOWN
))
5297 delete_insns_since (last
);
5301 convert_move (to
, target
, 0);
5309 /* Report whether we have an instruction to perform the operation
5310 specified by CODE on operands of mode MODE. */
5312 have_insn_for (enum rtx_code code
, machine_mode mode
)
5314 return (code_to_optab (code
)
5315 && (optab_handler (code_to_optab (code
), mode
)
5316 != CODE_FOR_nothing
));
5319 /* Print information about the current contents of the optabs on
5323 debug_optab_libfuncs (void)
5327 /* Dump the arithmetic optabs. */
5328 for (i
= FIRST_NORM_OPTAB
; i
<= LAST_NORMLIB_OPTAB
; ++i
)
5329 for (j
= 0; j
< NUM_MACHINE_MODES
; ++j
)
5331 rtx l
= optab_libfunc ((optab
) i
, (machine_mode
) j
);
5334 gcc_assert (GET_CODE (l
) == SYMBOL_REF
);
5335 fprintf (stderr
, "%s\t%s:\t%s\n",
5336 GET_RTX_NAME (optab_to_code ((optab
) i
)),
5342 /* Dump the conversion optabs. */
5343 for (i
= FIRST_CONV_OPTAB
; i
<= LAST_CONVLIB_OPTAB
; ++i
)
5344 for (j
= 0; j
< NUM_MACHINE_MODES
; ++j
)
5345 for (k
= 0; k
< NUM_MACHINE_MODES
; ++k
)
5347 rtx l
= convert_optab_libfunc ((optab
) i
, (machine_mode
) j
,
5351 gcc_assert (GET_CODE (l
) == SYMBOL_REF
);
5352 fprintf (stderr
, "%s\t%s\t%s:\t%s\n",
5353 GET_RTX_NAME (optab_to_code ((optab
) i
)),
5361 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5362 CODE. Return 0 on failure. */
5365 gen_cond_trap (enum rtx_code code
, rtx op1
, rtx op2
, rtx tcode
)
5367 machine_mode mode
= GET_MODE (op1
);
5368 enum insn_code icode
;
5372 if (mode
== VOIDmode
)
5375 icode
= optab_handler (ctrap_optab
, mode
);
5376 if (icode
== CODE_FOR_nothing
)
5379 /* Some targets only accept a zero trap code. */
5380 if (!insn_operand_matches (icode
, 3, tcode
))
5383 do_pending_stack_adjust ();
5385 prepare_cmp_insn (op1
, op2
, code
, NULL_RTX
, false, OPTAB_DIRECT
,
5390 insn
= GEN_FCN (icode
) (trap_rtx
, XEXP (trap_rtx
, 0), XEXP (trap_rtx
, 1),
5393 /* If that failed, then give up. */
5401 insn
= get_insns ();
5406 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
5407 or unsigned operation code. */
5410 get_rtx_code (enum tree_code tcode
, bool unsignedp
)
5422 code
= unsignedp
? LTU
: LT
;
5425 code
= unsignedp
? LEU
: LE
;
5428 code
= unsignedp
? GTU
: GT
;
5431 code
= unsignedp
? GEU
: GE
;
5434 case UNORDERED_EXPR
:
5473 /* Return a comparison rtx of mode CMP_MODE for COND. Use UNSIGNEDP to
5474 select signed or unsigned operators. OPNO holds the index of the
5475 first comparison operand for insn ICODE. Do not generate the
5476 compare instruction itself. */
5479 vector_compare_rtx (machine_mode cmp_mode
, enum tree_code tcode
,
5480 tree t_op0
, tree t_op1
, bool unsignedp
,
5481 enum insn_code icode
, unsigned int opno
)
5483 class expand_operand ops
[2];
5484 rtx rtx_op0
, rtx_op1
;
5485 machine_mode m0
, m1
;
5486 enum rtx_code rcode
= get_rtx_code (tcode
, unsignedp
);
5488 gcc_assert (TREE_CODE_CLASS (tcode
) == tcc_comparison
);
5490 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
5491 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
5492 cases, use the original mode. */
5493 rtx_op0
= expand_expr (t_op0
, NULL_RTX
, TYPE_MODE (TREE_TYPE (t_op0
)),
5495 m0
= GET_MODE (rtx_op0
);
5497 m0
= TYPE_MODE (TREE_TYPE (t_op0
));
5499 rtx_op1
= expand_expr (t_op1
, NULL_RTX
, TYPE_MODE (TREE_TYPE (t_op1
)),
5501 m1
= GET_MODE (rtx_op1
);
5503 m1
= TYPE_MODE (TREE_TYPE (t_op1
));
5505 create_input_operand (&ops
[0], rtx_op0
, m0
);
5506 create_input_operand (&ops
[1], rtx_op1
, m1
);
5507 if (!maybe_legitimize_operands (icode
, opno
, 2, ops
))
5509 return gen_rtx_fmt_ee (rcode
, cmp_mode
, ops
[0].value
, ops
[1].value
);
5512 /* Check if vec_perm mask SEL is a constant equivalent to a shift of
5513 the first vec_perm operand, assuming the second operand (for left shift
5514 first operand) is a constant vector of zeros. Return the shift distance
5515 in bits if so, or NULL_RTX if the vec_perm is not a shift. MODE is the
5516 mode of the value being shifted. SHIFT_OPTAB is vec_shr_optab for right
5517 shift or vec_shl_optab for left shift. */
5519 shift_amt_for_vec_perm_mask (machine_mode mode
, const vec_perm_indices
&sel
,
5522 unsigned int bitsize
= GET_MODE_UNIT_BITSIZE (mode
);
5523 poly_int64 first
= sel
[0];
5524 if (maybe_ge (sel
[0], GET_MODE_NUNITS (mode
)))
5527 if (shift_optab
== vec_shl_optab
)
5530 if (!GET_MODE_NUNITS (mode
).is_constant (&nelt
))
5532 unsigned firstidx
= 0;
5533 for (unsigned int i
= 0; i
< nelt
; i
++)
5535 if (known_eq (sel
[i
], nelt
))
5537 if (i
== 0 || firstidx
)
5542 ? maybe_ne (sel
[i
], nelt
+ i
- firstidx
)
5543 : maybe_ge (sel
[i
], nelt
))
5551 else if (!sel
.series_p (0, 1, first
, 1))
5554 if (!GET_MODE_NUNITS (mode
).is_constant (&nelt
))
5556 for (unsigned int i
= 1; i
< nelt
; i
++)
5558 poly_int64 expected
= i
+ first
;
5559 /* Indices into the second vector are all equivalent. */
5560 if (maybe_lt (sel
[i
], nelt
)
5561 ? maybe_ne (sel
[i
], expected
)
5562 : maybe_lt (expected
, nelt
))
5567 return gen_int_shift_amount (mode
, first
* bitsize
);
5570 /* A subroutine of expand_vec_perm_var for expanding one vec_perm insn. */
5573 expand_vec_perm_1 (enum insn_code icode
, rtx target
,
5574 rtx v0
, rtx v1
, rtx sel
)
5576 machine_mode tmode
= GET_MODE (target
);
5577 machine_mode smode
= GET_MODE (sel
);
5578 class expand_operand ops
[4];
5580 gcc_assert (GET_MODE_CLASS (smode
) == MODE_VECTOR_INT
5581 || related_int_vector_mode (tmode
).require () == smode
);
5582 create_output_operand (&ops
[0], target
, tmode
);
5583 create_input_operand (&ops
[3], sel
, smode
);
5585 /* Make an effort to preserve v0 == v1. The target expander is able to
5586 rely on this to determine if we're permuting a single input operand. */
5587 if (rtx_equal_p (v0
, v1
))
5589 if (!insn_operand_matches (icode
, 1, v0
))
5590 v0
= force_reg (tmode
, v0
);
5591 gcc_checking_assert (insn_operand_matches (icode
, 1, v0
));
5592 gcc_checking_assert (insn_operand_matches (icode
, 2, v0
));
5594 create_fixed_operand (&ops
[1], v0
);
5595 create_fixed_operand (&ops
[2], v0
);
5599 create_input_operand (&ops
[1], v0
, tmode
);
5600 create_input_operand (&ops
[2], v1
, tmode
);
5603 if (maybe_expand_insn (icode
, 4, ops
))
5604 return ops
[0].value
;
5608 /* Implement a permutation of vectors v0 and v1 using the permutation
5609 vector in SEL and return the result. Use TARGET to hold the result
5610 if nonnull and convenient.
5612 MODE is the mode of the vectors being permuted (V0 and V1). SEL_MODE
5613 is the TYPE_MODE associated with SEL, or BLKmode if SEL isn't known
5614 to have a particular mode. */
5617 expand_vec_perm_const (machine_mode mode
, rtx v0
, rtx v1
,
5618 const vec_perm_builder
&sel
, machine_mode sel_mode
,
5621 if (!target
|| !register_operand (target
, mode
))
5622 target
= gen_reg_rtx (mode
);
5624 /* Set QIMODE to a different vector mode with byte elements.
5625 If no such mode, or if MODE already has byte elements, use VOIDmode. */
5626 machine_mode qimode
;
5627 if (!qimode_for_vec_perm (mode
).exists (&qimode
))
5630 rtx_insn
*last
= get_last_insn ();
5632 bool single_arg_p
= rtx_equal_p (v0
, v1
);
5633 /* Always specify two input vectors here and leave the target to handle
5634 cases in which the inputs are equal. Not all backends can cope with
5635 the single-input representation when testing for a double-input
5636 target instruction. */
5637 vec_perm_indices
indices (sel
, 2, GET_MODE_NUNITS (mode
));
5639 /* See if this can be handled with a vec_shr or vec_shl. We only do this
5640 if the second (for vec_shr) or first (for vec_shl) vector is all
5642 insn_code shift_code
= CODE_FOR_nothing
;
5643 insn_code shift_code_qi
= CODE_FOR_nothing
;
5644 optab shift_optab
= unknown_optab
;
5646 if (v1
== CONST0_RTX (GET_MODE (v1
)))
5647 shift_optab
= vec_shr_optab
;
5648 else if (v0
== CONST0_RTX (GET_MODE (v0
)))
5650 shift_optab
= vec_shl_optab
;
5653 if (shift_optab
!= unknown_optab
)
5655 shift_code
= optab_handler (shift_optab
, mode
);
5656 shift_code_qi
= ((qimode
!= VOIDmode
&& qimode
!= mode
)
5657 ? optab_handler (shift_optab
, qimode
)
5658 : CODE_FOR_nothing
);
5660 if (shift_code
!= CODE_FOR_nothing
|| shift_code_qi
!= CODE_FOR_nothing
)
5662 rtx shift_amt
= shift_amt_for_vec_perm_mask (mode
, indices
, shift_optab
);
5665 class expand_operand ops
[3];
5666 if (shift_amt
== const0_rtx
)
5668 if (shift_code
!= CODE_FOR_nothing
)
5670 create_output_operand (&ops
[0], target
, mode
);
5671 create_input_operand (&ops
[1], v2
, mode
);
5672 create_convert_operand_from_type (&ops
[2], shift_amt
, sizetype
);
5673 if (maybe_expand_insn (shift_code
, 3, ops
))
5674 return ops
[0].value
;
5676 if (shift_code_qi
!= CODE_FOR_nothing
)
5678 rtx tmp
= gen_reg_rtx (qimode
);
5679 create_output_operand (&ops
[0], tmp
, qimode
);
5680 create_input_operand (&ops
[1], gen_lowpart (qimode
, v2
), qimode
);
5681 create_convert_operand_from_type (&ops
[2], shift_amt
, sizetype
);
5682 if (maybe_expand_insn (shift_code_qi
, 3, ops
))
5683 return gen_lowpart (mode
, ops
[0].value
);
5688 if (targetm
.vectorize
.vec_perm_const
!= NULL
)
5690 v0
= force_reg (mode
, v0
);
5694 v1
= force_reg (mode
, v1
);
5696 if (targetm
.vectorize
.vec_perm_const (mode
, target
, v0
, v1
, indices
))
5700 /* Fall back to a constant byte-based permutation. */
5701 vec_perm_indices qimode_indices
;
5702 rtx target_qi
= NULL_RTX
, v0_qi
= NULL_RTX
, v1_qi
= NULL_RTX
;
5703 if (qimode
!= VOIDmode
)
5705 qimode_indices
.new_expanded_vector (indices
, GET_MODE_UNIT_SIZE (mode
));
5706 target_qi
= gen_reg_rtx (qimode
);
5707 v0_qi
= gen_lowpart (qimode
, v0
);
5708 v1_qi
= gen_lowpart (qimode
, v1
);
5709 if (targetm
.vectorize
.vec_perm_const
!= NULL
5710 && targetm
.vectorize
.vec_perm_const (qimode
, target_qi
, v0_qi
,
5711 v1_qi
, qimode_indices
))
5712 return gen_lowpart (mode
, target_qi
);
5715 /* Otherwise expand as a fully variable permuation. */
5717 /* The optabs are only defined for selectors with the same width
5718 as the values being permuted. */
5719 machine_mode required_sel_mode
;
5720 if (!related_int_vector_mode (mode
).exists (&required_sel_mode
))
5722 delete_insns_since (last
);
5726 /* We know that it is semantically valid to treat SEL as having SEL_MODE.
5727 If that isn't the mode we want then we need to prove that using
5728 REQUIRED_SEL_MODE is OK. */
5729 if (sel_mode
!= required_sel_mode
)
5731 if (!selector_fits_mode_p (required_sel_mode
, indices
))
5733 delete_insns_since (last
);
5736 sel_mode
= required_sel_mode
;
5739 insn_code icode
= direct_optab_handler (vec_perm_optab
, mode
);
5740 if (icode
!= CODE_FOR_nothing
)
5742 rtx sel_rtx
= vec_perm_indices_to_rtx (sel_mode
, indices
);
5743 rtx tmp
= expand_vec_perm_1 (icode
, target
, v0
, v1
, sel_rtx
);
5748 if (qimode
!= VOIDmode
5749 && selector_fits_mode_p (qimode
, qimode_indices
))
5751 icode
= direct_optab_handler (vec_perm_optab
, qimode
);
5752 if (icode
!= CODE_FOR_nothing
)
5754 rtx sel_qi
= vec_perm_indices_to_rtx (qimode
, qimode_indices
);
5755 rtx tmp
= expand_vec_perm_1 (icode
, target_qi
, v0_qi
, v1_qi
, sel_qi
);
5757 return gen_lowpart (mode
, tmp
);
5761 delete_insns_since (last
);
5765 /* Implement a permutation of vectors v0 and v1 using the permutation
5766 vector in SEL and return the result. Use TARGET to hold the result
5767 if nonnull and convenient.
5769 MODE is the mode of the vectors being permuted (V0 and V1).
5770 SEL must have the integer equivalent of MODE and is known to be
5771 unsuitable for permutes with a constant permutation vector. */
5774 expand_vec_perm_var (machine_mode mode
, rtx v0
, rtx v1
, rtx sel
, rtx target
)
5776 enum insn_code icode
;
5780 u
= GET_MODE_UNIT_SIZE (mode
);
5782 if (!target
|| GET_MODE (target
) != mode
)
5783 target
= gen_reg_rtx (mode
);
5785 icode
= direct_optab_handler (vec_perm_optab
, mode
);
5786 if (icode
!= CODE_FOR_nothing
)
5788 tmp
= expand_vec_perm_1 (icode
, target
, v0
, v1
, sel
);
5793 /* As a special case to aid several targets, lower the element-based
5794 permutation to a byte-based permutation and try again. */
5795 machine_mode qimode
;
5796 if (!qimode_for_vec_perm (mode
).exists (&qimode
)
5797 || maybe_gt (GET_MODE_NUNITS (qimode
), GET_MODE_MASK (QImode
) + 1))
5799 icode
= direct_optab_handler (vec_perm_optab
, qimode
);
5800 if (icode
== CODE_FOR_nothing
)
5803 /* Multiply each element by its byte size. */
5804 machine_mode selmode
= GET_MODE (sel
);
5806 sel
= expand_simple_binop (selmode
, PLUS
, sel
, sel
,
5807 NULL
, 0, OPTAB_DIRECT
);
5809 sel
= expand_simple_binop (selmode
, ASHIFT
, sel
,
5810 gen_int_shift_amount (selmode
, exact_log2 (u
)),
5811 NULL
, 0, OPTAB_DIRECT
);
5812 gcc_assert (sel
!= NULL
);
5814 /* Broadcast the low byte each element into each of its bytes.
5815 The encoding has U interleaved stepped patterns, one for each
5816 byte of an element. */
5817 vec_perm_builder
const_sel (GET_MODE_SIZE (mode
), u
, 3);
5818 unsigned int low_byte_in_u
= BYTES_BIG_ENDIAN
? u
- 1 : 0;
5819 for (i
= 0; i
< 3; ++i
)
5820 for (unsigned int j
= 0; j
< u
; ++j
)
5821 const_sel
.quick_push (i
* u
+ low_byte_in_u
);
5822 sel
= gen_lowpart (qimode
, sel
);
5823 sel
= expand_vec_perm_const (qimode
, sel
, sel
, const_sel
, qimode
, NULL
);
5824 gcc_assert (sel
!= NULL
);
5826 /* Add the byte offset to each byte element. */
5827 /* Note that the definition of the indicies here is memory ordering,
5828 so there should be no difference between big and little endian. */
5829 rtx_vector_builder
byte_indices (qimode
, u
, 1);
5830 for (i
= 0; i
< u
; ++i
)
5831 byte_indices
.quick_push (GEN_INT (i
));
5832 tmp
= byte_indices
.build ();
5833 sel_qi
= expand_simple_binop (qimode
, PLUS
, sel
, tmp
,
5834 sel
, 0, OPTAB_DIRECT
);
5835 gcc_assert (sel_qi
!= NULL
);
5837 tmp
= mode
!= qimode
? gen_reg_rtx (qimode
) : target
;
5838 tmp
= expand_vec_perm_1 (icode
, tmp
, gen_lowpart (qimode
, v0
),
5839 gen_lowpart (qimode
, v1
), sel_qi
);
5841 tmp
= gen_lowpart (mode
, tmp
);
5845 /* Generate VEC_SERIES_EXPR <OP0, OP1>, returning a value of mode VMODE.
5846 Use TARGET for the result if nonnull and convenient. */
5849 expand_vec_series_expr (machine_mode vmode
, rtx op0
, rtx op1
, rtx target
)
5851 class expand_operand ops
[3];
5852 enum insn_code icode
;
5853 machine_mode emode
= GET_MODE_INNER (vmode
);
5855 icode
= direct_optab_handler (vec_series_optab
, vmode
);
5856 gcc_assert (icode
!= CODE_FOR_nothing
);
5858 create_output_operand (&ops
[0], target
, vmode
);
5859 create_input_operand (&ops
[1], op0
, emode
);
5860 create_input_operand (&ops
[2], op1
, emode
);
5862 expand_insn (icode
, 3, ops
);
5863 return ops
[0].value
;
5866 /* Generate insns for a vector comparison into a mask. */
5869 expand_vec_cmp_expr (tree type
, tree exp
, rtx target
)
5871 class expand_operand ops
[4];
5872 enum insn_code icode
;
5874 machine_mode mask_mode
= TYPE_MODE (type
);
5878 enum tree_code tcode
;
5880 op0a
= TREE_OPERAND (exp
, 0);
5881 op0b
= TREE_OPERAND (exp
, 1);
5882 tcode
= TREE_CODE (exp
);
5884 unsignedp
= TYPE_UNSIGNED (TREE_TYPE (op0a
));
5885 vmode
= TYPE_MODE (TREE_TYPE (op0a
));
5887 icode
= get_vec_cmp_icode (vmode
, mask_mode
, unsignedp
);
5888 if (icode
== CODE_FOR_nothing
)
5890 if (tcode
== EQ_EXPR
|| tcode
== NE_EXPR
)
5891 icode
= get_vec_cmp_eq_icode (vmode
, mask_mode
);
5892 if (icode
== CODE_FOR_nothing
)
5896 comparison
= vector_compare_rtx (mask_mode
, tcode
, op0a
, op0b
,
5897 unsignedp
, icode
, 2);
5898 create_output_operand (&ops
[0], target
, mask_mode
);
5899 create_fixed_operand (&ops
[1], comparison
);
5900 create_fixed_operand (&ops
[2], XEXP (comparison
, 0));
5901 create_fixed_operand (&ops
[3], XEXP (comparison
, 1));
5902 expand_insn (icode
, 4, ops
);
5903 return ops
[0].value
;
5906 /* Expand a highpart multiply. */
5909 expand_mult_highpart (machine_mode mode
, rtx op0
, rtx op1
,
5910 rtx target
, bool uns_p
)
5912 class expand_operand eops
[3];
5913 enum insn_code icode
;
5919 method
= can_mult_highpart_p (mode
, uns_p
);
5925 tab1
= uns_p
? umul_highpart_optab
: smul_highpart_optab
;
5926 return expand_binop (mode
, tab1
, op0
, op1
, target
, uns_p
,
5929 tab1
= uns_p
? vec_widen_umult_even_optab
: vec_widen_smult_even_optab
;
5930 tab2
= uns_p
? vec_widen_umult_odd_optab
: vec_widen_smult_odd_optab
;
5933 tab1
= uns_p
? vec_widen_umult_lo_optab
: vec_widen_smult_lo_optab
;
5934 tab2
= uns_p
? vec_widen_umult_hi_optab
: vec_widen_smult_hi_optab
;
5935 if (BYTES_BIG_ENDIAN
)
5936 std::swap (tab1
, tab2
);
5942 icode
= optab_handler (tab1
, mode
);
5943 wmode
= insn_data
[icode
].operand
[0].mode
;
5944 gcc_checking_assert (known_eq (2 * GET_MODE_NUNITS (wmode
),
5945 GET_MODE_NUNITS (mode
)));
5946 gcc_checking_assert (known_eq (GET_MODE_SIZE (wmode
), GET_MODE_SIZE (mode
)));
5948 create_output_operand (&eops
[0], gen_reg_rtx (wmode
), wmode
);
5949 create_input_operand (&eops
[1], op0
, mode
);
5950 create_input_operand (&eops
[2], op1
, mode
);
5951 expand_insn (icode
, 3, eops
);
5952 m1
= gen_lowpart (mode
, eops
[0].value
);
5954 create_output_operand (&eops
[0], gen_reg_rtx (wmode
), wmode
);
5955 create_input_operand (&eops
[1], op0
, mode
);
5956 create_input_operand (&eops
[2], op1
, mode
);
5957 expand_insn (optab_handler (tab2
, mode
), 3, eops
);
5958 m2
= gen_lowpart (mode
, eops
[0].value
);
5960 vec_perm_builder sel
;
5963 /* The encoding has 2 interleaved stepped patterns. */
5964 sel
.new_vector (GET_MODE_NUNITS (mode
), 2, 3);
5965 for (i
= 0; i
< 6; ++i
)
5966 sel
.quick_push (!BYTES_BIG_ENDIAN
+ (i
& ~1)
5967 + ((i
& 1) ? GET_MODE_NUNITS (mode
) : 0));
5971 /* The encoding has a single interleaved stepped pattern. */
5972 sel
.new_vector (GET_MODE_NUNITS (mode
), 1, 3);
5973 for (i
= 0; i
< 3; ++i
)
5974 sel
.quick_push (2 * i
+ (BYTES_BIG_ENDIAN
? 0 : 1));
5977 return expand_vec_perm_const (mode
, m1
, m2
, sel
, BLKmode
, target
);
5980 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
5984 find_cc_set (rtx x
, const_rtx pat
, void *data
)
5986 if (REG_P (x
) && GET_MODE_CLASS (GET_MODE (x
)) == MODE_CC
5987 && GET_CODE (pat
) == SET
)
5989 rtx
*p_cc_reg
= (rtx
*) data
;
5990 gcc_assert (!*p_cc_reg
);
5995 /* This is a helper function for the other atomic operations. This function
5996 emits a loop that contains SEQ that iterates until a compare-and-swap
5997 operation at the end succeeds. MEM is the memory to be modified. SEQ is
5998 a set of instructions that takes a value from OLD_REG as an input and
5999 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
6000 set to the current contents of MEM. After SEQ, a compare-and-swap will
6001 attempt to update MEM with NEW_REG. The function returns true when the
6002 loop was generated successfully. */
6005 expand_compare_and_swap_loop (rtx mem
, rtx old_reg
, rtx new_reg
, rtx seq
)
6007 machine_mode mode
= GET_MODE (mem
);
6008 rtx_code_label
*label
;
6009 rtx cmp_reg
, success
, oldval
;
6011 /* The loop we want to generate looks like
6017 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
6021 Note that we only do the plain load from memory once. Subsequent
6022 iterations use the value loaded by the compare-and-swap pattern. */
6024 label
= gen_label_rtx ();
6025 cmp_reg
= gen_reg_rtx (mode
);
6027 emit_move_insn (cmp_reg
, mem
);
6029 emit_move_insn (old_reg
, cmp_reg
);
6035 if (!expand_atomic_compare_and_swap (&success
, &oldval
, mem
, old_reg
,
6036 new_reg
, false, MEMMODEL_SYNC_SEQ_CST
,
6040 if (oldval
!= cmp_reg
)
6041 emit_move_insn (cmp_reg
, oldval
);
6043 /* Mark this jump predicted not taken. */
6044 emit_cmp_and_jump_insns (success
, const0_rtx
, EQ
, const0_rtx
,
6045 GET_MODE (success
), 1, label
,
6046 profile_probability::guessed_never ());
6051 /* This function tries to emit an atomic_exchange intruction. VAL is written
6052 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
6053 using TARGET if possible. */
6056 maybe_emit_atomic_exchange (rtx target
, rtx mem
, rtx val
, enum memmodel model
)
6058 machine_mode mode
= GET_MODE (mem
);
6059 enum insn_code icode
;
6061 /* If the target supports the exchange directly, great. */
6062 icode
= direct_optab_handler (atomic_exchange_optab
, mode
);
6063 if (icode
!= CODE_FOR_nothing
)
6065 class expand_operand ops
[4];
6067 create_output_operand (&ops
[0], target
, mode
);
6068 create_fixed_operand (&ops
[1], mem
);
6069 create_input_operand (&ops
[2], val
, mode
);
6070 create_integer_operand (&ops
[3], model
);
6071 if (maybe_expand_insn (icode
, 4, ops
))
6072 return ops
[0].value
;
6078 /* This function tries to implement an atomic exchange operation using
6079 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
6080 The previous contents of *MEM are returned, using TARGET if possible.
6081 Since this instructionn is an acquire barrier only, stronger memory
6082 models may require additional barriers to be emitted. */
6085 maybe_emit_sync_lock_test_and_set (rtx target
, rtx mem
, rtx val
,
6086 enum memmodel model
)
6088 machine_mode mode
= GET_MODE (mem
);
6089 enum insn_code icode
;
6090 rtx_insn
*last_insn
= get_last_insn ();
6092 icode
= optab_handler (sync_lock_test_and_set_optab
, mode
);
6094 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
6095 exists, and the memory model is stronger than acquire, add a release
6096 barrier before the instruction. */
6098 if (is_mm_seq_cst (model
) || is_mm_release (model
) || is_mm_acq_rel (model
))
6099 expand_mem_thread_fence (model
);
6101 if (icode
!= CODE_FOR_nothing
)
6103 class expand_operand ops
[3];
6104 create_output_operand (&ops
[0], target
, mode
);
6105 create_fixed_operand (&ops
[1], mem
);
6106 create_input_operand (&ops
[2], val
, mode
);
6107 if (maybe_expand_insn (icode
, 3, ops
))
6108 return ops
[0].value
;
6111 /* If an external test-and-set libcall is provided, use that instead of
6112 any external compare-and-swap that we might get from the compare-and-
6113 swap-loop expansion later. */
6114 if (!can_compare_and_swap_p (mode
, false))
6116 rtx libfunc
= optab_libfunc (sync_lock_test_and_set_optab
, mode
);
6117 if (libfunc
!= NULL
)
6121 addr
= convert_memory_address (ptr_mode
, XEXP (mem
, 0));
6122 return emit_library_call_value (libfunc
, NULL_RTX
, LCT_NORMAL
,
6123 mode
, addr
, ptr_mode
,
6128 /* If the test_and_set can't be emitted, eliminate any barrier that might
6129 have been emitted. */
6130 delete_insns_since (last_insn
);
6134 /* This function tries to implement an atomic exchange operation using a
6135 compare_and_swap loop. VAL is written to *MEM. The previous contents of
6136 *MEM are returned, using TARGET if possible. No memory model is required
6137 since a compare_and_swap loop is seq-cst. */
6140 maybe_emit_compare_and_swap_exchange_loop (rtx target
, rtx mem
, rtx val
)
6142 machine_mode mode
= GET_MODE (mem
);
6144 if (can_compare_and_swap_p (mode
, true))
6146 if (!target
|| !register_operand (target
, mode
))
6147 target
= gen_reg_rtx (mode
);
6148 if (expand_compare_and_swap_loop (mem
, target
, val
, NULL_RTX
))
6155 /* This function tries to implement an atomic test-and-set operation
6156 using the atomic_test_and_set instruction pattern. A boolean value
6157 is returned from the operation, using TARGET if possible. */
6160 maybe_emit_atomic_test_and_set (rtx target
, rtx mem
, enum memmodel model
)
6162 machine_mode pat_bool_mode
;
6163 class expand_operand ops
[3];
6165 if (!targetm
.have_atomic_test_and_set ())
6168 /* While we always get QImode from __atomic_test_and_set, we get
6169 other memory modes from __sync_lock_test_and_set. Note that we
6170 use no endian adjustment here. This matches the 4.6 behavior
6171 in the Sparc backend. */
6172 enum insn_code icode
= targetm
.code_for_atomic_test_and_set
;
6173 gcc_checking_assert (insn_data
[icode
].operand
[1].mode
== QImode
);
6174 if (GET_MODE (mem
) != QImode
)
6175 mem
= adjust_address_nv (mem
, QImode
, 0);
6177 pat_bool_mode
= insn_data
[icode
].operand
[0].mode
;
6178 create_output_operand (&ops
[0], target
, pat_bool_mode
);
6179 create_fixed_operand (&ops
[1], mem
);
6180 create_integer_operand (&ops
[2], model
);
6182 if (maybe_expand_insn (icode
, 3, ops
))
6183 return ops
[0].value
;
6187 /* This function expands the legacy _sync_lock test_and_set operation which is
6188 generally an atomic exchange. Some limited targets only allow the
6189 constant 1 to be stored. This is an ACQUIRE operation.
6191 TARGET is an optional place to stick the return value.
6192 MEM is where VAL is stored. */
6195 expand_sync_lock_test_and_set (rtx target
, rtx mem
, rtx val
)
6199 /* Try an atomic_exchange first. */
6200 ret
= maybe_emit_atomic_exchange (target
, mem
, val
, MEMMODEL_SYNC_ACQUIRE
);
6204 ret
= maybe_emit_sync_lock_test_and_set (target
, mem
, val
,
6205 MEMMODEL_SYNC_ACQUIRE
);
6209 ret
= maybe_emit_compare_and_swap_exchange_loop (target
, mem
, val
);
6213 /* If there are no other options, try atomic_test_and_set if the value
6214 being stored is 1. */
6215 if (val
== const1_rtx
)
6216 ret
= maybe_emit_atomic_test_and_set (target
, mem
, MEMMODEL_SYNC_ACQUIRE
);
6221 /* This function expands the atomic test_and_set operation:
6222 atomically store a boolean TRUE into MEM and return the previous value.
6224 MEMMODEL is the memory model variant to use.
6225 TARGET is an optional place to stick the return value. */
6228 expand_atomic_test_and_set (rtx target
, rtx mem
, enum memmodel model
)
6230 machine_mode mode
= GET_MODE (mem
);
6231 rtx ret
, trueval
, subtarget
;
6233 ret
= maybe_emit_atomic_test_and_set (target
, mem
, model
);
6237 /* Be binary compatible with non-default settings of trueval, and different
6238 cpu revisions. E.g. one revision may have atomic-test-and-set, but
6239 another only has atomic-exchange. */
6240 if (targetm
.atomic_test_and_set_trueval
== 1)
6242 trueval
= const1_rtx
;
6243 subtarget
= target
? target
: gen_reg_rtx (mode
);
6247 trueval
= gen_int_mode (targetm
.atomic_test_and_set_trueval
, mode
);
6248 subtarget
= gen_reg_rtx (mode
);
6251 /* Try the atomic-exchange optab... */
6252 ret
= maybe_emit_atomic_exchange (subtarget
, mem
, trueval
, model
);
6254 /* ... then an atomic-compare-and-swap loop ... */
6256 ret
= maybe_emit_compare_and_swap_exchange_loop (subtarget
, mem
, trueval
);
6258 /* ... before trying the vaguely defined legacy lock_test_and_set. */
6260 ret
= maybe_emit_sync_lock_test_and_set (subtarget
, mem
, trueval
, model
);
6262 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
6263 things with the value 1. Thus we try again without trueval. */
6264 if (!ret
&& targetm
.atomic_test_and_set_trueval
!= 1)
6265 ret
= maybe_emit_sync_lock_test_and_set (subtarget
, mem
, const1_rtx
, model
);
6267 /* Failing all else, assume a single threaded environment and simply
6268 perform the operation. */
6271 /* If the result is ignored skip the move to target. */
6272 if (subtarget
!= const0_rtx
)
6273 emit_move_insn (subtarget
, mem
);
6275 emit_move_insn (mem
, trueval
);
6279 /* Recall that have to return a boolean value; rectify if trueval
6280 is not exactly one. */
6281 if (targetm
.atomic_test_and_set_trueval
!= 1)
6282 ret
= emit_store_flag_force (target
, NE
, ret
, const0_rtx
, mode
, 0, 1);
6287 /* This function expands the atomic exchange operation:
6288 atomically store VAL in MEM and return the previous value in MEM.
6290 MEMMODEL is the memory model variant to use.
6291 TARGET is an optional place to stick the return value. */
6294 expand_atomic_exchange (rtx target
, rtx mem
, rtx val
, enum memmodel model
)
6296 machine_mode mode
= GET_MODE (mem
);
6299 /* If loads are not atomic for the required size and we are not called to
6300 provide a __sync builtin, do not do anything so that we stay consistent
6301 with atomic loads of the same size. */
6302 if (!can_atomic_load_p (mode
) && !is_mm_sync (model
))
6305 ret
= maybe_emit_atomic_exchange (target
, mem
, val
, model
);
6307 /* Next try a compare-and-swap loop for the exchange. */
6309 ret
= maybe_emit_compare_and_swap_exchange_loop (target
, mem
, val
);
6314 /* This function expands the atomic compare exchange operation:
6316 *PTARGET_BOOL is an optional place to store the boolean success/failure.
6317 *PTARGET_OVAL is an optional place to store the old value from memory.
6318 Both target parameters may be NULL or const0_rtx to indicate that we do
6319 not care about that return value. Both target parameters are updated on
6320 success to the actual location of the corresponding result.
6322 MEMMODEL is the memory model variant to use.
6324 The return value of the function is true for success. */
6327 expand_atomic_compare_and_swap (rtx
*ptarget_bool
, rtx
*ptarget_oval
,
6328 rtx mem
, rtx expected
, rtx desired
,
6329 bool is_weak
, enum memmodel succ_model
,
6330 enum memmodel fail_model
)
6332 machine_mode mode
= GET_MODE (mem
);
6333 class expand_operand ops
[8];
6334 enum insn_code icode
;
6335 rtx target_oval
, target_bool
= NULL_RTX
;
6338 /* If loads are not atomic for the required size and we are not called to
6339 provide a __sync builtin, do not do anything so that we stay consistent
6340 with atomic loads of the same size. */
6341 if (!can_atomic_load_p (mode
) && !is_mm_sync (succ_model
))
6344 /* Load expected into a register for the compare and swap. */
6345 if (MEM_P (expected
))
6346 expected
= copy_to_reg (expected
);
6348 /* Make sure we always have some place to put the return oldval.
6349 Further, make sure that place is distinct from the input expected,
6350 just in case we need that path down below. */
6351 if (ptarget_oval
&& *ptarget_oval
== const0_rtx
)
6352 ptarget_oval
= NULL
;
6354 if (ptarget_oval
== NULL
6355 || (target_oval
= *ptarget_oval
) == NULL
6356 || reg_overlap_mentioned_p (expected
, target_oval
))
6357 target_oval
= gen_reg_rtx (mode
);
6359 icode
= direct_optab_handler (atomic_compare_and_swap_optab
, mode
);
6360 if (icode
!= CODE_FOR_nothing
)
6362 machine_mode bool_mode
= insn_data
[icode
].operand
[0].mode
;
6364 if (ptarget_bool
&& *ptarget_bool
== const0_rtx
)
6365 ptarget_bool
= NULL
;
6367 /* Make sure we always have a place for the bool operand. */
6368 if (ptarget_bool
== NULL
6369 || (target_bool
= *ptarget_bool
) == NULL
6370 || GET_MODE (target_bool
) != bool_mode
)
6371 target_bool
= gen_reg_rtx (bool_mode
);
6373 /* Emit the compare_and_swap. */
6374 create_output_operand (&ops
[0], target_bool
, bool_mode
);
6375 create_output_operand (&ops
[1], target_oval
, mode
);
6376 create_fixed_operand (&ops
[2], mem
);
6377 create_input_operand (&ops
[3], expected
, mode
);
6378 create_input_operand (&ops
[4], desired
, mode
);
6379 create_integer_operand (&ops
[5], is_weak
);
6380 create_integer_operand (&ops
[6], succ_model
);
6381 create_integer_operand (&ops
[7], fail_model
);
6382 if (maybe_expand_insn (icode
, 8, ops
))
6384 /* Return success/failure. */
6385 target_bool
= ops
[0].value
;
6386 target_oval
= ops
[1].value
;
6391 /* Otherwise fall back to the original __sync_val_compare_and_swap
6392 which is always seq-cst. */
6393 icode
= optab_handler (sync_compare_and_swap_optab
, mode
);
6394 if (icode
!= CODE_FOR_nothing
)
6398 create_output_operand (&ops
[0], target_oval
, mode
);
6399 create_fixed_operand (&ops
[1], mem
);
6400 create_input_operand (&ops
[2], expected
, mode
);
6401 create_input_operand (&ops
[3], desired
, mode
);
6402 if (!maybe_expand_insn (icode
, 4, ops
))
6405 target_oval
= ops
[0].value
;
6407 /* If the caller isn't interested in the boolean return value,
6408 skip the computation of it. */
6409 if (ptarget_bool
== NULL
)
6412 /* Otherwise, work out if the compare-and-swap succeeded. */
6414 if (have_insn_for (COMPARE
, CCmode
))
6415 note_stores (get_last_insn (), find_cc_set
, &cc_reg
);
6418 target_bool
= emit_store_flag_force (target_bool
, EQ
, cc_reg
,
6419 const0_rtx
, VOIDmode
, 0, 1);
6422 goto success_bool_from_val
;
6425 /* Also check for library support for __sync_val_compare_and_swap. */
6426 libfunc
= optab_libfunc (sync_compare_and_swap_optab
, mode
);
6427 if (libfunc
!= NULL
)
6429 rtx addr
= convert_memory_address (ptr_mode
, XEXP (mem
, 0));
6430 rtx target
= emit_library_call_value (libfunc
, NULL_RTX
, LCT_NORMAL
,
6431 mode
, addr
, ptr_mode
,
6432 expected
, mode
, desired
, mode
);
6433 emit_move_insn (target_oval
, target
);
6435 /* Compute the boolean return value only if requested. */
6437 goto success_bool_from_val
;
6445 success_bool_from_val
:
6446 target_bool
= emit_store_flag_force (target_bool
, EQ
, target_oval
,
6447 expected
, VOIDmode
, 1, 1);
6449 /* Make sure that the oval output winds up where the caller asked. */
6451 *ptarget_oval
= target_oval
;
6453 *ptarget_bool
= target_bool
;
6457 /* Generate asm volatile("" : : : "memory") as the memory blockage. */
6460 expand_asm_memory_blockage (void)
6464 asm_op
= gen_rtx_ASM_OPERANDS (VOIDmode
, "", "", 0,
6465 rtvec_alloc (0), rtvec_alloc (0),
6466 rtvec_alloc (0), UNKNOWN_LOCATION
);
6467 MEM_VOLATILE_P (asm_op
) = 1;
6469 clob
= gen_rtx_SCRATCH (VOIDmode
);
6470 clob
= gen_rtx_MEM (BLKmode
, clob
);
6471 clob
= gen_rtx_CLOBBER (VOIDmode
, clob
);
6473 emit_insn (gen_rtx_PARALLEL (VOIDmode
, gen_rtvec (2, asm_op
, clob
)));
6476 /* Do not propagate memory accesses across this point. */
6479 expand_memory_blockage (void)
6481 if (targetm
.have_memory_blockage ())
6482 emit_insn (targetm
.gen_memory_blockage ());
6484 expand_asm_memory_blockage ();
6487 /* This routine will either emit the mem_thread_fence pattern or issue a
6488 sync_synchronize to generate a fence for memory model MEMMODEL. */
6491 expand_mem_thread_fence (enum memmodel model
)
6493 if (is_mm_relaxed (model
))
6495 if (targetm
.have_mem_thread_fence ())
6497 emit_insn (targetm
.gen_mem_thread_fence (GEN_INT (model
)));
6498 expand_memory_blockage ();
6500 else if (targetm
.have_memory_barrier ())
6501 emit_insn (targetm
.gen_memory_barrier ());
6502 else if (synchronize_libfunc
!= NULL_RTX
)
6503 emit_library_call (synchronize_libfunc
, LCT_NORMAL
, VOIDmode
);
6505 expand_memory_blockage ();
6508 /* Emit a signal fence with given memory model. */
6511 expand_mem_signal_fence (enum memmodel model
)
6513 /* No machine barrier is required to implement a signal fence, but
6514 a compiler memory barrier must be issued, except for relaxed MM. */
6515 if (!is_mm_relaxed (model
))
6516 expand_memory_blockage ();
6519 /* This function expands the atomic load operation:
6520 return the atomically loaded value in MEM.
6522 MEMMODEL is the memory model variant to use.
6523 TARGET is an option place to stick the return value. */
6526 expand_atomic_load (rtx target
, rtx mem
, enum memmodel model
)
6528 machine_mode mode
= GET_MODE (mem
);
6529 enum insn_code icode
;
6531 /* If the target supports the load directly, great. */
6532 icode
= direct_optab_handler (atomic_load_optab
, mode
);
6533 if (icode
!= CODE_FOR_nothing
)
6535 class expand_operand ops
[3];
6536 rtx_insn
*last
= get_last_insn ();
6537 if (is_mm_seq_cst (model
))
6538 expand_memory_blockage ();
6540 create_output_operand (&ops
[0], target
, mode
);
6541 create_fixed_operand (&ops
[1], mem
);
6542 create_integer_operand (&ops
[2], model
);
6543 if (maybe_expand_insn (icode
, 3, ops
))
6545 if (!is_mm_relaxed (model
))
6546 expand_memory_blockage ();
6547 return ops
[0].value
;
6549 delete_insns_since (last
);
6552 /* If the size of the object is greater than word size on this target,
6553 then we assume that a load will not be atomic. We could try to
6554 emulate a load with a compare-and-swap operation, but the store that
6555 doing this could result in would be incorrect if this is a volatile
6556 atomic load or targetting read-only-mapped memory. */
6557 if (maybe_gt (GET_MODE_PRECISION (mode
), BITS_PER_WORD
))
6558 /* If there is no atomic load, leave the library call. */
6561 /* Otherwise assume loads are atomic, and emit the proper barriers. */
6562 if (!target
|| target
== const0_rtx
)
6563 target
= gen_reg_rtx (mode
);
6565 /* For SEQ_CST, emit a barrier before the load. */
6566 if (is_mm_seq_cst (model
))
6567 expand_mem_thread_fence (model
);
6569 emit_move_insn (target
, mem
);
6571 /* Emit the appropriate barrier after the load. */
6572 expand_mem_thread_fence (model
);
6577 /* This function expands the atomic store operation:
6578 Atomically store VAL in MEM.
6579 MEMMODEL is the memory model variant to use.
6580 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
6581 function returns const0_rtx if a pattern was emitted. */
6584 expand_atomic_store (rtx mem
, rtx val
, enum memmodel model
, bool use_release
)
6586 machine_mode mode
= GET_MODE (mem
);
6587 enum insn_code icode
;
6588 class expand_operand ops
[3];
6590 /* If the target supports the store directly, great. */
6591 icode
= direct_optab_handler (atomic_store_optab
, mode
);
6592 if (icode
!= CODE_FOR_nothing
)
6594 rtx_insn
*last
= get_last_insn ();
6595 if (!is_mm_relaxed (model
))
6596 expand_memory_blockage ();
6597 create_fixed_operand (&ops
[0], mem
);
6598 create_input_operand (&ops
[1], val
, mode
);
6599 create_integer_operand (&ops
[2], model
);
6600 if (maybe_expand_insn (icode
, 3, ops
))
6602 if (is_mm_seq_cst (model
))
6603 expand_memory_blockage ();
6606 delete_insns_since (last
);
6609 /* If using __sync_lock_release is a viable alternative, try it.
6610 Note that this will not be set to true if we are expanding a generic
6611 __atomic_store_n. */
6614 icode
= direct_optab_handler (sync_lock_release_optab
, mode
);
6615 if (icode
!= CODE_FOR_nothing
)
6617 create_fixed_operand (&ops
[0], mem
);
6618 create_input_operand (&ops
[1], const0_rtx
, mode
);
6619 if (maybe_expand_insn (icode
, 2, ops
))
6621 /* lock_release is only a release barrier. */
6622 if (is_mm_seq_cst (model
))
6623 expand_mem_thread_fence (model
);
6629 /* If the size of the object is greater than word size on this target,
6630 a default store will not be atomic. */
6631 if (maybe_gt (GET_MODE_PRECISION (mode
), BITS_PER_WORD
))
6633 /* If loads are atomic or we are called to provide a __sync builtin,
6634 we can try a atomic_exchange and throw away the result. Otherwise,
6635 don't do anything so that we do not create an inconsistency between
6636 loads and stores. */
6637 if (can_atomic_load_p (mode
) || is_mm_sync (model
))
6639 rtx target
= maybe_emit_atomic_exchange (NULL_RTX
, mem
, val
, model
);
6641 target
= maybe_emit_compare_and_swap_exchange_loop (NULL_RTX
, mem
,
6649 /* Otherwise assume stores are atomic, and emit the proper barriers. */
6650 expand_mem_thread_fence (model
);
6652 emit_move_insn (mem
, val
);
6654 /* For SEQ_CST, also emit a barrier after the store. */
6655 if (is_mm_seq_cst (model
))
6656 expand_mem_thread_fence (model
);
6662 /* Structure containing the pointers and values required to process the
6663 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
6665 struct atomic_op_functions
6667 direct_optab mem_fetch_before
;
6668 direct_optab mem_fetch_after
;
6669 direct_optab mem_no_result
;
6672 direct_optab no_result
;
6673 enum rtx_code reverse_code
;
6677 /* Fill in structure pointed to by OP with the various optab entries for an
6678 operation of type CODE. */
6681 get_atomic_op_for_code (struct atomic_op_functions
*op
, enum rtx_code code
)
6683 gcc_assert (op
!= NULL
);
6685 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
6686 in the source code during compilation, and the optab entries are not
6687 computable until runtime. Fill in the values at runtime. */
6691 op
->mem_fetch_before
= atomic_fetch_add_optab
;
6692 op
->mem_fetch_after
= atomic_add_fetch_optab
;
6693 op
->mem_no_result
= atomic_add_optab
;
6694 op
->fetch_before
= sync_old_add_optab
;
6695 op
->fetch_after
= sync_new_add_optab
;
6696 op
->no_result
= sync_add_optab
;
6697 op
->reverse_code
= MINUS
;
6700 op
->mem_fetch_before
= atomic_fetch_sub_optab
;
6701 op
->mem_fetch_after
= atomic_sub_fetch_optab
;
6702 op
->mem_no_result
= atomic_sub_optab
;
6703 op
->fetch_before
= sync_old_sub_optab
;
6704 op
->fetch_after
= sync_new_sub_optab
;
6705 op
->no_result
= sync_sub_optab
;
6706 op
->reverse_code
= PLUS
;
6709 op
->mem_fetch_before
= atomic_fetch_xor_optab
;
6710 op
->mem_fetch_after
= atomic_xor_fetch_optab
;
6711 op
->mem_no_result
= atomic_xor_optab
;
6712 op
->fetch_before
= sync_old_xor_optab
;
6713 op
->fetch_after
= sync_new_xor_optab
;
6714 op
->no_result
= sync_xor_optab
;
6715 op
->reverse_code
= XOR
;
6718 op
->mem_fetch_before
= atomic_fetch_and_optab
;
6719 op
->mem_fetch_after
= atomic_and_fetch_optab
;
6720 op
->mem_no_result
= atomic_and_optab
;
6721 op
->fetch_before
= sync_old_and_optab
;
6722 op
->fetch_after
= sync_new_and_optab
;
6723 op
->no_result
= sync_and_optab
;
6724 op
->reverse_code
= UNKNOWN
;
6727 op
->mem_fetch_before
= atomic_fetch_or_optab
;
6728 op
->mem_fetch_after
= atomic_or_fetch_optab
;
6729 op
->mem_no_result
= atomic_or_optab
;
6730 op
->fetch_before
= sync_old_ior_optab
;
6731 op
->fetch_after
= sync_new_ior_optab
;
6732 op
->no_result
= sync_ior_optab
;
6733 op
->reverse_code
= UNKNOWN
;
6736 op
->mem_fetch_before
= atomic_fetch_nand_optab
;
6737 op
->mem_fetch_after
= atomic_nand_fetch_optab
;
6738 op
->mem_no_result
= atomic_nand_optab
;
6739 op
->fetch_before
= sync_old_nand_optab
;
6740 op
->fetch_after
= sync_new_nand_optab
;
6741 op
->no_result
= sync_nand_optab
;
6742 op
->reverse_code
= UNKNOWN
;
6749 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
6750 using memory order MODEL. If AFTER is true the operation needs to return
6751 the value of *MEM after the operation, otherwise the previous value.
6752 TARGET is an optional place to place the result. The result is unused if
6754 Return the result if there is a better sequence, otherwise NULL_RTX. */
6757 maybe_optimize_fetch_op (rtx target
, rtx mem
, rtx val
, enum rtx_code code
,
6758 enum memmodel model
, bool after
)
6760 /* If the value is prefetched, or not used, it may be possible to replace
6761 the sequence with a native exchange operation. */
6762 if (!after
|| target
== const0_rtx
)
6764 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
6765 if (code
== AND
&& val
== const0_rtx
)
6767 if (target
== const0_rtx
)
6768 target
= gen_reg_rtx (GET_MODE (mem
));
6769 return maybe_emit_atomic_exchange (target
, mem
, val
, model
);
6772 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
6773 if (code
== IOR
&& val
== constm1_rtx
)
6775 if (target
== const0_rtx
)
6776 target
= gen_reg_rtx (GET_MODE (mem
));
6777 return maybe_emit_atomic_exchange (target
, mem
, val
, model
);
6784 /* Try to emit an instruction for a specific operation varaition.
6785 OPTAB contains the OP functions.
6786 TARGET is an optional place to return the result. const0_rtx means unused.
6787 MEM is the memory location to operate on.
6788 VAL is the value to use in the operation.
6789 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
6790 MODEL is the memory model, if used.
6791 AFTER is true if the returned result is the value after the operation. */
6794 maybe_emit_op (const struct atomic_op_functions
*optab
, rtx target
, rtx mem
,
6795 rtx val
, bool use_memmodel
, enum memmodel model
, bool after
)
6797 machine_mode mode
= GET_MODE (mem
);
6798 class expand_operand ops
[4];
6799 enum insn_code icode
;
6803 /* Check to see if there is a result returned. */
6804 if (target
== const0_rtx
)
6808 icode
= direct_optab_handler (optab
->mem_no_result
, mode
);
6809 create_integer_operand (&ops
[2], model
);
6814 icode
= direct_optab_handler (optab
->no_result
, mode
);
6818 /* Otherwise, we need to generate a result. */
6823 icode
= direct_optab_handler (after
? optab
->mem_fetch_after
6824 : optab
->mem_fetch_before
, mode
);
6825 create_integer_operand (&ops
[3], model
);
6830 icode
= optab_handler (after
? optab
->fetch_after
6831 : optab
->fetch_before
, mode
);
6834 create_output_operand (&ops
[op_counter
++], target
, mode
);
6836 if (icode
== CODE_FOR_nothing
)
6839 create_fixed_operand (&ops
[op_counter
++], mem
);
6840 /* VAL may have been promoted to a wider mode. Shrink it if so. */
6841 create_convert_operand_to (&ops
[op_counter
++], val
, mode
, true);
6843 if (maybe_expand_insn (icode
, num_ops
, ops
))
6844 return (target
== const0_rtx
? const0_rtx
: ops
[0].value
);
6850 /* This function expands an atomic fetch_OP or OP_fetch operation:
6851 TARGET is an option place to stick the return value. const0_rtx indicates
6852 the result is unused.
6853 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6854 CODE is the operation being performed (OP)
6855 MEMMODEL is the memory model variant to use.
6856 AFTER is true to return the result of the operation (OP_fetch).
6857 AFTER is false to return the value before the operation (fetch_OP).
6859 This function will *only* generate instructions if there is a direct
6860 optab. No compare and swap loops or libcalls will be generated. */
6863 expand_atomic_fetch_op_no_fallback (rtx target
, rtx mem
, rtx val
,
6864 enum rtx_code code
, enum memmodel model
,
6867 machine_mode mode
= GET_MODE (mem
);
6868 struct atomic_op_functions optab
;
6870 bool unused_result
= (target
== const0_rtx
);
6872 get_atomic_op_for_code (&optab
, code
);
6874 /* Check to see if there are any better instructions. */
6875 result
= maybe_optimize_fetch_op (target
, mem
, val
, code
, model
, after
);
6879 /* Check for the case where the result isn't used and try those patterns. */
6882 /* Try the memory model variant first. */
6883 result
= maybe_emit_op (&optab
, target
, mem
, val
, true, model
, true);
6887 /* Next try the old style withuot a memory model. */
6888 result
= maybe_emit_op (&optab
, target
, mem
, val
, false, model
, true);
6892 /* There is no no-result pattern, so try patterns with a result. */
6896 /* Try the __atomic version. */
6897 result
= maybe_emit_op (&optab
, target
, mem
, val
, true, model
, after
);
6901 /* Try the older __sync version. */
6902 result
= maybe_emit_op (&optab
, target
, mem
, val
, false, model
, after
);
6906 /* If the fetch value can be calculated from the other variation of fetch,
6907 try that operation. */
6908 if (after
|| unused_result
|| optab
.reverse_code
!= UNKNOWN
)
6910 /* Try the __atomic version, then the older __sync version. */
6911 result
= maybe_emit_op (&optab
, target
, mem
, val
, true, model
, !after
);
6913 result
= maybe_emit_op (&optab
, target
, mem
, val
, false, model
, !after
);
6917 /* If the result isn't used, no need to do compensation code. */
6921 /* Issue compensation code. Fetch_after == fetch_before OP val.
6922 Fetch_before == after REVERSE_OP val. */
6924 code
= optab
.reverse_code
;
6927 result
= expand_simple_binop (mode
, AND
, result
, val
, NULL_RTX
,
6928 true, OPTAB_LIB_WIDEN
);
6929 result
= expand_simple_unop (mode
, NOT
, result
, target
, true);
6932 result
= expand_simple_binop (mode
, code
, result
, val
, target
,
6933 true, OPTAB_LIB_WIDEN
);
6938 /* No direct opcode can be generated. */
6944 /* This function expands an atomic fetch_OP or OP_fetch operation:
6945 TARGET is an option place to stick the return value. const0_rtx indicates
6946 the result is unused.
6947 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6948 CODE is the operation being performed (OP)
6949 MEMMODEL is the memory model variant to use.
6950 AFTER is true to return the result of the operation (OP_fetch).
6951 AFTER is false to return the value before the operation (fetch_OP). */
6953 expand_atomic_fetch_op (rtx target
, rtx mem
, rtx val
, enum rtx_code code
,
6954 enum memmodel model
, bool after
)
6956 machine_mode mode
= GET_MODE (mem
);
6958 bool unused_result
= (target
== const0_rtx
);
6960 /* If loads are not atomic for the required size and we are not called to
6961 provide a __sync builtin, do not do anything so that we stay consistent
6962 with atomic loads of the same size. */
6963 if (!can_atomic_load_p (mode
) && !is_mm_sync (model
))
6966 result
= expand_atomic_fetch_op_no_fallback (target
, mem
, val
, code
, model
,
6972 /* Add/sub can be implemented by doing the reverse operation with -(val). */
6973 if (code
== PLUS
|| code
== MINUS
)
6976 enum rtx_code reverse
= (code
== PLUS
? MINUS
: PLUS
);
6979 tmp
= expand_simple_unop (mode
, NEG
, val
, NULL_RTX
, true);
6980 result
= expand_atomic_fetch_op_no_fallback (target
, mem
, tmp
, reverse
,
6984 /* PLUS worked so emit the insns and return. */
6991 /* PLUS did not work, so throw away the negation code and continue. */
6995 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
6996 if (!can_compare_and_swap_p (mode
, false))
7000 enum rtx_code orig_code
= code
;
7001 struct atomic_op_functions optab
;
7003 get_atomic_op_for_code (&optab
, code
);
7004 libfunc
= optab_libfunc (after
? optab
.fetch_after
7005 : optab
.fetch_before
, mode
);
7007 && (after
|| unused_result
|| optab
.reverse_code
!= UNKNOWN
))
7011 code
= optab
.reverse_code
;
7012 libfunc
= optab_libfunc (after
? optab
.fetch_before
7013 : optab
.fetch_after
, mode
);
7015 if (libfunc
!= NULL
)
7017 rtx addr
= convert_memory_address (ptr_mode
, XEXP (mem
, 0));
7018 result
= emit_library_call_value (libfunc
, NULL
, LCT_NORMAL
, mode
,
7019 addr
, ptr_mode
, val
, mode
);
7021 if (!unused_result
&& fixup
)
7022 result
= expand_simple_binop (mode
, code
, result
, val
, target
,
7023 true, OPTAB_LIB_WIDEN
);
7027 /* We need the original code for any further attempts. */
7031 /* If nothing else has succeeded, default to a compare and swap loop. */
7032 if (can_compare_and_swap_p (mode
, true))
7035 rtx t0
= gen_reg_rtx (mode
), t1
;
7039 /* If the result is used, get a register for it. */
7042 if (!target
|| !register_operand (target
, mode
))
7043 target
= gen_reg_rtx (mode
);
7044 /* If fetch_before, copy the value now. */
7046 emit_move_insn (target
, t0
);
7049 target
= const0_rtx
;
7054 t1
= expand_simple_binop (mode
, AND
, t1
, val
, NULL_RTX
,
7055 true, OPTAB_LIB_WIDEN
);
7056 t1
= expand_simple_unop (mode
, code
, t1
, NULL_RTX
, true);
7059 t1
= expand_simple_binop (mode
, code
, t1
, val
, NULL_RTX
, true,
7062 /* For after, copy the value now. */
7063 if (!unused_result
&& after
)
7064 emit_move_insn (target
, t1
);
7065 insn
= get_insns ();
7068 if (t1
!= NULL
&& expand_compare_and_swap_loop (mem
, t0
, t1
, insn
))
7075 /* Return true if OPERAND is suitable for operand number OPNO of
7076 instruction ICODE. */
7079 insn_operand_matches (enum insn_code icode
, unsigned int opno
, rtx operand
)
7081 return (!insn_data
[(int) icode
].operand
[opno
].predicate
7082 || (insn_data
[(int) icode
].operand
[opno
].predicate
7083 (operand
, insn_data
[(int) icode
].operand
[opno
].mode
)));
7086 /* TARGET is a target of a multiword operation that we are going to
7087 implement as a series of word-mode operations. Return true if
7088 TARGET is suitable for this purpose. */
7091 valid_multiword_target_p (rtx target
)
7096 mode
= GET_MODE (target
);
7097 if (!GET_MODE_SIZE (mode
).is_constant (&size
))
7099 for (i
= 0; i
< size
; i
+= UNITS_PER_WORD
)
7100 if (!validate_subreg (word_mode
, mode
, target
, i
))
7105 /* Make OP describe an input operand that has value INTVAL and that has
7106 no inherent mode. This function should only be used for operands that
7107 are always expand-time constants. The backend may request that INTVAL
7108 be copied into a different kind of rtx, but it must specify the mode
7109 of that rtx if so. */
7112 create_integer_operand (class expand_operand
*op
, poly_int64 intval
)
7114 create_expand_operand (op
, EXPAND_INTEGER
,
7115 gen_int_mode (intval
, MAX_MODE_INT
),
7116 VOIDmode
, false, intval
);
7119 /* Like maybe_legitimize_operand, but do not change the code of the
7120 current rtx value. */
7123 maybe_legitimize_operand_same_code (enum insn_code icode
, unsigned int opno
,
7124 class expand_operand
*op
)
7126 /* See if the operand matches in its current form. */
7127 if (insn_operand_matches (icode
, opno
, op
->value
))
7130 /* If the operand is a memory whose address has no side effects,
7131 try forcing the address into a non-virtual pseudo register.
7132 The check for side effects is important because copy_to_mode_reg
7133 cannot handle things like auto-modified addresses. */
7134 if (insn_data
[(int) icode
].operand
[opno
].allows_mem
&& MEM_P (op
->value
))
7139 addr
= XEXP (mem
, 0);
7140 if (!(REG_P (addr
) && REGNO (addr
) > LAST_VIRTUAL_REGISTER
)
7141 && !side_effects_p (addr
))
7146 last
= get_last_insn ();
7147 mode
= get_address_mode (mem
);
7148 mem
= replace_equiv_address (mem
, copy_to_mode_reg (mode
, addr
));
7149 if (insn_operand_matches (icode
, opno
, mem
))
7154 delete_insns_since (last
);
7161 /* Try to make OP match operand OPNO of instruction ICODE. Return true
7162 on success, storing the new operand value back in OP. */
7165 maybe_legitimize_operand (enum insn_code icode
, unsigned int opno
,
7166 class expand_operand
*op
)
7168 machine_mode mode
, imode
, tmode
;
7175 temporary_volatile_ok
v (true);
7176 return maybe_legitimize_operand_same_code (icode
, opno
, op
);
7180 gcc_assert (mode
!= VOIDmode
);
7182 && op
->value
!= const0_rtx
7183 && GET_MODE (op
->value
) == mode
7184 && maybe_legitimize_operand_same_code (icode
, opno
, op
))
7187 op
->value
= gen_reg_rtx (mode
);
7193 gcc_assert (mode
!= VOIDmode
);
7194 gcc_assert (GET_MODE (op
->value
) == VOIDmode
7195 || GET_MODE (op
->value
) == mode
);
7196 if (maybe_legitimize_operand_same_code (icode
, opno
, op
))
7199 op
->value
= copy_to_mode_reg (mode
, op
->value
);
7202 case EXPAND_CONVERT_TO
:
7203 gcc_assert (mode
!= VOIDmode
);
7204 op
->value
= convert_to_mode (mode
, op
->value
, op
->unsigned_p
);
7207 case EXPAND_CONVERT_FROM
:
7208 if (GET_MODE (op
->value
) != VOIDmode
)
7209 mode
= GET_MODE (op
->value
);
7211 /* The caller must tell us what mode this value has. */
7212 gcc_assert (mode
!= VOIDmode
);
7214 imode
= insn_data
[(int) icode
].operand
[opno
].mode
;
7215 tmode
= (VECTOR_MODE_P (imode
) && !VECTOR_MODE_P (mode
)
7216 ? GET_MODE_INNER (imode
) : imode
);
7217 if (tmode
!= VOIDmode
&& tmode
!= mode
)
7219 op
->value
= convert_modes (tmode
, mode
, op
->value
, op
->unsigned_p
);
7222 if (imode
!= VOIDmode
&& imode
!= mode
)
7224 gcc_assert (VECTOR_MODE_P (imode
) && !VECTOR_MODE_P (mode
));
7225 op
->value
= expand_vector_broadcast (imode
, op
->value
);
7230 case EXPAND_ADDRESS
:
7231 op
->value
= convert_memory_address (as_a
<scalar_int_mode
> (mode
),
7235 case EXPAND_INTEGER
:
7236 mode
= insn_data
[(int) icode
].operand
[opno
].mode
;
7237 if (mode
!= VOIDmode
7238 && known_eq (trunc_int_for_mode (op
->int_value
, mode
),
7241 op
->value
= gen_int_mode (op
->int_value
, mode
);
7246 return insn_operand_matches (icode
, opno
, op
->value
);
7249 /* Make OP describe an input operand that should have the same value
7250 as VALUE, after any mode conversion that the target might request.
7251 TYPE is the type of VALUE. */
7254 create_convert_operand_from_type (class expand_operand
*op
,
7255 rtx value
, tree type
)
7257 create_convert_operand_from (op
, value
, TYPE_MODE (type
),
7258 TYPE_UNSIGNED (type
));
7261 /* Return true if the requirements on operands OP1 and OP2 of instruction
7262 ICODE are similar enough for the result of legitimizing OP1 to be
7263 reusable for OP2. OPNO1 and OPNO2 are the operand numbers associated
7264 with OP1 and OP2 respectively. */
7267 can_reuse_operands_p (enum insn_code icode
,
7268 unsigned int opno1
, unsigned int opno2
,
7269 const class expand_operand
*op1
,
7270 const class expand_operand
*op2
)
7272 /* Check requirements that are common to all types. */
7273 if (op1
->type
!= op2
->type
7274 || op1
->mode
!= op2
->mode
7275 || (insn_data
[(int) icode
].operand
[opno1
].mode
7276 != insn_data
[(int) icode
].operand
[opno2
].mode
))
7279 /* Check the requirements for specific types. */
7283 /* Outputs must remain distinct. */
7288 case EXPAND_ADDRESS
:
7289 case EXPAND_INTEGER
:
7292 case EXPAND_CONVERT_TO
:
7293 case EXPAND_CONVERT_FROM
:
7294 return op1
->unsigned_p
== op2
->unsigned_p
;
7299 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
7300 of instruction ICODE. Return true on success, leaving the new operand
7301 values in the OPS themselves. Emit no code on failure. */
7304 maybe_legitimize_operands (enum insn_code icode
, unsigned int opno
,
7305 unsigned int nops
, class expand_operand
*ops
)
7307 rtx_insn
*last
= get_last_insn ();
7308 rtx
*orig_values
= XALLOCAVEC (rtx
, nops
);
7309 for (unsigned int i
= 0; i
< nops
; i
++)
7311 orig_values
[i
] = ops
[i
].value
;
7313 /* First try reusing the result of an earlier legitimization.
7314 This avoids duplicate rtl and ensures that tied operands
7317 This search is linear, but NOPS is bounded at compile time
7318 to a small number (current a single digit). */
7321 if (can_reuse_operands_p (icode
, opno
+ j
, opno
+ i
, &ops
[j
], &ops
[i
])
7322 && rtx_equal_p (orig_values
[j
], orig_values
[i
])
7324 && insn_operand_matches (icode
, opno
+ i
, ops
[j
].value
))
7326 ops
[i
].value
= copy_rtx (ops
[j
].value
);
7330 /* Otherwise try legitimizing the operand on its own. */
7331 if (j
== i
&& !maybe_legitimize_operand (icode
, opno
+ i
, &ops
[i
]))
7333 delete_insns_since (last
);
7340 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
7341 as its operands. Return the instruction pattern on success,
7342 and emit any necessary set-up code. Return null and emit no
7346 maybe_gen_insn (enum insn_code icode
, unsigned int nops
,
7347 class expand_operand
*ops
)
7349 gcc_assert (nops
== (unsigned int) insn_data
[(int) icode
].n_generator_args
);
7350 if (!maybe_legitimize_operands (icode
, 0, nops
, ops
))
7356 return GEN_FCN (icode
) (ops
[0].value
);
7358 return GEN_FCN (icode
) (ops
[0].value
, ops
[1].value
);
7360 return GEN_FCN (icode
) (ops
[0].value
, ops
[1].value
, ops
[2].value
);
7362 return GEN_FCN (icode
) (ops
[0].value
, ops
[1].value
, ops
[2].value
,
7365 return GEN_FCN (icode
) (ops
[0].value
, ops
[1].value
, ops
[2].value
,
7366 ops
[3].value
, ops
[4].value
);
7368 return GEN_FCN (icode
) (ops
[0].value
, ops
[1].value
, ops
[2].value
,
7369 ops
[3].value
, ops
[4].value
, ops
[5].value
);
7371 return GEN_FCN (icode
) (ops
[0].value
, ops
[1].value
, ops
[2].value
,
7372 ops
[3].value
, ops
[4].value
, ops
[5].value
,
7375 return GEN_FCN (icode
) (ops
[0].value
, ops
[1].value
, ops
[2].value
,
7376 ops
[3].value
, ops
[4].value
, ops
[5].value
,
7377 ops
[6].value
, ops
[7].value
);
7379 return GEN_FCN (icode
) (ops
[0].value
, ops
[1].value
, ops
[2].value
,
7380 ops
[3].value
, ops
[4].value
, ops
[5].value
,
7381 ops
[6].value
, ops
[7].value
, ops
[8].value
);
7386 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
7387 as its operands. Return true on success and emit no code on failure. */
7390 maybe_expand_insn (enum insn_code icode
, unsigned int nops
,
7391 class expand_operand
*ops
)
7393 rtx_insn
*pat
= maybe_gen_insn (icode
, nops
, ops
);
7402 /* Like maybe_expand_insn, but for jumps. */
7405 maybe_expand_jump_insn (enum insn_code icode
, unsigned int nops
,
7406 class expand_operand
*ops
)
7408 rtx_insn
*pat
= maybe_gen_insn (icode
, nops
, ops
);
7411 emit_jump_insn (pat
);
7417 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
7421 expand_insn (enum insn_code icode
, unsigned int nops
,
7422 class expand_operand
*ops
)
7424 if (!maybe_expand_insn (icode
, nops
, ops
))
7428 /* Like expand_insn, but for jumps. */
7431 expand_jump_insn (enum insn_code icode
, unsigned int nops
,
7432 class expand_operand
*ops
)
7434 if (!maybe_expand_jump_insn (icode
, nops
, ops
))