* config/alpha/alpha.md, arm/arm.c, darwin.c, frv/frv.md,
[official-gcc.git] / gcc / expmed.c
blobe12fdfbb103464f6eb78a03b2c842e99641992e7
1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
5 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "toplev.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "tm_p.h"
33 #include "flags.h"
34 #include "insn-config.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "real.h"
38 #include "recog.h"
39 #include "langhooks.h"
40 #include "target.h"
42 static void store_fixed_bit_field (rtx, unsigned HOST_WIDE_INT,
43 unsigned HOST_WIDE_INT,
44 unsigned HOST_WIDE_INT, rtx);
45 static void store_split_bit_field (rtx, unsigned HOST_WIDE_INT,
46 unsigned HOST_WIDE_INT, rtx);
47 static rtx extract_fixed_bit_field (enum machine_mode, rtx,
48 unsigned HOST_WIDE_INT,
49 unsigned HOST_WIDE_INT,
50 unsigned HOST_WIDE_INT, rtx, int);
51 static rtx mask_rtx (enum machine_mode, int, int, int);
52 static rtx lshift_value (enum machine_mode, rtx, int, int);
53 static rtx extract_split_bit_field (rtx, unsigned HOST_WIDE_INT,
54 unsigned HOST_WIDE_INT, int);
55 static void do_cmp_and_jump (rtx, rtx, enum rtx_code, enum machine_mode, rtx);
56 static rtx expand_smod_pow2 (enum machine_mode, rtx, HOST_WIDE_INT);
57 static rtx expand_sdiv_pow2 (enum machine_mode, rtx, HOST_WIDE_INT);
59 /* Test whether a value is zero of a power of two. */
60 #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
62 /* Nonzero means divides or modulus operations are relatively cheap for
63 powers of two, so don't use branches; emit the operation instead.
64 Usually, this will mean that the MD file will emit non-branch
65 sequences. */
67 static bool sdiv_pow2_cheap[NUM_MACHINE_MODES];
68 static bool smod_pow2_cheap[NUM_MACHINE_MODES];
70 #ifndef SLOW_UNALIGNED_ACCESS
71 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
72 #endif
74 /* For compilers that support multiple targets with different word sizes,
75 MAX_BITS_PER_WORD contains the biggest value of BITS_PER_WORD. An example
76 is the H8/300(H) compiler. */
78 #ifndef MAX_BITS_PER_WORD
79 #define MAX_BITS_PER_WORD BITS_PER_WORD
80 #endif
82 /* Reduce conditional compilation elsewhere. */
83 #ifndef HAVE_insv
84 #define HAVE_insv 0
85 #define CODE_FOR_insv CODE_FOR_nothing
86 #define gen_insv(a,b,c,d) NULL_RTX
87 #endif
88 #ifndef HAVE_extv
89 #define HAVE_extv 0
90 #define CODE_FOR_extv CODE_FOR_nothing
91 #define gen_extv(a,b,c,d) NULL_RTX
92 #endif
93 #ifndef HAVE_extzv
94 #define HAVE_extzv 0
95 #define CODE_FOR_extzv CODE_FOR_nothing
96 #define gen_extzv(a,b,c,d) NULL_RTX
97 #endif
99 /* Cost of various pieces of RTL. Note that some of these are indexed by
100 shift count and some by mode. */
101 static int zero_cost;
102 static int add_cost[NUM_MACHINE_MODES];
103 static int neg_cost[NUM_MACHINE_MODES];
104 static int shift_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
105 static int shiftadd_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
106 static int shiftsub_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
107 static int mul_cost[NUM_MACHINE_MODES];
108 static int sdiv_cost[NUM_MACHINE_MODES];
109 static int udiv_cost[NUM_MACHINE_MODES];
110 static int mul_widen_cost[NUM_MACHINE_MODES];
111 static int mul_highpart_cost[NUM_MACHINE_MODES];
113 void
114 init_expmed (void)
116 struct
118 struct rtx_def reg; rtunion reg_fld[2];
119 struct rtx_def plus; rtunion plus_fld1;
120 struct rtx_def neg;
121 struct rtx_def mult; rtunion mult_fld1;
122 struct rtx_def sdiv; rtunion sdiv_fld1;
123 struct rtx_def udiv; rtunion udiv_fld1;
124 struct rtx_def zext;
125 struct rtx_def sdiv_32; rtunion sdiv_32_fld1;
126 struct rtx_def smod_32; rtunion smod_32_fld1;
127 struct rtx_def wide_mult; rtunion wide_mult_fld1;
128 struct rtx_def wide_lshr; rtunion wide_lshr_fld1;
129 struct rtx_def wide_trunc;
130 struct rtx_def shift; rtunion shift_fld1;
131 struct rtx_def shift_mult; rtunion shift_mult_fld1;
132 struct rtx_def shift_add; rtunion shift_add_fld1;
133 struct rtx_def shift_sub; rtunion shift_sub_fld1;
134 } all;
136 rtx pow2[MAX_BITS_PER_WORD];
137 rtx cint[MAX_BITS_PER_WORD];
138 int m, n;
139 enum machine_mode mode, wider_mode;
141 zero_cost = rtx_cost (const0_rtx, 0);
143 for (m = 1; m < MAX_BITS_PER_WORD; m++)
145 pow2[m] = GEN_INT ((HOST_WIDE_INT) 1 << m);
146 cint[m] = GEN_INT (m);
149 memset (&all, 0, sizeof all);
151 PUT_CODE (&all.reg, REG);
152 /* Avoid using hard regs in ways which may be unsupported. */
153 REGNO (&all.reg) = LAST_VIRTUAL_REGISTER + 1;
155 PUT_CODE (&all.plus, PLUS);
156 XEXP (&all.plus, 0) = &all.reg;
157 XEXP (&all.plus, 1) = &all.reg;
159 PUT_CODE (&all.neg, NEG);
160 XEXP (&all.neg, 0) = &all.reg;
162 PUT_CODE (&all.mult, MULT);
163 XEXP (&all.mult, 0) = &all.reg;
164 XEXP (&all.mult, 1) = &all.reg;
166 PUT_CODE (&all.sdiv, DIV);
167 XEXP (&all.sdiv, 0) = &all.reg;
168 XEXP (&all.sdiv, 1) = &all.reg;
170 PUT_CODE (&all.udiv, UDIV);
171 XEXP (&all.udiv, 0) = &all.reg;
172 XEXP (&all.udiv, 1) = &all.reg;
174 PUT_CODE (&all.sdiv_32, DIV);
175 XEXP (&all.sdiv_32, 0) = &all.reg;
176 XEXP (&all.sdiv_32, 1) = 32 < MAX_BITS_PER_WORD ? cint[32] : GEN_INT (32);
178 PUT_CODE (&all.smod_32, MOD);
179 XEXP (&all.smod_32, 0) = &all.reg;
180 XEXP (&all.smod_32, 1) = XEXP (&all.sdiv_32, 1);
182 PUT_CODE (&all.zext, ZERO_EXTEND);
183 XEXP (&all.zext, 0) = &all.reg;
185 PUT_CODE (&all.wide_mult, MULT);
186 XEXP (&all.wide_mult, 0) = &all.zext;
187 XEXP (&all.wide_mult, 1) = &all.zext;
189 PUT_CODE (&all.wide_lshr, LSHIFTRT);
190 XEXP (&all.wide_lshr, 0) = &all.wide_mult;
192 PUT_CODE (&all.wide_trunc, TRUNCATE);
193 XEXP (&all.wide_trunc, 0) = &all.wide_lshr;
195 PUT_CODE (&all.shift, ASHIFT);
196 XEXP (&all.shift, 0) = &all.reg;
198 PUT_CODE (&all.shift_mult, MULT);
199 XEXP (&all.shift_mult, 0) = &all.reg;
201 PUT_CODE (&all.shift_add, PLUS);
202 XEXP (&all.shift_add, 0) = &all.shift_mult;
203 XEXP (&all.shift_add, 1) = &all.reg;
205 PUT_CODE (&all.shift_sub, MINUS);
206 XEXP (&all.shift_sub, 0) = &all.shift_mult;
207 XEXP (&all.shift_sub, 1) = &all.reg;
209 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
210 mode != VOIDmode;
211 mode = GET_MODE_WIDER_MODE (mode))
213 PUT_MODE (&all.reg, mode);
214 PUT_MODE (&all.plus, mode);
215 PUT_MODE (&all.neg, mode);
216 PUT_MODE (&all.mult, mode);
217 PUT_MODE (&all.sdiv, mode);
218 PUT_MODE (&all.udiv, mode);
219 PUT_MODE (&all.sdiv_32, mode);
220 PUT_MODE (&all.smod_32, mode);
221 PUT_MODE (&all.wide_trunc, mode);
222 PUT_MODE (&all.shift, mode);
223 PUT_MODE (&all.shift_mult, mode);
224 PUT_MODE (&all.shift_add, mode);
225 PUT_MODE (&all.shift_sub, mode);
227 add_cost[mode] = rtx_cost (&all.plus, SET);
228 neg_cost[mode] = rtx_cost (&all.neg, SET);
229 mul_cost[mode] = rtx_cost (&all.mult, SET);
230 sdiv_cost[mode] = rtx_cost (&all.sdiv, SET);
231 udiv_cost[mode] = rtx_cost (&all.udiv, SET);
233 sdiv_pow2_cheap[mode] = (rtx_cost (&all.sdiv_32, SET)
234 <= 2 * add_cost[mode]);
235 smod_pow2_cheap[mode] = (rtx_cost (&all.smod_32, SET)
236 <= 4 * add_cost[mode]);
238 wider_mode = GET_MODE_WIDER_MODE (mode);
239 if (wider_mode != VOIDmode)
241 PUT_MODE (&all.zext, wider_mode);
242 PUT_MODE (&all.wide_mult, wider_mode);
243 PUT_MODE (&all.wide_lshr, wider_mode);
244 XEXP (&all.wide_lshr, 1) = GEN_INT (GET_MODE_BITSIZE (mode));
246 mul_widen_cost[wider_mode] = rtx_cost (&all.wide_mult, SET);
247 mul_highpart_cost[mode] = rtx_cost (&all.wide_trunc, SET);
250 shift_cost[mode][0] = 0;
251 shiftadd_cost[mode][0] = shiftsub_cost[mode][0] = add_cost[mode];
253 n = MIN (MAX_BITS_PER_WORD, GET_MODE_BITSIZE (mode));
254 for (m = 1; m < n; m++)
256 XEXP (&all.shift, 1) = cint[m];
257 XEXP (&all.shift_mult, 1) = pow2[m];
259 shift_cost[mode][m] = rtx_cost (&all.shift, SET);
260 shiftadd_cost[mode][m] = rtx_cost (&all.shift_add, SET);
261 shiftsub_cost[mode][m] = rtx_cost (&all.shift_sub, SET);
266 /* Return an rtx representing minus the value of X.
267 MODE is the intended mode of the result,
268 useful if X is a CONST_INT. */
271 negate_rtx (enum machine_mode mode, rtx x)
273 rtx result = simplify_unary_operation (NEG, mode, x, mode);
275 if (result == 0)
276 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
278 return result;
281 /* Report on the availability of insv/extv/extzv and the desired mode
282 of each of their operands. Returns MAX_MACHINE_MODE if HAVE_foo
283 is false; else the mode of the specified operand. If OPNO is -1,
284 all the caller cares about is whether the insn is available. */
285 enum machine_mode
286 mode_for_extraction (enum extraction_pattern pattern, int opno)
288 const struct insn_data *data;
290 switch (pattern)
292 case EP_insv:
293 if (HAVE_insv)
295 data = &insn_data[CODE_FOR_insv];
296 break;
298 return MAX_MACHINE_MODE;
300 case EP_extv:
301 if (HAVE_extv)
303 data = &insn_data[CODE_FOR_extv];
304 break;
306 return MAX_MACHINE_MODE;
308 case EP_extzv:
309 if (HAVE_extzv)
311 data = &insn_data[CODE_FOR_extzv];
312 break;
314 return MAX_MACHINE_MODE;
316 default:
317 gcc_unreachable ();
320 if (opno == -1)
321 return VOIDmode;
323 /* Everyone who uses this function used to follow it with
324 if (result == VOIDmode) result = word_mode; */
325 if (data->operand[opno].mode == VOIDmode)
326 return word_mode;
327 return data->operand[opno].mode;
331 /* Generate code to store value from rtx VALUE
332 into a bit-field within structure STR_RTX
333 containing BITSIZE bits starting at bit BITNUM.
334 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
335 ALIGN is the alignment that STR_RTX is known to have.
336 TOTAL_SIZE is the size of the structure in bytes, or -1 if varying. */
338 /* ??? Note that there are two different ideas here for how
339 to determine the size to count bits within, for a register.
340 One is BITS_PER_WORD, and the other is the size of operand 3
341 of the insv pattern.
343 If operand 3 of the insv pattern is VOIDmode, then we will use BITS_PER_WORD
344 else, we use the mode of operand 3. */
347 store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
348 unsigned HOST_WIDE_INT bitnum, enum machine_mode fieldmode,
349 rtx value)
351 unsigned int unit
352 = (MEM_P (str_rtx)) ? BITS_PER_UNIT : BITS_PER_WORD;
353 unsigned HOST_WIDE_INT offset, bitpos;
354 rtx op0 = str_rtx;
355 int byte_offset;
356 rtx orig_value;
358 enum machine_mode op_mode = mode_for_extraction (EP_insv, 3);
360 while (GET_CODE (op0) == SUBREG)
362 /* The following line once was done only if WORDS_BIG_ENDIAN,
363 but I think that is a mistake. WORDS_BIG_ENDIAN is
364 meaningful at a much higher level; when structures are copied
365 between memory and regs, the higher-numbered regs
366 always get higher addresses. */
367 int inner_mode_size = GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)));
368 int outer_mode_size = GET_MODE_SIZE (GET_MODE (op0));
370 byte_offset = 0;
372 /* Paradoxical subregs need special handling on big endian machines. */
373 if (SUBREG_BYTE (op0) == 0 && inner_mode_size < outer_mode_size)
375 int difference = inner_mode_size - outer_mode_size;
377 if (WORDS_BIG_ENDIAN)
378 byte_offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
379 if (BYTES_BIG_ENDIAN)
380 byte_offset += difference % UNITS_PER_WORD;
382 else
383 byte_offset = SUBREG_BYTE (op0);
385 bitnum += byte_offset * BITS_PER_UNIT;
386 op0 = SUBREG_REG (op0);
389 /* No action is needed if the target is a register and if the field
390 lies completely outside that register. This can occur if the source
391 code contains an out-of-bounds access to a small array. */
392 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
393 return value;
395 /* Use vec_set patterns for inserting parts of vectors whenever
396 available. */
397 if (VECTOR_MODE_P (GET_MODE (op0))
398 && !MEM_P (op0)
399 && (vec_set_optab->handlers[GET_MODE (op0)].insn_code
400 != CODE_FOR_nothing)
401 && fieldmode == GET_MODE_INNER (GET_MODE (op0))
402 && bitsize == GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
403 && !(bitnum % GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
405 enum machine_mode outermode = GET_MODE (op0);
406 enum machine_mode innermode = GET_MODE_INNER (outermode);
407 int icode = (int) vec_set_optab->handlers[outermode].insn_code;
408 int pos = bitnum / GET_MODE_BITSIZE (innermode);
409 rtx rtxpos = GEN_INT (pos);
410 rtx src = value;
411 rtx dest = op0;
412 rtx pat, seq;
413 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
414 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
415 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
417 start_sequence ();
419 if (! (*insn_data[icode].operand[1].predicate) (src, mode1))
420 src = copy_to_mode_reg (mode1, src);
422 if (! (*insn_data[icode].operand[2].predicate) (rtxpos, mode2))
423 rtxpos = copy_to_mode_reg (mode1, rtxpos);
425 /* We could handle this, but we should always be called with a pseudo
426 for our targets and all insns should take them as outputs. */
427 gcc_assert ((*insn_data[icode].operand[0].predicate) (dest, mode0)
428 && (*insn_data[icode].operand[1].predicate) (src, mode1)
429 && (*insn_data[icode].operand[2].predicate) (rtxpos, mode2));
430 pat = GEN_FCN (icode) (dest, src, rtxpos);
431 seq = get_insns ();
432 end_sequence ();
433 if (pat)
435 emit_insn (seq);
436 emit_insn (pat);
437 return dest;
441 /* If the target is a register, overwriting the entire object, or storing
442 a full-word or multi-word field can be done with just a SUBREG.
444 If the target is memory, storing any naturally aligned field can be
445 done with a simple store. For targets that support fast unaligned
446 memory, any naturally sized, unit aligned field can be done directly. */
448 offset = bitnum / unit;
449 bitpos = bitnum % unit;
450 byte_offset = (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
451 + (offset * UNITS_PER_WORD);
453 if (bitpos == 0
454 && bitsize == GET_MODE_BITSIZE (fieldmode)
455 && (!MEM_P (op0)
456 ? ((GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
457 || GET_MODE_SIZE (GET_MODE (op0)) == GET_MODE_SIZE (fieldmode))
458 && byte_offset % GET_MODE_SIZE (fieldmode) == 0)
459 : (! SLOW_UNALIGNED_ACCESS (fieldmode, MEM_ALIGN (op0))
460 || (offset * BITS_PER_UNIT % bitsize == 0
461 && MEM_ALIGN (op0) % GET_MODE_BITSIZE (fieldmode) == 0))))
463 if (MEM_P (op0))
464 op0 = adjust_address (op0, fieldmode, offset);
465 else if (GET_MODE (op0) != fieldmode)
466 op0 = simplify_gen_subreg (fieldmode, op0, GET_MODE (op0),
467 byte_offset);
468 emit_move_insn (op0, value);
469 return value;
472 /* Make sure we are playing with integral modes. Pun with subregs
473 if we aren't. This must come after the entire register case above,
474 since that case is valid for any mode. The following cases are only
475 valid for integral modes. */
477 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
478 if (imode != GET_MODE (op0))
480 if (MEM_P (op0))
481 op0 = adjust_address (op0, imode, 0);
482 else
484 gcc_assert (imode != BLKmode);
485 op0 = gen_lowpart (imode, op0);
490 /* We may be accessing data outside the field, which means
491 we can alias adjacent data. */
492 if (MEM_P (op0))
494 op0 = shallow_copy_rtx (op0);
495 set_mem_alias_set (op0, 0);
496 set_mem_expr (op0, 0);
499 /* If OP0 is a register, BITPOS must count within a word.
500 But as we have it, it counts within whatever size OP0 now has.
501 On a bigendian machine, these are not the same, so convert. */
502 if (BYTES_BIG_ENDIAN
503 && !MEM_P (op0)
504 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
505 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
507 /* Storing an lsb-aligned field in a register
508 can be done with a movestrict instruction. */
510 if (!MEM_P (op0)
511 && (BYTES_BIG_ENDIAN ? bitpos + bitsize == unit : bitpos == 0)
512 && bitsize == GET_MODE_BITSIZE (fieldmode)
513 && (movstrict_optab->handlers[fieldmode].insn_code
514 != CODE_FOR_nothing))
516 int icode = movstrict_optab->handlers[fieldmode].insn_code;
518 /* Get appropriate low part of the value being stored. */
519 if (GET_CODE (value) == CONST_INT || REG_P (value))
520 value = gen_lowpart (fieldmode, value);
521 else if (!(GET_CODE (value) == SYMBOL_REF
522 || GET_CODE (value) == LABEL_REF
523 || GET_CODE (value) == CONST))
524 value = convert_to_mode (fieldmode, value, 0);
526 if (! (*insn_data[icode].operand[1].predicate) (value, fieldmode))
527 value = copy_to_mode_reg (fieldmode, value);
529 if (GET_CODE (op0) == SUBREG)
531 /* Else we've got some float mode source being extracted into
532 a different float mode destination -- this combination of
533 subregs results in Severe Tire Damage. */
534 gcc_assert (GET_MODE (SUBREG_REG (op0)) == fieldmode
535 || GET_MODE_CLASS (fieldmode) == MODE_INT
536 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
537 op0 = SUBREG_REG (op0);
540 emit_insn (GEN_FCN (icode)
541 (gen_rtx_SUBREG (fieldmode, op0,
542 (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
543 + (offset * UNITS_PER_WORD)),
544 value));
546 return value;
549 /* Handle fields bigger than a word. */
551 if (bitsize > BITS_PER_WORD)
553 /* Here we transfer the words of the field
554 in the order least significant first.
555 This is because the most significant word is the one which may
556 be less than full.
557 However, only do that if the value is not BLKmode. */
559 unsigned int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
560 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
561 unsigned int i;
563 /* This is the mode we must force value to, so that there will be enough
564 subwords to extract. Note that fieldmode will often (always?) be
565 VOIDmode, because that is what store_field uses to indicate that this
566 is a bit field, but passing VOIDmode to operand_subword_force
567 is not allowed. */
568 fieldmode = GET_MODE (value);
569 if (fieldmode == VOIDmode)
570 fieldmode = smallest_mode_for_size (nwords * BITS_PER_WORD, MODE_INT);
572 for (i = 0; i < nwords; i++)
574 /* If I is 0, use the low-order word in both field and target;
575 if I is 1, use the next to lowest word; and so on. */
576 unsigned int wordnum = (backwards ? nwords - i - 1 : i);
577 unsigned int bit_offset = (backwards
578 ? MAX ((int) bitsize - ((int) i + 1)
579 * BITS_PER_WORD,
581 : (int) i * BITS_PER_WORD);
583 store_bit_field (op0, MIN (BITS_PER_WORD,
584 bitsize - i * BITS_PER_WORD),
585 bitnum + bit_offset, word_mode,
586 operand_subword_force (value, wordnum, fieldmode));
588 return value;
591 /* From here on we can assume that the field to be stored in is
592 a full-word (whatever type that is), since it is shorter than a word. */
594 /* OFFSET is the number of words or bytes (UNIT says which)
595 from STR_RTX to the first word or byte containing part of the field. */
597 if (!MEM_P (op0))
599 if (offset != 0
600 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
602 if (!REG_P (op0))
604 /* Since this is a destination (lvalue), we can't copy
605 it to a pseudo. We can remove a SUBREG that does not
606 change the size of the operand. Such a SUBREG may
607 have been added above. */
608 gcc_assert (GET_CODE (op0) == SUBREG
609 && (GET_MODE_SIZE (GET_MODE (op0))
610 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))));
611 op0 = SUBREG_REG (op0);
613 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
614 op0, (offset * UNITS_PER_WORD));
616 offset = 0;
619 /* If VALUE has a floating-point or complex mode, access it as an
620 integer of the corresponding size. This can occur on a machine
621 with 64 bit registers that uses SFmode for float. It can also
622 occur for unaligned float or complex fields. */
623 orig_value = value;
624 if (GET_MODE (value) != VOIDmode
625 && GET_MODE_CLASS (GET_MODE (value)) != MODE_INT
626 && GET_MODE_CLASS (GET_MODE (value)) != MODE_PARTIAL_INT)
628 value = gen_reg_rtx (int_mode_for_mode (GET_MODE (value)));
629 emit_move_insn (gen_lowpart (GET_MODE (orig_value), value), orig_value);
632 /* Now OFFSET is nonzero only if OP0 is memory
633 and is therefore always measured in bytes. */
635 if (HAVE_insv
636 && GET_MODE (value) != BLKmode
637 && bitsize > 0
638 && GET_MODE_BITSIZE (op_mode) >= bitsize
639 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
640 && (bitsize + bitpos > GET_MODE_BITSIZE (op_mode)))
641 && insn_data[CODE_FOR_insv].operand[1].predicate (GEN_INT (bitsize),
642 VOIDmode))
644 int xbitpos = bitpos;
645 rtx value1;
646 rtx xop0 = op0;
647 rtx last = get_last_insn ();
648 rtx pat;
649 enum machine_mode maxmode = mode_for_extraction (EP_insv, 3);
650 int save_volatile_ok = volatile_ok;
652 volatile_ok = 1;
654 /* If this machine's insv can only insert into a register, copy OP0
655 into a register and save it back later. */
656 if (MEM_P (op0)
657 && ! ((*insn_data[(int) CODE_FOR_insv].operand[0].predicate)
658 (op0, VOIDmode)))
660 rtx tempreg;
661 enum machine_mode bestmode;
663 /* Get the mode to use for inserting into this field. If OP0 is
664 BLKmode, get the smallest mode consistent with the alignment. If
665 OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
666 mode. Otherwise, use the smallest mode containing the field. */
668 if (GET_MODE (op0) == BLKmode
669 || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
670 bestmode
671 = get_best_mode (bitsize, bitnum, MEM_ALIGN (op0), maxmode,
672 MEM_VOLATILE_P (op0));
673 else
674 bestmode = GET_MODE (op0);
676 if (bestmode == VOIDmode
677 || GET_MODE_SIZE (bestmode) < GET_MODE_SIZE (fieldmode)
678 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (op0))
679 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (op0)))
680 goto insv_loses;
682 /* Adjust address to point to the containing unit of that mode.
683 Compute offset as multiple of this unit, counting in bytes. */
684 unit = GET_MODE_BITSIZE (bestmode);
685 offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
686 bitpos = bitnum % unit;
687 op0 = adjust_address (op0, bestmode, offset);
689 /* Fetch that unit, store the bitfield in it, then store
690 the unit. */
691 tempreg = copy_to_reg (op0);
692 store_bit_field (tempreg, bitsize, bitpos, fieldmode, orig_value);
693 emit_move_insn (op0, tempreg);
694 return value;
696 volatile_ok = save_volatile_ok;
698 /* Add OFFSET into OP0's address. */
699 if (MEM_P (xop0))
700 xop0 = adjust_address (xop0, byte_mode, offset);
702 /* If xop0 is a register, we need it in MAXMODE
703 to make it acceptable to the format of insv. */
704 if (GET_CODE (xop0) == SUBREG)
705 /* We can't just change the mode, because this might clobber op0,
706 and we will need the original value of op0 if insv fails. */
707 xop0 = gen_rtx_SUBREG (maxmode, SUBREG_REG (xop0), SUBREG_BYTE (xop0));
708 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
709 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
711 /* On big-endian machines, we count bits from the most significant.
712 If the bit field insn does not, we must invert. */
714 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
715 xbitpos = unit - bitsize - xbitpos;
717 /* We have been counting XBITPOS within UNIT.
718 Count instead within the size of the register. */
719 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
720 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
722 unit = GET_MODE_BITSIZE (maxmode);
724 /* Convert VALUE to maxmode (which insv insn wants) in VALUE1. */
725 value1 = value;
726 if (GET_MODE (value) != maxmode)
728 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
730 /* Optimization: Don't bother really extending VALUE
731 if it has all the bits we will actually use. However,
732 if we must narrow it, be sure we do it correctly. */
734 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
736 rtx tmp;
738 tmp = simplify_subreg (maxmode, value1, GET_MODE (value), 0);
739 if (! tmp)
740 tmp = simplify_gen_subreg (maxmode,
741 force_reg (GET_MODE (value),
742 value1),
743 GET_MODE (value), 0);
744 value1 = tmp;
746 else
747 value1 = gen_lowpart (maxmode, value1);
749 else if (GET_CODE (value) == CONST_INT)
750 value1 = gen_int_mode (INTVAL (value), maxmode);
751 else
752 /* Parse phase is supposed to make VALUE's data type
753 match that of the component reference, which is a type
754 at least as wide as the field; so VALUE should have
755 a mode that corresponds to that type. */
756 gcc_assert (CONSTANT_P (value));
759 /* If this machine's insv insists on a register,
760 get VALUE1 into a register. */
761 if (! ((*insn_data[(int) CODE_FOR_insv].operand[3].predicate)
762 (value1, maxmode)))
763 value1 = force_reg (maxmode, value1);
765 pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
766 if (pat)
767 emit_insn (pat);
768 else
770 delete_insns_since (last);
771 store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
774 else
775 insv_loses:
776 /* Insv is not available; store using shifts and boolean ops. */
777 store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
778 return value;
781 /* Use shifts and boolean operations to store VALUE
782 into a bit field of width BITSIZE
783 in a memory location specified by OP0 except offset by OFFSET bytes.
784 (OFFSET must be 0 if OP0 is a register.)
785 The field starts at position BITPOS within the byte.
786 (If OP0 is a register, it may be a full word or a narrower mode,
787 but BITPOS still counts within a full word,
788 which is significant on bigendian machines.) */
790 static void
791 store_fixed_bit_field (rtx op0, unsigned HOST_WIDE_INT offset,
792 unsigned HOST_WIDE_INT bitsize,
793 unsigned HOST_WIDE_INT bitpos, rtx value)
795 enum machine_mode mode;
796 unsigned int total_bits = BITS_PER_WORD;
797 rtx temp;
798 int all_zero = 0;
799 int all_one = 0;
801 /* There is a case not handled here:
802 a structure with a known alignment of just a halfword
803 and a field split across two aligned halfwords within the structure.
804 Or likewise a structure with a known alignment of just a byte
805 and a field split across two bytes.
806 Such cases are not supposed to be able to occur. */
808 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
810 gcc_assert (!offset);
811 /* Special treatment for a bit field split across two registers. */
812 if (bitsize + bitpos > BITS_PER_WORD)
814 store_split_bit_field (op0, bitsize, bitpos, value);
815 return;
818 else
820 /* Get the proper mode to use for this field. We want a mode that
821 includes the entire field. If such a mode would be larger than
822 a word, we won't be doing the extraction the normal way.
823 We don't want a mode bigger than the destination. */
825 mode = GET_MODE (op0);
826 if (GET_MODE_BITSIZE (mode) == 0
827 || GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (word_mode))
828 mode = word_mode;
829 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
830 MEM_ALIGN (op0), mode, MEM_VOLATILE_P (op0));
832 if (mode == VOIDmode)
834 /* The only way this should occur is if the field spans word
835 boundaries. */
836 store_split_bit_field (op0, bitsize, bitpos + offset * BITS_PER_UNIT,
837 value);
838 return;
841 total_bits = GET_MODE_BITSIZE (mode);
843 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
844 be in the range 0 to total_bits-1, and put any excess bytes in
845 OFFSET. */
846 if (bitpos >= total_bits)
848 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
849 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
850 * BITS_PER_UNIT);
853 /* Get ref to an aligned byte, halfword, or word containing the field.
854 Adjust BITPOS to be position within a word,
855 and OFFSET to be the offset of that word.
856 Then alter OP0 to refer to that word. */
857 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
858 offset -= (offset % (total_bits / BITS_PER_UNIT));
859 op0 = adjust_address (op0, mode, offset);
862 mode = GET_MODE (op0);
864 /* Now MODE is either some integral mode for a MEM as OP0,
865 or is a full-word for a REG as OP0. TOTAL_BITS corresponds.
866 The bit field is contained entirely within OP0.
867 BITPOS is the starting bit number within OP0.
868 (OP0's mode may actually be narrower than MODE.) */
870 if (BYTES_BIG_ENDIAN)
871 /* BITPOS is the distance between our msb
872 and that of the containing datum.
873 Convert it to the distance from the lsb. */
874 bitpos = total_bits - bitsize - bitpos;
876 /* Now BITPOS is always the distance between our lsb
877 and that of OP0. */
879 /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
880 we must first convert its mode to MODE. */
882 if (GET_CODE (value) == CONST_INT)
884 HOST_WIDE_INT v = INTVAL (value);
886 if (bitsize < HOST_BITS_PER_WIDE_INT)
887 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
889 if (v == 0)
890 all_zero = 1;
891 else if ((bitsize < HOST_BITS_PER_WIDE_INT
892 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
893 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
894 all_one = 1;
896 value = lshift_value (mode, value, bitpos, bitsize);
898 else
900 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
901 && bitpos + bitsize != GET_MODE_BITSIZE (mode));
903 if (GET_MODE (value) != mode)
905 if ((REG_P (value) || GET_CODE (value) == SUBREG)
906 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
907 value = gen_lowpart (mode, value);
908 else
909 value = convert_to_mode (mode, value, 1);
912 if (must_and)
913 value = expand_binop (mode, and_optab, value,
914 mask_rtx (mode, 0, bitsize, 0),
915 NULL_RTX, 1, OPTAB_LIB_WIDEN);
916 if (bitpos > 0)
917 value = expand_shift (LSHIFT_EXPR, mode, value,
918 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
921 /* Now clear the chosen bits in OP0,
922 except that if VALUE is -1 we need not bother. */
923 /* We keep the intermediates in registers to allow CSE to combine
924 consecutive bitfield assignments. */
926 temp = force_reg (mode, op0);
928 if (! all_one)
930 temp = expand_binop (mode, and_optab, temp,
931 mask_rtx (mode, bitpos, bitsize, 1),
932 NULL_RTX, 1, OPTAB_LIB_WIDEN);
933 temp = force_reg (mode, temp);
936 /* Now logical-or VALUE into OP0, unless it is zero. */
938 if (! all_zero)
940 temp = expand_binop (mode, ior_optab, temp, value,
941 NULL_RTX, 1, OPTAB_LIB_WIDEN);
942 temp = force_reg (mode, temp);
945 if (op0 != temp)
946 emit_move_insn (op0, temp);
949 /* Store a bit field that is split across multiple accessible memory objects.
951 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
952 BITSIZE is the field width; BITPOS the position of its first bit
953 (within the word).
954 VALUE is the value to store.
956 This does not yet handle fields wider than BITS_PER_WORD. */
958 static void
959 store_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
960 unsigned HOST_WIDE_INT bitpos, rtx value)
962 unsigned int unit;
963 unsigned int bitsdone = 0;
965 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
966 much at a time. */
967 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
968 unit = BITS_PER_WORD;
969 else
970 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
972 /* If VALUE is a constant other than a CONST_INT, get it into a register in
973 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
974 that VALUE might be a floating-point constant. */
975 if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
977 rtx word = gen_lowpart_common (word_mode, value);
979 if (word && (value != word))
980 value = word;
981 else
982 value = gen_lowpart_common (word_mode,
983 force_reg (GET_MODE (value) != VOIDmode
984 ? GET_MODE (value)
985 : word_mode, value));
988 while (bitsdone < bitsize)
990 unsigned HOST_WIDE_INT thissize;
991 rtx part, word;
992 unsigned HOST_WIDE_INT thispos;
993 unsigned HOST_WIDE_INT offset;
995 offset = (bitpos + bitsdone) / unit;
996 thispos = (bitpos + bitsdone) % unit;
998 /* THISSIZE must not overrun a word boundary. Otherwise,
999 store_fixed_bit_field will call us again, and we will mutually
1000 recurse forever. */
1001 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1002 thissize = MIN (thissize, unit - thispos);
1004 if (BYTES_BIG_ENDIAN)
1006 int total_bits;
1008 /* We must do an endian conversion exactly the same way as it is
1009 done in extract_bit_field, so that the two calls to
1010 extract_fixed_bit_field will have comparable arguments. */
1011 if (!MEM_P (value) || GET_MODE (value) == BLKmode)
1012 total_bits = BITS_PER_WORD;
1013 else
1014 total_bits = GET_MODE_BITSIZE (GET_MODE (value));
1016 /* Fetch successively less significant portions. */
1017 if (GET_CODE (value) == CONST_INT)
1018 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1019 >> (bitsize - bitsdone - thissize))
1020 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1021 else
1022 /* The args are chosen so that the last part includes the
1023 lsb. Give extract_bit_field the value it needs (with
1024 endianness compensation) to fetch the piece we want. */
1025 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
1026 total_bits - bitsize + bitsdone,
1027 NULL_RTX, 1);
1029 else
1031 /* Fetch successively more significant portions. */
1032 if (GET_CODE (value) == CONST_INT)
1033 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1034 >> bitsdone)
1035 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1036 else
1037 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
1038 bitsdone, NULL_RTX, 1);
1041 /* If OP0 is a register, then handle OFFSET here.
1043 When handling multiword bitfields, extract_bit_field may pass
1044 down a word_mode SUBREG of a larger REG for a bitfield that actually
1045 crosses a word boundary. Thus, for a SUBREG, we must find
1046 the current word starting from the base register. */
1047 if (GET_CODE (op0) == SUBREG)
1049 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
1050 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1051 GET_MODE (SUBREG_REG (op0)));
1052 offset = 0;
1054 else if (REG_P (op0))
1056 word = operand_subword_force (op0, offset, GET_MODE (op0));
1057 offset = 0;
1059 else
1060 word = op0;
1062 /* OFFSET is in UNITs, and UNIT is in bits.
1063 store_fixed_bit_field wants offset in bytes. */
1064 store_fixed_bit_field (word, offset * unit / BITS_PER_UNIT, thissize,
1065 thispos, part);
1066 bitsdone += thissize;
1070 /* Generate code to extract a byte-field from STR_RTX
1071 containing BITSIZE bits, starting at BITNUM,
1072 and put it in TARGET if possible (if TARGET is nonzero).
1073 Regardless of TARGET, we return the rtx for where the value is placed.
1075 STR_RTX is the structure containing the byte (a REG or MEM).
1076 UNSIGNEDP is nonzero if this is an unsigned bit field.
1077 MODE is the natural mode of the field value once extracted.
1078 TMODE is the mode the caller would like the value to have;
1079 but the value may be returned with type MODE instead.
1081 TOTAL_SIZE is the size in bytes of the containing structure,
1082 or -1 if varying.
1084 If a TARGET is specified and we can store in it at no extra cost,
1085 we do so, and return TARGET.
1086 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1087 if they are equally easy. */
1090 extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1091 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1092 enum machine_mode mode, enum machine_mode tmode)
1094 unsigned int unit
1095 = (MEM_P (str_rtx)) ? BITS_PER_UNIT : BITS_PER_WORD;
1096 unsigned HOST_WIDE_INT offset, bitpos;
1097 rtx op0 = str_rtx;
1098 rtx spec_target = target;
1099 rtx spec_target_subreg = 0;
1100 enum machine_mode int_mode;
1101 enum machine_mode extv_mode = mode_for_extraction (EP_extv, 0);
1102 enum machine_mode extzv_mode = mode_for_extraction (EP_extzv, 0);
1103 enum machine_mode mode1;
1104 int byte_offset;
1106 if (tmode == VOIDmode)
1107 tmode = mode;
1109 while (GET_CODE (op0) == SUBREG)
1111 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1112 op0 = SUBREG_REG (op0);
1115 /* If we have an out-of-bounds access to a register, just return an
1116 uninitialized register of the required mode. This can occur if the
1117 source code contains an out-of-bounds access to a small array. */
1118 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
1119 return gen_reg_rtx (tmode);
1121 if (REG_P (op0)
1122 && mode == GET_MODE (op0)
1123 && bitnum == 0
1124 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
1126 /* We're trying to extract a full register from itself. */
1127 return op0;
1130 /* See if we can get a better vector mode before extracting. */
1131 if (VECTOR_MODE_P (GET_MODE (op0))
1132 && !MEM_P (op0)
1133 && GET_MODE_INNER (GET_MODE (op0)) != tmode)
1135 enum machine_mode new_mode;
1136 int nunits = GET_MODE_NUNITS (GET_MODE (op0));
1138 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1139 new_mode = MIN_MODE_VECTOR_FLOAT;
1140 else
1141 new_mode = MIN_MODE_VECTOR_INT;
1143 for (; new_mode != VOIDmode ; new_mode = GET_MODE_WIDER_MODE (new_mode))
1144 if (GET_MODE_NUNITS (new_mode) == nunits
1145 && GET_MODE_INNER (new_mode) == tmode
1146 && targetm.vector_mode_supported_p (new_mode))
1147 break;
1148 if (new_mode != VOIDmode)
1149 op0 = gen_lowpart (new_mode, op0);
1152 /* Use vec_extract patterns for extracting parts of vectors whenever
1153 available. */
1154 if (VECTOR_MODE_P (GET_MODE (op0))
1155 && !MEM_P (op0)
1156 && (vec_extract_optab->handlers[GET_MODE (op0)].insn_code
1157 != CODE_FOR_nothing)
1158 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
1159 == bitnum / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
1161 enum machine_mode outermode = GET_MODE (op0);
1162 enum machine_mode innermode = GET_MODE_INNER (outermode);
1163 int icode = (int) vec_extract_optab->handlers[outermode].insn_code;
1164 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1165 rtx rtxpos = GEN_INT (pos);
1166 rtx src = op0;
1167 rtx dest = NULL, pat, seq;
1168 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
1169 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
1170 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
1172 if (innermode == tmode || innermode == mode)
1173 dest = target;
1175 if (!dest)
1176 dest = gen_reg_rtx (innermode);
1178 start_sequence ();
1180 if (! (*insn_data[icode].operand[0].predicate) (dest, mode0))
1181 dest = copy_to_mode_reg (mode0, dest);
1183 if (! (*insn_data[icode].operand[1].predicate) (src, mode1))
1184 src = copy_to_mode_reg (mode1, src);
1186 if (! (*insn_data[icode].operand[2].predicate) (rtxpos, mode2))
1187 rtxpos = copy_to_mode_reg (mode1, rtxpos);
1189 /* We could handle this, but we should always be called with a pseudo
1190 for our targets and all insns should take them as outputs. */
1191 gcc_assert ((*insn_data[icode].operand[0].predicate) (dest, mode0)
1192 && (*insn_data[icode].operand[1].predicate) (src, mode1)
1193 && (*insn_data[icode].operand[2].predicate) (rtxpos, mode2));
1195 pat = GEN_FCN (icode) (dest, src, rtxpos);
1196 seq = get_insns ();
1197 end_sequence ();
1198 if (pat)
1200 emit_insn (seq);
1201 emit_insn (pat);
1202 if (mode0 != mode)
1203 return gen_lowpart (tmode, dest);
1204 return dest;
1208 /* Make sure we are playing with integral modes. Pun with subregs
1209 if we aren't. */
1211 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
1212 if (imode != GET_MODE (op0))
1214 if (MEM_P (op0))
1215 op0 = adjust_address (op0, imode, 0);
1216 else
1218 gcc_assert (imode != BLKmode);
1219 op0 = gen_lowpart (imode, op0);
1221 /* If we got a SUBREG, force it into a register since we
1222 aren't going to be able to do another SUBREG on it. */
1223 if (GET_CODE (op0) == SUBREG)
1224 op0 = force_reg (imode, op0);
1229 /* We may be accessing data outside the field, which means
1230 we can alias adjacent data. */
1231 if (MEM_P (op0))
1233 op0 = shallow_copy_rtx (op0);
1234 set_mem_alias_set (op0, 0);
1235 set_mem_expr (op0, 0);
1238 /* Extraction of a full-word or multi-word value from a structure
1239 in a register or aligned memory can be done with just a SUBREG.
1240 A subword value in the least significant part of a register
1241 can also be extracted with a SUBREG. For this, we need the
1242 byte offset of the value in op0. */
1244 bitpos = bitnum % unit;
1245 offset = bitnum / unit;
1246 byte_offset = bitpos / BITS_PER_UNIT + offset * UNITS_PER_WORD;
1248 /* If OP0 is a register, BITPOS must count within a word.
1249 But as we have it, it counts within whatever size OP0 now has.
1250 On a bigendian machine, these are not the same, so convert. */
1251 if (BYTES_BIG_ENDIAN
1252 && !MEM_P (op0)
1253 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
1254 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
1256 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1257 If that's wrong, the solution is to test for it and set TARGET to 0
1258 if needed. */
1260 /* Only scalar integer modes can be converted via subregs. There is an
1261 additional problem for FP modes here in that they can have a precision
1262 which is different from the size. mode_for_size uses precision, but
1263 we want a mode based on the size, so we must avoid calling it for FP
1264 modes. */
1265 mode1 = (SCALAR_INT_MODE_P (tmode)
1266 ? mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0)
1267 : mode);
1269 if (((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
1270 && bitpos % BITS_PER_WORD == 0)
1271 || (mode1 != BLKmode
1272 /* ??? The big endian test here is wrong. This is correct
1273 if the value is in a register, and if mode_for_size is not
1274 the same mode as op0. This causes us to get unnecessarily
1275 inefficient code from the Thumb port when -mbig-endian. */
1276 && (BYTES_BIG_ENDIAN
1277 ? bitpos + bitsize == BITS_PER_WORD
1278 : bitpos == 0)))
1279 && ((!MEM_P (op0)
1280 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1281 GET_MODE_BITSIZE (GET_MODE (op0)))
1282 && GET_MODE_SIZE (mode1) != 0
1283 && byte_offset % GET_MODE_SIZE (mode1) == 0)
1284 || (MEM_P (op0)
1285 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
1286 || (offset * BITS_PER_UNIT % bitsize == 0
1287 && MEM_ALIGN (op0) % bitsize == 0)))))
1289 if (mode1 != GET_MODE (op0))
1291 if (MEM_P (op0))
1292 op0 = adjust_address (op0, mode1, offset);
1293 else
1295 rtx sub = simplify_gen_subreg (mode1, op0, GET_MODE (op0),
1296 byte_offset);
1297 if (sub == NULL)
1298 goto no_subreg_mode_swap;
1299 op0 = sub;
1302 if (mode1 != mode)
1303 return convert_to_mode (tmode, op0, unsignedp);
1304 return op0;
1306 no_subreg_mode_swap:
1308 /* Handle fields bigger than a word. */
1310 if (bitsize > BITS_PER_WORD)
1312 /* Here we transfer the words of the field
1313 in the order least significant first.
1314 This is because the most significant word is the one which may
1315 be less than full. */
1317 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1318 unsigned int i;
1320 if (target == 0 || !REG_P (target))
1321 target = gen_reg_rtx (mode);
1323 /* Indicate for flow that the entire target reg is being set. */
1324 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
1326 for (i = 0; i < nwords; i++)
1328 /* If I is 0, use the low-order word in both field and target;
1329 if I is 1, use the next to lowest word; and so on. */
1330 /* Word number in TARGET to use. */
1331 unsigned int wordnum
1332 = (WORDS_BIG_ENDIAN
1333 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1334 : i);
1335 /* Offset from start of field in OP0. */
1336 unsigned int bit_offset = (WORDS_BIG_ENDIAN
1337 ? MAX (0, ((int) bitsize - ((int) i + 1)
1338 * (int) BITS_PER_WORD))
1339 : (int) i * BITS_PER_WORD);
1340 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1341 rtx result_part
1342 = extract_bit_field (op0, MIN (BITS_PER_WORD,
1343 bitsize - i * BITS_PER_WORD),
1344 bitnum + bit_offset, 1, target_part, mode,
1345 word_mode);
1347 gcc_assert (target_part);
1349 if (result_part != target_part)
1350 emit_move_insn (target_part, result_part);
1353 if (unsignedp)
1355 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1356 need to be zero'd out. */
1357 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1359 unsigned int i, total_words;
1361 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1362 for (i = nwords; i < total_words; i++)
1363 emit_move_insn
1364 (operand_subword (target,
1365 WORDS_BIG_ENDIAN ? total_words - i - 1 : i,
1366 1, VOIDmode),
1367 const0_rtx);
1369 return target;
1372 /* Signed bit field: sign-extend with two arithmetic shifts. */
1373 target = expand_shift (LSHIFT_EXPR, mode, target,
1374 build_int_cst (NULL_TREE,
1375 GET_MODE_BITSIZE (mode) - bitsize),
1376 NULL_RTX, 0);
1377 return expand_shift (RSHIFT_EXPR, mode, target,
1378 build_int_cst (NULL_TREE,
1379 GET_MODE_BITSIZE (mode) - bitsize),
1380 NULL_RTX, 0);
1383 /* From here on we know the desired field is smaller than a word. */
1385 /* Check if there is a correspondingly-sized integer field, so we can
1386 safely extract it as one size of integer, if necessary; then
1387 truncate or extend to the size that is wanted; then use SUBREGs or
1388 convert_to_mode to get one of the modes we really wanted. */
1390 int_mode = int_mode_for_mode (tmode);
1391 if (int_mode == BLKmode)
1392 int_mode = int_mode_for_mode (mode);
1393 /* Should probably push op0 out to memory and then do a load. */
1394 gcc_assert (int_mode != BLKmode);
1396 /* OFFSET is the number of words or bytes (UNIT says which)
1397 from STR_RTX to the first word or byte containing part of the field. */
1398 if (!MEM_P (op0))
1400 if (offset != 0
1401 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1403 if (!REG_P (op0))
1404 op0 = copy_to_reg (op0);
1405 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
1406 op0, (offset * UNITS_PER_WORD));
1408 offset = 0;
1411 /* Now OFFSET is nonzero only for memory operands. */
1413 if (unsignedp)
1415 if (HAVE_extzv
1416 && bitsize > 0
1417 && GET_MODE_BITSIZE (extzv_mode) >= bitsize
1418 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
1419 && (bitsize + bitpos > GET_MODE_BITSIZE (extzv_mode))))
1421 unsigned HOST_WIDE_INT xbitpos = bitpos, xoffset = offset;
1422 rtx bitsize_rtx, bitpos_rtx;
1423 rtx last = get_last_insn ();
1424 rtx xop0 = op0;
1425 rtx xtarget = target;
1426 rtx xspec_target = spec_target;
1427 rtx xspec_target_subreg = spec_target_subreg;
1428 rtx pat;
1429 enum machine_mode maxmode = mode_for_extraction (EP_extzv, 0);
1431 if (MEM_P (xop0))
1433 int save_volatile_ok = volatile_ok;
1434 volatile_ok = 1;
1436 /* Is the memory operand acceptable? */
1437 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[1].predicate)
1438 (xop0, GET_MODE (xop0))))
1440 /* No, load into a reg and extract from there. */
1441 enum machine_mode bestmode;
1443 /* Get the mode to use for inserting into this field. If
1444 OP0 is BLKmode, get the smallest mode consistent with the
1445 alignment. If OP0 is a non-BLKmode object that is no
1446 wider than MAXMODE, use its mode. Otherwise, use the
1447 smallest mode containing the field. */
1449 if (GET_MODE (xop0) == BLKmode
1450 || (GET_MODE_SIZE (GET_MODE (op0))
1451 > GET_MODE_SIZE (maxmode)))
1452 bestmode = get_best_mode (bitsize, bitnum,
1453 MEM_ALIGN (xop0), maxmode,
1454 MEM_VOLATILE_P (xop0));
1455 else
1456 bestmode = GET_MODE (xop0);
1458 if (bestmode == VOIDmode
1459 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (xop0))
1460 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (xop0)))
1461 goto extzv_loses;
1463 /* Compute offset as multiple of this unit,
1464 counting in bytes. */
1465 unit = GET_MODE_BITSIZE (bestmode);
1466 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1467 xbitpos = bitnum % unit;
1468 xop0 = adjust_address (xop0, bestmode, xoffset);
1470 /* Make sure register is big enough for the whole field. */
1471 if (xoffset * BITS_PER_UNIT + unit
1472 < offset * BITS_PER_UNIT + bitsize)
1473 goto extzv_loses;
1475 /* Fetch it to a register in that size. */
1476 xop0 = force_reg (bestmode, xop0);
1478 /* XBITPOS counts within UNIT, which is what is expected. */
1480 else
1481 /* Get ref to first byte containing part of the field. */
1482 xop0 = adjust_address (xop0, byte_mode, xoffset);
1484 volatile_ok = save_volatile_ok;
1487 /* If op0 is a register, we need it in MAXMODE (which is usually
1488 SImode). to make it acceptable to the format of extzv. */
1489 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1490 goto extzv_loses;
1491 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
1492 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1494 /* On big-endian machines, we count bits from the most significant.
1495 If the bit field insn does not, we must invert. */
1496 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1497 xbitpos = unit - bitsize - xbitpos;
1499 /* Now convert from counting within UNIT to counting in MAXMODE. */
1500 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
1501 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
1503 unit = GET_MODE_BITSIZE (maxmode);
1505 if (xtarget == 0)
1506 xtarget = xspec_target = gen_reg_rtx (tmode);
1508 if (GET_MODE (xtarget) != maxmode)
1510 if (REG_P (xtarget))
1512 int wider = (GET_MODE_SIZE (maxmode)
1513 > GET_MODE_SIZE (GET_MODE (xtarget)));
1514 xtarget = gen_lowpart (maxmode, xtarget);
1515 if (wider)
1516 xspec_target_subreg = xtarget;
1518 else
1519 xtarget = gen_reg_rtx (maxmode);
1522 /* If this machine's extzv insists on a register target,
1523 make sure we have one. */
1524 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[0].predicate)
1525 (xtarget, maxmode)))
1526 xtarget = gen_reg_rtx (maxmode);
1528 bitsize_rtx = GEN_INT (bitsize);
1529 bitpos_rtx = GEN_INT (xbitpos);
1531 pat = gen_extzv (xtarget, xop0, bitsize_rtx, bitpos_rtx);
1532 if (pat)
1534 emit_insn (pat);
1535 target = xtarget;
1536 spec_target = xspec_target;
1537 spec_target_subreg = xspec_target_subreg;
1539 else
1541 delete_insns_since (last);
1542 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1543 bitpos, target, 1);
1546 else
1547 extzv_loses:
1548 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1549 bitpos, target, 1);
1551 else
1553 if (HAVE_extv
1554 && bitsize > 0
1555 && GET_MODE_BITSIZE (extv_mode) >= bitsize
1556 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
1557 && (bitsize + bitpos > GET_MODE_BITSIZE (extv_mode))))
1559 int xbitpos = bitpos, xoffset = offset;
1560 rtx bitsize_rtx, bitpos_rtx;
1561 rtx last = get_last_insn ();
1562 rtx xop0 = op0, xtarget = target;
1563 rtx xspec_target = spec_target;
1564 rtx xspec_target_subreg = spec_target_subreg;
1565 rtx pat;
1566 enum machine_mode maxmode = mode_for_extraction (EP_extv, 0);
1568 if (MEM_P (xop0))
1570 /* Is the memory operand acceptable? */
1571 if (! ((*insn_data[(int) CODE_FOR_extv].operand[1].predicate)
1572 (xop0, GET_MODE (xop0))))
1574 /* No, load into a reg and extract from there. */
1575 enum machine_mode bestmode;
1577 /* Get the mode to use for inserting into this field. If
1578 OP0 is BLKmode, get the smallest mode consistent with the
1579 alignment. If OP0 is a non-BLKmode object that is no
1580 wider than MAXMODE, use its mode. Otherwise, use the
1581 smallest mode containing the field. */
1583 if (GET_MODE (xop0) == BLKmode
1584 || (GET_MODE_SIZE (GET_MODE (op0))
1585 > GET_MODE_SIZE (maxmode)))
1586 bestmode = get_best_mode (bitsize, bitnum,
1587 MEM_ALIGN (xop0), maxmode,
1588 MEM_VOLATILE_P (xop0));
1589 else
1590 bestmode = GET_MODE (xop0);
1592 if (bestmode == VOIDmode
1593 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (xop0))
1594 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (xop0)))
1595 goto extv_loses;
1597 /* Compute offset as multiple of this unit,
1598 counting in bytes. */
1599 unit = GET_MODE_BITSIZE (bestmode);
1600 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1601 xbitpos = bitnum % unit;
1602 xop0 = adjust_address (xop0, bestmode, xoffset);
1604 /* Make sure register is big enough for the whole field. */
1605 if (xoffset * BITS_PER_UNIT + unit
1606 < offset * BITS_PER_UNIT + bitsize)
1607 goto extv_loses;
1609 /* Fetch it to a register in that size. */
1610 xop0 = force_reg (bestmode, xop0);
1612 /* XBITPOS counts within UNIT, which is what is expected. */
1614 else
1615 /* Get ref to first byte containing part of the field. */
1616 xop0 = adjust_address (xop0, byte_mode, xoffset);
1619 /* If op0 is a register, we need it in MAXMODE (which is usually
1620 SImode) to make it acceptable to the format of extv. */
1621 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1622 goto extv_loses;
1623 if (REG_P (xop0) && GET_MODE (xop0) != maxmode)
1624 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1626 /* On big-endian machines, we count bits from the most significant.
1627 If the bit field insn does not, we must invert. */
1628 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1629 xbitpos = unit - bitsize - xbitpos;
1631 /* XBITPOS counts within a size of UNIT.
1632 Adjust to count within a size of MAXMODE. */
1633 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
1634 xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
1636 unit = GET_MODE_BITSIZE (maxmode);
1638 if (xtarget == 0)
1639 xtarget = xspec_target = gen_reg_rtx (tmode);
1641 if (GET_MODE (xtarget) != maxmode)
1643 if (REG_P (xtarget))
1645 int wider = (GET_MODE_SIZE (maxmode)
1646 > GET_MODE_SIZE (GET_MODE (xtarget)));
1647 xtarget = gen_lowpart (maxmode, xtarget);
1648 if (wider)
1649 xspec_target_subreg = xtarget;
1651 else
1652 xtarget = gen_reg_rtx (maxmode);
1655 /* If this machine's extv insists on a register target,
1656 make sure we have one. */
1657 if (! ((*insn_data[(int) CODE_FOR_extv].operand[0].predicate)
1658 (xtarget, maxmode)))
1659 xtarget = gen_reg_rtx (maxmode);
1661 bitsize_rtx = GEN_INT (bitsize);
1662 bitpos_rtx = GEN_INT (xbitpos);
1664 pat = gen_extv (xtarget, xop0, bitsize_rtx, bitpos_rtx);
1665 if (pat)
1667 emit_insn (pat);
1668 target = xtarget;
1669 spec_target = xspec_target;
1670 spec_target_subreg = xspec_target_subreg;
1672 else
1674 delete_insns_since (last);
1675 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1676 bitpos, target, 0);
1679 else
1680 extv_loses:
1681 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1682 bitpos, target, 0);
1684 if (target == spec_target)
1685 return target;
1686 if (target == spec_target_subreg)
1687 return spec_target;
1688 if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
1690 /* If the target mode is not a scalar integral, first convert to the
1691 integer mode of that size and then access it as a floating-point
1692 value via a SUBREG. */
1693 if (!SCALAR_INT_MODE_P (tmode))
1695 enum machine_mode smode
1696 = mode_for_size (GET_MODE_BITSIZE (tmode), MODE_INT, 0);
1697 target = convert_to_mode (smode, target, unsignedp);
1698 target = force_reg (smode, target);
1699 return gen_lowpart (tmode, target);
1702 return convert_to_mode (tmode, target, unsignedp);
1704 return target;
1707 /* Extract a bit field using shifts and boolean operations
1708 Returns an rtx to represent the value.
1709 OP0 addresses a register (word) or memory (byte).
1710 BITPOS says which bit within the word or byte the bit field starts in.
1711 OFFSET says how many bytes farther the bit field starts;
1712 it is 0 if OP0 is a register.
1713 BITSIZE says how many bits long the bit field is.
1714 (If OP0 is a register, it may be narrower than a full word,
1715 but BITPOS still counts within a full word,
1716 which is significant on bigendian machines.)
1718 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1719 If TARGET is nonzero, attempts to store the value there
1720 and return TARGET, but this is not guaranteed.
1721 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
1723 static rtx
1724 extract_fixed_bit_field (enum machine_mode tmode, rtx op0,
1725 unsigned HOST_WIDE_INT offset,
1726 unsigned HOST_WIDE_INT bitsize,
1727 unsigned HOST_WIDE_INT bitpos, rtx target,
1728 int unsignedp)
1730 unsigned int total_bits = BITS_PER_WORD;
1731 enum machine_mode mode;
1733 if (GET_CODE (op0) == SUBREG || REG_P (op0))
1735 /* Special treatment for a bit field split across two registers. */
1736 if (bitsize + bitpos > BITS_PER_WORD)
1737 return extract_split_bit_field (op0, bitsize, bitpos, unsignedp);
1739 else
1741 /* Get the proper mode to use for this field. We want a mode that
1742 includes the entire field. If such a mode would be larger than
1743 a word, we won't be doing the extraction the normal way. */
1745 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1746 MEM_ALIGN (op0), word_mode, MEM_VOLATILE_P (op0));
1748 if (mode == VOIDmode)
1749 /* The only way this should occur is if the field spans word
1750 boundaries. */
1751 return extract_split_bit_field (op0, bitsize,
1752 bitpos + offset * BITS_PER_UNIT,
1753 unsignedp);
1755 total_bits = GET_MODE_BITSIZE (mode);
1757 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
1758 be in the range 0 to total_bits-1, and put any excess bytes in
1759 OFFSET. */
1760 if (bitpos >= total_bits)
1762 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
1763 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
1764 * BITS_PER_UNIT);
1767 /* Get ref to an aligned byte, halfword, or word containing the field.
1768 Adjust BITPOS to be position within a word,
1769 and OFFSET to be the offset of that word.
1770 Then alter OP0 to refer to that word. */
1771 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1772 offset -= (offset % (total_bits / BITS_PER_UNIT));
1773 op0 = adjust_address (op0, mode, offset);
1776 mode = GET_MODE (op0);
1778 if (BYTES_BIG_ENDIAN)
1779 /* BITPOS is the distance between our msb and that of OP0.
1780 Convert it to the distance from the lsb. */
1781 bitpos = total_bits - bitsize - bitpos;
1783 /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1784 We have reduced the big-endian case to the little-endian case. */
1786 if (unsignedp)
1788 if (bitpos)
1790 /* If the field does not already start at the lsb,
1791 shift it so it does. */
1792 tree amount = build_int_cst (NULL_TREE, bitpos);
1793 /* Maybe propagate the target for the shift. */
1794 /* But not if we will return it--could confuse integrate.c. */
1795 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1796 if (tmode != mode) subtarget = 0;
1797 op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1799 /* Convert the value to the desired mode. */
1800 if (mode != tmode)
1801 op0 = convert_to_mode (tmode, op0, 1);
1803 /* Unless the msb of the field used to be the msb when we shifted,
1804 mask out the upper bits. */
1806 if (GET_MODE_BITSIZE (mode) != bitpos + bitsize)
1807 return expand_binop (GET_MODE (op0), and_optab, op0,
1808 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1809 target, 1, OPTAB_LIB_WIDEN);
1810 return op0;
1813 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1814 then arithmetic-shift its lsb to the lsb of the word. */
1815 op0 = force_reg (mode, op0);
1816 if (mode != tmode)
1817 target = 0;
1819 /* Find the narrowest integer mode that contains the field. */
1821 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1822 mode = GET_MODE_WIDER_MODE (mode))
1823 if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1825 op0 = convert_to_mode (mode, op0, 0);
1826 break;
1829 if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1831 tree amount
1832 = build_int_cst (NULL_TREE,
1833 GET_MODE_BITSIZE (mode) - (bitsize + bitpos));
1834 /* Maybe propagate the target for the shift. */
1835 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1836 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1839 return expand_shift (RSHIFT_EXPR, mode, op0,
1840 build_int_cst (NULL_TREE,
1841 GET_MODE_BITSIZE (mode) - bitsize),
1842 target, 0);
1845 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1846 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1847 complement of that if COMPLEMENT. The mask is truncated if
1848 necessary to the width of mode MODE. The mask is zero-extended if
1849 BITSIZE+BITPOS is too small for MODE. */
1851 static rtx
1852 mask_rtx (enum machine_mode mode, int bitpos, int bitsize, int complement)
1854 HOST_WIDE_INT masklow, maskhigh;
1856 if (bitsize == 0)
1857 masklow = 0;
1858 else if (bitpos < HOST_BITS_PER_WIDE_INT)
1859 masklow = (HOST_WIDE_INT) -1 << bitpos;
1860 else
1861 masklow = 0;
1863 if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1864 masklow &= ((unsigned HOST_WIDE_INT) -1
1865 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1867 if (bitpos <= HOST_BITS_PER_WIDE_INT)
1868 maskhigh = -1;
1869 else
1870 maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1872 if (bitsize == 0)
1873 maskhigh = 0;
1874 else if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
1875 maskhigh &= ((unsigned HOST_WIDE_INT) -1
1876 >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1877 else
1878 maskhigh = 0;
1880 if (complement)
1882 maskhigh = ~maskhigh;
1883 masklow = ~masklow;
1886 return immed_double_const (masklow, maskhigh, mode);
1889 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1890 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1892 static rtx
1893 lshift_value (enum machine_mode mode, rtx value, int bitpos, int bitsize)
1895 unsigned HOST_WIDE_INT v = INTVAL (value);
1896 HOST_WIDE_INT low, high;
1898 if (bitsize < HOST_BITS_PER_WIDE_INT)
1899 v &= ~((HOST_WIDE_INT) -1 << bitsize);
1901 if (bitpos < HOST_BITS_PER_WIDE_INT)
1903 low = v << bitpos;
1904 high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
1906 else
1908 low = 0;
1909 high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
1912 return immed_double_const (low, high, mode);
1915 /* Extract a bit field from a memory by forcing the alignment of the
1916 memory. This efficient only if the field spans at least 4 boundaries.
1918 OP0 is the MEM.
1919 BITSIZE is the field width; BITPOS is the position of the first bit.
1920 UNSIGNEDP is true if the result should be zero-extended. */
1922 static rtx
1923 extract_force_align_mem_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
1924 unsigned HOST_WIDE_INT bitpos,
1925 int unsignedp)
1927 enum machine_mode mode, dmode;
1928 unsigned int m_bitsize, m_size;
1929 unsigned int sign_shift_up, sign_shift_dn;
1930 rtx base, a1, a2, v1, v2, comb, shift, result, start;
1932 /* Choose a mode that will fit BITSIZE. */
1933 mode = smallest_mode_for_size (bitsize, MODE_INT);
1934 m_size = GET_MODE_SIZE (mode);
1935 m_bitsize = GET_MODE_BITSIZE (mode);
1937 /* Choose a mode twice as wide. Fail if no such mode exists. */
1938 dmode = mode_for_size (m_bitsize * 2, MODE_INT, false);
1939 if (dmode == BLKmode)
1940 return NULL;
1942 do_pending_stack_adjust ();
1943 start = get_last_insn ();
1945 /* At the end, we'll need an additional shift to deal with sign/zero
1946 extension. By default this will be a left+right shift of the
1947 appropriate size. But we may be able to eliminate one of them. */
1948 sign_shift_up = sign_shift_dn = m_bitsize - bitsize;
1950 if (STRICT_ALIGNMENT)
1952 base = plus_constant (XEXP (op0, 0), bitpos / BITS_PER_UNIT);
1953 bitpos %= BITS_PER_UNIT;
1955 /* We load two values to be concatenate. There's an edge condition
1956 that bears notice -- an aligned value at the end of a page can
1957 only load one value lest we segfault. So the two values we load
1958 are at "base & -size" and "(base + size - 1) & -size". If base
1959 is unaligned, the addresses will be aligned and sequential; if
1960 base is aligned, the addresses will both be equal to base. */
1962 a1 = expand_simple_binop (Pmode, AND, force_operand (base, NULL),
1963 GEN_INT (-(HOST_WIDE_INT)m_size),
1964 NULL, true, OPTAB_LIB_WIDEN);
1965 mark_reg_pointer (a1, m_bitsize);
1966 v1 = gen_rtx_MEM (mode, a1);
1967 set_mem_align (v1, m_bitsize);
1968 v1 = force_reg (mode, validize_mem (v1));
1970 a2 = plus_constant (base, GET_MODE_SIZE (mode) - 1);
1971 a2 = expand_simple_binop (Pmode, AND, force_operand (a2, NULL),
1972 GEN_INT (-(HOST_WIDE_INT)m_size),
1973 NULL, true, OPTAB_LIB_WIDEN);
1974 v2 = gen_rtx_MEM (mode, a2);
1975 set_mem_align (v2, m_bitsize);
1976 v2 = force_reg (mode, validize_mem (v2));
1978 /* Combine these two values into a double-word value. */
1979 if (m_bitsize == BITS_PER_WORD)
1981 comb = gen_reg_rtx (dmode);
1982 emit_insn (gen_rtx_CLOBBER (VOIDmode, comb));
1983 emit_move_insn (gen_rtx_SUBREG (mode, comb, 0), v1);
1984 emit_move_insn (gen_rtx_SUBREG (mode, comb, m_size), v2);
1986 else
1988 if (BYTES_BIG_ENDIAN)
1989 comb = v1, v1 = v2, v2 = comb;
1990 v1 = convert_modes (dmode, mode, v1, true);
1991 if (v1 == NULL)
1992 goto fail;
1993 v2 = convert_modes (dmode, mode, v2, true);
1994 v2 = expand_simple_binop (dmode, ASHIFT, v2, GEN_INT (m_bitsize),
1995 NULL, true, OPTAB_LIB_WIDEN);
1996 if (v2 == NULL)
1997 goto fail;
1998 comb = expand_simple_binop (dmode, IOR, v1, v2, NULL,
1999 true, OPTAB_LIB_WIDEN);
2000 if (comb == NULL)
2001 goto fail;
2004 shift = expand_simple_binop (Pmode, AND, base, GEN_INT (m_size - 1),
2005 NULL, true, OPTAB_LIB_WIDEN);
2006 shift = expand_mult (Pmode, shift, GEN_INT (BITS_PER_UNIT), NULL, 1);
2008 if (bitpos != 0)
2010 if (sign_shift_up <= bitpos)
2011 bitpos -= sign_shift_up, sign_shift_up = 0;
2012 shift = expand_simple_binop (Pmode, PLUS, shift, GEN_INT (bitpos),
2013 NULL, true, OPTAB_LIB_WIDEN);
2016 else
2018 unsigned HOST_WIDE_INT offset = bitpos / BITS_PER_UNIT;
2019 bitpos %= BITS_PER_UNIT;
2021 /* When strict alignment is not required, we can just load directly
2022 from memory without masking. If the remaining BITPOS offset is
2023 small enough, we may be able to do all operations in MODE as
2024 opposed to DMODE. */
2025 if (bitpos + bitsize <= m_bitsize)
2026 dmode = mode;
2027 comb = adjust_address (op0, dmode, offset);
2029 if (sign_shift_up <= bitpos)
2030 bitpos -= sign_shift_up, sign_shift_up = 0;
2031 shift = GEN_INT (bitpos);
2034 /* Shift down the double-word such that the requested value is at bit 0. */
2035 if (shift != const0_rtx)
2036 comb = expand_simple_binop (dmode, unsignedp ? LSHIFTRT : ASHIFTRT,
2037 comb, shift, NULL, unsignedp, OPTAB_LIB_WIDEN);
2038 if (comb == NULL)
2039 goto fail;
2041 /* If the field exactly matches MODE, then all we need to do is return the
2042 lowpart. Otherwise, shift to get the sign bits set properly. */
2043 result = force_reg (mode, gen_lowpart (mode, comb));
2045 if (sign_shift_up)
2046 result = expand_simple_binop (mode, ASHIFT, result,
2047 GEN_INT (sign_shift_up),
2048 NULL_RTX, 0, OPTAB_LIB_WIDEN);
2049 if (sign_shift_dn)
2050 result = expand_simple_binop (mode, unsignedp ? LSHIFTRT : ASHIFTRT,
2051 result, GEN_INT (sign_shift_dn),
2052 NULL_RTX, 0, OPTAB_LIB_WIDEN);
2054 return result;
2056 fail:
2057 delete_insns_since (start);
2058 return NULL;
2061 /* Extract a bit field that is split across two words
2062 and return an RTX for the result.
2064 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
2065 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
2066 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
2068 static rtx
2069 extract_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
2070 unsigned HOST_WIDE_INT bitpos, int unsignedp)
2072 unsigned int unit;
2073 unsigned int bitsdone = 0;
2074 rtx result = NULL_RTX;
2075 int first = 1;
2077 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
2078 much at a time. */
2079 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
2080 unit = BITS_PER_WORD;
2081 else
2083 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
2084 if (0 && bitsize / unit > 2)
2086 rtx tmp = extract_force_align_mem_bit_field (op0, bitsize, bitpos,
2087 unsignedp);
2088 if (tmp)
2089 return tmp;
2093 while (bitsdone < bitsize)
2095 unsigned HOST_WIDE_INT thissize;
2096 rtx part, word;
2097 unsigned HOST_WIDE_INT thispos;
2098 unsigned HOST_WIDE_INT offset;
2100 offset = (bitpos + bitsdone) / unit;
2101 thispos = (bitpos + bitsdone) % unit;
2103 /* THISSIZE must not overrun a word boundary. Otherwise,
2104 extract_fixed_bit_field will call us again, and we will mutually
2105 recurse forever. */
2106 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
2107 thissize = MIN (thissize, unit - thispos);
2109 /* If OP0 is a register, then handle OFFSET here.
2111 When handling multiword bitfields, extract_bit_field may pass
2112 down a word_mode SUBREG of a larger REG for a bitfield that actually
2113 crosses a word boundary. Thus, for a SUBREG, we must find
2114 the current word starting from the base register. */
2115 if (GET_CODE (op0) == SUBREG)
2117 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
2118 word = operand_subword_force (SUBREG_REG (op0), word_offset,
2119 GET_MODE (SUBREG_REG (op0)));
2120 offset = 0;
2122 else if (REG_P (op0))
2124 word = operand_subword_force (op0, offset, GET_MODE (op0));
2125 offset = 0;
2127 else
2128 word = op0;
2130 /* Extract the parts in bit-counting order,
2131 whose meaning is determined by BYTES_PER_UNIT.
2132 OFFSET is in UNITs, and UNIT is in bits.
2133 extract_fixed_bit_field wants offset in bytes. */
2134 part = extract_fixed_bit_field (word_mode, word,
2135 offset * unit / BITS_PER_UNIT,
2136 thissize, thispos, 0, 1);
2137 bitsdone += thissize;
2139 /* Shift this part into place for the result. */
2140 if (BYTES_BIG_ENDIAN)
2142 if (bitsize != bitsdone)
2143 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2144 build_int_cst (NULL_TREE, bitsize - bitsdone),
2145 0, 1);
2147 else
2149 if (bitsdone != thissize)
2150 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2151 build_int_cst (NULL_TREE,
2152 bitsdone - thissize), 0, 1);
2155 if (first)
2156 result = part;
2157 else
2158 /* Combine the parts with bitwise or. This works
2159 because we extracted each part as an unsigned bit field. */
2160 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
2161 OPTAB_LIB_WIDEN);
2163 first = 0;
2166 /* Unsigned bit field: we are done. */
2167 if (unsignedp)
2168 return result;
2169 /* Signed bit field: sign-extend with two arithmetic shifts. */
2170 result = expand_shift (LSHIFT_EXPR, word_mode, result,
2171 build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
2172 NULL_RTX, 0);
2173 return expand_shift (RSHIFT_EXPR, word_mode, result,
2174 build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
2175 NULL_RTX, 0);
2178 /* Add INC into TARGET. */
2180 void
2181 expand_inc (rtx target, rtx inc)
2183 rtx value = expand_binop (GET_MODE (target), add_optab,
2184 target, inc,
2185 target, 0, OPTAB_LIB_WIDEN);
2186 if (value != target)
2187 emit_move_insn (target, value);
2190 /* Subtract DEC from TARGET. */
2192 void
2193 expand_dec (rtx target, rtx dec)
2195 rtx value = expand_binop (GET_MODE (target), sub_optab,
2196 target, dec,
2197 target, 0, OPTAB_LIB_WIDEN);
2198 if (value != target)
2199 emit_move_insn (target, value);
2202 /* Output a shift instruction for expression code CODE,
2203 with SHIFTED being the rtx for the value to shift,
2204 and AMOUNT the tree for the amount to shift by.
2205 Store the result in the rtx TARGET, if that is convenient.
2206 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2207 Return the rtx for where the value is. */
2210 expand_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2211 tree amount, rtx target, int unsignedp)
2213 rtx op1, temp = 0;
2214 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
2215 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2216 int try;
2218 /* Previously detected shift-counts computed by NEGATE_EXPR
2219 and shifted in the other direction; but that does not work
2220 on all machines. */
2222 op1 = expand_normal (amount);
2224 if (SHIFT_COUNT_TRUNCATED)
2226 if (GET_CODE (op1) == CONST_INT
2227 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2228 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode)))
2229 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2230 % GET_MODE_BITSIZE (mode));
2231 else if (GET_CODE (op1) == SUBREG
2232 && subreg_lowpart_p (op1))
2233 op1 = SUBREG_REG (op1);
2236 if (op1 == const0_rtx)
2237 return shifted;
2239 /* Check whether its cheaper to implement a left shift by a constant
2240 bit count by a sequence of additions. */
2241 if (code == LSHIFT_EXPR
2242 && GET_CODE (op1) == CONST_INT
2243 && INTVAL (op1) > 0
2244 && INTVAL (op1) < GET_MODE_BITSIZE (mode)
2245 && INTVAL (op1) < MAX_BITS_PER_WORD
2246 && shift_cost[mode][INTVAL (op1)] > INTVAL (op1) * add_cost[mode]
2247 && shift_cost[mode][INTVAL (op1)] != MAX_COST)
2249 int i;
2250 for (i = 0; i < INTVAL (op1); i++)
2252 temp = force_reg (mode, shifted);
2253 shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2254 unsignedp, OPTAB_LIB_WIDEN);
2256 return shifted;
2259 for (try = 0; temp == 0 && try < 3; try++)
2261 enum optab_methods methods;
2263 if (try == 0)
2264 methods = OPTAB_DIRECT;
2265 else if (try == 1)
2266 methods = OPTAB_WIDEN;
2267 else
2268 methods = OPTAB_LIB_WIDEN;
2270 if (rotate)
2272 /* Widening does not work for rotation. */
2273 if (methods == OPTAB_WIDEN)
2274 continue;
2275 else if (methods == OPTAB_LIB_WIDEN)
2277 /* If we have been unable to open-code this by a rotation,
2278 do it as the IOR of two shifts. I.e., to rotate A
2279 by N bits, compute (A << N) | ((unsigned) A >> (C - N))
2280 where C is the bitsize of A.
2282 It is theoretically possible that the target machine might
2283 not be able to perform either shift and hence we would
2284 be making two libcalls rather than just the one for the
2285 shift (similarly if IOR could not be done). We will allow
2286 this extremely unlikely lossage to avoid complicating the
2287 code below. */
2289 rtx subtarget = target == shifted ? 0 : target;
2290 tree new_amount, other_amount;
2291 rtx temp1;
2292 tree type = TREE_TYPE (amount);
2293 if (GET_MODE (op1) != TYPE_MODE (type)
2294 && GET_MODE (op1) != VOIDmode)
2295 op1 = convert_to_mode (TYPE_MODE (type), op1, 1);
2296 new_amount = make_tree (type, op1);
2297 other_amount
2298 = fold_build2 (MINUS_EXPR, type,
2299 build_int_cst (type, GET_MODE_BITSIZE (mode)),
2300 new_amount);
2302 shifted = force_reg (mode, shifted);
2304 temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2305 mode, shifted, new_amount, 0, 1);
2306 temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2307 mode, shifted, other_amount, subtarget, 1);
2308 return expand_binop (mode, ior_optab, temp, temp1, target,
2309 unsignedp, methods);
2312 temp = expand_binop (mode,
2313 left ? rotl_optab : rotr_optab,
2314 shifted, op1, target, unsignedp, methods);
2316 else if (unsignedp)
2317 temp = expand_binop (mode,
2318 left ? ashl_optab : lshr_optab,
2319 shifted, op1, target, unsignedp, methods);
2321 /* Do arithmetic shifts.
2322 Also, if we are going to widen the operand, we can just as well
2323 use an arithmetic right-shift instead of a logical one. */
2324 if (temp == 0 && ! rotate
2325 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2327 enum optab_methods methods1 = methods;
2329 /* If trying to widen a log shift to an arithmetic shift,
2330 don't accept an arithmetic shift of the same size. */
2331 if (unsignedp)
2332 methods1 = OPTAB_MUST_WIDEN;
2334 /* Arithmetic shift */
2336 temp = expand_binop (mode,
2337 left ? ashl_optab : ashr_optab,
2338 shifted, op1, target, unsignedp, methods1);
2341 /* We used to try extzv here for logical right shifts, but that was
2342 only useful for one machine, the VAX, and caused poor code
2343 generation there for lshrdi3, so the code was deleted and a
2344 define_expand for lshrsi3 was added to vax.md. */
2347 gcc_assert (temp);
2348 return temp;
2351 enum alg_code {
2352 alg_unknown,
2353 alg_zero,
2354 alg_m, alg_shift,
2355 alg_add_t_m2,
2356 alg_sub_t_m2,
2357 alg_add_factor,
2358 alg_sub_factor,
2359 alg_add_t2_m,
2360 alg_sub_t2_m,
2361 alg_impossible
2364 /* This structure holds the "cost" of a multiply sequence. The
2365 "cost" field holds the total rtx_cost of every operator in the
2366 synthetic multiplication sequence, hence cost(a op b) is defined
2367 as rtx_cost(op) + cost(a) + cost(b), where cost(leaf) is zero.
2368 The "latency" field holds the minimum possible latency of the
2369 synthetic multiply, on a hypothetical infinitely parallel CPU.
2370 This is the critical path, or the maximum height, of the expression
2371 tree which is the sum of rtx_costs on the most expensive path from
2372 any leaf to the root. Hence latency(a op b) is defined as zero for
2373 leaves and rtx_cost(op) + max(latency(a), latency(b)) otherwise. */
2375 struct mult_cost {
2376 short cost; /* Total rtx_cost of the multiplication sequence. */
2377 short latency; /* The latency of the multiplication sequence. */
2380 /* This macro is used to compare a pointer to a mult_cost against an
2381 single integer "rtx_cost" value. This is equivalent to the macro
2382 CHEAPER_MULT_COST(X,Z) where Z = {Y,Y}. */
2383 #define MULT_COST_LESS(X,Y) ((X)->cost < (Y) \
2384 || ((X)->cost == (Y) && (X)->latency < (Y)))
2386 /* This macro is used to compare two pointers to mult_costs against
2387 each other. The macro returns true if X is cheaper than Y.
2388 Currently, the cheaper of two mult_costs is the one with the
2389 lower "cost". If "cost"s are tied, the lower latency is cheaper. */
2390 #define CHEAPER_MULT_COST(X,Y) ((X)->cost < (Y)->cost \
2391 || ((X)->cost == (Y)->cost \
2392 && (X)->latency < (Y)->latency))
2394 /* This structure records a sequence of operations.
2395 `ops' is the number of operations recorded.
2396 `cost' is their total cost.
2397 The operations are stored in `op' and the corresponding
2398 logarithms of the integer coefficients in `log'.
2400 These are the operations:
2401 alg_zero total := 0;
2402 alg_m total := multiplicand;
2403 alg_shift total := total * coeff
2404 alg_add_t_m2 total := total + multiplicand * coeff;
2405 alg_sub_t_m2 total := total - multiplicand * coeff;
2406 alg_add_factor total := total * coeff + total;
2407 alg_sub_factor total := total * coeff - total;
2408 alg_add_t2_m total := total * coeff + multiplicand;
2409 alg_sub_t2_m total := total * coeff - multiplicand;
2411 The first operand must be either alg_zero or alg_m. */
2413 struct algorithm
2415 struct mult_cost cost;
2416 short ops;
2417 /* The size of the OP and LOG fields are not directly related to the
2418 word size, but the worst-case algorithms will be if we have few
2419 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
2420 In that case we will generate shift-by-2, add, shift-by-2, add,...,
2421 in total wordsize operations. */
2422 enum alg_code op[MAX_BITS_PER_WORD];
2423 char log[MAX_BITS_PER_WORD];
2426 /* The entry for our multiplication cache/hash table. */
2427 struct alg_hash_entry {
2428 /* The number we are multiplying by. */
2429 unsigned HOST_WIDE_INT t;
2431 /* The mode in which we are multiplying something by T. */
2432 enum machine_mode mode;
2434 /* The best multiplication algorithm for t. */
2435 enum alg_code alg;
2437 /* The cost of multiplication if ALG_CODE is not alg_impossible.
2438 Otherwise, the cost within which multiplication by T is
2439 impossible. */
2440 struct mult_cost cost;
2443 /* The number of cache/hash entries. */
2444 #if HOST_BITS_PER_WIDE_INT == 64
2445 #define NUM_ALG_HASH_ENTRIES 1031
2446 #else
2447 #define NUM_ALG_HASH_ENTRIES 307
2448 #endif
2450 /* Each entry of ALG_HASH caches alg_code for some integer. This is
2451 actually a hash table. If we have a collision, that the older
2452 entry is kicked out. */
2453 static struct alg_hash_entry alg_hash[NUM_ALG_HASH_ENTRIES];
2455 /* Indicates the type of fixup needed after a constant multiplication.
2456 BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2457 the result should be negated, and ADD_VARIANT means that the
2458 multiplicand should be added to the result. */
2459 enum mult_variant {basic_variant, negate_variant, add_variant};
2461 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2462 const struct mult_cost *, enum machine_mode mode);
2463 static bool choose_mult_variant (enum machine_mode, HOST_WIDE_INT,
2464 struct algorithm *, enum mult_variant *, int);
2465 static rtx expand_mult_const (enum machine_mode, rtx, HOST_WIDE_INT, rtx,
2466 const struct algorithm *, enum mult_variant);
2467 static unsigned HOST_WIDE_INT choose_multiplier (unsigned HOST_WIDE_INT, int,
2468 int, rtx *, int *, int *);
2469 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2470 static rtx extract_high_half (enum machine_mode, rtx);
2471 static rtx expand_mult_highpart (enum machine_mode, rtx, rtx, rtx, int, int);
2472 static rtx expand_mult_highpart_optab (enum machine_mode, rtx, rtx, rtx,
2473 int, int);
2474 /* Compute and return the best algorithm for multiplying by T.
2475 The algorithm must cost less than cost_limit
2476 If retval.cost >= COST_LIMIT, no algorithm was found and all
2477 other field of the returned struct are undefined.
2478 MODE is the machine mode of the multiplication. */
2480 static void
2481 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2482 const struct mult_cost *cost_limit, enum machine_mode mode)
2484 int m;
2485 struct algorithm *alg_in, *best_alg;
2486 struct mult_cost best_cost;
2487 struct mult_cost new_limit;
2488 int op_cost, op_latency;
2489 unsigned HOST_WIDE_INT q;
2490 int maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (mode));
2491 int hash_index;
2492 bool cache_hit = false;
2493 enum alg_code cache_alg = alg_zero;
2495 /* Indicate that no algorithm is yet found. If no algorithm
2496 is found, this value will be returned and indicate failure. */
2497 alg_out->cost.cost = cost_limit->cost + 1;
2498 alg_out->cost.latency = cost_limit->latency + 1;
2500 if (cost_limit->cost < 0
2501 || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2502 return;
2504 /* Restrict the bits of "t" to the multiplication's mode. */
2505 t &= GET_MODE_MASK (mode);
2507 /* t == 1 can be done in zero cost. */
2508 if (t == 1)
2510 alg_out->ops = 1;
2511 alg_out->cost.cost = 0;
2512 alg_out->cost.latency = 0;
2513 alg_out->op[0] = alg_m;
2514 return;
2517 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2518 fail now. */
2519 if (t == 0)
2521 if (MULT_COST_LESS (cost_limit, zero_cost))
2522 return;
2523 else
2525 alg_out->ops = 1;
2526 alg_out->cost.cost = zero_cost;
2527 alg_out->cost.latency = zero_cost;
2528 alg_out->op[0] = alg_zero;
2529 return;
2533 /* We'll be needing a couple extra algorithm structures now. */
2535 alg_in = alloca (sizeof (struct algorithm));
2536 best_alg = alloca (sizeof (struct algorithm));
2537 best_cost = *cost_limit;
2539 /* Compute the hash index. */
2540 hash_index = (t ^ (unsigned int) mode) % NUM_ALG_HASH_ENTRIES;
2542 /* See if we already know what to do for T. */
2543 if (alg_hash[hash_index].t == t
2544 && alg_hash[hash_index].mode == mode
2545 && alg_hash[hash_index].alg != alg_unknown)
2547 cache_alg = alg_hash[hash_index].alg;
2549 if (cache_alg == alg_impossible)
2551 /* The cache tells us that it's impossible to synthesize
2552 multiplication by T within alg_hash[hash_index].cost. */
2553 if (!CHEAPER_MULT_COST (&alg_hash[hash_index].cost, cost_limit))
2554 /* COST_LIMIT is at least as restrictive as the one
2555 recorded in the hash table, in which case we have no
2556 hope of synthesizing a multiplication. Just
2557 return. */
2558 return;
2560 /* If we get here, COST_LIMIT is less restrictive than the
2561 one recorded in the hash table, so we may be able to
2562 synthesize a multiplication. Proceed as if we didn't
2563 have the cache entry. */
2565 else
2567 if (CHEAPER_MULT_COST (cost_limit, &alg_hash[hash_index].cost))
2568 /* The cached algorithm shows that this multiplication
2569 requires more cost than COST_LIMIT. Just return. This
2570 way, we don't clobber this cache entry with
2571 alg_impossible but retain useful information. */
2572 return;
2574 cache_hit = true;
2576 switch (cache_alg)
2578 case alg_shift:
2579 goto do_alg_shift;
2581 case alg_add_t_m2:
2582 case alg_sub_t_m2:
2583 goto do_alg_addsub_t_m2;
2585 case alg_add_factor:
2586 case alg_sub_factor:
2587 goto do_alg_addsub_factor;
2589 case alg_add_t2_m:
2590 goto do_alg_add_t2_m;
2592 case alg_sub_t2_m:
2593 goto do_alg_sub_t2_m;
2595 default:
2596 gcc_unreachable ();
2601 /* If we have a group of zero bits at the low-order part of T, try
2602 multiplying by the remaining bits and then doing a shift. */
2604 if ((t & 1) == 0)
2606 do_alg_shift:
2607 m = floor_log2 (t & -t); /* m = number of low zero bits */
2608 if (m < maxm)
2610 q = t >> m;
2611 /* The function expand_shift will choose between a shift and
2612 a sequence of additions, so the observed cost is given as
2613 MIN (m * add_cost[mode], shift_cost[mode][m]). */
2614 op_cost = m * add_cost[mode];
2615 if (shift_cost[mode][m] < op_cost)
2616 op_cost = shift_cost[mode][m];
2617 new_limit.cost = best_cost.cost - op_cost;
2618 new_limit.latency = best_cost.latency - op_cost;
2619 synth_mult (alg_in, q, &new_limit, mode);
2621 alg_in->cost.cost += op_cost;
2622 alg_in->cost.latency += op_cost;
2623 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2625 struct algorithm *x;
2626 best_cost = alg_in->cost;
2627 x = alg_in, alg_in = best_alg, best_alg = x;
2628 best_alg->log[best_alg->ops] = m;
2629 best_alg->op[best_alg->ops] = alg_shift;
2632 if (cache_hit)
2633 goto done;
2636 /* If we have an odd number, add or subtract one. */
2637 if ((t & 1) != 0)
2639 unsigned HOST_WIDE_INT w;
2641 do_alg_addsub_t_m2:
2642 for (w = 1; (w & t) != 0; w <<= 1)
2644 /* If T was -1, then W will be zero after the loop. This is another
2645 case where T ends with ...111. Handling this with (T + 1) and
2646 subtract 1 produces slightly better code and results in algorithm
2647 selection much faster than treating it like the ...0111 case
2648 below. */
2649 if (w == 0
2650 || (w > 2
2651 /* Reject the case where t is 3.
2652 Thus we prefer addition in that case. */
2653 && t != 3))
2655 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2657 op_cost = add_cost[mode];
2658 new_limit.cost = best_cost.cost - op_cost;
2659 new_limit.latency = best_cost.latency - op_cost;
2660 synth_mult (alg_in, t + 1, &new_limit, mode);
2662 alg_in->cost.cost += op_cost;
2663 alg_in->cost.latency += op_cost;
2664 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2666 struct algorithm *x;
2667 best_cost = alg_in->cost;
2668 x = alg_in, alg_in = best_alg, best_alg = x;
2669 best_alg->log[best_alg->ops] = 0;
2670 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2673 else
2675 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2677 op_cost = add_cost[mode];
2678 new_limit.cost = best_cost.cost - op_cost;
2679 new_limit.latency = best_cost.latency - op_cost;
2680 synth_mult (alg_in, t - 1, &new_limit, mode);
2682 alg_in->cost.cost += op_cost;
2683 alg_in->cost.latency += op_cost;
2684 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2686 struct algorithm *x;
2687 best_cost = alg_in->cost;
2688 x = alg_in, alg_in = best_alg, best_alg = x;
2689 best_alg->log[best_alg->ops] = 0;
2690 best_alg->op[best_alg->ops] = alg_add_t_m2;
2693 if (cache_hit)
2694 goto done;
2697 /* Look for factors of t of the form
2698 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2699 If we find such a factor, we can multiply by t using an algorithm that
2700 multiplies by q, shift the result by m and add/subtract it to itself.
2702 We search for large factors first and loop down, even if large factors
2703 are less probable than small; if we find a large factor we will find a
2704 good sequence quickly, and therefore be able to prune (by decreasing
2705 COST_LIMIT) the search. */
2707 do_alg_addsub_factor:
2708 for (m = floor_log2 (t - 1); m >= 2; m--)
2710 unsigned HOST_WIDE_INT d;
2712 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2713 if (t % d == 0 && t > d && m < maxm
2714 && (!cache_hit || cache_alg == alg_add_factor))
2716 /* If the target has a cheap shift-and-add instruction use
2717 that in preference to a shift insn followed by an add insn.
2718 Assume that the shift-and-add is "atomic" with a latency
2719 equal to its cost, otherwise assume that on superscalar
2720 hardware the shift may be executed concurrently with the
2721 earlier steps in the algorithm. */
2722 op_cost = add_cost[mode] + shift_cost[mode][m];
2723 if (shiftadd_cost[mode][m] < op_cost)
2725 op_cost = shiftadd_cost[mode][m];
2726 op_latency = op_cost;
2728 else
2729 op_latency = add_cost[mode];
2731 new_limit.cost = best_cost.cost - op_cost;
2732 new_limit.latency = best_cost.latency - op_latency;
2733 synth_mult (alg_in, t / d, &new_limit, mode);
2735 alg_in->cost.cost += op_cost;
2736 alg_in->cost.latency += op_latency;
2737 if (alg_in->cost.latency < op_cost)
2738 alg_in->cost.latency = op_cost;
2739 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2741 struct algorithm *x;
2742 best_cost = alg_in->cost;
2743 x = alg_in, alg_in = best_alg, best_alg = x;
2744 best_alg->log[best_alg->ops] = m;
2745 best_alg->op[best_alg->ops] = alg_add_factor;
2747 /* Other factors will have been taken care of in the recursion. */
2748 break;
2751 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2752 if (t % d == 0 && t > d && m < maxm
2753 && (!cache_hit || cache_alg == alg_sub_factor))
2755 /* If the target has a cheap shift-and-subtract insn use
2756 that in preference to a shift insn followed by a sub insn.
2757 Assume that the shift-and-sub is "atomic" with a latency
2758 equal to it's cost, otherwise assume that on superscalar
2759 hardware the shift may be executed concurrently with the
2760 earlier steps in the algorithm. */
2761 op_cost = add_cost[mode] + shift_cost[mode][m];
2762 if (shiftsub_cost[mode][m] < op_cost)
2764 op_cost = shiftsub_cost[mode][m];
2765 op_latency = op_cost;
2767 else
2768 op_latency = add_cost[mode];
2770 new_limit.cost = best_cost.cost - op_cost;
2771 new_limit.latency = best_cost.latency - op_latency;
2772 synth_mult (alg_in, t / d, &new_limit, mode);
2774 alg_in->cost.cost += op_cost;
2775 alg_in->cost.latency += op_latency;
2776 if (alg_in->cost.latency < op_cost)
2777 alg_in->cost.latency = op_cost;
2778 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2780 struct algorithm *x;
2781 best_cost = alg_in->cost;
2782 x = alg_in, alg_in = best_alg, best_alg = x;
2783 best_alg->log[best_alg->ops] = m;
2784 best_alg->op[best_alg->ops] = alg_sub_factor;
2786 break;
2789 if (cache_hit)
2790 goto done;
2792 /* Try shift-and-add (load effective address) instructions,
2793 i.e. do a*3, a*5, a*9. */
2794 if ((t & 1) != 0)
2796 do_alg_add_t2_m:
2797 q = t - 1;
2798 q = q & -q;
2799 m = exact_log2 (q);
2800 if (m >= 0 && m < maxm)
2802 op_cost = shiftadd_cost[mode][m];
2803 new_limit.cost = best_cost.cost - op_cost;
2804 new_limit.latency = best_cost.latency - op_cost;
2805 synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2807 alg_in->cost.cost += op_cost;
2808 alg_in->cost.latency += op_cost;
2809 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2811 struct algorithm *x;
2812 best_cost = alg_in->cost;
2813 x = alg_in, alg_in = best_alg, best_alg = x;
2814 best_alg->log[best_alg->ops] = m;
2815 best_alg->op[best_alg->ops] = alg_add_t2_m;
2818 if (cache_hit)
2819 goto done;
2821 do_alg_sub_t2_m:
2822 q = t + 1;
2823 q = q & -q;
2824 m = exact_log2 (q);
2825 if (m >= 0 && m < maxm)
2827 op_cost = shiftsub_cost[mode][m];
2828 new_limit.cost = best_cost.cost - op_cost;
2829 new_limit.latency = best_cost.latency - op_cost;
2830 synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2832 alg_in->cost.cost += op_cost;
2833 alg_in->cost.latency += op_cost;
2834 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2836 struct algorithm *x;
2837 best_cost = alg_in->cost;
2838 x = alg_in, alg_in = best_alg, best_alg = x;
2839 best_alg->log[best_alg->ops] = m;
2840 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2843 if (cache_hit)
2844 goto done;
2847 done:
2848 /* If best_cost has not decreased, we have not found any algorithm. */
2849 if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
2851 /* We failed to find an algorithm. Record alg_impossible for
2852 this case (that is, <T, MODE, COST_LIMIT>) so that next time
2853 we are asked to find an algorithm for T within the same or
2854 lower COST_LIMIT, we can immediately return to the
2855 caller. */
2856 alg_hash[hash_index].t = t;
2857 alg_hash[hash_index].mode = mode;
2858 alg_hash[hash_index].alg = alg_impossible;
2859 alg_hash[hash_index].cost = *cost_limit;
2860 return;
2863 /* Cache the result. */
2864 if (!cache_hit)
2866 alg_hash[hash_index].t = t;
2867 alg_hash[hash_index].mode = mode;
2868 alg_hash[hash_index].alg = best_alg->op[best_alg->ops];
2869 alg_hash[hash_index].cost.cost = best_cost.cost;
2870 alg_hash[hash_index].cost.latency = best_cost.latency;
2873 /* If we are getting a too long sequence for `struct algorithm'
2874 to record, make this search fail. */
2875 if (best_alg->ops == MAX_BITS_PER_WORD)
2876 return;
2878 /* Copy the algorithm from temporary space to the space at alg_out.
2879 We avoid using structure assignment because the majority of
2880 best_alg is normally undefined, and this is a critical function. */
2881 alg_out->ops = best_alg->ops + 1;
2882 alg_out->cost = best_cost;
2883 memcpy (alg_out->op, best_alg->op,
2884 alg_out->ops * sizeof *alg_out->op);
2885 memcpy (alg_out->log, best_alg->log,
2886 alg_out->ops * sizeof *alg_out->log);
2889 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2890 Try three variations:
2892 - a shift/add sequence based on VAL itself
2893 - a shift/add sequence based on -VAL, followed by a negation
2894 - a shift/add sequence based on VAL - 1, followed by an addition.
2896 Return true if the cheapest of these cost less than MULT_COST,
2897 describing the algorithm in *ALG and final fixup in *VARIANT. */
2899 static bool
2900 choose_mult_variant (enum machine_mode mode, HOST_WIDE_INT val,
2901 struct algorithm *alg, enum mult_variant *variant,
2902 int mult_cost)
2904 struct algorithm alg2;
2905 struct mult_cost limit;
2906 int op_cost;
2908 /* Fail quickly for impossible bounds. */
2909 if (mult_cost < 0)
2910 return false;
2912 /* Ensure that mult_cost provides a reasonable upper bound.
2913 Any constant multiplication can be performed with less
2914 than 2 * bits additions. */
2915 op_cost = 2 * GET_MODE_BITSIZE (mode) * add_cost[mode];
2916 if (mult_cost > op_cost)
2917 mult_cost = op_cost;
2919 *variant = basic_variant;
2920 limit.cost = mult_cost;
2921 limit.latency = mult_cost;
2922 synth_mult (alg, val, &limit, mode);
2924 /* This works only if the inverted value actually fits in an
2925 `unsigned int' */
2926 if (HOST_BITS_PER_INT >= GET_MODE_BITSIZE (mode))
2928 op_cost = neg_cost[mode];
2929 if (MULT_COST_LESS (&alg->cost, mult_cost))
2931 limit.cost = alg->cost.cost - op_cost;
2932 limit.latency = alg->cost.latency - op_cost;
2934 else
2936 limit.cost = mult_cost - op_cost;
2937 limit.latency = mult_cost - op_cost;
2940 synth_mult (&alg2, -val, &limit, mode);
2941 alg2.cost.cost += op_cost;
2942 alg2.cost.latency += op_cost;
2943 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2944 *alg = alg2, *variant = negate_variant;
2947 /* This proves very useful for division-by-constant. */
2948 op_cost = add_cost[mode];
2949 if (MULT_COST_LESS (&alg->cost, mult_cost))
2951 limit.cost = alg->cost.cost - op_cost;
2952 limit.latency = alg->cost.latency - op_cost;
2954 else
2956 limit.cost = mult_cost - op_cost;
2957 limit.latency = mult_cost - op_cost;
2960 synth_mult (&alg2, val - 1, &limit, mode);
2961 alg2.cost.cost += op_cost;
2962 alg2.cost.latency += op_cost;
2963 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2964 *alg = alg2, *variant = add_variant;
2966 return MULT_COST_LESS (&alg->cost, mult_cost);
2969 /* A subroutine of expand_mult, used for constant multiplications.
2970 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2971 convenient. Use the shift/add sequence described by ALG and apply
2972 the final fixup specified by VARIANT. */
2974 static rtx
2975 expand_mult_const (enum machine_mode mode, rtx op0, HOST_WIDE_INT val,
2976 rtx target, const struct algorithm *alg,
2977 enum mult_variant variant)
2979 HOST_WIDE_INT val_so_far;
2980 rtx insn, accum, tem;
2981 int opno;
2982 enum machine_mode nmode;
2984 /* Avoid referencing memory over and over and invalid sharing
2985 on SUBREGs. */
2986 op0 = force_reg (mode, op0);
2988 /* ACCUM starts out either as OP0 or as a zero, depending on
2989 the first operation. */
2991 if (alg->op[0] == alg_zero)
2993 accum = copy_to_mode_reg (mode, const0_rtx);
2994 val_so_far = 0;
2996 else if (alg->op[0] == alg_m)
2998 accum = copy_to_mode_reg (mode, op0);
2999 val_so_far = 1;
3001 else
3002 gcc_unreachable ();
3004 for (opno = 1; opno < alg->ops; opno++)
3006 int log = alg->log[opno];
3007 rtx shift_subtarget = optimize ? 0 : accum;
3008 rtx add_target
3009 = (opno == alg->ops - 1 && target != 0 && variant != add_variant
3010 && !optimize)
3011 ? target : 0;
3012 rtx accum_target = optimize ? 0 : accum;
3014 switch (alg->op[opno])
3016 case alg_shift:
3017 accum = expand_shift (LSHIFT_EXPR, mode, accum,
3018 build_int_cst (NULL_TREE, log),
3019 NULL_RTX, 0);
3020 val_so_far <<= log;
3021 break;
3023 case alg_add_t_m2:
3024 tem = expand_shift (LSHIFT_EXPR, mode, op0,
3025 build_int_cst (NULL_TREE, log),
3026 NULL_RTX, 0);
3027 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
3028 add_target ? add_target : accum_target);
3029 val_so_far += (HOST_WIDE_INT) 1 << log;
3030 break;
3032 case alg_sub_t_m2:
3033 tem = expand_shift (LSHIFT_EXPR, mode, op0,
3034 build_int_cst (NULL_TREE, log),
3035 NULL_RTX, 0);
3036 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
3037 add_target ? add_target : accum_target);
3038 val_so_far -= (HOST_WIDE_INT) 1 << log;
3039 break;
3041 case alg_add_t2_m:
3042 accum = expand_shift (LSHIFT_EXPR, mode, accum,
3043 build_int_cst (NULL_TREE, log),
3044 shift_subtarget,
3046 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
3047 add_target ? add_target : accum_target);
3048 val_so_far = (val_so_far << log) + 1;
3049 break;
3051 case alg_sub_t2_m:
3052 accum = expand_shift (LSHIFT_EXPR, mode, accum,
3053 build_int_cst (NULL_TREE, log),
3054 shift_subtarget, 0);
3055 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
3056 add_target ? add_target : accum_target);
3057 val_so_far = (val_so_far << log) - 1;
3058 break;
3060 case alg_add_factor:
3061 tem = expand_shift (LSHIFT_EXPR, mode, accum,
3062 build_int_cst (NULL_TREE, log),
3063 NULL_RTX, 0);
3064 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
3065 add_target ? add_target : accum_target);
3066 val_so_far += val_so_far << log;
3067 break;
3069 case alg_sub_factor:
3070 tem = expand_shift (LSHIFT_EXPR, mode, accum,
3071 build_int_cst (NULL_TREE, log),
3072 NULL_RTX, 0);
3073 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
3074 (add_target
3075 ? add_target : (optimize ? 0 : tem)));
3076 val_so_far = (val_so_far << log) - val_so_far;
3077 break;
3079 default:
3080 gcc_unreachable ();
3083 /* Write a REG_EQUAL note on the last insn so that we can cse
3084 multiplication sequences. Note that if ACCUM is a SUBREG,
3085 we've set the inner register and must properly indicate
3086 that. */
3088 tem = op0, nmode = mode;
3089 if (GET_CODE (accum) == SUBREG)
3091 nmode = GET_MODE (SUBREG_REG (accum));
3092 tem = gen_lowpart (nmode, op0);
3095 insn = get_last_insn ();
3096 set_unique_reg_note (insn, REG_EQUAL,
3097 gen_rtx_MULT (nmode, tem,
3098 GEN_INT (val_so_far)));
3101 if (variant == negate_variant)
3103 val_so_far = -val_so_far;
3104 accum = expand_unop (mode, neg_optab, accum, target, 0);
3106 else if (variant == add_variant)
3108 val_so_far = val_so_far + 1;
3109 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
3112 /* Compare only the bits of val and val_so_far that are significant
3113 in the result mode, to avoid sign-/zero-extension confusion. */
3114 val &= GET_MODE_MASK (mode);
3115 val_so_far &= GET_MODE_MASK (mode);
3116 gcc_assert (val == val_so_far);
3118 return accum;
3121 /* Perform a multiplication and return an rtx for the result.
3122 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3123 TARGET is a suggestion for where to store the result (an rtx).
3125 We check specially for a constant integer as OP1.
3126 If you want this check for OP0 as well, then before calling
3127 you should swap the two operands if OP0 would be constant. */
3130 expand_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3131 int unsignedp)
3133 enum mult_variant variant;
3134 struct algorithm algorithm;
3135 int max_cost;
3137 /* Handling const0_rtx here allows us to use zero as a rogue value for
3138 coeff below. */
3139 if (op1 == const0_rtx)
3140 return const0_rtx;
3141 if (op1 == const1_rtx)
3142 return op0;
3143 if (op1 == constm1_rtx)
3144 return expand_unop (mode,
3145 GET_MODE_CLASS (mode) == MODE_INT
3146 && !unsignedp && flag_trapv
3147 ? negv_optab : neg_optab,
3148 op0, target, 0);
3150 /* These are the operations that are potentially turned into a sequence
3151 of shifts and additions. */
3152 if (SCALAR_INT_MODE_P (mode)
3153 && (unsignedp || !flag_trapv))
3155 HOST_WIDE_INT coeff = 0;
3156 rtx fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3158 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3159 less than or equal in size to `unsigned int' this doesn't matter.
3160 If the mode is larger than `unsigned int', then synth_mult works
3161 only if the constant value exactly fits in an `unsigned int' without
3162 any truncation. This means that multiplying by negative values does
3163 not work; results are off by 2^32 on a 32 bit machine. */
3165 if (GET_CODE (op1) == CONST_INT)
3167 /* Attempt to handle multiplication of DImode values by negative
3168 coefficients, by performing the multiplication by a positive
3169 multiplier and then inverting the result. */
3170 if (INTVAL (op1) < 0
3171 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
3173 /* Its safe to use -INTVAL (op1) even for INT_MIN, as the
3174 result is interpreted as an unsigned coefficient.
3175 Exclude cost of op0 from max_cost to match the cost
3176 calculation of the synth_mult. */
3177 max_cost = rtx_cost (gen_rtx_MULT (mode, fake_reg, op1), SET)
3178 - neg_cost[mode];
3179 if (max_cost > 0
3180 && choose_mult_variant (mode, -INTVAL (op1), &algorithm,
3181 &variant, max_cost))
3183 rtx temp = expand_mult_const (mode, op0, -INTVAL (op1),
3184 NULL_RTX, &algorithm,
3185 variant);
3186 return expand_unop (mode, neg_optab, temp, target, 0);
3189 else coeff = INTVAL (op1);
3191 else if (GET_CODE (op1) == CONST_DOUBLE)
3193 /* If we are multiplying in DImode, it may still be a win
3194 to try to work with shifts and adds. */
3195 if (CONST_DOUBLE_HIGH (op1) == 0)
3196 coeff = CONST_DOUBLE_LOW (op1);
3197 else if (CONST_DOUBLE_LOW (op1) == 0
3198 && EXACT_POWER_OF_2_OR_ZERO_P (CONST_DOUBLE_HIGH (op1)))
3200 int shift = floor_log2 (CONST_DOUBLE_HIGH (op1))
3201 + HOST_BITS_PER_WIDE_INT;
3202 return expand_shift (LSHIFT_EXPR, mode, op0,
3203 build_int_cst (NULL_TREE, shift),
3204 target, unsignedp);
3208 /* We used to test optimize here, on the grounds that it's better to
3209 produce a smaller program when -O is not used. But this causes
3210 such a terrible slowdown sometimes that it seems better to always
3211 use synth_mult. */
3212 if (coeff != 0)
3214 /* Special case powers of two. */
3215 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3216 return expand_shift (LSHIFT_EXPR, mode, op0,
3217 build_int_cst (NULL_TREE, floor_log2 (coeff)),
3218 target, unsignedp);
3220 /* Exclude cost of op0 from max_cost to match the cost
3221 calculation of the synth_mult. */
3222 max_cost = rtx_cost (gen_rtx_MULT (mode, fake_reg, op1), SET);
3223 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3224 max_cost))
3225 return expand_mult_const (mode, op0, coeff, target,
3226 &algorithm, variant);
3230 if (GET_CODE (op0) == CONST_DOUBLE)
3232 rtx temp = op0;
3233 op0 = op1;
3234 op1 = temp;
3237 /* Expand x*2.0 as x+x. */
3238 if (GET_CODE (op1) == CONST_DOUBLE
3239 && SCALAR_FLOAT_MODE_P (mode))
3241 REAL_VALUE_TYPE d;
3242 REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
3244 if (REAL_VALUES_EQUAL (d, dconst2))
3246 op0 = force_reg (GET_MODE (op0), op0);
3247 return expand_binop (mode, add_optab, op0, op0,
3248 target, unsignedp, OPTAB_LIB_WIDEN);
3252 /* This used to use umul_optab if unsigned, but for non-widening multiply
3253 there is no difference between signed and unsigned. */
3254 op0 = expand_binop (mode,
3255 ! unsignedp
3256 && flag_trapv && (GET_MODE_CLASS(mode) == MODE_INT)
3257 ? smulv_optab : smul_optab,
3258 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
3259 gcc_assert (op0);
3260 return op0;
3263 /* Return the smallest n such that 2**n >= X. */
3266 ceil_log2 (unsigned HOST_WIDE_INT x)
3268 return floor_log2 (x - 1) + 1;
3271 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3272 replace division by D, and put the least significant N bits of the result
3273 in *MULTIPLIER_PTR and return the most significant bit.
3275 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3276 needed precision is in PRECISION (should be <= N).
3278 PRECISION should be as small as possible so this function can choose
3279 multiplier more freely.
3281 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3282 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3284 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3285 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3287 static
3288 unsigned HOST_WIDE_INT
3289 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
3290 rtx *multiplier_ptr, int *post_shift_ptr, int *lgup_ptr)
3292 HOST_WIDE_INT mhigh_hi, mlow_hi;
3293 unsigned HOST_WIDE_INT mhigh_lo, mlow_lo;
3294 int lgup, post_shift;
3295 int pow, pow2;
3296 unsigned HOST_WIDE_INT nl, dummy1;
3297 HOST_WIDE_INT nh, dummy2;
3299 /* lgup = ceil(log2(divisor)); */
3300 lgup = ceil_log2 (d);
3302 gcc_assert (lgup <= n);
3304 pow = n + lgup;
3305 pow2 = n + lgup - precision;
3307 /* We could handle this with some effort, but this case is much
3308 better handled directly with a scc insn, so rely on caller using
3309 that. */
3310 gcc_assert (pow != 2 * HOST_BITS_PER_WIDE_INT);
3312 /* mlow = 2^(N + lgup)/d */
3313 if (pow >= HOST_BITS_PER_WIDE_INT)
3315 nh = (HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
3316 nl = 0;
3318 else
3320 nh = 0;
3321 nl = (unsigned HOST_WIDE_INT) 1 << pow;
3323 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
3324 &mlow_lo, &mlow_hi, &dummy1, &dummy2);
3326 /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
3327 if (pow2 >= HOST_BITS_PER_WIDE_INT)
3328 nh |= (HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
3329 else
3330 nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
3331 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
3332 &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
3334 gcc_assert (!mhigh_hi || nh - d < d);
3335 gcc_assert (mhigh_hi <= 1 && mlow_hi <= 1);
3336 /* Assert that mlow < mhigh. */
3337 gcc_assert (mlow_hi < mhigh_hi
3338 || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo));
3340 /* If precision == N, then mlow, mhigh exceed 2^N
3341 (but they do not exceed 2^(N+1)). */
3343 /* Reduce to lowest terms. */
3344 for (post_shift = lgup; post_shift > 0; post_shift--)
3346 unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
3347 unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
3348 if (ml_lo >= mh_lo)
3349 break;
3351 mlow_hi = 0;
3352 mlow_lo = ml_lo;
3353 mhigh_hi = 0;
3354 mhigh_lo = mh_lo;
3357 *post_shift_ptr = post_shift;
3358 *lgup_ptr = lgup;
3359 if (n < HOST_BITS_PER_WIDE_INT)
3361 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
3362 *multiplier_ptr = GEN_INT (mhigh_lo & mask);
3363 return mhigh_lo >= mask;
3365 else
3367 *multiplier_ptr = GEN_INT (mhigh_lo);
3368 return mhigh_hi;
3372 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3373 congruent to 1 (mod 2**N). */
3375 static unsigned HOST_WIDE_INT
3376 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
3378 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3380 /* The algorithm notes that the choice y = x satisfies
3381 x*y == 1 mod 2^3, since x is assumed odd.
3382 Each iteration doubles the number of bits of significance in y. */
3384 unsigned HOST_WIDE_INT mask;
3385 unsigned HOST_WIDE_INT y = x;
3386 int nbit = 3;
3388 mask = (n == HOST_BITS_PER_WIDE_INT
3389 ? ~(unsigned HOST_WIDE_INT) 0
3390 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
3392 while (nbit < n)
3394 y = y * (2 - x*y) & mask; /* Modulo 2^N */
3395 nbit *= 2;
3397 return y;
3400 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3401 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3402 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3403 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3404 become signed.
3406 The result is put in TARGET if that is convenient.
3408 MODE is the mode of operation. */
3411 expand_mult_highpart_adjust (enum machine_mode mode, rtx adj_operand, rtx op0,
3412 rtx op1, rtx target, int unsignedp)
3414 rtx tem;
3415 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3417 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3418 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3419 NULL_RTX, 0);
3420 tem = expand_and (mode, tem, op1, NULL_RTX);
3421 adj_operand
3422 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3423 adj_operand);
3425 tem = expand_shift (RSHIFT_EXPR, mode, op1,
3426 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3427 NULL_RTX, 0);
3428 tem = expand_and (mode, tem, op0, NULL_RTX);
3429 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3430 target);
3432 return target;
3435 /* Subroutine of expand_mult_highpart. Return the MODE high part of OP. */
3437 static rtx
3438 extract_high_half (enum machine_mode mode, rtx op)
3440 enum machine_mode wider_mode;
3442 if (mode == word_mode)
3443 return gen_highpart (mode, op);
3445 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3447 wider_mode = GET_MODE_WIDER_MODE (mode);
3448 op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3449 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode)), 0, 1);
3450 return convert_modes (mode, wider_mode, op, 0);
3453 /* Like expand_mult_highpart, but only consider using a multiplication
3454 optab. OP1 is an rtx for the constant operand. */
3456 static rtx
3457 expand_mult_highpart_optab (enum machine_mode mode, rtx op0, rtx op1,
3458 rtx target, int unsignedp, int max_cost)
3460 rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3461 enum machine_mode wider_mode;
3462 optab moptab;
3463 rtx tem;
3464 int size;
3466 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3468 wider_mode = GET_MODE_WIDER_MODE (mode);
3469 size = GET_MODE_BITSIZE (mode);
3471 /* Firstly, try using a multiplication insn that only generates the needed
3472 high part of the product, and in the sign flavor of unsignedp. */
3473 if (mul_highpart_cost[mode] < max_cost)
3475 moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3476 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3477 unsignedp, OPTAB_DIRECT);
3478 if (tem)
3479 return tem;
3482 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3483 Need to adjust the result after the multiplication. */
3484 if (size - 1 < BITS_PER_WORD
3485 && (mul_highpart_cost[mode] + 2 * shift_cost[mode][size-1]
3486 + 4 * add_cost[mode] < max_cost))
3488 moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3489 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3490 unsignedp, OPTAB_DIRECT);
3491 if (tem)
3492 /* We used the wrong signedness. Adjust the result. */
3493 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3494 tem, unsignedp);
3497 /* Try widening multiplication. */
3498 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3499 if (moptab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3500 && mul_widen_cost[wider_mode] < max_cost)
3502 tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3503 unsignedp, OPTAB_WIDEN);
3504 if (tem)
3505 return extract_high_half (mode, tem);
3508 /* Try widening the mode and perform a non-widening multiplication. */
3509 if (smul_optab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3510 && size - 1 < BITS_PER_WORD
3511 && mul_cost[wider_mode] + shift_cost[mode][size-1] < max_cost)
3513 rtx insns, wop0, wop1;
3515 /* We need to widen the operands, for example to ensure the
3516 constant multiplier is correctly sign or zero extended.
3517 Use a sequence to clean-up any instructions emitted by
3518 the conversions if things don't work out. */
3519 start_sequence ();
3520 wop0 = convert_modes (wider_mode, mode, op0, unsignedp);
3521 wop1 = convert_modes (wider_mode, mode, op1, unsignedp);
3522 tem = expand_binop (wider_mode, smul_optab, wop0, wop1, 0,
3523 unsignedp, OPTAB_WIDEN);
3524 insns = get_insns ();
3525 end_sequence ();
3527 if (tem)
3529 emit_insn (insns);
3530 return extract_high_half (mode, tem);
3534 /* Try widening multiplication of opposite signedness, and adjust. */
3535 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3536 if (moptab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3537 && size - 1 < BITS_PER_WORD
3538 && (mul_widen_cost[wider_mode] + 2 * shift_cost[mode][size-1]
3539 + 4 * add_cost[mode] < max_cost))
3541 tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3542 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3543 if (tem != 0)
3545 tem = extract_high_half (mode, tem);
3546 /* We used the wrong signedness. Adjust the result. */
3547 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3548 target, unsignedp);
3552 return 0;
3555 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3556 putting the high half of the result in TARGET if that is convenient,
3557 and return where the result is. If the operation can not be performed,
3558 0 is returned.
3560 MODE is the mode of operation and result.
3562 UNSIGNEDP nonzero means unsigned multiply.
3564 MAX_COST is the total allowed cost for the expanded RTL. */
3566 static rtx
3567 expand_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
3568 rtx target, int unsignedp, int max_cost)
3570 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
3571 unsigned HOST_WIDE_INT cnst1;
3572 int extra_cost;
3573 bool sign_adjust = false;
3574 enum mult_variant variant;
3575 struct algorithm alg;
3576 rtx tem;
3578 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3579 /* We can't support modes wider than HOST_BITS_PER_INT. */
3580 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT);
3582 cnst1 = INTVAL (op1) & GET_MODE_MASK (mode);
3584 /* We can't optimize modes wider than BITS_PER_WORD.
3585 ??? We might be able to perform double-word arithmetic if
3586 mode == word_mode, however all the cost calculations in
3587 synth_mult etc. assume single-word operations. */
3588 if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3589 return expand_mult_highpart_optab (mode, op0, op1, target,
3590 unsignedp, max_cost);
3592 extra_cost = shift_cost[mode][GET_MODE_BITSIZE (mode) - 1];
3594 /* Check whether we try to multiply by a negative constant. */
3595 if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3597 sign_adjust = true;
3598 extra_cost += add_cost[mode];
3601 /* See whether shift/add multiplication is cheap enough. */
3602 if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3603 max_cost - extra_cost))
3605 /* See whether the specialized multiplication optabs are
3606 cheaper than the shift/add version. */
3607 tem = expand_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3608 alg.cost.cost + extra_cost);
3609 if (tem)
3610 return tem;
3612 tem = convert_to_mode (wider_mode, op0, unsignedp);
3613 tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3614 tem = extract_high_half (mode, tem);
3616 /* Adjust result for signedness. */
3617 if (sign_adjust)
3618 tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3620 return tem;
3622 return expand_mult_highpart_optab (mode, op0, op1, target,
3623 unsignedp, max_cost);
3627 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3629 static rtx
3630 expand_smod_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3632 unsigned HOST_WIDE_INT masklow, maskhigh;
3633 rtx result, temp, shift, label;
3634 int logd;
3636 logd = floor_log2 (d);
3637 result = gen_reg_rtx (mode);
3639 /* Avoid conditional branches when they're expensive. */
3640 if (BRANCH_COST >= 2
3641 && !optimize_size)
3643 rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3644 mode, 0, -1);
3645 if (signmask)
3647 signmask = force_reg (mode, signmask);
3648 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3649 shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3651 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3652 which instruction sequence to use. If logical right shifts
3653 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3654 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3656 temp = gen_rtx_LSHIFTRT (mode, result, shift);
3657 if (lshr_optab->handlers[mode].insn_code == CODE_FOR_nothing
3658 || rtx_cost (temp, SET) > COSTS_N_INSNS (2))
3660 temp = expand_binop (mode, xor_optab, op0, signmask,
3661 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3662 temp = expand_binop (mode, sub_optab, temp, signmask,
3663 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3664 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3665 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3666 temp = expand_binop (mode, xor_optab, temp, signmask,
3667 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3668 temp = expand_binop (mode, sub_optab, temp, signmask,
3669 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3671 else
3673 signmask = expand_binop (mode, lshr_optab, signmask, shift,
3674 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3675 signmask = force_reg (mode, signmask);
3677 temp = expand_binop (mode, add_optab, op0, signmask,
3678 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3679 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3680 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3681 temp = expand_binop (mode, sub_optab, temp, signmask,
3682 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3684 return temp;
3688 /* Mask contains the mode's signbit and the significant bits of the
3689 modulus. By including the signbit in the operation, many targets
3690 can avoid an explicit compare operation in the following comparison
3691 against zero. */
3693 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3694 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3696 masklow |= (HOST_WIDE_INT) -1 << (GET_MODE_BITSIZE (mode) - 1);
3697 maskhigh = -1;
3699 else
3700 maskhigh = (HOST_WIDE_INT) -1
3701 << (GET_MODE_BITSIZE (mode) - HOST_BITS_PER_WIDE_INT - 1);
3703 temp = expand_binop (mode, and_optab, op0,
3704 immed_double_const (masklow, maskhigh, mode),
3705 result, 1, OPTAB_LIB_WIDEN);
3706 if (temp != result)
3707 emit_move_insn (result, temp);
3709 label = gen_label_rtx ();
3710 do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3712 temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3713 0, OPTAB_LIB_WIDEN);
3714 masklow = (HOST_WIDE_INT) -1 << logd;
3715 maskhigh = -1;
3716 temp = expand_binop (mode, ior_optab, temp,
3717 immed_double_const (masklow, maskhigh, mode),
3718 result, 1, OPTAB_LIB_WIDEN);
3719 temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3720 0, OPTAB_LIB_WIDEN);
3721 if (temp != result)
3722 emit_move_insn (result, temp);
3723 emit_label (label);
3724 return result;
3727 /* Expand signed division of OP0 by a power of two D in mode MODE.
3728 This routine is only called for positive values of D. */
3730 static rtx
3731 expand_sdiv_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3733 rtx temp, label;
3734 tree shift;
3735 int logd;
3737 logd = floor_log2 (d);
3738 shift = build_int_cst (NULL_TREE, logd);
3740 if (d == 2 && BRANCH_COST >= 1)
3742 temp = gen_reg_rtx (mode);
3743 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3744 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3745 0, OPTAB_LIB_WIDEN);
3746 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3749 #ifdef HAVE_conditional_move
3750 if (BRANCH_COST >= 2)
3752 rtx temp2;
3754 /* ??? emit_conditional_move forces a stack adjustment via
3755 compare_from_rtx so, if the sequence is discarded, it will
3756 be lost. Do it now instead. */
3757 do_pending_stack_adjust ();
3759 start_sequence ();
3760 temp2 = copy_to_mode_reg (mode, op0);
3761 temp = expand_binop (mode, add_optab, temp2, GEN_INT (d-1),
3762 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3763 temp = force_reg (mode, temp);
3765 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3766 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3767 mode, temp, temp2, mode, 0);
3768 if (temp2)
3770 rtx seq = get_insns ();
3771 end_sequence ();
3772 emit_insn (seq);
3773 return expand_shift (RSHIFT_EXPR, mode, temp2, shift, NULL_RTX, 0);
3775 end_sequence ();
3777 #endif
3779 if (BRANCH_COST >= 2)
3781 int ushift = GET_MODE_BITSIZE (mode) - logd;
3783 temp = gen_reg_rtx (mode);
3784 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3785 if (shift_cost[mode][ushift] > COSTS_N_INSNS (1))
3786 temp = expand_binop (mode, and_optab, temp, GEN_INT (d - 1),
3787 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3788 else
3789 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3790 build_int_cst (NULL_TREE, ushift),
3791 NULL_RTX, 1);
3792 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3793 0, OPTAB_LIB_WIDEN);
3794 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3797 label = gen_label_rtx ();
3798 temp = copy_to_mode_reg (mode, op0);
3799 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3800 expand_inc (temp, GEN_INT (d - 1));
3801 emit_label (label);
3802 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3805 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3806 if that is convenient, and returning where the result is.
3807 You may request either the quotient or the remainder as the result;
3808 specify REM_FLAG nonzero to get the remainder.
3810 CODE is the expression code for which kind of division this is;
3811 it controls how rounding is done. MODE is the machine mode to use.
3812 UNSIGNEDP nonzero means do unsigned division. */
3814 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3815 and then correct it by or'ing in missing high bits
3816 if result of ANDI is nonzero.
3817 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3818 This could optimize to a bfexts instruction.
3819 But C doesn't use these operations, so their optimizations are
3820 left for later. */
3821 /* ??? For modulo, we don't actually need the highpart of the first product,
3822 the low part will do nicely. And for small divisors, the second multiply
3823 can also be a low-part only multiply or even be completely left out.
3824 E.g. to calculate the remainder of a division by 3 with a 32 bit
3825 multiply, multiply with 0x55555556 and extract the upper two bits;
3826 the result is exact for inputs up to 0x1fffffff.
3827 The input range can be reduced by using cross-sum rules.
3828 For odd divisors >= 3, the following table gives right shift counts
3829 so that if a number is shifted by an integer multiple of the given
3830 amount, the remainder stays the same:
3831 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3832 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3833 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3834 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3835 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3837 Cross-sum rules for even numbers can be derived by leaving as many bits
3838 to the right alone as the divisor has zeros to the right.
3839 E.g. if x is an unsigned 32 bit number:
3840 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3844 expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
3845 rtx op0, rtx op1, rtx target, int unsignedp)
3847 enum machine_mode compute_mode;
3848 rtx tquotient;
3849 rtx quotient = 0, remainder = 0;
3850 rtx last;
3851 int size;
3852 rtx insn, set;
3853 optab optab1, optab2;
3854 int op1_is_constant, op1_is_pow2 = 0;
3855 int max_cost, extra_cost;
3856 static HOST_WIDE_INT last_div_const = 0;
3857 static HOST_WIDE_INT ext_op1;
3859 op1_is_constant = GET_CODE (op1) == CONST_INT;
3860 if (op1_is_constant)
3862 ext_op1 = INTVAL (op1);
3863 if (unsignedp)
3864 ext_op1 &= GET_MODE_MASK (mode);
3865 op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
3866 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
3870 This is the structure of expand_divmod:
3872 First comes code to fix up the operands so we can perform the operations
3873 correctly and efficiently.
3875 Second comes a switch statement with code specific for each rounding mode.
3876 For some special operands this code emits all RTL for the desired
3877 operation, for other cases, it generates only a quotient and stores it in
3878 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3879 to indicate that it has not done anything.
3881 Last comes code that finishes the operation. If QUOTIENT is set and
3882 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3883 QUOTIENT is not set, it is computed using trunc rounding.
3885 We try to generate special code for division and remainder when OP1 is a
3886 constant. If |OP1| = 2**n we can use shifts and some other fast
3887 operations. For other values of OP1, we compute a carefully selected
3888 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3889 by m.
3891 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3892 half of the product. Different strategies for generating the product are
3893 implemented in expand_mult_highpart.
3895 If what we actually want is the remainder, we generate that by another
3896 by-constant multiplication and a subtraction. */
3898 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3899 code below will malfunction if we are, so check here and handle
3900 the special case if so. */
3901 if (op1 == const1_rtx)
3902 return rem_flag ? const0_rtx : op0;
3904 /* When dividing by -1, we could get an overflow.
3905 negv_optab can handle overflows. */
3906 if (! unsignedp && op1 == constm1_rtx)
3908 if (rem_flag)
3909 return const0_rtx;
3910 return expand_unop (mode, flag_trapv && GET_MODE_CLASS(mode) == MODE_INT
3911 ? negv_optab : neg_optab, op0, target, 0);
3914 if (target
3915 /* Don't use the function value register as a target
3916 since we have to read it as well as write it,
3917 and function-inlining gets confused by this. */
3918 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3919 /* Don't clobber an operand while doing a multi-step calculation. */
3920 || ((rem_flag || op1_is_constant)
3921 && (reg_mentioned_p (target, op0)
3922 || (MEM_P (op0) && MEM_P (target))))
3923 || reg_mentioned_p (target, op1)
3924 || (MEM_P (op1) && MEM_P (target))))
3925 target = 0;
3927 /* Get the mode in which to perform this computation. Normally it will
3928 be MODE, but sometimes we can't do the desired operation in MODE.
3929 If so, pick a wider mode in which we can do the operation. Convert
3930 to that mode at the start to avoid repeated conversions.
3932 First see what operations we need. These depend on the expression
3933 we are evaluating. (We assume that divxx3 insns exist under the
3934 same conditions that modxx3 insns and that these insns don't normally
3935 fail. If these assumptions are not correct, we may generate less
3936 efficient code in some cases.)
3938 Then see if we find a mode in which we can open-code that operation
3939 (either a division, modulus, or shift). Finally, check for the smallest
3940 mode for which we can do the operation with a library call. */
3942 /* We might want to refine this now that we have division-by-constant
3943 optimization. Since expand_mult_highpart tries so many variants, it is
3944 not straightforward to generalize this. Maybe we should make an array
3945 of possible modes in init_expmed? Save this for GCC 2.7. */
3947 optab1 = ((op1_is_pow2 && op1 != const0_rtx)
3948 ? (unsignedp ? lshr_optab : ashr_optab)
3949 : (unsignedp ? udiv_optab : sdiv_optab));
3950 optab2 = ((op1_is_pow2 && op1 != const0_rtx)
3951 ? optab1
3952 : (unsignedp ? udivmod_optab : sdivmod_optab));
3954 for (compute_mode = mode; compute_mode != VOIDmode;
3955 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3956 if (optab1->handlers[compute_mode].insn_code != CODE_FOR_nothing
3957 || optab2->handlers[compute_mode].insn_code != CODE_FOR_nothing)
3958 break;
3960 if (compute_mode == VOIDmode)
3961 for (compute_mode = mode; compute_mode != VOIDmode;
3962 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3963 if (optab1->handlers[compute_mode].libfunc
3964 || optab2->handlers[compute_mode].libfunc)
3965 break;
3967 /* If we still couldn't find a mode, use MODE, but expand_binop will
3968 probably die. */
3969 if (compute_mode == VOIDmode)
3970 compute_mode = mode;
3972 if (target && GET_MODE (target) == compute_mode)
3973 tquotient = target;
3974 else
3975 tquotient = gen_reg_rtx (compute_mode);
3977 size = GET_MODE_BITSIZE (compute_mode);
3978 #if 0
3979 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3980 (mode), and thereby get better code when OP1 is a constant. Do that
3981 later. It will require going over all usages of SIZE below. */
3982 size = GET_MODE_BITSIZE (mode);
3983 #endif
3985 /* Only deduct something for a REM if the last divide done was
3986 for a different constant. Then set the constant of the last
3987 divide. */
3988 max_cost = unsignedp ? udiv_cost[compute_mode] : sdiv_cost[compute_mode];
3989 if (rem_flag && ! (last_div_const != 0 && op1_is_constant
3990 && INTVAL (op1) == last_div_const))
3991 max_cost -= mul_cost[compute_mode] + add_cost[compute_mode];
3993 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
3995 /* Now convert to the best mode to use. */
3996 if (compute_mode != mode)
3998 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
3999 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
4001 /* convert_modes may have placed op1 into a register, so we
4002 must recompute the following. */
4003 op1_is_constant = GET_CODE (op1) == CONST_INT;
4004 op1_is_pow2 = (op1_is_constant
4005 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4006 || (! unsignedp
4007 && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
4010 /* If one of the operands is a volatile MEM, copy it into a register. */
4012 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
4013 op0 = force_reg (compute_mode, op0);
4014 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
4015 op1 = force_reg (compute_mode, op1);
4017 /* If we need the remainder or if OP1 is constant, we need to
4018 put OP0 in a register in case it has any queued subexpressions. */
4019 if (rem_flag || op1_is_constant)
4020 op0 = force_reg (compute_mode, op0);
4022 last = get_last_insn ();
4024 /* Promote floor rounding to trunc rounding for unsigned operations. */
4025 if (unsignedp)
4027 if (code == FLOOR_DIV_EXPR)
4028 code = TRUNC_DIV_EXPR;
4029 if (code == FLOOR_MOD_EXPR)
4030 code = TRUNC_MOD_EXPR;
4031 if (code == EXACT_DIV_EXPR && op1_is_pow2)
4032 code = TRUNC_DIV_EXPR;
4035 if (op1 != const0_rtx)
4036 switch (code)
4038 case TRUNC_MOD_EXPR:
4039 case TRUNC_DIV_EXPR:
4040 if (op1_is_constant)
4042 if (unsignedp)
4044 unsigned HOST_WIDE_INT mh;
4045 int pre_shift, post_shift;
4046 int dummy;
4047 rtx ml;
4048 unsigned HOST_WIDE_INT d = (INTVAL (op1)
4049 & GET_MODE_MASK (compute_mode));
4051 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4053 pre_shift = floor_log2 (d);
4054 if (rem_flag)
4056 remainder
4057 = expand_binop (compute_mode, and_optab, op0,
4058 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4059 remainder, 1,
4060 OPTAB_LIB_WIDEN);
4061 if (remainder)
4062 return gen_lowpart (mode, remainder);
4064 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4065 build_int_cst (NULL_TREE,
4066 pre_shift),
4067 tquotient, 1);
4069 else if (size <= HOST_BITS_PER_WIDE_INT)
4071 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
4073 /* Most significant bit of divisor is set; emit an scc
4074 insn. */
4075 quotient = emit_store_flag (tquotient, GEU, op0, op1,
4076 compute_mode, 1, 1);
4077 if (quotient == 0)
4078 goto fail1;
4080 else
4082 /* Find a suitable multiplier and right shift count
4083 instead of multiplying with D. */
4085 mh = choose_multiplier (d, size, size,
4086 &ml, &post_shift, &dummy);
4088 /* If the suggested multiplier is more than SIZE bits,
4089 we can do better for even divisors, using an
4090 initial right shift. */
4091 if (mh != 0 && (d & 1) == 0)
4093 pre_shift = floor_log2 (d & -d);
4094 mh = choose_multiplier (d >> pre_shift, size,
4095 size - pre_shift,
4096 &ml, &post_shift, &dummy);
4097 gcc_assert (!mh);
4099 else
4100 pre_shift = 0;
4102 if (mh != 0)
4104 rtx t1, t2, t3, t4;
4106 if (post_shift - 1 >= BITS_PER_WORD)
4107 goto fail1;
4109 extra_cost
4110 = (shift_cost[compute_mode][post_shift - 1]
4111 + shift_cost[compute_mode][1]
4112 + 2 * add_cost[compute_mode]);
4113 t1 = expand_mult_highpart (compute_mode, op0, ml,
4114 NULL_RTX, 1,
4115 max_cost - extra_cost);
4116 if (t1 == 0)
4117 goto fail1;
4118 t2 = force_operand (gen_rtx_MINUS (compute_mode,
4119 op0, t1),
4120 NULL_RTX);
4121 t3 = expand_shift
4122 (RSHIFT_EXPR, compute_mode, t2,
4123 build_int_cst (NULL_TREE, 1),
4124 NULL_RTX,1);
4125 t4 = force_operand (gen_rtx_PLUS (compute_mode,
4126 t1, t3),
4127 NULL_RTX);
4128 quotient = expand_shift
4129 (RSHIFT_EXPR, compute_mode, t4,
4130 build_int_cst (NULL_TREE, post_shift - 1),
4131 tquotient, 1);
4133 else
4135 rtx t1, t2;
4137 if (pre_shift >= BITS_PER_WORD
4138 || post_shift >= BITS_PER_WORD)
4139 goto fail1;
4141 t1 = expand_shift
4142 (RSHIFT_EXPR, compute_mode, op0,
4143 build_int_cst (NULL_TREE, pre_shift),
4144 NULL_RTX, 1);
4145 extra_cost
4146 = (shift_cost[compute_mode][pre_shift]
4147 + shift_cost[compute_mode][post_shift]);
4148 t2 = expand_mult_highpart (compute_mode, t1, ml,
4149 NULL_RTX, 1,
4150 max_cost - extra_cost);
4151 if (t2 == 0)
4152 goto fail1;
4153 quotient = expand_shift
4154 (RSHIFT_EXPR, compute_mode, t2,
4155 build_int_cst (NULL_TREE, post_shift),
4156 tquotient, 1);
4160 else /* Too wide mode to use tricky code */
4161 break;
4163 insn = get_last_insn ();
4164 if (insn != last
4165 && (set = single_set (insn)) != 0
4166 && SET_DEST (set) == quotient)
4167 set_unique_reg_note (insn,
4168 REG_EQUAL,
4169 gen_rtx_UDIV (compute_mode, op0, op1));
4171 else /* TRUNC_DIV, signed */
4173 unsigned HOST_WIDE_INT ml;
4174 int lgup, post_shift;
4175 rtx mlr;
4176 HOST_WIDE_INT d = INTVAL (op1);
4177 unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
4179 /* n rem d = n rem -d */
4180 if (rem_flag && d < 0)
4182 d = abs_d;
4183 op1 = gen_int_mode (abs_d, compute_mode);
4186 if (d == 1)
4187 quotient = op0;
4188 else if (d == -1)
4189 quotient = expand_unop (compute_mode, neg_optab, op0,
4190 tquotient, 0);
4191 else if (abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
4193 /* This case is not handled correctly below. */
4194 quotient = emit_store_flag (tquotient, EQ, op0, op1,
4195 compute_mode, 1, 1);
4196 if (quotient == 0)
4197 goto fail1;
4199 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
4200 && (rem_flag ? smod_pow2_cheap[compute_mode]
4201 : sdiv_pow2_cheap[compute_mode])
4202 /* We assume that cheap metric is true if the
4203 optab has an expander for this mode. */
4204 && (((rem_flag ? smod_optab : sdiv_optab)
4205 ->handlers[compute_mode].insn_code
4206 != CODE_FOR_nothing)
4207 || (sdivmod_optab->handlers[compute_mode]
4208 .insn_code != CODE_FOR_nothing)))
4210 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
4212 if (rem_flag)
4214 remainder = expand_smod_pow2 (compute_mode, op0, d);
4215 if (remainder)
4216 return gen_lowpart (mode, remainder);
4219 if (sdiv_pow2_cheap[compute_mode]
4220 && ((sdiv_optab->handlers[compute_mode].insn_code
4221 != CODE_FOR_nothing)
4222 || (sdivmod_optab->handlers[compute_mode].insn_code
4223 != CODE_FOR_nothing)))
4224 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
4225 compute_mode, op0,
4226 gen_int_mode (abs_d,
4227 compute_mode),
4228 NULL_RTX, 0);
4229 else
4230 quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
4232 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4233 negate the quotient. */
4234 if (d < 0)
4236 insn = get_last_insn ();
4237 if (insn != last
4238 && (set = single_set (insn)) != 0
4239 && SET_DEST (set) == quotient
4240 && abs_d < ((unsigned HOST_WIDE_INT) 1
4241 << (HOST_BITS_PER_WIDE_INT - 1)))
4242 set_unique_reg_note (insn,
4243 REG_EQUAL,
4244 gen_rtx_DIV (compute_mode,
4245 op0,
4246 GEN_INT
4247 (trunc_int_for_mode
4248 (abs_d,
4249 compute_mode))));
4251 quotient = expand_unop (compute_mode, neg_optab,
4252 quotient, quotient, 0);
4255 else if (size <= HOST_BITS_PER_WIDE_INT)
4257 choose_multiplier (abs_d, size, size - 1,
4258 &mlr, &post_shift, &lgup);
4259 ml = (unsigned HOST_WIDE_INT) INTVAL (mlr);
4260 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
4262 rtx t1, t2, t3;
4264 if (post_shift >= BITS_PER_WORD
4265 || size - 1 >= BITS_PER_WORD)
4266 goto fail1;
4268 extra_cost = (shift_cost[compute_mode][post_shift]
4269 + shift_cost[compute_mode][size - 1]
4270 + add_cost[compute_mode]);
4271 t1 = expand_mult_highpart (compute_mode, op0, mlr,
4272 NULL_RTX, 0,
4273 max_cost - extra_cost);
4274 if (t1 == 0)
4275 goto fail1;
4276 t2 = expand_shift
4277 (RSHIFT_EXPR, compute_mode, t1,
4278 build_int_cst (NULL_TREE, post_shift),
4279 NULL_RTX, 0);
4280 t3 = expand_shift
4281 (RSHIFT_EXPR, compute_mode, op0,
4282 build_int_cst (NULL_TREE, size - 1),
4283 NULL_RTX, 0);
4284 if (d < 0)
4285 quotient
4286 = force_operand (gen_rtx_MINUS (compute_mode,
4287 t3, t2),
4288 tquotient);
4289 else
4290 quotient
4291 = force_operand (gen_rtx_MINUS (compute_mode,
4292 t2, t3),
4293 tquotient);
4295 else
4297 rtx t1, t2, t3, t4;
4299 if (post_shift >= BITS_PER_WORD
4300 || size - 1 >= BITS_PER_WORD)
4301 goto fail1;
4303 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
4304 mlr = gen_int_mode (ml, compute_mode);
4305 extra_cost = (shift_cost[compute_mode][post_shift]
4306 + shift_cost[compute_mode][size - 1]
4307 + 2 * add_cost[compute_mode]);
4308 t1 = expand_mult_highpart (compute_mode, op0, mlr,
4309 NULL_RTX, 0,
4310 max_cost - extra_cost);
4311 if (t1 == 0)
4312 goto fail1;
4313 t2 = force_operand (gen_rtx_PLUS (compute_mode,
4314 t1, op0),
4315 NULL_RTX);
4316 t3 = expand_shift
4317 (RSHIFT_EXPR, compute_mode, t2,
4318 build_int_cst (NULL_TREE, post_shift),
4319 NULL_RTX, 0);
4320 t4 = expand_shift
4321 (RSHIFT_EXPR, compute_mode, op0,
4322 build_int_cst (NULL_TREE, size - 1),
4323 NULL_RTX, 0);
4324 if (d < 0)
4325 quotient
4326 = force_operand (gen_rtx_MINUS (compute_mode,
4327 t4, t3),
4328 tquotient);
4329 else
4330 quotient
4331 = force_operand (gen_rtx_MINUS (compute_mode,
4332 t3, t4),
4333 tquotient);
4336 else /* Too wide mode to use tricky code */
4337 break;
4339 insn = get_last_insn ();
4340 if (insn != last
4341 && (set = single_set (insn)) != 0
4342 && SET_DEST (set) == quotient)
4343 set_unique_reg_note (insn,
4344 REG_EQUAL,
4345 gen_rtx_DIV (compute_mode, op0, op1));
4347 break;
4349 fail1:
4350 delete_insns_since (last);
4351 break;
4353 case FLOOR_DIV_EXPR:
4354 case FLOOR_MOD_EXPR:
4355 /* We will come here only for signed operations. */
4356 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4358 unsigned HOST_WIDE_INT mh;
4359 int pre_shift, lgup, post_shift;
4360 HOST_WIDE_INT d = INTVAL (op1);
4361 rtx ml;
4363 if (d > 0)
4365 /* We could just as easily deal with negative constants here,
4366 but it does not seem worth the trouble for GCC 2.6. */
4367 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4369 pre_shift = floor_log2 (d);
4370 if (rem_flag)
4372 remainder = expand_binop (compute_mode, and_optab, op0,
4373 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4374 remainder, 0, OPTAB_LIB_WIDEN);
4375 if (remainder)
4376 return gen_lowpart (mode, remainder);
4378 quotient = expand_shift
4379 (RSHIFT_EXPR, compute_mode, op0,
4380 build_int_cst (NULL_TREE, pre_shift),
4381 tquotient, 0);
4383 else
4385 rtx t1, t2, t3, t4;
4387 mh = choose_multiplier (d, size, size - 1,
4388 &ml, &post_shift, &lgup);
4389 gcc_assert (!mh);
4391 if (post_shift < BITS_PER_WORD
4392 && size - 1 < BITS_PER_WORD)
4394 t1 = expand_shift
4395 (RSHIFT_EXPR, compute_mode, op0,
4396 build_int_cst (NULL_TREE, size - 1),
4397 NULL_RTX, 0);
4398 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
4399 NULL_RTX, 0, OPTAB_WIDEN);
4400 extra_cost = (shift_cost[compute_mode][post_shift]
4401 + shift_cost[compute_mode][size - 1]
4402 + 2 * add_cost[compute_mode]);
4403 t3 = expand_mult_highpart (compute_mode, t2, ml,
4404 NULL_RTX, 1,
4405 max_cost - extra_cost);
4406 if (t3 != 0)
4408 t4 = expand_shift
4409 (RSHIFT_EXPR, compute_mode, t3,
4410 build_int_cst (NULL_TREE, post_shift),
4411 NULL_RTX, 1);
4412 quotient = expand_binop (compute_mode, xor_optab,
4413 t4, t1, tquotient, 0,
4414 OPTAB_WIDEN);
4419 else
4421 rtx nsign, t1, t2, t3, t4;
4422 t1 = force_operand (gen_rtx_PLUS (compute_mode,
4423 op0, constm1_rtx), NULL_RTX);
4424 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
4425 0, OPTAB_WIDEN);
4426 nsign = expand_shift
4427 (RSHIFT_EXPR, compute_mode, t2,
4428 build_int_cst (NULL_TREE, size - 1),
4429 NULL_RTX, 0);
4430 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
4431 NULL_RTX);
4432 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
4433 NULL_RTX, 0);
4434 if (t4)
4436 rtx t5;
4437 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
4438 NULL_RTX, 0);
4439 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4440 t4, t5),
4441 tquotient);
4446 if (quotient != 0)
4447 break;
4448 delete_insns_since (last);
4450 /* Try using an instruction that produces both the quotient and
4451 remainder, using truncation. We can easily compensate the quotient
4452 or remainder to get floor rounding, once we have the remainder.
4453 Notice that we compute also the final remainder value here,
4454 and return the result right away. */
4455 if (target == 0 || GET_MODE (target) != compute_mode)
4456 target = gen_reg_rtx (compute_mode);
4458 if (rem_flag)
4460 remainder
4461 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4462 quotient = gen_reg_rtx (compute_mode);
4464 else
4466 quotient
4467 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4468 remainder = gen_reg_rtx (compute_mode);
4471 if (expand_twoval_binop (sdivmod_optab, op0, op1,
4472 quotient, remainder, 0))
4474 /* This could be computed with a branch-less sequence.
4475 Save that for later. */
4476 rtx tem;
4477 rtx label = gen_label_rtx ();
4478 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4479 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4480 NULL_RTX, 0, OPTAB_WIDEN);
4481 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4482 expand_dec (quotient, const1_rtx);
4483 expand_inc (remainder, op1);
4484 emit_label (label);
4485 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4488 /* No luck with division elimination or divmod. Have to do it
4489 by conditionally adjusting op0 *and* the result. */
4491 rtx label1, label2, label3, label4, label5;
4492 rtx adjusted_op0;
4493 rtx tem;
4495 quotient = gen_reg_rtx (compute_mode);
4496 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4497 label1 = gen_label_rtx ();
4498 label2 = gen_label_rtx ();
4499 label3 = gen_label_rtx ();
4500 label4 = gen_label_rtx ();
4501 label5 = gen_label_rtx ();
4502 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4503 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4504 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4505 quotient, 0, OPTAB_LIB_WIDEN);
4506 if (tem != quotient)
4507 emit_move_insn (quotient, tem);
4508 emit_jump_insn (gen_jump (label5));
4509 emit_barrier ();
4510 emit_label (label1);
4511 expand_inc (adjusted_op0, const1_rtx);
4512 emit_jump_insn (gen_jump (label4));
4513 emit_barrier ();
4514 emit_label (label2);
4515 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4516 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4517 quotient, 0, OPTAB_LIB_WIDEN);
4518 if (tem != quotient)
4519 emit_move_insn (quotient, tem);
4520 emit_jump_insn (gen_jump (label5));
4521 emit_barrier ();
4522 emit_label (label3);
4523 expand_dec (adjusted_op0, const1_rtx);
4524 emit_label (label4);
4525 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4526 quotient, 0, OPTAB_LIB_WIDEN);
4527 if (tem != quotient)
4528 emit_move_insn (quotient, tem);
4529 expand_dec (quotient, const1_rtx);
4530 emit_label (label5);
4532 break;
4534 case CEIL_DIV_EXPR:
4535 case CEIL_MOD_EXPR:
4536 if (unsignedp)
4538 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
4540 rtx t1, t2, t3;
4541 unsigned HOST_WIDE_INT d = INTVAL (op1);
4542 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4543 build_int_cst (NULL_TREE, floor_log2 (d)),
4544 tquotient, 1);
4545 t2 = expand_binop (compute_mode, and_optab, op0,
4546 GEN_INT (d - 1),
4547 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4548 t3 = gen_reg_rtx (compute_mode);
4549 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4550 compute_mode, 1, 1);
4551 if (t3 == 0)
4553 rtx lab;
4554 lab = gen_label_rtx ();
4555 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4556 expand_inc (t1, const1_rtx);
4557 emit_label (lab);
4558 quotient = t1;
4560 else
4561 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4562 t1, t3),
4563 tquotient);
4564 break;
4567 /* Try using an instruction that produces both the quotient and
4568 remainder, using truncation. We can easily compensate the
4569 quotient or remainder to get ceiling rounding, once we have the
4570 remainder. Notice that we compute also the final remainder
4571 value here, and return the result right away. */
4572 if (target == 0 || GET_MODE (target) != compute_mode)
4573 target = gen_reg_rtx (compute_mode);
4575 if (rem_flag)
4577 remainder = (REG_P (target)
4578 ? target : gen_reg_rtx (compute_mode));
4579 quotient = gen_reg_rtx (compute_mode);
4581 else
4583 quotient = (REG_P (target)
4584 ? target : gen_reg_rtx (compute_mode));
4585 remainder = gen_reg_rtx (compute_mode);
4588 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4589 remainder, 1))
4591 /* This could be computed with a branch-less sequence.
4592 Save that for later. */
4593 rtx label = gen_label_rtx ();
4594 do_cmp_and_jump (remainder, const0_rtx, EQ,
4595 compute_mode, label);
4596 expand_inc (quotient, const1_rtx);
4597 expand_dec (remainder, op1);
4598 emit_label (label);
4599 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4602 /* No luck with division elimination or divmod. Have to do it
4603 by conditionally adjusting op0 *and* the result. */
4605 rtx label1, label2;
4606 rtx adjusted_op0, tem;
4608 quotient = gen_reg_rtx (compute_mode);
4609 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4610 label1 = gen_label_rtx ();
4611 label2 = gen_label_rtx ();
4612 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4613 compute_mode, label1);
4614 emit_move_insn (quotient, const0_rtx);
4615 emit_jump_insn (gen_jump (label2));
4616 emit_barrier ();
4617 emit_label (label1);
4618 expand_dec (adjusted_op0, const1_rtx);
4619 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4620 quotient, 1, OPTAB_LIB_WIDEN);
4621 if (tem != quotient)
4622 emit_move_insn (quotient, tem);
4623 expand_inc (quotient, const1_rtx);
4624 emit_label (label2);
4627 else /* signed */
4629 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4630 && INTVAL (op1) >= 0)
4632 /* This is extremely similar to the code for the unsigned case
4633 above. For 2.7 we should merge these variants, but for
4634 2.6.1 I don't want to touch the code for unsigned since that
4635 get used in C. The signed case will only be used by other
4636 languages (Ada). */
4638 rtx t1, t2, t3;
4639 unsigned HOST_WIDE_INT d = INTVAL (op1);
4640 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4641 build_int_cst (NULL_TREE, floor_log2 (d)),
4642 tquotient, 0);
4643 t2 = expand_binop (compute_mode, and_optab, op0,
4644 GEN_INT (d - 1),
4645 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4646 t3 = gen_reg_rtx (compute_mode);
4647 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4648 compute_mode, 1, 1);
4649 if (t3 == 0)
4651 rtx lab;
4652 lab = gen_label_rtx ();
4653 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4654 expand_inc (t1, const1_rtx);
4655 emit_label (lab);
4656 quotient = t1;
4658 else
4659 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4660 t1, t3),
4661 tquotient);
4662 break;
4665 /* Try using an instruction that produces both the quotient and
4666 remainder, using truncation. We can easily compensate the
4667 quotient or remainder to get ceiling rounding, once we have the
4668 remainder. Notice that we compute also the final remainder
4669 value here, and return the result right away. */
4670 if (target == 0 || GET_MODE (target) != compute_mode)
4671 target = gen_reg_rtx (compute_mode);
4672 if (rem_flag)
4674 remainder= (REG_P (target)
4675 ? target : gen_reg_rtx (compute_mode));
4676 quotient = gen_reg_rtx (compute_mode);
4678 else
4680 quotient = (REG_P (target)
4681 ? target : gen_reg_rtx (compute_mode));
4682 remainder = gen_reg_rtx (compute_mode);
4685 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4686 remainder, 0))
4688 /* This could be computed with a branch-less sequence.
4689 Save that for later. */
4690 rtx tem;
4691 rtx label = gen_label_rtx ();
4692 do_cmp_and_jump (remainder, const0_rtx, EQ,
4693 compute_mode, label);
4694 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4695 NULL_RTX, 0, OPTAB_WIDEN);
4696 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4697 expand_inc (quotient, const1_rtx);
4698 expand_dec (remainder, op1);
4699 emit_label (label);
4700 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4703 /* No luck with division elimination or divmod. Have to do it
4704 by conditionally adjusting op0 *and* the result. */
4706 rtx label1, label2, label3, label4, label5;
4707 rtx adjusted_op0;
4708 rtx tem;
4710 quotient = gen_reg_rtx (compute_mode);
4711 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4712 label1 = gen_label_rtx ();
4713 label2 = gen_label_rtx ();
4714 label3 = gen_label_rtx ();
4715 label4 = gen_label_rtx ();
4716 label5 = gen_label_rtx ();
4717 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4718 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4719 compute_mode, label1);
4720 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4721 quotient, 0, OPTAB_LIB_WIDEN);
4722 if (tem != quotient)
4723 emit_move_insn (quotient, tem);
4724 emit_jump_insn (gen_jump (label5));
4725 emit_barrier ();
4726 emit_label (label1);
4727 expand_dec (adjusted_op0, const1_rtx);
4728 emit_jump_insn (gen_jump (label4));
4729 emit_barrier ();
4730 emit_label (label2);
4731 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4732 compute_mode, label3);
4733 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4734 quotient, 0, OPTAB_LIB_WIDEN);
4735 if (tem != quotient)
4736 emit_move_insn (quotient, tem);
4737 emit_jump_insn (gen_jump (label5));
4738 emit_barrier ();
4739 emit_label (label3);
4740 expand_inc (adjusted_op0, const1_rtx);
4741 emit_label (label4);
4742 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4743 quotient, 0, OPTAB_LIB_WIDEN);
4744 if (tem != quotient)
4745 emit_move_insn (quotient, tem);
4746 expand_inc (quotient, const1_rtx);
4747 emit_label (label5);
4750 break;
4752 case EXACT_DIV_EXPR:
4753 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4755 HOST_WIDE_INT d = INTVAL (op1);
4756 unsigned HOST_WIDE_INT ml;
4757 int pre_shift;
4758 rtx t1;
4760 pre_shift = floor_log2 (d & -d);
4761 ml = invert_mod2n (d >> pre_shift, size);
4762 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4763 build_int_cst (NULL_TREE, pre_shift),
4764 NULL_RTX, unsignedp);
4765 quotient = expand_mult (compute_mode, t1,
4766 gen_int_mode (ml, compute_mode),
4767 NULL_RTX, 1);
4769 insn = get_last_insn ();
4770 set_unique_reg_note (insn,
4771 REG_EQUAL,
4772 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4773 compute_mode,
4774 op0, op1));
4776 break;
4778 case ROUND_DIV_EXPR:
4779 case ROUND_MOD_EXPR:
4780 if (unsignedp)
4782 rtx tem;
4783 rtx label;
4784 label = gen_label_rtx ();
4785 quotient = gen_reg_rtx (compute_mode);
4786 remainder = gen_reg_rtx (compute_mode);
4787 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4789 rtx tem;
4790 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
4791 quotient, 1, OPTAB_LIB_WIDEN);
4792 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
4793 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4794 remainder, 1, OPTAB_LIB_WIDEN);
4796 tem = plus_constant (op1, -1);
4797 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4798 build_int_cst (NULL_TREE, 1),
4799 NULL_RTX, 1);
4800 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
4801 expand_inc (quotient, const1_rtx);
4802 expand_dec (remainder, op1);
4803 emit_label (label);
4805 else
4807 rtx abs_rem, abs_op1, tem, mask;
4808 rtx label;
4809 label = gen_label_rtx ();
4810 quotient = gen_reg_rtx (compute_mode);
4811 remainder = gen_reg_rtx (compute_mode);
4812 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
4814 rtx tem;
4815 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
4816 quotient, 0, OPTAB_LIB_WIDEN);
4817 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
4818 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4819 remainder, 0, OPTAB_LIB_WIDEN);
4821 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
4822 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
4823 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
4824 build_int_cst (NULL_TREE, 1),
4825 NULL_RTX, 1);
4826 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
4827 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4828 NULL_RTX, 0, OPTAB_WIDEN);
4829 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4830 build_int_cst (NULL_TREE, size - 1),
4831 NULL_RTX, 0);
4832 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
4833 NULL_RTX, 0, OPTAB_WIDEN);
4834 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4835 NULL_RTX, 0, OPTAB_WIDEN);
4836 expand_inc (quotient, tem);
4837 tem = expand_binop (compute_mode, xor_optab, mask, op1,
4838 NULL_RTX, 0, OPTAB_WIDEN);
4839 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4840 NULL_RTX, 0, OPTAB_WIDEN);
4841 expand_dec (remainder, tem);
4842 emit_label (label);
4844 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4846 default:
4847 gcc_unreachable ();
4850 if (quotient == 0)
4852 if (target && GET_MODE (target) != compute_mode)
4853 target = 0;
4855 if (rem_flag)
4857 /* Try to produce the remainder without producing the quotient.
4858 If we seem to have a divmod pattern that does not require widening,
4859 don't try widening here. We should really have a WIDEN argument
4860 to expand_twoval_binop, since what we'd really like to do here is
4861 1) try a mod insn in compute_mode
4862 2) try a divmod insn in compute_mode
4863 3) try a div insn in compute_mode and multiply-subtract to get
4864 remainder
4865 4) try the same things with widening allowed. */
4866 remainder
4867 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4868 op0, op1, target,
4869 unsignedp,
4870 ((optab2->handlers[compute_mode].insn_code
4871 != CODE_FOR_nothing)
4872 ? OPTAB_DIRECT : OPTAB_WIDEN));
4873 if (remainder == 0)
4875 /* No luck there. Can we do remainder and divide at once
4876 without a library call? */
4877 remainder = gen_reg_rtx (compute_mode);
4878 if (! expand_twoval_binop ((unsignedp
4879 ? udivmod_optab
4880 : sdivmod_optab),
4881 op0, op1,
4882 NULL_RTX, remainder, unsignedp))
4883 remainder = 0;
4886 if (remainder)
4887 return gen_lowpart (mode, remainder);
4890 /* Produce the quotient. Try a quotient insn, but not a library call.
4891 If we have a divmod in this mode, use it in preference to widening
4892 the div (for this test we assume it will not fail). Note that optab2
4893 is set to the one of the two optabs that the call below will use. */
4894 quotient
4895 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
4896 op0, op1, rem_flag ? NULL_RTX : target,
4897 unsignedp,
4898 ((optab2->handlers[compute_mode].insn_code
4899 != CODE_FOR_nothing)
4900 ? OPTAB_DIRECT : OPTAB_WIDEN));
4902 if (quotient == 0)
4904 /* No luck there. Try a quotient-and-remainder insn,
4905 keeping the quotient alone. */
4906 quotient = gen_reg_rtx (compute_mode);
4907 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
4908 op0, op1,
4909 quotient, NULL_RTX, unsignedp))
4911 quotient = 0;
4912 if (! rem_flag)
4913 /* Still no luck. If we are not computing the remainder,
4914 use a library call for the quotient. */
4915 quotient = sign_expand_binop (compute_mode,
4916 udiv_optab, sdiv_optab,
4917 op0, op1, target,
4918 unsignedp, OPTAB_LIB_WIDEN);
4923 if (rem_flag)
4925 if (target && GET_MODE (target) != compute_mode)
4926 target = 0;
4928 if (quotient == 0)
4930 /* No divide instruction either. Use library for remainder. */
4931 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4932 op0, op1, target,
4933 unsignedp, OPTAB_LIB_WIDEN);
4934 /* No remainder function. Try a quotient-and-remainder
4935 function, keeping the remainder. */
4936 if (!remainder)
4938 remainder = gen_reg_rtx (compute_mode);
4939 if (!expand_twoval_binop_libfunc
4940 (unsignedp ? udivmod_optab : sdivmod_optab,
4941 op0, op1,
4942 NULL_RTX, remainder,
4943 unsignedp ? UMOD : MOD))
4944 remainder = NULL_RTX;
4947 else
4949 /* We divided. Now finish doing X - Y * (X / Y). */
4950 remainder = expand_mult (compute_mode, quotient, op1,
4951 NULL_RTX, unsignedp);
4952 remainder = expand_binop (compute_mode, sub_optab, op0,
4953 remainder, target, unsignedp,
4954 OPTAB_LIB_WIDEN);
4958 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4961 /* Return a tree node with data type TYPE, describing the value of X.
4962 Usually this is an VAR_DECL, if there is no obvious better choice.
4963 X may be an expression, however we only support those expressions
4964 generated by loop.c. */
4966 tree
4967 make_tree (tree type, rtx x)
4969 tree t;
4971 switch (GET_CODE (x))
4973 case CONST_INT:
4975 HOST_WIDE_INT hi = 0;
4977 if (INTVAL (x) < 0
4978 && !(TYPE_UNSIGNED (type)
4979 && (GET_MODE_BITSIZE (TYPE_MODE (type))
4980 < HOST_BITS_PER_WIDE_INT)))
4981 hi = -1;
4983 t = build_int_cst_wide (type, INTVAL (x), hi);
4985 return t;
4988 case CONST_DOUBLE:
4989 if (GET_MODE (x) == VOIDmode)
4990 t = build_int_cst_wide (type,
4991 CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
4992 else
4994 REAL_VALUE_TYPE d;
4996 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
4997 t = build_real (type, d);
5000 return t;
5002 case CONST_VECTOR:
5004 int units = CONST_VECTOR_NUNITS (x);
5005 tree itype = TREE_TYPE (type);
5006 tree t = NULL_TREE;
5007 int i;
5010 /* Build a tree with vector elements. */
5011 for (i = units - 1; i >= 0; --i)
5013 rtx elt = CONST_VECTOR_ELT (x, i);
5014 t = tree_cons (NULL_TREE, make_tree (itype, elt), t);
5017 return build_vector (type, t);
5020 case PLUS:
5021 return fold_build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5022 make_tree (type, XEXP (x, 1)));
5024 case MINUS:
5025 return fold_build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5026 make_tree (type, XEXP (x, 1)));
5028 case NEG:
5029 return fold_build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0)));
5031 case MULT:
5032 return fold_build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
5033 make_tree (type, XEXP (x, 1)));
5035 case ASHIFT:
5036 return fold_build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
5037 make_tree (type, XEXP (x, 1)));
5039 case LSHIFTRT:
5040 t = lang_hooks.types.unsigned_type (type);
5041 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5042 make_tree (t, XEXP (x, 0)),
5043 make_tree (type, XEXP (x, 1))));
5045 case ASHIFTRT:
5046 t = lang_hooks.types.signed_type (type);
5047 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5048 make_tree (t, XEXP (x, 0)),
5049 make_tree (type, XEXP (x, 1))));
5051 case DIV:
5052 if (TREE_CODE (type) != REAL_TYPE)
5053 t = lang_hooks.types.signed_type (type);
5054 else
5055 t = type;
5057 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5058 make_tree (t, XEXP (x, 0)),
5059 make_tree (t, XEXP (x, 1))));
5060 case UDIV:
5061 t = lang_hooks.types.unsigned_type (type);
5062 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5063 make_tree (t, XEXP (x, 0)),
5064 make_tree (t, XEXP (x, 1))));
5066 case SIGN_EXTEND:
5067 case ZERO_EXTEND:
5068 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
5069 GET_CODE (x) == ZERO_EXTEND);
5070 return fold_convert (type, make_tree (t, XEXP (x, 0)));
5072 case CONST:
5073 return make_tree (type, XEXP (x, 0));
5075 case SYMBOL_REF:
5076 t = SYMBOL_REF_DECL (x);
5077 if (t)
5078 return fold_convert (type, build_fold_addr_expr (t));
5079 /* else fall through. */
5081 default:
5082 t = build_decl (VAR_DECL, NULL_TREE, type);
5084 /* If TYPE is a POINTER_TYPE, X might be Pmode with TYPE_MODE being
5085 ptr_mode. So convert. */
5086 if (POINTER_TYPE_P (type))
5087 x = convert_memory_address (TYPE_MODE (type), x);
5089 /* Note that we do *not* use SET_DECL_RTL here, because we do not
5090 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
5091 t->decl_with_rtl.rtl = x;
5093 return t;
5097 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5098 and returning TARGET.
5100 If TARGET is 0, a pseudo-register or constant is returned. */
5103 expand_and (enum machine_mode mode, rtx op0, rtx op1, rtx target)
5105 rtx tem = 0;
5107 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
5108 tem = simplify_binary_operation (AND, mode, op0, op1);
5109 if (tem == 0)
5110 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
5112 if (target == 0)
5113 target = tem;
5114 else if (tem != target)
5115 emit_move_insn (target, tem);
5116 return target;
5119 /* Helper function for emit_store_flag. */
5120 static rtx
5121 emit_store_flag_1 (rtx target, rtx subtarget, enum machine_mode mode,
5122 int normalizep)
5124 rtx op0;
5125 enum machine_mode target_mode = GET_MODE (target);
5127 /* If we are converting to a wider mode, first convert to
5128 TARGET_MODE, then normalize. This produces better combining
5129 opportunities on machines that have a SIGN_EXTRACT when we are
5130 testing a single bit. This mostly benefits the 68k.
5132 If STORE_FLAG_VALUE does not have the sign bit set when
5133 interpreted in MODE, we can do this conversion as unsigned, which
5134 is usually more efficient. */
5135 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
5137 convert_move (target, subtarget,
5138 (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5139 && 0 == (STORE_FLAG_VALUE
5140 & ((HOST_WIDE_INT) 1
5141 << (GET_MODE_BITSIZE (mode) -1))));
5142 op0 = target;
5143 mode = target_mode;
5145 else
5146 op0 = subtarget;
5148 /* If we want to keep subexpressions around, don't reuse our last
5149 target. */
5150 if (optimize)
5151 subtarget = 0;
5153 /* Now normalize to the proper value in MODE. Sometimes we don't
5154 have to do anything. */
5155 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
5157 /* STORE_FLAG_VALUE might be the most negative number, so write
5158 the comparison this way to avoid a compiler-time warning. */
5159 else if (- normalizep == STORE_FLAG_VALUE)
5160 op0 = expand_unop (mode, neg_optab, op0, subtarget, 0);
5162 /* We don't want to use STORE_FLAG_VALUE < 0 below since this makes
5163 it hard to use a value of just the sign bit due to ANSI integer
5164 constant typing rules. */
5165 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5166 && (STORE_FLAG_VALUE
5167 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))))
5168 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
5169 size_int (GET_MODE_BITSIZE (mode) - 1), subtarget,
5170 normalizep == 1);
5171 else
5173 gcc_assert (STORE_FLAG_VALUE & 1);
5175 op0 = expand_and (mode, op0, const1_rtx, subtarget);
5176 if (normalizep == -1)
5177 op0 = expand_unop (mode, neg_optab, op0, op0, 0);
5180 /* If we were converting to a smaller mode, do the conversion now. */
5181 if (target_mode != mode)
5183 convert_move (target, op0, 0);
5184 return target;
5186 else
5187 return op0;
5190 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5191 and storing in TARGET. Normally return TARGET.
5192 Return 0 if that cannot be done.
5194 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5195 it is VOIDmode, they cannot both be CONST_INT.
5197 UNSIGNEDP is for the case where we have to widen the operands
5198 to perform the operation. It says to use zero-extension.
5200 NORMALIZEP is 1 if we should convert the result to be either zero
5201 or one. Normalize is -1 if we should convert the result to be
5202 either zero or -1. If NORMALIZEP is zero, the result will be left
5203 "raw" out of the scc insn. */
5206 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
5207 enum machine_mode mode, int unsignedp, int normalizep)
5209 rtx subtarget;
5210 enum insn_code icode;
5211 enum machine_mode compare_mode;
5212 enum machine_mode target_mode = GET_MODE (target);
5213 rtx tem;
5214 rtx last = get_last_insn ();
5215 rtx pattern, comparison;
5217 if (unsignedp)
5218 code = unsigned_condition (code);
5220 /* If one operand is constant, make it the second one. Only do this
5221 if the other operand is not constant as well. */
5223 if (swap_commutative_operands_p (op0, op1))
5225 tem = op0;
5226 op0 = op1;
5227 op1 = tem;
5228 code = swap_condition (code);
5231 if (mode == VOIDmode)
5232 mode = GET_MODE (op0);
5234 /* For some comparisons with 1 and -1, we can convert this to
5235 comparisons with zero. This will often produce more opportunities for
5236 store-flag insns. */
5238 switch (code)
5240 case LT:
5241 if (op1 == const1_rtx)
5242 op1 = const0_rtx, code = LE;
5243 break;
5244 case LE:
5245 if (op1 == constm1_rtx)
5246 op1 = const0_rtx, code = LT;
5247 break;
5248 case GE:
5249 if (op1 == const1_rtx)
5250 op1 = const0_rtx, code = GT;
5251 break;
5252 case GT:
5253 if (op1 == constm1_rtx)
5254 op1 = const0_rtx, code = GE;
5255 break;
5256 case GEU:
5257 if (op1 == const1_rtx)
5258 op1 = const0_rtx, code = NE;
5259 break;
5260 case LTU:
5261 if (op1 == const1_rtx)
5262 op1 = const0_rtx, code = EQ;
5263 break;
5264 default:
5265 break;
5268 /* If we are comparing a double-word integer with zero or -1, we can
5269 convert the comparison into one involving a single word. */
5270 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
5271 && GET_MODE_CLASS (mode) == MODE_INT
5272 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
5274 if ((code == EQ || code == NE)
5275 && (op1 == const0_rtx || op1 == constm1_rtx))
5277 rtx op00, op01, op0both;
5279 /* Do a logical OR or AND of the two words and compare the
5280 result. */
5281 op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
5282 op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
5283 op0both = expand_binop (word_mode,
5284 op1 == const0_rtx ? ior_optab : and_optab,
5285 op00, op01, NULL_RTX, unsignedp,
5286 OPTAB_DIRECT);
5288 if (op0both != 0)
5289 return emit_store_flag (target, code, op0both, op1, word_mode,
5290 unsignedp, normalizep);
5292 else if ((code == LT || code == GE) && op1 == const0_rtx)
5294 rtx op0h;
5296 /* If testing the sign bit, can just test on high word. */
5297 op0h = simplify_gen_subreg (word_mode, op0, mode,
5298 subreg_highpart_offset (word_mode,
5299 mode));
5300 return emit_store_flag (target, code, op0h, op1, word_mode,
5301 unsignedp, normalizep);
5305 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5306 complement of A (for GE) and shifting the sign bit to the low bit. */
5307 if (op1 == const0_rtx && (code == LT || code == GE)
5308 && GET_MODE_CLASS (mode) == MODE_INT
5309 && (normalizep || STORE_FLAG_VALUE == 1
5310 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5311 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5312 == ((unsigned HOST_WIDE_INT) 1
5313 << (GET_MODE_BITSIZE (mode) - 1))))))
5315 subtarget = target;
5317 /* If the result is to be wider than OP0, it is best to convert it
5318 first. If it is to be narrower, it is *incorrect* to convert it
5319 first. */
5320 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
5322 op0 = convert_modes (target_mode, mode, op0, 0);
5323 mode = target_mode;
5326 if (target_mode != mode)
5327 subtarget = 0;
5329 if (code == GE)
5330 op0 = expand_unop (mode, one_cmpl_optab, op0,
5331 ((STORE_FLAG_VALUE == 1 || normalizep)
5332 ? 0 : subtarget), 0);
5334 if (STORE_FLAG_VALUE == 1 || normalizep)
5335 /* If we are supposed to produce a 0/1 value, we want to do
5336 a logical shift from the sign bit to the low-order bit; for
5337 a -1/0 value, we do an arithmetic shift. */
5338 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
5339 size_int (GET_MODE_BITSIZE (mode) - 1),
5340 subtarget, normalizep != -1);
5342 if (mode != target_mode)
5343 op0 = convert_modes (target_mode, mode, op0, 0);
5345 return op0;
5348 icode = setcc_gen_code[(int) code];
5350 if (icode != CODE_FOR_nothing)
5352 insn_operand_predicate_fn pred;
5354 /* We think we may be able to do this with a scc insn. Emit the
5355 comparison and then the scc insn. */
5357 do_pending_stack_adjust ();
5358 last = get_last_insn ();
5360 comparison
5361 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX);
5362 if (CONSTANT_P (comparison))
5364 switch (GET_CODE (comparison))
5366 case CONST_INT:
5367 if (comparison == const0_rtx)
5368 return const0_rtx;
5369 break;
5371 #ifdef FLOAT_STORE_FLAG_VALUE
5372 case CONST_DOUBLE:
5373 if (comparison == CONST0_RTX (GET_MODE (comparison)))
5374 return const0_rtx;
5375 break;
5376 #endif
5377 default:
5378 gcc_unreachable ();
5381 if (normalizep == 1)
5382 return const1_rtx;
5383 if (normalizep == -1)
5384 return constm1_rtx;
5385 return const_true_rtx;
5388 /* The code of COMPARISON may not match CODE if compare_from_rtx
5389 decided to swap its operands and reverse the original code.
5391 We know that compare_from_rtx returns either a CONST_INT or
5392 a new comparison code, so it is safe to just extract the
5393 code from COMPARISON. */
5394 code = GET_CODE (comparison);
5396 /* Get a reference to the target in the proper mode for this insn. */
5397 compare_mode = insn_data[(int) icode].operand[0].mode;
5398 subtarget = target;
5399 pred = insn_data[(int) icode].operand[0].predicate;
5400 if (optimize || ! (*pred) (subtarget, compare_mode))
5401 subtarget = gen_reg_rtx (compare_mode);
5403 pattern = GEN_FCN (icode) (subtarget);
5404 if (pattern)
5406 emit_insn (pattern);
5407 return emit_store_flag_1 (target, subtarget, compare_mode,
5408 normalizep);
5411 else
5413 /* We don't have an scc insn, so try a cstore insn. */
5415 for (compare_mode = mode; compare_mode != VOIDmode;
5416 compare_mode = GET_MODE_WIDER_MODE (compare_mode))
5418 icode = cstore_optab->handlers[(int) compare_mode].insn_code;
5419 if (icode != CODE_FOR_nothing)
5420 break;
5423 if (icode != CODE_FOR_nothing)
5425 enum machine_mode result_mode
5426 = insn_data[(int) icode].operand[0].mode;
5427 rtx cstore_op0 = op0;
5428 rtx cstore_op1 = op1;
5430 do_pending_stack_adjust ();
5431 last = get_last_insn ();
5433 if (compare_mode != mode)
5435 cstore_op0 = convert_modes (compare_mode, mode, cstore_op0,
5436 unsignedp);
5437 cstore_op1 = convert_modes (compare_mode, mode, cstore_op1,
5438 unsignedp);
5441 if (!insn_data[(int) icode].operand[2].predicate (cstore_op0,
5442 compare_mode))
5443 cstore_op0 = copy_to_mode_reg (compare_mode, cstore_op0);
5445 if (!insn_data[(int) icode].operand[3].predicate (cstore_op1,
5446 compare_mode))
5447 cstore_op1 = copy_to_mode_reg (compare_mode, cstore_op1);
5449 comparison = gen_rtx_fmt_ee (code, result_mode, cstore_op0,
5450 cstore_op1);
5451 subtarget = target;
5453 if (optimize || !(insn_data[(int) icode].operand[0].predicate
5454 (subtarget, result_mode)))
5455 subtarget = gen_reg_rtx (result_mode);
5457 pattern = GEN_FCN (icode) (subtarget, comparison, cstore_op0,
5458 cstore_op1);
5460 if (pattern)
5462 emit_insn (pattern);
5463 return emit_store_flag_1 (target, subtarget, result_mode,
5464 normalizep);
5469 delete_insns_since (last);
5471 /* If optimizing, use different pseudo registers for each insn, instead
5472 of reusing the same pseudo. This leads to better CSE, but slows
5473 down the compiler, since there are more pseudos */
5474 subtarget = (!optimize
5475 && (target_mode == mode)) ? target : NULL_RTX;
5477 /* If we reached here, we can't do this with a scc insn. However, there
5478 are some comparisons that can be done directly. For example, if
5479 this is an equality comparison of integers, we can try to exclusive-or
5480 (or subtract) the two operands and use a recursive call to try the
5481 comparison with zero. Don't do any of these cases if branches are
5482 very cheap. */
5484 if (BRANCH_COST > 0
5485 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
5486 && op1 != const0_rtx)
5488 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5489 OPTAB_WIDEN);
5491 if (tem == 0)
5492 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5493 OPTAB_WIDEN);
5494 if (tem != 0)
5495 tem = emit_store_flag (target, code, tem, const0_rtx,
5496 mode, unsignedp, normalizep);
5497 if (tem == 0)
5498 delete_insns_since (last);
5499 return tem;
5502 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5503 the constant zero. Reject all other comparisons at this point. Only
5504 do LE and GT if branches are expensive since they are expensive on
5505 2-operand machines. */
5507 if (BRANCH_COST == 0
5508 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
5509 || (code != EQ && code != NE
5510 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
5511 return 0;
5513 /* See what we need to return. We can only return a 1, -1, or the
5514 sign bit. */
5516 if (normalizep == 0)
5518 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5519 normalizep = STORE_FLAG_VALUE;
5521 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5522 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5523 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
5525 else
5526 return 0;
5529 /* Try to put the result of the comparison in the sign bit. Assume we can't
5530 do the necessary operation below. */
5532 tem = 0;
5534 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5535 the sign bit set. */
5537 if (code == LE)
5539 /* This is destructive, so SUBTARGET can't be OP0. */
5540 if (rtx_equal_p (subtarget, op0))
5541 subtarget = 0;
5543 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5544 OPTAB_WIDEN);
5545 if (tem)
5546 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5547 OPTAB_WIDEN);
5550 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5551 number of bits in the mode of OP0, minus one. */
5553 if (code == GT)
5555 if (rtx_equal_p (subtarget, op0))
5556 subtarget = 0;
5558 tem = expand_shift (RSHIFT_EXPR, mode, op0,
5559 size_int (GET_MODE_BITSIZE (mode) - 1),
5560 subtarget, 0);
5561 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5562 OPTAB_WIDEN);
5565 if (code == EQ || code == NE)
5567 /* For EQ or NE, one way to do the comparison is to apply an operation
5568 that converts the operand into a positive number if it is nonzero
5569 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5570 for NE we negate. This puts the result in the sign bit. Then we
5571 normalize with a shift, if needed.
5573 Two operations that can do the above actions are ABS and FFS, so try
5574 them. If that doesn't work, and MODE is smaller than a full word,
5575 we can use zero-extension to the wider mode (an unsigned conversion)
5576 as the operation. */
5578 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5579 that is compensated by the subsequent overflow when subtracting
5580 one / negating. */
5582 if (abs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5583 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5584 else if (ffs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5585 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5586 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5588 tem = convert_modes (word_mode, mode, op0, 1);
5589 mode = word_mode;
5592 if (tem != 0)
5594 if (code == EQ)
5595 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5596 0, OPTAB_WIDEN);
5597 else
5598 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5601 /* If we couldn't do it that way, for NE we can "or" the two's complement
5602 of the value with itself. For EQ, we take the one's complement of
5603 that "or", which is an extra insn, so we only handle EQ if branches
5604 are expensive. */
5606 if (tem == 0 && (code == NE || BRANCH_COST > 1))
5608 if (rtx_equal_p (subtarget, op0))
5609 subtarget = 0;
5611 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5612 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5613 OPTAB_WIDEN);
5615 if (tem && code == EQ)
5616 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5620 if (tem && normalizep)
5621 tem = expand_shift (RSHIFT_EXPR, mode, tem,
5622 size_int (GET_MODE_BITSIZE (mode) - 1),
5623 subtarget, normalizep == 1);
5625 if (tem)
5627 if (GET_MODE (tem) != target_mode)
5629 convert_move (target, tem, 0);
5630 tem = target;
5632 else if (!subtarget)
5634 emit_move_insn (target, tem);
5635 tem = target;
5638 else
5639 delete_insns_since (last);
5641 return tem;
5644 /* Like emit_store_flag, but always succeeds. */
5647 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5648 enum machine_mode mode, int unsignedp, int normalizep)
5650 rtx tem, label;
5652 /* First see if emit_store_flag can do the job. */
5653 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5654 if (tem != 0)
5655 return tem;
5657 if (normalizep == 0)
5658 normalizep = 1;
5660 /* If this failed, we have to do this with set/compare/jump/set code. */
5662 if (!REG_P (target)
5663 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5664 target = gen_reg_rtx (GET_MODE (target));
5666 emit_move_insn (target, const1_rtx);
5667 label = gen_label_rtx ();
5668 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
5669 NULL_RTX, label);
5671 emit_move_insn (target, const0_rtx);
5672 emit_label (label);
5674 return target;
5677 /* Perform possibly multi-word comparison and conditional jump to LABEL
5678 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE. This is
5679 now a thin wrapper around do_compare_rtx_and_jump. */
5681 static void
5682 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, enum machine_mode mode,
5683 rtx label)
5685 int unsignedp = (op == LTU || op == LEU || op == GTU || op == GEU);
5686 do_compare_rtx_and_jump (arg1, arg2, op, unsignedp, mode,
5687 NULL_RTX, NULL_RTX, label);