Debian Codegraph @

gcc/optabs.cc

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