1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987-2014 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"
26 #include "diagnostic-core.h"
29 #include "stor-layout.h"
32 #include "insn-config.h"
36 #include "langhooks.h"
41 struct target_expmed default_target_expmed
;
43 struct target_expmed
*this_target_expmed
= &default_target_expmed
;
46 static void store_fixed_bit_field (rtx
, unsigned HOST_WIDE_INT
,
47 unsigned HOST_WIDE_INT
,
48 unsigned HOST_WIDE_INT
,
49 unsigned HOST_WIDE_INT
,
51 static void store_fixed_bit_field_1 (rtx
, unsigned HOST_WIDE_INT
,
52 unsigned HOST_WIDE_INT
,
54 static void store_split_bit_field (rtx
, unsigned HOST_WIDE_INT
,
55 unsigned HOST_WIDE_INT
,
56 unsigned HOST_WIDE_INT
,
57 unsigned HOST_WIDE_INT
,
59 static rtx
extract_fixed_bit_field (enum machine_mode
, rtx
,
60 unsigned HOST_WIDE_INT
,
61 unsigned HOST_WIDE_INT
, rtx
, int);
62 static rtx
extract_fixed_bit_field_1 (enum machine_mode
, rtx
,
63 unsigned HOST_WIDE_INT
,
64 unsigned HOST_WIDE_INT
, rtx
, int);
65 static rtx
lshift_value (enum machine_mode
, unsigned HOST_WIDE_INT
, int);
66 static rtx
extract_split_bit_field (rtx
, unsigned HOST_WIDE_INT
,
67 unsigned HOST_WIDE_INT
, int);
68 static void do_cmp_and_jump (rtx
, rtx
, enum rtx_code
, enum machine_mode
, rtx_code_label
*);
69 static rtx
expand_smod_pow2 (enum machine_mode
, rtx
, HOST_WIDE_INT
);
70 static rtx
expand_sdiv_pow2 (enum machine_mode
, rtx
, HOST_WIDE_INT
);
72 /* Return a constant integer mask value of mode MODE with BITSIZE ones
73 followed by BITPOS zeros, or the complement of that if COMPLEMENT.
74 The mask is truncated if necessary to the width of mode MODE. The
75 mask is zero-extended if BITSIZE+BITPOS is too small for MODE. */
78 mask_rtx (enum machine_mode mode
, int bitpos
, int bitsize
, bool complement
)
80 return immed_wide_int_const
81 (wi::shifted_mask (bitpos
, bitsize
, complement
,
82 GET_MODE_PRECISION (mode
)), mode
);
85 /* Test whether a value is zero of a power of two. */
86 #define EXACT_POWER_OF_2_OR_ZERO_P(x) \
87 (((x) & ((x) - (unsigned HOST_WIDE_INT) 1)) == 0)
89 struct init_expmed_rtl
110 rtx pow2
[MAX_BITS_PER_WORD
];
111 rtx cint
[MAX_BITS_PER_WORD
];
115 init_expmed_one_conv (struct init_expmed_rtl
*all
, enum machine_mode to_mode
,
116 enum machine_mode from_mode
, bool speed
)
118 int to_size
, from_size
;
121 to_size
= GET_MODE_PRECISION (to_mode
);
122 from_size
= GET_MODE_PRECISION (from_mode
);
124 /* Most partial integers have a precision less than the "full"
125 integer it requires for storage. In case one doesn't, for
126 comparison purposes here, reduce the bit size by one in that
128 if (GET_MODE_CLASS (to_mode
) == MODE_PARTIAL_INT
129 && exact_log2 (to_size
) != -1)
131 if (GET_MODE_CLASS (from_mode
) == MODE_PARTIAL_INT
132 && exact_log2 (from_size
) != -1)
135 /* Assume cost of zero-extend and sign-extend is the same. */
136 which
= (to_size
< from_size
? all
->trunc
: all
->zext
);
138 PUT_MODE (all
->reg
, from_mode
);
139 set_convert_cost (to_mode
, from_mode
, speed
, set_src_cost (which
, speed
));
143 init_expmed_one_mode (struct init_expmed_rtl
*all
,
144 enum machine_mode mode
, int speed
)
146 int m
, n
, mode_bitsize
;
147 enum machine_mode mode_from
;
149 mode_bitsize
= GET_MODE_UNIT_BITSIZE (mode
);
151 PUT_MODE (all
->reg
, mode
);
152 PUT_MODE (all
->plus
, mode
);
153 PUT_MODE (all
->neg
, mode
);
154 PUT_MODE (all
->mult
, mode
);
155 PUT_MODE (all
->sdiv
, mode
);
156 PUT_MODE (all
->udiv
, mode
);
157 PUT_MODE (all
->sdiv_32
, mode
);
158 PUT_MODE (all
->smod_32
, mode
);
159 PUT_MODE (all
->wide_trunc
, mode
);
160 PUT_MODE (all
->shift
, mode
);
161 PUT_MODE (all
->shift_mult
, mode
);
162 PUT_MODE (all
->shift_add
, mode
);
163 PUT_MODE (all
->shift_sub0
, mode
);
164 PUT_MODE (all
->shift_sub1
, mode
);
165 PUT_MODE (all
->zext
, mode
);
166 PUT_MODE (all
->trunc
, mode
);
168 set_add_cost (speed
, mode
, set_src_cost (all
->plus
, speed
));
169 set_neg_cost (speed
, mode
, set_src_cost (all
->neg
, speed
));
170 set_mul_cost (speed
, mode
, set_src_cost (all
->mult
, speed
));
171 set_sdiv_cost (speed
, mode
, set_src_cost (all
->sdiv
, speed
));
172 set_udiv_cost (speed
, mode
, set_src_cost (all
->udiv
, speed
));
174 set_sdiv_pow2_cheap (speed
, mode
, (set_src_cost (all
->sdiv_32
, speed
)
175 <= 2 * add_cost (speed
, mode
)));
176 set_smod_pow2_cheap (speed
, mode
, (set_src_cost (all
->smod_32
, speed
)
177 <= 4 * add_cost (speed
, mode
)));
179 set_shift_cost (speed
, mode
, 0, 0);
181 int cost
= add_cost (speed
, mode
);
182 set_shiftadd_cost (speed
, mode
, 0, cost
);
183 set_shiftsub0_cost (speed
, mode
, 0, cost
);
184 set_shiftsub1_cost (speed
, mode
, 0, cost
);
187 n
= MIN (MAX_BITS_PER_WORD
, mode_bitsize
);
188 for (m
= 1; m
< n
; m
++)
190 XEXP (all
->shift
, 1) = all
->cint
[m
];
191 XEXP (all
->shift_mult
, 1) = all
->pow2
[m
];
193 set_shift_cost (speed
, mode
, m
, set_src_cost (all
->shift
, speed
));
194 set_shiftadd_cost (speed
, mode
, m
, set_src_cost (all
->shift_add
, speed
));
195 set_shiftsub0_cost (speed
, mode
, m
, set_src_cost (all
->shift_sub0
, speed
));
196 set_shiftsub1_cost (speed
, mode
, m
, set_src_cost (all
->shift_sub1
, speed
));
199 if (SCALAR_INT_MODE_P (mode
))
201 for (mode_from
= MIN_MODE_INT
; mode_from
<= MAX_MODE_INT
;
202 mode_from
= (enum machine_mode
)(mode_from
+ 1))
203 init_expmed_one_conv (all
, mode
, mode_from
, speed
);
205 if (GET_MODE_CLASS (mode
) == MODE_INT
)
207 enum machine_mode wider_mode
= GET_MODE_WIDER_MODE (mode
);
208 if (wider_mode
!= VOIDmode
)
210 PUT_MODE (all
->zext
, wider_mode
);
211 PUT_MODE (all
->wide_mult
, wider_mode
);
212 PUT_MODE (all
->wide_lshr
, wider_mode
);
213 XEXP (all
->wide_lshr
, 1) = GEN_INT (mode_bitsize
);
215 set_mul_widen_cost (speed
, wider_mode
,
216 set_src_cost (all
->wide_mult
, speed
));
217 set_mul_highpart_cost (speed
, mode
,
218 set_src_cost (all
->wide_trunc
, speed
));
226 struct init_expmed_rtl all
;
227 enum machine_mode mode
= QImode
;
230 memset (&all
, 0, sizeof all
);
231 for (m
= 1; m
< MAX_BITS_PER_WORD
; m
++)
233 all
.pow2
[m
] = GEN_INT ((HOST_WIDE_INT
) 1 << m
);
234 all
.cint
[m
] = GEN_INT (m
);
237 /* Avoid using hard regs in ways which may be unsupported. */
238 all
.reg
= gen_rtx_raw_REG (mode
, LAST_VIRTUAL_REGISTER
+ 1);
239 all
.plus
= gen_rtx_PLUS (mode
, all
.reg
, all
.reg
);
240 all
.neg
= gen_rtx_NEG (mode
, all
.reg
);
241 all
.mult
= gen_rtx_MULT (mode
, all
.reg
, all
.reg
);
242 all
.sdiv
= gen_rtx_DIV (mode
, all
.reg
, all
.reg
);
243 all
.udiv
= gen_rtx_UDIV (mode
, all
.reg
, all
.reg
);
244 all
.sdiv_32
= gen_rtx_DIV (mode
, all
.reg
, all
.pow2
[5]);
245 all
.smod_32
= gen_rtx_MOD (mode
, all
.reg
, all
.pow2
[5]);
246 all
.zext
= gen_rtx_ZERO_EXTEND (mode
, all
.reg
);
247 all
.wide_mult
= gen_rtx_MULT (mode
, all
.zext
, all
.zext
);
248 all
.wide_lshr
= gen_rtx_LSHIFTRT (mode
, all
.wide_mult
, all
.reg
);
249 all
.wide_trunc
= gen_rtx_TRUNCATE (mode
, all
.wide_lshr
);
250 all
.shift
= gen_rtx_ASHIFT (mode
, all
.reg
, all
.reg
);
251 all
.shift_mult
= gen_rtx_MULT (mode
, all
.reg
, all
.reg
);
252 all
.shift_add
= gen_rtx_PLUS (mode
, all
.shift_mult
, all
.reg
);
253 all
.shift_sub0
= gen_rtx_MINUS (mode
, all
.shift_mult
, all
.reg
);
254 all
.shift_sub1
= gen_rtx_MINUS (mode
, all
.reg
, all
.shift_mult
);
255 all
.trunc
= gen_rtx_TRUNCATE (mode
, all
.reg
);
257 for (speed
= 0; speed
< 2; speed
++)
259 crtl
->maybe_hot_insn_p
= speed
;
260 set_zero_cost (speed
, set_src_cost (const0_rtx
, speed
));
262 for (mode
= MIN_MODE_INT
; mode
<= MAX_MODE_INT
;
263 mode
= (enum machine_mode
)(mode
+ 1))
264 init_expmed_one_mode (&all
, mode
, speed
);
266 if (MIN_MODE_PARTIAL_INT
!= VOIDmode
)
267 for (mode
= MIN_MODE_PARTIAL_INT
; mode
<= MAX_MODE_PARTIAL_INT
;
268 mode
= (enum machine_mode
)(mode
+ 1))
269 init_expmed_one_mode (&all
, mode
, speed
);
271 if (MIN_MODE_VECTOR_INT
!= VOIDmode
)
272 for (mode
= MIN_MODE_VECTOR_INT
; mode
<= MAX_MODE_VECTOR_INT
;
273 mode
= (enum machine_mode
)(mode
+ 1))
274 init_expmed_one_mode (&all
, mode
, speed
);
277 if (alg_hash_used_p ())
279 struct alg_hash_entry
*p
= alg_hash_entry_ptr (0);
280 memset (p
, 0, sizeof (*p
) * NUM_ALG_HASH_ENTRIES
);
283 set_alg_hash_used_p (true);
284 default_rtl_profile ();
286 ggc_free (all
.trunc
);
287 ggc_free (all
.shift_sub1
);
288 ggc_free (all
.shift_sub0
);
289 ggc_free (all
.shift_add
);
290 ggc_free (all
.shift_mult
);
291 ggc_free (all
.shift
);
292 ggc_free (all
.wide_trunc
);
293 ggc_free (all
.wide_lshr
);
294 ggc_free (all
.wide_mult
);
296 ggc_free (all
.smod_32
);
297 ggc_free (all
.sdiv_32
);
306 /* Return an rtx representing minus the value of X.
307 MODE is the intended mode of the result,
308 useful if X is a CONST_INT. */
311 negate_rtx (enum machine_mode mode
, rtx x
)
313 rtx result
= simplify_unary_operation (NEG
, mode
, x
, mode
);
316 result
= expand_unop (mode
, neg_optab
, x
, NULL_RTX
, 0);
321 /* Adjust bitfield memory MEM so that it points to the first unit of mode
322 MODE that contains a bitfield of size BITSIZE at bit position BITNUM.
323 If MODE is BLKmode, return a reference to every byte in the bitfield.
324 Set *NEW_BITNUM to the bit position of the field within the new memory. */
327 narrow_bit_field_mem (rtx mem
, enum machine_mode mode
,
328 unsigned HOST_WIDE_INT bitsize
,
329 unsigned HOST_WIDE_INT bitnum
,
330 unsigned HOST_WIDE_INT
*new_bitnum
)
334 *new_bitnum
= bitnum
% BITS_PER_UNIT
;
335 HOST_WIDE_INT offset
= bitnum
/ BITS_PER_UNIT
;
336 HOST_WIDE_INT size
= ((*new_bitnum
+ bitsize
+ BITS_PER_UNIT
- 1)
338 return adjust_bitfield_address_size (mem
, mode
, offset
, size
);
342 unsigned int unit
= GET_MODE_BITSIZE (mode
);
343 *new_bitnum
= bitnum
% unit
;
344 HOST_WIDE_INT offset
= (bitnum
- *new_bitnum
) / BITS_PER_UNIT
;
345 return adjust_bitfield_address (mem
, mode
, offset
);
349 /* The caller wants to perform insertion or extraction PATTERN on a
350 bitfield of size BITSIZE at BITNUM bits into memory operand OP0.
351 BITREGION_START and BITREGION_END are as for store_bit_field
352 and FIELDMODE is the natural mode of the field.
354 Search for a mode that is compatible with the memory access
355 restrictions and (where applicable) with a register insertion or
356 extraction. Return the new memory on success, storing the adjusted
357 bit position in *NEW_BITNUM. Return null otherwise. */
360 adjust_bit_field_mem_for_reg (enum extraction_pattern pattern
,
361 rtx op0
, HOST_WIDE_INT bitsize
,
362 HOST_WIDE_INT bitnum
,
363 unsigned HOST_WIDE_INT bitregion_start
,
364 unsigned HOST_WIDE_INT bitregion_end
,
365 enum machine_mode fieldmode
,
366 unsigned HOST_WIDE_INT
*new_bitnum
)
368 bit_field_mode_iterator
iter (bitsize
, bitnum
, bitregion_start
,
369 bitregion_end
, MEM_ALIGN (op0
),
370 MEM_VOLATILE_P (op0
));
371 enum machine_mode best_mode
;
372 if (iter
.next_mode (&best_mode
))
374 /* We can use a memory in BEST_MODE. See whether this is true for
375 any wider modes. All other things being equal, we prefer to
376 use the widest mode possible because it tends to expose more
377 CSE opportunities. */
378 if (!iter
.prefer_smaller_modes ())
380 /* Limit the search to the mode required by the corresponding
381 register insertion or extraction instruction, if any. */
382 enum machine_mode limit_mode
= word_mode
;
383 extraction_insn insn
;
384 if (get_best_reg_extraction_insn (&insn
, pattern
,
385 GET_MODE_BITSIZE (best_mode
),
387 limit_mode
= insn
.field_mode
;
389 enum machine_mode wider_mode
;
390 while (iter
.next_mode (&wider_mode
)
391 && GET_MODE_SIZE (wider_mode
) <= GET_MODE_SIZE (limit_mode
))
392 best_mode
= wider_mode
;
394 return narrow_bit_field_mem (op0
, best_mode
, bitsize
, bitnum
,
400 /* Return true if a bitfield of size BITSIZE at bit number BITNUM within
401 a structure of mode STRUCT_MODE represents a lowpart subreg. The subreg
402 offset is then BITNUM / BITS_PER_UNIT. */
405 lowpart_bit_field_p (unsigned HOST_WIDE_INT bitnum
,
406 unsigned HOST_WIDE_INT bitsize
,
407 enum machine_mode struct_mode
)
409 if (BYTES_BIG_ENDIAN
)
410 return (bitnum
% BITS_PER_UNIT
== 0
411 && (bitnum
+ bitsize
== GET_MODE_BITSIZE (struct_mode
)
412 || (bitnum
+ bitsize
) % BITS_PER_WORD
== 0));
414 return bitnum
% BITS_PER_WORD
== 0;
417 /* Return true if -fstrict-volatile-bitfields applies to an access of OP0
418 containing BITSIZE bits starting at BITNUM, with field mode FIELDMODE.
419 Return false if the access would touch memory outside the range
420 BITREGION_START to BITREGION_END for conformance to the C++ memory
424 strict_volatile_bitfield_p (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
425 unsigned HOST_WIDE_INT bitnum
,
426 enum machine_mode fieldmode
,
427 unsigned HOST_WIDE_INT bitregion_start
,
428 unsigned HOST_WIDE_INT bitregion_end
)
430 unsigned HOST_WIDE_INT modesize
= GET_MODE_BITSIZE (fieldmode
);
432 /* -fstrict-volatile-bitfields must be enabled and we must have a
435 || !MEM_VOLATILE_P (op0
)
436 || flag_strict_volatile_bitfields
<= 0)
439 /* Non-integral modes likely only happen with packed structures.
441 if (!SCALAR_INT_MODE_P (fieldmode
))
444 /* The bit size must not be larger than the field mode, and
445 the field mode must not be larger than a word. */
446 if (bitsize
> modesize
|| modesize
> BITS_PER_WORD
)
449 /* Check for cases of unaligned fields that must be split. */
450 if (bitnum
% BITS_PER_UNIT
+ bitsize
> modesize
452 && bitnum
% GET_MODE_ALIGNMENT (fieldmode
) + bitsize
> modesize
))
455 /* Check for cases where the C++ memory model applies. */
456 if (bitregion_end
!= 0
457 && (bitnum
- bitnum
% modesize
< bitregion_start
458 || bitnum
- bitnum
% modesize
+ modesize
> bitregion_end
))
464 /* Return true if OP is a memory and if a bitfield of size BITSIZE at
465 bit number BITNUM can be treated as a simple value of mode MODE. */
468 simple_mem_bitfield_p (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
469 unsigned HOST_WIDE_INT bitnum
, enum machine_mode mode
)
472 && bitnum
% BITS_PER_UNIT
== 0
473 && bitsize
== GET_MODE_BITSIZE (mode
)
474 && (!SLOW_UNALIGNED_ACCESS (mode
, MEM_ALIGN (op0
))
475 || (bitnum
% GET_MODE_ALIGNMENT (mode
) == 0
476 && MEM_ALIGN (op0
) >= GET_MODE_ALIGNMENT (mode
))));
479 /* Try to use instruction INSV to store VALUE into a field of OP0.
480 BITSIZE and BITNUM are as for store_bit_field. */
483 store_bit_field_using_insv (const extraction_insn
*insv
, rtx op0
,
484 unsigned HOST_WIDE_INT bitsize
,
485 unsigned HOST_WIDE_INT bitnum
,
488 struct expand_operand ops
[4];
491 rtx_insn
*last
= get_last_insn ();
492 bool copy_back
= false;
494 enum machine_mode op_mode
= insv
->field_mode
;
495 unsigned int unit
= GET_MODE_BITSIZE (op_mode
);
496 if (bitsize
== 0 || bitsize
> unit
)
500 /* Get a reference to the first byte of the field. */
501 xop0
= narrow_bit_field_mem (xop0
, insv
->struct_mode
, bitsize
, bitnum
,
505 /* Convert from counting within OP0 to counting in OP_MODE. */
506 if (BYTES_BIG_ENDIAN
)
507 bitnum
+= unit
- GET_MODE_BITSIZE (GET_MODE (op0
));
509 /* If xop0 is a register, we need it in OP_MODE
510 to make it acceptable to the format of insv. */
511 if (GET_CODE (xop0
) == SUBREG
)
512 /* We can't just change the mode, because this might clobber op0,
513 and we will need the original value of op0 if insv fails. */
514 xop0
= gen_rtx_SUBREG (op_mode
, SUBREG_REG (xop0
), SUBREG_BYTE (xop0
));
515 if (REG_P (xop0
) && GET_MODE (xop0
) != op_mode
)
516 xop0
= gen_lowpart_SUBREG (op_mode
, xop0
);
519 /* If the destination is a paradoxical subreg such that we need a
520 truncate to the inner mode, perform the insertion on a temporary and
521 truncate the result to the original destination. Note that we can't
522 just truncate the paradoxical subreg as (truncate:N (subreg:W (reg:N
523 X) 0)) is (reg:N X). */
524 if (GET_CODE (xop0
) == SUBREG
525 && REG_P (SUBREG_REG (xop0
))
526 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (SUBREG_REG (xop0
)),
529 rtx tem
= gen_reg_rtx (op_mode
);
530 emit_move_insn (tem
, xop0
);
535 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
536 "backwards" from the size of the unit we are inserting into.
537 Otherwise, we count bits from the most significant on a
538 BYTES/BITS_BIG_ENDIAN machine. */
540 if (BITS_BIG_ENDIAN
!= BYTES_BIG_ENDIAN
)
541 bitnum
= unit
- bitsize
- bitnum
;
543 /* Convert VALUE to op_mode (which insv insn wants) in VALUE1. */
545 if (GET_MODE (value
) != op_mode
)
547 if (GET_MODE_BITSIZE (GET_MODE (value
)) >= bitsize
)
549 /* Optimization: Don't bother really extending VALUE
550 if it has all the bits we will actually use. However,
551 if we must narrow it, be sure we do it correctly. */
553 if (GET_MODE_SIZE (GET_MODE (value
)) < GET_MODE_SIZE (op_mode
))
557 tmp
= simplify_subreg (op_mode
, value1
, GET_MODE (value
), 0);
559 tmp
= simplify_gen_subreg (op_mode
,
560 force_reg (GET_MODE (value
),
562 GET_MODE (value
), 0);
566 value1
= gen_lowpart (op_mode
, value1
);
568 else if (CONST_INT_P (value
))
569 value1
= gen_int_mode (INTVAL (value
), op_mode
);
571 /* Parse phase is supposed to make VALUE's data type
572 match that of the component reference, which is a type
573 at least as wide as the field; so VALUE should have
574 a mode that corresponds to that type. */
575 gcc_assert (CONSTANT_P (value
));
578 create_fixed_operand (&ops
[0], xop0
);
579 create_integer_operand (&ops
[1], bitsize
);
580 create_integer_operand (&ops
[2], bitnum
);
581 create_input_operand (&ops
[3], value1
, op_mode
);
582 if (maybe_expand_insn (insv
->icode
, 4, ops
))
585 convert_move (op0
, xop0
, true);
588 delete_insns_since (last
);
592 /* A subroutine of store_bit_field, with the same arguments. Return true
593 if the operation could be implemented.
595 If FALLBACK_P is true, fall back to store_fixed_bit_field if we have
596 no other way of implementing the operation. If FALLBACK_P is false,
597 return false instead. */
600 store_bit_field_1 (rtx str_rtx
, unsigned HOST_WIDE_INT bitsize
,
601 unsigned HOST_WIDE_INT bitnum
,
602 unsigned HOST_WIDE_INT bitregion_start
,
603 unsigned HOST_WIDE_INT bitregion_end
,
604 enum machine_mode fieldmode
,
605 rtx value
, bool fallback_p
)
610 while (GET_CODE (op0
) == SUBREG
)
612 /* The following line once was done only if WORDS_BIG_ENDIAN,
613 but I think that is a mistake. WORDS_BIG_ENDIAN is
614 meaningful at a much higher level; when structures are copied
615 between memory and regs, the higher-numbered regs
616 always get higher addresses. */
617 int inner_mode_size
= GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0
)));
618 int outer_mode_size
= GET_MODE_SIZE (GET_MODE (op0
));
621 /* Paradoxical subregs need special handling on big endian machines. */
622 if (SUBREG_BYTE (op0
) == 0 && inner_mode_size
< outer_mode_size
)
624 int difference
= inner_mode_size
- outer_mode_size
;
626 if (WORDS_BIG_ENDIAN
)
627 byte_offset
+= (difference
/ UNITS_PER_WORD
) * UNITS_PER_WORD
;
628 if (BYTES_BIG_ENDIAN
)
629 byte_offset
+= difference
% UNITS_PER_WORD
;
632 byte_offset
= SUBREG_BYTE (op0
);
634 bitnum
+= byte_offset
* BITS_PER_UNIT
;
635 op0
= SUBREG_REG (op0
);
638 /* No action is needed if the target is a register and if the field
639 lies completely outside that register. This can occur if the source
640 code contains an out-of-bounds access to a small array. */
641 if (REG_P (op0
) && bitnum
>= GET_MODE_BITSIZE (GET_MODE (op0
)))
644 /* Use vec_set patterns for inserting parts of vectors whenever
646 if (VECTOR_MODE_P (GET_MODE (op0
))
648 && optab_handler (vec_set_optab
, GET_MODE (op0
)) != CODE_FOR_nothing
649 && fieldmode
== GET_MODE_INNER (GET_MODE (op0
))
650 && bitsize
== GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0
)))
651 && !(bitnum
% GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0
)))))
653 struct expand_operand ops
[3];
654 enum machine_mode outermode
= GET_MODE (op0
);
655 enum machine_mode innermode
= GET_MODE_INNER (outermode
);
656 enum insn_code icode
= optab_handler (vec_set_optab
, outermode
);
657 int pos
= bitnum
/ GET_MODE_BITSIZE (innermode
);
659 create_fixed_operand (&ops
[0], op0
);
660 create_input_operand (&ops
[1], value
, innermode
);
661 create_integer_operand (&ops
[2], pos
);
662 if (maybe_expand_insn (icode
, 3, ops
))
666 /* If the target is a register, overwriting the entire object, or storing
667 a full-word or multi-word field can be done with just a SUBREG. */
669 && bitsize
== GET_MODE_BITSIZE (fieldmode
)
670 && ((bitsize
== GET_MODE_BITSIZE (GET_MODE (op0
)) && bitnum
== 0)
671 || (bitsize
% BITS_PER_WORD
== 0 && bitnum
% BITS_PER_WORD
== 0)))
673 /* Use the subreg machinery either to narrow OP0 to the required
674 words or to cope with mode punning between equal-sized modes.
675 In the latter case, use subreg on the rhs side, not lhs. */
678 if (bitsize
== GET_MODE_BITSIZE (GET_MODE (op0
)))
680 sub
= simplify_gen_subreg (GET_MODE (op0
), value
, fieldmode
, 0);
683 emit_move_insn (op0
, sub
);
689 sub
= simplify_gen_subreg (fieldmode
, op0
, GET_MODE (op0
),
690 bitnum
/ BITS_PER_UNIT
);
693 emit_move_insn (sub
, value
);
699 /* If the target is memory, storing any naturally aligned field can be
700 done with a simple store. For targets that support fast unaligned
701 memory, any naturally sized, unit aligned field can be done directly. */
702 if (simple_mem_bitfield_p (op0
, bitsize
, bitnum
, fieldmode
))
704 op0
= adjust_bitfield_address (op0
, fieldmode
, bitnum
/ BITS_PER_UNIT
);
705 emit_move_insn (op0
, value
);
709 /* Make sure we are playing with integral modes. Pun with subregs
710 if we aren't. This must come after the entire register case above,
711 since that case is valid for any mode. The following cases are only
712 valid for integral modes. */
714 enum machine_mode imode
= int_mode_for_mode (GET_MODE (op0
));
715 if (imode
!= GET_MODE (op0
))
718 op0
= adjust_bitfield_address_size (op0
, imode
, 0, MEM_SIZE (op0
));
721 gcc_assert (imode
!= BLKmode
);
722 op0
= gen_lowpart (imode
, op0
);
727 /* Storing an lsb-aligned field in a register
728 can be done with a movstrict instruction. */
731 && lowpart_bit_field_p (bitnum
, bitsize
, GET_MODE (op0
))
732 && bitsize
== GET_MODE_BITSIZE (fieldmode
)
733 && optab_handler (movstrict_optab
, fieldmode
) != CODE_FOR_nothing
)
735 struct expand_operand ops
[2];
736 enum insn_code icode
= optab_handler (movstrict_optab
, fieldmode
);
738 unsigned HOST_WIDE_INT subreg_off
;
740 if (GET_CODE (arg0
) == SUBREG
)
742 /* Else we've got some float mode source being extracted into
743 a different float mode destination -- this combination of
744 subregs results in Severe Tire Damage. */
745 gcc_assert (GET_MODE (SUBREG_REG (arg0
)) == fieldmode
746 || GET_MODE_CLASS (fieldmode
) == MODE_INT
747 || GET_MODE_CLASS (fieldmode
) == MODE_PARTIAL_INT
);
748 arg0
= SUBREG_REG (arg0
);
751 subreg_off
= bitnum
/ BITS_PER_UNIT
;
752 if (validate_subreg (fieldmode
, GET_MODE (arg0
), arg0
, subreg_off
))
754 arg0
= gen_rtx_SUBREG (fieldmode
, arg0
, subreg_off
);
756 create_fixed_operand (&ops
[0], arg0
);
757 /* Shrink the source operand to FIELDMODE. */
758 create_convert_operand_to (&ops
[1], value
, fieldmode
, false);
759 if (maybe_expand_insn (icode
, 2, ops
))
764 /* Handle fields bigger than a word. */
766 if (bitsize
> BITS_PER_WORD
)
768 /* Here we transfer the words of the field
769 in the order least significant first.
770 This is because the most significant word is the one which may
772 However, only do that if the value is not BLKmode. */
774 unsigned int backwards
= WORDS_BIG_ENDIAN
&& fieldmode
!= BLKmode
;
775 unsigned int nwords
= (bitsize
+ (BITS_PER_WORD
- 1)) / BITS_PER_WORD
;
779 /* This is the mode we must force value to, so that there will be enough
780 subwords to extract. Note that fieldmode will often (always?) be
781 VOIDmode, because that is what store_field uses to indicate that this
782 is a bit field, but passing VOIDmode to operand_subword_force
784 fieldmode
= GET_MODE (value
);
785 if (fieldmode
== VOIDmode
)
786 fieldmode
= smallest_mode_for_size (nwords
* BITS_PER_WORD
, MODE_INT
);
788 last
= get_last_insn ();
789 for (i
= 0; i
< nwords
; i
++)
791 /* If I is 0, use the low-order word in both field and target;
792 if I is 1, use the next to lowest word; and so on. */
793 unsigned int wordnum
= (backwards
794 ? GET_MODE_SIZE (fieldmode
) / UNITS_PER_WORD
797 unsigned int bit_offset
= (backwards
798 ? MAX ((int) bitsize
- ((int) i
+ 1)
801 : (int) i
* BITS_PER_WORD
);
802 rtx value_word
= operand_subword_force (value
, wordnum
, fieldmode
);
803 unsigned HOST_WIDE_INT new_bitsize
=
804 MIN (BITS_PER_WORD
, bitsize
- i
* BITS_PER_WORD
);
806 /* If the remaining chunk doesn't have full wordsize we have
807 to make sure that for big endian machines the higher order
809 if (new_bitsize
< BITS_PER_WORD
&& BYTES_BIG_ENDIAN
&& !backwards
)
810 value_word
= simplify_expand_binop (word_mode
, lshr_optab
,
812 GEN_INT (BITS_PER_WORD
817 if (!store_bit_field_1 (op0
, new_bitsize
,
819 bitregion_start
, bitregion_end
,
821 value_word
, fallback_p
))
823 delete_insns_since (last
);
830 /* If VALUE has a floating-point or complex mode, access it as an
831 integer of the corresponding size. This can occur on a machine
832 with 64 bit registers that uses SFmode for float. It can also
833 occur for unaligned float or complex fields. */
835 if (GET_MODE (value
) != VOIDmode
836 && GET_MODE_CLASS (GET_MODE (value
)) != MODE_INT
837 && GET_MODE_CLASS (GET_MODE (value
)) != MODE_PARTIAL_INT
)
839 value
= gen_reg_rtx (int_mode_for_mode (GET_MODE (value
)));
840 emit_move_insn (gen_lowpart (GET_MODE (orig_value
), value
), orig_value
);
843 /* If OP0 is a multi-word register, narrow it to the affected word.
844 If the region spans two words, defer to store_split_bit_field. */
845 if (!MEM_P (op0
) && GET_MODE_SIZE (GET_MODE (op0
)) > UNITS_PER_WORD
)
847 op0
= simplify_gen_subreg (word_mode
, op0
, GET_MODE (op0
),
848 bitnum
/ BITS_PER_WORD
* UNITS_PER_WORD
);
850 bitnum
%= BITS_PER_WORD
;
851 if (bitnum
+ bitsize
> BITS_PER_WORD
)
856 store_split_bit_field (op0
, bitsize
, bitnum
, bitregion_start
,
857 bitregion_end
, value
);
862 /* From here on we can assume that the field to be stored in fits
863 within a word. If the destination is a register, it too fits
866 extraction_insn insv
;
868 && get_best_reg_extraction_insn (&insv
, EP_insv
,
869 GET_MODE_BITSIZE (GET_MODE (op0
)),
871 && store_bit_field_using_insv (&insv
, op0
, bitsize
, bitnum
, value
))
874 /* If OP0 is a memory, try copying it to a register and seeing if a
875 cheap register alternative is available. */
878 if (get_best_mem_extraction_insn (&insv
, EP_insv
, bitsize
, bitnum
,
880 && store_bit_field_using_insv (&insv
, op0
, bitsize
, bitnum
, value
))
883 rtx_insn
*last
= get_last_insn ();
885 /* Try loading part of OP0 into a register, inserting the bitfield
886 into that, and then copying the result back to OP0. */
887 unsigned HOST_WIDE_INT bitpos
;
888 rtx xop0
= adjust_bit_field_mem_for_reg (EP_insv
, op0
, bitsize
, bitnum
,
889 bitregion_start
, bitregion_end
,
893 rtx tempreg
= copy_to_reg (xop0
);
894 if (store_bit_field_1 (tempreg
, bitsize
, bitpos
,
895 bitregion_start
, bitregion_end
,
896 fieldmode
, orig_value
, false))
898 emit_move_insn (xop0
, tempreg
);
901 delete_insns_since (last
);
908 store_fixed_bit_field (op0
, bitsize
, bitnum
, bitregion_start
,
909 bitregion_end
, value
);
913 /* Generate code to store value from rtx VALUE
914 into a bit-field within structure STR_RTX
915 containing BITSIZE bits starting at bit BITNUM.
917 BITREGION_START is bitpos of the first bitfield in this region.
918 BITREGION_END is the bitpos of the ending bitfield in this region.
919 These two fields are 0, if the C++ memory model does not apply,
920 or we are not interested in keeping track of bitfield regions.
922 FIELDMODE is the machine-mode of the FIELD_DECL node for this field. */
925 store_bit_field (rtx str_rtx
, unsigned HOST_WIDE_INT bitsize
,
926 unsigned HOST_WIDE_INT bitnum
,
927 unsigned HOST_WIDE_INT bitregion_start
,
928 unsigned HOST_WIDE_INT bitregion_end
,
929 enum machine_mode fieldmode
,
932 /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
933 if (strict_volatile_bitfield_p (str_rtx
, bitsize
, bitnum
, fieldmode
,
934 bitregion_start
, bitregion_end
))
936 /* Storing any naturally aligned field can be done with a simple
937 store. For targets that support fast unaligned memory, any
938 naturally sized, unit aligned field can be done directly. */
939 if (simple_mem_bitfield_p (str_rtx
, bitsize
, bitnum
, fieldmode
))
941 str_rtx
= adjust_bitfield_address (str_rtx
, fieldmode
,
942 bitnum
/ BITS_PER_UNIT
);
943 emit_move_insn (str_rtx
, value
);
947 str_rtx
= narrow_bit_field_mem (str_rtx
, fieldmode
, bitsize
, bitnum
,
949 /* Explicitly override the C/C++ memory model; ignore the
950 bit range so that we can do the access in the mode mandated
951 by -fstrict-volatile-bitfields instead. */
952 store_fixed_bit_field_1 (str_rtx
, bitsize
, bitnum
, value
);
958 /* Under the C++0x memory model, we must not touch bits outside the
959 bit region. Adjust the address to start at the beginning of the
961 if (MEM_P (str_rtx
) && bitregion_start
> 0)
963 enum machine_mode bestmode
;
964 HOST_WIDE_INT offset
, size
;
966 gcc_assert ((bitregion_start
% BITS_PER_UNIT
) == 0);
968 offset
= bitregion_start
/ BITS_PER_UNIT
;
969 bitnum
-= bitregion_start
;
970 size
= (bitnum
+ bitsize
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
971 bitregion_end
-= bitregion_start
;
973 bestmode
= get_best_mode (bitsize
, bitnum
,
974 bitregion_start
, bitregion_end
,
975 MEM_ALIGN (str_rtx
), VOIDmode
,
976 MEM_VOLATILE_P (str_rtx
));
977 str_rtx
= adjust_bitfield_address_size (str_rtx
, bestmode
, offset
, size
);
980 if (!store_bit_field_1 (str_rtx
, bitsize
, bitnum
,
981 bitregion_start
, bitregion_end
,
982 fieldmode
, value
, true))
986 /* Use shifts and boolean operations to store VALUE into a bit field of
987 width BITSIZE in OP0, starting at bit BITNUM. */
990 store_fixed_bit_field (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
991 unsigned HOST_WIDE_INT bitnum
,
992 unsigned HOST_WIDE_INT bitregion_start
,
993 unsigned HOST_WIDE_INT bitregion_end
,
996 /* There is a case not handled here:
997 a structure with a known alignment of just a halfword
998 and a field split across two aligned halfwords within the structure.
999 Or likewise a structure with a known alignment of just a byte
1000 and a field split across two bytes.
1001 Such cases are not supposed to be able to occur. */
1005 enum machine_mode mode
= GET_MODE (op0
);
1006 if (GET_MODE_BITSIZE (mode
) == 0
1007 || GET_MODE_BITSIZE (mode
) > GET_MODE_BITSIZE (word_mode
))
1009 mode
= get_best_mode (bitsize
, bitnum
, bitregion_start
, bitregion_end
,
1010 MEM_ALIGN (op0
), mode
, MEM_VOLATILE_P (op0
));
1012 if (mode
== VOIDmode
)
1014 /* The only way this should occur is if the field spans word
1016 store_split_bit_field (op0
, bitsize
, bitnum
, bitregion_start
,
1017 bitregion_end
, value
);
1021 op0
= narrow_bit_field_mem (op0
, mode
, bitsize
, bitnum
, &bitnum
);
1024 store_fixed_bit_field_1 (op0
, bitsize
, bitnum
, value
);
1027 /* Helper function for store_fixed_bit_field, stores
1028 the bit field always using the MODE of OP0. */
1031 store_fixed_bit_field_1 (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
1032 unsigned HOST_WIDE_INT bitnum
,
1035 enum machine_mode mode
;
1040 mode
= GET_MODE (op0
);
1041 gcc_assert (SCALAR_INT_MODE_P (mode
));
1043 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
1044 for invalid input, such as f5 from gcc.dg/pr48335-2.c. */
1046 if (BYTES_BIG_ENDIAN
)
1047 /* BITNUM is the distance between our msb
1048 and that of the containing datum.
1049 Convert it to the distance from the lsb. */
1050 bitnum
= GET_MODE_BITSIZE (mode
) - bitsize
- bitnum
;
1052 /* Now BITNUM is always the distance between our lsb
1055 /* Shift VALUE left by BITNUM bits. If VALUE is not constant,
1056 we must first convert its mode to MODE. */
1058 if (CONST_INT_P (value
))
1060 unsigned HOST_WIDE_INT v
= UINTVAL (value
);
1062 if (bitsize
< HOST_BITS_PER_WIDE_INT
)
1063 v
&= ((unsigned HOST_WIDE_INT
) 1 << bitsize
) - 1;
1067 else if ((bitsize
< HOST_BITS_PER_WIDE_INT
1068 && v
== ((unsigned HOST_WIDE_INT
) 1 << bitsize
) - 1)
1069 || (bitsize
== HOST_BITS_PER_WIDE_INT
1070 && v
== (unsigned HOST_WIDE_INT
) -1))
1073 value
= lshift_value (mode
, v
, bitnum
);
1077 int must_and
= (GET_MODE_BITSIZE (GET_MODE (value
)) != bitsize
1078 && bitnum
+ bitsize
!= GET_MODE_BITSIZE (mode
));
1080 if (GET_MODE (value
) != mode
)
1081 value
= convert_to_mode (mode
, value
, 1);
1084 value
= expand_binop (mode
, and_optab
, value
,
1085 mask_rtx (mode
, 0, bitsize
, 0),
1086 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
1088 value
= expand_shift (LSHIFT_EXPR
, mode
, value
,
1089 bitnum
, NULL_RTX
, 1);
1092 /* Now clear the chosen bits in OP0,
1093 except that if VALUE is -1 we need not bother. */
1094 /* We keep the intermediates in registers to allow CSE to combine
1095 consecutive bitfield assignments. */
1097 temp
= force_reg (mode
, op0
);
1101 temp
= expand_binop (mode
, and_optab
, temp
,
1102 mask_rtx (mode
, bitnum
, bitsize
, 1),
1103 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
1104 temp
= force_reg (mode
, temp
);
1107 /* Now logical-or VALUE into OP0, unless it is zero. */
1111 temp
= expand_binop (mode
, ior_optab
, temp
, value
,
1112 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
1113 temp
= force_reg (mode
, temp
);
1118 op0
= copy_rtx (op0
);
1119 emit_move_insn (op0
, temp
);
1123 /* Store a bit field that is split across multiple accessible memory objects.
1125 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
1126 BITSIZE is the field width; BITPOS the position of its first bit
1128 VALUE is the value to store.
1130 This does not yet handle fields wider than BITS_PER_WORD. */
1133 store_split_bit_field (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
1134 unsigned HOST_WIDE_INT bitpos
,
1135 unsigned HOST_WIDE_INT bitregion_start
,
1136 unsigned HOST_WIDE_INT bitregion_end
,
1140 unsigned int bitsdone
= 0;
1142 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1144 if (REG_P (op0
) || GET_CODE (op0
) == SUBREG
)
1145 unit
= BITS_PER_WORD
;
1147 unit
= MIN (MEM_ALIGN (op0
), BITS_PER_WORD
);
1149 /* If OP0 is a memory with a mode, then UNIT must not be larger than
1150 OP0's mode as well. Otherwise, store_fixed_bit_field will call us
1151 again, and we will mutually recurse forever. */
1152 if (MEM_P (op0
) && GET_MODE_BITSIZE (GET_MODE (op0
)) > 0)
1153 unit
= MIN (unit
, GET_MODE_BITSIZE (GET_MODE (op0
)));
1155 /* If VALUE is a constant other than a CONST_INT, get it into a register in
1156 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
1157 that VALUE might be a floating-point constant. */
1158 if (CONSTANT_P (value
) && !CONST_INT_P (value
))
1160 rtx word
= gen_lowpart_common (word_mode
, value
);
1162 if (word
&& (value
!= word
))
1165 value
= gen_lowpart_common (word_mode
,
1166 force_reg (GET_MODE (value
) != VOIDmode
1168 : word_mode
, value
));
1171 while (bitsdone
< bitsize
)
1173 unsigned HOST_WIDE_INT thissize
;
1175 unsigned HOST_WIDE_INT thispos
;
1176 unsigned HOST_WIDE_INT offset
;
1178 offset
= (bitpos
+ bitsdone
) / unit
;
1179 thispos
= (bitpos
+ bitsdone
) % unit
;
1181 /* When region of bytes we can touch is restricted, decrease
1182 UNIT close to the end of the region as needed. If op0 is a REG
1183 or SUBREG of REG, don't do this, as there can't be data races
1184 on a register and we can expand shorter code in some cases. */
1186 && unit
> BITS_PER_UNIT
1187 && bitpos
+ bitsdone
- thispos
+ unit
> bitregion_end
+ 1
1189 && (GET_CODE (op0
) != SUBREG
|| !REG_P (SUBREG_REG (op0
))))
1195 /* THISSIZE must not overrun a word boundary. Otherwise,
1196 store_fixed_bit_field will call us again, and we will mutually
1198 thissize
= MIN (bitsize
- bitsdone
, BITS_PER_WORD
);
1199 thissize
= MIN (thissize
, unit
- thispos
);
1201 if (BYTES_BIG_ENDIAN
)
1203 /* Fetch successively less significant portions. */
1204 if (CONST_INT_P (value
))
1205 part
= GEN_INT (((unsigned HOST_WIDE_INT
) (INTVAL (value
))
1206 >> (bitsize
- bitsdone
- thissize
))
1207 & (((HOST_WIDE_INT
) 1 << thissize
) - 1));
1210 int total_bits
= GET_MODE_BITSIZE (GET_MODE (value
));
1211 /* The args are chosen so that the last part includes the
1212 lsb. Give extract_bit_field the value it needs (with
1213 endianness compensation) to fetch the piece we want. */
1214 part
= extract_fixed_bit_field (word_mode
, value
, thissize
,
1215 total_bits
- bitsize
+ bitsdone
,
1221 /* Fetch successively more significant portions. */
1222 if (CONST_INT_P (value
))
1223 part
= GEN_INT (((unsigned HOST_WIDE_INT
) (INTVAL (value
))
1225 & (((HOST_WIDE_INT
) 1 << thissize
) - 1));
1227 part
= extract_fixed_bit_field (word_mode
, value
, thissize
,
1228 bitsdone
, NULL_RTX
, 1);
1231 /* If OP0 is a register, then handle OFFSET here.
1233 When handling multiword bitfields, extract_bit_field may pass
1234 down a word_mode SUBREG of a larger REG for a bitfield that actually
1235 crosses a word boundary. Thus, for a SUBREG, we must find
1236 the current word starting from the base register. */
1237 if (GET_CODE (op0
) == SUBREG
)
1239 int word_offset
= (SUBREG_BYTE (op0
) / UNITS_PER_WORD
)
1240 + (offset
* unit
/ BITS_PER_WORD
);
1241 enum machine_mode sub_mode
= GET_MODE (SUBREG_REG (op0
));
1242 if (sub_mode
!= BLKmode
&& GET_MODE_SIZE (sub_mode
) < UNITS_PER_WORD
)
1243 word
= word_offset
? const0_rtx
: op0
;
1245 word
= operand_subword_force (SUBREG_REG (op0
), word_offset
,
1246 GET_MODE (SUBREG_REG (op0
)));
1247 offset
&= BITS_PER_WORD
/ unit
- 1;
1249 else if (REG_P (op0
))
1251 enum machine_mode op0_mode
= GET_MODE (op0
);
1252 if (op0_mode
!= BLKmode
&& GET_MODE_SIZE (op0_mode
) < UNITS_PER_WORD
)
1253 word
= offset
? const0_rtx
: op0
;
1255 word
= operand_subword_force (op0
, offset
* unit
/ BITS_PER_WORD
,
1257 offset
&= BITS_PER_WORD
/ unit
- 1;
1262 /* OFFSET is in UNITs, and UNIT is in bits. If WORD is const0_rtx,
1263 it is just an out-of-bounds access. Ignore it. */
1264 if (word
!= const0_rtx
)
1265 store_fixed_bit_field (word
, thissize
, offset
* unit
+ thispos
,
1266 bitregion_start
, bitregion_end
, part
);
1267 bitsdone
+= thissize
;
1271 /* A subroutine of extract_bit_field_1 that converts return value X
1272 to either MODE or TMODE. MODE, TMODE and UNSIGNEDP are arguments
1273 to extract_bit_field. */
1276 convert_extracted_bit_field (rtx x
, enum machine_mode mode
,
1277 enum machine_mode tmode
, bool unsignedp
)
1279 if (GET_MODE (x
) == tmode
|| GET_MODE (x
) == mode
)
1282 /* If the x mode is not a scalar integral, first convert to the
1283 integer mode of that size and then access it as a floating-point
1284 value via a SUBREG. */
1285 if (!SCALAR_INT_MODE_P (tmode
))
1287 enum machine_mode smode
;
1289 smode
= mode_for_size (GET_MODE_BITSIZE (tmode
), MODE_INT
, 0);
1290 x
= convert_to_mode (smode
, x
, unsignedp
);
1291 x
= force_reg (smode
, x
);
1292 return gen_lowpart (tmode
, x
);
1295 return convert_to_mode (tmode
, x
, unsignedp
);
1298 /* Try to use an ext(z)v pattern to extract a field from OP0.
1299 Return the extracted value on success, otherwise return null.
1300 EXT_MODE is the mode of the extraction and the other arguments
1301 are as for extract_bit_field. */
1304 extract_bit_field_using_extv (const extraction_insn
*extv
, rtx op0
,
1305 unsigned HOST_WIDE_INT bitsize
,
1306 unsigned HOST_WIDE_INT bitnum
,
1307 int unsignedp
, rtx target
,
1308 enum machine_mode mode
, enum machine_mode tmode
)
1310 struct expand_operand ops
[4];
1311 rtx spec_target
= target
;
1312 rtx spec_target_subreg
= 0;
1313 enum machine_mode ext_mode
= extv
->field_mode
;
1314 unsigned unit
= GET_MODE_BITSIZE (ext_mode
);
1316 if (bitsize
== 0 || unit
< bitsize
)
1320 /* Get a reference to the first byte of the field. */
1321 op0
= narrow_bit_field_mem (op0
, extv
->struct_mode
, bitsize
, bitnum
,
1325 /* Convert from counting within OP0 to counting in EXT_MODE. */
1326 if (BYTES_BIG_ENDIAN
)
1327 bitnum
+= unit
- GET_MODE_BITSIZE (GET_MODE (op0
));
1329 /* If op0 is a register, we need it in EXT_MODE to make it
1330 acceptable to the format of ext(z)v. */
1331 if (GET_CODE (op0
) == SUBREG
&& GET_MODE (op0
) != ext_mode
)
1333 if (REG_P (op0
) && GET_MODE (op0
) != ext_mode
)
1334 op0
= gen_lowpart_SUBREG (ext_mode
, op0
);
1337 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
1338 "backwards" from the size of the unit we are extracting from.
1339 Otherwise, we count bits from the most significant on a
1340 BYTES/BITS_BIG_ENDIAN machine. */
1342 if (BITS_BIG_ENDIAN
!= BYTES_BIG_ENDIAN
)
1343 bitnum
= unit
- bitsize
- bitnum
;
1346 target
= spec_target
= gen_reg_rtx (tmode
);
1348 if (GET_MODE (target
) != ext_mode
)
1350 /* Don't use LHS paradoxical subreg if explicit truncation is needed
1351 between the mode of the extraction (word_mode) and the target
1352 mode. Instead, create a temporary and use convert_move to set
1355 && TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (target
), ext_mode
))
1357 target
= gen_lowpart (ext_mode
, target
);
1358 if (GET_MODE_PRECISION (ext_mode
)
1359 > GET_MODE_PRECISION (GET_MODE (spec_target
)))
1360 spec_target_subreg
= target
;
1363 target
= gen_reg_rtx (ext_mode
);
1366 create_output_operand (&ops
[0], target
, ext_mode
);
1367 create_fixed_operand (&ops
[1], op0
);
1368 create_integer_operand (&ops
[2], bitsize
);
1369 create_integer_operand (&ops
[3], bitnum
);
1370 if (maybe_expand_insn (extv
->icode
, 4, ops
))
1372 target
= ops
[0].value
;
1373 if (target
== spec_target
)
1375 if (target
== spec_target_subreg
)
1377 return convert_extracted_bit_field (target
, mode
, tmode
, unsignedp
);
1382 /* A subroutine of extract_bit_field, with the same arguments.
1383 If FALLBACK_P is true, fall back to extract_fixed_bit_field
1384 if we can find no other means of implementing the operation.
1385 if FALLBACK_P is false, return NULL instead. */
1388 extract_bit_field_1 (rtx str_rtx
, unsigned HOST_WIDE_INT bitsize
,
1389 unsigned HOST_WIDE_INT bitnum
, int unsignedp
, rtx target
,
1390 enum machine_mode mode
, enum machine_mode tmode
,
1394 enum machine_mode int_mode
;
1395 enum machine_mode mode1
;
1397 if (tmode
== VOIDmode
)
1400 while (GET_CODE (op0
) == SUBREG
)
1402 bitnum
+= SUBREG_BYTE (op0
) * BITS_PER_UNIT
;
1403 op0
= SUBREG_REG (op0
);
1406 /* If we have an out-of-bounds access to a register, just return an
1407 uninitialized register of the required mode. This can occur if the
1408 source code contains an out-of-bounds access to a small array. */
1409 if (REG_P (op0
) && bitnum
>= GET_MODE_BITSIZE (GET_MODE (op0
)))
1410 return gen_reg_rtx (tmode
);
1413 && mode
== GET_MODE (op0
)
1415 && bitsize
== GET_MODE_BITSIZE (GET_MODE (op0
)))
1417 /* We're trying to extract a full register from itself. */
1421 /* See if we can get a better vector mode before extracting. */
1422 if (VECTOR_MODE_P (GET_MODE (op0
))
1424 && GET_MODE_INNER (GET_MODE (op0
)) != tmode
)
1426 enum machine_mode new_mode
;
1428 if (GET_MODE_CLASS (tmode
) == MODE_FLOAT
)
1429 new_mode
= MIN_MODE_VECTOR_FLOAT
;
1430 else if (GET_MODE_CLASS (tmode
) == MODE_FRACT
)
1431 new_mode
= MIN_MODE_VECTOR_FRACT
;
1432 else if (GET_MODE_CLASS (tmode
) == MODE_UFRACT
)
1433 new_mode
= MIN_MODE_VECTOR_UFRACT
;
1434 else if (GET_MODE_CLASS (tmode
) == MODE_ACCUM
)
1435 new_mode
= MIN_MODE_VECTOR_ACCUM
;
1436 else if (GET_MODE_CLASS (tmode
) == MODE_UACCUM
)
1437 new_mode
= MIN_MODE_VECTOR_UACCUM
;
1439 new_mode
= MIN_MODE_VECTOR_INT
;
1441 for (; new_mode
!= VOIDmode
; new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1442 if (GET_MODE_SIZE (new_mode
) == GET_MODE_SIZE (GET_MODE (op0
))
1443 && targetm
.vector_mode_supported_p (new_mode
))
1445 if (new_mode
!= VOIDmode
)
1446 op0
= gen_lowpart (new_mode
, op0
);
1449 /* Use vec_extract patterns for extracting parts of vectors whenever
1451 if (VECTOR_MODE_P (GET_MODE (op0
))
1453 && optab_handler (vec_extract_optab
, GET_MODE (op0
)) != CODE_FOR_nothing
1454 && ((bitnum
+ bitsize
- 1) / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0
)))
1455 == bitnum
/ GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0
)))))
1457 struct expand_operand ops
[3];
1458 enum machine_mode outermode
= GET_MODE (op0
);
1459 enum machine_mode innermode
= GET_MODE_INNER (outermode
);
1460 enum insn_code icode
= optab_handler (vec_extract_optab
, outermode
);
1461 unsigned HOST_WIDE_INT pos
= bitnum
/ GET_MODE_BITSIZE (innermode
);
1463 create_output_operand (&ops
[0], target
, innermode
);
1464 create_input_operand (&ops
[1], op0
, outermode
);
1465 create_integer_operand (&ops
[2], pos
);
1466 if (maybe_expand_insn (icode
, 3, ops
))
1468 target
= ops
[0].value
;
1469 if (GET_MODE (target
) != mode
)
1470 return gen_lowpart (tmode
, target
);
1475 /* Make sure we are playing with integral modes. Pun with subregs
1478 enum machine_mode imode
= int_mode_for_mode (GET_MODE (op0
));
1479 if (imode
!= GET_MODE (op0
))
1482 op0
= adjust_bitfield_address_size (op0
, imode
, 0, MEM_SIZE (op0
));
1483 else if (imode
!= BLKmode
)
1485 op0
= gen_lowpart (imode
, op0
);
1487 /* If we got a SUBREG, force it into a register since we
1488 aren't going to be able to do another SUBREG on it. */
1489 if (GET_CODE (op0
) == SUBREG
)
1490 op0
= force_reg (imode
, op0
);
1492 else if (REG_P (op0
))
1495 imode
= smallest_mode_for_size (GET_MODE_BITSIZE (GET_MODE (op0
)),
1497 reg
= gen_reg_rtx (imode
);
1498 subreg
= gen_lowpart_SUBREG (GET_MODE (op0
), reg
);
1499 emit_move_insn (subreg
, op0
);
1501 bitnum
+= SUBREG_BYTE (subreg
) * BITS_PER_UNIT
;
1505 HOST_WIDE_INT size
= GET_MODE_SIZE (GET_MODE (op0
));
1506 rtx mem
= assign_stack_temp (GET_MODE (op0
), size
);
1507 emit_move_insn (mem
, op0
);
1508 op0
= adjust_bitfield_address_size (mem
, BLKmode
, 0, size
);
1513 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1514 If that's wrong, the solution is to test for it and set TARGET to 0
1517 /* Get the mode of the field to use for atomic access or subreg
1520 if (SCALAR_INT_MODE_P (tmode
))
1522 enum machine_mode try_mode
= mode_for_size (bitsize
,
1523 GET_MODE_CLASS (tmode
), 0);
1524 if (try_mode
!= BLKmode
)
1527 gcc_assert (mode1
!= BLKmode
);
1529 /* Extraction of a full MODE1 value can be done with a subreg as long
1530 as the least significant bit of the value is the least significant
1531 bit of either OP0 or a word of OP0. */
1533 && lowpart_bit_field_p (bitnum
, bitsize
, GET_MODE (op0
))
1534 && bitsize
== GET_MODE_BITSIZE (mode1
)
1535 && TRULY_NOOP_TRUNCATION_MODES_P (mode1
, GET_MODE (op0
)))
1537 rtx sub
= simplify_gen_subreg (mode1
, op0
, GET_MODE (op0
),
1538 bitnum
/ BITS_PER_UNIT
);
1540 return convert_extracted_bit_field (sub
, mode
, tmode
, unsignedp
);
1543 /* Extraction of a full MODE1 value can be done with a load as long as
1544 the field is on a byte boundary and is sufficiently aligned. */
1545 if (simple_mem_bitfield_p (op0
, bitsize
, bitnum
, mode1
))
1547 op0
= adjust_bitfield_address (op0
, mode1
, bitnum
/ BITS_PER_UNIT
);
1548 return convert_extracted_bit_field (op0
, mode
, tmode
, unsignedp
);
1551 /* Handle fields bigger than a word. */
1553 if (bitsize
> BITS_PER_WORD
)
1555 /* Here we transfer the words of the field
1556 in the order least significant first.
1557 This is because the most significant word is the one which may
1558 be less than full. */
1560 unsigned int backwards
= WORDS_BIG_ENDIAN
;
1561 unsigned int nwords
= (bitsize
+ (BITS_PER_WORD
- 1)) / BITS_PER_WORD
;
1565 if (target
== 0 || !REG_P (target
) || !valid_multiword_target_p (target
))
1566 target
= gen_reg_rtx (mode
);
1568 /* Indicate for flow that the entire target reg is being set. */
1569 emit_clobber (target
);
1571 last
= get_last_insn ();
1572 for (i
= 0; i
< nwords
; i
++)
1574 /* If I is 0, use the low-order word in both field and target;
1575 if I is 1, use the next to lowest word; and so on. */
1576 /* Word number in TARGET to use. */
1577 unsigned int wordnum
1579 ? GET_MODE_SIZE (GET_MODE (target
)) / UNITS_PER_WORD
- i
- 1
1581 /* Offset from start of field in OP0. */
1582 unsigned int bit_offset
= (backwards
1583 ? MAX ((int) bitsize
- ((int) i
+ 1)
1586 : (int) i
* BITS_PER_WORD
);
1587 rtx target_part
= operand_subword (target
, wordnum
, 1, VOIDmode
);
1589 = extract_bit_field_1 (op0
, MIN (BITS_PER_WORD
,
1590 bitsize
- i
* BITS_PER_WORD
),
1591 bitnum
+ bit_offset
, 1, target_part
,
1592 mode
, word_mode
, fallback_p
);
1594 gcc_assert (target_part
);
1597 delete_insns_since (last
);
1601 if (result_part
!= target_part
)
1602 emit_move_insn (target_part
, result_part
);
1607 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1608 need to be zero'd out. */
1609 if (GET_MODE_SIZE (GET_MODE (target
)) > nwords
* UNITS_PER_WORD
)
1611 unsigned int i
, total_words
;
1613 total_words
= GET_MODE_SIZE (GET_MODE (target
)) / UNITS_PER_WORD
;
1614 for (i
= nwords
; i
< total_words
; i
++)
1616 (operand_subword (target
,
1617 backwards
? total_words
- i
- 1 : i
,
1624 /* Signed bit field: sign-extend with two arithmetic shifts. */
1625 target
= expand_shift (LSHIFT_EXPR
, mode
, target
,
1626 GET_MODE_BITSIZE (mode
) - bitsize
, NULL_RTX
, 0);
1627 return expand_shift (RSHIFT_EXPR
, mode
, target
,
1628 GET_MODE_BITSIZE (mode
) - bitsize
, NULL_RTX
, 0);
1631 /* If OP0 is a multi-word register, narrow it to the affected word.
1632 If the region spans two words, defer to extract_split_bit_field. */
1633 if (!MEM_P (op0
) && GET_MODE_SIZE (GET_MODE (op0
)) > UNITS_PER_WORD
)
1635 op0
= simplify_gen_subreg (word_mode
, op0
, GET_MODE (op0
),
1636 bitnum
/ BITS_PER_WORD
* UNITS_PER_WORD
);
1637 bitnum
%= BITS_PER_WORD
;
1638 if (bitnum
+ bitsize
> BITS_PER_WORD
)
1642 target
= extract_split_bit_field (op0
, bitsize
, bitnum
, unsignedp
);
1643 return convert_extracted_bit_field (target
, mode
, tmode
, unsignedp
);
1647 /* From here on we know the desired field is smaller than a word.
1648 If OP0 is a register, it too fits within a word. */
1649 enum extraction_pattern pattern
= unsignedp
? EP_extzv
: EP_extv
;
1650 extraction_insn extv
;
1652 /* ??? We could limit the structure size to the part of OP0 that
1653 contains the field, with appropriate checks for endianness
1654 and TRULY_NOOP_TRUNCATION. */
1655 && get_best_reg_extraction_insn (&extv
, pattern
,
1656 GET_MODE_BITSIZE (GET_MODE (op0
)),
1659 rtx result
= extract_bit_field_using_extv (&extv
, op0
, bitsize
, bitnum
,
1660 unsignedp
, target
, mode
,
1666 /* If OP0 is a memory, try copying it to a register and seeing if a
1667 cheap register alternative is available. */
1670 if (get_best_mem_extraction_insn (&extv
, pattern
, bitsize
, bitnum
,
1673 rtx result
= extract_bit_field_using_extv (&extv
, op0
, bitsize
,
1681 rtx_insn
*last
= get_last_insn ();
1683 /* Try loading part of OP0 into a register and extracting the
1684 bitfield from that. */
1685 unsigned HOST_WIDE_INT bitpos
;
1686 rtx xop0
= adjust_bit_field_mem_for_reg (pattern
, op0
, bitsize
, bitnum
,
1687 0, 0, tmode
, &bitpos
);
1690 xop0
= copy_to_reg (xop0
);
1691 rtx result
= extract_bit_field_1 (xop0
, bitsize
, bitpos
,
1693 mode
, tmode
, false);
1696 delete_insns_since (last
);
1703 /* Find a correspondingly-sized integer field, so we can apply
1704 shifts and masks to it. */
1705 int_mode
= int_mode_for_mode (tmode
);
1706 if (int_mode
== BLKmode
)
1707 int_mode
= int_mode_for_mode (mode
);
1708 /* Should probably push op0 out to memory and then do a load. */
1709 gcc_assert (int_mode
!= BLKmode
);
1711 target
= extract_fixed_bit_field (int_mode
, op0
, bitsize
, bitnum
,
1713 return convert_extracted_bit_field (target
, mode
, tmode
, unsignedp
);
1716 /* Generate code to extract a byte-field from STR_RTX
1717 containing BITSIZE bits, starting at BITNUM,
1718 and put it in TARGET if possible (if TARGET is nonzero).
1719 Regardless of TARGET, we return the rtx for where the value is placed.
1721 STR_RTX is the structure containing the byte (a REG or MEM).
1722 UNSIGNEDP is nonzero if this is an unsigned bit field.
1723 MODE is the natural mode of the field value once extracted.
1724 TMODE is the mode the caller would like the value to have;
1725 but the value may be returned with type MODE instead.
1727 If a TARGET is specified and we can store in it at no extra cost,
1728 we do so, and return TARGET.
1729 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1730 if they are equally easy. */
1733 extract_bit_field (rtx str_rtx
, unsigned HOST_WIDE_INT bitsize
,
1734 unsigned HOST_WIDE_INT bitnum
, int unsignedp
, rtx target
,
1735 enum machine_mode mode
, enum machine_mode tmode
)
1737 enum machine_mode mode1
;
1739 /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
1740 if (GET_MODE_BITSIZE (GET_MODE (str_rtx
)) > 0)
1741 mode1
= GET_MODE (str_rtx
);
1742 else if (target
&& GET_MODE_BITSIZE (GET_MODE (target
)) > 0)
1743 mode1
= GET_MODE (target
);
1747 if (strict_volatile_bitfield_p (str_rtx
, bitsize
, bitnum
, mode1
, 0, 0))
1751 /* Extraction of a full MODE1 value can be done with a load as long as
1752 the field is on a byte boundary and is sufficiently aligned. */
1753 if (simple_mem_bitfield_p (str_rtx
, bitsize
, bitnum
, mode1
))
1754 result
= adjust_bitfield_address (str_rtx
, mode1
,
1755 bitnum
/ BITS_PER_UNIT
);
1758 str_rtx
= narrow_bit_field_mem (str_rtx
, mode1
, bitsize
, bitnum
,
1760 result
= extract_fixed_bit_field_1 (mode
, str_rtx
, bitsize
, bitnum
,
1764 return convert_extracted_bit_field (result
, mode
, tmode
, unsignedp
);
1767 return extract_bit_field_1 (str_rtx
, bitsize
, bitnum
, unsignedp
,
1768 target
, mode
, tmode
, true);
1771 /* Use shifts and boolean operations to extract a field of BITSIZE bits
1772 from bit BITNUM of OP0.
1774 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1775 If TARGET is nonzero, attempts to store the value there
1776 and return TARGET, but this is not guaranteed.
1777 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
1780 extract_fixed_bit_field (enum machine_mode tmode
, rtx op0
,
1781 unsigned HOST_WIDE_INT bitsize
,
1782 unsigned HOST_WIDE_INT bitnum
, rtx target
,
1787 enum machine_mode mode
1788 = get_best_mode (bitsize
, bitnum
, 0, 0, MEM_ALIGN (op0
), word_mode
,
1789 MEM_VOLATILE_P (op0
));
1791 if (mode
== VOIDmode
)
1792 /* The only way this should occur is if the field spans word
1794 return extract_split_bit_field (op0
, bitsize
, bitnum
, unsignedp
);
1796 op0
= narrow_bit_field_mem (op0
, mode
, bitsize
, bitnum
, &bitnum
);
1799 return extract_fixed_bit_field_1 (tmode
, op0
, bitsize
, bitnum
,
1803 /* Helper function for extract_fixed_bit_field, extracts
1804 the bit field always using the MODE of OP0. */
1807 extract_fixed_bit_field_1 (enum machine_mode tmode
, rtx op0
,
1808 unsigned HOST_WIDE_INT bitsize
,
1809 unsigned HOST_WIDE_INT bitnum
, rtx target
,
1812 enum machine_mode mode
= GET_MODE (op0
);
1813 gcc_assert (SCALAR_INT_MODE_P (mode
));
1815 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
1816 for invalid input, such as extract equivalent of f5 from
1817 gcc.dg/pr48335-2.c. */
1819 if (BYTES_BIG_ENDIAN
)
1820 /* BITNUM is the distance between our msb and that of OP0.
1821 Convert it to the distance from the lsb. */
1822 bitnum
= GET_MODE_BITSIZE (mode
) - bitsize
- bitnum
;
1824 /* Now BITNUM is always the distance between the field's lsb and that of OP0.
1825 We have reduced the big-endian case to the little-endian case. */
1831 /* If the field does not already start at the lsb,
1832 shift it so it does. */
1833 /* Maybe propagate the target for the shift. */
1834 rtx subtarget
= (target
!= 0 && REG_P (target
) ? target
: 0);
1837 op0
= expand_shift (RSHIFT_EXPR
, mode
, op0
, bitnum
, subtarget
, 1);
1839 /* Convert the value to the desired mode. */
1841 op0
= convert_to_mode (tmode
, op0
, 1);
1843 /* Unless the msb of the field used to be the msb when we shifted,
1844 mask out the upper bits. */
1846 if (GET_MODE_BITSIZE (mode
) != bitnum
+ bitsize
)
1847 return expand_binop (GET_MODE (op0
), and_optab
, op0
,
1848 mask_rtx (GET_MODE (op0
), 0, bitsize
, 0),
1849 target
, 1, OPTAB_LIB_WIDEN
);
1853 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1854 then arithmetic-shift its lsb to the lsb of the word. */
1855 op0
= force_reg (mode
, op0
);
1857 /* Find the narrowest integer mode that contains the field. */
1859 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1860 mode
= GET_MODE_WIDER_MODE (mode
))
1861 if (GET_MODE_BITSIZE (mode
) >= bitsize
+ bitnum
)
1863 op0
= convert_to_mode (mode
, op0
, 0);
1870 if (GET_MODE_BITSIZE (mode
) != (bitsize
+ bitnum
))
1872 int amount
= GET_MODE_BITSIZE (mode
) - (bitsize
+ bitnum
);
1873 /* Maybe propagate the target for the shift. */
1874 rtx subtarget
= (target
!= 0 && REG_P (target
) ? target
: 0);
1875 op0
= expand_shift (LSHIFT_EXPR
, mode
, op0
, amount
, subtarget
, 1);
1878 return expand_shift (RSHIFT_EXPR
, mode
, op0
,
1879 GET_MODE_BITSIZE (mode
) - bitsize
, target
, 0);
1882 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1886 lshift_value (enum machine_mode mode
, unsigned HOST_WIDE_INT value
,
1889 return immed_wide_int_const (wi::lshift (value
, bitpos
), mode
);
1892 /* Extract a bit field that is split across two words
1893 and return an RTX for the result.
1895 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1896 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1897 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
1900 extract_split_bit_field (rtx op0
, unsigned HOST_WIDE_INT bitsize
,
1901 unsigned HOST_WIDE_INT bitpos
, int unsignedp
)
1904 unsigned int bitsdone
= 0;
1905 rtx result
= NULL_RTX
;
1908 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1910 if (REG_P (op0
) || GET_CODE (op0
) == SUBREG
)
1911 unit
= BITS_PER_WORD
;
1913 unit
= MIN (MEM_ALIGN (op0
), BITS_PER_WORD
);
1915 while (bitsdone
< bitsize
)
1917 unsigned HOST_WIDE_INT thissize
;
1919 unsigned HOST_WIDE_INT thispos
;
1920 unsigned HOST_WIDE_INT offset
;
1922 offset
= (bitpos
+ bitsdone
) / unit
;
1923 thispos
= (bitpos
+ bitsdone
) % unit
;
1925 /* THISSIZE must not overrun a word boundary. Otherwise,
1926 extract_fixed_bit_field will call us again, and we will mutually
1928 thissize
= MIN (bitsize
- bitsdone
, BITS_PER_WORD
);
1929 thissize
= MIN (thissize
, unit
- thispos
);
1931 /* If OP0 is a register, then handle OFFSET here.
1933 When handling multiword bitfields, extract_bit_field may pass
1934 down a word_mode SUBREG of a larger REG for a bitfield that actually
1935 crosses a word boundary. Thus, for a SUBREG, we must find
1936 the current word starting from the base register. */
1937 if (GET_CODE (op0
) == SUBREG
)
1939 int word_offset
= (SUBREG_BYTE (op0
) / UNITS_PER_WORD
) + offset
;
1940 word
= operand_subword_force (SUBREG_REG (op0
), word_offset
,
1941 GET_MODE (SUBREG_REG (op0
)));
1944 else if (REG_P (op0
))
1946 word
= operand_subword_force (op0
, offset
, GET_MODE (op0
));
1952 /* Extract the parts in bit-counting order,
1953 whose meaning is determined by BYTES_PER_UNIT.
1954 OFFSET is in UNITs, and UNIT is in bits. */
1955 part
= extract_fixed_bit_field (word_mode
, word
, thissize
,
1956 offset
* unit
+ thispos
, 0, 1);
1957 bitsdone
+= thissize
;
1959 /* Shift this part into place for the result. */
1960 if (BYTES_BIG_ENDIAN
)
1962 if (bitsize
!= bitsdone
)
1963 part
= expand_shift (LSHIFT_EXPR
, word_mode
, part
,
1964 bitsize
- bitsdone
, 0, 1);
1968 if (bitsdone
!= thissize
)
1969 part
= expand_shift (LSHIFT_EXPR
, word_mode
, part
,
1970 bitsdone
- thissize
, 0, 1);
1976 /* Combine the parts with bitwise or. This works
1977 because we extracted each part as an unsigned bit field. */
1978 result
= expand_binop (word_mode
, ior_optab
, part
, result
, NULL_RTX
, 1,
1984 /* Unsigned bit field: we are done. */
1987 /* Signed bit field: sign-extend with two arithmetic shifts. */
1988 result
= expand_shift (LSHIFT_EXPR
, word_mode
, result
,
1989 BITS_PER_WORD
- bitsize
, NULL_RTX
, 0);
1990 return expand_shift (RSHIFT_EXPR
, word_mode
, result
,
1991 BITS_PER_WORD
- bitsize
, NULL_RTX
, 0);
1994 /* Try to read the low bits of SRC as an rvalue of mode MODE, preserving
1995 the bit pattern. SRC_MODE is the mode of SRC; if this is smaller than
1996 MODE, fill the upper bits with zeros. Fail if the layout of either
1997 mode is unknown (as for CC modes) or if the extraction would involve
1998 unprofitable mode punning. Return the value on success, otherwise
2001 This is different from gen_lowpart* in these respects:
2003 - the returned value must always be considered an rvalue
2005 - when MODE is wider than SRC_MODE, the extraction involves
2008 - when MODE is smaller than SRC_MODE, the extraction involves
2009 a truncation (and is thus subject to TRULY_NOOP_TRUNCATION).
2011 In other words, this routine performs a computation, whereas the
2012 gen_lowpart* routines are conceptually lvalue or rvalue subreg
2016 extract_low_bits (enum machine_mode mode
, enum machine_mode src_mode
, rtx src
)
2018 enum machine_mode int_mode
, src_int_mode
;
2020 if (mode
== src_mode
)
2023 if (CONSTANT_P (src
))
2025 /* simplify_gen_subreg can't be used here, as if simplify_subreg
2026 fails, it will happily create (subreg (symbol_ref)) or similar
2028 unsigned int byte
= subreg_lowpart_offset (mode
, src_mode
);
2029 rtx ret
= simplify_subreg (mode
, src
, src_mode
, byte
);
2033 if (GET_MODE (src
) == VOIDmode
2034 || !validate_subreg (mode
, src_mode
, src
, byte
))
2037 src
= force_reg (GET_MODE (src
), src
);
2038 return gen_rtx_SUBREG (mode
, src
, byte
);
2041 if (GET_MODE_CLASS (mode
) == MODE_CC
|| GET_MODE_CLASS (src_mode
) == MODE_CC
)
2044 if (GET_MODE_BITSIZE (mode
) == GET_MODE_BITSIZE (src_mode
)
2045 && MODES_TIEABLE_P (mode
, src_mode
))
2047 rtx x
= gen_lowpart_common (mode
, src
);
2052 src_int_mode
= int_mode_for_mode (src_mode
);
2053 int_mode
= int_mode_for_mode (mode
);
2054 if (src_int_mode
== BLKmode
|| int_mode
== BLKmode
)
2057 if (!MODES_TIEABLE_P (src_int_mode
, src_mode
))
2059 if (!MODES_TIEABLE_P (int_mode
, mode
))
2062 src
= gen_lowpart (src_int_mode
, src
);
2063 src
= convert_modes (int_mode
, src_int_mode
, src
, true);
2064 src
= gen_lowpart (mode
, src
);
2068 /* Add INC into TARGET. */
2071 expand_inc (rtx target
, rtx inc
)
2073 rtx value
= expand_binop (GET_MODE (target
), add_optab
,
2075 target
, 0, OPTAB_LIB_WIDEN
);
2076 if (value
!= target
)
2077 emit_move_insn (target
, value
);
2080 /* Subtract DEC from TARGET. */
2083 expand_dec (rtx target
, rtx dec
)
2085 rtx value
= expand_binop (GET_MODE (target
), sub_optab
,
2087 target
, 0, OPTAB_LIB_WIDEN
);
2088 if (value
!= target
)
2089 emit_move_insn (target
, value
);
2092 /* Output a shift instruction for expression code CODE,
2093 with SHIFTED being the rtx for the value to shift,
2094 and AMOUNT the rtx for the amount to shift by.
2095 Store the result in the rtx TARGET, if that is convenient.
2096 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2097 Return the rtx for where the value is. */
2100 expand_shift_1 (enum tree_code code
, enum machine_mode mode
, rtx shifted
,
2101 rtx amount
, rtx target
, int unsignedp
)
2104 int left
= (code
== LSHIFT_EXPR
|| code
== LROTATE_EXPR
);
2105 int rotate
= (code
== LROTATE_EXPR
|| code
== RROTATE_EXPR
);
2106 optab lshift_optab
= ashl_optab
;
2107 optab rshift_arith_optab
= ashr_optab
;
2108 optab rshift_uns_optab
= lshr_optab
;
2109 optab lrotate_optab
= rotl_optab
;
2110 optab rrotate_optab
= rotr_optab
;
2111 enum machine_mode op1_mode
;
2112 enum machine_mode scalar_mode
= mode
;
2114 bool speed
= optimize_insn_for_speed_p ();
2116 if (VECTOR_MODE_P (mode
))
2117 scalar_mode
= GET_MODE_INNER (mode
);
2119 op1_mode
= GET_MODE (op1
);
2121 /* Determine whether the shift/rotate amount is a vector, or scalar. If the
2122 shift amount is a vector, use the vector/vector shift patterns. */
2123 if (VECTOR_MODE_P (mode
) && VECTOR_MODE_P (op1_mode
))
2125 lshift_optab
= vashl_optab
;
2126 rshift_arith_optab
= vashr_optab
;
2127 rshift_uns_optab
= vlshr_optab
;
2128 lrotate_optab
= vrotl_optab
;
2129 rrotate_optab
= vrotr_optab
;
2132 /* Previously detected shift-counts computed by NEGATE_EXPR
2133 and shifted in the other direction; but that does not work
2136 if (SHIFT_COUNT_TRUNCATED
)
2138 if (CONST_INT_P (op1
)
2139 && ((unsigned HOST_WIDE_INT
) INTVAL (op1
) >=
2140 (unsigned HOST_WIDE_INT
) GET_MODE_BITSIZE (scalar_mode
)))
2141 op1
= GEN_INT ((unsigned HOST_WIDE_INT
) INTVAL (op1
)
2142 % GET_MODE_BITSIZE (scalar_mode
));
2143 else if (GET_CODE (op1
) == SUBREG
2144 && subreg_lowpart_p (op1
)
2145 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op1
)))
2146 && SCALAR_INT_MODE_P (GET_MODE (op1
)))
2147 op1
= SUBREG_REG (op1
);
2150 /* Canonicalize rotates by constant amount. If op1 is bitsize / 2,
2151 prefer left rotation, if op1 is from bitsize / 2 + 1 to
2152 bitsize - 1, use other direction of rotate with 1 .. bitsize / 2 - 1
2155 && CONST_INT_P (op1
)
2156 && IN_RANGE (INTVAL (op1
), GET_MODE_BITSIZE (scalar_mode
) / 2 + left
,
2157 GET_MODE_BITSIZE (scalar_mode
) - 1))
2159 op1
= GEN_INT (GET_MODE_BITSIZE (scalar_mode
) - INTVAL (op1
));
2161 code
= left
? LROTATE_EXPR
: RROTATE_EXPR
;
2164 if (op1
== const0_rtx
)
2167 /* Check whether its cheaper to implement a left shift by a constant
2168 bit count by a sequence of additions. */
2169 if (code
== LSHIFT_EXPR
2170 && CONST_INT_P (op1
)
2172 && INTVAL (op1
) < GET_MODE_PRECISION (scalar_mode
)
2173 && INTVAL (op1
) < MAX_BITS_PER_WORD
2174 && (shift_cost (speed
, mode
, INTVAL (op1
))
2175 > INTVAL (op1
) * add_cost (speed
, mode
))
2176 && shift_cost (speed
, mode
, INTVAL (op1
)) != MAX_COST
)
2179 for (i
= 0; i
< INTVAL (op1
); i
++)
2181 temp
= force_reg (mode
, shifted
);
2182 shifted
= expand_binop (mode
, add_optab
, temp
, temp
, NULL_RTX
,
2183 unsignedp
, OPTAB_LIB_WIDEN
);
2188 for (attempt
= 0; temp
== 0 && attempt
< 3; attempt
++)
2190 enum optab_methods methods
;
2193 methods
= OPTAB_DIRECT
;
2194 else if (attempt
== 1)
2195 methods
= OPTAB_WIDEN
;
2197 methods
= OPTAB_LIB_WIDEN
;
2201 /* Widening does not work for rotation. */
2202 if (methods
== OPTAB_WIDEN
)
2204 else if (methods
== OPTAB_LIB_WIDEN
)
2206 /* If we have been unable to open-code this by a rotation,
2207 do it as the IOR of two shifts. I.e., to rotate A
2209 (A << N) | ((unsigned) A >> ((-N) & (C - 1)))
2210 where C is the bitsize of A.
2212 It is theoretically possible that the target machine might
2213 not be able to perform either shift and hence we would
2214 be making two libcalls rather than just the one for the
2215 shift (similarly if IOR could not be done). We will allow
2216 this extremely unlikely lossage to avoid complicating the
2219 rtx subtarget
= target
== shifted
? 0 : target
;
2220 rtx new_amount
, other_amount
;
2224 if (op1
== const0_rtx
)
2226 else if (CONST_INT_P (op1
))
2227 other_amount
= GEN_INT (GET_MODE_BITSIZE (scalar_mode
)
2232 = simplify_gen_unary (NEG
, GET_MODE (op1
),
2233 op1
, GET_MODE (op1
));
2234 HOST_WIDE_INT mask
= GET_MODE_PRECISION (scalar_mode
) - 1;
2236 = simplify_gen_binary (AND
, GET_MODE (op1
), other_amount
,
2237 gen_int_mode (mask
, GET_MODE (op1
)));
2240 shifted
= force_reg (mode
, shifted
);
2242 temp
= expand_shift_1 (left
? LSHIFT_EXPR
: RSHIFT_EXPR
,
2243 mode
, shifted
, new_amount
, 0, 1);
2244 temp1
= expand_shift_1 (left
? RSHIFT_EXPR
: LSHIFT_EXPR
,
2245 mode
, shifted
, other_amount
,
2247 return expand_binop (mode
, ior_optab
, temp
, temp1
, target
,
2248 unsignedp
, methods
);
2251 temp
= expand_binop (mode
,
2252 left
? lrotate_optab
: rrotate_optab
,
2253 shifted
, op1
, target
, unsignedp
, methods
);
2256 temp
= expand_binop (mode
,
2257 left
? lshift_optab
: rshift_uns_optab
,
2258 shifted
, op1
, target
, unsignedp
, methods
);
2260 /* Do arithmetic shifts.
2261 Also, if we are going to widen the operand, we can just as well
2262 use an arithmetic right-shift instead of a logical one. */
2263 if (temp
== 0 && ! rotate
2264 && (! unsignedp
|| (! left
&& methods
== OPTAB_WIDEN
)))
2266 enum optab_methods methods1
= methods
;
2268 /* If trying to widen a log shift to an arithmetic shift,
2269 don't accept an arithmetic shift of the same size. */
2271 methods1
= OPTAB_MUST_WIDEN
;
2273 /* Arithmetic shift */
2275 temp
= expand_binop (mode
,
2276 left
? lshift_optab
: rshift_arith_optab
,
2277 shifted
, op1
, target
, unsignedp
, methods1
);
2280 /* We used to try extzv here for logical right shifts, but that was
2281 only useful for one machine, the VAX, and caused poor code
2282 generation there for lshrdi3, so the code was deleted and a
2283 define_expand for lshrsi3 was added to vax.md. */
2290 /* Output a shift instruction for expression code CODE,
2291 with SHIFTED being the rtx for the value to shift,
2292 and AMOUNT the amount to shift by.
2293 Store the result in the rtx TARGET, if that is convenient.
2294 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2295 Return the rtx for where the value is. */
2298 expand_shift (enum tree_code code
, enum machine_mode mode
, rtx shifted
,
2299 int amount
, rtx target
, int unsignedp
)
2301 return expand_shift_1 (code
, mode
,
2302 shifted
, GEN_INT (amount
), target
, unsignedp
);
2305 /* Output a shift instruction for expression code CODE,
2306 with SHIFTED being the rtx for the value to shift,
2307 and AMOUNT the tree for the amount to shift by.
2308 Store the result in the rtx TARGET, if that is convenient.
2309 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2310 Return the rtx for where the value is. */
2313 expand_variable_shift (enum tree_code code
, enum machine_mode mode
, rtx shifted
,
2314 tree amount
, rtx target
, int unsignedp
)
2316 return expand_shift_1 (code
, mode
,
2317 shifted
, expand_normal (amount
), target
, unsignedp
);
2321 /* Indicates the type of fixup needed after a constant multiplication.
2322 BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2323 the result should be negated, and ADD_VARIANT means that the
2324 multiplicand should be added to the result. */
2325 enum mult_variant
{basic_variant
, negate_variant
, add_variant
};
2327 static void synth_mult (struct algorithm
*, unsigned HOST_WIDE_INT
,
2328 const struct mult_cost
*, enum machine_mode mode
);
2329 static bool choose_mult_variant (enum machine_mode
, HOST_WIDE_INT
,
2330 struct algorithm
*, enum mult_variant
*, int);
2331 static rtx
expand_mult_const (enum machine_mode
, rtx
, HOST_WIDE_INT
, rtx
,
2332 const struct algorithm
*, enum mult_variant
);
2333 static unsigned HOST_WIDE_INT
invert_mod2n (unsigned HOST_WIDE_INT
, int);
2334 static rtx
extract_high_half (enum machine_mode
, rtx
);
2335 static rtx
expmed_mult_highpart (enum machine_mode
, rtx
, rtx
, rtx
, int, int);
2336 static rtx
expmed_mult_highpart_optab (enum machine_mode
, rtx
, rtx
, rtx
,
2338 /* Compute and return the best algorithm for multiplying by T.
2339 The algorithm must cost less than cost_limit
2340 If retval.cost >= COST_LIMIT, no algorithm was found and all
2341 other field of the returned struct are undefined.
2342 MODE is the machine mode of the multiplication. */
2345 synth_mult (struct algorithm
*alg_out
, unsigned HOST_WIDE_INT t
,
2346 const struct mult_cost
*cost_limit
, enum machine_mode mode
)
2349 struct algorithm
*alg_in
, *best_alg
;
2350 struct mult_cost best_cost
;
2351 struct mult_cost new_limit
;
2352 int op_cost
, op_latency
;
2353 unsigned HOST_WIDE_INT orig_t
= t
;
2354 unsigned HOST_WIDE_INT q
;
2355 int maxm
, hash_index
;
2356 bool cache_hit
= false;
2357 enum alg_code cache_alg
= alg_zero
;
2358 bool speed
= optimize_insn_for_speed_p ();
2359 enum machine_mode imode
;
2360 struct alg_hash_entry
*entry_ptr
;
2362 /* Indicate that no algorithm is yet found. If no algorithm
2363 is found, this value will be returned and indicate failure. */
2364 alg_out
->cost
.cost
= cost_limit
->cost
+ 1;
2365 alg_out
->cost
.latency
= cost_limit
->latency
+ 1;
2367 if (cost_limit
->cost
< 0
2368 || (cost_limit
->cost
== 0 && cost_limit
->latency
<= 0))
2371 /* Be prepared for vector modes. */
2372 imode
= GET_MODE_INNER (mode
);
2373 if (imode
== VOIDmode
)
2376 maxm
= MIN (BITS_PER_WORD
, GET_MODE_BITSIZE (imode
));
2378 /* Restrict the bits of "t" to the multiplication's mode. */
2379 t
&= GET_MODE_MASK (imode
);
2381 /* t == 1 can be done in zero cost. */
2385 alg_out
->cost
.cost
= 0;
2386 alg_out
->cost
.latency
= 0;
2387 alg_out
->op
[0] = alg_m
;
2391 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2395 if (MULT_COST_LESS (cost_limit
, zero_cost (speed
)))
2400 alg_out
->cost
.cost
= zero_cost (speed
);
2401 alg_out
->cost
.latency
= zero_cost (speed
);
2402 alg_out
->op
[0] = alg_zero
;
2407 /* We'll be needing a couple extra algorithm structures now. */
2409 alg_in
= XALLOCA (struct algorithm
);
2410 best_alg
= XALLOCA (struct algorithm
);
2411 best_cost
= *cost_limit
;
2413 /* Compute the hash index. */
2414 hash_index
= (t
^ (unsigned int) mode
^ (speed
* 256)) % NUM_ALG_HASH_ENTRIES
;
2416 /* See if we already know what to do for T. */
2417 entry_ptr
= alg_hash_entry_ptr (hash_index
);
2418 if (entry_ptr
->t
== t
2419 && entry_ptr
->mode
== mode
2420 && entry_ptr
->mode
== mode
2421 && entry_ptr
->speed
== speed
2422 && entry_ptr
->alg
!= alg_unknown
)
2424 cache_alg
= entry_ptr
->alg
;
2426 if (cache_alg
== alg_impossible
)
2428 /* The cache tells us that it's impossible to synthesize
2429 multiplication by T within entry_ptr->cost. */
2430 if (!CHEAPER_MULT_COST (&entry_ptr
->cost
, cost_limit
))
2431 /* COST_LIMIT is at least as restrictive as the one
2432 recorded in the hash table, in which case we have no
2433 hope of synthesizing a multiplication. Just
2437 /* If we get here, COST_LIMIT is less restrictive than the
2438 one recorded in the hash table, so we may be able to
2439 synthesize a multiplication. Proceed as if we didn't
2440 have the cache entry. */
2444 if (CHEAPER_MULT_COST (cost_limit
, &entry_ptr
->cost
))
2445 /* The cached algorithm shows that this multiplication
2446 requires more cost than COST_LIMIT. Just return. This
2447 way, we don't clobber this cache entry with
2448 alg_impossible but retain useful information. */
2460 goto do_alg_addsub_t_m2
;
2462 case alg_add_factor
:
2463 case alg_sub_factor
:
2464 goto do_alg_addsub_factor
;
2467 goto do_alg_add_t2_m
;
2470 goto do_alg_sub_t2_m
;
2478 /* If we have a group of zero bits at the low-order part of T, try
2479 multiplying by the remaining bits and then doing a shift. */
2484 m
= floor_log2 (t
& -t
); /* m = number of low zero bits */
2488 /* The function expand_shift will choose between a shift and
2489 a sequence of additions, so the observed cost is given as
2490 MIN (m * add_cost(speed, mode), shift_cost(speed, mode, m)). */
2491 op_cost
= m
* add_cost (speed
, mode
);
2492 if (shift_cost (speed
, mode
, m
) < op_cost
)
2493 op_cost
= shift_cost (speed
, mode
, m
);
2494 new_limit
.cost
= best_cost
.cost
- op_cost
;
2495 new_limit
.latency
= best_cost
.latency
- op_cost
;
2496 synth_mult (alg_in
, q
, &new_limit
, mode
);
2498 alg_in
->cost
.cost
+= op_cost
;
2499 alg_in
->cost
.latency
+= op_cost
;
2500 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2502 struct algorithm
*x
;
2503 best_cost
= alg_in
->cost
;
2504 x
= alg_in
, alg_in
= best_alg
, best_alg
= x
;
2505 best_alg
->log
[best_alg
->ops
] = m
;
2506 best_alg
->op
[best_alg
->ops
] = alg_shift
;
2509 /* See if treating ORIG_T as a signed number yields a better
2510 sequence. Try this sequence only for a negative ORIG_T
2511 as it would be useless for a non-negative ORIG_T. */
2512 if ((HOST_WIDE_INT
) orig_t
< 0)
2514 /* Shift ORIG_T as follows because a right shift of a
2515 negative-valued signed type is implementation
2517 q
= ~(~orig_t
>> m
);
2518 /* The function expand_shift will choose between a shift
2519 and a sequence of additions, so the observed cost is
2520 given as MIN (m * add_cost(speed, mode),
2521 shift_cost(speed, mode, m)). */
2522 op_cost
= m
* add_cost (speed
, mode
);
2523 if (shift_cost (speed
, mode
, m
) < op_cost
)
2524 op_cost
= shift_cost (speed
, mode
, m
);
2525 new_limit
.cost
= best_cost
.cost
- op_cost
;
2526 new_limit
.latency
= best_cost
.latency
- op_cost
;
2527 synth_mult (alg_in
, q
, &new_limit
, mode
);
2529 alg_in
->cost
.cost
+= op_cost
;
2530 alg_in
->cost
.latency
+= op_cost
;
2531 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2533 struct algorithm
*x
;
2534 best_cost
= alg_in
->cost
;
2535 x
= alg_in
, alg_in
= best_alg
, best_alg
= x
;
2536 best_alg
->log
[best_alg
->ops
] = m
;
2537 best_alg
->op
[best_alg
->ops
] = alg_shift
;
2545 /* If we have an odd number, add or subtract one. */
2548 unsigned HOST_WIDE_INT w
;
2551 for (w
= 1; (w
& t
) != 0; w
<<= 1)
2553 /* If T was -1, then W will be zero after the loop. This is another
2554 case where T ends with ...111. Handling this with (T + 1) and
2555 subtract 1 produces slightly better code and results in algorithm
2556 selection much faster than treating it like the ...0111 case
2560 /* Reject the case where t is 3.
2561 Thus we prefer addition in that case. */
2564 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2566 op_cost
= add_cost (speed
, mode
);
2567 new_limit
.cost
= best_cost
.cost
- op_cost
;
2568 new_limit
.latency
= best_cost
.latency
- op_cost
;
2569 synth_mult (alg_in
, t
+ 1, &new_limit
, mode
);
2571 alg_in
->cost
.cost
+= op_cost
;
2572 alg_in
->cost
.latency
+= op_cost
;
2573 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2575 struct algorithm
*x
;
2576 best_cost
= alg_in
->cost
;
2577 x
= alg_in
, alg_in
= best_alg
, best_alg
= x
;
2578 best_alg
->log
[best_alg
->ops
] = 0;
2579 best_alg
->op
[best_alg
->ops
] = alg_sub_t_m2
;
2584 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2586 op_cost
= add_cost (speed
, mode
);
2587 new_limit
.cost
= best_cost
.cost
- op_cost
;
2588 new_limit
.latency
= best_cost
.latency
- op_cost
;
2589 synth_mult (alg_in
, t
- 1, &new_limit
, mode
);
2591 alg_in
->cost
.cost
+= op_cost
;
2592 alg_in
->cost
.latency
+= op_cost
;
2593 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2595 struct algorithm
*x
;
2596 best_cost
= alg_in
->cost
;
2597 x
= alg_in
, alg_in
= best_alg
, best_alg
= x
;
2598 best_alg
->log
[best_alg
->ops
] = 0;
2599 best_alg
->op
[best_alg
->ops
] = alg_add_t_m2
;
2603 /* We may be able to calculate a * -7, a * -15, a * -31, etc
2604 quickly with a - a * n for some appropriate constant n. */
2605 m
= exact_log2 (-orig_t
+ 1);
2606 if (m
>= 0 && m
< maxm
)
2608 op_cost
= shiftsub1_cost (speed
, mode
, m
);
2609 new_limit
.cost
= best_cost
.cost
- op_cost
;
2610 new_limit
.latency
= best_cost
.latency
- op_cost
;
2611 synth_mult (alg_in
, (unsigned HOST_WIDE_INT
) (-orig_t
+ 1) >> m
,
2614 alg_in
->cost
.cost
+= op_cost
;
2615 alg_in
->cost
.latency
+= op_cost
;
2616 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2618 struct algorithm
*x
;
2619 best_cost
= alg_in
->cost
;
2620 x
= alg_in
, alg_in
= best_alg
, best_alg
= x
;
2621 best_alg
->log
[best_alg
->ops
] = m
;
2622 best_alg
->op
[best_alg
->ops
] = alg_sub_t_m2
;
2630 /* Look for factors of t of the form
2631 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2632 If we find such a factor, we can multiply by t using an algorithm that
2633 multiplies by q, shift the result by m and add/subtract it to itself.
2635 We search for large factors first and loop down, even if large factors
2636 are less probable than small; if we find a large factor we will find a
2637 good sequence quickly, and therefore be able to prune (by decreasing
2638 COST_LIMIT) the search. */
2640 do_alg_addsub_factor
:
2641 for (m
= floor_log2 (t
- 1); m
>= 2; m
--)
2643 unsigned HOST_WIDE_INT d
;
2645 d
= ((unsigned HOST_WIDE_INT
) 1 << m
) + 1;
2646 if (t
% d
== 0 && t
> d
&& m
< maxm
2647 && (!cache_hit
|| cache_alg
== alg_add_factor
))
2649 /* If the target has a cheap shift-and-add instruction use
2650 that in preference to a shift insn followed by an add insn.
2651 Assume that the shift-and-add is "atomic" with a latency
2652 equal to its cost, otherwise assume that on superscalar
2653 hardware the shift may be executed concurrently with the
2654 earlier steps in the algorithm. */
2655 op_cost
= add_cost (speed
, mode
) + shift_cost (speed
, mode
, m
);
2656 if (shiftadd_cost (speed
, mode
, m
) < op_cost
)
2658 op_cost
= shiftadd_cost (speed
, mode
, m
);
2659 op_latency
= op_cost
;
2662 op_latency
= add_cost (speed
, mode
);
2664 new_limit
.cost
= best_cost
.cost
- op_cost
;
2665 new_limit
.latency
= best_cost
.latency
- op_latency
;
2666 synth_mult (alg_in
, t
/ d
, &new_limit
, mode
);
2668 alg_in
->cost
.cost
+= op_cost
;
2669 alg_in
->cost
.latency
+= op_latency
;
2670 if (alg_in
->cost
.latency
< op_cost
)
2671 alg_in
->cost
.latency
= op_cost
;
2672 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2674 struct algorithm
*x
;
2675 best_cost
= alg_in
->cost
;
2676 x
= alg_in
, alg_in
= best_alg
, best_alg
= x
;
2677 best_alg
->log
[best_alg
->ops
] = m
;
2678 best_alg
->op
[best_alg
->ops
] = alg_add_factor
;
2680 /* Other factors will have been taken care of in the recursion. */
2684 d
= ((unsigned HOST_WIDE_INT
) 1 << m
) - 1;
2685 if (t
% d
== 0 && t
> d
&& m
< maxm
2686 && (!cache_hit
|| cache_alg
== alg_sub_factor
))
2688 /* If the target has a cheap shift-and-subtract insn use
2689 that in preference to a shift insn followed by a sub insn.
2690 Assume that the shift-and-sub is "atomic" with a latency
2691 equal to it's cost, otherwise assume that on superscalar
2692 hardware the shift may be executed concurrently with the
2693 earlier steps in the algorithm. */
2694 op_cost
= add_cost (speed
, mode
) + shift_cost (speed
, mode
, m
);
2695 if (shiftsub0_cost (speed
, mode
, m
) < op_cost
)
2697 op_cost
= shiftsub0_cost (speed
, mode
, m
);
2698 op_latency
= op_cost
;
2701 op_latency
= add_cost (speed
, mode
);
2703 new_limit
.cost
= best_cost
.cost
- op_cost
;
2704 new_limit
.latency
= best_cost
.latency
- op_latency
;
2705 synth_mult (alg_in
, t
/ d
, &new_limit
, mode
);
2707 alg_in
->cost
.cost
+= op_cost
;
2708 alg_in
->cost
.latency
+= op_latency
;
2709 if (alg_in
->cost
.latency
< op_cost
)
2710 alg_in
->cost
.latency
= op_cost
;
2711 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2713 struct algorithm
*x
;
2714 best_cost
= alg_in
->cost
;
2715 x
= alg_in
, alg_in
= best_alg
, best_alg
= x
;
2716 best_alg
->log
[best_alg
->ops
] = m
;
2717 best_alg
->op
[best_alg
->ops
] = alg_sub_factor
;
2725 /* Try shift-and-add (load effective address) instructions,
2726 i.e. do a*3, a*5, a*9. */
2733 if (m
>= 0 && m
< maxm
)
2735 op_cost
= shiftadd_cost (speed
, mode
, m
);
2736 new_limit
.cost
= best_cost
.cost
- op_cost
;
2737 new_limit
.latency
= best_cost
.latency
- op_cost
;
2738 synth_mult (alg_in
, (t
- 1) >> m
, &new_limit
, mode
);
2740 alg_in
->cost
.cost
+= op_cost
;
2741 alg_in
->cost
.latency
+= op_cost
;
2742 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2744 struct algorithm
*x
;
2745 best_cost
= alg_in
->cost
;
2746 x
= alg_in
, alg_in
= best_alg
, best_alg
= x
;
2747 best_alg
->log
[best_alg
->ops
] = m
;
2748 best_alg
->op
[best_alg
->ops
] = alg_add_t2_m
;
2758 if (m
>= 0 && m
< maxm
)
2760 op_cost
= shiftsub0_cost (speed
, mode
, m
);
2761 new_limit
.cost
= best_cost
.cost
- op_cost
;
2762 new_limit
.latency
= best_cost
.latency
- op_cost
;
2763 synth_mult (alg_in
, (t
+ 1) >> m
, &new_limit
, mode
);
2765 alg_in
->cost
.cost
+= op_cost
;
2766 alg_in
->cost
.latency
+= op_cost
;
2767 if (CHEAPER_MULT_COST (&alg_in
->cost
, &best_cost
))
2769 struct algorithm
*x
;
2770 best_cost
= alg_in
->cost
;
2771 x
= alg_in
, alg_in
= best_alg
, best_alg
= x
;
2772 best_alg
->log
[best_alg
->ops
] = m
;
2773 best_alg
->op
[best_alg
->ops
] = alg_sub_t2_m
;
2781 /* If best_cost has not decreased, we have not found any algorithm. */
2782 if (!CHEAPER_MULT_COST (&best_cost
, cost_limit
))
2784 /* We failed to find an algorithm. Record alg_impossible for
2785 this case (that is, <T, MODE, COST_LIMIT>) so that next time
2786 we are asked to find an algorithm for T within the same or
2787 lower COST_LIMIT, we can immediately return to the
2790 entry_ptr
->mode
= mode
;
2791 entry_ptr
->speed
= speed
;
2792 entry_ptr
->alg
= alg_impossible
;
2793 entry_ptr
->cost
= *cost_limit
;
2797 /* Cache the result. */
2801 entry_ptr
->mode
= mode
;
2802 entry_ptr
->speed
= speed
;
2803 entry_ptr
->alg
= best_alg
->op
[best_alg
->ops
];
2804 entry_ptr
->cost
.cost
= best_cost
.cost
;
2805 entry_ptr
->cost
.latency
= best_cost
.latency
;
2808 /* If we are getting a too long sequence for `struct algorithm'
2809 to record, make this search fail. */
2810 if (best_alg
->ops
== MAX_BITS_PER_WORD
)
2813 /* Copy the algorithm from temporary space to the space at alg_out.
2814 We avoid using structure assignment because the majority of
2815 best_alg is normally undefined, and this is a critical function. */
2816 alg_out
->ops
= best_alg
->ops
+ 1;
2817 alg_out
->cost
= best_cost
;
2818 memcpy (alg_out
->op
, best_alg
->op
,
2819 alg_out
->ops
* sizeof *alg_out
->op
);
2820 memcpy (alg_out
->log
, best_alg
->log
,
2821 alg_out
->ops
* sizeof *alg_out
->log
);
2824 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2825 Try three variations:
2827 - a shift/add sequence based on VAL itself
2828 - a shift/add sequence based on -VAL, followed by a negation
2829 - a shift/add sequence based on VAL - 1, followed by an addition.
2831 Return true if the cheapest of these cost less than MULT_COST,
2832 describing the algorithm in *ALG and final fixup in *VARIANT. */
2835 choose_mult_variant (enum machine_mode mode
, HOST_WIDE_INT val
,
2836 struct algorithm
*alg
, enum mult_variant
*variant
,
2839 struct algorithm alg2
;
2840 struct mult_cost limit
;
2842 bool speed
= optimize_insn_for_speed_p ();
2844 /* Fail quickly for impossible bounds. */
2848 /* Ensure that mult_cost provides a reasonable upper bound.
2849 Any constant multiplication can be performed with less
2850 than 2 * bits additions. */
2851 op_cost
= 2 * GET_MODE_UNIT_BITSIZE (mode
) * add_cost (speed
, mode
);
2852 if (mult_cost
> op_cost
)
2853 mult_cost
= op_cost
;
2855 *variant
= basic_variant
;
2856 limit
.cost
= mult_cost
;
2857 limit
.latency
= mult_cost
;
2858 synth_mult (alg
, val
, &limit
, mode
);
2860 /* This works only if the inverted value actually fits in an
2862 if (HOST_BITS_PER_INT
>= GET_MODE_UNIT_BITSIZE (mode
))
2864 op_cost
= neg_cost (speed
, mode
);
2865 if (MULT_COST_LESS (&alg
->cost
, mult_cost
))
2867 limit
.cost
= alg
->cost
.cost
- op_cost
;
2868 limit
.latency
= alg
->cost
.latency
- op_cost
;
2872 limit
.cost
= mult_cost
- op_cost
;
2873 limit
.latency
= mult_cost
- op_cost
;
2876 synth_mult (&alg2
, -val
, &limit
, mode
);
2877 alg2
.cost
.cost
+= op_cost
;
2878 alg2
.cost
.latency
+= op_cost
;
2879 if (CHEAPER_MULT_COST (&alg2
.cost
, &alg
->cost
))
2880 *alg
= alg2
, *variant
= negate_variant
;
2883 /* This proves very useful for division-by-constant. */
2884 op_cost
= add_cost (speed
, mode
);
2885 if (MULT_COST_LESS (&alg
->cost
, mult_cost
))
2887 limit
.cost
= alg
->cost
.cost
- op_cost
;
2888 limit
.latency
= alg
->cost
.latency
- op_cost
;
2892 limit
.cost
= mult_cost
- op_cost
;
2893 limit
.latency
= mult_cost
- op_cost
;
2896 synth_mult (&alg2
, val
- 1, &limit
, mode
);
2897 alg2
.cost
.cost
+= op_cost
;
2898 alg2
.cost
.latency
+= op_cost
;
2899 if (CHEAPER_MULT_COST (&alg2
.cost
, &alg
->cost
))
2900 *alg
= alg2
, *variant
= add_variant
;
2902 return MULT_COST_LESS (&alg
->cost
, mult_cost
);
2905 /* A subroutine of expand_mult, used for constant multiplications.
2906 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2907 convenient. Use the shift/add sequence described by ALG and apply
2908 the final fixup specified by VARIANT. */
2911 expand_mult_const (enum machine_mode mode
, rtx op0
, HOST_WIDE_INT val
,
2912 rtx target
, const struct algorithm
*alg
,
2913 enum mult_variant variant
)
2915 HOST_WIDE_INT val_so_far
;
2919 enum machine_mode nmode
;
2921 /* Avoid referencing memory over and over and invalid sharing
2923 op0
= force_reg (mode
, op0
);
2925 /* ACCUM starts out either as OP0 or as a zero, depending on
2926 the first operation. */
2928 if (alg
->op
[0] == alg_zero
)
2930 accum
= copy_to_mode_reg (mode
, CONST0_RTX (mode
));
2933 else if (alg
->op
[0] == alg_m
)
2935 accum
= copy_to_mode_reg (mode
, op0
);
2941 for (opno
= 1; opno
< alg
->ops
; opno
++)
2943 int log
= alg
->log
[opno
];
2944 rtx shift_subtarget
= optimize
? 0 : accum
;
2946 = (opno
== alg
->ops
- 1 && target
!= 0 && variant
!= add_variant
2949 rtx accum_target
= optimize
? 0 : accum
;
2952 switch (alg
->op
[opno
])
2955 tem
= expand_shift (LSHIFT_EXPR
, mode
, accum
, log
, NULL_RTX
, 0);
2956 /* REG_EQUAL note will be attached to the following insn. */
2957 emit_move_insn (accum
, tem
);
2962 tem
= expand_shift (LSHIFT_EXPR
, mode
, op0
, log
, NULL_RTX
, 0);
2963 accum
= force_operand (gen_rtx_PLUS (mode
, accum
, tem
),
2964 add_target
? add_target
: accum_target
);
2965 val_so_far
+= (HOST_WIDE_INT
) 1 << log
;
2969 tem
= expand_shift (LSHIFT_EXPR
, mode
, op0
, log
, NULL_RTX
, 0);
2970 accum
= force_operand (gen_rtx_MINUS (mode
, accum
, tem
),
2971 add_target
? add_target
: accum_target
);
2972 val_so_far
-= (HOST_WIDE_INT
) 1 << log
;
2976 accum
= expand_shift (LSHIFT_EXPR
, mode
, accum
,
2977 log
, shift_subtarget
, 0);
2978 accum
= force_operand (gen_rtx_PLUS (mode
, accum
, op0
),
2979 add_target
? add_target
: accum_target
);
2980 val_so_far
= (val_so_far
<< log
) + 1;
2984 accum
= expand_shift (LSHIFT_EXPR
, mode
, accum
,
2985 log
, shift_subtarget
, 0);
2986 accum
= force_operand (gen_rtx_MINUS (mode
, accum
, op0
),
2987 add_target
? add_target
: accum_target
);
2988 val_so_far
= (val_so_far
<< log
) - 1;
2991 case alg_add_factor
:
2992 tem
= expand_shift (LSHIFT_EXPR
, mode
, accum
, log
, NULL_RTX
, 0);
2993 accum
= force_operand (gen_rtx_PLUS (mode
, accum
, tem
),
2994 add_target
? add_target
: accum_target
);
2995 val_so_far
+= val_so_far
<< log
;
2998 case alg_sub_factor
:
2999 tem
= expand_shift (LSHIFT_EXPR
, mode
, accum
, log
, NULL_RTX
, 0);
3000 accum
= force_operand (gen_rtx_MINUS (mode
, tem
, accum
),
3002 ? add_target
: (optimize
? 0 : tem
)));
3003 val_so_far
= (val_so_far
<< log
) - val_so_far
;
3010 if (SCALAR_INT_MODE_P (mode
))
3012 /* Write a REG_EQUAL note on the last insn so that we can cse
3013 multiplication sequences. Note that if ACCUM is a SUBREG,
3014 we've set the inner register and must properly indicate that. */
3015 tem
= op0
, nmode
= mode
;
3016 accum_inner
= accum
;
3017 if (GET_CODE (accum
) == SUBREG
)
3019 accum_inner
= SUBREG_REG (accum
);
3020 nmode
= GET_MODE (accum_inner
);
3021 tem
= gen_lowpart (nmode
, op0
);
3024 insn
= get_last_insn ();
3025 set_dst_reg_note (insn
, REG_EQUAL
,
3026 gen_rtx_MULT (nmode
, tem
,
3027 gen_int_mode (val_so_far
, nmode
)),
3032 if (variant
== negate_variant
)
3034 val_so_far
= -val_so_far
;
3035 accum
= expand_unop (mode
, neg_optab
, accum
, target
, 0);
3037 else if (variant
== add_variant
)
3039 val_so_far
= val_so_far
+ 1;
3040 accum
= force_operand (gen_rtx_PLUS (mode
, accum
, op0
), target
);
3043 /* Compare only the bits of val and val_so_far that are significant
3044 in the result mode, to avoid sign-/zero-extension confusion. */
3045 nmode
= GET_MODE_INNER (mode
);
3046 if (nmode
== VOIDmode
)
3048 val
&= GET_MODE_MASK (nmode
);
3049 val_so_far
&= GET_MODE_MASK (nmode
);
3050 gcc_assert (val
== val_so_far
);
3055 /* Perform a multiplication and return an rtx for the result.
3056 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3057 TARGET is a suggestion for where to store the result (an rtx).
3059 We check specially for a constant integer as OP1.
3060 If you want this check for OP0 as well, then before calling
3061 you should swap the two operands if OP0 would be constant. */
3064 expand_mult (enum machine_mode mode
, rtx op0
, rtx op1
, rtx target
,
3067 enum mult_variant variant
;
3068 struct algorithm algorithm
;
3071 bool speed
= optimize_insn_for_speed_p ();
3072 bool do_trapv
= flag_trapv
&& SCALAR_INT_MODE_P (mode
) && !unsignedp
;
3074 if (CONSTANT_P (op0
))
3081 /* For vectors, there are several simplifications that can be made if
3082 all elements of the vector constant are identical. */
3084 if (GET_CODE (op1
) == CONST_VECTOR
)
3086 int i
, n
= CONST_VECTOR_NUNITS (op1
);
3087 scalar_op1
= CONST_VECTOR_ELT (op1
, 0);
3088 for (i
= 1; i
< n
; ++i
)
3089 if (!rtx_equal_p (scalar_op1
, CONST_VECTOR_ELT (op1
, i
)))
3093 if (INTEGRAL_MODE_P (mode
))
3096 HOST_WIDE_INT coeff
;
3100 if (op1
== CONST0_RTX (mode
))
3102 if (op1
== CONST1_RTX (mode
))
3104 if (op1
== CONSTM1_RTX (mode
))
3105 return expand_unop (mode
, do_trapv
? negv_optab
: neg_optab
,
3111 /* If mode is integer vector mode, check if the backend supports
3112 vector lshift (by scalar or vector) at all. If not, we can't use
3113 synthetized multiply. */
3114 if (GET_MODE_CLASS (mode
) == MODE_VECTOR_INT
3115 && optab_handler (vashl_optab
, mode
) == CODE_FOR_nothing
3116 && optab_handler (ashl_optab
, mode
) == CODE_FOR_nothing
)
3119 /* These are the operations that are potentially turned into
3120 a sequence of shifts and additions. */
3121 mode_bitsize
= GET_MODE_UNIT_BITSIZE (mode
);
3123 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3124 less than or equal in size to `unsigned int' this doesn't matter.
3125 If the mode is larger than `unsigned int', then synth_mult works
3126 only if the constant value exactly fits in an `unsigned int' without
3127 any truncation. This means that multiplying by negative values does
3128 not work; results are off by 2^32 on a 32 bit machine. */
3129 if (CONST_INT_P (scalar_op1
))
3131 coeff
= INTVAL (scalar_op1
);
3134 #if TARGET_SUPPORTS_WIDE_INT
3135 else if (CONST_WIDE_INT_P (scalar_op1
))
3137 else if (CONST_DOUBLE_AS_INT_P (scalar_op1
))
3140 int shift
= wi::exact_log2 (std::make_pair (scalar_op1
, mode
));
3141 /* Perfect power of 2 (other than 1, which is handled above). */
3143 return expand_shift (LSHIFT_EXPR
, mode
, op0
,
3144 shift
, target
, unsignedp
);
3151 /* We used to test optimize here, on the grounds that it's better to
3152 produce a smaller program when -O is not used. But this causes
3153 such a terrible slowdown sometimes that it seems better to always
3156 /* Special case powers of two. */
3157 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff
)
3158 && !(is_neg
&& mode_bitsize
> HOST_BITS_PER_WIDE_INT
))
3159 return expand_shift (LSHIFT_EXPR
, mode
, op0
,
3160 floor_log2 (coeff
), target
, unsignedp
);
3162 fake_reg
= gen_raw_REG (mode
, LAST_VIRTUAL_REGISTER
+ 1);
3164 /* Attempt to handle multiplication of DImode values by negative
3165 coefficients, by performing the multiplication by a positive
3166 multiplier and then inverting the result. */
3167 if (is_neg
&& mode_bitsize
> HOST_BITS_PER_WIDE_INT
)
3169 /* Its safe to use -coeff even for INT_MIN, as the
3170 result is interpreted as an unsigned coefficient.
3171 Exclude cost of op0 from max_cost to match the cost
3172 calculation of the synth_mult. */
3173 coeff
= -(unsigned HOST_WIDE_INT
) coeff
;
3174 max_cost
= (set_src_cost (gen_rtx_MULT (mode
, fake_reg
, op1
), speed
)
3175 - neg_cost (speed
, mode
));
3179 /* Special case powers of two. */
3180 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff
))
3182 rtx temp
= expand_shift (LSHIFT_EXPR
, mode
, op0
,
3183 floor_log2 (coeff
), target
, unsignedp
);
3184 return expand_unop (mode
, neg_optab
, temp
, target
, 0);
3187 if (choose_mult_variant (mode
, coeff
, &algorithm
, &variant
,
3190 rtx temp
= expand_mult_const (mode
, op0
, coeff
, NULL_RTX
,
3191 &algorithm
, variant
);
3192 return expand_unop (mode
, neg_optab
, temp
, target
, 0);
3197 /* Exclude cost of op0 from max_cost to match the cost
3198 calculation of the synth_mult. */
3199 max_cost
= set_src_cost (gen_rtx_MULT (mode
, fake_reg
, op1
), speed
);
3200 if (choose_mult_variant (mode
, coeff
, &algorithm
, &variant
, max_cost
))
3201 return expand_mult_const (mode
, op0
, coeff
, target
,
3202 &algorithm
, variant
);
3206 /* Expand x*2.0 as x+x. */
3207 if (CONST_DOUBLE_AS_FLOAT_P (scalar_op1
))
3210 REAL_VALUE_FROM_CONST_DOUBLE (d
, scalar_op1
);
3212 if (REAL_VALUES_EQUAL (d
, dconst2
))
3214 op0
= force_reg (GET_MODE (op0
), op0
);
3215 return expand_binop (mode
, add_optab
, op0
, op0
,
3216 target
, unsignedp
, OPTAB_LIB_WIDEN
);
3221 /* This used to use umul_optab if unsigned, but for non-widening multiply
3222 there is no difference between signed and unsigned. */
3223 op0
= expand_binop (mode
, do_trapv
? smulv_optab
: smul_optab
,
3224 op0
, op1
, target
, unsignedp
, OPTAB_LIB_WIDEN
);
3229 /* Return a cost estimate for multiplying a register by the given
3230 COEFFicient in the given MODE and SPEED. */
3233 mult_by_coeff_cost (HOST_WIDE_INT coeff
, enum machine_mode mode
, bool speed
)
3236 struct algorithm algorithm
;
3237 enum mult_variant variant
;
3239 rtx fake_reg
= gen_raw_REG (mode
, LAST_VIRTUAL_REGISTER
+ 1);
3240 max_cost
= set_src_cost (gen_rtx_MULT (mode
, fake_reg
, fake_reg
), speed
);
3241 if (choose_mult_variant (mode
, coeff
, &algorithm
, &variant
, max_cost
))
3242 return algorithm
.cost
.cost
;
3247 /* Perform a widening multiplication and return an rtx for the result.
3248 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3249 TARGET is a suggestion for where to store the result (an rtx).
3250 THIS_OPTAB is the optab we should use, it must be either umul_widen_optab
3251 or smul_widen_optab.
3253 We check specially for a constant integer as OP1, comparing the
3254 cost of a widening multiply against the cost of a sequence of shifts
3258 expand_widening_mult (enum machine_mode mode
, rtx op0
, rtx op1
, rtx target
,
3259 int unsignedp
, optab this_optab
)
3261 bool speed
= optimize_insn_for_speed_p ();
3264 if (CONST_INT_P (op1
)
3265 && GET_MODE (op0
) != VOIDmode
3266 && (cop1
= convert_modes (mode
, GET_MODE (op0
), op1
,
3267 this_optab
== umul_widen_optab
))
3268 && CONST_INT_P (cop1
)
3269 && (INTVAL (cop1
) >= 0
3270 || HWI_COMPUTABLE_MODE_P (mode
)))
3272 HOST_WIDE_INT coeff
= INTVAL (cop1
);
3274 enum mult_variant variant
;
3275 struct algorithm algorithm
;
3277 /* Special case powers of two. */
3278 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff
))
3280 op0
= convert_to_mode (mode
, op0
, this_optab
== umul_widen_optab
);
3281 return expand_shift (LSHIFT_EXPR
, mode
, op0
,
3282 floor_log2 (coeff
), target
, unsignedp
);
3285 /* Exclude cost of op0 from max_cost to match the cost
3286 calculation of the synth_mult. */
3287 max_cost
= mul_widen_cost (speed
, mode
);
3288 if (choose_mult_variant (mode
, coeff
, &algorithm
, &variant
,
3291 op0
= convert_to_mode (mode
, op0
, this_optab
== umul_widen_optab
);
3292 return expand_mult_const (mode
, op0
, coeff
, target
,
3293 &algorithm
, variant
);
3296 return expand_binop (mode
, this_optab
, op0
, op1
, target
,
3297 unsignedp
, OPTAB_LIB_WIDEN
);
3300 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3301 replace division by D, and put the least significant N bits of the result
3302 in *MULTIPLIER_PTR and return the most significant bit.
3304 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3305 needed precision is in PRECISION (should be <= N).
3307 PRECISION should be as small as possible so this function can choose
3308 multiplier more freely.
3310 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3311 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3313 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3314 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3316 unsigned HOST_WIDE_INT
3317 choose_multiplier (unsigned HOST_WIDE_INT d
, int n
, int precision
,
3318 unsigned HOST_WIDE_INT
*multiplier_ptr
,
3319 int *post_shift_ptr
, int *lgup_ptr
)
3321 int lgup
, post_shift
;
3324 /* lgup = ceil(log2(divisor)); */
3325 lgup
= ceil_log2 (d
);
3327 gcc_assert (lgup
<= n
);
3330 pow2
= n
+ lgup
- precision
;
3332 /* mlow = 2^(N + lgup)/d */
3333 wide_int val
= wi::set_bit_in_zero (pow
, HOST_BITS_PER_DOUBLE_INT
);
3334 wide_int mlow
= wi::udiv_trunc (val
, d
);
3336 /* mhigh = (2^(N + lgup) + 2^(N + lgup - precision))/d */
3337 val
|= wi::set_bit_in_zero (pow2
, HOST_BITS_PER_DOUBLE_INT
);
3338 wide_int mhigh
= wi::udiv_trunc (val
, d
);
3340 /* If precision == N, then mlow, mhigh exceed 2^N
3341 (but they do not exceed 2^(N+1)). */
3343 /* Reduce to lowest terms. */
3344 for (post_shift
= lgup
; post_shift
> 0; post_shift
--)
3346 unsigned HOST_WIDE_INT ml_lo
= wi::extract_uhwi (mlow
, 1,
3347 HOST_BITS_PER_WIDE_INT
);
3348 unsigned HOST_WIDE_INT mh_lo
= wi::extract_uhwi (mhigh
, 1,
3349 HOST_BITS_PER_WIDE_INT
);
3353 mlow
= wi::uhwi (ml_lo
, HOST_BITS_PER_DOUBLE_INT
);
3354 mhigh
= wi::uhwi (mh_lo
, HOST_BITS_PER_DOUBLE_INT
);
3357 *post_shift_ptr
= post_shift
;
3359 if (n
< HOST_BITS_PER_WIDE_INT
)
3361 unsigned HOST_WIDE_INT mask
= ((unsigned HOST_WIDE_INT
) 1 << n
) - 1;
3362 *multiplier_ptr
= mhigh
.to_uhwi () & mask
;
3363 return mhigh
.to_uhwi () >= mask
;
3367 *multiplier_ptr
= mhigh
.to_uhwi ();
3368 return wi::extract_uhwi (mhigh
, HOST_BITS_PER_WIDE_INT
, 1);
3372 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3373 congruent to 1 (mod 2**N). */
3375 static unsigned HOST_WIDE_INT
3376 invert_mod2n (unsigned HOST_WIDE_INT x
, int n
)
3378 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3380 /* The algorithm notes that the choice y = x satisfies
3381 x*y == 1 mod 2^3, since x is assumed odd.
3382 Each iteration doubles the number of bits of significance in y. */
3384 unsigned HOST_WIDE_INT mask
;
3385 unsigned HOST_WIDE_INT y
= x
;
3388 mask
= (n
== HOST_BITS_PER_WIDE_INT
3389 ? ~(unsigned HOST_WIDE_INT
) 0
3390 : ((unsigned HOST_WIDE_INT
) 1 << n
) - 1);
3394 y
= y
* (2 - x
*y
) & mask
; /* Modulo 2^N */
3400 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3401 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3402 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3403 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3406 The result is put in TARGET if that is convenient.
3408 MODE is the mode of operation. */
3411 expand_mult_highpart_adjust (enum machine_mode mode
, rtx adj_operand
, rtx op0
,
3412 rtx op1
, rtx target
, int unsignedp
)
3415 enum rtx_code adj_code
= unsignedp
? PLUS
: MINUS
;
3417 tem
= expand_shift (RSHIFT_EXPR
, mode
, op0
,
3418 GET_MODE_BITSIZE (mode
) - 1, NULL_RTX
, 0);
3419 tem
= expand_and (mode
, tem
, op1
, NULL_RTX
);
3421 = force_operand (gen_rtx_fmt_ee (adj_code
, mode
, adj_operand
, tem
),
3424 tem
= expand_shift (RSHIFT_EXPR
, mode
, op1
,
3425 GET_MODE_BITSIZE (mode
) - 1, NULL_RTX
, 0);
3426 tem
= expand_and (mode
, tem
, op0
, NULL_RTX
);
3427 target
= force_operand (gen_rtx_fmt_ee (adj_code
, mode
, adj_operand
, tem
),
3433 /* Subroutine of expmed_mult_highpart. Return the MODE high part of OP. */
3436 extract_high_half (enum machine_mode mode
, rtx op
)
3438 enum machine_mode wider_mode
;
3440 if (mode
== word_mode
)
3441 return gen_highpart (mode
, op
);
3443 gcc_assert (!SCALAR_FLOAT_MODE_P (mode
));
3445 wider_mode
= GET_MODE_WIDER_MODE (mode
);
3446 op
= expand_shift (RSHIFT_EXPR
, wider_mode
, op
,
3447 GET_MODE_BITSIZE (mode
), 0, 1);
3448 return convert_modes (mode
, wider_mode
, op
, 0);
3451 /* Like expmed_mult_highpart, but only consider using a multiplication
3452 optab. OP1 is an rtx for the constant operand. */
3455 expmed_mult_highpart_optab (enum machine_mode mode
, rtx op0
, rtx op1
,
3456 rtx target
, int unsignedp
, int max_cost
)
3458 rtx narrow_op1
= gen_int_mode (INTVAL (op1
), mode
);
3459 enum machine_mode wider_mode
;
3463 bool speed
= optimize_insn_for_speed_p ();
3465 gcc_assert (!SCALAR_FLOAT_MODE_P (mode
));
3467 wider_mode
= GET_MODE_WIDER_MODE (mode
);
3468 size
= GET_MODE_BITSIZE (mode
);
3470 /* Firstly, try using a multiplication insn that only generates the needed
3471 high part of the product, and in the sign flavor of unsignedp. */
3472 if (mul_highpart_cost (speed
, mode
) < max_cost
)
3474 moptab
= unsignedp
? umul_highpart_optab
: smul_highpart_optab
;
3475 tem
= expand_binop (mode
, moptab
, op0
, narrow_op1
, target
,
3476 unsignedp
, OPTAB_DIRECT
);
3481 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3482 Need to adjust the result after the multiplication. */
3483 if (size
- 1 < BITS_PER_WORD
3484 && (mul_highpart_cost (speed
, mode
)
3485 + 2 * shift_cost (speed
, mode
, size
-1)
3486 + 4 * add_cost (speed
, mode
) < max_cost
))
3488 moptab
= unsignedp
? smul_highpart_optab
: umul_highpart_optab
;
3489 tem
= expand_binop (mode
, moptab
, op0
, narrow_op1
, target
,
3490 unsignedp
, OPTAB_DIRECT
);
3492 /* We used the wrong signedness. Adjust the result. */
3493 return expand_mult_highpart_adjust (mode
, tem
, op0
, narrow_op1
,
3497 /* Try widening multiplication. */
3498 moptab
= unsignedp
? umul_widen_optab
: smul_widen_optab
;
3499 if (widening_optab_handler (moptab
, wider_mode
, mode
) != CODE_FOR_nothing
3500 && mul_widen_cost (speed
, wider_mode
) < max_cost
)
3502 tem
= expand_binop (wider_mode
, moptab
, op0
, narrow_op1
, 0,
3503 unsignedp
, OPTAB_WIDEN
);
3505 return extract_high_half (mode
, tem
);
3508 /* Try widening the mode and perform a non-widening multiplication. */
3509 if (optab_handler (smul_optab
, wider_mode
) != CODE_FOR_nothing
3510 && size
- 1 < BITS_PER_WORD
3511 && (mul_cost (speed
, wider_mode
) + shift_cost (speed
, mode
, size
-1)
3517 /* We need to widen the operands, for example to ensure the
3518 constant multiplier is correctly sign or zero extended.
3519 Use a sequence to clean-up any instructions emitted by
3520 the conversions if things don't work out. */
3522 wop0
= convert_modes (wider_mode
, mode
, op0
, unsignedp
);
3523 wop1
= convert_modes (wider_mode
, mode
, op1
, unsignedp
);
3524 tem
= expand_binop (wider_mode
, smul_optab
, wop0
, wop1
, 0,
3525 unsignedp
, OPTAB_WIDEN
);
3526 insns
= get_insns ();
3532 return extract_high_half (mode
, tem
);
3536 /* Try widening multiplication of opposite signedness, and adjust. */
3537 moptab
= unsignedp
? smul_widen_optab
: umul_widen_optab
;
3538 if (widening_optab_handler (moptab
, wider_mode
, mode
) != CODE_FOR_nothing
3539 && size
- 1 < BITS_PER_WORD
3540 && (mul_widen_cost (speed
, wider_mode
)
3541 + 2 * shift_cost (speed
, mode
, size
-1)
3542 + 4 * add_cost (speed
, mode
) < max_cost
))
3544 tem
= expand_binop (wider_mode
, moptab
, op0
, narrow_op1
,
3545 NULL_RTX
, ! unsignedp
, OPTAB_WIDEN
);
3548 tem
= extract_high_half (mode
, tem
);
3549 /* We used the wrong signedness. Adjust the result. */
3550 return expand_mult_highpart_adjust (mode
, tem
, op0
, narrow_op1
,
3558 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3559 putting the high half of the result in TARGET if that is convenient,
3560 and return where the result is. If the operation can not be performed,
3563 MODE is the mode of operation and result.
3565 UNSIGNEDP nonzero means unsigned multiply.
3567 MAX_COST is the total allowed cost for the expanded RTL. */
3570 expmed_mult_highpart (enum machine_mode mode
, rtx op0
, rtx op1
,
3571 rtx target
, int unsignedp
, int max_cost
)
3573 enum machine_mode wider_mode
= GET_MODE_WIDER_MODE (mode
);
3574 unsigned HOST_WIDE_INT cnst1
;
3576 bool sign_adjust
= false;
3577 enum mult_variant variant
;
3578 struct algorithm alg
;
3580 bool speed
= optimize_insn_for_speed_p ();
3582 gcc_assert (!SCALAR_FLOAT_MODE_P (mode
));
3583 /* We can't support modes wider than HOST_BITS_PER_INT. */
3584 gcc_assert (HWI_COMPUTABLE_MODE_P (mode
));
3586 cnst1
= INTVAL (op1
) & GET_MODE_MASK (mode
);
3588 /* We can't optimize modes wider than BITS_PER_WORD.
3589 ??? We might be able to perform double-word arithmetic if
3590 mode == word_mode, however all the cost calculations in
3591 synth_mult etc. assume single-word operations. */
3592 if (GET_MODE_BITSIZE (wider_mode
) > BITS_PER_WORD
)
3593 return expmed_mult_highpart_optab (mode
, op0
, op1
, target
,
3594 unsignedp
, max_cost
);
3596 extra_cost
= shift_cost (speed
, mode
, GET_MODE_BITSIZE (mode
) - 1);
3598 /* Check whether we try to multiply by a negative constant. */
3599 if (!unsignedp
&& ((cnst1
>> (GET_MODE_BITSIZE (mode
) - 1)) & 1))
3602 extra_cost
+= add_cost (speed
, mode
);
3605 /* See whether shift/add multiplication is cheap enough. */
3606 if (choose_mult_variant (wider_mode
, cnst1
, &alg
, &variant
,
3607 max_cost
- extra_cost
))
3609 /* See whether the specialized multiplication optabs are
3610 cheaper than the shift/add version. */
3611 tem
= expmed_mult_highpart_optab (mode
, op0
, op1
, target
, unsignedp
,
3612 alg
.cost
.cost
+ extra_cost
);
3616 tem
= convert_to_mode (wider_mode
, op0
, unsignedp
);
3617 tem
= expand_mult_const (wider_mode
, tem
, cnst1
, 0, &alg
, variant
);
3618 tem
= extract_high_half (mode
, tem
);
3620 /* Adjust result for signedness. */
3622 tem
= force_operand (gen_rtx_MINUS (mode
, tem
, op0
), tem
);
3626 return expmed_mult_highpart_optab (mode
, op0
, op1
, target
,
3627 unsignedp
, max_cost
);
3631 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3634 expand_smod_pow2 (enum machine_mode mode
, rtx op0
, HOST_WIDE_INT d
)
3636 rtx result
, temp
, shift
;
3637 rtx_code_label
*label
;
3639 int prec
= GET_MODE_PRECISION (mode
);
3641 logd
= floor_log2 (d
);
3642 result
= gen_reg_rtx (mode
);
3644 /* Avoid conditional branches when they're expensive. */
3645 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2
3646 && optimize_insn_for_speed_p ())
3648 rtx signmask
= emit_store_flag (result
, LT
, op0
, const0_rtx
,
3652 HOST_WIDE_INT masklow
= ((HOST_WIDE_INT
) 1 << logd
) - 1;
3653 signmask
= force_reg (mode
, signmask
);
3654 shift
= GEN_INT (GET_MODE_BITSIZE (mode
) - logd
);
3656 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3657 which instruction sequence to use. If logical right shifts
3658 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3659 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3661 temp
= gen_rtx_LSHIFTRT (mode
, result
, shift
);
3662 if (optab_handler (lshr_optab
, mode
) == CODE_FOR_nothing
3663 || (set_src_cost (temp
, optimize_insn_for_speed_p ())
3664 > COSTS_N_INSNS (2)))
3666 temp
= expand_binop (mode
, xor_optab
, op0
, signmask
,
3667 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3668 temp
= expand_binop (mode
, sub_optab
, temp
, signmask
,
3669 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3670 temp
= expand_binop (mode
, and_optab
, temp
,
3671 gen_int_mode (masklow
, mode
),
3672 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3673 temp
= expand_binop (mode
, xor_optab
, temp
, signmask
,
3674 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3675 temp
= expand_binop (mode
, sub_optab
, temp
, signmask
,
3676 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3680 signmask
= expand_binop (mode
, lshr_optab
, signmask
, shift
,
3681 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3682 signmask
= force_reg (mode
, signmask
);
3684 temp
= expand_binop (mode
, add_optab
, op0
, signmask
,
3685 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3686 temp
= expand_binop (mode
, and_optab
, temp
,
3687 gen_int_mode (masklow
, mode
),
3688 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3689 temp
= expand_binop (mode
, sub_optab
, temp
, signmask
,
3690 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3696 /* Mask contains the mode's signbit and the significant bits of the
3697 modulus. By including the signbit in the operation, many targets
3698 can avoid an explicit compare operation in the following comparison
3700 wide_int mask
= wi::mask (logd
, false, prec
);
3701 mask
= wi::set_bit (mask
, prec
- 1);
3703 temp
= expand_binop (mode
, and_optab
, op0
,
3704 immed_wide_int_const (mask
, mode
),
3705 result
, 1, OPTAB_LIB_WIDEN
);
3707 emit_move_insn (result
, temp
);
3709 label
= gen_label_rtx ();
3710 do_cmp_and_jump (result
, const0_rtx
, GE
, mode
, label
);
3712 temp
= expand_binop (mode
, sub_optab
, result
, const1_rtx
, result
,
3713 0, OPTAB_LIB_WIDEN
);
3715 mask
= wi::mask (logd
, true, prec
);
3716 temp
= expand_binop (mode
, ior_optab
, temp
,
3717 immed_wide_int_const (mask
, mode
),
3718 result
, 1, OPTAB_LIB_WIDEN
);
3719 temp
= expand_binop (mode
, add_optab
, temp
, const1_rtx
, result
,
3720 0, OPTAB_LIB_WIDEN
);
3722 emit_move_insn (result
, temp
);
3727 /* Expand signed division of OP0 by a power of two D in mode MODE.
3728 This routine is only called for positive values of D. */
3731 expand_sdiv_pow2 (enum machine_mode mode
, rtx op0
, HOST_WIDE_INT d
)
3734 rtx_code_label
*label
;
3737 logd
= floor_log2 (d
);
3740 && BRANCH_COST (optimize_insn_for_speed_p (),
3743 temp
= gen_reg_rtx (mode
);
3744 temp
= emit_store_flag (temp
, LT
, op0
, const0_rtx
, mode
, 0, 1);
3745 temp
= expand_binop (mode
, add_optab
, temp
, op0
, NULL_RTX
,
3746 0, OPTAB_LIB_WIDEN
);
3747 return expand_shift (RSHIFT_EXPR
, mode
, temp
, logd
, NULL_RTX
, 0);
3750 #ifdef HAVE_conditional_move
3751 if (BRANCH_COST (optimize_insn_for_speed_p (), false)
3757 temp2
= copy_to_mode_reg (mode
, op0
);
3758 temp
= expand_binop (mode
, add_optab
, temp2
, gen_int_mode (d
- 1, mode
),
3759 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
3760 temp
= force_reg (mode
, temp
);
3762 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3763 temp2
= emit_conditional_move (temp2
, LT
, temp2
, const0_rtx
,
3764 mode
, temp
, temp2
, mode
, 0);
3767 rtx_insn
*seq
= get_insns ();
3770 return expand_shift (RSHIFT_EXPR
, mode
, temp2
, logd
, NULL_RTX
, 0);
3776 if (BRANCH_COST (optimize_insn_for_speed_p (),
3779 int ushift
= GET_MODE_BITSIZE (mode
) - logd
;
3781 temp
= gen_reg_rtx (mode
);
3782 temp
= emit_store_flag (temp
, LT
, op0
, const0_rtx
, mode
, 0, -1);
3783 if (GET_MODE_BITSIZE (mode
) >= BITS_PER_WORD
3784 || shift_cost (optimize_insn_for_speed_p (), mode
, ushift
)
3785 > COSTS_N_INSNS (1))
3786 temp
= expand_binop (mode
, and_optab
, temp
, gen_int_mode (d
- 1, mode
),
3787 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
3789 temp
= expand_shift (RSHIFT_EXPR
, mode
, temp
,
3790 ushift
, NULL_RTX
, 1);
3791 temp
= expand_binop (mode
, add_optab
, temp
, op0
, NULL_RTX
,
3792 0, OPTAB_LIB_WIDEN
);
3793 return expand_shift (RSHIFT_EXPR
, mode
, temp
, logd
, NULL_RTX
, 0);
3796 label
= gen_label_rtx ();
3797 temp
= copy_to_mode_reg (mode
, op0
);
3798 do_cmp_and_jump (temp
, const0_rtx
, GE
, mode
, label
);
3799 expand_inc (temp
, gen_int_mode (d
- 1, mode
));
3801 return expand_shift (RSHIFT_EXPR
, mode
, temp
, logd
, NULL_RTX
, 0);
3804 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3805 if that is convenient, and returning where the result is.
3806 You may request either the quotient or the remainder as the result;
3807 specify REM_FLAG nonzero to get the remainder.
3809 CODE is the expression code for which kind of division this is;
3810 it controls how rounding is done. MODE is the machine mode to use.
3811 UNSIGNEDP nonzero means do unsigned division. */
3813 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3814 and then correct it by or'ing in missing high bits
3815 if result of ANDI is nonzero.
3816 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3817 This could optimize to a bfexts instruction.
3818 But C doesn't use these operations, so their optimizations are
3820 /* ??? For modulo, we don't actually need the highpart of the first product,
3821 the low part will do nicely. And for small divisors, the second multiply
3822 can also be a low-part only multiply or even be completely left out.
3823 E.g. to calculate the remainder of a division by 3 with a 32 bit
3824 multiply, multiply with 0x55555556 and extract the upper two bits;
3825 the result is exact for inputs up to 0x1fffffff.
3826 The input range can be reduced by using cross-sum rules.
3827 For odd divisors >= 3, the following table gives right shift counts
3828 so that if a number is shifted by an integer multiple of the given
3829 amount, the remainder stays the same:
3830 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3831 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3832 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3833 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3834 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3836 Cross-sum rules for even numbers can be derived by leaving as many bits
3837 to the right alone as the divisor has zeros to the right.
3838 E.g. if x is an unsigned 32 bit number:
3839 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3843 expand_divmod (int rem_flag
, enum tree_code code
, enum machine_mode mode
,
3844 rtx op0
, rtx op1
, rtx target
, int unsignedp
)
3846 enum machine_mode compute_mode
;
3848 rtx quotient
= 0, remainder
= 0;
3852 optab optab1
, optab2
;
3853 int op1_is_constant
, op1_is_pow2
= 0;
3854 int max_cost
, extra_cost
;
3855 static HOST_WIDE_INT last_div_const
= 0;
3856 bool speed
= optimize_insn_for_speed_p ();
3858 op1_is_constant
= CONST_INT_P (op1
);
3859 if (op1_is_constant
)
3861 unsigned HOST_WIDE_INT ext_op1
= UINTVAL (op1
);
3863 ext_op1
&= GET_MODE_MASK (mode
);
3864 op1_is_pow2
= ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1
)
3865 || (! unsignedp
&& EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1
))));
3869 This is the structure of expand_divmod:
3871 First comes code to fix up the operands so we can perform the operations
3872 correctly and efficiently.
3874 Second comes a switch statement with code specific for each rounding mode.
3875 For some special operands this code emits all RTL for the desired
3876 operation, for other cases, it generates only a quotient and stores it in
3877 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3878 to indicate that it has not done anything.
3880 Last comes code that finishes the operation. If QUOTIENT is set and
3881 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3882 QUOTIENT is not set, it is computed using trunc rounding.
3884 We try to generate special code for division and remainder when OP1 is a
3885 constant. If |OP1| = 2**n we can use shifts and some other fast
3886 operations. For other values of OP1, we compute a carefully selected
3887 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3890 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3891 half of the product. Different strategies for generating the product are
3892 implemented in expmed_mult_highpart.
3894 If what we actually want is the remainder, we generate that by another
3895 by-constant multiplication and a subtraction. */
3897 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3898 code below will malfunction if we are, so check here and handle
3899 the special case if so. */
3900 if (op1
== const1_rtx
)
3901 return rem_flag
? const0_rtx
: op0
;
3903 /* When dividing by -1, we could get an overflow.
3904 negv_optab can handle overflows. */
3905 if (! unsignedp
&& op1
== constm1_rtx
)
3909 return expand_unop (mode
, flag_trapv
&& GET_MODE_CLASS (mode
) == MODE_INT
3910 ? negv_optab
: neg_optab
, op0
, target
, 0);
3914 /* Don't use the function value register as a target
3915 since we have to read it as well as write it,
3916 and function-inlining gets confused by this. */
3917 && ((REG_P (target
) && REG_FUNCTION_VALUE_P (target
))
3918 /* Don't clobber an operand while doing a multi-step calculation. */
3919 || ((rem_flag
|| op1_is_constant
)
3920 && (reg_mentioned_p (target
, op0
)
3921 || (MEM_P (op0
) && MEM_P (target
))))
3922 || reg_mentioned_p (target
, op1
)
3923 || (MEM_P (op1
) && MEM_P (target
))))
3926 /* Get the mode in which to perform this computation. Normally it will
3927 be MODE, but sometimes we can't do the desired operation in MODE.
3928 If so, pick a wider mode in which we can do the operation. Convert
3929 to that mode at the start to avoid repeated conversions.
3931 First see what operations we need. These depend on the expression
3932 we are evaluating. (We assume that divxx3 insns exist under the
3933 same conditions that modxx3 insns and that these insns don't normally
3934 fail. If these assumptions are not correct, we may generate less
3935 efficient code in some cases.)
3937 Then see if we find a mode in which we can open-code that operation
3938 (either a division, modulus, or shift). Finally, check for the smallest
3939 mode for which we can do the operation with a library call. */
3941 /* We might want to refine this now that we have division-by-constant
3942 optimization. Since expmed_mult_highpart tries so many variants, it is
3943 not straightforward to generalize this. Maybe we should make an array
3944 of possible modes in init_expmed? Save this for GCC 2.7. */
3946 optab1
= ((op1_is_pow2
&& op1
!= const0_rtx
)
3947 ? (unsignedp
? lshr_optab
: ashr_optab
)
3948 : (unsignedp
? udiv_optab
: sdiv_optab
));
3949 optab2
= ((op1_is_pow2
&& op1
!= const0_rtx
)
3951 : (unsignedp
? udivmod_optab
: sdivmod_optab
));
3953 for (compute_mode
= mode
; compute_mode
!= VOIDmode
;
3954 compute_mode
= GET_MODE_WIDER_MODE (compute_mode
))
3955 if (optab_handler (optab1
, compute_mode
) != CODE_FOR_nothing
3956 || optab_handler (optab2
, compute_mode
) != CODE_FOR_nothing
)
3959 if (compute_mode
== VOIDmode
)
3960 for (compute_mode
= mode
; compute_mode
!= VOIDmode
;
3961 compute_mode
= GET_MODE_WIDER_MODE (compute_mode
))
3962 if (optab_libfunc (optab1
, compute_mode
)
3963 || optab_libfunc (optab2
, compute_mode
))
3966 /* If we still couldn't find a mode, use MODE, but expand_binop will
3968 if (compute_mode
== VOIDmode
)
3969 compute_mode
= mode
;
3971 if (target
&& GET_MODE (target
) == compute_mode
)
3974 tquotient
= gen_reg_rtx (compute_mode
);
3976 size
= GET_MODE_BITSIZE (compute_mode
);
3978 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3979 (mode), and thereby get better code when OP1 is a constant. Do that
3980 later. It will require going over all usages of SIZE below. */
3981 size
= GET_MODE_BITSIZE (mode
);
3984 /* Only deduct something for a REM if the last divide done was
3985 for a different constant. Then set the constant of the last
3987 max_cost
= (unsignedp
3988 ? udiv_cost (speed
, compute_mode
)
3989 : sdiv_cost (speed
, compute_mode
));
3990 if (rem_flag
&& ! (last_div_const
!= 0 && op1_is_constant
3991 && INTVAL (op1
) == last_div_const
))
3992 max_cost
-= (mul_cost (speed
, compute_mode
)
3993 + add_cost (speed
, compute_mode
));
3995 last_div_const
= ! rem_flag
&& op1_is_constant
? INTVAL (op1
) : 0;
3997 /* Now convert to the best mode to use. */
3998 if (compute_mode
!= mode
)
4000 op0
= convert_modes (compute_mode
, mode
, op0
, unsignedp
);
4001 op1
= convert_modes (compute_mode
, mode
, op1
, unsignedp
);
4003 /* convert_modes may have placed op1 into a register, so we
4004 must recompute the following. */
4005 op1_is_constant
= CONST_INT_P (op1
);
4006 op1_is_pow2
= (op1_is_constant
4007 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1
))
4009 && EXACT_POWER_OF_2_OR_ZERO_P (-UINTVAL (op1
))))));
4012 /* If one of the operands is a volatile MEM, copy it into a register. */
4014 if (MEM_P (op0
) && MEM_VOLATILE_P (op0
))
4015 op0
= force_reg (compute_mode
, op0
);
4016 if (MEM_P (op1
) && MEM_VOLATILE_P (op1
))
4017 op1
= force_reg (compute_mode
, op1
);
4019 /* If we need the remainder or if OP1 is constant, we need to
4020 put OP0 in a register in case it has any queued subexpressions. */
4021 if (rem_flag
|| op1_is_constant
)
4022 op0
= force_reg (compute_mode
, op0
);
4024 last
= get_last_insn ();
4026 /* Promote floor rounding to trunc rounding for unsigned operations. */
4029 if (code
== FLOOR_DIV_EXPR
)
4030 code
= TRUNC_DIV_EXPR
;
4031 if (code
== FLOOR_MOD_EXPR
)
4032 code
= TRUNC_MOD_EXPR
;
4033 if (code
== EXACT_DIV_EXPR
&& op1_is_pow2
)
4034 code
= TRUNC_DIV_EXPR
;
4037 if (op1
!= const0_rtx
)
4040 case TRUNC_MOD_EXPR
:
4041 case TRUNC_DIV_EXPR
:
4042 if (op1_is_constant
)
4046 unsigned HOST_WIDE_INT mh
, ml
;
4047 int pre_shift
, post_shift
;
4049 unsigned HOST_WIDE_INT d
= (INTVAL (op1
)
4050 & GET_MODE_MASK (compute_mode
));
4052 if (EXACT_POWER_OF_2_OR_ZERO_P (d
))
4054 pre_shift
= floor_log2 (d
);
4057 unsigned HOST_WIDE_INT mask
4058 = ((unsigned HOST_WIDE_INT
) 1 << pre_shift
) - 1;
4060 = expand_binop (compute_mode
, and_optab
, op0
,
4061 gen_int_mode (mask
, compute_mode
),
4065 return gen_lowpart (mode
, remainder
);
4067 quotient
= expand_shift (RSHIFT_EXPR
, compute_mode
, op0
,
4068 pre_shift
, tquotient
, 1);
4070 else if (size
<= HOST_BITS_PER_WIDE_INT
)
4072 if (d
>= ((unsigned HOST_WIDE_INT
) 1 << (size
- 1)))
4074 /* Most significant bit of divisor is set; emit an scc
4076 quotient
= emit_store_flag_force (tquotient
, GEU
, op0
, op1
,
4077 compute_mode
, 1, 1);
4081 /* Find a suitable multiplier and right shift count
4082 instead of multiplying with D. */
4084 mh
= choose_multiplier (d
, size
, size
,
4085 &ml
, &post_shift
, &dummy
);
4087 /* If the suggested multiplier is more than SIZE bits,
4088 we can do better for even divisors, using an
4089 initial right shift. */
4090 if (mh
!= 0 && (d
& 1) == 0)
4092 pre_shift
= floor_log2 (d
& -d
);
4093 mh
= choose_multiplier (d
>> pre_shift
, size
,
4095 &ml
, &post_shift
, &dummy
);
4105 if (post_shift
- 1 >= BITS_PER_WORD
)
4109 = (shift_cost (speed
, compute_mode
, post_shift
- 1)
4110 + shift_cost (speed
, compute_mode
, 1)
4111 + 2 * add_cost (speed
, compute_mode
));
4112 t1
= expmed_mult_highpart
4114 gen_int_mode (ml
, compute_mode
),
4115 NULL_RTX
, 1, max_cost
- extra_cost
);
4118 t2
= force_operand (gen_rtx_MINUS (compute_mode
,
4121 t3
= expand_shift (RSHIFT_EXPR
, compute_mode
,
4122 t2
, 1, NULL_RTX
, 1);
4123 t4
= force_operand (gen_rtx_PLUS (compute_mode
,
4126 quotient
= expand_shift
4127 (RSHIFT_EXPR
, compute_mode
, t4
,
4128 post_shift
- 1, tquotient
, 1);
4134 if (pre_shift
>= BITS_PER_WORD
4135 || post_shift
>= BITS_PER_WORD
)
4139 (RSHIFT_EXPR
, compute_mode
, op0
,
4140 pre_shift
, NULL_RTX
, 1);
4142 = (shift_cost (speed
, compute_mode
, pre_shift
)
4143 + shift_cost (speed
, compute_mode
, post_shift
));
4144 t2
= expmed_mult_highpart
4146 gen_int_mode (ml
, compute_mode
),
4147 NULL_RTX
, 1, max_cost
- extra_cost
);
4150 quotient
= expand_shift
4151 (RSHIFT_EXPR
, compute_mode
, t2
,
4152 post_shift
, tquotient
, 1);
4156 else /* Too wide mode to use tricky code */
4159 insn
= get_last_insn ();
4161 set_dst_reg_note (insn
, REG_EQUAL
,
4162 gen_rtx_UDIV (compute_mode
, op0
, op1
),
4165 else /* TRUNC_DIV, signed */
4167 unsigned HOST_WIDE_INT ml
;
4168 int lgup
, post_shift
;
4170 HOST_WIDE_INT d
= INTVAL (op1
);
4171 unsigned HOST_WIDE_INT abs_d
;
4173 /* Since d might be INT_MIN, we have to cast to
4174 unsigned HOST_WIDE_INT before negating to avoid
4175 undefined signed overflow. */
4177 ? (unsigned HOST_WIDE_INT
) d
4178 : - (unsigned HOST_WIDE_INT
) d
);
4180 /* n rem d = n rem -d */
4181 if (rem_flag
&& d
< 0)
4184 op1
= gen_int_mode (abs_d
, compute_mode
);
4190 quotient
= expand_unop (compute_mode
, neg_optab
, op0
,
4192 else if (HOST_BITS_PER_WIDE_INT
>= size
4193 && abs_d
== (unsigned HOST_WIDE_INT
) 1 << (size
- 1))
4195 /* This case is not handled correctly below. */
4196 quotient
= emit_store_flag (tquotient
, EQ
, op0
, op1
,
4197 compute_mode
, 1, 1);
4201 else if (EXACT_POWER_OF_2_OR_ZERO_P (d
)
4203 ? smod_pow2_cheap (speed
, compute_mode
)
4204 : sdiv_pow2_cheap (speed
, compute_mode
))
4205 /* We assume that cheap metric is true if the
4206 optab has an expander for this mode. */
4207 && ((optab_handler ((rem_flag
? smod_optab
4210 != CODE_FOR_nothing
)
4211 || (optab_handler (sdivmod_optab
,
4213 != CODE_FOR_nothing
)))
4215 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d
))
4219 remainder
= expand_smod_pow2 (compute_mode
, op0
, d
);
4221 return gen_lowpart (mode
, remainder
);
4224 if (sdiv_pow2_cheap (speed
, compute_mode
)
4225 && ((optab_handler (sdiv_optab
, compute_mode
)
4226 != CODE_FOR_nothing
)
4227 || (optab_handler (sdivmod_optab
, compute_mode
)
4228 != CODE_FOR_nothing
)))
4229 quotient
= expand_divmod (0, TRUNC_DIV_EXPR
,
4231 gen_int_mode (abs_d
,
4235 quotient
= expand_sdiv_pow2 (compute_mode
, op0
, abs_d
);
4237 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4238 negate the quotient. */
4241 insn
= get_last_insn ();
4243 && abs_d
< ((unsigned HOST_WIDE_INT
) 1
4244 << (HOST_BITS_PER_WIDE_INT
- 1)))
4245 set_dst_reg_note (insn
, REG_EQUAL
,
4246 gen_rtx_DIV (compute_mode
, op0
,
4252 quotient
= expand_unop (compute_mode
, neg_optab
,
4253 quotient
, quotient
, 0);
4256 else if (size
<= HOST_BITS_PER_WIDE_INT
)
4258 choose_multiplier (abs_d
, size
, size
- 1,
4259 &ml
, &post_shift
, &lgup
);
4260 if (ml
< (unsigned HOST_WIDE_INT
) 1 << (size
- 1))
4264 if (post_shift
>= BITS_PER_WORD
4265 || size
- 1 >= BITS_PER_WORD
)
4268 extra_cost
= (shift_cost (speed
, compute_mode
, post_shift
)
4269 + shift_cost (speed
, compute_mode
, size
- 1)
4270 + add_cost (speed
, compute_mode
));
4271 t1
= expmed_mult_highpart
4272 (compute_mode
, op0
, gen_int_mode (ml
, compute_mode
),
4273 NULL_RTX
, 0, max_cost
- extra_cost
);
4277 (RSHIFT_EXPR
, compute_mode
, t1
,
4278 post_shift
, NULL_RTX
, 0);
4280 (RSHIFT_EXPR
, compute_mode
, op0
,
4281 size
- 1, NULL_RTX
, 0);
4284 = force_operand (gen_rtx_MINUS (compute_mode
,
4289 = force_operand (gen_rtx_MINUS (compute_mode
,
4297 if (post_shift
>= BITS_PER_WORD
4298 || size
- 1 >= BITS_PER_WORD
)
4301 ml
|= (~(unsigned HOST_WIDE_INT
) 0) << (size
- 1);
4302 mlr
= gen_int_mode (ml
, compute_mode
);
4303 extra_cost
= (shift_cost (speed
, compute_mode
, post_shift
)
4304 + shift_cost (speed
, compute_mode
, size
- 1)
4305 + 2 * add_cost (speed
, compute_mode
));
4306 t1
= expmed_mult_highpart (compute_mode
, op0
, mlr
,
4308 max_cost
- extra_cost
);
4311 t2
= force_operand (gen_rtx_PLUS (compute_mode
,
4315 (RSHIFT_EXPR
, compute_mode
, t2
,
4316 post_shift
, NULL_RTX
, 0);
4318 (RSHIFT_EXPR
, compute_mode
, op0
,
4319 size
- 1, NULL_RTX
, 0);
4322 = force_operand (gen_rtx_MINUS (compute_mode
,
4327 = force_operand (gen_rtx_MINUS (compute_mode
,
4332 else /* Too wide mode to use tricky code */
4335 insn
= get_last_insn ();
4337 set_dst_reg_note (insn
, REG_EQUAL
,
4338 gen_rtx_DIV (compute_mode
, op0
, op1
),
4344 delete_insns_since (last
);
4347 case FLOOR_DIV_EXPR
:
4348 case FLOOR_MOD_EXPR
:
4349 /* We will come here only for signed operations. */
4350 if (op1_is_constant
&& HOST_BITS_PER_WIDE_INT
>= size
)
4352 unsigned HOST_WIDE_INT mh
, ml
;
4353 int pre_shift
, lgup
, post_shift
;
4354 HOST_WIDE_INT d
= INTVAL (op1
);
4358 /* We could just as easily deal with negative constants here,
4359 but it does not seem worth the trouble for GCC 2.6. */
4360 if (EXACT_POWER_OF_2_OR_ZERO_P (d
))
4362 pre_shift
= floor_log2 (d
);
4365 unsigned HOST_WIDE_INT mask
4366 = ((unsigned HOST_WIDE_INT
) 1 << pre_shift
) - 1;
4367 remainder
= expand_binop
4368 (compute_mode
, and_optab
, op0
,
4369 gen_int_mode (mask
, compute_mode
),
4370 remainder
, 0, OPTAB_LIB_WIDEN
);
4372 return gen_lowpart (mode
, remainder
);
4374 quotient
= expand_shift
4375 (RSHIFT_EXPR
, compute_mode
, op0
,
4376 pre_shift
, tquotient
, 0);
4382 mh
= choose_multiplier (d
, size
, size
- 1,
4383 &ml
, &post_shift
, &lgup
);
4386 if (post_shift
< BITS_PER_WORD
4387 && size
- 1 < BITS_PER_WORD
)
4390 (RSHIFT_EXPR
, compute_mode
, op0
,
4391 size
- 1, NULL_RTX
, 0);
4392 t2
= expand_binop (compute_mode
, xor_optab
, op0
, t1
,
4393 NULL_RTX
, 0, OPTAB_WIDEN
);
4394 extra_cost
= (shift_cost (speed
, compute_mode
, post_shift
)
4395 + shift_cost (speed
, compute_mode
, size
- 1)
4396 + 2 * add_cost (speed
, compute_mode
));
4397 t3
= expmed_mult_highpart
4398 (compute_mode
, t2
, gen_int_mode (ml
, compute_mode
),
4399 NULL_RTX
, 1, max_cost
- extra_cost
);
4403 (RSHIFT_EXPR
, compute_mode
, t3
,
4404 post_shift
, NULL_RTX
, 1);
4405 quotient
= expand_binop (compute_mode
, xor_optab
,
4406 t4
, t1
, tquotient
, 0,
4414 rtx nsign
, t1
, t2
, t3
, t4
;
4415 t1
= force_operand (gen_rtx_PLUS (compute_mode
,
4416 op0
, constm1_rtx
), NULL_RTX
);
4417 t2
= expand_binop (compute_mode
, ior_optab
, op0
, t1
, NULL_RTX
,
4419 nsign
= expand_shift
4420 (RSHIFT_EXPR
, compute_mode
, t2
,
4421 size
- 1, NULL_RTX
, 0);
4422 t3
= force_operand (gen_rtx_MINUS (compute_mode
, t1
, nsign
),
4424 t4
= expand_divmod (0, TRUNC_DIV_EXPR
, compute_mode
, t3
, op1
,
4429 t5
= expand_unop (compute_mode
, one_cmpl_optab
, nsign
,
4431 quotient
= force_operand (gen_rtx_PLUS (compute_mode
,
4440 delete_insns_since (last
);
4442 /* Try using an instruction that produces both the quotient and
4443 remainder, using truncation. We can easily compensate the quotient
4444 or remainder to get floor rounding, once we have the remainder.
4445 Notice that we compute also the final remainder value here,
4446 and return the result right away. */
4447 if (target
== 0 || GET_MODE (target
) != compute_mode
)
4448 target
= gen_reg_rtx (compute_mode
);
4453 = REG_P (target
) ? target
: gen_reg_rtx (compute_mode
);
4454 quotient
= gen_reg_rtx (compute_mode
);
4459 = REG_P (target
) ? target
: gen_reg_rtx (compute_mode
);
4460 remainder
= gen_reg_rtx (compute_mode
);
4463 if (expand_twoval_binop (sdivmod_optab
, op0
, op1
,
4464 quotient
, remainder
, 0))
4466 /* This could be computed with a branch-less sequence.
4467 Save that for later. */
4469 rtx_code_label
*label
= gen_label_rtx ();
4470 do_cmp_and_jump (remainder
, const0_rtx
, EQ
, compute_mode
, label
);
4471 tem
= expand_binop (compute_mode
, xor_optab
, op0
, op1
,
4472 NULL_RTX
, 0, OPTAB_WIDEN
);
4473 do_cmp_and_jump (tem
, const0_rtx
, GE
, compute_mode
, label
);
4474 expand_dec (quotient
, const1_rtx
);
4475 expand_inc (remainder
, op1
);
4477 return gen_lowpart (mode
, rem_flag
? remainder
: quotient
);
4480 /* No luck with division elimination or divmod. Have to do it
4481 by conditionally adjusting op0 *and* the result. */
4483 rtx_code_label
*label1
, *label2
, *label3
, *label4
, *label5
;
4487 quotient
= gen_reg_rtx (compute_mode
);
4488 adjusted_op0
= copy_to_mode_reg (compute_mode
, op0
);
4489 label1
= gen_label_rtx ();
4490 label2
= gen_label_rtx ();
4491 label3
= gen_label_rtx ();
4492 label4
= gen_label_rtx ();
4493 label5
= gen_label_rtx ();
4494 do_cmp_and_jump (op1
, const0_rtx
, LT
, compute_mode
, label2
);
4495 do_cmp_and_jump (adjusted_op0
, const0_rtx
, LT
, compute_mode
, label1
);
4496 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4497 quotient
, 0, OPTAB_LIB_WIDEN
);
4498 if (tem
!= quotient
)
4499 emit_move_insn (quotient
, tem
);
4500 emit_jump_insn (gen_jump (label5
));
4502 emit_label (label1
);
4503 expand_inc (adjusted_op0
, const1_rtx
);
4504 emit_jump_insn (gen_jump (label4
));
4506 emit_label (label2
);
4507 do_cmp_and_jump (adjusted_op0
, const0_rtx
, GT
, compute_mode
, label3
);
4508 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4509 quotient
, 0, OPTAB_LIB_WIDEN
);
4510 if (tem
!= quotient
)
4511 emit_move_insn (quotient
, tem
);
4512 emit_jump_insn (gen_jump (label5
));
4514 emit_label (label3
);
4515 expand_dec (adjusted_op0
, const1_rtx
);
4516 emit_label (label4
);
4517 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4518 quotient
, 0, OPTAB_LIB_WIDEN
);
4519 if (tem
!= quotient
)
4520 emit_move_insn (quotient
, tem
);
4521 expand_dec (quotient
, const1_rtx
);
4522 emit_label (label5
);
4530 if (op1_is_constant
&& EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1
)))
4533 unsigned HOST_WIDE_INT d
= INTVAL (op1
);
4534 t1
= expand_shift (RSHIFT_EXPR
, compute_mode
, op0
,
4535 floor_log2 (d
), tquotient
, 1);
4536 t2
= expand_binop (compute_mode
, and_optab
, op0
,
4537 gen_int_mode (d
- 1, compute_mode
),
4538 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4539 t3
= gen_reg_rtx (compute_mode
);
4540 t3
= emit_store_flag (t3
, NE
, t2
, const0_rtx
,
4541 compute_mode
, 1, 1);
4544 rtx_code_label
*lab
;
4545 lab
= gen_label_rtx ();
4546 do_cmp_and_jump (t2
, const0_rtx
, EQ
, compute_mode
, lab
);
4547 expand_inc (t1
, const1_rtx
);
4552 quotient
= force_operand (gen_rtx_PLUS (compute_mode
,
4558 /* Try using an instruction that produces both the quotient and
4559 remainder, using truncation. We can easily compensate the
4560 quotient or remainder to get ceiling rounding, once we have the
4561 remainder. Notice that we compute also the final remainder
4562 value here, and return the result right away. */
4563 if (target
== 0 || GET_MODE (target
) != compute_mode
)
4564 target
= gen_reg_rtx (compute_mode
);
4568 remainder
= (REG_P (target
)
4569 ? target
: gen_reg_rtx (compute_mode
));
4570 quotient
= gen_reg_rtx (compute_mode
);
4574 quotient
= (REG_P (target
)
4575 ? target
: gen_reg_rtx (compute_mode
));
4576 remainder
= gen_reg_rtx (compute_mode
);
4579 if (expand_twoval_binop (udivmod_optab
, op0
, op1
, quotient
,
4582 /* This could be computed with a branch-less sequence.
4583 Save that for later. */
4584 rtx_code_label
*label
= gen_label_rtx ();
4585 do_cmp_and_jump (remainder
, const0_rtx
, EQ
,
4586 compute_mode
, label
);
4587 expand_inc (quotient
, const1_rtx
);
4588 expand_dec (remainder
, op1
);
4590 return gen_lowpart (mode
, rem_flag
? remainder
: quotient
);
4593 /* No luck with division elimination or divmod. Have to do it
4594 by conditionally adjusting op0 *and* the result. */
4596 rtx_code_label
*label1
, *label2
;
4597 rtx adjusted_op0
, tem
;
4599 quotient
= gen_reg_rtx (compute_mode
);
4600 adjusted_op0
= copy_to_mode_reg (compute_mode
, op0
);
4601 label1
= gen_label_rtx ();
4602 label2
= gen_label_rtx ();
4603 do_cmp_and_jump (adjusted_op0
, const0_rtx
, NE
,
4604 compute_mode
, label1
);
4605 emit_move_insn (quotient
, const0_rtx
);
4606 emit_jump_insn (gen_jump (label2
));
4608 emit_label (label1
);
4609 expand_dec (adjusted_op0
, const1_rtx
);
4610 tem
= expand_binop (compute_mode
, udiv_optab
, adjusted_op0
, op1
,
4611 quotient
, 1, OPTAB_LIB_WIDEN
);
4612 if (tem
!= quotient
)
4613 emit_move_insn (quotient
, tem
);
4614 expand_inc (quotient
, const1_rtx
);
4615 emit_label (label2
);
4620 if (op1_is_constant
&& EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1
))
4621 && INTVAL (op1
) >= 0)
4623 /* This is extremely similar to the code for the unsigned case
4624 above. For 2.7 we should merge these variants, but for
4625 2.6.1 I don't want to touch the code for unsigned since that
4626 get used in C. The signed case will only be used by other
4630 unsigned HOST_WIDE_INT d
= INTVAL (op1
);
4631 t1
= expand_shift (RSHIFT_EXPR
, compute_mode
, op0
,
4632 floor_log2 (d
), tquotient
, 0);
4633 t2
= expand_binop (compute_mode
, and_optab
, op0
,
4634 gen_int_mode (d
- 1, compute_mode
),
4635 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4636 t3
= gen_reg_rtx (compute_mode
);
4637 t3
= emit_store_flag (t3
, NE
, t2
, const0_rtx
,
4638 compute_mode
, 1, 1);
4641 rtx_code_label
*lab
;
4642 lab
= gen_label_rtx ();
4643 do_cmp_and_jump (t2
, const0_rtx
, EQ
, compute_mode
, lab
);
4644 expand_inc (t1
, const1_rtx
);
4649 quotient
= force_operand (gen_rtx_PLUS (compute_mode
,
4655 /* Try using an instruction that produces both the quotient and
4656 remainder, using truncation. We can easily compensate the
4657 quotient or remainder to get ceiling rounding, once we have the
4658 remainder. Notice that we compute also the final remainder
4659 value here, and return the result right away. */
4660 if (target
== 0 || GET_MODE (target
) != compute_mode
)
4661 target
= gen_reg_rtx (compute_mode
);
4664 remainder
= (REG_P (target
)
4665 ? target
: gen_reg_rtx (compute_mode
));
4666 quotient
= gen_reg_rtx (compute_mode
);
4670 quotient
= (REG_P (target
)
4671 ? target
: gen_reg_rtx (compute_mode
));
4672 remainder
= gen_reg_rtx (compute_mode
);
4675 if (expand_twoval_binop (sdivmod_optab
, op0
, op1
, quotient
,
4678 /* This could be computed with a branch-less sequence.
4679 Save that for later. */
4681 rtx_code_label
*label
= gen_label_rtx ();
4682 do_cmp_and_jump (remainder
, const0_rtx
, EQ
,
4683 compute_mode
, label
);
4684 tem
= expand_binop (compute_mode
, xor_optab
, op0
, op1
,
4685 NULL_RTX
, 0, OPTAB_WIDEN
);
4686 do_cmp_and_jump (tem
, const0_rtx
, LT
, compute_mode
, label
);
4687 expand_inc (quotient
, const1_rtx
);
4688 expand_dec (remainder
, op1
);
4690 return gen_lowpart (mode
, rem_flag
? remainder
: quotient
);
4693 /* No luck with division elimination or divmod. Have to do it
4694 by conditionally adjusting op0 *and* the result. */
4696 rtx_code_label
*label1
, *label2
, *label3
, *label4
, *label5
;
4700 quotient
= gen_reg_rtx (compute_mode
);
4701 adjusted_op0
= copy_to_mode_reg (compute_mode
, op0
);
4702 label1
= gen_label_rtx ();
4703 label2
= gen_label_rtx ();
4704 label3
= gen_label_rtx ();
4705 label4
= gen_label_rtx ();
4706 label5
= gen_label_rtx ();
4707 do_cmp_and_jump (op1
, const0_rtx
, LT
, compute_mode
, label2
);
4708 do_cmp_and_jump (adjusted_op0
, const0_rtx
, GT
,
4709 compute_mode
, label1
);
4710 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4711 quotient
, 0, OPTAB_LIB_WIDEN
);
4712 if (tem
!= quotient
)
4713 emit_move_insn (quotient
, tem
);
4714 emit_jump_insn (gen_jump (label5
));
4716 emit_label (label1
);
4717 expand_dec (adjusted_op0
, const1_rtx
);
4718 emit_jump_insn (gen_jump (label4
));
4720 emit_label (label2
);
4721 do_cmp_and_jump (adjusted_op0
, const0_rtx
, LT
,
4722 compute_mode
, label3
);
4723 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4724 quotient
, 0, OPTAB_LIB_WIDEN
);
4725 if (tem
!= quotient
)
4726 emit_move_insn (quotient
, tem
);
4727 emit_jump_insn (gen_jump (label5
));
4729 emit_label (label3
);
4730 expand_inc (adjusted_op0
, const1_rtx
);
4731 emit_label (label4
);
4732 tem
= expand_binop (compute_mode
, sdiv_optab
, adjusted_op0
, op1
,
4733 quotient
, 0, OPTAB_LIB_WIDEN
);
4734 if (tem
!= quotient
)
4735 emit_move_insn (quotient
, tem
);
4736 expand_inc (quotient
, const1_rtx
);
4737 emit_label (label5
);
4742 case EXACT_DIV_EXPR
:
4743 if (op1_is_constant
&& HOST_BITS_PER_WIDE_INT
>= size
)
4745 HOST_WIDE_INT d
= INTVAL (op1
);
4746 unsigned HOST_WIDE_INT ml
;
4750 pre_shift
= floor_log2 (d
& -d
);
4751 ml
= invert_mod2n (d
>> pre_shift
, size
);
4752 t1
= expand_shift (RSHIFT_EXPR
, compute_mode
, op0
,
4753 pre_shift
, NULL_RTX
, unsignedp
);
4754 quotient
= expand_mult (compute_mode
, t1
,
4755 gen_int_mode (ml
, compute_mode
),
4758 insn
= get_last_insn ();
4759 set_dst_reg_note (insn
, REG_EQUAL
,
4760 gen_rtx_fmt_ee (unsignedp
? UDIV
: DIV
,
4761 compute_mode
, op0
, op1
),
4766 case ROUND_DIV_EXPR
:
4767 case ROUND_MOD_EXPR
:
4771 rtx_code_label
*label
;
4772 label
= gen_label_rtx ();
4773 quotient
= gen_reg_rtx (compute_mode
);
4774 remainder
= gen_reg_rtx (compute_mode
);
4775 if (expand_twoval_binop (udivmod_optab
, op0
, op1
, quotient
, remainder
, 1) == 0)
4778 quotient
= expand_binop (compute_mode
, udiv_optab
, op0
, op1
,
4779 quotient
, 1, OPTAB_LIB_WIDEN
);
4780 tem
= expand_mult (compute_mode
, quotient
, op1
, NULL_RTX
, 1);
4781 remainder
= expand_binop (compute_mode
, sub_optab
, op0
, tem
,
4782 remainder
, 1, OPTAB_LIB_WIDEN
);
4784 tem
= plus_constant (compute_mode
, op1
, -1);
4785 tem
= expand_shift (RSHIFT_EXPR
, compute_mode
, tem
, 1, NULL_RTX
, 1);
4786 do_cmp_and_jump (remainder
, tem
, LEU
, compute_mode
, label
);
4787 expand_inc (quotient
, const1_rtx
);
4788 expand_dec (remainder
, op1
);
4793 rtx abs_rem
, abs_op1
, tem
, mask
;
4794 rtx_code_label
*label
;
4795 label
= gen_label_rtx ();
4796 quotient
= gen_reg_rtx (compute_mode
);
4797 remainder
= gen_reg_rtx (compute_mode
);
4798 if (expand_twoval_binop (sdivmod_optab
, op0
, op1
, quotient
, remainder
, 0) == 0)
4801 quotient
= expand_binop (compute_mode
, sdiv_optab
, op0
, op1
,
4802 quotient
, 0, OPTAB_LIB_WIDEN
);
4803 tem
= expand_mult (compute_mode
, quotient
, op1
, NULL_RTX
, 0);
4804 remainder
= expand_binop (compute_mode
, sub_optab
, op0
, tem
,
4805 remainder
, 0, OPTAB_LIB_WIDEN
);
4807 abs_rem
= expand_abs (compute_mode
, remainder
, NULL_RTX
, 1, 0);
4808 abs_op1
= expand_abs (compute_mode
, op1
, NULL_RTX
, 1, 0);
4809 tem
= expand_shift (LSHIFT_EXPR
, compute_mode
, abs_rem
,
4811 do_cmp_and_jump (tem
, abs_op1
, LTU
, compute_mode
, label
);
4812 tem
= expand_binop (compute_mode
, xor_optab
, op0
, op1
,
4813 NULL_RTX
, 0, OPTAB_WIDEN
);
4814 mask
= expand_shift (RSHIFT_EXPR
, compute_mode
, tem
,
4815 size
- 1, NULL_RTX
, 0);
4816 tem
= expand_binop (compute_mode
, xor_optab
, mask
, const1_rtx
,
4817 NULL_RTX
, 0, OPTAB_WIDEN
);
4818 tem
= expand_binop (compute_mode
, sub_optab
, tem
, mask
,
4819 NULL_RTX
, 0, OPTAB_WIDEN
);
4820 expand_inc (quotient
, tem
);
4821 tem
= expand_binop (compute_mode
, xor_optab
, mask
, op1
,
4822 NULL_RTX
, 0, OPTAB_WIDEN
);
4823 tem
= expand_binop (compute_mode
, sub_optab
, tem
, mask
,
4824 NULL_RTX
, 0, OPTAB_WIDEN
);
4825 expand_dec (remainder
, tem
);
4828 return gen_lowpart (mode
, rem_flag
? remainder
: quotient
);
4836 if (target
&& GET_MODE (target
) != compute_mode
)
4841 /* Try to produce the remainder without producing the quotient.
4842 If we seem to have a divmod pattern that does not require widening,
4843 don't try widening here. We should really have a WIDEN argument
4844 to expand_twoval_binop, since what we'd really like to do here is
4845 1) try a mod insn in compute_mode
4846 2) try a divmod insn in compute_mode
4847 3) try a div insn in compute_mode and multiply-subtract to get
4849 4) try the same things with widening allowed. */
4851 = sign_expand_binop (compute_mode
, umod_optab
, smod_optab
,
4854 ((optab_handler (optab2
, compute_mode
)
4855 != CODE_FOR_nothing
)
4856 ? OPTAB_DIRECT
: OPTAB_WIDEN
));
4859 /* No luck there. Can we do remainder and divide at once
4860 without a library call? */
4861 remainder
= gen_reg_rtx (compute_mode
);
4862 if (! expand_twoval_binop ((unsignedp
4866 NULL_RTX
, remainder
, unsignedp
))
4871 return gen_lowpart (mode
, remainder
);
4874 /* Produce the quotient. Try a quotient insn, but not a library call.
4875 If we have a divmod in this mode, use it in preference to widening
4876 the div (for this test we assume it will not fail). Note that optab2
4877 is set to the one of the two optabs that the call below will use. */
4879 = sign_expand_binop (compute_mode
, udiv_optab
, sdiv_optab
,
4880 op0
, op1
, rem_flag
? NULL_RTX
: target
,
4882 ((optab_handler (optab2
, compute_mode
)
4883 != CODE_FOR_nothing
)
4884 ? OPTAB_DIRECT
: OPTAB_WIDEN
));
4888 /* No luck there. Try a quotient-and-remainder insn,
4889 keeping the quotient alone. */
4890 quotient
= gen_reg_rtx (compute_mode
);
4891 if (! expand_twoval_binop (unsignedp
? udivmod_optab
: sdivmod_optab
,
4893 quotient
, NULL_RTX
, unsignedp
))
4897 /* Still no luck. If we are not computing the remainder,
4898 use a library call for the quotient. */
4899 quotient
= sign_expand_binop (compute_mode
,
4900 udiv_optab
, sdiv_optab
,
4902 unsignedp
, OPTAB_LIB_WIDEN
);
4909 if (target
&& GET_MODE (target
) != compute_mode
)
4914 /* No divide instruction either. Use library for remainder. */
4915 remainder
= sign_expand_binop (compute_mode
, umod_optab
, smod_optab
,
4917 unsignedp
, OPTAB_LIB_WIDEN
);
4918 /* No remainder function. Try a quotient-and-remainder
4919 function, keeping the remainder. */
4922 remainder
= gen_reg_rtx (compute_mode
);
4923 if (!expand_twoval_binop_libfunc
4924 (unsignedp
? udivmod_optab
: sdivmod_optab
,
4926 NULL_RTX
, remainder
,
4927 unsignedp
? UMOD
: MOD
))
4928 remainder
= NULL_RTX
;
4933 /* We divided. Now finish doing X - Y * (X / Y). */
4934 remainder
= expand_mult (compute_mode
, quotient
, op1
,
4935 NULL_RTX
, unsignedp
);
4936 remainder
= expand_binop (compute_mode
, sub_optab
, op0
,
4937 remainder
, target
, unsignedp
,
4942 return gen_lowpart (mode
, rem_flag
? remainder
: quotient
);
4945 /* Return a tree node with data type TYPE, describing the value of X.
4946 Usually this is an VAR_DECL, if there is no obvious better choice.
4947 X may be an expression, however we only support those expressions
4948 generated by loop.c. */
4951 make_tree (tree type
, rtx x
)
4955 switch (GET_CODE (x
))
4958 case CONST_WIDE_INT
:
4959 t
= wide_int_to_tree (type
, std::make_pair (x
, TYPE_MODE (type
)));
4963 STATIC_ASSERT (HOST_BITS_PER_WIDE_INT
* 2 <= MAX_BITSIZE_MODE_ANY_INT
);
4964 if (TARGET_SUPPORTS_WIDE_INT
== 0 && GET_MODE (x
) == VOIDmode
)
4965 t
= wide_int_to_tree (type
,
4966 wide_int::from_array (&CONST_DOUBLE_LOW (x
), 2,
4967 HOST_BITS_PER_WIDE_INT
* 2));
4972 REAL_VALUE_FROM_CONST_DOUBLE (d
, x
);
4973 t
= build_real (type
, d
);
4980 int units
= CONST_VECTOR_NUNITS (x
);
4981 tree itype
= TREE_TYPE (type
);
4985 /* Build a tree with vector elements. */
4986 elts
= XALLOCAVEC (tree
, units
);
4987 for (i
= units
- 1; i
>= 0; --i
)
4989 rtx elt
= CONST_VECTOR_ELT (x
, i
);
4990 elts
[i
] = make_tree (itype
, elt
);
4993 return build_vector (type
, elts
);
4997 return fold_build2 (PLUS_EXPR
, type
, make_tree (type
, XEXP (x
, 0)),
4998 make_tree (type
, XEXP (x
, 1)));
5001 return fold_build2 (MINUS_EXPR
, type
, make_tree (type
, XEXP (x
, 0)),
5002 make_tree (type
, XEXP (x
, 1)));
5005 return fold_build1 (NEGATE_EXPR
, type
, make_tree (type
, XEXP (x
, 0)));
5008 return fold_build2 (MULT_EXPR
, type
, make_tree (type
, XEXP (x
, 0)),
5009 make_tree (type
, XEXP (x
, 1)));
5012 return fold_build2 (LSHIFT_EXPR
, type
, make_tree (type
, XEXP (x
, 0)),
5013 make_tree (type
, XEXP (x
, 1)));
5016 t
= unsigned_type_for (type
);
5017 return fold_convert (type
, build2 (RSHIFT_EXPR
, t
,
5018 make_tree (t
, XEXP (x
, 0)),
5019 make_tree (type
, XEXP (x
, 1))));
5022 t
= signed_type_for (type
);
5023 return fold_convert (type
, build2 (RSHIFT_EXPR
, t
,
5024 make_tree (t
, XEXP (x
, 0)),
5025 make_tree (type
, XEXP (x
, 1))));
5028 if (TREE_CODE (type
) != REAL_TYPE
)
5029 t
= signed_type_for (type
);
5033 return fold_convert (type
, build2 (TRUNC_DIV_EXPR
, t
,
5034 make_tree (t
, XEXP (x
, 0)),
5035 make_tree (t
, XEXP (x
, 1))));
5037 t
= unsigned_type_for (type
);
5038 return fold_convert (type
, build2 (TRUNC_DIV_EXPR
, t
,
5039 make_tree (t
, XEXP (x
, 0)),
5040 make_tree (t
, XEXP (x
, 1))));
5044 t
= lang_hooks
.types
.type_for_mode (GET_MODE (XEXP (x
, 0)),
5045 GET_CODE (x
) == ZERO_EXTEND
);
5046 return fold_convert (type
, make_tree (t
, XEXP (x
, 0)));
5049 return make_tree (type
, XEXP (x
, 0));
5052 t
= SYMBOL_REF_DECL (x
);
5054 return fold_convert (type
, build_fold_addr_expr (t
));
5055 /* else fall through. */
5058 t
= build_decl (RTL_LOCATION (x
), VAR_DECL
, NULL_TREE
, type
);
5060 /* If TYPE is a POINTER_TYPE, we might need to convert X from
5061 address mode to pointer mode. */
5062 if (POINTER_TYPE_P (type
))
5063 x
= convert_memory_address_addr_space
5064 (TYPE_MODE (type
), x
, TYPE_ADDR_SPACE (TREE_TYPE (type
)));
5066 /* Note that we do *not* use SET_DECL_RTL here, because we do not
5067 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
5068 t
->decl_with_rtl
.rtl
= x
;
5074 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5075 and returning TARGET.
5077 If TARGET is 0, a pseudo-register or constant is returned. */
5080 expand_and (enum machine_mode mode
, rtx op0
, rtx op1
, rtx target
)
5084 if (GET_MODE (op0
) == VOIDmode
&& GET_MODE (op1
) == VOIDmode
)
5085 tem
= simplify_binary_operation (AND
, mode
, op0
, op1
);
5087 tem
= expand_binop (mode
, and_optab
, op0
, op1
, target
, 0, OPTAB_LIB_WIDEN
);
5091 else if (tem
!= target
)
5092 emit_move_insn (target
, tem
);
5096 /* Helper function for emit_store_flag. */
5098 emit_cstore (rtx target
, enum insn_code icode
, enum rtx_code code
,
5099 enum machine_mode mode
, enum machine_mode compare_mode
,
5100 int unsignedp
, rtx x
, rtx y
, int normalizep
,
5101 enum machine_mode target_mode
)
5103 struct expand_operand ops
[4];
5104 rtx op0
, comparison
, subtarget
;
5106 enum machine_mode result_mode
= targetm
.cstore_mode (icode
);
5108 last
= get_last_insn ();
5109 x
= prepare_operand (icode
, x
, 2, mode
, compare_mode
, unsignedp
);
5110 y
= prepare_operand (icode
, y
, 3, mode
, compare_mode
, unsignedp
);
5113 delete_insns_since (last
);
5117 if (target_mode
== VOIDmode
)
5118 target_mode
= result_mode
;
5120 target
= gen_reg_rtx (target_mode
);
5122 comparison
= gen_rtx_fmt_ee (code
, result_mode
, x
, y
);
5124 create_output_operand (&ops
[0], optimize
? NULL_RTX
: target
, result_mode
);
5125 create_fixed_operand (&ops
[1], comparison
);
5126 create_fixed_operand (&ops
[2], x
);
5127 create_fixed_operand (&ops
[3], y
);
5128 if (!maybe_expand_insn (icode
, 4, ops
))
5130 delete_insns_since (last
);
5133 subtarget
= ops
[0].value
;
5135 /* If we are converting to a wider mode, first convert to
5136 TARGET_MODE, then normalize. This produces better combining
5137 opportunities on machines that have a SIGN_EXTRACT when we are
5138 testing a single bit. This mostly benefits the 68k.
5140 If STORE_FLAG_VALUE does not have the sign bit set when
5141 interpreted in MODE, we can do this conversion as unsigned, which
5142 is usually more efficient. */
5143 if (GET_MODE_SIZE (target_mode
) > GET_MODE_SIZE (result_mode
))
5145 convert_move (target
, subtarget
,
5146 val_signbit_known_clear_p (result_mode
,
5149 result_mode
= target_mode
;
5154 /* If we want to keep subexpressions around, don't reuse our last
5159 /* Now normalize to the proper value in MODE. Sometimes we don't
5160 have to do anything. */
5161 if (normalizep
== 0 || normalizep
== STORE_FLAG_VALUE
)
5163 /* STORE_FLAG_VALUE might be the most negative number, so write
5164 the comparison this way to avoid a compiler-time warning. */
5165 else if (- normalizep
== STORE_FLAG_VALUE
)
5166 op0
= expand_unop (result_mode
, neg_optab
, op0
, subtarget
, 0);
5168 /* We don't want to use STORE_FLAG_VALUE < 0 below since this makes
5169 it hard to use a value of just the sign bit due to ANSI integer
5170 constant typing rules. */
5171 else if (val_signbit_known_set_p (result_mode
, STORE_FLAG_VALUE
))
5172 op0
= expand_shift (RSHIFT_EXPR
, result_mode
, op0
,
5173 GET_MODE_BITSIZE (result_mode
) - 1, subtarget
,
5177 gcc_assert (STORE_FLAG_VALUE
& 1);
5179 op0
= expand_and (result_mode
, op0
, const1_rtx
, subtarget
);
5180 if (normalizep
== -1)
5181 op0
= expand_unop (result_mode
, neg_optab
, op0
, op0
, 0);
5184 /* If we were converting to a smaller mode, do the conversion now. */
5185 if (target_mode
!= result_mode
)
5187 convert_move (target
, op0
, 0);
5195 /* A subroutine of emit_store_flag only including "tricks" that do not
5196 need a recursive call. These are kept separate to avoid infinite
5200 emit_store_flag_1 (rtx target
, enum rtx_code code
, rtx op0
, rtx op1
,
5201 enum machine_mode mode
, int unsignedp
, int normalizep
,
5202 enum machine_mode target_mode
)
5205 enum insn_code icode
;
5206 enum machine_mode compare_mode
;
5207 enum mode_class mclass
;
5208 enum rtx_code scode
;
5212 code
= unsigned_condition (code
);
5213 scode
= swap_condition (code
);
5215 /* If one operand is constant, make it the second one. Only do this
5216 if the other operand is not constant as well. */
5218 if (swap_commutative_operands_p (op0
, op1
))
5223 code
= swap_condition (code
);
5226 if (mode
== VOIDmode
)
5227 mode
= GET_MODE (op0
);
5229 /* For some comparisons with 1 and -1, we can convert this to
5230 comparisons with zero. This will often produce more opportunities for
5231 store-flag insns. */
5236 if (op1
== const1_rtx
)
5237 op1
= const0_rtx
, code
= LE
;
5240 if (op1
== constm1_rtx
)
5241 op1
= const0_rtx
, code
= LT
;
5244 if (op1
== const1_rtx
)
5245 op1
= const0_rtx
, code
= GT
;
5248 if (op1
== constm1_rtx
)
5249 op1
= const0_rtx
, code
= GE
;
5252 if (op1
== const1_rtx
)
5253 op1
= const0_rtx
, code
= NE
;
5256 if (op1
== const1_rtx
)
5257 op1
= const0_rtx
, code
= EQ
;
5263 /* If we are comparing a double-word integer with zero or -1, we can
5264 convert the comparison into one involving a single word. */
5265 if (GET_MODE_BITSIZE (mode
) == BITS_PER_WORD
* 2
5266 && GET_MODE_CLASS (mode
) == MODE_INT
5267 && (!MEM_P (op0
) || ! MEM_VOLATILE_P (op0
)))
5269 if ((code
== EQ
|| code
== NE
)
5270 && (op1
== const0_rtx
|| op1
== constm1_rtx
))
5274 /* Do a logical OR or AND of the two words and compare the
5276 op00
= simplify_gen_subreg (word_mode
, op0
, mode
, 0);
5277 op01
= simplify_gen_subreg (word_mode
, op0
, mode
, UNITS_PER_WORD
);
5278 tem
= expand_binop (word_mode
,
5279 op1
== const0_rtx
? ior_optab
: and_optab
,
5280 op00
, op01
, NULL_RTX
, unsignedp
,
5284 tem
= emit_store_flag (NULL_RTX
, code
, tem
, op1
, word_mode
,
5285 unsignedp
, normalizep
);
5287 else if ((code
== LT
|| code
== GE
) && op1
== const0_rtx
)
5291 /* If testing the sign bit, can just test on high word. */
5292 op0h
= simplify_gen_subreg (word_mode
, op0
, mode
,
5293 subreg_highpart_offset (word_mode
,
5295 tem
= emit_store_flag (NULL_RTX
, code
, op0h
, op1
, word_mode
,
5296 unsignedp
, normalizep
);
5303 if (target_mode
== VOIDmode
|| GET_MODE (tem
) == target_mode
)
5306 target
= gen_reg_rtx (target_mode
);
5308 convert_move (target
, tem
,
5309 !val_signbit_known_set_p (word_mode
,
5310 (normalizep
? normalizep
5311 : STORE_FLAG_VALUE
)));
5316 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5317 complement of A (for GE) and shifting the sign bit to the low bit. */
5318 if (op1
== const0_rtx
&& (code
== LT
|| code
== GE
)
5319 && GET_MODE_CLASS (mode
) == MODE_INT
5320 && (normalizep
|| STORE_FLAG_VALUE
== 1
5321 || val_signbit_p (mode
, STORE_FLAG_VALUE
)))
5328 /* If the result is to be wider than OP0, it is best to convert it
5329 first. If it is to be narrower, it is *incorrect* to convert it
5331 else if (GET_MODE_SIZE (target_mode
) > GET_MODE_SIZE (mode
))
5333 op0
= convert_modes (target_mode
, mode
, op0
, 0);
5337 if (target_mode
!= mode
)
5341 op0
= expand_unop (mode
, one_cmpl_optab
, op0
,
5342 ((STORE_FLAG_VALUE
== 1 || normalizep
)
5343 ? 0 : subtarget
), 0);
5345 if (STORE_FLAG_VALUE
== 1 || normalizep
)
5346 /* If we are supposed to produce a 0/1 value, we want to do
5347 a logical shift from the sign bit to the low-order bit; for
5348 a -1/0 value, we do an arithmetic shift. */
5349 op0
= expand_shift (RSHIFT_EXPR
, mode
, op0
,
5350 GET_MODE_BITSIZE (mode
) - 1,
5351 subtarget
, normalizep
!= -1);
5353 if (mode
!= target_mode
)
5354 op0
= convert_modes (target_mode
, mode
, op0
, 0);
5359 mclass
= GET_MODE_CLASS (mode
);
5360 for (compare_mode
= mode
; compare_mode
!= VOIDmode
;
5361 compare_mode
= GET_MODE_WIDER_MODE (compare_mode
))
5363 enum machine_mode optab_mode
= mclass
== MODE_CC
? CCmode
: compare_mode
;
5364 icode
= optab_handler (cstore_optab
, optab_mode
);
5365 if (icode
!= CODE_FOR_nothing
)
5367 do_pending_stack_adjust ();
5368 tem
= emit_cstore (target
, icode
, code
, mode
, compare_mode
,
5369 unsignedp
, op0
, op1
, normalizep
, target_mode
);
5373 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
)
5375 tem
= emit_cstore (target
, icode
, scode
, mode
, compare_mode
,
5376 unsignedp
, op1
, op0
, normalizep
, target_mode
);
5387 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5388 and storing in TARGET. Normally return TARGET.
5389 Return 0 if that cannot be done.
5391 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5392 it is VOIDmode, they cannot both be CONST_INT.
5394 UNSIGNEDP is for the case where we have to widen the operands
5395 to perform the operation. It says to use zero-extension.
5397 NORMALIZEP is 1 if we should convert the result to be either zero
5398 or one. Normalize is -1 if we should convert the result to be
5399 either zero or -1. If NORMALIZEP is zero, the result will be left
5400 "raw" out of the scc insn. */
5403 emit_store_flag (rtx target
, enum rtx_code code
, rtx op0
, rtx op1
,
5404 enum machine_mode mode
, int unsignedp
, int normalizep
)
5406 enum machine_mode target_mode
= target
? GET_MODE (target
) : VOIDmode
;
5407 enum rtx_code rcode
;
5412 /* If we compare constants, we shouldn't use a store-flag operation,
5413 but a constant load. We can get there via the vanilla route that
5414 usually generates a compare-branch sequence, but will in this case
5415 fold the comparison to a constant, and thus elide the branch. */
5416 if (CONSTANT_P (op0
) && CONSTANT_P (op1
))
5419 tem
= emit_store_flag_1 (target
, code
, op0
, op1
, mode
, unsignedp
, normalizep
,
5424 /* If we reached here, we can't do this with a scc insn, however there
5425 are some comparisons that can be done in other ways. Don't do any
5426 of these cases if branches are very cheap. */
5427 if (BRANCH_COST (optimize_insn_for_speed_p (), false) == 0)
5430 /* See what we need to return. We can only return a 1, -1, or the
5433 if (normalizep
== 0)
5435 if (STORE_FLAG_VALUE
== 1 || STORE_FLAG_VALUE
== -1)
5436 normalizep
= STORE_FLAG_VALUE
;
5438 else if (val_signbit_p (mode
, STORE_FLAG_VALUE
))
5444 last
= get_last_insn ();
5446 /* If optimizing, use different pseudo registers for each insn, instead
5447 of reusing the same pseudo. This leads to better CSE, but slows
5448 down the compiler, since there are more pseudos */
5449 subtarget
= (!optimize
5450 && (target_mode
== mode
)) ? target
: NULL_RTX
;
5451 trueval
= GEN_INT (normalizep
? normalizep
: STORE_FLAG_VALUE
);
5453 /* For floating-point comparisons, try the reverse comparison or try
5454 changing the "orderedness" of the comparison. */
5455 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
)
5457 enum rtx_code first_code
;
5460 rcode
= reverse_condition_maybe_unordered (code
);
5461 if (can_compare_p (rcode
, mode
, ccp_store_flag
)
5462 && (code
== ORDERED
|| code
== UNORDERED
5463 || (! HONOR_NANS (mode
) && (code
== LTGT
|| code
== UNEQ
))
5464 || (! HONOR_SNANS (mode
) && (code
== EQ
|| code
== NE
))))
5466 int want_add
= ((STORE_FLAG_VALUE
== 1 && normalizep
== -1)
5467 || (STORE_FLAG_VALUE
== -1 && normalizep
== 1));
5469 /* For the reverse comparison, use either an addition or a XOR. */
5471 && rtx_cost (GEN_INT (normalizep
), PLUS
, 1,
5472 optimize_insn_for_speed_p ()) == 0)
5474 tem
= emit_store_flag_1 (subtarget
, rcode
, op0
, op1
, mode
, 0,
5475 STORE_FLAG_VALUE
, target_mode
);
5477 return expand_binop (target_mode
, add_optab
, tem
,
5478 gen_int_mode (normalizep
, target_mode
),
5479 target
, 0, OPTAB_WIDEN
);
5482 && rtx_cost (trueval
, XOR
, 1,
5483 optimize_insn_for_speed_p ()) == 0)
5485 tem
= emit_store_flag_1 (subtarget
, rcode
, op0
, op1
, mode
, 0,
5486 normalizep
, target_mode
);
5488 return expand_binop (target_mode
, xor_optab
, tem
, trueval
,
5489 target
, INTVAL (trueval
) >= 0, OPTAB_WIDEN
);
5493 delete_insns_since (last
);
5495 /* Cannot split ORDERED and UNORDERED, only try the above trick. */
5496 if (code
== ORDERED
|| code
== UNORDERED
)
5499 and_them
= split_comparison (code
, mode
, &first_code
, &code
);
5501 /* If there are no NaNs, the first comparison should always fall through.
5502 Effectively change the comparison to the other one. */
5503 if (!HONOR_NANS (mode
))
5505 gcc_assert (first_code
== (and_them
? ORDERED
: UNORDERED
));
5506 return emit_store_flag_1 (target
, code
, op0
, op1
, mode
, 0, normalizep
,
5510 #ifdef HAVE_conditional_move
5511 /* Try using a setcc instruction for ORDERED/UNORDERED, followed by a
5512 conditional move. */
5513 tem
= emit_store_flag_1 (subtarget
, first_code
, op0
, op1
, mode
, 0,
5514 normalizep
, target_mode
);
5519 tem
= emit_conditional_move (target
, code
, op0
, op1
, mode
,
5520 tem
, const0_rtx
, GET_MODE (tem
), 0);
5522 tem
= emit_conditional_move (target
, code
, op0
, op1
, mode
,
5523 trueval
, tem
, GET_MODE (tem
), 0);
5526 delete_insns_since (last
);
5533 /* The remaining tricks only apply to integer comparisons. */
5535 if (GET_MODE_CLASS (mode
) != MODE_INT
)
5538 /* If this is an equality comparison of integers, we can try to exclusive-or
5539 (or subtract) the two operands and use a recursive call to try the
5540 comparison with zero. Don't do any of these cases if branches are
5543 if ((code
== EQ
|| code
== NE
) && op1
!= const0_rtx
)
5545 tem
= expand_binop (mode
, xor_optab
, op0
, op1
, subtarget
, 1,
5549 tem
= expand_binop (mode
, sub_optab
, op0
, op1
, subtarget
, 1,
5552 tem
= emit_store_flag (target
, code
, tem
, const0_rtx
,
5553 mode
, unsignedp
, normalizep
);
5557 delete_insns_since (last
);
5560 /* For integer comparisons, try the reverse comparison. However, for
5561 small X and if we'd have anyway to extend, implementing "X != 0"
5562 as "-(int)X >> 31" is still cheaper than inverting "(int)X == 0". */
5563 rcode
= reverse_condition (code
);
5564 if (can_compare_p (rcode
, mode
, ccp_store_flag
)
5565 && ! (optab_handler (cstore_optab
, mode
) == CODE_FOR_nothing
5567 && GET_MODE_SIZE (mode
) < UNITS_PER_WORD
5568 && op1
== const0_rtx
))
5570 int want_add
= ((STORE_FLAG_VALUE
== 1 && normalizep
== -1)
5571 || (STORE_FLAG_VALUE
== -1 && normalizep
== 1));
5573 /* Again, for the reverse comparison, use either an addition or a XOR. */
5575 && rtx_cost (GEN_INT (normalizep
), PLUS
, 1,
5576 optimize_insn_for_speed_p ()) == 0)
5578 tem
= emit_store_flag_1 (subtarget
, rcode
, op0
, op1
, mode
, 0,
5579 STORE_FLAG_VALUE
, target_mode
);
5581 tem
= expand_binop (target_mode
, add_optab
, tem
,
5582 gen_int_mode (normalizep
, target_mode
),
5583 target
, 0, OPTAB_WIDEN
);
5586 && rtx_cost (trueval
, XOR
, 1,
5587 optimize_insn_for_speed_p ()) == 0)
5589 tem
= emit_store_flag_1 (subtarget
, rcode
, op0
, op1
, mode
, 0,
5590 normalizep
, target_mode
);
5592 tem
= expand_binop (target_mode
, xor_optab
, tem
, trueval
, target
,
5593 INTVAL (trueval
) >= 0, OPTAB_WIDEN
);
5598 delete_insns_since (last
);
5601 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5602 the constant zero. Reject all other comparisons at this point. Only
5603 do LE and GT if branches are expensive since they are expensive on
5604 2-operand machines. */
5606 if (op1
!= const0_rtx
5607 || (code
!= EQ
&& code
!= NE
5608 && (BRANCH_COST (optimize_insn_for_speed_p (),
5609 false) <= 1 || (code
!= LE
&& code
!= GT
))))
5612 /* Try to put the result of the comparison in the sign bit. Assume we can't
5613 do the necessary operation below. */
5617 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5618 the sign bit set. */
5622 /* This is destructive, so SUBTARGET can't be OP0. */
5623 if (rtx_equal_p (subtarget
, op0
))
5626 tem
= expand_binop (mode
, sub_optab
, op0
, const1_rtx
, subtarget
, 0,
5629 tem
= expand_binop (mode
, ior_optab
, op0
, tem
, subtarget
, 0,
5633 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5634 number of bits in the mode of OP0, minus one. */
5638 if (rtx_equal_p (subtarget
, op0
))
5641 tem
= expand_shift (RSHIFT_EXPR
, mode
, op0
,
5642 GET_MODE_BITSIZE (mode
) - 1,
5644 tem
= expand_binop (mode
, sub_optab
, tem
, op0
, subtarget
, 0,
5648 if (code
== EQ
|| code
== NE
)
5650 /* For EQ or NE, one way to do the comparison is to apply an operation
5651 that converts the operand into a positive number if it is nonzero
5652 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5653 for NE we negate. This puts the result in the sign bit. Then we
5654 normalize with a shift, if needed.
5656 Two operations that can do the above actions are ABS and FFS, so try
5657 them. If that doesn't work, and MODE is smaller than a full word,
5658 we can use zero-extension to the wider mode (an unsigned conversion)
5659 as the operation. */
5661 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5662 that is compensated by the subsequent overflow when subtracting
5665 if (optab_handler (abs_optab
, mode
) != CODE_FOR_nothing
)
5666 tem
= expand_unop (mode
, abs_optab
, op0
, subtarget
, 1);
5667 else if (optab_handler (ffs_optab
, mode
) != CODE_FOR_nothing
)
5668 tem
= expand_unop (mode
, ffs_optab
, op0
, subtarget
, 1);
5669 else if (GET_MODE_SIZE (mode
) < UNITS_PER_WORD
)
5671 tem
= convert_modes (word_mode
, mode
, op0
, 1);
5678 tem
= expand_binop (mode
, sub_optab
, tem
, const1_rtx
, subtarget
,
5681 tem
= expand_unop (mode
, neg_optab
, tem
, subtarget
, 0);
5684 /* If we couldn't do it that way, for NE we can "or" the two's complement
5685 of the value with itself. For EQ, we take the one's complement of
5686 that "or", which is an extra insn, so we only handle EQ if branches
5691 || BRANCH_COST (optimize_insn_for_speed_p (),
5694 if (rtx_equal_p (subtarget
, op0
))
5697 tem
= expand_unop (mode
, neg_optab
, op0
, subtarget
, 0);
5698 tem
= expand_binop (mode
, ior_optab
, tem
, op0
, subtarget
, 0,
5701 if (tem
&& code
== EQ
)
5702 tem
= expand_unop (mode
, one_cmpl_optab
, tem
, subtarget
, 0);
5706 if (tem
&& normalizep
)
5707 tem
= expand_shift (RSHIFT_EXPR
, mode
, tem
,
5708 GET_MODE_BITSIZE (mode
) - 1,
5709 subtarget
, normalizep
== 1);
5715 else if (GET_MODE (tem
) != target_mode
)
5717 convert_move (target
, tem
, 0);
5720 else if (!subtarget
)
5722 emit_move_insn (target
, tem
);
5727 delete_insns_since (last
);
5732 /* Like emit_store_flag, but always succeeds. */
5735 emit_store_flag_force (rtx target
, enum rtx_code code
, rtx op0
, rtx op1
,
5736 enum machine_mode mode
, int unsignedp
, int normalizep
)
5739 rtx_code_label
*label
;
5740 rtx trueval
, falseval
;
5742 /* First see if emit_store_flag can do the job. */
5743 tem
= emit_store_flag (target
, code
, op0
, op1
, mode
, unsignedp
, normalizep
);
5748 target
= gen_reg_rtx (word_mode
);
5750 /* If this failed, we have to do this with set/compare/jump/set code.
5751 For foo != 0, if foo is in OP0, just replace it with 1 if nonzero. */
5752 trueval
= normalizep
? GEN_INT (normalizep
) : const1_rtx
;
5754 && GET_MODE_CLASS (mode
) == MODE_INT
5757 && op1
== const0_rtx
)
5759 label
= gen_label_rtx ();
5760 do_compare_rtx_and_jump (target
, const0_rtx
, EQ
, unsignedp
,
5761 mode
, NULL_RTX
, NULL_RTX
, label
, -1);
5762 emit_move_insn (target
, trueval
);
5768 || reg_mentioned_p (target
, op0
) || reg_mentioned_p (target
, op1
))
5769 target
= gen_reg_rtx (GET_MODE (target
));
5771 /* Jump in the right direction if the target cannot implement CODE
5772 but can jump on its reverse condition. */
5773 falseval
= const0_rtx
;
5774 if (! can_compare_p (code
, mode
, ccp_jump
)
5775 && (! FLOAT_MODE_P (mode
)
5776 || code
== ORDERED
|| code
== UNORDERED
5777 || (! HONOR_NANS (mode
) && (code
== LTGT
|| code
== UNEQ
))
5778 || (! HONOR_SNANS (mode
) && (code
== EQ
|| code
== NE
))))
5780 enum rtx_code rcode
;
5781 if (FLOAT_MODE_P (mode
))
5782 rcode
= reverse_condition_maybe_unordered (code
);
5784 rcode
= reverse_condition (code
);
5786 /* Canonicalize to UNORDERED for the libcall. */
5787 if (can_compare_p (rcode
, mode
, ccp_jump
)
5788 || (code
== ORDERED
&& ! can_compare_p (ORDERED
, mode
, ccp_jump
)))
5791 trueval
= const0_rtx
;
5796 emit_move_insn (target
, trueval
);
5797 label
= gen_label_rtx ();
5798 do_compare_rtx_and_jump (op0
, op1
, code
, unsignedp
, mode
, NULL_RTX
,
5799 NULL_RTX
, label
, -1);
5801 emit_move_insn (target
, falseval
);
5807 /* Perform possibly multi-word comparison and conditional jump to LABEL
5808 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE. This is
5809 now a thin wrapper around do_compare_rtx_and_jump. */
5812 do_cmp_and_jump (rtx arg1
, rtx arg2
, enum rtx_code op
, enum machine_mode mode
,
5813 rtx_code_label
*label
)
5815 int unsignedp
= (op
== LTU
|| op
== LEU
|| op
== GTU
|| op
== GEU
);
5816 do_compare_rtx_and_jump (arg1
, arg2
, op
, unsignedp
, mode
,
5817 NULL_RTX
, NULL_RTX
, label
, -1);