* decl.c (add_decl_to_level): Remove TREE_PERMANENT assertion.
[official-gcc.git] / gcc / expmed.c
blob1ac3272e80a54e56f9e3fc0999c577e116aea6e9
1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987, 88, 89, 92-97, 1998 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "toplev.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "flags.h"
29 #include "insn-flags.h"
30 #include "insn-codes.h"
31 #include "insn-config.h"
32 #include "expr.h"
33 #include "real.h"
34 #include "recog.h"
36 static void store_fixed_bit_field PROTO((rtx, int, int, int, rtx, int));
37 static void store_split_bit_field PROTO((rtx, int, int, rtx, int));
38 static rtx extract_fixed_bit_field PROTO((enum machine_mode, rtx, int,
39 int, int, rtx, int, int));
40 static rtx mask_rtx PROTO((enum machine_mode, int,
41 int, int));
42 static rtx lshift_value PROTO((enum machine_mode, rtx,
43 int, int));
44 static rtx extract_split_bit_field PROTO((rtx, int, int, int, int));
45 static void do_cmp_and_jump PROTO((rtx, rtx, enum rtx_code,
46 enum machine_mode, rtx));
48 #define CEIL(x,y) (((x) + (y) - 1) / (y))
50 /* Non-zero means divides or modulus operations are relatively cheap for
51 powers of two, so don't use branches; emit the operation instead.
52 Usually, this will mean that the MD file will emit non-branch
53 sequences. */
55 static int sdiv_pow2_cheap, smod_pow2_cheap;
57 #ifndef SLOW_UNALIGNED_ACCESS
58 #define SLOW_UNALIGNED_ACCESS STRICT_ALIGNMENT
59 #endif
61 /* For compilers that support multiple targets with different word sizes,
62 MAX_BITS_PER_WORD contains the biggest value of BITS_PER_WORD. An example
63 is the H8/300(H) compiler. */
65 #ifndef MAX_BITS_PER_WORD
66 #define MAX_BITS_PER_WORD BITS_PER_WORD
67 #endif
69 /* Cost of various pieces of RTL. Note that some of these are indexed by
70 shift count and some by mode. */
71 static int add_cost, negate_cost, zero_cost;
72 static int shift_cost[MAX_BITS_PER_WORD];
73 static int shiftadd_cost[MAX_BITS_PER_WORD];
74 static int shiftsub_cost[MAX_BITS_PER_WORD];
75 static int mul_cost[NUM_MACHINE_MODES];
76 static int div_cost[NUM_MACHINE_MODES];
77 static int mul_widen_cost[NUM_MACHINE_MODES];
78 static int mul_highpart_cost[NUM_MACHINE_MODES];
80 void
81 init_expmed ()
83 char *free_point;
84 /* This is "some random pseudo register" for purposes of calling recog
85 to see what insns exist. */
86 rtx reg = gen_rtx_REG (word_mode, 10000);
87 rtx shift_insn, shiftadd_insn, shiftsub_insn;
88 int dummy;
89 int m;
90 enum machine_mode mode, wider_mode;
92 start_sequence ();
94 /* Since we are on the permanent obstack, we must be sure we save this
95 spot AFTER we call start_sequence, since it will reuse the rtl it
96 makes. */
97 free_point = (char *) oballoc (0);
99 reg = gen_rtx_REG (word_mode, 10000);
101 zero_cost = rtx_cost (const0_rtx, 0);
102 add_cost = rtx_cost (gen_rtx_PLUS (word_mode, reg, reg), SET);
104 shift_insn = emit_insn (gen_rtx_SET (VOIDmode, reg,
105 gen_rtx_ASHIFT (word_mode, reg,
106 const0_rtx)));
108 shiftadd_insn
109 = emit_insn (gen_rtx_SET (VOIDmode, reg,
110 gen_rtx_PLUS (word_mode,
111 gen_rtx_MULT (word_mode,
112 reg, const0_rtx),
113 reg)));
115 shiftsub_insn
116 = emit_insn (gen_rtx_SET (VOIDmode, reg,
117 gen_rtx_MINUS (word_mode,
118 gen_rtx_MULT (word_mode,
119 reg, const0_rtx),
120 reg)));
122 init_recog ();
124 shift_cost[0] = 0;
125 shiftadd_cost[0] = shiftsub_cost[0] = add_cost;
127 for (m = 1; m < MAX_BITS_PER_WORD; m++)
129 shift_cost[m] = shiftadd_cost[m] = shiftsub_cost[m] = 32000;
131 XEXP (SET_SRC (PATTERN (shift_insn)), 1) = GEN_INT (m);
132 if (recog (PATTERN (shift_insn), shift_insn, &dummy) >= 0)
133 shift_cost[m] = rtx_cost (SET_SRC (PATTERN (shift_insn)), SET);
135 XEXP (XEXP (SET_SRC (PATTERN (shiftadd_insn)), 0), 1)
136 = GEN_INT ((HOST_WIDE_INT) 1 << m);
137 if (recog (PATTERN (shiftadd_insn), shiftadd_insn, &dummy) >= 0)
138 shiftadd_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftadd_insn)), SET);
140 XEXP (XEXP (SET_SRC (PATTERN (shiftsub_insn)), 0), 1)
141 = GEN_INT ((HOST_WIDE_INT) 1 << m);
142 if (recog (PATTERN (shiftsub_insn), shiftsub_insn, &dummy) >= 0)
143 shiftsub_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftsub_insn)), SET);
146 negate_cost = rtx_cost (gen_rtx_NEG (word_mode, reg), SET);
148 sdiv_pow2_cheap
149 = (rtx_cost (gen_rtx_DIV (word_mode, reg, GEN_INT (32)), SET)
150 <= 2 * add_cost);
151 smod_pow2_cheap
152 = (rtx_cost (gen_rtx_MOD (word_mode, reg, GEN_INT (32)), SET)
153 <= 2 * add_cost);
155 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
156 mode != VOIDmode;
157 mode = GET_MODE_WIDER_MODE (mode))
159 reg = gen_rtx_REG (mode, 10000);
160 div_cost[(int) mode] = rtx_cost (gen_rtx_UDIV (mode, reg, reg), SET);
161 mul_cost[(int) mode] = rtx_cost (gen_rtx_MULT (mode, reg, reg), SET);
162 wider_mode = GET_MODE_WIDER_MODE (mode);
163 if (wider_mode != VOIDmode)
165 mul_widen_cost[(int) wider_mode]
166 = rtx_cost (gen_rtx_MULT (wider_mode,
167 gen_rtx_ZERO_EXTEND (wider_mode, reg),
168 gen_rtx_ZERO_EXTEND (wider_mode, reg)),
169 SET);
170 mul_highpart_cost[(int) mode]
171 = rtx_cost (gen_rtx_TRUNCATE
172 (mode,
173 gen_rtx_LSHIFTRT (wider_mode,
174 gen_rtx_MULT (wider_mode,
175 gen_rtx_ZERO_EXTEND
176 (wider_mode, reg),
177 gen_rtx_ZERO_EXTEND
178 (wider_mode, reg)),
179 GEN_INT (GET_MODE_BITSIZE (mode)))),
180 SET);
184 /* Free the objects we just allocated. */
185 end_sequence ();
186 obfree (free_point);
189 /* Return an rtx representing minus the value of X.
190 MODE is the intended mode of the result,
191 useful if X is a CONST_INT. */
194 negate_rtx (mode, x)
195 enum machine_mode mode;
196 rtx x;
198 rtx result = simplify_unary_operation (NEG, mode, x, mode);
200 if (result == 0)
201 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
203 return result;
206 /* Generate code to store value from rtx VALUE
207 into a bit-field within structure STR_RTX
208 containing BITSIZE bits starting at bit BITNUM.
209 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
210 ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
211 TOTAL_SIZE is the size of the structure in bytes, or -1 if varying. */
213 /* ??? Note that there are two different ideas here for how
214 to determine the size to count bits within, for a register.
215 One is BITS_PER_WORD, and the other is the size of operand 3
216 of the insv pattern.
218 If operand 3 of the insv pattern is VOIDmode, then we will use BITS_PER_WORD
219 else, we use the mode of operand 3. */
222 store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, align, total_size)
223 rtx str_rtx;
224 register int bitsize;
225 int bitnum;
226 enum machine_mode fieldmode;
227 rtx value;
228 int align;
229 int total_size;
231 int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
232 register int offset = bitnum / unit;
233 register int bitpos = bitnum % unit;
234 register rtx op0 = str_rtx;
235 #ifdef HAVE_insv
236 int insv_bitsize;
237 enum machine_mode op_mode;
239 op_mode = insn_data[(int) CODE_FOR_insv].operand[3].mode;
240 if (op_mode == VOIDmode)
241 op_mode = word_mode;
242 insv_bitsize = GET_MODE_BITSIZE (op_mode);
243 #endif
245 if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
246 abort ();
248 /* Discount the part of the structure before the desired byte.
249 We need to know how many bytes are safe to reference after it. */
250 if (total_size >= 0)
251 total_size -= (bitpos / BIGGEST_ALIGNMENT
252 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
254 while (GET_CODE (op0) == SUBREG)
256 /* The following line once was done only if WORDS_BIG_ENDIAN,
257 but I think that is a mistake. WORDS_BIG_ENDIAN is
258 meaningful at a much higher level; when structures are copied
259 between memory and regs, the higher-numbered regs
260 always get higher addresses. */
261 offset += SUBREG_WORD (op0);
262 /* We used to adjust BITPOS here, but now we do the whole adjustment
263 right after the loop. */
264 op0 = SUBREG_REG (op0);
267 /* Make sure we are playing with integral modes. Pun with subregs
268 if we aren't. */
270 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
271 if (imode != GET_MODE (op0))
273 if (GET_CODE (op0) == MEM)
274 op0 = change_address (op0, imode, NULL_RTX);
275 else if (imode != BLKmode)
276 op0 = gen_lowpart (imode, op0);
277 else
278 abort ();
282 /* If OP0 is a register, BITPOS must count within a word.
283 But as we have it, it counts within whatever size OP0 now has.
284 On a bigendian machine, these are not the same, so convert. */
285 if (BYTES_BIG_ENDIAN
286 && GET_CODE (op0) != MEM
287 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
288 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
290 value = protect_from_queue (value, 0);
292 if (flag_force_mem)
293 value = force_not_mem (value);
295 /* Note that the adjustment of BITPOS above has no effect on whether
296 BITPOS is 0 in a REG bigger than a word. */
297 if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
298 && (GET_CODE (op0) != MEM
299 || ! SLOW_UNALIGNED_ACCESS
300 || (offset * BITS_PER_UNIT % bitsize == 0
301 && align % GET_MODE_SIZE (fieldmode) == 0))
302 && bitpos == 0 && bitsize == GET_MODE_BITSIZE (fieldmode))
304 /* Storing in a full-word or multi-word field in a register
305 can be done with just SUBREG. */
306 if (GET_MODE (op0) != fieldmode)
308 if (GET_CODE (op0) == SUBREG)
310 if (GET_MODE (SUBREG_REG (op0)) == fieldmode
311 || GET_MODE_CLASS (fieldmode) == MODE_INT
312 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT)
313 op0 = SUBREG_REG (op0);
314 else
315 /* Else we've got some float mode source being extracted into
316 a different float mode destination -- this combination of
317 subregs results in Severe Tire Damage. */
318 abort ();
320 if (GET_CODE (op0) == REG)
321 op0 = gen_rtx_SUBREG (fieldmode, op0, offset);
322 else
323 op0 = change_address (op0, fieldmode,
324 plus_constant (XEXP (op0, 0), offset));
326 emit_move_insn (op0, value);
327 return value;
330 /* Storing an lsb-aligned field in a register
331 can be done with a movestrict instruction. */
333 if (GET_CODE (op0) != MEM
334 && (BYTES_BIG_ENDIAN ? bitpos + bitsize == unit : bitpos == 0)
335 && bitsize == GET_MODE_BITSIZE (fieldmode)
336 && (GET_MODE (op0) == fieldmode
337 || (movstrict_optab->handlers[(int) fieldmode].insn_code
338 != CODE_FOR_nothing)))
340 /* Get appropriate low part of the value being stored. */
341 if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
342 value = gen_lowpart (fieldmode, value);
343 else if (!(GET_CODE (value) == SYMBOL_REF
344 || GET_CODE (value) == LABEL_REF
345 || GET_CODE (value) == CONST))
346 value = convert_to_mode (fieldmode, value, 0);
348 if (GET_MODE (op0) == fieldmode)
349 emit_move_insn (op0, value);
350 else
352 int icode = movstrict_optab->handlers[(int) fieldmode].insn_code;
353 if (! (*insn_data[icode].operand[1].predicate) (value, fieldmode))
354 value = copy_to_mode_reg (fieldmode, value);
356 if (GET_CODE (op0) == SUBREG)
358 if (GET_MODE (SUBREG_REG (op0)) == fieldmode
359 || GET_MODE_CLASS (fieldmode) == MODE_INT
360 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT)
361 op0 = SUBREG_REG (op0);
362 else
363 /* Else we've got some float mode source being extracted into
364 a different float mode destination -- this combination of
365 subregs results in Severe Tire Damage. */
366 abort ();
369 emit_insn (GEN_FCN (icode)
370 (gen_rtx_SUBREG (fieldmode, op0, offset), value));
372 return value;
375 /* Handle fields bigger than a word. */
377 if (bitsize > BITS_PER_WORD)
379 /* Here we transfer the words of the field
380 in the order least significant first.
381 This is because the most significant word is the one which may
382 be less than full.
383 However, only do that if the value is not BLKmode. */
385 int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
387 int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
388 int i;
390 /* This is the mode we must force value to, so that there will be enough
391 subwords to extract. Note that fieldmode will often (always?) be
392 VOIDmode, because that is what store_field uses to indicate that this
393 is a bit field, but passing VOIDmode to operand_subword_force will
394 result in an abort. */
395 fieldmode = mode_for_size (nwords * BITS_PER_WORD, MODE_INT, 0);
397 for (i = 0; i < nwords; i++)
399 /* If I is 0, use the low-order word in both field and target;
400 if I is 1, use the next to lowest word; and so on. */
401 int wordnum = (backwards ? nwords - i - 1 : i);
402 int bit_offset = (backwards
403 ? MAX (bitsize - (i + 1) * BITS_PER_WORD, 0)
404 : i * BITS_PER_WORD);
405 store_bit_field (op0, MIN (BITS_PER_WORD,
406 bitsize - i * BITS_PER_WORD),
407 bitnum + bit_offset, word_mode,
408 operand_subword_force (value, wordnum,
409 (GET_MODE (value) == VOIDmode
410 ? fieldmode
411 : GET_MODE (value))),
412 align, total_size);
414 return value;
417 /* From here on we can assume that the field to be stored in is
418 a full-word (whatever type that is), since it is shorter than a word. */
420 /* OFFSET is the number of words or bytes (UNIT says which)
421 from STR_RTX to the first word or byte containing part of the field. */
423 if (GET_CODE (op0) != MEM)
425 if (offset != 0
426 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
428 if (GET_CODE (op0) != REG)
430 /* Since this is a destination (lvalue), we can't copy it to a
431 pseudo. We can trivially remove a SUBREG that does not
432 change the size of the operand. Such a SUBREG may have been
433 added above. Otherwise, abort. */
434 if (GET_CODE (op0) == SUBREG
435 && (GET_MODE_SIZE (GET_MODE (op0))
436 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))))
437 op0 = SUBREG_REG (op0);
438 else
439 abort ();
441 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
442 op0, offset);
444 offset = 0;
446 else
448 op0 = protect_from_queue (op0, 1);
451 /* If VALUE is a floating-point mode, access it as an integer of the
452 corresponding size. This can occur on a machine with 64 bit registers
453 that uses SFmode for float. This can also occur for unaligned float
454 structure fields. */
455 if (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT)
457 if (GET_CODE (value) != REG)
458 value = copy_to_reg (value);
459 value = gen_rtx_SUBREG (word_mode, value, 0);
462 /* Now OFFSET is nonzero only if OP0 is memory
463 and is therefore always measured in bytes. */
465 #ifdef HAVE_insv
466 if (HAVE_insv
467 && GET_MODE (value) != BLKmode
468 && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
469 /* Ensure insv's size is wide enough for this field. */
470 && (insv_bitsize >= bitsize)
471 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
472 && (bitsize + bitpos > insv_bitsize)))
474 int xbitpos = bitpos;
475 rtx value1;
476 rtx xop0 = op0;
477 rtx last = get_last_insn ();
478 rtx pat;
479 enum machine_mode maxmode;
480 int save_volatile_ok = volatile_ok;
482 maxmode = insn_data[(int) CODE_FOR_insv].operand[3].mode;
483 if (maxmode == VOIDmode)
484 maxmode = word_mode;
486 volatile_ok = 1;
488 /* If this machine's insv can only insert into a register, copy OP0
489 into a register and save it back later. */
490 /* This used to check flag_force_mem, but that was a serious
491 de-optimization now that flag_force_mem is enabled by -O2. */
492 if (GET_CODE (op0) == MEM
493 && ! ((*insn_data[(int) CODE_FOR_insv].operand[0].predicate)
494 (op0, VOIDmode)))
496 rtx tempreg;
497 enum machine_mode bestmode;
499 /* Get the mode to use for inserting into this field. If OP0 is
500 BLKmode, get the smallest mode consistent with the alignment. If
501 OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
502 mode. Otherwise, use the smallest mode containing the field. */
504 if (GET_MODE (op0) == BLKmode
505 || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
506 bestmode
507 = get_best_mode (bitsize, bitnum, align * BITS_PER_UNIT, maxmode,
508 MEM_VOLATILE_P (op0));
509 else
510 bestmode = GET_MODE (op0);
512 if (bestmode == VOIDmode
513 || (SLOW_UNALIGNED_ACCESS && GET_MODE_SIZE (bestmode) > align))
514 goto insv_loses;
516 /* Adjust address to point to the containing unit of that mode. */
517 unit = GET_MODE_BITSIZE (bestmode);
518 /* Compute offset as multiple of this unit, counting in bytes. */
519 offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
520 bitpos = bitnum % unit;
521 op0 = change_address (op0, bestmode,
522 plus_constant (XEXP (op0, 0), offset));
524 /* Fetch that unit, store the bitfield in it, then store the unit. */
525 tempreg = copy_to_reg (op0);
526 store_bit_field (tempreg, bitsize, bitpos, fieldmode, value,
527 align, total_size);
528 emit_move_insn (op0, tempreg);
529 return value;
531 volatile_ok = save_volatile_ok;
533 /* Add OFFSET into OP0's address. */
534 if (GET_CODE (xop0) == MEM)
535 xop0 = change_address (xop0, byte_mode,
536 plus_constant (XEXP (xop0, 0), offset));
538 /* If xop0 is a register, we need it in MAXMODE
539 to make it acceptable to the format of insv. */
540 if (GET_CODE (xop0) == SUBREG)
541 /* We can't just change the mode, because this might clobber op0,
542 and we will need the original value of op0 if insv fails. */
543 xop0 = gen_rtx_SUBREG (maxmode, SUBREG_REG (xop0), SUBREG_WORD (xop0));
544 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
545 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
547 /* On big-endian machines, we count bits from the most significant.
548 If the bit field insn does not, we must invert. */
550 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
551 xbitpos = unit - bitsize - xbitpos;
553 /* We have been counting XBITPOS within UNIT.
554 Count instead within the size of the register. */
555 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
556 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
558 unit = GET_MODE_BITSIZE (maxmode);
560 /* Convert VALUE to maxmode (which insv insn wants) in VALUE1. */
561 value1 = value;
562 if (GET_MODE (value) != maxmode)
564 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
566 /* Optimization: Don't bother really extending VALUE
567 if it has all the bits we will actually use. However,
568 if we must narrow it, be sure we do it correctly. */
570 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
572 /* Avoid making subreg of a subreg, or of a mem. */
573 if (GET_CODE (value1) != REG)
574 value1 = copy_to_reg (value1);
575 value1 = gen_rtx_SUBREG (maxmode, value1, 0);
577 else
578 value1 = gen_lowpart (maxmode, value1);
580 else if (!CONSTANT_P (value))
581 /* Parse phase is supposed to make VALUE's data type
582 match that of the component reference, which is a type
583 at least as wide as the field; so VALUE should have
584 a mode that corresponds to that type. */
585 abort ();
588 /* If this machine's insv insists on a register,
589 get VALUE1 into a register. */
590 if (! ((*insn_data[(int) CODE_FOR_insv].operand[3].predicate)
591 (value1, maxmode)))
592 value1 = force_reg (maxmode, value1);
594 pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
595 if (pat)
596 emit_insn (pat);
597 else
599 delete_insns_since (last);
600 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
603 else
604 insv_loses:
605 #endif
606 /* Insv is not available; store using shifts and boolean ops. */
607 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
608 return value;
611 /* Use shifts and boolean operations to store VALUE
612 into a bit field of width BITSIZE
613 in a memory location specified by OP0 except offset by OFFSET bytes.
614 (OFFSET must be 0 if OP0 is a register.)
615 The field starts at position BITPOS within the byte.
616 (If OP0 is a register, it may be a full word or a narrower mode,
617 but BITPOS still counts within a full word,
618 which is significant on bigendian machines.)
619 STRUCT_ALIGN is the alignment the structure is known to have (in bytes).
621 Note that protect_from_queue has already been done on OP0 and VALUE. */
623 static void
624 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, struct_align)
625 register rtx op0;
626 register int offset, bitsize, bitpos;
627 register rtx value;
628 int struct_align;
630 register enum machine_mode mode;
631 int total_bits = BITS_PER_WORD;
632 rtx subtarget, temp;
633 int all_zero = 0;
634 int all_one = 0;
636 if (! SLOW_UNALIGNED_ACCESS)
637 struct_align = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
639 /* There is a case not handled here:
640 a structure with a known alignment of just a halfword
641 and a field split across two aligned halfwords within the structure.
642 Or likewise a structure with a known alignment of just a byte
643 and a field split across two bytes.
644 Such cases are not supposed to be able to occur. */
646 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
648 if (offset != 0)
649 abort ();
650 /* Special treatment for a bit field split across two registers. */
651 if (bitsize + bitpos > BITS_PER_WORD)
653 store_split_bit_field (op0, bitsize, bitpos,
654 value, BITS_PER_WORD);
655 return;
658 else
660 /* Get the proper mode to use for this field. We want a mode that
661 includes the entire field. If such a mode would be larger than
662 a word, we won't be doing the extraction the normal way. */
664 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
665 struct_align * BITS_PER_UNIT, word_mode,
666 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
668 if (mode == VOIDmode)
670 /* The only way this should occur is if the field spans word
671 boundaries. */
672 store_split_bit_field (op0,
673 bitsize, bitpos + offset * BITS_PER_UNIT,
674 value, struct_align);
675 return;
678 total_bits = GET_MODE_BITSIZE (mode);
680 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
681 be in the range 0 to total_bits-1, and put any excess bytes in
682 OFFSET. */
683 if (bitpos >= total_bits)
685 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
686 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
687 * BITS_PER_UNIT);
690 /* Get ref to an aligned byte, halfword, or word containing the field.
691 Adjust BITPOS to be position within a word,
692 and OFFSET to be the offset of that word.
693 Then alter OP0 to refer to that word. */
694 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
695 offset -= (offset % (total_bits / BITS_PER_UNIT));
696 op0 = change_address (op0, mode,
697 plus_constant (XEXP (op0, 0), offset));
700 mode = GET_MODE (op0);
702 /* Now MODE is either some integral mode for a MEM as OP0,
703 or is a full-word for a REG as OP0. TOTAL_BITS corresponds.
704 The bit field is contained entirely within OP0.
705 BITPOS is the starting bit number within OP0.
706 (OP0's mode may actually be narrower than MODE.) */
708 if (BYTES_BIG_ENDIAN)
709 /* BITPOS is the distance between our msb
710 and that of the containing datum.
711 Convert it to the distance from the lsb. */
712 bitpos = total_bits - bitsize - bitpos;
714 /* Now BITPOS is always the distance between our lsb
715 and that of OP0. */
717 /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
718 we must first convert its mode to MODE. */
720 if (GET_CODE (value) == CONST_INT)
722 register HOST_WIDE_INT v = INTVAL (value);
724 if (bitsize < HOST_BITS_PER_WIDE_INT)
725 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
727 if (v == 0)
728 all_zero = 1;
729 else if ((bitsize < HOST_BITS_PER_WIDE_INT
730 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
731 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
732 all_one = 1;
734 value = lshift_value (mode, value, bitpos, bitsize);
736 else
738 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
739 && bitpos + bitsize != GET_MODE_BITSIZE (mode));
741 if (GET_MODE (value) != mode)
743 if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
744 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
745 value = gen_lowpart (mode, value);
746 else
747 value = convert_to_mode (mode, value, 1);
750 if (must_and)
751 value = expand_binop (mode, and_optab, value,
752 mask_rtx (mode, 0, bitsize, 0),
753 NULL_RTX, 1, OPTAB_LIB_WIDEN);
754 if (bitpos > 0)
755 value = expand_shift (LSHIFT_EXPR, mode, value,
756 build_int_2 (bitpos, 0), NULL_RTX, 1);
759 /* Now clear the chosen bits in OP0,
760 except that if VALUE is -1 we need not bother. */
762 subtarget = (GET_CODE (op0) == REG || ! flag_force_mem) ? op0 : 0;
764 if (! all_one)
766 temp = expand_binop (mode, and_optab, op0,
767 mask_rtx (mode, bitpos, bitsize, 1),
768 subtarget, 1, OPTAB_LIB_WIDEN);
769 subtarget = temp;
771 else
772 temp = op0;
774 /* Now logical-or VALUE into OP0, unless it is zero. */
776 if (! all_zero)
777 temp = expand_binop (mode, ior_optab, temp, value,
778 subtarget, 1, OPTAB_LIB_WIDEN);
779 if (op0 != temp)
780 emit_move_insn (op0, temp);
783 /* Store a bit field that is split across multiple accessible memory objects.
785 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
786 BITSIZE is the field width; BITPOS the position of its first bit
787 (within the word).
788 VALUE is the value to store.
789 ALIGN is the known alignment of OP0, measured in bytes.
790 This is also the size of the memory objects to be used.
792 This does not yet handle fields wider than BITS_PER_WORD. */
794 static void
795 store_split_bit_field (op0, bitsize, bitpos, value, align)
796 rtx op0;
797 int bitsize, bitpos;
798 rtx value;
799 int align;
801 int unit;
802 int bitsdone = 0;
804 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
805 much at a time. */
806 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
807 unit = BITS_PER_WORD;
808 else
809 unit = MIN (align * BITS_PER_UNIT, BITS_PER_WORD);
811 /* If VALUE is a constant other than a CONST_INT, get it into a register in
812 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
813 that VALUE might be a floating-point constant. */
814 if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
816 rtx word = gen_lowpart_common (word_mode, value);
818 if (word && (value != word))
819 value = word;
820 else
821 value = gen_lowpart_common (word_mode,
822 force_reg (GET_MODE (value) != VOIDmode
823 ? GET_MODE (value)
824 : word_mode, value));
826 else if (GET_CODE (value) == ADDRESSOF)
827 value = copy_to_reg (value);
829 while (bitsdone < bitsize)
831 int thissize;
832 rtx part, word;
833 int thispos;
834 int offset;
836 offset = (bitpos + bitsdone) / unit;
837 thispos = (bitpos + bitsdone) % unit;
839 /* THISSIZE must not overrun a word boundary. Otherwise,
840 store_fixed_bit_field will call us again, and we will mutually
841 recurse forever. */
842 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
843 thissize = MIN (thissize, unit - thispos);
845 if (BYTES_BIG_ENDIAN)
847 int total_bits;
849 /* We must do an endian conversion exactly the same way as it is
850 done in extract_bit_field, so that the two calls to
851 extract_fixed_bit_field will have comparable arguments. */
852 if (GET_CODE (value) != MEM || GET_MODE (value) == BLKmode)
853 total_bits = BITS_PER_WORD;
854 else
855 total_bits = GET_MODE_BITSIZE (GET_MODE (value));
857 /* Fetch successively less significant portions. */
858 if (GET_CODE (value) == CONST_INT)
859 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
860 >> (bitsize - bitsdone - thissize))
861 & (((HOST_WIDE_INT) 1 << thissize) - 1));
862 else
863 /* The args are chosen so that the last part includes the
864 lsb. Give extract_bit_field the value it needs (with
865 endianness compensation) to fetch the piece we want.
867 ??? We have no idea what the alignment of VALUE is, so
868 we have to use a guess. */
869 part
870 = extract_fixed_bit_field
871 (word_mode, value, 0, thissize,
872 total_bits - bitsize + bitsdone, NULL_RTX, 1,
873 GET_MODE (value) == VOIDmode
874 ? UNITS_PER_WORD
875 : (GET_MODE (value) == BLKmode
877 : GET_MODE_ALIGNMENT (GET_MODE (value)) / BITS_PER_UNIT));
879 else
881 /* Fetch successively more significant portions. */
882 if (GET_CODE (value) == CONST_INT)
883 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
884 >> bitsdone)
885 & (((HOST_WIDE_INT) 1 << thissize) - 1));
886 else
887 part
888 = extract_fixed_bit_field
889 (word_mode, value, 0, thissize, bitsdone, NULL_RTX, 1,
890 GET_MODE (value) == VOIDmode
891 ? UNITS_PER_WORD
892 : (GET_MODE (value) == BLKmode
894 : GET_MODE_ALIGNMENT (GET_MODE (value)) / BITS_PER_UNIT));
897 /* If OP0 is a register, then handle OFFSET here.
899 When handling multiword bitfields, extract_bit_field may pass
900 down a word_mode SUBREG of a larger REG for a bitfield that actually
901 crosses a word boundary. Thus, for a SUBREG, we must find
902 the current word starting from the base register. */
903 if (GET_CODE (op0) == SUBREG)
905 word = operand_subword_force (SUBREG_REG (op0),
906 SUBREG_WORD (op0) + offset,
907 GET_MODE (SUBREG_REG (op0)));
908 offset = 0;
910 else if (GET_CODE (op0) == REG)
912 word = operand_subword_force (op0, offset, GET_MODE (op0));
913 offset = 0;
915 else
916 word = op0;
918 /* OFFSET is in UNITs, and UNIT is in bits.
919 store_fixed_bit_field wants offset in bytes. */
920 store_fixed_bit_field (word, offset * unit / BITS_PER_UNIT,
921 thissize, thispos, part, align);
922 bitsdone += thissize;
926 /* Generate code to extract a byte-field from STR_RTX
927 containing BITSIZE bits, starting at BITNUM,
928 and put it in TARGET if possible (if TARGET is nonzero).
929 Regardless of TARGET, we return the rtx for where the value is placed.
930 It may be a QUEUED.
932 STR_RTX is the structure containing the byte (a REG or MEM).
933 UNSIGNEDP is nonzero if this is an unsigned bit field.
934 MODE is the natural mode of the field value once extracted.
935 TMODE is the mode the caller would like the value to have;
936 but the value may be returned with type MODE instead.
938 ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
939 TOTAL_SIZE is the size in bytes of the containing structure,
940 or -1 if varying.
942 If a TARGET is specified and we can store in it at no extra cost,
943 we do so, and return TARGET.
944 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
945 if they are equally easy. */
948 extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
949 target, mode, tmode, align, total_size)
950 rtx str_rtx;
951 register int bitsize;
952 int bitnum;
953 int unsignedp;
954 rtx target;
955 enum machine_mode mode, tmode;
956 int align;
957 int total_size;
959 int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
960 register int offset = bitnum / unit;
961 register int bitpos = bitnum % unit;
962 register rtx op0 = str_rtx;
963 rtx spec_target = target;
964 rtx spec_target_subreg = 0;
965 #ifdef HAVE_extv
966 int extv_bitsize;
967 enum machine_mode extv_mode;
968 #endif
969 #ifdef HAVE_extzv
970 int extzv_bitsize;
971 enum machine_mode extzv_mode;
972 #endif
974 #ifdef HAVE_extv
975 extv_mode = insn_data[(int) CODE_FOR_extv].operand[0].mode;
976 if (extv_mode == VOIDmode)
977 extv_mode = word_mode;
978 extv_bitsize = GET_MODE_BITSIZE (extv_mode);
979 #endif
981 #ifdef HAVE_extzv
982 extzv_mode = insn_data[(int) CODE_FOR_extzv].operand[0].mode;
983 if (extzv_mode == VOIDmode)
984 extzv_mode = word_mode;
985 extzv_bitsize = GET_MODE_BITSIZE (extzv_mode);
986 #endif
988 /* Discount the part of the structure before the desired byte.
989 We need to know how many bytes are safe to reference after it. */
990 if (total_size >= 0)
991 total_size -= (bitpos / BIGGEST_ALIGNMENT
992 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
994 if (tmode == VOIDmode)
995 tmode = mode;
996 while (GET_CODE (op0) == SUBREG)
998 int outer_size = GET_MODE_BITSIZE (GET_MODE (op0));
999 int inner_size = GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)));
1001 offset += SUBREG_WORD (op0);
1003 inner_size = MIN (inner_size, BITS_PER_WORD);
1005 if (BYTES_BIG_ENDIAN && (outer_size < inner_size))
1007 bitpos += inner_size - outer_size;
1008 if (bitpos > unit)
1010 offset += (bitpos / unit);
1011 bitpos %= unit;
1015 op0 = SUBREG_REG (op0);
1018 /* Make sure we are playing with integral modes. Pun with subregs
1019 if we aren't. */
1021 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
1022 if (imode != GET_MODE (op0))
1024 if (GET_CODE (op0) == MEM)
1025 op0 = change_address (op0, imode, NULL_RTX);
1026 else if (imode != BLKmode)
1027 op0 = gen_lowpart (imode, op0);
1028 else
1029 abort ();
1033 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1034 If that's wrong, the solution is to test for it and set TARGET to 0
1035 if needed. */
1037 /* If OP0 is a register, BITPOS must count within a word.
1038 But as we have it, it counts within whatever size OP0 now has.
1039 On a bigendian machine, these are not the same, so convert. */
1040 if (BYTES_BIG_ENDIAN
1041 && GET_CODE (op0) != MEM
1042 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
1043 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
1045 /* Extracting a full-word or multi-word value
1046 from a structure in a register or aligned memory.
1047 This can be done with just SUBREG.
1048 So too extracting a subword value in
1049 the least significant part of the register. */
1051 if (((GET_CODE (op0) != MEM
1052 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1053 GET_MODE_BITSIZE (GET_MODE (op0))))
1054 || (GET_CODE (op0) == MEM
1055 && (! SLOW_UNALIGNED_ACCESS
1056 || (offset * BITS_PER_UNIT % bitsize == 0
1057 && align * BITS_PER_UNIT % bitsize == 0))))
1058 && ((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
1059 && bitpos % BITS_PER_WORD == 0)
1060 || (mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0) != BLKmode
1061 /* ??? The big endian test here is wrong. This is correct
1062 if the value is in a register, and if mode_for_size is not
1063 the same mode as op0. This causes us to get unnecessarily
1064 inefficient code from the Thumb port when -mbig-endian. */
1065 && (BYTES_BIG_ENDIAN
1066 ? bitpos + bitsize == BITS_PER_WORD
1067 : bitpos == 0))))
1069 enum machine_mode mode1
1070 = mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0);
1072 if (mode1 != GET_MODE (op0))
1074 if (GET_CODE (op0) == SUBREG)
1076 if (GET_MODE (SUBREG_REG (op0)) == mode1
1077 || GET_MODE_CLASS (mode1) == MODE_INT
1078 || GET_MODE_CLASS (mode1) == MODE_PARTIAL_INT)
1079 op0 = SUBREG_REG (op0);
1080 else
1081 /* Else we've got some float mode source being extracted into
1082 a different float mode destination -- this combination of
1083 subregs results in Severe Tire Damage. */
1084 abort ();
1086 if (GET_CODE (op0) == REG)
1087 op0 = gen_rtx_SUBREG (mode1, op0, offset);
1088 else
1089 op0 = change_address (op0, mode1,
1090 plus_constant (XEXP (op0, 0), offset));
1092 if (mode1 != mode)
1093 return convert_to_mode (tmode, op0, unsignedp);
1094 return op0;
1097 /* Handle fields bigger than a word. */
1099 if (bitsize > BITS_PER_WORD)
1101 /* Here we transfer the words of the field
1102 in the order least significant first.
1103 This is because the most significant word is the one which may
1104 be less than full. */
1106 int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1107 int i;
1109 if (target == 0 || GET_CODE (target) != REG)
1110 target = gen_reg_rtx (mode);
1112 /* Indicate for flow that the entire target reg is being set. */
1113 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
1115 for (i = 0; i < nwords; i++)
1117 /* If I is 0, use the low-order word in both field and target;
1118 if I is 1, use the next to lowest word; and so on. */
1119 /* Word number in TARGET to use. */
1120 int wordnum = (WORDS_BIG_ENDIAN
1121 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1122 : i);
1123 /* Offset from start of field in OP0. */
1124 int bit_offset = (WORDS_BIG_ENDIAN
1125 ? MAX (0, bitsize - (i + 1) * BITS_PER_WORD)
1126 : i * BITS_PER_WORD);
1127 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1128 rtx result_part
1129 = extract_bit_field (op0, MIN (BITS_PER_WORD,
1130 bitsize - i * BITS_PER_WORD),
1131 bitnum + bit_offset,
1132 1, target_part, mode, word_mode,
1133 align, total_size);
1135 if (target_part == 0)
1136 abort ();
1138 if (result_part != target_part)
1139 emit_move_insn (target_part, result_part);
1142 if (unsignedp)
1144 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1145 need to be zero'd out. */
1146 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1148 int i,total_words;
1150 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1151 for (i = nwords; i < total_words; i++)
1153 int wordnum = WORDS_BIG_ENDIAN ? total_words - i - 1 : i;
1154 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1155 emit_move_insn (target_part, const0_rtx);
1158 return target;
1161 /* Signed bit field: sign-extend with two arithmetic shifts. */
1162 target = expand_shift (LSHIFT_EXPR, mode, target,
1163 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1164 NULL_RTX, 0);
1165 return expand_shift (RSHIFT_EXPR, mode, target,
1166 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1167 NULL_RTX, 0);
1170 /* From here on we know the desired field is smaller than a word
1171 so we can assume it is an integer. So we can safely extract it as one
1172 size of integer, if necessary, and then truncate or extend
1173 to the size that is wanted. */
1175 /* OFFSET is the number of words or bytes (UNIT says which)
1176 from STR_RTX to the first word or byte containing part of the field. */
1178 if (GET_CODE (op0) != MEM)
1180 if (offset != 0
1181 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1183 if (GET_CODE (op0) != REG)
1184 op0 = copy_to_reg (op0);
1185 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
1186 op0, offset);
1188 offset = 0;
1190 else
1192 op0 = protect_from_queue (str_rtx, 1);
1195 /* Now OFFSET is nonzero only for memory operands. */
1197 if (unsignedp)
1199 #ifdef HAVE_extzv
1200 if (HAVE_extzv
1201 && (extzv_bitsize >= bitsize)
1202 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
1203 && (bitsize + bitpos > extzv_bitsize)))
1205 int xbitpos = bitpos, xoffset = offset;
1206 rtx bitsize_rtx, bitpos_rtx;
1207 rtx last = get_last_insn ();
1208 rtx xop0 = op0;
1209 rtx xtarget = target;
1210 rtx xspec_target = spec_target;
1211 rtx xspec_target_subreg = spec_target_subreg;
1212 rtx pat;
1213 enum machine_mode maxmode;
1215 maxmode = insn_data[(int) CODE_FOR_extzv].operand[0].mode;
1216 if (maxmode == VOIDmode)
1217 maxmode = word_mode;
1219 if (GET_CODE (xop0) == MEM)
1221 int save_volatile_ok = volatile_ok;
1222 volatile_ok = 1;
1224 /* Is the memory operand acceptable? */
1225 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[1].predicate)
1226 (xop0, GET_MODE (xop0))))
1228 /* No, load into a reg and extract from there. */
1229 enum machine_mode bestmode;
1231 /* Get the mode to use for inserting into this field. If
1232 OP0 is BLKmode, get the smallest mode consistent with the
1233 alignment. If OP0 is a non-BLKmode object that is no
1234 wider than MAXMODE, use its mode. Otherwise, use the
1235 smallest mode containing the field. */
1237 if (GET_MODE (xop0) == BLKmode
1238 || (GET_MODE_SIZE (GET_MODE (op0))
1239 > GET_MODE_SIZE (maxmode)))
1240 bestmode = get_best_mode (bitsize, bitnum,
1241 align * BITS_PER_UNIT, maxmode,
1242 MEM_VOLATILE_P (xop0));
1243 else
1244 bestmode = GET_MODE (xop0);
1246 if (bestmode == VOIDmode
1247 || (SLOW_UNALIGNED_ACCESS && GET_MODE_SIZE (bestmode) > align))
1248 goto extzv_loses;
1250 /* Compute offset as multiple of this unit,
1251 counting in bytes. */
1252 unit = GET_MODE_BITSIZE (bestmode);
1253 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1254 xbitpos = bitnum % unit;
1255 xop0 = change_address (xop0, bestmode,
1256 plus_constant (XEXP (xop0, 0),
1257 xoffset));
1258 /* Fetch it to a register in that size. */
1259 xop0 = force_reg (bestmode, xop0);
1261 /* XBITPOS counts within UNIT, which is what is expected. */
1263 else
1264 /* Get ref to first byte containing part of the field. */
1265 xop0 = change_address (xop0, byte_mode,
1266 plus_constant (XEXP (xop0, 0), xoffset));
1268 volatile_ok = save_volatile_ok;
1271 /* If op0 is a register, we need it in MAXMODE (which is usually
1272 SImode). to make it acceptable to the format of extzv. */
1273 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1274 goto extzv_loses;
1275 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
1276 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1278 /* On big-endian machines, we count bits from the most significant.
1279 If the bit field insn does not, we must invert. */
1280 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1281 xbitpos = unit - bitsize - xbitpos;
1283 /* Now convert from counting within UNIT to counting in MAXMODE. */
1284 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
1285 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
1287 unit = GET_MODE_BITSIZE (maxmode);
1289 if (xtarget == 0
1290 || (flag_force_mem && GET_CODE (xtarget) == MEM))
1291 xtarget = xspec_target = gen_reg_rtx (tmode);
1293 if (GET_MODE (xtarget) != maxmode)
1295 if (GET_CODE (xtarget) == REG)
1297 int wider = (GET_MODE_SIZE (maxmode)
1298 > GET_MODE_SIZE (GET_MODE (xtarget)));
1299 xtarget = gen_lowpart (maxmode, xtarget);
1300 if (wider)
1301 xspec_target_subreg = xtarget;
1303 else
1304 xtarget = gen_reg_rtx (maxmode);
1307 /* If this machine's extzv insists on a register target,
1308 make sure we have one. */
1309 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[0].predicate)
1310 (xtarget, maxmode)))
1311 xtarget = gen_reg_rtx (maxmode);
1313 bitsize_rtx = GEN_INT (bitsize);
1314 bitpos_rtx = GEN_INT (xbitpos);
1316 pat = gen_extzv (protect_from_queue (xtarget, 1),
1317 xop0, bitsize_rtx, bitpos_rtx);
1318 if (pat)
1320 emit_insn (pat);
1321 target = xtarget;
1322 spec_target = xspec_target;
1323 spec_target_subreg = xspec_target_subreg;
1325 else
1327 delete_insns_since (last);
1328 target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
1329 bitpos, target, 1, align);
1332 else
1333 extzv_loses:
1334 #endif
1335 target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1336 target, 1, align);
1338 else
1340 #ifdef HAVE_extv
1341 if (HAVE_extv
1342 && (extv_bitsize >= bitsize)
1343 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
1344 && (bitsize + bitpos > extv_bitsize)))
1346 int xbitpos = bitpos, xoffset = offset;
1347 rtx bitsize_rtx, bitpos_rtx;
1348 rtx last = get_last_insn ();
1349 rtx xop0 = op0, xtarget = target;
1350 rtx xspec_target = spec_target;
1351 rtx xspec_target_subreg = spec_target_subreg;
1352 rtx pat;
1353 enum machine_mode maxmode;
1355 maxmode = insn_data[(int) CODE_FOR_extv].operand[0].mode;
1356 if (maxmode == VOIDmode)
1357 maxmode = word_mode;
1359 if (GET_CODE (xop0) == MEM)
1361 /* Is the memory operand acceptable? */
1362 if (! ((*insn_data[(int) CODE_FOR_extv].operand[1].predicate)
1363 (xop0, GET_MODE (xop0))))
1365 /* No, load into a reg and extract from there. */
1366 enum machine_mode bestmode;
1368 /* Get the mode to use for inserting into this field. If
1369 OP0 is BLKmode, get the smallest mode consistent with the
1370 alignment. If OP0 is a non-BLKmode object that is no
1371 wider than MAXMODE, use its mode. Otherwise, use the
1372 smallest mode containing the field. */
1374 if (GET_MODE (xop0) == BLKmode
1375 || (GET_MODE_SIZE (GET_MODE (op0))
1376 > GET_MODE_SIZE (maxmode)))
1377 bestmode = get_best_mode (bitsize, bitnum,
1378 align * BITS_PER_UNIT, maxmode,
1379 MEM_VOLATILE_P (xop0));
1380 else
1381 bestmode = GET_MODE (xop0);
1383 if (bestmode == VOIDmode
1384 || (SLOW_UNALIGNED_ACCESS && GET_MODE_SIZE (bestmode) > align))
1385 goto extv_loses;
1387 /* Compute offset as multiple of this unit,
1388 counting in bytes. */
1389 unit = GET_MODE_BITSIZE (bestmode);
1390 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1391 xbitpos = bitnum % unit;
1392 xop0 = change_address (xop0, bestmode,
1393 plus_constant (XEXP (xop0, 0),
1394 xoffset));
1395 /* Fetch it to a register in that size. */
1396 xop0 = force_reg (bestmode, xop0);
1398 /* XBITPOS counts within UNIT, which is what is expected. */
1400 else
1401 /* Get ref to first byte containing part of the field. */
1402 xop0 = change_address (xop0, byte_mode,
1403 plus_constant (XEXP (xop0, 0), xoffset));
1406 /* If op0 is a register, we need it in MAXMODE (which is usually
1407 SImode) to make it acceptable to the format of extv. */
1408 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1409 goto extv_loses;
1410 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
1411 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1413 /* On big-endian machines, we count bits from the most significant.
1414 If the bit field insn does not, we must invert. */
1415 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1416 xbitpos = unit - bitsize - xbitpos;
1418 /* XBITPOS counts within a size of UNIT.
1419 Adjust to count within a size of MAXMODE. */
1420 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
1421 xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
1423 unit = GET_MODE_BITSIZE (maxmode);
1425 if (xtarget == 0
1426 || (flag_force_mem && GET_CODE (xtarget) == MEM))
1427 xtarget = xspec_target = gen_reg_rtx (tmode);
1429 if (GET_MODE (xtarget) != maxmode)
1431 if (GET_CODE (xtarget) == REG)
1433 int wider = (GET_MODE_SIZE (maxmode)
1434 > GET_MODE_SIZE (GET_MODE (xtarget)));
1435 xtarget = gen_lowpart (maxmode, xtarget);
1436 if (wider)
1437 xspec_target_subreg = xtarget;
1439 else
1440 xtarget = gen_reg_rtx (maxmode);
1443 /* If this machine's extv insists on a register target,
1444 make sure we have one. */
1445 if (! ((*insn_data[(int) CODE_FOR_extv].operand[0].predicate)
1446 (xtarget, maxmode)))
1447 xtarget = gen_reg_rtx (maxmode);
1449 bitsize_rtx = GEN_INT (bitsize);
1450 bitpos_rtx = GEN_INT (xbitpos);
1452 pat = gen_extv (protect_from_queue (xtarget, 1),
1453 xop0, bitsize_rtx, bitpos_rtx);
1454 if (pat)
1456 emit_insn (pat);
1457 target = xtarget;
1458 spec_target = xspec_target;
1459 spec_target_subreg = xspec_target_subreg;
1461 else
1463 delete_insns_since (last);
1464 target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
1465 bitpos, target, 0, align);
1468 else
1469 extv_loses:
1470 #endif
1471 target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1472 target, 0, align);
1474 if (target == spec_target)
1475 return target;
1476 if (target == spec_target_subreg)
1477 return spec_target;
1478 if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
1480 /* If the target mode is floating-point, first convert to the
1481 integer mode of that size and then access it as a floating-point
1482 value via a SUBREG. */
1483 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1485 target = convert_to_mode (mode_for_size (GET_MODE_BITSIZE (tmode),
1486 MODE_INT, 0),
1487 target, unsignedp);
1488 if (GET_CODE (target) != REG)
1489 target = copy_to_reg (target);
1490 return gen_rtx_SUBREG (tmode, target, 0);
1492 else
1493 return convert_to_mode (tmode, target, unsignedp);
1495 return target;
1498 /* Extract a bit field using shifts and boolean operations
1499 Returns an rtx to represent the value.
1500 OP0 addresses a register (word) or memory (byte).
1501 BITPOS says which bit within the word or byte the bit field starts in.
1502 OFFSET says how many bytes farther the bit field starts;
1503 it is 0 if OP0 is a register.
1504 BITSIZE says how many bits long the bit field is.
1505 (If OP0 is a register, it may be narrower than a full word,
1506 but BITPOS still counts within a full word,
1507 which is significant on bigendian machines.)
1509 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1510 If TARGET is nonzero, attempts to store the value there
1511 and return TARGET, but this is not guaranteed.
1512 If TARGET is not used, create a pseudo-reg of mode TMODE for the value.
1514 ALIGN is the alignment that STR_RTX is known to have, measured in bytes. */
1516 static rtx
1517 extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1518 target, unsignedp, align)
1519 enum machine_mode tmode;
1520 register rtx op0, target;
1521 register int offset, bitsize, bitpos;
1522 int unsignedp;
1523 int align;
1525 int total_bits = BITS_PER_WORD;
1526 enum machine_mode mode;
1528 if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
1530 /* Special treatment for a bit field split across two registers. */
1531 if (bitsize + bitpos > BITS_PER_WORD)
1532 return extract_split_bit_field (op0, bitsize, bitpos,
1533 unsignedp, align);
1535 else
1537 /* Get the proper mode to use for this field. We want a mode that
1538 includes the entire field. If such a mode would be larger than
1539 a word, we won't be doing the extraction the normal way. */
1541 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1542 align * BITS_PER_UNIT, word_mode,
1543 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
1545 if (mode == VOIDmode)
1546 /* The only way this should occur is if the field spans word
1547 boundaries. */
1548 return extract_split_bit_field (op0, bitsize,
1549 bitpos + offset * BITS_PER_UNIT,
1550 unsignedp, align);
1552 total_bits = GET_MODE_BITSIZE (mode);
1554 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
1555 be in the range 0 to total_bits-1, and put any excess bytes in
1556 OFFSET. */
1557 if (bitpos >= total_bits)
1559 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
1560 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
1561 * BITS_PER_UNIT);
1564 /* Get ref to an aligned byte, halfword, or word containing the field.
1565 Adjust BITPOS to be position within a word,
1566 and OFFSET to be the offset of that word.
1567 Then alter OP0 to refer to that word. */
1568 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1569 offset -= (offset % (total_bits / BITS_PER_UNIT));
1570 op0 = change_address (op0, mode,
1571 plus_constant (XEXP (op0, 0), offset));
1574 mode = GET_MODE (op0);
1576 if (BYTES_BIG_ENDIAN)
1578 /* BITPOS is the distance between our msb and that of OP0.
1579 Convert it to the distance from the lsb. */
1581 bitpos = total_bits - bitsize - bitpos;
1584 /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1585 We have reduced the big-endian case to the little-endian case. */
1587 if (unsignedp)
1589 if (bitpos)
1591 /* If the field does not already start at the lsb,
1592 shift it so it does. */
1593 tree amount = build_int_2 (bitpos, 0);
1594 /* Maybe propagate the target for the shift. */
1595 /* But not if we will return it--could confuse integrate.c. */
1596 rtx subtarget = (target != 0 && GET_CODE (target) == REG
1597 && !REG_FUNCTION_VALUE_P (target)
1598 ? target : 0);
1599 if (tmode != mode) subtarget = 0;
1600 op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1602 /* Convert the value to the desired mode. */
1603 if (mode != tmode)
1604 op0 = convert_to_mode (tmode, op0, 1);
1606 /* Unless the msb of the field used to be the msb when we shifted,
1607 mask out the upper bits. */
1609 if (GET_MODE_BITSIZE (mode) != bitpos + bitsize
1610 #if 0
1611 #ifdef SLOW_ZERO_EXTEND
1612 /* Always generate an `and' if
1613 we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
1614 will combine fruitfully with the zero-extend. */
1615 || tmode != mode
1616 #endif
1617 #endif
1619 return expand_binop (GET_MODE (op0), and_optab, op0,
1620 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1621 target, 1, OPTAB_LIB_WIDEN);
1622 return op0;
1625 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1626 then arithmetic-shift its lsb to the lsb of the word. */
1627 op0 = force_reg (mode, op0);
1628 if (mode != tmode)
1629 target = 0;
1631 /* Find the narrowest integer mode that contains the field. */
1633 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1634 mode = GET_MODE_WIDER_MODE (mode))
1635 if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1637 op0 = convert_to_mode (mode, op0, 0);
1638 break;
1641 if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1643 tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
1644 /* Maybe propagate the target for the shift. */
1645 /* But not if we will return the result--could confuse integrate.c. */
1646 rtx subtarget = (target != 0 && GET_CODE (target) == REG
1647 && ! REG_FUNCTION_VALUE_P (target)
1648 ? target : 0);
1649 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1652 return expand_shift (RSHIFT_EXPR, mode, op0,
1653 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1654 target, 0);
1657 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1658 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1659 complement of that if COMPLEMENT. The mask is truncated if
1660 necessary to the width of mode MODE. The mask is zero-extended if
1661 BITSIZE+BITPOS is too small for MODE. */
1663 static rtx
1664 mask_rtx (mode, bitpos, bitsize, complement)
1665 enum machine_mode mode;
1666 int bitpos, bitsize, complement;
1668 HOST_WIDE_INT masklow, maskhigh;
1670 if (bitpos < HOST_BITS_PER_WIDE_INT)
1671 masklow = (HOST_WIDE_INT) -1 << bitpos;
1672 else
1673 masklow = 0;
1675 if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1676 masklow &= ((unsigned HOST_WIDE_INT) -1
1677 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1679 if (bitpos <= HOST_BITS_PER_WIDE_INT)
1680 maskhigh = -1;
1681 else
1682 maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1684 if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
1685 maskhigh &= ((unsigned HOST_WIDE_INT) -1
1686 >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1687 else
1688 maskhigh = 0;
1690 if (complement)
1692 maskhigh = ~maskhigh;
1693 masklow = ~masklow;
1696 return immed_double_const (masklow, maskhigh, mode);
1699 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1700 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1702 static rtx
1703 lshift_value (mode, value, bitpos, bitsize)
1704 enum machine_mode mode;
1705 rtx value;
1706 int bitpos, bitsize;
1708 unsigned HOST_WIDE_INT v = INTVAL (value);
1709 HOST_WIDE_INT low, high;
1711 if (bitsize < HOST_BITS_PER_WIDE_INT)
1712 v &= ~((HOST_WIDE_INT) -1 << bitsize);
1714 if (bitpos < HOST_BITS_PER_WIDE_INT)
1716 low = v << bitpos;
1717 high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
1719 else
1721 low = 0;
1722 high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
1725 return immed_double_const (low, high, mode);
1728 /* Extract a bit field that is split across two words
1729 and return an RTX for the result.
1731 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1732 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1733 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.
1735 ALIGN is the known alignment of OP0, measured in bytes.
1736 This is also the size of the memory objects to be used. */
1738 static rtx
1739 extract_split_bit_field (op0, bitsize, bitpos, unsignedp, align)
1740 rtx op0;
1741 int bitsize, bitpos, unsignedp, align;
1743 int unit;
1744 int bitsdone = 0;
1745 rtx result = NULL_RTX;
1746 int first = 1;
1748 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1749 much at a time. */
1750 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
1751 unit = BITS_PER_WORD;
1752 else
1753 unit = MIN (align * BITS_PER_UNIT, BITS_PER_WORD);
1755 while (bitsdone < bitsize)
1757 int thissize;
1758 rtx part, word;
1759 int thispos;
1760 int offset;
1762 offset = (bitpos + bitsdone) / unit;
1763 thispos = (bitpos + bitsdone) % unit;
1765 /* THISSIZE must not overrun a word boundary. Otherwise,
1766 extract_fixed_bit_field will call us again, and we will mutually
1767 recurse forever. */
1768 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1769 thissize = MIN (thissize, unit - thispos);
1771 /* If OP0 is a register, then handle OFFSET here.
1773 When handling multiword bitfields, extract_bit_field may pass
1774 down a word_mode SUBREG of a larger REG for a bitfield that actually
1775 crosses a word boundary. Thus, for a SUBREG, we must find
1776 the current word starting from the base register. */
1777 if (GET_CODE (op0) == SUBREG)
1779 word = operand_subword_force (SUBREG_REG (op0),
1780 SUBREG_WORD (op0) + offset,
1781 GET_MODE (SUBREG_REG (op0)));
1782 offset = 0;
1784 else if (GET_CODE (op0) == REG)
1786 word = operand_subword_force (op0, offset, GET_MODE (op0));
1787 offset = 0;
1789 else
1790 word = op0;
1792 /* Extract the parts in bit-counting order,
1793 whose meaning is determined by BYTES_PER_UNIT.
1794 OFFSET is in UNITs, and UNIT is in bits.
1795 extract_fixed_bit_field wants offset in bytes. */
1796 part = extract_fixed_bit_field (word_mode, word,
1797 offset * unit / BITS_PER_UNIT,
1798 thissize, thispos, 0, 1, align);
1799 bitsdone += thissize;
1801 /* Shift this part into place for the result. */
1802 if (BYTES_BIG_ENDIAN)
1804 if (bitsize != bitsdone)
1805 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1806 build_int_2 (bitsize - bitsdone, 0), 0, 1);
1808 else
1810 if (bitsdone != thissize)
1811 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1812 build_int_2 (bitsdone - thissize, 0), 0, 1);
1815 if (first)
1816 result = part;
1817 else
1818 /* Combine the parts with bitwise or. This works
1819 because we extracted each part as an unsigned bit field. */
1820 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
1821 OPTAB_LIB_WIDEN);
1823 first = 0;
1826 /* Unsigned bit field: we are done. */
1827 if (unsignedp)
1828 return result;
1829 /* Signed bit field: sign-extend with two arithmetic shifts. */
1830 result = expand_shift (LSHIFT_EXPR, word_mode, result,
1831 build_int_2 (BITS_PER_WORD - bitsize, 0),
1832 NULL_RTX, 0);
1833 return expand_shift (RSHIFT_EXPR, word_mode, result,
1834 build_int_2 (BITS_PER_WORD - bitsize, 0), NULL_RTX, 0);
1837 /* Add INC into TARGET. */
1839 void
1840 expand_inc (target, inc)
1841 rtx target, inc;
1843 rtx value = expand_binop (GET_MODE (target), add_optab,
1844 target, inc,
1845 target, 0, OPTAB_LIB_WIDEN);
1846 if (value != target)
1847 emit_move_insn (target, value);
1850 /* Subtract DEC from TARGET. */
1852 void
1853 expand_dec (target, dec)
1854 rtx target, dec;
1856 rtx value = expand_binop (GET_MODE (target), sub_optab,
1857 target, dec,
1858 target, 0, OPTAB_LIB_WIDEN);
1859 if (value != target)
1860 emit_move_insn (target, value);
1863 /* Output a shift instruction for expression code CODE,
1864 with SHIFTED being the rtx for the value to shift,
1865 and AMOUNT the tree for the amount to shift by.
1866 Store the result in the rtx TARGET, if that is convenient.
1867 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
1868 Return the rtx for where the value is. */
1871 expand_shift (code, mode, shifted, amount, target, unsignedp)
1872 enum tree_code code;
1873 register enum machine_mode mode;
1874 rtx shifted;
1875 tree amount;
1876 register rtx target;
1877 int unsignedp;
1879 register rtx op1, temp = 0;
1880 register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
1881 register int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
1882 int try;
1884 /* Previously detected shift-counts computed by NEGATE_EXPR
1885 and shifted in the other direction; but that does not work
1886 on all machines. */
1888 op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
1890 #ifdef SHIFT_COUNT_TRUNCATED
1891 if (SHIFT_COUNT_TRUNCATED)
1893 if (GET_CODE (op1) == CONST_INT
1894 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
1895 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode)))
1896 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
1897 % GET_MODE_BITSIZE (mode));
1898 else if (GET_CODE (op1) == SUBREG
1899 && SUBREG_WORD (op1) == 0)
1900 op1 = SUBREG_REG (op1);
1902 #endif
1904 if (op1 == const0_rtx)
1905 return shifted;
1907 for (try = 0; temp == 0 && try < 3; try++)
1909 enum optab_methods methods;
1911 if (try == 0)
1912 methods = OPTAB_DIRECT;
1913 else if (try == 1)
1914 methods = OPTAB_WIDEN;
1915 else
1916 methods = OPTAB_LIB_WIDEN;
1918 if (rotate)
1920 /* Widening does not work for rotation. */
1921 if (methods == OPTAB_WIDEN)
1922 continue;
1923 else if (methods == OPTAB_LIB_WIDEN)
1925 /* If we have been unable to open-code this by a rotation,
1926 do it as the IOR of two shifts. I.e., to rotate A
1927 by N bits, compute (A << N) | ((unsigned) A >> (C - N))
1928 where C is the bitsize of A.
1930 It is theoretically possible that the target machine might
1931 not be able to perform either shift and hence we would
1932 be making two libcalls rather than just the one for the
1933 shift (similarly if IOR could not be done). We will allow
1934 this extremely unlikely lossage to avoid complicating the
1935 code below. */
1937 rtx subtarget = target == shifted ? 0 : target;
1938 rtx temp1;
1939 tree type = TREE_TYPE (amount);
1940 tree new_amount = make_tree (type, op1);
1941 tree other_amount
1942 = fold (build (MINUS_EXPR, type,
1943 convert (type,
1944 build_int_2 (GET_MODE_BITSIZE (mode),
1945 0)),
1946 amount));
1948 shifted = force_reg (mode, shifted);
1950 temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
1951 mode, shifted, new_amount, subtarget, 1);
1952 temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
1953 mode, shifted, other_amount, 0, 1);
1954 return expand_binop (mode, ior_optab, temp, temp1, target,
1955 unsignedp, methods);
1958 temp = expand_binop (mode,
1959 left ? rotl_optab : rotr_optab,
1960 shifted, op1, target, unsignedp, methods);
1962 /* If we don't have the rotate, but we are rotating by a constant
1963 that is in range, try a rotate in the opposite direction. */
1965 if (temp == 0 && GET_CODE (op1) == CONST_INT
1966 && INTVAL (op1) > 0 && INTVAL (op1) < GET_MODE_BITSIZE (mode))
1967 temp = expand_binop (mode,
1968 left ? rotr_optab : rotl_optab,
1969 shifted,
1970 GEN_INT (GET_MODE_BITSIZE (mode)
1971 - INTVAL (op1)),
1972 target, unsignedp, methods);
1974 else if (unsignedp)
1975 temp = expand_binop (mode,
1976 left ? ashl_optab : lshr_optab,
1977 shifted, op1, target, unsignedp, methods);
1979 /* Do arithmetic shifts.
1980 Also, if we are going to widen the operand, we can just as well
1981 use an arithmetic right-shift instead of a logical one. */
1982 if (temp == 0 && ! rotate
1983 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
1985 enum optab_methods methods1 = methods;
1987 /* If trying to widen a log shift to an arithmetic shift,
1988 don't accept an arithmetic shift of the same size. */
1989 if (unsignedp)
1990 methods1 = OPTAB_MUST_WIDEN;
1992 /* Arithmetic shift */
1994 temp = expand_binop (mode,
1995 left ? ashl_optab : ashr_optab,
1996 shifted, op1, target, unsignedp, methods1);
1999 /* We used to try extzv here for logical right shifts, but that was
2000 only useful for one machine, the VAX, and caused poor code
2001 generation there for lshrdi3, so the code was deleted and a
2002 define_expand for lshrsi3 was added to vax.md. */
2005 if (temp == 0)
2006 abort ();
2007 return temp;
2010 enum alg_code { alg_zero, alg_m, alg_shift,
2011 alg_add_t_m2, alg_sub_t_m2,
2012 alg_add_factor, alg_sub_factor,
2013 alg_add_t2_m, alg_sub_t2_m,
2014 alg_add, alg_subtract, alg_factor, alg_shiftop };
2016 /* This structure records a sequence of operations.
2017 `ops' is the number of operations recorded.
2018 `cost' is their total cost.
2019 The operations are stored in `op' and the corresponding
2020 logarithms of the integer coefficients in `log'.
2022 These are the operations:
2023 alg_zero total := 0;
2024 alg_m total := multiplicand;
2025 alg_shift total := total * coeff
2026 alg_add_t_m2 total := total + multiplicand * coeff;
2027 alg_sub_t_m2 total := total - multiplicand * coeff;
2028 alg_add_factor total := total * coeff + total;
2029 alg_sub_factor total := total * coeff - total;
2030 alg_add_t2_m total := total * coeff + multiplicand;
2031 alg_sub_t2_m total := total * coeff - multiplicand;
2033 The first operand must be either alg_zero or alg_m. */
2035 struct algorithm
2037 short cost;
2038 short ops;
2039 /* The size of the OP and LOG fields are not directly related to the
2040 word size, but the worst-case algorithms will be if we have few
2041 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
2042 In that case we will generate shift-by-2, add, shift-by-2, add,...,
2043 in total wordsize operations. */
2044 enum alg_code op[MAX_BITS_PER_WORD];
2045 char log[MAX_BITS_PER_WORD];
2048 static void synth_mult PROTO((struct algorithm *,
2049 unsigned HOST_WIDE_INT,
2050 int));
2051 static unsigned HOST_WIDE_INT choose_multiplier PROTO((unsigned HOST_WIDE_INT,
2052 int, int,
2053 unsigned HOST_WIDE_INT *,
2054 int *, int *));
2055 static unsigned HOST_WIDE_INT invert_mod2n PROTO((unsigned HOST_WIDE_INT,
2056 int));
2057 /* Compute and return the best algorithm for multiplying by T.
2058 The algorithm must cost less than cost_limit
2059 If retval.cost >= COST_LIMIT, no algorithm was found and all
2060 other field of the returned struct are undefined. */
2062 static void
2063 synth_mult (alg_out, t, cost_limit)
2064 struct algorithm *alg_out;
2065 unsigned HOST_WIDE_INT t;
2066 int cost_limit;
2068 int m;
2069 struct algorithm *alg_in, *best_alg;
2070 int cost;
2071 unsigned HOST_WIDE_INT q;
2073 /* Indicate that no algorithm is yet found. If no algorithm
2074 is found, this value will be returned and indicate failure. */
2075 alg_out->cost = cost_limit;
2077 if (cost_limit <= 0)
2078 return;
2080 /* t == 1 can be done in zero cost. */
2081 if (t == 1)
2083 alg_out->ops = 1;
2084 alg_out->cost = 0;
2085 alg_out->op[0] = alg_m;
2086 return;
2089 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2090 fail now. */
2091 if (t == 0)
2093 if (zero_cost >= cost_limit)
2094 return;
2095 else
2097 alg_out->ops = 1;
2098 alg_out->cost = zero_cost;
2099 alg_out->op[0] = alg_zero;
2100 return;
2104 /* We'll be needing a couple extra algorithm structures now. */
2106 alg_in = (struct algorithm *)alloca (sizeof (struct algorithm));
2107 best_alg = (struct algorithm *)alloca (sizeof (struct algorithm));
2109 /* If we have a group of zero bits at the low-order part of T, try
2110 multiplying by the remaining bits and then doing a shift. */
2112 if ((t & 1) == 0)
2114 m = floor_log2 (t & -t); /* m = number of low zero bits */
2115 q = t >> m;
2116 cost = shift_cost[m];
2117 synth_mult (alg_in, q, cost_limit - cost);
2119 cost += alg_in->cost;
2120 if (cost < cost_limit)
2122 struct algorithm *x;
2123 x = alg_in, alg_in = best_alg, best_alg = x;
2124 best_alg->log[best_alg->ops] = m;
2125 best_alg->op[best_alg->ops] = alg_shift;
2126 cost_limit = cost;
2130 /* If we have an odd number, add or subtract one. */
2131 if ((t & 1) != 0)
2133 unsigned HOST_WIDE_INT w;
2135 for (w = 1; (w & t) != 0; w <<= 1)
2137 /* If T was -1, then W will be zero after the loop. This is another
2138 case where T ends with ...111. Handling this with (T + 1) and
2139 subtract 1 produces slightly better code and results in algorithm
2140 selection much faster than treating it like the ...0111 case
2141 below. */
2142 if (w == 0
2143 || (w > 2
2144 /* Reject the case where t is 3.
2145 Thus we prefer addition in that case. */
2146 && t != 3))
2148 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2150 cost = add_cost;
2151 synth_mult (alg_in, t + 1, cost_limit - cost);
2153 cost += alg_in->cost;
2154 if (cost < cost_limit)
2156 struct algorithm *x;
2157 x = alg_in, alg_in = best_alg, best_alg = x;
2158 best_alg->log[best_alg->ops] = 0;
2159 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2160 cost_limit = cost;
2163 else
2165 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2167 cost = add_cost;
2168 synth_mult (alg_in, t - 1, cost_limit - cost);
2170 cost += alg_in->cost;
2171 if (cost < cost_limit)
2173 struct algorithm *x;
2174 x = alg_in, alg_in = best_alg, best_alg = x;
2175 best_alg->log[best_alg->ops] = 0;
2176 best_alg->op[best_alg->ops] = alg_add_t_m2;
2177 cost_limit = cost;
2182 /* Look for factors of t of the form
2183 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2184 If we find such a factor, we can multiply by t using an algorithm that
2185 multiplies by q, shift the result by m and add/subtract it to itself.
2187 We search for large factors first and loop down, even if large factors
2188 are less probable than small; if we find a large factor we will find a
2189 good sequence quickly, and therefore be able to prune (by decreasing
2190 COST_LIMIT) the search. */
2192 for (m = floor_log2 (t - 1); m >= 2; m--)
2194 unsigned HOST_WIDE_INT d;
2196 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2197 if (t % d == 0 && t > d)
2199 cost = MIN (shiftadd_cost[m], add_cost + shift_cost[m]);
2200 synth_mult (alg_in, t / d, cost_limit - cost);
2202 cost += alg_in->cost;
2203 if (cost < cost_limit)
2205 struct algorithm *x;
2206 x = alg_in, alg_in = best_alg, best_alg = x;
2207 best_alg->log[best_alg->ops] = m;
2208 best_alg->op[best_alg->ops] = alg_add_factor;
2209 cost_limit = cost;
2211 /* Other factors will have been taken care of in the recursion. */
2212 break;
2215 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2216 if (t % d == 0 && t > d)
2218 cost = MIN (shiftsub_cost[m], add_cost + shift_cost[m]);
2219 synth_mult (alg_in, t / d, cost_limit - cost);
2221 cost += alg_in->cost;
2222 if (cost < cost_limit)
2224 struct algorithm *x;
2225 x = alg_in, alg_in = best_alg, best_alg = x;
2226 best_alg->log[best_alg->ops] = m;
2227 best_alg->op[best_alg->ops] = alg_sub_factor;
2228 cost_limit = cost;
2230 break;
2234 /* Try shift-and-add (load effective address) instructions,
2235 i.e. do a*3, a*5, a*9. */
2236 if ((t & 1) != 0)
2238 q = t - 1;
2239 q = q & -q;
2240 m = exact_log2 (q);
2241 if (m >= 0)
2243 cost = shiftadd_cost[m];
2244 synth_mult (alg_in, (t - 1) >> m, cost_limit - cost);
2246 cost += alg_in->cost;
2247 if (cost < cost_limit)
2249 struct algorithm *x;
2250 x = alg_in, alg_in = best_alg, best_alg = x;
2251 best_alg->log[best_alg->ops] = m;
2252 best_alg->op[best_alg->ops] = alg_add_t2_m;
2253 cost_limit = cost;
2257 q = t + 1;
2258 q = q & -q;
2259 m = exact_log2 (q);
2260 if (m >= 0)
2262 cost = shiftsub_cost[m];
2263 synth_mult (alg_in, (t + 1) >> m, cost_limit - cost);
2265 cost += alg_in->cost;
2266 if (cost < cost_limit)
2268 struct algorithm *x;
2269 x = alg_in, alg_in = best_alg, best_alg = x;
2270 best_alg->log[best_alg->ops] = m;
2271 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2272 cost_limit = cost;
2277 /* If cost_limit has not decreased since we stored it in alg_out->cost,
2278 we have not found any algorithm. */
2279 if (cost_limit == alg_out->cost)
2280 return;
2282 /* If we are getting a too long sequence for `struct algorithm'
2283 to record, make this search fail. */
2284 if (best_alg->ops == MAX_BITS_PER_WORD)
2285 return;
2287 /* Copy the algorithm from temporary space to the space at alg_out.
2288 We avoid using structure assignment because the majority of
2289 best_alg is normally undefined, and this is a critical function. */
2290 alg_out->ops = best_alg->ops + 1;
2291 alg_out->cost = cost_limit;
2292 bcopy ((char *) best_alg->op, (char *) alg_out->op,
2293 alg_out->ops * sizeof *alg_out->op);
2294 bcopy ((char *) best_alg->log, (char *) alg_out->log,
2295 alg_out->ops * sizeof *alg_out->log);
2298 /* Perform a multiplication and return an rtx for the result.
2299 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
2300 TARGET is a suggestion for where to store the result (an rtx).
2302 We check specially for a constant integer as OP1.
2303 If you want this check for OP0 as well, then before calling
2304 you should swap the two operands if OP0 would be constant. */
2307 expand_mult (mode, op0, op1, target, unsignedp)
2308 enum machine_mode mode;
2309 register rtx op0, op1, target;
2310 int unsignedp;
2312 rtx const_op1 = op1;
2314 /* synth_mult does an `unsigned int' multiply. As long as the mode is
2315 less than or equal in size to `unsigned int' this doesn't matter.
2316 If the mode is larger than `unsigned int', then synth_mult works only
2317 if the constant value exactly fits in an `unsigned int' without any
2318 truncation. This means that multiplying by negative values does
2319 not work; results are off by 2^32 on a 32 bit machine. */
2321 /* If we are multiplying in DImode, it may still be a win
2322 to try to work with shifts and adds. */
2323 if (GET_CODE (op1) == CONST_DOUBLE
2324 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
2325 && HOST_BITS_PER_INT >= BITS_PER_WORD
2326 && CONST_DOUBLE_HIGH (op1) == 0)
2327 const_op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2328 else if (HOST_BITS_PER_INT < GET_MODE_BITSIZE (mode)
2329 && GET_CODE (op1) == CONST_INT
2330 && INTVAL (op1) < 0)
2331 const_op1 = 0;
2333 /* We used to test optimize here, on the grounds that it's better to
2334 produce a smaller program when -O is not used.
2335 But this causes such a terrible slowdown sometimes
2336 that it seems better to use synth_mult always. */
2338 if (const_op1 && GET_CODE (const_op1) == CONST_INT)
2340 struct algorithm alg;
2341 struct algorithm alg2;
2342 HOST_WIDE_INT val = INTVAL (op1);
2343 HOST_WIDE_INT val_so_far;
2344 rtx insn;
2345 int mult_cost;
2346 enum {basic_variant, negate_variant, add_variant} variant = basic_variant;
2348 /* Try to do the computation three ways: multiply by the negative of OP1
2349 and then negate, do the multiplication directly, or do multiplication
2350 by OP1 - 1. */
2352 mult_cost = rtx_cost (gen_rtx_MULT (mode, op0, op1), SET);
2353 mult_cost = MIN (12 * add_cost, mult_cost);
2355 synth_mult (&alg, val, mult_cost);
2357 /* This works only if the inverted value actually fits in an
2358 `unsigned int' */
2359 if (HOST_BITS_PER_INT >= GET_MODE_BITSIZE (mode))
2361 synth_mult (&alg2, - val,
2362 (alg.cost < mult_cost ? alg.cost : mult_cost) - negate_cost);
2363 if (alg2.cost + negate_cost < alg.cost)
2364 alg = alg2, variant = negate_variant;
2367 /* This proves very useful for division-by-constant. */
2368 synth_mult (&alg2, val - 1,
2369 (alg.cost < mult_cost ? alg.cost : mult_cost) - add_cost);
2370 if (alg2.cost + add_cost < alg.cost)
2371 alg = alg2, variant = add_variant;
2373 if (alg.cost < mult_cost)
2375 /* We found something cheaper than a multiply insn. */
2376 int opno;
2377 rtx accum, tem;
2379 op0 = protect_from_queue (op0, 0);
2381 /* Avoid referencing memory over and over.
2382 For speed, but also for correctness when mem is volatile. */
2383 if (GET_CODE (op0) == MEM)
2384 op0 = force_reg (mode, op0);
2386 /* ACCUM starts out either as OP0 or as a zero, depending on
2387 the first operation. */
2389 if (alg.op[0] == alg_zero)
2391 accum = copy_to_mode_reg (mode, const0_rtx);
2392 val_so_far = 0;
2394 else if (alg.op[0] == alg_m)
2396 accum = copy_to_mode_reg (mode, op0);
2397 val_so_far = 1;
2399 else
2400 abort ();
2402 for (opno = 1; opno < alg.ops; opno++)
2404 int log = alg.log[opno];
2405 int preserve = preserve_subexpressions_p ();
2406 rtx shift_subtarget = preserve ? 0 : accum;
2407 rtx add_target
2408 = (opno == alg.ops - 1 && target != 0 && variant != add_variant
2409 && ! preserve)
2410 ? target : 0;
2411 rtx accum_target = preserve ? 0 : accum;
2413 switch (alg.op[opno])
2415 case alg_shift:
2416 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2417 build_int_2 (log, 0), NULL_RTX, 0);
2418 val_so_far <<= log;
2419 break;
2421 case alg_add_t_m2:
2422 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2423 build_int_2 (log, 0), NULL_RTX, 0);
2424 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2425 add_target
2426 ? add_target : accum_target);
2427 val_so_far += (HOST_WIDE_INT) 1 << log;
2428 break;
2430 case alg_sub_t_m2:
2431 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2432 build_int_2 (log, 0), NULL_RTX, 0);
2433 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
2434 add_target
2435 ? add_target : accum_target);
2436 val_so_far -= (HOST_WIDE_INT) 1 << log;
2437 break;
2439 case alg_add_t2_m:
2440 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2441 build_int_2 (log, 0), shift_subtarget,
2443 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
2444 add_target
2445 ? add_target : accum_target);
2446 val_so_far = (val_so_far << log) + 1;
2447 break;
2449 case alg_sub_t2_m:
2450 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2451 build_int_2 (log, 0), shift_subtarget,
2453 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
2454 add_target
2455 ? add_target : accum_target);
2456 val_so_far = (val_so_far << log) - 1;
2457 break;
2459 case alg_add_factor:
2460 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2461 build_int_2 (log, 0), NULL_RTX, 0);
2462 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2463 add_target
2464 ? add_target : accum_target);
2465 val_so_far += val_so_far << log;
2466 break;
2468 case alg_sub_factor:
2469 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2470 build_int_2 (log, 0), NULL_RTX, 0);
2471 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
2472 (add_target ? add_target
2473 : preserve ? 0 : tem));
2474 val_so_far = (val_so_far << log) - val_so_far;
2475 break;
2477 default:
2478 abort ();
2481 /* Write a REG_EQUAL note on the last insn so that we can cse
2482 multiplication sequences. */
2484 insn = get_last_insn ();
2485 set_unique_reg_note (insn,
2486 REG_EQUAL,
2487 gen_rtx_MULT (mode, op0,
2488 GEN_INT (val_so_far)));
2491 if (variant == negate_variant)
2493 val_so_far = - val_so_far;
2494 accum = expand_unop (mode, neg_optab, accum, target, 0);
2496 else if (variant == add_variant)
2498 val_so_far = val_so_far + 1;
2499 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
2502 if (val != val_so_far)
2503 abort ();
2505 return accum;
2509 /* This used to use umul_optab if unsigned, but for non-widening multiply
2510 there is no difference between signed and unsigned. */
2511 op0 = expand_binop (mode, smul_optab,
2512 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
2513 if (op0 == 0)
2514 abort ();
2515 return op0;
2518 /* Return the smallest n such that 2**n >= X. */
2521 ceil_log2 (x)
2522 unsigned HOST_WIDE_INT x;
2524 return floor_log2 (x - 1) + 1;
2527 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
2528 replace division by D, and put the least significant N bits of the result
2529 in *MULTIPLIER_PTR and return the most significant bit.
2531 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
2532 needed precision is in PRECISION (should be <= N).
2534 PRECISION should be as small as possible so this function can choose
2535 multiplier more freely.
2537 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
2538 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
2540 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
2541 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
2543 static
2544 unsigned HOST_WIDE_INT
2545 choose_multiplier (d, n, precision, multiplier_ptr, post_shift_ptr, lgup_ptr)
2546 unsigned HOST_WIDE_INT d;
2547 int n;
2548 int precision;
2549 unsigned HOST_WIDE_INT *multiplier_ptr;
2550 int *post_shift_ptr;
2551 int *lgup_ptr;
2553 unsigned HOST_WIDE_INT mhigh_hi, mhigh_lo;
2554 unsigned HOST_WIDE_INT mlow_hi, mlow_lo;
2555 int lgup, post_shift;
2556 int pow, pow2;
2557 unsigned HOST_WIDE_INT nh, nl, dummy1, dummy2;
2559 /* lgup = ceil(log2(divisor)); */
2560 lgup = ceil_log2 (d);
2562 if (lgup > n)
2563 abort ();
2565 pow = n + lgup;
2566 pow2 = n + lgup - precision;
2568 if (pow == 2 * HOST_BITS_PER_WIDE_INT)
2570 /* We could handle this with some effort, but this case is much better
2571 handled directly with a scc insn, so rely on caller using that. */
2572 abort ();
2575 /* mlow = 2^(N + lgup)/d */
2576 if (pow >= HOST_BITS_PER_WIDE_INT)
2578 nh = (unsigned HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
2579 nl = 0;
2581 else
2583 nh = 0;
2584 nl = (unsigned HOST_WIDE_INT) 1 << pow;
2586 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
2587 &mlow_lo, &mlow_hi, &dummy1, &dummy2);
2589 /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
2590 if (pow2 >= HOST_BITS_PER_WIDE_INT)
2591 nh |= (unsigned HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
2592 else
2593 nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
2594 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
2595 &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
2597 if (mhigh_hi && nh - d >= d)
2598 abort ();
2599 if (mhigh_hi > 1 || mlow_hi > 1)
2600 abort ();
2601 /* assert that mlow < mhigh. */
2602 if (! (mlow_hi < mhigh_hi || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo)))
2603 abort();
2605 /* If precision == N, then mlow, mhigh exceed 2^N
2606 (but they do not exceed 2^(N+1)). */
2608 /* Reduce to lowest terms */
2609 for (post_shift = lgup; post_shift > 0; post_shift--)
2611 unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
2612 unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
2613 if (ml_lo >= mh_lo)
2614 break;
2616 mlow_hi = 0;
2617 mlow_lo = ml_lo;
2618 mhigh_hi = 0;
2619 mhigh_lo = mh_lo;
2622 *post_shift_ptr = post_shift;
2623 *lgup_ptr = lgup;
2624 if (n < HOST_BITS_PER_WIDE_INT)
2626 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
2627 *multiplier_ptr = mhigh_lo & mask;
2628 return mhigh_lo >= mask;
2630 else
2632 *multiplier_ptr = mhigh_lo;
2633 return mhigh_hi;
2637 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
2638 congruent to 1 (mod 2**N). */
2640 static unsigned HOST_WIDE_INT
2641 invert_mod2n (x, n)
2642 unsigned HOST_WIDE_INT x;
2643 int n;
2645 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
2647 /* The algorithm notes that the choice y = x satisfies
2648 x*y == 1 mod 2^3, since x is assumed odd.
2649 Each iteration doubles the number of bits of significance in y. */
2651 unsigned HOST_WIDE_INT mask;
2652 unsigned HOST_WIDE_INT y = x;
2653 int nbit = 3;
2655 mask = (n == HOST_BITS_PER_WIDE_INT
2656 ? ~(unsigned HOST_WIDE_INT) 0
2657 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
2659 while (nbit < n)
2661 y = y * (2 - x*y) & mask; /* Modulo 2^N */
2662 nbit *= 2;
2664 return y;
2667 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
2668 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
2669 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
2670 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
2671 become signed.
2673 The result is put in TARGET if that is convenient.
2675 MODE is the mode of operation. */
2678 expand_mult_highpart_adjust (mode, adj_operand, op0, op1, target, unsignedp)
2679 enum machine_mode mode;
2680 register rtx adj_operand, op0, op1, target;
2681 int unsignedp;
2683 rtx tem;
2684 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
2686 tem = expand_shift (RSHIFT_EXPR, mode, op0,
2687 build_int_2 (GET_MODE_BITSIZE (mode) - 1, 0),
2688 NULL_RTX, 0);
2689 tem = expand_and (tem, op1, NULL_RTX);
2690 adj_operand
2691 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
2692 adj_operand);
2694 tem = expand_shift (RSHIFT_EXPR, mode, op1,
2695 build_int_2 (GET_MODE_BITSIZE (mode) - 1, 0),
2696 NULL_RTX, 0);
2697 tem = expand_and (tem, op0, NULL_RTX);
2698 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
2699 target);
2701 return target;
2704 /* Emit code to multiply OP0 and CNST1, putting the high half of the result
2705 in TARGET if that is convenient, and return where the result is. If the
2706 operation can not be performed, 0 is returned.
2708 MODE is the mode of operation and result.
2710 UNSIGNEDP nonzero means unsigned multiply.
2712 MAX_COST is the total allowed cost for the expanded RTL. */
2715 expand_mult_highpart (mode, op0, cnst1, target, unsignedp, max_cost)
2716 enum machine_mode mode;
2717 register rtx op0, target;
2718 unsigned HOST_WIDE_INT cnst1;
2719 int unsignedp;
2720 int max_cost;
2722 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
2723 optab mul_highpart_optab;
2724 optab moptab;
2725 rtx tem;
2726 int size = GET_MODE_BITSIZE (mode);
2727 rtx op1, wide_op1;
2729 /* We can't support modes wider than HOST_BITS_PER_INT. */
2730 if (size > HOST_BITS_PER_WIDE_INT)
2731 abort ();
2733 op1 = GEN_INT (cnst1);
2735 if (GET_MODE_BITSIZE (wider_mode) <= HOST_BITS_PER_INT)
2736 wide_op1 = op1;
2737 else
2738 wide_op1
2739 = immed_double_const (cnst1,
2740 (unsignedp
2741 ? (HOST_WIDE_INT) 0
2742 : -(cnst1 >> (HOST_BITS_PER_WIDE_INT - 1))),
2743 wider_mode);
2745 /* expand_mult handles constant multiplication of word_mode
2746 or narrower. It does a poor job for large modes. */
2747 if (size < BITS_PER_WORD
2748 && mul_cost[(int) wider_mode] + shift_cost[size-1] < max_cost)
2750 /* We have to do this, since expand_binop doesn't do conversion for
2751 multiply. Maybe change expand_binop to handle widening multiply? */
2752 op0 = convert_to_mode (wider_mode, op0, unsignedp);
2754 tem = expand_mult (wider_mode, op0, wide_op1, NULL_RTX, unsignedp);
2755 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2756 build_int_2 (size, 0), NULL_RTX, 1);
2757 return convert_modes (mode, wider_mode, tem, unsignedp);
2760 if (target == 0)
2761 target = gen_reg_rtx (mode);
2763 /* Firstly, try using a multiplication insn that only generates the needed
2764 high part of the product, and in the sign flavor of unsignedp. */
2765 if (mul_highpart_cost[(int) mode] < max_cost)
2767 mul_highpart_optab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
2768 target = expand_binop (mode, mul_highpart_optab,
2769 op0, wide_op1, target, unsignedp, OPTAB_DIRECT);
2770 if (target)
2771 return target;
2774 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
2775 Need to adjust the result after the multiplication. */
2776 if (mul_highpart_cost[(int) mode] + 2 * shift_cost[size-1] + 4 * add_cost < max_cost)
2778 mul_highpart_optab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
2779 target = expand_binop (mode, mul_highpart_optab,
2780 op0, wide_op1, target, unsignedp, OPTAB_DIRECT);
2781 if (target)
2782 /* We used the wrong signedness. Adjust the result. */
2783 return expand_mult_highpart_adjust (mode, target, op0,
2784 op1, target, unsignedp);
2787 /* Try widening multiplication. */
2788 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
2789 if (moptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
2790 && mul_widen_cost[(int) wider_mode] < max_cost)
2792 op1 = force_reg (mode, op1);
2793 goto try;
2796 /* Try widening the mode and perform a non-widening multiplication. */
2797 moptab = smul_optab;
2798 if (smul_optab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
2799 && mul_cost[(int) wider_mode] + shift_cost[size-1] < max_cost)
2801 op1 = wide_op1;
2802 goto try;
2805 /* Try widening multiplication of opposite signedness, and adjust. */
2806 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
2807 if (moptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
2808 && (mul_widen_cost[(int) wider_mode]
2809 + 2 * shift_cost[size-1] + 4 * add_cost < max_cost))
2811 rtx regop1 = force_reg (mode, op1);
2812 tem = expand_binop (wider_mode, moptab, op0, regop1,
2813 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
2814 if (tem != 0)
2816 /* Extract the high half of the just generated product. */
2817 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2818 build_int_2 (size, 0), NULL_RTX, 1);
2819 tem = convert_modes (mode, wider_mode, tem, unsignedp);
2820 /* We used the wrong signedness. Adjust the result. */
2821 return expand_mult_highpart_adjust (mode, tem, op0, op1,
2822 target, unsignedp);
2826 return 0;
2828 try:
2829 /* Pass NULL_RTX as target since TARGET has wrong mode. */
2830 tem = expand_binop (wider_mode, moptab, op0, op1,
2831 NULL_RTX, unsignedp, OPTAB_WIDEN);
2832 if (tem == 0)
2833 return 0;
2835 /* Extract the high half of the just generated product. */
2836 if (mode == word_mode)
2838 return gen_highpart (mode, tem);
2840 else
2842 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2843 build_int_2 (size, 0), NULL_RTX, 1);
2844 return convert_modes (mode, wider_mode, tem, unsignedp);
2848 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
2849 if that is convenient, and returning where the result is.
2850 You may request either the quotient or the remainder as the result;
2851 specify REM_FLAG nonzero to get the remainder.
2853 CODE is the expression code for which kind of division this is;
2854 it controls how rounding is done. MODE is the machine mode to use.
2855 UNSIGNEDP nonzero means do unsigned division. */
2857 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
2858 and then correct it by or'ing in missing high bits
2859 if result of ANDI is nonzero.
2860 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
2861 This could optimize to a bfexts instruction.
2862 But C doesn't use these operations, so their optimizations are
2863 left for later. */
2864 /* ??? For modulo, we don't actually need the highpart of the first product,
2865 the low part will do nicely. And for small divisors, the second multiply
2866 can also be a low-part only multiply or even be completely left out.
2867 E.g. to calculate the remainder of a division by 3 with a 32 bit
2868 multiply, multiply with 0x55555556 and extract the upper two bits;
2869 the result is exact for inputs up to 0x1fffffff.
2870 The input range can be reduced by using cross-sum rules.
2871 For odd divisors >= 3, the following table gives right shift counts
2872 so that if an number is shifted by an integer multiple of the given
2873 amount, the remainder stays the same:
2874 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
2875 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
2876 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
2877 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
2878 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
2880 Cross-sum rules for even numbers can be derived by leaving as many bits
2881 to the right alone as the divisor has zeros to the right.
2882 E.g. if x is an unsigned 32 bit number:
2883 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
2886 #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
2889 expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
2890 int rem_flag;
2891 enum tree_code code;
2892 enum machine_mode mode;
2893 register rtx op0, op1, target;
2894 int unsignedp;
2896 enum machine_mode compute_mode;
2897 register rtx tquotient;
2898 rtx quotient = 0, remainder = 0;
2899 rtx last;
2900 int size;
2901 rtx insn, set;
2902 optab optab1, optab2;
2903 int op1_is_constant, op1_is_pow2;
2904 int max_cost, extra_cost;
2905 static HOST_WIDE_INT last_div_const = 0;
2907 op1_is_constant = GET_CODE (op1) == CONST_INT;
2908 op1_is_pow2 = (op1_is_constant
2909 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
2910 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1))))));
2913 This is the structure of expand_divmod:
2915 First comes code to fix up the operands so we can perform the operations
2916 correctly and efficiently.
2918 Second comes a switch statement with code specific for each rounding mode.
2919 For some special operands this code emits all RTL for the desired
2920 operation, for other cases, it generates only a quotient and stores it in
2921 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
2922 to indicate that it has not done anything.
2924 Last comes code that finishes the operation. If QUOTIENT is set and
2925 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
2926 QUOTIENT is not set, it is computed using trunc rounding.
2928 We try to generate special code for division and remainder when OP1 is a
2929 constant. If |OP1| = 2**n we can use shifts and some other fast
2930 operations. For other values of OP1, we compute a carefully selected
2931 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
2932 by m.
2934 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
2935 half of the product. Different strategies for generating the product are
2936 implemented in expand_mult_highpart.
2938 If what we actually want is the remainder, we generate that by another
2939 by-constant multiplication and a subtraction. */
2941 /* We shouldn't be called with OP1 == const1_rtx, but some of the
2942 code below will malfunction if we are, so check here and handle
2943 the special case if so. */
2944 if (op1 == const1_rtx)
2945 return rem_flag ? const0_rtx : op0;
2947 if (target
2948 /* Don't use the function value register as a target
2949 since we have to read it as well as write it,
2950 and function-inlining gets confused by this. */
2951 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
2952 /* Don't clobber an operand while doing a multi-step calculation. */
2953 || ((rem_flag || op1_is_constant)
2954 && (reg_mentioned_p (target, op0)
2955 || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
2956 || reg_mentioned_p (target, op1)
2957 || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM)))
2958 target = 0;
2960 /* Get the mode in which to perform this computation. Normally it will
2961 be MODE, but sometimes we can't do the desired operation in MODE.
2962 If so, pick a wider mode in which we can do the operation. Convert
2963 to that mode at the start to avoid repeated conversions.
2965 First see what operations we need. These depend on the expression
2966 we are evaluating. (We assume that divxx3 insns exist under the
2967 same conditions that modxx3 insns and that these insns don't normally
2968 fail. If these assumptions are not correct, we may generate less
2969 efficient code in some cases.)
2971 Then see if we find a mode in which we can open-code that operation
2972 (either a division, modulus, or shift). Finally, check for the smallest
2973 mode for which we can do the operation with a library call. */
2975 /* We might want to refine this now that we have division-by-constant
2976 optimization. Since expand_mult_highpart tries so many variants, it is
2977 not straightforward to generalize this. Maybe we should make an array
2978 of possible modes in init_expmed? Save this for GCC 2.7. */
2980 optab1 = (op1_is_pow2 ? (unsignedp ? lshr_optab : ashr_optab)
2981 : (unsignedp ? udiv_optab : sdiv_optab));
2982 optab2 = (op1_is_pow2 ? optab1 : (unsignedp ? udivmod_optab : sdivmod_optab));
2984 for (compute_mode = mode; compute_mode != VOIDmode;
2985 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
2986 if (optab1->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing
2987 || optab2->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing)
2988 break;
2990 if (compute_mode == VOIDmode)
2991 for (compute_mode = mode; compute_mode != VOIDmode;
2992 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
2993 if (optab1->handlers[(int) compute_mode].libfunc
2994 || optab2->handlers[(int) compute_mode].libfunc)
2995 break;
2997 /* If we still couldn't find a mode, use MODE, but we'll probably abort
2998 in expand_binop. */
2999 if (compute_mode == VOIDmode)
3000 compute_mode = mode;
3002 if (target && GET_MODE (target) == compute_mode)
3003 tquotient = target;
3004 else
3005 tquotient = gen_reg_rtx (compute_mode);
3007 size = GET_MODE_BITSIZE (compute_mode);
3008 #if 0
3009 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3010 (mode), and thereby get better code when OP1 is a constant. Do that
3011 later. It will require going over all usages of SIZE below. */
3012 size = GET_MODE_BITSIZE (mode);
3013 #endif
3015 /* Only deduct something for a REM if the last divide done was
3016 for a different constant. Then set the constant of the last
3017 divide. */
3018 max_cost = div_cost[(int) compute_mode]
3019 - (rem_flag && ! (last_div_const != 0 && op1_is_constant
3020 && INTVAL (op1) == last_div_const)
3021 ? mul_cost[(int) compute_mode] + add_cost : 0);
3023 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
3025 /* Now convert to the best mode to use. */
3026 if (compute_mode != mode)
3028 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
3029 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
3031 /* convert_modes may have placed op1 into a register, so we
3032 must recompute the following. */
3033 op1_is_constant = GET_CODE (op1) == CONST_INT;
3034 op1_is_pow2 = (op1_is_constant
3035 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3036 || (! unsignedp
3037 && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
3040 /* If one of the operands is a volatile MEM, copy it into a register. */
3042 if (GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0))
3043 op0 = force_reg (compute_mode, op0);
3044 if (GET_CODE (op1) == MEM && MEM_VOLATILE_P (op1))
3045 op1 = force_reg (compute_mode, op1);
3047 /* If we need the remainder or if OP1 is constant, we need to
3048 put OP0 in a register in case it has any queued subexpressions. */
3049 if (rem_flag || op1_is_constant)
3050 op0 = force_reg (compute_mode, op0);
3052 last = get_last_insn ();
3054 /* Promote floor rounding to trunc rounding for unsigned operations. */
3055 if (unsignedp)
3057 if (code == FLOOR_DIV_EXPR)
3058 code = TRUNC_DIV_EXPR;
3059 if (code == FLOOR_MOD_EXPR)
3060 code = TRUNC_MOD_EXPR;
3061 if (code == EXACT_DIV_EXPR && op1_is_pow2)
3062 code = TRUNC_DIV_EXPR;
3065 if (op1 != const0_rtx)
3066 switch (code)
3068 case TRUNC_MOD_EXPR:
3069 case TRUNC_DIV_EXPR:
3070 if (op1_is_constant)
3072 if (unsignedp)
3074 unsigned HOST_WIDE_INT mh, ml;
3075 int pre_shift, post_shift;
3076 int dummy;
3077 unsigned HOST_WIDE_INT d = INTVAL (op1);
3079 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3081 pre_shift = floor_log2 (d);
3082 if (rem_flag)
3084 remainder
3085 = expand_binop (compute_mode, and_optab, op0,
3086 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3087 remainder, 1,
3088 OPTAB_LIB_WIDEN);
3089 if (remainder)
3090 return gen_lowpart (mode, remainder);
3092 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3093 build_int_2 (pre_shift, 0),
3094 tquotient, 1);
3096 else if (size <= HOST_BITS_PER_WIDE_INT)
3098 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
3100 /* Most significant bit of divisor is set; emit an scc
3101 insn. */
3102 quotient = emit_store_flag (tquotient, GEU, op0, op1,
3103 compute_mode, 1, 1);
3104 if (quotient == 0)
3105 goto fail1;
3107 else
3109 /* Find a suitable multiplier and right shift count
3110 instead of multiplying with D. */
3112 mh = choose_multiplier (d, size, size,
3113 &ml, &post_shift, &dummy);
3115 /* If the suggested multiplier is more than SIZE bits,
3116 we can do better for even divisors, using an
3117 initial right shift. */
3118 if (mh != 0 && (d & 1) == 0)
3120 pre_shift = floor_log2 (d & -d);
3121 mh = choose_multiplier (d >> pre_shift, size,
3122 size - pre_shift,
3123 &ml, &post_shift, &dummy);
3124 if (mh)
3125 abort ();
3127 else
3128 pre_shift = 0;
3130 if (mh != 0)
3132 rtx t1, t2, t3, t4;
3134 extra_cost = (shift_cost[post_shift - 1]
3135 + shift_cost[1] + 2 * add_cost);
3136 t1 = expand_mult_highpart (compute_mode, op0, ml,
3137 NULL_RTX, 1,
3138 max_cost - extra_cost);
3139 if (t1 == 0)
3140 goto fail1;
3141 t2 = force_operand (gen_rtx_MINUS (compute_mode,
3142 op0, t1),
3143 NULL_RTX);
3144 t3 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3145 build_int_2 (1, 0), NULL_RTX,1);
3146 t4 = force_operand (gen_rtx_PLUS (compute_mode,
3147 t1, t3),
3148 NULL_RTX);
3149 quotient
3150 = expand_shift (RSHIFT_EXPR, compute_mode, t4,
3151 build_int_2 (post_shift - 1, 0),
3152 tquotient, 1);
3154 else
3156 rtx t1, t2;
3158 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3159 build_int_2 (pre_shift, 0),
3160 NULL_RTX, 1);
3161 extra_cost = (shift_cost[pre_shift]
3162 + shift_cost[post_shift]);
3163 t2 = expand_mult_highpart (compute_mode, t1, ml,
3164 NULL_RTX, 1,
3165 max_cost - extra_cost);
3166 if (t2 == 0)
3167 goto fail1;
3168 quotient
3169 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3170 build_int_2 (post_shift, 0),
3171 tquotient, 1);
3175 else /* Too wide mode to use tricky code */
3176 break;
3178 insn = get_last_insn ();
3179 if (insn != last
3180 && (set = single_set (insn)) != 0
3181 && SET_DEST (set) == quotient)
3182 set_unique_reg_note (insn,
3183 REG_EQUAL,
3184 gen_rtx_UDIV (compute_mode, op0, op1));
3186 else /* TRUNC_DIV, signed */
3188 unsigned HOST_WIDE_INT ml;
3189 int lgup, post_shift;
3190 HOST_WIDE_INT d = INTVAL (op1);
3191 unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
3193 /* n rem d = n rem -d */
3194 if (rem_flag && d < 0)
3196 d = abs_d;
3197 op1 = GEN_INT (abs_d);
3200 if (d == 1)
3201 quotient = op0;
3202 else if (d == -1)
3203 quotient = expand_unop (compute_mode, neg_optab, op0,
3204 tquotient, 0);
3205 else if (abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
3207 /* This case is not handled correctly below. */
3208 quotient = emit_store_flag (tquotient, EQ, op0, op1,
3209 compute_mode, 1, 1);
3210 if (quotient == 0)
3211 goto fail1;
3213 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
3214 && (rem_flag ? smod_pow2_cheap : sdiv_pow2_cheap))
3216 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
3218 lgup = floor_log2 (abs_d);
3219 if (abs_d != 2 && BRANCH_COST < 3)
3221 rtx label = gen_label_rtx ();
3222 rtx t1;
3224 t1 = copy_to_mode_reg (compute_mode, op0);
3225 do_cmp_and_jump (t1, const0_rtx, GE,
3226 compute_mode, label);
3227 expand_inc (t1, GEN_INT (abs_d - 1));
3228 emit_label (label);
3229 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3230 build_int_2 (lgup, 0),
3231 tquotient, 0);
3233 else
3235 rtx t1, t2, t3;
3236 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3237 build_int_2 (size - 1, 0),
3238 NULL_RTX, 0);
3239 t2 = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3240 build_int_2 (size - lgup, 0),
3241 NULL_RTX, 1);
3242 t3 = force_operand (gen_rtx_PLUS (compute_mode,
3243 op0, t2),
3244 NULL_RTX);
3245 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t3,
3246 build_int_2 (lgup, 0),
3247 tquotient, 0);
3250 /* We have computed OP0 / abs(OP1). If OP1 is negative, negate
3251 the quotient. */
3252 if (d < 0)
3254 insn = get_last_insn ();
3255 if (insn != last
3256 && (set = single_set (insn)) != 0
3257 && SET_DEST (set) == quotient
3258 && abs_d < ((unsigned HOST_WIDE_INT) 1
3259 << (HOST_BITS_PER_WIDE_INT - 1)))
3260 set_unique_reg_note (insn,
3261 REG_EQUAL,
3262 gen_rtx_DIV (compute_mode,
3263 op0,
3264 GEN_INT (abs_d)));
3266 quotient = expand_unop (compute_mode, neg_optab,
3267 quotient, quotient, 0);
3270 else if (size <= HOST_BITS_PER_WIDE_INT)
3272 choose_multiplier (abs_d, size, size - 1,
3273 &ml, &post_shift, &lgup);
3274 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
3276 rtx t1, t2, t3;
3278 extra_cost = (shift_cost[post_shift]
3279 + shift_cost[size - 1] + add_cost);
3280 t1 = expand_mult_highpart (compute_mode, op0, ml,
3281 NULL_RTX, 0,
3282 max_cost - extra_cost);
3283 if (t1 == 0)
3284 goto fail1;
3285 t2 = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3286 build_int_2 (post_shift, 0), NULL_RTX, 0);
3287 t3 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3288 build_int_2 (size - 1, 0), NULL_RTX, 0);
3289 if (d < 0)
3290 quotient
3291 = force_operand (gen_rtx_MINUS (compute_mode,
3292 t3, t2),
3293 tquotient);
3294 else
3295 quotient
3296 = force_operand (gen_rtx_MINUS (compute_mode,
3297 t2, t3),
3298 tquotient);
3300 else
3302 rtx t1, t2, t3, t4;
3304 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
3305 extra_cost = (shift_cost[post_shift]
3306 + shift_cost[size - 1] + 2 * add_cost);
3307 t1 = expand_mult_highpart (compute_mode, op0, ml,
3308 NULL_RTX, 0,
3309 max_cost - extra_cost);
3310 if (t1 == 0)
3311 goto fail1;
3312 t2 = force_operand (gen_rtx_PLUS (compute_mode,
3313 t1, op0),
3314 NULL_RTX);
3315 t3 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3316 build_int_2 (post_shift, 0),
3317 NULL_RTX, 0);
3318 t4 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3319 build_int_2 (size - 1, 0),
3320 NULL_RTX, 0);
3321 if (d < 0)
3322 quotient
3323 = force_operand (gen_rtx_MINUS (compute_mode,
3324 t4, t3),
3325 tquotient);
3326 else
3327 quotient
3328 = force_operand (gen_rtx_MINUS (compute_mode,
3329 t3, t4),
3330 tquotient);
3333 else /* Too wide mode to use tricky code */
3334 break;
3336 insn = get_last_insn ();
3337 if (insn != last
3338 && (set = single_set (insn)) != 0
3339 && SET_DEST (set) == quotient)
3340 set_unique_reg_note (insn,
3341 REG_EQUAL,
3342 gen_rtx_DIV (compute_mode, op0, op1));
3344 break;
3346 fail1:
3347 delete_insns_since (last);
3348 break;
3350 case FLOOR_DIV_EXPR:
3351 case FLOOR_MOD_EXPR:
3352 /* We will come here only for signed operations. */
3353 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
3355 unsigned HOST_WIDE_INT mh, ml;
3356 int pre_shift, lgup, post_shift;
3357 HOST_WIDE_INT d = INTVAL (op1);
3359 if (d > 0)
3361 /* We could just as easily deal with negative constants here,
3362 but it does not seem worth the trouble for GCC 2.6. */
3363 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3365 pre_shift = floor_log2 (d);
3366 if (rem_flag)
3368 remainder = expand_binop (compute_mode, and_optab, op0,
3369 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3370 remainder, 0, OPTAB_LIB_WIDEN);
3371 if (remainder)
3372 return gen_lowpart (mode, remainder);
3374 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3375 build_int_2 (pre_shift, 0),
3376 tquotient, 0);
3378 else
3380 rtx t1, t2, t3, t4;
3382 mh = choose_multiplier (d, size, size - 1,
3383 &ml, &post_shift, &lgup);
3384 if (mh)
3385 abort ();
3387 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3388 build_int_2 (size - 1, 0), NULL_RTX, 0);
3389 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
3390 NULL_RTX, 0, OPTAB_WIDEN);
3391 extra_cost = (shift_cost[post_shift]
3392 + shift_cost[size - 1] + 2 * add_cost);
3393 t3 = expand_mult_highpart (compute_mode, t2, ml,
3394 NULL_RTX, 1,
3395 max_cost - extra_cost);
3396 if (t3 != 0)
3398 t4 = expand_shift (RSHIFT_EXPR, compute_mode, t3,
3399 build_int_2 (post_shift, 0),
3400 NULL_RTX, 1);
3401 quotient = expand_binop (compute_mode, xor_optab,
3402 t4, t1, tquotient, 0,
3403 OPTAB_WIDEN);
3407 else
3409 rtx nsign, t1, t2, t3, t4;
3410 t1 = force_operand (gen_rtx_PLUS (compute_mode,
3411 op0, constm1_rtx), NULL_RTX);
3412 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
3413 0, OPTAB_WIDEN);
3414 nsign = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3415 build_int_2 (size - 1, 0), NULL_RTX, 0);
3416 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
3417 NULL_RTX);
3418 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
3419 NULL_RTX, 0);
3420 if (t4)
3422 rtx t5;
3423 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
3424 NULL_RTX, 0);
3425 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3426 t4, t5),
3427 tquotient);
3432 if (quotient != 0)
3433 break;
3434 delete_insns_since (last);
3436 /* Try using an instruction that produces both the quotient and
3437 remainder, using truncation. We can easily compensate the quotient
3438 or remainder to get floor rounding, once we have the remainder.
3439 Notice that we compute also the final remainder value here,
3440 and return the result right away. */
3441 if (target == 0 || GET_MODE (target) != compute_mode)
3442 target = gen_reg_rtx (compute_mode);
3444 if (rem_flag)
3446 remainder
3447 = GET_CODE (target) == REG ? target : gen_reg_rtx (compute_mode);
3448 quotient = gen_reg_rtx (compute_mode);
3450 else
3452 quotient
3453 = GET_CODE (target) == REG ? target : gen_reg_rtx (compute_mode);
3454 remainder = gen_reg_rtx (compute_mode);
3457 if (expand_twoval_binop (sdivmod_optab, op0, op1,
3458 quotient, remainder, 0))
3460 /* This could be computed with a branch-less sequence.
3461 Save that for later. */
3462 rtx tem;
3463 rtx label = gen_label_rtx ();
3464 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
3465 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3466 NULL_RTX, 0, OPTAB_WIDEN);
3467 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
3468 expand_dec (quotient, const1_rtx);
3469 expand_inc (remainder, op1);
3470 emit_label (label);
3471 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3474 /* No luck with division elimination or divmod. Have to do it
3475 by conditionally adjusting op0 *and* the result. */
3477 rtx label1, label2, label3, label4, label5;
3478 rtx adjusted_op0;
3479 rtx tem;
3481 quotient = gen_reg_rtx (compute_mode);
3482 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3483 label1 = gen_label_rtx ();
3484 label2 = gen_label_rtx ();
3485 label3 = gen_label_rtx ();
3486 label4 = gen_label_rtx ();
3487 label5 = gen_label_rtx ();
3488 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
3489 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
3490 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3491 quotient, 0, OPTAB_LIB_WIDEN);
3492 if (tem != quotient)
3493 emit_move_insn (quotient, tem);
3494 emit_jump_insn (gen_jump (label5));
3495 emit_barrier ();
3496 emit_label (label1);
3497 expand_inc (adjusted_op0, const1_rtx);
3498 emit_jump_insn (gen_jump (label4));
3499 emit_barrier ();
3500 emit_label (label2);
3501 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
3502 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3503 quotient, 0, OPTAB_LIB_WIDEN);
3504 if (tem != quotient)
3505 emit_move_insn (quotient, tem);
3506 emit_jump_insn (gen_jump (label5));
3507 emit_barrier ();
3508 emit_label (label3);
3509 expand_dec (adjusted_op0, const1_rtx);
3510 emit_label (label4);
3511 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3512 quotient, 0, OPTAB_LIB_WIDEN);
3513 if (tem != quotient)
3514 emit_move_insn (quotient, tem);
3515 expand_dec (quotient, const1_rtx);
3516 emit_label (label5);
3518 break;
3520 case CEIL_DIV_EXPR:
3521 case CEIL_MOD_EXPR:
3522 if (unsignedp)
3524 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
3526 rtx t1, t2, t3;
3527 unsigned HOST_WIDE_INT d = INTVAL (op1);
3528 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3529 build_int_2 (floor_log2 (d), 0),
3530 tquotient, 1);
3531 t2 = expand_binop (compute_mode, and_optab, op0,
3532 GEN_INT (d - 1),
3533 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3534 t3 = gen_reg_rtx (compute_mode);
3535 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
3536 compute_mode, 1, 1);
3537 if (t3 == 0)
3539 rtx lab;
3540 lab = gen_label_rtx ();
3541 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
3542 expand_inc (t1, const1_rtx);
3543 emit_label (lab);
3544 quotient = t1;
3546 else
3547 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3548 t1, t3),
3549 tquotient);
3550 break;
3553 /* Try using an instruction that produces both the quotient and
3554 remainder, using truncation. We can easily compensate the
3555 quotient or remainder to get ceiling rounding, once we have the
3556 remainder. Notice that we compute also the final remainder
3557 value here, and return the result right away. */
3558 if (target == 0 || GET_MODE (target) != compute_mode)
3559 target = gen_reg_rtx (compute_mode);
3561 if (rem_flag)
3563 remainder = (GET_CODE (target) == REG
3564 ? target : gen_reg_rtx (compute_mode));
3565 quotient = gen_reg_rtx (compute_mode);
3567 else
3569 quotient = (GET_CODE (target) == REG
3570 ? target : gen_reg_rtx (compute_mode));
3571 remainder = gen_reg_rtx (compute_mode);
3574 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
3575 remainder, 1))
3577 /* This could be computed with a branch-less sequence.
3578 Save that for later. */
3579 rtx label = gen_label_rtx ();
3580 do_cmp_and_jump (remainder, const0_rtx, EQ,
3581 compute_mode, label);
3582 expand_inc (quotient, const1_rtx);
3583 expand_dec (remainder, op1);
3584 emit_label (label);
3585 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3588 /* No luck with division elimination or divmod. Have to do it
3589 by conditionally adjusting op0 *and* the result. */
3591 rtx label1, label2;
3592 rtx adjusted_op0, tem;
3594 quotient = gen_reg_rtx (compute_mode);
3595 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3596 label1 = gen_label_rtx ();
3597 label2 = gen_label_rtx ();
3598 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
3599 compute_mode, label1);
3600 emit_move_insn (quotient, const0_rtx);
3601 emit_jump_insn (gen_jump (label2));
3602 emit_barrier ();
3603 emit_label (label1);
3604 expand_dec (adjusted_op0, const1_rtx);
3605 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
3606 quotient, 1, OPTAB_LIB_WIDEN);
3607 if (tem != quotient)
3608 emit_move_insn (quotient, tem);
3609 expand_inc (quotient, const1_rtx);
3610 emit_label (label2);
3613 else /* signed */
3615 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3616 && INTVAL (op1) >= 0)
3618 /* This is extremely similar to the code for the unsigned case
3619 above. For 2.7 we should merge these variants, but for
3620 2.6.1 I don't want to touch the code for unsigned since that
3621 get used in C. The signed case will only be used by other
3622 languages (Ada). */
3624 rtx t1, t2, t3;
3625 unsigned HOST_WIDE_INT d = INTVAL (op1);
3626 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3627 build_int_2 (floor_log2 (d), 0),
3628 tquotient, 0);
3629 t2 = expand_binop (compute_mode, and_optab, op0,
3630 GEN_INT (d - 1),
3631 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3632 t3 = gen_reg_rtx (compute_mode);
3633 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
3634 compute_mode, 1, 1);
3635 if (t3 == 0)
3637 rtx lab;
3638 lab = gen_label_rtx ();
3639 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
3640 expand_inc (t1, const1_rtx);
3641 emit_label (lab);
3642 quotient = t1;
3644 else
3645 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3646 t1, t3),
3647 tquotient);
3648 break;
3651 /* Try using an instruction that produces both the quotient and
3652 remainder, using truncation. We can easily compensate the
3653 quotient or remainder to get ceiling rounding, once we have the
3654 remainder. Notice that we compute also the final remainder
3655 value here, and return the result right away. */
3656 if (target == 0 || GET_MODE (target) != compute_mode)
3657 target = gen_reg_rtx (compute_mode);
3658 if (rem_flag)
3660 remainder= (GET_CODE (target) == REG
3661 ? target : gen_reg_rtx (compute_mode));
3662 quotient = gen_reg_rtx (compute_mode);
3664 else
3666 quotient = (GET_CODE (target) == REG
3667 ? target : gen_reg_rtx (compute_mode));
3668 remainder = gen_reg_rtx (compute_mode);
3671 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
3672 remainder, 0))
3674 /* This could be computed with a branch-less sequence.
3675 Save that for later. */
3676 rtx tem;
3677 rtx label = gen_label_rtx ();
3678 do_cmp_and_jump (remainder, const0_rtx, EQ,
3679 compute_mode, label);
3680 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3681 NULL_RTX, 0, OPTAB_WIDEN);
3682 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
3683 expand_inc (quotient, const1_rtx);
3684 expand_dec (remainder, op1);
3685 emit_label (label);
3686 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3689 /* No luck with division elimination or divmod. Have to do it
3690 by conditionally adjusting op0 *and* the result. */
3692 rtx label1, label2, label3, label4, label5;
3693 rtx adjusted_op0;
3694 rtx tem;
3696 quotient = gen_reg_rtx (compute_mode);
3697 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3698 label1 = gen_label_rtx ();
3699 label2 = gen_label_rtx ();
3700 label3 = gen_label_rtx ();
3701 label4 = gen_label_rtx ();
3702 label5 = gen_label_rtx ();
3703 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
3704 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
3705 compute_mode, label1);
3706 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3707 quotient, 0, OPTAB_LIB_WIDEN);
3708 if (tem != quotient)
3709 emit_move_insn (quotient, tem);
3710 emit_jump_insn (gen_jump (label5));
3711 emit_barrier ();
3712 emit_label (label1);
3713 expand_dec (adjusted_op0, const1_rtx);
3714 emit_jump_insn (gen_jump (label4));
3715 emit_barrier ();
3716 emit_label (label2);
3717 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
3718 compute_mode, label3);
3719 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3720 quotient, 0, OPTAB_LIB_WIDEN);
3721 if (tem != quotient)
3722 emit_move_insn (quotient, tem);
3723 emit_jump_insn (gen_jump (label5));
3724 emit_barrier ();
3725 emit_label (label3);
3726 expand_inc (adjusted_op0, const1_rtx);
3727 emit_label (label4);
3728 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3729 quotient, 0, OPTAB_LIB_WIDEN);
3730 if (tem != quotient)
3731 emit_move_insn (quotient, tem);
3732 expand_inc (quotient, const1_rtx);
3733 emit_label (label5);
3736 break;
3738 case EXACT_DIV_EXPR:
3739 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
3741 HOST_WIDE_INT d = INTVAL (op1);
3742 unsigned HOST_WIDE_INT ml;
3743 int post_shift;
3744 rtx t1;
3746 post_shift = floor_log2 (d & -d);
3747 ml = invert_mod2n (d >> post_shift, size);
3748 t1 = expand_mult (compute_mode, op0, GEN_INT (ml), NULL_RTX,
3749 unsignedp);
3750 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3751 build_int_2 (post_shift, 0),
3752 NULL_RTX, unsignedp);
3754 insn = get_last_insn ();
3755 set_unique_reg_note (insn,
3756 REG_EQUAL,
3757 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
3758 compute_mode,
3759 op0, op1));
3761 break;
3763 case ROUND_DIV_EXPR:
3764 case ROUND_MOD_EXPR:
3765 if (unsignedp)
3767 rtx tem;
3768 rtx label;
3769 label = gen_label_rtx ();
3770 quotient = gen_reg_rtx (compute_mode);
3771 remainder = gen_reg_rtx (compute_mode);
3772 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
3774 rtx tem;
3775 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
3776 quotient, 1, OPTAB_LIB_WIDEN);
3777 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
3778 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
3779 remainder, 1, OPTAB_LIB_WIDEN);
3781 tem = plus_constant (op1, -1);
3782 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
3783 build_int_2 (1, 0), NULL_RTX, 1);
3784 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
3785 expand_inc (quotient, const1_rtx);
3786 expand_dec (remainder, op1);
3787 emit_label (label);
3789 else
3791 rtx abs_rem, abs_op1, tem, mask;
3792 rtx label;
3793 label = gen_label_rtx ();
3794 quotient = gen_reg_rtx (compute_mode);
3795 remainder = gen_reg_rtx (compute_mode);
3796 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
3798 rtx tem;
3799 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
3800 quotient, 0, OPTAB_LIB_WIDEN);
3801 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
3802 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
3803 remainder, 0, OPTAB_LIB_WIDEN);
3805 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 0);
3806 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 0);
3807 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
3808 build_int_2 (1, 0), NULL_RTX, 1);
3809 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
3810 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3811 NULL_RTX, 0, OPTAB_WIDEN);
3812 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
3813 build_int_2 (size - 1, 0), NULL_RTX, 0);
3814 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
3815 NULL_RTX, 0, OPTAB_WIDEN);
3816 tem = expand_binop (compute_mode, sub_optab, tem, mask,
3817 NULL_RTX, 0, OPTAB_WIDEN);
3818 expand_inc (quotient, tem);
3819 tem = expand_binop (compute_mode, xor_optab, mask, op1,
3820 NULL_RTX, 0, OPTAB_WIDEN);
3821 tem = expand_binop (compute_mode, sub_optab, tem, mask,
3822 NULL_RTX, 0, OPTAB_WIDEN);
3823 expand_dec (remainder, tem);
3824 emit_label (label);
3826 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3828 default:
3829 abort ();
3832 if (quotient == 0)
3834 if (target && GET_MODE (target) != compute_mode)
3835 target = 0;
3837 if (rem_flag)
3839 /* Try to produce the remainder without producing the quotient.
3840 If we seem to have a divmod patten that does not require widening,
3841 don't try windening here. We should really have an WIDEN argument
3842 to expand_twoval_binop, since what we'd really like to do here is
3843 1) try a mod insn in compute_mode
3844 2) try a divmod insn in compute_mode
3845 3) try a div insn in compute_mode and multiply-subtract to get
3846 remainder
3847 4) try the same things with widening allowed. */
3848 remainder
3849 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
3850 op0, op1, target,
3851 unsignedp,
3852 ((optab2->handlers[(int) compute_mode].insn_code
3853 != CODE_FOR_nothing)
3854 ? OPTAB_DIRECT : OPTAB_WIDEN));
3855 if (remainder == 0)
3857 /* No luck there. Can we do remainder and divide at once
3858 without a library call? */
3859 remainder = gen_reg_rtx (compute_mode);
3860 if (! expand_twoval_binop ((unsignedp
3861 ? udivmod_optab
3862 : sdivmod_optab),
3863 op0, op1,
3864 NULL_RTX, remainder, unsignedp))
3865 remainder = 0;
3868 if (remainder)
3869 return gen_lowpart (mode, remainder);
3872 /* Produce the quotient. Try a quotient insn, but not a library call.
3873 If we have a divmod in this mode, use it in preference to widening
3874 the div (for this test we assume it will not fail). Note that optab2
3875 is set to the one of the two optabs that the call below will use. */
3876 quotient
3877 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
3878 op0, op1, rem_flag ? NULL_RTX : target,
3879 unsignedp,
3880 ((optab2->handlers[(int) compute_mode].insn_code
3881 != CODE_FOR_nothing)
3882 ? OPTAB_DIRECT : OPTAB_WIDEN));
3884 if (quotient == 0)
3886 /* No luck there. Try a quotient-and-remainder insn,
3887 keeping the quotient alone. */
3888 quotient = gen_reg_rtx (compute_mode);
3889 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
3890 op0, op1,
3891 quotient, NULL_RTX, unsignedp))
3893 quotient = 0;
3894 if (! rem_flag)
3895 /* Still no luck. If we are not computing the remainder,
3896 use a library call for the quotient. */
3897 quotient = sign_expand_binop (compute_mode,
3898 udiv_optab, sdiv_optab,
3899 op0, op1, target,
3900 unsignedp, OPTAB_LIB_WIDEN);
3905 if (rem_flag)
3907 if (target && GET_MODE (target) != compute_mode)
3908 target = 0;
3910 if (quotient == 0)
3911 /* No divide instruction either. Use library for remainder. */
3912 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
3913 op0, op1, target,
3914 unsignedp, OPTAB_LIB_WIDEN);
3915 else
3917 /* We divided. Now finish doing X - Y * (X / Y). */
3918 remainder = expand_mult (compute_mode, quotient, op1,
3919 NULL_RTX, unsignedp);
3920 remainder = expand_binop (compute_mode, sub_optab, op0,
3921 remainder, target, unsignedp,
3922 OPTAB_LIB_WIDEN);
3926 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3929 /* Return a tree node with data type TYPE, describing the value of X.
3930 Usually this is an RTL_EXPR, if there is no obvious better choice.
3931 X may be an expression, however we only support those expressions
3932 generated by loop.c. */
3934 tree
3935 make_tree (type, x)
3936 tree type;
3937 rtx x;
3939 tree t;
3941 switch (GET_CODE (x))
3943 case CONST_INT:
3944 t = build_int_2 (INTVAL (x),
3945 (TREE_UNSIGNED (type)
3946 && (GET_MODE_BITSIZE (TYPE_MODE (type)) < HOST_BITS_PER_WIDE_INT))
3947 || INTVAL (x) >= 0 ? 0 : -1);
3948 TREE_TYPE (t) = type;
3949 return t;
3951 case CONST_DOUBLE:
3952 if (GET_MODE (x) == VOIDmode)
3954 t = build_int_2 (CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
3955 TREE_TYPE (t) = type;
3957 else
3959 REAL_VALUE_TYPE d;
3961 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
3962 t = build_real (type, d);
3965 return t;
3967 case PLUS:
3968 return fold (build (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
3969 make_tree (type, XEXP (x, 1))));
3971 case MINUS:
3972 return fold (build (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
3973 make_tree (type, XEXP (x, 1))));
3975 case NEG:
3976 return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
3978 case MULT:
3979 return fold (build (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
3980 make_tree (type, XEXP (x, 1))));
3982 case ASHIFT:
3983 return fold (build (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
3984 make_tree (type, XEXP (x, 1))));
3986 case LSHIFTRT:
3987 return fold (convert (type,
3988 build (RSHIFT_EXPR, unsigned_type (type),
3989 make_tree (unsigned_type (type),
3990 XEXP (x, 0)),
3991 make_tree (type, XEXP (x, 1)))));
3993 case ASHIFTRT:
3994 return fold (convert (type,
3995 build (RSHIFT_EXPR, signed_type (type),
3996 make_tree (signed_type (type), XEXP (x, 0)),
3997 make_tree (type, XEXP (x, 1)))));
3999 case DIV:
4000 if (TREE_CODE (type) != REAL_TYPE)
4001 t = signed_type (type);
4002 else
4003 t = type;
4005 return fold (convert (type,
4006 build (TRUNC_DIV_EXPR, t,
4007 make_tree (t, XEXP (x, 0)),
4008 make_tree (t, XEXP (x, 1)))));
4009 case UDIV:
4010 t = unsigned_type (type);
4011 return fold (convert (type,
4012 build (TRUNC_DIV_EXPR, t,
4013 make_tree (t, XEXP (x, 0)),
4014 make_tree (t, XEXP (x, 1)))));
4015 default:
4016 t = make_node (RTL_EXPR);
4017 TREE_TYPE (t) = type;
4018 RTL_EXPR_RTL (t) = x;
4019 /* There are no insns to be output
4020 when this rtl_expr is used. */
4021 RTL_EXPR_SEQUENCE (t) = 0;
4022 return t;
4026 /* Return an rtx representing the value of X * MULT + ADD.
4027 TARGET is a suggestion for where to store the result (an rtx).
4028 MODE is the machine mode for the computation.
4029 X and MULT must have mode MODE. ADD may have a different mode.
4030 So can X (defaults to same as MODE).
4031 UNSIGNEDP is non-zero to do unsigned multiplication.
4032 This may emit insns. */
4035 expand_mult_add (x, target, mult, add, mode, unsignedp)
4036 rtx x, target, mult, add;
4037 enum machine_mode mode;
4038 int unsignedp;
4040 tree type = type_for_mode (mode, unsignedp);
4041 tree add_type = (GET_MODE (add) == VOIDmode
4042 ? type : type_for_mode (GET_MODE (add), unsignedp));
4043 tree result = fold (build (PLUS_EXPR, type,
4044 fold (build (MULT_EXPR, type,
4045 make_tree (type, x),
4046 make_tree (type, mult))),
4047 make_tree (add_type, add)));
4049 return expand_expr (result, target, VOIDmode, 0);
4052 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
4053 and returning TARGET.
4055 If TARGET is 0, a pseudo-register or constant is returned. */
4058 expand_and (op0, op1, target)
4059 rtx op0, op1, target;
4061 enum machine_mode mode = VOIDmode;
4062 rtx tem;
4064 if (GET_MODE (op0) != VOIDmode)
4065 mode = GET_MODE (op0);
4066 else if (GET_MODE (op1) != VOIDmode)
4067 mode = GET_MODE (op1);
4069 if (mode != VOIDmode)
4070 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
4071 else if (GET_CODE (op0) == CONST_INT && GET_CODE (op1) == CONST_INT)
4072 tem = GEN_INT (INTVAL (op0) & INTVAL (op1));
4073 else
4074 abort ();
4076 if (target == 0)
4077 target = tem;
4078 else if (tem != target)
4079 emit_move_insn (target, tem);
4080 return target;
4083 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
4084 and storing in TARGET. Normally return TARGET.
4085 Return 0 if that cannot be done.
4087 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
4088 it is VOIDmode, they cannot both be CONST_INT.
4090 UNSIGNEDP is for the case where we have to widen the operands
4091 to perform the operation. It says to use zero-extension.
4093 NORMALIZEP is 1 if we should convert the result to be either zero
4094 or one. Normalize is -1 if we should convert the result to be
4095 either zero or -1. If NORMALIZEP is zero, the result will be left
4096 "raw" out of the scc insn. */
4099 emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep)
4100 rtx target;
4101 enum rtx_code code;
4102 rtx op0, op1;
4103 enum machine_mode mode;
4104 int unsignedp;
4105 int normalizep;
4107 rtx subtarget;
4108 enum insn_code icode;
4109 enum machine_mode compare_mode;
4110 enum machine_mode target_mode = GET_MODE (target);
4111 rtx tem;
4112 rtx last = get_last_insn ();
4113 rtx pattern, comparison;
4115 if (unsignedp)
4116 code = unsigned_condition (code);
4118 /* If one operand is constant, make it the second one. Only do this
4119 if the other operand is not constant as well. */
4121 if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
4122 || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
4124 tem = op0;
4125 op0 = op1;
4126 op1 = tem;
4127 code = swap_condition (code);
4130 if (mode == VOIDmode)
4131 mode = GET_MODE (op0);
4133 /* For some comparisons with 1 and -1, we can convert this to
4134 comparisons with zero. This will often produce more opportunities for
4135 store-flag insns. */
4137 switch (code)
4139 case LT:
4140 if (op1 == const1_rtx)
4141 op1 = const0_rtx, code = LE;
4142 break;
4143 case LE:
4144 if (op1 == constm1_rtx)
4145 op1 = const0_rtx, code = LT;
4146 break;
4147 case GE:
4148 if (op1 == const1_rtx)
4149 op1 = const0_rtx, code = GT;
4150 break;
4151 case GT:
4152 if (op1 == constm1_rtx)
4153 op1 = const0_rtx, code = GE;
4154 break;
4155 case GEU:
4156 if (op1 == const1_rtx)
4157 op1 = const0_rtx, code = NE;
4158 break;
4159 case LTU:
4160 if (op1 == const1_rtx)
4161 op1 = const0_rtx, code = EQ;
4162 break;
4163 default:
4164 break;
4167 /* From now on, we won't change CODE, so set ICODE now. */
4168 icode = setcc_gen_code[(int) code];
4170 /* If this is A < 0 or A >= 0, we can do this by taking the ones
4171 complement of A (for GE) and shifting the sign bit to the low bit. */
4172 if (op1 == const0_rtx && (code == LT || code == GE)
4173 && GET_MODE_CLASS (mode) == MODE_INT
4174 && (normalizep || STORE_FLAG_VALUE == 1
4175 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4176 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4177 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
4179 subtarget = target;
4181 /* If the result is to be wider than OP0, it is best to convert it
4182 first. If it is to be narrower, it is *incorrect* to convert it
4183 first. */
4184 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
4186 op0 = protect_from_queue (op0, 0);
4187 op0 = convert_modes (target_mode, mode, op0, 0);
4188 mode = target_mode;
4191 if (target_mode != mode)
4192 subtarget = 0;
4194 if (code == GE)
4195 op0 = expand_unop (mode, one_cmpl_optab, op0,
4196 ((STORE_FLAG_VALUE == 1 || normalizep)
4197 ? 0 : subtarget), 0);
4199 if (STORE_FLAG_VALUE == 1 || normalizep)
4200 /* If we are supposed to produce a 0/1 value, we want to do
4201 a logical shift from the sign bit to the low-order bit; for
4202 a -1/0 value, we do an arithmetic shift. */
4203 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
4204 size_int (GET_MODE_BITSIZE (mode) - 1),
4205 subtarget, normalizep != -1);
4207 if (mode != target_mode)
4208 op0 = convert_modes (target_mode, mode, op0, 0);
4210 return op0;
4213 if (icode != CODE_FOR_nothing)
4215 insn_operand_predicate_fn pred;
4217 /* We think we may be able to do this with a scc insn. Emit the
4218 comparison and then the scc insn.
4220 compare_from_rtx may call emit_queue, which would be deleted below
4221 if the scc insn fails. So call it ourselves before setting LAST. */
4223 emit_queue ();
4224 last = get_last_insn ();
4226 comparison
4227 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX, 0);
4228 if (GET_CODE (comparison) == CONST_INT)
4229 return (comparison == const0_rtx ? const0_rtx
4230 : normalizep == 1 ? const1_rtx
4231 : normalizep == -1 ? constm1_rtx
4232 : const_true_rtx);
4234 /* If the code of COMPARISON doesn't match CODE, something is
4235 wrong; we can no longer be sure that we have the operation.
4236 We could handle this case, but it should not happen. */
4238 if (GET_CODE (comparison) != code)
4239 abort ();
4241 /* Get a reference to the target in the proper mode for this insn. */
4242 compare_mode = insn_data[(int) icode].operand[0].mode;
4243 subtarget = target;
4244 pred = insn_data[(int) icode].operand[0].predicate;
4245 if (preserve_subexpressions_p ()
4246 || ! (*pred) (subtarget, compare_mode))
4247 subtarget = gen_reg_rtx (compare_mode);
4249 pattern = GEN_FCN (icode) (subtarget);
4250 if (pattern)
4252 emit_insn (pattern);
4254 /* If we are converting to a wider mode, first convert to
4255 TARGET_MODE, then normalize. This produces better combining
4256 opportunities on machines that have a SIGN_EXTRACT when we are
4257 testing a single bit. This mostly benefits the 68k.
4259 If STORE_FLAG_VALUE does not have the sign bit set when
4260 interpreted in COMPARE_MODE, we can do this conversion as
4261 unsigned, which is usually more efficient. */
4262 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
4264 convert_move (target, subtarget,
4265 (GET_MODE_BITSIZE (compare_mode)
4266 <= HOST_BITS_PER_WIDE_INT)
4267 && 0 == (STORE_FLAG_VALUE
4268 & ((HOST_WIDE_INT) 1
4269 << (GET_MODE_BITSIZE (compare_mode) -1))));
4270 op0 = target;
4271 compare_mode = target_mode;
4273 else
4274 op0 = subtarget;
4276 /* If we want to keep subexpressions around, don't reuse our
4277 last target. */
4279 if (preserve_subexpressions_p ())
4280 subtarget = 0;
4282 /* Now normalize to the proper value in COMPARE_MODE. Sometimes
4283 we don't have to do anything. */
4284 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
4286 /* STORE_FLAG_VALUE might be the most negative number, so write
4287 the comparison this way to avoid a compiler-time warning. */
4288 else if (- normalizep == STORE_FLAG_VALUE)
4289 op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
4291 /* We don't want to use STORE_FLAG_VALUE < 0 below since this
4292 makes it hard to use a value of just the sign bit due to
4293 ANSI integer constant typing rules. */
4294 else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
4295 && (STORE_FLAG_VALUE
4296 & ((HOST_WIDE_INT) 1
4297 << (GET_MODE_BITSIZE (compare_mode) - 1))))
4298 op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
4299 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
4300 subtarget, normalizep == 1);
4301 else if (STORE_FLAG_VALUE & 1)
4303 op0 = expand_and (op0, const1_rtx, subtarget);
4304 if (normalizep == -1)
4305 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
4307 else
4308 abort ();
4310 /* If we were converting to a smaller mode, do the
4311 conversion now. */
4312 if (target_mode != compare_mode)
4314 convert_move (target, op0, 0);
4315 return target;
4317 else
4318 return op0;
4322 delete_insns_since (last);
4324 /* If expensive optimizations, use different pseudo registers for each
4325 insn, instead of reusing the same pseudo. This leads to better CSE,
4326 but slows down the compiler, since there are more pseudos */
4327 subtarget = (!flag_expensive_optimizations
4328 && (target_mode == mode)) ? target : NULL_RTX;
4330 /* If we reached here, we can't do this with a scc insn. However, there
4331 are some comparisons that can be done directly. For example, if
4332 this is an equality comparison of integers, we can try to exclusive-or
4333 (or subtract) the two operands and use a recursive call to try the
4334 comparison with zero. Don't do any of these cases if branches are
4335 very cheap. */
4337 if (BRANCH_COST > 0
4338 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
4339 && op1 != const0_rtx)
4341 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
4342 OPTAB_WIDEN);
4344 if (tem == 0)
4345 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
4346 OPTAB_WIDEN);
4347 if (tem != 0)
4348 tem = emit_store_flag (target, code, tem, const0_rtx,
4349 mode, unsignedp, normalizep);
4350 if (tem == 0)
4351 delete_insns_since (last);
4352 return tem;
4355 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
4356 the constant zero. Reject all other comparisons at this point. Only
4357 do LE and GT if branches are expensive since they are expensive on
4358 2-operand machines. */
4360 if (BRANCH_COST == 0
4361 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
4362 || (code != EQ && code != NE
4363 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
4364 return 0;
4366 /* See what we need to return. We can only return a 1, -1, or the
4367 sign bit. */
4369 if (normalizep == 0)
4371 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4372 normalizep = STORE_FLAG_VALUE;
4374 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4375 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4376 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
4378 else
4379 return 0;
4382 /* Try to put the result of the comparison in the sign bit. Assume we can't
4383 do the necessary operation below. */
4385 tem = 0;
4387 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
4388 the sign bit set. */
4390 if (code == LE)
4392 /* This is destructive, so SUBTARGET can't be OP0. */
4393 if (rtx_equal_p (subtarget, op0))
4394 subtarget = 0;
4396 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
4397 OPTAB_WIDEN);
4398 if (tem)
4399 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
4400 OPTAB_WIDEN);
4403 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
4404 number of bits in the mode of OP0, minus one. */
4406 if (code == GT)
4408 if (rtx_equal_p (subtarget, op0))
4409 subtarget = 0;
4411 tem = expand_shift (RSHIFT_EXPR, mode, op0,
4412 size_int (GET_MODE_BITSIZE (mode) - 1),
4413 subtarget, 0);
4414 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
4415 OPTAB_WIDEN);
4418 if (code == EQ || code == NE)
4420 /* For EQ or NE, one way to do the comparison is to apply an operation
4421 that converts the operand into a positive number if it is non-zero
4422 or zero if it was originally zero. Then, for EQ, we subtract 1 and
4423 for NE we negate. This puts the result in the sign bit. Then we
4424 normalize with a shift, if needed.
4426 Two operations that can do the above actions are ABS and FFS, so try
4427 them. If that doesn't work, and MODE is smaller than a full word,
4428 we can use zero-extension to the wider mode (an unsigned conversion)
4429 as the operation. */
4431 if (abs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
4432 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
4433 else if (ffs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
4434 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
4435 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
4437 op0 = protect_from_queue (op0, 0);
4438 tem = convert_modes (word_mode, mode, op0, 1);
4439 mode = word_mode;
4442 if (tem != 0)
4444 if (code == EQ)
4445 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
4446 0, OPTAB_WIDEN);
4447 else
4448 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
4451 /* If we couldn't do it that way, for NE we can "or" the two's complement
4452 of the value with itself. For EQ, we take the one's complement of
4453 that "or", which is an extra insn, so we only handle EQ if branches
4454 are expensive. */
4456 if (tem == 0 && (code == NE || BRANCH_COST > 1))
4458 if (rtx_equal_p (subtarget, op0))
4459 subtarget = 0;
4461 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
4462 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
4463 OPTAB_WIDEN);
4465 if (tem && code == EQ)
4466 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
4470 if (tem && normalizep)
4471 tem = expand_shift (RSHIFT_EXPR, mode, tem,
4472 size_int (GET_MODE_BITSIZE (mode) - 1),
4473 subtarget, normalizep == 1);
4475 if (tem)
4477 if (GET_MODE (tem) != target_mode)
4479 convert_move (target, tem, 0);
4480 tem = target;
4482 else if (!subtarget)
4484 emit_move_insn (target, tem);
4485 tem = target;
4488 else
4489 delete_insns_since (last);
4491 return tem;
4494 /* Like emit_store_flag, but always succeeds. */
4497 emit_store_flag_force (target, code, op0, op1, mode, unsignedp, normalizep)
4498 rtx target;
4499 enum rtx_code code;
4500 rtx op0, op1;
4501 enum machine_mode mode;
4502 int unsignedp;
4503 int normalizep;
4505 rtx tem, label;
4507 /* First see if emit_store_flag can do the job. */
4508 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
4509 if (tem != 0)
4510 return tem;
4512 if (normalizep == 0)
4513 normalizep = 1;
4515 /* If this failed, we have to do this with set/compare/jump/set code. */
4517 if (GET_CODE (target) != REG
4518 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
4519 target = gen_reg_rtx (GET_MODE (target));
4521 emit_move_insn (target, const1_rtx);
4522 label = gen_label_rtx ();
4523 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX, 0,
4524 NULL_RTX, label);
4526 emit_move_insn (target, const0_rtx);
4527 emit_label (label);
4529 return target;
4532 /* Perform possibly multi-word comparison and conditional jump to LABEL
4533 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE
4535 The algorithm is based on the code in expr.c:do_jump.
4537 Note that this does not perform a general comparison. Only variants
4538 generated within expmed.c are correctly handled, others abort (but could
4539 be handled if needed). */
4541 static void
4542 do_cmp_and_jump (arg1, arg2, op, mode, label)
4543 rtx arg1, arg2, label;
4544 enum rtx_code op;
4545 enum machine_mode mode;
4547 /* If this mode is an integer too wide to compare properly,
4548 compare word by word. Rely on cse to optimize constant cases. */
4550 if (GET_MODE_CLASS (mode) == MODE_INT && ! can_compare_p (mode))
4552 rtx label2 = gen_label_rtx ();
4554 switch (op)
4556 case LTU:
4557 do_jump_by_parts_greater_rtx (mode, 1, arg2, arg1, label2, label);
4558 break;
4560 case LEU:
4561 do_jump_by_parts_greater_rtx (mode, 1, arg1, arg2, label, label2);
4562 break;
4564 case LT:
4565 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label2, label);
4566 break;
4568 case GT:
4569 do_jump_by_parts_greater_rtx (mode, 0, arg1, arg2, label2, label);
4570 break;
4572 case GE:
4573 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label, label2);
4574 break;
4576 /* do_jump_by_parts_equality_rtx compares with zero. Luckily
4577 that's the only equality operations we do */
4578 case EQ:
4579 if (arg2 != const0_rtx || mode != GET_MODE(arg1))
4580 abort();
4581 do_jump_by_parts_equality_rtx (arg1, label2, label);
4582 break;
4584 case NE:
4585 if (arg2 != const0_rtx || mode != GET_MODE(arg1))
4586 abort();
4587 do_jump_by_parts_equality_rtx (arg1, label, label2);
4588 break;
4590 default:
4591 abort();
4594 emit_label (label2);
4596 else
4598 emit_cmp_and_jump_insns (arg1, arg2, op, NULL_RTX, mode, 0, 0, label);