d: Merge upstream dmd 56589f0f4, druntime 651389b5, phobos 1516ecad9.
[official-gcc.git] / gcc / optabs.cc
bloba50dd798f2a454ac54e247f3e6cbab17577ea304
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2022 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 static rtx emit_conditional_move_1 (rtx, rtx, rtx, rtx, machine_mode);
57 /* Debug facility for use in GDB. */
58 void debug_optab_libfuncs (void);
60 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
61 the result of operation CODE applied to OP0 (and OP1 if it is a binary
62 operation). OP0_MODE is OP0's mode.
64 If the last insn does not set TARGET, don't do anything, but return 1.
66 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
67 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
68 try again, ensuring that TARGET is not one of the operands. */
70 static int
71 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0,
72 rtx op1, machine_mode op0_mode)
74 rtx_insn *last_insn;
75 rtx set;
76 rtx note;
78 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
80 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
81 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
82 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
83 && GET_RTX_CLASS (code) != RTX_COMPARE
84 && GET_RTX_CLASS (code) != RTX_UNARY)
85 return 1;
87 if (GET_CODE (target) == ZERO_EXTRACT)
88 return 1;
90 for (last_insn = insns;
91 NEXT_INSN (last_insn) != NULL_RTX;
92 last_insn = NEXT_INSN (last_insn))
95 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
96 a value changing in the insn, so the note would be invalid for CSE. */
97 if (reg_overlap_mentioned_p (target, op0)
98 || (op1 && reg_overlap_mentioned_p (target, op1)))
100 if (MEM_P (target)
101 && (rtx_equal_p (target, op0)
102 || (op1 && rtx_equal_p (target, op1))))
104 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
105 over expanding it as temp = MEM op X, MEM = temp. If the target
106 supports MEM = MEM op X instructions, it is sometimes too hard
107 to reconstruct that form later, especially if X is also a memory,
108 and due to multiple occurrences of addresses the address might
109 be forced into register unnecessarily.
110 Note that not emitting the REG_EQUIV note might inhibit
111 CSE in some cases. */
112 set = single_set (last_insn);
113 if (set
114 && GET_CODE (SET_SRC (set)) == code
115 && MEM_P (SET_DEST (set))
116 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
117 || (op1 && rtx_equal_p (SET_DEST (set),
118 XEXP (SET_SRC (set), 1)))))
119 return 1;
121 return 0;
124 set = set_for_reg_notes (last_insn);
125 if (set == NULL_RTX)
126 return 1;
128 if (! rtx_equal_p (SET_DEST (set), target)
129 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
130 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
131 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
132 return 1;
134 if (GET_RTX_CLASS (code) == RTX_UNARY)
135 switch (code)
137 case FFS:
138 case CLZ:
139 case CTZ:
140 case CLRSB:
141 case POPCOUNT:
142 case PARITY:
143 case BSWAP:
144 if (op0_mode != VOIDmode && GET_MODE (target) != op0_mode)
146 note = gen_rtx_fmt_e (code, op0_mode, copy_rtx (op0));
147 if (GET_MODE_UNIT_SIZE (op0_mode)
148 > GET_MODE_UNIT_SIZE (GET_MODE (target)))
149 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
150 note, op0_mode);
151 else
152 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
153 note, op0_mode);
154 break;
156 /* FALLTHRU */
157 default:
158 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
159 break;
161 else
162 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
164 set_unique_reg_note (last_insn, REG_EQUAL, note);
166 return 1;
169 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
170 for a widening operation would be. In most cases this would be OP0, but if
171 that's a constant it'll be VOIDmode, which isn't useful. */
173 static machine_mode
174 widened_mode (machine_mode to_mode, rtx op0, rtx op1)
176 machine_mode m0 = GET_MODE (op0);
177 machine_mode m1 = GET_MODE (op1);
178 machine_mode result;
180 if (m0 == VOIDmode && m1 == VOIDmode)
181 return to_mode;
182 else if (m0 == VOIDmode || GET_MODE_UNIT_SIZE (m0) < GET_MODE_UNIT_SIZE (m1))
183 result = m1;
184 else
185 result = m0;
187 if (GET_MODE_UNIT_SIZE (result) > GET_MODE_UNIT_SIZE (to_mode))
188 return to_mode;
190 return result;
193 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
194 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
195 not actually do a sign-extend or zero-extend, but can leave the
196 higher-order bits of the result rtx undefined, for example, in the case
197 of logical operations, but not right shifts. */
199 static rtx
200 widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
201 int unsignedp, int no_extend)
203 rtx result;
204 scalar_int_mode int_mode;
206 /* If we don't have to extend and this is a constant, return it. */
207 if (no_extend && GET_MODE (op) == VOIDmode)
208 return op;
210 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
211 extend since it will be more efficient to do so unless the signedness of
212 a promoted object differs from our extension. */
213 if (! no_extend
214 || !is_a <scalar_int_mode> (mode, &int_mode)
215 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
216 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
217 return convert_modes (mode, oldmode, op, unsignedp);
219 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
220 SUBREG. */
221 if (GET_MODE_SIZE (int_mode) <= UNITS_PER_WORD)
222 return gen_lowpart (int_mode, force_reg (GET_MODE (op), op));
224 /* Otherwise, get an object of MODE, clobber it, and set the low-order
225 part to OP. */
227 result = gen_reg_rtx (int_mode);
228 emit_clobber (result);
229 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
230 return result;
233 /* Expand vector widening operations.
235 There are two different classes of operations handled here:
236 1) Operations whose result is wider than all the arguments to the operation.
237 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
238 In this case OP0 and optionally OP1 would be initialized,
239 but WIDE_OP wouldn't (not relevant for this case).
240 2) Operations whose result is of the same size as the last argument to the
241 operation, but wider than all the other arguments to the operation.
242 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
243 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
245 E.g, when called to expand the following operations, this is how
246 the arguments will be initialized:
247 nops OP0 OP1 WIDE_OP
248 widening-sum 2 oprnd0 - oprnd1
249 widening-dot-product 3 oprnd0 oprnd1 oprnd2
250 widening-mult 2 oprnd0 oprnd1 -
251 type-promotion (vec-unpack) 1 oprnd0 - - */
254 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
255 rtx target, int unsignedp)
257 class expand_operand eops[4];
258 tree oprnd0, oprnd1, oprnd2;
259 machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
260 optab widen_pattern_optab;
261 enum insn_code icode;
262 int nops = TREE_CODE_LENGTH (ops->code);
263 int op;
264 bool sbool = false;
266 oprnd0 = ops->op0;
267 oprnd1 = nops >= 2 ? ops->op1 : NULL_TREE;
268 oprnd2 = nops >= 3 ? ops->op2 : NULL_TREE;
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,
633 { cmp_code, cmp1, cmp2, op1_mode },
634 outof_target, outof_superword,
635 word_mode, false))
636 return false;
638 return true;
641 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
642 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
643 input operand; the shift moves bits in the direction OUTOF_INPUT->
644 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
645 of the target. OP1 is the shift count and OP1_MODE is its mode.
646 If OP1 is constant, it will have been truncated as appropriate
647 and is known to be nonzero.
649 If SHIFT_MASK is zero, the result of word shifts is undefined when the
650 shift count is outside the range [0, BITS_PER_WORD). This routine must
651 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
653 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
654 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
655 fill with zeros or sign bits as appropriate.
657 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
658 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
659 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
660 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
661 are undefined.
663 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
664 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
665 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
666 function wants to calculate it itself.
668 Return true if the shift could be successfully synthesized. */
670 static bool
671 expand_doubleword_shift (scalar_int_mode op1_mode, optab binoptab,
672 rtx outof_input, rtx into_input, rtx op1,
673 rtx outof_target, rtx into_target,
674 int unsignedp, enum optab_methods methods,
675 unsigned HOST_WIDE_INT shift_mask)
677 rtx superword_op1, tmp, cmp1, cmp2;
678 enum rtx_code cmp_code;
680 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
681 fill the result with sign or zero bits as appropriate. If so, the value
682 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
683 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
684 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
686 This isn't worthwhile for constant shifts since the optimizers will
687 cope better with in-range shift counts. */
688 if (shift_mask >= BITS_PER_WORD
689 && outof_target != 0
690 && !CONSTANT_P (op1))
692 if (!expand_doubleword_shift (op1_mode, binoptab,
693 outof_input, into_input, op1,
694 0, into_target,
695 unsignedp, methods, shift_mask))
696 return false;
697 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
698 outof_target, unsignedp, methods))
699 return false;
700 return true;
703 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
704 is true when the effective shift value is less than BITS_PER_WORD.
705 Set SUPERWORD_OP1 to the shift count that should be used to shift
706 OUTOF_INPUT into INTO_TARGET when the condition is false. */
707 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
708 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
710 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
711 is a subword shift count. */
712 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
713 0, true, methods);
714 cmp2 = CONST0_RTX (op1_mode);
715 cmp_code = EQ;
716 superword_op1 = op1;
718 else
720 /* Set CMP1 to OP1 - BITS_PER_WORD. */
721 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
722 0, true, methods);
723 cmp2 = CONST0_RTX (op1_mode);
724 cmp_code = LT;
725 superword_op1 = cmp1;
727 if (cmp1 == 0)
728 return false;
730 /* If we can compute the condition at compile time, pick the
731 appropriate subroutine. */
732 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
733 if (tmp != 0 && CONST_INT_P (tmp))
735 if (tmp == const0_rtx)
736 return expand_superword_shift (binoptab, outof_input, superword_op1,
737 outof_target, into_target,
738 unsignedp, methods);
739 else
740 return expand_subword_shift (op1_mode, binoptab,
741 outof_input, into_input, op1,
742 outof_target, into_target,
743 unsignedp, methods, shift_mask);
746 /* Try using conditional moves to generate straight-line code. */
747 if (HAVE_conditional_move)
749 rtx_insn *start = get_last_insn ();
750 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
751 cmp_code, cmp1, cmp2,
752 outof_input, into_input,
753 op1, superword_op1,
754 outof_target, into_target,
755 unsignedp, methods, shift_mask))
756 return true;
757 delete_insns_since (start);
760 /* As a last resort, use branches to select the correct alternative. */
761 rtx_code_label *subword_label = gen_label_rtx ();
762 rtx_code_label *done_label = gen_label_rtx ();
764 NO_DEFER_POP;
765 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
766 0, 0, subword_label,
767 profile_probability::uninitialized ());
768 OK_DEFER_POP;
770 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
771 outof_target, into_target,
772 unsignedp, methods))
773 return false;
775 emit_jump_insn (targetm.gen_jump (done_label));
776 emit_barrier ();
777 emit_label (subword_label);
779 if (!expand_subword_shift (op1_mode, binoptab,
780 outof_input, into_input, op1,
781 outof_target, into_target,
782 unsignedp, methods, shift_mask))
783 return false;
785 emit_label (done_label);
786 return true;
789 /* Subroutine of expand_binop. Perform a double word multiplication of
790 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
791 as the target's word_mode. This function return NULL_RTX if anything
792 goes wrong, in which case it may have already emitted instructions
793 which need to be deleted.
795 If we want to multiply two two-word values and have normal and widening
796 multiplies of single-word values, we can do this with three smaller
797 multiplications.
799 The multiplication proceeds as follows:
800 _______________________
801 [__op0_high_|__op0_low__]
802 _______________________
803 * [__op1_high_|__op1_low__]
804 _______________________________________________
805 _______________________
806 (1) [__op0_low__*__op1_low__]
807 _______________________
808 (2a) [__op0_low__*__op1_high_]
809 _______________________
810 (2b) [__op0_high_*__op1_low__]
811 _______________________
812 (3) [__op0_high_*__op1_high_]
815 This gives a 4-word result. Since we are only interested in the
816 lower 2 words, partial result (3) and the upper words of (2a) and
817 (2b) don't need to be calculated. Hence (2a) and (2b) can be
818 calculated using non-widening multiplication.
820 (1), however, needs to be calculated with an unsigned widening
821 multiplication. If this operation is not directly supported we
822 try using a signed widening multiplication and adjust the result.
823 This adjustment works as follows:
825 If both operands are positive then no adjustment is needed.
827 If the operands have different signs, for example op0_low < 0 and
828 op1_low >= 0, the instruction treats the most significant bit of
829 op0_low as a sign bit instead of a bit with significance
830 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
831 with 2**BITS_PER_WORD - op0_low, and two's complements the
832 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
833 the result.
835 Similarly, if both operands are negative, we need to add
836 (op0_low + op1_low) * 2**BITS_PER_WORD.
838 We use a trick to adjust quickly. We logically shift op0_low right
839 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
840 op0_high (op1_high) before it is used to calculate 2b (2a). If no
841 logical shift exists, we do an arithmetic right shift and subtract
842 the 0 or -1. */
844 static rtx
845 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
846 bool umulp, enum optab_methods methods)
848 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
849 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
850 rtx wordm1 = (umulp ? NULL_RTX
851 : gen_int_shift_amount (word_mode, BITS_PER_WORD - 1));
852 rtx product, adjust, product_high, temp;
854 rtx op0_high = operand_subword_force (op0, high, mode);
855 rtx op0_low = operand_subword_force (op0, low, mode);
856 rtx op1_high = operand_subword_force (op1, high, mode);
857 rtx op1_low = operand_subword_force (op1, low, mode);
859 /* If we're using an unsigned multiply to directly compute the product
860 of the low-order words of the operands and perform any required
861 adjustments of the operands, we begin by trying two more multiplications
862 and then computing the appropriate sum.
864 We have checked above that the required addition is provided.
865 Full-word addition will normally always succeed, especially if
866 it is provided at all, so we don't worry about its failure. The
867 multiplication may well fail, however, so we do handle that. */
869 if (!umulp)
871 /* ??? This could be done with emit_store_flag where available. */
872 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
873 NULL_RTX, 1, methods);
874 if (temp)
875 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
876 NULL_RTX, 0, OPTAB_DIRECT);
877 else
879 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
880 NULL_RTX, 0, methods);
881 if (!temp)
882 return NULL_RTX;
883 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
884 NULL_RTX, 0, OPTAB_DIRECT);
887 if (!op0_high)
888 return NULL_RTX;
891 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
892 NULL_RTX, 0, OPTAB_DIRECT);
893 if (!adjust)
894 return NULL_RTX;
896 /* OP0_HIGH should now be dead. */
898 if (!umulp)
900 /* ??? This could be done with emit_store_flag where available. */
901 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
902 NULL_RTX, 1, methods);
903 if (temp)
904 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
905 NULL_RTX, 0, OPTAB_DIRECT);
906 else
908 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
909 NULL_RTX, 0, methods);
910 if (!temp)
911 return NULL_RTX;
912 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
913 NULL_RTX, 0, OPTAB_DIRECT);
916 if (!op1_high)
917 return NULL_RTX;
920 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
921 NULL_RTX, 0, OPTAB_DIRECT);
922 if (!temp)
923 return NULL_RTX;
925 /* OP1_HIGH should now be dead. */
927 adjust = expand_binop (word_mode, add_optab, adjust, temp,
928 NULL_RTX, 0, OPTAB_DIRECT);
930 if (target && !REG_P (target))
931 target = NULL_RTX;
933 /* *_widen_optab needs to determine operand mode, make sure at least
934 one operand has non-VOID mode. */
935 if (GET_MODE (op0_low) == VOIDmode && GET_MODE (op1_low) == VOIDmode)
936 op0_low = force_reg (word_mode, op0_low);
938 if (umulp)
939 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
940 target, 1, OPTAB_DIRECT);
941 else
942 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
943 target, 1, OPTAB_DIRECT);
945 if (!product)
946 return NULL_RTX;
948 product_high = operand_subword (product, high, 1, mode);
949 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
950 NULL_RTX, 0, OPTAB_DIRECT);
951 emit_move_insn (product_high, adjust);
952 return product;
955 /* Subroutine of expand_binop. Optimize unsigned double-word OP0 % OP1 for
956 constant OP1. If for some bit in [BITS_PER_WORD / 2, BITS_PER_WORD] range
957 (prefer higher bits) ((1w << bit) % OP1) == 1, then the modulo can be
958 computed in word-mode as ((OP0 & (bit - 1)) + ((OP0 >> bit) & (bit - 1))
959 + (OP0 >> (2 * bit))) % OP1. Whether we need to sum 2, 3 or 4 values
960 depends on the bit value, if 2, then carry from the addition needs to be
961 added too, i.e. like:
962 sum += __builtin_add_overflow (low, high, &sum)
964 Optimize signed double-word OP0 % OP1 similarly, just apply some correction
965 factor to the sum before doing unsigned remainder, in the form of
966 sum += (((signed) OP0 >> (2 * BITS_PER_WORD - 1)) & const);
967 then perform unsigned
968 remainder = sum % OP1;
969 and finally
970 remainder += ((signed) OP0 >> (2 * BITS_PER_WORD - 1)) & (1 - OP1); */
972 static rtx
973 expand_doubleword_mod (machine_mode mode, rtx op0, rtx op1, bool unsignedp)
975 if (INTVAL (op1) <= 1 || (INTVAL (op1) & 1) == 0)
976 return NULL_RTX;
978 rtx_insn *last = get_last_insn ();
979 for (int bit = BITS_PER_WORD; bit >= BITS_PER_WORD / 2; bit--)
981 wide_int w = wi::shifted_mask (bit, 1, false, 2 * BITS_PER_WORD);
982 if (wi::ne_p (wi::umod_trunc (w, INTVAL (op1)), 1))
983 continue;
984 rtx sum = NULL_RTX, mask = NULL_RTX;
985 if (bit == BITS_PER_WORD)
987 /* For signed modulo we need to add correction to the sum
988 and that might again overflow. */
989 if (!unsignedp)
990 continue;
991 if (optab_handler (uaddv4_optab, word_mode) == CODE_FOR_nothing)
992 continue;
993 tree wtype = lang_hooks.types.type_for_mode (word_mode, 1);
994 if (wtype == NULL_TREE)
995 continue;
996 tree ctype = build_complex_type (wtype);
997 if (TYPE_MODE (ctype) != GET_MODE_COMPLEX_MODE (word_mode))
998 continue;
999 machine_mode cmode = TYPE_MODE (ctype);
1000 rtx op00 = operand_subword_force (op0, 0, mode);
1001 rtx op01 = operand_subword_force (op0, 1, mode);
1002 rtx cres = gen_rtx_CONCAT (cmode, gen_reg_rtx (word_mode),
1003 gen_reg_rtx (word_mode));
1004 tree lhs = make_tree (ctype, cres);
1005 tree arg0 = make_tree (wtype, op00);
1006 tree arg1 = make_tree (wtype, op01);
1007 expand_addsub_overflow (UNKNOWN_LOCATION, PLUS_EXPR, lhs, arg0,
1008 arg1, true, true, true, false, NULL);
1009 sum = expand_simple_binop (word_mode, PLUS, XEXP (cres, 0),
1010 XEXP (cres, 1), NULL_RTX, 1,
1011 OPTAB_DIRECT);
1012 if (sum == NULL_RTX)
1013 return NULL_RTX;
1015 else
1017 /* Code below uses GEN_INT, so we need the masks to be representable
1018 in HOST_WIDE_INTs. */
1019 if (bit >= HOST_BITS_PER_WIDE_INT)
1020 continue;
1021 /* If op0 is e.g. -1 or -2 unsigned, then the 2 additions might
1022 overflow. Consider 64-bit -1ULL for word size 32, if we add
1023 0x7fffffffU + 0x7fffffffU + 3U, it wraps around to 1. */
1024 if (bit == BITS_PER_WORD - 1)
1025 continue;
1027 int count = (2 * BITS_PER_WORD + bit - 1) / bit;
1028 rtx sum_corr = NULL_RTX;
1030 if (!unsignedp)
1032 /* For signed modulo, compute it as unsigned modulo of
1033 sum with a correction added to it if OP0 is negative,
1034 such that the result can be computed as unsigned
1035 remainder + ((OP1 >> (2 * BITS_PER_WORD - 1)) & (1 - OP1). */
1036 w = wi::min_value (2 * BITS_PER_WORD, SIGNED);
1037 wide_int wmod1 = wi::umod_trunc (w, INTVAL (op1));
1038 wide_int wmod2 = wi::smod_trunc (w, INTVAL (op1));
1039 /* wmod2 == -wmod1. */
1040 wmod2 = wmod2 + (INTVAL (op1) - 1);
1041 if (wi::ne_p (wmod1, wmod2))
1043 wide_int wcorr = wmod2 - wmod1;
1044 if (wi::neg_p (w))
1045 wcorr = wcorr + INTVAL (op1);
1046 /* Now verify if the count sums can't overflow, and punt
1047 if they could. */
1048 w = wi::mask (bit, false, 2 * BITS_PER_WORD);
1049 w = w * (count - 1);
1050 w = w + wi::mask (2 * BITS_PER_WORD - (count - 1) * bit,
1051 false, 2 * BITS_PER_WORD);
1052 w = w + wcorr;
1053 w = wi::lrshift (w, BITS_PER_WORD);
1054 if (wi::ne_p (w, 0))
1055 continue;
1057 mask = operand_subword_force (op0, WORDS_BIG_ENDIAN ? 0 : 1,
1058 mode);
1059 mask = expand_simple_binop (word_mode, ASHIFTRT, mask,
1060 GEN_INT (BITS_PER_WORD - 1),
1061 NULL_RTX, 0, OPTAB_DIRECT);
1062 if (mask == NULL_RTX)
1063 return NULL_RTX;
1064 sum_corr = immed_wide_int_const (wcorr, word_mode);
1065 sum_corr = expand_simple_binop (word_mode, AND, mask,
1066 sum_corr, NULL_RTX, 1,
1067 OPTAB_DIRECT);
1068 if (sum_corr == NULL_RTX)
1069 return NULL_RTX;
1073 for (int i = 0; i < count; i++)
1075 rtx v = op0;
1076 if (i)
1077 v = expand_simple_binop (mode, LSHIFTRT, v, GEN_INT (i * bit),
1078 NULL_RTX, 1, OPTAB_DIRECT);
1079 if (v == NULL_RTX)
1080 return NULL_RTX;
1081 v = lowpart_subreg (word_mode, v, mode);
1082 if (v == NULL_RTX)
1083 return NULL_RTX;
1084 if (i != count - 1)
1085 v = expand_simple_binop (word_mode, AND, v,
1086 GEN_INT ((HOST_WIDE_INT_1U << bit)
1087 - 1), NULL_RTX, 1,
1088 OPTAB_DIRECT);
1089 if (v == NULL_RTX)
1090 return NULL_RTX;
1091 if (sum == NULL_RTX)
1092 sum = v;
1093 else
1094 sum = expand_simple_binop (word_mode, PLUS, sum, v, NULL_RTX,
1095 1, OPTAB_DIRECT);
1096 if (sum == NULL_RTX)
1097 return NULL_RTX;
1099 if (sum_corr)
1101 sum = expand_simple_binop (word_mode, PLUS, sum, sum_corr,
1102 NULL_RTX, 1, OPTAB_DIRECT);
1103 if (sum == NULL_RTX)
1104 return NULL_RTX;
1107 rtx remainder = expand_divmod (1, TRUNC_MOD_EXPR, word_mode, sum,
1108 gen_int_mode (INTVAL (op1), word_mode),
1109 NULL_RTX, 1, OPTAB_DIRECT);
1110 if (remainder == NULL_RTX)
1111 return NULL_RTX;
1113 if (!unsignedp)
1115 if (mask == NULL_RTX)
1117 mask = operand_subword_force (op0, WORDS_BIG_ENDIAN ? 0 : 1,
1118 mode);
1119 mask = expand_simple_binop (word_mode, ASHIFTRT, mask,
1120 GEN_INT (BITS_PER_WORD - 1),
1121 NULL_RTX, 0, OPTAB_DIRECT);
1122 if (mask == NULL_RTX)
1123 return NULL_RTX;
1125 mask = expand_simple_binop (word_mode, AND, mask,
1126 gen_int_mode (1 - INTVAL (op1),
1127 word_mode),
1128 NULL_RTX, 1, OPTAB_DIRECT);
1129 if (mask == NULL_RTX)
1130 return NULL_RTX;
1131 remainder = expand_simple_binop (word_mode, PLUS, remainder,
1132 mask, NULL_RTX, 1, OPTAB_DIRECT);
1133 if (remainder == NULL_RTX)
1134 return NULL_RTX;
1137 remainder = convert_modes (mode, word_mode, remainder, unsignedp);
1138 /* Punt if we need any library calls. */
1139 if (last)
1140 last = NEXT_INSN (last);
1141 else
1142 last = get_insns ();
1143 for (; last; last = NEXT_INSN (last))
1144 if (CALL_P (last))
1145 return NULL_RTX;
1146 return remainder;
1148 return NULL_RTX;
1151 /* Similarly to the above function, but compute both quotient and remainder.
1152 Quotient can be computed from the remainder as:
1153 rem = op0 % op1; // Handled using expand_doubleword_mod
1154 quot = (op0 - rem) * inv; // inv is multiplicative inverse of op1 modulo
1155 // 2 * BITS_PER_WORD
1157 We can also handle cases where op1 is a multiple of power of two constant
1158 and constant handled by expand_doubleword_mod.
1159 op11 = 1 << __builtin_ctz (op1);
1160 op12 = op1 / op11;
1161 rem1 = op0 % op12; // Handled using expand_doubleword_mod
1162 quot1 = (op0 - rem1) * inv; // inv is multiplicative inverse of op12 modulo
1163 // 2 * BITS_PER_WORD
1164 rem = (quot1 % op11) * op12 + rem1;
1165 quot = quot1 / op11; */
1168 expand_doubleword_divmod (machine_mode mode, rtx op0, rtx op1, rtx *rem,
1169 bool unsignedp)
1171 *rem = NULL_RTX;
1173 /* Negative dividend should have been optimized into positive,
1174 similarly modulo by 1 and modulo by power of two is optimized
1175 differently too. */
1176 if (INTVAL (op1) <= 1 || pow2p_hwi (INTVAL (op1)))
1177 return NULL_RTX;
1179 rtx op11 = const1_rtx;
1180 rtx op12 = op1;
1181 if ((INTVAL (op1) & 1) == 0)
1183 int bit = ctz_hwi (INTVAL (op1));
1184 op11 = GEN_INT (HOST_WIDE_INT_1 << bit);
1185 op12 = GEN_INT (INTVAL (op1) >> bit);
1188 rtx rem1 = expand_doubleword_mod (mode, op0, op12, unsignedp);
1189 if (rem1 == NULL_RTX)
1190 return NULL_RTX;
1192 int prec = 2 * BITS_PER_WORD;
1193 wide_int a = wide_int::from (INTVAL (op12), prec + 1, UNSIGNED);
1194 wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
1195 wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
1196 rtx inv = immed_wide_int_const (m, mode);
1198 rtx_insn *last = get_last_insn ();
1199 rtx quot1 = expand_simple_binop (mode, MINUS, op0, rem1,
1200 NULL_RTX, unsignedp, OPTAB_DIRECT);
1201 if (quot1 == NULL_RTX)
1202 return NULL_RTX;
1204 quot1 = expand_simple_binop (mode, MULT, quot1, inv,
1205 NULL_RTX, unsignedp, OPTAB_DIRECT);
1206 if (quot1 == NULL_RTX)
1207 return NULL_RTX;
1209 if (op11 != const1_rtx)
1211 rtx rem2 = expand_divmod (1, TRUNC_MOD_EXPR, mode, quot1, op11,
1212 NULL_RTX, unsignedp, OPTAB_DIRECT);
1213 if (rem2 == NULL_RTX)
1214 return NULL_RTX;
1216 rem2 = expand_simple_binop (mode, MULT, rem2, op12, NULL_RTX,
1217 unsignedp, OPTAB_DIRECT);
1218 if (rem2 == NULL_RTX)
1219 return NULL_RTX;
1221 rem2 = expand_simple_binop (mode, PLUS, rem2, rem1, NULL_RTX,
1222 unsignedp, OPTAB_DIRECT);
1223 if (rem2 == NULL_RTX)
1224 return NULL_RTX;
1226 rtx quot2 = expand_divmod (0, TRUNC_DIV_EXPR, mode, quot1, op11,
1227 NULL_RTX, unsignedp, OPTAB_DIRECT);
1228 if (quot2 == NULL_RTX)
1229 return NULL_RTX;
1231 rem1 = rem2;
1232 quot1 = quot2;
1235 /* Punt if we need any library calls. */
1236 if (last)
1237 last = NEXT_INSN (last);
1238 else
1239 last = get_insns ();
1240 for (; last; last = NEXT_INSN (last))
1241 if (CALL_P (last))
1242 return NULL_RTX;
1244 *rem = rem1;
1245 return quot1;
1248 /* Wrapper around expand_binop which takes an rtx code to specify
1249 the operation to perform, not an optab pointer. All other
1250 arguments are the same. */
1252 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
1253 rtx op1, rtx target, int unsignedp,
1254 enum optab_methods methods)
1256 optab binop = code_to_optab (code);
1257 gcc_assert (binop);
1259 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1262 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1263 binop. Order them according to commutative_operand_precedence and, if
1264 possible, try to put TARGET or a pseudo first. */
1265 static bool
1266 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1268 int op0_prec = commutative_operand_precedence (op0);
1269 int op1_prec = commutative_operand_precedence (op1);
1271 if (op0_prec < op1_prec)
1272 return true;
1274 if (op0_prec > op1_prec)
1275 return false;
1277 /* With equal precedence, both orders are ok, but it is better if the
1278 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1279 if (target == 0 || REG_P (target))
1280 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1281 else
1282 return rtx_equal_p (op1, target);
1285 /* Return true if BINOPTAB implements a shift operation. */
1287 static bool
1288 shift_optab_p (optab binoptab)
1290 switch (optab_to_code (binoptab))
1292 case ASHIFT:
1293 case SS_ASHIFT:
1294 case US_ASHIFT:
1295 case ASHIFTRT:
1296 case LSHIFTRT:
1297 case ROTATE:
1298 case ROTATERT:
1299 return true;
1301 default:
1302 return false;
1306 /* Return true if BINOPTAB implements a commutative binary operation. */
1308 static bool
1309 commutative_optab_p (optab binoptab)
1311 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1312 || binoptab == smul_widen_optab
1313 || binoptab == umul_widen_optab
1314 || binoptab == smul_highpart_optab
1315 || binoptab == umul_highpart_optab);
1318 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1319 optimizing, and if the operand is a constant that costs more than
1320 1 instruction, force the constant into a register and return that
1321 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1323 static rtx
1324 avoid_expensive_constant (machine_mode mode, optab binoptab,
1325 int opn, rtx x, bool unsignedp)
1327 bool speed = optimize_insn_for_speed_p ();
1329 if (mode != VOIDmode
1330 && optimize
1331 && CONSTANT_P (x)
1332 && (rtx_cost (x, mode, optab_to_code (binoptab), opn, speed)
1333 > set_src_cost (x, mode, speed)))
1335 if (CONST_INT_P (x))
1337 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1338 if (intval != INTVAL (x))
1339 x = GEN_INT (intval);
1341 else
1342 x = convert_modes (mode, VOIDmode, x, unsignedp);
1343 x = force_reg (mode, x);
1345 return x;
1348 /* Helper function for expand_binop: handle the case where there
1349 is an insn ICODE that directly implements the indicated operation.
1350 Returns null if this is not possible. */
1351 static rtx
1352 expand_binop_directly (enum insn_code icode, machine_mode mode, optab binoptab,
1353 rtx op0, rtx op1,
1354 rtx target, int unsignedp, enum optab_methods methods,
1355 rtx_insn *last)
1357 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1358 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1359 machine_mode mode0, mode1, tmp_mode;
1360 class expand_operand ops[3];
1361 bool commutative_p;
1362 rtx_insn *pat;
1363 rtx xop0 = op0, xop1 = op1;
1364 bool canonicalize_op1 = false;
1366 /* If it is a commutative operator and the modes would match
1367 if we would swap the operands, we can save the conversions. */
1368 commutative_p = commutative_optab_p (binoptab);
1369 if (commutative_p
1370 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1371 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode0)
1372 std::swap (xop0, xop1);
1374 /* If we are optimizing, force expensive constants into a register. */
1375 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1376 if (!shift_optab_p (binoptab))
1377 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1378 else
1379 /* Shifts and rotates often use a different mode for op1 from op0;
1380 for VOIDmode constants we don't know the mode, so force it
1381 to be canonicalized using convert_modes. */
1382 canonicalize_op1 = true;
1384 /* In case the insn wants input operands in modes different from
1385 those of the actual operands, convert the operands. It would
1386 seem that we don't need to convert CONST_INTs, but we do, so
1387 that they're properly zero-extended, sign-extended or truncated
1388 for their mode. */
1390 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1391 if (xmode0 != VOIDmode && xmode0 != mode0)
1393 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1394 mode0 = xmode0;
1397 mode1 = ((GET_MODE (xop1) != VOIDmode || canonicalize_op1)
1398 ? GET_MODE (xop1) : mode);
1399 if (xmode1 != VOIDmode && xmode1 != mode1)
1401 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1402 mode1 = xmode1;
1405 /* If operation is commutative,
1406 try to make the first operand a register.
1407 Even better, try to make it the same as the target.
1408 Also try to make the last operand a constant. */
1409 if (commutative_p
1410 && swap_commutative_operands_with_target (target, xop0, xop1))
1411 std::swap (xop0, xop1);
1413 /* Now, if insn's predicates don't allow our operands, put them into
1414 pseudo regs. */
1416 if (binoptab == vec_pack_trunc_optab
1417 || binoptab == vec_pack_usat_optab
1418 || binoptab == vec_pack_ssat_optab
1419 || binoptab == vec_pack_ufix_trunc_optab
1420 || binoptab == vec_pack_sfix_trunc_optab
1421 || binoptab == vec_packu_float_optab
1422 || binoptab == vec_packs_float_optab)
1424 /* The mode of the result is different then the mode of the
1425 arguments. */
1426 tmp_mode = insn_data[(int) icode].operand[0].mode;
1427 if (VECTOR_MODE_P (mode)
1428 && maybe_ne (GET_MODE_NUNITS (tmp_mode), 2 * GET_MODE_NUNITS (mode)))
1430 delete_insns_since (last);
1431 return NULL_RTX;
1434 else
1435 tmp_mode = mode;
1437 create_output_operand (&ops[0], target, tmp_mode);
1438 create_input_operand (&ops[1], xop0, mode0);
1439 create_input_operand (&ops[2], xop1, mode1);
1440 pat = maybe_gen_insn (icode, 3, ops);
1441 if (pat)
1443 /* If PAT is composed of more than one insn, try to add an appropriate
1444 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1445 operand, call expand_binop again, this time without a target. */
1446 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1447 && ! add_equal_note (pat, ops[0].value,
1448 optab_to_code (binoptab),
1449 ops[1].value, ops[2].value, mode0))
1451 delete_insns_since (last);
1452 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1453 unsignedp, methods);
1456 emit_insn (pat);
1457 return ops[0].value;
1459 delete_insns_since (last);
1460 return NULL_RTX;
1463 /* Generate code to perform an operation specified by BINOPTAB
1464 on operands OP0 and OP1, with result having machine-mode MODE.
1466 UNSIGNEDP is for the case where we have to widen the operands
1467 to perform the operation. It says to use zero-extension.
1469 If TARGET is nonzero, the value
1470 is generated there, if it is convenient to do so.
1471 In all cases an rtx is returned for the locus of the value;
1472 this may or may not be TARGET. */
1475 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1476 rtx target, int unsignedp, enum optab_methods methods)
1478 enum optab_methods next_methods
1479 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1480 ? OPTAB_WIDEN : methods);
1481 enum mode_class mclass;
1482 enum insn_code icode;
1483 machine_mode wider_mode;
1484 scalar_int_mode int_mode;
1485 rtx libfunc;
1486 rtx temp;
1487 rtx_insn *entry_last = get_last_insn ();
1488 rtx_insn *last;
1490 mclass = GET_MODE_CLASS (mode);
1492 /* If subtracting an integer constant, convert this into an addition of
1493 the negated constant. */
1495 if (binoptab == sub_optab && CONST_INT_P (op1))
1497 op1 = negate_rtx (mode, op1);
1498 binoptab = add_optab;
1500 /* For shifts, constant invalid op1 might be expanded from different
1501 mode than MODE. As those are invalid, force them to a register
1502 to avoid further problems during expansion. */
1503 else if (CONST_INT_P (op1)
1504 && shift_optab_p (binoptab)
1505 && UINTVAL (op1) >= GET_MODE_BITSIZE (GET_MODE_INNER (mode)))
1507 op1 = gen_int_mode (INTVAL (op1), GET_MODE_INNER (mode));
1508 op1 = force_reg (GET_MODE_INNER (mode), op1);
1511 /* Record where to delete back to if we backtrack. */
1512 last = get_last_insn ();
1514 /* If we can do it with a three-operand insn, do so. */
1516 if (methods != OPTAB_MUST_WIDEN)
1518 if (convert_optab_p (binoptab))
1520 machine_mode from_mode = widened_mode (mode, op0, op1);
1521 icode = find_widening_optab_handler (binoptab, mode, from_mode);
1523 else
1524 icode = optab_handler (binoptab, mode);
1525 if (icode != CODE_FOR_nothing)
1527 temp = expand_binop_directly (icode, mode, binoptab, op0, op1,
1528 target, unsignedp, methods, last);
1529 if (temp)
1530 return temp;
1534 /* If we were trying to rotate, and that didn't work, try rotating
1535 the other direction before falling back to shifts and bitwise-or. */
1536 if (((binoptab == rotl_optab
1537 && (icode = optab_handler (rotr_optab, mode)) != CODE_FOR_nothing)
1538 || (binoptab == rotr_optab
1539 && (icode = optab_handler (rotl_optab, mode)) != CODE_FOR_nothing))
1540 && is_int_mode (mode, &int_mode))
1542 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1543 rtx newop1;
1544 unsigned int bits = GET_MODE_PRECISION (int_mode);
1546 if (CONST_INT_P (op1))
1547 newop1 = gen_int_shift_amount (int_mode, bits - INTVAL (op1));
1548 else if (targetm.shift_truncation_mask (int_mode) == bits - 1)
1549 newop1 = negate_rtx (GET_MODE (op1), op1);
1550 else
1551 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1552 gen_int_mode (bits, GET_MODE (op1)), op1,
1553 NULL_RTX, unsignedp, OPTAB_DIRECT);
1555 temp = expand_binop_directly (icode, int_mode, otheroptab, op0, newop1,
1556 target, unsignedp, methods, last);
1557 if (temp)
1558 return temp;
1561 /* If this is a multiply, see if we can do a widening operation that
1562 takes operands of this mode and makes a wider mode. */
1564 if (binoptab == smul_optab
1565 && GET_MODE_2XWIDER_MODE (mode).exists (&wider_mode)
1566 && (convert_optab_handler ((unsignedp
1567 ? umul_widen_optab
1568 : smul_widen_optab),
1569 wider_mode, mode) != CODE_FOR_nothing))
1571 /* *_widen_optab needs to determine operand mode, make sure at least
1572 one operand has non-VOID mode. */
1573 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
1574 op0 = force_reg (mode, op0);
1575 temp = expand_binop (wider_mode,
1576 unsignedp ? umul_widen_optab : smul_widen_optab,
1577 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1579 if (temp != 0)
1581 if (GET_MODE_CLASS (mode) == MODE_INT
1582 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1583 return gen_lowpart (mode, temp);
1584 else
1585 return convert_to_mode (mode, temp, unsignedp);
1589 /* If this is a vector shift by a scalar, see if we can do a vector
1590 shift by a vector. If so, broadcast the scalar into a vector. */
1591 if (mclass == MODE_VECTOR_INT)
1593 optab otheroptab = unknown_optab;
1595 if (binoptab == ashl_optab)
1596 otheroptab = vashl_optab;
1597 else if (binoptab == ashr_optab)
1598 otheroptab = vashr_optab;
1599 else if (binoptab == lshr_optab)
1600 otheroptab = vlshr_optab;
1601 else if (binoptab == rotl_optab)
1602 otheroptab = vrotl_optab;
1603 else if (binoptab == rotr_optab)
1604 otheroptab = vrotr_optab;
1606 if (otheroptab
1607 && (icode = optab_handler (otheroptab, mode)) != CODE_FOR_nothing)
1609 /* The scalar may have been extended to be too wide. Truncate
1610 it back to the proper size to fit in the broadcast vector. */
1611 scalar_mode inner_mode = GET_MODE_INNER (mode);
1612 if (!CONST_INT_P (op1)
1613 && (GET_MODE_BITSIZE (as_a <scalar_int_mode> (GET_MODE (op1)))
1614 > GET_MODE_BITSIZE (inner_mode)))
1615 op1 = force_reg (inner_mode,
1616 simplify_gen_unary (TRUNCATE, inner_mode, op1,
1617 GET_MODE (op1)));
1618 rtx vop1 = expand_vector_broadcast (mode, op1);
1619 if (vop1)
1621 temp = expand_binop_directly (icode, mode, otheroptab, op0, vop1,
1622 target, unsignedp, methods, last);
1623 if (temp)
1624 return temp;
1629 /* Look for a wider mode of the same class for which we think we
1630 can open-code the operation. Check for a widening multiply at the
1631 wider mode as well. */
1633 if (CLASS_HAS_WIDER_MODES_P (mclass)
1634 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1635 FOR_EACH_WIDER_MODE (wider_mode, mode)
1637 machine_mode next_mode;
1638 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1639 || (binoptab == smul_optab
1640 && GET_MODE_WIDER_MODE (wider_mode).exists (&next_mode)
1641 && (find_widening_optab_handler ((unsignedp
1642 ? umul_widen_optab
1643 : smul_widen_optab),
1644 next_mode, mode)
1645 != CODE_FOR_nothing)))
1647 rtx xop0 = op0, xop1 = op1;
1648 int no_extend = 0;
1650 /* For certain integer operations, we need not actually extend
1651 the narrow operands, as long as we will truncate
1652 the results to the same narrowness. */
1654 if ((binoptab == ior_optab || binoptab == and_optab
1655 || binoptab == xor_optab
1656 || binoptab == add_optab || binoptab == sub_optab
1657 || binoptab == smul_optab || binoptab == ashl_optab)
1658 && mclass == MODE_INT)
1660 no_extend = 1;
1661 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1662 xop0, unsignedp);
1663 if (binoptab != ashl_optab)
1664 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1665 xop1, unsignedp);
1668 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1670 /* The second operand of a shift must always be extended. */
1671 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1672 no_extend && binoptab != ashl_optab);
1674 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1675 unsignedp, OPTAB_DIRECT);
1676 if (temp)
1678 if (mclass != MODE_INT
1679 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1681 if (target == 0)
1682 target = gen_reg_rtx (mode);
1683 convert_move (target, temp, 0);
1684 return target;
1686 else
1687 return gen_lowpart (mode, temp);
1689 else
1690 delete_insns_since (last);
1694 /* If operation is commutative,
1695 try to make the first operand a register.
1696 Even better, try to make it the same as the target.
1697 Also try to make the last operand a constant. */
1698 if (commutative_optab_p (binoptab)
1699 && swap_commutative_operands_with_target (target, op0, op1))
1700 std::swap (op0, op1);
1702 /* These can be done a word at a time. */
1703 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1704 && is_int_mode (mode, &int_mode)
1705 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
1706 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1708 int i;
1709 rtx_insn *insns;
1711 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1712 won't be accurate, so use a new target. */
1713 if (target == 0
1714 || target == op0
1715 || target == op1
1716 || reg_overlap_mentioned_p (target, op0)
1717 || reg_overlap_mentioned_p (target, op1)
1718 || !valid_multiword_target_p (target))
1719 target = gen_reg_rtx (int_mode);
1721 start_sequence ();
1723 /* Do the actual arithmetic. */
1724 machine_mode op0_mode = GET_MODE (op0);
1725 machine_mode op1_mode = GET_MODE (op1);
1726 if (op0_mode == VOIDmode)
1727 op0_mode = int_mode;
1728 if (op1_mode == VOIDmode)
1729 op1_mode = int_mode;
1730 for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
1732 rtx target_piece = operand_subword (target, i, 1, int_mode);
1733 rtx x = expand_binop (word_mode, binoptab,
1734 operand_subword_force (op0, i, op0_mode),
1735 operand_subword_force (op1, i, op1_mode),
1736 target_piece, unsignedp, next_methods);
1738 if (x == 0)
1739 break;
1741 if (target_piece != x)
1742 emit_move_insn (target_piece, x);
1745 insns = get_insns ();
1746 end_sequence ();
1748 if (i == GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD)
1750 emit_insn (insns);
1751 return target;
1755 /* Synthesize double word shifts from single word shifts. */
1756 if ((binoptab == lshr_optab || binoptab == ashl_optab
1757 || binoptab == ashr_optab)
1758 && is_int_mode (mode, &int_mode)
1759 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1760 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
1761 && GET_MODE_PRECISION (int_mode) == GET_MODE_BITSIZE (int_mode)
1762 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1763 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1764 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1766 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1767 scalar_int_mode op1_mode;
1769 double_shift_mask = targetm.shift_truncation_mask (int_mode);
1770 shift_mask = targetm.shift_truncation_mask (word_mode);
1771 op1_mode = (GET_MODE (op1) != VOIDmode
1772 ? as_a <scalar_int_mode> (GET_MODE (op1))
1773 : word_mode);
1775 /* Apply the truncation to constant shifts. */
1776 if (double_shift_mask > 0 && CONST_INT_P (op1))
1777 op1 = gen_int_mode (INTVAL (op1) & double_shift_mask, op1_mode);
1779 if (op1 == CONST0_RTX (op1_mode))
1780 return op0;
1782 /* Make sure that this is a combination that expand_doubleword_shift
1783 can handle. See the comments there for details. */
1784 if (double_shift_mask == 0
1785 || (shift_mask == BITS_PER_WORD - 1
1786 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1788 rtx_insn *insns;
1789 rtx into_target, outof_target;
1790 rtx into_input, outof_input;
1791 int left_shift, outof_word;
1793 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1794 won't be accurate, so use a new target. */
1795 if (target == 0
1796 || target == op0
1797 || target == op1
1798 || reg_overlap_mentioned_p (target, op0)
1799 || reg_overlap_mentioned_p (target, op1)
1800 || !valid_multiword_target_p (target))
1801 target = gen_reg_rtx (int_mode);
1803 start_sequence ();
1805 /* OUTOF_* is the word we are shifting bits away from, and
1806 INTO_* is the word that we are shifting bits towards, thus
1807 they differ depending on the direction of the shift and
1808 WORDS_BIG_ENDIAN. */
1810 left_shift = binoptab == ashl_optab;
1811 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1813 outof_target = operand_subword (target, outof_word, 1, int_mode);
1814 into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
1816 outof_input = operand_subword_force (op0, outof_word, int_mode);
1817 into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
1819 if (expand_doubleword_shift (op1_mode, binoptab,
1820 outof_input, into_input, op1,
1821 outof_target, into_target,
1822 unsignedp, next_methods, shift_mask))
1824 insns = get_insns ();
1825 end_sequence ();
1827 emit_insn (insns);
1828 return target;
1830 end_sequence ();
1834 /* Synthesize double word rotates from single word shifts. */
1835 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1836 && is_int_mode (mode, &int_mode)
1837 && CONST_INT_P (op1)
1838 && GET_MODE_PRECISION (int_mode) == 2 * BITS_PER_WORD
1839 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1840 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1842 rtx_insn *insns;
1843 rtx into_target, outof_target;
1844 rtx into_input, outof_input;
1845 rtx inter;
1846 int shift_count, left_shift, outof_word;
1848 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1849 won't be accurate, so use a new target. Do this also if target is not
1850 a REG, first because having a register instead may open optimization
1851 opportunities, and second because if target and op0 happen to be MEMs
1852 designating the same location, we would risk clobbering it too early
1853 in the code sequence we generate below. */
1854 if (target == 0
1855 || target == op0
1856 || target == op1
1857 || !REG_P (target)
1858 || reg_overlap_mentioned_p (target, op0)
1859 || reg_overlap_mentioned_p (target, op1)
1860 || !valid_multiword_target_p (target))
1861 target = gen_reg_rtx (int_mode);
1863 start_sequence ();
1865 shift_count = INTVAL (op1);
1867 /* OUTOF_* is the word we are shifting bits away from, and
1868 INTO_* is the word that we are shifting bits towards, thus
1869 they differ depending on the direction of the shift and
1870 WORDS_BIG_ENDIAN. */
1872 left_shift = (binoptab == rotl_optab);
1873 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1875 outof_target = operand_subword (target, outof_word, 1, int_mode);
1876 into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
1878 outof_input = operand_subword_force (op0, outof_word, int_mode);
1879 into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
1881 if (shift_count == BITS_PER_WORD)
1883 /* This is just a word swap. */
1884 emit_move_insn (outof_target, into_input);
1885 emit_move_insn (into_target, outof_input);
1886 inter = const0_rtx;
1888 else
1890 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1891 HOST_WIDE_INT first_shift_count, second_shift_count;
1892 optab reverse_unsigned_shift, unsigned_shift;
1894 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1895 ? lshr_optab : ashl_optab);
1897 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1898 ? ashl_optab : lshr_optab);
1900 if (shift_count > BITS_PER_WORD)
1902 first_shift_count = shift_count - BITS_PER_WORD;
1903 second_shift_count = 2 * BITS_PER_WORD - shift_count;
1905 else
1907 first_shift_count = BITS_PER_WORD - shift_count;
1908 second_shift_count = shift_count;
1910 rtx first_shift_count_rtx
1911 = gen_int_shift_amount (word_mode, first_shift_count);
1912 rtx second_shift_count_rtx
1913 = gen_int_shift_amount (word_mode, second_shift_count);
1915 into_temp1 = expand_binop (word_mode, unsigned_shift,
1916 outof_input, first_shift_count_rtx,
1917 NULL_RTX, unsignedp, next_methods);
1918 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1919 into_input, second_shift_count_rtx,
1920 NULL_RTX, unsignedp, next_methods);
1922 if (into_temp1 != 0 && into_temp2 != 0)
1923 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1924 into_target, unsignedp, next_methods);
1925 else
1926 inter = 0;
1928 if (inter != 0 && inter != into_target)
1929 emit_move_insn (into_target, inter);
1931 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1932 into_input, first_shift_count_rtx,
1933 NULL_RTX, unsignedp, next_methods);
1934 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1935 outof_input, second_shift_count_rtx,
1936 NULL_RTX, unsignedp, next_methods);
1938 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1939 inter = expand_binop (word_mode, ior_optab,
1940 outof_temp1, outof_temp2,
1941 outof_target, unsignedp, next_methods);
1943 if (inter != 0 && inter != outof_target)
1944 emit_move_insn (outof_target, inter);
1947 insns = get_insns ();
1948 end_sequence ();
1950 if (inter != 0)
1952 emit_insn (insns);
1953 return target;
1957 /* These can be done a word at a time by propagating carries. */
1958 if ((binoptab == add_optab || binoptab == sub_optab)
1959 && is_int_mode (mode, &int_mode)
1960 && GET_MODE_SIZE (int_mode) >= 2 * UNITS_PER_WORD
1961 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1963 unsigned int i;
1964 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1965 const unsigned int nwords = GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD;
1966 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1967 rtx xop0, xop1, xtarget;
1969 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1970 value is one of those, use it. Otherwise, use 1 since it is the
1971 one easiest to get. */
1972 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1973 int normalizep = STORE_FLAG_VALUE;
1974 #else
1975 int normalizep = 1;
1976 #endif
1978 /* Prepare the operands. */
1979 xop0 = force_reg (int_mode, op0);
1980 xop1 = force_reg (int_mode, op1);
1982 xtarget = gen_reg_rtx (int_mode);
1984 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1985 target = xtarget;
1987 /* Indicate for flow that the entire target reg is being set. */
1988 if (REG_P (target))
1989 emit_clobber (xtarget);
1991 /* Do the actual arithmetic. */
1992 for (i = 0; i < nwords; i++)
1994 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1995 rtx target_piece = operand_subword (xtarget, index, 1, int_mode);
1996 rtx op0_piece = operand_subword_force (xop0, index, int_mode);
1997 rtx op1_piece = operand_subword_force (xop1, index, int_mode);
1998 rtx x;
2000 /* Main add/subtract of the input operands. */
2001 x = expand_binop (word_mode, binoptab,
2002 op0_piece, op1_piece,
2003 target_piece, unsignedp, next_methods);
2004 if (x == 0)
2005 break;
2007 if (i + 1 < nwords)
2009 /* Store carry from main add/subtract. */
2010 carry_out = gen_reg_rtx (word_mode);
2011 carry_out = emit_store_flag_force (carry_out,
2012 (binoptab == add_optab
2013 ? LT : GT),
2014 x, op0_piece,
2015 word_mode, 1, normalizep);
2018 if (i > 0)
2020 rtx newx;
2022 /* Add/subtract previous carry to main result. */
2023 newx = expand_binop (word_mode,
2024 normalizep == 1 ? binoptab : otheroptab,
2025 x, carry_in,
2026 NULL_RTX, 1, next_methods);
2028 if (i + 1 < nwords)
2030 /* Get out carry from adding/subtracting carry in. */
2031 rtx carry_tmp = gen_reg_rtx (word_mode);
2032 carry_tmp = emit_store_flag_force (carry_tmp,
2033 (binoptab == add_optab
2034 ? LT : GT),
2035 newx, x,
2036 word_mode, 1, normalizep);
2038 /* Logical-ior the two poss. carry together. */
2039 carry_out = expand_binop (word_mode, ior_optab,
2040 carry_out, carry_tmp,
2041 carry_out, 0, next_methods);
2042 if (carry_out == 0)
2043 break;
2045 emit_move_insn (target_piece, newx);
2047 else
2049 if (x != target_piece)
2050 emit_move_insn (target_piece, x);
2053 carry_in = carry_out;
2056 if (i == GET_MODE_BITSIZE (int_mode) / (unsigned) BITS_PER_WORD)
2058 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing
2059 || ! rtx_equal_p (target, xtarget))
2061 rtx_insn *temp = emit_move_insn (target, xtarget);
2063 set_dst_reg_note (temp, REG_EQUAL,
2064 gen_rtx_fmt_ee (optab_to_code (binoptab),
2065 int_mode, copy_rtx (xop0),
2066 copy_rtx (xop1)),
2067 target);
2069 else
2070 target = xtarget;
2072 return target;
2075 else
2076 delete_insns_since (last);
2079 /* Attempt to synthesize double word multiplies using a sequence of word
2080 mode multiplications. We first attempt to generate a sequence using a
2081 more efficient unsigned widening multiply, and if that fails we then
2082 try using a signed widening multiply. */
2084 if (binoptab == smul_optab
2085 && is_int_mode (mode, &int_mode)
2086 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2087 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2088 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2090 rtx product = NULL_RTX;
2091 if (convert_optab_handler (umul_widen_optab, int_mode, word_mode)
2092 != CODE_FOR_nothing)
2094 product = expand_doubleword_mult (int_mode, op0, op1, target,
2095 true, methods);
2096 if (!product)
2097 delete_insns_since (last);
2100 if (product == NULL_RTX
2101 && (convert_optab_handler (smul_widen_optab, int_mode, word_mode)
2102 != CODE_FOR_nothing))
2104 product = expand_doubleword_mult (int_mode, op0, op1, target,
2105 false, methods);
2106 if (!product)
2107 delete_insns_since (last);
2110 if (product != NULL_RTX)
2112 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
2114 rtx_insn *move = emit_move_insn (target ? target : product,
2115 product);
2116 set_dst_reg_note (move,
2117 REG_EQUAL,
2118 gen_rtx_fmt_ee (MULT, int_mode,
2119 copy_rtx (op0),
2120 copy_rtx (op1)),
2121 target ? target : product);
2123 return product;
2127 /* Attempt to synthetize double word modulo by constant divisor. */
2128 if ((binoptab == umod_optab
2129 || binoptab == smod_optab
2130 || binoptab == udiv_optab
2131 || binoptab == sdiv_optab)
2132 && optimize
2133 && CONST_INT_P (op1)
2134 && is_int_mode (mode, &int_mode)
2135 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2136 && optab_handler ((binoptab == umod_optab || binoptab == udiv_optab)
2137 ? udivmod_optab : sdivmod_optab,
2138 int_mode) == CODE_FOR_nothing
2139 && optab_handler (and_optab, word_mode) != CODE_FOR_nothing
2140 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing
2141 && optimize_insn_for_speed_p ())
2143 rtx res = NULL_RTX;
2144 if ((binoptab == umod_optab || binoptab == smod_optab)
2145 && (INTVAL (op1) & 1) == 0)
2146 res = expand_doubleword_mod (int_mode, op0, op1,
2147 binoptab == umod_optab);
2148 else
2150 rtx quot = expand_doubleword_divmod (int_mode, op0, op1, &res,
2151 binoptab == umod_optab
2152 || binoptab == udiv_optab);
2153 if (quot == NULL_RTX)
2154 res = NULL_RTX;
2155 else if (binoptab == udiv_optab || binoptab == sdiv_optab)
2156 res = quot;
2158 if (res != NULL_RTX)
2160 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
2162 rtx_insn *move = emit_move_insn (target ? target : res,
2163 res);
2164 set_dst_reg_note (move, REG_EQUAL,
2165 gen_rtx_fmt_ee (optab_to_code (binoptab),
2166 int_mode, copy_rtx (op0), op1),
2167 target ? target : res);
2169 return res;
2171 else
2172 delete_insns_since (last);
2175 /* It can't be open-coded in this mode.
2176 Use a library call if one is available and caller says that's ok. */
2178 libfunc = optab_libfunc (binoptab, mode);
2179 if (libfunc
2180 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2182 rtx_insn *insns;
2183 rtx op1x = op1;
2184 machine_mode op1_mode = mode;
2185 rtx value;
2187 start_sequence ();
2189 if (shift_optab_p (binoptab))
2191 op1_mode = targetm.libgcc_shift_count_mode ();
2192 /* Specify unsigned here,
2193 since negative shift counts are meaningless. */
2194 op1x = convert_to_mode (op1_mode, op1, 1);
2197 if (GET_MODE (op0) != VOIDmode
2198 && GET_MODE (op0) != mode)
2199 op0 = convert_to_mode (mode, op0, unsignedp);
2201 /* Pass 1 for NO_QUEUE so we don't lose any increments
2202 if the libcall is cse'd or moved. */
2203 value = emit_library_call_value (libfunc,
2204 NULL_RTX, LCT_CONST, mode,
2205 op0, mode, op1x, op1_mode);
2207 insns = get_insns ();
2208 end_sequence ();
2210 bool trapv = trapv_binoptab_p (binoptab);
2211 target = gen_reg_rtx (mode);
2212 emit_libcall_block_1 (insns, target, value,
2213 trapv ? NULL_RTX
2214 : gen_rtx_fmt_ee (optab_to_code (binoptab),
2215 mode, op0, op1), trapv);
2217 return target;
2220 delete_insns_since (last);
2222 /* It can't be done in this mode. Can we do it in a wider mode? */
2224 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2225 || methods == OPTAB_MUST_WIDEN))
2227 /* Caller says, don't even try. */
2228 delete_insns_since (entry_last);
2229 return 0;
2232 /* Compute the value of METHODS to pass to recursive calls.
2233 Don't allow widening to be tried recursively. */
2235 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2237 /* Look for a wider mode of the same class for which it appears we can do
2238 the operation. */
2240 if (CLASS_HAS_WIDER_MODES_P (mclass))
2242 /* This code doesn't make sense for conversion optabs, since we
2243 wouldn't then want to extend the operands to be the same size
2244 as the result. */
2245 gcc_assert (!convert_optab_p (binoptab));
2246 FOR_EACH_WIDER_MODE (wider_mode, mode)
2248 if (optab_handler (binoptab, wider_mode)
2249 || (methods == OPTAB_LIB
2250 && optab_libfunc (binoptab, wider_mode)))
2252 rtx xop0 = op0, xop1 = op1;
2253 int no_extend = 0;
2255 /* For certain integer operations, we need not actually extend
2256 the narrow operands, as long as we will truncate
2257 the results to the same narrowness. */
2259 if ((binoptab == ior_optab || binoptab == and_optab
2260 || binoptab == xor_optab
2261 || binoptab == add_optab || binoptab == sub_optab
2262 || binoptab == smul_optab || binoptab == ashl_optab)
2263 && mclass == MODE_INT)
2264 no_extend = 1;
2266 xop0 = widen_operand (xop0, wider_mode, mode,
2267 unsignedp, no_extend);
2269 /* The second operand of a shift must always be extended. */
2270 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2271 no_extend && binoptab != ashl_optab);
2273 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2274 unsignedp, methods);
2275 if (temp)
2277 if (mclass != MODE_INT
2278 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2280 if (target == 0)
2281 target = gen_reg_rtx (mode);
2282 convert_move (target, temp, 0);
2283 return target;
2285 else
2286 return gen_lowpart (mode, temp);
2288 else
2289 delete_insns_since (last);
2294 delete_insns_since (entry_last);
2295 return 0;
2298 /* Expand a binary operator which has both signed and unsigned forms.
2299 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2300 signed operations.
2302 If we widen unsigned operands, we may use a signed wider operation instead
2303 of an unsigned wider operation, since the result would be the same. */
2306 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
2307 rtx op0, rtx op1, rtx target, int unsignedp,
2308 enum optab_methods methods)
2310 rtx temp;
2311 optab direct_optab = unsignedp ? uoptab : soptab;
2312 bool save_enable;
2314 /* Do it without widening, if possible. */
2315 temp = expand_binop (mode, direct_optab, op0, op1, target,
2316 unsignedp, OPTAB_DIRECT);
2317 if (temp || methods == OPTAB_DIRECT)
2318 return temp;
2320 /* Try widening to a signed int. Disable any direct use of any
2321 signed insn in the current mode. */
2322 save_enable = swap_optab_enable (soptab, mode, false);
2324 temp = expand_binop (mode, soptab, op0, op1, target,
2325 unsignedp, OPTAB_WIDEN);
2327 /* For unsigned operands, try widening to an unsigned int. */
2328 if (!temp && unsignedp)
2329 temp = expand_binop (mode, uoptab, op0, op1, target,
2330 unsignedp, OPTAB_WIDEN);
2331 if (temp || methods == OPTAB_WIDEN)
2332 goto egress;
2334 /* Use the right width libcall if that exists. */
2335 temp = expand_binop (mode, direct_optab, op0, op1, target,
2336 unsignedp, OPTAB_LIB);
2337 if (temp || methods == OPTAB_LIB)
2338 goto egress;
2340 /* Must widen and use a libcall, use either signed or unsigned. */
2341 temp = expand_binop (mode, soptab, op0, op1, target,
2342 unsignedp, methods);
2343 if (!temp && unsignedp)
2344 temp = expand_binop (mode, uoptab, op0, op1, target,
2345 unsignedp, methods);
2347 egress:
2348 /* Undo the fiddling above. */
2349 if (save_enable)
2350 swap_optab_enable (soptab, mode, true);
2351 return temp;
2354 /* Generate code to perform an operation specified by UNOPPTAB
2355 on operand OP0, with two results to TARG0 and TARG1.
2356 We assume that the order of the operands for the instruction
2357 is TARG0, TARG1, OP0.
2359 Either TARG0 or TARG1 may be zero, but what that means is that
2360 the result is not actually wanted. We will generate it into
2361 a dummy pseudo-reg and discard it. They may not both be zero.
2363 Returns 1 if this operation can be performed; 0 if not. */
2366 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2367 int unsignedp)
2369 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2370 enum mode_class mclass;
2371 machine_mode wider_mode;
2372 rtx_insn *entry_last = get_last_insn ();
2373 rtx_insn *last;
2375 mclass = GET_MODE_CLASS (mode);
2377 if (!targ0)
2378 targ0 = gen_reg_rtx (mode);
2379 if (!targ1)
2380 targ1 = gen_reg_rtx (mode);
2382 /* Record where to go back to if we fail. */
2383 last = get_last_insn ();
2385 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2387 class expand_operand ops[3];
2388 enum insn_code icode = optab_handler (unoptab, mode);
2390 create_fixed_operand (&ops[0], targ0);
2391 create_fixed_operand (&ops[1], targ1);
2392 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2393 if (maybe_expand_insn (icode, 3, ops))
2394 return 1;
2397 /* It can't be done in this mode. Can we do it in a wider mode? */
2399 if (CLASS_HAS_WIDER_MODES_P (mclass))
2401 FOR_EACH_WIDER_MODE (wider_mode, mode)
2403 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2405 rtx t0 = gen_reg_rtx (wider_mode);
2406 rtx t1 = gen_reg_rtx (wider_mode);
2407 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2409 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2411 convert_move (targ0, t0, unsignedp);
2412 convert_move (targ1, t1, unsignedp);
2413 return 1;
2415 else
2416 delete_insns_since (last);
2421 delete_insns_since (entry_last);
2422 return 0;
2425 /* Generate code to perform an operation specified by BINOPTAB
2426 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2427 We assume that the order of the operands for the instruction
2428 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2429 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2431 Either TARG0 or TARG1 may be zero, but what that means is that
2432 the result is not actually wanted. We will generate it into
2433 a dummy pseudo-reg and discard it. They may not both be zero.
2435 Returns 1 if this operation can be performed; 0 if not. */
2438 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2439 int unsignedp)
2441 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2442 enum mode_class mclass;
2443 machine_mode wider_mode;
2444 rtx_insn *entry_last = get_last_insn ();
2445 rtx_insn *last;
2447 mclass = GET_MODE_CLASS (mode);
2449 if (!targ0)
2450 targ0 = gen_reg_rtx (mode);
2451 if (!targ1)
2452 targ1 = gen_reg_rtx (mode);
2454 /* Record where to go back to if we fail. */
2455 last = get_last_insn ();
2457 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2459 class expand_operand ops[4];
2460 enum insn_code icode = optab_handler (binoptab, mode);
2461 machine_mode mode0 = insn_data[icode].operand[1].mode;
2462 machine_mode mode1 = insn_data[icode].operand[2].mode;
2463 rtx xop0 = op0, xop1 = op1;
2465 /* If we are optimizing, force expensive constants into a register. */
2466 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2467 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2469 create_fixed_operand (&ops[0], targ0);
2470 create_convert_operand_from (&ops[1], xop0, mode, unsignedp);
2471 create_convert_operand_from (&ops[2], xop1, mode, unsignedp);
2472 create_fixed_operand (&ops[3], targ1);
2473 if (maybe_expand_insn (icode, 4, ops))
2474 return 1;
2475 delete_insns_since (last);
2478 /* It can't be done in this mode. Can we do it in a wider mode? */
2480 if (CLASS_HAS_WIDER_MODES_P (mclass))
2482 FOR_EACH_WIDER_MODE (wider_mode, mode)
2484 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2486 rtx t0 = gen_reg_rtx (wider_mode);
2487 rtx t1 = gen_reg_rtx (wider_mode);
2488 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2489 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2491 if (expand_twoval_binop (binoptab, cop0, cop1,
2492 t0, t1, unsignedp))
2494 convert_move (targ0, t0, unsignedp);
2495 convert_move (targ1, t1, unsignedp);
2496 return 1;
2498 else
2499 delete_insns_since (last);
2504 delete_insns_since (entry_last);
2505 return 0;
2508 /* Expand the two-valued library call indicated by BINOPTAB, but
2509 preserve only one of the values. If TARG0 is non-NULL, the first
2510 value is placed into TARG0; otherwise the second value is placed
2511 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2512 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2513 This routine assumes that the value returned by the library call is
2514 as if the return value was of an integral mode twice as wide as the
2515 mode of OP0. Returns 1 if the call was successful. */
2517 bool
2518 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2519 rtx targ0, rtx targ1, enum rtx_code code)
2521 machine_mode mode;
2522 machine_mode libval_mode;
2523 rtx libval;
2524 rtx_insn *insns;
2525 rtx libfunc;
2527 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2528 gcc_assert (!targ0 != !targ1);
2530 mode = GET_MODE (op0);
2531 libfunc = optab_libfunc (binoptab, mode);
2532 if (!libfunc)
2533 return false;
2535 /* The value returned by the library function will have twice as
2536 many bits as the nominal MODE. */
2537 libval_mode = smallest_int_mode_for_size (2 * GET_MODE_BITSIZE (mode));
2538 start_sequence ();
2539 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2540 libval_mode,
2541 op0, mode,
2542 op1, mode);
2543 /* Get the part of VAL containing the value that we want. */
2544 libval = simplify_gen_subreg (mode, libval, libval_mode,
2545 targ0 ? 0 : GET_MODE_SIZE (mode));
2546 insns = get_insns ();
2547 end_sequence ();
2548 /* Move the into the desired location. */
2549 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2550 gen_rtx_fmt_ee (code, mode, op0, op1));
2552 return true;
2556 /* Wrapper around expand_unop which takes an rtx code to specify
2557 the operation to perform, not an optab pointer. All other
2558 arguments are the same. */
2560 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2561 rtx target, int unsignedp)
2563 optab unop = code_to_optab (code);
2564 gcc_assert (unop);
2566 return expand_unop (mode, unop, op0, target, unsignedp);
2569 /* Try calculating
2570 (clz:narrow x)
2572 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2574 A similar operation can be used for clrsb. UNOPTAB says which operation
2575 we are trying to expand. */
2576 static rtx
2577 widen_leading (scalar_int_mode mode, rtx op0, rtx target, optab unoptab)
2579 opt_scalar_int_mode wider_mode_iter;
2580 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2582 scalar_int_mode wider_mode = wider_mode_iter.require ();
2583 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2585 rtx xop0, temp;
2586 rtx_insn *last;
2588 last = get_last_insn ();
2590 if (target == 0)
2591 target = gen_reg_rtx (mode);
2592 xop0 = widen_operand (op0, wider_mode, mode,
2593 unoptab != clrsb_optab, false);
2594 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2595 unoptab != clrsb_optab);
2596 if (temp != 0)
2597 temp = expand_binop
2598 (wider_mode, sub_optab, temp,
2599 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2600 - GET_MODE_PRECISION (mode),
2601 wider_mode),
2602 target, true, OPTAB_DIRECT);
2603 if (temp == 0)
2604 delete_insns_since (last);
2606 return temp;
2609 return 0;
2612 /* Attempt to emit (clrsb:mode op0) as
2613 (plus:mode (clz:mode (xor:mode op0 (ashr:mode op0 (const_int prec-1))))
2614 (const_int -1))
2615 if CLZ_DEFINED_VALUE_AT_ZERO (mode, val) is 2 and val is prec,
2616 or as
2617 (clz:mode (ior:mode (xor:mode (ashl:mode op0 (const_int 1))
2618 (ashr:mode op0 (const_int prec-1)))
2619 (const_int 1)))
2620 otherwise. */
2622 static rtx
2623 expand_clrsb_using_clz (scalar_int_mode mode, rtx op0, rtx target)
2625 if (optimize_insn_for_size_p ()
2626 || optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2627 return NULL_RTX;
2629 start_sequence ();
2630 HOST_WIDE_INT val = 0;
2631 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) != 2
2632 || val != GET_MODE_PRECISION (mode))
2633 val = 0;
2634 else
2635 val = 1;
2637 rtx temp2 = op0;
2638 if (!val)
2640 temp2 = expand_binop (mode, ashl_optab, op0, const1_rtx,
2641 NULL_RTX, 0, OPTAB_DIRECT);
2642 if (!temp2)
2644 fail:
2645 end_sequence ();
2646 return NULL_RTX;
2650 rtx temp = expand_binop (mode, ashr_optab, op0,
2651 GEN_INT (GET_MODE_PRECISION (mode) - 1),
2652 NULL_RTX, 0, OPTAB_DIRECT);
2653 if (!temp)
2654 goto fail;
2656 temp = expand_binop (mode, xor_optab, temp2, temp, NULL_RTX, 0,
2657 OPTAB_DIRECT);
2658 if (!temp)
2659 goto fail;
2661 if (!val)
2663 temp = expand_binop (mode, ior_optab, temp, const1_rtx,
2664 NULL_RTX, 0, OPTAB_DIRECT);
2665 if (!temp)
2666 goto fail;
2668 temp = expand_unop_direct (mode, clz_optab, temp, val ? NULL_RTX : target,
2669 true);
2670 if (!temp)
2671 goto fail;
2672 if (val)
2674 temp = expand_binop (mode, add_optab, temp, constm1_rtx,
2675 target, 0, OPTAB_DIRECT);
2676 if (!temp)
2677 goto fail;
2680 rtx_insn *seq = get_insns ();
2681 end_sequence ();
2683 add_equal_note (seq, temp, CLRSB, op0, NULL_RTX, mode);
2684 emit_insn (seq);
2685 return temp;
2688 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2689 quantities, choosing which based on whether the high word is nonzero. */
2690 static rtx
2691 expand_doubleword_clz (scalar_int_mode mode, rtx op0, rtx target)
2693 rtx xop0 = force_reg (mode, op0);
2694 rtx subhi = gen_highpart (word_mode, xop0);
2695 rtx sublo = gen_lowpart (word_mode, xop0);
2696 rtx_code_label *hi0_label = gen_label_rtx ();
2697 rtx_code_label *after_label = gen_label_rtx ();
2698 rtx_insn *seq;
2699 rtx temp, result;
2701 /* If we were not given a target, use a word_mode register, not a
2702 'mode' register. The result will fit, and nobody is expecting
2703 anything bigger (the return type of __builtin_clz* is int). */
2704 if (!target)
2705 target = gen_reg_rtx (word_mode);
2707 /* In any case, write to a word_mode scratch in both branches of the
2708 conditional, so we can ensure there is a single move insn setting
2709 'target' to tag a REG_EQUAL note on. */
2710 result = gen_reg_rtx (word_mode);
2712 start_sequence ();
2714 /* If the high word is not equal to zero,
2715 then clz of the full value is clz of the high word. */
2716 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2717 word_mode, true, hi0_label);
2719 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2720 if (!temp)
2721 goto fail;
2723 if (temp != result)
2724 convert_move (result, temp, true);
2726 emit_jump_insn (targetm.gen_jump (after_label));
2727 emit_barrier ();
2729 /* Else clz of the full value is clz of the low word plus the number
2730 of bits in the high word. */
2731 emit_label (hi0_label);
2733 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2734 if (!temp)
2735 goto fail;
2736 temp = expand_binop (word_mode, add_optab, temp,
2737 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2738 result, true, OPTAB_DIRECT);
2739 if (!temp)
2740 goto fail;
2741 if (temp != result)
2742 convert_move (result, temp, true);
2744 emit_label (after_label);
2745 convert_move (target, result, true);
2747 seq = get_insns ();
2748 end_sequence ();
2750 add_equal_note (seq, target, CLZ, xop0, NULL_RTX, mode);
2751 emit_insn (seq);
2752 return target;
2754 fail:
2755 end_sequence ();
2756 return 0;
2759 /* Try calculating popcount of a double-word quantity as two popcount's of
2760 word-sized quantities and summing up the results. */
2761 static rtx
2762 expand_doubleword_popcount (scalar_int_mode mode, rtx op0, rtx target)
2764 rtx t0, t1, t;
2765 rtx_insn *seq;
2767 start_sequence ();
2769 t0 = expand_unop_direct (word_mode, popcount_optab,
2770 operand_subword_force (op0, 0, mode), NULL_RTX,
2771 true);
2772 t1 = expand_unop_direct (word_mode, popcount_optab,
2773 operand_subword_force (op0, 1, mode), NULL_RTX,
2774 true);
2775 if (!t0 || !t1)
2777 end_sequence ();
2778 return NULL_RTX;
2781 /* If we were not given a target, use a word_mode register, not a
2782 'mode' register. The result will fit, and nobody is expecting
2783 anything bigger (the return type of __builtin_popcount* is int). */
2784 if (!target)
2785 target = gen_reg_rtx (word_mode);
2787 t = expand_binop (word_mode, add_optab, t0, t1, target, 0, OPTAB_DIRECT);
2789 seq = get_insns ();
2790 end_sequence ();
2792 add_equal_note (seq, t, POPCOUNT, op0, NULL_RTX, mode);
2793 emit_insn (seq);
2794 return t;
2797 /* Try calculating
2798 (parity:wide x)
2800 (parity:narrow (low (x) ^ high (x))) */
2801 static rtx
2802 expand_doubleword_parity (scalar_int_mode mode, rtx op0, rtx target)
2804 rtx t = expand_binop (word_mode, xor_optab,
2805 operand_subword_force (op0, 0, mode),
2806 operand_subword_force (op0, 1, mode),
2807 NULL_RTX, 0, OPTAB_DIRECT);
2808 return expand_unop (word_mode, parity_optab, t, target, true);
2811 /* Try calculating
2812 (bswap:narrow x)
2814 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2815 static rtx
2816 widen_bswap (scalar_int_mode mode, rtx op0, rtx target)
2818 rtx x;
2819 rtx_insn *last;
2820 opt_scalar_int_mode wider_mode_iter;
2822 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2823 if (optab_handler (bswap_optab, wider_mode_iter.require ())
2824 != CODE_FOR_nothing)
2825 break;
2827 if (!wider_mode_iter.exists ())
2828 return NULL_RTX;
2830 scalar_int_mode wider_mode = wider_mode_iter.require ();
2831 last = get_last_insn ();
2833 x = widen_operand (op0, wider_mode, mode, true, true);
2834 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2836 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2837 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2838 if (x != 0)
2839 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2840 GET_MODE_BITSIZE (wider_mode)
2841 - GET_MODE_BITSIZE (mode),
2842 NULL_RTX, true);
2844 if (x != 0)
2846 if (target == 0)
2847 target = gen_reg_rtx (mode);
2848 emit_move_insn (target, gen_lowpart (mode, x));
2850 else
2851 delete_insns_since (last);
2853 return target;
2856 /* Try calculating bswap as two bswaps of two word-sized operands. */
2858 static rtx
2859 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2861 rtx t0, t1;
2863 t1 = expand_unop (word_mode, bswap_optab,
2864 operand_subword_force (op, 0, mode), NULL_RTX, true);
2865 t0 = expand_unop (word_mode, bswap_optab,
2866 operand_subword_force (op, 1, mode), NULL_RTX, true);
2868 if (target == 0 || !valid_multiword_target_p (target))
2869 target = gen_reg_rtx (mode);
2870 if (REG_P (target))
2871 emit_clobber (target);
2872 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2873 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2875 return target;
2878 /* Try calculating (parity x) as (and (popcount x) 1), where
2879 popcount can also be done in a wider mode. */
2880 static rtx
2881 expand_parity (scalar_int_mode mode, rtx op0, rtx target)
2883 enum mode_class mclass = GET_MODE_CLASS (mode);
2884 opt_scalar_int_mode wider_mode_iter;
2885 FOR_EACH_MODE_FROM (wider_mode_iter, mode)
2887 scalar_int_mode wider_mode = wider_mode_iter.require ();
2888 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2890 rtx xop0, temp;
2891 rtx_insn *last;
2893 last = get_last_insn ();
2895 if (target == 0 || GET_MODE (target) != wider_mode)
2896 target = gen_reg_rtx (wider_mode);
2898 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2899 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2900 true);
2901 if (temp != 0)
2902 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2903 target, true, OPTAB_DIRECT);
2905 if (temp)
2907 if (mclass != MODE_INT
2908 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2909 return convert_to_mode (mode, temp, 0);
2910 else
2911 return gen_lowpart (mode, temp);
2913 else
2914 delete_insns_since (last);
2917 return 0;
2920 /* Try calculating ctz(x) as K - clz(x & -x) ,
2921 where K is GET_MODE_PRECISION(mode) - 1.
2923 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2924 don't have to worry about what the hardware does in that case. (If
2925 the clz instruction produces the usual value at 0, which is K, the
2926 result of this code sequence will be -1; expand_ffs, below, relies
2927 on this. It might be nice to have it be K instead, for consistency
2928 with the (very few) processors that provide a ctz with a defined
2929 value, but that would take one more instruction, and it would be
2930 less convenient for expand_ffs anyway. */
2932 static rtx
2933 expand_ctz (scalar_int_mode mode, rtx op0, rtx target)
2935 rtx_insn *seq;
2936 rtx temp;
2938 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2939 return 0;
2941 start_sequence ();
2943 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2944 if (temp)
2945 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2946 true, OPTAB_DIRECT);
2947 if (temp)
2948 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2949 if (temp)
2950 temp = expand_binop (mode, sub_optab,
2951 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2952 temp, target,
2953 true, OPTAB_DIRECT);
2954 if (temp == 0)
2956 end_sequence ();
2957 return 0;
2960 seq = get_insns ();
2961 end_sequence ();
2963 add_equal_note (seq, temp, CTZ, op0, NULL_RTX, mode);
2964 emit_insn (seq);
2965 return temp;
2969 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2970 else with the sequence used by expand_clz.
2972 The ffs builtin promises to return zero for a zero value and ctz/clz
2973 may have an undefined value in that case. If they do not give us a
2974 convenient value, we have to generate a test and branch. */
2975 static rtx
2976 expand_ffs (scalar_int_mode mode, rtx op0, rtx target)
2978 HOST_WIDE_INT val = 0;
2979 bool defined_at_zero = false;
2980 rtx temp;
2981 rtx_insn *seq;
2983 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2985 start_sequence ();
2987 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2988 if (!temp)
2989 goto fail;
2991 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2993 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2995 start_sequence ();
2996 temp = expand_ctz (mode, op0, 0);
2997 if (!temp)
2998 goto fail;
3000 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
3002 defined_at_zero = true;
3003 val = (GET_MODE_PRECISION (mode) - 1) - val;
3006 else
3007 return 0;
3009 if (defined_at_zero && val == -1)
3010 /* No correction needed at zero. */;
3011 else
3013 /* We don't try to do anything clever with the situation found
3014 on some processors (eg Alpha) where ctz(0:mode) ==
3015 bitsize(mode). If someone can think of a way to send N to -1
3016 and leave alone all values in the range 0..N-1 (where N is a
3017 power of two), cheaper than this test-and-branch, please add it.
3019 The test-and-branch is done after the operation itself, in case
3020 the operation sets condition codes that can be recycled for this.
3021 (This is true on i386, for instance.) */
3023 rtx_code_label *nonzero_label = gen_label_rtx ();
3024 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
3025 mode, true, nonzero_label);
3027 convert_move (temp, GEN_INT (-1), false);
3028 emit_label (nonzero_label);
3031 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
3032 to produce a value in the range 0..bitsize. */
3033 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
3034 target, false, OPTAB_DIRECT);
3035 if (!temp)
3036 goto fail;
3038 seq = get_insns ();
3039 end_sequence ();
3041 add_equal_note (seq, temp, FFS, op0, NULL_RTX, mode);
3042 emit_insn (seq);
3043 return temp;
3045 fail:
3046 end_sequence ();
3047 return 0;
3050 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
3051 conditions, VAL may already be a SUBREG against which we cannot generate
3052 a further SUBREG. In this case, we expect forcing the value into a
3053 register will work around the situation. */
3055 static rtx
3056 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
3057 machine_mode imode)
3059 rtx ret;
3060 ret = lowpart_subreg (omode, val, imode);
3061 if (ret == NULL)
3063 val = force_reg (imode, val);
3064 ret = lowpart_subreg (omode, val, imode);
3065 gcc_assert (ret != NULL);
3067 return ret;
3070 /* Expand a floating point absolute value or negation operation via a
3071 logical operation on the sign bit. */
3073 static rtx
3074 expand_absneg_bit (enum rtx_code code, scalar_float_mode mode,
3075 rtx op0, rtx target)
3077 const struct real_format *fmt;
3078 int bitpos, word, nwords, i;
3079 scalar_int_mode imode;
3080 rtx temp;
3081 rtx_insn *insns;
3083 /* The format has to have a simple sign bit. */
3084 fmt = REAL_MODE_FORMAT (mode);
3085 if (fmt == NULL)
3086 return NULL_RTX;
3088 bitpos = fmt->signbit_rw;
3089 if (bitpos < 0)
3090 return NULL_RTX;
3092 /* Don't create negative zeros if the format doesn't support them. */
3093 if (code == NEG && !fmt->has_signed_zero)
3094 return NULL_RTX;
3096 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3098 if (!int_mode_for_mode (mode).exists (&imode))
3099 return NULL_RTX;
3100 word = 0;
3101 nwords = 1;
3103 else
3105 imode = word_mode;
3107 if (FLOAT_WORDS_BIG_ENDIAN)
3108 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3109 else
3110 word = bitpos / BITS_PER_WORD;
3111 bitpos = bitpos % BITS_PER_WORD;
3112 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3115 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3116 if (code == ABS)
3117 mask = ~mask;
3119 if (target == 0
3120 || target == op0
3121 || reg_overlap_mentioned_p (target, op0)
3122 || (nwords > 1 && !valid_multiword_target_p (target)))
3123 target = gen_reg_rtx (mode);
3125 if (nwords > 1)
3127 start_sequence ();
3129 for (i = 0; i < nwords; ++i)
3131 rtx targ_piece = operand_subword (target, i, 1, mode);
3132 rtx op0_piece = operand_subword_force (op0, i, mode);
3134 if (i == word)
3136 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
3137 op0_piece,
3138 immed_wide_int_const (mask, imode),
3139 targ_piece, 1, OPTAB_LIB_WIDEN);
3140 if (temp != targ_piece)
3141 emit_move_insn (targ_piece, temp);
3143 else
3144 emit_move_insn (targ_piece, op0_piece);
3147 insns = get_insns ();
3148 end_sequence ();
3150 emit_insn (insns);
3152 else
3154 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
3155 gen_lowpart (imode, op0),
3156 immed_wide_int_const (mask, imode),
3157 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3158 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3160 set_dst_reg_note (get_last_insn (), REG_EQUAL,
3161 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
3162 target);
3165 return target;
3168 /* As expand_unop, but will fail rather than attempt the operation in a
3169 different mode or with a libcall. */
3170 static rtx
3171 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
3172 int unsignedp)
3174 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
3176 class expand_operand ops[2];
3177 enum insn_code icode = optab_handler (unoptab, mode);
3178 rtx_insn *last = get_last_insn ();
3179 rtx_insn *pat;
3181 create_output_operand (&ops[0], target, mode);
3182 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
3183 pat = maybe_gen_insn (icode, 2, ops);
3184 if (pat)
3186 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3187 && ! add_equal_note (pat, ops[0].value,
3188 optab_to_code (unoptab),
3189 ops[1].value, NULL_RTX, mode))
3191 delete_insns_since (last);
3192 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3195 emit_insn (pat);
3197 return ops[0].value;
3200 return 0;
3203 /* Generate code to perform an operation specified by UNOPTAB
3204 on operand OP0, with result having machine-mode MODE.
3206 UNSIGNEDP is for the case where we have to widen the operands
3207 to perform the operation. It says to use zero-extension.
3209 If TARGET is nonzero, the value
3210 is generated there, if it is convenient to do so.
3211 In all cases an rtx is returned for the locus of the value;
3212 this may or may not be TARGET. */
3215 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
3216 int unsignedp)
3218 enum mode_class mclass = GET_MODE_CLASS (mode);
3219 machine_mode wider_mode;
3220 scalar_int_mode int_mode;
3221 scalar_float_mode float_mode;
3222 rtx temp;
3223 rtx libfunc;
3225 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3226 if (temp)
3227 return temp;
3229 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3231 /* Widening (or narrowing) clz needs special treatment. */
3232 if (unoptab == clz_optab)
3234 if (is_a <scalar_int_mode> (mode, &int_mode))
3236 temp = widen_leading (int_mode, op0, target, unoptab);
3237 if (temp)
3238 return temp;
3240 if (GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3241 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3243 temp = expand_doubleword_clz (int_mode, op0, target);
3244 if (temp)
3245 return temp;
3249 goto try_libcall;
3252 if (unoptab == clrsb_optab)
3254 if (is_a <scalar_int_mode> (mode, &int_mode))
3256 temp = widen_leading (int_mode, op0, target, unoptab);
3257 if (temp)
3258 return temp;
3259 temp = expand_clrsb_using_clz (int_mode, op0, target);
3260 if (temp)
3261 return temp;
3263 goto try_libcall;
3266 if (unoptab == popcount_optab
3267 && is_a <scalar_int_mode> (mode, &int_mode)
3268 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3269 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing
3270 && optimize_insn_for_speed_p ())
3272 temp = expand_doubleword_popcount (int_mode, op0, target);
3273 if (temp)
3274 return temp;
3277 if (unoptab == parity_optab
3278 && is_a <scalar_int_mode> (mode, &int_mode)
3279 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3280 && (optab_handler (unoptab, word_mode) != CODE_FOR_nothing
3281 || optab_handler (popcount_optab, word_mode) != CODE_FOR_nothing)
3282 && optimize_insn_for_speed_p ())
3284 temp = expand_doubleword_parity (int_mode, op0, target);
3285 if (temp)
3286 return temp;
3289 /* Widening (or narrowing) bswap needs special treatment. */
3290 if (unoptab == bswap_optab)
3292 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3293 or ROTATERT. First try these directly; if this fails, then try the
3294 obvious pair of shifts with allowed widening, as this will probably
3295 be always more efficient than the other fallback methods. */
3296 if (mode == HImode)
3298 rtx_insn *last;
3299 rtx temp1, temp2;
3301 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3303 temp = expand_binop (mode, rotl_optab, op0,
3304 gen_int_shift_amount (mode, 8),
3305 target, unsignedp, OPTAB_DIRECT);
3306 if (temp)
3307 return temp;
3310 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3312 temp = expand_binop (mode, rotr_optab, op0,
3313 gen_int_shift_amount (mode, 8),
3314 target, unsignedp, OPTAB_DIRECT);
3315 if (temp)
3316 return temp;
3319 last = get_last_insn ();
3321 temp1 = expand_binop (mode, ashl_optab, op0,
3322 gen_int_shift_amount (mode, 8), NULL_RTX,
3323 unsignedp, OPTAB_WIDEN);
3324 temp2 = expand_binop (mode, lshr_optab, op0,
3325 gen_int_shift_amount (mode, 8), NULL_RTX,
3326 unsignedp, OPTAB_WIDEN);
3327 if (temp1 && temp2)
3329 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3330 unsignedp, OPTAB_WIDEN);
3331 if (temp)
3332 return temp;
3335 delete_insns_since (last);
3338 if (is_a <scalar_int_mode> (mode, &int_mode))
3340 temp = widen_bswap (int_mode, op0, target);
3341 if (temp)
3342 return temp;
3344 /* We do not provide a 128-bit bswap in libgcc so force the use of
3345 a double bswap for 64-bit targets. */
3346 if (GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3347 && (UNITS_PER_WORD == 8
3348 || optab_handler (unoptab, word_mode) != CODE_FOR_nothing))
3350 temp = expand_doubleword_bswap (mode, op0, target);
3351 if (temp)
3352 return temp;
3356 goto try_libcall;
3359 if (CLASS_HAS_WIDER_MODES_P (mclass))
3360 FOR_EACH_WIDER_MODE (wider_mode, mode)
3362 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3364 rtx xop0 = op0;
3365 rtx_insn *last = get_last_insn ();
3367 /* For certain operations, we need not actually extend
3368 the narrow operand, as long as we will truncate the
3369 results to the same narrowness. */
3371 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3372 (unoptab == neg_optab
3373 || unoptab == one_cmpl_optab)
3374 && mclass == MODE_INT);
3376 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3377 unsignedp);
3379 if (temp)
3381 if (mclass != MODE_INT
3382 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3384 if (target == 0)
3385 target = gen_reg_rtx (mode);
3386 convert_move (target, temp, 0);
3387 return target;
3389 else
3390 return gen_lowpart (mode, temp);
3392 else
3393 delete_insns_since (last);
3397 /* These can be done a word at a time. */
3398 if (unoptab == one_cmpl_optab
3399 && is_int_mode (mode, &int_mode)
3400 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
3401 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3403 int i;
3404 rtx_insn *insns;
3406 if (target == 0
3407 || target == op0
3408 || reg_overlap_mentioned_p (target, op0)
3409 || !valid_multiword_target_p (target))
3410 target = gen_reg_rtx (int_mode);
3412 start_sequence ();
3414 /* Do the actual arithmetic. */
3415 for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
3417 rtx target_piece = operand_subword (target, i, 1, int_mode);
3418 rtx x = expand_unop (word_mode, unoptab,
3419 operand_subword_force (op0, i, int_mode),
3420 target_piece, unsignedp);
3422 if (target_piece != x)
3423 emit_move_insn (target_piece, x);
3426 insns = get_insns ();
3427 end_sequence ();
3429 emit_insn (insns);
3430 return target;
3433 /* Emit ~op0 as op0 ^ -1. */
3434 if (unoptab == one_cmpl_optab
3435 && (SCALAR_INT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
3436 && optab_handler (xor_optab, mode) != CODE_FOR_nothing)
3438 temp = expand_binop (mode, xor_optab, op0, CONSTM1_RTX (mode),
3439 target, unsignedp, OPTAB_DIRECT);
3440 if (temp)
3441 return temp;
3444 if (optab_to_code (unoptab) == NEG)
3446 /* Try negating floating point values by flipping the sign bit. */
3447 if (is_a <scalar_float_mode> (mode, &float_mode))
3449 temp = expand_absneg_bit (NEG, float_mode, op0, target);
3450 if (temp)
3451 return temp;
3454 /* If there is no negation pattern, and we have no negative zero,
3455 try subtracting from zero. */
3456 if (!HONOR_SIGNED_ZEROS (mode))
3458 temp = expand_binop (mode, (unoptab == negv_optab
3459 ? subv_optab : sub_optab),
3460 CONST0_RTX (mode), op0, target,
3461 unsignedp, OPTAB_DIRECT);
3462 if (temp)
3463 return temp;
3467 /* Try calculating parity (x) as popcount (x) % 2. */
3468 if (unoptab == parity_optab && is_a <scalar_int_mode> (mode, &int_mode))
3470 temp = expand_parity (int_mode, op0, target);
3471 if (temp)
3472 return temp;
3475 /* Try implementing ffs (x) in terms of clz (x). */
3476 if (unoptab == ffs_optab && is_a <scalar_int_mode> (mode, &int_mode))
3478 temp = expand_ffs (int_mode, op0, target);
3479 if (temp)
3480 return temp;
3483 /* Try implementing ctz (x) in terms of clz (x). */
3484 if (unoptab == ctz_optab && is_a <scalar_int_mode> (mode, &int_mode))
3486 temp = expand_ctz (int_mode, op0, target);
3487 if (temp)
3488 return temp;
3491 try_libcall:
3492 /* Now try a library call in this mode. */
3493 libfunc = optab_libfunc (unoptab, mode);
3494 if (libfunc)
3496 rtx_insn *insns;
3497 rtx value;
3498 rtx eq_value;
3499 machine_mode outmode = mode;
3501 /* All of these functions return small values. Thus we choose to
3502 have them return something that isn't a double-word. */
3503 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3504 || unoptab == clrsb_optab || unoptab == popcount_optab
3505 || unoptab == parity_optab)
3506 outmode
3507 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3508 optab_libfunc (unoptab, mode)));
3510 start_sequence ();
3512 /* Pass 1 for NO_QUEUE so we don't lose any increments
3513 if the libcall is cse'd or moved. */
3514 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3515 op0, mode);
3516 insns = get_insns ();
3517 end_sequence ();
3519 target = gen_reg_rtx (outmode);
3520 bool trapv = trapv_unoptab_p (unoptab);
3521 if (trapv)
3522 eq_value = NULL_RTX;
3523 else
3525 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3526 if (GET_MODE_UNIT_SIZE (outmode) < GET_MODE_UNIT_SIZE (mode))
3527 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3528 else if (GET_MODE_UNIT_SIZE (outmode) > GET_MODE_UNIT_SIZE (mode))
3529 eq_value = simplify_gen_unary (ZERO_EXTEND,
3530 outmode, eq_value, mode);
3532 emit_libcall_block_1 (insns, target, value, eq_value, trapv);
3534 return target;
3537 /* It can't be done in this mode. Can we do it in a wider mode? */
3539 if (CLASS_HAS_WIDER_MODES_P (mclass))
3541 FOR_EACH_WIDER_MODE (wider_mode, mode)
3543 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3544 || optab_libfunc (unoptab, wider_mode))
3546 rtx xop0 = op0;
3547 rtx_insn *last = get_last_insn ();
3549 /* For certain operations, we need not actually extend
3550 the narrow operand, as long as we will truncate the
3551 results to the same narrowness. */
3552 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3553 (unoptab == neg_optab
3554 || unoptab == one_cmpl_optab
3555 || unoptab == bswap_optab)
3556 && mclass == MODE_INT);
3558 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3559 unsignedp);
3561 /* If we are generating clz using wider mode, adjust the
3562 result. Similarly for clrsb. */
3563 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3564 && temp != 0)
3566 scalar_int_mode wider_int_mode
3567 = as_a <scalar_int_mode> (wider_mode);
3568 int_mode = as_a <scalar_int_mode> (mode);
3569 temp = expand_binop
3570 (wider_mode, sub_optab, temp,
3571 gen_int_mode (GET_MODE_PRECISION (wider_int_mode)
3572 - GET_MODE_PRECISION (int_mode),
3573 wider_int_mode),
3574 target, true, OPTAB_DIRECT);
3577 /* Likewise for bswap. */
3578 if (unoptab == bswap_optab && temp != 0)
3580 scalar_int_mode wider_int_mode
3581 = as_a <scalar_int_mode> (wider_mode);
3582 int_mode = as_a <scalar_int_mode> (mode);
3583 gcc_assert (GET_MODE_PRECISION (wider_int_mode)
3584 == GET_MODE_BITSIZE (wider_int_mode)
3585 && GET_MODE_PRECISION (int_mode)
3586 == GET_MODE_BITSIZE (int_mode));
3588 temp = expand_shift (RSHIFT_EXPR, wider_int_mode, temp,
3589 GET_MODE_BITSIZE (wider_int_mode)
3590 - GET_MODE_BITSIZE (int_mode),
3591 NULL_RTX, true);
3594 if (temp)
3596 if (mclass != MODE_INT)
3598 if (target == 0)
3599 target = gen_reg_rtx (mode);
3600 convert_move (target, temp, 0);
3601 return target;
3603 else
3604 return gen_lowpart (mode, temp);
3606 else
3607 delete_insns_since (last);
3612 /* One final attempt at implementing negation via subtraction,
3613 this time allowing widening of the operand. */
3614 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3616 rtx temp;
3617 temp = expand_binop (mode,
3618 unoptab == negv_optab ? subv_optab : sub_optab,
3619 CONST0_RTX (mode), op0,
3620 target, unsignedp, OPTAB_LIB_WIDEN);
3621 if (temp)
3622 return temp;
3625 return 0;
3628 /* Emit code to compute the absolute value of OP0, with result to
3629 TARGET if convenient. (TARGET may be 0.) The return value says
3630 where the result actually is to be found.
3632 MODE is the mode of the operand; the mode of the result is
3633 different but can be deduced from MODE.
3638 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3639 int result_unsignedp)
3641 rtx temp;
3643 if (GET_MODE_CLASS (mode) != MODE_INT
3644 || ! flag_trapv)
3645 result_unsignedp = 1;
3647 /* First try to do it with a special abs instruction. */
3648 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3649 op0, target, 0);
3650 if (temp != 0)
3651 return temp;
3653 /* For floating point modes, try clearing the sign bit. */
3654 scalar_float_mode float_mode;
3655 if (is_a <scalar_float_mode> (mode, &float_mode))
3657 temp = expand_absneg_bit (ABS, float_mode, op0, target);
3658 if (temp)
3659 return temp;
3662 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3663 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3664 && !HONOR_SIGNED_ZEROS (mode))
3666 rtx_insn *last = get_last_insn ();
3668 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3669 op0, NULL_RTX, 0);
3670 if (temp != 0)
3671 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3672 OPTAB_WIDEN);
3674 if (temp != 0)
3675 return temp;
3677 delete_insns_since (last);
3680 /* If this machine has expensive jumps, we can do integer absolute
3681 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3682 where W is the width of MODE. */
3684 scalar_int_mode int_mode;
3685 if (is_int_mode (mode, &int_mode)
3686 && BRANCH_COST (optimize_insn_for_speed_p (),
3687 false) >= 2)
3689 rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
3690 GET_MODE_PRECISION (int_mode) - 1,
3691 NULL_RTX, 0);
3693 temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
3694 OPTAB_LIB_WIDEN);
3695 if (temp != 0)
3696 temp = expand_binop (int_mode,
3697 result_unsignedp ? sub_optab : subv_optab,
3698 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3700 if (temp != 0)
3701 return temp;
3704 return NULL_RTX;
3708 expand_abs (machine_mode mode, rtx op0, rtx target,
3709 int result_unsignedp, int safe)
3711 rtx temp;
3712 rtx_code_label *op1;
3714 if (GET_MODE_CLASS (mode) != MODE_INT
3715 || ! flag_trapv)
3716 result_unsignedp = 1;
3718 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3719 if (temp != 0)
3720 return temp;
3722 /* If that does not win, use conditional jump and negate. */
3724 /* It is safe to use the target if it is the same
3725 as the source if this is also a pseudo register */
3726 if (op0 == target && REG_P (op0)
3727 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3728 safe = 1;
3730 op1 = gen_label_rtx ();
3731 if (target == 0 || ! safe
3732 || GET_MODE (target) != mode
3733 || (MEM_P (target) && MEM_VOLATILE_P (target))
3734 || (REG_P (target)
3735 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3736 target = gen_reg_rtx (mode);
3738 emit_move_insn (target, op0);
3739 NO_DEFER_POP;
3741 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3742 NULL_RTX, NULL, op1,
3743 profile_probability::uninitialized ());
3745 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3746 target, target, 0);
3747 if (op0 != target)
3748 emit_move_insn (target, op0);
3749 emit_label (op1);
3750 OK_DEFER_POP;
3751 return target;
3754 /* Emit code to compute the one's complement absolute value of OP0
3755 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3756 (TARGET may be NULL_RTX.) The return value says where the result
3757 actually is to be found.
3759 MODE is the mode of the operand; the mode of the result is
3760 different but can be deduced from MODE. */
3763 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3765 rtx temp;
3767 /* Not applicable for floating point modes. */
3768 if (FLOAT_MODE_P (mode))
3769 return NULL_RTX;
3771 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3772 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3774 rtx_insn *last = get_last_insn ();
3776 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3777 if (temp != 0)
3778 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3779 OPTAB_WIDEN);
3781 if (temp != 0)
3782 return temp;
3784 delete_insns_since (last);
3787 /* If this machine has expensive jumps, we can do one's complement
3788 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3790 scalar_int_mode int_mode;
3791 if (is_int_mode (mode, &int_mode)
3792 && BRANCH_COST (optimize_insn_for_speed_p (),
3793 false) >= 2)
3795 rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
3796 GET_MODE_PRECISION (int_mode) - 1,
3797 NULL_RTX, 0);
3799 temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
3800 OPTAB_LIB_WIDEN);
3802 if (temp != 0)
3803 return temp;
3806 return NULL_RTX;
3809 /* A subroutine of expand_copysign, perform the copysign operation using the
3810 abs and neg primitives advertised to exist on the target. The assumption
3811 is that we have a split register file, and leaving op0 in fp registers,
3812 and not playing with subregs so much, will help the register allocator. */
3814 static rtx
3815 expand_copysign_absneg (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3816 int bitpos, bool op0_is_abs)
3818 scalar_int_mode imode;
3819 enum insn_code icode;
3820 rtx sign;
3821 rtx_code_label *label;
3823 if (target == op1)
3824 target = NULL_RTX;
3826 /* Check if the back end provides an insn that handles signbit for the
3827 argument's mode. */
3828 icode = optab_handler (signbit_optab, mode);
3829 if (icode != CODE_FOR_nothing)
3831 imode = as_a <scalar_int_mode> (insn_data[(int) icode].operand[0].mode);
3832 sign = gen_reg_rtx (imode);
3833 emit_unop_insn (icode, sign, op1, UNKNOWN);
3835 else
3837 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3839 if (!int_mode_for_mode (mode).exists (&imode))
3840 return NULL_RTX;
3841 op1 = gen_lowpart (imode, op1);
3843 else
3845 int word;
3847 imode = word_mode;
3848 if (FLOAT_WORDS_BIG_ENDIAN)
3849 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3850 else
3851 word = bitpos / BITS_PER_WORD;
3852 bitpos = bitpos % BITS_PER_WORD;
3853 op1 = operand_subword_force (op1, word, mode);
3856 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3857 sign = expand_binop (imode, and_optab, op1,
3858 immed_wide_int_const (mask, imode),
3859 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3862 if (!op0_is_abs)
3864 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3865 if (op0 == NULL)
3866 return NULL_RTX;
3867 target = op0;
3869 else
3871 if (target == NULL_RTX)
3872 target = copy_to_reg (op0);
3873 else
3874 emit_move_insn (target, op0);
3877 label = gen_label_rtx ();
3878 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3880 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3881 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3882 else
3883 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3884 if (op0 != target)
3885 emit_move_insn (target, op0);
3887 emit_label (label);
3889 return target;
3893 /* A subroutine of expand_copysign, perform the entire copysign operation
3894 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3895 is true if op0 is known to have its sign bit clear. */
3897 static rtx
3898 expand_copysign_bit (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3899 int bitpos, bool op0_is_abs)
3901 scalar_int_mode imode;
3902 int word, nwords, i;
3903 rtx temp;
3904 rtx_insn *insns;
3906 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3908 if (!int_mode_for_mode (mode).exists (&imode))
3909 return NULL_RTX;
3910 word = 0;
3911 nwords = 1;
3913 else
3915 imode = word_mode;
3917 if (FLOAT_WORDS_BIG_ENDIAN)
3918 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3919 else
3920 word = bitpos / BITS_PER_WORD;
3921 bitpos = bitpos % BITS_PER_WORD;
3922 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3925 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3927 if (target == 0
3928 || target == op0
3929 || target == op1
3930 || reg_overlap_mentioned_p (target, op0)
3931 || reg_overlap_mentioned_p (target, op1)
3932 || (nwords > 1 && !valid_multiword_target_p (target)))
3933 target = gen_reg_rtx (mode);
3935 if (nwords > 1)
3937 start_sequence ();
3939 for (i = 0; i < nwords; ++i)
3941 rtx targ_piece = operand_subword (target, i, 1, mode);
3942 rtx op0_piece = operand_subword_force (op0, i, mode);
3944 if (i == word)
3946 if (!op0_is_abs)
3947 op0_piece
3948 = expand_binop (imode, and_optab, op0_piece,
3949 immed_wide_int_const (~mask, imode),
3950 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3951 op1 = expand_binop (imode, and_optab,
3952 operand_subword_force (op1, i, mode),
3953 immed_wide_int_const (mask, imode),
3954 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3956 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3957 targ_piece, 1, OPTAB_LIB_WIDEN);
3958 if (temp != targ_piece)
3959 emit_move_insn (targ_piece, temp);
3961 else
3962 emit_move_insn (targ_piece, op0_piece);
3965 insns = get_insns ();
3966 end_sequence ();
3968 emit_insn (insns);
3970 else
3972 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3973 immed_wide_int_const (mask, imode),
3974 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3976 op0 = gen_lowpart (imode, op0);
3977 if (!op0_is_abs)
3978 op0 = expand_binop (imode, and_optab, op0,
3979 immed_wide_int_const (~mask, imode),
3980 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3982 temp = expand_binop (imode, ior_optab, op0, op1,
3983 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3984 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3987 return target;
3990 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3991 scalar floating point mode. Return NULL if we do not know how to
3992 expand the operation inline. */
3995 expand_copysign (rtx op0, rtx op1, rtx target)
3997 scalar_float_mode mode;
3998 const struct real_format *fmt;
3999 bool op0_is_abs;
4000 rtx temp;
4002 mode = as_a <scalar_float_mode> (GET_MODE (op0));
4003 gcc_assert (GET_MODE (op1) == mode);
4005 /* First try to do it with a special instruction. */
4006 temp = expand_binop (mode, copysign_optab, op0, op1,
4007 target, 0, OPTAB_DIRECT);
4008 if (temp)
4009 return temp;
4011 fmt = REAL_MODE_FORMAT (mode);
4012 if (fmt == NULL || !fmt->has_signed_zero)
4013 return NULL_RTX;
4015 op0_is_abs = false;
4016 if (CONST_DOUBLE_AS_FLOAT_P (op0))
4018 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
4019 op0 = simplify_unary_operation (ABS, mode, op0, mode);
4020 op0_is_abs = true;
4023 if (fmt->signbit_ro >= 0
4024 && (CONST_DOUBLE_AS_FLOAT_P (op0)
4025 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
4026 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
4028 temp = expand_copysign_absneg (mode, op0, op1, target,
4029 fmt->signbit_ro, op0_is_abs);
4030 if (temp)
4031 return temp;
4034 if (fmt->signbit_rw < 0)
4035 return NULL_RTX;
4036 return expand_copysign_bit (mode, op0, op1, target,
4037 fmt->signbit_rw, op0_is_abs);
4040 /* Generate an instruction whose insn-code is INSN_CODE,
4041 with two operands: an output TARGET and an input OP0.
4042 TARGET *must* be nonzero, and the output is always stored there.
4043 CODE is an rtx code such that (CODE OP0) is an rtx that describes
4044 the value that is stored into TARGET.
4046 Return false if expansion failed. */
4048 bool
4049 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
4050 enum rtx_code code)
4052 class expand_operand ops[2];
4053 rtx_insn *pat;
4055 create_output_operand (&ops[0], target, GET_MODE (target));
4056 create_input_operand (&ops[1], op0, GET_MODE (op0));
4057 pat = maybe_gen_insn (icode, 2, ops);
4058 if (!pat)
4059 return false;
4061 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
4062 && code != UNKNOWN)
4063 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX,
4064 GET_MODE (op0));
4066 emit_insn (pat);
4068 if (ops[0].value != target)
4069 emit_move_insn (target, ops[0].value);
4070 return true;
4072 /* Generate an instruction whose insn-code is INSN_CODE,
4073 with two operands: an output TARGET and an input OP0.
4074 TARGET *must* be nonzero, and the output is always stored there.
4075 CODE is an rtx code such that (CODE OP0) is an rtx that describes
4076 the value that is stored into TARGET. */
4078 void
4079 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
4081 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
4082 gcc_assert (ok);
4085 struct no_conflict_data
4087 rtx target;
4088 rtx_insn *first, *insn;
4089 bool must_stay;
4092 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
4093 the currently examined clobber / store has to stay in the list of
4094 insns that constitute the actual libcall block. */
4095 static void
4096 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
4098 struct no_conflict_data *p= (struct no_conflict_data *) p0;
4100 /* If this inns directly contributes to setting the target, it must stay. */
4101 if (reg_overlap_mentioned_p (p->target, dest))
4102 p->must_stay = true;
4103 /* If we haven't committed to keeping any other insns in the list yet,
4104 there is nothing more to check. */
4105 else if (p->insn == p->first)
4106 return;
4107 /* If this insn sets / clobbers a register that feeds one of the insns
4108 already in the list, this insn has to stay too. */
4109 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
4110 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
4111 || reg_used_between_p (dest, p->first, p->insn)
4112 /* Likewise if this insn depends on a register set by a previous
4113 insn in the list, or if it sets a result (presumably a hard
4114 register) that is set or clobbered by a previous insn.
4115 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
4116 SET_DEST perform the former check on the address, and the latter
4117 check on the MEM. */
4118 || (GET_CODE (set) == SET
4119 && (modified_in_p (SET_SRC (set), p->first)
4120 || modified_in_p (SET_DEST (set), p->first)
4121 || modified_between_p (SET_SRC (set), p->first, p->insn)
4122 || modified_between_p (SET_DEST (set), p->first, p->insn))))
4123 p->must_stay = true;
4127 /* Emit code to make a call to a constant function or a library call.
4129 INSNS is a list containing all insns emitted in the call.
4130 These insns leave the result in RESULT. Our block is to copy RESULT
4131 to TARGET, which is logically equivalent to EQUIV.
4133 We first emit any insns that set a pseudo on the assumption that these are
4134 loading constants into registers; doing so allows them to be safely cse'ed
4135 between blocks. Then we emit all the other insns in the block, followed by
4136 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
4137 note with an operand of EQUIV. */
4139 static void
4140 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
4141 bool equiv_may_trap)
4143 rtx final_dest = target;
4144 rtx_insn *next, *last, *insn;
4146 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
4147 into a MEM later. Protect the libcall block from this change. */
4148 if (! REG_P (target) || REG_USERVAR_P (target))
4149 target = gen_reg_rtx (GET_MODE (target));
4151 /* If we're using non-call exceptions, a libcall corresponding to an
4152 operation that may trap may also trap. */
4153 /* ??? See the comment in front of make_reg_eh_region_note. */
4154 if (cfun->can_throw_non_call_exceptions
4155 && (equiv_may_trap || may_trap_p (equiv)))
4157 for (insn = insns; insn; insn = NEXT_INSN (insn))
4158 if (CALL_P (insn))
4160 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
4161 if (note)
4163 int lp_nr = INTVAL (XEXP (note, 0));
4164 if (lp_nr == 0 || lp_nr == INT_MIN)
4165 remove_note (insn, note);
4169 else
4171 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
4172 reg note to indicate that this call cannot throw or execute a nonlocal
4173 goto (unless there is already a REG_EH_REGION note, in which case
4174 we update it). */
4175 for (insn = insns; insn; insn = NEXT_INSN (insn))
4176 if (CALL_P (insn))
4177 make_reg_eh_region_note_nothrow_nononlocal (insn);
4180 /* First emit all insns that set pseudos. Remove them from the list as
4181 we go. Avoid insns that set pseudos which were referenced in previous
4182 insns. These can be generated by move_by_pieces, for example,
4183 to update an address. Similarly, avoid insns that reference things
4184 set in previous insns. */
4186 for (insn = insns; insn; insn = next)
4188 rtx set = single_set (insn);
4190 next = NEXT_INSN (insn);
4192 if (set != 0 && REG_P (SET_DEST (set))
4193 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
4195 struct no_conflict_data data;
4197 data.target = const0_rtx;
4198 data.first = insns;
4199 data.insn = insn;
4200 data.must_stay = 0;
4201 note_stores (insn, no_conflict_move_test, &data);
4202 if (! data.must_stay)
4204 if (PREV_INSN (insn))
4205 SET_NEXT_INSN (PREV_INSN (insn)) = next;
4206 else
4207 insns = next;
4209 if (next)
4210 SET_PREV_INSN (next) = PREV_INSN (insn);
4212 add_insn (insn);
4216 /* Some ports use a loop to copy large arguments onto the stack.
4217 Don't move anything outside such a loop. */
4218 if (LABEL_P (insn))
4219 break;
4222 /* Write the remaining insns followed by the final copy. */
4223 for (insn = insns; insn; insn = next)
4225 next = NEXT_INSN (insn);
4227 add_insn (insn);
4230 last = emit_move_insn (target, result);
4231 if (equiv)
4232 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
4234 if (final_dest != target)
4235 emit_move_insn (final_dest, target);
4238 void
4239 emit_libcall_block (rtx_insn *insns, rtx target, rtx result, rtx equiv)
4241 emit_libcall_block_1 (insns, target, result, equiv, false);
4244 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
4245 PURPOSE describes how this comparison will be used. CODE is the rtx
4246 comparison code we will be using.
4248 ??? Actually, CODE is slightly weaker than that. A target is still
4249 required to implement all of the normal bcc operations, but not
4250 required to implement all (or any) of the unordered bcc operations. */
4253 can_compare_p (enum rtx_code code, machine_mode mode,
4254 enum can_compare_purpose purpose)
4256 rtx test;
4257 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
4260 enum insn_code icode;
4262 if (purpose == ccp_jump
4263 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4264 && insn_operand_matches (icode, 0, test))
4265 return 1;
4266 if (purpose == ccp_store_flag
4267 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4268 && insn_operand_matches (icode, 1, test))
4269 return 1;
4270 if (purpose == ccp_cmov
4271 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4272 return 1;
4274 mode = GET_MODE_WIDER_MODE (mode).else_void ();
4275 PUT_MODE (test, mode);
4277 while (mode != VOIDmode);
4279 return 0;
4282 /* Return whether RTL code CODE corresponds to an unsigned optab. */
4284 static bool
4285 unsigned_optab_p (enum rtx_code code)
4287 return code == LTU || code == LEU || code == GTU || code == GEU;
4290 /* Return whether the backend-emitted comparison for code CODE, comparing
4291 operands of mode VALUE_MODE and producing a result with MASK_MODE, matches
4292 operand OPNO of pattern ICODE. */
4294 static bool
4295 insn_predicate_matches_p (enum insn_code icode, unsigned int opno,
4296 enum rtx_code code, machine_mode mask_mode,
4297 machine_mode value_mode)
4299 rtx reg1 = alloca_raw_REG (value_mode, LAST_VIRTUAL_REGISTER + 1);
4300 rtx reg2 = alloca_raw_REG (value_mode, LAST_VIRTUAL_REGISTER + 2);
4301 rtx test = alloca_rtx_fmt_ee (code, mask_mode, reg1, reg2);
4302 return insn_operand_matches (icode, opno, test);
4305 /* Return whether the backend can emit a vector comparison (vec_cmp/vec_cmpu)
4306 for code CODE, comparing operands of mode VALUE_MODE and producing a result
4307 with MASK_MODE. */
4309 bool
4310 can_vec_cmp_compare_p (enum rtx_code code, machine_mode value_mode,
4311 machine_mode mask_mode)
4313 enum insn_code icode
4314 = get_vec_cmp_icode (value_mode, mask_mode, unsigned_optab_p (code));
4315 if (icode == CODE_FOR_nothing)
4316 return false;
4318 return insn_predicate_matches_p (icode, 1, code, mask_mode, value_mode);
4321 /* Return whether the backend can emit a vector comparison (vcond/vcondu) for
4322 code CODE, comparing operands of mode CMP_OP_MODE and producing a result
4323 with VALUE_MODE. */
4325 bool
4326 can_vcond_compare_p (enum rtx_code code, machine_mode value_mode,
4327 machine_mode cmp_op_mode)
4329 enum insn_code icode
4330 = get_vcond_icode (value_mode, cmp_op_mode, unsigned_optab_p (code));
4331 if (icode == CODE_FOR_nothing)
4332 return false;
4334 return insn_predicate_matches_p (icode, 3, code, value_mode, cmp_op_mode);
4337 /* Return whether the backend can emit vector set instructions for inserting
4338 element into vector at variable index position. */
4340 bool
4341 can_vec_set_var_idx_p (machine_mode vec_mode)
4343 if (!VECTOR_MODE_P (vec_mode))
4344 return false;
4346 machine_mode inner_mode = GET_MODE_INNER (vec_mode);
4347 rtx reg1 = alloca_raw_REG (vec_mode, LAST_VIRTUAL_REGISTER + 1);
4348 rtx reg2 = alloca_raw_REG (inner_mode, LAST_VIRTUAL_REGISTER + 2);
4349 rtx reg3 = alloca_raw_REG (VOIDmode, LAST_VIRTUAL_REGISTER + 3);
4351 enum insn_code icode = optab_handler (vec_set_optab, vec_mode);
4353 return icode != CODE_FOR_nothing && insn_operand_matches (icode, 0, reg1)
4354 && insn_operand_matches (icode, 1, reg2)
4355 && insn_operand_matches (icode, 2, reg3);
4358 /* This function is called when we are going to emit a compare instruction that
4359 compares the values found in X and Y, using the rtl operator COMPARISON.
4361 If they have mode BLKmode, then SIZE specifies the size of both operands.
4363 UNSIGNEDP nonzero says that the operands are unsigned;
4364 this matters if they need to be widened (as given by METHODS).
4366 *PTEST is where the resulting comparison RTX is returned or NULL_RTX
4367 if we failed to produce one.
4369 *PMODE is the mode of the inputs (in case they are const_int).
4371 This function performs all the setup necessary so that the caller only has
4372 to emit a single comparison insn. This setup can involve doing a BLKmode
4373 comparison or emitting a library call to perform the comparison if no insn
4374 is available to handle it.
4375 The values which are passed in through pointers can be modified; the caller
4376 should perform the comparison on the modified values. Constant
4377 comparisons must have already been folded. */
4379 static void
4380 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4381 int unsignedp, enum optab_methods methods,
4382 rtx *ptest, machine_mode *pmode)
4384 machine_mode mode = *pmode;
4385 rtx libfunc, test;
4386 machine_mode cmp_mode;
4387 enum mode_class mclass;
4389 /* The other methods are not needed. */
4390 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4391 || methods == OPTAB_LIB_WIDEN);
4393 if (CONST_SCALAR_INT_P (y))
4394 canonicalize_comparison (mode, &comparison, &y);
4396 /* If we are optimizing, force expensive constants into a register. */
4397 if (CONSTANT_P (x) && optimize
4398 && (rtx_cost (x, mode, COMPARE, 0, optimize_insn_for_speed_p ())
4399 > COSTS_N_INSNS (1))
4400 && can_create_pseudo_p ())
4401 x = force_reg (mode, x);
4403 if (CONSTANT_P (y) && optimize
4404 && (rtx_cost (y, mode, COMPARE, 1, optimize_insn_for_speed_p ())
4405 > COSTS_N_INSNS (1))
4406 && can_create_pseudo_p ())
4407 y = force_reg (mode, y);
4409 /* Don't let both operands fail to indicate the mode. */
4410 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4411 x = force_reg (mode, x);
4412 if (mode == VOIDmode)
4413 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4415 /* Handle all BLKmode compares. */
4417 if (mode == BLKmode)
4419 machine_mode result_mode;
4420 enum insn_code cmp_code;
4421 rtx result;
4422 rtx opalign
4423 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4425 gcc_assert (size);
4427 /* Try to use a memory block compare insn - either cmpstr
4428 or cmpmem will do. */
4429 opt_scalar_int_mode cmp_mode_iter;
4430 FOR_EACH_MODE_IN_CLASS (cmp_mode_iter, MODE_INT)
4432 scalar_int_mode cmp_mode = cmp_mode_iter.require ();
4433 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4434 if (cmp_code == CODE_FOR_nothing)
4435 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4436 if (cmp_code == CODE_FOR_nothing)
4437 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4438 if (cmp_code == CODE_FOR_nothing)
4439 continue;
4441 /* Must make sure the size fits the insn's mode. */
4442 if (CONST_INT_P (size)
4443 ? UINTVAL (size) > GET_MODE_MASK (cmp_mode)
4444 : (GET_MODE_BITSIZE (as_a <scalar_int_mode> (GET_MODE (size)))
4445 > GET_MODE_BITSIZE (cmp_mode)))
4446 continue;
4448 result_mode = insn_data[cmp_code].operand[0].mode;
4449 result = gen_reg_rtx (result_mode);
4450 size = convert_to_mode (cmp_mode, size, 1);
4451 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4453 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4454 *pmode = result_mode;
4455 return;
4458 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4459 goto fail;
4461 /* Otherwise call a library function. */
4462 result = emit_block_comp_via_libcall (x, y, size);
4464 x = result;
4465 y = const0_rtx;
4466 mode = TYPE_MODE (integer_type_node);
4467 methods = OPTAB_LIB_WIDEN;
4468 unsignedp = false;
4471 /* Don't allow operands to the compare to trap, as that can put the
4472 compare and branch in different basic blocks. */
4473 if (cfun->can_throw_non_call_exceptions)
4475 if (!can_create_pseudo_p () && (may_trap_p (x) || may_trap_p (y)))
4476 goto fail;
4477 if (may_trap_p (x))
4478 x = copy_to_reg (x);
4479 if (may_trap_p (y))
4480 y = copy_to_reg (y);
4483 if (GET_MODE_CLASS (mode) == MODE_CC)
4485 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
4486 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4487 gcc_assert (icode != CODE_FOR_nothing
4488 && insn_operand_matches (icode, 0, test));
4489 *ptest = test;
4490 return;
4493 mclass = GET_MODE_CLASS (mode);
4494 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4495 FOR_EACH_MODE_FROM (cmp_mode, mode)
4497 enum insn_code icode;
4498 icode = optab_handler (cbranch_optab, cmp_mode);
4499 if (icode != CODE_FOR_nothing
4500 && insn_operand_matches (icode, 0, test))
4502 rtx_insn *last = get_last_insn ();
4503 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4504 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4505 if (op0 && op1
4506 && insn_operand_matches (icode, 1, op0)
4507 && insn_operand_matches (icode, 2, op1))
4509 XEXP (test, 0) = op0;
4510 XEXP (test, 1) = op1;
4511 *ptest = test;
4512 *pmode = cmp_mode;
4513 return;
4515 delete_insns_since (last);
4518 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4519 break;
4522 if (methods != OPTAB_LIB_WIDEN)
4523 goto fail;
4525 if (SCALAR_FLOAT_MODE_P (mode))
4527 /* Small trick if UNORDERED isn't implemented by the hardware. */
4528 if (comparison == UNORDERED && rtx_equal_p (x, y))
4530 prepare_cmp_insn (x, y, UNLT, NULL_RTX, unsignedp, OPTAB_WIDEN,
4531 ptest, pmode);
4532 if (*ptest)
4533 return;
4536 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4538 else
4540 rtx result;
4541 machine_mode ret_mode;
4543 /* Handle a libcall just for the mode we are using. */
4544 libfunc = optab_libfunc (cmp_optab, mode);
4545 gcc_assert (libfunc);
4547 /* If we want unsigned, and this mode has a distinct unsigned
4548 comparison routine, use that. */
4549 if (unsignedp)
4551 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4552 if (ulibfunc)
4553 libfunc = ulibfunc;
4556 ret_mode = targetm.libgcc_cmp_return_mode ();
4557 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4558 ret_mode, x, mode, y, mode);
4560 /* There are two kinds of comparison routines. Biased routines
4561 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4562 of gcc expect that the comparison operation is equivalent
4563 to the modified comparison. For signed comparisons compare the
4564 result against 1 in the biased case, and zero in the unbiased
4565 case. For unsigned comparisons always compare against 1 after
4566 biasing the unbiased result by adding 1. This gives us a way to
4567 represent LTU.
4568 The comparisons in the fixed-point helper library are always
4569 biased. */
4570 x = result;
4571 y = const1_rtx;
4573 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4575 if (unsignedp)
4576 x = plus_constant (ret_mode, result, 1);
4577 else
4578 y = const0_rtx;
4581 *pmode = ret_mode;
4582 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4583 ptest, pmode);
4586 return;
4588 fail:
4589 *ptest = NULL_RTX;
4592 /* Before emitting an insn with code ICODE, make sure that X, which is going
4593 to be used for operand OPNUM of the insn, is converted from mode MODE to
4594 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4595 that it is accepted by the operand predicate. Return the new value. */
4598 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
4599 machine_mode wider_mode, int unsignedp)
4601 if (mode != wider_mode)
4602 x = convert_modes (wider_mode, mode, x, unsignedp);
4604 if (!insn_operand_matches (icode, opnum, x))
4606 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
4607 if (reload_completed)
4608 return NULL_RTX;
4609 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
4610 return NULL_RTX;
4611 x = copy_to_mode_reg (op_mode, x);
4614 return x;
4617 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4618 we can do the branch. */
4620 static void
4621 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label,
4622 profile_probability prob)
4624 machine_mode optab_mode;
4625 enum mode_class mclass;
4626 enum insn_code icode;
4627 rtx_insn *insn;
4629 mclass = GET_MODE_CLASS (mode);
4630 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4631 icode = optab_handler (cbranch_optab, optab_mode);
4633 gcc_assert (icode != CODE_FOR_nothing);
4634 gcc_assert (insn_operand_matches (icode, 0, test));
4635 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4636 XEXP (test, 1), label));
4637 if (prob.initialized_p ()
4638 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4639 && insn
4640 && JUMP_P (insn)
4641 && any_condjump_p (insn)
4642 && !find_reg_note (insn, REG_BR_PROB, 0))
4643 add_reg_br_prob_note (insn, prob);
4646 /* Generate code to compare X with Y so that the condition codes are
4647 set and to jump to LABEL if the condition is true. If X is a
4648 constant and Y is not a constant, then the comparison is swapped to
4649 ensure that the comparison RTL has the canonical form.
4651 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4652 need to be widened. UNSIGNEDP is also used to select the proper
4653 branch condition code.
4655 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4657 MODE is the mode of the inputs (in case they are const_int).
4659 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4660 It will be potentially converted into an unsigned variant based on
4661 UNSIGNEDP to select a proper jump instruction.
4663 PROB is the probability of jumping to LABEL. */
4665 void
4666 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4667 machine_mode mode, int unsignedp, rtx label,
4668 profile_probability prob)
4670 rtx op0 = x, op1 = y;
4671 rtx test;
4673 /* Swap operands and condition to ensure canonical RTL. */
4674 if (swap_commutative_operands_p (x, y)
4675 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4677 op0 = y, op1 = x;
4678 comparison = swap_condition (comparison);
4681 /* If OP0 is still a constant, then both X and Y must be constants
4682 or the opposite comparison is not supported. Force X into a register
4683 to create canonical RTL. */
4684 if (CONSTANT_P (op0))
4685 op0 = force_reg (mode, op0);
4687 if (unsignedp)
4688 comparison = unsigned_condition (comparison);
4690 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4691 &test, &mode);
4692 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4696 /* Emit a library call comparison between floating point X and Y.
4697 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4699 static void
4700 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4701 rtx *ptest, machine_mode *pmode)
4703 enum rtx_code swapped = swap_condition (comparison);
4704 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4705 machine_mode orig_mode = GET_MODE (x);
4706 machine_mode mode;
4707 rtx true_rtx, false_rtx;
4708 rtx value, target, equiv;
4709 rtx_insn *insns;
4710 rtx libfunc = 0;
4711 bool reversed_p = false;
4712 scalar_int_mode cmp_mode = targetm.libgcc_cmp_return_mode ();
4714 FOR_EACH_MODE_FROM (mode, orig_mode)
4716 if (code_to_optab (comparison)
4717 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4718 break;
4720 if (code_to_optab (swapped)
4721 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4723 std::swap (x, y);
4724 comparison = swapped;
4725 break;
4728 if (code_to_optab (reversed)
4729 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4731 comparison = reversed;
4732 reversed_p = true;
4733 break;
4737 gcc_assert (mode != VOIDmode);
4739 if (mode != orig_mode)
4741 x = convert_to_mode (mode, x, 0);
4742 y = convert_to_mode (mode, y, 0);
4745 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4746 the RTL. The allows the RTL optimizers to delete the libcall if the
4747 condition can be determined at compile-time. */
4748 if (comparison == UNORDERED
4749 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4751 true_rtx = const_true_rtx;
4752 false_rtx = const0_rtx;
4754 else
4756 switch (comparison)
4758 case EQ:
4759 true_rtx = const0_rtx;
4760 false_rtx = const_true_rtx;
4761 break;
4763 case NE:
4764 true_rtx = const_true_rtx;
4765 false_rtx = const0_rtx;
4766 break;
4768 case GT:
4769 true_rtx = const1_rtx;
4770 false_rtx = const0_rtx;
4771 break;
4773 case GE:
4774 true_rtx = const0_rtx;
4775 false_rtx = constm1_rtx;
4776 break;
4778 case LT:
4779 true_rtx = constm1_rtx;
4780 false_rtx = const0_rtx;
4781 break;
4783 case LE:
4784 true_rtx = const0_rtx;
4785 false_rtx = const1_rtx;
4786 break;
4788 default:
4789 gcc_unreachable ();
4793 if (comparison == UNORDERED)
4795 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4796 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4797 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4798 temp, const_true_rtx, equiv);
4800 else
4802 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4803 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4804 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4805 equiv, true_rtx, false_rtx);
4808 start_sequence ();
4809 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4810 cmp_mode, x, mode, y, mode);
4811 insns = get_insns ();
4812 end_sequence ();
4814 target = gen_reg_rtx (cmp_mode);
4815 emit_libcall_block (insns, target, value, equiv);
4817 if (comparison == UNORDERED
4818 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4819 || reversed_p)
4820 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4821 else
4822 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4824 *pmode = cmp_mode;
4827 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4829 void
4830 emit_indirect_jump (rtx loc)
4832 if (!targetm.have_indirect_jump ())
4833 sorry ("indirect jumps are not available on this target");
4834 else
4836 class expand_operand ops[1];
4837 create_address_operand (&ops[0], loc);
4838 expand_jump_insn (targetm.code_for_indirect_jump, 1, ops);
4839 emit_barrier ();
4844 /* Emit a conditional move instruction if the machine supports one for that
4845 condition and machine mode.
4847 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4848 the mode to use should they be constants. If it is VOIDmode, they cannot
4849 both be constants.
4851 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4852 should be stored there. MODE is the mode to use should they be constants.
4853 If it is VOIDmode, they cannot both be constants.
4855 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4856 is not supported. */
4859 emit_conditional_move (rtx target, struct rtx_comparison comp,
4860 rtx op2, rtx op3,
4861 machine_mode mode, int unsignedp)
4863 rtx comparison;
4864 rtx_insn *last;
4865 enum insn_code icode;
4866 enum rtx_code reversed;
4868 /* If the two source operands are identical, that's just a move. */
4870 if (rtx_equal_p (op2, op3))
4872 if (!target)
4873 target = gen_reg_rtx (mode);
4875 emit_move_insn (target, op3);
4876 return target;
4879 /* If one operand is constant, make it the second one. Only do this
4880 if the other operand is not constant as well. */
4882 if (swap_commutative_operands_p (comp.op0, comp.op1))
4884 std::swap (comp.op0, comp.op1);
4885 comp.code = swap_condition (comp.code);
4888 /* get_condition will prefer to generate LT and GT even if the old
4889 comparison was against zero, so undo that canonicalization here since
4890 comparisons against zero are cheaper. */
4892 if (comp.code == LT && comp.op1 == const1_rtx)
4893 comp.code = LE, comp.op1 = const0_rtx;
4894 else if (comp.code == GT && comp.op1 == constm1_rtx)
4895 comp.code = GE, comp.op1 = const0_rtx;
4897 if (comp.mode == VOIDmode)
4898 comp.mode = GET_MODE (comp.op0);
4900 enum rtx_code orig_code = comp.code;
4901 bool swapped = false;
4902 if (swap_commutative_operands_p (op2, op3)
4903 && ((reversed =
4904 reversed_comparison_code_parts (comp.code, comp.op0, comp.op1, NULL))
4905 != UNKNOWN))
4907 std::swap (op2, op3);
4908 comp.code = reversed;
4909 swapped = true;
4912 if (mode == VOIDmode)
4913 mode = GET_MODE (op2);
4915 icode = direct_optab_handler (movcc_optab, mode);
4917 if (icode == CODE_FOR_nothing)
4918 return NULL_RTX;
4920 if (!target)
4921 target = gen_reg_rtx (mode);
4923 for (int pass = 0; ; pass++)
4925 comp.code = unsignedp ? unsigned_condition (comp.code) : comp.code;
4926 comparison =
4927 simplify_gen_relational (comp.code, VOIDmode,
4928 comp.mode, comp.op0, comp.op1);
4930 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4931 punt and let the caller figure out how best to deal with this
4932 situation. */
4933 if (COMPARISON_P (comparison))
4935 saved_pending_stack_adjust save;
4936 save_pending_stack_adjust (&save);
4937 last = get_last_insn ();
4938 do_pending_stack_adjust ();
4939 machine_mode cmpmode = comp.mode;
4940 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4941 GET_CODE (comparison), NULL_RTX, unsignedp,
4942 OPTAB_WIDEN, &comparison, &cmpmode);
4943 if (comparison)
4945 rtx res = emit_conditional_move_1 (target, comparison,
4946 op2, op3, mode);
4947 if (res != NULL_RTX)
4948 return res;
4950 delete_insns_since (last);
4951 restore_pending_stack_adjust (&save);
4954 if (pass == 1)
4955 return NULL_RTX;
4957 /* If the preferred op2/op3 order is not usable, retry with other
4958 operand order, perhaps it will expand successfully. */
4959 if (swapped)
4960 comp.code = orig_code;
4961 else if ((reversed =
4962 reversed_comparison_code_parts (orig_code, comp.op0, comp.op1,
4963 NULL))
4964 != UNKNOWN)
4965 comp.code = reversed;
4966 else
4967 return NULL_RTX;
4968 std::swap (op2, op3);
4972 /* Helper function that, in addition to COMPARISON, also tries
4973 the reversed REV_COMPARISON with swapped OP2 and OP3. As opposed
4974 to when we pass the specific constituents of a comparison, no
4975 additional insns are emitted for it. It might still be necessary
4976 to emit more than one insn for the final conditional move, though. */
4979 emit_conditional_move (rtx target, rtx comparison, rtx rev_comparison,
4980 rtx op2, rtx op3, machine_mode mode)
4982 rtx res = emit_conditional_move_1 (target, comparison, op2, op3, mode);
4984 if (res != NULL_RTX)
4985 return res;
4987 return emit_conditional_move_1 (target, rev_comparison, op3, op2, mode);
4990 /* Helper for emitting a conditional move. */
4992 static rtx
4993 emit_conditional_move_1 (rtx target, rtx comparison,
4994 rtx op2, rtx op3, machine_mode mode)
4996 enum insn_code icode;
4998 if (comparison == NULL_RTX || !COMPARISON_P (comparison))
4999 return NULL_RTX;
5001 /* If the two source operands are identical, that's just a move.
5002 As the comparison comes in non-canonicalized, we must make
5003 sure not to discard any possible side effects. If there are
5004 side effects, just let the target handle it. */
5005 if (!side_effects_p (comparison) && rtx_equal_p (op2, op3))
5007 if (!target)
5008 target = gen_reg_rtx (mode);
5010 emit_move_insn (target, op3);
5011 return target;
5014 if (mode == VOIDmode)
5015 mode = GET_MODE (op2);
5017 icode = direct_optab_handler (movcc_optab, mode);
5019 if (icode == CODE_FOR_nothing)
5020 return NULL_RTX;
5022 if (!target)
5023 target = gen_reg_rtx (mode);
5025 class expand_operand ops[4];
5027 create_output_operand (&ops[0], target, mode);
5028 create_fixed_operand (&ops[1], comparison);
5029 create_input_operand (&ops[2], op2, mode);
5030 create_input_operand (&ops[3], op3, mode);
5032 if (maybe_expand_insn (icode, 4, ops))
5034 if (ops[0].value != target)
5035 convert_move (target, ops[0].value, false);
5036 return target;
5039 return NULL_RTX;
5043 /* Emit a conditional negate or bitwise complement using the
5044 negcc or notcc optabs if available. Return NULL_RTX if such operations
5045 are not available. Otherwise return the RTX holding the result.
5046 TARGET is the desired destination of the result. COMP is the comparison
5047 on which to negate. If COND is true move into TARGET the negation
5048 or bitwise complement of OP1. Otherwise move OP2 into TARGET.
5049 CODE is either NEG or NOT. MODE is the machine mode in which the
5050 operation is performed. */
5053 emit_conditional_neg_or_complement (rtx target, rtx_code code,
5054 machine_mode mode, rtx cond, rtx op1,
5055 rtx op2)
5057 optab op = unknown_optab;
5058 if (code == NEG)
5059 op = negcc_optab;
5060 else if (code == NOT)
5061 op = notcc_optab;
5062 else
5063 gcc_unreachable ();
5065 insn_code icode = direct_optab_handler (op, mode);
5067 if (icode == CODE_FOR_nothing)
5068 return NULL_RTX;
5070 if (!target)
5071 target = gen_reg_rtx (mode);
5073 rtx_insn *last = get_last_insn ();
5074 class expand_operand ops[4];
5076 create_output_operand (&ops[0], target, mode);
5077 create_fixed_operand (&ops[1], cond);
5078 create_input_operand (&ops[2], op1, mode);
5079 create_input_operand (&ops[3], op2, mode);
5081 if (maybe_expand_insn (icode, 4, ops))
5083 if (ops[0].value != target)
5084 convert_move (target, ops[0].value, false);
5086 return target;
5088 delete_insns_since (last);
5089 return NULL_RTX;
5092 /* Emit a conditional addition instruction if the machine supports one for that
5093 condition and machine mode.
5095 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
5096 the mode to use should they be constants. If it is VOIDmode, they cannot
5097 both be constants.
5099 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
5100 should be stored there. MODE is the mode to use should they be constants.
5101 If it is VOIDmode, they cannot both be constants.
5103 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
5104 is not supported. */
5107 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
5108 machine_mode cmode, rtx op2, rtx op3,
5109 machine_mode mode, int unsignedp)
5111 rtx comparison;
5112 rtx_insn *last;
5113 enum insn_code icode;
5115 /* If one operand is constant, make it the second one. Only do this
5116 if the other operand is not constant as well. */
5118 if (swap_commutative_operands_p (op0, op1))
5120 std::swap (op0, op1);
5121 code = swap_condition (code);
5124 /* get_condition will prefer to generate LT and GT even if the old
5125 comparison was against zero, so undo that canonicalization here since
5126 comparisons against zero are cheaper. */
5127 if (code == LT && op1 == const1_rtx)
5128 code = LE, op1 = const0_rtx;
5129 else if (code == GT && op1 == constm1_rtx)
5130 code = GE, op1 = const0_rtx;
5132 if (cmode == VOIDmode)
5133 cmode = GET_MODE (op0);
5135 if (mode == VOIDmode)
5136 mode = GET_MODE (op2);
5138 icode = optab_handler (addcc_optab, mode);
5140 if (icode == CODE_FOR_nothing)
5141 return 0;
5143 if (!target)
5144 target = gen_reg_rtx (mode);
5146 code = unsignedp ? unsigned_condition (code) : code;
5147 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
5149 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
5150 return NULL and let the caller figure out how best to deal with this
5151 situation. */
5152 if (!COMPARISON_P (comparison))
5153 return NULL_RTX;
5155 do_pending_stack_adjust ();
5156 last = get_last_insn ();
5157 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
5158 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
5159 &comparison, &cmode);
5160 if (comparison)
5162 class expand_operand ops[4];
5164 create_output_operand (&ops[0], target, mode);
5165 create_fixed_operand (&ops[1], comparison);
5166 create_input_operand (&ops[2], op2, mode);
5167 create_input_operand (&ops[3], op3, mode);
5168 if (maybe_expand_insn (icode, 4, ops))
5170 if (ops[0].value != target)
5171 convert_move (target, ops[0].value, false);
5172 return target;
5175 delete_insns_since (last);
5176 return NULL_RTX;
5179 /* These functions attempt to generate an insn body, rather than
5180 emitting the insn, but if the gen function already emits them, we
5181 make no attempt to turn them back into naked patterns. */
5183 /* Generate and return an insn body to add Y to X. */
5185 rtx_insn *
5186 gen_add2_insn (rtx x, rtx y)
5188 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
5190 gcc_assert (insn_operand_matches (icode, 0, x));
5191 gcc_assert (insn_operand_matches (icode, 1, x));
5192 gcc_assert (insn_operand_matches (icode, 2, y));
5194 return GEN_FCN (icode) (x, x, y);
5197 /* Generate and return an insn body to add r1 and c,
5198 storing the result in r0. */
5200 rtx_insn *
5201 gen_add3_insn (rtx r0, rtx r1, rtx c)
5203 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
5205 if (icode == CODE_FOR_nothing
5206 || !insn_operand_matches (icode, 0, r0)
5207 || !insn_operand_matches (icode, 1, r1)
5208 || !insn_operand_matches (icode, 2, c))
5209 return NULL;
5211 return GEN_FCN (icode) (r0, r1, c);
5215 have_add2_insn (rtx x, rtx y)
5217 enum insn_code icode;
5219 gcc_assert (GET_MODE (x) != VOIDmode);
5221 icode = optab_handler (add_optab, GET_MODE (x));
5223 if (icode == CODE_FOR_nothing)
5224 return 0;
5226 if (!insn_operand_matches (icode, 0, x)
5227 || !insn_operand_matches (icode, 1, x)
5228 || !insn_operand_matches (icode, 2, y))
5229 return 0;
5231 return 1;
5234 /* Generate and return an insn body to add Y to X. */
5236 rtx_insn *
5237 gen_addptr3_insn (rtx x, rtx y, rtx z)
5239 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
5241 gcc_assert (insn_operand_matches (icode, 0, x));
5242 gcc_assert (insn_operand_matches (icode, 1, y));
5243 gcc_assert (insn_operand_matches (icode, 2, z));
5245 return GEN_FCN (icode) (x, y, z);
5248 /* Return true if the target implements an addptr pattern and X, Y,
5249 and Z are valid for the pattern predicates. */
5252 have_addptr3_insn (rtx x, rtx y, rtx z)
5254 enum insn_code icode;
5256 gcc_assert (GET_MODE (x) != VOIDmode);
5258 icode = optab_handler (addptr3_optab, GET_MODE (x));
5260 if (icode == CODE_FOR_nothing)
5261 return 0;
5263 if (!insn_operand_matches (icode, 0, x)
5264 || !insn_operand_matches (icode, 1, y)
5265 || !insn_operand_matches (icode, 2, z))
5266 return 0;
5268 return 1;
5271 /* Generate and return an insn body to subtract Y from X. */
5273 rtx_insn *
5274 gen_sub2_insn (rtx x, rtx y)
5276 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
5278 gcc_assert (insn_operand_matches (icode, 0, x));
5279 gcc_assert (insn_operand_matches (icode, 1, x));
5280 gcc_assert (insn_operand_matches (icode, 2, y));
5282 return GEN_FCN (icode) (x, x, y);
5285 /* Generate and return an insn body to subtract r1 and c,
5286 storing the result in r0. */
5288 rtx_insn *
5289 gen_sub3_insn (rtx r0, rtx r1, rtx c)
5291 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
5293 if (icode == CODE_FOR_nothing
5294 || !insn_operand_matches (icode, 0, r0)
5295 || !insn_operand_matches (icode, 1, r1)
5296 || !insn_operand_matches (icode, 2, c))
5297 return NULL;
5299 return GEN_FCN (icode) (r0, r1, c);
5303 have_sub2_insn (rtx x, rtx y)
5305 enum insn_code icode;
5307 gcc_assert (GET_MODE (x) != VOIDmode);
5309 icode = optab_handler (sub_optab, GET_MODE (x));
5311 if (icode == CODE_FOR_nothing)
5312 return 0;
5314 if (!insn_operand_matches (icode, 0, x)
5315 || !insn_operand_matches (icode, 1, x)
5316 || !insn_operand_matches (icode, 2, y))
5317 return 0;
5319 return 1;
5322 /* Generate the body of an insn to extend Y (with mode MFROM)
5323 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
5325 rtx_insn *
5326 gen_extend_insn (rtx x, rtx y, machine_mode mto,
5327 machine_mode mfrom, int unsignedp)
5329 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
5330 return GEN_FCN (icode) (x, y);
5333 /* Generate code to convert FROM to floating point
5334 and store in TO. FROM must be fixed point and not VOIDmode.
5335 UNSIGNEDP nonzero means regard FROM as unsigned.
5336 Normally this is done by correcting the final value
5337 if it is negative. */
5339 void
5340 expand_float (rtx to, rtx from, int unsignedp)
5342 enum insn_code icode;
5343 rtx target = to;
5344 scalar_mode from_mode, to_mode;
5345 machine_mode fmode, imode;
5346 bool can_do_signed = false;
5348 /* Crash now, because we won't be able to decide which mode to use. */
5349 gcc_assert (GET_MODE (from) != VOIDmode);
5351 /* Look for an insn to do the conversion. Do it in the specified
5352 modes if possible; otherwise convert either input, output or both to
5353 wider mode. If the integer mode is wider than the mode of FROM,
5354 we can do the conversion signed even if the input is unsigned. */
5356 FOR_EACH_MODE_FROM (fmode, GET_MODE (to))
5357 FOR_EACH_MODE_FROM (imode, GET_MODE (from))
5359 int doing_unsigned = unsignedp;
5361 if (fmode != GET_MODE (to)
5362 && (significand_size (fmode)
5363 < GET_MODE_UNIT_PRECISION (GET_MODE (from))))
5364 continue;
5366 icode = can_float_p (fmode, imode, unsignedp);
5367 if (icode == CODE_FOR_nothing && unsignedp)
5369 enum insn_code scode = can_float_p (fmode, imode, 0);
5370 if (scode != CODE_FOR_nothing)
5371 can_do_signed = true;
5372 if (imode != GET_MODE (from))
5373 icode = scode, doing_unsigned = 0;
5376 if (icode != CODE_FOR_nothing)
5378 if (imode != GET_MODE (from))
5379 from = convert_to_mode (imode, from, unsignedp);
5381 if (fmode != GET_MODE (to))
5382 target = gen_reg_rtx (fmode);
5384 emit_unop_insn (icode, target, from,
5385 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5387 if (target != to)
5388 convert_move (to, target, 0);
5389 return;
5393 /* Unsigned integer, and no way to convert directly. Convert as signed,
5394 then unconditionally adjust the result. */
5395 if (unsignedp
5396 && can_do_signed
5397 && is_a <scalar_mode> (GET_MODE (to), &to_mode)
5398 && is_a <scalar_mode> (GET_MODE (from), &from_mode))
5400 opt_scalar_mode fmode_iter;
5401 rtx_code_label *label = gen_label_rtx ();
5402 rtx temp;
5403 REAL_VALUE_TYPE offset;
5405 /* Look for a usable floating mode FMODE wider than the source and at
5406 least as wide as the target. Using FMODE will avoid rounding woes
5407 with unsigned values greater than the signed maximum value. */
5409 FOR_EACH_MODE_FROM (fmode_iter, to_mode)
5411 scalar_mode fmode = fmode_iter.require ();
5412 if (GET_MODE_PRECISION (from_mode) < GET_MODE_BITSIZE (fmode)
5413 && can_float_p (fmode, from_mode, 0) != CODE_FOR_nothing)
5414 break;
5417 if (!fmode_iter.exists (&fmode))
5419 /* There is no such mode. Pretend the target is wide enough. */
5420 fmode = to_mode;
5422 /* Avoid double-rounding when TO is narrower than FROM. */
5423 if ((significand_size (fmode) + 1)
5424 < GET_MODE_PRECISION (from_mode))
5426 rtx temp1;
5427 rtx_code_label *neglabel = gen_label_rtx ();
5429 /* Don't use TARGET if it isn't a register, is a hard register,
5430 or is the wrong mode. */
5431 if (!REG_P (target)
5432 || REGNO (target) < FIRST_PSEUDO_REGISTER
5433 || GET_MODE (target) != fmode)
5434 target = gen_reg_rtx (fmode);
5436 imode = from_mode;
5437 do_pending_stack_adjust ();
5439 /* Test whether the sign bit is set. */
5440 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5441 0, neglabel);
5443 /* The sign bit is not set. Convert as signed. */
5444 expand_float (target, from, 0);
5445 emit_jump_insn (targetm.gen_jump (label));
5446 emit_barrier ();
5448 /* The sign bit is set.
5449 Convert to a usable (positive signed) value by shifting right
5450 one bit, while remembering if a nonzero bit was shifted
5451 out; i.e., compute (from & 1) | (from >> 1). */
5453 emit_label (neglabel);
5454 temp = expand_binop (imode, and_optab, from, const1_rtx,
5455 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5456 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5457 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5458 OPTAB_LIB_WIDEN);
5459 expand_float (target, temp, 0);
5461 /* Multiply by 2 to undo the shift above. */
5462 temp = expand_binop (fmode, add_optab, target, target,
5463 target, 0, OPTAB_LIB_WIDEN);
5464 if (temp != target)
5465 emit_move_insn (target, temp);
5467 do_pending_stack_adjust ();
5468 emit_label (label);
5469 goto done;
5473 /* If we are about to do some arithmetic to correct for an
5474 unsigned operand, do it in a pseudo-register. */
5476 if (to_mode != fmode
5477 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5478 target = gen_reg_rtx (fmode);
5480 /* Convert as signed integer to floating. */
5481 expand_float (target, from, 0);
5483 /* If FROM is negative (and therefore TO is negative),
5484 correct its value by 2**bitwidth. */
5486 do_pending_stack_adjust ();
5487 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, from_mode,
5488 0, label);
5491 real_2expN (&offset, GET_MODE_PRECISION (from_mode), fmode);
5492 temp = expand_binop (fmode, add_optab, target,
5493 const_double_from_real_value (offset, fmode),
5494 target, 0, OPTAB_LIB_WIDEN);
5495 if (temp != target)
5496 emit_move_insn (target, temp);
5498 do_pending_stack_adjust ();
5499 emit_label (label);
5500 goto done;
5503 /* No hardware instruction available; call a library routine. */
5505 rtx libfunc;
5506 rtx_insn *insns;
5507 rtx value;
5508 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5510 if (is_narrower_int_mode (GET_MODE (from), SImode))
5511 from = convert_to_mode (SImode, from, unsignedp);
5513 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5514 gcc_assert (libfunc);
5516 start_sequence ();
5518 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5519 GET_MODE (to), from, GET_MODE (from));
5520 insns = get_insns ();
5521 end_sequence ();
5523 emit_libcall_block (insns, target, value,
5524 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5525 GET_MODE (to), from));
5528 done:
5530 /* Copy result to requested destination
5531 if we have been computing in a temp location. */
5533 if (target != to)
5535 if (GET_MODE (target) == GET_MODE (to))
5536 emit_move_insn (to, target);
5537 else
5538 convert_move (to, target, 0);
5542 /* Generate code to convert FROM to fixed point and store in TO. FROM
5543 must be floating point. */
5545 void
5546 expand_fix (rtx to, rtx from, int unsignedp)
5548 enum insn_code icode;
5549 rtx target = to;
5550 machine_mode fmode, imode;
5551 opt_scalar_mode fmode_iter;
5552 bool must_trunc = false;
5554 /* We first try to find a pair of modes, one real and one integer, at
5555 least as wide as FROM and TO, respectively, in which we can open-code
5556 this conversion. If the integer mode is wider than the mode of TO,
5557 we can do the conversion either signed or unsigned. */
5559 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
5560 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
5562 int doing_unsigned = unsignedp;
5564 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5565 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5566 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5568 if (icode != CODE_FOR_nothing)
5570 rtx_insn *last = get_last_insn ();
5571 rtx from1 = from;
5572 if (fmode != GET_MODE (from))
5573 from1 = convert_to_mode (fmode, from, 0);
5575 if (must_trunc)
5577 rtx temp = gen_reg_rtx (GET_MODE (from1));
5578 from1 = expand_unop (GET_MODE (from1), ftrunc_optab, from1,
5579 temp, 0);
5582 if (imode != GET_MODE (to))
5583 target = gen_reg_rtx (imode);
5585 if (maybe_emit_unop_insn (icode, target, from1,
5586 doing_unsigned ? UNSIGNED_FIX : FIX))
5588 if (target != to)
5589 convert_move (to, target, unsignedp);
5590 return;
5592 delete_insns_since (last);
5596 /* For an unsigned conversion, there is one more way to do it.
5597 If we have a signed conversion, we generate code that compares
5598 the real value to the largest representable positive number. If if
5599 is smaller, the conversion is done normally. Otherwise, subtract
5600 one plus the highest signed number, convert, and add it back.
5602 We only need to check all real modes, since we know we didn't find
5603 anything with a wider integer mode.
5605 This code used to extend FP value into mode wider than the destination.
5606 This is needed for decimal float modes which cannot accurately
5607 represent one plus the highest signed number of the same size, but
5608 not for binary modes. Consider, for instance conversion from SFmode
5609 into DImode.
5611 The hot path through the code is dealing with inputs smaller than 2^63
5612 and doing just the conversion, so there is no bits to lose.
5614 In the other path we know the value is positive in the range 2^63..2^64-1
5615 inclusive. (as for other input overflow happens and result is undefined)
5616 So we know that the most important bit set in mantissa corresponds to
5617 2^63. The subtraction of 2^63 should not generate any rounding as it
5618 simply clears out that bit. The rest is trivial. */
5620 scalar_int_mode to_mode;
5621 if (unsignedp
5622 && is_a <scalar_int_mode> (GET_MODE (to), &to_mode)
5623 && HWI_COMPUTABLE_MODE_P (to_mode))
5624 FOR_EACH_MODE_FROM (fmode_iter, as_a <scalar_mode> (GET_MODE (from)))
5626 scalar_mode fmode = fmode_iter.require ();
5627 if (CODE_FOR_nothing != can_fix_p (to_mode, fmode,
5628 0, &must_trunc)
5629 && (!DECIMAL_FLOAT_MODE_P (fmode)
5630 || (GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (to_mode))))
5632 int bitsize;
5633 REAL_VALUE_TYPE offset;
5634 rtx limit;
5635 rtx_code_label *lab1, *lab2;
5636 rtx_insn *insn;
5638 bitsize = GET_MODE_PRECISION (to_mode);
5639 real_2expN (&offset, bitsize - 1, fmode);
5640 limit = const_double_from_real_value (offset, fmode);
5641 lab1 = gen_label_rtx ();
5642 lab2 = gen_label_rtx ();
5644 if (fmode != GET_MODE (from))
5645 from = convert_to_mode (fmode, from, 0);
5647 /* See if we need to do the subtraction. */
5648 do_pending_stack_adjust ();
5649 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX,
5650 GET_MODE (from), 0, lab1);
5652 /* If not, do the signed "fix" and branch around fixup code. */
5653 expand_fix (to, from, 0);
5654 emit_jump_insn (targetm.gen_jump (lab2));
5655 emit_barrier ();
5657 /* Otherwise, subtract 2**(N-1), convert to signed number,
5658 then add 2**(N-1). Do the addition using XOR since this
5659 will often generate better code. */
5660 emit_label (lab1);
5661 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5662 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5663 expand_fix (to, target, 0);
5664 target = expand_binop (to_mode, xor_optab, to,
5665 gen_int_mode
5666 (HOST_WIDE_INT_1 << (bitsize - 1),
5667 to_mode),
5668 to, 1, OPTAB_LIB_WIDEN);
5670 if (target != to)
5671 emit_move_insn (to, target);
5673 emit_label (lab2);
5675 if (optab_handler (mov_optab, to_mode) != CODE_FOR_nothing)
5677 /* Make a place for a REG_NOTE and add it. */
5678 insn = emit_move_insn (to, to);
5679 set_dst_reg_note (insn, REG_EQUAL,
5680 gen_rtx_fmt_e (UNSIGNED_FIX, to_mode,
5681 copy_rtx (from)),
5682 to);
5685 return;
5689 /* We can't do it with an insn, so use a library call. But first ensure
5690 that the mode of TO is at least as wide as SImode, since those are the
5691 only library calls we know about. */
5693 if (is_narrower_int_mode (GET_MODE (to), SImode))
5695 target = gen_reg_rtx (SImode);
5697 expand_fix (target, from, unsignedp);
5699 else
5701 rtx_insn *insns;
5702 rtx value;
5703 rtx libfunc;
5705 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5706 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5707 gcc_assert (libfunc);
5709 start_sequence ();
5711 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5712 GET_MODE (to), from, GET_MODE (from));
5713 insns = get_insns ();
5714 end_sequence ();
5716 emit_libcall_block (insns, target, value,
5717 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5718 GET_MODE (to), from));
5721 if (target != to)
5723 if (GET_MODE (to) == GET_MODE (target))
5724 emit_move_insn (to, target);
5725 else
5726 convert_move (to, target, 0);
5731 /* Promote integer arguments for a libcall if necessary.
5732 emit_library_call_value cannot do the promotion because it does not
5733 know if it should do a signed or unsigned promotion. This is because
5734 there are no tree types defined for libcalls. */
5736 static rtx
5737 prepare_libcall_arg (rtx arg, int uintp)
5739 scalar_int_mode mode;
5740 machine_mode arg_mode;
5741 if (is_a <scalar_int_mode> (GET_MODE (arg), &mode))
5743 /* If we need to promote the integer function argument we need to do
5744 it here instead of inside emit_library_call_value because in
5745 emit_library_call_value we don't know if we should do a signed or
5746 unsigned promotion. */
5748 int unsigned_p = 0;
5749 arg_mode = promote_function_mode (NULL_TREE, mode,
5750 &unsigned_p, NULL_TREE, 0);
5751 if (arg_mode != mode)
5752 return convert_to_mode (arg_mode, arg, uintp);
5754 return arg;
5757 /* Generate code to convert FROM or TO a fixed-point.
5758 If UINTP is true, either TO or FROM is an unsigned integer.
5759 If SATP is true, we need to saturate the result. */
5761 void
5762 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5764 machine_mode to_mode = GET_MODE (to);
5765 machine_mode from_mode = GET_MODE (from);
5766 convert_optab tab;
5767 enum rtx_code this_code;
5768 enum insn_code code;
5769 rtx_insn *insns;
5770 rtx value;
5771 rtx libfunc;
5773 if (to_mode == from_mode)
5775 emit_move_insn (to, from);
5776 return;
5779 if (uintp)
5781 tab = satp ? satfractuns_optab : fractuns_optab;
5782 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5784 else
5786 tab = satp ? satfract_optab : fract_optab;
5787 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5789 code = convert_optab_handler (tab, to_mode, from_mode);
5790 if (code != CODE_FOR_nothing)
5792 emit_unop_insn (code, to, from, this_code);
5793 return;
5796 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5797 gcc_assert (libfunc);
5799 from = prepare_libcall_arg (from, uintp);
5800 from_mode = GET_MODE (from);
5802 start_sequence ();
5803 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5804 from, from_mode);
5805 insns = get_insns ();
5806 end_sequence ();
5808 emit_libcall_block (insns, to, value,
5809 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5812 /* Generate code to convert FROM to fixed point and store in TO. FROM
5813 must be floating point, TO must be signed. Use the conversion optab
5814 TAB to do the conversion. */
5816 bool
5817 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5819 enum insn_code icode;
5820 rtx target = to;
5821 machine_mode fmode, imode;
5823 /* We first try to find a pair of modes, one real and one integer, at
5824 least as wide as FROM and TO, respectively, in which we can open-code
5825 this conversion. If the integer mode is wider than the mode of TO,
5826 we can do the conversion either signed or unsigned. */
5828 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
5829 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
5831 icode = convert_optab_handler (tab, imode, fmode);
5832 if (icode != CODE_FOR_nothing)
5834 rtx_insn *last = get_last_insn ();
5835 if (fmode != GET_MODE (from))
5836 from = convert_to_mode (fmode, from, 0);
5838 if (imode != GET_MODE (to))
5839 target = gen_reg_rtx (imode);
5841 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5843 delete_insns_since (last);
5844 continue;
5846 if (target != to)
5847 convert_move (to, target, 0);
5848 return true;
5852 return false;
5855 /* Report whether we have an instruction to perform the operation
5856 specified by CODE on operands of mode MODE. */
5858 have_insn_for (enum rtx_code code, machine_mode mode)
5860 return (code_to_optab (code)
5861 && (optab_handler (code_to_optab (code), mode)
5862 != CODE_FOR_nothing));
5865 /* Print information about the current contents of the optabs on
5866 STDERR. */
5868 DEBUG_FUNCTION void
5869 debug_optab_libfuncs (void)
5871 int i, j, k;
5873 /* Dump the arithmetic optabs. */
5874 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
5875 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5877 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
5878 if (l)
5880 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5881 fprintf (stderr, "%s\t%s:\t%s\n",
5882 GET_RTX_NAME (optab_to_code ((optab) i)),
5883 GET_MODE_NAME (j),
5884 XSTR (l, 0));
5888 /* Dump the conversion optabs. */
5889 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
5890 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5891 for (k = 0; k < NUM_MACHINE_MODES; ++k)
5893 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
5894 (machine_mode) k);
5895 if (l)
5897 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5898 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
5899 GET_RTX_NAME (optab_to_code ((optab) i)),
5900 GET_MODE_NAME (j),
5901 GET_MODE_NAME (k),
5902 XSTR (l, 0));
5907 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5908 CODE. Return 0 on failure. */
5910 rtx_insn *
5911 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
5913 machine_mode mode = GET_MODE (op1);
5914 enum insn_code icode;
5915 rtx_insn *insn;
5916 rtx trap_rtx;
5918 if (mode == VOIDmode)
5919 return 0;
5921 icode = optab_handler (ctrap_optab, mode);
5922 if (icode == CODE_FOR_nothing)
5923 return 0;
5925 /* Some targets only accept a zero trap code. */
5926 if (!insn_operand_matches (icode, 3, tcode))
5927 return 0;
5929 do_pending_stack_adjust ();
5930 start_sequence ();
5931 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
5932 &trap_rtx, &mode);
5933 if (!trap_rtx)
5934 insn = NULL;
5935 else
5936 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
5937 tcode);
5939 /* If that failed, then give up. */
5940 if (insn == 0)
5942 end_sequence ();
5943 return 0;
5946 emit_insn (insn);
5947 insn = get_insns ();
5948 end_sequence ();
5949 return insn;
5952 /* Return rtx code for TCODE or UNKNOWN. Use UNSIGNEDP to select signed
5953 or unsigned operation code. */
5955 enum rtx_code
5956 get_rtx_code_1 (enum tree_code tcode, bool unsignedp)
5958 enum rtx_code code;
5959 switch (tcode)
5961 case EQ_EXPR:
5962 code = EQ;
5963 break;
5964 case NE_EXPR:
5965 code = NE;
5966 break;
5967 case LT_EXPR:
5968 code = unsignedp ? LTU : LT;
5969 break;
5970 case LE_EXPR:
5971 code = unsignedp ? LEU : LE;
5972 break;
5973 case GT_EXPR:
5974 code = unsignedp ? GTU : GT;
5975 break;
5976 case GE_EXPR:
5977 code = unsignedp ? GEU : GE;
5978 break;
5980 case UNORDERED_EXPR:
5981 code = UNORDERED;
5982 break;
5983 case ORDERED_EXPR:
5984 code = ORDERED;
5985 break;
5986 case UNLT_EXPR:
5987 code = UNLT;
5988 break;
5989 case UNLE_EXPR:
5990 code = UNLE;
5991 break;
5992 case UNGT_EXPR:
5993 code = UNGT;
5994 break;
5995 case UNGE_EXPR:
5996 code = UNGE;
5997 break;
5998 case UNEQ_EXPR:
5999 code = UNEQ;
6000 break;
6001 case LTGT_EXPR:
6002 code = LTGT;
6003 break;
6005 case BIT_AND_EXPR:
6006 code = AND;
6007 break;
6009 case BIT_IOR_EXPR:
6010 code = IOR;
6011 break;
6013 default:
6014 code = UNKNOWN;
6015 break;
6017 return code;
6020 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6021 or unsigned operation code. */
6023 enum rtx_code
6024 get_rtx_code (enum tree_code tcode, bool unsignedp)
6026 enum rtx_code code = get_rtx_code_1 (tcode, unsignedp);
6027 gcc_assert (code != UNKNOWN);
6028 return code;
6031 /* Return a comparison rtx of mode CMP_MODE for COND. Use UNSIGNEDP to
6032 select signed or unsigned operators. OPNO holds the index of the
6033 first comparison operand for insn ICODE. Do not generate the
6034 compare instruction itself. */
6037 vector_compare_rtx (machine_mode cmp_mode, enum tree_code tcode,
6038 tree t_op0, tree t_op1, bool unsignedp,
6039 enum insn_code icode, unsigned int opno)
6041 class expand_operand ops[2];
6042 rtx rtx_op0, rtx_op1;
6043 machine_mode m0, m1;
6044 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6046 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6048 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
6049 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
6050 cases, use the original mode. */
6051 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6052 EXPAND_STACK_PARM);
6053 m0 = GET_MODE (rtx_op0);
6054 if (m0 == VOIDmode)
6055 m0 = TYPE_MODE (TREE_TYPE (t_op0));
6057 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6058 EXPAND_STACK_PARM);
6059 m1 = GET_MODE (rtx_op1);
6060 if (m1 == VOIDmode)
6061 m1 = TYPE_MODE (TREE_TYPE (t_op1));
6063 create_input_operand (&ops[0], rtx_op0, m0);
6064 create_input_operand (&ops[1], rtx_op1, m1);
6065 if (!maybe_legitimize_operands (icode, opno, 2, ops))
6066 gcc_unreachable ();
6067 return gen_rtx_fmt_ee (rcode, cmp_mode, ops[0].value, ops[1].value);
6070 /* Check if vec_perm mask SEL is a constant equivalent to a shift of
6071 the first vec_perm operand, assuming the second operand (for left shift
6072 first operand) is a constant vector of zeros. Return the shift distance
6073 in bits if so, or NULL_RTX if the vec_perm is not a shift. MODE is the
6074 mode of the value being shifted. SHIFT_OPTAB is vec_shr_optab for right
6075 shift or vec_shl_optab for left shift. */
6076 static rtx
6077 shift_amt_for_vec_perm_mask (machine_mode mode, const vec_perm_indices &sel,
6078 optab shift_optab)
6080 unsigned int bitsize = GET_MODE_UNIT_BITSIZE (mode);
6081 poly_int64 first = sel[0];
6082 if (maybe_ge (sel[0], GET_MODE_NUNITS (mode)))
6083 return NULL_RTX;
6085 if (shift_optab == vec_shl_optab)
6087 unsigned int nelt;
6088 if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
6089 return NULL_RTX;
6090 unsigned firstidx = 0;
6091 for (unsigned int i = 0; i < nelt; i++)
6093 if (known_eq (sel[i], nelt))
6095 if (i == 0 || firstidx)
6096 return NULL_RTX;
6097 firstidx = i;
6099 else if (firstidx
6100 ? maybe_ne (sel[i], nelt + i - firstidx)
6101 : maybe_ge (sel[i], nelt))
6102 return NULL_RTX;
6105 if (firstidx == 0)
6106 return NULL_RTX;
6107 first = firstidx;
6109 else if (!sel.series_p (0, 1, first, 1))
6111 unsigned int nelt;
6112 if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
6113 return NULL_RTX;
6114 for (unsigned int i = 1; i < nelt; i++)
6116 poly_int64 expected = i + first;
6117 /* Indices into the second vector are all equivalent. */
6118 if (maybe_lt (sel[i], nelt)
6119 ? maybe_ne (sel[i], expected)
6120 : maybe_lt (expected, nelt))
6121 return NULL_RTX;
6125 return gen_int_shift_amount (mode, first * bitsize);
6128 /* A subroutine of expand_vec_perm_var for expanding one vec_perm insn. */
6130 static rtx
6131 expand_vec_perm_1 (enum insn_code icode, rtx target,
6132 rtx v0, rtx v1, rtx sel)
6134 machine_mode tmode = GET_MODE (target);
6135 machine_mode smode = GET_MODE (sel);
6136 class expand_operand ops[4];
6138 gcc_assert (GET_MODE_CLASS (smode) == MODE_VECTOR_INT
6139 || related_int_vector_mode (tmode).require () == smode);
6140 create_output_operand (&ops[0], target, tmode);
6141 create_input_operand (&ops[3], sel, smode);
6143 /* Make an effort to preserve v0 == v1. The target expander is able to
6144 rely on this to determine if we're permuting a single input operand. */
6145 if (rtx_equal_p (v0, v1))
6147 if (!insn_operand_matches (icode, 1, v0))
6148 v0 = force_reg (tmode, v0);
6149 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6150 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6152 create_fixed_operand (&ops[1], v0);
6153 create_fixed_operand (&ops[2], v0);
6155 else
6157 create_input_operand (&ops[1], v0, tmode);
6158 create_input_operand (&ops[2], v1, tmode);
6161 if (maybe_expand_insn (icode, 4, ops))
6162 return ops[0].value;
6163 return NULL_RTX;
6166 /* Implement a permutation of vectors v0 and v1 using the permutation
6167 vector in SEL and return the result. Use TARGET to hold the result
6168 if nonnull and convenient.
6170 MODE is the mode of the vectors being permuted (V0 and V1). SEL_MODE
6171 is the TYPE_MODE associated with SEL, or BLKmode if SEL isn't known
6172 to have a particular mode. */
6175 expand_vec_perm_const (machine_mode mode, rtx v0, rtx v1,
6176 const vec_perm_builder &sel, machine_mode sel_mode,
6177 rtx target)
6179 if (!target || !register_operand (target, mode))
6180 target = gen_reg_rtx (mode);
6182 /* Set QIMODE to a different vector mode with byte elements.
6183 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6184 machine_mode qimode;
6185 if (!qimode_for_vec_perm (mode).exists (&qimode))
6186 qimode = VOIDmode;
6188 rtx_insn *last = get_last_insn ();
6190 bool single_arg_p = rtx_equal_p (v0, v1);
6191 /* Always specify two input vectors here and leave the target to handle
6192 cases in which the inputs are equal. Not all backends can cope with
6193 the single-input representation when testing for a double-input
6194 target instruction. */
6195 vec_perm_indices indices (sel, 2, GET_MODE_NUNITS (mode));
6197 /* See if this can be handled with a vec_shr or vec_shl. We only do this
6198 if the second (for vec_shr) or first (for vec_shl) vector is all
6199 zeroes. */
6200 insn_code shift_code = CODE_FOR_nothing;
6201 insn_code shift_code_qi = CODE_FOR_nothing;
6202 optab shift_optab = unknown_optab;
6203 rtx v2 = v0;
6204 if (v1 == CONST0_RTX (GET_MODE (v1)))
6205 shift_optab = vec_shr_optab;
6206 else if (v0 == CONST0_RTX (GET_MODE (v0)))
6208 shift_optab = vec_shl_optab;
6209 v2 = v1;
6211 if (shift_optab != unknown_optab)
6213 shift_code = optab_handler (shift_optab, mode);
6214 shift_code_qi = ((qimode != VOIDmode && qimode != mode)
6215 ? optab_handler (shift_optab, qimode)
6216 : CODE_FOR_nothing);
6218 if (shift_code != CODE_FOR_nothing || shift_code_qi != CODE_FOR_nothing)
6220 rtx shift_amt = shift_amt_for_vec_perm_mask (mode, indices, shift_optab);
6221 if (shift_amt)
6223 class expand_operand ops[3];
6224 if (shift_amt == const0_rtx)
6225 return v2;
6226 if (shift_code != CODE_FOR_nothing)
6228 create_output_operand (&ops[0], target, mode);
6229 create_input_operand (&ops[1], v2, mode);
6230 create_convert_operand_from_type (&ops[2], shift_amt, sizetype);
6231 if (maybe_expand_insn (shift_code, 3, ops))
6232 return ops[0].value;
6234 if (shift_code_qi != CODE_FOR_nothing)
6236 rtx tmp = gen_reg_rtx (qimode);
6237 create_output_operand (&ops[0], tmp, qimode);
6238 create_input_operand (&ops[1], gen_lowpart (qimode, v2), qimode);
6239 create_convert_operand_from_type (&ops[2], shift_amt, sizetype);
6240 if (maybe_expand_insn (shift_code_qi, 3, ops))
6241 return gen_lowpart (mode, ops[0].value);
6246 if (targetm.vectorize.vec_perm_const != NULL)
6248 if (single_arg_p)
6249 v1 = v0;
6251 gcc_checking_assert (GET_MODE (v0) == GET_MODE (v1));
6252 machine_mode op_mode = GET_MODE (v0);
6253 if (targetm.vectorize.vec_perm_const (mode, op_mode, target, v0, v1,
6254 indices))
6255 return target;
6258 /* Fall back to a constant byte-based permutation. */
6259 vec_perm_indices qimode_indices;
6260 rtx target_qi = NULL_RTX, v0_qi = NULL_RTX, v1_qi = NULL_RTX;
6261 if (qimode != VOIDmode)
6263 qimode_indices.new_expanded_vector (indices, GET_MODE_UNIT_SIZE (mode));
6264 target_qi = gen_reg_rtx (qimode);
6265 v0_qi = gen_lowpart (qimode, v0);
6266 v1_qi = gen_lowpart (qimode, v1);
6267 if (targetm.vectorize.vec_perm_const != NULL
6268 && targetm.vectorize.vec_perm_const (qimode, qimode, target_qi, v0_qi,
6269 v1_qi, qimode_indices))
6270 return gen_lowpart (mode, target_qi);
6273 v0 = force_reg (mode, v0);
6274 if (single_arg_p)
6275 v1 = v0;
6276 v1 = force_reg (mode, v1);
6278 /* Otherwise expand as a fully variable permuation. */
6280 /* The optabs are only defined for selectors with the same width
6281 as the values being permuted. */
6282 machine_mode required_sel_mode;
6283 if (!related_int_vector_mode (mode).exists (&required_sel_mode))
6285 delete_insns_since (last);
6286 return NULL_RTX;
6289 /* We know that it is semantically valid to treat SEL as having SEL_MODE.
6290 If that isn't the mode we want then we need to prove that using
6291 REQUIRED_SEL_MODE is OK. */
6292 if (sel_mode != required_sel_mode)
6294 if (!selector_fits_mode_p (required_sel_mode, indices))
6296 delete_insns_since (last);
6297 return NULL_RTX;
6299 sel_mode = required_sel_mode;
6302 insn_code icode = direct_optab_handler (vec_perm_optab, mode);
6303 if (icode != CODE_FOR_nothing)
6305 rtx sel_rtx = vec_perm_indices_to_rtx (sel_mode, indices);
6306 rtx tmp = expand_vec_perm_1 (icode, target, v0, v1, sel_rtx);
6307 if (tmp)
6308 return tmp;
6311 if (qimode != VOIDmode
6312 && selector_fits_mode_p (qimode, qimode_indices))
6314 icode = direct_optab_handler (vec_perm_optab, qimode);
6315 if (icode != CODE_FOR_nothing)
6317 rtx sel_qi = vec_perm_indices_to_rtx (qimode, qimode_indices);
6318 rtx tmp = expand_vec_perm_1 (icode, target_qi, v0_qi, v1_qi, sel_qi);
6319 if (tmp)
6320 return gen_lowpart (mode, tmp);
6324 delete_insns_since (last);
6325 return NULL_RTX;
6328 /* Implement a permutation of vectors v0 and v1 using the permutation
6329 vector in SEL and return the result. Use TARGET to hold the result
6330 if nonnull and convenient.
6332 MODE is the mode of the vectors being permuted (V0 and V1).
6333 SEL must have the integer equivalent of MODE and is known to be
6334 unsuitable for permutes with a constant permutation vector. */
6337 expand_vec_perm_var (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6339 enum insn_code icode;
6340 unsigned int i, u;
6341 rtx tmp, sel_qi;
6343 u = GET_MODE_UNIT_SIZE (mode);
6345 if (!target || GET_MODE (target) != mode)
6346 target = gen_reg_rtx (mode);
6348 icode = direct_optab_handler (vec_perm_optab, mode);
6349 if (icode != CODE_FOR_nothing)
6351 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6352 if (tmp)
6353 return tmp;
6356 /* As a special case to aid several targets, lower the element-based
6357 permutation to a byte-based permutation and try again. */
6358 machine_mode qimode;
6359 if (!qimode_for_vec_perm (mode).exists (&qimode)
6360 || maybe_gt (GET_MODE_NUNITS (qimode), GET_MODE_MASK (QImode) + 1))
6361 return NULL_RTX;
6362 icode = direct_optab_handler (vec_perm_optab, qimode);
6363 if (icode == CODE_FOR_nothing)
6364 return NULL_RTX;
6366 /* Multiply each element by its byte size. */
6367 machine_mode selmode = GET_MODE (sel);
6368 if (u == 2)
6369 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6370 NULL, 0, OPTAB_DIRECT);
6371 else
6372 sel = expand_simple_binop (selmode, ASHIFT, sel,
6373 gen_int_shift_amount (selmode, exact_log2 (u)),
6374 NULL, 0, OPTAB_DIRECT);
6375 gcc_assert (sel != NULL);
6377 /* Broadcast the low byte each element into each of its bytes.
6378 The encoding has U interleaved stepped patterns, one for each
6379 byte of an element. */
6380 vec_perm_builder const_sel (GET_MODE_SIZE (mode), u, 3);
6381 unsigned int low_byte_in_u = BYTES_BIG_ENDIAN ? u - 1 : 0;
6382 for (i = 0; i < 3; ++i)
6383 for (unsigned int j = 0; j < u; ++j)
6384 const_sel.quick_push (i * u + low_byte_in_u);
6385 sel = gen_lowpart (qimode, sel);
6386 sel = expand_vec_perm_const (qimode, sel, sel, const_sel, qimode, NULL);
6387 gcc_assert (sel != NULL);
6389 /* Add the byte offset to each byte element. */
6390 /* Note that the definition of the indicies here is memory ordering,
6391 so there should be no difference between big and little endian. */
6392 rtx_vector_builder byte_indices (qimode, u, 1);
6393 for (i = 0; i < u; ++i)
6394 byte_indices.quick_push (GEN_INT (i));
6395 tmp = byte_indices.build ();
6396 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6397 sel, 0, OPTAB_DIRECT);
6398 gcc_assert (sel_qi != NULL);
6400 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6401 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6402 gen_lowpart (qimode, v1), sel_qi);
6403 if (tmp)
6404 tmp = gen_lowpart (mode, tmp);
6405 return tmp;
6408 /* Generate VEC_SERIES_EXPR <OP0, OP1>, returning a value of mode VMODE.
6409 Use TARGET for the result if nonnull and convenient. */
6412 expand_vec_series_expr (machine_mode vmode, rtx op0, rtx op1, rtx target)
6414 class expand_operand ops[3];
6415 enum insn_code icode;
6416 machine_mode emode = GET_MODE_INNER (vmode);
6418 icode = direct_optab_handler (vec_series_optab, vmode);
6419 gcc_assert (icode != CODE_FOR_nothing);
6421 create_output_operand (&ops[0], target, vmode);
6422 create_input_operand (&ops[1], op0, emode);
6423 create_input_operand (&ops[2], op1, emode);
6425 expand_insn (icode, 3, ops);
6426 return ops[0].value;
6429 /* Generate insns for a vector comparison into a mask. */
6432 expand_vec_cmp_expr (tree type, tree exp, rtx target)
6434 class expand_operand ops[4];
6435 enum insn_code icode;
6436 rtx comparison;
6437 machine_mode mask_mode = TYPE_MODE (type);
6438 machine_mode vmode;
6439 bool unsignedp;
6440 tree op0a, op0b;
6441 enum tree_code tcode;
6443 op0a = TREE_OPERAND (exp, 0);
6444 op0b = TREE_OPERAND (exp, 1);
6445 tcode = TREE_CODE (exp);
6447 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6448 vmode = TYPE_MODE (TREE_TYPE (op0a));
6450 icode = get_vec_cmp_icode (vmode, mask_mode, unsignedp);
6451 if (icode == CODE_FOR_nothing)
6453 if (tcode == EQ_EXPR || tcode == NE_EXPR)
6454 icode = get_vec_cmp_eq_icode (vmode, mask_mode);
6455 if (icode == CODE_FOR_nothing)
6456 return 0;
6459 comparison = vector_compare_rtx (mask_mode, tcode, op0a, op0b,
6460 unsignedp, icode, 2);
6461 create_output_operand (&ops[0], target, mask_mode);
6462 create_fixed_operand (&ops[1], comparison);
6463 create_fixed_operand (&ops[2], XEXP (comparison, 0));
6464 create_fixed_operand (&ops[3], XEXP (comparison, 1));
6465 expand_insn (icode, 4, ops);
6466 return ops[0].value;
6469 /* Expand a highpart multiply. */
6472 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
6473 rtx target, bool uns_p)
6475 class expand_operand eops[3];
6476 enum insn_code icode;
6477 int method, i;
6478 machine_mode wmode;
6479 rtx m1, m2;
6480 optab tab1, tab2;
6482 method = can_mult_highpart_p (mode, uns_p);
6483 switch (method)
6485 case 0:
6486 return NULL_RTX;
6487 case 1:
6488 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6489 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6490 OPTAB_LIB_WIDEN);
6491 case 2:
6492 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6493 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6494 break;
6495 case 3:
6496 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6497 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6498 if (BYTES_BIG_ENDIAN)
6499 std::swap (tab1, tab2);
6500 break;
6501 default:
6502 gcc_unreachable ();
6505 icode = optab_handler (tab1, mode);
6506 wmode = insn_data[icode].operand[0].mode;
6507 gcc_checking_assert (known_eq (2 * GET_MODE_NUNITS (wmode),
6508 GET_MODE_NUNITS (mode)));
6509 gcc_checking_assert (known_eq (GET_MODE_SIZE (wmode), GET_MODE_SIZE (mode)));
6511 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6512 create_input_operand (&eops[1], op0, mode);
6513 create_input_operand (&eops[2], op1, mode);
6514 expand_insn (icode, 3, eops);
6515 m1 = gen_lowpart (mode, eops[0].value);
6517 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6518 create_input_operand (&eops[1], op0, mode);
6519 create_input_operand (&eops[2], op1, mode);
6520 expand_insn (optab_handler (tab2, mode), 3, eops);
6521 m2 = gen_lowpart (mode, eops[0].value);
6523 vec_perm_builder sel;
6524 if (method == 2)
6526 /* The encoding has 2 interleaved stepped patterns. */
6527 sel.new_vector (GET_MODE_NUNITS (mode), 2, 3);
6528 for (i = 0; i < 6; ++i)
6529 sel.quick_push (!BYTES_BIG_ENDIAN + (i & ~1)
6530 + ((i & 1) ? GET_MODE_NUNITS (mode) : 0));
6532 else
6534 /* The encoding has a single interleaved stepped pattern. */
6535 sel.new_vector (GET_MODE_NUNITS (mode), 1, 3);
6536 for (i = 0; i < 3; ++i)
6537 sel.quick_push (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
6540 return expand_vec_perm_const (mode, m1, m2, sel, BLKmode, target);
6543 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
6544 pattern. */
6546 static void
6547 find_cc_set (rtx x, const_rtx pat, void *data)
6549 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
6550 && GET_CODE (pat) == SET)
6552 rtx *p_cc_reg = (rtx *) data;
6553 gcc_assert (!*p_cc_reg);
6554 *p_cc_reg = x;
6558 /* This is a helper function for the other atomic operations. This function
6559 emits a loop that contains SEQ that iterates until a compare-and-swap
6560 operation at the end succeeds. MEM is the memory to be modified. SEQ is
6561 a set of instructions that takes a value from OLD_REG as an input and
6562 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
6563 set to the current contents of MEM. After SEQ, a compare-and-swap will
6564 attempt to update MEM with NEW_REG. The function returns true when the
6565 loop was generated successfully. */
6567 static bool
6568 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
6570 machine_mode mode = GET_MODE (mem);
6571 rtx_code_label *label;
6572 rtx cmp_reg, success, oldval;
6574 /* The loop we want to generate looks like
6576 cmp_reg = mem;
6577 label:
6578 old_reg = cmp_reg;
6579 seq;
6580 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
6581 if (success)
6582 goto label;
6584 Note that we only do the plain load from memory once. Subsequent
6585 iterations use the value loaded by the compare-and-swap pattern. */
6587 label = gen_label_rtx ();
6588 cmp_reg = gen_reg_rtx (mode);
6590 emit_move_insn (cmp_reg, mem);
6591 emit_label (label);
6592 emit_move_insn (old_reg, cmp_reg);
6593 if (seq)
6594 emit_insn (seq);
6596 success = NULL_RTX;
6597 oldval = cmp_reg;
6598 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
6599 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
6600 MEMMODEL_RELAXED))
6601 return false;
6603 if (oldval != cmp_reg)
6604 emit_move_insn (cmp_reg, oldval);
6606 /* Mark this jump predicted not taken. */
6607 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
6608 GET_MODE (success), 1, label,
6609 profile_probability::guessed_never ());
6610 return true;
6614 /* This function tries to emit an atomic_exchange intruction. VAL is written
6615 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
6616 using TARGET if possible. */
6618 static rtx
6619 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6621 machine_mode mode = GET_MODE (mem);
6622 enum insn_code icode;
6624 /* If the target supports the exchange directly, great. */
6625 icode = direct_optab_handler (atomic_exchange_optab, mode);
6626 if (icode != CODE_FOR_nothing)
6628 class expand_operand ops[4];
6630 create_output_operand (&ops[0], target, mode);
6631 create_fixed_operand (&ops[1], mem);
6632 create_input_operand (&ops[2], val, mode);
6633 create_integer_operand (&ops[3], model);
6634 if (maybe_expand_insn (icode, 4, ops))
6635 return ops[0].value;
6638 return NULL_RTX;
6641 /* This function tries to implement an atomic exchange operation using
6642 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
6643 The previous contents of *MEM are returned, using TARGET if possible.
6644 Since this instructionn is an acquire barrier only, stronger memory
6645 models may require additional barriers to be emitted. */
6647 static rtx
6648 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
6649 enum memmodel model)
6651 machine_mode mode = GET_MODE (mem);
6652 enum insn_code icode;
6653 rtx_insn *last_insn = get_last_insn ();
6655 icode = optab_handler (sync_lock_test_and_set_optab, mode);
6657 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
6658 exists, and the memory model is stronger than acquire, add a release
6659 barrier before the instruction. */
6661 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
6662 expand_mem_thread_fence (model);
6664 if (icode != CODE_FOR_nothing)
6666 class expand_operand ops[3];
6667 create_output_operand (&ops[0], target, mode);
6668 create_fixed_operand (&ops[1], mem);
6669 create_input_operand (&ops[2], val, mode);
6670 if (maybe_expand_insn (icode, 3, ops))
6671 return ops[0].value;
6674 /* If an external test-and-set libcall is provided, use that instead of
6675 any external compare-and-swap that we might get from the compare-and-
6676 swap-loop expansion later. */
6677 if (!can_compare_and_swap_p (mode, false))
6679 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
6680 if (libfunc != NULL)
6682 rtx addr;
6684 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6685 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6686 mode, addr, ptr_mode,
6687 val, mode);
6691 /* If the test_and_set can't be emitted, eliminate any barrier that might
6692 have been emitted. */
6693 delete_insns_since (last_insn);
6694 return NULL_RTX;
6697 /* This function tries to implement an atomic exchange operation using a
6698 compare_and_swap loop. VAL is written to *MEM. The previous contents of
6699 *MEM are returned, using TARGET if possible. No memory model is required
6700 since a compare_and_swap loop is seq-cst. */
6702 static rtx
6703 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
6705 machine_mode mode = GET_MODE (mem);
6707 if (can_compare_and_swap_p (mode, true))
6709 if (!target || !register_operand (target, mode))
6710 target = gen_reg_rtx (mode);
6711 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
6712 return target;
6715 return NULL_RTX;
6718 /* This function tries to implement an atomic test-and-set operation
6719 using the atomic_test_and_set instruction pattern. A boolean value
6720 is returned from the operation, using TARGET if possible. */
6722 static rtx
6723 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6725 machine_mode pat_bool_mode;
6726 class expand_operand ops[3];
6728 if (!targetm.have_atomic_test_and_set ())
6729 return NULL_RTX;
6731 /* While we always get QImode from __atomic_test_and_set, we get
6732 other memory modes from __sync_lock_test_and_set. Note that we
6733 use no endian adjustment here. This matches the 4.6 behavior
6734 in the Sparc backend. */
6735 enum insn_code icode = targetm.code_for_atomic_test_and_set;
6736 gcc_checking_assert (insn_data[icode].operand[1].mode == QImode);
6737 if (GET_MODE (mem) != QImode)
6738 mem = adjust_address_nv (mem, QImode, 0);
6740 pat_bool_mode = insn_data[icode].operand[0].mode;
6741 create_output_operand (&ops[0], target, pat_bool_mode);
6742 create_fixed_operand (&ops[1], mem);
6743 create_integer_operand (&ops[2], model);
6745 if (maybe_expand_insn (icode, 3, ops))
6746 return ops[0].value;
6747 return NULL_RTX;
6750 /* This function expands the legacy _sync_lock test_and_set operation which is
6751 generally an atomic exchange. Some limited targets only allow the
6752 constant 1 to be stored. This is an ACQUIRE operation.
6754 TARGET is an optional place to stick the return value.
6755 MEM is where VAL is stored. */
6758 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
6760 rtx ret;
6762 /* Try an atomic_exchange first. */
6763 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
6764 if (ret)
6765 return ret;
6767 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
6768 MEMMODEL_SYNC_ACQUIRE);
6769 if (ret)
6770 return ret;
6772 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6773 if (ret)
6774 return ret;
6776 /* If there are no other options, try atomic_test_and_set if the value
6777 being stored is 1. */
6778 if (val == const1_rtx)
6779 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
6781 return ret;
6784 /* This function expands the atomic test_and_set operation:
6785 atomically store a boolean TRUE into MEM and return the previous value.
6787 MEMMODEL is the memory model variant to use.
6788 TARGET is an optional place to stick the return value. */
6791 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6793 machine_mode mode = GET_MODE (mem);
6794 rtx ret, trueval, subtarget;
6796 ret = maybe_emit_atomic_test_and_set (target, mem, model);
6797 if (ret)
6798 return ret;
6800 /* Be binary compatible with non-default settings of trueval, and different
6801 cpu revisions. E.g. one revision may have atomic-test-and-set, but
6802 another only has atomic-exchange. */
6803 if (targetm.atomic_test_and_set_trueval == 1)
6805 trueval = const1_rtx;
6806 subtarget = target ? target : gen_reg_rtx (mode);
6808 else
6810 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
6811 subtarget = gen_reg_rtx (mode);
6814 /* Try the atomic-exchange optab... */
6815 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
6817 /* ... then an atomic-compare-and-swap loop ... */
6818 if (!ret)
6819 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
6821 /* ... before trying the vaguely defined legacy lock_test_and_set. */
6822 if (!ret)
6823 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
6825 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
6826 things with the value 1. Thus we try again without trueval. */
6827 if (!ret && targetm.atomic_test_and_set_trueval != 1)
6828 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
6830 /* Failing all else, assume a single threaded environment and simply
6831 perform the operation. */
6832 if (!ret)
6834 /* If the result is ignored skip the move to target. */
6835 if (subtarget != const0_rtx)
6836 emit_move_insn (subtarget, mem);
6838 emit_move_insn (mem, trueval);
6839 ret = subtarget;
6842 /* Recall that have to return a boolean value; rectify if trueval
6843 is not exactly one. */
6844 if (targetm.atomic_test_and_set_trueval != 1)
6845 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
6847 return ret;
6850 /* This function expands the atomic exchange operation:
6851 atomically store VAL in MEM and return the previous value in MEM.
6853 MEMMODEL is the memory model variant to use.
6854 TARGET is an optional place to stick the return value. */
6857 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6859 machine_mode mode = GET_MODE (mem);
6860 rtx ret;
6862 /* If loads are not atomic for the required size and we are not called to
6863 provide a __sync builtin, do not do anything so that we stay consistent
6864 with atomic loads of the same size. */
6865 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6866 return NULL_RTX;
6868 ret = maybe_emit_atomic_exchange (target, mem, val, model);
6870 /* Next try a compare-and-swap loop for the exchange. */
6871 if (!ret)
6872 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6874 return ret;
6877 /* This function expands the atomic compare exchange operation:
6879 *PTARGET_BOOL is an optional place to store the boolean success/failure.
6880 *PTARGET_OVAL is an optional place to store the old value from memory.
6881 Both target parameters may be NULL or const0_rtx to indicate that we do
6882 not care about that return value. Both target parameters are updated on
6883 success to the actual location of the corresponding result.
6885 MEMMODEL is the memory model variant to use.
6887 The return value of the function is true for success. */
6889 bool
6890 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
6891 rtx mem, rtx expected, rtx desired,
6892 bool is_weak, enum memmodel succ_model,
6893 enum memmodel fail_model)
6895 machine_mode mode = GET_MODE (mem);
6896 class expand_operand ops[8];
6897 enum insn_code icode;
6898 rtx target_oval, target_bool = NULL_RTX;
6899 rtx libfunc;
6901 /* If loads are not atomic for the required size and we are not called to
6902 provide a __sync builtin, do not do anything so that we stay consistent
6903 with atomic loads of the same size. */
6904 if (!can_atomic_load_p (mode) && !is_mm_sync (succ_model))
6905 return false;
6907 /* Load expected into a register for the compare and swap. */
6908 if (MEM_P (expected))
6909 expected = copy_to_reg (expected);
6911 /* Make sure we always have some place to put the return oldval.
6912 Further, make sure that place is distinct from the input expected,
6913 just in case we need that path down below. */
6914 if (ptarget_oval && *ptarget_oval == const0_rtx)
6915 ptarget_oval = NULL;
6917 if (ptarget_oval == NULL
6918 || (target_oval = *ptarget_oval) == NULL
6919 || reg_overlap_mentioned_p (expected, target_oval))
6920 target_oval = gen_reg_rtx (mode);
6922 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
6923 if (icode != CODE_FOR_nothing)
6925 machine_mode bool_mode = insn_data[icode].operand[0].mode;
6927 if (ptarget_bool && *ptarget_bool == const0_rtx)
6928 ptarget_bool = NULL;
6930 /* Make sure we always have a place for the bool operand. */
6931 if (ptarget_bool == NULL
6932 || (target_bool = *ptarget_bool) == NULL
6933 || GET_MODE (target_bool) != bool_mode)
6934 target_bool = gen_reg_rtx (bool_mode);
6936 /* Emit the compare_and_swap. */
6937 create_output_operand (&ops[0], target_bool, bool_mode);
6938 create_output_operand (&ops[1], target_oval, mode);
6939 create_fixed_operand (&ops[2], mem);
6940 create_input_operand (&ops[3], expected, mode);
6941 create_input_operand (&ops[4], desired, mode);
6942 create_integer_operand (&ops[5], is_weak);
6943 create_integer_operand (&ops[6], succ_model);
6944 create_integer_operand (&ops[7], fail_model);
6945 if (maybe_expand_insn (icode, 8, ops))
6947 /* Return success/failure. */
6948 target_bool = ops[0].value;
6949 target_oval = ops[1].value;
6950 goto success;
6954 /* Otherwise fall back to the original __sync_val_compare_and_swap
6955 which is always seq-cst. */
6956 icode = optab_handler (sync_compare_and_swap_optab, mode);
6957 if (icode != CODE_FOR_nothing)
6959 rtx cc_reg;
6961 create_output_operand (&ops[0], target_oval, mode);
6962 create_fixed_operand (&ops[1], mem);
6963 create_input_operand (&ops[2], expected, mode);
6964 create_input_operand (&ops[3], desired, mode);
6965 if (!maybe_expand_insn (icode, 4, ops))
6966 return false;
6968 target_oval = ops[0].value;
6970 /* If the caller isn't interested in the boolean return value,
6971 skip the computation of it. */
6972 if (ptarget_bool == NULL)
6973 goto success;
6975 /* Otherwise, work out if the compare-and-swap succeeded. */
6976 cc_reg = NULL_RTX;
6977 if (have_insn_for (COMPARE, CCmode))
6978 note_stores (get_last_insn (), find_cc_set, &cc_reg);
6979 if (cc_reg)
6981 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
6982 const0_rtx, VOIDmode, 0, 1);
6983 goto success;
6985 goto success_bool_from_val;
6988 /* Also check for library support for __sync_val_compare_and_swap. */
6989 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
6990 if (libfunc != NULL)
6992 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6993 rtx target = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6994 mode, addr, ptr_mode,
6995 expected, mode, desired, mode);
6996 emit_move_insn (target_oval, target);
6998 /* Compute the boolean return value only if requested. */
6999 if (ptarget_bool)
7000 goto success_bool_from_val;
7001 else
7002 goto success;
7005 /* Failure. */
7006 return false;
7008 success_bool_from_val:
7009 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7010 expected, VOIDmode, 1, 1);
7011 success:
7012 /* Make sure that the oval output winds up where the caller asked. */
7013 if (ptarget_oval)
7014 *ptarget_oval = target_oval;
7015 if (ptarget_bool)
7016 *ptarget_bool = target_bool;
7017 return true;
7020 /* Generate asm volatile("" : : : "memory") as the memory blockage. */
7022 static void
7023 expand_asm_memory_blockage (void)
7025 rtx asm_op, clob;
7027 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
7028 rtvec_alloc (0), rtvec_alloc (0),
7029 rtvec_alloc (0), UNKNOWN_LOCATION);
7030 MEM_VOLATILE_P (asm_op) = 1;
7032 clob = gen_rtx_SCRATCH (VOIDmode);
7033 clob = gen_rtx_MEM (BLKmode, clob);
7034 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7036 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7039 /* Do not propagate memory accesses across this point. */
7041 static void
7042 expand_memory_blockage (void)
7044 if (targetm.have_memory_blockage ())
7045 emit_insn (targetm.gen_memory_blockage ());
7046 else
7047 expand_asm_memory_blockage ();
7050 /* Generate asm volatile("" : : : "memory") as a memory blockage, at the
7051 same time clobbering the register set specified by REGS. */
7053 void
7054 expand_asm_reg_clobber_mem_blockage (HARD_REG_SET regs)
7056 rtx asm_op, clob_mem;
7058 unsigned int num_of_regs = 0;
7059 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7060 if (TEST_HARD_REG_BIT (regs, i))
7061 num_of_regs++;
7063 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
7064 rtvec_alloc (0), rtvec_alloc (0),
7065 rtvec_alloc (0), UNKNOWN_LOCATION);
7066 MEM_VOLATILE_P (asm_op) = 1;
7068 rtvec v = rtvec_alloc (num_of_regs + 2);
7070 clob_mem = gen_rtx_SCRATCH (VOIDmode);
7071 clob_mem = gen_rtx_MEM (BLKmode, clob_mem);
7072 clob_mem = gen_rtx_CLOBBER (VOIDmode, clob_mem);
7074 RTVEC_ELT (v, 0) = asm_op;
7075 RTVEC_ELT (v, 1) = clob_mem;
7077 if (num_of_regs > 0)
7079 unsigned int j = 2;
7080 for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7081 if (TEST_HARD_REG_BIT (regs, i))
7083 RTVEC_ELT (v, j) = gen_rtx_CLOBBER (VOIDmode, regno_reg_rtx[i]);
7084 j++;
7086 gcc_assert (j == (num_of_regs + 2));
7089 emit_insn (gen_rtx_PARALLEL (VOIDmode, v));
7092 /* This routine will either emit the mem_thread_fence pattern or issue a
7093 sync_synchronize to generate a fence for memory model MEMMODEL. */
7095 void
7096 expand_mem_thread_fence (enum memmodel model)
7098 if (is_mm_relaxed (model))
7099 return;
7100 if (targetm.have_mem_thread_fence ())
7102 emit_insn (targetm.gen_mem_thread_fence (GEN_INT (model)));
7103 expand_memory_blockage ();
7105 else if (targetm.have_memory_barrier ())
7106 emit_insn (targetm.gen_memory_barrier ());
7107 else if (synchronize_libfunc != NULL_RTX)
7108 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode);
7109 else
7110 expand_memory_blockage ();
7113 /* Emit a signal fence with given memory model. */
7115 void
7116 expand_mem_signal_fence (enum memmodel model)
7118 /* No machine barrier is required to implement a signal fence, but
7119 a compiler memory barrier must be issued, except for relaxed MM. */
7120 if (!is_mm_relaxed (model))
7121 expand_memory_blockage ();
7124 /* This function expands the atomic load operation:
7125 return the atomically loaded value in MEM.
7127 MEMMODEL is the memory model variant to use.
7128 TARGET is an option place to stick the return value. */
7131 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7133 machine_mode mode = GET_MODE (mem);
7134 enum insn_code icode;
7136 /* If the target supports the load directly, great. */
7137 icode = direct_optab_handler (atomic_load_optab, mode);
7138 if (icode != CODE_FOR_nothing)
7140 class expand_operand ops[3];
7141 rtx_insn *last = get_last_insn ();
7142 if (is_mm_seq_cst (model))
7143 expand_memory_blockage ();
7145 create_output_operand (&ops[0], target, mode);
7146 create_fixed_operand (&ops[1], mem);
7147 create_integer_operand (&ops[2], model);
7148 if (maybe_expand_insn (icode, 3, ops))
7150 if (!is_mm_relaxed (model))
7151 expand_memory_blockage ();
7152 return ops[0].value;
7154 delete_insns_since (last);
7157 /* If the size of the object is greater than word size on this target,
7158 then we assume that a load will not be atomic. We could try to
7159 emulate a load with a compare-and-swap operation, but the store that
7160 doing this could result in would be incorrect if this is a volatile
7161 atomic load or targetting read-only-mapped memory. */
7162 if (maybe_gt (GET_MODE_PRECISION (mode), BITS_PER_WORD))
7163 /* If there is no atomic load, leave the library call. */
7164 return NULL_RTX;
7166 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7167 if (!target || target == const0_rtx)
7168 target = gen_reg_rtx (mode);
7170 /* For SEQ_CST, emit a barrier before the load. */
7171 if (is_mm_seq_cst (model))
7172 expand_mem_thread_fence (model);
7174 emit_move_insn (target, mem);
7176 /* Emit the appropriate barrier after the load. */
7177 expand_mem_thread_fence (model);
7179 return target;
7182 /* This function expands the atomic store operation:
7183 Atomically store VAL in MEM.
7184 MEMMODEL is the memory model variant to use.
7185 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7186 function returns const0_rtx if a pattern was emitted. */
7189 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7191 machine_mode mode = GET_MODE (mem);
7192 enum insn_code icode;
7193 class expand_operand ops[3];
7195 /* If the target supports the store directly, great. */
7196 icode = direct_optab_handler (atomic_store_optab, mode);
7197 if (icode != CODE_FOR_nothing)
7199 rtx_insn *last = get_last_insn ();
7200 if (!is_mm_relaxed (model))
7201 expand_memory_blockage ();
7202 create_fixed_operand (&ops[0], mem);
7203 create_input_operand (&ops[1], val, mode);
7204 create_integer_operand (&ops[2], model);
7205 if (maybe_expand_insn (icode, 3, ops))
7207 if (is_mm_seq_cst (model))
7208 expand_memory_blockage ();
7209 return const0_rtx;
7211 delete_insns_since (last);
7214 /* If using __sync_lock_release is a viable alternative, try it.
7215 Note that this will not be set to true if we are expanding a generic
7216 __atomic_store_n. */
7217 if (use_release)
7219 icode = direct_optab_handler (sync_lock_release_optab, mode);
7220 if (icode != CODE_FOR_nothing)
7222 create_fixed_operand (&ops[0], mem);
7223 create_input_operand (&ops[1], const0_rtx, mode);
7224 if (maybe_expand_insn (icode, 2, ops))
7226 /* lock_release is only a release barrier. */
7227 if (is_mm_seq_cst (model))
7228 expand_mem_thread_fence (model);
7229 return const0_rtx;
7234 /* If the size of the object is greater than word size on this target,
7235 a default store will not be atomic. */
7236 if (maybe_gt (GET_MODE_PRECISION (mode), BITS_PER_WORD))
7238 /* If loads are atomic or we are called to provide a __sync builtin,
7239 we can try a atomic_exchange and throw away the result. Otherwise,
7240 don't do anything so that we do not create an inconsistency between
7241 loads and stores. */
7242 if (can_atomic_load_p (mode) || is_mm_sync (model))
7244 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7245 if (!target)
7246 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem,
7247 val);
7248 if (target)
7249 return const0_rtx;
7251 return NULL_RTX;
7254 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7255 expand_mem_thread_fence (model);
7257 emit_move_insn (mem, val);
7259 /* For SEQ_CST, also emit a barrier after the store. */
7260 if (is_mm_seq_cst (model))
7261 expand_mem_thread_fence (model);
7263 return const0_rtx;
7267 /* Structure containing the pointers and values required to process the
7268 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7270 struct atomic_op_functions
7272 direct_optab mem_fetch_before;
7273 direct_optab mem_fetch_after;
7274 direct_optab mem_no_result;
7275 optab fetch_before;
7276 optab fetch_after;
7277 direct_optab no_result;
7278 enum rtx_code reverse_code;
7282 /* Fill in structure pointed to by OP with the various optab entries for an
7283 operation of type CODE. */
7285 static void
7286 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7288 gcc_assert (op!= NULL);
7290 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7291 in the source code during compilation, and the optab entries are not
7292 computable until runtime. Fill in the values at runtime. */
7293 switch (code)
7295 case PLUS:
7296 op->mem_fetch_before = atomic_fetch_add_optab;
7297 op->mem_fetch_after = atomic_add_fetch_optab;
7298 op->mem_no_result = atomic_add_optab;
7299 op->fetch_before = sync_old_add_optab;
7300 op->fetch_after = sync_new_add_optab;
7301 op->no_result = sync_add_optab;
7302 op->reverse_code = MINUS;
7303 break;
7304 case MINUS:
7305 op->mem_fetch_before = atomic_fetch_sub_optab;
7306 op->mem_fetch_after = atomic_sub_fetch_optab;
7307 op->mem_no_result = atomic_sub_optab;
7308 op->fetch_before = sync_old_sub_optab;
7309 op->fetch_after = sync_new_sub_optab;
7310 op->no_result = sync_sub_optab;
7311 op->reverse_code = PLUS;
7312 break;
7313 case XOR:
7314 op->mem_fetch_before = atomic_fetch_xor_optab;
7315 op->mem_fetch_after = atomic_xor_fetch_optab;
7316 op->mem_no_result = atomic_xor_optab;
7317 op->fetch_before = sync_old_xor_optab;
7318 op->fetch_after = sync_new_xor_optab;
7319 op->no_result = sync_xor_optab;
7320 op->reverse_code = XOR;
7321 break;
7322 case AND:
7323 op->mem_fetch_before = atomic_fetch_and_optab;
7324 op->mem_fetch_after = atomic_and_fetch_optab;
7325 op->mem_no_result = atomic_and_optab;
7326 op->fetch_before = sync_old_and_optab;
7327 op->fetch_after = sync_new_and_optab;
7328 op->no_result = sync_and_optab;
7329 op->reverse_code = UNKNOWN;
7330 break;
7331 case IOR:
7332 op->mem_fetch_before = atomic_fetch_or_optab;
7333 op->mem_fetch_after = atomic_or_fetch_optab;
7334 op->mem_no_result = atomic_or_optab;
7335 op->fetch_before = sync_old_ior_optab;
7336 op->fetch_after = sync_new_ior_optab;
7337 op->no_result = sync_ior_optab;
7338 op->reverse_code = UNKNOWN;
7339 break;
7340 case NOT:
7341 op->mem_fetch_before = atomic_fetch_nand_optab;
7342 op->mem_fetch_after = atomic_nand_fetch_optab;
7343 op->mem_no_result = atomic_nand_optab;
7344 op->fetch_before = sync_old_nand_optab;
7345 op->fetch_after = sync_new_nand_optab;
7346 op->no_result = sync_nand_optab;
7347 op->reverse_code = UNKNOWN;
7348 break;
7349 default:
7350 gcc_unreachable ();
7354 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7355 using memory order MODEL. If AFTER is true the operation needs to return
7356 the value of *MEM after the operation, otherwise the previous value.
7357 TARGET is an optional place to place the result. The result is unused if
7358 it is const0_rtx.
7359 Return the result if there is a better sequence, otherwise NULL_RTX. */
7361 static rtx
7362 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7363 enum memmodel model, bool after)
7365 /* If the value is prefetched, or not used, it may be possible to replace
7366 the sequence with a native exchange operation. */
7367 if (!after || target == const0_rtx)
7369 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7370 if (code == AND && val == const0_rtx)
7372 if (target == const0_rtx)
7373 target = gen_reg_rtx (GET_MODE (mem));
7374 return maybe_emit_atomic_exchange (target, mem, val, model);
7377 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7378 if (code == IOR && val == constm1_rtx)
7380 if (target == const0_rtx)
7381 target = gen_reg_rtx (GET_MODE (mem));
7382 return maybe_emit_atomic_exchange (target, mem, val, model);
7386 return NULL_RTX;
7389 /* Try to emit an instruction for a specific operation varaition.
7390 OPTAB contains the OP functions.
7391 TARGET is an optional place to return the result. const0_rtx means unused.
7392 MEM is the memory location to operate on.
7393 VAL is the value to use in the operation.
7394 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7395 MODEL is the memory model, if used.
7396 AFTER is true if the returned result is the value after the operation. */
7398 static rtx
7399 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7400 rtx val, bool use_memmodel, enum memmodel model, bool after)
7402 machine_mode mode = GET_MODE (mem);
7403 class expand_operand ops[4];
7404 enum insn_code icode;
7405 int op_counter = 0;
7406 int num_ops;
7408 /* Check to see if there is a result returned. */
7409 if (target == const0_rtx)
7411 if (use_memmodel)
7413 icode = direct_optab_handler (optab->mem_no_result, mode);
7414 create_integer_operand (&ops[2], model);
7415 num_ops = 3;
7417 else
7419 icode = direct_optab_handler (optab->no_result, mode);
7420 num_ops = 2;
7423 /* Otherwise, we need to generate a result. */
7424 else
7426 if (use_memmodel)
7428 icode = direct_optab_handler (after ? optab->mem_fetch_after
7429 : optab->mem_fetch_before, mode);
7430 create_integer_operand (&ops[3], model);
7431 num_ops = 4;
7433 else
7435 icode = optab_handler (after ? optab->fetch_after
7436 : optab->fetch_before, mode);
7437 num_ops = 3;
7439 create_output_operand (&ops[op_counter++], target, mode);
7441 if (icode == CODE_FOR_nothing)
7442 return NULL_RTX;
7444 create_fixed_operand (&ops[op_counter++], mem);
7445 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7446 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7448 if (maybe_expand_insn (icode, num_ops, ops))
7449 return (target == const0_rtx ? const0_rtx : ops[0].value);
7451 return NULL_RTX;
7455 /* This function expands an atomic fetch_OP or OP_fetch operation:
7456 TARGET is an option place to stick the return value. const0_rtx indicates
7457 the result is unused.
7458 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7459 CODE is the operation being performed (OP)
7460 MEMMODEL is the memory model variant to use.
7461 AFTER is true to return the result of the operation (OP_fetch).
7462 AFTER is false to return the value before the operation (fetch_OP).
7464 This function will *only* generate instructions if there is a direct
7465 optab. No compare and swap loops or libcalls will be generated. */
7467 static rtx
7468 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7469 enum rtx_code code, enum memmodel model,
7470 bool after)
7472 machine_mode mode = GET_MODE (mem);
7473 struct atomic_op_functions optab;
7474 rtx result;
7475 bool unused_result = (target == const0_rtx);
7477 get_atomic_op_for_code (&optab, code);
7479 /* Check to see if there are any better instructions. */
7480 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7481 if (result)
7482 return result;
7484 /* Check for the case where the result isn't used and try those patterns. */
7485 if (unused_result)
7487 /* Try the memory model variant first. */
7488 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7489 if (result)
7490 return result;
7492 /* Next try the old style withuot a memory model. */
7493 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7494 if (result)
7495 return result;
7497 /* There is no no-result pattern, so try patterns with a result. */
7498 target = NULL_RTX;
7501 /* Try the __atomic version. */
7502 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7503 if (result)
7504 return result;
7506 /* Try the older __sync version. */
7507 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7508 if (result)
7509 return result;
7511 /* If the fetch value can be calculated from the other variation of fetch,
7512 try that operation. */
7513 if (after || unused_result || optab.reverse_code != UNKNOWN)
7515 /* Try the __atomic version, then the older __sync version. */
7516 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7517 if (!result)
7518 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7520 if (result)
7522 /* If the result isn't used, no need to do compensation code. */
7523 if (unused_result)
7524 return result;
7526 /* Issue compensation code. Fetch_after == fetch_before OP val.
7527 Fetch_before == after REVERSE_OP val. */
7528 if (!after)
7529 code = optab.reverse_code;
7530 if (code == NOT)
7532 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
7533 true, OPTAB_LIB_WIDEN);
7534 result = expand_simple_unop (mode, NOT, result, target, true);
7536 else
7537 result = expand_simple_binop (mode, code, result, val, target,
7538 true, OPTAB_LIB_WIDEN);
7539 return result;
7543 /* No direct opcode can be generated. */
7544 return NULL_RTX;
7549 /* This function expands an atomic fetch_OP or OP_fetch operation:
7550 TARGET is an option place to stick the return value. const0_rtx indicates
7551 the result is unused.
7552 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7553 CODE is the operation being performed (OP)
7554 MEMMODEL is the memory model variant to use.
7555 AFTER is true to return the result of the operation (OP_fetch).
7556 AFTER is false to return the value before the operation (fetch_OP). */
7558 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7559 enum memmodel model, bool after)
7561 machine_mode mode = GET_MODE (mem);
7562 rtx result;
7563 bool unused_result = (target == const0_rtx);
7565 /* If loads are not atomic for the required size and we are not called to
7566 provide a __sync builtin, do not do anything so that we stay consistent
7567 with atomic loads of the same size. */
7568 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
7569 return NULL_RTX;
7571 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
7572 after);
7574 if (result)
7575 return result;
7577 /* Add/sub can be implemented by doing the reverse operation with -(val). */
7578 if (code == PLUS || code == MINUS)
7580 rtx tmp;
7581 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
7583 start_sequence ();
7584 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
7585 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
7586 model, after);
7587 if (result)
7589 /* PLUS worked so emit the insns and return. */
7590 tmp = get_insns ();
7591 end_sequence ();
7592 emit_insn (tmp);
7593 return result;
7596 /* PLUS did not work, so throw away the negation code and continue. */
7597 end_sequence ();
7600 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
7601 if (!can_compare_and_swap_p (mode, false))
7603 rtx libfunc;
7604 bool fixup = false;
7605 enum rtx_code orig_code = code;
7606 struct atomic_op_functions optab;
7608 get_atomic_op_for_code (&optab, code);
7609 libfunc = optab_libfunc (after ? optab.fetch_after
7610 : optab.fetch_before, mode);
7611 if (libfunc == NULL
7612 && (after || unused_result || optab.reverse_code != UNKNOWN))
7614 fixup = true;
7615 if (!after)
7616 code = optab.reverse_code;
7617 libfunc = optab_libfunc (after ? optab.fetch_before
7618 : optab.fetch_after, mode);
7620 if (libfunc != NULL)
7622 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7623 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
7624 addr, ptr_mode, val, mode);
7626 if (!unused_result && fixup)
7627 result = expand_simple_binop (mode, code, result, val, target,
7628 true, OPTAB_LIB_WIDEN);
7629 return result;
7632 /* We need the original code for any further attempts. */
7633 code = orig_code;
7636 /* If nothing else has succeeded, default to a compare and swap loop. */
7637 if (can_compare_and_swap_p (mode, true))
7639 rtx_insn *insn;
7640 rtx t0 = gen_reg_rtx (mode), t1;
7642 start_sequence ();
7644 /* If the result is used, get a register for it. */
7645 if (!unused_result)
7647 if (!target || !register_operand (target, mode))
7648 target = gen_reg_rtx (mode);
7649 /* If fetch_before, copy the value now. */
7650 if (!after)
7651 emit_move_insn (target, t0);
7653 else
7654 target = const0_rtx;
7656 t1 = t0;
7657 if (code == NOT)
7659 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
7660 true, OPTAB_LIB_WIDEN);
7661 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
7663 else
7664 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
7665 OPTAB_LIB_WIDEN);
7667 /* For after, copy the value now. */
7668 if (!unused_result && after)
7669 emit_move_insn (target, t1);
7670 insn = get_insns ();
7671 end_sequence ();
7673 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7674 return target;
7677 return NULL_RTX;
7680 /* Return true if OPERAND is suitable for operand number OPNO of
7681 instruction ICODE. */
7683 bool
7684 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
7686 return (!insn_data[(int) icode].operand[opno].predicate
7687 || (insn_data[(int) icode].operand[opno].predicate
7688 (operand, insn_data[(int) icode].operand[opno].mode)));
7691 /* TARGET is a target of a multiword operation that we are going to
7692 implement as a series of word-mode operations. Return true if
7693 TARGET is suitable for this purpose. */
7695 bool
7696 valid_multiword_target_p (rtx target)
7698 machine_mode mode;
7699 int i, size;
7701 mode = GET_MODE (target);
7702 if (!GET_MODE_SIZE (mode).is_constant (&size))
7703 return false;
7704 for (i = 0; i < size; i += UNITS_PER_WORD)
7705 if (!validate_subreg (word_mode, mode, target, i))
7706 return false;
7707 return true;
7710 /* Make OP describe an input operand that has value INTVAL and that has
7711 no inherent mode. This function should only be used for operands that
7712 are always expand-time constants. The backend may request that INTVAL
7713 be copied into a different kind of rtx, but it must specify the mode
7714 of that rtx if so. */
7716 void
7717 create_integer_operand (class expand_operand *op, poly_int64 intval)
7719 create_expand_operand (op, EXPAND_INTEGER,
7720 gen_int_mode (intval, MAX_MODE_INT),
7721 VOIDmode, false, intval);
7724 /* Like maybe_legitimize_operand, but do not change the code of the
7725 current rtx value. */
7727 static bool
7728 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
7729 class expand_operand *op)
7731 /* See if the operand matches in its current form. */
7732 if (insn_operand_matches (icode, opno, op->value))
7733 return true;
7735 /* If the operand is a memory whose address has no side effects,
7736 try forcing the address into a non-virtual pseudo register.
7737 The check for side effects is important because copy_to_mode_reg
7738 cannot handle things like auto-modified addresses. */
7739 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
7741 rtx addr, mem;
7743 mem = op->value;
7744 addr = XEXP (mem, 0);
7745 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
7746 && !side_effects_p (addr))
7748 rtx_insn *last;
7749 machine_mode mode;
7751 last = get_last_insn ();
7752 mode = get_address_mode (mem);
7753 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
7754 if (insn_operand_matches (icode, opno, mem))
7756 op->value = mem;
7757 return true;
7759 delete_insns_since (last);
7763 return false;
7766 /* Try to make OP match operand OPNO of instruction ICODE. Return true
7767 on success, storing the new operand value back in OP. */
7769 static bool
7770 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
7771 class expand_operand *op)
7773 machine_mode mode, imode, tmode;
7775 mode = op->mode;
7776 switch (op->type)
7778 case EXPAND_FIXED:
7780 temporary_volatile_ok v (true);
7781 return maybe_legitimize_operand_same_code (icode, opno, op);
7784 case EXPAND_OUTPUT:
7785 gcc_assert (mode != VOIDmode);
7786 if (op->value
7787 && op->value != const0_rtx
7788 && GET_MODE (op->value) == mode
7789 && maybe_legitimize_operand_same_code (icode, opno, op))
7790 return true;
7792 op->value = gen_reg_rtx (mode);
7793 op->target = 0;
7794 break;
7796 case EXPAND_INPUT:
7797 input:
7798 gcc_assert (mode != VOIDmode);
7799 gcc_assert (GET_MODE (op->value) == VOIDmode
7800 || GET_MODE (op->value) == mode);
7801 if (maybe_legitimize_operand_same_code (icode, opno, op))
7802 return true;
7804 op->value = copy_to_mode_reg (mode, op->value);
7805 break;
7807 case EXPAND_CONVERT_TO:
7808 gcc_assert (mode != VOIDmode);
7809 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
7810 goto input;
7812 case EXPAND_CONVERT_FROM:
7813 if (GET_MODE (op->value) != VOIDmode)
7814 mode = GET_MODE (op->value);
7815 else
7816 /* The caller must tell us what mode this value has. */
7817 gcc_assert (mode != VOIDmode);
7819 imode = insn_data[(int) icode].operand[opno].mode;
7820 tmode = (VECTOR_MODE_P (imode) && !VECTOR_MODE_P (mode)
7821 ? GET_MODE_INNER (imode) : imode);
7822 if (tmode != VOIDmode && tmode != mode)
7824 op->value = convert_modes (tmode, mode, op->value, op->unsigned_p);
7825 mode = tmode;
7827 if (imode != VOIDmode && imode != mode)
7829 gcc_assert (VECTOR_MODE_P (imode) && !VECTOR_MODE_P (mode));
7830 op->value = expand_vector_broadcast (imode, op->value);
7831 mode = imode;
7833 goto input;
7835 case EXPAND_ADDRESS:
7836 op->value = convert_memory_address (as_a <scalar_int_mode> (mode),
7837 op->value);
7838 goto input;
7840 case EXPAND_INTEGER:
7841 mode = insn_data[(int) icode].operand[opno].mode;
7842 if (mode != VOIDmode
7843 && known_eq (trunc_int_for_mode (op->int_value, mode),
7844 op->int_value))
7846 op->value = gen_int_mode (op->int_value, mode);
7847 goto input;
7849 break;
7851 return insn_operand_matches (icode, opno, op->value);
7854 /* Make OP describe an input operand that should have the same value
7855 as VALUE, after any mode conversion that the target might request.
7856 TYPE is the type of VALUE. */
7858 void
7859 create_convert_operand_from_type (class expand_operand *op,
7860 rtx value, tree type)
7862 create_convert_operand_from (op, value, TYPE_MODE (type),
7863 TYPE_UNSIGNED (type));
7866 /* Return true if the requirements on operands OP1 and OP2 of instruction
7867 ICODE are similar enough for the result of legitimizing OP1 to be
7868 reusable for OP2. OPNO1 and OPNO2 are the operand numbers associated
7869 with OP1 and OP2 respectively. */
7871 static inline bool
7872 can_reuse_operands_p (enum insn_code icode,
7873 unsigned int opno1, unsigned int opno2,
7874 const class expand_operand *op1,
7875 const class expand_operand *op2)
7877 /* Check requirements that are common to all types. */
7878 if (op1->type != op2->type
7879 || op1->mode != op2->mode
7880 || (insn_data[(int) icode].operand[opno1].mode
7881 != insn_data[(int) icode].operand[opno2].mode))
7882 return false;
7884 /* Check the requirements for specific types. */
7885 switch (op1->type)
7887 case EXPAND_OUTPUT:
7888 /* Outputs must remain distinct. */
7889 return false;
7891 case EXPAND_FIXED:
7892 case EXPAND_INPUT:
7893 case EXPAND_ADDRESS:
7894 case EXPAND_INTEGER:
7895 return true;
7897 case EXPAND_CONVERT_TO:
7898 case EXPAND_CONVERT_FROM:
7899 return op1->unsigned_p == op2->unsigned_p;
7901 gcc_unreachable ();
7904 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
7905 of instruction ICODE. Return true on success, leaving the new operand
7906 values in the OPS themselves. Emit no code on failure. */
7908 bool
7909 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
7910 unsigned int nops, class expand_operand *ops)
7912 rtx_insn *last = get_last_insn ();
7913 rtx *orig_values = XALLOCAVEC (rtx, nops);
7914 for (unsigned int i = 0; i < nops; i++)
7916 orig_values[i] = ops[i].value;
7918 /* First try reusing the result of an earlier legitimization.
7919 This avoids duplicate rtl and ensures that tied operands
7920 remain tied.
7922 This search is linear, but NOPS is bounded at compile time
7923 to a small number (current a single digit). */
7924 unsigned int j = 0;
7925 for (; j < i; ++j)
7926 if (can_reuse_operands_p (icode, opno + j, opno + i, &ops[j], &ops[i])
7927 && rtx_equal_p (orig_values[j], orig_values[i])
7928 && ops[j].value
7929 && insn_operand_matches (icode, opno + i, ops[j].value))
7931 ops[i].value = copy_rtx (ops[j].value);
7932 break;
7935 /* Otherwise try legitimizing the operand on its own. */
7936 if (j == i && !maybe_legitimize_operand (icode, opno + i, &ops[i]))
7938 delete_insns_since (last);
7939 return false;
7942 return true;
7945 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
7946 as its operands. Return the instruction pattern on success,
7947 and emit any necessary set-up code. Return null and emit no
7948 code on failure. */
7950 rtx_insn *
7951 maybe_gen_insn (enum insn_code icode, unsigned int nops,
7952 class expand_operand *ops)
7954 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
7955 if (!maybe_legitimize_operands (icode, 0, nops, ops))
7956 return NULL;
7958 switch (nops)
7960 case 1:
7961 return GEN_FCN (icode) (ops[0].value);
7962 case 2:
7963 return GEN_FCN (icode) (ops[0].value, ops[1].value);
7964 case 3:
7965 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
7966 case 4:
7967 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7968 ops[3].value);
7969 case 5:
7970 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7971 ops[3].value, ops[4].value);
7972 case 6:
7973 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7974 ops[3].value, ops[4].value, ops[5].value);
7975 case 7:
7976 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7977 ops[3].value, ops[4].value, ops[5].value,
7978 ops[6].value);
7979 case 8:
7980 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7981 ops[3].value, ops[4].value, ops[5].value,
7982 ops[6].value, ops[7].value);
7983 case 9:
7984 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7985 ops[3].value, ops[4].value, ops[5].value,
7986 ops[6].value, ops[7].value, ops[8].value);
7988 gcc_unreachable ();
7991 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
7992 as its operands. Return true on success and emit no code on failure. */
7994 bool
7995 maybe_expand_insn (enum insn_code icode, unsigned int nops,
7996 class expand_operand *ops)
7998 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7999 if (pat)
8001 emit_insn (pat);
8002 return true;
8004 return false;
8007 /* Like maybe_expand_insn, but for jumps. */
8009 bool
8010 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8011 class expand_operand *ops)
8013 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
8014 if (pat)
8016 emit_jump_insn (pat);
8017 return true;
8019 return false;
8022 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8023 as its operands. */
8025 void
8026 expand_insn (enum insn_code icode, unsigned int nops,
8027 class expand_operand *ops)
8029 if (!maybe_expand_insn (icode, nops, ops))
8030 gcc_unreachable ();
8033 /* Like expand_insn, but for jumps. */
8035 void
8036 expand_jump_insn (enum insn_code icode, unsigned int nops,
8037 class expand_operand *ops)
8039 if (!maybe_expand_jump_insn (icode, nops, ops))
8040 gcc_unreachable ();