PR81285: Fix uninitialised variable in emit_store_flag_int
[official-gcc.git] / gcc / expmed.c
blobca48c60683db749c1c9f11cd6f5d80cde699fdba
1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987-2017 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "predict.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "expmed.h"
33 #include "optabs.h"
34 #include "emit-rtl.h"
35 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "dojump.h"
39 #include "explow.h"
40 #include "expr.h"
41 #include "langhooks.h"
43 struct target_expmed default_target_expmed;
44 #if SWITCHABLE_TARGET
45 struct target_expmed *this_target_expmed = &default_target_expmed;
46 #endif
48 static void store_fixed_bit_field (rtx, opt_scalar_int_mode,
49 unsigned HOST_WIDE_INT,
50 unsigned HOST_WIDE_INT,
51 unsigned HOST_WIDE_INT,
52 unsigned HOST_WIDE_INT,
53 rtx, scalar_int_mode, bool);
54 static void store_fixed_bit_field_1 (rtx, scalar_int_mode,
55 unsigned HOST_WIDE_INT,
56 unsigned HOST_WIDE_INT,
57 rtx, scalar_int_mode, bool);
58 static void store_split_bit_field (rtx, opt_scalar_int_mode,
59 unsigned HOST_WIDE_INT,
60 unsigned HOST_WIDE_INT,
61 unsigned HOST_WIDE_INT,
62 unsigned HOST_WIDE_INT,
63 rtx, scalar_int_mode, bool);
64 static rtx extract_fixed_bit_field (machine_mode, rtx, opt_scalar_int_mode,
65 unsigned HOST_WIDE_INT,
66 unsigned HOST_WIDE_INT, rtx, int, bool);
67 static rtx extract_fixed_bit_field_1 (machine_mode, rtx, scalar_int_mode,
68 unsigned HOST_WIDE_INT,
69 unsigned HOST_WIDE_INT, rtx, int, bool);
70 static rtx lshift_value (machine_mode, unsigned HOST_WIDE_INT, int);
71 static rtx extract_split_bit_field (rtx, opt_scalar_int_mode,
72 unsigned HOST_WIDE_INT,
73 unsigned HOST_WIDE_INT, int, bool);
74 static void do_cmp_and_jump (rtx, rtx, enum rtx_code, machine_mode, rtx_code_label *);
75 static rtx expand_smod_pow2 (scalar_int_mode, rtx, HOST_WIDE_INT);
76 static rtx expand_sdiv_pow2 (scalar_int_mode, rtx, HOST_WIDE_INT);
78 /* Return a constant integer mask value of mode MODE with BITSIZE ones
79 followed by BITPOS zeros, or the complement of that if COMPLEMENT.
80 The mask is truncated if necessary to the width of mode MODE. The
81 mask is zero-extended if BITSIZE+BITPOS is too small for MODE. */
83 static inline rtx
84 mask_rtx (scalar_int_mode mode, int bitpos, int bitsize, bool complement)
86 return immed_wide_int_const
87 (wi::shifted_mask (bitpos, bitsize, complement,
88 GET_MODE_PRECISION (mode)), mode);
91 /* Test whether a value is zero of a power of two. */
92 #define EXACT_POWER_OF_2_OR_ZERO_P(x) \
93 (((x) & ((x) - HOST_WIDE_INT_1U)) == 0)
95 struct init_expmed_rtl
97 rtx reg;
98 rtx plus;
99 rtx neg;
100 rtx mult;
101 rtx sdiv;
102 rtx udiv;
103 rtx sdiv_32;
104 rtx smod_32;
105 rtx wide_mult;
106 rtx wide_lshr;
107 rtx wide_trunc;
108 rtx shift;
109 rtx shift_mult;
110 rtx shift_add;
111 rtx shift_sub0;
112 rtx shift_sub1;
113 rtx zext;
114 rtx trunc;
116 rtx pow2[MAX_BITS_PER_WORD];
117 rtx cint[MAX_BITS_PER_WORD];
120 static void
121 init_expmed_one_conv (struct init_expmed_rtl *all, scalar_int_mode to_mode,
122 scalar_int_mode from_mode, bool speed)
124 int to_size, from_size;
125 rtx which;
127 to_size = GET_MODE_PRECISION (to_mode);
128 from_size = GET_MODE_PRECISION (from_mode);
130 /* Most partial integers have a precision less than the "full"
131 integer it requires for storage. In case one doesn't, for
132 comparison purposes here, reduce the bit size by one in that
133 case. */
134 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT
135 && pow2p_hwi (to_size))
136 to_size --;
137 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT
138 && pow2p_hwi (from_size))
139 from_size --;
141 /* Assume cost of zero-extend and sign-extend is the same. */
142 which = (to_size < from_size ? all->trunc : all->zext);
144 PUT_MODE (all->reg, from_mode);
145 set_convert_cost (to_mode, from_mode, speed,
146 set_src_cost (which, to_mode, speed));
149 static void
150 init_expmed_one_mode (struct init_expmed_rtl *all,
151 machine_mode mode, int speed)
153 int m, n, mode_bitsize;
154 machine_mode mode_from;
156 mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
158 PUT_MODE (all->reg, mode);
159 PUT_MODE (all->plus, mode);
160 PUT_MODE (all->neg, mode);
161 PUT_MODE (all->mult, mode);
162 PUT_MODE (all->sdiv, mode);
163 PUT_MODE (all->udiv, mode);
164 PUT_MODE (all->sdiv_32, mode);
165 PUT_MODE (all->smod_32, mode);
166 PUT_MODE (all->wide_trunc, mode);
167 PUT_MODE (all->shift, mode);
168 PUT_MODE (all->shift_mult, mode);
169 PUT_MODE (all->shift_add, mode);
170 PUT_MODE (all->shift_sub0, mode);
171 PUT_MODE (all->shift_sub1, mode);
172 PUT_MODE (all->zext, mode);
173 PUT_MODE (all->trunc, mode);
175 set_add_cost (speed, mode, set_src_cost (all->plus, mode, speed));
176 set_neg_cost (speed, mode, set_src_cost (all->neg, mode, speed));
177 set_mul_cost (speed, mode, set_src_cost (all->mult, mode, speed));
178 set_sdiv_cost (speed, mode, set_src_cost (all->sdiv, mode, speed));
179 set_udiv_cost (speed, mode, set_src_cost (all->udiv, mode, speed));
181 set_sdiv_pow2_cheap (speed, mode, (set_src_cost (all->sdiv_32, mode, speed)
182 <= 2 * add_cost (speed, mode)));
183 set_smod_pow2_cheap (speed, mode, (set_src_cost (all->smod_32, mode, speed)
184 <= 4 * add_cost (speed, mode)));
186 set_shift_cost (speed, mode, 0, 0);
188 int cost = add_cost (speed, mode);
189 set_shiftadd_cost (speed, mode, 0, cost);
190 set_shiftsub0_cost (speed, mode, 0, cost);
191 set_shiftsub1_cost (speed, mode, 0, cost);
194 n = MIN (MAX_BITS_PER_WORD, mode_bitsize);
195 for (m = 1; m < n; m++)
197 XEXP (all->shift, 1) = all->cint[m];
198 XEXP (all->shift_mult, 1) = all->pow2[m];
200 set_shift_cost (speed, mode, m, set_src_cost (all->shift, mode, speed));
201 set_shiftadd_cost (speed, mode, m, set_src_cost (all->shift_add, mode,
202 speed));
203 set_shiftsub0_cost (speed, mode, m, set_src_cost (all->shift_sub0, mode,
204 speed));
205 set_shiftsub1_cost (speed, mode, m, set_src_cost (all->shift_sub1, mode,
206 speed));
209 scalar_int_mode int_mode_to;
210 if (is_a <scalar_int_mode> (mode, &int_mode_to))
212 for (mode_from = MIN_MODE_INT; mode_from <= MAX_MODE_INT;
213 mode_from = (machine_mode)(mode_from + 1))
214 init_expmed_one_conv (all, int_mode_to,
215 as_a <scalar_int_mode> (mode_from), speed);
217 scalar_int_mode wider_mode;
218 if (GET_MODE_CLASS (int_mode_to) == MODE_INT
219 && GET_MODE_WIDER_MODE (int_mode_to).exists (&wider_mode))
221 PUT_MODE (all->zext, wider_mode);
222 PUT_MODE (all->wide_mult, wider_mode);
223 PUT_MODE (all->wide_lshr, wider_mode);
224 XEXP (all->wide_lshr, 1) = GEN_INT (mode_bitsize);
226 set_mul_widen_cost (speed, wider_mode,
227 set_src_cost (all->wide_mult, wider_mode, speed));
228 set_mul_highpart_cost (speed, int_mode_to,
229 set_src_cost (all->wide_trunc,
230 int_mode_to, speed));
235 void
236 init_expmed (void)
238 struct init_expmed_rtl all;
239 machine_mode mode = QImode;
240 int m, speed;
242 memset (&all, 0, sizeof all);
243 for (m = 1; m < MAX_BITS_PER_WORD; m++)
245 all.pow2[m] = GEN_INT (HOST_WIDE_INT_1 << m);
246 all.cint[m] = GEN_INT (m);
249 /* Avoid using hard regs in ways which may be unsupported. */
250 all.reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
251 all.plus = gen_rtx_PLUS (mode, all.reg, all.reg);
252 all.neg = gen_rtx_NEG (mode, all.reg);
253 all.mult = gen_rtx_MULT (mode, all.reg, all.reg);
254 all.sdiv = gen_rtx_DIV (mode, all.reg, all.reg);
255 all.udiv = gen_rtx_UDIV (mode, all.reg, all.reg);
256 all.sdiv_32 = gen_rtx_DIV (mode, all.reg, all.pow2[5]);
257 all.smod_32 = gen_rtx_MOD (mode, all.reg, all.pow2[5]);
258 all.zext = gen_rtx_ZERO_EXTEND (mode, all.reg);
259 all.wide_mult = gen_rtx_MULT (mode, all.zext, all.zext);
260 all.wide_lshr = gen_rtx_LSHIFTRT (mode, all.wide_mult, all.reg);
261 all.wide_trunc = gen_rtx_TRUNCATE (mode, all.wide_lshr);
262 all.shift = gen_rtx_ASHIFT (mode, all.reg, all.reg);
263 all.shift_mult = gen_rtx_MULT (mode, all.reg, all.reg);
264 all.shift_add = gen_rtx_PLUS (mode, all.shift_mult, all.reg);
265 all.shift_sub0 = gen_rtx_MINUS (mode, all.shift_mult, all.reg);
266 all.shift_sub1 = gen_rtx_MINUS (mode, all.reg, all.shift_mult);
267 all.trunc = gen_rtx_TRUNCATE (mode, all.reg);
269 for (speed = 0; speed < 2; speed++)
271 crtl->maybe_hot_insn_p = speed;
272 set_zero_cost (speed, set_src_cost (const0_rtx, mode, speed));
274 for (mode = MIN_MODE_INT; mode <= MAX_MODE_INT;
275 mode = (machine_mode)(mode + 1))
276 init_expmed_one_mode (&all, mode, speed);
278 if (MIN_MODE_PARTIAL_INT != VOIDmode)
279 for (mode = MIN_MODE_PARTIAL_INT; mode <= MAX_MODE_PARTIAL_INT;
280 mode = (machine_mode)(mode + 1))
281 init_expmed_one_mode (&all, mode, speed);
283 if (MIN_MODE_VECTOR_INT != VOIDmode)
284 for (mode = MIN_MODE_VECTOR_INT; mode <= MAX_MODE_VECTOR_INT;
285 mode = (machine_mode)(mode + 1))
286 init_expmed_one_mode (&all, mode, speed);
289 if (alg_hash_used_p ())
291 struct alg_hash_entry *p = alg_hash_entry_ptr (0);
292 memset (p, 0, sizeof (*p) * NUM_ALG_HASH_ENTRIES);
294 else
295 set_alg_hash_used_p (true);
296 default_rtl_profile ();
298 ggc_free (all.trunc);
299 ggc_free (all.shift_sub1);
300 ggc_free (all.shift_sub0);
301 ggc_free (all.shift_add);
302 ggc_free (all.shift_mult);
303 ggc_free (all.shift);
304 ggc_free (all.wide_trunc);
305 ggc_free (all.wide_lshr);
306 ggc_free (all.wide_mult);
307 ggc_free (all.zext);
308 ggc_free (all.smod_32);
309 ggc_free (all.sdiv_32);
310 ggc_free (all.udiv);
311 ggc_free (all.sdiv);
312 ggc_free (all.mult);
313 ggc_free (all.neg);
314 ggc_free (all.plus);
315 ggc_free (all.reg);
318 /* Return an rtx representing minus the value of X.
319 MODE is the intended mode of the result,
320 useful if X is a CONST_INT. */
323 negate_rtx (machine_mode mode, rtx x)
325 rtx result = simplify_unary_operation (NEG, mode, x, mode);
327 if (result == 0)
328 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
330 return result;
333 /* Whether reverse storage order is supported on the target. */
334 static int reverse_storage_order_supported = -1;
336 /* Check whether reverse storage order is supported on the target. */
338 static void
339 check_reverse_storage_order_support (void)
341 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
343 reverse_storage_order_supported = 0;
344 sorry ("reverse scalar storage order");
346 else
347 reverse_storage_order_supported = 1;
350 /* Whether reverse FP storage order is supported on the target. */
351 static int reverse_float_storage_order_supported = -1;
353 /* Check whether reverse FP storage order is supported on the target. */
355 static void
356 check_reverse_float_storage_order_support (void)
358 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
360 reverse_float_storage_order_supported = 0;
361 sorry ("reverse floating-point scalar storage order");
363 else
364 reverse_float_storage_order_supported = 1;
367 /* Return an rtx representing value of X with reverse storage order.
368 MODE is the intended mode of the result,
369 useful if X is a CONST_INT. */
372 flip_storage_order (machine_mode mode, rtx x)
374 scalar_int_mode int_mode;
375 rtx result;
377 if (mode == QImode)
378 return x;
380 if (COMPLEX_MODE_P (mode))
382 rtx real = read_complex_part (x, false);
383 rtx imag = read_complex_part (x, true);
385 real = flip_storage_order (GET_MODE_INNER (mode), real);
386 imag = flip_storage_order (GET_MODE_INNER (mode), imag);
388 return gen_rtx_CONCAT (mode, real, imag);
391 if (__builtin_expect (reverse_storage_order_supported < 0, 0))
392 check_reverse_storage_order_support ();
394 if (!is_a <scalar_int_mode> (mode, &int_mode))
396 if (FLOAT_MODE_P (mode)
397 && __builtin_expect (reverse_float_storage_order_supported < 0, 0))
398 check_reverse_float_storage_order_support ();
400 if (!int_mode_for_size (GET_MODE_PRECISION (mode), 0).exists (&int_mode))
402 sorry ("reverse storage order for %smode", GET_MODE_NAME (mode));
403 return x;
405 x = gen_lowpart (int_mode, x);
408 result = simplify_unary_operation (BSWAP, int_mode, x, int_mode);
409 if (result == 0)
410 result = expand_unop (int_mode, bswap_optab, x, NULL_RTX, 1);
412 if (int_mode != mode)
413 result = gen_lowpart (mode, result);
415 return result;
418 /* If MODE is set, adjust bitfield memory MEM so that it points to the
419 first unit of mode MODE that contains a bitfield of size BITSIZE at
420 bit position BITNUM. If MODE is not set, return a BLKmode reference
421 to every byte in the bitfield. Set *NEW_BITNUM to the bit position
422 of the field within the new memory. */
424 static rtx
425 narrow_bit_field_mem (rtx mem, opt_scalar_int_mode mode,
426 unsigned HOST_WIDE_INT bitsize,
427 unsigned HOST_WIDE_INT bitnum,
428 unsigned HOST_WIDE_INT *new_bitnum)
430 scalar_int_mode imode;
431 if (mode.exists (&imode))
433 unsigned int unit = GET_MODE_BITSIZE (imode);
434 *new_bitnum = bitnum % unit;
435 HOST_WIDE_INT offset = (bitnum - *new_bitnum) / BITS_PER_UNIT;
436 return adjust_bitfield_address (mem, imode, offset);
438 else
440 *new_bitnum = bitnum % BITS_PER_UNIT;
441 HOST_WIDE_INT offset = bitnum / BITS_PER_UNIT;
442 HOST_WIDE_INT size = ((*new_bitnum + bitsize + BITS_PER_UNIT - 1)
443 / BITS_PER_UNIT);
444 return adjust_bitfield_address_size (mem, BLKmode, offset, size);
448 /* The caller wants to perform insertion or extraction PATTERN on a
449 bitfield of size BITSIZE at BITNUM bits into memory operand OP0.
450 BITREGION_START and BITREGION_END are as for store_bit_field
451 and FIELDMODE is the natural mode of the field.
453 Search for a mode that is compatible with the memory access
454 restrictions and (where applicable) with a register insertion or
455 extraction. Return the new memory on success, storing the adjusted
456 bit position in *NEW_BITNUM. Return null otherwise. */
458 static rtx
459 adjust_bit_field_mem_for_reg (enum extraction_pattern pattern,
460 rtx op0, HOST_WIDE_INT bitsize,
461 HOST_WIDE_INT bitnum,
462 unsigned HOST_WIDE_INT bitregion_start,
463 unsigned HOST_WIDE_INT bitregion_end,
464 machine_mode fieldmode,
465 unsigned HOST_WIDE_INT *new_bitnum)
467 bit_field_mode_iterator iter (bitsize, bitnum, bitregion_start,
468 bitregion_end, MEM_ALIGN (op0),
469 MEM_VOLATILE_P (op0));
470 scalar_int_mode best_mode;
471 if (iter.next_mode (&best_mode))
473 /* We can use a memory in BEST_MODE. See whether this is true for
474 any wider modes. All other things being equal, we prefer to
475 use the widest mode possible because it tends to expose more
476 CSE opportunities. */
477 if (!iter.prefer_smaller_modes ())
479 /* Limit the search to the mode required by the corresponding
480 register insertion or extraction instruction, if any. */
481 scalar_int_mode limit_mode = word_mode;
482 extraction_insn insn;
483 if (get_best_reg_extraction_insn (&insn, pattern,
484 GET_MODE_BITSIZE (best_mode),
485 fieldmode))
486 limit_mode = insn.field_mode;
488 scalar_int_mode wider_mode;
489 while (iter.next_mode (&wider_mode)
490 && GET_MODE_SIZE (wider_mode) <= GET_MODE_SIZE (limit_mode))
491 best_mode = wider_mode;
493 return narrow_bit_field_mem (op0, best_mode, bitsize, bitnum,
494 new_bitnum);
496 return NULL_RTX;
499 /* Return true if a bitfield of size BITSIZE at bit number BITNUM within
500 a structure of mode STRUCT_MODE represents a lowpart subreg. The subreg
501 offset is then BITNUM / BITS_PER_UNIT. */
503 static bool
504 lowpart_bit_field_p (unsigned HOST_WIDE_INT bitnum,
505 unsigned HOST_WIDE_INT bitsize,
506 machine_mode struct_mode)
508 if (BYTES_BIG_ENDIAN)
509 return (bitnum % BITS_PER_UNIT == 0
510 && (bitnum + bitsize == GET_MODE_BITSIZE (struct_mode)
511 || (bitnum + bitsize) % BITS_PER_WORD == 0));
512 else
513 return bitnum % BITS_PER_WORD == 0;
516 /* Return true if -fstrict-volatile-bitfields applies to an access of OP0
517 containing BITSIZE bits starting at BITNUM, with field mode FIELDMODE.
518 Return false if the access would touch memory outside the range
519 BITREGION_START to BITREGION_END for conformance to the C++ memory
520 model. */
522 static bool
523 strict_volatile_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
524 unsigned HOST_WIDE_INT bitnum,
525 scalar_int_mode fieldmode,
526 unsigned HOST_WIDE_INT bitregion_start,
527 unsigned HOST_WIDE_INT bitregion_end)
529 unsigned HOST_WIDE_INT modesize = GET_MODE_BITSIZE (fieldmode);
531 /* -fstrict-volatile-bitfields must be enabled and we must have a
532 volatile MEM. */
533 if (!MEM_P (op0)
534 || !MEM_VOLATILE_P (op0)
535 || flag_strict_volatile_bitfields <= 0)
536 return false;
538 /* The bit size must not be larger than the field mode, and
539 the field mode must not be larger than a word. */
540 if (bitsize > modesize || modesize > BITS_PER_WORD)
541 return false;
543 /* Check for cases of unaligned fields that must be split. */
544 if (bitnum % modesize + bitsize > modesize)
545 return false;
547 /* The memory must be sufficiently aligned for a MODESIZE access.
548 This condition guarantees, that the memory access will not
549 touch anything after the end of the structure. */
550 if (MEM_ALIGN (op0) < modesize)
551 return false;
553 /* Check for cases where the C++ memory model applies. */
554 if (bitregion_end != 0
555 && (bitnum - bitnum % modesize < bitregion_start
556 || bitnum - bitnum % modesize + modesize - 1 > bitregion_end))
557 return false;
559 return true;
562 /* Return true if OP is a memory and if a bitfield of size BITSIZE at
563 bit number BITNUM can be treated as a simple value of mode MODE. */
565 static bool
566 simple_mem_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
567 unsigned HOST_WIDE_INT bitnum, machine_mode mode)
569 return (MEM_P (op0)
570 && bitnum % BITS_PER_UNIT == 0
571 && bitsize == GET_MODE_BITSIZE (mode)
572 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
573 || (bitnum % GET_MODE_ALIGNMENT (mode) == 0
574 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode))));
577 /* Try to use instruction INSV to store VALUE into a field of OP0.
578 If OP0_MODE is defined, it is the mode of OP0, otherwise OP0 is a
579 BLKmode MEM. VALUE_MODE is the mode of VALUE. BITSIZE and BITNUM
580 are as for store_bit_field. */
582 static bool
583 store_bit_field_using_insv (const extraction_insn *insv, rtx op0,
584 opt_scalar_int_mode op0_mode,
585 unsigned HOST_WIDE_INT bitsize,
586 unsigned HOST_WIDE_INT bitnum,
587 rtx value, scalar_int_mode value_mode)
589 struct expand_operand ops[4];
590 rtx value1;
591 rtx xop0 = op0;
592 rtx_insn *last = get_last_insn ();
593 bool copy_back = false;
595 scalar_int_mode op_mode = insv->field_mode;
596 unsigned int unit = GET_MODE_BITSIZE (op_mode);
597 if (bitsize == 0 || bitsize > unit)
598 return false;
600 if (MEM_P (xop0))
601 /* Get a reference to the first byte of the field. */
602 xop0 = narrow_bit_field_mem (xop0, insv->struct_mode, bitsize, bitnum,
603 &bitnum);
604 else
606 /* Convert from counting within OP0 to counting in OP_MODE. */
607 if (BYTES_BIG_ENDIAN)
608 bitnum += unit - GET_MODE_BITSIZE (op0_mode.require ());
610 /* If xop0 is a register, we need it in OP_MODE
611 to make it acceptable to the format of insv. */
612 if (GET_CODE (xop0) == SUBREG)
613 /* We can't just change the mode, because this might clobber op0,
614 and we will need the original value of op0 if insv fails. */
615 xop0 = gen_rtx_SUBREG (op_mode, SUBREG_REG (xop0), SUBREG_BYTE (xop0));
616 if (REG_P (xop0) && GET_MODE (xop0) != op_mode)
617 xop0 = gen_lowpart_SUBREG (op_mode, xop0);
620 /* If the destination is a paradoxical subreg such that we need a
621 truncate to the inner mode, perform the insertion on a temporary and
622 truncate the result to the original destination. Note that we can't
623 just truncate the paradoxical subreg as (truncate:N (subreg:W (reg:N
624 X) 0)) is (reg:N X). */
625 if (GET_CODE (xop0) == SUBREG
626 && REG_P (SUBREG_REG (xop0))
627 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (SUBREG_REG (xop0)),
628 op_mode))
630 rtx tem = gen_reg_rtx (op_mode);
631 emit_move_insn (tem, xop0);
632 xop0 = tem;
633 copy_back = true;
636 /* There are similar overflow check at the start of store_bit_field_1,
637 but that only check the situation where the field lies completely
638 outside the register, while there do have situation where the field
639 lies partialy in the register, we need to adjust bitsize for this
640 partial overflow situation. Without this fix, pr48335-2.c on big-endian
641 will broken on those arch support bit insert instruction, like arm, aarch64
642 etc. */
643 if (bitsize + bitnum > unit && bitnum < unit)
645 warning (OPT_Wextra, "write of %wu-bit data outside the bound of "
646 "destination object, data truncated into %wu-bit",
647 bitsize, unit - bitnum);
648 bitsize = unit - bitnum;
651 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
652 "backwards" from the size of the unit we are inserting into.
653 Otherwise, we count bits from the most significant on a
654 BYTES/BITS_BIG_ENDIAN machine. */
656 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
657 bitnum = unit - bitsize - bitnum;
659 /* Convert VALUE to op_mode (which insv insn wants) in VALUE1. */
660 value1 = value;
661 if (value_mode != op_mode)
663 if (GET_MODE_BITSIZE (value_mode) >= bitsize)
665 rtx tmp;
666 /* Optimization: Don't bother really extending VALUE
667 if it has all the bits we will actually use. However,
668 if we must narrow it, be sure we do it correctly. */
670 if (GET_MODE_SIZE (value_mode) < GET_MODE_SIZE (op_mode))
672 tmp = simplify_subreg (op_mode, value1, value_mode, 0);
673 if (! tmp)
674 tmp = simplify_gen_subreg (op_mode,
675 force_reg (value_mode, value1),
676 value_mode, 0);
678 else
680 tmp = gen_lowpart_if_possible (op_mode, value1);
681 if (! tmp)
682 tmp = gen_lowpart (op_mode, force_reg (value_mode, value1));
684 value1 = tmp;
686 else if (CONST_INT_P (value))
687 value1 = gen_int_mode (INTVAL (value), op_mode);
688 else
689 /* Parse phase is supposed to make VALUE's data type
690 match that of the component reference, which is a type
691 at least as wide as the field; so VALUE should have
692 a mode that corresponds to that type. */
693 gcc_assert (CONSTANT_P (value));
696 create_fixed_operand (&ops[0], xop0);
697 create_integer_operand (&ops[1], bitsize);
698 create_integer_operand (&ops[2], bitnum);
699 create_input_operand (&ops[3], value1, op_mode);
700 if (maybe_expand_insn (insv->icode, 4, ops))
702 if (copy_back)
703 convert_move (op0, xop0, true);
704 return true;
706 delete_insns_since (last);
707 return false;
710 /* A subroutine of store_bit_field, with the same arguments. Return true
711 if the operation could be implemented.
713 If FALLBACK_P is true, fall back to store_fixed_bit_field if we have
714 no other way of implementing the operation. If FALLBACK_P is false,
715 return false instead. */
717 static bool
718 store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
719 unsigned HOST_WIDE_INT bitnum,
720 unsigned HOST_WIDE_INT bitregion_start,
721 unsigned HOST_WIDE_INT bitregion_end,
722 machine_mode fieldmode,
723 rtx value, bool reverse, bool fallback_p)
725 rtx op0 = str_rtx;
726 rtx orig_value;
728 while (GET_CODE (op0) == SUBREG)
730 bitnum += subreg_memory_offset (op0) * BITS_PER_UNIT;
731 op0 = SUBREG_REG (op0);
734 /* No action is needed if the target is a register and if the field
735 lies completely outside that register. This can occur if the source
736 code contains an out-of-bounds access to a small array. */
737 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
738 return true;
740 /* Use vec_set patterns for inserting parts of vectors whenever
741 available. */
742 machine_mode outermode = GET_MODE (op0);
743 scalar_mode innermode = GET_MODE_INNER (outermode);
744 if (VECTOR_MODE_P (outermode)
745 && !MEM_P (op0)
746 && optab_handler (vec_set_optab, outermode) != CODE_FOR_nothing
747 && fieldmode == innermode
748 && bitsize == GET_MODE_BITSIZE (innermode)
749 && !(bitnum % GET_MODE_BITSIZE (innermode)))
751 struct expand_operand ops[3];
752 enum insn_code icode = optab_handler (vec_set_optab, outermode);
753 int pos = bitnum / GET_MODE_BITSIZE (innermode);
755 create_fixed_operand (&ops[0], op0);
756 create_input_operand (&ops[1], value, innermode);
757 create_integer_operand (&ops[2], pos);
758 if (maybe_expand_insn (icode, 3, ops))
759 return true;
762 /* If the target is a register, overwriting the entire object, or storing
763 a full-word or multi-word field can be done with just a SUBREG. */
764 if (!MEM_P (op0)
765 && bitsize == GET_MODE_BITSIZE (fieldmode)
766 && ((bitsize == GET_MODE_BITSIZE (GET_MODE (op0)) && bitnum == 0)
767 || (bitsize % BITS_PER_WORD == 0 && bitnum % BITS_PER_WORD == 0)))
769 /* Use the subreg machinery either to narrow OP0 to the required
770 words or to cope with mode punning between equal-sized modes.
771 In the latter case, use subreg on the rhs side, not lhs. */
772 rtx sub;
774 if (bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
776 sub = simplify_gen_subreg (GET_MODE (op0), value, fieldmode, 0);
777 if (sub)
779 if (reverse)
780 sub = flip_storage_order (GET_MODE (op0), sub);
781 emit_move_insn (op0, sub);
782 return true;
785 else
787 sub = simplify_gen_subreg (fieldmode, op0, GET_MODE (op0),
788 bitnum / BITS_PER_UNIT);
789 if (sub)
791 if (reverse)
792 value = flip_storage_order (fieldmode, value);
793 emit_move_insn (sub, value);
794 return true;
799 /* If the target is memory, storing any naturally aligned field can be
800 done with a simple store. For targets that support fast unaligned
801 memory, any naturally sized, unit aligned field can be done directly. */
802 if (simple_mem_bitfield_p (op0, bitsize, bitnum, fieldmode))
804 op0 = adjust_bitfield_address (op0, fieldmode, bitnum / BITS_PER_UNIT);
805 if (reverse)
806 value = flip_storage_order (fieldmode, value);
807 emit_move_insn (op0, value);
808 return true;
811 /* Make sure we are playing with integral modes. Pun with subregs
812 if we aren't. This must come after the entire register case above,
813 since that case is valid for any mode. The following cases are only
814 valid for integral modes. */
815 opt_scalar_int_mode op0_mode = int_mode_for_mode (GET_MODE (op0));
816 scalar_int_mode imode;
817 if (!op0_mode.exists (&imode) || imode != GET_MODE (op0))
819 if (MEM_P (op0))
820 op0 = adjust_bitfield_address_size (op0, op0_mode.else_blk (),
821 0, MEM_SIZE (op0));
822 else
823 op0 = gen_lowpart (op0_mode.require (), op0);
826 /* Storing an lsb-aligned field in a register
827 can be done with a movstrict instruction. */
829 if (!MEM_P (op0)
830 && !reverse
831 && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
832 && bitsize == GET_MODE_BITSIZE (fieldmode)
833 && optab_handler (movstrict_optab, fieldmode) != CODE_FOR_nothing)
835 struct expand_operand ops[2];
836 enum insn_code icode = optab_handler (movstrict_optab, fieldmode);
837 rtx arg0 = op0;
838 unsigned HOST_WIDE_INT subreg_off;
840 if (GET_CODE (arg0) == SUBREG)
842 /* Else we've got some float mode source being extracted into
843 a different float mode destination -- this combination of
844 subregs results in Severe Tire Damage. */
845 gcc_assert (GET_MODE (SUBREG_REG (arg0)) == fieldmode
846 || GET_MODE_CLASS (fieldmode) == MODE_INT
847 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
848 arg0 = SUBREG_REG (arg0);
851 subreg_off = bitnum / BITS_PER_UNIT;
852 if (validate_subreg (fieldmode, GET_MODE (arg0), arg0, subreg_off))
854 arg0 = gen_rtx_SUBREG (fieldmode, arg0, subreg_off);
856 create_fixed_operand (&ops[0], arg0);
857 /* Shrink the source operand to FIELDMODE. */
858 create_convert_operand_to (&ops[1], value, fieldmode, false);
859 if (maybe_expand_insn (icode, 2, ops))
860 return true;
864 /* Handle fields bigger than a word. */
866 if (bitsize > BITS_PER_WORD)
868 /* Here we transfer the words of the field
869 in the order least significant first.
870 This is because the most significant word is the one which may
871 be less than full.
872 However, only do that if the value is not BLKmode. */
874 const bool backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
875 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
876 unsigned int i;
877 rtx_insn *last;
879 /* This is the mode we must force value to, so that there will be enough
880 subwords to extract. Note that fieldmode will often (always?) be
881 VOIDmode, because that is what store_field uses to indicate that this
882 is a bit field, but passing VOIDmode to operand_subword_force
883 is not allowed. */
884 fieldmode = GET_MODE (value);
885 if (fieldmode == VOIDmode)
886 fieldmode = smallest_int_mode_for_size (nwords * BITS_PER_WORD);
888 last = get_last_insn ();
889 for (i = 0; i < nwords; i++)
891 /* If I is 0, use the low-order word in both field and target;
892 if I is 1, use the next to lowest word; and so on. */
893 unsigned int wordnum = (backwards
894 ? GET_MODE_SIZE (fieldmode) / UNITS_PER_WORD
895 - i - 1
896 : i);
897 unsigned int bit_offset = (backwards ^ reverse
898 ? MAX ((int) bitsize - ((int) i + 1)
899 * BITS_PER_WORD,
901 : (int) i * BITS_PER_WORD);
902 rtx value_word = operand_subword_force (value, wordnum, fieldmode);
903 unsigned HOST_WIDE_INT new_bitsize =
904 MIN (BITS_PER_WORD, bitsize - i * BITS_PER_WORD);
906 /* If the remaining chunk doesn't have full wordsize we have
907 to make sure that for big-endian machines the higher order
908 bits are used. */
909 if (new_bitsize < BITS_PER_WORD && BYTES_BIG_ENDIAN && !backwards)
910 value_word = simplify_expand_binop (word_mode, lshr_optab,
911 value_word,
912 GEN_INT (BITS_PER_WORD
913 - new_bitsize),
914 NULL_RTX, true,
915 OPTAB_LIB_WIDEN);
917 if (!store_bit_field_1 (op0, new_bitsize,
918 bitnum + bit_offset,
919 bitregion_start, bitregion_end,
920 word_mode,
921 value_word, reverse, fallback_p))
923 delete_insns_since (last);
924 return false;
927 return true;
930 /* If VALUE has a floating-point or complex mode, access it as an
931 integer of the corresponding size. This can occur on a machine
932 with 64 bit registers that uses SFmode for float. It can also
933 occur for unaligned float or complex fields. */
934 orig_value = value;
935 scalar_int_mode value_mode;
936 if (GET_MODE (value) == VOIDmode)
937 /* By this point we've dealt with values that are bigger than a word,
938 so word_mode is a conservatively correct choice. */
939 value_mode = word_mode;
940 else if (!is_a <scalar_int_mode> (GET_MODE (value), &value_mode))
942 value_mode = int_mode_for_mode (GET_MODE (value)).require ();
943 value = gen_reg_rtx (value_mode);
944 emit_move_insn (gen_lowpart (GET_MODE (orig_value), value), orig_value);
947 /* If OP0 is a multi-word register, narrow it to the affected word.
948 If the region spans two words, defer to store_split_bit_field.
949 Don't do this if op0 is a single hard register wider than word
950 such as a float or vector register. */
951 if (!MEM_P (op0)
952 && GET_MODE_SIZE (op0_mode.require ()) > UNITS_PER_WORD
953 && (!REG_P (op0)
954 || !HARD_REGISTER_P (op0)
955 || HARD_REGNO_NREGS (REGNO (op0), op0_mode.require ()) != 1))
957 if (bitnum % BITS_PER_WORD + bitsize > BITS_PER_WORD)
959 if (!fallback_p)
960 return false;
962 store_split_bit_field (op0, op0_mode, bitsize, bitnum,
963 bitregion_start, bitregion_end,
964 value, value_mode, reverse);
965 return true;
967 op0 = simplify_gen_subreg (word_mode, op0, op0_mode.require (),
968 bitnum / BITS_PER_WORD * UNITS_PER_WORD);
969 gcc_assert (op0);
970 op0_mode = word_mode;
971 bitnum %= BITS_PER_WORD;
974 /* From here on we can assume that the field to be stored in fits
975 within a word. If the destination is a register, it too fits
976 in a word. */
978 extraction_insn insv;
979 if (!MEM_P (op0)
980 && !reverse
981 && get_best_reg_extraction_insn (&insv, EP_insv,
982 GET_MODE_BITSIZE (op0_mode.require ()),
983 fieldmode)
984 && store_bit_field_using_insv (&insv, op0, op0_mode,
985 bitsize, bitnum, value, value_mode))
986 return true;
988 /* If OP0 is a memory, try copying it to a register and seeing if a
989 cheap register alternative is available. */
990 if (MEM_P (op0) && !reverse)
992 if (get_best_mem_extraction_insn (&insv, EP_insv, bitsize, bitnum,
993 fieldmode)
994 && store_bit_field_using_insv (&insv, op0, op0_mode,
995 bitsize, bitnum, value, value_mode))
996 return true;
998 rtx_insn *last = get_last_insn ();
1000 /* Try loading part of OP0 into a register, inserting the bitfield
1001 into that, and then copying the result back to OP0. */
1002 unsigned HOST_WIDE_INT bitpos;
1003 rtx xop0 = adjust_bit_field_mem_for_reg (EP_insv, op0, bitsize, bitnum,
1004 bitregion_start, bitregion_end,
1005 fieldmode, &bitpos);
1006 if (xop0)
1008 rtx tempreg = copy_to_reg (xop0);
1009 if (store_bit_field_1 (tempreg, bitsize, bitpos,
1010 bitregion_start, bitregion_end,
1011 fieldmode, orig_value, reverse, false))
1013 emit_move_insn (xop0, tempreg);
1014 return true;
1016 delete_insns_since (last);
1020 if (!fallback_p)
1021 return false;
1023 store_fixed_bit_field (op0, op0_mode, bitsize, bitnum, bitregion_start,
1024 bitregion_end, value, value_mode, reverse);
1025 return true;
1028 /* Generate code to store value from rtx VALUE
1029 into a bit-field within structure STR_RTX
1030 containing BITSIZE bits starting at bit BITNUM.
1032 BITREGION_START is bitpos of the first bitfield in this region.
1033 BITREGION_END is the bitpos of the ending bitfield in this region.
1034 These two fields are 0, if the C++ memory model does not apply,
1035 or we are not interested in keeping track of bitfield regions.
1037 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
1039 If REVERSE is true, the store is to be done in reverse order. */
1041 void
1042 store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1043 unsigned HOST_WIDE_INT bitnum,
1044 unsigned HOST_WIDE_INT bitregion_start,
1045 unsigned HOST_WIDE_INT bitregion_end,
1046 machine_mode fieldmode,
1047 rtx value, bool reverse)
1049 /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
1050 scalar_int_mode int_mode;
1051 if (is_a <scalar_int_mode> (fieldmode, &int_mode)
1052 && strict_volatile_bitfield_p (str_rtx, bitsize, bitnum, int_mode,
1053 bitregion_start, bitregion_end))
1055 /* Storing of a full word can be done with a simple store.
1056 We know here that the field can be accessed with one single
1057 instruction. For targets that support unaligned memory,
1058 an unaligned access may be necessary. */
1059 if (bitsize == GET_MODE_BITSIZE (int_mode))
1061 str_rtx = adjust_bitfield_address (str_rtx, int_mode,
1062 bitnum / BITS_PER_UNIT);
1063 if (reverse)
1064 value = flip_storage_order (int_mode, value);
1065 gcc_assert (bitnum % BITS_PER_UNIT == 0);
1066 emit_move_insn (str_rtx, value);
1068 else
1070 rtx temp;
1072 str_rtx = narrow_bit_field_mem (str_rtx, int_mode, bitsize, bitnum,
1073 &bitnum);
1074 gcc_assert (bitnum + bitsize <= GET_MODE_BITSIZE (int_mode));
1075 temp = copy_to_reg (str_rtx);
1076 if (!store_bit_field_1 (temp, bitsize, bitnum, 0, 0,
1077 int_mode, value, reverse, true))
1078 gcc_unreachable ();
1080 emit_move_insn (str_rtx, temp);
1083 return;
1086 /* Under the C++0x memory model, we must not touch bits outside the
1087 bit region. Adjust the address to start at the beginning of the
1088 bit region. */
1089 if (MEM_P (str_rtx) && bitregion_start > 0)
1091 scalar_int_mode best_mode;
1092 machine_mode addr_mode = VOIDmode;
1093 HOST_WIDE_INT offset, size;
1095 gcc_assert ((bitregion_start % BITS_PER_UNIT) == 0);
1097 offset = bitregion_start / BITS_PER_UNIT;
1098 bitnum -= bitregion_start;
1099 size = (bitnum + bitsize + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1100 bitregion_end -= bitregion_start;
1101 bitregion_start = 0;
1102 if (get_best_mode (bitsize, bitnum,
1103 bitregion_start, bitregion_end,
1104 MEM_ALIGN (str_rtx), INT_MAX,
1105 MEM_VOLATILE_P (str_rtx), &best_mode))
1106 addr_mode = best_mode;
1107 str_rtx = adjust_bitfield_address_size (str_rtx, addr_mode,
1108 offset, size);
1111 if (!store_bit_field_1 (str_rtx, bitsize, bitnum,
1112 bitregion_start, bitregion_end,
1113 fieldmode, value, reverse, true))
1114 gcc_unreachable ();
1117 /* Use shifts and boolean operations to store VALUE into a bit field of
1118 width BITSIZE in OP0, starting at bit BITNUM. If OP0_MODE is defined,
1119 it is the mode of OP0, otherwise OP0 is a BLKmode MEM. VALUE_MODE is
1120 the mode of VALUE.
1122 If REVERSE is true, the store is to be done in reverse order. */
1124 static void
1125 store_fixed_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
1126 unsigned HOST_WIDE_INT bitsize,
1127 unsigned HOST_WIDE_INT bitnum,
1128 unsigned HOST_WIDE_INT bitregion_start,
1129 unsigned HOST_WIDE_INT bitregion_end,
1130 rtx value, scalar_int_mode value_mode, bool reverse)
1132 /* There is a case not handled here:
1133 a structure with a known alignment of just a halfword
1134 and a field split across two aligned halfwords within the structure.
1135 Or likewise a structure with a known alignment of just a byte
1136 and a field split across two bytes.
1137 Such cases are not supposed to be able to occur. */
1139 scalar_int_mode best_mode;
1140 if (MEM_P (op0))
1142 unsigned int max_bitsize = BITS_PER_WORD;
1143 scalar_int_mode imode;
1144 if (op0_mode.exists (&imode) && GET_MODE_BITSIZE (imode) < max_bitsize)
1145 max_bitsize = GET_MODE_BITSIZE (imode);
1147 if (!get_best_mode (bitsize, bitnum, bitregion_start, bitregion_end,
1148 MEM_ALIGN (op0), max_bitsize, MEM_VOLATILE_P (op0),
1149 &best_mode))
1151 /* The only way this should occur is if the field spans word
1152 boundaries. */
1153 store_split_bit_field (op0, op0_mode, bitsize, bitnum,
1154 bitregion_start, bitregion_end,
1155 value, value_mode, reverse);
1156 return;
1159 op0 = narrow_bit_field_mem (op0, best_mode, bitsize, bitnum, &bitnum);
1161 else
1162 best_mode = op0_mode.require ();
1164 store_fixed_bit_field_1 (op0, best_mode, bitsize, bitnum,
1165 value, value_mode, reverse);
1168 /* Helper function for store_fixed_bit_field, stores
1169 the bit field always using MODE, which is the mode of OP0. The other
1170 arguments are as for store_fixed_bit_field. */
1172 static void
1173 store_fixed_bit_field_1 (rtx op0, scalar_int_mode mode,
1174 unsigned HOST_WIDE_INT bitsize,
1175 unsigned HOST_WIDE_INT bitnum,
1176 rtx value, scalar_int_mode value_mode, bool reverse)
1178 rtx temp;
1179 int all_zero = 0;
1180 int all_one = 0;
1182 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
1183 for invalid input, such as f5 from gcc.dg/pr48335-2.c. */
1185 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
1186 /* BITNUM is the distance between our msb
1187 and that of the containing datum.
1188 Convert it to the distance from the lsb. */
1189 bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
1191 /* Now BITNUM is always the distance between our lsb
1192 and that of OP0. */
1194 /* Shift VALUE left by BITNUM bits. If VALUE is not constant,
1195 we must first convert its mode to MODE. */
1197 if (CONST_INT_P (value))
1199 unsigned HOST_WIDE_INT v = UINTVAL (value);
1201 if (bitsize < HOST_BITS_PER_WIDE_INT)
1202 v &= (HOST_WIDE_INT_1U << bitsize) - 1;
1204 if (v == 0)
1205 all_zero = 1;
1206 else if ((bitsize < HOST_BITS_PER_WIDE_INT
1207 && v == (HOST_WIDE_INT_1U << bitsize) - 1)
1208 || (bitsize == HOST_BITS_PER_WIDE_INT
1209 && v == HOST_WIDE_INT_M1U))
1210 all_one = 1;
1212 value = lshift_value (mode, v, bitnum);
1214 else
1216 int must_and = (GET_MODE_BITSIZE (value_mode) != bitsize
1217 && bitnum + bitsize != GET_MODE_BITSIZE (mode));
1219 if (value_mode != mode)
1220 value = convert_to_mode (mode, value, 1);
1222 if (must_and)
1223 value = expand_binop (mode, and_optab, value,
1224 mask_rtx (mode, 0, bitsize, 0),
1225 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1226 if (bitnum > 0)
1227 value = expand_shift (LSHIFT_EXPR, mode, value,
1228 bitnum, NULL_RTX, 1);
1231 if (reverse)
1232 value = flip_storage_order (mode, value);
1234 /* Now clear the chosen bits in OP0,
1235 except that if VALUE is -1 we need not bother. */
1236 /* We keep the intermediates in registers to allow CSE to combine
1237 consecutive bitfield assignments. */
1239 temp = force_reg (mode, op0);
1241 if (! all_one)
1243 rtx mask = mask_rtx (mode, bitnum, bitsize, 1);
1244 if (reverse)
1245 mask = flip_storage_order (mode, mask);
1246 temp = expand_binop (mode, and_optab, temp, mask,
1247 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1248 temp = force_reg (mode, temp);
1251 /* Now logical-or VALUE into OP0, unless it is zero. */
1253 if (! all_zero)
1255 temp = expand_binop (mode, ior_optab, temp, value,
1256 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1257 temp = force_reg (mode, temp);
1260 if (op0 != temp)
1262 op0 = copy_rtx (op0);
1263 emit_move_insn (op0, temp);
1267 /* Store a bit field that is split across multiple accessible memory objects.
1269 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
1270 BITSIZE is the field width; BITPOS the position of its first bit
1271 (within the word).
1272 VALUE is the value to store, which has mode VALUE_MODE.
1273 If OP0_MODE is defined, it is the mode of OP0, otherwise OP0 is
1274 a BLKmode MEM.
1276 If REVERSE is true, the store is to be done in reverse order.
1278 This does not yet handle fields wider than BITS_PER_WORD. */
1280 static void
1281 store_split_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
1282 unsigned HOST_WIDE_INT bitsize,
1283 unsigned HOST_WIDE_INT bitpos,
1284 unsigned HOST_WIDE_INT bitregion_start,
1285 unsigned HOST_WIDE_INT bitregion_end,
1286 rtx value, scalar_int_mode value_mode, bool reverse)
1288 unsigned int unit, total_bits, bitsdone = 0;
1290 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1291 much at a time. */
1292 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
1293 unit = BITS_PER_WORD;
1294 else
1295 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
1297 /* If OP0 is a memory with a mode, then UNIT must not be larger than
1298 OP0's mode as well. Otherwise, store_fixed_bit_field will call us
1299 again, and we will mutually recurse forever. */
1300 if (MEM_P (op0) && op0_mode.exists ())
1301 unit = MIN (unit, GET_MODE_BITSIZE (op0_mode.require ()));
1303 /* If VALUE is a constant other than a CONST_INT, get it into a register in
1304 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
1305 that VALUE might be a floating-point constant. */
1306 if (CONSTANT_P (value) && !CONST_INT_P (value))
1308 rtx word = gen_lowpart_common (word_mode, value);
1310 if (word && (value != word))
1311 value = word;
1312 else
1313 value = gen_lowpart_common (word_mode, force_reg (value_mode, value));
1314 value_mode = word_mode;
1317 total_bits = GET_MODE_BITSIZE (value_mode);
1319 while (bitsdone < bitsize)
1321 unsigned HOST_WIDE_INT thissize;
1322 unsigned HOST_WIDE_INT thispos;
1323 unsigned HOST_WIDE_INT offset;
1324 rtx part;
1326 offset = (bitpos + bitsdone) / unit;
1327 thispos = (bitpos + bitsdone) % unit;
1329 /* When region of bytes we can touch is restricted, decrease
1330 UNIT close to the end of the region as needed. If op0 is a REG
1331 or SUBREG of REG, don't do this, as there can't be data races
1332 on a register and we can expand shorter code in some cases. */
1333 if (bitregion_end
1334 && unit > BITS_PER_UNIT
1335 && bitpos + bitsdone - thispos + unit > bitregion_end + 1
1336 && !REG_P (op0)
1337 && (GET_CODE (op0) != SUBREG || !REG_P (SUBREG_REG (op0))))
1339 unit = unit / 2;
1340 continue;
1343 /* THISSIZE must not overrun a word boundary. Otherwise,
1344 store_fixed_bit_field will call us again, and we will mutually
1345 recurse forever. */
1346 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1347 thissize = MIN (thissize, unit - thispos);
1349 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
1351 /* Fetch successively less significant portions. */
1352 if (CONST_INT_P (value))
1353 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1354 >> (bitsize - bitsdone - thissize))
1355 & ((HOST_WIDE_INT_1 << thissize) - 1));
1356 /* Likewise, but the source is little-endian. */
1357 else if (reverse)
1358 part = extract_fixed_bit_field (word_mode, value, value_mode,
1359 thissize,
1360 bitsize - bitsdone - thissize,
1361 NULL_RTX, 1, false);
1362 else
1363 /* The args are chosen so that the last part includes the
1364 lsb. Give extract_bit_field the value it needs (with
1365 endianness compensation) to fetch the piece we want. */
1366 part = extract_fixed_bit_field (word_mode, value, value_mode,
1367 thissize,
1368 total_bits - bitsize + bitsdone,
1369 NULL_RTX, 1, false);
1371 else
1373 /* Fetch successively more significant portions. */
1374 if (CONST_INT_P (value))
1375 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1376 >> bitsdone)
1377 & ((HOST_WIDE_INT_1 << thissize) - 1));
1378 /* Likewise, but the source is big-endian. */
1379 else if (reverse)
1380 part = extract_fixed_bit_field (word_mode, value, value_mode,
1381 thissize,
1382 total_bits - bitsdone - thissize,
1383 NULL_RTX, 1, false);
1384 else
1385 part = extract_fixed_bit_field (word_mode, value, value_mode,
1386 thissize, bitsdone, NULL_RTX,
1387 1, false);
1390 /* If OP0 is a register, then handle OFFSET here. */
1391 rtx op0_piece = op0;
1392 opt_scalar_int_mode op0_piece_mode = op0_mode;
1393 if (SUBREG_P (op0) || REG_P (op0))
1395 scalar_int_mode imode;
1396 if (op0_mode.exists (&imode)
1397 && GET_MODE_SIZE (imode) < UNITS_PER_WORD)
1399 if (offset)
1400 op0_piece = const0_rtx;
1402 else
1404 op0_piece = operand_subword_force (op0,
1405 offset * unit / BITS_PER_WORD,
1406 GET_MODE (op0));
1407 op0_piece_mode = word_mode;
1409 offset &= BITS_PER_WORD / unit - 1;
1412 /* OFFSET is in UNITs, and UNIT is in bits. If WORD is const0_rtx,
1413 it is just an out-of-bounds access. Ignore it. */
1414 if (op0_piece != const0_rtx)
1415 store_fixed_bit_field (op0_piece, op0_piece_mode, thissize,
1416 offset * unit + thispos, bitregion_start,
1417 bitregion_end, part, word_mode, reverse);
1418 bitsdone += thissize;
1422 /* A subroutine of extract_bit_field_1 that converts return value X
1423 to either MODE or TMODE. MODE, TMODE and UNSIGNEDP are arguments
1424 to extract_bit_field. */
1426 static rtx
1427 convert_extracted_bit_field (rtx x, machine_mode mode,
1428 machine_mode tmode, bool unsignedp)
1430 if (GET_MODE (x) == tmode || GET_MODE (x) == mode)
1431 return x;
1433 /* If the x mode is not a scalar integral, first convert to the
1434 integer mode of that size and then access it as a floating-point
1435 value via a SUBREG. */
1436 if (!SCALAR_INT_MODE_P (tmode))
1438 scalar_int_mode int_mode = int_mode_for_mode (tmode).require ();
1439 x = convert_to_mode (int_mode, x, unsignedp);
1440 x = force_reg (int_mode, x);
1441 return gen_lowpart (tmode, x);
1444 return convert_to_mode (tmode, x, unsignedp);
1447 /* Try to use an ext(z)v pattern to extract a field from OP0.
1448 Return the extracted value on success, otherwise return null.
1449 EXTV describes the extraction instruction to use. If OP0_MODE
1450 is defined, it is the mode of OP0, otherwise OP0 is a BLKmode MEM.
1451 The other arguments are as for extract_bit_field. */
1453 static rtx
1454 extract_bit_field_using_extv (const extraction_insn *extv, rtx op0,
1455 opt_scalar_int_mode op0_mode,
1456 unsigned HOST_WIDE_INT bitsize,
1457 unsigned HOST_WIDE_INT bitnum,
1458 int unsignedp, rtx target,
1459 machine_mode mode, machine_mode tmode)
1461 struct expand_operand ops[4];
1462 rtx spec_target = target;
1463 rtx spec_target_subreg = 0;
1464 scalar_int_mode ext_mode = extv->field_mode;
1465 unsigned unit = GET_MODE_BITSIZE (ext_mode);
1467 if (bitsize == 0 || unit < bitsize)
1468 return NULL_RTX;
1470 if (MEM_P (op0))
1471 /* Get a reference to the first byte of the field. */
1472 op0 = narrow_bit_field_mem (op0, extv->struct_mode, bitsize, bitnum,
1473 &bitnum);
1474 else
1476 /* Convert from counting within OP0 to counting in EXT_MODE. */
1477 if (BYTES_BIG_ENDIAN)
1478 bitnum += unit - GET_MODE_BITSIZE (op0_mode.require ());
1480 /* If op0 is a register, we need it in EXT_MODE to make it
1481 acceptable to the format of ext(z)v. */
1482 if (GET_CODE (op0) == SUBREG && op0_mode.require () != ext_mode)
1483 return NULL_RTX;
1484 if (REG_P (op0) && op0_mode.require () != ext_mode)
1485 op0 = gen_lowpart_SUBREG (ext_mode, op0);
1488 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
1489 "backwards" from the size of the unit we are extracting from.
1490 Otherwise, we count bits from the most significant on a
1491 BYTES/BITS_BIG_ENDIAN machine. */
1493 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1494 bitnum = unit - bitsize - bitnum;
1496 if (target == 0)
1497 target = spec_target = gen_reg_rtx (tmode);
1499 if (GET_MODE (target) != ext_mode)
1501 /* Don't use LHS paradoxical subreg if explicit truncation is needed
1502 between the mode of the extraction (word_mode) and the target
1503 mode. Instead, create a temporary and use convert_move to set
1504 the target. */
1505 if (REG_P (target)
1506 && TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (target), ext_mode))
1508 target = gen_lowpart (ext_mode, target);
1509 if (partial_subreg_p (GET_MODE (spec_target), ext_mode))
1510 spec_target_subreg = target;
1512 else
1513 target = gen_reg_rtx (ext_mode);
1516 create_output_operand (&ops[0], target, ext_mode);
1517 create_fixed_operand (&ops[1], op0);
1518 create_integer_operand (&ops[2], bitsize);
1519 create_integer_operand (&ops[3], bitnum);
1520 if (maybe_expand_insn (extv->icode, 4, ops))
1522 target = ops[0].value;
1523 if (target == spec_target)
1524 return target;
1525 if (target == spec_target_subreg)
1526 return spec_target;
1527 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1529 return NULL_RTX;
1532 /* A subroutine of extract_bit_field, with the same arguments.
1533 If FALLBACK_P is true, fall back to extract_fixed_bit_field
1534 if we can find no other means of implementing the operation.
1535 if FALLBACK_P is false, return NULL instead. */
1537 static rtx
1538 extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1539 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1540 machine_mode mode, machine_mode tmode,
1541 bool reverse, bool fallback_p, rtx *alt_rtl)
1543 rtx op0 = str_rtx;
1544 machine_mode mode1;
1546 if (tmode == VOIDmode)
1547 tmode = mode;
1549 while (GET_CODE (op0) == SUBREG)
1551 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1552 op0 = SUBREG_REG (op0);
1555 /* If we have an out-of-bounds access to a register, just return an
1556 uninitialized register of the required mode. This can occur if the
1557 source code contains an out-of-bounds access to a small array. */
1558 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
1559 return gen_reg_rtx (tmode);
1561 if (REG_P (op0)
1562 && mode == GET_MODE (op0)
1563 && bitnum == 0
1564 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
1566 if (reverse)
1567 op0 = flip_storage_order (mode, op0);
1568 /* We're trying to extract a full register from itself. */
1569 return op0;
1572 /* First try to check for vector from vector extractions. */
1573 if (VECTOR_MODE_P (GET_MODE (op0))
1574 && !MEM_P (op0)
1575 && VECTOR_MODE_P (tmode)
1576 && GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (tmode))
1578 machine_mode new_mode = GET_MODE (op0);
1579 if (GET_MODE_INNER (new_mode) != GET_MODE_INNER (tmode))
1581 scalar_mode inner_mode = GET_MODE_INNER (tmode);
1582 unsigned int nunits = (GET_MODE_BITSIZE (GET_MODE (op0))
1583 / GET_MODE_UNIT_BITSIZE (tmode));
1584 if (!mode_for_vector (inner_mode, nunits).exists (&new_mode)
1585 || !VECTOR_MODE_P (new_mode)
1586 || GET_MODE_SIZE (new_mode) != GET_MODE_SIZE (GET_MODE (op0))
1587 || GET_MODE_INNER (new_mode) != GET_MODE_INNER (tmode)
1588 || !targetm.vector_mode_supported_p (new_mode))
1589 new_mode = VOIDmode;
1591 if (new_mode != VOIDmode
1592 && (convert_optab_handler (vec_extract_optab, new_mode, tmode)
1593 != CODE_FOR_nothing)
1594 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (tmode)
1595 == bitnum / GET_MODE_BITSIZE (tmode)))
1597 struct expand_operand ops[3];
1598 machine_mode outermode = new_mode;
1599 machine_mode innermode = tmode;
1600 enum insn_code icode
1601 = convert_optab_handler (vec_extract_optab, outermode, innermode);
1602 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1604 if (new_mode != GET_MODE (op0))
1605 op0 = gen_lowpart (new_mode, op0);
1606 create_output_operand (&ops[0], target, innermode);
1607 ops[0].target = 1;
1608 create_input_operand (&ops[1], op0, outermode);
1609 create_integer_operand (&ops[2], pos);
1610 if (maybe_expand_insn (icode, 3, ops))
1612 if (alt_rtl && ops[0].target)
1613 *alt_rtl = target;
1614 target = ops[0].value;
1615 if (GET_MODE (target) != mode)
1616 return gen_lowpart (tmode, target);
1617 return target;
1622 /* See if we can get a better vector mode before extracting. */
1623 if (VECTOR_MODE_P (GET_MODE (op0))
1624 && !MEM_P (op0)
1625 && GET_MODE_INNER (GET_MODE (op0)) != tmode)
1627 machine_mode new_mode;
1629 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1630 new_mode = MIN_MODE_VECTOR_FLOAT;
1631 else if (GET_MODE_CLASS (tmode) == MODE_FRACT)
1632 new_mode = MIN_MODE_VECTOR_FRACT;
1633 else if (GET_MODE_CLASS (tmode) == MODE_UFRACT)
1634 new_mode = MIN_MODE_VECTOR_UFRACT;
1635 else if (GET_MODE_CLASS (tmode) == MODE_ACCUM)
1636 new_mode = MIN_MODE_VECTOR_ACCUM;
1637 else if (GET_MODE_CLASS (tmode) == MODE_UACCUM)
1638 new_mode = MIN_MODE_VECTOR_UACCUM;
1639 else
1640 new_mode = MIN_MODE_VECTOR_INT;
1642 FOR_EACH_MODE_FROM (new_mode, new_mode)
1643 if (GET_MODE_SIZE (new_mode) == GET_MODE_SIZE (GET_MODE (op0))
1644 && GET_MODE_UNIT_SIZE (new_mode) == GET_MODE_SIZE (tmode)
1645 && targetm.vector_mode_supported_p (new_mode))
1646 break;
1647 if (new_mode != VOIDmode)
1648 op0 = gen_lowpart (new_mode, op0);
1651 /* Use vec_extract patterns for extracting parts of vectors whenever
1652 available. */
1653 machine_mode outermode = GET_MODE (op0);
1654 scalar_mode innermode = GET_MODE_INNER (outermode);
1655 if (VECTOR_MODE_P (outermode)
1656 && !MEM_P (op0)
1657 && (convert_optab_handler (vec_extract_optab, outermode, innermode)
1658 != CODE_FOR_nothing)
1659 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (innermode)
1660 == bitnum / GET_MODE_BITSIZE (innermode)))
1662 struct expand_operand ops[3];
1663 enum insn_code icode
1664 = convert_optab_handler (vec_extract_optab, outermode, innermode);
1665 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1667 create_output_operand (&ops[0], target, innermode);
1668 ops[0].target = 1;
1669 create_input_operand (&ops[1], op0, outermode);
1670 create_integer_operand (&ops[2], pos);
1671 if (maybe_expand_insn (icode, 3, ops))
1673 if (alt_rtl && ops[0].target)
1674 *alt_rtl = target;
1675 target = ops[0].value;
1676 if (GET_MODE (target) != mode)
1677 return gen_lowpart (tmode, target);
1678 return target;
1682 /* Make sure we are playing with integral modes. Pun with subregs
1683 if we aren't. */
1684 opt_scalar_int_mode op0_mode = int_mode_for_mode (GET_MODE (op0));
1685 scalar_int_mode imode;
1686 if (!op0_mode.exists (&imode) || imode != GET_MODE (op0))
1688 if (MEM_P (op0))
1689 op0 = adjust_bitfield_address_size (op0, op0_mode.else_blk (),
1690 0, MEM_SIZE (op0));
1691 else if (op0_mode.exists (&imode))
1693 op0 = gen_lowpart (imode, op0);
1695 /* If we got a SUBREG, force it into a register since we
1696 aren't going to be able to do another SUBREG on it. */
1697 if (GET_CODE (op0) == SUBREG)
1698 op0 = force_reg (imode, op0);
1700 else
1702 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (op0));
1703 rtx mem = assign_stack_temp (GET_MODE (op0), size);
1704 emit_move_insn (mem, op0);
1705 op0 = adjust_bitfield_address_size (mem, BLKmode, 0, size);
1709 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1710 If that's wrong, the solution is to test for it and set TARGET to 0
1711 if needed. */
1713 /* Get the mode of the field to use for atomic access or subreg
1714 conversion. */
1715 if (!SCALAR_INT_MODE_P (tmode)
1716 || !mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0).exists (&mode1))
1717 mode1 = mode;
1718 gcc_assert (mode1 != BLKmode);
1720 /* Extraction of a full MODE1 value can be done with a subreg as long
1721 as the least significant bit of the value is the least significant
1722 bit of either OP0 or a word of OP0. */
1723 if (!MEM_P (op0)
1724 && !reverse
1725 && lowpart_bit_field_p (bitnum, bitsize, op0_mode.require ())
1726 && bitsize == GET_MODE_BITSIZE (mode1)
1727 && TRULY_NOOP_TRUNCATION_MODES_P (mode1, op0_mode.require ()))
1729 rtx sub = simplify_gen_subreg (mode1, op0, op0_mode.require (),
1730 bitnum / BITS_PER_UNIT);
1731 if (sub)
1732 return convert_extracted_bit_field (sub, mode, tmode, unsignedp);
1735 /* Extraction of a full MODE1 value can be done with a load as long as
1736 the field is on a byte boundary and is sufficiently aligned. */
1737 if (simple_mem_bitfield_p (op0, bitsize, bitnum, mode1))
1739 op0 = adjust_bitfield_address (op0, mode1, bitnum / BITS_PER_UNIT);
1740 if (reverse)
1741 op0 = flip_storage_order (mode1, op0);
1742 return convert_extracted_bit_field (op0, mode, tmode, unsignedp);
1745 /* Handle fields bigger than a word. */
1747 if (bitsize > BITS_PER_WORD)
1749 /* Here we transfer the words of the field
1750 in the order least significant first.
1751 This is because the most significant word is the one which may
1752 be less than full. */
1754 const bool backwards = WORDS_BIG_ENDIAN;
1755 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1756 unsigned int i;
1757 rtx_insn *last;
1759 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1760 target = gen_reg_rtx (mode);
1762 /* In case we're about to clobber a base register or something
1763 (see gcc.c-torture/execute/20040625-1.c). */
1764 if (reg_mentioned_p (target, str_rtx))
1765 target = gen_reg_rtx (mode);
1767 /* Indicate for flow that the entire target reg is being set. */
1768 emit_clobber (target);
1770 last = get_last_insn ();
1771 for (i = 0; i < nwords; i++)
1773 /* If I is 0, use the low-order word in both field and target;
1774 if I is 1, use the next to lowest word; and so on. */
1775 /* Word number in TARGET to use. */
1776 unsigned int wordnum
1777 = (backwards
1778 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1779 : i);
1780 /* Offset from start of field in OP0. */
1781 unsigned int bit_offset = (backwards ^ reverse
1782 ? MAX ((int) bitsize - ((int) i + 1)
1783 * BITS_PER_WORD,
1785 : (int) i * BITS_PER_WORD);
1786 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1787 rtx result_part
1788 = extract_bit_field_1 (op0, MIN (BITS_PER_WORD,
1789 bitsize - i * BITS_PER_WORD),
1790 bitnum + bit_offset, 1, target_part,
1791 mode, word_mode, reverse, fallback_p, NULL);
1793 gcc_assert (target_part);
1794 if (!result_part)
1796 delete_insns_since (last);
1797 return NULL;
1800 if (result_part != target_part)
1801 emit_move_insn (target_part, result_part);
1804 if (unsignedp)
1806 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1807 need to be zero'd out. */
1808 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1810 unsigned int i, total_words;
1812 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1813 for (i = nwords; i < total_words; i++)
1814 emit_move_insn
1815 (operand_subword (target,
1816 backwards ? total_words - i - 1 : i,
1817 1, VOIDmode),
1818 const0_rtx);
1820 return target;
1823 /* Signed bit field: sign-extend with two arithmetic shifts. */
1824 target = expand_shift (LSHIFT_EXPR, mode, target,
1825 GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
1826 return expand_shift (RSHIFT_EXPR, mode, target,
1827 GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
1830 /* If OP0 is a multi-word register, narrow it to the affected word.
1831 If the region spans two words, defer to extract_split_bit_field. */
1832 if (!MEM_P (op0) && GET_MODE_SIZE (op0_mode.require ()) > UNITS_PER_WORD)
1834 if (bitnum % BITS_PER_WORD + bitsize > BITS_PER_WORD)
1836 if (!fallback_p)
1837 return NULL_RTX;
1838 target = extract_split_bit_field (op0, op0_mode, bitsize, bitnum,
1839 unsignedp, reverse);
1840 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1842 op0 = simplify_gen_subreg (word_mode, op0, op0_mode.require (),
1843 bitnum / BITS_PER_WORD * UNITS_PER_WORD);
1844 op0_mode = word_mode;
1845 bitnum %= BITS_PER_WORD;
1848 /* From here on we know the desired field is smaller than a word.
1849 If OP0 is a register, it too fits within a word. */
1850 enum extraction_pattern pattern = unsignedp ? EP_extzv : EP_extv;
1851 extraction_insn extv;
1852 if (!MEM_P (op0)
1853 && !reverse
1854 /* ??? We could limit the structure size to the part of OP0 that
1855 contains the field, with appropriate checks for endianness
1856 and TRULY_NOOP_TRUNCATION. */
1857 && get_best_reg_extraction_insn (&extv, pattern,
1858 GET_MODE_BITSIZE (op0_mode.require ()),
1859 tmode))
1861 rtx result = extract_bit_field_using_extv (&extv, op0, op0_mode,
1862 bitsize, bitnum,
1863 unsignedp, target, mode,
1864 tmode);
1865 if (result)
1866 return result;
1869 /* If OP0 is a memory, try copying it to a register and seeing if a
1870 cheap register alternative is available. */
1871 if (MEM_P (op0) & !reverse)
1873 if (get_best_mem_extraction_insn (&extv, pattern, bitsize, bitnum,
1874 tmode))
1876 rtx result = extract_bit_field_using_extv (&extv, op0, op0_mode,
1877 bitsize, bitnum,
1878 unsignedp, target, mode,
1879 tmode);
1880 if (result)
1881 return result;
1884 rtx_insn *last = get_last_insn ();
1886 /* Try loading part of OP0 into a register and extracting the
1887 bitfield from that. */
1888 unsigned HOST_WIDE_INT bitpos;
1889 rtx xop0 = adjust_bit_field_mem_for_reg (pattern, op0, bitsize, bitnum,
1890 0, 0, tmode, &bitpos);
1891 if (xop0)
1893 xop0 = copy_to_reg (xop0);
1894 rtx result = extract_bit_field_1 (xop0, bitsize, bitpos,
1895 unsignedp, target,
1896 mode, tmode, reverse, false, NULL);
1897 if (result)
1898 return result;
1899 delete_insns_since (last);
1903 if (!fallback_p)
1904 return NULL;
1906 /* Find a correspondingly-sized integer field, so we can apply
1907 shifts and masks to it. */
1908 scalar_int_mode int_mode;
1909 if (!int_mode_for_mode (tmode).exists (&int_mode))
1910 /* If this fails, we should probably push op0 out to memory and then
1911 do a load. */
1912 int_mode = int_mode_for_mode (mode).require ();
1914 target = extract_fixed_bit_field (int_mode, op0, op0_mode, bitsize,
1915 bitnum, target, unsignedp, reverse);
1917 /* Complex values must be reversed piecewise, so we need to undo the global
1918 reversal, convert to the complex mode and reverse again. */
1919 if (reverse && COMPLEX_MODE_P (tmode))
1921 target = flip_storage_order (int_mode, target);
1922 target = convert_extracted_bit_field (target, mode, tmode, unsignedp);
1923 target = flip_storage_order (tmode, target);
1925 else
1926 target = convert_extracted_bit_field (target, mode, tmode, unsignedp);
1928 return target;
1931 /* Generate code to extract a byte-field from STR_RTX
1932 containing BITSIZE bits, starting at BITNUM,
1933 and put it in TARGET if possible (if TARGET is nonzero).
1934 Regardless of TARGET, we return the rtx for where the value is placed.
1936 STR_RTX is the structure containing the byte (a REG or MEM).
1937 UNSIGNEDP is nonzero if this is an unsigned bit field.
1938 MODE is the natural mode of the field value once extracted.
1939 TMODE is the mode the caller would like the value to have;
1940 but the value may be returned with type MODE instead.
1942 If REVERSE is true, the extraction is to be done in reverse order.
1944 If a TARGET is specified and we can store in it at no extra cost,
1945 we do so, and return TARGET.
1946 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1947 if they are equally easy. */
1950 extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1951 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1952 machine_mode mode, machine_mode tmode, bool reverse,
1953 rtx *alt_rtl)
1955 machine_mode mode1;
1957 /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
1958 if (GET_MODE_BITSIZE (GET_MODE (str_rtx)) > 0)
1959 mode1 = GET_MODE (str_rtx);
1960 else if (target && GET_MODE_BITSIZE (GET_MODE (target)) > 0)
1961 mode1 = GET_MODE (target);
1962 else
1963 mode1 = tmode;
1965 scalar_int_mode int_mode;
1966 if (is_a <scalar_int_mode> (mode1, &int_mode)
1967 && strict_volatile_bitfield_p (str_rtx, bitsize, bitnum, int_mode, 0, 0))
1969 /* Extraction of a full INT_MODE value can be done with a simple load.
1970 We know here that the field can be accessed with one single
1971 instruction. For targets that support unaligned memory,
1972 an unaligned access may be necessary. */
1973 if (bitsize == GET_MODE_BITSIZE (int_mode))
1975 rtx result = adjust_bitfield_address (str_rtx, int_mode,
1976 bitnum / BITS_PER_UNIT);
1977 if (reverse)
1978 result = flip_storage_order (int_mode, result);
1979 gcc_assert (bitnum % BITS_PER_UNIT == 0);
1980 return convert_extracted_bit_field (result, mode, tmode, unsignedp);
1983 str_rtx = narrow_bit_field_mem (str_rtx, int_mode, bitsize, bitnum,
1984 &bitnum);
1985 gcc_assert (bitnum + bitsize <= GET_MODE_BITSIZE (int_mode));
1986 str_rtx = copy_to_reg (str_rtx);
1989 return extract_bit_field_1 (str_rtx, bitsize, bitnum, unsignedp,
1990 target, mode, tmode, reverse, true, alt_rtl);
1993 /* Use shifts and boolean operations to extract a field of BITSIZE bits
1994 from bit BITNUM of OP0. If OP0_MODE is defined, it is the mode of OP0,
1995 otherwise OP0 is a BLKmode MEM.
1997 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1998 If REVERSE is true, the extraction is to be done in reverse order.
2000 If TARGET is nonzero, attempts to store the value there
2001 and return TARGET, but this is not guaranteed.
2002 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
2004 static rtx
2005 extract_fixed_bit_field (machine_mode tmode, rtx op0,
2006 opt_scalar_int_mode op0_mode,
2007 unsigned HOST_WIDE_INT bitsize,
2008 unsigned HOST_WIDE_INT bitnum, rtx target,
2009 int unsignedp, bool reverse)
2011 scalar_int_mode mode;
2012 if (MEM_P (op0))
2014 if (!get_best_mode (bitsize, bitnum, 0, 0, MEM_ALIGN (op0),
2015 BITS_PER_WORD, MEM_VOLATILE_P (op0), &mode))
2016 /* The only way this should occur is if the field spans word
2017 boundaries. */
2018 return extract_split_bit_field (op0, op0_mode, bitsize, bitnum,
2019 unsignedp, reverse);
2021 op0 = narrow_bit_field_mem (op0, mode, bitsize, bitnum, &bitnum);
2023 else
2024 mode = op0_mode.require ();
2026 return extract_fixed_bit_field_1 (tmode, op0, mode, bitsize, bitnum,
2027 target, unsignedp, reverse);
2030 /* Helper function for extract_fixed_bit_field, extracts
2031 the bit field always using MODE, which is the mode of OP0.
2032 The other arguments are as for extract_fixed_bit_field. */
2034 static rtx
2035 extract_fixed_bit_field_1 (machine_mode tmode, rtx op0, scalar_int_mode mode,
2036 unsigned HOST_WIDE_INT bitsize,
2037 unsigned HOST_WIDE_INT bitnum, rtx target,
2038 int unsignedp, bool reverse)
2040 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
2041 for invalid input, such as extract equivalent of f5 from
2042 gcc.dg/pr48335-2.c. */
2044 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
2045 /* BITNUM is the distance between our msb and that of OP0.
2046 Convert it to the distance from the lsb. */
2047 bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
2049 /* Now BITNUM is always the distance between the field's lsb and that of OP0.
2050 We have reduced the big-endian case to the little-endian case. */
2051 if (reverse)
2052 op0 = flip_storage_order (mode, op0);
2054 if (unsignedp)
2056 if (bitnum)
2058 /* If the field does not already start at the lsb,
2059 shift it so it does. */
2060 /* Maybe propagate the target for the shift. */
2061 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
2062 if (tmode != mode)
2063 subtarget = 0;
2064 op0 = expand_shift (RSHIFT_EXPR, mode, op0, bitnum, subtarget, 1);
2066 /* Convert the value to the desired mode. TMODE must also be a
2067 scalar integer for this conversion to make sense, since we
2068 shouldn't reinterpret the bits. */
2069 scalar_int_mode new_mode = as_a <scalar_int_mode> (tmode);
2070 if (mode != new_mode)
2071 op0 = convert_to_mode (new_mode, op0, 1);
2073 /* Unless the msb of the field used to be the msb when we shifted,
2074 mask out the upper bits. */
2076 if (GET_MODE_BITSIZE (mode) != bitnum + bitsize)
2077 return expand_binop (new_mode, and_optab, op0,
2078 mask_rtx (new_mode, 0, bitsize, 0),
2079 target, 1, OPTAB_LIB_WIDEN);
2080 return op0;
2083 /* To extract a signed bit-field, first shift its msb to the msb of the word,
2084 then arithmetic-shift its lsb to the lsb of the word. */
2085 op0 = force_reg (mode, op0);
2087 /* Find the narrowest integer mode that contains the field. */
2089 opt_scalar_int_mode mode_iter;
2090 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2091 if (GET_MODE_BITSIZE (mode_iter.require ()) >= bitsize + bitnum)
2092 break;
2094 mode = mode_iter.require ();
2095 op0 = convert_to_mode (mode, op0, 0);
2097 if (mode != tmode)
2098 target = 0;
2100 if (GET_MODE_BITSIZE (mode) != (bitsize + bitnum))
2102 int amount = GET_MODE_BITSIZE (mode) - (bitsize + bitnum);
2103 /* Maybe propagate the target for the shift. */
2104 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
2105 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
2108 return expand_shift (RSHIFT_EXPR, mode, op0,
2109 GET_MODE_BITSIZE (mode) - bitsize, target, 0);
2112 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
2113 VALUE << BITPOS. */
2115 static rtx
2116 lshift_value (machine_mode mode, unsigned HOST_WIDE_INT value,
2117 int bitpos)
2119 return immed_wide_int_const (wi::lshift (value, bitpos), mode);
2122 /* Extract a bit field that is split across two words
2123 and return an RTX for the result.
2125 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
2126 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
2127 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.
2128 If OP0_MODE is defined, it is the mode of OP0, otherwise OP0 is
2129 a BLKmode MEM.
2131 If REVERSE is true, the extraction is to be done in reverse order. */
2133 static rtx
2134 extract_split_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
2135 unsigned HOST_WIDE_INT bitsize,
2136 unsigned HOST_WIDE_INT bitpos, int unsignedp,
2137 bool reverse)
2139 unsigned int unit;
2140 unsigned int bitsdone = 0;
2141 rtx result = NULL_RTX;
2142 int first = 1;
2144 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
2145 much at a time. */
2146 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
2147 unit = BITS_PER_WORD;
2148 else
2149 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
2151 while (bitsdone < bitsize)
2153 unsigned HOST_WIDE_INT thissize;
2154 rtx part;
2155 unsigned HOST_WIDE_INT thispos;
2156 unsigned HOST_WIDE_INT offset;
2158 offset = (bitpos + bitsdone) / unit;
2159 thispos = (bitpos + bitsdone) % unit;
2161 /* THISSIZE must not overrun a word boundary. Otherwise,
2162 extract_fixed_bit_field will call us again, and we will mutually
2163 recurse forever. */
2164 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
2165 thissize = MIN (thissize, unit - thispos);
2167 /* If OP0 is a register, then handle OFFSET here. */
2168 rtx op0_piece = op0;
2169 opt_scalar_int_mode op0_piece_mode = op0_mode;
2170 if (SUBREG_P (op0) || REG_P (op0))
2172 op0_piece = operand_subword_force (op0, offset, op0_mode.require ());
2173 op0_piece_mode = word_mode;
2174 offset = 0;
2177 /* Extract the parts in bit-counting order,
2178 whose meaning is determined by BYTES_PER_UNIT.
2179 OFFSET is in UNITs, and UNIT is in bits. */
2180 part = extract_fixed_bit_field (word_mode, op0_piece, op0_piece_mode,
2181 thissize, offset * unit + thispos,
2182 0, 1, reverse);
2183 bitsdone += thissize;
2185 /* Shift this part into place for the result. */
2186 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
2188 if (bitsize != bitsdone)
2189 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2190 bitsize - bitsdone, 0, 1);
2192 else
2194 if (bitsdone != thissize)
2195 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2196 bitsdone - thissize, 0, 1);
2199 if (first)
2200 result = part;
2201 else
2202 /* Combine the parts with bitwise or. This works
2203 because we extracted each part as an unsigned bit field. */
2204 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
2205 OPTAB_LIB_WIDEN);
2207 first = 0;
2210 /* Unsigned bit field: we are done. */
2211 if (unsignedp)
2212 return result;
2213 /* Signed bit field: sign-extend with two arithmetic shifts. */
2214 result = expand_shift (LSHIFT_EXPR, word_mode, result,
2215 BITS_PER_WORD - bitsize, NULL_RTX, 0);
2216 return expand_shift (RSHIFT_EXPR, word_mode, result,
2217 BITS_PER_WORD - bitsize, NULL_RTX, 0);
2220 /* Try to read the low bits of SRC as an rvalue of mode MODE, preserving
2221 the bit pattern. SRC_MODE is the mode of SRC; if this is smaller than
2222 MODE, fill the upper bits with zeros. Fail if the layout of either
2223 mode is unknown (as for CC modes) or if the extraction would involve
2224 unprofitable mode punning. Return the value on success, otherwise
2225 return null.
2227 This is different from gen_lowpart* in these respects:
2229 - the returned value must always be considered an rvalue
2231 - when MODE is wider than SRC_MODE, the extraction involves
2232 a zero extension
2234 - when MODE is smaller than SRC_MODE, the extraction involves
2235 a truncation (and is thus subject to TRULY_NOOP_TRUNCATION).
2237 In other words, this routine performs a computation, whereas the
2238 gen_lowpart* routines are conceptually lvalue or rvalue subreg
2239 operations. */
2242 extract_low_bits (machine_mode mode, machine_mode src_mode, rtx src)
2244 scalar_int_mode int_mode, src_int_mode;
2246 if (mode == src_mode)
2247 return src;
2249 if (CONSTANT_P (src))
2251 /* simplify_gen_subreg can't be used here, as if simplify_subreg
2252 fails, it will happily create (subreg (symbol_ref)) or similar
2253 invalid SUBREGs. */
2254 unsigned int byte = subreg_lowpart_offset (mode, src_mode);
2255 rtx ret = simplify_subreg (mode, src, src_mode, byte);
2256 if (ret)
2257 return ret;
2259 if (GET_MODE (src) == VOIDmode
2260 || !validate_subreg (mode, src_mode, src, byte))
2261 return NULL_RTX;
2263 src = force_reg (GET_MODE (src), src);
2264 return gen_rtx_SUBREG (mode, src, byte);
2267 if (GET_MODE_CLASS (mode) == MODE_CC || GET_MODE_CLASS (src_mode) == MODE_CC)
2268 return NULL_RTX;
2270 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (src_mode)
2271 && targetm.modes_tieable_p (mode, src_mode))
2273 rtx x = gen_lowpart_common (mode, src);
2274 if (x)
2275 return x;
2278 if (!int_mode_for_mode (src_mode).exists (&src_int_mode)
2279 || !int_mode_for_mode (mode).exists (&int_mode))
2280 return NULL_RTX;
2282 if (!targetm.modes_tieable_p (src_int_mode, src_mode))
2283 return NULL_RTX;
2284 if (!targetm.modes_tieable_p (int_mode, mode))
2285 return NULL_RTX;
2287 src = gen_lowpart (src_int_mode, src);
2288 src = convert_modes (int_mode, src_int_mode, src, true);
2289 src = gen_lowpart (mode, src);
2290 return src;
2293 /* Add INC into TARGET. */
2295 void
2296 expand_inc (rtx target, rtx inc)
2298 rtx value = expand_binop (GET_MODE (target), add_optab,
2299 target, inc,
2300 target, 0, OPTAB_LIB_WIDEN);
2301 if (value != target)
2302 emit_move_insn (target, value);
2305 /* Subtract DEC from TARGET. */
2307 void
2308 expand_dec (rtx target, rtx dec)
2310 rtx value = expand_binop (GET_MODE (target), sub_optab,
2311 target, dec,
2312 target, 0, OPTAB_LIB_WIDEN);
2313 if (value != target)
2314 emit_move_insn (target, value);
2317 /* Output a shift instruction for expression code CODE,
2318 with SHIFTED being the rtx for the value to shift,
2319 and AMOUNT the rtx for the amount to shift by.
2320 Store the result in the rtx TARGET, if that is convenient.
2321 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2322 Return the rtx for where the value is.
2323 If that cannot be done, abort the compilation unless MAY_FAIL is true,
2324 in which case 0 is returned. */
2326 static rtx
2327 expand_shift_1 (enum tree_code code, machine_mode mode, rtx shifted,
2328 rtx amount, rtx target, int unsignedp, bool may_fail = false)
2330 rtx op1, temp = 0;
2331 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
2332 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2333 optab lshift_optab = ashl_optab;
2334 optab rshift_arith_optab = ashr_optab;
2335 optab rshift_uns_optab = lshr_optab;
2336 optab lrotate_optab = rotl_optab;
2337 optab rrotate_optab = rotr_optab;
2338 machine_mode op1_mode;
2339 machine_mode scalar_mode = mode;
2340 int attempt;
2341 bool speed = optimize_insn_for_speed_p ();
2343 if (VECTOR_MODE_P (mode))
2344 scalar_mode = GET_MODE_INNER (mode);
2345 op1 = amount;
2346 op1_mode = GET_MODE (op1);
2348 /* Determine whether the shift/rotate amount is a vector, or scalar. If the
2349 shift amount is a vector, use the vector/vector shift patterns. */
2350 if (VECTOR_MODE_P (mode) && VECTOR_MODE_P (op1_mode))
2352 lshift_optab = vashl_optab;
2353 rshift_arith_optab = vashr_optab;
2354 rshift_uns_optab = vlshr_optab;
2355 lrotate_optab = vrotl_optab;
2356 rrotate_optab = vrotr_optab;
2359 /* Previously detected shift-counts computed by NEGATE_EXPR
2360 and shifted in the other direction; but that does not work
2361 on all machines. */
2363 if (SHIFT_COUNT_TRUNCATED)
2365 if (CONST_INT_P (op1)
2366 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2367 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (scalar_mode)))
2368 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2369 % GET_MODE_BITSIZE (scalar_mode));
2370 else if (GET_CODE (op1) == SUBREG
2371 && subreg_lowpart_p (op1)
2372 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op1)))
2373 && SCALAR_INT_MODE_P (GET_MODE (op1)))
2374 op1 = SUBREG_REG (op1);
2377 /* Canonicalize rotates by constant amount. If op1 is bitsize / 2,
2378 prefer left rotation, if op1 is from bitsize / 2 + 1 to
2379 bitsize - 1, use other direction of rotate with 1 .. bitsize / 2 - 1
2380 amount instead. */
2381 if (rotate
2382 && CONST_INT_P (op1)
2383 && IN_RANGE (INTVAL (op1), GET_MODE_BITSIZE (scalar_mode) / 2 + left,
2384 GET_MODE_BITSIZE (scalar_mode) - 1))
2386 op1 = GEN_INT (GET_MODE_BITSIZE (scalar_mode) - INTVAL (op1));
2387 left = !left;
2388 code = left ? LROTATE_EXPR : RROTATE_EXPR;
2391 /* Rotation of 16bit values by 8 bits is effectively equivalent to a bswaphi.
2392 Note that this is not the case for bigger values. For instance a rotation
2393 of 0x01020304 by 16 bits gives 0x03040102 which is different from
2394 0x04030201 (bswapsi). */
2395 if (rotate
2396 && CONST_INT_P (op1)
2397 && INTVAL (op1) == BITS_PER_UNIT
2398 && GET_MODE_SIZE (scalar_mode) == 2
2399 && optab_handler (bswap_optab, HImode) != CODE_FOR_nothing)
2400 return expand_unop (HImode, bswap_optab, shifted, NULL_RTX,
2401 unsignedp);
2403 if (op1 == const0_rtx)
2404 return shifted;
2406 /* Check whether its cheaper to implement a left shift by a constant
2407 bit count by a sequence of additions. */
2408 if (code == LSHIFT_EXPR
2409 && CONST_INT_P (op1)
2410 && INTVAL (op1) > 0
2411 && INTVAL (op1) < GET_MODE_PRECISION (scalar_mode)
2412 && INTVAL (op1) < MAX_BITS_PER_WORD
2413 && (shift_cost (speed, mode, INTVAL (op1))
2414 > INTVAL (op1) * add_cost (speed, mode))
2415 && shift_cost (speed, mode, INTVAL (op1)) != MAX_COST)
2417 int i;
2418 for (i = 0; i < INTVAL (op1); i++)
2420 temp = force_reg (mode, shifted);
2421 shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2422 unsignedp, OPTAB_LIB_WIDEN);
2424 return shifted;
2427 for (attempt = 0; temp == 0 && attempt < 3; attempt++)
2429 enum optab_methods methods;
2431 if (attempt == 0)
2432 methods = OPTAB_DIRECT;
2433 else if (attempt == 1)
2434 methods = OPTAB_WIDEN;
2435 else
2436 methods = OPTAB_LIB_WIDEN;
2438 if (rotate)
2440 /* Widening does not work for rotation. */
2441 if (methods == OPTAB_WIDEN)
2442 continue;
2443 else if (methods == OPTAB_LIB_WIDEN)
2445 /* If we have been unable to open-code this by a rotation,
2446 do it as the IOR of two shifts. I.e., to rotate A
2447 by N bits, compute
2448 (A << N) | ((unsigned) A >> ((-N) & (C - 1)))
2449 where C is the bitsize of A.
2451 It is theoretically possible that the target machine might
2452 not be able to perform either shift and hence we would
2453 be making two libcalls rather than just the one for the
2454 shift (similarly if IOR could not be done). We will allow
2455 this extremely unlikely lossage to avoid complicating the
2456 code below. */
2458 rtx subtarget = target == shifted ? 0 : target;
2459 rtx new_amount, other_amount;
2460 rtx temp1;
2462 new_amount = op1;
2463 if (op1 == const0_rtx)
2464 return shifted;
2465 else if (CONST_INT_P (op1))
2466 other_amount = GEN_INT (GET_MODE_BITSIZE (scalar_mode)
2467 - INTVAL (op1));
2468 else
2470 other_amount
2471 = simplify_gen_unary (NEG, GET_MODE (op1),
2472 op1, GET_MODE (op1));
2473 HOST_WIDE_INT mask = GET_MODE_PRECISION (scalar_mode) - 1;
2474 other_amount
2475 = simplify_gen_binary (AND, GET_MODE (op1), other_amount,
2476 gen_int_mode (mask, GET_MODE (op1)));
2479 shifted = force_reg (mode, shifted);
2481 temp = expand_shift_1 (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2482 mode, shifted, new_amount, 0, 1);
2483 temp1 = expand_shift_1 (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2484 mode, shifted, other_amount,
2485 subtarget, 1);
2486 return expand_binop (mode, ior_optab, temp, temp1, target,
2487 unsignedp, methods);
2490 temp = expand_binop (mode,
2491 left ? lrotate_optab : rrotate_optab,
2492 shifted, op1, target, unsignedp, methods);
2494 else if (unsignedp)
2495 temp = expand_binop (mode,
2496 left ? lshift_optab : rshift_uns_optab,
2497 shifted, op1, target, unsignedp, methods);
2499 /* Do arithmetic shifts.
2500 Also, if we are going to widen the operand, we can just as well
2501 use an arithmetic right-shift instead of a logical one. */
2502 if (temp == 0 && ! rotate
2503 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2505 enum optab_methods methods1 = methods;
2507 /* If trying to widen a log shift to an arithmetic shift,
2508 don't accept an arithmetic shift of the same size. */
2509 if (unsignedp)
2510 methods1 = OPTAB_MUST_WIDEN;
2512 /* Arithmetic shift */
2514 temp = expand_binop (mode,
2515 left ? lshift_optab : rshift_arith_optab,
2516 shifted, op1, target, unsignedp, methods1);
2519 /* We used to try extzv here for logical right shifts, but that was
2520 only useful for one machine, the VAX, and caused poor code
2521 generation there for lshrdi3, so the code was deleted and a
2522 define_expand for lshrsi3 was added to vax.md. */
2525 gcc_assert (temp != NULL_RTX || may_fail);
2526 return temp;
2529 /* Output a shift instruction for expression code CODE,
2530 with SHIFTED being the rtx for the value to shift,
2531 and AMOUNT the amount to shift by.
2532 Store the result in the rtx TARGET, if that is convenient.
2533 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2534 Return the rtx for where the value is. */
2537 expand_shift (enum tree_code code, machine_mode mode, rtx shifted,
2538 int amount, rtx target, int unsignedp)
2540 return expand_shift_1 (code, mode,
2541 shifted, GEN_INT (amount), target, unsignedp);
2544 /* Likewise, but return 0 if that cannot be done. */
2546 static rtx
2547 maybe_expand_shift (enum tree_code code, machine_mode mode, rtx shifted,
2548 int amount, rtx target, int unsignedp)
2550 return expand_shift_1 (code, mode,
2551 shifted, GEN_INT (amount), target, unsignedp, true);
2554 /* Output a shift instruction for expression code CODE,
2555 with SHIFTED being the rtx for the value to shift,
2556 and AMOUNT the tree for the amount to shift by.
2557 Store the result in the rtx TARGET, if that is convenient.
2558 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2559 Return the rtx for where the value is. */
2562 expand_variable_shift (enum tree_code code, machine_mode mode, rtx shifted,
2563 tree amount, rtx target, int unsignedp)
2565 return expand_shift_1 (code, mode,
2566 shifted, expand_normal (amount), target, unsignedp);
2570 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2571 const struct mult_cost *, machine_mode mode);
2572 static rtx expand_mult_const (machine_mode, rtx, HOST_WIDE_INT, rtx,
2573 const struct algorithm *, enum mult_variant);
2574 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2575 static rtx extract_high_half (scalar_int_mode, rtx);
2576 static rtx expmed_mult_highpart (scalar_int_mode, rtx, rtx, rtx, int, int);
2577 static rtx expmed_mult_highpart_optab (scalar_int_mode, rtx, rtx, rtx,
2578 int, int);
2579 /* Compute and return the best algorithm for multiplying by T.
2580 The algorithm must cost less than cost_limit
2581 If retval.cost >= COST_LIMIT, no algorithm was found and all
2582 other field of the returned struct are undefined.
2583 MODE is the machine mode of the multiplication. */
2585 static void
2586 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2587 const struct mult_cost *cost_limit, machine_mode mode)
2589 int m;
2590 struct algorithm *alg_in, *best_alg;
2591 struct mult_cost best_cost;
2592 struct mult_cost new_limit;
2593 int op_cost, op_latency;
2594 unsigned HOST_WIDE_INT orig_t = t;
2595 unsigned HOST_WIDE_INT q;
2596 int maxm, hash_index;
2597 bool cache_hit = false;
2598 enum alg_code cache_alg = alg_zero;
2599 bool speed = optimize_insn_for_speed_p ();
2600 scalar_int_mode imode;
2601 struct alg_hash_entry *entry_ptr;
2603 /* Indicate that no algorithm is yet found. If no algorithm
2604 is found, this value will be returned and indicate failure. */
2605 alg_out->cost.cost = cost_limit->cost + 1;
2606 alg_out->cost.latency = cost_limit->latency + 1;
2608 if (cost_limit->cost < 0
2609 || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2610 return;
2612 /* Be prepared for vector modes. */
2613 imode = as_a <scalar_int_mode> (GET_MODE_INNER (mode));
2615 maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (imode));
2617 /* Restrict the bits of "t" to the multiplication's mode. */
2618 t &= GET_MODE_MASK (imode);
2620 /* t == 1 can be done in zero cost. */
2621 if (t == 1)
2623 alg_out->ops = 1;
2624 alg_out->cost.cost = 0;
2625 alg_out->cost.latency = 0;
2626 alg_out->op[0] = alg_m;
2627 return;
2630 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2631 fail now. */
2632 if (t == 0)
2634 if (MULT_COST_LESS (cost_limit, zero_cost (speed)))
2635 return;
2636 else
2638 alg_out->ops = 1;
2639 alg_out->cost.cost = zero_cost (speed);
2640 alg_out->cost.latency = zero_cost (speed);
2641 alg_out->op[0] = alg_zero;
2642 return;
2646 /* We'll be needing a couple extra algorithm structures now. */
2648 alg_in = XALLOCA (struct algorithm);
2649 best_alg = XALLOCA (struct algorithm);
2650 best_cost = *cost_limit;
2652 /* Compute the hash index. */
2653 hash_index = (t ^ (unsigned int) mode ^ (speed * 256)) % NUM_ALG_HASH_ENTRIES;
2655 /* See if we already know what to do for T. */
2656 entry_ptr = alg_hash_entry_ptr (hash_index);
2657 if (entry_ptr->t == t
2658 && entry_ptr->mode == mode
2659 && entry_ptr->speed == speed
2660 && entry_ptr->alg != alg_unknown)
2662 cache_alg = entry_ptr->alg;
2664 if (cache_alg == alg_impossible)
2666 /* The cache tells us that it's impossible to synthesize
2667 multiplication by T within entry_ptr->cost. */
2668 if (!CHEAPER_MULT_COST (&entry_ptr->cost, cost_limit))
2669 /* COST_LIMIT is at least as restrictive as the one
2670 recorded in the hash table, in which case we have no
2671 hope of synthesizing a multiplication. Just
2672 return. */
2673 return;
2675 /* If we get here, COST_LIMIT is less restrictive than the
2676 one recorded in the hash table, so we may be able to
2677 synthesize a multiplication. Proceed as if we didn't
2678 have the cache entry. */
2680 else
2682 if (CHEAPER_MULT_COST (cost_limit, &entry_ptr->cost))
2683 /* The cached algorithm shows that this multiplication
2684 requires more cost than COST_LIMIT. Just return. This
2685 way, we don't clobber this cache entry with
2686 alg_impossible but retain useful information. */
2687 return;
2689 cache_hit = true;
2691 switch (cache_alg)
2693 case alg_shift:
2694 goto do_alg_shift;
2696 case alg_add_t_m2:
2697 case alg_sub_t_m2:
2698 goto do_alg_addsub_t_m2;
2700 case alg_add_factor:
2701 case alg_sub_factor:
2702 goto do_alg_addsub_factor;
2704 case alg_add_t2_m:
2705 goto do_alg_add_t2_m;
2707 case alg_sub_t2_m:
2708 goto do_alg_sub_t2_m;
2710 default:
2711 gcc_unreachable ();
2716 /* If we have a group of zero bits at the low-order part of T, try
2717 multiplying by the remaining bits and then doing a shift. */
2719 if ((t & 1) == 0)
2721 do_alg_shift:
2722 m = ctz_or_zero (t); /* m = number of low zero bits */
2723 if (m < maxm)
2725 q = t >> m;
2726 /* The function expand_shift will choose between a shift and
2727 a sequence of additions, so the observed cost is given as
2728 MIN (m * add_cost(speed, mode), shift_cost(speed, mode, m)). */
2729 op_cost = m * add_cost (speed, mode);
2730 if (shift_cost (speed, mode, m) < op_cost)
2731 op_cost = shift_cost (speed, mode, m);
2732 new_limit.cost = best_cost.cost - op_cost;
2733 new_limit.latency = best_cost.latency - op_cost;
2734 synth_mult (alg_in, q, &new_limit, mode);
2736 alg_in->cost.cost += op_cost;
2737 alg_in->cost.latency += op_cost;
2738 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2740 best_cost = alg_in->cost;
2741 std::swap (alg_in, best_alg);
2742 best_alg->log[best_alg->ops] = m;
2743 best_alg->op[best_alg->ops] = alg_shift;
2746 /* See if treating ORIG_T as a signed number yields a better
2747 sequence. Try this sequence only for a negative ORIG_T
2748 as it would be useless for a non-negative ORIG_T. */
2749 if ((HOST_WIDE_INT) orig_t < 0)
2751 /* Shift ORIG_T as follows because a right shift of a
2752 negative-valued signed type is implementation
2753 defined. */
2754 q = ~(~orig_t >> m);
2755 /* The function expand_shift will choose between a shift
2756 and a sequence of additions, so the observed cost is
2757 given as MIN (m * add_cost(speed, mode),
2758 shift_cost(speed, mode, m)). */
2759 op_cost = m * add_cost (speed, mode);
2760 if (shift_cost (speed, mode, m) < op_cost)
2761 op_cost = shift_cost (speed, mode, m);
2762 new_limit.cost = best_cost.cost - op_cost;
2763 new_limit.latency = best_cost.latency - op_cost;
2764 synth_mult (alg_in, q, &new_limit, mode);
2766 alg_in->cost.cost += op_cost;
2767 alg_in->cost.latency += op_cost;
2768 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2770 best_cost = alg_in->cost;
2771 std::swap (alg_in, best_alg);
2772 best_alg->log[best_alg->ops] = m;
2773 best_alg->op[best_alg->ops] = alg_shift;
2777 if (cache_hit)
2778 goto done;
2781 /* If we have an odd number, add or subtract one. */
2782 if ((t & 1) != 0)
2784 unsigned HOST_WIDE_INT w;
2786 do_alg_addsub_t_m2:
2787 for (w = 1; (w & t) != 0; w <<= 1)
2789 /* If T was -1, then W will be zero after the loop. This is another
2790 case where T ends with ...111. Handling this with (T + 1) and
2791 subtract 1 produces slightly better code and results in algorithm
2792 selection much faster than treating it like the ...0111 case
2793 below. */
2794 if (w == 0
2795 || (w > 2
2796 /* Reject the case where t is 3.
2797 Thus we prefer addition in that case. */
2798 && t != 3))
2800 /* T ends with ...111. Multiply by (T + 1) and subtract T. */
2802 op_cost = add_cost (speed, mode);
2803 new_limit.cost = best_cost.cost - op_cost;
2804 new_limit.latency = best_cost.latency - op_cost;
2805 synth_mult (alg_in, t + 1, &new_limit, mode);
2807 alg_in->cost.cost += op_cost;
2808 alg_in->cost.latency += op_cost;
2809 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2811 best_cost = alg_in->cost;
2812 std::swap (alg_in, best_alg);
2813 best_alg->log[best_alg->ops] = 0;
2814 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2817 else
2819 /* T ends with ...01 or ...011. Multiply by (T - 1) and add T. */
2821 op_cost = add_cost (speed, mode);
2822 new_limit.cost = best_cost.cost - op_cost;
2823 new_limit.latency = best_cost.latency - op_cost;
2824 synth_mult (alg_in, t - 1, &new_limit, mode);
2826 alg_in->cost.cost += op_cost;
2827 alg_in->cost.latency += op_cost;
2828 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2830 best_cost = alg_in->cost;
2831 std::swap (alg_in, best_alg);
2832 best_alg->log[best_alg->ops] = 0;
2833 best_alg->op[best_alg->ops] = alg_add_t_m2;
2837 /* We may be able to calculate a * -7, a * -15, a * -31, etc
2838 quickly with a - a * n for some appropriate constant n. */
2839 m = exact_log2 (-orig_t + 1);
2840 if (m >= 0 && m < maxm)
2842 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2843 /* If the target has a cheap shift-and-subtract insn use
2844 that in preference to a shift insn followed by a sub insn.
2845 Assume that the shift-and-sub is "atomic" with a latency
2846 equal to it's cost, otherwise assume that on superscalar
2847 hardware the shift may be executed concurrently with the
2848 earlier steps in the algorithm. */
2849 if (shiftsub1_cost (speed, mode, m) <= op_cost)
2851 op_cost = shiftsub1_cost (speed, mode, m);
2852 op_latency = op_cost;
2854 else
2855 op_latency = add_cost (speed, mode);
2857 new_limit.cost = best_cost.cost - op_cost;
2858 new_limit.latency = best_cost.latency - op_latency;
2859 synth_mult (alg_in, (unsigned HOST_WIDE_INT) (-orig_t + 1) >> m,
2860 &new_limit, mode);
2862 alg_in->cost.cost += op_cost;
2863 alg_in->cost.latency += op_latency;
2864 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2866 best_cost = alg_in->cost;
2867 std::swap (alg_in, best_alg);
2868 best_alg->log[best_alg->ops] = m;
2869 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2873 if (cache_hit)
2874 goto done;
2877 /* Look for factors of t of the form
2878 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2879 If we find such a factor, we can multiply by t using an algorithm that
2880 multiplies by q, shift the result by m and add/subtract it to itself.
2882 We search for large factors first and loop down, even if large factors
2883 are less probable than small; if we find a large factor we will find a
2884 good sequence quickly, and therefore be able to prune (by decreasing
2885 COST_LIMIT) the search. */
2887 do_alg_addsub_factor:
2888 for (m = floor_log2 (t - 1); m >= 2; m--)
2890 unsigned HOST_WIDE_INT d;
2892 d = (HOST_WIDE_INT_1U << m) + 1;
2893 if (t % d == 0 && t > d && m < maxm
2894 && (!cache_hit || cache_alg == alg_add_factor))
2896 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2897 if (shiftadd_cost (speed, mode, m) <= op_cost)
2898 op_cost = shiftadd_cost (speed, mode, m);
2900 op_latency = op_cost;
2903 new_limit.cost = best_cost.cost - op_cost;
2904 new_limit.latency = best_cost.latency - op_latency;
2905 synth_mult (alg_in, t / d, &new_limit, mode);
2907 alg_in->cost.cost += op_cost;
2908 alg_in->cost.latency += op_latency;
2909 if (alg_in->cost.latency < op_cost)
2910 alg_in->cost.latency = op_cost;
2911 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2913 best_cost = alg_in->cost;
2914 std::swap (alg_in, best_alg);
2915 best_alg->log[best_alg->ops] = m;
2916 best_alg->op[best_alg->ops] = alg_add_factor;
2918 /* Other factors will have been taken care of in the recursion. */
2919 break;
2922 d = (HOST_WIDE_INT_1U << m) - 1;
2923 if (t % d == 0 && t > d && m < maxm
2924 && (!cache_hit || cache_alg == alg_sub_factor))
2926 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2927 if (shiftsub0_cost (speed, mode, m) <= op_cost)
2928 op_cost = shiftsub0_cost (speed, mode, m);
2930 op_latency = op_cost;
2932 new_limit.cost = best_cost.cost - op_cost;
2933 new_limit.latency = best_cost.latency - op_latency;
2934 synth_mult (alg_in, t / d, &new_limit, mode);
2936 alg_in->cost.cost += op_cost;
2937 alg_in->cost.latency += op_latency;
2938 if (alg_in->cost.latency < op_cost)
2939 alg_in->cost.latency = op_cost;
2940 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2942 best_cost = alg_in->cost;
2943 std::swap (alg_in, best_alg);
2944 best_alg->log[best_alg->ops] = m;
2945 best_alg->op[best_alg->ops] = alg_sub_factor;
2947 break;
2950 if (cache_hit)
2951 goto done;
2953 /* Try shift-and-add (load effective address) instructions,
2954 i.e. do a*3, a*5, a*9. */
2955 if ((t & 1) != 0)
2957 do_alg_add_t2_m:
2958 q = t - 1;
2959 m = ctz_hwi (q);
2960 if (q && m < maxm)
2962 op_cost = shiftadd_cost (speed, mode, m);
2963 new_limit.cost = best_cost.cost - op_cost;
2964 new_limit.latency = best_cost.latency - op_cost;
2965 synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2967 alg_in->cost.cost += op_cost;
2968 alg_in->cost.latency += op_cost;
2969 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2971 best_cost = alg_in->cost;
2972 std::swap (alg_in, best_alg);
2973 best_alg->log[best_alg->ops] = m;
2974 best_alg->op[best_alg->ops] = alg_add_t2_m;
2977 if (cache_hit)
2978 goto done;
2980 do_alg_sub_t2_m:
2981 q = t + 1;
2982 m = ctz_hwi (q);
2983 if (q && m < maxm)
2985 op_cost = shiftsub0_cost (speed, mode, m);
2986 new_limit.cost = best_cost.cost - op_cost;
2987 new_limit.latency = best_cost.latency - op_cost;
2988 synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2990 alg_in->cost.cost += op_cost;
2991 alg_in->cost.latency += op_cost;
2992 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2994 best_cost = alg_in->cost;
2995 std::swap (alg_in, best_alg);
2996 best_alg->log[best_alg->ops] = m;
2997 best_alg->op[best_alg->ops] = alg_sub_t2_m;
3000 if (cache_hit)
3001 goto done;
3004 done:
3005 /* If best_cost has not decreased, we have not found any algorithm. */
3006 if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
3008 /* We failed to find an algorithm. Record alg_impossible for
3009 this case (that is, <T, MODE, COST_LIMIT>) so that next time
3010 we are asked to find an algorithm for T within the same or
3011 lower COST_LIMIT, we can immediately return to the
3012 caller. */
3013 entry_ptr->t = t;
3014 entry_ptr->mode = mode;
3015 entry_ptr->speed = speed;
3016 entry_ptr->alg = alg_impossible;
3017 entry_ptr->cost = *cost_limit;
3018 return;
3021 /* Cache the result. */
3022 if (!cache_hit)
3024 entry_ptr->t = t;
3025 entry_ptr->mode = mode;
3026 entry_ptr->speed = speed;
3027 entry_ptr->alg = best_alg->op[best_alg->ops];
3028 entry_ptr->cost.cost = best_cost.cost;
3029 entry_ptr->cost.latency = best_cost.latency;
3032 /* If we are getting a too long sequence for `struct algorithm'
3033 to record, make this search fail. */
3034 if (best_alg->ops == MAX_BITS_PER_WORD)
3035 return;
3037 /* Copy the algorithm from temporary space to the space at alg_out.
3038 We avoid using structure assignment because the majority of
3039 best_alg is normally undefined, and this is a critical function. */
3040 alg_out->ops = best_alg->ops + 1;
3041 alg_out->cost = best_cost;
3042 memcpy (alg_out->op, best_alg->op,
3043 alg_out->ops * sizeof *alg_out->op);
3044 memcpy (alg_out->log, best_alg->log,
3045 alg_out->ops * sizeof *alg_out->log);
3048 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
3049 Try three variations:
3051 - a shift/add sequence based on VAL itself
3052 - a shift/add sequence based on -VAL, followed by a negation
3053 - a shift/add sequence based on VAL - 1, followed by an addition.
3055 Return true if the cheapest of these cost less than MULT_COST,
3056 describing the algorithm in *ALG and final fixup in *VARIANT. */
3058 bool
3059 choose_mult_variant (machine_mode mode, HOST_WIDE_INT val,
3060 struct algorithm *alg, enum mult_variant *variant,
3061 int mult_cost)
3063 struct algorithm alg2;
3064 struct mult_cost limit;
3065 int op_cost;
3066 bool speed = optimize_insn_for_speed_p ();
3068 /* Fail quickly for impossible bounds. */
3069 if (mult_cost < 0)
3070 return false;
3072 /* Ensure that mult_cost provides a reasonable upper bound.
3073 Any constant multiplication can be performed with less
3074 than 2 * bits additions. */
3075 op_cost = 2 * GET_MODE_UNIT_BITSIZE (mode) * add_cost (speed, mode);
3076 if (mult_cost > op_cost)
3077 mult_cost = op_cost;
3079 *variant = basic_variant;
3080 limit.cost = mult_cost;
3081 limit.latency = mult_cost;
3082 synth_mult (alg, val, &limit, mode);
3084 /* This works only if the inverted value actually fits in an
3085 `unsigned int' */
3086 if (HOST_BITS_PER_INT >= GET_MODE_UNIT_BITSIZE (mode))
3088 op_cost = neg_cost (speed, mode);
3089 if (MULT_COST_LESS (&alg->cost, mult_cost))
3091 limit.cost = alg->cost.cost - op_cost;
3092 limit.latency = alg->cost.latency - op_cost;
3094 else
3096 limit.cost = mult_cost - op_cost;
3097 limit.latency = mult_cost - op_cost;
3100 synth_mult (&alg2, -val, &limit, mode);
3101 alg2.cost.cost += op_cost;
3102 alg2.cost.latency += op_cost;
3103 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
3104 *alg = alg2, *variant = negate_variant;
3107 /* This proves very useful for division-by-constant. */
3108 op_cost = add_cost (speed, mode);
3109 if (MULT_COST_LESS (&alg->cost, mult_cost))
3111 limit.cost = alg->cost.cost - op_cost;
3112 limit.latency = alg->cost.latency - op_cost;
3114 else
3116 limit.cost = mult_cost - op_cost;
3117 limit.latency = mult_cost - op_cost;
3120 synth_mult (&alg2, val - 1, &limit, mode);
3121 alg2.cost.cost += op_cost;
3122 alg2.cost.latency += op_cost;
3123 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
3124 *alg = alg2, *variant = add_variant;
3126 return MULT_COST_LESS (&alg->cost, mult_cost);
3129 /* A subroutine of expand_mult, used for constant multiplications.
3130 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
3131 convenient. Use the shift/add sequence described by ALG and apply
3132 the final fixup specified by VARIANT. */
3134 static rtx
3135 expand_mult_const (machine_mode mode, rtx op0, HOST_WIDE_INT val,
3136 rtx target, const struct algorithm *alg,
3137 enum mult_variant variant)
3139 unsigned HOST_WIDE_INT val_so_far;
3140 rtx_insn *insn;
3141 rtx accum, tem;
3142 int opno;
3143 machine_mode nmode;
3145 /* Avoid referencing memory over and over and invalid sharing
3146 on SUBREGs. */
3147 op0 = force_reg (mode, op0);
3149 /* ACCUM starts out either as OP0 or as a zero, depending on
3150 the first operation. */
3152 if (alg->op[0] == alg_zero)
3154 accum = copy_to_mode_reg (mode, CONST0_RTX (mode));
3155 val_so_far = 0;
3157 else if (alg->op[0] == alg_m)
3159 accum = copy_to_mode_reg (mode, op0);
3160 val_so_far = 1;
3162 else
3163 gcc_unreachable ();
3165 for (opno = 1; opno < alg->ops; opno++)
3167 int log = alg->log[opno];
3168 rtx shift_subtarget = optimize ? 0 : accum;
3169 rtx add_target
3170 = (opno == alg->ops - 1 && target != 0 && variant != add_variant
3171 && !optimize)
3172 ? target : 0;
3173 rtx accum_target = optimize ? 0 : accum;
3174 rtx accum_inner;
3176 switch (alg->op[opno])
3178 case alg_shift:
3179 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
3180 /* REG_EQUAL note will be attached to the following insn. */
3181 emit_move_insn (accum, tem);
3182 val_so_far <<= log;
3183 break;
3185 case alg_add_t_m2:
3186 tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
3187 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
3188 add_target ? add_target : accum_target);
3189 val_so_far += HOST_WIDE_INT_1U << log;
3190 break;
3192 case alg_sub_t_m2:
3193 tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
3194 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
3195 add_target ? add_target : accum_target);
3196 val_so_far -= HOST_WIDE_INT_1U << log;
3197 break;
3199 case alg_add_t2_m:
3200 accum = expand_shift (LSHIFT_EXPR, mode, accum,
3201 log, shift_subtarget, 0);
3202 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
3203 add_target ? add_target : accum_target);
3204 val_so_far = (val_so_far << log) + 1;
3205 break;
3207 case alg_sub_t2_m:
3208 accum = expand_shift (LSHIFT_EXPR, mode, accum,
3209 log, shift_subtarget, 0);
3210 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
3211 add_target ? add_target : accum_target);
3212 val_so_far = (val_so_far << log) - 1;
3213 break;
3215 case alg_add_factor:
3216 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
3217 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
3218 add_target ? add_target : accum_target);
3219 val_so_far += val_so_far << log;
3220 break;
3222 case alg_sub_factor:
3223 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
3224 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
3225 (add_target
3226 ? add_target : (optimize ? 0 : tem)));
3227 val_so_far = (val_so_far << log) - val_so_far;
3228 break;
3230 default:
3231 gcc_unreachable ();
3234 if (SCALAR_INT_MODE_P (mode))
3236 /* Write a REG_EQUAL note on the last insn so that we can cse
3237 multiplication sequences. Note that if ACCUM is a SUBREG,
3238 we've set the inner register and must properly indicate that. */
3239 tem = op0, nmode = mode;
3240 accum_inner = accum;
3241 if (GET_CODE (accum) == SUBREG)
3243 accum_inner = SUBREG_REG (accum);
3244 nmode = GET_MODE (accum_inner);
3245 tem = gen_lowpart (nmode, op0);
3248 insn = get_last_insn ();
3249 set_dst_reg_note (insn, REG_EQUAL,
3250 gen_rtx_MULT (nmode, tem,
3251 gen_int_mode (val_so_far, nmode)),
3252 accum_inner);
3256 if (variant == negate_variant)
3258 val_so_far = -val_so_far;
3259 accum = expand_unop (mode, neg_optab, accum, target, 0);
3261 else if (variant == add_variant)
3263 val_so_far = val_so_far + 1;
3264 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
3267 /* Compare only the bits of val and val_so_far that are significant
3268 in the result mode, to avoid sign-/zero-extension confusion. */
3269 nmode = GET_MODE_INNER (mode);
3270 val &= GET_MODE_MASK (nmode);
3271 val_so_far &= GET_MODE_MASK (nmode);
3272 gcc_assert (val == (HOST_WIDE_INT) val_so_far);
3274 return accum;
3277 /* Perform a multiplication and return an rtx for the result.
3278 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3279 TARGET is a suggestion for where to store the result (an rtx).
3281 We check specially for a constant integer as OP1.
3282 If you want this check for OP0 as well, then before calling
3283 you should swap the two operands if OP0 would be constant. */
3286 expand_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
3287 int unsignedp)
3289 enum mult_variant variant;
3290 struct algorithm algorithm;
3291 rtx scalar_op1;
3292 int max_cost;
3293 bool speed = optimize_insn_for_speed_p ();
3294 bool do_trapv = flag_trapv && SCALAR_INT_MODE_P (mode) && !unsignedp;
3296 if (CONSTANT_P (op0))
3297 std::swap (op0, op1);
3299 /* For vectors, there are several simplifications that can be made if
3300 all elements of the vector constant are identical. */
3301 scalar_op1 = unwrap_const_vec_duplicate (op1);
3303 if (INTEGRAL_MODE_P (mode))
3305 rtx fake_reg;
3306 HOST_WIDE_INT coeff;
3307 bool is_neg;
3308 int mode_bitsize;
3310 if (op1 == CONST0_RTX (mode))
3311 return op1;
3312 if (op1 == CONST1_RTX (mode))
3313 return op0;
3314 if (op1 == CONSTM1_RTX (mode))
3315 return expand_unop (mode, do_trapv ? negv_optab : neg_optab,
3316 op0, target, 0);
3318 if (do_trapv)
3319 goto skip_synth;
3321 /* If mode is integer vector mode, check if the backend supports
3322 vector lshift (by scalar or vector) at all. If not, we can't use
3323 synthetized multiply. */
3324 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
3325 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing
3326 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing)
3327 goto skip_synth;
3329 /* These are the operations that are potentially turned into
3330 a sequence of shifts and additions. */
3331 mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
3333 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3334 less than or equal in size to `unsigned int' this doesn't matter.
3335 If the mode is larger than `unsigned int', then synth_mult works
3336 only if the constant value exactly fits in an `unsigned int' without
3337 any truncation. This means that multiplying by negative values does
3338 not work; results are off by 2^32 on a 32 bit machine. */
3339 if (CONST_INT_P (scalar_op1))
3341 coeff = INTVAL (scalar_op1);
3342 is_neg = coeff < 0;
3344 #if TARGET_SUPPORTS_WIDE_INT
3345 else if (CONST_WIDE_INT_P (scalar_op1))
3346 #else
3347 else if (CONST_DOUBLE_AS_INT_P (scalar_op1))
3348 #endif
3350 int shift = wi::exact_log2 (rtx_mode_t (scalar_op1, mode));
3351 /* Perfect power of 2 (other than 1, which is handled above). */
3352 if (shift > 0)
3353 return expand_shift (LSHIFT_EXPR, mode, op0,
3354 shift, target, unsignedp);
3355 else
3356 goto skip_synth;
3358 else
3359 goto skip_synth;
3361 /* We used to test optimize here, on the grounds that it's better to
3362 produce a smaller program when -O is not used. But this causes
3363 such a terrible slowdown sometimes that it seems better to always
3364 use synth_mult. */
3366 /* Special case powers of two. */
3367 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff)
3368 && !(is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT))
3369 return expand_shift (LSHIFT_EXPR, mode, op0,
3370 floor_log2 (coeff), target, unsignedp);
3372 fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3374 /* Attempt to handle multiplication of DImode values by negative
3375 coefficients, by performing the multiplication by a positive
3376 multiplier and then inverting the result. */
3377 if (is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT)
3379 /* Its safe to use -coeff even for INT_MIN, as the
3380 result is interpreted as an unsigned coefficient.
3381 Exclude cost of op0 from max_cost to match the cost
3382 calculation of the synth_mult. */
3383 coeff = -(unsigned HOST_WIDE_INT) coeff;
3384 max_cost = (set_src_cost (gen_rtx_MULT (mode, fake_reg, op1),
3385 mode, speed)
3386 - neg_cost (speed, mode));
3387 if (max_cost <= 0)
3388 goto skip_synth;
3390 /* Special case powers of two. */
3391 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3393 rtx temp = expand_shift (LSHIFT_EXPR, mode, op0,
3394 floor_log2 (coeff), target, unsignedp);
3395 return expand_unop (mode, neg_optab, temp, target, 0);
3398 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3399 max_cost))
3401 rtx temp = expand_mult_const (mode, op0, coeff, NULL_RTX,
3402 &algorithm, variant);
3403 return expand_unop (mode, neg_optab, temp, target, 0);
3405 goto skip_synth;
3408 /* Exclude cost of op0 from max_cost to match the cost
3409 calculation of the synth_mult. */
3410 max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, op1), mode, speed);
3411 if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
3412 return expand_mult_const (mode, op0, coeff, target,
3413 &algorithm, variant);
3415 skip_synth:
3417 /* Expand x*2.0 as x+x. */
3418 if (CONST_DOUBLE_AS_FLOAT_P (scalar_op1)
3419 && real_equal (CONST_DOUBLE_REAL_VALUE (scalar_op1), &dconst2))
3421 op0 = force_reg (GET_MODE (op0), op0);
3422 return expand_binop (mode, add_optab, op0, op0,
3423 target, unsignedp, OPTAB_LIB_WIDEN);
3426 /* This used to use umul_optab if unsigned, but for non-widening multiply
3427 there is no difference between signed and unsigned. */
3428 op0 = expand_binop (mode, do_trapv ? smulv_optab : smul_optab,
3429 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
3430 gcc_assert (op0);
3431 return op0;
3434 /* Return a cost estimate for multiplying a register by the given
3435 COEFFicient in the given MODE and SPEED. */
3438 mult_by_coeff_cost (HOST_WIDE_INT coeff, machine_mode mode, bool speed)
3440 int max_cost;
3441 struct algorithm algorithm;
3442 enum mult_variant variant;
3444 rtx fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3445 max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, fake_reg),
3446 mode, speed);
3447 if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
3448 return algorithm.cost.cost;
3449 else
3450 return max_cost;
3453 /* Perform a widening multiplication and return an rtx for the result.
3454 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3455 TARGET is a suggestion for where to store the result (an rtx).
3456 THIS_OPTAB is the optab we should use, it must be either umul_widen_optab
3457 or smul_widen_optab.
3459 We check specially for a constant integer as OP1, comparing the
3460 cost of a widening multiply against the cost of a sequence of shifts
3461 and adds. */
3464 expand_widening_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
3465 int unsignedp, optab this_optab)
3467 bool speed = optimize_insn_for_speed_p ();
3468 rtx cop1;
3470 if (CONST_INT_P (op1)
3471 && GET_MODE (op0) != VOIDmode
3472 && (cop1 = convert_modes (mode, GET_MODE (op0), op1,
3473 this_optab == umul_widen_optab))
3474 && CONST_INT_P (cop1)
3475 && (INTVAL (cop1) >= 0
3476 || HWI_COMPUTABLE_MODE_P (mode)))
3478 HOST_WIDE_INT coeff = INTVAL (cop1);
3479 int max_cost;
3480 enum mult_variant variant;
3481 struct algorithm algorithm;
3483 if (coeff == 0)
3484 return CONST0_RTX (mode);
3486 /* Special case powers of two. */
3487 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3489 op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
3490 return expand_shift (LSHIFT_EXPR, mode, op0,
3491 floor_log2 (coeff), target, unsignedp);
3494 /* Exclude cost of op0 from max_cost to match the cost
3495 calculation of the synth_mult. */
3496 max_cost = mul_widen_cost (speed, mode);
3497 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3498 max_cost))
3500 op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
3501 return expand_mult_const (mode, op0, coeff, target,
3502 &algorithm, variant);
3505 return expand_binop (mode, this_optab, op0, op1, target,
3506 unsignedp, OPTAB_LIB_WIDEN);
3509 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3510 replace division by D, and put the least significant N bits of the result
3511 in *MULTIPLIER_PTR and return the most significant bit.
3513 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3514 needed precision is in PRECISION (should be <= N).
3516 PRECISION should be as small as possible so this function can choose
3517 multiplier more freely.
3519 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3520 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3522 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3523 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3525 unsigned HOST_WIDE_INT
3526 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
3527 unsigned HOST_WIDE_INT *multiplier_ptr,
3528 int *post_shift_ptr, int *lgup_ptr)
3530 int lgup, post_shift;
3531 int pow, pow2;
3533 /* lgup = ceil(log2(divisor)); */
3534 lgup = ceil_log2 (d);
3536 gcc_assert (lgup <= n);
3538 pow = n + lgup;
3539 pow2 = n + lgup - precision;
3541 /* mlow = 2^(N + lgup)/d */
3542 wide_int val = wi::set_bit_in_zero (pow, HOST_BITS_PER_DOUBLE_INT);
3543 wide_int mlow = wi::udiv_trunc (val, d);
3545 /* mhigh = (2^(N + lgup) + 2^(N + lgup - precision))/d */
3546 val |= wi::set_bit_in_zero (pow2, HOST_BITS_PER_DOUBLE_INT);
3547 wide_int mhigh = wi::udiv_trunc (val, d);
3549 /* If precision == N, then mlow, mhigh exceed 2^N
3550 (but they do not exceed 2^(N+1)). */
3552 /* Reduce to lowest terms. */
3553 for (post_shift = lgup; post_shift > 0; post_shift--)
3555 unsigned HOST_WIDE_INT ml_lo = wi::extract_uhwi (mlow, 1,
3556 HOST_BITS_PER_WIDE_INT);
3557 unsigned HOST_WIDE_INT mh_lo = wi::extract_uhwi (mhigh, 1,
3558 HOST_BITS_PER_WIDE_INT);
3559 if (ml_lo >= mh_lo)
3560 break;
3562 mlow = wi::uhwi (ml_lo, HOST_BITS_PER_DOUBLE_INT);
3563 mhigh = wi::uhwi (mh_lo, HOST_BITS_PER_DOUBLE_INT);
3566 *post_shift_ptr = post_shift;
3567 *lgup_ptr = lgup;
3568 if (n < HOST_BITS_PER_WIDE_INT)
3570 unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << n) - 1;
3571 *multiplier_ptr = mhigh.to_uhwi () & mask;
3572 return mhigh.to_uhwi () >= mask;
3574 else
3576 *multiplier_ptr = mhigh.to_uhwi ();
3577 return wi::extract_uhwi (mhigh, HOST_BITS_PER_WIDE_INT, 1);
3581 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3582 congruent to 1 (mod 2**N). */
3584 static unsigned HOST_WIDE_INT
3585 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
3587 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3589 /* The algorithm notes that the choice y = x satisfies
3590 x*y == 1 mod 2^3, since x is assumed odd.
3591 Each iteration doubles the number of bits of significance in y. */
3593 unsigned HOST_WIDE_INT mask;
3594 unsigned HOST_WIDE_INT y = x;
3595 int nbit = 3;
3597 mask = (n == HOST_BITS_PER_WIDE_INT
3598 ? HOST_WIDE_INT_M1U
3599 : (HOST_WIDE_INT_1U << n) - 1);
3601 while (nbit < n)
3603 y = y * (2 - x*y) & mask; /* Modulo 2^N */
3604 nbit *= 2;
3606 return y;
3609 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3610 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3611 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3612 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3613 become signed.
3615 The result is put in TARGET if that is convenient.
3617 MODE is the mode of operation. */
3620 expand_mult_highpart_adjust (scalar_int_mode mode, rtx adj_operand, rtx op0,
3621 rtx op1, rtx target, int unsignedp)
3623 rtx tem;
3624 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3626 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3627 GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
3628 tem = expand_and (mode, tem, op1, NULL_RTX);
3629 adj_operand
3630 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3631 adj_operand);
3633 tem = expand_shift (RSHIFT_EXPR, mode, op1,
3634 GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
3635 tem = expand_and (mode, tem, op0, NULL_RTX);
3636 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3637 target);
3639 return target;
3642 /* Subroutine of expmed_mult_highpart. Return the MODE high part of OP. */
3644 static rtx
3645 extract_high_half (scalar_int_mode mode, rtx op)
3647 if (mode == word_mode)
3648 return gen_highpart (mode, op);
3650 scalar_int_mode wider_mode = GET_MODE_WIDER_MODE (mode).require ();
3652 op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3653 GET_MODE_BITSIZE (mode), 0, 1);
3654 return convert_modes (mode, wider_mode, op, 0);
3657 /* Like expmed_mult_highpart, but only consider using a multiplication
3658 optab. OP1 is an rtx for the constant operand. */
3660 static rtx
3661 expmed_mult_highpart_optab (scalar_int_mode mode, rtx op0, rtx op1,
3662 rtx target, int unsignedp, int max_cost)
3664 rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3665 optab moptab;
3666 rtx tem;
3667 int size;
3668 bool speed = optimize_insn_for_speed_p ();
3670 scalar_int_mode wider_mode = GET_MODE_WIDER_MODE (mode).require ();
3672 size = GET_MODE_BITSIZE (mode);
3674 /* Firstly, try using a multiplication insn that only generates the needed
3675 high part of the product, and in the sign flavor of unsignedp. */
3676 if (mul_highpart_cost (speed, mode) < max_cost)
3678 moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3679 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3680 unsignedp, OPTAB_DIRECT);
3681 if (tem)
3682 return tem;
3685 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3686 Need to adjust the result after the multiplication. */
3687 if (size - 1 < BITS_PER_WORD
3688 && (mul_highpart_cost (speed, mode)
3689 + 2 * shift_cost (speed, mode, size-1)
3690 + 4 * add_cost (speed, mode) < max_cost))
3692 moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3693 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3694 unsignedp, OPTAB_DIRECT);
3695 if (tem)
3696 /* We used the wrong signedness. Adjust the result. */
3697 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3698 tem, unsignedp);
3701 /* Try widening multiplication. */
3702 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3703 if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
3704 && mul_widen_cost (speed, wider_mode) < max_cost)
3706 tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3707 unsignedp, OPTAB_WIDEN);
3708 if (tem)
3709 return extract_high_half (mode, tem);
3712 /* Try widening the mode and perform a non-widening multiplication. */
3713 if (optab_handler (smul_optab, wider_mode) != CODE_FOR_nothing
3714 && size - 1 < BITS_PER_WORD
3715 && (mul_cost (speed, wider_mode) + shift_cost (speed, mode, size-1)
3716 < max_cost))
3718 rtx_insn *insns;
3719 rtx wop0, wop1;
3721 /* We need to widen the operands, for example to ensure the
3722 constant multiplier is correctly sign or zero extended.
3723 Use a sequence to clean-up any instructions emitted by
3724 the conversions if things don't work out. */
3725 start_sequence ();
3726 wop0 = convert_modes (wider_mode, mode, op0, unsignedp);
3727 wop1 = convert_modes (wider_mode, mode, op1, unsignedp);
3728 tem = expand_binop (wider_mode, smul_optab, wop0, wop1, 0,
3729 unsignedp, OPTAB_WIDEN);
3730 insns = get_insns ();
3731 end_sequence ();
3733 if (tem)
3735 emit_insn (insns);
3736 return extract_high_half (mode, tem);
3740 /* Try widening multiplication of opposite signedness, and adjust. */
3741 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3742 if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
3743 && size - 1 < BITS_PER_WORD
3744 && (mul_widen_cost (speed, wider_mode)
3745 + 2 * shift_cost (speed, mode, size-1)
3746 + 4 * add_cost (speed, mode) < max_cost))
3748 tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3749 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3750 if (tem != 0)
3752 tem = extract_high_half (mode, tem);
3753 /* We used the wrong signedness. Adjust the result. */
3754 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3755 target, unsignedp);
3759 return 0;
3762 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3763 putting the high half of the result in TARGET if that is convenient,
3764 and return where the result is. If the operation can not be performed,
3765 0 is returned.
3767 MODE is the mode of operation and result.
3769 UNSIGNEDP nonzero means unsigned multiply.
3771 MAX_COST is the total allowed cost for the expanded RTL. */
3773 static rtx
3774 expmed_mult_highpart (scalar_int_mode mode, rtx op0, rtx op1,
3775 rtx target, int unsignedp, int max_cost)
3777 unsigned HOST_WIDE_INT cnst1;
3778 int extra_cost;
3779 bool sign_adjust = false;
3780 enum mult_variant variant;
3781 struct algorithm alg;
3782 rtx tem;
3783 bool speed = optimize_insn_for_speed_p ();
3785 /* We can't support modes wider than HOST_BITS_PER_INT. */
3786 gcc_assert (HWI_COMPUTABLE_MODE_P (mode));
3788 cnst1 = INTVAL (op1) & GET_MODE_MASK (mode);
3790 /* We can't optimize modes wider than BITS_PER_WORD.
3791 ??? We might be able to perform double-word arithmetic if
3792 mode == word_mode, however all the cost calculations in
3793 synth_mult etc. assume single-word operations. */
3794 scalar_int_mode wider_mode = GET_MODE_WIDER_MODE (mode).require ();
3795 if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3796 return expmed_mult_highpart_optab (mode, op0, op1, target,
3797 unsignedp, max_cost);
3799 extra_cost = shift_cost (speed, mode, GET_MODE_BITSIZE (mode) - 1);
3801 /* Check whether we try to multiply by a negative constant. */
3802 if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3804 sign_adjust = true;
3805 extra_cost += add_cost (speed, mode);
3808 /* See whether shift/add multiplication is cheap enough. */
3809 if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3810 max_cost - extra_cost))
3812 /* See whether the specialized multiplication optabs are
3813 cheaper than the shift/add version. */
3814 tem = expmed_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3815 alg.cost.cost + extra_cost);
3816 if (tem)
3817 return tem;
3819 tem = convert_to_mode (wider_mode, op0, unsignedp);
3820 tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3821 tem = extract_high_half (mode, tem);
3823 /* Adjust result for signedness. */
3824 if (sign_adjust)
3825 tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3827 return tem;
3829 return expmed_mult_highpart_optab (mode, op0, op1, target,
3830 unsignedp, max_cost);
3834 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3836 static rtx
3837 expand_smod_pow2 (scalar_int_mode mode, rtx op0, HOST_WIDE_INT d)
3839 rtx result, temp, shift;
3840 rtx_code_label *label;
3841 int logd;
3842 int prec = GET_MODE_PRECISION (mode);
3844 logd = floor_log2 (d);
3845 result = gen_reg_rtx (mode);
3847 /* Avoid conditional branches when they're expensive. */
3848 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2
3849 && optimize_insn_for_speed_p ())
3851 rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3852 mode, 0, -1);
3853 if (signmask)
3855 HOST_WIDE_INT masklow = (HOST_WIDE_INT_1 << logd) - 1;
3856 signmask = force_reg (mode, signmask);
3857 shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3859 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3860 which instruction sequence to use. If logical right shifts
3861 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3862 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3864 temp = gen_rtx_LSHIFTRT (mode, result, shift);
3865 if (optab_handler (lshr_optab, mode) == CODE_FOR_nothing
3866 || (set_src_cost (temp, mode, optimize_insn_for_speed_p ())
3867 > COSTS_N_INSNS (2)))
3869 temp = expand_binop (mode, xor_optab, op0, signmask,
3870 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3871 temp = expand_binop (mode, sub_optab, temp, signmask,
3872 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3873 temp = expand_binop (mode, and_optab, temp,
3874 gen_int_mode (masklow, mode),
3875 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3876 temp = expand_binop (mode, xor_optab, temp, signmask,
3877 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3878 temp = expand_binop (mode, sub_optab, temp, signmask,
3879 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3881 else
3883 signmask = expand_binop (mode, lshr_optab, signmask, shift,
3884 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3885 signmask = force_reg (mode, signmask);
3887 temp = expand_binop (mode, add_optab, op0, signmask,
3888 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3889 temp = expand_binop (mode, and_optab, temp,
3890 gen_int_mode (masklow, mode),
3891 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3892 temp = expand_binop (mode, sub_optab, temp, signmask,
3893 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3895 return temp;
3899 /* Mask contains the mode's signbit and the significant bits of the
3900 modulus. By including the signbit in the operation, many targets
3901 can avoid an explicit compare operation in the following comparison
3902 against zero. */
3903 wide_int mask = wi::mask (logd, false, prec);
3904 mask = wi::set_bit (mask, prec - 1);
3906 temp = expand_binop (mode, and_optab, op0,
3907 immed_wide_int_const (mask, mode),
3908 result, 1, OPTAB_LIB_WIDEN);
3909 if (temp != result)
3910 emit_move_insn (result, temp);
3912 label = gen_label_rtx ();
3913 do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3915 temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3916 0, OPTAB_LIB_WIDEN);
3918 mask = wi::mask (logd, true, prec);
3919 temp = expand_binop (mode, ior_optab, temp,
3920 immed_wide_int_const (mask, mode),
3921 result, 1, OPTAB_LIB_WIDEN);
3922 temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3923 0, OPTAB_LIB_WIDEN);
3924 if (temp != result)
3925 emit_move_insn (result, temp);
3926 emit_label (label);
3927 return result;
3930 /* Expand signed division of OP0 by a power of two D in mode MODE.
3931 This routine is only called for positive values of D. */
3933 static rtx
3934 expand_sdiv_pow2 (scalar_int_mode mode, rtx op0, HOST_WIDE_INT d)
3936 rtx temp;
3937 rtx_code_label *label;
3938 int logd;
3940 logd = floor_log2 (d);
3942 if (d == 2
3943 && BRANCH_COST (optimize_insn_for_speed_p (),
3944 false) >= 1)
3946 temp = gen_reg_rtx (mode);
3947 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3948 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3949 0, OPTAB_LIB_WIDEN);
3950 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3953 if (HAVE_conditional_move
3954 && BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2)
3956 rtx temp2;
3958 start_sequence ();
3959 temp2 = copy_to_mode_reg (mode, op0);
3960 temp = expand_binop (mode, add_optab, temp2, gen_int_mode (d - 1, mode),
3961 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3962 temp = force_reg (mode, temp);
3964 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3965 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3966 mode, temp, temp2, mode, 0);
3967 if (temp2)
3969 rtx_insn *seq = get_insns ();
3970 end_sequence ();
3971 emit_insn (seq);
3972 return expand_shift (RSHIFT_EXPR, mode, temp2, logd, NULL_RTX, 0);
3974 end_sequence ();
3977 if (BRANCH_COST (optimize_insn_for_speed_p (),
3978 false) >= 2)
3980 int ushift = GET_MODE_BITSIZE (mode) - logd;
3982 temp = gen_reg_rtx (mode);
3983 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3984 if (GET_MODE_BITSIZE (mode) >= BITS_PER_WORD
3985 || shift_cost (optimize_insn_for_speed_p (), mode, ushift)
3986 > COSTS_N_INSNS (1))
3987 temp = expand_binop (mode, and_optab, temp, gen_int_mode (d - 1, mode),
3988 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3989 else
3990 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3991 ushift, NULL_RTX, 1);
3992 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3993 0, OPTAB_LIB_WIDEN);
3994 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3997 label = gen_label_rtx ();
3998 temp = copy_to_mode_reg (mode, op0);
3999 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
4000 expand_inc (temp, gen_int_mode (d - 1, mode));
4001 emit_label (label);
4002 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
4005 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
4006 if that is convenient, and returning where the result is.
4007 You may request either the quotient or the remainder as the result;
4008 specify REM_FLAG nonzero to get the remainder.
4010 CODE is the expression code for which kind of division this is;
4011 it controls how rounding is done. MODE is the machine mode to use.
4012 UNSIGNEDP nonzero means do unsigned division. */
4014 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
4015 and then correct it by or'ing in missing high bits
4016 if result of ANDI is nonzero.
4017 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
4018 This could optimize to a bfexts instruction.
4019 But C doesn't use these operations, so their optimizations are
4020 left for later. */
4021 /* ??? For modulo, we don't actually need the highpart of the first product,
4022 the low part will do nicely. And for small divisors, the second multiply
4023 can also be a low-part only multiply or even be completely left out.
4024 E.g. to calculate the remainder of a division by 3 with a 32 bit
4025 multiply, multiply with 0x55555556 and extract the upper two bits;
4026 the result is exact for inputs up to 0x1fffffff.
4027 The input range can be reduced by using cross-sum rules.
4028 For odd divisors >= 3, the following table gives right shift counts
4029 so that if a number is shifted by an integer multiple of the given
4030 amount, the remainder stays the same:
4031 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
4032 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
4033 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
4034 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
4035 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
4037 Cross-sum rules for even numbers can be derived by leaving as many bits
4038 to the right alone as the divisor has zeros to the right.
4039 E.g. if x is an unsigned 32 bit number:
4040 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
4044 expand_divmod (int rem_flag, enum tree_code code, machine_mode mode,
4045 rtx op0, rtx op1, rtx target, int unsignedp)
4047 machine_mode compute_mode;
4048 rtx tquotient;
4049 rtx quotient = 0, remainder = 0;
4050 rtx_insn *last;
4051 rtx_insn *insn;
4052 optab optab1, optab2;
4053 int op1_is_constant, op1_is_pow2 = 0;
4054 int max_cost, extra_cost;
4055 static HOST_WIDE_INT last_div_const = 0;
4056 bool speed = optimize_insn_for_speed_p ();
4058 op1_is_constant = CONST_INT_P (op1);
4059 if (op1_is_constant)
4061 wide_int ext_op1 = rtx_mode_t (op1, mode);
4062 op1_is_pow2 = (wi::popcount (ext_op1) == 1
4063 || (! unsignedp
4064 && wi::popcount (wi::neg (ext_op1)) == 1));
4068 This is the structure of expand_divmod:
4070 First comes code to fix up the operands so we can perform the operations
4071 correctly and efficiently.
4073 Second comes a switch statement with code specific for each rounding mode.
4074 For some special operands this code emits all RTL for the desired
4075 operation, for other cases, it generates only a quotient and stores it in
4076 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
4077 to indicate that it has not done anything.
4079 Last comes code that finishes the operation. If QUOTIENT is set and
4080 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
4081 QUOTIENT is not set, it is computed using trunc rounding.
4083 We try to generate special code for division and remainder when OP1 is a
4084 constant. If |OP1| = 2**n we can use shifts and some other fast
4085 operations. For other values of OP1, we compute a carefully selected
4086 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
4087 by m.
4089 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
4090 half of the product. Different strategies for generating the product are
4091 implemented in expmed_mult_highpart.
4093 If what we actually want is the remainder, we generate that by another
4094 by-constant multiplication and a subtraction. */
4096 /* We shouldn't be called with OP1 == const1_rtx, but some of the
4097 code below will malfunction if we are, so check here and handle
4098 the special case if so. */
4099 if (op1 == const1_rtx)
4100 return rem_flag ? const0_rtx : op0;
4102 /* When dividing by -1, we could get an overflow.
4103 negv_optab can handle overflows. */
4104 if (! unsignedp && op1 == constm1_rtx)
4106 if (rem_flag)
4107 return const0_rtx;
4108 return expand_unop (mode, flag_trapv && GET_MODE_CLASS (mode) == MODE_INT
4109 ? negv_optab : neg_optab, op0, target, 0);
4112 if (target
4113 /* Don't use the function value register as a target
4114 since we have to read it as well as write it,
4115 and function-inlining gets confused by this. */
4116 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
4117 /* Don't clobber an operand while doing a multi-step calculation. */
4118 || ((rem_flag || op1_is_constant)
4119 && (reg_mentioned_p (target, op0)
4120 || (MEM_P (op0) && MEM_P (target))))
4121 || reg_mentioned_p (target, op1)
4122 || (MEM_P (op1) && MEM_P (target))))
4123 target = 0;
4125 /* Get the mode in which to perform this computation. Normally it will
4126 be MODE, but sometimes we can't do the desired operation in MODE.
4127 If so, pick a wider mode in which we can do the operation. Convert
4128 to that mode at the start to avoid repeated conversions.
4130 First see what operations we need. These depend on the expression
4131 we are evaluating. (We assume that divxx3 insns exist under the
4132 same conditions that modxx3 insns and that these insns don't normally
4133 fail. If these assumptions are not correct, we may generate less
4134 efficient code in some cases.)
4136 Then see if we find a mode in which we can open-code that operation
4137 (either a division, modulus, or shift). Finally, check for the smallest
4138 mode for which we can do the operation with a library call. */
4140 /* We might want to refine this now that we have division-by-constant
4141 optimization. Since expmed_mult_highpart tries so many variants, it is
4142 not straightforward to generalize this. Maybe we should make an array
4143 of possible modes in init_expmed? Save this for GCC 2.7. */
4145 optab1 = (op1_is_pow2
4146 ? (unsignedp ? lshr_optab : ashr_optab)
4147 : (unsignedp ? udiv_optab : sdiv_optab));
4148 optab2 = (op1_is_pow2 ? optab1
4149 : (unsignedp ? udivmod_optab : sdivmod_optab));
4151 FOR_EACH_MODE_FROM (compute_mode, mode)
4152 if (optab_handler (optab1, compute_mode) != CODE_FOR_nothing
4153 || optab_handler (optab2, compute_mode) != CODE_FOR_nothing)
4154 break;
4156 if (compute_mode == VOIDmode)
4157 FOR_EACH_MODE_FROM (compute_mode, mode)
4158 if (optab_libfunc (optab1, compute_mode)
4159 || optab_libfunc (optab2, compute_mode))
4160 break;
4162 /* If we still couldn't find a mode, use MODE, but expand_binop will
4163 probably die. */
4164 if (compute_mode == VOIDmode)
4165 compute_mode = mode;
4167 if (target && GET_MODE (target) == compute_mode)
4168 tquotient = target;
4169 else
4170 tquotient = gen_reg_rtx (compute_mode);
4172 #if 0
4173 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
4174 (mode), and thereby get better code when OP1 is a constant. Do that
4175 later. It will require going over all usages of SIZE below. */
4176 size = GET_MODE_BITSIZE (mode);
4177 #endif
4179 /* Only deduct something for a REM if the last divide done was
4180 for a different constant. Then set the constant of the last
4181 divide. */
4182 max_cost = (unsignedp
4183 ? udiv_cost (speed, compute_mode)
4184 : sdiv_cost (speed, compute_mode));
4185 if (rem_flag && ! (last_div_const != 0 && op1_is_constant
4186 && INTVAL (op1) == last_div_const))
4187 max_cost -= (mul_cost (speed, compute_mode)
4188 + add_cost (speed, compute_mode));
4190 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
4192 /* Now convert to the best mode to use. */
4193 if (compute_mode != mode)
4195 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
4196 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
4198 /* convert_modes may have placed op1 into a register, so we
4199 must recompute the following. */
4200 op1_is_constant = CONST_INT_P (op1);
4201 if (op1_is_constant)
4203 wide_int ext_op1 = rtx_mode_t (op1, compute_mode);
4204 op1_is_pow2 = (wi::popcount (ext_op1) == 1
4205 || (! unsignedp
4206 && wi::popcount (wi::neg (ext_op1)) == 1));
4208 else
4209 op1_is_pow2 = 0;
4212 /* If one of the operands is a volatile MEM, copy it into a register. */
4214 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
4215 op0 = force_reg (compute_mode, op0);
4216 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
4217 op1 = force_reg (compute_mode, op1);
4219 /* If we need the remainder or if OP1 is constant, we need to
4220 put OP0 in a register in case it has any queued subexpressions. */
4221 if (rem_flag || op1_is_constant)
4222 op0 = force_reg (compute_mode, op0);
4224 last = get_last_insn ();
4226 /* Promote floor rounding to trunc rounding for unsigned operations. */
4227 if (unsignedp)
4229 if (code == FLOOR_DIV_EXPR)
4230 code = TRUNC_DIV_EXPR;
4231 if (code == FLOOR_MOD_EXPR)
4232 code = TRUNC_MOD_EXPR;
4233 if (code == EXACT_DIV_EXPR && op1_is_pow2)
4234 code = TRUNC_DIV_EXPR;
4237 if (op1 != const0_rtx)
4238 switch (code)
4240 case TRUNC_MOD_EXPR:
4241 case TRUNC_DIV_EXPR:
4242 if (op1_is_constant)
4244 scalar_int_mode int_mode = as_a <scalar_int_mode> (compute_mode);
4245 int size = GET_MODE_BITSIZE (int_mode);
4246 if (unsignedp)
4248 unsigned HOST_WIDE_INT mh, ml;
4249 int pre_shift, post_shift;
4250 int dummy;
4251 wide_int wd = rtx_mode_t (op1, int_mode);
4252 unsigned HOST_WIDE_INT d = wd.to_uhwi ();
4254 if (wi::popcount (wd) == 1)
4256 pre_shift = floor_log2 (d);
4257 if (rem_flag)
4259 unsigned HOST_WIDE_INT mask
4260 = (HOST_WIDE_INT_1U << pre_shift) - 1;
4261 remainder
4262 = expand_binop (int_mode, and_optab, op0,
4263 gen_int_mode (mask, int_mode),
4264 remainder, 1,
4265 OPTAB_LIB_WIDEN);
4266 if (remainder)
4267 return gen_lowpart (mode, remainder);
4269 quotient = expand_shift (RSHIFT_EXPR, int_mode, op0,
4270 pre_shift, tquotient, 1);
4272 else if (size <= HOST_BITS_PER_WIDE_INT)
4274 if (d >= (HOST_WIDE_INT_1U << (size - 1)))
4276 /* Most significant bit of divisor is set; emit an scc
4277 insn. */
4278 quotient = emit_store_flag_force (tquotient, GEU, op0, op1,
4279 int_mode, 1, 1);
4281 else
4283 /* Find a suitable multiplier and right shift count
4284 instead of multiplying with D. */
4286 mh = choose_multiplier (d, size, size,
4287 &ml, &post_shift, &dummy);
4289 /* If the suggested multiplier is more than SIZE bits,
4290 we can do better for even divisors, using an
4291 initial right shift. */
4292 if (mh != 0 && (d & 1) == 0)
4294 pre_shift = ctz_or_zero (d);
4295 mh = choose_multiplier (d >> pre_shift, size,
4296 size - pre_shift,
4297 &ml, &post_shift, &dummy);
4298 gcc_assert (!mh);
4300 else
4301 pre_shift = 0;
4303 if (mh != 0)
4305 rtx t1, t2, t3, t4;
4307 if (post_shift - 1 >= BITS_PER_WORD)
4308 goto fail1;
4310 extra_cost
4311 = (shift_cost (speed, int_mode, post_shift - 1)
4312 + shift_cost (speed, int_mode, 1)
4313 + 2 * add_cost (speed, int_mode));
4314 t1 = expmed_mult_highpart
4315 (int_mode, op0, gen_int_mode (ml, int_mode),
4316 NULL_RTX, 1, max_cost - extra_cost);
4317 if (t1 == 0)
4318 goto fail1;
4319 t2 = force_operand (gen_rtx_MINUS (int_mode,
4320 op0, t1),
4321 NULL_RTX);
4322 t3 = expand_shift (RSHIFT_EXPR, int_mode,
4323 t2, 1, NULL_RTX, 1);
4324 t4 = force_operand (gen_rtx_PLUS (int_mode,
4325 t1, t3),
4326 NULL_RTX);
4327 quotient = expand_shift
4328 (RSHIFT_EXPR, int_mode, t4,
4329 post_shift - 1, tquotient, 1);
4331 else
4333 rtx t1, t2;
4335 if (pre_shift >= BITS_PER_WORD
4336 || post_shift >= BITS_PER_WORD)
4337 goto fail1;
4339 t1 = expand_shift
4340 (RSHIFT_EXPR, int_mode, op0,
4341 pre_shift, NULL_RTX, 1);
4342 extra_cost
4343 = (shift_cost (speed, int_mode, pre_shift)
4344 + shift_cost (speed, int_mode, post_shift));
4345 t2 = expmed_mult_highpart
4346 (int_mode, t1,
4347 gen_int_mode (ml, int_mode),
4348 NULL_RTX, 1, max_cost - extra_cost);
4349 if (t2 == 0)
4350 goto fail1;
4351 quotient = expand_shift
4352 (RSHIFT_EXPR, int_mode, t2,
4353 post_shift, tquotient, 1);
4357 else /* Too wide mode to use tricky code */
4358 break;
4360 insn = get_last_insn ();
4361 if (insn != last)
4362 set_dst_reg_note (insn, REG_EQUAL,
4363 gen_rtx_UDIV (int_mode, op0, op1),
4364 quotient);
4366 else /* TRUNC_DIV, signed */
4368 unsigned HOST_WIDE_INT ml;
4369 int lgup, post_shift;
4370 rtx mlr;
4371 HOST_WIDE_INT d = INTVAL (op1);
4372 unsigned HOST_WIDE_INT abs_d;
4374 /* Since d might be INT_MIN, we have to cast to
4375 unsigned HOST_WIDE_INT before negating to avoid
4376 undefined signed overflow. */
4377 abs_d = (d >= 0
4378 ? (unsigned HOST_WIDE_INT) d
4379 : - (unsigned HOST_WIDE_INT) d);
4381 /* n rem d = n rem -d */
4382 if (rem_flag && d < 0)
4384 d = abs_d;
4385 op1 = gen_int_mode (abs_d, int_mode);
4388 if (d == 1)
4389 quotient = op0;
4390 else if (d == -1)
4391 quotient = expand_unop (int_mode, neg_optab, op0,
4392 tquotient, 0);
4393 else if (size <= HOST_BITS_PER_WIDE_INT
4394 && abs_d == HOST_WIDE_INT_1U << (size - 1))
4396 /* This case is not handled correctly below. */
4397 quotient = emit_store_flag (tquotient, EQ, op0, op1,
4398 int_mode, 1, 1);
4399 if (quotient == 0)
4400 goto fail1;
4402 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
4403 && (size <= HOST_BITS_PER_WIDE_INT || d >= 0)
4404 && (rem_flag
4405 ? smod_pow2_cheap (speed, int_mode)
4406 : sdiv_pow2_cheap (speed, int_mode))
4407 /* We assume that cheap metric is true if the
4408 optab has an expander for this mode. */
4409 && ((optab_handler ((rem_flag ? smod_optab
4410 : sdiv_optab),
4411 int_mode)
4412 != CODE_FOR_nothing)
4413 || (optab_handler (sdivmod_optab, int_mode)
4414 != CODE_FOR_nothing)))
4416 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d)
4417 && (size <= HOST_BITS_PER_WIDE_INT
4418 || abs_d != (unsigned HOST_WIDE_INT) d))
4420 if (rem_flag)
4422 remainder = expand_smod_pow2 (int_mode, op0, d);
4423 if (remainder)
4424 return gen_lowpart (mode, remainder);
4427 if (sdiv_pow2_cheap (speed, int_mode)
4428 && ((optab_handler (sdiv_optab, int_mode)
4429 != CODE_FOR_nothing)
4430 || (optab_handler (sdivmod_optab, int_mode)
4431 != CODE_FOR_nothing)))
4432 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
4433 int_mode, op0,
4434 gen_int_mode (abs_d,
4435 int_mode),
4436 NULL_RTX, 0);
4437 else
4438 quotient = expand_sdiv_pow2 (int_mode, op0, abs_d);
4440 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4441 negate the quotient. */
4442 if (d < 0)
4444 insn = get_last_insn ();
4445 if (insn != last
4446 && abs_d < (HOST_WIDE_INT_1U
4447 << (HOST_BITS_PER_WIDE_INT - 1)))
4448 set_dst_reg_note (insn, REG_EQUAL,
4449 gen_rtx_DIV (int_mode, op0,
4450 gen_int_mode
4451 (abs_d,
4452 int_mode)),
4453 quotient);
4455 quotient = expand_unop (int_mode, neg_optab,
4456 quotient, quotient, 0);
4459 else if (size <= HOST_BITS_PER_WIDE_INT)
4461 choose_multiplier (abs_d, size, size - 1,
4462 &ml, &post_shift, &lgup);
4463 if (ml < HOST_WIDE_INT_1U << (size - 1))
4465 rtx t1, t2, t3;
4467 if (post_shift >= BITS_PER_WORD
4468 || size - 1 >= BITS_PER_WORD)
4469 goto fail1;
4471 extra_cost = (shift_cost (speed, int_mode, post_shift)
4472 + shift_cost (speed, int_mode, size - 1)
4473 + add_cost (speed, int_mode));
4474 t1 = expmed_mult_highpart
4475 (int_mode, op0, gen_int_mode (ml, int_mode),
4476 NULL_RTX, 0, max_cost - extra_cost);
4477 if (t1 == 0)
4478 goto fail1;
4479 t2 = expand_shift
4480 (RSHIFT_EXPR, int_mode, t1,
4481 post_shift, NULL_RTX, 0);
4482 t3 = expand_shift
4483 (RSHIFT_EXPR, int_mode, op0,
4484 size - 1, NULL_RTX, 0);
4485 if (d < 0)
4486 quotient
4487 = force_operand (gen_rtx_MINUS (int_mode, t3, t2),
4488 tquotient);
4489 else
4490 quotient
4491 = force_operand (gen_rtx_MINUS (int_mode, t2, t3),
4492 tquotient);
4494 else
4496 rtx t1, t2, t3, t4;
4498 if (post_shift >= BITS_PER_WORD
4499 || size - 1 >= BITS_PER_WORD)
4500 goto fail1;
4502 ml |= HOST_WIDE_INT_M1U << (size - 1);
4503 mlr = gen_int_mode (ml, int_mode);
4504 extra_cost = (shift_cost (speed, int_mode, post_shift)
4505 + shift_cost (speed, int_mode, size - 1)
4506 + 2 * add_cost (speed, int_mode));
4507 t1 = expmed_mult_highpart (int_mode, op0, mlr,
4508 NULL_RTX, 0,
4509 max_cost - extra_cost);
4510 if (t1 == 0)
4511 goto fail1;
4512 t2 = force_operand (gen_rtx_PLUS (int_mode, t1, op0),
4513 NULL_RTX);
4514 t3 = expand_shift
4515 (RSHIFT_EXPR, int_mode, t2,
4516 post_shift, NULL_RTX, 0);
4517 t4 = expand_shift
4518 (RSHIFT_EXPR, int_mode, op0,
4519 size - 1, NULL_RTX, 0);
4520 if (d < 0)
4521 quotient
4522 = force_operand (gen_rtx_MINUS (int_mode, t4, t3),
4523 tquotient);
4524 else
4525 quotient
4526 = force_operand (gen_rtx_MINUS (int_mode, t3, t4),
4527 tquotient);
4530 else /* Too wide mode to use tricky code */
4531 break;
4533 insn = get_last_insn ();
4534 if (insn != last)
4535 set_dst_reg_note (insn, REG_EQUAL,
4536 gen_rtx_DIV (int_mode, op0, op1),
4537 quotient);
4539 break;
4541 fail1:
4542 delete_insns_since (last);
4543 break;
4545 case FLOOR_DIV_EXPR:
4546 case FLOOR_MOD_EXPR:
4547 /* We will come here only for signed operations. */
4548 if (op1_is_constant && HWI_COMPUTABLE_MODE_P (compute_mode))
4550 scalar_int_mode int_mode = as_a <scalar_int_mode> (compute_mode);
4551 int size = GET_MODE_BITSIZE (int_mode);
4552 unsigned HOST_WIDE_INT mh, ml;
4553 int pre_shift, lgup, post_shift;
4554 HOST_WIDE_INT d = INTVAL (op1);
4556 if (d > 0)
4558 /* We could just as easily deal with negative constants here,
4559 but it does not seem worth the trouble for GCC 2.6. */
4560 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4562 pre_shift = floor_log2 (d);
4563 if (rem_flag)
4565 unsigned HOST_WIDE_INT mask
4566 = (HOST_WIDE_INT_1U << pre_shift) - 1;
4567 remainder = expand_binop
4568 (int_mode, and_optab, op0,
4569 gen_int_mode (mask, int_mode),
4570 remainder, 0, OPTAB_LIB_WIDEN);
4571 if (remainder)
4572 return gen_lowpart (mode, remainder);
4574 quotient = expand_shift
4575 (RSHIFT_EXPR, int_mode, op0,
4576 pre_shift, tquotient, 0);
4578 else
4580 rtx t1, t2, t3, t4;
4582 mh = choose_multiplier (d, size, size - 1,
4583 &ml, &post_shift, &lgup);
4584 gcc_assert (!mh);
4586 if (post_shift < BITS_PER_WORD
4587 && size - 1 < BITS_PER_WORD)
4589 t1 = expand_shift
4590 (RSHIFT_EXPR, int_mode, op0,
4591 size - 1, NULL_RTX, 0);
4592 t2 = expand_binop (int_mode, xor_optab, op0, t1,
4593 NULL_RTX, 0, OPTAB_WIDEN);
4594 extra_cost = (shift_cost (speed, int_mode, post_shift)
4595 + shift_cost (speed, int_mode, size - 1)
4596 + 2 * add_cost (speed, int_mode));
4597 t3 = expmed_mult_highpart
4598 (int_mode, t2, gen_int_mode (ml, int_mode),
4599 NULL_RTX, 1, max_cost - extra_cost);
4600 if (t3 != 0)
4602 t4 = expand_shift
4603 (RSHIFT_EXPR, int_mode, t3,
4604 post_shift, NULL_RTX, 1);
4605 quotient = expand_binop (int_mode, xor_optab,
4606 t4, t1, tquotient, 0,
4607 OPTAB_WIDEN);
4612 else
4614 rtx nsign, t1, t2, t3, t4;
4615 t1 = force_operand (gen_rtx_PLUS (int_mode,
4616 op0, constm1_rtx), NULL_RTX);
4617 t2 = expand_binop (int_mode, ior_optab, op0, t1, NULL_RTX,
4618 0, OPTAB_WIDEN);
4619 nsign = expand_shift (RSHIFT_EXPR, int_mode, t2,
4620 size - 1, NULL_RTX, 0);
4621 t3 = force_operand (gen_rtx_MINUS (int_mode, t1, nsign),
4622 NULL_RTX);
4623 t4 = expand_divmod (0, TRUNC_DIV_EXPR, int_mode, t3, op1,
4624 NULL_RTX, 0);
4625 if (t4)
4627 rtx t5;
4628 t5 = expand_unop (int_mode, one_cmpl_optab, nsign,
4629 NULL_RTX, 0);
4630 quotient = force_operand (gen_rtx_PLUS (int_mode, t4, t5),
4631 tquotient);
4636 if (quotient != 0)
4637 break;
4638 delete_insns_since (last);
4640 /* Try using an instruction that produces both the quotient and
4641 remainder, using truncation. We can easily compensate the quotient
4642 or remainder to get floor rounding, once we have the remainder.
4643 Notice that we compute also the final remainder value here,
4644 and return the result right away. */
4645 if (target == 0 || GET_MODE (target) != compute_mode)
4646 target = gen_reg_rtx (compute_mode);
4648 if (rem_flag)
4650 remainder
4651 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4652 quotient = gen_reg_rtx (compute_mode);
4654 else
4656 quotient
4657 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4658 remainder = gen_reg_rtx (compute_mode);
4661 if (expand_twoval_binop (sdivmod_optab, op0, op1,
4662 quotient, remainder, 0))
4664 /* This could be computed with a branch-less sequence.
4665 Save that for later. */
4666 rtx tem;
4667 rtx_code_label *label = gen_label_rtx ();
4668 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4669 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4670 NULL_RTX, 0, OPTAB_WIDEN);
4671 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4672 expand_dec (quotient, const1_rtx);
4673 expand_inc (remainder, op1);
4674 emit_label (label);
4675 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4678 /* No luck with division elimination or divmod. Have to do it
4679 by conditionally adjusting op0 *and* the result. */
4681 rtx_code_label *label1, *label2, *label3, *label4, *label5;
4682 rtx adjusted_op0;
4683 rtx tem;
4685 quotient = gen_reg_rtx (compute_mode);
4686 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4687 label1 = gen_label_rtx ();
4688 label2 = gen_label_rtx ();
4689 label3 = gen_label_rtx ();
4690 label4 = gen_label_rtx ();
4691 label5 = gen_label_rtx ();
4692 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4693 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4694 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4695 quotient, 0, OPTAB_LIB_WIDEN);
4696 if (tem != quotient)
4697 emit_move_insn (quotient, tem);
4698 emit_jump_insn (targetm.gen_jump (label5));
4699 emit_barrier ();
4700 emit_label (label1);
4701 expand_inc (adjusted_op0, const1_rtx);
4702 emit_jump_insn (targetm.gen_jump (label4));
4703 emit_barrier ();
4704 emit_label (label2);
4705 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4706 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4707 quotient, 0, OPTAB_LIB_WIDEN);
4708 if (tem != quotient)
4709 emit_move_insn (quotient, tem);
4710 emit_jump_insn (targetm.gen_jump (label5));
4711 emit_barrier ();
4712 emit_label (label3);
4713 expand_dec (adjusted_op0, const1_rtx);
4714 emit_label (label4);
4715 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4716 quotient, 0, OPTAB_LIB_WIDEN);
4717 if (tem != quotient)
4718 emit_move_insn (quotient, tem);
4719 expand_dec (quotient, const1_rtx);
4720 emit_label (label5);
4722 break;
4724 case CEIL_DIV_EXPR:
4725 case CEIL_MOD_EXPR:
4726 if (unsignedp)
4728 if (op1_is_constant
4729 && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4730 && (HWI_COMPUTABLE_MODE_P (compute_mode)
4731 || INTVAL (op1) >= 0))
4733 scalar_int_mode int_mode
4734 = as_a <scalar_int_mode> (compute_mode);
4735 rtx t1, t2, t3;
4736 unsigned HOST_WIDE_INT d = INTVAL (op1);
4737 t1 = expand_shift (RSHIFT_EXPR, int_mode, op0,
4738 floor_log2 (d), tquotient, 1);
4739 t2 = expand_binop (int_mode, and_optab, op0,
4740 gen_int_mode (d - 1, int_mode),
4741 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4742 t3 = gen_reg_rtx (int_mode);
4743 t3 = emit_store_flag (t3, NE, t2, const0_rtx, int_mode, 1, 1);
4744 if (t3 == 0)
4746 rtx_code_label *lab;
4747 lab = gen_label_rtx ();
4748 do_cmp_and_jump (t2, const0_rtx, EQ, int_mode, lab);
4749 expand_inc (t1, const1_rtx);
4750 emit_label (lab);
4751 quotient = t1;
4753 else
4754 quotient = force_operand (gen_rtx_PLUS (int_mode, t1, t3),
4755 tquotient);
4756 break;
4759 /* Try using an instruction that produces both the quotient and
4760 remainder, using truncation. We can easily compensate the
4761 quotient or remainder to get ceiling rounding, once we have the
4762 remainder. Notice that we compute also the final remainder
4763 value here, and return the result right away. */
4764 if (target == 0 || GET_MODE (target) != compute_mode)
4765 target = gen_reg_rtx (compute_mode);
4767 if (rem_flag)
4769 remainder = (REG_P (target)
4770 ? target : gen_reg_rtx (compute_mode));
4771 quotient = gen_reg_rtx (compute_mode);
4773 else
4775 quotient = (REG_P (target)
4776 ? target : gen_reg_rtx (compute_mode));
4777 remainder = gen_reg_rtx (compute_mode);
4780 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4781 remainder, 1))
4783 /* This could be computed with a branch-less sequence.
4784 Save that for later. */
4785 rtx_code_label *label = gen_label_rtx ();
4786 do_cmp_and_jump (remainder, const0_rtx, EQ,
4787 compute_mode, label);
4788 expand_inc (quotient, const1_rtx);
4789 expand_dec (remainder, op1);
4790 emit_label (label);
4791 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4794 /* No luck with division elimination or divmod. Have to do it
4795 by conditionally adjusting op0 *and* the result. */
4797 rtx_code_label *label1, *label2;
4798 rtx adjusted_op0, tem;
4800 quotient = gen_reg_rtx (compute_mode);
4801 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4802 label1 = gen_label_rtx ();
4803 label2 = gen_label_rtx ();
4804 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4805 compute_mode, label1);
4806 emit_move_insn (quotient, const0_rtx);
4807 emit_jump_insn (targetm.gen_jump (label2));
4808 emit_barrier ();
4809 emit_label (label1);
4810 expand_dec (adjusted_op0, const1_rtx);
4811 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4812 quotient, 1, OPTAB_LIB_WIDEN);
4813 if (tem != quotient)
4814 emit_move_insn (quotient, tem);
4815 expand_inc (quotient, const1_rtx);
4816 emit_label (label2);
4819 else /* signed */
4821 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4822 && INTVAL (op1) >= 0)
4824 /* This is extremely similar to the code for the unsigned case
4825 above. For 2.7 we should merge these variants, but for
4826 2.6.1 I don't want to touch the code for unsigned since that
4827 get used in C. The signed case will only be used by other
4828 languages (Ada). */
4830 rtx t1, t2, t3;
4831 unsigned HOST_WIDE_INT d = INTVAL (op1);
4832 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4833 floor_log2 (d), tquotient, 0);
4834 t2 = expand_binop (compute_mode, and_optab, op0,
4835 gen_int_mode (d - 1, compute_mode),
4836 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4837 t3 = gen_reg_rtx (compute_mode);
4838 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4839 compute_mode, 1, 1);
4840 if (t3 == 0)
4842 rtx_code_label *lab;
4843 lab = gen_label_rtx ();
4844 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4845 expand_inc (t1, const1_rtx);
4846 emit_label (lab);
4847 quotient = t1;
4849 else
4850 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4851 t1, t3),
4852 tquotient);
4853 break;
4856 /* Try using an instruction that produces both the quotient and
4857 remainder, using truncation. We can easily compensate the
4858 quotient or remainder to get ceiling rounding, once we have the
4859 remainder. Notice that we compute also the final remainder
4860 value here, and return the result right away. */
4861 if (target == 0 || GET_MODE (target) != compute_mode)
4862 target = gen_reg_rtx (compute_mode);
4863 if (rem_flag)
4865 remainder= (REG_P (target)
4866 ? target : gen_reg_rtx (compute_mode));
4867 quotient = gen_reg_rtx (compute_mode);
4869 else
4871 quotient = (REG_P (target)
4872 ? target : gen_reg_rtx (compute_mode));
4873 remainder = gen_reg_rtx (compute_mode);
4876 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4877 remainder, 0))
4879 /* This could be computed with a branch-less sequence.
4880 Save that for later. */
4881 rtx tem;
4882 rtx_code_label *label = gen_label_rtx ();
4883 do_cmp_and_jump (remainder, const0_rtx, EQ,
4884 compute_mode, label);
4885 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4886 NULL_RTX, 0, OPTAB_WIDEN);
4887 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4888 expand_inc (quotient, const1_rtx);
4889 expand_dec (remainder, op1);
4890 emit_label (label);
4891 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4894 /* No luck with division elimination or divmod. Have to do it
4895 by conditionally adjusting op0 *and* the result. */
4897 rtx_code_label *label1, *label2, *label3, *label4, *label5;
4898 rtx adjusted_op0;
4899 rtx tem;
4901 quotient = gen_reg_rtx (compute_mode);
4902 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4903 label1 = gen_label_rtx ();
4904 label2 = gen_label_rtx ();
4905 label3 = gen_label_rtx ();
4906 label4 = gen_label_rtx ();
4907 label5 = gen_label_rtx ();
4908 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4909 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4910 compute_mode, label1);
4911 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4912 quotient, 0, OPTAB_LIB_WIDEN);
4913 if (tem != quotient)
4914 emit_move_insn (quotient, tem);
4915 emit_jump_insn (targetm.gen_jump (label5));
4916 emit_barrier ();
4917 emit_label (label1);
4918 expand_dec (adjusted_op0, const1_rtx);
4919 emit_jump_insn (targetm.gen_jump (label4));
4920 emit_barrier ();
4921 emit_label (label2);
4922 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4923 compute_mode, label3);
4924 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4925 quotient, 0, OPTAB_LIB_WIDEN);
4926 if (tem != quotient)
4927 emit_move_insn (quotient, tem);
4928 emit_jump_insn (targetm.gen_jump (label5));
4929 emit_barrier ();
4930 emit_label (label3);
4931 expand_inc (adjusted_op0, const1_rtx);
4932 emit_label (label4);
4933 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4934 quotient, 0, OPTAB_LIB_WIDEN);
4935 if (tem != quotient)
4936 emit_move_insn (quotient, tem);
4937 expand_inc (quotient, const1_rtx);
4938 emit_label (label5);
4941 break;
4943 case EXACT_DIV_EXPR:
4944 if (op1_is_constant && HWI_COMPUTABLE_MODE_P (compute_mode))
4946 scalar_int_mode int_mode = as_a <scalar_int_mode> (compute_mode);
4947 int size = GET_MODE_BITSIZE (int_mode);
4948 HOST_WIDE_INT d = INTVAL (op1);
4949 unsigned HOST_WIDE_INT ml;
4950 int pre_shift;
4951 rtx t1;
4953 pre_shift = ctz_or_zero (d);
4954 ml = invert_mod2n (d >> pre_shift, size);
4955 t1 = expand_shift (RSHIFT_EXPR, int_mode, op0,
4956 pre_shift, NULL_RTX, unsignedp);
4957 quotient = expand_mult (int_mode, t1, gen_int_mode (ml, int_mode),
4958 NULL_RTX, 1);
4960 insn = get_last_insn ();
4961 set_dst_reg_note (insn, REG_EQUAL,
4962 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4963 int_mode, op0, op1),
4964 quotient);
4966 break;
4968 case ROUND_DIV_EXPR:
4969 case ROUND_MOD_EXPR:
4970 if (unsignedp)
4972 scalar_int_mode int_mode = as_a <scalar_int_mode> (compute_mode);
4973 rtx tem;
4974 rtx_code_label *label;
4975 label = gen_label_rtx ();
4976 quotient = gen_reg_rtx (int_mode);
4977 remainder = gen_reg_rtx (int_mode);
4978 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4980 rtx tem;
4981 quotient = expand_binop (int_mode, udiv_optab, op0, op1,
4982 quotient, 1, OPTAB_LIB_WIDEN);
4983 tem = expand_mult (int_mode, quotient, op1, NULL_RTX, 1);
4984 remainder = expand_binop (int_mode, sub_optab, op0, tem,
4985 remainder, 1, OPTAB_LIB_WIDEN);
4987 tem = plus_constant (int_mode, op1, -1);
4988 tem = expand_shift (RSHIFT_EXPR, int_mode, tem, 1, NULL_RTX, 1);
4989 do_cmp_and_jump (remainder, tem, LEU, int_mode, label);
4990 expand_inc (quotient, const1_rtx);
4991 expand_dec (remainder, op1);
4992 emit_label (label);
4994 else
4996 scalar_int_mode int_mode = as_a <scalar_int_mode> (compute_mode);
4997 int size = GET_MODE_BITSIZE (int_mode);
4998 rtx abs_rem, abs_op1, tem, mask;
4999 rtx_code_label *label;
5000 label = gen_label_rtx ();
5001 quotient = gen_reg_rtx (int_mode);
5002 remainder = gen_reg_rtx (int_mode);
5003 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
5005 rtx tem;
5006 quotient = expand_binop (int_mode, sdiv_optab, op0, op1,
5007 quotient, 0, OPTAB_LIB_WIDEN);
5008 tem = expand_mult (int_mode, quotient, op1, NULL_RTX, 0);
5009 remainder = expand_binop (int_mode, sub_optab, op0, tem,
5010 remainder, 0, OPTAB_LIB_WIDEN);
5012 abs_rem = expand_abs (int_mode, remainder, NULL_RTX, 1, 0);
5013 abs_op1 = expand_abs (int_mode, op1, NULL_RTX, 1, 0);
5014 tem = expand_shift (LSHIFT_EXPR, int_mode, abs_rem,
5015 1, NULL_RTX, 1);
5016 do_cmp_and_jump (tem, abs_op1, LTU, int_mode, label);
5017 tem = expand_binop (int_mode, xor_optab, op0, op1,
5018 NULL_RTX, 0, OPTAB_WIDEN);
5019 mask = expand_shift (RSHIFT_EXPR, int_mode, tem,
5020 size - 1, NULL_RTX, 0);
5021 tem = expand_binop (int_mode, xor_optab, mask, const1_rtx,
5022 NULL_RTX, 0, OPTAB_WIDEN);
5023 tem = expand_binop (int_mode, sub_optab, tem, mask,
5024 NULL_RTX, 0, OPTAB_WIDEN);
5025 expand_inc (quotient, tem);
5026 tem = expand_binop (int_mode, xor_optab, mask, op1,
5027 NULL_RTX, 0, OPTAB_WIDEN);
5028 tem = expand_binop (int_mode, sub_optab, tem, mask,
5029 NULL_RTX, 0, OPTAB_WIDEN);
5030 expand_dec (remainder, tem);
5031 emit_label (label);
5033 return gen_lowpart (mode, rem_flag ? remainder : quotient);
5035 default:
5036 gcc_unreachable ();
5039 if (quotient == 0)
5041 if (target && GET_MODE (target) != compute_mode)
5042 target = 0;
5044 if (rem_flag)
5046 /* Try to produce the remainder without producing the quotient.
5047 If we seem to have a divmod pattern that does not require widening,
5048 don't try widening here. We should really have a WIDEN argument
5049 to expand_twoval_binop, since what we'd really like to do here is
5050 1) try a mod insn in compute_mode
5051 2) try a divmod insn in compute_mode
5052 3) try a div insn in compute_mode and multiply-subtract to get
5053 remainder
5054 4) try the same things with widening allowed. */
5055 remainder
5056 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
5057 op0, op1, target,
5058 unsignedp,
5059 ((optab_handler (optab2, compute_mode)
5060 != CODE_FOR_nothing)
5061 ? OPTAB_DIRECT : OPTAB_WIDEN));
5062 if (remainder == 0)
5064 /* No luck there. Can we do remainder and divide at once
5065 without a library call? */
5066 remainder = gen_reg_rtx (compute_mode);
5067 if (! expand_twoval_binop ((unsignedp
5068 ? udivmod_optab
5069 : sdivmod_optab),
5070 op0, op1,
5071 NULL_RTX, remainder, unsignedp))
5072 remainder = 0;
5075 if (remainder)
5076 return gen_lowpart (mode, remainder);
5079 /* Produce the quotient. Try a quotient insn, but not a library call.
5080 If we have a divmod in this mode, use it in preference to widening
5081 the div (for this test we assume it will not fail). Note that optab2
5082 is set to the one of the two optabs that the call below will use. */
5083 quotient
5084 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
5085 op0, op1, rem_flag ? NULL_RTX : target,
5086 unsignedp,
5087 ((optab_handler (optab2, compute_mode)
5088 != CODE_FOR_nothing)
5089 ? OPTAB_DIRECT : OPTAB_WIDEN));
5091 if (quotient == 0)
5093 /* No luck there. Try a quotient-and-remainder insn,
5094 keeping the quotient alone. */
5095 quotient = gen_reg_rtx (compute_mode);
5096 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
5097 op0, op1,
5098 quotient, NULL_RTX, unsignedp))
5100 quotient = 0;
5101 if (! rem_flag)
5102 /* Still no luck. If we are not computing the remainder,
5103 use a library call for the quotient. */
5104 quotient = sign_expand_binop (compute_mode,
5105 udiv_optab, sdiv_optab,
5106 op0, op1, target,
5107 unsignedp, OPTAB_LIB_WIDEN);
5112 if (rem_flag)
5114 if (target && GET_MODE (target) != compute_mode)
5115 target = 0;
5117 if (quotient == 0)
5119 /* No divide instruction either. Use library for remainder. */
5120 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
5121 op0, op1, target,
5122 unsignedp, OPTAB_LIB_WIDEN);
5123 /* No remainder function. Try a quotient-and-remainder
5124 function, keeping the remainder. */
5125 if (!remainder)
5127 remainder = gen_reg_rtx (compute_mode);
5128 if (!expand_twoval_binop_libfunc
5129 (unsignedp ? udivmod_optab : sdivmod_optab,
5130 op0, op1,
5131 NULL_RTX, remainder,
5132 unsignedp ? UMOD : MOD))
5133 remainder = NULL_RTX;
5136 else
5138 /* We divided. Now finish doing X - Y * (X / Y). */
5139 remainder = expand_mult (compute_mode, quotient, op1,
5140 NULL_RTX, unsignedp);
5141 remainder = expand_binop (compute_mode, sub_optab, op0,
5142 remainder, target, unsignedp,
5143 OPTAB_LIB_WIDEN);
5147 return gen_lowpart (mode, rem_flag ? remainder : quotient);
5150 /* Return a tree node with data type TYPE, describing the value of X.
5151 Usually this is an VAR_DECL, if there is no obvious better choice.
5152 X may be an expression, however we only support those expressions
5153 generated by loop.c. */
5155 tree
5156 make_tree (tree type, rtx x)
5158 tree t;
5160 switch (GET_CODE (x))
5162 case CONST_INT:
5163 case CONST_WIDE_INT:
5164 t = wide_int_to_tree (type, rtx_mode_t (x, TYPE_MODE (type)));
5165 return t;
5167 case CONST_DOUBLE:
5168 STATIC_ASSERT (HOST_BITS_PER_WIDE_INT * 2 <= MAX_BITSIZE_MODE_ANY_INT);
5169 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
5170 t = wide_int_to_tree (type,
5171 wide_int::from_array (&CONST_DOUBLE_LOW (x), 2,
5172 HOST_BITS_PER_WIDE_INT * 2));
5173 else
5174 t = build_real (type, *CONST_DOUBLE_REAL_VALUE (x));
5176 return t;
5178 case CONST_VECTOR:
5180 int units = CONST_VECTOR_NUNITS (x);
5181 tree itype = TREE_TYPE (type);
5182 tree *elts;
5183 int i;
5185 /* Build a tree with vector elements. */
5186 elts = XALLOCAVEC (tree, units);
5187 for (i = units - 1; i >= 0; --i)
5189 rtx elt = CONST_VECTOR_ELT (x, i);
5190 elts[i] = make_tree (itype, elt);
5193 return build_vector (type, elts);
5196 case PLUS:
5197 return fold_build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5198 make_tree (type, XEXP (x, 1)));
5200 case MINUS:
5201 return fold_build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5202 make_tree (type, XEXP (x, 1)));
5204 case NEG:
5205 return fold_build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0)));
5207 case MULT:
5208 return fold_build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
5209 make_tree (type, XEXP (x, 1)));
5211 case ASHIFT:
5212 return fold_build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
5213 make_tree (type, XEXP (x, 1)));
5215 case LSHIFTRT:
5216 t = unsigned_type_for (type);
5217 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5218 make_tree (t, XEXP (x, 0)),
5219 make_tree (type, XEXP (x, 1))));
5221 case ASHIFTRT:
5222 t = signed_type_for (type);
5223 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5224 make_tree (t, XEXP (x, 0)),
5225 make_tree (type, XEXP (x, 1))));
5227 case DIV:
5228 if (TREE_CODE (type) != REAL_TYPE)
5229 t = signed_type_for (type);
5230 else
5231 t = type;
5233 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5234 make_tree (t, XEXP (x, 0)),
5235 make_tree (t, XEXP (x, 1))));
5236 case UDIV:
5237 t = unsigned_type_for (type);
5238 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5239 make_tree (t, XEXP (x, 0)),
5240 make_tree (t, XEXP (x, 1))));
5242 case SIGN_EXTEND:
5243 case ZERO_EXTEND:
5244 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
5245 GET_CODE (x) == ZERO_EXTEND);
5246 return fold_convert (type, make_tree (t, XEXP (x, 0)));
5248 case CONST:
5249 return make_tree (type, XEXP (x, 0));
5251 case SYMBOL_REF:
5252 t = SYMBOL_REF_DECL (x);
5253 if (t)
5254 return fold_convert (type, build_fold_addr_expr (t));
5255 /* fall through. */
5257 default:
5258 t = build_decl (RTL_LOCATION (x), VAR_DECL, NULL_TREE, type);
5260 /* If TYPE is a POINTER_TYPE, we might need to convert X from
5261 address mode to pointer mode. */
5262 if (POINTER_TYPE_P (type))
5263 x = convert_memory_address_addr_space
5264 (SCALAR_INT_TYPE_MODE (type), x, TYPE_ADDR_SPACE (TREE_TYPE (type)));
5266 /* Note that we do *not* use SET_DECL_RTL here, because we do not
5267 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
5268 t->decl_with_rtl.rtl = x;
5270 return t;
5274 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5275 and returning TARGET.
5277 If TARGET is 0, a pseudo-register or constant is returned. */
5280 expand_and (machine_mode mode, rtx op0, rtx op1, rtx target)
5282 rtx tem = 0;
5284 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
5285 tem = simplify_binary_operation (AND, mode, op0, op1);
5286 if (tem == 0)
5287 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
5289 if (target == 0)
5290 target = tem;
5291 else if (tem != target)
5292 emit_move_insn (target, tem);
5293 return target;
5296 /* Helper function for emit_store_flag. */
5298 emit_cstore (rtx target, enum insn_code icode, enum rtx_code code,
5299 machine_mode mode, machine_mode compare_mode,
5300 int unsignedp, rtx x, rtx y, int normalizep,
5301 machine_mode target_mode)
5303 struct expand_operand ops[4];
5304 rtx op0, comparison, subtarget;
5305 rtx_insn *last;
5306 scalar_int_mode result_mode = targetm.cstore_mode (icode);
5307 scalar_int_mode int_target_mode;
5309 last = get_last_insn ();
5310 x = prepare_operand (icode, x, 2, mode, compare_mode, unsignedp);
5311 y = prepare_operand (icode, y, 3, mode, compare_mode, unsignedp);
5312 if (!x || !y)
5314 delete_insns_since (last);
5315 return NULL_RTX;
5318 if (target_mode == VOIDmode)
5319 int_target_mode = result_mode;
5320 else
5321 int_target_mode = as_a <scalar_int_mode> (target_mode);
5322 if (!target)
5323 target = gen_reg_rtx (int_target_mode);
5325 comparison = gen_rtx_fmt_ee (code, result_mode, x, y);
5327 create_output_operand (&ops[0], optimize ? NULL_RTX : target, result_mode);
5328 create_fixed_operand (&ops[1], comparison);
5329 create_fixed_operand (&ops[2], x);
5330 create_fixed_operand (&ops[3], y);
5331 if (!maybe_expand_insn (icode, 4, ops))
5333 delete_insns_since (last);
5334 return NULL_RTX;
5336 subtarget = ops[0].value;
5338 /* If we are converting to a wider mode, first convert to
5339 INT_TARGET_MODE, then normalize. This produces better combining
5340 opportunities on machines that have a SIGN_EXTRACT when we are
5341 testing a single bit. This mostly benefits the 68k.
5343 If STORE_FLAG_VALUE does not have the sign bit set when
5344 interpreted in MODE, we can do this conversion as unsigned, which
5345 is usually more efficient. */
5346 if (GET_MODE_SIZE (int_target_mode) > GET_MODE_SIZE (result_mode))
5348 convert_move (target, subtarget,
5349 val_signbit_known_clear_p (result_mode,
5350 STORE_FLAG_VALUE));
5351 op0 = target;
5352 result_mode = int_target_mode;
5354 else
5355 op0 = subtarget;
5357 /* If we want to keep subexpressions around, don't reuse our last
5358 target. */
5359 if (optimize)
5360 subtarget = 0;
5362 /* Now normalize to the proper value in MODE. Sometimes we don't
5363 have to do anything. */
5364 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
5366 /* STORE_FLAG_VALUE might be the most negative number, so write
5367 the comparison this way to avoid a compiler-time warning. */
5368 else if (- normalizep == STORE_FLAG_VALUE)
5369 op0 = expand_unop (result_mode, neg_optab, op0, subtarget, 0);
5371 /* We don't want to use STORE_FLAG_VALUE < 0 below since this makes
5372 it hard to use a value of just the sign bit due to ANSI integer
5373 constant typing rules. */
5374 else if (val_signbit_known_set_p (result_mode, STORE_FLAG_VALUE))
5375 op0 = expand_shift (RSHIFT_EXPR, result_mode, op0,
5376 GET_MODE_BITSIZE (result_mode) - 1, subtarget,
5377 normalizep == 1);
5378 else
5380 gcc_assert (STORE_FLAG_VALUE & 1);
5382 op0 = expand_and (result_mode, op0, const1_rtx, subtarget);
5383 if (normalizep == -1)
5384 op0 = expand_unop (result_mode, neg_optab, op0, op0, 0);
5387 /* If we were converting to a smaller mode, do the conversion now. */
5388 if (int_target_mode != result_mode)
5390 convert_move (target, op0, 0);
5391 return target;
5393 else
5394 return op0;
5398 /* A subroutine of emit_store_flag only including "tricks" that do not
5399 need a recursive call. These are kept separate to avoid infinite
5400 loops. */
5402 static rtx
5403 emit_store_flag_1 (rtx target, enum rtx_code code, rtx op0, rtx op1,
5404 machine_mode mode, int unsignedp, int normalizep,
5405 machine_mode target_mode)
5407 rtx subtarget;
5408 enum insn_code icode;
5409 machine_mode compare_mode;
5410 enum mode_class mclass;
5411 enum rtx_code scode;
5413 if (unsignedp)
5414 code = unsigned_condition (code);
5415 scode = swap_condition (code);
5417 /* If one operand is constant, make it the second one. Only do this
5418 if the other operand is not constant as well. */
5420 if (swap_commutative_operands_p (op0, op1))
5422 std::swap (op0, op1);
5423 code = swap_condition (code);
5426 if (mode == VOIDmode)
5427 mode = GET_MODE (op0);
5429 /* For some comparisons with 1 and -1, we can convert this to
5430 comparisons with zero. This will often produce more opportunities for
5431 store-flag insns. */
5433 switch (code)
5435 case LT:
5436 if (op1 == const1_rtx)
5437 op1 = const0_rtx, code = LE;
5438 break;
5439 case LE:
5440 if (op1 == constm1_rtx)
5441 op1 = const0_rtx, code = LT;
5442 break;
5443 case GE:
5444 if (op1 == const1_rtx)
5445 op1 = const0_rtx, code = GT;
5446 break;
5447 case GT:
5448 if (op1 == constm1_rtx)
5449 op1 = const0_rtx, code = GE;
5450 break;
5451 case GEU:
5452 if (op1 == const1_rtx)
5453 op1 = const0_rtx, code = NE;
5454 break;
5455 case LTU:
5456 if (op1 == const1_rtx)
5457 op1 = const0_rtx, code = EQ;
5458 break;
5459 default:
5460 break;
5463 /* If we are comparing a double-word integer with zero or -1, we can
5464 convert the comparison into one involving a single word. */
5465 scalar_int_mode int_mode;
5466 if (is_int_mode (mode, &int_mode)
5467 && GET_MODE_BITSIZE (int_mode) == BITS_PER_WORD * 2
5468 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
5470 rtx tem;
5471 if ((code == EQ || code == NE)
5472 && (op1 == const0_rtx || op1 == constm1_rtx))
5474 rtx op00, op01;
5476 /* Do a logical OR or AND of the two words and compare the
5477 result. */
5478 op00 = simplify_gen_subreg (word_mode, op0, int_mode, 0);
5479 op01 = simplify_gen_subreg (word_mode, op0, int_mode, UNITS_PER_WORD);
5480 tem = expand_binop (word_mode,
5481 op1 == const0_rtx ? ior_optab : and_optab,
5482 op00, op01, NULL_RTX, unsignedp,
5483 OPTAB_DIRECT);
5485 if (tem != 0)
5486 tem = emit_store_flag (NULL_RTX, code, tem, op1, word_mode,
5487 unsignedp, normalizep);
5489 else if ((code == LT || code == GE) && op1 == const0_rtx)
5491 rtx op0h;
5493 /* If testing the sign bit, can just test on high word. */
5494 op0h = simplify_gen_subreg (word_mode, op0, int_mode,
5495 subreg_highpart_offset (word_mode,
5496 int_mode));
5497 tem = emit_store_flag (NULL_RTX, code, op0h, op1, word_mode,
5498 unsignedp, normalizep);
5500 else
5501 tem = NULL_RTX;
5503 if (tem)
5505 if (target_mode == VOIDmode || GET_MODE (tem) == target_mode)
5506 return tem;
5507 if (!target)
5508 target = gen_reg_rtx (target_mode);
5510 convert_move (target, tem,
5511 !val_signbit_known_set_p (word_mode,
5512 (normalizep ? normalizep
5513 : STORE_FLAG_VALUE)));
5514 return target;
5518 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5519 complement of A (for GE) and shifting the sign bit to the low bit. */
5520 if (op1 == const0_rtx && (code == LT || code == GE)
5521 && is_int_mode (mode, &int_mode)
5522 && (normalizep || STORE_FLAG_VALUE == 1
5523 || val_signbit_p (int_mode, STORE_FLAG_VALUE)))
5525 scalar_int_mode int_target_mode;
5526 subtarget = target;
5528 if (!target)
5529 int_target_mode = int_mode;
5530 else
5532 /* If the result is to be wider than OP0, it is best to convert it
5533 first. If it is to be narrower, it is *incorrect* to convert it
5534 first. */
5535 int_target_mode = as_a <scalar_int_mode> (target_mode);
5536 if (GET_MODE_SIZE (int_target_mode) > GET_MODE_SIZE (int_mode))
5538 op0 = convert_modes (int_target_mode, int_mode, op0, 0);
5539 int_mode = int_target_mode;
5543 if (int_target_mode != int_mode)
5544 subtarget = 0;
5546 if (code == GE)
5547 op0 = expand_unop (int_mode, one_cmpl_optab, op0,
5548 ((STORE_FLAG_VALUE == 1 || normalizep)
5549 ? 0 : subtarget), 0);
5551 if (STORE_FLAG_VALUE == 1 || normalizep)
5552 /* If we are supposed to produce a 0/1 value, we want to do
5553 a logical shift from the sign bit to the low-order bit; for
5554 a -1/0 value, we do an arithmetic shift. */
5555 op0 = expand_shift (RSHIFT_EXPR, int_mode, op0,
5556 GET_MODE_BITSIZE (int_mode) - 1,
5557 subtarget, normalizep != -1);
5559 if (int_mode != int_target_mode)
5560 op0 = convert_modes (int_target_mode, int_mode, op0, 0);
5562 return op0;
5565 mclass = GET_MODE_CLASS (mode);
5566 FOR_EACH_MODE_FROM (compare_mode, mode)
5568 machine_mode optab_mode = mclass == MODE_CC ? CCmode : compare_mode;
5569 icode = optab_handler (cstore_optab, optab_mode);
5570 if (icode != CODE_FOR_nothing)
5572 do_pending_stack_adjust ();
5573 rtx tem = emit_cstore (target, icode, code, mode, compare_mode,
5574 unsignedp, op0, op1, normalizep, target_mode);
5575 if (tem)
5576 return tem;
5578 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5580 tem = emit_cstore (target, icode, scode, mode, compare_mode,
5581 unsignedp, op1, op0, normalizep, target_mode);
5582 if (tem)
5583 return tem;
5585 break;
5589 return 0;
5592 /* Subroutine of emit_store_flag that handles cases in which the operands
5593 are scalar integers. SUBTARGET is the target to use for temporary
5594 operations and TRUEVAL is the value to store when the condition is
5595 true. All other arguments are as for emit_store_flag. */
5598 emit_store_flag_int (rtx target, rtx subtarget, enum rtx_code code, rtx op0,
5599 rtx op1, scalar_int_mode mode, int unsignedp,
5600 int normalizep, rtx trueval)
5602 machine_mode target_mode = target ? GET_MODE (target) : VOIDmode;
5603 rtx_insn *last = get_last_insn ();
5605 /* If this is an equality comparison of integers, we can try to exclusive-or
5606 (or subtract) the two operands and use a recursive call to try the
5607 comparison with zero. Don't do any of these cases if branches are
5608 very cheap. */
5610 if ((code == EQ || code == NE) && op1 != const0_rtx)
5612 rtx tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5613 OPTAB_WIDEN);
5615 if (tem == 0)
5616 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5617 OPTAB_WIDEN);
5618 if (tem != 0)
5619 tem = emit_store_flag (target, code, tem, const0_rtx,
5620 mode, unsignedp, normalizep);
5621 if (tem != 0)
5622 return tem;
5624 delete_insns_since (last);
5627 /* For integer comparisons, try the reverse comparison. However, for
5628 small X and if we'd have anyway to extend, implementing "X != 0"
5629 as "-(int)X >> 31" is still cheaper than inverting "(int)X == 0". */
5630 rtx_code rcode = reverse_condition (code);
5631 if (can_compare_p (rcode, mode, ccp_store_flag)
5632 && ! (optab_handler (cstore_optab, mode) == CODE_FOR_nothing
5633 && code == NE
5634 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
5635 && op1 == const0_rtx))
5637 int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
5638 || (STORE_FLAG_VALUE == -1 && normalizep == 1));
5640 /* Again, for the reverse comparison, use either an addition or a XOR. */
5641 if (want_add
5642 && rtx_cost (GEN_INT (normalizep), mode, PLUS, 1,
5643 optimize_insn_for_speed_p ()) == 0)
5645 rtx tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5646 STORE_FLAG_VALUE, target_mode);
5647 if (tem != 0)
5648 tem = expand_binop (target_mode, add_optab, tem,
5649 gen_int_mode (normalizep, target_mode),
5650 target, 0, OPTAB_WIDEN);
5651 if (tem != 0)
5652 return tem;
5654 else if (!want_add
5655 && rtx_cost (trueval, mode, XOR, 1,
5656 optimize_insn_for_speed_p ()) == 0)
5658 rtx tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5659 normalizep, target_mode);
5660 if (tem != 0)
5661 tem = expand_binop (target_mode, xor_optab, tem, trueval, target,
5662 INTVAL (trueval) >= 0, OPTAB_WIDEN);
5663 if (tem != 0)
5664 return tem;
5667 delete_insns_since (last);
5670 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5671 the constant zero. Reject all other comparisons at this point. Only
5672 do LE and GT if branches are expensive since they are expensive on
5673 2-operand machines. */
5675 if (op1 != const0_rtx
5676 || (code != EQ && code != NE
5677 && (BRANCH_COST (optimize_insn_for_speed_p (),
5678 false) <= 1 || (code != LE && code != GT))))
5679 return 0;
5681 /* Try to put the result of the comparison in the sign bit. Assume we can't
5682 do the necessary operation below. */
5684 rtx tem = 0;
5686 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5687 the sign bit set. */
5689 if (code == LE)
5691 /* This is destructive, so SUBTARGET can't be OP0. */
5692 if (rtx_equal_p (subtarget, op0))
5693 subtarget = 0;
5695 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5696 OPTAB_WIDEN);
5697 if (tem)
5698 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5699 OPTAB_WIDEN);
5702 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5703 number of bits in the mode of OP0, minus one. */
5705 if (code == GT)
5707 if (rtx_equal_p (subtarget, op0))
5708 subtarget = 0;
5710 tem = maybe_expand_shift (RSHIFT_EXPR, mode, op0,
5711 GET_MODE_BITSIZE (mode) - 1,
5712 subtarget, 0);
5713 if (tem)
5714 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5715 OPTAB_WIDEN);
5718 if (code == EQ || code == NE)
5720 /* For EQ or NE, one way to do the comparison is to apply an operation
5721 that converts the operand into a positive number if it is nonzero
5722 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5723 for NE we negate. This puts the result in the sign bit. Then we
5724 normalize with a shift, if needed.
5726 Two operations that can do the above actions are ABS and FFS, so try
5727 them. If that doesn't work, and MODE is smaller than a full word,
5728 we can use zero-extension to the wider mode (an unsigned conversion)
5729 as the operation. */
5731 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5732 that is compensated by the subsequent overflow when subtracting
5733 one / negating. */
5735 if (optab_handler (abs_optab, mode) != CODE_FOR_nothing)
5736 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5737 else if (optab_handler (ffs_optab, mode) != CODE_FOR_nothing)
5738 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5739 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5741 tem = convert_modes (word_mode, mode, op0, 1);
5742 mode = word_mode;
5745 if (tem != 0)
5747 if (code == EQ)
5748 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5749 0, OPTAB_WIDEN);
5750 else
5751 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5754 /* If we couldn't do it that way, for NE we can "or" the two's complement
5755 of the value with itself. For EQ, we take the one's complement of
5756 that "or", which is an extra insn, so we only handle EQ if branches
5757 are expensive. */
5759 if (tem == 0
5760 && (code == NE
5761 || BRANCH_COST (optimize_insn_for_speed_p (),
5762 false) > 1))
5764 if (rtx_equal_p (subtarget, op0))
5765 subtarget = 0;
5767 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5768 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5769 OPTAB_WIDEN);
5771 if (tem && code == EQ)
5772 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5776 if (tem && normalizep)
5777 tem = maybe_expand_shift (RSHIFT_EXPR, mode, tem,
5778 GET_MODE_BITSIZE (mode) - 1,
5779 subtarget, normalizep == 1);
5781 if (tem)
5783 if (!target)
5785 else if (GET_MODE (tem) != target_mode)
5787 convert_move (target, tem, 0);
5788 tem = target;
5790 else if (!subtarget)
5792 emit_move_insn (target, tem);
5793 tem = target;
5796 else
5797 delete_insns_since (last);
5799 return tem;
5802 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5803 and storing in TARGET. Normally return TARGET.
5804 Return 0 if that cannot be done.
5806 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5807 it is VOIDmode, they cannot both be CONST_INT.
5809 UNSIGNEDP is for the case where we have to widen the operands
5810 to perform the operation. It says to use zero-extension.
5812 NORMALIZEP is 1 if we should convert the result to be either zero
5813 or one. Normalize is -1 if we should convert the result to be
5814 either zero or -1. If NORMALIZEP is zero, the result will be left
5815 "raw" out of the scc insn. */
5818 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
5819 machine_mode mode, int unsignedp, int normalizep)
5821 machine_mode target_mode = target ? GET_MODE (target) : VOIDmode;
5822 enum rtx_code rcode;
5823 rtx subtarget;
5824 rtx tem, trueval;
5825 rtx_insn *last;
5827 /* If we compare constants, we shouldn't use a store-flag operation,
5828 but a constant load. We can get there via the vanilla route that
5829 usually generates a compare-branch sequence, but will in this case
5830 fold the comparison to a constant, and thus elide the branch. */
5831 if (CONSTANT_P (op0) && CONSTANT_P (op1))
5832 return NULL_RTX;
5834 tem = emit_store_flag_1 (target, code, op0, op1, mode, unsignedp, normalizep,
5835 target_mode);
5836 if (tem)
5837 return tem;
5839 /* If we reached here, we can't do this with a scc insn, however there
5840 are some comparisons that can be done in other ways. Don't do any
5841 of these cases if branches are very cheap. */
5842 if (BRANCH_COST (optimize_insn_for_speed_p (), false) == 0)
5843 return 0;
5845 /* See what we need to return. We can only return a 1, -1, or the
5846 sign bit. */
5848 if (normalizep == 0)
5850 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5851 normalizep = STORE_FLAG_VALUE;
5853 else if (val_signbit_p (mode, STORE_FLAG_VALUE))
5855 else
5856 return 0;
5859 last = get_last_insn ();
5861 /* If optimizing, use different pseudo registers for each insn, instead
5862 of reusing the same pseudo. This leads to better CSE, but slows
5863 down the compiler, since there are more pseudos. */
5864 subtarget = (!optimize
5865 && (target_mode == mode)) ? target : NULL_RTX;
5866 trueval = GEN_INT (normalizep ? normalizep : STORE_FLAG_VALUE);
5868 /* For floating-point comparisons, try the reverse comparison or try
5869 changing the "orderedness" of the comparison. */
5870 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5872 enum rtx_code first_code;
5873 bool and_them;
5875 rcode = reverse_condition_maybe_unordered (code);
5876 if (can_compare_p (rcode, mode, ccp_store_flag)
5877 && (code == ORDERED || code == UNORDERED
5878 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
5879 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
5881 int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
5882 || (STORE_FLAG_VALUE == -1 && normalizep == 1));
5884 /* For the reverse comparison, use either an addition or a XOR. */
5885 if (want_add
5886 && rtx_cost (GEN_INT (normalizep), mode, PLUS, 1,
5887 optimize_insn_for_speed_p ()) == 0)
5889 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5890 STORE_FLAG_VALUE, target_mode);
5891 if (tem)
5892 return expand_binop (target_mode, add_optab, tem,
5893 gen_int_mode (normalizep, target_mode),
5894 target, 0, OPTAB_WIDEN);
5896 else if (!want_add
5897 && rtx_cost (trueval, mode, XOR, 1,
5898 optimize_insn_for_speed_p ()) == 0)
5900 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5901 normalizep, target_mode);
5902 if (tem)
5903 return expand_binop (target_mode, xor_optab, tem, trueval,
5904 target, INTVAL (trueval) >= 0,
5905 OPTAB_WIDEN);
5909 delete_insns_since (last);
5911 /* Cannot split ORDERED and UNORDERED, only try the above trick. */
5912 if (code == ORDERED || code == UNORDERED)
5913 return 0;
5915 and_them = split_comparison (code, mode, &first_code, &code);
5917 /* If there are no NaNs, the first comparison should always fall through.
5918 Effectively change the comparison to the other one. */
5919 if (!HONOR_NANS (mode))
5921 gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
5922 return emit_store_flag_1 (target, code, op0, op1, mode, 0, normalizep,
5923 target_mode);
5926 if (!HAVE_conditional_move)
5927 return 0;
5929 /* Try using a setcc instruction for ORDERED/UNORDERED, followed by a
5930 conditional move. */
5931 tem = emit_store_flag_1 (subtarget, first_code, op0, op1, mode, 0,
5932 normalizep, target_mode);
5933 if (tem == 0)
5934 return 0;
5936 if (and_them)
5937 tem = emit_conditional_move (target, code, op0, op1, mode,
5938 tem, const0_rtx, GET_MODE (tem), 0);
5939 else
5940 tem = emit_conditional_move (target, code, op0, op1, mode,
5941 trueval, tem, GET_MODE (tem), 0);
5943 if (tem == 0)
5944 delete_insns_since (last);
5945 return tem;
5948 /* The remaining tricks only apply to integer comparisons. */
5950 scalar_int_mode int_mode;
5951 if (is_int_mode (mode, &int_mode))
5952 return emit_store_flag_int (target, subtarget, code, op0, op1, int_mode,
5953 unsignedp, normalizep, trueval);
5955 return 0;
5958 /* Like emit_store_flag, but always succeeds. */
5961 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5962 machine_mode mode, int unsignedp, int normalizep)
5964 rtx tem;
5965 rtx_code_label *label;
5966 rtx trueval, falseval;
5968 /* First see if emit_store_flag can do the job. */
5969 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5970 if (tem != 0)
5971 return tem;
5973 if (!target)
5974 target = gen_reg_rtx (word_mode);
5976 /* If this failed, we have to do this with set/compare/jump/set code.
5977 For foo != 0, if foo is in OP0, just replace it with 1 if nonzero. */
5978 trueval = normalizep ? GEN_INT (normalizep) : const1_rtx;
5979 if (code == NE
5980 && GET_MODE_CLASS (mode) == MODE_INT
5981 && REG_P (target)
5982 && op0 == target
5983 && op1 == const0_rtx)
5985 label = gen_label_rtx ();
5986 do_compare_rtx_and_jump (target, const0_rtx, EQ, unsignedp, mode,
5987 NULL_RTX, NULL, label,
5988 profile_probability::uninitialized ());
5989 emit_move_insn (target, trueval);
5990 emit_label (label);
5991 return target;
5994 if (!REG_P (target)
5995 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5996 target = gen_reg_rtx (GET_MODE (target));
5998 /* Jump in the right direction if the target cannot implement CODE
5999 but can jump on its reverse condition. */
6000 falseval = const0_rtx;
6001 if (! can_compare_p (code, mode, ccp_jump)
6002 && (! FLOAT_MODE_P (mode)
6003 || code == ORDERED || code == UNORDERED
6004 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
6005 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
6007 enum rtx_code rcode;
6008 if (FLOAT_MODE_P (mode))
6009 rcode = reverse_condition_maybe_unordered (code);
6010 else
6011 rcode = reverse_condition (code);
6013 /* Canonicalize to UNORDERED for the libcall. */
6014 if (can_compare_p (rcode, mode, ccp_jump)
6015 || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
6017 falseval = trueval;
6018 trueval = const0_rtx;
6019 code = rcode;
6023 emit_move_insn (target, trueval);
6024 label = gen_label_rtx ();
6025 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX, NULL,
6026 label, profile_probability::uninitialized ());
6028 emit_move_insn (target, falseval);
6029 emit_label (label);
6031 return target;
6034 /* Perform possibly multi-word comparison and conditional jump to LABEL
6035 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE. This is
6036 now a thin wrapper around do_compare_rtx_and_jump. */
6038 static void
6039 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, machine_mode mode,
6040 rtx_code_label *label)
6042 int unsignedp = (op == LTU || op == LEU || op == GTU || op == GEU);
6043 do_compare_rtx_and_jump (arg1, arg2, op, unsignedp, mode, NULL_RTX,
6044 NULL, label, profile_probability::uninitialized ());