Daily bump.
[official-gcc.git] / gcc / expmed.c
blob57d476eb12b5d4e20c9fa9295974cad8f5478eea
1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987-2014 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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 "stor-layout.h"
30 #include "tm_p.h"
31 #include "flags.h"
32 #include "insn-config.h"
33 #include "expr.h"
34 #include "optabs.h"
35 #include "recog.h"
36 #include "langhooks.h"
37 #include "df.h"
38 #include "target.h"
39 #include "expmed.h"
41 struct target_expmed default_target_expmed;
42 #if SWITCHABLE_TARGET
43 struct target_expmed *this_target_expmed = &default_target_expmed;
44 #endif
46 static void store_fixed_bit_field (rtx, unsigned HOST_WIDE_INT,
47 unsigned HOST_WIDE_INT,
48 unsigned HOST_WIDE_INT,
49 unsigned HOST_WIDE_INT,
50 rtx);
51 static void store_fixed_bit_field_1 (rtx, unsigned HOST_WIDE_INT,
52 unsigned HOST_WIDE_INT,
53 rtx);
54 static void store_split_bit_field (rtx, unsigned HOST_WIDE_INT,
55 unsigned HOST_WIDE_INT,
56 unsigned HOST_WIDE_INT,
57 unsigned HOST_WIDE_INT,
58 rtx);
59 static rtx extract_fixed_bit_field (enum machine_mode, rtx,
60 unsigned HOST_WIDE_INT,
61 unsigned HOST_WIDE_INT, rtx, int);
62 static rtx extract_fixed_bit_field_1 (enum machine_mode, rtx,
63 unsigned HOST_WIDE_INT,
64 unsigned HOST_WIDE_INT, rtx, int);
65 static rtx mask_rtx (enum machine_mode, int, int, int);
66 static rtx lshift_value (enum machine_mode, unsigned HOST_WIDE_INT, int);
67 static rtx extract_split_bit_field (rtx, unsigned HOST_WIDE_INT,
68 unsigned HOST_WIDE_INT, int);
69 static void do_cmp_and_jump (rtx, rtx, enum rtx_code, enum machine_mode, rtx);
70 static rtx expand_smod_pow2 (enum machine_mode, rtx, HOST_WIDE_INT);
71 static rtx expand_sdiv_pow2 (enum machine_mode, rtx, HOST_WIDE_INT);
73 /* Test whether a value is zero of a power of two. */
74 #define EXACT_POWER_OF_2_OR_ZERO_P(x) \
75 (((x) & ((x) - (unsigned HOST_WIDE_INT) 1)) == 0)
77 struct init_expmed_rtl
79 struct rtx_def reg;
80 struct rtx_def plus;
81 struct rtx_def neg;
82 struct rtx_def mult;
83 struct rtx_def sdiv;
84 struct rtx_def udiv;
85 struct rtx_def sdiv_32;
86 struct rtx_def smod_32;
87 struct rtx_def wide_mult;
88 struct rtx_def wide_lshr;
89 struct rtx_def wide_trunc;
90 struct rtx_def shift;
91 struct rtx_def shift_mult;
92 struct rtx_def shift_add;
93 struct rtx_def shift_sub0;
94 struct rtx_def shift_sub1;
95 struct rtx_def zext;
96 struct rtx_def trunc;
98 rtx pow2[MAX_BITS_PER_WORD];
99 rtx cint[MAX_BITS_PER_WORD];
102 static void
103 init_expmed_one_conv (struct init_expmed_rtl *all, enum machine_mode to_mode,
104 enum machine_mode from_mode, bool speed)
106 int to_size, from_size;
107 rtx which;
109 /* We're given no information about the true size of a partial integer,
110 only the size of the "full" integer it requires for storage. For
111 comparison purposes here, reduce the bit size by one in that case. */
112 to_size = (GET_MODE_BITSIZE (to_mode)
113 - (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT));
114 from_size = (GET_MODE_BITSIZE (from_mode)
115 - (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT));
117 /* Assume cost of zero-extend and sign-extend is the same. */
118 which = (to_size < from_size ? &all->trunc : &all->zext);
120 PUT_MODE (&all->reg, from_mode);
121 set_convert_cost (to_mode, from_mode, speed, set_src_cost (which, speed));
124 static void
125 init_expmed_one_mode (struct init_expmed_rtl *all,
126 enum machine_mode mode, int speed)
128 int m, n, mode_bitsize;
129 enum machine_mode mode_from;
131 mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
133 PUT_MODE (&all->reg, mode);
134 PUT_MODE (&all->plus, mode);
135 PUT_MODE (&all->neg, mode);
136 PUT_MODE (&all->mult, mode);
137 PUT_MODE (&all->sdiv, mode);
138 PUT_MODE (&all->udiv, mode);
139 PUT_MODE (&all->sdiv_32, mode);
140 PUT_MODE (&all->smod_32, mode);
141 PUT_MODE (&all->wide_trunc, mode);
142 PUT_MODE (&all->shift, mode);
143 PUT_MODE (&all->shift_mult, mode);
144 PUT_MODE (&all->shift_add, mode);
145 PUT_MODE (&all->shift_sub0, mode);
146 PUT_MODE (&all->shift_sub1, mode);
147 PUT_MODE (&all->zext, mode);
148 PUT_MODE (&all->trunc, mode);
150 set_add_cost (speed, mode, set_src_cost (&all->plus, speed));
151 set_neg_cost (speed, mode, set_src_cost (&all->neg, speed));
152 set_mul_cost (speed, mode, set_src_cost (&all->mult, speed));
153 set_sdiv_cost (speed, mode, set_src_cost (&all->sdiv, speed));
154 set_udiv_cost (speed, mode, set_src_cost (&all->udiv, speed));
156 set_sdiv_pow2_cheap (speed, mode, (set_src_cost (&all->sdiv_32, speed)
157 <= 2 * add_cost (speed, mode)));
158 set_smod_pow2_cheap (speed, mode, (set_src_cost (&all->smod_32, speed)
159 <= 4 * add_cost (speed, mode)));
161 set_shift_cost (speed, mode, 0, 0);
163 int cost = add_cost (speed, mode);
164 set_shiftadd_cost (speed, mode, 0, cost);
165 set_shiftsub0_cost (speed, mode, 0, cost);
166 set_shiftsub1_cost (speed, mode, 0, cost);
169 n = MIN (MAX_BITS_PER_WORD, mode_bitsize);
170 for (m = 1; m < n; m++)
172 XEXP (&all->shift, 1) = all->cint[m];
173 XEXP (&all->shift_mult, 1) = all->pow2[m];
175 set_shift_cost (speed, mode, m, set_src_cost (&all->shift, speed));
176 set_shiftadd_cost (speed, mode, m, set_src_cost (&all->shift_add, speed));
177 set_shiftsub0_cost (speed, mode, m, set_src_cost (&all->shift_sub0, speed));
178 set_shiftsub1_cost (speed, mode, m, set_src_cost (&all->shift_sub1, speed));
181 if (SCALAR_INT_MODE_P (mode))
183 for (mode_from = MIN_MODE_INT; mode_from <= MAX_MODE_INT;
184 mode_from = (enum machine_mode)(mode_from + 1))
185 init_expmed_one_conv (all, mode, mode_from, speed);
187 if (GET_MODE_CLASS (mode) == MODE_INT)
189 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
190 if (wider_mode != VOIDmode)
192 PUT_MODE (&all->zext, wider_mode);
193 PUT_MODE (&all->wide_mult, wider_mode);
194 PUT_MODE (&all->wide_lshr, wider_mode);
195 XEXP (&all->wide_lshr, 1) = GEN_INT (mode_bitsize);
197 set_mul_widen_cost (speed, wider_mode,
198 set_src_cost (&all->wide_mult, speed));
199 set_mul_highpart_cost (speed, mode,
200 set_src_cost (&all->wide_trunc, speed));
205 void
206 init_expmed (void)
208 struct init_expmed_rtl all;
209 enum machine_mode mode;
210 int m, speed;
212 memset (&all, 0, sizeof all);
213 for (m = 1; m < MAX_BITS_PER_WORD; m++)
215 all.pow2[m] = GEN_INT ((HOST_WIDE_INT) 1 << m);
216 all.cint[m] = GEN_INT (m);
219 PUT_CODE (&all.reg, REG);
220 /* Avoid using hard regs in ways which may be unsupported. */
221 SET_REGNO (&all.reg, LAST_VIRTUAL_REGISTER + 1);
223 PUT_CODE (&all.plus, PLUS);
224 XEXP (&all.plus, 0) = &all.reg;
225 XEXP (&all.plus, 1) = &all.reg;
227 PUT_CODE (&all.neg, NEG);
228 XEXP (&all.neg, 0) = &all.reg;
230 PUT_CODE (&all.mult, MULT);
231 XEXP (&all.mult, 0) = &all.reg;
232 XEXP (&all.mult, 1) = &all.reg;
234 PUT_CODE (&all.sdiv, DIV);
235 XEXP (&all.sdiv, 0) = &all.reg;
236 XEXP (&all.sdiv, 1) = &all.reg;
238 PUT_CODE (&all.udiv, UDIV);
239 XEXP (&all.udiv, 0) = &all.reg;
240 XEXP (&all.udiv, 1) = &all.reg;
242 PUT_CODE (&all.sdiv_32, DIV);
243 XEXP (&all.sdiv_32, 0) = &all.reg;
244 XEXP (&all.sdiv_32, 1) = 32 < MAX_BITS_PER_WORD ? all.cint[32] : GEN_INT (32);
246 PUT_CODE (&all.smod_32, MOD);
247 XEXP (&all.smod_32, 0) = &all.reg;
248 XEXP (&all.smod_32, 1) = XEXP (&all.sdiv_32, 1);
250 PUT_CODE (&all.zext, ZERO_EXTEND);
251 XEXP (&all.zext, 0) = &all.reg;
253 PUT_CODE (&all.wide_mult, MULT);
254 XEXP (&all.wide_mult, 0) = &all.zext;
255 XEXP (&all.wide_mult, 1) = &all.zext;
257 PUT_CODE (&all.wide_lshr, LSHIFTRT);
258 XEXP (&all.wide_lshr, 0) = &all.wide_mult;
260 PUT_CODE (&all.wide_trunc, TRUNCATE);
261 XEXP (&all.wide_trunc, 0) = &all.wide_lshr;
263 PUT_CODE (&all.shift, ASHIFT);
264 XEXP (&all.shift, 0) = &all.reg;
266 PUT_CODE (&all.shift_mult, MULT);
267 XEXP (&all.shift_mult, 0) = &all.reg;
269 PUT_CODE (&all.shift_add, PLUS);
270 XEXP (&all.shift_add, 0) = &all.shift_mult;
271 XEXP (&all.shift_add, 1) = &all.reg;
273 PUT_CODE (&all.shift_sub0, MINUS);
274 XEXP (&all.shift_sub0, 0) = &all.shift_mult;
275 XEXP (&all.shift_sub0, 1) = &all.reg;
277 PUT_CODE (&all.shift_sub1, MINUS);
278 XEXP (&all.shift_sub1, 0) = &all.reg;
279 XEXP (&all.shift_sub1, 1) = &all.shift_mult;
281 PUT_CODE (&all.trunc, TRUNCATE);
282 XEXP (&all.trunc, 0) = &all.reg;
284 for (speed = 0; speed < 2; speed++)
286 crtl->maybe_hot_insn_p = speed;
287 set_zero_cost (speed, set_src_cost (const0_rtx, speed));
289 for (mode = MIN_MODE_INT; mode <= MAX_MODE_INT;
290 mode = (enum machine_mode)(mode + 1))
291 init_expmed_one_mode (&all, mode, speed);
293 if (MIN_MODE_PARTIAL_INT != VOIDmode)
294 for (mode = MIN_MODE_PARTIAL_INT; mode <= MAX_MODE_PARTIAL_INT;
295 mode = (enum machine_mode)(mode + 1))
296 init_expmed_one_mode (&all, mode, speed);
298 if (MIN_MODE_VECTOR_INT != VOIDmode)
299 for (mode = MIN_MODE_VECTOR_INT; mode <= MAX_MODE_VECTOR_INT;
300 mode = (enum machine_mode)(mode + 1))
301 init_expmed_one_mode (&all, mode, speed);
304 if (alg_hash_used_p ())
306 struct alg_hash_entry *p = alg_hash_entry_ptr (0);
307 memset (p, 0, sizeof (*p) * NUM_ALG_HASH_ENTRIES);
309 else
310 set_alg_hash_used_p (true);
311 default_rtl_profile ();
314 /* Return an rtx representing minus the value of X.
315 MODE is the intended mode of the result,
316 useful if X is a CONST_INT. */
319 negate_rtx (enum machine_mode mode, rtx x)
321 rtx result = simplify_unary_operation (NEG, mode, x, mode);
323 if (result == 0)
324 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
326 return result;
329 /* Adjust bitfield memory MEM so that it points to the first unit of mode
330 MODE that contains a bitfield of size BITSIZE at bit position BITNUM.
331 If MODE is BLKmode, return a reference to every byte in the bitfield.
332 Set *NEW_BITNUM to the bit position of the field within the new memory. */
334 static rtx
335 narrow_bit_field_mem (rtx mem, enum machine_mode mode,
336 unsigned HOST_WIDE_INT bitsize,
337 unsigned HOST_WIDE_INT bitnum,
338 unsigned HOST_WIDE_INT *new_bitnum)
340 if (mode == BLKmode)
342 *new_bitnum = bitnum % BITS_PER_UNIT;
343 HOST_WIDE_INT offset = bitnum / BITS_PER_UNIT;
344 HOST_WIDE_INT size = ((*new_bitnum + bitsize + BITS_PER_UNIT - 1)
345 / BITS_PER_UNIT);
346 return adjust_bitfield_address_size (mem, mode, offset, size);
348 else
350 unsigned int unit = GET_MODE_BITSIZE (mode);
351 *new_bitnum = bitnum % unit;
352 HOST_WIDE_INT offset = (bitnum - *new_bitnum) / BITS_PER_UNIT;
353 return adjust_bitfield_address (mem, mode, offset);
357 /* The caller wants to perform insertion or extraction PATTERN on a
358 bitfield of size BITSIZE at BITNUM bits into memory operand OP0.
359 BITREGION_START and BITREGION_END are as for store_bit_field
360 and FIELDMODE is the natural mode of the field.
362 Search for a mode that is compatible with the memory access
363 restrictions and (where applicable) with a register insertion or
364 extraction. Return the new memory on success, storing the adjusted
365 bit position in *NEW_BITNUM. Return null otherwise. */
367 static rtx
368 adjust_bit_field_mem_for_reg (enum extraction_pattern pattern,
369 rtx op0, HOST_WIDE_INT bitsize,
370 HOST_WIDE_INT bitnum,
371 unsigned HOST_WIDE_INT bitregion_start,
372 unsigned HOST_WIDE_INT bitregion_end,
373 enum machine_mode fieldmode,
374 unsigned HOST_WIDE_INT *new_bitnum)
376 bit_field_mode_iterator iter (bitsize, bitnum, bitregion_start,
377 bitregion_end, MEM_ALIGN (op0),
378 MEM_VOLATILE_P (op0));
379 enum machine_mode best_mode;
380 if (iter.next_mode (&best_mode))
382 /* We can use a memory in BEST_MODE. See whether this is true for
383 any wider modes. All other things being equal, we prefer to
384 use the widest mode possible because it tends to expose more
385 CSE opportunities. */
386 if (!iter.prefer_smaller_modes ())
388 /* Limit the search to the mode required by the corresponding
389 register insertion or extraction instruction, if any. */
390 enum machine_mode limit_mode = word_mode;
391 extraction_insn insn;
392 if (get_best_reg_extraction_insn (&insn, pattern,
393 GET_MODE_BITSIZE (best_mode),
394 fieldmode))
395 limit_mode = insn.field_mode;
397 enum machine_mode wider_mode;
398 while (iter.next_mode (&wider_mode)
399 && GET_MODE_SIZE (wider_mode) <= GET_MODE_SIZE (limit_mode))
400 best_mode = wider_mode;
402 return narrow_bit_field_mem (op0, best_mode, bitsize, bitnum,
403 new_bitnum);
405 return NULL_RTX;
408 /* Return true if a bitfield of size BITSIZE at bit number BITNUM within
409 a structure of mode STRUCT_MODE represents a lowpart subreg. The subreg
410 offset is then BITNUM / BITS_PER_UNIT. */
412 static bool
413 lowpart_bit_field_p (unsigned HOST_WIDE_INT bitnum,
414 unsigned HOST_WIDE_INT bitsize,
415 enum machine_mode struct_mode)
417 if (BYTES_BIG_ENDIAN)
418 return (bitnum % BITS_PER_UNIT == 0
419 && (bitnum + bitsize == GET_MODE_BITSIZE (struct_mode)
420 || (bitnum + bitsize) % BITS_PER_WORD == 0));
421 else
422 return bitnum % BITS_PER_WORD == 0;
425 /* Return true if -fstrict-volatile-bitfields applies to an access of OP0
426 containing BITSIZE bits starting at BITNUM, with field mode FIELDMODE.
427 Return false if the access would touch memory outside the range
428 BITREGION_START to BITREGION_END for conformance to the C++ memory
429 model. */
431 static bool
432 strict_volatile_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
433 unsigned HOST_WIDE_INT bitnum,
434 enum machine_mode fieldmode,
435 unsigned HOST_WIDE_INT bitregion_start,
436 unsigned HOST_WIDE_INT bitregion_end)
438 unsigned HOST_WIDE_INT modesize = GET_MODE_BITSIZE (fieldmode);
440 /* -fstrict-volatile-bitfields must be enabled and we must have a
441 volatile MEM. */
442 if (!MEM_P (op0)
443 || !MEM_VOLATILE_P (op0)
444 || flag_strict_volatile_bitfields <= 0)
445 return false;
447 /* Non-integral modes likely only happen with packed structures.
448 Punt. */
449 if (!SCALAR_INT_MODE_P (fieldmode))
450 return false;
452 /* The bit size must not be larger than the field mode, and
453 the field mode must not be larger than a word. */
454 if (bitsize > modesize || modesize > BITS_PER_WORD)
455 return false;
457 /* Check for cases of unaligned fields that must be split. */
458 if (bitnum % BITS_PER_UNIT + bitsize > modesize
459 || (STRICT_ALIGNMENT
460 && bitnum % GET_MODE_ALIGNMENT (fieldmode) + bitsize > modesize))
461 return false;
463 /* Check for cases where the C++ memory model applies. */
464 if (bitregion_end != 0
465 && (bitnum - bitnum % modesize < bitregion_start
466 || bitnum - bitnum % modesize + modesize - 1 > bitregion_end))
467 return false;
469 return true;
472 /* Return true if OP is a memory and if a bitfield of size BITSIZE at
473 bit number BITNUM can be treated as a simple value of mode MODE. */
475 static bool
476 simple_mem_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
477 unsigned HOST_WIDE_INT bitnum, enum machine_mode mode)
479 return (MEM_P (op0)
480 && bitnum % BITS_PER_UNIT == 0
481 && bitsize == GET_MODE_BITSIZE (mode)
482 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
483 || (bitnum % GET_MODE_ALIGNMENT (mode) == 0
484 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode))));
487 /* Try to use instruction INSV to store VALUE into a field of OP0.
488 BITSIZE and BITNUM are as for store_bit_field. */
490 static bool
491 store_bit_field_using_insv (const extraction_insn *insv, rtx op0,
492 unsigned HOST_WIDE_INT bitsize,
493 unsigned HOST_WIDE_INT bitnum,
494 rtx value)
496 struct expand_operand ops[4];
497 rtx value1;
498 rtx xop0 = op0;
499 rtx last = get_last_insn ();
500 bool copy_back = false;
502 enum machine_mode op_mode = insv->field_mode;
503 unsigned int unit = GET_MODE_BITSIZE (op_mode);
504 if (bitsize == 0 || bitsize > unit)
505 return false;
507 if (MEM_P (xop0))
508 /* Get a reference to the first byte of the field. */
509 xop0 = narrow_bit_field_mem (xop0, insv->struct_mode, bitsize, bitnum,
510 &bitnum);
511 else
513 /* Convert from counting within OP0 to counting in OP_MODE. */
514 if (BYTES_BIG_ENDIAN)
515 bitnum += unit - GET_MODE_BITSIZE (GET_MODE (op0));
517 /* If xop0 is a register, we need it in OP_MODE
518 to make it acceptable to the format of insv. */
519 if (GET_CODE (xop0) == SUBREG)
520 /* We can't just change the mode, because this might clobber op0,
521 and we will need the original value of op0 if insv fails. */
522 xop0 = gen_rtx_SUBREG (op_mode, SUBREG_REG (xop0), SUBREG_BYTE (xop0));
523 if (REG_P (xop0) && GET_MODE (xop0) != op_mode)
524 xop0 = gen_lowpart_SUBREG (op_mode, xop0);
527 /* If the destination is a paradoxical subreg such that we need a
528 truncate to the inner mode, perform the insertion on a temporary and
529 truncate the result to the original destination. Note that we can't
530 just truncate the paradoxical subreg as (truncate:N (subreg:W (reg:N
531 X) 0)) is (reg:N X). */
532 if (GET_CODE (xop0) == SUBREG
533 && REG_P (SUBREG_REG (xop0))
534 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (SUBREG_REG (xop0)),
535 op_mode))
537 rtx tem = gen_reg_rtx (op_mode);
538 emit_move_insn (tem, xop0);
539 xop0 = tem;
540 copy_back = true;
543 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
544 "backwards" from the size of the unit we are inserting into.
545 Otherwise, we count bits from the most significant on a
546 BYTES/BITS_BIG_ENDIAN machine. */
548 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
549 bitnum = unit - bitsize - bitnum;
551 /* Convert VALUE to op_mode (which insv insn wants) in VALUE1. */
552 value1 = value;
553 if (GET_MODE (value) != op_mode)
555 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
557 /* Optimization: Don't bother really extending VALUE
558 if it has all the bits we will actually use. However,
559 if we must narrow it, be sure we do it correctly. */
561 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (op_mode))
563 rtx tmp;
565 tmp = simplify_subreg (op_mode, value1, GET_MODE (value), 0);
566 if (! tmp)
567 tmp = simplify_gen_subreg (op_mode,
568 force_reg (GET_MODE (value),
569 value1),
570 GET_MODE (value), 0);
571 value1 = tmp;
573 else
574 value1 = gen_lowpart (op_mode, value1);
576 else if (CONST_INT_P (value))
577 value1 = gen_int_mode (INTVAL (value), op_mode);
578 else
579 /* Parse phase is supposed to make VALUE's data type
580 match that of the component reference, which is a type
581 at least as wide as the field; so VALUE should have
582 a mode that corresponds to that type. */
583 gcc_assert (CONSTANT_P (value));
586 create_fixed_operand (&ops[0], xop0);
587 create_integer_operand (&ops[1], bitsize);
588 create_integer_operand (&ops[2], bitnum);
589 create_input_operand (&ops[3], value1, op_mode);
590 if (maybe_expand_insn (insv->icode, 4, ops))
592 if (copy_back)
593 convert_move (op0, xop0, true);
594 return true;
596 delete_insns_since (last);
597 return false;
600 /* A subroutine of store_bit_field, with the same arguments. Return true
601 if the operation could be implemented.
603 If FALLBACK_P is true, fall back to store_fixed_bit_field if we have
604 no other way of implementing the operation. If FALLBACK_P is false,
605 return false instead. */
607 static bool
608 store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
609 unsigned HOST_WIDE_INT bitnum,
610 unsigned HOST_WIDE_INT bitregion_start,
611 unsigned HOST_WIDE_INT bitregion_end,
612 enum machine_mode fieldmode,
613 rtx value, bool fallback_p)
615 rtx op0 = str_rtx;
616 rtx orig_value;
618 while (GET_CODE (op0) == SUBREG)
620 /* The following line once was done only if WORDS_BIG_ENDIAN,
621 but I think that is a mistake. WORDS_BIG_ENDIAN is
622 meaningful at a much higher level; when structures are copied
623 between memory and regs, the higher-numbered regs
624 always get higher addresses. */
625 int inner_mode_size = GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)));
626 int outer_mode_size = GET_MODE_SIZE (GET_MODE (op0));
627 int byte_offset = 0;
629 /* Paradoxical subregs need special handling on big endian machines. */
630 if (SUBREG_BYTE (op0) == 0 && inner_mode_size < outer_mode_size)
632 int difference = inner_mode_size - outer_mode_size;
634 if (WORDS_BIG_ENDIAN)
635 byte_offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
636 if (BYTES_BIG_ENDIAN)
637 byte_offset += difference % UNITS_PER_WORD;
639 else
640 byte_offset = SUBREG_BYTE (op0);
642 bitnum += byte_offset * BITS_PER_UNIT;
643 op0 = SUBREG_REG (op0);
646 /* No action is needed if the target is a register and if the field
647 lies completely outside that register. This can occur if the source
648 code contains an out-of-bounds access to a small array. */
649 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
650 return true;
652 /* Use vec_set patterns for inserting parts of vectors whenever
653 available. */
654 if (VECTOR_MODE_P (GET_MODE (op0))
655 && !MEM_P (op0)
656 && optab_handler (vec_set_optab, GET_MODE (op0)) != CODE_FOR_nothing
657 && fieldmode == GET_MODE_INNER (GET_MODE (op0))
658 && bitsize == GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
659 && !(bitnum % GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
661 struct expand_operand ops[3];
662 enum machine_mode outermode = GET_MODE (op0);
663 enum machine_mode innermode = GET_MODE_INNER (outermode);
664 enum insn_code icode = optab_handler (vec_set_optab, outermode);
665 int pos = bitnum / GET_MODE_BITSIZE (innermode);
667 create_fixed_operand (&ops[0], op0);
668 create_input_operand (&ops[1], value, innermode);
669 create_integer_operand (&ops[2], pos);
670 if (maybe_expand_insn (icode, 3, ops))
671 return true;
674 /* If the target is a register, overwriting the entire object, or storing
675 a full-word or multi-word field can be done with just a SUBREG. */
676 if (!MEM_P (op0)
677 && bitsize == GET_MODE_BITSIZE (fieldmode)
678 && ((bitsize == GET_MODE_BITSIZE (GET_MODE (op0)) && bitnum == 0)
679 || (bitsize % BITS_PER_WORD == 0 && bitnum % BITS_PER_WORD == 0)))
681 /* Use the subreg machinery either to narrow OP0 to the required
682 words or to cope with mode punning between equal-sized modes.
683 In the latter case, use subreg on the rhs side, not lhs. */
684 rtx sub;
686 if (bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
688 sub = simplify_gen_subreg (GET_MODE (op0), value, fieldmode, 0);
689 if (sub)
691 emit_move_insn (op0, sub);
692 return true;
695 else
697 sub = simplify_gen_subreg (fieldmode, op0, GET_MODE (op0),
698 bitnum / BITS_PER_UNIT);
699 if (sub)
701 emit_move_insn (sub, value);
702 return true;
707 /* If the target is memory, storing any naturally aligned field can be
708 done with a simple store. For targets that support fast unaligned
709 memory, any naturally sized, unit aligned field can be done directly. */
710 if (simple_mem_bitfield_p (op0, bitsize, bitnum, fieldmode))
712 op0 = adjust_bitfield_address (op0, fieldmode, bitnum / BITS_PER_UNIT);
713 emit_move_insn (op0, value);
714 return true;
717 /* Make sure we are playing with integral modes. Pun with subregs
718 if we aren't. This must come after the entire register case above,
719 since that case is valid for any mode. The following cases are only
720 valid for integral modes. */
722 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
723 if (imode != GET_MODE (op0))
725 if (MEM_P (op0))
726 op0 = adjust_bitfield_address_size (op0, imode, 0, MEM_SIZE (op0));
727 else
729 gcc_assert (imode != BLKmode);
730 op0 = gen_lowpart (imode, op0);
735 /* Storing an lsb-aligned field in a register
736 can be done with a movstrict instruction. */
738 if (!MEM_P (op0)
739 && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
740 && bitsize == GET_MODE_BITSIZE (fieldmode)
741 && optab_handler (movstrict_optab, fieldmode) != CODE_FOR_nothing)
743 struct expand_operand ops[2];
744 enum insn_code icode = optab_handler (movstrict_optab, fieldmode);
745 rtx arg0 = op0;
746 unsigned HOST_WIDE_INT subreg_off;
748 if (GET_CODE (arg0) == SUBREG)
750 /* Else we've got some float mode source being extracted into
751 a different float mode destination -- this combination of
752 subregs results in Severe Tire Damage. */
753 gcc_assert (GET_MODE (SUBREG_REG (arg0)) == fieldmode
754 || GET_MODE_CLASS (fieldmode) == MODE_INT
755 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
756 arg0 = SUBREG_REG (arg0);
759 subreg_off = bitnum / BITS_PER_UNIT;
760 if (validate_subreg (fieldmode, GET_MODE (arg0), arg0, subreg_off))
762 arg0 = gen_rtx_SUBREG (fieldmode, arg0, subreg_off);
764 create_fixed_operand (&ops[0], arg0);
765 /* Shrink the source operand to FIELDMODE. */
766 create_convert_operand_to (&ops[1], value, fieldmode, false);
767 if (maybe_expand_insn (icode, 2, ops))
768 return true;
772 /* Handle fields bigger than a word. */
774 if (bitsize > BITS_PER_WORD)
776 /* Here we transfer the words of the field
777 in the order least significant first.
778 This is because the most significant word is the one which may
779 be less than full.
780 However, only do that if the value is not BLKmode. */
782 unsigned int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
783 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
784 unsigned int i;
785 rtx last;
787 /* This is the mode we must force value to, so that there will be enough
788 subwords to extract. Note that fieldmode will often (always?) be
789 VOIDmode, because that is what store_field uses to indicate that this
790 is a bit field, but passing VOIDmode to operand_subword_force
791 is not allowed. */
792 fieldmode = GET_MODE (value);
793 if (fieldmode == VOIDmode)
794 fieldmode = smallest_mode_for_size (nwords * BITS_PER_WORD, MODE_INT);
796 last = get_last_insn ();
797 for (i = 0; i < nwords; i++)
799 /* If I is 0, use the low-order word in both field and target;
800 if I is 1, use the next to lowest word; and so on. */
801 unsigned int wordnum = (backwards
802 ? GET_MODE_SIZE (fieldmode) / UNITS_PER_WORD
803 - i - 1
804 : i);
805 unsigned int bit_offset = (backwards
806 ? MAX ((int) bitsize - ((int) i + 1)
807 * BITS_PER_WORD,
809 : (int) i * BITS_PER_WORD);
810 rtx value_word = operand_subword_force (value, wordnum, fieldmode);
811 unsigned HOST_WIDE_INT new_bitsize =
812 MIN (BITS_PER_WORD, bitsize - i * BITS_PER_WORD);
814 /* If the remaining chunk doesn't have full wordsize we have
815 to make sure that for big endian machines the higher order
816 bits are used. */
817 if (new_bitsize < BITS_PER_WORD && BYTES_BIG_ENDIAN && !backwards)
818 value_word = simplify_expand_binop (word_mode, lshr_optab,
819 value_word,
820 GEN_INT (BITS_PER_WORD
821 - new_bitsize),
822 NULL_RTX, true,
823 OPTAB_LIB_WIDEN);
825 if (!store_bit_field_1 (op0, new_bitsize,
826 bitnum + bit_offset,
827 bitregion_start, bitregion_end,
828 word_mode,
829 value_word, fallback_p))
831 delete_insns_since (last);
832 return false;
835 return true;
838 /* If VALUE has a floating-point or complex mode, access it as an
839 integer of the corresponding size. This can occur on a machine
840 with 64 bit registers that uses SFmode for float. It can also
841 occur for unaligned float or complex fields. */
842 orig_value = value;
843 if (GET_MODE (value) != VOIDmode
844 && GET_MODE_CLASS (GET_MODE (value)) != MODE_INT
845 && GET_MODE_CLASS (GET_MODE (value)) != MODE_PARTIAL_INT)
847 value = gen_reg_rtx (int_mode_for_mode (GET_MODE (value)));
848 emit_move_insn (gen_lowpart (GET_MODE (orig_value), value), orig_value);
851 /* If OP0 is a multi-word register, narrow it to the affected word.
852 If the region spans two words, defer to store_split_bit_field. */
853 if (!MEM_P (op0) && GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
855 op0 = simplify_gen_subreg (word_mode, op0, GET_MODE (op0),
856 bitnum / BITS_PER_WORD * UNITS_PER_WORD);
857 gcc_assert (op0);
858 bitnum %= BITS_PER_WORD;
859 if (bitnum + bitsize > BITS_PER_WORD)
861 if (!fallback_p)
862 return false;
864 store_split_bit_field (op0, bitsize, bitnum, bitregion_start,
865 bitregion_end, value);
866 return true;
870 /* From here on we can assume that the field to be stored in fits
871 within a word. If the destination is a register, it too fits
872 in a word. */
874 extraction_insn insv;
875 if (!MEM_P (op0)
876 && get_best_reg_extraction_insn (&insv, EP_insv,
877 GET_MODE_BITSIZE (GET_MODE (op0)),
878 fieldmode)
879 && store_bit_field_using_insv (&insv, op0, bitsize, bitnum, value))
880 return true;
882 /* If OP0 is a memory, try copying it to a register and seeing if a
883 cheap register alternative is available. */
884 if (MEM_P (op0))
886 if (get_best_mem_extraction_insn (&insv, EP_insv, bitsize, bitnum,
887 fieldmode)
888 && store_bit_field_using_insv (&insv, op0, bitsize, bitnum, value))
889 return true;
891 rtx last = get_last_insn ();
893 /* Try loading part of OP0 into a register, inserting the bitfield
894 into that, and then copying the result back to OP0. */
895 unsigned HOST_WIDE_INT bitpos;
896 rtx xop0 = adjust_bit_field_mem_for_reg (EP_insv, op0, bitsize, bitnum,
897 bitregion_start, bitregion_end,
898 fieldmode, &bitpos);
899 if (xop0)
901 rtx tempreg = copy_to_reg (xop0);
902 if (store_bit_field_1 (tempreg, bitsize, bitpos,
903 bitregion_start, bitregion_end,
904 fieldmode, orig_value, false))
906 emit_move_insn (xop0, tempreg);
907 return true;
909 delete_insns_since (last);
913 if (!fallback_p)
914 return false;
916 store_fixed_bit_field (op0, bitsize, bitnum, bitregion_start,
917 bitregion_end, value);
918 return true;
921 /* Generate code to store value from rtx VALUE
922 into a bit-field within structure STR_RTX
923 containing BITSIZE bits starting at bit BITNUM.
925 BITREGION_START is bitpos of the first bitfield in this region.
926 BITREGION_END is the bitpos of the ending bitfield in this region.
927 These two fields are 0, if the C++ memory model does not apply,
928 or we are not interested in keeping track of bitfield regions.
930 FIELDMODE is the machine-mode of the FIELD_DECL node for this field. */
932 void
933 store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
934 unsigned HOST_WIDE_INT bitnum,
935 unsigned HOST_WIDE_INT bitregion_start,
936 unsigned HOST_WIDE_INT bitregion_end,
937 enum machine_mode fieldmode,
938 rtx value)
940 /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
941 if (strict_volatile_bitfield_p (str_rtx, bitsize, bitnum, fieldmode,
942 bitregion_start, bitregion_end))
944 /* Storing any naturally aligned field can be done with a simple
945 store. For targets that support fast unaligned memory, any
946 naturally sized, unit aligned field can be done directly. */
947 if (simple_mem_bitfield_p (str_rtx, bitsize, bitnum, fieldmode))
949 str_rtx = adjust_bitfield_address (str_rtx, fieldmode,
950 bitnum / BITS_PER_UNIT);
951 emit_move_insn (str_rtx, value);
953 else
955 str_rtx = narrow_bit_field_mem (str_rtx, fieldmode, bitsize, bitnum,
956 &bitnum);
957 /* Explicitly override the C/C++ memory model; ignore the
958 bit range so that we can do the access in the mode mandated
959 by -fstrict-volatile-bitfields instead. */
960 store_fixed_bit_field_1 (str_rtx, bitsize, bitnum, value);
963 return;
966 /* Under the C++0x memory model, we must not touch bits outside the
967 bit region. Adjust the address to start at the beginning of the
968 bit region. */
969 if (MEM_P (str_rtx) && bitregion_start > 0)
971 enum machine_mode bestmode;
972 HOST_WIDE_INT offset, size;
974 gcc_assert ((bitregion_start % BITS_PER_UNIT) == 0);
976 offset = bitregion_start / BITS_PER_UNIT;
977 bitnum -= bitregion_start;
978 size = (bitnum + bitsize + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
979 bitregion_end -= bitregion_start;
980 bitregion_start = 0;
981 bestmode = get_best_mode (bitsize, bitnum,
982 bitregion_start, bitregion_end,
983 MEM_ALIGN (str_rtx), VOIDmode,
984 MEM_VOLATILE_P (str_rtx));
985 str_rtx = adjust_bitfield_address_size (str_rtx, bestmode, offset, size);
988 if (!store_bit_field_1 (str_rtx, bitsize, bitnum,
989 bitregion_start, bitregion_end,
990 fieldmode, value, true))
991 gcc_unreachable ();
994 /* Use shifts and boolean operations to store VALUE into a bit field of
995 width BITSIZE in OP0, starting at bit BITNUM. */
997 static void
998 store_fixed_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
999 unsigned HOST_WIDE_INT bitnum,
1000 unsigned HOST_WIDE_INT bitregion_start,
1001 unsigned HOST_WIDE_INT bitregion_end,
1002 rtx value)
1004 /* There is a case not handled here:
1005 a structure with a known alignment of just a halfword
1006 and a field split across two aligned halfwords within the structure.
1007 Or likewise a structure with a known alignment of just a byte
1008 and a field split across two bytes.
1009 Such cases are not supposed to be able to occur. */
1011 if (MEM_P (op0))
1013 enum machine_mode mode = GET_MODE (op0);
1014 if (GET_MODE_BITSIZE (mode) == 0
1015 || GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (word_mode))
1016 mode = word_mode;
1017 mode = get_best_mode (bitsize, bitnum, bitregion_start, bitregion_end,
1018 MEM_ALIGN (op0), mode, MEM_VOLATILE_P (op0));
1020 if (mode == VOIDmode)
1022 /* The only way this should occur is if the field spans word
1023 boundaries. */
1024 store_split_bit_field (op0, bitsize, bitnum, bitregion_start,
1025 bitregion_end, value);
1026 return;
1029 op0 = narrow_bit_field_mem (op0, mode, bitsize, bitnum, &bitnum);
1032 store_fixed_bit_field_1 (op0, bitsize, bitnum, value);
1035 /* Helper function for store_fixed_bit_field, stores
1036 the bit field always using the MODE of OP0. */
1038 static void
1039 store_fixed_bit_field_1 (rtx op0, unsigned HOST_WIDE_INT bitsize,
1040 unsigned HOST_WIDE_INT bitnum,
1041 rtx value)
1043 enum machine_mode mode;
1044 rtx temp;
1045 int all_zero = 0;
1046 int all_one = 0;
1048 mode = GET_MODE (op0);
1049 gcc_assert (SCALAR_INT_MODE_P (mode));
1051 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
1052 for invalid input, such as f5 from gcc.dg/pr48335-2.c. */
1054 if (BYTES_BIG_ENDIAN)
1055 /* BITNUM is the distance between our msb
1056 and that of the containing datum.
1057 Convert it to the distance from the lsb. */
1058 bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
1060 /* Now BITNUM is always the distance between our lsb
1061 and that of OP0. */
1063 /* Shift VALUE left by BITNUM bits. If VALUE is not constant,
1064 we must first convert its mode to MODE. */
1066 if (CONST_INT_P (value))
1068 HOST_WIDE_INT v = INTVAL (value);
1070 if (bitsize < HOST_BITS_PER_WIDE_INT)
1071 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
1073 if (v == 0)
1074 all_zero = 1;
1075 else if ((bitsize < HOST_BITS_PER_WIDE_INT
1076 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
1077 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
1078 all_one = 1;
1080 value = lshift_value (mode, v, bitnum);
1082 else
1084 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
1085 && bitnum + bitsize != GET_MODE_BITSIZE (mode));
1087 if (GET_MODE (value) != mode)
1088 value = convert_to_mode (mode, value, 1);
1090 if (must_and)
1091 value = expand_binop (mode, and_optab, value,
1092 mask_rtx (mode, 0, bitsize, 0),
1093 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1094 if (bitnum > 0)
1095 value = expand_shift (LSHIFT_EXPR, mode, value,
1096 bitnum, NULL_RTX, 1);
1099 /* Now clear the chosen bits in OP0,
1100 except that if VALUE is -1 we need not bother. */
1101 /* We keep the intermediates in registers to allow CSE to combine
1102 consecutive bitfield assignments. */
1104 temp = force_reg (mode, op0);
1106 if (! all_one)
1108 temp = expand_binop (mode, and_optab, temp,
1109 mask_rtx (mode, bitnum, bitsize, 1),
1110 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1111 temp = force_reg (mode, temp);
1114 /* Now logical-or VALUE into OP0, unless it is zero. */
1116 if (! all_zero)
1118 temp = expand_binop (mode, ior_optab, temp, value,
1119 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1120 temp = force_reg (mode, temp);
1123 if (op0 != temp)
1125 op0 = copy_rtx (op0);
1126 emit_move_insn (op0, temp);
1130 /* Store a bit field that is split across multiple accessible memory objects.
1132 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
1133 BITSIZE is the field width; BITPOS the position of its first bit
1134 (within the word).
1135 VALUE is the value to store.
1137 This does not yet handle fields wider than BITS_PER_WORD. */
1139 static void
1140 store_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
1141 unsigned HOST_WIDE_INT bitpos,
1142 unsigned HOST_WIDE_INT bitregion_start,
1143 unsigned HOST_WIDE_INT bitregion_end,
1144 rtx value)
1146 unsigned int unit;
1147 unsigned int bitsdone = 0;
1149 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1150 much at a time. */
1151 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
1152 unit = BITS_PER_WORD;
1153 else
1154 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
1156 /* If OP0 is a memory with a mode, then UNIT must not be larger than
1157 OP0's mode as well. Otherwise, store_fixed_bit_field will call us
1158 again, and we will mutually recurse forever. */
1159 if (MEM_P (op0) && GET_MODE_BITSIZE (GET_MODE (op0)) > 0)
1160 unit = MIN (unit, GET_MODE_BITSIZE (GET_MODE (op0)));
1162 /* If VALUE is a constant other than a CONST_INT, get it into a register in
1163 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
1164 that VALUE might be a floating-point constant. */
1165 if (CONSTANT_P (value) && !CONST_INT_P (value))
1167 rtx word = gen_lowpart_common (word_mode, value);
1169 if (word && (value != word))
1170 value = word;
1171 else
1172 value = gen_lowpart_common (word_mode,
1173 force_reg (GET_MODE (value) != VOIDmode
1174 ? GET_MODE (value)
1175 : word_mode, value));
1178 while (bitsdone < bitsize)
1180 unsigned HOST_WIDE_INT thissize;
1181 rtx part, word;
1182 unsigned HOST_WIDE_INT thispos;
1183 unsigned HOST_WIDE_INT offset;
1185 offset = (bitpos + bitsdone) / unit;
1186 thispos = (bitpos + bitsdone) % unit;
1188 /* When region of bytes we can touch is restricted, decrease
1189 UNIT close to the end of the region as needed. If op0 is a REG
1190 or SUBREG of REG, don't do this, as there can't be data races
1191 on a register and we can expand shorter code in some cases. */
1192 if (bitregion_end
1193 && unit > BITS_PER_UNIT
1194 && bitpos + bitsdone - thispos + unit > bitregion_end + 1
1195 && !REG_P (op0)
1196 && (GET_CODE (op0) != SUBREG || !REG_P (SUBREG_REG (op0))))
1198 unit = unit / 2;
1199 continue;
1202 /* THISSIZE must not overrun a word boundary. Otherwise,
1203 store_fixed_bit_field will call us again, and we will mutually
1204 recurse forever. */
1205 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1206 thissize = MIN (thissize, unit - thispos);
1208 if (BYTES_BIG_ENDIAN)
1210 /* Fetch successively less significant portions. */
1211 if (CONST_INT_P (value))
1212 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1213 >> (bitsize - bitsdone - thissize))
1214 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1215 else
1217 int total_bits = GET_MODE_BITSIZE (GET_MODE (value));
1218 /* The args are chosen so that the last part includes the
1219 lsb. Give extract_bit_field the value it needs (with
1220 endianness compensation) to fetch the piece we want. */
1221 part = extract_fixed_bit_field (word_mode, value, thissize,
1222 total_bits - bitsize + bitsdone,
1223 NULL_RTX, 1);
1226 else
1228 /* Fetch successively more significant portions. */
1229 if (CONST_INT_P (value))
1230 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1231 >> bitsdone)
1232 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1233 else
1234 part = extract_fixed_bit_field (word_mode, value, thissize,
1235 bitsdone, NULL_RTX, 1);
1238 /* If OP0 is a register, then handle OFFSET here.
1240 When handling multiword bitfields, extract_bit_field may pass
1241 down a word_mode SUBREG of a larger REG for a bitfield that actually
1242 crosses a word boundary. Thus, for a SUBREG, we must find
1243 the current word starting from the base register. */
1244 if (GET_CODE (op0) == SUBREG)
1246 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD)
1247 + (offset * unit / BITS_PER_WORD);
1248 enum machine_mode sub_mode = GET_MODE (SUBREG_REG (op0));
1249 if (sub_mode != BLKmode && GET_MODE_SIZE (sub_mode) < UNITS_PER_WORD)
1250 word = word_offset ? const0_rtx : op0;
1251 else
1252 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1253 GET_MODE (SUBREG_REG (op0)));
1254 offset &= BITS_PER_WORD / unit - 1;
1256 else if (REG_P (op0))
1258 enum machine_mode op0_mode = GET_MODE (op0);
1259 if (op0_mode != BLKmode && GET_MODE_SIZE (op0_mode) < UNITS_PER_WORD)
1260 word = offset ? const0_rtx : op0;
1261 else
1262 word = operand_subword_force (op0, offset * unit / BITS_PER_WORD,
1263 GET_MODE (op0));
1264 offset &= BITS_PER_WORD / unit - 1;
1266 else
1267 word = op0;
1269 /* OFFSET is in UNITs, and UNIT is in bits. If WORD is const0_rtx,
1270 it is just an out-of-bounds access. Ignore it. */
1271 if (word != const0_rtx)
1272 store_fixed_bit_field (word, thissize, offset * unit + thispos,
1273 bitregion_start, bitregion_end, part);
1274 bitsdone += thissize;
1278 /* A subroutine of extract_bit_field_1 that converts return value X
1279 to either MODE or TMODE. MODE, TMODE and UNSIGNEDP are arguments
1280 to extract_bit_field. */
1282 static rtx
1283 convert_extracted_bit_field (rtx x, enum machine_mode mode,
1284 enum machine_mode tmode, bool unsignedp)
1286 if (GET_MODE (x) == tmode || GET_MODE (x) == mode)
1287 return x;
1289 /* If the x mode is not a scalar integral, first convert to the
1290 integer mode of that size and then access it as a floating-point
1291 value via a SUBREG. */
1292 if (!SCALAR_INT_MODE_P (tmode))
1294 enum machine_mode smode;
1296 smode = mode_for_size (GET_MODE_BITSIZE (tmode), MODE_INT, 0);
1297 x = convert_to_mode (smode, x, unsignedp);
1298 x = force_reg (smode, x);
1299 return gen_lowpart (tmode, x);
1302 return convert_to_mode (tmode, x, unsignedp);
1305 /* Try to use an ext(z)v pattern to extract a field from OP0.
1306 Return the extracted value on success, otherwise return null.
1307 EXT_MODE is the mode of the extraction and the other arguments
1308 are as for extract_bit_field. */
1310 static rtx
1311 extract_bit_field_using_extv (const extraction_insn *extv, rtx op0,
1312 unsigned HOST_WIDE_INT bitsize,
1313 unsigned HOST_WIDE_INT bitnum,
1314 int unsignedp, rtx target,
1315 enum machine_mode mode, enum machine_mode tmode)
1317 struct expand_operand ops[4];
1318 rtx spec_target = target;
1319 rtx spec_target_subreg = 0;
1320 enum machine_mode ext_mode = extv->field_mode;
1321 unsigned unit = GET_MODE_BITSIZE (ext_mode);
1323 if (bitsize == 0 || unit < bitsize)
1324 return NULL_RTX;
1326 if (MEM_P (op0))
1327 /* Get a reference to the first byte of the field. */
1328 op0 = narrow_bit_field_mem (op0, extv->struct_mode, bitsize, bitnum,
1329 &bitnum);
1330 else
1332 /* Convert from counting within OP0 to counting in EXT_MODE. */
1333 if (BYTES_BIG_ENDIAN)
1334 bitnum += unit - GET_MODE_BITSIZE (GET_MODE (op0));
1336 /* If op0 is a register, we need it in EXT_MODE to make it
1337 acceptable to the format of ext(z)v. */
1338 if (GET_CODE (op0) == SUBREG && GET_MODE (op0) != ext_mode)
1339 return NULL_RTX;
1340 if (REG_P (op0) && GET_MODE (op0) != ext_mode)
1341 op0 = gen_lowpart_SUBREG (ext_mode, op0);
1344 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
1345 "backwards" from the size of the unit we are extracting from.
1346 Otherwise, we count bits from the most significant on a
1347 BYTES/BITS_BIG_ENDIAN machine. */
1349 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1350 bitnum = unit - bitsize - bitnum;
1352 if (target == 0)
1353 target = spec_target = gen_reg_rtx (tmode);
1355 if (GET_MODE (target) != ext_mode)
1357 /* Don't use LHS paradoxical subreg if explicit truncation is needed
1358 between the mode of the extraction (word_mode) and the target
1359 mode. Instead, create a temporary and use convert_move to set
1360 the target. */
1361 if (REG_P (target)
1362 && TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (target), ext_mode))
1364 target = gen_lowpart (ext_mode, target);
1365 if (GET_MODE_PRECISION (ext_mode)
1366 > GET_MODE_PRECISION (GET_MODE (spec_target)))
1367 spec_target_subreg = target;
1369 else
1370 target = gen_reg_rtx (ext_mode);
1373 create_output_operand (&ops[0], target, ext_mode);
1374 create_fixed_operand (&ops[1], op0);
1375 create_integer_operand (&ops[2], bitsize);
1376 create_integer_operand (&ops[3], bitnum);
1377 if (maybe_expand_insn (extv->icode, 4, ops))
1379 target = ops[0].value;
1380 if (target == spec_target)
1381 return target;
1382 if (target == spec_target_subreg)
1383 return spec_target;
1384 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1386 return NULL_RTX;
1389 /* A subroutine of extract_bit_field, with the same arguments.
1390 If FALLBACK_P is true, fall back to extract_fixed_bit_field
1391 if we can find no other means of implementing the operation.
1392 if FALLBACK_P is false, return NULL instead. */
1394 static rtx
1395 extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1396 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1397 enum machine_mode mode, enum machine_mode tmode,
1398 bool fallback_p)
1400 rtx op0 = str_rtx;
1401 enum machine_mode int_mode;
1402 enum machine_mode mode1;
1404 if (tmode == VOIDmode)
1405 tmode = mode;
1407 while (GET_CODE (op0) == SUBREG)
1409 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1410 op0 = SUBREG_REG (op0);
1413 /* If we have an out-of-bounds access to a register, just return an
1414 uninitialized register of the required mode. This can occur if the
1415 source code contains an out-of-bounds access to a small array. */
1416 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
1417 return gen_reg_rtx (tmode);
1419 if (REG_P (op0)
1420 && mode == GET_MODE (op0)
1421 && bitnum == 0
1422 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
1424 /* We're trying to extract a full register from itself. */
1425 return op0;
1428 /* See if we can get a better vector mode before extracting. */
1429 if (VECTOR_MODE_P (GET_MODE (op0))
1430 && !MEM_P (op0)
1431 && GET_MODE_INNER (GET_MODE (op0)) != tmode)
1433 enum machine_mode new_mode;
1435 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1436 new_mode = MIN_MODE_VECTOR_FLOAT;
1437 else if (GET_MODE_CLASS (tmode) == MODE_FRACT)
1438 new_mode = MIN_MODE_VECTOR_FRACT;
1439 else if (GET_MODE_CLASS (tmode) == MODE_UFRACT)
1440 new_mode = MIN_MODE_VECTOR_UFRACT;
1441 else if (GET_MODE_CLASS (tmode) == MODE_ACCUM)
1442 new_mode = MIN_MODE_VECTOR_ACCUM;
1443 else if (GET_MODE_CLASS (tmode) == MODE_UACCUM)
1444 new_mode = MIN_MODE_VECTOR_UACCUM;
1445 else
1446 new_mode = MIN_MODE_VECTOR_INT;
1448 for (; new_mode != VOIDmode ; new_mode = GET_MODE_WIDER_MODE (new_mode))
1449 if (GET_MODE_SIZE (new_mode) == GET_MODE_SIZE (GET_MODE (op0))
1450 && targetm.vector_mode_supported_p (new_mode))
1451 break;
1452 if (new_mode != VOIDmode)
1453 op0 = gen_lowpart (new_mode, op0);
1456 /* Use vec_extract patterns for extracting parts of vectors whenever
1457 available. */
1458 if (VECTOR_MODE_P (GET_MODE (op0))
1459 && !MEM_P (op0)
1460 && optab_handler (vec_extract_optab, GET_MODE (op0)) != CODE_FOR_nothing
1461 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
1462 == bitnum / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
1464 struct expand_operand ops[3];
1465 enum machine_mode outermode = GET_MODE (op0);
1466 enum machine_mode innermode = GET_MODE_INNER (outermode);
1467 enum insn_code icode = optab_handler (vec_extract_optab, outermode);
1468 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1470 create_output_operand (&ops[0], target, innermode);
1471 create_input_operand (&ops[1], op0, outermode);
1472 create_integer_operand (&ops[2], pos);
1473 if (maybe_expand_insn (icode, 3, ops))
1475 target = ops[0].value;
1476 if (GET_MODE (target) != mode)
1477 return gen_lowpart (tmode, target);
1478 return target;
1482 /* Make sure we are playing with integral modes. Pun with subregs
1483 if we aren't. */
1485 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
1486 if (imode != GET_MODE (op0))
1488 if (MEM_P (op0))
1489 op0 = adjust_bitfield_address_size (op0, imode, 0, MEM_SIZE (op0));
1490 else if (imode != BLKmode)
1492 op0 = gen_lowpart (imode, op0);
1494 /* If we got a SUBREG, force it into a register since we
1495 aren't going to be able to do another SUBREG on it. */
1496 if (GET_CODE (op0) == SUBREG)
1497 op0 = force_reg (imode, op0);
1499 else if (REG_P (op0))
1501 rtx reg, subreg;
1502 imode = smallest_mode_for_size (GET_MODE_BITSIZE (GET_MODE (op0)),
1503 MODE_INT);
1504 reg = gen_reg_rtx (imode);
1505 subreg = gen_lowpart_SUBREG (GET_MODE (op0), reg);
1506 emit_move_insn (subreg, op0);
1507 op0 = reg;
1508 bitnum += SUBREG_BYTE (subreg) * BITS_PER_UNIT;
1510 else
1512 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (op0));
1513 rtx mem = assign_stack_temp (GET_MODE (op0), size);
1514 emit_move_insn (mem, op0);
1515 op0 = adjust_bitfield_address_size (mem, BLKmode, 0, size);
1520 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1521 If that's wrong, the solution is to test for it and set TARGET to 0
1522 if needed. */
1524 /* Get the mode of the field to use for atomic access or subreg
1525 conversion. */
1526 mode1 = mode;
1527 if (SCALAR_INT_MODE_P (tmode))
1529 enum machine_mode try_mode = mode_for_size (bitsize,
1530 GET_MODE_CLASS (tmode), 0);
1531 if (try_mode != BLKmode)
1532 mode1 = try_mode;
1534 gcc_assert (mode1 != BLKmode);
1536 /* Extraction of a full MODE1 value can be done with a subreg as long
1537 as the least significant bit of the value is the least significant
1538 bit of either OP0 or a word of OP0. */
1539 if (!MEM_P (op0)
1540 && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
1541 && bitsize == GET_MODE_BITSIZE (mode1)
1542 && TRULY_NOOP_TRUNCATION_MODES_P (mode1, GET_MODE (op0)))
1544 rtx sub = simplify_gen_subreg (mode1, op0, GET_MODE (op0),
1545 bitnum / BITS_PER_UNIT);
1546 if (sub)
1547 return convert_extracted_bit_field (sub, mode, tmode, unsignedp);
1550 /* Extraction of a full MODE1 value can be done with a load as long as
1551 the field is on a byte boundary and is sufficiently aligned. */
1552 if (simple_mem_bitfield_p (op0, bitsize, bitnum, mode1))
1554 op0 = adjust_bitfield_address (op0, mode1, bitnum / BITS_PER_UNIT);
1555 return convert_extracted_bit_field (op0, mode, tmode, unsignedp);
1558 /* Handle fields bigger than a word. */
1560 if (bitsize > BITS_PER_WORD)
1562 /* Here we transfer the words of the field
1563 in the order least significant first.
1564 This is because the most significant word is the one which may
1565 be less than full. */
1567 unsigned int backwards = WORDS_BIG_ENDIAN;
1568 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1569 unsigned int i;
1570 rtx last;
1572 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1573 target = gen_reg_rtx (mode);
1575 /* Indicate for flow that the entire target reg is being set. */
1576 emit_clobber (target);
1578 last = get_last_insn ();
1579 for (i = 0; i < nwords; i++)
1581 /* If I is 0, use the low-order word in both field and target;
1582 if I is 1, use the next to lowest word; and so on. */
1583 /* Word number in TARGET to use. */
1584 unsigned int wordnum
1585 = (backwards
1586 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1587 : i);
1588 /* Offset from start of field in OP0. */
1589 unsigned int bit_offset = (backwards
1590 ? MAX ((int) bitsize - ((int) i + 1)
1591 * BITS_PER_WORD,
1593 : (int) i * BITS_PER_WORD);
1594 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1595 rtx result_part
1596 = extract_bit_field_1 (op0, MIN (BITS_PER_WORD,
1597 bitsize - i * BITS_PER_WORD),
1598 bitnum + bit_offset, 1, target_part,
1599 mode, word_mode, fallback_p);
1601 gcc_assert (target_part);
1602 if (!result_part)
1604 delete_insns_since (last);
1605 return NULL;
1608 if (result_part != target_part)
1609 emit_move_insn (target_part, result_part);
1612 if (unsignedp)
1614 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1615 need to be zero'd out. */
1616 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1618 unsigned int i, total_words;
1620 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1621 for (i = nwords; i < total_words; i++)
1622 emit_move_insn
1623 (operand_subword (target,
1624 backwards ? total_words - i - 1 : i,
1625 1, VOIDmode),
1626 const0_rtx);
1628 return target;
1631 /* Signed bit field: sign-extend with two arithmetic shifts. */
1632 target = expand_shift (LSHIFT_EXPR, mode, target,
1633 GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
1634 return expand_shift (RSHIFT_EXPR, mode, target,
1635 GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
1638 /* If OP0 is a multi-word register, narrow it to the affected word.
1639 If the region spans two words, defer to extract_split_bit_field. */
1640 if (!MEM_P (op0) && GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1642 op0 = simplify_gen_subreg (word_mode, op0, GET_MODE (op0),
1643 bitnum / BITS_PER_WORD * UNITS_PER_WORD);
1644 bitnum %= BITS_PER_WORD;
1645 if (bitnum + bitsize > BITS_PER_WORD)
1647 if (!fallback_p)
1648 return NULL_RTX;
1649 target = extract_split_bit_field (op0, bitsize, bitnum, unsignedp);
1650 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1654 /* From here on we know the desired field is smaller than a word.
1655 If OP0 is a register, it too fits within a word. */
1656 enum extraction_pattern pattern = unsignedp ? EP_extzv : EP_extv;
1657 extraction_insn extv;
1658 if (!MEM_P (op0)
1659 /* ??? We could limit the structure size to the part of OP0 that
1660 contains the field, with appropriate checks for endianness
1661 and TRULY_NOOP_TRUNCATION. */
1662 && get_best_reg_extraction_insn (&extv, pattern,
1663 GET_MODE_BITSIZE (GET_MODE (op0)),
1664 tmode))
1666 rtx result = extract_bit_field_using_extv (&extv, op0, bitsize, bitnum,
1667 unsignedp, target, mode,
1668 tmode);
1669 if (result)
1670 return result;
1673 /* If OP0 is a memory, try copying it to a register and seeing if a
1674 cheap register alternative is available. */
1675 if (MEM_P (op0))
1677 if (get_best_mem_extraction_insn (&extv, pattern, bitsize, bitnum,
1678 tmode))
1680 rtx result = extract_bit_field_using_extv (&extv, op0, bitsize,
1681 bitnum, unsignedp,
1682 target, mode,
1683 tmode);
1684 if (result)
1685 return result;
1688 rtx last = get_last_insn ();
1690 /* Try loading part of OP0 into a register and extracting the
1691 bitfield from that. */
1692 unsigned HOST_WIDE_INT bitpos;
1693 rtx xop0 = adjust_bit_field_mem_for_reg (pattern, op0, bitsize, bitnum,
1694 0, 0, tmode, &bitpos);
1695 if (xop0)
1697 xop0 = copy_to_reg (xop0);
1698 rtx result = extract_bit_field_1 (xop0, bitsize, bitpos,
1699 unsignedp, target,
1700 mode, tmode, false);
1701 if (result)
1702 return result;
1703 delete_insns_since (last);
1707 if (!fallback_p)
1708 return NULL;
1710 /* Find a correspondingly-sized integer field, so we can apply
1711 shifts and masks to it. */
1712 int_mode = int_mode_for_mode (tmode);
1713 if (int_mode == BLKmode)
1714 int_mode = int_mode_for_mode (mode);
1715 /* Should probably push op0 out to memory and then do a load. */
1716 gcc_assert (int_mode != BLKmode);
1718 target = extract_fixed_bit_field (int_mode, op0, bitsize, bitnum,
1719 target, unsignedp);
1720 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1723 /* Generate code to extract a byte-field from STR_RTX
1724 containing BITSIZE bits, starting at BITNUM,
1725 and put it in TARGET if possible (if TARGET is nonzero).
1726 Regardless of TARGET, we return the rtx for where the value is placed.
1728 STR_RTX is the structure containing the byte (a REG or MEM).
1729 UNSIGNEDP is nonzero if this is an unsigned bit field.
1730 MODE is the natural mode of the field value once extracted.
1731 TMODE is the mode the caller would like the value to have;
1732 but the value may be returned with type MODE instead.
1734 If a TARGET is specified and we can store in it at no extra cost,
1735 we do so, and return TARGET.
1736 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1737 if they are equally easy. */
1740 extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1741 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1742 enum machine_mode mode, enum machine_mode tmode)
1744 enum machine_mode mode1;
1746 /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
1747 if (GET_MODE_BITSIZE (GET_MODE (str_rtx)) > 0)
1748 mode1 = GET_MODE (str_rtx);
1749 else if (target && GET_MODE_BITSIZE (GET_MODE (target)) > 0)
1750 mode1 = GET_MODE (target);
1751 else
1752 mode1 = tmode;
1754 if (strict_volatile_bitfield_p (str_rtx, bitsize, bitnum, mode1, 0, 0))
1756 rtx result;
1758 /* Extraction of a full MODE1 value can be done with a load as long as
1759 the field is on a byte boundary and is sufficiently aligned. */
1760 if (simple_mem_bitfield_p (str_rtx, bitsize, bitnum, mode1))
1761 result = adjust_bitfield_address (str_rtx, mode1,
1762 bitnum / BITS_PER_UNIT);
1763 else
1765 str_rtx = narrow_bit_field_mem (str_rtx, mode1, bitsize, bitnum,
1766 &bitnum);
1767 result = extract_fixed_bit_field_1 (mode, str_rtx, bitsize, bitnum,
1768 target, unsignedp);
1771 return convert_extracted_bit_field (result, mode, tmode, unsignedp);
1774 return extract_bit_field_1 (str_rtx, bitsize, bitnum, unsignedp,
1775 target, mode, tmode, true);
1778 /* Use shifts and boolean operations to extract a field of BITSIZE bits
1779 from bit BITNUM of OP0.
1781 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1782 If TARGET is nonzero, attempts to store the value there
1783 and return TARGET, but this is not guaranteed.
1784 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
1786 static rtx
1787 extract_fixed_bit_field (enum machine_mode tmode, rtx op0,
1788 unsigned HOST_WIDE_INT bitsize,
1789 unsigned HOST_WIDE_INT bitnum, rtx target,
1790 int unsignedp)
1792 if (MEM_P (op0))
1794 enum machine_mode mode
1795 = get_best_mode (bitsize, bitnum, 0, 0, MEM_ALIGN (op0), word_mode,
1796 MEM_VOLATILE_P (op0));
1798 if (mode == VOIDmode)
1799 /* The only way this should occur is if the field spans word
1800 boundaries. */
1801 return extract_split_bit_field (op0, bitsize, bitnum, unsignedp);
1803 op0 = narrow_bit_field_mem (op0, mode, bitsize, bitnum, &bitnum);
1806 return extract_fixed_bit_field_1 (tmode, op0, bitsize, bitnum,
1807 target, unsignedp);
1810 /* Helper function for extract_fixed_bit_field, extracts
1811 the bit field always using the MODE of OP0. */
1813 static rtx
1814 extract_fixed_bit_field_1 (enum machine_mode tmode, rtx op0,
1815 unsigned HOST_WIDE_INT bitsize,
1816 unsigned HOST_WIDE_INT bitnum, rtx target,
1817 int unsignedp)
1819 enum machine_mode mode = GET_MODE (op0);
1820 gcc_assert (SCALAR_INT_MODE_P (mode));
1822 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
1823 for invalid input, such as extract equivalent of f5 from
1824 gcc.dg/pr48335-2.c. */
1826 if (BYTES_BIG_ENDIAN)
1827 /* BITNUM is the distance between our msb and that of OP0.
1828 Convert it to the distance from the lsb. */
1829 bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
1831 /* Now BITNUM is always the distance between the field's lsb and that of OP0.
1832 We have reduced the big-endian case to the little-endian case. */
1834 if (unsignedp)
1836 if (bitnum)
1838 /* If the field does not already start at the lsb,
1839 shift it so it does. */
1840 /* Maybe propagate the target for the shift. */
1841 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1842 if (tmode != mode)
1843 subtarget = 0;
1844 op0 = expand_shift (RSHIFT_EXPR, mode, op0, bitnum, subtarget, 1);
1846 /* Convert the value to the desired mode. */
1847 if (mode != tmode)
1848 op0 = convert_to_mode (tmode, op0, 1);
1850 /* Unless the msb of the field used to be the msb when we shifted,
1851 mask out the upper bits. */
1853 if (GET_MODE_BITSIZE (mode) != bitnum + bitsize)
1854 return expand_binop (GET_MODE (op0), and_optab, op0,
1855 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1856 target, 1, OPTAB_LIB_WIDEN);
1857 return op0;
1860 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1861 then arithmetic-shift its lsb to the lsb of the word. */
1862 op0 = force_reg (mode, op0);
1864 /* Find the narrowest integer mode that contains the field. */
1866 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1867 mode = GET_MODE_WIDER_MODE (mode))
1868 if (GET_MODE_BITSIZE (mode) >= bitsize + bitnum)
1870 op0 = convert_to_mode (mode, op0, 0);
1871 break;
1874 if (mode != tmode)
1875 target = 0;
1877 if (GET_MODE_BITSIZE (mode) != (bitsize + bitnum))
1879 int amount = GET_MODE_BITSIZE (mode) - (bitsize + bitnum);
1880 /* Maybe propagate the target for the shift. */
1881 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1882 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1885 return expand_shift (RSHIFT_EXPR, mode, op0,
1886 GET_MODE_BITSIZE (mode) - bitsize, target, 0);
1889 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1890 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1891 complement of that if COMPLEMENT. The mask is truncated if
1892 necessary to the width of mode MODE. The mask is zero-extended if
1893 BITSIZE+BITPOS is too small for MODE. */
1895 static rtx
1896 mask_rtx (enum machine_mode mode, int bitpos, int bitsize, int complement)
1898 double_int mask;
1900 mask = double_int::mask (bitsize);
1901 mask = mask.llshift (bitpos, HOST_BITS_PER_DOUBLE_INT);
1903 if (complement)
1904 mask = ~mask;
1906 return immed_double_int_const (mask, mode);
1909 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1910 VALUE << BITPOS. */
1912 static rtx
1913 lshift_value (enum machine_mode mode, unsigned HOST_WIDE_INT value,
1914 int bitpos)
1916 double_int val;
1918 val = double_int::from_uhwi (value);
1919 val = val.llshift (bitpos, HOST_BITS_PER_DOUBLE_INT);
1921 return immed_double_int_const (val, mode);
1924 /* Extract a bit field that is split across two words
1925 and return an RTX for the result.
1927 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1928 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1929 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
1931 static rtx
1932 extract_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
1933 unsigned HOST_WIDE_INT bitpos, int unsignedp)
1935 unsigned int unit;
1936 unsigned int bitsdone = 0;
1937 rtx result = NULL_RTX;
1938 int first = 1;
1940 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1941 much at a time. */
1942 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
1943 unit = BITS_PER_WORD;
1944 else
1945 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
1947 while (bitsdone < bitsize)
1949 unsigned HOST_WIDE_INT thissize;
1950 rtx part, word;
1951 unsigned HOST_WIDE_INT thispos;
1952 unsigned HOST_WIDE_INT offset;
1954 offset = (bitpos + bitsdone) / unit;
1955 thispos = (bitpos + bitsdone) % unit;
1957 /* THISSIZE must not overrun a word boundary. Otherwise,
1958 extract_fixed_bit_field will call us again, and we will mutually
1959 recurse forever. */
1960 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1961 thissize = MIN (thissize, unit - thispos);
1963 /* If OP0 is a register, then handle OFFSET here.
1965 When handling multiword bitfields, extract_bit_field may pass
1966 down a word_mode SUBREG of a larger REG for a bitfield that actually
1967 crosses a word boundary. Thus, for a SUBREG, we must find
1968 the current word starting from the base register. */
1969 if (GET_CODE (op0) == SUBREG)
1971 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
1972 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1973 GET_MODE (SUBREG_REG (op0)));
1974 offset = 0;
1976 else if (REG_P (op0))
1978 word = operand_subword_force (op0, offset, GET_MODE (op0));
1979 offset = 0;
1981 else
1982 word = op0;
1984 /* Extract the parts in bit-counting order,
1985 whose meaning is determined by BYTES_PER_UNIT.
1986 OFFSET is in UNITs, and UNIT is in bits. */
1987 part = extract_fixed_bit_field (word_mode, word, thissize,
1988 offset * unit + thispos, 0, 1);
1989 bitsdone += thissize;
1991 /* Shift this part into place for the result. */
1992 if (BYTES_BIG_ENDIAN)
1994 if (bitsize != bitsdone)
1995 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1996 bitsize - bitsdone, 0, 1);
1998 else
2000 if (bitsdone != thissize)
2001 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2002 bitsdone - thissize, 0, 1);
2005 if (first)
2006 result = part;
2007 else
2008 /* Combine the parts with bitwise or. This works
2009 because we extracted each part as an unsigned bit field. */
2010 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
2011 OPTAB_LIB_WIDEN);
2013 first = 0;
2016 /* Unsigned bit field: we are done. */
2017 if (unsignedp)
2018 return result;
2019 /* Signed bit field: sign-extend with two arithmetic shifts. */
2020 result = expand_shift (LSHIFT_EXPR, word_mode, result,
2021 BITS_PER_WORD - bitsize, NULL_RTX, 0);
2022 return expand_shift (RSHIFT_EXPR, word_mode, result,
2023 BITS_PER_WORD - bitsize, NULL_RTX, 0);
2026 /* Try to read the low bits of SRC as an rvalue of mode MODE, preserving
2027 the bit pattern. SRC_MODE is the mode of SRC; if this is smaller than
2028 MODE, fill the upper bits with zeros. Fail if the layout of either
2029 mode is unknown (as for CC modes) or if the extraction would involve
2030 unprofitable mode punning. Return the value on success, otherwise
2031 return null.
2033 This is different from gen_lowpart* in these respects:
2035 - the returned value must always be considered an rvalue
2037 - when MODE is wider than SRC_MODE, the extraction involves
2038 a zero extension
2040 - when MODE is smaller than SRC_MODE, the extraction involves
2041 a truncation (and is thus subject to TRULY_NOOP_TRUNCATION).
2043 In other words, this routine performs a computation, whereas the
2044 gen_lowpart* routines are conceptually lvalue or rvalue subreg
2045 operations. */
2048 extract_low_bits (enum machine_mode mode, enum machine_mode src_mode, rtx src)
2050 enum machine_mode int_mode, src_int_mode;
2052 if (mode == src_mode)
2053 return src;
2055 if (CONSTANT_P (src))
2057 /* simplify_gen_subreg can't be used here, as if simplify_subreg
2058 fails, it will happily create (subreg (symbol_ref)) or similar
2059 invalid SUBREGs. */
2060 unsigned int byte = subreg_lowpart_offset (mode, src_mode);
2061 rtx ret = simplify_subreg (mode, src, src_mode, byte);
2062 if (ret)
2063 return ret;
2065 if (GET_MODE (src) == VOIDmode
2066 || !validate_subreg (mode, src_mode, src, byte))
2067 return NULL_RTX;
2069 src = force_reg (GET_MODE (src), src);
2070 return gen_rtx_SUBREG (mode, src, byte);
2073 if (GET_MODE_CLASS (mode) == MODE_CC || GET_MODE_CLASS (src_mode) == MODE_CC)
2074 return NULL_RTX;
2076 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (src_mode)
2077 && MODES_TIEABLE_P (mode, src_mode))
2079 rtx x = gen_lowpart_common (mode, src);
2080 if (x)
2081 return x;
2084 src_int_mode = int_mode_for_mode (src_mode);
2085 int_mode = int_mode_for_mode (mode);
2086 if (src_int_mode == BLKmode || int_mode == BLKmode)
2087 return NULL_RTX;
2089 if (!MODES_TIEABLE_P (src_int_mode, src_mode))
2090 return NULL_RTX;
2091 if (!MODES_TIEABLE_P (int_mode, mode))
2092 return NULL_RTX;
2094 src = gen_lowpart (src_int_mode, src);
2095 src = convert_modes (int_mode, src_int_mode, src, true);
2096 src = gen_lowpart (mode, src);
2097 return src;
2100 /* Add INC into TARGET. */
2102 void
2103 expand_inc (rtx target, rtx inc)
2105 rtx value = expand_binop (GET_MODE (target), add_optab,
2106 target, inc,
2107 target, 0, OPTAB_LIB_WIDEN);
2108 if (value != target)
2109 emit_move_insn (target, value);
2112 /* Subtract DEC from TARGET. */
2114 void
2115 expand_dec (rtx target, rtx dec)
2117 rtx value = expand_binop (GET_MODE (target), sub_optab,
2118 target, dec,
2119 target, 0, OPTAB_LIB_WIDEN);
2120 if (value != target)
2121 emit_move_insn (target, value);
2124 /* Output a shift instruction for expression code CODE,
2125 with SHIFTED being the rtx for the value to shift,
2126 and AMOUNT the rtx for the amount to shift by.
2127 Store the result in the rtx TARGET, if that is convenient.
2128 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2129 Return the rtx for where the value is. */
2131 static rtx
2132 expand_shift_1 (enum tree_code code, enum machine_mode mode, rtx shifted,
2133 rtx amount, rtx target, int unsignedp)
2135 rtx op1, temp = 0;
2136 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
2137 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2138 optab lshift_optab = ashl_optab;
2139 optab rshift_arith_optab = ashr_optab;
2140 optab rshift_uns_optab = lshr_optab;
2141 optab lrotate_optab = rotl_optab;
2142 optab rrotate_optab = rotr_optab;
2143 enum machine_mode op1_mode;
2144 enum machine_mode scalar_mode = mode;
2145 int attempt;
2146 bool speed = optimize_insn_for_speed_p ();
2148 if (VECTOR_MODE_P (mode))
2149 scalar_mode = GET_MODE_INNER (mode);
2150 op1 = amount;
2151 op1_mode = GET_MODE (op1);
2153 /* Determine whether the shift/rotate amount is a vector, or scalar. If the
2154 shift amount is a vector, use the vector/vector shift patterns. */
2155 if (VECTOR_MODE_P (mode) && VECTOR_MODE_P (op1_mode))
2157 lshift_optab = vashl_optab;
2158 rshift_arith_optab = vashr_optab;
2159 rshift_uns_optab = vlshr_optab;
2160 lrotate_optab = vrotl_optab;
2161 rrotate_optab = vrotr_optab;
2164 /* Previously detected shift-counts computed by NEGATE_EXPR
2165 and shifted in the other direction; but that does not work
2166 on all machines. */
2168 if (SHIFT_COUNT_TRUNCATED)
2170 if (CONST_INT_P (op1)
2171 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2172 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (scalar_mode)))
2173 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2174 % GET_MODE_BITSIZE (scalar_mode));
2175 else if (GET_CODE (op1) == SUBREG
2176 && subreg_lowpart_p (op1)
2177 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op1)))
2178 && SCALAR_INT_MODE_P (GET_MODE (op1)))
2179 op1 = SUBREG_REG (op1);
2182 /* Canonicalize rotates by constant amount. If op1 is bitsize / 2,
2183 prefer left rotation, if op1 is from bitsize / 2 + 1 to
2184 bitsize - 1, use other direction of rotate with 1 .. bitsize / 2 - 1
2185 amount instead. */
2186 if (rotate
2187 && CONST_INT_P (op1)
2188 && IN_RANGE (INTVAL (op1), GET_MODE_BITSIZE (scalar_mode) / 2 + left,
2189 GET_MODE_BITSIZE (scalar_mode) - 1))
2191 op1 = GEN_INT (GET_MODE_BITSIZE (scalar_mode) - INTVAL (op1));
2192 left = !left;
2193 code = left ? LROTATE_EXPR : RROTATE_EXPR;
2196 if (op1 == const0_rtx)
2197 return shifted;
2199 /* Check whether its cheaper to implement a left shift by a constant
2200 bit count by a sequence of additions. */
2201 if (code == LSHIFT_EXPR
2202 && CONST_INT_P (op1)
2203 && INTVAL (op1) > 0
2204 && INTVAL (op1) < GET_MODE_PRECISION (scalar_mode)
2205 && INTVAL (op1) < MAX_BITS_PER_WORD
2206 && (shift_cost (speed, mode, INTVAL (op1))
2207 > INTVAL (op1) * add_cost (speed, mode))
2208 && shift_cost (speed, mode, INTVAL (op1)) != MAX_COST)
2210 int i;
2211 for (i = 0; i < INTVAL (op1); i++)
2213 temp = force_reg (mode, shifted);
2214 shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2215 unsignedp, OPTAB_LIB_WIDEN);
2217 return shifted;
2220 for (attempt = 0; temp == 0 && attempt < 3; attempt++)
2222 enum optab_methods methods;
2224 if (attempt == 0)
2225 methods = OPTAB_DIRECT;
2226 else if (attempt == 1)
2227 methods = OPTAB_WIDEN;
2228 else
2229 methods = OPTAB_LIB_WIDEN;
2231 if (rotate)
2233 /* Widening does not work for rotation. */
2234 if (methods == OPTAB_WIDEN)
2235 continue;
2236 else if (methods == OPTAB_LIB_WIDEN)
2238 /* If we have been unable to open-code this by a rotation,
2239 do it as the IOR of two shifts. I.e., to rotate A
2240 by N bits, compute
2241 (A << N) | ((unsigned) A >> ((-N) & (C - 1)))
2242 where C is the bitsize of A.
2244 It is theoretically possible that the target machine might
2245 not be able to perform either shift and hence we would
2246 be making two libcalls rather than just the one for the
2247 shift (similarly if IOR could not be done). We will allow
2248 this extremely unlikely lossage to avoid complicating the
2249 code below. */
2251 rtx subtarget = target == shifted ? 0 : target;
2252 rtx new_amount, other_amount;
2253 rtx temp1;
2255 new_amount = op1;
2256 if (op1 == const0_rtx)
2257 return shifted;
2258 else if (CONST_INT_P (op1))
2259 other_amount = GEN_INT (GET_MODE_BITSIZE (scalar_mode)
2260 - INTVAL (op1));
2261 else
2263 other_amount
2264 = simplify_gen_unary (NEG, GET_MODE (op1),
2265 op1, GET_MODE (op1));
2266 HOST_WIDE_INT mask = GET_MODE_PRECISION (scalar_mode) - 1;
2267 other_amount
2268 = simplify_gen_binary (AND, GET_MODE (op1), other_amount,
2269 gen_int_mode (mask, GET_MODE (op1)));
2272 shifted = force_reg (mode, shifted);
2274 temp = expand_shift_1 (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2275 mode, shifted, new_amount, 0, 1);
2276 temp1 = expand_shift_1 (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2277 mode, shifted, other_amount,
2278 subtarget, 1);
2279 return expand_binop (mode, ior_optab, temp, temp1, target,
2280 unsignedp, methods);
2283 temp = expand_binop (mode,
2284 left ? lrotate_optab : rrotate_optab,
2285 shifted, op1, target, unsignedp, methods);
2287 else if (unsignedp)
2288 temp = expand_binop (mode,
2289 left ? lshift_optab : rshift_uns_optab,
2290 shifted, op1, target, unsignedp, methods);
2292 /* Do arithmetic shifts.
2293 Also, if we are going to widen the operand, we can just as well
2294 use an arithmetic right-shift instead of a logical one. */
2295 if (temp == 0 && ! rotate
2296 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2298 enum optab_methods methods1 = methods;
2300 /* If trying to widen a log shift to an arithmetic shift,
2301 don't accept an arithmetic shift of the same size. */
2302 if (unsignedp)
2303 methods1 = OPTAB_MUST_WIDEN;
2305 /* Arithmetic shift */
2307 temp = expand_binop (mode,
2308 left ? lshift_optab : rshift_arith_optab,
2309 shifted, op1, target, unsignedp, methods1);
2312 /* We used to try extzv here for logical right shifts, but that was
2313 only useful for one machine, the VAX, and caused poor code
2314 generation there for lshrdi3, so the code was deleted and a
2315 define_expand for lshrsi3 was added to vax.md. */
2318 gcc_assert (temp);
2319 return temp;
2322 /* Output a shift instruction for expression code CODE,
2323 with SHIFTED being the rtx for the value to shift,
2324 and AMOUNT the amount to shift by.
2325 Store the result in the rtx TARGET, if that is convenient.
2326 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2327 Return the rtx for where the value is. */
2330 expand_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2331 int amount, rtx target, int unsignedp)
2333 return expand_shift_1 (code, mode,
2334 shifted, GEN_INT (amount), target, unsignedp);
2337 /* Output a shift instruction for expression code CODE,
2338 with SHIFTED being the rtx for the value to shift,
2339 and AMOUNT the tree for the amount to shift by.
2340 Store the result in the rtx TARGET, if that is convenient.
2341 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2342 Return the rtx for where the value is. */
2345 expand_variable_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2346 tree amount, rtx target, int unsignedp)
2348 return expand_shift_1 (code, mode,
2349 shifted, expand_normal (amount), target, unsignedp);
2353 /* Indicates the type of fixup needed after a constant multiplication.
2354 BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2355 the result should be negated, and ADD_VARIANT means that the
2356 multiplicand should be added to the result. */
2357 enum mult_variant {basic_variant, negate_variant, add_variant};
2359 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2360 const struct mult_cost *, enum machine_mode mode);
2361 static bool choose_mult_variant (enum machine_mode, HOST_WIDE_INT,
2362 struct algorithm *, enum mult_variant *, int);
2363 static rtx expand_mult_const (enum machine_mode, rtx, HOST_WIDE_INT, rtx,
2364 const struct algorithm *, enum mult_variant);
2365 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2366 static rtx extract_high_half (enum machine_mode, rtx);
2367 static rtx expmed_mult_highpart (enum machine_mode, rtx, rtx, rtx, int, int);
2368 static rtx expmed_mult_highpart_optab (enum machine_mode, rtx, rtx, rtx,
2369 int, int);
2370 /* Compute and return the best algorithm for multiplying by T.
2371 The algorithm must cost less than cost_limit
2372 If retval.cost >= COST_LIMIT, no algorithm was found and all
2373 other field of the returned struct are undefined.
2374 MODE is the machine mode of the multiplication. */
2376 static void
2377 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2378 const struct mult_cost *cost_limit, enum machine_mode mode)
2380 int m;
2381 struct algorithm *alg_in, *best_alg;
2382 struct mult_cost best_cost;
2383 struct mult_cost new_limit;
2384 int op_cost, op_latency;
2385 unsigned HOST_WIDE_INT orig_t = t;
2386 unsigned HOST_WIDE_INT q;
2387 int maxm, hash_index;
2388 bool cache_hit = false;
2389 enum alg_code cache_alg = alg_zero;
2390 bool speed = optimize_insn_for_speed_p ();
2391 enum machine_mode imode;
2392 struct alg_hash_entry *entry_ptr;
2394 /* Indicate that no algorithm is yet found. If no algorithm
2395 is found, this value will be returned and indicate failure. */
2396 alg_out->cost.cost = cost_limit->cost + 1;
2397 alg_out->cost.latency = cost_limit->latency + 1;
2399 if (cost_limit->cost < 0
2400 || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2401 return;
2403 /* Be prepared for vector modes. */
2404 imode = GET_MODE_INNER (mode);
2405 if (imode == VOIDmode)
2406 imode = mode;
2408 maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (imode));
2410 /* Restrict the bits of "t" to the multiplication's mode. */
2411 t &= GET_MODE_MASK (imode);
2413 /* t == 1 can be done in zero cost. */
2414 if (t == 1)
2416 alg_out->ops = 1;
2417 alg_out->cost.cost = 0;
2418 alg_out->cost.latency = 0;
2419 alg_out->op[0] = alg_m;
2420 return;
2423 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2424 fail now. */
2425 if (t == 0)
2427 if (MULT_COST_LESS (cost_limit, zero_cost (speed)))
2428 return;
2429 else
2431 alg_out->ops = 1;
2432 alg_out->cost.cost = zero_cost (speed);
2433 alg_out->cost.latency = zero_cost (speed);
2434 alg_out->op[0] = alg_zero;
2435 return;
2439 /* We'll be needing a couple extra algorithm structures now. */
2441 alg_in = XALLOCA (struct algorithm);
2442 best_alg = XALLOCA (struct algorithm);
2443 best_cost = *cost_limit;
2445 /* Compute the hash index. */
2446 hash_index = (t ^ (unsigned int) mode ^ (speed * 256)) % NUM_ALG_HASH_ENTRIES;
2448 /* See if we already know what to do for T. */
2449 entry_ptr = alg_hash_entry_ptr (hash_index);
2450 if (entry_ptr->t == t
2451 && entry_ptr->mode == mode
2452 && entry_ptr->mode == mode
2453 && entry_ptr->speed == speed
2454 && entry_ptr->alg != alg_unknown)
2456 cache_alg = entry_ptr->alg;
2458 if (cache_alg == alg_impossible)
2460 /* The cache tells us that it's impossible to synthesize
2461 multiplication by T within entry_ptr->cost. */
2462 if (!CHEAPER_MULT_COST (&entry_ptr->cost, cost_limit))
2463 /* COST_LIMIT is at least as restrictive as the one
2464 recorded in the hash table, in which case we have no
2465 hope of synthesizing a multiplication. Just
2466 return. */
2467 return;
2469 /* If we get here, COST_LIMIT is less restrictive than the
2470 one recorded in the hash table, so we may be able to
2471 synthesize a multiplication. Proceed as if we didn't
2472 have the cache entry. */
2474 else
2476 if (CHEAPER_MULT_COST (cost_limit, &entry_ptr->cost))
2477 /* The cached algorithm shows that this multiplication
2478 requires more cost than COST_LIMIT. Just return. This
2479 way, we don't clobber this cache entry with
2480 alg_impossible but retain useful information. */
2481 return;
2483 cache_hit = true;
2485 switch (cache_alg)
2487 case alg_shift:
2488 goto do_alg_shift;
2490 case alg_add_t_m2:
2491 case alg_sub_t_m2:
2492 goto do_alg_addsub_t_m2;
2494 case alg_add_factor:
2495 case alg_sub_factor:
2496 goto do_alg_addsub_factor;
2498 case alg_add_t2_m:
2499 goto do_alg_add_t2_m;
2501 case alg_sub_t2_m:
2502 goto do_alg_sub_t2_m;
2504 default:
2505 gcc_unreachable ();
2510 /* If we have a group of zero bits at the low-order part of T, try
2511 multiplying by the remaining bits and then doing a shift. */
2513 if ((t & 1) == 0)
2515 do_alg_shift:
2516 m = floor_log2 (t & -t); /* m = number of low zero bits */
2517 if (m < maxm)
2519 q = t >> m;
2520 /* The function expand_shift will choose between a shift and
2521 a sequence of additions, so the observed cost is given as
2522 MIN (m * add_cost(speed, mode), shift_cost(speed, mode, m)). */
2523 op_cost = m * add_cost (speed, mode);
2524 if (shift_cost (speed, mode, m) < op_cost)
2525 op_cost = shift_cost (speed, mode, m);
2526 new_limit.cost = best_cost.cost - op_cost;
2527 new_limit.latency = best_cost.latency - op_cost;
2528 synth_mult (alg_in, q, &new_limit, mode);
2530 alg_in->cost.cost += op_cost;
2531 alg_in->cost.latency += op_cost;
2532 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2534 struct algorithm *x;
2535 best_cost = alg_in->cost;
2536 x = alg_in, alg_in = best_alg, best_alg = x;
2537 best_alg->log[best_alg->ops] = m;
2538 best_alg->op[best_alg->ops] = alg_shift;
2541 /* See if treating ORIG_T as a signed number yields a better
2542 sequence. Try this sequence only for a negative ORIG_T
2543 as it would be useless for a non-negative ORIG_T. */
2544 if ((HOST_WIDE_INT) orig_t < 0)
2546 /* Shift ORIG_T as follows because a right shift of a
2547 negative-valued signed type is implementation
2548 defined. */
2549 q = ~(~orig_t >> m);
2550 /* The function expand_shift will choose between a shift
2551 and a sequence of additions, so the observed cost is
2552 given as MIN (m * add_cost(speed, mode),
2553 shift_cost(speed, mode, m)). */
2554 op_cost = m * add_cost (speed, mode);
2555 if (shift_cost (speed, mode, m) < op_cost)
2556 op_cost = shift_cost (speed, mode, m);
2557 new_limit.cost = best_cost.cost - op_cost;
2558 new_limit.latency = best_cost.latency - op_cost;
2559 synth_mult (alg_in, q, &new_limit, mode);
2561 alg_in->cost.cost += op_cost;
2562 alg_in->cost.latency += op_cost;
2563 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2565 struct algorithm *x;
2566 best_cost = alg_in->cost;
2567 x = alg_in, alg_in = best_alg, best_alg = x;
2568 best_alg->log[best_alg->ops] = m;
2569 best_alg->op[best_alg->ops] = alg_shift;
2573 if (cache_hit)
2574 goto done;
2577 /* If we have an odd number, add or subtract one. */
2578 if ((t & 1) != 0)
2580 unsigned HOST_WIDE_INT w;
2582 do_alg_addsub_t_m2:
2583 for (w = 1; (w & t) != 0; w <<= 1)
2585 /* If T was -1, then W will be zero after the loop. This is another
2586 case where T ends with ...111. Handling this with (T + 1) and
2587 subtract 1 produces slightly better code and results in algorithm
2588 selection much faster than treating it like the ...0111 case
2589 below. */
2590 if (w == 0
2591 || (w > 2
2592 /* Reject the case where t is 3.
2593 Thus we prefer addition in that case. */
2594 && t != 3))
2596 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2598 op_cost = add_cost (speed, mode);
2599 new_limit.cost = best_cost.cost - op_cost;
2600 new_limit.latency = best_cost.latency - op_cost;
2601 synth_mult (alg_in, t + 1, &new_limit, mode);
2603 alg_in->cost.cost += op_cost;
2604 alg_in->cost.latency += op_cost;
2605 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2607 struct algorithm *x;
2608 best_cost = alg_in->cost;
2609 x = alg_in, alg_in = best_alg, best_alg = x;
2610 best_alg->log[best_alg->ops] = 0;
2611 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2614 else
2616 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2618 op_cost = add_cost (speed, mode);
2619 new_limit.cost = best_cost.cost - op_cost;
2620 new_limit.latency = best_cost.latency - op_cost;
2621 synth_mult (alg_in, t - 1, &new_limit, mode);
2623 alg_in->cost.cost += op_cost;
2624 alg_in->cost.latency += op_cost;
2625 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2627 struct algorithm *x;
2628 best_cost = alg_in->cost;
2629 x = alg_in, alg_in = best_alg, best_alg = x;
2630 best_alg->log[best_alg->ops] = 0;
2631 best_alg->op[best_alg->ops] = alg_add_t_m2;
2635 /* We may be able to calculate a * -7, a * -15, a * -31, etc
2636 quickly with a - a * n for some appropriate constant n. */
2637 m = exact_log2 (-orig_t + 1);
2638 if (m >= 0 && m < maxm)
2640 op_cost = shiftsub1_cost (speed, mode, m);
2641 new_limit.cost = best_cost.cost - op_cost;
2642 new_limit.latency = best_cost.latency - op_cost;
2643 synth_mult (alg_in, (unsigned HOST_WIDE_INT) (-orig_t + 1) >> m,
2644 &new_limit, mode);
2646 alg_in->cost.cost += 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_sub_t_m2;
2658 if (cache_hit)
2659 goto done;
2662 /* Look for factors of t of the form
2663 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2664 If we find such a factor, we can multiply by t using an algorithm that
2665 multiplies by q, shift the result by m and add/subtract it to itself.
2667 We search for large factors first and loop down, even if large factors
2668 are less probable than small; if we find a large factor we will find a
2669 good sequence quickly, and therefore be able to prune (by decreasing
2670 COST_LIMIT) the search. */
2672 do_alg_addsub_factor:
2673 for (m = floor_log2 (t - 1); m >= 2; m--)
2675 unsigned HOST_WIDE_INT d;
2677 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2678 if (t % d == 0 && t > d && m < maxm
2679 && (!cache_hit || cache_alg == alg_add_factor))
2681 /* If the target has a cheap shift-and-add instruction use
2682 that in preference to a shift insn followed by an add insn.
2683 Assume that the shift-and-add is "atomic" with a latency
2684 equal to its cost, otherwise assume that on superscalar
2685 hardware the shift may be executed concurrently with the
2686 earlier steps in the algorithm. */
2687 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2688 if (shiftadd_cost (speed, mode, m) < op_cost)
2690 op_cost = shiftadd_cost (speed, mode, m);
2691 op_latency = op_cost;
2693 else
2694 op_latency = add_cost (speed, mode);
2696 new_limit.cost = best_cost.cost - op_cost;
2697 new_limit.latency = best_cost.latency - op_latency;
2698 synth_mult (alg_in, t / d, &new_limit, mode);
2700 alg_in->cost.cost += op_cost;
2701 alg_in->cost.latency += op_latency;
2702 if (alg_in->cost.latency < op_cost)
2703 alg_in->cost.latency = op_cost;
2704 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2706 struct algorithm *x;
2707 best_cost = alg_in->cost;
2708 x = alg_in, alg_in = best_alg, best_alg = x;
2709 best_alg->log[best_alg->ops] = m;
2710 best_alg->op[best_alg->ops] = alg_add_factor;
2712 /* Other factors will have been taken care of in the recursion. */
2713 break;
2716 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2717 if (t % d == 0 && t > d && m < maxm
2718 && (!cache_hit || cache_alg == alg_sub_factor))
2720 /* If the target has a cheap shift-and-subtract insn use
2721 that in preference to a shift insn followed by a sub insn.
2722 Assume that the shift-and-sub is "atomic" with a latency
2723 equal to it's cost, otherwise assume that on superscalar
2724 hardware the shift may be executed concurrently with the
2725 earlier steps in the algorithm. */
2726 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2727 if (shiftsub0_cost (speed, mode, m) < op_cost)
2729 op_cost = shiftsub0_cost (speed, mode, m);
2730 op_latency = op_cost;
2732 else
2733 op_latency = add_cost (speed, mode);
2735 new_limit.cost = best_cost.cost - op_cost;
2736 new_limit.latency = best_cost.latency - op_latency;
2737 synth_mult (alg_in, t / d, &new_limit, mode);
2739 alg_in->cost.cost += op_cost;
2740 alg_in->cost.latency += op_latency;
2741 if (alg_in->cost.latency < 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_factor;
2751 break;
2754 if (cache_hit)
2755 goto done;
2757 /* Try shift-and-add (load effective address) instructions,
2758 i.e. do a*3, a*5, a*9. */
2759 if ((t & 1) != 0)
2761 do_alg_add_t2_m:
2762 q = t - 1;
2763 q = q & -q;
2764 m = exact_log2 (q);
2765 if (m >= 0 && m < maxm)
2767 op_cost = shiftadd_cost (speed, mode, m);
2768 new_limit.cost = best_cost.cost - op_cost;
2769 new_limit.latency = best_cost.latency - op_cost;
2770 synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2772 alg_in->cost.cost += op_cost;
2773 alg_in->cost.latency += op_cost;
2774 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2776 struct algorithm *x;
2777 best_cost = alg_in->cost;
2778 x = alg_in, alg_in = best_alg, best_alg = x;
2779 best_alg->log[best_alg->ops] = m;
2780 best_alg->op[best_alg->ops] = alg_add_t2_m;
2783 if (cache_hit)
2784 goto done;
2786 do_alg_sub_t2_m:
2787 q = t + 1;
2788 q = q & -q;
2789 m = exact_log2 (q);
2790 if (m >= 0 && m < maxm)
2792 op_cost = shiftsub0_cost (speed, mode, m);
2793 new_limit.cost = best_cost.cost - op_cost;
2794 new_limit.latency = best_cost.latency - op_cost;
2795 synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2797 alg_in->cost.cost += op_cost;
2798 alg_in->cost.latency += op_cost;
2799 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2801 struct algorithm *x;
2802 best_cost = alg_in->cost;
2803 x = alg_in, alg_in = best_alg, best_alg = x;
2804 best_alg->log[best_alg->ops] = m;
2805 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2808 if (cache_hit)
2809 goto done;
2812 done:
2813 /* If best_cost has not decreased, we have not found any algorithm. */
2814 if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
2816 /* We failed to find an algorithm. Record alg_impossible for
2817 this case (that is, <T, MODE, COST_LIMIT>) so that next time
2818 we are asked to find an algorithm for T within the same or
2819 lower COST_LIMIT, we can immediately return to the
2820 caller. */
2821 entry_ptr->t = t;
2822 entry_ptr->mode = mode;
2823 entry_ptr->speed = speed;
2824 entry_ptr->alg = alg_impossible;
2825 entry_ptr->cost = *cost_limit;
2826 return;
2829 /* Cache the result. */
2830 if (!cache_hit)
2832 entry_ptr->t = t;
2833 entry_ptr->mode = mode;
2834 entry_ptr->speed = speed;
2835 entry_ptr->alg = best_alg->op[best_alg->ops];
2836 entry_ptr->cost.cost = best_cost.cost;
2837 entry_ptr->cost.latency = best_cost.latency;
2840 /* If we are getting a too long sequence for `struct algorithm'
2841 to record, make this search fail. */
2842 if (best_alg->ops == MAX_BITS_PER_WORD)
2843 return;
2845 /* Copy the algorithm from temporary space to the space at alg_out.
2846 We avoid using structure assignment because the majority of
2847 best_alg is normally undefined, and this is a critical function. */
2848 alg_out->ops = best_alg->ops + 1;
2849 alg_out->cost = best_cost;
2850 memcpy (alg_out->op, best_alg->op,
2851 alg_out->ops * sizeof *alg_out->op);
2852 memcpy (alg_out->log, best_alg->log,
2853 alg_out->ops * sizeof *alg_out->log);
2856 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2857 Try three variations:
2859 - a shift/add sequence based on VAL itself
2860 - a shift/add sequence based on -VAL, followed by a negation
2861 - a shift/add sequence based on VAL - 1, followed by an addition.
2863 Return true if the cheapest of these cost less than MULT_COST,
2864 describing the algorithm in *ALG and final fixup in *VARIANT. */
2866 static bool
2867 choose_mult_variant (enum machine_mode mode, HOST_WIDE_INT val,
2868 struct algorithm *alg, enum mult_variant *variant,
2869 int mult_cost)
2871 struct algorithm alg2;
2872 struct mult_cost limit;
2873 int op_cost;
2874 bool speed = optimize_insn_for_speed_p ();
2876 /* Fail quickly for impossible bounds. */
2877 if (mult_cost < 0)
2878 return false;
2880 /* Ensure that mult_cost provides a reasonable upper bound.
2881 Any constant multiplication can be performed with less
2882 than 2 * bits additions. */
2883 op_cost = 2 * GET_MODE_UNIT_BITSIZE (mode) * add_cost (speed, mode);
2884 if (mult_cost > op_cost)
2885 mult_cost = op_cost;
2887 *variant = basic_variant;
2888 limit.cost = mult_cost;
2889 limit.latency = mult_cost;
2890 synth_mult (alg, val, &limit, mode);
2892 /* This works only if the inverted value actually fits in an
2893 `unsigned int' */
2894 if (HOST_BITS_PER_INT >= GET_MODE_UNIT_BITSIZE (mode))
2896 op_cost = neg_cost (speed, mode);
2897 if (MULT_COST_LESS (&alg->cost, mult_cost))
2899 limit.cost = alg->cost.cost - op_cost;
2900 limit.latency = alg->cost.latency - op_cost;
2902 else
2904 limit.cost = mult_cost - op_cost;
2905 limit.latency = mult_cost - op_cost;
2908 synth_mult (&alg2, -val, &limit, mode);
2909 alg2.cost.cost += op_cost;
2910 alg2.cost.latency += op_cost;
2911 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2912 *alg = alg2, *variant = negate_variant;
2915 /* This proves very useful for division-by-constant. */
2916 op_cost = add_cost (speed, mode);
2917 if (MULT_COST_LESS (&alg->cost, mult_cost))
2919 limit.cost = alg->cost.cost - op_cost;
2920 limit.latency = alg->cost.latency - op_cost;
2922 else
2924 limit.cost = mult_cost - op_cost;
2925 limit.latency = mult_cost - op_cost;
2928 synth_mult (&alg2, val - 1, &limit, mode);
2929 alg2.cost.cost += op_cost;
2930 alg2.cost.latency += op_cost;
2931 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2932 *alg = alg2, *variant = add_variant;
2934 return MULT_COST_LESS (&alg->cost, mult_cost);
2937 /* A subroutine of expand_mult, used for constant multiplications.
2938 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2939 convenient. Use the shift/add sequence described by ALG and apply
2940 the final fixup specified by VARIANT. */
2942 static rtx
2943 expand_mult_const (enum machine_mode mode, rtx op0, HOST_WIDE_INT val,
2944 rtx target, const struct algorithm *alg,
2945 enum mult_variant variant)
2947 HOST_WIDE_INT val_so_far;
2948 rtx insn, accum, tem;
2949 int opno;
2950 enum machine_mode nmode;
2952 /* Avoid referencing memory over and over and invalid sharing
2953 on SUBREGs. */
2954 op0 = force_reg (mode, op0);
2956 /* ACCUM starts out either as OP0 or as a zero, depending on
2957 the first operation. */
2959 if (alg->op[0] == alg_zero)
2961 accum = copy_to_mode_reg (mode, CONST0_RTX (mode));
2962 val_so_far = 0;
2964 else if (alg->op[0] == alg_m)
2966 accum = copy_to_mode_reg (mode, op0);
2967 val_so_far = 1;
2969 else
2970 gcc_unreachable ();
2972 for (opno = 1; opno < alg->ops; opno++)
2974 int log = alg->log[opno];
2975 rtx shift_subtarget = optimize ? 0 : accum;
2976 rtx add_target
2977 = (opno == alg->ops - 1 && target != 0 && variant != add_variant
2978 && !optimize)
2979 ? target : 0;
2980 rtx accum_target = optimize ? 0 : accum;
2981 rtx accum_inner;
2983 switch (alg->op[opno])
2985 case alg_shift:
2986 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
2987 /* REG_EQUAL note will be attached to the following insn. */
2988 emit_move_insn (accum, tem);
2989 val_so_far <<= log;
2990 break;
2992 case alg_add_t_m2:
2993 tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
2994 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2995 add_target ? add_target : accum_target);
2996 val_so_far += (HOST_WIDE_INT) 1 << log;
2997 break;
2999 case alg_sub_t_m2:
3000 tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
3001 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
3002 add_target ? add_target : accum_target);
3003 val_so_far -= (HOST_WIDE_INT) 1 << log;
3004 break;
3006 case alg_add_t2_m:
3007 accum = expand_shift (LSHIFT_EXPR, mode, accum,
3008 log, shift_subtarget, 0);
3009 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
3010 add_target ? add_target : accum_target);
3011 val_so_far = (val_so_far << log) + 1;
3012 break;
3014 case alg_sub_t2_m:
3015 accum = expand_shift (LSHIFT_EXPR, mode, accum,
3016 log, shift_subtarget, 0);
3017 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
3018 add_target ? add_target : accum_target);
3019 val_so_far = (val_so_far << log) - 1;
3020 break;
3022 case alg_add_factor:
3023 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
3024 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
3025 add_target ? add_target : accum_target);
3026 val_so_far += val_so_far << log;
3027 break;
3029 case alg_sub_factor:
3030 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
3031 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
3032 (add_target
3033 ? add_target : (optimize ? 0 : tem)));
3034 val_so_far = (val_so_far << log) - val_so_far;
3035 break;
3037 default:
3038 gcc_unreachable ();
3041 if (SCALAR_INT_MODE_P (mode))
3043 /* Write a REG_EQUAL note on the last insn so that we can cse
3044 multiplication sequences. Note that if ACCUM is a SUBREG,
3045 we've set the inner register and must properly indicate that. */
3046 tem = op0, nmode = mode;
3047 accum_inner = accum;
3048 if (GET_CODE (accum) == SUBREG)
3050 accum_inner = SUBREG_REG (accum);
3051 nmode = GET_MODE (accum_inner);
3052 tem = gen_lowpart (nmode, op0);
3055 insn = get_last_insn ();
3056 set_dst_reg_note (insn, REG_EQUAL,
3057 gen_rtx_MULT (nmode, tem,
3058 gen_int_mode (val_so_far, nmode)),
3059 accum_inner);
3063 if (variant == negate_variant)
3065 val_so_far = -val_so_far;
3066 accum = expand_unop (mode, neg_optab, accum, target, 0);
3068 else if (variant == add_variant)
3070 val_so_far = val_so_far + 1;
3071 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
3074 /* Compare only the bits of val and val_so_far that are significant
3075 in the result mode, to avoid sign-/zero-extension confusion. */
3076 nmode = GET_MODE_INNER (mode);
3077 if (nmode == VOIDmode)
3078 nmode = mode;
3079 val &= GET_MODE_MASK (nmode);
3080 val_so_far &= GET_MODE_MASK (nmode);
3081 gcc_assert (val == val_so_far);
3083 return accum;
3086 /* Perform a multiplication and return an rtx for the result.
3087 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3088 TARGET is a suggestion for where to store the result (an rtx).
3090 We check specially for a constant integer as OP1.
3091 If you want this check for OP0 as well, then before calling
3092 you should swap the two operands if OP0 would be constant. */
3095 expand_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3096 int unsignedp)
3098 enum mult_variant variant;
3099 struct algorithm algorithm;
3100 rtx scalar_op1;
3101 int max_cost;
3102 bool speed = optimize_insn_for_speed_p ();
3103 bool do_trapv = flag_trapv && SCALAR_INT_MODE_P (mode) && !unsignedp;
3105 if (CONSTANT_P (op0))
3107 rtx temp = op0;
3108 op0 = op1;
3109 op1 = temp;
3112 /* For vectors, there are several simplifications that can be made if
3113 all elements of the vector constant are identical. */
3114 scalar_op1 = op1;
3115 if (GET_CODE (op1) == CONST_VECTOR)
3117 int i, n = CONST_VECTOR_NUNITS (op1);
3118 scalar_op1 = CONST_VECTOR_ELT (op1, 0);
3119 for (i = 1; i < n; ++i)
3120 if (!rtx_equal_p (scalar_op1, CONST_VECTOR_ELT (op1, i)))
3121 goto skip_scalar;
3124 if (INTEGRAL_MODE_P (mode))
3126 rtx fake_reg;
3127 HOST_WIDE_INT coeff;
3128 bool is_neg;
3129 int mode_bitsize;
3131 if (op1 == CONST0_RTX (mode))
3132 return op1;
3133 if (op1 == CONST1_RTX (mode))
3134 return op0;
3135 if (op1 == CONSTM1_RTX (mode))
3136 return expand_unop (mode, do_trapv ? negv_optab : neg_optab,
3137 op0, target, 0);
3139 if (do_trapv)
3140 goto skip_synth;
3142 /* If mode is integer vector mode, check if the backend supports
3143 vector lshift (by scalar or vector) at all. If not, we can't use
3144 synthetized multiply. */
3145 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
3146 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing
3147 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing)
3148 goto skip_synth;
3150 /* These are the operations that are potentially turned into
3151 a sequence of shifts and additions. */
3152 mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
3154 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3155 less than or equal in size to `unsigned int' this doesn't matter.
3156 If the mode is larger than `unsigned int', then synth_mult works
3157 only if the constant value exactly fits in an `unsigned int' without
3158 any truncation. This means that multiplying by negative values does
3159 not work; results are off by 2^32 on a 32 bit machine. */
3161 if (CONST_INT_P (scalar_op1))
3163 coeff = INTVAL (scalar_op1);
3164 is_neg = coeff < 0;
3166 else if (CONST_DOUBLE_AS_INT_P (scalar_op1))
3168 /* If we are multiplying in DImode, it may still be a win
3169 to try to work with shifts and adds. */
3170 if (CONST_DOUBLE_HIGH (scalar_op1) == 0
3171 && (CONST_DOUBLE_LOW (scalar_op1) > 0
3172 || (CONST_DOUBLE_LOW (scalar_op1) < 0
3173 && EXACT_POWER_OF_2_OR_ZERO_P
3174 (CONST_DOUBLE_LOW (scalar_op1)))))
3176 coeff = CONST_DOUBLE_LOW (scalar_op1);
3177 is_neg = false;
3179 else if (CONST_DOUBLE_LOW (scalar_op1) == 0)
3181 coeff = CONST_DOUBLE_HIGH (scalar_op1);
3182 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3184 int shift = floor_log2 (coeff) + HOST_BITS_PER_WIDE_INT;
3185 if (shift < HOST_BITS_PER_DOUBLE_INT - 1
3186 || mode_bitsize <= HOST_BITS_PER_DOUBLE_INT)
3187 return expand_shift (LSHIFT_EXPR, mode, op0,
3188 shift, target, unsignedp);
3190 goto skip_synth;
3192 else
3193 goto skip_synth;
3195 else
3196 goto skip_synth;
3198 /* We used to test optimize here, on the grounds that it's better to
3199 produce a smaller program when -O is not used. But this causes
3200 such a terrible slowdown sometimes that it seems better to always
3201 use synth_mult. */
3203 /* Special case powers of two. */
3204 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff)
3205 && !(is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT))
3206 return expand_shift (LSHIFT_EXPR, mode, op0,
3207 floor_log2 (coeff), target, unsignedp);
3209 fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3211 /* Attempt to handle multiplication of DImode values by negative
3212 coefficients, by performing the multiplication by a positive
3213 multiplier and then inverting the result. */
3214 if (is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT)
3216 /* Its safe to use -coeff even for INT_MIN, as the
3217 result is interpreted as an unsigned coefficient.
3218 Exclude cost of op0 from max_cost to match the cost
3219 calculation of the synth_mult. */
3220 coeff = -(unsigned HOST_WIDE_INT) coeff;
3221 max_cost = (set_src_cost (gen_rtx_MULT (mode, fake_reg, op1), speed)
3222 - neg_cost (speed, mode));
3223 if (max_cost <= 0)
3224 goto skip_synth;
3226 /* Special case powers of two. */
3227 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3229 rtx temp = expand_shift (LSHIFT_EXPR, mode, op0,
3230 floor_log2 (coeff), target, unsignedp);
3231 return expand_unop (mode, neg_optab, temp, target, 0);
3234 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3235 max_cost))
3237 rtx temp = expand_mult_const (mode, op0, coeff, NULL_RTX,
3238 &algorithm, variant);
3239 return expand_unop (mode, neg_optab, temp, target, 0);
3241 goto skip_synth;
3244 /* Exclude cost of op0 from max_cost to match the cost
3245 calculation of the synth_mult. */
3246 max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, op1), speed);
3247 if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
3248 return expand_mult_const (mode, op0, coeff, target,
3249 &algorithm, variant);
3251 skip_synth:
3253 /* Expand x*2.0 as x+x. */
3254 if (CONST_DOUBLE_AS_FLOAT_P (scalar_op1))
3256 REAL_VALUE_TYPE d;
3257 REAL_VALUE_FROM_CONST_DOUBLE (d, scalar_op1);
3259 if (REAL_VALUES_EQUAL (d, dconst2))
3261 op0 = force_reg (GET_MODE (op0), op0);
3262 return expand_binop (mode, add_optab, op0, op0,
3263 target, unsignedp, OPTAB_LIB_WIDEN);
3266 skip_scalar:
3268 /* This used to use umul_optab if unsigned, but for non-widening multiply
3269 there is no difference between signed and unsigned. */
3270 op0 = expand_binop (mode, do_trapv ? smulv_optab : smul_optab,
3271 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
3272 gcc_assert (op0);
3273 return op0;
3276 /* Return a cost estimate for multiplying a register by the given
3277 COEFFicient in the given MODE and SPEED. */
3280 mult_by_coeff_cost (HOST_WIDE_INT coeff, enum machine_mode mode, bool speed)
3282 int max_cost;
3283 struct algorithm algorithm;
3284 enum mult_variant variant;
3286 rtx fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3287 max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, fake_reg), speed);
3288 if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
3289 return algorithm.cost.cost;
3290 else
3291 return max_cost;
3294 /* Perform a widening multiplication and return an rtx for the result.
3295 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3296 TARGET is a suggestion for where to store the result (an rtx).
3297 THIS_OPTAB is the optab we should use, it must be either umul_widen_optab
3298 or smul_widen_optab.
3300 We check specially for a constant integer as OP1, comparing the
3301 cost of a widening multiply against the cost of a sequence of shifts
3302 and adds. */
3305 expand_widening_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3306 int unsignedp, optab this_optab)
3308 bool speed = optimize_insn_for_speed_p ();
3309 rtx cop1;
3311 if (CONST_INT_P (op1)
3312 && GET_MODE (op0) != VOIDmode
3313 && (cop1 = convert_modes (mode, GET_MODE (op0), op1,
3314 this_optab == umul_widen_optab))
3315 && CONST_INT_P (cop1)
3316 && (INTVAL (cop1) >= 0
3317 || HWI_COMPUTABLE_MODE_P (mode)))
3319 HOST_WIDE_INT coeff = INTVAL (cop1);
3320 int max_cost;
3321 enum mult_variant variant;
3322 struct algorithm algorithm;
3324 if (coeff == 0)
3325 return CONST0_RTX (mode);
3327 /* Special case powers of two. */
3328 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3330 op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
3331 return expand_shift (LSHIFT_EXPR, mode, op0,
3332 floor_log2 (coeff), target, unsignedp);
3335 /* Exclude cost of op0 from max_cost to match the cost
3336 calculation of the synth_mult. */
3337 max_cost = mul_widen_cost (speed, mode);
3338 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3339 max_cost))
3341 op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
3342 return expand_mult_const (mode, op0, coeff, target,
3343 &algorithm, variant);
3346 return expand_binop (mode, this_optab, op0, op1, target,
3347 unsignedp, OPTAB_LIB_WIDEN);
3350 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3351 replace division by D, and put the least significant N bits of the result
3352 in *MULTIPLIER_PTR and return the most significant bit.
3354 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3355 needed precision is in PRECISION (should be <= N).
3357 PRECISION should be as small as possible so this function can choose
3358 multiplier more freely.
3360 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3361 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3363 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3364 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3366 unsigned HOST_WIDE_INT
3367 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
3368 unsigned HOST_WIDE_INT *multiplier_ptr,
3369 int *post_shift_ptr, int *lgup_ptr)
3371 double_int mhigh, mlow;
3372 int lgup, post_shift;
3373 int pow, pow2;
3375 /* lgup = ceil(log2(divisor)); */
3376 lgup = ceil_log2 (d);
3378 gcc_assert (lgup <= n);
3380 pow = n + lgup;
3381 pow2 = n + lgup - precision;
3383 /* We could handle this with some effort, but this case is much
3384 better handled directly with a scc insn, so rely on caller using
3385 that. */
3386 gcc_assert (pow != HOST_BITS_PER_DOUBLE_INT);
3388 /* mlow = 2^(N + lgup)/d */
3389 double_int val = double_int_zero.set_bit (pow);
3390 mlow = val.div (double_int::from_uhwi (d), true, TRUNC_DIV_EXPR);
3392 /* mhigh = (2^(N + lgup) + 2^(N + lgup - precision))/d */
3393 val |= double_int_zero.set_bit (pow2);
3394 mhigh = val.div (double_int::from_uhwi (d), true, TRUNC_DIV_EXPR);
3396 gcc_assert (!mhigh.high || val.high - d < d);
3397 gcc_assert (mhigh.high <= 1 && mlow.high <= 1);
3398 /* Assert that mlow < mhigh. */
3399 gcc_assert (mlow.ult (mhigh));
3401 /* If precision == N, then mlow, mhigh exceed 2^N
3402 (but they do not exceed 2^(N+1)). */
3404 /* Reduce to lowest terms. */
3405 for (post_shift = lgup; post_shift > 0; post_shift--)
3407 int shft = HOST_BITS_PER_WIDE_INT - 1;
3408 unsigned HOST_WIDE_INT ml_lo = (mlow.high << shft) | (mlow.low >> 1);
3409 unsigned HOST_WIDE_INT mh_lo = (mhigh.high << shft) | (mhigh.low >> 1);
3410 if (ml_lo >= mh_lo)
3411 break;
3413 mlow = double_int::from_uhwi (ml_lo);
3414 mhigh = double_int::from_uhwi (mh_lo);
3417 *post_shift_ptr = post_shift;
3418 *lgup_ptr = lgup;
3419 if (n < HOST_BITS_PER_WIDE_INT)
3421 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
3422 *multiplier_ptr = mhigh.low & mask;
3423 return mhigh.low >= mask;
3425 else
3427 *multiplier_ptr = mhigh.low;
3428 return mhigh.high;
3432 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3433 congruent to 1 (mod 2**N). */
3435 static unsigned HOST_WIDE_INT
3436 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
3438 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3440 /* The algorithm notes that the choice y = x satisfies
3441 x*y == 1 mod 2^3, since x is assumed odd.
3442 Each iteration doubles the number of bits of significance in y. */
3444 unsigned HOST_WIDE_INT mask;
3445 unsigned HOST_WIDE_INT y = x;
3446 int nbit = 3;
3448 mask = (n == HOST_BITS_PER_WIDE_INT
3449 ? ~(unsigned HOST_WIDE_INT) 0
3450 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
3452 while (nbit < n)
3454 y = y * (2 - x*y) & mask; /* Modulo 2^N */
3455 nbit *= 2;
3457 return y;
3460 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3461 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3462 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3463 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3464 become signed.
3466 The result is put in TARGET if that is convenient.
3468 MODE is the mode of operation. */
3471 expand_mult_highpart_adjust (enum machine_mode mode, rtx adj_operand, rtx op0,
3472 rtx op1, rtx target, int unsignedp)
3474 rtx tem;
3475 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3477 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3478 GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
3479 tem = expand_and (mode, tem, op1, NULL_RTX);
3480 adj_operand
3481 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3482 adj_operand);
3484 tem = expand_shift (RSHIFT_EXPR, mode, op1,
3485 GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
3486 tem = expand_and (mode, tem, op0, NULL_RTX);
3487 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3488 target);
3490 return target;
3493 /* Subroutine of expmed_mult_highpart. Return the MODE high part of OP. */
3495 static rtx
3496 extract_high_half (enum machine_mode mode, rtx op)
3498 enum machine_mode wider_mode;
3500 if (mode == word_mode)
3501 return gen_highpart (mode, op);
3503 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3505 wider_mode = GET_MODE_WIDER_MODE (mode);
3506 op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3507 GET_MODE_BITSIZE (mode), 0, 1);
3508 return convert_modes (mode, wider_mode, op, 0);
3511 /* Like expmed_mult_highpart, but only consider using a multiplication
3512 optab. OP1 is an rtx for the constant operand. */
3514 static rtx
3515 expmed_mult_highpart_optab (enum machine_mode mode, rtx op0, rtx op1,
3516 rtx target, int unsignedp, int max_cost)
3518 rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3519 enum machine_mode wider_mode;
3520 optab moptab;
3521 rtx tem;
3522 int size;
3523 bool speed = optimize_insn_for_speed_p ();
3525 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3527 wider_mode = GET_MODE_WIDER_MODE (mode);
3528 size = GET_MODE_BITSIZE (mode);
3530 /* Firstly, try using a multiplication insn that only generates the needed
3531 high part of the product, and in the sign flavor of unsignedp. */
3532 if (mul_highpart_cost (speed, mode) < max_cost)
3534 moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3535 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3536 unsignedp, OPTAB_DIRECT);
3537 if (tem)
3538 return tem;
3541 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3542 Need to adjust the result after the multiplication. */
3543 if (size - 1 < BITS_PER_WORD
3544 && (mul_highpart_cost (speed, mode)
3545 + 2 * shift_cost (speed, mode, size-1)
3546 + 4 * add_cost (speed, mode) < max_cost))
3548 moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3549 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3550 unsignedp, OPTAB_DIRECT);
3551 if (tem)
3552 /* We used the wrong signedness. Adjust the result. */
3553 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3554 tem, unsignedp);
3557 /* Try widening multiplication. */
3558 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3559 if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
3560 && mul_widen_cost (speed, wider_mode) < max_cost)
3562 tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3563 unsignedp, OPTAB_WIDEN);
3564 if (tem)
3565 return extract_high_half (mode, tem);
3568 /* Try widening the mode and perform a non-widening multiplication. */
3569 if (optab_handler (smul_optab, wider_mode) != CODE_FOR_nothing
3570 && size - 1 < BITS_PER_WORD
3571 && (mul_cost (speed, wider_mode) + shift_cost (speed, mode, size-1)
3572 < max_cost))
3574 rtx insns, wop0, wop1;
3576 /* We need to widen the operands, for example to ensure the
3577 constant multiplier is correctly sign or zero extended.
3578 Use a sequence to clean-up any instructions emitted by
3579 the conversions if things don't work out. */
3580 start_sequence ();
3581 wop0 = convert_modes (wider_mode, mode, op0, unsignedp);
3582 wop1 = convert_modes (wider_mode, mode, op1, unsignedp);
3583 tem = expand_binop (wider_mode, smul_optab, wop0, wop1, 0,
3584 unsignedp, OPTAB_WIDEN);
3585 insns = get_insns ();
3586 end_sequence ();
3588 if (tem)
3590 emit_insn (insns);
3591 return extract_high_half (mode, tem);
3595 /* Try widening multiplication of opposite signedness, and adjust. */
3596 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3597 if (widening_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
3598 && size - 1 < BITS_PER_WORD
3599 && (mul_widen_cost (speed, wider_mode)
3600 + 2 * shift_cost (speed, mode, size-1)
3601 + 4 * add_cost (speed, mode) < max_cost))
3603 tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3604 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3605 if (tem != 0)
3607 tem = extract_high_half (mode, tem);
3608 /* We used the wrong signedness. Adjust the result. */
3609 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3610 target, unsignedp);
3614 return 0;
3617 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3618 putting the high half of the result in TARGET if that is convenient,
3619 and return where the result is. If the operation can not be performed,
3620 0 is returned.
3622 MODE is the mode of operation and result.
3624 UNSIGNEDP nonzero means unsigned multiply.
3626 MAX_COST is the total allowed cost for the expanded RTL. */
3628 static rtx
3629 expmed_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
3630 rtx target, int unsignedp, int max_cost)
3632 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
3633 unsigned HOST_WIDE_INT cnst1;
3634 int extra_cost;
3635 bool sign_adjust = false;
3636 enum mult_variant variant;
3637 struct algorithm alg;
3638 rtx tem;
3639 bool speed = optimize_insn_for_speed_p ();
3641 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3642 /* We can't support modes wider than HOST_BITS_PER_INT. */
3643 gcc_assert (HWI_COMPUTABLE_MODE_P (mode));
3645 cnst1 = INTVAL (op1) & GET_MODE_MASK (mode);
3647 /* We can't optimize modes wider than BITS_PER_WORD.
3648 ??? We might be able to perform double-word arithmetic if
3649 mode == word_mode, however all the cost calculations in
3650 synth_mult etc. assume single-word operations. */
3651 if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3652 return expmed_mult_highpart_optab (mode, op0, op1, target,
3653 unsignedp, max_cost);
3655 extra_cost = shift_cost (speed, mode, GET_MODE_BITSIZE (mode) - 1);
3657 /* Check whether we try to multiply by a negative constant. */
3658 if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3660 sign_adjust = true;
3661 extra_cost += add_cost (speed, mode);
3664 /* See whether shift/add multiplication is cheap enough. */
3665 if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3666 max_cost - extra_cost))
3668 /* See whether the specialized multiplication optabs are
3669 cheaper than the shift/add version. */
3670 tem = expmed_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3671 alg.cost.cost + extra_cost);
3672 if (tem)
3673 return tem;
3675 tem = convert_to_mode (wider_mode, op0, unsignedp);
3676 tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3677 tem = extract_high_half (mode, tem);
3679 /* Adjust result for signedness. */
3680 if (sign_adjust)
3681 tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3683 return tem;
3685 return expmed_mult_highpart_optab (mode, op0, op1, target,
3686 unsignedp, max_cost);
3690 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3692 static rtx
3693 expand_smod_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3695 unsigned HOST_WIDE_INT masklow, maskhigh;
3696 rtx result, temp, shift, label;
3697 int logd;
3699 logd = floor_log2 (d);
3700 result = gen_reg_rtx (mode);
3702 /* Avoid conditional branches when they're expensive. */
3703 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2
3704 && optimize_insn_for_speed_p ())
3706 rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3707 mode, 0, -1);
3708 if (signmask)
3710 signmask = force_reg (mode, signmask);
3711 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3712 shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3714 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3715 which instruction sequence to use. If logical right shifts
3716 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3717 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3719 temp = gen_rtx_LSHIFTRT (mode, result, shift);
3720 if (optab_handler (lshr_optab, mode) == CODE_FOR_nothing
3721 || (set_src_cost (temp, optimize_insn_for_speed_p ())
3722 > COSTS_N_INSNS (2)))
3724 temp = expand_binop (mode, xor_optab, op0, signmask,
3725 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3726 temp = expand_binop (mode, sub_optab, temp, signmask,
3727 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3728 temp = expand_binop (mode, and_optab, temp,
3729 gen_int_mode (masklow, mode),
3730 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3731 temp = expand_binop (mode, xor_optab, temp, signmask,
3732 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3733 temp = expand_binop (mode, sub_optab, temp, signmask,
3734 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3736 else
3738 signmask = expand_binop (mode, lshr_optab, signmask, shift,
3739 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3740 signmask = force_reg (mode, signmask);
3742 temp = expand_binop (mode, add_optab, op0, signmask,
3743 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3744 temp = expand_binop (mode, and_optab, temp,
3745 gen_int_mode (masklow, mode),
3746 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3747 temp = expand_binop (mode, sub_optab, temp, signmask,
3748 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3750 return temp;
3754 /* Mask contains the mode's signbit and the significant bits of the
3755 modulus. By including the signbit in the operation, many targets
3756 can avoid an explicit compare operation in the following comparison
3757 against zero. */
3759 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3760 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3762 masklow |= HOST_WIDE_INT_M1U << (GET_MODE_BITSIZE (mode) - 1);
3763 maskhigh = -1;
3765 else
3766 maskhigh = HOST_WIDE_INT_M1U
3767 << (GET_MODE_BITSIZE (mode) - HOST_BITS_PER_WIDE_INT - 1);
3769 temp = expand_binop (mode, and_optab, op0,
3770 immed_double_const (masklow, maskhigh, mode),
3771 result, 1, OPTAB_LIB_WIDEN);
3772 if (temp != result)
3773 emit_move_insn (result, temp);
3775 label = gen_label_rtx ();
3776 do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3778 temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3779 0, OPTAB_LIB_WIDEN);
3780 masklow = HOST_WIDE_INT_M1U << logd;
3781 maskhigh = -1;
3782 temp = expand_binop (mode, ior_optab, temp,
3783 immed_double_const (masklow, maskhigh, mode),
3784 result, 1, OPTAB_LIB_WIDEN);
3785 temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3786 0, OPTAB_LIB_WIDEN);
3787 if (temp != result)
3788 emit_move_insn (result, temp);
3789 emit_label (label);
3790 return result;
3793 /* Expand signed division of OP0 by a power of two D in mode MODE.
3794 This routine is only called for positive values of D. */
3796 static rtx
3797 expand_sdiv_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3799 rtx temp, label;
3800 int logd;
3802 logd = floor_log2 (d);
3804 if (d == 2
3805 && BRANCH_COST (optimize_insn_for_speed_p (),
3806 false) >= 1)
3808 temp = gen_reg_rtx (mode);
3809 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3810 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3811 0, OPTAB_LIB_WIDEN);
3812 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3815 #ifdef HAVE_conditional_move
3816 if (BRANCH_COST (optimize_insn_for_speed_p (), false)
3817 >= 2)
3819 rtx temp2;
3821 start_sequence ();
3822 temp2 = copy_to_mode_reg (mode, op0);
3823 temp = expand_binop (mode, add_optab, temp2, gen_int_mode (d - 1, mode),
3824 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3825 temp = force_reg (mode, temp);
3827 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3828 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3829 mode, temp, temp2, mode, 0);
3830 if (temp2)
3832 rtx seq = get_insns ();
3833 end_sequence ();
3834 emit_insn (seq);
3835 return expand_shift (RSHIFT_EXPR, mode, temp2, logd, NULL_RTX, 0);
3837 end_sequence ();
3839 #endif
3841 if (BRANCH_COST (optimize_insn_for_speed_p (),
3842 false) >= 2)
3844 int ushift = GET_MODE_BITSIZE (mode) - logd;
3846 temp = gen_reg_rtx (mode);
3847 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3848 if (shift_cost (optimize_insn_for_speed_p (), mode, ushift)
3849 > COSTS_N_INSNS (1))
3850 temp = expand_binop (mode, and_optab, temp, gen_int_mode (d - 1, mode),
3851 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3852 else
3853 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3854 ushift, NULL_RTX, 1);
3855 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3856 0, OPTAB_LIB_WIDEN);
3857 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3860 label = gen_label_rtx ();
3861 temp = copy_to_mode_reg (mode, op0);
3862 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3863 expand_inc (temp, gen_int_mode (d - 1, mode));
3864 emit_label (label);
3865 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3868 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3869 if that is convenient, and returning where the result is.
3870 You may request either the quotient or the remainder as the result;
3871 specify REM_FLAG nonzero to get the remainder.
3873 CODE is the expression code for which kind of division this is;
3874 it controls how rounding is done. MODE is the machine mode to use.
3875 UNSIGNEDP nonzero means do unsigned division. */
3877 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3878 and then correct it by or'ing in missing high bits
3879 if result of ANDI is nonzero.
3880 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3881 This could optimize to a bfexts instruction.
3882 But C doesn't use these operations, so their optimizations are
3883 left for later. */
3884 /* ??? For modulo, we don't actually need the highpart of the first product,
3885 the low part will do nicely. And for small divisors, the second multiply
3886 can also be a low-part only multiply or even be completely left out.
3887 E.g. to calculate the remainder of a division by 3 with a 32 bit
3888 multiply, multiply with 0x55555556 and extract the upper two bits;
3889 the result is exact for inputs up to 0x1fffffff.
3890 The input range can be reduced by using cross-sum rules.
3891 For odd divisors >= 3, the following table gives right shift counts
3892 so that if a number is shifted by an integer multiple of the given
3893 amount, the remainder stays the same:
3894 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3895 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3896 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3897 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3898 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3900 Cross-sum rules for even numbers can be derived by leaving as many bits
3901 to the right alone as the divisor has zeros to the right.
3902 E.g. if x is an unsigned 32 bit number:
3903 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3907 expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
3908 rtx op0, rtx op1, rtx target, int unsignedp)
3910 enum machine_mode compute_mode;
3911 rtx tquotient;
3912 rtx quotient = 0, remainder = 0;
3913 rtx last;
3914 int size;
3915 rtx insn;
3916 optab optab1, optab2;
3917 int op1_is_constant, op1_is_pow2 = 0;
3918 int max_cost, extra_cost;
3919 static HOST_WIDE_INT last_div_const = 0;
3920 bool speed = optimize_insn_for_speed_p ();
3922 op1_is_constant = CONST_INT_P (op1);
3923 if (op1_is_constant)
3925 unsigned HOST_WIDE_INT ext_op1 = UINTVAL (op1);
3926 if (unsignedp)
3927 ext_op1 &= GET_MODE_MASK (mode);
3928 op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
3929 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
3933 This is the structure of expand_divmod:
3935 First comes code to fix up the operands so we can perform the operations
3936 correctly and efficiently.
3938 Second comes a switch statement with code specific for each rounding mode.
3939 For some special operands this code emits all RTL for the desired
3940 operation, for other cases, it generates only a quotient and stores it in
3941 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3942 to indicate that it has not done anything.
3944 Last comes code that finishes the operation. If QUOTIENT is set and
3945 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3946 QUOTIENT is not set, it is computed using trunc rounding.
3948 We try to generate special code for division and remainder when OP1 is a
3949 constant. If |OP1| = 2**n we can use shifts and some other fast
3950 operations. For other values of OP1, we compute a carefully selected
3951 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3952 by m.
3954 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3955 half of the product. Different strategies for generating the product are
3956 implemented in expmed_mult_highpart.
3958 If what we actually want is the remainder, we generate that by another
3959 by-constant multiplication and a subtraction. */
3961 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3962 code below will malfunction if we are, so check here and handle
3963 the special case if so. */
3964 if (op1 == const1_rtx)
3965 return rem_flag ? const0_rtx : op0;
3967 /* When dividing by -1, we could get an overflow.
3968 negv_optab can handle overflows. */
3969 if (! unsignedp && op1 == constm1_rtx)
3971 if (rem_flag)
3972 return const0_rtx;
3973 return expand_unop (mode, flag_trapv && GET_MODE_CLASS (mode) == MODE_INT
3974 ? negv_optab : neg_optab, op0, target, 0);
3977 if (target
3978 /* Don't use the function value register as a target
3979 since we have to read it as well as write it,
3980 and function-inlining gets confused by this. */
3981 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3982 /* Don't clobber an operand while doing a multi-step calculation. */
3983 || ((rem_flag || op1_is_constant)
3984 && (reg_mentioned_p (target, op0)
3985 || (MEM_P (op0) && MEM_P (target))))
3986 || reg_mentioned_p (target, op1)
3987 || (MEM_P (op1) && MEM_P (target))))
3988 target = 0;
3990 /* Get the mode in which to perform this computation. Normally it will
3991 be MODE, but sometimes we can't do the desired operation in MODE.
3992 If so, pick a wider mode in which we can do the operation. Convert
3993 to that mode at the start to avoid repeated conversions.
3995 First see what operations we need. These depend on the expression
3996 we are evaluating. (We assume that divxx3 insns exist under the
3997 same conditions that modxx3 insns and that these insns don't normally
3998 fail. If these assumptions are not correct, we may generate less
3999 efficient code in some cases.)
4001 Then see if we find a mode in which we can open-code that operation
4002 (either a division, modulus, or shift). Finally, check for the smallest
4003 mode for which we can do the operation with a library call. */
4005 /* We might want to refine this now that we have division-by-constant
4006 optimization. Since expmed_mult_highpart tries so many variants, it is
4007 not straightforward to generalize this. Maybe we should make an array
4008 of possible modes in init_expmed? Save this for GCC 2.7. */
4010 optab1 = ((op1_is_pow2 && op1 != const0_rtx)
4011 ? (unsignedp ? lshr_optab : ashr_optab)
4012 : (unsignedp ? udiv_optab : sdiv_optab));
4013 optab2 = ((op1_is_pow2 && op1 != const0_rtx)
4014 ? optab1
4015 : (unsignedp ? udivmod_optab : sdivmod_optab));
4017 for (compute_mode = mode; compute_mode != VOIDmode;
4018 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
4019 if (optab_handler (optab1, compute_mode) != CODE_FOR_nothing
4020 || optab_handler (optab2, compute_mode) != CODE_FOR_nothing)
4021 break;
4023 if (compute_mode == VOIDmode)
4024 for (compute_mode = mode; compute_mode != VOIDmode;
4025 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
4026 if (optab_libfunc (optab1, compute_mode)
4027 || optab_libfunc (optab2, compute_mode))
4028 break;
4030 /* If we still couldn't find a mode, use MODE, but expand_binop will
4031 probably die. */
4032 if (compute_mode == VOIDmode)
4033 compute_mode = mode;
4035 if (target && GET_MODE (target) == compute_mode)
4036 tquotient = target;
4037 else
4038 tquotient = gen_reg_rtx (compute_mode);
4040 size = GET_MODE_BITSIZE (compute_mode);
4041 #if 0
4042 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
4043 (mode), and thereby get better code when OP1 is a constant. Do that
4044 later. It will require going over all usages of SIZE below. */
4045 size = GET_MODE_BITSIZE (mode);
4046 #endif
4048 /* Only deduct something for a REM if the last divide done was
4049 for a different constant. Then set the constant of the last
4050 divide. */
4051 max_cost = (unsignedp
4052 ? udiv_cost (speed, compute_mode)
4053 : sdiv_cost (speed, compute_mode));
4054 if (rem_flag && ! (last_div_const != 0 && op1_is_constant
4055 && INTVAL (op1) == last_div_const))
4056 max_cost -= (mul_cost (speed, compute_mode)
4057 + add_cost (speed, compute_mode));
4059 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
4061 /* Now convert to the best mode to use. */
4062 if (compute_mode != mode)
4064 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
4065 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
4067 /* convert_modes may have placed op1 into a register, so we
4068 must recompute the following. */
4069 op1_is_constant = CONST_INT_P (op1);
4070 op1_is_pow2 = (op1_is_constant
4071 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4072 || (! unsignedp
4073 && EXACT_POWER_OF_2_OR_ZERO_P (-UINTVAL (op1))))));
4076 /* If one of the operands is a volatile MEM, copy it into a register. */
4078 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
4079 op0 = force_reg (compute_mode, op0);
4080 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
4081 op1 = force_reg (compute_mode, op1);
4083 /* If we need the remainder or if OP1 is constant, we need to
4084 put OP0 in a register in case it has any queued subexpressions. */
4085 if (rem_flag || op1_is_constant)
4086 op0 = force_reg (compute_mode, op0);
4088 last = get_last_insn ();
4090 /* Promote floor rounding to trunc rounding for unsigned operations. */
4091 if (unsignedp)
4093 if (code == FLOOR_DIV_EXPR)
4094 code = TRUNC_DIV_EXPR;
4095 if (code == FLOOR_MOD_EXPR)
4096 code = TRUNC_MOD_EXPR;
4097 if (code == EXACT_DIV_EXPR && op1_is_pow2)
4098 code = TRUNC_DIV_EXPR;
4101 if (op1 != const0_rtx)
4102 switch (code)
4104 case TRUNC_MOD_EXPR:
4105 case TRUNC_DIV_EXPR:
4106 if (op1_is_constant)
4108 if (unsignedp)
4110 unsigned HOST_WIDE_INT mh, ml;
4111 int pre_shift, post_shift;
4112 int dummy;
4113 unsigned HOST_WIDE_INT d = (INTVAL (op1)
4114 & GET_MODE_MASK (compute_mode));
4116 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4118 pre_shift = floor_log2 (d);
4119 if (rem_flag)
4121 unsigned HOST_WIDE_INT mask
4122 = ((unsigned HOST_WIDE_INT) 1 << pre_shift) - 1;
4123 remainder
4124 = expand_binop (compute_mode, and_optab, op0,
4125 gen_int_mode (mask, compute_mode),
4126 remainder, 1,
4127 OPTAB_LIB_WIDEN);
4128 if (remainder)
4129 return gen_lowpart (mode, remainder);
4131 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4132 pre_shift, tquotient, 1);
4134 else if (size <= HOST_BITS_PER_WIDE_INT)
4136 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
4138 /* Most significant bit of divisor is set; emit an scc
4139 insn. */
4140 quotient = emit_store_flag_force (tquotient, GEU, op0, op1,
4141 compute_mode, 1, 1);
4143 else
4145 /* Find a suitable multiplier and right shift count
4146 instead of multiplying with D. */
4148 mh = choose_multiplier (d, size, size,
4149 &ml, &post_shift, &dummy);
4151 /* If the suggested multiplier is more than SIZE bits,
4152 we can do better for even divisors, using an
4153 initial right shift. */
4154 if (mh != 0 && (d & 1) == 0)
4156 pre_shift = floor_log2 (d & -d);
4157 mh = choose_multiplier (d >> pre_shift, size,
4158 size - pre_shift,
4159 &ml, &post_shift, &dummy);
4160 gcc_assert (!mh);
4162 else
4163 pre_shift = 0;
4165 if (mh != 0)
4167 rtx t1, t2, t3, t4;
4169 if (post_shift - 1 >= BITS_PER_WORD)
4170 goto fail1;
4172 extra_cost
4173 = (shift_cost (speed, compute_mode, post_shift - 1)
4174 + shift_cost (speed, compute_mode, 1)
4175 + 2 * add_cost (speed, compute_mode));
4176 t1 = expmed_mult_highpart
4177 (compute_mode, op0,
4178 gen_int_mode (ml, compute_mode),
4179 NULL_RTX, 1, max_cost - extra_cost);
4180 if (t1 == 0)
4181 goto fail1;
4182 t2 = force_operand (gen_rtx_MINUS (compute_mode,
4183 op0, t1),
4184 NULL_RTX);
4185 t3 = expand_shift (RSHIFT_EXPR, compute_mode,
4186 t2, 1, NULL_RTX, 1);
4187 t4 = force_operand (gen_rtx_PLUS (compute_mode,
4188 t1, t3),
4189 NULL_RTX);
4190 quotient = expand_shift
4191 (RSHIFT_EXPR, compute_mode, t4,
4192 post_shift - 1, tquotient, 1);
4194 else
4196 rtx t1, t2;
4198 if (pre_shift >= BITS_PER_WORD
4199 || post_shift >= BITS_PER_WORD)
4200 goto fail1;
4202 t1 = expand_shift
4203 (RSHIFT_EXPR, compute_mode, op0,
4204 pre_shift, NULL_RTX, 1);
4205 extra_cost
4206 = (shift_cost (speed, compute_mode, pre_shift)
4207 + shift_cost (speed, compute_mode, post_shift));
4208 t2 = expmed_mult_highpart
4209 (compute_mode, t1,
4210 gen_int_mode (ml, compute_mode),
4211 NULL_RTX, 1, max_cost - extra_cost);
4212 if (t2 == 0)
4213 goto fail1;
4214 quotient = expand_shift
4215 (RSHIFT_EXPR, compute_mode, t2,
4216 post_shift, tquotient, 1);
4220 else /* Too wide mode to use tricky code */
4221 break;
4223 insn = get_last_insn ();
4224 if (insn != last)
4225 set_dst_reg_note (insn, REG_EQUAL,
4226 gen_rtx_UDIV (compute_mode, op0, op1),
4227 quotient);
4229 else /* TRUNC_DIV, signed */
4231 unsigned HOST_WIDE_INT ml;
4232 int lgup, post_shift;
4233 rtx mlr;
4234 HOST_WIDE_INT d = INTVAL (op1);
4235 unsigned HOST_WIDE_INT abs_d;
4237 /* Since d might be INT_MIN, we have to cast to
4238 unsigned HOST_WIDE_INT before negating to avoid
4239 undefined signed overflow. */
4240 abs_d = (d >= 0
4241 ? (unsigned HOST_WIDE_INT) d
4242 : - (unsigned HOST_WIDE_INT) d);
4244 /* n rem d = n rem -d */
4245 if (rem_flag && d < 0)
4247 d = abs_d;
4248 op1 = gen_int_mode (abs_d, compute_mode);
4251 if (d == 1)
4252 quotient = op0;
4253 else if (d == -1)
4254 quotient = expand_unop (compute_mode, neg_optab, op0,
4255 tquotient, 0);
4256 else if (HOST_BITS_PER_WIDE_INT >= size
4257 && abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
4259 /* This case is not handled correctly below. */
4260 quotient = emit_store_flag (tquotient, EQ, op0, op1,
4261 compute_mode, 1, 1);
4262 if (quotient == 0)
4263 goto fail1;
4265 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
4266 && (rem_flag
4267 ? smod_pow2_cheap (speed, compute_mode)
4268 : sdiv_pow2_cheap (speed, compute_mode))
4269 /* We assume that cheap metric is true if the
4270 optab has an expander for this mode. */
4271 && ((optab_handler ((rem_flag ? smod_optab
4272 : sdiv_optab),
4273 compute_mode)
4274 != CODE_FOR_nothing)
4275 || (optab_handler (sdivmod_optab,
4276 compute_mode)
4277 != CODE_FOR_nothing)))
4279 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
4281 if (rem_flag)
4283 remainder = expand_smod_pow2 (compute_mode, op0, d);
4284 if (remainder)
4285 return gen_lowpart (mode, remainder);
4288 if (sdiv_pow2_cheap (speed, compute_mode)
4289 && ((optab_handler (sdiv_optab, compute_mode)
4290 != CODE_FOR_nothing)
4291 || (optab_handler (sdivmod_optab, compute_mode)
4292 != CODE_FOR_nothing)))
4293 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
4294 compute_mode, op0,
4295 gen_int_mode (abs_d,
4296 compute_mode),
4297 NULL_RTX, 0);
4298 else
4299 quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
4301 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4302 negate the quotient. */
4303 if (d < 0)
4305 insn = get_last_insn ();
4306 if (insn != last
4307 && abs_d < ((unsigned HOST_WIDE_INT) 1
4308 << (HOST_BITS_PER_WIDE_INT - 1)))
4309 set_dst_reg_note (insn, REG_EQUAL,
4310 gen_rtx_DIV (compute_mode, op0,
4311 gen_int_mode
4312 (abs_d,
4313 compute_mode)),
4314 quotient);
4316 quotient = expand_unop (compute_mode, neg_optab,
4317 quotient, quotient, 0);
4320 else if (size <= HOST_BITS_PER_WIDE_INT)
4322 choose_multiplier (abs_d, size, size - 1,
4323 &ml, &post_shift, &lgup);
4324 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
4326 rtx t1, t2, t3;
4328 if (post_shift >= BITS_PER_WORD
4329 || size - 1 >= BITS_PER_WORD)
4330 goto fail1;
4332 extra_cost = (shift_cost (speed, compute_mode, post_shift)
4333 + shift_cost (speed, compute_mode, size - 1)
4334 + add_cost (speed, compute_mode));
4335 t1 = expmed_mult_highpart
4336 (compute_mode, op0, gen_int_mode (ml, compute_mode),
4337 NULL_RTX, 0, max_cost - extra_cost);
4338 if (t1 == 0)
4339 goto fail1;
4340 t2 = expand_shift
4341 (RSHIFT_EXPR, compute_mode, t1,
4342 post_shift, NULL_RTX, 0);
4343 t3 = expand_shift
4344 (RSHIFT_EXPR, compute_mode, op0,
4345 size - 1, NULL_RTX, 0);
4346 if (d < 0)
4347 quotient
4348 = force_operand (gen_rtx_MINUS (compute_mode,
4349 t3, t2),
4350 tquotient);
4351 else
4352 quotient
4353 = force_operand (gen_rtx_MINUS (compute_mode,
4354 t2, t3),
4355 tquotient);
4357 else
4359 rtx t1, t2, t3, t4;
4361 if (post_shift >= BITS_PER_WORD
4362 || size - 1 >= BITS_PER_WORD)
4363 goto fail1;
4365 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
4366 mlr = gen_int_mode (ml, compute_mode);
4367 extra_cost = (shift_cost (speed, compute_mode, post_shift)
4368 + shift_cost (speed, compute_mode, size - 1)
4369 + 2 * add_cost (speed, compute_mode));
4370 t1 = expmed_mult_highpart (compute_mode, op0, mlr,
4371 NULL_RTX, 0,
4372 max_cost - extra_cost);
4373 if (t1 == 0)
4374 goto fail1;
4375 t2 = force_operand (gen_rtx_PLUS (compute_mode,
4376 t1, op0),
4377 NULL_RTX);
4378 t3 = expand_shift
4379 (RSHIFT_EXPR, compute_mode, t2,
4380 post_shift, NULL_RTX, 0);
4381 t4 = expand_shift
4382 (RSHIFT_EXPR, compute_mode, op0,
4383 size - 1, NULL_RTX, 0);
4384 if (d < 0)
4385 quotient
4386 = force_operand (gen_rtx_MINUS (compute_mode,
4387 t4, t3),
4388 tquotient);
4389 else
4390 quotient
4391 = force_operand (gen_rtx_MINUS (compute_mode,
4392 t3, t4),
4393 tquotient);
4396 else /* Too wide mode to use tricky code */
4397 break;
4399 insn = get_last_insn ();
4400 if (insn != last)
4401 set_dst_reg_note (insn, REG_EQUAL,
4402 gen_rtx_DIV (compute_mode, op0, op1),
4403 quotient);
4405 break;
4407 fail1:
4408 delete_insns_since (last);
4409 break;
4411 case FLOOR_DIV_EXPR:
4412 case FLOOR_MOD_EXPR:
4413 /* We will come here only for signed operations. */
4414 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4416 unsigned HOST_WIDE_INT mh, ml;
4417 int pre_shift, lgup, post_shift;
4418 HOST_WIDE_INT d = INTVAL (op1);
4420 if (d > 0)
4422 /* We could just as easily deal with negative constants here,
4423 but it does not seem worth the trouble for GCC 2.6. */
4424 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4426 pre_shift = floor_log2 (d);
4427 if (rem_flag)
4429 unsigned HOST_WIDE_INT mask
4430 = ((unsigned HOST_WIDE_INT) 1 << pre_shift) - 1;
4431 remainder = expand_binop
4432 (compute_mode, and_optab, op0,
4433 gen_int_mode (mask, compute_mode),
4434 remainder, 0, OPTAB_LIB_WIDEN);
4435 if (remainder)
4436 return gen_lowpart (mode, remainder);
4438 quotient = expand_shift
4439 (RSHIFT_EXPR, compute_mode, op0,
4440 pre_shift, tquotient, 0);
4442 else
4444 rtx t1, t2, t3, t4;
4446 mh = choose_multiplier (d, size, size - 1,
4447 &ml, &post_shift, &lgup);
4448 gcc_assert (!mh);
4450 if (post_shift < BITS_PER_WORD
4451 && size - 1 < BITS_PER_WORD)
4453 t1 = expand_shift
4454 (RSHIFT_EXPR, compute_mode, op0,
4455 size - 1, NULL_RTX, 0);
4456 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
4457 NULL_RTX, 0, OPTAB_WIDEN);
4458 extra_cost = (shift_cost (speed, compute_mode, post_shift)
4459 + shift_cost (speed, compute_mode, size - 1)
4460 + 2 * add_cost (speed, compute_mode));
4461 t3 = expmed_mult_highpart
4462 (compute_mode, t2, gen_int_mode (ml, compute_mode),
4463 NULL_RTX, 1, max_cost - extra_cost);
4464 if (t3 != 0)
4466 t4 = expand_shift
4467 (RSHIFT_EXPR, compute_mode, t3,
4468 post_shift, NULL_RTX, 1);
4469 quotient = expand_binop (compute_mode, xor_optab,
4470 t4, t1, tquotient, 0,
4471 OPTAB_WIDEN);
4476 else
4478 rtx nsign, t1, t2, t3, t4;
4479 t1 = force_operand (gen_rtx_PLUS (compute_mode,
4480 op0, constm1_rtx), NULL_RTX);
4481 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
4482 0, OPTAB_WIDEN);
4483 nsign = expand_shift
4484 (RSHIFT_EXPR, compute_mode, t2,
4485 size - 1, NULL_RTX, 0);
4486 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
4487 NULL_RTX);
4488 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
4489 NULL_RTX, 0);
4490 if (t4)
4492 rtx t5;
4493 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
4494 NULL_RTX, 0);
4495 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4496 t4, t5),
4497 tquotient);
4502 if (quotient != 0)
4503 break;
4504 delete_insns_since (last);
4506 /* Try using an instruction that produces both the quotient and
4507 remainder, using truncation. We can easily compensate the quotient
4508 or remainder to get floor rounding, once we have the remainder.
4509 Notice that we compute also the final remainder value here,
4510 and return the result right away. */
4511 if (target == 0 || GET_MODE (target) != compute_mode)
4512 target = gen_reg_rtx (compute_mode);
4514 if (rem_flag)
4516 remainder
4517 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4518 quotient = gen_reg_rtx (compute_mode);
4520 else
4522 quotient
4523 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4524 remainder = gen_reg_rtx (compute_mode);
4527 if (expand_twoval_binop (sdivmod_optab, op0, op1,
4528 quotient, remainder, 0))
4530 /* This could be computed with a branch-less sequence.
4531 Save that for later. */
4532 rtx tem;
4533 rtx label = gen_label_rtx ();
4534 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4535 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4536 NULL_RTX, 0, OPTAB_WIDEN);
4537 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4538 expand_dec (quotient, const1_rtx);
4539 expand_inc (remainder, op1);
4540 emit_label (label);
4541 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4544 /* No luck with division elimination or divmod. Have to do it
4545 by conditionally adjusting op0 *and* the result. */
4547 rtx label1, label2, label3, label4, label5;
4548 rtx adjusted_op0;
4549 rtx tem;
4551 quotient = gen_reg_rtx (compute_mode);
4552 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4553 label1 = gen_label_rtx ();
4554 label2 = gen_label_rtx ();
4555 label3 = gen_label_rtx ();
4556 label4 = gen_label_rtx ();
4557 label5 = gen_label_rtx ();
4558 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4559 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4560 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4561 quotient, 0, OPTAB_LIB_WIDEN);
4562 if (tem != quotient)
4563 emit_move_insn (quotient, tem);
4564 emit_jump_insn (gen_jump (label5));
4565 emit_barrier ();
4566 emit_label (label1);
4567 expand_inc (adjusted_op0, const1_rtx);
4568 emit_jump_insn (gen_jump (label4));
4569 emit_barrier ();
4570 emit_label (label2);
4571 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4572 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4573 quotient, 0, OPTAB_LIB_WIDEN);
4574 if (tem != quotient)
4575 emit_move_insn (quotient, tem);
4576 emit_jump_insn (gen_jump (label5));
4577 emit_barrier ();
4578 emit_label (label3);
4579 expand_dec (adjusted_op0, const1_rtx);
4580 emit_label (label4);
4581 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4582 quotient, 0, OPTAB_LIB_WIDEN);
4583 if (tem != quotient)
4584 emit_move_insn (quotient, tem);
4585 expand_dec (quotient, const1_rtx);
4586 emit_label (label5);
4588 break;
4590 case CEIL_DIV_EXPR:
4591 case CEIL_MOD_EXPR:
4592 if (unsignedp)
4594 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
4596 rtx t1, t2, t3;
4597 unsigned HOST_WIDE_INT d = INTVAL (op1);
4598 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4599 floor_log2 (d), tquotient, 1);
4600 t2 = expand_binop (compute_mode, and_optab, op0,
4601 gen_int_mode (d - 1, compute_mode),
4602 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4603 t3 = gen_reg_rtx (compute_mode);
4604 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4605 compute_mode, 1, 1);
4606 if (t3 == 0)
4608 rtx lab;
4609 lab = gen_label_rtx ();
4610 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4611 expand_inc (t1, const1_rtx);
4612 emit_label (lab);
4613 quotient = t1;
4615 else
4616 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4617 t1, t3),
4618 tquotient);
4619 break;
4622 /* Try using an instruction that produces both the quotient and
4623 remainder, using truncation. We can easily compensate the
4624 quotient or remainder to get ceiling rounding, once we have the
4625 remainder. Notice that we compute also the final remainder
4626 value here, and return the result right away. */
4627 if (target == 0 || GET_MODE (target) != compute_mode)
4628 target = gen_reg_rtx (compute_mode);
4630 if (rem_flag)
4632 remainder = (REG_P (target)
4633 ? target : gen_reg_rtx (compute_mode));
4634 quotient = gen_reg_rtx (compute_mode);
4636 else
4638 quotient = (REG_P (target)
4639 ? target : gen_reg_rtx (compute_mode));
4640 remainder = gen_reg_rtx (compute_mode);
4643 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4644 remainder, 1))
4646 /* This could be computed with a branch-less sequence.
4647 Save that for later. */
4648 rtx label = gen_label_rtx ();
4649 do_cmp_and_jump (remainder, const0_rtx, EQ,
4650 compute_mode, label);
4651 expand_inc (quotient, const1_rtx);
4652 expand_dec (remainder, op1);
4653 emit_label (label);
4654 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4657 /* No luck with division elimination or divmod. Have to do it
4658 by conditionally adjusting op0 *and* the result. */
4660 rtx label1, label2;
4661 rtx adjusted_op0, tem;
4663 quotient = gen_reg_rtx (compute_mode);
4664 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4665 label1 = gen_label_rtx ();
4666 label2 = gen_label_rtx ();
4667 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4668 compute_mode, label1);
4669 emit_move_insn (quotient, const0_rtx);
4670 emit_jump_insn (gen_jump (label2));
4671 emit_barrier ();
4672 emit_label (label1);
4673 expand_dec (adjusted_op0, const1_rtx);
4674 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4675 quotient, 1, OPTAB_LIB_WIDEN);
4676 if (tem != quotient)
4677 emit_move_insn (quotient, tem);
4678 expand_inc (quotient, const1_rtx);
4679 emit_label (label2);
4682 else /* signed */
4684 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4685 && INTVAL (op1) >= 0)
4687 /* This is extremely similar to the code for the unsigned case
4688 above. For 2.7 we should merge these variants, but for
4689 2.6.1 I don't want to touch the code for unsigned since that
4690 get used in C. The signed case will only be used by other
4691 languages (Ada). */
4693 rtx t1, t2, t3;
4694 unsigned HOST_WIDE_INT d = INTVAL (op1);
4695 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4696 floor_log2 (d), tquotient, 0);
4697 t2 = expand_binop (compute_mode, and_optab, op0,
4698 gen_int_mode (d - 1, compute_mode),
4699 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4700 t3 = gen_reg_rtx (compute_mode);
4701 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4702 compute_mode, 1, 1);
4703 if (t3 == 0)
4705 rtx lab;
4706 lab = gen_label_rtx ();
4707 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4708 expand_inc (t1, const1_rtx);
4709 emit_label (lab);
4710 quotient = t1;
4712 else
4713 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4714 t1, t3),
4715 tquotient);
4716 break;
4719 /* Try using an instruction that produces both the quotient and
4720 remainder, using truncation. We can easily compensate the
4721 quotient or remainder to get ceiling rounding, once we have the
4722 remainder. Notice that we compute also the final remainder
4723 value here, and return the result right away. */
4724 if (target == 0 || GET_MODE (target) != compute_mode)
4725 target = gen_reg_rtx (compute_mode);
4726 if (rem_flag)
4728 remainder= (REG_P (target)
4729 ? target : gen_reg_rtx (compute_mode));
4730 quotient = gen_reg_rtx (compute_mode);
4732 else
4734 quotient = (REG_P (target)
4735 ? target : gen_reg_rtx (compute_mode));
4736 remainder = gen_reg_rtx (compute_mode);
4739 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4740 remainder, 0))
4742 /* This could be computed with a branch-less sequence.
4743 Save that for later. */
4744 rtx tem;
4745 rtx label = gen_label_rtx ();
4746 do_cmp_and_jump (remainder, const0_rtx, EQ,
4747 compute_mode, label);
4748 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4749 NULL_RTX, 0, OPTAB_WIDEN);
4750 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4751 expand_inc (quotient, const1_rtx);
4752 expand_dec (remainder, op1);
4753 emit_label (label);
4754 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4757 /* No luck with division elimination or divmod. Have to do it
4758 by conditionally adjusting op0 *and* the result. */
4760 rtx label1, label2, label3, label4, label5;
4761 rtx adjusted_op0;
4762 rtx tem;
4764 quotient = gen_reg_rtx (compute_mode);
4765 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4766 label1 = gen_label_rtx ();
4767 label2 = gen_label_rtx ();
4768 label3 = gen_label_rtx ();
4769 label4 = gen_label_rtx ();
4770 label5 = gen_label_rtx ();
4771 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4772 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4773 compute_mode, label1);
4774 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4775 quotient, 0, OPTAB_LIB_WIDEN);
4776 if (tem != quotient)
4777 emit_move_insn (quotient, tem);
4778 emit_jump_insn (gen_jump (label5));
4779 emit_barrier ();
4780 emit_label (label1);
4781 expand_dec (adjusted_op0, const1_rtx);
4782 emit_jump_insn (gen_jump (label4));
4783 emit_barrier ();
4784 emit_label (label2);
4785 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4786 compute_mode, label3);
4787 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4788 quotient, 0, OPTAB_LIB_WIDEN);
4789 if (tem != quotient)
4790 emit_move_insn (quotient, tem);
4791 emit_jump_insn (gen_jump (label5));
4792 emit_barrier ();
4793 emit_label (label3);
4794 expand_inc (adjusted_op0, const1_rtx);
4795 emit_label (label4);
4796 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4797 quotient, 0, OPTAB_LIB_WIDEN);
4798 if (tem != quotient)
4799 emit_move_insn (quotient, tem);
4800 expand_inc (quotient, const1_rtx);
4801 emit_label (label5);
4804 break;
4806 case EXACT_DIV_EXPR:
4807 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4809 HOST_WIDE_INT d = INTVAL (op1);
4810 unsigned HOST_WIDE_INT ml;
4811 int pre_shift;
4812 rtx t1;
4814 pre_shift = floor_log2 (d & -d);
4815 ml = invert_mod2n (d >> pre_shift, size);
4816 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4817 pre_shift, NULL_RTX, unsignedp);
4818 quotient = expand_mult (compute_mode, t1,
4819 gen_int_mode (ml, compute_mode),
4820 NULL_RTX, 1);
4822 insn = get_last_insn ();
4823 set_dst_reg_note (insn, REG_EQUAL,
4824 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4825 compute_mode, op0, op1),
4826 quotient);
4828 break;
4830 case ROUND_DIV_EXPR:
4831 case ROUND_MOD_EXPR:
4832 if (unsignedp)
4834 rtx tem;
4835 rtx label;
4836 label = gen_label_rtx ();
4837 quotient = gen_reg_rtx (compute_mode);
4838 remainder = gen_reg_rtx (compute_mode);
4839 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4841 rtx tem;
4842 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
4843 quotient, 1, OPTAB_LIB_WIDEN);
4844 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
4845 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4846 remainder, 1, OPTAB_LIB_WIDEN);
4848 tem = plus_constant (compute_mode, op1, -1);
4849 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem, 1, NULL_RTX, 1);
4850 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
4851 expand_inc (quotient, const1_rtx);
4852 expand_dec (remainder, op1);
4853 emit_label (label);
4855 else
4857 rtx abs_rem, abs_op1, tem, mask;
4858 rtx label;
4859 label = gen_label_rtx ();
4860 quotient = gen_reg_rtx (compute_mode);
4861 remainder = gen_reg_rtx (compute_mode);
4862 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
4864 rtx tem;
4865 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
4866 quotient, 0, OPTAB_LIB_WIDEN);
4867 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
4868 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4869 remainder, 0, OPTAB_LIB_WIDEN);
4871 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
4872 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
4873 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
4874 1, NULL_RTX, 1);
4875 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
4876 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4877 NULL_RTX, 0, OPTAB_WIDEN);
4878 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4879 size - 1, NULL_RTX, 0);
4880 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
4881 NULL_RTX, 0, OPTAB_WIDEN);
4882 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4883 NULL_RTX, 0, OPTAB_WIDEN);
4884 expand_inc (quotient, tem);
4885 tem = expand_binop (compute_mode, xor_optab, mask, op1,
4886 NULL_RTX, 0, OPTAB_WIDEN);
4887 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4888 NULL_RTX, 0, OPTAB_WIDEN);
4889 expand_dec (remainder, tem);
4890 emit_label (label);
4892 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4894 default:
4895 gcc_unreachable ();
4898 if (quotient == 0)
4900 if (target && GET_MODE (target) != compute_mode)
4901 target = 0;
4903 if (rem_flag)
4905 /* Try to produce the remainder without producing the quotient.
4906 If we seem to have a divmod pattern that does not require widening,
4907 don't try widening here. We should really have a WIDEN argument
4908 to expand_twoval_binop, since what we'd really like to do here is
4909 1) try a mod insn in compute_mode
4910 2) try a divmod insn in compute_mode
4911 3) try a div insn in compute_mode and multiply-subtract to get
4912 remainder
4913 4) try the same things with widening allowed. */
4914 remainder
4915 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4916 op0, op1, target,
4917 unsignedp,
4918 ((optab_handler (optab2, compute_mode)
4919 != CODE_FOR_nothing)
4920 ? OPTAB_DIRECT : OPTAB_WIDEN));
4921 if (remainder == 0)
4923 /* No luck there. Can we do remainder and divide at once
4924 without a library call? */
4925 remainder = gen_reg_rtx (compute_mode);
4926 if (! expand_twoval_binop ((unsignedp
4927 ? udivmod_optab
4928 : sdivmod_optab),
4929 op0, op1,
4930 NULL_RTX, remainder, unsignedp))
4931 remainder = 0;
4934 if (remainder)
4935 return gen_lowpart (mode, remainder);
4938 /* Produce the quotient. Try a quotient insn, but not a library call.
4939 If we have a divmod in this mode, use it in preference to widening
4940 the div (for this test we assume it will not fail). Note that optab2
4941 is set to the one of the two optabs that the call below will use. */
4942 quotient
4943 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
4944 op0, op1, rem_flag ? NULL_RTX : target,
4945 unsignedp,
4946 ((optab_handler (optab2, compute_mode)
4947 != CODE_FOR_nothing)
4948 ? OPTAB_DIRECT : OPTAB_WIDEN));
4950 if (quotient == 0)
4952 /* No luck there. Try a quotient-and-remainder insn,
4953 keeping the quotient alone. */
4954 quotient = gen_reg_rtx (compute_mode);
4955 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
4956 op0, op1,
4957 quotient, NULL_RTX, unsignedp))
4959 quotient = 0;
4960 if (! rem_flag)
4961 /* Still no luck. If we are not computing the remainder,
4962 use a library call for the quotient. */
4963 quotient = sign_expand_binop (compute_mode,
4964 udiv_optab, sdiv_optab,
4965 op0, op1, target,
4966 unsignedp, OPTAB_LIB_WIDEN);
4971 if (rem_flag)
4973 if (target && GET_MODE (target) != compute_mode)
4974 target = 0;
4976 if (quotient == 0)
4978 /* No divide instruction either. Use library for remainder. */
4979 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4980 op0, op1, target,
4981 unsignedp, OPTAB_LIB_WIDEN);
4982 /* No remainder function. Try a quotient-and-remainder
4983 function, keeping the remainder. */
4984 if (!remainder)
4986 remainder = gen_reg_rtx (compute_mode);
4987 if (!expand_twoval_binop_libfunc
4988 (unsignedp ? udivmod_optab : sdivmod_optab,
4989 op0, op1,
4990 NULL_RTX, remainder,
4991 unsignedp ? UMOD : MOD))
4992 remainder = NULL_RTX;
4995 else
4997 /* We divided. Now finish doing X - Y * (X / Y). */
4998 remainder = expand_mult (compute_mode, quotient, op1,
4999 NULL_RTX, unsignedp);
5000 remainder = expand_binop (compute_mode, sub_optab, op0,
5001 remainder, target, unsignedp,
5002 OPTAB_LIB_WIDEN);
5006 return gen_lowpart (mode, rem_flag ? remainder : quotient);
5009 /* Return a tree node with data type TYPE, describing the value of X.
5010 Usually this is an VAR_DECL, if there is no obvious better choice.
5011 X may be an expression, however we only support those expressions
5012 generated by loop.c. */
5014 tree
5015 make_tree (tree type, rtx x)
5017 tree t;
5019 switch (GET_CODE (x))
5021 case CONST_INT:
5023 HOST_WIDE_INT hi = 0;
5025 if (INTVAL (x) < 0
5026 && !(TYPE_UNSIGNED (type)
5027 && (GET_MODE_BITSIZE (TYPE_MODE (type))
5028 < HOST_BITS_PER_WIDE_INT)))
5029 hi = -1;
5031 t = build_int_cst_wide (type, INTVAL (x), hi);
5033 return t;
5036 case CONST_DOUBLE:
5037 if (GET_MODE (x) == VOIDmode)
5038 t = build_int_cst_wide (type,
5039 CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
5040 else
5042 REAL_VALUE_TYPE d;
5044 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
5045 t = build_real (type, d);
5048 return t;
5050 case CONST_VECTOR:
5052 int units = CONST_VECTOR_NUNITS (x);
5053 tree itype = TREE_TYPE (type);
5054 tree *elts;
5055 int i;
5057 /* Build a tree with vector elements. */
5058 elts = XALLOCAVEC (tree, units);
5059 for (i = units - 1; i >= 0; --i)
5061 rtx elt = CONST_VECTOR_ELT (x, i);
5062 elts[i] = make_tree (itype, elt);
5065 return build_vector (type, elts);
5068 case PLUS:
5069 return fold_build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5070 make_tree (type, XEXP (x, 1)));
5072 case MINUS:
5073 return fold_build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5074 make_tree (type, XEXP (x, 1)));
5076 case NEG:
5077 return fold_build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0)));
5079 case MULT:
5080 return fold_build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
5081 make_tree (type, XEXP (x, 1)));
5083 case ASHIFT:
5084 return fold_build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
5085 make_tree (type, XEXP (x, 1)));
5087 case LSHIFTRT:
5088 t = unsigned_type_for (type);
5089 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5090 make_tree (t, XEXP (x, 0)),
5091 make_tree (type, XEXP (x, 1))));
5093 case ASHIFTRT:
5094 t = signed_type_for (type);
5095 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5096 make_tree (t, XEXP (x, 0)),
5097 make_tree (type, XEXP (x, 1))));
5099 case DIV:
5100 if (TREE_CODE (type) != REAL_TYPE)
5101 t = signed_type_for (type);
5102 else
5103 t = type;
5105 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5106 make_tree (t, XEXP (x, 0)),
5107 make_tree (t, XEXP (x, 1))));
5108 case UDIV:
5109 t = unsigned_type_for (type);
5110 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5111 make_tree (t, XEXP (x, 0)),
5112 make_tree (t, XEXP (x, 1))));
5114 case SIGN_EXTEND:
5115 case ZERO_EXTEND:
5116 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
5117 GET_CODE (x) == ZERO_EXTEND);
5118 return fold_convert (type, make_tree (t, XEXP (x, 0)));
5120 case CONST:
5121 return make_tree (type, XEXP (x, 0));
5123 case SYMBOL_REF:
5124 t = SYMBOL_REF_DECL (x);
5125 if (t)
5126 return fold_convert (type, build_fold_addr_expr (t));
5127 /* else fall through. */
5129 default:
5130 t = build_decl (RTL_LOCATION (x), VAR_DECL, NULL_TREE, type);
5132 /* If TYPE is a POINTER_TYPE, we might need to convert X from
5133 address mode to pointer mode. */
5134 if (POINTER_TYPE_P (type))
5135 x = convert_memory_address_addr_space
5136 (TYPE_MODE (type), x, TYPE_ADDR_SPACE (TREE_TYPE (type)));
5138 /* Note that we do *not* use SET_DECL_RTL here, because we do not
5139 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
5140 t->decl_with_rtl.rtl = x;
5142 return t;
5146 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5147 and returning TARGET.
5149 If TARGET is 0, a pseudo-register or constant is returned. */
5152 expand_and (enum machine_mode mode, rtx op0, rtx op1, rtx target)
5154 rtx tem = 0;
5156 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
5157 tem = simplify_binary_operation (AND, mode, op0, op1);
5158 if (tem == 0)
5159 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
5161 if (target == 0)
5162 target = tem;
5163 else if (tem != target)
5164 emit_move_insn (target, tem);
5165 return target;
5168 /* Helper function for emit_store_flag. */
5169 static rtx
5170 emit_cstore (rtx target, enum insn_code icode, enum rtx_code code,
5171 enum machine_mode mode, enum machine_mode compare_mode,
5172 int unsignedp, rtx x, rtx y, int normalizep,
5173 enum machine_mode target_mode)
5175 struct expand_operand ops[4];
5176 rtx op0, last, comparison, subtarget;
5177 enum machine_mode result_mode = targetm.cstore_mode (icode);
5179 last = get_last_insn ();
5180 x = prepare_operand (icode, x, 2, mode, compare_mode, unsignedp);
5181 y = prepare_operand (icode, y, 3, mode, compare_mode, unsignedp);
5182 if (!x || !y)
5184 delete_insns_since (last);
5185 return NULL_RTX;
5188 if (target_mode == VOIDmode)
5189 target_mode = result_mode;
5190 if (!target)
5191 target = gen_reg_rtx (target_mode);
5193 comparison = gen_rtx_fmt_ee (code, result_mode, x, y);
5195 create_output_operand (&ops[0], optimize ? NULL_RTX : target, result_mode);
5196 create_fixed_operand (&ops[1], comparison);
5197 create_fixed_operand (&ops[2], x);
5198 create_fixed_operand (&ops[3], y);
5199 if (!maybe_expand_insn (icode, 4, ops))
5201 delete_insns_since (last);
5202 return NULL_RTX;
5204 subtarget = ops[0].value;
5206 /* If we are converting to a wider mode, first convert to
5207 TARGET_MODE, then normalize. This produces better combining
5208 opportunities on machines that have a SIGN_EXTRACT when we are
5209 testing a single bit. This mostly benefits the 68k.
5211 If STORE_FLAG_VALUE does not have the sign bit set when
5212 interpreted in MODE, we can do this conversion as unsigned, which
5213 is usually more efficient. */
5214 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (result_mode))
5216 convert_move (target, subtarget,
5217 val_signbit_known_clear_p (result_mode,
5218 STORE_FLAG_VALUE));
5219 op0 = target;
5220 result_mode = target_mode;
5222 else
5223 op0 = subtarget;
5225 /* If we want to keep subexpressions around, don't reuse our last
5226 target. */
5227 if (optimize)
5228 subtarget = 0;
5230 /* Now normalize to the proper value in MODE. Sometimes we don't
5231 have to do anything. */
5232 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
5234 /* STORE_FLAG_VALUE might be the most negative number, so write
5235 the comparison this way to avoid a compiler-time warning. */
5236 else if (- normalizep == STORE_FLAG_VALUE)
5237 op0 = expand_unop (result_mode, neg_optab, op0, subtarget, 0);
5239 /* We don't want to use STORE_FLAG_VALUE < 0 below since this makes
5240 it hard to use a value of just the sign bit due to ANSI integer
5241 constant typing rules. */
5242 else if (val_signbit_known_set_p (result_mode, STORE_FLAG_VALUE))
5243 op0 = expand_shift (RSHIFT_EXPR, result_mode, op0,
5244 GET_MODE_BITSIZE (result_mode) - 1, subtarget,
5245 normalizep == 1);
5246 else
5248 gcc_assert (STORE_FLAG_VALUE & 1);
5250 op0 = expand_and (result_mode, op0, const1_rtx, subtarget);
5251 if (normalizep == -1)
5252 op0 = expand_unop (result_mode, neg_optab, op0, op0, 0);
5255 /* If we were converting to a smaller mode, do the conversion now. */
5256 if (target_mode != result_mode)
5258 convert_move (target, op0, 0);
5259 return target;
5261 else
5262 return op0;
5266 /* A subroutine of emit_store_flag only including "tricks" that do not
5267 need a recursive call. These are kept separate to avoid infinite
5268 loops. */
5270 static rtx
5271 emit_store_flag_1 (rtx target, enum rtx_code code, rtx op0, rtx op1,
5272 enum machine_mode mode, int unsignedp, int normalizep,
5273 enum machine_mode target_mode)
5275 rtx subtarget;
5276 enum insn_code icode;
5277 enum machine_mode compare_mode;
5278 enum mode_class mclass;
5279 enum rtx_code scode;
5280 rtx tem;
5282 if (unsignedp)
5283 code = unsigned_condition (code);
5284 scode = swap_condition (code);
5286 /* If one operand is constant, make it the second one. Only do this
5287 if the other operand is not constant as well. */
5289 if (swap_commutative_operands_p (op0, op1))
5291 tem = op0;
5292 op0 = op1;
5293 op1 = tem;
5294 code = swap_condition (code);
5297 if (mode == VOIDmode)
5298 mode = GET_MODE (op0);
5300 /* For some comparisons with 1 and -1, we can convert this to
5301 comparisons with zero. This will often produce more opportunities for
5302 store-flag insns. */
5304 switch (code)
5306 case LT:
5307 if (op1 == const1_rtx)
5308 op1 = const0_rtx, code = LE;
5309 break;
5310 case LE:
5311 if (op1 == constm1_rtx)
5312 op1 = const0_rtx, code = LT;
5313 break;
5314 case GE:
5315 if (op1 == const1_rtx)
5316 op1 = const0_rtx, code = GT;
5317 break;
5318 case GT:
5319 if (op1 == constm1_rtx)
5320 op1 = const0_rtx, code = GE;
5321 break;
5322 case GEU:
5323 if (op1 == const1_rtx)
5324 op1 = const0_rtx, code = NE;
5325 break;
5326 case LTU:
5327 if (op1 == const1_rtx)
5328 op1 = const0_rtx, code = EQ;
5329 break;
5330 default:
5331 break;
5334 /* If we are comparing a double-word integer with zero or -1, we can
5335 convert the comparison into one involving a single word. */
5336 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
5337 && GET_MODE_CLASS (mode) == MODE_INT
5338 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
5340 if ((code == EQ || code == NE)
5341 && (op1 == const0_rtx || op1 == constm1_rtx))
5343 rtx op00, op01;
5345 /* Do a logical OR or AND of the two words and compare the
5346 result. */
5347 op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
5348 op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
5349 tem = expand_binop (word_mode,
5350 op1 == const0_rtx ? ior_optab : and_optab,
5351 op00, op01, NULL_RTX, unsignedp,
5352 OPTAB_DIRECT);
5354 if (tem != 0)
5355 tem = emit_store_flag (NULL_RTX, code, tem, op1, word_mode,
5356 unsignedp, normalizep);
5358 else if ((code == LT || code == GE) && op1 == const0_rtx)
5360 rtx op0h;
5362 /* If testing the sign bit, can just test on high word. */
5363 op0h = simplify_gen_subreg (word_mode, op0, mode,
5364 subreg_highpart_offset (word_mode,
5365 mode));
5366 tem = emit_store_flag (NULL_RTX, code, op0h, op1, word_mode,
5367 unsignedp, normalizep);
5369 else
5370 tem = NULL_RTX;
5372 if (tem)
5374 if (target_mode == VOIDmode || GET_MODE (tem) == target_mode)
5375 return tem;
5376 if (!target)
5377 target = gen_reg_rtx (target_mode);
5379 convert_move (target, tem,
5380 !val_signbit_known_set_p (word_mode,
5381 (normalizep ? normalizep
5382 : STORE_FLAG_VALUE)));
5383 return target;
5387 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5388 complement of A (for GE) and shifting the sign bit to the low bit. */
5389 if (op1 == const0_rtx && (code == LT || code == GE)
5390 && GET_MODE_CLASS (mode) == MODE_INT
5391 && (normalizep || STORE_FLAG_VALUE == 1
5392 || val_signbit_p (mode, STORE_FLAG_VALUE)))
5394 subtarget = target;
5396 if (!target)
5397 target_mode = mode;
5399 /* If the result is to be wider than OP0, it is best to convert it
5400 first. If it is to be narrower, it is *incorrect* to convert it
5401 first. */
5402 else if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
5404 op0 = convert_modes (target_mode, mode, op0, 0);
5405 mode = target_mode;
5408 if (target_mode != mode)
5409 subtarget = 0;
5411 if (code == GE)
5412 op0 = expand_unop (mode, one_cmpl_optab, op0,
5413 ((STORE_FLAG_VALUE == 1 || normalizep)
5414 ? 0 : subtarget), 0);
5416 if (STORE_FLAG_VALUE == 1 || normalizep)
5417 /* If we are supposed to produce a 0/1 value, we want to do
5418 a logical shift from the sign bit to the low-order bit; for
5419 a -1/0 value, we do an arithmetic shift. */
5420 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
5421 GET_MODE_BITSIZE (mode) - 1,
5422 subtarget, normalizep != -1);
5424 if (mode != target_mode)
5425 op0 = convert_modes (target_mode, mode, op0, 0);
5427 return op0;
5430 mclass = GET_MODE_CLASS (mode);
5431 for (compare_mode = mode; compare_mode != VOIDmode;
5432 compare_mode = GET_MODE_WIDER_MODE (compare_mode))
5434 enum machine_mode optab_mode = mclass == MODE_CC ? CCmode : compare_mode;
5435 icode = optab_handler (cstore_optab, optab_mode);
5436 if (icode != CODE_FOR_nothing)
5438 do_pending_stack_adjust ();
5439 tem = emit_cstore (target, icode, code, mode, compare_mode,
5440 unsignedp, op0, op1, normalizep, target_mode);
5441 if (tem)
5442 return tem;
5444 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5446 tem = emit_cstore (target, icode, scode, mode, compare_mode,
5447 unsignedp, op1, op0, normalizep, target_mode);
5448 if (tem)
5449 return tem;
5451 break;
5455 return 0;
5458 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5459 and storing in TARGET. Normally return TARGET.
5460 Return 0 if that cannot be done.
5462 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5463 it is VOIDmode, they cannot both be CONST_INT.
5465 UNSIGNEDP is for the case where we have to widen the operands
5466 to perform the operation. It says to use zero-extension.
5468 NORMALIZEP is 1 if we should convert the result to be either zero
5469 or one. Normalize is -1 if we should convert the result to be
5470 either zero or -1. If NORMALIZEP is zero, the result will be left
5471 "raw" out of the scc insn. */
5474 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
5475 enum machine_mode mode, int unsignedp, int normalizep)
5477 enum machine_mode target_mode = target ? GET_MODE (target) : VOIDmode;
5478 enum rtx_code rcode;
5479 rtx subtarget;
5480 rtx tem, last, trueval;
5482 /* If we compare constants, we shouldn't use a store-flag operation,
5483 but a constant load. We can get there via the vanilla route that
5484 usually generates a compare-branch sequence, but will in this case
5485 fold the comparison to a constant, and thus elide the branch. */
5486 if (CONSTANT_P (op0) && CONSTANT_P (op1))
5487 return NULL_RTX;
5489 tem = emit_store_flag_1 (target, code, op0, op1, mode, unsignedp, normalizep,
5490 target_mode);
5491 if (tem)
5492 return tem;
5494 /* If we reached here, we can't do this with a scc insn, however there
5495 are some comparisons that can be done in other ways. Don't do any
5496 of these cases if branches are very cheap. */
5497 if (BRANCH_COST (optimize_insn_for_speed_p (), false) == 0)
5498 return 0;
5500 /* See what we need to return. We can only return a 1, -1, or the
5501 sign bit. */
5503 if (normalizep == 0)
5505 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5506 normalizep = STORE_FLAG_VALUE;
5508 else if (val_signbit_p (mode, STORE_FLAG_VALUE))
5510 else
5511 return 0;
5514 last = get_last_insn ();
5516 /* If optimizing, use different pseudo registers for each insn, instead
5517 of reusing the same pseudo. This leads to better CSE, but slows
5518 down the compiler, since there are more pseudos */
5519 subtarget = (!optimize
5520 && (target_mode == mode)) ? target : NULL_RTX;
5521 trueval = GEN_INT (normalizep ? normalizep : STORE_FLAG_VALUE);
5523 /* For floating-point comparisons, try the reverse comparison or try
5524 changing the "orderedness" of the comparison. */
5525 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5527 enum rtx_code first_code;
5528 bool and_them;
5530 rcode = reverse_condition_maybe_unordered (code);
5531 if (can_compare_p (rcode, mode, ccp_store_flag)
5532 && (code == ORDERED || code == UNORDERED
5533 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
5534 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
5536 int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
5537 || (STORE_FLAG_VALUE == -1 && normalizep == 1));
5539 /* For the reverse comparison, use either an addition or a XOR. */
5540 if (want_add
5541 && rtx_cost (GEN_INT (normalizep), PLUS, 1,
5542 optimize_insn_for_speed_p ()) == 0)
5544 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5545 STORE_FLAG_VALUE, target_mode);
5546 if (tem)
5547 return expand_binop (target_mode, add_optab, tem,
5548 gen_int_mode (normalizep, target_mode),
5549 target, 0, OPTAB_WIDEN);
5551 else if (!want_add
5552 && rtx_cost (trueval, XOR, 1,
5553 optimize_insn_for_speed_p ()) == 0)
5555 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5556 normalizep, target_mode);
5557 if (tem)
5558 return expand_binop (target_mode, xor_optab, tem, trueval,
5559 target, INTVAL (trueval) >= 0, OPTAB_WIDEN);
5563 delete_insns_since (last);
5565 /* Cannot split ORDERED and UNORDERED, only try the above trick. */
5566 if (code == ORDERED || code == UNORDERED)
5567 return 0;
5569 and_them = split_comparison (code, mode, &first_code, &code);
5571 /* If there are no NaNs, the first comparison should always fall through.
5572 Effectively change the comparison to the other one. */
5573 if (!HONOR_NANS (mode))
5575 gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
5576 return emit_store_flag_1 (target, code, op0, op1, mode, 0, normalizep,
5577 target_mode);
5580 #ifdef HAVE_conditional_move
5581 /* Try using a setcc instruction for ORDERED/UNORDERED, followed by a
5582 conditional move. */
5583 tem = emit_store_flag_1 (subtarget, first_code, op0, op1, mode, 0,
5584 normalizep, target_mode);
5585 if (tem == 0)
5586 return 0;
5588 if (and_them)
5589 tem = emit_conditional_move (target, code, op0, op1, mode,
5590 tem, const0_rtx, GET_MODE (tem), 0);
5591 else
5592 tem = emit_conditional_move (target, code, op0, op1, mode,
5593 trueval, tem, GET_MODE (tem), 0);
5595 if (tem == 0)
5596 delete_insns_since (last);
5597 return tem;
5598 #else
5599 return 0;
5600 #endif
5603 /* The remaining tricks only apply to integer comparisons. */
5605 if (GET_MODE_CLASS (mode) != MODE_INT)
5606 return 0;
5608 /* If this is an equality comparison of integers, we can try to exclusive-or
5609 (or subtract) the two operands and use a recursive call to try the
5610 comparison with zero. Don't do any of these cases if branches are
5611 very cheap. */
5613 if ((code == EQ || code == NE) && op1 != const0_rtx)
5615 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5616 OPTAB_WIDEN);
5618 if (tem == 0)
5619 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5620 OPTAB_WIDEN);
5621 if (tem != 0)
5622 tem = emit_store_flag (target, code, tem, const0_rtx,
5623 mode, unsignedp, normalizep);
5624 if (tem != 0)
5625 return tem;
5627 delete_insns_since (last);
5630 /* For integer comparisons, try the reverse comparison. However, for
5631 small X and if we'd have anyway to extend, implementing "X != 0"
5632 as "-(int)X >> 31" is still cheaper than inverting "(int)X == 0". */
5633 rcode = reverse_condition (code);
5634 if (can_compare_p (rcode, mode, ccp_store_flag)
5635 && ! (optab_handler (cstore_optab, mode) == CODE_FOR_nothing
5636 && code == NE
5637 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
5638 && op1 == const0_rtx))
5640 int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
5641 || (STORE_FLAG_VALUE == -1 && normalizep == 1));
5643 /* Again, for the reverse comparison, use either an addition or a XOR. */
5644 if (want_add
5645 && rtx_cost (GEN_INT (normalizep), PLUS, 1,
5646 optimize_insn_for_speed_p ()) == 0)
5648 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5649 STORE_FLAG_VALUE, target_mode);
5650 if (tem != 0)
5651 tem = expand_binop (target_mode, add_optab, tem,
5652 gen_int_mode (normalizep, target_mode),
5653 target, 0, OPTAB_WIDEN);
5655 else if (!want_add
5656 && rtx_cost (trueval, XOR, 1,
5657 optimize_insn_for_speed_p ()) == 0)
5659 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5660 normalizep, target_mode);
5661 if (tem != 0)
5662 tem = expand_binop (target_mode, xor_optab, tem, trueval, target,
5663 INTVAL (trueval) >= 0, OPTAB_WIDEN);
5666 if (tem != 0)
5667 return tem;
5668 delete_insns_since (last);
5671 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5672 the constant zero. Reject all other comparisons at this point. Only
5673 do LE and GT if branches are expensive since they are expensive on
5674 2-operand machines. */
5676 if (op1 != const0_rtx
5677 || (code != EQ && code != NE
5678 && (BRANCH_COST (optimize_insn_for_speed_p (),
5679 false) <= 1 || (code != LE && code != GT))))
5680 return 0;
5682 /* Try to put the result of the comparison in the sign bit. Assume we can't
5683 do the necessary operation below. */
5685 tem = 0;
5687 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5688 the sign bit set. */
5690 if (code == LE)
5692 /* This is destructive, so SUBTARGET can't be OP0. */
5693 if (rtx_equal_p (subtarget, op0))
5694 subtarget = 0;
5696 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5697 OPTAB_WIDEN);
5698 if (tem)
5699 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5700 OPTAB_WIDEN);
5703 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5704 number of bits in the mode of OP0, minus one. */
5706 if (code == GT)
5708 if (rtx_equal_p (subtarget, op0))
5709 subtarget = 0;
5711 tem = expand_shift (RSHIFT_EXPR, mode, op0,
5712 GET_MODE_BITSIZE (mode) - 1,
5713 subtarget, 0);
5714 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5715 OPTAB_WIDEN);
5718 if (code == EQ || code == NE)
5720 /* For EQ or NE, one way to do the comparison is to apply an operation
5721 that converts the operand into a positive number if it is nonzero
5722 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5723 for NE we negate. This puts the result in the sign bit. Then we
5724 normalize with a shift, if needed.
5726 Two operations that can do the above actions are ABS and FFS, so try
5727 them. If that doesn't work, and MODE is smaller than a full word,
5728 we can use zero-extension to the wider mode (an unsigned conversion)
5729 as the operation. */
5731 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5732 that is compensated by the subsequent overflow when subtracting
5733 one / negating. */
5735 if (optab_handler (abs_optab, mode) != CODE_FOR_nothing)
5736 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5737 else if (optab_handler (ffs_optab, mode) != CODE_FOR_nothing)
5738 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5739 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5741 tem = convert_modes (word_mode, mode, op0, 1);
5742 mode = word_mode;
5745 if (tem != 0)
5747 if (code == EQ)
5748 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5749 0, OPTAB_WIDEN);
5750 else
5751 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5754 /* If we couldn't do it that way, for NE we can "or" the two's complement
5755 of the value with itself. For EQ, we take the one's complement of
5756 that "or", which is an extra insn, so we only handle EQ if branches
5757 are expensive. */
5759 if (tem == 0
5760 && (code == NE
5761 || BRANCH_COST (optimize_insn_for_speed_p (),
5762 false) > 1))
5764 if (rtx_equal_p (subtarget, op0))
5765 subtarget = 0;
5767 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5768 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5769 OPTAB_WIDEN);
5771 if (tem && code == EQ)
5772 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5776 if (tem && normalizep)
5777 tem = expand_shift (RSHIFT_EXPR, mode, tem,
5778 GET_MODE_BITSIZE (mode) - 1,
5779 subtarget, normalizep == 1);
5781 if (tem)
5783 if (!target)
5785 else if (GET_MODE (tem) != target_mode)
5787 convert_move (target, tem, 0);
5788 tem = target;
5790 else if (!subtarget)
5792 emit_move_insn (target, tem);
5793 tem = target;
5796 else
5797 delete_insns_since (last);
5799 return tem;
5802 /* Like emit_store_flag, but always succeeds. */
5805 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5806 enum machine_mode mode, int unsignedp, int normalizep)
5808 rtx tem, label;
5809 rtx trueval, falseval;
5811 /* First see if emit_store_flag can do the job. */
5812 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5813 if (tem != 0)
5814 return tem;
5816 if (!target)
5817 target = gen_reg_rtx (word_mode);
5819 /* If this failed, we have to do this with set/compare/jump/set code.
5820 For foo != 0, if foo is in OP0, just replace it with 1 if nonzero. */
5821 trueval = normalizep ? GEN_INT (normalizep) : const1_rtx;
5822 if (code == NE
5823 && GET_MODE_CLASS (mode) == MODE_INT
5824 && REG_P (target)
5825 && op0 == target
5826 && op1 == const0_rtx)
5828 label = gen_label_rtx ();
5829 do_compare_rtx_and_jump (target, const0_rtx, EQ, unsignedp,
5830 mode, NULL_RTX, NULL_RTX, label, -1);
5831 emit_move_insn (target, trueval);
5832 emit_label (label);
5833 return target;
5836 if (!REG_P (target)
5837 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5838 target = gen_reg_rtx (GET_MODE (target));
5840 /* Jump in the right direction if the target cannot implement CODE
5841 but can jump on its reverse condition. */
5842 falseval = const0_rtx;
5843 if (! can_compare_p (code, mode, ccp_jump)
5844 && (! FLOAT_MODE_P (mode)
5845 || code == ORDERED || code == UNORDERED
5846 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
5847 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
5849 enum rtx_code rcode;
5850 if (FLOAT_MODE_P (mode))
5851 rcode = reverse_condition_maybe_unordered (code);
5852 else
5853 rcode = reverse_condition (code);
5855 /* Canonicalize to UNORDERED for the libcall. */
5856 if (can_compare_p (rcode, mode, ccp_jump)
5857 || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
5859 falseval = trueval;
5860 trueval = const0_rtx;
5861 code = rcode;
5865 emit_move_insn (target, trueval);
5866 label = gen_label_rtx ();
5867 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
5868 NULL_RTX, label, -1);
5870 emit_move_insn (target, falseval);
5871 emit_label (label);
5873 return target;
5876 /* Perform possibly multi-word comparison and conditional jump to LABEL
5877 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE. This is
5878 now a thin wrapper around do_compare_rtx_and_jump. */
5880 static void
5881 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, enum machine_mode mode,
5882 rtx label)
5884 int unsignedp = (op == LTU || op == LEU || op == GTU || op == GEU);
5885 do_compare_rtx_and_jump (arg1, arg2, op, unsignedp, mode,
5886 NULL_RTX, NULL_RTX, label, -1);