gcc/ChangeLog
[official-gcc.git] / gcc / expmed.c
blobdaadd3def5d027980f8c53af3ddf5b034ede2d3d
1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987-2013 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "diagnostic-core.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "tm_p.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "expr.h"
33 #include "optabs.h"
34 #include "recog.h"
35 #include "langhooks.h"
36 #include "df.h"
37 #include "target.h"
38 #include "expmed.h"
40 struct target_expmed default_target_expmed;
41 #if SWITCHABLE_TARGET
42 struct target_expmed *this_target_expmed = &default_target_expmed;
43 #endif
45 static void store_fixed_bit_field (rtx, unsigned HOST_WIDE_INT,
46 unsigned HOST_WIDE_INT,
47 unsigned HOST_WIDE_INT,
48 unsigned HOST_WIDE_INT,
49 rtx);
50 static void store_split_bit_field (rtx, unsigned HOST_WIDE_INT,
51 unsigned HOST_WIDE_INT,
52 unsigned HOST_WIDE_INT,
53 unsigned HOST_WIDE_INT,
54 rtx);
55 static rtx extract_fixed_bit_field (enum machine_mode, rtx,
56 unsigned HOST_WIDE_INT,
57 unsigned HOST_WIDE_INT, rtx, int, bool);
58 static rtx mask_rtx (enum machine_mode, int, int, int);
59 static rtx lshift_value (enum machine_mode, rtx, int, int);
60 static rtx extract_split_bit_field (rtx, unsigned HOST_WIDE_INT,
61 unsigned HOST_WIDE_INT, int);
62 static void do_cmp_and_jump (rtx, rtx, enum rtx_code, enum machine_mode, rtx);
63 static rtx expand_smod_pow2 (enum machine_mode, rtx, HOST_WIDE_INT);
64 static rtx expand_sdiv_pow2 (enum machine_mode, rtx, HOST_WIDE_INT);
66 /* Test whether a value is zero of a power of two. */
67 #define EXACT_POWER_OF_2_OR_ZERO_P(x) \
68 (((x) & ((x) - (unsigned HOST_WIDE_INT) 1)) == 0)
70 struct init_expmed_rtl
72 struct rtx_def reg;
73 struct rtx_def plus;
74 struct rtx_def neg;
75 struct rtx_def mult;
76 struct rtx_def sdiv;
77 struct rtx_def udiv;
78 struct rtx_def sdiv_32;
79 struct rtx_def smod_32;
80 struct rtx_def wide_mult;
81 struct rtx_def wide_lshr;
82 struct rtx_def wide_trunc;
83 struct rtx_def shift;
84 struct rtx_def shift_mult;
85 struct rtx_def shift_add;
86 struct rtx_def shift_sub0;
87 struct rtx_def shift_sub1;
88 struct rtx_def zext;
89 struct rtx_def trunc;
91 rtx pow2[MAX_BITS_PER_WORD];
92 rtx cint[MAX_BITS_PER_WORD];
95 static void
96 init_expmed_one_conv (struct init_expmed_rtl *all, enum machine_mode to_mode,
97 enum machine_mode from_mode, bool speed)
99 int to_size, from_size;
100 rtx which;
102 /* We're given no information about the true size of a partial integer,
103 only the size of the "full" integer it requires for storage. For
104 comparison purposes here, reduce the bit size by one in that case. */
105 to_size = (GET_MODE_BITSIZE (to_mode)
106 - (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT));
107 from_size = (GET_MODE_BITSIZE (from_mode)
108 - (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT));
110 /* Assume cost of zero-extend and sign-extend is the same. */
111 which = (to_size < from_size ? &all->trunc : &all->zext);
113 PUT_MODE (&all->reg, from_mode);
114 set_convert_cost (to_mode, from_mode, speed, set_src_cost (which, speed));
117 static void
118 init_expmed_one_mode (struct init_expmed_rtl *all,
119 enum machine_mode mode, int speed)
121 int m, n, mode_bitsize;
122 enum machine_mode mode_from;
124 mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
126 PUT_MODE (&all->reg, mode);
127 PUT_MODE (&all->plus, mode);
128 PUT_MODE (&all->neg, mode);
129 PUT_MODE (&all->mult, mode);
130 PUT_MODE (&all->sdiv, mode);
131 PUT_MODE (&all->udiv, mode);
132 PUT_MODE (&all->sdiv_32, mode);
133 PUT_MODE (&all->smod_32, mode);
134 PUT_MODE (&all->wide_trunc, mode);
135 PUT_MODE (&all->shift, mode);
136 PUT_MODE (&all->shift_mult, mode);
137 PUT_MODE (&all->shift_add, mode);
138 PUT_MODE (&all->shift_sub0, mode);
139 PUT_MODE (&all->shift_sub1, mode);
140 PUT_MODE (&all->zext, mode);
141 PUT_MODE (&all->trunc, mode);
143 set_add_cost (speed, mode, set_src_cost (&all->plus, speed));
144 set_neg_cost (speed, mode, set_src_cost (&all->neg, speed));
145 set_mul_cost (speed, mode, set_src_cost (&all->mult, speed));
146 set_sdiv_cost (speed, mode, set_src_cost (&all->sdiv, speed));
147 set_udiv_cost (speed, mode, set_src_cost (&all->udiv, speed));
149 set_sdiv_pow2_cheap (speed, mode, (set_src_cost (&all->sdiv_32, speed)
150 <= 2 * add_cost (speed, mode)));
151 set_smod_pow2_cheap (speed, mode, (set_src_cost (&all->smod_32, speed)
152 <= 4 * add_cost (speed, mode)));
154 set_shift_cost (speed, mode, 0, 0);
156 int cost = add_cost (speed, mode);
157 set_shiftadd_cost (speed, mode, 0, cost);
158 set_shiftsub0_cost (speed, mode, 0, cost);
159 set_shiftsub1_cost (speed, mode, 0, cost);
162 n = MIN (MAX_BITS_PER_WORD, mode_bitsize);
163 for (m = 1; m < n; m++)
165 XEXP (&all->shift, 1) = all->cint[m];
166 XEXP (&all->shift_mult, 1) = all->pow2[m];
168 set_shift_cost (speed, mode, m, set_src_cost (&all->shift, speed));
169 set_shiftadd_cost (speed, mode, m, set_src_cost (&all->shift_add, speed));
170 set_shiftsub0_cost (speed, mode, m, set_src_cost (&all->shift_sub0, speed));
171 set_shiftsub1_cost (speed, mode, m, set_src_cost (&all->shift_sub1, speed));
174 if (SCALAR_INT_MODE_P (mode))
176 for (mode_from = MIN_MODE_INT; mode_from <= MAX_MODE_INT;
177 mode_from = (enum machine_mode)(mode_from + 1))
178 init_expmed_one_conv (all, mode, mode_from, speed);
180 if (GET_MODE_CLASS (mode) == MODE_INT)
182 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
183 if (wider_mode != VOIDmode)
185 PUT_MODE (&all->zext, wider_mode);
186 PUT_MODE (&all->wide_mult, wider_mode);
187 PUT_MODE (&all->wide_lshr, wider_mode);
188 XEXP (&all->wide_lshr, 1) = GEN_INT (mode_bitsize);
190 set_mul_widen_cost (speed, wider_mode,
191 set_src_cost (&all->wide_mult, speed));
192 set_mul_highpart_cost (speed, mode,
193 set_src_cost (&all->wide_trunc, speed));
198 void
199 init_expmed (void)
201 struct init_expmed_rtl all;
202 enum machine_mode mode;
203 int m, speed;
205 memset (&all, 0, sizeof all);
206 for (m = 1; m < MAX_BITS_PER_WORD; m++)
208 all.pow2[m] = GEN_INT ((HOST_WIDE_INT) 1 << m);
209 all.cint[m] = GEN_INT (m);
212 PUT_CODE (&all.reg, REG);
213 /* Avoid using hard regs in ways which may be unsupported. */
214 SET_REGNO (&all.reg, LAST_VIRTUAL_REGISTER + 1);
216 PUT_CODE (&all.plus, PLUS);
217 XEXP (&all.plus, 0) = &all.reg;
218 XEXP (&all.plus, 1) = &all.reg;
220 PUT_CODE (&all.neg, NEG);
221 XEXP (&all.neg, 0) = &all.reg;
223 PUT_CODE (&all.mult, MULT);
224 XEXP (&all.mult, 0) = &all.reg;
225 XEXP (&all.mult, 1) = &all.reg;
227 PUT_CODE (&all.sdiv, DIV);
228 XEXP (&all.sdiv, 0) = &all.reg;
229 XEXP (&all.sdiv, 1) = &all.reg;
231 PUT_CODE (&all.udiv, UDIV);
232 XEXP (&all.udiv, 0) = &all.reg;
233 XEXP (&all.udiv, 1) = &all.reg;
235 PUT_CODE (&all.sdiv_32, DIV);
236 XEXP (&all.sdiv_32, 0) = &all.reg;
237 XEXP (&all.sdiv_32, 1) = 32 < MAX_BITS_PER_WORD ? all.cint[32] : GEN_INT (32);
239 PUT_CODE (&all.smod_32, MOD);
240 XEXP (&all.smod_32, 0) = &all.reg;
241 XEXP (&all.smod_32, 1) = XEXP (&all.sdiv_32, 1);
243 PUT_CODE (&all.zext, ZERO_EXTEND);
244 XEXP (&all.zext, 0) = &all.reg;
246 PUT_CODE (&all.wide_mult, MULT);
247 XEXP (&all.wide_mult, 0) = &all.zext;
248 XEXP (&all.wide_mult, 1) = &all.zext;
250 PUT_CODE (&all.wide_lshr, LSHIFTRT);
251 XEXP (&all.wide_lshr, 0) = &all.wide_mult;
253 PUT_CODE (&all.wide_trunc, TRUNCATE);
254 XEXP (&all.wide_trunc, 0) = &all.wide_lshr;
256 PUT_CODE (&all.shift, ASHIFT);
257 XEXP (&all.shift, 0) = &all.reg;
259 PUT_CODE (&all.shift_mult, MULT);
260 XEXP (&all.shift_mult, 0) = &all.reg;
262 PUT_CODE (&all.shift_add, PLUS);
263 XEXP (&all.shift_add, 0) = &all.shift_mult;
264 XEXP (&all.shift_add, 1) = &all.reg;
266 PUT_CODE (&all.shift_sub0, MINUS);
267 XEXP (&all.shift_sub0, 0) = &all.shift_mult;
268 XEXP (&all.shift_sub0, 1) = &all.reg;
270 PUT_CODE (&all.shift_sub1, MINUS);
271 XEXP (&all.shift_sub1, 0) = &all.reg;
272 XEXP (&all.shift_sub1, 1) = &all.shift_mult;
274 PUT_CODE (&all.trunc, TRUNCATE);
275 XEXP (&all.trunc, 0) = &all.reg;
277 for (speed = 0; speed < 2; speed++)
279 crtl->maybe_hot_insn_p = speed;
280 set_zero_cost (speed, set_src_cost (const0_rtx, speed));
282 for (mode = MIN_MODE_INT; mode <= MAX_MODE_INT;
283 mode = (enum machine_mode)(mode + 1))
284 init_expmed_one_mode (&all, mode, speed);
286 if (MIN_MODE_PARTIAL_INT != VOIDmode)
287 for (mode = MIN_MODE_PARTIAL_INT; mode <= MAX_MODE_PARTIAL_INT;
288 mode = (enum machine_mode)(mode + 1))
289 init_expmed_one_mode (&all, mode, speed);
291 if (MIN_MODE_VECTOR_INT != VOIDmode)
292 for (mode = MIN_MODE_VECTOR_INT; mode <= MAX_MODE_VECTOR_INT;
293 mode = (enum machine_mode)(mode + 1))
294 init_expmed_one_mode (&all, mode, speed);
297 if (alg_hash_used_p ())
299 struct alg_hash_entry *p = alg_hash_entry_ptr (0);
300 memset (p, 0, sizeof (*p) * NUM_ALG_HASH_ENTRIES);
302 else
303 set_alg_hash_used_p (true);
304 default_rtl_profile ();
307 /* Return an rtx representing minus the value of X.
308 MODE is the intended mode of the result,
309 useful if X is a CONST_INT. */
312 negate_rtx (enum machine_mode mode, rtx x)
314 rtx result = simplify_unary_operation (NEG, mode, x, mode);
316 if (result == 0)
317 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
319 return result;
322 /* Adjust bitfield memory MEM so that it points to the first unit of mode
323 MODE that contains a bitfield of size BITSIZE at bit position BITNUM.
324 If MODE is BLKmode, return a reference to every byte in the bitfield.
325 Set *NEW_BITNUM to the bit position of the field within the new memory. */
327 static rtx
328 narrow_bit_field_mem (rtx mem, enum machine_mode mode,
329 unsigned HOST_WIDE_INT bitsize,
330 unsigned HOST_WIDE_INT bitnum,
331 unsigned HOST_WIDE_INT *new_bitnum)
333 if (mode == BLKmode)
335 *new_bitnum = bitnum % BITS_PER_UNIT;
336 HOST_WIDE_INT offset = bitnum / BITS_PER_UNIT;
337 HOST_WIDE_INT size = ((*new_bitnum + bitsize + BITS_PER_UNIT - 1)
338 / BITS_PER_UNIT);
339 return adjust_bitfield_address_size (mem, mode, offset, size);
341 else
343 unsigned int unit = GET_MODE_BITSIZE (mode);
344 *new_bitnum = bitnum % unit;
345 HOST_WIDE_INT offset = (bitnum - *new_bitnum) / BITS_PER_UNIT;
346 return adjust_bitfield_address (mem, mode, offset);
350 /* The caller wants to perform insertion or extraction PATTERN on a
351 bitfield of size BITSIZE at BITNUM bits into memory operand OP0.
352 BITREGION_START and BITREGION_END are as for store_bit_field
353 and FIELDMODE is the natural mode of the field.
355 Search for a mode that is compatible with the memory access
356 restrictions and (where applicable) with a register insertion or
357 extraction. Return the new memory on success, storing the adjusted
358 bit position in *NEW_BITNUM. Return null otherwise. */
360 static rtx
361 adjust_bit_field_mem_for_reg (enum extraction_pattern pattern,
362 rtx op0, HOST_WIDE_INT bitsize,
363 HOST_WIDE_INT bitnum,
364 unsigned HOST_WIDE_INT bitregion_start,
365 unsigned HOST_WIDE_INT bitregion_end,
366 enum machine_mode fieldmode,
367 unsigned HOST_WIDE_INT *new_bitnum)
369 bit_field_mode_iterator iter (bitsize, bitnum, bitregion_start,
370 bitregion_end, MEM_ALIGN (op0),
371 MEM_VOLATILE_P (op0));
372 enum machine_mode best_mode;
373 if (iter.next_mode (&best_mode))
375 /* We can use a memory in BEST_MODE. See whether this is true for
376 any wider modes. All other things being equal, we prefer to
377 use the widest mode possible because it tends to expose more
378 CSE opportunities. */
379 if (!iter.prefer_smaller_modes ())
381 /* Limit the search to the mode required by the corresponding
382 register insertion or extraction instruction, if any. */
383 enum machine_mode limit_mode = word_mode;
384 extraction_insn insn;
385 if (get_best_reg_extraction_insn (&insn, pattern,
386 GET_MODE_BITSIZE (best_mode),
387 fieldmode))
388 limit_mode = insn.field_mode;
390 enum machine_mode wider_mode;
391 while (iter.next_mode (&wider_mode)
392 && GET_MODE_SIZE (wider_mode) <= GET_MODE_SIZE (limit_mode))
393 best_mode = wider_mode;
395 return narrow_bit_field_mem (op0, best_mode, bitsize, bitnum,
396 new_bitnum);
398 return NULL_RTX;
401 /* Return true if a bitfield of size BITSIZE at bit number BITNUM within
402 a structure of mode STRUCT_MODE represents a lowpart subreg. The subreg
403 offset is then BITNUM / BITS_PER_UNIT. */
405 static bool
406 lowpart_bit_field_p (unsigned HOST_WIDE_INT bitnum,
407 unsigned HOST_WIDE_INT bitsize,
408 enum machine_mode struct_mode)
410 if (BYTES_BIG_ENDIAN)
411 return (bitnum % BITS_PER_UNIT == 0
412 && (bitnum + bitsize == GET_MODE_BITSIZE (struct_mode)
413 || (bitnum + bitsize) % BITS_PER_WORD == 0));
414 else
415 return bitnum % BITS_PER_WORD == 0;
418 /* Return true if OP is a memory and if a bitfield of size BITSIZE at
419 bit number BITNUM can be treated as a simple value of mode MODE. */
421 static bool
422 simple_mem_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
423 unsigned HOST_WIDE_INT bitnum, enum machine_mode mode)
425 return (MEM_P (op0)
426 && bitnum % BITS_PER_UNIT == 0
427 && bitsize == GET_MODE_BITSIZE (mode)
428 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
429 || (bitnum % GET_MODE_ALIGNMENT (mode) == 0
430 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode))));
433 /* Try to use instruction INSV to store VALUE into a field of OP0.
434 BITSIZE and BITNUM are as for store_bit_field. */
436 static bool
437 store_bit_field_using_insv (const extraction_insn *insv, rtx op0,
438 unsigned HOST_WIDE_INT bitsize,
439 unsigned HOST_WIDE_INT bitnum, rtx value)
441 struct expand_operand ops[4];
442 rtx value1;
443 rtx xop0 = op0;
444 rtx last = get_last_insn ();
445 bool copy_back = false;
447 enum machine_mode op_mode = insv->field_mode;
448 unsigned int unit = GET_MODE_BITSIZE (op_mode);
449 if (bitsize == 0 || bitsize > unit)
450 return false;
452 if (MEM_P (xop0))
453 /* Get a reference to the first byte of the field. */
454 xop0 = narrow_bit_field_mem (xop0, insv->struct_mode, bitsize, bitnum,
455 &bitnum);
456 else
458 /* Convert from counting within OP0 to counting in OP_MODE. */
459 if (BYTES_BIG_ENDIAN)
460 bitnum += unit - GET_MODE_BITSIZE (GET_MODE (op0));
462 /* If xop0 is a register, we need it in OP_MODE
463 to make it acceptable to the format of insv. */
464 if (GET_CODE (xop0) == SUBREG)
465 /* We can't just change the mode, because this might clobber op0,
466 and we will need the original value of op0 if insv fails. */
467 xop0 = gen_rtx_SUBREG (op_mode, SUBREG_REG (xop0), SUBREG_BYTE (xop0));
468 if (REG_P (xop0) && GET_MODE (xop0) != op_mode)
469 xop0 = gen_lowpart_SUBREG (op_mode, xop0);
472 /* If the destination is a paradoxical subreg such that we need a
473 truncate to the inner mode, perform the insertion on a temporary and
474 truncate the result to the original destination. Note that we can't
475 just truncate the paradoxical subreg as (truncate:N (subreg:W (reg:N
476 X) 0)) is (reg:N X). */
477 if (GET_CODE (xop0) == SUBREG
478 && REG_P (SUBREG_REG (xop0))
479 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (SUBREG_REG (xop0)),
480 op_mode))
482 rtx tem = gen_reg_rtx (op_mode);
483 emit_move_insn (tem, xop0);
484 xop0 = tem;
485 copy_back = true;
488 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
489 "backwards" from the size of the unit we are inserting into.
490 Otherwise, we count bits from the most significant on a
491 BYTES/BITS_BIG_ENDIAN machine. */
493 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
494 bitnum = unit - bitsize - bitnum;
496 /* Convert VALUE to op_mode (which insv insn wants) in VALUE1. */
497 value1 = value;
498 if (GET_MODE (value) != op_mode)
500 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
502 /* Optimization: Don't bother really extending VALUE
503 if it has all the bits we will actually use. However,
504 if we must narrow it, be sure we do it correctly. */
506 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (op_mode))
508 rtx tmp;
510 tmp = simplify_subreg (op_mode, value1, GET_MODE (value), 0);
511 if (! tmp)
512 tmp = simplify_gen_subreg (op_mode,
513 force_reg (GET_MODE (value),
514 value1),
515 GET_MODE (value), 0);
516 value1 = tmp;
518 else
519 value1 = gen_lowpart (op_mode, value1);
521 else if (CONST_INT_P (value))
522 value1 = gen_int_mode (INTVAL (value), op_mode);
523 else
524 /* Parse phase is supposed to make VALUE's data type
525 match that of the component reference, which is a type
526 at least as wide as the field; so VALUE should have
527 a mode that corresponds to that type. */
528 gcc_assert (CONSTANT_P (value));
531 create_fixed_operand (&ops[0], xop0);
532 create_integer_operand (&ops[1], bitsize);
533 create_integer_operand (&ops[2], bitnum);
534 create_input_operand (&ops[3], value1, op_mode);
535 if (maybe_expand_insn (insv->icode, 4, ops))
537 if (copy_back)
538 convert_move (op0, xop0, true);
539 return true;
541 delete_insns_since (last);
542 return false;
545 /* A subroutine of store_bit_field, with the same arguments. Return true
546 if the operation could be implemented.
548 If FALLBACK_P is true, fall back to store_fixed_bit_field if we have
549 no other way of implementing the operation. If FALLBACK_P is false,
550 return false instead. */
552 static bool
553 store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
554 unsigned HOST_WIDE_INT bitnum,
555 unsigned HOST_WIDE_INT bitregion_start,
556 unsigned HOST_WIDE_INT bitregion_end,
557 enum machine_mode fieldmode,
558 rtx value, bool fallback_p)
560 rtx op0 = str_rtx;
561 rtx orig_value;
563 while (GET_CODE (op0) == SUBREG)
565 /* The following line once was done only if WORDS_BIG_ENDIAN,
566 but I think that is a mistake. WORDS_BIG_ENDIAN is
567 meaningful at a much higher level; when structures are copied
568 between memory and regs, the higher-numbered regs
569 always get higher addresses. */
570 int inner_mode_size = GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)));
571 int outer_mode_size = GET_MODE_SIZE (GET_MODE (op0));
572 int byte_offset = 0;
574 /* Paradoxical subregs need special handling on big endian machines. */
575 if (SUBREG_BYTE (op0) == 0 && inner_mode_size < outer_mode_size)
577 int difference = inner_mode_size - outer_mode_size;
579 if (WORDS_BIG_ENDIAN)
580 byte_offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
581 if (BYTES_BIG_ENDIAN)
582 byte_offset += difference % UNITS_PER_WORD;
584 else
585 byte_offset = SUBREG_BYTE (op0);
587 bitnum += byte_offset * BITS_PER_UNIT;
588 op0 = SUBREG_REG (op0);
591 /* No action is needed if the target is a register and if the field
592 lies completely outside that register. This can occur if the source
593 code contains an out-of-bounds access to a small array. */
594 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
595 return true;
597 /* Use vec_set patterns for inserting parts of vectors whenever
598 available. */
599 if (VECTOR_MODE_P (GET_MODE (op0))
600 && !MEM_P (op0)
601 && optab_handler (vec_set_optab, GET_MODE (op0)) != CODE_FOR_nothing
602 && fieldmode == GET_MODE_INNER (GET_MODE (op0))
603 && bitsize == GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
604 && !(bitnum % GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
606 struct expand_operand ops[3];
607 enum machine_mode outermode = GET_MODE (op0);
608 enum machine_mode innermode = GET_MODE_INNER (outermode);
609 enum insn_code icode = optab_handler (vec_set_optab, outermode);
610 int pos = bitnum / GET_MODE_BITSIZE (innermode);
612 create_fixed_operand (&ops[0], op0);
613 create_input_operand (&ops[1], value, innermode);
614 create_integer_operand (&ops[2], pos);
615 if (maybe_expand_insn (icode, 3, ops))
616 return true;
619 /* If the target is a register, overwriting the entire object, or storing
620 a full-word or multi-word field can be done with just a SUBREG. */
621 if (!MEM_P (op0)
622 && bitsize == GET_MODE_BITSIZE (fieldmode)
623 && ((bitsize == GET_MODE_BITSIZE (GET_MODE (op0)) && bitnum == 0)
624 || (bitsize % BITS_PER_WORD == 0 && bitnum % BITS_PER_WORD == 0)))
626 /* Use the subreg machinery either to narrow OP0 to the required
627 words or to cope with mode punning between equal-sized modes. */
628 rtx sub = simplify_gen_subreg (fieldmode, op0, GET_MODE (op0),
629 bitnum / BITS_PER_UNIT);
630 if (sub)
632 emit_move_insn (sub, value);
633 return true;
637 /* If the target is memory, storing any naturally aligned field can be
638 done with a simple store. For targets that support fast unaligned
639 memory, any naturally sized, unit aligned field can be done directly. */
640 if (simple_mem_bitfield_p (op0, bitsize, bitnum, fieldmode))
642 op0 = adjust_bitfield_address (op0, fieldmode, bitnum / BITS_PER_UNIT);
643 emit_move_insn (op0, value);
644 return true;
647 /* Make sure we are playing with integral modes. Pun with subregs
648 if we aren't. This must come after the entire register case above,
649 since that case is valid for any mode. The following cases are only
650 valid for integral modes. */
652 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
653 if (imode != GET_MODE (op0))
655 if (MEM_P (op0))
656 op0 = adjust_bitfield_address_size (op0, imode, 0, MEM_SIZE (op0));
657 else
659 gcc_assert (imode != BLKmode);
660 op0 = gen_lowpart (imode, op0);
665 /* Storing an lsb-aligned field in a register
666 can be done with a movstrict instruction. */
668 if (!MEM_P (op0)
669 && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
670 && bitsize == GET_MODE_BITSIZE (fieldmode)
671 && optab_handler (movstrict_optab, fieldmode) != CODE_FOR_nothing)
673 struct expand_operand ops[2];
674 enum insn_code icode = optab_handler (movstrict_optab, fieldmode);
675 rtx arg0 = op0;
676 unsigned HOST_WIDE_INT subreg_off;
678 if (GET_CODE (arg0) == SUBREG)
680 /* Else we've got some float mode source being extracted into
681 a different float mode destination -- this combination of
682 subregs results in Severe Tire Damage. */
683 gcc_assert (GET_MODE (SUBREG_REG (arg0)) == fieldmode
684 || GET_MODE_CLASS (fieldmode) == MODE_INT
685 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
686 arg0 = SUBREG_REG (arg0);
689 subreg_off = bitnum / BITS_PER_UNIT;
690 if (validate_subreg (fieldmode, GET_MODE (arg0), arg0, subreg_off))
692 arg0 = gen_rtx_SUBREG (fieldmode, arg0, subreg_off);
694 create_fixed_operand (&ops[0], arg0);
695 /* Shrink the source operand to FIELDMODE. */
696 create_convert_operand_to (&ops[1], value, fieldmode, false);
697 if (maybe_expand_insn (icode, 2, ops))
698 return true;
702 /* Handle fields bigger than a word. */
704 if (bitsize > BITS_PER_WORD)
706 /* Here we transfer the words of the field
707 in the order least significant first.
708 This is because the most significant word is the one which may
709 be less than full.
710 However, only do that if the value is not BLKmode. */
712 unsigned int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
713 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
714 unsigned int i;
715 rtx last;
717 /* This is the mode we must force value to, so that there will be enough
718 subwords to extract. Note that fieldmode will often (always?) be
719 VOIDmode, because that is what store_field uses to indicate that this
720 is a bit field, but passing VOIDmode to operand_subword_force
721 is not allowed. */
722 fieldmode = GET_MODE (value);
723 if (fieldmode == VOIDmode)
724 fieldmode = smallest_mode_for_size (nwords * BITS_PER_WORD, MODE_INT);
726 last = get_last_insn ();
727 for (i = 0; i < nwords; i++)
729 /* If I is 0, use the low-order word in both field and target;
730 if I is 1, use the next to lowest word; and so on. */
731 unsigned int wordnum = (backwards
732 ? GET_MODE_SIZE (fieldmode) / UNITS_PER_WORD
733 - i - 1
734 : i);
735 unsigned int bit_offset = (backwards
736 ? MAX ((int) bitsize - ((int) i + 1)
737 * BITS_PER_WORD,
739 : (int) i * BITS_PER_WORD);
740 rtx value_word = operand_subword_force (value, wordnum, fieldmode);
741 unsigned HOST_WIDE_INT new_bitsize =
742 MIN (BITS_PER_WORD, bitsize - i * BITS_PER_WORD);
744 /* If the remaining chunk doesn't have full wordsize we have
745 to make sure that for big endian machines the higher order
746 bits are used. */
747 if (new_bitsize < BITS_PER_WORD && BYTES_BIG_ENDIAN && !backwards)
748 value_word = simplify_expand_binop (word_mode, lshr_optab,
749 value_word,
750 GEN_INT (BITS_PER_WORD
751 - new_bitsize),
752 NULL_RTX, true,
753 OPTAB_LIB_WIDEN);
755 if (!store_bit_field_1 (op0, new_bitsize,
756 bitnum + bit_offset,
757 bitregion_start, bitregion_end,
758 word_mode,
759 value_word, fallback_p))
761 delete_insns_since (last);
762 return false;
765 return true;
768 /* If VALUE has a floating-point or complex mode, access it as an
769 integer of the corresponding size. This can occur on a machine
770 with 64 bit registers that uses SFmode for float. It can also
771 occur for unaligned float or complex fields. */
772 orig_value = value;
773 if (GET_MODE (value) != VOIDmode
774 && GET_MODE_CLASS (GET_MODE (value)) != MODE_INT
775 && GET_MODE_CLASS (GET_MODE (value)) != MODE_PARTIAL_INT)
777 value = gen_reg_rtx (int_mode_for_mode (GET_MODE (value)));
778 emit_move_insn (gen_lowpart (GET_MODE (orig_value), value), orig_value);
781 /* If OP0 is a multi-word register, narrow it to the affected word.
782 If the region spans two words, defer to store_split_bit_field. */
783 if (!MEM_P (op0) && GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
785 op0 = simplify_gen_subreg (word_mode, op0, GET_MODE (op0),
786 bitnum / BITS_PER_WORD * UNITS_PER_WORD);
787 gcc_assert (op0);
788 bitnum %= BITS_PER_WORD;
789 if (bitnum + bitsize > BITS_PER_WORD)
791 if (!fallback_p)
792 return false;
794 store_split_bit_field (op0, bitsize, bitnum, bitregion_start,
795 bitregion_end, value);
796 return true;
800 /* From here on we can assume that the field to be stored in fits
801 within a word. If the destination is a register, it too fits
802 in a word. */
804 extraction_insn insv;
805 if (!MEM_P (op0)
806 && get_best_reg_extraction_insn (&insv, EP_insv,
807 GET_MODE_BITSIZE (GET_MODE (op0)),
808 fieldmode)
809 && store_bit_field_using_insv (&insv, op0, bitsize, bitnum, value))
810 return true;
812 /* If OP0 is a memory, try copying it to a register and seeing if a
813 cheap register alternative is available. */
814 if (MEM_P (op0))
816 /* Do not use unaligned memory insvs for volatile bitfields when
817 -fstrict-volatile-bitfields is in effect. */
818 if (!(MEM_VOLATILE_P (op0)
819 && flag_strict_volatile_bitfields > 0)
820 && get_best_mem_extraction_insn (&insv, EP_insv, bitsize, bitnum,
821 fieldmode)
822 && store_bit_field_using_insv (&insv, op0, bitsize, bitnum, value))
823 return true;
825 rtx last = get_last_insn ();
827 /* Try loading part of OP0 into a register, inserting the bitfield
828 into that, and then copying the result back to OP0. */
829 unsigned HOST_WIDE_INT bitpos;
830 rtx xop0 = adjust_bit_field_mem_for_reg (EP_insv, op0, bitsize, bitnum,
831 bitregion_start, bitregion_end,
832 fieldmode, &bitpos);
833 if (xop0)
835 rtx tempreg = copy_to_reg (xop0);
836 if (store_bit_field_1 (tempreg, bitsize, bitpos,
837 bitregion_start, bitregion_end,
838 fieldmode, orig_value, false))
840 emit_move_insn (xop0, tempreg);
841 return true;
843 delete_insns_since (last);
847 if (!fallback_p)
848 return false;
850 store_fixed_bit_field (op0, bitsize, bitnum, bitregion_start,
851 bitregion_end, value);
852 return true;
855 /* Generate code to store value from rtx VALUE
856 into a bit-field within structure STR_RTX
857 containing BITSIZE bits starting at bit BITNUM.
859 BITREGION_START is bitpos of the first bitfield in this region.
860 BITREGION_END is the bitpos of the ending bitfield in this region.
861 These two fields are 0, if the C++ memory model does not apply,
862 or we are not interested in keeping track of bitfield regions.
864 FIELDMODE is the machine-mode of the FIELD_DECL node for this field. */
866 void
867 store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
868 unsigned HOST_WIDE_INT bitnum,
869 unsigned HOST_WIDE_INT bitregion_start,
870 unsigned HOST_WIDE_INT bitregion_end,
871 enum machine_mode fieldmode,
872 rtx value)
874 /* Under the C++0x memory model, we must not touch bits outside the
875 bit region. Adjust the address to start at the beginning of the
876 bit region. */
877 if (MEM_P (str_rtx) && bitregion_start > 0)
879 enum machine_mode bestmode;
880 HOST_WIDE_INT offset, size;
882 gcc_assert ((bitregion_start % BITS_PER_UNIT) == 0);
884 offset = bitregion_start / BITS_PER_UNIT;
885 bitnum -= bitregion_start;
886 size = (bitnum + bitsize + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
887 bitregion_end -= bitregion_start;
888 bitregion_start = 0;
889 bestmode = get_best_mode (bitsize, bitnum,
890 bitregion_start, bitregion_end,
891 MEM_ALIGN (str_rtx), VOIDmode,
892 MEM_VOLATILE_P (str_rtx));
893 str_rtx = adjust_bitfield_address_size (str_rtx, bestmode, offset, size);
896 if (!store_bit_field_1 (str_rtx, bitsize, bitnum,
897 bitregion_start, bitregion_end,
898 fieldmode, value, true))
899 gcc_unreachable ();
902 /* Use shifts and boolean operations to store VALUE into a bit field of
903 width BITSIZE in OP0, starting at bit BITNUM. */
905 static void
906 store_fixed_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
907 unsigned HOST_WIDE_INT bitnum,
908 unsigned HOST_WIDE_INT bitregion_start,
909 unsigned HOST_WIDE_INT bitregion_end,
910 rtx value)
912 enum machine_mode mode;
913 rtx temp;
914 int all_zero = 0;
915 int all_one = 0;
917 /* There is a case not handled here:
918 a structure with a known alignment of just a halfword
919 and a field split across two aligned halfwords within the structure.
920 Or likewise a structure with a known alignment of just a byte
921 and a field split across two bytes.
922 Such cases are not supposed to be able to occur. */
924 if (MEM_P (op0))
926 unsigned HOST_WIDE_INT maxbits = MAX_FIXED_MODE_SIZE;
928 if (bitregion_end)
929 maxbits = bitregion_end - bitregion_start + 1;
931 /* Get the proper mode to use for this field. We want a mode that
932 includes the entire field. If such a mode would be larger than
933 a word, we won't be doing the extraction the normal way.
934 We don't want a mode bigger than the destination. */
936 mode = GET_MODE (op0);
937 if (GET_MODE_BITSIZE (mode) == 0
938 || GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (word_mode))
939 mode = word_mode;
941 if (MEM_VOLATILE_P (op0)
942 && GET_MODE_BITSIZE (GET_MODE (op0)) > 0
943 && GET_MODE_BITSIZE (GET_MODE (op0)) <= maxbits
944 && flag_strict_volatile_bitfields > 0)
945 mode = GET_MODE (op0);
946 else
947 mode = get_best_mode (bitsize, bitnum, bitregion_start, bitregion_end,
948 MEM_ALIGN (op0), mode, MEM_VOLATILE_P (op0));
950 if (mode == VOIDmode)
952 /* The only way this should occur is if the field spans word
953 boundaries. */
954 store_split_bit_field (op0, bitsize, bitnum, bitregion_start,
955 bitregion_end, value);
956 return;
959 op0 = narrow_bit_field_mem (op0, mode, bitsize, bitnum, &bitnum);
962 mode = GET_MODE (op0);
963 gcc_assert (SCALAR_INT_MODE_P (mode));
965 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
966 for invalid input, such as f5 from gcc.dg/pr48335-2.c. */
968 if (BYTES_BIG_ENDIAN)
969 /* BITNUM is the distance between our msb
970 and that of the containing datum.
971 Convert it to the distance from the lsb. */
972 bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
974 /* Now BITNUM is always the distance between our lsb
975 and that of OP0. */
977 /* Shift VALUE left by BITNUM bits. If VALUE is not constant,
978 we must first convert its mode to MODE. */
980 if (CONST_INT_P (value))
982 HOST_WIDE_INT v = INTVAL (value);
984 if (bitsize < HOST_BITS_PER_WIDE_INT)
985 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
987 if (v == 0)
988 all_zero = 1;
989 else if ((bitsize < HOST_BITS_PER_WIDE_INT
990 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
991 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
992 all_one = 1;
994 value = lshift_value (mode, value, bitnum, bitsize);
996 else
998 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
999 && bitnum + bitsize != GET_MODE_BITSIZE (mode));
1001 if (GET_MODE (value) != mode)
1002 value = convert_to_mode (mode, value, 1);
1004 if (must_and)
1005 value = expand_binop (mode, and_optab, value,
1006 mask_rtx (mode, 0, bitsize, 0),
1007 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1008 if (bitnum > 0)
1009 value = expand_shift (LSHIFT_EXPR, mode, value,
1010 bitnum, NULL_RTX, 1);
1013 /* Now clear the chosen bits in OP0,
1014 except that if VALUE is -1 we need not bother. */
1015 /* We keep the intermediates in registers to allow CSE to combine
1016 consecutive bitfield assignments. */
1018 temp = force_reg (mode, op0);
1020 if (! all_one)
1022 temp = expand_binop (mode, and_optab, temp,
1023 mask_rtx (mode, bitnum, bitsize, 1),
1024 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1025 temp = force_reg (mode, temp);
1028 /* Now logical-or VALUE into OP0, unless it is zero. */
1030 if (! all_zero)
1032 temp = expand_binop (mode, ior_optab, temp, value,
1033 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1034 temp = force_reg (mode, temp);
1037 if (op0 != temp)
1039 op0 = copy_rtx (op0);
1040 emit_move_insn (op0, temp);
1044 /* Store a bit field that is split across multiple accessible memory objects.
1046 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
1047 BITSIZE is the field width; BITPOS the position of its first bit
1048 (within the word).
1049 VALUE is the value to store.
1051 This does not yet handle fields wider than BITS_PER_WORD. */
1053 static void
1054 store_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
1055 unsigned HOST_WIDE_INT bitpos,
1056 unsigned HOST_WIDE_INT bitregion_start,
1057 unsigned HOST_WIDE_INT bitregion_end,
1058 rtx value)
1060 unsigned int unit;
1061 unsigned int bitsdone = 0;
1063 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1064 much at a time. */
1065 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
1066 unit = BITS_PER_WORD;
1067 else
1068 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
1070 /* If VALUE is a constant other than a CONST_INT, get it into a register in
1071 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
1072 that VALUE might be a floating-point constant. */
1073 if (CONSTANT_P (value) && !CONST_INT_P (value))
1075 rtx word = gen_lowpart_common (word_mode, value);
1077 if (word && (value != word))
1078 value = word;
1079 else
1080 value = gen_lowpart_common (word_mode,
1081 force_reg (GET_MODE (value) != VOIDmode
1082 ? GET_MODE (value)
1083 : word_mode, value));
1086 while (bitsdone < bitsize)
1088 unsigned HOST_WIDE_INT thissize;
1089 rtx part, word;
1090 unsigned HOST_WIDE_INT thispos;
1091 unsigned HOST_WIDE_INT offset;
1093 offset = (bitpos + bitsdone) / unit;
1094 thispos = (bitpos + bitsdone) % unit;
1096 /* When region of bytes we can touch is restricted, decrease
1097 UNIT close to the end of the region as needed. If op0 is a REG
1098 or SUBREG of REG, don't do this, as there can't be data races
1099 on a register and we can expand shorter code in some cases. */
1100 if (bitregion_end
1101 && unit > BITS_PER_UNIT
1102 && bitpos + bitsdone - thispos + unit > bitregion_end + 1
1103 && !REG_P (op0)
1104 && (GET_CODE (op0) != SUBREG || !REG_P (SUBREG_REG (op0))))
1106 unit = unit / 2;
1107 continue;
1110 /* THISSIZE must not overrun a word boundary. Otherwise,
1111 store_fixed_bit_field will call us again, and we will mutually
1112 recurse forever. */
1113 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1114 thissize = MIN (thissize, unit - thispos);
1116 if (BYTES_BIG_ENDIAN)
1118 /* Fetch successively less significant portions. */
1119 if (CONST_INT_P (value))
1120 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1121 >> (bitsize - bitsdone - thissize))
1122 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1123 else
1125 int total_bits = GET_MODE_BITSIZE (GET_MODE (value));
1126 /* The args are chosen so that the last part includes the
1127 lsb. Give extract_bit_field the value it needs (with
1128 endianness compensation) to fetch the piece we want. */
1129 part = extract_fixed_bit_field (word_mode, value, thissize,
1130 total_bits - bitsize + bitsdone,
1131 NULL_RTX, 1, false);
1134 else
1136 /* Fetch successively more significant portions. */
1137 if (CONST_INT_P (value))
1138 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1139 >> bitsdone)
1140 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1141 else
1142 part = extract_fixed_bit_field (word_mode, value, thissize,
1143 bitsdone, NULL_RTX, 1, false);
1146 /* If OP0 is a register, then handle OFFSET here.
1148 When handling multiword bitfields, extract_bit_field may pass
1149 down a word_mode SUBREG of a larger REG for a bitfield that actually
1150 crosses a word boundary. Thus, for a SUBREG, we must find
1151 the current word starting from the base register. */
1152 if (GET_CODE (op0) == SUBREG)
1154 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD)
1155 + (offset * unit / BITS_PER_WORD);
1156 enum machine_mode sub_mode = GET_MODE (SUBREG_REG (op0));
1157 if (sub_mode != BLKmode && GET_MODE_SIZE (sub_mode) < UNITS_PER_WORD)
1158 word = word_offset ? const0_rtx : op0;
1159 else
1160 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1161 GET_MODE (SUBREG_REG (op0)));
1162 offset &= BITS_PER_WORD / unit - 1;
1164 else if (REG_P (op0))
1166 enum machine_mode op0_mode = GET_MODE (op0);
1167 if (op0_mode != BLKmode && GET_MODE_SIZE (op0_mode) < UNITS_PER_WORD)
1168 word = offset ? const0_rtx : op0;
1169 else
1170 word = operand_subword_force (op0, offset * unit / BITS_PER_WORD,
1171 GET_MODE (op0));
1172 offset &= BITS_PER_WORD / unit - 1;
1174 else
1175 word = op0;
1177 /* OFFSET is in UNITs, and UNIT is in bits. If WORD is const0_rtx,
1178 it is just an out-of-bounds access. Ignore it. */
1179 if (word != const0_rtx)
1180 store_fixed_bit_field (word, thissize, offset * unit + thispos,
1181 bitregion_start, bitregion_end, part);
1182 bitsdone += thissize;
1186 /* A subroutine of extract_bit_field_1 that converts return value X
1187 to either MODE or TMODE. MODE, TMODE and UNSIGNEDP are arguments
1188 to extract_bit_field. */
1190 static rtx
1191 convert_extracted_bit_field (rtx x, enum machine_mode mode,
1192 enum machine_mode tmode, bool unsignedp)
1194 if (GET_MODE (x) == tmode || GET_MODE (x) == mode)
1195 return x;
1197 /* If the x mode is not a scalar integral, first convert to the
1198 integer mode of that size and then access it as a floating-point
1199 value via a SUBREG. */
1200 if (!SCALAR_INT_MODE_P (tmode))
1202 enum machine_mode smode;
1204 smode = mode_for_size (GET_MODE_BITSIZE (tmode), MODE_INT, 0);
1205 x = convert_to_mode (smode, x, unsignedp);
1206 x = force_reg (smode, x);
1207 return gen_lowpart (tmode, x);
1210 return convert_to_mode (tmode, x, unsignedp);
1213 /* Try to use an ext(z)v pattern to extract a field from OP0.
1214 Return the extracted value on success, otherwise return null.
1215 EXT_MODE is the mode of the extraction and the other arguments
1216 are as for extract_bit_field. */
1218 static rtx
1219 extract_bit_field_using_extv (const extraction_insn *extv, rtx op0,
1220 unsigned HOST_WIDE_INT bitsize,
1221 unsigned HOST_WIDE_INT bitnum,
1222 int unsignedp, rtx target,
1223 enum machine_mode mode, enum machine_mode tmode)
1225 struct expand_operand ops[4];
1226 rtx spec_target = target;
1227 rtx spec_target_subreg = 0;
1228 enum machine_mode ext_mode = extv->field_mode;
1229 unsigned unit = GET_MODE_BITSIZE (ext_mode);
1231 if (bitsize == 0 || unit < bitsize)
1232 return NULL_RTX;
1234 if (MEM_P (op0))
1235 /* Get a reference to the first byte of the field. */
1236 op0 = narrow_bit_field_mem (op0, extv->struct_mode, bitsize, bitnum,
1237 &bitnum);
1238 else
1240 /* Convert from counting within OP0 to counting in EXT_MODE. */
1241 if (BYTES_BIG_ENDIAN)
1242 bitnum += unit - GET_MODE_BITSIZE (GET_MODE (op0));
1244 /* If op0 is a register, we need it in EXT_MODE to make it
1245 acceptable to the format of ext(z)v. */
1246 if (GET_CODE (op0) == SUBREG && GET_MODE (op0) != ext_mode)
1247 return NULL_RTX;
1248 if (REG_P (op0) && GET_MODE (op0) != ext_mode)
1249 op0 = gen_lowpart_SUBREG (ext_mode, op0);
1252 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
1253 "backwards" from the size of the unit we are extracting from.
1254 Otherwise, we count bits from the most significant on a
1255 BYTES/BITS_BIG_ENDIAN machine. */
1257 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1258 bitnum = unit - bitsize - bitnum;
1260 if (target == 0)
1261 target = spec_target = gen_reg_rtx (tmode);
1263 if (GET_MODE (target) != ext_mode)
1265 /* Don't use LHS paradoxical subreg if explicit truncation is needed
1266 between the mode of the extraction (word_mode) and the target
1267 mode. Instead, create a temporary and use convert_move to set
1268 the target. */
1269 if (REG_P (target)
1270 && TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (target), ext_mode))
1272 target = gen_lowpart (ext_mode, target);
1273 if (GET_MODE_PRECISION (ext_mode)
1274 > GET_MODE_PRECISION (GET_MODE (spec_target)))
1275 spec_target_subreg = target;
1277 else
1278 target = gen_reg_rtx (ext_mode);
1281 create_output_operand (&ops[0], target, ext_mode);
1282 create_fixed_operand (&ops[1], op0);
1283 create_integer_operand (&ops[2], bitsize);
1284 create_integer_operand (&ops[3], bitnum);
1285 if (maybe_expand_insn (extv->icode, 4, ops))
1287 target = ops[0].value;
1288 if (target == spec_target)
1289 return target;
1290 if (target == spec_target_subreg)
1291 return spec_target;
1292 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1294 return NULL_RTX;
1297 /* A subroutine of extract_bit_field, with the same arguments.
1298 If FALLBACK_P is true, fall back to extract_fixed_bit_field
1299 if we can find no other means of implementing the operation.
1300 if FALLBACK_P is false, return NULL instead. */
1302 static rtx
1303 extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1304 unsigned HOST_WIDE_INT bitnum,
1305 int unsignedp, bool packedp, rtx target,
1306 enum machine_mode mode, enum machine_mode tmode,
1307 bool fallback_p)
1309 rtx op0 = str_rtx;
1310 enum machine_mode int_mode;
1311 enum machine_mode mode1;
1313 if (tmode == VOIDmode)
1314 tmode = mode;
1316 while (GET_CODE (op0) == SUBREG)
1318 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1319 op0 = SUBREG_REG (op0);
1322 /* If we have an out-of-bounds access to a register, just return an
1323 uninitialized register of the required mode. This can occur if the
1324 source code contains an out-of-bounds access to a small array. */
1325 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
1326 return gen_reg_rtx (tmode);
1328 if (REG_P (op0)
1329 && mode == GET_MODE (op0)
1330 && bitnum == 0
1331 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
1333 /* We're trying to extract a full register from itself. */
1334 return op0;
1337 /* See if we can get a better vector mode before extracting. */
1338 if (VECTOR_MODE_P (GET_MODE (op0))
1339 && !MEM_P (op0)
1340 && GET_MODE_INNER (GET_MODE (op0)) != tmode)
1342 enum machine_mode new_mode;
1344 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1345 new_mode = MIN_MODE_VECTOR_FLOAT;
1346 else if (GET_MODE_CLASS (tmode) == MODE_FRACT)
1347 new_mode = MIN_MODE_VECTOR_FRACT;
1348 else if (GET_MODE_CLASS (tmode) == MODE_UFRACT)
1349 new_mode = MIN_MODE_VECTOR_UFRACT;
1350 else if (GET_MODE_CLASS (tmode) == MODE_ACCUM)
1351 new_mode = MIN_MODE_VECTOR_ACCUM;
1352 else if (GET_MODE_CLASS (tmode) == MODE_UACCUM)
1353 new_mode = MIN_MODE_VECTOR_UACCUM;
1354 else
1355 new_mode = MIN_MODE_VECTOR_INT;
1357 for (; new_mode != VOIDmode ; new_mode = GET_MODE_WIDER_MODE (new_mode))
1358 if (GET_MODE_SIZE (new_mode) == GET_MODE_SIZE (GET_MODE (op0))
1359 && targetm.vector_mode_supported_p (new_mode))
1360 break;
1361 if (new_mode != VOIDmode)
1362 op0 = gen_lowpart (new_mode, op0);
1365 /* Use vec_extract patterns for extracting parts of vectors whenever
1366 available. */
1367 if (VECTOR_MODE_P (GET_MODE (op0))
1368 && !MEM_P (op0)
1369 && optab_handler (vec_extract_optab, GET_MODE (op0)) != CODE_FOR_nothing
1370 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
1371 == bitnum / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
1373 struct expand_operand ops[3];
1374 enum machine_mode outermode = GET_MODE (op0);
1375 enum machine_mode innermode = GET_MODE_INNER (outermode);
1376 enum insn_code icode = optab_handler (vec_extract_optab, outermode);
1377 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1379 create_output_operand (&ops[0], target, innermode);
1380 create_input_operand (&ops[1], op0, outermode);
1381 create_integer_operand (&ops[2], pos);
1382 if (maybe_expand_insn (icode, 3, ops))
1384 target = ops[0].value;
1385 if (GET_MODE (target) != mode)
1386 return gen_lowpart (tmode, target);
1387 return target;
1391 /* Make sure we are playing with integral modes. Pun with subregs
1392 if we aren't. */
1394 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
1395 if (imode != GET_MODE (op0))
1397 if (MEM_P (op0))
1398 op0 = adjust_bitfield_address_size (op0, imode, 0, MEM_SIZE (op0));
1399 else if (imode != BLKmode)
1401 op0 = gen_lowpart (imode, op0);
1403 /* If we got a SUBREG, force it into a register since we
1404 aren't going to be able to do another SUBREG on it. */
1405 if (GET_CODE (op0) == SUBREG)
1406 op0 = force_reg (imode, op0);
1408 else if (REG_P (op0))
1410 rtx reg, subreg;
1411 imode = smallest_mode_for_size (GET_MODE_BITSIZE (GET_MODE (op0)),
1412 MODE_INT);
1413 reg = gen_reg_rtx (imode);
1414 subreg = gen_lowpart_SUBREG (GET_MODE (op0), reg);
1415 emit_move_insn (subreg, op0);
1416 op0 = reg;
1417 bitnum += SUBREG_BYTE (subreg) * BITS_PER_UNIT;
1419 else
1421 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (op0));
1422 rtx mem = assign_stack_temp (GET_MODE (op0), size);
1423 emit_move_insn (mem, op0);
1424 op0 = adjust_bitfield_address_size (mem, BLKmode, 0, size);
1429 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1430 If that's wrong, the solution is to test for it and set TARGET to 0
1431 if needed. */
1433 /* If the bitfield is volatile, we need to make sure the access
1434 remains on a type-aligned boundary. */
1435 if (GET_CODE (op0) == MEM
1436 && MEM_VOLATILE_P (op0)
1437 && GET_MODE_BITSIZE (GET_MODE (op0)) > 0
1438 && flag_strict_volatile_bitfields > 0)
1439 goto no_subreg_mode_swap;
1441 /* Only scalar integer modes can be converted via subregs. There is an
1442 additional problem for FP modes here in that they can have a precision
1443 which is different from the size. mode_for_size uses precision, but
1444 we want a mode based on the size, so we must avoid calling it for FP
1445 modes. */
1446 mode1 = mode;
1447 if (SCALAR_INT_MODE_P (tmode))
1449 enum machine_mode try_mode = mode_for_size (bitsize,
1450 GET_MODE_CLASS (tmode), 0);
1451 if (try_mode != BLKmode)
1452 mode1 = try_mode;
1454 gcc_assert (mode1 != BLKmode);
1456 /* Extraction of a full MODE1 value can be done with a subreg as long
1457 as the least significant bit of the value is the least significant
1458 bit of either OP0 or a word of OP0. */
1459 if (!MEM_P (op0)
1460 && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
1461 && bitsize == GET_MODE_BITSIZE (mode1)
1462 && TRULY_NOOP_TRUNCATION_MODES_P (mode1, GET_MODE (op0)))
1464 rtx sub = simplify_gen_subreg (mode1, op0, GET_MODE (op0),
1465 bitnum / BITS_PER_UNIT);
1466 if (sub)
1467 return convert_extracted_bit_field (sub, mode, tmode, unsignedp);
1470 /* Extraction of a full MODE1 value can be done with a load as long as
1471 the field is on a byte boundary and is sufficiently aligned. */
1472 if (simple_mem_bitfield_p (op0, bitsize, bitnum, mode1))
1474 op0 = adjust_bitfield_address (op0, mode1, bitnum / BITS_PER_UNIT);
1475 return convert_extracted_bit_field (op0, mode, tmode, unsignedp);
1478 no_subreg_mode_swap:
1480 /* Handle fields bigger than a word. */
1482 if (bitsize > BITS_PER_WORD)
1484 /* Here we transfer the words of the field
1485 in the order least significant first.
1486 This is because the most significant word is the one which may
1487 be less than full. */
1489 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1490 unsigned int i;
1491 rtx last;
1493 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1494 target = gen_reg_rtx (mode);
1496 /* Indicate for flow that the entire target reg is being set. */
1497 emit_clobber (target);
1499 last = get_last_insn ();
1500 for (i = 0; i < nwords; i++)
1502 /* If I is 0, use the low-order word in both field and target;
1503 if I is 1, use the next to lowest word; and so on. */
1504 /* Word number in TARGET to use. */
1505 unsigned int wordnum
1506 = (WORDS_BIG_ENDIAN
1507 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1508 : i);
1509 /* Offset from start of field in OP0. */
1510 unsigned int bit_offset = (WORDS_BIG_ENDIAN
1511 ? MAX (0, ((int) bitsize - ((int) i + 1)
1512 * (int) BITS_PER_WORD))
1513 : (int) i * BITS_PER_WORD);
1514 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1515 rtx result_part
1516 = extract_bit_field_1 (op0, MIN (BITS_PER_WORD,
1517 bitsize - i * BITS_PER_WORD),
1518 bitnum + bit_offset, 1, false, target_part,
1519 mode, word_mode, fallback_p);
1521 gcc_assert (target_part);
1522 if (!result_part)
1524 delete_insns_since (last);
1525 return NULL;
1528 if (result_part != target_part)
1529 emit_move_insn (target_part, result_part);
1532 if (unsignedp)
1534 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1535 need to be zero'd out. */
1536 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1538 unsigned int i, total_words;
1540 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1541 for (i = nwords; i < total_words; i++)
1542 emit_move_insn
1543 (operand_subword (target,
1544 WORDS_BIG_ENDIAN ? total_words - i - 1 : i,
1545 1, VOIDmode),
1546 const0_rtx);
1548 return target;
1551 /* Signed bit field: sign-extend with two arithmetic shifts. */
1552 target = expand_shift (LSHIFT_EXPR, mode, target,
1553 GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
1554 return expand_shift (RSHIFT_EXPR, mode, target,
1555 GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
1558 /* If OP0 is a multi-word register, narrow it to the affected word.
1559 If the region spans two words, defer to extract_split_bit_field. */
1560 if (!MEM_P (op0) && GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1562 op0 = simplify_gen_subreg (word_mode, op0, GET_MODE (op0),
1563 bitnum / BITS_PER_WORD * UNITS_PER_WORD);
1564 bitnum %= BITS_PER_WORD;
1565 if (bitnum + bitsize > BITS_PER_WORD)
1567 if (!fallback_p)
1568 return NULL_RTX;
1569 target = extract_split_bit_field (op0, bitsize, bitnum, unsignedp);
1570 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1574 /* From here on we know the desired field is smaller than a word.
1575 If OP0 is a register, it too fits within a word. */
1576 enum extraction_pattern pattern = unsignedp ? EP_extzv : EP_extv;
1577 extraction_insn extv;
1578 if (!MEM_P (op0)
1579 /* ??? We could limit the structure size to the part of OP0 that
1580 contains the field, with appropriate checks for endianness
1581 and TRULY_NOOP_TRUNCATION. */
1582 && get_best_reg_extraction_insn (&extv, pattern,
1583 GET_MODE_BITSIZE (GET_MODE (op0)),
1584 tmode))
1586 rtx result = extract_bit_field_using_extv (&extv, op0, bitsize, bitnum,
1587 unsignedp, target, mode,
1588 tmode);
1589 if (result)
1590 return result;
1593 /* If OP0 is a memory, try copying it to a register and seeing if a
1594 cheap register alternative is available. */
1595 if (MEM_P (op0))
1597 /* Do not use extv/extzv for volatile bitfields when
1598 -fstrict-volatile-bitfields is in effect. */
1599 if (!(MEM_VOLATILE_P (op0) && flag_strict_volatile_bitfields > 0)
1600 && get_best_mem_extraction_insn (&extv, pattern, bitsize, bitnum,
1601 tmode))
1603 rtx result = extract_bit_field_using_extv (&extv, op0, bitsize,
1604 bitnum, unsignedp,
1605 target, mode,
1606 tmode);
1607 if (result)
1608 return result;
1611 rtx last = get_last_insn ();
1613 /* Try loading part of OP0 into a register and extracting the
1614 bitfield from that. */
1615 unsigned HOST_WIDE_INT bitpos;
1616 rtx xop0 = adjust_bit_field_mem_for_reg (pattern, op0, bitsize, bitnum,
1617 0, 0, tmode, &bitpos);
1618 if (xop0)
1620 xop0 = copy_to_reg (xop0);
1621 rtx result = extract_bit_field_1 (xop0, bitsize, bitpos,
1622 unsignedp, packedp, target,
1623 mode, tmode, false);
1624 if (result)
1625 return result;
1626 delete_insns_since (last);
1630 if (!fallback_p)
1631 return NULL;
1633 /* Find a correspondingly-sized integer field, so we can apply
1634 shifts and masks to it. */
1635 int_mode = int_mode_for_mode (tmode);
1636 if (int_mode == BLKmode)
1637 int_mode = int_mode_for_mode (mode);
1638 /* Should probably push op0 out to memory and then do a load. */
1639 gcc_assert (int_mode != BLKmode);
1641 target = extract_fixed_bit_field (int_mode, op0, bitsize, bitnum,
1642 target, unsignedp, packedp);
1643 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1646 /* Generate code to extract a byte-field from STR_RTX
1647 containing BITSIZE bits, starting at BITNUM,
1648 and put it in TARGET if possible (if TARGET is nonzero).
1649 Regardless of TARGET, we return the rtx for where the value is placed.
1651 STR_RTX is the structure containing the byte (a REG or MEM).
1652 UNSIGNEDP is nonzero if this is an unsigned bit field.
1653 PACKEDP is nonzero if the field has the packed attribute.
1654 MODE is the natural mode of the field value once extracted.
1655 TMODE is the mode the caller would like the value to have;
1656 but the value may be returned with type MODE instead.
1658 If a TARGET is specified and we can store in it at no extra cost,
1659 we do so, and return TARGET.
1660 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1661 if they are equally easy. */
1664 extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1665 unsigned HOST_WIDE_INT bitnum, int unsignedp, bool packedp,
1666 rtx target, enum machine_mode mode, enum machine_mode tmode)
1668 return extract_bit_field_1 (str_rtx, bitsize, bitnum, unsignedp, packedp,
1669 target, mode, tmode, true);
1672 /* Use shifts and boolean operations to extract a field of BITSIZE bits
1673 from bit BITNUM of OP0.
1675 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1676 PACKEDP is true if the field has the packed attribute.
1678 If TARGET is nonzero, attempts to store the value there
1679 and return TARGET, but this is not guaranteed.
1680 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
1682 static rtx
1683 extract_fixed_bit_field (enum machine_mode tmode, rtx op0,
1684 unsigned HOST_WIDE_INT bitsize,
1685 unsigned HOST_WIDE_INT bitnum, rtx target,
1686 int unsignedp, bool packedp)
1688 enum machine_mode mode;
1690 if (MEM_P (op0))
1692 /* Get the proper mode to use for this field. We want a mode that
1693 includes the entire field. If such a mode would be larger than
1694 a word, we won't be doing the extraction the normal way. */
1696 if (MEM_VOLATILE_P (op0)
1697 && flag_strict_volatile_bitfields > 0)
1699 if (GET_MODE_BITSIZE (GET_MODE (op0)) > 0)
1700 mode = GET_MODE (op0);
1701 else if (target && GET_MODE_BITSIZE (GET_MODE (target)) > 0)
1702 mode = GET_MODE (target);
1703 else
1704 mode = tmode;
1706 else
1707 mode = get_best_mode (bitsize, bitnum, 0, 0,
1708 MEM_ALIGN (op0), word_mode, MEM_VOLATILE_P (op0));
1710 if (mode == VOIDmode)
1711 /* The only way this should occur is if the field spans word
1712 boundaries. */
1713 return extract_split_bit_field (op0, bitsize, bitnum, unsignedp);
1715 unsigned int total_bits = GET_MODE_BITSIZE (mode);
1716 HOST_WIDE_INT bit_offset = bitnum - bitnum % total_bits;
1718 /* If we're accessing a volatile MEM, we can't apply BIT_OFFSET
1719 if it results in a multi-word access where we otherwise wouldn't
1720 have one. So, check for that case here. */
1721 if (MEM_P (op0)
1722 && MEM_VOLATILE_P (op0)
1723 && flag_strict_volatile_bitfields > 0
1724 && bitnum % BITS_PER_UNIT + bitsize <= total_bits
1725 && bitnum % GET_MODE_BITSIZE (mode) + bitsize > total_bits)
1727 if (STRICT_ALIGNMENT)
1729 static bool informed_about_misalignment = false;
1731 if (packedp)
1733 if (bitsize == total_bits)
1734 warning_at (input_location, OPT_fstrict_volatile_bitfields,
1735 "multiple accesses to volatile structure"
1736 " member because of packed attribute");
1737 else
1738 warning_at (input_location, OPT_fstrict_volatile_bitfields,
1739 "multiple accesses to volatile structure"
1740 " bitfield because of packed attribute");
1742 return extract_split_bit_field (op0, bitsize, bitnum,
1743 unsignedp);
1746 if (bitsize == total_bits)
1747 warning_at (input_location, OPT_fstrict_volatile_bitfields,
1748 "mis-aligned access used for structure member");
1749 else
1750 warning_at (input_location, OPT_fstrict_volatile_bitfields,
1751 "mis-aligned access used for structure bitfield");
1753 if (! informed_about_misalignment)
1755 informed_about_misalignment = true;
1756 inform (input_location,
1757 "when a volatile object spans multiple type-sized"
1758 " locations, the compiler must choose between using"
1759 " a single mis-aligned access to preserve the"
1760 " volatility, or using multiple aligned accesses"
1761 " to avoid runtime faults; this code may fail at"
1762 " runtime if the hardware does not allow this"
1763 " access");
1766 bit_offset = bitnum - bitnum % BITS_PER_UNIT;
1768 op0 = adjust_bitfield_address (op0, mode, bit_offset / BITS_PER_UNIT);
1769 bitnum -= bit_offset;
1772 mode = GET_MODE (op0);
1773 gcc_assert (SCALAR_INT_MODE_P (mode));
1775 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
1776 for invalid input, such as extract equivalent of f5 from
1777 gcc.dg/pr48335-2.c. */
1779 if (BYTES_BIG_ENDIAN)
1780 /* BITNUM is the distance between our msb and that of OP0.
1781 Convert it to the distance from the lsb. */
1782 bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
1784 /* Now BITNUM is always the distance between the field's lsb and that of OP0.
1785 We have reduced the big-endian case to the little-endian case. */
1787 if (unsignedp)
1789 if (bitnum)
1791 /* If the field does not already start at the lsb,
1792 shift it so it does. */
1793 /* Maybe propagate the target for the shift. */
1794 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1795 if (tmode != mode)
1796 subtarget = 0;
1797 op0 = expand_shift (RSHIFT_EXPR, mode, op0, bitnum, subtarget, 1);
1799 /* Convert the value to the desired mode. */
1800 if (mode != tmode)
1801 op0 = convert_to_mode (tmode, op0, 1);
1803 /* Unless the msb of the field used to be the msb when we shifted,
1804 mask out the upper bits. */
1806 if (GET_MODE_BITSIZE (mode) != bitnum + bitsize)
1807 return expand_binop (GET_MODE (op0), and_optab, op0,
1808 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1809 target, 1, OPTAB_LIB_WIDEN);
1810 return op0;
1813 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1814 then arithmetic-shift its lsb to the lsb of the word. */
1815 op0 = force_reg (mode, op0);
1817 /* Find the narrowest integer mode that contains the field. */
1819 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1820 mode = GET_MODE_WIDER_MODE (mode))
1821 if (GET_MODE_BITSIZE (mode) >= bitsize + bitnum)
1823 op0 = convert_to_mode (mode, op0, 0);
1824 break;
1827 if (mode != tmode)
1828 target = 0;
1830 if (GET_MODE_BITSIZE (mode) != (bitsize + bitnum))
1832 int amount = GET_MODE_BITSIZE (mode) - (bitsize + bitnum);
1833 /* Maybe propagate the target for the shift. */
1834 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1835 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1838 return expand_shift (RSHIFT_EXPR, mode, op0,
1839 GET_MODE_BITSIZE (mode) - bitsize, target, 0);
1842 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1843 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1844 complement of that if COMPLEMENT. The mask is truncated if
1845 necessary to the width of mode MODE. The mask is zero-extended if
1846 BITSIZE+BITPOS is too small for MODE. */
1848 static rtx
1849 mask_rtx (enum machine_mode mode, int bitpos, int bitsize, int complement)
1851 double_int mask;
1853 mask = double_int::mask (bitsize);
1854 mask = mask.llshift (bitpos, HOST_BITS_PER_DOUBLE_INT);
1856 if (complement)
1857 mask = ~mask;
1859 return immed_double_int_const (mask, mode);
1862 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1863 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1865 static rtx
1866 lshift_value (enum machine_mode mode, rtx value, int bitpos, int bitsize)
1868 double_int val;
1870 val = double_int::from_uhwi (INTVAL (value)).zext (bitsize);
1871 val = val.llshift (bitpos, HOST_BITS_PER_DOUBLE_INT);
1873 return immed_double_int_const (val, mode);
1876 /* Extract a bit field that is split across two words
1877 and return an RTX for the result.
1879 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1880 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1881 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
1883 static rtx
1884 extract_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
1885 unsigned HOST_WIDE_INT bitpos, int unsignedp)
1887 unsigned int unit;
1888 unsigned int bitsdone = 0;
1889 rtx result = NULL_RTX;
1890 int first = 1;
1892 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1893 much at a time. */
1894 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
1895 unit = BITS_PER_WORD;
1896 else
1897 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
1899 while (bitsdone < bitsize)
1901 unsigned HOST_WIDE_INT thissize;
1902 rtx part, word;
1903 unsigned HOST_WIDE_INT thispos;
1904 unsigned HOST_WIDE_INT offset;
1906 offset = (bitpos + bitsdone) / unit;
1907 thispos = (bitpos + bitsdone) % unit;
1909 /* THISSIZE must not overrun a word boundary. Otherwise,
1910 extract_fixed_bit_field will call us again, and we will mutually
1911 recurse forever. */
1912 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1913 thissize = MIN (thissize, unit - thispos);
1915 /* If OP0 is a register, then handle OFFSET here.
1917 When handling multiword bitfields, extract_bit_field may pass
1918 down a word_mode SUBREG of a larger REG for a bitfield that actually
1919 crosses a word boundary. Thus, for a SUBREG, we must find
1920 the current word starting from the base register. */
1921 if (GET_CODE (op0) == SUBREG)
1923 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
1924 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1925 GET_MODE (SUBREG_REG (op0)));
1926 offset = 0;
1928 else if (REG_P (op0))
1930 word = operand_subword_force (op0, offset, GET_MODE (op0));
1931 offset = 0;
1933 else
1934 word = op0;
1936 /* Extract the parts in bit-counting order,
1937 whose meaning is determined by BYTES_PER_UNIT.
1938 OFFSET is in UNITs, and UNIT is in bits. */
1939 part = extract_fixed_bit_field (word_mode, word, thissize,
1940 offset * unit + thispos, 0, 1, false);
1941 bitsdone += thissize;
1943 /* Shift this part into place for the result. */
1944 if (BYTES_BIG_ENDIAN)
1946 if (bitsize != bitsdone)
1947 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1948 bitsize - bitsdone, 0, 1);
1950 else
1952 if (bitsdone != thissize)
1953 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1954 bitsdone - thissize, 0, 1);
1957 if (first)
1958 result = part;
1959 else
1960 /* Combine the parts with bitwise or. This works
1961 because we extracted each part as an unsigned bit field. */
1962 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
1963 OPTAB_LIB_WIDEN);
1965 first = 0;
1968 /* Unsigned bit field: we are done. */
1969 if (unsignedp)
1970 return result;
1971 /* Signed bit field: sign-extend with two arithmetic shifts. */
1972 result = expand_shift (LSHIFT_EXPR, word_mode, result,
1973 BITS_PER_WORD - bitsize, NULL_RTX, 0);
1974 return expand_shift (RSHIFT_EXPR, word_mode, result,
1975 BITS_PER_WORD - bitsize, NULL_RTX, 0);
1978 /* Try to read the low bits of SRC as an rvalue of mode MODE, preserving
1979 the bit pattern. SRC_MODE is the mode of SRC; if this is smaller than
1980 MODE, fill the upper bits with zeros. Fail if the layout of either
1981 mode is unknown (as for CC modes) or if the extraction would involve
1982 unprofitable mode punning. Return the value on success, otherwise
1983 return null.
1985 This is different from gen_lowpart* in these respects:
1987 - the returned value must always be considered an rvalue
1989 - when MODE is wider than SRC_MODE, the extraction involves
1990 a zero extension
1992 - when MODE is smaller than SRC_MODE, the extraction involves
1993 a truncation (and is thus subject to TRULY_NOOP_TRUNCATION).
1995 In other words, this routine performs a computation, whereas the
1996 gen_lowpart* routines are conceptually lvalue or rvalue subreg
1997 operations. */
2000 extract_low_bits (enum machine_mode mode, enum machine_mode src_mode, rtx src)
2002 enum machine_mode int_mode, src_int_mode;
2004 if (mode == src_mode)
2005 return src;
2007 if (CONSTANT_P (src))
2009 /* simplify_gen_subreg can't be used here, as if simplify_subreg
2010 fails, it will happily create (subreg (symbol_ref)) or similar
2011 invalid SUBREGs. */
2012 unsigned int byte = subreg_lowpart_offset (mode, src_mode);
2013 rtx ret = simplify_subreg (mode, src, src_mode, byte);
2014 if (ret)
2015 return ret;
2017 if (GET_MODE (src) == VOIDmode
2018 || !validate_subreg (mode, src_mode, src, byte))
2019 return NULL_RTX;
2021 src = force_reg (GET_MODE (src), src);
2022 return gen_rtx_SUBREG (mode, src, byte);
2025 if (GET_MODE_CLASS (mode) == MODE_CC || GET_MODE_CLASS (src_mode) == MODE_CC)
2026 return NULL_RTX;
2028 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (src_mode)
2029 && MODES_TIEABLE_P (mode, src_mode))
2031 rtx x = gen_lowpart_common (mode, src);
2032 if (x)
2033 return x;
2036 src_int_mode = int_mode_for_mode (src_mode);
2037 int_mode = int_mode_for_mode (mode);
2038 if (src_int_mode == BLKmode || int_mode == BLKmode)
2039 return NULL_RTX;
2041 if (!MODES_TIEABLE_P (src_int_mode, src_mode))
2042 return NULL_RTX;
2043 if (!MODES_TIEABLE_P (int_mode, mode))
2044 return NULL_RTX;
2046 src = gen_lowpart (src_int_mode, src);
2047 src = convert_modes (int_mode, src_int_mode, src, true);
2048 src = gen_lowpart (mode, src);
2049 return src;
2052 /* Add INC into TARGET. */
2054 void
2055 expand_inc (rtx target, rtx inc)
2057 rtx value = expand_binop (GET_MODE (target), add_optab,
2058 target, inc,
2059 target, 0, OPTAB_LIB_WIDEN);
2060 if (value != target)
2061 emit_move_insn (target, value);
2064 /* Subtract DEC from TARGET. */
2066 void
2067 expand_dec (rtx target, rtx dec)
2069 rtx value = expand_binop (GET_MODE (target), sub_optab,
2070 target, dec,
2071 target, 0, OPTAB_LIB_WIDEN);
2072 if (value != target)
2073 emit_move_insn (target, value);
2076 /* Output a shift instruction for expression code CODE,
2077 with SHIFTED being the rtx for the value to shift,
2078 and AMOUNT the rtx for the amount to shift by.
2079 Store the result in the rtx TARGET, if that is convenient.
2080 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2081 Return the rtx for where the value is. */
2083 static rtx
2084 expand_shift_1 (enum tree_code code, enum machine_mode mode, rtx shifted,
2085 rtx amount, rtx target, int unsignedp)
2087 rtx op1, temp = 0;
2088 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
2089 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2090 optab lshift_optab = ashl_optab;
2091 optab rshift_arith_optab = ashr_optab;
2092 optab rshift_uns_optab = lshr_optab;
2093 optab lrotate_optab = rotl_optab;
2094 optab rrotate_optab = rotr_optab;
2095 enum machine_mode op1_mode;
2096 int attempt;
2097 bool speed = optimize_insn_for_speed_p ();
2099 op1 = amount;
2100 op1_mode = GET_MODE (op1);
2102 /* Determine whether the shift/rotate amount is a vector, or scalar. If the
2103 shift amount is a vector, use the vector/vector shift patterns. */
2104 if (VECTOR_MODE_P (mode) && VECTOR_MODE_P (op1_mode))
2106 lshift_optab = vashl_optab;
2107 rshift_arith_optab = vashr_optab;
2108 rshift_uns_optab = vlshr_optab;
2109 lrotate_optab = vrotl_optab;
2110 rrotate_optab = vrotr_optab;
2113 /* Previously detected shift-counts computed by NEGATE_EXPR
2114 and shifted in the other direction; but that does not work
2115 on all machines. */
2117 if (SHIFT_COUNT_TRUNCATED)
2119 if (CONST_INT_P (op1)
2120 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2121 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode)))
2122 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2123 % GET_MODE_BITSIZE (mode));
2124 else if (GET_CODE (op1) == SUBREG
2125 && subreg_lowpart_p (op1)
2126 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op1)))
2127 && SCALAR_INT_MODE_P (GET_MODE (op1)))
2128 op1 = SUBREG_REG (op1);
2131 /* Canonicalize rotates by constant amount. If op1 is bitsize / 2,
2132 prefer left rotation, if op1 is from bitsize / 2 + 1 to
2133 bitsize - 1, use other direction of rotate with 1 .. bitsize / 2 - 1
2134 amount instead. */
2135 if (rotate
2136 && CONST_INT_P (op1)
2137 && IN_RANGE (INTVAL (op1), GET_MODE_BITSIZE (mode) / 2 + left,
2138 GET_MODE_BITSIZE (mode) - 1))
2140 op1 = GEN_INT (GET_MODE_BITSIZE (mode) - INTVAL (op1));
2141 left = !left;
2142 code = left ? LROTATE_EXPR : RROTATE_EXPR;
2145 if (op1 == const0_rtx)
2146 return shifted;
2148 /* Check whether its cheaper to implement a left shift by a constant
2149 bit count by a sequence of additions. */
2150 if (code == LSHIFT_EXPR
2151 && CONST_INT_P (op1)
2152 && INTVAL (op1) > 0
2153 && INTVAL (op1) < GET_MODE_PRECISION (mode)
2154 && INTVAL (op1) < MAX_BITS_PER_WORD
2155 && (shift_cost (speed, mode, INTVAL (op1))
2156 > INTVAL (op1) * add_cost (speed, mode))
2157 && shift_cost (speed, mode, INTVAL (op1)) != MAX_COST)
2159 int i;
2160 for (i = 0; i < INTVAL (op1); i++)
2162 temp = force_reg (mode, shifted);
2163 shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2164 unsignedp, OPTAB_LIB_WIDEN);
2166 return shifted;
2169 for (attempt = 0; temp == 0 && attempt < 3; attempt++)
2171 enum optab_methods methods;
2173 if (attempt == 0)
2174 methods = OPTAB_DIRECT;
2175 else if (attempt == 1)
2176 methods = OPTAB_WIDEN;
2177 else
2178 methods = OPTAB_LIB_WIDEN;
2180 if (rotate)
2182 /* Widening does not work for rotation. */
2183 if (methods == OPTAB_WIDEN)
2184 continue;
2185 else if (methods == OPTAB_LIB_WIDEN)
2187 /* If we have been unable to open-code this by a rotation,
2188 do it as the IOR of two shifts. I.e., to rotate A
2189 by N bits, compute
2190 (A << N) | ((unsigned) A >> ((-N) & (C - 1)))
2191 where C is the bitsize of A.
2193 It is theoretically possible that the target machine might
2194 not be able to perform either shift and hence we would
2195 be making two libcalls rather than just the one for the
2196 shift (similarly if IOR could not be done). We will allow
2197 this extremely unlikely lossage to avoid complicating the
2198 code below. */
2200 rtx subtarget = target == shifted ? 0 : target;
2201 rtx new_amount, other_amount;
2202 rtx temp1;
2204 new_amount = op1;
2205 if (op1 == const0_rtx)
2206 return shifted;
2207 else if (CONST_INT_P (op1))
2208 other_amount = GEN_INT (GET_MODE_BITSIZE (mode)
2209 - INTVAL (op1));
2210 else
2212 other_amount
2213 = simplify_gen_unary (NEG, GET_MODE (op1),
2214 op1, GET_MODE (op1));
2215 other_amount
2216 = simplify_gen_binary (AND, GET_MODE (op1),
2217 other_amount,
2218 GEN_INT (GET_MODE_PRECISION (mode)
2219 - 1));
2222 shifted = force_reg (mode, shifted);
2224 temp = expand_shift_1 (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2225 mode, shifted, new_amount, 0, 1);
2226 temp1 = expand_shift_1 (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2227 mode, shifted, other_amount,
2228 subtarget, 1);
2229 return expand_binop (mode, ior_optab, temp, temp1, target,
2230 unsignedp, methods);
2233 temp = expand_binop (mode,
2234 left ? lrotate_optab : rrotate_optab,
2235 shifted, op1, target, unsignedp, methods);
2237 else if (unsignedp)
2238 temp = expand_binop (mode,
2239 left ? lshift_optab : rshift_uns_optab,
2240 shifted, op1, target, unsignedp, methods);
2242 /* Do arithmetic shifts.
2243 Also, if we are going to widen the operand, we can just as well
2244 use an arithmetic right-shift instead of a logical one. */
2245 if (temp == 0 && ! rotate
2246 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2248 enum optab_methods methods1 = methods;
2250 /* If trying to widen a log shift to an arithmetic shift,
2251 don't accept an arithmetic shift of the same size. */
2252 if (unsignedp)
2253 methods1 = OPTAB_MUST_WIDEN;
2255 /* Arithmetic shift */
2257 temp = expand_binop (mode,
2258 left ? lshift_optab : rshift_arith_optab,
2259 shifted, op1, target, unsignedp, methods1);
2262 /* We used to try extzv here for logical right shifts, but that was
2263 only useful for one machine, the VAX, and caused poor code
2264 generation there for lshrdi3, so the code was deleted and a
2265 define_expand for lshrsi3 was added to vax.md. */
2268 gcc_assert (temp);
2269 return temp;
2272 /* Output a shift instruction for expression code CODE,
2273 with SHIFTED being the rtx for the value to shift,
2274 and AMOUNT the amount to shift by.
2275 Store the result in the rtx TARGET, if that is convenient.
2276 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2277 Return the rtx for where the value is. */
2280 expand_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2281 int amount, rtx target, int unsignedp)
2283 return expand_shift_1 (code, mode,
2284 shifted, GEN_INT (amount), target, unsignedp);
2287 /* Output a shift instruction for expression code CODE,
2288 with SHIFTED being the rtx for the value to shift,
2289 and AMOUNT the tree for the amount to shift by.
2290 Store the result in the rtx TARGET, if that is convenient.
2291 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2292 Return the rtx for where the value is. */
2295 expand_variable_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2296 tree amount, rtx target, int unsignedp)
2298 return expand_shift_1 (code, mode,
2299 shifted, expand_normal (amount), target, unsignedp);
2303 /* Indicates the type of fixup needed after a constant multiplication.
2304 BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2305 the result should be negated, and ADD_VARIANT means that the
2306 multiplicand should be added to the result. */
2307 enum mult_variant {basic_variant, negate_variant, add_variant};
2309 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2310 const struct mult_cost *, enum machine_mode mode);
2311 static bool choose_mult_variant (enum machine_mode, HOST_WIDE_INT,
2312 struct algorithm *, enum mult_variant *, int);
2313 static rtx expand_mult_const (enum machine_mode, rtx, HOST_WIDE_INT, rtx,
2314 const struct algorithm *, enum mult_variant);
2315 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2316 static rtx extract_high_half (enum machine_mode, rtx);
2317 static rtx expmed_mult_highpart (enum machine_mode, rtx, rtx, rtx, int, int);
2318 static rtx expmed_mult_highpart_optab (enum machine_mode, rtx, rtx, rtx,
2319 int, int);
2320 /* Compute and return the best algorithm for multiplying by T.
2321 The algorithm must cost less than cost_limit
2322 If retval.cost >= COST_LIMIT, no algorithm was found and all
2323 other field of the returned struct are undefined.
2324 MODE is the machine mode of the multiplication. */
2326 static void
2327 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2328 const struct mult_cost *cost_limit, enum machine_mode mode)
2330 int m;
2331 struct algorithm *alg_in, *best_alg;
2332 struct mult_cost best_cost;
2333 struct mult_cost new_limit;
2334 int op_cost, op_latency;
2335 unsigned HOST_WIDE_INT orig_t = t;
2336 unsigned HOST_WIDE_INT q;
2337 int maxm, hash_index;
2338 bool cache_hit = false;
2339 enum alg_code cache_alg = alg_zero;
2340 bool speed = optimize_insn_for_speed_p ();
2341 enum machine_mode imode;
2342 struct alg_hash_entry *entry_ptr;
2344 /* Indicate that no algorithm is yet found. If no algorithm
2345 is found, this value will be returned and indicate failure. */
2346 alg_out->cost.cost = cost_limit->cost + 1;
2347 alg_out->cost.latency = cost_limit->latency + 1;
2349 if (cost_limit->cost < 0
2350 || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2351 return;
2353 /* Be prepared for vector modes. */
2354 imode = GET_MODE_INNER (mode);
2355 if (imode == VOIDmode)
2356 imode = mode;
2358 maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (imode));
2360 /* Restrict the bits of "t" to the multiplication's mode. */
2361 t &= GET_MODE_MASK (imode);
2363 /* t == 1 can be done in zero cost. */
2364 if (t == 1)
2366 alg_out->ops = 1;
2367 alg_out->cost.cost = 0;
2368 alg_out->cost.latency = 0;
2369 alg_out->op[0] = alg_m;
2370 return;
2373 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2374 fail now. */
2375 if (t == 0)
2377 if (MULT_COST_LESS (cost_limit, zero_cost (speed)))
2378 return;
2379 else
2381 alg_out->ops = 1;
2382 alg_out->cost.cost = zero_cost (speed);
2383 alg_out->cost.latency = zero_cost (speed);
2384 alg_out->op[0] = alg_zero;
2385 return;
2389 /* We'll be needing a couple extra algorithm structures now. */
2391 alg_in = XALLOCA (struct algorithm);
2392 best_alg = XALLOCA (struct algorithm);
2393 best_cost = *cost_limit;
2395 /* Compute the hash index. */
2396 hash_index = (t ^ (unsigned int) mode ^ (speed * 256)) % NUM_ALG_HASH_ENTRIES;
2398 /* See if we already know what to do for T. */
2399 entry_ptr = alg_hash_entry_ptr (hash_index);
2400 if (entry_ptr->t == t
2401 && entry_ptr->mode == mode
2402 && entry_ptr->mode == mode
2403 && entry_ptr->speed == speed
2404 && entry_ptr->alg != alg_unknown)
2406 cache_alg = entry_ptr->alg;
2408 if (cache_alg == alg_impossible)
2410 /* The cache tells us that it's impossible to synthesize
2411 multiplication by T within entry_ptr->cost. */
2412 if (!CHEAPER_MULT_COST (&entry_ptr->cost, cost_limit))
2413 /* COST_LIMIT is at least as restrictive as the one
2414 recorded in the hash table, in which case we have no
2415 hope of synthesizing a multiplication. Just
2416 return. */
2417 return;
2419 /* If we get here, COST_LIMIT is less restrictive than the
2420 one recorded in the hash table, so we may be able to
2421 synthesize a multiplication. Proceed as if we didn't
2422 have the cache entry. */
2424 else
2426 if (CHEAPER_MULT_COST (cost_limit, &entry_ptr->cost))
2427 /* The cached algorithm shows that this multiplication
2428 requires more cost than COST_LIMIT. Just return. This
2429 way, we don't clobber this cache entry with
2430 alg_impossible but retain useful information. */
2431 return;
2433 cache_hit = true;
2435 switch (cache_alg)
2437 case alg_shift:
2438 goto do_alg_shift;
2440 case alg_add_t_m2:
2441 case alg_sub_t_m2:
2442 goto do_alg_addsub_t_m2;
2444 case alg_add_factor:
2445 case alg_sub_factor:
2446 goto do_alg_addsub_factor;
2448 case alg_add_t2_m:
2449 goto do_alg_add_t2_m;
2451 case alg_sub_t2_m:
2452 goto do_alg_sub_t2_m;
2454 default:
2455 gcc_unreachable ();
2460 /* If we have a group of zero bits at the low-order part of T, try
2461 multiplying by the remaining bits and then doing a shift. */
2463 if ((t & 1) == 0)
2465 do_alg_shift:
2466 m = floor_log2 (t & -t); /* m = number of low zero bits */
2467 if (m < maxm)
2469 q = t >> m;
2470 /* The function expand_shift will choose between a shift and
2471 a sequence of additions, so the observed cost is given as
2472 MIN (m * add_cost(speed, mode), shift_cost(speed, mode, m)). */
2473 op_cost = m * add_cost (speed, mode);
2474 if (shift_cost (speed, mode, m) < op_cost)
2475 op_cost = shift_cost (speed, mode, m);
2476 new_limit.cost = best_cost.cost - op_cost;
2477 new_limit.latency = best_cost.latency - op_cost;
2478 synth_mult (alg_in, q, &new_limit, mode);
2480 alg_in->cost.cost += op_cost;
2481 alg_in->cost.latency += op_cost;
2482 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2484 struct algorithm *x;
2485 best_cost = alg_in->cost;
2486 x = alg_in, alg_in = best_alg, best_alg = x;
2487 best_alg->log[best_alg->ops] = m;
2488 best_alg->op[best_alg->ops] = alg_shift;
2491 /* See if treating ORIG_T as a signed number yields a better
2492 sequence. Try this sequence only for a negative ORIG_T
2493 as it would be useless for a non-negative ORIG_T. */
2494 if ((HOST_WIDE_INT) orig_t < 0)
2496 /* Shift ORIG_T as follows because a right shift of a
2497 negative-valued signed type is implementation
2498 defined. */
2499 q = ~(~orig_t >> m);
2500 /* The function expand_shift will choose between a shift
2501 and a sequence of additions, so the observed cost is
2502 given as MIN (m * add_cost(speed, mode),
2503 shift_cost(speed, mode, m)). */
2504 op_cost = m * add_cost (speed, mode);
2505 if (shift_cost (speed, mode, m) < op_cost)
2506 op_cost = shift_cost (speed, mode, m);
2507 new_limit.cost = best_cost.cost - op_cost;
2508 new_limit.latency = best_cost.latency - op_cost;
2509 synth_mult (alg_in, q, &new_limit, mode);
2511 alg_in->cost.cost += op_cost;
2512 alg_in->cost.latency += op_cost;
2513 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2515 struct algorithm *x;
2516 best_cost = alg_in->cost;
2517 x = alg_in, alg_in = best_alg, best_alg = x;
2518 best_alg->log[best_alg->ops] = m;
2519 best_alg->op[best_alg->ops] = alg_shift;
2523 if (cache_hit)
2524 goto done;
2527 /* If we have an odd number, add or subtract one. */
2528 if ((t & 1) != 0)
2530 unsigned HOST_WIDE_INT w;
2532 do_alg_addsub_t_m2:
2533 for (w = 1; (w & t) != 0; w <<= 1)
2535 /* If T was -1, then W will be zero after the loop. This is another
2536 case where T ends with ...111. Handling this with (T + 1) and
2537 subtract 1 produces slightly better code and results in algorithm
2538 selection much faster than treating it like the ...0111 case
2539 below. */
2540 if (w == 0
2541 || (w > 2
2542 /* Reject the case where t is 3.
2543 Thus we prefer addition in that case. */
2544 && t != 3))
2546 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2548 op_cost = add_cost (speed, mode);
2549 new_limit.cost = best_cost.cost - op_cost;
2550 new_limit.latency = best_cost.latency - op_cost;
2551 synth_mult (alg_in, t + 1, &new_limit, mode);
2553 alg_in->cost.cost += op_cost;
2554 alg_in->cost.latency += op_cost;
2555 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2557 struct algorithm *x;
2558 best_cost = alg_in->cost;
2559 x = alg_in, alg_in = best_alg, best_alg = x;
2560 best_alg->log[best_alg->ops] = 0;
2561 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2564 else
2566 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2568 op_cost = add_cost (speed, mode);
2569 new_limit.cost = best_cost.cost - op_cost;
2570 new_limit.latency = best_cost.latency - op_cost;
2571 synth_mult (alg_in, t - 1, &new_limit, mode);
2573 alg_in->cost.cost += op_cost;
2574 alg_in->cost.latency += op_cost;
2575 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2577 struct algorithm *x;
2578 best_cost = alg_in->cost;
2579 x = alg_in, alg_in = best_alg, best_alg = x;
2580 best_alg->log[best_alg->ops] = 0;
2581 best_alg->op[best_alg->ops] = alg_add_t_m2;
2585 /* We may be able to calculate a * -7, a * -15, a * -31, etc
2586 quickly with a - a * n for some appropriate constant n. */
2587 m = exact_log2 (-orig_t + 1);
2588 if (m >= 0 && m < maxm)
2590 op_cost = shiftsub1_cost (speed, mode, m);
2591 new_limit.cost = best_cost.cost - op_cost;
2592 new_limit.latency = best_cost.latency - op_cost;
2593 synth_mult (alg_in, (unsigned HOST_WIDE_INT) (-orig_t + 1) >> m,
2594 &new_limit, mode);
2596 alg_in->cost.cost += op_cost;
2597 alg_in->cost.latency += op_cost;
2598 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2600 struct algorithm *x;
2601 best_cost = alg_in->cost;
2602 x = alg_in, alg_in = best_alg, best_alg = x;
2603 best_alg->log[best_alg->ops] = m;
2604 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2608 if (cache_hit)
2609 goto done;
2612 /* Look for factors of t of the form
2613 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2614 If we find such a factor, we can multiply by t using an algorithm that
2615 multiplies by q, shift the result by m and add/subtract it to itself.
2617 We search for large factors first and loop down, even if large factors
2618 are less probable than small; if we find a large factor we will find a
2619 good sequence quickly, and therefore be able to prune (by decreasing
2620 COST_LIMIT) the search. */
2622 do_alg_addsub_factor:
2623 for (m = floor_log2 (t - 1); m >= 2; m--)
2625 unsigned HOST_WIDE_INT d;
2627 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2628 if (t % d == 0 && t > d && m < maxm
2629 && (!cache_hit || cache_alg == alg_add_factor))
2631 /* If the target has a cheap shift-and-add instruction use
2632 that in preference to a shift insn followed by an add insn.
2633 Assume that the shift-and-add is "atomic" with a latency
2634 equal to its cost, otherwise assume that on superscalar
2635 hardware the shift may be executed concurrently with the
2636 earlier steps in the algorithm. */
2637 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2638 if (shiftadd_cost (speed, mode, m) < op_cost)
2640 op_cost = shiftadd_cost (speed, mode, m);
2641 op_latency = op_cost;
2643 else
2644 op_latency = add_cost (speed, mode);
2646 new_limit.cost = best_cost.cost - op_cost;
2647 new_limit.latency = best_cost.latency - op_latency;
2648 synth_mult (alg_in, t / d, &new_limit, mode);
2650 alg_in->cost.cost += op_cost;
2651 alg_in->cost.latency += op_latency;
2652 if (alg_in->cost.latency < op_cost)
2653 alg_in->cost.latency = op_cost;
2654 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2656 struct algorithm *x;
2657 best_cost = alg_in->cost;
2658 x = alg_in, alg_in = best_alg, best_alg = x;
2659 best_alg->log[best_alg->ops] = m;
2660 best_alg->op[best_alg->ops] = alg_add_factor;
2662 /* Other factors will have been taken care of in the recursion. */
2663 break;
2666 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2667 if (t % d == 0 && t > d && m < maxm
2668 && (!cache_hit || cache_alg == alg_sub_factor))
2670 /* If the target has a cheap shift-and-subtract insn use
2671 that in preference to a shift insn followed by a sub insn.
2672 Assume that the shift-and-sub is "atomic" with a latency
2673 equal to it's cost, otherwise assume that on superscalar
2674 hardware the shift may be executed concurrently with the
2675 earlier steps in the algorithm. */
2676 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2677 if (shiftsub0_cost (speed, mode, m) < op_cost)
2679 op_cost = shiftsub0_cost (speed, mode, m);
2680 op_latency = op_cost;
2682 else
2683 op_latency = add_cost (speed, mode);
2685 new_limit.cost = best_cost.cost - op_cost;
2686 new_limit.latency = best_cost.latency - op_latency;
2687 synth_mult (alg_in, t / d, &new_limit, mode);
2689 alg_in->cost.cost += op_cost;
2690 alg_in->cost.latency += op_latency;
2691 if (alg_in->cost.latency < op_cost)
2692 alg_in->cost.latency = op_cost;
2693 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2695 struct algorithm *x;
2696 best_cost = alg_in->cost;
2697 x = alg_in, alg_in = best_alg, best_alg = x;
2698 best_alg->log[best_alg->ops] = m;
2699 best_alg->op[best_alg->ops] = alg_sub_factor;
2701 break;
2704 if (cache_hit)
2705 goto done;
2707 /* Try shift-and-add (load effective address) instructions,
2708 i.e. do a*3, a*5, a*9. */
2709 if ((t & 1) != 0)
2711 do_alg_add_t2_m:
2712 q = t - 1;
2713 q = q & -q;
2714 m = exact_log2 (q);
2715 if (m >= 0 && m < maxm)
2717 op_cost = shiftadd_cost (speed, mode, m);
2718 new_limit.cost = best_cost.cost - op_cost;
2719 new_limit.latency = best_cost.latency - op_cost;
2720 synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2722 alg_in->cost.cost += op_cost;
2723 alg_in->cost.latency += op_cost;
2724 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2726 struct algorithm *x;
2727 best_cost = alg_in->cost;
2728 x = alg_in, alg_in = best_alg, best_alg = x;
2729 best_alg->log[best_alg->ops] = m;
2730 best_alg->op[best_alg->ops] = alg_add_t2_m;
2733 if (cache_hit)
2734 goto done;
2736 do_alg_sub_t2_m:
2737 q = t + 1;
2738 q = q & -q;
2739 m = exact_log2 (q);
2740 if (m >= 0 && m < maxm)
2742 op_cost = shiftsub0_cost (speed, mode, m);
2743 new_limit.cost = best_cost.cost - op_cost;
2744 new_limit.latency = best_cost.latency - op_cost;
2745 synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2747 alg_in->cost.cost += op_cost;
2748 alg_in->cost.latency += op_cost;
2749 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2751 struct algorithm *x;
2752 best_cost = alg_in->cost;
2753 x = alg_in, alg_in = best_alg, best_alg = x;
2754 best_alg->log[best_alg->ops] = m;
2755 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2758 if (cache_hit)
2759 goto done;
2762 done:
2763 /* If best_cost has not decreased, we have not found any algorithm. */
2764 if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
2766 /* We failed to find an algorithm. Record alg_impossible for
2767 this case (that is, <T, MODE, COST_LIMIT>) so that next time
2768 we are asked to find an algorithm for T within the same or
2769 lower COST_LIMIT, we can immediately return to the
2770 caller. */
2771 entry_ptr->t = t;
2772 entry_ptr->mode = mode;
2773 entry_ptr->speed = speed;
2774 entry_ptr->alg = alg_impossible;
2775 entry_ptr->cost = *cost_limit;
2776 return;
2779 /* Cache the result. */
2780 if (!cache_hit)
2782 entry_ptr->t = t;
2783 entry_ptr->mode = mode;
2784 entry_ptr->speed = speed;
2785 entry_ptr->alg = best_alg->op[best_alg->ops];
2786 entry_ptr->cost.cost = best_cost.cost;
2787 entry_ptr->cost.latency = best_cost.latency;
2790 /* If we are getting a too long sequence for `struct algorithm'
2791 to record, make this search fail. */
2792 if (best_alg->ops == MAX_BITS_PER_WORD)
2793 return;
2795 /* Copy the algorithm from temporary space to the space at alg_out.
2796 We avoid using structure assignment because the majority of
2797 best_alg is normally undefined, and this is a critical function. */
2798 alg_out->ops = best_alg->ops + 1;
2799 alg_out->cost = best_cost;
2800 memcpy (alg_out->op, best_alg->op,
2801 alg_out->ops * sizeof *alg_out->op);
2802 memcpy (alg_out->log, best_alg->log,
2803 alg_out->ops * sizeof *alg_out->log);
2806 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2807 Try three variations:
2809 - a shift/add sequence based on VAL itself
2810 - a shift/add sequence based on -VAL, followed by a negation
2811 - a shift/add sequence based on VAL - 1, followed by an addition.
2813 Return true if the cheapest of these cost less than MULT_COST,
2814 describing the algorithm in *ALG and final fixup in *VARIANT. */
2816 static bool
2817 choose_mult_variant (enum machine_mode mode, HOST_WIDE_INT val,
2818 struct algorithm *alg, enum mult_variant *variant,
2819 int mult_cost)
2821 struct algorithm alg2;
2822 struct mult_cost limit;
2823 int op_cost;
2824 bool speed = optimize_insn_for_speed_p ();
2826 /* Fail quickly for impossible bounds. */
2827 if (mult_cost < 0)
2828 return false;
2830 /* Ensure that mult_cost provides a reasonable upper bound.
2831 Any constant multiplication can be performed with less
2832 than 2 * bits additions. */
2833 op_cost = 2 * GET_MODE_UNIT_BITSIZE (mode) * add_cost (speed, mode);
2834 if (mult_cost > op_cost)
2835 mult_cost = op_cost;
2837 *variant = basic_variant;
2838 limit.cost = mult_cost;
2839 limit.latency = mult_cost;
2840 synth_mult (alg, val, &limit, mode);
2842 /* This works only if the inverted value actually fits in an
2843 `unsigned int' */
2844 if (HOST_BITS_PER_INT >= GET_MODE_UNIT_BITSIZE (mode))
2846 op_cost = neg_cost(speed, mode);
2847 if (MULT_COST_LESS (&alg->cost, mult_cost))
2849 limit.cost = alg->cost.cost - op_cost;
2850 limit.latency = alg->cost.latency - op_cost;
2852 else
2854 limit.cost = mult_cost - op_cost;
2855 limit.latency = mult_cost - op_cost;
2858 synth_mult (&alg2, -val, &limit, mode);
2859 alg2.cost.cost += op_cost;
2860 alg2.cost.latency += op_cost;
2861 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2862 *alg = alg2, *variant = negate_variant;
2865 /* This proves very useful for division-by-constant. */
2866 op_cost = add_cost (speed, mode);
2867 if (MULT_COST_LESS (&alg->cost, mult_cost))
2869 limit.cost = alg->cost.cost - op_cost;
2870 limit.latency = alg->cost.latency - op_cost;
2872 else
2874 limit.cost = mult_cost - op_cost;
2875 limit.latency = mult_cost - op_cost;
2878 synth_mult (&alg2, val - 1, &limit, mode);
2879 alg2.cost.cost += op_cost;
2880 alg2.cost.latency += op_cost;
2881 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2882 *alg = alg2, *variant = add_variant;
2884 return MULT_COST_LESS (&alg->cost, mult_cost);
2887 /* A subroutine of expand_mult, used for constant multiplications.
2888 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2889 convenient. Use the shift/add sequence described by ALG and apply
2890 the final fixup specified by VARIANT. */
2892 static rtx
2893 expand_mult_const (enum machine_mode mode, rtx op0, HOST_WIDE_INT val,
2894 rtx target, const struct algorithm *alg,
2895 enum mult_variant variant)
2897 HOST_WIDE_INT val_so_far;
2898 rtx insn, accum, tem;
2899 int opno;
2900 enum machine_mode nmode;
2902 /* Avoid referencing memory over and over and invalid sharing
2903 on SUBREGs. */
2904 op0 = force_reg (mode, op0);
2906 /* ACCUM starts out either as OP0 or as a zero, depending on
2907 the first operation. */
2909 if (alg->op[0] == alg_zero)
2911 accum = copy_to_mode_reg (mode, CONST0_RTX (mode));
2912 val_so_far = 0;
2914 else if (alg->op[0] == alg_m)
2916 accum = copy_to_mode_reg (mode, op0);
2917 val_so_far = 1;
2919 else
2920 gcc_unreachable ();
2922 for (opno = 1; opno < alg->ops; opno++)
2924 int log = alg->log[opno];
2925 rtx shift_subtarget = optimize ? 0 : accum;
2926 rtx add_target
2927 = (opno == alg->ops - 1 && target != 0 && variant != add_variant
2928 && !optimize)
2929 ? target : 0;
2930 rtx accum_target = optimize ? 0 : accum;
2931 rtx accum_inner;
2933 switch (alg->op[opno])
2935 case alg_shift:
2936 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
2937 /* REG_EQUAL note will be attached to the following insn. */
2938 emit_move_insn (accum, tem);
2939 val_so_far <<= log;
2940 break;
2942 case alg_add_t_m2:
2943 tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
2944 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2945 add_target ? add_target : accum_target);
2946 val_so_far += (HOST_WIDE_INT) 1 << log;
2947 break;
2949 case alg_sub_t_m2:
2950 tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
2951 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
2952 add_target ? add_target : accum_target);
2953 val_so_far -= (HOST_WIDE_INT) 1 << log;
2954 break;
2956 case alg_add_t2_m:
2957 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2958 log, shift_subtarget, 0);
2959 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
2960 add_target ? add_target : accum_target);
2961 val_so_far = (val_so_far << log) + 1;
2962 break;
2964 case alg_sub_t2_m:
2965 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2966 log, shift_subtarget, 0);
2967 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
2968 add_target ? add_target : accum_target);
2969 val_so_far = (val_so_far << log) - 1;
2970 break;
2972 case alg_add_factor:
2973 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
2974 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2975 add_target ? add_target : accum_target);
2976 val_so_far += val_so_far << log;
2977 break;
2979 case alg_sub_factor:
2980 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
2981 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
2982 (add_target
2983 ? add_target : (optimize ? 0 : tem)));
2984 val_so_far = (val_so_far << log) - val_so_far;
2985 break;
2987 default:
2988 gcc_unreachable ();
2991 if (SCALAR_INT_MODE_P (mode))
2993 /* Write a REG_EQUAL note on the last insn so that we can cse
2994 multiplication sequences. Note that if ACCUM is a SUBREG,
2995 we've set the inner register and must properly indicate that. */
2996 tem = op0, nmode = mode;
2997 accum_inner = accum;
2998 if (GET_CODE (accum) == SUBREG)
3000 accum_inner = SUBREG_REG (accum);
3001 nmode = GET_MODE (accum_inner);
3002 tem = gen_lowpart (nmode, op0);
3005 insn = get_last_insn ();
3006 set_dst_reg_note (insn, REG_EQUAL,
3007 gen_rtx_MULT (nmode, tem, GEN_INT (val_so_far)),
3008 accum_inner);
3012 if (variant == negate_variant)
3014 val_so_far = -val_so_far;
3015 accum = expand_unop (mode, neg_optab, accum, target, 0);
3017 else if (variant == add_variant)
3019 val_so_far = val_so_far + 1;
3020 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
3023 /* Compare only the bits of val and val_so_far that are significant
3024 in the result mode, to avoid sign-/zero-extension confusion. */
3025 nmode = GET_MODE_INNER (mode);
3026 if (nmode == VOIDmode)
3027 nmode = mode;
3028 val &= GET_MODE_MASK (nmode);
3029 val_so_far &= GET_MODE_MASK (nmode);
3030 gcc_assert (val == val_so_far);
3032 return accum;
3035 /* Perform a multiplication and return an rtx for the result.
3036 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3037 TARGET is a suggestion for where to store the result (an rtx).
3039 We check specially for a constant integer as OP1.
3040 If you want this check for OP0 as well, then before calling
3041 you should swap the two operands if OP0 would be constant. */
3044 expand_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3045 int unsignedp)
3047 enum mult_variant variant;
3048 struct algorithm algorithm;
3049 rtx scalar_op1;
3050 int max_cost;
3051 bool speed = optimize_insn_for_speed_p ();
3052 bool do_trapv = flag_trapv && SCALAR_INT_MODE_P (mode) && !unsignedp;
3054 if (CONSTANT_P (op0))
3056 rtx temp = op0;
3057 op0 = op1;
3058 op1 = temp;
3061 /* For vectors, there are several simplifications that can be made if
3062 all elements of the vector constant are identical. */
3063 scalar_op1 = op1;
3064 if (GET_CODE (op1) == CONST_VECTOR)
3066 int i, n = CONST_VECTOR_NUNITS (op1);
3067 scalar_op1 = CONST_VECTOR_ELT (op1, 0);
3068 for (i = 1; i < n; ++i)
3069 if (!rtx_equal_p (scalar_op1, CONST_VECTOR_ELT (op1, i)))
3070 goto skip_scalar;
3073 if (INTEGRAL_MODE_P (mode))
3075 rtx fake_reg;
3076 HOST_WIDE_INT coeff;
3077 bool is_neg;
3078 int mode_bitsize;
3080 if (op1 == CONST0_RTX (mode))
3081 return op1;
3082 if (op1 == CONST1_RTX (mode))
3083 return op0;
3084 if (op1 == CONSTM1_RTX (mode))
3085 return expand_unop (mode, do_trapv ? negv_optab : neg_optab,
3086 op0, target, 0);
3088 if (do_trapv)
3089 goto skip_synth;
3091 /* These are the operations that are potentially turned into
3092 a sequence of shifts and additions. */
3093 mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
3095 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3096 less than or equal in size to `unsigned int' this doesn't matter.
3097 If the mode is larger than `unsigned int', then synth_mult works
3098 only if the constant value exactly fits in an `unsigned int' without
3099 any truncation. This means that multiplying by negative values does
3100 not work; results are off by 2^32 on a 32 bit machine. */
3102 if (CONST_INT_P (scalar_op1))
3104 coeff = INTVAL (scalar_op1);
3105 is_neg = coeff < 0;
3107 else if (CONST_DOUBLE_AS_INT_P (scalar_op1))
3109 /* If we are multiplying in DImode, it may still be a win
3110 to try to work with shifts and adds. */
3111 if (CONST_DOUBLE_HIGH (scalar_op1) == 0
3112 && (CONST_DOUBLE_LOW (scalar_op1) > 0
3113 || (CONST_DOUBLE_LOW (scalar_op1) < 0
3114 && EXACT_POWER_OF_2_OR_ZERO_P
3115 (CONST_DOUBLE_LOW (scalar_op1)))))
3117 coeff = CONST_DOUBLE_LOW (scalar_op1);
3118 is_neg = false;
3120 else if (CONST_DOUBLE_LOW (scalar_op1) == 0)
3122 coeff = CONST_DOUBLE_HIGH (scalar_op1);
3123 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3125 int shift = floor_log2 (coeff) + HOST_BITS_PER_WIDE_INT;
3126 if (shift < HOST_BITS_PER_DOUBLE_INT - 1
3127 || mode_bitsize <= HOST_BITS_PER_DOUBLE_INT)
3128 return expand_shift (LSHIFT_EXPR, mode, op0,
3129 shift, target, unsignedp);
3131 goto skip_synth;
3133 else
3134 goto skip_synth;
3136 else
3137 goto skip_synth;
3139 /* We used to test optimize here, on the grounds that it's better to
3140 produce a smaller program when -O is not used. But this causes
3141 such a terrible slowdown sometimes that it seems better to always
3142 use synth_mult. */
3144 /* Special case powers of two. */
3145 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff)
3146 && !(is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT))
3147 return expand_shift (LSHIFT_EXPR, mode, op0,
3148 floor_log2 (coeff), target, unsignedp);
3150 fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3152 /* Attempt to handle multiplication of DImode values by negative
3153 coefficients, by performing the multiplication by a positive
3154 multiplier and then inverting the result. */
3155 if (is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT)
3157 /* Its safe to use -coeff even for INT_MIN, as the
3158 result is interpreted as an unsigned coefficient.
3159 Exclude cost of op0 from max_cost to match the cost
3160 calculation of the synth_mult. */
3161 coeff = -(unsigned HOST_WIDE_INT) coeff;
3162 max_cost = (set_src_cost (gen_rtx_MULT (mode, fake_reg, op1), speed)
3163 - neg_cost(speed, mode));
3164 if (max_cost <= 0)
3165 goto skip_synth;
3167 /* Special case powers of two. */
3168 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3170 rtx temp = expand_shift (LSHIFT_EXPR, mode, op0,
3171 floor_log2 (coeff), target, unsignedp);
3172 return expand_unop (mode, neg_optab, temp, target, 0);
3175 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3176 max_cost))
3178 rtx temp = expand_mult_const (mode, op0, coeff, NULL_RTX,
3179 &algorithm, variant);
3180 return expand_unop (mode, neg_optab, temp, target, 0);
3182 goto skip_synth;
3185 /* Exclude cost of op0 from max_cost to match the cost
3186 calculation of the synth_mult. */
3187 max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, op1), speed);
3188 if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
3189 return expand_mult_const (mode, op0, coeff, target,
3190 &algorithm, variant);
3192 skip_synth:
3194 /* Expand x*2.0 as x+x. */
3195 if (CONST_DOUBLE_AS_FLOAT_P (scalar_op1))
3197 REAL_VALUE_TYPE d;
3198 REAL_VALUE_FROM_CONST_DOUBLE (d, scalar_op1);
3200 if (REAL_VALUES_EQUAL (d, dconst2))
3202 op0 = force_reg (GET_MODE (op0), op0);
3203 return expand_binop (mode, add_optab, op0, op0,
3204 target, unsignedp, OPTAB_LIB_WIDEN);
3207 skip_scalar:
3209 /* This used to use umul_optab if unsigned, but for non-widening multiply
3210 there is no difference between signed and unsigned. */
3211 op0 = expand_binop (mode, do_trapv ? smulv_optab : smul_optab,
3212 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
3213 gcc_assert (op0);
3214 return op0;
3217 /* Return a cost estimate for multiplying a register by the given
3218 COEFFicient in the given MODE and SPEED. */
3221 mult_by_coeff_cost (HOST_WIDE_INT coeff, enum machine_mode mode, bool speed)
3223 int max_cost;
3224 struct algorithm algorithm;
3225 enum mult_variant variant;
3227 rtx fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3228 max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, fake_reg), speed);
3229 if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
3230 return algorithm.cost.cost;
3231 else
3232 return max_cost;
3235 /* Perform a widening multiplication and return an rtx for the result.
3236 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3237 TARGET is a suggestion for where to store the result (an rtx).
3238 THIS_OPTAB is the optab we should use, it must be either umul_widen_optab
3239 or smul_widen_optab.
3241 We check specially for a constant integer as OP1, comparing the
3242 cost of a widening multiply against the cost of a sequence of shifts
3243 and adds. */
3246 expand_widening_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3247 int unsignedp, optab this_optab)
3249 bool speed = optimize_insn_for_speed_p ();
3250 rtx cop1;
3252 if (CONST_INT_P (op1)
3253 && GET_MODE (op0) != VOIDmode
3254 && (cop1 = convert_modes (mode, GET_MODE (op0), op1,
3255 this_optab == umul_widen_optab))
3256 && CONST_INT_P (cop1)
3257 && (INTVAL (cop1) >= 0
3258 || HWI_COMPUTABLE_MODE_P (mode)))
3260 HOST_WIDE_INT coeff = INTVAL (cop1);
3261 int max_cost;
3262 enum mult_variant variant;
3263 struct algorithm algorithm;
3265 /* Special case powers of two. */
3266 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3268 op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
3269 return expand_shift (LSHIFT_EXPR, mode, op0,
3270 floor_log2 (coeff), target, unsignedp);
3273 /* Exclude cost of op0 from max_cost to match the cost
3274 calculation of the synth_mult. */
3275 max_cost = mul_widen_cost (speed, mode);
3276 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3277 max_cost))
3279 op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
3280 return expand_mult_const (mode, op0, coeff, target,
3281 &algorithm, variant);
3284 return expand_binop (mode, this_optab, op0, op1, target,
3285 unsignedp, OPTAB_LIB_WIDEN);
3288 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3289 replace division by D, and put the least significant N bits of the result
3290 in *MULTIPLIER_PTR and return the most significant bit.
3292 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3293 needed precision is in PRECISION (should be <= N).
3295 PRECISION should be as small as possible so this function can choose
3296 multiplier more freely.
3298 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3299 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3301 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3302 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3304 unsigned HOST_WIDE_INT
3305 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
3306 unsigned HOST_WIDE_INT *multiplier_ptr,
3307 int *post_shift_ptr, int *lgup_ptr)
3309 double_int mhigh, mlow;
3310 int lgup, post_shift;
3311 int pow, pow2;
3313 /* lgup = ceil(log2(divisor)); */
3314 lgup = ceil_log2 (d);
3316 gcc_assert (lgup <= n);
3318 pow = n + lgup;
3319 pow2 = n + lgup - precision;
3321 /* We could handle this with some effort, but this case is much
3322 better handled directly with a scc insn, so rely on caller using
3323 that. */
3324 gcc_assert (pow != HOST_BITS_PER_DOUBLE_INT);
3326 /* mlow = 2^(N + lgup)/d */
3327 double_int val = double_int_zero.set_bit (pow);
3328 mlow = val.div (double_int::from_uhwi (d), true, TRUNC_DIV_EXPR);
3330 /* mhigh = (2^(N + lgup) + 2^(N + lgup - precision))/d */
3331 val |= double_int_zero.set_bit (pow2);
3332 mhigh = val.div (double_int::from_uhwi (d), true, TRUNC_DIV_EXPR);
3334 gcc_assert (!mhigh.high || val.high - d < d);
3335 gcc_assert (mhigh.high <= 1 && mlow.high <= 1);
3336 /* Assert that mlow < mhigh. */
3337 gcc_assert (mlow.ult (mhigh));
3339 /* If precision == N, then mlow, mhigh exceed 2^N
3340 (but they do not exceed 2^(N+1)). */
3342 /* Reduce to lowest terms. */
3343 for (post_shift = lgup; post_shift > 0; post_shift--)
3345 int shft = HOST_BITS_PER_WIDE_INT - 1;
3346 unsigned HOST_WIDE_INT ml_lo = (mlow.high << shft) | (mlow.low >> 1);
3347 unsigned HOST_WIDE_INT mh_lo = (mhigh.high << shft) | (mhigh.low >> 1);
3348 if (ml_lo >= mh_lo)
3349 break;
3351 mlow = double_int::from_uhwi (ml_lo);
3352 mhigh = double_int::from_uhwi (mh_lo);
3355 *post_shift_ptr = post_shift;
3356 *lgup_ptr = lgup;
3357 if (n < HOST_BITS_PER_WIDE_INT)
3359 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
3360 *multiplier_ptr = mhigh.low & mask;
3361 return mhigh.low >= mask;
3363 else
3365 *multiplier_ptr = mhigh.low;
3366 return mhigh.high;
3370 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3371 congruent to 1 (mod 2**N). */
3373 static unsigned HOST_WIDE_INT
3374 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
3376 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3378 /* The algorithm notes that the choice y = x satisfies
3379 x*y == 1 mod 2^3, since x is assumed odd.
3380 Each iteration doubles the number of bits of significance in y. */
3382 unsigned HOST_WIDE_INT mask;
3383 unsigned HOST_WIDE_INT y = x;
3384 int nbit = 3;
3386 mask = (n == HOST_BITS_PER_WIDE_INT
3387 ? ~(unsigned HOST_WIDE_INT) 0
3388 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
3390 while (nbit < n)
3392 y = y * (2 - x*y) & mask; /* Modulo 2^N */
3393 nbit *= 2;
3395 return y;
3398 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3399 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3400 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3401 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3402 become signed.
3404 The result is put in TARGET if that is convenient.
3406 MODE is the mode of operation. */
3409 expand_mult_highpart_adjust (enum machine_mode mode, rtx adj_operand, rtx op0,
3410 rtx op1, rtx target, int unsignedp)
3412 rtx tem;
3413 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3415 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3416 GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
3417 tem = expand_and (mode, tem, op1, NULL_RTX);
3418 adj_operand
3419 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3420 adj_operand);
3422 tem = expand_shift (RSHIFT_EXPR, mode, op1,
3423 GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
3424 tem = expand_and (mode, tem, op0, NULL_RTX);
3425 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3426 target);
3428 return target;
3431 /* Subroutine of expmed_mult_highpart. Return the MODE high part of OP. */
3433 static rtx
3434 extract_high_half (enum machine_mode mode, rtx op)
3436 enum machine_mode wider_mode;
3438 if (mode == word_mode)
3439 return gen_highpart (mode, op);
3441 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3443 wider_mode = GET_MODE_WIDER_MODE (mode);
3444 op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3445 GET_MODE_BITSIZE (mode), 0, 1);
3446 return convert_modes (mode, wider_mode, op, 0);
3449 /* Like expmed_mult_highpart, but only consider using a multiplication
3450 optab. OP1 is an rtx for the constant operand. */
3452 static rtx
3453 expmed_mult_highpart_optab (enum machine_mode mode, rtx op0, rtx op1,
3454 rtx target, int unsignedp, int max_cost)
3456 rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3457 enum machine_mode wider_mode;
3458 optab moptab;
3459 rtx tem;
3460 int size;
3461 bool speed = optimize_insn_for_speed_p ();
3463 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3465 wider_mode = GET_MODE_WIDER_MODE (mode);
3466 size = GET_MODE_BITSIZE (mode);
3468 /* Firstly, try using a multiplication insn that only generates the needed
3469 high part of the product, and in the sign flavor of unsignedp. */
3470 if (mul_highpart_cost (speed, mode) < max_cost)
3472 moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3473 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3474 unsignedp, OPTAB_DIRECT);
3475 if (tem)
3476 return tem;
3479 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3480 Need to adjust the result after the multiplication. */
3481 if (size - 1 < BITS_PER_WORD
3482 && (mul_highpart_cost (speed, mode)
3483 + 2 * shift_cost (speed, mode, size-1)
3484 + 4 * add_cost (speed, mode) < max_cost))
3486 moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3487 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3488 unsignedp, OPTAB_DIRECT);
3489 if (tem)
3490 /* We used the wrong signedness. Adjust the result. */
3491 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3492 tem, unsignedp);
3495 /* Try widening multiplication. */
3496 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3497 if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
3498 && mul_widen_cost (speed, wider_mode) < max_cost)
3500 tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3501 unsignedp, OPTAB_WIDEN);
3502 if (tem)
3503 return extract_high_half (mode, tem);
3506 /* Try widening the mode and perform a non-widening multiplication. */
3507 if (optab_handler (smul_optab, wider_mode) != CODE_FOR_nothing
3508 && size - 1 < BITS_PER_WORD
3509 && (mul_cost (speed, wider_mode) + shift_cost (speed, mode, size-1)
3510 < max_cost))
3512 rtx insns, wop0, wop1;
3514 /* We need to widen the operands, for example to ensure the
3515 constant multiplier is correctly sign or zero extended.
3516 Use a sequence to clean-up any instructions emitted by
3517 the conversions if things don't work out. */
3518 start_sequence ();
3519 wop0 = convert_modes (wider_mode, mode, op0, unsignedp);
3520 wop1 = convert_modes (wider_mode, mode, op1, unsignedp);
3521 tem = expand_binop (wider_mode, smul_optab, wop0, wop1, 0,
3522 unsignedp, OPTAB_WIDEN);
3523 insns = get_insns ();
3524 end_sequence ();
3526 if (tem)
3528 emit_insn (insns);
3529 return extract_high_half (mode, tem);
3533 /* Try widening multiplication of opposite signedness, and adjust. */
3534 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3535 if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
3536 && size - 1 < BITS_PER_WORD
3537 && (mul_widen_cost (speed, wider_mode)
3538 + 2 * shift_cost (speed, mode, size-1)
3539 + 4 * add_cost (speed, mode) < max_cost))
3541 tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3542 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3543 if (tem != 0)
3545 tem = extract_high_half (mode, tem);
3546 /* We used the wrong signedness. Adjust the result. */
3547 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3548 target, unsignedp);
3552 return 0;
3555 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3556 putting the high half of the result in TARGET if that is convenient,
3557 and return where the result is. If the operation can not be performed,
3558 0 is returned.
3560 MODE is the mode of operation and result.
3562 UNSIGNEDP nonzero means unsigned multiply.
3564 MAX_COST is the total allowed cost for the expanded RTL. */
3566 static rtx
3567 expmed_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
3568 rtx target, int unsignedp, int max_cost)
3570 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
3571 unsigned HOST_WIDE_INT cnst1;
3572 int extra_cost;
3573 bool sign_adjust = false;
3574 enum mult_variant variant;
3575 struct algorithm alg;
3576 rtx tem;
3577 bool speed = optimize_insn_for_speed_p ();
3579 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3580 /* We can't support modes wider than HOST_BITS_PER_INT. */
3581 gcc_assert (HWI_COMPUTABLE_MODE_P (mode));
3583 cnst1 = INTVAL (op1) & GET_MODE_MASK (mode);
3585 /* We can't optimize modes wider than BITS_PER_WORD.
3586 ??? We might be able to perform double-word arithmetic if
3587 mode == word_mode, however all the cost calculations in
3588 synth_mult etc. assume single-word operations. */
3589 if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3590 return expmed_mult_highpart_optab (mode, op0, op1, target,
3591 unsignedp, max_cost);
3593 extra_cost = shift_cost (speed, mode, GET_MODE_BITSIZE (mode) - 1);
3595 /* Check whether we try to multiply by a negative constant. */
3596 if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3598 sign_adjust = true;
3599 extra_cost += add_cost (speed, mode);
3602 /* See whether shift/add multiplication is cheap enough. */
3603 if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3604 max_cost - extra_cost))
3606 /* See whether the specialized multiplication optabs are
3607 cheaper than the shift/add version. */
3608 tem = expmed_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3609 alg.cost.cost + extra_cost);
3610 if (tem)
3611 return tem;
3613 tem = convert_to_mode (wider_mode, op0, unsignedp);
3614 tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3615 tem = extract_high_half (mode, tem);
3617 /* Adjust result for signedness. */
3618 if (sign_adjust)
3619 tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3621 return tem;
3623 return expmed_mult_highpart_optab (mode, op0, op1, target,
3624 unsignedp, max_cost);
3628 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3630 static rtx
3631 expand_smod_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3633 unsigned HOST_WIDE_INT masklow, maskhigh;
3634 rtx result, temp, shift, label;
3635 int logd;
3637 logd = floor_log2 (d);
3638 result = gen_reg_rtx (mode);
3640 /* Avoid conditional branches when they're expensive. */
3641 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2
3642 && optimize_insn_for_speed_p ())
3644 rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3645 mode, 0, -1);
3646 if (signmask)
3648 signmask = force_reg (mode, signmask);
3649 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3650 shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3652 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3653 which instruction sequence to use. If logical right shifts
3654 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3655 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3657 temp = gen_rtx_LSHIFTRT (mode, result, shift);
3658 if (optab_handler (lshr_optab, mode) == CODE_FOR_nothing
3659 || (set_src_cost (temp, optimize_insn_for_speed_p ())
3660 > COSTS_N_INSNS (2)))
3662 temp = expand_binop (mode, xor_optab, op0, signmask,
3663 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3664 temp = expand_binop (mode, sub_optab, temp, signmask,
3665 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3666 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3667 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3668 temp = expand_binop (mode, xor_optab, temp, signmask,
3669 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3670 temp = expand_binop (mode, sub_optab, temp, signmask,
3671 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3673 else
3675 signmask = expand_binop (mode, lshr_optab, signmask, shift,
3676 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3677 signmask = force_reg (mode, signmask);
3679 temp = expand_binop (mode, add_optab, op0, signmask,
3680 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3681 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3682 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3683 temp = expand_binop (mode, sub_optab, temp, signmask,
3684 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3686 return temp;
3690 /* Mask contains the mode's signbit and the significant bits of the
3691 modulus. By including the signbit in the operation, many targets
3692 can avoid an explicit compare operation in the following comparison
3693 against zero. */
3695 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3696 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3698 masklow |= (HOST_WIDE_INT) -1 << (GET_MODE_BITSIZE (mode) - 1);
3699 maskhigh = -1;
3701 else
3702 maskhigh = (HOST_WIDE_INT) -1
3703 << (GET_MODE_BITSIZE (mode) - HOST_BITS_PER_WIDE_INT - 1);
3705 temp = expand_binop (mode, and_optab, op0,
3706 immed_double_const (masklow, maskhigh, mode),
3707 result, 1, OPTAB_LIB_WIDEN);
3708 if (temp != result)
3709 emit_move_insn (result, temp);
3711 label = gen_label_rtx ();
3712 do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3714 temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3715 0, OPTAB_LIB_WIDEN);
3716 masklow = (HOST_WIDE_INT) -1 << logd;
3717 maskhigh = -1;
3718 temp = expand_binop (mode, ior_optab, temp,
3719 immed_double_const (masklow, maskhigh, mode),
3720 result, 1, OPTAB_LIB_WIDEN);
3721 temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3722 0, OPTAB_LIB_WIDEN);
3723 if (temp != result)
3724 emit_move_insn (result, temp);
3725 emit_label (label);
3726 return result;
3729 /* Expand signed division of OP0 by a power of two D in mode MODE.
3730 This routine is only called for positive values of D. */
3732 static rtx
3733 expand_sdiv_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3735 rtx temp, label;
3736 int logd;
3738 logd = floor_log2 (d);
3740 if (d == 2
3741 && BRANCH_COST (optimize_insn_for_speed_p (),
3742 false) >= 1)
3744 temp = gen_reg_rtx (mode);
3745 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3746 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3747 0, OPTAB_LIB_WIDEN);
3748 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3751 #ifdef HAVE_conditional_move
3752 if (BRANCH_COST (optimize_insn_for_speed_p (), false)
3753 >= 2)
3755 rtx temp2;
3757 /* ??? emit_conditional_move forces a stack adjustment via
3758 compare_from_rtx so, if the sequence is discarded, it will
3759 be lost. Do it now instead. */
3760 do_pending_stack_adjust ();
3762 start_sequence ();
3763 temp2 = copy_to_mode_reg (mode, op0);
3764 temp = expand_binop (mode, add_optab, temp2, GEN_INT (d-1),
3765 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3766 temp = force_reg (mode, temp);
3768 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3769 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3770 mode, temp, temp2, mode, 0);
3771 if (temp2)
3773 rtx seq = get_insns ();
3774 end_sequence ();
3775 emit_insn (seq);
3776 return expand_shift (RSHIFT_EXPR, mode, temp2, logd, NULL_RTX, 0);
3778 end_sequence ();
3780 #endif
3782 if (BRANCH_COST (optimize_insn_for_speed_p (),
3783 false) >= 2)
3785 int ushift = GET_MODE_BITSIZE (mode) - logd;
3787 temp = gen_reg_rtx (mode);
3788 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3789 if (shift_cost (optimize_insn_for_speed_p (), mode, ushift)
3790 > COSTS_N_INSNS (1))
3791 temp = expand_binop (mode, and_optab, temp, GEN_INT (d - 1),
3792 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3793 else
3794 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3795 ushift, NULL_RTX, 1);
3796 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3797 0, OPTAB_LIB_WIDEN);
3798 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3801 label = gen_label_rtx ();
3802 temp = copy_to_mode_reg (mode, op0);
3803 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3804 expand_inc (temp, GEN_INT (d - 1));
3805 emit_label (label);
3806 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3809 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3810 if that is convenient, and returning where the result is.
3811 You may request either the quotient or the remainder as the result;
3812 specify REM_FLAG nonzero to get the remainder.
3814 CODE is the expression code for which kind of division this is;
3815 it controls how rounding is done. MODE is the machine mode to use.
3816 UNSIGNEDP nonzero means do unsigned division. */
3818 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3819 and then correct it by or'ing in missing high bits
3820 if result of ANDI is nonzero.
3821 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3822 This could optimize to a bfexts instruction.
3823 But C doesn't use these operations, so their optimizations are
3824 left for later. */
3825 /* ??? For modulo, we don't actually need the highpart of the first product,
3826 the low part will do nicely. And for small divisors, the second multiply
3827 can also be a low-part only multiply or even be completely left out.
3828 E.g. to calculate the remainder of a division by 3 with a 32 bit
3829 multiply, multiply with 0x55555556 and extract the upper two bits;
3830 the result is exact for inputs up to 0x1fffffff.
3831 The input range can be reduced by using cross-sum rules.
3832 For odd divisors >= 3, the following table gives right shift counts
3833 so that if a number is shifted by an integer multiple of the given
3834 amount, the remainder stays the same:
3835 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3836 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3837 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3838 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3839 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3841 Cross-sum rules for even numbers can be derived by leaving as many bits
3842 to the right alone as the divisor has zeros to the right.
3843 E.g. if x is an unsigned 32 bit number:
3844 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3848 expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
3849 rtx op0, rtx op1, rtx target, int unsignedp)
3851 enum machine_mode compute_mode;
3852 rtx tquotient;
3853 rtx quotient = 0, remainder = 0;
3854 rtx last;
3855 int size;
3856 rtx insn;
3857 optab optab1, optab2;
3858 int op1_is_constant, op1_is_pow2 = 0;
3859 int max_cost, extra_cost;
3860 static HOST_WIDE_INT last_div_const = 0;
3861 bool speed = optimize_insn_for_speed_p ();
3863 op1_is_constant = CONST_INT_P (op1);
3864 if (op1_is_constant)
3866 unsigned HOST_WIDE_INT ext_op1 = UINTVAL (op1);
3867 if (unsignedp)
3868 ext_op1 &= GET_MODE_MASK (mode);
3869 op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
3870 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
3874 This is the structure of expand_divmod:
3876 First comes code to fix up the operands so we can perform the operations
3877 correctly and efficiently.
3879 Second comes a switch statement with code specific for each rounding mode.
3880 For some special operands this code emits all RTL for the desired
3881 operation, for other cases, it generates only a quotient and stores it in
3882 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3883 to indicate that it has not done anything.
3885 Last comes code that finishes the operation. If QUOTIENT is set and
3886 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3887 QUOTIENT is not set, it is computed using trunc rounding.
3889 We try to generate special code for division and remainder when OP1 is a
3890 constant. If |OP1| = 2**n we can use shifts and some other fast
3891 operations. For other values of OP1, we compute a carefully selected
3892 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3893 by m.
3895 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3896 half of the product. Different strategies for generating the product are
3897 implemented in expmed_mult_highpart.
3899 If what we actually want is the remainder, we generate that by another
3900 by-constant multiplication and a subtraction. */
3902 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3903 code below will malfunction if we are, so check here and handle
3904 the special case if so. */
3905 if (op1 == const1_rtx)
3906 return rem_flag ? const0_rtx : op0;
3908 /* When dividing by -1, we could get an overflow.
3909 negv_optab can handle overflows. */
3910 if (! unsignedp && op1 == constm1_rtx)
3912 if (rem_flag)
3913 return const0_rtx;
3914 return expand_unop (mode, flag_trapv && GET_MODE_CLASS(mode) == MODE_INT
3915 ? negv_optab : neg_optab, op0, target, 0);
3918 if (target
3919 /* Don't use the function value register as a target
3920 since we have to read it as well as write it,
3921 and function-inlining gets confused by this. */
3922 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3923 /* Don't clobber an operand while doing a multi-step calculation. */
3924 || ((rem_flag || op1_is_constant)
3925 && (reg_mentioned_p (target, op0)
3926 || (MEM_P (op0) && MEM_P (target))))
3927 || reg_mentioned_p (target, op1)
3928 || (MEM_P (op1) && MEM_P (target))))
3929 target = 0;
3931 /* Get the mode in which to perform this computation. Normally it will
3932 be MODE, but sometimes we can't do the desired operation in MODE.
3933 If so, pick a wider mode in which we can do the operation. Convert
3934 to that mode at the start to avoid repeated conversions.
3936 First see what operations we need. These depend on the expression
3937 we are evaluating. (We assume that divxx3 insns exist under the
3938 same conditions that modxx3 insns and that these insns don't normally
3939 fail. If these assumptions are not correct, we may generate less
3940 efficient code in some cases.)
3942 Then see if we find a mode in which we can open-code that operation
3943 (either a division, modulus, or shift). Finally, check for the smallest
3944 mode for which we can do the operation with a library call. */
3946 /* We might want to refine this now that we have division-by-constant
3947 optimization. Since expmed_mult_highpart tries so many variants, it is
3948 not straightforward to generalize this. Maybe we should make an array
3949 of possible modes in init_expmed? Save this for GCC 2.7. */
3951 optab1 = ((op1_is_pow2 && op1 != const0_rtx)
3952 ? (unsignedp ? lshr_optab : ashr_optab)
3953 : (unsignedp ? udiv_optab : sdiv_optab));
3954 optab2 = ((op1_is_pow2 && op1 != const0_rtx)
3955 ? optab1
3956 : (unsignedp ? udivmod_optab : sdivmod_optab));
3958 for (compute_mode = mode; compute_mode != VOIDmode;
3959 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3960 if (optab_handler (optab1, compute_mode) != CODE_FOR_nothing
3961 || optab_handler (optab2, compute_mode) != CODE_FOR_nothing)
3962 break;
3964 if (compute_mode == VOIDmode)
3965 for (compute_mode = mode; compute_mode != VOIDmode;
3966 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3967 if (optab_libfunc (optab1, compute_mode)
3968 || optab_libfunc (optab2, compute_mode))
3969 break;
3971 /* If we still couldn't find a mode, use MODE, but expand_binop will
3972 probably die. */
3973 if (compute_mode == VOIDmode)
3974 compute_mode = mode;
3976 if (target && GET_MODE (target) == compute_mode)
3977 tquotient = target;
3978 else
3979 tquotient = gen_reg_rtx (compute_mode);
3981 size = GET_MODE_BITSIZE (compute_mode);
3982 #if 0
3983 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3984 (mode), and thereby get better code when OP1 is a constant. Do that
3985 later. It will require going over all usages of SIZE below. */
3986 size = GET_MODE_BITSIZE (mode);
3987 #endif
3989 /* Only deduct something for a REM if the last divide done was
3990 for a different constant. Then set the constant of the last
3991 divide. */
3992 max_cost = (unsignedp
3993 ? udiv_cost (speed, compute_mode)
3994 : sdiv_cost (speed, compute_mode));
3995 if (rem_flag && ! (last_div_const != 0 && op1_is_constant
3996 && INTVAL (op1) == last_div_const))
3997 max_cost -= (mul_cost (speed, compute_mode)
3998 + add_cost (speed, compute_mode));
4000 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
4002 /* Now convert to the best mode to use. */
4003 if (compute_mode != mode)
4005 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
4006 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
4008 /* convert_modes may have placed op1 into a register, so we
4009 must recompute the following. */
4010 op1_is_constant = CONST_INT_P (op1);
4011 op1_is_pow2 = (op1_is_constant
4012 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4013 || (! unsignedp
4014 && EXACT_POWER_OF_2_OR_ZERO_P (-UINTVAL (op1))))));
4017 /* If one of the operands is a volatile MEM, copy it into a register. */
4019 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
4020 op0 = force_reg (compute_mode, op0);
4021 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
4022 op1 = force_reg (compute_mode, op1);
4024 /* If we need the remainder or if OP1 is constant, we need to
4025 put OP0 in a register in case it has any queued subexpressions. */
4026 if (rem_flag || op1_is_constant)
4027 op0 = force_reg (compute_mode, op0);
4029 last = get_last_insn ();
4031 /* Promote floor rounding to trunc rounding for unsigned operations. */
4032 if (unsignedp)
4034 if (code == FLOOR_DIV_EXPR)
4035 code = TRUNC_DIV_EXPR;
4036 if (code == FLOOR_MOD_EXPR)
4037 code = TRUNC_MOD_EXPR;
4038 if (code == EXACT_DIV_EXPR && op1_is_pow2)
4039 code = TRUNC_DIV_EXPR;
4042 if (op1 != const0_rtx)
4043 switch (code)
4045 case TRUNC_MOD_EXPR:
4046 case TRUNC_DIV_EXPR:
4047 if (op1_is_constant)
4049 if (unsignedp)
4051 unsigned HOST_WIDE_INT mh, ml;
4052 int pre_shift, post_shift;
4053 int dummy;
4054 unsigned HOST_WIDE_INT d = (INTVAL (op1)
4055 & GET_MODE_MASK (compute_mode));
4057 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4059 pre_shift = floor_log2 (d);
4060 if (rem_flag)
4062 remainder
4063 = expand_binop (compute_mode, and_optab, op0,
4064 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4065 remainder, 1,
4066 OPTAB_LIB_WIDEN);
4067 if (remainder)
4068 return gen_lowpart (mode, remainder);
4070 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4071 pre_shift, tquotient, 1);
4073 else if (size <= HOST_BITS_PER_WIDE_INT)
4075 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
4077 /* Most significant bit of divisor is set; emit an scc
4078 insn. */
4079 quotient = emit_store_flag_force (tquotient, GEU, op0, op1,
4080 compute_mode, 1, 1);
4082 else
4084 /* Find a suitable multiplier and right shift count
4085 instead of multiplying with D. */
4087 mh = choose_multiplier (d, size, size,
4088 &ml, &post_shift, &dummy);
4090 /* If the suggested multiplier is more than SIZE bits,
4091 we can do better for even divisors, using an
4092 initial right shift. */
4093 if (mh != 0 && (d & 1) == 0)
4095 pre_shift = floor_log2 (d & -d);
4096 mh = choose_multiplier (d >> pre_shift, size,
4097 size - pre_shift,
4098 &ml, &post_shift, &dummy);
4099 gcc_assert (!mh);
4101 else
4102 pre_shift = 0;
4104 if (mh != 0)
4106 rtx t1, t2, t3, t4;
4108 if (post_shift - 1 >= BITS_PER_WORD)
4109 goto fail1;
4111 extra_cost
4112 = (shift_cost (speed, compute_mode, post_shift - 1)
4113 + shift_cost (speed, compute_mode, 1)
4114 + 2 * add_cost (speed, compute_mode));
4115 t1 = expmed_mult_highpart (compute_mode, op0,
4116 GEN_INT (ml),
4117 NULL_RTX, 1,
4118 max_cost - extra_cost);
4119 if (t1 == 0)
4120 goto fail1;
4121 t2 = force_operand (gen_rtx_MINUS (compute_mode,
4122 op0, t1),
4123 NULL_RTX);
4124 t3 = expand_shift (RSHIFT_EXPR, compute_mode,
4125 t2, 1, NULL_RTX, 1);
4126 t4 = force_operand (gen_rtx_PLUS (compute_mode,
4127 t1, t3),
4128 NULL_RTX);
4129 quotient = expand_shift
4130 (RSHIFT_EXPR, compute_mode, t4,
4131 post_shift - 1, tquotient, 1);
4133 else
4135 rtx t1, t2;
4137 if (pre_shift >= BITS_PER_WORD
4138 || post_shift >= BITS_PER_WORD)
4139 goto fail1;
4141 t1 = expand_shift
4142 (RSHIFT_EXPR, compute_mode, op0,
4143 pre_shift, NULL_RTX, 1);
4144 extra_cost
4145 = (shift_cost (speed, compute_mode, pre_shift)
4146 + shift_cost (speed, compute_mode, post_shift));
4147 t2 = expmed_mult_highpart (compute_mode, t1,
4148 GEN_INT (ml),
4149 NULL_RTX, 1,
4150 max_cost - extra_cost);
4151 if (t2 == 0)
4152 goto fail1;
4153 quotient = expand_shift
4154 (RSHIFT_EXPR, compute_mode, t2,
4155 post_shift, tquotient, 1);
4159 else /* Too wide mode to use tricky code */
4160 break;
4162 insn = get_last_insn ();
4163 if (insn != last)
4164 set_dst_reg_note (insn, REG_EQUAL,
4165 gen_rtx_UDIV (compute_mode, op0, op1),
4166 quotient);
4168 else /* TRUNC_DIV, signed */
4170 unsigned HOST_WIDE_INT ml;
4171 int lgup, post_shift;
4172 rtx mlr;
4173 HOST_WIDE_INT d = INTVAL (op1);
4174 unsigned HOST_WIDE_INT abs_d;
4176 /* Since d might be INT_MIN, we have to cast to
4177 unsigned HOST_WIDE_INT before negating to avoid
4178 undefined signed overflow. */
4179 abs_d = (d >= 0
4180 ? (unsigned HOST_WIDE_INT) d
4181 : - (unsigned HOST_WIDE_INT) d);
4183 /* n rem d = n rem -d */
4184 if (rem_flag && d < 0)
4186 d = abs_d;
4187 op1 = gen_int_mode (abs_d, compute_mode);
4190 if (d == 1)
4191 quotient = op0;
4192 else if (d == -1)
4193 quotient = expand_unop (compute_mode, neg_optab, op0,
4194 tquotient, 0);
4195 else if (HOST_BITS_PER_WIDE_INT >= size
4196 && abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
4198 /* This case is not handled correctly below. */
4199 quotient = emit_store_flag (tquotient, EQ, op0, op1,
4200 compute_mode, 1, 1);
4201 if (quotient == 0)
4202 goto fail1;
4204 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
4205 && (rem_flag
4206 ? smod_pow2_cheap (speed, compute_mode)
4207 : sdiv_pow2_cheap (speed, compute_mode))
4208 /* We assume that cheap metric is true if the
4209 optab has an expander for this mode. */
4210 && ((optab_handler ((rem_flag ? smod_optab
4211 : sdiv_optab),
4212 compute_mode)
4213 != CODE_FOR_nothing)
4214 || (optab_handler (sdivmod_optab,
4215 compute_mode)
4216 != CODE_FOR_nothing)))
4218 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
4220 if (rem_flag)
4222 remainder = expand_smod_pow2 (compute_mode, op0, d);
4223 if (remainder)
4224 return gen_lowpart (mode, remainder);
4227 if (sdiv_pow2_cheap (speed, compute_mode)
4228 && ((optab_handler (sdiv_optab, compute_mode)
4229 != CODE_FOR_nothing)
4230 || (optab_handler (sdivmod_optab, compute_mode)
4231 != CODE_FOR_nothing)))
4232 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
4233 compute_mode, op0,
4234 gen_int_mode (abs_d,
4235 compute_mode),
4236 NULL_RTX, 0);
4237 else
4238 quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
4240 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4241 negate the quotient. */
4242 if (d < 0)
4244 insn = get_last_insn ();
4245 if (insn != last
4246 && abs_d < ((unsigned HOST_WIDE_INT) 1
4247 << (HOST_BITS_PER_WIDE_INT - 1)))
4248 set_dst_reg_note (insn, REG_EQUAL,
4249 gen_rtx_DIV (compute_mode, op0,
4250 gen_int_mode
4251 (abs_d,
4252 compute_mode)),
4253 quotient);
4255 quotient = expand_unop (compute_mode, neg_optab,
4256 quotient, quotient, 0);
4259 else if (size <= HOST_BITS_PER_WIDE_INT)
4261 choose_multiplier (abs_d, size, size - 1,
4262 &ml, &post_shift, &lgup);
4263 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
4265 rtx t1, t2, t3;
4267 if (post_shift >= BITS_PER_WORD
4268 || size - 1 >= BITS_PER_WORD)
4269 goto fail1;
4271 extra_cost = (shift_cost (speed, compute_mode, post_shift)
4272 + shift_cost (speed, compute_mode, size - 1)
4273 + add_cost (speed, compute_mode));
4274 t1 = expmed_mult_highpart (compute_mode, op0,
4275 GEN_INT (ml), NULL_RTX, 0,
4276 max_cost - extra_cost);
4277 if (t1 == 0)
4278 goto fail1;
4279 t2 = expand_shift
4280 (RSHIFT_EXPR, compute_mode, t1,
4281 post_shift, NULL_RTX, 0);
4282 t3 = expand_shift
4283 (RSHIFT_EXPR, compute_mode, op0,
4284 size - 1, NULL_RTX, 0);
4285 if (d < 0)
4286 quotient
4287 = force_operand (gen_rtx_MINUS (compute_mode,
4288 t3, t2),
4289 tquotient);
4290 else
4291 quotient
4292 = force_operand (gen_rtx_MINUS (compute_mode,
4293 t2, t3),
4294 tquotient);
4296 else
4298 rtx t1, t2, t3, t4;
4300 if (post_shift >= BITS_PER_WORD
4301 || size - 1 >= BITS_PER_WORD)
4302 goto fail1;
4304 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
4305 mlr = gen_int_mode (ml, compute_mode);
4306 extra_cost = (shift_cost (speed, compute_mode, post_shift)
4307 + shift_cost (speed, compute_mode, size - 1)
4308 + 2 * add_cost (speed, compute_mode));
4309 t1 = expmed_mult_highpart (compute_mode, op0, mlr,
4310 NULL_RTX, 0,
4311 max_cost - extra_cost);
4312 if (t1 == 0)
4313 goto fail1;
4314 t2 = force_operand (gen_rtx_PLUS (compute_mode,
4315 t1, op0),
4316 NULL_RTX);
4317 t3 = expand_shift
4318 (RSHIFT_EXPR, compute_mode, t2,
4319 post_shift, NULL_RTX, 0);
4320 t4 = expand_shift
4321 (RSHIFT_EXPR, compute_mode, op0,
4322 size - 1, NULL_RTX, 0);
4323 if (d < 0)
4324 quotient
4325 = force_operand (gen_rtx_MINUS (compute_mode,
4326 t4, t3),
4327 tquotient);
4328 else
4329 quotient
4330 = force_operand (gen_rtx_MINUS (compute_mode,
4331 t3, t4),
4332 tquotient);
4335 else /* Too wide mode to use tricky code */
4336 break;
4338 insn = get_last_insn ();
4339 if (insn != last)
4340 set_dst_reg_note (insn, REG_EQUAL,
4341 gen_rtx_DIV (compute_mode, op0, op1),
4342 quotient);
4344 break;
4346 fail1:
4347 delete_insns_since (last);
4348 break;
4350 case FLOOR_DIV_EXPR:
4351 case FLOOR_MOD_EXPR:
4352 /* We will come here only for signed operations. */
4353 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4355 unsigned HOST_WIDE_INT mh, ml;
4356 int pre_shift, lgup, post_shift;
4357 HOST_WIDE_INT d = INTVAL (op1);
4359 if (d > 0)
4361 /* We could just as easily deal with negative constants here,
4362 but it does not seem worth the trouble for GCC 2.6. */
4363 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4365 pre_shift = floor_log2 (d);
4366 if (rem_flag)
4368 remainder = expand_binop (compute_mode, and_optab, op0,
4369 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4370 remainder, 0, OPTAB_LIB_WIDEN);
4371 if (remainder)
4372 return gen_lowpart (mode, remainder);
4374 quotient = expand_shift
4375 (RSHIFT_EXPR, compute_mode, op0,
4376 pre_shift, tquotient, 0);
4378 else
4380 rtx t1, t2, t3, t4;
4382 mh = choose_multiplier (d, size, size - 1,
4383 &ml, &post_shift, &lgup);
4384 gcc_assert (!mh);
4386 if (post_shift < BITS_PER_WORD
4387 && size - 1 < BITS_PER_WORD)
4389 t1 = expand_shift
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 (compute_mode, t2,
4398 GEN_INT (ml), NULL_RTX, 1,
4399 max_cost - extra_cost);
4400 if (t3 != 0)
4402 t4 = expand_shift
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,
4407 OPTAB_WIDEN);
4412 else
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,
4418 0, OPTAB_WIDEN);
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),
4423 NULL_RTX);
4424 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
4425 NULL_RTX, 0);
4426 if (t4)
4428 rtx t5;
4429 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
4430 NULL_RTX, 0);
4431 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4432 t4, t5),
4433 tquotient);
4438 if (quotient != 0)
4439 break;
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);
4450 if (rem_flag)
4452 remainder
4453 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4454 quotient = gen_reg_rtx (compute_mode);
4456 else
4458 quotient
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. */
4468 rtx tem;
4469 rtx 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);
4476 emit_label (label);
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 label1, label2, label3, label4, label5;
4484 rtx adjusted_op0;
4485 rtx tem;
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));
4501 emit_barrier ();
4502 emit_label (label1);
4503 expand_inc (adjusted_op0, const1_rtx);
4504 emit_jump_insn (gen_jump (label4));
4505 emit_barrier ();
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));
4513 emit_barrier ();
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);
4524 break;
4526 case CEIL_DIV_EXPR:
4527 case CEIL_MOD_EXPR:
4528 if (unsignedp)
4530 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
4532 rtx t1, t2, t3;
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 (d - 1),
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);
4542 if (t3 == 0)
4544 rtx lab;
4545 lab = gen_label_rtx ();
4546 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4547 expand_inc (t1, const1_rtx);
4548 emit_label (lab);
4549 quotient = t1;
4551 else
4552 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4553 t1, t3),
4554 tquotient);
4555 break;
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);
4566 if (rem_flag)
4568 remainder = (REG_P (target)
4569 ? target : gen_reg_rtx (compute_mode));
4570 quotient = gen_reg_rtx (compute_mode);
4572 else
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,
4580 remainder, 1))
4582 /* This could be computed with a branch-less sequence.
4583 Save that for later. */
4584 rtx 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);
4589 emit_label (label);
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 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));
4607 emit_barrier ();
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);
4618 else /* signed */
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
4627 languages (Ada). */
4629 rtx t1, t2, t3;
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 (d - 1),
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);
4639 if (t3 == 0)
4641 rtx lab;
4642 lab = gen_label_rtx ();
4643 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4644 expand_inc (t1, const1_rtx);
4645 emit_label (lab);
4646 quotient = t1;
4648 else
4649 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4650 t1, t3),
4651 tquotient);
4652 break;
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);
4662 if (rem_flag)
4664 remainder= (REG_P (target)
4665 ? target : gen_reg_rtx (compute_mode));
4666 quotient = gen_reg_rtx (compute_mode);
4668 else
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,
4676 remainder, 0))
4678 /* This could be computed with a branch-less sequence.
4679 Save that for later. */
4680 rtx tem;
4681 rtx 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);
4689 emit_label (label);
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 label1, label2, label3, label4, label5;
4697 rtx adjusted_op0;
4698 rtx tem;
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));
4715 emit_barrier ();
4716 emit_label (label1);
4717 expand_dec (adjusted_op0, const1_rtx);
4718 emit_jump_insn (gen_jump (label4));
4719 emit_barrier ();
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));
4728 emit_barrier ();
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);
4740 break;
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;
4747 int pre_shift;
4748 rtx t1;
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),
4756 NULL_RTX, 1);
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),
4762 quotient);
4764 break;
4766 case ROUND_DIV_EXPR:
4767 case ROUND_MOD_EXPR:
4768 if (unsignedp)
4770 rtx tem;
4771 rtx 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)
4777 rtx tem;
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);
4789 emit_label (label);
4791 else
4793 rtx abs_rem, abs_op1, tem, mask;
4794 rtx 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)
4800 rtx tem;
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,
4810 1, NULL_RTX, 1);
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);
4826 emit_label (label);
4828 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4830 default:
4831 gcc_unreachable ();
4834 if (quotient == 0)
4836 if (target && GET_MODE (target) != compute_mode)
4837 target = 0;
4839 if (rem_flag)
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
4848 remainder
4849 4) try the same things with widening allowed. */
4850 remainder
4851 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4852 op0, op1, target,
4853 unsignedp,
4854 ((optab_handler (optab2, compute_mode)
4855 != CODE_FOR_nothing)
4856 ? OPTAB_DIRECT : OPTAB_WIDEN));
4857 if (remainder == 0)
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
4863 ? udivmod_optab
4864 : sdivmod_optab),
4865 op0, op1,
4866 NULL_RTX, remainder, unsignedp))
4867 remainder = 0;
4870 if (remainder)
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. */
4878 quotient
4879 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
4880 op0, op1, rem_flag ? NULL_RTX : target,
4881 unsignedp,
4882 ((optab_handler (optab2, compute_mode)
4883 != CODE_FOR_nothing)
4884 ? OPTAB_DIRECT : OPTAB_WIDEN));
4886 if (quotient == 0)
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,
4892 op0, op1,
4893 quotient, NULL_RTX, unsignedp))
4895 quotient = 0;
4896 if (! rem_flag)
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,
4901 op0, op1, target,
4902 unsignedp, OPTAB_LIB_WIDEN);
4907 if (rem_flag)
4909 if (target && GET_MODE (target) != compute_mode)
4910 target = 0;
4912 if (quotient == 0)
4914 /* No divide instruction either. Use library for remainder. */
4915 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4916 op0, op1, target,
4917 unsignedp, OPTAB_LIB_WIDEN);
4918 /* No remainder function. Try a quotient-and-remainder
4919 function, keeping the remainder. */
4920 if (!remainder)
4922 remainder = gen_reg_rtx (compute_mode);
4923 if (!expand_twoval_binop_libfunc
4924 (unsignedp ? udivmod_optab : sdivmod_optab,
4925 op0, op1,
4926 NULL_RTX, remainder,
4927 unsignedp ? UMOD : MOD))
4928 remainder = NULL_RTX;
4931 else
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,
4938 OPTAB_LIB_WIDEN);
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. */
4950 tree
4951 make_tree (tree type, rtx x)
4953 tree t;
4955 switch (GET_CODE (x))
4957 case CONST_INT:
4959 HOST_WIDE_INT hi = 0;
4961 if (INTVAL (x) < 0
4962 && !(TYPE_UNSIGNED (type)
4963 && (GET_MODE_BITSIZE (TYPE_MODE (type))
4964 < HOST_BITS_PER_WIDE_INT)))
4965 hi = -1;
4967 t = build_int_cst_wide (type, INTVAL (x), hi);
4969 return t;
4972 case CONST_DOUBLE:
4973 if (GET_MODE (x) == VOIDmode)
4974 t = build_int_cst_wide (type,
4975 CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
4976 else
4978 REAL_VALUE_TYPE d;
4980 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
4981 t = build_real (type, d);
4984 return t;
4986 case CONST_VECTOR:
4988 int units = CONST_VECTOR_NUNITS (x);
4989 tree itype = TREE_TYPE (type);
4990 tree *elts;
4991 int i;
4993 /* Build a tree with vector elements. */
4994 elts = XALLOCAVEC (tree, units);
4995 for (i = units - 1; i >= 0; --i)
4997 rtx elt = CONST_VECTOR_ELT (x, i);
4998 elts[i] = make_tree (itype, elt);
5001 return build_vector (type, elts);
5004 case PLUS:
5005 return fold_build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5006 make_tree (type, XEXP (x, 1)));
5008 case MINUS:
5009 return fold_build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5010 make_tree (type, XEXP (x, 1)));
5012 case NEG:
5013 return fold_build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0)));
5015 case MULT:
5016 return fold_build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
5017 make_tree (type, XEXP (x, 1)));
5019 case ASHIFT:
5020 return fold_build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
5021 make_tree (type, XEXP (x, 1)));
5023 case LSHIFTRT:
5024 t = unsigned_type_for (type);
5025 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5026 make_tree (t, XEXP (x, 0)),
5027 make_tree (type, XEXP (x, 1))));
5029 case ASHIFTRT:
5030 t = signed_type_for (type);
5031 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5032 make_tree (t, XEXP (x, 0)),
5033 make_tree (type, XEXP (x, 1))));
5035 case DIV:
5036 if (TREE_CODE (type) != REAL_TYPE)
5037 t = signed_type_for (type);
5038 else
5039 t = type;
5041 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5042 make_tree (t, XEXP (x, 0)),
5043 make_tree (t, XEXP (x, 1))));
5044 case UDIV:
5045 t = unsigned_type_for (type);
5046 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5047 make_tree (t, XEXP (x, 0)),
5048 make_tree (t, XEXP (x, 1))));
5050 case SIGN_EXTEND:
5051 case ZERO_EXTEND:
5052 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
5053 GET_CODE (x) == ZERO_EXTEND);
5054 return fold_convert (type, make_tree (t, XEXP (x, 0)));
5056 case CONST:
5057 return make_tree (type, XEXP (x, 0));
5059 case SYMBOL_REF:
5060 t = SYMBOL_REF_DECL (x);
5061 if (t)
5062 return fold_convert (type, build_fold_addr_expr (t));
5063 /* else fall through. */
5065 default:
5066 t = build_decl (RTL_LOCATION (x), VAR_DECL, NULL_TREE, type);
5068 /* If TYPE is a POINTER_TYPE, we might need to convert X from
5069 address mode to pointer mode. */
5070 if (POINTER_TYPE_P (type))
5071 x = convert_memory_address_addr_space
5072 (TYPE_MODE (type), x, TYPE_ADDR_SPACE (TREE_TYPE (type)));
5074 /* Note that we do *not* use SET_DECL_RTL here, because we do not
5075 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
5076 t->decl_with_rtl.rtl = x;
5078 return t;
5082 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5083 and returning TARGET.
5085 If TARGET is 0, a pseudo-register or constant is returned. */
5088 expand_and (enum machine_mode mode, rtx op0, rtx op1, rtx target)
5090 rtx tem = 0;
5092 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
5093 tem = simplify_binary_operation (AND, mode, op0, op1);
5094 if (tem == 0)
5095 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
5097 if (target == 0)
5098 target = tem;
5099 else if (tem != target)
5100 emit_move_insn (target, tem);
5101 return target;
5104 /* Helper function for emit_store_flag. */
5105 static rtx
5106 emit_cstore (rtx target, enum insn_code icode, enum rtx_code code,
5107 enum machine_mode mode, enum machine_mode compare_mode,
5108 int unsignedp, rtx x, rtx y, int normalizep,
5109 enum machine_mode target_mode)
5111 struct expand_operand ops[4];
5112 rtx op0, last, comparison, subtarget;
5113 enum machine_mode result_mode = targetm.cstore_mode (icode);
5115 last = get_last_insn ();
5116 x = prepare_operand (icode, x, 2, mode, compare_mode, unsignedp);
5117 y = prepare_operand (icode, y, 3, mode, compare_mode, unsignedp);
5118 if (!x || !y)
5120 delete_insns_since (last);
5121 return NULL_RTX;
5124 if (target_mode == VOIDmode)
5125 target_mode = result_mode;
5126 if (!target)
5127 target = gen_reg_rtx (target_mode);
5129 comparison = gen_rtx_fmt_ee (code, result_mode, x, y);
5131 create_output_operand (&ops[0], optimize ? NULL_RTX : target, result_mode);
5132 create_fixed_operand (&ops[1], comparison);
5133 create_fixed_operand (&ops[2], x);
5134 create_fixed_operand (&ops[3], y);
5135 if (!maybe_expand_insn (icode, 4, ops))
5137 delete_insns_since (last);
5138 return NULL_RTX;
5140 subtarget = ops[0].value;
5142 /* If we are converting to a wider mode, first convert to
5143 TARGET_MODE, then normalize. This produces better combining
5144 opportunities on machines that have a SIGN_EXTRACT when we are
5145 testing a single bit. This mostly benefits the 68k.
5147 If STORE_FLAG_VALUE does not have the sign bit set when
5148 interpreted in MODE, we can do this conversion as unsigned, which
5149 is usually more efficient. */
5150 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (result_mode))
5152 convert_move (target, subtarget,
5153 val_signbit_known_clear_p (result_mode,
5154 STORE_FLAG_VALUE));
5155 op0 = target;
5156 result_mode = target_mode;
5158 else
5159 op0 = subtarget;
5161 /* If we want to keep subexpressions around, don't reuse our last
5162 target. */
5163 if (optimize)
5164 subtarget = 0;
5166 /* Now normalize to the proper value in MODE. Sometimes we don't
5167 have to do anything. */
5168 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
5170 /* STORE_FLAG_VALUE might be the most negative number, so write
5171 the comparison this way to avoid a compiler-time warning. */
5172 else if (- normalizep == STORE_FLAG_VALUE)
5173 op0 = expand_unop (result_mode, neg_optab, op0, subtarget, 0);
5175 /* We don't want to use STORE_FLAG_VALUE < 0 below since this makes
5176 it hard to use a value of just the sign bit due to ANSI integer
5177 constant typing rules. */
5178 else if (val_signbit_known_set_p (result_mode, STORE_FLAG_VALUE))
5179 op0 = expand_shift (RSHIFT_EXPR, result_mode, op0,
5180 GET_MODE_BITSIZE (result_mode) - 1, subtarget,
5181 normalizep == 1);
5182 else
5184 gcc_assert (STORE_FLAG_VALUE & 1);
5186 op0 = expand_and (result_mode, op0, const1_rtx, subtarget);
5187 if (normalizep == -1)
5188 op0 = expand_unop (result_mode, neg_optab, op0, op0, 0);
5191 /* If we were converting to a smaller mode, do the conversion now. */
5192 if (target_mode != result_mode)
5194 convert_move (target, op0, 0);
5195 return target;
5197 else
5198 return op0;
5202 /* A subroutine of emit_store_flag only including "tricks" that do not
5203 need a recursive call. These are kept separate to avoid infinite
5204 loops. */
5206 static rtx
5207 emit_store_flag_1 (rtx target, enum rtx_code code, rtx op0, rtx op1,
5208 enum machine_mode mode, int unsignedp, int normalizep,
5209 enum machine_mode target_mode)
5211 rtx subtarget;
5212 enum insn_code icode;
5213 enum machine_mode compare_mode;
5214 enum mode_class mclass;
5215 enum rtx_code scode;
5216 rtx tem;
5218 if (unsignedp)
5219 code = unsigned_condition (code);
5220 scode = swap_condition (code);
5222 /* If one operand is constant, make it the second one. Only do this
5223 if the other operand is not constant as well. */
5225 if (swap_commutative_operands_p (op0, op1))
5227 tem = op0;
5228 op0 = op1;
5229 op1 = tem;
5230 code = swap_condition (code);
5233 if (mode == VOIDmode)
5234 mode = GET_MODE (op0);
5236 /* For some comparisons with 1 and -1, we can convert this to
5237 comparisons with zero. This will often produce more opportunities for
5238 store-flag insns. */
5240 switch (code)
5242 case LT:
5243 if (op1 == const1_rtx)
5244 op1 = const0_rtx, code = LE;
5245 break;
5246 case LE:
5247 if (op1 == constm1_rtx)
5248 op1 = const0_rtx, code = LT;
5249 break;
5250 case GE:
5251 if (op1 == const1_rtx)
5252 op1 = const0_rtx, code = GT;
5253 break;
5254 case GT:
5255 if (op1 == constm1_rtx)
5256 op1 = const0_rtx, code = GE;
5257 break;
5258 case GEU:
5259 if (op1 == const1_rtx)
5260 op1 = const0_rtx, code = NE;
5261 break;
5262 case LTU:
5263 if (op1 == const1_rtx)
5264 op1 = const0_rtx, code = EQ;
5265 break;
5266 default:
5267 break;
5270 /* If we are comparing a double-word integer with zero or -1, we can
5271 convert the comparison into one involving a single word. */
5272 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
5273 && GET_MODE_CLASS (mode) == MODE_INT
5274 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
5276 if ((code == EQ || code == NE)
5277 && (op1 == const0_rtx || op1 == constm1_rtx))
5279 rtx op00, op01;
5281 /* Do a logical OR or AND of the two words and compare the
5282 result. */
5283 op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
5284 op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
5285 tem = expand_binop (word_mode,
5286 op1 == const0_rtx ? ior_optab : and_optab,
5287 op00, op01, NULL_RTX, unsignedp,
5288 OPTAB_DIRECT);
5290 if (tem != 0)
5291 tem = emit_store_flag (NULL_RTX, code, tem, op1, word_mode,
5292 unsignedp, normalizep);
5294 else if ((code == LT || code == GE) && op1 == const0_rtx)
5296 rtx op0h;
5298 /* If testing the sign bit, can just test on high word. */
5299 op0h = simplify_gen_subreg (word_mode, op0, mode,
5300 subreg_highpart_offset (word_mode,
5301 mode));
5302 tem = emit_store_flag (NULL_RTX, code, op0h, op1, word_mode,
5303 unsignedp, normalizep);
5305 else
5306 tem = NULL_RTX;
5308 if (tem)
5310 if (target_mode == VOIDmode || GET_MODE (tem) == target_mode)
5311 return tem;
5312 if (!target)
5313 target = gen_reg_rtx (target_mode);
5315 convert_move (target, tem,
5316 !val_signbit_known_set_p (word_mode,
5317 (normalizep ? normalizep
5318 : STORE_FLAG_VALUE)));
5319 return target;
5323 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5324 complement of A (for GE) and shifting the sign bit to the low bit. */
5325 if (op1 == const0_rtx && (code == LT || code == GE)
5326 && GET_MODE_CLASS (mode) == MODE_INT
5327 && (normalizep || STORE_FLAG_VALUE == 1
5328 || val_signbit_p (mode, STORE_FLAG_VALUE)))
5330 subtarget = target;
5332 if (!target)
5333 target_mode = mode;
5335 /* If the result is to be wider than OP0, it is best to convert it
5336 first. If it is to be narrower, it is *incorrect* to convert it
5337 first. */
5338 else if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
5340 op0 = convert_modes (target_mode, mode, op0, 0);
5341 mode = target_mode;
5344 if (target_mode != mode)
5345 subtarget = 0;
5347 if (code == GE)
5348 op0 = expand_unop (mode, one_cmpl_optab, op0,
5349 ((STORE_FLAG_VALUE == 1 || normalizep)
5350 ? 0 : subtarget), 0);
5352 if (STORE_FLAG_VALUE == 1 || normalizep)
5353 /* If we are supposed to produce a 0/1 value, we want to do
5354 a logical shift from the sign bit to the low-order bit; for
5355 a -1/0 value, we do an arithmetic shift. */
5356 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
5357 GET_MODE_BITSIZE (mode) - 1,
5358 subtarget, normalizep != -1);
5360 if (mode != target_mode)
5361 op0 = convert_modes (target_mode, mode, op0, 0);
5363 return op0;
5366 mclass = GET_MODE_CLASS (mode);
5367 for (compare_mode = mode; compare_mode != VOIDmode;
5368 compare_mode = GET_MODE_WIDER_MODE (compare_mode))
5370 enum machine_mode optab_mode = mclass == MODE_CC ? CCmode : compare_mode;
5371 icode = optab_handler (cstore_optab, optab_mode);
5372 if (icode != CODE_FOR_nothing)
5374 do_pending_stack_adjust ();
5375 tem = emit_cstore (target, icode, code, mode, compare_mode,
5376 unsignedp, op0, op1, normalizep, target_mode);
5377 if (tem)
5378 return tem;
5380 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5382 tem = emit_cstore (target, icode, scode, mode, compare_mode,
5383 unsignedp, op1, op0, normalizep, target_mode);
5384 if (tem)
5385 return tem;
5387 break;
5391 return 0;
5394 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5395 and storing in TARGET. Normally return TARGET.
5396 Return 0 if that cannot be done.
5398 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5399 it is VOIDmode, they cannot both be CONST_INT.
5401 UNSIGNEDP is for the case where we have to widen the operands
5402 to perform the operation. It says to use zero-extension.
5404 NORMALIZEP is 1 if we should convert the result to be either zero
5405 or one. Normalize is -1 if we should convert the result to be
5406 either zero or -1. If NORMALIZEP is zero, the result will be left
5407 "raw" out of the scc insn. */
5410 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
5411 enum machine_mode mode, int unsignedp, int normalizep)
5413 enum machine_mode target_mode = target ? GET_MODE (target) : VOIDmode;
5414 enum rtx_code rcode;
5415 rtx subtarget;
5416 rtx tem, last, trueval;
5418 tem = emit_store_flag_1 (target, code, op0, op1, mode, unsignedp, normalizep,
5419 target_mode);
5420 if (tem)
5421 return tem;
5423 /* If we reached here, we can't do this with a scc insn, however there
5424 are some comparisons that can be done in other ways. Don't do any
5425 of these cases if branches are very cheap. */
5426 if (BRANCH_COST (optimize_insn_for_speed_p (), false) == 0)
5427 return 0;
5429 /* See what we need to return. We can only return a 1, -1, or the
5430 sign bit. */
5432 if (normalizep == 0)
5434 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5435 normalizep = STORE_FLAG_VALUE;
5437 else if (val_signbit_p (mode, STORE_FLAG_VALUE))
5439 else
5440 return 0;
5443 last = get_last_insn ();
5445 /* If optimizing, use different pseudo registers for each insn, instead
5446 of reusing the same pseudo. This leads to better CSE, but slows
5447 down the compiler, since there are more pseudos */
5448 subtarget = (!optimize
5449 && (target_mode == mode)) ? target : NULL_RTX;
5450 trueval = GEN_INT (normalizep ? normalizep : STORE_FLAG_VALUE);
5452 /* For floating-point comparisons, try the reverse comparison or try
5453 changing the "orderedness" of the comparison. */
5454 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5456 enum rtx_code first_code;
5457 bool and_them;
5459 rcode = reverse_condition_maybe_unordered (code);
5460 if (can_compare_p (rcode, mode, ccp_store_flag)
5461 && (code == ORDERED || code == UNORDERED
5462 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
5463 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
5465 int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
5466 || (STORE_FLAG_VALUE == -1 && normalizep == 1));
5468 /* For the reverse comparison, use either an addition or a XOR. */
5469 if (want_add
5470 && rtx_cost (GEN_INT (normalizep), PLUS, 1,
5471 optimize_insn_for_speed_p ()) == 0)
5473 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5474 STORE_FLAG_VALUE, target_mode);
5475 if (tem)
5476 return expand_binop (target_mode, add_optab, tem,
5477 GEN_INT (normalizep),
5478 target, 0, OPTAB_WIDEN);
5480 else if (!want_add
5481 && rtx_cost (trueval, XOR, 1,
5482 optimize_insn_for_speed_p ()) == 0)
5484 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5485 normalizep, target_mode);
5486 if (tem)
5487 return expand_binop (target_mode, xor_optab, tem, trueval,
5488 target, INTVAL (trueval) >= 0, OPTAB_WIDEN);
5492 delete_insns_since (last);
5494 /* Cannot split ORDERED and UNORDERED, only try the above trick. */
5495 if (code == ORDERED || code == UNORDERED)
5496 return 0;
5498 and_them = split_comparison (code, mode, &first_code, &code);
5500 /* If there are no NaNs, the first comparison should always fall through.
5501 Effectively change the comparison to the other one. */
5502 if (!HONOR_NANS (mode))
5504 gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
5505 return emit_store_flag_1 (target, code, op0, op1, mode, 0, normalizep,
5506 target_mode);
5509 #ifdef HAVE_conditional_move
5510 /* Try using a setcc instruction for ORDERED/UNORDERED, followed by a
5511 conditional move. */
5512 tem = emit_store_flag_1 (subtarget, first_code, op0, op1, mode, 0,
5513 normalizep, target_mode);
5514 if (tem == 0)
5515 return 0;
5517 if (and_them)
5518 tem = emit_conditional_move (target, code, op0, op1, mode,
5519 tem, const0_rtx, GET_MODE (tem), 0);
5520 else
5521 tem = emit_conditional_move (target, code, op0, op1, mode,
5522 trueval, tem, GET_MODE (tem), 0);
5524 if (tem == 0)
5525 delete_insns_since (last);
5526 return tem;
5527 #else
5528 return 0;
5529 #endif
5532 /* The remaining tricks only apply to integer comparisons. */
5534 if (GET_MODE_CLASS (mode) != MODE_INT)
5535 return 0;
5537 /* If this is an equality comparison of integers, we can try to exclusive-or
5538 (or subtract) the two operands and use a recursive call to try the
5539 comparison with zero. Don't do any of these cases if branches are
5540 very cheap. */
5542 if ((code == EQ || code == NE) && op1 != const0_rtx)
5544 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5545 OPTAB_WIDEN);
5547 if (tem == 0)
5548 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5549 OPTAB_WIDEN);
5550 if (tem != 0)
5551 tem = emit_store_flag (target, code, tem, const0_rtx,
5552 mode, unsignedp, normalizep);
5553 if (tem != 0)
5554 return tem;
5556 delete_insns_since (last);
5559 /* For integer comparisons, try the reverse comparison. However, for
5560 small X and if we'd have anyway to extend, implementing "X != 0"
5561 as "-(int)X >> 31" is still cheaper than inverting "(int)X == 0". */
5562 rcode = reverse_condition (code);
5563 if (can_compare_p (rcode, mode, ccp_store_flag)
5564 && ! (optab_handler (cstore_optab, mode) == CODE_FOR_nothing
5565 && code == NE
5566 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
5567 && op1 == const0_rtx))
5569 int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
5570 || (STORE_FLAG_VALUE == -1 && normalizep == 1));
5572 /* Again, for the reverse comparison, use either an addition or a XOR. */
5573 if (want_add
5574 && rtx_cost (GEN_INT (normalizep), PLUS, 1,
5575 optimize_insn_for_speed_p ()) == 0)
5577 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5578 STORE_FLAG_VALUE, target_mode);
5579 if (tem != 0)
5580 tem = expand_binop (target_mode, add_optab, tem,
5581 GEN_INT (normalizep), target, 0, OPTAB_WIDEN);
5583 else if (!want_add
5584 && rtx_cost (trueval, XOR, 1,
5585 optimize_insn_for_speed_p ()) == 0)
5587 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5588 normalizep, target_mode);
5589 if (tem != 0)
5590 tem = expand_binop (target_mode, xor_optab, tem, trueval, target,
5591 INTVAL (trueval) >= 0, OPTAB_WIDEN);
5594 if (tem != 0)
5595 return tem;
5596 delete_insns_since (last);
5599 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5600 the constant zero. Reject all other comparisons at this point. Only
5601 do LE and GT if branches are expensive since they are expensive on
5602 2-operand machines. */
5604 if (op1 != const0_rtx
5605 || (code != EQ && code != NE
5606 && (BRANCH_COST (optimize_insn_for_speed_p (),
5607 false) <= 1 || (code != LE && code != GT))))
5608 return 0;
5610 /* Try to put the result of the comparison in the sign bit. Assume we can't
5611 do the necessary operation below. */
5613 tem = 0;
5615 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5616 the sign bit set. */
5618 if (code == LE)
5620 /* This is destructive, so SUBTARGET can't be OP0. */
5621 if (rtx_equal_p (subtarget, op0))
5622 subtarget = 0;
5624 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5625 OPTAB_WIDEN);
5626 if (tem)
5627 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5628 OPTAB_WIDEN);
5631 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5632 number of bits in the mode of OP0, minus one. */
5634 if (code == GT)
5636 if (rtx_equal_p (subtarget, op0))
5637 subtarget = 0;
5639 tem = expand_shift (RSHIFT_EXPR, mode, op0,
5640 GET_MODE_BITSIZE (mode) - 1,
5641 subtarget, 0);
5642 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5643 OPTAB_WIDEN);
5646 if (code == EQ || code == NE)
5648 /* For EQ or NE, one way to do the comparison is to apply an operation
5649 that converts the operand into a positive number if it is nonzero
5650 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5651 for NE we negate. This puts the result in the sign bit. Then we
5652 normalize with a shift, if needed.
5654 Two operations that can do the above actions are ABS and FFS, so try
5655 them. If that doesn't work, and MODE is smaller than a full word,
5656 we can use zero-extension to the wider mode (an unsigned conversion)
5657 as the operation. */
5659 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5660 that is compensated by the subsequent overflow when subtracting
5661 one / negating. */
5663 if (optab_handler (abs_optab, mode) != CODE_FOR_nothing)
5664 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5665 else if (optab_handler (ffs_optab, mode) != CODE_FOR_nothing)
5666 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5667 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5669 tem = convert_modes (word_mode, mode, op0, 1);
5670 mode = word_mode;
5673 if (tem != 0)
5675 if (code == EQ)
5676 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5677 0, OPTAB_WIDEN);
5678 else
5679 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5682 /* If we couldn't do it that way, for NE we can "or" the two's complement
5683 of the value with itself. For EQ, we take the one's complement of
5684 that "or", which is an extra insn, so we only handle EQ if branches
5685 are expensive. */
5687 if (tem == 0
5688 && (code == NE
5689 || BRANCH_COST (optimize_insn_for_speed_p (),
5690 false) > 1))
5692 if (rtx_equal_p (subtarget, op0))
5693 subtarget = 0;
5695 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5696 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5697 OPTAB_WIDEN);
5699 if (tem && code == EQ)
5700 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5704 if (tem && normalizep)
5705 tem = expand_shift (RSHIFT_EXPR, mode, tem,
5706 GET_MODE_BITSIZE (mode) - 1,
5707 subtarget, normalizep == 1);
5709 if (tem)
5711 if (!target)
5713 else if (GET_MODE (tem) != target_mode)
5715 convert_move (target, tem, 0);
5716 tem = target;
5718 else if (!subtarget)
5720 emit_move_insn (target, tem);
5721 tem = target;
5724 else
5725 delete_insns_since (last);
5727 return tem;
5730 /* Like emit_store_flag, but always succeeds. */
5733 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5734 enum machine_mode mode, int unsignedp, int normalizep)
5736 rtx tem, label;
5737 rtx trueval, falseval;
5739 /* First see if emit_store_flag can do the job. */
5740 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5741 if (tem != 0)
5742 return tem;
5744 if (!target)
5745 target = gen_reg_rtx (word_mode);
5747 /* If this failed, we have to do this with set/compare/jump/set code.
5748 For foo != 0, if foo is in OP0, just replace it with 1 if nonzero. */
5749 trueval = normalizep ? GEN_INT (normalizep) : const1_rtx;
5750 if (code == NE
5751 && GET_MODE_CLASS (mode) == MODE_INT
5752 && REG_P (target)
5753 && op0 == target
5754 && op1 == const0_rtx)
5756 label = gen_label_rtx ();
5757 do_compare_rtx_and_jump (target, const0_rtx, EQ, unsignedp,
5758 mode, NULL_RTX, NULL_RTX, label, -1);
5759 emit_move_insn (target, trueval);
5760 emit_label (label);
5761 return target;
5764 if (!REG_P (target)
5765 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5766 target = gen_reg_rtx (GET_MODE (target));
5768 /* Jump in the right direction if the target cannot implement CODE
5769 but can jump on its reverse condition. */
5770 falseval = const0_rtx;
5771 if (! can_compare_p (code, mode, ccp_jump)
5772 && (! FLOAT_MODE_P (mode)
5773 || code == ORDERED || code == UNORDERED
5774 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
5775 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
5777 enum rtx_code rcode;
5778 if (FLOAT_MODE_P (mode))
5779 rcode = reverse_condition_maybe_unordered (code);
5780 else
5781 rcode = reverse_condition (code);
5783 /* Canonicalize to UNORDERED for the libcall. */
5784 if (can_compare_p (rcode, mode, ccp_jump)
5785 || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
5787 falseval = trueval;
5788 trueval = const0_rtx;
5789 code = rcode;
5793 emit_move_insn (target, trueval);
5794 label = gen_label_rtx ();
5795 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
5796 NULL_RTX, label, -1);
5798 emit_move_insn (target, falseval);
5799 emit_label (label);
5801 return target;
5804 /* Perform possibly multi-word comparison and conditional jump to LABEL
5805 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE. This is
5806 now a thin wrapper around do_compare_rtx_and_jump. */
5808 static void
5809 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, enum machine_mode mode,
5810 rtx label)
5812 int unsignedp = (op == LTU || op == LEU || op == GTU || op == GEU);
5813 do_compare_rtx_and_jump (arg1, arg2, op, unsignedp, mode,
5814 NULL_RTX, NULL_RTX, label, -1);