]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/optabs.c
Add gen_(const_)vec_duplicate helpers
[thirdparty/gcc.git] / gcc / optabs.c
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "memmodel.h"
29 #include "predict.h"
30 #include "tm_p.h"
31 #include "expmed.h"
32 #include "optabs.h"
33 #include "emit-rtl.h"
34 #include "recog.h"
35 #include "diagnostic-core.h"
36
37 /* Include insn-config.h before expr.h so that HAVE_conditional_move
38 is properly defined. */
39 #include "stor-layout.h"
40 #include "except.h"
41 #include "dojump.h"
42 #include "explow.h"
43 #include "expr.h"
44 #include "optabs-tree.h"
45 #include "libfuncs.h"
46
47 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
48 machine_mode *);
49 static rtx expand_unop_direct (machine_mode, optab, rtx, rtx, int);
50 static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
51
52 /* Debug facility for use in GDB. */
53 void debug_optab_libfuncs (void);
54 \f
55 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
56 the result of operation CODE applied to OP0 (and OP1 if it is a binary
57 operation).
58
59 If the last insn does not set TARGET, don't do anything, but return 1.
60
61 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
62 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
63 try again, ensuring that TARGET is not one of the operands. */
64
65 static int
66 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
67 {
68 rtx_insn *last_insn;
69 rtx set;
70 rtx note;
71
72 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
73
74 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
75 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
76 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
77 && GET_RTX_CLASS (code) != RTX_COMPARE
78 && GET_RTX_CLASS (code) != RTX_UNARY)
79 return 1;
80
81 if (GET_CODE (target) == ZERO_EXTRACT)
82 return 1;
83
84 for (last_insn = insns;
85 NEXT_INSN (last_insn) != NULL_RTX;
86 last_insn = NEXT_INSN (last_insn))
87 ;
88
89 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
90 a value changing in the insn, so the note would be invalid for CSE. */
91 if (reg_overlap_mentioned_p (target, op0)
92 || (op1 && reg_overlap_mentioned_p (target, op1)))
93 {
94 if (MEM_P (target)
95 && (rtx_equal_p (target, op0)
96 || (op1 && rtx_equal_p (target, op1))))
97 {
98 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
99 over expanding it as temp = MEM op X, MEM = temp. If the target
100 supports MEM = MEM op X instructions, it is sometimes too hard
101 to reconstruct that form later, especially if X is also a memory,
102 and due to multiple occurrences of addresses the address might
103 be forced into register unnecessarily.
104 Note that not emitting the REG_EQUIV note might inhibit
105 CSE in some cases. */
106 set = single_set (last_insn);
107 if (set
108 && GET_CODE (SET_SRC (set)) == code
109 && MEM_P (SET_DEST (set))
110 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
111 || (op1 && rtx_equal_p (SET_DEST (set),
112 XEXP (SET_SRC (set), 1)))))
113 return 1;
114 }
115 return 0;
116 }
117
118 set = set_for_reg_notes (last_insn);
119 if (set == NULL_RTX)
120 return 1;
121
122 if (! rtx_equal_p (SET_DEST (set), target)
123 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
124 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
125 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
126 return 1;
127
128 if (GET_RTX_CLASS (code) == RTX_UNARY)
129 switch (code)
130 {
131 case FFS:
132 case CLZ:
133 case CTZ:
134 case CLRSB:
135 case POPCOUNT:
136 case PARITY:
137 case BSWAP:
138 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
139 {
140 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
141 if (GET_MODE_UNIT_SIZE (GET_MODE (op0))
142 > GET_MODE_UNIT_SIZE (GET_MODE (target)))
143 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
144 note, GET_MODE (op0));
145 else
146 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
147 note, GET_MODE (op0));
148 break;
149 }
150 /* FALLTHRU */
151 default:
152 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
153 break;
154 }
155 else
156 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
157
158 set_unique_reg_note (last_insn, REG_EQUAL, note);
159
160 return 1;
161 }
162 \f
163 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
164 for a widening operation would be. In most cases this would be OP0, but if
165 that's a constant it'll be VOIDmode, which isn't useful. */
166
167 static machine_mode
168 widened_mode (machine_mode to_mode, rtx op0, rtx op1)
169 {
170 machine_mode m0 = GET_MODE (op0);
171 machine_mode m1 = GET_MODE (op1);
172 machine_mode result;
173
174 if (m0 == VOIDmode && m1 == VOIDmode)
175 return to_mode;
176 else if (m0 == VOIDmode || GET_MODE_UNIT_SIZE (m0) < GET_MODE_UNIT_SIZE (m1))
177 result = m1;
178 else
179 result = m0;
180
181 if (GET_MODE_UNIT_SIZE (result) > GET_MODE_UNIT_SIZE (to_mode))
182 return to_mode;
183
184 return result;
185 }
186 \f
187 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
188 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
189 not actually do a sign-extend or zero-extend, but can leave the
190 higher-order bits of the result rtx undefined, for example, in the case
191 of logical operations, but not right shifts. */
192
193 static rtx
194 widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
195 int unsignedp, int no_extend)
196 {
197 rtx result;
198 scalar_int_mode int_mode;
199
200 /* If we don't have to extend and this is a constant, return it. */
201 if (no_extend && GET_MODE (op) == VOIDmode)
202 return op;
203
204 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
205 extend since it will be more efficient to do so unless the signedness of
206 a promoted object differs from our extension. */
207 if (! no_extend
208 || !is_a <scalar_int_mode> (mode, &int_mode)
209 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
210 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
211 return convert_modes (mode, oldmode, op, unsignedp);
212
213 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
214 SUBREG. */
215 if (GET_MODE_SIZE (int_mode) <= UNITS_PER_WORD)
216 return gen_lowpart (int_mode, force_reg (GET_MODE (op), op));
217
218 /* Otherwise, get an object of MODE, clobber it, and set the low-order
219 part to OP. */
220
221 result = gen_reg_rtx (int_mode);
222 emit_clobber (result);
223 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
224 return result;
225 }
226 \f
227 /* Expand vector widening operations.
228
229 There are two different classes of operations handled here:
230 1) Operations whose result is wider than all the arguments to the operation.
231 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
232 In this case OP0 and optionally OP1 would be initialized,
233 but WIDE_OP wouldn't (not relevant for this case).
234 2) Operations whose result is of the same size as the last argument to the
235 operation, but wider than all the other arguments to the operation.
236 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
237 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
238
239 E.g, when called to expand the following operations, this is how
240 the arguments will be initialized:
241 nops OP0 OP1 WIDE_OP
242 widening-sum 2 oprnd0 - oprnd1
243 widening-dot-product 3 oprnd0 oprnd1 oprnd2
244 widening-mult 2 oprnd0 oprnd1 -
245 type-promotion (vec-unpack) 1 oprnd0 - - */
246
247 rtx
248 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
249 rtx target, int unsignedp)
250 {
251 struct expand_operand eops[4];
252 tree oprnd0, oprnd1, oprnd2;
253 machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
254 optab widen_pattern_optab;
255 enum insn_code icode;
256 int nops = TREE_CODE_LENGTH (ops->code);
257 int op;
258
259 oprnd0 = ops->op0;
260 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
261 widen_pattern_optab =
262 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
263 if (ops->code == WIDEN_MULT_PLUS_EXPR
264 || ops->code == WIDEN_MULT_MINUS_EXPR)
265 icode = find_widening_optab_handler (widen_pattern_optab,
266 TYPE_MODE (TREE_TYPE (ops->op2)),
267 tmode0, 0);
268 else
269 icode = optab_handler (widen_pattern_optab, tmode0);
270 gcc_assert (icode != CODE_FOR_nothing);
271
272 if (nops >= 2)
273 {
274 oprnd1 = ops->op1;
275 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
276 }
277
278 /* The last operand is of a wider mode than the rest of the operands. */
279 if (nops == 2)
280 wmode = tmode1;
281 else if (nops == 3)
282 {
283 gcc_assert (tmode1 == tmode0);
284 gcc_assert (op1);
285 oprnd2 = ops->op2;
286 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
287 }
288
289 op = 0;
290 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
291 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
292 if (op1)
293 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
294 if (wide_op)
295 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
296 expand_insn (icode, op, eops);
297 return eops[0].value;
298 }
299
300 /* Generate code to perform an operation specified by TERNARY_OPTAB
301 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
302
303 UNSIGNEDP is for the case where we have to widen the operands
304 to perform the operation. It says to use zero-extension.
305
306 If TARGET is nonzero, the value
307 is generated there, if it is convenient to do so.
308 In all cases an rtx is returned for the locus of the value;
309 this may or may not be TARGET. */
310
311 rtx
312 expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
313 rtx op1, rtx op2, rtx target, int unsignedp)
314 {
315 struct expand_operand ops[4];
316 enum insn_code icode = optab_handler (ternary_optab, mode);
317
318 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
319
320 create_output_operand (&ops[0], target, mode);
321 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
322 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
323 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
324 expand_insn (icode, 4, ops);
325 return ops[0].value;
326 }
327
328
329 /* Like expand_binop, but return a constant rtx if the result can be
330 calculated at compile time. The arguments and return value are
331 otherwise the same as for expand_binop. */
332
333 rtx
334 simplify_expand_binop (machine_mode mode, optab binoptab,
335 rtx op0, rtx op1, rtx target, int unsignedp,
336 enum optab_methods methods)
337 {
338 if (CONSTANT_P (op0) && CONSTANT_P (op1))
339 {
340 rtx x = simplify_binary_operation (optab_to_code (binoptab),
341 mode, op0, op1);
342 if (x)
343 return x;
344 }
345
346 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
347 }
348
349 /* Like simplify_expand_binop, but always put the result in TARGET.
350 Return true if the expansion succeeded. */
351
352 bool
353 force_expand_binop (machine_mode mode, optab binoptab,
354 rtx op0, rtx op1, rtx target, int unsignedp,
355 enum optab_methods methods)
356 {
357 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
358 target, unsignedp, methods);
359 if (x == 0)
360 return false;
361 if (x != target)
362 emit_move_insn (target, x);
363 return true;
364 }
365
366 /* Create a new vector value in VMODE with all elements set to OP. The
367 mode of OP must be the element mode of VMODE. If OP is a constant,
368 then the return value will be a constant. */
369
370 static rtx
371 expand_vector_broadcast (machine_mode vmode, rtx op)
372 {
373 enum insn_code icode;
374 rtvec vec;
375 rtx ret;
376 int i, n;
377
378 gcc_checking_assert (VECTOR_MODE_P (vmode));
379
380 if (CONSTANT_P (op))
381 return gen_const_vec_duplicate (vmode, op);
382
383 /* ??? If the target doesn't have a vec_init, then we have no easy way
384 of performing this operation. Most of this sort of generic support
385 is hidden away in the vector lowering support in gimple. */
386 icode = convert_optab_handler (vec_init_optab, vmode,
387 GET_MODE_INNER (vmode));
388 if (icode == CODE_FOR_nothing)
389 return NULL;
390
391 n = GET_MODE_NUNITS (vmode);
392 vec = rtvec_alloc (n);
393 for (i = 0; i < n; ++i)
394 RTVEC_ELT (vec, i) = op;
395 ret = gen_reg_rtx (vmode);
396 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
397
398 return ret;
399 }
400
401 /* This subroutine of expand_doubleword_shift handles the cases in which
402 the effective shift value is >= BITS_PER_WORD. The arguments and return
403 value are the same as for the parent routine, except that SUPERWORD_OP1
404 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
405 INTO_TARGET may be null if the caller has decided to calculate it. */
406
407 static bool
408 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
409 rtx outof_target, rtx into_target,
410 int unsignedp, enum optab_methods methods)
411 {
412 if (into_target != 0)
413 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
414 into_target, unsignedp, methods))
415 return false;
416
417 if (outof_target != 0)
418 {
419 /* For a signed right shift, we must fill OUTOF_TARGET with copies
420 of the sign bit, otherwise we must fill it with zeros. */
421 if (binoptab != ashr_optab)
422 emit_move_insn (outof_target, CONST0_RTX (word_mode));
423 else
424 if (!force_expand_binop (word_mode, binoptab,
425 outof_input, GEN_INT (BITS_PER_WORD - 1),
426 outof_target, unsignedp, methods))
427 return false;
428 }
429 return true;
430 }
431
432 /* This subroutine of expand_doubleword_shift handles the cases in which
433 the effective shift value is < BITS_PER_WORD. The arguments and return
434 value are the same as for the parent routine. */
435
436 static bool
437 expand_subword_shift (scalar_int_mode op1_mode, optab binoptab,
438 rtx outof_input, rtx into_input, rtx op1,
439 rtx outof_target, rtx into_target,
440 int unsignedp, enum optab_methods methods,
441 unsigned HOST_WIDE_INT shift_mask)
442 {
443 optab reverse_unsigned_shift, unsigned_shift;
444 rtx tmp, carries;
445
446 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
447 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
448
449 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
450 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
451 the opposite direction to BINOPTAB. */
452 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
453 {
454 carries = outof_input;
455 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
456 op1_mode), op1_mode);
457 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
458 0, true, methods);
459 }
460 else
461 {
462 /* We must avoid shifting by BITS_PER_WORD bits since that is either
463 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
464 has unknown behavior. Do a single shift first, then shift by the
465 remainder. It's OK to use ~OP1 as the remainder if shift counts
466 are truncated to the mode size. */
467 carries = expand_binop (word_mode, reverse_unsigned_shift,
468 outof_input, const1_rtx, 0, unsignedp, methods);
469 if (shift_mask == BITS_PER_WORD - 1)
470 {
471 tmp = immed_wide_int_const
472 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
473 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
474 0, true, methods);
475 }
476 else
477 {
478 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
479 op1_mode), op1_mode);
480 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
481 0, true, methods);
482 }
483 }
484 if (tmp == 0 || carries == 0)
485 return false;
486 carries = expand_binop (word_mode, reverse_unsigned_shift,
487 carries, tmp, 0, unsignedp, methods);
488 if (carries == 0)
489 return false;
490
491 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
492 so the result can go directly into INTO_TARGET if convenient. */
493 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
494 into_target, unsignedp, methods);
495 if (tmp == 0)
496 return false;
497
498 /* Now OR in the bits carried over from OUTOF_INPUT. */
499 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
500 into_target, unsignedp, methods))
501 return false;
502
503 /* Use a standard word_mode shift for the out-of half. */
504 if (outof_target != 0)
505 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
506 outof_target, unsignedp, methods))
507 return false;
508
509 return true;
510 }
511
512
513 /* Try implementing expand_doubleword_shift using conditional moves.
514 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
515 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
516 are the shift counts to use in the former and latter case. All other
517 arguments are the same as the parent routine. */
518
519 static bool
520 expand_doubleword_shift_condmove (scalar_int_mode op1_mode, optab binoptab,
521 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
522 rtx outof_input, rtx into_input,
523 rtx subword_op1, rtx superword_op1,
524 rtx outof_target, rtx into_target,
525 int unsignedp, enum optab_methods methods,
526 unsigned HOST_WIDE_INT shift_mask)
527 {
528 rtx outof_superword, into_superword;
529
530 /* Put the superword version of the output into OUTOF_SUPERWORD and
531 INTO_SUPERWORD. */
532 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
533 if (outof_target != 0 && subword_op1 == superword_op1)
534 {
535 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
536 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
537 into_superword = outof_target;
538 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
539 outof_superword, 0, unsignedp, methods))
540 return false;
541 }
542 else
543 {
544 into_superword = gen_reg_rtx (word_mode);
545 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
546 outof_superword, into_superword,
547 unsignedp, methods))
548 return false;
549 }
550
551 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
552 if (!expand_subword_shift (op1_mode, binoptab,
553 outof_input, into_input, subword_op1,
554 outof_target, into_target,
555 unsignedp, methods, shift_mask))
556 return false;
557
558 /* Select between them. Do the INTO half first because INTO_SUPERWORD
559 might be the current value of OUTOF_TARGET. */
560 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
561 into_target, into_superword, word_mode, false))
562 return false;
563
564 if (outof_target != 0)
565 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
566 outof_target, outof_superword,
567 word_mode, false))
568 return false;
569
570 return true;
571 }
572
573 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
574 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
575 input operand; the shift moves bits in the direction OUTOF_INPUT->
576 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
577 of the target. OP1 is the shift count and OP1_MODE is its mode.
578 If OP1 is constant, it will have been truncated as appropriate
579 and is known to be nonzero.
580
581 If SHIFT_MASK is zero, the result of word shifts is undefined when the
582 shift count is outside the range [0, BITS_PER_WORD). This routine must
583 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
584
585 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
586 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
587 fill with zeros or sign bits as appropriate.
588
589 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
590 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
591 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
592 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
593 are undefined.
594
595 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
596 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
597 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
598 function wants to calculate it itself.
599
600 Return true if the shift could be successfully synthesized. */
601
602 static bool
603 expand_doubleword_shift (scalar_int_mode op1_mode, optab binoptab,
604 rtx outof_input, rtx into_input, rtx op1,
605 rtx outof_target, rtx into_target,
606 int unsignedp, enum optab_methods methods,
607 unsigned HOST_WIDE_INT shift_mask)
608 {
609 rtx superword_op1, tmp, cmp1, cmp2;
610 enum rtx_code cmp_code;
611
612 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
613 fill the result with sign or zero bits as appropriate. If so, the value
614 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
615 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
616 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
617
618 This isn't worthwhile for constant shifts since the optimizers will
619 cope better with in-range shift counts. */
620 if (shift_mask >= BITS_PER_WORD
621 && outof_target != 0
622 && !CONSTANT_P (op1))
623 {
624 if (!expand_doubleword_shift (op1_mode, binoptab,
625 outof_input, into_input, op1,
626 0, into_target,
627 unsignedp, methods, shift_mask))
628 return false;
629 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
630 outof_target, unsignedp, methods))
631 return false;
632 return true;
633 }
634
635 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
636 is true when the effective shift value is less than BITS_PER_WORD.
637 Set SUPERWORD_OP1 to the shift count that should be used to shift
638 OUTOF_INPUT into INTO_TARGET when the condition is false. */
639 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
640 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
641 {
642 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
643 is a subword shift count. */
644 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
645 0, true, methods);
646 cmp2 = CONST0_RTX (op1_mode);
647 cmp_code = EQ;
648 superword_op1 = op1;
649 }
650 else
651 {
652 /* Set CMP1 to OP1 - BITS_PER_WORD. */
653 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
654 0, true, methods);
655 cmp2 = CONST0_RTX (op1_mode);
656 cmp_code = LT;
657 superword_op1 = cmp1;
658 }
659 if (cmp1 == 0)
660 return false;
661
662 /* If we can compute the condition at compile time, pick the
663 appropriate subroutine. */
664 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
665 if (tmp != 0 && CONST_INT_P (tmp))
666 {
667 if (tmp == const0_rtx)
668 return expand_superword_shift (binoptab, outof_input, superword_op1,
669 outof_target, into_target,
670 unsignedp, methods);
671 else
672 return expand_subword_shift (op1_mode, binoptab,
673 outof_input, into_input, op1,
674 outof_target, into_target,
675 unsignedp, methods, shift_mask);
676 }
677
678 /* Try using conditional moves to generate straight-line code. */
679 if (HAVE_conditional_move)
680 {
681 rtx_insn *start = get_last_insn ();
682 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
683 cmp_code, cmp1, cmp2,
684 outof_input, into_input,
685 op1, superword_op1,
686 outof_target, into_target,
687 unsignedp, methods, shift_mask))
688 return true;
689 delete_insns_since (start);
690 }
691
692 /* As a last resort, use branches to select the correct alternative. */
693 rtx_code_label *subword_label = gen_label_rtx ();
694 rtx_code_label *done_label = gen_label_rtx ();
695
696 NO_DEFER_POP;
697 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
698 0, 0, subword_label,
699 profile_probability::uninitialized ());
700 OK_DEFER_POP;
701
702 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
703 outof_target, into_target,
704 unsignedp, methods))
705 return false;
706
707 emit_jump_insn (targetm.gen_jump (done_label));
708 emit_barrier ();
709 emit_label (subword_label);
710
711 if (!expand_subword_shift (op1_mode, binoptab,
712 outof_input, into_input, op1,
713 outof_target, into_target,
714 unsignedp, methods, shift_mask))
715 return false;
716
717 emit_label (done_label);
718 return true;
719 }
720 \f
721 /* Subroutine of expand_binop. Perform a double word multiplication of
722 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
723 as the target's word_mode. This function return NULL_RTX if anything
724 goes wrong, in which case it may have already emitted instructions
725 which need to be deleted.
726
727 If we want to multiply two two-word values and have normal and widening
728 multiplies of single-word values, we can do this with three smaller
729 multiplications.
730
731 The multiplication proceeds as follows:
732 _______________________
733 [__op0_high_|__op0_low__]
734 _______________________
735 * [__op1_high_|__op1_low__]
736 _______________________________________________
737 _______________________
738 (1) [__op0_low__*__op1_low__]
739 _______________________
740 (2a) [__op0_low__*__op1_high_]
741 _______________________
742 (2b) [__op0_high_*__op1_low__]
743 _______________________
744 (3) [__op0_high_*__op1_high_]
745
746
747 This gives a 4-word result. Since we are only interested in the
748 lower 2 words, partial result (3) and the upper words of (2a) and
749 (2b) don't need to be calculated. Hence (2a) and (2b) can be
750 calculated using non-widening multiplication.
751
752 (1), however, needs to be calculated with an unsigned widening
753 multiplication. If this operation is not directly supported we
754 try using a signed widening multiplication and adjust the result.
755 This adjustment works as follows:
756
757 If both operands are positive then no adjustment is needed.
758
759 If the operands have different signs, for example op0_low < 0 and
760 op1_low >= 0, the instruction treats the most significant bit of
761 op0_low as a sign bit instead of a bit with significance
762 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
763 with 2**BITS_PER_WORD - op0_low, and two's complements the
764 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
765 the result.
766
767 Similarly, if both operands are negative, we need to add
768 (op0_low + op1_low) * 2**BITS_PER_WORD.
769
770 We use a trick to adjust quickly. We logically shift op0_low right
771 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
772 op0_high (op1_high) before it is used to calculate 2b (2a). If no
773 logical shift exists, we do an arithmetic right shift and subtract
774 the 0 or -1. */
775
776 static rtx
777 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
778 bool umulp, enum optab_methods methods)
779 {
780 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
781 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
782 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
783 rtx product, adjust, product_high, temp;
784
785 rtx op0_high = operand_subword_force (op0, high, mode);
786 rtx op0_low = operand_subword_force (op0, low, mode);
787 rtx op1_high = operand_subword_force (op1, high, mode);
788 rtx op1_low = operand_subword_force (op1, low, mode);
789
790 /* If we're using an unsigned multiply to directly compute the product
791 of the low-order words of the operands and perform any required
792 adjustments of the operands, we begin by trying two more multiplications
793 and then computing the appropriate sum.
794
795 We have checked above that the required addition is provided.
796 Full-word addition will normally always succeed, especially if
797 it is provided at all, so we don't worry about its failure. The
798 multiplication may well fail, however, so we do handle that. */
799
800 if (!umulp)
801 {
802 /* ??? This could be done with emit_store_flag where available. */
803 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
804 NULL_RTX, 1, methods);
805 if (temp)
806 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
807 NULL_RTX, 0, OPTAB_DIRECT);
808 else
809 {
810 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
811 NULL_RTX, 0, methods);
812 if (!temp)
813 return NULL_RTX;
814 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
815 NULL_RTX, 0, OPTAB_DIRECT);
816 }
817
818 if (!op0_high)
819 return NULL_RTX;
820 }
821
822 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
823 NULL_RTX, 0, OPTAB_DIRECT);
824 if (!adjust)
825 return NULL_RTX;
826
827 /* OP0_HIGH should now be dead. */
828
829 if (!umulp)
830 {
831 /* ??? This could be done with emit_store_flag where available. */
832 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
833 NULL_RTX, 1, methods);
834 if (temp)
835 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
836 NULL_RTX, 0, OPTAB_DIRECT);
837 else
838 {
839 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
840 NULL_RTX, 0, methods);
841 if (!temp)
842 return NULL_RTX;
843 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
844 NULL_RTX, 0, OPTAB_DIRECT);
845 }
846
847 if (!op1_high)
848 return NULL_RTX;
849 }
850
851 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
852 NULL_RTX, 0, OPTAB_DIRECT);
853 if (!temp)
854 return NULL_RTX;
855
856 /* OP1_HIGH should now be dead. */
857
858 adjust = expand_binop (word_mode, add_optab, adjust, temp,
859 NULL_RTX, 0, OPTAB_DIRECT);
860
861 if (target && !REG_P (target))
862 target = NULL_RTX;
863
864 if (umulp)
865 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
866 target, 1, OPTAB_DIRECT);
867 else
868 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
869 target, 1, OPTAB_DIRECT);
870
871 if (!product)
872 return NULL_RTX;
873
874 product_high = operand_subword (product, high, 1, mode);
875 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
876 NULL_RTX, 0, OPTAB_DIRECT);
877 emit_move_insn (product_high, adjust);
878 return product;
879 }
880 \f
881 /* Wrapper around expand_binop which takes an rtx code to specify
882 the operation to perform, not an optab pointer. All other
883 arguments are the same. */
884 rtx
885 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
886 rtx op1, rtx target, int unsignedp,
887 enum optab_methods methods)
888 {
889 optab binop = code_to_optab (code);
890 gcc_assert (binop);
891
892 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
893 }
894
895 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
896 binop. Order them according to commutative_operand_precedence and, if
897 possible, try to put TARGET or a pseudo first. */
898 static bool
899 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
900 {
901 int op0_prec = commutative_operand_precedence (op0);
902 int op1_prec = commutative_operand_precedence (op1);
903
904 if (op0_prec < op1_prec)
905 return true;
906
907 if (op0_prec > op1_prec)
908 return false;
909
910 /* With equal precedence, both orders are ok, but it is better if the
911 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
912 if (target == 0 || REG_P (target))
913 return (REG_P (op1) && !REG_P (op0)) || target == op1;
914 else
915 return rtx_equal_p (op1, target);
916 }
917
918 /* Return true if BINOPTAB implements a shift operation. */
919
920 static bool
921 shift_optab_p (optab binoptab)
922 {
923 switch (optab_to_code (binoptab))
924 {
925 case ASHIFT:
926 case SS_ASHIFT:
927 case US_ASHIFT:
928 case ASHIFTRT:
929 case LSHIFTRT:
930 case ROTATE:
931 case ROTATERT:
932 return true;
933
934 default:
935 return false;
936 }
937 }
938
939 /* Return true if BINOPTAB implements a commutative binary operation. */
940
941 static bool
942 commutative_optab_p (optab binoptab)
943 {
944 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
945 || binoptab == smul_widen_optab
946 || binoptab == umul_widen_optab
947 || binoptab == smul_highpart_optab
948 || binoptab == umul_highpart_optab);
949 }
950
951 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
952 optimizing, and if the operand is a constant that costs more than
953 1 instruction, force the constant into a register and return that
954 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
955
956 static rtx
957 avoid_expensive_constant (machine_mode mode, optab binoptab,
958 int opn, rtx x, bool unsignedp)
959 {
960 bool speed = optimize_insn_for_speed_p ();
961
962 if (mode != VOIDmode
963 && optimize
964 && CONSTANT_P (x)
965 && (rtx_cost (x, mode, optab_to_code (binoptab), opn, speed)
966 > set_src_cost (x, mode, speed)))
967 {
968 if (CONST_INT_P (x))
969 {
970 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
971 if (intval != INTVAL (x))
972 x = GEN_INT (intval);
973 }
974 else
975 x = convert_modes (mode, VOIDmode, x, unsignedp);
976 x = force_reg (mode, x);
977 }
978 return x;
979 }
980
981 /* Helper function for expand_binop: handle the case where there
982 is an insn that directly implements the indicated operation.
983 Returns null if this is not possible. */
984 static rtx
985 expand_binop_directly (machine_mode mode, optab binoptab,
986 rtx op0, rtx op1,
987 rtx target, int unsignedp, enum optab_methods methods,
988 rtx_insn *last)
989 {
990 machine_mode from_mode = widened_mode (mode, op0, op1);
991 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
992 from_mode, 1);
993 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
994 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
995 machine_mode mode0, mode1, tmp_mode;
996 struct expand_operand ops[3];
997 bool commutative_p;
998 rtx_insn *pat;
999 rtx xop0 = op0, xop1 = op1;
1000 bool canonicalize_op1 = false;
1001
1002 /* If it is a commutative operator and the modes would match
1003 if we would swap the operands, we can save the conversions. */
1004 commutative_p = commutative_optab_p (binoptab);
1005 if (commutative_p
1006 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1007 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1008 std::swap (xop0, xop1);
1009
1010 /* If we are optimizing, force expensive constants into a register. */
1011 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1012 if (!shift_optab_p (binoptab))
1013 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1014 else
1015 /* Shifts and rotates often use a different mode for op1 from op0;
1016 for VOIDmode constants we don't know the mode, so force it
1017 to be canonicalized using convert_modes. */
1018 canonicalize_op1 = true;
1019
1020 /* In case the insn wants input operands in modes different from
1021 those of the actual operands, convert the operands. It would
1022 seem that we don't need to convert CONST_INTs, but we do, so
1023 that they're properly zero-extended, sign-extended or truncated
1024 for their mode. */
1025
1026 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1027 if (xmode0 != VOIDmode && xmode0 != mode0)
1028 {
1029 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1030 mode0 = xmode0;
1031 }
1032
1033 mode1 = ((GET_MODE (xop1) != VOIDmode || canonicalize_op1)
1034 ? GET_MODE (xop1) : mode);
1035 if (xmode1 != VOIDmode && xmode1 != mode1)
1036 {
1037 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1038 mode1 = xmode1;
1039 }
1040
1041 /* If operation is commutative,
1042 try to make the first operand a register.
1043 Even better, try to make it the same as the target.
1044 Also try to make the last operand a constant. */
1045 if (commutative_p
1046 && swap_commutative_operands_with_target (target, xop0, xop1))
1047 std::swap (xop0, xop1);
1048
1049 /* Now, if insn's predicates don't allow our operands, put them into
1050 pseudo regs. */
1051
1052 if (binoptab == vec_pack_trunc_optab
1053 || binoptab == vec_pack_usat_optab
1054 || binoptab == vec_pack_ssat_optab
1055 || binoptab == vec_pack_ufix_trunc_optab
1056 || binoptab == vec_pack_sfix_trunc_optab)
1057 {
1058 /* The mode of the result is different then the mode of the
1059 arguments. */
1060 tmp_mode = insn_data[(int) icode].operand[0].mode;
1061 if (VECTOR_MODE_P (mode)
1062 && GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1063 {
1064 delete_insns_since (last);
1065 return NULL_RTX;
1066 }
1067 }
1068 else
1069 tmp_mode = mode;
1070
1071 create_output_operand (&ops[0], target, tmp_mode);
1072 create_input_operand (&ops[1], xop0, mode0);
1073 create_input_operand (&ops[2], xop1, mode1);
1074 pat = maybe_gen_insn (icode, 3, ops);
1075 if (pat)
1076 {
1077 /* If PAT is composed of more than one insn, try to add an appropriate
1078 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1079 operand, call expand_binop again, this time without a target. */
1080 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1081 && ! add_equal_note (pat, ops[0].value,
1082 optab_to_code (binoptab),
1083 ops[1].value, ops[2].value))
1084 {
1085 delete_insns_since (last);
1086 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1087 unsignedp, methods);
1088 }
1089
1090 emit_insn (pat);
1091 return ops[0].value;
1092 }
1093 delete_insns_since (last);
1094 return NULL_RTX;
1095 }
1096
1097 /* Generate code to perform an operation specified by BINOPTAB
1098 on operands OP0 and OP1, with result having machine-mode MODE.
1099
1100 UNSIGNEDP is for the case where we have to widen the operands
1101 to perform the operation. It says to use zero-extension.
1102
1103 If TARGET is nonzero, the value
1104 is generated there, if it is convenient to do so.
1105 In all cases an rtx is returned for the locus of the value;
1106 this may or may not be TARGET. */
1107
1108 rtx
1109 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1110 rtx target, int unsignedp, enum optab_methods methods)
1111 {
1112 enum optab_methods next_methods
1113 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1114 ? OPTAB_WIDEN : methods);
1115 enum mode_class mclass;
1116 machine_mode wider_mode;
1117 scalar_int_mode int_mode;
1118 rtx libfunc;
1119 rtx temp;
1120 rtx_insn *entry_last = get_last_insn ();
1121 rtx_insn *last;
1122
1123 mclass = GET_MODE_CLASS (mode);
1124
1125 /* If subtracting an integer constant, convert this into an addition of
1126 the negated constant. */
1127
1128 if (binoptab == sub_optab && CONST_INT_P (op1))
1129 {
1130 op1 = negate_rtx (mode, op1);
1131 binoptab = add_optab;
1132 }
1133 /* For shifts, constant invalid op1 might be expanded from different
1134 mode than MODE. As those are invalid, force them to a register
1135 to avoid further problems during expansion. */
1136 else if (CONST_INT_P (op1)
1137 && shift_optab_p (binoptab)
1138 && UINTVAL (op1) >= GET_MODE_BITSIZE (GET_MODE_INNER (mode)))
1139 {
1140 op1 = gen_int_mode (INTVAL (op1), GET_MODE_INNER (mode));
1141 op1 = force_reg (GET_MODE_INNER (mode), op1);
1142 }
1143
1144 /* Record where to delete back to if we backtrack. */
1145 last = get_last_insn ();
1146
1147 /* If we can do it with a three-operand insn, do so. */
1148
1149 if (methods != OPTAB_MUST_WIDEN
1150 && find_widening_optab_handler (binoptab, mode,
1151 widened_mode (mode, op0, op1), 1)
1152 != CODE_FOR_nothing)
1153 {
1154 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1155 unsignedp, methods, last);
1156 if (temp)
1157 return temp;
1158 }
1159
1160 /* If we were trying to rotate, and that didn't work, try rotating
1161 the other direction before falling back to shifts and bitwise-or. */
1162 if (((binoptab == rotl_optab
1163 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1164 || (binoptab == rotr_optab
1165 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1166 && is_int_mode (mode, &int_mode))
1167 {
1168 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1169 rtx newop1;
1170 unsigned int bits = GET_MODE_PRECISION (int_mode);
1171
1172 if (CONST_INT_P (op1))
1173 newop1 = GEN_INT (bits - INTVAL (op1));
1174 else if (targetm.shift_truncation_mask (int_mode) == bits - 1)
1175 newop1 = negate_rtx (GET_MODE (op1), op1);
1176 else
1177 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1178 gen_int_mode (bits, GET_MODE (op1)), op1,
1179 NULL_RTX, unsignedp, OPTAB_DIRECT);
1180
1181 temp = expand_binop_directly (int_mode, otheroptab, op0, newop1,
1182 target, unsignedp, methods, last);
1183 if (temp)
1184 return temp;
1185 }
1186
1187 /* If this is a multiply, see if we can do a widening operation that
1188 takes operands of this mode and makes a wider mode. */
1189
1190 if (binoptab == smul_optab
1191 && GET_MODE_2XWIDER_MODE (mode).exists (&wider_mode)
1192 && (convert_optab_handler ((unsignedp
1193 ? umul_widen_optab
1194 : smul_widen_optab),
1195 wider_mode, mode) != CODE_FOR_nothing))
1196 {
1197 temp = expand_binop (wider_mode,
1198 unsignedp ? umul_widen_optab : smul_widen_optab,
1199 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1200
1201 if (temp != 0)
1202 {
1203 if (GET_MODE_CLASS (mode) == MODE_INT
1204 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1205 return gen_lowpart (mode, temp);
1206 else
1207 return convert_to_mode (mode, temp, unsignedp);
1208 }
1209 }
1210
1211 /* If this is a vector shift by a scalar, see if we can do a vector
1212 shift by a vector. If so, broadcast the scalar into a vector. */
1213 if (mclass == MODE_VECTOR_INT)
1214 {
1215 optab otheroptab = unknown_optab;
1216
1217 if (binoptab == ashl_optab)
1218 otheroptab = vashl_optab;
1219 else if (binoptab == ashr_optab)
1220 otheroptab = vashr_optab;
1221 else if (binoptab == lshr_optab)
1222 otheroptab = vlshr_optab;
1223 else if (binoptab == rotl_optab)
1224 otheroptab = vrotl_optab;
1225 else if (binoptab == rotr_optab)
1226 otheroptab = vrotr_optab;
1227
1228 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1229 {
1230 /* The scalar may have been extended to be too wide. Truncate
1231 it back to the proper size to fit in the broadcast vector. */
1232 scalar_mode inner_mode = GET_MODE_INNER (mode);
1233 if (!CONST_INT_P (op1)
1234 && (GET_MODE_BITSIZE (as_a <scalar_int_mode> (GET_MODE (op1)))
1235 > GET_MODE_BITSIZE (inner_mode)))
1236 op1 = force_reg (inner_mode,
1237 simplify_gen_unary (TRUNCATE, inner_mode, op1,
1238 GET_MODE (op1)));
1239 rtx vop1 = expand_vector_broadcast (mode, op1);
1240 if (vop1)
1241 {
1242 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1243 target, unsignedp, methods, last);
1244 if (temp)
1245 return temp;
1246 }
1247 }
1248 }
1249
1250 /* Look for a wider mode of the same class for which we think we
1251 can open-code the operation. Check for a widening multiply at the
1252 wider mode as well. */
1253
1254 if (CLASS_HAS_WIDER_MODES_P (mclass)
1255 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1256 FOR_EACH_WIDER_MODE (wider_mode, mode)
1257 {
1258 machine_mode next_mode;
1259 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1260 || (binoptab == smul_optab
1261 && GET_MODE_WIDER_MODE (wider_mode).exists (&next_mode)
1262 && (find_widening_optab_handler ((unsignedp
1263 ? umul_widen_optab
1264 : smul_widen_optab),
1265 next_mode, mode, 0)
1266 != CODE_FOR_nothing)))
1267 {
1268 rtx xop0 = op0, xop1 = op1;
1269 int no_extend = 0;
1270
1271 /* For certain integer operations, we need not actually extend
1272 the narrow operands, as long as we will truncate
1273 the results to the same narrowness. */
1274
1275 if ((binoptab == ior_optab || binoptab == and_optab
1276 || binoptab == xor_optab
1277 || binoptab == add_optab || binoptab == sub_optab
1278 || binoptab == smul_optab || binoptab == ashl_optab)
1279 && mclass == MODE_INT)
1280 {
1281 no_extend = 1;
1282 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1283 xop0, unsignedp);
1284 if (binoptab != ashl_optab)
1285 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1286 xop1, unsignedp);
1287 }
1288
1289 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1290
1291 /* The second operand of a shift must always be extended. */
1292 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1293 no_extend && binoptab != ashl_optab);
1294
1295 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1296 unsignedp, OPTAB_DIRECT);
1297 if (temp)
1298 {
1299 if (mclass != MODE_INT
1300 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1301 {
1302 if (target == 0)
1303 target = gen_reg_rtx (mode);
1304 convert_move (target, temp, 0);
1305 return target;
1306 }
1307 else
1308 return gen_lowpart (mode, temp);
1309 }
1310 else
1311 delete_insns_since (last);
1312 }
1313 }
1314
1315 /* If operation is commutative,
1316 try to make the first operand a register.
1317 Even better, try to make it the same as the target.
1318 Also try to make the last operand a constant. */
1319 if (commutative_optab_p (binoptab)
1320 && swap_commutative_operands_with_target (target, op0, op1))
1321 std::swap (op0, op1);
1322
1323 /* These can be done a word at a time. */
1324 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1325 && is_int_mode (mode, &int_mode)
1326 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
1327 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1328 {
1329 int i;
1330 rtx_insn *insns;
1331
1332 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1333 won't be accurate, so use a new target. */
1334 if (target == 0
1335 || target == op0
1336 || target == op1
1337 || !valid_multiword_target_p (target))
1338 target = gen_reg_rtx (int_mode);
1339
1340 start_sequence ();
1341
1342 /* Do the actual arithmetic. */
1343 for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
1344 {
1345 rtx target_piece = operand_subword (target, i, 1, int_mode);
1346 rtx x = expand_binop (word_mode, binoptab,
1347 operand_subword_force (op0, i, int_mode),
1348 operand_subword_force (op1, i, int_mode),
1349 target_piece, unsignedp, next_methods);
1350
1351 if (x == 0)
1352 break;
1353
1354 if (target_piece != x)
1355 emit_move_insn (target_piece, x);
1356 }
1357
1358 insns = get_insns ();
1359 end_sequence ();
1360
1361 if (i == GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD)
1362 {
1363 emit_insn (insns);
1364 return target;
1365 }
1366 }
1367
1368 /* Synthesize double word shifts from single word shifts. */
1369 if ((binoptab == lshr_optab || binoptab == ashl_optab
1370 || binoptab == ashr_optab)
1371 && is_int_mode (mode, &int_mode)
1372 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1373 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
1374 && GET_MODE_PRECISION (int_mode) == GET_MODE_BITSIZE (int_mode)
1375 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1376 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1377 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1378 {
1379 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1380 scalar_int_mode op1_mode;
1381
1382 double_shift_mask = targetm.shift_truncation_mask (int_mode);
1383 shift_mask = targetm.shift_truncation_mask (word_mode);
1384 op1_mode = (GET_MODE (op1) != VOIDmode
1385 ? as_a <scalar_int_mode> (GET_MODE (op1))
1386 : word_mode);
1387
1388 /* Apply the truncation to constant shifts. */
1389 if (double_shift_mask > 0 && CONST_INT_P (op1))
1390 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1391
1392 if (op1 == CONST0_RTX (op1_mode))
1393 return op0;
1394
1395 /* Make sure that this is a combination that expand_doubleword_shift
1396 can handle. See the comments there for details. */
1397 if (double_shift_mask == 0
1398 || (shift_mask == BITS_PER_WORD - 1
1399 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1400 {
1401 rtx_insn *insns;
1402 rtx into_target, outof_target;
1403 rtx into_input, outof_input;
1404 int left_shift, outof_word;
1405
1406 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1407 won't be accurate, so use a new target. */
1408 if (target == 0
1409 || target == op0
1410 || target == op1
1411 || !valid_multiword_target_p (target))
1412 target = gen_reg_rtx (int_mode);
1413
1414 start_sequence ();
1415
1416 /* OUTOF_* is the word we are shifting bits away from, and
1417 INTO_* is the word that we are shifting bits towards, thus
1418 they differ depending on the direction of the shift and
1419 WORDS_BIG_ENDIAN. */
1420
1421 left_shift = binoptab == ashl_optab;
1422 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1423
1424 outof_target = operand_subword (target, outof_word, 1, int_mode);
1425 into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
1426
1427 outof_input = operand_subword_force (op0, outof_word, int_mode);
1428 into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
1429
1430 if (expand_doubleword_shift (op1_mode, binoptab,
1431 outof_input, into_input, op1,
1432 outof_target, into_target,
1433 unsignedp, next_methods, shift_mask))
1434 {
1435 insns = get_insns ();
1436 end_sequence ();
1437
1438 emit_insn (insns);
1439 return target;
1440 }
1441 end_sequence ();
1442 }
1443 }
1444
1445 /* Synthesize double word rotates from single word shifts. */
1446 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1447 && is_int_mode (mode, &int_mode)
1448 && CONST_INT_P (op1)
1449 && GET_MODE_PRECISION (int_mode) == 2 * BITS_PER_WORD
1450 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1451 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1452 {
1453 rtx_insn *insns;
1454 rtx into_target, outof_target;
1455 rtx into_input, outof_input;
1456 rtx inter;
1457 int shift_count, left_shift, outof_word;
1458
1459 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1460 won't be accurate, so use a new target. Do this also if target is not
1461 a REG, first because having a register instead may open optimization
1462 opportunities, and second because if target and op0 happen to be MEMs
1463 designating the same location, we would risk clobbering it too early
1464 in the code sequence we generate below. */
1465 if (target == 0
1466 || target == op0
1467 || target == op1
1468 || !REG_P (target)
1469 || !valid_multiword_target_p (target))
1470 target = gen_reg_rtx (int_mode);
1471
1472 start_sequence ();
1473
1474 shift_count = INTVAL (op1);
1475
1476 /* OUTOF_* is the word we are shifting bits away from, and
1477 INTO_* is the word that we are shifting bits towards, thus
1478 they differ depending on the direction of the shift and
1479 WORDS_BIG_ENDIAN. */
1480
1481 left_shift = (binoptab == rotl_optab);
1482 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1483
1484 outof_target = operand_subword (target, outof_word, 1, int_mode);
1485 into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
1486
1487 outof_input = operand_subword_force (op0, outof_word, int_mode);
1488 into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
1489
1490 if (shift_count == BITS_PER_WORD)
1491 {
1492 /* This is just a word swap. */
1493 emit_move_insn (outof_target, into_input);
1494 emit_move_insn (into_target, outof_input);
1495 inter = const0_rtx;
1496 }
1497 else
1498 {
1499 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1500 rtx first_shift_count, second_shift_count;
1501 optab reverse_unsigned_shift, unsigned_shift;
1502
1503 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1504 ? lshr_optab : ashl_optab);
1505
1506 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1507 ? ashl_optab : lshr_optab);
1508
1509 if (shift_count > BITS_PER_WORD)
1510 {
1511 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1512 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1513 }
1514 else
1515 {
1516 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1517 second_shift_count = GEN_INT (shift_count);
1518 }
1519
1520 into_temp1 = expand_binop (word_mode, unsigned_shift,
1521 outof_input, first_shift_count,
1522 NULL_RTX, unsignedp, next_methods);
1523 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1524 into_input, second_shift_count,
1525 NULL_RTX, unsignedp, next_methods);
1526
1527 if (into_temp1 != 0 && into_temp2 != 0)
1528 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1529 into_target, unsignedp, next_methods);
1530 else
1531 inter = 0;
1532
1533 if (inter != 0 && inter != into_target)
1534 emit_move_insn (into_target, inter);
1535
1536 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1537 into_input, first_shift_count,
1538 NULL_RTX, unsignedp, next_methods);
1539 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1540 outof_input, second_shift_count,
1541 NULL_RTX, unsignedp, next_methods);
1542
1543 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1544 inter = expand_binop (word_mode, ior_optab,
1545 outof_temp1, outof_temp2,
1546 outof_target, unsignedp, next_methods);
1547
1548 if (inter != 0 && inter != outof_target)
1549 emit_move_insn (outof_target, inter);
1550 }
1551
1552 insns = get_insns ();
1553 end_sequence ();
1554
1555 if (inter != 0)
1556 {
1557 emit_insn (insns);
1558 return target;
1559 }
1560 }
1561
1562 /* These can be done a word at a time by propagating carries. */
1563 if ((binoptab == add_optab || binoptab == sub_optab)
1564 && is_int_mode (mode, &int_mode)
1565 && GET_MODE_SIZE (int_mode) >= 2 * UNITS_PER_WORD
1566 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1567 {
1568 unsigned int i;
1569 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1570 const unsigned int nwords = GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD;
1571 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1572 rtx xop0, xop1, xtarget;
1573
1574 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1575 value is one of those, use it. Otherwise, use 1 since it is the
1576 one easiest to get. */
1577 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1578 int normalizep = STORE_FLAG_VALUE;
1579 #else
1580 int normalizep = 1;
1581 #endif
1582
1583 /* Prepare the operands. */
1584 xop0 = force_reg (int_mode, op0);
1585 xop1 = force_reg (int_mode, op1);
1586
1587 xtarget = gen_reg_rtx (int_mode);
1588
1589 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1590 target = xtarget;
1591
1592 /* Indicate for flow that the entire target reg is being set. */
1593 if (REG_P (target))
1594 emit_clobber (xtarget);
1595
1596 /* Do the actual arithmetic. */
1597 for (i = 0; i < nwords; i++)
1598 {
1599 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1600 rtx target_piece = operand_subword (xtarget, index, 1, int_mode);
1601 rtx op0_piece = operand_subword_force (xop0, index, int_mode);
1602 rtx op1_piece = operand_subword_force (xop1, index, int_mode);
1603 rtx x;
1604
1605 /* Main add/subtract of the input operands. */
1606 x = expand_binop (word_mode, binoptab,
1607 op0_piece, op1_piece,
1608 target_piece, unsignedp, next_methods);
1609 if (x == 0)
1610 break;
1611
1612 if (i + 1 < nwords)
1613 {
1614 /* Store carry from main add/subtract. */
1615 carry_out = gen_reg_rtx (word_mode);
1616 carry_out = emit_store_flag_force (carry_out,
1617 (binoptab == add_optab
1618 ? LT : GT),
1619 x, op0_piece,
1620 word_mode, 1, normalizep);
1621 }
1622
1623 if (i > 0)
1624 {
1625 rtx newx;
1626
1627 /* Add/subtract previous carry to main result. */
1628 newx = expand_binop (word_mode,
1629 normalizep == 1 ? binoptab : otheroptab,
1630 x, carry_in,
1631 NULL_RTX, 1, next_methods);
1632
1633 if (i + 1 < nwords)
1634 {
1635 /* Get out carry from adding/subtracting carry in. */
1636 rtx carry_tmp = gen_reg_rtx (word_mode);
1637 carry_tmp = emit_store_flag_force (carry_tmp,
1638 (binoptab == add_optab
1639 ? LT : GT),
1640 newx, x,
1641 word_mode, 1, normalizep);
1642
1643 /* Logical-ior the two poss. carry together. */
1644 carry_out = expand_binop (word_mode, ior_optab,
1645 carry_out, carry_tmp,
1646 carry_out, 0, next_methods);
1647 if (carry_out == 0)
1648 break;
1649 }
1650 emit_move_insn (target_piece, newx);
1651 }
1652 else
1653 {
1654 if (x != target_piece)
1655 emit_move_insn (target_piece, x);
1656 }
1657
1658 carry_in = carry_out;
1659 }
1660
1661 if (i == GET_MODE_BITSIZE (int_mode) / (unsigned) BITS_PER_WORD)
1662 {
1663 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing
1664 || ! rtx_equal_p (target, xtarget))
1665 {
1666 rtx_insn *temp = emit_move_insn (target, xtarget);
1667
1668 set_dst_reg_note (temp, REG_EQUAL,
1669 gen_rtx_fmt_ee (optab_to_code (binoptab),
1670 int_mode, copy_rtx (xop0),
1671 copy_rtx (xop1)),
1672 target);
1673 }
1674 else
1675 target = xtarget;
1676
1677 return target;
1678 }
1679
1680 else
1681 delete_insns_since (last);
1682 }
1683
1684 /* Attempt to synthesize double word multiplies using a sequence of word
1685 mode multiplications. We first attempt to generate a sequence using a
1686 more efficient unsigned widening multiply, and if that fails we then
1687 try using a signed widening multiply. */
1688
1689 if (binoptab == smul_optab
1690 && is_int_mode (mode, &int_mode)
1691 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
1692 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
1693 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
1694 {
1695 rtx product = NULL_RTX;
1696 if (widening_optab_handler (umul_widen_optab, int_mode, word_mode)
1697 != CODE_FOR_nothing)
1698 {
1699 product = expand_doubleword_mult (int_mode, op0, op1, target,
1700 true, methods);
1701 if (!product)
1702 delete_insns_since (last);
1703 }
1704
1705 if (product == NULL_RTX
1706 && (widening_optab_handler (smul_widen_optab, int_mode, word_mode)
1707 != CODE_FOR_nothing))
1708 {
1709 product = expand_doubleword_mult (int_mode, op0, op1, target,
1710 false, methods);
1711 if (!product)
1712 delete_insns_since (last);
1713 }
1714
1715 if (product != NULL_RTX)
1716 {
1717 if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
1718 {
1719 rtx_insn *move = emit_move_insn (target ? target : product,
1720 product);
1721 set_dst_reg_note (move,
1722 REG_EQUAL,
1723 gen_rtx_fmt_ee (MULT, int_mode,
1724 copy_rtx (op0),
1725 copy_rtx (op1)),
1726 target ? target : product);
1727 }
1728 return product;
1729 }
1730 }
1731
1732 /* It can't be open-coded in this mode.
1733 Use a library call if one is available and caller says that's ok. */
1734
1735 libfunc = optab_libfunc (binoptab, mode);
1736 if (libfunc
1737 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
1738 {
1739 rtx_insn *insns;
1740 rtx op1x = op1;
1741 machine_mode op1_mode = mode;
1742 rtx value;
1743
1744 start_sequence ();
1745
1746 if (shift_optab_p (binoptab))
1747 {
1748 op1_mode = targetm.libgcc_shift_count_mode ();
1749 /* Specify unsigned here,
1750 since negative shift counts are meaningless. */
1751 op1x = convert_to_mode (op1_mode, op1, 1);
1752 }
1753
1754 if (GET_MODE (op0) != VOIDmode
1755 && GET_MODE (op0) != mode)
1756 op0 = convert_to_mode (mode, op0, unsignedp);
1757
1758 /* Pass 1 for NO_QUEUE so we don't lose any increments
1759 if the libcall is cse'd or moved. */
1760 value = emit_library_call_value (libfunc,
1761 NULL_RTX, LCT_CONST, mode,
1762 op0, mode, op1x, op1_mode);
1763
1764 insns = get_insns ();
1765 end_sequence ();
1766
1767 bool trapv = trapv_binoptab_p (binoptab);
1768 target = gen_reg_rtx (mode);
1769 emit_libcall_block_1 (insns, target, value,
1770 trapv ? NULL_RTX
1771 : gen_rtx_fmt_ee (optab_to_code (binoptab),
1772 mode, op0, op1), trapv);
1773
1774 return target;
1775 }
1776
1777 delete_insns_since (last);
1778
1779 /* It can't be done in this mode. Can we do it in a wider mode? */
1780
1781 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
1782 || methods == OPTAB_MUST_WIDEN))
1783 {
1784 /* Caller says, don't even try. */
1785 delete_insns_since (entry_last);
1786 return 0;
1787 }
1788
1789 /* Compute the value of METHODS to pass to recursive calls.
1790 Don't allow widening to be tried recursively. */
1791
1792 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
1793
1794 /* Look for a wider mode of the same class for which it appears we can do
1795 the operation. */
1796
1797 if (CLASS_HAS_WIDER_MODES_P (mclass))
1798 {
1799 FOR_EACH_WIDER_MODE (wider_mode, mode)
1800 {
1801 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
1802 != CODE_FOR_nothing
1803 || (methods == OPTAB_LIB
1804 && optab_libfunc (binoptab, wider_mode)))
1805 {
1806 rtx xop0 = op0, xop1 = op1;
1807 int no_extend = 0;
1808
1809 /* For certain integer operations, we need not actually extend
1810 the narrow operands, as long as we will truncate
1811 the results to the same narrowness. */
1812
1813 if ((binoptab == ior_optab || binoptab == and_optab
1814 || binoptab == xor_optab
1815 || binoptab == add_optab || binoptab == sub_optab
1816 || binoptab == smul_optab || binoptab == ashl_optab)
1817 && mclass == MODE_INT)
1818 no_extend = 1;
1819
1820 xop0 = widen_operand (xop0, wider_mode, mode,
1821 unsignedp, no_extend);
1822
1823 /* The second operand of a shift must always be extended. */
1824 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1825 no_extend && binoptab != ashl_optab);
1826
1827 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1828 unsignedp, methods);
1829 if (temp)
1830 {
1831 if (mclass != MODE_INT
1832 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1833 {
1834 if (target == 0)
1835 target = gen_reg_rtx (mode);
1836 convert_move (target, temp, 0);
1837 return target;
1838 }
1839 else
1840 return gen_lowpart (mode, temp);
1841 }
1842 else
1843 delete_insns_since (last);
1844 }
1845 }
1846 }
1847
1848 delete_insns_since (entry_last);
1849 return 0;
1850 }
1851 \f
1852 /* Expand a binary operator which has both signed and unsigned forms.
1853 UOPTAB is the optab for unsigned operations, and SOPTAB is for
1854 signed operations.
1855
1856 If we widen unsigned operands, we may use a signed wider operation instead
1857 of an unsigned wider operation, since the result would be the same. */
1858
1859 rtx
1860 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
1861 rtx op0, rtx op1, rtx target, int unsignedp,
1862 enum optab_methods methods)
1863 {
1864 rtx temp;
1865 optab direct_optab = unsignedp ? uoptab : soptab;
1866 bool save_enable;
1867
1868 /* Do it without widening, if possible. */
1869 temp = expand_binop (mode, direct_optab, op0, op1, target,
1870 unsignedp, OPTAB_DIRECT);
1871 if (temp || methods == OPTAB_DIRECT)
1872 return temp;
1873
1874 /* Try widening to a signed int. Disable any direct use of any
1875 signed insn in the current mode. */
1876 save_enable = swap_optab_enable (soptab, mode, false);
1877
1878 temp = expand_binop (mode, soptab, op0, op1, target,
1879 unsignedp, OPTAB_WIDEN);
1880
1881 /* For unsigned operands, try widening to an unsigned int. */
1882 if (!temp && unsignedp)
1883 temp = expand_binop (mode, uoptab, op0, op1, target,
1884 unsignedp, OPTAB_WIDEN);
1885 if (temp || methods == OPTAB_WIDEN)
1886 goto egress;
1887
1888 /* Use the right width libcall if that exists. */
1889 temp = expand_binop (mode, direct_optab, op0, op1, target,
1890 unsignedp, OPTAB_LIB);
1891 if (temp || methods == OPTAB_LIB)
1892 goto egress;
1893
1894 /* Must widen and use a libcall, use either signed or unsigned. */
1895 temp = expand_binop (mode, soptab, op0, op1, target,
1896 unsignedp, methods);
1897 if (!temp && unsignedp)
1898 temp = expand_binop (mode, uoptab, op0, op1, target,
1899 unsignedp, methods);
1900
1901 egress:
1902 /* Undo the fiddling above. */
1903 if (save_enable)
1904 swap_optab_enable (soptab, mode, true);
1905 return temp;
1906 }
1907 \f
1908 /* Generate code to perform an operation specified by UNOPPTAB
1909 on operand OP0, with two results to TARG0 and TARG1.
1910 We assume that the order of the operands for the instruction
1911 is TARG0, TARG1, OP0.
1912
1913 Either TARG0 or TARG1 may be zero, but what that means is that
1914 the result is not actually wanted. We will generate it into
1915 a dummy pseudo-reg and discard it. They may not both be zero.
1916
1917 Returns 1 if this operation can be performed; 0 if not. */
1918
1919 int
1920 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
1921 int unsignedp)
1922 {
1923 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1924 enum mode_class mclass;
1925 machine_mode wider_mode;
1926 rtx_insn *entry_last = get_last_insn ();
1927 rtx_insn *last;
1928
1929 mclass = GET_MODE_CLASS (mode);
1930
1931 if (!targ0)
1932 targ0 = gen_reg_rtx (mode);
1933 if (!targ1)
1934 targ1 = gen_reg_rtx (mode);
1935
1936 /* Record where to go back to if we fail. */
1937 last = get_last_insn ();
1938
1939 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
1940 {
1941 struct expand_operand ops[3];
1942 enum insn_code icode = optab_handler (unoptab, mode);
1943
1944 create_fixed_operand (&ops[0], targ0);
1945 create_fixed_operand (&ops[1], targ1);
1946 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
1947 if (maybe_expand_insn (icode, 3, ops))
1948 return 1;
1949 }
1950
1951 /* It can't be done in this mode. Can we do it in a wider mode? */
1952
1953 if (CLASS_HAS_WIDER_MODES_P (mclass))
1954 {
1955 FOR_EACH_WIDER_MODE (wider_mode, mode)
1956 {
1957 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
1958 {
1959 rtx t0 = gen_reg_rtx (wider_mode);
1960 rtx t1 = gen_reg_rtx (wider_mode);
1961 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1962
1963 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
1964 {
1965 convert_move (targ0, t0, unsignedp);
1966 convert_move (targ1, t1, unsignedp);
1967 return 1;
1968 }
1969 else
1970 delete_insns_since (last);
1971 }
1972 }
1973 }
1974
1975 delete_insns_since (entry_last);
1976 return 0;
1977 }
1978 \f
1979 /* Generate code to perform an operation specified by BINOPTAB
1980 on operands OP0 and OP1, with two results to TARG1 and TARG2.
1981 We assume that the order of the operands for the instruction
1982 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
1983 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
1984
1985 Either TARG0 or TARG1 may be zero, but what that means is that
1986 the result is not actually wanted. We will generate it into
1987 a dummy pseudo-reg and discard it. They may not both be zero.
1988
1989 Returns 1 if this operation can be performed; 0 if not. */
1990
1991 int
1992 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
1993 int unsignedp)
1994 {
1995 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1996 enum mode_class mclass;
1997 machine_mode wider_mode;
1998 rtx_insn *entry_last = get_last_insn ();
1999 rtx_insn *last;
2000
2001 mclass = GET_MODE_CLASS (mode);
2002
2003 if (!targ0)
2004 targ0 = gen_reg_rtx (mode);
2005 if (!targ1)
2006 targ1 = gen_reg_rtx (mode);
2007
2008 /* Record where to go back to if we fail. */
2009 last = get_last_insn ();
2010
2011 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2012 {
2013 struct expand_operand ops[4];
2014 enum insn_code icode = optab_handler (binoptab, mode);
2015 machine_mode mode0 = insn_data[icode].operand[1].mode;
2016 machine_mode mode1 = insn_data[icode].operand[2].mode;
2017 rtx xop0 = op0, xop1 = op1;
2018
2019 /* If we are optimizing, force expensive constants into a register. */
2020 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2021 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2022
2023 create_fixed_operand (&ops[0], targ0);
2024 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2025 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2026 create_fixed_operand (&ops[3], targ1);
2027 if (maybe_expand_insn (icode, 4, ops))
2028 return 1;
2029 delete_insns_since (last);
2030 }
2031
2032 /* It can't be done in this mode. Can we do it in a wider mode? */
2033
2034 if (CLASS_HAS_WIDER_MODES_P (mclass))
2035 {
2036 FOR_EACH_WIDER_MODE (wider_mode, mode)
2037 {
2038 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2039 {
2040 rtx t0 = gen_reg_rtx (wider_mode);
2041 rtx t1 = gen_reg_rtx (wider_mode);
2042 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2043 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2044
2045 if (expand_twoval_binop (binoptab, cop0, cop1,
2046 t0, t1, unsignedp))
2047 {
2048 convert_move (targ0, t0, unsignedp);
2049 convert_move (targ1, t1, unsignedp);
2050 return 1;
2051 }
2052 else
2053 delete_insns_since (last);
2054 }
2055 }
2056 }
2057
2058 delete_insns_since (entry_last);
2059 return 0;
2060 }
2061
2062 /* Expand the two-valued library call indicated by BINOPTAB, but
2063 preserve only one of the values. If TARG0 is non-NULL, the first
2064 value is placed into TARG0; otherwise the second value is placed
2065 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2066 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2067 This routine assumes that the value returned by the library call is
2068 as if the return value was of an integral mode twice as wide as the
2069 mode of OP0. Returns 1 if the call was successful. */
2070
2071 bool
2072 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2073 rtx targ0, rtx targ1, enum rtx_code code)
2074 {
2075 machine_mode mode;
2076 machine_mode libval_mode;
2077 rtx libval;
2078 rtx_insn *insns;
2079 rtx libfunc;
2080
2081 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2082 gcc_assert (!targ0 != !targ1);
2083
2084 mode = GET_MODE (op0);
2085 libfunc = optab_libfunc (binoptab, mode);
2086 if (!libfunc)
2087 return false;
2088
2089 /* The value returned by the library function will have twice as
2090 many bits as the nominal MODE. */
2091 libval_mode = smallest_int_mode_for_size (2 * GET_MODE_BITSIZE (mode));
2092 start_sequence ();
2093 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2094 libval_mode,
2095 op0, mode,
2096 op1, mode);
2097 /* Get the part of VAL containing the value that we want. */
2098 libval = simplify_gen_subreg (mode, libval, libval_mode,
2099 targ0 ? 0 : GET_MODE_SIZE (mode));
2100 insns = get_insns ();
2101 end_sequence ();
2102 /* Move the into the desired location. */
2103 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2104 gen_rtx_fmt_ee (code, mode, op0, op1));
2105
2106 return true;
2107 }
2108
2109 \f
2110 /* Wrapper around expand_unop which takes an rtx code to specify
2111 the operation to perform, not an optab pointer. All other
2112 arguments are the same. */
2113 rtx
2114 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2115 rtx target, int unsignedp)
2116 {
2117 optab unop = code_to_optab (code);
2118 gcc_assert (unop);
2119
2120 return expand_unop (mode, unop, op0, target, unsignedp);
2121 }
2122
2123 /* Try calculating
2124 (clz:narrow x)
2125 as
2126 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2127
2128 A similar operation can be used for clrsb. UNOPTAB says which operation
2129 we are trying to expand. */
2130 static rtx
2131 widen_leading (scalar_int_mode mode, rtx op0, rtx target, optab unoptab)
2132 {
2133 opt_scalar_int_mode wider_mode_iter;
2134 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2135 {
2136 scalar_int_mode wider_mode = wider_mode_iter.require ();
2137 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2138 {
2139 rtx xop0, temp;
2140 rtx_insn *last;
2141
2142 last = get_last_insn ();
2143
2144 if (target == 0)
2145 target = gen_reg_rtx (mode);
2146 xop0 = widen_operand (op0, wider_mode, mode,
2147 unoptab != clrsb_optab, false);
2148 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2149 unoptab != clrsb_optab);
2150 if (temp != 0)
2151 temp = expand_binop
2152 (wider_mode, sub_optab, temp,
2153 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2154 - GET_MODE_PRECISION (mode),
2155 wider_mode),
2156 target, true, OPTAB_DIRECT);
2157 if (temp == 0)
2158 delete_insns_since (last);
2159
2160 return temp;
2161 }
2162 }
2163 return 0;
2164 }
2165
2166 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2167 quantities, choosing which based on whether the high word is nonzero. */
2168 static rtx
2169 expand_doubleword_clz (scalar_int_mode mode, rtx op0, rtx target)
2170 {
2171 rtx xop0 = force_reg (mode, op0);
2172 rtx subhi = gen_highpart (word_mode, xop0);
2173 rtx sublo = gen_lowpart (word_mode, xop0);
2174 rtx_code_label *hi0_label = gen_label_rtx ();
2175 rtx_code_label *after_label = gen_label_rtx ();
2176 rtx_insn *seq;
2177 rtx temp, result;
2178
2179 /* If we were not given a target, use a word_mode register, not a
2180 'mode' register. The result will fit, and nobody is expecting
2181 anything bigger (the return type of __builtin_clz* is int). */
2182 if (!target)
2183 target = gen_reg_rtx (word_mode);
2184
2185 /* In any case, write to a word_mode scratch in both branches of the
2186 conditional, so we can ensure there is a single move insn setting
2187 'target' to tag a REG_EQUAL note on. */
2188 result = gen_reg_rtx (word_mode);
2189
2190 start_sequence ();
2191
2192 /* If the high word is not equal to zero,
2193 then clz of the full value is clz of the high word. */
2194 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2195 word_mode, true, hi0_label);
2196
2197 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2198 if (!temp)
2199 goto fail;
2200
2201 if (temp != result)
2202 convert_move (result, temp, true);
2203
2204 emit_jump_insn (targetm.gen_jump (after_label));
2205 emit_barrier ();
2206
2207 /* Else clz of the full value is clz of the low word plus the number
2208 of bits in the high word. */
2209 emit_label (hi0_label);
2210
2211 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2212 if (!temp)
2213 goto fail;
2214 temp = expand_binop (word_mode, add_optab, temp,
2215 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2216 result, true, OPTAB_DIRECT);
2217 if (!temp)
2218 goto fail;
2219 if (temp != result)
2220 convert_move (result, temp, true);
2221
2222 emit_label (after_label);
2223 convert_move (target, result, true);
2224
2225 seq = get_insns ();
2226 end_sequence ();
2227
2228 add_equal_note (seq, target, CLZ, xop0, 0);
2229 emit_insn (seq);
2230 return target;
2231
2232 fail:
2233 end_sequence ();
2234 return 0;
2235 }
2236
2237 /* Try calculating popcount of a double-word quantity as two popcount's of
2238 word-sized quantities and summing up the results. */
2239 static rtx
2240 expand_doubleword_popcount (scalar_int_mode mode, rtx op0, rtx target)
2241 {
2242 rtx t0, t1, t;
2243 rtx_insn *seq;
2244
2245 start_sequence ();
2246
2247 t0 = expand_unop_direct (word_mode, popcount_optab,
2248 operand_subword_force (op0, 0, mode), NULL_RTX,
2249 true);
2250 t1 = expand_unop_direct (word_mode, popcount_optab,
2251 operand_subword_force (op0, 1, mode), NULL_RTX,
2252 true);
2253 if (!t0 || !t1)
2254 {
2255 end_sequence ();
2256 return NULL_RTX;
2257 }
2258
2259 /* If we were not given a target, use a word_mode register, not a
2260 'mode' register. The result will fit, and nobody is expecting
2261 anything bigger (the return type of __builtin_popcount* is int). */
2262 if (!target)
2263 target = gen_reg_rtx (word_mode);
2264
2265 t = expand_binop (word_mode, add_optab, t0, t1, target, 0, OPTAB_DIRECT);
2266
2267 seq = get_insns ();
2268 end_sequence ();
2269
2270 add_equal_note (seq, t, POPCOUNT, op0, 0);
2271 emit_insn (seq);
2272 return t;
2273 }
2274
2275 /* Try calculating
2276 (parity:wide x)
2277 as
2278 (parity:narrow (low (x) ^ high (x))) */
2279 static rtx
2280 expand_doubleword_parity (scalar_int_mode mode, rtx op0, rtx target)
2281 {
2282 rtx t = expand_binop (word_mode, xor_optab,
2283 operand_subword_force (op0, 0, mode),
2284 operand_subword_force (op0, 1, mode),
2285 NULL_RTX, 0, OPTAB_DIRECT);
2286 return expand_unop (word_mode, parity_optab, t, target, true);
2287 }
2288
2289 /* Try calculating
2290 (bswap:narrow x)
2291 as
2292 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2293 static rtx
2294 widen_bswap (scalar_int_mode mode, rtx op0, rtx target)
2295 {
2296 rtx x;
2297 rtx_insn *last;
2298 opt_scalar_int_mode wider_mode_iter;
2299
2300 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2301 if (optab_handler (bswap_optab, wider_mode_iter.require ())
2302 != CODE_FOR_nothing)
2303 break;
2304
2305 if (!wider_mode_iter.exists ())
2306 return NULL_RTX;
2307
2308 scalar_int_mode wider_mode = wider_mode_iter.require ();
2309 last = get_last_insn ();
2310
2311 x = widen_operand (op0, wider_mode, mode, true, true);
2312 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2313
2314 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2315 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2316 if (x != 0)
2317 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2318 GET_MODE_BITSIZE (wider_mode)
2319 - GET_MODE_BITSIZE (mode),
2320 NULL_RTX, true);
2321
2322 if (x != 0)
2323 {
2324 if (target == 0)
2325 target = gen_reg_rtx (mode);
2326 emit_move_insn (target, gen_lowpart (mode, x));
2327 }
2328 else
2329 delete_insns_since (last);
2330
2331 return target;
2332 }
2333
2334 /* Try calculating bswap as two bswaps of two word-sized operands. */
2335
2336 static rtx
2337 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2338 {
2339 rtx t0, t1;
2340
2341 t1 = expand_unop (word_mode, bswap_optab,
2342 operand_subword_force (op, 0, mode), NULL_RTX, true);
2343 t0 = expand_unop (word_mode, bswap_optab,
2344 operand_subword_force (op, 1, mode), NULL_RTX, true);
2345
2346 if (target == 0 || !valid_multiword_target_p (target))
2347 target = gen_reg_rtx (mode);
2348 if (REG_P (target))
2349 emit_clobber (target);
2350 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2351 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2352
2353 return target;
2354 }
2355
2356 /* Try calculating (parity x) as (and (popcount x) 1), where
2357 popcount can also be done in a wider mode. */
2358 static rtx
2359 expand_parity (scalar_int_mode mode, rtx op0, rtx target)
2360 {
2361 enum mode_class mclass = GET_MODE_CLASS (mode);
2362 opt_scalar_int_mode wider_mode_iter;
2363 FOR_EACH_MODE_FROM (wider_mode_iter, mode)
2364 {
2365 scalar_int_mode wider_mode = wider_mode_iter.require ();
2366 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2367 {
2368 rtx xop0, temp;
2369 rtx_insn *last;
2370
2371 last = get_last_insn ();
2372
2373 if (target == 0 || GET_MODE (target) != wider_mode)
2374 target = gen_reg_rtx (wider_mode);
2375
2376 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2377 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2378 true);
2379 if (temp != 0)
2380 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2381 target, true, OPTAB_DIRECT);
2382
2383 if (temp)
2384 {
2385 if (mclass != MODE_INT
2386 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2387 return convert_to_mode (mode, temp, 0);
2388 else
2389 return gen_lowpart (mode, temp);
2390 }
2391 else
2392 delete_insns_since (last);
2393 }
2394 }
2395 return 0;
2396 }
2397
2398 /* Try calculating ctz(x) as K - clz(x & -x) ,
2399 where K is GET_MODE_PRECISION(mode) - 1.
2400
2401 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2402 don't have to worry about what the hardware does in that case. (If
2403 the clz instruction produces the usual value at 0, which is K, the
2404 result of this code sequence will be -1; expand_ffs, below, relies
2405 on this. It might be nice to have it be K instead, for consistency
2406 with the (very few) processors that provide a ctz with a defined
2407 value, but that would take one more instruction, and it would be
2408 less convenient for expand_ffs anyway. */
2409
2410 static rtx
2411 expand_ctz (scalar_int_mode mode, rtx op0, rtx target)
2412 {
2413 rtx_insn *seq;
2414 rtx temp;
2415
2416 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2417 return 0;
2418
2419 start_sequence ();
2420
2421 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2422 if (temp)
2423 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2424 true, OPTAB_DIRECT);
2425 if (temp)
2426 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2427 if (temp)
2428 temp = expand_binop (mode, sub_optab,
2429 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2430 temp, target,
2431 true, OPTAB_DIRECT);
2432 if (temp == 0)
2433 {
2434 end_sequence ();
2435 return 0;
2436 }
2437
2438 seq = get_insns ();
2439 end_sequence ();
2440
2441 add_equal_note (seq, temp, CTZ, op0, 0);
2442 emit_insn (seq);
2443 return temp;
2444 }
2445
2446
2447 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2448 else with the sequence used by expand_clz.
2449
2450 The ffs builtin promises to return zero for a zero value and ctz/clz
2451 may have an undefined value in that case. If they do not give us a
2452 convenient value, we have to generate a test and branch. */
2453 static rtx
2454 expand_ffs (scalar_int_mode mode, rtx op0, rtx target)
2455 {
2456 HOST_WIDE_INT val = 0;
2457 bool defined_at_zero = false;
2458 rtx temp;
2459 rtx_insn *seq;
2460
2461 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2462 {
2463 start_sequence ();
2464
2465 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2466 if (!temp)
2467 goto fail;
2468
2469 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2470 }
2471 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2472 {
2473 start_sequence ();
2474 temp = expand_ctz (mode, op0, 0);
2475 if (!temp)
2476 goto fail;
2477
2478 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2479 {
2480 defined_at_zero = true;
2481 val = (GET_MODE_PRECISION (mode) - 1) - val;
2482 }
2483 }
2484 else
2485 return 0;
2486
2487 if (defined_at_zero && val == -1)
2488 /* No correction needed at zero. */;
2489 else
2490 {
2491 /* We don't try to do anything clever with the situation found
2492 on some processors (eg Alpha) where ctz(0:mode) ==
2493 bitsize(mode). If someone can think of a way to send N to -1
2494 and leave alone all values in the range 0..N-1 (where N is a
2495 power of two), cheaper than this test-and-branch, please add it.
2496
2497 The test-and-branch is done after the operation itself, in case
2498 the operation sets condition codes that can be recycled for this.
2499 (This is true on i386, for instance.) */
2500
2501 rtx_code_label *nonzero_label = gen_label_rtx ();
2502 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2503 mode, true, nonzero_label);
2504
2505 convert_move (temp, GEN_INT (-1), false);
2506 emit_label (nonzero_label);
2507 }
2508
2509 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2510 to produce a value in the range 0..bitsize. */
2511 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2512 target, false, OPTAB_DIRECT);
2513 if (!temp)
2514 goto fail;
2515
2516 seq = get_insns ();
2517 end_sequence ();
2518
2519 add_equal_note (seq, temp, FFS, op0, 0);
2520 emit_insn (seq);
2521 return temp;
2522
2523 fail:
2524 end_sequence ();
2525 return 0;
2526 }
2527
2528 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2529 conditions, VAL may already be a SUBREG against which we cannot generate
2530 a further SUBREG. In this case, we expect forcing the value into a
2531 register will work around the situation. */
2532
2533 static rtx
2534 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
2535 machine_mode imode)
2536 {
2537 rtx ret;
2538 ret = lowpart_subreg (omode, val, imode);
2539 if (ret == NULL)
2540 {
2541 val = force_reg (imode, val);
2542 ret = lowpart_subreg (omode, val, imode);
2543 gcc_assert (ret != NULL);
2544 }
2545 return ret;
2546 }
2547
2548 /* Expand a floating point absolute value or negation operation via a
2549 logical operation on the sign bit. */
2550
2551 static rtx
2552 expand_absneg_bit (enum rtx_code code, scalar_float_mode mode,
2553 rtx op0, rtx target)
2554 {
2555 const struct real_format *fmt;
2556 int bitpos, word, nwords, i;
2557 scalar_int_mode imode;
2558 rtx temp;
2559 rtx_insn *insns;
2560
2561 /* The format has to have a simple sign bit. */
2562 fmt = REAL_MODE_FORMAT (mode);
2563 if (fmt == NULL)
2564 return NULL_RTX;
2565
2566 bitpos = fmt->signbit_rw;
2567 if (bitpos < 0)
2568 return NULL_RTX;
2569
2570 /* Don't create negative zeros if the format doesn't support them. */
2571 if (code == NEG && !fmt->has_signed_zero)
2572 return NULL_RTX;
2573
2574 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2575 {
2576 if (!int_mode_for_mode (mode).exists (&imode))
2577 return NULL_RTX;
2578 word = 0;
2579 nwords = 1;
2580 }
2581 else
2582 {
2583 imode = word_mode;
2584
2585 if (FLOAT_WORDS_BIG_ENDIAN)
2586 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2587 else
2588 word = bitpos / BITS_PER_WORD;
2589 bitpos = bitpos % BITS_PER_WORD;
2590 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2591 }
2592
2593 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2594 if (code == ABS)
2595 mask = ~mask;
2596
2597 if (target == 0
2598 || target == op0
2599 || (nwords > 1 && !valid_multiword_target_p (target)))
2600 target = gen_reg_rtx (mode);
2601
2602 if (nwords > 1)
2603 {
2604 start_sequence ();
2605
2606 for (i = 0; i < nwords; ++i)
2607 {
2608 rtx targ_piece = operand_subword (target, i, 1, mode);
2609 rtx op0_piece = operand_subword_force (op0, i, mode);
2610
2611 if (i == word)
2612 {
2613 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2614 op0_piece,
2615 immed_wide_int_const (mask, imode),
2616 targ_piece, 1, OPTAB_LIB_WIDEN);
2617 if (temp != targ_piece)
2618 emit_move_insn (targ_piece, temp);
2619 }
2620 else
2621 emit_move_insn (targ_piece, op0_piece);
2622 }
2623
2624 insns = get_insns ();
2625 end_sequence ();
2626
2627 emit_insn (insns);
2628 }
2629 else
2630 {
2631 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2632 gen_lowpart (imode, op0),
2633 immed_wide_int_const (mask, imode),
2634 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2635 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2636
2637 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2638 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2639 target);
2640 }
2641
2642 return target;
2643 }
2644
2645 /* As expand_unop, but will fail rather than attempt the operation in a
2646 different mode or with a libcall. */
2647 static rtx
2648 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
2649 int unsignedp)
2650 {
2651 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2652 {
2653 struct expand_operand ops[2];
2654 enum insn_code icode = optab_handler (unoptab, mode);
2655 rtx_insn *last = get_last_insn ();
2656 rtx_insn *pat;
2657
2658 create_output_operand (&ops[0], target, mode);
2659 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2660 pat = maybe_gen_insn (icode, 2, ops);
2661 if (pat)
2662 {
2663 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2664 && ! add_equal_note (pat, ops[0].value,
2665 optab_to_code (unoptab),
2666 ops[1].value, NULL_RTX))
2667 {
2668 delete_insns_since (last);
2669 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2670 }
2671
2672 emit_insn (pat);
2673
2674 return ops[0].value;
2675 }
2676 }
2677 return 0;
2678 }
2679
2680 /* Generate code to perform an operation specified by UNOPTAB
2681 on operand OP0, with result having machine-mode MODE.
2682
2683 UNSIGNEDP is for the case where we have to widen the operands
2684 to perform the operation. It says to use zero-extension.
2685
2686 If TARGET is nonzero, the value
2687 is generated there, if it is convenient to do so.
2688 In all cases an rtx is returned for the locus of the value;
2689 this may or may not be TARGET. */
2690
2691 rtx
2692 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
2693 int unsignedp)
2694 {
2695 enum mode_class mclass = GET_MODE_CLASS (mode);
2696 machine_mode wider_mode;
2697 scalar_int_mode int_mode;
2698 scalar_float_mode float_mode;
2699 rtx temp;
2700 rtx libfunc;
2701
2702 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
2703 if (temp)
2704 return temp;
2705
2706 /* It can't be done in this mode. Can we open-code it in a wider mode? */
2707
2708 /* Widening (or narrowing) clz needs special treatment. */
2709 if (unoptab == clz_optab)
2710 {
2711 if (is_a <scalar_int_mode> (mode, &int_mode))
2712 {
2713 temp = widen_leading (int_mode, op0, target, unoptab);
2714 if (temp)
2715 return temp;
2716
2717 if (GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2718 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2719 {
2720 temp = expand_doubleword_clz (int_mode, op0, target);
2721 if (temp)
2722 return temp;
2723 }
2724 }
2725
2726 goto try_libcall;
2727 }
2728
2729 if (unoptab == clrsb_optab)
2730 {
2731 if (is_a <scalar_int_mode> (mode, &int_mode))
2732 {
2733 temp = widen_leading (int_mode, op0, target, unoptab);
2734 if (temp)
2735 return temp;
2736 }
2737 goto try_libcall;
2738 }
2739
2740 if (unoptab == popcount_optab
2741 && is_a <scalar_int_mode> (mode, &int_mode)
2742 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2743 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing
2744 && optimize_insn_for_speed_p ())
2745 {
2746 temp = expand_doubleword_popcount (int_mode, op0, target);
2747 if (temp)
2748 return temp;
2749 }
2750
2751 if (unoptab == parity_optab
2752 && is_a <scalar_int_mode> (mode, &int_mode)
2753 && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2754 && (optab_handler (unoptab, word_mode) != CODE_FOR_nothing
2755 || optab_handler (popcount_optab, word_mode) != CODE_FOR_nothing)
2756 && optimize_insn_for_speed_p ())
2757 {
2758 temp = expand_doubleword_parity (int_mode, op0, target);
2759 if (temp)
2760 return temp;
2761 }
2762
2763 /* Widening (or narrowing) bswap needs special treatment. */
2764 if (unoptab == bswap_optab)
2765 {
2766 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
2767 or ROTATERT. First try these directly; if this fails, then try the
2768 obvious pair of shifts with allowed widening, as this will probably
2769 be always more efficient than the other fallback methods. */
2770 if (mode == HImode)
2771 {
2772 rtx_insn *last;
2773 rtx temp1, temp2;
2774
2775 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
2776 {
2777 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
2778 unsignedp, OPTAB_DIRECT);
2779 if (temp)
2780 return temp;
2781 }
2782
2783 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
2784 {
2785 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
2786 unsignedp, OPTAB_DIRECT);
2787 if (temp)
2788 return temp;
2789 }
2790
2791 last = get_last_insn ();
2792
2793 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
2794 unsignedp, OPTAB_WIDEN);
2795 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
2796 unsignedp, OPTAB_WIDEN);
2797 if (temp1 && temp2)
2798 {
2799 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
2800 unsignedp, OPTAB_WIDEN);
2801 if (temp)
2802 return temp;
2803 }
2804
2805 delete_insns_since (last);
2806 }
2807
2808 if (is_a <scalar_int_mode> (mode, &int_mode))
2809 {
2810 temp = widen_bswap (int_mode, op0, target);
2811 if (temp)
2812 return temp;
2813
2814 if (GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
2815 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2816 {
2817 temp = expand_doubleword_bswap (mode, op0, target);
2818 if (temp)
2819 return temp;
2820 }
2821 }
2822
2823 goto try_libcall;
2824 }
2825
2826 if (CLASS_HAS_WIDER_MODES_P (mclass))
2827 FOR_EACH_WIDER_MODE (wider_mode, mode)
2828 {
2829 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2830 {
2831 rtx xop0 = op0;
2832 rtx_insn *last = get_last_insn ();
2833
2834 /* For certain operations, we need not actually extend
2835 the narrow operand, as long as we will truncate the
2836 results to the same narrowness. */
2837
2838 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2839 (unoptab == neg_optab
2840 || unoptab == one_cmpl_optab)
2841 && mclass == MODE_INT);
2842
2843 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2844 unsignedp);
2845
2846 if (temp)
2847 {
2848 if (mclass != MODE_INT
2849 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2850 {
2851 if (target == 0)
2852 target = gen_reg_rtx (mode);
2853 convert_move (target, temp, 0);
2854 return target;
2855 }
2856 else
2857 return gen_lowpart (mode, temp);
2858 }
2859 else
2860 delete_insns_since (last);
2861 }
2862 }
2863
2864 /* These can be done a word at a time. */
2865 if (unoptab == one_cmpl_optab
2866 && is_int_mode (mode, &int_mode)
2867 && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
2868 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2869 {
2870 int i;
2871 rtx_insn *insns;
2872
2873 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
2874 target = gen_reg_rtx (int_mode);
2875
2876 start_sequence ();
2877
2878 /* Do the actual arithmetic. */
2879 for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
2880 {
2881 rtx target_piece = operand_subword (target, i, 1, int_mode);
2882 rtx x = expand_unop (word_mode, unoptab,
2883 operand_subword_force (op0, i, int_mode),
2884 target_piece, unsignedp);
2885
2886 if (target_piece != x)
2887 emit_move_insn (target_piece, x);
2888 }
2889
2890 insns = get_insns ();
2891 end_sequence ();
2892
2893 emit_insn (insns);
2894 return target;
2895 }
2896
2897 if (optab_to_code (unoptab) == NEG)
2898 {
2899 /* Try negating floating point values by flipping the sign bit. */
2900 if (is_a <scalar_float_mode> (mode, &float_mode))
2901 {
2902 temp = expand_absneg_bit (NEG, float_mode, op0, target);
2903 if (temp)
2904 return temp;
2905 }
2906
2907 /* If there is no negation pattern, and we have no negative zero,
2908 try subtracting from zero. */
2909 if (!HONOR_SIGNED_ZEROS (mode))
2910 {
2911 temp = expand_binop (mode, (unoptab == negv_optab
2912 ? subv_optab : sub_optab),
2913 CONST0_RTX (mode), op0, target,
2914 unsignedp, OPTAB_DIRECT);
2915 if (temp)
2916 return temp;
2917 }
2918 }
2919
2920 /* Try calculating parity (x) as popcount (x) % 2. */
2921 if (unoptab == parity_optab && is_a <scalar_int_mode> (mode, &int_mode))
2922 {
2923 temp = expand_parity (int_mode, op0, target);
2924 if (temp)
2925 return temp;
2926 }
2927
2928 /* Try implementing ffs (x) in terms of clz (x). */
2929 if (unoptab == ffs_optab && is_a <scalar_int_mode> (mode, &int_mode))
2930 {
2931 temp = expand_ffs (int_mode, op0, target);
2932 if (temp)
2933 return temp;
2934 }
2935
2936 /* Try implementing ctz (x) in terms of clz (x). */
2937 if (unoptab == ctz_optab && is_a <scalar_int_mode> (mode, &int_mode))
2938 {
2939 temp = expand_ctz (int_mode, op0, target);
2940 if (temp)
2941 return temp;
2942 }
2943
2944 try_libcall:
2945 /* Now try a library call in this mode. */
2946 libfunc = optab_libfunc (unoptab, mode);
2947 if (libfunc)
2948 {
2949 rtx_insn *insns;
2950 rtx value;
2951 rtx eq_value;
2952 machine_mode outmode = mode;
2953
2954 /* All of these functions return small values. Thus we choose to
2955 have them return something that isn't a double-word. */
2956 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
2957 || unoptab == clrsb_optab || unoptab == popcount_optab
2958 || unoptab == parity_optab)
2959 outmode
2960 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
2961 optab_libfunc (unoptab, mode)));
2962
2963 start_sequence ();
2964
2965 /* Pass 1 for NO_QUEUE so we don't lose any increments
2966 if the libcall is cse'd or moved. */
2967 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
2968 op0, mode);
2969 insns = get_insns ();
2970 end_sequence ();
2971
2972 target = gen_reg_rtx (outmode);
2973 bool trapv = trapv_unoptab_p (unoptab);
2974 if (trapv)
2975 eq_value = NULL_RTX;
2976 else
2977 {
2978 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
2979 if (GET_MODE_UNIT_SIZE (outmode) < GET_MODE_UNIT_SIZE (mode))
2980 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
2981 else if (GET_MODE_UNIT_SIZE (outmode) > GET_MODE_UNIT_SIZE (mode))
2982 eq_value = simplify_gen_unary (ZERO_EXTEND,
2983 outmode, eq_value, mode);
2984 }
2985 emit_libcall_block_1 (insns, target, value, eq_value, trapv);
2986
2987 return target;
2988 }
2989
2990 /* It can't be done in this mode. Can we do it in a wider mode? */
2991
2992 if (CLASS_HAS_WIDER_MODES_P (mclass))
2993 {
2994 FOR_EACH_WIDER_MODE (wider_mode, mode)
2995 {
2996 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
2997 || optab_libfunc (unoptab, wider_mode))
2998 {
2999 rtx xop0 = op0;
3000 rtx_insn *last = get_last_insn ();
3001
3002 /* For certain operations, we need not actually extend
3003 the narrow operand, as long as we will truncate the
3004 results to the same narrowness. */
3005 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3006 (unoptab == neg_optab
3007 || unoptab == one_cmpl_optab
3008 || unoptab == bswap_optab)
3009 && mclass == MODE_INT);
3010
3011 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3012 unsignedp);
3013
3014 /* If we are generating clz using wider mode, adjust the
3015 result. Similarly for clrsb. */
3016 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3017 && temp != 0)
3018 {
3019 scalar_int_mode wider_int_mode
3020 = as_a <scalar_int_mode> (wider_mode);
3021 int_mode = as_a <scalar_int_mode> (mode);
3022 temp = expand_binop
3023 (wider_mode, sub_optab, temp,
3024 gen_int_mode (GET_MODE_PRECISION (wider_int_mode)
3025 - GET_MODE_PRECISION (int_mode),
3026 wider_int_mode),
3027 target, true, OPTAB_DIRECT);
3028 }
3029
3030 /* Likewise for bswap. */
3031 if (unoptab == bswap_optab && temp != 0)
3032 {
3033 scalar_int_mode wider_int_mode
3034 = as_a <scalar_int_mode> (wider_mode);
3035 int_mode = as_a <scalar_int_mode> (mode);
3036 gcc_assert (GET_MODE_PRECISION (wider_int_mode)
3037 == GET_MODE_BITSIZE (wider_int_mode)
3038 && GET_MODE_PRECISION (int_mode)
3039 == GET_MODE_BITSIZE (int_mode));
3040
3041 temp = expand_shift (RSHIFT_EXPR, wider_int_mode, temp,
3042 GET_MODE_BITSIZE (wider_int_mode)
3043 - GET_MODE_BITSIZE (int_mode),
3044 NULL_RTX, true);
3045 }
3046
3047 if (temp)
3048 {
3049 if (mclass != MODE_INT)
3050 {
3051 if (target == 0)
3052 target = gen_reg_rtx (mode);
3053 convert_move (target, temp, 0);
3054 return target;
3055 }
3056 else
3057 return gen_lowpart (mode, temp);
3058 }
3059 else
3060 delete_insns_since (last);
3061 }
3062 }
3063 }
3064
3065 /* One final attempt at implementing negation via subtraction,
3066 this time allowing widening of the operand. */
3067 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3068 {
3069 rtx temp;
3070 temp = expand_binop (mode,
3071 unoptab == negv_optab ? subv_optab : sub_optab,
3072 CONST0_RTX (mode), op0,
3073 target, unsignedp, OPTAB_LIB_WIDEN);
3074 if (temp)
3075 return temp;
3076 }
3077
3078 return 0;
3079 }
3080 \f
3081 /* Emit code to compute the absolute value of OP0, with result to
3082 TARGET if convenient. (TARGET may be 0.) The return value says
3083 where the result actually is to be found.
3084
3085 MODE is the mode of the operand; the mode of the result is
3086 different but can be deduced from MODE.
3087
3088 */
3089
3090 rtx
3091 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3092 int result_unsignedp)
3093 {
3094 rtx temp;
3095
3096 if (GET_MODE_CLASS (mode) != MODE_INT
3097 || ! flag_trapv)
3098 result_unsignedp = 1;
3099
3100 /* First try to do it with a special abs instruction. */
3101 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3102 op0, target, 0);
3103 if (temp != 0)
3104 return temp;
3105
3106 /* For floating point modes, try clearing the sign bit. */
3107 scalar_float_mode float_mode;
3108 if (is_a <scalar_float_mode> (mode, &float_mode))
3109 {
3110 temp = expand_absneg_bit (ABS, float_mode, op0, target);
3111 if (temp)
3112 return temp;
3113 }
3114
3115 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3116 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3117 && !HONOR_SIGNED_ZEROS (mode))
3118 {
3119 rtx_insn *last = get_last_insn ();
3120
3121 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3122 op0, NULL_RTX, 0);
3123 if (temp != 0)
3124 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3125 OPTAB_WIDEN);
3126
3127 if (temp != 0)
3128 return temp;
3129
3130 delete_insns_since (last);
3131 }
3132
3133 /* If this machine has expensive jumps, we can do integer absolute
3134 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3135 where W is the width of MODE. */
3136
3137 scalar_int_mode int_mode;
3138 if (is_int_mode (mode, &int_mode)
3139 && BRANCH_COST (optimize_insn_for_speed_p (),
3140 false) >= 2)
3141 {
3142 rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
3143 GET_MODE_PRECISION (int_mode) - 1,
3144 NULL_RTX, 0);
3145
3146 temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
3147 OPTAB_LIB_WIDEN);
3148 if (temp != 0)
3149 temp = expand_binop (int_mode,
3150 result_unsignedp ? sub_optab : subv_optab,
3151 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3152
3153 if (temp != 0)
3154 return temp;
3155 }
3156
3157 return NULL_RTX;
3158 }
3159
3160 rtx
3161 expand_abs (machine_mode mode, rtx op0, rtx target,
3162 int result_unsignedp, int safe)
3163 {
3164 rtx temp;
3165 rtx_code_label *op1;
3166
3167 if (GET_MODE_CLASS (mode) != MODE_INT
3168 || ! flag_trapv)
3169 result_unsignedp = 1;
3170
3171 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3172 if (temp != 0)
3173 return temp;
3174
3175 /* If that does not win, use conditional jump and negate. */
3176
3177 /* It is safe to use the target if it is the same
3178 as the source if this is also a pseudo register */
3179 if (op0 == target && REG_P (op0)
3180 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3181 safe = 1;
3182
3183 op1 = gen_label_rtx ();
3184 if (target == 0 || ! safe
3185 || GET_MODE (target) != mode
3186 || (MEM_P (target) && MEM_VOLATILE_P (target))
3187 || (REG_P (target)
3188 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3189 target = gen_reg_rtx (mode);
3190
3191 emit_move_insn (target, op0);
3192 NO_DEFER_POP;
3193
3194 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3195 NULL_RTX, NULL, op1,
3196 profile_probability::uninitialized ());
3197
3198 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3199 target, target, 0);
3200 if (op0 != target)
3201 emit_move_insn (target, op0);
3202 emit_label (op1);
3203 OK_DEFER_POP;
3204 return target;
3205 }
3206
3207 /* Emit code to compute the one's complement absolute value of OP0
3208 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3209 (TARGET may be NULL_RTX.) The return value says where the result
3210 actually is to be found.
3211
3212 MODE is the mode of the operand; the mode of the result is
3213 different but can be deduced from MODE. */
3214
3215 rtx
3216 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3217 {
3218 rtx temp;
3219
3220 /* Not applicable for floating point modes. */
3221 if (FLOAT_MODE_P (mode))
3222 return NULL_RTX;
3223
3224 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3225 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3226 {
3227 rtx_insn *last = get_last_insn ();
3228
3229 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3230 if (temp != 0)
3231 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3232 OPTAB_WIDEN);
3233
3234 if (temp != 0)
3235 return temp;
3236
3237 delete_insns_since (last);
3238 }
3239
3240 /* If this machine has expensive jumps, we can do one's complement
3241 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3242
3243 scalar_int_mode int_mode;
3244 if (is_int_mode (mode, &int_mode)
3245 && BRANCH_COST (optimize_insn_for_speed_p (),
3246 false) >= 2)
3247 {
3248 rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
3249 GET_MODE_PRECISION (int_mode) - 1,
3250 NULL_RTX, 0);
3251
3252 temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
3253 OPTAB_LIB_WIDEN);
3254
3255 if (temp != 0)
3256 return temp;
3257 }
3258
3259 return NULL_RTX;
3260 }
3261
3262 /* A subroutine of expand_copysign, perform the copysign operation using the
3263 abs and neg primitives advertised to exist on the target. The assumption
3264 is that we have a split register file, and leaving op0 in fp registers,
3265 and not playing with subregs so much, will help the register allocator. */
3266
3267 static rtx
3268 expand_copysign_absneg (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3269 int bitpos, bool op0_is_abs)
3270 {
3271 scalar_int_mode imode;
3272 enum insn_code icode;
3273 rtx sign;
3274 rtx_code_label *label;
3275
3276 if (target == op1)
3277 target = NULL_RTX;
3278
3279 /* Check if the back end provides an insn that handles signbit for the
3280 argument's mode. */
3281 icode = optab_handler (signbit_optab, mode);
3282 if (icode != CODE_FOR_nothing)
3283 {
3284 imode = as_a <scalar_int_mode> (insn_data[(int) icode].operand[0].mode);
3285 sign = gen_reg_rtx (imode);
3286 emit_unop_insn (icode, sign, op1, UNKNOWN);
3287 }
3288 else
3289 {
3290 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3291 {
3292 if (!int_mode_for_mode (mode).exists (&imode))
3293 return NULL_RTX;
3294 op1 = gen_lowpart (imode, op1);
3295 }
3296 else
3297 {
3298 int word;
3299
3300 imode = word_mode;
3301 if (FLOAT_WORDS_BIG_ENDIAN)
3302 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3303 else
3304 word = bitpos / BITS_PER_WORD;
3305 bitpos = bitpos % BITS_PER_WORD;
3306 op1 = operand_subword_force (op1, word, mode);
3307 }
3308
3309 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3310 sign = expand_binop (imode, and_optab, op1,
3311 immed_wide_int_const (mask, imode),
3312 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3313 }
3314
3315 if (!op0_is_abs)
3316 {
3317 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3318 if (op0 == NULL)
3319 return NULL_RTX;
3320 target = op0;
3321 }
3322 else
3323 {
3324 if (target == NULL_RTX)
3325 target = copy_to_reg (op0);
3326 else
3327 emit_move_insn (target, op0);
3328 }
3329
3330 label = gen_label_rtx ();
3331 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3332
3333 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3334 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3335 else
3336 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3337 if (op0 != target)
3338 emit_move_insn (target, op0);
3339
3340 emit_label (label);
3341
3342 return target;
3343 }
3344
3345
3346 /* A subroutine of expand_copysign, perform the entire copysign operation
3347 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3348 is true if op0 is known to have its sign bit clear. */
3349
3350 static rtx
3351 expand_copysign_bit (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
3352 int bitpos, bool op0_is_abs)
3353 {
3354 scalar_int_mode imode;
3355 int word, nwords, i;
3356 rtx temp;
3357 rtx_insn *insns;
3358
3359 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3360 {
3361 if (!int_mode_for_mode (mode).exists (&imode))
3362 return NULL_RTX;
3363 word = 0;
3364 nwords = 1;
3365 }
3366 else
3367 {
3368 imode = word_mode;
3369
3370 if (FLOAT_WORDS_BIG_ENDIAN)
3371 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3372 else
3373 word = bitpos / BITS_PER_WORD;
3374 bitpos = bitpos % BITS_PER_WORD;
3375 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3376 }
3377
3378 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3379
3380 if (target == 0
3381 || target == op0
3382 || target == op1
3383 || (nwords > 1 && !valid_multiword_target_p (target)))
3384 target = gen_reg_rtx (mode);
3385
3386 if (nwords > 1)
3387 {
3388 start_sequence ();
3389
3390 for (i = 0; i < nwords; ++i)
3391 {
3392 rtx targ_piece = operand_subword (target, i, 1, mode);
3393 rtx op0_piece = operand_subword_force (op0, i, mode);
3394
3395 if (i == word)
3396 {
3397 if (!op0_is_abs)
3398 op0_piece
3399 = expand_binop (imode, and_optab, op0_piece,
3400 immed_wide_int_const (~mask, imode),
3401 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3402 op1 = expand_binop (imode, and_optab,
3403 operand_subword_force (op1, i, mode),
3404 immed_wide_int_const (mask, imode),
3405 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3406
3407 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3408 targ_piece, 1, OPTAB_LIB_WIDEN);
3409 if (temp != targ_piece)
3410 emit_move_insn (targ_piece, temp);
3411 }
3412 else
3413 emit_move_insn (targ_piece, op0_piece);
3414 }
3415
3416 insns = get_insns ();
3417 end_sequence ();
3418
3419 emit_insn (insns);
3420 }
3421 else
3422 {
3423 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3424 immed_wide_int_const (mask, imode),
3425 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3426
3427 op0 = gen_lowpart (imode, op0);
3428 if (!op0_is_abs)
3429 op0 = expand_binop (imode, and_optab, op0,
3430 immed_wide_int_const (~mask, imode),
3431 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3432
3433 temp = expand_binop (imode, ior_optab, op0, op1,
3434 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3435 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3436 }
3437
3438 return target;
3439 }
3440
3441 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3442 scalar floating point mode. Return NULL if we do not know how to
3443 expand the operation inline. */
3444
3445 rtx
3446 expand_copysign (rtx op0, rtx op1, rtx target)
3447 {
3448 scalar_float_mode mode;
3449 const struct real_format *fmt;
3450 bool op0_is_abs;
3451 rtx temp;
3452
3453 mode = as_a <scalar_float_mode> (GET_MODE (op0));
3454 gcc_assert (GET_MODE (op1) == mode);
3455
3456 /* First try to do it with a special instruction. */
3457 temp = expand_binop (mode, copysign_optab, op0, op1,
3458 target, 0, OPTAB_DIRECT);
3459 if (temp)
3460 return temp;
3461
3462 fmt = REAL_MODE_FORMAT (mode);
3463 if (fmt == NULL || !fmt->has_signed_zero)
3464 return NULL_RTX;
3465
3466 op0_is_abs = false;
3467 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3468 {
3469 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3470 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3471 op0_is_abs = true;
3472 }
3473
3474 if (fmt->signbit_ro >= 0
3475 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3476 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3477 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3478 {
3479 temp = expand_copysign_absneg (mode, op0, op1, target,
3480 fmt->signbit_ro, op0_is_abs);
3481 if (temp)
3482 return temp;
3483 }
3484
3485 if (fmt->signbit_rw < 0)
3486 return NULL_RTX;
3487 return expand_copysign_bit (mode, op0, op1, target,
3488 fmt->signbit_rw, op0_is_abs);
3489 }
3490 \f
3491 /* Generate an instruction whose insn-code is INSN_CODE,
3492 with two operands: an output TARGET and an input OP0.
3493 TARGET *must* be nonzero, and the output is always stored there.
3494 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3495 the value that is stored into TARGET.
3496
3497 Return false if expansion failed. */
3498
3499 bool
3500 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3501 enum rtx_code code)
3502 {
3503 struct expand_operand ops[2];
3504 rtx_insn *pat;
3505
3506 create_output_operand (&ops[0], target, GET_MODE (target));
3507 create_input_operand (&ops[1], op0, GET_MODE (op0));
3508 pat = maybe_gen_insn (icode, 2, ops);
3509 if (!pat)
3510 return false;
3511
3512 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3513 && code != UNKNOWN)
3514 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3515
3516 emit_insn (pat);
3517
3518 if (ops[0].value != target)
3519 emit_move_insn (target, ops[0].value);
3520 return true;
3521 }
3522 /* Generate an instruction whose insn-code is INSN_CODE,
3523 with two operands: an output TARGET and an input OP0.
3524 TARGET *must* be nonzero, and the output is always stored there.
3525 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3526 the value that is stored into TARGET. */
3527
3528 void
3529 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3530 {
3531 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3532 gcc_assert (ok);
3533 }
3534 \f
3535 struct no_conflict_data
3536 {
3537 rtx target;
3538 rtx_insn *first, *insn;
3539 bool must_stay;
3540 };
3541
3542 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3543 the currently examined clobber / store has to stay in the list of
3544 insns that constitute the actual libcall block. */
3545 static void
3546 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3547 {
3548 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3549
3550 /* If this inns directly contributes to setting the target, it must stay. */
3551 if (reg_overlap_mentioned_p (p->target, dest))
3552 p->must_stay = true;
3553 /* If we haven't committed to keeping any other insns in the list yet,
3554 there is nothing more to check. */
3555 else if (p->insn == p->first)
3556 return;
3557 /* If this insn sets / clobbers a register that feeds one of the insns
3558 already in the list, this insn has to stay too. */
3559 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3560 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3561 || reg_used_between_p (dest, p->first, p->insn)
3562 /* Likewise if this insn depends on a register set by a previous
3563 insn in the list, or if it sets a result (presumably a hard
3564 register) that is set or clobbered by a previous insn.
3565 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3566 SET_DEST perform the former check on the address, and the latter
3567 check on the MEM. */
3568 || (GET_CODE (set) == SET
3569 && (modified_in_p (SET_SRC (set), p->first)
3570 || modified_in_p (SET_DEST (set), p->first)
3571 || modified_between_p (SET_SRC (set), p->first, p->insn)
3572 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3573 p->must_stay = true;
3574 }
3575
3576 \f
3577 /* Emit code to make a call to a constant function or a library call.
3578
3579 INSNS is a list containing all insns emitted in the call.
3580 These insns leave the result in RESULT. Our block is to copy RESULT
3581 to TARGET, which is logically equivalent to EQUIV.
3582
3583 We first emit any insns that set a pseudo on the assumption that these are
3584 loading constants into registers; doing so allows them to be safely cse'ed
3585 between blocks. Then we emit all the other insns in the block, followed by
3586 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3587 note with an operand of EQUIV. */
3588
3589 static void
3590 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3591 bool equiv_may_trap)
3592 {
3593 rtx final_dest = target;
3594 rtx_insn *next, *last, *insn;
3595
3596 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3597 into a MEM later. Protect the libcall block from this change. */
3598 if (! REG_P (target) || REG_USERVAR_P (target))
3599 target = gen_reg_rtx (GET_MODE (target));
3600
3601 /* If we're using non-call exceptions, a libcall corresponding to an
3602 operation that may trap may also trap. */
3603 /* ??? See the comment in front of make_reg_eh_region_note. */
3604 if (cfun->can_throw_non_call_exceptions
3605 && (equiv_may_trap || may_trap_p (equiv)))
3606 {
3607 for (insn = insns; insn; insn = NEXT_INSN (insn))
3608 if (CALL_P (insn))
3609 {
3610 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3611 if (note)
3612 {
3613 int lp_nr = INTVAL (XEXP (note, 0));
3614 if (lp_nr == 0 || lp_nr == INT_MIN)
3615 remove_note (insn, note);
3616 }
3617 }
3618 }
3619 else
3620 {
3621 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3622 reg note to indicate that this call cannot throw or execute a nonlocal
3623 goto (unless there is already a REG_EH_REGION note, in which case
3624 we update it). */
3625 for (insn = insns; insn; insn = NEXT_INSN (insn))
3626 if (CALL_P (insn))
3627 make_reg_eh_region_note_nothrow_nononlocal (insn);
3628 }
3629
3630 /* First emit all insns that set pseudos. Remove them from the list as
3631 we go. Avoid insns that set pseudos which were referenced in previous
3632 insns. These can be generated by move_by_pieces, for example,
3633 to update an address. Similarly, avoid insns that reference things
3634 set in previous insns. */
3635
3636 for (insn = insns; insn; insn = next)
3637 {
3638 rtx set = single_set (insn);
3639
3640 next = NEXT_INSN (insn);
3641
3642 if (set != 0 && REG_P (SET_DEST (set))
3643 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3644 {
3645 struct no_conflict_data data;
3646
3647 data.target = const0_rtx;
3648 data.first = insns;
3649 data.insn = insn;
3650 data.must_stay = 0;
3651 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3652 if (! data.must_stay)
3653 {
3654 if (PREV_INSN (insn))
3655 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3656 else
3657 insns = next;
3658
3659 if (next)
3660 SET_PREV_INSN (next) = PREV_INSN (insn);
3661
3662 add_insn (insn);
3663 }
3664 }
3665
3666 /* Some ports use a loop to copy large arguments onto the stack.
3667 Don't move anything outside such a loop. */
3668 if (LABEL_P (insn))
3669 break;
3670 }
3671
3672 /* Write the remaining insns followed by the final copy. */
3673 for (insn = insns; insn; insn = next)
3674 {
3675 next = NEXT_INSN (insn);
3676
3677 add_insn (insn);
3678 }
3679
3680 last = emit_move_insn (target, result);
3681 if (equiv)
3682 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3683
3684 if (final_dest != target)
3685 emit_move_insn (final_dest, target);
3686 }
3687
3688 void
3689 emit_libcall_block (rtx_insn *insns, rtx target, rtx result, rtx equiv)
3690 {
3691 emit_libcall_block_1 (insns, target, result, equiv, false);
3692 }
3693 \f
3694 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3695 PURPOSE describes how this comparison will be used. CODE is the rtx
3696 comparison code we will be using.
3697
3698 ??? Actually, CODE is slightly weaker than that. A target is still
3699 required to implement all of the normal bcc operations, but not
3700 required to implement all (or any) of the unordered bcc operations. */
3701
3702 int
3703 can_compare_p (enum rtx_code code, machine_mode mode,
3704 enum can_compare_purpose purpose)
3705 {
3706 rtx test;
3707 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3708 do
3709 {
3710 enum insn_code icode;
3711
3712 if (purpose == ccp_jump
3713 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
3714 && insn_operand_matches (icode, 0, test))
3715 return 1;
3716 if (purpose == ccp_store_flag
3717 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
3718 && insn_operand_matches (icode, 1, test))
3719 return 1;
3720 if (purpose == ccp_cmov
3721 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
3722 return 1;
3723
3724 mode = GET_MODE_WIDER_MODE (mode).else_void ();
3725 PUT_MODE (test, mode);
3726 }
3727 while (mode != VOIDmode);
3728
3729 return 0;
3730 }
3731
3732 /* This function is called when we are going to emit a compare instruction that
3733 compares the values found in X and Y, using the rtl operator COMPARISON.
3734
3735 If they have mode BLKmode, then SIZE specifies the size of both operands.
3736
3737 UNSIGNEDP nonzero says that the operands are unsigned;
3738 this matters if they need to be widened (as given by METHODS).
3739
3740 *PTEST is where the resulting comparison RTX is returned or NULL_RTX
3741 if we failed to produce one.
3742
3743 *PMODE is the mode of the inputs (in case they are const_int).
3744
3745 This function performs all the setup necessary so that the caller only has
3746 to emit a single comparison insn. This setup can involve doing a BLKmode
3747 comparison or emitting a library call to perform the comparison if no insn
3748 is available to handle it.
3749 The values which are passed in through pointers can be modified; the caller
3750 should perform the comparison on the modified values. Constant
3751 comparisons must have already been folded. */
3752
3753 static void
3754 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3755 int unsignedp, enum optab_methods methods,
3756 rtx *ptest, machine_mode *pmode)
3757 {
3758 machine_mode mode = *pmode;
3759 rtx libfunc, test;
3760 machine_mode cmp_mode;
3761 enum mode_class mclass;
3762
3763 /* The other methods are not needed. */
3764 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
3765 || methods == OPTAB_LIB_WIDEN);
3766
3767 /* If we are optimizing, force expensive constants into a register. */
3768 if (CONSTANT_P (x) && optimize
3769 && (rtx_cost (x, mode, COMPARE, 0, optimize_insn_for_speed_p ())
3770 > COSTS_N_INSNS (1)))
3771 x = force_reg (mode, x);
3772
3773 if (CONSTANT_P (y) && optimize
3774 && (rtx_cost (y, mode, COMPARE, 1, optimize_insn_for_speed_p ())
3775 > COSTS_N_INSNS (1)))
3776 y = force_reg (mode, y);
3777
3778 #if HAVE_cc0
3779 /* Make sure if we have a canonical comparison. The RTL
3780 documentation states that canonical comparisons are required only
3781 for targets which have cc0. */
3782 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
3783 #endif
3784
3785 /* Don't let both operands fail to indicate the mode. */
3786 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
3787 x = force_reg (mode, x);
3788 if (mode == VOIDmode)
3789 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
3790
3791 /* Handle all BLKmode compares. */
3792
3793 if (mode == BLKmode)
3794 {
3795 machine_mode result_mode;
3796 enum insn_code cmp_code;
3797 rtx result;
3798 rtx opalign
3799 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
3800
3801 gcc_assert (size);
3802
3803 /* Try to use a memory block compare insn - either cmpstr
3804 or cmpmem will do. */
3805 opt_scalar_int_mode cmp_mode_iter;
3806 FOR_EACH_MODE_IN_CLASS (cmp_mode_iter, MODE_INT)
3807 {
3808 scalar_int_mode cmp_mode = cmp_mode_iter.require ();
3809 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
3810 if (cmp_code == CODE_FOR_nothing)
3811 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
3812 if (cmp_code == CODE_FOR_nothing)
3813 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
3814 if (cmp_code == CODE_FOR_nothing)
3815 continue;
3816
3817 /* Must make sure the size fits the insn's mode. */
3818 if (CONST_INT_P (size)
3819 ? INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode))
3820 : (GET_MODE_BITSIZE (as_a <scalar_int_mode> (GET_MODE (size)))
3821 > GET_MODE_BITSIZE (cmp_mode)))
3822 continue;
3823
3824 result_mode = insn_data[cmp_code].operand[0].mode;
3825 result = gen_reg_rtx (result_mode);
3826 size = convert_to_mode (cmp_mode, size, 1);
3827 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
3828
3829 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
3830 *pmode = result_mode;
3831 return;
3832 }
3833
3834 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
3835 goto fail;
3836
3837 /* Otherwise call a library function. */
3838 result = emit_block_comp_via_libcall (XEXP (x, 0), XEXP (y, 0), size);
3839
3840 x = result;
3841 y = const0_rtx;
3842 mode = TYPE_MODE (integer_type_node);
3843 methods = OPTAB_LIB_WIDEN;
3844 unsignedp = false;
3845 }
3846
3847 /* Don't allow operands to the compare to trap, as that can put the
3848 compare and branch in different basic blocks. */
3849 if (cfun->can_throw_non_call_exceptions)
3850 {
3851 if (may_trap_p (x))
3852 x = copy_to_reg (x);
3853 if (may_trap_p (y))
3854 y = copy_to_reg (y);
3855 }
3856
3857 if (GET_MODE_CLASS (mode) == MODE_CC)
3858 {
3859 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
3860 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3861 gcc_assert (icode != CODE_FOR_nothing
3862 && insn_operand_matches (icode, 0, test));
3863 *ptest = test;
3864 return;
3865 }
3866
3867 mclass = GET_MODE_CLASS (mode);
3868 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3869 FOR_EACH_MODE_FROM (cmp_mode, mode)
3870 {
3871 enum insn_code icode;
3872 icode = optab_handler (cbranch_optab, cmp_mode);
3873 if (icode != CODE_FOR_nothing
3874 && insn_operand_matches (icode, 0, test))
3875 {
3876 rtx_insn *last = get_last_insn ();
3877 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
3878 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
3879 if (op0 && op1
3880 && insn_operand_matches (icode, 1, op0)
3881 && insn_operand_matches (icode, 2, op1))
3882 {
3883 XEXP (test, 0) = op0;
3884 XEXP (test, 1) = op1;
3885 *ptest = test;
3886 *pmode = cmp_mode;
3887 return;
3888 }
3889 delete_insns_since (last);
3890 }
3891
3892 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
3893 break;
3894 }
3895
3896 if (methods != OPTAB_LIB_WIDEN)
3897 goto fail;
3898
3899 if (!SCALAR_FLOAT_MODE_P (mode))
3900 {
3901 rtx result;
3902 machine_mode ret_mode;
3903
3904 /* Handle a libcall just for the mode we are using. */
3905 libfunc = optab_libfunc (cmp_optab, mode);
3906 gcc_assert (libfunc);
3907
3908 /* If we want unsigned, and this mode has a distinct unsigned
3909 comparison routine, use that. */
3910 if (unsignedp)
3911 {
3912 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
3913 if (ulibfunc)
3914 libfunc = ulibfunc;
3915 }
3916
3917 ret_mode = targetm.libgcc_cmp_return_mode ();
3918 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3919 ret_mode, x, mode, y, mode);
3920
3921 /* There are two kinds of comparison routines. Biased routines
3922 return 0/1/2, and unbiased routines return -1/0/1. Other parts
3923 of gcc expect that the comparison operation is equivalent
3924 to the modified comparison. For signed comparisons compare the
3925 result against 1 in the biased case, and zero in the unbiased
3926 case. For unsigned comparisons always compare against 1 after
3927 biasing the unbiased result by adding 1. This gives us a way to
3928 represent LTU.
3929 The comparisons in the fixed-point helper library are always
3930 biased. */
3931 x = result;
3932 y = const1_rtx;
3933
3934 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
3935 {
3936 if (unsignedp)
3937 x = plus_constant (ret_mode, result, 1);
3938 else
3939 y = const0_rtx;
3940 }
3941
3942 *pmode = ret_mode;
3943 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
3944 ptest, pmode);
3945 }
3946 else
3947 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
3948
3949 return;
3950
3951 fail:
3952 *ptest = NULL_RTX;
3953 }
3954
3955 /* Before emitting an insn with code ICODE, make sure that X, which is going
3956 to be used for operand OPNUM of the insn, is converted from mode MODE to
3957 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
3958 that it is accepted by the operand predicate. Return the new value. */
3959
3960 rtx
3961 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
3962 machine_mode wider_mode, int unsignedp)
3963 {
3964 if (mode != wider_mode)
3965 x = convert_modes (wider_mode, mode, x, unsignedp);
3966
3967 if (!insn_operand_matches (icode, opnum, x))
3968 {
3969 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
3970 if (reload_completed)
3971 return NULL_RTX;
3972 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
3973 return NULL_RTX;
3974 x = copy_to_mode_reg (op_mode, x);
3975 }
3976
3977 return x;
3978 }
3979
3980 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
3981 we can do the branch. */
3982
3983 static void
3984 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label,
3985 profile_probability prob)
3986 {
3987 machine_mode optab_mode;
3988 enum mode_class mclass;
3989 enum insn_code icode;
3990 rtx_insn *insn;
3991
3992 mclass = GET_MODE_CLASS (mode);
3993 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
3994 icode = optab_handler (cbranch_optab, optab_mode);
3995
3996 gcc_assert (icode != CODE_FOR_nothing);
3997 gcc_assert (insn_operand_matches (icode, 0, test));
3998 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
3999 XEXP (test, 1), label));
4000 if (prob.initialized_p ()
4001 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4002 && insn
4003 && JUMP_P (insn)
4004 && any_condjump_p (insn)
4005 && !find_reg_note (insn, REG_BR_PROB, 0))
4006 add_reg_br_prob_note (insn, prob);
4007 }
4008
4009 /* Generate code to compare X with Y so that the condition codes are
4010 set and to jump to LABEL if the condition is true. If X is a
4011 constant and Y is not a constant, then the comparison is swapped to
4012 ensure that the comparison RTL has the canonical form.
4013
4014 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4015 need to be widened. UNSIGNEDP is also used to select the proper
4016 branch condition code.
4017
4018 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4019
4020 MODE is the mode of the inputs (in case they are const_int).
4021
4022 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4023 It will be potentially converted into an unsigned variant based on
4024 UNSIGNEDP to select a proper jump instruction.
4025
4026 PROB is the probability of jumping to LABEL. */
4027
4028 void
4029 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4030 machine_mode mode, int unsignedp, rtx label,
4031 profile_probability prob)
4032 {
4033 rtx op0 = x, op1 = y;
4034 rtx test;
4035
4036 /* Swap operands and condition to ensure canonical RTL. */
4037 if (swap_commutative_operands_p (x, y)
4038 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4039 {
4040 op0 = y, op1 = x;
4041 comparison = swap_condition (comparison);
4042 }
4043
4044 /* If OP0 is still a constant, then both X and Y must be constants
4045 or the opposite comparison is not supported. Force X into a register
4046 to create canonical RTL. */
4047 if (CONSTANT_P (op0))
4048 op0 = force_reg (mode, op0);
4049
4050 if (unsignedp)
4051 comparison = unsigned_condition (comparison);
4052
4053 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4054 &test, &mode);
4055 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4056 }
4057
4058 \f
4059 /* Emit a library call comparison between floating point X and Y.
4060 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4061
4062 static void
4063 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4064 rtx *ptest, machine_mode *pmode)
4065 {
4066 enum rtx_code swapped = swap_condition (comparison);
4067 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4068 machine_mode orig_mode = GET_MODE (x);
4069 machine_mode mode;
4070 rtx true_rtx, false_rtx;
4071 rtx value, target, equiv;
4072 rtx_insn *insns;
4073 rtx libfunc = 0;
4074 bool reversed_p = false;
4075 scalar_int_mode cmp_mode = targetm.libgcc_cmp_return_mode ();
4076
4077 FOR_EACH_MODE_FROM (mode, orig_mode)
4078 {
4079 if (code_to_optab (comparison)
4080 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4081 break;
4082
4083 if (code_to_optab (swapped)
4084 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4085 {
4086 std::swap (x, y);
4087 comparison = swapped;
4088 break;
4089 }
4090
4091 if (code_to_optab (reversed)
4092 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4093 {
4094 comparison = reversed;
4095 reversed_p = true;
4096 break;
4097 }
4098 }
4099
4100 gcc_assert (mode != VOIDmode);
4101
4102 if (mode != orig_mode)
4103 {
4104 x = convert_to_mode (mode, x, 0);
4105 y = convert_to_mode (mode, y, 0);
4106 }
4107
4108 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4109 the RTL. The allows the RTL optimizers to delete the libcall if the
4110 condition can be determined at compile-time. */
4111 if (comparison == UNORDERED
4112 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4113 {
4114 true_rtx = const_true_rtx;
4115 false_rtx = const0_rtx;
4116 }
4117 else
4118 {
4119 switch (comparison)
4120 {
4121 case EQ:
4122 true_rtx = const0_rtx;
4123 false_rtx = const_true_rtx;
4124 break;
4125
4126 case NE:
4127 true_rtx = const_true_rtx;
4128 false_rtx = const0_rtx;
4129 break;
4130
4131 case GT:
4132 true_rtx = const1_rtx;
4133 false_rtx = const0_rtx;
4134 break;
4135
4136 case GE:
4137 true_rtx = const0_rtx;
4138 false_rtx = constm1_rtx;
4139 break;
4140
4141 case LT:
4142 true_rtx = constm1_rtx;
4143 false_rtx = const0_rtx;
4144 break;
4145
4146 case LE:
4147 true_rtx = const0_rtx;
4148 false_rtx = const1_rtx;
4149 break;
4150
4151 default:
4152 gcc_unreachable ();
4153 }
4154 }
4155
4156 if (comparison == UNORDERED)
4157 {
4158 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4159 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4160 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4161 temp, const_true_rtx, equiv);
4162 }
4163 else
4164 {
4165 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4166 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4167 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4168 equiv, true_rtx, false_rtx);
4169 }
4170
4171 start_sequence ();
4172 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4173 cmp_mode, x, mode, y, mode);
4174 insns = get_insns ();
4175 end_sequence ();
4176
4177 target = gen_reg_rtx (cmp_mode);
4178 emit_libcall_block (insns, target, value, equiv);
4179
4180 if (comparison == UNORDERED
4181 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4182 || reversed_p)
4183 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4184 else
4185 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4186
4187 *pmode = cmp_mode;
4188 }
4189 \f
4190 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4191
4192 void
4193 emit_indirect_jump (rtx loc)
4194 {
4195 if (!targetm.have_indirect_jump ())
4196 sorry ("indirect jumps are not available on this target");
4197 else
4198 {
4199 struct expand_operand ops[1];
4200 create_address_operand (&ops[0], loc);
4201 expand_jump_insn (targetm.code_for_indirect_jump, 1, ops);
4202 emit_barrier ();
4203 }
4204 }
4205 \f
4206
4207 /* Emit a conditional move instruction if the machine supports one for that
4208 condition and machine mode.
4209
4210 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4211 the mode to use should they be constants. If it is VOIDmode, they cannot
4212 both be constants.
4213
4214 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4215 should be stored there. MODE is the mode to use should they be constants.
4216 If it is VOIDmode, they cannot both be constants.
4217
4218 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4219 is not supported. */
4220
4221 rtx
4222 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4223 machine_mode cmode, rtx op2, rtx op3,
4224 machine_mode mode, int unsignedp)
4225 {
4226 rtx comparison;
4227 rtx_insn *last;
4228 enum insn_code icode;
4229 enum rtx_code reversed;
4230
4231 /* If the two source operands are identical, that's just a move. */
4232
4233 if (rtx_equal_p (op2, op3))
4234 {
4235 if (!target)
4236 target = gen_reg_rtx (mode);
4237
4238 emit_move_insn (target, op3);
4239 return target;
4240 }
4241
4242 /* If one operand is constant, make it the second one. Only do this
4243 if the other operand is not constant as well. */
4244
4245 if (swap_commutative_operands_p (op0, op1))
4246 {
4247 std::swap (op0, op1);
4248 code = swap_condition (code);
4249 }
4250
4251 /* get_condition will prefer to generate LT and GT even if the old
4252 comparison was against zero, so undo that canonicalization here since
4253 comparisons against zero are cheaper. */
4254 if (code == LT && op1 == const1_rtx)
4255 code = LE, op1 = const0_rtx;
4256 else if (code == GT && op1 == constm1_rtx)
4257 code = GE, op1 = const0_rtx;
4258
4259 if (cmode == VOIDmode)
4260 cmode = GET_MODE (op0);
4261
4262 enum rtx_code orig_code = code;
4263 bool swapped = false;
4264 if (swap_commutative_operands_p (op2, op3)
4265 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4266 != UNKNOWN))
4267 {
4268 std::swap (op2, op3);
4269 code = reversed;
4270 swapped = true;
4271 }
4272
4273 if (mode == VOIDmode)
4274 mode = GET_MODE (op2);
4275
4276 icode = direct_optab_handler (movcc_optab, mode);
4277
4278 if (icode == CODE_FOR_nothing)
4279 return NULL_RTX;
4280
4281 if (!target)
4282 target = gen_reg_rtx (mode);
4283
4284 for (int pass = 0; ; pass++)
4285 {
4286 code = unsignedp ? unsigned_condition (code) : code;
4287 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4288
4289 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4290 punt and let the caller figure out how best to deal with this
4291 situation. */
4292 if (COMPARISON_P (comparison))
4293 {
4294 saved_pending_stack_adjust save;
4295 save_pending_stack_adjust (&save);
4296 last = get_last_insn ();
4297 do_pending_stack_adjust ();
4298 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4299 GET_CODE (comparison), NULL_RTX, unsignedp,
4300 OPTAB_WIDEN, &comparison, &cmode);
4301 if (comparison)
4302 {
4303 struct expand_operand ops[4];
4304
4305 create_output_operand (&ops[0], target, mode);
4306 create_fixed_operand (&ops[1], comparison);
4307 create_input_operand (&ops[2], op2, mode);
4308 create_input_operand (&ops[3], op3, mode);
4309 if (maybe_expand_insn (icode, 4, ops))
4310 {
4311 if (ops[0].value != target)
4312 convert_move (target, ops[0].value, false);
4313 return target;
4314 }
4315 }
4316 delete_insns_since (last);
4317 restore_pending_stack_adjust (&save);
4318 }
4319
4320 if (pass == 1)
4321 return NULL_RTX;
4322
4323 /* If the preferred op2/op3 order is not usable, retry with other
4324 operand order, perhaps it will expand successfully. */
4325 if (swapped)
4326 code = orig_code;
4327 else if ((reversed = reversed_comparison_code_parts (orig_code, op0, op1,
4328 NULL))
4329 != UNKNOWN)
4330 code = reversed;
4331 else
4332 return NULL_RTX;
4333 std::swap (op2, op3);
4334 }
4335 }
4336
4337
4338 /* Emit a conditional negate or bitwise complement using the
4339 negcc or notcc optabs if available. Return NULL_RTX if such operations
4340 are not available. Otherwise return the RTX holding the result.
4341 TARGET is the desired destination of the result. COMP is the comparison
4342 on which to negate. If COND is true move into TARGET the negation
4343 or bitwise complement of OP1. Otherwise move OP2 into TARGET.
4344 CODE is either NEG or NOT. MODE is the machine mode in which the
4345 operation is performed. */
4346
4347 rtx
4348 emit_conditional_neg_or_complement (rtx target, rtx_code code,
4349 machine_mode mode, rtx cond, rtx op1,
4350 rtx op2)
4351 {
4352 optab op = unknown_optab;
4353 if (code == NEG)
4354 op = negcc_optab;
4355 else if (code == NOT)
4356 op = notcc_optab;
4357 else
4358 gcc_unreachable ();
4359
4360 insn_code icode = direct_optab_handler (op, mode);
4361
4362 if (icode == CODE_FOR_nothing)
4363 return NULL_RTX;
4364
4365 if (!target)
4366 target = gen_reg_rtx (mode);
4367
4368 rtx_insn *last = get_last_insn ();
4369 struct expand_operand ops[4];
4370
4371 create_output_operand (&ops[0], target, mode);
4372 create_fixed_operand (&ops[1], cond);
4373 create_input_operand (&ops[2], op1, mode);
4374 create_input_operand (&ops[3], op2, mode);
4375
4376 if (maybe_expand_insn (icode, 4, ops))
4377 {
4378 if (ops[0].value != target)
4379 convert_move (target, ops[0].value, false);
4380
4381 return target;
4382 }
4383 delete_insns_since (last);
4384 return NULL_RTX;
4385 }
4386
4387 /* Emit a conditional addition instruction if the machine supports one for that
4388 condition and machine mode.
4389
4390 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4391 the mode to use should they be constants. If it is VOIDmode, they cannot
4392 both be constants.
4393
4394 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4395 should be stored there. MODE is the mode to use should they be constants.
4396 If it is VOIDmode, they cannot both be constants.
4397
4398 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4399 is not supported. */
4400
4401 rtx
4402 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4403 machine_mode cmode, rtx op2, rtx op3,
4404 machine_mode mode, int unsignedp)
4405 {
4406 rtx comparison;
4407 rtx_insn *last;
4408 enum insn_code icode;
4409
4410 /* If one operand is constant, make it the second one. Only do this
4411 if the other operand is not constant as well. */
4412
4413 if (swap_commutative_operands_p (op0, op1))
4414 {
4415 std::swap (op0, op1);
4416 code = swap_condition (code);
4417 }
4418
4419 /* get_condition will prefer to generate LT and GT even if the old
4420 comparison was against zero, so undo that canonicalization here since
4421 comparisons against zero are cheaper. */
4422 if (code == LT && op1 == const1_rtx)
4423 code = LE, op1 = const0_rtx;
4424 else if (code == GT && op1 == constm1_rtx)
4425 code = GE, op1 = const0_rtx;
4426
4427 if (cmode == VOIDmode)
4428 cmode = GET_MODE (op0);
4429
4430 if (mode == VOIDmode)
4431 mode = GET_MODE (op2);
4432
4433 icode = optab_handler (addcc_optab, mode);
4434
4435 if (icode == CODE_FOR_nothing)
4436 return 0;
4437
4438 if (!target)
4439 target = gen_reg_rtx (mode);
4440
4441 code = unsignedp ? unsigned_condition (code) : code;
4442 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4443
4444 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4445 return NULL and let the caller figure out how best to deal with this
4446 situation. */
4447 if (!COMPARISON_P (comparison))
4448 return NULL_RTX;
4449
4450 do_pending_stack_adjust ();
4451 last = get_last_insn ();
4452 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4453 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4454 &comparison, &cmode);
4455 if (comparison)
4456 {
4457 struct expand_operand ops[4];
4458
4459 create_output_operand (&ops[0], target, mode);
4460 create_fixed_operand (&ops[1], comparison);
4461 create_input_operand (&ops[2], op2, mode);
4462 create_input_operand (&ops[3], op3, mode);
4463 if (maybe_expand_insn (icode, 4, ops))
4464 {
4465 if (ops[0].value != target)
4466 convert_move (target, ops[0].value, false);
4467 return target;
4468 }
4469 }
4470 delete_insns_since (last);
4471 return NULL_RTX;
4472 }
4473 \f
4474 /* These functions attempt to generate an insn body, rather than
4475 emitting the insn, but if the gen function already emits them, we
4476 make no attempt to turn them back into naked patterns. */
4477
4478 /* Generate and return an insn body to add Y to X. */
4479
4480 rtx_insn *
4481 gen_add2_insn (rtx x, rtx y)
4482 {
4483 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4484
4485 gcc_assert (insn_operand_matches (icode, 0, x));
4486 gcc_assert (insn_operand_matches (icode, 1, x));
4487 gcc_assert (insn_operand_matches (icode, 2, y));
4488
4489 return GEN_FCN (icode) (x, x, y);
4490 }
4491
4492 /* Generate and return an insn body to add r1 and c,
4493 storing the result in r0. */
4494
4495 rtx_insn *
4496 gen_add3_insn (rtx r0, rtx r1, rtx c)
4497 {
4498 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4499
4500 if (icode == CODE_FOR_nothing
4501 || !insn_operand_matches (icode, 0, r0)
4502 || !insn_operand_matches (icode, 1, r1)
4503 || !insn_operand_matches (icode, 2, c))
4504 return NULL;
4505
4506 return GEN_FCN (icode) (r0, r1, c);
4507 }
4508
4509 int
4510 have_add2_insn (rtx x, rtx y)
4511 {
4512 enum insn_code icode;
4513
4514 gcc_assert (GET_MODE (x) != VOIDmode);
4515
4516 icode = optab_handler (add_optab, GET_MODE (x));
4517
4518 if (icode == CODE_FOR_nothing)
4519 return 0;
4520
4521 if (!insn_operand_matches (icode, 0, x)
4522 || !insn_operand_matches (icode, 1, x)
4523 || !insn_operand_matches (icode, 2, y))
4524 return 0;
4525
4526 return 1;
4527 }
4528
4529 /* Generate and return an insn body to add Y to X. */
4530
4531 rtx_insn *
4532 gen_addptr3_insn (rtx x, rtx y, rtx z)
4533 {
4534 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4535
4536 gcc_assert (insn_operand_matches (icode, 0, x));
4537 gcc_assert (insn_operand_matches (icode, 1, y));
4538 gcc_assert (insn_operand_matches (icode, 2, z));
4539
4540 return GEN_FCN (icode) (x, y, z);
4541 }
4542
4543 /* Return true if the target implements an addptr pattern and X, Y,
4544 and Z are valid for the pattern predicates. */
4545
4546 int
4547 have_addptr3_insn (rtx x, rtx y, rtx z)
4548 {
4549 enum insn_code icode;
4550
4551 gcc_assert (GET_MODE (x) != VOIDmode);
4552
4553 icode = optab_handler (addptr3_optab, GET_MODE (x));
4554
4555 if (icode == CODE_FOR_nothing)
4556 return 0;
4557
4558 if (!insn_operand_matches (icode, 0, x)
4559 || !insn_operand_matches (icode, 1, y)
4560 || !insn_operand_matches (icode, 2, z))
4561 return 0;
4562
4563 return 1;
4564 }
4565
4566 /* Generate and return an insn body to subtract Y from X. */
4567
4568 rtx_insn *
4569 gen_sub2_insn (rtx x, rtx y)
4570 {
4571 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4572
4573 gcc_assert (insn_operand_matches (icode, 0, x));
4574 gcc_assert (insn_operand_matches (icode, 1, x));
4575 gcc_assert (insn_operand_matches (icode, 2, y));
4576
4577 return GEN_FCN (icode) (x, x, y);
4578 }
4579
4580 /* Generate and return an insn body to subtract r1 and c,
4581 storing the result in r0. */
4582
4583 rtx_insn *
4584 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4585 {
4586 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4587
4588 if (icode == CODE_FOR_nothing
4589 || !insn_operand_matches (icode, 0, r0)
4590 || !insn_operand_matches (icode, 1, r1)
4591 || !insn_operand_matches (icode, 2, c))
4592 return NULL;
4593
4594 return GEN_FCN (icode) (r0, r1, c);
4595 }
4596
4597 int
4598 have_sub2_insn (rtx x, rtx y)
4599 {
4600 enum insn_code icode;
4601
4602 gcc_assert (GET_MODE (x) != VOIDmode);
4603
4604 icode = optab_handler (sub_optab, GET_MODE (x));
4605
4606 if (icode == CODE_FOR_nothing)
4607 return 0;
4608
4609 if (!insn_operand_matches (icode, 0, x)
4610 || !insn_operand_matches (icode, 1, x)
4611 || !insn_operand_matches (icode, 2, y))
4612 return 0;
4613
4614 return 1;
4615 }
4616 \f
4617 /* Generate the body of an insn to extend Y (with mode MFROM)
4618 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4619
4620 rtx_insn *
4621 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4622 machine_mode mfrom, int unsignedp)
4623 {
4624 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4625 return GEN_FCN (icode) (x, y);
4626 }
4627 \f
4628 /* Generate code to convert FROM to floating point
4629 and store in TO. FROM must be fixed point and not VOIDmode.
4630 UNSIGNEDP nonzero means regard FROM as unsigned.
4631 Normally this is done by correcting the final value
4632 if it is negative. */
4633
4634 void
4635 expand_float (rtx to, rtx from, int unsignedp)
4636 {
4637 enum insn_code icode;
4638 rtx target = to;
4639 scalar_mode from_mode, to_mode;
4640 machine_mode fmode, imode;
4641 bool can_do_signed = false;
4642
4643 /* Crash now, because we won't be able to decide which mode to use. */
4644 gcc_assert (GET_MODE (from) != VOIDmode);
4645
4646 /* Look for an insn to do the conversion. Do it in the specified
4647 modes if possible; otherwise convert either input, output or both to
4648 wider mode. If the integer mode is wider than the mode of FROM,
4649 we can do the conversion signed even if the input is unsigned. */
4650
4651 FOR_EACH_MODE_FROM (fmode, GET_MODE (to))
4652 FOR_EACH_MODE_FROM (imode, GET_MODE (from))
4653 {
4654 int doing_unsigned = unsignedp;
4655
4656 if (fmode != GET_MODE (to)
4657 && (significand_size (fmode)
4658 < GET_MODE_UNIT_PRECISION (GET_MODE (from))))
4659 continue;
4660
4661 icode = can_float_p (fmode, imode, unsignedp);
4662 if (icode == CODE_FOR_nothing && unsignedp)
4663 {
4664 enum insn_code scode = can_float_p (fmode, imode, 0);
4665 if (scode != CODE_FOR_nothing)
4666 can_do_signed = true;
4667 if (imode != GET_MODE (from))
4668 icode = scode, doing_unsigned = 0;
4669 }
4670
4671 if (icode != CODE_FOR_nothing)
4672 {
4673 if (imode != GET_MODE (from))
4674 from = convert_to_mode (imode, from, unsignedp);
4675
4676 if (fmode != GET_MODE (to))
4677 target = gen_reg_rtx (fmode);
4678
4679 emit_unop_insn (icode, target, from,
4680 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4681
4682 if (target != to)
4683 convert_move (to, target, 0);
4684 return;
4685 }
4686 }
4687
4688 /* Unsigned integer, and no way to convert directly. Convert as signed,
4689 then unconditionally adjust the result. */
4690 if (unsignedp
4691 && can_do_signed
4692 && is_a <scalar_mode> (GET_MODE (to), &to_mode)
4693 && is_a <scalar_mode> (GET_MODE (from), &from_mode))
4694 {
4695 opt_scalar_mode fmode_iter;
4696 rtx_code_label *label = gen_label_rtx ();
4697 rtx temp;
4698 REAL_VALUE_TYPE offset;
4699
4700 /* Look for a usable floating mode FMODE wider than the source and at
4701 least as wide as the target. Using FMODE will avoid rounding woes
4702 with unsigned values greater than the signed maximum value. */
4703
4704 FOR_EACH_MODE_FROM (fmode_iter, to_mode)
4705 {
4706 scalar_mode fmode = fmode_iter.require ();
4707 if (GET_MODE_PRECISION (from_mode) < GET_MODE_BITSIZE (fmode)
4708 && can_float_p (fmode, from_mode, 0) != CODE_FOR_nothing)
4709 break;
4710 }
4711
4712 if (!fmode_iter.exists (&fmode))
4713 {
4714 /* There is no such mode. Pretend the target is wide enough. */
4715 fmode = to_mode;
4716
4717 /* Avoid double-rounding when TO is narrower than FROM. */
4718 if ((significand_size (fmode) + 1)
4719 < GET_MODE_PRECISION (from_mode))
4720 {
4721 rtx temp1;
4722 rtx_code_label *neglabel = gen_label_rtx ();
4723
4724 /* Don't use TARGET if it isn't a register, is a hard register,
4725 or is the wrong mode. */
4726 if (!REG_P (target)
4727 || REGNO (target) < FIRST_PSEUDO_REGISTER
4728 || GET_MODE (target) != fmode)
4729 target = gen_reg_rtx (fmode);
4730
4731 imode = from_mode;
4732 do_pending_stack_adjust ();
4733
4734 /* Test whether the sign bit is set. */
4735 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4736 0, neglabel);
4737
4738 /* The sign bit is not set. Convert as signed. */
4739 expand_float (target, from, 0);
4740 emit_jump_insn (targetm.gen_jump (label));
4741 emit_barrier ();
4742
4743 /* The sign bit is set.
4744 Convert to a usable (positive signed) value by shifting right
4745 one bit, while remembering if a nonzero bit was shifted
4746 out; i.e., compute (from & 1) | (from >> 1). */
4747
4748 emit_label (neglabel);
4749 temp = expand_binop (imode, and_optab, from, const1_rtx,
4750 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4751 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
4752 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4753 OPTAB_LIB_WIDEN);
4754 expand_float (target, temp, 0);
4755
4756 /* Multiply by 2 to undo the shift above. */
4757 temp = expand_binop (fmode, add_optab, target, target,
4758 target, 0, OPTAB_LIB_WIDEN);
4759 if (temp != target)
4760 emit_move_insn (target, temp);
4761
4762 do_pending_stack_adjust ();
4763 emit_label (label);
4764 goto done;
4765 }
4766 }
4767
4768 /* If we are about to do some arithmetic to correct for an
4769 unsigned operand, do it in a pseudo-register. */
4770
4771 if (to_mode != fmode
4772 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
4773 target = gen_reg_rtx (fmode);
4774
4775 /* Convert as signed integer to floating. */
4776 expand_float (target, from, 0);
4777
4778 /* If FROM is negative (and therefore TO is negative),
4779 correct its value by 2**bitwidth. */
4780
4781 do_pending_stack_adjust ();
4782 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, from_mode,
4783 0, label);
4784
4785
4786 real_2expN (&offset, GET_MODE_PRECISION (from_mode), fmode);
4787 temp = expand_binop (fmode, add_optab, target,
4788 const_double_from_real_value (offset, fmode),
4789 target, 0, OPTAB_LIB_WIDEN);
4790 if (temp != target)
4791 emit_move_insn (target, temp);
4792
4793 do_pending_stack_adjust ();
4794 emit_label (label);
4795 goto done;
4796 }
4797
4798 /* No hardware instruction available; call a library routine. */
4799 {
4800 rtx libfunc;
4801 rtx_insn *insns;
4802 rtx value;
4803 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
4804
4805 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
4806 from = convert_to_mode (SImode, from, unsignedp);
4807
4808 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
4809 gcc_assert (libfunc);
4810
4811 start_sequence ();
4812
4813 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4814 GET_MODE (to), from, GET_MODE (from));
4815 insns = get_insns ();
4816 end_sequence ();
4817
4818 emit_libcall_block (insns, target, value,
4819 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
4820 GET_MODE (to), from));
4821 }
4822
4823 done:
4824
4825 /* Copy result to requested destination
4826 if we have been computing in a temp location. */
4827
4828 if (target != to)
4829 {
4830 if (GET_MODE (target) == GET_MODE (to))
4831 emit_move_insn (to, target);
4832 else
4833 convert_move (to, target, 0);
4834 }
4835 }
4836 \f
4837 /* Generate code to convert FROM to fixed point and store in TO. FROM
4838 must be floating point. */
4839
4840 void
4841 expand_fix (rtx to, rtx from, int unsignedp)
4842 {
4843 enum insn_code icode;
4844 rtx target = to;
4845 machine_mode fmode, imode;
4846 opt_scalar_mode fmode_iter;
4847 bool must_trunc = false;
4848
4849 /* We first try to find a pair of modes, one real and one integer, at
4850 least as wide as FROM and TO, respectively, in which we can open-code
4851 this conversion. If the integer mode is wider than the mode of TO,
4852 we can do the conversion either signed or unsigned. */
4853
4854 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
4855 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
4856 {
4857 int doing_unsigned = unsignedp;
4858
4859 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
4860 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
4861 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
4862
4863 if (icode != CODE_FOR_nothing)
4864 {
4865 rtx_insn *last = get_last_insn ();
4866 if (fmode != GET_MODE (from))
4867 from = convert_to_mode (fmode, from, 0);
4868
4869 if (must_trunc)
4870 {
4871 rtx temp = gen_reg_rtx (GET_MODE (from));
4872 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
4873 temp, 0);
4874 }
4875
4876 if (imode != GET_MODE (to))
4877 target = gen_reg_rtx (imode);
4878
4879 if (maybe_emit_unop_insn (icode, target, from,
4880 doing_unsigned ? UNSIGNED_FIX : FIX))
4881 {
4882 if (target != to)
4883 convert_move (to, target, unsignedp);
4884 return;
4885 }
4886 delete_insns_since (last);
4887 }
4888 }
4889
4890 /* For an unsigned conversion, there is one more way to do it.
4891 If we have a signed conversion, we generate code that compares
4892 the real value to the largest representable positive number. If if
4893 is smaller, the conversion is done normally. Otherwise, subtract
4894 one plus the highest signed number, convert, and add it back.
4895
4896 We only need to check all real modes, since we know we didn't find
4897 anything with a wider integer mode.
4898
4899 This code used to extend FP value into mode wider than the destination.
4900 This is needed for decimal float modes which cannot accurately
4901 represent one plus the highest signed number of the same size, but
4902 not for binary modes. Consider, for instance conversion from SFmode
4903 into DImode.
4904
4905 The hot path through the code is dealing with inputs smaller than 2^63
4906 and doing just the conversion, so there is no bits to lose.
4907
4908 In the other path we know the value is positive in the range 2^63..2^64-1
4909 inclusive. (as for other input overflow happens and result is undefined)
4910 So we know that the most important bit set in mantissa corresponds to
4911 2^63. The subtraction of 2^63 should not generate any rounding as it
4912 simply clears out that bit. The rest is trivial. */
4913
4914 scalar_int_mode to_mode;
4915 if (unsignedp
4916 && is_a <scalar_int_mode> (GET_MODE (to), &to_mode)
4917 && HWI_COMPUTABLE_MODE_P (to_mode))
4918 FOR_EACH_MODE_FROM (fmode_iter, as_a <scalar_mode> (GET_MODE (from)))
4919 {
4920 scalar_mode fmode = fmode_iter.require ();
4921 if (CODE_FOR_nothing != can_fix_p (to_mode, fmode,
4922 0, &must_trunc)
4923 && (!DECIMAL_FLOAT_MODE_P (fmode)
4924 || (GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (to_mode))))
4925 {
4926 int bitsize;
4927 REAL_VALUE_TYPE offset;
4928 rtx limit;
4929 rtx_code_label *lab1, *lab2;
4930 rtx_insn *insn;
4931
4932 bitsize = GET_MODE_PRECISION (to_mode);
4933 real_2expN (&offset, bitsize - 1, fmode);
4934 limit = const_double_from_real_value (offset, fmode);
4935 lab1 = gen_label_rtx ();
4936 lab2 = gen_label_rtx ();
4937
4938 if (fmode != GET_MODE (from))
4939 from = convert_to_mode (fmode, from, 0);
4940
4941 /* See if we need to do the subtraction. */
4942 do_pending_stack_adjust ();
4943 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX,
4944 GET_MODE (from), 0, lab1);
4945
4946 /* If not, do the signed "fix" and branch around fixup code. */
4947 expand_fix (to, from, 0);
4948 emit_jump_insn (targetm.gen_jump (lab2));
4949 emit_barrier ();
4950
4951 /* Otherwise, subtract 2**(N-1), convert to signed number,
4952 then add 2**(N-1). Do the addition using XOR since this
4953 will often generate better code. */
4954 emit_label (lab1);
4955 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
4956 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4957 expand_fix (to, target, 0);
4958 target = expand_binop (to_mode, xor_optab, to,
4959 gen_int_mode
4960 (HOST_WIDE_INT_1 << (bitsize - 1),
4961 to_mode),
4962 to, 1, OPTAB_LIB_WIDEN);
4963
4964 if (target != to)
4965 emit_move_insn (to, target);
4966
4967 emit_label (lab2);
4968
4969 if (optab_handler (mov_optab, to_mode) != CODE_FOR_nothing)
4970 {
4971 /* Make a place for a REG_NOTE and add it. */
4972 insn = emit_move_insn (to, to);
4973 set_dst_reg_note (insn, REG_EQUAL,
4974 gen_rtx_fmt_e (UNSIGNED_FIX, to_mode,
4975 copy_rtx (from)),
4976 to);
4977 }
4978
4979 return;
4980 }
4981 }
4982
4983 /* We can't do it with an insn, so use a library call. But first ensure
4984 that the mode of TO is at least as wide as SImode, since those are the
4985 only library calls we know about. */
4986
4987 if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
4988 {
4989 target = gen_reg_rtx (SImode);
4990
4991 expand_fix (target, from, unsignedp);
4992 }
4993 else
4994 {
4995 rtx_insn *insns;
4996 rtx value;
4997 rtx libfunc;
4998
4999 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5000 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5001 gcc_assert (libfunc);
5002
5003 start_sequence ();
5004
5005 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5006 GET_MODE (to), from, GET_MODE (from));
5007 insns = get_insns ();
5008 end_sequence ();
5009
5010 emit_libcall_block (insns, target, value,
5011 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5012 GET_MODE (to), from));
5013 }
5014
5015 if (target != to)
5016 {
5017 if (GET_MODE (to) == GET_MODE (target))
5018 emit_move_insn (to, target);
5019 else
5020 convert_move (to, target, 0);
5021 }
5022 }
5023
5024
5025 /* Promote integer arguments for a libcall if necessary.
5026 emit_library_call_value cannot do the promotion because it does not
5027 know if it should do a signed or unsigned promotion. This is because
5028 there are no tree types defined for libcalls. */
5029
5030 static rtx
5031 prepare_libcall_arg (rtx arg, int uintp)
5032 {
5033 scalar_int_mode mode;
5034 machine_mode arg_mode;
5035 if (is_a <scalar_int_mode> (GET_MODE (arg), &mode))
5036 {
5037 /* If we need to promote the integer function argument we need to do
5038 it here instead of inside emit_library_call_value because in
5039 emit_library_call_value we don't know if we should do a signed or
5040 unsigned promotion. */
5041
5042 int unsigned_p = 0;
5043 arg_mode = promote_function_mode (NULL_TREE, mode,
5044 &unsigned_p, NULL_TREE, 0);
5045 if (arg_mode != mode)
5046 return convert_to_mode (arg_mode, arg, uintp);
5047 }
5048 return arg;
5049 }
5050
5051 /* Generate code to convert FROM or TO a fixed-point.
5052 If UINTP is true, either TO or FROM is an unsigned integer.
5053 If SATP is true, we need to saturate the result. */
5054
5055 void
5056 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5057 {
5058 machine_mode to_mode = GET_MODE (to);
5059 machine_mode from_mode = GET_MODE (from);
5060 convert_optab tab;
5061 enum rtx_code this_code;
5062 enum insn_code code;
5063 rtx_insn *insns;
5064 rtx value;
5065 rtx libfunc;
5066
5067 if (to_mode == from_mode)
5068 {
5069 emit_move_insn (to, from);
5070 return;
5071 }
5072
5073 if (uintp)
5074 {
5075 tab = satp ? satfractuns_optab : fractuns_optab;
5076 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5077 }
5078 else
5079 {
5080 tab = satp ? satfract_optab : fract_optab;
5081 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5082 }
5083 code = convert_optab_handler (tab, to_mode, from_mode);
5084 if (code != CODE_FOR_nothing)
5085 {
5086 emit_unop_insn (code, to, from, this_code);
5087 return;
5088 }
5089
5090 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5091 gcc_assert (libfunc);
5092
5093 from = prepare_libcall_arg (from, uintp);
5094 from_mode = GET_MODE (from);
5095
5096 start_sequence ();
5097 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5098 from, from_mode);
5099 insns = get_insns ();
5100 end_sequence ();
5101
5102 emit_libcall_block (insns, to, value,
5103 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5104 }
5105
5106 /* Generate code to convert FROM to fixed point and store in TO. FROM
5107 must be floating point, TO must be signed. Use the conversion optab
5108 TAB to do the conversion. */
5109
5110 bool
5111 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5112 {
5113 enum insn_code icode;
5114 rtx target = to;
5115 machine_mode fmode, imode;
5116
5117 /* We first try to find a pair of modes, one real and one integer, at
5118 least as wide as FROM and TO, respectively, in which we can open-code
5119 this conversion. If the integer mode is wider than the mode of TO,
5120 we can do the conversion either signed or unsigned. */
5121
5122 FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
5123 FOR_EACH_MODE_FROM (imode, GET_MODE (to))
5124 {
5125 icode = convert_optab_handler (tab, imode, fmode);
5126 if (icode != CODE_FOR_nothing)
5127 {
5128 rtx_insn *last = get_last_insn ();
5129 if (fmode != GET_MODE (from))
5130 from = convert_to_mode (fmode, from, 0);
5131
5132 if (imode != GET_MODE (to))
5133 target = gen_reg_rtx (imode);
5134
5135 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5136 {
5137 delete_insns_since (last);
5138 continue;
5139 }
5140 if (target != to)
5141 convert_move (to, target, 0);
5142 return true;
5143 }
5144 }
5145
5146 return false;
5147 }
5148 \f
5149 /* Report whether we have an instruction to perform the operation
5150 specified by CODE on operands of mode MODE. */
5151 int
5152 have_insn_for (enum rtx_code code, machine_mode mode)
5153 {
5154 return (code_to_optab (code)
5155 && (optab_handler (code_to_optab (code), mode)
5156 != CODE_FOR_nothing));
5157 }
5158
5159 /* Print information about the current contents of the optabs on
5160 STDERR. */
5161
5162 DEBUG_FUNCTION void
5163 debug_optab_libfuncs (void)
5164 {
5165 int i, j, k;
5166
5167 /* Dump the arithmetic optabs. */
5168 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
5169 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5170 {
5171 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
5172 if (l)
5173 {
5174 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5175 fprintf (stderr, "%s\t%s:\t%s\n",
5176 GET_RTX_NAME (optab_to_code ((optab) i)),
5177 GET_MODE_NAME (j),
5178 XSTR (l, 0));
5179 }
5180 }
5181
5182 /* Dump the conversion optabs. */
5183 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
5184 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5185 for (k = 0; k < NUM_MACHINE_MODES; ++k)
5186 {
5187 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
5188 (machine_mode) k);
5189 if (l)
5190 {
5191 gcc_assert (GET_CODE (l) == SYMBOL_REF);
5192 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
5193 GET_RTX_NAME (optab_to_code ((optab) i)),
5194 GET_MODE_NAME (j),
5195 GET_MODE_NAME (k),
5196 XSTR (l, 0));
5197 }
5198 }
5199 }
5200
5201 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5202 CODE. Return 0 on failure. */
5203
5204 rtx_insn *
5205 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
5206 {
5207 machine_mode mode = GET_MODE (op1);
5208 enum insn_code icode;
5209 rtx_insn *insn;
5210 rtx trap_rtx;
5211
5212 if (mode == VOIDmode)
5213 return 0;
5214
5215 icode = optab_handler (ctrap_optab, mode);
5216 if (icode == CODE_FOR_nothing)
5217 return 0;
5218
5219 /* Some targets only accept a zero trap code. */
5220 if (!insn_operand_matches (icode, 3, tcode))
5221 return 0;
5222
5223 do_pending_stack_adjust ();
5224 start_sequence ();
5225 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
5226 &trap_rtx, &mode);
5227 if (!trap_rtx)
5228 insn = NULL;
5229 else
5230 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
5231 tcode);
5232
5233 /* If that failed, then give up. */
5234 if (insn == 0)
5235 {
5236 end_sequence ();
5237 return 0;
5238 }
5239
5240 emit_insn (insn);
5241 insn = get_insns ();
5242 end_sequence ();
5243 return insn;
5244 }
5245
5246 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
5247 or unsigned operation code. */
5248
5249 enum rtx_code
5250 get_rtx_code (enum tree_code tcode, bool unsignedp)
5251 {
5252 enum rtx_code code;
5253 switch (tcode)
5254 {
5255 case EQ_EXPR:
5256 code = EQ;
5257 break;
5258 case NE_EXPR:
5259 code = NE;
5260 break;
5261 case LT_EXPR:
5262 code = unsignedp ? LTU : LT;
5263 break;
5264 case LE_EXPR:
5265 code = unsignedp ? LEU : LE;
5266 break;
5267 case GT_EXPR:
5268 code = unsignedp ? GTU : GT;
5269 break;
5270 case GE_EXPR:
5271 code = unsignedp ? GEU : GE;
5272 break;
5273
5274 case UNORDERED_EXPR:
5275 code = UNORDERED;
5276 break;
5277 case ORDERED_EXPR:
5278 code = ORDERED;
5279 break;
5280 case UNLT_EXPR:
5281 code = UNLT;
5282 break;
5283 case UNLE_EXPR:
5284 code = UNLE;
5285 break;
5286 case UNGT_EXPR:
5287 code = UNGT;
5288 break;
5289 case UNGE_EXPR:
5290 code = UNGE;
5291 break;
5292 case UNEQ_EXPR:
5293 code = UNEQ;
5294 break;
5295 case LTGT_EXPR:
5296 code = LTGT;
5297 break;
5298
5299 case BIT_AND_EXPR:
5300 code = AND;
5301 break;
5302
5303 case BIT_IOR_EXPR:
5304 code = IOR;
5305 break;
5306
5307 default:
5308 gcc_unreachable ();
5309 }
5310 return code;
5311 }
5312
5313 /* Return a comparison rtx of mode CMP_MODE for COND. Use UNSIGNEDP to
5314 select signed or unsigned operators. OPNO holds the index of the
5315 first comparison operand for insn ICODE. Do not generate the
5316 compare instruction itself. */
5317
5318 static rtx
5319 vector_compare_rtx (machine_mode cmp_mode, enum tree_code tcode,
5320 tree t_op0, tree t_op1, bool unsignedp,
5321 enum insn_code icode, unsigned int opno)
5322 {
5323 struct expand_operand ops[2];
5324 rtx rtx_op0, rtx_op1;
5325 machine_mode m0, m1;
5326 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
5327
5328 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
5329
5330 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
5331 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
5332 cases, use the original mode. */
5333 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
5334 EXPAND_STACK_PARM);
5335 m0 = GET_MODE (rtx_op0);
5336 if (m0 == VOIDmode)
5337 m0 = TYPE_MODE (TREE_TYPE (t_op0));
5338
5339 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
5340 EXPAND_STACK_PARM);
5341 m1 = GET_MODE (rtx_op1);
5342 if (m1 == VOIDmode)
5343 m1 = TYPE_MODE (TREE_TYPE (t_op1));
5344
5345 create_input_operand (&ops[0], rtx_op0, m0);
5346 create_input_operand (&ops[1], rtx_op1, m1);
5347 if (!maybe_legitimize_operands (icode, opno, 2, ops))
5348 gcc_unreachable ();
5349 return gen_rtx_fmt_ee (rcode, cmp_mode, ops[0].value, ops[1].value);
5350 }
5351
5352 /* Checks if vec_perm mask SEL is a constant equivalent to a shift of the first
5353 vec_perm operand, assuming the second operand is a constant vector of zeroes.
5354 Return the shift distance in bits if so, or NULL_RTX if the vec_perm is not a
5355 shift. */
5356 static rtx
5357 shift_amt_for_vec_perm_mask (rtx sel)
5358 {
5359 unsigned int i, first, nelt = GET_MODE_NUNITS (GET_MODE (sel));
5360 unsigned int bitsize = GET_MODE_UNIT_BITSIZE (GET_MODE (sel));
5361
5362 if (GET_CODE (sel) != CONST_VECTOR)
5363 return NULL_RTX;
5364
5365 first = INTVAL (CONST_VECTOR_ELT (sel, 0));
5366 if (first >= nelt)
5367 return NULL_RTX;
5368 for (i = 1; i < nelt; i++)
5369 {
5370 int idx = INTVAL (CONST_VECTOR_ELT (sel, i));
5371 unsigned int expected = i + first;
5372 /* Indices into the second vector are all equivalent. */
5373 if (idx < 0 || (MIN (nelt, (unsigned) idx) != MIN (nelt, expected)))
5374 return NULL_RTX;
5375 }
5376
5377 return GEN_INT (first * bitsize);
5378 }
5379
5380 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
5381
5382 static rtx
5383 expand_vec_perm_1 (enum insn_code icode, rtx target,
5384 rtx v0, rtx v1, rtx sel)
5385 {
5386 machine_mode tmode = GET_MODE (target);
5387 machine_mode smode = GET_MODE (sel);
5388 struct expand_operand ops[4];
5389
5390 create_output_operand (&ops[0], target, tmode);
5391 create_input_operand (&ops[3], sel, smode);
5392
5393 /* Make an effort to preserve v0 == v1. The target expander is able to
5394 rely on this to determine if we're permuting a single input operand. */
5395 if (rtx_equal_p (v0, v1))
5396 {
5397 if (!insn_operand_matches (icode, 1, v0))
5398 v0 = force_reg (tmode, v0);
5399 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
5400 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
5401
5402 create_fixed_operand (&ops[1], v0);
5403 create_fixed_operand (&ops[2], v0);
5404 }
5405 else
5406 {
5407 create_input_operand (&ops[1], v0, tmode);
5408 create_input_operand (&ops[2], v1, tmode);
5409 }
5410
5411 if (maybe_expand_insn (icode, 4, ops))
5412 return ops[0].value;
5413 return NULL_RTX;
5414 }
5415
5416 /* Generate instructions for vec_perm optab given its mode
5417 and three operands. */
5418
5419 rtx
5420 expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
5421 {
5422 enum insn_code icode;
5423 machine_mode qimode;
5424 unsigned int i, w, e, u;
5425 rtx tmp, sel_qi = NULL;
5426 rtvec vec;
5427
5428 if (!target || GET_MODE (target) != mode)
5429 target = gen_reg_rtx (mode);
5430
5431 w = GET_MODE_SIZE (mode);
5432 e = GET_MODE_NUNITS (mode);
5433 u = GET_MODE_UNIT_SIZE (mode);
5434
5435 /* Set QIMODE to a different vector mode with byte elements.
5436 If no such mode, or if MODE already has byte elements, use VOIDmode. */
5437 if (GET_MODE_INNER (mode) == QImode
5438 || !mode_for_vector (QImode, w).exists (&qimode)
5439 || !VECTOR_MODE_P (qimode))
5440 qimode = VOIDmode;
5441
5442 /* If the input is a constant, expand it specially. */
5443 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
5444 if (GET_CODE (sel) == CONST_VECTOR)
5445 {
5446 /* See if this can be handled with a vec_shr. We only do this if the
5447 second vector is all zeroes. */
5448 enum insn_code shift_code = optab_handler (vec_shr_optab, mode);
5449 enum insn_code shift_code_qi = ((qimode != VOIDmode && qimode != mode)
5450 ? optab_handler (vec_shr_optab, qimode)
5451 : CODE_FOR_nothing);
5452 rtx shift_amt = NULL_RTX;
5453 if (v1 == CONST0_RTX (GET_MODE (v1))
5454 && (shift_code != CODE_FOR_nothing
5455 || shift_code_qi != CODE_FOR_nothing))
5456 {
5457 shift_amt = shift_amt_for_vec_perm_mask (sel);
5458 if (shift_amt)
5459 {
5460 struct expand_operand ops[3];
5461 if (shift_code != CODE_FOR_nothing)
5462 {
5463 create_output_operand (&ops[0], target, mode);
5464 create_input_operand (&ops[1], v0, mode);
5465 create_convert_operand_from_type (&ops[2], shift_amt,
5466 sizetype);
5467 if (maybe_expand_insn (shift_code, 3, ops))
5468 return ops[0].value;
5469 }
5470 if (shift_code_qi != CODE_FOR_nothing)
5471 {
5472 tmp = gen_reg_rtx (qimode);
5473 create_output_operand (&ops[0], tmp, qimode);
5474 create_input_operand (&ops[1], gen_lowpart (qimode, v0),
5475 qimode);
5476 create_convert_operand_from_type (&ops[2], shift_amt,
5477 sizetype);
5478 if (maybe_expand_insn (shift_code_qi, 3, ops))
5479 return gen_lowpart (mode, ops[0].value);
5480 }
5481 }
5482 }
5483
5484 icode = direct_optab_handler (vec_perm_const_optab, mode);
5485 if (icode != CODE_FOR_nothing)
5486 {
5487 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
5488 if (tmp)
5489 return tmp;
5490 }
5491
5492 /* Fall back to a constant byte-based permutation. */
5493 if (qimode != VOIDmode)
5494 {
5495 vec = rtvec_alloc (w);
5496 for (i = 0; i < e; ++i)
5497 {
5498 unsigned int j, this_e;
5499
5500 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
5501 this_e &= 2 * e - 1;
5502 this_e *= u;
5503
5504 for (j = 0; j < u; ++j)
5505 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
5506 }
5507 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
5508
5509 icode = direct_optab_handler (vec_perm_const_optab, qimode);
5510 if (icode != CODE_FOR_nothing)
5511 {
5512 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
5513 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
5514 gen_lowpart (qimode, v1), sel_qi);
5515 if (tmp)
5516 return gen_lowpart (mode, tmp);
5517 }
5518 }
5519 }
5520
5521 /* Otherwise expand as a fully variable permuation. */
5522 icode = direct_optab_handler (vec_perm_optab, mode);
5523 if (icode != CODE_FOR_nothing)
5524 {
5525 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
5526 if (tmp)
5527 return tmp;
5528 }
5529
5530 /* As a special case to aid several targets, lower the element-based
5531 permutation to a byte-based permutation and try again. */
5532 if (qimode == VOIDmode)
5533 return NULL_RTX;
5534 icode = direct_optab_handler (vec_perm_optab, qimode);
5535 if (icode == CODE_FOR_nothing)
5536 return NULL_RTX;
5537
5538 if (sel_qi == NULL)
5539 {
5540 /* Multiply each element by its byte size. */
5541 machine_mode selmode = GET_MODE (sel);
5542 if (u == 2)
5543 sel = expand_simple_binop (selmode, PLUS, sel, sel,
5544 NULL, 0, OPTAB_DIRECT);
5545 else
5546 sel = expand_simple_binop (selmode, ASHIFT, sel,
5547 GEN_INT (exact_log2 (u)),
5548 NULL, 0, OPTAB_DIRECT);
5549 gcc_assert (sel != NULL);
5550
5551 /* Broadcast the low byte each element into each of its bytes. */
5552 vec = rtvec_alloc (w);
5553 for (i = 0; i < w; ++i)
5554 {
5555 int this_e = i / u * u;
5556 if (BYTES_BIG_ENDIAN)
5557 this_e += u - 1;
5558 RTVEC_ELT (vec, i) = GEN_INT (this_e);
5559 }
5560 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
5561 sel = gen_lowpart (qimode, sel);
5562 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
5563 gcc_assert (sel != NULL);
5564
5565 /* Add the byte offset to each byte element. */
5566 /* Note that the definition of the indicies here is memory ordering,
5567 so there should be no difference between big and little endian. */
5568 vec = rtvec_alloc (w);
5569 for (i = 0; i < w; ++i)
5570 RTVEC_ELT (vec, i) = GEN_INT (i % u);
5571 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
5572 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
5573 sel, 0, OPTAB_DIRECT);
5574 gcc_assert (sel_qi != NULL);
5575 }
5576
5577 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
5578 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
5579 gen_lowpart (qimode, v1), sel_qi);
5580 if (tmp)
5581 tmp = gen_lowpart (mode, tmp);
5582 return tmp;
5583 }
5584
5585 /* Generate insns for a VEC_COND_EXPR with mask, given its TYPE and its
5586 three operands. */
5587
5588 rtx
5589 expand_vec_cond_mask_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
5590 rtx target)
5591 {
5592 struct expand_operand ops[4];
5593 machine_mode mode = TYPE_MODE (vec_cond_type);
5594 machine_mode mask_mode = TYPE_MODE (TREE_TYPE (op0));
5595 enum insn_code icode = get_vcond_mask_icode (mode, mask_mode);
5596 rtx mask, rtx_op1, rtx_op2;
5597
5598 if (icode == CODE_FOR_nothing)
5599 return 0;
5600
5601 mask = expand_normal (op0);
5602 rtx_op1 = expand_normal (op1);
5603 rtx_op2 = expand_normal (op2);
5604
5605 mask = force_reg (mask_mode, mask);
5606 rtx_op1 = force_reg (GET_MODE (rtx_op1), rtx_op1);
5607
5608 create_output_operand (&ops[0], target, mode);
5609 create_input_operand (&ops[1], rtx_op1, mode);
5610 create_input_operand (&ops[2], rtx_op2, mode);
5611 create_input_operand (&ops[3], mask, mask_mode);
5612 expand_insn (icode, 4, ops);
5613
5614 return ops[0].value;
5615 }
5616
5617 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
5618 three operands. */
5619
5620 rtx
5621 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
5622 rtx target)
5623 {
5624 struct expand_operand ops[6];
5625 enum insn_code icode;
5626 rtx comparison, rtx_op1, rtx_op2;
5627 machine_mode mode = TYPE_MODE (vec_cond_type);
5628 machine_mode cmp_op_mode;
5629 bool unsignedp;
5630 tree op0a, op0b;
5631 enum tree_code tcode;
5632
5633 if (COMPARISON_CLASS_P (op0))
5634 {
5635 op0a = TREE_OPERAND (op0, 0);
5636 op0b = TREE_OPERAND (op0, 1);
5637 tcode = TREE_CODE (op0);
5638 }
5639 else
5640 {
5641 gcc_assert (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (op0)));
5642 if (get_vcond_mask_icode (mode, TYPE_MODE (TREE_TYPE (op0)))
5643 != CODE_FOR_nothing)
5644 return expand_vec_cond_mask_expr (vec_cond_type, op0, op1,
5645 op2, target);
5646 /* Fake op0 < 0. */
5647 else
5648 {
5649 gcc_assert (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (op0)))
5650 == MODE_VECTOR_INT);
5651 op0a = op0;
5652 op0b = build_zero_cst (TREE_TYPE (op0));
5653 tcode = LT_EXPR;
5654 }
5655 }
5656 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
5657 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
5658
5659
5660 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
5661 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
5662
5663 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
5664 if (icode == CODE_FOR_nothing)
5665 {
5666 if (tcode == EQ_EXPR || tcode == NE_EXPR)
5667 icode = get_vcond_eq_icode (mode, cmp_op_mode);
5668 if (icode == CODE_FOR_nothing)
5669 return 0;
5670 }
5671
5672 comparison = vector_compare_rtx (VOIDmode, tcode, op0a, op0b, unsignedp,
5673 icode, 4);
5674 rtx_op1 = expand_normal (op1);
5675 rtx_op2 = expand_normal (op2);
5676
5677 create_output_operand (&ops[0], target, mode);
5678 create_input_operand (&ops[1], rtx_op1, mode);
5679 create_input_operand (&ops[2], rtx_op2, mode);
5680 create_fixed_operand (&ops[3], comparison);
5681 create_fixed_operand (&ops[4], XEXP (comparison, 0));
5682 create_fixed_operand (&ops[5], XEXP (comparison, 1));
5683 expand_insn (icode, 6, ops);
5684 return ops[0].value;
5685 }
5686
5687 /* Generate insns for a vector comparison into a mask. */
5688
5689 rtx
5690 expand_vec_cmp_expr (tree type, tree exp, rtx target)
5691 {
5692 struct expand_operand ops[4];
5693 enum insn_code icode;
5694 rtx comparison;
5695 machine_mode mask_mode = TYPE_MODE (type);
5696 machine_mode vmode;
5697 bool unsignedp;
5698 tree op0a, op0b;
5699 enum tree_code tcode;
5700
5701 op0a = TREE_OPERAND (exp, 0);
5702 op0b = TREE_OPERAND (exp, 1);
5703 tcode = TREE_CODE (exp);
5704
5705 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
5706 vmode = TYPE_MODE (TREE_TYPE (op0a));
5707
5708 icode = get_vec_cmp_icode (vmode, mask_mode, unsignedp);
5709 if (icode == CODE_FOR_nothing)
5710 {
5711 if (tcode == EQ_EXPR || tcode == NE_EXPR)
5712 icode = get_vec_cmp_eq_icode (vmode, mask_mode);
5713 if (icode == CODE_FOR_nothing)
5714 return 0;
5715 }
5716
5717 comparison = vector_compare_rtx (mask_mode, tcode, op0a, op0b,
5718 unsignedp, icode, 2);
5719 create_output_operand (&ops[0], target, mask_mode);
5720 create_fixed_operand (&ops[1], comparison);
5721 create_fixed_operand (&ops[2], XEXP (comparison, 0));
5722 create_fixed_operand (&ops[3], XEXP (comparison, 1));
5723 expand_insn (icode, 4, ops);
5724 return ops[0].value;
5725 }
5726
5727 /* Expand a highpart multiply. */
5728
5729 rtx
5730 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
5731 rtx target, bool uns_p)
5732 {
5733 struct expand_operand eops[3];
5734 enum insn_code icode;
5735 int method, i, nunits;
5736 machine_mode wmode;
5737 rtx m1, m2, perm;
5738 optab tab1, tab2;
5739 rtvec v;
5740
5741 method = can_mult_highpart_p (mode, uns_p);
5742 switch (method)
5743 {
5744 case 0:
5745 return NULL_RTX;
5746 case 1:
5747 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
5748 return expand_binop (mode, tab1, op0, op1, target, uns_p,
5749 OPTAB_LIB_WIDEN);
5750 case 2:
5751 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
5752 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
5753 break;
5754 case 3:
5755 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
5756 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
5757 if (BYTES_BIG_ENDIAN)
5758 std::swap (tab1, tab2);
5759 break;
5760 default:
5761 gcc_unreachable ();
5762 }
5763
5764 icode = optab_handler (tab1, mode);
5765 nunits = GET_MODE_NUNITS (mode);
5766 wmode = insn_data[icode].operand[0].mode;
5767 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
5768 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
5769
5770 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
5771 create_input_operand (&eops[1], op0, mode);
5772 create_input_operand (&eops[2], op1, mode);
5773 expand_insn (icode, 3, eops);
5774 m1 = gen_lowpart (mode, eops[0].value);
5775
5776 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
5777 create_input_operand (&eops[1], op0, mode);
5778 create_input_operand (&eops[2], op1, mode);
5779 expand_insn (optab_handler (tab2, mode), 3, eops);
5780 m2 = gen_lowpart (mode, eops[0].value);
5781
5782 v = rtvec_alloc (nunits);
5783 if (method == 2)
5784 {
5785 for (i = 0; i < nunits; ++i)
5786 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
5787 + ((i & 1) ? nunits : 0));
5788 }
5789 else
5790 {
5791 for (i = 0; i < nunits; ++i)
5792 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
5793 }
5794 perm = gen_rtx_CONST_VECTOR (mode, v);
5795
5796 return expand_vec_perm (mode, m1, m2, perm, target);
5797 }
5798 \f
5799 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
5800 pattern. */
5801
5802 static void
5803 find_cc_set (rtx x, const_rtx pat, void *data)
5804 {
5805 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
5806 && GET_CODE (pat) == SET)
5807 {
5808 rtx *p_cc_reg = (rtx *) data;
5809 gcc_assert (!*p_cc_reg);
5810 *p_cc_reg = x;
5811 }
5812 }
5813
5814 /* This is a helper function for the other atomic operations. This function
5815 emits a loop that contains SEQ that iterates until a compare-and-swap
5816 operation at the end succeeds. MEM is the memory to be modified. SEQ is
5817 a set of instructions that takes a value from OLD_REG as an input and
5818 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
5819 set to the current contents of MEM. After SEQ, a compare-and-swap will
5820 attempt to update MEM with NEW_REG. The function returns true when the
5821 loop was generated successfully. */
5822
5823 static bool
5824 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
5825 {
5826 machine_mode mode = GET_MODE (mem);
5827 rtx_code_label *label;
5828 rtx cmp_reg, success, oldval;
5829
5830 /* The loop we want to generate looks like
5831
5832 cmp_reg = mem;
5833 label:
5834 old_reg = cmp_reg;
5835 seq;
5836 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
5837 if (success)
5838 goto label;
5839
5840 Note that we only do the plain load from memory once. Subsequent
5841 iterations use the value loaded by the compare-and-swap pattern. */
5842
5843 label = gen_label_rtx ();
5844 cmp_reg = gen_reg_rtx (mode);
5845
5846 emit_move_insn (cmp_reg, mem);
5847 emit_label (label);
5848 emit_move_insn (old_reg, cmp_reg);
5849 if (seq)
5850 emit_insn (seq);
5851
5852 success = NULL_RTX;
5853 oldval = cmp_reg;
5854 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
5855 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
5856 MEMMODEL_RELAXED))
5857 return false;
5858
5859 if (oldval != cmp_reg)
5860 emit_move_insn (cmp_reg, oldval);
5861
5862 /* Mark this jump predicted not taken. */
5863 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
5864 GET_MODE (success), 1, label,
5865 profile_probability::guessed_never ());
5866 return true;
5867 }
5868
5869
5870 /* This function tries to emit an atomic_exchange intruction. VAL is written
5871 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
5872 using TARGET if possible. */
5873
5874 static rtx
5875 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
5876 {
5877 machine_mode mode = GET_MODE (mem);
5878 enum insn_code icode;
5879
5880 /* If the target supports the exchange directly, great. */
5881 icode = direct_optab_handler (atomic_exchange_optab, mode);
5882 if (icode != CODE_FOR_nothing)
5883 {
5884 struct expand_operand ops[4];
5885
5886 create_output_operand (&ops[0], target, mode);
5887 create_fixed_operand (&ops[1], mem);
5888 create_input_operand (&ops[2], val, mode);
5889 create_integer_operand (&ops[3], model);
5890 if (maybe_expand_insn (icode, 4, ops))
5891 return ops[0].value;
5892 }
5893
5894 return NULL_RTX;
5895 }
5896
5897 /* This function tries to implement an atomic exchange operation using
5898 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
5899 The previous contents of *MEM are returned, using TARGET if possible.
5900 Since this instructionn is an acquire barrier only, stronger memory
5901 models may require additional barriers to be emitted. */
5902
5903 static rtx
5904 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
5905 enum memmodel model)
5906 {
5907 machine_mode mode = GET_MODE (mem);
5908 enum insn_code icode;
5909 rtx_insn *last_insn = get_last_insn ();
5910
5911 icode = optab_handler (sync_lock_test_and_set_optab, mode);
5912
5913 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
5914 exists, and the memory model is stronger than acquire, add a release
5915 barrier before the instruction. */
5916
5917 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
5918 expand_mem_thread_fence (model);
5919
5920 if (icode != CODE_FOR_nothing)
5921 {
5922 struct expand_operand ops[3];
5923 create_output_operand (&ops[0], target, mode);
5924 create_fixed_operand (&ops[1], mem);
5925 create_input_operand (&ops[2], val, mode);
5926 if (maybe_expand_insn (icode, 3, ops))
5927 return ops[0].value;
5928 }
5929
5930 /* If an external test-and-set libcall is provided, use that instead of
5931 any external compare-and-swap that we might get from the compare-and-
5932 swap-loop expansion later. */
5933 if (!can_compare_and_swap_p (mode, false))
5934 {
5935 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
5936 if (libfunc != NULL)
5937 {
5938 rtx addr;
5939
5940 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
5941 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
5942 mode, addr, ptr_mode,
5943 val, mode);
5944 }
5945 }
5946
5947 /* If the test_and_set can't be emitted, eliminate any barrier that might
5948 have been emitted. */
5949 delete_insns_since (last_insn);
5950 return NULL_RTX;
5951 }
5952
5953 /* This function tries to implement an atomic exchange operation using a
5954 compare_and_swap loop. VAL is written to *MEM. The previous contents of
5955 *MEM are returned, using TARGET if possible. No memory model is required
5956 since a compare_and_swap loop is seq-cst. */
5957
5958 static rtx
5959 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
5960 {
5961 machine_mode mode = GET_MODE (mem);
5962
5963 if (can_compare_and_swap_p (mode, true))
5964 {
5965 if (!target || !register_operand (target, mode))
5966 target = gen_reg_rtx (mode);
5967 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
5968 return target;
5969 }
5970
5971 return NULL_RTX;
5972 }
5973
5974 /* This function tries to implement an atomic test-and-set operation
5975 using the atomic_test_and_set instruction pattern. A boolean value
5976 is returned from the operation, using TARGET if possible. */
5977
5978 static rtx
5979 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
5980 {
5981 machine_mode pat_bool_mode;
5982 struct expand_operand ops[3];
5983
5984 if (!targetm.have_atomic_test_and_set ())
5985 return NULL_RTX;
5986
5987 /* While we always get QImode from __atomic_test_and_set, we get
5988 other memory modes from __sync_lock_test_and_set. Note that we
5989 use no endian adjustment here. This matches the 4.6 behavior
5990 in the Sparc backend. */
5991 enum insn_code icode = targetm.code_for_atomic_test_and_set;
5992 gcc_checking_assert (insn_data[icode].operand[1].mode == QImode);
5993 if (GET_MODE (mem) != QImode)
5994 mem = adjust_address_nv (mem, QImode, 0);
5995
5996 pat_bool_mode = insn_data[icode].operand[0].mode;
5997 create_output_operand (&ops[0], target, pat_bool_mode);
5998 create_fixed_operand (&ops[1], mem);
5999 create_integer_operand (&ops[2], model);
6000
6001 if (maybe_expand_insn (icode, 3, ops))
6002 return ops[0].value;
6003 return NULL_RTX;
6004 }
6005
6006 /* This function expands the legacy _sync_lock test_and_set operation which is
6007 generally an atomic exchange. Some limited targets only allow the
6008 constant 1 to be stored. This is an ACQUIRE operation.
6009
6010 TARGET is an optional place to stick the return value.
6011 MEM is where VAL is stored. */
6012
6013 rtx
6014 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
6015 {
6016 rtx ret;
6017
6018 /* Try an atomic_exchange first. */
6019 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
6020 if (ret)
6021 return ret;
6022
6023 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
6024 MEMMODEL_SYNC_ACQUIRE);
6025 if (ret)
6026 return ret;
6027
6028 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6029 if (ret)
6030 return ret;
6031
6032 /* If there are no other options, try atomic_test_and_set if the value
6033 being stored is 1. */
6034 if (val == const1_rtx)
6035 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
6036
6037 return ret;
6038 }
6039
6040 /* This function expands the atomic test_and_set operation:
6041 atomically store a boolean TRUE into MEM and return the previous value.
6042
6043 MEMMODEL is the memory model variant to use.
6044 TARGET is an optional place to stick the return value. */
6045
6046 rtx
6047 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
6048 {
6049 machine_mode mode = GET_MODE (mem);
6050 rtx ret, trueval, subtarget;
6051
6052 ret = maybe_emit_atomic_test_and_set (target, mem, model);
6053 if (ret)
6054 return ret;
6055
6056 /* Be binary compatible with non-default settings of trueval, and different
6057 cpu revisions. E.g. one revision may have atomic-test-and-set, but
6058 another only has atomic-exchange. */
6059 if (targetm.atomic_test_and_set_trueval == 1)
6060 {
6061 trueval = const1_rtx;
6062 subtarget = target ? target : gen_reg_rtx (mode);
6063 }
6064 else
6065 {
6066 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
6067 subtarget = gen_reg_rtx (mode);
6068 }
6069
6070 /* Try the atomic-exchange optab... */
6071 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
6072
6073 /* ... then an atomic-compare-and-swap loop ... */
6074 if (!ret)
6075 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
6076
6077 /* ... before trying the vaguely defined legacy lock_test_and_set. */
6078 if (!ret)
6079 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
6080
6081 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
6082 things with the value 1. Thus we try again without trueval. */
6083 if (!ret && targetm.atomic_test_and_set_trueval != 1)
6084 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
6085
6086 /* Failing all else, assume a single threaded environment and simply
6087 perform the operation. */
6088 if (!ret)
6089 {
6090 /* If the result is ignored skip the move to target. */
6091 if (subtarget != const0_rtx)
6092 emit_move_insn (subtarget, mem);
6093
6094 emit_move_insn (mem, trueval);
6095 ret = subtarget;
6096 }
6097
6098 /* Recall that have to return a boolean value; rectify if trueval
6099 is not exactly one. */
6100 if (targetm.atomic_test_and_set_trueval != 1)
6101 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
6102
6103 return ret;
6104 }
6105
6106 /* This function expands the atomic exchange operation:
6107 atomically store VAL in MEM and return the previous value in MEM.
6108
6109 MEMMODEL is the memory model variant to use.
6110 TARGET is an optional place to stick the return value. */
6111
6112 rtx
6113 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6114 {
6115 machine_mode mode = GET_MODE (mem);
6116 rtx ret;
6117
6118 /* If loads are not atomic for the required size and we are not called to
6119 provide a __sync builtin, do not do anything so that we stay consistent
6120 with atomic loads of the same size. */
6121 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6122 return NULL_RTX;
6123
6124 ret = maybe_emit_atomic_exchange (target, mem, val, model);
6125
6126 /* Next try a compare-and-swap loop for the exchange. */
6127 if (!ret)
6128 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
6129
6130 return ret;
6131 }
6132
6133 /* This function expands the atomic compare exchange operation:
6134
6135 *PTARGET_BOOL is an optional place to store the boolean success/failure.
6136 *PTARGET_OVAL is an optional place to store the old value from memory.
6137 Both target parameters may be NULL or const0_rtx to indicate that we do
6138 not care about that return value. Both target parameters are updated on
6139 success to the actual location of the corresponding result.
6140
6141 MEMMODEL is the memory model variant to use.
6142
6143 The return value of the function is true for success. */
6144
6145 bool
6146 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
6147 rtx mem, rtx expected, rtx desired,
6148 bool is_weak, enum memmodel succ_model,
6149 enum memmodel fail_model)
6150 {
6151 machine_mode mode = GET_MODE (mem);
6152 struct expand_operand ops[8];
6153 enum insn_code icode;
6154 rtx target_oval, target_bool = NULL_RTX;
6155 rtx libfunc;
6156
6157 /* If loads are not atomic for the required size and we are not called to
6158 provide a __sync builtin, do not do anything so that we stay consistent
6159 with atomic loads of the same size. */
6160 if (!can_atomic_load_p (mode) && !is_mm_sync (succ_model))
6161 return false;
6162
6163 /* Load expected into a register for the compare and swap. */
6164 if (MEM_P (expected))
6165 expected = copy_to_reg (expected);
6166
6167 /* Make sure we always have some place to put the return oldval.
6168 Further, make sure that place is distinct from the input expected,
6169 just in case we need that path down below. */
6170 if (ptarget_oval && *ptarget_oval == const0_rtx)
6171 ptarget_oval = NULL;
6172
6173 if (ptarget_oval == NULL
6174 || (target_oval = *ptarget_oval) == NULL
6175 || reg_overlap_mentioned_p (expected, target_oval))
6176 target_oval = gen_reg_rtx (mode);
6177
6178 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
6179 if (icode != CODE_FOR_nothing)
6180 {
6181 machine_mode bool_mode = insn_data[icode].operand[0].mode;
6182
6183 if (ptarget_bool && *ptarget_bool == const0_rtx)
6184 ptarget_bool = NULL;
6185
6186 /* Make sure we always have a place for the bool operand. */
6187 if (ptarget_bool == NULL
6188 || (target_bool = *ptarget_bool) == NULL
6189 || GET_MODE (target_bool) != bool_mode)
6190 target_bool = gen_reg_rtx (bool_mode);
6191
6192 /* Emit the compare_and_swap. */
6193 create_output_operand (&ops[0], target_bool, bool_mode);
6194 create_output_operand (&ops[1], target_oval, mode);
6195 create_fixed_operand (&ops[2], mem);
6196 create_input_operand (&ops[3], expected, mode);
6197 create_input_operand (&ops[4], desired, mode);
6198 create_integer_operand (&ops[5], is_weak);
6199 create_integer_operand (&ops[6], succ_model);
6200 create_integer_operand (&ops[7], fail_model);
6201 if (maybe_expand_insn (icode, 8, ops))
6202 {
6203 /* Return success/failure. */
6204 target_bool = ops[0].value;
6205 target_oval = ops[1].value;
6206 goto success;
6207 }
6208 }
6209
6210 /* Otherwise fall back to the original __sync_val_compare_and_swap
6211 which is always seq-cst. */
6212 icode = optab_handler (sync_compare_and_swap_optab, mode);
6213 if (icode != CODE_FOR_nothing)
6214 {
6215 rtx cc_reg;
6216
6217 create_output_operand (&ops[0], target_oval, mode);
6218 create_fixed_operand (&ops[1], mem);
6219 create_input_operand (&ops[2], expected, mode);
6220 create_input_operand (&ops[3], desired, mode);
6221 if (!maybe_expand_insn (icode, 4, ops))
6222 return false;
6223
6224 target_oval = ops[0].value;
6225
6226 /* If the caller isn't interested in the boolean return value,
6227 skip the computation of it. */
6228 if (ptarget_bool == NULL)
6229 goto success;
6230
6231 /* Otherwise, work out if the compare-and-swap succeeded. */
6232 cc_reg = NULL_RTX;
6233 if (have_insn_for (COMPARE, CCmode))
6234 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6235 if (cc_reg)
6236 {
6237 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
6238 const0_rtx, VOIDmode, 0, 1);
6239 goto success;
6240 }
6241 goto success_bool_from_val;
6242 }
6243
6244 /* Also check for library support for __sync_val_compare_and_swap. */
6245 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
6246 if (libfunc != NULL)
6247 {
6248 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6249 rtx target = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
6250 mode, addr, ptr_mode,
6251 expected, mode, desired, mode);
6252 emit_move_insn (target_oval, target);
6253
6254 /* Compute the boolean return value only if requested. */
6255 if (ptarget_bool)
6256 goto success_bool_from_val;
6257 else
6258 goto success;
6259 }
6260
6261 /* Failure. */
6262 return false;
6263
6264 success_bool_from_val:
6265 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
6266 expected, VOIDmode, 1, 1);
6267 success:
6268 /* Make sure that the oval output winds up where the caller asked. */
6269 if (ptarget_oval)
6270 *ptarget_oval = target_oval;
6271 if (ptarget_bool)
6272 *ptarget_bool = target_bool;
6273 return true;
6274 }
6275
6276 /* Generate asm volatile("" : : : "memory") as the memory blockage. */
6277
6278 static void
6279 expand_asm_memory_blockage (void)
6280 {
6281 rtx asm_op, clob;
6282
6283 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
6284 rtvec_alloc (0), rtvec_alloc (0),
6285 rtvec_alloc (0), UNKNOWN_LOCATION);
6286 MEM_VOLATILE_P (asm_op) = 1;
6287
6288 clob = gen_rtx_SCRATCH (VOIDmode);
6289 clob = gen_rtx_MEM (BLKmode, clob);
6290 clob = gen_rtx_CLOBBER (VOIDmode, clob);
6291
6292 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
6293 }
6294
6295 /* Do not propagate memory accesses across this point. */
6296
6297 static void
6298 expand_memory_blockage (void)
6299 {
6300 if (targetm.have_memory_blockage ())
6301 emit_insn (targetm.gen_memory_blockage ());
6302 else
6303 expand_asm_memory_blockage ();
6304 }
6305
6306 /* This routine will either emit the mem_thread_fence pattern or issue a
6307 sync_synchronize to generate a fence for memory model MEMMODEL. */
6308
6309 void
6310 expand_mem_thread_fence (enum memmodel model)
6311 {
6312 if (is_mm_relaxed (model))
6313 return;
6314 if (targetm.have_mem_thread_fence ())
6315 {
6316 emit_insn (targetm.gen_mem_thread_fence (GEN_INT (model)));
6317 expand_memory_blockage ();
6318 }
6319 else if (targetm.have_memory_barrier ())
6320 emit_insn (targetm.gen_memory_barrier ());
6321 else if (synchronize_libfunc != NULL_RTX)
6322 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode);
6323 else
6324 expand_memory_blockage ();
6325 }
6326
6327 /* Emit a signal fence with given memory model. */
6328
6329 void
6330 expand_mem_signal_fence (enum memmodel model)
6331 {
6332 /* No machine barrier is required to implement a signal fence, but
6333 a compiler memory barrier must be issued, except for relaxed MM. */
6334 if (!is_mm_relaxed (model))
6335 expand_memory_blockage ();
6336 }
6337
6338 /* This function expands the atomic load operation:
6339 return the atomically loaded value in MEM.
6340
6341 MEMMODEL is the memory model variant to use.
6342 TARGET is an option place to stick the return value. */
6343
6344 rtx
6345 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
6346 {
6347 machine_mode mode = GET_MODE (mem);
6348 enum insn_code icode;
6349
6350 /* If the target supports the load directly, great. */
6351 icode = direct_optab_handler (atomic_load_optab, mode);
6352 if (icode != CODE_FOR_nothing)
6353 {
6354 struct expand_operand ops[3];
6355 rtx_insn *last = get_last_insn ();
6356 if (is_mm_seq_cst (model))
6357 expand_memory_blockage ();
6358
6359 create_output_operand (&ops[0], target, mode);
6360 create_fixed_operand (&ops[1], mem);
6361 create_integer_operand (&ops[2], model);
6362 if (maybe_expand_insn (icode, 3, ops))
6363 {
6364 if (!is_mm_relaxed (model))
6365 expand_memory_blockage ();
6366 return ops[0].value;
6367 }
6368 delete_insns_since (last);
6369 }
6370
6371 /* If the size of the object is greater than word size on this target,
6372 then we assume that a load will not be atomic. We could try to
6373 emulate a load with a compare-and-swap operation, but the store that
6374 doing this could result in would be incorrect if this is a volatile
6375 atomic load or targetting read-only-mapped memory. */
6376 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
6377 /* If there is no atomic load, leave the library call. */
6378 return NULL_RTX;
6379
6380 /* Otherwise assume loads are atomic, and emit the proper barriers. */
6381 if (!target || target == const0_rtx)
6382 target = gen_reg_rtx (mode);
6383
6384 /* For SEQ_CST, emit a barrier before the load. */
6385 if (is_mm_seq_cst (model))
6386 expand_mem_thread_fence (model);
6387
6388 emit_move_insn (target, mem);
6389
6390 /* Emit the appropriate barrier after the load. */
6391 expand_mem_thread_fence (model);
6392
6393 return target;
6394 }
6395
6396 /* This function expands the atomic store operation:
6397 Atomically store VAL in MEM.
6398 MEMMODEL is the memory model variant to use.
6399 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
6400 function returns const0_rtx if a pattern was emitted. */
6401
6402 rtx
6403 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
6404 {
6405 machine_mode mode = GET_MODE (mem);
6406 enum insn_code icode;
6407 struct expand_operand ops[3];
6408
6409 /* If the target supports the store directly, great. */
6410 icode = direct_optab_handler (atomic_store_optab, mode);
6411 if (icode != CODE_FOR_nothing)
6412 {
6413 rtx_insn *last = get_last_insn ();
6414 if (!is_mm_relaxed (model))
6415 expand_memory_blockage ();
6416 create_fixed_operand (&ops[0], mem);
6417 create_input_operand (&ops[1], val, mode);
6418 create_integer_operand (&ops[2], model);
6419 if (maybe_expand_insn (icode, 3, ops))
6420 {
6421 if (is_mm_seq_cst (model))
6422 expand_memory_blockage ();
6423 return const0_rtx;
6424 }
6425 delete_insns_since (last);
6426 }
6427
6428 /* If using __sync_lock_release is a viable alternative, try it.
6429 Note that this will not be set to true if we are expanding a generic
6430 __atomic_store_n. */
6431 if (use_release)
6432 {
6433 icode = direct_optab_handler (sync_lock_release_optab, mode);
6434 if (icode != CODE_FOR_nothing)
6435 {
6436 create_fixed_operand (&ops[0], mem);
6437 create_input_operand (&ops[1], const0_rtx, mode);
6438 if (maybe_expand_insn (icode, 2, ops))
6439 {
6440 /* lock_release is only a release barrier. */
6441 if (is_mm_seq_cst (model))
6442 expand_mem_thread_fence (model);
6443 return const0_rtx;
6444 }
6445 }
6446 }
6447
6448 /* If the size of the object is greater than word size on this target,
6449 a default store will not be atomic. */
6450 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
6451 {
6452 /* If loads are atomic or we are called to provide a __sync builtin,
6453 we can try a atomic_exchange and throw away the result. Otherwise,
6454 don't do anything so that we do not create an inconsistency between
6455 loads and stores. */
6456 if (can_atomic_load_p (mode) || is_mm_sync (model))
6457 {
6458 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
6459 if (!target)
6460 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem,
6461 val);
6462 if (target)
6463 return const0_rtx;
6464 }
6465 return NULL_RTX;
6466 }
6467
6468 /* Otherwise assume stores are atomic, and emit the proper barriers. */
6469 expand_mem_thread_fence (model);
6470
6471 emit_move_insn (mem, val);
6472
6473 /* For SEQ_CST, also emit a barrier after the store. */
6474 if (is_mm_seq_cst (model))
6475 expand_mem_thread_fence (model);
6476
6477 return const0_rtx;
6478 }
6479
6480
6481 /* Structure containing the pointers and values required to process the
6482 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
6483
6484 struct atomic_op_functions
6485 {
6486 direct_optab mem_fetch_before;
6487 direct_optab mem_fetch_after;
6488 direct_optab mem_no_result;
6489 optab fetch_before;
6490 optab fetch_after;
6491 direct_optab no_result;
6492 enum rtx_code reverse_code;
6493 };
6494
6495
6496 /* Fill in structure pointed to by OP with the various optab entries for an
6497 operation of type CODE. */
6498
6499 static void
6500 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
6501 {
6502 gcc_assert (op!= NULL);
6503
6504 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
6505 in the source code during compilation, and the optab entries are not
6506 computable until runtime. Fill in the values at runtime. */
6507 switch (code)
6508 {
6509 case PLUS:
6510 op->mem_fetch_before = atomic_fetch_add_optab;
6511 op->mem_fetch_after = atomic_add_fetch_optab;
6512 op->mem_no_result = atomic_add_optab;
6513 op->fetch_before = sync_old_add_optab;
6514 op->fetch_after = sync_new_add_optab;
6515 op->no_result = sync_add_optab;
6516 op->reverse_code = MINUS;
6517 break;
6518 case MINUS:
6519 op->mem_fetch_before = atomic_fetch_sub_optab;
6520 op->mem_fetch_after = atomic_sub_fetch_optab;
6521 op->mem_no_result = atomic_sub_optab;
6522 op->fetch_before = sync_old_sub_optab;
6523 op->fetch_after = sync_new_sub_optab;
6524 op->no_result = sync_sub_optab;
6525 op->reverse_code = PLUS;
6526 break;
6527 case XOR:
6528 op->mem_fetch_before = atomic_fetch_xor_optab;
6529 op->mem_fetch_after = atomic_xor_fetch_optab;
6530 op->mem_no_result = atomic_xor_optab;
6531 op->fetch_before = sync_old_xor_optab;
6532 op->fetch_after = sync_new_xor_optab;
6533 op->no_result = sync_xor_optab;
6534 op->reverse_code = XOR;
6535 break;
6536 case AND:
6537 op->mem_fetch_before = atomic_fetch_and_optab;
6538 op->mem_fetch_after = atomic_and_fetch_optab;
6539 op->mem_no_result = atomic_and_optab;
6540 op->fetch_before = sync_old_and_optab;
6541 op->fetch_after = sync_new_and_optab;
6542 op->no_result = sync_and_optab;
6543 op->reverse_code = UNKNOWN;
6544 break;
6545 case IOR:
6546 op->mem_fetch_before = atomic_fetch_or_optab;
6547 op->mem_fetch_after = atomic_or_fetch_optab;
6548 op->mem_no_result = atomic_or_optab;
6549 op->fetch_before = sync_old_ior_optab;
6550 op->fetch_after = sync_new_ior_optab;
6551 op->no_result = sync_ior_optab;
6552 op->reverse_code = UNKNOWN;
6553 break;
6554 case NOT:
6555 op->mem_fetch_before = atomic_fetch_nand_optab;
6556 op->mem_fetch_after = atomic_nand_fetch_optab;
6557 op->mem_no_result = atomic_nand_optab;
6558 op->fetch_before = sync_old_nand_optab;
6559 op->fetch_after = sync_new_nand_optab;
6560 op->no_result = sync_nand_optab;
6561 op->reverse_code = UNKNOWN;
6562 break;
6563 default:
6564 gcc_unreachable ();
6565 }
6566 }
6567
6568 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
6569 using memory order MODEL. If AFTER is true the operation needs to return
6570 the value of *MEM after the operation, otherwise the previous value.
6571 TARGET is an optional place to place the result. The result is unused if
6572 it is const0_rtx.
6573 Return the result if there is a better sequence, otherwise NULL_RTX. */
6574
6575 static rtx
6576 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
6577 enum memmodel model, bool after)
6578 {
6579 /* If the value is prefetched, or not used, it may be possible to replace
6580 the sequence with a native exchange operation. */
6581 if (!after || target == const0_rtx)
6582 {
6583 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
6584 if (code == AND && val == const0_rtx)
6585 {
6586 if (target == const0_rtx)
6587 target = gen_reg_rtx (GET_MODE (mem));
6588 return maybe_emit_atomic_exchange (target, mem, val, model);
6589 }
6590
6591 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
6592 if (code == IOR && val == constm1_rtx)
6593 {
6594 if (target == const0_rtx)
6595 target = gen_reg_rtx (GET_MODE (mem));
6596 return maybe_emit_atomic_exchange (target, mem, val, model);
6597 }
6598 }
6599
6600 return NULL_RTX;
6601 }
6602
6603 /* Try to emit an instruction for a specific operation varaition.
6604 OPTAB contains the OP functions.
6605 TARGET is an optional place to return the result. const0_rtx means unused.
6606 MEM is the memory location to operate on.
6607 VAL is the value to use in the operation.
6608 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
6609 MODEL is the memory model, if used.
6610 AFTER is true if the returned result is the value after the operation. */
6611
6612 static rtx
6613 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
6614 rtx val, bool use_memmodel, enum memmodel model, bool after)
6615 {
6616 machine_mode mode = GET_MODE (mem);
6617 struct expand_operand ops[4];
6618 enum insn_code icode;
6619 int op_counter = 0;
6620 int num_ops;
6621
6622 /* Check to see if there is a result returned. */
6623 if (target == const0_rtx)
6624 {
6625 if (use_memmodel)
6626 {
6627 icode = direct_optab_handler (optab->mem_no_result, mode);
6628 create_integer_operand (&ops[2], model);
6629 num_ops = 3;
6630 }
6631 else
6632 {
6633 icode = direct_optab_handler (optab->no_result, mode);
6634 num_ops = 2;
6635 }
6636 }
6637 /* Otherwise, we need to generate a result. */
6638 else
6639 {
6640 if (use_memmodel)
6641 {
6642 icode = direct_optab_handler (after ? optab->mem_fetch_after
6643 : optab->mem_fetch_before, mode);
6644 create_integer_operand (&ops[3], model);
6645 num_ops = 4;
6646 }
6647 else
6648 {
6649 icode = optab_handler (after ? optab->fetch_after
6650 : optab->fetch_before, mode);
6651 num_ops = 3;
6652 }
6653 create_output_operand (&ops[op_counter++], target, mode);
6654 }
6655 if (icode == CODE_FOR_nothing)
6656 return NULL_RTX;
6657
6658 create_fixed_operand (&ops[op_counter++], mem);
6659 /* VAL may have been promoted to a wider mode. Shrink it if so. */
6660 create_convert_operand_to (&ops[op_counter++], val, mode, true);
6661
6662 if (maybe_expand_insn (icode, num_ops, ops))
6663 return (target == const0_rtx ? const0_rtx : ops[0].value);
6664
6665 return NULL_RTX;
6666 }
6667
6668
6669 /* This function expands an atomic fetch_OP or OP_fetch operation:
6670 TARGET is an option place to stick the return value. const0_rtx indicates
6671 the result is unused.
6672 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6673 CODE is the operation being performed (OP)
6674 MEMMODEL is the memory model variant to use.
6675 AFTER is true to return the result of the operation (OP_fetch).
6676 AFTER is false to return the value before the operation (fetch_OP).
6677
6678 This function will *only* generate instructions if there is a direct
6679 optab. No compare and swap loops or libcalls will be generated. */
6680
6681 static rtx
6682 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
6683 enum rtx_code code, enum memmodel model,
6684 bool after)
6685 {
6686 machine_mode mode = GET_MODE (mem);
6687 struct atomic_op_functions optab;
6688 rtx result;
6689 bool unused_result = (target == const0_rtx);
6690
6691 get_atomic_op_for_code (&optab, code);
6692
6693 /* Check to see if there are any better instructions. */
6694 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
6695 if (result)
6696 return result;
6697
6698 /* Check for the case where the result isn't used and try those patterns. */
6699 if (unused_result)
6700 {
6701 /* Try the memory model variant first. */
6702 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
6703 if (result)
6704 return result;
6705
6706 /* Next try the old style withuot a memory model. */
6707 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
6708 if (result)
6709 return result;
6710
6711 /* There is no no-result pattern, so try patterns with a result. */
6712 target = NULL_RTX;
6713 }
6714
6715 /* Try the __atomic version. */
6716 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
6717 if (result)
6718 return result;
6719
6720 /* Try the older __sync version. */
6721 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
6722 if (result)
6723 return result;
6724
6725 /* If the fetch value can be calculated from the other variation of fetch,
6726 try that operation. */
6727 if (after || unused_result || optab.reverse_code != UNKNOWN)
6728 {
6729 /* Try the __atomic version, then the older __sync version. */
6730 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
6731 if (!result)
6732 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
6733
6734 if (result)
6735 {
6736 /* If the result isn't used, no need to do compensation code. */
6737 if (unused_result)
6738 return result;
6739
6740 /* Issue compensation code. Fetch_after == fetch_before OP val.
6741 Fetch_before == after REVERSE_OP val. */
6742 if (!after)
6743 code = optab.reverse_code;
6744 if (code == NOT)
6745 {
6746 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
6747 true, OPTAB_LIB_WIDEN);
6748 result = expand_simple_unop (mode, NOT, result, target, true);
6749 }
6750 else
6751 result = expand_simple_binop (mode, code, result, val, target,
6752 true, OPTAB_LIB_WIDEN);
6753 return result;
6754 }
6755 }
6756
6757 /* No direct opcode can be generated. */
6758 return NULL_RTX;
6759 }
6760
6761
6762
6763 /* This function expands an atomic fetch_OP or OP_fetch operation:
6764 TARGET is an option place to stick the return value. const0_rtx indicates
6765 the result is unused.
6766 atomically fetch MEM, perform the operation with VAL and return it to MEM.
6767 CODE is the operation being performed (OP)
6768 MEMMODEL is the memory model variant to use.
6769 AFTER is true to return the result of the operation (OP_fetch).
6770 AFTER is false to return the value before the operation (fetch_OP). */
6771 rtx
6772 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
6773 enum memmodel model, bool after)
6774 {
6775 machine_mode mode = GET_MODE (mem);
6776 rtx result;
6777 bool unused_result = (target == const0_rtx);
6778
6779 /* If loads are not atomic for the required size and we are not called to
6780 provide a __sync builtin, do not do anything so that we stay consistent
6781 with atomic loads of the same size. */
6782 if (!can_atomic_load_p (mode) && !is_mm_sync (model))
6783 return NULL_RTX;
6784
6785 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
6786 after);
6787
6788 if (result)
6789 return result;
6790
6791 /* Add/sub can be implemented by doing the reverse operation with -(val). */
6792 if (code == PLUS || code == MINUS)
6793 {
6794 rtx tmp;
6795 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
6796
6797 start_sequence ();
6798 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
6799 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
6800 model, after);
6801 if (result)
6802 {
6803 /* PLUS worked so emit the insns and return. */
6804 tmp = get_insns ();
6805 end_sequence ();
6806 emit_insn (tmp);
6807 return result;
6808 }
6809
6810 /* PLUS did not work, so throw away the negation code and continue. */
6811 end_sequence ();
6812 }
6813
6814 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
6815 if (!can_compare_and_swap_p (mode, false))
6816 {
6817 rtx libfunc;
6818 bool fixup = false;
6819 enum rtx_code orig_code = code;
6820 struct atomic_op_functions optab;
6821
6822 get_atomic_op_for_code (&optab, code);
6823 libfunc = optab_libfunc (after ? optab.fetch_after
6824 : optab.fetch_before, mode);
6825 if (libfunc == NULL
6826 && (after || unused_result || optab.reverse_code != UNKNOWN))
6827 {
6828 fixup = true;
6829 if (!after)
6830 code = optab.reverse_code;
6831 libfunc = optab_libfunc (after ? optab.fetch_before
6832 : optab.fetch_after, mode);
6833 }
6834 if (libfunc != NULL)
6835 {
6836 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
6837 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
6838 addr, ptr_mode, val, mode);
6839
6840 if (!unused_result && fixup)
6841 result = expand_simple_binop (mode, code, result, val, target,
6842 true, OPTAB_LIB_WIDEN);
6843 return result;
6844 }
6845
6846 /* We need the original code for any further attempts. */
6847 code = orig_code;
6848 }
6849
6850 /* If nothing else has succeeded, default to a compare and swap loop. */
6851 if (can_compare_and_swap_p (mode, true))
6852 {
6853 rtx_insn *insn;
6854 rtx t0 = gen_reg_rtx (mode), t1;
6855
6856 start_sequence ();
6857
6858 /* If the result is used, get a register for it. */
6859 if (!unused_result)
6860 {
6861 if (!target || !register_operand (target, mode))
6862 target = gen_reg_rtx (mode);
6863 /* If fetch_before, copy the value now. */
6864 if (!after)
6865 emit_move_insn (target, t0);
6866 }
6867 else
6868 target = const0_rtx;
6869
6870 t1 = t0;
6871 if (code == NOT)
6872 {
6873 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
6874 true, OPTAB_LIB_WIDEN);
6875 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
6876 }
6877 else
6878 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
6879 OPTAB_LIB_WIDEN);
6880
6881 /* For after, copy the value now. */
6882 if (!unused_result && after)
6883 emit_move_insn (target, t1);
6884 insn = get_insns ();
6885 end_sequence ();
6886
6887 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
6888 return target;
6889 }
6890
6891 return NULL_RTX;
6892 }
6893 \f
6894 /* Return true if OPERAND is suitable for operand number OPNO of
6895 instruction ICODE. */
6896
6897 bool
6898 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
6899 {
6900 return (!insn_data[(int) icode].operand[opno].predicate
6901 || (insn_data[(int) icode].operand[opno].predicate
6902 (operand, insn_data[(int) icode].operand[opno].mode)));
6903 }
6904 \f
6905 /* TARGET is a target of a multiword operation that we are going to
6906 implement as a series of word-mode operations. Return true if
6907 TARGET is suitable for this purpose. */
6908
6909 bool
6910 valid_multiword_target_p (rtx target)
6911 {
6912 machine_mode mode;
6913 int i;
6914
6915 mode = GET_MODE (target);
6916 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
6917 if (!validate_subreg (word_mode, mode, target, i))
6918 return false;
6919 return true;
6920 }
6921
6922 /* Like maybe_legitimize_operand, but do not change the code of the
6923 current rtx value. */
6924
6925 static bool
6926 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
6927 struct expand_operand *op)
6928 {
6929 /* See if the operand matches in its current form. */
6930 if (insn_operand_matches (icode, opno, op->value))
6931 return true;
6932
6933 /* If the operand is a memory whose address has no side effects,
6934 try forcing the address into a non-virtual pseudo register.
6935 The check for side effects is important because copy_to_mode_reg
6936 cannot handle things like auto-modified addresses. */
6937 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
6938 {
6939 rtx addr, mem;
6940
6941 mem = op->value;
6942 addr = XEXP (mem, 0);
6943 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
6944 && !side_effects_p (addr))
6945 {
6946 rtx_insn *last;
6947 machine_mode mode;
6948
6949 last = get_last_insn ();
6950 mode = get_address_mode (mem);
6951 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
6952 if (insn_operand_matches (icode, opno, mem))
6953 {
6954 op->value = mem;
6955 return true;
6956 }
6957 delete_insns_since (last);
6958 }
6959 }
6960
6961 return false;
6962 }
6963
6964 /* Try to make OP match operand OPNO of instruction ICODE. Return true
6965 on success, storing the new operand value back in OP. */
6966
6967 static bool
6968 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
6969 struct expand_operand *op)
6970 {
6971 machine_mode mode, imode;
6972 bool old_volatile_ok, result;
6973
6974 mode = op->mode;
6975 switch (op->type)
6976 {
6977 case EXPAND_FIXED:
6978 old_volatile_ok = volatile_ok;
6979 volatile_ok = true;
6980 result = maybe_legitimize_operand_same_code (icode, opno, op);
6981 volatile_ok = old_volatile_ok;
6982 return result;
6983
6984 case EXPAND_OUTPUT:
6985 gcc_assert (mode != VOIDmode);
6986 if (op->value
6987 && op->value != const0_rtx
6988 && GET_MODE (op->value) == mode
6989 && maybe_legitimize_operand_same_code (icode, opno, op))
6990 return true;
6991
6992 op->value = gen_reg_rtx (mode);
6993 op->target = 0;
6994 break;
6995
6996 case EXPAND_INPUT:
6997 input:
6998 gcc_assert (mode != VOIDmode);
6999 gcc_assert (GET_MODE (op->value) == VOIDmode
7000 || GET_MODE (op->value) == mode);
7001 if (maybe_legitimize_operand_same_code (icode, opno, op))
7002 return true;
7003
7004 op->value = copy_to_mode_reg (mode, op->value);
7005 break;
7006
7007 case EXPAND_CONVERT_TO:
7008 gcc_assert (mode != VOIDmode);
7009 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
7010 goto input;
7011
7012 case EXPAND_CONVERT_FROM:
7013 if (GET_MODE (op->value) != VOIDmode)
7014 mode = GET_MODE (op->value);
7015 else
7016 /* The caller must tell us what mode this value has. */
7017 gcc_assert (mode != VOIDmode);
7018
7019 imode = insn_data[(int) icode].operand[opno].mode;
7020 if (imode != VOIDmode && imode != mode)
7021 {
7022 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
7023 mode = imode;
7024 }
7025 goto input;
7026
7027 case EXPAND_ADDRESS:
7028 op->value = convert_memory_address (as_a <scalar_int_mode> (mode),
7029 op->value);
7030 goto input;
7031
7032 case EXPAND_INTEGER:
7033 mode = insn_data[(int) icode].operand[opno].mode;
7034 if (mode != VOIDmode && const_int_operand (op->value, mode))
7035 goto input;
7036 break;
7037 }
7038 return insn_operand_matches (icode, opno, op->value);
7039 }
7040
7041 /* Make OP describe an input operand that should have the same value
7042 as VALUE, after any mode conversion that the target might request.
7043 TYPE is the type of VALUE. */
7044
7045 void
7046 create_convert_operand_from_type (struct expand_operand *op,
7047 rtx value, tree type)
7048 {
7049 create_convert_operand_from (op, value, TYPE_MODE (type),
7050 TYPE_UNSIGNED (type));
7051 }
7052
7053 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
7054 of instruction ICODE. Return true on success, leaving the new operand
7055 values in the OPS themselves. Emit no code on failure. */
7056
7057 bool
7058 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
7059 unsigned int nops, struct expand_operand *ops)
7060 {
7061 rtx_insn *last;
7062 unsigned int i;
7063
7064 last = get_last_insn ();
7065 for (i = 0; i < nops; i++)
7066 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
7067 {
7068 delete_insns_since (last);
7069 return false;
7070 }
7071 return true;
7072 }
7073
7074 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
7075 as its operands. Return the instruction pattern on success,
7076 and emit any necessary set-up code. Return null and emit no
7077 code on failure. */
7078
7079 rtx_insn *
7080 maybe_gen_insn (enum insn_code icode, unsigned int nops,
7081 struct expand_operand *ops)
7082 {
7083 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
7084 if (!maybe_legitimize_operands (icode, 0, nops, ops))
7085 return NULL;
7086
7087 switch (nops)
7088 {
7089 case 1:
7090 return GEN_FCN (icode) (ops[0].value);
7091 case 2:
7092 return GEN_FCN (icode) (ops[0].value, ops[1].value);
7093 case 3:
7094 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
7095 case 4:
7096 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7097 ops[3].value);
7098 case 5:
7099 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7100 ops[3].value, ops[4].value);
7101 case 6:
7102 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7103 ops[3].value, ops[4].value, ops[5].value);
7104 case 7:
7105 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7106 ops[3].value, ops[4].value, ops[5].value,
7107 ops[6].value);
7108 case 8:
7109 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7110 ops[3].value, ops[4].value, ops[5].value,
7111 ops[6].value, ops[7].value);
7112 case 9:
7113 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7114 ops[3].value, ops[4].value, ops[5].value,
7115 ops[6].value, ops[7].value, ops[8].value);
7116 }
7117 gcc_unreachable ();
7118 }
7119
7120 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
7121 as its operands. Return true on success and emit no code on failure. */
7122
7123 bool
7124 maybe_expand_insn (enum insn_code icode, unsigned int nops,
7125 struct expand_operand *ops)
7126 {
7127 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7128 if (pat)
7129 {
7130 emit_insn (pat);
7131 return true;
7132 }
7133 return false;
7134 }
7135
7136 /* Like maybe_expand_insn, but for jumps. */
7137
7138 bool
7139 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
7140 struct expand_operand *ops)
7141 {
7142 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
7143 if (pat)
7144 {
7145 emit_jump_insn (pat);
7146 return true;
7147 }
7148 return false;
7149 }
7150
7151 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
7152 as its operands. */
7153
7154 void
7155 expand_insn (enum insn_code icode, unsigned int nops,
7156 struct expand_operand *ops)
7157 {
7158 if (!maybe_expand_insn (icode, nops, ops))
7159 gcc_unreachable ();
7160 }
7161
7162 /* Like expand_insn, but for jumps. */
7163
7164 void
7165 expand_jump_insn (enum insn_code icode, unsigned int nops,
7166 struct expand_operand *ops)
7167 {
7168 if (!maybe_expand_jump_insn (icode, nops, ops))
7169 gcc_unreachable ();
7170 }