Make vect_model_store_cost take a vec_load_store_type
[official-gcc.git] / gcc / optabs.c
blobdaac5457223232dcae76565b973ec1b9bbc7e52d
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 rtx result;
3941 machine_mode ret_mode;
3943 /* Handle a libcall just for the mode we are using. */
3944 libfunc = optab_libfunc (cmp_optab, mode);
3945 gcc_assert (libfunc);
3947 /* If we want unsigned, and this mode has a distinct unsigned
3948 comparison routine, use that. */
3949 if (unsignedp)
3951 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
3952 if (ulibfunc)
3953 libfunc = ulibfunc;
3956 ret_mode = targetm.libgcc_cmp_return_mode ();
3957 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3958 ret_mode, x, mode, y, mode);
3960 /* There are two kinds of comparison routines. Biased routines
3961 return 0/1/2, and unbiased routines return -1/0/1. Other parts
3962 of gcc expect that the comparison operation is equivalent
3963 to the modified comparison. For signed comparisons compare the
3964 result against 1 in the biased case, and zero in the unbiased
3965 case. For unsigned comparisons always compare against 1 after
3966 biasing the unbiased result by adding 1. This gives us a way to
3967 represent LTU.
3968 The comparisons in the fixed-point helper library are always
3969 biased. */
3970 x = result;
3971 y = const1_rtx;
3973 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
3975 if (unsignedp)
3976 x = plus_constant (ret_mode, result, 1);
3977 else
3978 y = const0_rtx;
3981 *pmode = ret_mode;
3982 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
3983 ptest, pmode);
3985 else
3986 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
3988 return;
3990 fail:
3991 *ptest = NULL_RTX;
3994 /* Before emitting an insn with code ICODE, make sure that X, which is going
3995 to be used for operand OPNUM of the insn, is converted from mode MODE to
3996 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
3997 that it is accepted by the operand predicate. Return the new value. */
4000 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
4001 machine_mode wider_mode, int unsignedp)
4003 if (mode != wider_mode)
4004 x = convert_modes (wider_mode, mode, x, unsignedp);
4006 if (!insn_operand_matches (icode, opnum, x))
4008 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
4009 if (reload_completed)
4010 return NULL_RTX;
4011 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
4012 return NULL_RTX;
4013 x = copy_to_mode_reg (op_mode, x);
4016 return x;
4019 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4020 we can do the branch. */
4022 static void
4023 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label,
4024 profile_probability prob)
4026 machine_mode optab_mode;
4027 enum mode_class mclass;
4028 enum insn_code icode;
4029 rtx_insn *insn;
4031 mclass = GET_MODE_CLASS (mode);
4032 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4033 icode = optab_handler (cbranch_optab, optab_mode);
4035 gcc_assert (icode != CODE_FOR_nothing);
4036 gcc_assert (insn_operand_matches (icode, 0, test));
4037 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4038 XEXP (test, 1), label));
4039 if (prob.initialized_p ()
4040 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4041 && insn
4042 && JUMP_P (insn)
4043 && any_condjump_p (insn)
4044 && !find_reg_note (insn, REG_BR_PROB, 0))
4045 add_reg_br_prob_note (insn, prob);
4048 /* Generate code to compare X with Y so that the condition codes are
4049 set and to jump to LABEL if the condition is true. If X is a
4050 constant and Y is not a constant, then the comparison is swapped to
4051 ensure that the comparison RTL has the canonical form.
4053 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4054 need to be widened. UNSIGNEDP is also used to select the proper
4055 branch condition code.
4057 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4059 MODE is the mode of the inputs (in case they are const_int).
4061 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4062 It will be potentially converted into an unsigned variant based on
4063 UNSIGNEDP to select a proper jump instruction.
4065 PROB is the probability of jumping to LABEL. */
4067 void
4068 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4069 machine_mode mode, int unsignedp, rtx label,
4070 profile_probability prob)
4072 rtx op0 = x, op1 = y;
4073 rtx test;
4075 /* Swap operands and condition to ensure canonical RTL. */
4076 if (swap_commutative_operands_p (x, y)
4077 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4079 op0 = y, op1 = x;
4080 comparison = swap_condition (comparison);
4083 /* If OP0 is still a constant, then both X and Y must be constants
4084 or the opposite comparison is not supported. Force X into a register
4085 to create canonical RTL. */
4086 if (CONSTANT_P (op0))
4087 op0 = force_reg (mode, op0);
4089 if (unsignedp)
4090 comparison = unsigned_condition (comparison);
4092 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4093 &test, &mode);
4094 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4098 /* Emit a library call comparison between floating point X and Y.
4099 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4101 static void
4102 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4103 rtx *ptest, machine_mode *pmode)
4105 enum rtx_code swapped = swap_condition (comparison);
4106 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4107 machine_mode orig_mode = GET_MODE (x);
4108 machine_mode mode;
4109 rtx true_rtx, false_rtx;
4110 rtx value, target, equiv;
4111 rtx_insn *insns;
4112 rtx libfunc = 0;
4113 bool reversed_p = false;
4114 scalar_int_mode cmp_mode = targetm.libgcc_cmp_return_mode ();
4116 FOR_EACH_MODE_FROM (mode, orig_mode)
4118 if (code_to_optab (comparison)
4119 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4120 break;
4122 if (code_to_optab (swapped)
4123 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4125 std::swap (x, y);
4126 comparison = swapped;
4127 break;
4130 if (code_to_optab (reversed)
4131 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4133 comparison = reversed;
4134 reversed_p = true;
4135 break;
4139 gcc_assert (mode != VOIDmode);
4141 if (mode != orig_mode)
4143 x = convert_to_mode (mode, x, 0);
4144 y = convert_to_mode (mode, y, 0);
4147 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4148 the RTL. The allows the RTL optimizers to delete the libcall if the
4149 condition can be determined at compile-time. */
4150 if (comparison == UNORDERED
4151 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4153 true_rtx = const_true_rtx;
4154 false_rtx = const0_rtx;
4156 else
4158 switch (comparison)
4160 case EQ:
4161 true_rtx = const0_rtx;
4162 false_rtx = const_true_rtx;
4163 break;
4165 case NE:
4166 true_rtx = const_true_rtx;
4167 false_rtx = const0_rtx;
4168 break;
4170 case GT:
4171 true_rtx = const1_rtx;
4172 false_rtx = const0_rtx;
4173 break;
4175 case GE:
4176 true_rtx = const0_rtx;
4177 false_rtx = constm1_rtx;
4178 break;
4180 case LT:
4181 true_rtx = constm1_rtx;
4182 false_rtx = const0_rtx;
4183 break;
4185 case LE:
4186 true_rtx = const0_rtx;
4187 false_rtx = const1_rtx;
4188 break;
4190 default:
4191 gcc_unreachable ();
4195 if (comparison == UNORDERED)
4197 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4198 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4199 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4200 temp, const_true_rtx, equiv);
4202 else
4204 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4205 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4206 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4207 equiv, true_rtx, false_rtx);
4210 start_sequence ();
4211 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4212 cmp_mode, x, mode, y, mode);
4213 insns = get_insns ();
4214 end_sequence ();
4216 target = gen_reg_rtx (cmp_mode);
4217 emit_libcall_block (insns, target, value, equiv);
4219 if (comparison == UNORDERED
4220 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4221 || reversed_p)
4222 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4223 else
4224 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4226 *pmode = cmp_mode;
4229 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4231 void
4232 emit_indirect_jump (rtx loc)
4234 if (!targetm.have_indirect_jump ())
4235 sorry ("indirect jumps are not available on this target");
4236 else
4238 struct expand_operand ops[1];
4239 create_address_operand (&ops[0], loc);
4240 expand_jump_insn (targetm.code_for_indirect_jump, 1, ops);
4241 emit_barrier ();
4246 /* Emit a conditional move instruction if the machine supports one for that
4247 condition and machine mode.
4249 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4250 the mode to use should they be constants. If it is VOIDmode, they cannot
4251 both be constants.
4253 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4254 should be stored there. MODE is the mode to use should they be constants.
4255 If it is VOIDmode, they cannot both be constants.
4257 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4258 is not supported. */
4261 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4262 machine_mode cmode, rtx op2, rtx op3,
4263 machine_mode mode, int unsignedp)
4265 rtx comparison;
4266 rtx_insn *last;
4267 enum insn_code icode;
4268 enum rtx_code reversed;
4270 /* If the two source operands are identical, that's just a move. */
4272 if (rtx_equal_p (op2, op3))
4274 if (!target)
4275 target = gen_reg_rtx (mode);
4277 emit_move_insn (target, op3);
4278 return target;
4281 /* If one operand is constant, make it the second one. Only do this
4282 if the other operand is not constant as well. */
4284 if (swap_commutative_operands_p (op0, op1))
4286 std::swap (op0, op1);
4287 code = swap_condition (code);
4290 /* get_condition will prefer to generate LT and GT even if the old
4291 comparison was against zero, so undo that canonicalization here since
4292 comparisons against zero are cheaper. */
4293 if (code == LT && op1 == const1_rtx)
4294 code = LE, op1 = const0_rtx;
4295 else if (code == GT && op1 == constm1_rtx)
4296 code = GE, op1 = const0_rtx;
4298 if (cmode == VOIDmode)
4299 cmode = GET_MODE (op0);
4301 enum rtx_code orig_code = code;
4302 bool swapped = false;
4303 if (swap_commutative_operands_p (op2, op3)
4304 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4305 != UNKNOWN))
4307 std::swap (op2, op3);
4308 code = reversed;
4309 swapped = true;
4312 if (mode == VOIDmode)
4313 mode = GET_MODE (op2);
4315 icode = direct_optab_handler (movcc_optab, mode);
4317 if (icode == CODE_FOR_nothing)
4318 return NULL_RTX;
4320 if (!target)
4321 target = gen_reg_rtx (mode);
4323 for (int pass = 0; ; pass++)
4325 code = unsignedp ? unsigned_condition (code) : code;
4326 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4328 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4329 punt and let the caller figure out how best to deal with this
4330 situation. */
4331 if (COMPARISON_P (comparison))
4333 saved_pending_stack_adjust save;
4334 save_pending_stack_adjust (&save);
4335 last = get_last_insn ();
4336 do_pending_stack_adjust ();
4337 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4338 GET_CODE (comparison), NULL_RTX, unsignedp,
4339 OPTAB_WIDEN, &comparison, &cmode);
4340 if (comparison)
4342 struct expand_operand ops[4];
4344 create_output_operand (&ops[0], target, mode);
4345 create_fixed_operand (&ops[1], comparison);
4346 create_input_operand (&ops[2], op2, mode);
4347 create_input_operand (&ops[3], op3, mode);
4348 if (maybe_expand_insn (icode, 4, ops))
4350 if (ops[0].value != target)
4351 convert_move (target, ops[0].value, false);
4352 return target;
4355 delete_insns_since (last);
4356 restore_pending_stack_adjust (&save);
4359 if (pass == 1)
4360 return NULL_RTX;
4362 /* If the preferred op2/op3 order is not usable, retry with other
4363 operand order, perhaps it will expand successfully. */
4364 if (swapped)
4365 code = orig_code;
4366 else if ((reversed = reversed_comparison_code_parts (orig_code, op0, op1,
4367 NULL))
4368 != UNKNOWN)
4369 code = reversed;
4370 else
4371 return NULL_RTX;
4372 std::swap (op2, op3);
4377 /* Emit a conditional negate or bitwise complement using the
4378 negcc or notcc optabs if available. Return NULL_RTX if such operations
4379 are not available. Otherwise return the RTX holding the result.
4380 TARGET is the desired destination of the result. COMP is the comparison
4381 on which to negate. If COND is true move into TARGET the negation
4382 or bitwise complement of OP1. Otherwise move OP2 into TARGET.
4383 CODE is either NEG or NOT. MODE is the machine mode in which the
4384 operation is performed. */
4387 emit_conditional_neg_or_complement (rtx target, rtx_code code,
4388 machine_mode mode, rtx cond, rtx op1,
4389 rtx op2)
4391 optab op = unknown_optab;
4392 if (code == NEG)
4393 op = negcc_optab;
4394 else if (code == NOT)
4395 op = notcc_optab;
4396 else
4397 gcc_unreachable ();
4399 insn_code icode = direct_optab_handler (op, mode);
4401 if (icode == CODE_FOR_nothing)
4402 return NULL_RTX;
4404 if (!target)
4405 target = gen_reg_rtx (mode);
4407 rtx_insn *last = get_last_insn ();
4408 struct expand_operand ops[4];
4410 create_output_operand (&ops[0], target, mode);
4411 create_fixed_operand (&ops[1], cond);
4412 create_input_operand (&ops[2], op1, mode);
4413 create_input_operand (&ops[3], op2, mode);
4415 if (maybe_expand_insn (icode, 4, ops))
4417 if (ops[0].value != target)
4418 convert_move (target, ops[0].value, false);
4420 return target;
4422 delete_insns_since (last);
4423 return NULL_RTX;
4426 /* Emit a conditional addition instruction if the machine supports one for that
4427 condition and machine mode.
4429 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4430 the mode to use should they be constants. If it is VOIDmode, they cannot
4431 both be constants.
4433 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4434 should be stored there. MODE is the mode to use should they be constants.
4435 If it is VOIDmode, they cannot both be constants.
4437 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4438 is not supported. */
4441 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4442 machine_mode cmode, rtx op2, rtx op3,
4443 machine_mode mode, int unsignedp)
4445 rtx comparison;
4446 rtx_insn *last;
4447 enum insn_code icode;
4449 /* If one operand is constant, make it the second one. Only do this
4450 if the other operand is not constant as well. */
4452 if (swap_commutative_operands_p (op0, op1))
4454 std::swap (op0, op1);
4455 code = swap_condition (code);
4458 /* get_condition will prefer to generate LT and GT even if the old
4459 comparison was against zero, so undo that canonicalization here since
4460 comparisons against zero are cheaper. */
4461 if (code == LT && op1 == const1_rtx)
4462 code = LE, op1 = const0_rtx;
4463 else if (code == GT && op1 == constm1_rtx)
4464 code = GE, op1 = const0_rtx;
4466 if (cmode == VOIDmode)
4467 cmode = GET_MODE (op0);
4469 if (mode == VOIDmode)
4470 mode = GET_MODE (op2);
4472 icode = optab_handler (addcc_optab, mode);
4474 if (icode == CODE_FOR_nothing)
4475 return 0;
4477 if (!target)
4478 target = gen_reg_rtx (mode);
4480 code = unsignedp ? unsigned_condition (code) : code;
4481 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4483 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4484 return NULL and let the caller figure out how best to deal with this
4485 situation. */
4486 if (!COMPARISON_P (comparison))
4487 return NULL_RTX;
4489 do_pending_stack_adjust ();
4490 last = get_last_insn ();
4491 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4492 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4493 &comparison, &cmode);
4494 if (comparison)
4496 struct expand_operand ops[4];
4498 create_output_operand (&ops[0], target, mode);
4499 create_fixed_operand (&ops[1], comparison);
4500 create_input_operand (&ops[2], op2, mode);
4501 create_input_operand (&ops[3], op3, mode);
4502 if (maybe_expand_insn (icode, 4, ops))
4504 if (ops[0].value != target)
4505 convert_move (target, ops[0].value, false);
4506 return target;
4509 delete_insns_since (last);
4510 return NULL_RTX;
4513 /* These functions attempt to generate an insn body, rather than
4514 emitting the insn, but if the gen function already emits them, we
4515 make no attempt to turn them back into naked patterns. */
4517 /* Generate and return an insn body to add Y to X. */
4519 rtx_insn *
4520 gen_add2_insn (rtx x, rtx y)
4522 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4524 gcc_assert (insn_operand_matches (icode, 0, x));
4525 gcc_assert (insn_operand_matches (icode, 1, x));
4526 gcc_assert (insn_operand_matches (icode, 2, y));
4528 return GEN_FCN (icode) (x, x, y);
4531 /* Generate and return an insn body to add r1 and c,
4532 storing the result in r0. */
4534 rtx_insn *
4535 gen_add3_insn (rtx r0, rtx r1, rtx c)
4537 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4539 if (icode == CODE_FOR_nothing
4540 || !insn_operand_matches (icode, 0, r0)
4541 || !insn_operand_matches (icode, 1, r1)
4542 || !insn_operand_matches (icode, 2, c))
4543 return NULL;
4545 return GEN_FCN (icode) (r0, r1, c);
4549 have_add2_insn (rtx x, rtx y)
4551 enum insn_code icode;
4553 gcc_assert (GET_MODE (x) != VOIDmode);
4555 icode = optab_handler (add_optab, GET_MODE (x));
4557 if (icode == CODE_FOR_nothing)
4558 return 0;
4560 if (!insn_operand_matches (icode, 0, x)
4561 || !insn_operand_matches (icode, 1, x)
4562 || !insn_operand_matches (icode, 2, y))
4563 return 0;
4565 return 1;
4568 /* Generate and return an insn body to add Y to X. */
4570 rtx_insn *
4571 gen_addptr3_insn (rtx x, rtx y, rtx z)
4573 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4575 gcc_assert (insn_operand_matches (icode, 0, x));
4576 gcc_assert (insn_operand_matches (icode, 1, y));
4577 gcc_assert (insn_operand_matches (icode, 2, z));
4579 return GEN_FCN (icode) (x, y, z);
4582 /* Return true if the target implements an addptr pattern and X, Y,
4583 and Z are valid for the pattern predicates. */
4586 have_addptr3_insn (rtx x, rtx y, rtx z)
4588 enum insn_code icode;
4590 gcc_assert (GET_MODE (x) != VOIDmode);
4592 icode = optab_handler (addptr3_optab, GET_MODE (x));
4594 if (icode == CODE_FOR_nothing)
4595 return 0;
4597 if (!insn_operand_matches (icode, 0, x)
4598 || !insn_operand_matches (icode, 1, y)
4599 || !insn_operand_matches (icode, 2, z))
4600 return 0;
4602 return 1;
4605 /* Generate and return an insn body to subtract Y from X. */
4607 rtx_insn *
4608 gen_sub2_insn (rtx x, rtx y)
4610 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4612 gcc_assert (insn_operand_matches (icode, 0, x));
4613 gcc_assert (insn_operand_matches (icode, 1, x));
4614 gcc_assert (insn_operand_matches (icode, 2, y));
4616 return GEN_FCN (icode) (x, x, y);
4619 /* Generate and return an insn body to subtract r1 and c,
4620 storing the result in r0. */
4622 rtx_insn *
4623 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4625 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4627 if (icode == CODE_FOR_nothing
4628 || !insn_operand_matches (icode, 0, r0)
4629 || !insn_operand_matches (icode, 1, r1)
4630 || !insn_operand_matches (icode, 2, c))
4631 return NULL;
4633 return GEN_FCN (icode) (r0, r1, c);
4637 have_sub2_insn (rtx x, rtx y)
4639 enum insn_code icode;
4641 gcc_assert (GET_MODE (x) != VOIDmode);
4643 icode = optab_handler (sub_optab, GET_MODE (x));
4645 if (icode == CODE_FOR_nothing)
4646 return 0;
4648 if (!insn_operand_matches (icode, 0, x)
4649 || !insn_operand_matches (icode, 1, x)
4650 || !insn_operand_matches (icode, 2, y))
4651 return 0;
4653 return 1;
4656 /* Generate the body of an insn to extend Y (with mode MFROM)
4657 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4659 rtx_insn *
4660 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4661 machine_mode mfrom, int unsignedp)
4663 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4664 return GEN_FCN (icode) (x, y);
4667 /* Generate code to convert FROM to floating point
4668 and store in TO. FROM must be fixed point and not VOIDmode.
4669 UNSIGNEDP nonzero means regard FROM as unsigned.
4670 Normally this is done by correcting the final value
4671 if it is negative. */
4673 void
4674 expand_float (rtx to, rtx from, int unsignedp)
4676 enum insn_code icode;
4677 rtx target = to;
4678 scalar_mode from_mode, to_mode;
4679 machine_mode fmode, imode;
4680 bool can_do_signed = false;
4682 /* Crash now, because we won't be able to decide which mode to use. */
4683 gcc_assert (GET_MODE (from) != VOIDmode);
4685 /* Look for an insn to do the conversion. Do it in the specified
4686 modes if possible; otherwise convert either input, output or both to
4687 wider mode. If the integer mode is wider than the mode of FROM,
4688 we can do the conversion signed even if the input is unsigned. */
4690 FOR_EACH_MODE_FROM (fmode, GET_MODE (to))
4691 FOR_EACH_MODE_FROM (imode, GET_MODE (from))
4693 int doing_unsigned = unsignedp;
4695 if (fmode != GET_MODE (to)
4696 && (significand_size (fmode)
4697 < GET_MODE_UNIT_PRECISION (GET_MODE (from))))
4698 continue;
4700 icode = can_float_p (fmode, imode, unsignedp);
4701 if (icode == CODE_FOR_nothing && unsignedp)
4703 enum insn_code scode = can_float_p (fmode, imode, 0);
4704 if (scode != CODE_FOR_nothing)
4705 can_do_signed = true;
4706 if (imode != GET_MODE (from))
4707 icode = scode, doing_unsigned = 0;
4710 if (icode != CODE_FOR_nothing)
4712 if (imode != GET_MODE (from))
4713 from = convert_to_mode (imode, from, unsignedp);
4715 if (fmode != GET_MODE (to))
4716 target = gen_reg_rtx (fmode);
4718 emit_unop_insn (icode, target, from,
4719 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4721 if (target != to)
4722 convert_move (to, target, 0);
4723 return;
4727 /* Unsigned integer, and no way to convert directly. Convert as signed,
4728 then unconditionally adjust the result. */
4729 if (unsignedp
4730 && can_do_signed
4731 && is_a <scalar_mode> (GET_MODE (to), &to_mode)
4732 && is_a <scalar_mode> (GET_MODE (from), &from_mode))
4734 opt_scalar_mode fmode_iter;
4735 rtx_code_label *label = gen_label_rtx ();
4736 rtx temp;
4737 REAL_VALUE_TYPE offset;
4739 /* Look for a usable floating mode FMODE wider than the source and at
4740 least as wide as the target. Using FMODE will avoid rounding woes
4741 with unsigned values greater than the signed maximum value. */
4743 FOR_EACH_MODE_FROM (fmode_iter, to_mode)
4745 scalar_mode fmode = fmode_iter.require ();
4746 if (GET_MODE_PRECISION (from_mode) < GET_MODE_BITSIZE (fmode)
4747 && can_float_p (fmode, from_mode, 0) != CODE_FOR_nothing)
4748 break;
4751 if (!fmode_iter.exists (&fmode))
4753 /* There is no such mode. Pretend the target is wide enough. */
4754 fmode = to_mode;
4756 /* Avoid double-rounding when TO is narrower than FROM. */
4757 if ((significand_size (fmode) + 1)
4758 < GET_MODE_PRECISION (from_mode))
4760 rtx temp1;
4761 rtx_code_label *neglabel = gen_label_rtx ();
4763 /* Don't use TARGET if it isn't a register, is a hard register,
4764 or is the wrong mode. */
4765 if (!REG_P (target)
4766 || REGNO (target) < FIRST_PSEUDO_REGISTER
4767 || GET_MODE (target) != fmode)
4768 target = gen_reg_rtx (fmode);
4770 imode = from_mode;
4771 do_pending_stack_adjust ();
4773 /* Test whether the sign bit is set. */
4774 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4775 0, neglabel);
4777 /* The sign bit is not set. Convert as signed. */
4778 expand_float (target, from, 0);
4779 emit_jump_insn (targetm.gen_jump (label));
4780 emit_barrier ();
4782 /* The sign bit is set.
4783 Convert to a usable (positive signed) value by shifting right
4784 one bit, while remembering if a nonzero bit was shifted
4785 out; i.e., compute (from & 1) | (from >> 1). */
4787 emit_label (neglabel);
4788 temp = expand_binop (imode, and_optab, from, const1_rtx,
4789 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4790 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
4791 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4792 OPTAB_LIB_WIDEN);
4793 expand_float (target, temp, 0);
4795 /* Multiply by 2 to undo the shift above. */
4796 temp = expand_binop (fmode, add_optab, target, target,
4797 target, 0, OPTAB_LIB_WIDEN);
4798 if (temp != target)
4799 emit_move_insn (target, temp);
4801 do_pending_stack_adjust ();
4802 emit_label (label);
4803 goto done;
4807 /* If we are about to do some arithmetic to correct for an
4808 unsigned operand, do it in a pseudo-register. */
4810 if (to_mode != fmode
4811 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
4812 target = gen_reg_rtx (fmode);
4814 /* Convert as signed integer to floating. */
4815 expand_float (target, from, 0);
4817 /* If FROM is negative (and therefore TO is negative),
4818 correct its value by 2**bitwidth. */
4820 do_pending_stack_adjust ();
4821 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, from_mode,
4822 0, label);
4825 real_2expN (&offset, GET_MODE_PRECISION (from_mode), fmode);
4826 temp = expand_binop (fmode, add_optab, target,
4827 const_double_from_real_value (offset, fmode),
4828 target, 0, OPTAB_LIB_WIDEN);
4829 if (temp != target)
4830 emit_move_insn (target, temp);
4832 do_pending_stack_adjust ();
4833 emit_label (label);
4834 goto done;
4837 /* No hardware instruction available; call a library routine. */
4839 rtx libfunc;
4840 rtx_insn *insns;
4841 rtx value;
4842 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
4844 if (is_narrower_int_mode (GET_MODE (from), SImode))
4845 from = convert_to_mode (SImode, from, unsignedp);
4847 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
4848 gcc_assert (libfunc);
4850 start_sequence ();
4852 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4853 GET_MODE (to), from, GET_MODE (from));
4854 insns = get_insns ();
4855 end_sequence ();
4857 emit_libcall_block (insns, target, value,
4858 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
4859 GET_MODE (to), from));
4862 done:
4864 /* Copy result to requested destination
4865 if we have been computing in a temp location. */
4867 if (target != to)
4869 if (GET_MODE (target) == GET_MODE (to))
4870 emit_move_insn (to, target);
4871 else
4872 convert_move (to, target, 0);
4876 /* Generate code to convert FROM to fixed point and store in TO. FROM
4877 must be floating point. */
4879 void
4880 expand_fix (rtx to, rtx from, int unsignedp)
4882 enum insn_code icode;
4883 rtx target = to;
4884 machine_mode fmode, imode;
4885 opt_scalar_mode fmode_iter;
4886 bool must_trunc = false;
4888 /* We first try to find a pair of modes, one real and one integer, at
4889 least as wide as FROM and TO, respectively, in which we can open-code
4890 this conversion. If the integer mode is wider than the mode of TO,
4891 we can do the conversion either signed or unsigned. */
4893 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
4894 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
4896 int doing_unsigned = unsignedp;
4898 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
4899 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
4900 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
4902 if (icode != CODE_FOR_nothing)
4904 rtx_insn *last = get_last_insn ();
4905 if (fmode != GET_MODE (from))
4906 from = convert_to_mode (fmode, from, 0);
4908 if (must_trunc)
4910 rtx temp = gen_reg_rtx (GET_MODE (from));
4911 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
4912 temp, 0);
4915 if (imode != GET_MODE (to))
4916 target = gen_reg_rtx (imode);
4918 if (maybe_emit_unop_insn (icode, target, from,
4919 doing_unsigned ? UNSIGNED_FIX : FIX))
4921 if (target != to)
4922 convert_move (to, target, unsignedp);
4923 return;
4925 delete_insns_since (last);
4929 /* For an unsigned conversion, there is one more way to do it.
4930 If we have a signed conversion, we generate code that compares
4931 the real value to the largest representable positive number. If if
4932 is smaller, the conversion is done normally. Otherwise, subtract
4933 one plus the highest signed number, convert, and add it back.
4935 We only need to check all real modes, since we know we didn't find
4936 anything with a wider integer mode.
4938 This code used to extend FP value into mode wider than the destination.
4939 This is needed for decimal float modes which cannot accurately
4940 represent one plus the highest signed number of the same size, but
4941 not for binary modes. Consider, for instance conversion from SFmode
4942 into DImode.
4944 The hot path through the code is dealing with inputs smaller than 2^63
4945 and doing just the conversion, so there is no bits to lose.
4947 In the other path we know the value is positive in the range 2^63..2^64-1
4948 inclusive. (as for other input overflow happens and result is undefined)
4949 So we know that the most important bit set in mantissa corresponds to
4950 2^63. The subtraction of 2^63 should not generate any rounding as it
4951 simply clears out that bit. The rest is trivial. */
4953 scalar_int_mode to_mode;
4954 if (unsignedp
4955 && is_a <scalar_int_mode> (GET_MODE (to), &to_mode)
4956 && HWI_COMPUTABLE_MODE_P (to_mode))
4957 FOR_EACH_MODE_FROM (fmode_iter, as_a <scalar_mode> (GET_MODE (from)))
4959 scalar_mode fmode = fmode_iter.require ();
4960 if (CODE_FOR_nothing != can_fix_p (to_mode, fmode,
4961 0, &must_trunc)
4962 && (!DECIMAL_FLOAT_MODE_P (fmode)
4963 || (GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (to_mode))))
4965 int bitsize;
4966 REAL_VALUE_TYPE offset;
4967 rtx limit;
4968 rtx_code_label *lab1, *lab2;
4969 rtx_insn *insn;
4971 bitsize = GET_MODE_PRECISION (to_mode);
4972 real_2expN (&offset, bitsize - 1, fmode);
4973 limit = const_double_from_real_value (offset, fmode);
4974 lab1 = gen_label_rtx ();
4975 lab2 = gen_label_rtx ();
4977 if (fmode != GET_MODE (from))
4978 from = convert_to_mode (fmode, from, 0);
4980 /* See if we need to do the subtraction. */
4981 do_pending_stack_adjust ();
4982 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX,
4983 GET_MODE (from), 0, lab1);
4985 /* If not, do the signed "fix" and branch around fixup code. */
4986 expand_fix (to, from, 0);
4987 emit_jump_insn (targetm.gen_jump (lab2));
4988 emit_barrier ();
4990 /* Otherwise, subtract 2**(N-1), convert to signed number,
4991 then add 2**(N-1). Do the addition using XOR since this
4992 will often generate better code. */
4993 emit_label (lab1);
4994 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
4995 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4996 expand_fix (to, target, 0);
4997 target = expand_binop (to_mode, xor_optab, to,
4998 gen_int_mode
4999 (HOST_WIDE_INT_1 << (bitsize - 1),
5000 to_mode),
5001 to, 1, OPTAB_LIB_WIDEN);
5003 if (target != to)
5004 emit_move_insn (to, target);
5006 emit_label (lab2);
5008 if (optab_handler (mov_optab, to_mode) != CODE_FOR_nothing)
5010 /* Make a place for a REG_NOTE and add it. */
5011 insn = emit_move_insn (to, to);
5012 set_dst_reg_note (insn, REG_EQUAL,
5013 gen_rtx_fmt_e (UNSIGNED_FIX, to_mode,
5014 copy_rtx (from)),
5015 to);
5018 return;
5022 /* We can't do it with an insn, so use a library call. But first ensure
5023 that the mode of TO is at least as wide as SImode, since those are the
5024 only library calls we know about. */
5026 if (is_narrower_int_mode (GET_MODE (to), SImode))
5028 target = gen_reg_rtx (SImode);
5030 expand_fix (target, from, unsignedp);
5032 else
5034 rtx_insn *insns;
5035 rtx value;
5036 rtx libfunc;
5038 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5039 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5040 gcc_assert (libfunc);
5042 start_sequence ();
5044 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5045 GET_MODE (to), from, GET_MODE (from));
5046 insns = get_insns ();
5047 end_sequence ();
5049 emit_libcall_block (insns, target, value,
5050 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5051 GET_MODE (to), from));
5054 if (target != to)
5056 if (GET_MODE (to) == GET_MODE (target))
5057 emit_move_insn (to, target);
5058 else
5059 convert_move (to, target, 0);
5064 /* Promote integer arguments for a libcall if necessary.
5065 emit_library_call_value cannot do the promotion because it does not
5066 know if it should do a signed or unsigned promotion. This is because
5067 there are no tree types defined for libcalls. */
5069 static rtx
5070 prepare_libcall_arg (rtx arg, int uintp)
5072 scalar_int_mode mode;
5073 machine_mode arg_mode;
5074 if (is_a <scalar_int_mode> (GET_MODE (arg), &mode))
5076 /* If we need to promote the integer function argument we need to do
5077 it here instead of inside emit_library_call_value because in
5078 emit_library_call_value we don't know if we should do a signed or
5079 unsigned promotion. */
5081 int unsigned_p = 0;
5082 arg_mode = promote_function_mode (NULL_TREE, mode,
5083 &unsigned_p, NULL_TREE, 0);
5084 if (arg_mode != mode)
5085 return convert_to_mode (arg_mode, arg, uintp);
5087 return arg;
5090 /* Generate code to convert FROM or TO a fixed-point.
5091 If UINTP is true, either TO or FROM is an unsigned integer.
5092 If SATP is true, we need to saturate the result. */
5094 void
5095 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5097 machine_mode to_mode = GET_MODE (to);
5098 machine_mode from_mode = GET_MODE (from);
5099 convert_optab tab;
5100 enum rtx_code this_code;
5101 enum insn_code code;
5102 rtx_insn *insns;
5103 rtx value;
5104 rtx libfunc;
5106 if (to_mode == from_mode)
5108 emit_move_insn (to, from);
5109 return;
5112 if (uintp)
5114 tab = satp ? satfractuns_optab : fractuns_optab;
5115 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5117 else
5119 tab = satp ? satfract_optab : fract_optab;
5120 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5122 code = convert_optab_handler (tab, to_mode, from_mode);
5123 if (code != CODE_FOR_nothing)
5125 emit_unop_insn (code, to, from, this_code);
5126 return;
5129 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5130 gcc_assert (libfunc);
5132 from = prepare_libcall_arg (from, uintp);
5133 from_mode = GET_MODE (from);
5135 start_sequence ();
5136 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5137 from, from_mode);
5138 insns = get_insns ();
5139 end_sequence ();
5141 emit_libcall_block (insns, to, value,
5142 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5145 /* Generate code to convert FROM to fixed point and store in TO. FROM
5146 must be floating point, TO must be signed. Use the conversion optab
5147 TAB to do the conversion. */
5149 bool
5150 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5152 enum insn_code icode;
5153 rtx target = to;
5154 machine_mode fmode, imode;
5156 /* We first try to find a pair of modes, one real and one integer, at
5157 least as wide as FROM and TO, respectively, in which we can open-code
5158 this conversion. If the integer mode is wider than the mode of TO,
5159 we can do the conversion either signed or unsigned. */
5161 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
5162 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
5164 icode = convert_optab_handler (tab, imode, fmode);
5165 if (icode != CODE_FOR_nothing)
5167 rtx_insn *last = get_last_insn ();
5168 if (fmode != GET_MODE (from))
5169 from = convert_to_mode (fmode, from, 0);
5171 if (imode != GET_MODE (to))
5172 target = gen_reg_rtx (imode);
5174 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5176 delete_insns_since (last);
5177 continue;
5179 if (target != to)
5180 convert_move (to, target, 0);
5181 return true;
5185 return false;
5188 /* Report whether we have an instruction to perform the operation
5189 specified by CODE on operands of mode MODE. */
5191 have_insn_for (enum rtx_code code, machine_mode mode)
5193 return (code_to_optab (code)
5194 && (optab_handler (code_to_optab (code), mode)
5195 != CODE_FOR_nothing));
5198 /* Print information about the current contents of the optabs on
5199 STDERR. */
5201 DEBUG_FUNCTION void
5202 debug_optab_libfuncs (void)
5204 int i, j, k;
5206 /* Dump the arithmetic optabs. */
5207 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
5208 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5210 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
5211 if (l)
5213 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5214 fprintf (stderr, "%s\t%s:\t%s\n",
5215 GET_RTX_NAME (optab_to_code ((optab) i)),
5216 GET_MODE_NAME (j),
5217 XSTR (l, 0));
5221 /* Dump the conversion optabs. */
5222 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
5223 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5224 for (k = 0; k < NUM_MACHINE_MODES; ++k)
5226 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
5227 (machine_mode) k);
5228 if (l)
5230 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5231 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
5232 GET_RTX_NAME (optab_to_code ((optab) i)),
5233 GET_MODE_NAME (j),
5234 GET_MODE_NAME (k),
5235 XSTR (l, 0));
5240 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5241 CODE. Return 0 on failure. */
5243 rtx_insn *
5244 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
5246 machine_mode mode = GET_MODE (op1);
5247 enum insn_code icode;
5248 rtx_insn *insn;
5249 rtx trap_rtx;
5251 if (mode == VOIDmode)
5252 return 0;
5254 icode = optab_handler (ctrap_optab, mode);
5255 if (icode == CODE_FOR_nothing)
5256 return 0;
5258 /* Some targets only accept a zero trap code. */
5259 if (!insn_operand_matches (icode, 3, tcode))
5260 return 0;
5262 do_pending_stack_adjust ();
5263 start_sequence ();
5264 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
5265 &trap_rtx, &mode);
5266 if (!trap_rtx)
5267 insn = NULL;
5268 else
5269 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
5270 tcode);
5272 /* If that failed, then give up. */
5273 if (insn == 0)
5275 end_sequence ();
5276 return 0;
5279 emit_insn (insn);
5280 insn = get_insns ();
5281 end_sequence ();
5282 return insn;
5285 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
5286 or unsigned operation code. */
5288 enum rtx_code
5289 get_rtx_code (enum tree_code tcode, bool unsignedp)
5291 enum rtx_code code;
5292 switch (tcode)
5294 case EQ_EXPR:
5295 code = EQ;
5296 break;
5297 case NE_EXPR:
5298 code = NE;
5299 break;
5300 case LT_EXPR:
5301 code = unsignedp ? LTU : LT;
5302 break;
5303 case LE_EXPR:
5304 code = unsignedp ? LEU : LE;
5305 break;
5306 case GT_EXPR:
5307 code = unsignedp ? GTU : GT;
5308 break;
5309 case GE_EXPR:
5310 code = unsignedp ? GEU : GE;
5311 break;
5313 case UNORDERED_EXPR:
5314 code = UNORDERED;
5315 break;
5316 case ORDERED_EXPR:
5317 code = ORDERED;
5318 break;
5319 case UNLT_EXPR:
5320 code = UNLT;
5321 break;
5322 case UNLE_EXPR:
5323 code = UNLE;
5324 break;
5325 case UNGT_EXPR:
5326 code = UNGT;
5327 break;
5328 case UNGE_EXPR:
5329 code = UNGE;
5330 break;
5331 case UNEQ_EXPR:
5332 code = UNEQ;
5333 break;
5334 case LTGT_EXPR:
5335 code = LTGT;
5336 break;
5338 case BIT_AND_EXPR:
5339 code = AND;
5340 break;
5342 case BIT_IOR_EXPR:
5343 code = IOR;
5344 break;
5346 default:
5347 gcc_unreachable ();
5349 return code;
5352 /* Return a comparison rtx of mode CMP_MODE for COND. Use UNSIGNEDP to
5353 select signed or unsigned operators. OPNO holds the index of the
5354 first comparison operand for insn ICODE. Do not generate the
5355 compare instruction itself. */
5357 static rtx
5358 vector_compare_rtx (machine_mode cmp_mode, enum tree_code tcode,
5359 tree t_op0, tree t_op1, bool unsignedp,
5360 enum insn_code icode, unsigned int opno)
5362 struct expand_operand ops[2];
5363 rtx rtx_op0, rtx_op1;
5364 machine_mode m0, m1;
5365 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
5367 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
5369 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
5370 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
5371 cases, use the original mode. */
5372 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
5373 EXPAND_STACK_PARM);
5374 m0 = GET_MODE (rtx_op0);
5375 if (m0 == VOIDmode)
5376 m0 = TYPE_MODE (TREE_TYPE (t_op0));
5378 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
5379 EXPAND_STACK_PARM);
5380 m1 = GET_MODE (rtx_op1);
5381 if (m1 == VOIDmode)
5382 m1 = TYPE_MODE (TREE_TYPE (t_op1));
5384 create_input_operand (&ops[0], rtx_op0, m0);
5385 create_input_operand (&ops[1], rtx_op1, m1);
5386 if (!maybe_legitimize_operands (icode, opno, 2, ops))
5387 gcc_unreachable ();
5388 return gen_rtx_fmt_ee (rcode, cmp_mode, ops[0].value, ops[1].value);
5391 /* Check if vec_perm mask SEL is a constant equivalent to a shift of
5392 the first vec_perm operand, assuming the second operand is a constant
5393 vector of zeros. Return the shift distance in bits if so, or NULL_RTX
5394 if the vec_perm is not a shift. MODE is the mode of the value being
5395 shifted. */
5396 static rtx
5397 shift_amt_for_vec_perm_mask (machine_mode mode, const vec_perm_indices &sel)
5399 unsigned int bitsize = GET_MODE_UNIT_BITSIZE (mode);
5400 poly_int64 first = sel[0];
5401 if (maybe_ge (sel[0], GET_MODE_NUNITS (mode)))
5402 return NULL_RTX;
5404 if (!sel.series_p (0, 1, first, 1))
5406 unsigned int nelt;
5407 if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
5408 return NULL_RTX;
5409 for (unsigned int i = 1; i < nelt; i++)
5411 poly_int64 expected = i + first;
5412 /* Indices into the second vector are all equivalent. */
5413 if (maybe_lt (sel[i], nelt)
5414 ? maybe_ne (sel[i], expected)
5415 : maybe_lt (expected, nelt))
5416 return NULL_RTX;
5420 return gen_int_shift_amount (mode, first * bitsize);
5423 /* A subroutine of expand_vec_perm_var for expanding one vec_perm insn. */
5425 static rtx
5426 expand_vec_perm_1 (enum insn_code icode, rtx target,
5427 rtx v0, rtx v1, rtx sel)
5429 machine_mode tmode = GET_MODE (target);
5430 machine_mode smode = GET_MODE (sel);
5431 struct expand_operand ops[4];
5433 gcc_assert (GET_MODE_CLASS (smode) == MODE_VECTOR_INT
5434 || mode_for_int_vector (tmode).require () == smode);
5435 create_output_operand (&ops[0], target, tmode);
5436 create_input_operand (&ops[3], sel, smode);
5438 /* Make an effort to preserve v0 == v1. The target expander is able to
5439 rely on this to determine if we're permuting a single input operand. */
5440 if (rtx_equal_p (v0, v1))
5442 if (!insn_operand_matches (icode, 1, v0))
5443 v0 = force_reg (tmode, v0);
5444 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
5445 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
5447 create_fixed_operand (&ops[1], v0);
5448 create_fixed_operand (&ops[2], v0);
5450 else
5452 create_input_operand (&ops[1], v0, tmode);
5453 create_input_operand (&ops[2], v1, tmode);
5456 if (maybe_expand_insn (icode, 4, ops))
5457 return ops[0].value;
5458 return NULL_RTX;
5461 /* Implement a permutation of vectors v0 and v1 using the permutation
5462 vector in SEL and return the result. Use TARGET to hold the result
5463 if nonnull and convenient.
5465 MODE is the mode of the vectors being permuted (V0 and V1). SEL_MODE
5466 is the TYPE_MODE associated with SEL, or BLKmode if SEL isn't known
5467 to have a particular mode. */
5470 expand_vec_perm_const (machine_mode mode, rtx v0, rtx v1,
5471 const vec_perm_builder &sel, machine_mode sel_mode,
5472 rtx target)
5474 if (!target || !register_operand (target, mode))
5475 target = gen_reg_rtx (mode);
5477 /* Set QIMODE to a different vector mode with byte elements.
5478 If no such mode, or if MODE already has byte elements, use VOIDmode. */
5479 machine_mode qimode;
5480 if (!qimode_for_vec_perm (mode).exists (&qimode))
5481 qimode = VOIDmode;
5483 rtx_insn *last = get_last_insn ();
5485 bool single_arg_p = rtx_equal_p (v0, v1);
5486 /* Always specify two input vectors here and leave the target to handle
5487 cases in which the inputs are equal. Not all backends can cope with
5488 the single-input representation when testing for a double-input
5489 target instruction. */
5490 vec_perm_indices indices (sel, 2, GET_MODE_NUNITS (mode));
5492 /* See if this can be handled with a vec_shr. We only do this if the
5493 second vector is all zeroes. */
5494 insn_code shift_code = optab_handler (vec_shr_optab, mode);
5495 insn_code shift_code_qi = ((qimode != VOIDmode && qimode != mode)
5496 ? optab_handler (vec_shr_optab, qimode)
5497 : CODE_FOR_nothing);
5499 if (v1 == CONST0_RTX (GET_MODE (v1))
5500 && (shift_code != CODE_FOR_nothing
5501 || shift_code_qi != CODE_FOR_nothing))
5503 rtx shift_amt = shift_amt_for_vec_perm_mask (mode, indices);
5504 if (shift_amt)
5506 struct expand_operand ops[3];
5507 if (shift_code != CODE_FOR_nothing)
5509 create_output_operand (&ops[0], target, mode);
5510 create_input_operand (&ops[1], v0, mode);
5511 create_convert_operand_from_type (&ops[2], shift_amt, sizetype);
5512 if (maybe_expand_insn (shift_code, 3, ops))
5513 return ops[0].value;
5515 if (shift_code_qi != CODE_FOR_nothing)
5517 rtx tmp = gen_reg_rtx (qimode);
5518 create_output_operand (&ops[0], tmp, qimode);
5519 create_input_operand (&ops[1], gen_lowpart (qimode, v0), qimode);
5520 create_convert_operand_from_type (&ops[2], shift_amt, sizetype);
5521 if (maybe_expand_insn (shift_code_qi, 3, ops))
5522 return gen_lowpart (mode, ops[0].value);
5527 if (targetm.vectorize.vec_perm_const != NULL)
5529 v0 = force_reg (mode, v0);
5530 if (single_arg_p)
5531 v1 = v0;
5532 else
5533 v1 = force_reg (mode, v1);
5535 if (targetm.vectorize.vec_perm_const (mode, target, v0, v1, indices))
5536 return target;
5539 /* Fall back to a constant byte-based permutation. */
5540 vec_perm_indices qimode_indices;
5541 rtx target_qi = NULL_RTX, v0_qi = NULL_RTX, v1_qi = NULL_RTX;
5542 if (qimode != VOIDmode)
5544 qimode_indices.new_expanded_vector (indices, GET_MODE_UNIT_SIZE (mode));
5545 target_qi = gen_reg_rtx (qimode);
5546 v0_qi = gen_lowpart (qimode, v0);
5547 v1_qi = gen_lowpart (qimode, v1);
5548 if (targetm.vectorize.vec_perm_const != NULL
5549 && targetm.vectorize.vec_perm_const (qimode, target_qi, v0_qi,
5550 v1_qi, qimode_indices))
5551 return gen_lowpart (mode, target_qi);
5554 /* Otherwise expand as a fully variable permuation. */
5556 /* The optabs are only defined for selectors with the same width
5557 as the values being permuted. */
5558 machine_mode required_sel_mode;
5559 if (!mode_for_int_vector (mode).exists (&required_sel_mode)
5560 || !VECTOR_MODE_P (required_sel_mode))
5562 delete_insns_since (last);
5563 return NULL_RTX;
5566 /* We know that it is semantically valid to treat SEL as having SEL_MODE.
5567 If that isn't the mode we want then we need to prove that using
5568 REQUIRED_SEL_MODE is OK. */
5569 if (sel_mode != required_sel_mode)
5571 if (!selector_fits_mode_p (required_sel_mode, indices))
5573 delete_insns_since (last);
5574 return NULL_RTX;
5576 sel_mode = required_sel_mode;
5579 insn_code icode = direct_optab_handler (vec_perm_optab, mode);
5580 if (icode != CODE_FOR_nothing)
5582 rtx sel_rtx = vec_perm_indices_to_rtx (sel_mode, indices);
5583 rtx tmp = expand_vec_perm_1 (icode, target, v0, v1, sel_rtx);
5584 if (tmp)
5585 return tmp;
5588 if (qimode != VOIDmode
5589 && selector_fits_mode_p (qimode, qimode_indices))
5591 icode = direct_optab_handler (vec_perm_optab, qimode);
5592 if (icode != CODE_FOR_nothing)
5594 rtx sel_qi = vec_perm_indices_to_rtx (qimode, qimode_indices);
5595 rtx tmp = expand_vec_perm_1 (icode, target_qi, v0_qi, v1_qi, sel_qi);
5596 if (tmp)
5597 return gen_lowpart (mode, tmp);
5601 delete_insns_since (last);
5602 return NULL_RTX;
5605 /* Implement a permutation of vectors v0 and v1 using the permutation
5606 vector in SEL and return the result. Use TARGET to hold the result
5607 if nonnull and convenient.
5609 MODE is the mode of the vectors being permuted (V0 and V1).
5610 SEL must have the integer equivalent of MODE and is known to be
5611 unsuitable for permutes with a constant permutation vector. */
5614 expand_vec_perm_var (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
5616 enum insn_code icode;
5617 unsigned int i, u;
5618 rtx tmp, sel_qi;
5620 u = GET_MODE_UNIT_SIZE (mode);
5622 if (!target || GET_MODE (target) != mode)
5623 target = gen_reg_rtx (mode);
5625 icode = direct_optab_handler (vec_perm_optab, mode);
5626 if (icode != CODE_FOR_nothing)
5628 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
5629 if (tmp)
5630 return tmp;
5633 /* As a special case to aid several targets, lower the element-based
5634 permutation to a byte-based permutation and try again. */
5635 machine_mode qimode;
5636 if (!qimode_for_vec_perm (mode).exists (&qimode)
5637 || maybe_gt (GET_MODE_NUNITS (qimode), GET_MODE_MASK (QImode) + 1))
5638 return NULL_RTX;
5639 icode = direct_optab_handler (vec_perm_optab, qimode);
5640 if (icode == CODE_FOR_nothing)
5641 return NULL_RTX;
5643 /* Multiply each element by its byte size. */
5644 machine_mode selmode = GET_MODE (sel);
5645 if (u == 2)
5646 sel = expand_simple_binop (selmode, PLUS, sel, sel,
5647 NULL, 0, OPTAB_DIRECT);
5648 else
5649 sel = expand_simple_binop (selmode, ASHIFT, sel,
5650 gen_int_shift_amount (selmode, exact_log2 (u)),
5651 NULL, 0, OPTAB_DIRECT);
5652 gcc_assert (sel != NULL);
5654 /* Broadcast the low byte each element into each of its bytes.
5655 The encoding has U interleaved stepped patterns, one for each
5656 byte of an element. */
5657 vec_perm_builder const_sel (GET_MODE_SIZE (mode), u, 3);
5658 unsigned int low_byte_in_u = BYTES_BIG_ENDIAN ? u - 1 : 0;
5659 for (i = 0; i < 3; ++i)
5660 for (unsigned int j = 0; j < u; ++j)
5661 const_sel.quick_push (i * u + low_byte_in_u);
5662 sel = gen_lowpart (qimode, sel);
5663 sel = expand_vec_perm_const (qimode, sel, sel, const_sel, qimode, NULL);
5664 gcc_assert (sel != NULL);
5666 /* Add the byte offset to each byte element. */
5667 /* Note that the definition of the indicies here is memory ordering,
5668 so there should be no difference between big and little endian. */
5669 rtx_vector_builder byte_indices (qimode, u, 1);
5670 for (i = 0; i < u; ++i)
5671 byte_indices.quick_push (GEN_INT (i));
5672 tmp = byte_indices.build ();
5673 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
5674 sel, 0, OPTAB_DIRECT);
5675 gcc_assert (sel_qi != NULL);
5677 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
5678 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
5679 gen_lowpart (qimode, v1), sel_qi);
5680 if (tmp)
5681 tmp = gen_lowpart (mode, tmp);
5682 return tmp;
5685 /* Generate insns for a VEC_COND_EXPR with mask, given its TYPE and its
5686 three operands. */
5689 expand_vec_cond_mask_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
5690 rtx target)
5692 struct expand_operand ops[4];
5693 machine_mode mode = TYPE_MODE (vec_cond_type);
5694 machine_mode mask_mode = TYPE_MODE (TREE_TYPE (op0));
5695 enum insn_code icode = get_vcond_mask_icode (mode, mask_mode);
5696 rtx mask, rtx_op1, rtx_op2;
5698 if (icode == CODE_FOR_nothing)
5699 return 0;
5701 mask = expand_normal (op0);
5702 rtx_op1 = expand_normal (op1);
5703 rtx_op2 = expand_normal (op2);
5705 mask = force_reg (mask_mode, mask);
5706 rtx_op1 = force_reg (GET_MODE (rtx_op1), rtx_op1);
5708 create_output_operand (&ops[0], target, mode);
5709 create_input_operand (&ops[1], rtx_op1, mode);
5710 create_input_operand (&ops[2], rtx_op2, mode);
5711 create_input_operand (&ops[3], mask, mask_mode);
5712 expand_insn (icode, 4, ops);
5714 return ops[0].value;
5717 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
5718 three operands. */
5721 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
5722 rtx target)
5724 struct expand_operand ops[6];
5725 enum insn_code icode;
5726 rtx comparison, rtx_op1, rtx_op2;
5727 machine_mode mode = TYPE_MODE (vec_cond_type);
5728 machine_mode cmp_op_mode;
5729 bool unsignedp;
5730 tree op0a, op0b;
5731 enum tree_code tcode;
5733 if (COMPARISON_CLASS_P (op0))
5735 op0a = TREE_OPERAND (op0, 0);
5736 op0b = TREE_OPERAND (op0, 1);
5737 tcode = TREE_CODE (op0);
5739 else
5741 gcc_assert (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (op0)));
5742 if (get_vcond_mask_icode (mode, TYPE_MODE (TREE_TYPE (op0)))
5743 != CODE_FOR_nothing)
5744 return expand_vec_cond_mask_expr (vec_cond_type, op0, op1,
5745 op2, target);
5746 /* Fake op0 < 0. */
5747 else
5749 gcc_assert (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (op0)))
5750 == MODE_VECTOR_INT);
5751 op0a = op0;
5752 op0b = build_zero_cst (TREE_TYPE (op0));
5753 tcode = LT_EXPR;
5756 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
5757 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
5760 gcc_assert (known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (cmp_op_mode))
5761 && known_eq (GET_MODE_NUNITS (mode),
5762 GET_MODE_NUNITS (cmp_op_mode)));
5764 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
5765 if (icode == CODE_FOR_nothing)
5767 if (tcode == EQ_EXPR || tcode == NE_EXPR)
5768 icode = get_vcond_eq_icode (mode, cmp_op_mode);
5769 if (icode == CODE_FOR_nothing)
5770 return 0;
5773 comparison = vector_compare_rtx (VOIDmode, tcode, op0a, op0b, unsignedp,
5774 icode, 4);
5775 rtx_op1 = expand_normal (op1);
5776 rtx_op2 = expand_normal (op2);
5778 create_output_operand (&ops[0], target, mode);
5779 create_input_operand (&ops[1], rtx_op1, mode);
5780 create_input_operand (&ops[2], rtx_op2, mode);
5781 create_fixed_operand (&ops[3], comparison);
5782 create_fixed_operand (&ops[4], XEXP (comparison, 0));
5783 create_fixed_operand (&ops[5], XEXP (comparison, 1));
5784 expand_insn (icode, 6, ops);
5785 return ops[0].value;
5788 /* Generate VEC_SERIES_EXPR <OP0, OP1>, returning a value of mode VMODE.
5789 Use TARGET for the result if nonnull and convenient. */
5792 expand_vec_series_expr (machine_mode vmode, rtx op0, rtx op1, rtx target)
5794 struct expand_operand ops[3];
5795 enum insn_code icode;
5796 machine_mode emode = GET_MODE_INNER (vmode);
5798 icode = direct_optab_handler (vec_series_optab, vmode);
5799 gcc_assert (icode != CODE_FOR_nothing);
5801 create_output_operand (&ops[0], target, vmode);
5802 create_input_operand (&ops[1], op0, emode);
5803 create_input_operand (&ops[2], op1, emode);
5805 expand_insn (icode, 3, ops);
5806 return ops[0].value;
5809 /* Generate insns for a vector comparison into a mask. */
5812 expand_vec_cmp_expr (tree type, tree exp, rtx target)
5814 struct expand_operand ops[4];
5815 enum insn_code icode;
5816 rtx comparison;
5817 machine_mode mask_mode = TYPE_MODE (type);
5818 machine_mode vmode;
5819 bool unsignedp;
5820 tree op0a, op0b;
5821 enum tree_code tcode;
5823 op0a = TREE_OPERAND (exp, 0);
5824 op0b = TREE_OPERAND (exp, 1);
5825 tcode = TREE_CODE (exp);
5827 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
5828 vmode = TYPE_MODE (TREE_TYPE (op0a));
5830 icode = get_vec_cmp_icode (vmode, mask_mode, unsignedp);
5831 if (icode == CODE_FOR_nothing)
5833 if (tcode == EQ_EXPR || tcode == NE_EXPR)
5834 icode = get_vec_cmp_eq_icode (vmode, mask_mode);
5835 if (icode == CODE_FOR_nothing)
5836 return 0;
5839 comparison = vector_compare_rtx (mask_mode, tcode, op0a, op0b,
5840 unsignedp, icode, 2);
5841 create_output_operand (&ops[0], target, mask_mode);
5842 create_fixed_operand (&ops[1], comparison);
5843 create_fixed_operand (&ops[2], XEXP (comparison, 0));
5844 create_fixed_operand (&ops[3], XEXP (comparison, 1));
5845 expand_insn (icode, 4, ops);
5846 return ops[0].value;
5849 /* Expand a highpart multiply. */
5852 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
5853 rtx target, bool uns_p)
5855 struct expand_operand eops[3];
5856 enum insn_code icode;
5857 int method, i;
5858 machine_mode wmode;
5859 rtx m1, m2;
5860 optab tab1, tab2;
5862 method = can_mult_highpart_p (mode, uns_p);
5863 switch (method)
5865 case 0:
5866 return NULL_RTX;
5867 case 1:
5868 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
5869 return expand_binop (mode, tab1, op0, op1, target, uns_p,
5870 OPTAB_LIB_WIDEN);
5871 case 2:
5872 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
5873 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
5874 break;
5875 case 3:
5876 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
5877 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
5878 if (BYTES_BIG_ENDIAN)
5879 std::swap (tab1, tab2);
5880 break;
5881 default:
5882 gcc_unreachable ();
5885 icode = optab_handler (tab1, mode);
5886 wmode = insn_data[icode].operand[0].mode;
5887 gcc_checking_assert (known_eq (2 * GET_MODE_NUNITS (wmode),
5888 GET_MODE_NUNITS (mode)));
5889 gcc_checking_assert (known_eq (GET_MODE_SIZE (wmode), GET_MODE_SIZE (mode)));
5891 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
5892 create_input_operand (&eops[1], op0, mode);
5893 create_input_operand (&eops[2], op1, mode);
5894 expand_insn (icode, 3, eops);
5895 m1 = gen_lowpart (mode, eops[0].value);
5897 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
5898 create_input_operand (&eops[1], op0, mode);
5899 create_input_operand (&eops[2], op1, mode);
5900 expand_insn (optab_handler (tab2, mode), 3, eops);
5901 m2 = gen_lowpart (mode, eops[0].value);
5903 vec_perm_builder sel;
5904 if (method == 2)
5906 /* The encoding has 2 interleaved stepped patterns. */
5907 sel.new_vector (GET_MODE_NUNITS (mode), 2, 3);
5908 for (i = 0; i < 6; ++i)
5909 sel.quick_push (!BYTES_BIG_ENDIAN + (i & ~1)
5910 + ((i & 1) ? GET_MODE_NUNITS (mode) : 0));
5912 else
5914 /* The encoding has a single interleaved stepped pattern. */
5915 sel.new_vector (GET_MODE_NUNITS (mode), 1, 3);
5916 for (i = 0; i < 3; ++i)
5917 sel.quick_push (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
5920 return expand_vec_perm_const (mode, m1, m2, sel, BLKmode, target);
5923 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
5924 pattern. */
5926 static void
5927 find_cc_set (rtx x, const_rtx pat, void *data)
5929 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
5930 && GET_CODE (pat) == SET)
5932 rtx *p_cc_reg = (rtx *) data;
5933 gcc_assert (!*p_cc_reg);
5934 *p_cc_reg = x;
5938 /* This is a helper function for the other atomic operations. This function
5939 emits a loop that contains SEQ that iterates until a compare-and-swap
5940 operation at the end succeeds. MEM is the memory to be modified. SEQ is
5941 a set of instructions that takes a value from OLD_REG as an input and
5942 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
5943 set to the current contents of MEM. After SEQ, a compare-and-swap will
5944 attempt to update MEM with NEW_REG. The function returns true when the
5945 loop was generated successfully. */
5947 static bool
5948 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
5950 machine_mode mode = GET_MODE (mem);
5951 rtx_code_label *label;
5952 rtx cmp_reg, success, oldval;
5954 /* The loop we want to generate looks like
5956 cmp_reg = mem;
5957 label:
5958 old_reg = cmp_reg;
5959 seq;
5960 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
5961 if (success)
5962 goto label;
5964 Note that we only do the plain load from memory once. Subsequent
5965 iterations use the value loaded by the compare-and-swap pattern. */
5967 label = gen_label_rtx ();
5968 cmp_reg = gen_reg_rtx (mode);
5970 emit_move_insn (cmp_reg, mem);
5971 emit_label (label);
5972 emit_move_insn (old_reg, cmp_reg);
5973 if (seq)
5974 emit_insn (seq);
5976 success = NULL_RTX;
5977 oldval = cmp_reg;
5978 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
5979 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
5980 MEMMODEL_RELAXED))
5981 return false;
5983 if (oldval != cmp_reg)
5984 emit_move_insn (cmp_reg, oldval);
5986 /* Mark this jump predicted not taken. */
5987 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
5988 GET_MODE (success), 1, label,
5989 profile_probability::guessed_never ());
5990 return true;
5994 /* This function tries to emit an atomic_exchange intruction. VAL is written
5995 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
5996 using TARGET if possible. */
5998 static rtx
5999 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6001 machine_mode mode = GET_MODE (mem);
6002 enum insn_code icode;
6004 /* If the target supports the exchange directly, great. */
6005 icode = direct_optab_handler (atomic_exchange_optab, mode);
6006 if (icode != CODE_FOR_nothing)
6008 struct expand_operand ops[4];
6010 create_output_operand (&ops[0], target, mode);
6011 create_fixed_operand (&ops[1], mem);
6012 create_input_operand (&ops[2], val, mode);
6013 create_integer_operand (&ops[3], model);
6014 if (maybe_expand_insn (icode, 4, ops))
6015 return ops[0].value;
6018 return NULL_RTX;
6021 /* This function tries to implement an atomic exchange operation using
6022 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
6023 The previous contents of *MEM are returned, using TARGET if possible.
6024 Since this instructionn is an acquire barrier only, stronger memory
6025 models may require additional barriers to be emitted. */
6027 static rtx
6028 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
6029 enum memmodel model)
6031 machine_mode mode = GET_MODE (mem);
6032 enum insn_code icode;
6033 rtx_insn *last_insn = get_last_insn ();
6035 icode = optab_handler (sync_lock_test_and_set_optab, mode);
6037 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
6038 exists, and the memory model is stronger than acquire, add a release
6039 barrier before the instruction. */
6041 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
6042 expand_mem_thread_fence (model);
6044 if (icode != CODE_FOR_nothing)
6046 struct expand_operand ops[3];
6047 create_output_operand (&ops[0], target, mode);
6048 create_fixed_operand (&ops[1], mem);
6049 create_input_operand (&ops[2], val, mode);
6050 if (maybe_expand_insn (icode, 3, ops))
6051 return ops[0].value;
6054 /* If an external test-and-set libcall is provided, use that instead of
6055 any external compare-and-swap that we might get from the compare-and-
6056 swap-loop expansion later. */
6057 if (!can_compare_and_swap_p (mode, false))
6059 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
6060 if (libfunc != NULL)
6062 rtx addr;
6064 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6065 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6066 mode, addr, ptr_mode,
6067 val, mode);
6071 /* If the test_and_set can't be emitted, eliminate any barrier that might
6072 have been emitted. */
6073 delete_insns_since (last_insn);
6074 return NULL_RTX;
6077 /* This function tries to implement an atomic exchange operation using a
6078 compare_and_swap loop. VAL is written to *MEM. The previous contents of
6079 *MEM are returned, using TARGET if possible. No memory model is required
6080 since a compare_and_swap loop is seq-cst. */
6082 static rtx
6083 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
6085 machine_mode mode = GET_MODE (mem);
6087 if (can_compare_and_swap_p (mode, true))
6089 if (!target || !register_operand (target, mode))
6090 target = gen_reg_rtx (mode);
6091 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
6092 return target;
6095 return NULL_RTX;
6098 /* This function tries to implement an atomic test-and-set operation
6099 using the atomic_test_and_set instruction pattern. A boolean value
6100 is returned from the operation, using TARGET if possible. */
6102 static rtx
6103 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6105 machine_mode pat_bool_mode;
6106 struct expand_operand ops[3];
6108 if (!targetm.have_atomic_test_and_set ())
6109 return NULL_RTX;
6111 /* While we always get QImode from __atomic_test_and_set, we get
6112 other memory modes from __sync_lock_test_and_set. Note that we
6113 use no endian adjustment here. This matches the 4.6 behavior
6114 in the Sparc backend. */
6115 enum insn_code icode = targetm.code_for_atomic_test_and_set;
6116 gcc_checking_assert (insn_data[icode].operand[1].mode == QImode);
6117 if (GET_MODE (mem) != QImode)
6118 mem = adjust_address_nv (mem, QImode, 0);
6120 pat_bool_mode = insn_data[icode].operand[0].mode;
6121 create_output_operand (&ops[0], target, pat_bool_mode);
6122 create_fixed_operand (&ops[1], mem);
6123 create_integer_operand (&ops[2], model);
6125 if (maybe_expand_insn (icode, 3, ops))
6126 return ops[0].value;
6127 return NULL_RTX;
6130 /* This function expands the legacy _sync_lock test_and_set operation which is
6131 generally an atomic exchange. Some limited targets only allow the
6132 constant 1 to be stored. This is an ACQUIRE operation.
6134 TARGET is an optional place to stick the return value.
6135 MEM is where VAL is stored. */
6138 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
6140 rtx ret;
6142 /* Try an atomic_exchange first. */
6143 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
6144 if (ret)
6145 return ret;
6147 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
6148 MEMMODEL_SYNC_ACQUIRE);
6149 if (ret)
6150 return ret;
6152 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6153 if (ret)
6154 return ret;
6156 /* If there are no other options, try atomic_test_and_set if the value
6157 being stored is 1. */
6158 if (val == const1_rtx)
6159 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
6161 return ret;
6164 /* This function expands the atomic test_and_set operation:
6165 atomically store a boolean TRUE into MEM and return the previous value.
6167 MEMMODEL is the memory model variant to use.
6168 TARGET is an optional place to stick the return value. */
6171 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6173 machine_mode mode = GET_MODE (mem);
6174 rtx ret, trueval, subtarget;
6176 ret = maybe_emit_atomic_test_and_set (target, mem, model);
6177 if (ret)
6178 return ret;
6180 /* Be binary compatible with non-default settings of trueval, and different
6181 cpu revisions. E.g. one revision may have atomic-test-and-set, but
6182 another only has atomic-exchange. */
6183 if (targetm.atomic_test_and_set_trueval == 1)
6185 trueval = const1_rtx;
6186 subtarget = target ? target : gen_reg_rtx (mode);
6188 else
6190 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
6191 subtarget = gen_reg_rtx (mode);
6194 /* Try the atomic-exchange optab... */
6195 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
6197 /* ... then an atomic-compare-and-swap loop ... */
6198 if (!ret)
6199 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
6201 /* ... before trying the vaguely defined legacy lock_test_and_set. */
6202 if (!ret)
6203 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
6205 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
6206 things with the value 1. Thus we try again without trueval. */
6207 if (!ret && targetm.atomic_test_and_set_trueval != 1)
6208 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
6210 /* Failing all else, assume a single threaded environment and simply
6211 perform the operation. */
6212 if (!ret)
6214 /* If the result is ignored skip the move to target. */
6215 if (subtarget != const0_rtx)
6216 emit_move_insn (subtarget, mem);
6218 emit_move_insn (mem, trueval);
6219 ret = subtarget;
6222 /* Recall that have to return a boolean value; rectify if trueval
6223 is not exactly one. */
6224 if (targetm.atomic_test_and_set_trueval != 1)
6225 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
6227 return ret;
6230 /* This function expands the atomic exchange operation:
6231 atomically store VAL in MEM and return the previous value in MEM.
6233 MEMMODEL is the memory model variant to use.
6234 TARGET is an optional place to stick the return value. */
6237 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6239 machine_mode mode = GET_MODE (mem);
6240 rtx ret;
6242 /* If loads are not atomic for the required size and we are not called to
6243 provide a __sync builtin, do not do anything so that we stay consistent
6244 with atomic loads of the same size. */
6245 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6246 return NULL_RTX;
6248 ret = maybe_emit_atomic_exchange (target, mem, val, model);
6250 /* Next try a compare-and-swap loop for the exchange. */
6251 if (!ret)
6252 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6254 return ret;
6257 /* This function expands the atomic compare exchange operation:
6259 *PTARGET_BOOL is an optional place to store the boolean success/failure.
6260 *PTARGET_OVAL is an optional place to store the old value from memory.
6261 Both target parameters may be NULL or const0_rtx to indicate that we do
6262 not care about that return value. Both target parameters are updated on
6263 success to the actual location of the corresponding result.
6265 MEMMODEL is the memory model variant to use.
6267 The return value of the function is true for success. */
6269 bool
6270 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
6271 rtx mem, rtx expected, rtx desired,
6272 bool is_weak, enum memmodel succ_model,
6273 enum memmodel fail_model)
6275 machine_mode mode = GET_MODE (mem);
6276 struct expand_operand ops[8];
6277 enum insn_code icode;
6278 rtx target_oval, target_bool = NULL_RTX;
6279 rtx libfunc;
6281 /* If loads are not atomic for the required size and we are not called to
6282 provide a __sync builtin, do not do anything so that we stay consistent
6283 with atomic loads of the same size. */
6284 if (!can_atomic_load_p (mode) && !is_mm_sync (succ_model))
6285 return false;
6287 /* Load expected into a register for the compare and swap. */
6288 if (MEM_P (expected))
6289 expected = copy_to_reg (expected);
6291 /* Make sure we always have some place to put the return oldval.
6292 Further, make sure that place is distinct from the input expected,
6293 just in case we need that path down below. */
6294 if (ptarget_oval && *ptarget_oval == const0_rtx)
6295 ptarget_oval = NULL;
6297 if (ptarget_oval == NULL
6298 || (target_oval = *ptarget_oval) == NULL
6299 || reg_overlap_mentioned_p (expected, target_oval))
6300 target_oval = gen_reg_rtx (mode);
6302 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
6303 if (icode != CODE_FOR_nothing)
6305 machine_mode bool_mode = insn_data[icode].operand[0].mode;
6307 if (ptarget_bool && *ptarget_bool == const0_rtx)
6308 ptarget_bool = NULL;
6310 /* Make sure we always have a place for the bool operand. */
6311 if (ptarget_bool == NULL
6312 || (target_bool = *ptarget_bool) == NULL
6313 || GET_MODE (target_bool) != bool_mode)
6314 target_bool = gen_reg_rtx (bool_mode);
6316 /* Emit the compare_and_swap. */
6317 create_output_operand (&ops[0], target_bool, bool_mode);
6318 create_output_operand (&ops[1], target_oval, mode);
6319 create_fixed_operand (&ops[2], mem);
6320 create_input_operand (&ops[3], expected, mode);
6321 create_input_operand (&ops[4], desired, mode);
6322 create_integer_operand (&ops[5], is_weak);
6323 create_integer_operand (&ops[6], succ_model);
6324 create_integer_operand (&ops[7], fail_model);
6325 if (maybe_expand_insn (icode, 8, ops))
6327 /* Return success/failure. */
6328 target_bool = ops[0].value;
6329 target_oval = ops[1].value;
6330 goto success;
6334 /* Otherwise fall back to the original __sync_val_compare_and_swap
6335 which is always seq-cst. */
6336 icode = optab_handler (sync_compare_and_swap_optab, mode);
6337 if (icode != CODE_FOR_nothing)
6339 rtx cc_reg;
6341 create_output_operand (&ops[0], target_oval, mode);
6342 create_fixed_operand (&ops[1], mem);
6343 create_input_operand (&ops[2], expected, mode);
6344 create_input_operand (&ops[3], desired, mode);
6345 if (!maybe_expand_insn (icode, 4, ops))
6346 return false;
6348 target_oval = ops[0].value;
6350 /* If the caller isn't interested in the boolean return value,
6351 skip the computation of it. */
6352 if (ptarget_bool == NULL)
6353 goto success;
6355 /* Otherwise, work out if the compare-and-swap succeeded. */
6356 cc_reg = NULL_RTX;
6357 if (have_insn_for (COMPARE, CCmode))
6358 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6359 if (cc_reg)
6361 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
6362 const0_rtx, VOIDmode, 0, 1);
6363 goto success;
6365 goto success_bool_from_val;
6368 /* Also check for library support for __sync_val_compare_and_swap. */
6369 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
6370 if (libfunc != NULL)
6372 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6373 rtx target = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6374 mode, addr, ptr_mode,
6375 expected, mode, desired, mode);
6376 emit_move_insn (target_oval, target);
6378 /* Compute the boolean return value only if requested. */
6379 if (ptarget_bool)
6380 goto success_bool_from_val;
6381 else
6382 goto success;
6385 /* Failure. */
6386 return false;
6388 success_bool_from_val:
6389 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
6390 expected, VOIDmode, 1, 1);
6391 success:
6392 /* Make sure that the oval output winds up where the caller asked. */
6393 if (ptarget_oval)
6394 *ptarget_oval = target_oval;
6395 if (ptarget_bool)
6396 *ptarget_bool = target_bool;
6397 return true;
6400 /* Generate asm volatile("" : : : "memory") as the memory blockage. */
6402 static void
6403 expand_asm_memory_blockage (void)
6405 rtx asm_op, clob;
6407 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
6408 rtvec_alloc (0), rtvec_alloc (0),
6409 rtvec_alloc (0), UNKNOWN_LOCATION);
6410 MEM_VOLATILE_P (asm_op) = 1;
6412 clob = gen_rtx_SCRATCH (VOIDmode);
6413 clob = gen_rtx_MEM (BLKmode, clob);
6414 clob = gen_rtx_CLOBBER (VOIDmode, clob);
6416 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
6419 /* Do not propagate memory accesses across this point. */
6421 static void
6422 expand_memory_blockage (void)
6424 if (targetm.have_memory_blockage ())
6425 emit_insn (targetm.gen_memory_blockage ());
6426 else
6427 expand_asm_memory_blockage ();
6430 /* This routine will either emit the mem_thread_fence pattern or issue a
6431 sync_synchronize to generate a fence for memory model MEMMODEL. */
6433 void
6434 expand_mem_thread_fence (enum memmodel model)
6436 if (is_mm_relaxed (model))
6437 return;
6438 if (targetm.have_mem_thread_fence ())
6440 emit_insn (targetm.gen_mem_thread_fence (GEN_INT (model)));
6441 expand_memory_blockage ();
6443 else if (targetm.have_memory_barrier ())
6444 emit_insn (targetm.gen_memory_barrier ());
6445 else if (synchronize_libfunc != NULL_RTX)
6446 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode);
6447 else
6448 expand_memory_blockage ();
6451 /* Emit a signal fence with given memory model. */
6453 void
6454 expand_mem_signal_fence (enum memmodel model)
6456 /* No machine barrier is required to implement a signal fence, but
6457 a compiler memory barrier must be issued, except for relaxed MM. */
6458 if (!is_mm_relaxed (model))
6459 expand_memory_blockage ();
6462 /* This function expands the atomic load operation:
6463 return the atomically loaded value in MEM.
6465 MEMMODEL is the memory model variant to use.
6466 TARGET is an option place to stick the return value. */
6469 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
6471 machine_mode mode = GET_MODE (mem);
6472 enum insn_code icode;
6474 /* If the target supports the load directly, great. */
6475 icode = direct_optab_handler (atomic_load_optab, mode);
6476 if (icode != CODE_FOR_nothing)
6478 struct expand_operand ops[3];
6479 rtx_insn *last = get_last_insn ();
6480 if (is_mm_seq_cst (model))
6481 expand_memory_blockage ();
6483 create_output_operand (&ops[0], target, mode);
6484 create_fixed_operand (&ops[1], mem);
6485 create_integer_operand (&ops[2], model);
6486 if (maybe_expand_insn (icode, 3, ops))
6488 if (!is_mm_relaxed (model))
6489 expand_memory_blockage ();
6490 return ops[0].value;
6492 delete_insns_since (last);
6495 /* If the size of the object is greater than word size on this target,
6496 then we assume that a load will not be atomic. We could try to
6497 emulate a load with a compare-and-swap operation, but the store that
6498 doing this could result in would be incorrect if this is a volatile
6499 atomic load or targetting read-only-mapped memory. */
6500 if (maybe_gt (GET_MODE_PRECISION (mode), BITS_PER_WORD))
6501 /* If there is no atomic load, leave the library call. */
6502 return NULL_RTX;
6504 /* Otherwise assume loads are atomic, and emit the proper barriers. */
6505 if (!target || target == const0_rtx)
6506 target = gen_reg_rtx (mode);
6508 /* For SEQ_CST, emit a barrier before the load. */
6509 if (is_mm_seq_cst (model))
6510 expand_mem_thread_fence (model);
6512 emit_move_insn (target, mem);
6514 /* Emit the appropriate barrier after the load. */
6515 expand_mem_thread_fence (model);
6517 return target;
6520 /* This function expands the atomic store operation:
6521 Atomically store VAL in MEM.
6522 MEMMODEL is the memory model variant to use.
6523 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
6524 function returns const0_rtx if a pattern was emitted. */
6527 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
6529 machine_mode mode = GET_MODE (mem);
6530 enum insn_code icode;
6531 struct expand_operand ops[3];
6533 /* If the target supports the store directly, great. */
6534 icode = direct_optab_handler (atomic_store_optab, mode);
6535 if (icode != CODE_FOR_nothing)
6537 rtx_insn *last = get_last_insn ();
6538 if (!is_mm_relaxed (model))
6539 expand_memory_blockage ();
6540 create_fixed_operand (&ops[0], mem);
6541 create_input_operand (&ops[1], val, mode);
6542 create_integer_operand (&ops[2], model);
6543 if (maybe_expand_insn (icode, 3, ops))
6545 if (is_mm_seq_cst (model))
6546 expand_memory_blockage ();
6547 return const0_rtx;
6549 delete_insns_since (last);
6552 /* If using __sync_lock_release is a viable alternative, try it.
6553 Note that this will not be set to true if we are expanding a generic
6554 __atomic_store_n. */
6555 if (use_release)
6557 icode = direct_optab_handler (sync_lock_release_optab, mode);
6558 if (icode != CODE_FOR_nothing)
6560 create_fixed_operand (&ops[0], mem);
6561 create_input_operand (&ops[1], const0_rtx, mode);
6562 if (maybe_expand_insn (icode, 2, ops))
6564 /* lock_release is only a release barrier. */
6565 if (is_mm_seq_cst (model))
6566 expand_mem_thread_fence (model);
6567 return const0_rtx;
6572 /* If the size of the object is greater than word size on this target,
6573 a default store will not be atomic. */
6574 if (maybe_gt (GET_MODE_PRECISION (mode), BITS_PER_WORD))
6576 /* If loads are atomic or we are called to provide a __sync builtin,
6577 we can try a atomic_exchange and throw away the result. Otherwise,
6578 don't do anything so that we do not create an inconsistency between
6579 loads and stores. */
6580 if (can_atomic_load_p (mode) || is_mm_sync (model))
6582 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
6583 if (!target)
6584 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem,
6585 val);
6586 if (target)
6587 return const0_rtx;
6589 return NULL_RTX;
6592 /* Otherwise assume stores are atomic, and emit the proper barriers. */
6593 expand_mem_thread_fence (model);
6595 emit_move_insn (mem, val);
6597 /* For SEQ_CST, also emit a barrier after the store. */
6598 if (is_mm_seq_cst (model))
6599 expand_mem_thread_fence (model);
6601 return const0_rtx;
6605 /* Structure containing the pointers and values required to process the
6606 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
6608 struct atomic_op_functions
6610 direct_optab mem_fetch_before;
6611 direct_optab mem_fetch_after;
6612 direct_optab mem_no_result;
6613 optab fetch_before;
6614 optab fetch_after;
6615 direct_optab no_result;
6616 enum rtx_code reverse_code;
6620 /* Fill in structure pointed to by OP with the various optab entries for an
6621 operation of type CODE. */
6623 static void
6624 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
6626 gcc_assert (op!= NULL);
6628 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
6629 in the source code during compilation, and the optab entries are not
6630 computable until runtime. Fill in the values at runtime. */
6631 switch (code)
6633 case PLUS:
6634 op->mem_fetch_before = atomic_fetch_add_optab;
6635 op->mem_fetch_after = atomic_add_fetch_optab;
6636 op->mem_no_result = atomic_add_optab;
6637 op->fetch_before = sync_old_add_optab;
6638 op->fetch_after = sync_new_add_optab;
6639 op->no_result = sync_add_optab;
6640 op->reverse_code = MINUS;
6641 break;
6642 case MINUS:
6643 op->mem_fetch_before = atomic_fetch_sub_optab;
6644 op->mem_fetch_after = atomic_sub_fetch_optab;
6645 op->mem_no_result = atomic_sub_optab;
6646 op->fetch_before = sync_old_sub_optab;
6647 op->fetch_after = sync_new_sub_optab;
6648 op->no_result = sync_sub_optab;
6649 op->reverse_code = PLUS;
6650 break;
6651 case XOR:
6652 op->mem_fetch_before = atomic_fetch_xor_optab;
6653 op->mem_fetch_after = atomic_xor_fetch_optab;
6654 op->mem_no_result = atomic_xor_optab;
6655 op->fetch_before = sync_old_xor_optab;
6656 op->fetch_after = sync_new_xor_optab;
6657 op->no_result = sync_xor_optab;
6658 op->reverse_code = XOR;
6659 break;
6660 case AND:
6661 op->mem_fetch_before = atomic_fetch_and_optab;
6662 op->mem_fetch_after = atomic_and_fetch_optab;
6663 op->mem_no_result = atomic_and_optab;
6664 op->fetch_before = sync_old_and_optab;
6665 op->fetch_after = sync_new_and_optab;
6666 op->no_result = sync_and_optab;
6667 op->reverse_code = UNKNOWN;
6668 break;
6669 case IOR:
6670 op->mem_fetch_before = atomic_fetch_or_optab;
6671 op->mem_fetch_after = atomic_or_fetch_optab;
6672 op->mem_no_result = atomic_or_optab;
6673 op->fetch_before = sync_old_ior_optab;
6674 op->fetch_after = sync_new_ior_optab;
6675 op->no_result = sync_ior_optab;
6676 op->reverse_code = UNKNOWN;
6677 break;
6678 case NOT:
6679 op->mem_fetch_before = atomic_fetch_nand_optab;
6680 op->mem_fetch_after = atomic_nand_fetch_optab;
6681 op->mem_no_result = atomic_nand_optab;
6682 op->fetch_before = sync_old_nand_optab;
6683 op->fetch_after = sync_new_nand_optab;
6684 op->no_result = sync_nand_optab;
6685 op->reverse_code = UNKNOWN;
6686 break;
6687 default:
6688 gcc_unreachable ();
6692 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
6693 using memory order MODEL. If AFTER is true the operation needs to return
6694 the value of *MEM after the operation, otherwise the previous value.
6695 TARGET is an optional place to place the result. The result is unused if
6696 it is const0_rtx.
6697 Return the result if there is a better sequence, otherwise NULL_RTX. */
6699 static rtx
6700 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
6701 enum memmodel model, bool after)
6703 /* If the value is prefetched, or not used, it may be possible to replace
6704 the sequence with a native exchange operation. */
6705 if (!after || target == const0_rtx)
6707 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
6708 if (code == AND && val == const0_rtx)
6710 if (target == const0_rtx)
6711 target = gen_reg_rtx (GET_MODE (mem));
6712 return maybe_emit_atomic_exchange (target, mem, val, model);
6715 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
6716 if (code == IOR && val == constm1_rtx)
6718 if (target == const0_rtx)
6719 target = gen_reg_rtx (GET_MODE (mem));
6720 return maybe_emit_atomic_exchange (target, mem, val, model);
6724 return NULL_RTX;
6727 /* Try to emit an instruction for a specific operation varaition.
6728 OPTAB contains the OP functions.
6729 TARGET is an optional place to return the result. const0_rtx means unused.
6730 MEM is the memory location to operate on.
6731 VAL is the value to use in the operation.
6732 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
6733 MODEL is the memory model, if used.
6734 AFTER is true if the returned result is the value after the operation. */
6736 static rtx
6737 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
6738 rtx val, bool use_memmodel, enum memmodel model, bool after)
6740 machine_mode mode = GET_MODE (mem);
6741 struct expand_operand ops[4];
6742 enum insn_code icode;
6743 int op_counter = 0;
6744 int num_ops;
6746 /* Check to see if there is a result returned. */
6747 if (target == const0_rtx)
6749 if (use_memmodel)
6751 icode = direct_optab_handler (optab->mem_no_result, mode);
6752 create_integer_operand (&ops[2], model);
6753 num_ops = 3;
6755 else
6757 icode = direct_optab_handler (optab->no_result, mode);
6758 num_ops = 2;
6761 /* Otherwise, we need to generate a result. */
6762 else
6764 if (use_memmodel)
6766 icode = direct_optab_handler (after ? optab->mem_fetch_after
6767 : optab->mem_fetch_before, mode);
6768 create_integer_operand (&ops[3], model);
6769 num_ops = 4;
6771 else
6773 icode = optab_handler (after ? optab->fetch_after
6774 : optab->fetch_before, mode);
6775 num_ops = 3;
6777 create_output_operand (&ops[op_counter++], target, mode);
6779 if (icode == CODE_FOR_nothing)
6780 return NULL_RTX;
6782 create_fixed_operand (&ops[op_counter++], mem);
6783 /* VAL may have been promoted to a wider mode. Shrink it if so. */
6784 create_convert_operand_to (&ops[op_counter++], val, mode, true);
6786 if (maybe_expand_insn (icode, num_ops, ops))
6787 return (target == const0_rtx ? const0_rtx : ops[0].value);
6789 return NULL_RTX;
6793 /* This function expands an atomic fetch_OP or OP_fetch operation:
6794 TARGET is an option place to stick the return value. const0_rtx indicates
6795 the result is unused.
6796 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6797 CODE is the operation being performed (OP)
6798 MEMMODEL is the memory model variant to use.
6799 AFTER is true to return the result of the operation (OP_fetch).
6800 AFTER is false to return the value before the operation (fetch_OP).
6802 This function will *only* generate instructions if there is a direct
6803 optab. No compare and swap loops or libcalls will be generated. */
6805 static rtx
6806 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
6807 enum rtx_code code, enum memmodel model,
6808 bool after)
6810 machine_mode mode = GET_MODE (mem);
6811 struct atomic_op_functions optab;
6812 rtx result;
6813 bool unused_result = (target == const0_rtx);
6815 get_atomic_op_for_code (&optab, code);
6817 /* Check to see if there are any better instructions. */
6818 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
6819 if (result)
6820 return result;
6822 /* Check for the case where the result isn't used and try those patterns. */
6823 if (unused_result)
6825 /* Try the memory model variant first. */
6826 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
6827 if (result)
6828 return result;
6830 /* Next try the old style withuot a memory model. */
6831 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
6832 if (result)
6833 return result;
6835 /* There is no no-result pattern, so try patterns with a result. */
6836 target = NULL_RTX;
6839 /* Try the __atomic version. */
6840 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
6841 if (result)
6842 return result;
6844 /* Try the older __sync version. */
6845 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
6846 if (result)
6847 return result;
6849 /* If the fetch value can be calculated from the other variation of fetch,
6850 try that operation. */
6851 if (after || unused_result || optab.reverse_code != UNKNOWN)
6853 /* Try the __atomic version, then the older __sync version. */
6854 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
6855 if (!result)
6856 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
6858 if (result)
6860 /* If the result isn't used, no need to do compensation code. */
6861 if (unused_result)
6862 return result;
6864 /* Issue compensation code. Fetch_after == fetch_before OP val.
6865 Fetch_before == after REVERSE_OP val. */
6866 if (!after)
6867 code = optab.reverse_code;
6868 if (code == NOT)
6870 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
6871 true, OPTAB_LIB_WIDEN);
6872 result = expand_simple_unop (mode, NOT, result, target, true);
6874 else
6875 result = expand_simple_binop (mode, code, result, val, target,
6876 true, OPTAB_LIB_WIDEN);
6877 return result;
6881 /* No direct opcode can be generated. */
6882 return NULL_RTX;
6887 /* This function expands an atomic fetch_OP or OP_fetch operation:
6888 TARGET is an option place to stick the return value. const0_rtx indicates
6889 the result is unused.
6890 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6891 CODE is the operation being performed (OP)
6892 MEMMODEL is the memory model variant to use.
6893 AFTER is true to return the result of the operation (OP_fetch).
6894 AFTER is false to return the value before the operation (fetch_OP). */
6896 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
6897 enum memmodel model, bool after)
6899 machine_mode mode = GET_MODE (mem);
6900 rtx result;
6901 bool unused_result = (target == const0_rtx);
6903 /* If loads are not atomic for the required size and we are not called to
6904 provide a __sync builtin, do not do anything so that we stay consistent
6905 with atomic loads of the same size. */
6906 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6907 return NULL_RTX;
6909 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
6910 after);
6912 if (result)
6913 return result;
6915 /* Add/sub can be implemented by doing the reverse operation with -(val). */
6916 if (code == PLUS || code == MINUS)
6918 rtx tmp;
6919 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
6921 start_sequence ();
6922 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
6923 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
6924 model, after);
6925 if (result)
6927 /* PLUS worked so emit the insns and return. */
6928 tmp = get_insns ();
6929 end_sequence ();
6930 emit_insn (tmp);
6931 return result;
6934 /* PLUS did not work, so throw away the negation code and continue. */
6935 end_sequence ();
6938 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
6939 if (!can_compare_and_swap_p (mode, false))
6941 rtx libfunc;
6942 bool fixup = false;
6943 enum rtx_code orig_code = code;
6944 struct atomic_op_functions optab;
6946 get_atomic_op_for_code (&optab, code);
6947 libfunc = optab_libfunc (after ? optab.fetch_after
6948 : optab.fetch_before, mode);
6949 if (libfunc == NULL
6950 && (after || unused_result || optab.reverse_code != UNKNOWN))
6952 fixup = true;
6953 if (!after)
6954 code = optab.reverse_code;
6955 libfunc = optab_libfunc (after ? optab.fetch_before
6956 : optab.fetch_after, mode);
6958 if (libfunc != NULL)
6960 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6961 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
6962 addr, ptr_mode, val, mode);
6964 if (!unused_result && fixup)
6965 result = expand_simple_binop (mode, code, result, val, target,
6966 true, OPTAB_LIB_WIDEN);
6967 return result;
6970 /* We need the original code for any further attempts. */
6971 code = orig_code;
6974 /* If nothing else has succeeded, default to a compare and swap loop. */
6975 if (can_compare_and_swap_p (mode, true))
6977 rtx_insn *insn;
6978 rtx t0 = gen_reg_rtx (mode), t1;
6980 start_sequence ();
6982 /* If the result is used, get a register for it. */
6983 if (!unused_result)
6985 if (!target || !register_operand (target, mode))
6986 target = gen_reg_rtx (mode);
6987 /* If fetch_before, copy the value now. */
6988 if (!after)
6989 emit_move_insn (target, t0);
6991 else
6992 target = const0_rtx;
6994 t1 = t0;
6995 if (code == NOT)
6997 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
6998 true, OPTAB_LIB_WIDEN);
6999 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
7001 else
7002 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
7003 OPTAB_LIB_WIDEN);
7005 /* For after, copy the value now. */
7006 if (!unused_result && after)
7007 emit_move_insn (target, t1);
7008 insn = get_insns ();
7009 end_sequence ();
7011 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7012 return target;
7015 return NULL_RTX;
7018 /* Return true if OPERAND is suitable for operand number OPNO of
7019 instruction ICODE. */
7021 bool
7022 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
7024 return (!insn_data[(int) icode].operand[opno].predicate
7025 || (insn_data[(int) icode].operand[opno].predicate
7026 (operand, insn_data[(int) icode].operand[opno].mode)));
7029 /* TARGET is a target of a multiword operation that we are going to
7030 implement as a series of word-mode operations. Return true if
7031 TARGET is suitable for this purpose. */
7033 bool
7034 valid_multiword_target_p (rtx target)
7036 machine_mode mode;
7037 int i, size;
7039 mode = GET_MODE (target);
7040 if (!GET_MODE_SIZE (mode).is_constant (&size))
7041 return false;
7042 for (i = 0; i < size; i += UNITS_PER_WORD)
7043 if (!validate_subreg (word_mode, mode, target, i))
7044 return false;
7045 return true;
7048 /* Make OP describe an input operand that has value INTVAL and that has
7049 no inherent mode. This function should only be used for operands that
7050 are always expand-time constants. The backend may request that INTVAL
7051 be copied into a different kind of rtx, but it must specify the mode
7052 of that rtx if so. */
7054 void
7055 create_integer_operand (struct expand_operand *op, poly_int64 intval)
7057 create_expand_operand (op, EXPAND_INTEGER,
7058 gen_int_mode (intval, MAX_MODE_INT),
7059 VOIDmode, false, intval);
7062 /* Like maybe_legitimize_operand, but do not change the code of the
7063 current rtx value. */
7065 static bool
7066 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
7067 struct expand_operand *op)
7069 /* See if the operand matches in its current form. */
7070 if (insn_operand_matches (icode, opno, op->value))
7071 return true;
7073 /* If the operand is a memory whose address has no side effects,
7074 try forcing the address into a non-virtual pseudo register.
7075 The check for side effects is important because copy_to_mode_reg
7076 cannot handle things like auto-modified addresses. */
7077 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
7079 rtx addr, mem;
7081 mem = op->value;
7082 addr = XEXP (mem, 0);
7083 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
7084 && !side_effects_p (addr))
7086 rtx_insn *last;
7087 machine_mode mode;
7089 last = get_last_insn ();
7090 mode = get_address_mode (mem);
7091 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
7092 if (insn_operand_matches (icode, opno, mem))
7094 op->value = mem;
7095 return true;
7097 delete_insns_since (last);
7101 return false;
7104 /* Try to make OP match operand OPNO of instruction ICODE. Return true
7105 on success, storing the new operand value back in OP. */
7107 static bool
7108 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
7109 struct expand_operand *op)
7111 machine_mode mode, imode;
7112 bool old_volatile_ok, result;
7114 mode = op->mode;
7115 switch (op->type)
7117 case EXPAND_FIXED:
7118 old_volatile_ok = volatile_ok;
7119 volatile_ok = true;
7120 result = maybe_legitimize_operand_same_code (icode, opno, op);
7121 volatile_ok = old_volatile_ok;
7122 return result;
7124 case EXPAND_OUTPUT:
7125 gcc_assert (mode != VOIDmode);
7126 if (op->value
7127 && op->value != const0_rtx
7128 && GET_MODE (op->value) == mode
7129 && maybe_legitimize_operand_same_code (icode, opno, op))
7130 return true;
7132 op->value = gen_reg_rtx (mode);
7133 op->target = 0;
7134 break;
7136 case EXPAND_INPUT:
7137 input:
7138 gcc_assert (mode != VOIDmode);
7139 gcc_assert (GET_MODE (op->value) == VOIDmode
7140 || GET_MODE (op->value) == mode);
7141 if (maybe_legitimize_operand_same_code (icode, opno, op))
7142 return true;
7144 op->value = copy_to_mode_reg (mode, op->value);
7145 break;
7147 case EXPAND_CONVERT_TO:
7148 gcc_assert (mode != VOIDmode);
7149 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
7150 goto input;
7152 case EXPAND_CONVERT_FROM:
7153 if (GET_MODE (op->value) != VOIDmode)
7154 mode = GET_MODE (op->value);
7155 else
7156 /* The caller must tell us what mode this value has. */
7157 gcc_assert (mode != VOIDmode);
7159 imode = insn_data[(int) icode].operand[opno].mode;
7160 if (imode != VOIDmode && imode != mode)
7162 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
7163 mode = imode;
7165 goto input;
7167 case EXPAND_ADDRESS:
7168 op->value = convert_memory_address (as_a <scalar_int_mode> (mode),
7169 op->value);
7170 goto input;
7172 case EXPAND_INTEGER:
7173 mode = insn_data[(int) icode].operand[opno].mode;
7174 if (mode != VOIDmode
7175 && known_eq (trunc_int_for_mode (op->int_value, mode),
7176 op->int_value))
7178 op->value = gen_int_mode (op->int_value, mode);
7179 goto input;
7181 break;
7183 return insn_operand_matches (icode, opno, op->value);
7186 /* Make OP describe an input operand that should have the same value
7187 as VALUE, after any mode conversion that the target might request.
7188 TYPE is the type of VALUE. */
7190 void
7191 create_convert_operand_from_type (struct expand_operand *op,
7192 rtx value, tree type)
7194 create_convert_operand_from (op, value, TYPE_MODE (type),
7195 TYPE_UNSIGNED (type));
7198 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
7199 of instruction ICODE. Return true on success, leaving the new operand
7200 values in the OPS themselves. Emit no code on failure. */
7202 bool
7203 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
7204 unsigned int nops, struct expand_operand *ops)
7206 rtx_insn *last;
7207 unsigned int i;
7209 last = get_last_insn ();
7210 for (i = 0; i < nops; i++)
7211 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
7213 delete_insns_since (last);
7214 return false;
7216 return true;
7219 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
7220 as its operands. Return the instruction pattern on success,
7221 and emit any necessary set-up code. Return null and emit no
7222 code on failure. */
7224 rtx_insn *
7225 maybe_gen_insn (enum insn_code icode, unsigned int nops,
7226 struct expand_operand *ops)
7228 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
7229 if (!maybe_legitimize_operands (icode, 0, nops, ops))
7230 return NULL;
7232 switch (nops)
7234 case 1:
7235 return GEN_FCN (icode) (ops[0].value);
7236 case 2:
7237 return GEN_FCN (icode) (ops[0].value, ops[1].value);
7238 case 3:
7239 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
7240 case 4:
7241 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7242 ops[3].value);
7243 case 5:
7244 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7245 ops[3].value, ops[4].value);
7246 case 6:
7247 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7248 ops[3].value, ops[4].value, ops[5].value);
7249 case 7:
7250 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7251 ops[3].value, ops[4].value, ops[5].value,
7252 ops[6].value);
7253 case 8:
7254 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7255 ops[3].value, ops[4].value, ops[5].value,
7256 ops[6].value, ops[7].value);
7257 case 9:
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 ops[6].value, ops[7].value, ops[8].value);
7262 gcc_unreachable ();
7265 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
7266 as its operands. Return true on success and emit no code on failure. */
7268 bool
7269 maybe_expand_insn (enum insn_code icode, unsigned int nops,
7270 struct expand_operand *ops)
7272 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7273 if (pat)
7275 emit_insn (pat);
7276 return true;
7278 return false;
7281 /* Like maybe_expand_insn, but for jumps. */
7283 bool
7284 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
7285 struct expand_operand *ops)
7287 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7288 if (pat)
7290 emit_jump_insn (pat);
7291 return true;
7293 return false;
7296 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
7297 as its operands. */
7299 void
7300 expand_insn (enum insn_code icode, unsigned int nops,
7301 struct expand_operand *ops)
7303 if (!maybe_expand_insn (icode, nops, ops))
7304 gcc_unreachable ();
7307 /* Like expand_insn, but for jumps. */
7309 void
7310 expand_jump_insn (enum insn_code icode, unsigned int nops,
7311 struct expand_operand *ops)
7313 if (!maybe_expand_jump_insn (icode, nops, ops))
7314 gcc_unreachable ();