* config/arm/symbian.h (STARTFILE_SPEC): Remove crt*.o.
[official-gcc.git] / gcc / expmed.c
blob232381f2c5ab2210f8ab685d89430a50ff8f2be7
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 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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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 /* Nonzero means divides or modulus operations are relatively cheap for
58 powers of two, so don't use branches; emit the operation instead.
59 Usually, this will mean that the MD file will emit non-branch
60 sequences. */
62 static bool sdiv_pow2_cheap[NUM_MACHINE_MODES];
63 static bool smod_pow2_cheap[NUM_MACHINE_MODES];
65 #ifndef SLOW_UNALIGNED_ACCESS
66 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
67 #endif
69 /* For compilers that support multiple targets with different word sizes,
70 MAX_BITS_PER_WORD contains the biggest value of BITS_PER_WORD. An example
71 is the H8/300(H) compiler. */
73 #ifndef MAX_BITS_PER_WORD
74 #define MAX_BITS_PER_WORD BITS_PER_WORD
75 #endif
77 /* Reduce conditional compilation elsewhere. */
78 #ifndef HAVE_insv
79 #define HAVE_insv 0
80 #define CODE_FOR_insv CODE_FOR_nothing
81 #define gen_insv(a,b,c,d) NULL_RTX
82 #endif
83 #ifndef HAVE_extv
84 #define HAVE_extv 0
85 #define CODE_FOR_extv CODE_FOR_nothing
86 #define gen_extv(a,b,c,d) NULL_RTX
87 #endif
88 #ifndef HAVE_extzv
89 #define HAVE_extzv 0
90 #define CODE_FOR_extzv CODE_FOR_nothing
91 #define gen_extzv(a,b,c,d) NULL_RTX
92 #endif
94 /* Cost of various pieces of RTL. Note that some of these are indexed by
95 shift count and some by mode. */
96 static int zero_cost;
97 static int add_cost[NUM_MACHINE_MODES];
98 static int neg_cost[NUM_MACHINE_MODES];
99 static int shift_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
100 static int shiftadd_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
101 static int shiftsub_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
102 static int mul_cost[NUM_MACHINE_MODES];
103 static int div_cost[NUM_MACHINE_MODES];
104 static int mul_widen_cost[NUM_MACHINE_MODES];
105 static int mul_highpart_cost[NUM_MACHINE_MODES];
107 void
108 init_expmed (void)
110 struct
112 struct rtx_def reg; rtunion reg_fld[2];
113 struct rtx_def plus; rtunion plus_fld1;
114 struct rtx_def neg;
115 struct rtx_def udiv; rtunion udiv_fld1;
116 struct rtx_def mult; rtunion mult_fld1;
117 struct rtx_def div; rtunion div_fld1;
118 struct rtx_def mod; rtunion mod_fld1;
119 struct rtx_def zext;
120 struct rtx_def wide_mult; rtunion wide_mult_fld1;
121 struct rtx_def wide_lshr; rtunion wide_lshr_fld1;
122 struct rtx_def wide_trunc;
123 struct rtx_def shift; rtunion shift_fld1;
124 struct rtx_def shift_mult; rtunion shift_mult_fld1;
125 struct rtx_def shift_add; rtunion shift_add_fld1;
126 struct rtx_def shift_sub; rtunion shift_sub_fld1;
127 } all;
129 rtx pow2[MAX_BITS_PER_WORD];
130 rtx cint[MAX_BITS_PER_WORD];
131 int m, n;
132 enum machine_mode mode, wider_mode;
134 zero_cost = rtx_cost (const0_rtx, 0);
136 for (m = 1; m < MAX_BITS_PER_WORD; m++)
138 pow2[m] = GEN_INT ((HOST_WIDE_INT) 1 << m);
139 cint[m] = GEN_INT (m);
142 memset (&all, 0, sizeof all);
144 PUT_CODE (&all.reg, REG);
145 REGNO (&all.reg) = 10000;
147 PUT_CODE (&all.plus, PLUS);
148 XEXP (&all.plus, 0) = &all.reg;
149 XEXP (&all.plus, 1) = &all.reg;
151 PUT_CODE (&all.neg, NEG);
152 XEXP (&all.neg, 0) = &all.reg;
154 PUT_CODE (&all.udiv, UDIV);
155 XEXP (&all.udiv, 0) = &all.reg;
156 XEXP (&all.udiv, 1) = &all.reg;
158 PUT_CODE (&all.mult, MULT);
159 XEXP (&all.mult, 0) = &all.reg;
160 XEXP (&all.mult, 1) = &all.reg;
162 PUT_CODE (&all.div, DIV);
163 XEXP (&all.div, 0) = &all.reg;
164 XEXP (&all.div, 1) = 32 < MAX_BITS_PER_WORD ? cint[32] : GEN_INT (32);
166 PUT_CODE (&all.mod, MOD);
167 XEXP (&all.mod, 0) = &all.reg;
168 XEXP (&all.mod, 1) = XEXP (&all.div, 1);
170 PUT_CODE (&all.zext, ZERO_EXTEND);
171 XEXP (&all.zext, 0) = &all.reg;
173 PUT_CODE (&all.wide_mult, MULT);
174 XEXP (&all.wide_mult, 0) = &all.zext;
175 XEXP (&all.wide_mult, 1) = &all.zext;
177 PUT_CODE (&all.wide_lshr, LSHIFTRT);
178 XEXP (&all.wide_lshr, 0) = &all.wide_mult;
180 PUT_CODE (&all.wide_trunc, TRUNCATE);
181 XEXP (&all.wide_trunc, 0) = &all.wide_lshr;
183 PUT_CODE (&all.shift, ASHIFT);
184 XEXP (&all.shift, 0) = &all.reg;
186 PUT_CODE (&all.shift_mult, MULT);
187 XEXP (&all.shift_mult, 0) = &all.reg;
189 PUT_CODE (&all.shift_add, PLUS);
190 XEXP (&all.shift_add, 0) = &all.shift_mult;
191 XEXP (&all.shift_add, 1) = &all.reg;
193 PUT_CODE (&all.shift_sub, MINUS);
194 XEXP (&all.shift_sub, 0) = &all.shift_mult;
195 XEXP (&all.shift_sub, 1) = &all.reg;
197 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
198 mode != VOIDmode;
199 mode = GET_MODE_WIDER_MODE (mode))
201 PUT_MODE (&all.reg, mode);
202 PUT_MODE (&all.plus, mode);
203 PUT_MODE (&all.neg, mode);
204 PUT_MODE (&all.udiv, mode);
205 PUT_MODE (&all.mult, mode);
206 PUT_MODE (&all.div, mode);
207 PUT_MODE (&all.mod, mode);
208 PUT_MODE (&all.wide_trunc, mode);
209 PUT_MODE (&all.shift, mode);
210 PUT_MODE (&all.shift_mult, mode);
211 PUT_MODE (&all.shift_add, mode);
212 PUT_MODE (&all.shift_sub, mode);
214 add_cost[mode] = rtx_cost (&all.plus, SET);
215 neg_cost[mode] = rtx_cost (&all.neg, SET);
216 div_cost[mode] = rtx_cost (&all.udiv, SET);
217 mul_cost[mode] = rtx_cost (&all.mult, SET);
219 sdiv_pow2_cheap[mode] = (rtx_cost (&all.div, SET) <= 2 * add_cost[mode]);
220 smod_pow2_cheap[mode] = (rtx_cost (&all.mod, SET) <= 4 * add_cost[mode]);
222 wider_mode = GET_MODE_WIDER_MODE (mode);
223 if (wider_mode != VOIDmode)
225 PUT_MODE (&all.zext, wider_mode);
226 PUT_MODE (&all.wide_mult, wider_mode);
227 PUT_MODE (&all.wide_lshr, wider_mode);
228 XEXP (&all.wide_lshr, 1) = GEN_INT (GET_MODE_BITSIZE (mode));
230 mul_widen_cost[wider_mode] = rtx_cost (&all.wide_mult, SET);
231 mul_highpart_cost[mode] = rtx_cost (&all.wide_trunc, SET);
234 shift_cost[mode][0] = 0;
235 shiftadd_cost[mode][0] = shiftsub_cost[mode][0] = add_cost[mode];
237 n = MIN (MAX_BITS_PER_WORD, GET_MODE_BITSIZE (mode));
238 for (m = 1; m < n; m++)
240 XEXP (&all.shift, 1) = cint[m];
241 XEXP (&all.shift_mult, 1) = pow2[m];
243 shift_cost[mode][m] = rtx_cost (&all.shift, SET);
244 shiftadd_cost[mode][m] = rtx_cost (&all.shift_add, SET);
245 shiftsub_cost[mode][m] = rtx_cost (&all.shift_sub, SET);
250 /* Return an rtx representing minus the value of X.
251 MODE is the intended mode of the result,
252 useful if X is a CONST_INT. */
255 negate_rtx (enum machine_mode mode, rtx x)
257 rtx result = simplify_unary_operation (NEG, mode, x, mode);
259 if (result == 0)
260 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
262 return result;
265 /* Report on the availability of insv/extv/extzv and the desired mode
266 of each of their operands. Returns MAX_MACHINE_MODE if HAVE_foo
267 is false; else the mode of the specified operand. If OPNO is -1,
268 all the caller cares about is whether the insn is available. */
269 enum machine_mode
270 mode_for_extraction (enum extraction_pattern pattern, int opno)
272 const struct insn_data *data;
274 switch (pattern)
276 case EP_insv:
277 if (HAVE_insv)
279 data = &insn_data[CODE_FOR_insv];
280 break;
282 return MAX_MACHINE_MODE;
284 case EP_extv:
285 if (HAVE_extv)
287 data = &insn_data[CODE_FOR_extv];
288 break;
290 return MAX_MACHINE_MODE;
292 case EP_extzv:
293 if (HAVE_extzv)
295 data = &insn_data[CODE_FOR_extzv];
296 break;
298 return MAX_MACHINE_MODE;
300 default:
301 gcc_unreachable ();
304 if (opno == -1)
305 return VOIDmode;
307 /* Everyone who uses this function used to follow it with
308 if (result == VOIDmode) result = word_mode; */
309 if (data->operand[opno].mode == VOIDmode)
310 return word_mode;
311 return data->operand[opno].mode;
315 /* Generate code to store value from rtx VALUE
316 into a bit-field within structure STR_RTX
317 containing BITSIZE bits starting at bit BITNUM.
318 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
319 ALIGN is the alignment that STR_RTX is known to have.
320 TOTAL_SIZE is the size of the structure in bytes, or -1 if varying. */
322 /* ??? Note that there are two different ideas here for how
323 to determine the size to count bits within, for a register.
324 One is BITS_PER_WORD, and the other is the size of operand 3
325 of the insv pattern.
327 If operand 3 of the insv pattern is VOIDmode, then we will use BITS_PER_WORD
328 else, we use the mode of operand 3. */
331 store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
332 unsigned HOST_WIDE_INT bitnum, enum machine_mode fieldmode,
333 rtx value)
335 unsigned int unit
336 = (MEM_P (str_rtx)) ? BITS_PER_UNIT : BITS_PER_WORD;
337 unsigned HOST_WIDE_INT offset = bitnum / unit;
338 unsigned HOST_WIDE_INT bitpos = bitnum % unit;
339 rtx op0 = str_rtx;
340 int byte_offset;
342 enum machine_mode op_mode = mode_for_extraction (EP_insv, 3);
344 while (GET_CODE (op0) == SUBREG)
346 /* The following line once was done only if WORDS_BIG_ENDIAN,
347 but I think that is a mistake. WORDS_BIG_ENDIAN is
348 meaningful at a much higher level; when structures are copied
349 between memory and regs, the higher-numbered regs
350 always get higher addresses. */
351 offset += (SUBREG_BYTE (op0) / UNITS_PER_WORD);
352 /* We used to adjust BITPOS here, but now we do the whole adjustment
353 right after the loop. */
354 op0 = SUBREG_REG (op0);
357 /* Use vec_set patterns for inserting parts of vectors whenever
358 available. */
359 if (VECTOR_MODE_P (GET_MODE (op0))
360 && !MEM_P (op0)
361 && (vec_set_optab->handlers[GET_MODE (op0)].insn_code
362 != CODE_FOR_nothing)
363 && fieldmode == GET_MODE_INNER (GET_MODE (op0))
364 && bitsize == GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
365 && !(bitnum % GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
367 enum machine_mode outermode = GET_MODE (op0);
368 enum machine_mode innermode = GET_MODE_INNER (outermode);
369 int icode = (int) vec_set_optab->handlers[outermode].insn_code;
370 int pos = bitnum / GET_MODE_BITSIZE (innermode);
371 rtx rtxpos = GEN_INT (pos);
372 rtx src = value;
373 rtx dest = op0;
374 rtx pat, seq;
375 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
376 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
377 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
379 start_sequence ();
381 if (! (*insn_data[icode].operand[1].predicate) (src, mode1))
382 src = copy_to_mode_reg (mode1, src);
384 if (! (*insn_data[icode].operand[2].predicate) (rtxpos, mode2))
385 rtxpos = copy_to_mode_reg (mode1, rtxpos);
387 /* We could handle this, but we should always be called with a pseudo
388 for our targets and all insns should take them as outputs. */
389 gcc_assert ((*insn_data[icode].operand[0].predicate) (dest, mode0)
390 && (*insn_data[icode].operand[1].predicate) (src, mode1)
391 && (*insn_data[icode].operand[2].predicate) (rtxpos, mode2));
392 pat = GEN_FCN (icode) (dest, src, rtxpos);
393 seq = get_insns ();
394 end_sequence ();
395 if (pat)
397 emit_insn (seq);
398 emit_insn (pat);
399 return dest;
403 if (flag_force_mem)
405 int old_generating_concat_p = generating_concat_p;
406 generating_concat_p = 0;
407 value = force_not_mem (value);
408 generating_concat_p = old_generating_concat_p;
411 /* If the target is a register, overwriting the entire object, or storing
412 a full-word or multi-word field can be done with just a SUBREG.
414 If the target is memory, storing any naturally aligned field can be
415 done with a simple store. For targets that support fast unaligned
416 memory, any naturally sized, unit aligned field can be done directly. */
418 byte_offset = (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
419 + (offset * UNITS_PER_WORD);
421 if (bitpos == 0
422 && bitsize == GET_MODE_BITSIZE (fieldmode)
423 && (!MEM_P (op0)
424 ? ((GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
425 || GET_MODE_SIZE (GET_MODE (op0)) == GET_MODE_SIZE (fieldmode))
426 && byte_offset % GET_MODE_SIZE (fieldmode) == 0)
427 : (! SLOW_UNALIGNED_ACCESS (fieldmode, MEM_ALIGN (op0))
428 || (offset * BITS_PER_UNIT % bitsize == 0
429 && MEM_ALIGN (op0) % GET_MODE_BITSIZE (fieldmode) == 0))))
431 if (GET_MODE (op0) != fieldmode)
433 if (GET_CODE (op0) == SUBREG)
435 /* Else we've got some float mode source being extracted
436 into a different float mode destination -- this
437 combination of subregs results in Severe Tire
438 Damage. */
439 gcc_assert (GET_MODE (SUBREG_REG (op0)) == fieldmode
440 || GET_MODE_CLASS (fieldmode) == MODE_INT
441 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
442 op0 = SUBREG_REG (op0);
444 if (REG_P (op0))
445 op0 = gen_rtx_SUBREG (fieldmode, op0, byte_offset);
446 else
447 op0 = adjust_address (op0, fieldmode, offset);
449 emit_move_insn (op0, value);
450 return value;
453 /* Make sure we are playing with integral modes. Pun with subregs
454 if we aren't. This must come after the entire register case above,
455 since that case is valid for any mode. The following cases are only
456 valid for integral modes. */
458 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
459 if (imode != GET_MODE (op0))
461 if (MEM_P (op0))
462 op0 = adjust_address (op0, imode, 0);
463 else
465 gcc_assert (imode != BLKmode);
466 op0 = gen_lowpart (imode, op0);
471 /* We may be accessing data outside the field, which means
472 we can alias adjacent data. */
473 if (MEM_P (op0))
475 op0 = shallow_copy_rtx (op0);
476 set_mem_alias_set (op0, 0);
477 set_mem_expr (op0, 0);
480 /* If OP0 is a register, BITPOS must count within a word.
481 But as we have it, it counts within whatever size OP0 now has.
482 On a bigendian machine, these are not the same, so convert. */
483 if (BYTES_BIG_ENDIAN
484 && !MEM_P (op0)
485 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
486 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
488 /* Storing an lsb-aligned field in a register
489 can be done with a movestrict instruction. */
491 if (!MEM_P (op0)
492 && (BYTES_BIG_ENDIAN ? bitpos + bitsize == unit : bitpos == 0)
493 && bitsize == GET_MODE_BITSIZE (fieldmode)
494 && (movstrict_optab->handlers[fieldmode].insn_code
495 != CODE_FOR_nothing))
497 int icode = movstrict_optab->handlers[fieldmode].insn_code;
499 /* Get appropriate low part of the value being stored. */
500 if (GET_CODE (value) == CONST_INT || REG_P (value))
501 value = gen_lowpart (fieldmode, value);
502 else if (!(GET_CODE (value) == SYMBOL_REF
503 || GET_CODE (value) == LABEL_REF
504 || GET_CODE (value) == CONST))
505 value = convert_to_mode (fieldmode, value, 0);
507 if (! (*insn_data[icode].operand[1].predicate) (value, fieldmode))
508 value = copy_to_mode_reg (fieldmode, value);
510 if (GET_CODE (op0) == SUBREG)
512 /* Else we've got some float mode source being extracted into
513 a different float mode destination -- this combination of
514 subregs results in Severe Tire Damage. */
515 gcc_assert (GET_MODE (SUBREG_REG (op0)) == fieldmode
516 || GET_MODE_CLASS (fieldmode) == MODE_INT
517 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
518 op0 = SUBREG_REG (op0);
521 emit_insn (GEN_FCN (icode)
522 (gen_rtx_SUBREG (fieldmode, op0,
523 (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
524 + (offset * UNITS_PER_WORD)),
525 value));
527 return value;
530 /* Handle fields bigger than a word. */
532 if (bitsize > BITS_PER_WORD)
534 /* Here we transfer the words of the field
535 in the order least significant first.
536 This is because the most significant word is the one which may
537 be less than full.
538 However, only do that if the value is not BLKmode. */
540 unsigned int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
541 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
542 unsigned int i;
544 /* This is the mode we must force value to, so that there will be enough
545 subwords to extract. Note that fieldmode will often (always?) be
546 VOIDmode, because that is what store_field uses to indicate that this
547 is a bit field, but passing VOIDmode to operand_subword_force will
548 result in an abort. */
549 fieldmode = GET_MODE (value);
550 if (fieldmode == VOIDmode)
551 fieldmode = smallest_mode_for_size (nwords * BITS_PER_WORD, MODE_INT);
553 for (i = 0; i < nwords; i++)
555 /* If I is 0, use the low-order word in both field and target;
556 if I is 1, use the next to lowest word; and so on. */
557 unsigned int wordnum = (backwards ? nwords - i - 1 : i);
558 unsigned int bit_offset = (backwards
559 ? MAX ((int) bitsize - ((int) i + 1)
560 * BITS_PER_WORD,
562 : (int) i * BITS_PER_WORD);
564 store_bit_field (op0, MIN (BITS_PER_WORD,
565 bitsize - i * BITS_PER_WORD),
566 bitnum + bit_offset, word_mode,
567 operand_subword_force (value, wordnum, fieldmode));
569 return value;
572 /* From here on we can assume that the field to be stored in is
573 a full-word (whatever type that is), since it is shorter than a word. */
575 /* OFFSET is the number of words or bytes (UNIT says which)
576 from STR_RTX to the first word or byte containing part of the field. */
578 if (!MEM_P (op0))
580 if (offset != 0
581 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
583 if (!REG_P (op0))
585 /* Since this is a destination (lvalue), we can't copy it to a
586 pseudo. We can trivially remove a SUBREG that does not
587 change the size of the operand. Such a SUBREG may have been
588 added above. Otherwise, abort. */
589 gcc_assert (GET_CODE (op0) == SUBREG
590 && (GET_MODE_SIZE (GET_MODE (op0))
591 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))));
592 op0 = SUBREG_REG (op0);
594 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
595 op0, (offset * UNITS_PER_WORD));
597 offset = 0;
600 /* If VALUE is a floating-point mode, access it as an integer of the
601 corresponding size. This can occur on a machine with 64 bit registers
602 that uses SFmode for float. This can also occur for unaligned float
603 structure fields. */
604 if (GET_MODE_CLASS (GET_MODE (value)) != MODE_INT
605 && GET_MODE_CLASS (GET_MODE (value)) != MODE_PARTIAL_INT)
606 value = gen_lowpart ((GET_MODE (value) == VOIDmode
607 ? word_mode : int_mode_for_mode (GET_MODE (value))),
608 value);
610 /* Now OFFSET is nonzero only if OP0 is memory
611 and is therefore always measured in bytes. */
613 if (HAVE_insv
614 && GET_MODE (value) != BLKmode
615 && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
616 /* Ensure insv's size is wide enough for this field. */
617 && (GET_MODE_BITSIZE (op_mode) >= bitsize)
618 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
619 && (bitsize + bitpos > GET_MODE_BITSIZE (op_mode))))
621 int xbitpos = bitpos;
622 rtx value1;
623 rtx xop0 = op0;
624 rtx last = get_last_insn ();
625 rtx pat;
626 enum machine_mode maxmode = mode_for_extraction (EP_insv, 3);
627 int save_volatile_ok = volatile_ok;
629 volatile_ok = 1;
631 /* If this machine's insv can only insert into a register, copy OP0
632 into a register and save it back later. */
633 /* This used to check flag_force_mem, but that was a serious
634 de-optimization now that flag_force_mem is enabled by -O2. */
635 if (MEM_P (op0)
636 && ! ((*insn_data[(int) CODE_FOR_insv].operand[0].predicate)
637 (op0, VOIDmode)))
639 rtx tempreg;
640 enum machine_mode bestmode;
642 /* Get the mode to use for inserting into this field. If OP0 is
643 BLKmode, get the smallest mode consistent with the alignment. If
644 OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
645 mode. Otherwise, use the smallest mode containing the field. */
647 if (GET_MODE (op0) == BLKmode
648 || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
649 bestmode
650 = get_best_mode (bitsize, bitnum, MEM_ALIGN (op0), maxmode,
651 MEM_VOLATILE_P (op0));
652 else
653 bestmode = GET_MODE (op0);
655 if (bestmode == VOIDmode
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, 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 = (REG_P (op0) || ! flag_force_mem) ? op0 : 0;
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 = bitnum / unit;
1071 unsigned HOST_WIDE_INT bitpos = bitnum % unit;
1072 rtx op0 = str_rtx;
1073 rtx spec_target = target;
1074 rtx spec_target_subreg = 0;
1075 enum machine_mode int_mode;
1076 enum machine_mode extv_mode = mode_for_extraction (EP_extv, 0);
1077 enum machine_mode extzv_mode = mode_for_extraction (EP_extzv, 0);
1078 enum machine_mode mode1;
1079 int byte_offset;
1081 if (tmode == VOIDmode)
1082 tmode = mode;
1084 while (GET_CODE (op0) == SUBREG)
1086 bitpos += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1087 if (bitpos > unit)
1089 offset += (bitpos / unit);
1090 bitpos %= unit;
1092 op0 = SUBREG_REG (op0);
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);
1174 /* We may be accessing data outside the field, which means
1175 we can alias adjacent data. */
1176 if (MEM_P (op0))
1178 op0 = shallow_copy_rtx (op0);
1179 set_mem_alias_set (op0, 0);
1180 set_mem_expr (op0, 0);
1183 /* Extraction of a full-word or multi-word value from a structure
1184 in a register or aligned memory can be done with just a SUBREG.
1185 A subword value in the least significant part of a register
1186 can also be extracted with a SUBREG. For this, we need the
1187 byte offset of the value in op0. */
1189 byte_offset = bitpos / BITS_PER_UNIT + offset * UNITS_PER_WORD;
1191 /* If OP0 is a register, BITPOS must count within a word.
1192 But as we have it, it counts within whatever size OP0 now has.
1193 On a bigendian machine, these are not the same, so convert. */
1194 if (BYTES_BIG_ENDIAN
1195 && !MEM_P (op0)
1196 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
1197 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
1199 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1200 If that's wrong, the solution is to test for it and set TARGET to 0
1201 if needed. */
1203 /* Only scalar integer modes can be converted via subregs. There is an
1204 additional problem for FP modes here in that they can have a precision
1205 which is different from the size. mode_for_size uses precision, but
1206 we want a mode based on the size, so we must avoid calling it for FP
1207 modes. */
1208 mode1 = (SCALAR_INT_MODE_P (tmode)
1209 ? mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0)
1210 : mode);
1212 if (((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
1213 && bitpos % BITS_PER_WORD == 0)
1214 || (mode1 != BLKmode
1215 /* ??? The big endian test here is wrong. This is correct
1216 if the value is in a register, and if mode_for_size is not
1217 the same mode as op0. This causes us to get unnecessarily
1218 inefficient code from the Thumb port when -mbig-endian. */
1219 && (BYTES_BIG_ENDIAN
1220 ? bitpos + bitsize == BITS_PER_WORD
1221 : bitpos == 0)))
1222 && ((!MEM_P (op0)
1223 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1224 GET_MODE_BITSIZE (GET_MODE (op0)))
1225 && GET_MODE_SIZE (mode1) != 0
1226 && byte_offset % GET_MODE_SIZE (mode1) == 0)
1227 || (MEM_P (op0)
1228 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
1229 || (offset * BITS_PER_UNIT % bitsize == 0
1230 && MEM_ALIGN (op0) % bitsize == 0)))))
1232 if (mode1 != GET_MODE (op0))
1234 if (GET_CODE (op0) == SUBREG)
1236 if (GET_MODE (SUBREG_REG (op0)) == mode1
1237 || GET_MODE_CLASS (mode1) == MODE_INT
1238 || GET_MODE_CLASS (mode1) == MODE_PARTIAL_INT)
1239 op0 = SUBREG_REG (op0);
1240 else
1241 /* Else we've got some float mode source being extracted into
1242 a different float mode destination -- this combination of
1243 subregs results in Severe Tire Damage. */
1244 goto no_subreg_mode_swap;
1246 if (REG_P (op0))
1247 op0 = gen_rtx_SUBREG (mode1, op0, byte_offset);
1248 else
1249 op0 = adjust_address (op0, mode1, offset);
1251 if (mode1 != mode)
1252 return convert_to_mode (tmode, op0, unsignedp);
1253 return op0;
1255 no_subreg_mode_swap:
1257 /* Handle fields bigger than a word. */
1259 if (bitsize > BITS_PER_WORD)
1261 /* Here we transfer the words of the field
1262 in the order least significant first.
1263 This is because the most significant word is the one which may
1264 be less than full. */
1266 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1267 unsigned int i;
1269 if (target == 0 || !REG_P (target))
1270 target = gen_reg_rtx (mode);
1272 /* Indicate for flow that the entire target reg is being set. */
1273 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
1275 for (i = 0; i < nwords; i++)
1277 /* If I is 0, use the low-order word in both field and target;
1278 if I is 1, use the next to lowest word; and so on. */
1279 /* Word number in TARGET to use. */
1280 unsigned int wordnum
1281 = (WORDS_BIG_ENDIAN
1282 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1283 : i);
1284 /* Offset from start of field in OP0. */
1285 unsigned int bit_offset = (WORDS_BIG_ENDIAN
1286 ? MAX (0, ((int) bitsize - ((int) i + 1)
1287 * (int) BITS_PER_WORD))
1288 : (int) i * BITS_PER_WORD);
1289 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1290 rtx result_part
1291 = extract_bit_field (op0, MIN (BITS_PER_WORD,
1292 bitsize - i * BITS_PER_WORD),
1293 bitnum + bit_offset, 1, target_part, mode,
1294 word_mode);
1296 gcc_assert (target_part);
1298 if (result_part != target_part)
1299 emit_move_insn (target_part, result_part);
1302 if (unsignedp)
1304 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1305 need to be zero'd out. */
1306 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1308 unsigned int i, total_words;
1310 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1311 for (i = nwords; i < total_words; i++)
1312 emit_move_insn
1313 (operand_subword (target,
1314 WORDS_BIG_ENDIAN ? total_words - i - 1 : i,
1315 1, VOIDmode),
1316 const0_rtx);
1318 return target;
1321 /* Signed bit field: sign-extend with two arithmetic shifts. */
1322 target = expand_shift (LSHIFT_EXPR, mode, target,
1323 build_int_cst (NULL_TREE,
1324 GET_MODE_BITSIZE (mode) - bitsize),
1325 NULL_RTX, 0);
1326 return expand_shift (RSHIFT_EXPR, mode, target,
1327 build_int_cst (NULL_TREE,
1328 GET_MODE_BITSIZE (mode) - bitsize),
1329 NULL_RTX, 0);
1332 /* From here on we know the desired field is smaller than a word. */
1334 /* Check if there is a correspondingly-sized integer field, so we can
1335 safely extract it as one size of integer, if necessary; then
1336 truncate or extend to the size that is wanted; then use SUBREGs or
1337 convert_to_mode to get one of the modes we really wanted. */
1339 int_mode = int_mode_for_mode (tmode);
1340 if (int_mode == BLKmode)
1341 int_mode = int_mode_for_mode (mode);
1342 /* Should probably push op0 out to memory and then do a load. */
1343 gcc_assert (int_mode != BLKmode);
1345 /* OFFSET is the number of words or bytes (UNIT says which)
1346 from STR_RTX to the first word or byte containing part of the field. */
1347 if (!MEM_P (op0))
1349 if (offset != 0
1350 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1352 if (!REG_P (op0))
1353 op0 = copy_to_reg (op0);
1354 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
1355 op0, (offset * UNITS_PER_WORD));
1357 offset = 0;
1360 /* Now OFFSET is nonzero only for memory operands. */
1362 if (unsignedp)
1364 if (HAVE_extzv
1365 && (GET_MODE_BITSIZE (extzv_mode) >= bitsize)
1366 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
1367 && (bitsize + bitpos > GET_MODE_BITSIZE (extzv_mode))))
1369 unsigned HOST_WIDE_INT xbitpos = bitpos, xoffset = offset;
1370 rtx bitsize_rtx, bitpos_rtx;
1371 rtx last = get_last_insn ();
1372 rtx xop0 = op0;
1373 rtx xtarget = target;
1374 rtx xspec_target = spec_target;
1375 rtx xspec_target_subreg = spec_target_subreg;
1376 rtx pat;
1377 enum machine_mode maxmode = mode_for_extraction (EP_extzv, 0);
1379 if (MEM_P (xop0))
1381 int save_volatile_ok = volatile_ok;
1382 volatile_ok = 1;
1384 /* Is the memory operand acceptable? */
1385 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[1].predicate)
1386 (xop0, GET_MODE (xop0))))
1388 /* No, load into a reg and extract from there. */
1389 enum machine_mode bestmode;
1391 /* Get the mode to use for inserting into this field. If
1392 OP0 is BLKmode, get the smallest mode consistent with the
1393 alignment. If OP0 is a non-BLKmode object that is no
1394 wider than MAXMODE, use its mode. Otherwise, use the
1395 smallest mode containing the field. */
1397 if (GET_MODE (xop0) == BLKmode
1398 || (GET_MODE_SIZE (GET_MODE (op0))
1399 > GET_MODE_SIZE (maxmode)))
1400 bestmode = get_best_mode (bitsize, bitnum,
1401 MEM_ALIGN (xop0), maxmode,
1402 MEM_VOLATILE_P (xop0));
1403 else
1404 bestmode = GET_MODE (xop0);
1406 if (bestmode == VOIDmode
1407 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (xop0))
1408 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (xop0)))
1409 goto extzv_loses;
1411 /* Compute offset as multiple of this unit,
1412 counting in bytes. */
1413 unit = GET_MODE_BITSIZE (bestmode);
1414 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1415 xbitpos = bitnum % unit;
1416 xop0 = adjust_address (xop0, bestmode, xoffset);
1418 /* Fetch it to a register in that size. */
1419 xop0 = force_reg (bestmode, xop0);
1421 /* XBITPOS counts within UNIT, which is what is expected. */
1423 else
1424 /* Get ref to first byte containing part of the field. */
1425 xop0 = adjust_address (xop0, byte_mode, xoffset);
1427 volatile_ok = save_volatile_ok;
1430 /* If op0 is a register, we need it in MAXMODE (which is usually
1431 SImode). to make it acceptable to the format of extzv. */
1432 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1433 goto extzv_loses;
1434 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
1435 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1437 /* On big-endian machines, we count bits from the most significant.
1438 If the bit field insn does not, we must invert. */
1439 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1440 xbitpos = unit - bitsize - xbitpos;
1442 /* Now convert from counting within UNIT to counting in MAXMODE. */
1443 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
1444 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
1446 unit = GET_MODE_BITSIZE (maxmode);
1448 if (xtarget == 0
1449 || (flag_force_mem && MEM_P (xtarget)))
1450 xtarget = xspec_target = gen_reg_rtx (tmode);
1452 if (GET_MODE (xtarget) != maxmode)
1454 if (REG_P (xtarget))
1456 int wider = (GET_MODE_SIZE (maxmode)
1457 > GET_MODE_SIZE (GET_MODE (xtarget)));
1458 xtarget = gen_lowpart (maxmode, xtarget);
1459 if (wider)
1460 xspec_target_subreg = xtarget;
1462 else
1463 xtarget = gen_reg_rtx (maxmode);
1466 /* If this machine's extzv insists on a register target,
1467 make sure we have one. */
1468 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[0].predicate)
1469 (xtarget, maxmode)))
1470 xtarget = gen_reg_rtx (maxmode);
1472 bitsize_rtx = GEN_INT (bitsize);
1473 bitpos_rtx = GEN_INT (xbitpos);
1475 pat = gen_extzv (xtarget, xop0, bitsize_rtx, bitpos_rtx);
1476 if (pat)
1478 emit_insn (pat);
1479 target = xtarget;
1480 spec_target = xspec_target;
1481 spec_target_subreg = xspec_target_subreg;
1483 else
1485 delete_insns_since (last);
1486 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1487 bitpos, target, 1);
1490 else
1491 extzv_loses:
1492 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1493 bitpos, target, 1);
1495 else
1497 if (HAVE_extv
1498 && (GET_MODE_BITSIZE (extv_mode) >= bitsize)
1499 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
1500 && (bitsize + bitpos > GET_MODE_BITSIZE (extv_mode))))
1502 int xbitpos = bitpos, xoffset = offset;
1503 rtx bitsize_rtx, bitpos_rtx;
1504 rtx last = get_last_insn ();
1505 rtx xop0 = op0, xtarget = target;
1506 rtx xspec_target = spec_target;
1507 rtx xspec_target_subreg = spec_target_subreg;
1508 rtx pat;
1509 enum machine_mode maxmode = mode_for_extraction (EP_extv, 0);
1511 if (MEM_P (xop0))
1513 /* Is the memory operand acceptable? */
1514 if (! ((*insn_data[(int) CODE_FOR_extv].operand[1].predicate)
1515 (xop0, GET_MODE (xop0))))
1517 /* No, load into a reg and extract from there. */
1518 enum machine_mode bestmode;
1520 /* Get the mode to use for inserting into this field. If
1521 OP0 is BLKmode, get the smallest mode consistent with the
1522 alignment. If OP0 is a non-BLKmode object that is no
1523 wider than MAXMODE, use its mode. Otherwise, use the
1524 smallest mode containing the field. */
1526 if (GET_MODE (xop0) == BLKmode
1527 || (GET_MODE_SIZE (GET_MODE (op0))
1528 > GET_MODE_SIZE (maxmode)))
1529 bestmode = get_best_mode (bitsize, bitnum,
1530 MEM_ALIGN (xop0), maxmode,
1531 MEM_VOLATILE_P (xop0));
1532 else
1533 bestmode = GET_MODE (xop0);
1535 if (bestmode == VOIDmode
1536 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (xop0))
1537 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (xop0)))
1538 goto extv_loses;
1540 /* Compute offset as multiple of this unit,
1541 counting in bytes. */
1542 unit = GET_MODE_BITSIZE (bestmode);
1543 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1544 xbitpos = bitnum % unit;
1545 xop0 = adjust_address (xop0, bestmode, xoffset);
1547 /* Fetch it to a register in that size. */
1548 xop0 = force_reg (bestmode, xop0);
1550 /* XBITPOS counts within UNIT, which is what is expected. */
1552 else
1553 /* Get ref to first byte containing part of the field. */
1554 xop0 = adjust_address (xop0, byte_mode, xoffset);
1557 /* If op0 is a register, we need it in MAXMODE (which is usually
1558 SImode) to make it acceptable to the format of extv. */
1559 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1560 goto extv_loses;
1561 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
1562 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1564 /* On big-endian machines, we count bits from the most significant.
1565 If the bit field insn does not, we must invert. */
1566 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1567 xbitpos = unit - bitsize - xbitpos;
1569 /* XBITPOS counts within a size of UNIT.
1570 Adjust to count within a size of MAXMODE. */
1571 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
1572 xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
1574 unit = GET_MODE_BITSIZE (maxmode);
1576 if (xtarget == 0
1577 || (flag_force_mem && MEM_P (xtarget)))
1578 xtarget = xspec_target = gen_reg_rtx (tmode);
1580 if (GET_MODE (xtarget) != maxmode)
1582 if (REG_P (xtarget))
1584 int wider = (GET_MODE_SIZE (maxmode)
1585 > GET_MODE_SIZE (GET_MODE (xtarget)));
1586 xtarget = gen_lowpart (maxmode, xtarget);
1587 if (wider)
1588 xspec_target_subreg = xtarget;
1590 else
1591 xtarget = gen_reg_rtx (maxmode);
1594 /* If this machine's extv insists on a register target,
1595 make sure we have one. */
1596 if (! ((*insn_data[(int) CODE_FOR_extv].operand[0].predicate)
1597 (xtarget, maxmode)))
1598 xtarget = gen_reg_rtx (maxmode);
1600 bitsize_rtx = GEN_INT (bitsize);
1601 bitpos_rtx = GEN_INT (xbitpos);
1603 pat = gen_extv (xtarget, xop0, bitsize_rtx, bitpos_rtx);
1604 if (pat)
1606 emit_insn (pat);
1607 target = xtarget;
1608 spec_target = xspec_target;
1609 spec_target_subreg = xspec_target_subreg;
1611 else
1613 delete_insns_since (last);
1614 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1615 bitpos, target, 0);
1618 else
1619 extv_loses:
1620 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1621 bitpos, target, 0);
1623 if (target == spec_target)
1624 return target;
1625 if (target == spec_target_subreg)
1626 return spec_target;
1627 if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
1629 /* If the target mode is floating-point, first convert to the
1630 integer mode of that size and then access it as a floating-point
1631 value via a SUBREG. */
1632 if (GET_MODE_CLASS (tmode) != MODE_INT
1633 && GET_MODE_CLASS (tmode) != MODE_PARTIAL_INT)
1635 target = convert_to_mode (mode_for_size (GET_MODE_BITSIZE (tmode),
1636 MODE_INT, 0),
1637 target, unsignedp);
1638 return gen_lowpart (tmode, target);
1640 else
1641 return convert_to_mode (tmode, target, unsignedp);
1643 return target;
1646 /* Extract a bit field using shifts and boolean operations
1647 Returns an rtx to represent the value.
1648 OP0 addresses a register (word) or memory (byte).
1649 BITPOS says which bit within the word or byte the bit field starts in.
1650 OFFSET says how many bytes farther the bit field starts;
1651 it is 0 if OP0 is a register.
1652 BITSIZE says how many bits long the bit field is.
1653 (If OP0 is a register, it may be narrower than a full word,
1654 but BITPOS still counts within a full word,
1655 which is significant on bigendian machines.)
1657 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1658 If TARGET is nonzero, attempts to store the value there
1659 and return TARGET, but this is not guaranteed.
1660 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
1662 static rtx
1663 extract_fixed_bit_field (enum machine_mode tmode, rtx op0,
1664 unsigned HOST_WIDE_INT offset,
1665 unsigned HOST_WIDE_INT bitsize,
1666 unsigned HOST_WIDE_INT bitpos, rtx target,
1667 int unsignedp)
1669 unsigned int total_bits = BITS_PER_WORD;
1670 enum machine_mode mode;
1672 if (GET_CODE (op0) == SUBREG || REG_P (op0))
1674 /* Special treatment for a bit field split across two registers. */
1675 if (bitsize + bitpos > BITS_PER_WORD)
1676 return extract_split_bit_field (op0, bitsize, bitpos, unsignedp);
1678 else
1680 /* Get the proper mode to use for this field. We want a mode that
1681 includes the entire field. If such a mode would be larger than
1682 a word, we won't be doing the extraction the normal way. */
1684 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1685 MEM_ALIGN (op0), word_mode, MEM_VOLATILE_P (op0));
1687 if (mode == VOIDmode)
1688 /* The only way this should occur is if the field spans word
1689 boundaries. */
1690 return extract_split_bit_field (op0, bitsize,
1691 bitpos + offset * BITS_PER_UNIT,
1692 unsignedp);
1694 total_bits = GET_MODE_BITSIZE (mode);
1696 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
1697 be in the range 0 to total_bits-1, and put any excess bytes in
1698 OFFSET. */
1699 if (bitpos >= total_bits)
1701 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
1702 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
1703 * BITS_PER_UNIT);
1706 /* Get ref to an aligned byte, halfword, or word containing the field.
1707 Adjust BITPOS to be position within a word,
1708 and OFFSET to be the offset of that word.
1709 Then alter OP0 to refer to that word. */
1710 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1711 offset -= (offset % (total_bits / BITS_PER_UNIT));
1712 op0 = adjust_address (op0, mode, offset);
1715 mode = GET_MODE (op0);
1717 if (BYTES_BIG_ENDIAN)
1718 /* BITPOS is the distance between our msb and that of OP0.
1719 Convert it to the distance from the lsb. */
1720 bitpos = total_bits - bitsize - bitpos;
1722 /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1723 We have reduced the big-endian case to the little-endian case. */
1725 if (unsignedp)
1727 if (bitpos)
1729 /* If the field does not already start at the lsb,
1730 shift it so it does. */
1731 tree amount = build_int_cst (NULL_TREE, bitpos);
1732 /* Maybe propagate the target for the shift. */
1733 /* But not if we will return it--could confuse integrate.c. */
1734 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1735 if (tmode != mode) subtarget = 0;
1736 op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1738 /* Convert the value to the desired mode. */
1739 if (mode != tmode)
1740 op0 = convert_to_mode (tmode, op0, 1);
1742 /* Unless the msb of the field used to be the msb when we shifted,
1743 mask out the upper bits. */
1745 if (GET_MODE_BITSIZE (mode) != bitpos + bitsize)
1746 return expand_binop (GET_MODE (op0), and_optab, op0,
1747 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1748 target, 1, OPTAB_LIB_WIDEN);
1749 return op0;
1752 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1753 then arithmetic-shift its lsb to the lsb of the word. */
1754 op0 = force_reg (mode, op0);
1755 if (mode != tmode)
1756 target = 0;
1758 /* Find the narrowest integer mode that contains the field. */
1760 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1761 mode = GET_MODE_WIDER_MODE (mode))
1762 if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1764 op0 = convert_to_mode (mode, op0, 0);
1765 break;
1768 if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1770 tree amount
1771 = build_int_cst (NULL_TREE,
1772 GET_MODE_BITSIZE (mode) - (bitsize + bitpos));
1773 /* Maybe propagate the target for the shift. */
1774 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1775 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1778 return expand_shift (RSHIFT_EXPR, mode, op0,
1779 build_int_cst (NULL_TREE,
1780 GET_MODE_BITSIZE (mode) - bitsize),
1781 target, 0);
1784 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1785 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1786 complement of that if COMPLEMENT. The mask is truncated if
1787 necessary to the width of mode MODE. The mask is zero-extended if
1788 BITSIZE+BITPOS is too small for MODE. */
1790 static rtx
1791 mask_rtx (enum machine_mode mode, int bitpos, int bitsize, int complement)
1793 HOST_WIDE_INT masklow, maskhigh;
1795 if (bitsize == 0)
1796 masklow = 0;
1797 else if (bitpos < HOST_BITS_PER_WIDE_INT)
1798 masklow = (HOST_WIDE_INT) -1 << bitpos;
1799 else
1800 masklow = 0;
1802 if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1803 masklow &= ((unsigned HOST_WIDE_INT) -1
1804 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1806 if (bitpos <= HOST_BITS_PER_WIDE_INT)
1807 maskhigh = -1;
1808 else
1809 maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1811 if (bitsize == 0)
1812 maskhigh = 0;
1813 else if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
1814 maskhigh &= ((unsigned HOST_WIDE_INT) -1
1815 >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1816 else
1817 maskhigh = 0;
1819 if (complement)
1821 maskhigh = ~maskhigh;
1822 masklow = ~masklow;
1825 return immed_double_const (masklow, maskhigh, mode);
1828 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1829 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1831 static rtx
1832 lshift_value (enum machine_mode mode, rtx value, int bitpos, int bitsize)
1834 unsigned HOST_WIDE_INT v = INTVAL (value);
1835 HOST_WIDE_INT low, high;
1837 if (bitsize < HOST_BITS_PER_WIDE_INT)
1838 v &= ~((HOST_WIDE_INT) -1 << bitsize);
1840 if (bitpos < HOST_BITS_PER_WIDE_INT)
1842 low = v << bitpos;
1843 high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
1845 else
1847 low = 0;
1848 high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
1851 return immed_double_const (low, high, mode);
1854 /* Extract a bit field that is split across two words
1855 and return an RTX for the result.
1857 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1858 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1859 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
1861 static rtx
1862 extract_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
1863 unsigned HOST_WIDE_INT bitpos, int unsignedp)
1865 unsigned int unit;
1866 unsigned int bitsdone = 0;
1867 rtx result = NULL_RTX;
1868 int first = 1;
1870 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1871 much at a time. */
1872 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
1873 unit = BITS_PER_WORD;
1874 else
1875 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
1877 while (bitsdone < bitsize)
1879 unsigned HOST_WIDE_INT thissize;
1880 rtx part, word;
1881 unsigned HOST_WIDE_INT thispos;
1882 unsigned HOST_WIDE_INT offset;
1884 offset = (bitpos + bitsdone) / unit;
1885 thispos = (bitpos + bitsdone) % unit;
1887 /* THISSIZE must not overrun a word boundary. Otherwise,
1888 extract_fixed_bit_field will call us again, and we will mutually
1889 recurse forever. */
1890 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1891 thissize = MIN (thissize, unit - thispos);
1893 /* If OP0 is a register, then handle OFFSET here.
1895 When handling multiword bitfields, extract_bit_field may pass
1896 down a word_mode SUBREG of a larger REG for a bitfield that actually
1897 crosses a word boundary. Thus, for a SUBREG, we must find
1898 the current word starting from the base register. */
1899 if (GET_CODE (op0) == SUBREG)
1901 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
1902 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1903 GET_MODE (SUBREG_REG (op0)));
1904 offset = 0;
1906 else if (REG_P (op0))
1908 word = operand_subword_force (op0, offset, GET_MODE (op0));
1909 offset = 0;
1911 else
1912 word = op0;
1914 /* Extract the parts in bit-counting order,
1915 whose meaning is determined by BYTES_PER_UNIT.
1916 OFFSET is in UNITs, and UNIT is in bits.
1917 extract_fixed_bit_field wants offset in bytes. */
1918 part = extract_fixed_bit_field (word_mode, word,
1919 offset * unit / BITS_PER_UNIT,
1920 thissize, thispos, 0, 1);
1921 bitsdone += thissize;
1923 /* Shift this part into place for the result. */
1924 if (BYTES_BIG_ENDIAN)
1926 if (bitsize != bitsdone)
1927 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1928 build_int_cst (NULL_TREE, bitsize - bitsdone),
1929 0, 1);
1931 else
1933 if (bitsdone != thissize)
1934 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1935 build_int_cst (NULL_TREE,
1936 bitsdone - thissize), 0, 1);
1939 if (first)
1940 result = part;
1941 else
1942 /* Combine the parts with bitwise or. This works
1943 because we extracted each part as an unsigned bit field. */
1944 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
1945 OPTAB_LIB_WIDEN);
1947 first = 0;
1950 /* Unsigned bit field: we are done. */
1951 if (unsignedp)
1952 return result;
1953 /* Signed bit field: sign-extend with two arithmetic shifts. */
1954 result = expand_shift (LSHIFT_EXPR, word_mode, result,
1955 build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
1956 NULL_RTX, 0);
1957 return expand_shift (RSHIFT_EXPR, word_mode, result,
1958 build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
1959 NULL_RTX, 0);
1962 /* Add INC into TARGET. */
1964 void
1965 expand_inc (rtx target, rtx inc)
1967 rtx value = expand_binop (GET_MODE (target), add_optab,
1968 target, inc,
1969 target, 0, OPTAB_LIB_WIDEN);
1970 if (value != target)
1971 emit_move_insn (target, value);
1974 /* Subtract DEC from TARGET. */
1976 void
1977 expand_dec (rtx target, rtx dec)
1979 rtx value = expand_binop (GET_MODE (target), sub_optab,
1980 target, dec,
1981 target, 0, OPTAB_LIB_WIDEN);
1982 if (value != target)
1983 emit_move_insn (target, value);
1986 /* Output a shift instruction for expression code CODE,
1987 with SHIFTED being the rtx for the value to shift,
1988 and AMOUNT the tree for the amount to shift by.
1989 Store the result in the rtx TARGET, if that is convenient.
1990 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
1991 Return the rtx for where the value is. */
1994 expand_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
1995 tree amount, rtx target, int unsignedp)
1997 rtx op1, temp = 0;
1998 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
1999 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2000 int try;
2002 /* Previously detected shift-counts computed by NEGATE_EXPR
2003 and shifted in the other direction; but that does not work
2004 on all machines. */
2006 op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
2008 if (SHIFT_COUNT_TRUNCATED)
2010 if (GET_CODE (op1) == CONST_INT
2011 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2012 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode)))
2013 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2014 % GET_MODE_BITSIZE (mode));
2015 else if (GET_CODE (op1) == SUBREG
2016 && subreg_lowpart_p (op1))
2017 op1 = SUBREG_REG (op1);
2020 if (op1 == const0_rtx)
2021 return shifted;
2023 /* Check whether its cheaper to implement a left shift by a constant
2024 bit count by a sequence of additions. */
2025 if (code == LSHIFT_EXPR
2026 && GET_CODE (op1) == CONST_INT
2027 && INTVAL (op1) > 0
2028 && INTVAL (op1) < GET_MODE_BITSIZE (mode)
2029 && shift_cost[mode][INTVAL (op1)] > INTVAL (op1) * add_cost[mode])
2031 int i;
2032 for (i = 0; i < INTVAL (op1); i++)
2034 temp = force_reg (mode, shifted);
2035 shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2036 unsignedp, OPTAB_LIB_WIDEN);
2038 return shifted;
2041 for (try = 0; temp == 0 && try < 3; try++)
2043 enum optab_methods methods;
2045 if (try == 0)
2046 methods = OPTAB_DIRECT;
2047 else if (try == 1)
2048 methods = OPTAB_WIDEN;
2049 else
2050 methods = OPTAB_LIB_WIDEN;
2052 if (rotate)
2054 /* Widening does not work for rotation. */
2055 if (methods == OPTAB_WIDEN)
2056 continue;
2057 else if (methods == OPTAB_LIB_WIDEN)
2059 /* If we have been unable to open-code this by a rotation,
2060 do it as the IOR of two shifts. I.e., to rotate A
2061 by N bits, compute (A << N) | ((unsigned) A >> (C - N))
2062 where C is the bitsize of A.
2064 It is theoretically possible that the target machine might
2065 not be able to perform either shift and hence we would
2066 be making two libcalls rather than just the one for the
2067 shift (similarly if IOR could not be done). We will allow
2068 this extremely unlikely lossage to avoid complicating the
2069 code below. */
2071 rtx subtarget = target == shifted ? 0 : target;
2072 rtx temp1;
2073 tree type = TREE_TYPE (amount);
2074 tree new_amount = make_tree (type, op1);
2075 tree other_amount
2076 = fold (build2 (MINUS_EXPR, type, convert
2077 (type, build_int_cst
2078 (NULL_TREE, GET_MODE_BITSIZE (mode))),
2079 amount));
2081 shifted = force_reg (mode, shifted);
2083 temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2084 mode, shifted, new_amount, subtarget, 1);
2085 temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2086 mode, shifted, other_amount, 0, 1);
2087 return expand_binop (mode, ior_optab, temp, temp1, target,
2088 unsignedp, methods);
2091 temp = expand_binop (mode,
2092 left ? rotl_optab : rotr_optab,
2093 shifted, op1, target, unsignedp, methods);
2095 /* If we don't have the rotate, but we are rotating by a constant
2096 that is in range, try a rotate in the opposite direction. */
2098 if (temp == 0 && GET_CODE (op1) == CONST_INT
2099 && INTVAL (op1) > 0
2100 && (unsigned int) INTVAL (op1) < GET_MODE_BITSIZE (mode))
2101 temp = expand_binop (mode,
2102 left ? rotr_optab : rotl_optab,
2103 shifted,
2104 GEN_INT (GET_MODE_BITSIZE (mode)
2105 - INTVAL (op1)),
2106 target, unsignedp, methods);
2108 else if (unsignedp)
2109 temp = expand_binop (mode,
2110 left ? ashl_optab : lshr_optab,
2111 shifted, op1, target, unsignedp, methods);
2113 /* Do arithmetic shifts.
2114 Also, if we are going to widen the operand, we can just as well
2115 use an arithmetic right-shift instead of a logical one. */
2116 if (temp == 0 && ! rotate
2117 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2119 enum optab_methods methods1 = methods;
2121 /* If trying to widen a log shift to an arithmetic shift,
2122 don't accept an arithmetic shift of the same size. */
2123 if (unsignedp)
2124 methods1 = OPTAB_MUST_WIDEN;
2126 /* Arithmetic shift */
2128 temp = expand_binop (mode,
2129 left ? ashl_optab : ashr_optab,
2130 shifted, op1, target, unsignedp, methods1);
2133 /* We used to try extzv here for logical right shifts, but that was
2134 only useful for one machine, the VAX, and caused poor code
2135 generation there for lshrdi3, so the code was deleted and a
2136 define_expand for lshrsi3 was added to vax.md. */
2139 gcc_assert (temp);
2140 return temp;
2143 enum alg_code { alg_zero, alg_m, alg_shift,
2144 alg_add_t_m2, alg_sub_t_m2,
2145 alg_add_factor, alg_sub_factor,
2146 alg_add_t2_m, alg_sub_t2_m };
2148 /* This structure holds the "cost" of a multiply sequence. The
2149 "cost" field holds the total rtx_cost of every operator in the
2150 synthetic multiplication sequence, hence cost(a op b) is defined
2151 as rtx_cost(op) + cost(a) + cost(b), where cost(leaf) is zero.
2152 The "latency" field holds the minimum possible latency of the
2153 synthetic multiply, on a hypothetical infinitely parallel CPU.
2154 This is the critical path, or the maximum height, of the expression
2155 tree which is the sum of rtx_costs on the most expensive path from
2156 any leaf to the root. Hence latency(a op b) is defined as zero for
2157 leaves and rtx_cost(op) + max(latency(a), latency(b)) otherwise. */
2159 struct mult_cost {
2160 short cost; /* Total rtx_cost of the multiplication sequence. */
2161 short latency; /* The latency of the multiplication sequence. */
2164 /* This macro is used to compare a pointer to a mult_cost against an
2165 single integer "rtx_cost" value. This is equivalent to the macro
2166 CHEAPER_MULT_COST(X,Z) where Z = {Y,Y}. */
2167 #define MULT_COST_LESS(X,Y) ((X)->cost < (Y) \
2168 || ((X)->cost == (Y) && (X)->latency < (Y)))
2170 /* This macro is used to compare two pointers to mult_costs against
2171 each other. The macro returns true if X is cheaper than Y.
2172 Currently, the cheaper of two mult_costs is the one with the
2173 lower "cost". If "cost"s are tied, the lower latency is cheaper. */
2174 #define CHEAPER_MULT_COST(X,Y) ((X)->cost < (Y)->cost \
2175 || ((X)->cost == (Y)->cost \
2176 && (X)->latency < (Y)->latency))
2178 /* This structure records a sequence of operations.
2179 `ops' is the number of operations recorded.
2180 `cost' is their total cost.
2181 The operations are stored in `op' and the corresponding
2182 logarithms of the integer coefficients in `log'.
2184 These are the operations:
2185 alg_zero total := 0;
2186 alg_m total := multiplicand;
2187 alg_shift total := total * coeff
2188 alg_add_t_m2 total := total + multiplicand * coeff;
2189 alg_sub_t_m2 total := total - multiplicand * coeff;
2190 alg_add_factor total := total * coeff + total;
2191 alg_sub_factor total := total * coeff - total;
2192 alg_add_t2_m total := total * coeff + multiplicand;
2193 alg_sub_t2_m total := total * coeff - multiplicand;
2195 The first operand must be either alg_zero or alg_m. */
2197 struct algorithm
2199 struct mult_cost cost;
2200 short ops;
2201 /* The size of the OP and LOG fields are not directly related to the
2202 word size, but the worst-case algorithms will be if we have few
2203 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
2204 In that case we will generate shift-by-2, add, shift-by-2, add,...,
2205 in total wordsize operations. */
2206 enum alg_code op[MAX_BITS_PER_WORD];
2207 char log[MAX_BITS_PER_WORD];
2210 /* Indicates the type of fixup needed after a constant multiplication.
2211 BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2212 the result should be negated, and ADD_VARIANT means that the
2213 multiplicand should be added to the result. */
2214 enum mult_variant {basic_variant, negate_variant, add_variant};
2216 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2217 const struct mult_cost *, enum machine_mode mode);
2218 static bool choose_mult_variant (enum machine_mode, HOST_WIDE_INT,
2219 struct algorithm *, enum mult_variant *, int);
2220 static rtx expand_mult_const (enum machine_mode, rtx, HOST_WIDE_INT, rtx,
2221 const struct algorithm *, enum mult_variant);
2222 static unsigned HOST_WIDE_INT choose_multiplier (unsigned HOST_WIDE_INT, int,
2223 int, unsigned HOST_WIDE_INT *,
2224 int *, int *);
2225 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2226 static rtx extract_high_half (enum machine_mode, rtx);
2227 static rtx expand_mult_highpart_optab (enum machine_mode, rtx, rtx, rtx,
2228 int, int);
2229 /* Compute and return the best algorithm for multiplying by T.
2230 The algorithm must cost less than cost_limit
2231 If retval.cost >= COST_LIMIT, no algorithm was found and all
2232 other field of the returned struct are undefined.
2233 MODE is the machine mode of the multiplication. */
2235 static void
2236 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2237 const struct mult_cost *cost_limit, enum machine_mode mode)
2239 int m;
2240 struct algorithm *alg_in, *best_alg;
2241 struct mult_cost best_cost;
2242 struct mult_cost new_limit;
2243 int op_cost, op_latency;
2244 unsigned HOST_WIDE_INT q;
2245 int maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (mode));
2247 /* Indicate that no algorithm is yet found. If no algorithm
2248 is found, this value will be returned and indicate failure. */
2249 alg_out->cost.cost = cost_limit->cost + 1;
2250 alg_out->cost.latency = cost_limit->latency + 1;
2252 if (cost_limit->cost < 0
2253 || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2254 return;
2256 /* Restrict the bits of "t" to the multiplication's mode. */
2257 t &= GET_MODE_MASK (mode);
2259 /* t == 1 can be done in zero cost. */
2260 if (t == 1)
2262 alg_out->ops = 1;
2263 alg_out->cost.cost = 0;
2264 alg_out->cost.latency = 0;
2265 alg_out->op[0] = alg_m;
2266 return;
2269 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2270 fail now. */
2271 if (t == 0)
2273 if (MULT_COST_LESS (cost_limit, zero_cost))
2274 return;
2275 else
2277 alg_out->ops = 1;
2278 alg_out->cost.cost = zero_cost;
2279 alg_out->cost.latency = zero_cost;
2280 alg_out->op[0] = alg_zero;
2281 return;
2285 /* We'll be needing a couple extra algorithm structures now. */
2287 alg_in = alloca (sizeof (struct algorithm));
2288 best_alg = alloca (sizeof (struct algorithm));
2289 best_cost = *cost_limit;
2291 /* If we have a group of zero bits at the low-order part of T, try
2292 multiplying by the remaining bits and then doing a shift. */
2294 if ((t & 1) == 0)
2296 m = floor_log2 (t & -t); /* m = number of low zero bits */
2297 if (m < maxm)
2299 q = t >> m;
2300 /* The function expand_shift will choose between a shift and
2301 a sequence of additions, so the observed cost is given as
2302 MIN (m * add_cost[mode], shift_cost[mode][m]). */
2303 op_cost = m * add_cost[mode];
2304 if (shift_cost[mode][m] < op_cost)
2305 op_cost = shift_cost[mode][m];
2306 new_limit.cost = best_cost.cost - op_cost;
2307 new_limit.latency = best_cost.latency - op_cost;
2308 synth_mult (alg_in, q, &new_limit, mode);
2310 alg_in->cost.cost += op_cost;
2311 alg_in->cost.latency += op_cost;
2312 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2314 struct algorithm *x;
2315 best_cost = alg_in->cost;
2316 x = alg_in, alg_in = best_alg, best_alg = x;
2317 best_alg->log[best_alg->ops] = m;
2318 best_alg->op[best_alg->ops] = alg_shift;
2323 /* If we have an odd number, add or subtract one. */
2324 if ((t & 1) != 0)
2326 unsigned HOST_WIDE_INT w;
2328 for (w = 1; (w & t) != 0; w <<= 1)
2330 /* If T was -1, then W will be zero after the loop. This is another
2331 case where T ends with ...111. Handling this with (T + 1) and
2332 subtract 1 produces slightly better code and results in algorithm
2333 selection much faster than treating it like the ...0111 case
2334 below. */
2335 if (w == 0
2336 || (w > 2
2337 /* Reject the case where t is 3.
2338 Thus we prefer addition in that case. */
2339 && t != 3))
2341 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2343 op_cost = add_cost[mode];
2344 new_limit.cost = best_cost.cost - op_cost;
2345 new_limit.latency = best_cost.latency - op_cost;
2346 synth_mult (alg_in, t + 1, &new_limit, mode);
2348 alg_in->cost.cost += op_cost;
2349 alg_in->cost.latency += op_cost;
2350 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2352 struct algorithm *x;
2353 best_cost = alg_in->cost;
2354 x = alg_in, alg_in = best_alg, best_alg = x;
2355 best_alg->log[best_alg->ops] = 0;
2356 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2359 else
2361 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2363 op_cost = add_cost[mode];
2364 new_limit.cost = best_cost.cost - op_cost;
2365 new_limit.latency = best_cost.latency - op_cost;
2366 synth_mult (alg_in, t - 1, &new_limit, mode);
2368 alg_in->cost.cost += op_cost;
2369 alg_in->cost.latency += op_cost;
2370 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2372 struct algorithm *x;
2373 best_cost = alg_in->cost;
2374 x = alg_in, alg_in = best_alg, best_alg = x;
2375 best_alg->log[best_alg->ops] = 0;
2376 best_alg->op[best_alg->ops] = alg_add_t_m2;
2381 /* Look for factors of t of the form
2382 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2383 If we find such a factor, we can multiply by t using an algorithm that
2384 multiplies by q, shift the result by m and add/subtract it to itself.
2386 We search for large factors first and loop down, even if large factors
2387 are less probable than small; if we find a large factor we will find a
2388 good sequence quickly, and therefore be able to prune (by decreasing
2389 COST_LIMIT) the search. */
2391 for (m = floor_log2 (t - 1); m >= 2; m--)
2393 unsigned HOST_WIDE_INT d;
2395 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2396 if (t % d == 0 && t > d && m < maxm)
2398 /* If the target has a cheap shift-and-add instruction use
2399 that in preference to a shift insn followed by an add insn.
2400 Assume that the shift-and-add is "atomic" with a latency
2401 equal to it's cost, otherwise assume that on superscalar
2402 hardware the shift may be executed concurrently with the
2403 earlier steps in the algorithm. */
2404 op_cost = add_cost[mode] + shift_cost[mode][m];
2405 if (shiftadd_cost[mode][m] < op_cost)
2407 op_cost = shiftadd_cost[mode][m];
2408 op_latency = op_cost;
2410 else
2411 op_latency = add_cost[mode];
2413 new_limit.cost = best_cost.cost - op_cost;
2414 new_limit.latency = best_cost.latency - op_latency;
2415 synth_mult (alg_in, t / d, &new_limit, mode);
2417 alg_in->cost.cost += op_cost;
2418 alg_in->cost.latency += op_latency;
2419 if (alg_in->cost.latency < op_cost)
2420 alg_in->cost.latency = op_cost;
2421 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2423 struct algorithm *x;
2424 best_cost = alg_in->cost;
2425 x = alg_in, alg_in = best_alg, best_alg = x;
2426 best_alg->log[best_alg->ops] = m;
2427 best_alg->op[best_alg->ops] = alg_add_factor;
2429 /* Other factors will have been taken care of in the recursion. */
2430 break;
2433 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2434 if (t % d == 0 && t > d && m < maxm)
2436 /* If the target has a cheap shift-and-subtract insn use
2437 that in preference to a shift insn followed by a sub insn.
2438 Assume that the shift-and-sub is "atomic" with a latency
2439 equal to it's cost, otherwise assume that on superscalar
2440 hardware the shift may be executed concurrently with the
2441 earlier steps in the algorithm. */
2442 op_cost = add_cost[mode] + shift_cost[mode][m];
2443 if (shiftsub_cost[mode][m] < op_cost)
2445 op_cost = shiftsub_cost[mode][m];
2446 op_latency = op_cost;
2448 else
2449 op_latency = add_cost[mode];
2451 new_limit.cost = best_cost.cost - op_cost;
2452 new_limit.cost = best_cost.cost - op_latency;
2453 synth_mult (alg_in, t / d, &new_limit, mode);
2455 alg_in->cost.cost += op_cost;
2456 alg_in->cost.latency += op_latency;
2457 if (alg_in->cost.latency < op_cost)
2458 alg_in->cost.latency = op_cost;
2459 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2461 struct algorithm *x;
2462 best_cost = alg_in->cost;
2463 x = alg_in, alg_in = best_alg, best_alg = x;
2464 best_alg->log[best_alg->ops] = m;
2465 best_alg->op[best_alg->ops] = alg_sub_factor;
2467 break;
2471 /* Try shift-and-add (load effective address) instructions,
2472 i.e. do a*3, a*5, a*9. */
2473 if ((t & 1) != 0)
2475 q = t - 1;
2476 q = q & -q;
2477 m = exact_log2 (q);
2478 if (m >= 0 && m < maxm)
2480 op_cost = shiftadd_cost[mode][m];
2481 new_limit.cost = best_cost.cost - op_cost;
2482 new_limit.latency = best_cost.latency - op_cost;
2483 synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2485 alg_in->cost.cost += op_cost;
2486 alg_in->cost.latency += op_cost;
2487 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2489 struct algorithm *x;
2490 best_cost = alg_in->cost;
2491 x = alg_in, alg_in = best_alg, best_alg = x;
2492 best_alg->log[best_alg->ops] = m;
2493 best_alg->op[best_alg->ops] = alg_add_t2_m;
2497 q = t + 1;
2498 q = q & -q;
2499 m = exact_log2 (q);
2500 if (m >= 0 && m < maxm)
2502 op_cost = shiftsub_cost[mode][m];
2503 new_limit.cost = best_cost.cost - op_cost;
2504 new_limit.latency = best_cost.latency - op_cost;
2505 synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2507 alg_in->cost.cost += op_cost;
2508 alg_in->cost.latency += op_cost;
2509 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2511 struct algorithm *x;
2512 best_cost = alg_in->cost;
2513 x = alg_in, alg_in = best_alg, best_alg = x;
2514 best_alg->log[best_alg->ops] = m;
2515 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2520 /* If best_cost has not decreased, we have not found any algorithm. */
2521 if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
2522 return;
2524 /* If we are getting a too long sequence for `struct algorithm'
2525 to record, make this search fail. */
2526 if (best_alg->ops == MAX_BITS_PER_WORD)
2527 return;
2529 /* Copy the algorithm from temporary space to the space at alg_out.
2530 We avoid using structure assignment because the majority of
2531 best_alg is normally undefined, and this is a critical function. */
2532 alg_out->ops = best_alg->ops + 1;
2533 alg_out->cost = best_cost;
2534 memcpy (alg_out->op, best_alg->op,
2535 alg_out->ops * sizeof *alg_out->op);
2536 memcpy (alg_out->log, best_alg->log,
2537 alg_out->ops * sizeof *alg_out->log);
2540 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2541 Try three variations:
2543 - a shift/add sequence based on VAL itself
2544 - a shift/add sequence based on -VAL, followed by a negation
2545 - a shift/add sequence based on VAL - 1, followed by an addition.
2547 Return true if the cheapest of these cost less than MULT_COST,
2548 describing the algorithm in *ALG and final fixup in *VARIANT. */
2550 static bool
2551 choose_mult_variant (enum machine_mode mode, HOST_WIDE_INT val,
2552 struct algorithm *alg, enum mult_variant *variant,
2553 int mult_cost)
2555 struct algorithm alg2;
2556 struct mult_cost limit;
2557 int op_cost;
2559 *variant = basic_variant;
2560 limit.cost = mult_cost;
2561 limit.latency = mult_cost;
2562 synth_mult (alg, val, &limit, mode);
2564 /* This works only if the inverted value actually fits in an
2565 `unsigned int' */
2566 if (HOST_BITS_PER_INT >= GET_MODE_BITSIZE (mode))
2568 op_cost = neg_cost[mode];
2569 if (MULT_COST_LESS (&alg->cost, mult_cost))
2571 limit.cost = alg->cost.cost - op_cost;
2572 limit.latency = alg->cost.latency - op_cost;
2574 else
2576 limit.cost = mult_cost - op_cost;
2577 limit.latency = mult_cost - op_cost;
2580 synth_mult (&alg2, -val, &limit, mode);
2581 alg2.cost.cost += op_cost;
2582 alg2.cost.latency += op_cost;
2583 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2584 *alg = alg2, *variant = negate_variant;
2587 /* This proves very useful for division-by-constant. */
2588 op_cost = add_cost[mode];
2589 if (MULT_COST_LESS (&alg->cost, mult_cost))
2591 limit.cost = alg->cost.cost - op_cost;
2592 limit.latency = alg->cost.latency - op_cost;
2594 else
2596 limit.cost = mult_cost - op_cost;
2597 limit.latency = mult_cost - op_cost;
2600 synth_mult (&alg2, val - 1, &limit, mode);
2601 alg2.cost.cost += op_cost;
2602 alg2.cost.latency += op_cost;
2603 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2604 *alg = alg2, *variant = add_variant;
2606 return MULT_COST_LESS (&alg->cost, mult_cost);
2609 /* A subroutine of expand_mult, used for constant multiplications.
2610 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2611 convenient. Use the shift/add sequence described by ALG and apply
2612 the final fixup specified by VARIANT. */
2614 static rtx
2615 expand_mult_const (enum machine_mode mode, rtx op0, HOST_WIDE_INT val,
2616 rtx target, const struct algorithm *alg,
2617 enum mult_variant variant)
2619 HOST_WIDE_INT val_so_far;
2620 rtx insn, accum, tem;
2621 int opno;
2622 enum machine_mode nmode;
2624 /* Avoid referencing memory over and over.
2625 For speed, but also for correctness when mem is volatile. */
2626 if (MEM_P (op0))
2627 op0 = force_reg (mode, op0);
2629 /* ACCUM starts out either as OP0 or as a zero, depending on
2630 the first operation. */
2632 if (alg->op[0] == alg_zero)
2634 accum = copy_to_mode_reg (mode, const0_rtx);
2635 val_so_far = 0;
2637 else if (alg->op[0] == alg_m)
2639 accum = copy_to_mode_reg (mode, op0);
2640 val_so_far = 1;
2642 else
2643 gcc_unreachable ();
2645 for (opno = 1; opno < alg->ops; opno++)
2647 int log = alg->log[opno];
2648 rtx shift_subtarget = optimize ? 0 : accum;
2649 rtx add_target
2650 = (opno == alg->ops - 1 && target != 0 && variant != add_variant
2651 && !optimize)
2652 ? target : 0;
2653 rtx accum_target = optimize ? 0 : accum;
2655 switch (alg->op[opno])
2657 case alg_shift:
2658 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2659 build_int_cst (NULL_TREE, log),
2660 NULL_RTX, 0);
2661 val_so_far <<= log;
2662 break;
2664 case alg_add_t_m2:
2665 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2666 build_int_cst (NULL_TREE, log),
2667 NULL_RTX, 0);
2668 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2669 add_target ? add_target : accum_target);
2670 val_so_far += (HOST_WIDE_INT) 1 << log;
2671 break;
2673 case alg_sub_t_m2:
2674 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2675 build_int_cst (NULL_TREE, log),
2676 NULL_RTX, 0);
2677 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
2678 add_target ? add_target : accum_target);
2679 val_so_far -= (HOST_WIDE_INT) 1 << log;
2680 break;
2682 case alg_add_t2_m:
2683 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2684 build_int_cst (NULL_TREE, log),
2685 shift_subtarget,
2687 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
2688 add_target ? add_target : accum_target);
2689 val_so_far = (val_so_far << log) + 1;
2690 break;
2692 case alg_sub_t2_m:
2693 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2694 build_int_cst (NULL_TREE, log),
2695 shift_subtarget, 0);
2696 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
2697 add_target ? add_target : accum_target);
2698 val_so_far = (val_so_far << log) - 1;
2699 break;
2701 case alg_add_factor:
2702 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2703 build_int_cst (NULL_TREE, log),
2704 NULL_RTX, 0);
2705 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2706 add_target ? add_target : accum_target);
2707 val_so_far += val_so_far << log;
2708 break;
2710 case alg_sub_factor:
2711 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2712 build_int_cst (NULL_TREE, log),
2713 NULL_RTX, 0);
2714 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
2715 (add_target
2716 ? add_target : (optimize ? 0 : tem)));
2717 val_so_far = (val_so_far << log) - val_so_far;
2718 break;
2720 default:
2721 gcc_unreachable ();
2724 /* Write a REG_EQUAL note on the last insn so that we can cse
2725 multiplication sequences. Note that if ACCUM is a SUBREG,
2726 we've set the inner register and must properly indicate
2727 that. */
2729 tem = op0, nmode = mode;
2730 if (GET_CODE (accum) == SUBREG)
2732 nmode = GET_MODE (SUBREG_REG (accum));
2733 tem = gen_lowpart (nmode, op0);
2736 insn = get_last_insn ();
2737 set_unique_reg_note (insn, REG_EQUAL,
2738 gen_rtx_MULT (nmode, tem, GEN_INT (val_so_far)));
2741 if (variant == negate_variant)
2743 val_so_far = -val_so_far;
2744 accum = expand_unop (mode, neg_optab, accum, target, 0);
2746 else if (variant == add_variant)
2748 val_so_far = val_so_far + 1;
2749 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
2752 /* Compare only the bits of val and val_so_far that are significant
2753 in the result mode, to avoid sign-/zero-extension confusion. */
2754 val &= GET_MODE_MASK (mode);
2755 val_so_far &= GET_MODE_MASK (mode);
2756 gcc_assert (val == val_so_far);
2758 return accum;
2761 /* Perform a multiplication and return an rtx for the result.
2762 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
2763 TARGET is a suggestion for where to store the result (an rtx).
2765 We check specially for a constant integer as OP1.
2766 If you want this check for OP0 as well, then before calling
2767 you should swap the two operands if OP0 would be constant. */
2770 expand_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
2771 int unsignedp)
2773 rtx const_op1 = op1;
2774 enum mult_variant variant;
2775 struct algorithm algorithm;
2777 /* synth_mult does an `unsigned int' multiply. As long as the mode is
2778 less than or equal in size to `unsigned int' this doesn't matter.
2779 If the mode is larger than `unsigned int', then synth_mult works only
2780 if the constant value exactly fits in an `unsigned int' without any
2781 truncation. This means that multiplying by negative values does
2782 not work; results are off by 2^32 on a 32 bit machine. */
2784 /* If we are multiplying in DImode, it may still be a win
2785 to try to work with shifts and adds. */
2786 if (GET_CODE (op1) == CONST_DOUBLE
2787 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
2788 && HOST_BITS_PER_INT >= BITS_PER_WORD
2789 && CONST_DOUBLE_HIGH (op1) == 0)
2790 const_op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2791 else if (HOST_BITS_PER_INT < GET_MODE_BITSIZE (mode)
2792 && GET_CODE (op1) == CONST_INT
2793 && INTVAL (op1) < 0)
2794 const_op1 = 0;
2796 /* We used to test optimize here, on the grounds that it's better to
2797 produce a smaller program when -O is not used.
2798 But this causes such a terrible slowdown sometimes
2799 that it seems better to use synth_mult always. */
2801 if (const_op1 && GET_CODE (const_op1) == CONST_INT
2802 && (unsignedp || !flag_trapv))
2804 int mult_cost = rtx_cost (gen_rtx_MULT (mode, op0, op1), SET);
2806 if (choose_mult_variant (mode, INTVAL (const_op1), &algorithm, &variant,
2807 mult_cost))
2808 return expand_mult_const (mode, op0, INTVAL (const_op1), target,
2809 &algorithm, variant);
2812 if (GET_CODE (op0) == CONST_DOUBLE)
2814 rtx temp = op0;
2815 op0 = op1;
2816 op1 = temp;
2819 /* Expand x*2.0 as x+x. */
2820 if (GET_CODE (op1) == CONST_DOUBLE
2821 && GET_MODE_CLASS (mode) == MODE_FLOAT)
2823 REAL_VALUE_TYPE d;
2824 REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
2826 if (REAL_VALUES_EQUAL (d, dconst2))
2828 op0 = force_reg (GET_MODE (op0), op0);
2829 return expand_binop (mode, add_optab, op0, op0,
2830 target, unsignedp, OPTAB_LIB_WIDEN);
2834 /* This used to use umul_optab if unsigned, but for non-widening multiply
2835 there is no difference between signed and unsigned. */
2836 op0 = expand_binop (mode,
2837 ! unsignedp
2838 && flag_trapv && (GET_MODE_CLASS(mode) == MODE_INT)
2839 ? smulv_optab : smul_optab,
2840 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
2841 gcc_assert (op0);
2842 return op0;
2845 /* Return the smallest n such that 2**n >= X. */
2848 ceil_log2 (unsigned HOST_WIDE_INT x)
2850 return floor_log2 (x - 1) + 1;
2853 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
2854 replace division by D, and put the least significant N bits of the result
2855 in *MULTIPLIER_PTR and return the most significant bit.
2857 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
2858 needed precision is in PRECISION (should be <= N).
2860 PRECISION should be as small as possible so this function can choose
2861 multiplier more freely.
2863 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
2864 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
2866 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
2867 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
2869 static
2870 unsigned HOST_WIDE_INT
2871 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
2872 unsigned HOST_WIDE_INT *multiplier_ptr,
2873 int *post_shift_ptr, int *lgup_ptr)
2875 HOST_WIDE_INT mhigh_hi, mlow_hi;
2876 unsigned HOST_WIDE_INT mhigh_lo, mlow_lo;
2877 int lgup, post_shift;
2878 int pow, pow2;
2879 unsigned HOST_WIDE_INT nl, dummy1;
2880 HOST_WIDE_INT nh, dummy2;
2882 /* lgup = ceil(log2(divisor)); */
2883 lgup = ceil_log2 (d);
2885 gcc_assert (lgup <= n);
2887 pow = n + lgup;
2888 pow2 = n + lgup - precision;
2890 /* We could handle this with some effort, but this case is much
2891 better handled directly with a scc insn, so rely on caller using
2892 that. */
2893 gcc_assert (pow != 2 * HOST_BITS_PER_WIDE_INT);
2895 /* mlow = 2^(N + lgup)/d */
2896 if (pow >= HOST_BITS_PER_WIDE_INT)
2898 nh = (HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
2899 nl = 0;
2901 else
2903 nh = 0;
2904 nl = (unsigned HOST_WIDE_INT) 1 << pow;
2906 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
2907 &mlow_lo, &mlow_hi, &dummy1, &dummy2);
2909 /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
2910 if (pow2 >= HOST_BITS_PER_WIDE_INT)
2911 nh |= (HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
2912 else
2913 nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
2914 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
2915 &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
2917 gcc_assert (!mhigh_hi || nh - d < d);
2918 gcc_assert (mhigh_hi <= 1 && mlow_hi <= 1);
2919 /* Assert that mlow < mhigh. */
2920 gcc_assert (mlow_hi < mhigh_hi
2921 || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo));
2923 /* If precision == N, then mlow, mhigh exceed 2^N
2924 (but they do not exceed 2^(N+1)). */
2926 /* Reduce to lowest terms. */
2927 for (post_shift = lgup; post_shift > 0; post_shift--)
2929 unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
2930 unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
2931 if (ml_lo >= mh_lo)
2932 break;
2934 mlow_hi = 0;
2935 mlow_lo = ml_lo;
2936 mhigh_hi = 0;
2937 mhigh_lo = mh_lo;
2940 *post_shift_ptr = post_shift;
2941 *lgup_ptr = lgup;
2942 if (n < HOST_BITS_PER_WIDE_INT)
2944 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
2945 *multiplier_ptr = mhigh_lo & mask;
2946 return mhigh_lo >= mask;
2948 else
2950 *multiplier_ptr = mhigh_lo;
2951 return mhigh_hi;
2955 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
2956 congruent to 1 (mod 2**N). */
2958 static unsigned HOST_WIDE_INT
2959 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
2961 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
2963 /* The algorithm notes that the choice y = x satisfies
2964 x*y == 1 mod 2^3, since x is assumed odd.
2965 Each iteration doubles the number of bits of significance in y. */
2967 unsigned HOST_WIDE_INT mask;
2968 unsigned HOST_WIDE_INT y = x;
2969 int nbit = 3;
2971 mask = (n == HOST_BITS_PER_WIDE_INT
2972 ? ~(unsigned HOST_WIDE_INT) 0
2973 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
2975 while (nbit < n)
2977 y = y * (2 - x*y) & mask; /* Modulo 2^N */
2978 nbit *= 2;
2980 return y;
2983 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
2984 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
2985 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
2986 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
2987 become signed.
2989 The result is put in TARGET if that is convenient.
2991 MODE is the mode of operation. */
2994 expand_mult_highpart_adjust (enum machine_mode mode, rtx adj_operand, rtx op0,
2995 rtx op1, rtx target, int unsignedp)
2997 rtx tem;
2998 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3000 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3001 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3002 NULL_RTX, 0);
3003 tem = expand_and (mode, tem, op1, NULL_RTX);
3004 adj_operand
3005 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3006 adj_operand);
3008 tem = expand_shift (RSHIFT_EXPR, mode, op1,
3009 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3010 NULL_RTX, 0);
3011 tem = expand_and (mode, tem, op0, NULL_RTX);
3012 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3013 target);
3015 return target;
3018 /* Subroutine of expand_mult_highpart. Return the MODE high part of OP. */
3020 static rtx
3021 extract_high_half (enum machine_mode mode, rtx op)
3023 enum machine_mode wider_mode;
3025 if (mode == word_mode)
3026 return gen_highpart (mode, op);
3028 wider_mode = GET_MODE_WIDER_MODE (mode);
3029 op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3030 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode)), 0, 1);
3031 return convert_modes (mode, wider_mode, op, 0);
3034 /* Like expand_mult_highpart, but only consider using a multiplication
3035 optab. OP1 is an rtx for the constant operand. */
3037 static rtx
3038 expand_mult_highpart_optab (enum machine_mode mode, rtx op0, rtx op1,
3039 rtx target, int unsignedp, int max_cost)
3041 rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3042 enum machine_mode wider_mode;
3043 optab moptab;
3044 rtx tem;
3045 int size;
3047 wider_mode = GET_MODE_WIDER_MODE (mode);
3048 size = GET_MODE_BITSIZE (mode);
3050 /* Firstly, try using a multiplication insn that only generates the needed
3051 high part of the product, and in the sign flavor of unsignedp. */
3052 if (mul_highpart_cost[mode] < max_cost)
3054 moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3055 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3056 unsignedp, OPTAB_DIRECT);
3057 if (tem)
3058 return tem;
3061 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3062 Need to adjust the result after the multiplication. */
3063 if (size - 1 < BITS_PER_WORD
3064 && (mul_highpart_cost[mode] + 2 * shift_cost[mode][size-1]
3065 + 4 * add_cost[mode] < max_cost))
3067 moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3068 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3069 unsignedp, OPTAB_DIRECT);
3070 if (tem)
3071 /* We used the wrong signedness. Adjust the result. */
3072 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3073 tem, unsignedp);
3076 /* Try widening multiplication. */
3077 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3078 if (moptab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3079 && mul_widen_cost[wider_mode] < max_cost)
3081 tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3082 unsignedp, OPTAB_WIDEN);
3083 if (tem)
3084 return extract_high_half (mode, tem);
3087 /* Try widening the mode and perform a non-widening multiplication. */
3088 moptab = smul_optab;
3089 if (smul_optab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3090 && size - 1 < BITS_PER_WORD
3091 && mul_cost[wider_mode] + shift_cost[mode][size-1] < max_cost)
3093 tem = expand_binop (wider_mode, moptab, op0, op1, 0,
3094 unsignedp, OPTAB_WIDEN);
3095 if (tem)
3096 return extract_high_half (mode, tem);
3099 /* Try widening multiplication of opposite signedness, and adjust. */
3100 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3101 if (moptab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3102 && size - 1 < BITS_PER_WORD
3103 && (mul_widen_cost[wider_mode] + 2 * shift_cost[mode][size-1]
3104 + 4 * add_cost[mode] < max_cost))
3106 tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3107 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3108 if (tem != 0)
3110 tem = extract_high_half (mode, tem);
3111 /* We used the wrong signedness. Adjust the result. */
3112 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3113 target, unsignedp);
3117 return 0;
3120 /* Emit code to multiply OP0 and CNST1, putting the high half of the result
3121 in TARGET if that is convenient, and return where the result is. If the
3122 operation can not be performed, 0 is returned.
3124 MODE is the mode of operation and result.
3126 UNSIGNEDP nonzero means unsigned multiply.
3128 MAX_COST is the total allowed cost for the expanded RTL. */
3131 expand_mult_highpart (enum machine_mode mode, rtx op0,
3132 unsigned HOST_WIDE_INT cnst1, rtx target,
3133 int unsignedp, int max_cost)
3135 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
3136 int extra_cost;
3137 bool sign_adjust = false;
3138 enum mult_variant variant;
3139 struct algorithm alg;
3140 rtx op1, tem;
3142 /* We can't support modes wider than HOST_BITS_PER_INT. */
3143 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT);
3145 op1 = gen_int_mode (cnst1, wider_mode);
3146 cnst1 &= GET_MODE_MASK (mode);
3148 /* We can't optimize modes wider than BITS_PER_WORD.
3149 ??? We might be able to perform double-word arithmetic if
3150 mode == word_mode, however all the cost calculations in
3151 synth_mult etc. assume single-word operations. */
3152 if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3153 return expand_mult_highpart_optab (mode, op0, op1, target,
3154 unsignedp, max_cost);
3156 extra_cost = shift_cost[mode][GET_MODE_BITSIZE (mode) - 1];
3158 /* Check whether we try to multiply by a negative constant. */
3159 if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3161 sign_adjust = true;
3162 extra_cost += add_cost[mode];
3165 /* See whether shift/add multiplication is cheap enough. */
3166 if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3167 max_cost - extra_cost))
3169 /* See whether the specialized multiplication optabs are
3170 cheaper than the shift/add version. */
3171 tem = expand_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3172 alg.cost.cost + extra_cost);
3173 if (tem)
3174 return tem;
3176 tem = convert_to_mode (wider_mode, op0, unsignedp);
3177 tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3178 tem = extract_high_half (mode, tem);
3180 /* Adjust result for signedness. */
3181 if (sign_adjust)
3182 tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3184 return tem;
3186 return expand_mult_highpart_optab (mode, op0, op1, target,
3187 unsignedp, max_cost);
3191 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3193 static rtx
3194 expand_smod_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3196 unsigned HOST_WIDE_INT mask;
3197 rtx result, temp, shift, label;
3198 int logd;
3200 logd = floor_log2 (d);
3201 result = gen_reg_rtx (mode);
3203 /* Avoid conditional branches when they're expensive. */
3204 if (BRANCH_COST >= 2
3205 && !optimize_size)
3207 rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3208 mode, 0, -1);
3209 if (signmask)
3211 signmask = force_reg (mode, signmask);
3212 mask = ((HOST_WIDE_INT) 1 << logd) - 1;
3213 shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3215 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3216 which instruction sequence to use. If logical right shifts
3217 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3218 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3220 temp = gen_rtx_LSHIFTRT (mode, result, shift);
3221 if (lshr_optab->handlers[mode].insn_code == CODE_FOR_nothing
3222 || rtx_cost (temp, SET) > COSTS_N_INSNS (2))
3224 temp = expand_binop (mode, xor_optab, op0, signmask,
3225 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3226 temp = expand_binop (mode, sub_optab, temp, signmask,
3227 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3228 temp = expand_binop (mode, and_optab, temp, GEN_INT (mask),
3229 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3230 temp = expand_binop (mode, xor_optab, temp, signmask,
3231 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3232 temp = expand_binop (mode, sub_optab, temp, signmask,
3233 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3235 else
3237 signmask = expand_binop (mode, lshr_optab, signmask, shift,
3238 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3239 signmask = force_reg (mode, signmask);
3241 temp = expand_binop (mode, add_optab, op0, signmask,
3242 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3243 temp = expand_binop (mode, and_optab, temp, GEN_INT (mask),
3244 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3245 temp = expand_binop (mode, sub_optab, temp, signmask,
3246 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3248 return temp;
3252 /* Mask contains the mode's signbit and the significant bits of the
3253 modulus. By including the signbit in the operation, many targets
3254 can avoid an explicit compare operation in the following comparison
3255 against zero. */
3257 mask = (HOST_WIDE_INT) -1 << (GET_MODE_BITSIZE (mode) - 1)
3258 | (((HOST_WIDE_INT) 1 << logd) - 1);
3260 temp = expand_binop (mode, and_optab, op0, GEN_INT (mask), result,
3261 1, OPTAB_LIB_WIDEN);
3262 if (temp != result)
3263 emit_move_insn (result, temp);
3265 label = gen_label_rtx ();
3266 do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3268 temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3269 0, OPTAB_LIB_WIDEN);
3270 mask = (HOST_WIDE_INT) -1 << logd;
3271 temp = expand_binop (mode, ior_optab, temp, GEN_INT (mask), result,
3272 1, OPTAB_LIB_WIDEN);
3273 temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3274 0, OPTAB_LIB_WIDEN);
3275 if (temp != result)
3276 emit_move_insn (result, temp);
3277 emit_label (label);
3278 return result;
3281 /* Expand signed division of OP0 by a power of two D in mode MODE.
3282 This routine is only called for positive values of D. */
3284 static rtx
3285 expand_sdiv_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3287 rtx temp, label;
3288 tree shift;
3289 int logd;
3291 logd = floor_log2 (d);
3292 shift = build_int_cst (NULL_TREE, logd);
3294 if (d == 2 && BRANCH_COST >= 1)
3296 temp = gen_reg_rtx (mode);
3297 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3298 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3299 0, OPTAB_LIB_WIDEN);
3300 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3303 #ifdef HAVE_conditional_move
3304 if (BRANCH_COST >= 2)
3306 rtx temp2;
3308 start_sequence ();
3309 temp2 = copy_to_mode_reg (mode, op0);
3310 temp = expand_binop (mode, add_optab, temp2, GEN_INT (d-1),
3311 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3312 temp = force_reg (mode, temp);
3314 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3315 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3316 mode, temp, temp2, mode, 0);
3317 if (temp2)
3319 rtx seq = get_insns ();
3320 end_sequence ();
3321 emit_insn (seq);
3322 return expand_shift (RSHIFT_EXPR, mode, temp2, shift, NULL_RTX, 0);
3324 end_sequence ();
3326 #endif
3328 if (BRANCH_COST >= 2)
3330 int ushift = GET_MODE_BITSIZE (mode) - logd;
3332 temp = gen_reg_rtx (mode);
3333 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3334 if (shift_cost[mode][ushift] > COSTS_N_INSNS (1))
3335 temp = expand_binop (mode, and_optab, temp, GEN_INT (d - 1),
3336 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3337 else
3338 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3339 build_int_cst (NULL_TREE, ushift),
3340 NULL_RTX, 1);
3341 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3342 0, OPTAB_LIB_WIDEN);
3343 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3346 label = gen_label_rtx ();
3347 temp = copy_to_mode_reg (mode, op0);
3348 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3349 expand_inc (temp, GEN_INT (d - 1));
3350 emit_label (label);
3351 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3354 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3355 if that is convenient, and returning where the result is.
3356 You may request either the quotient or the remainder as the result;
3357 specify REM_FLAG nonzero to get the remainder.
3359 CODE is the expression code for which kind of division this is;
3360 it controls how rounding is done. MODE is the machine mode to use.
3361 UNSIGNEDP nonzero means do unsigned division. */
3363 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3364 and then correct it by or'ing in missing high bits
3365 if result of ANDI is nonzero.
3366 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3367 This could optimize to a bfexts instruction.
3368 But C doesn't use these operations, so their optimizations are
3369 left for later. */
3370 /* ??? For modulo, we don't actually need the highpart of the first product,
3371 the low part will do nicely. And for small divisors, the second multiply
3372 can also be a low-part only multiply or even be completely left out.
3373 E.g. to calculate the remainder of a division by 3 with a 32 bit
3374 multiply, multiply with 0x55555556 and extract the upper two bits;
3375 the result is exact for inputs up to 0x1fffffff.
3376 The input range can be reduced by using cross-sum rules.
3377 For odd divisors >= 3, the following table gives right shift counts
3378 so that if a number is shifted by an integer multiple of the given
3379 amount, the remainder stays the same:
3380 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3381 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3382 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3383 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3384 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3386 Cross-sum rules for even numbers can be derived by leaving as many bits
3387 to the right alone as the divisor has zeros to the right.
3388 E.g. if x is an unsigned 32 bit number:
3389 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3392 #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
3395 expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
3396 rtx op0, rtx op1, rtx target, int unsignedp)
3398 enum machine_mode compute_mode;
3399 rtx tquotient;
3400 rtx quotient = 0, remainder = 0;
3401 rtx last;
3402 int size;
3403 rtx insn, set;
3404 optab optab1, optab2;
3405 int op1_is_constant, op1_is_pow2 = 0;
3406 int max_cost, extra_cost;
3407 static HOST_WIDE_INT last_div_const = 0;
3408 static HOST_WIDE_INT ext_op1;
3410 op1_is_constant = GET_CODE (op1) == CONST_INT;
3411 if (op1_is_constant)
3413 ext_op1 = INTVAL (op1);
3414 if (unsignedp)
3415 ext_op1 &= GET_MODE_MASK (mode);
3416 op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
3417 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
3421 This is the structure of expand_divmod:
3423 First comes code to fix up the operands so we can perform the operations
3424 correctly and efficiently.
3426 Second comes a switch statement with code specific for each rounding mode.
3427 For some special operands this code emits all RTL for the desired
3428 operation, for other cases, it generates only a quotient and stores it in
3429 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3430 to indicate that it has not done anything.
3432 Last comes code that finishes the operation. If QUOTIENT is set and
3433 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3434 QUOTIENT is not set, it is computed using trunc rounding.
3436 We try to generate special code for division and remainder when OP1 is a
3437 constant. If |OP1| = 2**n we can use shifts and some other fast
3438 operations. For other values of OP1, we compute a carefully selected
3439 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3440 by m.
3442 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3443 half of the product. Different strategies for generating the product are
3444 implemented in expand_mult_highpart.
3446 If what we actually want is the remainder, we generate that by another
3447 by-constant multiplication and a subtraction. */
3449 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3450 code below will malfunction if we are, so check here and handle
3451 the special case if so. */
3452 if (op1 == const1_rtx)
3453 return rem_flag ? const0_rtx : op0;
3455 /* When dividing by -1, we could get an overflow.
3456 negv_optab can handle overflows. */
3457 if (! unsignedp && op1 == constm1_rtx)
3459 if (rem_flag)
3460 return const0_rtx;
3461 return expand_unop (mode, flag_trapv && GET_MODE_CLASS(mode) == MODE_INT
3462 ? negv_optab : neg_optab, op0, target, 0);
3465 if (target
3466 /* Don't use the function value register as a target
3467 since we have to read it as well as write it,
3468 and function-inlining gets confused by this. */
3469 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3470 /* Don't clobber an operand while doing a multi-step calculation. */
3471 || ((rem_flag || op1_is_constant)
3472 && (reg_mentioned_p (target, op0)
3473 || (MEM_P (op0) && MEM_P (target))))
3474 || reg_mentioned_p (target, op1)
3475 || (MEM_P (op1) && MEM_P (target))))
3476 target = 0;
3478 /* Get the mode in which to perform this computation. Normally it will
3479 be MODE, but sometimes we can't do the desired operation in MODE.
3480 If so, pick a wider mode in which we can do the operation. Convert
3481 to that mode at the start to avoid repeated conversions.
3483 First see what operations we need. These depend on the expression
3484 we are evaluating. (We assume that divxx3 insns exist under the
3485 same conditions that modxx3 insns and that these insns don't normally
3486 fail. If these assumptions are not correct, we may generate less
3487 efficient code in some cases.)
3489 Then see if we find a mode in which we can open-code that operation
3490 (either a division, modulus, or shift). Finally, check for the smallest
3491 mode for which we can do the operation with a library call. */
3493 /* We might want to refine this now that we have division-by-constant
3494 optimization. Since expand_mult_highpart tries so many variants, it is
3495 not straightforward to generalize this. Maybe we should make an array
3496 of possible modes in init_expmed? Save this for GCC 2.7. */
3498 optab1 = ((op1_is_pow2 && op1 != const0_rtx)
3499 ? (unsignedp ? lshr_optab : ashr_optab)
3500 : (unsignedp ? udiv_optab : sdiv_optab));
3501 optab2 = ((op1_is_pow2 && op1 != const0_rtx)
3502 ? optab1
3503 : (unsignedp ? udivmod_optab : sdivmod_optab));
3505 for (compute_mode = mode; compute_mode != VOIDmode;
3506 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3507 if (optab1->handlers[compute_mode].insn_code != CODE_FOR_nothing
3508 || optab2->handlers[compute_mode].insn_code != CODE_FOR_nothing)
3509 break;
3511 if (compute_mode == VOIDmode)
3512 for (compute_mode = mode; compute_mode != VOIDmode;
3513 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3514 if (optab1->handlers[compute_mode].libfunc
3515 || optab2->handlers[compute_mode].libfunc)
3516 break;
3518 /* If we still couldn't find a mode, use MODE, but we'll probably abort
3519 in expand_binop. */
3520 if (compute_mode == VOIDmode)
3521 compute_mode = mode;
3523 if (target && GET_MODE (target) == compute_mode)
3524 tquotient = target;
3525 else
3526 tquotient = gen_reg_rtx (compute_mode);
3528 size = GET_MODE_BITSIZE (compute_mode);
3529 #if 0
3530 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3531 (mode), and thereby get better code when OP1 is a constant. Do that
3532 later. It will require going over all usages of SIZE below. */
3533 size = GET_MODE_BITSIZE (mode);
3534 #endif
3536 /* Only deduct something for a REM if the last divide done was
3537 for a different constant. Then set the constant of the last
3538 divide. */
3539 max_cost = div_cost[compute_mode]
3540 - (rem_flag && ! (last_div_const != 0 && op1_is_constant
3541 && INTVAL (op1) == last_div_const)
3542 ? mul_cost[compute_mode] + add_cost[compute_mode]
3543 : 0);
3545 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
3547 /* Now convert to the best mode to use. */
3548 if (compute_mode != mode)
3550 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
3551 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
3553 /* convert_modes may have placed op1 into a register, so we
3554 must recompute the following. */
3555 op1_is_constant = GET_CODE (op1) == CONST_INT;
3556 op1_is_pow2 = (op1_is_constant
3557 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3558 || (! unsignedp
3559 && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
3562 /* If one of the operands is a volatile MEM, copy it into a register. */
3564 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
3565 op0 = force_reg (compute_mode, op0);
3566 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
3567 op1 = force_reg (compute_mode, op1);
3569 /* If we need the remainder or if OP1 is constant, we need to
3570 put OP0 in a register in case it has any queued subexpressions. */
3571 if (rem_flag || op1_is_constant)
3572 op0 = force_reg (compute_mode, op0);
3574 last = get_last_insn ();
3576 /* Promote floor rounding to trunc rounding for unsigned operations. */
3577 if (unsignedp)
3579 if (code == FLOOR_DIV_EXPR)
3580 code = TRUNC_DIV_EXPR;
3581 if (code == FLOOR_MOD_EXPR)
3582 code = TRUNC_MOD_EXPR;
3583 if (code == EXACT_DIV_EXPR && op1_is_pow2)
3584 code = TRUNC_DIV_EXPR;
3587 if (op1 != const0_rtx)
3588 switch (code)
3590 case TRUNC_MOD_EXPR:
3591 case TRUNC_DIV_EXPR:
3592 if (op1_is_constant)
3594 if (unsignedp)
3596 unsigned HOST_WIDE_INT mh, ml;
3597 int pre_shift, post_shift;
3598 int dummy;
3599 unsigned HOST_WIDE_INT d = (INTVAL (op1)
3600 & GET_MODE_MASK (compute_mode));
3602 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3604 pre_shift = floor_log2 (d);
3605 if (rem_flag)
3607 remainder
3608 = expand_binop (compute_mode, and_optab, op0,
3609 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3610 remainder, 1,
3611 OPTAB_LIB_WIDEN);
3612 if (remainder)
3613 return gen_lowpart (mode, remainder);
3615 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3616 build_int_cst (NULL_TREE,
3617 pre_shift),
3618 tquotient, 1);
3620 else if (size <= HOST_BITS_PER_WIDE_INT)
3622 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
3624 /* Most significant bit of divisor is set; emit an scc
3625 insn. */
3626 quotient = emit_store_flag (tquotient, GEU, op0, op1,
3627 compute_mode, 1, 1);
3628 if (quotient == 0)
3629 goto fail1;
3631 else
3633 /* Find a suitable multiplier and right shift count
3634 instead of multiplying with D. */
3636 mh = choose_multiplier (d, size, size,
3637 &ml, &post_shift, &dummy);
3639 /* If the suggested multiplier is more than SIZE bits,
3640 we can do better for even divisors, using an
3641 initial right shift. */
3642 if (mh != 0 && (d & 1) == 0)
3644 pre_shift = floor_log2 (d & -d);
3645 mh = choose_multiplier (d >> pre_shift, size,
3646 size - pre_shift,
3647 &ml, &post_shift, &dummy);
3648 gcc_assert (!mh);
3650 else
3651 pre_shift = 0;
3653 if (mh != 0)
3655 rtx t1, t2, t3, t4;
3657 if (post_shift - 1 >= BITS_PER_WORD)
3658 goto fail1;
3660 extra_cost
3661 = (shift_cost[compute_mode][post_shift - 1]
3662 + shift_cost[compute_mode][1]
3663 + 2 * add_cost[compute_mode]);
3664 t1 = expand_mult_highpart (compute_mode, op0, ml,
3665 NULL_RTX, 1,
3666 max_cost - extra_cost);
3667 if (t1 == 0)
3668 goto fail1;
3669 t2 = force_operand (gen_rtx_MINUS (compute_mode,
3670 op0, t1),
3671 NULL_RTX);
3672 t3 = expand_shift
3673 (RSHIFT_EXPR, compute_mode, t2,
3674 build_int_cst (NULL_TREE, 1),
3675 NULL_RTX,1);
3676 t4 = force_operand (gen_rtx_PLUS (compute_mode,
3677 t1, t3),
3678 NULL_RTX);
3679 quotient = expand_shift
3680 (RSHIFT_EXPR, compute_mode, t4,
3681 build_int_cst (NULL_TREE, post_shift - 1),
3682 tquotient, 1);
3684 else
3686 rtx t1, t2;
3688 if (pre_shift >= BITS_PER_WORD
3689 || post_shift >= BITS_PER_WORD)
3690 goto fail1;
3692 t1 = expand_shift
3693 (RSHIFT_EXPR, compute_mode, op0,
3694 build_int_cst (NULL_TREE, pre_shift),
3695 NULL_RTX, 1);
3696 extra_cost
3697 = (shift_cost[compute_mode][pre_shift]
3698 + shift_cost[compute_mode][post_shift]);
3699 t2 = expand_mult_highpart (compute_mode, t1, ml,
3700 NULL_RTX, 1,
3701 max_cost - extra_cost);
3702 if (t2 == 0)
3703 goto fail1;
3704 quotient = expand_shift
3705 (RSHIFT_EXPR, compute_mode, t2,
3706 build_int_cst (NULL_TREE, post_shift),
3707 tquotient, 1);
3711 else /* Too wide mode to use tricky code */
3712 break;
3714 insn = get_last_insn ();
3715 if (insn != last
3716 && (set = single_set (insn)) != 0
3717 && SET_DEST (set) == quotient)
3718 set_unique_reg_note (insn,
3719 REG_EQUAL,
3720 gen_rtx_UDIV (compute_mode, op0, op1));
3722 else /* TRUNC_DIV, signed */
3724 unsigned HOST_WIDE_INT ml;
3725 int lgup, post_shift;
3726 HOST_WIDE_INT d = INTVAL (op1);
3727 unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
3729 /* n rem d = n rem -d */
3730 if (rem_flag && d < 0)
3732 d = abs_d;
3733 op1 = gen_int_mode (abs_d, compute_mode);
3736 if (d == 1)
3737 quotient = op0;
3738 else if (d == -1)
3739 quotient = expand_unop (compute_mode, neg_optab, op0,
3740 tquotient, 0);
3741 else if (abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
3743 /* This case is not handled correctly below. */
3744 quotient = emit_store_flag (tquotient, EQ, op0, op1,
3745 compute_mode, 1, 1);
3746 if (quotient == 0)
3747 goto fail1;
3749 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
3750 && (rem_flag ? smod_pow2_cheap[compute_mode]
3751 : sdiv_pow2_cheap[compute_mode])
3752 /* We assume that cheap metric is true if the
3753 optab has an expander for this mode. */
3754 && (((rem_flag ? smod_optab : sdiv_optab)
3755 ->handlers[compute_mode].insn_code
3756 != CODE_FOR_nothing)
3757 || (sdivmod_optab->handlers[compute_mode]
3758 .insn_code != CODE_FOR_nothing)))
3760 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
3762 if (rem_flag)
3764 remainder = expand_smod_pow2 (compute_mode, op0, d);
3765 if (remainder)
3766 return gen_lowpart (mode, remainder);
3769 if (sdiv_pow2_cheap[compute_mode]
3770 && ((sdiv_optab->handlers[compute_mode].insn_code
3771 != CODE_FOR_nothing)
3772 || (sdivmod_optab->handlers[compute_mode].insn_code
3773 != CODE_FOR_nothing)))
3774 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
3775 compute_mode, op0,
3776 gen_int_mode (abs_d,
3777 compute_mode),
3778 NULL_RTX, 0);
3779 else
3780 quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
3782 /* We have computed OP0 / abs(OP1). If OP1 is negative,
3783 negate the quotient. */
3784 if (d < 0)
3786 insn = get_last_insn ();
3787 if (insn != last
3788 && (set = single_set (insn)) != 0
3789 && SET_DEST (set) == quotient
3790 && abs_d < ((unsigned HOST_WIDE_INT) 1
3791 << (HOST_BITS_PER_WIDE_INT - 1)))
3792 set_unique_reg_note (insn,
3793 REG_EQUAL,
3794 gen_rtx_DIV (compute_mode,
3795 op0,
3796 GEN_INT
3797 (trunc_int_for_mode
3798 (abs_d,
3799 compute_mode))));
3801 quotient = expand_unop (compute_mode, neg_optab,
3802 quotient, quotient, 0);
3805 else if (size <= HOST_BITS_PER_WIDE_INT)
3807 choose_multiplier (abs_d, size, size - 1,
3808 &ml, &post_shift, &lgup);
3809 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
3811 rtx t1, t2, t3;
3813 if (post_shift >= BITS_PER_WORD
3814 || size - 1 >= BITS_PER_WORD)
3815 goto fail1;
3817 extra_cost = (shift_cost[compute_mode][post_shift]
3818 + shift_cost[compute_mode][size - 1]
3819 + add_cost[compute_mode]);
3820 t1 = expand_mult_highpart (compute_mode, op0, ml,
3821 NULL_RTX, 0,
3822 max_cost - extra_cost);
3823 if (t1 == 0)
3824 goto fail1;
3825 t2 = expand_shift
3826 (RSHIFT_EXPR, compute_mode, t1,
3827 build_int_cst (NULL_TREE, post_shift),
3828 NULL_RTX, 0);
3829 t3 = expand_shift
3830 (RSHIFT_EXPR, compute_mode, op0,
3831 build_int_cst (NULL_TREE, size - 1),
3832 NULL_RTX, 0);
3833 if (d < 0)
3834 quotient
3835 = force_operand (gen_rtx_MINUS (compute_mode,
3836 t3, t2),
3837 tquotient);
3838 else
3839 quotient
3840 = force_operand (gen_rtx_MINUS (compute_mode,
3841 t2, t3),
3842 tquotient);
3844 else
3846 rtx t1, t2, t3, t4;
3848 if (post_shift >= BITS_PER_WORD
3849 || size - 1 >= BITS_PER_WORD)
3850 goto fail1;
3852 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
3853 extra_cost = (shift_cost[compute_mode][post_shift]
3854 + shift_cost[compute_mode][size - 1]
3855 + 2 * add_cost[compute_mode]);
3856 t1 = expand_mult_highpart (compute_mode, op0, ml,
3857 NULL_RTX, 0,
3858 max_cost - extra_cost);
3859 if (t1 == 0)
3860 goto fail1;
3861 t2 = force_operand (gen_rtx_PLUS (compute_mode,
3862 t1, op0),
3863 NULL_RTX);
3864 t3 = expand_shift
3865 (RSHIFT_EXPR, compute_mode, t2,
3866 build_int_cst (NULL_TREE, post_shift),
3867 NULL_RTX, 0);
3868 t4 = expand_shift
3869 (RSHIFT_EXPR, compute_mode, op0,
3870 build_int_cst (NULL_TREE, size - 1),
3871 NULL_RTX, 0);
3872 if (d < 0)
3873 quotient
3874 = force_operand (gen_rtx_MINUS (compute_mode,
3875 t4, t3),
3876 tquotient);
3877 else
3878 quotient
3879 = force_operand (gen_rtx_MINUS (compute_mode,
3880 t3, t4),
3881 tquotient);
3884 else /* Too wide mode to use tricky code */
3885 break;
3887 insn = get_last_insn ();
3888 if (insn != last
3889 && (set = single_set (insn)) != 0
3890 && SET_DEST (set) == quotient)
3891 set_unique_reg_note (insn,
3892 REG_EQUAL,
3893 gen_rtx_DIV (compute_mode, op0, op1));
3895 break;
3897 fail1:
3898 delete_insns_since (last);
3899 break;
3901 case FLOOR_DIV_EXPR:
3902 case FLOOR_MOD_EXPR:
3903 /* We will come here only for signed operations. */
3904 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
3906 unsigned HOST_WIDE_INT mh, ml;
3907 int pre_shift, lgup, post_shift;
3908 HOST_WIDE_INT d = INTVAL (op1);
3910 if (d > 0)
3912 /* We could just as easily deal with negative constants here,
3913 but it does not seem worth the trouble for GCC 2.6. */
3914 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3916 pre_shift = floor_log2 (d);
3917 if (rem_flag)
3919 remainder = expand_binop (compute_mode, and_optab, op0,
3920 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3921 remainder, 0, OPTAB_LIB_WIDEN);
3922 if (remainder)
3923 return gen_lowpart (mode, remainder);
3925 quotient = expand_shift
3926 (RSHIFT_EXPR, compute_mode, op0,
3927 build_int_cst (NULL_TREE, pre_shift),
3928 tquotient, 0);
3930 else
3932 rtx t1, t2, t3, t4;
3934 mh = choose_multiplier (d, size, size - 1,
3935 &ml, &post_shift, &lgup);
3936 gcc_assert (!mh);
3938 if (post_shift < BITS_PER_WORD
3939 && size - 1 < BITS_PER_WORD)
3941 t1 = expand_shift
3942 (RSHIFT_EXPR, compute_mode, op0,
3943 build_int_cst (NULL_TREE, size - 1),
3944 NULL_RTX, 0);
3945 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
3946 NULL_RTX, 0, OPTAB_WIDEN);
3947 extra_cost = (shift_cost[compute_mode][post_shift]
3948 + shift_cost[compute_mode][size - 1]
3949 + 2 * add_cost[compute_mode]);
3950 t3 = expand_mult_highpart (compute_mode, t2, ml,
3951 NULL_RTX, 1,
3952 max_cost - extra_cost);
3953 if (t3 != 0)
3955 t4 = expand_shift
3956 (RSHIFT_EXPR, compute_mode, t3,
3957 build_int_cst (NULL_TREE, post_shift),
3958 NULL_RTX, 1);
3959 quotient = expand_binop (compute_mode, xor_optab,
3960 t4, t1, tquotient, 0,
3961 OPTAB_WIDEN);
3966 else
3968 rtx nsign, t1, t2, t3, t4;
3969 t1 = force_operand (gen_rtx_PLUS (compute_mode,
3970 op0, constm1_rtx), NULL_RTX);
3971 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
3972 0, OPTAB_WIDEN);
3973 nsign = expand_shift
3974 (RSHIFT_EXPR, compute_mode, t2,
3975 build_int_cst (NULL_TREE, size - 1),
3976 NULL_RTX, 0);
3977 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
3978 NULL_RTX);
3979 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
3980 NULL_RTX, 0);
3981 if (t4)
3983 rtx t5;
3984 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
3985 NULL_RTX, 0);
3986 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3987 t4, t5),
3988 tquotient);
3993 if (quotient != 0)
3994 break;
3995 delete_insns_since (last);
3997 /* Try using an instruction that produces both the quotient and
3998 remainder, using truncation. We can easily compensate the quotient
3999 or remainder to get floor rounding, once we have the remainder.
4000 Notice that we compute also the final remainder value here,
4001 and return the result right away. */
4002 if (target == 0 || GET_MODE (target) != compute_mode)
4003 target = gen_reg_rtx (compute_mode);
4005 if (rem_flag)
4007 remainder
4008 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4009 quotient = gen_reg_rtx (compute_mode);
4011 else
4013 quotient
4014 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4015 remainder = gen_reg_rtx (compute_mode);
4018 if (expand_twoval_binop (sdivmod_optab, op0, op1,
4019 quotient, remainder, 0))
4021 /* This could be computed with a branch-less sequence.
4022 Save that for later. */
4023 rtx tem;
4024 rtx label = gen_label_rtx ();
4025 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4026 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4027 NULL_RTX, 0, OPTAB_WIDEN);
4028 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4029 expand_dec (quotient, const1_rtx);
4030 expand_inc (remainder, op1);
4031 emit_label (label);
4032 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4035 /* No luck with division elimination or divmod. Have to do it
4036 by conditionally adjusting op0 *and* the result. */
4038 rtx label1, label2, label3, label4, label5;
4039 rtx adjusted_op0;
4040 rtx tem;
4042 quotient = gen_reg_rtx (compute_mode);
4043 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4044 label1 = gen_label_rtx ();
4045 label2 = gen_label_rtx ();
4046 label3 = gen_label_rtx ();
4047 label4 = gen_label_rtx ();
4048 label5 = gen_label_rtx ();
4049 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4050 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4051 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4052 quotient, 0, OPTAB_LIB_WIDEN);
4053 if (tem != quotient)
4054 emit_move_insn (quotient, tem);
4055 emit_jump_insn (gen_jump (label5));
4056 emit_barrier ();
4057 emit_label (label1);
4058 expand_inc (adjusted_op0, const1_rtx);
4059 emit_jump_insn (gen_jump (label4));
4060 emit_barrier ();
4061 emit_label (label2);
4062 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4063 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4064 quotient, 0, OPTAB_LIB_WIDEN);
4065 if (tem != quotient)
4066 emit_move_insn (quotient, tem);
4067 emit_jump_insn (gen_jump (label5));
4068 emit_barrier ();
4069 emit_label (label3);
4070 expand_dec (adjusted_op0, const1_rtx);
4071 emit_label (label4);
4072 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4073 quotient, 0, OPTAB_LIB_WIDEN);
4074 if (tem != quotient)
4075 emit_move_insn (quotient, tem);
4076 expand_dec (quotient, const1_rtx);
4077 emit_label (label5);
4079 break;
4081 case CEIL_DIV_EXPR:
4082 case CEIL_MOD_EXPR:
4083 if (unsignedp)
4085 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
4087 rtx t1, t2, t3;
4088 unsigned HOST_WIDE_INT d = INTVAL (op1);
4089 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4090 build_int_cst (NULL_TREE, floor_log2 (d)),
4091 tquotient, 1);
4092 t2 = expand_binop (compute_mode, and_optab, op0,
4093 GEN_INT (d - 1),
4094 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4095 t3 = gen_reg_rtx (compute_mode);
4096 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4097 compute_mode, 1, 1);
4098 if (t3 == 0)
4100 rtx lab;
4101 lab = gen_label_rtx ();
4102 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4103 expand_inc (t1, const1_rtx);
4104 emit_label (lab);
4105 quotient = t1;
4107 else
4108 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4109 t1, t3),
4110 tquotient);
4111 break;
4114 /* Try using an instruction that produces both the quotient and
4115 remainder, using truncation. We can easily compensate the
4116 quotient or remainder to get ceiling rounding, once we have the
4117 remainder. Notice that we compute also the final remainder
4118 value here, and return the result right away. */
4119 if (target == 0 || GET_MODE (target) != compute_mode)
4120 target = gen_reg_rtx (compute_mode);
4122 if (rem_flag)
4124 remainder = (REG_P (target)
4125 ? target : gen_reg_rtx (compute_mode));
4126 quotient = gen_reg_rtx (compute_mode);
4128 else
4130 quotient = (REG_P (target)
4131 ? target : gen_reg_rtx (compute_mode));
4132 remainder = gen_reg_rtx (compute_mode);
4135 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4136 remainder, 1))
4138 /* This could be computed with a branch-less sequence.
4139 Save that for later. */
4140 rtx label = gen_label_rtx ();
4141 do_cmp_and_jump (remainder, const0_rtx, EQ,
4142 compute_mode, label);
4143 expand_inc (quotient, const1_rtx);
4144 expand_dec (remainder, op1);
4145 emit_label (label);
4146 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4149 /* No luck with division elimination or divmod. Have to do it
4150 by conditionally adjusting op0 *and* the result. */
4152 rtx label1, label2;
4153 rtx adjusted_op0, tem;
4155 quotient = gen_reg_rtx (compute_mode);
4156 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4157 label1 = gen_label_rtx ();
4158 label2 = gen_label_rtx ();
4159 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4160 compute_mode, label1);
4161 emit_move_insn (quotient, const0_rtx);
4162 emit_jump_insn (gen_jump (label2));
4163 emit_barrier ();
4164 emit_label (label1);
4165 expand_dec (adjusted_op0, const1_rtx);
4166 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4167 quotient, 1, OPTAB_LIB_WIDEN);
4168 if (tem != quotient)
4169 emit_move_insn (quotient, tem);
4170 expand_inc (quotient, const1_rtx);
4171 emit_label (label2);
4174 else /* signed */
4176 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4177 && INTVAL (op1) >= 0)
4179 /* This is extremely similar to the code for the unsigned case
4180 above. For 2.7 we should merge these variants, but for
4181 2.6.1 I don't want to touch the code for unsigned since that
4182 get used in C. The signed case will only be used by other
4183 languages (Ada). */
4185 rtx t1, t2, t3;
4186 unsigned HOST_WIDE_INT d = INTVAL (op1);
4187 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4188 build_int_cst (NULL_TREE, floor_log2 (d)),
4189 tquotient, 0);
4190 t2 = expand_binop (compute_mode, and_optab, op0,
4191 GEN_INT (d - 1),
4192 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4193 t3 = gen_reg_rtx (compute_mode);
4194 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4195 compute_mode, 1, 1);
4196 if (t3 == 0)
4198 rtx lab;
4199 lab = gen_label_rtx ();
4200 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4201 expand_inc (t1, const1_rtx);
4202 emit_label (lab);
4203 quotient = t1;
4205 else
4206 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4207 t1, t3),
4208 tquotient);
4209 break;
4212 /* Try using an instruction that produces both the quotient and
4213 remainder, using truncation. We can easily compensate the
4214 quotient or remainder to get ceiling rounding, once we have the
4215 remainder. Notice that we compute also the final remainder
4216 value here, and return the result right away. */
4217 if (target == 0 || GET_MODE (target) != compute_mode)
4218 target = gen_reg_rtx (compute_mode);
4219 if (rem_flag)
4221 remainder= (REG_P (target)
4222 ? target : gen_reg_rtx (compute_mode));
4223 quotient = gen_reg_rtx (compute_mode);
4225 else
4227 quotient = (REG_P (target)
4228 ? target : gen_reg_rtx (compute_mode));
4229 remainder = gen_reg_rtx (compute_mode);
4232 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4233 remainder, 0))
4235 /* This could be computed with a branch-less sequence.
4236 Save that for later. */
4237 rtx tem;
4238 rtx label = gen_label_rtx ();
4239 do_cmp_and_jump (remainder, const0_rtx, EQ,
4240 compute_mode, label);
4241 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4242 NULL_RTX, 0, OPTAB_WIDEN);
4243 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4244 expand_inc (quotient, const1_rtx);
4245 expand_dec (remainder, op1);
4246 emit_label (label);
4247 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4250 /* No luck with division elimination or divmod. Have to do it
4251 by conditionally adjusting op0 *and* the result. */
4253 rtx label1, label2, label3, label4, label5;
4254 rtx adjusted_op0;
4255 rtx tem;
4257 quotient = gen_reg_rtx (compute_mode);
4258 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4259 label1 = gen_label_rtx ();
4260 label2 = gen_label_rtx ();
4261 label3 = gen_label_rtx ();
4262 label4 = gen_label_rtx ();
4263 label5 = gen_label_rtx ();
4264 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4265 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4266 compute_mode, label1);
4267 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4268 quotient, 0, OPTAB_LIB_WIDEN);
4269 if (tem != quotient)
4270 emit_move_insn (quotient, tem);
4271 emit_jump_insn (gen_jump (label5));
4272 emit_barrier ();
4273 emit_label (label1);
4274 expand_dec (adjusted_op0, const1_rtx);
4275 emit_jump_insn (gen_jump (label4));
4276 emit_barrier ();
4277 emit_label (label2);
4278 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4279 compute_mode, label3);
4280 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4281 quotient, 0, OPTAB_LIB_WIDEN);
4282 if (tem != quotient)
4283 emit_move_insn (quotient, tem);
4284 emit_jump_insn (gen_jump (label5));
4285 emit_barrier ();
4286 emit_label (label3);
4287 expand_inc (adjusted_op0, const1_rtx);
4288 emit_label (label4);
4289 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4290 quotient, 0, OPTAB_LIB_WIDEN);
4291 if (tem != quotient)
4292 emit_move_insn (quotient, tem);
4293 expand_inc (quotient, const1_rtx);
4294 emit_label (label5);
4297 break;
4299 case EXACT_DIV_EXPR:
4300 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4302 HOST_WIDE_INT d = INTVAL (op1);
4303 unsigned HOST_WIDE_INT ml;
4304 int pre_shift;
4305 rtx t1;
4307 pre_shift = floor_log2 (d & -d);
4308 ml = invert_mod2n (d >> pre_shift, size);
4309 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4310 build_int_cst (NULL_TREE, pre_shift),
4311 NULL_RTX, unsignedp);
4312 quotient = expand_mult (compute_mode, t1,
4313 gen_int_mode (ml, compute_mode),
4314 NULL_RTX, 1);
4316 insn = get_last_insn ();
4317 set_unique_reg_note (insn,
4318 REG_EQUAL,
4319 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4320 compute_mode,
4321 op0, op1));
4323 break;
4325 case ROUND_DIV_EXPR:
4326 case ROUND_MOD_EXPR:
4327 if (unsignedp)
4329 rtx tem;
4330 rtx label;
4331 label = gen_label_rtx ();
4332 quotient = gen_reg_rtx (compute_mode);
4333 remainder = gen_reg_rtx (compute_mode);
4334 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4336 rtx tem;
4337 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
4338 quotient, 1, OPTAB_LIB_WIDEN);
4339 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
4340 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4341 remainder, 1, OPTAB_LIB_WIDEN);
4343 tem = plus_constant (op1, -1);
4344 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4345 build_int_cst (NULL_TREE, 1),
4346 NULL_RTX, 1);
4347 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
4348 expand_inc (quotient, const1_rtx);
4349 expand_dec (remainder, op1);
4350 emit_label (label);
4352 else
4354 rtx abs_rem, abs_op1, tem, mask;
4355 rtx label;
4356 label = gen_label_rtx ();
4357 quotient = gen_reg_rtx (compute_mode);
4358 remainder = gen_reg_rtx (compute_mode);
4359 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
4361 rtx tem;
4362 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
4363 quotient, 0, OPTAB_LIB_WIDEN);
4364 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
4365 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4366 remainder, 0, OPTAB_LIB_WIDEN);
4368 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
4369 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
4370 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
4371 build_int_cst (NULL_TREE, 1),
4372 NULL_RTX, 1);
4373 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
4374 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4375 NULL_RTX, 0, OPTAB_WIDEN);
4376 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4377 build_int_cst (NULL_TREE, size - 1),
4378 NULL_RTX, 0);
4379 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
4380 NULL_RTX, 0, OPTAB_WIDEN);
4381 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4382 NULL_RTX, 0, OPTAB_WIDEN);
4383 expand_inc (quotient, tem);
4384 tem = expand_binop (compute_mode, xor_optab, mask, op1,
4385 NULL_RTX, 0, OPTAB_WIDEN);
4386 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4387 NULL_RTX, 0, OPTAB_WIDEN);
4388 expand_dec (remainder, tem);
4389 emit_label (label);
4391 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4393 default:
4394 gcc_unreachable ();
4397 if (quotient == 0)
4399 if (target && GET_MODE (target) != compute_mode)
4400 target = 0;
4402 if (rem_flag)
4404 /* Try to produce the remainder without producing the quotient.
4405 If we seem to have a divmod pattern that does not require widening,
4406 don't try widening here. We should really have a WIDEN argument
4407 to expand_twoval_binop, since what we'd really like to do here is
4408 1) try a mod insn in compute_mode
4409 2) try a divmod insn in compute_mode
4410 3) try a div insn in compute_mode and multiply-subtract to get
4411 remainder
4412 4) try the same things with widening allowed. */
4413 remainder
4414 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4415 op0, op1, target,
4416 unsignedp,
4417 ((optab2->handlers[compute_mode].insn_code
4418 != CODE_FOR_nothing)
4419 ? OPTAB_DIRECT : OPTAB_WIDEN));
4420 if (remainder == 0)
4422 /* No luck there. Can we do remainder and divide at once
4423 without a library call? */
4424 remainder = gen_reg_rtx (compute_mode);
4425 if (! expand_twoval_binop ((unsignedp
4426 ? udivmod_optab
4427 : sdivmod_optab),
4428 op0, op1,
4429 NULL_RTX, remainder, unsignedp))
4430 remainder = 0;
4433 if (remainder)
4434 return gen_lowpart (mode, remainder);
4437 /* Produce the quotient. Try a quotient insn, but not a library call.
4438 If we have a divmod in this mode, use it in preference to widening
4439 the div (for this test we assume it will not fail). Note that optab2
4440 is set to the one of the two optabs that the call below will use. */
4441 quotient
4442 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
4443 op0, op1, rem_flag ? NULL_RTX : target,
4444 unsignedp,
4445 ((optab2->handlers[compute_mode].insn_code
4446 != CODE_FOR_nothing)
4447 ? OPTAB_DIRECT : OPTAB_WIDEN));
4449 if (quotient == 0)
4451 /* No luck there. Try a quotient-and-remainder insn,
4452 keeping the quotient alone. */
4453 quotient = gen_reg_rtx (compute_mode);
4454 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
4455 op0, op1,
4456 quotient, NULL_RTX, unsignedp))
4458 quotient = 0;
4459 if (! rem_flag)
4460 /* Still no luck. If we are not computing the remainder,
4461 use a library call for the quotient. */
4462 quotient = sign_expand_binop (compute_mode,
4463 udiv_optab, sdiv_optab,
4464 op0, op1, target,
4465 unsignedp, OPTAB_LIB_WIDEN);
4470 if (rem_flag)
4472 if (target && GET_MODE (target) != compute_mode)
4473 target = 0;
4475 if (quotient == 0)
4477 /* No divide instruction either. Use library for remainder. */
4478 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4479 op0, op1, target,
4480 unsignedp, OPTAB_LIB_WIDEN);
4481 /* No remainder function. Try a quotient-and-remainder
4482 function, keeping the remainder. */
4483 if (!remainder)
4485 remainder = gen_reg_rtx (compute_mode);
4486 if (!expand_twoval_binop_libfunc
4487 (unsignedp ? udivmod_optab : sdivmod_optab,
4488 op0, op1,
4489 NULL_RTX, remainder,
4490 unsignedp ? UMOD : MOD))
4491 remainder = NULL_RTX;
4494 else
4496 /* We divided. Now finish doing X - Y * (X / Y). */
4497 remainder = expand_mult (compute_mode, quotient, op1,
4498 NULL_RTX, unsignedp);
4499 remainder = expand_binop (compute_mode, sub_optab, op0,
4500 remainder, target, unsignedp,
4501 OPTAB_LIB_WIDEN);
4505 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4508 /* Return a tree node with data type TYPE, describing the value of X.
4509 Usually this is an VAR_DECL, if there is no obvious better choice.
4510 X may be an expression, however we only support those expressions
4511 generated by loop.c. */
4513 tree
4514 make_tree (tree type, rtx x)
4516 tree t;
4518 switch (GET_CODE (x))
4520 case CONST_INT:
4522 HOST_WIDE_INT hi = 0;
4524 if (INTVAL (x) < 0
4525 && !(TYPE_UNSIGNED (type)
4526 && (GET_MODE_BITSIZE (TYPE_MODE (type))
4527 < HOST_BITS_PER_WIDE_INT)))
4528 hi = -1;
4530 t = build_int_cst_wide (type, INTVAL (x), hi);
4532 return t;
4535 case CONST_DOUBLE:
4536 if (GET_MODE (x) == VOIDmode)
4537 t = build_int_cst_wide (type,
4538 CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
4539 else
4541 REAL_VALUE_TYPE d;
4543 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
4544 t = build_real (type, d);
4547 return t;
4549 case CONST_VECTOR:
4551 int i, units;
4552 rtx elt;
4553 tree t = NULL_TREE;
4555 units = CONST_VECTOR_NUNITS (x);
4557 /* Build a tree with vector elements. */
4558 for (i = units - 1; i >= 0; --i)
4560 elt = CONST_VECTOR_ELT (x, i);
4561 t = tree_cons (NULL_TREE, make_tree (type, elt), t);
4564 return build_vector (type, t);
4567 case PLUS:
4568 return fold (build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4569 make_tree (type, XEXP (x, 1))));
4571 case MINUS:
4572 return fold (build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4573 make_tree (type, XEXP (x, 1))));
4575 case NEG:
4576 return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
4578 case MULT:
4579 return fold (build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
4580 make_tree (type, XEXP (x, 1))));
4582 case ASHIFT:
4583 return fold (build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
4584 make_tree (type, XEXP (x, 1))));
4586 case LSHIFTRT:
4587 t = lang_hooks.types.unsigned_type (type);
4588 return fold (convert (type,
4589 build2 (RSHIFT_EXPR, t,
4590 make_tree (t, XEXP (x, 0)),
4591 make_tree (type, XEXP (x, 1)))));
4593 case ASHIFTRT:
4594 t = lang_hooks.types.signed_type (type);
4595 return fold (convert (type,
4596 build2 (RSHIFT_EXPR, t,
4597 make_tree (t, XEXP (x, 0)),
4598 make_tree (type, XEXP (x, 1)))));
4600 case DIV:
4601 if (TREE_CODE (type) != REAL_TYPE)
4602 t = lang_hooks.types.signed_type (type);
4603 else
4604 t = type;
4606 return fold (convert (type,
4607 build2 (TRUNC_DIV_EXPR, t,
4608 make_tree (t, XEXP (x, 0)),
4609 make_tree (t, XEXP (x, 1)))));
4610 case UDIV:
4611 t = lang_hooks.types.unsigned_type (type);
4612 return fold (convert (type,
4613 build2 (TRUNC_DIV_EXPR, t,
4614 make_tree (t, XEXP (x, 0)),
4615 make_tree (t, XEXP (x, 1)))));
4617 case SIGN_EXTEND:
4618 case ZERO_EXTEND:
4619 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
4620 GET_CODE (x) == ZERO_EXTEND);
4621 return fold (convert (type, make_tree (t, XEXP (x, 0))));
4623 default:
4624 t = build_decl (VAR_DECL, NULL_TREE, type);
4626 /* If TYPE is a POINTER_TYPE, X might be Pmode with TYPE_MODE being
4627 ptr_mode. So convert. */
4628 if (POINTER_TYPE_P (type))
4629 x = convert_memory_address (TYPE_MODE (type), x);
4631 /* Note that we do *not* use SET_DECL_RTL here, because we do not
4632 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
4633 t->decl.rtl = x;
4635 return t;
4639 /* Check whether the multiplication X * MULT + ADD overflows.
4640 X, MULT and ADD must be CONST_*.
4641 MODE is the machine mode for the computation.
4642 X and MULT must have mode MODE. ADD may have a different mode.
4643 So can X (defaults to same as MODE).
4644 UNSIGNEDP is nonzero to do unsigned multiplication. */
4646 bool
4647 const_mult_add_overflow_p (rtx x, rtx mult, rtx add,
4648 enum machine_mode mode, int unsignedp)
4650 tree type, mult_type, add_type, result;
4652 type = lang_hooks.types.type_for_mode (mode, unsignedp);
4654 /* In order to get a proper overflow indication from an unsigned
4655 type, we have to pretend that it's a sizetype. */
4656 mult_type = type;
4657 if (unsignedp)
4659 /* FIXME:It would be nice if we could step directly from this
4660 type to its sizetype equivalent. */
4661 mult_type = build_distinct_type_copy (type);
4662 TYPE_IS_SIZETYPE (mult_type) = 1;
4665 add_type = (GET_MODE (add) == VOIDmode ? mult_type
4666 : lang_hooks.types.type_for_mode (GET_MODE (add), unsignedp));
4668 result = fold (build2 (PLUS_EXPR, mult_type,
4669 fold (build2 (MULT_EXPR, mult_type,
4670 make_tree (mult_type, x),
4671 make_tree (mult_type, mult))),
4672 make_tree (add_type, add)));
4674 return TREE_CONSTANT_OVERFLOW (result);
4677 /* Return an rtx representing the value of X * MULT + ADD.
4678 TARGET is a suggestion for where to store the result (an rtx).
4679 MODE is the machine mode for the computation.
4680 X and MULT must have mode MODE. ADD may have a different mode.
4681 So can X (defaults to same as MODE).
4682 UNSIGNEDP is nonzero to do unsigned multiplication.
4683 This may emit insns. */
4686 expand_mult_add (rtx x, rtx target, rtx mult, rtx add, enum machine_mode mode,
4687 int unsignedp)
4689 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
4690 tree add_type = (GET_MODE (add) == VOIDmode
4691 ? type: lang_hooks.types.type_for_mode (GET_MODE (add),
4692 unsignedp));
4693 tree result = fold (build2 (PLUS_EXPR, type,
4694 fold (build2 (MULT_EXPR, type,
4695 make_tree (type, x),
4696 make_tree (type, mult))),
4697 make_tree (add_type, add)));
4699 return expand_expr (result, target, VOIDmode, 0);
4702 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
4703 and returning TARGET.
4705 If TARGET is 0, a pseudo-register or constant is returned. */
4708 expand_and (enum machine_mode mode, rtx op0, rtx op1, rtx target)
4710 rtx tem = 0;
4712 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
4713 tem = simplify_binary_operation (AND, mode, op0, op1);
4714 if (tem == 0)
4715 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
4717 if (target == 0)
4718 target = tem;
4719 else if (tem != target)
4720 emit_move_insn (target, tem);
4721 return target;
4724 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
4725 and storing in TARGET. Normally return TARGET.
4726 Return 0 if that cannot be done.
4728 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
4729 it is VOIDmode, they cannot both be CONST_INT.
4731 UNSIGNEDP is for the case where we have to widen the operands
4732 to perform the operation. It says to use zero-extension.
4734 NORMALIZEP is 1 if we should convert the result to be either zero
4735 or one. Normalize is -1 if we should convert the result to be
4736 either zero or -1. If NORMALIZEP is zero, the result will be left
4737 "raw" out of the scc insn. */
4740 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
4741 enum machine_mode mode, int unsignedp, int normalizep)
4743 rtx subtarget;
4744 enum insn_code icode;
4745 enum machine_mode compare_mode;
4746 enum machine_mode target_mode = GET_MODE (target);
4747 rtx tem;
4748 rtx last = get_last_insn ();
4749 rtx pattern, comparison;
4751 if (unsignedp)
4752 code = unsigned_condition (code);
4754 /* If one operand is constant, make it the second one. Only do this
4755 if the other operand is not constant as well. */
4757 if (swap_commutative_operands_p (op0, op1))
4759 tem = op0;
4760 op0 = op1;
4761 op1 = tem;
4762 code = swap_condition (code);
4765 if (mode == VOIDmode)
4766 mode = GET_MODE (op0);
4768 /* For some comparisons with 1 and -1, we can convert this to
4769 comparisons with zero. This will often produce more opportunities for
4770 store-flag insns. */
4772 switch (code)
4774 case LT:
4775 if (op1 == const1_rtx)
4776 op1 = const0_rtx, code = LE;
4777 break;
4778 case LE:
4779 if (op1 == constm1_rtx)
4780 op1 = const0_rtx, code = LT;
4781 break;
4782 case GE:
4783 if (op1 == const1_rtx)
4784 op1 = const0_rtx, code = GT;
4785 break;
4786 case GT:
4787 if (op1 == constm1_rtx)
4788 op1 = const0_rtx, code = GE;
4789 break;
4790 case GEU:
4791 if (op1 == const1_rtx)
4792 op1 = const0_rtx, code = NE;
4793 break;
4794 case LTU:
4795 if (op1 == const1_rtx)
4796 op1 = const0_rtx, code = EQ;
4797 break;
4798 default:
4799 break;
4802 /* If we are comparing a double-word integer with zero or -1, we can
4803 convert the comparison into one involving a single word. */
4804 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
4805 && GET_MODE_CLASS (mode) == MODE_INT
4806 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
4808 if ((code == EQ || code == NE)
4809 && (op1 == const0_rtx || op1 == constm1_rtx))
4811 rtx op00, op01, op0both;
4813 /* Do a logical OR or AND of the two words and compare the result. */
4814 op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
4815 op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
4816 op0both = expand_binop (word_mode,
4817 op1 == const0_rtx ? ior_optab : and_optab,
4818 op00, op01, NULL_RTX, unsignedp, OPTAB_DIRECT);
4820 if (op0both != 0)
4821 return emit_store_flag (target, code, op0both, op1, word_mode,
4822 unsignedp, normalizep);
4824 else if ((code == LT || code == GE) && op1 == const0_rtx)
4826 rtx op0h;
4828 /* If testing the sign bit, can just test on high word. */
4829 op0h = simplify_gen_subreg (word_mode, op0, mode,
4830 subreg_highpart_offset (word_mode, mode));
4831 return emit_store_flag (target, code, op0h, op1, word_mode,
4832 unsignedp, normalizep);
4836 /* From now on, we won't change CODE, so set ICODE now. */
4837 icode = setcc_gen_code[(int) code];
4839 /* If this is A < 0 or A >= 0, we can do this by taking the ones
4840 complement of A (for GE) and shifting the sign bit to the low bit. */
4841 if (op1 == const0_rtx && (code == LT || code == GE)
4842 && GET_MODE_CLASS (mode) == MODE_INT
4843 && (normalizep || STORE_FLAG_VALUE == 1
4844 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4845 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4846 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
4848 subtarget = target;
4850 /* If the result is to be wider than OP0, it is best to convert it
4851 first. If it is to be narrower, it is *incorrect* to convert it
4852 first. */
4853 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
4855 op0 = convert_modes (target_mode, mode, op0, 0);
4856 mode = target_mode;
4859 if (target_mode != mode)
4860 subtarget = 0;
4862 if (code == GE)
4863 op0 = expand_unop (mode, one_cmpl_optab, op0,
4864 ((STORE_FLAG_VALUE == 1 || normalizep)
4865 ? 0 : subtarget), 0);
4867 if (STORE_FLAG_VALUE == 1 || normalizep)
4868 /* If we are supposed to produce a 0/1 value, we want to do
4869 a logical shift from the sign bit to the low-order bit; for
4870 a -1/0 value, we do an arithmetic shift. */
4871 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
4872 size_int (GET_MODE_BITSIZE (mode) - 1),
4873 subtarget, normalizep != -1);
4875 if (mode != target_mode)
4876 op0 = convert_modes (target_mode, mode, op0, 0);
4878 return op0;
4881 if (icode != CODE_FOR_nothing)
4883 insn_operand_predicate_fn pred;
4885 /* We think we may be able to do this with a scc insn. Emit the
4886 comparison and then the scc insn. */
4888 do_pending_stack_adjust ();
4889 last = get_last_insn ();
4891 comparison
4892 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX);
4893 if (CONSTANT_P (comparison))
4895 switch (GET_CODE (comparison))
4897 case CONST_INT:
4898 if (comparison == const0_rtx)
4899 return const0_rtx;
4900 break;
4902 #ifdef FLOAT_STORE_FLAG_VALUE
4903 case CONST_DOUBLE:
4904 if (comparison == CONST0_RTX (GET_MODE (comparison)))
4905 return const0_rtx;
4906 break;
4907 #endif
4908 default:
4909 gcc_unreachable ();
4912 if (normalizep == 1)
4913 return const1_rtx;
4914 if (normalizep == -1)
4915 return constm1_rtx;
4916 return const_true_rtx;
4919 /* The code of COMPARISON may not match CODE if compare_from_rtx
4920 decided to swap its operands and reverse the original code.
4922 We know that compare_from_rtx returns either a CONST_INT or
4923 a new comparison code, so it is safe to just extract the
4924 code from COMPARISON. */
4925 code = GET_CODE (comparison);
4927 /* Get a reference to the target in the proper mode for this insn. */
4928 compare_mode = insn_data[(int) icode].operand[0].mode;
4929 subtarget = target;
4930 pred = insn_data[(int) icode].operand[0].predicate;
4931 if (optimize || ! (*pred) (subtarget, compare_mode))
4932 subtarget = gen_reg_rtx (compare_mode);
4934 pattern = GEN_FCN (icode) (subtarget);
4935 if (pattern)
4937 emit_insn (pattern);
4939 /* If we are converting to a wider mode, first convert to
4940 TARGET_MODE, then normalize. This produces better combining
4941 opportunities on machines that have a SIGN_EXTRACT when we are
4942 testing a single bit. This mostly benefits the 68k.
4944 If STORE_FLAG_VALUE does not have the sign bit set when
4945 interpreted in COMPARE_MODE, we can do this conversion as
4946 unsigned, which is usually more efficient. */
4947 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
4949 convert_move (target, subtarget,
4950 (GET_MODE_BITSIZE (compare_mode)
4951 <= HOST_BITS_PER_WIDE_INT)
4952 && 0 == (STORE_FLAG_VALUE
4953 & ((HOST_WIDE_INT) 1
4954 << (GET_MODE_BITSIZE (compare_mode) -1))));
4955 op0 = target;
4956 compare_mode = target_mode;
4958 else
4959 op0 = subtarget;
4961 /* If we want to keep subexpressions around, don't reuse our
4962 last target. */
4964 if (optimize)
4965 subtarget = 0;
4967 /* Now normalize to the proper value in COMPARE_MODE. Sometimes
4968 we don't have to do anything. */
4969 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
4971 /* STORE_FLAG_VALUE might be the most negative number, so write
4972 the comparison this way to avoid a compiler-time warning. */
4973 else if (- normalizep == STORE_FLAG_VALUE)
4974 op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
4976 /* We don't want to use STORE_FLAG_VALUE < 0 below since this
4977 makes it hard to use a value of just the sign bit due to
4978 ANSI integer constant typing rules. */
4979 else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
4980 && (STORE_FLAG_VALUE
4981 & ((HOST_WIDE_INT) 1
4982 << (GET_MODE_BITSIZE (compare_mode) - 1))))
4983 op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
4984 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
4985 subtarget, normalizep == 1);
4986 else
4988 gcc_assert (STORE_FLAG_VALUE & 1);
4990 op0 = expand_and (compare_mode, op0, const1_rtx, subtarget);
4991 if (normalizep == -1)
4992 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
4995 /* If we were converting to a smaller mode, do the
4996 conversion now. */
4997 if (target_mode != compare_mode)
4999 convert_move (target, op0, 0);
5000 return target;
5002 else
5003 return op0;
5007 delete_insns_since (last);
5009 /* If optimizing, use different pseudo registers for each insn, instead
5010 of reusing the same pseudo. This leads to better CSE, but slows
5011 down the compiler, since there are more pseudos */
5012 subtarget = (!optimize
5013 && (target_mode == mode)) ? target : NULL_RTX;
5015 /* If we reached here, we can't do this with a scc insn. However, there
5016 are some comparisons that can be done directly. For example, if
5017 this is an equality comparison of integers, we can try to exclusive-or
5018 (or subtract) the two operands and use a recursive call to try the
5019 comparison with zero. Don't do any of these cases if branches are
5020 very cheap. */
5022 if (BRANCH_COST > 0
5023 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
5024 && op1 != const0_rtx)
5026 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5027 OPTAB_WIDEN);
5029 if (tem == 0)
5030 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5031 OPTAB_WIDEN);
5032 if (tem != 0)
5033 tem = emit_store_flag (target, code, tem, const0_rtx,
5034 mode, unsignedp, normalizep);
5035 if (tem == 0)
5036 delete_insns_since (last);
5037 return tem;
5040 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5041 the constant zero. Reject all other comparisons at this point. Only
5042 do LE and GT if branches are expensive since they are expensive on
5043 2-operand machines. */
5045 if (BRANCH_COST == 0
5046 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
5047 || (code != EQ && code != NE
5048 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
5049 return 0;
5051 /* See what we need to return. We can only return a 1, -1, or the
5052 sign bit. */
5054 if (normalizep == 0)
5056 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5057 normalizep = STORE_FLAG_VALUE;
5059 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5060 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5061 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
5063 else
5064 return 0;
5067 /* Try to put the result of the comparison in the sign bit. Assume we can't
5068 do the necessary operation below. */
5070 tem = 0;
5072 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5073 the sign bit set. */
5075 if (code == LE)
5077 /* This is destructive, so SUBTARGET can't be OP0. */
5078 if (rtx_equal_p (subtarget, op0))
5079 subtarget = 0;
5081 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5082 OPTAB_WIDEN);
5083 if (tem)
5084 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5085 OPTAB_WIDEN);
5088 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5089 number of bits in the mode of OP0, minus one. */
5091 if (code == GT)
5093 if (rtx_equal_p (subtarget, op0))
5094 subtarget = 0;
5096 tem = expand_shift (RSHIFT_EXPR, mode, op0,
5097 size_int (GET_MODE_BITSIZE (mode) - 1),
5098 subtarget, 0);
5099 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5100 OPTAB_WIDEN);
5103 if (code == EQ || code == NE)
5105 /* For EQ or NE, one way to do the comparison is to apply an operation
5106 that converts the operand into a positive number if it is nonzero
5107 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5108 for NE we negate. This puts the result in the sign bit. Then we
5109 normalize with a shift, if needed.
5111 Two operations that can do the above actions are ABS and FFS, so try
5112 them. If that doesn't work, and MODE is smaller than a full word,
5113 we can use zero-extension to the wider mode (an unsigned conversion)
5114 as the operation. */
5116 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5117 that is compensated by the subsequent overflow when subtracting
5118 one / negating. */
5120 if (abs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5121 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5122 else if (ffs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5123 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5124 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5126 tem = convert_modes (word_mode, mode, op0, 1);
5127 mode = word_mode;
5130 if (tem != 0)
5132 if (code == EQ)
5133 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5134 0, OPTAB_WIDEN);
5135 else
5136 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5139 /* If we couldn't do it that way, for NE we can "or" the two's complement
5140 of the value with itself. For EQ, we take the one's complement of
5141 that "or", which is an extra insn, so we only handle EQ if branches
5142 are expensive. */
5144 if (tem == 0 && (code == NE || BRANCH_COST > 1))
5146 if (rtx_equal_p (subtarget, op0))
5147 subtarget = 0;
5149 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5150 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5151 OPTAB_WIDEN);
5153 if (tem && code == EQ)
5154 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5158 if (tem && normalizep)
5159 tem = expand_shift (RSHIFT_EXPR, mode, tem,
5160 size_int (GET_MODE_BITSIZE (mode) - 1),
5161 subtarget, normalizep == 1);
5163 if (tem)
5165 if (GET_MODE (tem) != target_mode)
5167 convert_move (target, tem, 0);
5168 tem = target;
5170 else if (!subtarget)
5172 emit_move_insn (target, tem);
5173 tem = target;
5176 else
5177 delete_insns_since (last);
5179 return tem;
5182 /* Like emit_store_flag, but always succeeds. */
5185 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5186 enum machine_mode mode, int unsignedp, int normalizep)
5188 rtx tem, label;
5190 /* First see if emit_store_flag can do the job. */
5191 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5192 if (tem != 0)
5193 return tem;
5195 if (normalizep == 0)
5196 normalizep = 1;
5198 /* If this failed, we have to do this with set/compare/jump/set code. */
5200 if (!REG_P (target)
5201 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5202 target = gen_reg_rtx (GET_MODE (target));
5204 emit_move_insn (target, const1_rtx);
5205 label = gen_label_rtx ();
5206 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
5207 NULL_RTX, label);
5209 emit_move_insn (target, const0_rtx);
5210 emit_label (label);
5212 return target;
5215 /* Perform possibly multi-word comparison and conditional jump to LABEL
5216 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE
5218 The algorithm is based on the code in expr.c:do_jump.
5220 Note that this does not perform a general comparison. Only variants
5221 generated within expmed.c are correctly handled, others abort (but could
5222 be handled if needed). */
5224 static void
5225 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, enum machine_mode mode,
5226 rtx label)
5228 /* If this mode is an integer too wide to compare properly,
5229 compare word by word. Rely on cse to optimize constant cases. */
5231 if (GET_MODE_CLASS (mode) == MODE_INT
5232 && ! can_compare_p (op, mode, ccp_jump))
5234 rtx label2 = gen_label_rtx ();
5236 switch (op)
5238 case LTU:
5239 do_jump_by_parts_greater_rtx (mode, 1, arg2, arg1, label2, label);
5240 break;
5242 case LEU:
5243 do_jump_by_parts_greater_rtx (mode, 1, arg1, arg2, label, label2);
5244 break;
5246 case LT:
5247 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label2, label);
5248 break;
5250 case GT:
5251 do_jump_by_parts_greater_rtx (mode, 0, arg1, arg2, label2, label);
5252 break;
5254 case GE:
5255 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label, label2);
5256 break;
5258 /* do_jump_by_parts_equality_rtx compares with zero. Luckily
5259 that's the only equality operations we do */
5260 case EQ:
5261 gcc_assert (arg2 == const0_rtx && mode == GET_MODE(arg1));
5262 do_jump_by_parts_equality_rtx (arg1, label2, label);
5263 break;
5265 case NE:
5266 gcc_assert (arg2 == const0_rtx && mode == GET_MODE(arg1));
5267 do_jump_by_parts_equality_rtx (arg1, label, label2);
5268 break;
5270 default:
5271 gcc_unreachable ();
5274 emit_label (label2);
5276 else
5277 emit_cmp_and_jump_insns (arg1, arg2, op, NULL_RTX, mode, 0, label);