PR target/84226
[official-gcc.git] / gcc / optabs.c
blob5f61e4e9521b58b9cdb7f8207d69fa8e042feecf
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "memmodel.h"
29 #include "predict.h"
30 #include "tm_p.h"
31 #include "expmed.h"
32 #include "optabs.h"
33 #include "emit-rtl.h"
34 #include "recog.h"
35 #include "diagnostic-core.h"
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"
41 #include "except.h"
42 #include "dojump.h"
43 #include "explow.h"
44 #include "expr.h"
45 #include "optabs-tree.h"
46 #include "libfuncs.h"
48 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
49 machine_mode *);
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).
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. */
66 static int
67 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
69 rtx_insn *last_insn;
70 rtx set;
71 rtx note;
73 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
75 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
76 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
77 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
78 && GET_RTX_CLASS (code) != RTX_COMPARE
79 && GET_RTX_CLASS (code) != RTX_UNARY)
80 return 1;
82 if (GET_CODE (target) == ZERO_EXTRACT)
83 return 1;
85 for (last_insn = insns;
86 NEXT_INSN (last_insn) != NULL_RTX;
87 last_insn = NEXT_INSN (last_insn))
90 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
91 a value changing in the insn, so the note would be invalid for CSE. */
92 if (reg_overlap_mentioned_p (target, op0)
93 || (op1 && reg_overlap_mentioned_p (target, op1)))
95 if (MEM_P (target)
96 && (rtx_equal_p (target, op0)
97 || (op1 && rtx_equal_p (target, op1))))
99 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
100 over expanding it as temp = MEM op X, MEM = temp. If the target
101 supports MEM = MEM op X instructions, it is sometimes too hard
102 to reconstruct that form later, especially if X is also a memory,
103 and due to multiple occurrences of addresses the address might
104 be forced into register unnecessarily.
105 Note that not emitting the REG_EQUIV note might inhibit
106 CSE in some cases. */
107 set = single_set (last_insn);
108 if (set
109 && GET_CODE (SET_SRC (set)) == code
110 && MEM_P (SET_DEST (set))
111 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
112 || (op1 && rtx_equal_p (SET_DEST (set),
113 XEXP (SET_SRC (set), 1)))))
114 return 1;
116 return 0;
119 set = set_for_reg_notes (last_insn);
120 if (set == NULL_RTX)
121 return 1;
123 if (! rtx_equal_p (SET_DEST (set), target)
124 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
125 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
126 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
127 return 1;
129 if (GET_RTX_CLASS (code) == RTX_UNARY)
130 switch (code)
132 case FFS:
133 case CLZ:
134 case CTZ:
135 case CLRSB:
136 case POPCOUNT:
137 case PARITY:
138 case BSWAP:
139 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
141 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
142 if (GET_MODE_UNIT_SIZE (GET_MODE (op0))
143 > GET_MODE_UNIT_SIZE (GET_MODE (target)))
144 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
145 note, GET_MODE (op0));
146 else
147 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
148 note, GET_MODE (op0));
149 break;
151 /* FALLTHRU */
152 default:
153 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
154 break;
156 else
157 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
159 set_unique_reg_note (last_insn, REG_EQUAL, note);
161 return 1;
164 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
165 for a widening operation would be. In most cases this would be OP0, but if
166 that's a constant it'll be VOIDmode, which isn't useful. */
168 static machine_mode
169 widened_mode (machine_mode to_mode, rtx op0, rtx op1)
171 machine_mode m0 = GET_MODE (op0);
172 machine_mode m1 = GET_MODE (op1);
173 machine_mode result;
175 if (m0 == VOIDmode && m1 == VOIDmode)
176 return to_mode;
177 else if (m0 == VOIDmode || GET_MODE_UNIT_SIZE (m0) < GET_MODE_UNIT_SIZE (m1))
178 result = m1;
179 else
180 result = m0;
182 if (GET_MODE_UNIT_SIZE (result) > GET_MODE_UNIT_SIZE (to_mode))
183 return to_mode;
185 return result;
188 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
189 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
190 not actually do a sign-extend or zero-extend, but can leave the
191 higher-order bits of the result rtx undefined, for example, in the case
192 of logical operations, but not right shifts. */
194 static rtx
195 widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
196 int unsignedp, int no_extend)
198 rtx result;
199 scalar_int_mode int_mode;
201 /* If we don't have to extend and this is a constant, return it. */
202 if (no_extend && GET_MODE (op) == VOIDmode)
203 return op;
205 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
206 extend since it will be more efficient to do so unless the signedness of
207 a promoted object differs from our extension. */
208 if (! no_extend
209 || !is_a <scalar_int_mode> (mode, &int_mode)
210 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
211 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
212 return convert_modes (mode, oldmode, op, unsignedp);
214 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
215 SUBREG. */
216 if (GET_MODE_SIZE (int_mode) <= UNITS_PER_WORD)
217 return gen_lowpart (int_mode, force_reg (GET_MODE (op), op));
219 /* Otherwise, get an object of MODE, clobber it, and set the low-order
220 part to OP. */
222 result = gen_reg_rtx (int_mode);
223 emit_clobber (result);
224 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
225 return result;
228 /* Expand vector widening operations.
230 There are two different classes of operations handled here:
231 1) Operations whose result is wider than all the arguments to the operation.
232 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
233 In this case OP0 and optionally OP1 would be initialized,
234 but WIDE_OP wouldn't (not relevant for this case).
235 2) Operations whose result is of the same size as the last argument to the
236 operation, but wider than all the other arguments to the operation.
237 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
238 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
240 E.g, when called to expand the following operations, this is how
241 the arguments will be initialized:
242 nops OP0 OP1 WIDE_OP
243 widening-sum 2 oprnd0 - oprnd1
244 widening-dot-product 3 oprnd0 oprnd1 oprnd2
245 widening-mult 2 oprnd0 oprnd1 -
246 type-promotion (vec-unpack) 1 oprnd0 - - */
249 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
250 rtx target, int unsignedp)
252 struct expand_operand eops[4];
253 tree oprnd0, oprnd1, oprnd2;
254 machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
255 optab widen_pattern_optab;
256 enum insn_code icode;
257 int nops = TREE_CODE_LENGTH (ops->code);
258 int op;
260 oprnd0 = ops->op0;
261 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
262 widen_pattern_optab =
263 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
264 if (ops->code == WIDEN_MULT_PLUS_EXPR
265 || ops->code == WIDEN_MULT_MINUS_EXPR)
266 icode = find_widening_optab_handler (widen_pattern_optab,
267 TYPE_MODE (TREE_TYPE (ops->op2)),
268 tmode0);
269 else
270 icode = optab_handler (widen_pattern_optab, tmode0);
271 gcc_assert (icode != CODE_FOR_nothing);
273 if (nops >= 2)
275 oprnd1 = ops->op1;
276 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
279 /* The last operand is of a wider mode than the rest of the operands. */
280 if (nops == 2)
281 wmode = tmode1;
282 else if (nops == 3)
284 gcc_assert (tmode1 == tmode0);
285 gcc_assert (op1);
286 oprnd2 = ops->op2;
287 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
290 op = 0;
291 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
292 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
293 if (op1)
294 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
295 if (wide_op)
296 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
297 expand_insn (icode, op, eops);
298 return eops[0].value;
301 /* Generate code to perform an operation specified by TERNARY_OPTAB
302 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
304 UNSIGNEDP is for the case where we have to widen the operands
305 to perform the operation. It says to use zero-extension.
307 If TARGET is nonzero, the value
308 is generated there, if it is convenient to do so.
309 In all cases an rtx is returned for the locus of the value;
310 this may or may not be TARGET. */
313 expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
314 rtx op1, rtx op2, rtx target, int unsignedp)
316 struct expand_operand ops[4];
317 enum insn_code icode = optab_handler (ternary_optab, mode);
319 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
321 create_output_operand (&ops[0], target, mode);
322 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
323 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
324 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
325 expand_insn (icode, 4, ops);
326 return ops[0].value;
330 /* Like expand_binop, but return a constant rtx if the result can be
331 calculated at compile time. The arguments and return value are
332 otherwise the same as for expand_binop. */
335 simplify_expand_binop (machine_mode mode, optab binoptab,
336 rtx op0, rtx op1, rtx target, int unsignedp,
337 enum optab_methods methods)
339 if (CONSTANT_P (op0) && CONSTANT_P (op1))
341 rtx x = simplify_binary_operation (optab_to_code (binoptab),
342 mode, op0, op1);
343 if (x)
344 return x;
347 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
350 /* Like simplify_expand_binop, but always put the result in TARGET.
351 Return true if the expansion succeeded. */
353 bool
354 force_expand_binop (machine_mode mode, optab binoptab,
355 rtx op0, rtx op1, rtx target, int unsignedp,
356 enum optab_methods methods)
358 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
359 target, unsignedp, methods);
360 if (x == 0)
361 return false;
362 if (x != target)
363 emit_move_insn (target, x);
364 return true;
367 /* Create a new vector value in VMODE with all elements set to OP. The
368 mode of OP must be the element mode of VMODE. If OP is a constant,
369 then the return value will be a constant. */
372 expand_vector_broadcast (machine_mode vmode, rtx op)
374 int n;
375 rtvec vec;
377 gcc_checking_assert (VECTOR_MODE_P (vmode));
379 if (valid_for_const_vector_p (vmode, op))
380 return gen_const_vec_duplicate (vmode, op);
382 insn_code icode = optab_handler (vec_duplicate_optab, vmode);
383 if (icode != CODE_FOR_nothing)
385 struct expand_operand ops[2];
386 create_output_operand (&ops[0], NULL_RTX, vmode);
387 create_input_operand (&ops[1], op, GET_MODE (op));
388 expand_insn (icode, 2, ops);
389 return ops[0].value;
392 if (!GET_MODE_NUNITS (vmode).is_constant (&n))
393 return NULL;
395 /* ??? If the target doesn't have a vec_init, then we have no easy way
396 of performing this operation. Most of this sort of generic support
397 is hidden away in the vector lowering support in gimple. */
398 icode = convert_optab_handler (vec_init_optab, vmode,
399 GET_MODE_INNER (vmode));
400 if (icode == CODE_FOR_nothing)
401 return NULL;
403 vec = rtvec_alloc (n);
404 for (int i = 0; i < n; ++i)
405 RTVEC_ELT (vec, i) = op;
406 rtx ret = gen_reg_rtx (vmode);
407 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
409 return ret;
412 /* This subroutine of expand_doubleword_shift handles the cases in which
413 the effective shift value is >= BITS_PER_WORD. The arguments and return
414 value are the same as for the parent routine, except that SUPERWORD_OP1
415 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
416 INTO_TARGET may be null if the caller has decided to calculate it. */
418 static bool
419 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
420 rtx outof_target, rtx into_target,
421 int unsignedp, enum optab_methods methods)
423 if (into_target != 0)
424 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
425 into_target, unsignedp, methods))
426 return false;
428 if (outof_target != 0)
430 /* For a signed right shift, we must fill OUTOF_TARGET with copies
431 of the sign bit, otherwise we must fill it with zeros. */
432 if (binoptab != ashr_optab)
433 emit_move_insn (outof_target, CONST0_RTX (word_mode));
434 else
435 if (!force_expand_binop (word_mode, binoptab, outof_input,
436 gen_int_shift_amount (word_mode,
437 BITS_PER_WORD - 1),
438 outof_target, unsignedp, methods))
439 return false;
441 return true;
444 /* This subroutine of expand_doubleword_shift handles the cases in which
445 the effective shift value is < BITS_PER_WORD. The arguments and return
446 value are the same as for the parent routine. */
448 static bool
449 expand_subword_shift (scalar_int_mode op1_mode, optab binoptab,
450 rtx outof_input, rtx into_input, rtx op1,
451 rtx outof_target, rtx into_target,
452 int unsignedp, enum optab_methods methods,
453 unsigned HOST_WIDE_INT shift_mask)
455 optab reverse_unsigned_shift, unsigned_shift;
456 rtx tmp, carries;
458 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
459 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
461 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
462 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
463 the opposite direction to BINOPTAB. */
464 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
466 carries = outof_input;
467 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
468 op1_mode), op1_mode);
469 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
470 0, true, methods);
472 else
474 /* We must avoid shifting by BITS_PER_WORD bits since that is either
475 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
476 has unknown behavior. Do a single shift first, then shift by the
477 remainder. It's OK to use ~OP1 as the remainder if shift counts
478 are truncated to the mode size. */
479 carries = expand_binop (word_mode, reverse_unsigned_shift,
480 outof_input, const1_rtx, 0, unsignedp, methods);
481 if (shift_mask == BITS_PER_WORD - 1)
483 tmp = immed_wide_int_const
484 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
485 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
486 0, true, methods);
488 else
490 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
491 op1_mode), op1_mode);
492 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
493 0, true, methods);
496 if (tmp == 0 || carries == 0)
497 return false;
498 carries = expand_binop (word_mode, reverse_unsigned_shift,
499 carries, tmp, 0, unsignedp, methods);
500 if (carries == 0)
501 return false;
503 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
504 so the result can go directly into INTO_TARGET if convenient. */
505 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
506 into_target, unsignedp, methods);
507 if (tmp == 0)
508 return false;
510 /* Now OR in the bits carried over from OUTOF_INPUT. */
511 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
512 into_target, unsignedp, methods))
513 return false;
515 /* Use a standard word_mode shift for the out-of half. */
516 if (outof_target != 0)
517 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
518 outof_target, unsignedp, methods))
519 return false;
521 return true;
525 /* Try implementing expand_doubleword_shift using conditional moves.
526 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
527 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
528 are the shift counts to use in the former and latter case. All other
529 arguments are the same as the parent routine. */
531 static bool
532 expand_doubleword_shift_condmove (scalar_int_mode op1_mode, optab binoptab,
533 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
534 rtx outof_input, rtx into_input,
535 rtx subword_op1, rtx superword_op1,
536 rtx outof_target, rtx into_target,
537 int unsignedp, enum optab_methods methods,
538 unsigned HOST_WIDE_INT shift_mask)
540 rtx outof_superword, into_superword;
542 /* Put the superword version of the output into OUTOF_SUPERWORD and
543 INTO_SUPERWORD. */
544 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
545 if (outof_target != 0 && subword_op1 == superword_op1)
547 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
548 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
549 into_superword = outof_target;
550 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
551 outof_superword, 0, unsignedp, methods))
552 return false;
554 else
556 into_superword = gen_reg_rtx (word_mode);
557 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
558 outof_superword, into_superword,
559 unsignedp, methods))
560 return false;
563 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
564 if (!expand_subword_shift (op1_mode, binoptab,
565 outof_input, into_input, subword_op1,
566 outof_target, into_target,
567 unsignedp, methods, shift_mask))
568 return false;
570 /* Select between them. Do the INTO half first because INTO_SUPERWORD
571 might be the current value of OUTOF_TARGET. */
572 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
573 into_target, into_superword, word_mode, false))
574 return false;
576 if (outof_target != 0)
577 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
578 outof_target, outof_superword,
579 word_mode, false))
580 return false;
582 return true;
585 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
586 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
587 input operand; the shift moves bits in the direction OUTOF_INPUT->
588 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
589 of the target. OP1 is the shift count and OP1_MODE is its mode.
590 If OP1 is constant, it will have been truncated as appropriate
591 and is known to be nonzero.
593 If SHIFT_MASK is zero, the result of word shifts is undefined when the
594 shift count is outside the range [0, BITS_PER_WORD). This routine must
595 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
597 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
598 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
599 fill with zeros or sign bits as appropriate.
601 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
602 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
603 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
604 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
605 are undefined.
607 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
608 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
609 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
610 function wants to calculate it itself.
612 Return true if the shift could be successfully synthesized. */
614 static bool
615 expand_doubleword_shift (scalar_int_mode op1_mode, optab binoptab,
616 rtx outof_input, rtx into_input, rtx op1,
617 rtx outof_target, rtx into_target,
618 int unsignedp, enum optab_methods methods,
619 unsigned HOST_WIDE_INT shift_mask)
621 rtx superword_op1, tmp, cmp1, cmp2;
622 enum rtx_code cmp_code;
624 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
625 fill the result with sign or zero bits as appropriate. If so, the value
626 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
627 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
628 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
630 This isn't worthwhile for constant shifts since the optimizers will
631 cope better with in-range shift counts. */
632 if (shift_mask >= BITS_PER_WORD
633 && outof_target != 0
634 && !CONSTANT_P (op1))
636 if (!expand_doubleword_shift (op1_mode, binoptab,
637 outof_input, into_input, op1,
638 0, into_target,
639 unsignedp, methods, shift_mask))
640 return false;
641 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
642 outof_target, unsignedp, methods))
643 return false;
644 return true;
647 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
648 is true when the effective shift value is less than BITS_PER_WORD.
649 Set SUPERWORD_OP1 to the shift count that should be used to shift
650 OUTOF_INPUT into INTO_TARGET when the condition is false. */
651 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
652 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
654 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
655 is a subword shift count. */
656 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
657 0, true, methods);
658 cmp2 = CONST0_RTX (op1_mode);
659 cmp_code = EQ;
660 superword_op1 = op1;
662 else
664 /* Set CMP1 to OP1 - BITS_PER_WORD. */
665 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
666 0, true, methods);
667 cmp2 = CONST0_RTX (op1_mode);
668 cmp_code = LT;
669 superword_op1 = cmp1;
671 if (cmp1 == 0)
672 return false;
674 /* If we can compute the condition at compile time, pick the
675 appropriate subroutine. */
676 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
677 if (tmp != 0 && CONST_INT_P (tmp))
679 if (tmp == const0_rtx)
680 return expand_superword_shift (binoptab, outof_input, superword_op1,
681 outof_target, into_target,
682 unsignedp, methods);
683 else
684 return expand_subword_shift (op1_mode, binoptab,
685 outof_input, into_input, op1,
686 outof_target, into_target,
687 unsignedp, methods, shift_mask);
690 /* Try using conditional moves to generate straight-line code. */
691 if (HAVE_conditional_move)
693 rtx_insn *start = get_last_insn ();
694 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
695 cmp_code, cmp1, cmp2,
696 outof_input, into_input,
697 op1, superword_op1,
698 outof_target, into_target,
699 unsignedp, methods, shift_mask))
700 return true;
701 delete_insns_since (start);
704 /* As a last resort, use branches to select the correct alternative. */
705 rtx_code_label *subword_label = gen_label_rtx ();
706 rtx_code_label *done_label = gen_label_rtx ();
708 NO_DEFER_POP;
709 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
710 0, 0, subword_label,
711 profile_probability::uninitialized ());
712 OK_DEFER_POP;
714 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
715 outof_target, into_target,
716 unsignedp, methods))
717 return false;
719 emit_jump_insn (targetm.gen_jump (done_label));
720 emit_barrier ();
721 emit_label (subword_label);
723 if (!expand_subword_shift (op1_mode, binoptab,
724 outof_input, into_input, op1,
725 outof_target, into_target,
726 unsignedp, methods, shift_mask))
727 return false;
729 emit_label (done_label);
730 return true;
733 /* Subroutine of expand_binop. Perform a double word multiplication of
734 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
735 as the target's word_mode. This function return NULL_RTX if anything
736 goes wrong, in which case it may have already emitted instructions
737 which need to be deleted.
739 If we want to multiply two two-word values and have normal and widening
740 multiplies of single-word values, we can do this with three smaller
741 multiplications.
743 The multiplication proceeds as follows:
744 _______________________
745 [__op0_high_|__op0_low__]
746 _______________________
747 * [__op1_high_|__op1_low__]
748 _______________________________________________
749 _______________________
750 (1) [__op0_low__*__op1_low__]
751 _______________________
752 (2a) [__op0_low__*__op1_high_]
753 _______________________
754 (2b) [__op0_high_*__op1_low__]
755 _______________________
756 (3) [__op0_high_*__op1_high_]
759 This gives a 4-word result. Since we are only interested in the
760 lower 2 words, partial result (3) and the upper words of (2a) and
761 (2b) don't need to be calculated. Hence (2a) and (2b) can be
762 calculated using non-widening multiplication.
764 (1), however, needs to be calculated with an unsigned widening
765 multiplication. If this operation is not directly supported we
766 try using a signed widening multiplication and adjust the result.
767 This adjustment works as follows:
769 If both operands are positive then no adjustment is needed.
771 If the operands have different signs, for example op0_low < 0 and
772 op1_low >= 0, the instruction treats the most significant bit of
773 op0_low as a sign bit instead of a bit with significance
774 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
775 with 2**BITS_PER_WORD - op0_low, and two's complements the
776 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
777 the result.
779 Similarly, if both operands are negative, we need to add
780 (op0_low + op1_low) * 2**BITS_PER_WORD.
782 We use a trick to adjust quickly. We logically shift op0_low right
783 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
784 op0_high (op1_high) before it is used to calculate 2b (2a). If no
785 logical shift exists, we do an arithmetic right shift and subtract
786 the 0 or -1. */
788 static rtx
789 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
790 bool umulp, enum optab_methods methods)
792 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
793 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
794 rtx wordm1 = (umulp ? NULL_RTX
795 : gen_int_shift_amount (word_mode, BITS_PER_WORD - 1));
796 rtx product, adjust, product_high, temp;
798 rtx op0_high = operand_subword_force (op0, high, mode);
799 rtx op0_low = operand_subword_force (op0, low, mode);
800 rtx op1_high = operand_subword_force (op1, high, mode);
801 rtx op1_low = operand_subword_force (op1, low, mode);
803 /* If we're using an unsigned multiply to directly compute the product
804 of the low-order words of the operands and perform any required
805 adjustments of the operands, we begin by trying two more multiplications
806 and then computing the appropriate sum.
808 We have checked above that the required addition is provided.
809 Full-word addition will normally always succeed, especially if
810 it is provided at all, so we don't worry about its failure. The
811 multiplication may well fail, however, so we do handle that. */
813 if (!umulp)
815 /* ??? This could be done with emit_store_flag where available. */
816 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
817 NULL_RTX, 1, methods);
818 if (temp)
819 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
820 NULL_RTX, 0, OPTAB_DIRECT);
821 else
823 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
824 NULL_RTX, 0, methods);
825 if (!temp)
826 return NULL_RTX;
827 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
828 NULL_RTX, 0, OPTAB_DIRECT);
831 if (!op0_high)
832 return NULL_RTX;
835 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
836 NULL_RTX, 0, OPTAB_DIRECT);
837 if (!adjust)
838 return NULL_RTX;
840 /* OP0_HIGH should now be dead. */
842 if (!umulp)
844 /* ??? This could be done with emit_store_flag where available. */
845 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
846 NULL_RTX, 1, methods);
847 if (temp)
848 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
849 NULL_RTX, 0, OPTAB_DIRECT);
850 else
852 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
853 NULL_RTX, 0, methods);
854 if (!temp)
855 return NULL_RTX;
856 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
857 NULL_RTX, 0, OPTAB_DIRECT);
860 if (!op1_high)
861 return NULL_RTX;
864 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
865 NULL_RTX, 0, OPTAB_DIRECT);
866 if (!temp)
867 return NULL_RTX;
869 /* OP1_HIGH should now be dead. */
871 adjust = expand_binop (word_mode, add_optab, adjust, temp,
872 NULL_RTX, 0, OPTAB_DIRECT);
874 if (target && !REG_P (target))
875 target = NULL_RTX;
877 /* *_widen_optab needs to determine operand mode, make sure at least
878 one operand has non-VOID mode. */
879 if (GET_MODE (op0_low) == VOIDmode && GET_MODE (op1_low) == VOIDmode)
880 op0_low = force_reg (word_mode, op0_low);
882 if (umulp)
883 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
884 target, 1, OPTAB_DIRECT);
885 else
886 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
887 target, 1, OPTAB_DIRECT);
889 if (!product)
890 return NULL_RTX;
892 product_high = operand_subword (product, high, 1, mode);
893 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
894 NULL_RTX, 0, OPTAB_DIRECT);
895 emit_move_insn (product_high, adjust);
896 return product;
899 /* Wrapper around expand_binop which takes an rtx code to specify
900 the operation to perform, not an optab pointer. All other
901 arguments are the same. */
903 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
904 rtx op1, rtx target, int unsignedp,
905 enum optab_methods methods)
907 optab binop = code_to_optab (code);
908 gcc_assert (binop);
910 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
913 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
914 binop. Order them according to commutative_operand_precedence and, if
915 possible, try to put TARGET or a pseudo first. */
916 static bool
917 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
919 int op0_prec = commutative_operand_precedence (op0);
920 int op1_prec = commutative_operand_precedence (op1);
922 if (op0_prec < op1_prec)
923 return true;
925 if (op0_prec > op1_prec)
926 return false;
928 /* With equal precedence, both orders are ok, but it is better if the
929 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
930 if (target == 0 || REG_P (target))
931 return (REG_P (op1) && !REG_P (op0)) || target == op1;
932 else
933 return rtx_equal_p (op1, target);
936 /* Return true if BINOPTAB implements a shift operation. */
938 static bool
939 shift_optab_p (optab binoptab)
941 switch (optab_to_code (binoptab))
943 case ASHIFT:
944 case SS_ASHIFT:
945 case US_ASHIFT:
946 case ASHIFTRT:
947 case LSHIFTRT:
948 case ROTATE:
949 case ROTATERT:
950 return true;
952 default:
953 return false;
957 /* Return true if BINOPTAB implements a commutative binary operation. */
959 static bool
960 commutative_optab_p (optab binoptab)
962 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
963 || binoptab == smul_widen_optab
964 || binoptab == umul_widen_optab
965 || binoptab == smul_highpart_optab
966 || binoptab == umul_highpart_optab);
969 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
970 optimizing, and if the operand is a constant that costs more than
971 1 instruction, force the constant into a register and return that
972 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
974 static rtx
975 avoid_expensive_constant (machine_mode mode, optab binoptab,
976 int opn, rtx x, bool unsignedp)
978 bool speed = optimize_insn_for_speed_p ();
980 if (mode != VOIDmode
981 && optimize
982 && CONSTANT_P (x)
983 && (rtx_cost (x, mode, optab_to_code (binoptab), opn, speed)
984 > set_src_cost (x, mode, speed)))
986 if (CONST_INT_P (x))
988 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
989 if (intval != INTVAL (x))
990 x = GEN_INT (intval);
992 else
993 x = convert_modes (mode, VOIDmode, x, unsignedp);
994 x = force_reg (mode, x);
996 return x;
999 /* Helper function for expand_binop: handle the case where there
1000 is an insn ICODE that directly implements the indicated operation.
1001 Returns null if this is not possible. */
1002 static rtx
1003 expand_binop_directly (enum insn_code icode, machine_mode mode, optab binoptab,
1004 rtx op0, rtx op1,
1005 rtx target, int unsignedp, enum optab_methods methods,
1006 rtx_insn *last)
1008 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1009 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1010 machine_mode mode0, mode1, tmp_mode;
1011 struct expand_operand ops[3];
1012 bool commutative_p;
1013 rtx_insn *pat;
1014 rtx xop0 = op0, xop1 = op1;
1015 bool canonicalize_op1 = false;
1017 /* If it is a commutative operator and the modes would match
1018 if we would swap the operands, we can save the conversions. */
1019 commutative_p = commutative_optab_p (binoptab);
1020 if (commutative_p
1021 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1022 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1023 std::swap (xop0, xop1);
1025 /* If we are optimizing, force expensive constants into a register. */
1026 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1027 if (!shift_optab_p (binoptab))
1028 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1029 else
1030 /* Shifts and rotates often use a different mode for op1 from op0;
1031 for VOIDmode constants we don't know the mode, so force it
1032 to be canonicalized using convert_modes. */
1033 canonicalize_op1 = true;
1035 /* In case the insn wants input operands in modes different from
1036 those of the actual operands, convert the operands. It would
1037 seem that we don't need to convert CONST_INTs, but we do, so
1038 that they're properly zero-extended, sign-extended or truncated
1039 for their mode. */
1041 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1042 if (xmode0 != VOIDmode && xmode0 != mode0)
1044 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1045 mode0 = xmode0;
1048 mode1 = ((GET_MODE (xop1) != VOIDmode || canonicalize_op1)
1049 ? GET_MODE (xop1) : mode);
1050 if (xmode1 != VOIDmode && xmode1 != mode1)
1052 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1053 mode1 = xmode1;
1056 /* If operation is commutative,
1057 try to make the first operand a register.
1058 Even better, try to make it the same as the target.
1059 Also try to make the last operand a constant. */
1060 if (commutative_p
1061 && swap_commutative_operands_with_target (target, xop0, xop1))
1062 std::swap (xop0, xop1);
1064 /* Now, if insn's predicates don't allow our operands, put them into
1065 pseudo regs. */
1067 if (binoptab == vec_pack_trunc_optab
1068 || binoptab == vec_pack_usat_optab
1069 || binoptab == vec_pack_ssat_optab
1070 || binoptab == vec_pack_ufix_trunc_optab
1071 || binoptab == vec_pack_sfix_trunc_optab)
1073 /* The mode of the result is different then the mode of the
1074 arguments. */
1075 tmp_mode = insn_data[(int) icode].operand[0].mode;
1076 if (VECTOR_MODE_P (mode)
1077 && maybe_ne (GET_MODE_NUNITS (tmp_mode), 2 * GET_MODE_NUNITS (mode)))
1079 delete_insns_since (last);
1080 return NULL_RTX;
1083 else
1084 tmp_mode = mode;
1086 create_output_operand (&ops[0], target, tmp_mode);
1087 create_input_operand (&ops[1], xop0, mode0);
1088 create_input_operand (&ops[2], xop1, mode1);
1089 pat = maybe_gen_insn (icode, 3, ops);
1090 if (pat)
1092 /* If PAT is composed of more than one insn, try to add an appropriate
1093 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1094 operand, call expand_binop again, this time without a target. */
1095 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1096 && ! add_equal_note (pat, ops[0].value,
1097 optab_to_code (binoptab),
1098 ops[1].value, ops[2].value))
1100 delete_insns_since (last);
1101 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1102 unsignedp, methods);
1105 emit_insn (pat);
1106 return ops[0].value;
1108 delete_insns_since (last);
1109 return NULL_RTX;
1112 /* Generate code to perform an operation specified by BINOPTAB
1113 on operands OP0 and OP1, with result having machine-mode MODE.
1115 UNSIGNEDP is for the case where we have to widen the operands
1116 to perform the operation. It says to use zero-extension.
1118 If TARGET is nonzero, the value
1119 is generated there, if it is convenient to do so.
1120 In all cases an rtx is returned for the locus of the value;
1121 this may or may not be TARGET. */
1124 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1125 rtx target, int unsignedp, enum optab_methods methods)
1127 enum optab_methods next_methods
1128 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1129 ? OPTAB_WIDEN : methods);
1130 enum mode_class mclass;
1131 enum insn_code icode;
1132 machine_mode wider_mode;
1133 scalar_int_mode int_mode;
1134 rtx libfunc;
1135 rtx temp;
1136 rtx_insn *entry_last = get_last_insn ();
1137 rtx_insn *last;
1139 mclass = GET_MODE_CLASS (mode);
1141 /* If subtracting an integer constant, convert this into an addition of
1142 the negated constant. */
1144 if (binoptab == sub_optab && CONST_INT_P (op1))
1146 op1 = negate_rtx (mode, op1);
1147 binoptab = add_optab;
1149 /* For shifts, constant invalid op1 might be expanded from different
1150 mode than MODE. As those are invalid, force them to a register
1151 to avoid further problems during expansion. */
1152 else if (CONST_INT_P (op1)
1153 && shift_optab_p (binoptab)
1154 && UINTVAL (op1) >= GET_MODE_BITSIZE (GET_MODE_INNER (mode)))
1156 op1 = gen_int_mode (INTVAL (op1), GET_MODE_INNER (mode));
1157 op1 = force_reg (GET_MODE_INNER (mode), op1);
1160 /* Record where to delete back to if we backtrack. */
1161 last = get_last_insn ();
1163 /* If we can do it with a three-operand insn, do so. */
1165 if (methods != OPTAB_MUST_WIDEN)
1167 if (convert_optab_p (binoptab))
1169 machine_mode from_mode = widened_mode (mode, op0, op1);
1170 icode = find_widening_optab_handler (binoptab, mode, from_mode);
1172 else
1173 icode = optab_handler (binoptab, mode);
1174 if (icode != CODE_FOR_nothing)
1176 temp = expand_binop_directly (icode, mode, binoptab, op0, op1,
1177 target, unsignedp, methods, last);
1178 if (temp)
1179 return temp;
1183 /* If we were trying to rotate, and that didn't work, try rotating
1184 the other direction before falling back to shifts and bitwise-or. */
1185 if (((binoptab == rotl_optab
1186 && (icode = optab_handler (rotr_optab, mode)) != CODE_FOR_nothing)
1187 || (binoptab == rotr_optab
1188 && (icode = optab_handler (rotl_optab, mode)) != CODE_FOR_nothing))
1189 && is_int_mode (mode, &int_mode))
1191 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1192 rtx newop1;
1193 unsigned int bits = GET_MODE_PRECISION (int_mode);
1195 if (CONST_INT_P (op1))
1196 newop1 = gen_int_shift_amount (int_mode, bits - INTVAL (op1));
1197 else if (targetm.shift_truncation_mask (int_mode) == bits - 1)
1198 newop1 = negate_rtx (GET_MODE (op1), op1);
1199 else
1200 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1201 gen_int_mode (bits, GET_MODE (op1)), op1,
1202 NULL_RTX, unsignedp, OPTAB_DIRECT);
1204 temp = expand_binop_directly (icode, int_mode, otheroptab, op0, newop1,
1205 target, unsignedp, methods, last);
1206 if (temp)
1207 return temp;
1210 /* If this is a multiply, see if we can do a widening operation that
1211 takes operands of this mode and makes a wider mode. */
1213 if (binoptab == smul_optab
1214 && GET_MODE_2XWIDER_MODE (mode).exists (&wider_mode)
1215 && (convert_optab_handler ((unsignedp
1216 ? umul_widen_optab
1217 : smul_widen_optab),
1218 wider_mode, mode) != CODE_FOR_nothing))
1220 /* *_widen_optab needs to determine operand mode, make sure at least
1221 one operand has non-VOID mode. */
1222 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
1223 op0 = force_reg (mode, op0);
1224 temp = expand_binop (wider_mode,
1225 unsignedp ? umul_widen_optab : smul_widen_optab,
1226 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1228 if (temp != 0)
1230 if (GET_MODE_CLASS (mode) == MODE_INT
1231 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1232 return gen_lowpart (mode, temp);
1233 else
1234 return convert_to_mode (mode, temp, unsignedp);
1238 /* If this is a vector shift by a scalar, see if we can do a vector
1239 shift by a vector. If so, broadcast the scalar into a vector. */
1240 if (mclass == MODE_VECTOR_INT)
1242 optab otheroptab = unknown_optab;
1244 if (binoptab == ashl_optab)
1245 otheroptab = vashl_optab;
1246 else if (binoptab == ashr_optab)
1247 otheroptab = vashr_optab;
1248 else if (binoptab == lshr_optab)
1249 otheroptab = vlshr_optab;
1250 else if (binoptab == rotl_optab)
1251 otheroptab = vrotl_optab;
1252 else if (binoptab == rotr_optab)
1253 otheroptab = vrotr_optab;
1255 if (otheroptab
1256 && (icode = optab_handler (otheroptab, mode)) != CODE_FOR_nothing)
1258 /* The scalar may have been extended to be too wide. Truncate
1259 it back to the proper size to fit in the broadcast vector. */
1260 scalar_mode inner_mode = GET_MODE_INNER (mode);
1261 if (!CONST_INT_P (op1)
1262 && (GET_MODE_BITSIZE (as_a <scalar_int_mode> (GET_MODE (op1)))
1263 > GET_MODE_BITSIZE (inner_mode)))
1264 op1 = force_reg (inner_mode,
1265 simplify_gen_unary (TRUNCATE, inner_mode, op1,
1266 GET_MODE (op1)));
1267 rtx vop1 = expand_vector_broadcast (mode, op1);
1268 if (vop1)
1270 temp = expand_binop_directly (icode, mode, otheroptab, op0, vop1,
1271 target, unsignedp, methods, last);
1272 if (temp)
1273 return temp;
1278 /* Look for a wider mode of the same class for which we think we
1279 can open-code the operation. Check for a widening multiply at the
1280 wider mode as well. */
1282 if (CLASS_HAS_WIDER_MODES_P (mclass)
1283 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1284 FOR_EACH_WIDER_MODE (wider_mode, mode)
1286 machine_mode next_mode;
1287 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1288 || (binoptab == smul_optab
1289 && GET_MODE_WIDER_MODE (wider_mode).exists (&next_mode)
1290 && (find_widening_optab_handler ((unsignedp
1291 ? umul_widen_optab
1292 : smul_widen_optab),
1293 next_mode, mode)
1294 != CODE_FOR_nothing)))
1296 rtx xop0 = op0, xop1 = op1;
1297 int no_extend = 0;
1299 /* For certain integer operations, we need not actually extend
1300 the narrow operands, as long as we will truncate
1301 the results to the same narrowness. */
1303 if ((binoptab == ior_optab || binoptab == and_optab
1304 || binoptab == xor_optab
1305 || binoptab == add_optab || binoptab == sub_optab
1306 || binoptab == smul_optab || binoptab == ashl_optab)
1307 && mclass == MODE_INT)
1309 no_extend = 1;
1310 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1311 xop0, unsignedp);
1312 if (binoptab != ashl_optab)
1313 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1314 xop1, unsignedp);
1317 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1319 /* The second operand of a shift must always be extended. */
1320 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1321 no_extend && binoptab != ashl_optab);
1323 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1324 unsignedp, OPTAB_DIRECT);
1325 if (temp)
1327 if (mclass != MODE_INT
1328 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1330 if (target == 0)
1331 target = gen_reg_rtx (mode);
1332 convert_move (target, temp, 0);
1333 return target;
1335 else
1336 return gen_lowpart (mode, temp);
1338 else
1339 delete_insns_since (last);
1343 /* If operation is commutative,
1344 try to make the first operand a register.
1345 Even better, try to make it the same as the target.
1346 Also try to make the last operand a constant. */
1347 if (commutative_optab_p (binoptab)
1348 && swap_commutative_operands_with_target (target, op0, op1))
1349 std::swap (op0, op1);
1351 /* These can be done a word at a time. */
1352 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1353 && is_int_mode (mode, &int_mode)
1354 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
1355 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1357 int i;
1358 rtx_insn *insns;
1360 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1361 won't be accurate, so use a new target. */
1362 if (target == 0
1363 || target == op0
1364 || target == op1
1365 || !valid_multiword_target_p (target))
1366 target = gen_reg_rtx (int_mode);
1368 start_sequence ();
1370 /* Do the actual arithmetic. */
1371 for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
1373 rtx target_piece = operand_subword (target, i, 1, int_mode);
1374 rtx x = expand_binop (word_mode, binoptab,
1375 operand_subword_force (op0, i, int_mode),
1376 operand_subword_force (op1, i, int_mode),
1377 target_piece, unsignedp, next_methods);
1379 if (x == 0)
1380 break;
1382 if (target_piece != x)
1383 emit_move_insn (target_piece, x);
1386 insns = get_insns ();
1387 end_sequence ();
1389 if (i == GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD)
1391 emit_insn (insns);
1392 return target;
1396 /* Synthesize double word shifts from single word shifts. */
1397 if ((binoptab == lshr_optab || binoptab == ashl_optab
1398 || binoptab == ashr_optab)
1399 && is_int_mode (mode, &int_mode)
1400 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1401 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
1402 && GET_MODE_PRECISION (int_mode) == GET_MODE_BITSIZE (int_mode)
1403 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1404 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1405 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1407 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1408 scalar_int_mode op1_mode;
1410 double_shift_mask = targetm.shift_truncation_mask (int_mode);
1411 shift_mask = targetm.shift_truncation_mask (word_mode);
1412 op1_mode = (GET_MODE (op1) != VOIDmode
1413 ? as_a <scalar_int_mode> (GET_MODE (op1))
1414 : word_mode);
1416 /* Apply the truncation to constant shifts. */
1417 if (double_shift_mask > 0 && CONST_INT_P (op1))
1418 op1 = gen_int_mode (INTVAL (op1) & double_shift_mask, op1_mode);
1420 if (op1 == CONST0_RTX (op1_mode))
1421 return op0;
1423 /* Make sure that this is a combination that expand_doubleword_shift
1424 can handle. See the comments there for details. */
1425 if (double_shift_mask == 0
1426 || (shift_mask == BITS_PER_WORD - 1
1427 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1429 rtx_insn *insns;
1430 rtx into_target, outof_target;
1431 rtx into_input, outof_input;
1432 int left_shift, outof_word;
1434 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1435 won't be accurate, so use a new target. */
1436 if (target == 0
1437 || target == op0
1438 || target == op1
1439 || !valid_multiword_target_p (target))
1440 target = gen_reg_rtx (int_mode);
1442 start_sequence ();
1444 /* OUTOF_* is the word we are shifting bits away from, and
1445 INTO_* is the word that we are shifting bits towards, thus
1446 they differ depending on the direction of the shift and
1447 WORDS_BIG_ENDIAN. */
1449 left_shift = binoptab == ashl_optab;
1450 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1452 outof_target = operand_subword (target, outof_word, 1, int_mode);
1453 into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
1455 outof_input = operand_subword_force (op0, outof_word, int_mode);
1456 into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
1458 if (expand_doubleword_shift (op1_mode, binoptab,
1459 outof_input, into_input, op1,
1460 outof_target, into_target,
1461 unsignedp, next_methods, shift_mask))
1463 insns = get_insns ();
1464 end_sequence ();
1466 emit_insn (insns);
1467 return target;
1469 end_sequence ();
1473 /* Synthesize double word rotates from single word shifts. */
1474 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1475 && is_int_mode (mode, &int_mode)
1476 && CONST_INT_P (op1)
1477 && GET_MODE_PRECISION (int_mode) == 2 * BITS_PER_WORD
1478 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1479 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1481 rtx_insn *insns;
1482 rtx into_target, outof_target;
1483 rtx into_input, outof_input;
1484 rtx inter;
1485 int shift_count, left_shift, outof_word;
1487 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1488 won't be accurate, so use a new target. Do this also if target is not
1489 a REG, first because having a register instead may open optimization
1490 opportunities, and second because if target and op0 happen to be MEMs
1491 designating the same location, we would risk clobbering it too early
1492 in the code sequence we generate below. */
1493 if (target == 0
1494 || target == op0
1495 || target == op1
1496 || !REG_P (target)
1497 || !valid_multiword_target_p (target))
1498 target = gen_reg_rtx (int_mode);
1500 start_sequence ();
1502 shift_count = INTVAL (op1);
1504 /* OUTOF_* is the word we are shifting bits away from, and
1505 INTO_* is the word that we are shifting bits towards, thus
1506 they differ depending on the direction of the shift and
1507 WORDS_BIG_ENDIAN. */
1509 left_shift = (binoptab == rotl_optab);
1510 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1512 outof_target = operand_subword (target, outof_word, 1, int_mode);
1513 into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
1515 outof_input = operand_subword_force (op0, outof_word, int_mode);
1516 into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
1518 if (shift_count == BITS_PER_WORD)
1520 /* This is just a word swap. */
1521 emit_move_insn (outof_target, into_input);
1522 emit_move_insn (into_target, outof_input);
1523 inter = const0_rtx;
1525 else
1527 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1528 HOST_WIDE_INT first_shift_count, second_shift_count;
1529 optab reverse_unsigned_shift, unsigned_shift;
1531 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1532 ? lshr_optab : ashl_optab);
1534 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1535 ? ashl_optab : lshr_optab);
1537 if (shift_count > BITS_PER_WORD)
1539 first_shift_count = shift_count - BITS_PER_WORD;
1540 second_shift_count = 2 * BITS_PER_WORD - shift_count;
1542 else
1544 first_shift_count = BITS_PER_WORD - shift_count;
1545 second_shift_count = shift_count;
1547 rtx first_shift_count_rtx
1548 = gen_int_shift_amount (word_mode, first_shift_count);
1549 rtx second_shift_count_rtx
1550 = gen_int_shift_amount (word_mode, second_shift_count);
1552 into_temp1 = expand_binop (word_mode, unsigned_shift,
1553 outof_input, first_shift_count_rtx,
1554 NULL_RTX, unsignedp, next_methods);
1555 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1556 into_input, second_shift_count_rtx,
1557 NULL_RTX, unsignedp, next_methods);
1559 if (into_temp1 != 0 && into_temp2 != 0)
1560 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1561 into_target, unsignedp, next_methods);
1562 else
1563 inter = 0;
1565 if (inter != 0 && inter != into_target)
1566 emit_move_insn (into_target, inter);
1568 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1569 into_input, first_shift_count_rtx,
1570 NULL_RTX, unsignedp, next_methods);
1571 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1572 outof_input, second_shift_count_rtx,
1573 NULL_RTX, unsignedp, next_methods);
1575 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1576 inter = expand_binop (word_mode, ior_optab,
1577 outof_temp1, outof_temp2,
1578 outof_target, unsignedp, next_methods);
1580 if (inter != 0 && inter != outof_target)
1581 emit_move_insn (outof_target, inter);
1584 insns = get_insns ();
1585 end_sequence ();
1587 if (inter != 0)
1589 emit_insn (insns);
1590 return target;
1594 /* These can be done a word at a time by propagating carries. */
1595 if ((binoptab == add_optab || binoptab == sub_optab)
1596 && is_int_mode (mode, &int_mode)
1597 && GET_MODE_SIZE (int_mode) >= 2 * UNITS_PER_WORD
1598 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1600 unsigned int i;
1601 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1602 const unsigned int nwords = GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD;
1603 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1604 rtx xop0, xop1, xtarget;
1606 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1607 value is one of those, use it. Otherwise, use 1 since it is the
1608 one easiest to get. */
1609 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1610 int normalizep = STORE_FLAG_VALUE;
1611 #else
1612 int normalizep = 1;
1613 #endif
1615 /* Prepare the operands. */
1616 xop0 = force_reg (int_mode, op0);
1617 xop1 = force_reg (int_mode, op1);
1619 xtarget = gen_reg_rtx (int_mode);
1621 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1622 target = xtarget;
1624 /* Indicate for flow that the entire target reg is being set. */
1625 if (REG_P (target))
1626 emit_clobber (xtarget);
1628 /* Do the actual arithmetic. */
1629 for (i = 0; i < nwords; i++)
1631 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1632 rtx target_piece = operand_subword (xtarget, index, 1, int_mode);
1633 rtx op0_piece = operand_subword_force (xop0, index, int_mode);
1634 rtx op1_piece = operand_subword_force (xop1, index, int_mode);
1635 rtx x;
1637 /* Main add/subtract of the input operands. */
1638 x = expand_binop (word_mode, binoptab,
1639 op0_piece, op1_piece,
1640 target_piece, unsignedp, next_methods);
1641 if (x == 0)
1642 break;
1644 if (i + 1 < nwords)
1646 /* Store carry from main add/subtract. */
1647 carry_out = gen_reg_rtx (word_mode);
1648 carry_out = emit_store_flag_force (carry_out,
1649 (binoptab == add_optab
1650 ? LT : GT),
1651 x, op0_piece,
1652 word_mode, 1, normalizep);
1655 if (i > 0)
1657 rtx newx;
1659 /* Add/subtract previous carry to main result. */
1660 newx = expand_binop (word_mode,
1661 normalizep == 1 ? binoptab : otheroptab,
1662 x, carry_in,
1663 NULL_RTX, 1, next_methods);
1665 if (i + 1 < nwords)
1667 /* Get out carry from adding/subtracting carry in. */
1668 rtx carry_tmp = gen_reg_rtx (word_mode);
1669 carry_tmp = emit_store_flag_force (carry_tmp,
1670 (binoptab == add_optab
1671 ? LT : GT),
1672 newx, x,
1673 word_mode, 1, normalizep);
1675 /* Logical-ior the two poss. carry together. */
1676 carry_out = expand_binop (word_mode, ior_optab,
1677 carry_out, carry_tmp,
1678 carry_out, 0, next_methods);
1679 if (carry_out == 0)
1680 break;
1682 emit_move_insn (target_piece, newx);
1684 else
1686 if (x != target_piece)
1687 emit_move_insn (target_piece, x);
1690 carry_in = carry_out;
1693 if (i == GET_MODE_BITSIZE (int_mode) / (unsigned) BITS_PER_WORD)
1695 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing
1696 || ! rtx_equal_p (target, xtarget))
1698 rtx_insn *temp = emit_move_insn (target, xtarget);
1700 set_dst_reg_note (temp, REG_EQUAL,
1701 gen_rtx_fmt_ee (optab_to_code (binoptab),
1702 int_mode, copy_rtx (xop0),
1703 copy_rtx (xop1)),
1704 target);
1706 else
1707 target = xtarget;
1709 return target;
1712 else
1713 delete_insns_since (last);
1716 /* Attempt to synthesize double word multiplies using a sequence of word
1717 mode multiplications. We first attempt to generate a sequence using a
1718 more efficient unsigned widening multiply, and if that fails we then
1719 try using a signed widening multiply. */
1721 if (binoptab == smul_optab
1722 && is_int_mode (mode, &int_mode)
1723 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
1724 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
1725 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
1727 rtx product = NULL_RTX;
1728 if (convert_optab_handler (umul_widen_optab, int_mode, word_mode)
1729 != CODE_FOR_nothing)
1731 product = expand_doubleword_mult (int_mode, op0, op1, target,
1732 true, methods);
1733 if (!product)
1734 delete_insns_since (last);
1737 if (product == NULL_RTX
1738 && (convert_optab_handler (smul_widen_optab, int_mode, word_mode)
1739 != CODE_FOR_nothing))
1741 product = expand_doubleword_mult (int_mode, op0, op1, target,
1742 false, methods);
1743 if (!product)
1744 delete_insns_since (last);
1747 if (product != NULL_RTX)
1749 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
1751 rtx_insn *move = emit_move_insn (target ? target : product,
1752 product);
1753 set_dst_reg_note (move,
1754 REG_EQUAL,
1755 gen_rtx_fmt_ee (MULT, int_mode,
1756 copy_rtx (op0),
1757 copy_rtx (op1)),
1758 target ? target : product);
1760 return product;
1764 /* It can't be open-coded in this mode.
1765 Use a library call if one is available and caller says that's ok. */
1767 libfunc = optab_libfunc (binoptab, mode);
1768 if (libfunc
1769 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
1771 rtx_insn *insns;
1772 rtx op1x = op1;
1773 machine_mode op1_mode = mode;
1774 rtx value;
1776 start_sequence ();
1778 if (shift_optab_p (binoptab))
1780 op1_mode = targetm.libgcc_shift_count_mode ();
1781 /* Specify unsigned here,
1782 since negative shift counts are meaningless. */
1783 op1x = convert_to_mode (op1_mode, op1, 1);
1786 if (GET_MODE (op0) != VOIDmode
1787 && GET_MODE (op0) != mode)
1788 op0 = convert_to_mode (mode, op0, unsignedp);
1790 /* Pass 1 for NO_QUEUE so we don't lose any increments
1791 if the libcall is cse'd or moved. */
1792 value = emit_library_call_value (libfunc,
1793 NULL_RTX, LCT_CONST, mode,
1794 op0, mode, op1x, op1_mode);
1796 insns = get_insns ();
1797 end_sequence ();
1799 bool trapv = trapv_binoptab_p (binoptab);
1800 target = gen_reg_rtx (mode);
1801 emit_libcall_block_1 (insns, target, value,
1802 trapv ? NULL_RTX
1803 : gen_rtx_fmt_ee (optab_to_code (binoptab),
1804 mode, op0, op1), trapv);
1806 return target;
1809 delete_insns_since (last);
1811 /* It can't be done in this mode. Can we do it in a wider mode? */
1813 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
1814 || methods == OPTAB_MUST_WIDEN))
1816 /* Caller says, don't even try. */
1817 delete_insns_since (entry_last);
1818 return 0;
1821 /* Compute the value of METHODS to pass to recursive calls.
1822 Don't allow widening to be tried recursively. */
1824 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
1826 /* Look for a wider mode of the same class for which it appears we can do
1827 the operation. */
1829 if (CLASS_HAS_WIDER_MODES_P (mclass))
1831 /* This code doesn't make sense for conversion optabs, since we
1832 wouldn't then want to extend the operands to be the same size
1833 as the result. */
1834 gcc_assert (!convert_optab_p (binoptab));
1835 FOR_EACH_WIDER_MODE (wider_mode, mode)
1837 if (optab_handler (binoptab, wider_mode)
1838 || (methods == OPTAB_LIB
1839 && optab_libfunc (binoptab, wider_mode)))
1841 rtx xop0 = op0, xop1 = op1;
1842 int no_extend = 0;
1844 /* For certain integer operations, we need not actually extend
1845 the narrow operands, as long as we will truncate
1846 the results to the same narrowness. */
1848 if ((binoptab == ior_optab || binoptab == and_optab
1849 || binoptab == xor_optab
1850 || binoptab == add_optab || binoptab == sub_optab
1851 || binoptab == smul_optab || binoptab == ashl_optab)
1852 && mclass == MODE_INT)
1853 no_extend = 1;
1855 xop0 = widen_operand (xop0, wider_mode, mode,
1856 unsignedp, no_extend);
1858 /* The second operand of a shift must always be extended. */
1859 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1860 no_extend && binoptab != ashl_optab);
1862 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1863 unsignedp, methods);
1864 if (temp)
1866 if (mclass != MODE_INT
1867 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1869 if (target == 0)
1870 target = gen_reg_rtx (mode);
1871 convert_move (target, temp, 0);
1872 return target;
1874 else
1875 return gen_lowpart (mode, temp);
1877 else
1878 delete_insns_since (last);
1883 delete_insns_since (entry_last);
1884 return 0;
1887 /* Expand a binary operator which has both signed and unsigned forms.
1888 UOPTAB is the optab for unsigned operations, and SOPTAB is for
1889 signed operations.
1891 If we widen unsigned operands, we may use a signed wider operation instead
1892 of an unsigned wider operation, since the result would be the same. */
1895 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
1896 rtx op0, rtx op1, rtx target, int unsignedp,
1897 enum optab_methods methods)
1899 rtx temp;
1900 optab direct_optab = unsignedp ? uoptab : soptab;
1901 bool save_enable;
1903 /* Do it without widening, if possible. */
1904 temp = expand_binop (mode, direct_optab, op0, op1, target,
1905 unsignedp, OPTAB_DIRECT);
1906 if (temp || methods == OPTAB_DIRECT)
1907 return temp;
1909 /* Try widening to a signed int. Disable any direct use of any
1910 signed insn in the current mode. */
1911 save_enable = swap_optab_enable (soptab, mode, false);
1913 temp = expand_binop (mode, soptab, op0, op1, target,
1914 unsignedp, OPTAB_WIDEN);
1916 /* For unsigned operands, try widening to an unsigned int. */
1917 if (!temp && unsignedp)
1918 temp = expand_binop (mode, uoptab, op0, op1, target,
1919 unsignedp, OPTAB_WIDEN);
1920 if (temp || methods == OPTAB_WIDEN)
1921 goto egress;
1923 /* Use the right width libcall if that exists. */
1924 temp = expand_binop (mode, direct_optab, op0, op1, target,
1925 unsignedp, OPTAB_LIB);
1926 if (temp || methods == OPTAB_LIB)
1927 goto egress;
1929 /* Must widen and use a libcall, use either signed or unsigned. */
1930 temp = expand_binop (mode, soptab, op0, op1, target,
1931 unsignedp, methods);
1932 if (!temp && unsignedp)
1933 temp = expand_binop (mode, uoptab, op0, op1, target,
1934 unsignedp, methods);
1936 egress:
1937 /* Undo the fiddling above. */
1938 if (save_enable)
1939 swap_optab_enable (soptab, mode, true);
1940 return temp;
1943 /* Generate code to perform an operation specified by UNOPPTAB
1944 on operand OP0, with two results to TARG0 and TARG1.
1945 We assume that the order of the operands for the instruction
1946 is TARG0, TARG1, OP0.
1948 Either TARG0 or TARG1 may be zero, but what that means is that
1949 the result is not actually wanted. We will generate it into
1950 a dummy pseudo-reg and discard it. They may not both be zero.
1952 Returns 1 if this operation can be performed; 0 if not. */
1955 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
1956 int unsignedp)
1958 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1959 enum mode_class mclass;
1960 machine_mode wider_mode;
1961 rtx_insn *entry_last = get_last_insn ();
1962 rtx_insn *last;
1964 mclass = GET_MODE_CLASS (mode);
1966 if (!targ0)
1967 targ0 = gen_reg_rtx (mode);
1968 if (!targ1)
1969 targ1 = gen_reg_rtx (mode);
1971 /* Record where to go back to if we fail. */
1972 last = get_last_insn ();
1974 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
1976 struct expand_operand ops[3];
1977 enum insn_code icode = optab_handler (unoptab, mode);
1979 create_fixed_operand (&ops[0], targ0);
1980 create_fixed_operand (&ops[1], targ1);
1981 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
1982 if (maybe_expand_insn (icode, 3, ops))
1983 return 1;
1986 /* It can't be done in this mode. Can we do it in a wider mode? */
1988 if (CLASS_HAS_WIDER_MODES_P (mclass))
1990 FOR_EACH_WIDER_MODE (wider_mode, mode)
1992 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
1994 rtx t0 = gen_reg_rtx (wider_mode);
1995 rtx t1 = gen_reg_rtx (wider_mode);
1996 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1998 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2000 convert_move (targ0, t0, unsignedp);
2001 convert_move (targ1, t1, unsignedp);
2002 return 1;
2004 else
2005 delete_insns_since (last);
2010 delete_insns_since (entry_last);
2011 return 0;
2014 /* Generate code to perform an operation specified by BINOPTAB
2015 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2016 We assume that the order of the operands for the instruction
2017 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2018 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2020 Either TARG0 or TARG1 may be zero, but what that means is that
2021 the result is not actually wanted. We will generate it into
2022 a dummy pseudo-reg and discard it. They may not both be zero.
2024 Returns 1 if this operation can be performed; 0 if not. */
2027 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2028 int unsignedp)
2030 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2031 enum mode_class mclass;
2032 machine_mode wider_mode;
2033 rtx_insn *entry_last = get_last_insn ();
2034 rtx_insn *last;
2036 mclass = GET_MODE_CLASS (mode);
2038 if (!targ0)
2039 targ0 = gen_reg_rtx (mode);
2040 if (!targ1)
2041 targ1 = gen_reg_rtx (mode);
2043 /* Record where to go back to if we fail. */
2044 last = get_last_insn ();
2046 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2048 struct expand_operand ops[4];
2049 enum insn_code icode = optab_handler (binoptab, mode);
2050 machine_mode mode0 = insn_data[icode].operand[1].mode;
2051 machine_mode mode1 = insn_data[icode].operand[2].mode;
2052 rtx xop0 = op0, xop1 = op1;
2054 /* If we are optimizing, force expensive constants into a register. */
2055 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2056 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2058 create_fixed_operand (&ops[0], targ0);
2059 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2060 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2061 create_fixed_operand (&ops[3], targ1);
2062 if (maybe_expand_insn (icode, 4, ops))
2063 return 1;
2064 delete_insns_since (last);
2067 /* It can't be done in this mode. Can we do it in a wider mode? */
2069 if (CLASS_HAS_WIDER_MODES_P (mclass))
2071 FOR_EACH_WIDER_MODE (wider_mode, mode)
2073 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2075 rtx t0 = gen_reg_rtx (wider_mode);
2076 rtx t1 = gen_reg_rtx (wider_mode);
2077 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2078 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2080 if (expand_twoval_binop (binoptab, cop0, cop1,
2081 t0, t1, unsignedp))
2083 convert_move (targ0, t0, unsignedp);
2084 convert_move (targ1, t1, unsignedp);
2085 return 1;
2087 else
2088 delete_insns_since (last);
2093 delete_insns_since (entry_last);
2094 return 0;
2097 /* Expand the two-valued library call indicated by BINOPTAB, but
2098 preserve only one of the values. If TARG0 is non-NULL, the first
2099 value is placed into TARG0; otherwise the second value is placed
2100 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2101 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2102 This routine assumes that the value returned by the library call is
2103 as if the return value was of an integral mode twice as wide as the
2104 mode of OP0. Returns 1 if the call was successful. */
2106 bool
2107 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2108 rtx targ0, rtx targ1, enum rtx_code code)
2110 machine_mode mode;
2111 machine_mode libval_mode;
2112 rtx libval;
2113 rtx_insn *insns;
2114 rtx libfunc;
2116 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2117 gcc_assert (!targ0 != !targ1);
2119 mode = GET_MODE (op0);
2120 libfunc = optab_libfunc (binoptab, mode);
2121 if (!libfunc)
2122 return false;
2124 /* The value returned by the library function will have twice as
2125 many bits as the nominal MODE. */
2126 libval_mode = smallest_int_mode_for_size (2 * GET_MODE_BITSIZE (mode));
2127 start_sequence ();
2128 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2129 libval_mode,
2130 op0, mode,
2131 op1, mode);
2132 /* Get the part of VAL containing the value that we want. */
2133 libval = simplify_gen_subreg (mode, libval, libval_mode,
2134 targ0 ? 0 : GET_MODE_SIZE (mode));
2135 insns = get_insns ();
2136 end_sequence ();
2137 /* Move the into the desired location. */
2138 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2139 gen_rtx_fmt_ee (code, mode, op0, op1));
2141 return true;
2145 /* Wrapper around expand_unop which takes an rtx code to specify
2146 the operation to perform, not an optab pointer. All other
2147 arguments are the same. */
2149 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2150 rtx target, int unsignedp)
2152 optab unop = code_to_optab (code);
2153 gcc_assert (unop);
2155 return expand_unop (mode, unop, op0, target, unsignedp);
2158 /* Try calculating
2159 (clz:narrow x)
2161 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2163 A similar operation can be used for clrsb. UNOPTAB says which operation
2164 we are trying to expand. */
2165 static rtx
2166 widen_leading (scalar_int_mode mode, rtx op0, rtx target, optab unoptab)
2168 opt_scalar_int_mode wider_mode_iter;
2169 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2171 scalar_int_mode wider_mode = wider_mode_iter.require ();
2172 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2174 rtx xop0, temp;
2175 rtx_insn *last;
2177 last = get_last_insn ();
2179 if (target == 0)
2180 target = gen_reg_rtx (mode);
2181 xop0 = widen_operand (op0, wider_mode, mode,
2182 unoptab != clrsb_optab, false);
2183 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2184 unoptab != clrsb_optab);
2185 if (temp != 0)
2186 temp = expand_binop
2187 (wider_mode, sub_optab, temp,
2188 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2189 - GET_MODE_PRECISION (mode),
2190 wider_mode),
2191 target, true, OPTAB_DIRECT);
2192 if (temp == 0)
2193 delete_insns_since (last);
2195 return temp;
2198 return 0;
2201 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2202 quantities, choosing which based on whether the high word is nonzero. */
2203 static rtx
2204 expand_doubleword_clz (scalar_int_mode mode, rtx op0, rtx target)
2206 rtx xop0 = force_reg (mode, op0);
2207 rtx subhi = gen_highpart (word_mode, xop0);
2208 rtx sublo = gen_lowpart (word_mode, xop0);
2209 rtx_code_label *hi0_label = gen_label_rtx ();
2210 rtx_code_label *after_label = gen_label_rtx ();
2211 rtx_insn *seq;
2212 rtx temp, result;
2214 /* If we were not given a target, use a word_mode register, not a
2215 'mode' register. The result will fit, and nobody is expecting
2216 anything bigger (the return type of __builtin_clz* is int). */
2217 if (!target)
2218 target = gen_reg_rtx (word_mode);
2220 /* In any case, write to a word_mode scratch in both branches of the
2221 conditional, so we can ensure there is a single move insn setting
2222 'target' to tag a REG_EQUAL note on. */
2223 result = gen_reg_rtx (word_mode);
2225 start_sequence ();
2227 /* If the high word is not equal to zero,
2228 then clz of the full value is clz of the high word. */
2229 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2230 word_mode, true, hi0_label);
2232 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2233 if (!temp)
2234 goto fail;
2236 if (temp != result)
2237 convert_move (result, temp, true);
2239 emit_jump_insn (targetm.gen_jump (after_label));
2240 emit_barrier ();
2242 /* Else clz of the full value is clz of the low word plus the number
2243 of bits in the high word. */
2244 emit_label (hi0_label);
2246 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2247 if (!temp)
2248 goto fail;
2249 temp = expand_binop (word_mode, add_optab, temp,
2250 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2251 result, true, OPTAB_DIRECT);
2252 if (!temp)
2253 goto fail;
2254 if (temp != result)
2255 convert_move (result, temp, true);
2257 emit_label (after_label);
2258 convert_move (target, result, true);
2260 seq = get_insns ();
2261 end_sequence ();
2263 add_equal_note (seq, target, CLZ, xop0, 0);
2264 emit_insn (seq);
2265 return target;
2267 fail:
2268 end_sequence ();
2269 return 0;
2272 /* Try calculating popcount of a double-word quantity as two popcount's of
2273 word-sized quantities and summing up the results. */
2274 static rtx
2275 expand_doubleword_popcount (scalar_int_mode mode, rtx op0, rtx target)
2277 rtx t0, t1, t;
2278 rtx_insn *seq;
2280 start_sequence ();
2282 t0 = expand_unop_direct (word_mode, popcount_optab,
2283 operand_subword_force (op0, 0, mode), NULL_RTX,
2284 true);
2285 t1 = expand_unop_direct (word_mode, popcount_optab,
2286 operand_subword_force (op0, 1, mode), NULL_RTX,
2287 true);
2288 if (!t0 || !t1)
2290 end_sequence ();
2291 return NULL_RTX;
2294 /* If we were not given a target, use a word_mode register, not a
2295 'mode' register. The result will fit, and nobody is expecting
2296 anything bigger (the return type of __builtin_popcount* is int). */
2297 if (!target)
2298 target = gen_reg_rtx (word_mode);
2300 t = expand_binop (word_mode, add_optab, t0, t1, target, 0, OPTAB_DIRECT);
2302 seq = get_insns ();
2303 end_sequence ();
2305 add_equal_note (seq, t, POPCOUNT, op0, 0);
2306 emit_insn (seq);
2307 return t;
2310 /* Try calculating
2311 (parity:wide x)
2313 (parity:narrow (low (x) ^ high (x))) */
2314 static rtx
2315 expand_doubleword_parity (scalar_int_mode mode, rtx op0, rtx target)
2317 rtx t = expand_binop (word_mode, xor_optab,
2318 operand_subword_force (op0, 0, mode),
2319 operand_subword_force (op0, 1, mode),
2320 NULL_RTX, 0, OPTAB_DIRECT);
2321 return expand_unop (word_mode, parity_optab, t, target, true);
2324 /* Try calculating
2325 (bswap:narrow x)
2327 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2328 static rtx
2329 widen_bswap (scalar_int_mode mode, rtx op0, rtx target)
2331 rtx x;
2332 rtx_insn *last;
2333 opt_scalar_int_mode wider_mode_iter;
2335 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2336 if (optab_handler (bswap_optab, wider_mode_iter.require ())
2337 != CODE_FOR_nothing)
2338 break;
2340 if (!wider_mode_iter.exists ())
2341 return NULL_RTX;
2343 scalar_int_mode wider_mode = wider_mode_iter.require ();
2344 last = get_last_insn ();
2346 x = widen_operand (op0, wider_mode, mode, true, true);
2347 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2349 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2350 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2351 if (x != 0)
2352 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2353 GET_MODE_BITSIZE (wider_mode)
2354 - GET_MODE_BITSIZE (mode),
2355 NULL_RTX, true);
2357 if (x != 0)
2359 if (target == 0)
2360 target = gen_reg_rtx (mode);
2361 emit_move_insn (target, gen_lowpart (mode, x));
2363 else
2364 delete_insns_since (last);
2366 return target;
2369 /* Try calculating bswap as two bswaps of two word-sized operands. */
2371 static rtx
2372 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2374 rtx t0, t1;
2376 t1 = expand_unop (word_mode, bswap_optab,
2377 operand_subword_force (op, 0, mode), NULL_RTX, true);
2378 t0 = expand_unop (word_mode, bswap_optab,
2379 operand_subword_force (op, 1, mode), NULL_RTX, true);
2381 if (target == 0 || !valid_multiword_target_p (target))
2382 target = gen_reg_rtx (mode);
2383 if (REG_P (target))
2384 emit_clobber (target);
2385 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2386 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2388 return target;
2391 /* Try calculating (parity x) as (and (popcount x) 1), where
2392 popcount can also be done in a wider mode. */
2393 static rtx
2394 expand_parity (scalar_int_mode mode, rtx op0, rtx target)
2396 enum mode_class mclass = GET_MODE_CLASS (mode);
2397 opt_scalar_int_mode wider_mode_iter;
2398 FOR_EACH_MODE_FROM (wider_mode_iter, mode)
2400 scalar_int_mode wider_mode = wider_mode_iter.require ();
2401 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2403 rtx xop0, temp;
2404 rtx_insn *last;
2406 last = get_last_insn ();
2408 if (target == 0 || GET_MODE (target) != wider_mode)
2409 target = gen_reg_rtx (wider_mode);
2411 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2412 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2413 true);
2414 if (temp != 0)
2415 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2416 target, true, OPTAB_DIRECT);
2418 if (temp)
2420 if (mclass != MODE_INT
2421 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2422 return convert_to_mode (mode, temp, 0);
2423 else
2424 return gen_lowpart (mode, temp);
2426 else
2427 delete_insns_since (last);
2430 return 0;
2433 /* Try calculating ctz(x) as K - clz(x & -x) ,
2434 where K is GET_MODE_PRECISION(mode) - 1.
2436 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2437 don't have to worry about what the hardware does in that case. (If
2438 the clz instruction produces the usual value at 0, which is K, the
2439 result of this code sequence will be -1; expand_ffs, below, relies
2440 on this. It might be nice to have it be K instead, for consistency
2441 with the (very few) processors that provide a ctz with a defined
2442 value, but that would take one more instruction, and it would be
2443 less convenient for expand_ffs anyway. */
2445 static rtx
2446 expand_ctz (scalar_int_mode mode, rtx op0, rtx target)
2448 rtx_insn *seq;
2449 rtx temp;
2451 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2452 return 0;
2454 start_sequence ();
2456 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2457 if (temp)
2458 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2459 true, OPTAB_DIRECT);
2460 if (temp)
2461 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2462 if (temp)
2463 temp = expand_binop (mode, sub_optab,
2464 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2465 temp, target,
2466 true, OPTAB_DIRECT);
2467 if (temp == 0)
2469 end_sequence ();
2470 return 0;
2473 seq = get_insns ();
2474 end_sequence ();
2476 add_equal_note (seq, temp, CTZ, op0, 0);
2477 emit_insn (seq);
2478 return temp;
2482 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2483 else with the sequence used by expand_clz.
2485 The ffs builtin promises to return zero for a zero value and ctz/clz
2486 may have an undefined value in that case. If they do not give us a
2487 convenient value, we have to generate a test and branch. */
2488 static rtx
2489 expand_ffs (scalar_int_mode mode, rtx op0, rtx target)
2491 HOST_WIDE_INT val = 0;
2492 bool defined_at_zero = false;
2493 rtx temp;
2494 rtx_insn *seq;
2496 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2498 start_sequence ();
2500 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2501 if (!temp)
2502 goto fail;
2504 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2506 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2508 start_sequence ();
2509 temp = expand_ctz (mode, op0, 0);
2510 if (!temp)
2511 goto fail;
2513 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2515 defined_at_zero = true;
2516 val = (GET_MODE_PRECISION (mode) - 1) - val;
2519 else
2520 return 0;
2522 if (defined_at_zero && val == -1)
2523 /* No correction needed at zero. */;
2524 else
2526 /* We don't try to do anything clever with the situation found
2527 on some processors (eg Alpha) where ctz(0:mode) ==
2528 bitsize(mode). If someone can think of a way to send N to -1
2529 and leave alone all values in the range 0..N-1 (where N is a
2530 power of two), cheaper than this test-and-branch, please add it.
2532 The test-and-branch is done after the operation itself, in case
2533 the operation sets condition codes that can be recycled for this.
2534 (This is true on i386, for instance.) */
2536 rtx_code_label *nonzero_label = gen_label_rtx ();
2537 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2538 mode, true, nonzero_label);
2540 convert_move (temp, GEN_INT (-1), false);
2541 emit_label (nonzero_label);
2544 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2545 to produce a value in the range 0..bitsize. */
2546 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2547 target, false, OPTAB_DIRECT);
2548 if (!temp)
2549 goto fail;
2551 seq = get_insns ();
2552 end_sequence ();
2554 add_equal_note (seq, temp, FFS, op0, 0);
2555 emit_insn (seq);
2556 return temp;
2558 fail:
2559 end_sequence ();
2560 return 0;
2563 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2564 conditions, VAL may already be a SUBREG against which we cannot generate
2565 a further SUBREG. In this case, we expect forcing the value into a
2566 register will work around the situation. */
2568 static rtx
2569 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
2570 machine_mode imode)
2572 rtx ret;
2573 ret = lowpart_subreg (omode, val, imode);
2574 if (ret == NULL)
2576 val = force_reg (imode, val);
2577 ret = lowpart_subreg (omode, val, imode);
2578 gcc_assert (ret != NULL);
2580 return ret;
2583 /* Expand a floating point absolute value or negation operation via a
2584 logical operation on the sign bit. */
2586 static rtx
2587 expand_absneg_bit (enum rtx_code code, scalar_float_mode mode,
2588 rtx op0, rtx target)
2590 const struct real_format *fmt;
2591 int bitpos, word, nwords, i;
2592 scalar_int_mode imode;
2593 rtx temp;
2594 rtx_insn *insns;
2596 /* The format has to have a simple sign bit. */
2597 fmt = REAL_MODE_FORMAT (mode);
2598 if (fmt == NULL)
2599 return NULL_RTX;
2601 bitpos = fmt->signbit_rw;
2602 if (bitpos < 0)
2603 return NULL_RTX;
2605 /* Don't create negative zeros if the format doesn't support them. */
2606 if (code == NEG && !fmt->has_signed_zero)
2607 return NULL_RTX;
2609 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2611 if (!int_mode_for_mode (mode).exists (&imode))
2612 return NULL_RTX;
2613 word = 0;
2614 nwords = 1;
2616 else
2618 imode = word_mode;
2620 if (FLOAT_WORDS_BIG_ENDIAN)
2621 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2622 else
2623 word = bitpos / BITS_PER_WORD;
2624 bitpos = bitpos % BITS_PER_WORD;
2625 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2628 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2629 if (code == ABS)
2630 mask = ~mask;
2632 if (target == 0
2633 || target == op0
2634 || (nwords > 1 && !valid_multiword_target_p (target)))
2635 target = gen_reg_rtx (mode);
2637 if (nwords > 1)
2639 start_sequence ();
2641 for (i = 0; i < nwords; ++i)
2643 rtx targ_piece = operand_subword (target, i, 1, mode);
2644 rtx op0_piece = operand_subword_force (op0, i, mode);
2646 if (i == word)
2648 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2649 op0_piece,
2650 immed_wide_int_const (mask, imode),
2651 targ_piece, 1, OPTAB_LIB_WIDEN);
2652 if (temp != targ_piece)
2653 emit_move_insn (targ_piece, temp);
2655 else
2656 emit_move_insn (targ_piece, op0_piece);
2659 insns = get_insns ();
2660 end_sequence ();
2662 emit_insn (insns);
2664 else
2666 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2667 gen_lowpart (imode, op0),
2668 immed_wide_int_const (mask, imode),
2669 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2670 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2672 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2673 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2674 target);
2677 return target;
2680 /* As expand_unop, but will fail rather than attempt the operation in a
2681 different mode or with a libcall. */
2682 static rtx
2683 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
2684 int unsignedp)
2686 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2688 struct expand_operand ops[2];
2689 enum insn_code icode = optab_handler (unoptab, mode);
2690 rtx_insn *last = get_last_insn ();
2691 rtx_insn *pat;
2693 create_output_operand (&ops[0], target, mode);
2694 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2695 pat = maybe_gen_insn (icode, 2, ops);
2696 if (pat)
2698 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2699 && ! add_equal_note (pat, ops[0].value,
2700 optab_to_code (unoptab),
2701 ops[1].value, NULL_RTX))
2703 delete_insns_since (last);
2704 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2707 emit_insn (pat);
2709 return ops[0].value;
2712 return 0;
2715 /* Generate code to perform an operation specified by UNOPTAB
2716 on operand OP0, with result having machine-mode MODE.
2718 UNSIGNEDP is for the case where we have to widen the operands
2719 to perform the operation. It says to use zero-extension.
2721 If TARGET is nonzero, the value
2722 is generated there, if it is convenient to do so.
2723 In all cases an rtx is returned for the locus of the value;
2724 this may or may not be TARGET. */
2727 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
2728 int unsignedp)
2730 enum mode_class mclass = GET_MODE_CLASS (mode);
2731 machine_mode wider_mode;
2732 scalar_int_mode int_mode;
2733 scalar_float_mode float_mode;
2734 rtx temp;
2735 rtx libfunc;
2737 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
2738 if (temp)
2739 return temp;
2741 /* It can't be done in this mode. Can we open-code it in a wider mode? */
2743 /* Widening (or narrowing) clz needs special treatment. */
2744 if (unoptab == clz_optab)
2746 if (is_a <scalar_int_mode> (mode, &int_mode))
2748 temp = widen_leading (int_mode, op0, target, unoptab);
2749 if (temp)
2750 return temp;
2752 if (GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2753 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2755 temp = expand_doubleword_clz (int_mode, op0, target);
2756 if (temp)
2757 return temp;
2761 goto try_libcall;
2764 if (unoptab == clrsb_optab)
2766 if (is_a <scalar_int_mode> (mode, &int_mode))
2768 temp = widen_leading (int_mode, op0, target, unoptab);
2769 if (temp)
2770 return temp;
2772 goto try_libcall;
2775 if (unoptab == popcount_optab
2776 && is_a <scalar_int_mode> (mode, &int_mode)
2777 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2778 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing
2779 && optimize_insn_for_speed_p ())
2781 temp = expand_doubleword_popcount (int_mode, op0, target);
2782 if (temp)
2783 return temp;
2786 if (unoptab == parity_optab
2787 && is_a <scalar_int_mode> (mode, &int_mode)
2788 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2789 && (optab_handler (unoptab, word_mode) != CODE_FOR_nothing
2790 || optab_handler (popcount_optab, word_mode) != CODE_FOR_nothing)
2791 && optimize_insn_for_speed_p ())
2793 temp = expand_doubleword_parity (int_mode, op0, target);
2794 if (temp)
2795 return temp;
2798 /* Widening (or narrowing) bswap needs special treatment. */
2799 if (unoptab == bswap_optab)
2801 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
2802 or ROTATERT. First try these directly; if this fails, then try the
2803 obvious pair of shifts with allowed widening, as this will probably
2804 be always more efficient than the other fallback methods. */
2805 if (mode == HImode)
2807 rtx_insn *last;
2808 rtx temp1, temp2;
2810 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
2812 temp = expand_binop (mode, rotl_optab, op0,
2813 gen_int_shift_amount (mode, 8),
2814 target, unsignedp, OPTAB_DIRECT);
2815 if (temp)
2816 return temp;
2819 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
2821 temp = expand_binop (mode, rotr_optab, op0,
2822 gen_int_shift_amount (mode, 8),
2823 target, unsignedp, OPTAB_DIRECT);
2824 if (temp)
2825 return temp;
2828 last = get_last_insn ();
2830 temp1 = expand_binop (mode, ashl_optab, op0,
2831 gen_int_shift_amount (mode, 8), NULL_RTX,
2832 unsignedp, OPTAB_WIDEN);
2833 temp2 = expand_binop (mode, lshr_optab, op0,
2834 gen_int_shift_amount (mode, 8), NULL_RTX,
2835 unsignedp, OPTAB_WIDEN);
2836 if (temp1 && temp2)
2838 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
2839 unsignedp, OPTAB_WIDEN);
2840 if (temp)
2841 return temp;
2844 delete_insns_since (last);
2847 if (is_a <scalar_int_mode> (mode, &int_mode))
2849 temp = widen_bswap (int_mode, op0, target);
2850 if (temp)
2851 return temp;
2853 if (GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2854 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2856 temp = expand_doubleword_bswap (mode, op0, target);
2857 if (temp)
2858 return temp;
2862 goto try_libcall;
2865 if (CLASS_HAS_WIDER_MODES_P (mclass))
2866 FOR_EACH_WIDER_MODE (wider_mode, mode)
2868 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2870 rtx xop0 = op0;
2871 rtx_insn *last = get_last_insn ();
2873 /* For certain operations, we need not actually extend
2874 the narrow operand, as long as we will truncate the
2875 results to the same narrowness. */
2877 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2878 (unoptab == neg_optab
2879 || unoptab == one_cmpl_optab)
2880 && mclass == MODE_INT);
2882 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2883 unsignedp);
2885 if (temp)
2887 if (mclass != MODE_INT
2888 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2890 if (target == 0)
2891 target = gen_reg_rtx (mode);
2892 convert_move (target, temp, 0);
2893 return target;
2895 else
2896 return gen_lowpart (mode, temp);
2898 else
2899 delete_insns_since (last);
2903 /* These can be done a word at a time. */
2904 if (unoptab == one_cmpl_optab
2905 && is_int_mode (mode, &int_mode)
2906 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
2907 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2909 int i;
2910 rtx_insn *insns;
2912 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
2913 target = gen_reg_rtx (int_mode);
2915 start_sequence ();
2917 /* Do the actual arithmetic. */
2918 for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
2920 rtx target_piece = operand_subword (target, i, 1, int_mode);
2921 rtx x = expand_unop (word_mode, unoptab,
2922 operand_subword_force (op0, i, int_mode),
2923 target_piece, unsignedp);
2925 if (target_piece != x)
2926 emit_move_insn (target_piece, x);
2929 insns = get_insns ();
2930 end_sequence ();
2932 emit_insn (insns);
2933 return target;
2936 if (optab_to_code (unoptab) == NEG)
2938 /* Try negating floating point values by flipping the sign bit. */
2939 if (is_a <scalar_float_mode> (mode, &float_mode))
2941 temp = expand_absneg_bit (NEG, float_mode, op0, target);
2942 if (temp)
2943 return temp;
2946 /* If there is no negation pattern, and we have no negative zero,
2947 try subtracting from zero. */
2948 if (!HONOR_SIGNED_ZEROS (mode))
2950 temp = expand_binop (mode, (unoptab == negv_optab
2951 ? subv_optab : sub_optab),
2952 CONST0_RTX (mode), op0, target,
2953 unsignedp, OPTAB_DIRECT);
2954 if (temp)
2955 return temp;
2959 /* Try calculating parity (x) as popcount (x) % 2. */
2960 if (unoptab == parity_optab && is_a <scalar_int_mode> (mode, &int_mode))
2962 temp = expand_parity (int_mode, op0, target);
2963 if (temp)
2964 return temp;
2967 /* Try implementing ffs (x) in terms of clz (x). */
2968 if (unoptab == ffs_optab && is_a <scalar_int_mode> (mode, &int_mode))
2970 temp = expand_ffs (int_mode, op0, target);
2971 if (temp)
2972 return temp;
2975 /* Try implementing ctz (x) in terms of clz (x). */
2976 if (unoptab == ctz_optab && is_a <scalar_int_mode> (mode, &int_mode))
2978 temp = expand_ctz (int_mode, op0, target);
2979 if (temp)
2980 return temp;
2983 try_libcall:
2984 /* Now try a library call in this mode. */
2985 libfunc = optab_libfunc (unoptab, mode);
2986 if (libfunc)
2988 rtx_insn *insns;
2989 rtx value;
2990 rtx eq_value;
2991 machine_mode outmode = mode;
2993 /* All of these functions return small values. Thus we choose to
2994 have them return something that isn't a double-word. */
2995 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
2996 || unoptab == clrsb_optab || unoptab == popcount_optab
2997 || unoptab == parity_optab)
2998 outmode
2999 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3000 optab_libfunc (unoptab, mode)));
3002 start_sequence ();
3004 /* Pass 1 for NO_QUEUE so we don't lose any increments
3005 if the libcall is cse'd or moved. */
3006 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3007 op0, mode);
3008 insns = get_insns ();
3009 end_sequence ();
3011 target = gen_reg_rtx (outmode);
3012 bool trapv = trapv_unoptab_p (unoptab);
3013 if (trapv)
3014 eq_value = NULL_RTX;
3015 else
3017 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3018 if (GET_MODE_UNIT_SIZE (outmode) < GET_MODE_UNIT_SIZE (mode))
3019 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3020 else if (GET_MODE_UNIT_SIZE (outmode) > GET_MODE_UNIT_SIZE (mode))
3021 eq_value = simplify_gen_unary (ZERO_EXTEND,
3022 outmode, eq_value, mode);
3024 emit_libcall_block_1 (insns, target, value, eq_value, trapv);
3026 return target;
3029 /* It can't be done in this mode. Can we do it in a wider mode? */
3031 if (CLASS_HAS_WIDER_MODES_P (mclass))
3033 FOR_EACH_WIDER_MODE (wider_mode, mode)
3035 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3036 || optab_libfunc (unoptab, wider_mode))
3038 rtx xop0 = op0;
3039 rtx_insn *last = get_last_insn ();
3041 /* For certain operations, we need not actually extend
3042 the narrow operand, as long as we will truncate the
3043 results to the same narrowness. */
3044 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3045 (unoptab == neg_optab
3046 || unoptab == one_cmpl_optab
3047 || unoptab == bswap_optab)
3048 && mclass == MODE_INT);
3050 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3051 unsignedp);
3053 /* If we are generating clz using wider mode, adjust the
3054 result. Similarly for clrsb. */
3055 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3056 && temp != 0)
3058 scalar_int_mode wider_int_mode
3059 = as_a <scalar_int_mode> (wider_mode);
3060 int_mode = as_a <scalar_int_mode> (mode);
3061 temp = expand_binop
3062 (wider_mode, sub_optab, temp,
3063 gen_int_mode (GET_MODE_PRECISION (wider_int_mode)
3064 - GET_MODE_PRECISION (int_mode),
3065 wider_int_mode),
3066 target, true, OPTAB_DIRECT);
3069 /* Likewise for bswap. */
3070 if (unoptab == bswap_optab && temp != 0)
3072 scalar_int_mode wider_int_mode
3073 = as_a <scalar_int_mode> (wider_mode);
3074 int_mode = as_a <scalar_int_mode> (mode);
3075 gcc_assert (GET_MODE_PRECISION (wider_int_mode)
3076 == GET_MODE_BITSIZE (wider_int_mode)
3077 && GET_MODE_PRECISION (int_mode)
3078 == GET_MODE_BITSIZE (int_mode));
3080 temp = expand_shift (RSHIFT_EXPR, wider_int_mode, temp,
3081 GET_MODE_BITSIZE (wider_int_mode)
3082 - GET_MODE_BITSIZE (int_mode),
3083 NULL_RTX, true);
3086 if (temp)
3088 if (mclass != MODE_INT)
3090 if (target == 0)
3091 target = gen_reg_rtx (mode);
3092 convert_move (target, temp, 0);
3093 return target;
3095 else
3096 return gen_lowpart (mode, temp);
3098 else
3099 delete_insns_since (last);
3104 /* One final attempt at implementing negation via subtraction,
3105 this time allowing widening of the operand. */
3106 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3108 rtx temp;
3109 temp = expand_binop (mode,
3110 unoptab == negv_optab ? subv_optab : sub_optab,
3111 CONST0_RTX (mode), op0,
3112 target, unsignedp, OPTAB_LIB_WIDEN);
3113 if (temp)
3114 return temp;
3117 return 0;
3120 /* Emit code to compute the absolute value of OP0, with result to
3121 TARGET if convenient. (TARGET may be 0.) The return value says
3122 where the result actually is to be found.
3124 MODE is the mode of the operand; the mode of the result is
3125 different but can be deduced from MODE.
3130 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3131 int result_unsignedp)
3133 rtx temp;
3135 if (GET_MODE_CLASS (mode) != MODE_INT
3136 || ! flag_trapv)
3137 result_unsignedp = 1;
3139 /* First try to do it with a special abs instruction. */
3140 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3141 op0, target, 0);
3142 if (temp != 0)
3143 return temp;
3145 /* For floating point modes, try clearing the sign bit. */
3146 scalar_float_mode float_mode;
3147 if (is_a <scalar_float_mode> (mode, &float_mode))
3149 temp = expand_absneg_bit (ABS, float_mode, op0, target);
3150 if (temp)
3151 return temp;
3154 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3155 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3156 && !HONOR_SIGNED_ZEROS (mode))
3158 rtx_insn *last = get_last_insn ();
3160 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3161 op0, NULL_RTX, 0);
3162 if (temp != 0)
3163 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3164 OPTAB_WIDEN);
3166 if (temp != 0)
3167 return temp;
3169 delete_insns_since (last);
3172 /* If this machine has expensive jumps, we can do integer absolute
3173 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3174 where W is the width of MODE. */
3176 scalar_int_mode int_mode;
3177 if (is_int_mode (mode, &int_mode)
3178 && BRANCH_COST (optimize_insn_for_speed_p (),
3179 false) >= 2)
3181 rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
3182 GET_MODE_PRECISION (int_mode) - 1,
3183 NULL_RTX, 0);
3185 temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
3186 OPTAB_LIB_WIDEN);
3187 if (temp != 0)
3188 temp = expand_binop (int_mode,
3189 result_unsignedp ? sub_optab : subv_optab,
3190 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3192 if (temp != 0)
3193 return temp;
3196 return NULL_RTX;
3200 expand_abs (machine_mode mode, rtx op0, rtx target,
3201 int result_unsignedp, int safe)
3203 rtx temp;
3204 rtx_code_label *op1;
3206 if (GET_MODE_CLASS (mode) != MODE_INT
3207 || ! flag_trapv)
3208 result_unsignedp = 1;
3210 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3211 if (temp != 0)
3212 return temp;
3214 /* If that does not win, use conditional jump and negate. */
3216 /* It is safe to use the target if it is the same
3217 as the source if this is also a pseudo register */
3218 if (op0 == target && REG_P (op0)
3219 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3220 safe = 1;
3222 op1 = gen_label_rtx ();
3223 if (target == 0 || ! safe
3224 || GET_MODE (target) != mode
3225 || (MEM_P (target) && MEM_VOLATILE_P (target))
3226 || (REG_P (target)
3227 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3228 target = gen_reg_rtx (mode);
3230 emit_move_insn (target, op0);
3231 NO_DEFER_POP;
3233 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3234 NULL_RTX, NULL, op1,
3235 profile_probability::uninitialized ());
3237 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3238 target, target, 0);
3239 if (op0 != target)
3240 emit_move_insn (target, op0);
3241 emit_label (op1);
3242 OK_DEFER_POP;
3243 return target;
3246 /* Emit code to compute the one's complement absolute value of OP0
3247 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3248 (TARGET may be NULL_RTX.) The return value says where the result
3249 actually is to be found.
3251 MODE is the mode of the operand; the mode of the result is
3252 different but can be deduced from MODE. */
3255 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3257 rtx temp;
3259 /* Not applicable for floating point modes. */
3260 if (FLOAT_MODE_P (mode))
3261 return NULL_RTX;
3263 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3264 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3266 rtx_insn *last = get_last_insn ();
3268 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3269 if (temp != 0)
3270 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3271 OPTAB_WIDEN);
3273 if (temp != 0)
3274 return temp;
3276 delete_insns_since (last);
3279 /* If this machine has expensive jumps, we can do one's complement
3280 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3282 scalar_int_mode int_mode;
3283 if (is_int_mode (mode, &int_mode)
3284 && BRANCH_COST (optimize_insn_for_speed_p (),
3285 false) >= 2)
3287 rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
3288 GET_MODE_PRECISION (int_mode) - 1,
3289 NULL_RTX, 0);
3291 temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
3292 OPTAB_LIB_WIDEN);
3294 if (temp != 0)
3295 return temp;
3298 return NULL_RTX;
3301 /* A subroutine of expand_copysign, perform the copysign operation using the
3302 abs and neg primitives advertised to exist on the target. The assumption
3303 is that we have a split register file, and leaving op0 in fp registers,
3304 and not playing with subregs so much, will help the register allocator. */
3306 static rtx
3307 expand_copysign_absneg (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3308 int bitpos, bool op0_is_abs)
3310 scalar_int_mode imode;
3311 enum insn_code icode;
3312 rtx sign;
3313 rtx_code_label *label;
3315 if (target == op1)
3316 target = NULL_RTX;
3318 /* Check if the back end provides an insn that handles signbit for the
3319 argument's mode. */
3320 icode = optab_handler (signbit_optab, mode);
3321 if (icode != CODE_FOR_nothing)
3323 imode = as_a <scalar_int_mode> (insn_data[(int) icode].operand[0].mode);
3324 sign = gen_reg_rtx (imode);
3325 emit_unop_insn (icode, sign, op1, UNKNOWN);
3327 else
3329 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3331 if (!int_mode_for_mode (mode).exists (&imode))
3332 return NULL_RTX;
3333 op1 = gen_lowpart (imode, op1);
3335 else
3337 int word;
3339 imode = word_mode;
3340 if (FLOAT_WORDS_BIG_ENDIAN)
3341 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3342 else
3343 word = bitpos / BITS_PER_WORD;
3344 bitpos = bitpos % BITS_PER_WORD;
3345 op1 = operand_subword_force (op1, word, mode);
3348 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3349 sign = expand_binop (imode, and_optab, op1,
3350 immed_wide_int_const (mask, imode),
3351 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3354 if (!op0_is_abs)
3356 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3357 if (op0 == NULL)
3358 return NULL_RTX;
3359 target = op0;
3361 else
3363 if (target == NULL_RTX)
3364 target = copy_to_reg (op0);
3365 else
3366 emit_move_insn (target, op0);
3369 label = gen_label_rtx ();
3370 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3372 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3373 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3374 else
3375 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3376 if (op0 != target)
3377 emit_move_insn (target, op0);
3379 emit_label (label);
3381 return target;
3385 /* A subroutine of expand_copysign, perform the entire copysign operation
3386 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3387 is true if op0 is known to have its sign bit clear. */
3389 static rtx
3390 expand_copysign_bit (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3391 int bitpos, bool op0_is_abs)
3393 scalar_int_mode imode;
3394 int word, nwords, i;
3395 rtx temp;
3396 rtx_insn *insns;
3398 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3400 if (!int_mode_for_mode (mode).exists (&imode))
3401 return NULL_RTX;
3402 word = 0;
3403 nwords = 1;
3405 else
3407 imode = word_mode;
3409 if (FLOAT_WORDS_BIG_ENDIAN)
3410 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3411 else
3412 word = bitpos / BITS_PER_WORD;
3413 bitpos = bitpos % BITS_PER_WORD;
3414 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3417 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3419 if (target == 0
3420 || target == op0
3421 || target == op1
3422 || (nwords > 1 && !valid_multiword_target_p (target)))
3423 target = gen_reg_rtx (mode);
3425 if (nwords > 1)
3427 start_sequence ();
3429 for (i = 0; i < nwords; ++i)
3431 rtx targ_piece = operand_subword (target, i, 1, mode);
3432 rtx op0_piece = operand_subword_force (op0, i, mode);
3434 if (i == word)
3436 if (!op0_is_abs)
3437 op0_piece
3438 = expand_binop (imode, and_optab, op0_piece,
3439 immed_wide_int_const (~mask, imode),
3440 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3441 op1 = expand_binop (imode, and_optab,
3442 operand_subword_force (op1, i, mode),
3443 immed_wide_int_const (mask, imode),
3444 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3446 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3447 targ_piece, 1, OPTAB_LIB_WIDEN);
3448 if (temp != targ_piece)
3449 emit_move_insn (targ_piece, temp);
3451 else
3452 emit_move_insn (targ_piece, op0_piece);
3455 insns = get_insns ();
3456 end_sequence ();
3458 emit_insn (insns);
3460 else
3462 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3463 immed_wide_int_const (mask, imode),
3464 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3466 op0 = gen_lowpart (imode, op0);
3467 if (!op0_is_abs)
3468 op0 = expand_binop (imode, and_optab, op0,
3469 immed_wide_int_const (~mask, imode),
3470 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3472 temp = expand_binop (imode, ior_optab, op0, op1,
3473 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3474 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3477 return target;
3480 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3481 scalar floating point mode. Return NULL if we do not know how to
3482 expand the operation inline. */
3485 expand_copysign (rtx op0, rtx op1, rtx target)
3487 scalar_float_mode mode;
3488 const struct real_format *fmt;
3489 bool op0_is_abs;
3490 rtx temp;
3492 mode = as_a <scalar_float_mode> (GET_MODE (op0));
3493 gcc_assert (GET_MODE (op1) == mode);
3495 /* First try to do it with a special instruction. */
3496 temp = expand_binop (mode, copysign_optab, op0, op1,
3497 target, 0, OPTAB_DIRECT);
3498 if (temp)
3499 return temp;
3501 fmt = REAL_MODE_FORMAT (mode);
3502 if (fmt == NULL || !fmt->has_signed_zero)
3503 return NULL_RTX;
3505 op0_is_abs = false;
3506 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3508 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3509 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3510 op0_is_abs = true;
3513 if (fmt->signbit_ro >= 0
3514 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3515 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3516 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3518 temp = expand_copysign_absneg (mode, op0, op1, target,
3519 fmt->signbit_ro, op0_is_abs);
3520 if (temp)
3521 return temp;
3524 if (fmt->signbit_rw < 0)
3525 return NULL_RTX;
3526 return expand_copysign_bit (mode, op0, op1, target,
3527 fmt->signbit_rw, op0_is_abs);
3530 /* Generate an instruction whose insn-code is INSN_CODE,
3531 with two operands: an output TARGET and an input OP0.
3532 TARGET *must* be nonzero, and the output is always stored there.
3533 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3534 the value that is stored into TARGET.
3536 Return false if expansion failed. */
3538 bool
3539 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3540 enum rtx_code code)
3542 struct expand_operand ops[2];
3543 rtx_insn *pat;
3545 create_output_operand (&ops[0], target, GET_MODE (target));
3546 create_input_operand (&ops[1], op0, GET_MODE (op0));
3547 pat = maybe_gen_insn (icode, 2, ops);
3548 if (!pat)
3549 return false;
3551 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3552 && code != UNKNOWN)
3553 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3555 emit_insn (pat);
3557 if (ops[0].value != target)
3558 emit_move_insn (target, ops[0].value);
3559 return true;
3561 /* Generate an instruction whose insn-code is INSN_CODE,
3562 with two operands: an output TARGET and an input OP0.
3563 TARGET *must* be nonzero, and the output is always stored there.
3564 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3565 the value that is stored into TARGET. */
3567 void
3568 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3570 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3571 gcc_assert (ok);
3574 struct no_conflict_data
3576 rtx target;
3577 rtx_insn *first, *insn;
3578 bool must_stay;
3581 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3582 the currently examined clobber / store has to stay in the list of
3583 insns that constitute the actual libcall block. */
3584 static void
3585 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3587 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3589 /* If this inns directly contributes to setting the target, it must stay. */
3590 if (reg_overlap_mentioned_p (p->target, dest))
3591 p->must_stay = true;
3592 /* If we haven't committed to keeping any other insns in the list yet,
3593 there is nothing more to check. */
3594 else if (p->insn == p->first)
3595 return;
3596 /* If this insn sets / clobbers a register that feeds one of the insns
3597 already in the list, this insn has to stay too. */
3598 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3599 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3600 || reg_used_between_p (dest, p->first, p->insn)
3601 /* Likewise if this insn depends on a register set by a previous
3602 insn in the list, or if it sets a result (presumably a hard
3603 register) that is set or clobbered by a previous insn.
3604 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3605 SET_DEST perform the former check on the address, and the latter
3606 check on the MEM. */
3607 || (GET_CODE (set) == SET
3608 && (modified_in_p (SET_SRC (set), p->first)
3609 || modified_in_p (SET_DEST (set), p->first)
3610 || modified_between_p (SET_SRC (set), p->first, p->insn)
3611 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3612 p->must_stay = true;
3616 /* Emit code to make a call to a constant function or a library call.
3618 INSNS is a list containing all insns emitted in the call.
3619 These insns leave the result in RESULT. Our block is to copy RESULT
3620 to TARGET, which is logically equivalent to EQUIV.
3622 We first emit any insns that set a pseudo on the assumption that these are
3623 loading constants into registers; doing so allows them to be safely cse'ed
3624 between blocks. Then we emit all the other insns in the block, followed by
3625 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3626 note with an operand of EQUIV. */
3628 static void
3629 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3630 bool equiv_may_trap)
3632 rtx final_dest = target;
3633 rtx_insn *next, *last, *insn;
3635 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3636 into a MEM later. Protect the libcall block from this change. */
3637 if (! REG_P (target) || REG_USERVAR_P (target))
3638 target = gen_reg_rtx (GET_MODE (target));
3640 /* If we're using non-call exceptions, a libcall corresponding to an
3641 operation that may trap may also trap. */
3642 /* ??? See the comment in front of make_reg_eh_region_note. */
3643 if (cfun->can_throw_non_call_exceptions
3644 && (equiv_may_trap || may_trap_p (equiv)))
3646 for (insn = insns; insn; insn = NEXT_INSN (insn))
3647 if (CALL_P (insn))
3649 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3650 if (note)
3652 int lp_nr = INTVAL (XEXP (note, 0));
3653 if (lp_nr == 0 || lp_nr == INT_MIN)
3654 remove_note (insn, note);
3658 else
3660 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3661 reg note to indicate that this call cannot throw or execute a nonlocal
3662 goto (unless there is already a REG_EH_REGION note, in which case
3663 we update it). */
3664 for (insn = insns; insn; insn = NEXT_INSN (insn))
3665 if (CALL_P (insn))
3666 make_reg_eh_region_note_nothrow_nononlocal (insn);
3669 /* First emit all insns that set pseudos. Remove them from the list as
3670 we go. Avoid insns that set pseudos which were referenced in previous
3671 insns. These can be generated by move_by_pieces, for example,
3672 to update an address. Similarly, avoid insns that reference things
3673 set in previous insns. */
3675 for (insn = insns; insn; insn = next)
3677 rtx set = single_set (insn);
3679 next = NEXT_INSN (insn);
3681 if (set != 0 && REG_P (SET_DEST (set))
3682 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3684 struct no_conflict_data data;
3686 data.target = const0_rtx;
3687 data.first = insns;
3688 data.insn = insn;
3689 data.must_stay = 0;
3690 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3691 if (! data.must_stay)
3693 if (PREV_INSN (insn))
3694 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3695 else
3696 insns = next;
3698 if (next)
3699 SET_PREV_INSN (next) = PREV_INSN (insn);
3701 add_insn (insn);
3705 /* Some ports use a loop to copy large arguments onto the stack.
3706 Don't move anything outside such a loop. */
3707 if (LABEL_P (insn))
3708 break;
3711 /* Write the remaining insns followed by the final copy. */
3712 for (insn = insns; insn; insn = next)
3714 next = NEXT_INSN (insn);
3716 add_insn (insn);
3719 last = emit_move_insn (target, result);
3720 if (equiv)
3721 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3723 if (final_dest != target)
3724 emit_move_insn (final_dest, target);
3727 void
3728 emit_libcall_block (rtx_insn *insns, rtx target, rtx result, rtx equiv)
3730 emit_libcall_block_1 (insns, target, result, equiv, false);
3733 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3734 PURPOSE describes how this comparison will be used. CODE is the rtx
3735 comparison code we will be using.
3737 ??? Actually, CODE is slightly weaker than that. A target is still
3738 required to implement all of the normal bcc operations, but not
3739 required to implement all (or any) of the unordered bcc operations. */
3742 can_compare_p (enum rtx_code code, machine_mode mode,
3743 enum can_compare_purpose purpose)
3745 rtx test;
3746 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3749 enum insn_code icode;
3751 if (purpose == ccp_jump
3752 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
3753 && insn_operand_matches (icode, 0, test))
3754 return 1;
3755 if (purpose == ccp_store_flag
3756 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
3757 && insn_operand_matches (icode, 1, test))
3758 return 1;
3759 if (purpose == ccp_cmov
3760 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
3761 return 1;
3763 mode = GET_MODE_WIDER_MODE (mode).else_void ();
3764 PUT_MODE (test, mode);
3766 while (mode != VOIDmode);
3768 return 0;
3771 /* This function is called when we are going to emit a compare instruction that
3772 compares the values found in X and Y, using the rtl operator COMPARISON.
3774 If they have mode BLKmode, then SIZE specifies the size of both operands.
3776 UNSIGNEDP nonzero says that the operands are unsigned;
3777 this matters if they need to be widened (as given by METHODS).
3779 *PTEST is where the resulting comparison RTX is returned or NULL_RTX
3780 if we failed to produce one.
3782 *PMODE is the mode of the inputs (in case they are const_int).
3784 This function performs all the setup necessary so that the caller only has
3785 to emit a single comparison insn. This setup can involve doing a BLKmode
3786 comparison or emitting a library call to perform the comparison if no insn
3787 is available to handle it.
3788 The values which are passed in through pointers can be modified; the caller
3789 should perform the comparison on the modified values. Constant
3790 comparisons must have already been folded. */
3792 static void
3793 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3794 int unsignedp, enum optab_methods methods,
3795 rtx *ptest, machine_mode *pmode)
3797 machine_mode mode = *pmode;
3798 rtx libfunc, test;
3799 machine_mode cmp_mode;
3800 enum mode_class mclass;
3802 /* The other methods are not needed. */
3803 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
3804 || methods == OPTAB_LIB_WIDEN);
3806 /* If we are optimizing, force expensive constants into a register. */
3807 if (CONSTANT_P (x) && optimize
3808 && (rtx_cost (x, mode, COMPARE, 0, optimize_insn_for_speed_p ())
3809 > COSTS_N_INSNS (1)))
3810 x = force_reg (mode, x);
3812 if (CONSTANT_P (y) && optimize
3813 && (rtx_cost (y, mode, COMPARE, 1, optimize_insn_for_speed_p ())
3814 > COSTS_N_INSNS (1)))
3815 y = force_reg (mode, y);
3817 #if HAVE_cc0
3818 /* Make sure if we have a canonical comparison. The RTL
3819 documentation states that canonical comparisons are required only
3820 for targets which have cc0. */
3821 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
3822 #endif
3824 /* Don't let both operands fail to indicate the mode. */
3825 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
3826 x = force_reg (mode, x);
3827 if (mode == VOIDmode)
3828 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
3830 /* Handle all BLKmode compares. */
3832 if (mode == BLKmode)
3834 machine_mode result_mode;
3835 enum insn_code cmp_code;
3836 rtx result;
3837 rtx opalign
3838 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
3840 gcc_assert (size);
3842 /* Try to use a memory block compare insn - either cmpstr
3843 or cmpmem will do. */
3844 opt_scalar_int_mode cmp_mode_iter;
3845 FOR_EACH_MODE_IN_CLASS (cmp_mode_iter, MODE_INT)
3847 scalar_int_mode cmp_mode = cmp_mode_iter.require ();
3848 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
3849 if (cmp_code == CODE_FOR_nothing)
3850 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
3851 if (cmp_code == CODE_FOR_nothing)
3852 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
3853 if (cmp_code == CODE_FOR_nothing)
3854 continue;
3856 /* Must make sure the size fits the insn's mode. */
3857 if (CONST_INT_P (size)
3858 ? INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode))
3859 : (GET_MODE_BITSIZE (as_a <scalar_int_mode> (GET_MODE (size)))
3860 > GET_MODE_BITSIZE (cmp_mode)))
3861 continue;
3863 result_mode = insn_data[cmp_code].operand[0].mode;
3864 result = gen_reg_rtx (result_mode);
3865 size = convert_to_mode (cmp_mode, size, 1);
3866 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
3868 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
3869 *pmode = result_mode;
3870 return;
3873 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
3874 goto fail;
3876 /* Otherwise call a library function. */
3877 result = emit_block_comp_via_libcall (XEXP (x, 0), XEXP (y, 0), size);
3879 x = result;
3880 y = const0_rtx;
3881 mode = TYPE_MODE (integer_type_node);
3882 methods = OPTAB_LIB_WIDEN;
3883 unsignedp = false;
3886 /* Don't allow operands to the compare to trap, as that can put the
3887 compare and branch in different basic blocks. */
3888 if (cfun->can_throw_non_call_exceptions)
3890 if (may_trap_p (x))
3891 x = copy_to_reg (x);
3892 if (may_trap_p (y))
3893 y = copy_to_reg (y);
3896 if (GET_MODE_CLASS (mode) == MODE_CC)
3898 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
3899 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3900 gcc_assert (icode != CODE_FOR_nothing
3901 && insn_operand_matches (icode, 0, test));
3902 *ptest = test;
3903 return;
3906 mclass = GET_MODE_CLASS (mode);
3907 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3908 FOR_EACH_MODE_FROM (cmp_mode, mode)
3910 enum insn_code icode;
3911 icode = optab_handler (cbranch_optab, cmp_mode);
3912 if (icode != CODE_FOR_nothing
3913 && insn_operand_matches (icode, 0, test))
3915 rtx_insn *last = get_last_insn ();
3916 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
3917 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
3918 if (op0 && op1
3919 && insn_operand_matches (icode, 1, op0)
3920 && insn_operand_matches (icode, 2, op1))
3922 XEXP (test, 0) = op0;
3923 XEXP (test, 1) = op1;
3924 *ptest = test;
3925 *pmode = cmp_mode;
3926 return;
3928 delete_insns_since (last);
3931 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
3932 break;
3935 if (methods != OPTAB_LIB_WIDEN)
3936 goto fail;
3938 if (SCALAR_FLOAT_MODE_P (mode))
3940 /* Small trick if UNORDERED isn't implemented by the hardware. */
3941 if (comparison == UNORDERED && rtx_equal_p (x, y))
3943 prepare_cmp_insn (x, y, UNLT, NULL_RTX, unsignedp, OPTAB_WIDEN,
3944 ptest, pmode);
3945 if (*ptest)
3946 return;
3949 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
3951 else
3953 rtx result;
3954 machine_mode ret_mode;
3956 /* Handle a libcall just for the mode we are using. */
3957 libfunc = optab_libfunc (cmp_optab, mode);
3958 gcc_assert (libfunc);
3960 /* If we want unsigned, and this mode has a distinct unsigned
3961 comparison routine, use that. */
3962 if (unsignedp)
3964 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
3965 if (ulibfunc)
3966 libfunc = ulibfunc;
3969 ret_mode = targetm.libgcc_cmp_return_mode ();
3970 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3971 ret_mode, x, mode, y, mode);
3973 /* There are two kinds of comparison routines. Biased routines
3974 return 0/1/2, and unbiased routines return -1/0/1. Other parts
3975 of gcc expect that the comparison operation is equivalent
3976 to the modified comparison. For signed comparisons compare the
3977 result against 1 in the biased case, and zero in the unbiased
3978 case. For unsigned comparisons always compare against 1 after
3979 biasing the unbiased result by adding 1. This gives us a way to
3980 represent LTU.
3981 The comparisons in the fixed-point helper library are always
3982 biased. */
3983 x = result;
3984 y = const1_rtx;
3986 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
3988 if (unsignedp)
3989 x = plus_constant (ret_mode, result, 1);
3990 else
3991 y = const0_rtx;
3994 *pmode = ret_mode;
3995 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
3996 ptest, pmode);
3999 return;
4001 fail:
4002 *ptest = NULL_RTX;
4005 /* Before emitting an insn with code ICODE, make sure that X, which is going
4006 to be used for operand OPNUM of the insn, is converted from mode MODE to
4007 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4008 that it is accepted by the operand predicate. Return the new value. */
4011 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
4012 machine_mode wider_mode, int unsignedp)
4014 if (mode != wider_mode)
4015 x = convert_modes (wider_mode, mode, x, unsignedp);
4017 if (!insn_operand_matches (icode, opnum, x))
4019 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
4020 if (reload_completed)
4021 return NULL_RTX;
4022 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
4023 return NULL_RTX;
4024 x = copy_to_mode_reg (op_mode, x);
4027 return x;
4030 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4031 we can do the branch. */
4033 static void
4034 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label,
4035 profile_probability prob)
4037 machine_mode optab_mode;
4038 enum mode_class mclass;
4039 enum insn_code icode;
4040 rtx_insn *insn;
4042 mclass = GET_MODE_CLASS (mode);
4043 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4044 icode = optab_handler (cbranch_optab, optab_mode);
4046 gcc_assert (icode != CODE_FOR_nothing);
4047 gcc_assert (insn_operand_matches (icode, 0, test));
4048 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4049 XEXP (test, 1), label));
4050 if (prob.initialized_p ()
4051 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4052 && insn
4053 && JUMP_P (insn)
4054 && any_condjump_p (insn)
4055 && !find_reg_note (insn, REG_BR_PROB, 0))
4056 add_reg_br_prob_note (insn, prob);
4059 /* Generate code to compare X with Y so that the condition codes are
4060 set and to jump to LABEL if the condition is true. If X is a
4061 constant and Y is not a constant, then the comparison is swapped to
4062 ensure that the comparison RTL has the canonical form.
4064 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4065 need to be widened. UNSIGNEDP is also used to select the proper
4066 branch condition code.
4068 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4070 MODE is the mode of the inputs (in case they are const_int).
4072 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4073 It will be potentially converted into an unsigned variant based on
4074 UNSIGNEDP to select a proper jump instruction.
4076 PROB is the probability of jumping to LABEL. */
4078 void
4079 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4080 machine_mode mode, int unsignedp, rtx label,
4081 profile_probability prob)
4083 rtx op0 = x, op1 = y;
4084 rtx test;
4086 /* Swap operands and condition to ensure canonical RTL. */
4087 if (swap_commutative_operands_p (x, y)
4088 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4090 op0 = y, op1 = x;
4091 comparison = swap_condition (comparison);
4094 /* If OP0 is still a constant, then both X and Y must be constants
4095 or the opposite comparison is not supported. Force X into a register
4096 to create canonical RTL. */
4097 if (CONSTANT_P (op0))
4098 op0 = force_reg (mode, op0);
4100 if (unsignedp)
4101 comparison = unsigned_condition (comparison);
4103 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4104 &test, &mode);
4105 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4109 /* Emit a library call comparison between floating point X and Y.
4110 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4112 static void
4113 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4114 rtx *ptest, machine_mode *pmode)
4116 enum rtx_code swapped = swap_condition (comparison);
4117 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4118 machine_mode orig_mode = GET_MODE (x);
4119 machine_mode mode;
4120 rtx true_rtx, false_rtx;
4121 rtx value, target, equiv;
4122 rtx_insn *insns;
4123 rtx libfunc = 0;
4124 bool reversed_p = false;
4125 scalar_int_mode cmp_mode = targetm.libgcc_cmp_return_mode ();
4127 FOR_EACH_MODE_FROM (mode, orig_mode)
4129 if (code_to_optab (comparison)
4130 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4131 break;
4133 if (code_to_optab (swapped)
4134 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4136 std::swap (x, y);
4137 comparison = swapped;
4138 break;
4141 if (code_to_optab (reversed)
4142 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4144 comparison = reversed;
4145 reversed_p = true;
4146 break;
4150 gcc_assert (mode != VOIDmode);
4152 if (mode != orig_mode)
4154 x = convert_to_mode (mode, x, 0);
4155 y = convert_to_mode (mode, y, 0);
4158 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4159 the RTL. The allows the RTL optimizers to delete the libcall if the
4160 condition can be determined at compile-time. */
4161 if (comparison == UNORDERED
4162 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4164 true_rtx = const_true_rtx;
4165 false_rtx = const0_rtx;
4167 else
4169 switch (comparison)
4171 case EQ:
4172 true_rtx = const0_rtx;
4173 false_rtx = const_true_rtx;
4174 break;
4176 case NE:
4177 true_rtx = const_true_rtx;
4178 false_rtx = const0_rtx;
4179 break;
4181 case GT:
4182 true_rtx = const1_rtx;
4183 false_rtx = const0_rtx;
4184 break;
4186 case GE:
4187 true_rtx = const0_rtx;
4188 false_rtx = constm1_rtx;
4189 break;
4191 case LT:
4192 true_rtx = constm1_rtx;
4193 false_rtx = const0_rtx;
4194 break;
4196 case LE:
4197 true_rtx = const0_rtx;
4198 false_rtx = const1_rtx;
4199 break;
4201 default:
4202 gcc_unreachable ();
4206 if (comparison == UNORDERED)
4208 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4209 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4210 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4211 temp, const_true_rtx, equiv);
4213 else
4215 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4216 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4217 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4218 equiv, true_rtx, false_rtx);
4221 start_sequence ();
4222 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4223 cmp_mode, x, mode, y, mode);
4224 insns = get_insns ();
4225 end_sequence ();
4227 target = gen_reg_rtx (cmp_mode);
4228 emit_libcall_block (insns, target, value, equiv);
4230 if (comparison == UNORDERED
4231 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4232 || reversed_p)
4233 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4234 else
4235 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4237 *pmode = cmp_mode;
4240 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4242 void
4243 emit_indirect_jump (rtx loc)
4245 if (!targetm.have_indirect_jump ())
4246 sorry ("indirect jumps are not available on this target");
4247 else
4249 struct expand_operand ops[1];
4250 create_address_operand (&ops[0], loc);
4251 expand_jump_insn (targetm.code_for_indirect_jump, 1, ops);
4252 emit_barrier ();
4257 /* Emit a conditional move instruction if the machine supports one for that
4258 condition and machine mode.
4260 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4261 the mode to use should they be constants. If it is VOIDmode, they cannot
4262 both be constants.
4264 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4265 should be stored there. MODE is the mode to use should they be constants.
4266 If it is VOIDmode, they cannot both be constants.
4268 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4269 is not supported. */
4272 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4273 machine_mode cmode, rtx op2, rtx op3,
4274 machine_mode mode, int unsignedp)
4276 rtx comparison;
4277 rtx_insn *last;
4278 enum insn_code icode;
4279 enum rtx_code reversed;
4281 /* If the two source operands are identical, that's just a move. */
4283 if (rtx_equal_p (op2, op3))
4285 if (!target)
4286 target = gen_reg_rtx (mode);
4288 emit_move_insn (target, op3);
4289 return target;
4292 /* If one operand is constant, make it the second one. Only do this
4293 if the other operand is not constant as well. */
4295 if (swap_commutative_operands_p (op0, op1))
4297 std::swap (op0, op1);
4298 code = swap_condition (code);
4301 /* get_condition will prefer to generate LT and GT even if the old
4302 comparison was against zero, so undo that canonicalization here since
4303 comparisons against zero are cheaper. */
4304 if (code == LT && op1 == const1_rtx)
4305 code = LE, op1 = const0_rtx;
4306 else if (code == GT && op1 == constm1_rtx)
4307 code = GE, op1 = const0_rtx;
4309 if (cmode == VOIDmode)
4310 cmode = GET_MODE (op0);
4312 enum rtx_code orig_code = code;
4313 bool swapped = false;
4314 if (swap_commutative_operands_p (op2, op3)
4315 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4316 != UNKNOWN))
4318 std::swap (op2, op3);
4319 code = reversed;
4320 swapped = true;
4323 if (mode == VOIDmode)
4324 mode = GET_MODE (op2);
4326 icode = direct_optab_handler (movcc_optab, mode);
4328 if (icode == CODE_FOR_nothing)
4329 return NULL_RTX;
4331 if (!target)
4332 target = gen_reg_rtx (mode);
4334 for (int pass = 0; ; pass++)
4336 code = unsignedp ? unsigned_condition (code) : code;
4337 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4339 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4340 punt and let the caller figure out how best to deal with this
4341 situation. */
4342 if (COMPARISON_P (comparison))
4344 saved_pending_stack_adjust save;
4345 save_pending_stack_adjust (&save);
4346 last = get_last_insn ();
4347 do_pending_stack_adjust ();
4348 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4349 GET_CODE (comparison), NULL_RTX, unsignedp,
4350 OPTAB_WIDEN, &comparison, &cmode);
4351 if (comparison)
4353 struct expand_operand ops[4];
4355 create_output_operand (&ops[0], target, mode);
4356 create_fixed_operand (&ops[1], comparison);
4357 create_input_operand (&ops[2], op2, mode);
4358 create_input_operand (&ops[3], op3, mode);
4359 if (maybe_expand_insn (icode, 4, ops))
4361 if (ops[0].value != target)
4362 convert_move (target, ops[0].value, false);
4363 return target;
4366 delete_insns_since (last);
4367 restore_pending_stack_adjust (&save);
4370 if (pass == 1)
4371 return NULL_RTX;
4373 /* If the preferred op2/op3 order is not usable, retry with other
4374 operand order, perhaps it will expand successfully. */
4375 if (swapped)
4376 code = orig_code;
4377 else if ((reversed = reversed_comparison_code_parts (orig_code, op0, op1,
4378 NULL))
4379 != UNKNOWN)
4380 code = reversed;
4381 else
4382 return NULL_RTX;
4383 std::swap (op2, op3);
4388 /* Emit a conditional negate or bitwise complement using the
4389 negcc or notcc optabs if available. Return NULL_RTX if such operations
4390 are not available. Otherwise return the RTX holding the result.
4391 TARGET is the desired destination of the result. COMP is the comparison
4392 on which to negate. If COND is true move into TARGET the negation
4393 or bitwise complement of OP1. Otherwise move OP2 into TARGET.
4394 CODE is either NEG or NOT. MODE is the machine mode in which the
4395 operation is performed. */
4398 emit_conditional_neg_or_complement (rtx target, rtx_code code,
4399 machine_mode mode, rtx cond, rtx op1,
4400 rtx op2)
4402 optab op = unknown_optab;
4403 if (code == NEG)
4404 op = negcc_optab;
4405 else if (code == NOT)
4406 op = notcc_optab;
4407 else
4408 gcc_unreachable ();
4410 insn_code icode = direct_optab_handler (op, mode);
4412 if (icode == CODE_FOR_nothing)
4413 return NULL_RTX;
4415 if (!target)
4416 target = gen_reg_rtx (mode);
4418 rtx_insn *last = get_last_insn ();
4419 struct expand_operand ops[4];
4421 create_output_operand (&ops[0], target, mode);
4422 create_fixed_operand (&ops[1], cond);
4423 create_input_operand (&ops[2], op1, mode);
4424 create_input_operand (&ops[3], op2, mode);
4426 if (maybe_expand_insn (icode, 4, ops))
4428 if (ops[0].value != target)
4429 convert_move (target, ops[0].value, false);
4431 return target;
4433 delete_insns_since (last);
4434 return NULL_RTX;
4437 /* Emit a conditional addition instruction if the machine supports one for that
4438 condition and machine mode.
4440 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4441 the mode to use should they be constants. If it is VOIDmode, they cannot
4442 both be constants.
4444 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4445 should be stored there. MODE is the mode to use should they be constants.
4446 If it is VOIDmode, they cannot both be constants.
4448 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4449 is not supported. */
4452 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4453 machine_mode cmode, rtx op2, rtx op3,
4454 machine_mode mode, int unsignedp)
4456 rtx comparison;
4457 rtx_insn *last;
4458 enum insn_code icode;
4460 /* If one operand is constant, make it the second one. Only do this
4461 if the other operand is not constant as well. */
4463 if (swap_commutative_operands_p (op0, op1))
4465 std::swap (op0, op1);
4466 code = swap_condition (code);
4469 /* get_condition will prefer to generate LT and GT even if the old
4470 comparison was against zero, so undo that canonicalization here since
4471 comparisons against zero are cheaper. */
4472 if (code == LT && op1 == const1_rtx)
4473 code = LE, op1 = const0_rtx;
4474 else if (code == GT && op1 == constm1_rtx)
4475 code = GE, op1 = const0_rtx;
4477 if (cmode == VOIDmode)
4478 cmode = GET_MODE (op0);
4480 if (mode == VOIDmode)
4481 mode = GET_MODE (op2);
4483 icode = optab_handler (addcc_optab, mode);
4485 if (icode == CODE_FOR_nothing)
4486 return 0;
4488 if (!target)
4489 target = gen_reg_rtx (mode);
4491 code = unsignedp ? unsigned_condition (code) : code;
4492 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4494 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4495 return NULL and let the caller figure out how best to deal with this
4496 situation. */
4497 if (!COMPARISON_P (comparison))
4498 return NULL_RTX;
4500 do_pending_stack_adjust ();
4501 last = get_last_insn ();
4502 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4503 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4504 &comparison, &cmode);
4505 if (comparison)
4507 struct expand_operand ops[4];
4509 create_output_operand (&ops[0], target, mode);
4510 create_fixed_operand (&ops[1], comparison);
4511 create_input_operand (&ops[2], op2, mode);
4512 create_input_operand (&ops[3], op3, mode);
4513 if (maybe_expand_insn (icode, 4, ops))
4515 if (ops[0].value != target)
4516 convert_move (target, ops[0].value, false);
4517 return target;
4520 delete_insns_since (last);
4521 return NULL_RTX;
4524 /* These functions attempt to generate an insn body, rather than
4525 emitting the insn, but if the gen function already emits them, we
4526 make no attempt to turn them back into naked patterns. */
4528 /* Generate and return an insn body to add Y to X. */
4530 rtx_insn *
4531 gen_add2_insn (rtx x, rtx y)
4533 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4535 gcc_assert (insn_operand_matches (icode, 0, x));
4536 gcc_assert (insn_operand_matches (icode, 1, x));
4537 gcc_assert (insn_operand_matches (icode, 2, y));
4539 return GEN_FCN (icode) (x, x, y);
4542 /* Generate and return an insn body to add r1 and c,
4543 storing the result in r0. */
4545 rtx_insn *
4546 gen_add3_insn (rtx r0, rtx r1, rtx c)
4548 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4550 if (icode == CODE_FOR_nothing
4551 || !insn_operand_matches (icode, 0, r0)
4552 || !insn_operand_matches (icode, 1, r1)
4553 || !insn_operand_matches (icode, 2, c))
4554 return NULL;
4556 return GEN_FCN (icode) (r0, r1, c);
4560 have_add2_insn (rtx x, rtx y)
4562 enum insn_code icode;
4564 gcc_assert (GET_MODE (x) != VOIDmode);
4566 icode = optab_handler (add_optab, GET_MODE (x));
4568 if (icode == CODE_FOR_nothing)
4569 return 0;
4571 if (!insn_operand_matches (icode, 0, x)
4572 || !insn_operand_matches (icode, 1, x)
4573 || !insn_operand_matches (icode, 2, y))
4574 return 0;
4576 return 1;
4579 /* Generate and return an insn body to add Y to X. */
4581 rtx_insn *
4582 gen_addptr3_insn (rtx x, rtx y, rtx z)
4584 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4586 gcc_assert (insn_operand_matches (icode, 0, x));
4587 gcc_assert (insn_operand_matches (icode, 1, y));
4588 gcc_assert (insn_operand_matches (icode, 2, z));
4590 return GEN_FCN (icode) (x, y, z);
4593 /* Return true if the target implements an addptr pattern and X, Y,
4594 and Z are valid for the pattern predicates. */
4597 have_addptr3_insn (rtx x, rtx y, rtx z)
4599 enum insn_code icode;
4601 gcc_assert (GET_MODE (x) != VOIDmode);
4603 icode = optab_handler (addptr3_optab, GET_MODE (x));
4605 if (icode == CODE_FOR_nothing)
4606 return 0;
4608 if (!insn_operand_matches (icode, 0, x)
4609 || !insn_operand_matches (icode, 1, y)
4610 || !insn_operand_matches (icode, 2, z))
4611 return 0;
4613 return 1;
4616 /* Generate and return an insn body to subtract Y from X. */
4618 rtx_insn *
4619 gen_sub2_insn (rtx x, rtx y)
4621 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4623 gcc_assert (insn_operand_matches (icode, 0, x));
4624 gcc_assert (insn_operand_matches (icode, 1, x));
4625 gcc_assert (insn_operand_matches (icode, 2, y));
4627 return GEN_FCN (icode) (x, x, y);
4630 /* Generate and return an insn body to subtract r1 and c,
4631 storing the result in r0. */
4633 rtx_insn *
4634 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4636 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4638 if (icode == CODE_FOR_nothing
4639 || !insn_operand_matches (icode, 0, r0)
4640 || !insn_operand_matches (icode, 1, r1)
4641 || !insn_operand_matches (icode, 2, c))
4642 return NULL;
4644 return GEN_FCN (icode) (r0, r1, c);
4648 have_sub2_insn (rtx x, rtx y)
4650 enum insn_code icode;
4652 gcc_assert (GET_MODE (x) != VOIDmode);
4654 icode = optab_handler (sub_optab, GET_MODE (x));
4656 if (icode == CODE_FOR_nothing)
4657 return 0;
4659 if (!insn_operand_matches (icode, 0, x)
4660 || !insn_operand_matches (icode, 1, x)
4661 || !insn_operand_matches (icode, 2, y))
4662 return 0;
4664 return 1;
4667 /* Generate the body of an insn to extend Y (with mode MFROM)
4668 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4670 rtx_insn *
4671 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4672 machine_mode mfrom, int unsignedp)
4674 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4675 return GEN_FCN (icode) (x, y);
4678 /* Generate code to convert FROM to floating point
4679 and store in TO. FROM must be fixed point and not VOIDmode.
4680 UNSIGNEDP nonzero means regard FROM as unsigned.
4681 Normally this is done by correcting the final value
4682 if it is negative. */
4684 void
4685 expand_float (rtx to, rtx from, int unsignedp)
4687 enum insn_code icode;
4688 rtx target = to;
4689 scalar_mode from_mode, to_mode;
4690 machine_mode fmode, imode;
4691 bool can_do_signed = false;
4693 /* Crash now, because we won't be able to decide which mode to use. */
4694 gcc_assert (GET_MODE (from) != VOIDmode);
4696 /* Look for an insn to do the conversion. Do it in the specified
4697 modes if possible; otherwise convert either input, output or both to
4698 wider mode. If the integer mode is wider than the mode of FROM,
4699 we can do the conversion signed even if the input is unsigned. */
4701 FOR_EACH_MODE_FROM (fmode, GET_MODE (to))
4702 FOR_EACH_MODE_FROM (imode, GET_MODE (from))
4704 int doing_unsigned = unsignedp;
4706 if (fmode != GET_MODE (to)
4707 && (significand_size (fmode)
4708 < GET_MODE_UNIT_PRECISION (GET_MODE (from))))
4709 continue;
4711 icode = can_float_p (fmode, imode, unsignedp);
4712 if (icode == CODE_FOR_nothing && unsignedp)
4714 enum insn_code scode = can_float_p (fmode, imode, 0);
4715 if (scode != CODE_FOR_nothing)
4716 can_do_signed = true;
4717 if (imode != GET_MODE (from))
4718 icode = scode, doing_unsigned = 0;
4721 if (icode != CODE_FOR_nothing)
4723 if (imode != GET_MODE (from))
4724 from = convert_to_mode (imode, from, unsignedp);
4726 if (fmode != GET_MODE (to))
4727 target = gen_reg_rtx (fmode);
4729 emit_unop_insn (icode, target, from,
4730 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4732 if (target != to)
4733 convert_move (to, target, 0);
4734 return;
4738 /* Unsigned integer, and no way to convert directly. Convert as signed,
4739 then unconditionally adjust the result. */
4740 if (unsignedp
4741 && can_do_signed
4742 && is_a <scalar_mode> (GET_MODE (to), &to_mode)
4743 && is_a <scalar_mode> (GET_MODE (from), &from_mode))
4745 opt_scalar_mode fmode_iter;
4746 rtx_code_label *label = gen_label_rtx ();
4747 rtx temp;
4748 REAL_VALUE_TYPE offset;
4750 /* Look for a usable floating mode FMODE wider than the source and at
4751 least as wide as the target. Using FMODE will avoid rounding woes
4752 with unsigned values greater than the signed maximum value. */
4754 FOR_EACH_MODE_FROM (fmode_iter, to_mode)
4756 scalar_mode fmode = fmode_iter.require ();
4757 if (GET_MODE_PRECISION (from_mode) < GET_MODE_BITSIZE (fmode)
4758 && can_float_p (fmode, from_mode, 0) != CODE_FOR_nothing)
4759 break;
4762 if (!fmode_iter.exists (&fmode))
4764 /* There is no such mode. Pretend the target is wide enough. */
4765 fmode = to_mode;
4767 /* Avoid double-rounding when TO is narrower than FROM. */
4768 if ((significand_size (fmode) + 1)
4769 < GET_MODE_PRECISION (from_mode))
4771 rtx temp1;
4772 rtx_code_label *neglabel = gen_label_rtx ();
4774 /* Don't use TARGET if it isn't a register, is a hard register,
4775 or is the wrong mode. */
4776 if (!REG_P (target)
4777 || REGNO (target) < FIRST_PSEUDO_REGISTER
4778 || GET_MODE (target) != fmode)
4779 target = gen_reg_rtx (fmode);
4781 imode = from_mode;
4782 do_pending_stack_adjust ();
4784 /* Test whether the sign bit is set. */
4785 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4786 0, neglabel);
4788 /* The sign bit is not set. Convert as signed. */
4789 expand_float (target, from, 0);
4790 emit_jump_insn (targetm.gen_jump (label));
4791 emit_barrier ();
4793 /* The sign bit is set.
4794 Convert to a usable (positive signed) value by shifting right
4795 one bit, while remembering if a nonzero bit was shifted
4796 out; i.e., compute (from & 1) | (from >> 1). */
4798 emit_label (neglabel);
4799 temp = expand_binop (imode, and_optab, from, const1_rtx,
4800 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4801 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
4802 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4803 OPTAB_LIB_WIDEN);
4804 expand_float (target, temp, 0);
4806 /* Multiply by 2 to undo the shift above. */
4807 temp = expand_binop (fmode, add_optab, target, target,
4808 target, 0, OPTAB_LIB_WIDEN);
4809 if (temp != target)
4810 emit_move_insn (target, temp);
4812 do_pending_stack_adjust ();
4813 emit_label (label);
4814 goto done;
4818 /* If we are about to do some arithmetic to correct for an
4819 unsigned operand, do it in a pseudo-register. */
4821 if (to_mode != fmode
4822 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
4823 target = gen_reg_rtx (fmode);
4825 /* Convert as signed integer to floating. */
4826 expand_float (target, from, 0);
4828 /* If FROM is negative (and therefore TO is negative),
4829 correct its value by 2**bitwidth. */
4831 do_pending_stack_adjust ();
4832 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, from_mode,
4833 0, label);
4836 real_2expN (&offset, GET_MODE_PRECISION (from_mode), fmode);
4837 temp = expand_binop (fmode, add_optab, target,
4838 const_double_from_real_value (offset, fmode),
4839 target, 0, OPTAB_LIB_WIDEN);
4840 if (temp != target)
4841 emit_move_insn (target, temp);
4843 do_pending_stack_adjust ();
4844 emit_label (label);
4845 goto done;
4848 /* No hardware instruction available; call a library routine. */
4850 rtx libfunc;
4851 rtx_insn *insns;
4852 rtx value;
4853 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
4855 if (is_narrower_int_mode (GET_MODE (from), SImode))
4856 from = convert_to_mode (SImode, from, unsignedp);
4858 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
4859 gcc_assert (libfunc);
4861 start_sequence ();
4863 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4864 GET_MODE (to), from, GET_MODE (from));
4865 insns = get_insns ();
4866 end_sequence ();
4868 emit_libcall_block (insns, target, value,
4869 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
4870 GET_MODE (to), from));
4873 done:
4875 /* Copy result to requested destination
4876 if we have been computing in a temp location. */
4878 if (target != to)
4880 if (GET_MODE (target) == GET_MODE (to))
4881 emit_move_insn (to, target);
4882 else
4883 convert_move (to, target, 0);
4887 /* Generate code to convert FROM to fixed point and store in TO. FROM
4888 must be floating point. */
4890 void
4891 expand_fix (rtx to, rtx from, int unsignedp)
4893 enum insn_code icode;
4894 rtx target = to;
4895 machine_mode fmode, imode;
4896 opt_scalar_mode fmode_iter;
4897 bool must_trunc = false;
4899 /* We first try to find a pair of modes, one real and one integer, at
4900 least as wide as FROM and TO, respectively, in which we can open-code
4901 this conversion. If the integer mode is wider than the mode of TO,
4902 we can do the conversion either signed or unsigned. */
4904 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
4905 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
4907 int doing_unsigned = unsignedp;
4909 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
4910 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
4911 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
4913 if (icode != CODE_FOR_nothing)
4915 rtx_insn *last = get_last_insn ();
4916 if (fmode != GET_MODE (from))
4917 from = convert_to_mode (fmode, from, 0);
4919 if (must_trunc)
4921 rtx temp = gen_reg_rtx (GET_MODE (from));
4922 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
4923 temp, 0);
4926 if (imode != GET_MODE (to))
4927 target = gen_reg_rtx (imode);
4929 if (maybe_emit_unop_insn (icode, target, from,
4930 doing_unsigned ? UNSIGNED_FIX : FIX))
4932 if (target != to)
4933 convert_move (to, target, unsignedp);
4934 return;
4936 delete_insns_since (last);
4940 /* For an unsigned conversion, there is one more way to do it.
4941 If we have a signed conversion, we generate code that compares
4942 the real value to the largest representable positive number. If if
4943 is smaller, the conversion is done normally. Otherwise, subtract
4944 one plus the highest signed number, convert, and add it back.
4946 We only need to check all real modes, since we know we didn't find
4947 anything with a wider integer mode.
4949 This code used to extend FP value into mode wider than the destination.
4950 This is needed for decimal float modes which cannot accurately
4951 represent one plus the highest signed number of the same size, but
4952 not for binary modes. Consider, for instance conversion from SFmode
4953 into DImode.
4955 The hot path through the code is dealing with inputs smaller than 2^63
4956 and doing just the conversion, so there is no bits to lose.
4958 In the other path we know the value is positive in the range 2^63..2^64-1
4959 inclusive. (as for other input overflow happens and result is undefined)
4960 So we know that the most important bit set in mantissa corresponds to
4961 2^63. The subtraction of 2^63 should not generate any rounding as it
4962 simply clears out that bit. The rest is trivial. */
4964 scalar_int_mode to_mode;
4965 if (unsignedp
4966 && is_a <scalar_int_mode> (GET_MODE (to), &to_mode)
4967 && HWI_COMPUTABLE_MODE_P (to_mode))
4968 FOR_EACH_MODE_FROM (fmode_iter, as_a <scalar_mode> (GET_MODE (from)))
4970 scalar_mode fmode = fmode_iter.require ();
4971 if (CODE_FOR_nothing != can_fix_p (to_mode, fmode,
4972 0, &must_trunc)
4973 && (!DECIMAL_FLOAT_MODE_P (fmode)
4974 || (GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (to_mode))))
4976 int bitsize;
4977 REAL_VALUE_TYPE offset;
4978 rtx limit;
4979 rtx_code_label *lab1, *lab2;
4980 rtx_insn *insn;
4982 bitsize = GET_MODE_PRECISION (to_mode);
4983 real_2expN (&offset, bitsize - 1, fmode);
4984 limit = const_double_from_real_value (offset, fmode);
4985 lab1 = gen_label_rtx ();
4986 lab2 = gen_label_rtx ();
4988 if (fmode != GET_MODE (from))
4989 from = convert_to_mode (fmode, from, 0);
4991 /* See if we need to do the subtraction. */
4992 do_pending_stack_adjust ();
4993 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX,
4994 GET_MODE (from), 0, lab1);
4996 /* If not, do the signed "fix" and branch around fixup code. */
4997 expand_fix (to, from, 0);
4998 emit_jump_insn (targetm.gen_jump (lab2));
4999 emit_barrier ();
5001 /* Otherwise, subtract 2**(N-1), convert to signed number,
5002 then add 2**(N-1). Do the addition using XOR since this
5003 will often generate better code. */
5004 emit_label (lab1);
5005 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5006 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5007 expand_fix (to, target, 0);
5008 target = expand_binop (to_mode, xor_optab, to,
5009 gen_int_mode
5010 (HOST_WIDE_INT_1 << (bitsize - 1),
5011 to_mode),
5012 to, 1, OPTAB_LIB_WIDEN);
5014 if (target != to)
5015 emit_move_insn (to, target);
5017 emit_label (lab2);
5019 if (optab_handler (mov_optab, to_mode) != CODE_FOR_nothing)
5021 /* Make a place for a REG_NOTE and add it. */
5022 insn = emit_move_insn (to, to);
5023 set_dst_reg_note (insn, REG_EQUAL,
5024 gen_rtx_fmt_e (UNSIGNED_FIX, to_mode,
5025 copy_rtx (from)),
5026 to);
5029 return;
5033 /* We can't do it with an insn, so use a library call. But first ensure
5034 that the mode of TO is at least as wide as SImode, since those are the
5035 only library calls we know about. */
5037 if (is_narrower_int_mode (GET_MODE (to), SImode))
5039 target = gen_reg_rtx (SImode);
5041 expand_fix (target, from, unsignedp);
5043 else
5045 rtx_insn *insns;
5046 rtx value;
5047 rtx libfunc;
5049 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5050 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5051 gcc_assert (libfunc);
5053 start_sequence ();
5055 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5056 GET_MODE (to), from, GET_MODE (from));
5057 insns = get_insns ();
5058 end_sequence ();
5060 emit_libcall_block (insns, target, value,
5061 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5062 GET_MODE (to), from));
5065 if (target != to)
5067 if (GET_MODE (to) == GET_MODE (target))
5068 emit_move_insn (to, target);
5069 else
5070 convert_move (to, target, 0);
5075 /* Promote integer arguments for a libcall if necessary.
5076 emit_library_call_value cannot do the promotion because it does not
5077 know if it should do a signed or unsigned promotion. This is because
5078 there are no tree types defined for libcalls. */
5080 static rtx
5081 prepare_libcall_arg (rtx arg, int uintp)
5083 scalar_int_mode mode;
5084 machine_mode arg_mode;
5085 if (is_a <scalar_int_mode> (GET_MODE (arg), &mode))
5087 /* If we need to promote the integer function argument we need to do
5088 it here instead of inside emit_library_call_value because in
5089 emit_library_call_value we don't know if we should do a signed or
5090 unsigned promotion. */
5092 int unsigned_p = 0;
5093 arg_mode = promote_function_mode (NULL_TREE, mode,
5094 &unsigned_p, NULL_TREE, 0);
5095 if (arg_mode != mode)
5096 return convert_to_mode (arg_mode, arg, uintp);
5098 return arg;
5101 /* Generate code to convert FROM or TO a fixed-point.
5102 If UINTP is true, either TO or FROM is an unsigned integer.
5103 If SATP is true, we need to saturate the result. */
5105 void
5106 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5108 machine_mode to_mode = GET_MODE (to);
5109 machine_mode from_mode = GET_MODE (from);
5110 convert_optab tab;
5111 enum rtx_code this_code;
5112 enum insn_code code;
5113 rtx_insn *insns;
5114 rtx value;
5115 rtx libfunc;
5117 if (to_mode == from_mode)
5119 emit_move_insn (to, from);
5120 return;
5123 if (uintp)
5125 tab = satp ? satfractuns_optab : fractuns_optab;
5126 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5128 else
5130 tab = satp ? satfract_optab : fract_optab;
5131 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5133 code = convert_optab_handler (tab, to_mode, from_mode);
5134 if (code != CODE_FOR_nothing)
5136 emit_unop_insn (code, to, from, this_code);
5137 return;
5140 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5141 gcc_assert (libfunc);
5143 from = prepare_libcall_arg (from, uintp);
5144 from_mode = GET_MODE (from);
5146 start_sequence ();
5147 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5148 from, from_mode);
5149 insns = get_insns ();
5150 end_sequence ();
5152 emit_libcall_block (insns, to, value,
5153 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5156 /* Generate code to convert FROM to fixed point and store in TO. FROM
5157 must be floating point, TO must be signed. Use the conversion optab
5158 TAB to do the conversion. */
5160 bool
5161 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5163 enum insn_code icode;
5164 rtx target = to;
5165 machine_mode fmode, imode;
5167 /* We first try to find a pair of modes, one real and one integer, at
5168 least as wide as FROM and TO, respectively, in which we can open-code
5169 this conversion. If the integer mode is wider than the mode of TO,
5170 we can do the conversion either signed or unsigned. */
5172 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
5173 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
5175 icode = convert_optab_handler (tab, imode, fmode);
5176 if (icode != CODE_FOR_nothing)
5178 rtx_insn *last = get_last_insn ();
5179 if (fmode != GET_MODE (from))
5180 from = convert_to_mode (fmode, from, 0);
5182 if (imode != GET_MODE (to))
5183 target = gen_reg_rtx (imode);
5185 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5187 delete_insns_since (last);
5188 continue;
5190 if (target != to)
5191 convert_move (to, target, 0);
5192 return true;
5196 return false;
5199 /* Report whether we have an instruction to perform the operation
5200 specified by CODE on operands of mode MODE. */
5202 have_insn_for (enum rtx_code code, machine_mode mode)
5204 return (code_to_optab (code)
5205 && (optab_handler (code_to_optab (code), mode)
5206 != CODE_FOR_nothing));
5209 /* Print information about the current contents of the optabs on
5210 STDERR. */
5212 DEBUG_FUNCTION void
5213 debug_optab_libfuncs (void)
5215 int i, j, k;
5217 /* Dump the arithmetic optabs. */
5218 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
5219 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5221 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
5222 if (l)
5224 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5225 fprintf (stderr, "%s\t%s:\t%s\n",
5226 GET_RTX_NAME (optab_to_code ((optab) i)),
5227 GET_MODE_NAME (j),
5228 XSTR (l, 0));
5232 /* Dump the conversion optabs. */
5233 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
5234 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5235 for (k = 0; k < NUM_MACHINE_MODES; ++k)
5237 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
5238 (machine_mode) k);
5239 if (l)
5241 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5242 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
5243 GET_RTX_NAME (optab_to_code ((optab) i)),
5244 GET_MODE_NAME (j),
5245 GET_MODE_NAME (k),
5246 XSTR (l, 0));
5251 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5252 CODE. Return 0 on failure. */
5254 rtx_insn *
5255 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
5257 machine_mode mode = GET_MODE (op1);
5258 enum insn_code icode;
5259 rtx_insn *insn;
5260 rtx trap_rtx;
5262 if (mode == VOIDmode)
5263 return 0;
5265 icode = optab_handler (ctrap_optab, mode);
5266 if (icode == CODE_FOR_nothing)
5267 return 0;
5269 /* Some targets only accept a zero trap code. */
5270 if (!insn_operand_matches (icode, 3, tcode))
5271 return 0;
5273 do_pending_stack_adjust ();
5274 start_sequence ();
5275 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
5276 &trap_rtx, &mode);
5277 if (!trap_rtx)
5278 insn = NULL;
5279 else
5280 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
5281 tcode);
5283 /* If that failed, then give up. */
5284 if (insn == 0)
5286 end_sequence ();
5287 return 0;
5290 emit_insn (insn);
5291 insn = get_insns ();
5292 end_sequence ();
5293 return insn;
5296 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
5297 or unsigned operation code. */
5299 enum rtx_code
5300 get_rtx_code (enum tree_code tcode, bool unsignedp)
5302 enum rtx_code code;
5303 switch (tcode)
5305 case EQ_EXPR:
5306 code = EQ;
5307 break;
5308 case NE_EXPR:
5309 code = NE;
5310 break;
5311 case LT_EXPR:
5312 code = unsignedp ? LTU : LT;
5313 break;
5314 case LE_EXPR:
5315 code = unsignedp ? LEU : LE;
5316 break;
5317 case GT_EXPR:
5318 code = unsignedp ? GTU : GT;
5319 break;
5320 case GE_EXPR:
5321 code = unsignedp ? GEU : GE;
5322 break;
5324 case UNORDERED_EXPR:
5325 code = UNORDERED;
5326 break;
5327 case ORDERED_EXPR:
5328 code = ORDERED;
5329 break;
5330 case UNLT_EXPR:
5331 code = UNLT;
5332 break;
5333 case UNLE_EXPR:
5334 code = UNLE;
5335 break;
5336 case UNGT_EXPR:
5337 code = UNGT;
5338 break;
5339 case UNGE_EXPR:
5340 code = UNGE;
5341 break;
5342 case UNEQ_EXPR:
5343 code = UNEQ;
5344 break;
5345 case LTGT_EXPR:
5346 code = LTGT;
5347 break;
5349 case BIT_AND_EXPR:
5350 code = AND;
5351 break;
5353 case BIT_IOR_EXPR:
5354 code = IOR;
5355 break;
5357 default:
5358 gcc_unreachable ();
5360 return code;
5363 /* Return a comparison rtx of mode CMP_MODE for COND. Use UNSIGNEDP to
5364 select signed or unsigned operators. OPNO holds the index of the
5365 first comparison operand for insn ICODE. Do not generate the
5366 compare instruction itself. */
5368 static rtx
5369 vector_compare_rtx (machine_mode cmp_mode, enum tree_code tcode,
5370 tree t_op0, tree t_op1, bool unsignedp,
5371 enum insn_code icode, unsigned int opno)
5373 struct expand_operand ops[2];
5374 rtx rtx_op0, rtx_op1;
5375 machine_mode m0, m1;
5376 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
5378 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
5380 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
5381 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
5382 cases, use the original mode. */
5383 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
5384 EXPAND_STACK_PARM);
5385 m0 = GET_MODE (rtx_op0);
5386 if (m0 == VOIDmode)
5387 m0 = TYPE_MODE (TREE_TYPE (t_op0));
5389 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
5390 EXPAND_STACK_PARM);
5391 m1 = GET_MODE (rtx_op1);
5392 if (m1 == VOIDmode)
5393 m1 = TYPE_MODE (TREE_TYPE (t_op1));
5395 create_input_operand (&ops[0], rtx_op0, m0);
5396 create_input_operand (&ops[1], rtx_op1, m1);
5397 if (!maybe_legitimize_operands (icode, opno, 2, ops))
5398 gcc_unreachable ();
5399 return gen_rtx_fmt_ee (rcode, cmp_mode, ops[0].value, ops[1].value);
5402 /* Check if vec_perm mask SEL is a constant equivalent to a shift of
5403 the first vec_perm operand, assuming the second operand is a constant
5404 vector of zeros. Return the shift distance in bits if so, or NULL_RTX
5405 if the vec_perm is not a shift. MODE is the mode of the value being
5406 shifted. */
5407 static rtx
5408 shift_amt_for_vec_perm_mask (machine_mode mode, const vec_perm_indices &sel)
5410 unsigned int bitsize = GET_MODE_UNIT_BITSIZE (mode);
5411 poly_int64 first = sel[0];
5412 if (maybe_ge (sel[0], GET_MODE_NUNITS (mode)))
5413 return NULL_RTX;
5415 if (!sel.series_p (0, 1, first, 1))
5417 unsigned int nelt;
5418 if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
5419 return NULL_RTX;
5420 for (unsigned int i = 1; i < nelt; i++)
5422 poly_int64 expected = i + first;
5423 /* Indices into the second vector are all equivalent. */
5424 if (maybe_lt (sel[i], nelt)
5425 ? maybe_ne (sel[i], expected)
5426 : maybe_lt (expected, nelt))
5427 return NULL_RTX;
5431 return gen_int_shift_amount (mode, first * bitsize);
5434 /* A subroutine of expand_vec_perm_var for expanding one vec_perm insn. */
5436 static rtx
5437 expand_vec_perm_1 (enum insn_code icode, rtx target,
5438 rtx v0, rtx v1, rtx sel)
5440 machine_mode tmode = GET_MODE (target);
5441 machine_mode smode = GET_MODE (sel);
5442 struct expand_operand ops[4];
5444 gcc_assert (GET_MODE_CLASS (smode) == MODE_VECTOR_INT
5445 || mode_for_int_vector (tmode).require () == smode);
5446 create_output_operand (&ops[0], target, tmode);
5447 create_input_operand (&ops[3], sel, smode);
5449 /* Make an effort to preserve v0 == v1. The target expander is able to
5450 rely on this to determine if we're permuting a single input operand. */
5451 if (rtx_equal_p (v0, v1))
5453 if (!insn_operand_matches (icode, 1, v0))
5454 v0 = force_reg (tmode, v0);
5455 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
5456 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
5458 create_fixed_operand (&ops[1], v0);
5459 create_fixed_operand (&ops[2], v0);
5461 else
5463 create_input_operand (&ops[1], v0, tmode);
5464 create_input_operand (&ops[2], v1, tmode);
5467 if (maybe_expand_insn (icode, 4, ops))
5468 return ops[0].value;
5469 return NULL_RTX;
5472 /* Implement a permutation of vectors v0 and v1 using the permutation
5473 vector in SEL and return the result. Use TARGET to hold the result
5474 if nonnull and convenient.
5476 MODE is the mode of the vectors being permuted (V0 and V1). SEL_MODE
5477 is the TYPE_MODE associated with SEL, or BLKmode if SEL isn't known
5478 to have a particular mode. */
5481 expand_vec_perm_const (machine_mode mode, rtx v0, rtx v1,
5482 const vec_perm_builder &sel, machine_mode sel_mode,
5483 rtx target)
5485 if (!target || !register_operand (target, mode))
5486 target = gen_reg_rtx (mode);
5488 /* Set QIMODE to a different vector mode with byte elements.
5489 If no such mode, or if MODE already has byte elements, use VOIDmode. */
5490 machine_mode qimode;
5491 if (!qimode_for_vec_perm (mode).exists (&qimode))
5492 qimode = VOIDmode;
5494 rtx_insn *last = get_last_insn ();
5496 bool single_arg_p = rtx_equal_p (v0, v1);
5497 /* Always specify two input vectors here and leave the target to handle
5498 cases in which the inputs are equal. Not all backends can cope with
5499 the single-input representation when testing for a double-input
5500 target instruction. */
5501 vec_perm_indices indices (sel, 2, GET_MODE_NUNITS (mode));
5503 /* See if this can be handled with a vec_shr. We only do this if the
5504 second vector is all zeroes. */
5505 insn_code shift_code = optab_handler (vec_shr_optab, mode);
5506 insn_code shift_code_qi = ((qimode != VOIDmode && qimode != mode)
5507 ? optab_handler (vec_shr_optab, qimode)
5508 : CODE_FOR_nothing);
5510 if (v1 == CONST0_RTX (GET_MODE (v1))
5511 && (shift_code != CODE_FOR_nothing
5512 || shift_code_qi != CODE_FOR_nothing))
5514 rtx shift_amt = shift_amt_for_vec_perm_mask (mode, indices);
5515 if (shift_amt)
5517 struct expand_operand ops[3];
5518 if (shift_code != CODE_FOR_nothing)
5520 create_output_operand (&ops[0], target, mode);
5521 create_input_operand (&ops[1], v0, mode);
5522 create_convert_operand_from_type (&ops[2], shift_amt, sizetype);
5523 if (maybe_expand_insn (shift_code, 3, ops))
5524 return ops[0].value;
5526 if (shift_code_qi != CODE_FOR_nothing)
5528 rtx tmp = gen_reg_rtx (qimode);
5529 create_output_operand (&ops[0], tmp, qimode);
5530 create_input_operand (&ops[1], gen_lowpart (qimode, v0), qimode);
5531 create_convert_operand_from_type (&ops[2], shift_amt, sizetype);
5532 if (maybe_expand_insn (shift_code_qi, 3, ops))
5533 return gen_lowpart (mode, ops[0].value);
5538 if (targetm.vectorize.vec_perm_const != NULL)
5540 v0 = force_reg (mode, v0);
5541 if (single_arg_p)
5542 v1 = v0;
5543 else
5544 v1 = force_reg (mode, v1);
5546 if (targetm.vectorize.vec_perm_const (mode, target, v0, v1, indices))
5547 return target;
5550 /* Fall back to a constant byte-based permutation. */
5551 vec_perm_indices qimode_indices;
5552 rtx target_qi = NULL_RTX, v0_qi = NULL_RTX, v1_qi = NULL_RTX;
5553 if (qimode != VOIDmode)
5555 qimode_indices.new_expanded_vector (indices, GET_MODE_UNIT_SIZE (mode));
5556 target_qi = gen_reg_rtx (qimode);
5557 v0_qi = gen_lowpart (qimode, v0);
5558 v1_qi = gen_lowpart (qimode, v1);
5559 if (targetm.vectorize.vec_perm_const != NULL
5560 && targetm.vectorize.vec_perm_const (qimode, target_qi, v0_qi,
5561 v1_qi, qimode_indices))
5562 return gen_lowpart (mode, target_qi);
5565 /* Otherwise expand as a fully variable permuation. */
5567 /* The optabs are only defined for selectors with the same width
5568 as the values being permuted. */
5569 machine_mode required_sel_mode;
5570 if (!mode_for_int_vector (mode).exists (&required_sel_mode)
5571 || !VECTOR_MODE_P (required_sel_mode))
5573 delete_insns_since (last);
5574 return NULL_RTX;
5577 /* We know that it is semantically valid to treat SEL as having SEL_MODE.
5578 If that isn't the mode we want then we need to prove that using
5579 REQUIRED_SEL_MODE is OK. */
5580 if (sel_mode != required_sel_mode)
5582 if (!selector_fits_mode_p (required_sel_mode, indices))
5584 delete_insns_since (last);
5585 return NULL_RTX;
5587 sel_mode = required_sel_mode;
5590 insn_code icode = direct_optab_handler (vec_perm_optab, mode);
5591 if (icode != CODE_FOR_nothing)
5593 rtx sel_rtx = vec_perm_indices_to_rtx (sel_mode, indices);
5594 rtx tmp = expand_vec_perm_1 (icode, target, v0, v1, sel_rtx);
5595 if (tmp)
5596 return tmp;
5599 if (qimode != VOIDmode
5600 && selector_fits_mode_p (qimode, qimode_indices))
5602 icode = direct_optab_handler (vec_perm_optab, qimode);
5603 if (icode != CODE_FOR_nothing)
5605 rtx sel_qi = vec_perm_indices_to_rtx (qimode, qimode_indices);
5606 rtx tmp = expand_vec_perm_1 (icode, target_qi, v0_qi, v1_qi, sel_qi);
5607 if (tmp)
5608 return gen_lowpart (mode, tmp);
5612 delete_insns_since (last);
5613 return NULL_RTX;
5616 /* Implement a permutation of vectors v0 and v1 using the permutation
5617 vector in SEL and return the result. Use TARGET to hold the result
5618 if nonnull and convenient.
5620 MODE is the mode of the vectors being permuted (V0 and V1).
5621 SEL must have the integer equivalent of MODE and is known to be
5622 unsuitable for permutes with a constant permutation vector. */
5625 expand_vec_perm_var (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
5627 enum insn_code icode;
5628 unsigned int i, u;
5629 rtx tmp, sel_qi;
5631 u = GET_MODE_UNIT_SIZE (mode);
5633 if (!target || GET_MODE (target) != mode)
5634 target = gen_reg_rtx (mode);
5636 icode = direct_optab_handler (vec_perm_optab, mode);
5637 if (icode != CODE_FOR_nothing)
5639 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
5640 if (tmp)
5641 return tmp;
5644 /* As a special case to aid several targets, lower the element-based
5645 permutation to a byte-based permutation and try again. */
5646 machine_mode qimode;
5647 if (!qimode_for_vec_perm (mode).exists (&qimode)
5648 || maybe_gt (GET_MODE_NUNITS (qimode), GET_MODE_MASK (QImode) + 1))
5649 return NULL_RTX;
5650 icode = direct_optab_handler (vec_perm_optab, qimode);
5651 if (icode == CODE_FOR_nothing)
5652 return NULL_RTX;
5654 /* Multiply each element by its byte size. */
5655 machine_mode selmode = GET_MODE (sel);
5656 if (u == 2)
5657 sel = expand_simple_binop (selmode, PLUS, sel, sel,
5658 NULL, 0, OPTAB_DIRECT);
5659 else
5660 sel = expand_simple_binop (selmode, ASHIFT, sel,
5661 gen_int_shift_amount (selmode, exact_log2 (u)),
5662 NULL, 0, OPTAB_DIRECT);
5663 gcc_assert (sel != NULL);
5665 /* Broadcast the low byte each element into each of its bytes.
5666 The encoding has U interleaved stepped patterns, one for each
5667 byte of an element. */
5668 vec_perm_builder const_sel (GET_MODE_SIZE (mode), u, 3);
5669 unsigned int low_byte_in_u = BYTES_BIG_ENDIAN ? u - 1 : 0;
5670 for (i = 0; i < 3; ++i)
5671 for (unsigned int j = 0; j < u; ++j)
5672 const_sel.quick_push (i * u + low_byte_in_u);
5673 sel = gen_lowpart (qimode, sel);
5674 sel = expand_vec_perm_const (qimode, sel, sel, const_sel, qimode, NULL);
5675 gcc_assert (sel != NULL);
5677 /* Add the byte offset to each byte element. */
5678 /* Note that the definition of the indicies here is memory ordering,
5679 so there should be no difference between big and little endian. */
5680 rtx_vector_builder byte_indices (qimode, u, 1);
5681 for (i = 0; i < u; ++i)
5682 byte_indices.quick_push (GEN_INT (i));
5683 tmp = byte_indices.build ();
5684 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
5685 sel, 0, OPTAB_DIRECT);
5686 gcc_assert (sel_qi != NULL);
5688 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
5689 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
5690 gen_lowpart (qimode, v1), sel_qi);
5691 if (tmp)
5692 tmp = gen_lowpart (mode, tmp);
5693 return tmp;
5696 /* Generate insns for a VEC_COND_EXPR with mask, given its TYPE and its
5697 three operands. */
5700 expand_vec_cond_mask_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
5701 rtx target)
5703 struct expand_operand ops[4];
5704 machine_mode mode = TYPE_MODE (vec_cond_type);
5705 machine_mode mask_mode = TYPE_MODE (TREE_TYPE (op0));
5706 enum insn_code icode = get_vcond_mask_icode (mode, mask_mode);
5707 rtx mask, rtx_op1, rtx_op2;
5709 if (icode == CODE_FOR_nothing)
5710 return 0;
5712 mask = expand_normal (op0);
5713 rtx_op1 = expand_normal (op1);
5714 rtx_op2 = expand_normal (op2);
5716 mask = force_reg (mask_mode, mask);
5717 rtx_op1 = force_reg (GET_MODE (rtx_op1), rtx_op1);
5719 create_output_operand (&ops[0], target, mode);
5720 create_input_operand (&ops[1], rtx_op1, mode);
5721 create_input_operand (&ops[2], rtx_op2, mode);
5722 create_input_operand (&ops[3], mask, mask_mode);
5723 expand_insn (icode, 4, ops);
5725 return ops[0].value;
5728 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
5729 three operands. */
5732 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
5733 rtx target)
5735 struct expand_operand ops[6];
5736 enum insn_code icode;
5737 rtx comparison, rtx_op1, rtx_op2;
5738 machine_mode mode = TYPE_MODE (vec_cond_type);
5739 machine_mode cmp_op_mode;
5740 bool unsignedp;
5741 tree op0a, op0b;
5742 enum tree_code tcode;
5744 if (COMPARISON_CLASS_P (op0))
5746 op0a = TREE_OPERAND (op0, 0);
5747 op0b = TREE_OPERAND (op0, 1);
5748 tcode = TREE_CODE (op0);
5750 else
5752 gcc_assert (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (op0)));
5753 if (get_vcond_mask_icode (mode, TYPE_MODE (TREE_TYPE (op0)))
5754 != CODE_FOR_nothing)
5755 return expand_vec_cond_mask_expr (vec_cond_type, op0, op1,
5756 op2, target);
5757 /* Fake op0 < 0. */
5758 else
5760 gcc_assert (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (op0)))
5761 == MODE_VECTOR_INT);
5762 op0a = op0;
5763 op0b = build_zero_cst (TREE_TYPE (op0));
5764 tcode = LT_EXPR;
5767 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
5768 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
5771 gcc_assert (known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (cmp_op_mode))
5772 && known_eq (GET_MODE_NUNITS (mode),
5773 GET_MODE_NUNITS (cmp_op_mode)));
5775 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
5776 if (icode == CODE_FOR_nothing)
5778 if (tcode == EQ_EXPR || tcode == NE_EXPR)
5779 icode = get_vcond_eq_icode (mode, cmp_op_mode);
5780 if (icode == CODE_FOR_nothing)
5781 return 0;
5784 comparison = vector_compare_rtx (VOIDmode, tcode, op0a, op0b, unsignedp,
5785 icode, 4);
5786 rtx_op1 = expand_normal (op1);
5787 rtx_op2 = expand_normal (op2);
5789 create_output_operand (&ops[0], target, mode);
5790 create_input_operand (&ops[1], rtx_op1, mode);
5791 create_input_operand (&ops[2], rtx_op2, mode);
5792 create_fixed_operand (&ops[3], comparison);
5793 create_fixed_operand (&ops[4], XEXP (comparison, 0));
5794 create_fixed_operand (&ops[5], XEXP (comparison, 1));
5795 expand_insn (icode, 6, ops);
5796 return ops[0].value;
5799 /* Generate VEC_SERIES_EXPR <OP0, OP1>, returning a value of mode VMODE.
5800 Use TARGET for the result if nonnull and convenient. */
5803 expand_vec_series_expr (machine_mode vmode, rtx op0, rtx op1, rtx target)
5805 struct expand_operand ops[3];
5806 enum insn_code icode;
5807 machine_mode emode = GET_MODE_INNER (vmode);
5809 icode = direct_optab_handler (vec_series_optab, vmode);
5810 gcc_assert (icode != CODE_FOR_nothing);
5812 create_output_operand (&ops[0], target, vmode);
5813 create_input_operand (&ops[1], op0, emode);
5814 create_input_operand (&ops[2], op1, emode);
5816 expand_insn (icode, 3, ops);
5817 return ops[0].value;
5820 /* Generate insns for a vector comparison into a mask. */
5823 expand_vec_cmp_expr (tree type, tree exp, rtx target)
5825 struct expand_operand ops[4];
5826 enum insn_code icode;
5827 rtx comparison;
5828 machine_mode mask_mode = TYPE_MODE (type);
5829 machine_mode vmode;
5830 bool unsignedp;
5831 tree op0a, op0b;
5832 enum tree_code tcode;
5834 op0a = TREE_OPERAND (exp, 0);
5835 op0b = TREE_OPERAND (exp, 1);
5836 tcode = TREE_CODE (exp);
5838 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
5839 vmode = TYPE_MODE (TREE_TYPE (op0a));
5841 icode = get_vec_cmp_icode (vmode, mask_mode, unsignedp);
5842 if (icode == CODE_FOR_nothing)
5844 if (tcode == EQ_EXPR || tcode == NE_EXPR)
5845 icode = get_vec_cmp_eq_icode (vmode, mask_mode);
5846 if (icode == CODE_FOR_nothing)
5847 return 0;
5850 comparison = vector_compare_rtx (mask_mode, tcode, op0a, op0b,
5851 unsignedp, icode, 2);
5852 create_output_operand (&ops[0], target, mask_mode);
5853 create_fixed_operand (&ops[1], comparison);
5854 create_fixed_operand (&ops[2], XEXP (comparison, 0));
5855 create_fixed_operand (&ops[3], XEXP (comparison, 1));
5856 expand_insn (icode, 4, ops);
5857 return ops[0].value;
5860 /* Expand a highpart multiply. */
5863 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
5864 rtx target, bool uns_p)
5866 struct expand_operand eops[3];
5867 enum insn_code icode;
5868 int method, i;
5869 machine_mode wmode;
5870 rtx m1, m2;
5871 optab tab1, tab2;
5873 method = can_mult_highpart_p (mode, uns_p);
5874 switch (method)
5876 case 0:
5877 return NULL_RTX;
5878 case 1:
5879 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
5880 return expand_binop (mode, tab1, op0, op1, target, uns_p,
5881 OPTAB_LIB_WIDEN);
5882 case 2:
5883 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
5884 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
5885 break;
5886 case 3:
5887 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
5888 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
5889 if (BYTES_BIG_ENDIAN)
5890 std::swap (tab1, tab2);
5891 break;
5892 default:
5893 gcc_unreachable ();
5896 icode = optab_handler (tab1, mode);
5897 wmode = insn_data[icode].operand[0].mode;
5898 gcc_checking_assert (known_eq (2 * GET_MODE_NUNITS (wmode),
5899 GET_MODE_NUNITS (mode)));
5900 gcc_checking_assert (known_eq (GET_MODE_SIZE (wmode), GET_MODE_SIZE (mode)));
5902 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
5903 create_input_operand (&eops[1], op0, mode);
5904 create_input_operand (&eops[2], op1, mode);
5905 expand_insn (icode, 3, eops);
5906 m1 = gen_lowpart (mode, eops[0].value);
5908 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
5909 create_input_operand (&eops[1], op0, mode);
5910 create_input_operand (&eops[2], op1, mode);
5911 expand_insn (optab_handler (tab2, mode), 3, eops);
5912 m2 = gen_lowpart (mode, eops[0].value);
5914 vec_perm_builder sel;
5915 if (method == 2)
5917 /* The encoding has 2 interleaved stepped patterns. */
5918 sel.new_vector (GET_MODE_NUNITS (mode), 2, 3);
5919 for (i = 0; i < 6; ++i)
5920 sel.quick_push (!BYTES_BIG_ENDIAN + (i & ~1)
5921 + ((i & 1) ? GET_MODE_NUNITS (mode) : 0));
5923 else
5925 /* The encoding has a single interleaved stepped pattern. */
5926 sel.new_vector (GET_MODE_NUNITS (mode), 1, 3);
5927 for (i = 0; i < 3; ++i)
5928 sel.quick_push (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
5931 return expand_vec_perm_const (mode, m1, m2, sel, BLKmode, target);
5934 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
5935 pattern. */
5937 static void
5938 find_cc_set (rtx x, const_rtx pat, void *data)
5940 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
5941 && GET_CODE (pat) == SET)
5943 rtx *p_cc_reg = (rtx *) data;
5944 gcc_assert (!*p_cc_reg);
5945 *p_cc_reg = x;
5949 /* This is a helper function for the other atomic operations. This function
5950 emits a loop that contains SEQ that iterates until a compare-and-swap
5951 operation at the end succeeds. MEM is the memory to be modified. SEQ is
5952 a set of instructions that takes a value from OLD_REG as an input and
5953 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
5954 set to the current contents of MEM. After SEQ, a compare-and-swap will
5955 attempt to update MEM with NEW_REG. The function returns true when the
5956 loop was generated successfully. */
5958 static bool
5959 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
5961 machine_mode mode = GET_MODE (mem);
5962 rtx_code_label *label;
5963 rtx cmp_reg, success, oldval;
5965 /* The loop we want to generate looks like
5967 cmp_reg = mem;
5968 label:
5969 old_reg = cmp_reg;
5970 seq;
5971 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
5972 if (success)
5973 goto label;
5975 Note that we only do the plain load from memory once. Subsequent
5976 iterations use the value loaded by the compare-and-swap pattern. */
5978 label = gen_label_rtx ();
5979 cmp_reg = gen_reg_rtx (mode);
5981 emit_move_insn (cmp_reg, mem);
5982 emit_label (label);
5983 emit_move_insn (old_reg, cmp_reg);
5984 if (seq)
5985 emit_insn (seq);
5987 success = NULL_RTX;
5988 oldval = cmp_reg;
5989 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
5990 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
5991 MEMMODEL_RELAXED))
5992 return false;
5994 if (oldval != cmp_reg)
5995 emit_move_insn (cmp_reg, oldval);
5997 /* Mark this jump predicted not taken. */
5998 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
5999 GET_MODE (success), 1, label,
6000 profile_probability::guessed_never ());
6001 return true;
6005 /* This function tries to emit an atomic_exchange intruction. VAL is written
6006 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
6007 using TARGET if possible. */
6009 static rtx
6010 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6012 machine_mode mode = GET_MODE (mem);
6013 enum insn_code icode;
6015 /* If the target supports the exchange directly, great. */
6016 icode = direct_optab_handler (atomic_exchange_optab, mode);
6017 if (icode != CODE_FOR_nothing)
6019 struct expand_operand ops[4];
6021 create_output_operand (&ops[0], target, mode);
6022 create_fixed_operand (&ops[1], mem);
6023 create_input_operand (&ops[2], val, mode);
6024 create_integer_operand (&ops[3], model);
6025 if (maybe_expand_insn (icode, 4, ops))
6026 return ops[0].value;
6029 return NULL_RTX;
6032 /* This function tries to implement an atomic exchange operation using
6033 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
6034 The previous contents of *MEM are returned, using TARGET if possible.
6035 Since this instructionn is an acquire barrier only, stronger memory
6036 models may require additional barriers to be emitted. */
6038 static rtx
6039 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
6040 enum memmodel model)
6042 machine_mode mode = GET_MODE (mem);
6043 enum insn_code icode;
6044 rtx_insn *last_insn = get_last_insn ();
6046 icode = optab_handler (sync_lock_test_and_set_optab, mode);
6048 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
6049 exists, and the memory model is stronger than acquire, add a release
6050 barrier before the instruction. */
6052 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
6053 expand_mem_thread_fence (model);
6055 if (icode != CODE_FOR_nothing)
6057 struct expand_operand ops[3];
6058 create_output_operand (&ops[0], target, mode);
6059 create_fixed_operand (&ops[1], mem);
6060 create_input_operand (&ops[2], val, mode);
6061 if (maybe_expand_insn (icode, 3, ops))
6062 return ops[0].value;
6065 /* If an external test-and-set libcall is provided, use that instead of
6066 any external compare-and-swap that we might get from the compare-and-
6067 swap-loop expansion later. */
6068 if (!can_compare_and_swap_p (mode, false))
6070 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
6071 if (libfunc != NULL)
6073 rtx addr;
6075 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6076 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6077 mode, addr, ptr_mode,
6078 val, mode);
6082 /* If the test_and_set can't be emitted, eliminate any barrier that might
6083 have been emitted. */
6084 delete_insns_since (last_insn);
6085 return NULL_RTX;
6088 /* This function tries to implement an atomic exchange operation using a
6089 compare_and_swap loop. VAL is written to *MEM. The previous contents of
6090 *MEM are returned, using TARGET if possible. No memory model is required
6091 since a compare_and_swap loop is seq-cst. */
6093 static rtx
6094 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
6096 machine_mode mode = GET_MODE (mem);
6098 if (can_compare_and_swap_p (mode, true))
6100 if (!target || !register_operand (target, mode))
6101 target = gen_reg_rtx (mode);
6102 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
6103 return target;
6106 return NULL_RTX;
6109 /* This function tries to implement an atomic test-and-set operation
6110 using the atomic_test_and_set instruction pattern. A boolean value
6111 is returned from the operation, using TARGET if possible. */
6113 static rtx
6114 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6116 machine_mode pat_bool_mode;
6117 struct expand_operand ops[3];
6119 if (!targetm.have_atomic_test_and_set ())
6120 return NULL_RTX;
6122 /* While we always get QImode from __atomic_test_and_set, we get
6123 other memory modes from __sync_lock_test_and_set. Note that we
6124 use no endian adjustment here. This matches the 4.6 behavior
6125 in the Sparc backend. */
6126 enum insn_code icode = targetm.code_for_atomic_test_and_set;
6127 gcc_checking_assert (insn_data[icode].operand[1].mode == QImode);
6128 if (GET_MODE (mem) != QImode)
6129 mem = adjust_address_nv (mem, QImode, 0);
6131 pat_bool_mode = insn_data[icode].operand[0].mode;
6132 create_output_operand (&ops[0], target, pat_bool_mode);
6133 create_fixed_operand (&ops[1], mem);
6134 create_integer_operand (&ops[2], model);
6136 if (maybe_expand_insn (icode, 3, ops))
6137 return ops[0].value;
6138 return NULL_RTX;
6141 /* This function expands the legacy _sync_lock test_and_set operation which is
6142 generally an atomic exchange. Some limited targets only allow the
6143 constant 1 to be stored. This is an ACQUIRE operation.
6145 TARGET is an optional place to stick the return value.
6146 MEM is where VAL is stored. */
6149 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
6151 rtx ret;
6153 /* Try an atomic_exchange first. */
6154 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
6155 if (ret)
6156 return ret;
6158 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
6159 MEMMODEL_SYNC_ACQUIRE);
6160 if (ret)
6161 return ret;
6163 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6164 if (ret)
6165 return ret;
6167 /* If there are no other options, try atomic_test_and_set if the value
6168 being stored is 1. */
6169 if (val == const1_rtx)
6170 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
6172 return ret;
6175 /* This function expands the atomic test_and_set operation:
6176 atomically store a boolean TRUE into MEM and return the previous value.
6178 MEMMODEL is the memory model variant to use.
6179 TARGET is an optional place to stick the return value. */
6182 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6184 machine_mode mode = GET_MODE (mem);
6185 rtx ret, trueval, subtarget;
6187 ret = maybe_emit_atomic_test_and_set (target, mem, model);
6188 if (ret)
6189 return ret;
6191 /* Be binary compatible with non-default settings of trueval, and different
6192 cpu revisions. E.g. one revision may have atomic-test-and-set, but
6193 another only has atomic-exchange. */
6194 if (targetm.atomic_test_and_set_trueval == 1)
6196 trueval = const1_rtx;
6197 subtarget = target ? target : gen_reg_rtx (mode);
6199 else
6201 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
6202 subtarget = gen_reg_rtx (mode);
6205 /* Try the atomic-exchange optab... */
6206 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
6208 /* ... then an atomic-compare-and-swap loop ... */
6209 if (!ret)
6210 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
6212 /* ... before trying the vaguely defined legacy lock_test_and_set. */
6213 if (!ret)
6214 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
6216 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
6217 things with the value 1. Thus we try again without trueval. */
6218 if (!ret && targetm.atomic_test_and_set_trueval != 1)
6219 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
6221 /* Failing all else, assume a single threaded environment and simply
6222 perform the operation. */
6223 if (!ret)
6225 /* If the result is ignored skip the move to target. */
6226 if (subtarget != const0_rtx)
6227 emit_move_insn (subtarget, mem);
6229 emit_move_insn (mem, trueval);
6230 ret = subtarget;
6233 /* Recall that have to return a boolean value; rectify if trueval
6234 is not exactly one. */
6235 if (targetm.atomic_test_and_set_trueval != 1)
6236 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
6238 return ret;
6241 /* This function expands the atomic exchange operation:
6242 atomically store VAL in MEM and return the previous value in MEM.
6244 MEMMODEL is the memory model variant to use.
6245 TARGET is an optional place to stick the return value. */
6248 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6250 machine_mode mode = GET_MODE (mem);
6251 rtx ret;
6253 /* If loads are not atomic for the required size and we are not called to
6254 provide a __sync builtin, do not do anything so that we stay consistent
6255 with atomic loads of the same size. */
6256 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6257 return NULL_RTX;
6259 ret = maybe_emit_atomic_exchange (target, mem, val, model);
6261 /* Next try a compare-and-swap loop for the exchange. */
6262 if (!ret)
6263 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6265 return ret;
6268 /* This function expands the atomic compare exchange operation:
6270 *PTARGET_BOOL is an optional place to store the boolean success/failure.
6271 *PTARGET_OVAL is an optional place to store the old value from memory.
6272 Both target parameters may be NULL or const0_rtx to indicate that we do
6273 not care about that return value. Both target parameters are updated on
6274 success to the actual location of the corresponding result.
6276 MEMMODEL is the memory model variant to use.
6278 The return value of the function is true for success. */
6280 bool
6281 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
6282 rtx mem, rtx expected, rtx desired,
6283 bool is_weak, enum memmodel succ_model,
6284 enum memmodel fail_model)
6286 machine_mode mode = GET_MODE (mem);
6287 struct expand_operand ops[8];
6288 enum insn_code icode;
6289 rtx target_oval, target_bool = NULL_RTX;
6290 rtx libfunc;
6292 /* If loads are not atomic for the required size and we are not called to
6293 provide a __sync builtin, do not do anything so that we stay consistent
6294 with atomic loads of the same size. */
6295 if (!can_atomic_load_p (mode) && !is_mm_sync (succ_model))
6296 return false;
6298 /* Load expected into a register for the compare and swap. */
6299 if (MEM_P (expected))
6300 expected = copy_to_reg (expected);
6302 /* Make sure we always have some place to put the return oldval.
6303 Further, make sure that place is distinct from the input expected,
6304 just in case we need that path down below. */
6305 if (ptarget_oval && *ptarget_oval == const0_rtx)
6306 ptarget_oval = NULL;
6308 if (ptarget_oval == NULL
6309 || (target_oval = *ptarget_oval) == NULL
6310 || reg_overlap_mentioned_p (expected, target_oval))
6311 target_oval = gen_reg_rtx (mode);
6313 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
6314 if (icode != CODE_FOR_nothing)
6316 machine_mode bool_mode = insn_data[icode].operand[0].mode;
6318 if (ptarget_bool && *ptarget_bool == const0_rtx)
6319 ptarget_bool = NULL;
6321 /* Make sure we always have a place for the bool operand. */
6322 if (ptarget_bool == NULL
6323 || (target_bool = *ptarget_bool) == NULL
6324 || GET_MODE (target_bool) != bool_mode)
6325 target_bool = gen_reg_rtx (bool_mode);
6327 /* Emit the compare_and_swap. */
6328 create_output_operand (&ops[0], target_bool, bool_mode);
6329 create_output_operand (&ops[1], target_oval, mode);
6330 create_fixed_operand (&ops[2], mem);
6331 create_input_operand (&ops[3], expected, mode);
6332 create_input_operand (&ops[4], desired, mode);
6333 create_integer_operand (&ops[5], is_weak);
6334 create_integer_operand (&ops[6], succ_model);
6335 create_integer_operand (&ops[7], fail_model);
6336 if (maybe_expand_insn (icode, 8, ops))
6338 /* Return success/failure. */
6339 target_bool = ops[0].value;
6340 target_oval = ops[1].value;
6341 goto success;
6345 /* Otherwise fall back to the original __sync_val_compare_and_swap
6346 which is always seq-cst. */
6347 icode = optab_handler (sync_compare_and_swap_optab, mode);
6348 if (icode != CODE_FOR_nothing)
6350 rtx cc_reg;
6352 create_output_operand (&ops[0], target_oval, mode);
6353 create_fixed_operand (&ops[1], mem);
6354 create_input_operand (&ops[2], expected, mode);
6355 create_input_operand (&ops[3], desired, mode);
6356 if (!maybe_expand_insn (icode, 4, ops))
6357 return false;
6359 target_oval = ops[0].value;
6361 /* If the caller isn't interested in the boolean return value,
6362 skip the computation of it. */
6363 if (ptarget_bool == NULL)
6364 goto success;
6366 /* Otherwise, work out if the compare-and-swap succeeded. */
6367 cc_reg = NULL_RTX;
6368 if (have_insn_for (COMPARE, CCmode))
6369 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6370 if (cc_reg)
6372 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
6373 const0_rtx, VOIDmode, 0, 1);
6374 goto success;
6376 goto success_bool_from_val;
6379 /* Also check for library support for __sync_val_compare_and_swap. */
6380 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
6381 if (libfunc != NULL)
6383 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6384 rtx target = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6385 mode, addr, ptr_mode,
6386 expected, mode, desired, mode);
6387 emit_move_insn (target_oval, target);
6389 /* Compute the boolean return value only if requested. */
6390 if (ptarget_bool)
6391 goto success_bool_from_val;
6392 else
6393 goto success;
6396 /* Failure. */
6397 return false;
6399 success_bool_from_val:
6400 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
6401 expected, VOIDmode, 1, 1);
6402 success:
6403 /* Make sure that the oval output winds up where the caller asked. */
6404 if (ptarget_oval)
6405 *ptarget_oval = target_oval;
6406 if (ptarget_bool)
6407 *ptarget_bool = target_bool;
6408 return true;
6411 /* Generate asm volatile("" : : : "memory") as the memory blockage. */
6413 static void
6414 expand_asm_memory_blockage (void)
6416 rtx asm_op, clob;
6418 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
6419 rtvec_alloc (0), rtvec_alloc (0),
6420 rtvec_alloc (0), UNKNOWN_LOCATION);
6421 MEM_VOLATILE_P (asm_op) = 1;
6423 clob = gen_rtx_SCRATCH (VOIDmode);
6424 clob = gen_rtx_MEM (BLKmode, clob);
6425 clob = gen_rtx_CLOBBER (VOIDmode, clob);
6427 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
6430 /* Do not propagate memory accesses across this point. */
6432 static void
6433 expand_memory_blockage (void)
6435 if (targetm.have_memory_blockage ())
6436 emit_insn (targetm.gen_memory_blockage ());
6437 else
6438 expand_asm_memory_blockage ();
6441 /* This routine will either emit the mem_thread_fence pattern or issue a
6442 sync_synchronize to generate a fence for memory model MEMMODEL. */
6444 void
6445 expand_mem_thread_fence (enum memmodel model)
6447 if (is_mm_relaxed (model))
6448 return;
6449 if (targetm.have_mem_thread_fence ())
6451 emit_insn (targetm.gen_mem_thread_fence (GEN_INT (model)));
6452 expand_memory_blockage ();
6454 else if (targetm.have_memory_barrier ())
6455 emit_insn (targetm.gen_memory_barrier ());
6456 else if (synchronize_libfunc != NULL_RTX)
6457 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode);
6458 else
6459 expand_memory_blockage ();
6462 /* Emit a signal fence with given memory model. */
6464 void
6465 expand_mem_signal_fence (enum memmodel model)
6467 /* No machine barrier is required to implement a signal fence, but
6468 a compiler memory barrier must be issued, except for relaxed MM. */
6469 if (!is_mm_relaxed (model))
6470 expand_memory_blockage ();
6473 /* This function expands the atomic load operation:
6474 return the atomically loaded value in MEM.
6476 MEMMODEL is the memory model variant to use.
6477 TARGET is an option place to stick the return value. */
6480 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
6482 machine_mode mode = GET_MODE (mem);
6483 enum insn_code icode;
6485 /* If the target supports the load directly, great. */
6486 icode = direct_optab_handler (atomic_load_optab, mode);
6487 if (icode != CODE_FOR_nothing)
6489 struct expand_operand ops[3];
6490 rtx_insn *last = get_last_insn ();
6491 if (is_mm_seq_cst (model))
6492 expand_memory_blockage ();
6494 create_output_operand (&ops[0], target, mode);
6495 create_fixed_operand (&ops[1], mem);
6496 create_integer_operand (&ops[2], model);
6497 if (maybe_expand_insn (icode, 3, ops))
6499 if (!is_mm_relaxed (model))
6500 expand_memory_blockage ();
6501 return ops[0].value;
6503 delete_insns_since (last);
6506 /* If the size of the object is greater than word size on this target,
6507 then we assume that a load will not be atomic. We could try to
6508 emulate a load with a compare-and-swap operation, but the store that
6509 doing this could result in would be incorrect if this is a volatile
6510 atomic load or targetting read-only-mapped memory. */
6511 if (maybe_gt (GET_MODE_PRECISION (mode), BITS_PER_WORD))
6512 /* If there is no atomic load, leave the library call. */
6513 return NULL_RTX;
6515 /* Otherwise assume loads are atomic, and emit the proper barriers. */
6516 if (!target || target == const0_rtx)
6517 target = gen_reg_rtx (mode);
6519 /* For SEQ_CST, emit a barrier before the load. */
6520 if (is_mm_seq_cst (model))
6521 expand_mem_thread_fence (model);
6523 emit_move_insn (target, mem);
6525 /* Emit the appropriate barrier after the load. */
6526 expand_mem_thread_fence (model);
6528 return target;
6531 /* This function expands the atomic store operation:
6532 Atomically store VAL in MEM.
6533 MEMMODEL is the memory model variant to use.
6534 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
6535 function returns const0_rtx if a pattern was emitted. */
6538 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
6540 machine_mode mode = GET_MODE (mem);
6541 enum insn_code icode;
6542 struct expand_operand ops[3];
6544 /* If the target supports the store directly, great. */
6545 icode = direct_optab_handler (atomic_store_optab, mode);
6546 if (icode != CODE_FOR_nothing)
6548 rtx_insn *last = get_last_insn ();
6549 if (!is_mm_relaxed (model))
6550 expand_memory_blockage ();
6551 create_fixed_operand (&ops[0], mem);
6552 create_input_operand (&ops[1], val, mode);
6553 create_integer_operand (&ops[2], model);
6554 if (maybe_expand_insn (icode, 3, ops))
6556 if (is_mm_seq_cst (model))
6557 expand_memory_blockage ();
6558 return const0_rtx;
6560 delete_insns_since (last);
6563 /* If using __sync_lock_release is a viable alternative, try it.
6564 Note that this will not be set to true if we are expanding a generic
6565 __atomic_store_n. */
6566 if (use_release)
6568 icode = direct_optab_handler (sync_lock_release_optab, mode);
6569 if (icode != CODE_FOR_nothing)
6571 create_fixed_operand (&ops[0], mem);
6572 create_input_operand (&ops[1], const0_rtx, mode);
6573 if (maybe_expand_insn (icode, 2, ops))
6575 /* lock_release is only a release barrier. */
6576 if (is_mm_seq_cst (model))
6577 expand_mem_thread_fence (model);
6578 return const0_rtx;
6583 /* If the size of the object is greater than word size on this target,
6584 a default store will not be atomic. */
6585 if (maybe_gt (GET_MODE_PRECISION (mode), BITS_PER_WORD))
6587 /* If loads are atomic or we are called to provide a __sync builtin,
6588 we can try a atomic_exchange and throw away the result. Otherwise,
6589 don't do anything so that we do not create an inconsistency between
6590 loads and stores. */
6591 if (can_atomic_load_p (mode) || is_mm_sync (model))
6593 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
6594 if (!target)
6595 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem,
6596 val);
6597 if (target)
6598 return const0_rtx;
6600 return NULL_RTX;
6603 /* Otherwise assume stores are atomic, and emit the proper barriers. */
6604 expand_mem_thread_fence (model);
6606 emit_move_insn (mem, val);
6608 /* For SEQ_CST, also emit a barrier after the store. */
6609 if (is_mm_seq_cst (model))
6610 expand_mem_thread_fence (model);
6612 return const0_rtx;
6616 /* Structure containing the pointers and values required to process the
6617 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
6619 struct atomic_op_functions
6621 direct_optab mem_fetch_before;
6622 direct_optab mem_fetch_after;
6623 direct_optab mem_no_result;
6624 optab fetch_before;
6625 optab fetch_after;
6626 direct_optab no_result;
6627 enum rtx_code reverse_code;
6631 /* Fill in structure pointed to by OP with the various optab entries for an
6632 operation of type CODE. */
6634 static void
6635 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
6637 gcc_assert (op!= NULL);
6639 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
6640 in the source code during compilation, and the optab entries are not
6641 computable until runtime. Fill in the values at runtime. */
6642 switch (code)
6644 case PLUS:
6645 op->mem_fetch_before = atomic_fetch_add_optab;
6646 op->mem_fetch_after = atomic_add_fetch_optab;
6647 op->mem_no_result = atomic_add_optab;
6648 op->fetch_before = sync_old_add_optab;
6649 op->fetch_after = sync_new_add_optab;
6650 op->no_result = sync_add_optab;
6651 op->reverse_code = MINUS;
6652 break;
6653 case MINUS:
6654 op->mem_fetch_before = atomic_fetch_sub_optab;
6655 op->mem_fetch_after = atomic_sub_fetch_optab;
6656 op->mem_no_result = atomic_sub_optab;
6657 op->fetch_before = sync_old_sub_optab;
6658 op->fetch_after = sync_new_sub_optab;
6659 op->no_result = sync_sub_optab;
6660 op->reverse_code = PLUS;
6661 break;
6662 case XOR:
6663 op->mem_fetch_before = atomic_fetch_xor_optab;
6664 op->mem_fetch_after = atomic_xor_fetch_optab;
6665 op->mem_no_result = atomic_xor_optab;
6666 op->fetch_before = sync_old_xor_optab;
6667 op->fetch_after = sync_new_xor_optab;
6668 op->no_result = sync_xor_optab;
6669 op->reverse_code = XOR;
6670 break;
6671 case AND:
6672 op->mem_fetch_before = atomic_fetch_and_optab;
6673 op->mem_fetch_after = atomic_and_fetch_optab;
6674 op->mem_no_result = atomic_and_optab;
6675 op->fetch_before = sync_old_and_optab;
6676 op->fetch_after = sync_new_and_optab;
6677 op->no_result = sync_and_optab;
6678 op->reverse_code = UNKNOWN;
6679 break;
6680 case IOR:
6681 op->mem_fetch_before = atomic_fetch_or_optab;
6682 op->mem_fetch_after = atomic_or_fetch_optab;
6683 op->mem_no_result = atomic_or_optab;
6684 op->fetch_before = sync_old_ior_optab;
6685 op->fetch_after = sync_new_ior_optab;
6686 op->no_result = sync_ior_optab;
6687 op->reverse_code = UNKNOWN;
6688 break;
6689 case NOT:
6690 op->mem_fetch_before = atomic_fetch_nand_optab;
6691 op->mem_fetch_after = atomic_nand_fetch_optab;
6692 op->mem_no_result = atomic_nand_optab;
6693 op->fetch_before = sync_old_nand_optab;
6694 op->fetch_after = sync_new_nand_optab;
6695 op->no_result = sync_nand_optab;
6696 op->reverse_code = UNKNOWN;
6697 break;
6698 default:
6699 gcc_unreachable ();
6703 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
6704 using memory order MODEL. If AFTER is true the operation needs to return
6705 the value of *MEM after the operation, otherwise the previous value.
6706 TARGET is an optional place to place the result. The result is unused if
6707 it is const0_rtx.
6708 Return the result if there is a better sequence, otherwise NULL_RTX. */
6710 static rtx
6711 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
6712 enum memmodel model, bool after)
6714 /* If the value is prefetched, or not used, it may be possible to replace
6715 the sequence with a native exchange operation. */
6716 if (!after || target == const0_rtx)
6718 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
6719 if (code == AND && val == const0_rtx)
6721 if (target == const0_rtx)
6722 target = gen_reg_rtx (GET_MODE (mem));
6723 return maybe_emit_atomic_exchange (target, mem, val, model);
6726 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
6727 if (code == IOR && val == constm1_rtx)
6729 if (target == const0_rtx)
6730 target = gen_reg_rtx (GET_MODE (mem));
6731 return maybe_emit_atomic_exchange (target, mem, val, model);
6735 return NULL_RTX;
6738 /* Try to emit an instruction for a specific operation varaition.
6739 OPTAB contains the OP functions.
6740 TARGET is an optional place to return the result. const0_rtx means unused.
6741 MEM is the memory location to operate on.
6742 VAL is the value to use in the operation.
6743 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
6744 MODEL is the memory model, if used.
6745 AFTER is true if the returned result is the value after the operation. */
6747 static rtx
6748 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
6749 rtx val, bool use_memmodel, enum memmodel model, bool after)
6751 machine_mode mode = GET_MODE (mem);
6752 struct expand_operand ops[4];
6753 enum insn_code icode;
6754 int op_counter = 0;
6755 int num_ops;
6757 /* Check to see if there is a result returned. */
6758 if (target == const0_rtx)
6760 if (use_memmodel)
6762 icode = direct_optab_handler (optab->mem_no_result, mode);
6763 create_integer_operand (&ops[2], model);
6764 num_ops = 3;
6766 else
6768 icode = direct_optab_handler (optab->no_result, mode);
6769 num_ops = 2;
6772 /* Otherwise, we need to generate a result. */
6773 else
6775 if (use_memmodel)
6777 icode = direct_optab_handler (after ? optab->mem_fetch_after
6778 : optab->mem_fetch_before, mode);
6779 create_integer_operand (&ops[3], model);
6780 num_ops = 4;
6782 else
6784 icode = optab_handler (after ? optab->fetch_after
6785 : optab->fetch_before, mode);
6786 num_ops = 3;
6788 create_output_operand (&ops[op_counter++], target, mode);
6790 if (icode == CODE_FOR_nothing)
6791 return NULL_RTX;
6793 create_fixed_operand (&ops[op_counter++], mem);
6794 /* VAL may have been promoted to a wider mode. Shrink it if so. */
6795 create_convert_operand_to (&ops[op_counter++], val, mode, true);
6797 if (maybe_expand_insn (icode, num_ops, ops))
6798 return (target == const0_rtx ? const0_rtx : ops[0].value);
6800 return NULL_RTX;
6804 /* This function expands an atomic fetch_OP or OP_fetch operation:
6805 TARGET is an option place to stick the return value. const0_rtx indicates
6806 the result is unused.
6807 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6808 CODE is the operation being performed (OP)
6809 MEMMODEL is the memory model variant to use.
6810 AFTER is true to return the result of the operation (OP_fetch).
6811 AFTER is false to return the value before the operation (fetch_OP).
6813 This function will *only* generate instructions if there is a direct
6814 optab. No compare and swap loops or libcalls will be generated. */
6816 static rtx
6817 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
6818 enum rtx_code code, enum memmodel model,
6819 bool after)
6821 machine_mode mode = GET_MODE (mem);
6822 struct atomic_op_functions optab;
6823 rtx result;
6824 bool unused_result = (target == const0_rtx);
6826 get_atomic_op_for_code (&optab, code);
6828 /* Check to see if there are any better instructions. */
6829 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
6830 if (result)
6831 return result;
6833 /* Check for the case where the result isn't used and try those patterns. */
6834 if (unused_result)
6836 /* Try the memory model variant first. */
6837 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
6838 if (result)
6839 return result;
6841 /* Next try the old style withuot a memory model. */
6842 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
6843 if (result)
6844 return result;
6846 /* There is no no-result pattern, so try patterns with a result. */
6847 target = NULL_RTX;
6850 /* Try the __atomic version. */
6851 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
6852 if (result)
6853 return result;
6855 /* Try the older __sync version. */
6856 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
6857 if (result)
6858 return result;
6860 /* If the fetch value can be calculated from the other variation of fetch,
6861 try that operation. */
6862 if (after || unused_result || optab.reverse_code != UNKNOWN)
6864 /* Try the __atomic version, then the older __sync version. */
6865 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
6866 if (!result)
6867 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
6869 if (result)
6871 /* If the result isn't used, no need to do compensation code. */
6872 if (unused_result)
6873 return result;
6875 /* Issue compensation code. Fetch_after == fetch_before OP val.
6876 Fetch_before == after REVERSE_OP val. */
6877 if (!after)
6878 code = optab.reverse_code;
6879 if (code == NOT)
6881 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
6882 true, OPTAB_LIB_WIDEN);
6883 result = expand_simple_unop (mode, NOT, result, target, true);
6885 else
6886 result = expand_simple_binop (mode, code, result, val, target,
6887 true, OPTAB_LIB_WIDEN);
6888 return result;
6892 /* No direct opcode can be generated. */
6893 return NULL_RTX;
6898 /* This function expands an atomic fetch_OP or OP_fetch operation:
6899 TARGET is an option place to stick the return value. const0_rtx indicates
6900 the result is unused.
6901 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6902 CODE is the operation being performed (OP)
6903 MEMMODEL is the memory model variant to use.
6904 AFTER is true to return the result of the operation (OP_fetch).
6905 AFTER is false to return the value before the operation (fetch_OP). */
6907 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
6908 enum memmodel model, bool after)
6910 machine_mode mode = GET_MODE (mem);
6911 rtx result;
6912 bool unused_result = (target == const0_rtx);
6914 /* If loads are not atomic for the required size and we are not called to
6915 provide a __sync builtin, do not do anything so that we stay consistent
6916 with atomic loads of the same size. */
6917 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6918 return NULL_RTX;
6920 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
6921 after);
6923 if (result)
6924 return result;
6926 /* Add/sub can be implemented by doing the reverse operation with -(val). */
6927 if (code == PLUS || code == MINUS)
6929 rtx tmp;
6930 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
6932 start_sequence ();
6933 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
6934 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
6935 model, after);
6936 if (result)
6938 /* PLUS worked so emit the insns and return. */
6939 tmp = get_insns ();
6940 end_sequence ();
6941 emit_insn (tmp);
6942 return result;
6945 /* PLUS did not work, so throw away the negation code and continue. */
6946 end_sequence ();
6949 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
6950 if (!can_compare_and_swap_p (mode, false))
6952 rtx libfunc;
6953 bool fixup = false;
6954 enum rtx_code orig_code = code;
6955 struct atomic_op_functions optab;
6957 get_atomic_op_for_code (&optab, code);
6958 libfunc = optab_libfunc (after ? optab.fetch_after
6959 : optab.fetch_before, mode);
6960 if (libfunc == NULL
6961 && (after || unused_result || optab.reverse_code != UNKNOWN))
6963 fixup = true;
6964 if (!after)
6965 code = optab.reverse_code;
6966 libfunc = optab_libfunc (after ? optab.fetch_before
6967 : optab.fetch_after, mode);
6969 if (libfunc != NULL)
6971 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6972 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
6973 addr, ptr_mode, val, mode);
6975 if (!unused_result && fixup)
6976 result = expand_simple_binop (mode, code, result, val, target,
6977 true, OPTAB_LIB_WIDEN);
6978 return result;
6981 /* We need the original code for any further attempts. */
6982 code = orig_code;
6985 /* If nothing else has succeeded, default to a compare and swap loop. */
6986 if (can_compare_and_swap_p (mode, true))
6988 rtx_insn *insn;
6989 rtx t0 = gen_reg_rtx (mode), t1;
6991 start_sequence ();
6993 /* If the result is used, get a register for it. */
6994 if (!unused_result)
6996 if (!target || !register_operand (target, mode))
6997 target = gen_reg_rtx (mode);
6998 /* If fetch_before, copy the value now. */
6999 if (!after)
7000 emit_move_insn (target, t0);
7002 else
7003 target = const0_rtx;
7005 t1 = t0;
7006 if (code == NOT)
7008 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
7009 true, OPTAB_LIB_WIDEN);
7010 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
7012 else
7013 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
7014 OPTAB_LIB_WIDEN);
7016 /* For after, copy the value now. */
7017 if (!unused_result && after)
7018 emit_move_insn (target, t1);
7019 insn = get_insns ();
7020 end_sequence ();
7022 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7023 return target;
7026 return NULL_RTX;
7029 /* Return true if OPERAND is suitable for operand number OPNO of
7030 instruction ICODE. */
7032 bool
7033 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
7035 return (!insn_data[(int) icode].operand[opno].predicate
7036 || (insn_data[(int) icode].operand[opno].predicate
7037 (operand, insn_data[(int) icode].operand[opno].mode)));
7040 /* TARGET is a target of a multiword operation that we are going to
7041 implement as a series of word-mode operations. Return true if
7042 TARGET is suitable for this purpose. */
7044 bool
7045 valid_multiword_target_p (rtx target)
7047 machine_mode mode;
7048 int i, size;
7050 mode = GET_MODE (target);
7051 if (!GET_MODE_SIZE (mode).is_constant (&size))
7052 return false;
7053 for (i = 0; i < size; i += UNITS_PER_WORD)
7054 if (!validate_subreg (word_mode, mode, target, i))
7055 return false;
7056 return true;
7059 /* Make OP describe an input operand that has value INTVAL and that has
7060 no inherent mode. This function should only be used for operands that
7061 are always expand-time constants. The backend may request that INTVAL
7062 be copied into a different kind of rtx, but it must specify the mode
7063 of that rtx if so. */
7065 void
7066 create_integer_operand (struct expand_operand *op, poly_int64 intval)
7068 create_expand_operand (op, EXPAND_INTEGER,
7069 gen_int_mode (intval, MAX_MODE_INT),
7070 VOIDmode, false, intval);
7073 /* Like maybe_legitimize_operand, but do not change the code of the
7074 current rtx value. */
7076 static bool
7077 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
7078 struct expand_operand *op)
7080 /* See if the operand matches in its current form. */
7081 if (insn_operand_matches (icode, opno, op->value))
7082 return true;
7084 /* If the operand is a memory whose address has no side effects,
7085 try forcing the address into a non-virtual pseudo register.
7086 The check for side effects is important because copy_to_mode_reg
7087 cannot handle things like auto-modified addresses. */
7088 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
7090 rtx addr, mem;
7092 mem = op->value;
7093 addr = XEXP (mem, 0);
7094 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
7095 && !side_effects_p (addr))
7097 rtx_insn *last;
7098 machine_mode mode;
7100 last = get_last_insn ();
7101 mode = get_address_mode (mem);
7102 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
7103 if (insn_operand_matches (icode, opno, mem))
7105 op->value = mem;
7106 return true;
7108 delete_insns_since (last);
7112 return false;
7115 /* Try to make OP match operand OPNO of instruction ICODE. Return true
7116 on success, storing the new operand value back in OP. */
7118 static bool
7119 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
7120 struct expand_operand *op)
7122 machine_mode mode, imode;
7123 bool old_volatile_ok, result;
7125 mode = op->mode;
7126 switch (op->type)
7128 case EXPAND_FIXED:
7129 old_volatile_ok = volatile_ok;
7130 volatile_ok = true;
7131 result = maybe_legitimize_operand_same_code (icode, opno, op);
7132 volatile_ok = old_volatile_ok;
7133 return result;
7135 case EXPAND_OUTPUT:
7136 gcc_assert (mode != VOIDmode);
7137 if (op->value
7138 && op->value != const0_rtx
7139 && GET_MODE (op->value) == mode
7140 && maybe_legitimize_operand_same_code (icode, opno, op))
7141 return true;
7143 op->value = gen_reg_rtx (mode);
7144 op->target = 0;
7145 break;
7147 case EXPAND_INPUT:
7148 input:
7149 gcc_assert (mode != VOIDmode);
7150 gcc_assert (GET_MODE (op->value) == VOIDmode
7151 || GET_MODE (op->value) == mode);
7152 if (maybe_legitimize_operand_same_code (icode, opno, op))
7153 return true;
7155 op->value = copy_to_mode_reg (mode, op->value);
7156 break;
7158 case EXPAND_CONVERT_TO:
7159 gcc_assert (mode != VOIDmode);
7160 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
7161 goto input;
7163 case EXPAND_CONVERT_FROM:
7164 if (GET_MODE (op->value) != VOIDmode)
7165 mode = GET_MODE (op->value);
7166 else
7167 /* The caller must tell us what mode this value has. */
7168 gcc_assert (mode != VOIDmode);
7170 imode = insn_data[(int) icode].operand[opno].mode;
7171 if (imode != VOIDmode && imode != mode)
7173 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
7174 mode = imode;
7176 goto input;
7178 case EXPAND_ADDRESS:
7179 op->value = convert_memory_address (as_a <scalar_int_mode> (mode),
7180 op->value);
7181 goto input;
7183 case EXPAND_INTEGER:
7184 mode = insn_data[(int) icode].operand[opno].mode;
7185 if (mode != VOIDmode
7186 && known_eq (trunc_int_for_mode (op->int_value, mode),
7187 op->int_value))
7189 op->value = gen_int_mode (op->int_value, mode);
7190 goto input;
7192 break;
7194 return insn_operand_matches (icode, opno, op->value);
7197 /* Make OP describe an input operand that should have the same value
7198 as VALUE, after any mode conversion that the target might request.
7199 TYPE is the type of VALUE. */
7201 void
7202 create_convert_operand_from_type (struct expand_operand *op,
7203 rtx value, tree type)
7205 create_convert_operand_from (op, value, TYPE_MODE (type),
7206 TYPE_UNSIGNED (type));
7209 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
7210 of instruction ICODE. Return true on success, leaving the new operand
7211 values in the OPS themselves. Emit no code on failure. */
7213 bool
7214 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
7215 unsigned int nops, struct expand_operand *ops)
7217 rtx_insn *last;
7218 unsigned int i;
7220 last = get_last_insn ();
7221 for (i = 0; i < nops; i++)
7222 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
7224 delete_insns_since (last);
7225 return false;
7227 return true;
7230 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
7231 as its operands. Return the instruction pattern on success,
7232 and emit any necessary set-up code. Return null and emit no
7233 code on failure. */
7235 rtx_insn *
7236 maybe_gen_insn (enum insn_code icode, unsigned int nops,
7237 struct expand_operand *ops)
7239 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
7240 if (!maybe_legitimize_operands (icode, 0, nops, ops))
7241 return NULL;
7243 switch (nops)
7245 case 1:
7246 return GEN_FCN (icode) (ops[0].value);
7247 case 2:
7248 return GEN_FCN (icode) (ops[0].value, ops[1].value);
7249 case 3:
7250 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
7251 case 4:
7252 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7253 ops[3].value);
7254 case 5:
7255 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7256 ops[3].value, ops[4].value);
7257 case 6:
7258 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7259 ops[3].value, ops[4].value, ops[5].value);
7260 case 7:
7261 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7262 ops[3].value, ops[4].value, ops[5].value,
7263 ops[6].value);
7264 case 8:
7265 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7266 ops[3].value, ops[4].value, ops[5].value,
7267 ops[6].value, ops[7].value);
7268 case 9:
7269 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7270 ops[3].value, ops[4].value, ops[5].value,
7271 ops[6].value, ops[7].value, ops[8].value);
7273 gcc_unreachable ();
7276 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
7277 as its operands. Return true on success and emit no code on failure. */
7279 bool
7280 maybe_expand_insn (enum insn_code icode, unsigned int nops,
7281 struct expand_operand *ops)
7283 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7284 if (pat)
7286 emit_insn (pat);
7287 return true;
7289 return false;
7292 /* Like maybe_expand_insn, but for jumps. */
7294 bool
7295 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
7296 struct expand_operand *ops)
7298 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7299 if (pat)
7301 emit_jump_insn (pat);
7302 return true;
7304 return false;
7307 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
7308 as its operands. */
7310 void
7311 expand_insn (enum insn_code icode, unsigned int nops,
7312 struct expand_operand *ops)
7314 if (!maybe_expand_insn (icode, nops, ops))
7315 gcc_unreachable ();
7318 /* Like expand_insn, but for jumps. */
7320 void
7321 expand_jump_insn (enum insn_code icode, unsigned int nops,
7322 struct expand_operand *ops)
7324 if (!maybe_expand_jump_insn (icode, nops, ops))
7325 gcc_unreachable ();