Skip gcc.dg/guality/example.c on hppa-linux.
[official-gcc.git] / gcc / optabs.c
blobca00bbda10767a1216ac8eccaa959999d4177c94
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2021 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 "optabs.h"
32 #include "expmed.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"
47 #include "internal-fn.h"
48 #include "langhooks.h"
50 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
51 machine_mode *);
52 static rtx expand_unop_direct (machine_mode, optab, rtx, rtx, int);
53 static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
55 /* Debug facility for use in GDB. */
56 void debug_optab_libfuncs (void);
58 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
59 the result of operation CODE applied to OP0 (and OP1 if it is a binary
60 operation). OP0_MODE is OP0's mode.
62 If the last insn does not set TARGET, don't do anything, but return 1.
64 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
65 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
66 try again, ensuring that TARGET is not one of the operands. */
68 static int
69 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0,
70 rtx op1, machine_mode op0_mode)
72 rtx_insn *last_insn;
73 rtx set;
74 rtx note;
76 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
78 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
79 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
80 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
81 && GET_RTX_CLASS (code) != RTX_COMPARE
82 && GET_RTX_CLASS (code) != RTX_UNARY)
83 return 1;
85 if (GET_CODE (target) == ZERO_EXTRACT)
86 return 1;
88 for (last_insn = insns;
89 NEXT_INSN (last_insn) != NULL_RTX;
90 last_insn = NEXT_INSN (last_insn))
93 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
94 a value changing in the insn, so the note would be invalid for CSE. */
95 if (reg_overlap_mentioned_p (target, op0)
96 || (op1 && reg_overlap_mentioned_p (target, op1)))
98 if (MEM_P (target)
99 && (rtx_equal_p (target, op0)
100 || (op1 && rtx_equal_p (target, op1))))
102 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
103 over expanding it as temp = MEM op X, MEM = temp. If the target
104 supports MEM = MEM op X instructions, it is sometimes too hard
105 to reconstruct that form later, especially if X is also a memory,
106 and due to multiple occurrences of addresses the address might
107 be forced into register unnecessarily.
108 Note that not emitting the REG_EQUIV note might inhibit
109 CSE in some cases. */
110 set = single_set (last_insn);
111 if (set
112 && GET_CODE (SET_SRC (set)) == code
113 && MEM_P (SET_DEST (set))
114 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
115 || (op1 && rtx_equal_p (SET_DEST (set),
116 XEXP (SET_SRC (set), 1)))))
117 return 1;
119 return 0;
122 set = set_for_reg_notes (last_insn);
123 if (set == NULL_RTX)
124 return 1;
126 if (! rtx_equal_p (SET_DEST (set), target)
127 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
128 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
129 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
130 return 1;
132 if (GET_RTX_CLASS (code) == RTX_UNARY)
133 switch (code)
135 case FFS:
136 case CLZ:
137 case CTZ:
138 case CLRSB:
139 case POPCOUNT:
140 case PARITY:
141 case BSWAP:
142 if (op0_mode != VOIDmode && GET_MODE (target) != op0_mode)
144 note = gen_rtx_fmt_e (code, op0_mode, copy_rtx (op0));
145 if (GET_MODE_UNIT_SIZE (op0_mode)
146 > GET_MODE_UNIT_SIZE (GET_MODE (target)))
147 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
148 note, op0_mode);
149 else
150 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
151 note, op0_mode);
152 break;
154 /* FALLTHRU */
155 default:
156 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
157 break;
159 else
160 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
162 set_unique_reg_note (last_insn, REG_EQUAL, note);
164 return 1;
167 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
168 for a widening operation would be. In most cases this would be OP0, but if
169 that's a constant it'll be VOIDmode, which isn't useful. */
171 static machine_mode
172 widened_mode (machine_mode to_mode, rtx op0, rtx op1)
174 machine_mode m0 = GET_MODE (op0);
175 machine_mode m1 = GET_MODE (op1);
176 machine_mode result;
178 if (m0 == VOIDmode && m1 == VOIDmode)
179 return to_mode;
180 else if (m0 == VOIDmode || GET_MODE_UNIT_SIZE (m0) < GET_MODE_UNIT_SIZE (m1))
181 result = m1;
182 else
183 result = m0;
185 if (GET_MODE_UNIT_SIZE (result) > GET_MODE_UNIT_SIZE (to_mode))
186 return to_mode;
188 return result;
191 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
192 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
193 not actually do a sign-extend or zero-extend, but can leave the
194 higher-order bits of the result rtx undefined, for example, in the case
195 of logical operations, but not right shifts. */
197 static rtx
198 widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
199 int unsignedp, int no_extend)
201 rtx result;
202 scalar_int_mode int_mode;
204 /* If we don't have to extend and this is a constant, return it. */
205 if (no_extend && GET_MODE (op) == VOIDmode)
206 return op;
208 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
209 extend since it will be more efficient to do so unless the signedness of
210 a promoted object differs from our extension. */
211 if (! no_extend
212 || !is_a <scalar_int_mode> (mode, &int_mode)
213 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
214 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
215 return convert_modes (mode, oldmode, op, unsignedp);
217 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
218 SUBREG. */
219 if (GET_MODE_SIZE (int_mode) <= UNITS_PER_WORD)
220 return gen_lowpart (int_mode, force_reg (GET_MODE (op), op));
222 /* Otherwise, get an object of MODE, clobber it, and set the low-order
223 part to OP. */
225 result = gen_reg_rtx (int_mode);
226 emit_clobber (result);
227 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
228 return result;
231 /* Expand vector widening operations.
233 There are two different classes of operations handled here:
234 1) Operations whose result is wider than all the arguments to the operation.
235 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
236 In this case OP0 and optionally OP1 would be initialized,
237 but WIDE_OP wouldn't (not relevant for this case).
238 2) Operations whose result is of the same size as the last argument to the
239 operation, but wider than all the other arguments to the operation.
240 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
241 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
243 E.g, when called to expand the following operations, this is how
244 the arguments will be initialized:
245 nops OP0 OP1 WIDE_OP
246 widening-sum 2 oprnd0 - oprnd1
247 widening-dot-product 3 oprnd0 oprnd1 oprnd2
248 widening-mult 2 oprnd0 oprnd1 -
249 type-promotion (vec-unpack) 1 oprnd0 - - */
252 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
253 rtx target, int unsignedp)
255 class expand_operand eops[4];
256 tree oprnd0, oprnd1, oprnd2;
257 machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
258 optab widen_pattern_optab;
259 enum insn_code icode;
260 int nops = TREE_CODE_LENGTH (ops->code);
261 int op;
262 bool sbool = false;
264 oprnd0 = ops->op0;
265 if (nops >= 2)
266 oprnd1 = ops->op1;
267 if (nops >= 3)
268 oprnd2 = ops->op2;
270 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
271 if (ops->code == VEC_UNPACK_FIX_TRUNC_HI_EXPR
272 || ops->code == VEC_UNPACK_FIX_TRUNC_LO_EXPR)
273 /* The sign is from the result type rather than operand's type
274 for these ops. */
275 widen_pattern_optab
276 = optab_for_tree_code (ops->code, ops->type, optab_default);
277 else if ((ops->code == VEC_UNPACK_HI_EXPR
278 || ops->code == VEC_UNPACK_LO_EXPR)
279 && VECTOR_BOOLEAN_TYPE_P (ops->type)
280 && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (oprnd0))
281 && TYPE_MODE (ops->type) == TYPE_MODE (TREE_TYPE (oprnd0))
282 && SCALAR_INT_MODE_P (TYPE_MODE (ops->type)))
284 /* For VEC_UNPACK_{LO,HI}_EXPR if the mode of op0 and result is
285 the same scalar mode for VECTOR_BOOLEAN_TYPE_P vectors, use
286 vec_unpacks_sbool_{lo,hi}_optab, so that we can pass in
287 the pattern number of elements in the wider vector. */
288 widen_pattern_optab
289 = (ops->code == VEC_UNPACK_HI_EXPR
290 ? vec_unpacks_sbool_hi_optab : vec_unpacks_sbool_lo_optab);
291 sbool = true;
293 else if (ops->code == DOT_PROD_EXPR)
295 enum optab_subtype subtype = optab_default;
296 signop sign1 = TYPE_SIGN (TREE_TYPE (oprnd0));
297 signop sign2 = TYPE_SIGN (TREE_TYPE (oprnd1));
298 if (sign1 == sign2)
300 else if (sign1 == SIGNED && sign2 == UNSIGNED)
302 subtype = optab_vector_mixed_sign;
303 /* Same as optab_vector_mixed_sign but flip the operands. */
304 std::swap (op0, op1);
306 else if (sign1 == UNSIGNED && sign2 == SIGNED)
307 subtype = optab_vector_mixed_sign;
308 else
309 gcc_unreachable ();
311 widen_pattern_optab
312 = optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), subtype);
314 else
315 widen_pattern_optab
316 = optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
317 if (ops->code == WIDEN_MULT_PLUS_EXPR
318 || ops->code == WIDEN_MULT_MINUS_EXPR)
319 icode = find_widening_optab_handler (widen_pattern_optab,
320 TYPE_MODE (TREE_TYPE (ops->op2)),
321 tmode0);
322 else
323 icode = optab_handler (widen_pattern_optab, tmode0);
324 gcc_assert (icode != CODE_FOR_nothing);
326 if (nops >= 2)
327 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
328 else if (sbool)
330 nops = 2;
331 op1 = GEN_INT (TYPE_VECTOR_SUBPARTS (TREE_TYPE (oprnd0)).to_constant ());
332 tmode1 = tmode0;
335 /* The last operand is of a wider mode than the rest of the operands. */
336 if (nops == 2)
337 wmode = tmode1;
338 else if (nops == 3)
340 gcc_assert (tmode1 == tmode0);
341 gcc_assert (op1);
342 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
345 op = 0;
346 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
347 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
348 if (op1)
349 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
350 if (wide_op)
351 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
352 expand_insn (icode, op, eops);
353 return eops[0].value;
356 /* Generate code to perform an operation specified by TERNARY_OPTAB
357 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
359 UNSIGNEDP is for the case where we have to widen the operands
360 to perform the operation. It says to use zero-extension.
362 If TARGET is nonzero, the value
363 is generated there, if it is convenient to do so.
364 In all cases an rtx is returned for the locus of the value;
365 this may or may not be TARGET. */
368 expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
369 rtx op1, rtx op2, rtx target, int unsignedp)
371 class expand_operand ops[4];
372 enum insn_code icode = optab_handler (ternary_optab, mode);
374 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
376 create_output_operand (&ops[0], target, mode);
377 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
378 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
379 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
380 expand_insn (icode, 4, ops);
381 return ops[0].value;
385 /* Like expand_binop, but return a constant rtx if the result can be
386 calculated at compile time. The arguments and return value are
387 otherwise the same as for expand_binop. */
390 simplify_expand_binop (machine_mode mode, optab binoptab,
391 rtx op0, rtx op1, rtx target, int unsignedp,
392 enum optab_methods methods)
394 if (CONSTANT_P (op0) && CONSTANT_P (op1))
396 rtx x = simplify_binary_operation (optab_to_code (binoptab),
397 mode, op0, op1);
398 if (x)
399 return x;
402 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
405 /* Like simplify_expand_binop, but always put the result in TARGET.
406 Return true if the expansion succeeded. */
408 bool
409 force_expand_binop (machine_mode mode, optab binoptab,
410 rtx op0, rtx op1, rtx target, int unsignedp,
411 enum optab_methods methods)
413 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
414 target, unsignedp, methods);
415 if (x == 0)
416 return false;
417 if (x != target)
418 emit_move_insn (target, x);
419 return true;
422 /* Create a new vector value in VMODE with all elements set to OP. The
423 mode of OP must be the element mode of VMODE. If OP is a constant,
424 then the return value will be a constant. */
427 expand_vector_broadcast (machine_mode vmode, rtx op)
429 int n;
430 rtvec vec;
432 gcc_checking_assert (VECTOR_MODE_P (vmode));
434 if (valid_for_const_vector_p (vmode, op))
435 return gen_const_vec_duplicate (vmode, op);
437 insn_code icode = optab_handler (vec_duplicate_optab, vmode);
438 if (icode != CODE_FOR_nothing)
440 class expand_operand ops[2];
441 create_output_operand (&ops[0], NULL_RTX, vmode);
442 create_input_operand (&ops[1], op, GET_MODE (op));
443 expand_insn (icode, 2, ops);
444 return ops[0].value;
447 if (!GET_MODE_NUNITS (vmode).is_constant (&n))
448 return NULL;
450 /* ??? If the target doesn't have a vec_init, then we have no easy way
451 of performing this operation. Most of this sort of generic support
452 is hidden away in the vector lowering support in gimple. */
453 icode = convert_optab_handler (vec_init_optab, vmode,
454 GET_MODE_INNER (vmode));
455 if (icode == CODE_FOR_nothing)
456 return NULL;
458 vec = rtvec_alloc (n);
459 for (int i = 0; i < n; ++i)
460 RTVEC_ELT (vec, i) = op;
461 rtx ret = gen_reg_rtx (vmode);
462 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
464 return ret;
467 /* This subroutine of expand_doubleword_shift handles the cases in which
468 the effective shift value is >= BITS_PER_WORD. The arguments and return
469 value are the same as for the parent routine, except that SUPERWORD_OP1
470 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
471 INTO_TARGET may be null if the caller has decided to calculate it. */
473 static bool
474 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
475 rtx outof_target, rtx into_target,
476 int unsignedp, enum optab_methods methods)
478 if (into_target != 0)
479 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
480 into_target, unsignedp, methods))
481 return false;
483 if (outof_target != 0)
485 /* For a signed right shift, we must fill OUTOF_TARGET with copies
486 of the sign bit, otherwise we must fill it with zeros. */
487 if (binoptab != ashr_optab)
488 emit_move_insn (outof_target, CONST0_RTX (word_mode));
489 else
490 if (!force_expand_binop (word_mode, binoptab, outof_input,
491 gen_int_shift_amount (word_mode,
492 BITS_PER_WORD - 1),
493 outof_target, unsignedp, methods))
494 return false;
496 return true;
499 /* This subroutine of expand_doubleword_shift handles the cases in which
500 the effective shift value is < BITS_PER_WORD. The arguments and return
501 value are the same as for the parent routine. */
503 static bool
504 expand_subword_shift (scalar_int_mode op1_mode, optab binoptab,
505 rtx outof_input, rtx into_input, rtx op1,
506 rtx outof_target, rtx into_target,
507 int unsignedp, enum optab_methods methods,
508 unsigned HOST_WIDE_INT shift_mask)
510 optab reverse_unsigned_shift, unsigned_shift;
511 rtx tmp, carries;
513 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
514 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
516 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
517 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
518 the opposite direction to BINOPTAB. */
519 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
521 carries = outof_input;
522 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
523 op1_mode), op1_mode);
524 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
525 0, true, methods);
527 else
529 /* We must avoid shifting by BITS_PER_WORD bits since that is either
530 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
531 has unknown behavior. Do a single shift first, then shift by the
532 remainder. It's OK to use ~OP1 as the remainder if shift counts
533 are truncated to the mode size. */
534 carries = expand_binop (word_mode, reverse_unsigned_shift,
535 outof_input, const1_rtx, 0, unsignedp, methods);
536 if (shift_mask == BITS_PER_WORD - 1)
538 tmp = immed_wide_int_const
539 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
540 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
541 0, true, methods);
543 else
545 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
546 op1_mode), op1_mode);
547 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
548 0, true, methods);
551 if (tmp == 0 || carries == 0)
552 return false;
553 carries = expand_binop (word_mode, reverse_unsigned_shift,
554 carries, tmp, 0, unsignedp, methods);
555 if (carries == 0)
556 return false;
558 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
559 so the result can go directly into INTO_TARGET if convenient. */
560 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
561 into_target, unsignedp, methods);
562 if (tmp == 0)
563 return false;
565 /* Now OR in the bits carried over from OUTOF_INPUT. */
566 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
567 into_target, unsignedp, methods))
568 return false;
570 /* Use a standard word_mode shift for the out-of half. */
571 if (outof_target != 0)
572 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
573 outof_target, unsignedp, methods))
574 return false;
576 return true;
580 /* Try implementing expand_doubleword_shift using conditional moves.
581 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
582 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
583 are the shift counts to use in the former and latter case. All other
584 arguments are the same as the parent routine. */
586 static bool
587 expand_doubleword_shift_condmove (scalar_int_mode op1_mode, optab binoptab,
588 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
589 rtx outof_input, rtx into_input,
590 rtx subword_op1, rtx superword_op1,
591 rtx outof_target, rtx into_target,
592 int unsignedp, enum optab_methods methods,
593 unsigned HOST_WIDE_INT shift_mask)
595 rtx outof_superword, into_superword;
597 /* Put the superword version of the output into OUTOF_SUPERWORD and
598 INTO_SUPERWORD. */
599 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
600 if (outof_target != 0 && subword_op1 == superword_op1)
602 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
603 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
604 into_superword = outof_target;
605 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
606 outof_superword, 0, unsignedp, methods))
607 return false;
609 else
611 into_superword = gen_reg_rtx (word_mode);
612 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
613 outof_superword, into_superword,
614 unsignedp, methods))
615 return false;
618 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
619 if (!expand_subword_shift (op1_mode, binoptab,
620 outof_input, into_input, subword_op1,
621 outof_target, into_target,
622 unsignedp, methods, shift_mask))
623 return false;
625 /* Select between them. Do the INTO half first because INTO_SUPERWORD
626 might be the current value of OUTOF_TARGET. */
627 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
628 into_target, into_superword, word_mode, false))
629 return false;
631 if (outof_target != 0)
632 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
633 outof_target, outof_superword,
634 word_mode, false))
635 return false;
637 return true;
640 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
641 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
642 input operand; the shift moves bits in the direction OUTOF_INPUT->
643 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
644 of the target. OP1 is the shift count and OP1_MODE is its mode.
645 If OP1 is constant, it will have been truncated as appropriate
646 and is known to be nonzero.
648 If SHIFT_MASK is zero, the result of word shifts is undefined when the
649 shift count is outside the range [0, BITS_PER_WORD). This routine must
650 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
652 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
653 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
654 fill with zeros or sign bits as appropriate.
656 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
657 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
658 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
659 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
660 are undefined.
662 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
663 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
664 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
665 function wants to calculate it itself.
667 Return true if the shift could be successfully synthesized. */
669 static bool
670 expand_doubleword_shift (scalar_int_mode op1_mode, optab binoptab,
671 rtx outof_input, rtx into_input, rtx op1,
672 rtx outof_target, rtx into_target,
673 int unsignedp, enum optab_methods methods,
674 unsigned HOST_WIDE_INT shift_mask)
676 rtx superword_op1, tmp, cmp1, cmp2;
677 enum rtx_code cmp_code;
679 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
680 fill the result with sign or zero bits as appropriate. If so, the value
681 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
682 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
683 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
685 This isn't worthwhile for constant shifts since the optimizers will
686 cope better with in-range shift counts. */
687 if (shift_mask >= BITS_PER_WORD
688 && outof_target != 0
689 && !CONSTANT_P (op1))
691 if (!expand_doubleword_shift (op1_mode, binoptab,
692 outof_input, into_input, op1,
693 0, into_target,
694 unsignedp, methods, shift_mask))
695 return false;
696 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
697 outof_target, unsignedp, methods))
698 return false;
699 return true;
702 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
703 is true when the effective shift value is less than BITS_PER_WORD.
704 Set SUPERWORD_OP1 to the shift count that should be used to shift
705 OUTOF_INPUT into INTO_TARGET when the condition is false. */
706 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
707 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
709 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
710 is a subword shift count. */
711 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
712 0, true, methods);
713 cmp2 = CONST0_RTX (op1_mode);
714 cmp_code = EQ;
715 superword_op1 = op1;
717 else
719 /* Set CMP1 to OP1 - BITS_PER_WORD. */
720 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
721 0, true, methods);
722 cmp2 = CONST0_RTX (op1_mode);
723 cmp_code = LT;
724 superword_op1 = cmp1;
726 if (cmp1 == 0)
727 return false;
729 /* If we can compute the condition at compile time, pick the
730 appropriate subroutine. */
731 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
732 if (tmp != 0 && CONST_INT_P (tmp))
734 if (tmp == const0_rtx)
735 return expand_superword_shift (binoptab, outof_input, superword_op1,
736 outof_target, into_target,
737 unsignedp, methods);
738 else
739 return expand_subword_shift (op1_mode, binoptab,
740 outof_input, into_input, op1,
741 outof_target, into_target,
742 unsignedp, methods, shift_mask);
745 /* Try using conditional moves to generate straight-line code. */
746 if (HAVE_conditional_move)
748 rtx_insn *start = get_last_insn ();
749 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
750 cmp_code, cmp1, cmp2,
751 outof_input, into_input,
752 op1, superword_op1,
753 outof_target, into_target,
754 unsignedp, methods, shift_mask))
755 return true;
756 delete_insns_since (start);
759 /* As a last resort, use branches to select the correct alternative. */
760 rtx_code_label *subword_label = gen_label_rtx ();
761 rtx_code_label *done_label = gen_label_rtx ();
763 NO_DEFER_POP;
764 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
765 0, 0, subword_label,
766 profile_probability::uninitialized ());
767 OK_DEFER_POP;
769 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
770 outof_target, into_target,
771 unsignedp, methods))
772 return false;
774 emit_jump_insn (targetm.gen_jump (done_label));
775 emit_barrier ();
776 emit_label (subword_label);
778 if (!expand_subword_shift (op1_mode, binoptab,
779 outof_input, into_input, op1,
780 outof_target, into_target,
781 unsignedp, methods, shift_mask))
782 return false;
784 emit_label (done_label);
785 return true;
788 /* Subroutine of expand_binop. Perform a double word multiplication of
789 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
790 as the target's word_mode. This function return NULL_RTX if anything
791 goes wrong, in which case it may have already emitted instructions
792 which need to be deleted.
794 If we want to multiply two two-word values and have normal and widening
795 multiplies of single-word values, we can do this with three smaller
796 multiplications.
798 The multiplication proceeds as follows:
799 _______________________
800 [__op0_high_|__op0_low__]
801 _______________________
802 * [__op1_high_|__op1_low__]
803 _______________________________________________
804 _______________________
805 (1) [__op0_low__*__op1_low__]
806 _______________________
807 (2a) [__op0_low__*__op1_high_]
808 _______________________
809 (2b) [__op0_high_*__op1_low__]
810 _______________________
811 (3) [__op0_high_*__op1_high_]
814 This gives a 4-word result. Since we are only interested in the
815 lower 2 words, partial result (3) and the upper words of (2a) and
816 (2b) don't need to be calculated. Hence (2a) and (2b) can be
817 calculated using non-widening multiplication.
819 (1), however, needs to be calculated with an unsigned widening
820 multiplication. If this operation is not directly supported we
821 try using a signed widening multiplication and adjust the result.
822 This adjustment works as follows:
824 If both operands are positive then no adjustment is needed.
826 If the operands have different signs, for example op0_low < 0 and
827 op1_low >= 0, the instruction treats the most significant bit of
828 op0_low as a sign bit instead of a bit with significance
829 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
830 with 2**BITS_PER_WORD - op0_low, and two's complements the
831 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
832 the result.
834 Similarly, if both operands are negative, we need to add
835 (op0_low + op1_low) * 2**BITS_PER_WORD.
837 We use a trick to adjust quickly. We logically shift op0_low right
838 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
839 op0_high (op1_high) before it is used to calculate 2b (2a). If no
840 logical shift exists, we do an arithmetic right shift and subtract
841 the 0 or -1. */
843 static rtx
844 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
845 bool umulp, enum optab_methods methods)
847 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
848 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
849 rtx wordm1 = (umulp ? NULL_RTX
850 : gen_int_shift_amount (word_mode, BITS_PER_WORD - 1));
851 rtx product, adjust, product_high, temp;
853 rtx op0_high = operand_subword_force (op0, high, mode);
854 rtx op0_low = operand_subword_force (op0, low, mode);
855 rtx op1_high = operand_subword_force (op1, high, mode);
856 rtx op1_low = operand_subword_force (op1, low, mode);
858 /* If we're using an unsigned multiply to directly compute the product
859 of the low-order words of the operands and perform any required
860 adjustments of the operands, we begin by trying two more multiplications
861 and then computing the appropriate sum.
863 We have checked above that the required addition is provided.
864 Full-word addition will normally always succeed, especially if
865 it is provided at all, so we don't worry about its failure. The
866 multiplication may well fail, however, so we do handle that. */
868 if (!umulp)
870 /* ??? This could be done with emit_store_flag where available. */
871 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
872 NULL_RTX, 1, methods);
873 if (temp)
874 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
875 NULL_RTX, 0, OPTAB_DIRECT);
876 else
878 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
879 NULL_RTX, 0, methods);
880 if (!temp)
881 return NULL_RTX;
882 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
883 NULL_RTX, 0, OPTAB_DIRECT);
886 if (!op0_high)
887 return NULL_RTX;
890 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
891 NULL_RTX, 0, OPTAB_DIRECT);
892 if (!adjust)
893 return NULL_RTX;
895 /* OP0_HIGH should now be dead. */
897 if (!umulp)
899 /* ??? This could be done with emit_store_flag where available. */
900 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
901 NULL_RTX, 1, methods);
902 if (temp)
903 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
904 NULL_RTX, 0, OPTAB_DIRECT);
905 else
907 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
908 NULL_RTX, 0, methods);
909 if (!temp)
910 return NULL_RTX;
911 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
912 NULL_RTX, 0, OPTAB_DIRECT);
915 if (!op1_high)
916 return NULL_RTX;
919 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
920 NULL_RTX, 0, OPTAB_DIRECT);
921 if (!temp)
922 return NULL_RTX;
924 /* OP1_HIGH should now be dead. */
926 adjust = expand_binop (word_mode, add_optab, adjust, temp,
927 NULL_RTX, 0, OPTAB_DIRECT);
929 if (target && !REG_P (target))
930 target = NULL_RTX;
932 /* *_widen_optab needs to determine operand mode, make sure at least
933 one operand has non-VOID mode. */
934 if (GET_MODE (op0_low) == VOIDmode && GET_MODE (op1_low) == VOIDmode)
935 op0_low = force_reg (word_mode, op0_low);
937 if (umulp)
938 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
939 target, 1, OPTAB_DIRECT);
940 else
941 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
942 target, 1, OPTAB_DIRECT);
944 if (!product)
945 return NULL_RTX;
947 product_high = operand_subword (product, high, 1, mode);
948 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
949 NULL_RTX, 0, OPTAB_DIRECT);
950 emit_move_insn (product_high, adjust);
951 return product;
954 /* Subroutine of expand_binop. Optimize unsigned double-word OP0 % OP1 for
955 constant OP1. If for some bit in [BITS_PER_WORD / 2, BITS_PER_WORD] range
956 (prefer higher bits) ((1w << bit) % OP1) == 1, then the modulo can be
957 computed in word-mode as ((OP0 & (bit - 1)) + ((OP0 >> bit) & (bit - 1))
958 + (OP0 >> (2 * bit))) % OP1. Whether we need to sum 2, 3 or 4 values
959 depends on the bit value, if 2, then carry from the addition needs to be
960 added too, i.e. like:
961 sum += __builtin_add_overflow (low, high, &sum)
963 Optimize signed double-word OP0 % OP1 similarly, just apply some correction
964 factor to the sum before doing unsigned remainder, in the form of
965 sum += (((signed) OP0 >> (2 * BITS_PER_WORD - 1)) & const);
966 then perform unsigned
967 remainder = sum % OP1;
968 and finally
969 remainder += ((signed) OP0 >> (2 * BITS_PER_WORD - 1)) & (1 - OP1); */
971 static rtx
972 expand_doubleword_mod (machine_mode mode, rtx op0, rtx op1, bool unsignedp)
974 if (INTVAL (op1) <= 1 || (INTVAL (op1) & 1) == 0)
975 return NULL_RTX;
977 rtx_insn *last = get_last_insn ();
978 for (int bit = BITS_PER_WORD; bit >= BITS_PER_WORD / 2; bit--)
980 wide_int w = wi::shifted_mask (bit, 1, false, 2 * BITS_PER_WORD);
981 if (wi::ne_p (wi::umod_trunc (w, INTVAL (op1)), 1))
982 continue;
983 rtx sum = NULL_RTX, mask = NULL_RTX;
984 if (bit == BITS_PER_WORD)
986 /* For signed modulo we need to add correction to the sum
987 and that might again overflow. */
988 if (!unsignedp)
989 continue;
990 if (optab_handler (uaddv4_optab, word_mode) == CODE_FOR_nothing)
991 continue;
992 tree wtype = lang_hooks.types.type_for_mode (word_mode, 1);
993 if (wtype == NULL_TREE)
994 continue;
995 tree ctype = build_complex_type (wtype);
996 if (TYPE_MODE (ctype) != GET_MODE_COMPLEX_MODE (word_mode))
997 continue;
998 machine_mode cmode = TYPE_MODE (ctype);
999 rtx op00 = operand_subword_force (op0, 0, mode);
1000 rtx op01 = operand_subword_force (op0, 1, mode);
1001 rtx cres = gen_rtx_CONCAT (cmode, gen_reg_rtx (word_mode),
1002 gen_reg_rtx (word_mode));
1003 tree lhs = make_tree (ctype, cres);
1004 tree arg0 = make_tree (wtype, op00);
1005 tree arg1 = make_tree (wtype, op01);
1006 expand_addsub_overflow (UNKNOWN_LOCATION, PLUS_EXPR, lhs, arg0,
1007 arg1, true, true, true, false, NULL);
1008 sum = expand_simple_binop (word_mode, PLUS, XEXP (cres, 0),
1009 XEXP (cres, 1), NULL_RTX, 1,
1010 OPTAB_DIRECT);
1011 if (sum == NULL_RTX)
1012 return NULL_RTX;
1014 else
1016 /* Code below uses GEN_INT, so we need the masks to be representable
1017 in HOST_WIDE_INTs. */
1018 if (bit >= HOST_BITS_PER_WIDE_INT)
1019 continue;
1020 /* If op0 is e.g. -1 or -2 unsigned, then the 2 additions might
1021 overflow. Consider 64-bit -1ULL for word size 32, if we add
1022 0x7fffffffU + 0x7fffffffU + 3U, it wraps around to 1. */
1023 if (bit == BITS_PER_WORD - 1)
1024 continue;
1026 int count = (2 * BITS_PER_WORD + bit - 1) / bit;
1027 rtx sum_corr = NULL_RTX;
1029 if (!unsignedp)
1031 /* For signed modulo, compute it as unsigned modulo of
1032 sum with a correction added to it if OP0 is negative,
1033 such that the result can be computed as unsigned
1034 remainder + ((OP1 >> (2 * BITS_PER_WORD - 1)) & (1 - OP1). */
1035 w = wi::min_value (2 * BITS_PER_WORD, SIGNED);
1036 wide_int wmod1 = wi::umod_trunc (w, INTVAL (op1));
1037 wide_int wmod2 = wi::smod_trunc (w, INTVAL (op1));
1038 /* wmod2 == -wmod1. */
1039 wmod2 = wmod2 + (INTVAL (op1) - 1);
1040 if (wi::ne_p (wmod1, wmod2))
1042 wide_int wcorr = wmod2 - wmod1;
1043 if (wi::neg_p (w))
1044 wcorr = wcorr + INTVAL (op1);
1045 /* Now verify if the count sums can't overflow, and punt
1046 if they could. */
1047 w = wi::mask (bit, false, 2 * BITS_PER_WORD);
1048 w = w * (count - 1);
1049 w = w + wi::mask (2 * BITS_PER_WORD - (count - 1) * bit,
1050 false, 2 * BITS_PER_WORD);
1051 w = w + wcorr;
1052 w = wi::lrshift (w, BITS_PER_WORD);
1053 if (wi::ne_p (w, 0))
1054 continue;
1056 mask = operand_subword_force (op0, WORDS_BIG_ENDIAN ? 0 : 1,
1057 mode);
1058 mask = expand_simple_binop (word_mode, ASHIFTRT, mask,
1059 GEN_INT (BITS_PER_WORD - 1),
1060 NULL_RTX, 0, OPTAB_DIRECT);
1061 if (mask == NULL_RTX)
1062 return NULL_RTX;
1063 sum_corr = immed_wide_int_const (wcorr, word_mode);
1064 sum_corr = expand_simple_binop (word_mode, AND, mask,
1065 sum_corr, NULL_RTX, 1,
1066 OPTAB_DIRECT);
1067 if (sum_corr == NULL_RTX)
1068 return NULL_RTX;
1072 for (int i = 0; i < count; i++)
1074 rtx v = op0;
1075 if (i)
1076 v = expand_simple_binop (mode, LSHIFTRT, v, GEN_INT (i * bit),
1077 NULL_RTX, 1, OPTAB_DIRECT);
1078 if (v == NULL_RTX)
1079 return NULL_RTX;
1080 v = lowpart_subreg (word_mode, v, mode);
1081 if (v == NULL_RTX)
1082 return NULL_RTX;
1083 if (i != count - 1)
1084 v = expand_simple_binop (word_mode, AND, v,
1085 GEN_INT ((HOST_WIDE_INT_1U << bit)
1086 - 1), NULL_RTX, 1,
1087 OPTAB_DIRECT);
1088 if (v == NULL_RTX)
1089 return NULL_RTX;
1090 if (sum == NULL_RTX)
1091 sum = v;
1092 else
1093 sum = expand_simple_binop (word_mode, PLUS, sum, v, NULL_RTX,
1094 1, OPTAB_DIRECT);
1095 if (sum == NULL_RTX)
1096 return NULL_RTX;
1098 if (sum_corr)
1100 sum = expand_simple_binop (word_mode, PLUS, sum, sum_corr,
1101 NULL_RTX, 1, OPTAB_DIRECT);
1102 if (sum == NULL_RTX)
1103 return NULL_RTX;
1106 rtx remainder = expand_divmod (1, TRUNC_MOD_EXPR, word_mode, sum,
1107 gen_int_mode (INTVAL (op1), word_mode),
1108 NULL_RTX, 1, OPTAB_DIRECT);
1109 if (remainder == NULL_RTX)
1110 return NULL_RTX;
1112 if (!unsignedp)
1114 if (mask == NULL_RTX)
1116 mask = operand_subword_force (op0, WORDS_BIG_ENDIAN ? 0 : 1,
1117 mode);
1118 mask = expand_simple_binop (word_mode, ASHIFTRT, mask,
1119 GEN_INT (BITS_PER_WORD - 1),
1120 NULL_RTX, 0, OPTAB_DIRECT);
1121 if (mask == NULL_RTX)
1122 return NULL_RTX;
1124 mask = expand_simple_binop (word_mode, AND, mask,
1125 gen_int_mode (1 - INTVAL (op1),
1126 word_mode),
1127 NULL_RTX, 1, OPTAB_DIRECT);
1128 if (mask == NULL_RTX)
1129 return NULL_RTX;
1130 remainder = expand_simple_binop (word_mode, PLUS, remainder,
1131 mask, NULL_RTX, 1, OPTAB_DIRECT);
1132 if (remainder == NULL_RTX)
1133 return NULL_RTX;
1136 remainder = convert_modes (mode, word_mode, remainder, unsignedp);
1137 /* Punt if we need any library calls. */
1138 if (last)
1139 last = NEXT_INSN (last);
1140 else
1141 last = get_insns ();
1142 for (; last; last = NEXT_INSN (last))
1143 if (CALL_P (last))
1144 return NULL_RTX;
1145 return remainder;
1147 return NULL_RTX;
1150 /* Similarly to the above function, but compute both quotient and remainder.
1151 Quotient can be computed from the remainder as:
1152 rem = op0 % op1; // Handled using expand_doubleword_mod
1153 quot = (op0 - rem) * inv; // inv is multiplicative inverse of op1 modulo
1154 // 2 * BITS_PER_WORD
1156 We can also handle cases where op1 is a multiple of power of two constant
1157 and constant handled by expand_doubleword_mod.
1158 op11 = 1 << __builtin_ctz (op1);
1159 op12 = op1 / op11;
1160 rem1 = op0 % op12; // Handled using expand_doubleword_mod
1161 quot1 = (op0 - rem1) * inv; // inv is multiplicative inverse of op12 modulo
1162 // 2 * BITS_PER_WORD
1163 rem = (quot1 % op11) * op12 + rem1;
1164 quot = quot1 / op11; */
1167 expand_doubleword_divmod (machine_mode mode, rtx op0, rtx op1, rtx *rem,
1168 bool unsignedp)
1170 *rem = NULL_RTX;
1172 /* Negative dividend should have been optimized into positive,
1173 similarly modulo by 1 and modulo by power of two is optimized
1174 differently too. */
1175 if (INTVAL (op1) <= 1 || pow2p_hwi (INTVAL (op1)))
1176 return NULL_RTX;
1178 rtx op11 = const1_rtx;
1179 rtx op12 = op1;
1180 if ((INTVAL (op1) & 1) == 0)
1182 int bit = ctz_hwi (INTVAL (op1));
1183 op11 = GEN_INT (HOST_WIDE_INT_1 << bit);
1184 op12 = GEN_INT (INTVAL (op1) >> bit);
1187 rtx rem1 = expand_doubleword_mod (mode, op0, op12, unsignedp);
1188 if (rem1 == NULL_RTX)
1189 return NULL_RTX;
1191 int prec = 2 * BITS_PER_WORD;
1192 wide_int a = wide_int::from (INTVAL (op12), prec + 1, UNSIGNED);
1193 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
1194 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
1195 rtx inv = immed_wide_int_const (m, mode);
1197 rtx_insn *last = get_last_insn ();
1198 rtx quot1 = expand_simple_binop (mode, MINUS, op0, rem1,
1199 NULL_RTX, unsignedp, OPTAB_DIRECT);
1200 if (quot1 == NULL_RTX)
1201 return NULL_RTX;
1203 quot1 = expand_simple_binop (mode, MULT, quot1, inv,
1204 NULL_RTX, unsignedp, OPTAB_DIRECT);
1205 if (quot1 == NULL_RTX)
1206 return NULL_RTX;
1208 if (op11 != const1_rtx)
1210 rtx rem2 = expand_divmod (1, TRUNC_MOD_EXPR, mode, quot1, op11,
1211 NULL_RTX, unsignedp, OPTAB_DIRECT);
1212 if (rem2 == NULL_RTX)
1213 return NULL_RTX;
1215 rem2 = expand_simple_binop (mode, MULT, rem2, op12, NULL_RTX,
1216 unsignedp, OPTAB_DIRECT);
1217 if (rem2 == NULL_RTX)
1218 return NULL_RTX;
1220 rem2 = expand_simple_binop (mode, PLUS, rem2, rem1, NULL_RTX,
1221 unsignedp, OPTAB_DIRECT);
1222 if (rem2 == NULL_RTX)
1223 return NULL_RTX;
1225 rtx quot2 = expand_divmod (0, TRUNC_DIV_EXPR, mode, quot1, op11,
1226 NULL_RTX, unsignedp, OPTAB_DIRECT);
1227 if (quot2 == NULL_RTX)
1228 return NULL_RTX;
1230 rem1 = rem2;
1231 quot1 = quot2;
1234 /* Punt if we need any library calls. */
1235 if (last)
1236 last = NEXT_INSN (last);
1237 else
1238 last = get_insns ();
1239 for (; last; last = NEXT_INSN (last))
1240 if (CALL_P (last))
1241 return NULL_RTX;
1243 *rem = rem1;
1244 return quot1;
1247 /* Wrapper around expand_binop which takes an rtx code to specify
1248 the operation to perform, not an optab pointer. All other
1249 arguments are the same. */
1251 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
1252 rtx op1, rtx target, int unsignedp,
1253 enum optab_methods methods)
1255 optab binop = code_to_optab (code);
1256 gcc_assert (binop);
1258 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1261 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1262 binop. Order them according to commutative_operand_precedence and, if
1263 possible, try to put TARGET or a pseudo first. */
1264 static bool
1265 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1267 int op0_prec = commutative_operand_precedence (op0);
1268 int op1_prec = commutative_operand_precedence (op1);
1270 if (op0_prec < op1_prec)
1271 return true;
1273 if (op0_prec > op1_prec)
1274 return false;
1276 /* With equal precedence, both orders are ok, but it is better if the
1277 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1278 if (target == 0 || REG_P (target))
1279 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1280 else
1281 return rtx_equal_p (op1, target);
1284 /* Return true if BINOPTAB implements a shift operation. */
1286 static bool
1287 shift_optab_p (optab binoptab)
1289 switch (optab_to_code (binoptab))
1291 case ASHIFT:
1292 case SS_ASHIFT:
1293 case US_ASHIFT:
1294 case ASHIFTRT:
1295 case LSHIFTRT:
1296 case ROTATE:
1297 case ROTATERT:
1298 return true;
1300 default:
1301 return false;
1305 /* Return true if BINOPTAB implements a commutative binary operation. */
1307 static bool
1308 commutative_optab_p (optab binoptab)
1310 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1311 || binoptab == smul_widen_optab
1312 || binoptab == umul_widen_optab
1313 || binoptab == smul_highpart_optab
1314 || binoptab == umul_highpart_optab);
1317 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1318 optimizing, and if the operand is a constant that costs more than
1319 1 instruction, force the constant into a register and return that
1320 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1322 static rtx
1323 avoid_expensive_constant (machine_mode mode, optab binoptab,
1324 int opn, rtx x, bool unsignedp)
1326 bool speed = optimize_insn_for_speed_p ();
1328 if (mode != VOIDmode
1329 && optimize
1330 && CONSTANT_P (x)
1331 && (rtx_cost (x, mode, optab_to_code (binoptab), opn, speed)
1332 > set_src_cost (x, mode, speed)))
1334 if (CONST_INT_P (x))
1336 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1337 if (intval != INTVAL (x))
1338 x = GEN_INT (intval);
1340 else
1341 x = convert_modes (mode, VOIDmode, x, unsignedp);
1342 x = force_reg (mode, x);
1344 return x;
1347 /* Helper function for expand_binop: handle the case where there
1348 is an insn ICODE that directly implements the indicated operation.
1349 Returns null if this is not possible. */
1350 static rtx
1351 expand_binop_directly (enum insn_code icode, machine_mode mode, optab binoptab,
1352 rtx op0, rtx op1,
1353 rtx target, int unsignedp, enum optab_methods methods,
1354 rtx_insn *last)
1356 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1357 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1358 machine_mode mode0, mode1, tmp_mode;
1359 class expand_operand ops[3];
1360 bool commutative_p;
1361 rtx_insn *pat;
1362 rtx xop0 = op0, xop1 = op1;
1363 bool canonicalize_op1 = false;
1365 /* If it is a commutative operator and the modes would match
1366 if we would swap the operands, we can save the conversions. */
1367 commutative_p = commutative_optab_p (binoptab);
1368 if (commutative_p
1369 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1370 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode0)
1371 std::swap (xop0, xop1);
1373 /* If we are optimizing, force expensive constants into a register. */
1374 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1375 if (!shift_optab_p (binoptab))
1376 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1377 else
1378 /* Shifts and rotates often use a different mode for op1 from op0;
1379 for VOIDmode constants we don't know the mode, so force it
1380 to be canonicalized using convert_modes. */
1381 canonicalize_op1 = true;
1383 /* In case the insn wants input operands in modes different from
1384 those of the actual operands, convert the operands. It would
1385 seem that we don't need to convert CONST_INTs, but we do, so
1386 that they're properly zero-extended, sign-extended or truncated
1387 for their mode. */
1389 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1390 if (xmode0 != VOIDmode && xmode0 != mode0)
1392 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1393 mode0 = xmode0;
1396 mode1 = ((GET_MODE (xop1) != VOIDmode || canonicalize_op1)
1397 ? GET_MODE (xop1) : mode);
1398 if (xmode1 != VOIDmode && xmode1 != mode1)
1400 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1401 mode1 = xmode1;
1404 /* If operation is commutative,
1405 try to make the first operand a register.
1406 Even better, try to make it the same as the target.
1407 Also try to make the last operand a constant. */
1408 if (commutative_p
1409 && swap_commutative_operands_with_target (target, xop0, xop1))
1410 std::swap (xop0, xop1);
1412 /* Now, if insn's predicates don't allow our operands, put them into
1413 pseudo regs. */
1415 if (binoptab == vec_pack_trunc_optab
1416 || binoptab == vec_pack_usat_optab
1417 || binoptab == vec_pack_ssat_optab
1418 || binoptab == vec_pack_ufix_trunc_optab
1419 || binoptab == vec_pack_sfix_trunc_optab
1420 || binoptab == vec_packu_float_optab
1421 || binoptab == vec_packs_float_optab)
1423 /* The mode of the result is different then the mode of the
1424 arguments. */
1425 tmp_mode = insn_data[(int) icode].operand[0].mode;
1426 if (VECTOR_MODE_P (mode)
1427 && maybe_ne (GET_MODE_NUNITS (tmp_mode), 2 * GET_MODE_NUNITS (mode)))
1429 delete_insns_since (last);
1430 return NULL_RTX;
1433 else
1434 tmp_mode = mode;
1436 create_output_operand (&ops[0], target, tmp_mode);
1437 create_input_operand (&ops[1], xop0, mode0);
1438 create_input_operand (&ops[2], xop1, mode1);
1439 pat = maybe_gen_insn (icode, 3, ops);
1440 if (pat)
1442 /* If PAT is composed of more than one insn, try to add an appropriate
1443 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1444 operand, call expand_binop again, this time without a target. */
1445 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1446 && ! add_equal_note (pat, ops[0].value,
1447 optab_to_code (binoptab),
1448 ops[1].value, ops[2].value, mode0))
1450 delete_insns_since (last);
1451 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1452 unsignedp, methods);
1455 emit_insn (pat);
1456 return ops[0].value;
1458 delete_insns_since (last);
1459 return NULL_RTX;
1462 /* Generate code to perform an operation specified by BINOPTAB
1463 on operands OP0 and OP1, with result having machine-mode MODE.
1465 UNSIGNEDP is for the case where we have to widen the operands
1466 to perform the operation. It says to use zero-extension.
1468 If TARGET is nonzero, the value
1469 is generated there, if it is convenient to do so.
1470 In all cases an rtx is returned for the locus of the value;
1471 this may or may not be TARGET. */
1474 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1475 rtx target, int unsignedp, enum optab_methods methods)
1477 enum optab_methods next_methods
1478 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1479 ? OPTAB_WIDEN : methods);
1480 enum mode_class mclass;
1481 enum insn_code icode;
1482 machine_mode wider_mode;
1483 scalar_int_mode int_mode;
1484 rtx libfunc;
1485 rtx temp;
1486 rtx_insn *entry_last = get_last_insn ();
1487 rtx_insn *last;
1489 mclass = GET_MODE_CLASS (mode);
1491 /* If subtracting an integer constant, convert this into an addition of
1492 the negated constant. */
1494 if (binoptab == sub_optab && CONST_INT_P (op1))
1496 op1 = negate_rtx (mode, op1);
1497 binoptab = add_optab;
1499 /* For shifts, constant invalid op1 might be expanded from different
1500 mode than MODE. As those are invalid, force them to a register
1501 to avoid further problems during expansion. */
1502 else if (CONST_INT_P (op1)
1503 && shift_optab_p (binoptab)
1504 && UINTVAL (op1) >= GET_MODE_BITSIZE (GET_MODE_INNER (mode)))
1506 op1 = gen_int_mode (INTVAL (op1), GET_MODE_INNER (mode));
1507 op1 = force_reg (GET_MODE_INNER (mode), op1);
1510 /* Record where to delete back to if we backtrack. */
1511 last = get_last_insn ();
1513 /* If we can do it with a three-operand insn, do so. */
1515 if (methods != OPTAB_MUST_WIDEN)
1517 if (convert_optab_p (binoptab))
1519 machine_mode from_mode = widened_mode (mode, op0, op1);
1520 icode = find_widening_optab_handler (binoptab, mode, from_mode);
1522 else
1523 icode = optab_handler (binoptab, mode);
1524 if (icode != CODE_FOR_nothing)
1526 temp = expand_binop_directly (icode, mode, binoptab, op0, op1,
1527 target, unsignedp, methods, last);
1528 if (temp)
1529 return temp;
1533 /* If we were trying to rotate, and that didn't work, try rotating
1534 the other direction before falling back to shifts and bitwise-or. */
1535 if (((binoptab == rotl_optab
1536 && (icode = optab_handler (rotr_optab, mode)) != CODE_FOR_nothing)
1537 || (binoptab == rotr_optab
1538 && (icode = optab_handler (rotl_optab, mode)) != CODE_FOR_nothing))
1539 && is_int_mode (mode, &int_mode))
1541 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1542 rtx newop1;
1543 unsigned int bits = GET_MODE_PRECISION (int_mode);
1545 if (CONST_INT_P (op1))
1546 newop1 = gen_int_shift_amount (int_mode, bits - INTVAL (op1));
1547 else if (targetm.shift_truncation_mask (int_mode) == bits - 1)
1548 newop1 = negate_rtx (GET_MODE (op1), op1);
1549 else
1550 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1551 gen_int_mode (bits, GET_MODE (op1)), op1,
1552 NULL_RTX, unsignedp, OPTAB_DIRECT);
1554 temp = expand_binop_directly (icode, int_mode, otheroptab, op0, newop1,
1555 target, unsignedp, methods, last);
1556 if (temp)
1557 return temp;
1560 /* If this is a multiply, see if we can do a widening operation that
1561 takes operands of this mode and makes a wider mode. */
1563 if (binoptab == smul_optab
1564 && GET_MODE_2XWIDER_MODE (mode).exists (&wider_mode)
1565 && (convert_optab_handler ((unsignedp
1566 ? umul_widen_optab
1567 : smul_widen_optab),
1568 wider_mode, mode) != CODE_FOR_nothing))
1570 /* *_widen_optab needs to determine operand mode, make sure at least
1571 one operand has non-VOID mode. */
1572 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
1573 op0 = force_reg (mode, op0);
1574 temp = expand_binop (wider_mode,
1575 unsignedp ? umul_widen_optab : smul_widen_optab,
1576 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1578 if (temp != 0)
1580 if (GET_MODE_CLASS (mode) == MODE_INT
1581 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1582 return gen_lowpart (mode, temp);
1583 else
1584 return convert_to_mode (mode, temp, unsignedp);
1588 /* If this is a vector shift by a scalar, see if we can do a vector
1589 shift by a vector. If so, broadcast the scalar into a vector. */
1590 if (mclass == MODE_VECTOR_INT)
1592 optab otheroptab = unknown_optab;
1594 if (binoptab == ashl_optab)
1595 otheroptab = vashl_optab;
1596 else if (binoptab == ashr_optab)
1597 otheroptab = vashr_optab;
1598 else if (binoptab == lshr_optab)
1599 otheroptab = vlshr_optab;
1600 else if (binoptab == rotl_optab)
1601 otheroptab = vrotl_optab;
1602 else if (binoptab == rotr_optab)
1603 otheroptab = vrotr_optab;
1605 if (otheroptab
1606 && (icode = optab_handler (otheroptab, mode)) != CODE_FOR_nothing)
1608 /* The scalar may have been extended to be too wide. Truncate
1609 it back to the proper size to fit in the broadcast vector. */
1610 scalar_mode inner_mode = GET_MODE_INNER (mode);
1611 if (!CONST_INT_P (op1)
1612 && (GET_MODE_BITSIZE (as_a <scalar_int_mode> (GET_MODE (op1)))
1613 > GET_MODE_BITSIZE (inner_mode)))
1614 op1 = force_reg (inner_mode,
1615 simplify_gen_unary (TRUNCATE, inner_mode, op1,
1616 GET_MODE (op1)));
1617 rtx vop1 = expand_vector_broadcast (mode, op1);
1618 if (vop1)
1620 temp = expand_binop_directly (icode, mode, otheroptab, op0, vop1,
1621 target, unsignedp, methods, last);
1622 if (temp)
1623 return temp;
1628 /* Look for a wider mode of the same class for which we think we
1629 can open-code the operation. Check for a widening multiply at the
1630 wider mode as well. */
1632 if (CLASS_HAS_WIDER_MODES_P (mclass)
1633 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1634 FOR_EACH_WIDER_MODE (wider_mode, mode)
1636 machine_mode next_mode;
1637 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1638 || (binoptab == smul_optab
1639 && GET_MODE_WIDER_MODE (wider_mode).exists (&next_mode)
1640 && (find_widening_optab_handler ((unsignedp
1641 ? umul_widen_optab
1642 : smul_widen_optab),
1643 next_mode, mode)
1644 != CODE_FOR_nothing)))
1646 rtx xop0 = op0, xop1 = op1;
1647 int no_extend = 0;
1649 /* For certain integer operations, we need not actually extend
1650 the narrow operands, as long as we will truncate
1651 the results to the same narrowness. */
1653 if ((binoptab == ior_optab || binoptab == and_optab
1654 || binoptab == xor_optab
1655 || binoptab == add_optab || binoptab == sub_optab
1656 || binoptab == smul_optab || binoptab == ashl_optab)
1657 && mclass == MODE_INT)
1659 no_extend = 1;
1660 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1661 xop0, unsignedp);
1662 if (binoptab != ashl_optab)
1663 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1664 xop1, unsignedp);
1667 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1669 /* The second operand of a shift must always be extended. */
1670 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1671 no_extend && binoptab != ashl_optab);
1673 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1674 unsignedp, OPTAB_DIRECT);
1675 if (temp)
1677 if (mclass != MODE_INT
1678 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1680 if (target == 0)
1681 target = gen_reg_rtx (mode);
1682 convert_move (target, temp, 0);
1683 return target;
1685 else
1686 return gen_lowpart (mode, temp);
1688 else
1689 delete_insns_since (last);
1693 /* If operation is commutative,
1694 try to make the first operand a register.
1695 Even better, try to make it the same as the target.
1696 Also try to make the last operand a constant. */
1697 if (commutative_optab_p (binoptab)
1698 && swap_commutative_operands_with_target (target, op0, op1))
1699 std::swap (op0, op1);
1701 /* These can be done a word at a time. */
1702 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1703 && is_int_mode (mode, &int_mode)
1704 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
1705 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1707 int i;
1708 rtx_insn *insns;
1710 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1711 won't be accurate, so use a new target. */
1712 if (target == 0
1713 || target == op0
1714 || target == op1
1715 || reg_overlap_mentioned_p (target, op0)
1716 || reg_overlap_mentioned_p (target, op1)
1717 || !valid_multiword_target_p (target))
1718 target = gen_reg_rtx (int_mode);
1720 start_sequence ();
1722 /* Do the actual arithmetic. */
1723 machine_mode op0_mode = GET_MODE (op0);
1724 machine_mode op1_mode = GET_MODE (op1);
1725 if (op0_mode == VOIDmode)
1726 op0_mode = int_mode;
1727 if (op1_mode == VOIDmode)
1728 op1_mode = int_mode;
1729 for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
1731 rtx target_piece = operand_subword (target, i, 1, int_mode);
1732 rtx x = expand_binop (word_mode, binoptab,
1733 operand_subword_force (op0, i, op0_mode),
1734 operand_subword_force (op1, i, op1_mode),
1735 target_piece, unsignedp, next_methods);
1737 if (x == 0)
1738 break;
1740 if (target_piece != x)
1741 emit_move_insn (target_piece, x);
1744 insns = get_insns ();
1745 end_sequence ();
1747 if (i == GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD)
1749 emit_insn (insns);
1750 return target;
1754 /* Synthesize double word shifts from single word shifts. */
1755 if ((binoptab == lshr_optab || binoptab == ashl_optab
1756 || binoptab == ashr_optab)
1757 && is_int_mode (mode, &int_mode)
1758 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1759 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
1760 && GET_MODE_PRECISION (int_mode) == GET_MODE_BITSIZE (int_mode)
1761 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1762 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1763 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1765 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1766 scalar_int_mode op1_mode;
1768 double_shift_mask = targetm.shift_truncation_mask (int_mode);
1769 shift_mask = targetm.shift_truncation_mask (word_mode);
1770 op1_mode = (GET_MODE (op1) != VOIDmode
1771 ? as_a <scalar_int_mode> (GET_MODE (op1))
1772 : word_mode);
1774 /* Apply the truncation to constant shifts. */
1775 if (double_shift_mask > 0 && CONST_INT_P (op1))
1776 op1 = gen_int_mode (INTVAL (op1) & double_shift_mask, op1_mode);
1778 if (op1 == CONST0_RTX (op1_mode))
1779 return op0;
1781 /* Make sure that this is a combination that expand_doubleword_shift
1782 can handle. See the comments there for details. */
1783 if (double_shift_mask == 0
1784 || (shift_mask == BITS_PER_WORD - 1
1785 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1787 rtx_insn *insns;
1788 rtx into_target, outof_target;
1789 rtx into_input, outof_input;
1790 int left_shift, outof_word;
1792 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1793 won't be accurate, so use a new target. */
1794 if (target == 0
1795 || target == op0
1796 || target == op1
1797 || reg_overlap_mentioned_p (target, op0)
1798 || reg_overlap_mentioned_p (target, op1)
1799 || !valid_multiword_target_p (target))
1800 target = gen_reg_rtx (int_mode);
1802 start_sequence ();
1804 /* OUTOF_* is the word we are shifting bits away from, and
1805 INTO_* is the word that we are shifting bits towards, thus
1806 they differ depending on the direction of the shift and
1807 WORDS_BIG_ENDIAN. */
1809 left_shift = binoptab == ashl_optab;
1810 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1812 outof_target = operand_subword (target, outof_word, 1, int_mode);
1813 into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
1815 outof_input = operand_subword_force (op0, outof_word, int_mode);
1816 into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
1818 if (expand_doubleword_shift (op1_mode, binoptab,
1819 outof_input, into_input, op1,
1820 outof_target, into_target,
1821 unsignedp, next_methods, shift_mask))
1823 insns = get_insns ();
1824 end_sequence ();
1826 emit_insn (insns);
1827 return target;
1829 end_sequence ();
1833 /* Synthesize double word rotates from single word shifts. */
1834 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1835 && is_int_mode (mode, &int_mode)
1836 && CONST_INT_P (op1)
1837 && GET_MODE_PRECISION (int_mode) == 2 * BITS_PER_WORD
1838 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1839 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1841 rtx_insn *insns;
1842 rtx into_target, outof_target;
1843 rtx into_input, outof_input;
1844 rtx inter;
1845 int shift_count, left_shift, outof_word;
1847 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1848 won't be accurate, so use a new target. Do this also if target is not
1849 a REG, first because having a register instead may open optimization
1850 opportunities, and second because if target and op0 happen to be MEMs
1851 designating the same location, we would risk clobbering it too early
1852 in the code sequence we generate below. */
1853 if (target == 0
1854 || target == op0
1855 || target == op1
1856 || !REG_P (target)
1857 || reg_overlap_mentioned_p (target, op0)
1858 || reg_overlap_mentioned_p (target, op1)
1859 || !valid_multiword_target_p (target))
1860 target = gen_reg_rtx (int_mode);
1862 start_sequence ();
1864 shift_count = INTVAL (op1);
1866 /* OUTOF_* is the word we are shifting bits away from, and
1867 INTO_* is the word that we are shifting bits towards, thus
1868 they differ depending on the direction of the shift and
1869 WORDS_BIG_ENDIAN. */
1871 left_shift = (binoptab == rotl_optab);
1872 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1874 outof_target = operand_subword (target, outof_word, 1, int_mode);
1875 into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
1877 outof_input = operand_subword_force (op0, outof_word, int_mode);
1878 into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
1880 if (shift_count == BITS_PER_WORD)
1882 /* This is just a word swap. */
1883 emit_move_insn (outof_target, into_input);
1884 emit_move_insn (into_target, outof_input);
1885 inter = const0_rtx;
1887 else
1889 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1890 HOST_WIDE_INT first_shift_count, second_shift_count;
1891 optab reverse_unsigned_shift, unsigned_shift;
1893 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1894 ? lshr_optab : ashl_optab);
1896 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1897 ? ashl_optab : lshr_optab);
1899 if (shift_count > BITS_PER_WORD)
1901 first_shift_count = shift_count - BITS_PER_WORD;
1902 second_shift_count = 2 * BITS_PER_WORD - shift_count;
1904 else
1906 first_shift_count = BITS_PER_WORD - shift_count;
1907 second_shift_count = shift_count;
1909 rtx first_shift_count_rtx
1910 = gen_int_shift_amount (word_mode, first_shift_count);
1911 rtx second_shift_count_rtx
1912 = gen_int_shift_amount (word_mode, second_shift_count);
1914 into_temp1 = expand_binop (word_mode, unsigned_shift,
1915 outof_input, first_shift_count_rtx,
1916 NULL_RTX, unsignedp, next_methods);
1917 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1918 into_input, second_shift_count_rtx,
1919 NULL_RTX, unsignedp, next_methods);
1921 if (into_temp1 != 0 && into_temp2 != 0)
1922 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1923 into_target, unsignedp, next_methods);
1924 else
1925 inter = 0;
1927 if (inter != 0 && inter != into_target)
1928 emit_move_insn (into_target, inter);
1930 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1931 into_input, first_shift_count_rtx,
1932 NULL_RTX, unsignedp, next_methods);
1933 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1934 outof_input, second_shift_count_rtx,
1935 NULL_RTX, unsignedp, next_methods);
1937 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1938 inter = expand_binop (word_mode, ior_optab,
1939 outof_temp1, outof_temp2,
1940 outof_target, unsignedp, next_methods);
1942 if (inter != 0 && inter != outof_target)
1943 emit_move_insn (outof_target, inter);
1946 insns = get_insns ();
1947 end_sequence ();
1949 if (inter != 0)
1951 emit_insn (insns);
1952 return target;
1956 /* These can be done a word at a time by propagating carries. */
1957 if ((binoptab == add_optab || binoptab == sub_optab)
1958 && is_int_mode (mode, &int_mode)
1959 && GET_MODE_SIZE (int_mode) >= 2 * UNITS_PER_WORD
1960 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1962 unsigned int i;
1963 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1964 const unsigned int nwords = GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD;
1965 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1966 rtx xop0, xop1, xtarget;
1968 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1969 value is one of those, use it. Otherwise, use 1 since it is the
1970 one easiest to get. */
1971 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1972 int normalizep = STORE_FLAG_VALUE;
1973 #else
1974 int normalizep = 1;
1975 #endif
1977 /* Prepare the operands. */
1978 xop0 = force_reg (int_mode, op0);
1979 xop1 = force_reg (int_mode, op1);
1981 xtarget = gen_reg_rtx (int_mode);
1983 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1984 target = xtarget;
1986 /* Indicate for flow that the entire target reg is being set. */
1987 if (REG_P (target))
1988 emit_clobber (xtarget);
1990 /* Do the actual arithmetic. */
1991 for (i = 0; i < nwords; i++)
1993 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1994 rtx target_piece = operand_subword (xtarget, index, 1, int_mode);
1995 rtx op0_piece = operand_subword_force (xop0, index, int_mode);
1996 rtx op1_piece = operand_subword_force (xop1, index, int_mode);
1997 rtx x;
1999 /* Main add/subtract of the input operands. */
2000 x = expand_binop (word_mode, binoptab,
2001 op0_piece, op1_piece,
2002 target_piece, unsignedp, next_methods);
2003 if (x == 0)
2004 break;
2006 if (i + 1 < nwords)
2008 /* Store carry from main add/subtract. */
2009 carry_out = gen_reg_rtx (word_mode);
2010 carry_out = emit_store_flag_force (carry_out,
2011 (binoptab == add_optab
2012 ? LT : GT),
2013 x, op0_piece,
2014 word_mode, 1, normalizep);
2017 if (i > 0)
2019 rtx newx;
2021 /* Add/subtract previous carry to main result. */
2022 newx = expand_binop (word_mode,
2023 normalizep == 1 ? binoptab : otheroptab,
2024 x, carry_in,
2025 NULL_RTX, 1, next_methods);
2027 if (i + 1 < nwords)
2029 /* Get out carry from adding/subtracting carry in. */
2030 rtx carry_tmp = gen_reg_rtx (word_mode);
2031 carry_tmp = emit_store_flag_force (carry_tmp,
2032 (binoptab == add_optab
2033 ? LT : GT),
2034 newx, x,
2035 word_mode, 1, normalizep);
2037 /* Logical-ior the two poss. carry together. */
2038 carry_out = expand_binop (word_mode, ior_optab,
2039 carry_out, carry_tmp,
2040 carry_out, 0, next_methods);
2041 if (carry_out == 0)
2042 break;
2044 emit_move_insn (target_piece, newx);
2046 else
2048 if (x != target_piece)
2049 emit_move_insn (target_piece, x);
2052 carry_in = carry_out;
2055 if (i == GET_MODE_BITSIZE (int_mode) / (unsigned) BITS_PER_WORD)
2057 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing
2058 || ! rtx_equal_p (target, xtarget))
2060 rtx_insn *temp = emit_move_insn (target, xtarget);
2062 set_dst_reg_note (temp, REG_EQUAL,
2063 gen_rtx_fmt_ee (optab_to_code (binoptab),
2064 int_mode, copy_rtx (xop0),
2065 copy_rtx (xop1)),
2066 target);
2068 else
2069 target = xtarget;
2071 return target;
2074 else
2075 delete_insns_since (last);
2078 /* Attempt to synthesize double word multiplies using a sequence of word
2079 mode multiplications. We first attempt to generate a sequence using a
2080 more efficient unsigned widening multiply, and if that fails we then
2081 try using a signed widening multiply. */
2083 if (binoptab == smul_optab
2084 && is_int_mode (mode, &int_mode)
2085 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2086 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2087 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2089 rtx product = NULL_RTX;
2090 if (convert_optab_handler (umul_widen_optab, int_mode, word_mode)
2091 != CODE_FOR_nothing)
2093 product = expand_doubleword_mult (int_mode, op0, op1, target,
2094 true, methods);
2095 if (!product)
2096 delete_insns_since (last);
2099 if (product == NULL_RTX
2100 && (convert_optab_handler (smul_widen_optab, int_mode, word_mode)
2101 != CODE_FOR_nothing))
2103 product = expand_doubleword_mult (int_mode, op0, op1, target,
2104 false, methods);
2105 if (!product)
2106 delete_insns_since (last);
2109 if (product != NULL_RTX)
2111 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
2113 rtx_insn *move = emit_move_insn (target ? target : product,
2114 product);
2115 set_dst_reg_note (move,
2116 REG_EQUAL,
2117 gen_rtx_fmt_ee (MULT, int_mode,
2118 copy_rtx (op0),
2119 copy_rtx (op1)),
2120 target ? target : product);
2122 return product;
2126 /* Attempt to synthetize double word modulo by constant divisor. */
2127 if ((binoptab == umod_optab
2128 || binoptab == smod_optab
2129 || binoptab == udiv_optab
2130 || binoptab == sdiv_optab)
2131 && optimize
2132 && CONST_INT_P (op1)
2133 && is_int_mode (mode, &int_mode)
2134 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2135 && optab_handler ((binoptab == umod_optab || binoptab == udiv_optab)
2136 ? udivmod_optab : sdivmod_optab,
2137 int_mode) == CODE_FOR_nothing
2138 && optab_handler (and_optab, word_mode) != CODE_FOR_nothing
2139 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing
2140 && optimize_insn_for_speed_p ())
2142 rtx res = NULL_RTX;
2143 if ((binoptab == umod_optab || binoptab == smod_optab)
2144 && (INTVAL (op1) & 1) == 0)
2145 res = expand_doubleword_mod (int_mode, op0, op1,
2146 binoptab == umod_optab);
2147 else
2149 rtx quot = expand_doubleword_divmod (int_mode, op0, op1, &res,
2150 binoptab == umod_optab
2151 || binoptab == udiv_optab);
2152 if (quot == NULL_RTX)
2153 res = NULL_RTX;
2154 else if (binoptab == udiv_optab || binoptab == sdiv_optab)
2155 res = quot;
2157 if (res != NULL_RTX)
2159 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
2161 rtx_insn *move = emit_move_insn (target ? target : res,
2162 res);
2163 set_dst_reg_note (move, REG_EQUAL,
2164 gen_rtx_fmt_ee (optab_to_code (binoptab),
2165 int_mode, copy_rtx (op0), op1),
2166 target ? target : res);
2168 return res;
2170 else
2171 delete_insns_since (last);
2174 /* It can't be open-coded in this mode.
2175 Use a library call if one is available and caller says that's ok. */
2177 libfunc = optab_libfunc (binoptab, mode);
2178 if (libfunc
2179 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2181 rtx_insn *insns;
2182 rtx op1x = op1;
2183 machine_mode op1_mode = mode;
2184 rtx value;
2186 start_sequence ();
2188 if (shift_optab_p (binoptab))
2190 op1_mode = targetm.libgcc_shift_count_mode ();
2191 /* Specify unsigned here,
2192 since negative shift counts are meaningless. */
2193 op1x = convert_to_mode (op1_mode, op1, 1);
2196 if (GET_MODE (op0) != VOIDmode
2197 && GET_MODE (op0) != mode)
2198 op0 = convert_to_mode (mode, op0, unsignedp);
2200 /* Pass 1 for NO_QUEUE so we don't lose any increments
2201 if the libcall is cse'd or moved. */
2202 value = emit_library_call_value (libfunc,
2203 NULL_RTX, LCT_CONST, mode,
2204 op0, mode, op1x, op1_mode);
2206 insns = get_insns ();
2207 end_sequence ();
2209 bool trapv = trapv_binoptab_p (binoptab);
2210 target = gen_reg_rtx (mode);
2211 emit_libcall_block_1 (insns, target, value,
2212 trapv ? NULL_RTX
2213 : gen_rtx_fmt_ee (optab_to_code (binoptab),
2214 mode, op0, op1), trapv);
2216 return target;
2219 delete_insns_since (last);
2221 /* It can't be done in this mode. Can we do it in a wider mode? */
2223 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2224 || methods == OPTAB_MUST_WIDEN))
2226 /* Caller says, don't even try. */
2227 delete_insns_since (entry_last);
2228 return 0;
2231 /* Compute the value of METHODS to pass to recursive calls.
2232 Don't allow widening to be tried recursively. */
2234 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2236 /* Look for a wider mode of the same class for which it appears we can do
2237 the operation. */
2239 if (CLASS_HAS_WIDER_MODES_P (mclass))
2241 /* This code doesn't make sense for conversion optabs, since we
2242 wouldn't then want to extend the operands to be the same size
2243 as the result. */
2244 gcc_assert (!convert_optab_p (binoptab));
2245 FOR_EACH_WIDER_MODE (wider_mode, mode)
2247 if (optab_handler (binoptab, wider_mode)
2248 || (methods == OPTAB_LIB
2249 && optab_libfunc (binoptab, wider_mode)))
2251 rtx xop0 = op0, xop1 = op1;
2252 int no_extend = 0;
2254 /* For certain integer operations, we need not actually extend
2255 the narrow operands, as long as we will truncate
2256 the results to the same narrowness. */
2258 if ((binoptab == ior_optab || binoptab == and_optab
2259 || binoptab == xor_optab
2260 || binoptab == add_optab || binoptab == sub_optab
2261 || binoptab == smul_optab || binoptab == ashl_optab)
2262 && mclass == MODE_INT)
2263 no_extend = 1;
2265 xop0 = widen_operand (xop0, wider_mode, mode,
2266 unsignedp, no_extend);
2268 /* The second operand of a shift must always be extended. */
2269 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2270 no_extend && binoptab != ashl_optab);
2272 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2273 unsignedp, methods);
2274 if (temp)
2276 if (mclass != MODE_INT
2277 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2279 if (target == 0)
2280 target = gen_reg_rtx (mode);
2281 convert_move (target, temp, 0);
2282 return target;
2284 else
2285 return gen_lowpart (mode, temp);
2287 else
2288 delete_insns_since (last);
2293 delete_insns_since (entry_last);
2294 return 0;
2297 /* Expand a binary operator which has both signed and unsigned forms.
2298 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2299 signed operations.
2301 If we widen unsigned operands, we may use a signed wider operation instead
2302 of an unsigned wider operation, since the result would be the same. */
2305 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
2306 rtx op0, rtx op1, rtx target, int unsignedp,
2307 enum optab_methods methods)
2309 rtx temp;
2310 optab direct_optab = unsignedp ? uoptab : soptab;
2311 bool save_enable;
2313 /* Do it without widening, if possible. */
2314 temp = expand_binop (mode, direct_optab, op0, op1, target,
2315 unsignedp, OPTAB_DIRECT);
2316 if (temp || methods == OPTAB_DIRECT)
2317 return temp;
2319 /* Try widening to a signed int. Disable any direct use of any
2320 signed insn in the current mode. */
2321 save_enable = swap_optab_enable (soptab, mode, false);
2323 temp = expand_binop (mode, soptab, op0, op1, target,
2324 unsignedp, OPTAB_WIDEN);
2326 /* For unsigned operands, try widening to an unsigned int. */
2327 if (!temp && unsignedp)
2328 temp = expand_binop (mode, uoptab, op0, op1, target,
2329 unsignedp, OPTAB_WIDEN);
2330 if (temp || methods == OPTAB_WIDEN)
2331 goto egress;
2333 /* Use the right width libcall if that exists. */
2334 temp = expand_binop (mode, direct_optab, op0, op1, target,
2335 unsignedp, OPTAB_LIB);
2336 if (temp || methods == OPTAB_LIB)
2337 goto egress;
2339 /* Must widen and use a libcall, use either signed or unsigned. */
2340 temp = expand_binop (mode, soptab, op0, op1, target,
2341 unsignedp, methods);
2342 if (!temp && unsignedp)
2343 temp = expand_binop (mode, uoptab, op0, op1, target,
2344 unsignedp, methods);
2346 egress:
2347 /* Undo the fiddling above. */
2348 if (save_enable)
2349 swap_optab_enable (soptab, mode, true);
2350 return temp;
2353 /* Generate code to perform an operation specified by UNOPPTAB
2354 on operand OP0, with two results to TARG0 and TARG1.
2355 We assume that the order of the operands for the instruction
2356 is TARG0, TARG1, OP0.
2358 Either TARG0 or TARG1 may be zero, but what that means is that
2359 the result is not actually wanted. We will generate it into
2360 a dummy pseudo-reg and discard it. They may not both be zero.
2362 Returns 1 if this operation can be performed; 0 if not. */
2365 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2366 int unsignedp)
2368 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2369 enum mode_class mclass;
2370 machine_mode wider_mode;
2371 rtx_insn *entry_last = get_last_insn ();
2372 rtx_insn *last;
2374 mclass = GET_MODE_CLASS (mode);
2376 if (!targ0)
2377 targ0 = gen_reg_rtx (mode);
2378 if (!targ1)
2379 targ1 = gen_reg_rtx (mode);
2381 /* Record where to go back to if we fail. */
2382 last = get_last_insn ();
2384 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2386 class expand_operand ops[3];
2387 enum insn_code icode = optab_handler (unoptab, mode);
2389 create_fixed_operand (&ops[0], targ0);
2390 create_fixed_operand (&ops[1], targ1);
2391 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2392 if (maybe_expand_insn (icode, 3, ops))
2393 return 1;
2396 /* It can't be done in this mode. Can we do it in a wider mode? */
2398 if (CLASS_HAS_WIDER_MODES_P (mclass))
2400 FOR_EACH_WIDER_MODE (wider_mode, mode)
2402 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2404 rtx t0 = gen_reg_rtx (wider_mode);
2405 rtx t1 = gen_reg_rtx (wider_mode);
2406 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2408 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2410 convert_move (targ0, t0, unsignedp);
2411 convert_move (targ1, t1, unsignedp);
2412 return 1;
2414 else
2415 delete_insns_since (last);
2420 delete_insns_since (entry_last);
2421 return 0;
2424 /* Generate code to perform an operation specified by BINOPTAB
2425 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2426 We assume that the order of the operands for the instruction
2427 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2428 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2430 Either TARG0 or TARG1 may be zero, but what that means is that
2431 the result is not actually wanted. We will generate it into
2432 a dummy pseudo-reg and discard it. They may not both be zero.
2434 Returns 1 if this operation can be performed; 0 if not. */
2437 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2438 int unsignedp)
2440 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2441 enum mode_class mclass;
2442 machine_mode wider_mode;
2443 rtx_insn *entry_last = get_last_insn ();
2444 rtx_insn *last;
2446 mclass = GET_MODE_CLASS (mode);
2448 if (!targ0)
2449 targ0 = gen_reg_rtx (mode);
2450 if (!targ1)
2451 targ1 = gen_reg_rtx (mode);
2453 /* Record where to go back to if we fail. */
2454 last = get_last_insn ();
2456 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2458 class expand_operand ops[4];
2459 enum insn_code icode = optab_handler (binoptab, mode);
2460 machine_mode mode0 = insn_data[icode].operand[1].mode;
2461 machine_mode mode1 = insn_data[icode].operand[2].mode;
2462 rtx xop0 = op0, xop1 = op1;
2464 /* If we are optimizing, force expensive constants into a register. */
2465 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2466 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2468 create_fixed_operand (&ops[0], targ0);
2469 create_convert_operand_from (&ops[1], xop0, mode, unsignedp);
2470 create_convert_operand_from (&ops[2], xop1, mode, unsignedp);
2471 create_fixed_operand (&ops[3], targ1);
2472 if (maybe_expand_insn (icode, 4, ops))
2473 return 1;
2474 delete_insns_since (last);
2477 /* It can't be done in this mode. Can we do it in a wider mode? */
2479 if (CLASS_HAS_WIDER_MODES_P (mclass))
2481 FOR_EACH_WIDER_MODE (wider_mode, mode)
2483 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2485 rtx t0 = gen_reg_rtx (wider_mode);
2486 rtx t1 = gen_reg_rtx (wider_mode);
2487 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2488 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2490 if (expand_twoval_binop (binoptab, cop0, cop1,
2491 t0, t1, unsignedp))
2493 convert_move (targ0, t0, unsignedp);
2494 convert_move (targ1, t1, unsignedp);
2495 return 1;
2497 else
2498 delete_insns_since (last);
2503 delete_insns_since (entry_last);
2504 return 0;
2507 /* Expand the two-valued library call indicated by BINOPTAB, but
2508 preserve only one of the values. If TARG0 is non-NULL, the first
2509 value is placed into TARG0; otherwise the second value is placed
2510 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2511 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2512 This routine assumes that the value returned by the library call is
2513 as if the return value was of an integral mode twice as wide as the
2514 mode of OP0. Returns 1 if the call was successful. */
2516 bool
2517 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2518 rtx targ0, rtx targ1, enum rtx_code code)
2520 machine_mode mode;
2521 machine_mode libval_mode;
2522 rtx libval;
2523 rtx_insn *insns;
2524 rtx libfunc;
2526 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2527 gcc_assert (!targ0 != !targ1);
2529 mode = GET_MODE (op0);
2530 libfunc = optab_libfunc (binoptab, mode);
2531 if (!libfunc)
2532 return false;
2534 /* The value returned by the library function will have twice as
2535 many bits as the nominal MODE. */
2536 libval_mode = smallest_int_mode_for_size (2 * GET_MODE_BITSIZE (mode));
2537 start_sequence ();
2538 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2539 libval_mode,
2540 op0, mode,
2541 op1, mode);
2542 /* Get the part of VAL containing the value that we want. */
2543 libval = simplify_gen_subreg (mode, libval, libval_mode,
2544 targ0 ? 0 : GET_MODE_SIZE (mode));
2545 insns = get_insns ();
2546 end_sequence ();
2547 /* Move the into the desired location. */
2548 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2549 gen_rtx_fmt_ee (code, mode, op0, op1));
2551 return true;
2555 /* Wrapper around expand_unop which takes an rtx code to specify
2556 the operation to perform, not an optab pointer. All other
2557 arguments are the same. */
2559 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2560 rtx target, int unsignedp)
2562 optab unop = code_to_optab (code);
2563 gcc_assert (unop);
2565 return expand_unop (mode, unop, op0, target, unsignedp);
2568 /* Try calculating
2569 (clz:narrow x)
2571 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2573 A similar operation can be used for clrsb. UNOPTAB says which operation
2574 we are trying to expand. */
2575 static rtx
2576 widen_leading (scalar_int_mode mode, rtx op0, rtx target, optab unoptab)
2578 opt_scalar_int_mode wider_mode_iter;
2579 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2581 scalar_int_mode wider_mode = wider_mode_iter.require ();
2582 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2584 rtx xop0, temp;
2585 rtx_insn *last;
2587 last = get_last_insn ();
2589 if (target == 0)
2590 target = gen_reg_rtx (mode);
2591 xop0 = widen_operand (op0, wider_mode, mode,
2592 unoptab != clrsb_optab, false);
2593 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2594 unoptab != clrsb_optab);
2595 if (temp != 0)
2596 temp = expand_binop
2597 (wider_mode, sub_optab, temp,
2598 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2599 - GET_MODE_PRECISION (mode),
2600 wider_mode),
2601 target, true, OPTAB_DIRECT);
2602 if (temp == 0)
2603 delete_insns_since (last);
2605 return temp;
2608 return 0;
2611 /* Attempt to emit (clrsb:mode op0) as
2612 (plus:mode (clz:mode (xor:mode op0 (ashr:mode op0 (const_int prec-1))))
2613 (const_int -1))
2614 if CLZ_DEFINED_VALUE_AT_ZERO (mode, val) is 2 and val is prec,
2615 or as
2616 (clz:mode (ior:mode (xor:mode (ashl:mode op0 (const_int 1))
2617 (ashr:mode op0 (const_int prec-1)))
2618 (const_int 1)))
2619 otherwise. */
2621 static rtx
2622 expand_clrsb_using_clz (scalar_int_mode mode, rtx op0, rtx target)
2624 if (optimize_insn_for_size_p ()
2625 || optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2626 return NULL_RTX;
2628 start_sequence ();
2629 HOST_WIDE_INT val = 0;
2630 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) != 2
2631 || val != GET_MODE_PRECISION (mode))
2632 val = 0;
2633 else
2634 val = 1;
2636 rtx temp2 = op0;
2637 if (!val)
2639 temp2 = expand_binop (mode, ashl_optab, op0, const1_rtx,
2640 NULL_RTX, 0, OPTAB_DIRECT);
2641 if (!temp2)
2643 fail:
2644 end_sequence ();
2645 return NULL_RTX;
2649 rtx temp = expand_binop (mode, ashr_optab, op0,
2650 GEN_INT (GET_MODE_PRECISION (mode) - 1),
2651 NULL_RTX, 0, OPTAB_DIRECT);
2652 if (!temp)
2653 goto fail;
2655 temp = expand_binop (mode, xor_optab, temp2, temp, NULL_RTX, 0,
2656 OPTAB_DIRECT);
2657 if (!temp)
2658 goto fail;
2660 if (!val)
2662 temp = expand_binop (mode, ior_optab, temp, const1_rtx,
2663 NULL_RTX, 0, OPTAB_DIRECT);
2664 if (!temp)
2665 goto fail;
2667 temp = expand_unop_direct (mode, clz_optab, temp, val ? NULL_RTX : target,
2668 true);
2669 if (!temp)
2670 goto fail;
2671 if (val)
2673 temp = expand_binop (mode, add_optab, temp, constm1_rtx,
2674 target, 0, OPTAB_DIRECT);
2675 if (!temp)
2676 goto fail;
2679 rtx_insn *seq = get_insns ();
2680 end_sequence ();
2682 add_equal_note (seq, temp, CLRSB, op0, NULL_RTX, mode);
2683 emit_insn (seq);
2684 return temp;
2687 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2688 quantities, choosing which based on whether the high word is nonzero. */
2689 static rtx
2690 expand_doubleword_clz (scalar_int_mode mode, rtx op0, rtx target)
2692 rtx xop0 = force_reg (mode, op0);
2693 rtx subhi = gen_highpart (word_mode, xop0);
2694 rtx sublo = gen_lowpart (word_mode, xop0);
2695 rtx_code_label *hi0_label = gen_label_rtx ();
2696 rtx_code_label *after_label = gen_label_rtx ();
2697 rtx_insn *seq;
2698 rtx temp, result;
2700 /* If we were not given a target, use a word_mode register, not a
2701 'mode' register. The result will fit, and nobody is expecting
2702 anything bigger (the return type of __builtin_clz* is int). */
2703 if (!target)
2704 target = gen_reg_rtx (word_mode);
2706 /* In any case, write to a word_mode scratch in both branches of the
2707 conditional, so we can ensure there is a single move insn setting
2708 'target' to tag a REG_EQUAL note on. */
2709 result = gen_reg_rtx (word_mode);
2711 start_sequence ();
2713 /* If the high word is not equal to zero,
2714 then clz of the full value is clz of the high word. */
2715 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2716 word_mode, true, hi0_label);
2718 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2719 if (!temp)
2720 goto fail;
2722 if (temp != result)
2723 convert_move (result, temp, true);
2725 emit_jump_insn (targetm.gen_jump (after_label));
2726 emit_barrier ();
2728 /* Else clz of the full value is clz of the low word plus the number
2729 of bits in the high word. */
2730 emit_label (hi0_label);
2732 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2733 if (!temp)
2734 goto fail;
2735 temp = expand_binop (word_mode, add_optab, temp,
2736 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2737 result, true, OPTAB_DIRECT);
2738 if (!temp)
2739 goto fail;
2740 if (temp != result)
2741 convert_move (result, temp, true);
2743 emit_label (after_label);
2744 convert_move (target, result, true);
2746 seq = get_insns ();
2747 end_sequence ();
2749 add_equal_note (seq, target, CLZ, xop0, NULL_RTX, mode);
2750 emit_insn (seq);
2751 return target;
2753 fail:
2754 end_sequence ();
2755 return 0;
2758 /* Try calculating popcount of a double-word quantity as two popcount's of
2759 word-sized quantities and summing up the results. */
2760 static rtx
2761 expand_doubleword_popcount (scalar_int_mode mode, rtx op0, rtx target)
2763 rtx t0, t1, t;
2764 rtx_insn *seq;
2766 start_sequence ();
2768 t0 = expand_unop_direct (word_mode, popcount_optab,
2769 operand_subword_force (op0, 0, mode), NULL_RTX,
2770 true);
2771 t1 = expand_unop_direct (word_mode, popcount_optab,
2772 operand_subword_force (op0, 1, mode), NULL_RTX,
2773 true);
2774 if (!t0 || !t1)
2776 end_sequence ();
2777 return NULL_RTX;
2780 /* If we were not given a target, use a word_mode register, not a
2781 'mode' register. The result will fit, and nobody is expecting
2782 anything bigger (the return type of __builtin_popcount* is int). */
2783 if (!target)
2784 target = gen_reg_rtx (word_mode);
2786 t = expand_binop (word_mode, add_optab, t0, t1, target, 0, OPTAB_DIRECT);
2788 seq = get_insns ();
2789 end_sequence ();
2791 add_equal_note (seq, t, POPCOUNT, op0, NULL_RTX, mode);
2792 emit_insn (seq);
2793 return t;
2796 /* Try calculating
2797 (parity:wide x)
2799 (parity:narrow (low (x) ^ high (x))) */
2800 static rtx
2801 expand_doubleword_parity (scalar_int_mode mode, rtx op0, rtx target)
2803 rtx t = expand_binop (word_mode, xor_optab,
2804 operand_subword_force (op0, 0, mode),
2805 operand_subword_force (op0, 1, mode),
2806 NULL_RTX, 0, OPTAB_DIRECT);
2807 return expand_unop (word_mode, parity_optab, t, target, true);
2810 /* Try calculating
2811 (bswap:narrow x)
2813 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2814 static rtx
2815 widen_bswap (scalar_int_mode mode, rtx op0, rtx target)
2817 rtx x;
2818 rtx_insn *last;
2819 opt_scalar_int_mode wider_mode_iter;
2821 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2822 if (optab_handler (bswap_optab, wider_mode_iter.require ())
2823 != CODE_FOR_nothing)
2824 break;
2826 if (!wider_mode_iter.exists ())
2827 return NULL_RTX;
2829 scalar_int_mode wider_mode = wider_mode_iter.require ();
2830 last = get_last_insn ();
2832 x = widen_operand (op0, wider_mode, mode, true, true);
2833 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2835 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2836 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2837 if (x != 0)
2838 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2839 GET_MODE_BITSIZE (wider_mode)
2840 - GET_MODE_BITSIZE (mode),
2841 NULL_RTX, true);
2843 if (x != 0)
2845 if (target == 0)
2846 target = gen_reg_rtx (mode);
2847 emit_move_insn (target, gen_lowpart (mode, x));
2849 else
2850 delete_insns_since (last);
2852 return target;
2855 /* Try calculating bswap as two bswaps of two word-sized operands. */
2857 static rtx
2858 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2860 rtx t0, t1;
2862 t1 = expand_unop (word_mode, bswap_optab,
2863 operand_subword_force (op, 0, mode), NULL_RTX, true);
2864 t0 = expand_unop (word_mode, bswap_optab,
2865 operand_subword_force (op, 1, mode), NULL_RTX, true);
2867 if (target == 0 || !valid_multiword_target_p (target))
2868 target = gen_reg_rtx (mode);
2869 if (REG_P (target))
2870 emit_clobber (target);
2871 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2872 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2874 return target;
2877 /* Try calculating (parity x) as (and (popcount x) 1), where
2878 popcount can also be done in a wider mode. */
2879 static rtx
2880 expand_parity (scalar_int_mode mode, rtx op0, rtx target)
2882 enum mode_class mclass = GET_MODE_CLASS (mode);
2883 opt_scalar_int_mode wider_mode_iter;
2884 FOR_EACH_MODE_FROM (wider_mode_iter, mode)
2886 scalar_int_mode wider_mode = wider_mode_iter.require ();
2887 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2889 rtx xop0, temp;
2890 rtx_insn *last;
2892 last = get_last_insn ();
2894 if (target == 0 || GET_MODE (target) != wider_mode)
2895 target = gen_reg_rtx (wider_mode);
2897 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2898 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2899 true);
2900 if (temp != 0)
2901 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2902 target, true, OPTAB_DIRECT);
2904 if (temp)
2906 if (mclass != MODE_INT
2907 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2908 return convert_to_mode (mode, temp, 0);
2909 else
2910 return gen_lowpart (mode, temp);
2912 else
2913 delete_insns_since (last);
2916 return 0;
2919 /* Try calculating ctz(x) as K - clz(x & -x) ,
2920 where K is GET_MODE_PRECISION(mode) - 1.
2922 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2923 don't have to worry about what the hardware does in that case. (If
2924 the clz instruction produces the usual value at 0, which is K, the
2925 result of this code sequence will be -1; expand_ffs, below, relies
2926 on this. It might be nice to have it be K instead, for consistency
2927 with the (very few) processors that provide a ctz with a defined
2928 value, but that would take one more instruction, and it would be
2929 less convenient for expand_ffs anyway. */
2931 static rtx
2932 expand_ctz (scalar_int_mode mode, rtx op0, rtx target)
2934 rtx_insn *seq;
2935 rtx temp;
2937 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2938 return 0;
2940 start_sequence ();
2942 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2943 if (temp)
2944 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2945 true, OPTAB_DIRECT);
2946 if (temp)
2947 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2948 if (temp)
2949 temp = expand_binop (mode, sub_optab,
2950 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2951 temp, target,
2952 true, OPTAB_DIRECT);
2953 if (temp == 0)
2955 end_sequence ();
2956 return 0;
2959 seq = get_insns ();
2960 end_sequence ();
2962 add_equal_note (seq, temp, CTZ, op0, NULL_RTX, mode);
2963 emit_insn (seq);
2964 return temp;
2968 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2969 else with the sequence used by expand_clz.
2971 The ffs builtin promises to return zero for a zero value and ctz/clz
2972 may have an undefined value in that case. If they do not give us a
2973 convenient value, we have to generate a test and branch. */
2974 static rtx
2975 expand_ffs (scalar_int_mode mode, rtx op0, rtx target)
2977 HOST_WIDE_INT val = 0;
2978 bool defined_at_zero = false;
2979 rtx temp;
2980 rtx_insn *seq;
2982 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2984 start_sequence ();
2986 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2987 if (!temp)
2988 goto fail;
2990 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2992 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2994 start_sequence ();
2995 temp = expand_ctz (mode, op0, 0);
2996 if (!temp)
2997 goto fail;
2999 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
3001 defined_at_zero = true;
3002 val = (GET_MODE_PRECISION (mode) - 1) - val;
3005 else
3006 return 0;
3008 if (defined_at_zero && val == -1)
3009 /* No correction needed at zero. */;
3010 else
3012 /* We don't try to do anything clever with the situation found
3013 on some processors (eg Alpha) where ctz(0:mode) ==
3014 bitsize(mode). If someone can think of a way to send N to -1
3015 and leave alone all values in the range 0..N-1 (where N is a
3016 power of two), cheaper than this test-and-branch, please add it.
3018 The test-and-branch is done after the operation itself, in case
3019 the operation sets condition codes that can be recycled for this.
3020 (This is true on i386, for instance.) */
3022 rtx_code_label *nonzero_label = gen_label_rtx ();
3023 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
3024 mode, true, nonzero_label);
3026 convert_move (temp, GEN_INT (-1), false);
3027 emit_label (nonzero_label);
3030 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
3031 to produce a value in the range 0..bitsize. */
3032 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
3033 target, false, OPTAB_DIRECT);
3034 if (!temp)
3035 goto fail;
3037 seq = get_insns ();
3038 end_sequence ();
3040 add_equal_note (seq, temp, FFS, op0, NULL_RTX, mode);
3041 emit_insn (seq);
3042 return temp;
3044 fail:
3045 end_sequence ();
3046 return 0;
3049 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
3050 conditions, VAL may already be a SUBREG against which we cannot generate
3051 a further SUBREG. In this case, we expect forcing the value into a
3052 register will work around the situation. */
3054 static rtx
3055 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
3056 machine_mode imode)
3058 rtx ret;
3059 ret = lowpart_subreg (omode, val, imode);
3060 if (ret == NULL)
3062 val = force_reg (imode, val);
3063 ret = lowpart_subreg (omode, val, imode);
3064 gcc_assert (ret != NULL);
3066 return ret;
3069 /* Expand a floating point absolute value or negation operation via a
3070 logical operation on the sign bit. */
3072 static rtx
3073 expand_absneg_bit (enum rtx_code code, scalar_float_mode mode,
3074 rtx op0, rtx target)
3076 const struct real_format *fmt;
3077 int bitpos, word, nwords, i;
3078 scalar_int_mode imode;
3079 rtx temp;
3080 rtx_insn *insns;
3082 /* The format has to have a simple sign bit. */
3083 fmt = REAL_MODE_FORMAT (mode);
3084 if (fmt == NULL)
3085 return NULL_RTX;
3087 bitpos = fmt->signbit_rw;
3088 if (bitpos < 0)
3089 return NULL_RTX;
3091 /* Don't create negative zeros if the format doesn't support them. */
3092 if (code == NEG && !fmt->has_signed_zero)
3093 return NULL_RTX;
3095 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3097 if (!int_mode_for_mode (mode).exists (&imode))
3098 return NULL_RTX;
3099 word = 0;
3100 nwords = 1;
3102 else
3104 imode = word_mode;
3106 if (FLOAT_WORDS_BIG_ENDIAN)
3107 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3108 else
3109 word = bitpos / BITS_PER_WORD;
3110 bitpos = bitpos % BITS_PER_WORD;
3111 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3114 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3115 if (code == ABS)
3116 mask = ~mask;
3118 if (target == 0
3119 || target == op0
3120 || reg_overlap_mentioned_p (target, op0)
3121 || (nwords > 1 && !valid_multiword_target_p (target)))
3122 target = gen_reg_rtx (mode);
3124 if (nwords > 1)
3126 start_sequence ();
3128 for (i = 0; i < nwords; ++i)
3130 rtx targ_piece = operand_subword (target, i, 1, mode);
3131 rtx op0_piece = operand_subword_force (op0, i, mode);
3133 if (i == word)
3135 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
3136 op0_piece,
3137 immed_wide_int_const (mask, imode),
3138 targ_piece, 1, OPTAB_LIB_WIDEN);
3139 if (temp != targ_piece)
3140 emit_move_insn (targ_piece, temp);
3142 else
3143 emit_move_insn (targ_piece, op0_piece);
3146 insns = get_insns ();
3147 end_sequence ();
3149 emit_insn (insns);
3151 else
3153 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
3154 gen_lowpart (imode, op0),
3155 immed_wide_int_const (mask, imode),
3156 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3157 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3159 set_dst_reg_note (get_last_insn (), REG_EQUAL,
3160 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
3161 target);
3164 return target;
3167 /* As expand_unop, but will fail rather than attempt the operation in a
3168 different mode or with a libcall. */
3169 static rtx
3170 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
3171 int unsignedp)
3173 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
3175 class expand_operand ops[2];
3176 enum insn_code icode = optab_handler (unoptab, mode);
3177 rtx_insn *last = get_last_insn ();
3178 rtx_insn *pat;
3180 create_output_operand (&ops[0], target, mode);
3181 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
3182 pat = maybe_gen_insn (icode, 2, ops);
3183 if (pat)
3185 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3186 && ! add_equal_note (pat, ops[0].value,
3187 optab_to_code (unoptab),
3188 ops[1].value, NULL_RTX, mode))
3190 delete_insns_since (last);
3191 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3194 emit_insn (pat);
3196 return ops[0].value;
3199 return 0;
3202 /* Generate code to perform an operation specified by UNOPTAB
3203 on operand OP0, with result having machine-mode MODE.
3205 UNSIGNEDP is for the case where we have to widen the operands
3206 to perform the operation. It says to use zero-extension.
3208 If TARGET is nonzero, the value
3209 is generated there, if it is convenient to do so.
3210 In all cases an rtx is returned for the locus of the value;
3211 this may or may not be TARGET. */
3214 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
3215 int unsignedp)
3217 enum mode_class mclass = GET_MODE_CLASS (mode);
3218 machine_mode wider_mode;
3219 scalar_int_mode int_mode;
3220 scalar_float_mode float_mode;
3221 rtx temp;
3222 rtx libfunc;
3224 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3225 if (temp)
3226 return temp;
3228 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3230 /* Widening (or narrowing) clz needs special treatment. */
3231 if (unoptab == clz_optab)
3233 if (is_a <scalar_int_mode> (mode, &int_mode))
3235 temp = widen_leading (int_mode, op0, target, unoptab);
3236 if (temp)
3237 return temp;
3239 if (GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3240 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3242 temp = expand_doubleword_clz (int_mode, op0, target);
3243 if (temp)
3244 return temp;
3248 goto try_libcall;
3251 if (unoptab == clrsb_optab)
3253 if (is_a <scalar_int_mode> (mode, &int_mode))
3255 temp = widen_leading (int_mode, op0, target, unoptab);
3256 if (temp)
3257 return temp;
3258 temp = expand_clrsb_using_clz (int_mode, op0, target);
3259 if (temp)
3260 return temp;
3262 goto try_libcall;
3265 if (unoptab == popcount_optab
3266 && is_a <scalar_int_mode> (mode, &int_mode)
3267 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3268 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing
3269 && optimize_insn_for_speed_p ())
3271 temp = expand_doubleword_popcount (int_mode, op0, target);
3272 if (temp)
3273 return temp;
3276 if (unoptab == parity_optab
3277 && is_a <scalar_int_mode> (mode, &int_mode)
3278 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3279 && (optab_handler (unoptab, word_mode) != CODE_FOR_nothing
3280 || optab_handler (popcount_optab, word_mode) != CODE_FOR_nothing)
3281 && optimize_insn_for_speed_p ())
3283 temp = expand_doubleword_parity (int_mode, op0, target);
3284 if (temp)
3285 return temp;
3288 /* Widening (or narrowing) bswap needs special treatment. */
3289 if (unoptab == bswap_optab)
3291 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3292 or ROTATERT. First try these directly; if this fails, then try the
3293 obvious pair of shifts with allowed widening, as this will probably
3294 be always more efficient than the other fallback methods. */
3295 if (mode == HImode)
3297 rtx_insn *last;
3298 rtx temp1, temp2;
3300 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3302 temp = expand_binop (mode, rotl_optab, op0,
3303 gen_int_shift_amount (mode, 8),
3304 target, unsignedp, OPTAB_DIRECT);
3305 if (temp)
3306 return temp;
3309 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3311 temp = expand_binop (mode, rotr_optab, op0,
3312 gen_int_shift_amount (mode, 8),
3313 target, unsignedp, OPTAB_DIRECT);
3314 if (temp)
3315 return temp;
3318 last = get_last_insn ();
3320 temp1 = expand_binop (mode, ashl_optab, op0,
3321 gen_int_shift_amount (mode, 8), NULL_RTX,
3322 unsignedp, OPTAB_WIDEN);
3323 temp2 = expand_binop (mode, lshr_optab, op0,
3324 gen_int_shift_amount (mode, 8), NULL_RTX,
3325 unsignedp, OPTAB_WIDEN);
3326 if (temp1 && temp2)
3328 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3329 unsignedp, OPTAB_WIDEN);
3330 if (temp)
3331 return temp;
3334 delete_insns_since (last);
3337 if (is_a <scalar_int_mode> (mode, &int_mode))
3339 temp = widen_bswap (int_mode, op0, target);
3340 if (temp)
3341 return temp;
3343 /* We do not provide a 128-bit bswap in libgcc so force the use of
3344 a double bswap for 64-bit targets. */
3345 if (GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3346 && (UNITS_PER_WORD == 8
3347 || optab_handler (unoptab, word_mode) != CODE_FOR_nothing))
3349 temp = expand_doubleword_bswap (mode, op0, target);
3350 if (temp)
3351 return temp;
3355 goto try_libcall;
3358 if (CLASS_HAS_WIDER_MODES_P (mclass))
3359 FOR_EACH_WIDER_MODE (wider_mode, mode)
3361 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3363 rtx xop0 = op0;
3364 rtx_insn *last = get_last_insn ();
3366 /* For certain operations, we need not actually extend
3367 the narrow operand, as long as we will truncate the
3368 results to the same narrowness. */
3370 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3371 (unoptab == neg_optab
3372 || unoptab == one_cmpl_optab)
3373 && mclass == MODE_INT);
3375 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3376 unsignedp);
3378 if (temp)
3380 if (mclass != MODE_INT
3381 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3383 if (target == 0)
3384 target = gen_reg_rtx (mode);
3385 convert_move (target, temp, 0);
3386 return target;
3388 else
3389 return gen_lowpart (mode, temp);
3391 else
3392 delete_insns_since (last);
3396 /* These can be done a word at a time. */
3397 if (unoptab == one_cmpl_optab
3398 && is_int_mode (mode, &int_mode)
3399 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
3400 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3402 int i;
3403 rtx_insn *insns;
3405 if (target == 0
3406 || target == op0
3407 || reg_overlap_mentioned_p (target, op0)
3408 || !valid_multiword_target_p (target))
3409 target = gen_reg_rtx (int_mode);
3411 start_sequence ();
3413 /* Do the actual arithmetic. */
3414 for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
3416 rtx target_piece = operand_subword (target, i, 1, int_mode);
3417 rtx x = expand_unop (word_mode, unoptab,
3418 operand_subword_force (op0, i, int_mode),
3419 target_piece, unsignedp);
3421 if (target_piece != x)
3422 emit_move_insn (target_piece, x);
3425 insns = get_insns ();
3426 end_sequence ();
3428 emit_insn (insns);
3429 return target;
3432 /* Emit ~op0 as op0 ^ -1. */
3433 if (unoptab == one_cmpl_optab
3434 && (SCALAR_INT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3435 && optab_handler (xor_optab, mode) != CODE_FOR_nothing)
3437 temp = expand_binop (mode, xor_optab, op0, CONSTM1_RTX (mode),
3438 target, unsignedp, OPTAB_DIRECT);
3439 if (temp)
3440 return temp;
3443 if (optab_to_code (unoptab) == NEG)
3445 /* Try negating floating point values by flipping the sign bit. */
3446 if (is_a <scalar_float_mode> (mode, &float_mode))
3448 temp = expand_absneg_bit (NEG, float_mode, op0, target);
3449 if (temp)
3450 return temp;
3453 /* If there is no negation pattern, and we have no negative zero,
3454 try subtracting from zero. */
3455 if (!HONOR_SIGNED_ZEROS (mode))
3457 temp = expand_binop (mode, (unoptab == negv_optab
3458 ? subv_optab : sub_optab),
3459 CONST0_RTX (mode), op0, target,
3460 unsignedp, OPTAB_DIRECT);
3461 if (temp)
3462 return temp;
3466 /* Try calculating parity (x) as popcount (x) % 2. */
3467 if (unoptab == parity_optab && is_a <scalar_int_mode> (mode, &int_mode))
3469 temp = expand_parity (int_mode, op0, target);
3470 if (temp)
3471 return temp;
3474 /* Try implementing ffs (x) in terms of clz (x). */
3475 if (unoptab == ffs_optab && is_a <scalar_int_mode> (mode, &int_mode))
3477 temp = expand_ffs (int_mode, op0, target);
3478 if (temp)
3479 return temp;
3482 /* Try implementing ctz (x) in terms of clz (x). */
3483 if (unoptab == ctz_optab && is_a <scalar_int_mode> (mode, &int_mode))
3485 temp = expand_ctz (int_mode, op0, target);
3486 if (temp)
3487 return temp;
3490 try_libcall:
3491 /* Now try a library call in this mode. */
3492 libfunc = optab_libfunc (unoptab, mode);
3493 if (libfunc)
3495 rtx_insn *insns;
3496 rtx value;
3497 rtx eq_value;
3498 machine_mode outmode = mode;
3500 /* All of these functions return small values. Thus we choose to
3501 have them return something that isn't a double-word. */
3502 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3503 || unoptab == clrsb_optab || unoptab == popcount_optab
3504 || unoptab == parity_optab)
3505 outmode
3506 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3507 optab_libfunc (unoptab, mode)));
3509 start_sequence ();
3511 /* Pass 1 for NO_QUEUE so we don't lose any increments
3512 if the libcall is cse'd or moved. */
3513 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3514 op0, mode);
3515 insns = get_insns ();
3516 end_sequence ();
3518 target = gen_reg_rtx (outmode);
3519 bool trapv = trapv_unoptab_p (unoptab);
3520 if (trapv)
3521 eq_value = NULL_RTX;
3522 else
3524 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3525 if (GET_MODE_UNIT_SIZE (outmode) < GET_MODE_UNIT_SIZE (mode))
3526 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3527 else if (GET_MODE_UNIT_SIZE (outmode) > GET_MODE_UNIT_SIZE (mode))
3528 eq_value = simplify_gen_unary (ZERO_EXTEND,
3529 outmode, eq_value, mode);
3531 emit_libcall_block_1 (insns, target, value, eq_value, trapv);
3533 return target;
3536 /* It can't be done in this mode. Can we do it in a wider mode? */
3538 if (CLASS_HAS_WIDER_MODES_P (mclass))
3540 FOR_EACH_WIDER_MODE (wider_mode, mode)
3542 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3543 || optab_libfunc (unoptab, wider_mode))
3545 rtx xop0 = op0;
3546 rtx_insn *last = get_last_insn ();
3548 /* For certain operations, we need not actually extend
3549 the narrow operand, as long as we will truncate the
3550 results to the same narrowness. */
3551 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3552 (unoptab == neg_optab
3553 || unoptab == one_cmpl_optab
3554 || unoptab == bswap_optab)
3555 && mclass == MODE_INT);
3557 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3558 unsignedp);
3560 /* If we are generating clz using wider mode, adjust the
3561 result. Similarly for clrsb. */
3562 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3563 && temp != 0)
3565 scalar_int_mode wider_int_mode
3566 = as_a <scalar_int_mode> (wider_mode);
3567 int_mode = as_a <scalar_int_mode> (mode);
3568 temp = expand_binop
3569 (wider_mode, sub_optab, temp,
3570 gen_int_mode (GET_MODE_PRECISION (wider_int_mode)
3571 - GET_MODE_PRECISION (int_mode),
3572 wider_int_mode),
3573 target, true, OPTAB_DIRECT);
3576 /* Likewise for bswap. */
3577 if (unoptab == bswap_optab && temp != 0)
3579 scalar_int_mode wider_int_mode
3580 = as_a <scalar_int_mode> (wider_mode);
3581 int_mode = as_a <scalar_int_mode> (mode);
3582 gcc_assert (GET_MODE_PRECISION (wider_int_mode)
3583 == GET_MODE_BITSIZE (wider_int_mode)
3584 && GET_MODE_PRECISION (int_mode)
3585 == GET_MODE_BITSIZE (int_mode));
3587 temp = expand_shift (RSHIFT_EXPR, wider_int_mode, temp,
3588 GET_MODE_BITSIZE (wider_int_mode)
3589 - GET_MODE_BITSIZE (int_mode),
3590 NULL_RTX, true);
3593 if (temp)
3595 if (mclass != MODE_INT)
3597 if (target == 0)
3598 target = gen_reg_rtx (mode);
3599 convert_move (target, temp, 0);
3600 return target;
3602 else
3603 return gen_lowpart (mode, temp);
3605 else
3606 delete_insns_since (last);
3611 /* One final attempt at implementing negation via subtraction,
3612 this time allowing widening of the operand. */
3613 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3615 rtx temp;
3616 temp = expand_binop (mode,
3617 unoptab == negv_optab ? subv_optab : sub_optab,
3618 CONST0_RTX (mode), op0,
3619 target, unsignedp, OPTAB_LIB_WIDEN);
3620 if (temp)
3621 return temp;
3624 return 0;
3627 /* Emit code to compute the absolute value of OP0, with result to
3628 TARGET if convenient. (TARGET may be 0.) The return value says
3629 where the result actually is to be found.
3631 MODE is the mode of the operand; the mode of the result is
3632 different but can be deduced from MODE.
3637 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3638 int result_unsignedp)
3640 rtx temp;
3642 if (GET_MODE_CLASS (mode) != MODE_INT
3643 || ! flag_trapv)
3644 result_unsignedp = 1;
3646 /* First try to do it with a special abs instruction. */
3647 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3648 op0, target, 0);
3649 if (temp != 0)
3650 return temp;
3652 /* For floating point modes, try clearing the sign bit. */
3653 scalar_float_mode float_mode;
3654 if (is_a <scalar_float_mode> (mode, &float_mode))
3656 temp = expand_absneg_bit (ABS, float_mode, op0, target);
3657 if (temp)
3658 return temp;
3661 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3662 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3663 && !HONOR_SIGNED_ZEROS (mode))
3665 rtx_insn *last = get_last_insn ();
3667 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3668 op0, NULL_RTX, 0);
3669 if (temp != 0)
3670 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3671 OPTAB_WIDEN);
3673 if (temp != 0)
3674 return temp;
3676 delete_insns_since (last);
3679 /* If this machine has expensive jumps, we can do integer absolute
3680 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3681 where W is the width of MODE. */
3683 scalar_int_mode int_mode;
3684 if (is_int_mode (mode, &int_mode)
3685 && BRANCH_COST (optimize_insn_for_speed_p (),
3686 false) >= 2)
3688 rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
3689 GET_MODE_PRECISION (int_mode) - 1,
3690 NULL_RTX, 0);
3692 temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
3693 OPTAB_LIB_WIDEN);
3694 if (temp != 0)
3695 temp = expand_binop (int_mode,
3696 result_unsignedp ? sub_optab : subv_optab,
3697 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3699 if (temp != 0)
3700 return temp;
3703 return NULL_RTX;
3707 expand_abs (machine_mode mode, rtx op0, rtx target,
3708 int result_unsignedp, int safe)
3710 rtx temp;
3711 rtx_code_label *op1;
3713 if (GET_MODE_CLASS (mode) != MODE_INT
3714 || ! flag_trapv)
3715 result_unsignedp = 1;
3717 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3718 if (temp != 0)
3719 return temp;
3721 /* If that does not win, use conditional jump and negate. */
3723 /* It is safe to use the target if it is the same
3724 as the source if this is also a pseudo register */
3725 if (op0 == target && REG_P (op0)
3726 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3727 safe = 1;
3729 op1 = gen_label_rtx ();
3730 if (target == 0 || ! safe
3731 || GET_MODE (target) != mode
3732 || (MEM_P (target) && MEM_VOLATILE_P (target))
3733 || (REG_P (target)
3734 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3735 target = gen_reg_rtx (mode);
3737 emit_move_insn (target, op0);
3738 NO_DEFER_POP;
3740 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3741 NULL_RTX, NULL, op1,
3742 profile_probability::uninitialized ());
3744 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3745 target, target, 0);
3746 if (op0 != target)
3747 emit_move_insn (target, op0);
3748 emit_label (op1);
3749 OK_DEFER_POP;
3750 return target;
3753 /* Emit code to compute the one's complement absolute value of OP0
3754 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3755 (TARGET may be NULL_RTX.) The return value says where the result
3756 actually is to be found.
3758 MODE is the mode of the operand; the mode of the result is
3759 different but can be deduced from MODE. */
3762 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3764 rtx temp;
3766 /* Not applicable for floating point modes. */
3767 if (FLOAT_MODE_P (mode))
3768 return NULL_RTX;
3770 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3771 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3773 rtx_insn *last = get_last_insn ();
3775 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3776 if (temp != 0)
3777 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3778 OPTAB_WIDEN);
3780 if (temp != 0)
3781 return temp;
3783 delete_insns_since (last);
3786 /* If this machine has expensive jumps, we can do one's complement
3787 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3789 scalar_int_mode int_mode;
3790 if (is_int_mode (mode, &int_mode)
3791 && BRANCH_COST (optimize_insn_for_speed_p (),
3792 false) >= 2)
3794 rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
3795 GET_MODE_PRECISION (int_mode) - 1,
3796 NULL_RTX, 0);
3798 temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
3799 OPTAB_LIB_WIDEN);
3801 if (temp != 0)
3802 return temp;
3805 return NULL_RTX;
3808 /* A subroutine of expand_copysign, perform the copysign operation using the
3809 abs and neg primitives advertised to exist on the target. The assumption
3810 is that we have a split register file, and leaving op0 in fp registers,
3811 and not playing with subregs so much, will help the register allocator. */
3813 static rtx
3814 expand_copysign_absneg (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3815 int bitpos, bool op0_is_abs)
3817 scalar_int_mode imode;
3818 enum insn_code icode;
3819 rtx sign;
3820 rtx_code_label *label;
3822 if (target == op1)
3823 target = NULL_RTX;
3825 /* Check if the back end provides an insn that handles signbit for the
3826 argument's mode. */
3827 icode = optab_handler (signbit_optab, mode);
3828 if (icode != CODE_FOR_nothing)
3830 imode = as_a <scalar_int_mode> (insn_data[(int) icode].operand[0].mode);
3831 sign = gen_reg_rtx (imode);
3832 emit_unop_insn (icode, sign, op1, UNKNOWN);
3834 else
3836 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3838 if (!int_mode_for_mode (mode).exists (&imode))
3839 return NULL_RTX;
3840 op1 = gen_lowpart (imode, op1);
3842 else
3844 int word;
3846 imode = word_mode;
3847 if (FLOAT_WORDS_BIG_ENDIAN)
3848 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3849 else
3850 word = bitpos / BITS_PER_WORD;
3851 bitpos = bitpos % BITS_PER_WORD;
3852 op1 = operand_subword_force (op1, word, mode);
3855 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3856 sign = expand_binop (imode, and_optab, op1,
3857 immed_wide_int_const (mask, imode),
3858 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3861 if (!op0_is_abs)
3863 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3864 if (op0 == NULL)
3865 return NULL_RTX;
3866 target = op0;
3868 else
3870 if (target == NULL_RTX)
3871 target = copy_to_reg (op0);
3872 else
3873 emit_move_insn (target, op0);
3876 label = gen_label_rtx ();
3877 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3879 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3880 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3881 else
3882 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3883 if (op0 != target)
3884 emit_move_insn (target, op0);
3886 emit_label (label);
3888 return target;
3892 /* A subroutine of expand_copysign, perform the entire copysign operation
3893 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3894 is true if op0 is known to have its sign bit clear. */
3896 static rtx
3897 expand_copysign_bit (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3898 int bitpos, bool op0_is_abs)
3900 scalar_int_mode imode;
3901 int word, nwords, i;
3902 rtx temp;
3903 rtx_insn *insns;
3905 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3907 if (!int_mode_for_mode (mode).exists (&imode))
3908 return NULL_RTX;
3909 word = 0;
3910 nwords = 1;
3912 else
3914 imode = word_mode;
3916 if (FLOAT_WORDS_BIG_ENDIAN)
3917 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3918 else
3919 word = bitpos / BITS_PER_WORD;
3920 bitpos = bitpos % BITS_PER_WORD;
3921 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3924 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3926 if (target == 0
3927 || target == op0
3928 || target == op1
3929 || reg_overlap_mentioned_p (target, op0)
3930 || reg_overlap_mentioned_p (target, op1)
3931 || (nwords > 1 && !valid_multiword_target_p (target)))
3932 target = gen_reg_rtx (mode);
3934 if (nwords > 1)
3936 start_sequence ();
3938 for (i = 0; i < nwords; ++i)
3940 rtx targ_piece = operand_subword (target, i, 1, mode);
3941 rtx op0_piece = operand_subword_force (op0, i, mode);
3943 if (i == word)
3945 if (!op0_is_abs)
3946 op0_piece
3947 = expand_binop (imode, and_optab, op0_piece,
3948 immed_wide_int_const (~mask, imode),
3949 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3950 op1 = expand_binop (imode, and_optab,
3951 operand_subword_force (op1, i, mode),
3952 immed_wide_int_const (mask, imode),
3953 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3955 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3956 targ_piece, 1, OPTAB_LIB_WIDEN);
3957 if (temp != targ_piece)
3958 emit_move_insn (targ_piece, temp);
3960 else
3961 emit_move_insn (targ_piece, op0_piece);
3964 insns = get_insns ();
3965 end_sequence ();
3967 emit_insn (insns);
3969 else
3971 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3972 immed_wide_int_const (mask, imode),
3973 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3975 op0 = gen_lowpart (imode, op0);
3976 if (!op0_is_abs)
3977 op0 = expand_binop (imode, and_optab, op0,
3978 immed_wide_int_const (~mask, imode),
3979 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3981 temp = expand_binop (imode, ior_optab, op0, op1,
3982 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3983 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3986 return target;
3989 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3990 scalar floating point mode. Return NULL if we do not know how to
3991 expand the operation inline. */
3994 expand_copysign (rtx op0, rtx op1, rtx target)
3996 scalar_float_mode mode;
3997 const struct real_format *fmt;
3998 bool op0_is_abs;
3999 rtx temp;
4001 mode = as_a <scalar_float_mode> (GET_MODE (op0));
4002 gcc_assert (GET_MODE (op1) == mode);
4004 /* First try to do it with a special instruction. */
4005 temp = expand_binop (mode, copysign_optab, op0, op1,
4006 target, 0, OPTAB_DIRECT);
4007 if (temp)
4008 return temp;
4010 fmt = REAL_MODE_FORMAT (mode);
4011 if (fmt == NULL || !fmt->has_signed_zero)
4012 return NULL_RTX;
4014 op0_is_abs = false;
4015 if (CONST_DOUBLE_AS_FLOAT_P (op0))
4017 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
4018 op0 = simplify_unary_operation (ABS, mode, op0, mode);
4019 op0_is_abs = true;
4022 if (fmt->signbit_ro >= 0
4023 && (CONST_DOUBLE_AS_FLOAT_P (op0)
4024 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
4025 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
4027 temp = expand_copysign_absneg (mode, op0, op1, target,
4028 fmt->signbit_ro, op0_is_abs);
4029 if (temp)
4030 return temp;
4033 if (fmt->signbit_rw < 0)
4034 return NULL_RTX;
4035 return expand_copysign_bit (mode, op0, op1, target,
4036 fmt->signbit_rw, op0_is_abs);
4039 /* Generate an instruction whose insn-code is INSN_CODE,
4040 with two operands: an output TARGET and an input OP0.
4041 TARGET *must* be nonzero, and the output is always stored there.
4042 CODE is an rtx code such that (CODE OP0) is an rtx that describes
4043 the value that is stored into TARGET.
4045 Return false if expansion failed. */
4047 bool
4048 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
4049 enum rtx_code code)
4051 class expand_operand ops[2];
4052 rtx_insn *pat;
4054 create_output_operand (&ops[0], target, GET_MODE (target));
4055 create_input_operand (&ops[1], op0, GET_MODE (op0));
4056 pat = maybe_gen_insn (icode, 2, ops);
4057 if (!pat)
4058 return false;
4060 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
4061 && code != UNKNOWN)
4062 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX,
4063 GET_MODE (op0));
4065 emit_insn (pat);
4067 if (ops[0].value != target)
4068 emit_move_insn (target, ops[0].value);
4069 return true;
4071 /* Generate an instruction whose insn-code is INSN_CODE,
4072 with two operands: an output TARGET and an input OP0.
4073 TARGET *must* be nonzero, and the output is always stored there.
4074 CODE is an rtx code such that (CODE OP0) is an rtx that describes
4075 the value that is stored into TARGET. */
4077 void
4078 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
4080 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
4081 gcc_assert (ok);
4084 struct no_conflict_data
4086 rtx target;
4087 rtx_insn *first, *insn;
4088 bool must_stay;
4091 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
4092 the currently examined clobber / store has to stay in the list of
4093 insns that constitute the actual libcall block. */
4094 static void
4095 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
4097 struct no_conflict_data *p= (struct no_conflict_data *) p0;
4099 /* If this inns directly contributes to setting the target, it must stay. */
4100 if (reg_overlap_mentioned_p (p->target, dest))
4101 p->must_stay = true;
4102 /* If we haven't committed to keeping any other insns in the list yet,
4103 there is nothing more to check. */
4104 else if (p->insn == p->first)
4105 return;
4106 /* If this insn sets / clobbers a register that feeds one of the insns
4107 already in the list, this insn has to stay too. */
4108 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
4109 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
4110 || reg_used_between_p (dest, p->first, p->insn)
4111 /* Likewise if this insn depends on a register set by a previous
4112 insn in the list, or if it sets a result (presumably a hard
4113 register) that is set or clobbered by a previous insn.
4114 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
4115 SET_DEST perform the former check on the address, and the latter
4116 check on the MEM. */
4117 || (GET_CODE (set) == SET
4118 && (modified_in_p (SET_SRC (set), p->first)
4119 || modified_in_p (SET_DEST (set), p->first)
4120 || modified_between_p (SET_SRC (set), p->first, p->insn)
4121 || modified_between_p (SET_DEST (set), p->first, p->insn))))
4122 p->must_stay = true;
4126 /* Emit code to make a call to a constant function or a library call.
4128 INSNS is a list containing all insns emitted in the call.
4129 These insns leave the result in RESULT. Our block is to copy RESULT
4130 to TARGET, which is logically equivalent to EQUIV.
4132 We first emit any insns that set a pseudo on the assumption that these are
4133 loading constants into registers; doing so allows them to be safely cse'ed
4134 between blocks. Then we emit all the other insns in the block, followed by
4135 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
4136 note with an operand of EQUIV. */
4138 static void
4139 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
4140 bool equiv_may_trap)
4142 rtx final_dest = target;
4143 rtx_insn *next, *last, *insn;
4145 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
4146 into a MEM later. Protect the libcall block from this change. */
4147 if (! REG_P (target) || REG_USERVAR_P (target))
4148 target = gen_reg_rtx (GET_MODE (target));
4150 /* If we're using non-call exceptions, a libcall corresponding to an
4151 operation that may trap may also trap. */
4152 /* ??? See the comment in front of make_reg_eh_region_note. */
4153 if (cfun->can_throw_non_call_exceptions
4154 && (equiv_may_trap || may_trap_p (equiv)))
4156 for (insn = insns; insn; insn = NEXT_INSN (insn))
4157 if (CALL_P (insn))
4159 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
4160 if (note)
4162 int lp_nr = INTVAL (XEXP (note, 0));
4163 if (lp_nr == 0 || lp_nr == INT_MIN)
4164 remove_note (insn, note);
4168 else
4170 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
4171 reg note to indicate that this call cannot throw or execute a nonlocal
4172 goto (unless there is already a REG_EH_REGION note, in which case
4173 we update it). */
4174 for (insn = insns; insn; insn = NEXT_INSN (insn))
4175 if (CALL_P (insn))
4176 make_reg_eh_region_note_nothrow_nononlocal (insn);
4179 /* First emit all insns that set pseudos. Remove them from the list as
4180 we go. Avoid insns that set pseudos which were referenced in previous
4181 insns. These can be generated by move_by_pieces, for example,
4182 to update an address. Similarly, avoid insns that reference things
4183 set in previous insns. */
4185 for (insn = insns; insn; insn = next)
4187 rtx set = single_set (insn);
4189 next = NEXT_INSN (insn);
4191 if (set != 0 && REG_P (SET_DEST (set))
4192 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
4194 struct no_conflict_data data;
4196 data.target = const0_rtx;
4197 data.first = insns;
4198 data.insn = insn;
4199 data.must_stay = 0;
4200 note_stores (insn, no_conflict_move_test, &data);
4201 if (! data.must_stay)
4203 if (PREV_INSN (insn))
4204 SET_NEXT_INSN (PREV_INSN (insn)) = next;
4205 else
4206 insns = next;
4208 if (next)
4209 SET_PREV_INSN (next) = PREV_INSN (insn);
4211 add_insn (insn);
4215 /* Some ports use a loop to copy large arguments onto the stack.
4216 Don't move anything outside such a loop. */
4217 if (LABEL_P (insn))
4218 break;
4221 /* Write the remaining insns followed by the final copy. */
4222 for (insn = insns; insn; insn = next)
4224 next = NEXT_INSN (insn);
4226 add_insn (insn);
4229 last = emit_move_insn (target, result);
4230 if (equiv)
4231 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
4233 if (final_dest != target)
4234 emit_move_insn (final_dest, target);
4237 void
4238 emit_libcall_block (rtx_insn *insns, rtx target, rtx result, rtx equiv)
4240 emit_libcall_block_1 (insns, target, result, equiv, false);
4243 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
4244 PURPOSE describes how this comparison will be used. CODE is the rtx
4245 comparison code we will be using.
4247 ??? Actually, CODE is slightly weaker than that. A target is still
4248 required to implement all of the normal bcc operations, but not
4249 required to implement all (or any) of the unordered bcc operations. */
4252 can_compare_p (enum rtx_code code, machine_mode mode,
4253 enum can_compare_purpose purpose)
4255 rtx test;
4256 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
4259 enum insn_code icode;
4261 if (purpose == ccp_jump
4262 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4263 && insn_operand_matches (icode, 0, test))
4264 return 1;
4265 if (purpose == ccp_store_flag
4266 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4267 && insn_operand_matches (icode, 1, test))
4268 return 1;
4269 if (purpose == ccp_cmov
4270 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4271 return 1;
4273 mode = GET_MODE_WIDER_MODE (mode).else_void ();
4274 PUT_MODE (test, mode);
4276 while (mode != VOIDmode);
4278 return 0;
4281 /* Return whether RTL code CODE corresponds to an unsigned optab. */
4283 static bool
4284 unsigned_optab_p (enum rtx_code code)
4286 return code == LTU || code == LEU || code == GTU || code == GEU;
4289 /* Return whether the backend-emitted comparison for code CODE, comparing
4290 operands of mode VALUE_MODE and producing a result with MASK_MODE, matches
4291 operand OPNO of pattern ICODE. */
4293 static bool
4294 insn_predicate_matches_p (enum insn_code icode, unsigned int opno,
4295 enum rtx_code code, machine_mode mask_mode,
4296 machine_mode value_mode)
4298 rtx reg1 = alloca_raw_REG (value_mode, LAST_VIRTUAL_REGISTER + 1);
4299 rtx reg2 = alloca_raw_REG (value_mode, LAST_VIRTUAL_REGISTER + 2);
4300 rtx test = alloca_rtx_fmt_ee (code, mask_mode, reg1, reg2);
4301 return insn_operand_matches (icode, opno, test);
4304 /* Return whether the backend can emit a vector comparison (vec_cmp/vec_cmpu)
4305 for code CODE, comparing operands of mode VALUE_MODE and producing a result
4306 with MASK_MODE. */
4308 bool
4309 can_vec_cmp_compare_p (enum rtx_code code, machine_mode value_mode,
4310 machine_mode mask_mode)
4312 enum insn_code icode
4313 = get_vec_cmp_icode (value_mode, mask_mode, unsigned_optab_p (code));
4314 if (icode == CODE_FOR_nothing)
4315 return false;
4317 return insn_predicate_matches_p (icode, 1, code, mask_mode, value_mode);
4320 /* Return whether the backend can emit a vector comparison (vcond/vcondu) for
4321 code CODE, comparing operands of mode CMP_OP_MODE and producing a result
4322 with VALUE_MODE. */
4324 bool
4325 can_vcond_compare_p (enum rtx_code code, machine_mode value_mode,
4326 machine_mode cmp_op_mode)
4328 enum insn_code icode
4329 = get_vcond_icode (value_mode, cmp_op_mode, unsigned_optab_p (code));
4330 if (icode == CODE_FOR_nothing)
4331 return false;
4333 return insn_predicate_matches_p (icode, 3, code, value_mode, cmp_op_mode);
4336 /* Return whether the backend can emit vector set instructions for inserting
4337 element into vector at variable index position. */
4339 bool
4340 can_vec_set_var_idx_p (machine_mode vec_mode)
4342 if (!VECTOR_MODE_P (vec_mode))
4343 return false;
4345 machine_mode inner_mode = GET_MODE_INNER (vec_mode);
4346 rtx reg1 = alloca_raw_REG (vec_mode, LAST_VIRTUAL_REGISTER + 1);
4347 rtx reg2 = alloca_raw_REG (inner_mode, LAST_VIRTUAL_REGISTER + 2);
4348 rtx reg3 = alloca_raw_REG (VOIDmode, LAST_VIRTUAL_REGISTER + 3);
4350 enum insn_code icode = optab_handler (vec_set_optab, vec_mode);
4352 return icode != CODE_FOR_nothing && insn_operand_matches (icode, 0, reg1)
4353 && insn_operand_matches (icode, 1, reg2)
4354 && insn_operand_matches (icode, 2, reg3);
4357 /* This function is called when we are going to emit a compare instruction that
4358 compares the values found in X and Y, using the rtl operator COMPARISON.
4360 If they have mode BLKmode, then SIZE specifies the size of both operands.
4362 UNSIGNEDP nonzero says that the operands are unsigned;
4363 this matters if they need to be widened (as given by METHODS).
4365 *PTEST is where the resulting comparison RTX is returned or NULL_RTX
4366 if we failed to produce one.
4368 *PMODE is the mode of the inputs (in case they are const_int).
4370 This function performs all the setup necessary so that the caller only has
4371 to emit a single comparison insn. This setup can involve doing a BLKmode
4372 comparison or emitting a library call to perform the comparison if no insn
4373 is available to handle it.
4374 The values which are passed in through pointers can be modified; the caller
4375 should perform the comparison on the modified values. Constant
4376 comparisons must have already been folded. */
4378 static void
4379 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4380 int unsignedp, enum optab_methods methods,
4381 rtx *ptest, machine_mode *pmode)
4383 machine_mode mode = *pmode;
4384 rtx libfunc, test;
4385 machine_mode cmp_mode;
4386 enum mode_class mclass;
4388 /* The other methods are not needed. */
4389 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4390 || methods == OPTAB_LIB_WIDEN);
4392 if (CONST_SCALAR_INT_P (y))
4393 canonicalize_comparison (mode, &comparison, &y);
4395 /* If we are optimizing, force expensive constants into a register. */
4396 if (CONSTANT_P (x) && optimize
4397 && (rtx_cost (x, mode, COMPARE, 0, optimize_insn_for_speed_p ())
4398 > COSTS_N_INSNS (1)))
4399 x = force_reg (mode, x);
4401 if (CONSTANT_P (y) && optimize
4402 && (rtx_cost (y, mode, COMPARE, 1, optimize_insn_for_speed_p ())
4403 > COSTS_N_INSNS (1)))
4404 y = force_reg (mode, y);
4406 /* Don't let both operands fail to indicate the mode. */
4407 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4408 x = force_reg (mode, x);
4409 if (mode == VOIDmode)
4410 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4412 /* Handle all BLKmode compares. */
4414 if (mode == BLKmode)
4416 machine_mode result_mode;
4417 enum insn_code cmp_code;
4418 rtx result;
4419 rtx opalign
4420 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4422 gcc_assert (size);
4424 /* Try to use a memory block compare insn - either cmpstr
4425 or cmpmem will do. */
4426 opt_scalar_int_mode cmp_mode_iter;
4427 FOR_EACH_MODE_IN_CLASS (cmp_mode_iter, MODE_INT)
4429 scalar_int_mode cmp_mode = cmp_mode_iter.require ();
4430 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4431 if (cmp_code == CODE_FOR_nothing)
4432 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4433 if (cmp_code == CODE_FOR_nothing)
4434 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4435 if (cmp_code == CODE_FOR_nothing)
4436 continue;
4438 /* Must make sure the size fits the insn's mode. */
4439 if (CONST_INT_P (size)
4440 ? UINTVAL (size) > GET_MODE_MASK (cmp_mode)
4441 : (GET_MODE_BITSIZE (as_a <scalar_int_mode> (GET_MODE (size)))
4442 > GET_MODE_BITSIZE (cmp_mode)))
4443 continue;
4445 result_mode = insn_data[cmp_code].operand[0].mode;
4446 result = gen_reg_rtx (result_mode);
4447 size = convert_to_mode (cmp_mode, size, 1);
4448 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4450 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4451 *pmode = result_mode;
4452 return;
4455 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4456 goto fail;
4458 /* Otherwise call a library function. */
4459 result = emit_block_comp_via_libcall (x, y, size);
4461 x = result;
4462 y = const0_rtx;
4463 mode = TYPE_MODE (integer_type_node);
4464 methods = OPTAB_LIB_WIDEN;
4465 unsignedp = false;
4468 /* Don't allow operands to the compare to trap, as that can put the
4469 compare and branch in different basic blocks. */
4470 if (cfun->can_throw_non_call_exceptions)
4472 if (may_trap_p (x))
4473 x = copy_to_reg (x);
4474 if (may_trap_p (y))
4475 y = copy_to_reg (y);
4478 if (GET_MODE_CLASS (mode) == MODE_CC)
4480 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
4481 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4482 gcc_assert (icode != CODE_FOR_nothing
4483 && insn_operand_matches (icode, 0, test));
4484 *ptest = test;
4485 return;
4488 mclass = GET_MODE_CLASS (mode);
4489 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4490 FOR_EACH_MODE_FROM (cmp_mode, mode)
4492 enum insn_code icode;
4493 icode = optab_handler (cbranch_optab, cmp_mode);
4494 if (icode != CODE_FOR_nothing
4495 && insn_operand_matches (icode, 0, test))
4497 rtx_insn *last = get_last_insn ();
4498 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4499 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4500 if (op0 && op1
4501 && insn_operand_matches (icode, 1, op0)
4502 && insn_operand_matches (icode, 2, op1))
4504 XEXP (test, 0) = op0;
4505 XEXP (test, 1) = op1;
4506 *ptest = test;
4507 *pmode = cmp_mode;
4508 return;
4510 delete_insns_since (last);
4513 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4514 break;
4517 if (methods != OPTAB_LIB_WIDEN)
4518 goto fail;
4520 if (SCALAR_FLOAT_MODE_P (mode))
4522 /* Small trick if UNORDERED isn't implemented by the hardware. */
4523 if (comparison == UNORDERED && rtx_equal_p (x, y))
4525 prepare_cmp_insn (x, y, UNLT, NULL_RTX, unsignedp, OPTAB_WIDEN,
4526 ptest, pmode);
4527 if (*ptest)
4528 return;
4531 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4533 else
4535 rtx result;
4536 machine_mode ret_mode;
4538 /* Handle a libcall just for the mode we are using. */
4539 libfunc = optab_libfunc (cmp_optab, mode);
4540 gcc_assert (libfunc);
4542 /* If we want unsigned, and this mode has a distinct unsigned
4543 comparison routine, use that. */
4544 if (unsignedp)
4546 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4547 if (ulibfunc)
4548 libfunc = ulibfunc;
4551 ret_mode = targetm.libgcc_cmp_return_mode ();
4552 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4553 ret_mode, x, mode, y, mode);
4555 /* There are two kinds of comparison routines. Biased routines
4556 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4557 of gcc expect that the comparison operation is equivalent
4558 to the modified comparison. For signed comparisons compare the
4559 result against 1 in the biased case, and zero in the unbiased
4560 case. For unsigned comparisons always compare against 1 after
4561 biasing the unbiased result by adding 1. This gives us a way to
4562 represent LTU.
4563 The comparisons in the fixed-point helper library are always
4564 biased. */
4565 x = result;
4566 y = const1_rtx;
4568 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4570 if (unsignedp)
4571 x = plus_constant (ret_mode, result, 1);
4572 else
4573 y = const0_rtx;
4576 *pmode = ret_mode;
4577 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4578 ptest, pmode);
4581 return;
4583 fail:
4584 *ptest = NULL_RTX;
4587 /* Before emitting an insn with code ICODE, make sure that X, which is going
4588 to be used for operand OPNUM of the insn, is converted from mode MODE to
4589 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4590 that it is accepted by the operand predicate. Return the new value. */
4593 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
4594 machine_mode wider_mode, int unsignedp)
4596 if (mode != wider_mode)
4597 x = convert_modes (wider_mode, mode, x, unsignedp);
4599 if (!insn_operand_matches (icode, opnum, x))
4601 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
4602 if (reload_completed)
4603 return NULL_RTX;
4604 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
4605 return NULL_RTX;
4606 x = copy_to_mode_reg (op_mode, x);
4609 return x;
4612 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4613 we can do the branch. */
4615 static void
4616 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label,
4617 profile_probability prob)
4619 machine_mode optab_mode;
4620 enum mode_class mclass;
4621 enum insn_code icode;
4622 rtx_insn *insn;
4624 mclass = GET_MODE_CLASS (mode);
4625 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4626 icode = optab_handler (cbranch_optab, optab_mode);
4628 gcc_assert (icode != CODE_FOR_nothing);
4629 gcc_assert (insn_operand_matches (icode, 0, test));
4630 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4631 XEXP (test, 1), label));
4632 if (prob.initialized_p ()
4633 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4634 && insn
4635 && JUMP_P (insn)
4636 && any_condjump_p (insn)
4637 && !find_reg_note (insn, REG_BR_PROB, 0))
4638 add_reg_br_prob_note (insn, prob);
4641 /* Generate code to compare X with Y so that the condition codes are
4642 set and to jump to LABEL if the condition is true. If X is a
4643 constant and Y is not a constant, then the comparison is swapped to
4644 ensure that the comparison RTL has the canonical form.
4646 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4647 need to be widened. UNSIGNEDP is also used to select the proper
4648 branch condition code.
4650 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4652 MODE is the mode of the inputs (in case they are const_int).
4654 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4655 It will be potentially converted into an unsigned variant based on
4656 UNSIGNEDP to select a proper jump instruction.
4658 PROB is the probability of jumping to LABEL. */
4660 void
4661 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4662 machine_mode mode, int unsignedp, rtx label,
4663 profile_probability prob)
4665 rtx op0 = x, op1 = y;
4666 rtx test;
4668 /* Swap operands and condition to ensure canonical RTL. */
4669 if (swap_commutative_operands_p (x, y)
4670 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4672 op0 = y, op1 = x;
4673 comparison = swap_condition (comparison);
4676 /* If OP0 is still a constant, then both X and Y must be constants
4677 or the opposite comparison is not supported. Force X into a register
4678 to create canonical RTL. */
4679 if (CONSTANT_P (op0))
4680 op0 = force_reg (mode, op0);
4682 if (unsignedp)
4683 comparison = unsigned_condition (comparison);
4685 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4686 &test, &mode);
4687 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4691 /* Emit a library call comparison between floating point X and Y.
4692 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4694 static void
4695 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4696 rtx *ptest, machine_mode *pmode)
4698 enum rtx_code swapped = swap_condition (comparison);
4699 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4700 machine_mode orig_mode = GET_MODE (x);
4701 machine_mode mode;
4702 rtx true_rtx, false_rtx;
4703 rtx value, target, equiv;
4704 rtx_insn *insns;
4705 rtx libfunc = 0;
4706 bool reversed_p = false;
4707 scalar_int_mode cmp_mode = targetm.libgcc_cmp_return_mode ();
4709 FOR_EACH_MODE_FROM (mode, orig_mode)
4711 if (code_to_optab (comparison)
4712 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4713 break;
4715 if (code_to_optab (swapped)
4716 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4718 std::swap (x, y);
4719 comparison = swapped;
4720 break;
4723 if (code_to_optab (reversed)
4724 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4726 comparison = reversed;
4727 reversed_p = true;
4728 break;
4732 gcc_assert (mode != VOIDmode);
4734 if (mode != orig_mode)
4736 x = convert_to_mode (mode, x, 0);
4737 y = convert_to_mode (mode, y, 0);
4740 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4741 the RTL. The allows the RTL optimizers to delete the libcall if the
4742 condition can be determined at compile-time. */
4743 if (comparison == UNORDERED
4744 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4746 true_rtx = const_true_rtx;
4747 false_rtx = const0_rtx;
4749 else
4751 switch (comparison)
4753 case EQ:
4754 true_rtx = const0_rtx;
4755 false_rtx = const_true_rtx;
4756 break;
4758 case NE:
4759 true_rtx = const_true_rtx;
4760 false_rtx = const0_rtx;
4761 break;
4763 case GT:
4764 true_rtx = const1_rtx;
4765 false_rtx = const0_rtx;
4766 break;
4768 case GE:
4769 true_rtx = const0_rtx;
4770 false_rtx = constm1_rtx;
4771 break;
4773 case LT:
4774 true_rtx = constm1_rtx;
4775 false_rtx = const0_rtx;
4776 break;
4778 case LE:
4779 true_rtx = const0_rtx;
4780 false_rtx = const1_rtx;
4781 break;
4783 default:
4784 gcc_unreachable ();
4788 if (comparison == UNORDERED)
4790 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4791 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4792 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4793 temp, const_true_rtx, equiv);
4795 else
4797 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4798 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4799 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4800 equiv, true_rtx, false_rtx);
4803 start_sequence ();
4804 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4805 cmp_mode, x, mode, y, mode);
4806 insns = get_insns ();
4807 end_sequence ();
4809 target = gen_reg_rtx (cmp_mode);
4810 emit_libcall_block (insns, target, value, equiv);
4812 if (comparison == UNORDERED
4813 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4814 || reversed_p)
4815 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4816 else
4817 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4819 *pmode = cmp_mode;
4822 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4824 void
4825 emit_indirect_jump (rtx loc)
4827 if (!targetm.have_indirect_jump ())
4828 sorry ("indirect jumps are not available on this target");
4829 else
4831 class expand_operand ops[1];
4832 create_address_operand (&ops[0], loc);
4833 expand_jump_insn (targetm.code_for_indirect_jump, 1, ops);
4834 emit_barrier ();
4839 /* Emit a conditional move instruction if the machine supports one for that
4840 condition and machine mode.
4842 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4843 the mode to use should they be constants. If it is VOIDmode, they cannot
4844 both be constants.
4846 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4847 should be stored there. MODE is the mode to use should they be constants.
4848 If it is VOIDmode, they cannot both be constants.
4850 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4851 is not supported. */
4854 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4855 machine_mode cmode, rtx op2, rtx op3,
4856 machine_mode mode, int unsignedp)
4858 rtx comparison;
4859 rtx_insn *last;
4860 enum insn_code icode;
4861 enum rtx_code reversed;
4863 /* If the two source operands are identical, that's just a move. */
4865 if (rtx_equal_p (op2, op3))
4867 if (!target)
4868 target = gen_reg_rtx (mode);
4870 emit_move_insn (target, op3);
4871 return target;
4874 /* If one operand is constant, make it the second one. Only do this
4875 if the other operand is not constant as well. */
4877 if (swap_commutative_operands_p (op0, op1))
4879 std::swap (op0, op1);
4880 code = swap_condition (code);
4883 /* get_condition will prefer to generate LT and GT even if the old
4884 comparison was against zero, so undo that canonicalization here since
4885 comparisons against zero are cheaper. */
4886 if (code == LT && op1 == const1_rtx)
4887 code = LE, op1 = const0_rtx;
4888 else if (code == GT && op1 == constm1_rtx)
4889 code = GE, op1 = const0_rtx;
4891 if (cmode == VOIDmode)
4892 cmode = GET_MODE (op0);
4894 enum rtx_code orig_code = code;
4895 bool swapped = false;
4896 if (swap_commutative_operands_p (op2, op3)
4897 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4898 != UNKNOWN))
4900 std::swap (op2, op3);
4901 code = reversed;
4902 swapped = true;
4905 if (mode == VOIDmode)
4906 mode = GET_MODE (op2);
4908 icode = direct_optab_handler (movcc_optab, mode);
4910 if (icode == CODE_FOR_nothing)
4911 return NULL_RTX;
4913 if (!target)
4914 target = gen_reg_rtx (mode);
4916 for (int pass = 0; ; pass++)
4918 code = unsignedp ? unsigned_condition (code) : code;
4919 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4921 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4922 punt and let the caller figure out how best to deal with this
4923 situation. */
4924 if (COMPARISON_P (comparison))
4926 saved_pending_stack_adjust save;
4927 save_pending_stack_adjust (&save);
4928 last = get_last_insn ();
4929 do_pending_stack_adjust ();
4930 machine_mode cmpmode = cmode;
4931 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4932 GET_CODE (comparison), NULL_RTX, unsignedp,
4933 OPTAB_WIDEN, &comparison, &cmpmode);
4934 if (comparison)
4936 class expand_operand ops[4];
4938 create_output_operand (&ops[0], target, mode);
4939 create_fixed_operand (&ops[1], comparison);
4940 create_input_operand (&ops[2], op2, mode);
4941 create_input_operand (&ops[3], op3, mode);
4942 if (maybe_expand_insn (icode, 4, ops))
4944 if (ops[0].value != target)
4945 convert_move (target, ops[0].value, false);
4946 return target;
4949 delete_insns_since (last);
4950 restore_pending_stack_adjust (&save);
4953 if (pass == 1)
4954 return NULL_RTX;
4956 /* If the preferred op2/op3 order is not usable, retry with other
4957 operand order, perhaps it will expand successfully. */
4958 if (swapped)
4959 code = orig_code;
4960 else if ((reversed = reversed_comparison_code_parts (orig_code, op0, op1,
4961 NULL))
4962 != UNKNOWN)
4963 code = reversed;
4964 else
4965 return NULL_RTX;
4966 std::swap (op2, op3);
4971 /* Emit a conditional negate or bitwise complement using the
4972 negcc or notcc optabs if available. Return NULL_RTX if such operations
4973 are not available. Otherwise return the RTX holding the result.
4974 TARGET is the desired destination of the result. COMP is the comparison
4975 on which to negate. If COND is true move into TARGET the negation
4976 or bitwise complement of OP1. Otherwise move OP2 into TARGET.
4977 CODE is either NEG or NOT. MODE is the machine mode in which the
4978 operation is performed. */
4981 emit_conditional_neg_or_complement (rtx target, rtx_code code,
4982 machine_mode mode, rtx cond, rtx op1,
4983 rtx op2)
4985 optab op = unknown_optab;
4986 if (code == NEG)
4987 op = negcc_optab;
4988 else if (code == NOT)
4989 op = notcc_optab;
4990 else
4991 gcc_unreachable ();
4993 insn_code icode = direct_optab_handler (op, mode);
4995 if (icode == CODE_FOR_nothing)
4996 return NULL_RTX;
4998 if (!target)
4999 target = gen_reg_rtx (mode);
5001 rtx_insn *last = get_last_insn ();
5002 class expand_operand ops[4];
5004 create_output_operand (&ops[0], target, mode);
5005 create_fixed_operand (&ops[1], cond);
5006 create_input_operand (&ops[2], op1, mode);
5007 create_input_operand (&ops[3], op2, mode);
5009 if (maybe_expand_insn (icode, 4, ops))
5011 if (ops[0].value != target)
5012 convert_move (target, ops[0].value, false);
5014 return target;
5016 delete_insns_since (last);
5017 return NULL_RTX;
5020 /* Emit a conditional addition instruction if the machine supports one for that
5021 condition and machine mode.
5023 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
5024 the mode to use should they be constants. If it is VOIDmode, they cannot
5025 both be constants.
5027 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
5028 should be stored there. MODE is the mode to use should they be constants.
5029 If it is VOIDmode, they cannot both be constants.
5031 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
5032 is not supported. */
5035 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
5036 machine_mode cmode, rtx op2, rtx op3,
5037 machine_mode mode, int unsignedp)
5039 rtx comparison;
5040 rtx_insn *last;
5041 enum insn_code icode;
5043 /* If one operand is constant, make it the second one. Only do this
5044 if the other operand is not constant as well. */
5046 if (swap_commutative_operands_p (op0, op1))
5048 std::swap (op0, op1);
5049 code = swap_condition (code);
5052 /* get_condition will prefer to generate LT and GT even if the old
5053 comparison was against zero, so undo that canonicalization here since
5054 comparisons against zero are cheaper. */
5055 if (code == LT && op1 == const1_rtx)
5056 code = LE, op1 = const0_rtx;
5057 else if (code == GT && op1 == constm1_rtx)
5058 code = GE, op1 = const0_rtx;
5060 if (cmode == VOIDmode)
5061 cmode = GET_MODE (op0);
5063 if (mode == VOIDmode)
5064 mode = GET_MODE (op2);
5066 icode = optab_handler (addcc_optab, mode);
5068 if (icode == CODE_FOR_nothing)
5069 return 0;
5071 if (!target)
5072 target = gen_reg_rtx (mode);
5074 code = unsignedp ? unsigned_condition (code) : code;
5075 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
5077 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
5078 return NULL and let the caller figure out how best to deal with this
5079 situation. */
5080 if (!COMPARISON_P (comparison))
5081 return NULL_RTX;
5083 do_pending_stack_adjust ();
5084 last = get_last_insn ();
5085 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
5086 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
5087 &comparison, &cmode);
5088 if (comparison)
5090 class expand_operand ops[4];
5092 create_output_operand (&ops[0], target, mode);
5093 create_fixed_operand (&ops[1], comparison);
5094 create_input_operand (&ops[2], op2, mode);
5095 create_input_operand (&ops[3], op3, mode);
5096 if (maybe_expand_insn (icode, 4, ops))
5098 if (ops[0].value != target)
5099 convert_move (target, ops[0].value, false);
5100 return target;
5103 delete_insns_since (last);
5104 return NULL_RTX;
5107 /* These functions attempt to generate an insn body, rather than
5108 emitting the insn, but if the gen function already emits them, we
5109 make no attempt to turn them back into naked patterns. */
5111 /* Generate and return an insn body to add Y to X. */
5113 rtx_insn *
5114 gen_add2_insn (rtx x, rtx y)
5116 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
5118 gcc_assert (insn_operand_matches (icode, 0, x));
5119 gcc_assert (insn_operand_matches (icode, 1, x));
5120 gcc_assert (insn_operand_matches (icode, 2, y));
5122 return GEN_FCN (icode) (x, x, y);
5125 /* Generate and return an insn body to add r1 and c,
5126 storing the result in r0. */
5128 rtx_insn *
5129 gen_add3_insn (rtx r0, rtx r1, rtx c)
5131 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
5133 if (icode == CODE_FOR_nothing
5134 || !insn_operand_matches (icode, 0, r0)
5135 || !insn_operand_matches (icode, 1, r1)
5136 || !insn_operand_matches (icode, 2, c))
5137 return NULL;
5139 return GEN_FCN (icode) (r0, r1, c);
5143 have_add2_insn (rtx x, rtx y)
5145 enum insn_code icode;
5147 gcc_assert (GET_MODE (x) != VOIDmode);
5149 icode = optab_handler (add_optab, GET_MODE (x));
5151 if (icode == CODE_FOR_nothing)
5152 return 0;
5154 if (!insn_operand_matches (icode, 0, x)
5155 || !insn_operand_matches (icode, 1, x)
5156 || !insn_operand_matches (icode, 2, y))
5157 return 0;
5159 return 1;
5162 /* Generate and return an insn body to add Y to X. */
5164 rtx_insn *
5165 gen_addptr3_insn (rtx x, rtx y, rtx z)
5167 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
5169 gcc_assert (insn_operand_matches (icode, 0, x));
5170 gcc_assert (insn_operand_matches (icode, 1, y));
5171 gcc_assert (insn_operand_matches (icode, 2, z));
5173 return GEN_FCN (icode) (x, y, z);
5176 /* Return true if the target implements an addptr pattern and X, Y,
5177 and Z are valid for the pattern predicates. */
5180 have_addptr3_insn (rtx x, rtx y, rtx z)
5182 enum insn_code icode;
5184 gcc_assert (GET_MODE (x) != VOIDmode);
5186 icode = optab_handler (addptr3_optab, GET_MODE (x));
5188 if (icode == CODE_FOR_nothing)
5189 return 0;
5191 if (!insn_operand_matches (icode, 0, x)
5192 || !insn_operand_matches (icode, 1, y)
5193 || !insn_operand_matches (icode, 2, z))
5194 return 0;
5196 return 1;
5199 /* Generate and return an insn body to subtract Y from X. */
5201 rtx_insn *
5202 gen_sub2_insn (rtx x, rtx y)
5204 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
5206 gcc_assert (insn_operand_matches (icode, 0, x));
5207 gcc_assert (insn_operand_matches (icode, 1, x));
5208 gcc_assert (insn_operand_matches (icode, 2, y));
5210 return GEN_FCN (icode) (x, x, y);
5213 /* Generate and return an insn body to subtract r1 and c,
5214 storing the result in r0. */
5216 rtx_insn *
5217 gen_sub3_insn (rtx r0, rtx r1, rtx c)
5219 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
5221 if (icode == CODE_FOR_nothing
5222 || !insn_operand_matches (icode, 0, r0)
5223 || !insn_operand_matches (icode, 1, r1)
5224 || !insn_operand_matches (icode, 2, c))
5225 return NULL;
5227 return GEN_FCN (icode) (r0, r1, c);
5231 have_sub2_insn (rtx x, rtx y)
5233 enum insn_code icode;
5235 gcc_assert (GET_MODE (x) != VOIDmode);
5237 icode = optab_handler (sub_optab, GET_MODE (x));
5239 if (icode == CODE_FOR_nothing)
5240 return 0;
5242 if (!insn_operand_matches (icode, 0, x)
5243 || !insn_operand_matches (icode, 1, x)
5244 || !insn_operand_matches (icode, 2, y))
5245 return 0;
5247 return 1;
5250 /* Generate the body of an insn to extend Y (with mode MFROM)
5251 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
5253 rtx_insn *
5254 gen_extend_insn (rtx x, rtx y, machine_mode mto,
5255 machine_mode mfrom, int unsignedp)
5257 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
5258 return GEN_FCN (icode) (x, y);
5261 /* Generate code to convert FROM to floating point
5262 and store in TO. FROM must be fixed point and not VOIDmode.
5263 UNSIGNEDP nonzero means regard FROM as unsigned.
5264 Normally this is done by correcting the final value
5265 if it is negative. */
5267 void
5268 expand_float (rtx to, rtx from, int unsignedp)
5270 enum insn_code icode;
5271 rtx target = to;
5272 scalar_mode from_mode, to_mode;
5273 machine_mode fmode, imode;
5274 bool can_do_signed = false;
5276 /* Crash now, because we won't be able to decide which mode to use. */
5277 gcc_assert (GET_MODE (from) != VOIDmode);
5279 /* Look for an insn to do the conversion. Do it in the specified
5280 modes if possible; otherwise convert either input, output or both to
5281 wider mode. If the integer mode is wider than the mode of FROM,
5282 we can do the conversion signed even if the input is unsigned. */
5284 FOR_EACH_MODE_FROM (fmode, GET_MODE (to))
5285 FOR_EACH_MODE_FROM (imode, GET_MODE (from))
5287 int doing_unsigned = unsignedp;
5289 if (fmode != GET_MODE (to)
5290 && (significand_size (fmode)
5291 < GET_MODE_UNIT_PRECISION (GET_MODE (from))))
5292 continue;
5294 icode = can_float_p (fmode, imode, unsignedp);
5295 if (icode == CODE_FOR_nothing && unsignedp)
5297 enum insn_code scode = can_float_p (fmode, imode, 0);
5298 if (scode != CODE_FOR_nothing)
5299 can_do_signed = true;
5300 if (imode != GET_MODE (from))
5301 icode = scode, doing_unsigned = 0;
5304 if (icode != CODE_FOR_nothing)
5306 if (imode != GET_MODE (from))
5307 from = convert_to_mode (imode, from, unsignedp);
5309 if (fmode != GET_MODE (to))
5310 target = gen_reg_rtx (fmode);
5312 emit_unop_insn (icode, target, from,
5313 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5315 if (target != to)
5316 convert_move (to, target, 0);
5317 return;
5321 /* Unsigned integer, and no way to convert directly. Convert as signed,
5322 then unconditionally adjust the result. */
5323 if (unsignedp
5324 && can_do_signed
5325 && is_a <scalar_mode> (GET_MODE (to), &to_mode)
5326 && is_a <scalar_mode> (GET_MODE (from), &from_mode))
5328 opt_scalar_mode fmode_iter;
5329 rtx_code_label *label = gen_label_rtx ();
5330 rtx temp;
5331 REAL_VALUE_TYPE offset;
5333 /* Look for a usable floating mode FMODE wider than the source and at
5334 least as wide as the target. Using FMODE will avoid rounding woes
5335 with unsigned values greater than the signed maximum value. */
5337 FOR_EACH_MODE_FROM (fmode_iter, to_mode)
5339 scalar_mode fmode = fmode_iter.require ();
5340 if (GET_MODE_PRECISION (from_mode) < GET_MODE_BITSIZE (fmode)
5341 && can_float_p (fmode, from_mode, 0) != CODE_FOR_nothing)
5342 break;
5345 if (!fmode_iter.exists (&fmode))
5347 /* There is no such mode. Pretend the target is wide enough. */
5348 fmode = to_mode;
5350 /* Avoid double-rounding when TO is narrower than FROM. */
5351 if ((significand_size (fmode) + 1)
5352 < GET_MODE_PRECISION (from_mode))
5354 rtx temp1;
5355 rtx_code_label *neglabel = gen_label_rtx ();
5357 /* Don't use TARGET if it isn't a register, is a hard register,
5358 or is the wrong mode. */
5359 if (!REG_P (target)
5360 || REGNO (target) < FIRST_PSEUDO_REGISTER
5361 || GET_MODE (target) != fmode)
5362 target = gen_reg_rtx (fmode);
5364 imode = from_mode;
5365 do_pending_stack_adjust ();
5367 /* Test whether the sign bit is set. */
5368 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5369 0, neglabel);
5371 /* The sign bit is not set. Convert as signed. */
5372 expand_float (target, from, 0);
5373 emit_jump_insn (targetm.gen_jump (label));
5374 emit_barrier ();
5376 /* The sign bit is set.
5377 Convert to a usable (positive signed) value by shifting right
5378 one bit, while remembering if a nonzero bit was shifted
5379 out; i.e., compute (from & 1) | (from >> 1). */
5381 emit_label (neglabel);
5382 temp = expand_binop (imode, and_optab, from, const1_rtx,
5383 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5384 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5385 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5386 OPTAB_LIB_WIDEN);
5387 expand_float (target, temp, 0);
5389 /* Multiply by 2 to undo the shift above. */
5390 temp = expand_binop (fmode, add_optab, target, target,
5391 target, 0, OPTAB_LIB_WIDEN);
5392 if (temp != target)
5393 emit_move_insn (target, temp);
5395 do_pending_stack_adjust ();
5396 emit_label (label);
5397 goto done;
5401 /* If we are about to do some arithmetic to correct for an
5402 unsigned operand, do it in a pseudo-register. */
5404 if (to_mode != fmode
5405 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5406 target = gen_reg_rtx (fmode);
5408 /* Convert as signed integer to floating. */
5409 expand_float (target, from, 0);
5411 /* If FROM is negative (and therefore TO is negative),
5412 correct its value by 2**bitwidth. */
5414 do_pending_stack_adjust ();
5415 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, from_mode,
5416 0, label);
5419 real_2expN (&offset, GET_MODE_PRECISION (from_mode), fmode);
5420 temp = expand_binop (fmode, add_optab, target,
5421 const_double_from_real_value (offset, fmode),
5422 target, 0, OPTAB_LIB_WIDEN);
5423 if (temp != target)
5424 emit_move_insn (target, temp);
5426 do_pending_stack_adjust ();
5427 emit_label (label);
5428 goto done;
5431 /* No hardware instruction available; call a library routine. */
5433 rtx libfunc;
5434 rtx_insn *insns;
5435 rtx value;
5436 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5438 if (is_narrower_int_mode (GET_MODE (from), SImode))
5439 from = convert_to_mode (SImode, from, unsignedp);
5441 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5442 gcc_assert (libfunc);
5444 start_sequence ();
5446 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5447 GET_MODE (to), from, GET_MODE (from));
5448 insns = get_insns ();
5449 end_sequence ();
5451 emit_libcall_block (insns, target, value,
5452 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5453 GET_MODE (to), from));
5456 done:
5458 /* Copy result to requested destination
5459 if we have been computing in a temp location. */
5461 if (target != to)
5463 if (GET_MODE (target) == GET_MODE (to))
5464 emit_move_insn (to, target);
5465 else
5466 convert_move (to, target, 0);
5470 /* Generate code to convert FROM to fixed point and store in TO. FROM
5471 must be floating point. */
5473 void
5474 expand_fix (rtx to, rtx from, int unsignedp)
5476 enum insn_code icode;
5477 rtx target = to;
5478 machine_mode fmode, imode;
5479 opt_scalar_mode fmode_iter;
5480 bool must_trunc = false;
5482 /* We first try to find a pair of modes, one real and one integer, at
5483 least as wide as FROM and TO, respectively, in which we can open-code
5484 this conversion. If the integer mode is wider than the mode of TO,
5485 we can do the conversion either signed or unsigned. */
5487 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
5488 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
5490 int doing_unsigned = unsignedp;
5492 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5493 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5494 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5496 if (icode != CODE_FOR_nothing)
5498 rtx_insn *last = get_last_insn ();
5499 rtx from1 = from;
5500 if (fmode != GET_MODE (from))
5501 from1 = convert_to_mode (fmode, from, 0);
5503 if (must_trunc)
5505 rtx temp = gen_reg_rtx (GET_MODE (from1));
5506 from1 = expand_unop (GET_MODE (from1), ftrunc_optab, from1,
5507 temp, 0);
5510 if (imode != GET_MODE (to))
5511 target = gen_reg_rtx (imode);
5513 if (maybe_emit_unop_insn (icode, target, from1,
5514 doing_unsigned ? UNSIGNED_FIX : FIX))
5516 if (target != to)
5517 convert_move (to, target, unsignedp);
5518 return;
5520 delete_insns_since (last);
5524 /* For an unsigned conversion, there is one more way to do it.
5525 If we have a signed conversion, we generate code that compares
5526 the real value to the largest representable positive number. If if
5527 is smaller, the conversion is done normally. Otherwise, subtract
5528 one plus the highest signed number, convert, and add it back.
5530 We only need to check all real modes, since we know we didn't find
5531 anything with a wider integer mode.
5533 This code used to extend FP value into mode wider than the destination.
5534 This is needed for decimal float modes which cannot accurately
5535 represent one plus the highest signed number of the same size, but
5536 not for binary modes. Consider, for instance conversion from SFmode
5537 into DImode.
5539 The hot path through the code is dealing with inputs smaller than 2^63
5540 and doing just the conversion, so there is no bits to lose.
5542 In the other path we know the value is positive in the range 2^63..2^64-1
5543 inclusive. (as for other input overflow happens and result is undefined)
5544 So we know that the most important bit set in mantissa corresponds to
5545 2^63. The subtraction of 2^63 should not generate any rounding as it
5546 simply clears out that bit. The rest is trivial. */
5548 scalar_int_mode to_mode;
5549 if (unsignedp
5550 && is_a <scalar_int_mode> (GET_MODE (to), &to_mode)
5551 && HWI_COMPUTABLE_MODE_P (to_mode))
5552 FOR_EACH_MODE_FROM (fmode_iter, as_a <scalar_mode> (GET_MODE (from)))
5554 scalar_mode fmode = fmode_iter.require ();
5555 if (CODE_FOR_nothing != can_fix_p (to_mode, fmode,
5556 0, &must_trunc)
5557 && (!DECIMAL_FLOAT_MODE_P (fmode)
5558 || (GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (to_mode))))
5560 int bitsize;
5561 REAL_VALUE_TYPE offset;
5562 rtx limit;
5563 rtx_code_label *lab1, *lab2;
5564 rtx_insn *insn;
5566 bitsize = GET_MODE_PRECISION (to_mode);
5567 real_2expN (&offset, bitsize - 1, fmode);
5568 limit = const_double_from_real_value (offset, fmode);
5569 lab1 = gen_label_rtx ();
5570 lab2 = gen_label_rtx ();
5572 if (fmode != GET_MODE (from))
5573 from = convert_to_mode (fmode, from, 0);
5575 /* See if we need to do the subtraction. */
5576 do_pending_stack_adjust ();
5577 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX,
5578 GET_MODE (from), 0, lab1);
5580 /* If not, do the signed "fix" and branch around fixup code. */
5581 expand_fix (to, from, 0);
5582 emit_jump_insn (targetm.gen_jump (lab2));
5583 emit_barrier ();
5585 /* Otherwise, subtract 2**(N-1), convert to signed number,
5586 then add 2**(N-1). Do the addition using XOR since this
5587 will often generate better code. */
5588 emit_label (lab1);
5589 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5590 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5591 expand_fix (to, target, 0);
5592 target = expand_binop (to_mode, xor_optab, to,
5593 gen_int_mode
5594 (HOST_WIDE_INT_1 << (bitsize - 1),
5595 to_mode),
5596 to, 1, OPTAB_LIB_WIDEN);
5598 if (target != to)
5599 emit_move_insn (to, target);
5601 emit_label (lab2);
5603 if (optab_handler (mov_optab, to_mode) != CODE_FOR_nothing)
5605 /* Make a place for a REG_NOTE and add it. */
5606 insn = emit_move_insn (to, to);
5607 set_dst_reg_note (insn, REG_EQUAL,
5608 gen_rtx_fmt_e (UNSIGNED_FIX, to_mode,
5609 copy_rtx (from)),
5610 to);
5613 return;
5617 /* We can't do it with an insn, so use a library call. But first ensure
5618 that the mode of TO is at least as wide as SImode, since those are the
5619 only library calls we know about. */
5621 if (is_narrower_int_mode (GET_MODE (to), SImode))
5623 target = gen_reg_rtx (SImode);
5625 expand_fix (target, from, unsignedp);
5627 else
5629 rtx_insn *insns;
5630 rtx value;
5631 rtx libfunc;
5633 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5634 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5635 gcc_assert (libfunc);
5637 start_sequence ();
5639 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5640 GET_MODE (to), from, GET_MODE (from));
5641 insns = get_insns ();
5642 end_sequence ();
5644 emit_libcall_block (insns, target, value,
5645 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5646 GET_MODE (to), from));
5649 if (target != to)
5651 if (GET_MODE (to) == GET_MODE (target))
5652 emit_move_insn (to, target);
5653 else
5654 convert_move (to, target, 0);
5659 /* Promote integer arguments for a libcall if necessary.
5660 emit_library_call_value cannot do the promotion because it does not
5661 know if it should do a signed or unsigned promotion. This is because
5662 there are no tree types defined for libcalls. */
5664 static rtx
5665 prepare_libcall_arg (rtx arg, int uintp)
5667 scalar_int_mode mode;
5668 machine_mode arg_mode;
5669 if (is_a <scalar_int_mode> (GET_MODE (arg), &mode))
5671 /* If we need to promote the integer function argument we need to do
5672 it here instead of inside emit_library_call_value because in
5673 emit_library_call_value we don't know if we should do a signed or
5674 unsigned promotion. */
5676 int unsigned_p = 0;
5677 arg_mode = promote_function_mode (NULL_TREE, mode,
5678 &unsigned_p, NULL_TREE, 0);
5679 if (arg_mode != mode)
5680 return convert_to_mode (arg_mode, arg, uintp);
5682 return arg;
5685 /* Generate code to convert FROM or TO a fixed-point.
5686 If UINTP is true, either TO or FROM is an unsigned integer.
5687 If SATP is true, we need to saturate the result. */
5689 void
5690 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5692 machine_mode to_mode = GET_MODE (to);
5693 machine_mode from_mode = GET_MODE (from);
5694 convert_optab tab;
5695 enum rtx_code this_code;
5696 enum insn_code code;
5697 rtx_insn *insns;
5698 rtx value;
5699 rtx libfunc;
5701 if (to_mode == from_mode)
5703 emit_move_insn (to, from);
5704 return;
5707 if (uintp)
5709 tab = satp ? satfractuns_optab : fractuns_optab;
5710 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5712 else
5714 tab = satp ? satfract_optab : fract_optab;
5715 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5717 code = convert_optab_handler (tab, to_mode, from_mode);
5718 if (code != CODE_FOR_nothing)
5720 emit_unop_insn (code, to, from, this_code);
5721 return;
5724 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5725 gcc_assert (libfunc);
5727 from = prepare_libcall_arg (from, uintp);
5728 from_mode = GET_MODE (from);
5730 start_sequence ();
5731 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5732 from, from_mode);
5733 insns = get_insns ();
5734 end_sequence ();
5736 emit_libcall_block (insns, to, value,
5737 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5740 /* Generate code to convert FROM to fixed point and store in TO. FROM
5741 must be floating point, TO must be signed. Use the conversion optab
5742 TAB to do the conversion. */
5744 bool
5745 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5747 enum insn_code icode;
5748 rtx target = to;
5749 machine_mode fmode, imode;
5751 /* We first try to find a pair of modes, one real and one integer, at
5752 least as wide as FROM and TO, respectively, in which we can open-code
5753 this conversion. If the integer mode is wider than the mode of TO,
5754 we can do the conversion either signed or unsigned. */
5756 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
5757 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
5759 icode = convert_optab_handler (tab, imode, fmode);
5760 if (icode != CODE_FOR_nothing)
5762 rtx_insn *last = get_last_insn ();
5763 if (fmode != GET_MODE (from))
5764 from = convert_to_mode (fmode, from, 0);
5766 if (imode != GET_MODE (to))
5767 target = gen_reg_rtx (imode);
5769 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5771 delete_insns_since (last);
5772 continue;
5774 if (target != to)
5775 convert_move (to, target, 0);
5776 return true;
5780 return false;
5783 /* Report whether we have an instruction to perform the operation
5784 specified by CODE on operands of mode MODE. */
5786 have_insn_for (enum rtx_code code, machine_mode mode)
5788 return (code_to_optab (code)
5789 && (optab_handler (code_to_optab (code), mode)
5790 != CODE_FOR_nothing));
5793 /* Print information about the current contents of the optabs on
5794 STDERR. */
5796 DEBUG_FUNCTION void
5797 debug_optab_libfuncs (void)
5799 int i, j, k;
5801 /* Dump the arithmetic optabs. */
5802 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
5803 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5805 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
5806 if (l)
5808 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5809 fprintf (stderr, "%s\t%s:\t%s\n",
5810 GET_RTX_NAME (optab_to_code ((optab) i)),
5811 GET_MODE_NAME (j),
5812 XSTR (l, 0));
5816 /* Dump the conversion optabs. */
5817 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
5818 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5819 for (k = 0; k < NUM_MACHINE_MODES; ++k)
5821 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
5822 (machine_mode) k);
5823 if (l)
5825 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5826 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
5827 GET_RTX_NAME (optab_to_code ((optab) i)),
5828 GET_MODE_NAME (j),
5829 GET_MODE_NAME (k),
5830 XSTR (l, 0));
5835 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5836 CODE. Return 0 on failure. */
5838 rtx_insn *
5839 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
5841 machine_mode mode = GET_MODE (op1);
5842 enum insn_code icode;
5843 rtx_insn *insn;
5844 rtx trap_rtx;
5846 if (mode == VOIDmode)
5847 return 0;
5849 icode = optab_handler (ctrap_optab, mode);
5850 if (icode == CODE_FOR_nothing)
5851 return 0;
5853 /* Some targets only accept a zero trap code. */
5854 if (!insn_operand_matches (icode, 3, tcode))
5855 return 0;
5857 do_pending_stack_adjust ();
5858 start_sequence ();
5859 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
5860 &trap_rtx, &mode);
5861 if (!trap_rtx)
5862 insn = NULL;
5863 else
5864 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
5865 tcode);
5867 /* If that failed, then give up. */
5868 if (insn == 0)
5870 end_sequence ();
5871 return 0;
5874 emit_insn (insn);
5875 insn = get_insns ();
5876 end_sequence ();
5877 return insn;
5880 /* Return rtx code for TCODE or UNKNOWN. Use UNSIGNEDP to select signed
5881 or unsigned operation code. */
5883 enum rtx_code
5884 get_rtx_code_1 (enum tree_code tcode, bool unsignedp)
5886 enum rtx_code code;
5887 switch (tcode)
5889 case EQ_EXPR:
5890 code = EQ;
5891 break;
5892 case NE_EXPR:
5893 code = NE;
5894 break;
5895 case LT_EXPR:
5896 code = unsignedp ? LTU : LT;
5897 break;
5898 case LE_EXPR:
5899 code = unsignedp ? LEU : LE;
5900 break;
5901 case GT_EXPR:
5902 code = unsignedp ? GTU : GT;
5903 break;
5904 case GE_EXPR:
5905 code = unsignedp ? GEU : GE;
5906 break;
5908 case UNORDERED_EXPR:
5909 code = UNORDERED;
5910 break;
5911 case ORDERED_EXPR:
5912 code = ORDERED;
5913 break;
5914 case UNLT_EXPR:
5915 code = UNLT;
5916 break;
5917 case UNLE_EXPR:
5918 code = UNLE;
5919 break;
5920 case UNGT_EXPR:
5921 code = UNGT;
5922 break;
5923 case UNGE_EXPR:
5924 code = UNGE;
5925 break;
5926 case UNEQ_EXPR:
5927 code = UNEQ;
5928 break;
5929 case LTGT_EXPR:
5930 code = LTGT;
5931 break;
5933 case BIT_AND_EXPR:
5934 code = AND;
5935 break;
5937 case BIT_IOR_EXPR:
5938 code = IOR;
5939 break;
5941 default:
5942 code = UNKNOWN;
5943 break;
5945 return code;
5948 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
5949 or unsigned operation code. */
5951 enum rtx_code
5952 get_rtx_code (enum tree_code tcode, bool unsignedp)
5954 enum rtx_code code = get_rtx_code_1 (tcode, unsignedp);
5955 gcc_assert (code != UNKNOWN);
5956 return code;
5959 /* Return a comparison rtx of mode CMP_MODE for COND. Use UNSIGNEDP to
5960 select signed or unsigned operators. OPNO holds the index of the
5961 first comparison operand for insn ICODE. Do not generate the
5962 compare instruction itself. */
5965 vector_compare_rtx (machine_mode cmp_mode, enum tree_code tcode,
5966 tree t_op0, tree t_op1, bool unsignedp,
5967 enum insn_code icode, unsigned int opno)
5969 class expand_operand ops[2];
5970 rtx rtx_op0, rtx_op1;
5971 machine_mode m0, m1;
5972 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
5974 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
5976 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
5977 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
5978 cases, use the original mode. */
5979 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
5980 EXPAND_STACK_PARM);
5981 m0 = GET_MODE (rtx_op0);
5982 if (m0 == VOIDmode)
5983 m0 = TYPE_MODE (TREE_TYPE (t_op0));
5985 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
5986 EXPAND_STACK_PARM);
5987 m1 = GET_MODE (rtx_op1);
5988 if (m1 == VOIDmode)
5989 m1 = TYPE_MODE (TREE_TYPE (t_op1));
5991 create_input_operand (&ops[0], rtx_op0, m0);
5992 create_input_operand (&ops[1], rtx_op1, m1);
5993 if (!maybe_legitimize_operands (icode, opno, 2, ops))
5994 gcc_unreachable ();
5995 return gen_rtx_fmt_ee (rcode, cmp_mode, ops[0].value, ops[1].value);
5998 /* Check if vec_perm mask SEL is a constant equivalent to a shift of
5999 the first vec_perm operand, assuming the second operand (for left shift
6000 first operand) is a constant vector of zeros. Return the shift distance
6001 in bits if so, or NULL_RTX if the vec_perm is not a shift. MODE is the
6002 mode of the value being shifted. SHIFT_OPTAB is vec_shr_optab for right
6003 shift or vec_shl_optab for left shift. */
6004 static rtx
6005 shift_amt_for_vec_perm_mask (machine_mode mode, const vec_perm_indices &sel,
6006 optab shift_optab)
6008 unsigned int bitsize = GET_MODE_UNIT_BITSIZE (mode);
6009 poly_int64 first = sel[0];
6010 if (maybe_ge (sel[0], GET_MODE_NUNITS (mode)))
6011 return NULL_RTX;
6013 if (shift_optab == vec_shl_optab)
6015 unsigned int nelt;
6016 if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
6017 return NULL_RTX;
6018 unsigned firstidx = 0;
6019 for (unsigned int i = 0; i < nelt; i++)
6021 if (known_eq (sel[i], nelt))
6023 if (i == 0 || firstidx)
6024 return NULL_RTX;
6025 firstidx = i;
6027 else if (firstidx
6028 ? maybe_ne (sel[i], nelt + i - firstidx)
6029 : maybe_ge (sel[i], nelt))
6030 return NULL_RTX;
6033 if (firstidx == 0)
6034 return NULL_RTX;
6035 first = firstidx;
6037 else if (!sel.series_p (0, 1, first, 1))
6039 unsigned int nelt;
6040 if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
6041 return NULL_RTX;
6042 for (unsigned int i = 1; i < nelt; i++)
6044 poly_int64 expected = i + first;
6045 /* Indices into the second vector are all equivalent. */
6046 if (maybe_lt (sel[i], nelt)
6047 ? maybe_ne (sel[i], expected)
6048 : maybe_lt (expected, nelt))
6049 return NULL_RTX;
6053 return gen_int_shift_amount (mode, first * bitsize);
6056 /* A subroutine of expand_vec_perm_var for expanding one vec_perm insn. */
6058 static rtx
6059 expand_vec_perm_1 (enum insn_code icode, rtx target,
6060 rtx v0, rtx v1, rtx sel)
6062 machine_mode tmode = GET_MODE (target);
6063 machine_mode smode = GET_MODE (sel);
6064 class expand_operand ops[4];
6066 gcc_assert (GET_MODE_CLASS (smode) == MODE_VECTOR_INT
6067 || related_int_vector_mode (tmode).require () == smode);
6068 create_output_operand (&ops[0], target, tmode);
6069 create_input_operand (&ops[3], sel, smode);
6071 /* Make an effort to preserve v0 == v1. The target expander is able to
6072 rely on this to determine if we're permuting a single input operand. */
6073 if (rtx_equal_p (v0, v1))
6075 if (!insn_operand_matches (icode, 1, v0))
6076 v0 = force_reg (tmode, v0);
6077 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6078 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6080 create_fixed_operand (&ops[1], v0);
6081 create_fixed_operand (&ops[2], v0);
6083 else
6085 create_input_operand (&ops[1], v0, tmode);
6086 create_input_operand (&ops[2], v1, tmode);
6089 if (maybe_expand_insn (icode, 4, ops))
6090 return ops[0].value;
6091 return NULL_RTX;
6094 /* Implement a permutation of vectors v0 and v1 using the permutation
6095 vector in SEL and return the result. Use TARGET to hold the result
6096 if nonnull and convenient.
6098 MODE is the mode of the vectors being permuted (V0 and V1). SEL_MODE
6099 is the TYPE_MODE associated with SEL, or BLKmode if SEL isn't known
6100 to have a particular mode. */
6103 expand_vec_perm_const (machine_mode mode, rtx v0, rtx v1,
6104 const vec_perm_builder &sel, machine_mode sel_mode,
6105 rtx target)
6107 if (!target || !register_operand (target, mode))
6108 target = gen_reg_rtx (mode);
6110 /* Set QIMODE to a different vector mode with byte elements.
6111 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6112 machine_mode qimode;
6113 if (!qimode_for_vec_perm (mode).exists (&qimode))
6114 qimode = VOIDmode;
6116 rtx_insn *last = get_last_insn ();
6118 bool single_arg_p = rtx_equal_p (v0, v1);
6119 /* Always specify two input vectors here and leave the target to handle
6120 cases in which the inputs are equal. Not all backends can cope with
6121 the single-input representation when testing for a double-input
6122 target instruction. */
6123 vec_perm_indices indices (sel, 2, GET_MODE_NUNITS (mode));
6125 /* See if this can be handled with a vec_shr or vec_shl. We only do this
6126 if the second (for vec_shr) or first (for vec_shl) vector is all
6127 zeroes. */
6128 insn_code shift_code = CODE_FOR_nothing;
6129 insn_code shift_code_qi = CODE_FOR_nothing;
6130 optab shift_optab = unknown_optab;
6131 rtx v2 = v0;
6132 if (v1 == CONST0_RTX (GET_MODE (v1)))
6133 shift_optab = vec_shr_optab;
6134 else if (v0 == CONST0_RTX (GET_MODE (v0)))
6136 shift_optab = vec_shl_optab;
6137 v2 = v1;
6139 if (shift_optab != unknown_optab)
6141 shift_code = optab_handler (shift_optab, mode);
6142 shift_code_qi = ((qimode != VOIDmode && qimode != mode)
6143 ? optab_handler (shift_optab, qimode)
6144 : CODE_FOR_nothing);
6146 if (shift_code != CODE_FOR_nothing || shift_code_qi != CODE_FOR_nothing)
6148 rtx shift_amt = shift_amt_for_vec_perm_mask (mode, indices, shift_optab);
6149 if (shift_amt)
6151 class expand_operand ops[3];
6152 if (shift_amt == const0_rtx)
6153 return v2;
6154 if (shift_code != CODE_FOR_nothing)
6156 create_output_operand (&ops[0], target, mode);
6157 create_input_operand (&ops[1], v2, mode);
6158 create_convert_operand_from_type (&ops[2], shift_amt, sizetype);
6159 if (maybe_expand_insn (shift_code, 3, ops))
6160 return ops[0].value;
6162 if (shift_code_qi != CODE_FOR_nothing)
6164 rtx tmp = gen_reg_rtx (qimode);
6165 create_output_operand (&ops[0], tmp, qimode);
6166 create_input_operand (&ops[1], gen_lowpart (qimode, v2), qimode);
6167 create_convert_operand_from_type (&ops[2], shift_amt, sizetype);
6168 if (maybe_expand_insn (shift_code_qi, 3, ops))
6169 return gen_lowpart (mode, ops[0].value);
6174 if (targetm.vectorize.vec_perm_const != NULL)
6176 if (single_arg_p)
6177 v1 = v0;
6179 if (targetm.vectorize.vec_perm_const (mode, target, v0, v1, indices))
6180 return target;
6183 /* Fall back to a constant byte-based permutation. */
6184 vec_perm_indices qimode_indices;
6185 rtx target_qi = NULL_RTX, v0_qi = NULL_RTX, v1_qi = NULL_RTX;
6186 if (qimode != VOIDmode)
6188 qimode_indices.new_expanded_vector (indices, GET_MODE_UNIT_SIZE (mode));
6189 target_qi = gen_reg_rtx (qimode);
6190 v0_qi = gen_lowpart (qimode, v0);
6191 v1_qi = gen_lowpart (qimode, v1);
6192 if (targetm.vectorize.vec_perm_const != NULL
6193 && targetm.vectorize.vec_perm_const (qimode, target_qi, v0_qi,
6194 v1_qi, qimode_indices))
6195 return gen_lowpart (mode, target_qi);
6198 v0 = force_reg (mode, v0);
6199 if (single_arg_p)
6200 v1 = v0;
6201 v1 = force_reg (mode, v1);
6203 /* Otherwise expand as a fully variable permuation. */
6205 /* The optabs are only defined for selectors with the same width
6206 as the values being permuted. */
6207 machine_mode required_sel_mode;
6208 if (!related_int_vector_mode (mode).exists (&required_sel_mode))
6210 delete_insns_since (last);
6211 return NULL_RTX;
6214 /* We know that it is semantically valid to treat SEL as having SEL_MODE.
6215 If that isn't the mode we want then we need to prove that using
6216 REQUIRED_SEL_MODE is OK. */
6217 if (sel_mode != required_sel_mode)
6219 if (!selector_fits_mode_p (required_sel_mode, indices))
6221 delete_insns_since (last);
6222 return NULL_RTX;
6224 sel_mode = required_sel_mode;
6227 insn_code icode = direct_optab_handler (vec_perm_optab, mode);
6228 if (icode != CODE_FOR_nothing)
6230 rtx sel_rtx = vec_perm_indices_to_rtx (sel_mode, indices);
6231 rtx tmp = expand_vec_perm_1 (icode, target, v0, v1, sel_rtx);
6232 if (tmp)
6233 return tmp;
6236 if (qimode != VOIDmode
6237 && selector_fits_mode_p (qimode, qimode_indices))
6239 icode = direct_optab_handler (vec_perm_optab, qimode);
6240 if (icode != CODE_FOR_nothing)
6242 rtx sel_qi = vec_perm_indices_to_rtx (qimode, qimode_indices);
6243 rtx tmp = expand_vec_perm_1 (icode, target_qi, v0_qi, v1_qi, sel_qi);
6244 if (tmp)
6245 return gen_lowpart (mode, tmp);
6249 delete_insns_since (last);
6250 return NULL_RTX;
6253 /* Implement a permutation of vectors v0 and v1 using the permutation
6254 vector in SEL and return the result. Use TARGET to hold the result
6255 if nonnull and convenient.
6257 MODE is the mode of the vectors being permuted (V0 and V1).
6258 SEL must have the integer equivalent of MODE and is known to be
6259 unsuitable for permutes with a constant permutation vector. */
6262 expand_vec_perm_var (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6264 enum insn_code icode;
6265 unsigned int i, u;
6266 rtx tmp, sel_qi;
6268 u = GET_MODE_UNIT_SIZE (mode);
6270 if (!target || GET_MODE (target) != mode)
6271 target = gen_reg_rtx (mode);
6273 icode = direct_optab_handler (vec_perm_optab, mode);
6274 if (icode != CODE_FOR_nothing)
6276 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6277 if (tmp)
6278 return tmp;
6281 /* As a special case to aid several targets, lower the element-based
6282 permutation to a byte-based permutation and try again. */
6283 machine_mode qimode;
6284 if (!qimode_for_vec_perm (mode).exists (&qimode)
6285 || maybe_gt (GET_MODE_NUNITS (qimode), GET_MODE_MASK (QImode) + 1))
6286 return NULL_RTX;
6287 icode = direct_optab_handler (vec_perm_optab, qimode);
6288 if (icode == CODE_FOR_nothing)
6289 return NULL_RTX;
6291 /* Multiply each element by its byte size. */
6292 machine_mode selmode = GET_MODE (sel);
6293 if (u == 2)
6294 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6295 NULL, 0, OPTAB_DIRECT);
6296 else
6297 sel = expand_simple_binop (selmode, ASHIFT, sel,
6298 gen_int_shift_amount (selmode, exact_log2 (u)),
6299 NULL, 0, OPTAB_DIRECT);
6300 gcc_assert (sel != NULL);
6302 /* Broadcast the low byte each element into each of its bytes.
6303 The encoding has U interleaved stepped patterns, one for each
6304 byte of an element. */
6305 vec_perm_builder const_sel (GET_MODE_SIZE (mode), u, 3);
6306 unsigned int low_byte_in_u = BYTES_BIG_ENDIAN ? u - 1 : 0;
6307 for (i = 0; i < 3; ++i)
6308 for (unsigned int j = 0; j < u; ++j)
6309 const_sel.quick_push (i * u + low_byte_in_u);
6310 sel = gen_lowpart (qimode, sel);
6311 sel = expand_vec_perm_const (qimode, sel, sel, const_sel, qimode, NULL);
6312 gcc_assert (sel != NULL);
6314 /* Add the byte offset to each byte element. */
6315 /* Note that the definition of the indicies here is memory ordering,
6316 so there should be no difference between big and little endian. */
6317 rtx_vector_builder byte_indices (qimode, u, 1);
6318 for (i = 0; i < u; ++i)
6319 byte_indices.quick_push (GEN_INT (i));
6320 tmp = byte_indices.build ();
6321 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6322 sel, 0, OPTAB_DIRECT);
6323 gcc_assert (sel_qi != NULL);
6325 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6326 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6327 gen_lowpart (qimode, v1), sel_qi);
6328 if (tmp)
6329 tmp = gen_lowpart (mode, tmp);
6330 return tmp;
6333 /* Generate VEC_SERIES_EXPR <OP0, OP1>, returning a value of mode VMODE.
6334 Use TARGET for the result if nonnull and convenient. */
6337 expand_vec_series_expr (machine_mode vmode, rtx op0, rtx op1, rtx target)
6339 class expand_operand ops[3];
6340 enum insn_code icode;
6341 machine_mode emode = GET_MODE_INNER (vmode);
6343 icode = direct_optab_handler (vec_series_optab, vmode);
6344 gcc_assert (icode != CODE_FOR_nothing);
6346 create_output_operand (&ops[0], target, vmode);
6347 create_input_operand (&ops[1], op0, emode);
6348 create_input_operand (&ops[2], op1, emode);
6350 expand_insn (icode, 3, ops);
6351 return ops[0].value;
6354 /* Generate insns for a vector comparison into a mask. */
6357 expand_vec_cmp_expr (tree type, tree exp, rtx target)
6359 class expand_operand ops[4];
6360 enum insn_code icode;
6361 rtx comparison;
6362 machine_mode mask_mode = TYPE_MODE (type);
6363 machine_mode vmode;
6364 bool unsignedp;
6365 tree op0a, op0b;
6366 enum tree_code tcode;
6368 op0a = TREE_OPERAND (exp, 0);
6369 op0b = TREE_OPERAND (exp, 1);
6370 tcode = TREE_CODE (exp);
6372 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6373 vmode = TYPE_MODE (TREE_TYPE (op0a));
6375 icode = get_vec_cmp_icode (vmode, mask_mode, unsignedp);
6376 if (icode == CODE_FOR_nothing)
6378 if (tcode == EQ_EXPR || tcode == NE_EXPR)
6379 icode = get_vec_cmp_eq_icode (vmode, mask_mode);
6380 if (icode == CODE_FOR_nothing)
6381 return 0;
6384 comparison = vector_compare_rtx (mask_mode, tcode, op0a, op0b,
6385 unsignedp, icode, 2);
6386 create_output_operand (&ops[0], target, mask_mode);
6387 create_fixed_operand (&ops[1], comparison);
6388 create_fixed_operand (&ops[2], XEXP (comparison, 0));
6389 create_fixed_operand (&ops[3], XEXP (comparison, 1));
6390 expand_insn (icode, 4, ops);
6391 return ops[0].value;
6394 /* Expand a highpart multiply. */
6397 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
6398 rtx target, bool uns_p)
6400 class expand_operand eops[3];
6401 enum insn_code icode;
6402 int method, i;
6403 machine_mode wmode;
6404 rtx m1, m2;
6405 optab tab1, tab2;
6407 method = can_mult_highpart_p (mode, uns_p);
6408 switch (method)
6410 case 0:
6411 return NULL_RTX;
6412 case 1:
6413 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6414 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6415 OPTAB_LIB_WIDEN);
6416 case 2:
6417 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6418 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6419 break;
6420 case 3:
6421 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6422 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6423 if (BYTES_BIG_ENDIAN)
6424 std::swap (tab1, tab2);
6425 break;
6426 default:
6427 gcc_unreachable ();
6430 icode = optab_handler (tab1, mode);
6431 wmode = insn_data[icode].operand[0].mode;
6432 gcc_checking_assert (known_eq (2 * GET_MODE_NUNITS (wmode),
6433 GET_MODE_NUNITS (mode)));
6434 gcc_checking_assert (known_eq (GET_MODE_SIZE (wmode), GET_MODE_SIZE (mode)));
6436 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6437 create_input_operand (&eops[1], op0, mode);
6438 create_input_operand (&eops[2], op1, mode);
6439 expand_insn (icode, 3, eops);
6440 m1 = gen_lowpart (mode, eops[0].value);
6442 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6443 create_input_operand (&eops[1], op0, mode);
6444 create_input_operand (&eops[2], op1, mode);
6445 expand_insn (optab_handler (tab2, mode), 3, eops);
6446 m2 = gen_lowpart (mode, eops[0].value);
6448 vec_perm_builder sel;
6449 if (method == 2)
6451 /* The encoding has 2 interleaved stepped patterns. */
6452 sel.new_vector (GET_MODE_NUNITS (mode), 2, 3);
6453 for (i = 0; i < 6; ++i)
6454 sel.quick_push (!BYTES_BIG_ENDIAN + (i & ~1)
6455 + ((i & 1) ? GET_MODE_NUNITS (mode) : 0));
6457 else
6459 /* The encoding has a single interleaved stepped pattern. */
6460 sel.new_vector (GET_MODE_NUNITS (mode), 1, 3);
6461 for (i = 0; i < 3; ++i)
6462 sel.quick_push (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
6465 return expand_vec_perm_const (mode, m1, m2, sel, BLKmode, target);
6468 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
6469 pattern. */
6471 static void
6472 find_cc_set (rtx x, const_rtx pat, void *data)
6474 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
6475 && GET_CODE (pat) == SET)
6477 rtx *p_cc_reg = (rtx *) data;
6478 gcc_assert (!*p_cc_reg);
6479 *p_cc_reg = x;
6483 /* This is a helper function for the other atomic operations. This function
6484 emits a loop that contains SEQ that iterates until a compare-and-swap
6485 operation at the end succeeds. MEM is the memory to be modified. SEQ is
6486 a set of instructions that takes a value from OLD_REG as an input and
6487 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
6488 set to the current contents of MEM. After SEQ, a compare-and-swap will
6489 attempt to update MEM with NEW_REG. The function returns true when the
6490 loop was generated successfully. */
6492 static bool
6493 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
6495 machine_mode mode = GET_MODE (mem);
6496 rtx_code_label *label;
6497 rtx cmp_reg, success, oldval;
6499 /* The loop we want to generate looks like
6501 cmp_reg = mem;
6502 label:
6503 old_reg = cmp_reg;
6504 seq;
6505 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
6506 if (success)
6507 goto label;
6509 Note that we only do the plain load from memory once. Subsequent
6510 iterations use the value loaded by the compare-and-swap pattern. */
6512 label = gen_label_rtx ();
6513 cmp_reg = gen_reg_rtx (mode);
6515 emit_move_insn (cmp_reg, mem);
6516 emit_label (label);
6517 emit_move_insn (old_reg, cmp_reg);
6518 if (seq)
6519 emit_insn (seq);
6521 success = NULL_RTX;
6522 oldval = cmp_reg;
6523 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
6524 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
6525 MEMMODEL_RELAXED))
6526 return false;
6528 if (oldval != cmp_reg)
6529 emit_move_insn (cmp_reg, oldval);
6531 /* Mark this jump predicted not taken. */
6532 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
6533 GET_MODE (success), 1, label,
6534 profile_probability::guessed_never ());
6535 return true;
6539 /* This function tries to emit an atomic_exchange intruction. VAL is written
6540 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
6541 using TARGET if possible. */
6543 static rtx
6544 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6546 machine_mode mode = GET_MODE (mem);
6547 enum insn_code icode;
6549 /* If the target supports the exchange directly, great. */
6550 icode = direct_optab_handler (atomic_exchange_optab, mode);
6551 if (icode != CODE_FOR_nothing)
6553 class expand_operand ops[4];
6555 create_output_operand (&ops[0], target, mode);
6556 create_fixed_operand (&ops[1], mem);
6557 create_input_operand (&ops[2], val, mode);
6558 create_integer_operand (&ops[3], model);
6559 if (maybe_expand_insn (icode, 4, ops))
6560 return ops[0].value;
6563 return NULL_RTX;
6566 /* This function tries to implement an atomic exchange operation using
6567 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
6568 The previous contents of *MEM are returned, using TARGET if possible.
6569 Since this instructionn is an acquire barrier only, stronger memory
6570 models may require additional barriers to be emitted. */
6572 static rtx
6573 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
6574 enum memmodel model)
6576 machine_mode mode = GET_MODE (mem);
6577 enum insn_code icode;
6578 rtx_insn *last_insn = get_last_insn ();
6580 icode = optab_handler (sync_lock_test_and_set_optab, mode);
6582 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
6583 exists, and the memory model is stronger than acquire, add a release
6584 barrier before the instruction. */
6586 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
6587 expand_mem_thread_fence (model);
6589 if (icode != CODE_FOR_nothing)
6591 class expand_operand ops[3];
6592 create_output_operand (&ops[0], target, mode);
6593 create_fixed_operand (&ops[1], mem);
6594 create_input_operand (&ops[2], val, mode);
6595 if (maybe_expand_insn (icode, 3, ops))
6596 return ops[0].value;
6599 /* If an external test-and-set libcall is provided, use that instead of
6600 any external compare-and-swap that we might get from the compare-and-
6601 swap-loop expansion later. */
6602 if (!can_compare_and_swap_p (mode, false))
6604 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
6605 if (libfunc != NULL)
6607 rtx addr;
6609 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6610 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6611 mode, addr, ptr_mode,
6612 val, mode);
6616 /* If the test_and_set can't be emitted, eliminate any barrier that might
6617 have been emitted. */
6618 delete_insns_since (last_insn);
6619 return NULL_RTX;
6622 /* This function tries to implement an atomic exchange operation using a
6623 compare_and_swap loop. VAL is written to *MEM. The previous contents of
6624 *MEM are returned, using TARGET if possible. No memory model is required
6625 since a compare_and_swap loop is seq-cst. */
6627 static rtx
6628 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
6630 machine_mode mode = GET_MODE (mem);
6632 if (can_compare_and_swap_p (mode, true))
6634 if (!target || !register_operand (target, mode))
6635 target = gen_reg_rtx (mode);
6636 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
6637 return target;
6640 return NULL_RTX;
6643 /* This function tries to implement an atomic test-and-set operation
6644 using the atomic_test_and_set instruction pattern. A boolean value
6645 is returned from the operation, using TARGET if possible. */
6647 static rtx
6648 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6650 machine_mode pat_bool_mode;
6651 class expand_operand ops[3];
6653 if (!targetm.have_atomic_test_and_set ())
6654 return NULL_RTX;
6656 /* While we always get QImode from __atomic_test_and_set, we get
6657 other memory modes from __sync_lock_test_and_set. Note that we
6658 use no endian adjustment here. This matches the 4.6 behavior
6659 in the Sparc backend. */
6660 enum insn_code icode = targetm.code_for_atomic_test_and_set;
6661 gcc_checking_assert (insn_data[icode].operand[1].mode == QImode);
6662 if (GET_MODE (mem) != QImode)
6663 mem = adjust_address_nv (mem, QImode, 0);
6665 pat_bool_mode = insn_data[icode].operand[0].mode;
6666 create_output_operand (&ops[0], target, pat_bool_mode);
6667 create_fixed_operand (&ops[1], mem);
6668 create_integer_operand (&ops[2], model);
6670 if (maybe_expand_insn (icode, 3, ops))
6671 return ops[0].value;
6672 return NULL_RTX;
6675 /* This function expands the legacy _sync_lock test_and_set operation which is
6676 generally an atomic exchange. Some limited targets only allow the
6677 constant 1 to be stored. This is an ACQUIRE operation.
6679 TARGET is an optional place to stick the return value.
6680 MEM is where VAL is stored. */
6683 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
6685 rtx ret;
6687 /* Try an atomic_exchange first. */
6688 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
6689 if (ret)
6690 return ret;
6692 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
6693 MEMMODEL_SYNC_ACQUIRE);
6694 if (ret)
6695 return ret;
6697 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6698 if (ret)
6699 return ret;
6701 /* If there are no other options, try atomic_test_and_set if the value
6702 being stored is 1. */
6703 if (val == const1_rtx)
6704 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
6706 return ret;
6709 /* This function expands the atomic test_and_set operation:
6710 atomically store a boolean TRUE into MEM and return the previous value.
6712 MEMMODEL is the memory model variant to use.
6713 TARGET is an optional place to stick the return value. */
6716 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6718 machine_mode mode = GET_MODE (mem);
6719 rtx ret, trueval, subtarget;
6721 ret = maybe_emit_atomic_test_and_set (target, mem, model);
6722 if (ret)
6723 return ret;
6725 /* Be binary compatible with non-default settings of trueval, and different
6726 cpu revisions. E.g. one revision may have atomic-test-and-set, but
6727 another only has atomic-exchange. */
6728 if (targetm.atomic_test_and_set_trueval == 1)
6730 trueval = const1_rtx;
6731 subtarget = target ? target : gen_reg_rtx (mode);
6733 else
6735 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
6736 subtarget = gen_reg_rtx (mode);
6739 /* Try the atomic-exchange optab... */
6740 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
6742 /* ... then an atomic-compare-and-swap loop ... */
6743 if (!ret)
6744 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
6746 /* ... before trying the vaguely defined legacy lock_test_and_set. */
6747 if (!ret)
6748 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
6750 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
6751 things with the value 1. Thus we try again without trueval. */
6752 if (!ret && targetm.atomic_test_and_set_trueval != 1)
6753 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
6755 /* Failing all else, assume a single threaded environment and simply
6756 perform the operation. */
6757 if (!ret)
6759 /* If the result is ignored skip the move to target. */
6760 if (subtarget != const0_rtx)
6761 emit_move_insn (subtarget, mem);
6763 emit_move_insn (mem, trueval);
6764 ret = subtarget;
6767 /* Recall that have to return a boolean value; rectify if trueval
6768 is not exactly one. */
6769 if (targetm.atomic_test_and_set_trueval != 1)
6770 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
6772 return ret;
6775 /* This function expands the atomic exchange operation:
6776 atomically store VAL in MEM and return the previous value in MEM.
6778 MEMMODEL is the memory model variant to use.
6779 TARGET is an optional place to stick the return value. */
6782 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6784 machine_mode mode = GET_MODE (mem);
6785 rtx ret;
6787 /* If loads are not atomic for the required size and we are not called to
6788 provide a __sync builtin, do not do anything so that we stay consistent
6789 with atomic loads of the same size. */
6790 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6791 return NULL_RTX;
6793 ret = maybe_emit_atomic_exchange (target, mem, val, model);
6795 /* Next try a compare-and-swap loop for the exchange. */
6796 if (!ret)
6797 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6799 return ret;
6802 /* This function expands the atomic compare exchange operation:
6804 *PTARGET_BOOL is an optional place to store the boolean success/failure.
6805 *PTARGET_OVAL is an optional place to store the old value from memory.
6806 Both target parameters may be NULL or const0_rtx to indicate that we do
6807 not care about that return value. Both target parameters are updated on
6808 success to the actual location of the corresponding result.
6810 MEMMODEL is the memory model variant to use.
6812 The return value of the function is true for success. */
6814 bool
6815 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
6816 rtx mem, rtx expected, rtx desired,
6817 bool is_weak, enum memmodel succ_model,
6818 enum memmodel fail_model)
6820 machine_mode mode = GET_MODE (mem);
6821 class expand_operand ops[8];
6822 enum insn_code icode;
6823 rtx target_oval, target_bool = NULL_RTX;
6824 rtx libfunc;
6826 /* If loads are not atomic for the required size and we are not called to
6827 provide a __sync builtin, do not do anything so that we stay consistent
6828 with atomic loads of the same size. */
6829 if (!can_atomic_load_p (mode) && !is_mm_sync (succ_model))
6830 return false;
6832 /* Load expected into a register for the compare and swap. */
6833 if (MEM_P (expected))
6834 expected = copy_to_reg (expected);
6836 /* Make sure we always have some place to put the return oldval.
6837 Further, make sure that place is distinct from the input expected,
6838 just in case we need that path down below. */
6839 if (ptarget_oval && *ptarget_oval == const0_rtx)
6840 ptarget_oval = NULL;
6842 if (ptarget_oval == NULL
6843 || (target_oval = *ptarget_oval) == NULL
6844 || reg_overlap_mentioned_p (expected, target_oval))
6845 target_oval = gen_reg_rtx (mode);
6847 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
6848 if (icode != CODE_FOR_nothing)
6850 machine_mode bool_mode = insn_data[icode].operand[0].mode;
6852 if (ptarget_bool && *ptarget_bool == const0_rtx)
6853 ptarget_bool = NULL;
6855 /* Make sure we always have a place for the bool operand. */
6856 if (ptarget_bool == NULL
6857 || (target_bool = *ptarget_bool) == NULL
6858 || GET_MODE (target_bool) != bool_mode)
6859 target_bool = gen_reg_rtx (bool_mode);
6861 /* Emit the compare_and_swap. */
6862 create_output_operand (&ops[0], target_bool, bool_mode);
6863 create_output_operand (&ops[1], target_oval, mode);
6864 create_fixed_operand (&ops[2], mem);
6865 create_input_operand (&ops[3], expected, mode);
6866 create_input_operand (&ops[4], desired, mode);
6867 create_integer_operand (&ops[5], is_weak);
6868 create_integer_operand (&ops[6], succ_model);
6869 create_integer_operand (&ops[7], fail_model);
6870 if (maybe_expand_insn (icode, 8, ops))
6872 /* Return success/failure. */
6873 target_bool = ops[0].value;
6874 target_oval = ops[1].value;
6875 goto success;
6879 /* Otherwise fall back to the original __sync_val_compare_and_swap
6880 which is always seq-cst. */
6881 icode = optab_handler (sync_compare_and_swap_optab, mode);
6882 if (icode != CODE_FOR_nothing)
6884 rtx cc_reg;
6886 create_output_operand (&ops[0], target_oval, mode);
6887 create_fixed_operand (&ops[1], mem);
6888 create_input_operand (&ops[2], expected, mode);
6889 create_input_operand (&ops[3], desired, mode);
6890 if (!maybe_expand_insn (icode, 4, ops))
6891 return false;
6893 target_oval = ops[0].value;
6895 /* If the caller isn't interested in the boolean return value,
6896 skip the computation of it. */
6897 if (ptarget_bool == NULL)
6898 goto success;
6900 /* Otherwise, work out if the compare-and-swap succeeded. */
6901 cc_reg = NULL_RTX;
6902 if (have_insn_for (COMPARE, CCmode))
6903 note_stores (get_last_insn (), find_cc_set, &cc_reg);
6904 if (cc_reg)
6906 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
6907 const0_rtx, VOIDmode, 0, 1);
6908 goto success;
6910 goto success_bool_from_val;
6913 /* Also check for library support for __sync_val_compare_and_swap. */
6914 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
6915 if (libfunc != NULL)
6917 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6918 rtx target = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6919 mode, addr, ptr_mode,
6920 expected, mode, desired, mode);
6921 emit_move_insn (target_oval, target);
6923 /* Compute the boolean return value only if requested. */
6924 if (ptarget_bool)
6925 goto success_bool_from_val;
6926 else
6927 goto success;
6930 /* Failure. */
6931 return false;
6933 success_bool_from_val:
6934 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
6935 expected, VOIDmode, 1, 1);
6936 success:
6937 /* Make sure that the oval output winds up where the caller asked. */
6938 if (ptarget_oval)
6939 *ptarget_oval = target_oval;
6940 if (ptarget_bool)
6941 *ptarget_bool = target_bool;
6942 return true;
6945 /* Generate asm volatile("" : : : "memory") as the memory blockage. */
6947 static void
6948 expand_asm_memory_blockage (void)
6950 rtx asm_op, clob;
6952 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
6953 rtvec_alloc (0), rtvec_alloc (0),
6954 rtvec_alloc (0), UNKNOWN_LOCATION);
6955 MEM_VOLATILE_P (asm_op) = 1;
6957 clob = gen_rtx_SCRATCH (VOIDmode);
6958 clob = gen_rtx_MEM (BLKmode, clob);
6959 clob = gen_rtx_CLOBBER (VOIDmode, clob);
6961 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
6964 /* Do not propagate memory accesses across this point. */
6966 static void
6967 expand_memory_blockage (void)
6969 if (targetm.have_memory_blockage ())
6970 emit_insn (targetm.gen_memory_blockage ());
6971 else
6972 expand_asm_memory_blockage ();
6975 /* Generate asm volatile("" : : : "memory") as a memory blockage, at the
6976 same time clobbering the register set specified by REGS. */
6978 void
6979 expand_asm_reg_clobber_mem_blockage (HARD_REG_SET regs)
6981 rtx asm_op, clob_mem;
6983 unsigned int num_of_regs = 0;
6984 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
6985 if (TEST_HARD_REG_BIT (regs, i))
6986 num_of_regs++;
6988 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
6989 rtvec_alloc (0), rtvec_alloc (0),
6990 rtvec_alloc (0), UNKNOWN_LOCATION);
6991 MEM_VOLATILE_P (asm_op) = 1;
6993 rtvec v = rtvec_alloc (num_of_regs + 2);
6995 clob_mem = gen_rtx_SCRATCH (VOIDmode);
6996 clob_mem = gen_rtx_MEM (BLKmode, clob_mem);
6997 clob_mem = gen_rtx_CLOBBER (VOIDmode, clob_mem);
6999 RTVEC_ELT (v, 0) = asm_op;
7000 RTVEC_ELT (v, 1) = clob_mem;
7002 if (num_of_regs > 0)
7004 unsigned int j = 2;
7005 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7006 if (TEST_HARD_REG_BIT (regs, i))
7008 RTVEC_ELT (v, j) = gen_rtx_CLOBBER (VOIDmode, regno_reg_rtx[i]);
7009 j++;
7011 gcc_assert (j == (num_of_regs + 2));
7014 emit_insn (gen_rtx_PARALLEL (VOIDmode, v));
7017 /* This routine will either emit the mem_thread_fence pattern or issue a
7018 sync_synchronize to generate a fence for memory model MEMMODEL. */
7020 void
7021 expand_mem_thread_fence (enum memmodel model)
7023 if (is_mm_relaxed (model))
7024 return;
7025 if (targetm.have_mem_thread_fence ())
7027 emit_insn (targetm.gen_mem_thread_fence (GEN_INT (model)));
7028 expand_memory_blockage ();
7030 else if (targetm.have_memory_barrier ())
7031 emit_insn (targetm.gen_memory_barrier ());
7032 else if (synchronize_libfunc != NULL_RTX)
7033 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode);
7034 else
7035 expand_memory_blockage ();
7038 /* Emit a signal fence with given memory model. */
7040 void
7041 expand_mem_signal_fence (enum memmodel model)
7043 /* No machine barrier is required to implement a signal fence, but
7044 a compiler memory barrier must be issued, except for relaxed MM. */
7045 if (!is_mm_relaxed (model))
7046 expand_memory_blockage ();
7049 /* This function expands the atomic load operation:
7050 return the atomically loaded value in MEM.
7052 MEMMODEL is the memory model variant to use.
7053 TARGET is an option place to stick the return value. */
7056 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7058 machine_mode mode = GET_MODE (mem);
7059 enum insn_code icode;
7061 /* If the target supports the load directly, great. */
7062 icode = direct_optab_handler (atomic_load_optab, mode);
7063 if (icode != CODE_FOR_nothing)
7065 class expand_operand ops[3];
7066 rtx_insn *last = get_last_insn ();
7067 if (is_mm_seq_cst (model))
7068 expand_memory_blockage ();
7070 create_output_operand (&ops[0], target, mode);
7071 create_fixed_operand (&ops[1], mem);
7072 create_integer_operand (&ops[2], model);
7073 if (maybe_expand_insn (icode, 3, ops))
7075 if (!is_mm_relaxed (model))
7076 expand_memory_blockage ();
7077 return ops[0].value;
7079 delete_insns_since (last);
7082 /* If the size of the object is greater than word size on this target,
7083 then we assume that a load will not be atomic. We could try to
7084 emulate a load with a compare-and-swap operation, but the store that
7085 doing this could result in would be incorrect if this is a volatile
7086 atomic load or targetting read-only-mapped memory. */
7087 if (maybe_gt (GET_MODE_PRECISION (mode), BITS_PER_WORD))
7088 /* If there is no atomic load, leave the library call. */
7089 return NULL_RTX;
7091 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7092 if (!target || target == const0_rtx)
7093 target = gen_reg_rtx (mode);
7095 /* For SEQ_CST, emit a barrier before the load. */
7096 if (is_mm_seq_cst (model))
7097 expand_mem_thread_fence (model);
7099 emit_move_insn (target, mem);
7101 /* Emit the appropriate barrier after the load. */
7102 expand_mem_thread_fence (model);
7104 return target;
7107 /* This function expands the atomic store operation:
7108 Atomically store VAL in MEM.
7109 MEMMODEL is the memory model variant to use.
7110 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7111 function returns const0_rtx if a pattern was emitted. */
7114 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7116 machine_mode mode = GET_MODE (mem);
7117 enum insn_code icode;
7118 class expand_operand ops[3];
7120 /* If the target supports the store directly, great. */
7121 icode = direct_optab_handler (atomic_store_optab, mode);
7122 if (icode != CODE_FOR_nothing)
7124 rtx_insn *last = get_last_insn ();
7125 if (!is_mm_relaxed (model))
7126 expand_memory_blockage ();
7127 create_fixed_operand (&ops[0], mem);
7128 create_input_operand (&ops[1], val, mode);
7129 create_integer_operand (&ops[2], model);
7130 if (maybe_expand_insn (icode, 3, ops))
7132 if (is_mm_seq_cst (model))
7133 expand_memory_blockage ();
7134 return const0_rtx;
7136 delete_insns_since (last);
7139 /* If using __sync_lock_release is a viable alternative, try it.
7140 Note that this will not be set to true if we are expanding a generic
7141 __atomic_store_n. */
7142 if (use_release)
7144 icode = direct_optab_handler (sync_lock_release_optab, mode);
7145 if (icode != CODE_FOR_nothing)
7147 create_fixed_operand (&ops[0], mem);
7148 create_input_operand (&ops[1], const0_rtx, mode);
7149 if (maybe_expand_insn (icode, 2, ops))
7151 /* lock_release is only a release barrier. */
7152 if (is_mm_seq_cst (model))
7153 expand_mem_thread_fence (model);
7154 return const0_rtx;
7159 /* If the size of the object is greater than word size on this target,
7160 a default store will not be atomic. */
7161 if (maybe_gt (GET_MODE_PRECISION (mode), BITS_PER_WORD))
7163 /* If loads are atomic or we are called to provide a __sync builtin,
7164 we can try a atomic_exchange and throw away the result. Otherwise,
7165 don't do anything so that we do not create an inconsistency between
7166 loads and stores. */
7167 if (can_atomic_load_p (mode) || is_mm_sync (model))
7169 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7170 if (!target)
7171 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem,
7172 val);
7173 if (target)
7174 return const0_rtx;
7176 return NULL_RTX;
7179 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7180 expand_mem_thread_fence (model);
7182 emit_move_insn (mem, val);
7184 /* For SEQ_CST, also emit a barrier after the store. */
7185 if (is_mm_seq_cst (model))
7186 expand_mem_thread_fence (model);
7188 return const0_rtx;
7192 /* Structure containing the pointers and values required to process the
7193 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7195 struct atomic_op_functions
7197 direct_optab mem_fetch_before;
7198 direct_optab mem_fetch_after;
7199 direct_optab mem_no_result;
7200 optab fetch_before;
7201 optab fetch_after;
7202 direct_optab no_result;
7203 enum rtx_code reverse_code;
7207 /* Fill in structure pointed to by OP with the various optab entries for an
7208 operation of type CODE. */
7210 static void
7211 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7213 gcc_assert (op!= NULL);
7215 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7216 in the source code during compilation, and the optab entries are not
7217 computable until runtime. Fill in the values at runtime. */
7218 switch (code)
7220 case PLUS:
7221 op->mem_fetch_before = atomic_fetch_add_optab;
7222 op->mem_fetch_after = atomic_add_fetch_optab;
7223 op->mem_no_result = atomic_add_optab;
7224 op->fetch_before = sync_old_add_optab;
7225 op->fetch_after = sync_new_add_optab;
7226 op->no_result = sync_add_optab;
7227 op->reverse_code = MINUS;
7228 break;
7229 case MINUS:
7230 op->mem_fetch_before = atomic_fetch_sub_optab;
7231 op->mem_fetch_after = atomic_sub_fetch_optab;
7232 op->mem_no_result = atomic_sub_optab;
7233 op->fetch_before = sync_old_sub_optab;
7234 op->fetch_after = sync_new_sub_optab;
7235 op->no_result = sync_sub_optab;
7236 op->reverse_code = PLUS;
7237 break;
7238 case XOR:
7239 op->mem_fetch_before = atomic_fetch_xor_optab;
7240 op->mem_fetch_after = atomic_xor_fetch_optab;
7241 op->mem_no_result = atomic_xor_optab;
7242 op->fetch_before = sync_old_xor_optab;
7243 op->fetch_after = sync_new_xor_optab;
7244 op->no_result = sync_xor_optab;
7245 op->reverse_code = XOR;
7246 break;
7247 case AND:
7248 op->mem_fetch_before = atomic_fetch_and_optab;
7249 op->mem_fetch_after = atomic_and_fetch_optab;
7250 op->mem_no_result = atomic_and_optab;
7251 op->fetch_before = sync_old_and_optab;
7252 op->fetch_after = sync_new_and_optab;
7253 op->no_result = sync_and_optab;
7254 op->reverse_code = UNKNOWN;
7255 break;
7256 case IOR:
7257 op->mem_fetch_before = atomic_fetch_or_optab;
7258 op->mem_fetch_after = atomic_or_fetch_optab;
7259 op->mem_no_result = atomic_or_optab;
7260 op->fetch_before = sync_old_ior_optab;
7261 op->fetch_after = sync_new_ior_optab;
7262 op->no_result = sync_ior_optab;
7263 op->reverse_code = UNKNOWN;
7264 break;
7265 case NOT:
7266 op->mem_fetch_before = atomic_fetch_nand_optab;
7267 op->mem_fetch_after = atomic_nand_fetch_optab;
7268 op->mem_no_result = atomic_nand_optab;
7269 op->fetch_before = sync_old_nand_optab;
7270 op->fetch_after = sync_new_nand_optab;
7271 op->no_result = sync_nand_optab;
7272 op->reverse_code = UNKNOWN;
7273 break;
7274 default:
7275 gcc_unreachable ();
7279 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7280 using memory order MODEL. If AFTER is true the operation needs to return
7281 the value of *MEM after the operation, otherwise the previous value.
7282 TARGET is an optional place to place the result. The result is unused if
7283 it is const0_rtx.
7284 Return the result if there is a better sequence, otherwise NULL_RTX. */
7286 static rtx
7287 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7288 enum memmodel model, bool after)
7290 /* If the value is prefetched, or not used, it may be possible to replace
7291 the sequence with a native exchange operation. */
7292 if (!after || target == const0_rtx)
7294 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7295 if (code == AND && val == const0_rtx)
7297 if (target == const0_rtx)
7298 target = gen_reg_rtx (GET_MODE (mem));
7299 return maybe_emit_atomic_exchange (target, mem, val, model);
7302 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7303 if (code == IOR && val == constm1_rtx)
7305 if (target == const0_rtx)
7306 target = gen_reg_rtx (GET_MODE (mem));
7307 return maybe_emit_atomic_exchange (target, mem, val, model);
7311 return NULL_RTX;
7314 /* Try to emit an instruction for a specific operation varaition.
7315 OPTAB contains the OP functions.
7316 TARGET is an optional place to return the result. const0_rtx means unused.
7317 MEM is the memory location to operate on.
7318 VAL is the value to use in the operation.
7319 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7320 MODEL is the memory model, if used.
7321 AFTER is true if the returned result is the value after the operation. */
7323 static rtx
7324 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7325 rtx val, bool use_memmodel, enum memmodel model, bool after)
7327 machine_mode mode = GET_MODE (mem);
7328 class expand_operand ops[4];
7329 enum insn_code icode;
7330 int op_counter = 0;
7331 int num_ops;
7333 /* Check to see if there is a result returned. */
7334 if (target == const0_rtx)
7336 if (use_memmodel)
7338 icode = direct_optab_handler (optab->mem_no_result, mode);
7339 create_integer_operand (&ops[2], model);
7340 num_ops = 3;
7342 else
7344 icode = direct_optab_handler (optab->no_result, mode);
7345 num_ops = 2;
7348 /* Otherwise, we need to generate a result. */
7349 else
7351 if (use_memmodel)
7353 icode = direct_optab_handler (after ? optab->mem_fetch_after
7354 : optab->mem_fetch_before, mode);
7355 create_integer_operand (&ops[3], model);
7356 num_ops = 4;
7358 else
7360 icode = optab_handler (after ? optab->fetch_after
7361 : optab->fetch_before, mode);
7362 num_ops = 3;
7364 create_output_operand (&ops[op_counter++], target, mode);
7366 if (icode == CODE_FOR_nothing)
7367 return NULL_RTX;
7369 create_fixed_operand (&ops[op_counter++], mem);
7370 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7371 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7373 if (maybe_expand_insn (icode, num_ops, ops))
7374 return (target == const0_rtx ? const0_rtx : ops[0].value);
7376 return NULL_RTX;
7380 /* This function expands an atomic fetch_OP or OP_fetch operation:
7381 TARGET is an option place to stick the return value. const0_rtx indicates
7382 the result is unused.
7383 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7384 CODE is the operation being performed (OP)
7385 MEMMODEL is the memory model variant to use.
7386 AFTER is true to return the result of the operation (OP_fetch).
7387 AFTER is false to return the value before the operation (fetch_OP).
7389 This function will *only* generate instructions if there is a direct
7390 optab. No compare and swap loops or libcalls will be generated. */
7392 static rtx
7393 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7394 enum rtx_code code, enum memmodel model,
7395 bool after)
7397 machine_mode mode = GET_MODE (mem);
7398 struct atomic_op_functions optab;
7399 rtx result;
7400 bool unused_result = (target == const0_rtx);
7402 get_atomic_op_for_code (&optab, code);
7404 /* Check to see if there are any better instructions. */
7405 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7406 if (result)
7407 return result;
7409 /* Check for the case where the result isn't used and try those patterns. */
7410 if (unused_result)
7412 /* Try the memory model variant first. */
7413 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7414 if (result)
7415 return result;
7417 /* Next try the old style withuot a memory model. */
7418 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7419 if (result)
7420 return result;
7422 /* There is no no-result pattern, so try patterns with a result. */
7423 target = NULL_RTX;
7426 /* Try the __atomic version. */
7427 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7428 if (result)
7429 return result;
7431 /* Try the older __sync version. */
7432 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7433 if (result)
7434 return result;
7436 /* If the fetch value can be calculated from the other variation of fetch,
7437 try that operation. */
7438 if (after || unused_result || optab.reverse_code != UNKNOWN)
7440 /* Try the __atomic version, then the older __sync version. */
7441 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7442 if (!result)
7443 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7445 if (result)
7447 /* If the result isn't used, no need to do compensation code. */
7448 if (unused_result)
7449 return result;
7451 /* Issue compensation code. Fetch_after == fetch_before OP val.
7452 Fetch_before == after REVERSE_OP val. */
7453 if (!after)
7454 code = optab.reverse_code;
7455 if (code == NOT)
7457 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
7458 true, OPTAB_LIB_WIDEN);
7459 result = expand_simple_unop (mode, NOT, result, target, true);
7461 else
7462 result = expand_simple_binop (mode, code, result, val, target,
7463 true, OPTAB_LIB_WIDEN);
7464 return result;
7468 /* No direct opcode can be generated. */
7469 return NULL_RTX;
7474 /* This function expands an atomic fetch_OP or OP_fetch operation:
7475 TARGET is an option place to stick the return value. const0_rtx indicates
7476 the result is unused.
7477 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7478 CODE is the operation being performed (OP)
7479 MEMMODEL is the memory model variant to use.
7480 AFTER is true to return the result of the operation (OP_fetch).
7481 AFTER is false to return the value before the operation (fetch_OP). */
7483 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7484 enum memmodel model, bool after)
7486 machine_mode mode = GET_MODE (mem);
7487 rtx result;
7488 bool unused_result = (target == const0_rtx);
7490 /* If loads are not atomic for the required size and we are not called to
7491 provide a __sync builtin, do not do anything so that we stay consistent
7492 with atomic loads of the same size. */
7493 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
7494 return NULL_RTX;
7496 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
7497 after);
7499 if (result)
7500 return result;
7502 /* Add/sub can be implemented by doing the reverse operation with -(val). */
7503 if (code == PLUS || code == MINUS)
7505 rtx tmp;
7506 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
7508 start_sequence ();
7509 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
7510 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
7511 model, after);
7512 if (result)
7514 /* PLUS worked so emit the insns and return. */
7515 tmp = get_insns ();
7516 end_sequence ();
7517 emit_insn (tmp);
7518 return result;
7521 /* PLUS did not work, so throw away the negation code and continue. */
7522 end_sequence ();
7525 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
7526 if (!can_compare_and_swap_p (mode, false))
7528 rtx libfunc;
7529 bool fixup = false;
7530 enum rtx_code orig_code = code;
7531 struct atomic_op_functions optab;
7533 get_atomic_op_for_code (&optab, code);
7534 libfunc = optab_libfunc (after ? optab.fetch_after
7535 : optab.fetch_before, mode);
7536 if (libfunc == NULL
7537 && (after || unused_result || optab.reverse_code != UNKNOWN))
7539 fixup = true;
7540 if (!after)
7541 code = optab.reverse_code;
7542 libfunc = optab_libfunc (after ? optab.fetch_before
7543 : optab.fetch_after, mode);
7545 if (libfunc != NULL)
7547 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7548 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
7549 addr, ptr_mode, val, mode);
7551 if (!unused_result && fixup)
7552 result = expand_simple_binop (mode, code, result, val, target,
7553 true, OPTAB_LIB_WIDEN);
7554 return result;
7557 /* We need the original code for any further attempts. */
7558 code = orig_code;
7561 /* If nothing else has succeeded, default to a compare and swap loop. */
7562 if (can_compare_and_swap_p (mode, true))
7564 rtx_insn *insn;
7565 rtx t0 = gen_reg_rtx (mode), t1;
7567 start_sequence ();
7569 /* If the result is used, get a register for it. */
7570 if (!unused_result)
7572 if (!target || !register_operand (target, mode))
7573 target = gen_reg_rtx (mode);
7574 /* If fetch_before, copy the value now. */
7575 if (!after)
7576 emit_move_insn (target, t0);
7578 else
7579 target = const0_rtx;
7581 t1 = t0;
7582 if (code == NOT)
7584 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
7585 true, OPTAB_LIB_WIDEN);
7586 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
7588 else
7589 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
7590 OPTAB_LIB_WIDEN);
7592 /* For after, copy the value now. */
7593 if (!unused_result && after)
7594 emit_move_insn (target, t1);
7595 insn = get_insns ();
7596 end_sequence ();
7598 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7599 return target;
7602 return NULL_RTX;
7605 /* Return true if OPERAND is suitable for operand number OPNO of
7606 instruction ICODE. */
7608 bool
7609 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
7611 return (!insn_data[(int) icode].operand[opno].predicate
7612 || (insn_data[(int) icode].operand[opno].predicate
7613 (operand, insn_data[(int) icode].operand[opno].mode)));
7616 /* TARGET is a target of a multiword operation that we are going to
7617 implement as a series of word-mode operations. Return true if
7618 TARGET is suitable for this purpose. */
7620 bool
7621 valid_multiword_target_p (rtx target)
7623 machine_mode mode;
7624 int i, size;
7626 mode = GET_MODE (target);
7627 if (!GET_MODE_SIZE (mode).is_constant (&size))
7628 return false;
7629 for (i = 0; i < size; i += UNITS_PER_WORD)
7630 if (!validate_subreg (word_mode, mode, target, i))
7631 return false;
7632 return true;
7635 /* Make OP describe an input operand that has value INTVAL and that has
7636 no inherent mode. This function should only be used for operands that
7637 are always expand-time constants. The backend may request that INTVAL
7638 be copied into a different kind of rtx, but it must specify the mode
7639 of that rtx if so. */
7641 void
7642 create_integer_operand (class expand_operand *op, poly_int64 intval)
7644 create_expand_operand (op, EXPAND_INTEGER,
7645 gen_int_mode (intval, MAX_MODE_INT),
7646 VOIDmode, false, intval);
7649 /* Like maybe_legitimize_operand, but do not change the code of the
7650 current rtx value. */
7652 static bool
7653 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
7654 class expand_operand *op)
7656 /* See if the operand matches in its current form. */
7657 if (insn_operand_matches (icode, opno, op->value))
7658 return true;
7660 /* If the operand is a memory whose address has no side effects,
7661 try forcing the address into a non-virtual pseudo register.
7662 The check for side effects is important because copy_to_mode_reg
7663 cannot handle things like auto-modified addresses. */
7664 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
7666 rtx addr, mem;
7668 mem = op->value;
7669 addr = XEXP (mem, 0);
7670 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
7671 && !side_effects_p (addr))
7673 rtx_insn *last;
7674 machine_mode mode;
7676 last = get_last_insn ();
7677 mode = get_address_mode (mem);
7678 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
7679 if (insn_operand_matches (icode, opno, mem))
7681 op->value = mem;
7682 return true;
7684 delete_insns_since (last);
7688 return false;
7691 /* Try to make OP match operand OPNO of instruction ICODE. Return true
7692 on success, storing the new operand value back in OP. */
7694 static bool
7695 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
7696 class expand_operand *op)
7698 machine_mode mode, imode, tmode;
7700 mode = op->mode;
7701 switch (op->type)
7703 case EXPAND_FIXED:
7705 temporary_volatile_ok v (true);
7706 return maybe_legitimize_operand_same_code (icode, opno, op);
7709 case EXPAND_OUTPUT:
7710 gcc_assert (mode != VOIDmode);
7711 if (op->value
7712 && op->value != const0_rtx
7713 && GET_MODE (op->value) == mode
7714 && maybe_legitimize_operand_same_code (icode, opno, op))
7715 return true;
7717 op->value = gen_reg_rtx (mode);
7718 op->target = 0;
7719 break;
7721 case EXPAND_INPUT:
7722 input:
7723 gcc_assert (mode != VOIDmode);
7724 gcc_assert (GET_MODE (op->value) == VOIDmode
7725 || GET_MODE (op->value) == mode);
7726 if (maybe_legitimize_operand_same_code (icode, opno, op))
7727 return true;
7729 op->value = copy_to_mode_reg (mode, op->value);
7730 break;
7732 case EXPAND_CONVERT_TO:
7733 gcc_assert (mode != VOIDmode);
7734 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
7735 goto input;
7737 case EXPAND_CONVERT_FROM:
7738 if (GET_MODE (op->value) != VOIDmode)
7739 mode = GET_MODE (op->value);
7740 else
7741 /* The caller must tell us what mode this value has. */
7742 gcc_assert (mode != VOIDmode);
7744 imode = insn_data[(int) icode].operand[opno].mode;
7745 tmode = (VECTOR_MODE_P (imode) && !VECTOR_MODE_P (mode)
7746 ? GET_MODE_INNER (imode) : imode);
7747 if (tmode != VOIDmode && tmode != mode)
7749 op->value = convert_modes (tmode, mode, op->value, op->unsigned_p);
7750 mode = tmode;
7752 if (imode != VOIDmode && imode != mode)
7754 gcc_assert (VECTOR_MODE_P (imode) && !VECTOR_MODE_P (mode));
7755 op->value = expand_vector_broadcast (imode, op->value);
7756 mode = imode;
7758 goto input;
7760 case EXPAND_ADDRESS:
7761 op->value = convert_memory_address (as_a <scalar_int_mode> (mode),
7762 op->value);
7763 goto input;
7765 case EXPAND_INTEGER:
7766 mode = insn_data[(int) icode].operand[opno].mode;
7767 if (mode != VOIDmode
7768 && known_eq (trunc_int_for_mode (op->int_value, mode),
7769 op->int_value))
7771 op->value = gen_int_mode (op->int_value, mode);
7772 goto input;
7774 break;
7776 return insn_operand_matches (icode, opno, op->value);
7779 /* Make OP describe an input operand that should have the same value
7780 as VALUE, after any mode conversion that the target might request.
7781 TYPE is the type of VALUE. */
7783 void
7784 create_convert_operand_from_type (class expand_operand *op,
7785 rtx value, tree type)
7787 create_convert_operand_from (op, value, TYPE_MODE (type),
7788 TYPE_UNSIGNED (type));
7791 /* Return true if the requirements on operands OP1 and OP2 of instruction
7792 ICODE are similar enough for the result of legitimizing OP1 to be
7793 reusable for OP2. OPNO1 and OPNO2 are the operand numbers associated
7794 with OP1 and OP2 respectively. */
7796 static inline bool
7797 can_reuse_operands_p (enum insn_code icode,
7798 unsigned int opno1, unsigned int opno2,
7799 const class expand_operand *op1,
7800 const class expand_operand *op2)
7802 /* Check requirements that are common to all types. */
7803 if (op1->type != op2->type
7804 || op1->mode != op2->mode
7805 || (insn_data[(int) icode].operand[opno1].mode
7806 != insn_data[(int) icode].operand[opno2].mode))
7807 return false;
7809 /* Check the requirements for specific types. */
7810 switch (op1->type)
7812 case EXPAND_OUTPUT:
7813 /* Outputs must remain distinct. */
7814 return false;
7816 case EXPAND_FIXED:
7817 case EXPAND_INPUT:
7818 case EXPAND_ADDRESS:
7819 case EXPAND_INTEGER:
7820 return true;
7822 case EXPAND_CONVERT_TO:
7823 case EXPAND_CONVERT_FROM:
7824 return op1->unsigned_p == op2->unsigned_p;
7826 gcc_unreachable ();
7829 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
7830 of instruction ICODE. Return true on success, leaving the new operand
7831 values in the OPS themselves. Emit no code on failure. */
7833 bool
7834 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
7835 unsigned int nops, class expand_operand *ops)
7837 rtx_insn *last = get_last_insn ();
7838 rtx *orig_values = XALLOCAVEC (rtx, nops);
7839 for (unsigned int i = 0; i < nops; i++)
7841 orig_values[i] = ops[i].value;
7843 /* First try reusing the result of an earlier legitimization.
7844 This avoids duplicate rtl and ensures that tied operands
7845 remain tied.
7847 This search is linear, but NOPS is bounded at compile time
7848 to a small number (current a single digit). */
7849 unsigned int j = 0;
7850 for (; j < i; ++j)
7851 if (can_reuse_operands_p (icode, opno + j, opno + i, &ops[j], &ops[i])
7852 && rtx_equal_p (orig_values[j], orig_values[i])
7853 && ops[j].value
7854 && insn_operand_matches (icode, opno + i, ops[j].value))
7856 ops[i].value = copy_rtx (ops[j].value);
7857 break;
7860 /* Otherwise try legitimizing the operand on its own. */
7861 if (j == i && !maybe_legitimize_operand (icode, opno + i, &ops[i]))
7863 delete_insns_since (last);
7864 return false;
7867 return true;
7870 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
7871 as its operands. Return the instruction pattern on success,
7872 and emit any necessary set-up code. Return null and emit no
7873 code on failure. */
7875 rtx_insn *
7876 maybe_gen_insn (enum insn_code icode, unsigned int nops,
7877 class expand_operand *ops)
7879 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
7880 if (!maybe_legitimize_operands (icode, 0, nops, ops))
7881 return NULL;
7883 switch (nops)
7885 case 1:
7886 return GEN_FCN (icode) (ops[0].value);
7887 case 2:
7888 return GEN_FCN (icode) (ops[0].value, ops[1].value);
7889 case 3:
7890 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
7891 case 4:
7892 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7893 ops[3].value);
7894 case 5:
7895 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7896 ops[3].value, ops[4].value);
7897 case 6:
7898 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7899 ops[3].value, ops[4].value, ops[5].value);
7900 case 7:
7901 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7902 ops[3].value, ops[4].value, ops[5].value,
7903 ops[6].value);
7904 case 8:
7905 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7906 ops[3].value, ops[4].value, ops[5].value,
7907 ops[6].value, ops[7].value);
7908 case 9:
7909 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7910 ops[3].value, ops[4].value, ops[5].value,
7911 ops[6].value, ops[7].value, ops[8].value);
7913 gcc_unreachable ();
7916 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
7917 as its operands. Return true on success and emit no code on failure. */
7919 bool
7920 maybe_expand_insn (enum insn_code icode, unsigned int nops,
7921 class expand_operand *ops)
7923 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7924 if (pat)
7926 emit_insn (pat);
7927 return true;
7929 return false;
7932 /* Like maybe_expand_insn, but for jumps. */
7934 bool
7935 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
7936 class expand_operand *ops)
7938 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7939 if (pat)
7941 emit_jump_insn (pat);
7942 return true;
7944 return false;
7947 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
7948 as its operands. */
7950 void
7951 expand_insn (enum insn_code icode, unsigned int nops,
7952 class expand_operand *ops)
7954 if (!maybe_expand_insn (icode, nops, ops))
7955 gcc_unreachable ();
7958 /* Like expand_insn, but for jumps. */
7960 void
7961 expand_jump_insn (enum insn_code icode, unsigned int nops,
7962 class expand_operand *ops)
7964 if (!maybe_expand_jump_insn (icode, nops, ops))
7965 gcc_unreachable ();