1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987-2015 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
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
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/>. */
24 #include "coretypes.h"
30 #include "diagnostic-core.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
36 #include "insn-config.h"
45 #include "insn-codes.h"
48 #include "langhooks.h"
51 struct target_expmed default_target_expmed
;
53 struct target_expmed
*this_target_expmed
= &default_target_expmed
;
56 static void store_fixed_bit_field (rtx
, unsigned HOST_WIDE_INT
,
57 unsigned HOST_WIDE_INT
,
58 unsigned HOST_WIDE_INT
,
59 unsigned HOST_WIDE_INT
,
61 static void store_fixed_bit_field_1 (rtx
, unsigned HOST_WIDE_INT
,
62 unsigned HOST_WIDE_INT
,
64 static void store_split_bit_field (rtx
, unsigned HOST_WIDE_INT
,
65 unsigned HOST_WIDE_INT
,
66 unsigned HOST_WIDE_INT
,
67 unsigned HOST_WIDE_INT
,
69 static rtx
extract_fixed_bit_field (machine_mode
, rtx
,
70 unsigned HOST_WIDE_INT
,
71 unsigned HOST_WIDE_INT
, rtx
, int);
72 static rtx
extract_fixed_bit_field_1 (machine_mode
, rtx
,
73 unsigned HOST_WIDE_INT
,
74 unsigned HOST_WIDE_INT
, rtx
, int);
75 static rtx
lshift_value (machine_mode
, unsigned HOST_WIDE_INT
, int);
76 static rtx
extract_split_bit_field (rtx
, unsigned HOST_WIDE_INT
,
77 unsigned HOST_WIDE_INT
, int);
78 static void do_cmp_and_jump (rtx
, rtx
, enum rtx_code
, machine_mode
, rtx_code_label
*);
79 static rtx
expand_smod_pow2 (machine_mode
, rtx
, HOST_WIDE_INT
);
80 static rtx
expand_sdiv_pow2 (machine_mode
, rtx
, HOST_WIDE_INT
);
82 /* Return a constant integer mask value of mode MODE with BITSIZE ones
83 followed by BITPOS zeros, or the complement of that if COMPLEMENT.
84 The mask is truncated if necessary to the width of mode MODE. The
85 mask is zero-extended if BITSIZE+BITPOS is too small for MODE. */
88 mask_rtx (machine_mode mode
, int bitpos
, int bitsize
, bool complement
)
90 return immed_wide_int_const
91 (wi::shifted_mask (bitpos
, bitsize
, complement
,
92 GET_MODE_PRECISION (mode
)), mode
);
95 /* Test whether a value is zero of a power of two. */
96 #define EXACT_POWER_OF_2_OR_ZERO_P(x) \
97 (((x) & ((x) - (unsigned HOST_WIDE_INT) 1)) == 0)
99 struct init_expmed_rtl
120 rtx pow2
[MAX_BITS_PER_WORD
];
121 rtx cint
[MAX_BITS_PER_WORD
];
125 init_expmed_one_conv (struct init_expmed_rtl
*all
, machine_mode to_mode
,
126 machine_mode from_mode
, bool speed
)
128 int to_size
, from_size
;
131 to_size
= GET_MODE_PRECISION (to_mode
);
132 from_size
= GET_MODE_PRECISION (from_mode
);
134 /* Most partial integers have a precision less than the "full"
135 integer it requires for storage. In case one doesn't, for
136 comparison purposes here, reduce the bit size by one in that
138 if (GET_MODE_CLASS (to_mode
) == MODE_PARTIAL_INT
139 && exact_log2 (to_size
) != -1)
141 if (GET_MODE_CLASS (from_mode
) == MODE_PARTIAL_INT
142 && exact_log2 (from_size
) != -1)
145 /* Assume cost of zero-extend and sign-extend is the same. */
146 which
= (to_size
< from_size
? all
->trunc
: all
->zext
);
148 PUT_MODE (all
->reg
, from_mode
);
149 set_convert_cost (to_mode
, from_mode
, speed
,
150 set_src_cost (which
, to_mode
, speed
));
154 init_expmed_one_mode (struct init_expmed_rtl
*all
,
155 machine_mode mode
, int speed
)
157 int m
, n
, mode_bitsize
;
158 machine_mode mode_from
;
160 mode_bitsize
= GET_MODE_UNIT_BITSIZE (mode
);
162 PUT_MODE (all
->reg
, mode
);
163 PUT_MODE (all
->plus
, mode
);
164 PUT_MODE (all
->neg
, mode
);
165 PUT_MODE (all
->mult
, mode
);
166 PUT_MODE (all
->sdiv
, mode
);
167 PUT_MODE (all
->udiv
, mode
);
168 PUT_MODE (all
->sdiv_32
, mode
);
169 PUT_MODE (all
->smod_32
, mode
);
170 PUT_MODE (all
->wide_trunc
, mode
);
171 PUT_MODE (all
->shift
, mode
);
172 PUT_MODE (all
->shift_mult
, mode
);
173 PUT_MODE (all
->shift_add
, mode
);
174 PUT_MODE (all
->shift_sub0
, mode
);
175 PUT_MODE (all
->shift_sub1
, mode
);
176 PUT_MODE (all
->zext
, mode
);
177 PUT_MODE (all
->trunc
, mode
);
179 set_add_cost (speed
, mode
, set_src_cost (all
->plus
, mode
, speed
));
180 set_neg_cost (speed
, mode
, set_src_cost (all
->neg
, mode
, speed
));
181 set_mul_cost (speed
, mode
, set_src_cost (all
->mult
, mode
, speed
));
182 set_sdiv_cost (speed
, mode
, set_src_cost (all
->sdiv
, mode
, speed
));
183 set_udiv_cost (speed
, mode
, set_src_cost (all
->udiv
, mode
, speed
));
185 set_sdiv_pow2_cheap (speed
, mode
, (set_src_cost (all
->sdiv_32
, mode
, speed
)
186 <= 2 * add_cost (speed
, mode
)));
187 set_smod_pow2_cheap (speed
, mode
, (set_src_cost (all
->smod_32
, mode
, speed
)
188 <= 4 * add_cost (speed
, mode
)));
190 set_shift_cost (speed
, mode
, 0, 0);
192 int cost
= add_cost (speed
, mode
);
193 set_shiftadd_cost (speed
, mode
, 0, cost
);
194 set_shiftsub0_cost (speed
, mode
, 0, cost
);
195 set_shiftsub1_cost (speed
, mode
, 0, cost
);
198 n
= MIN (MAX_BITS_PER_WORD
, mode_bitsize
);
199 for (m
= 1; m
< n
; m
++)
201 XEXP (all
->shift
, 1) = all
->cint
[m
];
202 XEXP (all
->shift_mult
, 1) = all
->pow2
[m
];
204 set_shift_cost (speed
, mode
, m
, set_src_cost (all
->shift
, mode
, speed
));
205 set_shiftadd_cost (speed
, mode
, m
, set_src_cost (all
->shift_add
, mode
,
207 set_shiftsub0_cost (speed
, mode
, m
, set_src_cost (all
->shift_sub0
, mode
,
209 set_shiftsub1_cost (speed
, mode
, m
, set_src_cost (all
->shift_sub1
, mode
,
213 if (SCALAR_INT_MODE_P (mode
))
215 for (mode_from
= MIN_MODE_INT
; mode_from
<= MAX_MODE_INT
;
216 mode_from
= (machine_mode
)(mode_from
+ 1))
217 init_expmed_one_conv (all
, mode
, mode_from
, speed
);
219 if (GET_MODE_CLASS (mode
) == MODE_INT
)
221 machine_mode wider_mode
= GET_MODE_WIDER_MODE (mode
);
222 if (wider_mode
!= VOIDmode
)
224 PUT_MODE (all
->zext
, wider_mode
);
225 PUT_MODE (all
->wide_mult
, wider_mode
);
226 PUT_MODE (all
->wide_lshr
, wider_mode
);
227 XEXP (all
->wide_lshr
, 1) = GEN_INT (mode_bitsize
);
229 set_mul_widen_cost (speed
, wider_mode
,
230 set_src_cost (all
->wide_mult
, wider_mode
, speed
));
231 set_mul_highpart_cost (speed
, mode
,
232 set_src_cost (all
->wide_trunc
, mode
, speed
));
240 struct init_expmed_rtl all
;
241 machine_mode mode
= QImode
;
244 memset (&all
, 0, sizeof all
);
245 for (m
= 1; m
< MAX_BITS_PER_WORD
; m
++)
247 all
.pow2
[m
] = GEN_INT ((HOST_WIDE_INT
) 1 << m
);
248 all
.cint
[m
] = GEN_INT (m
);
251 /* Avoid using hard regs in ways which may be unsupported. */
252 all
.reg
= gen_raw_REG (mode
, LAST_VIRTUAL_REGISTER
+ 1);
253 all
.plus
= gen_rtx_PLUS (mode
, all
.reg
, all
.reg
);
254 all
.neg
= gen_rtx_NEG (mode
, all
.reg
);
255 all
.mult
= gen_rtx_MULT (mode
, all
.reg
, all
.reg
);
256 all
.sdiv
= gen_rtx_DIV (mode
, all
.reg
, all
.reg
);
257 all
.udiv
= gen_rtx_UDIV (mode
, all
.reg
, all
.reg
);
258 all
.sdiv_32
= gen_rtx_DIV (mode
, all
.reg
, all
.pow2
[5]);
259 all
.smod_32
= gen_rtx_MOD (mode
, all
.reg
, all
.pow2
[5]);
260 all
.zext
= gen_rtx_ZERO_EXTEND (mode
, all
.reg
);
261 all
.wide_mult
= gen_rtx_MULT (mode
, all
.zext
, all
.zext
);
262 all
.wide_lshr
= gen_rtx_LSHIFTRT (mode
, all
.wide_mult
, all
.reg
);
263 all
.wide_trunc
= gen_rtx_TRUNCATE (mode
, all
.wide_lshr
);
264 all
.shift
= gen_rtx_ASHIFT (mode
, all
.reg
, all
.reg
);
265 all
.shift_mult
= gen_rtx_MULT (mode
, all
.reg
, all
.reg
);
266 all
.shift_add
= gen_rtx_PLUS (mode
, all
.shift_mult
, all
.reg
);
267 all
.shift_sub0
= gen_rtx_MINUS (mode
, all
.shift_mult
, all
.reg
);
268 all
.shift_sub1
= gen_rtx_MINUS (mode
, all
.reg
, all
.shift_mult
);
269 all
.trunc
= gen_rtx_TRUNCATE (mode
, all
.reg
);
271 for (speed
= 0; speed
< 2; speed
++)
273 crtl
->maybe_hot_insn_p
= speed
;
274 set_zero_cost (speed
, set_src_cost (const0_rtx
, mode
, speed
));
276 for (mode
= MIN_MODE_INT
; mode
<= MAX_MODE_INT
;
277 mode
= (machine_mode
)(mode
+ 1))
278 init_expmed_one_mode (&all
, mode
, speed
);
280 if (MIN_MODE_PARTIAL_INT
!= VOIDmode
)
281 for (mode
= MIN_MODE_PARTIAL_INT
; mode
<= MAX_MODE_PARTIAL_INT
;
282 mode
= (machine_mode
)(mode
+ 1))
283 init_expmed_one_mode (&all
, mode
, speed
);
285 if (MIN_MODE_VECTOR_INT
!= VOIDmode
)
286 for (mode
= MIN_MODE_VECTOR_INT
; mode
<= MAX_MODE_VECTOR_INT
;
287 mode
= (machine_mode
)(mode
+ 1))
288 init_expmed_one_mode (&all
, mode
, speed
);
291 if (alg_hash_used_p ())
293 struct alg_hash_entry
*p
= alg_hash_entry_ptr (0);
294 memset (p
, 0, sizeof (*p
) * NUM_ALG_HASH_ENTRIES
);
297 set_alg_hash_used_p (true);
298 default_rtl_profile ();
300 ggc_free (all
.trunc
);
301 ggc_free (all
.shift_sub1
);
302 ggc_free (all
.shift_sub0
);
303 ggc_free (all
.shift_add
);
304 ggc_free (all
.shift_mult
);
305 ggc_free (all
.shift
);
306 ggc_free (all
.wide_trunc
);
307 ggc_free (all
.wide_lshr
);
308 ggc_free (all
.wide_mult
);
310 ggc_free (all
.smod_32
);
311 ggc_free (all
.sdiv_32
);
320 /* Return an rtx representing minus the value of X.
321 MODE is the intended mode of the result,
322 useful if X is a CONST_INT. */
325 negate_rtx (machine_mode mode
, rtx x
)
327 rtx result
= simplify_unary_operation (NEG
, mode
, x
, mode
);
330 result
= expand_unop (mode
, neg_optab
, x
, NULL_RTX
, 0);
335 /* Adjust bitfield memory MEM so that it points to the first unit of mode
336 MODE that contains a bitfield of size BITSIZE at bit position BITNUM.
337 If MODE is BLKmode, return a reference to every byte in the bitfield.
338 Set *NEW_BITNUM to the bit position of the field within the new memory. */
341 narrow_bit_field_mem (rtx mem
, machine_mode mode
,
342 unsigned HOST_WIDE_INT bitsize
,
343 unsigned HOST_WIDE_INT bitnum
,
344 unsigned HOST_WIDE_INT
*new_bitnum
)
348 *new_bitnum
= bitnum
% BITS_PER_UNIT
;
349 HOST_WIDE_INT offset
= bitnum
/ BITS_PER_UNIT
;
350 HOST_WIDE_INT size
= ((*new_bitnum
+ bitsize
+ BITS_PER_UNIT
- 1)
352 return adjust_bitfield_address_size (mem
, mode
, offset
, size
);
356 unsigned int unit
= GET_MODE_BITSIZE (mode
);
357 *new_bitnum
= bitnum
% unit
;
358 HOST_WIDE_INT offset
= (bitnum
- *new_bitnum
) / BITS_PER_UNIT
;
359 return adjust_bitfield_address (mem
, mode
, offset
);
363 /* The caller wants to perform insertion or extraction PATTERN on a
364 bitfield of size BITSIZE at BITNUM bits into memory operand OP0.
365 BITREGION_START and BITREGION_END are as for store_bit_field
366 and FIELDMODE is the natural mode of the field.
368 Search for a mode that is compatible with the memory access
369 restrictions and (where applicable) with a register insertion or
370 extraction. Return the new memory on success, storing the adjusted
371 bit position in *NEW_BITNUM. Return null otherwise. */
374 adjust_bit_field_mem_for_reg (enum extraction_pattern pattern
,
375 rtx op0
, HOST_WIDE_INT bitsize
,
376 HOST_WIDE_INT bitnum
,
377 unsigned HOST_WIDE_INT bitregion_start
,
378 unsigned HOST_WIDE_INT bitregion_end
,
379 machine_mode fieldmode
,
380 unsigned HOST_WIDE_INT
*new_bitnum
)
382 bit_field_mode_iterator
iter (bitsize
, bitnum
, bitregion_start
,
383 bitregion_end
, MEM_ALIGN (op0
),
384 MEM_VOLATILE_P (op0
));
385 machine_mode best_mode
;
386 if (iter
.next_mode (&best_mode
))
388 /* We can use a memory in BEST_MODE. See whether this is true for
389 any wider modes. All other things being equal, we prefer to
390 use the widest mode possible because it tends to expose more
391 CSE opportunities. */
392 if (!iter
.prefer_smaller_modes ())
394 /* Limit the search to the mode required by the corresponding
395 register insertion or extraction instruction, if any. */
396 machine_mode limit_mode
= word_mode
;
397 extraction_insn insn
;
398 if (get_best_reg_extraction_insn (&insn
, pattern
,
399 GET_MODE_BITSIZE (best_mode
),
401 limit_mode
= insn
.field_mode
;
403 machine_mode wider_mode
;
404 while (iter
.next_mode (&wider_mode
)
405 && GET_MODE_SIZE (wider_mode
) <= GET_MODE_SIZE (limit_mode
))
406 best_mode
= wider_mode
;
408 return narrow_bit_field_mem (op0
, best_mode
, bitsize
, bitnum
,
414 /* Return true if a bitfield of size BITSIZE at bit number BITNUM within
415 a structure of mode STRUCT_MODE represents a lowpart subreg. The subreg
416 offset is then BITNUM / BITS_PER_UNIT. */
419 lowpart_bit_field_p (unsigned HOST_WIDE_INT bitnum
,
420 unsigned HOST_WIDE_INT bitsize
,
421 machine_mode struct_mode
)
423 if (BYTES_BIG_ENDIAN
)
424 return (bitnum
% BITS_PER_UNIT
== 0
425 && (bitnum
+ bitsize
== GET_MODE_BITSIZE (struct_mode
)
426 || (bitnum
+ bitsize
) % BITS_PER_WORD
== 0));
428 return bitnum
% BITS_PER_WORD
== 0;
431 /* Return true if -fstrict-volatile-bitfields applies to an access of OP0
432 containing BITSIZE bits starting at BITNUM, with field mode FIELDMODE.
433 Return false if the access would touch memory outside the range
434 BITREGION_START to BITREGION_END for conformance to the C++ memory
438 strict_volatile_bitfield_p (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
439 unsigned HOST_WIDE_INT bitnum
,
440 machine_mode fieldmode
,
441 unsigned HOST_WIDE_INT bitregion_start
,
442 unsigned HOST_WIDE_INT bitregion_end
)
444 unsigned HOST_WIDE_INT modesize
= GET_MODE_BITSIZE (fieldmode
);
446 /* -fstrict-volatile-bitfields must be enabled and we must have a
449 || !MEM_VOLATILE_P (op0
)
450 || flag_strict_volatile_bitfields
<= 0)
453 /* Non-integral modes likely only happen with packed structures.
455 if (!SCALAR_INT_MODE_P (fieldmode
))
458 /* The bit size must not be larger than the field mode, and
459 the field mode must not be larger than a word. */
460 if (bitsize
> modesize
|| modesize
> BITS_PER_WORD
)
463 /* Check for cases of unaligned fields that must be split. */
464 if (bitnum
% modesize
+ bitsize
> modesize
)
467 /* The memory must be sufficiently aligned for a MODESIZE access.
468 This condition guarantees, that the memory access will not
469 touch anything after the end of the structure. */
470 if (MEM_ALIGN (op0
) < modesize
)
473 /* Check for cases where the C++ memory model applies. */
474 if (bitregion_end
!= 0
475 && (bitnum
- bitnum
% modesize
< bitregion_start
476 || bitnum
- bitnum
% modesize
+ modesize
- 1 > bitregion_end
))
482 /* Return true if OP is a memory and if a bitfield of size BITSIZE at
483 bit number BITNUM can be treated as a simple value of mode MODE. */
486 simple_mem_bitfield_p (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
487 unsigned HOST_WIDE_INT bitnum
, machine_mode mode
)
490 && bitnum
% BITS_PER_UNIT
== 0
491 && bitsize
== GET_MODE_BITSIZE (mode
)
492 && (!SLOW_UNALIGNED_ACCESS (mode
, MEM_ALIGN (op0
))
493 || (bitnum
% GET_MODE_ALIGNMENT (mode
) == 0
494 && MEM_ALIGN (op0
) >= GET_MODE_ALIGNMENT (mode
))));
497 /* Try to use instruction INSV to store VALUE into a field of OP0.
498 BITSIZE and BITNUM are as for store_bit_field. */
501 store_bit_field_using_insv (const extraction_insn
*insv
, rtx op0
,
502 unsigned HOST_WIDE_INT bitsize
,
503 unsigned HOST_WIDE_INT bitnum
,
506 struct expand_operand ops
[4];
509 rtx_insn
*last
= get_last_insn ();
510 bool copy_back
= false;
512 machine_mode op_mode
= insv
->field_mode
;
513 unsigned int unit
= GET_MODE_BITSIZE (op_mode
);
514 if (bitsize
== 0 || bitsize
> unit
)
518 /* Get a reference to the first byte of the field. */
519 xop0
= narrow_bit_field_mem (xop0
, insv
->struct_mode
, bitsize
, bitnum
,
523 /* Convert from counting within OP0 to counting in OP_MODE. */
524 if (BYTES_BIG_ENDIAN
)
525 bitnum
+= unit
- GET_MODE_BITSIZE (GET_MODE (op0
));
527 /* If xop0 is a register, we need it in OP_MODE
528 to make it acceptable to the format of insv. */
529 if (GET_CODE (xop0
) == SUBREG
)
530 /* We can't just change the mode, because this might clobber op0,
531 and we will need the original value of op0 if insv fails. */
532 xop0
= gen_rtx_SUBREG (op_mode
, SUBREG_REG (xop0
), SUBREG_BYTE (xop0
));
533 if (REG_P (xop0
) && GET_MODE (xop0
) != op_mode
)
534 xop0
= gen_lowpart_SUBREG (op_mode
, xop0
);
537 /* If the destination is a paradoxical subreg such that we need a
538 truncate to the inner mode, perform the insertion on a temporary and
539 truncate the result to the original destination. Note that we can't
540 just truncate the paradoxical subreg as (truncate:N (subreg:W (reg:N
541 X) 0)) is (reg:N X). */
542 if (GET_CODE (xop0
) == SUBREG
543 && REG_P (SUBREG_REG (xop0
))
544 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (SUBREG_REG (xop0
)),
547 rtx tem
= gen_reg_rtx (op_mode
);
548 emit_move_insn (tem
, xop0
);
553 /* There are similar overflow check at the start of store_bit_field_1,
554 but that only check the situation where the field lies completely
555 outside the register, while there do have situation where the field
556 lies partialy in the register, we need to adjust bitsize for this
557 partial overflow situation. Without this fix, pr48335-2.c on big-endian
558 will broken on those arch support bit insert instruction, like arm, aarch64
560 if (bitsize
+ bitnum
> unit
&& bitnum
< unit
)
562 warning (OPT_Wextra
, "write of %wu-bit data outside the bound of "
563 "destination object, data truncated into %wu-bit",
564 bitsize
, unit
- bitnum
);
565 bitsize
= unit
- bitnum
;
568 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
569 "backwards" from the size of the unit we are inserting into.
570 Otherwise, we count bits from the most significant on a
571 BYTES/BITS_BIG_ENDIAN machine. */
573 if (BITS_BIG_ENDIAN
!= BYTES_BIG_ENDIAN
)
574 bitnum
= unit
- bitsize
- bitnum
;
576 /* Convert VALUE to op_mode (which insv insn wants) in VALUE1. */
578 if (GET_MODE (value
) != op_mode
)
580 if (GET_MODE_BITSIZE (GET_MODE (value
)) >= bitsize
)
582 /* Optimization: Don't bother really extending VALUE
583 if it has all the bits we will actually use. However,
584 if we must narrow it, be sure we do it correctly. */
586 if (GET_MODE_SIZE (GET_MODE (value
)) < GET_MODE_SIZE (op_mode
))
590 tmp
= simplify_subreg (op_mode
, value1
, GET_MODE (value
), 0);
592 tmp
= simplify_gen_subreg (op_mode
,
593 force_reg (GET_MODE (value
),
595 GET_MODE (value
), 0);
599 value1
= gen_lowpart (op_mode
, value1
);
601 else if (CONST_INT_P (value
))
602 value1
= gen_int_mode (INTVAL (value
), op_mode
);
604 /* Parse phase is supposed to make VALUE's data type
605 match that of the component reference, which is a type
606 at least as wide as the field; so VALUE should have
607 a mode that corresponds to that type. */
608 gcc_assert (CONSTANT_P (value
));
611 create_fixed_operand (&ops
[0], xop0
);
612 create_integer_operand (&ops
[1], bitsize
);
613 create_integer_operand (&ops
[2], bitnum
);
614 create_input_operand (&ops
[3], value1
, op_mode
);
615 if (maybe_expand_insn (insv
->icode
, 4, ops
))
618 convert_move (op0
, xop0
, true);
621 delete_insns_since (last
);
625 /* A subroutine of store_bit_field, with the same arguments. Return true
626 if the operation could be implemented.
628 If FALLBACK_P is true, fall back to store_fixed_bit_field if we have
629 no other way of implementing the operation. If FALLBACK_P is false,
630 return false instead. */
633 store_bit_field_1 (rtx str_rtx
, unsigned HOST_WIDE_INT bitsize
,
634 unsigned HOST_WIDE_INT bitnum
,
635 unsigned HOST_WIDE_INT bitregion_start
,
636 unsigned HOST_WIDE_INT bitregion_end
,
637 machine_mode fieldmode
,
638 rtx value
, bool fallback_p
)
643 while (GET_CODE (op0
) == SUBREG
)
645 /* The following line once was done only if WORDS_BIG_ENDIAN,
646 but I think that is a mistake. WORDS_BIG_ENDIAN is
647 meaningful at a much higher level; when structures are copied
648 between memory and regs, the higher-numbered regs
649 always get higher addresses. */
650 int inner_mode_size
= GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0
)));
651 int outer_mode_size
= GET_MODE_SIZE (GET_MODE (op0
));
654 /* Paradoxical subregs need special handling on big endian machines. */
655 if (SUBREG_BYTE (op0
) == 0 && inner_mode_size
< outer_mode_size
)
657 int difference
= inner_mode_size
- outer_mode_size
;
659 if (WORDS_BIG_ENDIAN
)
660 byte_offset
+= (difference
/ UNITS_PER_WORD
) * UNITS_PER_WORD
;
661 if (BYTES_BIG_ENDIAN
)
662 byte_offset
+= difference
% UNITS_PER_WORD
;
665 byte_offset
= SUBREG_BYTE (op0
);
667 bitnum
+= byte_offset
* BITS_PER_UNIT
;
668 op0
= SUBREG_REG (op0
);
671 /* No action is needed if the target is a register and if the field
672 lies completely outside that register. This can occur if the source
673 code contains an out-of-bounds access to a small array. */
674 if (REG_P (op0
) && bitnum
>= GET_MODE_BITSIZE (GET_MODE (op0
)))
677 /* Use vec_set patterns for inserting parts of vectors whenever
679 if (VECTOR_MODE_P (GET_MODE (op0
))
681 && optab_handler (vec_set_optab
, GET_MODE (op0
)) != CODE_FOR_nothing
682 && fieldmode
== GET_MODE_INNER (GET_MODE (op0
))
683 && bitsize
== GET_MODE_UNIT_BITSIZE (GET_MODE (op0
))
684 && !(bitnum
% GET_MODE_UNIT_BITSIZE (GET_MODE (op0
))))
686 struct expand_operand ops
[3];
687 machine_mode outermode
= GET_MODE (op0
);
688 machine_mode innermode
= GET_MODE_INNER (outermode
);
689 enum insn_code icode
= optab_handler (vec_set_optab
, outermode
);
690 int pos
= bitnum
/ GET_MODE_BITSIZE (innermode
);
692 create_fixed_operand (&ops
[0], op0
);
693 create_input_operand (&ops
[1], value
, innermode
);
694 create_integer_operand (&ops
[2], pos
);
695 if (maybe_expand_insn (icode
, 3, ops
))
699 /* If the target is a register, overwriting the entire object, or storing
700 a full-word or multi-word field can be done with just a SUBREG. */
702 && bitsize
== GET_MODE_BITSIZE (fieldmode
)
703 && ((bitsize
== GET_MODE_BITSIZE (GET_MODE (op0
)) && bitnum
== 0)
704 || (bitsize
% BITS_PER_WORD
== 0 && bitnum
% BITS_PER_WORD
== 0)))
706 /* Use the subreg machinery either to narrow OP0 to the required
707 words or to cope with mode punning between equal-sized modes.
708 In the latter case, use subreg on the rhs side, not lhs. */
711 if (bitsize
== GET_MODE_BITSIZE (GET_MODE (op0
)))
713 sub
= simplify_gen_subreg (GET_MODE (op0
), value
, fieldmode
, 0);
716 emit_move_insn (op0
, sub
);
722 sub
= simplify_gen_subreg (fieldmode
, op0
, GET_MODE (op0
),
723 bitnum
/ BITS_PER_UNIT
);
726 emit_move_insn (sub
, value
);
732 /* If the target is memory, storing any naturally aligned field can be
733 done with a simple store. For targets that support fast unaligned
734 memory, any naturally sized, unit aligned field can be done directly. */
735 if (simple_mem_bitfield_p (op0
, bitsize
, bitnum
, fieldmode
))
737 op0
= adjust_bitfield_address (op0
, fieldmode
, bitnum
/ BITS_PER_UNIT
);
738 emit_move_insn (op0
, value
);
742 /* Make sure we are playing with integral modes. Pun with subregs
743 if we aren't. This must come after the entire register case above,
744 since that case is valid for any mode. The following cases are only
745 valid for integral modes. */
747 machine_mode imode
= int_mode_for_mode (GET_MODE (op0
));
748 if (imode
!= GET_MODE (op0
))
751 op0
= adjust_bitfield_address_size (op0
, imode
, 0, MEM_SIZE (op0
));
754 gcc_assert (imode
!= BLKmode
);
755 op0
= gen_lowpart (imode
, op0
);
760 /* Storing an lsb-aligned field in a register
761 can be done with a movstrict instruction. */
764 && lowpart_bit_field_p (bitnum
, bitsize
, GET_MODE (op0
))
765 && bitsize
== GET_MODE_BITSIZE (fieldmode
)
766 && optab_handler (movstrict_optab
, fieldmode
) != CODE_FOR_nothing
)
768 struct expand_operand ops
[2];
769 enum insn_code icode
= optab_handler (movstrict_optab
, fieldmode
);
771 unsigned HOST_WIDE_INT subreg_off
;
773 if (GET_CODE (arg0
) == SUBREG
)
775 /* Else we've got some float mode source being extracted into
776 a different float mode destination -- this combination of
777 subregs results in Severe Tire Damage. */
778 gcc_assert (GET_MODE (SUBREG_REG (arg0
)) == fieldmode
779 || GET_MODE_CLASS (fieldmode
) == MODE_INT
780 || GET_MODE_CLASS (fieldmode
) == MODE_PARTIAL_INT
);
781 arg0
= SUBREG_REG (arg0
);
784 subreg_off
= bitnum
/ BITS_PER_UNIT
;
785 if (validate_subreg (fieldmode
, GET_MODE (arg0
), arg0
, subreg_off
))
787 arg0
= gen_rtx_SUBREG (fieldmode
, arg0
, subreg_off
);
789 create_fixed_operand (&ops
[0], arg0
);
790 /* Shrink the source operand to FIELDMODE. */
791 create_convert_operand_to (&ops
[1], value
, fieldmode
, false);
792 if (maybe_expand_insn (icode
, 2, ops
))
797 /* Handle fields bigger than a word. */
799 if (bitsize
> BITS_PER_WORD
)
801 /* Here we transfer the words of the field
802 in the order least significant first.
803 This is because the most significant word is the one which may
805 However, only do that if the value is not BLKmode. */
807 unsigned int backwards
= WORDS_BIG_ENDIAN
&& fieldmode
!= BLKmode
;
808 unsigned int nwords
= (bitsize
+ (BITS_PER_WORD
- 1)) / BITS_PER_WORD
;
812 /* This is the mode we must force value to, so that there will be enough
813 subwords to extract. Note that fieldmode will often (always?) be
814 VOIDmode, because that is what store_field uses to indicate that this
815 is a bit field, but passing VOIDmode to operand_subword_force
817 fieldmode
= GET_MODE (value
);
818 if (fieldmode
== VOIDmode
)
819 fieldmode
= smallest_mode_for_size (nwords
* BITS_PER_WORD
, MODE_INT
);
821 last
= get_last_insn ();
822 for (i
= 0; i
< nwords
; i
++)
824 /* If I is 0, use the low-order word in both field and target;
825 if I is 1, use the next to lowest word; and so on. */
826 unsigned int wordnum
= (backwards
827 ? GET_MODE_SIZE (fieldmode
) / UNITS_PER_WORD
830 unsigned int bit_offset
= (backwards
831 ? MAX ((int) bitsize
- ((int) i
+ 1)
834 : (int) i
* BITS_PER_WORD
);
835 rtx value_word
= operand_subword_force (value
, wordnum
, fieldmode
);
836 unsigned HOST_WIDE_INT new_bitsize
=
837 MIN (BITS_PER_WORD
, bitsize
- i
* BITS_PER_WORD
);
839 /* If the remaining chunk doesn't have full wordsize we have
840 to make sure that for big endian machines the higher order
842 if (new_bitsize
< BITS_PER_WORD
&& BYTES_BIG_ENDIAN
&& !backwards
)
843 value_word
= simplify_expand_binop (word_mode
, lshr_optab
,
845 GEN_INT (BITS_PER_WORD
850 if (!store_bit_field_1 (op0
, new_bitsize
,
852 bitregion_start
, bitregion_end
,
854 value_word
, fallback_p
))
856 delete_insns_since (last
);
863 /* If VALUE has a floating-point or complex mode, access it as an
864 integer of the corresponding size. This can occur on a machine
865 with 64 bit registers that uses SFmode for float. It can also
866 occur for unaligned float or complex fields. */
868 if (GET_MODE (value
) != VOIDmode
869 && GET_MODE_CLASS (GET_MODE (value
)) != MODE_INT
870 && GET_MODE_CLASS (GET_MODE (value
)) != MODE_PARTIAL_INT
)
872 value
= gen_reg_rtx (int_mode_for_mode (GET_MODE (value
)));
873 emit_move_insn (gen_lowpart (GET_MODE (orig_value
), value
), orig_value
);
876 /* If OP0 is a multi-word register, narrow it to the affected word.
877 If the region spans two words, defer to store_split_bit_field. */
878 if (!MEM_P (op0
) && GET_MODE_SIZE (GET_MODE (op0
)) > UNITS_PER_WORD
)
880 op0
= simplify_gen_subreg (word_mode
, op0
, GET_MODE (op0
),
881 bitnum
/ BITS_PER_WORD
* UNITS_PER_WORD
);
883 bitnum
%= BITS_PER_WORD
;
884 if (bitnum
+ bitsize
> BITS_PER_WORD
)
889 store_split_bit_field (op0
, bitsize
, bitnum
, bitregion_start
,
890 bitregion_end
, value
);
895 /* From here on we can assume that the field to be stored in fits
896 within a word. If the destination is a register, it too fits
899 extraction_insn insv
;
901 && get_best_reg_extraction_insn (&insv
, EP_insv
,
902 GET_MODE_BITSIZE (GET_MODE (op0
)),
904 && store_bit_field_using_insv (&insv
, op0
, bitsize
, bitnum
, value
))
907 /* If OP0 is a memory, try copying it to a register and seeing if a
908 cheap register alternative is available. */
911 if (get_best_mem_extraction_insn (&insv
, EP_insv
, bitsize
, bitnum
,
913 && store_bit_field_using_insv (&insv
, op0
, bitsize
, bitnum
, value
))
916 rtx_insn
*last
= get_last_insn ();
918 /* Try loading part of OP0 into a register, inserting the bitfield
919 into that, and then copying the result back to OP0. */
920 unsigned HOST_WIDE_INT bitpos
;
921 rtx xop0
= adjust_bit_field_mem_for_reg (EP_insv
, op0
, bitsize
, bitnum
,
922 bitregion_start
, bitregion_end
,
926 rtx tempreg
= copy_to_reg (xop0
);
927 if (store_bit_field_1 (tempreg
, bitsize
, bitpos
,
928 bitregion_start
, bitregion_end
,
929 fieldmode
, orig_value
, false))
931 emit_move_insn (xop0
, tempreg
);
934 delete_insns_since (last
);
941 store_fixed_bit_field (op0
, bitsize
, bitnum
, bitregion_start
,
942 bitregion_end
, value
);
946 /* Generate code to store value from rtx VALUE
947 into a bit-field within structure STR_RTX
948 containing BITSIZE bits starting at bit BITNUM.
950 BITREGION_START is bitpos of the first bitfield in this region.
951 BITREGION_END is the bitpos of the ending bitfield in this region.
952 These two fields are 0, if the C++ memory model does not apply,
953 or we are not interested in keeping track of bitfield regions.
955 FIELDMODE is the machine-mode of the FIELD_DECL node for this field. */
958 store_bit_field (rtx str_rtx
, unsigned HOST_WIDE_INT bitsize
,
959 unsigned HOST_WIDE_INT bitnum
,
960 unsigned HOST_WIDE_INT bitregion_start
,
961 unsigned HOST_WIDE_INT bitregion_end
,
962 machine_mode fieldmode
,
965 /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
966 if (strict_volatile_bitfield_p (str_rtx
, bitsize
, bitnum
, fieldmode
,
967 bitregion_start
, bitregion_end
))
969 /* Storing of a full word can be done with a simple store.
970 We know here that the field can be accessed with one single
971 instruction. For targets that support unaligned memory,
972 an unaligned access may be necessary. */
973 if (bitsize
== GET_MODE_BITSIZE (fieldmode
))
975 str_rtx
= adjust_bitfield_address (str_rtx
, fieldmode
,
976 bitnum
/ BITS_PER_UNIT
);
977 gcc_assert (bitnum
% BITS_PER_UNIT
== 0);
978 emit_move_insn (str_rtx
, value
);
984 str_rtx
= narrow_bit_field_mem (str_rtx
, fieldmode
, bitsize
, bitnum
,
986 gcc_assert (bitnum
+ bitsize
<= GET_MODE_BITSIZE (fieldmode
));
987 temp
= copy_to_reg (str_rtx
);
988 if (!store_bit_field_1 (temp
, bitsize
, bitnum
, 0, 0,
989 fieldmode
, value
, true))
992 emit_move_insn (str_rtx
, temp
);
998 /* Under the C++0x memory model, we must not touch bits outside the
999 bit region. Adjust the address to start at the beginning of the
1001 if (MEM_P (str_rtx
) && bitregion_start
> 0)
1003 machine_mode bestmode
;
1004 HOST_WIDE_INT offset
, size
;
1006 gcc_assert ((bitregion_start
% BITS_PER_UNIT
) == 0);
1008 offset
= bitregion_start
/ BITS_PER_UNIT
;
1009 bitnum
-= bitregion_start
;
1010 size
= (bitnum
+ bitsize
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
1011 bitregion_end
-= bitregion_start
;
1012 bitregion_start
= 0;
1013 bestmode
= get_best_mode (bitsize
, bitnum
,
1014 bitregion_start
, bitregion_end
,
1015 MEM_ALIGN (str_rtx
), VOIDmode
,
1016 MEM_VOLATILE_P (str_rtx
));
1017 str_rtx
= adjust_bitfield_address_size (str_rtx
, bestmode
, offset
, size
);
1020 if (!store_bit_field_1 (str_rtx
, bitsize
, bitnum
,
1021 bitregion_start
, bitregion_end
,
1022 fieldmode
, value
, true))
1026 /* Use shifts and boolean operations to store VALUE into a bit field of
1027 width BITSIZE in OP0, starting at bit BITNUM. */
1030 store_fixed_bit_field (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
1031 unsigned HOST_WIDE_INT bitnum
,
1032 unsigned HOST_WIDE_INT bitregion_start
,
1033 unsigned HOST_WIDE_INT bitregion_end
,
1036 /* There is a case not handled here:
1037 a structure with a known alignment of just a halfword
1038 and a field split across two aligned halfwords within the structure.
1039 Or likewise a structure with a known alignment of just a byte
1040 and a field split across two bytes.
1041 Such cases are not supposed to be able to occur. */
1045 machine_mode mode
= GET_MODE (op0
);
1046 if (GET_MODE_BITSIZE (mode
) == 0
1047 || GET_MODE_BITSIZE (mode
) > GET_MODE_BITSIZE (word_mode
))
1049 mode
= get_best_mode (bitsize
, bitnum
, bitregion_start
, bitregion_end
,
1050 MEM_ALIGN (op0
), mode
, MEM_VOLATILE_P (op0
));
1052 if (mode
== VOIDmode
)
1054 /* The only way this should occur is if the field spans word
1056 store_split_bit_field (op0
, bitsize
, bitnum
, bitregion_start
,
1057 bitregion_end
, value
);
1061 op0
= narrow_bit_field_mem (op0
, mode
, bitsize
, bitnum
, &bitnum
);
1064 store_fixed_bit_field_1 (op0
, bitsize
, bitnum
, value
);
1067 /* Helper function for store_fixed_bit_field, stores
1068 the bit field always using the MODE of OP0. */
1071 store_fixed_bit_field_1 (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
1072 unsigned HOST_WIDE_INT bitnum
,
1080 mode
= GET_MODE (op0
);
1081 gcc_assert (SCALAR_INT_MODE_P (mode
));
1083 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
1084 for invalid input, such as f5 from gcc.dg/pr48335-2.c. */
1086 if (BYTES_BIG_ENDIAN
)
1087 /* BITNUM is the distance between our msb
1088 and that of the containing datum.
1089 Convert it to the distance from the lsb. */
1090 bitnum
= GET_MODE_BITSIZE (mode
) - bitsize
- bitnum
;
1092 /* Now BITNUM is always the distance between our lsb
1095 /* Shift VALUE left by BITNUM bits. If VALUE is not constant,
1096 we must first convert its mode to MODE. */
1098 if (CONST_INT_P (value
))
1100 unsigned HOST_WIDE_INT v
= UINTVAL (value
);
1102 if (bitsize
< HOST_BITS_PER_WIDE_INT
)
1103 v
&= ((unsigned HOST_WIDE_INT
) 1 << bitsize
) - 1;
1107 else if ((bitsize
< HOST_BITS_PER_WIDE_INT
1108 && v
== ((unsigned HOST_WIDE_INT
) 1 << bitsize
) - 1)
1109 || (bitsize
== HOST_BITS_PER_WIDE_INT
1110 && v
== (unsigned HOST_WIDE_INT
) -1))
1113 value
= lshift_value (mode
, v
, bitnum
);
1117 int must_and
= (GET_MODE_BITSIZE (GET_MODE (value
)) != bitsize
1118 && bitnum
+ bitsize
!= GET_MODE_BITSIZE (mode
));
1120 if (GET_MODE (value
) != mode
)
1121 value
= convert_to_mode (mode
, value
, 1);
1124 value
= expand_binop (mode
, and_optab
, value
,
1125 mask_rtx (mode
, 0, bitsize
, 0),
1126 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
1128 value
= expand_shift (LSHIFT_EXPR
, mode
, value
,
1129 bitnum
, NULL_RTX
, 1);
1132 /* Now clear the chosen bits in OP0,
1133 except that if VALUE is -1 we need not bother. */
1134 /* We keep the intermediates in registers to allow CSE to combine
1135 consecutive bitfield assignments. */
1137 temp
= force_reg (mode
, op0
);
1141 temp
= expand_binop (mode
, and_optab
, temp
,
1142 mask_rtx (mode
, bitnum
, bitsize
, 1),
1143 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
1144 temp
= force_reg (mode
, temp
);
1147 /* Now logical-or VALUE into OP0, unless it is zero. */
1151 temp
= expand_binop (mode
, ior_optab
, temp
, value
,
1152 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
1153 temp
= force_reg (mode
, temp
);
1158 op0
= copy_rtx (op0
);
1159 emit_move_insn (op0
, temp
);
1163 /* Store a bit field that is split across multiple accessible memory objects.
1165 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
1166 BITSIZE is the field width; BITPOS the position of its first bit
1168 VALUE is the value to store.
1170 This does not yet handle fields wider than BITS_PER_WORD. */
1173 store_split_bit_field (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
1174 unsigned HOST_WIDE_INT bitpos
,
1175 unsigned HOST_WIDE_INT bitregion_start
,
1176 unsigned HOST_WIDE_INT bitregion_end
,
1180 unsigned int bitsdone
= 0;
1182 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1184 if (REG_P (op0
) || GET_CODE (op0
) == SUBREG
)
1185 unit
= BITS_PER_WORD
;
1187 unit
= MIN (MEM_ALIGN (op0
), BITS_PER_WORD
);
1189 /* If OP0 is a memory with a mode, then UNIT must not be larger than
1190 OP0's mode as well. Otherwise, store_fixed_bit_field will call us
1191 again, and we will mutually recurse forever. */
1192 if (MEM_P (op0
) && GET_MODE_BITSIZE (GET_MODE (op0
)) > 0)
1193 unit
= MIN (unit
, GET_MODE_BITSIZE (GET_MODE (op0
)));
1195 /* If VALUE is a constant other than a CONST_INT, get it into a register in
1196 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
1197 that VALUE might be a floating-point constant. */
1198 if (CONSTANT_P (value
) && !CONST_INT_P (value
))
1200 rtx word
= gen_lowpart_common (word_mode
, value
);
1202 if (word
&& (value
!= word
))
1205 value
= gen_lowpart_common (word_mode
,
1206 force_reg (GET_MODE (value
) != VOIDmode
1208 : word_mode
, value
));
1211 while (bitsdone
< bitsize
)
1213 unsigned HOST_WIDE_INT thissize
;
1215 unsigned HOST_WIDE_INT thispos
;
1216 unsigned HOST_WIDE_INT offset
;
1218 offset
= (bitpos
+ bitsdone
) / unit
;
1219 thispos
= (bitpos
+ bitsdone
) % unit
;
1221 /* When region of bytes we can touch is restricted, decrease
1222 UNIT close to the end of the region as needed. If op0 is a REG
1223 or SUBREG of REG, don't do this, as there can't be data races
1224 on a register and we can expand shorter code in some cases. */
1226 && unit
> BITS_PER_UNIT
1227 && bitpos
+ bitsdone
- thispos
+ unit
> bitregion_end
+ 1
1229 && (GET_CODE (op0
) != SUBREG
|| !REG_P (SUBREG_REG (op0
))))
1235 /* THISSIZE must not overrun a word boundary. Otherwise,
1236 store_fixed_bit_field will call us again, and we will mutually
1238 thissize
= MIN (bitsize
- bitsdone
, BITS_PER_WORD
);
1239 thissize
= MIN (thissize
, unit
- thispos
);
1241 if (BYTES_BIG_ENDIAN
)
1243 /* Fetch successively less significant portions. */
1244 if (CONST_INT_P (value
))
1245 part
= GEN_INT (((unsigned HOST_WIDE_INT
) (INTVAL (value
))
1246 >> (bitsize
- bitsdone
- thissize
))
1247 & (((HOST_WIDE_INT
) 1 << thissize
) - 1));
1250 int total_bits
= GET_MODE_BITSIZE (GET_MODE (value
));
1251 /* The args are chosen so that the last part includes the
1252 lsb. Give extract_bit_field the value it needs (with
1253 endianness compensation) to fetch the piece we want. */
1254 part
= extract_fixed_bit_field (word_mode
, value
, thissize
,
1255 total_bits
- bitsize
+ bitsdone
,
1261 /* Fetch successively more significant portions. */
1262 if (CONST_INT_P (value
))
1263 part
= GEN_INT (((unsigned HOST_WIDE_INT
) (INTVAL (value
))
1265 & (((HOST_WIDE_INT
) 1 << thissize
) - 1));
1267 part
= extract_fixed_bit_field (word_mode
, value
, thissize
,
1268 bitsdone
, NULL_RTX
, 1);
1271 /* If OP0 is a register, then handle OFFSET here.
1273 When handling multiword bitfields, extract_bit_field may pass
1274 down a word_mode SUBREG of a larger REG for a bitfield that actually
1275 crosses a word boundary. Thus, for a SUBREG, we must find
1276 the current word starting from the base register. */
1277 if (GET_CODE (op0
) == SUBREG
)
1279 int word_offset
= (SUBREG_BYTE (op0
) / UNITS_PER_WORD
)
1280 + (offset
* unit
/ BITS_PER_WORD
);
1281 machine_mode sub_mode
= GET_MODE (SUBREG_REG (op0
));
1282 if (sub_mode
!= BLKmode
&& GET_MODE_SIZE (sub_mode
) < UNITS_PER_WORD
)
1283 word
= word_offset
? const0_rtx
: op0
;
1285 word
= operand_subword_force (SUBREG_REG (op0
), word_offset
,
1286 GET_MODE (SUBREG_REG (op0
)));
1287 offset
&= BITS_PER_WORD
/ unit
- 1;
1289 else if (REG_P (op0
))
1291 machine_mode op0_mode
= GET_MODE (op0
);
1292 if (op0_mode
!= BLKmode
&& GET_MODE_SIZE (op0_mode
) < UNITS_PER_WORD
)
1293 word
= offset
? const0_rtx
: op0
;
1295 word
= operand_subword_force (op0
, offset
* unit
/ BITS_PER_WORD
,
1297 offset
&= BITS_PER_WORD
/ unit
- 1;
1302 /* OFFSET is in UNITs, and UNIT is in bits. If WORD is const0_rtx,
1303 it is just an out-of-bounds access. Ignore it. */
1304 if (word
!= const0_rtx
)
1305 store_fixed_bit_field (word
, thissize
, offset
* unit
+ thispos
,
1306 bitregion_start
, bitregion_end
, part
);
1307 bitsdone
+= thissize
;
1311 /* A subroutine of extract_bit_field_1 that converts return value X
1312 to either MODE or TMODE. MODE, TMODE and UNSIGNEDP are arguments
1313 to extract_bit_field. */
1316 convert_extracted_bit_field (rtx x
, machine_mode mode
,
1317 machine_mode tmode
, bool unsignedp
)
1319 if (GET_MODE (x
) == tmode
|| GET_MODE (x
) == mode
)
1322 /* If the x mode is not a scalar integral, first convert to the
1323 integer mode of that size and then access it as a floating-point
1324 value via a SUBREG. */
1325 if (!SCALAR_INT_MODE_P (tmode
))
1329 smode
= mode_for_size (GET_MODE_BITSIZE (tmode
), MODE_INT
, 0);
1330 x
= convert_to_mode (smode
, x
, unsignedp
);
1331 x
= force_reg (smode
, x
);
1332 return gen_lowpart (tmode
, x
);
1335 return convert_to_mode (tmode
, x
, unsignedp
);
1338 /* Try to use an ext(z)v pattern to extract a field from OP0.
1339 Return the extracted value on success, otherwise return null.
1340 EXT_MODE is the mode of the extraction and the other arguments
1341 are as for extract_bit_field. */
1344 extract_bit_field_using_extv (const extraction_insn
*extv
, rtx op0
,
1345 unsigned HOST_WIDE_INT bitsize
,
1346 unsigned HOST_WIDE_INT bitnum
,
1347 int unsignedp
, rtx target
,
1348 machine_mode mode
, machine_mode tmode
)
1350 struct expand_operand ops
[4];
1351 rtx spec_target
= target
;
1352 rtx spec_target_subreg
= 0;
1353 machine_mode ext_mode
= extv
->field_mode
;
1354 unsigned unit
= GET_MODE_BITSIZE (ext_mode
);
1356 if (bitsize
== 0 || unit
< bitsize
)
1360 /* Get a reference to the first byte of the field. */
1361 op0
= narrow_bit_field_mem (op0
, extv
->struct_mode
, bitsize
, bitnum
,
1365 /* Convert from counting within OP0 to counting in EXT_MODE. */
1366 if (BYTES_BIG_ENDIAN
)
1367 bitnum
+= unit
- GET_MODE_BITSIZE (GET_MODE (op0
));
1369 /* If op0 is a register, we need it in EXT_MODE to make it
1370 acceptable to the format of ext(z)v. */
1371 if (GET_CODE (op0
) == SUBREG
&& GET_MODE (op0
) != ext_mode
)
1373 if (REG_P (op0
) && GET_MODE (op0
) != ext_mode
)
1374 op0
= gen_lowpart_SUBREG (ext_mode
, op0
);
1377 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
1378 "backwards" from the size of the unit we are extracting from.
1379 Otherwise, we count bits from the most significant on a
1380 BYTES/BITS_BIG_ENDIAN machine. */
1382 if (BITS_BIG_ENDIAN
!= BYTES_BIG_ENDIAN
)
1383 bitnum
= unit
- bitsize
- bitnum
;
1386 target
= spec_target
= gen_reg_rtx (tmode
);
1388 if (GET_MODE (target
) != ext_mode
)
1390 /* Don't use LHS paradoxical subreg if explicit truncation is needed
1391 between the mode of the extraction (word_mode) and the target
1392 mode. Instead, create a temporary and use convert_move to set
1395 && TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (target
), ext_mode
))
1397 target
= gen_lowpart (ext_mode
, target
);
1398 if (GET_MODE_PRECISION (ext_mode
)
1399 > GET_MODE_PRECISION (GET_MODE (spec_target
)))
1400 spec_target_subreg
= target
;
1403 target
= gen_reg_rtx (ext_mode
);
1406 create_output_operand (&ops
[0], target
, ext_mode
);
1407 create_fixed_operand (&ops
[1], op0
);
1408 create_integer_operand (&ops
[2], bitsize
);
1409 create_integer_operand (&ops
[3], bitnum
);
1410 if (maybe_expand_insn (extv
->icode
, 4, ops
))
1412 target
= ops
[0].value
;
1413 if (target
== spec_target
)
1415 if (target
== spec_target_subreg
)
1417 return convert_extracted_bit_field (target
, mode
, tmode
, unsignedp
);
1422 /* A subroutine of extract_bit_field, with the same arguments.
1423 If FALLBACK_P is true, fall back to extract_fixed_bit_field
1424 if we can find no other means of implementing the operation.
1425 if FALLBACK_P is false, return NULL instead. */
1428 extract_bit_field_1 (rtx str_rtx
, unsigned HOST_WIDE_INT bitsize
,
1429 unsigned HOST_WIDE_INT bitnum
, int unsignedp
, rtx target
,
1430 machine_mode mode
, machine_mode tmode
,
1434 machine_mode int_mode
;
1437 if (tmode
== VOIDmode
)
1440 while (GET_CODE (op0
) == SUBREG
)
1442 bitnum
+= SUBREG_BYTE (op0
) * BITS_PER_UNIT
;
1443 op0
= SUBREG_REG (op0
);
1446 /* If we have an out-of-bounds access to a register, just return an
1447 uninitialized register of the required mode. This can occur if the
1448 source code contains an out-of-bounds access to a small array. */
1449 if (REG_P (op0
) && bitnum
>= GET_MODE_BITSIZE (GET_MODE (op0
)))
1450 return gen_reg_rtx (tmode
);
1453 && mode
== GET_MODE (op0
)
1455 && bitsize
== GET_MODE_BITSIZE (GET_MODE (op0
)))
1457 /* We're trying to extract a full register from itself. */
1461 /* See if we can get a better vector mode before extracting. */
1462 if (VECTOR_MODE_P (GET_MODE (op0
))
1464 && GET_MODE_INNER (GET_MODE (op0
)) != tmode
)
1466 machine_mode new_mode
;
1468 if (GET_MODE_CLASS (tmode
) == MODE_FLOAT
)
1469 new_mode
= MIN_MODE_VECTOR_FLOAT
;
1470 else if (GET_MODE_CLASS (tmode
) == MODE_FRACT
)
1471 new_mode
= MIN_MODE_VECTOR_FRACT
;
1472 else if (GET_MODE_CLASS (tmode
) == MODE_UFRACT
)
1473 new_mode
= MIN_MODE_VECTOR_UFRACT
;
1474 else if (GET_MODE_CLASS (tmode
) == MODE_ACCUM
)
1475 new_mode
= MIN_MODE_VECTOR_ACCUM
;
1476 else if (GET_MODE_CLASS (tmode
) == MODE_UACCUM
)
1477 new_mode
= MIN_MODE_VECTOR_UACCUM
;
1479 new_mode
= MIN_MODE_VECTOR_INT
;
1481 for (; new_mode
!= VOIDmode
; new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1482 if (GET_MODE_SIZE (new_mode
) == GET_MODE_SIZE (GET_MODE (op0
))
1483 && targetm
.vector_mode_supported_p (new_mode
))
1485 if (new_mode
!= VOIDmode
)
1486 op0
= gen_lowpart (new_mode
, op0
);
1489 /* Use vec_extract patterns for extracting parts of vectors whenever
1491 if (VECTOR_MODE_P (GET_MODE (op0
))
1493 && optab_handler (vec_extract_optab
, GET_MODE (op0
)) != CODE_FOR_nothing
1494 && ((bitnum
+ bitsize
- 1) / GET_MODE_UNIT_BITSIZE (GET_MODE (op0
))
1495 == bitnum
/ GET_MODE_UNIT_BITSIZE (GET_MODE (op0
))))
1497 struct expand_operand ops
[3];
1498 machine_mode outermode
= GET_MODE (op0
);
1499 machine_mode innermode
= GET_MODE_INNER (outermode
);
1500 enum insn_code icode
= optab_handler (vec_extract_optab
, outermode
);
1501 unsigned HOST_WIDE_INT pos
= bitnum
/ GET_MODE_BITSIZE (innermode
);
1503 create_output_operand (&ops
[0], target
, innermode
);
1504 create_input_operand (&ops
[1], op0
, outermode
);
1505 create_integer_operand (&ops
[2], pos
);
1506 if (maybe_expand_insn (icode
, 3, ops
))
1508 target
= ops
[0].value
;
1509 if (GET_MODE (target
) != mode
)
1510 return gen_lowpart (tmode
, target
);
1515 /* Make sure we are playing with integral modes. Pun with subregs
1518 machine_mode imode
= int_mode_for_mode (GET_MODE (op0
));
1519 if (imode
!= GET_MODE (op0
))
1522 op0
= adjust_bitfield_address_size (op0
, imode
, 0, MEM_SIZE (op0
));
1523 else if (imode
!= BLKmode
)
1525 op0
= gen_lowpart (imode
, op0
);
1527 /* If we got a SUBREG, force it into a register since we
1528 aren't going to be able to do another SUBREG on it. */
1529 if (GET_CODE (op0
) == SUBREG
)
1530 op0
= force_reg (imode
, op0
);
1532 else if (REG_P (op0
))
1535 imode
= smallest_mode_for_size (GET_MODE_BITSIZE (GET_MODE (op0
)),
1537 reg
= gen_reg_rtx (imode
);
1538 subreg
= gen_lowpart_SUBREG (GET_MODE (op0
), reg
);
1539 emit_move_insn (subreg
, op0
);
1541 bitnum
+= SUBREG_BYTE (subreg
) * BITS_PER_UNIT
;
1545 HOST_WIDE_INT size
= GET_MODE_SIZE (GET_MODE (op0
));
1546 rtx mem
= assign_stack_temp (GET_MODE (op0
), size
);
1547 emit_move_insn (mem
, op0
);
1548 op0
= adjust_bitfield_address_size (mem
, BLKmode
, 0, size
);
1553 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1554 If that's wrong, the solution is to test for it and set TARGET to 0
1557 /* Get the mode of the field to use for atomic access or subreg
1560 if (SCALAR_INT_MODE_P (tmode
))
1562 machine_mode try_mode
= mode_for_size (bitsize
,
1563 GET_MODE_CLASS (tmode
), 0);
1564 if (try_mode
!= BLKmode
)
1567 gcc_assert (mode1
!= BLKmode
);
1569 /* Extraction of a full MODE1 value can be done with a subreg as long
1570 as the least significant bit of the value is the least significant
1571 bit of either OP0 or a word of OP0. */
1573 && lowpart_bit_field_p (bitnum
, bitsize
, GET_MODE (op0
))
1574 && bitsize
== GET_MODE_BITSIZE (mode1
)
1575 && TRULY_NOOP_TRUNCATION_MODES_P (mode1
, GET_MODE (op0
)))
1577 rtx sub
= simplify_gen_subreg (mode1
, op0
, GET_MODE (op0
),
1578 bitnum
/ BITS_PER_UNIT
);
1580 return convert_extracted_bit_field (sub
, mode
, tmode
, unsignedp
);
1583 /* Extraction of a full MODE1 value can be done with a load as long as
1584 the field is on a byte boundary and is sufficiently aligned. */
1585 if (simple_mem_bitfield_p (op0
, bitsize
, bitnum
, mode1
))
1587 op0
= adjust_bitfield_address (op0
, mode1
, bitnum
/ BITS_PER_UNIT
);
1588 return convert_extracted_bit_field (op0
, mode
, tmode
, unsignedp
);
1591 /* Handle fields bigger than a word. */
1593 if (bitsize
> BITS_PER_WORD
)
1595 /* Here we transfer the words of the field
1596 in the order least significant first.
1597 This is because the most significant word is the one which may
1598 be less than full. */
1600 unsigned int backwards
= WORDS_BIG_ENDIAN
;
1601 unsigned int nwords
= (bitsize
+ (BITS_PER_WORD
- 1)) / BITS_PER_WORD
;
1605 if (target
== 0 || !REG_P (target
) || !valid_multiword_target_p (target
))
1606 target
= gen_reg_rtx (mode
);
1608 /* In case we're about to clobber a base register or something
1609 (see gcc.c-torture/execute/20040625-1.c). */
1610 if (reg_mentioned_p (target
, str_rtx
))
1611 target
= gen_reg_rtx (mode
);
1613 /* Indicate for flow that the entire target reg is being set. */
1614 emit_clobber (target
);
1616 last
= get_last_insn ();
1617 for (i
= 0; i
< nwords
; i
++)
1619 /* If I is 0, use the low-order word in both field and target;
1620 if I is 1, use the next to lowest word; and so on. */
1621 /* Word number in TARGET to use. */
1622 unsigned int wordnum
1624 ? GET_MODE_SIZE (GET_MODE (target
)) / UNITS_PER_WORD
- i
- 1
1626 /* Offset from start of field in OP0. */
1627 unsigned int bit_offset
= (backwards
1628 ? MAX ((int) bitsize
- ((int) i
+ 1)
1631 : (int) i
* BITS_PER_WORD
);
1632 rtx target_part
= operand_subword (target
, wordnum
, 1, VOIDmode
);
1634 = extract_bit_field_1 (op0
, MIN (BITS_PER_WORD
,
1635 bitsize
- i
* BITS_PER_WORD
),
1636 bitnum
+ bit_offset
, 1, target_part
,
1637 mode
, word_mode
, fallback_p
);
1639 gcc_assert (target_part
);
1642 delete_insns_since (last
);
1646 if (result_part
!= target_part
)
1647 emit_move_insn (target_part
, result_part
);
1652 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1653 need to be zero'd out. */
1654 if (GET_MODE_SIZE (GET_MODE (target
)) > nwords
* UNITS_PER_WORD
)
1656 unsigned int i
, total_words
;
1658 total_words
= GET_MODE_SIZE (GET_MODE (target
)) / UNITS_PER_WORD
;
1659 for (i
= nwords
; i
< total_words
; i
++)
1661 (operand_subword (target
,
1662 backwards
? total_words
- i
- 1 : i
,
1669 /* Signed bit field: sign-extend with two arithmetic shifts. */
1670 target
= expand_shift (LSHIFT_EXPR
, mode
, target
,
1671 GET_MODE_BITSIZE (mode
) - bitsize
, NULL_RTX
, 0);
1672 return expand_shift (RSHIFT_EXPR
, mode
, target
,
1673 GET_MODE_BITSIZE (mode
) - bitsize
, NULL_RTX
, 0);
1676 /* If OP0 is a multi-word register, narrow it to the affected word.
1677 If the region spans two words, defer to extract_split_bit_field. */
1678 if (!MEM_P (op0
) && GET_MODE_SIZE (GET_MODE (op0
)) > UNITS_PER_WORD
)
1680 op0
= simplify_gen_subreg (word_mode
, op0
, GET_MODE (op0
),
1681 bitnum
/ BITS_PER_WORD
* UNITS_PER_WORD
);
1682 bitnum
%= BITS_PER_WORD
;
1683 if (bitnum
+ bitsize
> BITS_PER_WORD
)
1687 target
= extract_split_bit_field (op0
, bitsize
, bitnum
, unsignedp
);
1688 return convert_extracted_bit_field (target
, mode
, tmode
, unsignedp
);
1692 /* From here on we know the desired field is smaller than a word.
1693 If OP0 is a register, it too fits within a word. */
1694 enum extraction_pattern pattern
= unsignedp
? EP_extzv
: EP_extv
;
1695 extraction_insn extv
;
1697 /* ??? We could limit the structure size to the part of OP0 that
1698 contains the field, with appropriate checks for endianness
1699 and TRULY_NOOP_TRUNCATION. */
1700 && get_best_reg_extraction_insn (&extv
, pattern
,
1701 GET_MODE_BITSIZE (GET_MODE (op0
)),
1704 rtx result
= extract_bit_field_using_extv (&extv
, op0
, bitsize
, bitnum
,
1705 unsignedp
, target
, mode
,
1711 /* If OP0 is a memory, try copying it to a register and seeing if a
1712 cheap register alternative is available. */
1715 if (get_best_mem_extraction_insn (&extv
, pattern
, bitsize
, bitnum
,
1718 rtx result
= extract_bit_field_using_extv (&extv
, op0
, bitsize
,
1726 rtx_insn
*last
= get_last_insn ();
1728 /* Try loading part of OP0 into a register and extracting the
1729 bitfield from that. */
1730 unsigned HOST_WIDE_INT bitpos
;
1731 rtx xop0
= adjust_bit_field_mem_for_reg (pattern
, op0
, bitsize
, bitnum
,
1732 0, 0, tmode
, &bitpos
);
1735 xop0
= copy_to_reg (xop0
);
1736 rtx result
= extract_bit_field_1 (xop0
, bitsize
, bitpos
,
1738 mode
, tmode
, false);
1741 delete_insns_since (last
);
1748 /* Find a correspondingly-sized integer field, so we can apply
1749 shifts and masks to it. */
1750 int_mode
= int_mode_for_mode (tmode
);
1751 if (int_mode
== BLKmode
)
1752 int_mode
= int_mode_for_mode (mode
);
1753 /* Should probably push op0 out to memory and then do a load. */
1754 gcc_assert (int_mode
!= BLKmode
);
1756 target
= extract_fixed_bit_field (int_mode
, op0
, bitsize
, bitnum
,
1758 return convert_extracted_bit_field (target
, mode
, tmode
, unsignedp
);
1761 /* Generate code to extract a byte-field from STR_RTX
1762 containing BITSIZE bits, starting at BITNUM,
1763 and put it in TARGET if possible (if TARGET is nonzero).
1764 Regardless of TARGET, we return the rtx for where the value is placed.
1766 STR_RTX is the structure containing the byte (a REG or MEM).
1767 UNSIGNEDP is nonzero if this is an unsigned bit field.
1768 MODE is the natural mode of the field value once extracted.
1769 TMODE is the mode the caller would like the value to have;
1770 but the value may be returned with type MODE instead.
1772 If a TARGET is specified and we can store in it at no extra cost,
1773 we do so, and return TARGET.
1774 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1775 if they are equally easy. */
1778 extract_bit_field (rtx str_rtx
, unsigned HOST_WIDE_INT bitsize
,
1779 unsigned HOST_WIDE_INT bitnum
, int unsignedp
, rtx target
,
1780 machine_mode mode
, machine_mode tmode
)
1784 /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
1785 if (GET_MODE_BITSIZE (GET_MODE (str_rtx
)) > 0)
1786 mode1
= GET_MODE (str_rtx
);
1787 else if (target
&& GET_MODE_BITSIZE (GET_MODE (target
)) > 0)
1788 mode1
= GET_MODE (target
);
1792 if (strict_volatile_bitfield_p (str_rtx
, bitsize
, bitnum
, mode1
, 0, 0))
1794 /* Extraction of a full MODE1 value can be done with a simple load.
1795 We know here that the field can be accessed with one single
1796 instruction. For targets that support unaligned memory,
1797 an unaligned access may be necessary. */
1798 if (bitsize
== GET_MODE_BITSIZE (mode1
))
1800 rtx result
= adjust_bitfield_address (str_rtx
, mode1
,
1801 bitnum
/ BITS_PER_UNIT
);
1802 gcc_assert (bitnum
% BITS_PER_UNIT
== 0);
1803 return convert_extracted_bit_field (result
, mode
, tmode
, unsignedp
);
1806 str_rtx
= narrow_bit_field_mem (str_rtx
, mode1
, bitsize
, bitnum
,
1808 gcc_assert (bitnum
+ bitsize
<= GET_MODE_BITSIZE (mode1
));
1809 str_rtx
= copy_to_reg (str_rtx
);
1812 return extract_bit_field_1 (str_rtx
, bitsize
, bitnum
, unsignedp
,
1813 target
, mode
, tmode
, true);
1816 /* Use shifts and boolean operations to extract a field of BITSIZE bits
1817 from bit BITNUM of OP0.
1819 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1820 If TARGET is nonzero, attempts to store the value there
1821 and return TARGET, but this is not guaranteed.
1822 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
1825 extract_fixed_bit_field (machine_mode tmode
, rtx op0
,
1826 unsigned HOST_WIDE_INT bitsize
,
1827 unsigned HOST_WIDE_INT bitnum
, rtx target
,
1833 = get_best_mode (bitsize
, bitnum
, 0, 0, MEM_ALIGN (op0
), word_mode
,
1834 MEM_VOLATILE_P (op0
));
1836 if (mode
== VOIDmode
)
1837 /* The only way this should occur is if the field spans word
1839 return extract_split_bit_field (op0
, bitsize
, bitnum
, unsignedp
);
1841 op0
= narrow_bit_field_mem (op0
, mode
, bitsize
, bitnum
, &bitnum
);
1844 return extract_fixed_bit_field_1 (tmode
, op0
, bitsize
, bitnum
,
1848 /* Helper function for extract_fixed_bit_field, extracts
1849 the bit field always using the MODE of OP0. */
1852 extract_fixed_bit_field_1 (machine_mode tmode
, rtx op0
,
1853 unsigned HOST_WIDE_INT bitsize
,
1854 unsigned HOST_WIDE_INT bitnum
, rtx target
,
1857 machine_mode mode
= GET_MODE (op0
);
1858 gcc_assert (SCALAR_INT_MODE_P (mode
));
1860 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
1861 for invalid input, such as extract equivalent of f5 from
1862 gcc.dg/pr48335-2.c. */
1864 if (BYTES_BIG_ENDIAN
)
1865 /* BITNUM is the distance between our msb and that of OP0.
1866 Convert it to the distance from the lsb. */
1867 bitnum
= GET_MODE_BITSIZE (mode
) - bitsize
- bitnum
;
1869 /* Now BITNUM is always the distance between the field's lsb and that of OP0.
1870 We have reduced the big-endian case to the little-endian case. */
1876 /* If the field does not already start at the lsb,
1877 shift it so it does. */
1878 /* Maybe propagate the target for the shift. */
1879 rtx subtarget
= (target
!= 0 && REG_P (target
) ? target
: 0);
1882 op0
= expand_shift (RSHIFT_EXPR
, mode
, op0
, bitnum
, subtarget
, 1);
1884 /* Convert the value to the desired mode. */
1886 op0
= convert_to_mode (tmode
, op0
, 1);
1888 /* Unless the msb of the field used to be the msb when we shifted,
1889 mask out the upper bits. */
1891 if (GET_MODE_BITSIZE (mode
) != bitnum
+ bitsize
)
1892 return expand_binop (GET_MODE (op0
), and_optab
, op0
,
1893 mask_rtx (GET_MODE (op0
), 0, bitsize
, 0),
1894 target
, 1, OPTAB_LIB_WIDEN
);
1898 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1899 then arithmetic-shift its lsb to the lsb of the word. */
1900 op0
= force_reg (mode
, op0
);
1902 /* Find the narrowest integer mode that contains the field. */
1904 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1905 mode
= GET_MODE_WIDER_MODE (mode
))
1906 if (GET_MODE_BITSIZE (mode
) >= bitsize
+ bitnum
)
1908 op0
= convert_to_mode (mode
, op0
, 0);
1915 if (GET_MODE_BITSIZE (mode
) != (bitsize
+ bitnum
))
1917 int amount
= GET_MODE_BITSIZE (mode
) - (bitsize
+ bitnum
);
1918 /* Maybe propagate the target for the shift. */
1919 rtx subtarget
= (target
!= 0 && REG_P (target
) ? target
: 0);
1920 op0
= expand_shift (LSHIFT_EXPR
, mode
, op0
, amount
, subtarget
, 1);
1923 return expand_shift (RSHIFT_EXPR
, mode
, op0
,
1924 GET_MODE_BITSIZE (mode
) - bitsize
, target
, 0);
1927 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1931 lshift_value (machine_mode mode
, unsigned HOST_WIDE_INT value
,
1934 return immed_wide_int_const (wi::lshift (value
, bitpos
), mode
);
1937 /* Extract a bit field that is split across two words
1938 and return an RTX for the result.
1940 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1941 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1942 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
1945 extract_split_bit_field (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
1946 unsigned HOST_WIDE_INT bitpos
, int unsignedp
)
1949 unsigned int bitsdone
= 0;
1950 rtx result
= NULL_RTX
;
1953 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1955 if (REG_P (op0
) || GET_CODE (op0
) == SUBREG
)
1956 unit
= BITS_PER_WORD
;
1958 unit
= MIN (MEM_ALIGN (op0
), BITS_PER_WORD
);
1960 while (bitsdone
< bitsize
)
1962 unsigned HOST_WIDE_INT thissize
;
1964 unsigned HOST_WIDE_INT thispos
;
1965 unsigned HOST_WIDE_INT offset
;
1967 offset
= (bitpos
+ bitsdone
) / unit
;
1968 thispos
= (bitpos
+ bitsdone
) % unit
;
1970 /* THISSIZE must not overrun a word boundary. Otherwise,
1971 extract_fixed_bit_field will call us again, and we will mutually
1973 thissize
= MIN (bitsize
- bitsdone
, BITS_PER_WORD
);
1974 thissize
= MIN (thissize
, unit
- thispos
);
1976 /* If OP0 is a register, then handle OFFSET here.
1978 When handling multiword bitfields, extract_bit_field may pass
1979 down a word_mode SUBREG of a larger REG for a bitfield that actually
1980 crosses a word boundary. Thus, for a SUBREG, we must find
1981 the current word starting from the base register. */
1982 if (GET_CODE (op0
) == SUBREG
)
1984 int word_offset
= (SUBREG_BYTE (op0
) / UNITS_PER_WORD
) + offset
;
1985 word
= operand_subword_force (SUBREG_REG (op0
), word_offset
,
1986 GET_MODE (SUBREG_REG (op0
)));
1989 else if (REG_P (op0
))
1991 word
= operand_subword_force (op0
, offset
, GET_MODE (op0
));
1997 /* Extract the parts in bit-counting order,
1998 whose meaning is determined by BYTES_PER_UNIT.
1999 OFFSET is in UNITs, and UNIT is in bits. */
2000 part
= extract_fixed_bit_field (word_mode
, word
, thissize
,
2001 offset
* unit
+ thispos
, 0, 1);
2002 bitsdone
+= thissize
;
2004 /* Shift this part into place for the result. */
2005 if (BYTES_BIG_ENDIAN
)
2007 if (bitsize
!= bitsdone
)
2008 part
= expand_shift (LSHIFT_EXPR
, word_mode
, part
,
2009 bitsize
- bitsdone
, 0, 1);
2013 if (bitsdone
!= thissize
)
2014 part
= expand_shift (LSHIFT_EXPR
, word_mode
, part
,
2015 bitsdone
- thissize
, 0, 1);
2021 /* Combine the parts with bitwise or. This works
2022 because we extracted each part as an unsigned bit field. */
2023 result
= expand_binop (word_mode
, ior_optab
, part
, result
, NULL_RTX
, 1,
2029 /* Unsigned bit field: we are done. */
2032 /* Signed bit field: sign-extend with two arithmetic shifts. */
2033 result
= expand_shift (LSHIFT_EXPR
, word_mode
, result
,
2034 BITS_PER_WORD
- bitsize
, NULL_RTX
, 0);
2035 return expand_shift (RSHIFT_EXPR
, word_mode
, result
,
2036 BITS_PER_WORD
- bitsize
, NULL_RTX
, 0);
2039 /* Try to read the low bits of SRC as an rvalue of mode MODE, preserving
2040 the bit pattern. SRC_MODE is the mode of SRC; if this is smaller than
2041 MODE, fill the upper bits with zeros. Fail if the layout of either
2042 mode is unknown (as for CC modes) or if the extraction would involve
2043 unprofitable mode punning. Return the value on success, otherwise
2046 This is different from gen_lowpart* in these respects:
2048 - the returned value must always be considered an rvalue
2050 - when MODE is wider than SRC_MODE, the extraction involves
2053 - when MODE is smaller than SRC_MODE, the extraction involves
2054 a truncation (and is thus subject to TRULY_NOOP_TRUNCATION).
2056 In other words, this routine performs a computation, whereas the
2057 gen_lowpart* routines are conceptually lvalue or rvalue subreg
2061 extract_low_bits (machine_mode mode
, machine_mode src_mode
, rtx src
)
2063 machine_mode int_mode
, src_int_mode
;
2065 if (mode
== src_mode
)
2068 if (CONSTANT_P (src
))
2070 /* simplify_gen_subreg can't be used here, as if simplify_subreg
2071 fails, it will happily create (subreg (symbol_ref)) or similar
2073 unsigned int byte
= subreg_lowpart_offset (mode
, src_mode
);
2074 rtx ret
= simplify_subreg (mode
, src
, src_mode
, byte
);
2078 if (GET_MODE (src
) == VOIDmode
2079 || !validate_subreg (mode
, src_mode
, src
, byte
))
2082 src
= force_reg (GET_MODE (src
), src
);
2083 return gen_rtx_SUBREG (mode
, src
, byte
);
2086 if (GET_MODE_CLASS (mode
) == MODE_CC
|| GET_MODE_CLASS (src_mode
) == MODE_CC
)
2089 if (GET_MODE_BITSIZE (mode
) == GET_MODE_BITSIZE (src_mode
)
2090 && MODES_TIEABLE_P (mode
, src_mode
))
2092 rtx x
= gen_lowpart_common (mode
, src
);
2097 src_int_mode
= int_mode_for_mode (src_mode
);
2098 int_mode
= int_mode_for_mode (mode
);
2099 if (src_int_mode
== BLKmode
|| int_mode
== BLKmode
)
2102 if (!MODES_TIEABLE_P (src_int_mode
, src_mode
))
2104 if (!MODES_TIEABLE_P (int_mode
, mode
))
2107 src
= gen_lowpart (src_int_mode
, src
);
2108 src
= convert_modes (int_mode
, src_int_mode
, src
, true);
2109 src
= gen_lowpart (mode
, src
);
2113 /* Add INC into TARGET. */
2116 expand_inc (rtx target
, rtx inc
)
2118 rtx value
= expand_binop (GET_MODE (target
), add_optab
,
2120 target
, 0, OPTAB_LIB_WIDEN
);
2121 if (value
!= target
)
2122 emit_move_insn (target
, value
);
2125 /* Subtract DEC from TARGET. */
2128 expand_dec (rtx target
, rtx dec
)
2130 rtx value
= expand_binop (GET_MODE (target
), sub_optab
,
2132 target
, 0, OPTAB_LIB_WIDEN
);
2133 if (value
!= target
)
2134 emit_move_insn (target
, value
);
2137 /* Output a shift instruction for expression code CODE,
2138 with SHIFTED being the rtx for the value to shift,
2139 and AMOUNT the rtx for the amount to shift by.
2140 Store the result in the rtx TARGET, if that is convenient.
2141 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2142 Return the rtx for where the value is. */
2145 expand_shift_1 (enum tree_code code
, machine_mode mode
, rtx shifted
,
2146 rtx amount
, rtx target
, int unsignedp
)
2149 int left
= (code
== LSHIFT_EXPR
|| code
== LROTATE_EXPR
);
2150 int rotate
= (code
== LROTATE_EXPR
|| code
== RROTATE_EXPR
);
2151 optab lshift_optab
= ashl_optab
;
2152 optab rshift_arith_optab
= ashr_optab
;
2153 optab rshift_uns_optab
= lshr_optab
;
2154 optab lrotate_optab
= rotl_optab
;
2155 optab rrotate_optab
= rotr_optab
;
2156 machine_mode op1_mode
;
2157 machine_mode scalar_mode
= mode
;
2159 bool speed
= optimize_insn_for_speed_p ();
2161 if (VECTOR_MODE_P (mode
))
2162 scalar_mode
= GET_MODE_INNER (mode
);
2164 op1_mode
= GET_MODE (op1
);
2166 /* Determine whether the shift/rotate amount is a vector, or scalar. If the
2167 shift amount is a vector, use the vector/vector shift patterns. */
2168 if (VECTOR_MODE_P (mode
) && VECTOR_MODE_P (op1_mode
))
2170 lshift_optab
= vashl_optab
;
2171 rshift_arith_optab
= vashr_optab
;
2172 rshift_uns_optab
= vlshr_optab
;
2173 lrotate_optab
= vrotl_optab
;
2174 rrotate_optab
= vrotr_optab
;
2177 /* Previously detected shift-counts computed by NEGATE_EXPR
2178 and shifted in the other direction; but that does not work
2181 if (SHIFT_COUNT_TRUNCATED
)
2183 if (CONST_INT_P (op1
)
2184 && ((unsigned HOST_WIDE_INT
) INTVAL (op1
) >=
2185 (unsigned HOST_WIDE_INT
) GET_MODE_BITSIZE (scalar_mode
)))
2186 op1
= GEN_INT ((unsigned HOST_WIDE_INT
) INTVAL (op1
)
2187 % GET_MODE_BITSIZE (scalar_mode
));
2188 else if (GET_CODE (op1
) == SUBREG
2189 && subreg_lowpart_p (op1
)
2190 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op1
)))
2191 && SCALAR_INT_MODE_P (GET_MODE (op1
)))
2192 op1
= SUBREG_REG (op1
);
2195 /* Canonicalize rotates by constant amount. If op1 is bitsize / 2,
2196 prefer left rotation, if op1 is from bitsize / 2 + 1 to
2197 bitsize - 1, use other direction of rotate with 1 .. bitsize / 2 - 1
2200 && CONST_INT_P (op1
)
2201 && IN_RANGE (INTVAL (op1
), GET_MODE_BITSIZE (scalar_mode
) / 2 + left
,
2202 GET_MODE_BITSIZE (scalar_mode
) - 1))
2204 op1
= GEN_INT (GET_MODE_BITSIZE (scalar_mode
) - INTVAL (op1
));
2206 code
= left
? LROTATE_EXPR
: RROTATE_EXPR
;
2209 /* Rotation of 16bit values by 8 bits is effectively equivalent to a bswaphi.
2210 Note that this is not the case for bigger values. For instance a rotation
2211 of 0x01020304 by 16 bits gives 0x03040102 which is different from
2212 0x04030201 (bswapsi). */
2214 && CONST_INT_P (op1
)
2215 && INTVAL (op1
) == BITS_PER_UNIT
2216 && GET_MODE_SIZE (scalar_mode
) == 2
2217 && optab_handler (bswap_optab
, HImode
) != CODE_FOR_nothing
)
2218 return expand_unop (HImode
, bswap_optab
, shifted
, NULL_RTX
,
2221 if (op1
== const0_rtx
)
2224 /* Check whether its cheaper to implement a left shift by a constant
2225 bit count by a sequence of additions. */
2226 if (code
== LSHIFT_EXPR
2227 && CONST_INT_P (op1
)
2229 && INTVAL (op1
) < GET_MODE_PRECISION (scalar_mode
)
2230 && INTVAL (op1
) < MAX_BITS_PER_WORD
2231 && (shift_cost (speed
, mode
, INTVAL (op1
))
2232 > INTVAL (op1
) * add_cost (speed
, mode
))
2233 && shift_cost (speed
, mode
, INTVAL (op1
)) != MAX_COST
)
2236 for (i
= 0; i
< INTVAL (op1
); i
++)
2238 temp
= force_reg (mode
, shifted
);
2239 shifted
= expand_binop (mode
, add_optab
, temp
, temp
, NULL_RTX
,
2240 unsignedp
, OPTAB_LIB_WIDEN
);
2245 for (attempt
= 0; temp
== 0 && attempt
< 3; attempt
++)
2247 enum optab_methods methods
;
2250 methods
= OPTAB_DIRECT
;
2251 else if (attempt
== 1)
2252 methods
= OPTAB_WIDEN
;
2254 methods
= OPTAB_LIB_WIDEN
;
2258 /* Widening does not work for rotation. */
2259 if (methods
== OPTAB_WIDEN
)
2261 else if (methods
== OPTAB_LIB_WIDEN
)
2263 /* If we have been unable to open-code this by a rotation,
2264 do it as the IOR of two shifts. I.e., to rotate A
2266 (A << N) | ((unsigned) A >> ((-N) & (C - 1)))
2267 where C is the bitsize of A.
2269 It is theoretically possible that the target machine might
2270 not be able to perform either shift and hence we would
2271 be making two libcalls rather than just the one for the
2272 shift (similarly if IOR could not be done). We will allow
2273 this extremely unlikely lossage to avoid complicating the
2276 rtx subtarget
= target
== shifted
? 0 : target
;
2277 rtx new_amount
, other_amount
;
2281 if (op1
== const0_rtx
)
2283 else if (CONST_INT_P (op1
))
2284 other_amount
= GEN_INT (GET_MODE_BITSIZE (scalar_mode
)
2289 = simplify_gen_unary (NEG
, GET_MODE (op1
),
2290 op1
, GET_MODE (op1
));
2291 HOST_WIDE_INT mask
= GET_MODE_PRECISION (scalar_mode
) - 1;
2293 = simplify_gen_binary (AND
, GET_MODE (op1
), other_amount
,
2294 gen_int_mode (mask
, GET_MODE (op1
)));
2297 shifted
= force_reg (mode
, shifted
);
2299 temp
= expand_shift_1 (left
? LSHIFT_EXPR
: RSHIFT_EXPR
,
2300 mode
, shifted
, new_amount
, 0, 1);
2301 temp1
= expand_shift_1 (left
? RSHIFT_EXPR
: LSHIFT_EXPR
,
2302 mode
, shifted
, other_amount
,
2304 return expand_binop (mode
, ior_optab
, temp
, temp1
, target
,
2305 unsignedp
, methods
);
2308 temp
= expand_binop (mode
,
2309 left
? lrotate_optab
: rrotate_optab
,
2310 shifted
, op1
, target
, unsignedp
, methods
);
2313 temp
= expand_binop (mode
,
2314 left
? lshift_optab
: rshift_uns_optab
,
2315 shifted
, op1
, target
, unsignedp
, methods
);
2317 /* Do arithmetic shifts.
2318 Also, if we are going to widen the operand, we can just as well
2319 use an arithmetic right-shift instead of a logical one. */
2320 if (temp
== 0 && ! rotate
2321 && (! unsignedp
|| (! left
&& methods
== OPTAB_WIDEN
)))
2323 enum optab_methods methods1
= methods
;
2325 /* If trying to widen a log shift to an arithmetic shift,
2326 don't accept an arithmetic shift of the same size. */
2328 methods1
= OPTAB_MUST_WIDEN
;
2330 /* Arithmetic shift */
2332 temp
= expand_binop (mode
,
2333 left
? lshift_optab
: rshift_arith_optab
,
2334 shifted
, op1
, target
, unsignedp
, methods1
);
2337 /* We used to try extzv here for logical right shifts, but that was
2338 only useful for one machine, the VAX, and caused poor code
2339 generation there for lshrdi3, so the code was deleted and a
2340 define_expand for lshrsi3 was added to vax.md. */
2347 /* Output a shift instruction for expression code CODE,
2348 with SHIFTED being the rtx for the value to shift,
2349 and AMOUNT the amount to shift by.
2350 Store the result in the rtx TARGET, if that is convenient.
2351 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2352 Return the rtx for where the value is. */
2355 expand_shift (enum tree_code code
, machine_mode mode
, rtx shifted
,
2356 int amount
, rtx target
, int unsignedp
)
2358 return expand_shift_1 (code
, mode
,
2359 shifted
, GEN_INT (amount
), target
, unsignedp
);
2362 /* Output a shift instruction for expression code CODE,
2363 with SHIFTED being the rtx for the value to shift,
2364 and AMOUNT the tree for the amount to shift by.
2365 Store the result in the rtx TARGET, if that is convenient.
2366 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2367 Return the rtx for where the value is. */
2370 expand_variable_shift (enum tree_code code
, machine_mode mode
, rtx shifted
,
2371 tree amount
, rtx target
, int unsignedp
)
2373 return expand_shift_1 (code
, mode
,
2374 shifted
, expand_normal (amount
), target
, unsignedp
);
2378 /* Indicates the type of fixup needed after a constant multiplication.
2379 BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2380 the result should be negated, and ADD_VARIANT means that the
2381 multiplicand should be added to the result. */
2382 enum mult_variant
{basic_variant
, negate_variant
, add_variant
};
2384 static void synth_mult (struct algorithm
*, unsigned HOST_WIDE_INT
,
2385 const struct mult_cost
*, machine_mode mode
);
2386 static bool choose_mult_variant (machine_mode
, HOST_WIDE_INT
,
2387 struct algorithm
*, enum mult_variant
*, int);
2388 static rtx
expand_mult_const (machine_mode
, rtx
, HOST_WIDE_INT
, rtx
,
2389 const struct algorithm
*, enum mult_variant
);
2390 static unsigned HOST_WIDE_INT
invert_mod2n (unsigned HOST_WIDE_INT
, int);
2391 static rtx
extract_high_half (machine_mode
, rtx
);
2392 static rtx
expmed_mult_highpart (machine_mode
, rtx
, rtx
, rtx
, int, int);
2393 static rtx
expmed_mult_highpart_optab (machine_mode
, rtx
, rtx
, rtx
,
2395 /* Compute and return the best algorithm for multiplying by T.
2396 The algorithm must cost less than cost_limit
2397 If retval.cost >= COST_LIMIT, no algorithm was found and all
2398 other field of the returned struct are undefined.
2399 MODE is the machine mode of the multiplication. */
2402 synth_mult (struct algorithm
*alg_out
, unsigned HOST_WIDE_INT t
,
2403 const struct mult_cost
*cost_limit
, machine_mode mode
)
2406 struct algorithm
*alg_in
, *best_alg
;
2407 struct mult_cost best_cost
;
2408 struct mult_cost new_limit
;
2409 int op_cost
, op_latency
;
2410 unsigned HOST_WIDE_INT orig_t
= t
;
2411 unsigned HOST_WIDE_INT q
;
2412 int maxm
, hash_index
;
2413 bool cache_hit
= false;
2414 enum alg_code cache_alg
= alg_zero
;
2415 bool speed
= optimize_insn_for_speed_p ();
2417 struct alg_hash_entry
*entry_ptr
;
2419 /* Indicate that no algorithm is yet found. If no algorithm
2420 is found, this value will be returned and indicate failure. */
2421 alg_out
->cost
.cost
= cost_limit
->cost
+ 1;
2422 alg_out
->cost
.latency
= cost_limit
->latency
+ 1;
2424 if (cost_limit
->cost
< 0
2425 || (cost_limit
->cost
== 0 && cost_limit
->latency
<= 0))
2428 /* Be prepared for vector modes. */
2429 imode
= GET_MODE_INNER (mode
);
2431 maxm
= MIN (BITS_PER_WORD
, GET_MODE_BITSIZE (imode
));
2433 /* Restrict the bits of "t" to the multiplication's mode. */
2434 t
&= GET_MODE_MASK (imode
);
2436 /* t == 1 can be done in zero cost. */
2440 alg_out
->cost
.cost
= 0;
2441 alg_out
->cost
.latency
= 0;
2442 alg_out
->op
[0] = alg_m
;
2446 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2450 if (MULT_COST_LESS (cost_limit
, zero_cost (speed
)))
2455 alg_out
->cost
.cost
= zero_cost (speed
);
2456 alg_out
->cost
.latency
= zero_cost (speed
);
2457 alg_out
->op
[0] = alg_zero
;
2462 /* We'll be needing a couple extra algorithm structures now. */
2464 alg_in
= XALLOCA (struct algorithm
);
2465 best_alg
= XALLOCA (struct algorithm
);
2466 best_cost
= *cost_limit
;
2468 /* Compute the hash index. */
2469 hash_index
= (t
^ (unsigned int) mode
^ (speed
* 256)) % NUM_ALG_HASH_ENTRIES
;
2471 /* See if we already know what to do for T. */
2472 entry_ptr
= alg_hash_entry_ptr (hash_index
);
2473 if (entry_ptr
->t
== t
2474 && entry_ptr
->mode
== mode
2475 && entry_ptr
->mode
== mode
2476 && entry_ptr
->speed
== speed
2477 && entry_ptr
->alg
!= alg_unknown
)
2479 cache_alg
= entry_ptr
->alg
;
2481 if (cache_alg
== alg_impossible
)
2483 /* The cache tells us that it's impossible to synthesize
2484 multiplication by T within entry_ptr->cost. */
2485 if (!CHEAPER_MULT_COST (&entry_ptr
->cost
, cost_limit
))
2486 /* COST_LIMIT is at least as restrictive as the one
2487 recorded in the hash table, in which case we have no
2488 hope of synthesizing a multiplication. Just
2492 /* If we get here, COST_LIMIT is less restrictive than the
2493 one recorded in the hash table, so we may be able to
2494 synthesize a multiplication. Proceed as if we didn't
2495 have the cache entry. */
2499 if (CHEAPER_MULT_COST (cost_limit
, &entry_ptr
->cost
))
2500 /* The cached algorithm shows that this multiplication
2501 requires more cost than COST_LIMIT. Just return. This
2502 way, we don't clobber this cache entry with
2503 alg_impossible but retain useful information. */
2515 goto do_alg_addsub_t_m2
;
2517 case alg_add_factor
:
2518 case alg_sub_factor
:
2519 goto do_alg_addsub_factor
;
2522 goto do_alg_add_t2_m
;
2525 goto do_alg_sub_t2_m
;
2533 /* If we have a group of zero bits at the low-order part of T, try
2534 multiplying by the remaining bits and then doing a shift. */
2539 m
= floor_log2 (t
& -t
); /* m = number of low zero bits */
2543 /* The function expand_shift will choose between a shift and
2544 a sequence of additions, so the observed cost is given as
2545 MIN (m * add_cost(speed, mode), shift_cost(speed, mode, m)). */
2546 op_cost
= m
* add_cost (speed
, mode
);
2547 if (shift_cost (speed
, mode
, m
) < op_cost
)
2548 op_cost
= shift_cost (speed
, mode
, m
);
2549 new_limit
.cost
= best_cost
.cost
- op_cost
;
2550 new_limit
.latency
= best_cost
.latency
- op_cost
;
2551 synth_mult (alg_in
, q
, &new_limit
, mode
);
2553 alg_in
->cost
.cost
+= op_cost
;
2554 alg_in
->cost
.latency
+= op_cost
;
2555 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2557 best_cost
= alg_in
->cost
;
2558 std::swap (alg_in
, best_alg
);
2559 best_alg
->log
[best_alg
->ops
] = m
;
2560 best_alg
->op
[best_alg
->ops
] = alg_shift
;
2563 /* See if treating ORIG_T as a signed number yields a better
2564 sequence. Try this sequence only for a negative ORIG_T
2565 as it would be useless for a non-negative ORIG_T. */
2566 if ((HOST_WIDE_INT
) orig_t
< 0)
2568 /* Shift ORIG_T as follows because a right shift of a
2569 negative-valued signed type is implementation
2571 q
= ~(~orig_t
>> m
);
2572 /* The function expand_shift will choose between a shift
2573 and a sequence of additions, so the observed cost is
2574 given as MIN (m * add_cost(speed, mode),
2575 shift_cost(speed, mode, m)). */
2576 op_cost
= m
* add_cost (speed
, mode
);
2577 if (shift_cost (speed
, mode
, m
) < op_cost
)
2578 op_cost
= shift_cost (speed
, mode
, m
);
2579 new_limit
.cost
= best_cost
.cost
- op_cost
;
2580 new_limit
.latency
= best_cost
.latency
- op_cost
;
2581 synth_mult (alg_in
, q
, &new_limit
, mode
);
2583 alg_in
->cost
.cost
+= op_cost
;
2584 alg_in
->cost
.latency
+= op_cost
;
2585 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2587 best_cost
= alg_in
->cost
;
2588 std::swap (alg_in
, best_alg
);
2589 best_alg
->log
[best_alg
->ops
] = m
;
2590 best_alg
->op
[best_alg
->ops
] = alg_shift
;
2598 /* If we have an odd number, add or subtract one. */
2601 unsigned HOST_WIDE_INT w
;
2604 for (w
= 1; (w
& t
) != 0; w
<<= 1)
2606 /* If T was -1, then W will be zero after the loop. This is another
2607 case where T ends with ...111. Handling this with (T + 1) and
2608 subtract 1 produces slightly better code and results in algorithm
2609 selection much faster than treating it like the ...0111 case
2613 /* Reject the case where t is 3.
2614 Thus we prefer addition in that case. */
2617 /* T ends with ...111. Multiply by (T + 1) and subtract T. */
2619 op_cost
= add_cost (speed
, mode
);
2620 new_limit
.cost
= best_cost
.cost
- op_cost
;
2621 new_limit
.latency
= best_cost
.latency
- op_cost
;
2622 synth_mult (alg_in
, t
+ 1, &new_limit
, mode
);
2624 alg_in
->cost
.cost
+= op_cost
;
2625 alg_in
->cost
.latency
+= op_cost
;
2626 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2628 best_cost
= alg_in
->cost
;
2629 std::swap (alg_in
, best_alg
);
2630 best_alg
->log
[best_alg
->ops
] = 0;
2631 best_alg
->op
[best_alg
->ops
] = alg_sub_t_m2
;
2636 /* T ends with ...01 or ...011. Multiply by (T - 1) and add T. */
2638 op_cost
= add_cost (speed
, mode
);
2639 new_limit
.cost
= best_cost
.cost
- op_cost
;
2640 new_limit
.latency
= best_cost
.latency
- op_cost
;
2641 synth_mult (alg_in
, t
- 1, &new_limit
, mode
);
2643 alg_in
->cost
.cost
+= op_cost
;
2644 alg_in
->cost
.latency
+= op_cost
;
2645 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2647 best_cost
= alg_in
->cost
;
2648 std::swap (alg_in
, best_alg
);
2649 best_alg
->log
[best_alg
->ops
] = 0;
2650 best_alg
->op
[best_alg
->ops
] = alg_add_t_m2
;
2654 /* We may be able to calculate a * -7, a * -15, a * -31, etc
2655 quickly with a - a * n for some appropriate constant n. */
2656 m
= exact_log2 (-orig_t
+ 1);
2657 if (m
>= 0 && m
< maxm
)
2659 op_cost
= add_cost (speed
, mode
) + shift_cost (speed
, mode
, m
);
2660 /* If the target has a cheap shift-and-subtract insn use
2661 that in preference to a shift insn followed by a sub insn.
2662 Assume that the shift-and-sub is "atomic" with a latency
2663 equal to it's cost, otherwise assume that on superscalar
2664 hardware the shift may be executed concurrently with the
2665 earlier steps in the algorithm. */
2666 if (shiftsub1_cost (speed
, mode
, m
) <= op_cost
)
2668 op_cost
= shiftsub1_cost (speed
, mode
, m
);
2669 op_latency
= op_cost
;
2672 op_latency
= add_cost (speed
, mode
);
2674 new_limit
.cost
= best_cost
.cost
- op_cost
;
2675 new_limit
.latency
= best_cost
.latency
- op_latency
;
2676 synth_mult (alg_in
, (unsigned HOST_WIDE_INT
) (-orig_t
+ 1) >> m
,
2679 alg_in
->cost
.cost
+= op_cost
;
2680 alg_in
->cost
.latency
+= op_latency
;
2681 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2683 best_cost
= alg_in
->cost
;
2684 std::swap (alg_in
, best_alg
);
2685 best_alg
->log
[best_alg
->ops
] = m
;
2686 best_alg
->op
[best_alg
->ops
] = alg_sub_t_m2
;
2694 /* Look for factors of t of the form
2695 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2696 If we find such a factor, we can multiply by t using an algorithm that
2697 multiplies by q, shift the result by m and add/subtract it to itself.
2699 We search for large factors first and loop down, even if large factors
2700 are less probable than small; if we find a large factor we will find a
2701 good sequence quickly, and therefore be able to prune (by decreasing
2702 COST_LIMIT) the search. */
2704 do_alg_addsub_factor
:
2705 for (m
= floor_log2 (t
- 1); m
>= 2; m
--)
2707 unsigned HOST_WIDE_INT d
;
2709 d
= ((unsigned HOST_WIDE_INT
) 1 << m
) + 1;
2710 if (t
% d
== 0 && t
> d
&& m
< maxm
2711 && (!cache_hit
|| cache_alg
== alg_add_factor
))
2713 op_cost
= add_cost (speed
, mode
) + shift_cost (speed
, mode
, m
);
2714 if (shiftadd_cost (speed
, mode
, m
) <= op_cost
)
2715 op_cost
= shiftadd_cost (speed
, mode
, m
);
2717 op_latency
= op_cost
;
2720 new_limit
.cost
= best_cost
.cost
- op_cost
;
2721 new_limit
.latency
= best_cost
.latency
- op_latency
;
2722 synth_mult (alg_in
, t
/ d
, &new_limit
, mode
);
2724 alg_in
->cost
.cost
+= op_cost
;
2725 alg_in
->cost
.latency
+= op_latency
;
2726 if (alg_in
->cost
.latency
< op_cost
)
2727 alg_in
->cost
.latency
= op_cost
;
2728 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2730 best_cost
= alg_in
->cost
;
2731 std::swap (alg_in
, best_alg
);
2732 best_alg
->log
[best_alg
->ops
] = m
;
2733 best_alg
->op
[best_alg
->ops
] = alg_add_factor
;
2735 /* Other factors will have been taken care of in the recursion. */
2739 d
= ((unsigned HOST_WIDE_INT
) 1 << m
) - 1;
2740 if (t
% d
== 0 && t
> d
&& m
< maxm
2741 && (!cache_hit
|| cache_alg
== alg_sub_factor
))
2743 op_cost
= add_cost (speed
, mode
) + shift_cost (speed
, mode
, m
);
2744 if (shiftsub0_cost (speed
, mode
, m
) <= op_cost
)
2745 op_cost
= shiftsub0_cost (speed
, mode
, m
);
2747 op_latency
= op_cost
;
2749 new_limit
.cost
= best_cost
.cost
- op_cost
;
2750 new_limit
.latency
= best_cost
.latency
- op_latency
;
2751 synth_mult (alg_in
, t
/ d
, &new_limit
, mode
);
2753 alg_in
->cost
.cost
+= op_cost
;
2754 alg_in
->cost
.latency
+= op_latency
;
2755 if (alg_in
->cost
.latency
< op_cost
)
2756 alg_in
->cost
.latency
= op_cost
;
2757 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2759 best_cost
= alg_in
->cost
;
2760 std::swap (alg_in
, best_alg
);
2761 best_alg
->log
[best_alg
->ops
] = m
;
2762 best_alg
->op
[best_alg
->ops
] = alg_sub_factor
;
2770 /* Try shift-and-add (load effective address) instructions,
2771 i.e. do a*3, a*5, a*9. */
2778 if (m
>= 0 && m
< maxm
)
2780 op_cost
= shiftadd_cost (speed
, mode
, m
);
2781 new_limit
.cost
= best_cost
.cost
- op_cost
;
2782 new_limit
.latency
= best_cost
.latency
- op_cost
;
2783 synth_mult (alg_in
, (t
- 1) >> m
, &new_limit
, mode
);
2785 alg_in
->cost
.cost
+= op_cost
;
2786 alg_in
->cost
.latency
+= op_cost
;
2787 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2789 best_cost
= alg_in
->cost
;
2790 std::swap (alg_in
, best_alg
);
2791 best_alg
->log
[best_alg
->ops
] = m
;
2792 best_alg
->op
[best_alg
->ops
] = alg_add_t2_m
;
2802 if (m
>= 0 && m
< maxm
)
2804 op_cost
= shiftsub0_cost (speed
, mode
, m
);
2805 new_limit
.cost
= best_cost
.cost
- op_cost
;
2806 new_limit
.latency
= best_cost
.latency
- op_cost
;
2807 synth_mult (alg_in
, (t
+ 1) >> m
, &new_limit
, mode
);
2809 alg_in
->cost
.cost
+= op_cost
;
2810 alg_in
->cost
.latency
+= op_cost
;
2811 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2813 best_cost
= alg_in
->cost
;
2814 std::swap (alg_in
, best_alg
);
2815 best_alg
->log
[best_alg
->ops
] = m
;
2816 best_alg
->op
[best_alg
->ops
] = alg_sub_t2_m
;
2824 /* If best_cost has not decreased, we have not found any algorithm. */
2825 if (!CHEAPER_MULT_COST (&best_cost
, cost_limit
))
2827 /* We failed to find an algorithm. Record alg_impossible for
2828 this case (that is, <T, MODE, COST_LIMIT>) so that next time
2829 we are asked to find an algorithm for T within the same or
2830 lower COST_LIMIT, we can immediately return to the
2833 entry_ptr
->mode
= mode
;
2834 entry_ptr
->speed
= speed
;
2835 entry_ptr
->alg
= alg_impossible
;
2836 entry_ptr
->cost
= *cost_limit
;
2840 /* Cache the result. */
2844 entry_ptr
->mode
= mode
;
2845 entry_ptr
->speed
= speed
;
2846 entry_ptr
->alg
= best_alg
->op
[best_alg
->ops
];
2847 entry_ptr
->cost
.cost
= best_cost
.cost
;
2848 entry_ptr
->cost
.latency
= best_cost
.latency
;
2851 /* If we are getting a too long sequence for `struct algorithm'
2852 to record, make this search fail. */
2853 if (best_alg
->ops
== MAX_BITS_PER_WORD
)
2856 /* Copy the algorithm from temporary space to the space at alg_out.
2857 We avoid using structure assignment because the majority of
2858 best_alg is normally undefined, and this is a critical function. */
2859 alg_out
->ops
= best_alg
->ops
+ 1;
2860 alg_out
->cost
= best_cost
;
2861 memcpy (alg_out
->op
, best_alg
->op
,
2862 alg_out
->ops
* sizeof *alg_out
->op
);
2863 memcpy (alg_out
->log
, best_alg
->log
,
2864 alg_out
->ops
* sizeof *alg_out
->log
);
2867 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2868 Try three variations:
2870 - a shift/add sequence based on VAL itself
2871 - a shift/add sequence based on -VAL, followed by a negation
2872 - a shift/add sequence based on VAL - 1, followed by an addition.
2874 Return true if the cheapest of these cost less than MULT_COST,
2875 describing the algorithm in *ALG and final fixup in *VARIANT. */
2878 choose_mult_variant (machine_mode mode
, HOST_WIDE_INT val
,
2879 struct algorithm
*alg
, enum mult_variant
*variant
,
2882 struct algorithm alg2
;
2883 struct mult_cost limit
;
2885 bool speed
= optimize_insn_for_speed_p ();
2887 /* Fail quickly for impossible bounds. */
2891 /* Ensure that mult_cost provides a reasonable upper bound.
2892 Any constant multiplication can be performed with less
2893 than 2 * bits additions. */
2894 op_cost
= 2 * GET_MODE_UNIT_BITSIZE (mode
) * add_cost (speed
, mode
);
2895 if (mult_cost
> op_cost
)
2896 mult_cost
= op_cost
;
2898 *variant
= basic_variant
;
2899 limit
.cost
= mult_cost
;
2900 limit
.latency
= mult_cost
;
2901 synth_mult (alg
, val
, &limit
, mode
);
2903 /* This works only if the inverted value actually fits in an
2905 if (HOST_BITS_PER_INT
>= GET_MODE_UNIT_BITSIZE (mode
))
2907 op_cost
= neg_cost (speed
, mode
);
2908 if (MULT_COST_LESS (&alg
->cost
, mult_cost
))
2910 limit
.cost
= alg
->cost
.cost
- op_cost
;
2911 limit
.latency
= alg
->cost
.latency
- op_cost
;
2915 limit
.cost
= mult_cost
- op_cost
;
2916 limit
.latency
= mult_cost
- op_cost
;
2919 synth_mult (&alg2
, -val
, &limit
, mode
);
2920 alg2
.cost
.cost
+= op_cost
;
2921 alg2
.cost
.latency
+= op_cost
;
2922 if (CHEAPER_MULT_COST (&alg2
.cost
, &alg
->cost
))
2923 *alg
= alg2
, *variant
= negate_variant
;
2926 /* This proves very useful for division-by-constant. */
2927 op_cost
= add_cost (speed
, mode
);
2928 if (MULT_COST_LESS (&alg
->cost
, mult_cost
))
2930 limit
.cost
= alg
->cost
.cost
- op_cost
;
2931 limit
.latency
= alg
->cost
.latency
- op_cost
;
2935 limit
.cost
= mult_cost
- op_cost
;
2936 limit
.latency
= mult_cost
- op_cost
;
2939 synth_mult (&alg2
, val
- 1, &limit
, mode
);
2940 alg2
.cost
.cost
+= op_cost
;
2941 alg2
.cost
.latency
+= op_cost
;
2942 if (CHEAPER_MULT_COST (&alg2
.cost
, &alg
->cost
))
2943 *alg
= alg2
, *variant
= add_variant
;
2945 return MULT_COST_LESS (&alg
->cost
, mult_cost
);
2948 /* A subroutine of expand_mult, used for constant multiplications.
2949 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2950 convenient. Use the shift/add sequence described by ALG and apply
2951 the final fixup specified by VARIANT. */
2954 expand_mult_const (machine_mode mode
, rtx op0
, HOST_WIDE_INT val
,
2955 rtx target
, const struct algorithm
*alg
,
2956 enum mult_variant variant
)
2958 HOST_WIDE_INT val_so_far
;
2964 /* Avoid referencing memory over and over and invalid sharing
2966 op0
= force_reg (mode
, op0
);
2968 /* ACCUM starts out either as OP0 or as a zero, depending on
2969 the first operation. */
2971 if (alg
->op
[0] == alg_zero
)
2973 accum
= copy_to_mode_reg (mode
, CONST0_RTX (mode
));
2976 else if (alg
->op
[0] == alg_m
)
2978 accum
= copy_to_mode_reg (mode
, op0
);
2984 for (opno
= 1; opno
< alg
->ops
; opno
++)
2986 int log
= alg
->log
[opno
];
2987 rtx shift_subtarget
= optimize
? 0 : accum
;
2989 = (opno
== alg
->ops
- 1 && target
!= 0 && variant
!= add_variant
2992 rtx accum_target
= optimize
? 0 : accum
;
2995 switch (alg
->op
[opno
])
2998 tem
= expand_shift (LSHIFT_EXPR
, mode
, accum
, log
, NULL_RTX
, 0);
2999 /* REG_EQUAL note will be attached to the following insn. */
3000 emit_move_insn (accum
, tem
);
3005 tem
= expand_shift (LSHIFT_EXPR
, mode
, op0
, log
, NULL_RTX
, 0);
3006 accum
= force_operand (gen_rtx_PLUS (mode
, accum
, tem
),
3007 add_target
? add_target
: accum_target
);
3008 val_so_far
+= (HOST_WIDE_INT
) 1 << log
;
3012 tem
= expand_shift (LSHIFT_EXPR
, mode
, op0
, log
, NULL_RTX
, 0);
3013 accum
= force_operand (gen_rtx_MINUS (mode
, accum
, tem
),
3014 add_target
? add_target
: accum_target
);
3015 val_so_far
-= (HOST_WIDE_INT
) 1 << log
;
3019 accum
= expand_shift (LSHIFT_EXPR
, mode
, accum
,
3020 log
, shift_subtarget
, 0);
3021 accum
= force_operand (gen_rtx_PLUS (mode
, accum
, op0
),
3022 add_target
? add_target
: accum_target
);
3023 val_so_far
= (val_so_far
<< log
) + 1;
3027 accum
= expand_shift (LSHIFT_EXPR
, mode
, accum
,
3028 log
, shift_subtarget
, 0);
3029 accum
= force_operand (gen_rtx_MINUS (mode
, accum
, op0
),
3030 add_target
? add_target
: accum_target
);
3031 val_so_far
= (val_so_far
<< log
) - 1;
3034 case alg_add_factor
:
3035 tem
= expand_shift (LSHIFT_EXPR
, mode
, accum
, log
, NULL_RTX
, 0);
3036 accum
= force_operand (gen_rtx_PLUS (mode
, accum
, tem
),
3037 add_target
? add_target
: accum_target
);
3038 val_so_far
+= val_so_far
<< log
;
3041 case alg_sub_factor
:
3042 tem
= expand_shift (LSHIFT_EXPR
, mode
, accum
, log
, NULL_RTX
, 0);
3043 accum
= force_operand (gen_rtx_MINUS (mode
, tem
, accum
),
3045 ? add_target
: (optimize
? 0 : tem
)));
3046 val_so_far
= (val_so_far
<< log
) - val_so_far
;
3053 if (SCALAR_INT_MODE_P (mode
))
3055 /* Write a REG_EQUAL note on the last insn so that we can cse
3056 multiplication sequences. Note that if ACCUM is a SUBREG,
3057 we've set the inner register and must properly indicate that. */
3058 tem
= op0
, nmode
= mode
;
3059 accum_inner
= accum
;
3060 if (GET_CODE (accum
) == SUBREG
)
3062 accum_inner
= SUBREG_REG (accum
);
3063 nmode
= GET_MODE (accum_inner
);
3064 tem
= gen_lowpart (nmode
, op0
);
3067 insn
= get_last_insn ();
3068 set_dst_reg_note (insn
, REG_EQUAL
,
3069 gen_rtx_MULT (nmode
, tem
,
3070 gen_int_mode (val_so_far
, nmode
)),
3075 if (variant
== negate_variant
)
3077 val_so_far
= -val_so_far
;
3078 accum
= expand_unop (mode
, neg_optab
, accum
, target
, 0);
3080 else if (variant
== add_variant
)
3082 val_so_far
= val_so_far
+ 1;
3083 accum
= force_operand (gen_rtx_PLUS (mode
, accum
, op0
), target
);
3086 /* Compare only the bits of val and val_so_far that are significant
3087 in the result mode, to avoid sign-/zero-extension confusion. */
3088 nmode
= GET_MODE_INNER (mode
);
3089 val
&= GET_MODE_MASK (nmode
);
3090 val_so_far
&= GET_MODE_MASK (nmode
);
3091 gcc_assert (val
== val_so_far
);
3096 /* Perform a multiplication and return an rtx for the result.
3097 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3098 TARGET is a suggestion for where to store the result (an rtx).
3100 We check specially for a constant integer as OP1.
3101 If you want this check for OP0 as well, then before calling
3102 you should swap the two operands if OP0 would be constant. */
3105 expand_mult (machine_mode mode
, rtx op0
, rtx op1
, rtx target
,
3108 enum mult_variant variant
;
3109 struct algorithm algorithm
;
3112 bool speed
= optimize_insn_for_speed_p ();
3113 bool do_trapv
= flag_trapv
&& SCALAR_INT_MODE_P (mode
) && !unsignedp
;
3115 if (CONSTANT_P (op0
))
3116 std::swap (op0
, op1
);
3118 /* For vectors, there are several simplifications that can be made if
3119 all elements of the vector constant are identical. */
3120 scalar_op1
= unwrap_const_vec_duplicate (op1
);
3122 if (INTEGRAL_MODE_P (mode
))
3125 HOST_WIDE_INT coeff
;
3129 if (op1
== CONST0_RTX (mode
))
3131 if (op1
== CONST1_RTX (mode
))
3133 if (op1
== CONSTM1_RTX (mode
))
3134 return expand_unop (mode
, do_trapv
? negv_optab
: neg_optab
,
3140 /* If mode is integer vector mode, check if the backend supports
3141 vector lshift (by scalar or vector) at all. If not, we can't use
3142 synthetized multiply. */
3143 if (GET_MODE_CLASS (mode
) == MODE_VECTOR_INT
3144 && optab_handler (vashl_optab
, mode
) == CODE_FOR_nothing
3145 && optab_handler (ashl_optab
, mode
) == CODE_FOR_nothing
)
3148 /* These are the operations that are potentially turned into
3149 a sequence of shifts and additions. */
3150 mode_bitsize
= GET_MODE_UNIT_BITSIZE (mode
);
3152 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3153 less than or equal in size to `unsigned int' this doesn't matter.
3154 If the mode is larger than `unsigned int', then synth_mult works
3155 only if the constant value exactly fits in an `unsigned int' without
3156 any truncation. This means that multiplying by negative values does
3157 not work; results are off by 2^32 on a 32 bit machine. */
3158 if (CONST_INT_P (scalar_op1
))
3160 coeff
= INTVAL (scalar_op1
);
3163 #if TARGET_SUPPORTS_WIDE_INT
3164 else if (CONST_WIDE_INT_P (scalar_op1
))
3166 else if (CONST_DOUBLE_AS_INT_P (scalar_op1
))
3169 int shift
= wi::exact_log2 (std::make_pair (scalar_op1
, mode
));
3170 /* Perfect power of 2 (other than 1, which is handled above). */
3172 return expand_shift (LSHIFT_EXPR
, mode
, op0
,
3173 shift
, target
, unsignedp
);
3180 /* We used to test optimize here, on the grounds that it's better to
3181 produce a smaller program when -O is not used. But this causes
3182 such a terrible slowdown sometimes that it seems better to always
3185 /* Special case powers of two. */
3186 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff
)
3187 && !(is_neg
&& mode_bitsize
> HOST_BITS_PER_WIDE_INT
))
3188 return expand_shift (LSHIFT_EXPR
, mode
, op0
,
3189 floor_log2 (coeff
), target
, unsignedp
);
3191 fake_reg
= gen_raw_REG (mode
, LAST_VIRTUAL_REGISTER
+ 1);
3193 /* Attempt to handle multiplication of DImode values by negative
3194 coefficients, by performing the multiplication by a positive
3195 multiplier and then inverting the result. */
3196 if (is_neg
&& mode_bitsize
> HOST_BITS_PER_WIDE_INT
)
3198 /* Its safe to use -coeff even for INT_MIN, as the
3199 result is interpreted as an unsigned coefficient.
3200 Exclude cost of op0 from max_cost to match the cost
3201 calculation of the synth_mult. */
3202 coeff
= -(unsigned HOST_WIDE_INT
) coeff
;
3203 max_cost
= (set_src_cost (gen_rtx_MULT (mode
, fake_reg
, op1
),
3205 - neg_cost (speed
, mode
));
3209 /* Special case powers of two. */
3210 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff
))
3212 rtx temp
= expand_shift (LSHIFT_EXPR
, mode
, op0
,
3213 floor_log2 (coeff
), target
, unsignedp
);
3214 return expand_unop (mode
, neg_optab
, temp
, target
, 0);
3217 if (choose_mult_variant (mode
, coeff
, &algorithm
, &variant
,
3220 rtx temp
= expand_mult_const (mode
, op0
, coeff
, NULL_RTX
,
3221 &algorithm
, variant
);
3222 return expand_unop (mode
, neg_optab
, temp
, target
, 0);
3227 /* Exclude cost of op0 from max_cost to match the cost
3228 calculation of the synth_mult. */
3229 max_cost
= set_src_cost (gen_rtx_MULT (mode
, fake_reg
, op1
), mode
, speed
);
3230 if (choose_mult_variant (mode
, coeff
, &algorithm
, &variant
, max_cost
))
3231 return expand_mult_const (mode
, op0
, coeff
, target
,
3232 &algorithm
, variant
);
3236 /* Expand x*2.0 as x+x. */
3237 if (CONST_DOUBLE_AS_FLOAT_P (scalar_op1
))
3240 REAL_VALUE_FROM_CONST_DOUBLE (d
, scalar_op1
);
3242 if (REAL_VALUES_EQUAL (d
, dconst2
))
3244 op0
= force_reg (GET_MODE (op0
), op0
);
3245 return expand_binop (mode
, add_optab
, op0
, op0
,
3246 target
, unsignedp
, OPTAB_LIB_WIDEN
);
3250 /* This used to use umul_optab if unsigned, but for non-widening multiply
3251 there is no difference between signed and unsigned. */
3252 op0
= expand_binop (mode
, do_trapv
? smulv_optab
: smul_optab
,
3253 op0
, op1
, target
, unsignedp
, OPTAB_LIB_WIDEN
);
3258 /* Return a cost estimate for multiplying a register by the given
3259 COEFFicient in the given MODE and SPEED. */
3262 mult_by_coeff_cost (HOST_WIDE_INT coeff
, machine_mode mode
, bool speed
)
3265 struct algorithm algorithm
;
3266 enum mult_variant variant
;
3268 rtx fake_reg
= gen_raw_REG (mode
, LAST_VIRTUAL_REGISTER
+ 1);
3269 max_cost
= set_src_cost (gen_rtx_MULT (mode
, fake_reg
, fake_reg
),
3271 if (choose_mult_variant (mode
, coeff
, &algorithm
, &variant
, max_cost
))
3272 return algorithm
.cost
.cost
;
3277 /* Perform a widening 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).
3280 THIS_OPTAB is the optab we should use, it must be either umul_widen_optab
3281 or smul_widen_optab.
3283 We check specially for a constant integer as OP1, comparing the
3284 cost of a widening multiply against the cost of a sequence of shifts
3288 expand_widening_mult (machine_mode mode
, rtx op0
, rtx op1
, rtx target
,
3289 int unsignedp
, optab this_optab
)
3291 bool speed
= optimize_insn_for_speed_p ();
3294 if (CONST_INT_P (op1
)
3295 && GET_MODE (op0
) != VOIDmode
3296 && (cop1
= convert_modes (mode
, GET_MODE (op0
), op1
,
3297 this_optab
== umul_widen_optab
))
3298 && CONST_INT_P (cop1
)
3299 && (INTVAL (cop1
) >= 0
3300 || HWI_COMPUTABLE_MODE_P (mode
)))
3302 HOST_WIDE_INT coeff
= INTVAL (cop1
);
3304 enum mult_variant variant
;
3305 struct algorithm algorithm
;
3308 return CONST0_RTX (mode
);
3310 /* Special case powers of two. */
3311 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff
))
3313 op0
= convert_to_mode (mode
, op0
, this_optab
== umul_widen_optab
);
3314 return expand_shift (LSHIFT_EXPR
, mode
, op0
,
3315 floor_log2 (coeff
), target
, unsignedp
);
3318 /* Exclude cost of op0 from max_cost to match the cost
3319 calculation of the synth_mult. */
3320 max_cost
= mul_widen_cost (speed
, mode
);
3321 if (choose_mult_variant (mode
, coeff
, &algorithm
, &variant
,
3324 op0
= convert_to_mode (mode
, op0
, this_optab
== umul_widen_optab
);
3325 return expand_mult_const (mode
, op0
, coeff
, target
,
3326 &algorithm
, variant
);
3329 return expand_binop (mode
, this_optab
, op0
, op1
, target
,
3330 unsignedp
, OPTAB_LIB_WIDEN
);
3333 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3334 replace division by D, and put the least significant N bits of the result
3335 in *MULTIPLIER_PTR and return the most significant bit.
3337 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3338 needed precision is in PRECISION (should be <= N).
3340 PRECISION should be as small as possible so this function can choose
3341 multiplier more freely.
3343 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3344 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3346 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3347 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3349 unsigned HOST_WIDE_INT
3350 choose_multiplier (unsigned HOST_WIDE_INT d
, int n
, int precision
,
3351 unsigned HOST_WIDE_INT
*multiplier_ptr
,
3352 int *post_shift_ptr
, int *lgup_ptr
)
3354 int lgup
, post_shift
;
3357 /* lgup = ceil(log2(divisor)); */
3358 lgup
= ceil_log2 (d
);
3360 gcc_assert (lgup
<= n
);
3363 pow2
= n
+ lgup
- precision
;
3365 /* mlow = 2^(N + lgup)/d */
3366 wide_int val
= wi::set_bit_in_zero (pow
, HOST_BITS_PER_DOUBLE_INT
);
3367 wide_int mlow
= wi::udiv_trunc (val
, d
);
3369 /* mhigh = (2^(N + lgup) + 2^(N + lgup - precision))/d */
3370 val
|= wi::set_bit_in_zero (pow2
, HOST_BITS_PER_DOUBLE_INT
);
3371 wide_int mhigh
= wi::udiv_trunc (val
, d
);
3373 /* If precision == N, then mlow, mhigh exceed 2^N
3374 (but they do not exceed 2^(N+1)). */
3376 /* Reduce to lowest terms. */
3377 for (post_shift
= lgup
; post_shift
> 0; post_shift
--)
3379 unsigned HOST_WIDE_INT ml_lo
= wi::extract_uhwi (mlow
, 1,
3380 HOST_BITS_PER_WIDE_INT
);
3381 unsigned HOST_WIDE_INT mh_lo
= wi::extract_uhwi (mhigh
, 1,
3382 HOST_BITS_PER_WIDE_INT
);
3386 mlow
= wi::uhwi (ml_lo
, HOST_BITS_PER_DOUBLE_INT
);
3387 mhigh
= wi::uhwi (mh_lo
, HOST_BITS_PER_DOUBLE_INT
);
3390 *post_shift_ptr
= post_shift
;
3392 if (n
< HOST_BITS_PER_WIDE_INT
)
3394 unsigned HOST_WIDE_INT mask
= ((unsigned HOST_WIDE_INT
) 1 << n
) - 1;
3395 *multiplier_ptr
= mhigh
.to_uhwi () & mask
;
3396 return mhigh
.to_uhwi () >= mask
;
3400 *multiplier_ptr
= mhigh
.to_uhwi ();
3401 return wi::extract_uhwi (mhigh
, HOST_BITS_PER_WIDE_INT
, 1);
3405 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3406 congruent to 1 (mod 2**N). */
3408 static unsigned HOST_WIDE_INT
3409 invert_mod2n (unsigned HOST_WIDE_INT x
, int n
)
3411 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3413 /* The algorithm notes that the choice y = x satisfies
3414 x*y == 1 mod 2^3, since x is assumed odd.
3415 Each iteration doubles the number of bits of significance in y. */
3417 unsigned HOST_WIDE_INT mask
;
3418 unsigned HOST_WIDE_INT y
= x
;
3421 mask
= (n
== HOST_BITS_PER_WIDE_INT
3422 ? ~(unsigned HOST_WIDE_INT
) 0
3423 : ((unsigned HOST_WIDE_INT
) 1 << n
) - 1);
3427 y
= y
* (2 - x
*y
) & mask
; /* Modulo 2^N */
3433 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3434 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3435 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3436 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3439 The result is put in TARGET if that is convenient.
3441 MODE is the mode of operation. */
3444 expand_mult_highpart_adjust (machine_mode mode
, rtx adj_operand
, rtx op0
,
3445 rtx op1
, rtx target
, int unsignedp
)
3448 enum rtx_code adj_code
= unsignedp
? PLUS
: MINUS
;
3450 tem
= expand_shift (RSHIFT_EXPR
, mode
, op0
,
3451 GET_MODE_BITSIZE (mode
) - 1, NULL_RTX
, 0);
3452 tem
= expand_and (mode
, tem
, op1
, NULL_RTX
);
3454 = force_operand (gen_rtx_fmt_ee (adj_code
, mode
, adj_operand
, tem
),
3457 tem
= expand_shift (RSHIFT_EXPR
, mode
, op1
,
3458 GET_MODE_BITSIZE (mode
) - 1, NULL_RTX
, 0);
3459 tem
= expand_and (mode
, tem
, op0
, NULL_RTX
);
3460 target
= force_operand (gen_rtx_fmt_ee (adj_code
, mode
, adj_operand
, tem
),
3466 /* Subroutine of expmed_mult_highpart. Return the MODE high part of OP. */
3469 extract_high_half (machine_mode mode
, rtx op
)
3471 machine_mode wider_mode
;
3473 if (mode
== word_mode
)
3474 return gen_highpart (mode
, op
);
3476 gcc_assert (!SCALAR_FLOAT_MODE_P (mode
));
3478 wider_mode
= GET_MODE_WIDER_MODE (mode
);
3479 op
= expand_shift (RSHIFT_EXPR
, wider_mode
, op
,
3480 GET_MODE_BITSIZE (mode
), 0, 1);
3481 return convert_modes (mode
, wider_mode
, op
, 0);
3484 /* Like expmed_mult_highpart, but only consider using a multiplication
3485 optab. OP1 is an rtx for the constant operand. */
3488 expmed_mult_highpart_optab (machine_mode mode
, rtx op0
, rtx op1
,
3489 rtx target
, int unsignedp
, int max_cost
)
3491 rtx narrow_op1
= gen_int_mode (INTVAL (op1
), mode
);
3492 machine_mode wider_mode
;
3496 bool speed
= optimize_insn_for_speed_p ();
3498 gcc_assert (!SCALAR_FLOAT_MODE_P (mode
));
3500 wider_mode
= GET_MODE_WIDER_MODE (mode
);
3501 size
= GET_MODE_BITSIZE (mode
);
3503 /* Firstly, try using a multiplication insn that only generates the needed
3504 high part of the product, and in the sign flavor of unsignedp. */
3505 if (mul_highpart_cost (speed
, mode
) < max_cost
)
3507 moptab
= unsignedp
? umul_highpart_optab
: smul_highpart_optab
;
3508 tem
= expand_binop (mode
, moptab
, op0
, narrow_op1
, target
,
3509 unsignedp
, OPTAB_DIRECT
);
3514 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3515 Need to adjust the result after the multiplication. */
3516 if (size
- 1 < BITS_PER_WORD
3517 && (mul_highpart_cost (speed
, mode
)
3518 + 2 * shift_cost (speed
, mode
, size
-1)
3519 + 4 * add_cost (speed
, mode
) < max_cost
))
3521 moptab
= unsignedp
? smul_highpart_optab
: umul_highpart_optab
;
3522 tem
= expand_binop (mode
, moptab
, op0
, narrow_op1
, target
,
3523 unsignedp
, OPTAB_DIRECT
);
3525 /* We used the wrong signedness. Adjust the result. */
3526 return expand_mult_highpart_adjust (mode
, tem
, op0
, narrow_op1
,
3530 /* Try widening multiplication. */
3531 moptab
= unsignedp
? umul_widen_optab
: smul_widen_optab
;
3532 if (widening_optab_handler (moptab
, wider_mode
, mode
) != CODE_FOR_nothing
3533 && mul_widen_cost (speed
, wider_mode
) < max_cost
)
3535 tem
= expand_binop (wider_mode
, moptab
, op0
, narrow_op1
, 0,
3536 unsignedp
, OPTAB_WIDEN
);
3538 return extract_high_half (mode
, tem
);
3541 /* Try widening the mode and perform a non-widening multiplication. */
3542 if (optab_handler (smul_optab
, wider_mode
) != CODE_FOR_nothing
3543 && size
- 1 < BITS_PER_WORD
3544 && (mul_cost (speed
, wider_mode
) + shift_cost (speed
, mode
, size
-1)
3550 /* We need to widen the operands, for example to ensure the
3551 constant multiplier is correctly sign or zero extended.
3552 Use a sequence to clean-up any instructions emitted by
3553 the conversions if things don't work out. */
3555 wop0
= convert_modes (wider_mode
, mode
, op0
, unsignedp
);
3556 wop1
= convert_modes (wider_mode
, mode
, op1
, unsignedp
);
3557 tem
= expand_binop (wider_mode
, smul_optab
, wop0
, wop1
, 0,
3558 unsignedp
, OPTAB_WIDEN
);
3559 insns
= get_insns ();
3565 return extract_high_half (mode
, tem
);
3569 /* Try widening multiplication of opposite signedness, and adjust. */
3570 moptab
= unsignedp
? smul_widen_optab
: umul_widen_optab
;
3571 if (widening_optab_handler (moptab
, wider_mode
, mode
) != CODE_FOR_nothing
3572 && size
- 1 < BITS_PER_WORD
3573 && (mul_widen_cost (speed
, wider_mode
)
3574 + 2 * shift_cost (speed
, mode
, size
-1)
3575 + 4 * add_cost (speed
, mode
) < max_cost
))
3577 tem
= expand_binop (wider_mode
, moptab
, op0
, narrow_op1
,
3578 NULL_RTX
, ! unsignedp
, OPTAB_WIDEN
);
3581 tem
= extract_high_half (mode
, tem
);
3582 /* We used the wrong signedness. Adjust the result. */
3583 return expand_mult_highpart_adjust (mode
, tem
, op0
, narrow_op1
,
3591 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3592 putting the high half of the result in TARGET if that is convenient,
3593 and return where the result is. If the operation can not be performed,
3596 MODE is the mode of operation and result.
3598 UNSIGNEDP nonzero means unsigned multiply.
3600 MAX_COST is the total allowed cost for the expanded RTL. */
3603 expmed_mult_highpart (machine_mode mode
, rtx op0
, rtx op1
,
3604 rtx target
, int unsignedp
, int max_cost
)
3606 machine_mode wider_mode
= GET_MODE_WIDER_MODE (mode
);
3607 unsigned HOST_WIDE_INT cnst1
;
3609 bool sign_adjust
= false;
3610 enum mult_variant variant
;
3611 struct algorithm alg
;
3613 bool speed
= optimize_insn_for_speed_p ();
3615 gcc_assert (!SCALAR_FLOAT_MODE_P (mode
));
3616 /* We can't support modes wider than HOST_BITS_PER_INT. */
3617 gcc_assert (HWI_COMPUTABLE_MODE_P (mode
));
3619 cnst1
= INTVAL (op1
) & GET_MODE_MASK (mode
);
3621 /* We can't optimize modes wider than BITS_PER_WORD.
3622 ??? We might be able to perform double-word arithmetic if
3623 mode == word_mode, however all the cost calculations in
3624 synth_mult etc. assume single-word operations. */
3625 if (GET_MODE_BITSIZE (wider_mode
) > BITS_PER_WORD
)
3626 return expmed_mult_highpart_optab (mode
, op0
, op1
, target
,
3627 unsignedp
, max_cost
);
3629 extra_cost
= shift_cost (speed
, mode
, GET_MODE_BITSIZE (mode
) - 1);
3631 /* Check whether we try to multiply by a negative constant. */
3632 if (!unsignedp
&& ((cnst1
>> (GET_MODE_BITSIZE (mode
) - 1)) & 1))
3635 extra_cost
+= add_cost (speed
, mode
);
3638 /* See whether shift/add multiplication is cheap enough. */
3639 if (choose_mult_variant (wider_mode
, cnst1
, &alg
, &variant
,
3640 max_cost
- extra_cost
))
3642 /* See whether the specialized multiplication optabs are
3643 cheaper than the shift/add version. */
3644 tem
= expmed_mult_highpart_optab (mode
, op0
, op1
, target
, unsignedp
,
3645 alg
.cost
.cost
+ extra_cost
);
3649 tem
= convert_to_mode (wider_mode
, op0
, unsignedp
);
3650 tem
= expand_mult_const (wider_mode
, tem
, cnst1
, 0, &alg
, variant
);
3651 tem
= extract_high_half (mode
, tem
);
3653 /* Adjust result for signedness. */
3655 tem
= force_operand (gen_rtx_MINUS (mode
, tem
, op0
), tem
);
3659 return expmed_mult_highpart_optab (mode
, op0
, op1
, target
,
3660 unsignedp
, max_cost
);
3664 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3667 expand_smod_pow2 (machine_mode mode
, rtx op0
, HOST_WIDE_INT d
)
3669 rtx result
, temp
, shift
;
3670 rtx_code_label
*label
;
3672 int prec
= GET_MODE_PRECISION (mode
);
3674 logd
= floor_log2 (d
);
3675 result
= gen_reg_rtx (mode
);
3677 /* Avoid conditional branches when they're expensive. */
3678 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2
3679 && optimize_insn_for_speed_p ())
3681 rtx signmask
= emit_store_flag (result
, LT
, op0
, const0_rtx
,
3685 HOST_WIDE_INT masklow
= ((HOST_WIDE_INT
) 1 << logd
) - 1;
3686 signmask
= force_reg (mode
, signmask
);
3687 shift
= GEN_INT (GET_MODE_BITSIZE (mode
) - logd
);
3689 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3690 which instruction sequence to use. If logical right shifts
3691 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3692 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3694 temp
= gen_rtx_LSHIFTRT (mode
, result
, shift
);
3695 if (optab_handler (lshr_optab
, mode
) == CODE_FOR_nothing
3696 || (set_src_cost (temp
, mode
, optimize_insn_for_speed_p ())
3697 > COSTS_N_INSNS (2)))
3699 temp
= expand_binop (mode
, xor_optab
, op0
, signmask
,
3700 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3701 temp
= expand_binop (mode
, sub_optab
, temp
, signmask
,
3702 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3703 temp
= expand_binop (mode
, and_optab
, temp
,
3704 gen_int_mode (masklow
, mode
),
3705 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3706 temp
= expand_binop (mode
, xor_optab
, temp
, signmask
,
3707 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3708 temp
= expand_binop (mode
, sub_optab
, temp
, signmask
,
3709 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3713 signmask
= expand_binop (mode
, lshr_optab
, signmask
, shift
,
3714 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3715 signmask
= force_reg (mode
, signmask
);
3717 temp
= expand_binop (mode
, add_optab
, op0
, signmask
,
3718 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3719 temp
= expand_binop (mode
, and_optab
, temp
,
3720 gen_int_mode (masklow
, mode
),
3721 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3722 temp
= expand_binop (mode
, sub_optab
, temp
, signmask
,
3723 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3729 /* Mask contains the mode's signbit and the significant bits of the
3730 modulus. By including the signbit in the operation, many targets
3731 can avoid an explicit compare operation in the following comparison
3733 wide_int mask
= wi::mask (logd
, false, prec
);
3734 mask
= wi::set_bit (mask
, prec
- 1);
3736 temp
= expand_binop (mode
, and_optab
, op0
,
3737 immed_wide_int_const (mask
, mode
),
3738 result
, 1, OPTAB_LIB_WIDEN
);
3740 emit_move_insn (result
, temp
);
3742 label
= gen_label_rtx ();
3743 do_cmp_and_jump (result
, const0_rtx
, GE
, mode
, label
);
3745 temp
= expand_binop (mode
, sub_optab
, result
, const1_rtx
, result
,
3746 0, OPTAB_LIB_WIDEN
);
3748 mask
= wi::mask (logd
, true, prec
);
3749 temp
= expand_binop (mode
, ior_optab
, temp
,
3750 immed_wide_int_const (mask
, mode
),
3751 result
, 1, OPTAB_LIB_WIDEN
);
3752 temp
= expand_binop (mode
, add_optab
, temp
, const1_rtx
, result
,
3753 0, OPTAB_LIB_WIDEN
);
3755 emit_move_insn (result
, temp
);
3760 /* Expand signed division of OP0 by a power of two D in mode MODE.
3761 This routine is only called for positive values of D. */
3764 expand_sdiv_pow2 (machine_mode mode
, rtx op0
, HOST_WIDE_INT d
)
3767 rtx_code_label
*label
;
3770 logd
= floor_log2 (d
);
3773 && BRANCH_COST (optimize_insn_for_speed_p (),
3776 temp
= gen_reg_rtx (mode
);
3777 temp
= emit_store_flag (temp
, LT
, op0
, const0_rtx
, mode
, 0, 1);
3778 temp
= expand_binop (mode
, add_optab
, temp
, op0
, NULL_RTX
,
3779 0, OPTAB_LIB_WIDEN
);
3780 return expand_shift (RSHIFT_EXPR
, mode
, temp
, logd
, NULL_RTX
, 0);
3783 if (HAVE_conditional_move
3784 && BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2)
3789 temp2
= copy_to_mode_reg (mode
, op0
);
3790 temp
= expand_binop (mode
, add_optab
, temp2
, gen_int_mode (d
- 1, mode
),
3791 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
3792 temp
= force_reg (mode
, temp
);
3794 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3795 temp2
= emit_conditional_move (temp2
, LT
, temp2
, const0_rtx
,
3796 mode
, temp
, temp2
, mode
, 0);
3799 rtx_insn
*seq
= get_insns ();
3802 return expand_shift (RSHIFT_EXPR
, mode
, temp2
, logd
, NULL_RTX
, 0);
3807 if (BRANCH_COST (optimize_insn_for_speed_p (),
3810 int ushift
= GET_MODE_BITSIZE (mode
) - logd
;
3812 temp
= gen_reg_rtx (mode
);
3813 temp
= emit_store_flag (temp
, LT
, op0
, const0_rtx
, mode
, 0, -1);
3814 if (GET_MODE_BITSIZE (mode
) >= BITS_PER_WORD
3815 || shift_cost (optimize_insn_for_speed_p (), mode
, ushift
)
3816 > COSTS_N_INSNS (1))
3817 temp
= expand_binop (mode
, and_optab
, temp
, gen_int_mode (d
- 1, mode
),
3818 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
3820 temp
= expand_shift (RSHIFT_EXPR
, mode
, temp
,
3821 ushift
, NULL_RTX
, 1);
3822 temp
= expand_binop (mode
, add_optab
, temp
, op0
, NULL_RTX
,
3823 0, OPTAB_LIB_WIDEN
);
3824 return expand_shift (RSHIFT_EXPR
, mode
, temp
, logd
, NULL_RTX
, 0);
3827 label
= gen_label_rtx ();
3828 temp
= copy_to_mode_reg (mode
, op0
);
3829 do_cmp_and_jump (temp
, const0_rtx
, GE
, mode
, label
);
3830 expand_inc (temp
, gen_int_mode (d
- 1, mode
));
3832 return expand_shift (RSHIFT_EXPR
, mode
, temp
, logd
, NULL_RTX
, 0);
3835 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3836 if that is convenient, and returning where the result is.
3837 You may request either the quotient or the remainder as the result;
3838 specify REM_FLAG nonzero to get the remainder.
3840 CODE is the expression code for which kind of division this is;
3841 it controls how rounding is done. MODE is the machine mode to use.
3842 UNSIGNEDP nonzero means do unsigned division. */
3844 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3845 and then correct it by or'ing in missing high bits
3846 if result of ANDI is nonzero.
3847 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3848 This could optimize to a bfexts instruction.
3849 But C doesn't use these operations, so their optimizations are
3851 /* ??? For modulo, we don't actually need the highpart of the first product,
3852 the low part will do nicely. And for small divisors, the second multiply
3853 can also be a low-part only multiply or even be completely left out.
3854 E.g. to calculate the remainder of a division by 3 with a 32 bit
3855 multiply, multiply with 0x55555556 and extract the upper two bits;
3856 the result is exact for inputs up to 0x1fffffff.
3857 The input range can be reduced by using cross-sum rules.
3858 For odd divisors >= 3, the following table gives right shift counts
3859 so that if a number is shifted by an integer multiple of the given
3860 amount, the remainder stays the same:
3861 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3862 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3863 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3864 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3865 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3867 Cross-sum rules for even numbers can be derived by leaving as many bits
3868 to the right alone as the divisor has zeros to the right.
3869 E.g. if x is an unsigned 32 bit number:
3870 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3874 expand_divmod (int rem_flag
, enum tree_code code
, machine_mode mode
,
3875 rtx op0
, rtx op1
, rtx target
, int unsignedp
)
3877 machine_mode compute_mode
;
3879 rtx quotient
= 0, remainder
= 0;
3883 optab optab1
, optab2
;
3884 int op1_is_constant
, op1_is_pow2
= 0;
3885 int max_cost
, extra_cost
;
3886 static HOST_WIDE_INT last_div_const
= 0;
3887 bool speed
= optimize_insn_for_speed_p ();
3889 op1_is_constant
= CONST_INT_P (op1
);
3890 if (op1_is_constant
)
3892 unsigned HOST_WIDE_INT ext_op1
= UINTVAL (op1
);
3894 ext_op1
&= GET_MODE_MASK (mode
);
3895 op1_is_pow2
= ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1
)
3896 || (! unsignedp
&& EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1
))));
3900 This is the structure of expand_divmod:
3902 First comes code to fix up the operands so we can perform the operations
3903 correctly and efficiently.
3905 Second comes a switch statement with code specific for each rounding mode.
3906 For some special operands this code emits all RTL for the desired
3907 operation, for other cases, it generates only a quotient and stores it in
3908 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3909 to indicate that it has not done anything.
3911 Last comes code that finishes the operation. If QUOTIENT is set and
3912 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3913 QUOTIENT is not set, it is computed using trunc rounding.
3915 We try to generate special code for division and remainder when OP1 is a
3916 constant. If |OP1| = 2**n we can use shifts and some other fast
3917 operations. For other values of OP1, we compute a carefully selected
3918 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3921 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3922 half of the product. Different strategies for generating the product are
3923 implemented in expmed_mult_highpart.
3925 If what we actually want is the remainder, we generate that by another
3926 by-constant multiplication and a subtraction. */
3928 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3929 code below will malfunction if we are, so check here and handle
3930 the special case if so. */
3931 if (op1
== const1_rtx
)
3932 return rem_flag
? const0_rtx
: op0
;
3934 /* When dividing by -1, we could get an overflow.
3935 negv_optab can handle overflows. */
3936 if (! unsignedp
&& op1
== constm1_rtx
)
3940 return expand_unop (mode
, flag_trapv
&& GET_MODE_CLASS (mode
) == MODE_INT
3941 ? negv_optab
: neg_optab
, op0
, target
, 0);
3945 /* Don't use the function value register as a target
3946 since we have to read it as well as write it,
3947 and function-inlining gets confused by this. */
3948 && ((REG_P (target
) && REG_FUNCTION_VALUE_P (target
))
3949 /* Don't clobber an operand while doing a multi-step calculation. */
3950 || ((rem_flag
|| op1_is_constant
)
3951 && (reg_mentioned_p (target
, op0
)
3952 || (MEM_P (op0
) && MEM_P (target
))))
3953 || reg_mentioned_p (target
, op1
)
3954 || (MEM_P (op1
) && MEM_P (target
))))
3957 /* Get the mode in which to perform this computation. Normally it will
3958 be MODE, but sometimes we can't do the desired operation in MODE.
3959 If so, pick a wider mode in which we can do the operation. Convert
3960 to that mode at the start to avoid repeated conversions.
3962 First see what operations we need. These depend on the expression
3963 we are evaluating. (We assume that divxx3 insns exist under the
3964 same conditions that modxx3 insns and that these insns don't normally
3965 fail. If these assumptions are not correct, we may generate less
3966 efficient code in some cases.)
3968 Then see if we find a mode in which we can open-code that operation
3969 (either a division, modulus, or shift). Finally, check for the smallest
3970 mode for which we can do the operation with a library call. */
3972 /* We might want to refine this now that we have division-by-constant
3973 optimization. Since expmed_mult_highpart tries so many variants, it is
3974 not straightforward to generalize this. Maybe we should make an array
3975 of possible modes in init_expmed? Save this for GCC 2.7. */
3977 optab1
= ((op1_is_pow2
&& op1
!= const0_rtx
)
3978 ? (unsignedp
? lshr_optab
: ashr_optab
)
3979 : (unsignedp
? udiv_optab
: sdiv_optab
));
3980 optab2
= ((op1_is_pow2
&& op1
!= const0_rtx
)
3982 : (unsignedp
? udivmod_optab
: sdivmod_optab
));
3984 for (compute_mode
= mode
; compute_mode
!= VOIDmode
;
3985 compute_mode
= GET_MODE_WIDER_MODE (compute_mode
))
3986 if (optab_handler (optab1
, compute_mode
) != CODE_FOR_nothing
3987 || optab_handler (optab2
, compute_mode
) != CODE_FOR_nothing
)
3990 if (compute_mode
== VOIDmode
)
3991 for (compute_mode
= mode
; compute_mode
!= VOIDmode
;
3992 compute_mode
= GET_MODE_WIDER_MODE (compute_mode
))
3993 if (optab_libfunc (optab1
, compute_mode
)
3994 || optab_libfunc (optab2
, compute_mode
))
3997 /* If we still couldn't find a mode, use MODE, but expand_binop will
3999 if (compute_mode
== VOIDmode
)
4000 compute_mode
= mode
;
4002 if (target
&& GET_MODE (target
) == compute_mode
)
4005 tquotient
= gen_reg_rtx (compute_mode
);
4007 size
= GET_MODE_BITSIZE (compute_mode
);
4009 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
4010 (mode), and thereby get better code when OP1 is a constant. Do that
4011 later. It will require going over all usages of SIZE below. */
4012 size
= GET_MODE_BITSIZE (mode
);
4015 /* Only deduct something for a REM if the last divide done was
4016 for a different constant. Then set the constant of the last
4018 max_cost
= (unsignedp
4019 ? udiv_cost (speed
, compute_mode
)
4020 : sdiv_cost (speed
, compute_mode
));
4021 if (rem_flag
&& ! (last_div_const
!= 0 && op1_is_constant
4022 && INTVAL (op1
) == last_div_const
))
4023 max_cost
-= (mul_cost (speed
, compute_mode
)
4024 + add_cost (speed
, compute_mode
));
4026 last_div_const
= ! rem_flag
&& op1_is_constant
? INTVAL (op1
) : 0;
4028 /* Now convert to the best mode to use. */
4029 if (compute_mode
!= mode
)
4031 op0
= convert_modes (compute_mode
, mode
, op0
, unsignedp
);
4032 op1
= convert_modes (compute_mode
, mode
, op1
, unsignedp
);
4034 /* convert_modes may have placed op1 into a register, so we
4035 must recompute the following. */
4036 op1_is_constant
= CONST_INT_P (op1
);
4037 op1_is_pow2
= (op1_is_constant
4038 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1
))
4040 && EXACT_POWER_OF_2_OR_ZERO_P (-UINTVAL (op1
))))));
4043 /* If one of the operands is a volatile MEM, copy it into a register. */
4045 if (MEM_P (op0
) && MEM_VOLATILE_P (op0
))
4046 op0
= force_reg (compute_mode
, op0
);
4047 if (MEM_P (op1
) && MEM_VOLATILE_P (op1
))
4048 op1
= force_reg (compute_mode
, op1
);
4050 /* If we need the remainder or if OP1 is constant, we need to
4051 put OP0 in a register in case it has any queued subexpressions. */
4052 if (rem_flag
|| op1_is_constant
)
4053 op0
= force_reg (compute_mode
, op0
);
4055 last
= get_last_insn ();
4057 /* Promote floor rounding to trunc rounding for unsigned operations. */
4060 if (code
== FLOOR_DIV_EXPR
)
4061 code
= TRUNC_DIV_EXPR
;
4062 if (code
== FLOOR_MOD_EXPR
)
4063 code
= TRUNC_MOD_EXPR
;
4064 if (code
== EXACT_DIV_EXPR
&& op1_is_pow2
)
4065 code
= TRUNC_DIV_EXPR
;
4068 if (op1
!= const0_rtx
)
4071 case TRUNC_MOD_EXPR
:
4072 case TRUNC_DIV_EXPR
:
4073 if (op1_is_constant
)
4077 unsigned HOST_WIDE_INT mh
, ml
;
4078 int pre_shift
, post_shift
;
4080 unsigned HOST_WIDE_INT d
= (INTVAL (op1
)
4081 & GET_MODE_MASK (compute_mode
));
4083 if (EXACT_POWER_OF_2_OR_ZERO_P (d
))
4085 pre_shift
= floor_log2 (d
);
4088 unsigned HOST_WIDE_INT mask
4089 = ((unsigned HOST_WIDE_INT
) 1 << pre_shift
) - 1;
4091 = expand_binop (compute_mode
, and_optab
, op0
,
4092 gen_int_mode (mask
, compute_mode
),
4096 return gen_lowpart (mode
, remainder
);
4098 quotient
= expand_shift (RSHIFT_EXPR
, compute_mode
, op0
,
4099 pre_shift
, tquotient
, 1);
4101 else if (size
<= HOST_BITS_PER_WIDE_INT
)
4103 if (d
>= ((unsigned HOST_WIDE_INT
) 1 << (size
- 1)))
4105 /* Most significant bit of divisor is set; emit an scc
4107 quotient
= emit_store_flag_force (tquotient
, GEU
, op0
, op1
,
4108 compute_mode
, 1, 1);
4112 /* Find a suitable multiplier and right shift count
4113 instead of multiplying with D. */
4115 mh
= choose_multiplier (d
, size
, size
,
4116 &ml
, &post_shift
, &dummy
);
4118 /* If the suggested multiplier is more than SIZE bits,
4119 we can do better for even divisors, using an
4120 initial right shift. */
4121 if (mh
!= 0 && (d
& 1) == 0)
4123 pre_shift
= floor_log2 (d
& -d
);
4124 mh
= choose_multiplier (d
>> pre_shift
, size
,
4126 &ml
, &post_shift
, &dummy
);
4136 if (post_shift
- 1 >= BITS_PER_WORD
)
4140 = (shift_cost (speed
, compute_mode
, post_shift
- 1)
4141 + shift_cost (speed
, compute_mode
, 1)
4142 + 2 * add_cost (speed
, compute_mode
));
4143 t1
= expmed_mult_highpart
4145 gen_int_mode (ml
, compute_mode
),
4146 NULL_RTX
, 1, max_cost
- extra_cost
);
4149 t2
= force_operand (gen_rtx_MINUS (compute_mode
,
4152 t3
= expand_shift (RSHIFT_EXPR
, compute_mode
,
4153 t2
, 1, NULL_RTX
, 1);
4154 t4
= force_operand (gen_rtx_PLUS (compute_mode
,
4157 quotient
= expand_shift
4158 (RSHIFT_EXPR
, compute_mode
, t4
,
4159 post_shift
- 1, tquotient
, 1);
4165 if (pre_shift
>= BITS_PER_WORD
4166 || post_shift
>= BITS_PER_WORD
)
4170 (RSHIFT_EXPR
, compute_mode
, op0
,
4171 pre_shift
, NULL_RTX
, 1);
4173 = (shift_cost (speed
, compute_mode
, pre_shift
)
4174 + shift_cost (speed
, compute_mode
, post_shift
));
4175 t2
= expmed_mult_highpart
4177 gen_int_mode (ml
, compute_mode
),
4178 NULL_RTX
, 1, max_cost
- extra_cost
);
4181 quotient
= expand_shift
4182 (RSHIFT_EXPR
, compute_mode
, t2
,
4183 post_shift
, tquotient
, 1);
4187 else /* Too wide mode to use tricky code */
4190 insn
= get_last_insn ();
4192 set_dst_reg_note (insn
, REG_EQUAL
,
4193 gen_rtx_UDIV (compute_mode
, op0
, op1
),
4196 else /* TRUNC_DIV, signed */
4198 unsigned HOST_WIDE_INT ml
;
4199 int lgup
, post_shift
;
4201 HOST_WIDE_INT d
= INTVAL (op1
);
4202 unsigned HOST_WIDE_INT abs_d
;
4204 /* Since d might be INT_MIN, we have to cast to
4205 unsigned HOST_WIDE_INT before negating to avoid
4206 undefined signed overflow. */
4208 ? (unsigned HOST_WIDE_INT
) d
4209 : - (unsigned HOST_WIDE_INT
) d
);
4211 /* n rem d = n rem -d */
4212 if (rem_flag
&& d
< 0)
4215 op1
= gen_int_mode (abs_d
, compute_mode
);
4221 quotient
= expand_unop (compute_mode
, neg_optab
, op0
,
4223 else if (HOST_BITS_PER_WIDE_INT
>= size
4224 && abs_d
== (unsigned HOST_WIDE_INT
) 1 << (size
- 1))
4226 /* This case is not handled correctly below. */
4227 quotient
= emit_store_flag (tquotient
, EQ
, op0
, op1
,
4228 compute_mode
, 1, 1);
4232 else if (EXACT_POWER_OF_2_OR_ZERO_P (d
)
4234 ? smod_pow2_cheap (speed
, compute_mode
)
4235 : sdiv_pow2_cheap (speed
, compute_mode
))
4236 /* We assume that cheap metric is true if the
4237 optab has an expander for this mode. */
4238 && ((optab_handler ((rem_flag
? smod_optab
4241 != CODE_FOR_nothing
)
4242 || (optab_handler (sdivmod_optab
,
4244 != CODE_FOR_nothing
)))
4246 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d
))
4250 remainder
= expand_smod_pow2 (compute_mode
, op0
, d
);
4252 return gen_lowpart (mode
, remainder
);
4255 if (sdiv_pow2_cheap (speed
, compute_mode
)
4256 && ((optab_handler (sdiv_optab
, compute_mode
)
4257 != CODE_FOR_nothing
)
4258 || (optab_handler (sdivmod_optab
, compute_mode
)
4259 != CODE_FOR_nothing
)))
4260 quotient
= expand_divmod (0, TRUNC_DIV_EXPR
,
4262 gen_int_mode (abs_d
,
4266 quotient
= expand_sdiv_pow2 (compute_mode
, op0
, abs_d
);
4268 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4269 negate the quotient. */
4272 insn
= get_last_insn ();
4274 && abs_d
< ((unsigned HOST_WIDE_INT
) 1
4275 << (HOST_BITS_PER_WIDE_INT
- 1)))
4276 set_dst_reg_note (insn
, REG_EQUAL
,
4277 gen_rtx_DIV (compute_mode
, op0
,
4283 quotient
= expand_unop (compute_mode
, neg_optab
,
4284 quotient
, quotient
, 0);
4287 else if (size
<= HOST_BITS_PER_WIDE_INT
)
4289 choose_multiplier (abs_d
, size
, size
- 1,
4290 &ml
, &post_shift
, &lgup
);
4291 if (ml
< (unsigned HOST_WIDE_INT
) 1 << (size
- 1))
4295 if (post_shift
>= BITS_PER_WORD
4296 || size
- 1 >= BITS_PER_WORD
)
4299 extra_cost
= (shift_cost (speed
, compute_mode
, post_shift
)
4300 + shift_cost (speed
, compute_mode
, size
- 1)
4301 + add_cost (speed
, compute_mode
));
4302 t1
= expmed_mult_highpart
4303 (compute_mode
, op0
, gen_int_mode (ml
, compute_mode
),
4304 NULL_RTX
, 0, max_cost
- extra_cost
);
4308 (RSHIFT_EXPR
, compute_mode
, t1
,
4309 post_shift
, NULL_RTX
, 0);
4311 (RSHIFT_EXPR
, compute_mode
, op0
,
4312 size
- 1, NULL_RTX
, 0);
4315 = force_operand (gen_rtx_MINUS (compute_mode
,
4320 = force_operand (gen_rtx_MINUS (compute_mode
,
4328 if (post_shift
>= BITS_PER_WORD
4329 || size
- 1 >= BITS_PER_WORD
)
4332 ml
|= (~(unsigned HOST_WIDE_INT
) 0) << (size
- 1);
4333 mlr
= gen_int_mode (ml
, compute_mode
);
4334 extra_cost
= (shift_cost (speed
, compute_mode
, post_shift
)
4335 + shift_cost (speed
, compute_mode
, size
- 1)
4336 + 2 * add_cost (speed
, compute_mode
));
4337 t1
= expmed_mult_highpart (compute_mode
, op0
, mlr
,
4339 max_cost
- extra_cost
);
4342 t2
= force_operand (gen_rtx_PLUS (compute_mode
,
4346 (RSHIFT_EXPR
, compute_mode
, t2
,
4347 post_shift
, NULL_RTX
, 0);
4349 (RSHIFT_EXPR
, compute_mode
, op0
,
4350 size
- 1, NULL_RTX
, 0);
4353 = force_operand (gen_rtx_MINUS (compute_mode
,
4358 = force_operand (gen_rtx_MINUS (compute_mode
,
4363 else /* Too wide mode to use tricky code */
4366 insn
= get_last_insn ();
4368 set_dst_reg_note (insn
, REG_EQUAL
,
4369 gen_rtx_DIV (compute_mode
, op0
, op1
),
4375 delete_insns_since (last
);
4378 case FLOOR_DIV_EXPR
:
4379 case FLOOR_MOD_EXPR
:
4380 /* We will come here only for signed operations. */
4381 if (op1_is_constant
&& HOST_BITS_PER_WIDE_INT
>= size
)
4383 unsigned HOST_WIDE_INT mh
, ml
;
4384 int pre_shift
, lgup
, post_shift
;
4385 HOST_WIDE_INT d
= INTVAL (op1
);
4389 /* We could just as easily deal with negative constants here,
4390 but it does not seem worth the trouble for GCC 2.6. */
4391 if (EXACT_POWER_OF_2_OR_ZERO_P (d
))
4393 pre_shift
= floor_log2 (d
);
4396 unsigned HOST_WIDE_INT mask
4397 = ((unsigned HOST_WIDE_INT
) 1 << pre_shift
) - 1;
4398 remainder
= expand_binop
4399 (compute_mode
, and_optab
, op0
,
4400 gen_int_mode (mask
, compute_mode
),
4401 remainder
, 0, OPTAB_LIB_WIDEN
);
4403 return gen_lowpart (mode
, remainder
);
4405 quotient
= expand_shift
4406 (RSHIFT_EXPR
, compute_mode
, op0
,
4407 pre_shift
, tquotient
, 0);
4413 mh
= choose_multiplier (d
, size
, size
- 1,
4414 &ml
, &post_shift
, &lgup
);
4417 if (post_shift
< BITS_PER_WORD
4418 && size
- 1 < BITS_PER_WORD
)
4421 (RSHIFT_EXPR
, compute_mode
, op0
,
4422 size
- 1, NULL_RTX
, 0);
4423 t2
= expand_binop (compute_mode
, xor_optab
, op0
, t1
,
4424 NULL_RTX
, 0, OPTAB_WIDEN
);
4425 extra_cost
= (shift_cost (speed
, compute_mode
, post_shift
)
4426 + shift_cost (speed
, compute_mode
, size
- 1)
4427 + 2 * add_cost (speed
, compute_mode
));
4428 t3
= expmed_mult_highpart
4429 (compute_mode
, t2
, gen_int_mode (ml
, compute_mode
),
4430 NULL_RTX
, 1, max_cost
- extra_cost
);
4434 (RSHIFT_EXPR
, compute_mode
, t3
,
4435 post_shift
, NULL_RTX
, 1);
4436 quotient
= expand_binop (compute_mode
, xor_optab
,
4437 t4
, t1
, tquotient
, 0,
4445 rtx nsign
, t1
, t2
, t3
, t4
;
4446 t1
= force_operand (gen_rtx_PLUS (compute_mode
,
4447 op0
, constm1_rtx
), NULL_RTX
);
4448 t2
= expand_binop (compute_mode
, ior_optab
, op0
, t1
, NULL_RTX
,
4450 nsign
= expand_shift
4451 (RSHIFT_EXPR
, compute_mode
, t2
,
4452 size
- 1, NULL_RTX
, 0);
4453 t3
= force_operand (gen_rtx_MINUS (compute_mode
, t1
, nsign
),
4455 t4
= expand_divmod (0, TRUNC_DIV_EXPR
, compute_mode
, t3
, op1
,
4460 t5
= expand_unop (compute_mode
, one_cmpl_optab
, nsign
,
4462 quotient
= force_operand (gen_rtx_PLUS (compute_mode
,
4471 delete_insns_since (last
);
4473 /* Try using an instruction that produces both the quotient and
4474 remainder, using truncation. We can easily compensate the quotient
4475 or remainder to get floor rounding, once we have the remainder.
4476 Notice that we compute also the final remainder value here,
4477 and return the result right away. */
4478 if (target
== 0 || GET_MODE (target
) != compute_mode
)
4479 target
= gen_reg_rtx (compute_mode
);
4484 = REG_P (target
) ? target
: gen_reg_rtx (compute_mode
);
4485 quotient
= gen_reg_rtx (compute_mode
);
4490 = REG_P (target
) ? target
: gen_reg_rtx (compute_mode
);
4491 remainder
= gen_reg_rtx (compute_mode
);
4494 if (expand_twoval_binop (sdivmod_optab
, op0
, op1
,
4495 quotient
, remainder
, 0))
4497 /* This could be computed with a branch-less sequence.
4498 Save that for later. */
4500 rtx_code_label
*label
= gen_label_rtx ();
4501 do_cmp_and_jump (remainder
, const0_rtx
, EQ
, compute_mode
, label
);
4502 tem
= expand_binop (compute_mode
, xor_optab
, op0
, op1
,
4503 NULL_RTX
, 0, OPTAB_WIDEN
);
4504 do_cmp_and_jump (tem
, const0_rtx
, GE
, compute_mode
, label
);
4505 expand_dec (quotient
, const1_rtx
);
4506 expand_inc (remainder
, op1
);
4508 return gen_lowpart (mode
, rem_flag
? remainder
: quotient
);
4511 /* No luck with division elimination or divmod. Have to do it
4512 by conditionally adjusting op0 *and* the result. */
4514 rtx_code_label
*label1
, *label2
, *label3
, *label4
, *label5
;
4518 quotient
= gen_reg_rtx (compute_mode
);
4519 adjusted_op0
= copy_to_mode_reg (compute_mode
, op0
);
4520 label1
= gen_label_rtx ();
4521 label2
= gen_label_rtx ();
4522 label3
= gen_label_rtx ();
4523 label4
= gen_label_rtx ();
4524 label5
= gen_label_rtx ();
4525 do_cmp_and_jump (op1
, const0_rtx
, LT
, compute_mode
, label2
);
4526 do_cmp_and_jump (adjusted_op0
, const0_rtx
, LT
, compute_mode
, label1
);
4527 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4528 quotient
, 0, OPTAB_LIB_WIDEN
);
4529 if (tem
!= quotient
)
4530 emit_move_insn (quotient
, tem
);
4531 emit_jump_insn (targetm
.gen_jump (label5
));
4533 emit_label (label1
);
4534 expand_inc (adjusted_op0
, const1_rtx
);
4535 emit_jump_insn (targetm
.gen_jump (label4
));
4537 emit_label (label2
);
4538 do_cmp_and_jump (adjusted_op0
, const0_rtx
, GT
, compute_mode
, label3
);
4539 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4540 quotient
, 0, OPTAB_LIB_WIDEN
);
4541 if (tem
!= quotient
)
4542 emit_move_insn (quotient
, tem
);
4543 emit_jump_insn (targetm
.gen_jump (label5
));
4545 emit_label (label3
);
4546 expand_dec (adjusted_op0
, const1_rtx
);
4547 emit_label (label4
);
4548 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4549 quotient
, 0, OPTAB_LIB_WIDEN
);
4550 if (tem
!= quotient
)
4551 emit_move_insn (quotient
, tem
);
4552 expand_dec (quotient
, const1_rtx
);
4553 emit_label (label5
);
4561 if (op1_is_constant
&& EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1
)))
4564 unsigned HOST_WIDE_INT d
= INTVAL (op1
);
4565 t1
= expand_shift (RSHIFT_EXPR
, compute_mode
, op0
,
4566 floor_log2 (d
), tquotient
, 1);
4567 t2
= expand_binop (compute_mode
, and_optab
, op0
,
4568 gen_int_mode (d
- 1, compute_mode
),
4569 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4570 t3
= gen_reg_rtx (compute_mode
);
4571 t3
= emit_store_flag (t3
, NE
, t2
, const0_rtx
,
4572 compute_mode
, 1, 1);
4575 rtx_code_label
*lab
;
4576 lab
= gen_label_rtx ();
4577 do_cmp_and_jump (t2
, const0_rtx
, EQ
, compute_mode
, lab
);
4578 expand_inc (t1
, const1_rtx
);
4583 quotient
= force_operand (gen_rtx_PLUS (compute_mode
,
4589 /* Try using an instruction that produces both the quotient and
4590 remainder, using truncation. We can easily compensate the
4591 quotient or remainder to get ceiling rounding, once we have the
4592 remainder. Notice that we compute also the final remainder
4593 value here, and return the result right away. */
4594 if (target
== 0 || GET_MODE (target
) != compute_mode
)
4595 target
= gen_reg_rtx (compute_mode
);
4599 remainder
= (REG_P (target
)
4600 ? target
: gen_reg_rtx (compute_mode
));
4601 quotient
= gen_reg_rtx (compute_mode
);
4605 quotient
= (REG_P (target
)
4606 ? target
: gen_reg_rtx (compute_mode
));
4607 remainder
= gen_reg_rtx (compute_mode
);
4610 if (expand_twoval_binop (udivmod_optab
, op0
, op1
, quotient
,
4613 /* This could be computed with a branch-less sequence.
4614 Save that for later. */
4615 rtx_code_label
*label
= gen_label_rtx ();
4616 do_cmp_and_jump (remainder
, const0_rtx
, EQ
,
4617 compute_mode
, label
);
4618 expand_inc (quotient
, const1_rtx
);
4619 expand_dec (remainder
, op1
);
4621 return gen_lowpart (mode
, rem_flag
? remainder
: quotient
);
4624 /* No luck with division elimination or divmod. Have to do it
4625 by conditionally adjusting op0 *and* the result. */
4627 rtx_code_label
*label1
, *label2
;
4628 rtx adjusted_op0
, tem
;
4630 quotient
= gen_reg_rtx (compute_mode
);
4631 adjusted_op0
= copy_to_mode_reg (compute_mode
, op0
);
4632 label1
= gen_label_rtx ();
4633 label2
= gen_label_rtx ();
4634 do_cmp_and_jump (adjusted_op0
, const0_rtx
, NE
,
4635 compute_mode
, label1
);
4636 emit_move_insn (quotient
, const0_rtx
);
4637 emit_jump_insn (targetm
.gen_jump (label2
));
4639 emit_label (label1
);
4640 expand_dec (adjusted_op0
, const1_rtx
);
4641 tem
= expand_binop (compute_mode
, udiv_optab
, adjusted_op0
, op1
,
4642 quotient
, 1, OPTAB_LIB_WIDEN
);
4643 if (tem
!= quotient
)
4644 emit_move_insn (quotient
, tem
);
4645 expand_inc (quotient
, const1_rtx
);
4646 emit_label (label2
);
4651 if (op1_is_constant
&& EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1
))
4652 && INTVAL (op1
) >= 0)
4654 /* This is extremely similar to the code for the unsigned case
4655 above. For 2.7 we should merge these variants, but for
4656 2.6.1 I don't want to touch the code for unsigned since that
4657 get used in C. The signed case will only be used by other
4661 unsigned HOST_WIDE_INT d
= INTVAL (op1
);
4662 t1
= expand_shift (RSHIFT_EXPR
, compute_mode
, op0
,
4663 floor_log2 (d
), tquotient
, 0);
4664 t2
= expand_binop (compute_mode
, and_optab
, op0
,
4665 gen_int_mode (d
- 1, compute_mode
),
4666 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4667 t3
= gen_reg_rtx (compute_mode
);
4668 t3
= emit_store_flag (t3
, NE
, t2
, const0_rtx
,
4669 compute_mode
, 1, 1);
4672 rtx_code_label
*lab
;
4673 lab
= gen_label_rtx ();
4674 do_cmp_and_jump (t2
, const0_rtx
, EQ
, compute_mode
, lab
);
4675 expand_inc (t1
, const1_rtx
);
4680 quotient
= force_operand (gen_rtx_PLUS (compute_mode
,
4686 /* Try using an instruction that produces both the quotient and
4687 remainder, using truncation. We can easily compensate the
4688 quotient or remainder to get ceiling rounding, once we have the
4689 remainder. Notice that we compute also the final remainder
4690 value here, and return the result right away. */
4691 if (target
== 0 || GET_MODE (target
) != compute_mode
)
4692 target
= gen_reg_rtx (compute_mode
);
4695 remainder
= (REG_P (target
)
4696 ? target
: gen_reg_rtx (compute_mode
));
4697 quotient
= gen_reg_rtx (compute_mode
);
4701 quotient
= (REG_P (target
)
4702 ? target
: gen_reg_rtx (compute_mode
));
4703 remainder
= gen_reg_rtx (compute_mode
);
4706 if (expand_twoval_binop (sdivmod_optab
, op0
, op1
, quotient
,
4709 /* This could be computed with a branch-less sequence.
4710 Save that for later. */
4712 rtx_code_label
*label
= gen_label_rtx ();
4713 do_cmp_and_jump (remainder
, const0_rtx
, EQ
,
4714 compute_mode
, label
);
4715 tem
= expand_binop (compute_mode
, xor_optab
, op0
, op1
,
4716 NULL_RTX
, 0, OPTAB_WIDEN
);
4717 do_cmp_and_jump (tem
, const0_rtx
, LT
, compute_mode
, label
);
4718 expand_inc (quotient
, const1_rtx
);
4719 expand_dec (remainder
, op1
);
4721 return gen_lowpart (mode
, rem_flag
? remainder
: quotient
);
4724 /* No luck with division elimination or divmod. Have to do it
4725 by conditionally adjusting op0 *and* the result. */
4727 rtx_code_label
*label1
, *label2
, *label3
, *label4
, *label5
;
4731 quotient
= gen_reg_rtx (compute_mode
);
4732 adjusted_op0
= copy_to_mode_reg (compute_mode
, op0
);
4733 label1
= gen_label_rtx ();
4734 label2
= gen_label_rtx ();
4735 label3
= gen_label_rtx ();
4736 label4
= gen_label_rtx ();
4737 label5
= gen_label_rtx ();
4738 do_cmp_and_jump (op1
, const0_rtx
, LT
, compute_mode
, label2
);
4739 do_cmp_and_jump (adjusted_op0
, const0_rtx
, GT
,
4740 compute_mode
, label1
);
4741 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4742 quotient
, 0, OPTAB_LIB_WIDEN
);
4743 if (tem
!= quotient
)
4744 emit_move_insn (quotient
, tem
);
4745 emit_jump_insn (targetm
.gen_jump (label5
));
4747 emit_label (label1
);
4748 expand_dec (adjusted_op0
, const1_rtx
);
4749 emit_jump_insn (targetm
.gen_jump (label4
));
4751 emit_label (label2
);
4752 do_cmp_and_jump (adjusted_op0
, const0_rtx
, LT
,
4753 compute_mode
, label3
);
4754 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4755 quotient
, 0, OPTAB_LIB_WIDEN
);
4756 if (tem
!= quotient
)
4757 emit_move_insn (quotient
, tem
);
4758 emit_jump_insn (targetm
.gen_jump (label5
));
4760 emit_label (label3
);
4761 expand_inc (adjusted_op0
, const1_rtx
);
4762 emit_label (label4
);
4763 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4764 quotient
, 0, OPTAB_LIB_WIDEN
);
4765 if (tem
!= quotient
)
4766 emit_move_insn (quotient
, tem
);
4767 expand_inc (quotient
, const1_rtx
);
4768 emit_label (label5
);
4773 case EXACT_DIV_EXPR
:
4774 if (op1_is_constant
&& HOST_BITS_PER_WIDE_INT
>= size
)
4776 HOST_WIDE_INT d
= INTVAL (op1
);
4777 unsigned HOST_WIDE_INT ml
;
4781 pre_shift
= floor_log2 (d
& -d
);
4782 ml
= invert_mod2n (d
>> pre_shift
, size
);
4783 t1
= expand_shift (RSHIFT_EXPR
, compute_mode
, op0
,
4784 pre_shift
, NULL_RTX
, unsignedp
);
4785 quotient
= expand_mult (compute_mode
, t1
,
4786 gen_int_mode (ml
, compute_mode
),
4789 insn
= get_last_insn ();
4790 set_dst_reg_note (insn
, REG_EQUAL
,
4791 gen_rtx_fmt_ee (unsignedp
? UDIV
: DIV
,
4792 compute_mode
, op0
, op1
),
4797 case ROUND_DIV_EXPR
:
4798 case ROUND_MOD_EXPR
:
4802 rtx_code_label
*label
;
4803 label
= gen_label_rtx ();
4804 quotient
= gen_reg_rtx (compute_mode
);
4805 remainder
= gen_reg_rtx (compute_mode
);
4806 if (expand_twoval_binop (udivmod_optab
, op0
, op1
, quotient
, remainder
, 1) == 0)
4809 quotient
= expand_binop (compute_mode
, udiv_optab
, op0
, op1
,
4810 quotient
, 1, OPTAB_LIB_WIDEN
);
4811 tem
= expand_mult (compute_mode
, quotient
, op1
, NULL_RTX
, 1);
4812 remainder
= expand_binop (compute_mode
, sub_optab
, op0
, tem
,
4813 remainder
, 1, OPTAB_LIB_WIDEN
);
4815 tem
= plus_constant (compute_mode
, op1
, -1);
4816 tem
= expand_shift (RSHIFT_EXPR
, compute_mode
, tem
, 1, NULL_RTX
, 1);
4817 do_cmp_and_jump (remainder
, tem
, LEU
, compute_mode
, label
);
4818 expand_inc (quotient
, const1_rtx
);
4819 expand_dec (remainder
, op1
);
4824 rtx abs_rem
, abs_op1
, tem
, mask
;
4825 rtx_code_label
*label
;
4826 label
= gen_label_rtx ();
4827 quotient
= gen_reg_rtx (compute_mode
);
4828 remainder
= gen_reg_rtx (compute_mode
);
4829 if (expand_twoval_binop (sdivmod_optab
, op0
, op1
, quotient
, remainder
, 0) == 0)
4832 quotient
= expand_binop (compute_mode
, sdiv_optab
, op0
, op1
,
4833 quotient
, 0, OPTAB_LIB_WIDEN
);
4834 tem
= expand_mult (compute_mode
, quotient
, op1
, NULL_RTX
, 0);
4835 remainder
= expand_binop (compute_mode
, sub_optab
, op0
, tem
,
4836 remainder
, 0, OPTAB_LIB_WIDEN
);
4838 abs_rem
= expand_abs (compute_mode
, remainder
, NULL_RTX
, 1, 0);
4839 abs_op1
= expand_abs (compute_mode
, op1
, NULL_RTX
, 1, 0);
4840 tem
= expand_shift (LSHIFT_EXPR
, compute_mode
, abs_rem
,
4842 do_cmp_and_jump (tem
, abs_op1
, LTU
, compute_mode
, label
);
4843 tem
= expand_binop (compute_mode
, xor_optab
, op0
, op1
,
4844 NULL_RTX
, 0, OPTAB_WIDEN
);
4845 mask
= expand_shift (RSHIFT_EXPR
, compute_mode
, tem
,
4846 size
- 1, NULL_RTX
, 0);
4847 tem
= expand_binop (compute_mode
, xor_optab
, mask
, const1_rtx
,
4848 NULL_RTX
, 0, OPTAB_WIDEN
);
4849 tem
= expand_binop (compute_mode
, sub_optab
, tem
, mask
,
4850 NULL_RTX
, 0, OPTAB_WIDEN
);
4851 expand_inc (quotient
, tem
);
4852 tem
= expand_binop (compute_mode
, xor_optab
, mask
, op1
,
4853 NULL_RTX
, 0, OPTAB_WIDEN
);
4854 tem
= expand_binop (compute_mode
, sub_optab
, tem
, mask
,
4855 NULL_RTX
, 0, OPTAB_WIDEN
);
4856 expand_dec (remainder
, tem
);
4859 return gen_lowpart (mode
, rem_flag
? remainder
: quotient
);
4867 if (target
&& GET_MODE (target
) != compute_mode
)
4872 /* Try to produce the remainder without producing the quotient.
4873 If we seem to have a divmod pattern that does not require widening,
4874 don't try widening here. We should really have a WIDEN argument
4875 to expand_twoval_binop, since what we'd really like to do here is
4876 1) try a mod insn in compute_mode
4877 2) try a divmod insn in compute_mode
4878 3) try a div insn in compute_mode and multiply-subtract to get
4880 4) try the same things with widening allowed. */
4882 = sign_expand_binop (compute_mode
, umod_optab
, smod_optab
,
4885 ((optab_handler (optab2
, compute_mode
)
4886 != CODE_FOR_nothing
)
4887 ? OPTAB_DIRECT
: OPTAB_WIDEN
));
4890 /* No luck there. Can we do remainder and divide at once
4891 without a library call? */
4892 remainder
= gen_reg_rtx (compute_mode
);
4893 if (! expand_twoval_binop ((unsignedp
4897 NULL_RTX
, remainder
, unsignedp
))
4902 return gen_lowpart (mode
, remainder
);
4905 /* Produce the quotient. Try a quotient insn, but not a library call.
4906 If we have a divmod in this mode, use it in preference to widening
4907 the div (for this test we assume it will not fail). Note that optab2
4908 is set to the one of the two optabs that the call below will use. */
4910 = sign_expand_binop (compute_mode
, udiv_optab
, sdiv_optab
,
4911 op0
, op1
, rem_flag
? NULL_RTX
: target
,
4913 ((optab_handler (optab2
, compute_mode
)
4914 != CODE_FOR_nothing
)
4915 ? OPTAB_DIRECT
: OPTAB_WIDEN
));
4919 /* No luck there. Try a quotient-and-remainder insn,
4920 keeping the quotient alone. */
4921 quotient
= gen_reg_rtx (compute_mode
);
4922 if (! expand_twoval_binop (unsignedp
? udivmod_optab
: sdivmod_optab
,
4924 quotient
, NULL_RTX
, unsignedp
))
4928 /* Still no luck. If we are not computing the remainder,
4929 use a library call for the quotient. */
4930 quotient
= sign_expand_binop (compute_mode
,
4931 udiv_optab
, sdiv_optab
,
4933 unsignedp
, OPTAB_LIB_WIDEN
);
4940 if (target
&& GET_MODE (target
) != compute_mode
)
4945 /* No divide instruction either. Use library for remainder. */
4946 remainder
= sign_expand_binop (compute_mode
, umod_optab
, smod_optab
,
4948 unsignedp
, OPTAB_LIB_WIDEN
);
4949 /* No remainder function. Try a quotient-and-remainder
4950 function, keeping the remainder. */
4953 remainder
= gen_reg_rtx (compute_mode
);
4954 if (!expand_twoval_binop_libfunc
4955 (unsignedp
? udivmod_optab
: sdivmod_optab
,
4957 NULL_RTX
, remainder
,
4958 unsignedp
? UMOD
: MOD
))
4959 remainder
= NULL_RTX
;
4964 /* We divided. Now finish doing X - Y * (X / Y). */
4965 remainder
= expand_mult (compute_mode
, quotient
, op1
,
4966 NULL_RTX
, unsignedp
);
4967 remainder
= expand_binop (compute_mode
, sub_optab
, op0
,
4968 remainder
, target
, unsignedp
,
4973 return gen_lowpart (mode
, rem_flag
? remainder
: quotient
);
4976 /* Return a tree node with data type TYPE, describing the value of X.
4977 Usually this is an VAR_DECL, if there is no obvious better choice.
4978 X may be an expression, however we only support those expressions
4979 generated by loop.c. */
4982 make_tree (tree type
, rtx x
)
4986 switch (GET_CODE (x
))
4989 case CONST_WIDE_INT
:
4990 t
= wide_int_to_tree (type
, std::make_pair (x
, TYPE_MODE (type
)));
4994 STATIC_ASSERT (HOST_BITS_PER_WIDE_INT
* 2 <= MAX_BITSIZE_MODE_ANY_INT
);
4995 if (TARGET_SUPPORTS_WIDE_INT
== 0 && GET_MODE (x
) == VOIDmode
)
4996 t
= wide_int_to_tree (type
,
4997 wide_int::from_array (&CONST_DOUBLE_LOW (x
), 2,
4998 HOST_BITS_PER_WIDE_INT
* 2));
5003 REAL_VALUE_FROM_CONST_DOUBLE (d
, x
);
5004 t
= build_real (type
, d
);
5011 int units
= CONST_VECTOR_NUNITS (x
);
5012 tree itype
= TREE_TYPE (type
);
5016 /* Build a tree with vector elements. */
5017 elts
= XALLOCAVEC (tree
, units
);
5018 for (i
= units
- 1; i
>= 0; --i
)
5020 rtx elt
= CONST_VECTOR_ELT (x
, i
);
5021 elts
[i
] = make_tree (itype
, elt
);
5024 return build_vector (type
, elts
);
5028 return fold_build2 (PLUS_EXPR
, type
, make_tree (type
, XEXP (x
, 0)),
5029 make_tree (type
, XEXP (x
, 1)));
5032 return fold_build2 (MINUS_EXPR
, type
, make_tree (type
, XEXP (x
, 0)),
5033 make_tree (type
, XEXP (x
, 1)));
5036 return fold_build1 (NEGATE_EXPR
, type
, make_tree (type
, XEXP (x
, 0)));
5039 return fold_build2 (MULT_EXPR
, type
, make_tree (type
, XEXP (x
, 0)),
5040 make_tree (type
, XEXP (x
, 1)));
5043 return fold_build2 (LSHIFT_EXPR
, type
, make_tree (type
, XEXP (x
, 0)),
5044 make_tree (type
, XEXP (x
, 1)));
5047 t
= unsigned_type_for (type
);
5048 return fold_convert (type
, build2 (RSHIFT_EXPR
, t
,
5049 make_tree (t
, XEXP (x
, 0)),
5050 make_tree (type
, XEXP (x
, 1))));
5053 t
= signed_type_for (type
);
5054 return fold_convert (type
, build2 (RSHIFT_EXPR
, t
,
5055 make_tree (t
, XEXP (x
, 0)),
5056 make_tree (type
, XEXP (x
, 1))));
5059 if (TREE_CODE (type
) != REAL_TYPE
)
5060 t
= signed_type_for (type
);
5064 return fold_convert (type
, build2 (TRUNC_DIV_EXPR
, t
,
5065 make_tree (t
, XEXP (x
, 0)),
5066 make_tree (t
, XEXP (x
, 1))));
5068 t
= unsigned_type_for (type
);
5069 return fold_convert (type
, build2 (TRUNC_DIV_EXPR
, t
,
5070 make_tree (t
, XEXP (x
, 0)),
5071 make_tree (t
, XEXP (x
, 1))));
5075 t
= lang_hooks
.types
.type_for_mode (GET_MODE (XEXP (x
, 0)),
5076 GET_CODE (x
) == ZERO_EXTEND
);
5077 return fold_convert (type
, make_tree (t
, XEXP (x
, 0)));
5080 return make_tree (type
, XEXP (x
, 0));
5083 t
= SYMBOL_REF_DECL (x
);
5085 return fold_convert (type
, build_fold_addr_expr (t
));
5086 /* else fall through. */
5089 t
= build_decl (RTL_LOCATION (x
), VAR_DECL
, NULL_TREE
, type
);
5091 /* If TYPE is a POINTER_TYPE, we might need to convert X from
5092 address mode to pointer mode. */
5093 if (POINTER_TYPE_P (type
))
5094 x
= convert_memory_address_addr_space
5095 (TYPE_MODE (type
), x
, TYPE_ADDR_SPACE (TREE_TYPE (type
)));
5097 /* Note that we do *not* use SET_DECL_RTL here, because we do not
5098 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
5099 t
->decl_with_rtl
.rtl
= x
;
5105 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5106 and returning TARGET.
5108 If TARGET is 0, a pseudo-register or constant is returned. */
5111 expand_and (machine_mode mode
, rtx op0
, rtx op1
, rtx target
)
5115 if (GET_MODE (op0
) == VOIDmode
&& GET_MODE (op1
) == VOIDmode
)
5116 tem
= simplify_binary_operation (AND
, mode
, op0
, op1
);
5118 tem
= expand_binop (mode
, and_optab
, op0
, op1
, target
, 0, OPTAB_LIB_WIDEN
);
5122 else if (tem
!= target
)
5123 emit_move_insn (target
, tem
);
5127 /* Helper function for emit_store_flag. */
5129 emit_cstore (rtx target
, enum insn_code icode
, enum rtx_code code
,
5130 machine_mode mode
, machine_mode compare_mode
,
5131 int unsignedp
, rtx x
, rtx y
, int normalizep
,
5132 machine_mode target_mode
)
5134 struct expand_operand ops
[4];
5135 rtx op0
, comparison
, subtarget
;
5137 machine_mode result_mode
= targetm
.cstore_mode (icode
);
5139 last
= get_last_insn ();
5140 x
= prepare_operand (icode
, x
, 2, mode
, compare_mode
, unsignedp
);
5141 y
= prepare_operand (icode
, y
, 3, mode
, compare_mode
, unsignedp
);
5144 delete_insns_since (last
);
5148 if (target_mode
== VOIDmode
)
5149 target_mode
= result_mode
;
5151 target
= gen_reg_rtx (target_mode
);
5153 comparison
= gen_rtx_fmt_ee (code
, result_mode
, x
, y
);
5155 create_output_operand (&ops
[0], optimize
? NULL_RTX
: target
, result_mode
);
5156 create_fixed_operand (&ops
[1], comparison
);
5157 create_fixed_operand (&ops
[2], x
);
5158 create_fixed_operand (&ops
[3], y
);
5159 if (!maybe_expand_insn (icode
, 4, ops
))
5161 delete_insns_since (last
);
5164 subtarget
= ops
[0].value
;
5166 /* If we are converting to a wider mode, first convert to
5167 TARGET_MODE, then normalize. This produces better combining
5168 opportunities on machines that have a SIGN_EXTRACT when we are
5169 testing a single bit. This mostly benefits the 68k.
5171 If STORE_FLAG_VALUE does not have the sign bit set when
5172 interpreted in MODE, we can do this conversion as unsigned, which
5173 is usually more efficient. */
5174 if (GET_MODE_SIZE (target_mode
) > GET_MODE_SIZE (result_mode
))
5176 convert_move (target
, subtarget
,
5177 val_signbit_known_clear_p (result_mode
,
5180 result_mode
= target_mode
;
5185 /* If we want to keep subexpressions around, don't reuse our last
5190 /* Now normalize to the proper value in MODE. Sometimes we don't
5191 have to do anything. */
5192 if (normalizep
== 0 || normalizep
== STORE_FLAG_VALUE
)
5194 /* STORE_FLAG_VALUE might be the most negative number, so write
5195 the comparison this way to avoid a compiler-time warning. */
5196 else if (- normalizep
== STORE_FLAG_VALUE
)
5197 op0
= expand_unop (result_mode
, neg_optab
, op0
, subtarget
, 0);
5199 /* We don't want to use STORE_FLAG_VALUE < 0 below since this makes
5200 it hard to use a value of just the sign bit due to ANSI integer
5201 constant typing rules. */
5202 else if (val_signbit_known_set_p (result_mode
, STORE_FLAG_VALUE
))
5203 op0
= expand_shift (RSHIFT_EXPR
, result_mode
, op0
,
5204 GET_MODE_BITSIZE (result_mode
) - 1, subtarget
,
5208 gcc_assert (STORE_FLAG_VALUE
& 1);
5210 op0
= expand_and (result_mode
, op0
, const1_rtx
, subtarget
);
5211 if (normalizep
== -1)
5212 op0
= expand_unop (result_mode
, neg_optab
, op0
, op0
, 0);
5215 /* If we were converting to a smaller mode, do the conversion now. */
5216 if (target_mode
!= result_mode
)
5218 convert_move (target
, op0
, 0);
5226 /* A subroutine of emit_store_flag only including "tricks" that do not
5227 need a recursive call. These are kept separate to avoid infinite
5231 emit_store_flag_1 (rtx target
, enum rtx_code code
, rtx op0
, rtx op1
,
5232 machine_mode mode
, int unsignedp
, int normalizep
,
5233 machine_mode target_mode
)
5236 enum insn_code icode
;
5237 machine_mode compare_mode
;
5238 enum mode_class mclass
;
5239 enum rtx_code scode
;
5242 code
= unsigned_condition (code
);
5243 scode
= swap_condition (code
);
5245 /* If one operand is constant, make it the second one. Only do this
5246 if the other operand is not constant as well. */
5248 if (swap_commutative_operands_p (op0
, op1
))
5250 std::swap (op0
, op1
);
5251 code
= swap_condition (code
);
5254 if (mode
== VOIDmode
)
5255 mode
= GET_MODE (op0
);
5257 /* For some comparisons with 1 and -1, we can convert this to
5258 comparisons with zero. This will often produce more opportunities for
5259 store-flag insns. */
5264 if (op1
== const1_rtx
)
5265 op1
= const0_rtx
, code
= LE
;
5268 if (op1
== constm1_rtx
)
5269 op1
= const0_rtx
, code
= LT
;
5272 if (op1
== const1_rtx
)
5273 op1
= const0_rtx
, code
= GT
;
5276 if (op1
== constm1_rtx
)
5277 op1
= const0_rtx
, code
= GE
;
5280 if (op1
== const1_rtx
)
5281 op1
= const0_rtx
, code
= NE
;
5284 if (op1
== const1_rtx
)
5285 op1
= const0_rtx
, code
= EQ
;
5291 /* If we are comparing a double-word integer with zero or -1, we can
5292 convert the comparison into one involving a single word. */
5293 if (GET_MODE_BITSIZE (mode
) == BITS_PER_WORD
* 2
5294 && GET_MODE_CLASS (mode
) == MODE_INT
5295 && (!MEM_P (op0
) || ! MEM_VOLATILE_P (op0
)))
5298 if ((code
== EQ
|| code
== NE
)
5299 && (op1
== const0_rtx
|| op1
== constm1_rtx
))
5303 /* Do a logical OR or AND of the two words and compare the
5305 op00
= simplify_gen_subreg (word_mode
, op0
, mode
, 0);
5306 op01
= simplify_gen_subreg (word_mode
, op0
, mode
, UNITS_PER_WORD
);
5307 tem
= expand_binop (word_mode
,
5308 op1
== const0_rtx
? ior_optab
: and_optab
,
5309 op00
, op01
, NULL_RTX
, unsignedp
,
5313 tem
= emit_store_flag (NULL_RTX
, code
, tem
, op1
, word_mode
,
5314 unsignedp
, normalizep
);
5316 else if ((code
== LT
|| code
== GE
) && op1
== const0_rtx
)
5320 /* If testing the sign bit, can just test on high word. */
5321 op0h
= simplify_gen_subreg (word_mode
, op0
, mode
,
5322 subreg_highpart_offset (word_mode
,
5324 tem
= emit_store_flag (NULL_RTX
, code
, op0h
, op1
, word_mode
,
5325 unsignedp
, normalizep
);
5332 if (target_mode
== VOIDmode
|| GET_MODE (tem
) == target_mode
)
5335 target
= gen_reg_rtx (target_mode
);
5337 convert_move (target
, tem
,
5338 !val_signbit_known_set_p (word_mode
,
5339 (normalizep
? normalizep
5340 : STORE_FLAG_VALUE
)));
5345 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5346 complement of A (for GE) and shifting the sign bit to the low bit. */
5347 if (op1
== const0_rtx
&& (code
== LT
|| code
== GE
)
5348 && GET_MODE_CLASS (mode
) == MODE_INT
5349 && (normalizep
|| STORE_FLAG_VALUE
== 1
5350 || val_signbit_p (mode
, STORE_FLAG_VALUE
)))
5357 /* If the result is to be wider than OP0, it is best to convert it
5358 first. If it is to be narrower, it is *incorrect* to convert it
5360 else if (GET_MODE_SIZE (target_mode
) > GET_MODE_SIZE (mode
))
5362 op0
= convert_modes (target_mode
, mode
, op0
, 0);
5366 if (target_mode
!= mode
)
5370 op0
= expand_unop (mode
, one_cmpl_optab
, op0
,
5371 ((STORE_FLAG_VALUE
== 1 || normalizep
)
5372 ? 0 : subtarget
), 0);
5374 if (STORE_FLAG_VALUE
== 1 || normalizep
)
5375 /* If we are supposed to produce a 0/1 value, we want to do
5376 a logical shift from the sign bit to the low-order bit; for
5377 a -1/0 value, we do an arithmetic shift. */
5378 op0
= expand_shift (RSHIFT_EXPR
, mode
, op0
,
5379 GET_MODE_BITSIZE (mode
) - 1,
5380 subtarget
, normalizep
!= -1);
5382 if (mode
!= target_mode
)
5383 op0
= convert_modes (target_mode
, mode
, op0
, 0);
5388 mclass
= GET_MODE_CLASS (mode
);
5389 for (compare_mode
= mode
; compare_mode
!= VOIDmode
;
5390 compare_mode
= GET_MODE_WIDER_MODE (compare_mode
))
5392 machine_mode optab_mode
= mclass
== MODE_CC
? CCmode
: compare_mode
;
5393 icode
= optab_handler (cstore_optab
, optab_mode
);
5394 if (icode
!= CODE_FOR_nothing
)
5396 do_pending_stack_adjust ();
5397 rtx tem
= emit_cstore (target
, icode
, code
, mode
, compare_mode
,
5398 unsignedp
, op0
, op1
, normalizep
, target_mode
);
5402 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
)
5404 tem
= emit_cstore (target
, icode
, scode
, mode
, compare_mode
,
5405 unsignedp
, op1
, op0
, normalizep
, target_mode
);
5416 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5417 and storing in TARGET. Normally return TARGET.
5418 Return 0 if that cannot be done.
5420 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5421 it is VOIDmode, they cannot both be CONST_INT.
5423 UNSIGNEDP is for the case where we have to widen the operands
5424 to perform the operation. It says to use zero-extension.
5426 NORMALIZEP is 1 if we should convert the result to be either zero
5427 or one. Normalize is -1 if we should convert the result to be
5428 either zero or -1. If NORMALIZEP is zero, the result will be left
5429 "raw" out of the scc insn. */
5432 emit_store_flag (rtx target
, enum rtx_code code
, rtx op0
, rtx op1
,
5433 machine_mode mode
, int unsignedp
, int normalizep
)
5435 machine_mode target_mode
= target
? GET_MODE (target
) : VOIDmode
;
5436 enum rtx_code rcode
;
5441 /* If we compare constants, we shouldn't use a store-flag operation,
5442 but a constant load. We can get there via the vanilla route that
5443 usually generates a compare-branch sequence, but will in this case
5444 fold the comparison to a constant, and thus elide the branch. */
5445 if (CONSTANT_P (op0
) && CONSTANT_P (op1
))
5448 tem
= emit_store_flag_1 (target
, code
, op0
, op1
, mode
, unsignedp
, normalizep
,
5453 /* If we reached here, we can't do this with a scc insn, however there
5454 are some comparisons that can be done in other ways. Don't do any
5455 of these cases if branches are very cheap. */
5456 if (BRANCH_COST (optimize_insn_for_speed_p (), false) == 0)
5459 /* See what we need to return. We can only return a 1, -1, or the
5462 if (normalizep
== 0)
5464 if (STORE_FLAG_VALUE
== 1 || STORE_FLAG_VALUE
== -1)
5465 normalizep
= STORE_FLAG_VALUE
;
5467 else if (val_signbit_p (mode
, STORE_FLAG_VALUE
))
5473 last
= get_last_insn ();
5475 /* If optimizing, use different pseudo registers for each insn, instead
5476 of reusing the same pseudo. This leads to better CSE, but slows
5477 down the compiler, since there are more pseudos */
5478 subtarget
= (!optimize
5479 && (target_mode
== mode
)) ? target
: NULL_RTX
;
5480 trueval
= GEN_INT (normalizep
? normalizep
: STORE_FLAG_VALUE
);
5482 /* For floating-point comparisons, try the reverse comparison or try
5483 changing the "orderedness" of the comparison. */
5484 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
)
5486 enum rtx_code first_code
;
5489 rcode
= reverse_condition_maybe_unordered (code
);
5490 if (can_compare_p (rcode
, mode
, ccp_store_flag
)
5491 && (code
== ORDERED
|| code
== UNORDERED
5492 || (! HONOR_NANS (mode
) && (code
== LTGT
|| code
== UNEQ
))
5493 || (! HONOR_SNANS (mode
) && (code
== EQ
|| code
== NE
))))
5495 int want_add
= ((STORE_FLAG_VALUE
== 1 && normalizep
== -1)
5496 || (STORE_FLAG_VALUE
== -1 && normalizep
== 1));
5498 /* For the reverse comparison, use either an addition or a XOR. */
5500 && rtx_cost (GEN_INT (normalizep
), mode
, PLUS
, 1,
5501 optimize_insn_for_speed_p ()) == 0)
5503 tem
= emit_store_flag_1 (subtarget
, rcode
, op0
, op1
, mode
, 0,
5504 STORE_FLAG_VALUE
, target_mode
);
5506 return expand_binop (target_mode
, add_optab
, tem
,
5507 gen_int_mode (normalizep
, target_mode
),
5508 target
, 0, OPTAB_WIDEN
);
5511 && rtx_cost (trueval
, mode
, XOR
, 1,
5512 optimize_insn_for_speed_p ()) == 0)
5514 tem
= emit_store_flag_1 (subtarget
, rcode
, op0
, op1
, mode
, 0,
5515 normalizep
, target_mode
);
5517 return expand_binop (target_mode
, xor_optab
, tem
, trueval
,
5518 target
, INTVAL (trueval
) >= 0, OPTAB_WIDEN
);
5522 delete_insns_since (last
);
5524 /* Cannot split ORDERED and UNORDERED, only try the above trick. */
5525 if (code
== ORDERED
|| code
== UNORDERED
)
5528 and_them
= split_comparison (code
, mode
, &first_code
, &code
);
5530 /* If there are no NaNs, the first comparison should always fall through.
5531 Effectively change the comparison to the other one. */
5532 if (!HONOR_NANS (mode
))
5534 gcc_assert (first_code
== (and_them
? ORDERED
: UNORDERED
));
5535 return emit_store_flag_1 (target
, code
, op0
, op1
, mode
, 0, normalizep
,
5539 if (!HAVE_conditional_move
)
5542 /* Try using a setcc instruction for ORDERED/UNORDERED, followed by a
5543 conditional move. */
5544 tem
= emit_store_flag_1 (subtarget
, first_code
, op0
, op1
, mode
, 0,
5545 normalizep
, target_mode
);
5550 tem
= emit_conditional_move (target
, code
, op0
, op1
, mode
,
5551 tem
, const0_rtx
, GET_MODE (tem
), 0);
5553 tem
= emit_conditional_move (target
, code
, op0
, op1
, mode
,
5554 trueval
, tem
, GET_MODE (tem
), 0);
5557 delete_insns_since (last
);
5561 /* The remaining tricks only apply to integer comparisons. */
5563 if (GET_MODE_CLASS (mode
) != MODE_INT
)
5566 /* If this is an equality comparison of integers, we can try to exclusive-or
5567 (or subtract) the two operands and use a recursive call to try the
5568 comparison with zero. Don't do any of these cases if branches are
5571 if ((code
== EQ
|| code
== NE
) && op1
!= const0_rtx
)
5573 tem
= expand_binop (mode
, xor_optab
, op0
, op1
, subtarget
, 1,
5577 tem
= expand_binop (mode
, sub_optab
, op0
, op1
, subtarget
, 1,
5580 tem
= emit_store_flag (target
, code
, tem
, const0_rtx
,
5581 mode
, unsignedp
, normalizep
);
5585 delete_insns_since (last
);
5588 /* For integer comparisons, try the reverse comparison. However, for
5589 small X and if we'd have anyway to extend, implementing "X != 0"
5590 as "-(int)X >> 31" is still cheaper than inverting "(int)X == 0". */
5591 rcode
= reverse_condition (code
);
5592 if (can_compare_p (rcode
, mode
, ccp_store_flag
)
5593 && ! (optab_handler (cstore_optab
, mode
) == CODE_FOR_nothing
5595 && GET_MODE_SIZE (mode
) < UNITS_PER_WORD
5596 && op1
== const0_rtx
))
5598 int want_add
= ((STORE_FLAG_VALUE
== 1 && normalizep
== -1)
5599 || (STORE_FLAG_VALUE
== -1 && normalizep
== 1));
5601 /* Again, for the reverse comparison, use either an addition or a XOR. */
5603 && rtx_cost (GEN_INT (normalizep
), mode
, PLUS
, 1,
5604 optimize_insn_for_speed_p ()) == 0)
5606 tem
= emit_store_flag_1 (subtarget
, rcode
, op0
, op1
, mode
, 0,
5607 STORE_FLAG_VALUE
, target_mode
);
5609 tem
= expand_binop (target_mode
, add_optab
, tem
,
5610 gen_int_mode (normalizep
, target_mode
),
5611 target
, 0, OPTAB_WIDEN
);
5614 && rtx_cost (trueval
, mode
, XOR
, 1,
5615 optimize_insn_for_speed_p ()) == 0)
5617 tem
= emit_store_flag_1 (subtarget
, rcode
, op0
, op1
, mode
, 0,
5618 normalizep
, target_mode
);
5620 tem
= expand_binop (target_mode
, xor_optab
, tem
, trueval
, target
,
5621 INTVAL (trueval
) >= 0, OPTAB_WIDEN
);
5626 delete_insns_since (last
);
5629 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5630 the constant zero. Reject all other comparisons at this point. Only
5631 do LE and GT if branches are expensive since they are expensive on
5632 2-operand machines. */
5634 if (op1
!= const0_rtx
5635 || (code
!= EQ
&& code
!= NE
5636 && (BRANCH_COST (optimize_insn_for_speed_p (),
5637 false) <= 1 || (code
!= LE
&& code
!= GT
))))
5640 /* Try to put the result of the comparison in the sign bit. Assume we can't
5641 do the necessary operation below. */
5645 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5646 the sign bit set. */
5650 /* This is destructive, so SUBTARGET can't be OP0. */
5651 if (rtx_equal_p (subtarget
, op0
))
5654 tem
= expand_binop (mode
, sub_optab
, op0
, const1_rtx
, subtarget
, 0,
5657 tem
= expand_binop (mode
, ior_optab
, op0
, tem
, subtarget
, 0,
5661 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5662 number of bits in the mode of OP0, minus one. */
5666 if (rtx_equal_p (subtarget
, op0
))
5669 tem
= expand_shift (RSHIFT_EXPR
, mode
, op0
,
5670 GET_MODE_BITSIZE (mode
) - 1,
5672 tem
= expand_binop (mode
, sub_optab
, tem
, op0
, subtarget
, 0,
5676 if (code
== EQ
|| code
== NE
)
5678 /* For EQ or NE, one way to do the comparison is to apply an operation
5679 that converts the operand into a positive number if it is nonzero
5680 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5681 for NE we negate. This puts the result in the sign bit. Then we
5682 normalize with a shift, if needed.
5684 Two operations that can do the above actions are ABS and FFS, so try
5685 them. If that doesn't work, and MODE is smaller than a full word,
5686 we can use zero-extension to the wider mode (an unsigned conversion)
5687 as the operation. */
5689 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5690 that is compensated by the subsequent overflow when subtracting
5693 if (optab_handler (abs_optab
, mode
) != CODE_FOR_nothing
)
5694 tem
= expand_unop (mode
, abs_optab
, op0
, subtarget
, 1);
5695 else if (optab_handler (ffs_optab
, mode
) != CODE_FOR_nothing
)
5696 tem
= expand_unop (mode
, ffs_optab
, op0
, subtarget
, 1);
5697 else if (GET_MODE_SIZE (mode
) < UNITS_PER_WORD
)
5699 tem
= convert_modes (word_mode
, mode
, op0
, 1);
5706 tem
= expand_binop (mode
, sub_optab
, tem
, const1_rtx
, subtarget
,
5709 tem
= expand_unop (mode
, neg_optab
, tem
, subtarget
, 0);
5712 /* If we couldn't do it that way, for NE we can "or" the two's complement
5713 of the value with itself. For EQ, we take the one's complement of
5714 that "or", which is an extra insn, so we only handle EQ if branches
5719 || BRANCH_COST (optimize_insn_for_speed_p (),
5722 if (rtx_equal_p (subtarget
, op0
))
5725 tem
= expand_unop (mode
, neg_optab
, op0
, subtarget
, 0);
5726 tem
= expand_binop (mode
, ior_optab
, tem
, op0
, subtarget
, 0,
5729 if (tem
&& code
== EQ
)
5730 tem
= expand_unop (mode
, one_cmpl_optab
, tem
, subtarget
, 0);
5734 if (tem
&& normalizep
)
5735 tem
= expand_shift (RSHIFT_EXPR
, mode
, tem
,
5736 GET_MODE_BITSIZE (mode
) - 1,
5737 subtarget
, normalizep
== 1);
5743 else if (GET_MODE (tem
) != target_mode
)
5745 convert_move (target
, tem
, 0);
5748 else if (!subtarget
)
5750 emit_move_insn (target
, tem
);
5755 delete_insns_since (last
);
5760 /* Like emit_store_flag, but always succeeds. */
5763 emit_store_flag_force (rtx target
, enum rtx_code code
, rtx op0
, rtx op1
,
5764 machine_mode mode
, int unsignedp
, int normalizep
)
5767 rtx_code_label
*label
;
5768 rtx trueval
, falseval
;
5770 /* First see if emit_store_flag can do the job. */
5771 tem
= emit_store_flag (target
, code
, op0
, op1
, mode
, unsignedp
, normalizep
);
5776 target
= gen_reg_rtx (word_mode
);
5778 /* If this failed, we have to do this with set/compare/jump/set code.
5779 For foo != 0, if foo is in OP0, just replace it with 1 if nonzero. */
5780 trueval
= normalizep
? GEN_INT (normalizep
) : const1_rtx
;
5782 && GET_MODE_CLASS (mode
) == MODE_INT
5785 && op1
== const0_rtx
)
5787 label
= gen_label_rtx ();
5788 do_compare_rtx_and_jump (target
, const0_rtx
, EQ
, unsignedp
, mode
,
5789 NULL_RTX
, NULL
, label
, -1);
5790 emit_move_insn (target
, trueval
);
5796 || reg_mentioned_p (target
, op0
) || reg_mentioned_p (target
, op1
))
5797 target
= gen_reg_rtx (GET_MODE (target
));
5799 /* Jump in the right direction if the target cannot implement CODE
5800 but can jump on its reverse condition. */
5801 falseval
= const0_rtx
;
5802 if (! can_compare_p (code
, mode
, ccp_jump
)
5803 && (! FLOAT_MODE_P (mode
)
5804 || code
== ORDERED
|| code
== UNORDERED
5805 || (! HONOR_NANS (mode
) && (code
== LTGT
|| code
== UNEQ
))
5806 || (! HONOR_SNANS (mode
) && (code
== EQ
|| code
== NE
))))
5808 enum rtx_code rcode
;
5809 if (FLOAT_MODE_P (mode
))
5810 rcode
= reverse_condition_maybe_unordered (code
);
5812 rcode
= reverse_condition (code
);
5814 /* Canonicalize to UNORDERED for the libcall. */
5815 if (can_compare_p (rcode
, mode
, ccp_jump
)
5816 || (code
== ORDERED
&& ! can_compare_p (ORDERED
, mode
, ccp_jump
)))
5819 trueval
= const0_rtx
;
5824 emit_move_insn (target
, trueval
);
5825 label
= gen_label_rtx ();
5826 do_compare_rtx_and_jump (op0
, op1
, code
, unsignedp
, mode
, NULL_RTX
, NULL
,
5829 emit_move_insn (target
, falseval
);
5835 /* Perform possibly multi-word comparison and conditional jump to LABEL
5836 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE. This is
5837 now a thin wrapper around do_compare_rtx_and_jump. */
5840 do_cmp_and_jump (rtx arg1
, rtx arg2
, enum rtx_code op
, machine_mode mode
,
5841 rtx_code_label
*label
)
5843 int unsignedp
= (op
== LTU
|| op
== LEU
|| op
== GTU
|| op
== GEU
);
5844 do_compare_rtx_and_jump (arg1
, arg2
, op
, unsignedp
, mode
, NULL_RTX
,