2005-09-18 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / expmed.c
blob8f71b8b6437a5d45f46e9abe62cfc9672721b957
1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "toplev.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "tm_p.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "expr.h"
35 #include "optabs.h"
36 #include "real.h"
37 #include "recog.h"
38 #include "langhooks.h"
40 static void store_fixed_bit_field (rtx, unsigned HOST_WIDE_INT,
41 unsigned HOST_WIDE_INT,
42 unsigned HOST_WIDE_INT, rtx);
43 static void store_split_bit_field (rtx, unsigned HOST_WIDE_INT,
44 unsigned HOST_WIDE_INT, rtx);
45 static rtx extract_fixed_bit_field (enum machine_mode, rtx,
46 unsigned HOST_WIDE_INT,
47 unsigned HOST_WIDE_INT,
48 unsigned HOST_WIDE_INT, rtx, int);
49 static rtx mask_rtx (enum machine_mode, int, int, int);
50 static rtx lshift_value (enum machine_mode, rtx, int, int);
51 static rtx extract_split_bit_field (rtx, unsigned HOST_WIDE_INT,
52 unsigned HOST_WIDE_INT, int);
53 static void do_cmp_and_jump (rtx, rtx, enum rtx_code, enum machine_mode, rtx);
54 static rtx expand_smod_pow2 (enum machine_mode, rtx, HOST_WIDE_INT);
55 static rtx expand_sdiv_pow2 (enum machine_mode, rtx, HOST_WIDE_INT);
57 /* Test whether a value is zero of a power of two. */
58 #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
60 /* Nonzero means divides or modulus operations are relatively cheap for
61 powers of two, so don't use branches; emit the operation instead.
62 Usually, this will mean that the MD file will emit non-branch
63 sequences. */
65 static bool sdiv_pow2_cheap[NUM_MACHINE_MODES];
66 static bool smod_pow2_cheap[NUM_MACHINE_MODES];
68 #ifndef SLOW_UNALIGNED_ACCESS
69 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
70 #endif
72 /* For compilers that support multiple targets with different word sizes,
73 MAX_BITS_PER_WORD contains the biggest value of BITS_PER_WORD. An example
74 is the H8/300(H) compiler. */
76 #ifndef MAX_BITS_PER_WORD
77 #define MAX_BITS_PER_WORD BITS_PER_WORD
78 #endif
80 /* Reduce conditional compilation elsewhere. */
81 #ifndef HAVE_insv
82 #define HAVE_insv 0
83 #define CODE_FOR_insv CODE_FOR_nothing
84 #define gen_insv(a,b,c,d) NULL_RTX
85 #endif
86 #ifndef HAVE_extv
87 #define HAVE_extv 0
88 #define CODE_FOR_extv CODE_FOR_nothing
89 #define gen_extv(a,b,c,d) NULL_RTX
90 #endif
91 #ifndef HAVE_extzv
92 #define HAVE_extzv 0
93 #define CODE_FOR_extzv CODE_FOR_nothing
94 #define gen_extzv(a,b,c,d) NULL_RTX
95 #endif
97 /* Cost of various pieces of RTL. Note that some of these are indexed by
98 shift count and some by mode. */
99 static int zero_cost;
100 static int add_cost[NUM_MACHINE_MODES];
101 static int neg_cost[NUM_MACHINE_MODES];
102 static int shift_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
103 static int shiftadd_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
104 static int shiftsub_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
105 static int mul_cost[NUM_MACHINE_MODES];
106 static int div_cost[NUM_MACHINE_MODES];
107 static int mul_widen_cost[NUM_MACHINE_MODES];
108 static int mul_highpart_cost[NUM_MACHINE_MODES];
110 void
111 init_expmed (void)
113 struct
115 struct rtx_def reg; rtunion reg_fld[2];
116 struct rtx_def plus; rtunion plus_fld1;
117 struct rtx_def neg;
118 struct rtx_def udiv; rtunion udiv_fld1;
119 struct rtx_def mult; rtunion mult_fld1;
120 struct rtx_def div; rtunion div_fld1;
121 struct rtx_def mod; rtunion mod_fld1;
122 struct rtx_def zext;
123 struct rtx_def wide_mult; rtunion wide_mult_fld1;
124 struct rtx_def wide_lshr; rtunion wide_lshr_fld1;
125 struct rtx_def wide_trunc;
126 struct rtx_def shift; rtunion shift_fld1;
127 struct rtx_def shift_mult; rtunion shift_mult_fld1;
128 struct rtx_def shift_add; rtunion shift_add_fld1;
129 struct rtx_def shift_sub; rtunion shift_sub_fld1;
130 } all;
132 rtx pow2[MAX_BITS_PER_WORD];
133 rtx cint[MAX_BITS_PER_WORD];
134 int m, n;
135 enum machine_mode mode, wider_mode;
137 zero_cost = rtx_cost (const0_rtx, 0);
139 for (m = 1; m < MAX_BITS_PER_WORD; m++)
141 pow2[m] = GEN_INT ((HOST_WIDE_INT) 1 << m);
142 cint[m] = GEN_INT (m);
145 memset (&all, 0, sizeof all);
147 PUT_CODE (&all.reg, REG);
148 /* Avoid using hard regs in ways which may be unsupported. */
149 REGNO (&all.reg) = LAST_VIRTUAL_REGISTER + 1;
151 PUT_CODE (&all.plus, PLUS);
152 XEXP (&all.plus, 0) = &all.reg;
153 XEXP (&all.plus, 1) = &all.reg;
155 PUT_CODE (&all.neg, NEG);
156 XEXP (&all.neg, 0) = &all.reg;
158 PUT_CODE (&all.udiv, UDIV);
159 XEXP (&all.udiv, 0) = &all.reg;
160 XEXP (&all.udiv, 1) = &all.reg;
162 PUT_CODE (&all.mult, MULT);
163 XEXP (&all.mult, 0) = &all.reg;
164 XEXP (&all.mult, 1) = &all.reg;
166 PUT_CODE (&all.div, DIV);
167 XEXP (&all.div, 0) = &all.reg;
168 XEXP (&all.div, 1) = 32 < MAX_BITS_PER_WORD ? cint[32] : GEN_INT (32);
170 PUT_CODE (&all.mod, MOD);
171 XEXP (&all.mod, 0) = &all.reg;
172 XEXP (&all.mod, 1) = XEXP (&all.div, 1);
174 PUT_CODE (&all.zext, ZERO_EXTEND);
175 XEXP (&all.zext, 0) = &all.reg;
177 PUT_CODE (&all.wide_mult, MULT);
178 XEXP (&all.wide_mult, 0) = &all.zext;
179 XEXP (&all.wide_mult, 1) = &all.zext;
181 PUT_CODE (&all.wide_lshr, LSHIFTRT);
182 XEXP (&all.wide_lshr, 0) = &all.wide_mult;
184 PUT_CODE (&all.wide_trunc, TRUNCATE);
185 XEXP (&all.wide_trunc, 0) = &all.wide_lshr;
187 PUT_CODE (&all.shift, ASHIFT);
188 XEXP (&all.shift, 0) = &all.reg;
190 PUT_CODE (&all.shift_mult, MULT);
191 XEXP (&all.shift_mult, 0) = &all.reg;
193 PUT_CODE (&all.shift_add, PLUS);
194 XEXP (&all.shift_add, 0) = &all.shift_mult;
195 XEXP (&all.shift_add, 1) = &all.reg;
197 PUT_CODE (&all.shift_sub, MINUS);
198 XEXP (&all.shift_sub, 0) = &all.shift_mult;
199 XEXP (&all.shift_sub, 1) = &all.reg;
201 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
202 mode != VOIDmode;
203 mode = GET_MODE_WIDER_MODE (mode))
205 PUT_MODE (&all.reg, mode);
206 PUT_MODE (&all.plus, mode);
207 PUT_MODE (&all.neg, mode);
208 PUT_MODE (&all.udiv, mode);
209 PUT_MODE (&all.mult, mode);
210 PUT_MODE (&all.div, mode);
211 PUT_MODE (&all.mod, mode);
212 PUT_MODE (&all.wide_trunc, mode);
213 PUT_MODE (&all.shift, mode);
214 PUT_MODE (&all.shift_mult, mode);
215 PUT_MODE (&all.shift_add, mode);
216 PUT_MODE (&all.shift_sub, mode);
218 add_cost[mode] = rtx_cost (&all.plus, SET);
219 neg_cost[mode] = rtx_cost (&all.neg, SET);
220 div_cost[mode] = rtx_cost (&all.udiv, SET);
221 mul_cost[mode] = rtx_cost (&all.mult, SET);
223 sdiv_pow2_cheap[mode] = (rtx_cost (&all.div, SET) <= 2 * add_cost[mode]);
224 smod_pow2_cheap[mode] = (rtx_cost (&all.mod, SET) <= 4 * add_cost[mode]);
226 wider_mode = GET_MODE_WIDER_MODE (mode);
227 if (wider_mode != VOIDmode)
229 PUT_MODE (&all.zext, wider_mode);
230 PUT_MODE (&all.wide_mult, wider_mode);
231 PUT_MODE (&all.wide_lshr, wider_mode);
232 XEXP (&all.wide_lshr, 1) = GEN_INT (GET_MODE_BITSIZE (mode));
234 mul_widen_cost[wider_mode] = rtx_cost (&all.wide_mult, SET);
235 mul_highpart_cost[mode] = rtx_cost (&all.wide_trunc, SET);
238 shift_cost[mode][0] = 0;
239 shiftadd_cost[mode][0] = shiftsub_cost[mode][0] = add_cost[mode];
241 n = MIN (MAX_BITS_PER_WORD, GET_MODE_BITSIZE (mode));
242 for (m = 1; m < n; m++)
244 XEXP (&all.shift, 1) = cint[m];
245 XEXP (&all.shift_mult, 1) = pow2[m];
247 shift_cost[mode][m] = rtx_cost (&all.shift, SET);
248 shiftadd_cost[mode][m] = rtx_cost (&all.shift_add, SET);
249 shiftsub_cost[mode][m] = rtx_cost (&all.shift_sub, SET);
254 /* Return an rtx representing minus the value of X.
255 MODE is the intended mode of the result,
256 useful if X is a CONST_INT. */
259 negate_rtx (enum machine_mode mode, rtx x)
261 rtx result = simplify_unary_operation (NEG, mode, x, mode);
263 if (result == 0)
264 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
266 return result;
269 /* Report on the availability of insv/extv/extzv and the desired mode
270 of each of their operands. Returns MAX_MACHINE_MODE if HAVE_foo
271 is false; else the mode of the specified operand. If OPNO is -1,
272 all the caller cares about is whether the insn is available. */
273 enum machine_mode
274 mode_for_extraction (enum extraction_pattern pattern, int opno)
276 const struct insn_data *data;
278 switch (pattern)
280 case EP_insv:
281 if (HAVE_insv)
283 data = &insn_data[CODE_FOR_insv];
284 break;
286 return MAX_MACHINE_MODE;
288 case EP_extv:
289 if (HAVE_extv)
291 data = &insn_data[CODE_FOR_extv];
292 break;
294 return MAX_MACHINE_MODE;
296 case EP_extzv:
297 if (HAVE_extzv)
299 data = &insn_data[CODE_FOR_extzv];
300 break;
302 return MAX_MACHINE_MODE;
304 default:
305 gcc_unreachable ();
308 if (opno == -1)
309 return VOIDmode;
311 /* Everyone who uses this function used to follow it with
312 if (result == VOIDmode) result = word_mode; */
313 if (data->operand[opno].mode == VOIDmode)
314 return word_mode;
315 return data->operand[opno].mode;
319 /* Generate code to store value from rtx VALUE
320 into a bit-field within structure STR_RTX
321 containing BITSIZE bits starting at bit BITNUM.
322 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
323 ALIGN is the alignment that STR_RTX is known to have.
324 TOTAL_SIZE is the size of the structure in bytes, or -1 if varying. */
326 /* ??? Note that there are two different ideas here for how
327 to determine the size to count bits within, for a register.
328 One is BITS_PER_WORD, and the other is the size of operand 3
329 of the insv pattern.
331 If operand 3 of the insv pattern is VOIDmode, then we will use BITS_PER_WORD
332 else, we use the mode of operand 3. */
335 store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
336 unsigned HOST_WIDE_INT bitnum, enum machine_mode fieldmode,
337 rtx value)
339 unsigned int unit
340 = (MEM_P (str_rtx)) ? BITS_PER_UNIT : BITS_PER_WORD;
341 unsigned HOST_WIDE_INT offset, bitpos;
342 rtx op0 = str_rtx;
343 int byte_offset;
344 rtx orig_value;
346 enum machine_mode op_mode = mode_for_extraction (EP_insv, 3);
348 while (GET_CODE (op0) == SUBREG)
350 /* The following line once was done only if WORDS_BIG_ENDIAN,
351 but I think that is a mistake. WORDS_BIG_ENDIAN is
352 meaningful at a much higher level; when structures are copied
353 between memory and regs, the higher-numbered regs
354 always get higher addresses. */
355 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
356 op0 = SUBREG_REG (op0);
359 /* No action is needed if the target is a register and if the field
360 lies completely outside that register. This can occur if the source
361 code contains an out-of-bounds access to a small array. */
362 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
363 return value;
365 /* Use vec_set patterns for inserting parts of vectors whenever
366 available. */
367 if (VECTOR_MODE_P (GET_MODE (op0))
368 && !MEM_P (op0)
369 && (vec_set_optab->handlers[GET_MODE (op0)].insn_code
370 != CODE_FOR_nothing)
371 && fieldmode == GET_MODE_INNER (GET_MODE (op0))
372 && bitsize == GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
373 && !(bitnum % GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
375 enum machine_mode outermode = GET_MODE (op0);
376 enum machine_mode innermode = GET_MODE_INNER (outermode);
377 int icode = (int) vec_set_optab->handlers[outermode].insn_code;
378 int pos = bitnum / GET_MODE_BITSIZE (innermode);
379 rtx rtxpos = GEN_INT (pos);
380 rtx src = value;
381 rtx dest = op0;
382 rtx pat, seq;
383 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
384 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
385 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
387 start_sequence ();
389 if (! (*insn_data[icode].operand[1].predicate) (src, mode1))
390 src = copy_to_mode_reg (mode1, src);
392 if (! (*insn_data[icode].operand[2].predicate) (rtxpos, mode2))
393 rtxpos = copy_to_mode_reg (mode1, rtxpos);
395 /* We could handle this, but we should always be called with a pseudo
396 for our targets and all insns should take them as outputs. */
397 gcc_assert ((*insn_data[icode].operand[0].predicate) (dest, mode0)
398 && (*insn_data[icode].operand[1].predicate) (src, mode1)
399 && (*insn_data[icode].operand[2].predicate) (rtxpos, mode2));
400 pat = GEN_FCN (icode) (dest, src, rtxpos);
401 seq = get_insns ();
402 end_sequence ();
403 if (pat)
405 emit_insn (seq);
406 emit_insn (pat);
407 return dest;
411 /* If the target is a register, overwriting the entire object, or storing
412 a full-word or multi-word field can be done with just a SUBREG.
414 If the target is memory, storing any naturally aligned field can be
415 done with a simple store. For targets that support fast unaligned
416 memory, any naturally sized, unit aligned field can be done directly. */
418 offset = bitnum / unit;
419 bitpos = bitnum % unit;
420 byte_offset = (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
421 + (offset * UNITS_PER_WORD);
423 if (bitpos == 0
424 && bitsize == GET_MODE_BITSIZE (fieldmode)
425 && (!MEM_P (op0)
426 ? ((GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
427 || GET_MODE_SIZE (GET_MODE (op0)) == GET_MODE_SIZE (fieldmode))
428 && byte_offset % GET_MODE_SIZE (fieldmode) == 0)
429 : (! SLOW_UNALIGNED_ACCESS (fieldmode, MEM_ALIGN (op0))
430 || (offset * BITS_PER_UNIT % bitsize == 0
431 && MEM_ALIGN (op0) % GET_MODE_BITSIZE (fieldmode) == 0))))
433 if (GET_MODE (op0) != fieldmode)
435 if (MEM_P (op0))
436 op0 = adjust_address (op0, fieldmode, offset);
437 else
438 op0 = simplify_gen_subreg (fieldmode, op0, GET_MODE (op0),
439 byte_offset);
441 emit_move_insn (op0, value);
442 return value;
445 /* Make sure we are playing with integral modes. Pun with subregs
446 if we aren't. This must come after the entire register case above,
447 since that case is valid for any mode. The following cases are only
448 valid for integral modes. */
450 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
451 if (imode != GET_MODE (op0))
453 if (MEM_P (op0))
454 op0 = adjust_address (op0, imode, 0);
455 else
457 gcc_assert (imode != BLKmode);
458 op0 = gen_lowpart (imode, op0);
463 /* We may be accessing data outside the field, which means
464 we can alias adjacent data. */
465 if (MEM_P (op0))
467 op0 = shallow_copy_rtx (op0);
468 set_mem_alias_set (op0, 0);
469 set_mem_expr (op0, 0);
472 /* If OP0 is a register, BITPOS must count within a word.
473 But as we have it, it counts within whatever size OP0 now has.
474 On a bigendian machine, these are not the same, so convert. */
475 if (BYTES_BIG_ENDIAN
476 && !MEM_P (op0)
477 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
478 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
480 /* Storing an lsb-aligned field in a register
481 can be done with a movestrict instruction. */
483 if (!MEM_P (op0)
484 && (BYTES_BIG_ENDIAN ? bitpos + bitsize == unit : bitpos == 0)
485 && bitsize == GET_MODE_BITSIZE (fieldmode)
486 && (movstrict_optab->handlers[fieldmode].insn_code
487 != CODE_FOR_nothing))
489 int icode = movstrict_optab->handlers[fieldmode].insn_code;
491 /* Get appropriate low part of the value being stored. */
492 if (GET_CODE (value) == CONST_INT || REG_P (value))
493 value = gen_lowpart (fieldmode, value);
494 else if (!(GET_CODE (value) == SYMBOL_REF
495 || GET_CODE (value) == LABEL_REF
496 || GET_CODE (value) == CONST))
497 value = convert_to_mode (fieldmode, value, 0);
499 if (! (*insn_data[icode].operand[1].predicate) (value, fieldmode))
500 value = copy_to_mode_reg (fieldmode, value);
502 if (GET_CODE (op0) == SUBREG)
504 /* Else we've got some float mode source being extracted into
505 a different float mode destination -- this combination of
506 subregs results in Severe Tire Damage. */
507 gcc_assert (GET_MODE (SUBREG_REG (op0)) == fieldmode
508 || GET_MODE_CLASS (fieldmode) == MODE_INT
509 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
510 op0 = SUBREG_REG (op0);
513 emit_insn (GEN_FCN (icode)
514 (gen_rtx_SUBREG (fieldmode, op0,
515 (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
516 + (offset * UNITS_PER_WORD)),
517 value));
519 return value;
522 /* Handle fields bigger than a word. */
524 if (bitsize > BITS_PER_WORD)
526 /* Here we transfer the words of the field
527 in the order least significant first.
528 This is because the most significant word is the one which may
529 be less than full.
530 However, only do that if the value is not BLKmode. */
532 unsigned int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
533 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
534 unsigned int i;
536 /* This is the mode we must force value to, so that there will be enough
537 subwords to extract. Note that fieldmode will often (always?) be
538 VOIDmode, because that is what store_field uses to indicate that this
539 is a bit field, but passing VOIDmode to operand_subword_force
540 is not allowed. */
541 fieldmode = GET_MODE (value);
542 if (fieldmode == VOIDmode)
543 fieldmode = smallest_mode_for_size (nwords * BITS_PER_WORD, MODE_INT);
545 for (i = 0; i < nwords; i++)
547 /* If I is 0, use the low-order word in both field and target;
548 if I is 1, use the next to lowest word; and so on. */
549 unsigned int wordnum = (backwards ? nwords - i - 1 : i);
550 unsigned int bit_offset = (backwards
551 ? MAX ((int) bitsize - ((int) i + 1)
552 * BITS_PER_WORD,
554 : (int) i * BITS_PER_WORD);
556 store_bit_field (op0, MIN (BITS_PER_WORD,
557 bitsize - i * BITS_PER_WORD),
558 bitnum + bit_offset, word_mode,
559 operand_subword_force (value, wordnum, fieldmode));
561 return value;
564 /* From here on we can assume that the field to be stored in is
565 a full-word (whatever type that is), since it is shorter than a word. */
567 /* OFFSET is the number of words or bytes (UNIT says which)
568 from STR_RTX to the first word or byte containing part of the field. */
570 if (!MEM_P (op0))
572 if (offset != 0
573 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
575 if (!REG_P (op0))
577 /* Since this is a destination (lvalue), we can't copy
578 it to a pseudo. We can remove a SUBREG that does not
579 change the size of the operand. Such a SUBREG may
580 have been added above. */
581 gcc_assert (GET_CODE (op0) == SUBREG
582 && (GET_MODE_SIZE (GET_MODE (op0))
583 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))));
584 op0 = SUBREG_REG (op0);
586 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
587 op0, (offset * UNITS_PER_WORD));
589 offset = 0;
592 /* If VALUE has a floating-point or complex mode, access it as an
593 integer of the corresponding size. This can occur on a machine
594 with 64 bit registers that uses SFmode for float. It can also
595 occur for unaligned float or complex fields. */
596 orig_value = value;
597 if (GET_MODE (value) != VOIDmode
598 && GET_MODE_CLASS (GET_MODE (value)) != MODE_INT
599 && GET_MODE_CLASS (GET_MODE (value)) != MODE_PARTIAL_INT)
601 value = gen_reg_rtx (int_mode_for_mode (GET_MODE (value)));
602 emit_move_insn (gen_lowpart (GET_MODE (orig_value), value), orig_value);
605 /* Now OFFSET is nonzero only if OP0 is memory
606 and is therefore always measured in bytes. */
608 if (HAVE_insv
609 && GET_MODE (value) != BLKmode
610 && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
611 /* Ensure insv's size is wide enough for this field. */
612 && (GET_MODE_BITSIZE (op_mode) >= bitsize)
613 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
614 && (bitsize + bitpos > GET_MODE_BITSIZE (op_mode))))
616 int xbitpos = bitpos;
617 rtx value1;
618 rtx xop0 = op0;
619 rtx last = get_last_insn ();
620 rtx pat;
621 enum machine_mode maxmode = mode_for_extraction (EP_insv, 3);
622 int save_volatile_ok = volatile_ok;
624 volatile_ok = 1;
626 /* If this machine's insv can only insert into a register, copy OP0
627 into a register and save it back later. */
628 if (MEM_P (op0)
629 && ! ((*insn_data[(int) CODE_FOR_insv].operand[0].predicate)
630 (op0, VOIDmode)))
632 rtx tempreg;
633 enum machine_mode bestmode;
635 /* Get the mode to use for inserting into this field. If OP0 is
636 BLKmode, get the smallest mode consistent with the alignment. If
637 OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
638 mode. Otherwise, use the smallest mode containing the field. */
640 if (GET_MODE (op0) == BLKmode
641 || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
642 bestmode
643 = get_best_mode (bitsize, bitnum, MEM_ALIGN (op0), maxmode,
644 MEM_VOLATILE_P (op0));
645 else
646 bestmode = GET_MODE (op0);
648 if (bestmode == VOIDmode
649 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (op0))
650 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (op0)))
651 goto insv_loses;
653 /* Adjust address to point to the containing unit of that mode.
654 Compute offset as multiple of this unit, counting in bytes. */
655 unit = GET_MODE_BITSIZE (bestmode);
656 offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
657 bitpos = bitnum % unit;
658 op0 = adjust_address (op0, bestmode, offset);
660 /* Fetch that unit, store the bitfield in it, then store
661 the unit. */
662 tempreg = copy_to_reg (op0);
663 store_bit_field (tempreg, bitsize, bitpos, fieldmode, orig_value);
664 emit_move_insn (op0, tempreg);
665 return value;
667 volatile_ok = save_volatile_ok;
669 /* Add OFFSET into OP0's address. */
670 if (MEM_P (xop0))
671 xop0 = adjust_address (xop0, byte_mode, offset);
673 /* If xop0 is a register, we need it in MAXMODE
674 to make it acceptable to the format of insv. */
675 if (GET_CODE (xop0) == SUBREG)
676 /* We can't just change the mode, because this might clobber op0,
677 and we will need the original value of op0 if insv fails. */
678 xop0 = gen_rtx_SUBREG (maxmode, SUBREG_REG (xop0), SUBREG_BYTE (xop0));
679 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
680 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
682 /* On big-endian machines, we count bits from the most significant.
683 If the bit field insn does not, we must invert. */
685 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
686 xbitpos = unit - bitsize - xbitpos;
688 /* We have been counting XBITPOS within UNIT.
689 Count instead within the size of the register. */
690 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
691 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
693 unit = GET_MODE_BITSIZE (maxmode);
695 /* Convert VALUE to maxmode (which insv insn wants) in VALUE1. */
696 value1 = value;
697 if (GET_MODE (value) != maxmode)
699 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
701 /* Optimization: Don't bother really extending VALUE
702 if it has all the bits we will actually use. However,
703 if we must narrow it, be sure we do it correctly. */
705 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
707 rtx tmp;
709 tmp = simplify_subreg (maxmode, value1, GET_MODE (value), 0);
710 if (! tmp)
711 tmp = simplify_gen_subreg (maxmode,
712 force_reg (GET_MODE (value),
713 value1),
714 GET_MODE (value), 0);
715 value1 = tmp;
717 else
718 value1 = gen_lowpart (maxmode, value1);
720 else if (GET_CODE (value) == CONST_INT)
721 value1 = gen_int_mode (INTVAL (value), maxmode);
722 else
723 /* Parse phase is supposed to make VALUE's data type
724 match that of the component reference, which is a type
725 at least as wide as the field; so VALUE should have
726 a mode that corresponds to that type. */
727 gcc_assert (CONSTANT_P (value));
730 /* If this machine's insv insists on a register,
731 get VALUE1 into a register. */
732 if (! ((*insn_data[(int) CODE_FOR_insv].operand[3].predicate)
733 (value1, maxmode)))
734 value1 = force_reg (maxmode, value1);
736 pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
737 if (pat)
738 emit_insn (pat);
739 else
741 delete_insns_since (last);
742 store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
745 else
746 insv_loses:
747 /* Insv is not available; store using shifts and boolean ops. */
748 store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
749 return value;
752 /* Use shifts and boolean operations to store VALUE
753 into a bit field of width BITSIZE
754 in a memory location specified by OP0 except offset by OFFSET bytes.
755 (OFFSET must be 0 if OP0 is a register.)
756 The field starts at position BITPOS within the byte.
757 (If OP0 is a register, it may be a full word or a narrower mode,
758 but BITPOS still counts within a full word,
759 which is significant on bigendian machines.) */
761 static void
762 store_fixed_bit_field (rtx op0, unsigned HOST_WIDE_INT offset,
763 unsigned HOST_WIDE_INT bitsize,
764 unsigned HOST_WIDE_INT bitpos, rtx value)
766 enum machine_mode mode;
767 unsigned int total_bits = BITS_PER_WORD;
768 rtx subtarget, temp;
769 int all_zero = 0;
770 int all_one = 0;
772 /* There is a case not handled here:
773 a structure with a known alignment of just a halfword
774 and a field split across two aligned halfwords within the structure.
775 Or likewise a structure with a known alignment of just a byte
776 and a field split across two bytes.
777 Such cases are not supposed to be able to occur. */
779 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
781 gcc_assert (!offset);
782 /* Special treatment for a bit field split across two registers. */
783 if (bitsize + bitpos > BITS_PER_WORD)
785 store_split_bit_field (op0, bitsize, bitpos, value);
786 return;
789 else
791 /* Get the proper mode to use for this field. We want a mode that
792 includes the entire field. If such a mode would be larger than
793 a word, we won't be doing the extraction the normal way.
794 We don't want a mode bigger than the destination. */
796 mode = GET_MODE (op0);
797 if (GET_MODE_BITSIZE (mode) == 0
798 || GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (word_mode))
799 mode = word_mode;
800 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
801 MEM_ALIGN (op0), mode, MEM_VOLATILE_P (op0));
803 if (mode == VOIDmode)
805 /* The only way this should occur is if the field spans word
806 boundaries. */
807 store_split_bit_field (op0, bitsize, bitpos + offset * BITS_PER_UNIT,
808 value);
809 return;
812 total_bits = GET_MODE_BITSIZE (mode);
814 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
815 be in the range 0 to total_bits-1, and put any excess bytes in
816 OFFSET. */
817 if (bitpos >= total_bits)
819 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
820 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
821 * BITS_PER_UNIT);
824 /* Get ref to an aligned byte, halfword, or word containing the field.
825 Adjust BITPOS to be position within a word,
826 and OFFSET to be the offset of that word.
827 Then alter OP0 to refer to that word. */
828 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
829 offset -= (offset % (total_bits / BITS_PER_UNIT));
830 op0 = adjust_address (op0, mode, offset);
833 mode = GET_MODE (op0);
835 /* Now MODE is either some integral mode for a MEM as OP0,
836 or is a full-word for a REG as OP0. TOTAL_BITS corresponds.
837 The bit field is contained entirely within OP0.
838 BITPOS is the starting bit number within OP0.
839 (OP0's mode may actually be narrower than MODE.) */
841 if (BYTES_BIG_ENDIAN)
842 /* BITPOS is the distance between our msb
843 and that of the containing datum.
844 Convert it to the distance from the lsb. */
845 bitpos = total_bits - bitsize - bitpos;
847 /* Now BITPOS is always the distance between our lsb
848 and that of OP0. */
850 /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
851 we must first convert its mode to MODE. */
853 if (GET_CODE (value) == CONST_INT)
855 HOST_WIDE_INT v = INTVAL (value);
857 if (bitsize < HOST_BITS_PER_WIDE_INT)
858 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
860 if (v == 0)
861 all_zero = 1;
862 else if ((bitsize < HOST_BITS_PER_WIDE_INT
863 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
864 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
865 all_one = 1;
867 value = lshift_value (mode, value, bitpos, bitsize);
869 else
871 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
872 && bitpos + bitsize != GET_MODE_BITSIZE (mode));
874 if (GET_MODE (value) != mode)
876 if ((REG_P (value) || GET_CODE (value) == SUBREG)
877 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
878 value = gen_lowpart (mode, value);
879 else
880 value = convert_to_mode (mode, value, 1);
883 if (must_and)
884 value = expand_binop (mode, and_optab, value,
885 mask_rtx (mode, 0, bitsize, 0),
886 NULL_RTX, 1, OPTAB_LIB_WIDEN);
887 if (bitpos > 0)
888 value = expand_shift (LSHIFT_EXPR, mode, value,
889 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
892 /* Now clear the chosen bits in OP0,
893 except that if VALUE is -1 we need not bother. */
895 subtarget = op0;
897 if (! all_one)
899 temp = expand_binop (mode, and_optab, op0,
900 mask_rtx (mode, bitpos, bitsize, 1),
901 subtarget, 1, OPTAB_LIB_WIDEN);
902 subtarget = temp;
904 else
905 temp = op0;
907 /* Now logical-or VALUE into OP0, unless it is zero. */
909 if (! all_zero)
910 temp = expand_binop (mode, ior_optab, temp, value,
911 subtarget, 1, OPTAB_LIB_WIDEN);
912 if (op0 != temp)
913 emit_move_insn (op0, temp);
916 /* Store a bit field that is split across multiple accessible memory objects.
918 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
919 BITSIZE is the field width; BITPOS the position of its first bit
920 (within the word).
921 VALUE is the value to store.
923 This does not yet handle fields wider than BITS_PER_WORD. */
925 static void
926 store_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
927 unsigned HOST_WIDE_INT bitpos, rtx value)
929 unsigned int unit;
930 unsigned int bitsdone = 0;
932 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
933 much at a time. */
934 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
935 unit = BITS_PER_WORD;
936 else
937 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
939 /* If VALUE is a constant other than a CONST_INT, get it into a register in
940 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
941 that VALUE might be a floating-point constant. */
942 if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
944 rtx word = gen_lowpart_common (word_mode, value);
946 if (word && (value != word))
947 value = word;
948 else
949 value = gen_lowpart_common (word_mode,
950 force_reg (GET_MODE (value) != VOIDmode
951 ? GET_MODE (value)
952 : word_mode, value));
955 while (bitsdone < bitsize)
957 unsigned HOST_WIDE_INT thissize;
958 rtx part, word;
959 unsigned HOST_WIDE_INT thispos;
960 unsigned HOST_WIDE_INT offset;
962 offset = (bitpos + bitsdone) / unit;
963 thispos = (bitpos + bitsdone) % unit;
965 /* THISSIZE must not overrun a word boundary. Otherwise,
966 store_fixed_bit_field will call us again, and we will mutually
967 recurse forever. */
968 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
969 thissize = MIN (thissize, unit - thispos);
971 if (BYTES_BIG_ENDIAN)
973 int total_bits;
975 /* We must do an endian conversion exactly the same way as it is
976 done in extract_bit_field, so that the two calls to
977 extract_fixed_bit_field will have comparable arguments. */
978 if (!MEM_P (value) || GET_MODE (value) == BLKmode)
979 total_bits = BITS_PER_WORD;
980 else
981 total_bits = GET_MODE_BITSIZE (GET_MODE (value));
983 /* Fetch successively less significant portions. */
984 if (GET_CODE (value) == CONST_INT)
985 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
986 >> (bitsize - bitsdone - thissize))
987 & (((HOST_WIDE_INT) 1 << thissize) - 1));
988 else
989 /* The args are chosen so that the last part includes the
990 lsb. Give extract_bit_field the value it needs (with
991 endianness compensation) to fetch the piece we want. */
992 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
993 total_bits - bitsize + bitsdone,
994 NULL_RTX, 1);
996 else
998 /* Fetch successively more significant portions. */
999 if (GET_CODE (value) == CONST_INT)
1000 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1001 >> bitsdone)
1002 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1003 else
1004 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
1005 bitsdone, NULL_RTX, 1);
1008 /* If OP0 is a register, then handle OFFSET here.
1010 When handling multiword bitfields, extract_bit_field may pass
1011 down a word_mode SUBREG of a larger REG for a bitfield that actually
1012 crosses a word boundary. Thus, for a SUBREG, we must find
1013 the current word starting from the base register. */
1014 if (GET_CODE (op0) == SUBREG)
1016 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
1017 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1018 GET_MODE (SUBREG_REG (op0)));
1019 offset = 0;
1021 else if (REG_P (op0))
1023 word = operand_subword_force (op0, offset, GET_MODE (op0));
1024 offset = 0;
1026 else
1027 word = op0;
1029 /* OFFSET is in UNITs, and UNIT is in bits.
1030 store_fixed_bit_field wants offset in bytes. */
1031 store_fixed_bit_field (word, offset * unit / BITS_PER_UNIT, thissize,
1032 thispos, part);
1033 bitsdone += thissize;
1037 /* Generate code to extract a byte-field from STR_RTX
1038 containing BITSIZE bits, starting at BITNUM,
1039 and put it in TARGET if possible (if TARGET is nonzero).
1040 Regardless of TARGET, we return the rtx for where the value is placed.
1042 STR_RTX is the structure containing the byte (a REG or MEM).
1043 UNSIGNEDP is nonzero if this is an unsigned bit field.
1044 MODE is the natural mode of the field value once extracted.
1045 TMODE is the mode the caller would like the value to have;
1046 but the value may be returned with type MODE instead.
1048 TOTAL_SIZE is the size in bytes of the containing structure,
1049 or -1 if varying.
1051 If a TARGET is specified and we can store in it at no extra cost,
1052 we do so, and return TARGET.
1053 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1054 if they are equally easy. */
1057 extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1058 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1059 enum machine_mode mode, enum machine_mode tmode)
1061 unsigned int unit
1062 = (MEM_P (str_rtx)) ? BITS_PER_UNIT : BITS_PER_WORD;
1063 unsigned HOST_WIDE_INT offset, bitpos;
1064 rtx op0 = str_rtx;
1065 rtx spec_target = target;
1066 rtx spec_target_subreg = 0;
1067 enum machine_mode int_mode;
1068 enum machine_mode extv_mode = mode_for_extraction (EP_extv, 0);
1069 enum machine_mode extzv_mode = mode_for_extraction (EP_extzv, 0);
1070 enum machine_mode mode1;
1071 int byte_offset;
1073 if (tmode == VOIDmode)
1074 tmode = mode;
1076 while (GET_CODE (op0) == SUBREG)
1078 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1079 op0 = SUBREG_REG (op0);
1082 /* If we have an out-of-bounds access to a register, just return an
1083 uninitialized register of the required mode. This can occur if the
1084 source code contains an out-of-bounds access to a small array. */
1085 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
1086 return gen_reg_rtx (tmode);
1088 if (REG_P (op0)
1089 && mode == GET_MODE (op0)
1090 && bitnum == 0
1091 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
1093 /* We're trying to extract a full register from itself. */
1094 return op0;
1097 /* Use vec_extract patterns for extracting parts of vectors whenever
1098 available. */
1099 if (VECTOR_MODE_P (GET_MODE (op0))
1100 && !MEM_P (op0)
1101 && (vec_extract_optab->handlers[GET_MODE (op0)].insn_code
1102 != CODE_FOR_nothing)
1103 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
1104 == bitnum / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
1106 enum machine_mode outermode = GET_MODE (op0);
1107 enum machine_mode innermode = GET_MODE_INNER (outermode);
1108 int icode = (int) vec_extract_optab->handlers[outermode].insn_code;
1109 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1110 rtx rtxpos = GEN_INT (pos);
1111 rtx src = op0;
1112 rtx dest = NULL, pat, seq;
1113 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
1114 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
1115 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
1117 if (innermode == tmode || innermode == mode)
1118 dest = target;
1120 if (!dest)
1121 dest = gen_reg_rtx (innermode);
1123 start_sequence ();
1125 if (! (*insn_data[icode].operand[0].predicate) (dest, mode0))
1126 dest = copy_to_mode_reg (mode0, dest);
1128 if (! (*insn_data[icode].operand[1].predicate) (src, mode1))
1129 src = copy_to_mode_reg (mode1, src);
1131 if (! (*insn_data[icode].operand[2].predicate) (rtxpos, mode2))
1132 rtxpos = copy_to_mode_reg (mode1, rtxpos);
1134 /* We could handle this, but we should always be called with a pseudo
1135 for our targets and all insns should take them as outputs. */
1136 gcc_assert ((*insn_data[icode].operand[0].predicate) (dest, mode0)
1137 && (*insn_data[icode].operand[1].predicate) (src, mode1)
1138 && (*insn_data[icode].operand[2].predicate) (rtxpos, mode2));
1140 pat = GEN_FCN (icode) (dest, src, rtxpos);
1141 seq = get_insns ();
1142 end_sequence ();
1143 if (pat)
1145 emit_insn (seq);
1146 emit_insn (pat);
1147 return dest;
1151 /* Make sure we are playing with integral modes. Pun with subregs
1152 if we aren't. */
1154 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
1155 if (imode != GET_MODE (op0))
1157 if (MEM_P (op0))
1158 op0 = adjust_address (op0, imode, 0);
1159 else
1161 gcc_assert (imode != BLKmode);
1162 op0 = gen_lowpart (imode, op0);
1164 /* If we got a SUBREG, force it into a register since we
1165 aren't going to be able to do another SUBREG on it. */
1166 if (GET_CODE (op0) == SUBREG)
1167 op0 = force_reg (imode, op0);
1172 /* We may be accessing data outside the field, which means
1173 we can alias adjacent data. */
1174 if (MEM_P (op0))
1176 op0 = shallow_copy_rtx (op0);
1177 set_mem_alias_set (op0, 0);
1178 set_mem_expr (op0, 0);
1181 /* Extraction of a full-word or multi-word value from a structure
1182 in a register or aligned memory can be done with just a SUBREG.
1183 A subword value in the least significant part of a register
1184 can also be extracted with a SUBREG. For this, we need the
1185 byte offset of the value in op0. */
1187 bitpos = bitnum % unit;
1188 offset = bitnum / unit;
1189 byte_offset = bitpos / BITS_PER_UNIT + offset * UNITS_PER_WORD;
1191 /* If OP0 is a register, BITPOS must count within a word.
1192 But as we have it, it counts within whatever size OP0 now has.
1193 On a bigendian machine, these are not the same, so convert. */
1194 if (BYTES_BIG_ENDIAN
1195 && !MEM_P (op0)
1196 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
1197 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
1199 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1200 If that's wrong, the solution is to test for it and set TARGET to 0
1201 if needed. */
1203 /* Only scalar integer modes can be converted via subregs. There is an
1204 additional problem for FP modes here in that they can have a precision
1205 which is different from the size. mode_for_size uses precision, but
1206 we want a mode based on the size, so we must avoid calling it for FP
1207 modes. */
1208 mode1 = (SCALAR_INT_MODE_P (tmode)
1209 ? mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0)
1210 : mode);
1212 if (((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
1213 && bitpos % BITS_PER_WORD == 0)
1214 || (mode1 != BLKmode
1215 /* ??? The big endian test here is wrong. This is correct
1216 if the value is in a register, and if mode_for_size is not
1217 the same mode as op0. This causes us to get unnecessarily
1218 inefficient code from the Thumb port when -mbig-endian. */
1219 && (BYTES_BIG_ENDIAN
1220 ? bitpos + bitsize == BITS_PER_WORD
1221 : bitpos == 0)))
1222 && ((!MEM_P (op0)
1223 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1224 GET_MODE_BITSIZE (GET_MODE (op0)))
1225 && GET_MODE_SIZE (mode1) != 0
1226 && byte_offset % GET_MODE_SIZE (mode1) == 0)
1227 || (MEM_P (op0)
1228 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
1229 || (offset * BITS_PER_UNIT % bitsize == 0
1230 && MEM_ALIGN (op0) % bitsize == 0)))))
1232 if (mode1 != GET_MODE (op0))
1234 if (MEM_P (op0))
1235 op0 = adjust_address (op0, mode1, offset);
1236 else
1238 rtx sub = simplify_gen_subreg (mode1, op0, GET_MODE (op0),
1239 byte_offset);
1240 if (sub == NULL)
1241 goto no_subreg_mode_swap;
1242 op0 = sub;
1245 if (mode1 != mode)
1246 return convert_to_mode (tmode, op0, unsignedp);
1247 return op0;
1249 no_subreg_mode_swap:
1251 /* Handle fields bigger than a word. */
1253 if (bitsize > BITS_PER_WORD)
1255 /* Here we transfer the words of the field
1256 in the order least significant first.
1257 This is because the most significant word is the one which may
1258 be less than full. */
1260 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1261 unsigned int i;
1263 if (target == 0 || !REG_P (target))
1264 target = gen_reg_rtx (mode);
1266 /* Indicate for flow that the entire target reg is being set. */
1267 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
1269 for (i = 0; i < nwords; i++)
1271 /* If I is 0, use the low-order word in both field and target;
1272 if I is 1, use the next to lowest word; and so on. */
1273 /* Word number in TARGET to use. */
1274 unsigned int wordnum
1275 = (WORDS_BIG_ENDIAN
1276 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1277 : i);
1278 /* Offset from start of field in OP0. */
1279 unsigned int bit_offset = (WORDS_BIG_ENDIAN
1280 ? MAX (0, ((int) bitsize - ((int) i + 1)
1281 * (int) BITS_PER_WORD))
1282 : (int) i * BITS_PER_WORD);
1283 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1284 rtx result_part
1285 = extract_bit_field (op0, MIN (BITS_PER_WORD,
1286 bitsize - i * BITS_PER_WORD),
1287 bitnum + bit_offset, 1, target_part, mode,
1288 word_mode);
1290 gcc_assert (target_part);
1292 if (result_part != target_part)
1293 emit_move_insn (target_part, result_part);
1296 if (unsignedp)
1298 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1299 need to be zero'd out. */
1300 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1302 unsigned int i, total_words;
1304 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1305 for (i = nwords; i < total_words; i++)
1306 emit_move_insn
1307 (operand_subword (target,
1308 WORDS_BIG_ENDIAN ? total_words - i - 1 : i,
1309 1, VOIDmode),
1310 const0_rtx);
1312 return target;
1315 /* Signed bit field: sign-extend with two arithmetic shifts. */
1316 target = expand_shift (LSHIFT_EXPR, mode, target,
1317 build_int_cst (NULL_TREE,
1318 GET_MODE_BITSIZE (mode) - bitsize),
1319 NULL_RTX, 0);
1320 return expand_shift (RSHIFT_EXPR, mode, target,
1321 build_int_cst (NULL_TREE,
1322 GET_MODE_BITSIZE (mode) - bitsize),
1323 NULL_RTX, 0);
1326 /* From here on we know the desired field is smaller than a word. */
1328 /* Check if there is a correspondingly-sized integer field, so we can
1329 safely extract it as one size of integer, if necessary; then
1330 truncate or extend to the size that is wanted; then use SUBREGs or
1331 convert_to_mode to get one of the modes we really wanted. */
1333 int_mode = int_mode_for_mode (tmode);
1334 if (int_mode == BLKmode)
1335 int_mode = int_mode_for_mode (mode);
1336 /* Should probably push op0 out to memory and then do a load. */
1337 gcc_assert (int_mode != BLKmode);
1339 /* OFFSET is the number of words or bytes (UNIT says which)
1340 from STR_RTX to the first word or byte containing part of the field. */
1341 if (!MEM_P (op0))
1343 if (offset != 0
1344 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1346 if (!REG_P (op0))
1347 op0 = copy_to_reg (op0);
1348 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
1349 op0, (offset * UNITS_PER_WORD));
1351 offset = 0;
1354 /* Now OFFSET is nonzero only for memory operands. */
1356 if (unsignedp)
1358 if (HAVE_extzv
1359 && (GET_MODE_BITSIZE (extzv_mode) >= bitsize)
1360 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
1361 && (bitsize + bitpos > GET_MODE_BITSIZE (extzv_mode))))
1363 unsigned HOST_WIDE_INT xbitpos = bitpos, xoffset = offset;
1364 rtx bitsize_rtx, bitpos_rtx;
1365 rtx last = get_last_insn ();
1366 rtx xop0 = op0;
1367 rtx xtarget = target;
1368 rtx xspec_target = spec_target;
1369 rtx xspec_target_subreg = spec_target_subreg;
1370 rtx pat;
1371 enum machine_mode maxmode = mode_for_extraction (EP_extzv, 0);
1373 if (MEM_P (xop0))
1375 int save_volatile_ok = volatile_ok;
1376 volatile_ok = 1;
1378 /* Is the memory operand acceptable? */
1379 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[1].predicate)
1380 (xop0, GET_MODE (xop0))))
1382 /* No, load into a reg and extract from there. */
1383 enum machine_mode bestmode;
1385 /* Get the mode to use for inserting into this field. If
1386 OP0 is BLKmode, get the smallest mode consistent with the
1387 alignment. If OP0 is a non-BLKmode object that is no
1388 wider than MAXMODE, use its mode. Otherwise, use the
1389 smallest mode containing the field. */
1391 if (GET_MODE (xop0) == BLKmode
1392 || (GET_MODE_SIZE (GET_MODE (op0))
1393 > GET_MODE_SIZE (maxmode)))
1394 bestmode = get_best_mode (bitsize, bitnum,
1395 MEM_ALIGN (xop0), maxmode,
1396 MEM_VOLATILE_P (xop0));
1397 else
1398 bestmode = GET_MODE (xop0);
1400 if (bestmode == VOIDmode
1401 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (xop0))
1402 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (xop0)))
1403 goto extzv_loses;
1405 /* Compute offset as multiple of this unit,
1406 counting in bytes. */
1407 unit = GET_MODE_BITSIZE (bestmode);
1408 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1409 xbitpos = bitnum % unit;
1410 xop0 = adjust_address (xop0, bestmode, xoffset);
1412 /* Fetch it to a register in that size. */
1413 xop0 = force_reg (bestmode, xop0);
1415 /* XBITPOS counts within UNIT, which is what is expected. */
1417 else
1418 /* Get ref to first byte containing part of the field. */
1419 xop0 = adjust_address (xop0, byte_mode, xoffset);
1421 volatile_ok = save_volatile_ok;
1424 /* If op0 is a register, we need it in MAXMODE (which is usually
1425 SImode). to make it acceptable to the format of extzv. */
1426 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1427 goto extzv_loses;
1428 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
1429 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1431 /* On big-endian machines, we count bits from the most significant.
1432 If the bit field insn does not, we must invert. */
1433 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1434 xbitpos = unit - bitsize - xbitpos;
1436 /* Now convert from counting within UNIT to counting in MAXMODE. */
1437 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
1438 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
1440 unit = GET_MODE_BITSIZE (maxmode);
1442 if (xtarget == 0)
1443 xtarget = xspec_target = gen_reg_rtx (tmode);
1445 if (GET_MODE (xtarget) != maxmode)
1447 if (REG_P (xtarget))
1449 int wider = (GET_MODE_SIZE (maxmode)
1450 > GET_MODE_SIZE (GET_MODE (xtarget)));
1451 xtarget = gen_lowpart (maxmode, xtarget);
1452 if (wider)
1453 xspec_target_subreg = xtarget;
1455 else
1456 xtarget = gen_reg_rtx (maxmode);
1459 /* If this machine's extzv insists on a register target,
1460 make sure we have one. */
1461 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[0].predicate)
1462 (xtarget, maxmode)))
1463 xtarget = gen_reg_rtx (maxmode);
1465 bitsize_rtx = GEN_INT (bitsize);
1466 bitpos_rtx = GEN_INT (xbitpos);
1468 pat = gen_extzv (xtarget, xop0, bitsize_rtx, bitpos_rtx);
1469 if (pat)
1471 emit_insn (pat);
1472 target = xtarget;
1473 spec_target = xspec_target;
1474 spec_target_subreg = xspec_target_subreg;
1476 else
1478 delete_insns_since (last);
1479 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1480 bitpos, target, 1);
1483 else
1484 extzv_loses:
1485 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1486 bitpos, target, 1);
1488 else
1490 if (HAVE_extv
1491 && (GET_MODE_BITSIZE (extv_mode) >= bitsize)
1492 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
1493 && (bitsize + bitpos > GET_MODE_BITSIZE (extv_mode))))
1495 int xbitpos = bitpos, xoffset = offset;
1496 rtx bitsize_rtx, bitpos_rtx;
1497 rtx last = get_last_insn ();
1498 rtx xop0 = op0, xtarget = target;
1499 rtx xspec_target = spec_target;
1500 rtx xspec_target_subreg = spec_target_subreg;
1501 rtx pat;
1502 enum machine_mode maxmode = mode_for_extraction (EP_extv, 0);
1504 if (MEM_P (xop0))
1506 /* Is the memory operand acceptable? */
1507 if (! ((*insn_data[(int) CODE_FOR_extv].operand[1].predicate)
1508 (xop0, GET_MODE (xop0))))
1510 /* No, load into a reg and extract from there. */
1511 enum machine_mode bestmode;
1513 /* Get the mode to use for inserting into this field. If
1514 OP0 is BLKmode, get the smallest mode consistent with the
1515 alignment. If OP0 is a non-BLKmode object that is no
1516 wider than MAXMODE, use its mode. Otherwise, use the
1517 smallest mode containing the field. */
1519 if (GET_MODE (xop0) == BLKmode
1520 || (GET_MODE_SIZE (GET_MODE (op0))
1521 > GET_MODE_SIZE (maxmode)))
1522 bestmode = get_best_mode (bitsize, bitnum,
1523 MEM_ALIGN (xop0), maxmode,
1524 MEM_VOLATILE_P (xop0));
1525 else
1526 bestmode = GET_MODE (xop0);
1528 if (bestmode == VOIDmode
1529 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (xop0))
1530 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (xop0)))
1531 goto extv_loses;
1533 /* Compute offset as multiple of this unit,
1534 counting in bytes. */
1535 unit = GET_MODE_BITSIZE (bestmode);
1536 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1537 xbitpos = bitnum % unit;
1538 xop0 = adjust_address (xop0, bestmode, xoffset);
1540 /* Fetch it to a register in that size. */
1541 xop0 = force_reg (bestmode, xop0);
1543 /* XBITPOS counts within UNIT, which is what is expected. */
1545 else
1546 /* Get ref to first byte containing part of the field. */
1547 xop0 = adjust_address (xop0, byte_mode, xoffset);
1550 /* If op0 is a register, we need it in MAXMODE (which is usually
1551 SImode) to make it acceptable to the format of extv. */
1552 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1553 goto extv_loses;
1554 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
1555 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1557 /* On big-endian machines, we count bits from the most significant.
1558 If the bit field insn does not, we must invert. */
1559 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1560 xbitpos = unit - bitsize - xbitpos;
1562 /* XBITPOS counts within a size of UNIT.
1563 Adjust to count within a size of MAXMODE. */
1564 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
1565 xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
1567 unit = GET_MODE_BITSIZE (maxmode);
1569 if (xtarget == 0)
1570 xtarget = xspec_target = gen_reg_rtx (tmode);
1572 if (GET_MODE (xtarget) != maxmode)
1574 if (REG_P (xtarget))
1576 int wider = (GET_MODE_SIZE (maxmode)
1577 > GET_MODE_SIZE (GET_MODE (xtarget)));
1578 xtarget = gen_lowpart (maxmode, xtarget);
1579 if (wider)
1580 xspec_target_subreg = xtarget;
1582 else
1583 xtarget = gen_reg_rtx (maxmode);
1586 /* If this machine's extv insists on a register target,
1587 make sure we have one. */
1588 if (! ((*insn_data[(int) CODE_FOR_extv].operand[0].predicate)
1589 (xtarget, maxmode)))
1590 xtarget = gen_reg_rtx (maxmode);
1592 bitsize_rtx = GEN_INT (bitsize);
1593 bitpos_rtx = GEN_INT (xbitpos);
1595 pat = gen_extv (xtarget, xop0, bitsize_rtx, bitpos_rtx);
1596 if (pat)
1598 emit_insn (pat);
1599 target = xtarget;
1600 spec_target = xspec_target;
1601 spec_target_subreg = xspec_target_subreg;
1603 else
1605 delete_insns_since (last);
1606 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1607 bitpos, target, 0);
1610 else
1611 extv_loses:
1612 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1613 bitpos, target, 0);
1615 if (target == spec_target)
1616 return target;
1617 if (target == spec_target_subreg)
1618 return spec_target;
1619 if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
1621 /* If the target mode is not a scalar integral, first convert to the
1622 integer mode of that size and then access it as a floating-point
1623 value via a SUBREG. */
1624 if (!SCALAR_INT_MODE_P (tmode))
1626 enum machine_mode smode
1627 = mode_for_size (GET_MODE_BITSIZE (tmode), MODE_INT, 0);
1628 target = convert_to_mode (smode, target, unsignedp);
1629 target = force_reg (smode, target);
1630 return gen_lowpart (tmode, target);
1633 return convert_to_mode (tmode, target, unsignedp);
1635 return target;
1638 /* Extract a bit field using shifts and boolean operations
1639 Returns an rtx to represent the value.
1640 OP0 addresses a register (word) or memory (byte).
1641 BITPOS says which bit within the word or byte the bit field starts in.
1642 OFFSET says how many bytes farther the bit field starts;
1643 it is 0 if OP0 is a register.
1644 BITSIZE says how many bits long the bit field is.
1645 (If OP0 is a register, it may be narrower than a full word,
1646 but BITPOS still counts within a full word,
1647 which is significant on bigendian machines.)
1649 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1650 If TARGET is nonzero, attempts to store the value there
1651 and return TARGET, but this is not guaranteed.
1652 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
1654 static rtx
1655 extract_fixed_bit_field (enum machine_mode tmode, rtx op0,
1656 unsigned HOST_WIDE_INT offset,
1657 unsigned HOST_WIDE_INT bitsize,
1658 unsigned HOST_WIDE_INT bitpos, rtx target,
1659 int unsignedp)
1661 unsigned int total_bits = BITS_PER_WORD;
1662 enum machine_mode mode;
1664 if (GET_CODE (op0) == SUBREG || REG_P (op0))
1666 /* Special treatment for a bit field split across two registers. */
1667 if (bitsize + bitpos > BITS_PER_WORD)
1668 return extract_split_bit_field (op0, bitsize, bitpos, unsignedp);
1670 else
1672 /* Get the proper mode to use for this field. We want a mode that
1673 includes the entire field. If such a mode would be larger than
1674 a word, we won't be doing the extraction the normal way. */
1676 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1677 MEM_ALIGN (op0), word_mode, MEM_VOLATILE_P (op0));
1679 if (mode == VOIDmode)
1680 /* The only way this should occur is if the field spans word
1681 boundaries. */
1682 return extract_split_bit_field (op0, bitsize,
1683 bitpos + offset * BITS_PER_UNIT,
1684 unsignedp);
1686 total_bits = GET_MODE_BITSIZE (mode);
1688 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
1689 be in the range 0 to total_bits-1, and put any excess bytes in
1690 OFFSET. */
1691 if (bitpos >= total_bits)
1693 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
1694 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
1695 * BITS_PER_UNIT);
1698 /* Get ref to an aligned byte, halfword, or word containing the field.
1699 Adjust BITPOS to be position within a word,
1700 and OFFSET to be the offset of that word.
1701 Then alter OP0 to refer to that word. */
1702 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1703 offset -= (offset % (total_bits / BITS_PER_UNIT));
1704 op0 = adjust_address (op0, mode, offset);
1707 mode = GET_MODE (op0);
1709 if (BYTES_BIG_ENDIAN)
1710 /* BITPOS is the distance between our msb and that of OP0.
1711 Convert it to the distance from the lsb. */
1712 bitpos = total_bits - bitsize - bitpos;
1714 /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1715 We have reduced the big-endian case to the little-endian case. */
1717 if (unsignedp)
1719 if (bitpos)
1721 /* If the field does not already start at the lsb,
1722 shift it so it does. */
1723 tree amount = build_int_cst (NULL_TREE, bitpos);
1724 /* Maybe propagate the target for the shift. */
1725 /* But not if we will return it--could confuse integrate.c. */
1726 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1727 if (tmode != mode) subtarget = 0;
1728 op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1730 /* Convert the value to the desired mode. */
1731 if (mode != tmode)
1732 op0 = convert_to_mode (tmode, op0, 1);
1734 /* Unless the msb of the field used to be the msb when we shifted,
1735 mask out the upper bits. */
1737 if (GET_MODE_BITSIZE (mode) != bitpos + bitsize)
1738 return expand_binop (GET_MODE (op0), and_optab, op0,
1739 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1740 target, 1, OPTAB_LIB_WIDEN);
1741 return op0;
1744 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1745 then arithmetic-shift its lsb to the lsb of the word. */
1746 op0 = force_reg (mode, op0);
1747 if (mode != tmode)
1748 target = 0;
1750 /* Find the narrowest integer mode that contains the field. */
1752 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1753 mode = GET_MODE_WIDER_MODE (mode))
1754 if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1756 op0 = convert_to_mode (mode, op0, 0);
1757 break;
1760 if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1762 tree amount
1763 = build_int_cst (NULL_TREE,
1764 GET_MODE_BITSIZE (mode) - (bitsize + bitpos));
1765 /* Maybe propagate the target for the shift. */
1766 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1767 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1770 return expand_shift (RSHIFT_EXPR, mode, op0,
1771 build_int_cst (NULL_TREE,
1772 GET_MODE_BITSIZE (mode) - bitsize),
1773 target, 0);
1776 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1777 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1778 complement of that if COMPLEMENT. The mask is truncated if
1779 necessary to the width of mode MODE. The mask is zero-extended if
1780 BITSIZE+BITPOS is too small for MODE. */
1782 static rtx
1783 mask_rtx (enum machine_mode mode, int bitpos, int bitsize, int complement)
1785 HOST_WIDE_INT masklow, maskhigh;
1787 if (bitsize == 0)
1788 masklow = 0;
1789 else if (bitpos < HOST_BITS_PER_WIDE_INT)
1790 masklow = (HOST_WIDE_INT) -1 << bitpos;
1791 else
1792 masklow = 0;
1794 if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1795 masklow &= ((unsigned HOST_WIDE_INT) -1
1796 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1798 if (bitpos <= HOST_BITS_PER_WIDE_INT)
1799 maskhigh = -1;
1800 else
1801 maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1803 if (bitsize == 0)
1804 maskhigh = 0;
1805 else if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
1806 maskhigh &= ((unsigned HOST_WIDE_INT) -1
1807 >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1808 else
1809 maskhigh = 0;
1811 if (complement)
1813 maskhigh = ~maskhigh;
1814 masklow = ~masklow;
1817 return immed_double_const (masklow, maskhigh, mode);
1820 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1821 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1823 static rtx
1824 lshift_value (enum machine_mode mode, rtx value, int bitpos, int bitsize)
1826 unsigned HOST_WIDE_INT v = INTVAL (value);
1827 HOST_WIDE_INT low, high;
1829 if (bitsize < HOST_BITS_PER_WIDE_INT)
1830 v &= ~((HOST_WIDE_INT) -1 << bitsize);
1832 if (bitpos < HOST_BITS_PER_WIDE_INT)
1834 low = v << bitpos;
1835 high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
1837 else
1839 low = 0;
1840 high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
1843 return immed_double_const (low, high, mode);
1846 /* Extract a bit field from a memory by forcing the alignment of the
1847 memory. This efficient only if the field spans at least 4 boundaries.
1849 OP0 is the MEM.
1850 BITSIZE is the field width; BITPOS is the position of the first bit.
1851 UNSIGNEDP is true if the result should be zero-extended. */
1853 static rtx
1854 extract_force_align_mem_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
1855 unsigned HOST_WIDE_INT bitpos,
1856 int unsignedp)
1858 enum machine_mode mode, dmode;
1859 unsigned int m_bitsize, m_size;
1860 unsigned int sign_shift_up, sign_shift_dn;
1861 rtx base, a1, a2, v1, v2, comb, shift, result, start;
1863 /* Choose a mode that will fit BITSIZE. */
1864 mode = smallest_mode_for_size (bitsize, MODE_INT);
1865 m_size = GET_MODE_SIZE (mode);
1866 m_bitsize = GET_MODE_BITSIZE (mode);
1868 /* Choose a mode twice as wide. Fail if no such mode exists. */
1869 dmode = mode_for_size (m_bitsize * 2, MODE_INT, false);
1870 if (dmode == BLKmode)
1871 return NULL;
1873 do_pending_stack_adjust ();
1874 start = get_last_insn ();
1876 /* At the end, we'll need an additional shift to deal with sign/zero
1877 extension. By default this will be a left+right shift of the
1878 appropriate size. But we may be able to eliminate one of them. */
1879 sign_shift_up = sign_shift_dn = m_bitsize - bitsize;
1881 if (STRICT_ALIGNMENT)
1883 base = plus_constant (XEXP (op0, 0), bitpos / BITS_PER_UNIT);
1884 bitpos %= BITS_PER_UNIT;
1886 /* We load two values to be concatenate. There's an edge condition
1887 that bears notice -- an aligned value at the end of a page can
1888 only load one value lest we segfault. So the two values we load
1889 are at "base & -size" and "(base + size - 1) & -size". If base
1890 is unaligned, the addresses will be aligned and sequential; if
1891 base is aligned, the addresses will both be equal to base. */
1893 a1 = expand_simple_binop (Pmode, AND, force_operand (base, NULL),
1894 GEN_INT (-(HOST_WIDE_INT)m_size),
1895 NULL, true, OPTAB_LIB_WIDEN);
1896 mark_reg_pointer (a1, m_bitsize);
1897 v1 = gen_rtx_MEM (mode, a1);
1898 set_mem_align (v1, m_bitsize);
1899 v1 = force_reg (mode, validize_mem (v1));
1901 a2 = plus_constant (base, GET_MODE_SIZE (mode) - 1);
1902 a2 = expand_simple_binop (Pmode, AND, force_operand (a2, NULL),
1903 GEN_INT (-(HOST_WIDE_INT)m_size),
1904 NULL, true, OPTAB_LIB_WIDEN);
1905 v2 = gen_rtx_MEM (mode, a2);
1906 set_mem_align (v2, m_bitsize);
1907 v2 = force_reg (mode, validize_mem (v2));
1909 /* Combine these two values into a double-word value. */
1910 if (m_bitsize == BITS_PER_WORD)
1912 comb = gen_reg_rtx (dmode);
1913 emit_insn (gen_rtx_CLOBBER (VOIDmode, comb));
1914 emit_move_insn (gen_rtx_SUBREG (mode, comb, 0), v1);
1915 emit_move_insn (gen_rtx_SUBREG (mode, comb, m_size), v2);
1917 else
1919 if (BYTES_BIG_ENDIAN)
1920 comb = v1, v1 = v2, v2 = comb;
1921 v1 = convert_modes (dmode, mode, v1, true);
1922 if (v1 == NULL)
1923 goto fail;
1924 v2 = convert_modes (dmode, mode, v2, true);
1925 v2 = expand_simple_binop (dmode, ASHIFT, v2, GEN_INT (m_bitsize),
1926 NULL, true, OPTAB_LIB_WIDEN);
1927 if (v2 == NULL)
1928 goto fail;
1929 comb = expand_simple_binop (dmode, IOR, v1, v2, NULL,
1930 true, OPTAB_LIB_WIDEN);
1931 if (comb == NULL)
1932 goto fail;
1935 shift = expand_simple_binop (Pmode, AND, base, GEN_INT (m_size - 1),
1936 NULL, true, OPTAB_LIB_WIDEN);
1937 shift = expand_mult (Pmode, shift, GEN_INT (BITS_PER_UNIT), NULL, 1);
1939 if (bitpos != 0)
1941 if (sign_shift_up <= bitpos)
1942 bitpos -= sign_shift_up, sign_shift_up = 0;
1943 shift = expand_simple_binop (Pmode, PLUS, shift, GEN_INT (bitpos),
1944 NULL, true, OPTAB_LIB_WIDEN);
1947 else
1949 unsigned HOST_WIDE_INT offset = bitpos / BITS_PER_UNIT;
1950 bitpos %= BITS_PER_UNIT;
1952 /* When strict alignment is not required, we can just load directly
1953 from memory without masking. If the remaining BITPOS offset is
1954 small enough, we may be able to do all operations in MODE as
1955 opposed to DMODE. */
1956 if (bitpos + bitsize <= m_bitsize)
1957 dmode = mode;
1958 comb = adjust_address (op0, dmode, offset);
1960 if (sign_shift_up <= bitpos)
1961 bitpos -= sign_shift_up, sign_shift_up = 0;
1962 shift = GEN_INT (bitpos);
1965 /* Shift down the double-word such that the requested value is at bit 0. */
1966 if (shift != const0_rtx)
1967 comb = expand_simple_binop (dmode, unsignedp ? LSHIFTRT : ASHIFTRT,
1968 comb, shift, NULL, unsignedp, OPTAB_LIB_WIDEN);
1969 if (comb == NULL)
1970 goto fail;
1972 /* If the field exactly matches MODE, then all we need to do is return the
1973 lowpart. Otherwise, shift to get the sign bits set properly. */
1974 result = force_reg (mode, gen_lowpart (mode, comb));
1976 if (sign_shift_up)
1977 result = expand_simple_binop (mode, ASHIFT, result,
1978 GEN_INT (sign_shift_up),
1979 NULL_RTX, 0, OPTAB_LIB_WIDEN);
1980 if (sign_shift_dn)
1981 result = expand_simple_binop (mode, unsignedp ? LSHIFTRT : ASHIFTRT,
1982 result, GEN_INT (sign_shift_dn),
1983 NULL_RTX, 0, OPTAB_LIB_WIDEN);
1985 return result;
1987 fail:
1988 delete_insns_since (start);
1989 return NULL;
1992 /* Extract a bit field that is split across two words
1993 and return an RTX for the result.
1995 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1996 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1997 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
1999 static rtx
2000 extract_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
2001 unsigned HOST_WIDE_INT bitpos, int unsignedp)
2003 unsigned int unit;
2004 unsigned int bitsdone = 0;
2005 rtx result = NULL_RTX;
2006 int first = 1;
2008 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
2009 much at a time. */
2010 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
2011 unit = BITS_PER_WORD;
2012 else
2014 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
2015 if (0 && bitsize / unit > 2)
2017 rtx tmp = extract_force_align_mem_bit_field (op0, bitsize, bitpos,
2018 unsignedp);
2019 if (tmp)
2020 return tmp;
2024 while (bitsdone < bitsize)
2026 unsigned HOST_WIDE_INT thissize;
2027 rtx part, word;
2028 unsigned HOST_WIDE_INT thispos;
2029 unsigned HOST_WIDE_INT offset;
2031 offset = (bitpos + bitsdone) / unit;
2032 thispos = (bitpos + bitsdone) % unit;
2034 /* THISSIZE must not overrun a word boundary. Otherwise,
2035 extract_fixed_bit_field will call us again, and we will mutually
2036 recurse forever. */
2037 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
2038 thissize = MIN (thissize, unit - thispos);
2040 /* If OP0 is a register, then handle OFFSET here.
2042 When handling multiword bitfields, extract_bit_field may pass
2043 down a word_mode SUBREG of a larger REG for a bitfield that actually
2044 crosses a word boundary. Thus, for a SUBREG, we must find
2045 the current word starting from the base register. */
2046 if (GET_CODE (op0) == SUBREG)
2048 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
2049 word = operand_subword_force (SUBREG_REG (op0), word_offset,
2050 GET_MODE (SUBREG_REG (op0)));
2051 offset = 0;
2053 else if (REG_P (op0))
2055 word = operand_subword_force (op0, offset, GET_MODE (op0));
2056 offset = 0;
2058 else
2059 word = op0;
2061 /* Extract the parts in bit-counting order,
2062 whose meaning is determined by BYTES_PER_UNIT.
2063 OFFSET is in UNITs, and UNIT is in bits.
2064 extract_fixed_bit_field wants offset in bytes. */
2065 part = extract_fixed_bit_field (word_mode, word,
2066 offset * unit / BITS_PER_UNIT,
2067 thissize, thispos, 0, 1);
2068 bitsdone += thissize;
2070 /* Shift this part into place for the result. */
2071 if (BYTES_BIG_ENDIAN)
2073 if (bitsize != bitsdone)
2074 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2075 build_int_cst (NULL_TREE, bitsize - bitsdone),
2076 0, 1);
2078 else
2080 if (bitsdone != thissize)
2081 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2082 build_int_cst (NULL_TREE,
2083 bitsdone - thissize), 0, 1);
2086 if (first)
2087 result = part;
2088 else
2089 /* Combine the parts with bitwise or. This works
2090 because we extracted each part as an unsigned bit field. */
2091 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
2092 OPTAB_LIB_WIDEN);
2094 first = 0;
2097 /* Unsigned bit field: we are done. */
2098 if (unsignedp)
2099 return result;
2100 /* Signed bit field: sign-extend with two arithmetic shifts. */
2101 result = expand_shift (LSHIFT_EXPR, word_mode, result,
2102 build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
2103 NULL_RTX, 0);
2104 return expand_shift (RSHIFT_EXPR, word_mode, result,
2105 build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
2106 NULL_RTX, 0);
2109 /* Add INC into TARGET. */
2111 void
2112 expand_inc (rtx target, rtx inc)
2114 rtx value = expand_binop (GET_MODE (target), add_optab,
2115 target, inc,
2116 target, 0, OPTAB_LIB_WIDEN);
2117 if (value != target)
2118 emit_move_insn (target, value);
2121 /* Subtract DEC from TARGET. */
2123 void
2124 expand_dec (rtx target, rtx dec)
2126 rtx value = expand_binop (GET_MODE (target), sub_optab,
2127 target, dec,
2128 target, 0, OPTAB_LIB_WIDEN);
2129 if (value != target)
2130 emit_move_insn (target, value);
2133 /* Output a shift instruction for expression code CODE,
2134 with SHIFTED being the rtx for the value to shift,
2135 and AMOUNT the tree for the amount to shift by.
2136 Store the result in the rtx TARGET, if that is convenient.
2137 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2138 Return the rtx for where the value is. */
2141 expand_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2142 tree amount, rtx target, int unsignedp)
2144 rtx op1, temp = 0;
2145 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
2146 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2147 int try;
2149 /* Previously detected shift-counts computed by NEGATE_EXPR
2150 and shifted in the other direction; but that does not work
2151 on all machines. */
2153 op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
2155 if (SHIFT_COUNT_TRUNCATED)
2157 if (GET_CODE (op1) == CONST_INT
2158 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2159 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode)))
2160 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2161 % GET_MODE_BITSIZE (mode));
2162 else if (GET_CODE (op1) == SUBREG
2163 && subreg_lowpart_p (op1))
2164 op1 = SUBREG_REG (op1);
2167 if (op1 == const0_rtx)
2168 return shifted;
2170 /* Check whether its cheaper to implement a left shift by a constant
2171 bit count by a sequence of additions. */
2172 if (code == LSHIFT_EXPR
2173 && GET_CODE (op1) == CONST_INT
2174 && INTVAL (op1) > 0
2175 && INTVAL (op1) < GET_MODE_BITSIZE (mode)
2176 && shift_cost[mode][INTVAL (op1)] > INTVAL (op1) * add_cost[mode])
2178 int i;
2179 for (i = 0; i < INTVAL (op1); i++)
2181 temp = force_reg (mode, shifted);
2182 shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2183 unsignedp, OPTAB_LIB_WIDEN);
2185 return shifted;
2188 for (try = 0; temp == 0 && try < 3; try++)
2190 enum optab_methods methods;
2192 if (try == 0)
2193 methods = OPTAB_DIRECT;
2194 else if (try == 1)
2195 methods = OPTAB_WIDEN;
2196 else
2197 methods = OPTAB_LIB_WIDEN;
2199 if (rotate)
2201 /* Widening does not work for rotation. */
2202 if (methods == OPTAB_WIDEN)
2203 continue;
2204 else if (methods == OPTAB_LIB_WIDEN)
2206 /* If we have been unable to open-code this by a rotation,
2207 do it as the IOR of two shifts. I.e., to rotate A
2208 by N bits, compute (A << N) | ((unsigned) A >> (C - N))
2209 where C is the bitsize of A.
2211 It is theoretically possible that the target machine might
2212 not be able to perform either shift and hence we would
2213 be making two libcalls rather than just the one for the
2214 shift (similarly if IOR could not be done). We will allow
2215 this extremely unlikely lossage to avoid complicating the
2216 code below. */
2218 rtx subtarget = target == shifted ? 0 : target;
2219 rtx temp1;
2220 tree type = TREE_TYPE (amount);
2221 tree new_amount = make_tree (type, op1);
2222 tree other_amount
2223 = fold_build2 (MINUS_EXPR, type,
2224 build_int_cst (type, GET_MODE_BITSIZE (mode)),
2225 amount);
2227 shifted = force_reg (mode, shifted);
2229 temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2230 mode, shifted, new_amount, subtarget, 1);
2231 temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2232 mode, shifted, other_amount, 0, 1);
2233 return expand_binop (mode, ior_optab, temp, temp1, target,
2234 unsignedp, methods);
2237 temp = expand_binop (mode,
2238 left ? rotl_optab : rotr_optab,
2239 shifted, op1, target, unsignedp, methods);
2241 /* If we don't have the rotate, but we are rotating by a constant
2242 that is in range, try a rotate in the opposite direction. */
2244 if (temp == 0 && GET_CODE (op1) == CONST_INT
2245 && INTVAL (op1) > 0
2246 && (unsigned int) INTVAL (op1) < GET_MODE_BITSIZE (mode))
2247 temp = expand_binop (mode,
2248 left ? rotr_optab : rotl_optab,
2249 shifted,
2250 GEN_INT (GET_MODE_BITSIZE (mode)
2251 - INTVAL (op1)),
2252 target, unsignedp, methods);
2254 else if (unsignedp)
2255 temp = expand_binop (mode,
2256 left ? ashl_optab : lshr_optab,
2257 shifted, op1, target, unsignedp, methods);
2259 /* Do arithmetic shifts.
2260 Also, if we are going to widen the operand, we can just as well
2261 use an arithmetic right-shift instead of a logical one. */
2262 if (temp == 0 && ! rotate
2263 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2265 enum optab_methods methods1 = methods;
2267 /* If trying to widen a log shift to an arithmetic shift,
2268 don't accept an arithmetic shift of the same size. */
2269 if (unsignedp)
2270 methods1 = OPTAB_MUST_WIDEN;
2272 /* Arithmetic shift */
2274 temp = expand_binop (mode,
2275 left ? ashl_optab : ashr_optab,
2276 shifted, op1, target, unsignedp, methods1);
2279 /* We used to try extzv here for logical right shifts, but that was
2280 only useful for one machine, the VAX, and caused poor code
2281 generation there for lshrdi3, so the code was deleted and a
2282 define_expand for lshrsi3 was added to vax.md. */
2285 gcc_assert (temp);
2286 return temp;
2289 enum alg_code { alg_unknown, alg_zero, alg_m, alg_shift,
2290 alg_add_t_m2, alg_sub_t_m2,
2291 alg_add_factor, alg_sub_factor,
2292 alg_add_t2_m, alg_sub_t2_m };
2294 /* This structure holds the "cost" of a multiply sequence. The
2295 "cost" field holds the total rtx_cost of every operator in the
2296 synthetic multiplication sequence, hence cost(a op b) is defined
2297 as rtx_cost(op) + cost(a) + cost(b), where cost(leaf) is zero.
2298 The "latency" field holds the minimum possible latency of the
2299 synthetic multiply, on a hypothetical infinitely parallel CPU.
2300 This is the critical path, or the maximum height, of the expression
2301 tree which is the sum of rtx_costs on the most expensive path from
2302 any leaf to the root. Hence latency(a op b) is defined as zero for
2303 leaves and rtx_cost(op) + max(latency(a), latency(b)) otherwise. */
2305 struct mult_cost {
2306 short cost; /* Total rtx_cost of the multiplication sequence. */
2307 short latency; /* The latency of the multiplication sequence. */
2310 /* This macro is used to compare a pointer to a mult_cost against an
2311 single integer "rtx_cost" value. This is equivalent to the macro
2312 CHEAPER_MULT_COST(X,Z) where Z = {Y,Y}. */
2313 #define MULT_COST_LESS(X,Y) ((X)->cost < (Y) \
2314 || ((X)->cost == (Y) && (X)->latency < (Y)))
2316 /* This macro is used to compare two pointers to mult_costs against
2317 each other. The macro returns true if X is cheaper than Y.
2318 Currently, the cheaper of two mult_costs is the one with the
2319 lower "cost". If "cost"s are tied, the lower latency is cheaper. */
2320 #define CHEAPER_MULT_COST(X,Y) ((X)->cost < (Y)->cost \
2321 || ((X)->cost == (Y)->cost \
2322 && (X)->latency < (Y)->latency))
2324 /* This structure records a sequence of operations.
2325 `ops' is the number of operations recorded.
2326 `cost' is their total cost.
2327 The operations are stored in `op' and the corresponding
2328 logarithms of the integer coefficients in `log'.
2330 These are the operations:
2331 alg_zero total := 0;
2332 alg_m total := multiplicand;
2333 alg_shift total := total * coeff
2334 alg_add_t_m2 total := total + multiplicand * coeff;
2335 alg_sub_t_m2 total := total - multiplicand * coeff;
2336 alg_add_factor total := total * coeff + total;
2337 alg_sub_factor total := total * coeff - total;
2338 alg_add_t2_m total := total * coeff + multiplicand;
2339 alg_sub_t2_m total := total * coeff - multiplicand;
2341 The first operand must be either alg_zero or alg_m. */
2343 struct algorithm
2345 struct mult_cost cost;
2346 short ops;
2347 /* The size of the OP and LOG fields are not directly related to the
2348 word size, but the worst-case algorithms will be if we have few
2349 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
2350 In that case we will generate shift-by-2, add, shift-by-2, add,...,
2351 in total wordsize operations. */
2352 enum alg_code op[MAX_BITS_PER_WORD];
2353 char log[MAX_BITS_PER_WORD];
2356 /* The entry for our multiplication cache/hash table. */
2357 struct alg_hash_entry {
2358 /* The number we are multiplying by. */
2359 unsigned int t;
2361 /* The mode in which we are multiplying something by T. */
2362 enum machine_mode mode;
2364 /* The best multiplication algorithm for t. */
2365 enum alg_code alg;
2368 /* The number of cache/hash entries. */
2369 #define NUM_ALG_HASH_ENTRIES 307
2371 /* Each entry of ALG_HASH caches alg_code for some integer. This is
2372 actually a hash table. If we have a collision, that the older
2373 entry is kicked out. */
2374 static struct alg_hash_entry alg_hash[NUM_ALG_HASH_ENTRIES];
2376 /* Indicates the type of fixup needed after a constant multiplication.
2377 BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2378 the result should be negated, and ADD_VARIANT means that the
2379 multiplicand should be added to the result. */
2380 enum mult_variant {basic_variant, negate_variant, add_variant};
2382 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2383 const struct mult_cost *, enum machine_mode mode);
2384 static bool choose_mult_variant (enum machine_mode, HOST_WIDE_INT,
2385 struct algorithm *, enum mult_variant *, int);
2386 static rtx expand_mult_const (enum machine_mode, rtx, HOST_WIDE_INT, rtx,
2387 const struct algorithm *, enum mult_variant);
2388 static unsigned HOST_WIDE_INT choose_multiplier (unsigned HOST_WIDE_INT, int,
2389 int, rtx *, int *, int *);
2390 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2391 static rtx extract_high_half (enum machine_mode, rtx);
2392 static rtx expand_mult_highpart (enum machine_mode, rtx, rtx, rtx, int, int);
2393 static rtx expand_mult_highpart_optab (enum machine_mode, rtx, rtx, rtx,
2394 int, int);
2395 /* Compute and return the best algorithm for multiplying by T.
2396 The algorithm must cost less than cost_limit
2397 If retval.cost >= COST_LIMIT, no algorithm was found and all
2398 other field of the returned struct are undefined.
2399 MODE is the machine mode of the multiplication. */
2401 static void
2402 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2403 const struct mult_cost *cost_limit, enum machine_mode mode)
2405 int m;
2406 struct algorithm *alg_in, *best_alg;
2407 struct mult_cost best_cost;
2408 struct mult_cost new_limit;
2409 int op_cost, op_latency;
2410 unsigned HOST_WIDE_INT q;
2411 int maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (mode));
2412 int hash_index;
2413 bool cache_hit = false;
2414 enum alg_code cache_alg = alg_zero;
2416 /* Indicate that no algorithm is yet found. If no algorithm
2417 is found, this value will be returned and indicate failure. */
2418 alg_out->cost.cost = cost_limit->cost + 1;
2419 alg_out->cost.latency = cost_limit->latency + 1;
2421 if (cost_limit->cost < 0
2422 || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2423 return;
2425 /* Restrict the bits of "t" to the multiplication's mode. */
2426 t &= GET_MODE_MASK (mode);
2428 /* t == 1 can be done in zero cost. */
2429 if (t == 1)
2431 alg_out->ops = 1;
2432 alg_out->cost.cost = 0;
2433 alg_out->cost.latency = 0;
2434 alg_out->op[0] = alg_m;
2435 return;
2438 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2439 fail now. */
2440 if (t == 0)
2442 if (MULT_COST_LESS (cost_limit, zero_cost))
2443 return;
2444 else
2446 alg_out->ops = 1;
2447 alg_out->cost.cost = zero_cost;
2448 alg_out->cost.latency = zero_cost;
2449 alg_out->op[0] = alg_zero;
2450 return;
2454 /* We'll be needing a couple extra algorithm structures now. */
2456 alg_in = alloca (sizeof (struct algorithm));
2457 best_alg = alloca (sizeof (struct algorithm));
2458 best_cost = *cost_limit;
2460 /* Compute the hash index. */
2461 hash_index = (t ^ (unsigned int) mode) % NUM_ALG_HASH_ENTRIES;
2463 /* See if we already know what to do for T. */
2464 if (alg_hash[hash_index].t == t
2465 && alg_hash[hash_index].mode == mode
2466 && alg_hash[hash_index].alg != alg_unknown)
2468 cache_hit = true;
2469 cache_alg = alg_hash[hash_index].alg;
2470 switch (cache_alg)
2472 case alg_shift:
2473 goto do_alg_shift;
2475 case alg_add_t_m2:
2476 case alg_sub_t_m2:
2477 goto do_alg_addsub_t_m2;
2479 case alg_add_factor:
2480 case alg_sub_factor:
2481 goto do_alg_addsub_factor;
2483 case alg_add_t2_m:
2484 goto do_alg_add_t2_m;
2486 case alg_sub_t2_m:
2487 goto do_alg_sub_t2_m;
2489 default:
2490 gcc_unreachable ();
2494 /* If we have a group of zero bits at the low-order part of T, try
2495 multiplying by the remaining bits and then doing a shift. */
2497 if ((t & 1) == 0)
2499 do_alg_shift:
2500 m = floor_log2 (t & -t); /* m = number of low zero bits */
2501 if (m < maxm)
2503 q = t >> m;
2504 /* The function expand_shift will choose between a shift and
2505 a sequence of additions, so the observed cost is given as
2506 MIN (m * add_cost[mode], shift_cost[mode][m]). */
2507 op_cost = m * add_cost[mode];
2508 if (shift_cost[mode][m] < op_cost)
2509 op_cost = shift_cost[mode][m];
2510 new_limit.cost = best_cost.cost - op_cost;
2511 new_limit.latency = best_cost.latency - op_cost;
2512 synth_mult (alg_in, q, &new_limit, mode);
2514 alg_in->cost.cost += op_cost;
2515 alg_in->cost.latency += op_cost;
2516 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2518 struct algorithm *x;
2519 best_cost = alg_in->cost;
2520 x = alg_in, alg_in = best_alg, best_alg = x;
2521 best_alg->log[best_alg->ops] = m;
2522 best_alg->op[best_alg->ops] = alg_shift;
2525 if (cache_hit)
2526 goto done;
2529 /* If we have an odd number, add or subtract one. */
2530 if ((t & 1) != 0)
2532 unsigned HOST_WIDE_INT w;
2534 do_alg_addsub_t_m2:
2535 for (w = 1; (w & t) != 0; w <<= 1)
2537 /* If T was -1, then W will be zero after the loop. This is another
2538 case where T ends with ...111. Handling this with (T + 1) and
2539 subtract 1 produces slightly better code and results in algorithm
2540 selection much faster than treating it like the ...0111 case
2541 below. */
2542 if (w == 0
2543 || (w > 2
2544 /* Reject the case where t is 3.
2545 Thus we prefer addition in that case. */
2546 && t != 3))
2548 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2550 op_cost = add_cost[mode];
2551 new_limit.cost = best_cost.cost - op_cost;
2552 new_limit.latency = best_cost.latency - op_cost;
2553 synth_mult (alg_in, t + 1, &new_limit, mode);
2555 alg_in->cost.cost += op_cost;
2556 alg_in->cost.latency += op_cost;
2557 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2559 struct algorithm *x;
2560 best_cost = alg_in->cost;
2561 x = alg_in, alg_in = best_alg, best_alg = x;
2562 best_alg->log[best_alg->ops] = 0;
2563 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2566 else
2568 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2570 op_cost = add_cost[mode];
2571 new_limit.cost = best_cost.cost - op_cost;
2572 new_limit.latency = best_cost.latency - op_cost;
2573 synth_mult (alg_in, t - 1, &new_limit, mode);
2575 alg_in->cost.cost += op_cost;
2576 alg_in->cost.latency += op_cost;
2577 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2579 struct algorithm *x;
2580 best_cost = alg_in->cost;
2581 x = alg_in, alg_in = best_alg, best_alg = x;
2582 best_alg->log[best_alg->ops] = 0;
2583 best_alg->op[best_alg->ops] = alg_add_t_m2;
2586 if (cache_hit)
2587 goto done;
2590 /* Look for factors of t of the form
2591 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2592 If we find such a factor, we can multiply by t using an algorithm that
2593 multiplies by q, shift the result by m and add/subtract it to itself.
2595 We search for large factors first and loop down, even if large factors
2596 are less probable than small; if we find a large factor we will find a
2597 good sequence quickly, and therefore be able to prune (by decreasing
2598 COST_LIMIT) the search. */
2600 do_alg_addsub_factor:
2601 for (m = floor_log2 (t - 1); m >= 2; m--)
2603 unsigned HOST_WIDE_INT d;
2605 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2606 if (t % d == 0 && t > d && m < maxm
2607 && (!cache_hit || cache_alg == alg_add_factor))
2609 /* If the target has a cheap shift-and-add instruction use
2610 that in preference to a shift insn followed by an add insn.
2611 Assume that the shift-and-add is "atomic" with a latency
2612 equal to its cost, otherwise assume that on superscalar
2613 hardware the shift may be executed concurrently with the
2614 earlier steps in the algorithm. */
2615 op_cost = add_cost[mode] + shift_cost[mode][m];
2616 if (shiftadd_cost[mode][m] < op_cost)
2618 op_cost = shiftadd_cost[mode][m];
2619 op_latency = op_cost;
2621 else
2622 op_latency = add_cost[mode];
2624 new_limit.cost = best_cost.cost - op_cost;
2625 new_limit.latency = best_cost.latency - op_latency;
2626 synth_mult (alg_in, t / d, &new_limit, mode);
2628 alg_in->cost.cost += op_cost;
2629 alg_in->cost.latency += op_latency;
2630 if (alg_in->cost.latency < op_cost)
2631 alg_in->cost.latency = op_cost;
2632 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2634 struct algorithm *x;
2635 best_cost = alg_in->cost;
2636 x = alg_in, alg_in = best_alg, best_alg = x;
2637 best_alg->log[best_alg->ops] = m;
2638 best_alg->op[best_alg->ops] = alg_add_factor;
2640 /* Other factors will have been taken care of in the recursion. */
2641 break;
2644 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2645 if (t % d == 0 && t > d && m < maxm
2646 && (!cache_hit || cache_alg == alg_sub_factor))
2648 /* If the target has a cheap shift-and-subtract insn use
2649 that in preference to a shift insn followed by a sub insn.
2650 Assume that the shift-and-sub is "atomic" with a latency
2651 equal to it's cost, otherwise assume that on superscalar
2652 hardware the shift may be executed concurrently with the
2653 earlier steps in the algorithm. */
2654 op_cost = add_cost[mode] + shift_cost[mode][m];
2655 if (shiftsub_cost[mode][m] < op_cost)
2657 op_cost = shiftsub_cost[mode][m];
2658 op_latency = op_cost;
2660 else
2661 op_latency = add_cost[mode];
2663 new_limit.cost = best_cost.cost - op_cost;
2664 new_limit.latency = best_cost.latency - op_latency;
2665 synth_mult (alg_in, t / d, &new_limit, mode);
2667 alg_in->cost.cost += op_cost;
2668 alg_in->cost.latency += op_latency;
2669 if (alg_in->cost.latency < op_cost)
2670 alg_in->cost.latency = op_cost;
2671 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2673 struct algorithm *x;
2674 best_cost = alg_in->cost;
2675 x = alg_in, alg_in = best_alg, best_alg = x;
2676 best_alg->log[best_alg->ops] = m;
2677 best_alg->op[best_alg->ops] = alg_sub_factor;
2679 break;
2682 if (cache_hit)
2683 goto done;
2685 /* Try shift-and-add (load effective address) instructions,
2686 i.e. do a*3, a*5, a*9. */
2687 if ((t & 1) != 0)
2689 do_alg_add_t2_m:
2690 q = t - 1;
2691 q = q & -q;
2692 m = exact_log2 (q);
2693 if (m >= 0 && m < maxm)
2695 op_cost = shiftadd_cost[mode][m];
2696 new_limit.cost = best_cost.cost - op_cost;
2697 new_limit.latency = best_cost.latency - op_cost;
2698 synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2700 alg_in->cost.cost += op_cost;
2701 alg_in->cost.latency += op_cost;
2702 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2704 struct algorithm *x;
2705 best_cost = alg_in->cost;
2706 x = alg_in, alg_in = best_alg, best_alg = x;
2707 best_alg->log[best_alg->ops] = m;
2708 best_alg->op[best_alg->ops] = alg_add_t2_m;
2711 if (cache_hit)
2712 goto done;
2714 do_alg_sub_t2_m:
2715 q = t + 1;
2716 q = q & -q;
2717 m = exact_log2 (q);
2718 if (m >= 0 && m < maxm)
2720 op_cost = shiftsub_cost[mode][m];
2721 new_limit.cost = best_cost.cost - op_cost;
2722 new_limit.latency = best_cost.latency - op_cost;
2723 synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2725 alg_in->cost.cost += op_cost;
2726 alg_in->cost.latency += op_cost;
2727 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2729 struct algorithm *x;
2730 best_cost = alg_in->cost;
2731 x = alg_in, alg_in = best_alg, best_alg = x;
2732 best_alg->log[best_alg->ops] = m;
2733 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2736 if (cache_hit)
2737 goto done;
2740 done:
2741 /* If best_cost has not decreased, we have not found any algorithm. */
2742 if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
2743 return;
2745 /* Cache the result. */
2746 if (!cache_hit)
2748 alg_hash[hash_index].t = t;
2749 alg_hash[hash_index].mode = mode;
2750 alg_hash[hash_index].alg = best_alg->op[best_alg->ops];
2753 /* If we are getting a too long sequence for `struct algorithm'
2754 to record, make this search fail. */
2755 if (best_alg->ops == MAX_BITS_PER_WORD)
2756 return;
2758 /* Copy the algorithm from temporary space to the space at alg_out.
2759 We avoid using structure assignment because the majority of
2760 best_alg is normally undefined, and this is a critical function. */
2761 alg_out->ops = best_alg->ops + 1;
2762 alg_out->cost = best_cost;
2763 memcpy (alg_out->op, best_alg->op,
2764 alg_out->ops * sizeof *alg_out->op);
2765 memcpy (alg_out->log, best_alg->log,
2766 alg_out->ops * sizeof *alg_out->log);
2769 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2770 Try three variations:
2772 - a shift/add sequence based on VAL itself
2773 - a shift/add sequence based on -VAL, followed by a negation
2774 - a shift/add sequence based on VAL - 1, followed by an addition.
2776 Return true if the cheapest of these cost less than MULT_COST,
2777 describing the algorithm in *ALG and final fixup in *VARIANT. */
2779 static bool
2780 choose_mult_variant (enum machine_mode mode, HOST_WIDE_INT val,
2781 struct algorithm *alg, enum mult_variant *variant,
2782 int mult_cost)
2784 struct algorithm alg2;
2785 struct mult_cost limit;
2786 int op_cost;
2788 *variant = basic_variant;
2789 limit.cost = mult_cost;
2790 limit.latency = mult_cost;
2791 synth_mult (alg, val, &limit, mode);
2793 /* This works only if the inverted value actually fits in an
2794 `unsigned int' */
2795 if (HOST_BITS_PER_INT >= GET_MODE_BITSIZE (mode))
2797 op_cost = neg_cost[mode];
2798 if (MULT_COST_LESS (&alg->cost, mult_cost))
2800 limit.cost = alg->cost.cost - op_cost;
2801 limit.latency = alg->cost.latency - op_cost;
2803 else
2805 limit.cost = mult_cost - op_cost;
2806 limit.latency = mult_cost - op_cost;
2809 synth_mult (&alg2, -val, &limit, mode);
2810 alg2.cost.cost += op_cost;
2811 alg2.cost.latency += op_cost;
2812 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2813 *alg = alg2, *variant = negate_variant;
2816 /* This proves very useful for division-by-constant. */
2817 op_cost = add_cost[mode];
2818 if (MULT_COST_LESS (&alg->cost, mult_cost))
2820 limit.cost = alg->cost.cost - op_cost;
2821 limit.latency = alg->cost.latency - op_cost;
2823 else
2825 limit.cost = mult_cost - op_cost;
2826 limit.latency = mult_cost - op_cost;
2829 synth_mult (&alg2, val - 1, &limit, mode);
2830 alg2.cost.cost += op_cost;
2831 alg2.cost.latency += op_cost;
2832 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2833 *alg = alg2, *variant = add_variant;
2835 return MULT_COST_LESS (&alg->cost, mult_cost);
2838 /* A subroutine of expand_mult, used for constant multiplications.
2839 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2840 convenient. Use the shift/add sequence described by ALG and apply
2841 the final fixup specified by VARIANT. */
2843 static rtx
2844 expand_mult_const (enum machine_mode mode, rtx op0, HOST_WIDE_INT val,
2845 rtx target, const struct algorithm *alg,
2846 enum mult_variant variant)
2848 HOST_WIDE_INT val_so_far;
2849 rtx insn, accum, tem;
2850 int opno;
2851 enum machine_mode nmode;
2853 /* Avoid referencing memory over and over.
2854 For speed, but also for correctness when mem is volatile. */
2855 if (MEM_P (op0))
2856 op0 = force_reg (mode, op0);
2858 /* ACCUM starts out either as OP0 or as a zero, depending on
2859 the first operation. */
2861 if (alg->op[0] == alg_zero)
2863 accum = copy_to_mode_reg (mode, const0_rtx);
2864 val_so_far = 0;
2866 else if (alg->op[0] == alg_m)
2868 accum = copy_to_mode_reg (mode, op0);
2869 val_so_far = 1;
2871 else
2872 gcc_unreachable ();
2874 for (opno = 1; opno < alg->ops; opno++)
2876 int log = alg->log[opno];
2877 rtx shift_subtarget = optimize ? 0 : accum;
2878 rtx add_target
2879 = (opno == alg->ops - 1 && target != 0 && variant != add_variant
2880 && !optimize)
2881 ? target : 0;
2882 rtx accum_target = optimize ? 0 : accum;
2884 switch (alg->op[opno])
2886 case alg_shift:
2887 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2888 build_int_cst (NULL_TREE, log),
2889 NULL_RTX, 0);
2890 val_so_far <<= log;
2891 break;
2893 case alg_add_t_m2:
2894 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2895 build_int_cst (NULL_TREE, log),
2896 NULL_RTX, 0);
2897 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2898 add_target ? add_target : accum_target);
2899 val_so_far += (HOST_WIDE_INT) 1 << log;
2900 break;
2902 case alg_sub_t_m2:
2903 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2904 build_int_cst (NULL_TREE, log),
2905 NULL_RTX, 0);
2906 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
2907 add_target ? add_target : accum_target);
2908 val_so_far -= (HOST_WIDE_INT) 1 << log;
2909 break;
2911 case alg_add_t2_m:
2912 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2913 build_int_cst (NULL_TREE, log),
2914 shift_subtarget,
2916 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
2917 add_target ? add_target : accum_target);
2918 val_so_far = (val_so_far << log) + 1;
2919 break;
2921 case alg_sub_t2_m:
2922 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2923 build_int_cst (NULL_TREE, log),
2924 shift_subtarget, 0);
2925 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
2926 add_target ? add_target : accum_target);
2927 val_so_far = (val_so_far << log) - 1;
2928 break;
2930 case alg_add_factor:
2931 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2932 build_int_cst (NULL_TREE, log),
2933 NULL_RTX, 0);
2934 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2935 add_target ? add_target : accum_target);
2936 val_so_far += val_so_far << log;
2937 break;
2939 case alg_sub_factor:
2940 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2941 build_int_cst (NULL_TREE, log),
2942 NULL_RTX, 0);
2943 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
2944 (add_target
2945 ? add_target : (optimize ? 0 : tem)));
2946 val_so_far = (val_so_far << log) - val_so_far;
2947 break;
2949 default:
2950 gcc_unreachable ();
2953 /* Write a REG_EQUAL note on the last insn so that we can cse
2954 multiplication sequences. Note that if ACCUM is a SUBREG,
2955 we've set the inner register and must properly indicate
2956 that. */
2958 tem = op0, nmode = mode;
2959 if (GET_CODE (accum) == SUBREG)
2961 nmode = GET_MODE (SUBREG_REG (accum));
2962 tem = gen_lowpart (nmode, op0);
2965 insn = get_last_insn ();
2966 set_unique_reg_note (insn, REG_EQUAL,
2967 gen_rtx_MULT (nmode, tem, GEN_INT (val_so_far)));
2970 if (variant == negate_variant)
2972 val_so_far = -val_so_far;
2973 accum = expand_unop (mode, neg_optab, accum, target, 0);
2975 else if (variant == add_variant)
2977 val_so_far = val_so_far + 1;
2978 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
2981 /* Compare only the bits of val and val_so_far that are significant
2982 in the result mode, to avoid sign-/zero-extension confusion. */
2983 val &= GET_MODE_MASK (mode);
2984 val_so_far &= GET_MODE_MASK (mode);
2985 gcc_assert (val == val_so_far);
2987 return accum;
2990 /* Perform a multiplication and return an rtx for the result.
2991 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
2992 TARGET is a suggestion for where to store the result (an rtx).
2994 We check specially for a constant integer as OP1.
2995 If you want this check for OP0 as well, then before calling
2996 you should swap the two operands if OP0 would be constant. */
2999 expand_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3000 int unsignedp)
3002 enum mult_variant variant;
3003 struct algorithm algorithm;
3004 int max_cost;
3006 /* Handling const0_rtx here allows us to use zero as a rogue value for
3007 coeff below. */
3008 if (op1 == const0_rtx)
3009 return const0_rtx;
3010 if (op1 == const1_rtx)
3011 return op0;
3012 if (op1 == constm1_rtx)
3013 return expand_unop (mode,
3014 GET_MODE_CLASS (mode) == MODE_INT
3015 && !unsignedp && flag_trapv
3016 ? negv_optab : neg_optab,
3017 op0, target, 0);
3019 /* These are the operations that are potentially turned into a sequence
3020 of shifts and additions. */
3021 if (SCALAR_INT_MODE_P (mode)
3022 && (unsignedp || !flag_trapv))
3024 HOST_WIDE_INT coeff = 0;
3025 rtx fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3027 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3028 less than or equal in size to `unsigned int' this doesn't matter.
3029 If the mode is larger than `unsigned int', then synth_mult works
3030 only if the constant value exactly fits in an `unsigned int' without
3031 any truncation. This means that multiplying by negative values does
3032 not work; results are off by 2^32 on a 32 bit machine. */
3034 if (GET_CODE (op1) == CONST_INT)
3036 /* Attempt to handle multiplication of DImode values by negative
3037 coefficients, by performing the multiplication by a positive
3038 multiplier and then inverting the result. */
3039 if (INTVAL (op1) < 0
3040 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
3042 /* Its safe to use -INTVAL (op1) even for INT_MIN, as the
3043 result is interpreted as an unsigned coefficient.
3044 Exclude cost of op0 from max_cost to match the cost
3045 calculation of the synth_mult. */
3046 max_cost = rtx_cost (gen_rtx_MULT (mode, fake_reg, op1), SET)
3047 - neg_cost[mode];
3048 if (max_cost > 0
3049 && choose_mult_variant (mode, -INTVAL (op1), &algorithm,
3050 &variant, max_cost))
3052 rtx temp = expand_mult_const (mode, op0, -INTVAL (op1),
3053 NULL_RTX, &algorithm,
3054 variant);
3055 return expand_unop (mode, neg_optab, temp, target, 0);
3058 else coeff = INTVAL (op1);
3060 else if (GET_CODE (op1) == CONST_DOUBLE)
3062 /* If we are multiplying in DImode, it may still be a win
3063 to try to work with shifts and adds. */
3064 if (CONST_DOUBLE_HIGH (op1) == 0)
3065 coeff = CONST_DOUBLE_LOW (op1);
3066 else if (CONST_DOUBLE_LOW (op1) == 0
3067 && EXACT_POWER_OF_2_OR_ZERO_P (CONST_DOUBLE_HIGH (op1)))
3069 int shift = floor_log2 (CONST_DOUBLE_HIGH (op1))
3070 + HOST_BITS_PER_WIDE_INT;
3071 return expand_shift (LSHIFT_EXPR, mode, op0,
3072 build_int_cst (NULL_TREE, shift),
3073 target, unsignedp);
3077 /* We used to test optimize here, on the grounds that it's better to
3078 produce a smaller program when -O is not used. But this causes
3079 such a terrible slowdown sometimes that it seems better to always
3080 use synth_mult. */
3081 if (coeff != 0)
3083 /* Special case powers of two. */
3084 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3085 return expand_shift (LSHIFT_EXPR, mode, op0,
3086 build_int_cst (NULL_TREE, floor_log2 (coeff)),
3087 target, unsignedp);
3089 /* Exclude cost of op0 from max_cost to match the cost
3090 calculation of the synth_mult. */
3091 max_cost = rtx_cost (gen_rtx_MULT (mode, fake_reg, op1), SET);
3092 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3093 max_cost))
3094 return expand_mult_const (mode, op0, coeff, target,
3095 &algorithm, variant);
3099 if (GET_CODE (op0) == CONST_DOUBLE)
3101 rtx temp = op0;
3102 op0 = op1;
3103 op1 = temp;
3106 /* Expand x*2.0 as x+x. */
3107 if (GET_CODE (op1) == CONST_DOUBLE
3108 && GET_MODE_CLASS (mode) == MODE_FLOAT)
3110 REAL_VALUE_TYPE d;
3111 REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
3113 if (REAL_VALUES_EQUAL (d, dconst2))
3115 op0 = force_reg (GET_MODE (op0), op0);
3116 return expand_binop (mode, add_optab, op0, op0,
3117 target, unsignedp, OPTAB_LIB_WIDEN);
3121 /* This used to use umul_optab if unsigned, but for non-widening multiply
3122 there is no difference between signed and unsigned. */
3123 op0 = expand_binop (mode,
3124 ! unsignedp
3125 && flag_trapv && (GET_MODE_CLASS(mode) == MODE_INT)
3126 ? smulv_optab : smul_optab,
3127 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
3128 gcc_assert (op0);
3129 return op0;
3132 /* Return the smallest n such that 2**n >= X. */
3135 ceil_log2 (unsigned HOST_WIDE_INT x)
3137 return floor_log2 (x - 1) + 1;
3140 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3141 replace division by D, and put the least significant N bits of the result
3142 in *MULTIPLIER_PTR and return the most significant bit.
3144 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3145 needed precision is in PRECISION (should be <= N).
3147 PRECISION should be as small as possible so this function can choose
3148 multiplier more freely.
3150 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3151 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3153 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3154 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3156 static
3157 unsigned HOST_WIDE_INT
3158 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
3159 rtx *multiplier_ptr, int *post_shift_ptr, int *lgup_ptr)
3161 HOST_WIDE_INT mhigh_hi, mlow_hi;
3162 unsigned HOST_WIDE_INT mhigh_lo, mlow_lo;
3163 int lgup, post_shift;
3164 int pow, pow2;
3165 unsigned HOST_WIDE_INT nl, dummy1;
3166 HOST_WIDE_INT nh, dummy2;
3168 /* lgup = ceil(log2(divisor)); */
3169 lgup = ceil_log2 (d);
3171 gcc_assert (lgup <= n);
3173 pow = n + lgup;
3174 pow2 = n + lgup - precision;
3176 /* We could handle this with some effort, but this case is much
3177 better handled directly with a scc insn, so rely on caller using
3178 that. */
3179 gcc_assert (pow != 2 * HOST_BITS_PER_WIDE_INT);
3181 /* mlow = 2^(N + lgup)/d */
3182 if (pow >= HOST_BITS_PER_WIDE_INT)
3184 nh = (HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
3185 nl = 0;
3187 else
3189 nh = 0;
3190 nl = (unsigned HOST_WIDE_INT) 1 << pow;
3192 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
3193 &mlow_lo, &mlow_hi, &dummy1, &dummy2);
3195 /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
3196 if (pow2 >= HOST_BITS_PER_WIDE_INT)
3197 nh |= (HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
3198 else
3199 nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
3200 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
3201 &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
3203 gcc_assert (!mhigh_hi || nh - d < d);
3204 gcc_assert (mhigh_hi <= 1 && mlow_hi <= 1);
3205 /* Assert that mlow < mhigh. */
3206 gcc_assert (mlow_hi < mhigh_hi
3207 || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo));
3209 /* If precision == N, then mlow, mhigh exceed 2^N
3210 (but they do not exceed 2^(N+1)). */
3212 /* Reduce to lowest terms. */
3213 for (post_shift = lgup; post_shift > 0; post_shift--)
3215 unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
3216 unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
3217 if (ml_lo >= mh_lo)
3218 break;
3220 mlow_hi = 0;
3221 mlow_lo = ml_lo;
3222 mhigh_hi = 0;
3223 mhigh_lo = mh_lo;
3226 *post_shift_ptr = post_shift;
3227 *lgup_ptr = lgup;
3228 if (n < HOST_BITS_PER_WIDE_INT)
3230 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
3231 *multiplier_ptr = GEN_INT (mhigh_lo & mask);
3232 return mhigh_lo >= mask;
3234 else
3236 *multiplier_ptr = GEN_INT (mhigh_lo);
3237 return mhigh_hi;
3241 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3242 congruent to 1 (mod 2**N). */
3244 static unsigned HOST_WIDE_INT
3245 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
3247 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3249 /* The algorithm notes that the choice y = x satisfies
3250 x*y == 1 mod 2^3, since x is assumed odd.
3251 Each iteration doubles the number of bits of significance in y. */
3253 unsigned HOST_WIDE_INT mask;
3254 unsigned HOST_WIDE_INT y = x;
3255 int nbit = 3;
3257 mask = (n == HOST_BITS_PER_WIDE_INT
3258 ? ~(unsigned HOST_WIDE_INT) 0
3259 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
3261 while (nbit < n)
3263 y = y * (2 - x*y) & mask; /* Modulo 2^N */
3264 nbit *= 2;
3266 return y;
3269 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3270 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3271 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3272 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3273 become signed.
3275 The result is put in TARGET if that is convenient.
3277 MODE is the mode of operation. */
3280 expand_mult_highpart_adjust (enum machine_mode mode, rtx adj_operand, rtx op0,
3281 rtx op1, rtx target, int unsignedp)
3283 rtx tem;
3284 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3286 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3287 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3288 NULL_RTX, 0);
3289 tem = expand_and (mode, tem, op1, NULL_RTX);
3290 adj_operand
3291 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3292 adj_operand);
3294 tem = expand_shift (RSHIFT_EXPR, mode, op1,
3295 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3296 NULL_RTX, 0);
3297 tem = expand_and (mode, tem, op0, NULL_RTX);
3298 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3299 target);
3301 return target;
3304 /* Subroutine of expand_mult_highpart. Return the MODE high part of OP. */
3306 static rtx
3307 extract_high_half (enum machine_mode mode, rtx op)
3309 enum machine_mode wider_mode;
3311 if (mode == word_mode)
3312 return gen_highpart (mode, op);
3314 wider_mode = GET_MODE_WIDER_MODE (mode);
3315 op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3316 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode)), 0, 1);
3317 return convert_modes (mode, wider_mode, op, 0);
3320 /* Like expand_mult_highpart, but only consider using a multiplication
3321 optab. OP1 is an rtx for the constant operand. */
3323 static rtx
3324 expand_mult_highpart_optab (enum machine_mode mode, rtx op0, rtx op1,
3325 rtx target, int unsignedp, int max_cost)
3327 rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3328 enum machine_mode wider_mode;
3329 optab moptab;
3330 rtx tem;
3331 int size;
3333 wider_mode = GET_MODE_WIDER_MODE (mode);
3334 size = GET_MODE_BITSIZE (mode);
3336 /* Firstly, try using a multiplication insn that only generates the needed
3337 high part of the product, and in the sign flavor of unsignedp. */
3338 if (mul_highpart_cost[mode] < max_cost)
3340 moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3341 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3342 unsignedp, OPTAB_DIRECT);
3343 if (tem)
3344 return tem;
3347 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3348 Need to adjust the result after the multiplication. */
3349 if (size - 1 < BITS_PER_WORD
3350 && (mul_highpart_cost[mode] + 2 * shift_cost[mode][size-1]
3351 + 4 * add_cost[mode] < max_cost))
3353 moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3354 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3355 unsignedp, OPTAB_DIRECT);
3356 if (tem)
3357 /* We used the wrong signedness. Adjust the result. */
3358 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3359 tem, unsignedp);
3362 /* Try widening multiplication. */
3363 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3364 if (moptab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3365 && mul_widen_cost[wider_mode] < max_cost)
3367 tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3368 unsignedp, OPTAB_WIDEN);
3369 if (tem)
3370 return extract_high_half (mode, tem);
3373 /* Try widening the mode and perform a non-widening multiplication. */
3374 if (smul_optab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3375 && size - 1 < BITS_PER_WORD
3376 && mul_cost[wider_mode] + shift_cost[mode][size-1] < max_cost)
3378 rtx insns, wop0, wop1;
3380 /* We need to widen the operands, for example to ensure the
3381 constant multiplier is correctly sign or zero extended.
3382 Use a sequence to clean-up any instructions emitted by
3383 the conversions if things don't work out. */
3384 start_sequence ();
3385 wop0 = convert_modes (wider_mode, mode, op0, unsignedp);
3386 wop1 = convert_modes (wider_mode, mode, op1, unsignedp);
3387 tem = expand_binop (wider_mode, smul_optab, wop0, wop1, 0,
3388 unsignedp, OPTAB_WIDEN);
3389 insns = get_insns ();
3390 end_sequence ();
3392 if (tem)
3394 emit_insn (insns);
3395 return extract_high_half (mode, tem);
3399 /* Try widening multiplication of opposite signedness, and adjust. */
3400 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3401 if (moptab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3402 && size - 1 < BITS_PER_WORD
3403 && (mul_widen_cost[wider_mode] + 2 * shift_cost[mode][size-1]
3404 + 4 * add_cost[mode] < max_cost))
3406 tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3407 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3408 if (tem != 0)
3410 tem = extract_high_half (mode, tem);
3411 /* We used the wrong signedness. Adjust the result. */
3412 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3413 target, unsignedp);
3417 return 0;
3420 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3421 putting the high half of the result in TARGET if that is convenient,
3422 and return where the result is. If the operation can not be performed,
3423 0 is returned.
3425 MODE is the mode of operation and result.
3427 UNSIGNEDP nonzero means unsigned multiply.
3429 MAX_COST is the total allowed cost for the expanded RTL. */
3431 static rtx
3432 expand_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
3433 rtx target, int unsignedp, int max_cost)
3435 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
3436 unsigned HOST_WIDE_INT cnst1;
3437 int extra_cost;
3438 bool sign_adjust = false;
3439 enum mult_variant variant;
3440 struct algorithm alg;
3441 rtx tem;
3443 /* We can't support modes wider than HOST_BITS_PER_INT. */
3444 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT);
3446 cnst1 = INTVAL (op1) & GET_MODE_MASK (mode);
3448 /* We can't optimize modes wider than BITS_PER_WORD.
3449 ??? We might be able to perform double-word arithmetic if
3450 mode == word_mode, however all the cost calculations in
3451 synth_mult etc. assume single-word operations. */
3452 if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3453 return expand_mult_highpart_optab (mode, op0, op1, target,
3454 unsignedp, max_cost);
3456 extra_cost = shift_cost[mode][GET_MODE_BITSIZE (mode) - 1];
3458 /* Check whether we try to multiply by a negative constant. */
3459 if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3461 sign_adjust = true;
3462 extra_cost += add_cost[mode];
3465 /* See whether shift/add multiplication is cheap enough. */
3466 if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3467 max_cost - extra_cost))
3469 /* See whether the specialized multiplication optabs are
3470 cheaper than the shift/add version. */
3471 tem = expand_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3472 alg.cost.cost + extra_cost);
3473 if (tem)
3474 return tem;
3476 tem = convert_to_mode (wider_mode, op0, unsignedp);
3477 tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3478 tem = extract_high_half (mode, tem);
3480 /* Adjust result for signedness. */
3481 if (sign_adjust)
3482 tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3484 return tem;
3486 return expand_mult_highpart_optab (mode, op0, op1, target,
3487 unsignedp, max_cost);
3491 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3493 static rtx
3494 expand_smod_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3496 unsigned HOST_WIDE_INT masklow, maskhigh;
3497 rtx result, temp, shift, label;
3498 int logd;
3500 logd = floor_log2 (d);
3501 result = gen_reg_rtx (mode);
3503 /* Avoid conditional branches when they're expensive. */
3504 if (BRANCH_COST >= 2
3505 && !optimize_size)
3507 rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3508 mode, 0, -1);
3509 if (signmask)
3511 signmask = force_reg (mode, signmask);
3512 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3513 shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3515 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3516 which instruction sequence to use. If logical right shifts
3517 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3518 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3520 temp = gen_rtx_LSHIFTRT (mode, result, shift);
3521 if (lshr_optab->handlers[mode].insn_code == CODE_FOR_nothing
3522 || rtx_cost (temp, SET) > COSTS_N_INSNS (2))
3524 temp = expand_binop (mode, xor_optab, op0, signmask,
3525 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3526 temp = expand_binop (mode, sub_optab, temp, signmask,
3527 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3528 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3529 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3530 temp = expand_binop (mode, xor_optab, temp, signmask,
3531 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3532 temp = expand_binop (mode, sub_optab, temp, signmask,
3533 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3535 else
3537 signmask = expand_binop (mode, lshr_optab, signmask, shift,
3538 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3539 signmask = force_reg (mode, signmask);
3541 temp = expand_binop (mode, add_optab, op0, signmask,
3542 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3543 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3544 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3545 temp = expand_binop (mode, sub_optab, temp, signmask,
3546 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3548 return temp;
3552 /* Mask contains the mode's signbit and the significant bits of the
3553 modulus. By including the signbit in the operation, many targets
3554 can avoid an explicit compare operation in the following comparison
3555 against zero. */
3557 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3558 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3560 masklow |= (HOST_WIDE_INT) -1 << (GET_MODE_BITSIZE (mode) - 1);
3561 maskhigh = -1;
3563 else
3564 maskhigh = (HOST_WIDE_INT) -1
3565 << (GET_MODE_BITSIZE (mode) - HOST_BITS_PER_WIDE_INT - 1);
3567 temp = expand_binop (mode, and_optab, op0,
3568 immed_double_const (masklow, maskhigh, mode),
3569 result, 1, OPTAB_LIB_WIDEN);
3570 if (temp != result)
3571 emit_move_insn (result, temp);
3573 label = gen_label_rtx ();
3574 do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3576 temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3577 0, OPTAB_LIB_WIDEN);
3578 masklow = (HOST_WIDE_INT) -1 << logd;
3579 maskhigh = -1;
3580 temp = expand_binop (mode, ior_optab, temp,
3581 immed_double_const (masklow, maskhigh, mode),
3582 result, 1, OPTAB_LIB_WIDEN);
3583 temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3584 0, OPTAB_LIB_WIDEN);
3585 if (temp != result)
3586 emit_move_insn (result, temp);
3587 emit_label (label);
3588 return result;
3591 /* Expand signed division of OP0 by a power of two D in mode MODE.
3592 This routine is only called for positive values of D. */
3594 static rtx
3595 expand_sdiv_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3597 rtx temp, label;
3598 tree shift;
3599 int logd;
3601 logd = floor_log2 (d);
3602 shift = build_int_cst (NULL_TREE, logd);
3604 if (d == 2 && BRANCH_COST >= 1)
3606 temp = gen_reg_rtx (mode);
3607 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3608 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3609 0, OPTAB_LIB_WIDEN);
3610 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3613 #ifdef HAVE_conditional_move
3614 if (BRANCH_COST >= 2)
3616 rtx temp2;
3618 /* ??? emit_conditional_move forces a stack adjustment via
3619 compare_from_rtx so, if the sequence is discarded, it will
3620 be lost. Do it now instead. */
3621 do_pending_stack_adjust ();
3623 start_sequence ();
3624 temp2 = copy_to_mode_reg (mode, op0);
3625 temp = expand_binop (mode, add_optab, temp2, GEN_INT (d-1),
3626 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3627 temp = force_reg (mode, temp);
3629 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3630 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3631 mode, temp, temp2, mode, 0);
3632 if (temp2)
3634 rtx seq = get_insns ();
3635 end_sequence ();
3636 emit_insn (seq);
3637 return expand_shift (RSHIFT_EXPR, mode, temp2, shift, NULL_RTX, 0);
3639 end_sequence ();
3641 #endif
3643 if (BRANCH_COST >= 2)
3645 int ushift = GET_MODE_BITSIZE (mode) - logd;
3647 temp = gen_reg_rtx (mode);
3648 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3649 if (shift_cost[mode][ushift] > COSTS_N_INSNS (1))
3650 temp = expand_binop (mode, and_optab, temp, GEN_INT (d - 1),
3651 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3652 else
3653 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3654 build_int_cst (NULL_TREE, ushift),
3655 NULL_RTX, 1);
3656 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3657 0, OPTAB_LIB_WIDEN);
3658 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3661 label = gen_label_rtx ();
3662 temp = copy_to_mode_reg (mode, op0);
3663 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3664 expand_inc (temp, GEN_INT (d - 1));
3665 emit_label (label);
3666 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3669 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3670 if that is convenient, and returning where the result is.
3671 You may request either the quotient or the remainder as the result;
3672 specify REM_FLAG nonzero to get the remainder.
3674 CODE is the expression code for which kind of division this is;
3675 it controls how rounding is done. MODE is the machine mode to use.
3676 UNSIGNEDP nonzero means do unsigned division. */
3678 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3679 and then correct it by or'ing in missing high bits
3680 if result of ANDI is nonzero.
3681 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3682 This could optimize to a bfexts instruction.
3683 But C doesn't use these operations, so their optimizations are
3684 left for later. */
3685 /* ??? For modulo, we don't actually need the highpart of the first product,
3686 the low part will do nicely. And for small divisors, the second multiply
3687 can also be a low-part only multiply or even be completely left out.
3688 E.g. to calculate the remainder of a division by 3 with a 32 bit
3689 multiply, multiply with 0x55555556 and extract the upper two bits;
3690 the result is exact for inputs up to 0x1fffffff.
3691 The input range can be reduced by using cross-sum rules.
3692 For odd divisors >= 3, the following table gives right shift counts
3693 so that if a number is shifted by an integer multiple of the given
3694 amount, the remainder stays the same:
3695 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3696 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3697 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3698 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3699 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3701 Cross-sum rules for even numbers can be derived by leaving as many bits
3702 to the right alone as the divisor has zeros to the right.
3703 E.g. if x is an unsigned 32 bit number:
3704 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3708 expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
3709 rtx op0, rtx op1, rtx target, int unsignedp)
3711 enum machine_mode compute_mode;
3712 rtx tquotient;
3713 rtx quotient = 0, remainder = 0;
3714 rtx last;
3715 int size;
3716 rtx insn, set;
3717 optab optab1, optab2;
3718 int op1_is_constant, op1_is_pow2 = 0;
3719 int max_cost, extra_cost;
3720 static HOST_WIDE_INT last_div_const = 0;
3721 static HOST_WIDE_INT ext_op1;
3723 op1_is_constant = GET_CODE (op1) == CONST_INT;
3724 if (op1_is_constant)
3726 ext_op1 = INTVAL (op1);
3727 if (unsignedp)
3728 ext_op1 &= GET_MODE_MASK (mode);
3729 op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
3730 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
3734 This is the structure of expand_divmod:
3736 First comes code to fix up the operands so we can perform the operations
3737 correctly and efficiently.
3739 Second comes a switch statement with code specific for each rounding mode.
3740 For some special operands this code emits all RTL for the desired
3741 operation, for other cases, it generates only a quotient and stores it in
3742 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3743 to indicate that it has not done anything.
3745 Last comes code that finishes the operation. If QUOTIENT is set and
3746 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3747 QUOTIENT is not set, it is computed using trunc rounding.
3749 We try to generate special code for division and remainder when OP1 is a
3750 constant. If |OP1| = 2**n we can use shifts and some other fast
3751 operations. For other values of OP1, we compute a carefully selected
3752 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3753 by m.
3755 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3756 half of the product. Different strategies for generating the product are
3757 implemented in expand_mult_highpart.
3759 If what we actually want is the remainder, we generate that by another
3760 by-constant multiplication and a subtraction. */
3762 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3763 code below will malfunction if we are, so check here and handle
3764 the special case if so. */
3765 if (op1 == const1_rtx)
3766 return rem_flag ? const0_rtx : op0;
3768 /* When dividing by -1, we could get an overflow.
3769 negv_optab can handle overflows. */
3770 if (! unsignedp && op1 == constm1_rtx)
3772 if (rem_flag)
3773 return const0_rtx;
3774 return expand_unop (mode, flag_trapv && GET_MODE_CLASS(mode) == MODE_INT
3775 ? negv_optab : neg_optab, op0, target, 0);
3778 if (target
3779 /* Don't use the function value register as a target
3780 since we have to read it as well as write it,
3781 and function-inlining gets confused by this. */
3782 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3783 /* Don't clobber an operand while doing a multi-step calculation. */
3784 || ((rem_flag || op1_is_constant)
3785 && (reg_mentioned_p (target, op0)
3786 || (MEM_P (op0) && MEM_P (target))))
3787 || reg_mentioned_p (target, op1)
3788 || (MEM_P (op1) && MEM_P (target))))
3789 target = 0;
3791 /* Get the mode in which to perform this computation. Normally it will
3792 be MODE, but sometimes we can't do the desired operation in MODE.
3793 If so, pick a wider mode in which we can do the operation. Convert
3794 to that mode at the start to avoid repeated conversions.
3796 First see what operations we need. These depend on the expression
3797 we are evaluating. (We assume that divxx3 insns exist under the
3798 same conditions that modxx3 insns and that these insns don't normally
3799 fail. If these assumptions are not correct, we may generate less
3800 efficient code in some cases.)
3802 Then see if we find a mode in which we can open-code that operation
3803 (either a division, modulus, or shift). Finally, check for the smallest
3804 mode for which we can do the operation with a library call. */
3806 /* We might want to refine this now that we have division-by-constant
3807 optimization. Since expand_mult_highpart tries so many variants, it is
3808 not straightforward to generalize this. Maybe we should make an array
3809 of possible modes in init_expmed? Save this for GCC 2.7. */
3811 optab1 = ((op1_is_pow2 && op1 != const0_rtx)
3812 ? (unsignedp ? lshr_optab : ashr_optab)
3813 : (unsignedp ? udiv_optab : sdiv_optab));
3814 optab2 = ((op1_is_pow2 && op1 != const0_rtx)
3815 ? optab1
3816 : (unsignedp ? udivmod_optab : sdivmod_optab));
3818 for (compute_mode = mode; compute_mode != VOIDmode;
3819 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3820 if (optab1->handlers[compute_mode].insn_code != CODE_FOR_nothing
3821 || optab2->handlers[compute_mode].insn_code != CODE_FOR_nothing)
3822 break;
3824 if (compute_mode == VOIDmode)
3825 for (compute_mode = mode; compute_mode != VOIDmode;
3826 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3827 if (optab1->handlers[compute_mode].libfunc
3828 || optab2->handlers[compute_mode].libfunc)
3829 break;
3831 /* If we still couldn't find a mode, use MODE, but expand_binop will
3832 probably die. */
3833 if (compute_mode == VOIDmode)
3834 compute_mode = mode;
3836 if (target && GET_MODE (target) == compute_mode)
3837 tquotient = target;
3838 else
3839 tquotient = gen_reg_rtx (compute_mode);
3841 size = GET_MODE_BITSIZE (compute_mode);
3842 #if 0
3843 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3844 (mode), and thereby get better code when OP1 is a constant. Do that
3845 later. It will require going over all usages of SIZE below. */
3846 size = GET_MODE_BITSIZE (mode);
3847 #endif
3849 /* Only deduct something for a REM if the last divide done was
3850 for a different constant. Then set the constant of the last
3851 divide. */
3852 max_cost = div_cost[compute_mode]
3853 - (rem_flag && ! (last_div_const != 0 && op1_is_constant
3854 && INTVAL (op1) == last_div_const)
3855 ? mul_cost[compute_mode] + add_cost[compute_mode]
3856 : 0);
3858 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
3860 /* Now convert to the best mode to use. */
3861 if (compute_mode != mode)
3863 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
3864 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
3866 /* convert_modes may have placed op1 into a register, so we
3867 must recompute the following. */
3868 op1_is_constant = GET_CODE (op1) == CONST_INT;
3869 op1_is_pow2 = (op1_is_constant
3870 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3871 || (! unsignedp
3872 && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
3875 /* If one of the operands is a volatile MEM, copy it into a register. */
3877 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
3878 op0 = force_reg (compute_mode, op0);
3879 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
3880 op1 = force_reg (compute_mode, op1);
3882 /* If we need the remainder or if OP1 is constant, we need to
3883 put OP0 in a register in case it has any queued subexpressions. */
3884 if (rem_flag || op1_is_constant)
3885 op0 = force_reg (compute_mode, op0);
3887 last = get_last_insn ();
3889 /* Promote floor rounding to trunc rounding for unsigned operations. */
3890 if (unsignedp)
3892 if (code == FLOOR_DIV_EXPR)
3893 code = TRUNC_DIV_EXPR;
3894 if (code == FLOOR_MOD_EXPR)
3895 code = TRUNC_MOD_EXPR;
3896 if (code == EXACT_DIV_EXPR && op1_is_pow2)
3897 code = TRUNC_DIV_EXPR;
3900 if (op1 != const0_rtx)
3901 switch (code)
3903 case TRUNC_MOD_EXPR:
3904 case TRUNC_DIV_EXPR:
3905 if (op1_is_constant)
3907 if (unsignedp)
3909 unsigned HOST_WIDE_INT mh;
3910 int pre_shift, post_shift;
3911 int dummy;
3912 rtx ml;
3913 unsigned HOST_WIDE_INT d = (INTVAL (op1)
3914 & GET_MODE_MASK (compute_mode));
3916 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3918 pre_shift = floor_log2 (d);
3919 if (rem_flag)
3921 remainder
3922 = expand_binop (compute_mode, and_optab, op0,
3923 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3924 remainder, 1,
3925 OPTAB_LIB_WIDEN);
3926 if (remainder)
3927 return gen_lowpart (mode, remainder);
3929 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3930 build_int_cst (NULL_TREE,
3931 pre_shift),
3932 tquotient, 1);
3934 else if (size <= HOST_BITS_PER_WIDE_INT)
3936 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
3938 /* Most significant bit of divisor is set; emit an scc
3939 insn. */
3940 quotient = emit_store_flag (tquotient, GEU, op0, op1,
3941 compute_mode, 1, 1);
3942 if (quotient == 0)
3943 goto fail1;
3945 else
3947 /* Find a suitable multiplier and right shift count
3948 instead of multiplying with D. */
3950 mh = choose_multiplier (d, size, size,
3951 &ml, &post_shift, &dummy);
3953 /* If the suggested multiplier is more than SIZE bits,
3954 we can do better for even divisors, using an
3955 initial right shift. */
3956 if (mh != 0 && (d & 1) == 0)
3958 pre_shift = floor_log2 (d & -d);
3959 mh = choose_multiplier (d >> pre_shift, size,
3960 size - pre_shift,
3961 &ml, &post_shift, &dummy);
3962 gcc_assert (!mh);
3964 else
3965 pre_shift = 0;
3967 if (mh != 0)
3969 rtx t1, t2, t3, t4;
3971 if (post_shift - 1 >= BITS_PER_WORD)
3972 goto fail1;
3974 extra_cost
3975 = (shift_cost[compute_mode][post_shift - 1]
3976 + shift_cost[compute_mode][1]
3977 + 2 * add_cost[compute_mode]);
3978 t1 = expand_mult_highpart (compute_mode, op0, ml,
3979 NULL_RTX, 1,
3980 max_cost - extra_cost);
3981 if (t1 == 0)
3982 goto fail1;
3983 t2 = force_operand (gen_rtx_MINUS (compute_mode,
3984 op0, t1),
3985 NULL_RTX);
3986 t3 = expand_shift
3987 (RSHIFT_EXPR, compute_mode, t2,
3988 build_int_cst (NULL_TREE, 1),
3989 NULL_RTX,1);
3990 t4 = force_operand (gen_rtx_PLUS (compute_mode,
3991 t1, t3),
3992 NULL_RTX);
3993 quotient = expand_shift
3994 (RSHIFT_EXPR, compute_mode, t4,
3995 build_int_cst (NULL_TREE, post_shift - 1),
3996 tquotient, 1);
3998 else
4000 rtx t1, t2;
4002 if (pre_shift >= BITS_PER_WORD
4003 || post_shift >= BITS_PER_WORD)
4004 goto fail1;
4006 t1 = expand_shift
4007 (RSHIFT_EXPR, compute_mode, op0,
4008 build_int_cst (NULL_TREE, pre_shift),
4009 NULL_RTX, 1);
4010 extra_cost
4011 = (shift_cost[compute_mode][pre_shift]
4012 + shift_cost[compute_mode][post_shift]);
4013 t2 = expand_mult_highpart (compute_mode, t1, ml,
4014 NULL_RTX, 1,
4015 max_cost - extra_cost);
4016 if (t2 == 0)
4017 goto fail1;
4018 quotient = expand_shift
4019 (RSHIFT_EXPR, compute_mode, t2,
4020 build_int_cst (NULL_TREE, post_shift),
4021 tquotient, 1);
4025 else /* Too wide mode to use tricky code */
4026 break;
4028 insn = get_last_insn ();
4029 if (insn != last
4030 && (set = single_set (insn)) != 0
4031 && SET_DEST (set) == quotient)
4032 set_unique_reg_note (insn,
4033 REG_EQUAL,
4034 gen_rtx_UDIV (compute_mode, op0, op1));
4036 else /* TRUNC_DIV, signed */
4038 unsigned HOST_WIDE_INT ml;
4039 int lgup, post_shift;
4040 rtx mlr;
4041 HOST_WIDE_INT d = INTVAL (op1);
4042 unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
4044 /* n rem d = n rem -d */
4045 if (rem_flag && d < 0)
4047 d = abs_d;
4048 op1 = gen_int_mode (abs_d, compute_mode);
4051 if (d == 1)
4052 quotient = op0;
4053 else if (d == -1)
4054 quotient = expand_unop (compute_mode, neg_optab, op0,
4055 tquotient, 0);
4056 else if (abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
4058 /* This case is not handled correctly below. */
4059 quotient = emit_store_flag (tquotient, EQ, op0, op1,
4060 compute_mode, 1, 1);
4061 if (quotient == 0)
4062 goto fail1;
4064 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
4065 && (rem_flag ? smod_pow2_cheap[compute_mode]
4066 : sdiv_pow2_cheap[compute_mode])
4067 /* We assume that cheap metric is true if the
4068 optab has an expander for this mode. */
4069 && (((rem_flag ? smod_optab : sdiv_optab)
4070 ->handlers[compute_mode].insn_code
4071 != CODE_FOR_nothing)
4072 || (sdivmod_optab->handlers[compute_mode]
4073 .insn_code != CODE_FOR_nothing)))
4075 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
4077 if (rem_flag)
4079 remainder = expand_smod_pow2 (compute_mode, op0, d);
4080 if (remainder)
4081 return gen_lowpart (mode, remainder);
4084 if (sdiv_pow2_cheap[compute_mode]
4085 && ((sdiv_optab->handlers[compute_mode].insn_code
4086 != CODE_FOR_nothing)
4087 || (sdivmod_optab->handlers[compute_mode].insn_code
4088 != CODE_FOR_nothing)))
4089 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
4090 compute_mode, op0,
4091 gen_int_mode (abs_d,
4092 compute_mode),
4093 NULL_RTX, 0);
4094 else
4095 quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
4097 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4098 negate the quotient. */
4099 if (d < 0)
4101 insn = get_last_insn ();
4102 if (insn != last
4103 && (set = single_set (insn)) != 0
4104 && SET_DEST (set) == quotient
4105 && abs_d < ((unsigned HOST_WIDE_INT) 1
4106 << (HOST_BITS_PER_WIDE_INT - 1)))
4107 set_unique_reg_note (insn,
4108 REG_EQUAL,
4109 gen_rtx_DIV (compute_mode,
4110 op0,
4111 GEN_INT
4112 (trunc_int_for_mode
4113 (abs_d,
4114 compute_mode))));
4116 quotient = expand_unop (compute_mode, neg_optab,
4117 quotient, quotient, 0);
4120 else if (size <= HOST_BITS_PER_WIDE_INT)
4122 choose_multiplier (abs_d, size, size - 1,
4123 &mlr, &post_shift, &lgup);
4124 ml = (unsigned HOST_WIDE_INT) INTVAL (mlr);
4125 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
4127 rtx t1, t2, t3;
4129 if (post_shift >= BITS_PER_WORD
4130 || size - 1 >= BITS_PER_WORD)
4131 goto fail1;
4133 extra_cost = (shift_cost[compute_mode][post_shift]
4134 + shift_cost[compute_mode][size - 1]
4135 + add_cost[compute_mode]);
4136 t1 = expand_mult_highpart (compute_mode, op0, mlr,
4137 NULL_RTX, 0,
4138 max_cost - extra_cost);
4139 if (t1 == 0)
4140 goto fail1;
4141 t2 = expand_shift
4142 (RSHIFT_EXPR, compute_mode, t1,
4143 build_int_cst (NULL_TREE, post_shift),
4144 NULL_RTX, 0);
4145 t3 = expand_shift
4146 (RSHIFT_EXPR, compute_mode, op0,
4147 build_int_cst (NULL_TREE, size - 1),
4148 NULL_RTX, 0);
4149 if (d < 0)
4150 quotient
4151 = force_operand (gen_rtx_MINUS (compute_mode,
4152 t3, t2),
4153 tquotient);
4154 else
4155 quotient
4156 = force_operand (gen_rtx_MINUS (compute_mode,
4157 t2, t3),
4158 tquotient);
4160 else
4162 rtx t1, t2, t3, t4;
4164 if (post_shift >= BITS_PER_WORD
4165 || size - 1 >= BITS_PER_WORD)
4166 goto fail1;
4168 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
4169 mlr = gen_int_mode (ml, compute_mode);
4170 extra_cost = (shift_cost[compute_mode][post_shift]
4171 + shift_cost[compute_mode][size - 1]
4172 + 2 * add_cost[compute_mode]);
4173 t1 = expand_mult_highpart (compute_mode, op0, mlr,
4174 NULL_RTX, 0,
4175 max_cost - extra_cost);
4176 if (t1 == 0)
4177 goto fail1;
4178 t2 = force_operand (gen_rtx_PLUS (compute_mode,
4179 t1, op0),
4180 NULL_RTX);
4181 t3 = expand_shift
4182 (RSHIFT_EXPR, compute_mode, t2,
4183 build_int_cst (NULL_TREE, post_shift),
4184 NULL_RTX, 0);
4185 t4 = expand_shift
4186 (RSHIFT_EXPR, compute_mode, op0,
4187 build_int_cst (NULL_TREE, size - 1),
4188 NULL_RTX, 0);
4189 if (d < 0)
4190 quotient
4191 = force_operand (gen_rtx_MINUS (compute_mode,
4192 t4, t3),
4193 tquotient);
4194 else
4195 quotient
4196 = force_operand (gen_rtx_MINUS (compute_mode,
4197 t3, t4),
4198 tquotient);
4201 else /* Too wide mode to use tricky code */
4202 break;
4204 insn = get_last_insn ();
4205 if (insn != last
4206 && (set = single_set (insn)) != 0
4207 && SET_DEST (set) == quotient)
4208 set_unique_reg_note (insn,
4209 REG_EQUAL,
4210 gen_rtx_DIV (compute_mode, op0, op1));
4212 break;
4214 fail1:
4215 delete_insns_since (last);
4216 break;
4218 case FLOOR_DIV_EXPR:
4219 case FLOOR_MOD_EXPR:
4220 /* We will come here only for signed operations. */
4221 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4223 unsigned HOST_WIDE_INT mh;
4224 int pre_shift, lgup, post_shift;
4225 HOST_WIDE_INT d = INTVAL (op1);
4226 rtx ml;
4228 if (d > 0)
4230 /* We could just as easily deal with negative constants here,
4231 but it does not seem worth the trouble for GCC 2.6. */
4232 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4234 pre_shift = floor_log2 (d);
4235 if (rem_flag)
4237 remainder = expand_binop (compute_mode, and_optab, op0,
4238 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4239 remainder, 0, OPTAB_LIB_WIDEN);
4240 if (remainder)
4241 return gen_lowpart (mode, remainder);
4243 quotient = expand_shift
4244 (RSHIFT_EXPR, compute_mode, op0,
4245 build_int_cst (NULL_TREE, pre_shift),
4246 tquotient, 0);
4248 else
4250 rtx t1, t2, t3, t4;
4252 mh = choose_multiplier (d, size, size - 1,
4253 &ml, &post_shift, &lgup);
4254 gcc_assert (!mh);
4256 if (post_shift < BITS_PER_WORD
4257 && size - 1 < BITS_PER_WORD)
4259 t1 = expand_shift
4260 (RSHIFT_EXPR, compute_mode, op0,
4261 build_int_cst (NULL_TREE, size - 1),
4262 NULL_RTX, 0);
4263 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
4264 NULL_RTX, 0, OPTAB_WIDEN);
4265 extra_cost = (shift_cost[compute_mode][post_shift]
4266 + shift_cost[compute_mode][size - 1]
4267 + 2 * add_cost[compute_mode]);
4268 t3 = expand_mult_highpart (compute_mode, t2, ml,
4269 NULL_RTX, 1,
4270 max_cost - extra_cost);
4271 if (t3 != 0)
4273 t4 = expand_shift
4274 (RSHIFT_EXPR, compute_mode, t3,
4275 build_int_cst (NULL_TREE, post_shift),
4276 NULL_RTX, 1);
4277 quotient = expand_binop (compute_mode, xor_optab,
4278 t4, t1, tquotient, 0,
4279 OPTAB_WIDEN);
4284 else
4286 rtx nsign, t1, t2, t3, t4;
4287 t1 = force_operand (gen_rtx_PLUS (compute_mode,
4288 op0, constm1_rtx), NULL_RTX);
4289 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
4290 0, OPTAB_WIDEN);
4291 nsign = expand_shift
4292 (RSHIFT_EXPR, compute_mode, t2,
4293 build_int_cst (NULL_TREE, size - 1),
4294 NULL_RTX, 0);
4295 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
4296 NULL_RTX);
4297 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
4298 NULL_RTX, 0);
4299 if (t4)
4301 rtx t5;
4302 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
4303 NULL_RTX, 0);
4304 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4305 t4, t5),
4306 tquotient);
4311 if (quotient != 0)
4312 break;
4313 delete_insns_since (last);
4315 /* Try using an instruction that produces both the quotient and
4316 remainder, using truncation. We can easily compensate the quotient
4317 or remainder to get floor rounding, once we have the remainder.
4318 Notice that we compute also the final remainder value here,
4319 and return the result right away. */
4320 if (target == 0 || GET_MODE (target) != compute_mode)
4321 target = gen_reg_rtx (compute_mode);
4323 if (rem_flag)
4325 remainder
4326 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4327 quotient = gen_reg_rtx (compute_mode);
4329 else
4331 quotient
4332 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4333 remainder = gen_reg_rtx (compute_mode);
4336 if (expand_twoval_binop (sdivmod_optab, op0, op1,
4337 quotient, remainder, 0))
4339 /* This could be computed with a branch-less sequence.
4340 Save that for later. */
4341 rtx tem;
4342 rtx label = gen_label_rtx ();
4343 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4344 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4345 NULL_RTX, 0, OPTAB_WIDEN);
4346 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4347 expand_dec (quotient, const1_rtx);
4348 expand_inc (remainder, op1);
4349 emit_label (label);
4350 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4353 /* No luck with division elimination or divmod. Have to do it
4354 by conditionally adjusting op0 *and* the result. */
4356 rtx label1, label2, label3, label4, label5;
4357 rtx adjusted_op0;
4358 rtx tem;
4360 quotient = gen_reg_rtx (compute_mode);
4361 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4362 label1 = gen_label_rtx ();
4363 label2 = gen_label_rtx ();
4364 label3 = gen_label_rtx ();
4365 label4 = gen_label_rtx ();
4366 label5 = gen_label_rtx ();
4367 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4368 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4369 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4370 quotient, 0, OPTAB_LIB_WIDEN);
4371 if (tem != quotient)
4372 emit_move_insn (quotient, tem);
4373 emit_jump_insn (gen_jump (label5));
4374 emit_barrier ();
4375 emit_label (label1);
4376 expand_inc (adjusted_op0, const1_rtx);
4377 emit_jump_insn (gen_jump (label4));
4378 emit_barrier ();
4379 emit_label (label2);
4380 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4381 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4382 quotient, 0, OPTAB_LIB_WIDEN);
4383 if (tem != quotient)
4384 emit_move_insn (quotient, tem);
4385 emit_jump_insn (gen_jump (label5));
4386 emit_barrier ();
4387 emit_label (label3);
4388 expand_dec (adjusted_op0, const1_rtx);
4389 emit_label (label4);
4390 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4391 quotient, 0, OPTAB_LIB_WIDEN);
4392 if (tem != quotient)
4393 emit_move_insn (quotient, tem);
4394 expand_dec (quotient, const1_rtx);
4395 emit_label (label5);
4397 break;
4399 case CEIL_DIV_EXPR:
4400 case CEIL_MOD_EXPR:
4401 if (unsignedp)
4403 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
4405 rtx t1, t2, t3;
4406 unsigned HOST_WIDE_INT d = INTVAL (op1);
4407 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4408 build_int_cst (NULL_TREE, floor_log2 (d)),
4409 tquotient, 1);
4410 t2 = expand_binop (compute_mode, and_optab, op0,
4411 GEN_INT (d - 1),
4412 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4413 t3 = gen_reg_rtx (compute_mode);
4414 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4415 compute_mode, 1, 1);
4416 if (t3 == 0)
4418 rtx lab;
4419 lab = gen_label_rtx ();
4420 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4421 expand_inc (t1, const1_rtx);
4422 emit_label (lab);
4423 quotient = t1;
4425 else
4426 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4427 t1, t3),
4428 tquotient);
4429 break;
4432 /* Try using an instruction that produces both the quotient and
4433 remainder, using truncation. We can easily compensate the
4434 quotient or remainder to get ceiling rounding, once we have the
4435 remainder. Notice that we compute also the final remainder
4436 value here, and return the result right away. */
4437 if (target == 0 || GET_MODE (target) != compute_mode)
4438 target = gen_reg_rtx (compute_mode);
4440 if (rem_flag)
4442 remainder = (REG_P (target)
4443 ? target : gen_reg_rtx (compute_mode));
4444 quotient = gen_reg_rtx (compute_mode);
4446 else
4448 quotient = (REG_P (target)
4449 ? target : gen_reg_rtx (compute_mode));
4450 remainder = gen_reg_rtx (compute_mode);
4453 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4454 remainder, 1))
4456 /* This could be computed with a branch-less sequence.
4457 Save that for later. */
4458 rtx label = gen_label_rtx ();
4459 do_cmp_and_jump (remainder, const0_rtx, EQ,
4460 compute_mode, label);
4461 expand_inc (quotient, const1_rtx);
4462 expand_dec (remainder, op1);
4463 emit_label (label);
4464 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4467 /* No luck with division elimination or divmod. Have to do it
4468 by conditionally adjusting op0 *and* the result. */
4470 rtx label1, label2;
4471 rtx adjusted_op0, tem;
4473 quotient = gen_reg_rtx (compute_mode);
4474 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4475 label1 = gen_label_rtx ();
4476 label2 = gen_label_rtx ();
4477 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4478 compute_mode, label1);
4479 emit_move_insn (quotient, const0_rtx);
4480 emit_jump_insn (gen_jump (label2));
4481 emit_barrier ();
4482 emit_label (label1);
4483 expand_dec (adjusted_op0, const1_rtx);
4484 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4485 quotient, 1, OPTAB_LIB_WIDEN);
4486 if (tem != quotient)
4487 emit_move_insn (quotient, tem);
4488 expand_inc (quotient, const1_rtx);
4489 emit_label (label2);
4492 else /* signed */
4494 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4495 && INTVAL (op1) >= 0)
4497 /* This is extremely similar to the code for the unsigned case
4498 above. For 2.7 we should merge these variants, but for
4499 2.6.1 I don't want to touch the code for unsigned since that
4500 get used in C. The signed case will only be used by other
4501 languages (Ada). */
4503 rtx t1, t2, t3;
4504 unsigned HOST_WIDE_INT d = INTVAL (op1);
4505 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4506 build_int_cst (NULL_TREE, floor_log2 (d)),
4507 tquotient, 0);
4508 t2 = expand_binop (compute_mode, and_optab, op0,
4509 GEN_INT (d - 1),
4510 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4511 t3 = gen_reg_rtx (compute_mode);
4512 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4513 compute_mode, 1, 1);
4514 if (t3 == 0)
4516 rtx lab;
4517 lab = gen_label_rtx ();
4518 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4519 expand_inc (t1, const1_rtx);
4520 emit_label (lab);
4521 quotient = t1;
4523 else
4524 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4525 t1, t3),
4526 tquotient);
4527 break;
4530 /* Try using an instruction that produces both the quotient and
4531 remainder, using truncation. We can easily compensate the
4532 quotient or remainder to get ceiling rounding, once we have the
4533 remainder. Notice that we compute also the final remainder
4534 value here, and return the result right away. */
4535 if (target == 0 || GET_MODE (target) != compute_mode)
4536 target = gen_reg_rtx (compute_mode);
4537 if (rem_flag)
4539 remainder= (REG_P (target)
4540 ? target : gen_reg_rtx (compute_mode));
4541 quotient = gen_reg_rtx (compute_mode);
4543 else
4545 quotient = (REG_P (target)
4546 ? target : gen_reg_rtx (compute_mode));
4547 remainder = gen_reg_rtx (compute_mode);
4550 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4551 remainder, 0))
4553 /* This could be computed with a branch-less sequence.
4554 Save that for later. */
4555 rtx tem;
4556 rtx label = gen_label_rtx ();
4557 do_cmp_and_jump (remainder, const0_rtx, EQ,
4558 compute_mode, label);
4559 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4560 NULL_RTX, 0, OPTAB_WIDEN);
4561 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4562 expand_inc (quotient, const1_rtx);
4563 expand_dec (remainder, op1);
4564 emit_label (label);
4565 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4568 /* No luck with division elimination or divmod. Have to do it
4569 by conditionally adjusting op0 *and* the result. */
4571 rtx label1, label2, label3, label4, label5;
4572 rtx adjusted_op0;
4573 rtx tem;
4575 quotient = gen_reg_rtx (compute_mode);
4576 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4577 label1 = gen_label_rtx ();
4578 label2 = gen_label_rtx ();
4579 label3 = gen_label_rtx ();
4580 label4 = gen_label_rtx ();
4581 label5 = gen_label_rtx ();
4582 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4583 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4584 compute_mode, label1);
4585 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4586 quotient, 0, OPTAB_LIB_WIDEN);
4587 if (tem != quotient)
4588 emit_move_insn (quotient, tem);
4589 emit_jump_insn (gen_jump (label5));
4590 emit_barrier ();
4591 emit_label (label1);
4592 expand_dec (adjusted_op0, const1_rtx);
4593 emit_jump_insn (gen_jump (label4));
4594 emit_barrier ();
4595 emit_label (label2);
4596 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4597 compute_mode, label3);
4598 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4599 quotient, 0, OPTAB_LIB_WIDEN);
4600 if (tem != quotient)
4601 emit_move_insn (quotient, tem);
4602 emit_jump_insn (gen_jump (label5));
4603 emit_barrier ();
4604 emit_label (label3);
4605 expand_inc (adjusted_op0, const1_rtx);
4606 emit_label (label4);
4607 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4608 quotient, 0, OPTAB_LIB_WIDEN);
4609 if (tem != quotient)
4610 emit_move_insn (quotient, tem);
4611 expand_inc (quotient, const1_rtx);
4612 emit_label (label5);
4615 break;
4617 case EXACT_DIV_EXPR:
4618 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4620 HOST_WIDE_INT d = INTVAL (op1);
4621 unsigned HOST_WIDE_INT ml;
4622 int pre_shift;
4623 rtx t1;
4625 pre_shift = floor_log2 (d & -d);
4626 ml = invert_mod2n (d >> pre_shift, size);
4627 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4628 build_int_cst (NULL_TREE, pre_shift),
4629 NULL_RTX, unsignedp);
4630 quotient = expand_mult (compute_mode, t1,
4631 gen_int_mode (ml, compute_mode),
4632 NULL_RTX, 1);
4634 insn = get_last_insn ();
4635 set_unique_reg_note (insn,
4636 REG_EQUAL,
4637 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4638 compute_mode,
4639 op0, op1));
4641 break;
4643 case ROUND_DIV_EXPR:
4644 case ROUND_MOD_EXPR:
4645 if (unsignedp)
4647 rtx tem;
4648 rtx label;
4649 label = gen_label_rtx ();
4650 quotient = gen_reg_rtx (compute_mode);
4651 remainder = gen_reg_rtx (compute_mode);
4652 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4654 rtx tem;
4655 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
4656 quotient, 1, OPTAB_LIB_WIDEN);
4657 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
4658 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4659 remainder, 1, OPTAB_LIB_WIDEN);
4661 tem = plus_constant (op1, -1);
4662 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4663 build_int_cst (NULL_TREE, 1),
4664 NULL_RTX, 1);
4665 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
4666 expand_inc (quotient, const1_rtx);
4667 expand_dec (remainder, op1);
4668 emit_label (label);
4670 else
4672 rtx abs_rem, abs_op1, tem, mask;
4673 rtx label;
4674 label = gen_label_rtx ();
4675 quotient = gen_reg_rtx (compute_mode);
4676 remainder = gen_reg_rtx (compute_mode);
4677 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
4679 rtx tem;
4680 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
4681 quotient, 0, OPTAB_LIB_WIDEN);
4682 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
4683 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4684 remainder, 0, OPTAB_LIB_WIDEN);
4686 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
4687 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
4688 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
4689 build_int_cst (NULL_TREE, 1),
4690 NULL_RTX, 1);
4691 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
4692 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4693 NULL_RTX, 0, OPTAB_WIDEN);
4694 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4695 build_int_cst (NULL_TREE, size - 1),
4696 NULL_RTX, 0);
4697 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
4698 NULL_RTX, 0, OPTAB_WIDEN);
4699 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4700 NULL_RTX, 0, OPTAB_WIDEN);
4701 expand_inc (quotient, tem);
4702 tem = expand_binop (compute_mode, xor_optab, mask, op1,
4703 NULL_RTX, 0, OPTAB_WIDEN);
4704 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4705 NULL_RTX, 0, OPTAB_WIDEN);
4706 expand_dec (remainder, tem);
4707 emit_label (label);
4709 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4711 default:
4712 gcc_unreachable ();
4715 if (quotient == 0)
4717 if (target && GET_MODE (target) != compute_mode)
4718 target = 0;
4720 if (rem_flag)
4722 /* Try to produce the remainder without producing the quotient.
4723 If we seem to have a divmod pattern that does not require widening,
4724 don't try widening here. We should really have a WIDEN argument
4725 to expand_twoval_binop, since what we'd really like to do here is
4726 1) try a mod insn in compute_mode
4727 2) try a divmod insn in compute_mode
4728 3) try a div insn in compute_mode and multiply-subtract to get
4729 remainder
4730 4) try the same things with widening allowed. */
4731 remainder
4732 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4733 op0, op1, target,
4734 unsignedp,
4735 ((optab2->handlers[compute_mode].insn_code
4736 != CODE_FOR_nothing)
4737 ? OPTAB_DIRECT : OPTAB_WIDEN));
4738 if (remainder == 0)
4740 /* No luck there. Can we do remainder and divide at once
4741 without a library call? */
4742 remainder = gen_reg_rtx (compute_mode);
4743 if (! expand_twoval_binop ((unsignedp
4744 ? udivmod_optab
4745 : sdivmod_optab),
4746 op0, op1,
4747 NULL_RTX, remainder, unsignedp))
4748 remainder = 0;
4751 if (remainder)
4752 return gen_lowpart (mode, remainder);
4755 /* Produce the quotient. Try a quotient insn, but not a library call.
4756 If we have a divmod in this mode, use it in preference to widening
4757 the div (for this test we assume it will not fail). Note that optab2
4758 is set to the one of the two optabs that the call below will use. */
4759 quotient
4760 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
4761 op0, op1, rem_flag ? NULL_RTX : target,
4762 unsignedp,
4763 ((optab2->handlers[compute_mode].insn_code
4764 != CODE_FOR_nothing)
4765 ? OPTAB_DIRECT : OPTAB_WIDEN));
4767 if (quotient == 0)
4769 /* No luck there. Try a quotient-and-remainder insn,
4770 keeping the quotient alone. */
4771 quotient = gen_reg_rtx (compute_mode);
4772 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
4773 op0, op1,
4774 quotient, NULL_RTX, unsignedp))
4776 quotient = 0;
4777 if (! rem_flag)
4778 /* Still no luck. If we are not computing the remainder,
4779 use a library call for the quotient. */
4780 quotient = sign_expand_binop (compute_mode,
4781 udiv_optab, sdiv_optab,
4782 op0, op1, target,
4783 unsignedp, OPTAB_LIB_WIDEN);
4788 if (rem_flag)
4790 if (target && GET_MODE (target) != compute_mode)
4791 target = 0;
4793 if (quotient == 0)
4795 /* No divide instruction either. Use library for remainder. */
4796 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4797 op0, op1, target,
4798 unsignedp, OPTAB_LIB_WIDEN);
4799 /* No remainder function. Try a quotient-and-remainder
4800 function, keeping the remainder. */
4801 if (!remainder)
4803 remainder = gen_reg_rtx (compute_mode);
4804 if (!expand_twoval_binop_libfunc
4805 (unsignedp ? udivmod_optab : sdivmod_optab,
4806 op0, op1,
4807 NULL_RTX, remainder,
4808 unsignedp ? UMOD : MOD))
4809 remainder = NULL_RTX;
4812 else
4814 /* We divided. Now finish doing X - Y * (X / Y). */
4815 remainder = expand_mult (compute_mode, quotient, op1,
4816 NULL_RTX, unsignedp);
4817 remainder = expand_binop (compute_mode, sub_optab, op0,
4818 remainder, target, unsignedp,
4819 OPTAB_LIB_WIDEN);
4823 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4826 /* Return a tree node with data type TYPE, describing the value of X.
4827 Usually this is an VAR_DECL, if there is no obvious better choice.
4828 X may be an expression, however we only support those expressions
4829 generated by loop.c. */
4831 tree
4832 make_tree (tree type, rtx x)
4834 tree t;
4836 switch (GET_CODE (x))
4838 case CONST_INT:
4840 HOST_WIDE_INT hi = 0;
4842 if (INTVAL (x) < 0
4843 && !(TYPE_UNSIGNED (type)
4844 && (GET_MODE_BITSIZE (TYPE_MODE (type))
4845 < HOST_BITS_PER_WIDE_INT)))
4846 hi = -1;
4848 t = build_int_cst_wide (type, INTVAL (x), hi);
4850 return t;
4853 case CONST_DOUBLE:
4854 if (GET_MODE (x) == VOIDmode)
4855 t = build_int_cst_wide (type,
4856 CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
4857 else
4859 REAL_VALUE_TYPE d;
4861 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
4862 t = build_real (type, d);
4865 return t;
4867 case CONST_VECTOR:
4869 int i, units;
4870 rtx elt;
4871 tree t = NULL_TREE;
4873 units = CONST_VECTOR_NUNITS (x);
4875 /* Build a tree with vector elements. */
4876 for (i = units - 1; i >= 0; --i)
4878 elt = CONST_VECTOR_ELT (x, i);
4879 t = tree_cons (NULL_TREE, make_tree (type, elt), t);
4882 return build_vector (type, t);
4885 case PLUS:
4886 return fold_build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4887 make_tree (type, XEXP (x, 1)));
4889 case MINUS:
4890 return fold_build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4891 make_tree (type, XEXP (x, 1)));
4893 case NEG:
4894 return fold_build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0)));
4896 case MULT:
4897 return fold_build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
4898 make_tree (type, XEXP (x, 1)));
4900 case ASHIFT:
4901 return fold_build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
4902 make_tree (type, XEXP (x, 1)));
4904 case LSHIFTRT:
4905 t = lang_hooks.types.unsigned_type (type);
4906 return fold_convert (type, build2 (RSHIFT_EXPR, t,
4907 make_tree (t, XEXP (x, 0)),
4908 make_tree (type, XEXP (x, 1))));
4910 case ASHIFTRT:
4911 t = lang_hooks.types.signed_type (type);
4912 return fold_convert (type, build2 (RSHIFT_EXPR, t,
4913 make_tree (t, XEXP (x, 0)),
4914 make_tree (type, XEXP (x, 1))));
4916 case DIV:
4917 if (TREE_CODE (type) != REAL_TYPE)
4918 t = lang_hooks.types.signed_type (type);
4919 else
4920 t = type;
4922 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
4923 make_tree (t, XEXP (x, 0)),
4924 make_tree (t, XEXP (x, 1))));
4925 case UDIV:
4926 t = lang_hooks.types.unsigned_type (type);
4927 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
4928 make_tree (t, XEXP (x, 0)),
4929 make_tree (t, XEXP (x, 1))));
4931 case SIGN_EXTEND:
4932 case ZERO_EXTEND:
4933 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
4934 GET_CODE (x) == ZERO_EXTEND);
4935 return fold_convert (type, make_tree (t, XEXP (x, 0)));
4937 default:
4938 t = build_decl (VAR_DECL, NULL_TREE, type);
4940 /* If TYPE is a POINTER_TYPE, X might be Pmode with TYPE_MODE being
4941 ptr_mode. So convert. */
4942 if (POINTER_TYPE_P (type))
4943 x = convert_memory_address (TYPE_MODE (type), x);
4945 /* Note that we do *not* use SET_DECL_RTL here, because we do not
4946 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
4947 t->decl_with_rtl.rtl = x;
4949 return t;
4953 /* Check whether the multiplication X * MULT + ADD overflows.
4954 X, MULT and ADD must be CONST_*.
4955 MODE is the machine mode for the computation.
4956 X and MULT must have mode MODE. ADD may have a different mode.
4957 So can X (defaults to same as MODE).
4958 UNSIGNEDP is nonzero to do unsigned multiplication. */
4960 bool
4961 const_mult_add_overflow_p (rtx x, rtx mult, rtx add,
4962 enum machine_mode mode, int unsignedp)
4964 tree type, mult_type, add_type, result;
4966 type = lang_hooks.types.type_for_mode (mode, unsignedp);
4968 /* In order to get a proper overflow indication from an unsigned
4969 type, we have to pretend that it's a sizetype. */
4970 mult_type = type;
4971 if (unsignedp)
4973 /* FIXME:It would be nice if we could step directly from this
4974 type to its sizetype equivalent. */
4975 mult_type = build_distinct_type_copy (type);
4976 TYPE_IS_SIZETYPE (mult_type) = 1;
4979 add_type = (GET_MODE (add) == VOIDmode ? mult_type
4980 : lang_hooks.types.type_for_mode (GET_MODE (add), unsignedp));
4982 result = fold_build2 (PLUS_EXPR, mult_type,
4983 fold_build2 (MULT_EXPR, mult_type,
4984 make_tree (mult_type, x),
4985 make_tree (mult_type, mult)),
4986 make_tree (add_type, add));
4988 return TREE_CONSTANT_OVERFLOW (result);
4991 /* Return an rtx representing the value of X * MULT + ADD.
4992 TARGET is a suggestion for where to store the result (an rtx).
4993 MODE is the machine mode for the computation.
4994 X and MULT must have mode MODE. ADD may have a different mode.
4995 So can X (defaults to same as MODE).
4996 UNSIGNEDP is nonzero to do unsigned multiplication.
4997 This may emit insns. */
5000 expand_mult_add (rtx x, rtx target, rtx mult, rtx add, enum machine_mode mode,
5001 int unsignedp)
5003 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
5004 tree add_type = (GET_MODE (add) == VOIDmode
5005 ? type: lang_hooks.types.type_for_mode (GET_MODE (add),
5006 unsignedp));
5007 tree result = fold_build2 (PLUS_EXPR, type,
5008 fold_build2 (MULT_EXPR, type,
5009 make_tree (type, x),
5010 make_tree (type, mult)),
5011 make_tree (add_type, add));
5013 return expand_expr (result, target, VOIDmode, 0);
5016 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5017 and returning TARGET.
5019 If TARGET is 0, a pseudo-register or constant is returned. */
5022 expand_and (enum machine_mode mode, rtx op0, rtx op1, rtx target)
5024 rtx tem = 0;
5026 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
5027 tem = simplify_binary_operation (AND, mode, op0, op1);
5028 if (tem == 0)
5029 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
5031 if (target == 0)
5032 target = tem;
5033 else if (tem != target)
5034 emit_move_insn (target, tem);
5035 return target;
5038 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5039 and storing in TARGET. Normally return TARGET.
5040 Return 0 if that cannot be done.
5042 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5043 it is VOIDmode, they cannot both be CONST_INT.
5045 UNSIGNEDP is for the case where we have to widen the operands
5046 to perform the operation. It says to use zero-extension.
5048 NORMALIZEP is 1 if we should convert the result to be either zero
5049 or one. Normalize is -1 if we should convert the result to be
5050 either zero or -1. If NORMALIZEP is zero, the result will be left
5051 "raw" out of the scc insn. */
5054 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
5055 enum machine_mode mode, int unsignedp, int normalizep)
5057 rtx subtarget;
5058 enum insn_code icode;
5059 enum machine_mode compare_mode;
5060 enum machine_mode target_mode = GET_MODE (target);
5061 rtx tem;
5062 rtx last = get_last_insn ();
5063 rtx pattern, comparison;
5065 if (unsignedp)
5066 code = unsigned_condition (code);
5068 /* If one operand is constant, make it the second one. Only do this
5069 if the other operand is not constant as well. */
5071 if (swap_commutative_operands_p (op0, op1))
5073 tem = op0;
5074 op0 = op1;
5075 op1 = tem;
5076 code = swap_condition (code);
5079 if (mode == VOIDmode)
5080 mode = GET_MODE (op0);
5082 /* For some comparisons with 1 and -1, we can convert this to
5083 comparisons with zero. This will often produce more opportunities for
5084 store-flag insns. */
5086 switch (code)
5088 case LT:
5089 if (op1 == const1_rtx)
5090 op1 = const0_rtx, code = LE;
5091 break;
5092 case LE:
5093 if (op1 == constm1_rtx)
5094 op1 = const0_rtx, code = LT;
5095 break;
5096 case GE:
5097 if (op1 == const1_rtx)
5098 op1 = const0_rtx, code = GT;
5099 break;
5100 case GT:
5101 if (op1 == constm1_rtx)
5102 op1 = const0_rtx, code = GE;
5103 break;
5104 case GEU:
5105 if (op1 == const1_rtx)
5106 op1 = const0_rtx, code = NE;
5107 break;
5108 case LTU:
5109 if (op1 == const1_rtx)
5110 op1 = const0_rtx, code = EQ;
5111 break;
5112 default:
5113 break;
5116 /* If we are comparing a double-word integer with zero or -1, we can
5117 convert the comparison into one involving a single word. */
5118 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
5119 && GET_MODE_CLASS (mode) == MODE_INT
5120 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
5122 if ((code == EQ || code == NE)
5123 && (op1 == const0_rtx || op1 == constm1_rtx))
5125 rtx op00, op01, op0both;
5127 /* Do a logical OR or AND of the two words and compare the result. */
5128 op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
5129 op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
5130 op0both = expand_binop (word_mode,
5131 op1 == const0_rtx ? ior_optab : and_optab,
5132 op00, op01, NULL_RTX, unsignedp, OPTAB_DIRECT);
5134 if (op0both != 0)
5135 return emit_store_flag (target, code, op0both, op1, word_mode,
5136 unsignedp, normalizep);
5138 else if ((code == LT || code == GE) && op1 == const0_rtx)
5140 rtx op0h;
5142 /* If testing the sign bit, can just test on high word. */
5143 op0h = simplify_gen_subreg (word_mode, op0, mode,
5144 subreg_highpart_offset (word_mode, mode));
5145 return emit_store_flag (target, code, op0h, op1, word_mode,
5146 unsignedp, normalizep);
5150 /* From now on, we won't change CODE, so set ICODE now. */
5151 icode = setcc_gen_code[(int) code];
5153 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5154 complement of A (for GE) and shifting the sign bit to the low bit. */
5155 if (op1 == const0_rtx && (code == LT || code == GE)
5156 && GET_MODE_CLASS (mode) == MODE_INT
5157 && (normalizep || STORE_FLAG_VALUE == 1
5158 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5159 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5160 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
5162 subtarget = target;
5164 /* If the result is to be wider than OP0, it is best to convert it
5165 first. If it is to be narrower, it is *incorrect* to convert it
5166 first. */
5167 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
5169 op0 = convert_modes (target_mode, mode, op0, 0);
5170 mode = target_mode;
5173 if (target_mode != mode)
5174 subtarget = 0;
5176 if (code == GE)
5177 op0 = expand_unop (mode, one_cmpl_optab, op0,
5178 ((STORE_FLAG_VALUE == 1 || normalizep)
5179 ? 0 : subtarget), 0);
5181 if (STORE_FLAG_VALUE == 1 || normalizep)
5182 /* If we are supposed to produce a 0/1 value, we want to do
5183 a logical shift from the sign bit to the low-order bit; for
5184 a -1/0 value, we do an arithmetic shift. */
5185 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
5186 size_int (GET_MODE_BITSIZE (mode) - 1),
5187 subtarget, normalizep != -1);
5189 if (mode != target_mode)
5190 op0 = convert_modes (target_mode, mode, op0, 0);
5192 return op0;
5195 if (icode != CODE_FOR_nothing)
5197 insn_operand_predicate_fn pred;
5199 /* We think we may be able to do this with a scc insn. Emit the
5200 comparison and then the scc insn. */
5202 do_pending_stack_adjust ();
5203 last = get_last_insn ();
5205 comparison
5206 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX);
5207 if (CONSTANT_P (comparison))
5209 switch (GET_CODE (comparison))
5211 case CONST_INT:
5212 if (comparison == const0_rtx)
5213 return const0_rtx;
5214 break;
5216 #ifdef FLOAT_STORE_FLAG_VALUE
5217 case CONST_DOUBLE:
5218 if (comparison == CONST0_RTX (GET_MODE (comparison)))
5219 return const0_rtx;
5220 break;
5221 #endif
5222 default:
5223 gcc_unreachable ();
5226 if (normalizep == 1)
5227 return const1_rtx;
5228 if (normalizep == -1)
5229 return constm1_rtx;
5230 return const_true_rtx;
5233 /* The code of COMPARISON may not match CODE if compare_from_rtx
5234 decided to swap its operands and reverse the original code.
5236 We know that compare_from_rtx returns either a CONST_INT or
5237 a new comparison code, so it is safe to just extract the
5238 code from COMPARISON. */
5239 code = GET_CODE (comparison);
5241 /* Get a reference to the target in the proper mode for this insn. */
5242 compare_mode = insn_data[(int) icode].operand[0].mode;
5243 subtarget = target;
5244 pred = insn_data[(int) icode].operand[0].predicate;
5245 if (optimize || ! (*pred) (subtarget, compare_mode))
5246 subtarget = gen_reg_rtx (compare_mode);
5248 pattern = GEN_FCN (icode) (subtarget);
5249 if (pattern)
5251 emit_insn (pattern);
5253 /* If we are converting to a wider mode, first convert to
5254 TARGET_MODE, then normalize. This produces better combining
5255 opportunities on machines that have a SIGN_EXTRACT when we are
5256 testing a single bit. This mostly benefits the 68k.
5258 If STORE_FLAG_VALUE does not have the sign bit set when
5259 interpreted in COMPARE_MODE, we can do this conversion as
5260 unsigned, which is usually more efficient. */
5261 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
5263 convert_move (target, subtarget,
5264 (GET_MODE_BITSIZE (compare_mode)
5265 <= HOST_BITS_PER_WIDE_INT)
5266 && 0 == (STORE_FLAG_VALUE
5267 & ((HOST_WIDE_INT) 1
5268 << (GET_MODE_BITSIZE (compare_mode) -1))));
5269 op0 = target;
5270 compare_mode = target_mode;
5272 else
5273 op0 = subtarget;
5275 /* If we want to keep subexpressions around, don't reuse our
5276 last target. */
5278 if (optimize)
5279 subtarget = 0;
5281 /* Now normalize to the proper value in COMPARE_MODE. Sometimes
5282 we don't have to do anything. */
5283 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
5285 /* STORE_FLAG_VALUE might be the most negative number, so write
5286 the comparison this way to avoid a compiler-time warning. */
5287 else if (- normalizep == STORE_FLAG_VALUE)
5288 op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
5290 /* We don't want to use STORE_FLAG_VALUE < 0 below since this
5291 makes it hard to use a value of just the sign bit due to
5292 ANSI integer constant typing rules. */
5293 else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
5294 && (STORE_FLAG_VALUE
5295 & ((HOST_WIDE_INT) 1
5296 << (GET_MODE_BITSIZE (compare_mode) - 1))))
5297 op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
5298 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
5299 subtarget, normalizep == 1);
5300 else
5302 gcc_assert (STORE_FLAG_VALUE & 1);
5304 op0 = expand_and (compare_mode, op0, const1_rtx, subtarget);
5305 if (normalizep == -1)
5306 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
5309 /* If we were converting to a smaller mode, do the
5310 conversion now. */
5311 if (target_mode != compare_mode)
5313 convert_move (target, op0, 0);
5314 return target;
5316 else
5317 return op0;
5321 delete_insns_since (last);
5323 /* If optimizing, use different pseudo registers for each insn, instead
5324 of reusing the same pseudo. This leads to better CSE, but slows
5325 down the compiler, since there are more pseudos */
5326 subtarget = (!optimize
5327 && (target_mode == mode)) ? target : NULL_RTX;
5329 /* If we reached here, we can't do this with a scc insn. However, there
5330 are some comparisons that can be done directly. For example, if
5331 this is an equality comparison of integers, we can try to exclusive-or
5332 (or subtract) the two operands and use a recursive call to try the
5333 comparison with zero. Don't do any of these cases if branches are
5334 very cheap. */
5336 if (BRANCH_COST > 0
5337 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
5338 && op1 != const0_rtx)
5340 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5341 OPTAB_WIDEN);
5343 if (tem == 0)
5344 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5345 OPTAB_WIDEN);
5346 if (tem != 0)
5347 tem = emit_store_flag (target, code, tem, const0_rtx,
5348 mode, unsignedp, normalizep);
5349 if (tem == 0)
5350 delete_insns_since (last);
5351 return tem;
5354 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5355 the constant zero. Reject all other comparisons at this point. Only
5356 do LE and GT if branches are expensive since they are expensive on
5357 2-operand machines. */
5359 if (BRANCH_COST == 0
5360 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
5361 || (code != EQ && code != NE
5362 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
5363 return 0;
5365 /* See what we need to return. We can only return a 1, -1, or the
5366 sign bit. */
5368 if (normalizep == 0)
5370 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5371 normalizep = STORE_FLAG_VALUE;
5373 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5374 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5375 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
5377 else
5378 return 0;
5381 /* Try to put the result of the comparison in the sign bit. Assume we can't
5382 do the necessary operation below. */
5384 tem = 0;
5386 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5387 the sign bit set. */
5389 if (code == LE)
5391 /* This is destructive, so SUBTARGET can't be OP0. */
5392 if (rtx_equal_p (subtarget, op0))
5393 subtarget = 0;
5395 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5396 OPTAB_WIDEN);
5397 if (tem)
5398 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5399 OPTAB_WIDEN);
5402 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5403 number of bits in the mode of OP0, minus one. */
5405 if (code == GT)
5407 if (rtx_equal_p (subtarget, op0))
5408 subtarget = 0;
5410 tem = expand_shift (RSHIFT_EXPR, mode, op0,
5411 size_int (GET_MODE_BITSIZE (mode) - 1),
5412 subtarget, 0);
5413 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5414 OPTAB_WIDEN);
5417 if (code == EQ || code == NE)
5419 /* For EQ or NE, one way to do the comparison is to apply an operation
5420 that converts the operand into a positive number if it is nonzero
5421 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5422 for NE we negate. This puts the result in the sign bit. Then we
5423 normalize with a shift, if needed.
5425 Two operations that can do the above actions are ABS and FFS, so try
5426 them. If that doesn't work, and MODE is smaller than a full word,
5427 we can use zero-extension to the wider mode (an unsigned conversion)
5428 as the operation. */
5430 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5431 that is compensated by the subsequent overflow when subtracting
5432 one / negating. */
5434 if (abs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5435 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5436 else if (ffs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5437 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5438 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5440 tem = convert_modes (word_mode, mode, op0, 1);
5441 mode = word_mode;
5444 if (tem != 0)
5446 if (code == EQ)
5447 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5448 0, OPTAB_WIDEN);
5449 else
5450 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5453 /* If we couldn't do it that way, for NE we can "or" the two's complement
5454 of the value with itself. For EQ, we take the one's complement of
5455 that "or", which is an extra insn, so we only handle EQ if branches
5456 are expensive. */
5458 if (tem == 0 && (code == NE || BRANCH_COST > 1))
5460 if (rtx_equal_p (subtarget, op0))
5461 subtarget = 0;
5463 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5464 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5465 OPTAB_WIDEN);
5467 if (tem && code == EQ)
5468 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5472 if (tem && normalizep)
5473 tem = expand_shift (RSHIFT_EXPR, mode, tem,
5474 size_int (GET_MODE_BITSIZE (mode) - 1),
5475 subtarget, normalizep == 1);
5477 if (tem)
5479 if (GET_MODE (tem) != target_mode)
5481 convert_move (target, tem, 0);
5482 tem = target;
5484 else if (!subtarget)
5486 emit_move_insn (target, tem);
5487 tem = target;
5490 else
5491 delete_insns_since (last);
5493 return tem;
5496 /* Like emit_store_flag, but always succeeds. */
5499 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5500 enum machine_mode mode, int unsignedp, int normalizep)
5502 rtx tem, label;
5504 /* First see if emit_store_flag can do the job. */
5505 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5506 if (tem != 0)
5507 return tem;
5509 if (normalizep == 0)
5510 normalizep = 1;
5512 /* If this failed, we have to do this with set/compare/jump/set code. */
5514 if (!REG_P (target)
5515 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5516 target = gen_reg_rtx (GET_MODE (target));
5518 emit_move_insn (target, const1_rtx);
5519 label = gen_label_rtx ();
5520 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
5521 NULL_RTX, label);
5523 emit_move_insn (target, const0_rtx);
5524 emit_label (label);
5526 return target;
5529 /* Perform possibly multi-word comparison and conditional jump to LABEL
5530 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE
5532 The algorithm is based on the code in expr.c:do_jump.
5534 Note that this does not perform a general comparison. Only
5535 variants generated within expmed.c are correctly handled, others
5536 could be handled if needed. */
5538 static void
5539 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, enum machine_mode mode,
5540 rtx label)
5542 /* If this mode is an integer too wide to compare properly,
5543 compare word by word. Rely on cse to optimize constant cases. */
5545 if (GET_MODE_CLASS (mode) == MODE_INT
5546 && ! can_compare_p (op, mode, ccp_jump))
5548 rtx label2 = gen_label_rtx ();
5550 switch (op)
5552 case LTU:
5553 do_jump_by_parts_greater_rtx (mode, 1, arg2, arg1, label2, label);
5554 break;
5556 case LEU:
5557 do_jump_by_parts_greater_rtx (mode, 1, arg1, arg2, label, label2);
5558 break;
5560 case LT:
5561 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label2, label);
5562 break;
5564 case GT:
5565 do_jump_by_parts_greater_rtx (mode, 0, arg1, arg2, label2, label);
5566 break;
5568 case GE:
5569 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label, label2);
5570 break;
5572 /* do_jump_by_parts_equality_rtx compares with zero. Luckily
5573 that's the only equality operations we do */
5574 case EQ:
5575 gcc_assert (arg2 == const0_rtx && mode == GET_MODE(arg1));
5576 do_jump_by_parts_equality_rtx (arg1, label2, label);
5577 break;
5579 case NE:
5580 gcc_assert (arg2 == const0_rtx && mode == GET_MODE(arg1));
5581 do_jump_by_parts_equality_rtx (arg1, label, label2);
5582 break;
5584 default:
5585 gcc_unreachable ();
5588 emit_label (label2);
5590 else
5591 emit_cmp_and_jump_insns (arg1, arg2, op, NULL_RTX, mode, 0, label);