2006-02-09 Kaz Kojima <kkojima@gcc.gnu.org>
[official-gcc.git] / gcc / expmed.c
bloba12c65d14440161d97a8e130a8d391561515591e
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 sdiv_cost[NUM_MACHINE_MODES];
107 static int udiv_cost[NUM_MACHINE_MODES];
108 static int mul_widen_cost[NUM_MACHINE_MODES];
109 static int mul_highpart_cost[NUM_MACHINE_MODES];
111 void
112 init_expmed (void)
114 struct
116 struct rtx_def reg; rtunion reg_fld[2];
117 struct rtx_def plus; rtunion plus_fld1;
118 struct rtx_def neg;
119 struct rtx_def mult; rtunion mult_fld1;
120 struct rtx_def sdiv; rtunion sdiv_fld1;
121 struct rtx_def udiv; rtunion udiv_fld1;
122 struct rtx_def zext;
123 struct rtx_def sdiv_32; rtunion sdiv_32_fld1;
124 struct rtx_def smod_32; rtunion smod_32_fld1;
125 struct rtx_def wide_mult; rtunion wide_mult_fld1;
126 struct rtx_def wide_lshr; rtunion wide_lshr_fld1;
127 struct rtx_def wide_trunc;
128 struct rtx_def shift; rtunion shift_fld1;
129 struct rtx_def shift_mult; rtunion shift_mult_fld1;
130 struct rtx_def shift_add; rtunion shift_add_fld1;
131 struct rtx_def shift_sub; rtunion shift_sub_fld1;
132 } all;
134 rtx pow2[MAX_BITS_PER_WORD];
135 rtx cint[MAX_BITS_PER_WORD];
136 int m, n;
137 enum machine_mode mode, wider_mode;
139 zero_cost = rtx_cost (const0_rtx, 0);
141 for (m = 1; m < MAX_BITS_PER_WORD; m++)
143 pow2[m] = GEN_INT ((HOST_WIDE_INT) 1 << m);
144 cint[m] = GEN_INT (m);
147 memset (&all, 0, sizeof all);
149 PUT_CODE (&all.reg, REG);
150 /* Avoid using hard regs in ways which may be unsupported. */
151 REGNO (&all.reg) = LAST_VIRTUAL_REGISTER + 1;
153 PUT_CODE (&all.plus, PLUS);
154 XEXP (&all.plus, 0) = &all.reg;
155 XEXP (&all.plus, 1) = &all.reg;
157 PUT_CODE (&all.neg, NEG);
158 XEXP (&all.neg, 0) = &all.reg;
160 PUT_CODE (&all.mult, MULT);
161 XEXP (&all.mult, 0) = &all.reg;
162 XEXP (&all.mult, 1) = &all.reg;
164 PUT_CODE (&all.sdiv, DIV);
165 XEXP (&all.sdiv, 0) = &all.reg;
166 XEXP (&all.sdiv, 1) = &all.reg;
168 PUT_CODE (&all.udiv, UDIV);
169 XEXP (&all.udiv, 0) = &all.reg;
170 XEXP (&all.udiv, 1) = &all.reg;
172 PUT_CODE (&all.sdiv_32, DIV);
173 XEXP (&all.sdiv_32, 0) = &all.reg;
174 XEXP (&all.sdiv_32, 1) = 32 < MAX_BITS_PER_WORD ? cint[32] : GEN_INT (32);
176 PUT_CODE (&all.smod_32, MOD);
177 XEXP (&all.smod_32, 0) = &all.reg;
178 XEXP (&all.smod_32, 1) = XEXP (&all.sdiv_32, 1);
180 PUT_CODE (&all.zext, ZERO_EXTEND);
181 XEXP (&all.zext, 0) = &all.reg;
183 PUT_CODE (&all.wide_mult, MULT);
184 XEXP (&all.wide_mult, 0) = &all.zext;
185 XEXP (&all.wide_mult, 1) = &all.zext;
187 PUT_CODE (&all.wide_lshr, LSHIFTRT);
188 XEXP (&all.wide_lshr, 0) = &all.wide_mult;
190 PUT_CODE (&all.wide_trunc, TRUNCATE);
191 XEXP (&all.wide_trunc, 0) = &all.wide_lshr;
193 PUT_CODE (&all.shift, ASHIFT);
194 XEXP (&all.shift, 0) = &all.reg;
196 PUT_CODE (&all.shift_mult, MULT);
197 XEXP (&all.shift_mult, 0) = &all.reg;
199 PUT_CODE (&all.shift_add, PLUS);
200 XEXP (&all.shift_add, 0) = &all.shift_mult;
201 XEXP (&all.shift_add, 1) = &all.reg;
203 PUT_CODE (&all.shift_sub, MINUS);
204 XEXP (&all.shift_sub, 0) = &all.shift_mult;
205 XEXP (&all.shift_sub, 1) = &all.reg;
207 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
208 mode != VOIDmode;
209 mode = GET_MODE_WIDER_MODE (mode))
211 PUT_MODE (&all.reg, mode);
212 PUT_MODE (&all.plus, mode);
213 PUT_MODE (&all.neg, mode);
214 PUT_MODE (&all.mult, mode);
215 PUT_MODE (&all.sdiv, mode);
216 PUT_MODE (&all.udiv, mode);
217 PUT_MODE (&all.sdiv_32, mode);
218 PUT_MODE (&all.smod_32, mode);
219 PUT_MODE (&all.wide_trunc, mode);
220 PUT_MODE (&all.shift, mode);
221 PUT_MODE (&all.shift_mult, mode);
222 PUT_MODE (&all.shift_add, mode);
223 PUT_MODE (&all.shift_sub, mode);
225 add_cost[mode] = rtx_cost (&all.plus, SET);
226 neg_cost[mode] = rtx_cost (&all.neg, SET);
227 mul_cost[mode] = rtx_cost (&all.mult, SET);
228 sdiv_cost[mode] = rtx_cost (&all.sdiv, SET);
229 udiv_cost[mode] = rtx_cost (&all.udiv, SET);
231 sdiv_pow2_cheap[mode] = (rtx_cost (&all.sdiv_32, SET)
232 <= 2 * add_cost[mode]);
233 smod_pow2_cheap[mode] = (rtx_cost (&all.smod_32, SET)
234 <= 4 * add_cost[mode]);
236 wider_mode = GET_MODE_WIDER_MODE (mode);
237 if (wider_mode != VOIDmode)
239 PUT_MODE (&all.zext, wider_mode);
240 PUT_MODE (&all.wide_mult, wider_mode);
241 PUT_MODE (&all.wide_lshr, wider_mode);
242 XEXP (&all.wide_lshr, 1) = GEN_INT (GET_MODE_BITSIZE (mode));
244 mul_widen_cost[wider_mode] = rtx_cost (&all.wide_mult, SET);
245 mul_highpart_cost[mode] = rtx_cost (&all.wide_trunc, SET);
248 shift_cost[mode][0] = 0;
249 shiftadd_cost[mode][0] = shiftsub_cost[mode][0] = add_cost[mode];
251 n = MIN (MAX_BITS_PER_WORD, GET_MODE_BITSIZE (mode));
252 for (m = 1; m < n; m++)
254 XEXP (&all.shift, 1) = cint[m];
255 XEXP (&all.shift_mult, 1) = pow2[m];
257 shift_cost[mode][m] = rtx_cost (&all.shift, SET);
258 shiftadd_cost[mode][m] = rtx_cost (&all.shift_add, SET);
259 shiftsub_cost[mode][m] = rtx_cost (&all.shift_sub, SET);
264 /* Return an rtx representing minus the value of X.
265 MODE is the intended mode of the result,
266 useful if X is a CONST_INT. */
269 negate_rtx (enum machine_mode mode, rtx x)
271 rtx result = simplify_unary_operation (NEG, mode, x, mode);
273 if (result == 0)
274 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
276 return result;
279 /* Report on the availability of insv/extv/extzv and the desired mode
280 of each of their operands. Returns MAX_MACHINE_MODE if HAVE_foo
281 is false; else the mode of the specified operand. If OPNO is -1,
282 all the caller cares about is whether the insn is available. */
283 enum machine_mode
284 mode_for_extraction (enum extraction_pattern pattern, int opno)
286 const struct insn_data *data;
288 switch (pattern)
290 case EP_insv:
291 if (HAVE_insv)
293 data = &insn_data[CODE_FOR_insv];
294 break;
296 return MAX_MACHINE_MODE;
298 case EP_extv:
299 if (HAVE_extv)
301 data = &insn_data[CODE_FOR_extv];
302 break;
304 return MAX_MACHINE_MODE;
306 case EP_extzv:
307 if (HAVE_extzv)
309 data = &insn_data[CODE_FOR_extzv];
310 break;
312 return MAX_MACHINE_MODE;
314 default:
315 gcc_unreachable ();
318 if (opno == -1)
319 return VOIDmode;
321 /* Everyone who uses this function used to follow it with
322 if (result == VOIDmode) result = word_mode; */
323 if (data->operand[opno].mode == VOIDmode)
324 return word_mode;
325 return data->operand[opno].mode;
329 /* Generate code to store value from rtx VALUE
330 into a bit-field within structure STR_RTX
331 containing BITSIZE bits starting at bit BITNUM.
332 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
333 ALIGN is the alignment that STR_RTX is known to have.
334 TOTAL_SIZE is the size of the structure in bytes, or -1 if varying. */
336 /* ??? Note that there are two different ideas here for how
337 to determine the size to count bits within, for a register.
338 One is BITS_PER_WORD, and the other is the size of operand 3
339 of the insv pattern.
341 If operand 3 of the insv pattern is VOIDmode, then we will use BITS_PER_WORD
342 else, we use the mode of operand 3. */
345 store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
346 unsigned HOST_WIDE_INT bitnum, enum machine_mode fieldmode,
347 rtx value)
349 unsigned int unit
350 = (MEM_P (str_rtx)) ? BITS_PER_UNIT : BITS_PER_WORD;
351 unsigned HOST_WIDE_INT offset, bitpos;
352 rtx op0 = str_rtx;
353 int byte_offset;
354 rtx orig_value;
356 enum machine_mode op_mode = mode_for_extraction (EP_insv, 3);
358 while (GET_CODE (op0) == SUBREG)
360 /* The following line once was done only if WORDS_BIG_ENDIAN,
361 but I think that is a mistake. WORDS_BIG_ENDIAN is
362 meaningful at a much higher level; when structures are copied
363 between memory and regs, the higher-numbered regs
364 always get higher addresses. */
365 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
366 op0 = SUBREG_REG (op0);
369 /* No action is needed if the target is a register and if the field
370 lies completely outside that register. This can occur if the source
371 code contains an out-of-bounds access to a small array. */
372 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
373 return value;
375 /* Use vec_set patterns for inserting parts of vectors whenever
376 available. */
377 if (VECTOR_MODE_P (GET_MODE (op0))
378 && !MEM_P (op0)
379 && (vec_set_optab->handlers[GET_MODE (op0)].insn_code
380 != CODE_FOR_nothing)
381 && fieldmode == GET_MODE_INNER (GET_MODE (op0))
382 && bitsize == GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
383 && !(bitnum % GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
385 enum machine_mode outermode = GET_MODE (op0);
386 enum machine_mode innermode = GET_MODE_INNER (outermode);
387 int icode = (int) vec_set_optab->handlers[outermode].insn_code;
388 int pos = bitnum / GET_MODE_BITSIZE (innermode);
389 rtx rtxpos = GEN_INT (pos);
390 rtx src = value;
391 rtx dest = op0;
392 rtx pat, seq;
393 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
394 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
395 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
397 start_sequence ();
399 if (! (*insn_data[icode].operand[1].predicate) (src, mode1))
400 src = copy_to_mode_reg (mode1, src);
402 if (! (*insn_data[icode].operand[2].predicate) (rtxpos, mode2))
403 rtxpos = copy_to_mode_reg (mode1, rtxpos);
405 /* We could handle this, but we should always be called with a pseudo
406 for our targets and all insns should take them as outputs. */
407 gcc_assert ((*insn_data[icode].operand[0].predicate) (dest, mode0)
408 && (*insn_data[icode].operand[1].predicate) (src, mode1)
409 && (*insn_data[icode].operand[2].predicate) (rtxpos, mode2));
410 pat = GEN_FCN (icode) (dest, src, rtxpos);
411 seq = get_insns ();
412 end_sequence ();
413 if (pat)
415 emit_insn (seq);
416 emit_insn (pat);
417 return dest;
421 /* If the target is a register, overwriting the entire object, or storing
422 a full-word or multi-word field can be done with just a SUBREG.
424 If the target is memory, storing any naturally aligned field can be
425 done with a simple store. For targets that support fast unaligned
426 memory, any naturally sized, unit aligned field can be done directly. */
428 offset = bitnum / unit;
429 bitpos = bitnum % unit;
430 byte_offset = (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
431 + (offset * UNITS_PER_WORD);
433 if (bitpos == 0
434 && bitsize == GET_MODE_BITSIZE (fieldmode)
435 && (!MEM_P (op0)
436 ? ((GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
437 || GET_MODE_SIZE (GET_MODE (op0)) == GET_MODE_SIZE (fieldmode))
438 && byte_offset % GET_MODE_SIZE (fieldmode) == 0)
439 : (! SLOW_UNALIGNED_ACCESS (fieldmode, MEM_ALIGN (op0))
440 || (offset * BITS_PER_UNIT % bitsize == 0
441 && MEM_ALIGN (op0) % GET_MODE_BITSIZE (fieldmode) == 0))))
443 if (MEM_P (op0))
444 op0 = adjust_address (op0, fieldmode, offset);
445 else if (GET_MODE (op0) != fieldmode)
446 op0 = simplify_gen_subreg (fieldmode, op0, GET_MODE (op0),
447 byte_offset);
448 emit_move_insn (op0, value);
449 return value;
452 /* Make sure we are playing with integral modes. Pun with subregs
453 if we aren't. This must come after the entire register case above,
454 since that case is valid for any mode. The following cases are only
455 valid for integral modes. */
457 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
458 if (imode != GET_MODE (op0))
460 if (MEM_P (op0))
461 op0 = adjust_address (op0, imode, 0);
462 else
464 gcc_assert (imode != BLKmode);
465 op0 = gen_lowpart (imode, op0);
470 /* We may be accessing data outside the field, which means
471 we can alias adjacent data. */
472 if (MEM_P (op0))
474 op0 = shallow_copy_rtx (op0);
475 set_mem_alias_set (op0, 0);
476 set_mem_expr (op0, 0);
479 /* If OP0 is a register, BITPOS must count within a word.
480 But as we have it, it counts within whatever size OP0 now has.
481 On a bigendian machine, these are not the same, so convert. */
482 if (BYTES_BIG_ENDIAN
483 && !MEM_P (op0)
484 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
485 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
487 /* Storing an lsb-aligned field in a register
488 can be done with a movestrict instruction. */
490 if (!MEM_P (op0)
491 && (BYTES_BIG_ENDIAN ? bitpos + bitsize == unit : bitpos == 0)
492 && bitsize == GET_MODE_BITSIZE (fieldmode)
493 && (movstrict_optab->handlers[fieldmode].insn_code
494 != CODE_FOR_nothing))
496 int icode = movstrict_optab->handlers[fieldmode].insn_code;
498 /* Get appropriate low part of the value being stored. */
499 if (GET_CODE (value) == CONST_INT || REG_P (value))
500 value = gen_lowpart (fieldmode, value);
501 else if (!(GET_CODE (value) == SYMBOL_REF
502 || GET_CODE (value) == LABEL_REF
503 || GET_CODE (value) == CONST))
504 value = convert_to_mode (fieldmode, value, 0);
506 if (! (*insn_data[icode].operand[1].predicate) (value, fieldmode))
507 value = copy_to_mode_reg (fieldmode, value);
509 if (GET_CODE (op0) == SUBREG)
511 /* Else we've got some float mode source being extracted into
512 a different float mode destination -- this combination of
513 subregs results in Severe Tire Damage. */
514 gcc_assert (GET_MODE (SUBREG_REG (op0)) == fieldmode
515 || GET_MODE_CLASS (fieldmode) == MODE_INT
516 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
517 op0 = SUBREG_REG (op0);
520 emit_insn (GEN_FCN (icode)
521 (gen_rtx_SUBREG (fieldmode, op0,
522 (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
523 + (offset * UNITS_PER_WORD)),
524 value));
526 return value;
529 /* Handle fields bigger than a word. */
531 if (bitsize > BITS_PER_WORD)
533 /* Here we transfer the words of the field
534 in the order least significant first.
535 This is because the most significant word is the one which may
536 be less than full.
537 However, only do that if the value is not BLKmode. */
539 unsigned int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
540 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
541 unsigned int i;
543 /* This is the mode we must force value to, so that there will be enough
544 subwords to extract. Note that fieldmode will often (always?) be
545 VOIDmode, because that is what store_field uses to indicate that this
546 is a bit field, but passing VOIDmode to operand_subword_force
547 is not allowed. */
548 fieldmode = GET_MODE (value);
549 if (fieldmode == VOIDmode)
550 fieldmode = smallest_mode_for_size (nwords * BITS_PER_WORD, MODE_INT);
552 for (i = 0; i < nwords; i++)
554 /* If I is 0, use the low-order word in both field and target;
555 if I is 1, use the next to lowest word; and so on. */
556 unsigned int wordnum = (backwards ? nwords - i - 1 : i);
557 unsigned int bit_offset = (backwards
558 ? MAX ((int) bitsize - ((int) i + 1)
559 * BITS_PER_WORD,
561 : (int) i * BITS_PER_WORD);
563 store_bit_field (op0, MIN (BITS_PER_WORD,
564 bitsize - i * BITS_PER_WORD),
565 bitnum + bit_offset, word_mode,
566 operand_subword_force (value, wordnum, fieldmode));
568 return value;
571 /* From here on we can assume that the field to be stored in is
572 a full-word (whatever type that is), since it is shorter than a word. */
574 /* OFFSET is the number of words or bytes (UNIT says which)
575 from STR_RTX to the first word or byte containing part of the field. */
577 if (!MEM_P (op0))
579 if (offset != 0
580 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
582 if (!REG_P (op0))
584 /* Since this is a destination (lvalue), we can't copy
585 it to a pseudo. We can remove a SUBREG that does not
586 change the size of the operand. Such a SUBREG may
587 have been added above. */
588 gcc_assert (GET_CODE (op0) == SUBREG
589 && (GET_MODE_SIZE (GET_MODE (op0))
590 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))));
591 op0 = SUBREG_REG (op0);
593 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
594 op0, (offset * UNITS_PER_WORD));
596 offset = 0;
599 /* If VALUE has a floating-point or complex mode, access it as an
600 integer of the corresponding size. This can occur on a machine
601 with 64 bit registers that uses SFmode for float. It can also
602 occur for unaligned float or complex fields. */
603 orig_value = value;
604 if (GET_MODE (value) != VOIDmode
605 && GET_MODE_CLASS (GET_MODE (value)) != MODE_INT
606 && GET_MODE_CLASS (GET_MODE (value)) != MODE_PARTIAL_INT)
608 value = gen_reg_rtx (int_mode_for_mode (GET_MODE (value)));
609 emit_move_insn (gen_lowpart (GET_MODE (orig_value), value), orig_value);
612 /* Now OFFSET is nonzero only if OP0 is memory
613 and is therefore always measured in bytes. */
615 if (HAVE_insv
616 && GET_MODE (value) != BLKmode
617 && bitsize > 0
618 && GET_MODE_BITSIZE (op_mode) >= bitsize
619 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
620 && (bitsize + bitpos > GET_MODE_BITSIZE (op_mode))))
622 int xbitpos = bitpos;
623 rtx value1;
624 rtx xop0 = op0;
625 rtx last = get_last_insn ();
626 rtx pat;
627 enum machine_mode maxmode = mode_for_extraction (EP_insv, 3);
628 int save_volatile_ok = volatile_ok;
630 volatile_ok = 1;
632 /* If this machine's insv can only insert into a register, copy OP0
633 into a register and save it back later. */
634 if (MEM_P (op0)
635 && ! ((*insn_data[(int) CODE_FOR_insv].operand[0].predicate)
636 (op0, VOIDmode)))
638 rtx tempreg;
639 enum machine_mode bestmode;
641 /* Get the mode to use for inserting into this field. If OP0 is
642 BLKmode, get the smallest mode consistent with the alignment. If
643 OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
644 mode. Otherwise, use the smallest mode containing the field. */
646 if (GET_MODE (op0) == BLKmode
647 || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
648 bestmode
649 = get_best_mode (bitsize, bitnum, MEM_ALIGN (op0), maxmode,
650 MEM_VOLATILE_P (op0));
651 else
652 bestmode = GET_MODE (op0);
654 if (bestmode == VOIDmode
655 || GET_MODE_SIZE (bestmode) < GET_MODE_SIZE (fieldmode)
656 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (op0))
657 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (op0)))
658 goto insv_loses;
660 /* Adjust address to point to the containing unit of that mode.
661 Compute offset as multiple of this unit, counting in bytes. */
662 unit = GET_MODE_BITSIZE (bestmode);
663 offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
664 bitpos = bitnum % unit;
665 op0 = adjust_address (op0, bestmode, offset);
667 /* Fetch that unit, store the bitfield in it, then store
668 the unit. */
669 tempreg = copy_to_reg (op0);
670 store_bit_field (tempreg, bitsize, bitpos, fieldmode, orig_value);
671 emit_move_insn (op0, tempreg);
672 return value;
674 volatile_ok = save_volatile_ok;
676 /* Add OFFSET into OP0's address. */
677 if (MEM_P (xop0))
678 xop0 = adjust_address (xop0, byte_mode, offset);
680 /* If xop0 is a register, we need it in MAXMODE
681 to make it acceptable to the format of insv. */
682 if (GET_CODE (xop0) == SUBREG)
683 /* We can't just change the mode, because this might clobber op0,
684 and we will need the original value of op0 if insv fails. */
685 xop0 = gen_rtx_SUBREG (maxmode, SUBREG_REG (xop0), SUBREG_BYTE (xop0));
686 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
687 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
689 /* On big-endian machines, we count bits from the most significant.
690 If the bit field insn does not, we must invert. */
692 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
693 xbitpos = unit - bitsize - xbitpos;
695 /* We have been counting XBITPOS within UNIT.
696 Count instead within the size of the register. */
697 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
698 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
700 unit = GET_MODE_BITSIZE (maxmode);
702 /* Convert VALUE to maxmode (which insv insn wants) in VALUE1. */
703 value1 = value;
704 if (GET_MODE (value) != maxmode)
706 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
708 /* Optimization: Don't bother really extending VALUE
709 if it has all the bits we will actually use. However,
710 if we must narrow it, be sure we do it correctly. */
712 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
714 rtx tmp;
716 tmp = simplify_subreg (maxmode, value1, GET_MODE (value), 0);
717 if (! tmp)
718 tmp = simplify_gen_subreg (maxmode,
719 force_reg (GET_MODE (value),
720 value1),
721 GET_MODE (value), 0);
722 value1 = tmp;
724 else
725 value1 = gen_lowpart (maxmode, value1);
727 else if (GET_CODE (value) == CONST_INT)
728 value1 = gen_int_mode (INTVAL (value), maxmode);
729 else
730 /* Parse phase is supposed to make VALUE's data type
731 match that of the component reference, which is a type
732 at least as wide as the field; so VALUE should have
733 a mode that corresponds to that type. */
734 gcc_assert (CONSTANT_P (value));
737 /* If this machine's insv insists on a register,
738 get VALUE1 into a register. */
739 if (! ((*insn_data[(int) CODE_FOR_insv].operand[3].predicate)
740 (value1, maxmode)))
741 value1 = force_reg (maxmode, value1);
743 pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
744 if (pat)
745 emit_insn (pat);
746 else
748 delete_insns_since (last);
749 store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
752 else
753 insv_loses:
754 /* Insv is not available; store using shifts and boolean ops. */
755 store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
756 return value;
759 /* Use shifts and boolean operations to store VALUE
760 into a bit field of width BITSIZE
761 in a memory location specified by OP0 except offset by OFFSET bytes.
762 (OFFSET must be 0 if OP0 is a register.)
763 The field starts at position BITPOS within the byte.
764 (If OP0 is a register, it may be a full word or a narrower mode,
765 but BITPOS still counts within a full word,
766 which is significant on bigendian machines.) */
768 static void
769 store_fixed_bit_field (rtx op0, unsigned HOST_WIDE_INT offset,
770 unsigned HOST_WIDE_INT bitsize,
771 unsigned HOST_WIDE_INT bitpos, rtx value)
773 enum machine_mode mode;
774 unsigned int total_bits = BITS_PER_WORD;
775 rtx subtarget, temp;
776 int all_zero = 0;
777 int all_one = 0;
779 /* There is a case not handled here:
780 a structure with a known alignment of just a halfword
781 and a field split across two aligned halfwords within the structure.
782 Or likewise a structure with a known alignment of just a byte
783 and a field split across two bytes.
784 Such cases are not supposed to be able to occur. */
786 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
788 gcc_assert (!offset);
789 /* Special treatment for a bit field split across two registers. */
790 if (bitsize + bitpos > BITS_PER_WORD)
792 store_split_bit_field (op0, bitsize, bitpos, value);
793 return;
796 else
798 /* Get the proper mode to use for this field. We want a mode that
799 includes the entire field. If such a mode would be larger than
800 a word, we won't be doing the extraction the normal way.
801 We don't want a mode bigger than the destination. */
803 mode = GET_MODE (op0);
804 if (GET_MODE_BITSIZE (mode) == 0
805 || GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (word_mode))
806 mode = word_mode;
807 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
808 MEM_ALIGN (op0), mode, MEM_VOLATILE_P (op0));
810 if (mode == VOIDmode)
812 /* The only way this should occur is if the field spans word
813 boundaries. */
814 store_split_bit_field (op0, bitsize, bitpos + offset * BITS_PER_UNIT,
815 value);
816 return;
819 total_bits = GET_MODE_BITSIZE (mode);
821 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
822 be in the range 0 to total_bits-1, and put any excess bytes in
823 OFFSET. */
824 if (bitpos >= total_bits)
826 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
827 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
828 * BITS_PER_UNIT);
831 /* Get ref to an aligned byte, halfword, or word containing the field.
832 Adjust BITPOS to be position within a word,
833 and OFFSET to be the offset of that word.
834 Then alter OP0 to refer to that word. */
835 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
836 offset -= (offset % (total_bits / BITS_PER_UNIT));
837 op0 = adjust_address (op0, mode, offset);
840 mode = GET_MODE (op0);
842 /* Now MODE is either some integral mode for a MEM as OP0,
843 or is a full-word for a REG as OP0. TOTAL_BITS corresponds.
844 The bit field is contained entirely within OP0.
845 BITPOS is the starting bit number within OP0.
846 (OP0's mode may actually be narrower than MODE.) */
848 if (BYTES_BIG_ENDIAN)
849 /* BITPOS is the distance between our msb
850 and that of the containing datum.
851 Convert it to the distance from the lsb. */
852 bitpos = total_bits - bitsize - bitpos;
854 /* Now BITPOS is always the distance between our lsb
855 and that of OP0. */
857 /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
858 we must first convert its mode to MODE. */
860 if (GET_CODE (value) == CONST_INT)
862 HOST_WIDE_INT v = INTVAL (value);
864 if (bitsize < HOST_BITS_PER_WIDE_INT)
865 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
867 if (v == 0)
868 all_zero = 1;
869 else if ((bitsize < HOST_BITS_PER_WIDE_INT
870 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
871 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
872 all_one = 1;
874 value = lshift_value (mode, value, bitpos, bitsize);
876 else
878 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
879 && bitpos + bitsize != GET_MODE_BITSIZE (mode));
881 if (GET_MODE (value) != mode)
883 if ((REG_P (value) || GET_CODE (value) == SUBREG)
884 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
885 value = gen_lowpart (mode, value);
886 else
887 value = convert_to_mode (mode, value, 1);
890 if (must_and)
891 value = expand_binop (mode, and_optab, value,
892 mask_rtx (mode, 0, bitsize, 0),
893 NULL_RTX, 1, OPTAB_LIB_WIDEN);
894 if (bitpos > 0)
895 value = expand_shift (LSHIFT_EXPR, mode, value,
896 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
899 /* Now clear the chosen bits in OP0,
900 except that if VALUE is -1 we need not bother. */
902 subtarget = op0;
904 if (! all_one)
906 temp = expand_binop (mode, and_optab, op0,
907 mask_rtx (mode, bitpos, bitsize, 1),
908 subtarget, 1, OPTAB_LIB_WIDEN);
909 subtarget = temp;
911 else
912 temp = op0;
914 /* Now logical-or VALUE into OP0, unless it is zero. */
916 if (! all_zero)
917 temp = expand_binop (mode, ior_optab, temp, value,
918 subtarget, 1, OPTAB_LIB_WIDEN);
919 if (op0 != temp)
920 emit_move_insn (op0, temp);
923 /* Store a bit field that is split across multiple accessible memory objects.
925 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
926 BITSIZE is the field width; BITPOS the position of its first bit
927 (within the word).
928 VALUE is the value to store.
930 This does not yet handle fields wider than BITS_PER_WORD. */
932 static void
933 store_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
934 unsigned HOST_WIDE_INT bitpos, rtx value)
936 unsigned int unit;
937 unsigned int bitsdone = 0;
939 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
940 much at a time. */
941 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
942 unit = BITS_PER_WORD;
943 else
944 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
946 /* If VALUE is a constant other than a CONST_INT, get it into a register in
947 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
948 that VALUE might be a floating-point constant. */
949 if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
951 rtx word = gen_lowpart_common (word_mode, value);
953 if (word && (value != word))
954 value = word;
955 else
956 value = gen_lowpart_common (word_mode,
957 force_reg (GET_MODE (value) != VOIDmode
958 ? GET_MODE (value)
959 : word_mode, value));
962 while (bitsdone < bitsize)
964 unsigned HOST_WIDE_INT thissize;
965 rtx part, word;
966 unsigned HOST_WIDE_INT thispos;
967 unsigned HOST_WIDE_INT offset;
969 offset = (bitpos + bitsdone) / unit;
970 thispos = (bitpos + bitsdone) % unit;
972 /* THISSIZE must not overrun a word boundary. Otherwise,
973 store_fixed_bit_field will call us again, and we will mutually
974 recurse forever. */
975 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
976 thissize = MIN (thissize, unit - thispos);
978 if (BYTES_BIG_ENDIAN)
980 int total_bits;
982 /* We must do an endian conversion exactly the same way as it is
983 done in extract_bit_field, so that the two calls to
984 extract_fixed_bit_field will have comparable arguments. */
985 if (!MEM_P (value) || GET_MODE (value) == BLKmode)
986 total_bits = BITS_PER_WORD;
987 else
988 total_bits = GET_MODE_BITSIZE (GET_MODE (value));
990 /* Fetch successively less significant portions. */
991 if (GET_CODE (value) == CONST_INT)
992 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
993 >> (bitsize - bitsdone - thissize))
994 & (((HOST_WIDE_INT) 1 << thissize) - 1));
995 else
996 /* The args are chosen so that the last part includes the
997 lsb. Give extract_bit_field the value it needs (with
998 endianness compensation) to fetch the piece we want. */
999 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
1000 total_bits - bitsize + bitsdone,
1001 NULL_RTX, 1);
1003 else
1005 /* Fetch successively more significant portions. */
1006 if (GET_CODE (value) == CONST_INT)
1007 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1008 >> bitsdone)
1009 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1010 else
1011 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
1012 bitsdone, NULL_RTX, 1);
1015 /* If OP0 is a register, then handle OFFSET here.
1017 When handling multiword bitfields, extract_bit_field may pass
1018 down a word_mode SUBREG of a larger REG for a bitfield that actually
1019 crosses a word boundary. Thus, for a SUBREG, we must find
1020 the current word starting from the base register. */
1021 if (GET_CODE (op0) == SUBREG)
1023 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
1024 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1025 GET_MODE (SUBREG_REG (op0)));
1026 offset = 0;
1028 else if (REG_P (op0))
1030 word = operand_subword_force (op0, offset, GET_MODE (op0));
1031 offset = 0;
1033 else
1034 word = op0;
1036 /* OFFSET is in UNITs, and UNIT is in bits.
1037 store_fixed_bit_field wants offset in bytes. */
1038 store_fixed_bit_field (word, offset * unit / BITS_PER_UNIT, thissize,
1039 thispos, part);
1040 bitsdone += thissize;
1044 /* Generate code to extract a byte-field from STR_RTX
1045 containing BITSIZE bits, starting at BITNUM,
1046 and put it in TARGET if possible (if TARGET is nonzero).
1047 Regardless of TARGET, we return the rtx for where the value is placed.
1049 STR_RTX is the structure containing the byte (a REG or MEM).
1050 UNSIGNEDP is nonzero if this is an unsigned bit field.
1051 MODE is the natural mode of the field value once extracted.
1052 TMODE is the mode the caller would like the value to have;
1053 but the value may be returned with type MODE instead.
1055 TOTAL_SIZE is the size in bytes of the containing structure,
1056 or -1 if varying.
1058 If a TARGET is specified and we can store in it at no extra cost,
1059 we do so, and return TARGET.
1060 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1061 if they are equally easy. */
1064 extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1065 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1066 enum machine_mode mode, enum machine_mode tmode)
1068 unsigned int unit
1069 = (MEM_P (str_rtx)) ? BITS_PER_UNIT : BITS_PER_WORD;
1070 unsigned HOST_WIDE_INT offset, bitpos;
1071 rtx op0 = str_rtx;
1072 rtx spec_target = target;
1073 rtx spec_target_subreg = 0;
1074 enum machine_mode int_mode;
1075 enum machine_mode extv_mode = mode_for_extraction (EP_extv, 0);
1076 enum machine_mode extzv_mode = mode_for_extraction (EP_extzv, 0);
1077 enum machine_mode mode1;
1078 int byte_offset;
1080 if (tmode == VOIDmode)
1081 tmode = mode;
1083 while (GET_CODE (op0) == SUBREG)
1085 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1086 op0 = SUBREG_REG (op0);
1089 /* If we have an out-of-bounds access to a register, just return an
1090 uninitialized register of the required mode. This can occur if the
1091 source code contains an out-of-bounds access to a small array. */
1092 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
1093 return gen_reg_rtx (tmode);
1095 if (REG_P (op0)
1096 && mode == GET_MODE (op0)
1097 && bitnum == 0
1098 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
1100 /* We're trying to extract a full register from itself. */
1101 return op0;
1104 /* Use vec_extract patterns for extracting parts of vectors whenever
1105 available. */
1106 if (VECTOR_MODE_P (GET_MODE (op0))
1107 && !MEM_P (op0)
1108 && (vec_extract_optab->handlers[GET_MODE (op0)].insn_code
1109 != CODE_FOR_nothing)
1110 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
1111 == bitnum / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
1113 enum machine_mode outermode = GET_MODE (op0);
1114 enum machine_mode innermode = GET_MODE_INNER (outermode);
1115 int icode = (int) vec_extract_optab->handlers[outermode].insn_code;
1116 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1117 rtx rtxpos = GEN_INT (pos);
1118 rtx src = op0;
1119 rtx dest = NULL, pat, seq;
1120 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
1121 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
1122 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
1124 if (innermode == tmode || innermode == mode)
1125 dest = target;
1127 if (!dest)
1128 dest = gen_reg_rtx (innermode);
1130 start_sequence ();
1132 if (! (*insn_data[icode].operand[0].predicate) (dest, mode0))
1133 dest = copy_to_mode_reg (mode0, dest);
1135 if (! (*insn_data[icode].operand[1].predicate) (src, mode1))
1136 src = copy_to_mode_reg (mode1, src);
1138 if (! (*insn_data[icode].operand[2].predicate) (rtxpos, mode2))
1139 rtxpos = copy_to_mode_reg (mode1, rtxpos);
1141 /* We could handle this, but we should always be called with a pseudo
1142 for our targets and all insns should take them as outputs. */
1143 gcc_assert ((*insn_data[icode].operand[0].predicate) (dest, mode0)
1144 && (*insn_data[icode].operand[1].predicate) (src, mode1)
1145 && (*insn_data[icode].operand[2].predicate) (rtxpos, mode2));
1147 pat = GEN_FCN (icode) (dest, src, rtxpos);
1148 seq = get_insns ();
1149 end_sequence ();
1150 if (pat)
1152 emit_insn (seq);
1153 emit_insn (pat);
1154 return dest;
1158 /* Make sure we are playing with integral modes. Pun with subregs
1159 if we aren't. */
1161 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
1162 if (imode != GET_MODE (op0))
1164 if (MEM_P (op0))
1165 op0 = adjust_address (op0, imode, 0);
1166 else
1168 gcc_assert (imode != BLKmode);
1169 op0 = gen_lowpart (imode, op0);
1171 /* If we got a SUBREG, force it into a register since we
1172 aren't going to be able to do another SUBREG on it. */
1173 if (GET_CODE (op0) == SUBREG)
1174 op0 = force_reg (imode, op0);
1179 /* We may be accessing data outside the field, which means
1180 we can alias adjacent data. */
1181 if (MEM_P (op0))
1183 op0 = shallow_copy_rtx (op0);
1184 set_mem_alias_set (op0, 0);
1185 set_mem_expr (op0, 0);
1188 /* Extraction of a full-word or multi-word value from a structure
1189 in a register or aligned memory can be done with just a SUBREG.
1190 A subword value in the least significant part of a register
1191 can also be extracted with a SUBREG. For this, we need the
1192 byte offset of the value in op0. */
1194 bitpos = bitnum % unit;
1195 offset = bitnum / unit;
1196 byte_offset = bitpos / BITS_PER_UNIT + offset * UNITS_PER_WORD;
1198 /* If OP0 is a register, BITPOS must count within a word.
1199 But as we have it, it counts within whatever size OP0 now has.
1200 On a bigendian machine, these are not the same, so convert. */
1201 if (BYTES_BIG_ENDIAN
1202 && !MEM_P (op0)
1203 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
1204 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
1206 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1207 If that's wrong, the solution is to test for it and set TARGET to 0
1208 if needed. */
1210 /* Only scalar integer modes can be converted via subregs. There is an
1211 additional problem for FP modes here in that they can have a precision
1212 which is different from the size. mode_for_size uses precision, but
1213 we want a mode based on the size, so we must avoid calling it for FP
1214 modes. */
1215 mode1 = (SCALAR_INT_MODE_P (tmode)
1216 ? mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0)
1217 : mode);
1219 if (((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
1220 && bitpos % BITS_PER_WORD == 0)
1221 || (mode1 != BLKmode
1222 /* ??? The big endian test here is wrong. This is correct
1223 if the value is in a register, and if mode_for_size is not
1224 the same mode as op0. This causes us to get unnecessarily
1225 inefficient code from the Thumb port when -mbig-endian. */
1226 && (BYTES_BIG_ENDIAN
1227 ? bitpos + bitsize == BITS_PER_WORD
1228 : bitpos == 0)))
1229 && ((!MEM_P (op0)
1230 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1231 GET_MODE_BITSIZE (GET_MODE (op0)))
1232 && GET_MODE_SIZE (mode1) != 0
1233 && byte_offset % GET_MODE_SIZE (mode1) == 0)
1234 || (MEM_P (op0)
1235 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
1236 || (offset * BITS_PER_UNIT % bitsize == 0
1237 && MEM_ALIGN (op0) % bitsize == 0)))))
1239 if (mode1 != GET_MODE (op0))
1241 if (MEM_P (op0))
1242 op0 = adjust_address (op0, mode1, offset);
1243 else
1245 rtx sub = simplify_gen_subreg (mode1, op0, GET_MODE (op0),
1246 byte_offset);
1247 if (sub == NULL)
1248 goto no_subreg_mode_swap;
1249 op0 = sub;
1252 if (mode1 != mode)
1253 return convert_to_mode (tmode, op0, unsignedp);
1254 return op0;
1256 no_subreg_mode_swap:
1258 /* Handle fields bigger than a word. */
1260 if (bitsize > BITS_PER_WORD)
1262 /* Here we transfer the words of the field
1263 in the order least significant first.
1264 This is because the most significant word is the one which may
1265 be less than full. */
1267 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1268 unsigned int i;
1270 if (target == 0 || !REG_P (target))
1271 target = gen_reg_rtx (mode);
1273 /* Indicate for flow that the entire target reg is being set. */
1274 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
1276 for (i = 0; i < nwords; i++)
1278 /* If I is 0, use the low-order word in both field and target;
1279 if I is 1, use the next to lowest word; and so on. */
1280 /* Word number in TARGET to use. */
1281 unsigned int wordnum
1282 = (WORDS_BIG_ENDIAN
1283 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1284 : i);
1285 /* Offset from start of field in OP0. */
1286 unsigned int bit_offset = (WORDS_BIG_ENDIAN
1287 ? MAX (0, ((int) bitsize - ((int) i + 1)
1288 * (int) BITS_PER_WORD))
1289 : (int) i * BITS_PER_WORD);
1290 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1291 rtx result_part
1292 = extract_bit_field (op0, MIN (BITS_PER_WORD,
1293 bitsize - i * BITS_PER_WORD),
1294 bitnum + bit_offset, 1, target_part, mode,
1295 word_mode);
1297 gcc_assert (target_part);
1299 if (result_part != target_part)
1300 emit_move_insn (target_part, result_part);
1303 if (unsignedp)
1305 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1306 need to be zero'd out. */
1307 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1309 unsigned int i, total_words;
1311 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1312 for (i = nwords; i < total_words; i++)
1313 emit_move_insn
1314 (operand_subword (target,
1315 WORDS_BIG_ENDIAN ? total_words - i - 1 : i,
1316 1, VOIDmode),
1317 const0_rtx);
1319 return target;
1322 /* Signed bit field: sign-extend with two arithmetic shifts. */
1323 target = expand_shift (LSHIFT_EXPR, mode, target,
1324 build_int_cst (NULL_TREE,
1325 GET_MODE_BITSIZE (mode) - bitsize),
1326 NULL_RTX, 0);
1327 return expand_shift (RSHIFT_EXPR, mode, target,
1328 build_int_cst (NULL_TREE,
1329 GET_MODE_BITSIZE (mode) - bitsize),
1330 NULL_RTX, 0);
1333 /* From here on we know the desired field is smaller than a word. */
1335 /* Check if there is a correspondingly-sized integer field, so we can
1336 safely extract it as one size of integer, if necessary; then
1337 truncate or extend to the size that is wanted; then use SUBREGs or
1338 convert_to_mode to get one of the modes we really wanted. */
1340 int_mode = int_mode_for_mode (tmode);
1341 if (int_mode == BLKmode)
1342 int_mode = int_mode_for_mode (mode);
1343 /* Should probably push op0 out to memory and then do a load. */
1344 gcc_assert (int_mode != BLKmode);
1346 /* OFFSET is the number of words or bytes (UNIT says which)
1347 from STR_RTX to the first word or byte containing part of the field. */
1348 if (!MEM_P (op0))
1350 if (offset != 0
1351 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1353 if (!REG_P (op0))
1354 op0 = copy_to_reg (op0);
1355 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
1356 op0, (offset * UNITS_PER_WORD));
1358 offset = 0;
1361 /* Now OFFSET is nonzero only for memory operands. */
1363 if (unsignedp)
1365 if (HAVE_extzv
1366 && bitsize > 0
1367 && GET_MODE_BITSIZE (extzv_mode) >= bitsize
1368 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
1369 && (bitsize + bitpos > GET_MODE_BITSIZE (extzv_mode))))
1371 unsigned HOST_WIDE_INT xbitpos = bitpos, xoffset = offset;
1372 rtx bitsize_rtx, bitpos_rtx;
1373 rtx last = get_last_insn ();
1374 rtx xop0 = op0;
1375 rtx xtarget = target;
1376 rtx xspec_target = spec_target;
1377 rtx xspec_target_subreg = spec_target_subreg;
1378 rtx pat;
1379 enum machine_mode maxmode = mode_for_extraction (EP_extzv, 0);
1381 if (MEM_P (xop0))
1383 int save_volatile_ok = volatile_ok;
1384 volatile_ok = 1;
1386 /* Is the memory operand acceptable? */
1387 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[1].predicate)
1388 (xop0, GET_MODE (xop0))))
1390 /* No, load into a reg and extract from there. */
1391 enum machine_mode bestmode;
1393 /* Get the mode to use for inserting into this field. If
1394 OP0 is BLKmode, get the smallest mode consistent with the
1395 alignment. If OP0 is a non-BLKmode object that is no
1396 wider than MAXMODE, use its mode. Otherwise, use the
1397 smallest mode containing the field. */
1399 if (GET_MODE (xop0) == BLKmode
1400 || (GET_MODE_SIZE (GET_MODE (op0))
1401 > GET_MODE_SIZE (maxmode)))
1402 bestmode = get_best_mode (bitsize, bitnum,
1403 MEM_ALIGN (xop0), maxmode,
1404 MEM_VOLATILE_P (xop0));
1405 else
1406 bestmode = GET_MODE (xop0);
1408 if (bestmode == VOIDmode
1409 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (xop0))
1410 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (xop0)))
1411 goto extzv_loses;
1413 /* Compute offset as multiple of this unit,
1414 counting in bytes. */
1415 unit = GET_MODE_BITSIZE (bestmode);
1416 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1417 xbitpos = bitnum % unit;
1418 xop0 = adjust_address (xop0, bestmode, xoffset);
1420 /* Make sure register is big enough for the whole field. */
1421 if (xoffset * BITS_PER_UNIT + unit
1422 < offset * BITS_PER_UNIT + bitsize)
1423 goto extzv_loses;
1425 /* Fetch it to a register in that size. */
1426 xop0 = force_reg (bestmode, xop0);
1428 /* XBITPOS counts within UNIT, which is what is expected. */
1430 else
1431 /* Get ref to first byte containing part of the field. */
1432 xop0 = adjust_address (xop0, byte_mode, xoffset);
1434 volatile_ok = save_volatile_ok;
1437 /* If op0 is a register, we need it in MAXMODE (which is usually
1438 SImode). to make it acceptable to the format of extzv. */
1439 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1440 goto extzv_loses;
1441 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
1442 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1444 /* On big-endian machines, we count bits from the most significant.
1445 If the bit field insn does not, we must invert. */
1446 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1447 xbitpos = unit - bitsize - xbitpos;
1449 /* Now convert from counting within UNIT to counting in MAXMODE. */
1450 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
1451 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
1453 unit = GET_MODE_BITSIZE (maxmode);
1455 if (xtarget == 0)
1456 xtarget = xspec_target = gen_reg_rtx (tmode);
1458 if (GET_MODE (xtarget) != maxmode)
1460 if (REG_P (xtarget))
1462 int wider = (GET_MODE_SIZE (maxmode)
1463 > GET_MODE_SIZE (GET_MODE (xtarget)));
1464 xtarget = gen_lowpart (maxmode, xtarget);
1465 if (wider)
1466 xspec_target_subreg = xtarget;
1468 else
1469 xtarget = gen_reg_rtx (maxmode);
1472 /* If this machine's extzv insists on a register target,
1473 make sure we have one. */
1474 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[0].predicate)
1475 (xtarget, maxmode)))
1476 xtarget = gen_reg_rtx (maxmode);
1478 bitsize_rtx = GEN_INT (bitsize);
1479 bitpos_rtx = GEN_INT (xbitpos);
1481 pat = gen_extzv (xtarget, xop0, bitsize_rtx, bitpos_rtx);
1482 if (pat)
1484 emit_insn (pat);
1485 target = xtarget;
1486 spec_target = xspec_target;
1487 spec_target_subreg = xspec_target_subreg;
1489 else
1491 delete_insns_since (last);
1492 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1493 bitpos, target, 1);
1496 else
1497 extzv_loses:
1498 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1499 bitpos, target, 1);
1501 else
1503 if (HAVE_extv
1504 && bitsize > 0
1505 && GET_MODE_BITSIZE (extv_mode) >= bitsize
1506 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
1507 && (bitsize + bitpos > GET_MODE_BITSIZE (extv_mode))))
1509 int xbitpos = bitpos, xoffset = offset;
1510 rtx bitsize_rtx, bitpos_rtx;
1511 rtx last = get_last_insn ();
1512 rtx xop0 = op0, xtarget = target;
1513 rtx xspec_target = spec_target;
1514 rtx xspec_target_subreg = spec_target_subreg;
1515 rtx pat;
1516 enum machine_mode maxmode = mode_for_extraction (EP_extv, 0);
1518 if (MEM_P (xop0))
1520 /* Is the memory operand acceptable? */
1521 if (! ((*insn_data[(int) CODE_FOR_extv].operand[1].predicate)
1522 (xop0, GET_MODE (xop0))))
1524 /* No, load into a reg and extract from there. */
1525 enum machine_mode bestmode;
1527 /* Get the mode to use for inserting into this field. If
1528 OP0 is BLKmode, get the smallest mode consistent with the
1529 alignment. If OP0 is a non-BLKmode object that is no
1530 wider than MAXMODE, use its mode. Otherwise, use the
1531 smallest mode containing the field. */
1533 if (GET_MODE (xop0) == BLKmode
1534 || (GET_MODE_SIZE (GET_MODE (op0))
1535 > GET_MODE_SIZE (maxmode)))
1536 bestmode = get_best_mode (bitsize, bitnum,
1537 MEM_ALIGN (xop0), maxmode,
1538 MEM_VOLATILE_P (xop0));
1539 else
1540 bestmode = GET_MODE (xop0);
1542 if (bestmode == VOIDmode
1543 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (xop0))
1544 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (xop0)))
1545 goto extv_loses;
1547 /* Compute offset as multiple of this unit,
1548 counting in bytes. */
1549 unit = GET_MODE_BITSIZE (bestmode);
1550 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1551 xbitpos = bitnum % unit;
1552 xop0 = adjust_address (xop0, bestmode, xoffset);
1554 /* Make sure register is big enough for the whole field. */
1555 if (xoffset * BITS_PER_UNIT + unit
1556 < offset * BITS_PER_UNIT + bitsize)
1557 goto extv_loses;
1559 /* Fetch it to a register in that size. */
1560 xop0 = force_reg (bestmode, xop0);
1562 /* XBITPOS counts within UNIT, which is what is expected. */
1564 else
1565 /* Get ref to first byte containing part of the field. */
1566 xop0 = adjust_address (xop0, byte_mode, xoffset);
1569 /* If op0 is a register, we need it in MAXMODE (which is usually
1570 SImode) to make it acceptable to the format of extv. */
1571 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1572 goto extv_loses;
1573 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
1574 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1576 /* On big-endian machines, we count bits from the most significant.
1577 If the bit field insn does not, we must invert. */
1578 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1579 xbitpos = unit - bitsize - xbitpos;
1581 /* XBITPOS counts within a size of UNIT.
1582 Adjust to count within a size of MAXMODE. */
1583 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
1584 xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
1586 unit = GET_MODE_BITSIZE (maxmode);
1588 if (xtarget == 0)
1589 xtarget = xspec_target = gen_reg_rtx (tmode);
1591 if (GET_MODE (xtarget) != maxmode)
1593 if (REG_P (xtarget))
1595 int wider = (GET_MODE_SIZE (maxmode)
1596 > GET_MODE_SIZE (GET_MODE (xtarget)));
1597 xtarget = gen_lowpart (maxmode, xtarget);
1598 if (wider)
1599 xspec_target_subreg = xtarget;
1601 else
1602 xtarget = gen_reg_rtx (maxmode);
1605 /* If this machine's extv insists on a register target,
1606 make sure we have one. */
1607 if (! ((*insn_data[(int) CODE_FOR_extv].operand[0].predicate)
1608 (xtarget, maxmode)))
1609 xtarget = gen_reg_rtx (maxmode);
1611 bitsize_rtx = GEN_INT (bitsize);
1612 bitpos_rtx = GEN_INT (xbitpos);
1614 pat = gen_extv (xtarget, xop0, bitsize_rtx, bitpos_rtx);
1615 if (pat)
1617 emit_insn (pat);
1618 target = xtarget;
1619 spec_target = xspec_target;
1620 spec_target_subreg = xspec_target_subreg;
1622 else
1624 delete_insns_since (last);
1625 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1626 bitpos, target, 0);
1629 else
1630 extv_loses:
1631 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1632 bitpos, target, 0);
1634 if (target == spec_target)
1635 return target;
1636 if (target == spec_target_subreg)
1637 return spec_target;
1638 if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
1640 /* If the target mode is not a scalar integral, first convert to the
1641 integer mode of that size and then access it as a floating-point
1642 value via a SUBREG. */
1643 if (!SCALAR_INT_MODE_P (tmode))
1645 enum machine_mode smode
1646 = mode_for_size (GET_MODE_BITSIZE (tmode), MODE_INT, 0);
1647 target = convert_to_mode (smode, target, unsignedp);
1648 target = force_reg (smode, target);
1649 return gen_lowpart (tmode, target);
1652 return convert_to_mode (tmode, target, unsignedp);
1654 return target;
1657 /* Extract a bit field using shifts and boolean operations
1658 Returns an rtx to represent the value.
1659 OP0 addresses a register (word) or memory (byte).
1660 BITPOS says which bit within the word or byte the bit field starts in.
1661 OFFSET says how many bytes farther the bit field starts;
1662 it is 0 if OP0 is a register.
1663 BITSIZE says how many bits long the bit field is.
1664 (If OP0 is a register, it may be narrower than a full word,
1665 but BITPOS still counts within a full word,
1666 which is significant on bigendian machines.)
1668 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1669 If TARGET is nonzero, attempts to store the value there
1670 and return TARGET, but this is not guaranteed.
1671 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
1673 static rtx
1674 extract_fixed_bit_field (enum machine_mode tmode, rtx op0,
1675 unsigned HOST_WIDE_INT offset,
1676 unsigned HOST_WIDE_INT bitsize,
1677 unsigned HOST_WIDE_INT bitpos, rtx target,
1678 int unsignedp)
1680 unsigned int total_bits = BITS_PER_WORD;
1681 enum machine_mode mode;
1683 if (GET_CODE (op0) == SUBREG || REG_P (op0))
1685 /* Special treatment for a bit field split across two registers. */
1686 if (bitsize + bitpos > BITS_PER_WORD)
1687 return extract_split_bit_field (op0, bitsize, bitpos, unsignedp);
1689 else
1691 /* Get the proper mode to use for this field. We want a mode that
1692 includes the entire field. If such a mode would be larger than
1693 a word, we won't be doing the extraction the normal way. */
1695 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1696 MEM_ALIGN (op0), word_mode, MEM_VOLATILE_P (op0));
1698 if (mode == VOIDmode)
1699 /* The only way this should occur is if the field spans word
1700 boundaries. */
1701 return extract_split_bit_field (op0, bitsize,
1702 bitpos + offset * BITS_PER_UNIT,
1703 unsignedp);
1705 total_bits = GET_MODE_BITSIZE (mode);
1707 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
1708 be in the range 0 to total_bits-1, and put any excess bytes in
1709 OFFSET. */
1710 if (bitpos >= total_bits)
1712 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
1713 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
1714 * BITS_PER_UNIT);
1717 /* Get ref to an aligned byte, halfword, or word containing the field.
1718 Adjust BITPOS to be position within a word,
1719 and OFFSET to be the offset of that word.
1720 Then alter OP0 to refer to that word. */
1721 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1722 offset -= (offset % (total_bits / BITS_PER_UNIT));
1723 op0 = adjust_address (op0, mode, offset);
1726 mode = GET_MODE (op0);
1728 if (BYTES_BIG_ENDIAN)
1729 /* BITPOS is the distance between our msb and that of OP0.
1730 Convert it to the distance from the lsb. */
1731 bitpos = total_bits - bitsize - bitpos;
1733 /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1734 We have reduced the big-endian case to the little-endian case. */
1736 if (unsignedp)
1738 if (bitpos)
1740 /* If the field does not already start at the lsb,
1741 shift it so it does. */
1742 tree amount = build_int_cst (NULL_TREE, bitpos);
1743 /* Maybe propagate the target for the shift. */
1744 /* But not if we will return it--could confuse integrate.c. */
1745 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1746 if (tmode != mode) subtarget = 0;
1747 op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1749 /* Convert the value to the desired mode. */
1750 if (mode != tmode)
1751 op0 = convert_to_mode (tmode, op0, 1);
1753 /* Unless the msb of the field used to be the msb when we shifted,
1754 mask out the upper bits. */
1756 if (GET_MODE_BITSIZE (mode) != bitpos + bitsize)
1757 return expand_binop (GET_MODE (op0), and_optab, op0,
1758 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1759 target, 1, OPTAB_LIB_WIDEN);
1760 return op0;
1763 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1764 then arithmetic-shift its lsb to the lsb of the word. */
1765 op0 = force_reg (mode, op0);
1766 if (mode != tmode)
1767 target = 0;
1769 /* Find the narrowest integer mode that contains the field. */
1771 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1772 mode = GET_MODE_WIDER_MODE (mode))
1773 if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1775 op0 = convert_to_mode (mode, op0, 0);
1776 break;
1779 if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1781 tree amount
1782 = build_int_cst (NULL_TREE,
1783 GET_MODE_BITSIZE (mode) - (bitsize + bitpos));
1784 /* Maybe propagate the target for the shift. */
1785 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1786 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1789 return expand_shift (RSHIFT_EXPR, mode, op0,
1790 build_int_cst (NULL_TREE,
1791 GET_MODE_BITSIZE (mode) - bitsize),
1792 target, 0);
1795 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1796 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1797 complement of that if COMPLEMENT. The mask is truncated if
1798 necessary to the width of mode MODE. The mask is zero-extended if
1799 BITSIZE+BITPOS is too small for MODE. */
1801 static rtx
1802 mask_rtx (enum machine_mode mode, int bitpos, int bitsize, int complement)
1804 HOST_WIDE_INT masklow, maskhigh;
1806 if (bitsize == 0)
1807 masklow = 0;
1808 else if (bitpos < HOST_BITS_PER_WIDE_INT)
1809 masklow = (HOST_WIDE_INT) -1 << bitpos;
1810 else
1811 masklow = 0;
1813 if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1814 masklow &= ((unsigned HOST_WIDE_INT) -1
1815 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1817 if (bitpos <= HOST_BITS_PER_WIDE_INT)
1818 maskhigh = -1;
1819 else
1820 maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1822 if (bitsize == 0)
1823 maskhigh = 0;
1824 else if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
1825 maskhigh &= ((unsigned HOST_WIDE_INT) -1
1826 >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1827 else
1828 maskhigh = 0;
1830 if (complement)
1832 maskhigh = ~maskhigh;
1833 masklow = ~masklow;
1836 return immed_double_const (masklow, maskhigh, mode);
1839 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1840 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1842 static rtx
1843 lshift_value (enum machine_mode mode, rtx value, int bitpos, int bitsize)
1845 unsigned HOST_WIDE_INT v = INTVAL (value);
1846 HOST_WIDE_INT low, high;
1848 if (bitsize < HOST_BITS_PER_WIDE_INT)
1849 v &= ~((HOST_WIDE_INT) -1 << bitsize);
1851 if (bitpos < HOST_BITS_PER_WIDE_INT)
1853 low = v << bitpos;
1854 high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
1856 else
1858 low = 0;
1859 high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
1862 return immed_double_const (low, high, mode);
1865 /* Extract a bit field from a memory by forcing the alignment of the
1866 memory. This efficient only if the field spans at least 4 boundaries.
1868 OP0 is the MEM.
1869 BITSIZE is the field width; BITPOS is the position of the first bit.
1870 UNSIGNEDP is true if the result should be zero-extended. */
1872 static rtx
1873 extract_force_align_mem_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
1874 unsigned HOST_WIDE_INT bitpos,
1875 int unsignedp)
1877 enum machine_mode mode, dmode;
1878 unsigned int m_bitsize, m_size;
1879 unsigned int sign_shift_up, sign_shift_dn;
1880 rtx base, a1, a2, v1, v2, comb, shift, result, start;
1882 /* Choose a mode that will fit BITSIZE. */
1883 mode = smallest_mode_for_size (bitsize, MODE_INT);
1884 m_size = GET_MODE_SIZE (mode);
1885 m_bitsize = GET_MODE_BITSIZE (mode);
1887 /* Choose a mode twice as wide. Fail if no such mode exists. */
1888 dmode = mode_for_size (m_bitsize * 2, MODE_INT, false);
1889 if (dmode == BLKmode)
1890 return NULL;
1892 do_pending_stack_adjust ();
1893 start = get_last_insn ();
1895 /* At the end, we'll need an additional shift to deal with sign/zero
1896 extension. By default this will be a left+right shift of the
1897 appropriate size. But we may be able to eliminate one of them. */
1898 sign_shift_up = sign_shift_dn = m_bitsize - bitsize;
1900 if (STRICT_ALIGNMENT)
1902 base = plus_constant (XEXP (op0, 0), bitpos / BITS_PER_UNIT);
1903 bitpos %= BITS_PER_UNIT;
1905 /* We load two values to be concatenate. There's an edge condition
1906 that bears notice -- an aligned value at the end of a page can
1907 only load one value lest we segfault. So the two values we load
1908 are at "base & -size" and "(base + size - 1) & -size". If base
1909 is unaligned, the addresses will be aligned and sequential; if
1910 base is aligned, the addresses will both be equal to base. */
1912 a1 = expand_simple_binop (Pmode, AND, force_operand (base, NULL),
1913 GEN_INT (-(HOST_WIDE_INT)m_size),
1914 NULL, true, OPTAB_LIB_WIDEN);
1915 mark_reg_pointer (a1, m_bitsize);
1916 v1 = gen_rtx_MEM (mode, a1);
1917 set_mem_align (v1, m_bitsize);
1918 v1 = force_reg (mode, validize_mem (v1));
1920 a2 = plus_constant (base, GET_MODE_SIZE (mode) - 1);
1921 a2 = expand_simple_binop (Pmode, AND, force_operand (a2, NULL),
1922 GEN_INT (-(HOST_WIDE_INT)m_size),
1923 NULL, true, OPTAB_LIB_WIDEN);
1924 v2 = gen_rtx_MEM (mode, a2);
1925 set_mem_align (v2, m_bitsize);
1926 v2 = force_reg (mode, validize_mem (v2));
1928 /* Combine these two values into a double-word value. */
1929 if (m_bitsize == BITS_PER_WORD)
1931 comb = gen_reg_rtx (dmode);
1932 emit_insn (gen_rtx_CLOBBER (VOIDmode, comb));
1933 emit_move_insn (gen_rtx_SUBREG (mode, comb, 0), v1);
1934 emit_move_insn (gen_rtx_SUBREG (mode, comb, m_size), v2);
1936 else
1938 if (BYTES_BIG_ENDIAN)
1939 comb = v1, v1 = v2, v2 = comb;
1940 v1 = convert_modes (dmode, mode, v1, true);
1941 if (v1 == NULL)
1942 goto fail;
1943 v2 = convert_modes (dmode, mode, v2, true);
1944 v2 = expand_simple_binop (dmode, ASHIFT, v2, GEN_INT (m_bitsize),
1945 NULL, true, OPTAB_LIB_WIDEN);
1946 if (v2 == NULL)
1947 goto fail;
1948 comb = expand_simple_binop (dmode, IOR, v1, v2, NULL,
1949 true, OPTAB_LIB_WIDEN);
1950 if (comb == NULL)
1951 goto fail;
1954 shift = expand_simple_binop (Pmode, AND, base, GEN_INT (m_size - 1),
1955 NULL, true, OPTAB_LIB_WIDEN);
1956 shift = expand_mult (Pmode, shift, GEN_INT (BITS_PER_UNIT), NULL, 1);
1958 if (bitpos != 0)
1960 if (sign_shift_up <= bitpos)
1961 bitpos -= sign_shift_up, sign_shift_up = 0;
1962 shift = expand_simple_binop (Pmode, PLUS, shift, GEN_INT (bitpos),
1963 NULL, true, OPTAB_LIB_WIDEN);
1966 else
1968 unsigned HOST_WIDE_INT offset = bitpos / BITS_PER_UNIT;
1969 bitpos %= BITS_PER_UNIT;
1971 /* When strict alignment is not required, we can just load directly
1972 from memory without masking. If the remaining BITPOS offset is
1973 small enough, we may be able to do all operations in MODE as
1974 opposed to DMODE. */
1975 if (bitpos + bitsize <= m_bitsize)
1976 dmode = mode;
1977 comb = adjust_address (op0, dmode, offset);
1979 if (sign_shift_up <= bitpos)
1980 bitpos -= sign_shift_up, sign_shift_up = 0;
1981 shift = GEN_INT (bitpos);
1984 /* Shift down the double-word such that the requested value is at bit 0. */
1985 if (shift != const0_rtx)
1986 comb = expand_simple_binop (dmode, unsignedp ? LSHIFTRT : ASHIFTRT,
1987 comb, shift, NULL, unsignedp, OPTAB_LIB_WIDEN);
1988 if (comb == NULL)
1989 goto fail;
1991 /* If the field exactly matches MODE, then all we need to do is return the
1992 lowpart. Otherwise, shift to get the sign bits set properly. */
1993 result = force_reg (mode, gen_lowpart (mode, comb));
1995 if (sign_shift_up)
1996 result = expand_simple_binop (mode, ASHIFT, result,
1997 GEN_INT (sign_shift_up),
1998 NULL_RTX, 0, OPTAB_LIB_WIDEN);
1999 if (sign_shift_dn)
2000 result = expand_simple_binop (mode, unsignedp ? LSHIFTRT : ASHIFTRT,
2001 result, GEN_INT (sign_shift_dn),
2002 NULL_RTX, 0, OPTAB_LIB_WIDEN);
2004 return result;
2006 fail:
2007 delete_insns_since (start);
2008 return NULL;
2011 /* Extract a bit field that is split across two words
2012 and return an RTX for the result.
2014 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
2015 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
2016 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
2018 static rtx
2019 extract_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
2020 unsigned HOST_WIDE_INT bitpos, int unsignedp)
2022 unsigned int unit;
2023 unsigned int bitsdone = 0;
2024 rtx result = NULL_RTX;
2025 int first = 1;
2027 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
2028 much at a time. */
2029 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
2030 unit = BITS_PER_WORD;
2031 else
2033 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
2034 if (0 && bitsize / unit > 2)
2036 rtx tmp = extract_force_align_mem_bit_field (op0, bitsize, bitpos,
2037 unsignedp);
2038 if (tmp)
2039 return tmp;
2043 while (bitsdone < bitsize)
2045 unsigned HOST_WIDE_INT thissize;
2046 rtx part, word;
2047 unsigned HOST_WIDE_INT thispos;
2048 unsigned HOST_WIDE_INT offset;
2050 offset = (bitpos + bitsdone) / unit;
2051 thispos = (bitpos + bitsdone) % unit;
2053 /* THISSIZE must not overrun a word boundary. Otherwise,
2054 extract_fixed_bit_field will call us again, and we will mutually
2055 recurse forever. */
2056 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
2057 thissize = MIN (thissize, unit - thispos);
2059 /* If OP0 is a register, then handle OFFSET here.
2061 When handling multiword bitfields, extract_bit_field may pass
2062 down a word_mode SUBREG of a larger REG for a bitfield that actually
2063 crosses a word boundary. Thus, for a SUBREG, we must find
2064 the current word starting from the base register. */
2065 if (GET_CODE (op0) == SUBREG)
2067 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
2068 word = operand_subword_force (SUBREG_REG (op0), word_offset,
2069 GET_MODE (SUBREG_REG (op0)));
2070 offset = 0;
2072 else if (REG_P (op0))
2074 word = operand_subword_force (op0, offset, GET_MODE (op0));
2075 offset = 0;
2077 else
2078 word = op0;
2080 /* Extract the parts in bit-counting order,
2081 whose meaning is determined by BYTES_PER_UNIT.
2082 OFFSET is in UNITs, and UNIT is in bits.
2083 extract_fixed_bit_field wants offset in bytes. */
2084 part = extract_fixed_bit_field (word_mode, word,
2085 offset * unit / BITS_PER_UNIT,
2086 thissize, thispos, 0, 1);
2087 bitsdone += thissize;
2089 /* Shift this part into place for the result. */
2090 if (BYTES_BIG_ENDIAN)
2092 if (bitsize != bitsdone)
2093 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2094 build_int_cst (NULL_TREE, bitsize - bitsdone),
2095 0, 1);
2097 else
2099 if (bitsdone != thissize)
2100 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2101 build_int_cst (NULL_TREE,
2102 bitsdone - thissize), 0, 1);
2105 if (first)
2106 result = part;
2107 else
2108 /* Combine the parts with bitwise or. This works
2109 because we extracted each part as an unsigned bit field. */
2110 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
2111 OPTAB_LIB_WIDEN);
2113 first = 0;
2116 /* Unsigned bit field: we are done. */
2117 if (unsignedp)
2118 return result;
2119 /* Signed bit field: sign-extend with two arithmetic shifts. */
2120 result = expand_shift (LSHIFT_EXPR, word_mode, result,
2121 build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
2122 NULL_RTX, 0);
2123 return expand_shift (RSHIFT_EXPR, word_mode, result,
2124 build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
2125 NULL_RTX, 0);
2128 /* Add INC into TARGET. */
2130 void
2131 expand_inc (rtx target, rtx inc)
2133 rtx value = expand_binop (GET_MODE (target), add_optab,
2134 target, inc,
2135 target, 0, OPTAB_LIB_WIDEN);
2136 if (value != target)
2137 emit_move_insn (target, value);
2140 /* Subtract DEC from TARGET. */
2142 void
2143 expand_dec (rtx target, rtx dec)
2145 rtx value = expand_binop (GET_MODE (target), sub_optab,
2146 target, dec,
2147 target, 0, OPTAB_LIB_WIDEN);
2148 if (value != target)
2149 emit_move_insn (target, value);
2152 /* Output a shift instruction for expression code CODE,
2153 with SHIFTED being the rtx for the value to shift,
2154 and AMOUNT the tree for the amount to shift by.
2155 Store the result in the rtx TARGET, if that is convenient.
2156 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2157 Return the rtx for where the value is. */
2160 expand_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2161 tree amount, rtx target, int unsignedp)
2163 rtx op1, temp = 0;
2164 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
2165 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2166 int try;
2168 /* Previously detected shift-counts computed by NEGATE_EXPR
2169 and shifted in the other direction; but that does not work
2170 on all machines. */
2172 op1 = expand_normal (amount);
2174 if (SHIFT_COUNT_TRUNCATED)
2176 if (GET_CODE (op1) == CONST_INT
2177 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2178 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode)))
2179 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2180 % GET_MODE_BITSIZE (mode));
2181 else if (GET_CODE (op1) == SUBREG
2182 && subreg_lowpart_p (op1))
2183 op1 = SUBREG_REG (op1);
2186 if (op1 == const0_rtx)
2187 return shifted;
2189 /* Check whether its cheaper to implement a left shift by a constant
2190 bit count by a sequence of additions. */
2191 if (code == LSHIFT_EXPR
2192 && GET_CODE (op1) == CONST_INT
2193 && INTVAL (op1) > 0
2194 && INTVAL (op1) < GET_MODE_BITSIZE (mode)
2195 && shift_cost[mode][INTVAL (op1)] > INTVAL (op1) * add_cost[mode])
2197 int i;
2198 for (i = 0; i < INTVAL (op1); i++)
2200 temp = force_reg (mode, shifted);
2201 shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2202 unsignedp, OPTAB_LIB_WIDEN);
2204 return shifted;
2207 for (try = 0; temp == 0 && try < 3; try++)
2209 enum optab_methods methods;
2211 if (try == 0)
2212 methods = OPTAB_DIRECT;
2213 else if (try == 1)
2214 methods = OPTAB_WIDEN;
2215 else
2216 methods = OPTAB_LIB_WIDEN;
2218 if (rotate)
2220 /* Widening does not work for rotation. */
2221 if (methods == OPTAB_WIDEN)
2222 continue;
2223 else if (methods == OPTAB_LIB_WIDEN)
2225 /* If we have been unable to open-code this by a rotation,
2226 do it as the IOR of two shifts. I.e., to rotate A
2227 by N bits, compute (A << N) | ((unsigned) A >> (C - N))
2228 where C is the bitsize of A.
2230 It is theoretically possible that the target machine might
2231 not be able to perform either shift and hence we would
2232 be making two libcalls rather than just the one for the
2233 shift (similarly if IOR could not be done). We will allow
2234 this extremely unlikely lossage to avoid complicating the
2235 code below. */
2237 rtx subtarget = target == shifted ? 0 : target;
2238 rtx temp1;
2239 tree type = TREE_TYPE (amount);
2240 tree new_amount = make_tree (type, op1);
2241 tree other_amount
2242 = fold_build2 (MINUS_EXPR, type,
2243 build_int_cst (type, GET_MODE_BITSIZE (mode)),
2244 amount);
2246 shifted = force_reg (mode, shifted);
2248 temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2249 mode, shifted, new_amount, 0, 1);
2250 temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2251 mode, shifted, other_amount, subtarget, 1);
2252 return expand_binop (mode, ior_optab, temp, temp1, target,
2253 unsignedp, methods);
2256 temp = expand_binop (mode,
2257 left ? rotl_optab : rotr_optab,
2258 shifted, op1, target, unsignedp, methods);
2260 else if (unsignedp)
2261 temp = expand_binop (mode,
2262 left ? ashl_optab : lshr_optab,
2263 shifted, op1, target, unsignedp, methods);
2265 /* Do arithmetic shifts.
2266 Also, if we are going to widen the operand, we can just as well
2267 use an arithmetic right-shift instead of a logical one. */
2268 if (temp == 0 && ! rotate
2269 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2271 enum optab_methods methods1 = methods;
2273 /* If trying to widen a log shift to an arithmetic shift,
2274 don't accept an arithmetic shift of the same size. */
2275 if (unsignedp)
2276 methods1 = OPTAB_MUST_WIDEN;
2278 /* Arithmetic shift */
2280 temp = expand_binop (mode,
2281 left ? ashl_optab : ashr_optab,
2282 shifted, op1, target, unsignedp, methods1);
2285 /* We used to try extzv here for logical right shifts, but that was
2286 only useful for one machine, the VAX, and caused poor code
2287 generation there for lshrdi3, so the code was deleted and a
2288 define_expand for lshrsi3 was added to vax.md. */
2291 gcc_assert (temp);
2292 return temp;
2295 enum alg_code {
2296 alg_unknown,
2297 alg_zero,
2298 alg_m, alg_shift,
2299 alg_add_t_m2,
2300 alg_sub_t_m2,
2301 alg_add_factor,
2302 alg_sub_factor,
2303 alg_add_t2_m,
2304 alg_sub_t2_m,
2305 alg_impossible
2308 /* This structure holds the "cost" of a multiply sequence. The
2309 "cost" field holds the total rtx_cost of every operator in the
2310 synthetic multiplication sequence, hence cost(a op b) is defined
2311 as rtx_cost(op) + cost(a) + cost(b), where cost(leaf) is zero.
2312 The "latency" field holds the minimum possible latency of the
2313 synthetic multiply, on a hypothetical infinitely parallel CPU.
2314 This is the critical path, or the maximum height, of the expression
2315 tree which is the sum of rtx_costs on the most expensive path from
2316 any leaf to the root. Hence latency(a op b) is defined as zero for
2317 leaves and rtx_cost(op) + max(latency(a), latency(b)) otherwise. */
2319 struct mult_cost {
2320 short cost; /* Total rtx_cost of the multiplication sequence. */
2321 short latency; /* The latency of the multiplication sequence. */
2324 /* This macro is used to compare a pointer to a mult_cost against an
2325 single integer "rtx_cost" value. This is equivalent to the macro
2326 CHEAPER_MULT_COST(X,Z) where Z = {Y,Y}. */
2327 #define MULT_COST_LESS(X,Y) ((X)->cost < (Y) \
2328 || ((X)->cost == (Y) && (X)->latency < (Y)))
2330 /* This macro is used to compare two pointers to mult_costs against
2331 each other. The macro returns true if X is cheaper than Y.
2332 Currently, the cheaper of two mult_costs is the one with the
2333 lower "cost". If "cost"s are tied, the lower latency is cheaper. */
2334 #define CHEAPER_MULT_COST(X,Y) ((X)->cost < (Y)->cost \
2335 || ((X)->cost == (Y)->cost \
2336 && (X)->latency < (Y)->latency))
2338 /* This structure records a sequence of operations.
2339 `ops' is the number of operations recorded.
2340 `cost' is their total cost.
2341 The operations are stored in `op' and the corresponding
2342 logarithms of the integer coefficients in `log'.
2344 These are the operations:
2345 alg_zero total := 0;
2346 alg_m total := multiplicand;
2347 alg_shift total := total * coeff
2348 alg_add_t_m2 total := total + multiplicand * coeff;
2349 alg_sub_t_m2 total := total - multiplicand * coeff;
2350 alg_add_factor total := total * coeff + total;
2351 alg_sub_factor total := total * coeff - total;
2352 alg_add_t2_m total := total * coeff + multiplicand;
2353 alg_sub_t2_m total := total * coeff - multiplicand;
2355 The first operand must be either alg_zero or alg_m. */
2357 struct algorithm
2359 struct mult_cost cost;
2360 short ops;
2361 /* The size of the OP and LOG fields are not directly related to the
2362 word size, but the worst-case algorithms will be if we have few
2363 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
2364 In that case we will generate shift-by-2, add, shift-by-2, add,...,
2365 in total wordsize operations. */
2366 enum alg_code op[MAX_BITS_PER_WORD];
2367 char log[MAX_BITS_PER_WORD];
2370 /* The entry for our multiplication cache/hash table. */
2371 struct alg_hash_entry {
2372 /* The number we are multiplying by. */
2373 unsigned int t;
2375 /* The mode in which we are multiplying something by T. */
2376 enum machine_mode mode;
2378 /* The best multiplication algorithm for t. */
2379 enum alg_code alg;
2381 /* The cost of multiplication if ALG_CODE is not alg_impossible.
2382 Otherwise, the cost within which multiplication by T is
2383 impossible. */
2384 struct mult_cost cost;
2387 /* The number of cache/hash entries. */
2388 #define NUM_ALG_HASH_ENTRIES 307
2390 /* Each entry of ALG_HASH caches alg_code for some integer. This is
2391 actually a hash table. If we have a collision, that the older
2392 entry is kicked out. */
2393 static struct alg_hash_entry alg_hash[NUM_ALG_HASH_ENTRIES];
2395 /* Indicates the type of fixup needed after a constant multiplication.
2396 BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2397 the result should be negated, and ADD_VARIANT means that the
2398 multiplicand should be added to the result. */
2399 enum mult_variant {basic_variant, negate_variant, add_variant};
2401 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2402 const struct mult_cost *, enum machine_mode mode);
2403 static bool choose_mult_variant (enum machine_mode, HOST_WIDE_INT,
2404 struct algorithm *, enum mult_variant *, int);
2405 static rtx expand_mult_const (enum machine_mode, rtx, HOST_WIDE_INT, rtx,
2406 const struct algorithm *, enum mult_variant);
2407 static unsigned HOST_WIDE_INT choose_multiplier (unsigned HOST_WIDE_INT, int,
2408 int, rtx *, int *, int *);
2409 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2410 static rtx extract_high_half (enum machine_mode, rtx);
2411 static rtx expand_mult_highpart (enum machine_mode, rtx, rtx, rtx, int, int);
2412 static rtx expand_mult_highpart_optab (enum machine_mode, rtx, rtx, rtx,
2413 int, int);
2414 /* Compute and return the best algorithm for multiplying by T.
2415 The algorithm must cost less than cost_limit
2416 If retval.cost >= COST_LIMIT, no algorithm was found and all
2417 other field of the returned struct are undefined.
2418 MODE is the machine mode of the multiplication. */
2420 static void
2421 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2422 const struct mult_cost *cost_limit, enum machine_mode mode)
2424 int m;
2425 struct algorithm *alg_in, *best_alg;
2426 struct mult_cost best_cost;
2427 struct mult_cost new_limit;
2428 int op_cost, op_latency;
2429 unsigned HOST_WIDE_INT q;
2430 int maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (mode));
2431 int hash_index;
2432 bool cache_hit = false;
2433 enum alg_code cache_alg = alg_zero;
2435 /* Indicate that no algorithm is yet found. If no algorithm
2436 is found, this value will be returned and indicate failure. */
2437 alg_out->cost.cost = cost_limit->cost + 1;
2438 alg_out->cost.latency = cost_limit->latency + 1;
2440 if (cost_limit->cost < 0
2441 || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2442 return;
2444 /* Restrict the bits of "t" to the multiplication's mode. */
2445 t &= GET_MODE_MASK (mode);
2447 /* t == 1 can be done in zero cost. */
2448 if (t == 1)
2450 alg_out->ops = 1;
2451 alg_out->cost.cost = 0;
2452 alg_out->cost.latency = 0;
2453 alg_out->op[0] = alg_m;
2454 return;
2457 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2458 fail now. */
2459 if (t == 0)
2461 if (MULT_COST_LESS (cost_limit, zero_cost))
2462 return;
2463 else
2465 alg_out->ops = 1;
2466 alg_out->cost.cost = zero_cost;
2467 alg_out->cost.latency = zero_cost;
2468 alg_out->op[0] = alg_zero;
2469 return;
2473 /* We'll be needing a couple extra algorithm structures now. */
2475 alg_in = alloca (sizeof (struct algorithm));
2476 best_alg = alloca (sizeof (struct algorithm));
2477 best_cost = *cost_limit;
2479 /* Compute the hash index. */
2480 hash_index = (t ^ (unsigned int) mode) % NUM_ALG_HASH_ENTRIES;
2482 /* See if we already know what to do for T. */
2483 if (alg_hash[hash_index].t == t
2484 && alg_hash[hash_index].mode == mode
2485 && alg_hash[hash_index].alg != alg_unknown)
2487 cache_alg = alg_hash[hash_index].alg;
2489 if (cache_alg == alg_impossible)
2491 /* The cache tells us that it's impossible to synthesize
2492 multiplication by T within alg_hash[hash_index].cost. */
2493 if (!CHEAPER_MULT_COST (&alg_hash[hash_index].cost, cost_limit))
2494 /* COST_LIMIT is at least as restrictive as the one
2495 recorded in the hash table, in which case we have no
2496 hope of synthesizing a multiplication. Just
2497 return. */
2498 return;
2500 /* If we get here, COST_LIMIT is less restrictive than the
2501 one recorded in the hash table, so we may be able to
2502 synthesize a multiplication. Proceed as if we didn't
2503 have the cache entry. */
2505 else
2507 if (CHEAPER_MULT_COST (cost_limit, &alg_hash[hash_index].cost))
2508 /* The cached algorithm shows that this multiplication
2509 requires more cost than COST_LIMIT. Just return. This
2510 way, we don't clobber this cache entry with
2511 alg_impossible but retain useful information. */
2512 return;
2514 cache_hit = true;
2516 switch (cache_alg)
2518 case alg_shift:
2519 goto do_alg_shift;
2521 case alg_add_t_m2:
2522 case alg_sub_t_m2:
2523 goto do_alg_addsub_t_m2;
2525 case alg_add_factor:
2526 case alg_sub_factor:
2527 goto do_alg_addsub_factor;
2529 case alg_add_t2_m:
2530 goto do_alg_add_t2_m;
2532 case alg_sub_t2_m:
2533 goto do_alg_sub_t2_m;
2535 default:
2536 gcc_unreachable ();
2541 /* If we have a group of zero bits at the low-order part of T, try
2542 multiplying by the remaining bits and then doing a shift. */
2544 if ((t & 1) == 0)
2546 do_alg_shift:
2547 m = floor_log2 (t & -t); /* m = number of low zero bits */
2548 if (m < maxm)
2550 q = t >> m;
2551 /* The function expand_shift will choose between a shift and
2552 a sequence of additions, so the observed cost is given as
2553 MIN (m * add_cost[mode], shift_cost[mode][m]). */
2554 op_cost = m * add_cost[mode];
2555 if (shift_cost[mode][m] < op_cost)
2556 op_cost = shift_cost[mode][m];
2557 new_limit.cost = best_cost.cost - op_cost;
2558 new_limit.latency = best_cost.latency - op_cost;
2559 synth_mult (alg_in, q, &new_limit, mode);
2561 alg_in->cost.cost += op_cost;
2562 alg_in->cost.latency += op_cost;
2563 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2565 struct algorithm *x;
2566 best_cost = alg_in->cost;
2567 x = alg_in, alg_in = best_alg, best_alg = x;
2568 best_alg->log[best_alg->ops] = m;
2569 best_alg->op[best_alg->ops] = alg_shift;
2572 if (cache_hit)
2573 goto done;
2576 /* If we have an odd number, add or subtract one. */
2577 if ((t & 1) != 0)
2579 unsigned HOST_WIDE_INT w;
2581 do_alg_addsub_t_m2:
2582 for (w = 1; (w & t) != 0; w <<= 1)
2584 /* If T was -1, then W will be zero after the loop. This is another
2585 case where T ends with ...111. Handling this with (T + 1) and
2586 subtract 1 produces slightly better code and results in algorithm
2587 selection much faster than treating it like the ...0111 case
2588 below. */
2589 if (w == 0
2590 || (w > 2
2591 /* Reject the case where t is 3.
2592 Thus we prefer addition in that case. */
2593 && t != 3))
2595 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2597 op_cost = add_cost[mode];
2598 new_limit.cost = best_cost.cost - op_cost;
2599 new_limit.latency = best_cost.latency - op_cost;
2600 synth_mult (alg_in, t + 1, &new_limit, mode);
2602 alg_in->cost.cost += op_cost;
2603 alg_in->cost.latency += op_cost;
2604 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2606 struct algorithm *x;
2607 best_cost = alg_in->cost;
2608 x = alg_in, alg_in = best_alg, best_alg = x;
2609 best_alg->log[best_alg->ops] = 0;
2610 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2613 else
2615 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2617 op_cost = add_cost[mode];
2618 new_limit.cost = best_cost.cost - op_cost;
2619 new_limit.latency = best_cost.latency - op_cost;
2620 synth_mult (alg_in, t - 1, &new_limit, mode);
2622 alg_in->cost.cost += op_cost;
2623 alg_in->cost.latency += op_cost;
2624 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2626 struct algorithm *x;
2627 best_cost = alg_in->cost;
2628 x = alg_in, alg_in = best_alg, best_alg = x;
2629 best_alg->log[best_alg->ops] = 0;
2630 best_alg->op[best_alg->ops] = alg_add_t_m2;
2633 if (cache_hit)
2634 goto done;
2637 /* Look for factors of t of the form
2638 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2639 If we find such a factor, we can multiply by t using an algorithm that
2640 multiplies by q, shift the result by m and add/subtract it to itself.
2642 We search for large factors first and loop down, even if large factors
2643 are less probable than small; if we find a large factor we will find a
2644 good sequence quickly, and therefore be able to prune (by decreasing
2645 COST_LIMIT) the search. */
2647 do_alg_addsub_factor:
2648 for (m = floor_log2 (t - 1); m >= 2; m--)
2650 unsigned HOST_WIDE_INT d;
2652 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2653 if (t % d == 0 && t > d && m < maxm
2654 && (!cache_hit || cache_alg == alg_add_factor))
2656 /* If the target has a cheap shift-and-add instruction use
2657 that in preference to a shift insn followed by an add insn.
2658 Assume that the shift-and-add is "atomic" with a latency
2659 equal to its cost, otherwise assume that on superscalar
2660 hardware the shift may be executed concurrently with the
2661 earlier steps in the algorithm. */
2662 op_cost = add_cost[mode] + shift_cost[mode][m];
2663 if (shiftadd_cost[mode][m] < op_cost)
2665 op_cost = shiftadd_cost[mode][m];
2666 op_latency = op_cost;
2668 else
2669 op_latency = add_cost[mode];
2671 new_limit.cost = best_cost.cost - op_cost;
2672 new_limit.latency = best_cost.latency - op_latency;
2673 synth_mult (alg_in, t / d, &new_limit, mode);
2675 alg_in->cost.cost += op_cost;
2676 alg_in->cost.latency += op_latency;
2677 if (alg_in->cost.latency < op_cost)
2678 alg_in->cost.latency = op_cost;
2679 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2681 struct algorithm *x;
2682 best_cost = alg_in->cost;
2683 x = alg_in, alg_in = best_alg, best_alg = x;
2684 best_alg->log[best_alg->ops] = m;
2685 best_alg->op[best_alg->ops] = alg_add_factor;
2687 /* Other factors will have been taken care of in the recursion. */
2688 break;
2691 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2692 if (t % d == 0 && t > d && m < maxm
2693 && (!cache_hit || cache_alg == alg_sub_factor))
2695 /* If the target has a cheap shift-and-subtract insn use
2696 that in preference to a shift insn followed by a sub insn.
2697 Assume that the shift-and-sub is "atomic" with a latency
2698 equal to it's cost, otherwise assume that on superscalar
2699 hardware the shift may be executed concurrently with the
2700 earlier steps in the algorithm. */
2701 op_cost = add_cost[mode] + shift_cost[mode][m];
2702 if (shiftsub_cost[mode][m] < op_cost)
2704 op_cost = shiftsub_cost[mode][m];
2705 op_latency = op_cost;
2707 else
2708 op_latency = add_cost[mode];
2710 new_limit.cost = best_cost.cost - op_cost;
2711 new_limit.latency = best_cost.latency - op_latency;
2712 synth_mult (alg_in, t / d, &new_limit, mode);
2714 alg_in->cost.cost += op_cost;
2715 alg_in->cost.latency += op_latency;
2716 if (alg_in->cost.latency < op_cost)
2717 alg_in->cost.latency = op_cost;
2718 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2720 struct algorithm *x;
2721 best_cost = alg_in->cost;
2722 x = alg_in, alg_in = best_alg, best_alg = x;
2723 best_alg->log[best_alg->ops] = m;
2724 best_alg->op[best_alg->ops] = alg_sub_factor;
2726 break;
2729 if (cache_hit)
2730 goto done;
2732 /* Try shift-and-add (load effective address) instructions,
2733 i.e. do a*3, a*5, a*9. */
2734 if ((t & 1) != 0)
2736 do_alg_add_t2_m:
2737 q = t - 1;
2738 q = q & -q;
2739 m = exact_log2 (q);
2740 if (m >= 0 && m < maxm)
2742 op_cost = shiftadd_cost[mode][m];
2743 new_limit.cost = best_cost.cost - op_cost;
2744 new_limit.latency = best_cost.latency - op_cost;
2745 synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2747 alg_in->cost.cost += op_cost;
2748 alg_in->cost.latency += op_cost;
2749 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2751 struct algorithm *x;
2752 best_cost = alg_in->cost;
2753 x = alg_in, alg_in = best_alg, best_alg = x;
2754 best_alg->log[best_alg->ops] = m;
2755 best_alg->op[best_alg->ops] = alg_add_t2_m;
2758 if (cache_hit)
2759 goto done;
2761 do_alg_sub_t2_m:
2762 q = t + 1;
2763 q = q & -q;
2764 m = exact_log2 (q);
2765 if (m >= 0 && m < maxm)
2767 op_cost = shiftsub_cost[mode][m];
2768 new_limit.cost = best_cost.cost - op_cost;
2769 new_limit.latency = best_cost.latency - op_cost;
2770 synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2772 alg_in->cost.cost += op_cost;
2773 alg_in->cost.latency += op_cost;
2774 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2776 struct algorithm *x;
2777 best_cost = alg_in->cost;
2778 x = alg_in, alg_in = best_alg, best_alg = x;
2779 best_alg->log[best_alg->ops] = m;
2780 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2783 if (cache_hit)
2784 goto done;
2787 done:
2788 /* If best_cost has not decreased, we have not found any algorithm. */
2789 if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
2791 /* We failed to find an algorithm. Record alg_impossible for
2792 this case (that is, <T, MODE, COST_LIMIT>) so that next time
2793 we are asked to find an algorithm for T within the same or
2794 lower COST_LIMIT, we can immediately return to the
2795 caller. */
2796 alg_hash[hash_index].t = t;
2797 alg_hash[hash_index].mode = mode;
2798 alg_hash[hash_index].alg = alg_impossible;
2799 alg_hash[hash_index].cost = *cost_limit;
2800 return;
2803 /* Cache the result. */
2804 if (!cache_hit)
2806 alg_hash[hash_index].t = t;
2807 alg_hash[hash_index].mode = mode;
2808 alg_hash[hash_index].alg = best_alg->op[best_alg->ops];
2809 alg_hash[hash_index].cost.cost = best_cost.cost;
2810 alg_hash[hash_index].cost.latency = best_cost.latency;
2813 /* If we are getting a too long sequence for `struct algorithm'
2814 to record, make this search fail. */
2815 if (best_alg->ops == MAX_BITS_PER_WORD)
2816 return;
2818 /* Copy the algorithm from temporary space to the space at alg_out.
2819 We avoid using structure assignment because the majority of
2820 best_alg is normally undefined, and this is a critical function. */
2821 alg_out->ops = best_alg->ops + 1;
2822 alg_out->cost = best_cost;
2823 memcpy (alg_out->op, best_alg->op,
2824 alg_out->ops * sizeof *alg_out->op);
2825 memcpy (alg_out->log, best_alg->log,
2826 alg_out->ops * sizeof *alg_out->log);
2829 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2830 Try three variations:
2832 - a shift/add sequence based on VAL itself
2833 - a shift/add sequence based on -VAL, followed by a negation
2834 - a shift/add sequence based on VAL - 1, followed by an addition.
2836 Return true if the cheapest of these cost less than MULT_COST,
2837 describing the algorithm in *ALG and final fixup in *VARIANT. */
2839 static bool
2840 choose_mult_variant (enum machine_mode mode, HOST_WIDE_INT val,
2841 struct algorithm *alg, enum mult_variant *variant,
2842 int mult_cost)
2844 struct algorithm alg2;
2845 struct mult_cost limit;
2846 int op_cost;
2848 /* Fail quickly for impossible bounds. */
2849 if (mult_cost < 0)
2850 return false;
2852 /* Ensure that mult_cost provides a reasonable upper bound.
2853 Any constant multiplication can be performed with less
2854 than 2 * bits additions. */
2855 op_cost = 2 * GET_MODE_BITSIZE (mode) * add_cost[mode];
2856 if (mult_cost > op_cost)
2857 mult_cost = op_cost;
2859 *variant = basic_variant;
2860 limit.cost = mult_cost;
2861 limit.latency = mult_cost;
2862 synth_mult (alg, val, &limit, mode);
2864 /* This works only if the inverted value actually fits in an
2865 `unsigned int' */
2866 if (HOST_BITS_PER_INT >= GET_MODE_BITSIZE (mode))
2868 op_cost = neg_cost[mode];
2869 if (MULT_COST_LESS (&alg->cost, mult_cost))
2871 limit.cost = alg->cost.cost - op_cost;
2872 limit.latency = alg->cost.latency - op_cost;
2874 else
2876 limit.cost = mult_cost - op_cost;
2877 limit.latency = mult_cost - op_cost;
2880 synth_mult (&alg2, -val, &limit, mode);
2881 alg2.cost.cost += op_cost;
2882 alg2.cost.latency += op_cost;
2883 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2884 *alg = alg2, *variant = negate_variant;
2887 /* This proves very useful for division-by-constant. */
2888 op_cost = add_cost[mode];
2889 if (MULT_COST_LESS (&alg->cost, mult_cost))
2891 limit.cost = alg->cost.cost - op_cost;
2892 limit.latency = alg->cost.latency - op_cost;
2894 else
2896 limit.cost = mult_cost - op_cost;
2897 limit.latency = mult_cost - op_cost;
2900 synth_mult (&alg2, val - 1, &limit, mode);
2901 alg2.cost.cost += op_cost;
2902 alg2.cost.latency += op_cost;
2903 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2904 *alg = alg2, *variant = add_variant;
2906 return MULT_COST_LESS (&alg->cost, mult_cost);
2909 /* A subroutine of expand_mult, used for constant multiplications.
2910 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2911 convenient. Use the shift/add sequence described by ALG and apply
2912 the final fixup specified by VARIANT. */
2914 static rtx
2915 expand_mult_const (enum machine_mode mode, rtx op0, HOST_WIDE_INT val,
2916 rtx target, const struct algorithm *alg,
2917 enum mult_variant variant)
2919 HOST_WIDE_INT val_so_far;
2920 rtx insn, accum, tem;
2921 int opno;
2922 enum machine_mode nmode;
2924 /* Avoid referencing memory over and over.
2925 For speed, but also for correctness when mem is volatile. */
2926 if (MEM_P (op0))
2927 op0 = force_reg (mode, op0);
2929 /* ACCUM starts out either as OP0 or as a zero, depending on
2930 the first operation. */
2932 if (alg->op[0] == alg_zero)
2934 accum = copy_to_mode_reg (mode, const0_rtx);
2935 val_so_far = 0;
2937 else if (alg->op[0] == alg_m)
2939 accum = copy_to_mode_reg (mode, op0);
2940 val_so_far = 1;
2942 else
2943 gcc_unreachable ();
2945 for (opno = 1; opno < alg->ops; opno++)
2947 int log = alg->log[opno];
2948 rtx shift_subtarget = optimize ? 0 : accum;
2949 rtx add_target
2950 = (opno == alg->ops - 1 && target != 0 && variant != add_variant
2951 && !optimize)
2952 ? target : 0;
2953 rtx accum_target = optimize ? 0 : accum;
2955 switch (alg->op[opno])
2957 case alg_shift:
2958 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2959 build_int_cst (NULL_TREE, log),
2960 NULL_RTX, 0);
2961 val_so_far <<= log;
2962 break;
2964 case alg_add_t_m2:
2965 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2966 build_int_cst (NULL_TREE, log),
2967 NULL_RTX, 0);
2968 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2969 add_target ? add_target : accum_target);
2970 val_so_far += (HOST_WIDE_INT) 1 << log;
2971 break;
2973 case alg_sub_t_m2:
2974 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2975 build_int_cst (NULL_TREE, log),
2976 NULL_RTX, 0);
2977 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
2978 add_target ? add_target : accum_target);
2979 val_so_far -= (HOST_WIDE_INT) 1 << log;
2980 break;
2982 case alg_add_t2_m:
2983 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2984 build_int_cst (NULL_TREE, log),
2985 shift_subtarget,
2987 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
2988 add_target ? add_target : accum_target);
2989 val_so_far = (val_so_far << log) + 1;
2990 break;
2992 case alg_sub_t2_m:
2993 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2994 build_int_cst (NULL_TREE, log),
2995 shift_subtarget, 0);
2996 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
2997 add_target ? add_target : accum_target);
2998 val_so_far = (val_so_far << log) - 1;
2999 break;
3001 case alg_add_factor:
3002 tem = expand_shift (LSHIFT_EXPR, mode, accum,
3003 build_int_cst (NULL_TREE, log),
3004 NULL_RTX, 0);
3005 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
3006 add_target ? add_target : accum_target);
3007 val_so_far += val_so_far << log;
3008 break;
3010 case alg_sub_factor:
3011 tem = expand_shift (LSHIFT_EXPR, mode, accum,
3012 build_int_cst (NULL_TREE, log),
3013 NULL_RTX, 0);
3014 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
3015 (add_target
3016 ? add_target : (optimize ? 0 : tem)));
3017 val_so_far = (val_so_far << log) - val_so_far;
3018 break;
3020 default:
3021 gcc_unreachable ();
3024 /* Write a REG_EQUAL note on the last insn so that we can cse
3025 multiplication sequences. Note that if ACCUM is a SUBREG,
3026 we've set the inner register and must properly indicate
3027 that. */
3029 tem = op0, nmode = mode;
3030 if (GET_CODE (accum) == SUBREG)
3032 nmode = GET_MODE (SUBREG_REG (accum));
3033 tem = gen_lowpart (nmode, op0);
3036 insn = get_last_insn ();
3037 set_unique_reg_note (insn, REG_EQUAL,
3038 gen_rtx_MULT (nmode, tem, GEN_INT (val_so_far)));
3041 if (variant == negate_variant)
3043 val_so_far = -val_so_far;
3044 accum = expand_unop (mode, neg_optab, accum, target, 0);
3046 else if (variant == add_variant)
3048 val_so_far = val_so_far + 1;
3049 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
3052 /* Compare only the bits of val and val_so_far that are significant
3053 in the result mode, to avoid sign-/zero-extension confusion. */
3054 val &= GET_MODE_MASK (mode);
3055 val_so_far &= GET_MODE_MASK (mode);
3056 gcc_assert (val == val_so_far);
3058 return accum;
3061 /* Perform a multiplication and return an rtx for the result.
3062 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3063 TARGET is a suggestion for where to store the result (an rtx).
3065 We check specially for a constant integer as OP1.
3066 If you want this check for OP0 as well, then before calling
3067 you should swap the two operands if OP0 would be constant. */
3070 expand_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3071 int unsignedp)
3073 enum mult_variant variant;
3074 struct algorithm algorithm;
3075 int max_cost;
3077 /* Handling const0_rtx here allows us to use zero as a rogue value for
3078 coeff below. */
3079 if (op1 == const0_rtx)
3080 return const0_rtx;
3081 if (op1 == const1_rtx)
3082 return op0;
3083 if (op1 == constm1_rtx)
3084 return expand_unop (mode,
3085 GET_MODE_CLASS (mode) == MODE_INT
3086 && !unsignedp && flag_trapv
3087 ? negv_optab : neg_optab,
3088 op0, target, 0);
3090 /* These are the operations that are potentially turned into a sequence
3091 of shifts and additions. */
3092 if (SCALAR_INT_MODE_P (mode)
3093 && (unsignedp || !flag_trapv))
3095 HOST_WIDE_INT coeff = 0;
3096 rtx fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3098 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3099 less than or equal in size to `unsigned int' this doesn't matter.
3100 If the mode is larger than `unsigned int', then synth_mult works
3101 only if the constant value exactly fits in an `unsigned int' without
3102 any truncation. This means that multiplying by negative values does
3103 not work; results are off by 2^32 on a 32 bit machine. */
3105 if (GET_CODE (op1) == CONST_INT)
3107 /* Attempt to handle multiplication of DImode values by negative
3108 coefficients, by performing the multiplication by a positive
3109 multiplier and then inverting the result. */
3110 if (INTVAL (op1) < 0
3111 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
3113 /* Its safe to use -INTVAL (op1) even for INT_MIN, as the
3114 result is interpreted as an unsigned coefficient.
3115 Exclude cost of op0 from max_cost to match the cost
3116 calculation of the synth_mult. */
3117 max_cost = rtx_cost (gen_rtx_MULT (mode, fake_reg, op1), SET)
3118 - neg_cost[mode];
3119 if (max_cost > 0
3120 && choose_mult_variant (mode, -INTVAL (op1), &algorithm,
3121 &variant, max_cost))
3123 rtx temp = expand_mult_const (mode, op0, -INTVAL (op1),
3124 NULL_RTX, &algorithm,
3125 variant);
3126 return expand_unop (mode, neg_optab, temp, target, 0);
3129 else coeff = INTVAL (op1);
3131 else if (GET_CODE (op1) == CONST_DOUBLE)
3133 /* If we are multiplying in DImode, it may still be a win
3134 to try to work with shifts and adds. */
3135 if (CONST_DOUBLE_HIGH (op1) == 0)
3136 coeff = CONST_DOUBLE_LOW (op1);
3137 else if (CONST_DOUBLE_LOW (op1) == 0
3138 && EXACT_POWER_OF_2_OR_ZERO_P (CONST_DOUBLE_HIGH (op1)))
3140 int shift = floor_log2 (CONST_DOUBLE_HIGH (op1))
3141 + HOST_BITS_PER_WIDE_INT;
3142 return expand_shift (LSHIFT_EXPR, mode, op0,
3143 build_int_cst (NULL_TREE, shift),
3144 target, unsignedp);
3148 /* We used to test optimize here, on the grounds that it's better to
3149 produce a smaller program when -O is not used. But this causes
3150 such a terrible slowdown sometimes that it seems better to always
3151 use synth_mult. */
3152 if (coeff != 0)
3154 /* Special case powers of two. */
3155 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3156 return expand_shift (LSHIFT_EXPR, mode, op0,
3157 build_int_cst (NULL_TREE, floor_log2 (coeff)),
3158 target, unsignedp);
3160 /* Exclude cost of op0 from max_cost to match the cost
3161 calculation of the synth_mult. */
3162 max_cost = rtx_cost (gen_rtx_MULT (mode, fake_reg, op1), SET);
3163 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3164 max_cost))
3165 return expand_mult_const (mode, op0, coeff, target,
3166 &algorithm, variant);
3170 if (GET_CODE (op0) == CONST_DOUBLE)
3172 rtx temp = op0;
3173 op0 = op1;
3174 op1 = temp;
3177 /* Expand x*2.0 as x+x. */
3178 if (GET_CODE (op1) == CONST_DOUBLE
3179 && SCALAR_FLOAT_MODE_P (mode))
3181 REAL_VALUE_TYPE d;
3182 REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
3184 if (REAL_VALUES_EQUAL (d, dconst2))
3186 op0 = force_reg (GET_MODE (op0), op0);
3187 return expand_binop (mode, add_optab, op0, op0,
3188 target, unsignedp, OPTAB_LIB_WIDEN);
3192 /* This used to use umul_optab if unsigned, but for non-widening multiply
3193 there is no difference between signed and unsigned. */
3194 op0 = expand_binop (mode,
3195 ! unsignedp
3196 && flag_trapv && (GET_MODE_CLASS(mode) == MODE_INT)
3197 ? smulv_optab : smul_optab,
3198 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
3199 gcc_assert (op0);
3200 return op0;
3203 /* Return the smallest n such that 2**n >= X. */
3206 ceil_log2 (unsigned HOST_WIDE_INT x)
3208 return floor_log2 (x - 1) + 1;
3211 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3212 replace division by D, and put the least significant N bits of the result
3213 in *MULTIPLIER_PTR and return the most significant bit.
3215 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3216 needed precision is in PRECISION (should be <= N).
3218 PRECISION should be as small as possible so this function can choose
3219 multiplier more freely.
3221 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3222 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3224 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3225 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3227 static
3228 unsigned HOST_WIDE_INT
3229 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
3230 rtx *multiplier_ptr, int *post_shift_ptr, int *lgup_ptr)
3232 HOST_WIDE_INT mhigh_hi, mlow_hi;
3233 unsigned HOST_WIDE_INT mhigh_lo, mlow_lo;
3234 int lgup, post_shift;
3235 int pow, pow2;
3236 unsigned HOST_WIDE_INT nl, dummy1;
3237 HOST_WIDE_INT nh, dummy2;
3239 /* lgup = ceil(log2(divisor)); */
3240 lgup = ceil_log2 (d);
3242 gcc_assert (lgup <= n);
3244 pow = n + lgup;
3245 pow2 = n + lgup - precision;
3247 /* We could handle this with some effort, but this case is much
3248 better handled directly with a scc insn, so rely on caller using
3249 that. */
3250 gcc_assert (pow != 2 * HOST_BITS_PER_WIDE_INT);
3252 /* mlow = 2^(N + lgup)/d */
3253 if (pow >= HOST_BITS_PER_WIDE_INT)
3255 nh = (HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
3256 nl = 0;
3258 else
3260 nh = 0;
3261 nl = (unsigned HOST_WIDE_INT) 1 << pow;
3263 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
3264 &mlow_lo, &mlow_hi, &dummy1, &dummy2);
3266 /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
3267 if (pow2 >= HOST_BITS_PER_WIDE_INT)
3268 nh |= (HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
3269 else
3270 nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
3271 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
3272 &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
3274 gcc_assert (!mhigh_hi || nh - d < d);
3275 gcc_assert (mhigh_hi <= 1 && mlow_hi <= 1);
3276 /* Assert that mlow < mhigh. */
3277 gcc_assert (mlow_hi < mhigh_hi
3278 || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo));
3280 /* If precision == N, then mlow, mhigh exceed 2^N
3281 (but they do not exceed 2^(N+1)). */
3283 /* Reduce to lowest terms. */
3284 for (post_shift = lgup; post_shift > 0; post_shift--)
3286 unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
3287 unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
3288 if (ml_lo >= mh_lo)
3289 break;
3291 mlow_hi = 0;
3292 mlow_lo = ml_lo;
3293 mhigh_hi = 0;
3294 mhigh_lo = mh_lo;
3297 *post_shift_ptr = post_shift;
3298 *lgup_ptr = lgup;
3299 if (n < HOST_BITS_PER_WIDE_INT)
3301 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
3302 *multiplier_ptr = GEN_INT (mhigh_lo & mask);
3303 return mhigh_lo >= mask;
3305 else
3307 *multiplier_ptr = GEN_INT (mhigh_lo);
3308 return mhigh_hi;
3312 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3313 congruent to 1 (mod 2**N). */
3315 static unsigned HOST_WIDE_INT
3316 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
3318 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3320 /* The algorithm notes that the choice y = x satisfies
3321 x*y == 1 mod 2^3, since x is assumed odd.
3322 Each iteration doubles the number of bits of significance in y. */
3324 unsigned HOST_WIDE_INT mask;
3325 unsigned HOST_WIDE_INT y = x;
3326 int nbit = 3;
3328 mask = (n == HOST_BITS_PER_WIDE_INT
3329 ? ~(unsigned HOST_WIDE_INT) 0
3330 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
3332 while (nbit < n)
3334 y = y * (2 - x*y) & mask; /* Modulo 2^N */
3335 nbit *= 2;
3337 return y;
3340 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3341 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3342 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3343 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3344 become signed.
3346 The result is put in TARGET if that is convenient.
3348 MODE is the mode of operation. */
3351 expand_mult_highpart_adjust (enum machine_mode mode, rtx adj_operand, rtx op0,
3352 rtx op1, rtx target, int unsignedp)
3354 rtx tem;
3355 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3357 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3358 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3359 NULL_RTX, 0);
3360 tem = expand_and (mode, tem, op1, NULL_RTX);
3361 adj_operand
3362 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3363 adj_operand);
3365 tem = expand_shift (RSHIFT_EXPR, mode, op1,
3366 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3367 NULL_RTX, 0);
3368 tem = expand_and (mode, tem, op0, NULL_RTX);
3369 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3370 target);
3372 return target;
3375 /* Subroutine of expand_mult_highpart. Return the MODE high part of OP. */
3377 static rtx
3378 extract_high_half (enum machine_mode mode, rtx op)
3380 enum machine_mode wider_mode;
3382 if (mode == word_mode)
3383 return gen_highpart (mode, op);
3385 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3387 wider_mode = GET_MODE_WIDER_MODE (mode);
3388 op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3389 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode)), 0, 1);
3390 return convert_modes (mode, wider_mode, op, 0);
3393 /* Like expand_mult_highpart, but only consider using a multiplication
3394 optab. OP1 is an rtx for the constant operand. */
3396 static rtx
3397 expand_mult_highpart_optab (enum machine_mode mode, rtx op0, rtx op1,
3398 rtx target, int unsignedp, int max_cost)
3400 rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3401 enum machine_mode wider_mode;
3402 optab moptab;
3403 rtx tem;
3404 int size;
3406 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3408 wider_mode = GET_MODE_WIDER_MODE (mode);
3409 size = GET_MODE_BITSIZE (mode);
3411 /* Firstly, try using a multiplication insn that only generates the needed
3412 high part of the product, and in the sign flavor of unsignedp. */
3413 if (mul_highpart_cost[mode] < max_cost)
3415 moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3416 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3417 unsignedp, OPTAB_DIRECT);
3418 if (tem)
3419 return tem;
3422 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3423 Need to adjust the result after the multiplication. */
3424 if (size - 1 < BITS_PER_WORD
3425 && (mul_highpart_cost[mode] + 2 * shift_cost[mode][size-1]
3426 + 4 * add_cost[mode] < max_cost))
3428 moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3429 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3430 unsignedp, OPTAB_DIRECT);
3431 if (tem)
3432 /* We used the wrong signedness. Adjust the result. */
3433 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3434 tem, unsignedp);
3437 /* Try widening multiplication. */
3438 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3439 if (moptab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3440 && mul_widen_cost[wider_mode] < max_cost)
3442 tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3443 unsignedp, OPTAB_WIDEN);
3444 if (tem)
3445 return extract_high_half (mode, tem);
3448 /* Try widening the mode and perform a non-widening multiplication. */
3449 if (smul_optab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3450 && size - 1 < BITS_PER_WORD
3451 && mul_cost[wider_mode] + shift_cost[mode][size-1] < max_cost)
3453 rtx insns, wop0, wop1;
3455 /* We need to widen the operands, for example to ensure the
3456 constant multiplier is correctly sign or zero extended.
3457 Use a sequence to clean-up any instructions emitted by
3458 the conversions if things don't work out. */
3459 start_sequence ();
3460 wop0 = convert_modes (wider_mode, mode, op0, unsignedp);
3461 wop1 = convert_modes (wider_mode, mode, op1, unsignedp);
3462 tem = expand_binop (wider_mode, smul_optab, wop0, wop1, 0,
3463 unsignedp, OPTAB_WIDEN);
3464 insns = get_insns ();
3465 end_sequence ();
3467 if (tem)
3469 emit_insn (insns);
3470 return extract_high_half (mode, tem);
3474 /* Try widening multiplication of opposite signedness, and adjust. */
3475 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3476 if (moptab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3477 && size - 1 < BITS_PER_WORD
3478 && (mul_widen_cost[wider_mode] + 2 * shift_cost[mode][size-1]
3479 + 4 * add_cost[mode] < max_cost))
3481 tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3482 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3483 if (tem != 0)
3485 tem = extract_high_half (mode, tem);
3486 /* We used the wrong signedness. Adjust the result. */
3487 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3488 target, unsignedp);
3492 return 0;
3495 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3496 putting the high half of the result in TARGET if that is convenient,
3497 and return where the result is. If the operation can not be performed,
3498 0 is returned.
3500 MODE is the mode of operation and result.
3502 UNSIGNEDP nonzero means unsigned multiply.
3504 MAX_COST is the total allowed cost for the expanded RTL. */
3506 static rtx
3507 expand_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
3508 rtx target, int unsignedp, int max_cost)
3510 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
3511 unsigned HOST_WIDE_INT cnst1;
3512 int extra_cost;
3513 bool sign_adjust = false;
3514 enum mult_variant variant;
3515 struct algorithm alg;
3516 rtx tem;
3518 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3519 /* We can't support modes wider than HOST_BITS_PER_INT. */
3520 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT);
3522 cnst1 = INTVAL (op1) & GET_MODE_MASK (mode);
3524 /* We can't optimize modes wider than BITS_PER_WORD.
3525 ??? We might be able to perform double-word arithmetic if
3526 mode == word_mode, however all the cost calculations in
3527 synth_mult etc. assume single-word operations. */
3528 if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3529 return expand_mult_highpart_optab (mode, op0, op1, target,
3530 unsignedp, max_cost);
3532 extra_cost = shift_cost[mode][GET_MODE_BITSIZE (mode) - 1];
3534 /* Check whether we try to multiply by a negative constant. */
3535 if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3537 sign_adjust = true;
3538 extra_cost += add_cost[mode];
3541 /* See whether shift/add multiplication is cheap enough. */
3542 if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3543 max_cost - extra_cost))
3545 /* See whether the specialized multiplication optabs are
3546 cheaper than the shift/add version. */
3547 tem = expand_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3548 alg.cost.cost + extra_cost);
3549 if (tem)
3550 return tem;
3552 tem = convert_to_mode (wider_mode, op0, unsignedp);
3553 tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3554 tem = extract_high_half (mode, tem);
3556 /* Adjust result for signedness. */
3557 if (sign_adjust)
3558 tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3560 return tem;
3562 return expand_mult_highpart_optab (mode, op0, op1, target,
3563 unsignedp, max_cost);
3567 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3569 static rtx
3570 expand_smod_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3572 unsigned HOST_WIDE_INT masklow, maskhigh;
3573 rtx result, temp, shift, label;
3574 int logd;
3576 logd = floor_log2 (d);
3577 result = gen_reg_rtx (mode);
3579 /* Avoid conditional branches when they're expensive. */
3580 if (BRANCH_COST >= 2
3581 && !optimize_size)
3583 rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3584 mode, 0, -1);
3585 if (signmask)
3587 signmask = force_reg (mode, signmask);
3588 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3589 shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3591 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3592 which instruction sequence to use. If logical right shifts
3593 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3594 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3596 temp = gen_rtx_LSHIFTRT (mode, result, shift);
3597 if (lshr_optab->handlers[mode].insn_code == CODE_FOR_nothing
3598 || rtx_cost (temp, SET) > COSTS_N_INSNS (2))
3600 temp = expand_binop (mode, xor_optab, op0, signmask,
3601 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3602 temp = expand_binop (mode, sub_optab, temp, signmask,
3603 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3604 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3605 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3606 temp = expand_binop (mode, xor_optab, temp, signmask,
3607 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3608 temp = expand_binop (mode, sub_optab, temp, signmask,
3609 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3611 else
3613 signmask = expand_binop (mode, lshr_optab, signmask, shift,
3614 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3615 signmask = force_reg (mode, signmask);
3617 temp = expand_binop (mode, add_optab, op0, signmask,
3618 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3619 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3620 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3621 temp = expand_binop (mode, sub_optab, temp, signmask,
3622 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3624 return temp;
3628 /* Mask contains the mode's signbit and the significant bits of the
3629 modulus. By including the signbit in the operation, many targets
3630 can avoid an explicit compare operation in the following comparison
3631 against zero. */
3633 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3634 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3636 masklow |= (HOST_WIDE_INT) -1 << (GET_MODE_BITSIZE (mode) - 1);
3637 maskhigh = -1;
3639 else
3640 maskhigh = (HOST_WIDE_INT) -1
3641 << (GET_MODE_BITSIZE (mode) - HOST_BITS_PER_WIDE_INT - 1);
3643 temp = expand_binop (mode, and_optab, op0,
3644 immed_double_const (masklow, maskhigh, mode),
3645 result, 1, OPTAB_LIB_WIDEN);
3646 if (temp != result)
3647 emit_move_insn (result, temp);
3649 label = gen_label_rtx ();
3650 do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3652 temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3653 0, OPTAB_LIB_WIDEN);
3654 masklow = (HOST_WIDE_INT) -1 << logd;
3655 maskhigh = -1;
3656 temp = expand_binop (mode, ior_optab, temp,
3657 immed_double_const (masklow, maskhigh, mode),
3658 result, 1, OPTAB_LIB_WIDEN);
3659 temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3660 0, OPTAB_LIB_WIDEN);
3661 if (temp != result)
3662 emit_move_insn (result, temp);
3663 emit_label (label);
3664 return result;
3667 /* Expand signed division of OP0 by a power of two D in mode MODE.
3668 This routine is only called for positive values of D. */
3670 static rtx
3671 expand_sdiv_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3673 rtx temp, label;
3674 tree shift;
3675 int logd;
3677 logd = floor_log2 (d);
3678 shift = build_int_cst (NULL_TREE, logd);
3680 if (d == 2 && BRANCH_COST >= 1)
3682 temp = gen_reg_rtx (mode);
3683 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3684 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3685 0, OPTAB_LIB_WIDEN);
3686 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3689 #ifdef HAVE_conditional_move
3690 if (BRANCH_COST >= 2)
3692 rtx temp2;
3694 /* ??? emit_conditional_move forces a stack adjustment via
3695 compare_from_rtx so, if the sequence is discarded, it will
3696 be lost. Do it now instead. */
3697 do_pending_stack_adjust ();
3699 start_sequence ();
3700 temp2 = copy_to_mode_reg (mode, op0);
3701 temp = expand_binop (mode, add_optab, temp2, GEN_INT (d-1),
3702 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3703 temp = force_reg (mode, temp);
3705 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3706 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3707 mode, temp, temp2, mode, 0);
3708 if (temp2)
3710 rtx seq = get_insns ();
3711 end_sequence ();
3712 emit_insn (seq);
3713 return expand_shift (RSHIFT_EXPR, mode, temp2, shift, NULL_RTX, 0);
3715 end_sequence ();
3717 #endif
3719 if (BRANCH_COST >= 2)
3721 int ushift = GET_MODE_BITSIZE (mode) - logd;
3723 temp = gen_reg_rtx (mode);
3724 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3725 if (shift_cost[mode][ushift] > COSTS_N_INSNS (1))
3726 temp = expand_binop (mode, and_optab, temp, GEN_INT (d - 1),
3727 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3728 else
3729 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3730 build_int_cst (NULL_TREE, ushift),
3731 NULL_RTX, 1);
3732 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3733 0, OPTAB_LIB_WIDEN);
3734 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3737 label = gen_label_rtx ();
3738 temp = copy_to_mode_reg (mode, op0);
3739 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3740 expand_inc (temp, GEN_INT (d - 1));
3741 emit_label (label);
3742 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3745 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3746 if that is convenient, and returning where the result is.
3747 You may request either the quotient or the remainder as the result;
3748 specify REM_FLAG nonzero to get the remainder.
3750 CODE is the expression code for which kind of division this is;
3751 it controls how rounding is done. MODE is the machine mode to use.
3752 UNSIGNEDP nonzero means do unsigned division. */
3754 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3755 and then correct it by or'ing in missing high bits
3756 if result of ANDI is nonzero.
3757 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3758 This could optimize to a bfexts instruction.
3759 But C doesn't use these operations, so their optimizations are
3760 left for later. */
3761 /* ??? For modulo, we don't actually need the highpart of the first product,
3762 the low part will do nicely. And for small divisors, the second multiply
3763 can also be a low-part only multiply or even be completely left out.
3764 E.g. to calculate the remainder of a division by 3 with a 32 bit
3765 multiply, multiply with 0x55555556 and extract the upper two bits;
3766 the result is exact for inputs up to 0x1fffffff.
3767 The input range can be reduced by using cross-sum rules.
3768 For odd divisors >= 3, the following table gives right shift counts
3769 so that if a number is shifted by an integer multiple of the given
3770 amount, the remainder stays the same:
3771 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3772 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3773 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3774 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3775 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3777 Cross-sum rules for even numbers can be derived by leaving as many bits
3778 to the right alone as the divisor has zeros to the right.
3779 E.g. if x is an unsigned 32 bit number:
3780 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3784 expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
3785 rtx op0, rtx op1, rtx target, int unsignedp)
3787 enum machine_mode compute_mode;
3788 rtx tquotient;
3789 rtx quotient = 0, remainder = 0;
3790 rtx last;
3791 int size;
3792 rtx insn, set;
3793 optab optab1, optab2;
3794 int op1_is_constant, op1_is_pow2 = 0;
3795 int max_cost, extra_cost;
3796 static HOST_WIDE_INT last_div_const = 0;
3797 static HOST_WIDE_INT ext_op1;
3799 op1_is_constant = GET_CODE (op1) == CONST_INT;
3800 if (op1_is_constant)
3802 ext_op1 = INTVAL (op1);
3803 if (unsignedp)
3804 ext_op1 &= GET_MODE_MASK (mode);
3805 op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
3806 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
3810 This is the structure of expand_divmod:
3812 First comes code to fix up the operands so we can perform the operations
3813 correctly and efficiently.
3815 Second comes a switch statement with code specific for each rounding mode.
3816 For some special operands this code emits all RTL for the desired
3817 operation, for other cases, it generates only a quotient and stores it in
3818 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3819 to indicate that it has not done anything.
3821 Last comes code that finishes the operation. If QUOTIENT is set and
3822 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3823 QUOTIENT is not set, it is computed using trunc rounding.
3825 We try to generate special code for division and remainder when OP1 is a
3826 constant. If |OP1| = 2**n we can use shifts and some other fast
3827 operations. For other values of OP1, we compute a carefully selected
3828 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3829 by m.
3831 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3832 half of the product. Different strategies for generating the product are
3833 implemented in expand_mult_highpart.
3835 If what we actually want is the remainder, we generate that by another
3836 by-constant multiplication and a subtraction. */
3838 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3839 code below will malfunction if we are, so check here and handle
3840 the special case if so. */
3841 if (op1 == const1_rtx)
3842 return rem_flag ? const0_rtx : op0;
3844 /* When dividing by -1, we could get an overflow.
3845 negv_optab can handle overflows. */
3846 if (! unsignedp && op1 == constm1_rtx)
3848 if (rem_flag)
3849 return const0_rtx;
3850 return expand_unop (mode, flag_trapv && GET_MODE_CLASS(mode) == MODE_INT
3851 ? negv_optab : neg_optab, op0, target, 0);
3854 if (target
3855 /* Don't use the function value register as a target
3856 since we have to read it as well as write it,
3857 and function-inlining gets confused by this. */
3858 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3859 /* Don't clobber an operand while doing a multi-step calculation. */
3860 || ((rem_flag || op1_is_constant)
3861 && (reg_mentioned_p (target, op0)
3862 || (MEM_P (op0) && MEM_P (target))))
3863 || reg_mentioned_p (target, op1)
3864 || (MEM_P (op1) && MEM_P (target))))
3865 target = 0;
3867 /* Get the mode in which to perform this computation. Normally it will
3868 be MODE, but sometimes we can't do the desired operation in MODE.
3869 If so, pick a wider mode in which we can do the operation. Convert
3870 to that mode at the start to avoid repeated conversions.
3872 First see what operations we need. These depend on the expression
3873 we are evaluating. (We assume that divxx3 insns exist under the
3874 same conditions that modxx3 insns and that these insns don't normally
3875 fail. If these assumptions are not correct, we may generate less
3876 efficient code in some cases.)
3878 Then see if we find a mode in which we can open-code that operation
3879 (either a division, modulus, or shift). Finally, check for the smallest
3880 mode for which we can do the operation with a library call. */
3882 /* We might want to refine this now that we have division-by-constant
3883 optimization. Since expand_mult_highpart tries so many variants, it is
3884 not straightforward to generalize this. Maybe we should make an array
3885 of possible modes in init_expmed? Save this for GCC 2.7. */
3887 optab1 = ((op1_is_pow2 && op1 != const0_rtx)
3888 ? (unsignedp ? lshr_optab : ashr_optab)
3889 : (unsignedp ? udiv_optab : sdiv_optab));
3890 optab2 = ((op1_is_pow2 && op1 != const0_rtx)
3891 ? optab1
3892 : (unsignedp ? udivmod_optab : sdivmod_optab));
3894 for (compute_mode = mode; compute_mode != VOIDmode;
3895 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3896 if (optab1->handlers[compute_mode].insn_code != CODE_FOR_nothing
3897 || optab2->handlers[compute_mode].insn_code != CODE_FOR_nothing)
3898 break;
3900 if (compute_mode == VOIDmode)
3901 for (compute_mode = mode; compute_mode != VOIDmode;
3902 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3903 if (optab1->handlers[compute_mode].libfunc
3904 || optab2->handlers[compute_mode].libfunc)
3905 break;
3907 /* If we still couldn't find a mode, use MODE, but expand_binop will
3908 probably die. */
3909 if (compute_mode == VOIDmode)
3910 compute_mode = mode;
3912 if (target && GET_MODE (target) == compute_mode)
3913 tquotient = target;
3914 else
3915 tquotient = gen_reg_rtx (compute_mode);
3917 size = GET_MODE_BITSIZE (compute_mode);
3918 #if 0
3919 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3920 (mode), and thereby get better code when OP1 is a constant. Do that
3921 later. It will require going over all usages of SIZE below. */
3922 size = GET_MODE_BITSIZE (mode);
3923 #endif
3925 /* Only deduct something for a REM if the last divide done was
3926 for a different constant. Then set the constant of the last
3927 divide. */
3928 max_cost = unsignedp ? udiv_cost[compute_mode] : sdiv_cost[compute_mode];
3929 if (rem_flag && ! (last_div_const != 0 && op1_is_constant
3930 && INTVAL (op1) == last_div_const))
3931 max_cost -= mul_cost[compute_mode] + add_cost[compute_mode];
3933 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
3935 /* Now convert to the best mode to use. */
3936 if (compute_mode != mode)
3938 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
3939 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
3941 /* convert_modes may have placed op1 into a register, so we
3942 must recompute the following. */
3943 op1_is_constant = GET_CODE (op1) == CONST_INT;
3944 op1_is_pow2 = (op1_is_constant
3945 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3946 || (! unsignedp
3947 && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
3950 /* If one of the operands is a volatile MEM, copy it into a register. */
3952 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
3953 op0 = force_reg (compute_mode, op0);
3954 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
3955 op1 = force_reg (compute_mode, op1);
3957 /* If we need the remainder or if OP1 is constant, we need to
3958 put OP0 in a register in case it has any queued subexpressions. */
3959 if (rem_flag || op1_is_constant)
3960 op0 = force_reg (compute_mode, op0);
3962 last = get_last_insn ();
3964 /* Promote floor rounding to trunc rounding for unsigned operations. */
3965 if (unsignedp)
3967 if (code == FLOOR_DIV_EXPR)
3968 code = TRUNC_DIV_EXPR;
3969 if (code == FLOOR_MOD_EXPR)
3970 code = TRUNC_MOD_EXPR;
3971 if (code == EXACT_DIV_EXPR && op1_is_pow2)
3972 code = TRUNC_DIV_EXPR;
3975 if (op1 != const0_rtx)
3976 switch (code)
3978 case TRUNC_MOD_EXPR:
3979 case TRUNC_DIV_EXPR:
3980 if (op1_is_constant)
3982 if (unsignedp)
3984 unsigned HOST_WIDE_INT mh;
3985 int pre_shift, post_shift;
3986 int dummy;
3987 rtx ml;
3988 unsigned HOST_WIDE_INT d = (INTVAL (op1)
3989 & GET_MODE_MASK (compute_mode));
3991 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3993 pre_shift = floor_log2 (d);
3994 if (rem_flag)
3996 remainder
3997 = expand_binop (compute_mode, and_optab, op0,
3998 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3999 remainder, 1,
4000 OPTAB_LIB_WIDEN);
4001 if (remainder)
4002 return gen_lowpart (mode, remainder);
4004 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4005 build_int_cst (NULL_TREE,
4006 pre_shift),
4007 tquotient, 1);
4009 else if (size <= HOST_BITS_PER_WIDE_INT)
4011 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
4013 /* Most significant bit of divisor is set; emit an scc
4014 insn. */
4015 quotient = emit_store_flag (tquotient, GEU, op0, op1,
4016 compute_mode, 1, 1);
4017 if (quotient == 0)
4018 goto fail1;
4020 else
4022 /* Find a suitable multiplier and right shift count
4023 instead of multiplying with D. */
4025 mh = choose_multiplier (d, size, size,
4026 &ml, &post_shift, &dummy);
4028 /* If the suggested multiplier is more than SIZE bits,
4029 we can do better for even divisors, using an
4030 initial right shift. */
4031 if (mh != 0 && (d & 1) == 0)
4033 pre_shift = floor_log2 (d & -d);
4034 mh = choose_multiplier (d >> pre_shift, size,
4035 size - pre_shift,
4036 &ml, &post_shift, &dummy);
4037 gcc_assert (!mh);
4039 else
4040 pre_shift = 0;
4042 if (mh != 0)
4044 rtx t1, t2, t3, t4;
4046 if (post_shift - 1 >= BITS_PER_WORD)
4047 goto fail1;
4049 extra_cost
4050 = (shift_cost[compute_mode][post_shift - 1]
4051 + shift_cost[compute_mode][1]
4052 + 2 * add_cost[compute_mode]);
4053 t1 = expand_mult_highpart (compute_mode, op0, ml,
4054 NULL_RTX, 1,
4055 max_cost - extra_cost);
4056 if (t1 == 0)
4057 goto fail1;
4058 t2 = force_operand (gen_rtx_MINUS (compute_mode,
4059 op0, t1),
4060 NULL_RTX);
4061 t3 = expand_shift
4062 (RSHIFT_EXPR, compute_mode, t2,
4063 build_int_cst (NULL_TREE, 1),
4064 NULL_RTX,1);
4065 t4 = force_operand (gen_rtx_PLUS (compute_mode,
4066 t1, t3),
4067 NULL_RTX);
4068 quotient = expand_shift
4069 (RSHIFT_EXPR, compute_mode, t4,
4070 build_int_cst (NULL_TREE, post_shift - 1),
4071 tquotient, 1);
4073 else
4075 rtx t1, t2;
4077 if (pre_shift >= BITS_PER_WORD
4078 || post_shift >= BITS_PER_WORD)
4079 goto fail1;
4081 t1 = expand_shift
4082 (RSHIFT_EXPR, compute_mode, op0,
4083 build_int_cst (NULL_TREE, pre_shift),
4084 NULL_RTX, 1);
4085 extra_cost
4086 = (shift_cost[compute_mode][pre_shift]
4087 + shift_cost[compute_mode][post_shift]);
4088 t2 = expand_mult_highpart (compute_mode, t1, ml,
4089 NULL_RTX, 1,
4090 max_cost - extra_cost);
4091 if (t2 == 0)
4092 goto fail1;
4093 quotient = expand_shift
4094 (RSHIFT_EXPR, compute_mode, t2,
4095 build_int_cst (NULL_TREE, post_shift),
4096 tquotient, 1);
4100 else /* Too wide mode to use tricky code */
4101 break;
4103 insn = get_last_insn ();
4104 if (insn != last
4105 && (set = single_set (insn)) != 0
4106 && SET_DEST (set) == quotient)
4107 set_unique_reg_note (insn,
4108 REG_EQUAL,
4109 gen_rtx_UDIV (compute_mode, op0, op1));
4111 else /* TRUNC_DIV, signed */
4113 unsigned HOST_WIDE_INT ml;
4114 int lgup, post_shift;
4115 rtx mlr;
4116 HOST_WIDE_INT d = INTVAL (op1);
4117 unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
4119 /* n rem d = n rem -d */
4120 if (rem_flag && d < 0)
4122 d = abs_d;
4123 op1 = gen_int_mode (abs_d, compute_mode);
4126 if (d == 1)
4127 quotient = op0;
4128 else if (d == -1)
4129 quotient = expand_unop (compute_mode, neg_optab, op0,
4130 tquotient, 0);
4131 else if (abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
4133 /* This case is not handled correctly below. */
4134 quotient = emit_store_flag (tquotient, EQ, op0, op1,
4135 compute_mode, 1, 1);
4136 if (quotient == 0)
4137 goto fail1;
4139 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
4140 && (rem_flag ? smod_pow2_cheap[compute_mode]
4141 : sdiv_pow2_cheap[compute_mode])
4142 /* We assume that cheap metric is true if the
4143 optab has an expander for this mode. */
4144 && (((rem_flag ? smod_optab : sdiv_optab)
4145 ->handlers[compute_mode].insn_code
4146 != CODE_FOR_nothing)
4147 || (sdivmod_optab->handlers[compute_mode]
4148 .insn_code != CODE_FOR_nothing)))
4150 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
4152 if (rem_flag)
4154 remainder = expand_smod_pow2 (compute_mode, op0, d);
4155 if (remainder)
4156 return gen_lowpart (mode, remainder);
4159 if (sdiv_pow2_cheap[compute_mode]
4160 && ((sdiv_optab->handlers[compute_mode].insn_code
4161 != CODE_FOR_nothing)
4162 || (sdivmod_optab->handlers[compute_mode].insn_code
4163 != CODE_FOR_nothing)))
4164 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
4165 compute_mode, op0,
4166 gen_int_mode (abs_d,
4167 compute_mode),
4168 NULL_RTX, 0);
4169 else
4170 quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
4172 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4173 negate the quotient. */
4174 if (d < 0)
4176 insn = get_last_insn ();
4177 if (insn != last
4178 && (set = single_set (insn)) != 0
4179 && SET_DEST (set) == quotient
4180 && abs_d < ((unsigned HOST_WIDE_INT) 1
4181 << (HOST_BITS_PER_WIDE_INT - 1)))
4182 set_unique_reg_note (insn,
4183 REG_EQUAL,
4184 gen_rtx_DIV (compute_mode,
4185 op0,
4186 GEN_INT
4187 (trunc_int_for_mode
4188 (abs_d,
4189 compute_mode))));
4191 quotient = expand_unop (compute_mode, neg_optab,
4192 quotient, quotient, 0);
4195 else if (size <= HOST_BITS_PER_WIDE_INT)
4197 choose_multiplier (abs_d, size, size - 1,
4198 &mlr, &post_shift, &lgup);
4199 ml = (unsigned HOST_WIDE_INT) INTVAL (mlr);
4200 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
4202 rtx t1, t2, t3;
4204 if (post_shift >= BITS_PER_WORD
4205 || size - 1 >= BITS_PER_WORD)
4206 goto fail1;
4208 extra_cost = (shift_cost[compute_mode][post_shift]
4209 + shift_cost[compute_mode][size - 1]
4210 + add_cost[compute_mode]);
4211 t1 = expand_mult_highpart (compute_mode, op0, mlr,
4212 NULL_RTX, 0,
4213 max_cost - extra_cost);
4214 if (t1 == 0)
4215 goto fail1;
4216 t2 = expand_shift
4217 (RSHIFT_EXPR, compute_mode, t1,
4218 build_int_cst (NULL_TREE, post_shift),
4219 NULL_RTX, 0);
4220 t3 = expand_shift
4221 (RSHIFT_EXPR, compute_mode, op0,
4222 build_int_cst (NULL_TREE, size - 1),
4223 NULL_RTX, 0);
4224 if (d < 0)
4225 quotient
4226 = force_operand (gen_rtx_MINUS (compute_mode,
4227 t3, t2),
4228 tquotient);
4229 else
4230 quotient
4231 = force_operand (gen_rtx_MINUS (compute_mode,
4232 t2, t3),
4233 tquotient);
4235 else
4237 rtx t1, t2, t3, t4;
4239 if (post_shift >= BITS_PER_WORD
4240 || size - 1 >= BITS_PER_WORD)
4241 goto fail1;
4243 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
4244 mlr = gen_int_mode (ml, compute_mode);
4245 extra_cost = (shift_cost[compute_mode][post_shift]
4246 + shift_cost[compute_mode][size - 1]
4247 + 2 * add_cost[compute_mode]);
4248 t1 = expand_mult_highpart (compute_mode, op0, mlr,
4249 NULL_RTX, 0,
4250 max_cost - extra_cost);
4251 if (t1 == 0)
4252 goto fail1;
4253 t2 = force_operand (gen_rtx_PLUS (compute_mode,
4254 t1, op0),
4255 NULL_RTX);
4256 t3 = expand_shift
4257 (RSHIFT_EXPR, compute_mode, t2,
4258 build_int_cst (NULL_TREE, post_shift),
4259 NULL_RTX, 0);
4260 t4 = expand_shift
4261 (RSHIFT_EXPR, compute_mode, op0,
4262 build_int_cst (NULL_TREE, size - 1),
4263 NULL_RTX, 0);
4264 if (d < 0)
4265 quotient
4266 = force_operand (gen_rtx_MINUS (compute_mode,
4267 t4, t3),
4268 tquotient);
4269 else
4270 quotient
4271 = force_operand (gen_rtx_MINUS (compute_mode,
4272 t3, t4),
4273 tquotient);
4276 else /* Too wide mode to use tricky code */
4277 break;
4279 insn = get_last_insn ();
4280 if (insn != last
4281 && (set = single_set (insn)) != 0
4282 && SET_DEST (set) == quotient)
4283 set_unique_reg_note (insn,
4284 REG_EQUAL,
4285 gen_rtx_DIV (compute_mode, op0, op1));
4287 break;
4289 fail1:
4290 delete_insns_since (last);
4291 break;
4293 case FLOOR_DIV_EXPR:
4294 case FLOOR_MOD_EXPR:
4295 /* We will come here only for signed operations. */
4296 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4298 unsigned HOST_WIDE_INT mh;
4299 int pre_shift, lgup, post_shift;
4300 HOST_WIDE_INT d = INTVAL (op1);
4301 rtx ml;
4303 if (d > 0)
4305 /* We could just as easily deal with negative constants here,
4306 but it does not seem worth the trouble for GCC 2.6. */
4307 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4309 pre_shift = floor_log2 (d);
4310 if (rem_flag)
4312 remainder = expand_binop (compute_mode, and_optab, op0,
4313 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4314 remainder, 0, OPTAB_LIB_WIDEN);
4315 if (remainder)
4316 return gen_lowpart (mode, remainder);
4318 quotient = expand_shift
4319 (RSHIFT_EXPR, compute_mode, op0,
4320 build_int_cst (NULL_TREE, pre_shift),
4321 tquotient, 0);
4323 else
4325 rtx t1, t2, t3, t4;
4327 mh = choose_multiplier (d, size, size - 1,
4328 &ml, &post_shift, &lgup);
4329 gcc_assert (!mh);
4331 if (post_shift < BITS_PER_WORD
4332 && size - 1 < BITS_PER_WORD)
4334 t1 = expand_shift
4335 (RSHIFT_EXPR, compute_mode, op0,
4336 build_int_cst (NULL_TREE, size - 1),
4337 NULL_RTX, 0);
4338 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
4339 NULL_RTX, 0, OPTAB_WIDEN);
4340 extra_cost = (shift_cost[compute_mode][post_shift]
4341 + shift_cost[compute_mode][size - 1]
4342 + 2 * add_cost[compute_mode]);
4343 t3 = expand_mult_highpart (compute_mode, t2, ml,
4344 NULL_RTX, 1,
4345 max_cost - extra_cost);
4346 if (t3 != 0)
4348 t4 = expand_shift
4349 (RSHIFT_EXPR, compute_mode, t3,
4350 build_int_cst (NULL_TREE, post_shift),
4351 NULL_RTX, 1);
4352 quotient = expand_binop (compute_mode, xor_optab,
4353 t4, t1, tquotient, 0,
4354 OPTAB_WIDEN);
4359 else
4361 rtx nsign, t1, t2, t3, t4;
4362 t1 = force_operand (gen_rtx_PLUS (compute_mode,
4363 op0, constm1_rtx), NULL_RTX);
4364 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
4365 0, OPTAB_WIDEN);
4366 nsign = expand_shift
4367 (RSHIFT_EXPR, compute_mode, t2,
4368 build_int_cst (NULL_TREE, size - 1),
4369 NULL_RTX, 0);
4370 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
4371 NULL_RTX);
4372 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
4373 NULL_RTX, 0);
4374 if (t4)
4376 rtx t5;
4377 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
4378 NULL_RTX, 0);
4379 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4380 t4, t5),
4381 tquotient);
4386 if (quotient != 0)
4387 break;
4388 delete_insns_since (last);
4390 /* Try using an instruction that produces both the quotient and
4391 remainder, using truncation. We can easily compensate the quotient
4392 or remainder to get floor rounding, once we have the remainder.
4393 Notice that we compute also the final remainder value here,
4394 and return the result right away. */
4395 if (target == 0 || GET_MODE (target) != compute_mode)
4396 target = gen_reg_rtx (compute_mode);
4398 if (rem_flag)
4400 remainder
4401 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4402 quotient = gen_reg_rtx (compute_mode);
4404 else
4406 quotient
4407 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4408 remainder = gen_reg_rtx (compute_mode);
4411 if (expand_twoval_binop (sdivmod_optab, op0, op1,
4412 quotient, remainder, 0))
4414 /* This could be computed with a branch-less sequence.
4415 Save that for later. */
4416 rtx tem;
4417 rtx label = gen_label_rtx ();
4418 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4419 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4420 NULL_RTX, 0, OPTAB_WIDEN);
4421 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4422 expand_dec (quotient, const1_rtx);
4423 expand_inc (remainder, op1);
4424 emit_label (label);
4425 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4428 /* No luck with division elimination or divmod. Have to do it
4429 by conditionally adjusting op0 *and* the result. */
4431 rtx label1, label2, label3, label4, label5;
4432 rtx adjusted_op0;
4433 rtx tem;
4435 quotient = gen_reg_rtx (compute_mode);
4436 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4437 label1 = gen_label_rtx ();
4438 label2 = gen_label_rtx ();
4439 label3 = gen_label_rtx ();
4440 label4 = gen_label_rtx ();
4441 label5 = gen_label_rtx ();
4442 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4443 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4444 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4445 quotient, 0, OPTAB_LIB_WIDEN);
4446 if (tem != quotient)
4447 emit_move_insn (quotient, tem);
4448 emit_jump_insn (gen_jump (label5));
4449 emit_barrier ();
4450 emit_label (label1);
4451 expand_inc (adjusted_op0, const1_rtx);
4452 emit_jump_insn (gen_jump (label4));
4453 emit_barrier ();
4454 emit_label (label2);
4455 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4456 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4457 quotient, 0, OPTAB_LIB_WIDEN);
4458 if (tem != quotient)
4459 emit_move_insn (quotient, tem);
4460 emit_jump_insn (gen_jump (label5));
4461 emit_barrier ();
4462 emit_label (label3);
4463 expand_dec (adjusted_op0, const1_rtx);
4464 emit_label (label4);
4465 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4466 quotient, 0, OPTAB_LIB_WIDEN);
4467 if (tem != quotient)
4468 emit_move_insn (quotient, tem);
4469 expand_dec (quotient, const1_rtx);
4470 emit_label (label5);
4472 break;
4474 case CEIL_DIV_EXPR:
4475 case CEIL_MOD_EXPR:
4476 if (unsignedp)
4478 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
4480 rtx t1, t2, t3;
4481 unsigned HOST_WIDE_INT d = INTVAL (op1);
4482 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4483 build_int_cst (NULL_TREE, floor_log2 (d)),
4484 tquotient, 1);
4485 t2 = expand_binop (compute_mode, and_optab, op0,
4486 GEN_INT (d - 1),
4487 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4488 t3 = gen_reg_rtx (compute_mode);
4489 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4490 compute_mode, 1, 1);
4491 if (t3 == 0)
4493 rtx lab;
4494 lab = gen_label_rtx ();
4495 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4496 expand_inc (t1, const1_rtx);
4497 emit_label (lab);
4498 quotient = t1;
4500 else
4501 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4502 t1, t3),
4503 tquotient);
4504 break;
4507 /* Try using an instruction that produces both the quotient and
4508 remainder, using truncation. We can easily compensate the
4509 quotient or remainder to get ceiling rounding, once we have the
4510 remainder. Notice that we compute also the final remainder
4511 value here, and return the result right away. */
4512 if (target == 0 || GET_MODE (target) != compute_mode)
4513 target = gen_reg_rtx (compute_mode);
4515 if (rem_flag)
4517 remainder = (REG_P (target)
4518 ? target : gen_reg_rtx (compute_mode));
4519 quotient = gen_reg_rtx (compute_mode);
4521 else
4523 quotient = (REG_P (target)
4524 ? target : gen_reg_rtx (compute_mode));
4525 remainder = gen_reg_rtx (compute_mode);
4528 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4529 remainder, 1))
4531 /* This could be computed with a branch-less sequence.
4532 Save that for later. */
4533 rtx label = gen_label_rtx ();
4534 do_cmp_and_jump (remainder, const0_rtx, EQ,
4535 compute_mode, label);
4536 expand_inc (quotient, const1_rtx);
4537 expand_dec (remainder, op1);
4538 emit_label (label);
4539 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4542 /* No luck with division elimination or divmod. Have to do it
4543 by conditionally adjusting op0 *and* the result. */
4545 rtx label1, label2;
4546 rtx adjusted_op0, tem;
4548 quotient = gen_reg_rtx (compute_mode);
4549 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4550 label1 = gen_label_rtx ();
4551 label2 = gen_label_rtx ();
4552 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4553 compute_mode, label1);
4554 emit_move_insn (quotient, const0_rtx);
4555 emit_jump_insn (gen_jump (label2));
4556 emit_barrier ();
4557 emit_label (label1);
4558 expand_dec (adjusted_op0, const1_rtx);
4559 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4560 quotient, 1, OPTAB_LIB_WIDEN);
4561 if (tem != quotient)
4562 emit_move_insn (quotient, tem);
4563 expand_inc (quotient, const1_rtx);
4564 emit_label (label2);
4567 else /* signed */
4569 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4570 && INTVAL (op1) >= 0)
4572 /* This is extremely similar to the code for the unsigned case
4573 above. For 2.7 we should merge these variants, but for
4574 2.6.1 I don't want to touch the code for unsigned since that
4575 get used in C. The signed case will only be used by other
4576 languages (Ada). */
4578 rtx t1, t2, t3;
4579 unsigned HOST_WIDE_INT d = INTVAL (op1);
4580 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4581 build_int_cst (NULL_TREE, floor_log2 (d)),
4582 tquotient, 0);
4583 t2 = expand_binop (compute_mode, and_optab, op0,
4584 GEN_INT (d - 1),
4585 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4586 t3 = gen_reg_rtx (compute_mode);
4587 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4588 compute_mode, 1, 1);
4589 if (t3 == 0)
4591 rtx lab;
4592 lab = gen_label_rtx ();
4593 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4594 expand_inc (t1, const1_rtx);
4595 emit_label (lab);
4596 quotient = t1;
4598 else
4599 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4600 t1, t3),
4601 tquotient);
4602 break;
4605 /* Try using an instruction that produces both the quotient and
4606 remainder, using truncation. We can easily compensate the
4607 quotient or remainder to get ceiling rounding, once we have the
4608 remainder. Notice that we compute also the final remainder
4609 value here, and return the result right away. */
4610 if (target == 0 || GET_MODE (target) != compute_mode)
4611 target = gen_reg_rtx (compute_mode);
4612 if (rem_flag)
4614 remainder= (REG_P (target)
4615 ? target : gen_reg_rtx (compute_mode));
4616 quotient = gen_reg_rtx (compute_mode);
4618 else
4620 quotient = (REG_P (target)
4621 ? target : gen_reg_rtx (compute_mode));
4622 remainder = gen_reg_rtx (compute_mode);
4625 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4626 remainder, 0))
4628 /* This could be computed with a branch-less sequence.
4629 Save that for later. */
4630 rtx tem;
4631 rtx label = gen_label_rtx ();
4632 do_cmp_and_jump (remainder, const0_rtx, EQ,
4633 compute_mode, label);
4634 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4635 NULL_RTX, 0, OPTAB_WIDEN);
4636 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4637 expand_inc (quotient, const1_rtx);
4638 expand_dec (remainder, op1);
4639 emit_label (label);
4640 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4643 /* No luck with division elimination or divmod. Have to do it
4644 by conditionally adjusting op0 *and* the result. */
4646 rtx label1, label2, label3, label4, label5;
4647 rtx adjusted_op0;
4648 rtx tem;
4650 quotient = gen_reg_rtx (compute_mode);
4651 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4652 label1 = gen_label_rtx ();
4653 label2 = gen_label_rtx ();
4654 label3 = gen_label_rtx ();
4655 label4 = gen_label_rtx ();
4656 label5 = gen_label_rtx ();
4657 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4658 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4659 compute_mode, label1);
4660 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4661 quotient, 0, OPTAB_LIB_WIDEN);
4662 if (tem != quotient)
4663 emit_move_insn (quotient, tem);
4664 emit_jump_insn (gen_jump (label5));
4665 emit_barrier ();
4666 emit_label (label1);
4667 expand_dec (adjusted_op0, const1_rtx);
4668 emit_jump_insn (gen_jump (label4));
4669 emit_barrier ();
4670 emit_label (label2);
4671 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4672 compute_mode, label3);
4673 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4674 quotient, 0, OPTAB_LIB_WIDEN);
4675 if (tem != quotient)
4676 emit_move_insn (quotient, tem);
4677 emit_jump_insn (gen_jump (label5));
4678 emit_barrier ();
4679 emit_label (label3);
4680 expand_inc (adjusted_op0, const1_rtx);
4681 emit_label (label4);
4682 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4683 quotient, 0, OPTAB_LIB_WIDEN);
4684 if (tem != quotient)
4685 emit_move_insn (quotient, tem);
4686 expand_inc (quotient, const1_rtx);
4687 emit_label (label5);
4690 break;
4692 case EXACT_DIV_EXPR:
4693 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4695 HOST_WIDE_INT d = INTVAL (op1);
4696 unsigned HOST_WIDE_INT ml;
4697 int pre_shift;
4698 rtx t1;
4700 pre_shift = floor_log2 (d & -d);
4701 ml = invert_mod2n (d >> pre_shift, size);
4702 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4703 build_int_cst (NULL_TREE, pre_shift),
4704 NULL_RTX, unsignedp);
4705 quotient = expand_mult (compute_mode, t1,
4706 gen_int_mode (ml, compute_mode),
4707 NULL_RTX, 1);
4709 insn = get_last_insn ();
4710 set_unique_reg_note (insn,
4711 REG_EQUAL,
4712 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4713 compute_mode,
4714 op0, op1));
4716 break;
4718 case ROUND_DIV_EXPR:
4719 case ROUND_MOD_EXPR:
4720 if (unsignedp)
4722 rtx tem;
4723 rtx label;
4724 label = gen_label_rtx ();
4725 quotient = gen_reg_rtx (compute_mode);
4726 remainder = gen_reg_rtx (compute_mode);
4727 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4729 rtx tem;
4730 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
4731 quotient, 1, OPTAB_LIB_WIDEN);
4732 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
4733 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4734 remainder, 1, OPTAB_LIB_WIDEN);
4736 tem = plus_constant (op1, -1);
4737 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4738 build_int_cst (NULL_TREE, 1),
4739 NULL_RTX, 1);
4740 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
4741 expand_inc (quotient, const1_rtx);
4742 expand_dec (remainder, op1);
4743 emit_label (label);
4745 else
4747 rtx abs_rem, abs_op1, tem, mask;
4748 rtx label;
4749 label = gen_label_rtx ();
4750 quotient = gen_reg_rtx (compute_mode);
4751 remainder = gen_reg_rtx (compute_mode);
4752 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
4754 rtx tem;
4755 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
4756 quotient, 0, OPTAB_LIB_WIDEN);
4757 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
4758 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4759 remainder, 0, OPTAB_LIB_WIDEN);
4761 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
4762 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
4763 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
4764 build_int_cst (NULL_TREE, 1),
4765 NULL_RTX, 1);
4766 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
4767 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4768 NULL_RTX, 0, OPTAB_WIDEN);
4769 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4770 build_int_cst (NULL_TREE, size - 1),
4771 NULL_RTX, 0);
4772 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
4773 NULL_RTX, 0, OPTAB_WIDEN);
4774 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4775 NULL_RTX, 0, OPTAB_WIDEN);
4776 expand_inc (quotient, tem);
4777 tem = expand_binop (compute_mode, xor_optab, mask, op1,
4778 NULL_RTX, 0, OPTAB_WIDEN);
4779 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4780 NULL_RTX, 0, OPTAB_WIDEN);
4781 expand_dec (remainder, tem);
4782 emit_label (label);
4784 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4786 default:
4787 gcc_unreachable ();
4790 if (quotient == 0)
4792 if (target && GET_MODE (target) != compute_mode)
4793 target = 0;
4795 if (rem_flag)
4797 /* Try to produce the remainder without producing the quotient.
4798 If we seem to have a divmod pattern that does not require widening,
4799 don't try widening here. We should really have a WIDEN argument
4800 to expand_twoval_binop, since what we'd really like to do here is
4801 1) try a mod insn in compute_mode
4802 2) try a divmod insn in compute_mode
4803 3) try a div insn in compute_mode and multiply-subtract to get
4804 remainder
4805 4) try the same things with widening allowed. */
4806 remainder
4807 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4808 op0, op1, target,
4809 unsignedp,
4810 ((optab2->handlers[compute_mode].insn_code
4811 != CODE_FOR_nothing)
4812 ? OPTAB_DIRECT : OPTAB_WIDEN));
4813 if (remainder == 0)
4815 /* No luck there. Can we do remainder and divide at once
4816 without a library call? */
4817 remainder = gen_reg_rtx (compute_mode);
4818 if (! expand_twoval_binop ((unsignedp
4819 ? udivmod_optab
4820 : sdivmod_optab),
4821 op0, op1,
4822 NULL_RTX, remainder, unsignedp))
4823 remainder = 0;
4826 if (remainder)
4827 return gen_lowpart (mode, remainder);
4830 /* Produce the quotient. Try a quotient insn, but not a library call.
4831 If we have a divmod in this mode, use it in preference to widening
4832 the div (for this test we assume it will not fail). Note that optab2
4833 is set to the one of the two optabs that the call below will use. */
4834 quotient
4835 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
4836 op0, op1, rem_flag ? NULL_RTX : target,
4837 unsignedp,
4838 ((optab2->handlers[compute_mode].insn_code
4839 != CODE_FOR_nothing)
4840 ? OPTAB_DIRECT : OPTAB_WIDEN));
4842 if (quotient == 0)
4844 /* No luck there. Try a quotient-and-remainder insn,
4845 keeping the quotient alone. */
4846 quotient = gen_reg_rtx (compute_mode);
4847 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
4848 op0, op1,
4849 quotient, NULL_RTX, unsignedp))
4851 quotient = 0;
4852 if (! rem_flag)
4853 /* Still no luck. If we are not computing the remainder,
4854 use a library call for the quotient. */
4855 quotient = sign_expand_binop (compute_mode,
4856 udiv_optab, sdiv_optab,
4857 op0, op1, target,
4858 unsignedp, OPTAB_LIB_WIDEN);
4863 if (rem_flag)
4865 if (target && GET_MODE (target) != compute_mode)
4866 target = 0;
4868 if (quotient == 0)
4870 /* No divide instruction either. Use library for remainder. */
4871 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4872 op0, op1, target,
4873 unsignedp, OPTAB_LIB_WIDEN);
4874 /* No remainder function. Try a quotient-and-remainder
4875 function, keeping the remainder. */
4876 if (!remainder)
4878 remainder = gen_reg_rtx (compute_mode);
4879 if (!expand_twoval_binop_libfunc
4880 (unsignedp ? udivmod_optab : sdivmod_optab,
4881 op0, op1,
4882 NULL_RTX, remainder,
4883 unsignedp ? UMOD : MOD))
4884 remainder = NULL_RTX;
4887 else
4889 /* We divided. Now finish doing X - Y * (X / Y). */
4890 remainder = expand_mult (compute_mode, quotient, op1,
4891 NULL_RTX, unsignedp);
4892 remainder = expand_binop (compute_mode, sub_optab, op0,
4893 remainder, target, unsignedp,
4894 OPTAB_LIB_WIDEN);
4898 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4901 /* Return a tree node with data type TYPE, describing the value of X.
4902 Usually this is an VAR_DECL, if there is no obvious better choice.
4903 X may be an expression, however we only support those expressions
4904 generated by loop.c. */
4906 tree
4907 make_tree (tree type, rtx x)
4909 tree t;
4911 switch (GET_CODE (x))
4913 case CONST_INT:
4915 HOST_WIDE_INT hi = 0;
4917 if (INTVAL (x) < 0
4918 && !(TYPE_UNSIGNED (type)
4919 && (GET_MODE_BITSIZE (TYPE_MODE (type))
4920 < HOST_BITS_PER_WIDE_INT)))
4921 hi = -1;
4923 t = build_int_cst_wide (type, INTVAL (x), hi);
4925 return t;
4928 case CONST_DOUBLE:
4929 if (GET_MODE (x) == VOIDmode)
4930 t = build_int_cst_wide (type,
4931 CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
4932 else
4934 REAL_VALUE_TYPE d;
4936 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
4937 t = build_real (type, d);
4940 return t;
4942 case CONST_VECTOR:
4944 int i, units;
4945 rtx elt;
4946 tree t = NULL_TREE;
4948 units = CONST_VECTOR_NUNITS (x);
4950 /* Build a tree with vector elements. */
4951 for (i = units - 1; i >= 0; --i)
4953 elt = CONST_VECTOR_ELT (x, i);
4954 t = tree_cons (NULL_TREE, make_tree (type, elt), t);
4957 return build_vector (type, t);
4960 case PLUS:
4961 return fold_build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4962 make_tree (type, XEXP (x, 1)));
4964 case MINUS:
4965 return fold_build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4966 make_tree (type, XEXP (x, 1)));
4968 case NEG:
4969 return fold_build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0)));
4971 case MULT:
4972 return fold_build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
4973 make_tree (type, XEXP (x, 1)));
4975 case ASHIFT:
4976 return fold_build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
4977 make_tree (type, XEXP (x, 1)));
4979 case LSHIFTRT:
4980 t = lang_hooks.types.unsigned_type (type);
4981 return fold_convert (type, build2 (RSHIFT_EXPR, t,
4982 make_tree (t, XEXP (x, 0)),
4983 make_tree (type, XEXP (x, 1))));
4985 case ASHIFTRT:
4986 t = lang_hooks.types.signed_type (type);
4987 return fold_convert (type, build2 (RSHIFT_EXPR, t,
4988 make_tree (t, XEXP (x, 0)),
4989 make_tree (type, XEXP (x, 1))));
4991 case DIV:
4992 if (TREE_CODE (type) != REAL_TYPE)
4993 t = lang_hooks.types.signed_type (type);
4994 else
4995 t = type;
4997 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
4998 make_tree (t, XEXP (x, 0)),
4999 make_tree (t, XEXP (x, 1))));
5000 case UDIV:
5001 t = lang_hooks.types.unsigned_type (type);
5002 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5003 make_tree (t, XEXP (x, 0)),
5004 make_tree (t, XEXP (x, 1))));
5006 case SIGN_EXTEND:
5007 case ZERO_EXTEND:
5008 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
5009 GET_CODE (x) == ZERO_EXTEND);
5010 return fold_convert (type, make_tree (t, XEXP (x, 0)));
5012 default:
5013 t = build_decl (VAR_DECL, NULL_TREE, type);
5015 /* If TYPE is a POINTER_TYPE, X might be Pmode with TYPE_MODE being
5016 ptr_mode. So convert. */
5017 if (POINTER_TYPE_P (type))
5018 x = convert_memory_address (TYPE_MODE (type), x);
5020 /* Note that we do *not* use SET_DECL_RTL here, because we do not
5021 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
5022 t->decl_with_rtl.rtl = x;
5024 return t;
5028 /* Return an rtx representing the value of X * MULT + ADD.
5029 TARGET is a suggestion for where to store the result (an rtx).
5030 MODE is the machine mode for the computation.
5031 X and MULT must have mode MODE. ADD may have a different mode.
5032 So can X (defaults to same as MODE).
5033 UNSIGNEDP is nonzero to do unsigned multiplication.
5034 This may emit insns. */
5037 expand_mult_add (rtx x, rtx target, rtx mult, rtx add, enum machine_mode mode,
5038 int unsignedp)
5040 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
5041 tree add_type = (GET_MODE (add) == VOIDmode
5042 ? type: lang_hooks.types.type_for_mode (GET_MODE (add),
5043 unsignedp));
5044 tree result = fold_build2 (PLUS_EXPR, type,
5045 fold_build2 (MULT_EXPR, type,
5046 make_tree (type, x),
5047 make_tree (type, mult)),
5048 make_tree (add_type, add));
5050 return expand_expr (result, target, VOIDmode, 0);
5053 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5054 and returning TARGET.
5056 If TARGET is 0, a pseudo-register or constant is returned. */
5059 expand_and (enum machine_mode mode, rtx op0, rtx op1, rtx target)
5061 rtx tem = 0;
5063 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
5064 tem = simplify_binary_operation (AND, mode, op0, op1);
5065 if (tem == 0)
5066 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
5068 if (target == 0)
5069 target = tem;
5070 else if (tem != target)
5071 emit_move_insn (target, tem);
5072 return target;
5075 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5076 and storing in TARGET. Normally return TARGET.
5077 Return 0 if that cannot be done.
5079 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5080 it is VOIDmode, they cannot both be CONST_INT.
5082 UNSIGNEDP is for the case where we have to widen the operands
5083 to perform the operation. It says to use zero-extension.
5085 NORMALIZEP is 1 if we should convert the result to be either zero
5086 or one. Normalize is -1 if we should convert the result to be
5087 either zero or -1. If NORMALIZEP is zero, the result will be left
5088 "raw" out of the scc insn. */
5091 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
5092 enum machine_mode mode, int unsignedp, int normalizep)
5094 rtx subtarget;
5095 enum insn_code icode;
5096 enum machine_mode compare_mode;
5097 enum machine_mode target_mode = GET_MODE (target);
5098 rtx tem;
5099 rtx last = get_last_insn ();
5100 rtx pattern, comparison;
5102 if (unsignedp)
5103 code = unsigned_condition (code);
5105 /* If one operand is constant, make it the second one. Only do this
5106 if the other operand is not constant as well. */
5108 if (swap_commutative_operands_p (op0, op1))
5110 tem = op0;
5111 op0 = op1;
5112 op1 = tem;
5113 code = swap_condition (code);
5116 if (mode == VOIDmode)
5117 mode = GET_MODE (op0);
5119 /* For some comparisons with 1 and -1, we can convert this to
5120 comparisons with zero. This will often produce more opportunities for
5121 store-flag insns. */
5123 switch (code)
5125 case LT:
5126 if (op1 == const1_rtx)
5127 op1 = const0_rtx, code = LE;
5128 break;
5129 case LE:
5130 if (op1 == constm1_rtx)
5131 op1 = const0_rtx, code = LT;
5132 break;
5133 case GE:
5134 if (op1 == const1_rtx)
5135 op1 = const0_rtx, code = GT;
5136 break;
5137 case GT:
5138 if (op1 == constm1_rtx)
5139 op1 = const0_rtx, code = GE;
5140 break;
5141 case GEU:
5142 if (op1 == const1_rtx)
5143 op1 = const0_rtx, code = NE;
5144 break;
5145 case LTU:
5146 if (op1 == const1_rtx)
5147 op1 = const0_rtx, code = EQ;
5148 break;
5149 default:
5150 break;
5153 /* If we are comparing a double-word integer with zero or -1, we can
5154 convert the comparison into one involving a single word. */
5155 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
5156 && GET_MODE_CLASS (mode) == MODE_INT
5157 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
5159 if ((code == EQ || code == NE)
5160 && (op1 == const0_rtx || op1 == constm1_rtx))
5162 rtx op00, op01, op0both;
5164 /* Do a logical OR or AND of the two words and compare the result. */
5165 op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
5166 op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
5167 op0both = expand_binop (word_mode,
5168 op1 == const0_rtx ? ior_optab : and_optab,
5169 op00, op01, NULL_RTX, unsignedp, OPTAB_DIRECT);
5171 if (op0both != 0)
5172 return emit_store_flag (target, code, op0both, op1, word_mode,
5173 unsignedp, normalizep);
5175 else if ((code == LT || code == GE) && op1 == const0_rtx)
5177 rtx op0h;
5179 /* If testing the sign bit, can just test on high word. */
5180 op0h = simplify_gen_subreg (word_mode, op0, mode,
5181 subreg_highpart_offset (word_mode, mode));
5182 return emit_store_flag (target, code, op0h, op1, word_mode,
5183 unsignedp, normalizep);
5187 /* From now on, we won't change CODE, so set ICODE now. */
5188 icode = setcc_gen_code[(int) code];
5190 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5191 complement of A (for GE) and shifting the sign bit to the low bit. */
5192 if (op1 == const0_rtx && (code == LT || code == GE)
5193 && GET_MODE_CLASS (mode) == MODE_INT
5194 && (normalizep || STORE_FLAG_VALUE == 1
5195 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5196 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5197 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
5199 subtarget = target;
5201 /* If the result is to be wider than OP0, it is best to convert it
5202 first. If it is to be narrower, it is *incorrect* to convert it
5203 first. */
5204 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
5206 op0 = convert_modes (target_mode, mode, op0, 0);
5207 mode = target_mode;
5210 if (target_mode != mode)
5211 subtarget = 0;
5213 if (code == GE)
5214 op0 = expand_unop (mode, one_cmpl_optab, op0,
5215 ((STORE_FLAG_VALUE == 1 || normalizep)
5216 ? 0 : subtarget), 0);
5218 if (STORE_FLAG_VALUE == 1 || normalizep)
5219 /* If we are supposed to produce a 0/1 value, we want to do
5220 a logical shift from the sign bit to the low-order bit; for
5221 a -1/0 value, we do an arithmetic shift. */
5222 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
5223 size_int (GET_MODE_BITSIZE (mode) - 1),
5224 subtarget, normalizep != -1);
5226 if (mode != target_mode)
5227 op0 = convert_modes (target_mode, mode, op0, 0);
5229 return op0;
5232 if (icode != CODE_FOR_nothing)
5234 insn_operand_predicate_fn pred;
5236 /* We think we may be able to do this with a scc insn. Emit the
5237 comparison and then the scc insn. */
5239 do_pending_stack_adjust ();
5240 last = get_last_insn ();
5242 comparison
5243 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX);
5244 if (CONSTANT_P (comparison))
5246 switch (GET_CODE (comparison))
5248 case CONST_INT:
5249 if (comparison == const0_rtx)
5250 return const0_rtx;
5251 break;
5253 #ifdef FLOAT_STORE_FLAG_VALUE
5254 case CONST_DOUBLE:
5255 if (comparison == CONST0_RTX (GET_MODE (comparison)))
5256 return const0_rtx;
5257 break;
5258 #endif
5259 default:
5260 gcc_unreachable ();
5263 if (normalizep == 1)
5264 return const1_rtx;
5265 if (normalizep == -1)
5266 return constm1_rtx;
5267 return const_true_rtx;
5270 /* The code of COMPARISON may not match CODE if compare_from_rtx
5271 decided to swap its operands and reverse the original code.
5273 We know that compare_from_rtx returns either a CONST_INT or
5274 a new comparison code, so it is safe to just extract the
5275 code from COMPARISON. */
5276 code = GET_CODE (comparison);
5278 /* Get a reference to the target in the proper mode for this insn. */
5279 compare_mode = insn_data[(int) icode].operand[0].mode;
5280 subtarget = target;
5281 pred = insn_data[(int) icode].operand[0].predicate;
5282 if (optimize || ! (*pred) (subtarget, compare_mode))
5283 subtarget = gen_reg_rtx (compare_mode);
5285 pattern = GEN_FCN (icode) (subtarget);
5286 if (pattern)
5288 emit_insn (pattern);
5290 /* If we are converting to a wider mode, first convert to
5291 TARGET_MODE, then normalize. This produces better combining
5292 opportunities on machines that have a SIGN_EXTRACT when we are
5293 testing a single bit. This mostly benefits the 68k.
5295 If STORE_FLAG_VALUE does not have the sign bit set when
5296 interpreted in COMPARE_MODE, we can do this conversion as
5297 unsigned, which is usually more efficient. */
5298 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
5300 convert_move (target, subtarget,
5301 (GET_MODE_BITSIZE (compare_mode)
5302 <= HOST_BITS_PER_WIDE_INT)
5303 && 0 == (STORE_FLAG_VALUE
5304 & ((HOST_WIDE_INT) 1
5305 << (GET_MODE_BITSIZE (compare_mode) -1))));
5306 op0 = target;
5307 compare_mode = target_mode;
5309 else
5310 op0 = subtarget;
5312 /* If we want to keep subexpressions around, don't reuse our
5313 last target. */
5315 if (optimize)
5316 subtarget = 0;
5318 /* Now normalize to the proper value in COMPARE_MODE. Sometimes
5319 we don't have to do anything. */
5320 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
5322 /* STORE_FLAG_VALUE might be the most negative number, so write
5323 the comparison this way to avoid a compiler-time warning. */
5324 else if (- normalizep == STORE_FLAG_VALUE)
5325 op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
5327 /* We don't want to use STORE_FLAG_VALUE < 0 below since this
5328 makes it hard to use a value of just the sign bit due to
5329 ANSI integer constant typing rules. */
5330 else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
5331 && (STORE_FLAG_VALUE
5332 & ((HOST_WIDE_INT) 1
5333 << (GET_MODE_BITSIZE (compare_mode) - 1))))
5334 op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
5335 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
5336 subtarget, normalizep == 1);
5337 else
5339 gcc_assert (STORE_FLAG_VALUE & 1);
5341 op0 = expand_and (compare_mode, op0, const1_rtx, subtarget);
5342 if (normalizep == -1)
5343 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
5346 /* If we were converting to a smaller mode, do the
5347 conversion now. */
5348 if (target_mode != compare_mode)
5350 convert_move (target, op0, 0);
5351 return target;
5353 else
5354 return op0;
5358 delete_insns_since (last);
5360 /* If optimizing, use different pseudo registers for each insn, instead
5361 of reusing the same pseudo. This leads to better CSE, but slows
5362 down the compiler, since there are more pseudos */
5363 subtarget = (!optimize
5364 && (target_mode == mode)) ? target : NULL_RTX;
5366 /* If we reached here, we can't do this with a scc insn. However, there
5367 are some comparisons that can be done directly. For example, if
5368 this is an equality comparison of integers, we can try to exclusive-or
5369 (or subtract) the two operands and use a recursive call to try the
5370 comparison with zero. Don't do any of these cases if branches are
5371 very cheap. */
5373 if (BRANCH_COST > 0
5374 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
5375 && op1 != const0_rtx)
5377 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5378 OPTAB_WIDEN);
5380 if (tem == 0)
5381 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5382 OPTAB_WIDEN);
5383 if (tem != 0)
5384 tem = emit_store_flag (target, code, tem, const0_rtx,
5385 mode, unsignedp, normalizep);
5386 if (tem == 0)
5387 delete_insns_since (last);
5388 return tem;
5391 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5392 the constant zero. Reject all other comparisons at this point. Only
5393 do LE and GT if branches are expensive since they are expensive on
5394 2-operand machines. */
5396 if (BRANCH_COST == 0
5397 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
5398 || (code != EQ && code != NE
5399 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
5400 return 0;
5402 /* See what we need to return. We can only return a 1, -1, or the
5403 sign bit. */
5405 if (normalizep == 0)
5407 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5408 normalizep = STORE_FLAG_VALUE;
5410 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5411 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5412 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
5414 else
5415 return 0;
5418 /* Try to put the result of the comparison in the sign bit. Assume we can't
5419 do the necessary operation below. */
5421 tem = 0;
5423 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5424 the sign bit set. */
5426 if (code == LE)
5428 /* This is destructive, so SUBTARGET can't be OP0. */
5429 if (rtx_equal_p (subtarget, op0))
5430 subtarget = 0;
5432 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5433 OPTAB_WIDEN);
5434 if (tem)
5435 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5436 OPTAB_WIDEN);
5439 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5440 number of bits in the mode of OP0, minus one. */
5442 if (code == GT)
5444 if (rtx_equal_p (subtarget, op0))
5445 subtarget = 0;
5447 tem = expand_shift (RSHIFT_EXPR, mode, op0,
5448 size_int (GET_MODE_BITSIZE (mode) - 1),
5449 subtarget, 0);
5450 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5451 OPTAB_WIDEN);
5454 if (code == EQ || code == NE)
5456 /* For EQ or NE, one way to do the comparison is to apply an operation
5457 that converts the operand into a positive number if it is nonzero
5458 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5459 for NE we negate. This puts the result in the sign bit. Then we
5460 normalize with a shift, if needed.
5462 Two operations that can do the above actions are ABS and FFS, so try
5463 them. If that doesn't work, and MODE is smaller than a full word,
5464 we can use zero-extension to the wider mode (an unsigned conversion)
5465 as the operation. */
5467 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5468 that is compensated by the subsequent overflow when subtracting
5469 one / negating. */
5471 if (abs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5472 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5473 else if (ffs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5474 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5475 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5477 tem = convert_modes (word_mode, mode, op0, 1);
5478 mode = word_mode;
5481 if (tem != 0)
5483 if (code == EQ)
5484 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5485 0, OPTAB_WIDEN);
5486 else
5487 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5490 /* If we couldn't do it that way, for NE we can "or" the two's complement
5491 of the value with itself. For EQ, we take the one's complement of
5492 that "or", which is an extra insn, so we only handle EQ if branches
5493 are expensive. */
5495 if (tem == 0 && (code == NE || BRANCH_COST > 1))
5497 if (rtx_equal_p (subtarget, op0))
5498 subtarget = 0;
5500 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5501 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5502 OPTAB_WIDEN);
5504 if (tem && code == EQ)
5505 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5509 if (tem && normalizep)
5510 tem = expand_shift (RSHIFT_EXPR, mode, tem,
5511 size_int (GET_MODE_BITSIZE (mode) - 1),
5512 subtarget, normalizep == 1);
5514 if (tem)
5516 if (GET_MODE (tem) != target_mode)
5518 convert_move (target, tem, 0);
5519 tem = target;
5521 else if (!subtarget)
5523 emit_move_insn (target, tem);
5524 tem = target;
5527 else
5528 delete_insns_since (last);
5530 return tem;
5533 /* Like emit_store_flag, but always succeeds. */
5536 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5537 enum machine_mode mode, int unsignedp, int normalizep)
5539 rtx tem, label;
5541 /* First see if emit_store_flag can do the job. */
5542 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5543 if (tem != 0)
5544 return tem;
5546 if (normalizep == 0)
5547 normalizep = 1;
5549 /* If this failed, we have to do this with set/compare/jump/set code. */
5551 if (!REG_P (target)
5552 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5553 target = gen_reg_rtx (GET_MODE (target));
5555 emit_move_insn (target, const1_rtx);
5556 label = gen_label_rtx ();
5557 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
5558 NULL_RTX, label);
5560 emit_move_insn (target, const0_rtx);
5561 emit_label (label);
5563 return target;
5566 /* Perform possibly multi-word comparison and conditional jump to LABEL
5567 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE
5569 The algorithm is based on the code in expr.c:do_jump.
5571 Note that this does not perform a general comparison. Only
5572 variants generated within expmed.c are correctly handled, others
5573 could be handled if needed. */
5575 static void
5576 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, enum machine_mode mode,
5577 rtx label)
5579 /* If this mode is an integer too wide to compare properly,
5580 compare word by word. Rely on cse to optimize constant cases. */
5582 if (GET_MODE_CLASS (mode) == MODE_INT
5583 && ! can_compare_p (op, mode, ccp_jump))
5585 rtx label2 = gen_label_rtx ();
5587 switch (op)
5589 case LTU:
5590 do_jump_by_parts_greater_rtx (mode, 1, arg2, arg1, label2, label);
5591 break;
5593 case LEU:
5594 do_jump_by_parts_greater_rtx (mode, 1, arg1, arg2, label, label2);
5595 break;
5597 case LT:
5598 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label2, label);
5599 break;
5601 case GT:
5602 do_jump_by_parts_greater_rtx (mode, 0, arg1, arg2, label2, label);
5603 break;
5605 case GE:
5606 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label, label2);
5607 break;
5609 /* do_jump_by_parts_equality_rtx compares with zero. Luckily
5610 that's the only equality operations we do */
5611 case EQ:
5612 gcc_assert (arg2 == const0_rtx && mode == GET_MODE(arg1));
5613 do_jump_by_parts_equality_rtx (arg1, label2, label);
5614 break;
5616 case NE:
5617 gcc_assert (arg2 == const0_rtx && mode == GET_MODE(arg1));
5618 do_jump_by_parts_equality_rtx (arg1, label, label2);
5619 break;
5621 default:
5622 gcc_unreachable ();
5625 emit_label (label2);
5627 else
5628 emit_cmp_and_jump_insns (arg1, arg2, op, NULL_RTX, mode, 0, label);