Makefile.am (bits_headers): Remove ${bits_host_headers}.
[official-gcc.git] / gcc / expmed.c
blobc85e68cf617a1dce88c7477f3b67a75088650b6a
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. */
1098 if (bitregion_end
1099 && unit > BITS_PER_UNIT
1100 && bitpos + bitsdone - thispos + unit > bitregion_end + 1)
1102 unit = unit / 2;
1103 continue;
1106 /* THISSIZE must not overrun a word boundary. Otherwise,
1107 store_fixed_bit_field will call us again, and we will mutually
1108 recurse forever. */
1109 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1110 thissize = MIN (thissize, unit - thispos);
1112 if (BYTES_BIG_ENDIAN)
1114 /* Fetch successively less significant portions. */
1115 if (CONST_INT_P (value))
1116 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1117 >> (bitsize - bitsdone - thissize))
1118 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1119 else
1121 int total_bits = GET_MODE_BITSIZE (GET_MODE (value));
1122 /* The args are chosen so that the last part includes the
1123 lsb. Give extract_bit_field the value it needs (with
1124 endianness compensation) to fetch the piece we want. */
1125 part = extract_fixed_bit_field (word_mode, value, thissize,
1126 total_bits - bitsize + bitsdone,
1127 NULL_RTX, 1, false);
1130 else
1132 /* Fetch successively more significant portions. */
1133 if (CONST_INT_P (value))
1134 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1135 >> bitsdone)
1136 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1137 else
1138 part = extract_fixed_bit_field (word_mode, value, thissize,
1139 bitsdone, NULL_RTX, 1, false);
1142 /* If OP0 is a register, then handle OFFSET here.
1144 When handling multiword bitfields, extract_bit_field may pass
1145 down a word_mode SUBREG of a larger REG for a bitfield that actually
1146 crosses a word boundary. Thus, for a SUBREG, we must find
1147 the current word starting from the base register. */
1148 if (GET_CODE (op0) == SUBREG)
1150 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
1151 enum machine_mode sub_mode = GET_MODE (SUBREG_REG (op0));
1152 if (sub_mode != BLKmode && GET_MODE_SIZE (sub_mode) < UNITS_PER_WORD)
1153 word = word_offset ? const0_rtx : op0;
1154 else
1155 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1156 GET_MODE (SUBREG_REG (op0)));
1157 offset = 0;
1159 else if (REG_P (op0))
1161 enum machine_mode op0_mode = GET_MODE (op0);
1162 if (op0_mode != BLKmode && GET_MODE_SIZE (op0_mode) < UNITS_PER_WORD)
1163 word = offset ? const0_rtx : op0;
1164 else
1165 word = operand_subword_force (op0, offset, GET_MODE (op0));
1166 offset = 0;
1168 else
1169 word = op0;
1171 /* OFFSET is in UNITs, and UNIT is in bits. If WORD is const0_rtx,
1172 it is just an out-of-bounds access. Ignore it. */
1173 if (word != const0_rtx)
1174 store_fixed_bit_field (word, thissize, offset * unit + thispos,
1175 bitregion_start, bitregion_end, part);
1176 bitsdone += thissize;
1180 /* A subroutine of extract_bit_field_1 that converts return value X
1181 to either MODE or TMODE. MODE, TMODE and UNSIGNEDP are arguments
1182 to extract_bit_field. */
1184 static rtx
1185 convert_extracted_bit_field (rtx x, enum machine_mode mode,
1186 enum machine_mode tmode, bool unsignedp)
1188 if (GET_MODE (x) == tmode || GET_MODE (x) == mode)
1189 return x;
1191 /* If the x mode is not a scalar integral, first convert to the
1192 integer mode of that size and then access it as a floating-point
1193 value via a SUBREG. */
1194 if (!SCALAR_INT_MODE_P (tmode))
1196 enum machine_mode smode;
1198 smode = mode_for_size (GET_MODE_BITSIZE (tmode), MODE_INT, 0);
1199 x = convert_to_mode (smode, x, unsignedp);
1200 x = force_reg (smode, x);
1201 return gen_lowpart (tmode, x);
1204 return convert_to_mode (tmode, x, unsignedp);
1207 /* Try to use an ext(z)v pattern to extract a field from OP0.
1208 Return the extracted value on success, otherwise return null.
1209 EXT_MODE is the mode of the extraction and the other arguments
1210 are as for extract_bit_field. */
1212 static rtx
1213 extract_bit_field_using_extv (const extraction_insn *extv, rtx op0,
1214 unsigned HOST_WIDE_INT bitsize,
1215 unsigned HOST_WIDE_INT bitnum,
1216 int unsignedp, rtx target,
1217 enum machine_mode mode, enum machine_mode tmode)
1219 struct expand_operand ops[4];
1220 rtx spec_target = target;
1221 rtx spec_target_subreg = 0;
1222 enum machine_mode ext_mode = extv->field_mode;
1223 unsigned unit = GET_MODE_BITSIZE (ext_mode);
1225 if (bitsize == 0 || unit < bitsize)
1226 return NULL_RTX;
1228 if (MEM_P (op0))
1229 /* Get a reference to the first byte of the field. */
1230 op0 = narrow_bit_field_mem (op0, extv->struct_mode, bitsize, bitnum,
1231 &bitnum);
1232 else
1234 /* Convert from counting within OP0 to counting in EXT_MODE. */
1235 if (BYTES_BIG_ENDIAN)
1236 bitnum += unit - GET_MODE_BITSIZE (GET_MODE (op0));
1238 /* If op0 is a register, we need it in EXT_MODE to make it
1239 acceptable to the format of ext(z)v. */
1240 if (GET_CODE (op0) == SUBREG && GET_MODE (op0) != ext_mode)
1241 return NULL_RTX;
1242 if (REG_P (op0) && GET_MODE (op0) != ext_mode)
1243 op0 = gen_lowpart_SUBREG (ext_mode, op0);
1246 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
1247 "backwards" from the size of the unit we are extracting from.
1248 Otherwise, we count bits from the most significant on a
1249 BYTES/BITS_BIG_ENDIAN machine. */
1251 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1252 bitnum = unit - bitsize - bitnum;
1254 if (target == 0)
1255 target = spec_target = gen_reg_rtx (tmode);
1257 if (GET_MODE (target) != ext_mode)
1259 /* Don't use LHS paradoxical subreg if explicit truncation is needed
1260 between the mode of the extraction (word_mode) and the target
1261 mode. Instead, create a temporary and use convert_move to set
1262 the target. */
1263 if (REG_P (target)
1264 && TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (target), ext_mode))
1266 target = gen_lowpart (ext_mode, target);
1267 if (GET_MODE_PRECISION (ext_mode)
1268 > GET_MODE_PRECISION (GET_MODE (spec_target)))
1269 spec_target_subreg = target;
1271 else
1272 target = gen_reg_rtx (ext_mode);
1275 create_output_operand (&ops[0], target, ext_mode);
1276 create_fixed_operand (&ops[1], op0);
1277 create_integer_operand (&ops[2], bitsize);
1278 create_integer_operand (&ops[3], bitnum);
1279 if (maybe_expand_insn (extv->icode, 4, ops))
1281 target = ops[0].value;
1282 if (target == spec_target)
1283 return target;
1284 if (target == spec_target_subreg)
1285 return spec_target;
1286 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1288 return NULL_RTX;
1291 /* A subroutine of extract_bit_field, with the same arguments.
1292 If FALLBACK_P is true, fall back to extract_fixed_bit_field
1293 if we can find no other means of implementing the operation.
1294 if FALLBACK_P is false, return NULL instead. */
1296 static rtx
1297 extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1298 unsigned HOST_WIDE_INT bitnum,
1299 int unsignedp, bool packedp, rtx target,
1300 enum machine_mode mode, enum machine_mode tmode,
1301 bool fallback_p)
1303 rtx op0 = str_rtx;
1304 enum machine_mode int_mode;
1305 enum machine_mode mode1;
1307 if (tmode == VOIDmode)
1308 tmode = mode;
1310 while (GET_CODE (op0) == SUBREG)
1312 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1313 op0 = SUBREG_REG (op0);
1316 /* If we have an out-of-bounds access to a register, just return an
1317 uninitialized register of the required mode. This can occur if the
1318 source code contains an out-of-bounds access to a small array. */
1319 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
1320 return gen_reg_rtx (tmode);
1322 if (REG_P (op0)
1323 && mode == GET_MODE (op0)
1324 && bitnum == 0
1325 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
1327 /* We're trying to extract a full register from itself. */
1328 return op0;
1331 /* See if we can get a better vector mode before extracting. */
1332 if (VECTOR_MODE_P (GET_MODE (op0))
1333 && !MEM_P (op0)
1334 && GET_MODE_INNER (GET_MODE (op0)) != tmode)
1336 enum machine_mode new_mode;
1338 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1339 new_mode = MIN_MODE_VECTOR_FLOAT;
1340 else if (GET_MODE_CLASS (tmode) == MODE_FRACT)
1341 new_mode = MIN_MODE_VECTOR_FRACT;
1342 else if (GET_MODE_CLASS (tmode) == MODE_UFRACT)
1343 new_mode = MIN_MODE_VECTOR_UFRACT;
1344 else if (GET_MODE_CLASS (tmode) == MODE_ACCUM)
1345 new_mode = MIN_MODE_VECTOR_ACCUM;
1346 else if (GET_MODE_CLASS (tmode) == MODE_UACCUM)
1347 new_mode = MIN_MODE_VECTOR_UACCUM;
1348 else
1349 new_mode = MIN_MODE_VECTOR_INT;
1351 for (; new_mode != VOIDmode ; new_mode = GET_MODE_WIDER_MODE (new_mode))
1352 if (GET_MODE_SIZE (new_mode) == GET_MODE_SIZE (GET_MODE (op0))
1353 && targetm.vector_mode_supported_p (new_mode))
1354 break;
1355 if (new_mode != VOIDmode)
1356 op0 = gen_lowpart (new_mode, op0);
1359 /* Use vec_extract patterns for extracting parts of vectors whenever
1360 available. */
1361 if (VECTOR_MODE_P (GET_MODE (op0))
1362 && !MEM_P (op0)
1363 && optab_handler (vec_extract_optab, GET_MODE (op0)) != CODE_FOR_nothing
1364 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
1365 == bitnum / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
1367 struct expand_operand ops[3];
1368 enum machine_mode outermode = GET_MODE (op0);
1369 enum machine_mode innermode = GET_MODE_INNER (outermode);
1370 enum insn_code icode = optab_handler (vec_extract_optab, outermode);
1371 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1373 create_output_operand (&ops[0], target, innermode);
1374 create_input_operand (&ops[1], op0, outermode);
1375 create_integer_operand (&ops[2], pos);
1376 if (maybe_expand_insn (icode, 3, ops))
1378 target = ops[0].value;
1379 if (GET_MODE (target) != mode)
1380 return gen_lowpart (tmode, target);
1381 return target;
1385 /* Make sure we are playing with integral modes. Pun with subregs
1386 if we aren't. */
1388 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
1389 if (imode != GET_MODE (op0))
1391 if (MEM_P (op0))
1392 op0 = adjust_bitfield_address_size (op0, imode, 0, MEM_SIZE (op0));
1393 else if (imode != BLKmode)
1395 op0 = gen_lowpart (imode, op0);
1397 /* If we got a SUBREG, force it into a register since we
1398 aren't going to be able to do another SUBREG on it. */
1399 if (GET_CODE (op0) == SUBREG)
1400 op0 = force_reg (imode, op0);
1402 else if (REG_P (op0))
1404 rtx reg, subreg;
1405 imode = smallest_mode_for_size (GET_MODE_BITSIZE (GET_MODE (op0)),
1406 MODE_INT);
1407 reg = gen_reg_rtx (imode);
1408 subreg = gen_lowpart_SUBREG (GET_MODE (op0), reg);
1409 emit_move_insn (subreg, op0);
1410 op0 = reg;
1411 bitnum += SUBREG_BYTE (subreg) * BITS_PER_UNIT;
1413 else
1415 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (op0));
1416 rtx mem = assign_stack_temp (GET_MODE (op0), size);
1417 emit_move_insn (mem, op0);
1418 op0 = adjust_bitfield_address_size (mem, BLKmode, 0, size);
1423 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1424 If that's wrong, the solution is to test for it and set TARGET to 0
1425 if needed. */
1427 /* If the bitfield is volatile, we need to make sure the access
1428 remains on a type-aligned boundary. */
1429 if (GET_CODE (op0) == MEM
1430 && MEM_VOLATILE_P (op0)
1431 && GET_MODE_BITSIZE (GET_MODE (op0)) > 0
1432 && flag_strict_volatile_bitfields > 0)
1433 goto no_subreg_mode_swap;
1435 /* Only scalar integer modes can be converted via subregs. There is an
1436 additional problem for FP modes here in that they can have a precision
1437 which is different from the size. mode_for_size uses precision, but
1438 we want a mode based on the size, so we must avoid calling it for FP
1439 modes. */
1440 mode1 = mode;
1441 if (SCALAR_INT_MODE_P (tmode))
1443 enum machine_mode try_mode = mode_for_size (bitsize,
1444 GET_MODE_CLASS (tmode), 0);
1445 if (try_mode != BLKmode)
1446 mode1 = try_mode;
1448 gcc_assert (mode1 != BLKmode);
1450 /* Extraction of a full MODE1 value can be done with a subreg as long
1451 as the least significant bit of the value is the least significant
1452 bit of either OP0 or a word of OP0. */
1453 if (!MEM_P (op0)
1454 && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
1455 && bitsize == GET_MODE_BITSIZE (mode1)
1456 && TRULY_NOOP_TRUNCATION_MODES_P (mode1, GET_MODE (op0)))
1458 rtx sub = simplify_gen_subreg (mode1, op0, GET_MODE (op0),
1459 bitnum / BITS_PER_UNIT);
1460 if (sub)
1461 return convert_extracted_bit_field (sub, mode, tmode, unsignedp);
1464 /* Extraction of a full MODE1 value can be done with a load as long as
1465 the field is on a byte boundary and is sufficiently aligned. */
1466 if (simple_mem_bitfield_p (op0, bitsize, bitnum, mode1))
1468 op0 = adjust_bitfield_address (op0, mode1, bitnum / BITS_PER_UNIT);
1469 return convert_extracted_bit_field (op0, mode, tmode, unsignedp);
1472 no_subreg_mode_swap:
1474 /* Handle fields bigger than a word. */
1476 if (bitsize > BITS_PER_WORD)
1478 /* Here we transfer the words of the field
1479 in the order least significant first.
1480 This is because the most significant word is the one which may
1481 be less than full. */
1483 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1484 unsigned int i;
1485 rtx last;
1487 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1488 target = gen_reg_rtx (mode);
1490 /* Indicate for flow that the entire target reg is being set. */
1491 emit_clobber (target);
1493 last = get_last_insn ();
1494 for (i = 0; i < nwords; i++)
1496 /* If I is 0, use the low-order word in both field and target;
1497 if I is 1, use the next to lowest word; and so on. */
1498 /* Word number in TARGET to use. */
1499 unsigned int wordnum
1500 = (WORDS_BIG_ENDIAN
1501 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1502 : i);
1503 /* Offset from start of field in OP0. */
1504 unsigned int bit_offset = (WORDS_BIG_ENDIAN
1505 ? MAX (0, ((int) bitsize - ((int) i + 1)
1506 * (int) BITS_PER_WORD))
1507 : (int) i * BITS_PER_WORD);
1508 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1509 rtx result_part
1510 = extract_bit_field_1 (op0, MIN (BITS_PER_WORD,
1511 bitsize - i * BITS_PER_WORD),
1512 bitnum + bit_offset, 1, false, target_part,
1513 mode, word_mode, fallback_p);
1515 gcc_assert (target_part);
1516 if (!result_part)
1518 delete_insns_since (last);
1519 return NULL;
1522 if (result_part != target_part)
1523 emit_move_insn (target_part, result_part);
1526 if (unsignedp)
1528 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1529 need to be zero'd out. */
1530 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1532 unsigned int i, total_words;
1534 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1535 for (i = nwords; i < total_words; i++)
1536 emit_move_insn
1537 (operand_subword (target,
1538 WORDS_BIG_ENDIAN ? total_words - i - 1 : i,
1539 1, VOIDmode),
1540 const0_rtx);
1542 return target;
1545 /* Signed bit field: sign-extend with two arithmetic shifts. */
1546 target = expand_shift (LSHIFT_EXPR, mode, target,
1547 GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
1548 return expand_shift (RSHIFT_EXPR, mode, target,
1549 GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
1552 /* If OP0 is a multi-word register, narrow it to the affected word.
1553 If the region spans two words, defer to extract_split_bit_field. */
1554 if (!MEM_P (op0) && GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1556 op0 = simplify_gen_subreg (word_mode, op0, GET_MODE (op0),
1557 bitnum / BITS_PER_WORD * UNITS_PER_WORD);
1558 bitnum %= BITS_PER_WORD;
1559 if (bitnum + bitsize > BITS_PER_WORD)
1561 if (!fallback_p)
1562 return NULL_RTX;
1563 target = extract_split_bit_field (op0, bitsize, bitnum, unsignedp);
1564 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1568 /* From here on we know the desired field is smaller than a word.
1569 If OP0 is a register, it too fits within a word. */
1570 enum extraction_pattern pattern = unsignedp ? EP_extzv : EP_extv;
1571 extraction_insn extv;
1572 if (!MEM_P (op0)
1573 /* ??? We could limit the structure size to the part of OP0 that
1574 contains the field, with appropriate checks for endianness
1575 and TRULY_NOOP_TRUNCATION. */
1576 && get_best_reg_extraction_insn (&extv, pattern,
1577 GET_MODE_BITSIZE (GET_MODE (op0)),
1578 tmode))
1580 rtx result = extract_bit_field_using_extv (&extv, op0, bitsize, bitnum,
1581 unsignedp, target, mode,
1582 tmode);
1583 if (result)
1584 return result;
1587 /* If OP0 is a memory, try copying it to a register and seeing if a
1588 cheap register alternative is available. */
1589 if (MEM_P (op0))
1591 /* Do not use extv/extzv for volatile bitfields when
1592 -fstrict-volatile-bitfields is in effect. */
1593 if (!(MEM_VOLATILE_P (op0) && flag_strict_volatile_bitfields > 0)
1594 && get_best_mem_extraction_insn (&extv, pattern, bitsize, bitnum,
1595 tmode))
1597 rtx result = extract_bit_field_using_extv (&extv, op0, bitsize,
1598 bitnum, unsignedp,
1599 target, mode,
1600 tmode);
1601 if (result)
1602 return result;
1605 rtx last = get_last_insn ();
1607 /* Try loading part of OP0 into a register and extracting the
1608 bitfield from that. */
1609 unsigned HOST_WIDE_INT bitpos;
1610 rtx xop0 = adjust_bit_field_mem_for_reg (pattern, op0, bitsize, bitnum,
1611 0, 0, tmode, &bitpos);
1612 if (xop0)
1614 xop0 = copy_to_reg (xop0);
1615 rtx result = extract_bit_field_1 (xop0, bitsize, bitpos,
1616 unsignedp, packedp, target,
1617 mode, tmode, false);
1618 if (result)
1619 return result;
1620 delete_insns_since (last);
1624 if (!fallback_p)
1625 return NULL;
1627 /* Find a correspondingly-sized integer field, so we can apply
1628 shifts and masks to it. */
1629 int_mode = int_mode_for_mode (tmode);
1630 if (int_mode == BLKmode)
1631 int_mode = int_mode_for_mode (mode);
1632 /* Should probably push op0 out to memory and then do a load. */
1633 gcc_assert (int_mode != BLKmode);
1635 target = extract_fixed_bit_field (int_mode, op0, bitsize, bitnum,
1636 target, unsignedp, packedp);
1637 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1640 /* Generate code to extract a byte-field from STR_RTX
1641 containing BITSIZE bits, starting at BITNUM,
1642 and put it in TARGET if possible (if TARGET is nonzero).
1643 Regardless of TARGET, we return the rtx for where the value is placed.
1645 STR_RTX is the structure containing the byte (a REG or MEM).
1646 UNSIGNEDP is nonzero if this is an unsigned bit field.
1647 PACKEDP is nonzero if the field has the packed attribute.
1648 MODE is the natural mode of the field value once extracted.
1649 TMODE is the mode the caller would like the value to have;
1650 but the value may be returned with type MODE instead.
1652 If a TARGET is specified and we can store in it at no extra cost,
1653 we do so, and return TARGET.
1654 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1655 if they are equally easy. */
1658 extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1659 unsigned HOST_WIDE_INT bitnum, int unsignedp, bool packedp,
1660 rtx target, enum machine_mode mode, enum machine_mode tmode)
1662 return extract_bit_field_1 (str_rtx, bitsize, bitnum, unsignedp, packedp,
1663 target, mode, tmode, true);
1666 /* Use shifts and boolean operations to extract a field of BITSIZE bits
1667 from bit BITNUM of OP0.
1669 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1670 PACKEDP is true if the field has the packed attribute.
1672 If TARGET is nonzero, attempts to store the value there
1673 and return TARGET, but this is not guaranteed.
1674 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
1676 static rtx
1677 extract_fixed_bit_field (enum machine_mode tmode, rtx op0,
1678 unsigned HOST_WIDE_INT bitsize,
1679 unsigned HOST_WIDE_INT bitnum, rtx target,
1680 int unsignedp, bool packedp)
1682 enum machine_mode mode;
1684 if (MEM_P (op0))
1686 /* Get the proper mode to use for this field. We want a mode that
1687 includes the entire field. If such a mode would be larger than
1688 a word, we won't be doing the extraction the normal way. */
1690 if (MEM_VOLATILE_P (op0)
1691 && flag_strict_volatile_bitfields > 0)
1693 if (GET_MODE_BITSIZE (GET_MODE (op0)) > 0)
1694 mode = GET_MODE (op0);
1695 else if (target && GET_MODE_BITSIZE (GET_MODE (target)) > 0)
1696 mode = GET_MODE (target);
1697 else
1698 mode = tmode;
1700 else
1701 mode = get_best_mode (bitsize, bitnum, 0, 0,
1702 MEM_ALIGN (op0), word_mode, MEM_VOLATILE_P (op0));
1704 if (mode == VOIDmode)
1705 /* The only way this should occur is if the field spans word
1706 boundaries. */
1707 return extract_split_bit_field (op0, bitsize, bitnum, unsignedp);
1709 unsigned int total_bits = GET_MODE_BITSIZE (mode);
1710 HOST_WIDE_INT bit_offset = bitnum - bitnum % total_bits;
1712 /* If we're accessing a volatile MEM, we can't apply BIT_OFFSET
1713 if it results in a multi-word access where we otherwise wouldn't
1714 have one. So, check for that case here. */
1715 if (MEM_P (op0)
1716 && MEM_VOLATILE_P (op0)
1717 && flag_strict_volatile_bitfields > 0
1718 && bitnum % BITS_PER_UNIT + bitsize <= total_bits
1719 && bitnum % GET_MODE_BITSIZE (mode) + bitsize > total_bits)
1721 if (STRICT_ALIGNMENT)
1723 static bool informed_about_misalignment = false;
1725 if (packedp)
1727 if (bitsize == total_bits)
1728 warning_at (input_location, OPT_fstrict_volatile_bitfields,
1729 "multiple accesses to volatile structure"
1730 " member because of packed attribute");
1731 else
1732 warning_at (input_location, OPT_fstrict_volatile_bitfields,
1733 "multiple accesses to volatile structure"
1734 " bitfield because of packed attribute");
1736 return extract_split_bit_field (op0, bitsize, bitnum,
1737 unsignedp);
1740 if (bitsize == total_bits)
1741 warning_at (input_location, OPT_fstrict_volatile_bitfields,
1742 "mis-aligned access used for structure member");
1743 else
1744 warning_at (input_location, OPT_fstrict_volatile_bitfields,
1745 "mis-aligned access used for structure bitfield");
1747 if (! informed_about_misalignment)
1749 informed_about_misalignment = true;
1750 inform (input_location,
1751 "when a volatile object spans multiple type-sized"
1752 " locations, the compiler must choose between using"
1753 " a single mis-aligned access to preserve the"
1754 " volatility, or using multiple aligned accesses"
1755 " to avoid runtime faults; this code may fail at"
1756 " runtime if the hardware does not allow this"
1757 " access");
1760 bit_offset = bitnum - bitnum % BITS_PER_UNIT;
1762 op0 = adjust_bitfield_address (op0, mode, bit_offset / BITS_PER_UNIT);
1763 bitnum -= bit_offset;
1766 mode = GET_MODE (op0);
1767 gcc_assert (SCALAR_INT_MODE_P (mode));
1769 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
1770 for invalid input, such as extract equivalent of f5 from
1771 gcc.dg/pr48335-2.c. */
1773 if (BYTES_BIG_ENDIAN)
1774 /* BITNUM is the distance between our msb and that of OP0.
1775 Convert it to the distance from the lsb. */
1776 bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
1778 /* Now BITNUM is always the distance between the field's lsb and that of OP0.
1779 We have reduced the big-endian case to the little-endian case. */
1781 if (unsignedp)
1783 if (bitnum)
1785 /* If the field does not already start at the lsb,
1786 shift it so it does. */
1787 /* Maybe propagate the target for the shift. */
1788 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1789 if (tmode != mode)
1790 subtarget = 0;
1791 op0 = expand_shift (RSHIFT_EXPR, mode, op0, bitnum, subtarget, 1);
1793 /* Convert the value to the desired mode. */
1794 if (mode != tmode)
1795 op0 = convert_to_mode (tmode, op0, 1);
1797 /* Unless the msb of the field used to be the msb when we shifted,
1798 mask out the upper bits. */
1800 if (GET_MODE_BITSIZE (mode) != bitnum + bitsize)
1801 return expand_binop (GET_MODE (op0), and_optab, op0,
1802 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1803 target, 1, OPTAB_LIB_WIDEN);
1804 return op0;
1807 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1808 then arithmetic-shift its lsb to the lsb of the word. */
1809 op0 = force_reg (mode, op0);
1811 /* Find the narrowest integer mode that contains the field. */
1813 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1814 mode = GET_MODE_WIDER_MODE (mode))
1815 if (GET_MODE_BITSIZE (mode) >= bitsize + bitnum)
1817 op0 = convert_to_mode (mode, op0, 0);
1818 break;
1821 if (mode != tmode)
1822 target = 0;
1824 if (GET_MODE_BITSIZE (mode) != (bitsize + bitnum))
1826 int amount = GET_MODE_BITSIZE (mode) - (bitsize + bitnum);
1827 /* Maybe propagate the target for the shift. */
1828 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1829 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1832 return expand_shift (RSHIFT_EXPR, mode, op0,
1833 GET_MODE_BITSIZE (mode) - bitsize, target, 0);
1836 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1837 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1838 complement of that if COMPLEMENT. The mask is truncated if
1839 necessary to the width of mode MODE. The mask is zero-extended if
1840 BITSIZE+BITPOS is too small for MODE. */
1842 static rtx
1843 mask_rtx (enum machine_mode mode, int bitpos, int bitsize, int complement)
1845 double_int mask;
1847 mask = double_int::mask (bitsize);
1848 mask = mask.llshift (bitpos, HOST_BITS_PER_DOUBLE_INT);
1850 if (complement)
1851 mask = ~mask;
1853 return immed_double_int_const (mask, mode);
1856 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1857 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1859 static rtx
1860 lshift_value (enum machine_mode mode, rtx value, int bitpos, int bitsize)
1862 double_int val;
1864 val = double_int::from_uhwi (INTVAL (value)).zext (bitsize);
1865 val = val.llshift (bitpos, HOST_BITS_PER_DOUBLE_INT);
1867 return immed_double_int_const (val, mode);
1870 /* Extract a bit field that is split across two words
1871 and return an RTX for the result.
1873 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1874 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1875 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
1877 static rtx
1878 extract_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
1879 unsigned HOST_WIDE_INT bitpos, int unsignedp)
1881 unsigned int unit;
1882 unsigned int bitsdone = 0;
1883 rtx result = NULL_RTX;
1884 int first = 1;
1886 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1887 much at a time. */
1888 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
1889 unit = BITS_PER_WORD;
1890 else
1891 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
1893 while (bitsdone < bitsize)
1895 unsigned HOST_WIDE_INT thissize;
1896 rtx part, word;
1897 unsigned HOST_WIDE_INT thispos;
1898 unsigned HOST_WIDE_INT offset;
1900 offset = (bitpos + bitsdone) / unit;
1901 thispos = (bitpos + bitsdone) % unit;
1903 /* THISSIZE must not overrun a word boundary. Otherwise,
1904 extract_fixed_bit_field will call us again, and we will mutually
1905 recurse forever. */
1906 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1907 thissize = MIN (thissize, unit - thispos);
1909 /* If OP0 is a register, then handle OFFSET here.
1911 When handling multiword bitfields, extract_bit_field may pass
1912 down a word_mode SUBREG of a larger REG for a bitfield that actually
1913 crosses a word boundary. Thus, for a SUBREG, we must find
1914 the current word starting from the base register. */
1915 if (GET_CODE (op0) == SUBREG)
1917 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
1918 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1919 GET_MODE (SUBREG_REG (op0)));
1920 offset = 0;
1922 else if (REG_P (op0))
1924 word = operand_subword_force (op0, offset, GET_MODE (op0));
1925 offset = 0;
1927 else
1928 word = op0;
1930 /* Extract the parts in bit-counting order,
1931 whose meaning is determined by BYTES_PER_UNIT.
1932 OFFSET is in UNITs, and UNIT is in bits. */
1933 part = extract_fixed_bit_field (word_mode, word, thissize,
1934 offset * unit + thispos, 0, 1, false);
1935 bitsdone += thissize;
1937 /* Shift this part into place for the result. */
1938 if (BYTES_BIG_ENDIAN)
1940 if (bitsize != bitsdone)
1941 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1942 bitsize - bitsdone, 0, 1);
1944 else
1946 if (bitsdone != thissize)
1947 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1948 bitsdone - thissize, 0, 1);
1951 if (first)
1952 result = part;
1953 else
1954 /* Combine the parts with bitwise or. This works
1955 because we extracted each part as an unsigned bit field. */
1956 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
1957 OPTAB_LIB_WIDEN);
1959 first = 0;
1962 /* Unsigned bit field: we are done. */
1963 if (unsignedp)
1964 return result;
1965 /* Signed bit field: sign-extend with two arithmetic shifts. */
1966 result = expand_shift (LSHIFT_EXPR, word_mode, result,
1967 BITS_PER_WORD - bitsize, NULL_RTX, 0);
1968 return expand_shift (RSHIFT_EXPR, word_mode, result,
1969 BITS_PER_WORD - bitsize, NULL_RTX, 0);
1972 /* Try to read the low bits of SRC as an rvalue of mode MODE, preserving
1973 the bit pattern. SRC_MODE is the mode of SRC; if this is smaller than
1974 MODE, fill the upper bits with zeros. Fail if the layout of either
1975 mode is unknown (as for CC modes) or if the extraction would involve
1976 unprofitable mode punning. Return the value on success, otherwise
1977 return null.
1979 This is different from gen_lowpart* in these respects:
1981 - the returned value must always be considered an rvalue
1983 - when MODE is wider than SRC_MODE, the extraction involves
1984 a zero extension
1986 - when MODE is smaller than SRC_MODE, the extraction involves
1987 a truncation (and is thus subject to TRULY_NOOP_TRUNCATION).
1989 In other words, this routine performs a computation, whereas the
1990 gen_lowpart* routines are conceptually lvalue or rvalue subreg
1991 operations. */
1994 extract_low_bits (enum machine_mode mode, enum machine_mode src_mode, rtx src)
1996 enum machine_mode int_mode, src_int_mode;
1998 if (mode == src_mode)
1999 return src;
2001 if (CONSTANT_P (src))
2003 /* simplify_gen_subreg can't be used here, as if simplify_subreg
2004 fails, it will happily create (subreg (symbol_ref)) or similar
2005 invalid SUBREGs. */
2006 unsigned int byte = subreg_lowpart_offset (mode, src_mode);
2007 rtx ret = simplify_subreg (mode, src, src_mode, byte);
2008 if (ret)
2009 return ret;
2011 if (GET_MODE (src) == VOIDmode
2012 || !validate_subreg (mode, src_mode, src, byte))
2013 return NULL_RTX;
2015 src = force_reg (GET_MODE (src), src);
2016 return gen_rtx_SUBREG (mode, src, byte);
2019 if (GET_MODE_CLASS (mode) == MODE_CC || GET_MODE_CLASS (src_mode) == MODE_CC)
2020 return NULL_RTX;
2022 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (src_mode)
2023 && MODES_TIEABLE_P (mode, src_mode))
2025 rtx x = gen_lowpart_common (mode, src);
2026 if (x)
2027 return x;
2030 src_int_mode = int_mode_for_mode (src_mode);
2031 int_mode = int_mode_for_mode (mode);
2032 if (src_int_mode == BLKmode || int_mode == BLKmode)
2033 return NULL_RTX;
2035 if (!MODES_TIEABLE_P (src_int_mode, src_mode))
2036 return NULL_RTX;
2037 if (!MODES_TIEABLE_P (int_mode, mode))
2038 return NULL_RTX;
2040 src = gen_lowpart (src_int_mode, src);
2041 src = convert_modes (int_mode, src_int_mode, src, true);
2042 src = gen_lowpart (mode, src);
2043 return src;
2046 /* Add INC into TARGET. */
2048 void
2049 expand_inc (rtx target, rtx inc)
2051 rtx value = expand_binop (GET_MODE (target), add_optab,
2052 target, inc,
2053 target, 0, OPTAB_LIB_WIDEN);
2054 if (value != target)
2055 emit_move_insn (target, value);
2058 /* Subtract DEC from TARGET. */
2060 void
2061 expand_dec (rtx target, rtx dec)
2063 rtx value = expand_binop (GET_MODE (target), sub_optab,
2064 target, dec,
2065 target, 0, OPTAB_LIB_WIDEN);
2066 if (value != target)
2067 emit_move_insn (target, value);
2070 /* Output a shift instruction for expression code CODE,
2071 with SHIFTED being the rtx for the value to shift,
2072 and AMOUNT the rtx for the amount to shift by.
2073 Store the result in the rtx TARGET, if that is convenient.
2074 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2075 Return the rtx for where the value is. */
2077 static rtx
2078 expand_shift_1 (enum tree_code code, enum machine_mode mode, rtx shifted,
2079 rtx amount, rtx target, int unsignedp)
2081 rtx op1, temp = 0;
2082 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
2083 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2084 optab lshift_optab = ashl_optab;
2085 optab rshift_arith_optab = ashr_optab;
2086 optab rshift_uns_optab = lshr_optab;
2087 optab lrotate_optab = rotl_optab;
2088 optab rrotate_optab = rotr_optab;
2089 enum machine_mode op1_mode;
2090 int attempt;
2091 bool speed = optimize_insn_for_speed_p ();
2093 op1 = amount;
2094 op1_mode = GET_MODE (op1);
2096 /* Determine whether the shift/rotate amount is a vector, or scalar. If the
2097 shift amount is a vector, use the vector/vector shift patterns. */
2098 if (VECTOR_MODE_P (mode) && VECTOR_MODE_P (op1_mode))
2100 lshift_optab = vashl_optab;
2101 rshift_arith_optab = vashr_optab;
2102 rshift_uns_optab = vlshr_optab;
2103 lrotate_optab = vrotl_optab;
2104 rrotate_optab = vrotr_optab;
2107 /* Previously detected shift-counts computed by NEGATE_EXPR
2108 and shifted in the other direction; but that does not work
2109 on all machines. */
2111 if (SHIFT_COUNT_TRUNCATED)
2113 if (CONST_INT_P (op1)
2114 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2115 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode)))
2116 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2117 % GET_MODE_BITSIZE (mode));
2118 else if (GET_CODE (op1) == SUBREG
2119 && subreg_lowpart_p (op1)
2120 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op1)))
2121 && SCALAR_INT_MODE_P (GET_MODE (op1)))
2122 op1 = SUBREG_REG (op1);
2125 /* Canonicalize rotates by constant amount. If op1 is bitsize / 2,
2126 prefer left rotation, if op1 is from bitsize / 2 + 1 to
2127 bitsize - 1, use other direction of rotate with 1 .. bitsize / 2 - 1
2128 amount instead. */
2129 if (rotate
2130 && CONST_INT_P (op1)
2131 && IN_RANGE (INTVAL (op1), GET_MODE_BITSIZE (mode) / 2 + left,
2132 GET_MODE_BITSIZE (mode) - 1))
2134 op1 = GEN_INT (GET_MODE_BITSIZE (mode) - INTVAL (op1));
2135 left = !left;
2136 code = left ? LROTATE_EXPR : RROTATE_EXPR;
2139 if (op1 == const0_rtx)
2140 return shifted;
2142 /* Check whether its cheaper to implement a left shift by a constant
2143 bit count by a sequence of additions. */
2144 if (code == LSHIFT_EXPR
2145 && CONST_INT_P (op1)
2146 && INTVAL (op1) > 0
2147 && INTVAL (op1) < GET_MODE_PRECISION (mode)
2148 && INTVAL (op1) < MAX_BITS_PER_WORD
2149 && (shift_cost (speed, mode, INTVAL (op1))
2150 > INTVAL (op1) * add_cost (speed, mode))
2151 && shift_cost (speed, mode, INTVAL (op1)) != MAX_COST)
2153 int i;
2154 for (i = 0; i < INTVAL (op1); i++)
2156 temp = force_reg (mode, shifted);
2157 shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2158 unsignedp, OPTAB_LIB_WIDEN);
2160 return shifted;
2163 for (attempt = 0; temp == 0 && attempt < 3; attempt++)
2165 enum optab_methods methods;
2167 if (attempt == 0)
2168 methods = OPTAB_DIRECT;
2169 else if (attempt == 1)
2170 methods = OPTAB_WIDEN;
2171 else
2172 methods = OPTAB_LIB_WIDEN;
2174 if (rotate)
2176 /* Widening does not work for rotation. */
2177 if (methods == OPTAB_WIDEN)
2178 continue;
2179 else if (methods == OPTAB_LIB_WIDEN)
2181 /* If we have been unable to open-code this by a rotation,
2182 do it as the IOR of two shifts. I.e., to rotate A
2183 by N bits, compute
2184 (A << N) | ((unsigned) A >> ((-N) & (C - 1)))
2185 where C is the bitsize of A.
2187 It is theoretically possible that the target machine might
2188 not be able to perform either shift and hence we would
2189 be making two libcalls rather than just the one for the
2190 shift (similarly if IOR could not be done). We will allow
2191 this extremely unlikely lossage to avoid complicating the
2192 code below. */
2194 rtx subtarget = target == shifted ? 0 : target;
2195 rtx new_amount, other_amount;
2196 rtx temp1;
2198 new_amount = op1;
2199 if (op1 == const0_rtx)
2200 return shifted;
2201 else if (CONST_INT_P (op1))
2202 other_amount = GEN_INT (GET_MODE_BITSIZE (mode)
2203 - INTVAL (op1));
2204 else
2206 other_amount
2207 = simplify_gen_unary (NEG, GET_MODE (op1),
2208 op1, GET_MODE (op1));
2209 other_amount
2210 = simplify_gen_binary (AND, GET_MODE (op1),
2211 other_amount,
2212 GEN_INT (GET_MODE_PRECISION (mode)
2213 - 1));
2216 shifted = force_reg (mode, shifted);
2218 temp = expand_shift_1 (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2219 mode, shifted, new_amount, 0, 1);
2220 temp1 = expand_shift_1 (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2221 mode, shifted, other_amount,
2222 subtarget, 1);
2223 return expand_binop (mode, ior_optab, temp, temp1, target,
2224 unsignedp, methods);
2227 temp = expand_binop (mode,
2228 left ? lrotate_optab : rrotate_optab,
2229 shifted, op1, target, unsignedp, methods);
2231 else if (unsignedp)
2232 temp = expand_binop (mode,
2233 left ? lshift_optab : rshift_uns_optab,
2234 shifted, op1, target, unsignedp, methods);
2236 /* Do arithmetic shifts.
2237 Also, if we are going to widen the operand, we can just as well
2238 use an arithmetic right-shift instead of a logical one. */
2239 if (temp == 0 && ! rotate
2240 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2242 enum optab_methods methods1 = methods;
2244 /* If trying to widen a log shift to an arithmetic shift,
2245 don't accept an arithmetic shift of the same size. */
2246 if (unsignedp)
2247 methods1 = OPTAB_MUST_WIDEN;
2249 /* Arithmetic shift */
2251 temp = expand_binop (mode,
2252 left ? lshift_optab : rshift_arith_optab,
2253 shifted, op1, target, unsignedp, methods1);
2256 /* We used to try extzv here for logical right shifts, but that was
2257 only useful for one machine, the VAX, and caused poor code
2258 generation there for lshrdi3, so the code was deleted and a
2259 define_expand for lshrsi3 was added to vax.md. */
2262 gcc_assert (temp);
2263 return temp;
2266 /* Output a shift instruction for expression code CODE,
2267 with SHIFTED being the rtx for the value to shift,
2268 and AMOUNT the amount to shift by.
2269 Store the result in the rtx TARGET, if that is convenient.
2270 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2271 Return the rtx for where the value is. */
2274 expand_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2275 int amount, rtx target, int unsignedp)
2277 return expand_shift_1 (code, mode,
2278 shifted, GEN_INT (amount), target, unsignedp);
2281 /* Output a shift instruction for expression code CODE,
2282 with SHIFTED being the rtx for the value to shift,
2283 and AMOUNT the tree for the amount to shift by.
2284 Store the result in the rtx TARGET, if that is convenient.
2285 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2286 Return the rtx for where the value is. */
2289 expand_variable_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2290 tree amount, rtx target, int unsignedp)
2292 return expand_shift_1 (code, mode,
2293 shifted, expand_normal (amount), target, unsignedp);
2297 /* Indicates the type of fixup needed after a constant multiplication.
2298 BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2299 the result should be negated, and ADD_VARIANT means that the
2300 multiplicand should be added to the result. */
2301 enum mult_variant {basic_variant, negate_variant, add_variant};
2303 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2304 const struct mult_cost *, enum machine_mode mode);
2305 static bool choose_mult_variant (enum machine_mode, HOST_WIDE_INT,
2306 struct algorithm *, enum mult_variant *, int);
2307 static rtx expand_mult_const (enum machine_mode, rtx, HOST_WIDE_INT, rtx,
2308 const struct algorithm *, enum mult_variant);
2309 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2310 static rtx extract_high_half (enum machine_mode, rtx);
2311 static rtx expmed_mult_highpart (enum machine_mode, rtx, rtx, rtx, int, int);
2312 static rtx expmed_mult_highpart_optab (enum machine_mode, rtx, rtx, rtx,
2313 int, int);
2314 /* Compute and return the best algorithm for multiplying by T.
2315 The algorithm must cost less than cost_limit
2316 If retval.cost >= COST_LIMIT, no algorithm was found and all
2317 other field of the returned struct are undefined.
2318 MODE is the machine mode of the multiplication. */
2320 static void
2321 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2322 const struct mult_cost *cost_limit, enum machine_mode mode)
2324 int m;
2325 struct algorithm *alg_in, *best_alg;
2326 struct mult_cost best_cost;
2327 struct mult_cost new_limit;
2328 int op_cost, op_latency;
2329 unsigned HOST_WIDE_INT orig_t = t;
2330 unsigned HOST_WIDE_INT q;
2331 int maxm, hash_index;
2332 bool cache_hit = false;
2333 enum alg_code cache_alg = alg_zero;
2334 bool speed = optimize_insn_for_speed_p ();
2335 enum machine_mode imode;
2336 struct alg_hash_entry *entry_ptr;
2338 /* Indicate that no algorithm is yet found. If no algorithm
2339 is found, this value will be returned and indicate failure. */
2340 alg_out->cost.cost = cost_limit->cost + 1;
2341 alg_out->cost.latency = cost_limit->latency + 1;
2343 if (cost_limit->cost < 0
2344 || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2345 return;
2347 /* Be prepared for vector modes. */
2348 imode = GET_MODE_INNER (mode);
2349 if (imode == VOIDmode)
2350 imode = mode;
2352 maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (imode));
2354 /* Restrict the bits of "t" to the multiplication's mode. */
2355 t &= GET_MODE_MASK (imode);
2357 /* t == 1 can be done in zero cost. */
2358 if (t == 1)
2360 alg_out->ops = 1;
2361 alg_out->cost.cost = 0;
2362 alg_out->cost.latency = 0;
2363 alg_out->op[0] = alg_m;
2364 return;
2367 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2368 fail now. */
2369 if (t == 0)
2371 if (MULT_COST_LESS (cost_limit, zero_cost (speed)))
2372 return;
2373 else
2375 alg_out->ops = 1;
2376 alg_out->cost.cost = zero_cost (speed);
2377 alg_out->cost.latency = zero_cost (speed);
2378 alg_out->op[0] = alg_zero;
2379 return;
2383 /* We'll be needing a couple extra algorithm structures now. */
2385 alg_in = XALLOCA (struct algorithm);
2386 best_alg = XALLOCA (struct algorithm);
2387 best_cost = *cost_limit;
2389 /* Compute the hash index. */
2390 hash_index = (t ^ (unsigned int) mode ^ (speed * 256)) % NUM_ALG_HASH_ENTRIES;
2392 /* See if we already know what to do for T. */
2393 entry_ptr = alg_hash_entry_ptr (hash_index);
2394 if (entry_ptr->t == t
2395 && entry_ptr->mode == mode
2396 && entry_ptr->mode == mode
2397 && entry_ptr->speed == speed
2398 && entry_ptr->alg != alg_unknown)
2400 cache_alg = entry_ptr->alg;
2402 if (cache_alg == alg_impossible)
2404 /* The cache tells us that it's impossible to synthesize
2405 multiplication by T within entry_ptr->cost. */
2406 if (!CHEAPER_MULT_COST (&entry_ptr->cost, cost_limit))
2407 /* COST_LIMIT is at least as restrictive as the one
2408 recorded in the hash table, in which case we have no
2409 hope of synthesizing a multiplication. Just
2410 return. */
2411 return;
2413 /* If we get here, COST_LIMIT is less restrictive than the
2414 one recorded in the hash table, so we may be able to
2415 synthesize a multiplication. Proceed as if we didn't
2416 have the cache entry. */
2418 else
2420 if (CHEAPER_MULT_COST (cost_limit, &entry_ptr->cost))
2421 /* The cached algorithm shows that this multiplication
2422 requires more cost than COST_LIMIT. Just return. This
2423 way, we don't clobber this cache entry with
2424 alg_impossible but retain useful information. */
2425 return;
2427 cache_hit = true;
2429 switch (cache_alg)
2431 case alg_shift:
2432 goto do_alg_shift;
2434 case alg_add_t_m2:
2435 case alg_sub_t_m2:
2436 goto do_alg_addsub_t_m2;
2438 case alg_add_factor:
2439 case alg_sub_factor:
2440 goto do_alg_addsub_factor;
2442 case alg_add_t2_m:
2443 goto do_alg_add_t2_m;
2445 case alg_sub_t2_m:
2446 goto do_alg_sub_t2_m;
2448 default:
2449 gcc_unreachable ();
2454 /* If we have a group of zero bits at the low-order part of T, try
2455 multiplying by the remaining bits and then doing a shift. */
2457 if ((t & 1) == 0)
2459 do_alg_shift:
2460 m = floor_log2 (t & -t); /* m = number of low zero bits */
2461 if (m < maxm)
2463 q = t >> m;
2464 /* The function expand_shift will choose between a shift and
2465 a sequence of additions, so the observed cost is given as
2466 MIN (m * add_cost(speed, mode), shift_cost(speed, mode, m)). */
2467 op_cost = m * add_cost (speed, mode);
2468 if (shift_cost (speed, mode, m) < op_cost)
2469 op_cost = shift_cost (speed, mode, m);
2470 new_limit.cost = best_cost.cost - op_cost;
2471 new_limit.latency = best_cost.latency - op_cost;
2472 synth_mult (alg_in, q, &new_limit, mode);
2474 alg_in->cost.cost += op_cost;
2475 alg_in->cost.latency += op_cost;
2476 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2478 struct algorithm *x;
2479 best_cost = alg_in->cost;
2480 x = alg_in, alg_in = best_alg, best_alg = x;
2481 best_alg->log[best_alg->ops] = m;
2482 best_alg->op[best_alg->ops] = alg_shift;
2485 /* See if treating ORIG_T as a signed number yields a better
2486 sequence. Try this sequence only for a negative ORIG_T
2487 as it would be useless for a non-negative ORIG_T. */
2488 if ((HOST_WIDE_INT) orig_t < 0)
2490 /* Shift ORIG_T as follows because a right shift of a
2491 negative-valued signed type is implementation
2492 defined. */
2493 q = ~(~orig_t >> m);
2494 /* The function expand_shift will choose between a shift
2495 and a sequence of additions, so the observed cost is
2496 given as MIN (m * add_cost(speed, mode),
2497 shift_cost(speed, mode, m)). */
2498 op_cost = m * add_cost (speed, mode);
2499 if (shift_cost (speed, mode, m) < op_cost)
2500 op_cost = shift_cost (speed, mode, m);
2501 new_limit.cost = best_cost.cost - op_cost;
2502 new_limit.latency = best_cost.latency - op_cost;
2503 synth_mult (alg_in, q, &new_limit, mode);
2505 alg_in->cost.cost += op_cost;
2506 alg_in->cost.latency += op_cost;
2507 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2509 struct algorithm *x;
2510 best_cost = alg_in->cost;
2511 x = alg_in, alg_in = best_alg, best_alg = x;
2512 best_alg->log[best_alg->ops] = m;
2513 best_alg->op[best_alg->ops] = alg_shift;
2517 if (cache_hit)
2518 goto done;
2521 /* If we have an odd number, add or subtract one. */
2522 if ((t & 1) != 0)
2524 unsigned HOST_WIDE_INT w;
2526 do_alg_addsub_t_m2:
2527 for (w = 1; (w & t) != 0; w <<= 1)
2529 /* If T was -1, then W will be zero after the loop. This is another
2530 case where T ends with ...111. Handling this with (T + 1) and
2531 subtract 1 produces slightly better code and results in algorithm
2532 selection much faster than treating it like the ...0111 case
2533 below. */
2534 if (w == 0
2535 || (w > 2
2536 /* Reject the case where t is 3.
2537 Thus we prefer addition in that case. */
2538 && t != 3))
2540 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2542 op_cost = add_cost (speed, mode);
2543 new_limit.cost = best_cost.cost - op_cost;
2544 new_limit.latency = best_cost.latency - op_cost;
2545 synth_mult (alg_in, t + 1, &new_limit, mode);
2547 alg_in->cost.cost += op_cost;
2548 alg_in->cost.latency += op_cost;
2549 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2551 struct algorithm *x;
2552 best_cost = alg_in->cost;
2553 x = alg_in, alg_in = best_alg, best_alg = x;
2554 best_alg->log[best_alg->ops] = 0;
2555 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2558 else
2560 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2562 op_cost = add_cost (speed, mode);
2563 new_limit.cost = best_cost.cost - op_cost;
2564 new_limit.latency = best_cost.latency - op_cost;
2565 synth_mult (alg_in, t - 1, &new_limit, mode);
2567 alg_in->cost.cost += op_cost;
2568 alg_in->cost.latency += op_cost;
2569 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2571 struct algorithm *x;
2572 best_cost = alg_in->cost;
2573 x = alg_in, alg_in = best_alg, best_alg = x;
2574 best_alg->log[best_alg->ops] = 0;
2575 best_alg->op[best_alg->ops] = alg_add_t_m2;
2579 /* We may be able to calculate a * -7, a * -15, a * -31, etc
2580 quickly with a - a * n for some appropriate constant n. */
2581 m = exact_log2 (-orig_t + 1);
2582 if (m >= 0 && m < maxm)
2584 op_cost = shiftsub1_cost (speed, mode, m);
2585 new_limit.cost = best_cost.cost - op_cost;
2586 new_limit.latency = best_cost.latency - op_cost;
2587 synth_mult (alg_in, (unsigned HOST_WIDE_INT) (-orig_t + 1) >> m,
2588 &new_limit, mode);
2590 alg_in->cost.cost += op_cost;
2591 alg_in->cost.latency += op_cost;
2592 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2594 struct algorithm *x;
2595 best_cost = alg_in->cost;
2596 x = alg_in, alg_in = best_alg, best_alg = x;
2597 best_alg->log[best_alg->ops] = m;
2598 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2602 if (cache_hit)
2603 goto done;
2606 /* Look for factors of t of the form
2607 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2608 If we find such a factor, we can multiply by t using an algorithm that
2609 multiplies by q, shift the result by m and add/subtract it to itself.
2611 We search for large factors first and loop down, even if large factors
2612 are less probable than small; if we find a large factor we will find a
2613 good sequence quickly, and therefore be able to prune (by decreasing
2614 COST_LIMIT) the search. */
2616 do_alg_addsub_factor:
2617 for (m = floor_log2 (t - 1); m >= 2; m--)
2619 unsigned HOST_WIDE_INT d;
2621 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2622 if (t % d == 0 && t > d && m < maxm
2623 && (!cache_hit || cache_alg == alg_add_factor))
2625 /* If the target has a cheap shift-and-add instruction use
2626 that in preference to a shift insn followed by an add insn.
2627 Assume that the shift-and-add is "atomic" with a latency
2628 equal to its cost, otherwise assume that on superscalar
2629 hardware the shift may be executed concurrently with the
2630 earlier steps in the algorithm. */
2631 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2632 if (shiftadd_cost (speed, mode, m) < op_cost)
2634 op_cost = shiftadd_cost (speed, mode, m);
2635 op_latency = op_cost;
2637 else
2638 op_latency = add_cost (speed, mode);
2640 new_limit.cost = best_cost.cost - op_cost;
2641 new_limit.latency = best_cost.latency - op_latency;
2642 synth_mult (alg_in, t / d, &new_limit, mode);
2644 alg_in->cost.cost += op_cost;
2645 alg_in->cost.latency += op_latency;
2646 if (alg_in->cost.latency < op_cost)
2647 alg_in->cost.latency = op_cost;
2648 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2650 struct algorithm *x;
2651 best_cost = alg_in->cost;
2652 x = alg_in, alg_in = best_alg, best_alg = x;
2653 best_alg->log[best_alg->ops] = m;
2654 best_alg->op[best_alg->ops] = alg_add_factor;
2656 /* Other factors will have been taken care of in the recursion. */
2657 break;
2660 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2661 if (t % d == 0 && t > d && m < maxm
2662 && (!cache_hit || cache_alg == alg_sub_factor))
2664 /* If the target has a cheap shift-and-subtract insn use
2665 that in preference to a shift insn followed by a sub insn.
2666 Assume that the shift-and-sub is "atomic" with a latency
2667 equal to it's cost, otherwise assume that on superscalar
2668 hardware the shift may be executed concurrently with the
2669 earlier steps in the algorithm. */
2670 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2671 if (shiftsub0_cost (speed, mode, m) < op_cost)
2673 op_cost = shiftsub0_cost (speed, mode, m);
2674 op_latency = op_cost;
2676 else
2677 op_latency = add_cost (speed, mode);
2679 new_limit.cost = best_cost.cost - op_cost;
2680 new_limit.latency = best_cost.latency - op_latency;
2681 synth_mult (alg_in, t / d, &new_limit, mode);
2683 alg_in->cost.cost += op_cost;
2684 alg_in->cost.latency += op_latency;
2685 if (alg_in->cost.latency < op_cost)
2686 alg_in->cost.latency = op_cost;
2687 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2689 struct algorithm *x;
2690 best_cost = alg_in->cost;
2691 x = alg_in, alg_in = best_alg, best_alg = x;
2692 best_alg->log[best_alg->ops] = m;
2693 best_alg->op[best_alg->ops] = alg_sub_factor;
2695 break;
2698 if (cache_hit)
2699 goto done;
2701 /* Try shift-and-add (load effective address) instructions,
2702 i.e. do a*3, a*5, a*9. */
2703 if ((t & 1) != 0)
2705 do_alg_add_t2_m:
2706 q = t - 1;
2707 q = q & -q;
2708 m = exact_log2 (q);
2709 if (m >= 0 && m < maxm)
2711 op_cost = shiftadd_cost (speed, mode, m);
2712 new_limit.cost = best_cost.cost - op_cost;
2713 new_limit.latency = best_cost.latency - op_cost;
2714 synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2716 alg_in->cost.cost += op_cost;
2717 alg_in->cost.latency += op_cost;
2718 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2720 struct algorithm *x;
2721 best_cost = alg_in->cost;
2722 x = alg_in, alg_in = best_alg, best_alg = x;
2723 best_alg->log[best_alg->ops] = m;
2724 best_alg->op[best_alg->ops] = alg_add_t2_m;
2727 if (cache_hit)
2728 goto done;
2730 do_alg_sub_t2_m:
2731 q = t + 1;
2732 q = q & -q;
2733 m = exact_log2 (q);
2734 if (m >= 0 && m < maxm)
2736 op_cost = shiftsub0_cost (speed, mode, m);
2737 new_limit.cost = best_cost.cost - op_cost;
2738 new_limit.latency = best_cost.latency - op_cost;
2739 synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2741 alg_in->cost.cost += op_cost;
2742 alg_in->cost.latency += op_cost;
2743 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2745 struct algorithm *x;
2746 best_cost = alg_in->cost;
2747 x = alg_in, alg_in = best_alg, best_alg = x;
2748 best_alg->log[best_alg->ops] = m;
2749 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2752 if (cache_hit)
2753 goto done;
2756 done:
2757 /* If best_cost has not decreased, we have not found any algorithm. */
2758 if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
2760 /* We failed to find an algorithm. Record alg_impossible for
2761 this case (that is, <T, MODE, COST_LIMIT>) so that next time
2762 we are asked to find an algorithm for T within the same or
2763 lower COST_LIMIT, we can immediately return to the
2764 caller. */
2765 entry_ptr->t = t;
2766 entry_ptr->mode = mode;
2767 entry_ptr->speed = speed;
2768 entry_ptr->alg = alg_impossible;
2769 entry_ptr->cost = *cost_limit;
2770 return;
2773 /* Cache the result. */
2774 if (!cache_hit)
2776 entry_ptr->t = t;
2777 entry_ptr->mode = mode;
2778 entry_ptr->speed = speed;
2779 entry_ptr->alg = best_alg->op[best_alg->ops];
2780 entry_ptr->cost.cost = best_cost.cost;
2781 entry_ptr->cost.latency = best_cost.latency;
2784 /* If we are getting a too long sequence for `struct algorithm'
2785 to record, make this search fail. */
2786 if (best_alg->ops == MAX_BITS_PER_WORD)
2787 return;
2789 /* Copy the algorithm from temporary space to the space at alg_out.
2790 We avoid using structure assignment because the majority of
2791 best_alg is normally undefined, and this is a critical function. */
2792 alg_out->ops = best_alg->ops + 1;
2793 alg_out->cost = best_cost;
2794 memcpy (alg_out->op, best_alg->op,
2795 alg_out->ops * sizeof *alg_out->op);
2796 memcpy (alg_out->log, best_alg->log,
2797 alg_out->ops * sizeof *alg_out->log);
2800 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2801 Try three variations:
2803 - a shift/add sequence based on VAL itself
2804 - a shift/add sequence based on -VAL, followed by a negation
2805 - a shift/add sequence based on VAL - 1, followed by an addition.
2807 Return true if the cheapest of these cost less than MULT_COST,
2808 describing the algorithm in *ALG and final fixup in *VARIANT. */
2810 static bool
2811 choose_mult_variant (enum machine_mode mode, HOST_WIDE_INT val,
2812 struct algorithm *alg, enum mult_variant *variant,
2813 int mult_cost)
2815 struct algorithm alg2;
2816 struct mult_cost limit;
2817 int op_cost;
2818 bool speed = optimize_insn_for_speed_p ();
2820 /* Fail quickly for impossible bounds. */
2821 if (mult_cost < 0)
2822 return false;
2824 /* Ensure that mult_cost provides a reasonable upper bound.
2825 Any constant multiplication can be performed with less
2826 than 2 * bits additions. */
2827 op_cost = 2 * GET_MODE_UNIT_BITSIZE (mode) * add_cost (speed, mode);
2828 if (mult_cost > op_cost)
2829 mult_cost = op_cost;
2831 *variant = basic_variant;
2832 limit.cost = mult_cost;
2833 limit.latency = mult_cost;
2834 synth_mult (alg, val, &limit, mode);
2836 /* This works only if the inverted value actually fits in an
2837 `unsigned int' */
2838 if (HOST_BITS_PER_INT >= GET_MODE_UNIT_BITSIZE (mode))
2840 op_cost = neg_cost(speed, mode);
2841 if (MULT_COST_LESS (&alg->cost, mult_cost))
2843 limit.cost = alg->cost.cost - op_cost;
2844 limit.latency = alg->cost.latency - op_cost;
2846 else
2848 limit.cost = mult_cost - op_cost;
2849 limit.latency = mult_cost - op_cost;
2852 synth_mult (&alg2, -val, &limit, mode);
2853 alg2.cost.cost += op_cost;
2854 alg2.cost.latency += op_cost;
2855 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2856 *alg = alg2, *variant = negate_variant;
2859 /* This proves very useful for division-by-constant. */
2860 op_cost = add_cost (speed, mode);
2861 if (MULT_COST_LESS (&alg->cost, mult_cost))
2863 limit.cost = alg->cost.cost - op_cost;
2864 limit.latency = alg->cost.latency - op_cost;
2866 else
2868 limit.cost = mult_cost - op_cost;
2869 limit.latency = mult_cost - op_cost;
2872 synth_mult (&alg2, val - 1, &limit, mode);
2873 alg2.cost.cost += op_cost;
2874 alg2.cost.latency += op_cost;
2875 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2876 *alg = alg2, *variant = add_variant;
2878 return MULT_COST_LESS (&alg->cost, mult_cost);
2881 /* A subroutine of expand_mult, used for constant multiplications.
2882 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2883 convenient. Use the shift/add sequence described by ALG and apply
2884 the final fixup specified by VARIANT. */
2886 static rtx
2887 expand_mult_const (enum machine_mode mode, rtx op0, HOST_WIDE_INT val,
2888 rtx target, const struct algorithm *alg,
2889 enum mult_variant variant)
2891 HOST_WIDE_INT val_so_far;
2892 rtx insn, accum, tem;
2893 int opno;
2894 enum machine_mode nmode;
2896 /* Avoid referencing memory over and over and invalid sharing
2897 on SUBREGs. */
2898 op0 = force_reg (mode, op0);
2900 /* ACCUM starts out either as OP0 or as a zero, depending on
2901 the first operation. */
2903 if (alg->op[0] == alg_zero)
2905 accum = copy_to_mode_reg (mode, CONST0_RTX (mode));
2906 val_so_far = 0;
2908 else if (alg->op[0] == alg_m)
2910 accum = copy_to_mode_reg (mode, op0);
2911 val_so_far = 1;
2913 else
2914 gcc_unreachable ();
2916 for (opno = 1; opno < alg->ops; opno++)
2918 int log = alg->log[opno];
2919 rtx shift_subtarget = optimize ? 0 : accum;
2920 rtx add_target
2921 = (opno == alg->ops - 1 && target != 0 && variant != add_variant
2922 && !optimize)
2923 ? target : 0;
2924 rtx accum_target = optimize ? 0 : accum;
2925 rtx accum_inner;
2927 switch (alg->op[opno])
2929 case alg_shift:
2930 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
2931 /* REG_EQUAL note will be attached to the following insn. */
2932 emit_move_insn (accum, tem);
2933 val_so_far <<= log;
2934 break;
2936 case alg_add_t_m2:
2937 tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
2938 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2939 add_target ? add_target : accum_target);
2940 val_so_far += (HOST_WIDE_INT) 1 << log;
2941 break;
2943 case alg_sub_t_m2:
2944 tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
2945 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
2946 add_target ? add_target : accum_target);
2947 val_so_far -= (HOST_WIDE_INT) 1 << log;
2948 break;
2950 case alg_add_t2_m:
2951 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2952 log, shift_subtarget, 0);
2953 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
2954 add_target ? add_target : accum_target);
2955 val_so_far = (val_so_far << log) + 1;
2956 break;
2958 case alg_sub_t2_m:
2959 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2960 log, shift_subtarget, 0);
2961 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
2962 add_target ? add_target : accum_target);
2963 val_so_far = (val_so_far << log) - 1;
2964 break;
2966 case alg_add_factor:
2967 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
2968 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2969 add_target ? add_target : accum_target);
2970 val_so_far += val_so_far << log;
2971 break;
2973 case alg_sub_factor:
2974 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
2975 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
2976 (add_target
2977 ? add_target : (optimize ? 0 : tem)));
2978 val_so_far = (val_so_far << log) - val_so_far;
2979 break;
2981 default:
2982 gcc_unreachable ();
2985 if (SCALAR_INT_MODE_P (mode))
2987 /* Write a REG_EQUAL note on the last insn so that we can cse
2988 multiplication sequences. Note that if ACCUM is a SUBREG,
2989 we've set the inner register and must properly indicate that. */
2990 tem = op0, nmode = mode;
2991 accum_inner = accum;
2992 if (GET_CODE (accum) == SUBREG)
2994 accum_inner = SUBREG_REG (accum);
2995 nmode = GET_MODE (accum_inner);
2996 tem = gen_lowpart (nmode, op0);
2999 insn = get_last_insn ();
3000 set_dst_reg_note (insn, REG_EQUAL,
3001 gen_rtx_MULT (nmode, tem, GEN_INT (val_so_far)),
3002 accum_inner);
3006 if (variant == negate_variant)
3008 val_so_far = -val_so_far;
3009 accum = expand_unop (mode, neg_optab, accum, target, 0);
3011 else if (variant == add_variant)
3013 val_so_far = val_so_far + 1;
3014 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
3017 /* Compare only the bits of val and val_so_far that are significant
3018 in the result mode, to avoid sign-/zero-extension confusion. */
3019 nmode = GET_MODE_INNER (mode);
3020 if (nmode == VOIDmode)
3021 nmode = mode;
3022 val &= GET_MODE_MASK (nmode);
3023 val_so_far &= GET_MODE_MASK (nmode);
3024 gcc_assert (val == val_so_far);
3026 return accum;
3029 /* Perform a multiplication and return an rtx for the result.
3030 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3031 TARGET is a suggestion for where to store the result (an rtx).
3033 We check specially for a constant integer as OP1.
3034 If you want this check for OP0 as well, then before calling
3035 you should swap the two operands if OP0 would be constant. */
3038 expand_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3039 int unsignedp)
3041 enum mult_variant variant;
3042 struct algorithm algorithm;
3043 rtx scalar_op1;
3044 int max_cost;
3045 bool speed = optimize_insn_for_speed_p ();
3046 bool do_trapv = flag_trapv && SCALAR_INT_MODE_P (mode) && !unsignedp;
3048 if (CONSTANT_P (op0))
3050 rtx temp = op0;
3051 op0 = op1;
3052 op1 = temp;
3055 /* For vectors, there are several simplifications that can be made if
3056 all elements of the vector constant are identical. */
3057 scalar_op1 = op1;
3058 if (GET_CODE (op1) == CONST_VECTOR)
3060 int i, n = CONST_VECTOR_NUNITS (op1);
3061 scalar_op1 = CONST_VECTOR_ELT (op1, 0);
3062 for (i = 1; i < n; ++i)
3063 if (!rtx_equal_p (scalar_op1, CONST_VECTOR_ELT (op1, i)))
3064 goto skip_scalar;
3067 if (INTEGRAL_MODE_P (mode))
3069 rtx fake_reg;
3070 HOST_WIDE_INT coeff;
3071 bool is_neg;
3072 int mode_bitsize;
3074 if (op1 == CONST0_RTX (mode))
3075 return op1;
3076 if (op1 == CONST1_RTX (mode))
3077 return op0;
3078 if (op1 == CONSTM1_RTX (mode))
3079 return expand_unop (mode, do_trapv ? negv_optab : neg_optab,
3080 op0, target, 0);
3082 if (do_trapv)
3083 goto skip_synth;
3085 /* These are the operations that are potentially turned into
3086 a sequence of shifts and additions. */
3087 mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
3089 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3090 less than or equal in size to `unsigned int' this doesn't matter.
3091 If the mode is larger than `unsigned int', then synth_mult works
3092 only if the constant value exactly fits in an `unsigned int' without
3093 any truncation. This means that multiplying by negative values does
3094 not work; results are off by 2^32 on a 32 bit machine. */
3096 if (CONST_INT_P (scalar_op1))
3098 coeff = INTVAL (scalar_op1);
3099 is_neg = coeff < 0;
3101 else if (CONST_DOUBLE_AS_INT_P (scalar_op1))
3103 /* If we are multiplying in DImode, it may still be a win
3104 to try to work with shifts and adds. */
3105 if (CONST_DOUBLE_HIGH (scalar_op1) == 0
3106 && (CONST_DOUBLE_LOW (scalar_op1) > 0
3107 || (CONST_DOUBLE_LOW (scalar_op1) < 0
3108 && EXACT_POWER_OF_2_OR_ZERO_P
3109 (CONST_DOUBLE_LOW (scalar_op1)))))
3111 coeff = CONST_DOUBLE_LOW (scalar_op1);
3112 is_neg = false;
3114 else if (CONST_DOUBLE_LOW (scalar_op1) == 0)
3116 coeff = CONST_DOUBLE_HIGH (scalar_op1);
3117 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3119 int shift = floor_log2 (coeff) + HOST_BITS_PER_WIDE_INT;
3120 if (shift < HOST_BITS_PER_DOUBLE_INT - 1
3121 || mode_bitsize <= HOST_BITS_PER_DOUBLE_INT)
3122 return expand_shift (LSHIFT_EXPR, mode, op0,
3123 shift, target, unsignedp);
3125 goto skip_synth;
3127 else
3128 goto skip_synth;
3130 else
3131 goto skip_synth;
3133 /* We used to test optimize here, on the grounds that it's better to
3134 produce a smaller program when -O is not used. But this causes
3135 such a terrible slowdown sometimes that it seems better to always
3136 use synth_mult. */
3138 /* Special case powers of two. */
3139 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff)
3140 && !(is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT))
3141 return expand_shift (LSHIFT_EXPR, mode, op0,
3142 floor_log2 (coeff), target, unsignedp);
3144 fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3146 /* Attempt to handle multiplication of DImode values by negative
3147 coefficients, by performing the multiplication by a positive
3148 multiplier and then inverting the result. */
3149 if (is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT)
3151 /* Its safe to use -coeff even for INT_MIN, as the
3152 result is interpreted as an unsigned coefficient.
3153 Exclude cost of op0 from max_cost to match the cost
3154 calculation of the synth_mult. */
3155 coeff = -(unsigned HOST_WIDE_INT) coeff;
3156 max_cost = (set_src_cost (gen_rtx_MULT (mode, fake_reg, op1), speed)
3157 - neg_cost(speed, mode));
3158 if (max_cost <= 0)
3159 goto skip_synth;
3161 /* Special case powers of two. */
3162 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3164 rtx temp = expand_shift (LSHIFT_EXPR, mode, op0,
3165 floor_log2 (coeff), target, unsignedp);
3166 return expand_unop (mode, neg_optab, temp, target, 0);
3169 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3170 max_cost))
3172 rtx temp = expand_mult_const (mode, op0, coeff, NULL_RTX,
3173 &algorithm, variant);
3174 return expand_unop (mode, neg_optab, temp, target, 0);
3176 goto skip_synth;
3179 /* Exclude cost of op0 from max_cost to match the cost
3180 calculation of the synth_mult. */
3181 max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, op1), speed);
3182 if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
3183 return expand_mult_const (mode, op0, coeff, target,
3184 &algorithm, variant);
3186 skip_synth:
3188 /* Expand x*2.0 as x+x. */
3189 if (CONST_DOUBLE_AS_FLOAT_P (scalar_op1))
3191 REAL_VALUE_TYPE d;
3192 REAL_VALUE_FROM_CONST_DOUBLE (d, scalar_op1);
3194 if (REAL_VALUES_EQUAL (d, dconst2))
3196 op0 = force_reg (GET_MODE (op0), op0);
3197 return expand_binop (mode, add_optab, op0, op0,
3198 target, unsignedp, OPTAB_LIB_WIDEN);
3201 skip_scalar:
3203 /* This used to use umul_optab if unsigned, but for non-widening multiply
3204 there is no difference between signed and unsigned. */
3205 op0 = expand_binop (mode, do_trapv ? smulv_optab : smul_optab,
3206 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
3207 gcc_assert (op0);
3208 return op0;
3211 /* Return a cost estimate for multiplying a register by the given
3212 COEFFicient in the given MODE and SPEED. */
3215 mult_by_coeff_cost (HOST_WIDE_INT coeff, enum machine_mode mode, bool speed)
3217 int max_cost;
3218 struct algorithm algorithm;
3219 enum mult_variant variant;
3221 rtx fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3222 max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, fake_reg), speed);
3223 if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
3224 return algorithm.cost.cost;
3225 else
3226 return max_cost;
3229 /* Perform a widening multiplication and return an rtx for the result.
3230 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3231 TARGET is a suggestion for where to store the result (an rtx).
3232 THIS_OPTAB is the optab we should use, it must be either umul_widen_optab
3233 or smul_widen_optab.
3235 We check specially for a constant integer as OP1, comparing the
3236 cost of a widening multiply against the cost of a sequence of shifts
3237 and adds. */
3240 expand_widening_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3241 int unsignedp, optab this_optab)
3243 bool speed = optimize_insn_for_speed_p ();
3244 rtx cop1;
3246 if (CONST_INT_P (op1)
3247 && GET_MODE (op0) != VOIDmode
3248 && (cop1 = convert_modes (mode, GET_MODE (op0), op1,
3249 this_optab == umul_widen_optab))
3250 && CONST_INT_P (cop1)
3251 && (INTVAL (cop1) >= 0
3252 || HWI_COMPUTABLE_MODE_P (mode)))
3254 HOST_WIDE_INT coeff = INTVAL (cop1);
3255 int max_cost;
3256 enum mult_variant variant;
3257 struct algorithm algorithm;
3259 /* Special case powers of two. */
3260 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3262 op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
3263 return expand_shift (LSHIFT_EXPR, mode, op0,
3264 floor_log2 (coeff), target, unsignedp);
3267 /* Exclude cost of op0 from max_cost to match the cost
3268 calculation of the synth_mult. */
3269 max_cost = mul_widen_cost (speed, mode);
3270 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3271 max_cost))
3273 op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
3274 return expand_mult_const (mode, op0, coeff, target,
3275 &algorithm, variant);
3278 return expand_binop (mode, this_optab, op0, op1, target,
3279 unsignedp, OPTAB_LIB_WIDEN);
3282 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3283 replace division by D, and put the least significant N bits of the result
3284 in *MULTIPLIER_PTR and return the most significant bit.
3286 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3287 needed precision is in PRECISION (should be <= N).
3289 PRECISION should be as small as possible so this function can choose
3290 multiplier more freely.
3292 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3293 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3295 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3296 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3298 unsigned HOST_WIDE_INT
3299 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
3300 unsigned HOST_WIDE_INT *multiplier_ptr,
3301 int *post_shift_ptr, int *lgup_ptr)
3303 double_int mhigh, mlow;
3304 int lgup, post_shift;
3305 int pow, pow2;
3307 /* lgup = ceil(log2(divisor)); */
3308 lgup = ceil_log2 (d);
3310 gcc_assert (lgup <= n);
3312 pow = n + lgup;
3313 pow2 = n + lgup - precision;
3315 /* We could handle this with some effort, but this case is much
3316 better handled directly with a scc insn, so rely on caller using
3317 that. */
3318 gcc_assert (pow != HOST_BITS_PER_DOUBLE_INT);
3320 /* mlow = 2^(N + lgup)/d */
3321 double_int val = double_int_zero.set_bit (pow);
3322 mlow = val.div (double_int::from_uhwi (d), true, TRUNC_DIV_EXPR);
3324 /* mhigh = (2^(N + lgup) + 2^(N + lgup - precision))/d */
3325 val |= double_int_zero.set_bit (pow2);
3326 mhigh = val.div (double_int::from_uhwi (d), true, TRUNC_DIV_EXPR);
3328 gcc_assert (!mhigh.high || val.high - d < d);
3329 gcc_assert (mhigh.high <= 1 && mlow.high <= 1);
3330 /* Assert that mlow < mhigh. */
3331 gcc_assert (mlow.ult (mhigh));
3333 /* If precision == N, then mlow, mhigh exceed 2^N
3334 (but they do not exceed 2^(N+1)). */
3336 /* Reduce to lowest terms. */
3337 for (post_shift = lgup; post_shift > 0; post_shift--)
3339 int shft = HOST_BITS_PER_WIDE_INT - 1;
3340 unsigned HOST_WIDE_INT ml_lo = (mlow.high << shft) | (mlow.low >> 1);
3341 unsigned HOST_WIDE_INT mh_lo = (mhigh.high << shft) | (mhigh.low >> 1);
3342 if (ml_lo >= mh_lo)
3343 break;
3345 mlow = double_int::from_uhwi (ml_lo);
3346 mhigh = double_int::from_uhwi (mh_lo);
3349 *post_shift_ptr = post_shift;
3350 *lgup_ptr = lgup;
3351 if (n < HOST_BITS_PER_WIDE_INT)
3353 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
3354 *multiplier_ptr = mhigh.low & mask;
3355 return mhigh.low >= mask;
3357 else
3359 *multiplier_ptr = mhigh.low;
3360 return mhigh.high;
3364 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3365 congruent to 1 (mod 2**N). */
3367 static unsigned HOST_WIDE_INT
3368 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
3370 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3372 /* The algorithm notes that the choice y = x satisfies
3373 x*y == 1 mod 2^3, since x is assumed odd.
3374 Each iteration doubles the number of bits of significance in y. */
3376 unsigned HOST_WIDE_INT mask;
3377 unsigned HOST_WIDE_INT y = x;
3378 int nbit = 3;
3380 mask = (n == HOST_BITS_PER_WIDE_INT
3381 ? ~(unsigned HOST_WIDE_INT) 0
3382 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
3384 while (nbit < n)
3386 y = y * (2 - x*y) & mask; /* Modulo 2^N */
3387 nbit *= 2;
3389 return y;
3392 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3393 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3394 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3395 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3396 become signed.
3398 The result is put in TARGET if that is convenient.
3400 MODE is the mode of operation. */
3403 expand_mult_highpart_adjust (enum machine_mode mode, rtx adj_operand, rtx op0,
3404 rtx op1, rtx target, int unsignedp)
3406 rtx tem;
3407 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3409 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3410 GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
3411 tem = expand_and (mode, tem, op1, NULL_RTX);
3412 adj_operand
3413 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3414 adj_operand);
3416 tem = expand_shift (RSHIFT_EXPR, mode, op1,
3417 GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
3418 tem = expand_and (mode, tem, op0, NULL_RTX);
3419 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3420 target);
3422 return target;
3425 /* Subroutine of expmed_mult_highpart. Return the MODE high part of OP. */
3427 static rtx
3428 extract_high_half (enum machine_mode mode, rtx op)
3430 enum machine_mode wider_mode;
3432 if (mode == word_mode)
3433 return gen_highpart (mode, op);
3435 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3437 wider_mode = GET_MODE_WIDER_MODE (mode);
3438 op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3439 GET_MODE_BITSIZE (mode), 0, 1);
3440 return convert_modes (mode, wider_mode, op, 0);
3443 /* Like expmed_mult_highpart, but only consider using a multiplication
3444 optab. OP1 is an rtx for the constant operand. */
3446 static rtx
3447 expmed_mult_highpart_optab (enum machine_mode mode, rtx op0, rtx op1,
3448 rtx target, int unsignedp, int max_cost)
3450 rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3451 enum machine_mode wider_mode;
3452 optab moptab;
3453 rtx tem;
3454 int size;
3455 bool speed = optimize_insn_for_speed_p ();
3457 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3459 wider_mode = GET_MODE_WIDER_MODE (mode);
3460 size = GET_MODE_BITSIZE (mode);
3462 /* Firstly, try using a multiplication insn that only generates the needed
3463 high part of the product, and in the sign flavor of unsignedp. */
3464 if (mul_highpart_cost (speed, mode) < max_cost)
3466 moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3467 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3468 unsignedp, OPTAB_DIRECT);
3469 if (tem)
3470 return tem;
3473 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3474 Need to adjust the result after the multiplication. */
3475 if (size - 1 < BITS_PER_WORD
3476 && (mul_highpart_cost (speed, mode)
3477 + 2 * shift_cost (speed, mode, size-1)
3478 + 4 * add_cost (speed, mode) < max_cost))
3480 moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3481 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3482 unsignedp, OPTAB_DIRECT);
3483 if (tem)
3484 /* We used the wrong signedness. Adjust the result. */
3485 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3486 tem, unsignedp);
3489 /* Try widening multiplication. */
3490 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3491 if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
3492 && mul_widen_cost (speed, wider_mode) < max_cost)
3494 tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3495 unsignedp, OPTAB_WIDEN);
3496 if (tem)
3497 return extract_high_half (mode, tem);
3500 /* Try widening the mode and perform a non-widening multiplication. */
3501 if (optab_handler (smul_optab, wider_mode) != CODE_FOR_nothing
3502 && size - 1 < BITS_PER_WORD
3503 && (mul_cost (speed, wider_mode) + shift_cost (speed, mode, size-1)
3504 < max_cost))
3506 rtx insns, wop0, wop1;
3508 /* We need to widen the operands, for example to ensure the
3509 constant multiplier is correctly sign or zero extended.
3510 Use a sequence to clean-up any instructions emitted by
3511 the conversions if things don't work out. */
3512 start_sequence ();
3513 wop0 = convert_modes (wider_mode, mode, op0, unsignedp);
3514 wop1 = convert_modes (wider_mode, mode, op1, unsignedp);
3515 tem = expand_binop (wider_mode, smul_optab, wop0, wop1, 0,
3516 unsignedp, OPTAB_WIDEN);
3517 insns = get_insns ();
3518 end_sequence ();
3520 if (tem)
3522 emit_insn (insns);
3523 return extract_high_half (mode, tem);
3527 /* Try widening multiplication of opposite signedness, and adjust. */
3528 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3529 if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
3530 && size - 1 < BITS_PER_WORD
3531 && (mul_widen_cost (speed, wider_mode)
3532 + 2 * shift_cost (speed, mode, size-1)
3533 + 4 * add_cost (speed, mode) < max_cost))
3535 tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3536 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3537 if (tem != 0)
3539 tem = extract_high_half (mode, tem);
3540 /* We used the wrong signedness. Adjust the result. */
3541 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3542 target, unsignedp);
3546 return 0;
3549 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3550 putting the high half of the result in TARGET if that is convenient,
3551 and return where the result is. If the operation can not be performed,
3552 0 is returned.
3554 MODE is the mode of operation and result.
3556 UNSIGNEDP nonzero means unsigned multiply.
3558 MAX_COST is the total allowed cost for the expanded RTL. */
3560 static rtx
3561 expmed_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
3562 rtx target, int unsignedp, int max_cost)
3564 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
3565 unsigned HOST_WIDE_INT cnst1;
3566 int extra_cost;
3567 bool sign_adjust = false;
3568 enum mult_variant variant;
3569 struct algorithm alg;
3570 rtx tem;
3571 bool speed = optimize_insn_for_speed_p ();
3573 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3574 /* We can't support modes wider than HOST_BITS_PER_INT. */
3575 gcc_assert (HWI_COMPUTABLE_MODE_P (mode));
3577 cnst1 = INTVAL (op1) & GET_MODE_MASK (mode);
3579 /* We can't optimize modes wider than BITS_PER_WORD.
3580 ??? We might be able to perform double-word arithmetic if
3581 mode == word_mode, however all the cost calculations in
3582 synth_mult etc. assume single-word operations. */
3583 if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3584 return expmed_mult_highpart_optab (mode, op0, op1, target,
3585 unsignedp, max_cost);
3587 extra_cost = shift_cost (speed, mode, GET_MODE_BITSIZE (mode) - 1);
3589 /* Check whether we try to multiply by a negative constant. */
3590 if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3592 sign_adjust = true;
3593 extra_cost += add_cost (speed, mode);
3596 /* See whether shift/add multiplication is cheap enough. */
3597 if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3598 max_cost - extra_cost))
3600 /* See whether the specialized multiplication optabs are
3601 cheaper than the shift/add version. */
3602 tem = expmed_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3603 alg.cost.cost + extra_cost);
3604 if (tem)
3605 return tem;
3607 tem = convert_to_mode (wider_mode, op0, unsignedp);
3608 tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3609 tem = extract_high_half (mode, tem);
3611 /* Adjust result for signedness. */
3612 if (sign_adjust)
3613 tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3615 return tem;
3617 return expmed_mult_highpart_optab (mode, op0, op1, target,
3618 unsignedp, max_cost);
3622 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3624 static rtx
3625 expand_smod_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3627 unsigned HOST_WIDE_INT masklow, maskhigh;
3628 rtx result, temp, shift, label;
3629 int logd;
3631 logd = floor_log2 (d);
3632 result = gen_reg_rtx (mode);
3634 /* Avoid conditional branches when they're expensive. */
3635 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2
3636 && optimize_insn_for_speed_p ())
3638 rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3639 mode, 0, -1);
3640 if (signmask)
3642 signmask = force_reg (mode, signmask);
3643 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3644 shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3646 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3647 which instruction sequence to use. If logical right shifts
3648 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3649 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3651 temp = gen_rtx_LSHIFTRT (mode, result, shift);
3652 if (optab_handler (lshr_optab, mode) == CODE_FOR_nothing
3653 || (set_src_cost (temp, optimize_insn_for_speed_p ())
3654 > COSTS_N_INSNS (2)))
3656 temp = expand_binop (mode, xor_optab, op0, signmask,
3657 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3658 temp = expand_binop (mode, sub_optab, temp, signmask,
3659 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3660 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3661 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3662 temp = expand_binop (mode, xor_optab, temp, signmask,
3663 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3664 temp = expand_binop (mode, sub_optab, temp, signmask,
3665 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3667 else
3669 signmask = expand_binop (mode, lshr_optab, signmask, shift,
3670 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3671 signmask = force_reg (mode, signmask);
3673 temp = expand_binop (mode, add_optab, op0, signmask,
3674 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3675 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3676 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3677 temp = expand_binop (mode, sub_optab, temp, signmask,
3678 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3680 return temp;
3684 /* Mask contains the mode's signbit and the significant bits of the
3685 modulus. By including the signbit in the operation, many targets
3686 can avoid an explicit compare operation in the following comparison
3687 against zero. */
3689 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3690 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3692 masklow |= (HOST_WIDE_INT) -1 << (GET_MODE_BITSIZE (mode) - 1);
3693 maskhigh = -1;
3695 else
3696 maskhigh = (HOST_WIDE_INT) -1
3697 << (GET_MODE_BITSIZE (mode) - HOST_BITS_PER_WIDE_INT - 1);
3699 temp = expand_binop (mode, and_optab, op0,
3700 immed_double_const (masklow, maskhigh, mode),
3701 result, 1, OPTAB_LIB_WIDEN);
3702 if (temp != result)
3703 emit_move_insn (result, temp);
3705 label = gen_label_rtx ();
3706 do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3708 temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3709 0, OPTAB_LIB_WIDEN);
3710 masklow = (HOST_WIDE_INT) -1 << logd;
3711 maskhigh = -1;
3712 temp = expand_binop (mode, ior_optab, temp,
3713 immed_double_const (masklow, maskhigh, mode),
3714 result, 1, OPTAB_LIB_WIDEN);
3715 temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3716 0, OPTAB_LIB_WIDEN);
3717 if (temp != result)
3718 emit_move_insn (result, temp);
3719 emit_label (label);
3720 return result;
3723 /* Expand signed division of OP0 by a power of two D in mode MODE.
3724 This routine is only called for positive values of D. */
3726 static rtx
3727 expand_sdiv_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3729 rtx temp, label;
3730 int logd;
3732 logd = floor_log2 (d);
3734 if (d == 2
3735 && BRANCH_COST (optimize_insn_for_speed_p (),
3736 false) >= 1)
3738 temp = gen_reg_rtx (mode);
3739 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3740 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3741 0, OPTAB_LIB_WIDEN);
3742 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3745 #ifdef HAVE_conditional_move
3746 if (BRANCH_COST (optimize_insn_for_speed_p (), false)
3747 >= 2)
3749 rtx temp2;
3751 /* ??? emit_conditional_move forces a stack adjustment via
3752 compare_from_rtx so, if the sequence is discarded, it will
3753 be lost. Do it now instead. */
3754 do_pending_stack_adjust ();
3756 start_sequence ();
3757 temp2 = copy_to_mode_reg (mode, op0);
3758 temp = expand_binop (mode, add_optab, temp2, GEN_INT (d-1),
3759 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3760 temp = force_reg (mode, temp);
3762 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3763 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3764 mode, temp, temp2, mode, 0);
3765 if (temp2)
3767 rtx seq = get_insns ();
3768 end_sequence ();
3769 emit_insn (seq);
3770 return expand_shift (RSHIFT_EXPR, mode, temp2, logd, NULL_RTX, 0);
3772 end_sequence ();
3774 #endif
3776 if (BRANCH_COST (optimize_insn_for_speed_p (),
3777 false) >= 2)
3779 int ushift = GET_MODE_BITSIZE (mode) - logd;
3781 temp = gen_reg_rtx (mode);
3782 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3783 if (shift_cost (optimize_insn_for_speed_p (), mode, ushift)
3784 > COSTS_N_INSNS (1))
3785 temp = expand_binop (mode, and_optab, temp, GEN_INT (d - 1),
3786 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3787 else
3788 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3789 ushift, NULL_RTX, 1);
3790 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3791 0, OPTAB_LIB_WIDEN);
3792 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3795 label = gen_label_rtx ();
3796 temp = copy_to_mode_reg (mode, op0);
3797 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3798 expand_inc (temp, GEN_INT (d - 1));
3799 emit_label (label);
3800 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3803 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3804 if that is convenient, and returning where the result is.
3805 You may request either the quotient or the remainder as the result;
3806 specify REM_FLAG nonzero to get the remainder.
3808 CODE is the expression code for which kind of division this is;
3809 it controls how rounding is done. MODE is the machine mode to use.
3810 UNSIGNEDP nonzero means do unsigned division. */
3812 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3813 and then correct it by or'ing in missing high bits
3814 if result of ANDI is nonzero.
3815 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3816 This could optimize to a bfexts instruction.
3817 But C doesn't use these operations, so their optimizations are
3818 left for later. */
3819 /* ??? For modulo, we don't actually need the highpart of the first product,
3820 the low part will do nicely. And for small divisors, the second multiply
3821 can also be a low-part only multiply or even be completely left out.
3822 E.g. to calculate the remainder of a division by 3 with a 32 bit
3823 multiply, multiply with 0x55555556 and extract the upper two bits;
3824 the result is exact for inputs up to 0x1fffffff.
3825 The input range can be reduced by using cross-sum rules.
3826 For odd divisors >= 3, the following table gives right shift counts
3827 so that if a number is shifted by an integer multiple of the given
3828 amount, the remainder stays the same:
3829 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3830 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3831 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3832 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3833 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3835 Cross-sum rules for even numbers can be derived by leaving as many bits
3836 to the right alone as the divisor has zeros to the right.
3837 E.g. if x is an unsigned 32 bit number:
3838 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3842 expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
3843 rtx op0, rtx op1, rtx target, int unsignedp)
3845 enum machine_mode compute_mode;
3846 rtx tquotient;
3847 rtx quotient = 0, remainder = 0;
3848 rtx last;
3849 int size;
3850 rtx insn;
3851 optab optab1, optab2;
3852 int op1_is_constant, op1_is_pow2 = 0;
3853 int max_cost, extra_cost;
3854 static HOST_WIDE_INT last_div_const = 0;
3855 bool speed = optimize_insn_for_speed_p ();
3857 op1_is_constant = CONST_INT_P (op1);
3858 if (op1_is_constant)
3860 unsigned HOST_WIDE_INT ext_op1 = UINTVAL (op1);
3861 if (unsignedp)
3862 ext_op1 &= GET_MODE_MASK (mode);
3863 op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
3864 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
3868 This is the structure of expand_divmod:
3870 First comes code to fix up the operands so we can perform the operations
3871 correctly and efficiently.
3873 Second comes a switch statement with code specific for each rounding mode.
3874 For some special operands this code emits all RTL for the desired
3875 operation, for other cases, it generates only a quotient and stores it in
3876 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3877 to indicate that it has not done anything.
3879 Last comes code that finishes the operation. If QUOTIENT is set and
3880 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3881 QUOTIENT is not set, it is computed using trunc rounding.
3883 We try to generate special code for division and remainder when OP1 is a
3884 constant. If |OP1| = 2**n we can use shifts and some other fast
3885 operations. For other values of OP1, we compute a carefully selected
3886 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3887 by m.
3889 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3890 half of the product. Different strategies for generating the product are
3891 implemented in expmed_mult_highpart.
3893 If what we actually want is the remainder, we generate that by another
3894 by-constant multiplication and a subtraction. */
3896 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3897 code below will malfunction if we are, so check here and handle
3898 the special case if so. */
3899 if (op1 == const1_rtx)
3900 return rem_flag ? const0_rtx : op0;
3902 /* When dividing by -1, we could get an overflow.
3903 negv_optab can handle overflows. */
3904 if (! unsignedp && op1 == constm1_rtx)
3906 if (rem_flag)
3907 return const0_rtx;
3908 return expand_unop (mode, flag_trapv && GET_MODE_CLASS(mode) == MODE_INT
3909 ? negv_optab : neg_optab, op0, target, 0);
3912 if (target
3913 /* Don't use the function value register as a target
3914 since we have to read it as well as write it,
3915 and function-inlining gets confused by this. */
3916 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3917 /* Don't clobber an operand while doing a multi-step calculation. */
3918 || ((rem_flag || op1_is_constant)
3919 && (reg_mentioned_p (target, op0)
3920 || (MEM_P (op0) && MEM_P (target))))
3921 || reg_mentioned_p (target, op1)
3922 || (MEM_P (op1) && MEM_P (target))))
3923 target = 0;
3925 /* Get the mode in which to perform this computation. Normally it will
3926 be MODE, but sometimes we can't do the desired operation in MODE.
3927 If so, pick a wider mode in which we can do the operation. Convert
3928 to that mode at the start to avoid repeated conversions.
3930 First see what operations we need. These depend on the expression
3931 we are evaluating. (We assume that divxx3 insns exist under the
3932 same conditions that modxx3 insns and that these insns don't normally
3933 fail. If these assumptions are not correct, we may generate less
3934 efficient code in some cases.)
3936 Then see if we find a mode in which we can open-code that operation
3937 (either a division, modulus, or shift). Finally, check for the smallest
3938 mode for which we can do the operation with a library call. */
3940 /* We might want to refine this now that we have division-by-constant
3941 optimization. Since expmed_mult_highpart tries so many variants, it is
3942 not straightforward to generalize this. Maybe we should make an array
3943 of possible modes in init_expmed? Save this for GCC 2.7. */
3945 optab1 = ((op1_is_pow2 && op1 != const0_rtx)
3946 ? (unsignedp ? lshr_optab : ashr_optab)
3947 : (unsignedp ? udiv_optab : sdiv_optab));
3948 optab2 = ((op1_is_pow2 && op1 != const0_rtx)
3949 ? optab1
3950 : (unsignedp ? udivmod_optab : sdivmod_optab));
3952 for (compute_mode = mode; compute_mode != VOIDmode;
3953 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3954 if (optab_handler (optab1, compute_mode) != CODE_FOR_nothing
3955 || optab_handler (optab2, compute_mode) != CODE_FOR_nothing)
3956 break;
3958 if (compute_mode == VOIDmode)
3959 for (compute_mode = mode; compute_mode != VOIDmode;
3960 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3961 if (optab_libfunc (optab1, compute_mode)
3962 || optab_libfunc (optab2, compute_mode))
3963 break;
3965 /* If we still couldn't find a mode, use MODE, but expand_binop will
3966 probably die. */
3967 if (compute_mode == VOIDmode)
3968 compute_mode = mode;
3970 if (target && GET_MODE (target) == compute_mode)
3971 tquotient = target;
3972 else
3973 tquotient = gen_reg_rtx (compute_mode);
3975 size = GET_MODE_BITSIZE (compute_mode);
3976 #if 0
3977 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3978 (mode), and thereby get better code when OP1 is a constant. Do that
3979 later. It will require going over all usages of SIZE below. */
3980 size = GET_MODE_BITSIZE (mode);
3981 #endif
3983 /* Only deduct something for a REM if the last divide done was
3984 for a different constant. Then set the constant of the last
3985 divide. */
3986 max_cost = (unsignedp
3987 ? udiv_cost (speed, compute_mode)
3988 : sdiv_cost (speed, compute_mode));
3989 if (rem_flag && ! (last_div_const != 0 && op1_is_constant
3990 && INTVAL (op1) == last_div_const))
3991 max_cost -= (mul_cost (speed, compute_mode)
3992 + add_cost (speed, compute_mode));
3994 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
3996 /* Now convert to the best mode to use. */
3997 if (compute_mode != mode)
3999 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
4000 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
4002 /* convert_modes may have placed op1 into a register, so we
4003 must recompute the following. */
4004 op1_is_constant = CONST_INT_P (op1);
4005 op1_is_pow2 = (op1_is_constant
4006 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4007 || (! unsignedp
4008 && EXACT_POWER_OF_2_OR_ZERO_P (-UINTVAL (op1))))));
4011 /* If one of the operands is a volatile MEM, copy it into a register. */
4013 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
4014 op0 = force_reg (compute_mode, op0);
4015 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
4016 op1 = force_reg (compute_mode, op1);
4018 /* If we need the remainder or if OP1 is constant, we need to
4019 put OP0 in a register in case it has any queued subexpressions. */
4020 if (rem_flag || op1_is_constant)
4021 op0 = force_reg (compute_mode, op0);
4023 last = get_last_insn ();
4025 /* Promote floor rounding to trunc rounding for unsigned operations. */
4026 if (unsignedp)
4028 if (code == FLOOR_DIV_EXPR)
4029 code = TRUNC_DIV_EXPR;
4030 if (code == FLOOR_MOD_EXPR)
4031 code = TRUNC_MOD_EXPR;
4032 if (code == EXACT_DIV_EXPR && op1_is_pow2)
4033 code = TRUNC_DIV_EXPR;
4036 if (op1 != const0_rtx)
4037 switch (code)
4039 case TRUNC_MOD_EXPR:
4040 case TRUNC_DIV_EXPR:
4041 if (op1_is_constant)
4043 if (unsignedp)
4045 unsigned HOST_WIDE_INT mh, ml;
4046 int pre_shift, post_shift;
4047 int dummy;
4048 unsigned HOST_WIDE_INT d = (INTVAL (op1)
4049 & GET_MODE_MASK (compute_mode));
4051 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4053 pre_shift = floor_log2 (d);
4054 if (rem_flag)
4056 remainder
4057 = expand_binop (compute_mode, and_optab, op0,
4058 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4059 remainder, 1,
4060 OPTAB_LIB_WIDEN);
4061 if (remainder)
4062 return gen_lowpart (mode, remainder);
4064 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4065 pre_shift, tquotient, 1);
4067 else if (size <= HOST_BITS_PER_WIDE_INT)
4069 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
4071 /* Most significant bit of divisor is set; emit an scc
4072 insn. */
4073 quotient = emit_store_flag_force (tquotient, GEU, op0, op1,
4074 compute_mode, 1, 1);
4076 else
4078 /* Find a suitable multiplier and right shift count
4079 instead of multiplying with D. */
4081 mh = choose_multiplier (d, size, size,
4082 &ml, &post_shift, &dummy);
4084 /* If the suggested multiplier is more than SIZE bits,
4085 we can do better for even divisors, using an
4086 initial right shift. */
4087 if (mh != 0 && (d & 1) == 0)
4089 pre_shift = floor_log2 (d & -d);
4090 mh = choose_multiplier (d >> pre_shift, size,
4091 size - pre_shift,
4092 &ml, &post_shift, &dummy);
4093 gcc_assert (!mh);
4095 else
4096 pre_shift = 0;
4098 if (mh != 0)
4100 rtx t1, t2, t3, t4;
4102 if (post_shift - 1 >= BITS_PER_WORD)
4103 goto fail1;
4105 extra_cost
4106 = (shift_cost (speed, compute_mode, post_shift - 1)
4107 + shift_cost (speed, compute_mode, 1)
4108 + 2 * add_cost (speed, compute_mode));
4109 t1 = expmed_mult_highpart (compute_mode, op0,
4110 GEN_INT (ml),
4111 NULL_RTX, 1,
4112 max_cost - extra_cost);
4113 if (t1 == 0)
4114 goto fail1;
4115 t2 = force_operand (gen_rtx_MINUS (compute_mode,
4116 op0, t1),
4117 NULL_RTX);
4118 t3 = expand_shift (RSHIFT_EXPR, compute_mode,
4119 t2, 1, NULL_RTX, 1);
4120 t4 = force_operand (gen_rtx_PLUS (compute_mode,
4121 t1, t3),
4122 NULL_RTX);
4123 quotient = expand_shift
4124 (RSHIFT_EXPR, compute_mode, t4,
4125 post_shift - 1, tquotient, 1);
4127 else
4129 rtx t1, t2;
4131 if (pre_shift >= BITS_PER_WORD
4132 || post_shift >= BITS_PER_WORD)
4133 goto fail1;
4135 t1 = expand_shift
4136 (RSHIFT_EXPR, compute_mode, op0,
4137 pre_shift, NULL_RTX, 1);
4138 extra_cost
4139 = (shift_cost (speed, compute_mode, pre_shift)
4140 + shift_cost (speed, compute_mode, post_shift));
4141 t2 = expmed_mult_highpart (compute_mode, t1,
4142 GEN_INT (ml),
4143 NULL_RTX, 1,
4144 max_cost - extra_cost);
4145 if (t2 == 0)
4146 goto fail1;
4147 quotient = expand_shift
4148 (RSHIFT_EXPR, compute_mode, t2,
4149 post_shift, tquotient, 1);
4153 else /* Too wide mode to use tricky code */
4154 break;
4156 insn = get_last_insn ();
4157 if (insn != last)
4158 set_dst_reg_note (insn, REG_EQUAL,
4159 gen_rtx_UDIV (compute_mode, op0, op1),
4160 quotient);
4162 else /* TRUNC_DIV, signed */
4164 unsigned HOST_WIDE_INT ml;
4165 int lgup, post_shift;
4166 rtx mlr;
4167 HOST_WIDE_INT d = INTVAL (op1);
4168 unsigned HOST_WIDE_INT abs_d;
4170 /* Since d might be INT_MIN, we have to cast to
4171 unsigned HOST_WIDE_INT before negating to avoid
4172 undefined signed overflow. */
4173 abs_d = (d >= 0
4174 ? (unsigned HOST_WIDE_INT) d
4175 : - (unsigned HOST_WIDE_INT) d);
4177 /* n rem d = n rem -d */
4178 if (rem_flag && d < 0)
4180 d = abs_d;
4181 op1 = gen_int_mode (abs_d, compute_mode);
4184 if (d == 1)
4185 quotient = op0;
4186 else if (d == -1)
4187 quotient = expand_unop (compute_mode, neg_optab, op0,
4188 tquotient, 0);
4189 else if (HOST_BITS_PER_WIDE_INT >= size
4190 && abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
4192 /* This case is not handled correctly below. */
4193 quotient = emit_store_flag (tquotient, EQ, op0, op1,
4194 compute_mode, 1, 1);
4195 if (quotient == 0)
4196 goto fail1;
4198 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
4199 && (rem_flag
4200 ? smod_pow2_cheap (speed, compute_mode)
4201 : sdiv_pow2_cheap (speed, compute_mode))
4202 /* We assume that cheap metric is true if the
4203 optab has an expander for this mode. */
4204 && ((optab_handler ((rem_flag ? smod_optab
4205 : sdiv_optab),
4206 compute_mode)
4207 != CODE_FOR_nothing)
4208 || (optab_handler (sdivmod_optab,
4209 compute_mode)
4210 != CODE_FOR_nothing)))
4212 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
4214 if (rem_flag)
4216 remainder = expand_smod_pow2 (compute_mode, op0, d);
4217 if (remainder)
4218 return gen_lowpart (mode, remainder);
4221 if (sdiv_pow2_cheap (speed, compute_mode)
4222 && ((optab_handler (sdiv_optab, compute_mode)
4223 != CODE_FOR_nothing)
4224 || (optab_handler (sdivmod_optab, compute_mode)
4225 != CODE_FOR_nothing)))
4226 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
4227 compute_mode, op0,
4228 gen_int_mode (abs_d,
4229 compute_mode),
4230 NULL_RTX, 0);
4231 else
4232 quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
4234 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4235 negate the quotient. */
4236 if (d < 0)
4238 insn = get_last_insn ();
4239 if (insn != last
4240 && abs_d < ((unsigned HOST_WIDE_INT) 1
4241 << (HOST_BITS_PER_WIDE_INT - 1)))
4242 set_dst_reg_note (insn, REG_EQUAL,
4243 gen_rtx_DIV (compute_mode, op0,
4244 gen_int_mode
4245 (abs_d,
4246 compute_mode)),
4247 quotient);
4249 quotient = expand_unop (compute_mode, neg_optab,
4250 quotient, quotient, 0);
4253 else if (size <= HOST_BITS_PER_WIDE_INT)
4255 choose_multiplier (abs_d, size, size - 1,
4256 &ml, &post_shift, &lgup);
4257 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
4259 rtx t1, t2, t3;
4261 if (post_shift >= BITS_PER_WORD
4262 || size - 1 >= BITS_PER_WORD)
4263 goto fail1;
4265 extra_cost = (shift_cost (speed, compute_mode, post_shift)
4266 + shift_cost (speed, compute_mode, size - 1)
4267 + add_cost (speed, compute_mode));
4268 t1 = expmed_mult_highpart (compute_mode, op0,
4269 GEN_INT (ml), NULL_RTX, 0,
4270 max_cost - extra_cost);
4271 if (t1 == 0)
4272 goto fail1;
4273 t2 = expand_shift
4274 (RSHIFT_EXPR, compute_mode, t1,
4275 post_shift, NULL_RTX, 0);
4276 t3 = expand_shift
4277 (RSHIFT_EXPR, compute_mode, op0,
4278 size - 1, NULL_RTX, 0);
4279 if (d < 0)
4280 quotient
4281 = force_operand (gen_rtx_MINUS (compute_mode,
4282 t3, t2),
4283 tquotient);
4284 else
4285 quotient
4286 = force_operand (gen_rtx_MINUS (compute_mode,
4287 t2, t3),
4288 tquotient);
4290 else
4292 rtx t1, t2, t3, t4;
4294 if (post_shift >= BITS_PER_WORD
4295 || size - 1 >= BITS_PER_WORD)
4296 goto fail1;
4298 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
4299 mlr = gen_int_mode (ml, compute_mode);
4300 extra_cost = (shift_cost (speed, compute_mode, post_shift)
4301 + shift_cost (speed, compute_mode, size - 1)
4302 + 2 * add_cost (speed, compute_mode));
4303 t1 = expmed_mult_highpart (compute_mode, op0, mlr,
4304 NULL_RTX, 0,
4305 max_cost - extra_cost);
4306 if (t1 == 0)
4307 goto fail1;
4308 t2 = force_operand (gen_rtx_PLUS (compute_mode,
4309 t1, op0),
4310 NULL_RTX);
4311 t3 = expand_shift
4312 (RSHIFT_EXPR, compute_mode, t2,
4313 post_shift, NULL_RTX, 0);
4314 t4 = expand_shift
4315 (RSHIFT_EXPR, compute_mode, op0,
4316 size - 1, NULL_RTX, 0);
4317 if (d < 0)
4318 quotient
4319 = force_operand (gen_rtx_MINUS (compute_mode,
4320 t4, t3),
4321 tquotient);
4322 else
4323 quotient
4324 = force_operand (gen_rtx_MINUS (compute_mode,
4325 t3, t4),
4326 tquotient);
4329 else /* Too wide mode to use tricky code */
4330 break;
4332 insn = get_last_insn ();
4333 if (insn != last)
4334 set_dst_reg_note (insn, REG_EQUAL,
4335 gen_rtx_DIV (compute_mode, op0, op1),
4336 quotient);
4338 break;
4340 fail1:
4341 delete_insns_since (last);
4342 break;
4344 case FLOOR_DIV_EXPR:
4345 case FLOOR_MOD_EXPR:
4346 /* We will come here only for signed operations. */
4347 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4349 unsigned HOST_WIDE_INT mh, ml;
4350 int pre_shift, lgup, post_shift;
4351 HOST_WIDE_INT d = INTVAL (op1);
4353 if (d > 0)
4355 /* We could just as easily deal with negative constants here,
4356 but it does not seem worth the trouble for GCC 2.6. */
4357 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4359 pre_shift = floor_log2 (d);
4360 if (rem_flag)
4362 remainder = expand_binop (compute_mode, and_optab, op0,
4363 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4364 remainder, 0, OPTAB_LIB_WIDEN);
4365 if (remainder)
4366 return gen_lowpart (mode, remainder);
4368 quotient = expand_shift
4369 (RSHIFT_EXPR, compute_mode, op0,
4370 pre_shift, tquotient, 0);
4372 else
4374 rtx t1, t2, t3, t4;
4376 mh = choose_multiplier (d, size, size - 1,
4377 &ml, &post_shift, &lgup);
4378 gcc_assert (!mh);
4380 if (post_shift < BITS_PER_WORD
4381 && size - 1 < BITS_PER_WORD)
4383 t1 = expand_shift
4384 (RSHIFT_EXPR, compute_mode, op0,
4385 size - 1, NULL_RTX, 0);
4386 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
4387 NULL_RTX, 0, OPTAB_WIDEN);
4388 extra_cost = (shift_cost (speed, compute_mode, post_shift)
4389 + shift_cost (speed, compute_mode, size - 1)
4390 + 2 * add_cost (speed, compute_mode));
4391 t3 = expmed_mult_highpart (compute_mode, t2,
4392 GEN_INT (ml), NULL_RTX, 1,
4393 max_cost - extra_cost);
4394 if (t3 != 0)
4396 t4 = expand_shift
4397 (RSHIFT_EXPR, compute_mode, t3,
4398 post_shift, NULL_RTX, 1);
4399 quotient = expand_binop (compute_mode, xor_optab,
4400 t4, t1, tquotient, 0,
4401 OPTAB_WIDEN);
4406 else
4408 rtx nsign, t1, t2, t3, t4;
4409 t1 = force_operand (gen_rtx_PLUS (compute_mode,
4410 op0, constm1_rtx), NULL_RTX);
4411 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
4412 0, OPTAB_WIDEN);
4413 nsign = expand_shift
4414 (RSHIFT_EXPR, compute_mode, t2,
4415 size - 1, NULL_RTX, 0);
4416 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
4417 NULL_RTX);
4418 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
4419 NULL_RTX, 0);
4420 if (t4)
4422 rtx t5;
4423 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
4424 NULL_RTX, 0);
4425 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4426 t4, t5),
4427 tquotient);
4432 if (quotient != 0)
4433 break;
4434 delete_insns_since (last);
4436 /* Try using an instruction that produces both the quotient and
4437 remainder, using truncation. We can easily compensate the quotient
4438 or remainder to get floor rounding, once we have the remainder.
4439 Notice that we compute also the final remainder value here,
4440 and return the result right away. */
4441 if (target == 0 || GET_MODE (target) != compute_mode)
4442 target = gen_reg_rtx (compute_mode);
4444 if (rem_flag)
4446 remainder
4447 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4448 quotient = gen_reg_rtx (compute_mode);
4450 else
4452 quotient
4453 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4454 remainder = gen_reg_rtx (compute_mode);
4457 if (expand_twoval_binop (sdivmod_optab, op0, op1,
4458 quotient, remainder, 0))
4460 /* This could be computed with a branch-less sequence.
4461 Save that for later. */
4462 rtx tem;
4463 rtx label = gen_label_rtx ();
4464 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4465 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4466 NULL_RTX, 0, OPTAB_WIDEN);
4467 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4468 expand_dec (quotient, const1_rtx);
4469 expand_inc (remainder, op1);
4470 emit_label (label);
4471 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4474 /* No luck with division elimination or divmod. Have to do it
4475 by conditionally adjusting op0 *and* the result. */
4477 rtx label1, label2, label3, label4, label5;
4478 rtx adjusted_op0;
4479 rtx tem;
4481 quotient = gen_reg_rtx (compute_mode);
4482 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4483 label1 = gen_label_rtx ();
4484 label2 = gen_label_rtx ();
4485 label3 = gen_label_rtx ();
4486 label4 = gen_label_rtx ();
4487 label5 = gen_label_rtx ();
4488 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4489 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4490 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4491 quotient, 0, OPTAB_LIB_WIDEN);
4492 if (tem != quotient)
4493 emit_move_insn (quotient, tem);
4494 emit_jump_insn (gen_jump (label5));
4495 emit_barrier ();
4496 emit_label (label1);
4497 expand_inc (adjusted_op0, const1_rtx);
4498 emit_jump_insn (gen_jump (label4));
4499 emit_barrier ();
4500 emit_label (label2);
4501 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4502 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4503 quotient, 0, OPTAB_LIB_WIDEN);
4504 if (tem != quotient)
4505 emit_move_insn (quotient, tem);
4506 emit_jump_insn (gen_jump (label5));
4507 emit_barrier ();
4508 emit_label (label3);
4509 expand_dec (adjusted_op0, const1_rtx);
4510 emit_label (label4);
4511 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4512 quotient, 0, OPTAB_LIB_WIDEN);
4513 if (tem != quotient)
4514 emit_move_insn (quotient, tem);
4515 expand_dec (quotient, const1_rtx);
4516 emit_label (label5);
4518 break;
4520 case CEIL_DIV_EXPR:
4521 case CEIL_MOD_EXPR:
4522 if (unsignedp)
4524 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
4526 rtx t1, t2, t3;
4527 unsigned HOST_WIDE_INT d = INTVAL (op1);
4528 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4529 floor_log2 (d), tquotient, 1);
4530 t2 = expand_binop (compute_mode, and_optab, op0,
4531 GEN_INT (d - 1),
4532 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4533 t3 = gen_reg_rtx (compute_mode);
4534 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4535 compute_mode, 1, 1);
4536 if (t3 == 0)
4538 rtx lab;
4539 lab = gen_label_rtx ();
4540 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4541 expand_inc (t1, const1_rtx);
4542 emit_label (lab);
4543 quotient = t1;
4545 else
4546 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4547 t1, t3),
4548 tquotient);
4549 break;
4552 /* Try using an instruction that produces both the quotient and
4553 remainder, using truncation. We can easily compensate the
4554 quotient or remainder to get ceiling rounding, once we have the
4555 remainder. Notice that we compute also the final remainder
4556 value here, and return the result right away. */
4557 if (target == 0 || GET_MODE (target) != compute_mode)
4558 target = gen_reg_rtx (compute_mode);
4560 if (rem_flag)
4562 remainder = (REG_P (target)
4563 ? target : gen_reg_rtx (compute_mode));
4564 quotient = gen_reg_rtx (compute_mode);
4566 else
4568 quotient = (REG_P (target)
4569 ? target : gen_reg_rtx (compute_mode));
4570 remainder = gen_reg_rtx (compute_mode);
4573 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4574 remainder, 1))
4576 /* This could be computed with a branch-less sequence.
4577 Save that for later. */
4578 rtx label = gen_label_rtx ();
4579 do_cmp_and_jump (remainder, const0_rtx, EQ,
4580 compute_mode, label);
4581 expand_inc (quotient, const1_rtx);
4582 expand_dec (remainder, op1);
4583 emit_label (label);
4584 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4587 /* No luck with division elimination or divmod. Have to do it
4588 by conditionally adjusting op0 *and* the result. */
4590 rtx label1, label2;
4591 rtx adjusted_op0, tem;
4593 quotient = gen_reg_rtx (compute_mode);
4594 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4595 label1 = gen_label_rtx ();
4596 label2 = gen_label_rtx ();
4597 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4598 compute_mode, label1);
4599 emit_move_insn (quotient, const0_rtx);
4600 emit_jump_insn (gen_jump (label2));
4601 emit_barrier ();
4602 emit_label (label1);
4603 expand_dec (adjusted_op0, const1_rtx);
4604 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4605 quotient, 1, OPTAB_LIB_WIDEN);
4606 if (tem != quotient)
4607 emit_move_insn (quotient, tem);
4608 expand_inc (quotient, const1_rtx);
4609 emit_label (label2);
4612 else /* signed */
4614 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4615 && INTVAL (op1) >= 0)
4617 /* This is extremely similar to the code for the unsigned case
4618 above. For 2.7 we should merge these variants, but for
4619 2.6.1 I don't want to touch the code for unsigned since that
4620 get used in C. The signed case will only be used by other
4621 languages (Ada). */
4623 rtx t1, t2, t3;
4624 unsigned HOST_WIDE_INT d = INTVAL (op1);
4625 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4626 floor_log2 (d), tquotient, 0);
4627 t2 = expand_binop (compute_mode, and_optab, op0,
4628 GEN_INT (d - 1),
4629 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4630 t3 = gen_reg_rtx (compute_mode);
4631 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4632 compute_mode, 1, 1);
4633 if (t3 == 0)
4635 rtx lab;
4636 lab = gen_label_rtx ();
4637 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4638 expand_inc (t1, const1_rtx);
4639 emit_label (lab);
4640 quotient = t1;
4642 else
4643 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4644 t1, t3),
4645 tquotient);
4646 break;
4649 /* Try using an instruction that produces both the quotient and
4650 remainder, using truncation. We can easily compensate the
4651 quotient or remainder to get ceiling rounding, once we have the
4652 remainder. Notice that we compute also the final remainder
4653 value here, and return the result right away. */
4654 if (target == 0 || GET_MODE (target) != compute_mode)
4655 target = gen_reg_rtx (compute_mode);
4656 if (rem_flag)
4658 remainder= (REG_P (target)
4659 ? target : gen_reg_rtx (compute_mode));
4660 quotient = gen_reg_rtx (compute_mode);
4662 else
4664 quotient = (REG_P (target)
4665 ? target : gen_reg_rtx (compute_mode));
4666 remainder = gen_reg_rtx (compute_mode);
4669 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4670 remainder, 0))
4672 /* This could be computed with a branch-less sequence.
4673 Save that for later. */
4674 rtx tem;
4675 rtx label = gen_label_rtx ();
4676 do_cmp_and_jump (remainder, const0_rtx, EQ,
4677 compute_mode, label);
4678 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4679 NULL_RTX, 0, OPTAB_WIDEN);
4680 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4681 expand_inc (quotient, const1_rtx);
4682 expand_dec (remainder, op1);
4683 emit_label (label);
4684 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4687 /* No luck with division elimination or divmod. Have to do it
4688 by conditionally adjusting op0 *and* the result. */
4690 rtx label1, label2, label3, label4, label5;
4691 rtx adjusted_op0;
4692 rtx tem;
4694 quotient = gen_reg_rtx (compute_mode);
4695 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4696 label1 = gen_label_rtx ();
4697 label2 = gen_label_rtx ();
4698 label3 = gen_label_rtx ();
4699 label4 = gen_label_rtx ();
4700 label5 = gen_label_rtx ();
4701 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4702 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4703 compute_mode, label1);
4704 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4705 quotient, 0, OPTAB_LIB_WIDEN);
4706 if (tem != quotient)
4707 emit_move_insn (quotient, tem);
4708 emit_jump_insn (gen_jump (label5));
4709 emit_barrier ();
4710 emit_label (label1);
4711 expand_dec (adjusted_op0, const1_rtx);
4712 emit_jump_insn (gen_jump (label4));
4713 emit_barrier ();
4714 emit_label (label2);
4715 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4716 compute_mode, label3);
4717 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4718 quotient, 0, OPTAB_LIB_WIDEN);
4719 if (tem != quotient)
4720 emit_move_insn (quotient, tem);
4721 emit_jump_insn (gen_jump (label5));
4722 emit_barrier ();
4723 emit_label (label3);
4724 expand_inc (adjusted_op0, const1_rtx);
4725 emit_label (label4);
4726 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4727 quotient, 0, OPTAB_LIB_WIDEN);
4728 if (tem != quotient)
4729 emit_move_insn (quotient, tem);
4730 expand_inc (quotient, const1_rtx);
4731 emit_label (label5);
4734 break;
4736 case EXACT_DIV_EXPR:
4737 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4739 HOST_WIDE_INT d = INTVAL (op1);
4740 unsigned HOST_WIDE_INT ml;
4741 int pre_shift;
4742 rtx t1;
4744 pre_shift = floor_log2 (d & -d);
4745 ml = invert_mod2n (d >> pre_shift, size);
4746 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4747 pre_shift, NULL_RTX, unsignedp);
4748 quotient = expand_mult (compute_mode, t1,
4749 gen_int_mode (ml, compute_mode),
4750 NULL_RTX, 1);
4752 insn = get_last_insn ();
4753 set_dst_reg_note (insn, REG_EQUAL,
4754 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4755 compute_mode, op0, op1),
4756 quotient);
4758 break;
4760 case ROUND_DIV_EXPR:
4761 case ROUND_MOD_EXPR:
4762 if (unsignedp)
4764 rtx tem;
4765 rtx label;
4766 label = gen_label_rtx ();
4767 quotient = gen_reg_rtx (compute_mode);
4768 remainder = gen_reg_rtx (compute_mode);
4769 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4771 rtx tem;
4772 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
4773 quotient, 1, OPTAB_LIB_WIDEN);
4774 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
4775 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4776 remainder, 1, OPTAB_LIB_WIDEN);
4778 tem = plus_constant (compute_mode, op1, -1);
4779 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem, 1, NULL_RTX, 1);
4780 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
4781 expand_inc (quotient, const1_rtx);
4782 expand_dec (remainder, op1);
4783 emit_label (label);
4785 else
4787 rtx abs_rem, abs_op1, tem, mask;
4788 rtx label;
4789 label = gen_label_rtx ();
4790 quotient = gen_reg_rtx (compute_mode);
4791 remainder = gen_reg_rtx (compute_mode);
4792 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
4794 rtx tem;
4795 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
4796 quotient, 0, OPTAB_LIB_WIDEN);
4797 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
4798 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4799 remainder, 0, OPTAB_LIB_WIDEN);
4801 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
4802 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
4803 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
4804 1, NULL_RTX, 1);
4805 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
4806 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4807 NULL_RTX, 0, OPTAB_WIDEN);
4808 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4809 size - 1, NULL_RTX, 0);
4810 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
4811 NULL_RTX, 0, OPTAB_WIDEN);
4812 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4813 NULL_RTX, 0, OPTAB_WIDEN);
4814 expand_inc (quotient, tem);
4815 tem = expand_binop (compute_mode, xor_optab, mask, op1,
4816 NULL_RTX, 0, OPTAB_WIDEN);
4817 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4818 NULL_RTX, 0, OPTAB_WIDEN);
4819 expand_dec (remainder, tem);
4820 emit_label (label);
4822 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4824 default:
4825 gcc_unreachable ();
4828 if (quotient == 0)
4830 if (target && GET_MODE (target) != compute_mode)
4831 target = 0;
4833 if (rem_flag)
4835 /* Try to produce the remainder without producing the quotient.
4836 If we seem to have a divmod pattern that does not require widening,
4837 don't try widening here. We should really have a WIDEN argument
4838 to expand_twoval_binop, since what we'd really like to do here is
4839 1) try a mod insn in compute_mode
4840 2) try a divmod insn in compute_mode
4841 3) try a div insn in compute_mode and multiply-subtract to get
4842 remainder
4843 4) try the same things with widening allowed. */
4844 remainder
4845 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4846 op0, op1, target,
4847 unsignedp,
4848 ((optab_handler (optab2, compute_mode)
4849 != CODE_FOR_nothing)
4850 ? OPTAB_DIRECT : OPTAB_WIDEN));
4851 if (remainder == 0)
4853 /* No luck there. Can we do remainder and divide at once
4854 without a library call? */
4855 remainder = gen_reg_rtx (compute_mode);
4856 if (! expand_twoval_binop ((unsignedp
4857 ? udivmod_optab
4858 : sdivmod_optab),
4859 op0, op1,
4860 NULL_RTX, remainder, unsignedp))
4861 remainder = 0;
4864 if (remainder)
4865 return gen_lowpart (mode, remainder);
4868 /* Produce the quotient. Try a quotient insn, but not a library call.
4869 If we have a divmod in this mode, use it in preference to widening
4870 the div (for this test we assume it will not fail). Note that optab2
4871 is set to the one of the two optabs that the call below will use. */
4872 quotient
4873 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
4874 op0, op1, rem_flag ? NULL_RTX : target,
4875 unsignedp,
4876 ((optab_handler (optab2, compute_mode)
4877 != CODE_FOR_nothing)
4878 ? OPTAB_DIRECT : OPTAB_WIDEN));
4880 if (quotient == 0)
4882 /* No luck there. Try a quotient-and-remainder insn,
4883 keeping the quotient alone. */
4884 quotient = gen_reg_rtx (compute_mode);
4885 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
4886 op0, op1,
4887 quotient, NULL_RTX, unsignedp))
4889 quotient = 0;
4890 if (! rem_flag)
4891 /* Still no luck. If we are not computing the remainder,
4892 use a library call for the quotient. */
4893 quotient = sign_expand_binop (compute_mode,
4894 udiv_optab, sdiv_optab,
4895 op0, op1, target,
4896 unsignedp, OPTAB_LIB_WIDEN);
4901 if (rem_flag)
4903 if (target && GET_MODE (target) != compute_mode)
4904 target = 0;
4906 if (quotient == 0)
4908 /* No divide instruction either. Use library for remainder. */
4909 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4910 op0, op1, target,
4911 unsignedp, OPTAB_LIB_WIDEN);
4912 /* No remainder function. Try a quotient-and-remainder
4913 function, keeping the remainder. */
4914 if (!remainder)
4916 remainder = gen_reg_rtx (compute_mode);
4917 if (!expand_twoval_binop_libfunc
4918 (unsignedp ? udivmod_optab : sdivmod_optab,
4919 op0, op1,
4920 NULL_RTX, remainder,
4921 unsignedp ? UMOD : MOD))
4922 remainder = NULL_RTX;
4925 else
4927 /* We divided. Now finish doing X - Y * (X / Y). */
4928 remainder = expand_mult (compute_mode, quotient, op1,
4929 NULL_RTX, unsignedp);
4930 remainder = expand_binop (compute_mode, sub_optab, op0,
4931 remainder, target, unsignedp,
4932 OPTAB_LIB_WIDEN);
4936 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4939 /* Return a tree node with data type TYPE, describing the value of X.
4940 Usually this is an VAR_DECL, if there is no obvious better choice.
4941 X may be an expression, however we only support those expressions
4942 generated by loop.c. */
4944 tree
4945 make_tree (tree type, rtx x)
4947 tree t;
4949 switch (GET_CODE (x))
4951 case CONST_INT:
4953 HOST_WIDE_INT hi = 0;
4955 if (INTVAL (x) < 0
4956 && !(TYPE_UNSIGNED (type)
4957 && (GET_MODE_BITSIZE (TYPE_MODE (type))
4958 < HOST_BITS_PER_WIDE_INT)))
4959 hi = -1;
4961 t = build_int_cst_wide (type, INTVAL (x), hi);
4963 return t;
4966 case CONST_DOUBLE:
4967 if (GET_MODE (x) == VOIDmode)
4968 t = build_int_cst_wide (type,
4969 CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
4970 else
4972 REAL_VALUE_TYPE d;
4974 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
4975 t = build_real (type, d);
4978 return t;
4980 case CONST_VECTOR:
4982 int units = CONST_VECTOR_NUNITS (x);
4983 tree itype = TREE_TYPE (type);
4984 tree *elts;
4985 int i;
4987 /* Build a tree with vector elements. */
4988 elts = XALLOCAVEC (tree, units);
4989 for (i = units - 1; i >= 0; --i)
4991 rtx elt = CONST_VECTOR_ELT (x, i);
4992 elts[i] = make_tree (itype, elt);
4995 return build_vector (type, elts);
4998 case PLUS:
4999 return fold_build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5000 make_tree (type, XEXP (x, 1)));
5002 case MINUS:
5003 return fold_build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5004 make_tree (type, XEXP (x, 1)));
5006 case NEG:
5007 return fold_build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0)));
5009 case MULT:
5010 return fold_build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
5011 make_tree (type, XEXP (x, 1)));
5013 case ASHIFT:
5014 return fold_build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
5015 make_tree (type, XEXP (x, 1)));
5017 case LSHIFTRT:
5018 t = unsigned_type_for (type);
5019 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5020 make_tree (t, XEXP (x, 0)),
5021 make_tree (type, XEXP (x, 1))));
5023 case ASHIFTRT:
5024 t = signed_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 DIV:
5030 if (TREE_CODE (type) != REAL_TYPE)
5031 t = signed_type_for (type);
5032 else
5033 t = type;
5035 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5036 make_tree (t, XEXP (x, 0)),
5037 make_tree (t, XEXP (x, 1))));
5038 case UDIV:
5039 t = unsigned_type_for (type);
5040 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5041 make_tree (t, XEXP (x, 0)),
5042 make_tree (t, XEXP (x, 1))));
5044 case SIGN_EXTEND:
5045 case ZERO_EXTEND:
5046 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
5047 GET_CODE (x) == ZERO_EXTEND);
5048 return fold_convert (type, make_tree (t, XEXP (x, 0)));
5050 case CONST:
5051 return make_tree (type, XEXP (x, 0));
5053 case SYMBOL_REF:
5054 t = SYMBOL_REF_DECL (x);
5055 if (t)
5056 return fold_convert (type, build_fold_addr_expr (t));
5057 /* else fall through. */
5059 default:
5060 t = build_decl (RTL_LOCATION (x), VAR_DECL, NULL_TREE, type);
5062 /* If TYPE is a POINTER_TYPE, we might need to convert X from
5063 address mode to pointer mode. */
5064 if (POINTER_TYPE_P (type))
5065 x = convert_memory_address_addr_space
5066 (TYPE_MODE (type), x, TYPE_ADDR_SPACE (TREE_TYPE (type)));
5068 /* Note that we do *not* use SET_DECL_RTL here, because we do not
5069 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
5070 t->decl_with_rtl.rtl = x;
5072 return t;
5076 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5077 and returning TARGET.
5079 If TARGET is 0, a pseudo-register or constant is returned. */
5082 expand_and (enum machine_mode mode, rtx op0, rtx op1, rtx target)
5084 rtx tem = 0;
5086 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
5087 tem = simplify_binary_operation (AND, mode, op0, op1);
5088 if (tem == 0)
5089 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
5091 if (target == 0)
5092 target = tem;
5093 else if (tem != target)
5094 emit_move_insn (target, tem);
5095 return target;
5098 /* Helper function for emit_store_flag. */
5099 static rtx
5100 emit_cstore (rtx target, enum insn_code icode, enum rtx_code code,
5101 enum machine_mode mode, enum machine_mode compare_mode,
5102 int unsignedp, rtx x, rtx y, int normalizep,
5103 enum machine_mode target_mode)
5105 struct expand_operand ops[4];
5106 rtx op0, last, comparison, subtarget;
5107 enum machine_mode result_mode = targetm.cstore_mode (icode);
5109 last = get_last_insn ();
5110 x = prepare_operand (icode, x, 2, mode, compare_mode, unsignedp);
5111 y = prepare_operand (icode, y, 3, mode, compare_mode, unsignedp);
5112 if (!x || !y)
5114 delete_insns_since (last);
5115 return NULL_RTX;
5118 if (target_mode == VOIDmode)
5119 target_mode = result_mode;
5120 if (!target)
5121 target = gen_reg_rtx (target_mode);
5123 comparison = gen_rtx_fmt_ee (code, result_mode, x, y);
5125 create_output_operand (&ops[0], optimize ? NULL_RTX : target, result_mode);
5126 create_fixed_operand (&ops[1], comparison);
5127 create_fixed_operand (&ops[2], x);
5128 create_fixed_operand (&ops[3], y);
5129 if (!maybe_expand_insn (icode, 4, ops))
5131 delete_insns_since (last);
5132 return NULL_RTX;
5134 subtarget = ops[0].value;
5136 /* If we are converting to a wider mode, first convert to
5137 TARGET_MODE, then normalize. This produces better combining
5138 opportunities on machines that have a SIGN_EXTRACT when we are
5139 testing a single bit. This mostly benefits the 68k.
5141 If STORE_FLAG_VALUE does not have the sign bit set when
5142 interpreted in MODE, we can do this conversion as unsigned, which
5143 is usually more efficient. */
5144 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (result_mode))
5146 convert_move (target, subtarget,
5147 val_signbit_known_clear_p (result_mode,
5148 STORE_FLAG_VALUE));
5149 op0 = target;
5150 result_mode = target_mode;
5152 else
5153 op0 = subtarget;
5155 /* If we want to keep subexpressions around, don't reuse our last
5156 target. */
5157 if (optimize)
5158 subtarget = 0;
5160 /* Now normalize to the proper value in MODE. Sometimes we don't
5161 have to do anything. */
5162 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
5164 /* STORE_FLAG_VALUE might be the most negative number, so write
5165 the comparison this way to avoid a compiler-time warning. */
5166 else if (- normalizep == STORE_FLAG_VALUE)
5167 op0 = expand_unop (result_mode, neg_optab, op0, subtarget, 0);
5169 /* We don't want to use STORE_FLAG_VALUE < 0 below since this makes
5170 it hard to use a value of just the sign bit due to ANSI integer
5171 constant typing rules. */
5172 else if (val_signbit_known_set_p (result_mode, STORE_FLAG_VALUE))
5173 op0 = expand_shift (RSHIFT_EXPR, result_mode, op0,
5174 GET_MODE_BITSIZE (result_mode) - 1, subtarget,
5175 normalizep == 1);
5176 else
5178 gcc_assert (STORE_FLAG_VALUE & 1);
5180 op0 = expand_and (result_mode, op0, const1_rtx, subtarget);
5181 if (normalizep == -1)
5182 op0 = expand_unop (result_mode, neg_optab, op0, op0, 0);
5185 /* If we were converting to a smaller mode, do the conversion now. */
5186 if (target_mode != result_mode)
5188 convert_move (target, op0, 0);
5189 return target;
5191 else
5192 return op0;
5196 /* A subroutine of emit_store_flag only including "tricks" that do not
5197 need a recursive call. These are kept separate to avoid infinite
5198 loops. */
5200 static rtx
5201 emit_store_flag_1 (rtx target, enum rtx_code code, rtx op0, rtx op1,
5202 enum machine_mode mode, int unsignedp, int normalizep,
5203 enum machine_mode target_mode)
5205 rtx subtarget;
5206 enum insn_code icode;
5207 enum machine_mode compare_mode;
5208 enum mode_class mclass;
5209 enum rtx_code scode;
5210 rtx tem;
5212 if (unsignedp)
5213 code = unsigned_condition (code);
5214 scode = swap_condition (code);
5216 /* If one operand is constant, make it the second one. Only do this
5217 if the other operand is not constant as well. */
5219 if (swap_commutative_operands_p (op0, op1))
5221 tem = op0;
5222 op0 = op1;
5223 op1 = tem;
5224 code = swap_condition (code);
5227 if (mode == VOIDmode)
5228 mode = GET_MODE (op0);
5230 /* For some comparisons with 1 and -1, we can convert this to
5231 comparisons with zero. This will often produce more opportunities for
5232 store-flag insns. */
5234 switch (code)
5236 case LT:
5237 if (op1 == const1_rtx)
5238 op1 = const0_rtx, code = LE;
5239 break;
5240 case LE:
5241 if (op1 == constm1_rtx)
5242 op1 = const0_rtx, code = LT;
5243 break;
5244 case GE:
5245 if (op1 == const1_rtx)
5246 op1 = const0_rtx, code = GT;
5247 break;
5248 case GT:
5249 if (op1 == constm1_rtx)
5250 op1 = const0_rtx, code = GE;
5251 break;
5252 case GEU:
5253 if (op1 == const1_rtx)
5254 op1 = const0_rtx, code = NE;
5255 break;
5256 case LTU:
5257 if (op1 == const1_rtx)
5258 op1 = const0_rtx, code = EQ;
5259 break;
5260 default:
5261 break;
5264 /* If we are comparing a double-word integer with zero or -1, we can
5265 convert the comparison into one involving a single word. */
5266 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
5267 && GET_MODE_CLASS (mode) == MODE_INT
5268 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
5270 if ((code == EQ || code == NE)
5271 && (op1 == const0_rtx || op1 == constm1_rtx))
5273 rtx op00, op01;
5275 /* Do a logical OR or AND of the two words and compare the
5276 result. */
5277 op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
5278 op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
5279 tem = expand_binop (word_mode,
5280 op1 == const0_rtx ? ior_optab : and_optab,
5281 op00, op01, NULL_RTX, unsignedp,
5282 OPTAB_DIRECT);
5284 if (tem != 0)
5285 tem = emit_store_flag (NULL_RTX, code, tem, op1, word_mode,
5286 unsignedp, normalizep);
5288 else if ((code == LT || code == GE) && op1 == const0_rtx)
5290 rtx op0h;
5292 /* If testing the sign bit, can just test on high word. */
5293 op0h = simplify_gen_subreg (word_mode, op0, mode,
5294 subreg_highpart_offset (word_mode,
5295 mode));
5296 tem = emit_store_flag (NULL_RTX, code, op0h, op1, word_mode,
5297 unsignedp, normalizep);
5299 else
5300 tem = NULL_RTX;
5302 if (tem)
5304 if (target_mode == VOIDmode || GET_MODE (tem) == target_mode)
5305 return tem;
5306 if (!target)
5307 target = gen_reg_rtx (target_mode);
5309 convert_move (target, tem,
5310 !val_signbit_known_set_p (word_mode,
5311 (normalizep ? normalizep
5312 : STORE_FLAG_VALUE)));
5313 return target;
5317 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5318 complement of A (for GE) and shifting the sign bit to the low bit. */
5319 if (op1 == const0_rtx && (code == LT || code == GE)
5320 && GET_MODE_CLASS (mode) == MODE_INT
5321 && (normalizep || STORE_FLAG_VALUE == 1
5322 || val_signbit_p (mode, STORE_FLAG_VALUE)))
5324 subtarget = target;
5326 if (!target)
5327 target_mode = mode;
5329 /* If the result is to be wider than OP0, it is best to convert it
5330 first. If it is to be narrower, it is *incorrect* to convert it
5331 first. */
5332 else if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
5334 op0 = convert_modes (target_mode, mode, op0, 0);
5335 mode = target_mode;
5338 if (target_mode != mode)
5339 subtarget = 0;
5341 if (code == GE)
5342 op0 = expand_unop (mode, one_cmpl_optab, op0,
5343 ((STORE_FLAG_VALUE == 1 || normalizep)
5344 ? 0 : subtarget), 0);
5346 if (STORE_FLAG_VALUE == 1 || normalizep)
5347 /* If we are supposed to produce a 0/1 value, we want to do
5348 a logical shift from the sign bit to the low-order bit; for
5349 a -1/0 value, we do an arithmetic shift. */
5350 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
5351 GET_MODE_BITSIZE (mode) - 1,
5352 subtarget, normalizep != -1);
5354 if (mode != target_mode)
5355 op0 = convert_modes (target_mode, mode, op0, 0);
5357 return op0;
5360 mclass = GET_MODE_CLASS (mode);
5361 for (compare_mode = mode; compare_mode != VOIDmode;
5362 compare_mode = GET_MODE_WIDER_MODE (compare_mode))
5364 enum machine_mode optab_mode = mclass == MODE_CC ? CCmode : compare_mode;
5365 icode = optab_handler (cstore_optab, optab_mode);
5366 if (icode != CODE_FOR_nothing)
5368 do_pending_stack_adjust ();
5369 tem = emit_cstore (target, icode, code, mode, compare_mode,
5370 unsignedp, op0, op1, normalizep, target_mode);
5371 if (tem)
5372 return tem;
5374 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5376 tem = emit_cstore (target, icode, scode, mode, compare_mode,
5377 unsignedp, op1, op0, normalizep, target_mode);
5378 if (tem)
5379 return tem;
5381 break;
5385 return 0;
5388 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5389 and storing in TARGET. Normally return TARGET.
5390 Return 0 if that cannot be done.
5392 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5393 it is VOIDmode, they cannot both be CONST_INT.
5395 UNSIGNEDP is for the case where we have to widen the operands
5396 to perform the operation. It says to use zero-extension.
5398 NORMALIZEP is 1 if we should convert the result to be either zero
5399 or one. Normalize is -1 if we should convert the result to be
5400 either zero or -1. If NORMALIZEP is zero, the result will be left
5401 "raw" out of the scc insn. */
5404 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
5405 enum machine_mode mode, int unsignedp, int normalizep)
5407 enum machine_mode target_mode = target ? GET_MODE (target) : VOIDmode;
5408 enum rtx_code rcode;
5409 rtx subtarget;
5410 rtx tem, last, trueval;
5412 tem = emit_store_flag_1 (target, code, op0, op1, mode, unsignedp, normalizep,
5413 target_mode);
5414 if (tem)
5415 return tem;
5417 /* If we reached here, we can't do this with a scc insn, however there
5418 are some comparisons that can be done in other ways. Don't do any
5419 of these cases if branches are very cheap. */
5420 if (BRANCH_COST (optimize_insn_for_speed_p (), false) == 0)
5421 return 0;
5423 /* See what we need to return. We can only return a 1, -1, or the
5424 sign bit. */
5426 if (normalizep == 0)
5428 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5429 normalizep = STORE_FLAG_VALUE;
5431 else if (val_signbit_p (mode, STORE_FLAG_VALUE))
5433 else
5434 return 0;
5437 last = get_last_insn ();
5439 /* If optimizing, use different pseudo registers for each insn, instead
5440 of reusing the same pseudo. This leads to better CSE, but slows
5441 down the compiler, since there are more pseudos */
5442 subtarget = (!optimize
5443 && (target_mode == mode)) ? target : NULL_RTX;
5444 trueval = GEN_INT (normalizep ? normalizep : STORE_FLAG_VALUE);
5446 /* For floating-point comparisons, try the reverse comparison or try
5447 changing the "orderedness" of the comparison. */
5448 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5450 enum rtx_code first_code;
5451 bool and_them;
5453 rcode = reverse_condition_maybe_unordered (code);
5454 if (can_compare_p (rcode, mode, ccp_store_flag)
5455 && (code == ORDERED || code == UNORDERED
5456 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
5457 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
5459 int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
5460 || (STORE_FLAG_VALUE == -1 && normalizep == 1));
5462 /* For the reverse comparison, use either an addition or a XOR. */
5463 if (want_add
5464 && rtx_cost (GEN_INT (normalizep), PLUS, 1,
5465 optimize_insn_for_speed_p ()) == 0)
5467 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5468 STORE_FLAG_VALUE, target_mode);
5469 if (tem)
5470 return expand_binop (target_mode, add_optab, tem,
5471 GEN_INT (normalizep),
5472 target, 0, OPTAB_WIDEN);
5474 else if (!want_add
5475 && rtx_cost (trueval, XOR, 1,
5476 optimize_insn_for_speed_p ()) == 0)
5478 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5479 normalizep, target_mode);
5480 if (tem)
5481 return expand_binop (target_mode, xor_optab, tem, trueval,
5482 target, INTVAL (trueval) >= 0, OPTAB_WIDEN);
5486 delete_insns_since (last);
5488 /* Cannot split ORDERED and UNORDERED, only try the above trick. */
5489 if (code == ORDERED || code == UNORDERED)
5490 return 0;
5492 and_them = split_comparison (code, mode, &first_code, &code);
5494 /* If there are no NaNs, the first comparison should always fall through.
5495 Effectively change the comparison to the other one. */
5496 if (!HONOR_NANS (mode))
5498 gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
5499 return emit_store_flag_1 (target, code, op0, op1, mode, 0, normalizep,
5500 target_mode);
5503 #ifdef HAVE_conditional_move
5504 /* Try using a setcc instruction for ORDERED/UNORDERED, followed by a
5505 conditional move. */
5506 tem = emit_store_flag_1 (subtarget, first_code, op0, op1, mode, 0,
5507 normalizep, target_mode);
5508 if (tem == 0)
5509 return 0;
5511 if (and_them)
5512 tem = emit_conditional_move (target, code, op0, op1, mode,
5513 tem, const0_rtx, GET_MODE (tem), 0);
5514 else
5515 tem = emit_conditional_move (target, code, op0, op1, mode,
5516 trueval, tem, GET_MODE (tem), 0);
5518 if (tem == 0)
5519 delete_insns_since (last);
5520 return tem;
5521 #else
5522 return 0;
5523 #endif
5526 /* The remaining tricks only apply to integer comparisons. */
5528 if (GET_MODE_CLASS (mode) != MODE_INT)
5529 return 0;
5531 /* If this is an equality comparison of integers, we can try to exclusive-or
5532 (or subtract) the two operands and use a recursive call to try the
5533 comparison with zero. Don't do any of these cases if branches are
5534 very cheap. */
5536 if ((code == EQ || code == NE) && op1 != const0_rtx)
5538 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5539 OPTAB_WIDEN);
5541 if (tem == 0)
5542 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5543 OPTAB_WIDEN);
5544 if (tem != 0)
5545 tem = emit_store_flag (target, code, tem, const0_rtx,
5546 mode, unsignedp, normalizep);
5547 if (tem != 0)
5548 return tem;
5550 delete_insns_since (last);
5553 /* For integer comparisons, try the reverse comparison. However, for
5554 small X and if we'd have anyway to extend, implementing "X != 0"
5555 as "-(int)X >> 31" is still cheaper than inverting "(int)X == 0". */
5556 rcode = reverse_condition (code);
5557 if (can_compare_p (rcode, mode, ccp_store_flag)
5558 && ! (optab_handler (cstore_optab, mode) == CODE_FOR_nothing
5559 && code == NE
5560 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
5561 && op1 == const0_rtx))
5563 int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
5564 || (STORE_FLAG_VALUE == -1 && normalizep == 1));
5566 /* Again, for the reverse comparison, use either an addition or a XOR. */
5567 if (want_add
5568 && rtx_cost (GEN_INT (normalizep), PLUS, 1,
5569 optimize_insn_for_speed_p ()) == 0)
5571 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5572 STORE_FLAG_VALUE, target_mode);
5573 if (tem != 0)
5574 tem = expand_binop (target_mode, add_optab, tem,
5575 GEN_INT (normalizep), target, 0, OPTAB_WIDEN);
5577 else if (!want_add
5578 && rtx_cost (trueval, XOR, 1,
5579 optimize_insn_for_speed_p ()) == 0)
5581 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5582 normalizep, target_mode);
5583 if (tem != 0)
5584 tem = expand_binop (target_mode, xor_optab, tem, trueval, target,
5585 INTVAL (trueval) >= 0, OPTAB_WIDEN);
5588 if (tem != 0)
5589 return tem;
5590 delete_insns_since (last);
5593 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5594 the constant zero. Reject all other comparisons at this point. Only
5595 do LE and GT if branches are expensive since they are expensive on
5596 2-operand machines. */
5598 if (op1 != const0_rtx
5599 || (code != EQ && code != NE
5600 && (BRANCH_COST (optimize_insn_for_speed_p (),
5601 false) <= 1 || (code != LE && code != GT))))
5602 return 0;
5604 /* Try to put the result of the comparison in the sign bit. Assume we can't
5605 do the necessary operation below. */
5607 tem = 0;
5609 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5610 the sign bit set. */
5612 if (code == LE)
5614 /* This is destructive, so SUBTARGET can't be OP0. */
5615 if (rtx_equal_p (subtarget, op0))
5616 subtarget = 0;
5618 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5619 OPTAB_WIDEN);
5620 if (tem)
5621 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5622 OPTAB_WIDEN);
5625 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5626 number of bits in the mode of OP0, minus one. */
5628 if (code == GT)
5630 if (rtx_equal_p (subtarget, op0))
5631 subtarget = 0;
5633 tem = expand_shift (RSHIFT_EXPR, mode, op0,
5634 GET_MODE_BITSIZE (mode) - 1,
5635 subtarget, 0);
5636 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5637 OPTAB_WIDEN);
5640 if (code == EQ || code == NE)
5642 /* For EQ or NE, one way to do the comparison is to apply an operation
5643 that converts the operand into a positive number if it is nonzero
5644 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5645 for NE we negate. This puts the result in the sign bit. Then we
5646 normalize with a shift, if needed.
5648 Two operations that can do the above actions are ABS and FFS, so try
5649 them. If that doesn't work, and MODE is smaller than a full word,
5650 we can use zero-extension to the wider mode (an unsigned conversion)
5651 as the operation. */
5653 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5654 that is compensated by the subsequent overflow when subtracting
5655 one / negating. */
5657 if (optab_handler (abs_optab, mode) != CODE_FOR_nothing)
5658 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5659 else if (optab_handler (ffs_optab, mode) != CODE_FOR_nothing)
5660 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5661 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5663 tem = convert_modes (word_mode, mode, op0, 1);
5664 mode = word_mode;
5667 if (tem != 0)
5669 if (code == EQ)
5670 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5671 0, OPTAB_WIDEN);
5672 else
5673 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5676 /* If we couldn't do it that way, for NE we can "or" the two's complement
5677 of the value with itself. For EQ, we take the one's complement of
5678 that "or", which is an extra insn, so we only handle EQ if branches
5679 are expensive. */
5681 if (tem == 0
5682 && (code == NE
5683 || BRANCH_COST (optimize_insn_for_speed_p (),
5684 false) > 1))
5686 if (rtx_equal_p (subtarget, op0))
5687 subtarget = 0;
5689 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5690 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5691 OPTAB_WIDEN);
5693 if (tem && code == EQ)
5694 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5698 if (tem && normalizep)
5699 tem = expand_shift (RSHIFT_EXPR, mode, tem,
5700 GET_MODE_BITSIZE (mode) - 1,
5701 subtarget, normalizep == 1);
5703 if (tem)
5705 if (!target)
5707 else if (GET_MODE (tem) != target_mode)
5709 convert_move (target, tem, 0);
5710 tem = target;
5712 else if (!subtarget)
5714 emit_move_insn (target, tem);
5715 tem = target;
5718 else
5719 delete_insns_since (last);
5721 return tem;
5724 /* Like emit_store_flag, but always succeeds. */
5727 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5728 enum machine_mode mode, int unsignedp, int normalizep)
5730 rtx tem, label;
5731 rtx trueval, falseval;
5733 /* First see if emit_store_flag can do the job. */
5734 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5735 if (tem != 0)
5736 return tem;
5738 if (!target)
5739 target = gen_reg_rtx (word_mode);
5741 /* If this failed, we have to do this with set/compare/jump/set code.
5742 For foo != 0, if foo is in OP0, just replace it with 1 if nonzero. */
5743 trueval = normalizep ? GEN_INT (normalizep) : const1_rtx;
5744 if (code == NE
5745 && GET_MODE_CLASS (mode) == MODE_INT
5746 && REG_P (target)
5747 && op0 == target
5748 && op1 == const0_rtx)
5750 label = gen_label_rtx ();
5751 do_compare_rtx_and_jump (target, const0_rtx, EQ, unsignedp,
5752 mode, NULL_RTX, NULL_RTX, label, -1);
5753 emit_move_insn (target, trueval);
5754 emit_label (label);
5755 return target;
5758 if (!REG_P (target)
5759 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5760 target = gen_reg_rtx (GET_MODE (target));
5762 /* Jump in the right direction if the target cannot implement CODE
5763 but can jump on its reverse condition. */
5764 falseval = const0_rtx;
5765 if (! can_compare_p (code, mode, ccp_jump)
5766 && (! FLOAT_MODE_P (mode)
5767 || code == ORDERED || code == UNORDERED
5768 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
5769 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
5771 enum rtx_code rcode;
5772 if (FLOAT_MODE_P (mode))
5773 rcode = reverse_condition_maybe_unordered (code);
5774 else
5775 rcode = reverse_condition (code);
5777 /* Canonicalize to UNORDERED for the libcall. */
5778 if (can_compare_p (rcode, mode, ccp_jump)
5779 || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
5781 falseval = trueval;
5782 trueval = const0_rtx;
5783 code = rcode;
5787 emit_move_insn (target, trueval);
5788 label = gen_label_rtx ();
5789 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
5790 NULL_RTX, label, -1);
5792 emit_move_insn (target, falseval);
5793 emit_label (label);
5795 return target;
5798 /* Perform possibly multi-word comparison and conditional jump to LABEL
5799 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE. This is
5800 now a thin wrapper around do_compare_rtx_and_jump. */
5802 static void
5803 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, enum machine_mode mode,
5804 rtx label)
5806 int unsignedp = (op == LTU || op == LEU || op == GTU || op == GEU);
5807 do_compare_rtx_and_jump (arg1, arg2, op, unsignedp, mode,
5808 NULL_RTX, NULL_RTX, label, -1);