The apostrophe was there to signal that the s was coming
[official-gcc.git] / gcc / expmed.c
blob98981b8f24739e9c4cd4a801b2b1ce349bdcd653
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 /* ??? emit_conditional_move forces a stack adjustment via
3309 compare_from_rtx so, if the sequence is discarded, it will
3310 be lost. Do it now instead. */
3311 do_pending_stack_adjust ();
3313 start_sequence ();
3314 temp2 = copy_to_mode_reg (mode, op0);
3315 temp = expand_binop (mode, add_optab, temp2, GEN_INT (d-1),
3316 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3317 temp = force_reg (mode, temp);
3319 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3320 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3321 mode, temp, temp2, mode, 0);
3322 if (temp2)
3324 rtx seq = get_insns ();
3325 end_sequence ();
3326 emit_insn (seq);
3327 return expand_shift (RSHIFT_EXPR, mode, temp2, shift, NULL_RTX, 0);
3329 end_sequence ();
3331 #endif
3333 if (BRANCH_COST >= 2)
3335 int ushift = GET_MODE_BITSIZE (mode) - logd;
3337 temp = gen_reg_rtx (mode);
3338 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3339 if (shift_cost[mode][ushift] > COSTS_N_INSNS (1))
3340 temp = expand_binop (mode, and_optab, temp, GEN_INT (d - 1),
3341 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3342 else
3343 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3344 build_int_cst (NULL_TREE, ushift),
3345 NULL_RTX, 1);
3346 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3347 0, OPTAB_LIB_WIDEN);
3348 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3351 label = gen_label_rtx ();
3352 temp = copy_to_mode_reg (mode, op0);
3353 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3354 expand_inc (temp, GEN_INT (d - 1));
3355 emit_label (label);
3356 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3359 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3360 if that is convenient, and returning where the result is.
3361 You may request either the quotient or the remainder as the result;
3362 specify REM_FLAG nonzero to get the remainder.
3364 CODE is the expression code for which kind of division this is;
3365 it controls how rounding is done. MODE is the machine mode to use.
3366 UNSIGNEDP nonzero means do unsigned division. */
3368 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3369 and then correct it by or'ing in missing high bits
3370 if result of ANDI is nonzero.
3371 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3372 This could optimize to a bfexts instruction.
3373 But C doesn't use these operations, so their optimizations are
3374 left for later. */
3375 /* ??? For modulo, we don't actually need the highpart of the first product,
3376 the low part will do nicely. And for small divisors, the second multiply
3377 can also be a low-part only multiply or even be completely left out.
3378 E.g. to calculate the remainder of a division by 3 with a 32 bit
3379 multiply, multiply with 0x55555556 and extract the upper two bits;
3380 the result is exact for inputs up to 0x1fffffff.
3381 The input range can be reduced by using cross-sum rules.
3382 For odd divisors >= 3, the following table gives right shift counts
3383 so that if a number is shifted by an integer multiple of the given
3384 amount, the remainder stays the same:
3385 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3386 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3387 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3388 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3389 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3391 Cross-sum rules for even numbers can be derived by leaving as many bits
3392 to the right alone as the divisor has zeros to the right.
3393 E.g. if x is an unsigned 32 bit number:
3394 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3397 #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
3400 expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
3401 rtx op0, rtx op1, rtx target, int unsignedp)
3403 enum machine_mode compute_mode;
3404 rtx tquotient;
3405 rtx quotient = 0, remainder = 0;
3406 rtx last;
3407 int size;
3408 rtx insn, set;
3409 optab optab1, optab2;
3410 int op1_is_constant, op1_is_pow2 = 0;
3411 int max_cost, extra_cost;
3412 static HOST_WIDE_INT last_div_const = 0;
3413 static HOST_WIDE_INT ext_op1;
3415 op1_is_constant = GET_CODE (op1) == CONST_INT;
3416 if (op1_is_constant)
3418 ext_op1 = INTVAL (op1);
3419 if (unsignedp)
3420 ext_op1 &= GET_MODE_MASK (mode);
3421 op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
3422 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
3426 This is the structure of expand_divmod:
3428 First comes code to fix up the operands so we can perform the operations
3429 correctly and efficiently.
3431 Second comes a switch statement with code specific for each rounding mode.
3432 For some special operands this code emits all RTL for the desired
3433 operation, for other cases, it generates only a quotient and stores it in
3434 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3435 to indicate that it has not done anything.
3437 Last comes code that finishes the operation. If QUOTIENT is set and
3438 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3439 QUOTIENT is not set, it is computed using trunc rounding.
3441 We try to generate special code for division and remainder when OP1 is a
3442 constant. If |OP1| = 2**n we can use shifts and some other fast
3443 operations. For other values of OP1, we compute a carefully selected
3444 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3445 by m.
3447 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3448 half of the product. Different strategies for generating the product are
3449 implemented in expand_mult_highpart.
3451 If what we actually want is the remainder, we generate that by another
3452 by-constant multiplication and a subtraction. */
3454 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3455 code below will malfunction if we are, so check here and handle
3456 the special case if so. */
3457 if (op1 == const1_rtx)
3458 return rem_flag ? const0_rtx : op0;
3460 /* When dividing by -1, we could get an overflow.
3461 negv_optab can handle overflows. */
3462 if (! unsignedp && op1 == constm1_rtx)
3464 if (rem_flag)
3465 return const0_rtx;
3466 return expand_unop (mode, flag_trapv && GET_MODE_CLASS(mode) == MODE_INT
3467 ? negv_optab : neg_optab, op0, target, 0);
3470 if (target
3471 /* Don't use the function value register as a target
3472 since we have to read it as well as write it,
3473 and function-inlining gets confused by this. */
3474 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3475 /* Don't clobber an operand while doing a multi-step calculation. */
3476 || ((rem_flag || op1_is_constant)
3477 && (reg_mentioned_p (target, op0)
3478 || (MEM_P (op0) && MEM_P (target))))
3479 || reg_mentioned_p (target, op1)
3480 || (MEM_P (op1) && MEM_P (target))))
3481 target = 0;
3483 /* Get the mode in which to perform this computation. Normally it will
3484 be MODE, but sometimes we can't do the desired operation in MODE.
3485 If so, pick a wider mode in which we can do the operation. Convert
3486 to that mode at the start to avoid repeated conversions.
3488 First see what operations we need. These depend on the expression
3489 we are evaluating. (We assume that divxx3 insns exist under the
3490 same conditions that modxx3 insns and that these insns don't normally
3491 fail. If these assumptions are not correct, we may generate less
3492 efficient code in some cases.)
3494 Then see if we find a mode in which we can open-code that operation
3495 (either a division, modulus, or shift). Finally, check for the smallest
3496 mode for which we can do the operation with a library call. */
3498 /* We might want to refine this now that we have division-by-constant
3499 optimization. Since expand_mult_highpart tries so many variants, it is
3500 not straightforward to generalize this. Maybe we should make an array
3501 of possible modes in init_expmed? Save this for GCC 2.7. */
3503 optab1 = ((op1_is_pow2 && op1 != const0_rtx)
3504 ? (unsignedp ? lshr_optab : ashr_optab)
3505 : (unsignedp ? udiv_optab : sdiv_optab));
3506 optab2 = ((op1_is_pow2 && op1 != const0_rtx)
3507 ? optab1
3508 : (unsignedp ? udivmod_optab : sdivmod_optab));
3510 for (compute_mode = mode; compute_mode != VOIDmode;
3511 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3512 if (optab1->handlers[compute_mode].insn_code != CODE_FOR_nothing
3513 || optab2->handlers[compute_mode].insn_code != CODE_FOR_nothing)
3514 break;
3516 if (compute_mode == VOIDmode)
3517 for (compute_mode = mode; compute_mode != VOIDmode;
3518 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3519 if (optab1->handlers[compute_mode].libfunc
3520 || optab2->handlers[compute_mode].libfunc)
3521 break;
3523 /* If we still couldn't find a mode, use MODE, but we'll probably abort
3524 in expand_binop. */
3525 if (compute_mode == VOIDmode)
3526 compute_mode = mode;
3528 if (target && GET_MODE (target) == compute_mode)
3529 tquotient = target;
3530 else
3531 tquotient = gen_reg_rtx (compute_mode);
3533 size = GET_MODE_BITSIZE (compute_mode);
3534 #if 0
3535 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3536 (mode), and thereby get better code when OP1 is a constant. Do that
3537 later. It will require going over all usages of SIZE below. */
3538 size = GET_MODE_BITSIZE (mode);
3539 #endif
3541 /* Only deduct something for a REM if the last divide done was
3542 for a different constant. Then set the constant of the last
3543 divide. */
3544 max_cost = div_cost[compute_mode]
3545 - (rem_flag && ! (last_div_const != 0 && op1_is_constant
3546 && INTVAL (op1) == last_div_const)
3547 ? mul_cost[compute_mode] + add_cost[compute_mode]
3548 : 0);
3550 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
3552 /* Now convert to the best mode to use. */
3553 if (compute_mode != mode)
3555 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
3556 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
3558 /* convert_modes may have placed op1 into a register, so we
3559 must recompute the following. */
3560 op1_is_constant = GET_CODE (op1) == CONST_INT;
3561 op1_is_pow2 = (op1_is_constant
3562 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3563 || (! unsignedp
3564 && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
3567 /* If one of the operands is a volatile MEM, copy it into a register. */
3569 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
3570 op0 = force_reg (compute_mode, op0);
3571 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
3572 op1 = force_reg (compute_mode, op1);
3574 /* If we need the remainder or if OP1 is constant, we need to
3575 put OP0 in a register in case it has any queued subexpressions. */
3576 if (rem_flag || op1_is_constant)
3577 op0 = force_reg (compute_mode, op0);
3579 last = get_last_insn ();
3581 /* Promote floor rounding to trunc rounding for unsigned operations. */
3582 if (unsignedp)
3584 if (code == FLOOR_DIV_EXPR)
3585 code = TRUNC_DIV_EXPR;
3586 if (code == FLOOR_MOD_EXPR)
3587 code = TRUNC_MOD_EXPR;
3588 if (code == EXACT_DIV_EXPR && op1_is_pow2)
3589 code = TRUNC_DIV_EXPR;
3592 if (op1 != const0_rtx)
3593 switch (code)
3595 case TRUNC_MOD_EXPR:
3596 case TRUNC_DIV_EXPR:
3597 if (op1_is_constant)
3599 if (unsignedp)
3601 unsigned HOST_WIDE_INT mh, ml;
3602 int pre_shift, post_shift;
3603 int dummy;
3604 unsigned HOST_WIDE_INT d = (INTVAL (op1)
3605 & GET_MODE_MASK (compute_mode));
3607 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3609 pre_shift = floor_log2 (d);
3610 if (rem_flag)
3612 remainder
3613 = expand_binop (compute_mode, and_optab, op0,
3614 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3615 remainder, 1,
3616 OPTAB_LIB_WIDEN);
3617 if (remainder)
3618 return gen_lowpart (mode, remainder);
3620 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3621 build_int_cst (NULL_TREE,
3622 pre_shift),
3623 tquotient, 1);
3625 else if (size <= HOST_BITS_PER_WIDE_INT)
3627 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
3629 /* Most significant bit of divisor is set; emit an scc
3630 insn. */
3631 quotient = emit_store_flag (tquotient, GEU, op0, op1,
3632 compute_mode, 1, 1);
3633 if (quotient == 0)
3634 goto fail1;
3636 else
3638 /* Find a suitable multiplier and right shift count
3639 instead of multiplying with D. */
3641 mh = choose_multiplier (d, size, size,
3642 &ml, &post_shift, &dummy);
3644 /* If the suggested multiplier is more than SIZE bits,
3645 we can do better for even divisors, using an
3646 initial right shift. */
3647 if (mh != 0 && (d & 1) == 0)
3649 pre_shift = floor_log2 (d & -d);
3650 mh = choose_multiplier (d >> pre_shift, size,
3651 size - pre_shift,
3652 &ml, &post_shift, &dummy);
3653 gcc_assert (!mh);
3655 else
3656 pre_shift = 0;
3658 if (mh != 0)
3660 rtx t1, t2, t3, t4;
3662 if (post_shift - 1 >= BITS_PER_WORD)
3663 goto fail1;
3665 extra_cost
3666 = (shift_cost[compute_mode][post_shift - 1]
3667 + shift_cost[compute_mode][1]
3668 + 2 * add_cost[compute_mode]);
3669 t1 = expand_mult_highpart (compute_mode, op0, ml,
3670 NULL_RTX, 1,
3671 max_cost - extra_cost);
3672 if (t1 == 0)
3673 goto fail1;
3674 t2 = force_operand (gen_rtx_MINUS (compute_mode,
3675 op0, t1),
3676 NULL_RTX);
3677 t3 = expand_shift
3678 (RSHIFT_EXPR, compute_mode, t2,
3679 build_int_cst (NULL_TREE, 1),
3680 NULL_RTX,1);
3681 t4 = force_operand (gen_rtx_PLUS (compute_mode,
3682 t1, t3),
3683 NULL_RTX);
3684 quotient = expand_shift
3685 (RSHIFT_EXPR, compute_mode, t4,
3686 build_int_cst (NULL_TREE, post_shift - 1),
3687 tquotient, 1);
3689 else
3691 rtx t1, t2;
3693 if (pre_shift >= BITS_PER_WORD
3694 || post_shift >= BITS_PER_WORD)
3695 goto fail1;
3697 t1 = expand_shift
3698 (RSHIFT_EXPR, compute_mode, op0,
3699 build_int_cst (NULL_TREE, pre_shift),
3700 NULL_RTX, 1);
3701 extra_cost
3702 = (shift_cost[compute_mode][pre_shift]
3703 + shift_cost[compute_mode][post_shift]);
3704 t2 = expand_mult_highpart (compute_mode, t1, ml,
3705 NULL_RTX, 1,
3706 max_cost - extra_cost);
3707 if (t2 == 0)
3708 goto fail1;
3709 quotient = expand_shift
3710 (RSHIFT_EXPR, compute_mode, t2,
3711 build_int_cst (NULL_TREE, post_shift),
3712 tquotient, 1);
3716 else /* Too wide mode to use tricky code */
3717 break;
3719 insn = get_last_insn ();
3720 if (insn != last
3721 && (set = single_set (insn)) != 0
3722 && SET_DEST (set) == quotient)
3723 set_unique_reg_note (insn,
3724 REG_EQUAL,
3725 gen_rtx_UDIV (compute_mode, op0, op1));
3727 else /* TRUNC_DIV, signed */
3729 unsigned HOST_WIDE_INT ml;
3730 int lgup, post_shift;
3731 HOST_WIDE_INT d = INTVAL (op1);
3732 unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
3734 /* n rem d = n rem -d */
3735 if (rem_flag && d < 0)
3737 d = abs_d;
3738 op1 = gen_int_mode (abs_d, compute_mode);
3741 if (d == 1)
3742 quotient = op0;
3743 else if (d == -1)
3744 quotient = expand_unop (compute_mode, neg_optab, op0,
3745 tquotient, 0);
3746 else if (abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
3748 /* This case is not handled correctly below. */
3749 quotient = emit_store_flag (tquotient, EQ, op0, op1,
3750 compute_mode, 1, 1);
3751 if (quotient == 0)
3752 goto fail1;
3754 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
3755 && (rem_flag ? smod_pow2_cheap[compute_mode]
3756 : sdiv_pow2_cheap[compute_mode])
3757 /* We assume that cheap metric is true if the
3758 optab has an expander for this mode. */
3759 && (((rem_flag ? smod_optab : sdiv_optab)
3760 ->handlers[compute_mode].insn_code
3761 != CODE_FOR_nothing)
3762 || (sdivmod_optab->handlers[compute_mode]
3763 .insn_code != CODE_FOR_nothing)))
3765 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
3767 if (rem_flag)
3769 remainder = expand_smod_pow2 (compute_mode, op0, d);
3770 if (remainder)
3771 return gen_lowpart (mode, remainder);
3774 if (sdiv_pow2_cheap[compute_mode]
3775 && ((sdiv_optab->handlers[compute_mode].insn_code
3776 != CODE_FOR_nothing)
3777 || (sdivmod_optab->handlers[compute_mode].insn_code
3778 != CODE_FOR_nothing)))
3779 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
3780 compute_mode, op0,
3781 gen_int_mode (abs_d,
3782 compute_mode),
3783 NULL_RTX, 0);
3784 else
3785 quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
3787 /* We have computed OP0 / abs(OP1). If OP1 is negative,
3788 negate the quotient. */
3789 if (d < 0)
3791 insn = get_last_insn ();
3792 if (insn != last
3793 && (set = single_set (insn)) != 0
3794 && SET_DEST (set) == quotient
3795 && abs_d < ((unsigned HOST_WIDE_INT) 1
3796 << (HOST_BITS_PER_WIDE_INT - 1)))
3797 set_unique_reg_note (insn,
3798 REG_EQUAL,
3799 gen_rtx_DIV (compute_mode,
3800 op0,
3801 GEN_INT
3802 (trunc_int_for_mode
3803 (abs_d,
3804 compute_mode))));
3806 quotient = expand_unop (compute_mode, neg_optab,
3807 quotient, quotient, 0);
3810 else if (size <= HOST_BITS_PER_WIDE_INT)
3812 choose_multiplier (abs_d, size, size - 1,
3813 &ml, &post_shift, &lgup);
3814 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
3816 rtx t1, t2, t3;
3818 if (post_shift >= BITS_PER_WORD
3819 || size - 1 >= BITS_PER_WORD)
3820 goto fail1;
3822 extra_cost = (shift_cost[compute_mode][post_shift]
3823 + shift_cost[compute_mode][size - 1]
3824 + add_cost[compute_mode]);
3825 t1 = expand_mult_highpart (compute_mode, op0, ml,
3826 NULL_RTX, 0,
3827 max_cost - extra_cost);
3828 if (t1 == 0)
3829 goto fail1;
3830 t2 = expand_shift
3831 (RSHIFT_EXPR, compute_mode, t1,
3832 build_int_cst (NULL_TREE, post_shift),
3833 NULL_RTX, 0);
3834 t3 = expand_shift
3835 (RSHIFT_EXPR, compute_mode, op0,
3836 build_int_cst (NULL_TREE, size - 1),
3837 NULL_RTX, 0);
3838 if (d < 0)
3839 quotient
3840 = force_operand (gen_rtx_MINUS (compute_mode,
3841 t3, t2),
3842 tquotient);
3843 else
3844 quotient
3845 = force_operand (gen_rtx_MINUS (compute_mode,
3846 t2, t3),
3847 tquotient);
3849 else
3851 rtx t1, t2, t3, t4;
3853 if (post_shift >= BITS_PER_WORD
3854 || size - 1 >= BITS_PER_WORD)
3855 goto fail1;
3857 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
3858 extra_cost = (shift_cost[compute_mode][post_shift]
3859 + shift_cost[compute_mode][size - 1]
3860 + 2 * add_cost[compute_mode]);
3861 t1 = expand_mult_highpart (compute_mode, op0, ml,
3862 NULL_RTX, 0,
3863 max_cost - extra_cost);
3864 if (t1 == 0)
3865 goto fail1;
3866 t2 = force_operand (gen_rtx_PLUS (compute_mode,
3867 t1, op0),
3868 NULL_RTX);
3869 t3 = expand_shift
3870 (RSHIFT_EXPR, compute_mode, t2,
3871 build_int_cst (NULL_TREE, post_shift),
3872 NULL_RTX, 0);
3873 t4 = expand_shift
3874 (RSHIFT_EXPR, compute_mode, op0,
3875 build_int_cst (NULL_TREE, size - 1),
3876 NULL_RTX, 0);
3877 if (d < 0)
3878 quotient
3879 = force_operand (gen_rtx_MINUS (compute_mode,
3880 t4, t3),
3881 tquotient);
3882 else
3883 quotient
3884 = force_operand (gen_rtx_MINUS (compute_mode,
3885 t3, t4),
3886 tquotient);
3889 else /* Too wide mode to use tricky code */
3890 break;
3892 insn = get_last_insn ();
3893 if (insn != last
3894 && (set = single_set (insn)) != 0
3895 && SET_DEST (set) == quotient)
3896 set_unique_reg_note (insn,
3897 REG_EQUAL,
3898 gen_rtx_DIV (compute_mode, op0, op1));
3900 break;
3902 fail1:
3903 delete_insns_since (last);
3904 break;
3906 case FLOOR_DIV_EXPR:
3907 case FLOOR_MOD_EXPR:
3908 /* We will come here only for signed operations. */
3909 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
3911 unsigned HOST_WIDE_INT mh, ml;
3912 int pre_shift, lgup, post_shift;
3913 HOST_WIDE_INT d = INTVAL (op1);
3915 if (d > 0)
3917 /* We could just as easily deal with negative constants here,
3918 but it does not seem worth the trouble for GCC 2.6. */
3919 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3921 pre_shift = floor_log2 (d);
3922 if (rem_flag)
3924 remainder = expand_binop (compute_mode, and_optab, op0,
3925 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3926 remainder, 0, OPTAB_LIB_WIDEN);
3927 if (remainder)
3928 return gen_lowpart (mode, remainder);
3930 quotient = expand_shift
3931 (RSHIFT_EXPR, compute_mode, op0,
3932 build_int_cst (NULL_TREE, pre_shift),
3933 tquotient, 0);
3935 else
3937 rtx t1, t2, t3, t4;
3939 mh = choose_multiplier (d, size, size - 1,
3940 &ml, &post_shift, &lgup);
3941 gcc_assert (!mh);
3943 if (post_shift < BITS_PER_WORD
3944 && size - 1 < BITS_PER_WORD)
3946 t1 = expand_shift
3947 (RSHIFT_EXPR, compute_mode, op0,
3948 build_int_cst (NULL_TREE, size - 1),
3949 NULL_RTX, 0);
3950 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
3951 NULL_RTX, 0, OPTAB_WIDEN);
3952 extra_cost = (shift_cost[compute_mode][post_shift]
3953 + shift_cost[compute_mode][size - 1]
3954 + 2 * add_cost[compute_mode]);
3955 t3 = expand_mult_highpart (compute_mode, t2, ml,
3956 NULL_RTX, 1,
3957 max_cost - extra_cost);
3958 if (t3 != 0)
3960 t4 = expand_shift
3961 (RSHIFT_EXPR, compute_mode, t3,
3962 build_int_cst (NULL_TREE, post_shift),
3963 NULL_RTX, 1);
3964 quotient = expand_binop (compute_mode, xor_optab,
3965 t4, t1, tquotient, 0,
3966 OPTAB_WIDEN);
3971 else
3973 rtx nsign, t1, t2, t3, t4;
3974 t1 = force_operand (gen_rtx_PLUS (compute_mode,
3975 op0, constm1_rtx), NULL_RTX);
3976 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
3977 0, OPTAB_WIDEN);
3978 nsign = expand_shift
3979 (RSHIFT_EXPR, compute_mode, t2,
3980 build_int_cst (NULL_TREE, size - 1),
3981 NULL_RTX, 0);
3982 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
3983 NULL_RTX);
3984 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
3985 NULL_RTX, 0);
3986 if (t4)
3988 rtx t5;
3989 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
3990 NULL_RTX, 0);
3991 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3992 t4, t5),
3993 tquotient);
3998 if (quotient != 0)
3999 break;
4000 delete_insns_since (last);
4002 /* Try using an instruction that produces both the quotient and
4003 remainder, using truncation. We can easily compensate the quotient
4004 or remainder to get floor rounding, once we have the remainder.
4005 Notice that we compute also the final remainder value here,
4006 and return the result right away. */
4007 if (target == 0 || GET_MODE (target) != compute_mode)
4008 target = gen_reg_rtx (compute_mode);
4010 if (rem_flag)
4012 remainder
4013 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4014 quotient = gen_reg_rtx (compute_mode);
4016 else
4018 quotient
4019 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4020 remainder = gen_reg_rtx (compute_mode);
4023 if (expand_twoval_binop (sdivmod_optab, op0, op1,
4024 quotient, remainder, 0))
4026 /* This could be computed with a branch-less sequence.
4027 Save that for later. */
4028 rtx tem;
4029 rtx label = gen_label_rtx ();
4030 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4031 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4032 NULL_RTX, 0, OPTAB_WIDEN);
4033 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4034 expand_dec (quotient, const1_rtx);
4035 expand_inc (remainder, op1);
4036 emit_label (label);
4037 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4040 /* No luck with division elimination or divmod. Have to do it
4041 by conditionally adjusting op0 *and* the result. */
4043 rtx label1, label2, label3, label4, label5;
4044 rtx adjusted_op0;
4045 rtx tem;
4047 quotient = gen_reg_rtx (compute_mode);
4048 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4049 label1 = gen_label_rtx ();
4050 label2 = gen_label_rtx ();
4051 label3 = gen_label_rtx ();
4052 label4 = gen_label_rtx ();
4053 label5 = gen_label_rtx ();
4054 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4055 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4056 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4057 quotient, 0, OPTAB_LIB_WIDEN);
4058 if (tem != quotient)
4059 emit_move_insn (quotient, tem);
4060 emit_jump_insn (gen_jump (label5));
4061 emit_barrier ();
4062 emit_label (label1);
4063 expand_inc (adjusted_op0, const1_rtx);
4064 emit_jump_insn (gen_jump (label4));
4065 emit_barrier ();
4066 emit_label (label2);
4067 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4068 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4069 quotient, 0, OPTAB_LIB_WIDEN);
4070 if (tem != quotient)
4071 emit_move_insn (quotient, tem);
4072 emit_jump_insn (gen_jump (label5));
4073 emit_barrier ();
4074 emit_label (label3);
4075 expand_dec (adjusted_op0, const1_rtx);
4076 emit_label (label4);
4077 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4078 quotient, 0, OPTAB_LIB_WIDEN);
4079 if (tem != quotient)
4080 emit_move_insn (quotient, tem);
4081 expand_dec (quotient, const1_rtx);
4082 emit_label (label5);
4084 break;
4086 case CEIL_DIV_EXPR:
4087 case CEIL_MOD_EXPR:
4088 if (unsignedp)
4090 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
4092 rtx t1, t2, t3;
4093 unsigned HOST_WIDE_INT d = INTVAL (op1);
4094 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4095 build_int_cst (NULL_TREE, floor_log2 (d)),
4096 tquotient, 1);
4097 t2 = expand_binop (compute_mode, and_optab, op0,
4098 GEN_INT (d - 1),
4099 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4100 t3 = gen_reg_rtx (compute_mode);
4101 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4102 compute_mode, 1, 1);
4103 if (t3 == 0)
4105 rtx lab;
4106 lab = gen_label_rtx ();
4107 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4108 expand_inc (t1, const1_rtx);
4109 emit_label (lab);
4110 quotient = t1;
4112 else
4113 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4114 t1, t3),
4115 tquotient);
4116 break;
4119 /* Try using an instruction that produces both the quotient and
4120 remainder, using truncation. We can easily compensate the
4121 quotient or remainder to get ceiling rounding, once we have the
4122 remainder. Notice that we compute also the final remainder
4123 value here, and return the result right away. */
4124 if (target == 0 || GET_MODE (target) != compute_mode)
4125 target = gen_reg_rtx (compute_mode);
4127 if (rem_flag)
4129 remainder = (REG_P (target)
4130 ? target : gen_reg_rtx (compute_mode));
4131 quotient = gen_reg_rtx (compute_mode);
4133 else
4135 quotient = (REG_P (target)
4136 ? target : gen_reg_rtx (compute_mode));
4137 remainder = gen_reg_rtx (compute_mode);
4140 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4141 remainder, 1))
4143 /* This could be computed with a branch-less sequence.
4144 Save that for later. */
4145 rtx label = gen_label_rtx ();
4146 do_cmp_and_jump (remainder, const0_rtx, EQ,
4147 compute_mode, label);
4148 expand_inc (quotient, const1_rtx);
4149 expand_dec (remainder, op1);
4150 emit_label (label);
4151 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4154 /* No luck with division elimination or divmod. Have to do it
4155 by conditionally adjusting op0 *and* the result. */
4157 rtx label1, label2;
4158 rtx adjusted_op0, tem;
4160 quotient = gen_reg_rtx (compute_mode);
4161 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4162 label1 = gen_label_rtx ();
4163 label2 = gen_label_rtx ();
4164 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4165 compute_mode, label1);
4166 emit_move_insn (quotient, const0_rtx);
4167 emit_jump_insn (gen_jump (label2));
4168 emit_barrier ();
4169 emit_label (label1);
4170 expand_dec (adjusted_op0, const1_rtx);
4171 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4172 quotient, 1, OPTAB_LIB_WIDEN);
4173 if (tem != quotient)
4174 emit_move_insn (quotient, tem);
4175 expand_inc (quotient, const1_rtx);
4176 emit_label (label2);
4179 else /* signed */
4181 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4182 && INTVAL (op1) >= 0)
4184 /* This is extremely similar to the code for the unsigned case
4185 above. For 2.7 we should merge these variants, but for
4186 2.6.1 I don't want to touch the code for unsigned since that
4187 get used in C. The signed case will only be used by other
4188 languages (Ada). */
4190 rtx t1, t2, t3;
4191 unsigned HOST_WIDE_INT d = INTVAL (op1);
4192 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4193 build_int_cst (NULL_TREE, floor_log2 (d)),
4194 tquotient, 0);
4195 t2 = expand_binop (compute_mode, and_optab, op0,
4196 GEN_INT (d - 1),
4197 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4198 t3 = gen_reg_rtx (compute_mode);
4199 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4200 compute_mode, 1, 1);
4201 if (t3 == 0)
4203 rtx lab;
4204 lab = gen_label_rtx ();
4205 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4206 expand_inc (t1, const1_rtx);
4207 emit_label (lab);
4208 quotient = t1;
4210 else
4211 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4212 t1, t3),
4213 tquotient);
4214 break;
4217 /* Try using an instruction that produces both the quotient and
4218 remainder, using truncation. We can easily compensate the
4219 quotient or remainder to get ceiling rounding, once we have the
4220 remainder. Notice that we compute also the final remainder
4221 value here, and return the result right away. */
4222 if (target == 0 || GET_MODE (target) != compute_mode)
4223 target = gen_reg_rtx (compute_mode);
4224 if (rem_flag)
4226 remainder= (REG_P (target)
4227 ? target : gen_reg_rtx (compute_mode));
4228 quotient = gen_reg_rtx (compute_mode);
4230 else
4232 quotient = (REG_P (target)
4233 ? target : gen_reg_rtx (compute_mode));
4234 remainder = gen_reg_rtx (compute_mode);
4237 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4238 remainder, 0))
4240 /* This could be computed with a branch-less sequence.
4241 Save that for later. */
4242 rtx tem;
4243 rtx label = gen_label_rtx ();
4244 do_cmp_and_jump (remainder, const0_rtx, EQ,
4245 compute_mode, label);
4246 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4247 NULL_RTX, 0, OPTAB_WIDEN);
4248 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4249 expand_inc (quotient, const1_rtx);
4250 expand_dec (remainder, op1);
4251 emit_label (label);
4252 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4255 /* No luck with division elimination or divmod. Have to do it
4256 by conditionally adjusting op0 *and* the result. */
4258 rtx label1, label2, label3, label4, label5;
4259 rtx adjusted_op0;
4260 rtx tem;
4262 quotient = gen_reg_rtx (compute_mode);
4263 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4264 label1 = gen_label_rtx ();
4265 label2 = gen_label_rtx ();
4266 label3 = gen_label_rtx ();
4267 label4 = gen_label_rtx ();
4268 label5 = gen_label_rtx ();
4269 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4270 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4271 compute_mode, label1);
4272 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4273 quotient, 0, OPTAB_LIB_WIDEN);
4274 if (tem != quotient)
4275 emit_move_insn (quotient, tem);
4276 emit_jump_insn (gen_jump (label5));
4277 emit_barrier ();
4278 emit_label (label1);
4279 expand_dec (adjusted_op0, const1_rtx);
4280 emit_jump_insn (gen_jump (label4));
4281 emit_barrier ();
4282 emit_label (label2);
4283 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4284 compute_mode, label3);
4285 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4286 quotient, 0, OPTAB_LIB_WIDEN);
4287 if (tem != quotient)
4288 emit_move_insn (quotient, tem);
4289 emit_jump_insn (gen_jump (label5));
4290 emit_barrier ();
4291 emit_label (label3);
4292 expand_inc (adjusted_op0, const1_rtx);
4293 emit_label (label4);
4294 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4295 quotient, 0, OPTAB_LIB_WIDEN);
4296 if (tem != quotient)
4297 emit_move_insn (quotient, tem);
4298 expand_inc (quotient, const1_rtx);
4299 emit_label (label5);
4302 break;
4304 case EXACT_DIV_EXPR:
4305 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4307 HOST_WIDE_INT d = INTVAL (op1);
4308 unsigned HOST_WIDE_INT ml;
4309 int pre_shift;
4310 rtx t1;
4312 pre_shift = floor_log2 (d & -d);
4313 ml = invert_mod2n (d >> pre_shift, size);
4314 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4315 build_int_cst (NULL_TREE, pre_shift),
4316 NULL_RTX, unsignedp);
4317 quotient = expand_mult (compute_mode, t1,
4318 gen_int_mode (ml, compute_mode),
4319 NULL_RTX, 1);
4321 insn = get_last_insn ();
4322 set_unique_reg_note (insn,
4323 REG_EQUAL,
4324 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4325 compute_mode,
4326 op0, op1));
4328 break;
4330 case ROUND_DIV_EXPR:
4331 case ROUND_MOD_EXPR:
4332 if (unsignedp)
4334 rtx tem;
4335 rtx label;
4336 label = gen_label_rtx ();
4337 quotient = gen_reg_rtx (compute_mode);
4338 remainder = gen_reg_rtx (compute_mode);
4339 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4341 rtx tem;
4342 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
4343 quotient, 1, OPTAB_LIB_WIDEN);
4344 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
4345 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4346 remainder, 1, OPTAB_LIB_WIDEN);
4348 tem = plus_constant (op1, -1);
4349 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4350 build_int_cst (NULL_TREE, 1),
4351 NULL_RTX, 1);
4352 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
4353 expand_inc (quotient, const1_rtx);
4354 expand_dec (remainder, op1);
4355 emit_label (label);
4357 else
4359 rtx abs_rem, abs_op1, tem, mask;
4360 rtx label;
4361 label = gen_label_rtx ();
4362 quotient = gen_reg_rtx (compute_mode);
4363 remainder = gen_reg_rtx (compute_mode);
4364 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
4366 rtx tem;
4367 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
4368 quotient, 0, OPTAB_LIB_WIDEN);
4369 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
4370 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4371 remainder, 0, OPTAB_LIB_WIDEN);
4373 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
4374 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
4375 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
4376 build_int_cst (NULL_TREE, 1),
4377 NULL_RTX, 1);
4378 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
4379 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4380 NULL_RTX, 0, OPTAB_WIDEN);
4381 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4382 build_int_cst (NULL_TREE, size - 1),
4383 NULL_RTX, 0);
4384 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
4385 NULL_RTX, 0, OPTAB_WIDEN);
4386 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4387 NULL_RTX, 0, OPTAB_WIDEN);
4388 expand_inc (quotient, tem);
4389 tem = expand_binop (compute_mode, xor_optab, mask, op1,
4390 NULL_RTX, 0, OPTAB_WIDEN);
4391 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4392 NULL_RTX, 0, OPTAB_WIDEN);
4393 expand_dec (remainder, tem);
4394 emit_label (label);
4396 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4398 default:
4399 gcc_unreachable ();
4402 if (quotient == 0)
4404 if (target && GET_MODE (target) != compute_mode)
4405 target = 0;
4407 if (rem_flag)
4409 /* Try to produce the remainder without producing the quotient.
4410 If we seem to have a divmod pattern that does not require widening,
4411 don't try widening here. We should really have a WIDEN argument
4412 to expand_twoval_binop, since what we'd really like to do here is
4413 1) try a mod insn in compute_mode
4414 2) try a divmod insn in compute_mode
4415 3) try a div insn in compute_mode and multiply-subtract to get
4416 remainder
4417 4) try the same things with widening allowed. */
4418 remainder
4419 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4420 op0, op1, target,
4421 unsignedp,
4422 ((optab2->handlers[compute_mode].insn_code
4423 != CODE_FOR_nothing)
4424 ? OPTAB_DIRECT : OPTAB_WIDEN));
4425 if (remainder == 0)
4427 /* No luck there. Can we do remainder and divide at once
4428 without a library call? */
4429 remainder = gen_reg_rtx (compute_mode);
4430 if (! expand_twoval_binop ((unsignedp
4431 ? udivmod_optab
4432 : sdivmod_optab),
4433 op0, op1,
4434 NULL_RTX, remainder, unsignedp))
4435 remainder = 0;
4438 if (remainder)
4439 return gen_lowpart (mode, remainder);
4442 /* Produce the quotient. Try a quotient insn, but not a library call.
4443 If we have a divmod in this mode, use it in preference to widening
4444 the div (for this test we assume it will not fail). Note that optab2
4445 is set to the one of the two optabs that the call below will use. */
4446 quotient
4447 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
4448 op0, op1, rem_flag ? NULL_RTX : target,
4449 unsignedp,
4450 ((optab2->handlers[compute_mode].insn_code
4451 != CODE_FOR_nothing)
4452 ? OPTAB_DIRECT : OPTAB_WIDEN));
4454 if (quotient == 0)
4456 /* No luck there. Try a quotient-and-remainder insn,
4457 keeping the quotient alone. */
4458 quotient = gen_reg_rtx (compute_mode);
4459 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
4460 op0, op1,
4461 quotient, NULL_RTX, unsignedp))
4463 quotient = 0;
4464 if (! rem_flag)
4465 /* Still no luck. If we are not computing the remainder,
4466 use a library call for the quotient. */
4467 quotient = sign_expand_binop (compute_mode,
4468 udiv_optab, sdiv_optab,
4469 op0, op1, target,
4470 unsignedp, OPTAB_LIB_WIDEN);
4475 if (rem_flag)
4477 if (target && GET_MODE (target) != compute_mode)
4478 target = 0;
4480 if (quotient == 0)
4482 /* No divide instruction either. Use library for remainder. */
4483 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4484 op0, op1, target,
4485 unsignedp, OPTAB_LIB_WIDEN);
4486 /* No remainder function. Try a quotient-and-remainder
4487 function, keeping the remainder. */
4488 if (!remainder)
4490 remainder = gen_reg_rtx (compute_mode);
4491 if (!expand_twoval_binop_libfunc
4492 (unsignedp ? udivmod_optab : sdivmod_optab,
4493 op0, op1,
4494 NULL_RTX, remainder,
4495 unsignedp ? UMOD : MOD))
4496 remainder = NULL_RTX;
4499 else
4501 /* We divided. Now finish doing X - Y * (X / Y). */
4502 remainder = expand_mult (compute_mode, quotient, op1,
4503 NULL_RTX, unsignedp);
4504 remainder = expand_binop (compute_mode, sub_optab, op0,
4505 remainder, target, unsignedp,
4506 OPTAB_LIB_WIDEN);
4510 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4513 /* Return a tree node with data type TYPE, describing the value of X.
4514 Usually this is an VAR_DECL, if there is no obvious better choice.
4515 X may be an expression, however we only support those expressions
4516 generated by loop.c. */
4518 tree
4519 make_tree (tree type, rtx x)
4521 tree t;
4523 switch (GET_CODE (x))
4525 case CONST_INT:
4527 HOST_WIDE_INT hi = 0;
4529 if (INTVAL (x) < 0
4530 && !(TYPE_UNSIGNED (type)
4531 && (GET_MODE_BITSIZE (TYPE_MODE (type))
4532 < HOST_BITS_PER_WIDE_INT)))
4533 hi = -1;
4535 t = build_int_cst_wide (type, INTVAL (x), hi);
4537 return t;
4540 case CONST_DOUBLE:
4541 if (GET_MODE (x) == VOIDmode)
4542 t = build_int_cst_wide (type,
4543 CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
4544 else
4546 REAL_VALUE_TYPE d;
4548 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
4549 t = build_real (type, d);
4552 return t;
4554 case CONST_VECTOR:
4556 int i, units;
4557 rtx elt;
4558 tree t = NULL_TREE;
4560 units = CONST_VECTOR_NUNITS (x);
4562 /* Build a tree with vector elements. */
4563 for (i = units - 1; i >= 0; --i)
4565 elt = CONST_VECTOR_ELT (x, i);
4566 t = tree_cons (NULL_TREE, make_tree (type, elt), t);
4569 return build_vector (type, t);
4572 case PLUS:
4573 return fold (build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4574 make_tree (type, XEXP (x, 1))));
4576 case MINUS:
4577 return fold (build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4578 make_tree (type, XEXP (x, 1))));
4580 case NEG:
4581 return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
4583 case MULT:
4584 return fold (build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
4585 make_tree (type, XEXP (x, 1))));
4587 case ASHIFT:
4588 return fold (build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
4589 make_tree (type, XEXP (x, 1))));
4591 case LSHIFTRT:
4592 t = lang_hooks.types.unsigned_type (type);
4593 return fold (convert (type,
4594 build2 (RSHIFT_EXPR, t,
4595 make_tree (t, XEXP (x, 0)),
4596 make_tree (type, XEXP (x, 1)))));
4598 case ASHIFTRT:
4599 t = lang_hooks.types.signed_type (type);
4600 return fold (convert (type,
4601 build2 (RSHIFT_EXPR, t,
4602 make_tree (t, XEXP (x, 0)),
4603 make_tree (type, XEXP (x, 1)))));
4605 case DIV:
4606 if (TREE_CODE (type) != REAL_TYPE)
4607 t = lang_hooks.types.signed_type (type);
4608 else
4609 t = type;
4611 return fold (convert (type,
4612 build2 (TRUNC_DIV_EXPR, t,
4613 make_tree (t, XEXP (x, 0)),
4614 make_tree (t, XEXP (x, 1)))));
4615 case UDIV:
4616 t = lang_hooks.types.unsigned_type (type);
4617 return fold (convert (type,
4618 build2 (TRUNC_DIV_EXPR, t,
4619 make_tree (t, XEXP (x, 0)),
4620 make_tree (t, XEXP (x, 1)))));
4622 case SIGN_EXTEND:
4623 case ZERO_EXTEND:
4624 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
4625 GET_CODE (x) == ZERO_EXTEND);
4626 return fold (convert (type, make_tree (t, XEXP (x, 0))));
4628 default:
4629 t = build_decl (VAR_DECL, NULL_TREE, type);
4631 /* If TYPE is a POINTER_TYPE, X might be Pmode with TYPE_MODE being
4632 ptr_mode. So convert. */
4633 if (POINTER_TYPE_P (type))
4634 x = convert_memory_address (TYPE_MODE (type), x);
4636 /* Note that we do *not* use SET_DECL_RTL here, because we do not
4637 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
4638 t->decl.rtl = x;
4640 return t;
4644 /* Check whether the multiplication X * MULT + ADD overflows.
4645 X, MULT and ADD must be CONST_*.
4646 MODE is the machine mode for the computation.
4647 X and MULT must have mode MODE. ADD may have a different mode.
4648 So can X (defaults to same as MODE).
4649 UNSIGNEDP is nonzero to do unsigned multiplication. */
4651 bool
4652 const_mult_add_overflow_p (rtx x, rtx mult, rtx add,
4653 enum machine_mode mode, int unsignedp)
4655 tree type, mult_type, add_type, result;
4657 type = lang_hooks.types.type_for_mode (mode, unsignedp);
4659 /* In order to get a proper overflow indication from an unsigned
4660 type, we have to pretend that it's a sizetype. */
4661 mult_type = type;
4662 if (unsignedp)
4664 /* FIXME:It would be nice if we could step directly from this
4665 type to its sizetype equivalent. */
4666 mult_type = build_distinct_type_copy (type);
4667 TYPE_IS_SIZETYPE (mult_type) = 1;
4670 add_type = (GET_MODE (add) == VOIDmode ? mult_type
4671 : lang_hooks.types.type_for_mode (GET_MODE (add), unsignedp));
4673 result = fold (build2 (PLUS_EXPR, mult_type,
4674 fold (build2 (MULT_EXPR, mult_type,
4675 make_tree (mult_type, x),
4676 make_tree (mult_type, mult))),
4677 make_tree (add_type, add)));
4679 return TREE_CONSTANT_OVERFLOW (result);
4682 /* Return an rtx representing the value of X * MULT + ADD.
4683 TARGET is a suggestion for where to store the result (an rtx).
4684 MODE is the machine mode for the computation.
4685 X and MULT must have mode MODE. ADD may have a different mode.
4686 So can X (defaults to same as MODE).
4687 UNSIGNEDP is nonzero to do unsigned multiplication.
4688 This may emit insns. */
4691 expand_mult_add (rtx x, rtx target, rtx mult, rtx add, enum machine_mode mode,
4692 int unsignedp)
4694 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
4695 tree add_type = (GET_MODE (add) == VOIDmode
4696 ? type: lang_hooks.types.type_for_mode (GET_MODE (add),
4697 unsignedp));
4698 tree result = fold (build2 (PLUS_EXPR, type,
4699 fold (build2 (MULT_EXPR, type,
4700 make_tree (type, x),
4701 make_tree (type, mult))),
4702 make_tree (add_type, add)));
4704 return expand_expr (result, target, VOIDmode, 0);
4707 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
4708 and returning TARGET.
4710 If TARGET is 0, a pseudo-register or constant is returned. */
4713 expand_and (enum machine_mode mode, rtx op0, rtx op1, rtx target)
4715 rtx tem = 0;
4717 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
4718 tem = simplify_binary_operation (AND, mode, op0, op1);
4719 if (tem == 0)
4720 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
4722 if (target == 0)
4723 target = tem;
4724 else if (tem != target)
4725 emit_move_insn (target, tem);
4726 return target;
4729 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
4730 and storing in TARGET. Normally return TARGET.
4731 Return 0 if that cannot be done.
4733 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
4734 it is VOIDmode, they cannot both be CONST_INT.
4736 UNSIGNEDP is for the case where we have to widen the operands
4737 to perform the operation. It says to use zero-extension.
4739 NORMALIZEP is 1 if we should convert the result to be either zero
4740 or one. Normalize is -1 if we should convert the result to be
4741 either zero or -1. If NORMALIZEP is zero, the result will be left
4742 "raw" out of the scc insn. */
4745 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
4746 enum machine_mode mode, int unsignedp, int normalizep)
4748 rtx subtarget;
4749 enum insn_code icode;
4750 enum machine_mode compare_mode;
4751 enum machine_mode target_mode = GET_MODE (target);
4752 rtx tem;
4753 rtx last = get_last_insn ();
4754 rtx pattern, comparison;
4756 if (unsignedp)
4757 code = unsigned_condition (code);
4759 /* If one operand is constant, make it the second one. Only do this
4760 if the other operand is not constant as well. */
4762 if (swap_commutative_operands_p (op0, op1))
4764 tem = op0;
4765 op0 = op1;
4766 op1 = tem;
4767 code = swap_condition (code);
4770 if (mode == VOIDmode)
4771 mode = GET_MODE (op0);
4773 /* For some comparisons with 1 and -1, we can convert this to
4774 comparisons with zero. This will often produce more opportunities for
4775 store-flag insns. */
4777 switch (code)
4779 case LT:
4780 if (op1 == const1_rtx)
4781 op1 = const0_rtx, code = LE;
4782 break;
4783 case LE:
4784 if (op1 == constm1_rtx)
4785 op1 = const0_rtx, code = LT;
4786 break;
4787 case GE:
4788 if (op1 == const1_rtx)
4789 op1 = const0_rtx, code = GT;
4790 break;
4791 case GT:
4792 if (op1 == constm1_rtx)
4793 op1 = const0_rtx, code = GE;
4794 break;
4795 case GEU:
4796 if (op1 == const1_rtx)
4797 op1 = const0_rtx, code = NE;
4798 break;
4799 case LTU:
4800 if (op1 == const1_rtx)
4801 op1 = const0_rtx, code = EQ;
4802 break;
4803 default:
4804 break;
4807 /* If we are comparing a double-word integer with zero or -1, we can
4808 convert the comparison into one involving a single word. */
4809 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
4810 && GET_MODE_CLASS (mode) == MODE_INT
4811 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
4813 if ((code == EQ || code == NE)
4814 && (op1 == const0_rtx || op1 == constm1_rtx))
4816 rtx op00, op01, op0both;
4818 /* Do a logical OR or AND of the two words and compare the result. */
4819 op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
4820 op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
4821 op0both = expand_binop (word_mode,
4822 op1 == const0_rtx ? ior_optab : and_optab,
4823 op00, op01, NULL_RTX, unsignedp, OPTAB_DIRECT);
4825 if (op0both != 0)
4826 return emit_store_flag (target, code, op0both, op1, word_mode,
4827 unsignedp, normalizep);
4829 else if ((code == LT || code == GE) && op1 == const0_rtx)
4831 rtx op0h;
4833 /* If testing the sign bit, can just test on high word. */
4834 op0h = simplify_gen_subreg (word_mode, op0, mode,
4835 subreg_highpart_offset (word_mode, mode));
4836 return emit_store_flag (target, code, op0h, op1, word_mode,
4837 unsignedp, normalizep);
4841 /* From now on, we won't change CODE, so set ICODE now. */
4842 icode = setcc_gen_code[(int) code];
4844 /* If this is A < 0 or A >= 0, we can do this by taking the ones
4845 complement of A (for GE) and shifting the sign bit to the low bit. */
4846 if (op1 == const0_rtx && (code == LT || code == GE)
4847 && GET_MODE_CLASS (mode) == MODE_INT
4848 && (normalizep || STORE_FLAG_VALUE == 1
4849 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4850 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4851 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
4853 subtarget = target;
4855 /* If the result is to be wider than OP0, it is best to convert it
4856 first. If it is to be narrower, it is *incorrect* to convert it
4857 first. */
4858 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
4860 op0 = convert_modes (target_mode, mode, op0, 0);
4861 mode = target_mode;
4864 if (target_mode != mode)
4865 subtarget = 0;
4867 if (code == GE)
4868 op0 = expand_unop (mode, one_cmpl_optab, op0,
4869 ((STORE_FLAG_VALUE == 1 || normalizep)
4870 ? 0 : subtarget), 0);
4872 if (STORE_FLAG_VALUE == 1 || normalizep)
4873 /* If we are supposed to produce a 0/1 value, we want to do
4874 a logical shift from the sign bit to the low-order bit; for
4875 a -1/0 value, we do an arithmetic shift. */
4876 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
4877 size_int (GET_MODE_BITSIZE (mode) - 1),
4878 subtarget, normalizep != -1);
4880 if (mode != target_mode)
4881 op0 = convert_modes (target_mode, mode, op0, 0);
4883 return op0;
4886 if (icode != CODE_FOR_nothing)
4888 insn_operand_predicate_fn pred;
4890 /* We think we may be able to do this with a scc insn. Emit the
4891 comparison and then the scc insn. */
4893 do_pending_stack_adjust ();
4894 last = get_last_insn ();
4896 comparison
4897 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX);
4898 if (CONSTANT_P (comparison))
4900 switch (GET_CODE (comparison))
4902 case CONST_INT:
4903 if (comparison == const0_rtx)
4904 return const0_rtx;
4905 break;
4907 #ifdef FLOAT_STORE_FLAG_VALUE
4908 case CONST_DOUBLE:
4909 if (comparison == CONST0_RTX (GET_MODE (comparison)))
4910 return const0_rtx;
4911 break;
4912 #endif
4913 default:
4914 gcc_unreachable ();
4917 if (normalizep == 1)
4918 return const1_rtx;
4919 if (normalizep == -1)
4920 return constm1_rtx;
4921 return const_true_rtx;
4924 /* The code of COMPARISON may not match CODE if compare_from_rtx
4925 decided to swap its operands and reverse the original code.
4927 We know that compare_from_rtx returns either a CONST_INT or
4928 a new comparison code, so it is safe to just extract the
4929 code from COMPARISON. */
4930 code = GET_CODE (comparison);
4932 /* Get a reference to the target in the proper mode for this insn. */
4933 compare_mode = insn_data[(int) icode].operand[0].mode;
4934 subtarget = target;
4935 pred = insn_data[(int) icode].operand[0].predicate;
4936 if (optimize || ! (*pred) (subtarget, compare_mode))
4937 subtarget = gen_reg_rtx (compare_mode);
4939 pattern = GEN_FCN (icode) (subtarget);
4940 if (pattern)
4942 emit_insn (pattern);
4944 /* If we are converting to a wider mode, first convert to
4945 TARGET_MODE, then normalize. This produces better combining
4946 opportunities on machines that have a SIGN_EXTRACT when we are
4947 testing a single bit. This mostly benefits the 68k.
4949 If STORE_FLAG_VALUE does not have the sign bit set when
4950 interpreted in COMPARE_MODE, we can do this conversion as
4951 unsigned, which is usually more efficient. */
4952 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
4954 convert_move (target, subtarget,
4955 (GET_MODE_BITSIZE (compare_mode)
4956 <= HOST_BITS_PER_WIDE_INT)
4957 && 0 == (STORE_FLAG_VALUE
4958 & ((HOST_WIDE_INT) 1
4959 << (GET_MODE_BITSIZE (compare_mode) -1))));
4960 op0 = target;
4961 compare_mode = target_mode;
4963 else
4964 op0 = subtarget;
4966 /* If we want to keep subexpressions around, don't reuse our
4967 last target. */
4969 if (optimize)
4970 subtarget = 0;
4972 /* Now normalize to the proper value in COMPARE_MODE. Sometimes
4973 we don't have to do anything. */
4974 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
4976 /* STORE_FLAG_VALUE might be the most negative number, so write
4977 the comparison this way to avoid a compiler-time warning. */
4978 else if (- normalizep == STORE_FLAG_VALUE)
4979 op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
4981 /* We don't want to use STORE_FLAG_VALUE < 0 below since this
4982 makes it hard to use a value of just the sign bit due to
4983 ANSI integer constant typing rules. */
4984 else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
4985 && (STORE_FLAG_VALUE
4986 & ((HOST_WIDE_INT) 1
4987 << (GET_MODE_BITSIZE (compare_mode) - 1))))
4988 op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
4989 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
4990 subtarget, normalizep == 1);
4991 else
4993 gcc_assert (STORE_FLAG_VALUE & 1);
4995 op0 = expand_and (compare_mode, op0, const1_rtx, subtarget);
4996 if (normalizep == -1)
4997 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
5000 /* If we were converting to a smaller mode, do the
5001 conversion now. */
5002 if (target_mode != compare_mode)
5004 convert_move (target, op0, 0);
5005 return target;
5007 else
5008 return op0;
5012 delete_insns_since (last);
5014 /* If optimizing, use different pseudo registers for each insn, instead
5015 of reusing the same pseudo. This leads to better CSE, but slows
5016 down the compiler, since there are more pseudos */
5017 subtarget = (!optimize
5018 && (target_mode == mode)) ? target : NULL_RTX;
5020 /* If we reached here, we can't do this with a scc insn. However, there
5021 are some comparisons that can be done directly. For example, if
5022 this is an equality comparison of integers, we can try to exclusive-or
5023 (or subtract) the two operands and use a recursive call to try the
5024 comparison with zero. Don't do any of these cases if branches are
5025 very cheap. */
5027 if (BRANCH_COST > 0
5028 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
5029 && op1 != const0_rtx)
5031 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5032 OPTAB_WIDEN);
5034 if (tem == 0)
5035 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5036 OPTAB_WIDEN);
5037 if (tem != 0)
5038 tem = emit_store_flag (target, code, tem, const0_rtx,
5039 mode, unsignedp, normalizep);
5040 if (tem == 0)
5041 delete_insns_since (last);
5042 return tem;
5045 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5046 the constant zero. Reject all other comparisons at this point. Only
5047 do LE and GT if branches are expensive since they are expensive on
5048 2-operand machines. */
5050 if (BRANCH_COST == 0
5051 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
5052 || (code != EQ && code != NE
5053 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
5054 return 0;
5056 /* See what we need to return. We can only return a 1, -1, or the
5057 sign bit. */
5059 if (normalizep == 0)
5061 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5062 normalizep = STORE_FLAG_VALUE;
5064 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5065 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5066 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
5068 else
5069 return 0;
5072 /* Try to put the result of the comparison in the sign bit. Assume we can't
5073 do the necessary operation below. */
5075 tem = 0;
5077 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5078 the sign bit set. */
5080 if (code == LE)
5082 /* This is destructive, so SUBTARGET can't be OP0. */
5083 if (rtx_equal_p (subtarget, op0))
5084 subtarget = 0;
5086 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5087 OPTAB_WIDEN);
5088 if (tem)
5089 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5090 OPTAB_WIDEN);
5093 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5094 number of bits in the mode of OP0, minus one. */
5096 if (code == GT)
5098 if (rtx_equal_p (subtarget, op0))
5099 subtarget = 0;
5101 tem = expand_shift (RSHIFT_EXPR, mode, op0,
5102 size_int (GET_MODE_BITSIZE (mode) - 1),
5103 subtarget, 0);
5104 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5105 OPTAB_WIDEN);
5108 if (code == EQ || code == NE)
5110 /* For EQ or NE, one way to do the comparison is to apply an operation
5111 that converts the operand into a positive number if it is nonzero
5112 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5113 for NE we negate. This puts the result in the sign bit. Then we
5114 normalize with a shift, if needed.
5116 Two operations that can do the above actions are ABS and FFS, so try
5117 them. If that doesn't work, and MODE is smaller than a full word,
5118 we can use zero-extension to the wider mode (an unsigned conversion)
5119 as the operation. */
5121 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5122 that is compensated by the subsequent overflow when subtracting
5123 one / negating. */
5125 if (abs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5126 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5127 else if (ffs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5128 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5129 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5131 tem = convert_modes (word_mode, mode, op0, 1);
5132 mode = word_mode;
5135 if (tem != 0)
5137 if (code == EQ)
5138 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5139 0, OPTAB_WIDEN);
5140 else
5141 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5144 /* If we couldn't do it that way, for NE we can "or" the two's complement
5145 of the value with itself. For EQ, we take the one's complement of
5146 that "or", which is an extra insn, so we only handle EQ if branches
5147 are expensive. */
5149 if (tem == 0 && (code == NE || BRANCH_COST > 1))
5151 if (rtx_equal_p (subtarget, op0))
5152 subtarget = 0;
5154 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5155 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5156 OPTAB_WIDEN);
5158 if (tem && code == EQ)
5159 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5163 if (tem && normalizep)
5164 tem = expand_shift (RSHIFT_EXPR, mode, tem,
5165 size_int (GET_MODE_BITSIZE (mode) - 1),
5166 subtarget, normalizep == 1);
5168 if (tem)
5170 if (GET_MODE (tem) != target_mode)
5172 convert_move (target, tem, 0);
5173 tem = target;
5175 else if (!subtarget)
5177 emit_move_insn (target, tem);
5178 tem = target;
5181 else
5182 delete_insns_since (last);
5184 return tem;
5187 /* Like emit_store_flag, but always succeeds. */
5190 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5191 enum machine_mode mode, int unsignedp, int normalizep)
5193 rtx tem, label;
5195 /* First see if emit_store_flag can do the job. */
5196 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5197 if (tem != 0)
5198 return tem;
5200 if (normalizep == 0)
5201 normalizep = 1;
5203 /* If this failed, we have to do this with set/compare/jump/set code. */
5205 if (!REG_P (target)
5206 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5207 target = gen_reg_rtx (GET_MODE (target));
5209 emit_move_insn (target, const1_rtx);
5210 label = gen_label_rtx ();
5211 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
5212 NULL_RTX, label);
5214 emit_move_insn (target, const0_rtx);
5215 emit_label (label);
5217 return target;
5220 /* Perform possibly multi-word comparison and conditional jump to LABEL
5221 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE
5223 The algorithm is based on the code in expr.c:do_jump.
5225 Note that this does not perform a general comparison. Only variants
5226 generated within expmed.c are correctly handled, others abort (but could
5227 be handled if needed). */
5229 static void
5230 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, enum machine_mode mode,
5231 rtx label)
5233 /* If this mode is an integer too wide to compare properly,
5234 compare word by word. Rely on cse to optimize constant cases. */
5236 if (GET_MODE_CLASS (mode) == MODE_INT
5237 && ! can_compare_p (op, mode, ccp_jump))
5239 rtx label2 = gen_label_rtx ();
5241 switch (op)
5243 case LTU:
5244 do_jump_by_parts_greater_rtx (mode, 1, arg2, arg1, label2, label);
5245 break;
5247 case LEU:
5248 do_jump_by_parts_greater_rtx (mode, 1, arg1, arg2, label, label2);
5249 break;
5251 case LT:
5252 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label2, label);
5253 break;
5255 case GT:
5256 do_jump_by_parts_greater_rtx (mode, 0, arg1, arg2, label2, label);
5257 break;
5259 case GE:
5260 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label, label2);
5261 break;
5263 /* do_jump_by_parts_equality_rtx compares with zero. Luckily
5264 that's the only equality operations we do */
5265 case EQ:
5266 gcc_assert (arg2 == const0_rtx && mode == GET_MODE(arg1));
5267 do_jump_by_parts_equality_rtx (arg1, label2, label);
5268 break;
5270 case NE:
5271 gcc_assert (arg2 == const0_rtx && mode == GET_MODE(arg1));
5272 do_jump_by_parts_equality_rtx (arg1, label, label2);
5273 break;
5275 default:
5276 gcc_unreachable ();
5279 emit_label (label2);
5281 else
5282 emit_cmp_and_jump_insns (arg1, arg2, op, NULL_RTX, mode, 0, label);