import of gcc-2.8
[official-gcc.git] / gcc / expmed.c
blobf419f24ae41aa40217eefda0d79c894d50e3eff5
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-6, 1997 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 <stdio.h>
25 #include "rtl.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "insn-flags.h"
29 #include "insn-codes.h"
30 #include "insn-config.h"
31 #include "expr.h"
32 #include "real.h"
33 #include "recog.h"
35 static void store_fixed_bit_field PROTO((rtx, int, int, int, rtx, int));
36 static void store_split_bit_field PROTO((rtx, int, int, rtx, int));
37 static rtx extract_fixed_bit_field PROTO((enum machine_mode, rtx, int,
38 int, int, rtx, int, int));
39 static rtx mask_rtx PROTO((enum machine_mode, int,
40 int, int));
41 static rtx lshift_value PROTO((enum machine_mode, rtx,
42 int, int));
43 static rtx extract_split_bit_field PROTO((rtx, int, int, int, int));
45 #define CEIL(x,y) (((x) + (y) - 1) / (y))
47 /* Non-zero means divides or modulus operations are relatively cheap for
48 powers of two, so don't use branches; emit the operation instead.
49 Usually, this will mean that the MD file will emit non-branch
50 sequences. */
52 static int sdiv_pow2_cheap, smod_pow2_cheap;
54 #ifndef SLOW_UNALIGNED_ACCESS
55 #define SLOW_UNALIGNED_ACCESS STRICT_ALIGNMENT
56 #endif
58 /* For compilers that support multiple targets with different word sizes,
59 MAX_BITS_PER_WORD contains the biggest value of BITS_PER_WORD. An example
60 is the H8/300(H) compiler. */
62 #ifndef MAX_BITS_PER_WORD
63 #define MAX_BITS_PER_WORD BITS_PER_WORD
64 #endif
66 /* Cost of various pieces of RTL. Note that some of these are indexed by shift count,
67 and some by mode. */
68 static int add_cost, negate_cost, zero_cost;
69 static int shift_cost[MAX_BITS_PER_WORD];
70 static int shiftadd_cost[MAX_BITS_PER_WORD];
71 static int shiftsub_cost[MAX_BITS_PER_WORD];
72 static int mul_cost[NUM_MACHINE_MODES];
73 static int div_cost[NUM_MACHINE_MODES];
74 static int mul_widen_cost[NUM_MACHINE_MODES];
75 static int mul_highpart_cost[NUM_MACHINE_MODES];
77 void
78 init_expmed ()
80 char *free_point;
81 /* This is "some random pseudo register" for purposes of calling recog
82 to see what insns exist. */
83 rtx reg = gen_rtx (REG, word_mode, 10000);
84 rtx shift_insn, shiftadd_insn, shiftsub_insn;
85 int dummy;
86 int m;
87 enum machine_mode mode, wider_mode;
89 start_sequence ();
91 /* Since we are on the permanent obstack, we must be sure we save this
92 spot AFTER we call start_sequence, since it will reuse the rtl it
93 makes. */
95 free_point = (char *) oballoc (0);
97 zero_cost = rtx_cost (const0_rtx, 0);
98 add_cost = rtx_cost (gen_rtx (PLUS, word_mode, reg, reg), SET);
100 shift_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
101 gen_rtx (ASHIFT, word_mode, reg,
102 const0_rtx)));
104 shiftadd_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
105 gen_rtx (PLUS, word_mode,
106 gen_rtx (MULT, word_mode,
107 reg, const0_rtx),
108 reg)));
110 shiftsub_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
111 gen_rtx (MINUS, word_mode,
112 gen_rtx (MULT, word_mode,
113 reg, const0_rtx),
114 reg)));
116 init_recog ();
118 shift_cost[0] = 0;
119 shiftadd_cost[0] = shiftsub_cost[0] = add_cost;
121 for (m = 1; m < BITS_PER_WORD; m++)
123 shift_cost[m] = shiftadd_cost[m] = shiftsub_cost[m] = 32000;
125 XEXP (SET_SRC (PATTERN (shift_insn)), 1) = GEN_INT (m);
126 if (recog (PATTERN (shift_insn), shift_insn, &dummy) >= 0)
127 shift_cost[m] = rtx_cost (SET_SRC (PATTERN (shift_insn)), SET);
129 XEXP (XEXP (SET_SRC (PATTERN (shiftadd_insn)), 0), 1)
130 = GEN_INT ((HOST_WIDE_INT) 1 << m);
131 if (recog (PATTERN (shiftadd_insn), shiftadd_insn, &dummy) >= 0)
132 shiftadd_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftadd_insn)), SET);
134 XEXP (XEXP (SET_SRC (PATTERN (shiftsub_insn)), 0), 1)
135 = GEN_INT ((HOST_WIDE_INT) 1 << m);
136 if (recog (PATTERN (shiftsub_insn), shiftsub_insn, &dummy) >= 0)
137 shiftsub_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftsub_insn)), SET);
140 negate_cost = rtx_cost (gen_rtx (NEG, word_mode, reg), SET);
142 sdiv_pow2_cheap
143 = (rtx_cost (gen_rtx (DIV, word_mode, reg, GEN_INT (32)), SET)
144 <= 2 * add_cost);
145 smod_pow2_cheap
146 = (rtx_cost (gen_rtx (MOD, word_mode, reg, GEN_INT (32)), SET)
147 <= 2 * add_cost);
149 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
150 mode != VOIDmode;
151 mode = GET_MODE_WIDER_MODE (mode))
153 reg = gen_rtx (REG, mode, 10000);
154 div_cost[(int) mode] = rtx_cost (gen_rtx (UDIV, mode, reg, reg), SET);
155 mul_cost[(int) mode] = rtx_cost (gen_rtx (MULT, mode, reg, reg), SET);
156 wider_mode = GET_MODE_WIDER_MODE (mode);
157 if (wider_mode != VOIDmode)
159 mul_widen_cost[(int) wider_mode]
160 = rtx_cost (gen_rtx (MULT, wider_mode,
161 gen_rtx (ZERO_EXTEND, wider_mode, reg),
162 gen_rtx (ZERO_EXTEND, wider_mode, reg)),
163 SET);
164 mul_highpart_cost[(int) mode]
165 = rtx_cost (gen_rtx (TRUNCATE, mode,
166 gen_rtx (LSHIFTRT, wider_mode,
167 gen_rtx (MULT, wider_mode,
168 gen_rtx (ZERO_EXTEND, wider_mode, reg),
169 gen_rtx (ZERO_EXTEND, wider_mode, reg)),
170 GEN_INT (GET_MODE_BITSIZE (mode)))),
171 SET);
175 /* Free the objects we just allocated. */
176 end_sequence ();
177 obfree (free_point);
180 /* Return an rtx representing minus the value of X.
181 MODE is the intended mode of the result,
182 useful if X is a CONST_INT. */
185 negate_rtx (mode, x)
186 enum machine_mode mode;
187 rtx x;
189 rtx result = simplify_unary_operation (NEG, mode, x, mode);
191 if (result == 0)
192 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
194 return result;
197 /* Generate code to store value from rtx VALUE
198 into a bit-field within structure STR_RTX
199 containing BITSIZE bits starting at bit BITNUM.
200 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
201 ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
202 TOTAL_SIZE is the size of the structure in bytes, or -1 if varying. */
204 /* ??? Note that there are two different ideas here for how
205 to determine the size to count bits within, for a register.
206 One is BITS_PER_WORD, and the other is the size of operand 3
207 of the insv pattern. (The latter assumes that an n-bit machine
208 will be able to insert bit fields up to n bits wide.)
209 It isn't certain that either of these is right.
210 extract_bit_field has the same quandary. */
213 store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, align, total_size)
214 rtx str_rtx;
215 register int bitsize;
216 int bitnum;
217 enum machine_mode fieldmode;
218 rtx value;
219 int align;
220 int total_size;
222 int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
223 register int offset = bitnum / unit;
224 register int bitpos = bitnum % unit;
225 register rtx op0 = str_rtx;
227 if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
228 abort ();
230 /* Discount the part of the structure before the desired byte.
231 We need to know how many bytes are safe to reference after it. */
232 if (total_size >= 0)
233 total_size -= (bitpos / BIGGEST_ALIGNMENT
234 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
236 while (GET_CODE (op0) == SUBREG)
238 /* The following line once was done only if WORDS_BIG_ENDIAN,
239 but I think that is a mistake. WORDS_BIG_ENDIAN is
240 meaningful at a much higher level; when structures are copied
241 between memory and regs, the higher-numbered regs
242 always get higher addresses. */
243 offset += SUBREG_WORD (op0);
244 /* We used to adjust BITPOS here, but now we do the whole adjustment
245 right after the loop. */
246 op0 = SUBREG_REG (op0);
249 /* If OP0 is a register, BITPOS must count within a word.
250 But as we have it, it counts within whatever size OP0 now has.
251 On a bigendian machine, these are not the same, so convert. */
252 if (BYTES_BIG_ENDIAN
253 && GET_CODE (op0) != MEM
254 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
255 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
257 value = protect_from_queue (value, 0);
259 if (flag_force_mem)
260 value = force_not_mem (value);
262 /* Note that the adjustment of BITPOS above has no effect on whether
263 BITPOS is 0 in a REG bigger than a word. */
264 if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
265 && (GET_CODE (op0) != MEM
266 || ! SLOW_UNALIGNED_ACCESS
267 || (offset * BITS_PER_UNIT % bitsize == 0
268 && align % GET_MODE_SIZE (fieldmode) == 0))
269 && bitpos == 0 && bitsize == GET_MODE_BITSIZE (fieldmode))
271 /* Storing in a full-word or multi-word field in a register
272 can be done with just SUBREG. */
273 if (GET_MODE (op0) != fieldmode)
275 if (GET_CODE (op0) == REG)
276 op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
277 else
278 op0 = change_address (op0, fieldmode,
279 plus_constant (XEXP (op0, 0), offset));
281 emit_move_insn (op0, value);
282 return value;
285 /* Storing an lsb-aligned field in a register
286 can be done with a movestrict instruction. */
288 if (GET_CODE (op0) != MEM
289 && (BYTES_BIG_ENDIAN ? bitpos + bitsize == unit : bitpos == 0)
290 && bitsize == GET_MODE_BITSIZE (fieldmode)
291 && (GET_MODE (op0) == fieldmode
292 || (movstrict_optab->handlers[(int) fieldmode].insn_code
293 != CODE_FOR_nothing)))
295 /* Get appropriate low part of the value being stored. */
296 if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
297 value = gen_lowpart (fieldmode, value);
298 else if (!(GET_CODE (value) == SYMBOL_REF
299 || GET_CODE (value) == LABEL_REF
300 || GET_CODE (value) == CONST))
301 value = convert_to_mode (fieldmode, value, 0);
303 if (GET_MODE (op0) == fieldmode)
304 emit_move_insn (op0, value);
305 else
307 int icode = movstrict_optab->handlers[(int) fieldmode].insn_code;
308 if(! (*insn_operand_predicate[icode][1]) (value, fieldmode))
309 value = copy_to_mode_reg (fieldmode, value);
310 emit_insn (GEN_FCN (icode)
311 (gen_rtx (SUBREG, fieldmode, op0, offset), value));
313 return value;
316 /* Handle fields bigger than a word. */
318 if (bitsize > BITS_PER_WORD)
320 /* Here we transfer the words of the field
321 in the order least significant first.
322 This is because the most significant word is the one which may
323 be less than full.
324 However, only do that if the value is not BLKmode. */
326 int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
328 int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
329 int i;
331 /* This is the mode we must force value to, so that there will be enough
332 subwords to extract. Note that fieldmode will often (always?) be
333 VOIDmode, because that is what store_field uses to indicate that this
334 is a bit field, but passing VOIDmode to operand_subword_force will
335 result in an abort. */
336 fieldmode = mode_for_size (nwords * BITS_PER_WORD, MODE_INT, 0);
338 for (i = 0; i < nwords; i++)
340 /* If I is 0, use the low-order word in both field and target;
341 if I is 1, use the next to lowest word; and so on. */
342 int wordnum = (backwards ? nwords - i - 1 : i);
343 int bit_offset = (backwards
344 ? MAX (bitsize - (i + 1) * BITS_PER_WORD, 0)
345 : i * BITS_PER_WORD);
346 store_bit_field (op0, MIN (BITS_PER_WORD,
347 bitsize - i * BITS_PER_WORD),
348 bitnum + bit_offset, word_mode,
349 operand_subword_force (value, wordnum,
350 (GET_MODE (value) == VOIDmode
351 ? fieldmode
352 : GET_MODE (value))),
353 align, total_size);
355 return value;
358 /* From here on we can assume that the field to be stored in is
359 a full-word (whatever type that is), since it is shorter than a word. */
361 /* OFFSET is the number of words or bytes (UNIT says which)
362 from STR_RTX to the first word or byte containing part of the field. */
364 if (GET_CODE (op0) == REG)
366 if (offset != 0
367 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
368 op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
369 op0, offset);
370 offset = 0;
372 else
374 op0 = protect_from_queue (op0, 1);
377 /* If VALUE is a floating-point mode, access it as an integer of the
378 corresponding size. This can occur on a machine with 64 bit registers
379 that uses SFmode for float. This can also occur for unaligned float
380 structure fields. */
381 if (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT)
383 if (GET_CODE (value) != REG)
384 value = copy_to_reg (value);
385 value = gen_rtx (SUBREG, word_mode, value, 0);
388 /* Now OFFSET is nonzero only if OP0 is memory
389 and is therefore always measured in bytes. */
391 #ifdef HAVE_insv
392 if (HAVE_insv
393 && GET_MODE (value) != BLKmode
394 && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
395 /* Ensure insv's size is wide enough for this field. */
396 && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_insv][3])
397 >= bitsize)
398 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
399 && (bitsize + bitpos
400 > GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_insv][3]))))
402 int xbitpos = bitpos;
403 rtx value1;
404 rtx xop0 = op0;
405 rtx last = get_last_insn ();
406 rtx pat;
407 enum machine_mode maxmode
408 = insn_operand_mode[(int) CODE_FOR_insv][3];
410 int save_volatile_ok = volatile_ok;
411 volatile_ok = 1;
413 /* If this machine's insv can only insert into a register, copy OP0
414 into a register and save it back later. */
415 /* This used to check flag_force_mem, but that was a serious
416 de-optimization now that flag_force_mem is enabled by -O2. */
417 if (GET_CODE (op0) == MEM
418 && ! ((*insn_operand_predicate[(int) CODE_FOR_insv][0])
419 (op0, VOIDmode)))
421 rtx tempreg;
422 enum machine_mode bestmode;
424 /* Get the mode to use for inserting into this field. If OP0 is
425 BLKmode, get the smallest mode consistent with the alignment. If
426 OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
427 mode. Otherwise, use the smallest mode containing the field. */
429 if (GET_MODE (op0) == BLKmode
430 || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
431 bestmode
432 = get_best_mode (bitsize, bitnum, align * BITS_PER_UNIT, maxmode,
433 MEM_VOLATILE_P (op0));
434 else
435 bestmode = GET_MODE (op0);
437 if (bestmode == VOIDmode
438 || (SLOW_UNALIGNED_ACCESS && GET_MODE_SIZE (bestmode) > align))
439 goto insv_loses;
441 /* Adjust address to point to the containing unit of that mode. */
442 unit = GET_MODE_BITSIZE (bestmode);
443 /* Compute offset as multiple of this unit, counting in bytes. */
444 offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
445 bitpos = bitnum % unit;
446 op0 = change_address (op0, bestmode,
447 plus_constant (XEXP (op0, 0), offset));
449 /* Fetch that unit, store the bitfield in it, then store the unit. */
450 tempreg = copy_to_reg (op0);
451 store_bit_field (tempreg, bitsize, bitpos, fieldmode, value,
452 align, total_size);
453 emit_move_insn (op0, tempreg);
454 return value;
456 volatile_ok = save_volatile_ok;
458 /* Add OFFSET into OP0's address. */
459 if (GET_CODE (xop0) == MEM)
460 xop0 = change_address (xop0, byte_mode,
461 plus_constant (XEXP (xop0, 0), offset));
463 /* If xop0 is a register, we need it in MAXMODE
464 to make it acceptable to the format of insv. */
465 if (GET_CODE (xop0) == SUBREG)
466 /* We can't just change the mode, because this might clobber op0,
467 and we will need the original value of op0 if insv fails. */
468 xop0 = gen_rtx (SUBREG, maxmode, SUBREG_REG (xop0), SUBREG_WORD (xop0));
469 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
470 xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
472 /* On big-endian machines, we count bits from the most significant.
473 If the bit field insn does not, we must invert. */
475 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
476 xbitpos = unit - bitsize - xbitpos;
478 /* We have been counting XBITPOS within UNIT.
479 Count instead within the size of the register. */
480 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
481 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
483 unit = GET_MODE_BITSIZE (maxmode);
485 /* Convert VALUE to maxmode (which insv insn wants) in VALUE1. */
486 value1 = value;
487 if (GET_MODE (value) != maxmode)
489 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
491 /* Optimization: Don't bother really extending VALUE
492 if it has all the bits we will actually use. However,
493 if we must narrow it, be sure we do it correctly. */
495 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
497 /* Avoid making subreg of a subreg, or of a mem. */
498 if (GET_CODE (value1) != REG)
499 value1 = copy_to_reg (value1);
500 value1 = gen_rtx (SUBREG, maxmode, value1, 0);
502 else
503 value1 = gen_lowpart (maxmode, value1);
505 else if (!CONSTANT_P (value))
506 /* Parse phase is supposed to make VALUE's data type
507 match that of the component reference, which is a type
508 at least as wide as the field; so VALUE should have
509 a mode that corresponds to that type. */
510 abort ();
513 /* If this machine's insv insists on a register,
514 get VALUE1 into a register. */
515 if (! ((*insn_operand_predicate[(int) CODE_FOR_insv][3])
516 (value1, maxmode)))
517 value1 = force_reg (maxmode, value1);
519 pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
520 if (pat)
521 emit_insn (pat);
522 else
524 delete_insns_since (last);
525 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
528 else
529 insv_loses:
530 #endif
531 /* Insv is not available; store using shifts and boolean ops. */
532 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
533 return value;
536 /* Use shifts and boolean operations to store VALUE
537 into a bit field of width BITSIZE
538 in a memory location specified by OP0 except offset by OFFSET bytes.
539 (OFFSET must be 0 if OP0 is a register.)
540 The field starts at position BITPOS within the byte.
541 (If OP0 is a register, it may be a full word or a narrower mode,
542 but BITPOS still counts within a full word,
543 which is significant on bigendian machines.)
544 STRUCT_ALIGN is the alignment the structure is known to have (in bytes).
546 Note that protect_from_queue has already been done on OP0 and VALUE. */
548 static void
549 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, struct_align)
550 register rtx op0;
551 register int offset, bitsize, bitpos;
552 register rtx value;
553 int struct_align;
555 register enum machine_mode mode;
556 int total_bits = BITS_PER_WORD;
557 rtx subtarget, temp;
558 int all_zero = 0;
559 int all_one = 0;
561 if (! SLOW_UNALIGNED_ACCESS)
562 struct_align = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
564 /* There is a case not handled here:
565 a structure with a known alignment of just a halfword
566 and a field split across two aligned halfwords within the structure.
567 Or likewise a structure with a known alignment of just a byte
568 and a field split across two bytes.
569 Such cases are not supposed to be able to occur. */
571 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
573 if (offset != 0)
574 abort ();
575 /* Special treatment for a bit field split across two registers. */
576 if (bitsize + bitpos > BITS_PER_WORD)
578 store_split_bit_field (op0, bitsize, bitpos,
579 value, BITS_PER_WORD);
580 return;
583 else
585 /* Get the proper mode to use for this field. We want a mode that
586 includes the entire field. If such a mode would be larger than
587 a word, we won't be doing the extraction the normal way. */
589 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
590 struct_align * BITS_PER_UNIT, word_mode,
591 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
593 if (mode == VOIDmode)
595 /* The only way this should occur is if the field spans word
596 boundaries. */
597 store_split_bit_field (op0,
598 bitsize, bitpos + offset * BITS_PER_UNIT,
599 value, struct_align);
600 return;
603 total_bits = GET_MODE_BITSIZE (mode);
605 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
606 be be in the range 0 to total_bits-1, and put any excess bytes in
607 OFFSET. */
608 if (bitpos >= total_bits)
610 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
611 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
612 * BITS_PER_UNIT);
615 /* Get ref to an aligned byte, halfword, or word containing the field.
616 Adjust BITPOS to be position within a word,
617 and OFFSET to be the offset of that word.
618 Then alter OP0 to refer to that word. */
619 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
620 offset -= (offset % (total_bits / BITS_PER_UNIT));
621 op0 = change_address (op0, mode,
622 plus_constant (XEXP (op0, 0), offset));
625 mode = GET_MODE (op0);
627 /* Now MODE is either some integral mode for a MEM as OP0,
628 or is a full-word for a REG as OP0. TOTAL_BITS corresponds.
629 The bit field is contained entirely within OP0.
630 BITPOS is the starting bit number within OP0.
631 (OP0's mode may actually be narrower than MODE.) */
633 if (BYTES_BIG_ENDIAN)
634 /* BITPOS is the distance between our msb
635 and that of the containing datum.
636 Convert it to the distance from the lsb. */
637 bitpos = total_bits - bitsize - bitpos;
639 /* Now BITPOS is always the distance between our lsb
640 and that of OP0. */
642 /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
643 we must first convert its mode to MODE. */
645 if (GET_CODE (value) == CONST_INT)
647 register HOST_WIDE_INT v = INTVAL (value);
649 if (bitsize < HOST_BITS_PER_WIDE_INT)
650 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
652 if (v == 0)
653 all_zero = 1;
654 else if ((bitsize < HOST_BITS_PER_WIDE_INT
655 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
656 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
657 all_one = 1;
659 value = lshift_value (mode, value, bitpos, bitsize);
661 else
663 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
664 && bitpos + bitsize != GET_MODE_BITSIZE (mode));
666 if (GET_MODE (value) != mode)
668 if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
669 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
670 value = gen_lowpart (mode, value);
671 else
672 value = convert_to_mode (mode, value, 1);
675 if (must_and)
676 value = expand_binop (mode, and_optab, value,
677 mask_rtx (mode, 0, bitsize, 0),
678 NULL_RTX, 1, OPTAB_LIB_WIDEN);
679 if (bitpos > 0)
680 value = expand_shift (LSHIFT_EXPR, mode, value,
681 build_int_2 (bitpos, 0), NULL_RTX, 1);
684 /* Now clear the chosen bits in OP0,
685 except that if VALUE is -1 we need not bother. */
687 subtarget = (GET_CODE (op0) == REG || ! flag_force_mem) ? op0 : 0;
689 if (! all_one)
691 temp = expand_binop (mode, and_optab, op0,
692 mask_rtx (mode, bitpos, bitsize, 1),
693 subtarget, 1, OPTAB_LIB_WIDEN);
694 subtarget = temp;
696 else
697 temp = op0;
699 /* Now logical-or VALUE into OP0, unless it is zero. */
701 if (! all_zero)
702 temp = expand_binop (mode, ior_optab, temp, value,
703 subtarget, 1, OPTAB_LIB_WIDEN);
704 if (op0 != temp)
705 emit_move_insn (op0, temp);
708 /* Store a bit field that is split across multiple accessible memory objects.
710 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
711 BITSIZE is the field width; BITPOS the position of its first bit
712 (within the word).
713 VALUE is the value to store.
714 ALIGN is the known alignment of OP0, measured in bytes.
715 This is also the size of the memory objects to be used.
717 This does not yet handle fields wider than BITS_PER_WORD. */
719 static void
720 store_split_bit_field (op0, bitsize, bitpos, value, align)
721 rtx op0;
722 int bitsize, bitpos;
723 rtx value;
724 int align;
726 int unit;
727 int bitsdone = 0;
729 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
730 much at a time. */
731 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
732 unit = BITS_PER_WORD;
733 else
734 unit = MIN (align * BITS_PER_UNIT, BITS_PER_WORD);
736 /* If VALUE is a constant other than a CONST_INT, get it into a register in
737 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
738 that VALUE might be a floating-point constant. */
739 if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
741 rtx word = gen_lowpart_common (word_mode, value);
743 if (word && (value != word))
744 value = word;
745 else
746 value = gen_lowpart_common (word_mode,
747 force_reg (GET_MODE (value) != VOIDmode
748 ? GET_MODE (value)
749 : word_mode, value));
751 else if (GET_CODE (value) == ADDRESSOF)
752 value = copy_to_reg (value);
754 while (bitsdone < bitsize)
756 int thissize;
757 rtx part, word;
758 int thispos;
759 int offset;
761 offset = (bitpos + bitsdone) / unit;
762 thispos = (bitpos + bitsdone) % unit;
764 /* THISSIZE must not overrun a word boundary. Otherwise,
765 store_fixed_bit_field will call us again, and we will mutually
766 recurse forever. */
767 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
768 thissize = MIN (thissize, unit - thispos);
770 if (BYTES_BIG_ENDIAN)
772 int total_bits;
774 /* We must do an endian conversion exactly the same way as it is
775 done in extract_bit_field, so that the two calls to
776 extract_fixed_bit_field will have comparable arguments. */
777 if (GET_CODE (value) != MEM || GET_MODE (value) == BLKmode)
778 total_bits = BITS_PER_WORD;
779 else
780 total_bits = GET_MODE_BITSIZE (GET_MODE (value));
782 /* Fetch successively less significant portions. */
783 if (GET_CODE (value) == CONST_INT)
784 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
785 >> (bitsize - bitsdone - thissize))
786 & (((HOST_WIDE_INT) 1 << thissize) - 1));
787 else
788 /* The args are chosen so that the last part includes the
789 lsb. Give extract_bit_field the value it needs (with
790 endianness compensation) to fetch the piece we want.
792 ??? We have no idea what the alignment of VALUE is, so
793 we have to use a guess. */
794 part
795 = extract_fixed_bit_field
796 (word_mode, value, 0, thissize,
797 total_bits - bitsize + bitsdone, NULL_RTX, 1,
798 GET_MODE (value) == VOIDmode
799 ? UNITS_PER_WORD
800 : (GET_MODE (value) == BLKmode
802 : GET_MODE_ALIGNMENT (GET_MODE (value)) / BITS_PER_UNIT));
804 else
806 /* Fetch successively more significant portions. */
807 if (GET_CODE (value) == CONST_INT)
808 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
809 >> bitsdone)
810 & (((HOST_WIDE_INT) 1 << thissize) - 1));
811 else
812 part
813 = extract_fixed_bit_field
814 (word_mode, value, 0, thissize, bitsdone, NULL_RTX, 1,
815 GET_MODE (value) == VOIDmode
816 ? UNITS_PER_WORD
817 : (GET_MODE (value) == BLKmode
819 : GET_MODE_ALIGNMENT (GET_MODE (value)) / BITS_PER_UNIT));
822 /* If OP0 is a register, then handle OFFSET here.
824 When handling multiword bitfields, extract_bit_field may pass
825 down a word_mode SUBREG of a larger REG for a bitfield that actually
826 crosses a word boundary. Thus, for a SUBREG, we must find
827 the current word starting from the base register. */
828 if (GET_CODE (op0) == SUBREG)
830 word = operand_subword_force (SUBREG_REG (op0),
831 SUBREG_WORD (op0) + offset,
832 GET_MODE (SUBREG_REG (op0)));
833 offset = 0;
835 else if (GET_CODE (op0) == REG)
837 word = operand_subword_force (op0, offset, GET_MODE (op0));
838 offset = 0;
840 else
841 word = op0;
843 /* OFFSET is in UNITs, and UNIT is in bits.
844 store_fixed_bit_field wants offset in bytes. */
845 store_fixed_bit_field (word, offset * unit / BITS_PER_UNIT,
846 thissize, thispos, part, align);
847 bitsdone += thissize;
851 /* Generate code to extract a byte-field from STR_RTX
852 containing BITSIZE bits, starting at BITNUM,
853 and put it in TARGET if possible (if TARGET is nonzero).
854 Regardless of TARGET, we return the rtx for where the value is placed.
855 It may be a QUEUED.
857 STR_RTX is the structure containing the byte (a REG or MEM).
858 UNSIGNEDP is nonzero if this is an unsigned bit field.
859 MODE is the natural mode of the field value once extracted.
860 TMODE is the mode the caller would like the value to have;
861 but the value may be returned with type MODE instead.
863 ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
864 TOTAL_SIZE is the size in bytes of the containing structure,
865 or -1 if varying.
867 If a TARGET is specified and we can store in it at no extra cost,
868 we do so, and return TARGET.
869 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
870 if they are equally easy. */
873 extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
874 target, mode, tmode, align, total_size)
875 rtx str_rtx;
876 register int bitsize;
877 int bitnum;
878 int unsignedp;
879 rtx target;
880 enum machine_mode mode, tmode;
881 int align;
882 int total_size;
884 int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
885 register int offset = bitnum / unit;
886 register int bitpos = bitnum % unit;
887 register rtx op0 = str_rtx;
888 rtx spec_target = target;
889 rtx spec_target_subreg = 0;
891 /* Discount the part of the structure before the desired byte.
892 We need to know how many bytes are safe to reference after it. */
893 if (total_size >= 0)
894 total_size -= (bitpos / BIGGEST_ALIGNMENT
895 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
897 if (tmode == VOIDmode)
898 tmode = mode;
899 while (GET_CODE (op0) == SUBREG)
901 int outer_size = GET_MODE_BITSIZE (GET_MODE (op0));
902 int inner_size = GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)));
904 offset += SUBREG_WORD (op0);
906 if (BYTES_BIG_ENDIAN && (outer_size < inner_size))
908 bitpos += inner_size - outer_size;
909 if (bitpos > unit)
911 offset += (bitpos / unit);
912 bitpos %= unit;
916 op0 = SUBREG_REG (op0);
919 /* ??? We currently assume TARGET is at least as big as BITSIZE.
920 If that's wrong, the solution is to test for it and set TARGET to 0
921 if needed. */
923 /* If OP0 is a register, BITPOS must count within a word.
924 But as we have it, it counts within whatever size OP0 now has.
925 On a bigendian machine, these are not the same, so convert. */
926 if (BYTES_BIG_ENDIAN
927 && GET_CODE (op0) != MEM
928 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
929 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
931 /* Extracting a full-word or multi-word value
932 from a structure in a register or aligned memory.
933 This can be done with just SUBREG.
934 So too extracting a subword value in
935 the least significant part of the register. */
937 if (((GET_CODE (op0) == REG
938 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
939 GET_MODE_BITSIZE (GET_MODE (op0))))
940 || (GET_CODE (op0) == MEM
941 && (! SLOW_UNALIGNED_ACCESS
942 || (offset * BITS_PER_UNIT % bitsize == 0
943 && align * BITS_PER_UNIT % bitsize == 0))))
944 && ((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
945 && bitpos % BITS_PER_WORD == 0)
946 || (mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0) != BLKmode
947 && (BYTES_BIG_ENDIAN
948 ? bitpos + bitsize == BITS_PER_WORD
949 : bitpos == 0))))
951 enum machine_mode mode1
952 = mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0);
954 if (mode1 != GET_MODE (op0))
956 if (GET_CODE (op0) == REG)
957 op0 = gen_rtx (SUBREG, mode1, op0, offset);
958 else
959 op0 = change_address (op0, mode1,
960 plus_constant (XEXP (op0, 0), offset));
962 if (mode1 != mode)
963 return convert_to_mode (tmode, op0, unsignedp);
964 return op0;
967 /* Handle fields bigger than a word. */
969 if (bitsize > BITS_PER_WORD)
971 /* Here we transfer the words of the field
972 in the order least significant first.
973 This is because the most significant word is the one which may
974 be less than full. */
976 int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
977 int i;
979 if (target == 0 || GET_CODE (target) != REG)
980 target = gen_reg_rtx (mode);
982 /* Indicate for flow that the entire target reg is being set. */
983 emit_insn (gen_rtx (CLOBBER, VOIDmode, target));
985 for (i = 0; i < nwords; i++)
987 /* If I is 0, use the low-order word in both field and target;
988 if I is 1, use the next to lowest word; and so on. */
989 /* Word number in TARGET to use. */
990 int wordnum = (WORDS_BIG_ENDIAN
991 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
992 : i);
993 /* Offset from start of field in OP0. */
994 int bit_offset = (WORDS_BIG_ENDIAN
995 ? MAX (0, bitsize - (i + 1) * BITS_PER_WORD)
996 : i * BITS_PER_WORD);
997 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
998 rtx result_part
999 = extract_bit_field (op0, MIN (BITS_PER_WORD,
1000 bitsize - i * BITS_PER_WORD),
1001 bitnum + bit_offset,
1002 1, target_part, mode, word_mode,
1003 align, total_size);
1005 if (target_part == 0)
1006 abort ();
1008 if (result_part != target_part)
1009 emit_move_insn (target_part, result_part);
1012 if (unsignedp)
1014 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1015 need to be zero'd out. */
1016 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1018 int i,total_words;
1020 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1021 for (i = nwords; i < total_words; i++)
1023 int wordnum = WORDS_BIG_ENDIAN ? total_words - i - 1 : i;
1024 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1025 emit_move_insn (target_part, const0_rtx);
1028 return target;
1031 /* Signed bit field: sign-extend with two arithmetic shifts. */
1032 target = expand_shift (LSHIFT_EXPR, mode, target,
1033 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1034 NULL_RTX, 0);
1035 return expand_shift (RSHIFT_EXPR, mode, target,
1036 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1037 NULL_RTX, 0);
1040 /* From here on we know the desired field is smaller than a word
1041 so we can assume it is an integer. So we can safely extract it as one
1042 size of integer, if necessary, and then truncate or extend
1043 to the size that is wanted. */
1045 /* OFFSET is the number of words or bytes (UNIT says which)
1046 from STR_RTX to the first word or byte containing part of the field. */
1048 if (GET_CODE (op0) == REG)
1050 if (offset != 0
1051 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1052 op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
1053 op0, offset);
1054 offset = 0;
1056 else
1058 op0 = protect_from_queue (str_rtx, 1);
1061 /* Now OFFSET is nonzero only for memory operands. */
1063 if (unsignedp)
1065 #ifdef HAVE_extzv
1066 if (HAVE_extzv
1067 && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extzv][0])
1068 >= bitsize)
1069 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
1070 && (bitsize + bitpos
1071 > GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extzv][0]))))
1073 int xbitpos = bitpos, xoffset = offset;
1074 rtx bitsize_rtx, bitpos_rtx;
1075 rtx last = get_last_insn();
1076 rtx xop0 = op0;
1077 rtx xtarget = target;
1078 rtx xspec_target = spec_target;
1079 rtx xspec_target_subreg = spec_target_subreg;
1080 rtx pat;
1081 enum machine_mode maxmode
1082 = insn_operand_mode[(int) CODE_FOR_extzv][0];
1084 if (GET_CODE (xop0) == MEM)
1086 int save_volatile_ok = volatile_ok;
1087 volatile_ok = 1;
1089 /* Is the memory operand acceptable? */
1090 if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
1091 (xop0, GET_MODE (xop0))))
1093 /* No, load into a reg and extract from there. */
1094 enum machine_mode bestmode;
1096 /* Get the mode to use for inserting into this field. If
1097 OP0 is BLKmode, get the smallest mode consistent with the
1098 alignment. If OP0 is a non-BLKmode object that is no
1099 wider than MAXMODE, use its mode. Otherwise, use the
1100 smallest mode containing the field. */
1102 if (GET_MODE (xop0) == BLKmode
1103 || (GET_MODE_SIZE (GET_MODE (op0))
1104 > GET_MODE_SIZE (maxmode)))
1105 bestmode = get_best_mode (bitsize, bitnum,
1106 align * BITS_PER_UNIT, maxmode,
1107 MEM_VOLATILE_P (xop0));
1108 else
1109 bestmode = GET_MODE (xop0);
1111 if (bestmode == VOIDmode
1112 || (SLOW_UNALIGNED_ACCESS && GET_MODE_SIZE (bestmode) > align))
1113 goto extzv_loses;
1115 /* Compute offset as multiple of this unit,
1116 counting in bytes. */
1117 unit = GET_MODE_BITSIZE (bestmode);
1118 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1119 xbitpos = bitnum % unit;
1120 xop0 = change_address (xop0, bestmode,
1121 plus_constant (XEXP (xop0, 0),
1122 xoffset));
1123 /* Fetch it to a register in that size. */
1124 xop0 = force_reg (bestmode, xop0);
1126 /* XBITPOS counts within UNIT, which is what is expected. */
1128 else
1129 /* Get ref to first byte containing part of the field. */
1130 xop0 = change_address (xop0, byte_mode,
1131 plus_constant (XEXP (xop0, 0), xoffset));
1133 volatile_ok = save_volatile_ok;
1136 /* If op0 is a register, we need it in MAXMODE (which is usually
1137 SImode). to make it acceptable to the format of extzv. */
1138 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1139 abort ();
1140 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
1141 xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
1143 /* On big-endian machines, we count bits from the most significant.
1144 If the bit field insn does not, we must invert. */
1145 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1146 xbitpos = unit - bitsize - xbitpos;
1148 /* Now convert from counting within UNIT to counting in MAXMODE. */
1149 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
1150 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
1152 unit = GET_MODE_BITSIZE (maxmode);
1154 if (xtarget == 0
1155 || (flag_force_mem && GET_CODE (xtarget) == MEM))
1156 xtarget = xspec_target = gen_reg_rtx (tmode);
1158 if (GET_MODE (xtarget) != maxmode)
1160 if (GET_CODE (xtarget) == REG)
1162 int wider = (GET_MODE_SIZE (maxmode)
1163 > GET_MODE_SIZE (GET_MODE (xtarget)));
1164 xtarget = gen_lowpart (maxmode, xtarget);
1165 if (wider)
1166 xspec_target_subreg = xtarget;
1168 else
1169 xtarget = gen_reg_rtx (maxmode);
1172 /* If this machine's extzv insists on a register target,
1173 make sure we have one. */
1174 if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
1175 (xtarget, maxmode)))
1176 xtarget = gen_reg_rtx (maxmode);
1178 bitsize_rtx = GEN_INT (bitsize);
1179 bitpos_rtx = GEN_INT (xbitpos);
1181 pat = gen_extzv (protect_from_queue (xtarget, 1),
1182 xop0, bitsize_rtx, bitpos_rtx);
1183 if (pat)
1185 emit_insn (pat);
1186 target = xtarget;
1187 spec_target = xspec_target;
1188 spec_target_subreg = xspec_target_subreg;
1190 else
1192 delete_insns_since (last);
1193 target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
1194 bitpos, target, 1, align);
1197 else
1198 extzv_loses:
1199 #endif
1200 target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1201 target, 1, align);
1203 else
1205 #ifdef HAVE_extv
1206 if (HAVE_extv
1207 && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extv][0])
1208 >= bitsize)
1209 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
1210 && (bitsize + bitpos
1211 > GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extv][0]))))
1213 int xbitpos = bitpos, xoffset = offset;
1214 rtx bitsize_rtx, bitpos_rtx;
1215 rtx last = get_last_insn();
1216 rtx xop0 = op0, xtarget = target;
1217 rtx xspec_target = spec_target;
1218 rtx xspec_target_subreg = spec_target_subreg;
1219 rtx pat;
1220 enum machine_mode maxmode
1221 = insn_operand_mode[(int) CODE_FOR_extv][0];
1223 if (GET_CODE (xop0) == MEM)
1225 /* Is the memory operand acceptable? */
1226 if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][1])
1227 (xop0, GET_MODE (xop0))))
1229 /* No, load into a reg and extract from there. */
1230 enum machine_mode bestmode;
1232 /* Get the mode to use for inserting into this field. If
1233 OP0 is BLKmode, get the smallest mode consistent with the
1234 alignment. If OP0 is a non-BLKmode object that is no
1235 wider than MAXMODE, use its mode. Otherwise, use the
1236 smallest mode containing the field. */
1238 if (GET_MODE (xop0) == BLKmode
1239 || (GET_MODE_SIZE (GET_MODE (op0))
1240 > GET_MODE_SIZE (maxmode)))
1241 bestmode = get_best_mode (bitsize, bitnum,
1242 align * BITS_PER_UNIT, maxmode,
1243 MEM_VOLATILE_P (xop0));
1244 else
1245 bestmode = GET_MODE (xop0);
1247 if (bestmode == VOIDmode
1248 || (SLOW_UNALIGNED_ACCESS && GET_MODE_SIZE (bestmode) > align))
1249 goto extv_loses;
1251 /* Compute offset as multiple of this unit,
1252 counting in bytes. */
1253 unit = GET_MODE_BITSIZE (bestmode);
1254 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1255 xbitpos = bitnum % unit;
1256 xop0 = change_address (xop0, bestmode,
1257 plus_constant (XEXP (xop0, 0),
1258 xoffset));
1259 /* Fetch it to a register in that size. */
1260 xop0 = force_reg (bestmode, xop0);
1262 /* XBITPOS counts within UNIT, which is what is expected. */
1264 else
1265 /* Get ref to first byte containing part of the field. */
1266 xop0 = change_address (xop0, byte_mode,
1267 plus_constant (XEXP (xop0, 0), xoffset));
1270 /* If op0 is a register, we need it in MAXMODE (which is usually
1271 SImode) to make it acceptable to the format of extv. */
1272 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1273 abort ();
1274 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
1275 xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
1277 /* On big-endian machines, we count bits from the most significant.
1278 If the bit field insn does not, we must invert. */
1279 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1280 xbitpos = unit - bitsize - xbitpos;
1282 /* XBITPOS counts within a size of UNIT.
1283 Adjust to count within a size of 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 extv insists on a register target,
1308 make sure we have one. */
1309 if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][0])
1310 (xtarget, maxmode)))
1311 xtarget = gen_reg_rtx (maxmode);
1313 bitsize_rtx = GEN_INT (bitsize);
1314 bitpos_rtx = GEN_INT (xbitpos);
1316 pat = gen_extv (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, 0, align);
1332 else
1333 extv_loses:
1334 #endif
1335 target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1336 target, 0, align);
1338 if (target == spec_target)
1339 return target;
1340 if (target == spec_target_subreg)
1341 return spec_target;
1342 if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
1344 /* If the target mode is floating-point, first convert to the
1345 integer mode of that size and then access it as a floating-point
1346 value via a SUBREG. */
1347 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1349 target = convert_to_mode (mode_for_size (GET_MODE_BITSIZE (tmode),
1350 MODE_INT, 0),
1351 target, unsignedp);
1352 if (GET_CODE (target) != REG)
1353 target = copy_to_reg (target);
1354 return gen_rtx (SUBREG, tmode, target, 0);
1356 else
1357 return convert_to_mode (tmode, target, unsignedp);
1359 return target;
1362 /* Extract a bit field using shifts and boolean operations
1363 Returns an rtx to represent the value.
1364 OP0 addresses a register (word) or memory (byte).
1365 BITPOS says which bit within the word or byte the bit field starts in.
1366 OFFSET says how many bytes farther the bit field starts;
1367 it is 0 if OP0 is a register.
1368 BITSIZE says how many bits long the bit field is.
1369 (If OP0 is a register, it may be narrower than a full word,
1370 but BITPOS still counts within a full word,
1371 which is significant on bigendian machines.)
1373 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1374 If TARGET is nonzero, attempts to store the value there
1375 and return TARGET, but this is not guaranteed.
1376 If TARGET is not used, create a pseudo-reg of mode TMODE for the value.
1378 ALIGN is the alignment that STR_RTX is known to have, measured in bytes. */
1380 static rtx
1381 extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1382 target, unsignedp, align)
1383 enum machine_mode tmode;
1384 register rtx op0, target;
1385 register int offset, bitsize, bitpos;
1386 int unsignedp;
1387 int align;
1389 int total_bits = BITS_PER_WORD;
1390 enum machine_mode mode;
1392 if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
1394 /* Special treatment for a bit field split across two registers. */
1395 if (bitsize + bitpos > BITS_PER_WORD)
1396 return extract_split_bit_field (op0, bitsize, bitpos,
1397 unsignedp, align);
1399 else
1401 /* Get the proper mode to use for this field. We want a mode that
1402 includes the entire field. If such a mode would be larger than
1403 a word, we won't be doing the extraction the normal way. */
1405 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1406 align * BITS_PER_UNIT, word_mode,
1407 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
1409 if (mode == VOIDmode)
1410 /* The only way this should occur is if the field spans word
1411 boundaries. */
1412 return extract_split_bit_field (op0, bitsize,
1413 bitpos + offset * BITS_PER_UNIT,
1414 unsignedp, align);
1416 total_bits = GET_MODE_BITSIZE (mode);
1418 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
1419 be be in the range 0 to total_bits-1, and put any excess bytes in
1420 OFFSET. */
1421 if (bitpos >= total_bits)
1423 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
1424 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
1425 * BITS_PER_UNIT);
1428 /* Get ref to an aligned byte, halfword, or word containing the field.
1429 Adjust BITPOS to be position within a word,
1430 and OFFSET to be the offset of that word.
1431 Then alter OP0 to refer to that word. */
1432 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1433 offset -= (offset % (total_bits / BITS_PER_UNIT));
1434 op0 = change_address (op0, mode,
1435 plus_constant (XEXP (op0, 0), offset));
1438 mode = GET_MODE (op0);
1440 if (BYTES_BIG_ENDIAN)
1442 /* BITPOS is the distance between our msb and that of OP0.
1443 Convert it to the distance from the lsb. */
1445 bitpos = total_bits - bitsize - bitpos;
1448 /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1449 We have reduced the big-endian case to the little-endian case. */
1451 if (unsignedp)
1453 if (bitpos)
1455 /* If the field does not already start at the lsb,
1456 shift it so it does. */
1457 tree amount = build_int_2 (bitpos, 0);
1458 /* Maybe propagate the target for the shift. */
1459 /* But not if we will return it--could confuse integrate.c. */
1460 rtx subtarget = (target != 0 && GET_CODE (target) == REG
1461 && !REG_FUNCTION_VALUE_P (target)
1462 ? target : 0);
1463 if (tmode != mode) subtarget = 0;
1464 op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1466 /* Convert the value to the desired mode. */
1467 if (mode != tmode)
1468 op0 = convert_to_mode (tmode, op0, 1);
1470 /* Unless the msb of the field used to be the msb when we shifted,
1471 mask out the upper bits. */
1473 if (GET_MODE_BITSIZE (mode) != bitpos + bitsize
1474 #if 0
1475 #ifdef SLOW_ZERO_EXTEND
1476 /* Always generate an `and' if
1477 we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
1478 will combine fruitfully with the zero-extend. */
1479 || tmode != mode
1480 #endif
1481 #endif
1483 return expand_binop (GET_MODE (op0), and_optab, op0,
1484 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1485 target, 1, OPTAB_LIB_WIDEN);
1486 return op0;
1489 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1490 then arithmetic-shift its lsb to the lsb of the word. */
1491 op0 = force_reg (mode, op0);
1492 if (mode != tmode)
1493 target = 0;
1495 /* Find the narrowest integer mode that contains the field. */
1497 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1498 mode = GET_MODE_WIDER_MODE (mode))
1499 if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1501 op0 = convert_to_mode (mode, op0, 0);
1502 break;
1505 if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1507 tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
1508 /* Maybe propagate the target for the shift. */
1509 /* But not if we will return the result--could confuse integrate.c. */
1510 rtx subtarget = (target != 0 && GET_CODE (target) == REG
1511 && ! REG_FUNCTION_VALUE_P (target)
1512 ? target : 0);
1513 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1516 return expand_shift (RSHIFT_EXPR, mode, op0,
1517 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1518 target, 0);
1521 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1522 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1523 complement of that if COMPLEMENT. The mask is truncated if
1524 necessary to the width of mode MODE. The mask is zero-extended if
1525 BITSIZE+BITPOS is too small for MODE. */
1527 static rtx
1528 mask_rtx (mode, bitpos, bitsize, complement)
1529 enum machine_mode mode;
1530 int bitpos, bitsize, complement;
1532 HOST_WIDE_INT masklow, maskhigh;
1534 if (bitpos < HOST_BITS_PER_WIDE_INT)
1535 masklow = (HOST_WIDE_INT) -1 << bitpos;
1536 else
1537 masklow = 0;
1539 if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1540 masklow &= ((unsigned HOST_WIDE_INT) -1
1541 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1543 if (bitpos <= HOST_BITS_PER_WIDE_INT)
1544 maskhigh = -1;
1545 else
1546 maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1548 if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
1549 maskhigh &= ((unsigned HOST_WIDE_INT) -1
1550 >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1551 else
1552 maskhigh = 0;
1554 if (complement)
1556 maskhigh = ~maskhigh;
1557 masklow = ~masklow;
1560 return immed_double_const (masklow, maskhigh, mode);
1563 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1564 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1566 static rtx
1567 lshift_value (mode, value, bitpos, bitsize)
1568 enum machine_mode mode;
1569 rtx value;
1570 int bitpos, bitsize;
1572 unsigned HOST_WIDE_INT v = INTVAL (value);
1573 HOST_WIDE_INT low, high;
1575 if (bitsize < HOST_BITS_PER_WIDE_INT)
1576 v &= ~((HOST_WIDE_INT) -1 << bitsize);
1578 if (bitpos < HOST_BITS_PER_WIDE_INT)
1580 low = v << bitpos;
1581 high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
1583 else
1585 low = 0;
1586 high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
1589 return immed_double_const (low, high, mode);
1592 /* Extract a bit field that is split across two words
1593 and return an RTX for the result.
1595 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1596 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1597 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.
1599 ALIGN is the known alignment of OP0, measured in bytes.
1600 This is also the size of the memory objects to be used. */
1602 static rtx
1603 extract_split_bit_field (op0, bitsize, bitpos, unsignedp, align)
1604 rtx op0;
1605 int bitsize, bitpos, unsignedp, align;
1607 int unit;
1608 int bitsdone = 0;
1609 rtx result;
1610 int first = 1;
1612 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1613 much at a time. */
1614 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
1615 unit = BITS_PER_WORD;
1616 else
1617 unit = MIN (align * BITS_PER_UNIT, BITS_PER_WORD);
1619 while (bitsdone < bitsize)
1621 int thissize;
1622 rtx part, word;
1623 int thispos;
1624 int offset;
1626 offset = (bitpos + bitsdone) / unit;
1627 thispos = (bitpos + bitsdone) % unit;
1629 /* THISSIZE must not overrun a word boundary. Otherwise,
1630 extract_fixed_bit_field will call us again, and we will mutually
1631 recurse forever. */
1632 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1633 thissize = MIN (thissize, unit - thispos);
1635 /* If OP0 is a register, then handle OFFSET here.
1637 When handling multiword bitfields, extract_bit_field may pass
1638 down a word_mode SUBREG of a larger REG for a bitfield that actually
1639 crosses a word boundary. Thus, for a SUBREG, we must find
1640 the current word starting from the base register. */
1641 if (GET_CODE (op0) == SUBREG)
1643 word = operand_subword_force (SUBREG_REG (op0),
1644 SUBREG_WORD (op0) + offset,
1645 GET_MODE (SUBREG_REG (op0)));
1646 offset = 0;
1648 else if (GET_CODE (op0) == REG)
1650 word = operand_subword_force (op0, offset, GET_MODE (op0));
1651 offset = 0;
1653 else
1654 word = op0;
1656 /* Extract the parts in bit-counting order,
1657 whose meaning is determined by BYTES_PER_UNIT.
1658 OFFSET is in UNITs, and UNIT is in bits.
1659 extract_fixed_bit_field wants offset in bytes. */
1660 part = extract_fixed_bit_field (word_mode, word,
1661 offset * unit / BITS_PER_UNIT,
1662 thissize, thispos, 0, 1, align);
1663 bitsdone += thissize;
1665 /* Shift this part into place for the result. */
1666 if (BYTES_BIG_ENDIAN)
1668 if (bitsize != bitsdone)
1669 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1670 build_int_2 (bitsize - bitsdone, 0), 0, 1);
1672 else
1674 if (bitsdone != thissize)
1675 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1676 build_int_2 (bitsdone - thissize, 0), 0, 1);
1679 if (first)
1680 result = part;
1681 else
1682 /* Combine the parts with bitwise or. This works
1683 because we extracted each part as an unsigned bit field. */
1684 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
1685 OPTAB_LIB_WIDEN);
1687 first = 0;
1690 /* Unsigned bit field: we are done. */
1691 if (unsignedp)
1692 return result;
1693 /* Signed bit field: sign-extend with two arithmetic shifts. */
1694 result = expand_shift (LSHIFT_EXPR, word_mode, result,
1695 build_int_2 (BITS_PER_WORD - bitsize, 0),
1696 NULL_RTX, 0);
1697 return expand_shift (RSHIFT_EXPR, word_mode, result,
1698 build_int_2 (BITS_PER_WORD - bitsize, 0), NULL_RTX, 0);
1701 /* Add INC into TARGET. */
1703 void
1704 expand_inc (target, inc)
1705 rtx target, inc;
1707 rtx value = expand_binop (GET_MODE (target), add_optab,
1708 target, inc,
1709 target, 0, OPTAB_LIB_WIDEN);
1710 if (value != target)
1711 emit_move_insn (target, value);
1714 /* Subtract DEC from TARGET. */
1716 void
1717 expand_dec (target, dec)
1718 rtx target, dec;
1720 rtx value = expand_binop (GET_MODE (target), sub_optab,
1721 target, dec,
1722 target, 0, OPTAB_LIB_WIDEN);
1723 if (value != target)
1724 emit_move_insn (target, value);
1727 /* Output a shift instruction for expression code CODE,
1728 with SHIFTED being the rtx for the value to shift,
1729 and AMOUNT the tree for the amount to shift by.
1730 Store the result in the rtx TARGET, if that is convenient.
1731 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
1732 Return the rtx for where the value is. */
1735 expand_shift (code, mode, shifted, amount, target, unsignedp)
1736 enum tree_code code;
1737 register enum machine_mode mode;
1738 rtx shifted;
1739 tree amount;
1740 register rtx target;
1741 int unsignedp;
1743 register rtx op1, temp = 0;
1744 register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
1745 register int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
1746 int try;
1748 /* Previously detected shift-counts computed by NEGATE_EXPR
1749 and shifted in the other direction; but that does not work
1750 on all machines. */
1752 op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
1754 #ifdef SHIFT_COUNT_TRUNCATED
1755 if (SHIFT_COUNT_TRUNCATED
1756 && GET_CODE (op1) == CONST_INT
1757 && (unsigned HOST_WIDE_INT) INTVAL (op1) >= GET_MODE_BITSIZE (mode))
1758 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
1759 % GET_MODE_BITSIZE (mode));
1760 #endif
1762 if (op1 == const0_rtx)
1763 return shifted;
1765 for (try = 0; temp == 0 && try < 3; try++)
1767 enum optab_methods methods;
1769 if (try == 0)
1770 methods = OPTAB_DIRECT;
1771 else if (try == 1)
1772 methods = OPTAB_WIDEN;
1773 else
1774 methods = OPTAB_LIB_WIDEN;
1776 if (rotate)
1778 /* Widening does not work for rotation. */
1779 if (methods == OPTAB_WIDEN)
1780 continue;
1781 else if (methods == OPTAB_LIB_WIDEN)
1783 /* If we have been unable to open-code this by a rotation,
1784 do it as the IOR of two shifts. I.e., to rotate A
1785 by N bits, compute (A << N) | ((unsigned) A >> (C - N))
1786 where C is the bitsize of A.
1788 It is theoretically possible that the target machine might
1789 not be able to perform either shift and hence we would
1790 be making two libcalls rather than just the one for the
1791 shift (similarly if IOR could not be done). We will allow
1792 this extremely unlikely lossage to avoid complicating the
1793 code below. */
1795 rtx subtarget = target == shifted ? 0 : target;
1796 rtx temp1;
1797 tree type = TREE_TYPE (amount);
1798 tree new_amount = make_tree (type, op1);
1799 tree other_amount
1800 = fold (build (MINUS_EXPR, type,
1801 convert (type,
1802 build_int_2 (GET_MODE_BITSIZE (mode),
1803 0)),
1804 amount));
1806 shifted = force_reg (mode, shifted);
1808 temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
1809 mode, shifted, new_amount, subtarget, 1);
1810 temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
1811 mode, shifted, other_amount, 0, 1);
1812 return expand_binop (mode, ior_optab, temp, temp1, target,
1813 unsignedp, methods);
1816 temp = expand_binop (mode,
1817 left ? rotl_optab : rotr_optab,
1818 shifted, op1, target, unsignedp, methods);
1820 /* If we don't have the rotate, but we are rotating by a constant
1821 that is in range, try a rotate in the opposite direction. */
1823 if (temp == 0 && GET_CODE (op1) == CONST_INT
1824 && INTVAL (op1) > 0 && INTVAL (op1) < GET_MODE_BITSIZE (mode))
1825 temp = expand_binop (mode,
1826 left ? rotr_optab : rotl_optab,
1827 shifted,
1828 GEN_INT (GET_MODE_BITSIZE (mode)
1829 - INTVAL (op1)),
1830 target, unsignedp, methods);
1832 else if (unsignedp)
1833 temp = expand_binop (mode,
1834 left ? ashl_optab : lshr_optab,
1835 shifted, op1, target, unsignedp, methods);
1837 /* Do arithmetic shifts.
1838 Also, if we are going to widen the operand, we can just as well
1839 use an arithmetic right-shift instead of a logical one. */
1840 if (temp == 0 && ! rotate
1841 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
1843 enum optab_methods methods1 = methods;
1845 /* If trying to widen a log shift to an arithmetic shift,
1846 don't accept an arithmetic shift of the same size. */
1847 if (unsignedp)
1848 methods1 = OPTAB_MUST_WIDEN;
1850 /* Arithmetic shift */
1852 temp = expand_binop (mode,
1853 left ? ashl_optab : ashr_optab,
1854 shifted, op1, target, unsignedp, methods1);
1857 /* We used to try extzv here for logical right shifts, but that was
1858 only useful for one machine, the VAX, and caused poor code
1859 generation there for lshrdi3, so the code was deleted and a
1860 define_expand for lshrsi3 was added to vax.md. */
1863 if (temp == 0)
1864 abort ();
1865 return temp;
1868 enum alg_code { alg_zero, alg_m, alg_shift,
1869 alg_add_t_m2, alg_sub_t_m2,
1870 alg_add_factor, alg_sub_factor,
1871 alg_add_t2_m, alg_sub_t2_m,
1872 alg_add, alg_subtract, alg_factor, alg_shiftop };
1874 /* This structure records a sequence of operations.
1875 `ops' is the number of operations recorded.
1876 `cost' is their total cost.
1877 The operations are stored in `op' and the corresponding
1878 logarithms of the integer coefficients in `log'.
1880 These are the operations:
1881 alg_zero total := 0;
1882 alg_m total := multiplicand;
1883 alg_shift total := total * coeff
1884 alg_add_t_m2 total := total + multiplicand * coeff;
1885 alg_sub_t_m2 total := total - multiplicand * coeff;
1886 alg_add_factor total := total * coeff + total;
1887 alg_sub_factor total := total * coeff - total;
1888 alg_add_t2_m total := total * coeff + multiplicand;
1889 alg_sub_t2_m total := total * coeff - multiplicand;
1891 The first operand must be either alg_zero or alg_m. */
1893 struct algorithm
1895 short cost;
1896 short ops;
1897 /* The size of the OP and LOG fields are not directly related to the
1898 word size, but the worst-case algorithms will be if we have few
1899 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
1900 In that case we will generate shift-by-2, add, shift-by-2, add,...,
1901 in total wordsize operations. */
1902 enum alg_code op[MAX_BITS_PER_WORD];
1903 char log[MAX_BITS_PER_WORD];
1906 /* Compute and return the best algorithm for multiplying by T.
1907 The algorithm must cost less than cost_limit
1908 If retval.cost >= COST_LIMIT, no algorithm was found and all
1909 other field of the returned struct are undefined. */
1911 static void
1912 synth_mult (alg_out, t, cost_limit)
1913 struct algorithm *alg_out;
1914 unsigned HOST_WIDE_INT t;
1915 int cost_limit;
1917 int m;
1918 struct algorithm *alg_in, *best_alg;
1919 unsigned int cost;
1920 unsigned HOST_WIDE_INT q;
1922 /* Indicate that no algorithm is yet found. If no algorithm
1923 is found, this value will be returned and indicate failure. */
1924 alg_out->cost = cost_limit;
1926 if (cost_limit <= 0)
1927 return;
1929 /* t == 1 can be done in zero cost. */
1930 if (t == 1)
1932 alg_out->ops = 1;
1933 alg_out->cost = 0;
1934 alg_out->op[0] = alg_m;
1935 return;
1938 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
1939 fail now. */
1940 if (t == 0)
1942 if (zero_cost >= cost_limit)
1943 return;
1944 else
1946 alg_out->ops = 1;
1947 alg_out->cost = zero_cost;
1948 alg_out->op[0] = alg_zero;
1949 return;
1953 /* We'll be needing a couple extra algorithm structures now. */
1955 alg_in = (struct algorithm *)alloca (sizeof (struct algorithm));
1956 best_alg = (struct algorithm *)alloca (sizeof (struct algorithm));
1958 /* If we have a group of zero bits at the low-order part of T, try
1959 multiplying by the remaining bits and then doing a shift. */
1961 if ((t & 1) == 0)
1963 m = floor_log2 (t & -t); /* m = number of low zero bits */
1964 q = t >> m;
1965 cost = shift_cost[m];
1966 synth_mult (alg_in, q, cost_limit - cost);
1968 cost += alg_in->cost;
1969 if (cost < cost_limit)
1971 struct algorithm *x;
1972 x = alg_in, alg_in = best_alg, best_alg = x;
1973 best_alg->log[best_alg->ops] = m;
1974 best_alg->op[best_alg->ops] = alg_shift;
1975 cost_limit = cost;
1979 /* If we have an odd number, add or subtract one. */
1980 if ((t & 1) != 0)
1982 unsigned HOST_WIDE_INT w;
1984 for (w = 1; (w & t) != 0; w <<= 1)
1986 if (w > 2
1987 /* Reject the case where t is 3.
1988 Thus we prefer addition in that case. */
1989 && t != 3)
1991 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
1993 cost = add_cost;
1994 synth_mult (alg_in, t + 1, cost_limit - cost);
1996 cost += alg_in->cost;
1997 if (cost < cost_limit)
1999 struct algorithm *x;
2000 x = alg_in, alg_in = best_alg, best_alg = x;
2001 best_alg->log[best_alg->ops] = 0;
2002 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2003 cost_limit = cost;
2006 else
2008 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2010 cost = add_cost;
2011 synth_mult (alg_in, t - 1, cost_limit - cost);
2013 cost += alg_in->cost;
2014 if (cost < cost_limit)
2016 struct algorithm *x;
2017 x = alg_in, alg_in = best_alg, best_alg = x;
2018 best_alg->log[best_alg->ops] = 0;
2019 best_alg->op[best_alg->ops] = alg_add_t_m2;
2020 cost_limit = cost;
2025 /* Look for factors of t of the form
2026 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2027 If we find such a factor, we can multiply by t using an algorithm that
2028 multiplies by q, shift the result by m and add/subtract it to itself.
2030 We search for large factors first and loop down, even if large factors
2031 are less probable than small; if we find a large factor we will find a
2032 good sequence quickly, and therefore be able to prune (by decreasing
2033 COST_LIMIT) the search. */
2035 for (m = floor_log2 (t - 1); m >= 2; m--)
2037 unsigned HOST_WIDE_INT d;
2039 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2040 if (t % d == 0 && t > d)
2042 cost = MIN (shiftadd_cost[m], add_cost + shift_cost[m]);
2043 synth_mult (alg_in, t / d, cost_limit - cost);
2045 cost += alg_in->cost;
2046 if (cost < cost_limit)
2048 struct algorithm *x;
2049 x = alg_in, alg_in = best_alg, best_alg = x;
2050 best_alg->log[best_alg->ops] = m;
2051 best_alg->op[best_alg->ops] = alg_add_factor;
2052 cost_limit = cost;
2054 /* Other factors will have been taken care of in the recursion. */
2055 break;
2058 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2059 if (t % d == 0 && t > d)
2061 cost = MIN (shiftsub_cost[m], add_cost + shift_cost[m]);
2062 synth_mult (alg_in, t / d, cost_limit - cost);
2064 cost += alg_in->cost;
2065 if (cost < cost_limit)
2067 struct algorithm *x;
2068 x = alg_in, alg_in = best_alg, best_alg = x;
2069 best_alg->log[best_alg->ops] = m;
2070 best_alg->op[best_alg->ops] = alg_sub_factor;
2071 cost_limit = cost;
2073 break;
2077 /* Try shift-and-add (load effective address) instructions,
2078 i.e. do a*3, a*5, a*9. */
2079 if ((t & 1) != 0)
2081 q = t - 1;
2082 q = q & -q;
2083 m = exact_log2 (q);
2084 if (m >= 0)
2086 cost = shiftadd_cost[m];
2087 synth_mult (alg_in, (t - 1) >> m, cost_limit - cost);
2089 cost += alg_in->cost;
2090 if (cost < cost_limit)
2092 struct algorithm *x;
2093 x = alg_in, alg_in = best_alg, best_alg = x;
2094 best_alg->log[best_alg->ops] = m;
2095 best_alg->op[best_alg->ops] = alg_add_t2_m;
2096 cost_limit = cost;
2100 q = t + 1;
2101 q = q & -q;
2102 m = exact_log2 (q);
2103 if (m >= 0)
2105 cost = shiftsub_cost[m];
2106 synth_mult (alg_in, (t + 1) >> m, cost_limit - cost);
2108 cost += alg_in->cost;
2109 if (cost < cost_limit)
2111 struct algorithm *x;
2112 x = alg_in, alg_in = best_alg, best_alg = x;
2113 best_alg->log[best_alg->ops] = m;
2114 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2115 cost_limit = cost;
2120 /* If cost_limit has not decreased since we stored it in alg_out->cost,
2121 we have not found any algorithm. */
2122 if (cost_limit == alg_out->cost)
2123 return;
2125 /* If we are getting a too long sequence for `struct algorithm'
2126 to record, make this search fail. */
2127 if (best_alg->ops == MAX_BITS_PER_WORD)
2128 return;
2130 /* Copy the algorithm from temporary space to the space at alg_out.
2131 We avoid using structure assignment because the majority of
2132 best_alg is normally undefined, and this is a critical function. */
2133 alg_out->ops = best_alg->ops + 1;
2134 alg_out->cost = cost_limit;
2135 bcopy ((char *) best_alg->op, (char *) alg_out->op,
2136 alg_out->ops * sizeof *alg_out->op);
2137 bcopy ((char *) best_alg->log, (char *) alg_out->log,
2138 alg_out->ops * sizeof *alg_out->log);
2141 /* Perform a multiplication and return an rtx for the result.
2142 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
2143 TARGET is a suggestion for where to store the result (an rtx).
2145 We check specially for a constant integer as OP1.
2146 If you want this check for OP0 as well, then before calling
2147 you should swap the two operands if OP0 would be constant. */
2150 expand_mult (mode, op0, op1, target, unsignedp)
2151 enum machine_mode mode;
2152 register rtx op0, op1, target;
2153 int unsignedp;
2155 rtx const_op1 = op1;
2157 /* synth_mult does an `unsigned int' multiply. As long as the mode is
2158 less than or equal in size to `unsigned int' this doesn't matter.
2159 If the mode is larger than `unsigned int', then synth_mult works only
2160 if the constant value exactly fits in an `unsigned int' without any
2161 truncation. This means that multiplying by negative values does
2162 not work; results are off by 2^32 on a 32 bit machine. */
2164 /* If we are multiplying in DImode, it may still be a win
2165 to try to work with shifts and adds. */
2166 if (GET_CODE (op1) == CONST_DOUBLE
2167 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
2168 && HOST_BITS_PER_INT >= BITS_PER_WORD
2169 && CONST_DOUBLE_HIGH (op1) == 0)
2170 const_op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2171 else if (HOST_BITS_PER_INT < GET_MODE_BITSIZE (mode)
2172 && GET_CODE (op1) == CONST_INT
2173 && INTVAL (op1) < 0)
2174 const_op1 = 0;
2176 /* We used to test optimize here, on the grounds that it's better to
2177 produce a smaller program when -O is not used.
2178 But this causes such a terrible slowdown sometimes
2179 that it seems better to use synth_mult always. */
2181 if (const_op1 && GET_CODE (const_op1) == CONST_INT)
2183 struct algorithm alg;
2184 struct algorithm alg2;
2185 HOST_WIDE_INT val = INTVAL (op1);
2186 HOST_WIDE_INT val_so_far;
2187 rtx insn;
2188 int mult_cost;
2189 enum {basic_variant, negate_variant, add_variant} variant = basic_variant;
2191 /* Try to do the computation three ways: multiply by the negative of OP1
2192 and then negate, do the multiplication directly, or do multiplication
2193 by OP1 - 1. */
2195 mult_cost = rtx_cost (gen_rtx (MULT, mode, op0, op1), SET);
2196 mult_cost = MIN (12 * add_cost, mult_cost);
2198 synth_mult (&alg, val, mult_cost);
2200 /* This works only if the inverted value actually fits in an
2201 `unsigned int' */
2202 if (HOST_BITS_PER_INT >= GET_MODE_BITSIZE (mode))
2204 synth_mult (&alg2, - val,
2205 (alg.cost < mult_cost ? alg.cost : mult_cost) - negate_cost);
2206 if (alg2.cost + negate_cost < alg.cost)
2207 alg = alg2, variant = negate_variant;
2210 /* This proves very useful for division-by-constant. */
2211 synth_mult (&alg2, val - 1,
2212 (alg.cost < mult_cost ? alg.cost : mult_cost) - add_cost);
2213 if (alg2.cost + add_cost < alg.cost)
2214 alg = alg2, variant = add_variant;
2216 if (alg.cost < mult_cost)
2218 /* We found something cheaper than a multiply insn. */
2219 int opno;
2220 rtx accum, tem;
2222 op0 = protect_from_queue (op0, 0);
2224 /* Avoid referencing memory over and over.
2225 For speed, but also for correctness when mem is volatile. */
2226 if (GET_CODE (op0) == MEM)
2227 op0 = force_reg (mode, op0);
2229 /* ACCUM starts out either as OP0 or as a zero, depending on
2230 the first operation. */
2232 if (alg.op[0] == alg_zero)
2234 accum = copy_to_mode_reg (mode, const0_rtx);
2235 val_so_far = 0;
2237 else if (alg.op[0] == alg_m)
2239 accum = copy_to_mode_reg (mode, op0);
2240 val_so_far = 1;
2242 else
2243 abort ();
2245 for (opno = 1; opno < alg.ops; opno++)
2247 int log = alg.log[opno];
2248 int preserve = preserve_subexpressions_p ();
2249 rtx shift_subtarget = preserve ? 0 : accum;
2250 rtx add_target
2251 = (opno == alg.ops - 1 && target != 0 && variant != add_variant
2252 ? target : 0);
2253 rtx accum_target = preserve ? 0 : accum;
2255 switch (alg.op[opno])
2257 case alg_shift:
2258 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2259 build_int_2 (log, 0), NULL_RTX, 0);
2260 val_so_far <<= log;
2261 break;
2263 case alg_add_t_m2:
2264 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2265 build_int_2 (log, 0), NULL_RTX, 0);
2266 accum = force_operand (gen_rtx (PLUS, mode, accum, tem),
2267 add_target ? add_target : accum_target);
2268 val_so_far += (HOST_WIDE_INT) 1 << log;
2269 break;
2271 case alg_sub_t_m2:
2272 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2273 build_int_2 (log, 0), NULL_RTX, 0);
2274 accum = force_operand (gen_rtx (MINUS, mode, accum, tem),
2275 add_target ? add_target : accum_target);
2276 val_so_far -= (HOST_WIDE_INT) 1 << log;
2277 break;
2279 case alg_add_t2_m:
2280 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2281 build_int_2 (log, 0), shift_subtarget,
2283 accum = force_operand (gen_rtx (PLUS, mode, accum, op0),
2284 add_target ? add_target : accum_target);
2285 val_so_far = (val_so_far << log) + 1;
2286 break;
2288 case alg_sub_t2_m:
2289 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2290 build_int_2 (log, 0), shift_subtarget,
2292 accum = force_operand (gen_rtx (MINUS, mode, accum, op0),
2293 add_target ? add_target : accum_target);
2294 val_so_far = (val_so_far << log) - 1;
2295 break;
2297 case alg_add_factor:
2298 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2299 build_int_2 (log, 0), NULL_RTX, 0);
2300 accum = force_operand (gen_rtx (PLUS, mode, accum, tem),
2301 add_target ? add_target : accum_target);
2302 val_so_far += val_so_far << log;
2303 break;
2305 case alg_sub_factor:
2306 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2307 build_int_2 (log, 0), NULL_RTX, 0);
2308 accum = force_operand (gen_rtx (MINUS, mode, tem, accum),
2309 (add_target ? add_target
2310 : preserve ? 0 : tem));
2311 val_so_far = (val_so_far << log) - val_so_far;
2312 break;
2314 default:
2315 abort ();;
2318 /* Write a REG_EQUAL note on the last insn so that we can cse
2319 multiplication sequences. */
2321 insn = get_last_insn ();
2322 REG_NOTES (insn)
2323 = gen_rtx (EXPR_LIST, REG_EQUAL,
2324 gen_rtx (MULT, mode, op0, GEN_INT (val_so_far)),
2325 REG_NOTES (insn));
2328 if (variant == negate_variant)
2330 val_so_far = - val_so_far;
2331 accum = expand_unop (mode, neg_optab, accum, target, 0);
2333 else if (variant == add_variant)
2335 val_so_far = val_so_far + 1;
2336 accum = force_operand (gen_rtx (PLUS, mode, accum, op0), target);
2339 if (val != val_so_far)
2340 abort ();
2342 return accum;
2346 /* This used to use umul_optab if unsigned, but for non-widening multiply
2347 there is no difference between signed and unsigned. */
2348 op0 = expand_binop (mode, smul_optab,
2349 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
2350 if (op0 == 0)
2351 abort ();
2352 return op0;
2355 /* Return the smallest n such that 2**n >= X. */
2358 ceil_log2 (x)
2359 unsigned HOST_WIDE_INT x;
2361 return floor_log2 (x - 1) + 1;
2364 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
2365 replace division by D, and put the least significant N bits of the result
2366 in *MULTIPLIER_PTR and return the most significant bit.
2368 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
2369 needed precision is in PRECISION (should be <= N).
2371 PRECISION should be as small as possible so this function can choose
2372 multiplier more freely.
2374 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
2375 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
2377 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
2378 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
2380 static
2381 unsigned HOST_WIDE_INT
2382 choose_multiplier (d, n, precision, multiplier_ptr, post_shift_ptr, lgup_ptr)
2383 unsigned HOST_WIDE_INT d;
2384 int n;
2385 int precision;
2386 unsigned HOST_WIDE_INT *multiplier_ptr;
2387 int *post_shift_ptr;
2388 int *lgup_ptr;
2390 unsigned HOST_WIDE_INT mhigh_hi, mhigh_lo;
2391 unsigned HOST_WIDE_INT mlow_hi, mlow_lo;
2392 int lgup, post_shift;
2393 int pow, pow2;
2394 unsigned HOST_WIDE_INT nh, nl, dummy1, dummy2;
2396 /* lgup = ceil(log2(divisor)); */
2397 lgup = ceil_log2 (d);
2399 if (lgup > n)
2400 abort ();
2402 pow = n + lgup;
2403 pow2 = n + lgup - precision;
2405 if (pow == 2 * HOST_BITS_PER_WIDE_INT)
2407 /* We could handle this with some effort, but this case is much better
2408 handled directly with a scc insn, so rely on caller using that. */
2409 abort ();
2412 /* mlow = 2^(N + lgup)/d */
2413 if (pow >= HOST_BITS_PER_WIDE_INT)
2415 nh = (unsigned HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
2416 nl = 0;
2418 else
2420 nh = 0;
2421 nl = (unsigned HOST_WIDE_INT) 1 << pow;
2423 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
2424 &mlow_lo, &mlow_hi, &dummy1, &dummy2);
2426 /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
2427 if (pow2 >= HOST_BITS_PER_WIDE_INT)
2428 nh |= (unsigned HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
2429 else
2430 nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
2431 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
2432 &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
2434 if (mhigh_hi && nh - d >= d)
2435 abort ();
2436 if (mhigh_hi > 1 || mlow_hi > 1)
2437 abort ();
2438 /* assert that mlow < mhigh. */
2439 if (! (mlow_hi < mhigh_hi || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo)))
2440 abort();
2442 /* If precision == N, then mlow, mhigh exceed 2^N
2443 (but they do not exceed 2^(N+1)). */
2445 /* Reduce to lowest terms */
2446 for (post_shift = lgup; post_shift > 0; post_shift--)
2448 unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
2449 unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
2450 if (ml_lo >= mh_lo)
2451 break;
2453 mlow_hi = 0;
2454 mlow_lo = ml_lo;
2455 mhigh_hi = 0;
2456 mhigh_lo = mh_lo;
2459 *post_shift_ptr = post_shift;
2460 *lgup_ptr = lgup;
2461 if (n < HOST_BITS_PER_WIDE_INT)
2463 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
2464 *multiplier_ptr = mhigh_lo & mask;
2465 return mhigh_lo >= mask;
2467 else
2469 *multiplier_ptr = mhigh_lo;
2470 return mhigh_hi;
2474 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
2475 congruent to 1 (mod 2**N). */
2477 static unsigned HOST_WIDE_INT
2478 invert_mod2n (x, n)
2479 unsigned HOST_WIDE_INT x;
2480 int n;
2482 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
2484 /* The algorithm notes that the choice y = x satisfies
2485 x*y == 1 mod 2^3, since x is assumed odd.
2486 Each iteration doubles the number of bits of significance in y. */
2488 unsigned HOST_WIDE_INT mask;
2489 unsigned HOST_WIDE_INT y = x;
2490 int nbit = 3;
2492 mask = (n == HOST_BITS_PER_WIDE_INT
2493 ? ~(unsigned HOST_WIDE_INT) 0
2494 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
2496 while (nbit < n)
2498 y = y * (2 - x*y) & mask; /* Modulo 2^N */
2499 nbit *= 2;
2501 return y;
2504 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
2505 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
2506 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
2507 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
2508 become signed.
2510 The result is put in TARGET if that is convenient.
2512 MODE is the mode of operation. */
2515 expand_mult_highpart_adjust (mode, adj_operand, op0, op1, target, unsignedp)
2516 enum machine_mode mode;
2517 register rtx adj_operand, op0, op1, target;
2518 int unsignedp;
2520 rtx tem;
2521 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
2523 tem = expand_shift (RSHIFT_EXPR, mode, op0,
2524 build_int_2 (GET_MODE_BITSIZE (mode) - 1, 0),
2525 NULL_RTX, 0);
2526 tem = expand_and (tem, op1, NULL_RTX);
2527 adj_operand = force_operand (gen_rtx (adj_code, mode, adj_operand, tem),
2528 adj_operand);
2530 tem = expand_shift (RSHIFT_EXPR, mode, op1,
2531 build_int_2 (GET_MODE_BITSIZE (mode) - 1, 0),
2532 NULL_RTX, 0);
2533 tem = expand_and (tem, op0, NULL_RTX);
2534 target = force_operand (gen_rtx (adj_code, mode, adj_operand, tem), target);
2536 return target;
2539 /* Emit code to multiply OP0 and CNST1, putting the high half of the result
2540 in TARGET if that is convenient, and return where the result is. If the
2541 operation can not be performed, 0 is returned.
2543 MODE is the mode of operation and result.
2545 UNSIGNEDP nonzero means unsigned multiply.
2547 MAX_COST is the total allowed cost for the expanded RTL. */
2550 expand_mult_highpart (mode, op0, cnst1, target, unsignedp, max_cost)
2551 enum machine_mode mode;
2552 register rtx op0, target;
2553 unsigned HOST_WIDE_INT cnst1;
2554 int unsignedp;
2555 int max_cost;
2557 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
2558 optab mul_highpart_optab;
2559 optab moptab;
2560 rtx tem;
2561 int size = GET_MODE_BITSIZE (mode);
2562 rtx op1, wide_op1;
2564 /* We can't support modes wider than HOST_BITS_PER_INT. */
2565 if (size > HOST_BITS_PER_WIDE_INT)
2566 abort ();
2568 op1 = GEN_INT (cnst1);
2570 if (GET_MODE_BITSIZE (wider_mode) <= HOST_BITS_PER_INT)
2571 wide_op1 = op1;
2572 else
2573 wide_op1
2574 = immed_double_const (cnst1,
2575 (unsignedp
2576 ? (HOST_WIDE_INT) 0
2577 : -(cnst1 >> (HOST_BITS_PER_WIDE_INT - 1))),
2578 wider_mode);
2580 /* expand_mult handles constant multiplication of word_mode
2581 or narrower. It does a poor job for large modes. */
2582 if (size < BITS_PER_WORD
2583 && mul_cost[(int) wider_mode] + shift_cost[size-1] < max_cost)
2585 /* We have to do this, since expand_binop doesn't do conversion for
2586 multiply. Maybe change expand_binop to handle widening multiply? */
2587 op0 = convert_to_mode (wider_mode, op0, unsignedp);
2589 tem = expand_mult (wider_mode, op0, wide_op1, NULL_RTX, unsignedp);
2590 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2591 build_int_2 (size, 0), NULL_RTX, 1);
2592 return convert_modes (mode, wider_mode, tem, unsignedp);
2595 if (target == 0)
2596 target = gen_reg_rtx (mode);
2598 /* Firstly, try using a multiplication insn that only generates the needed
2599 high part of the product, and in the sign flavor of unsignedp. */
2600 if (mul_highpart_cost[(int) mode] < max_cost)
2602 mul_highpart_optab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
2603 target = expand_binop (mode, mul_highpart_optab,
2604 op0, wide_op1, target, unsignedp, OPTAB_DIRECT);
2605 if (target)
2606 return target;
2609 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
2610 Need to adjust the result after the multiplication. */
2611 if (mul_highpart_cost[(int) mode] + 2 * shift_cost[size-1] + 4 * add_cost < max_cost)
2613 mul_highpart_optab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
2614 target = expand_binop (mode, mul_highpart_optab,
2615 op0, wide_op1, target, unsignedp, OPTAB_DIRECT);
2616 if (target)
2617 /* We used the wrong signedness. Adjust the result. */
2618 return expand_mult_highpart_adjust (mode, target, op0,
2619 op1, target, unsignedp);
2622 /* Try widening multiplication. */
2623 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
2624 if (moptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
2625 && mul_widen_cost[(int) wider_mode] < max_cost)
2627 op1 = force_reg (mode, op1);
2628 goto try;
2631 /* Try widening the mode and perform a non-widening multiplication. */
2632 moptab = smul_optab;
2633 if (smul_optab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
2634 && mul_cost[(int) wider_mode] + shift_cost[size-1] < max_cost)
2636 op1 = wide_op1;
2637 goto try;
2640 /* Try widening multiplication of opposite signedness, and adjust. */
2641 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
2642 if (moptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
2643 && (mul_widen_cost[(int) wider_mode]
2644 + 2 * shift_cost[size-1] + 4 * add_cost < max_cost))
2646 rtx regop1 = force_reg (mode, op1);
2647 tem = expand_binop (wider_mode, moptab, op0, regop1,
2648 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
2649 if (tem != 0)
2651 /* Extract the high half of the just generated product. */
2652 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2653 build_int_2 (size, 0), NULL_RTX, 1);
2654 tem = convert_modes (mode, wider_mode, tem, unsignedp);
2655 /* We used the wrong signedness. Adjust the result. */
2656 return expand_mult_highpart_adjust (mode, tem, op0, op1,
2657 target, unsignedp);
2661 return 0;
2663 try:
2664 /* Pass NULL_RTX as target since TARGET has wrong mode. */
2665 tem = expand_binop (wider_mode, moptab, op0, op1,
2666 NULL_RTX, unsignedp, OPTAB_WIDEN);
2667 if (tem == 0)
2668 return 0;
2670 /* Extract the high half of the just generated product. */
2671 if (mode == word_mode)
2673 return gen_highpart (mode, tem);
2675 else
2677 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2678 build_int_2 (size, 0), NULL_RTX, 1);
2679 return convert_modes (mode, wider_mode, tem, unsignedp);
2683 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
2684 if that is convenient, and returning where the result is.
2685 You may request either the quotient or the remainder as the result;
2686 specify REM_FLAG nonzero to get the remainder.
2688 CODE is the expression code for which kind of division this is;
2689 it controls how rounding is done. MODE is the machine mode to use.
2690 UNSIGNEDP nonzero means do unsigned division. */
2692 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
2693 and then correct it by or'ing in missing high bits
2694 if result of ANDI is nonzero.
2695 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
2696 This could optimize to a bfexts instruction.
2697 But C doesn't use these operations, so their optimizations are
2698 left for later. */
2700 #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
2703 expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
2704 int rem_flag;
2705 enum tree_code code;
2706 enum machine_mode mode;
2707 register rtx op0, op1, target;
2708 int unsignedp;
2710 enum machine_mode compute_mode;
2711 register rtx tquotient;
2712 rtx quotient = 0, remainder = 0;
2713 rtx last;
2714 int size;
2715 rtx insn, set;
2716 optab optab1, optab2;
2717 int op1_is_constant, op1_is_pow2;
2718 int max_cost, extra_cost;
2720 op1_is_constant = GET_CODE (op1) == CONST_INT;
2721 op1_is_pow2 = (op1_is_constant
2722 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
2723 || EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))));
2726 This is the structure of expand_divmod:
2728 First comes code to fix up the operands so we can perform the operations
2729 correctly and efficiently.
2731 Second comes a switch statement with code specific for each rounding mode.
2732 For some special operands this code emits all RTL for the desired
2733 operation, for other cases, it generates only a quotient and stores it in
2734 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
2735 to indicate that it has not done anything.
2737 Last comes code that finishes the operation. If QUOTIENT is set and
2738 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
2739 QUOTIENT is not set, it is computed using trunc rounding.
2741 We try to generate special code for division and remainder when OP1 is a
2742 constant. If |OP1| = 2**n we can use shifts and some other fast
2743 operations. For other values of OP1, we compute a carefully selected
2744 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
2745 by m.
2747 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
2748 half of the product. Different strategies for generating the product are
2749 implemented in expand_mult_highpart.
2751 If what we actually want is the remainder, we generate that by another
2752 by-constant multiplication and a subtraction. */
2754 /* We shouldn't be called with OP1 == const1_rtx, but some of the
2755 code below will malfunction if we are, so check here and handle
2756 the special case if so. */
2757 if (op1 == const1_rtx)
2758 return rem_flag ? const0_rtx : op0;
2760 if (target
2761 /* Don't use the function value register as a target
2762 since we have to read it as well as write it,
2763 and function-inlining gets confused by this. */
2764 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
2765 /* Don't clobber an operand while doing a multi-step calculation. */
2766 || ((rem_flag || op1_is_constant)
2767 && (reg_mentioned_p (target, op0)
2768 || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
2769 || reg_mentioned_p (target, op1)
2770 || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM)))
2771 target = 0;
2773 /* Get the mode in which to perform this computation. Normally it will
2774 be MODE, but sometimes we can't do the desired operation in MODE.
2775 If so, pick a wider mode in which we can do the operation. Convert
2776 to that mode at the start to avoid repeated conversions.
2778 First see what operations we need. These depend on the expression
2779 we are evaluating. (We assume that divxx3 insns exist under the
2780 same conditions that modxx3 insns and that these insns don't normally
2781 fail. If these assumptions are not correct, we may generate less
2782 efficient code in some cases.)
2784 Then see if we find a mode in which we can open-code that operation
2785 (either a division, modulus, or shift). Finally, check for the smallest
2786 mode for which we can do the operation with a library call. */
2788 /* We might want to refine this now that we have division-by-constant
2789 optimization. Since expand_mult_highpart tries so many variants, it is
2790 not straightforward to generalize this. Maybe we should make an array
2791 of possible modes in init_expmed? Save this for GCC 2.7. */
2793 optab1 = (op1_is_pow2 ? (unsignedp ? lshr_optab : ashr_optab)
2794 : (unsignedp ? udiv_optab : sdiv_optab));
2795 optab2 = (op1_is_pow2 ? optab1 : (unsignedp ? udivmod_optab : sdivmod_optab));
2797 for (compute_mode = mode; compute_mode != VOIDmode;
2798 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
2799 if (optab1->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing
2800 || optab2->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing)
2801 break;
2803 if (compute_mode == VOIDmode)
2804 for (compute_mode = mode; compute_mode != VOIDmode;
2805 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
2806 if (optab1->handlers[(int) compute_mode].libfunc
2807 || optab2->handlers[(int) compute_mode].libfunc)
2808 break;
2810 /* If we still couldn't find a mode, use MODE, but we'll probably abort
2811 in expand_binop. */
2812 if (compute_mode == VOIDmode)
2813 compute_mode = mode;
2815 if (target && GET_MODE (target) == compute_mode)
2816 tquotient = target;
2817 else
2818 tquotient = gen_reg_rtx (compute_mode);
2820 size = GET_MODE_BITSIZE (compute_mode);
2821 #if 0
2822 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
2823 (mode), and thereby get better code when OP1 is a constant. Do that
2824 later. It will require going over all usages of SIZE below. */
2825 size = GET_MODE_BITSIZE (mode);
2826 #endif
2828 max_cost = div_cost[(int) compute_mode]
2829 - (rem_flag ? mul_cost[(int) compute_mode] + add_cost : 0);
2831 /* Now convert to the best mode to use. */
2832 if (compute_mode != mode)
2834 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
2835 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
2837 /* convert_modes may have placed op1 into a register, so we
2838 must recompute the following. */
2839 op1_is_constant = GET_CODE (op1) == CONST_INT;
2840 op1_is_pow2 = (op1_is_constant
2841 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
2842 || (! unsignedp
2843 && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
2846 /* If one of the operands is a volatile MEM, copy it into a register. */
2848 if (GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0))
2849 op0 = force_reg (compute_mode, op0);
2850 if (GET_CODE (op1) == MEM && MEM_VOLATILE_P (op1))
2851 op1 = force_reg (compute_mode, op1);
2853 /* If we need the remainder or if OP1 is constant, we need to
2854 put OP0 in a register in case it has any queued subexpressions. */
2855 if (rem_flag || op1_is_constant)
2856 op0 = force_reg (compute_mode, op0);
2858 last = get_last_insn ();
2860 /* Promote floor rounding to trunc rounding for unsigned operations. */
2861 if (unsignedp)
2863 if (code == FLOOR_DIV_EXPR)
2864 code = TRUNC_DIV_EXPR;
2865 if (code == FLOOR_MOD_EXPR)
2866 code = TRUNC_MOD_EXPR;
2869 if (op1 != const0_rtx)
2870 switch (code)
2872 case TRUNC_MOD_EXPR:
2873 case TRUNC_DIV_EXPR:
2874 if (op1_is_constant)
2876 if (unsignedp)
2878 unsigned HOST_WIDE_INT mh, ml;
2879 int pre_shift, post_shift;
2880 int dummy;
2881 unsigned HOST_WIDE_INT d = INTVAL (op1);
2883 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
2885 pre_shift = floor_log2 (d);
2886 if (rem_flag)
2888 remainder
2889 = expand_binop (compute_mode, and_optab, op0,
2890 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
2891 remainder, 1,
2892 OPTAB_LIB_WIDEN);
2893 if (remainder)
2894 return gen_lowpart (mode, remainder);
2896 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
2897 build_int_2 (pre_shift, 0),
2898 tquotient, 1);
2900 else if (size <= HOST_BITS_PER_WIDE_INT)
2902 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
2904 /* Most significant bit of divisor is set; emit an scc
2905 insn. */
2906 quotient = emit_store_flag (tquotient, GEU, op0, op1,
2907 compute_mode, 1, 1);
2908 if (quotient == 0)
2909 goto fail1;
2911 else
2913 /* Find a suitable multiplier and right shift count
2914 instead of multiplying with D. */
2916 mh = choose_multiplier (d, size, size,
2917 &ml, &post_shift, &dummy);
2919 /* If the suggested multiplier is more than SIZE bits,
2920 we can do better for even divisors, using an
2921 initial right shift. */
2922 if (mh != 0 && (d & 1) == 0)
2924 pre_shift = floor_log2 (d & -d);
2925 mh = choose_multiplier (d >> pre_shift, size,
2926 size - pre_shift,
2927 &ml, &post_shift, &dummy);
2928 if (mh)
2929 abort ();
2931 else
2932 pre_shift = 0;
2934 if (mh != 0)
2936 rtx t1, t2, t3, t4;
2938 extra_cost = (shift_cost[post_shift - 1]
2939 + shift_cost[1] + 2 * add_cost);
2940 t1 = expand_mult_highpart (compute_mode, op0, ml,
2941 NULL_RTX, 1,
2942 max_cost - extra_cost);
2943 if (t1 == 0)
2944 goto fail1;
2945 t2 = force_operand (gen_rtx (MINUS, compute_mode,
2946 op0, t1),
2947 NULL_RTX);
2948 t3 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
2949 build_int_2 (1, 0), NULL_RTX,1);
2950 t4 = force_operand (gen_rtx (PLUS, compute_mode,
2951 t1, t3),
2952 NULL_RTX);
2953 quotient
2954 = expand_shift (RSHIFT_EXPR, compute_mode, t4,
2955 build_int_2 (post_shift - 1, 0),
2956 tquotient, 1);
2958 else
2960 rtx t1, t2;
2962 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
2963 build_int_2 (pre_shift, 0),
2964 NULL_RTX, 1);
2965 extra_cost = (shift_cost[pre_shift]
2966 + shift_cost[post_shift]);
2967 t2 = expand_mult_highpart (compute_mode, t1, ml,
2968 NULL_RTX, 1,
2969 max_cost - extra_cost);
2970 if (t2 == 0)
2971 goto fail1;
2972 quotient
2973 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
2974 build_int_2 (post_shift, 0),
2975 tquotient, 1);
2979 else /* Too wide mode to use tricky code */
2980 break;
2982 insn = get_last_insn ();
2983 if (insn != last
2984 && (set = single_set (insn)) != 0
2985 && SET_DEST (set) == quotient)
2986 REG_NOTES (insn)
2987 = gen_rtx (EXPR_LIST, REG_EQUAL,
2988 gen_rtx (UDIV, compute_mode, op0, op1),
2989 REG_NOTES (insn));
2991 else /* TRUNC_DIV, signed */
2993 unsigned HOST_WIDE_INT ml;
2994 int lgup, post_shift;
2995 HOST_WIDE_INT d = INTVAL (op1);
2996 unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
2998 /* n rem d = n rem -d */
2999 if (rem_flag && d < 0)
3001 d = abs_d;
3002 op1 = GEN_INT (abs_d);
3005 if (d == 1)
3006 quotient = op0;
3007 else if (d == -1)
3008 quotient = expand_unop (compute_mode, neg_optab, op0,
3009 tquotient, 0);
3010 else if (abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
3012 /* This case is not handled correctly below. */
3013 quotient = emit_store_flag (tquotient, EQ, op0, op1,
3014 compute_mode, 1, 1);
3015 if (quotient == 0)
3016 goto fail1;
3018 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
3019 && (rem_flag ? smod_pow2_cheap : sdiv_pow2_cheap))
3021 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
3023 lgup = floor_log2 (abs_d);
3024 if (abs_d != 2 && BRANCH_COST < 3)
3026 rtx label = gen_label_rtx ();
3027 rtx t1;
3029 t1 = copy_to_mode_reg (compute_mode, op0);
3030 emit_cmp_insn (t1, const0_rtx, GE,
3031 NULL_RTX, compute_mode, 0, 0);
3032 emit_jump_insn (gen_bge (label));
3033 expand_inc (t1, GEN_INT (abs_d - 1));
3034 emit_label (label);
3035 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3036 build_int_2 (lgup, 0),
3037 tquotient, 0);
3039 else
3041 rtx t1, t2, t3;
3042 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3043 build_int_2 (size - 1, 0),
3044 NULL_RTX, 0);
3045 t2 = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3046 build_int_2 (size - lgup, 0),
3047 NULL_RTX, 1);
3048 t3 = force_operand (gen_rtx (PLUS, compute_mode,
3049 op0, t2),
3050 NULL_RTX);
3051 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t3,
3052 build_int_2 (lgup, 0),
3053 tquotient, 0);
3056 /* We have computed OP0 / abs(OP1). If OP1 is negative, negate
3057 the quotient. */
3058 if (d < 0)
3060 insn = get_last_insn ();
3061 if (insn != last
3062 && (set = single_set (insn)) != 0
3063 && SET_DEST (set) == quotient)
3064 REG_NOTES (insn)
3065 = gen_rtx (EXPR_LIST, REG_EQUAL,
3066 gen_rtx (DIV, compute_mode, op0,
3067 GEN_INT (abs_d)),
3068 REG_NOTES (insn));
3070 quotient = expand_unop (compute_mode, neg_optab,
3071 quotient, quotient, 0);
3074 else if (size <= HOST_BITS_PER_WIDE_INT)
3076 choose_multiplier (abs_d, size, size - 1,
3077 &ml, &post_shift, &lgup);
3078 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
3080 rtx t1, t2, t3;
3082 extra_cost = (shift_cost[post_shift]
3083 + shift_cost[size - 1] + add_cost);
3084 t1 = expand_mult_highpart (compute_mode, op0, ml,
3085 NULL_RTX, 0,
3086 max_cost - extra_cost);
3087 if (t1 == 0)
3088 goto fail1;
3089 t2 = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3090 build_int_2 (post_shift, 0), NULL_RTX, 0);
3091 t3 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3092 build_int_2 (size - 1, 0), NULL_RTX, 0);
3093 if (d < 0)
3094 quotient = force_operand (gen_rtx (MINUS, compute_mode, t3, t2),
3095 tquotient);
3096 else
3097 quotient = force_operand (gen_rtx (MINUS, compute_mode, t2, t3),
3098 tquotient);
3100 else
3102 rtx t1, t2, t3, t4;
3104 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
3105 extra_cost = (shift_cost[post_shift]
3106 + shift_cost[size - 1] + 2 * add_cost);
3107 t1 = expand_mult_highpart (compute_mode, op0, ml,
3108 NULL_RTX, 0,
3109 max_cost - extra_cost);
3110 if (t1 == 0)
3111 goto fail1;
3112 t2 = force_operand (gen_rtx (PLUS, compute_mode, t1, op0),
3113 NULL_RTX);
3114 t3 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3115 build_int_2 (post_shift, 0), NULL_RTX, 0);
3116 t4 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3117 build_int_2 (size - 1, 0), NULL_RTX, 0);
3118 if (d < 0)
3119 quotient = force_operand (gen_rtx (MINUS, compute_mode, t4, t3),
3120 tquotient);
3121 else
3122 quotient = force_operand (gen_rtx (MINUS, compute_mode, t3, t4),
3123 tquotient);
3126 else /* Too wide mode to use tricky code */
3127 break;
3129 insn = get_last_insn ();
3130 if (insn != last
3131 && (set = single_set (insn)) != 0
3132 && SET_DEST (set) == quotient)
3133 REG_NOTES (insn)
3134 = gen_rtx (EXPR_LIST, REG_EQUAL,
3135 gen_rtx (DIV, compute_mode, op0, op1),
3136 REG_NOTES (insn));
3138 break;
3140 fail1:
3141 delete_insns_since (last);
3142 break;
3144 case FLOOR_DIV_EXPR:
3145 case FLOOR_MOD_EXPR:
3146 /* We will come here only for signed operations. */
3147 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
3149 unsigned HOST_WIDE_INT mh, ml;
3150 int pre_shift, lgup, post_shift;
3151 HOST_WIDE_INT d = INTVAL (op1);
3153 if (d > 0)
3155 /* We could just as easily deal with negative constants here,
3156 but it does not seem worth the trouble for GCC 2.6. */
3157 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3159 pre_shift = floor_log2 (d);
3160 if (rem_flag)
3162 remainder = expand_binop (compute_mode, and_optab, op0,
3163 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3164 remainder, 0, OPTAB_LIB_WIDEN);
3165 if (remainder)
3166 return gen_lowpart (mode, remainder);
3168 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3169 build_int_2 (pre_shift, 0),
3170 tquotient, 0);
3172 else
3174 rtx t1, t2, t3, t4;
3176 mh = choose_multiplier (d, size, size - 1,
3177 &ml, &post_shift, &lgup);
3178 if (mh)
3179 abort ();
3181 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3182 build_int_2 (size - 1, 0), NULL_RTX, 0);
3183 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
3184 NULL_RTX, 0, OPTAB_WIDEN);
3185 extra_cost = (shift_cost[post_shift]
3186 + shift_cost[size - 1] + 2 * add_cost);
3187 t3 = expand_mult_highpart (compute_mode, t2, ml,
3188 NULL_RTX, 1,
3189 max_cost - extra_cost);
3190 if (t3 != 0)
3192 t4 = expand_shift (RSHIFT_EXPR, compute_mode, t3,
3193 build_int_2 (post_shift, 0),
3194 NULL_RTX, 1);
3195 quotient = expand_binop (compute_mode, xor_optab,
3196 t4, t1, tquotient, 0,
3197 OPTAB_WIDEN);
3201 else
3203 rtx nsign, t1, t2, t3, t4;
3204 t1 = force_operand (gen_rtx (PLUS, compute_mode,
3205 op0, constm1_rtx), NULL_RTX);
3206 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
3207 0, OPTAB_WIDEN);
3208 nsign = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3209 build_int_2 (size - 1, 0), NULL_RTX, 0);
3210 t3 = force_operand (gen_rtx (MINUS, compute_mode, t1, nsign),
3211 NULL_RTX);
3212 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
3213 NULL_RTX, 0);
3214 if (t4)
3216 rtx t5;
3217 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
3218 NULL_RTX, 0);
3219 quotient = force_operand (gen_rtx (PLUS, compute_mode,
3220 t4, t5),
3221 tquotient);
3226 if (quotient != 0)
3227 break;
3228 delete_insns_since (last);
3230 /* Try using an instruction that produces both the quotient and
3231 remainder, using truncation. We can easily compensate the quotient
3232 or remainder to get floor rounding, once we have the remainder.
3233 Notice that we compute also the final remainder value here,
3234 and return the result right away. */
3235 if (target == 0 || GET_MODE (target) != compute_mode)
3236 target = gen_reg_rtx (compute_mode);
3238 if (rem_flag)
3240 remainder
3241 = GET_CODE (target) == REG ? target : gen_reg_rtx (compute_mode);
3242 quotient = gen_reg_rtx (compute_mode);
3244 else
3246 quotient
3247 = GET_CODE (target) == REG ? target : gen_reg_rtx (compute_mode);
3248 remainder = gen_reg_rtx (compute_mode);
3251 if (expand_twoval_binop (sdivmod_optab, op0, op1,
3252 quotient, remainder, 0))
3254 /* This could be computed with a branch-less sequence.
3255 Save that for later. */
3256 rtx tem;
3257 rtx label = gen_label_rtx ();
3258 emit_cmp_insn (remainder, const0_rtx, EQ, NULL_RTX,
3259 compute_mode, 0, 0);
3260 emit_jump_insn (gen_beq (label));
3261 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3262 NULL_RTX, 0, OPTAB_WIDEN);
3263 emit_cmp_insn (tem, const0_rtx, GE, NULL_RTX, compute_mode, 0, 0);
3264 emit_jump_insn (gen_bge (label));
3265 expand_dec (quotient, const1_rtx);
3266 expand_inc (remainder, op1);
3267 emit_label (label);
3268 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3271 /* No luck with division elimination or divmod. Have to do it
3272 by conditionally adjusting op0 *and* the result. */
3274 rtx label1, label2, label3, label4, label5;
3275 rtx adjusted_op0;
3276 rtx tem;
3278 quotient = gen_reg_rtx (compute_mode);
3279 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3280 label1 = gen_label_rtx ();
3281 label2 = gen_label_rtx ();
3282 label3 = gen_label_rtx ();
3283 label4 = gen_label_rtx ();
3284 label5 = gen_label_rtx ();
3285 emit_cmp_insn (op1, const0_rtx, LT, NULL_RTX, compute_mode, 0, 0);
3286 emit_jump_insn (gen_blt (label2));
3287 emit_cmp_insn (adjusted_op0, const0_rtx, LT, NULL_RTX,
3288 compute_mode, 0, 0);
3289 emit_jump_insn (gen_blt (label1));
3290 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3291 quotient, 0, OPTAB_LIB_WIDEN);
3292 if (tem != quotient)
3293 emit_move_insn (quotient, tem);
3294 emit_jump_insn (gen_jump (label5));
3295 emit_barrier ();
3296 emit_label (label1);
3297 expand_inc (adjusted_op0, const1_rtx);
3298 emit_jump_insn (gen_jump (label4));
3299 emit_barrier ();
3300 emit_label (label2);
3301 emit_cmp_insn (adjusted_op0, const0_rtx, GT, NULL_RTX,
3302 compute_mode, 0, 0);
3303 emit_jump_insn (gen_bgt (label3));
3304 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3305 quotient, 0, OPTAB_LIB_WIDEN);
3306 if (tem != quotient)
3307 emit_move_insn (quotient, tem);
3308 emit_jump_insn (gen_jump (label5));
3309 emit_barrier ();
3310 emit_label (label3);
3311 expand_dec (adjusted_op0, const1_rtx);
3312 emit_label (label4);
3313 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3314 quotient, 0, OPTAB_LIB_WIDEN);
3315 if (tem != quotient)
3316 emit_move_insn (quotient, tem);
3317 expand_dec (quotient, const1_rtx);
3318 emit_label (label5);
3320 break;
3322 case CEIL_DIV_EXPR:
3323 case CEIL_MOD_EXPR:
3324 if (unsignedp)
3326 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
3328 rtx t1, t2, t3;
3329 unsigned HOST_WIDE_INT d = INTVAL (op1);
3330 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3331 build_int_2 (floor_log2 (d), 0),
3332 tquotient, 1);
3333 t2 = expand_binop (compute_mode, and_optab, op0,
3334 GEN_INT (d - 1),
3335 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3336 t3 = gen_reg_rtx (compute_mode);
3337 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
3338 compute_mode, 1, 1);
3339 if (t3 == 0)
3341 rtx lab;
3342 lab = gen_label_rtx ();
3343 emit_cmp_insn (t2, const0_rtx, EQ, NULL_RTX,
3344 compute_mode, 0, 0);
3345 emit_jump_insn (gen_beq (lab));
3346 expand_inc (t1, const1_rtx);
3347 emit_label (lab);
3348 quotient = t1;
3350 else
3351 quotient = force_operand (gen_rtx (PLUS, compute_mode,
3352 t1, t3),
3353 tquotient);
3354 break;
3357 /* Try using an instruction that produces both the quotient and
3358 remainder, using truncation. We can easily compensate the
3359 quotient or remainder to get ceiling rounding, once we have the
3360 remainder. Notice that we compute also the final remainder
3361 value here, and return the result right away. */
3362 if (target == 0 || GET_MODE (target) != compute_mode)
3363 target = gen_reg_rtx (compute_mode);
3365 if (rem_flag)
3367 remainder = (GET_CODE (target) == REG
3368 ? target : gen_reg_rtx (compute_mode));
3369 quotient = gen_reg_rtx (compute_mode);
3371 else
3373 quotient = (GET_CODE (target) == REG
3374 ? target : gen_reg_rtx (compute_mode));
3375 remainder = gen_reg_rtx (compute_mode);
3378 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
3379 remainder, 1))
3381 /* This could be computed with a branch-less sequence.
3382 Save that for later. */
3383 rtx label = gen_label_rtx ();
3384 emit_cmp_insn (remainder, const0_rtx, EQ, NULL_RTX,
3385 compute_mode, 0, 0);
3386 emit_jump_insn (gen_beq (label));
3387 expand_inc (quotient, const1_rtx);
3388 expand_dec (remainder, op1);
3389 emit_label (label);
3390 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3393 /* No luck with division elimination or divmod. Have to do it
3394 by conditionally adjusting op0 *and* the result. */
3396 rtx label1, label2;
3397 rtx adjusted_op0, tem;
3399 quotient = gen_reg_rtx (compute_mode);
3400 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3401 label1 = gen_label_rtx ();
3402 label2 = gen_label_rtx ();
3403 emit_cmp_insn (adjusted_op0, const0_rtx, NE, NULL_RTX,
3404 compute_mode, 0, 0);
3405 emit_jump_insn (gen_bne (label1));
3406 emit_move_insn (quotient, const0_rtx);
3407 emit_jump_insn (gen_jump (label2));
3408 emit_barrier ();
3409 emit_label (label1);
3410 expand_dec (adjusted_op0, const1_rtx);
3411 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
3412 quotient, 1, OPTAB_LIB_WIDEN);
3413 if (tem != quotient)
3414 emit_move_insn (quotient, tem);
3415 expand_inc (quotient, const1_rtx);
3416 emit_label (label2);
3419 else /* signed */
3421 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3422 && INTVAL (op1) >= 0)
3424 /* This is extremely similar to the code for the unsigned case
3425 above. For 2.7 we should merge these variants, but for
3426 2.6.1 I don't want to touch the code for unsigned since that
3427 get used in C. The signed case will only be used by other
3428 languages (Ada). */
3430 rtx t1, t2, t3;
3431 unsigned HOST_WIDE_INT d = INTVAL (op1);
3432 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3433 build_int_2 (floor_log2 (d), 0),
3434 tquotient, 0);
3435 t2 = expand_binop (compute_mode, and_optab, op0,
3436 GEN_INT (d - 1),
3437 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3438 t3 = gen_reg_rtx (compute_mode);
3439 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
3440 compute_mode, 1, 1);
3441 if (t3 == 0)
3443 rtx lab;
3444 lab = gen_label_rtx ();
3445 emit_cmp_insn (t2, const0_rtx, EQ, NULL_RTX,
3446 compute_mode, 0, 0);
3447 emit_jump_insn (gen_beq (lab));
3448 expand_inc (t1, const1_rtx);
3449 emit_label (lab);
3450 quotient = t1;
3452 else
3453 quotient = force_operand (gen_rtx (PLUS, compute_mode,
3454 t1, t3),
3455 tquotient);
3456 break;
3459 /* Try using an instruction that produces both the quotient and
3460 remainder, using truncation. We can easily compensate the
3461 quotient or remainder to get ceiling rounding, once we have the
3462 remainder. Notice that we compute also the final remainder
3463 value here, and return the result right away. */
3464 if (target == 0 || GET_MODE (target) != compute_mode)
3465 target = gen_reg_rtx (compute_mode);
3466 if (rem_flag)
3468 remainder= (GET_CODE (target) == REG
3469 ? target : gen_reg_rtx (compute_mode));
3470 quotient = gen_reg_rtx (compute_mode);
3472 else
3474 quotient = (GET_CODE (target) == REG
3475 ? target : gen_reg_rtx (compute_mode));
3476 remainder = gen_reg_rtx (compute_mode);
3479 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
3480 remainder, 0))
3482 /* This could be computed with a branch-less sequence.
3483 Save that for later. */
3484 rtx tem;
3485 rtx label = gen_label_rtx ();
3486 emit_cmp_insn (remainder, const0_rtx, EQ, NULL_RTX,
3487 compute_mode, 0, 0);
3488 emit_jump_insn (gen_beq (label));
3489 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3490 NULL_RTX, 0, OPTAB_WIDEN);
3491 emit_cmp_insn (tem, const0_rtx, LT, NULL_RTX,
3492 compute_mode, 0, 0);
3493 emit_jump_insn (gen_blt (label));
3494 expand_inc (quotient, const1_rtx);
3495 expand_dec (remainder, op1);
3496 emit_label (label);
3497 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3500 /* No luck with division elimination or divmod. Have to do it
3501 by conditionally adjusting op0 *and* the result. */
3503 rtx label1, label2, label3, label4, label5;
3504 rtx adjusted_op0;
3505 rtx tem;
3507 quotient = gen_reg_rtx (compute_mode);
3508 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3509 label1 = gen_label_rtx ();
3510 label2 = gen_label_rtx ();
3511 label3 = gen_label_rtx ();
3512 label4 = gen_label_rtx ();
3513 label5 = gen_label_rtx ();
3514 emit_cmp_insn (op1, const0_rtx, LT, NULL_RTX,
3515 compute_mode, 0, 0);
3516 emit_jump_insn (gen_blt (label2));
3517 emit_cmp_insn (adjusted_op0, const0_rtx, GT, NULL_RTX,
3518 compute_mode, 0, 0);
3519 emit_jump_insn (gen_bgt (label1));
3520 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3521 quotient, 0, OPTAB_LIB_WIDEN);
3522 if (tem != quotient)
3523 emit_move_insn (quotient, tem);
3524 emit_jump_insn (gen_jump (label5));
3525 emit_barrier ();
3526 emit_label (label1);
3527 expand_dec (adjusted_op0, const1_rtx);
3528 emit_jump_insn (gen_jump (label4));
3529 emit_barrier ();
3530 emit_label (label2);
3531 emit_cmp_insn (adjusted_op0, const0_rtx, LT, NULL_RTX,
3532 compute_mode, 0, 0);
3533 emit_jump_insn (gen_blt (label3));
3534 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3535 quotient, 0, OPTAB_LIB_WIDEN);
3536 if (tem != quotient)
3537 emit_move_insn (quotient, tem);
3538 emit_jump_insn (gen_jump (label5));
3539 emit_barrier ();
3540 emit_label (label3);
3541 expand_inc (adjusted_op0, const1_rtx);
3542 emit_label (label4);
3543 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3544 quotient, 0, OPTAB_LIB_WIDEN);
3545 if (tem != quotient)
3546 emit_move_insn (quotient, tem);
3547 expand_inc (quotient, const1_rtx);
3548 emit_label (label5);
3551 break;
3553 case EXACT_DIV_EXPR:
3554 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
3556 HOST_WIDE_INT d = INTVAL (op1);
3557 unsigned HOST_WIDE_INT ml;
3558 int post_shift;
3559 rtx t1;
3561 post_shift = floor_log2 (d & -d);
3562 ml = invert_mod2n (d >> post_shift, size);
3563 t1 = expand_mult (compute_mode, op0, GEN_INT (ml), NULL_RTX,
3564 unsignedp);
3565 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3566 build_int_2 (post_shift, 0),
3567 NULL_RTX, unsignedp);
3569 insn = get_last_insn ();
3570 REG_NOTES (insn)
3571 = gen_rtx (EXPR_LIST, REG_EQUAL,
3572 gen_rtx (unsignedp ? UDIV : DIV, compute_mode,
3573 op0, op1),
3574 REG_NOTES (insn));
3576 break;
3578 case ROUND_DIV_EXPR:
3579 case ROUND_MOD_EXPR:
3580 if (unsignedp)
3582 rtx tem;
3583 rtx label;
3584 label = gen_label_rtx ();
3585 quotient = gen_reg_rtx (compute_mode);
3586 remainder = gen_reg_rtx (compute_mode);
3587 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
3589 rtx tem;
3590 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
3591 quotient, 1, OPTAB_LIB_WIDEN);
3592 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
3593 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
3594 remainder, 1, OPTAB_LIB_WIDEN);
3596 tem = plus_constant (op1, -1);
3597 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
3598 build_int_2 (1, 0), NULL_RTX, 1);
3599 emit_cmp_insn (remainder, tem, LEU, NULL_RTX, compute_mode, 0, 0);
3600 emit_jump_insn (gen_bleu (label));
3601 expand_inc (quotient, const1_rtx);
3602 expand_dec (remainder, op1);
3603 emit_label (label);
3605 else
3607 rtx abs_rem, abs_op1, tem, mask;
3608 rtx label;
3609 label = gen_label_rtx ();
3610 quotient = gen_reg_rtx (compute_mode);
3611 remainder = gen_reg_rtx (compute_mode);
3612 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
3614 rtx tem;
3615 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
3616 quotient, 0, OPTAB_LIB_WIDEN);
3617 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
3618 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
3619 remainder, 0, OPTAB_LIB_WIDEN);
3621 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 0, 0);
3622 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 0, 0);
3623 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
3624 build_int_2 (1, 0), NULL_RTX, 1);
3625 emit_cmp_insn (tem, abs_op1, LTU, NULL_RTX, compute_mode, 0, 0);
3626 emit_jump_insn (gen_bltu (label));
3627 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3628 NULL_RTX, 0, OPTAB_WIDEN);
3629 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
3630 build_int_2 (size - 1, 0), NULL_RTX, 0);
3631 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
3632 NULL_RTX, 0, OPTAB_WIDEN);
3633 tem = expand_binop (compute_mode, sub_optab, tem, mask,
3634 NULL_RTX, 0, OPTAB_WIDEN);
3635 expand_inc (quotient, tem);
3636 tem = expand_binop (compute_mode, xor_optab, mask, op1,
3637 NULL_RTX, 0, OPTAB_WIDEN);
3638 tem = expand_binop (compute_mode, sub_optab, tem, mask,
3639 NULL_RTX, 0, OPTAB_WIDEN);
3640 expand_dec (remainder, tem);
3641 emit_label (label);
3643 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3645 default:
3646 abort ();
3649 if (quotient == 0)
3651 if (target && GET_MODE (target) != compute_mode)
3652 target = 0;
3654 if (rem_flag)
3656 /* Try to produce the remainder directly without a library call. */
3657 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
3658 op0, op1, target,
3659 unsignedp, OPTAB_WIDEN);
3660 if (remainder == 0)
3662 /* No luck there. Can we do remainder and divide at once
3663 without a library call? */
3664 remainder = gen_reg_rtx (compute_mode);
3665 if (! expand_twoval_binop ((unsignedp
3666 ? udivmod_optab
3667 : sdivmod_optab),
3668 op0, op1,
3669 NULL_RTX, remainder, unsignedp))
3670 remainder = 0;
3673 if (remainder)
3674 return gen_lowpart (mode, remainder);
3677 /* Produce the quotient. Try a quotient insn, but not a library call.
3678 If we have a divmod in this mode, use it in preference to widening
3679 the div (for this test we assume it will not fail). Note that optab2
3680 is set to the one of the two optabs that the call below will use. */
3681 quotient
3682 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
3683 op0, op1, rem_flag ? NULL_RTX : target,
3684 unsignedp,
3685 ((optab2->handlers[(int) compute_mode].insn_code
3686 != CODE_FOR_nothing)
3687 ? OPTAB_DIRECT : OPTAB_WIDEN));
3689 if (quotient == 0)
3691 /* No luck there. Try a quotient-and-remainder insn,
3692 keeping the quotient alone. */
3693 quotient = gen_reg_rtx (compute_mode);
3694 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
3695 op0, op1,
3696 quotient, NULL_RTX, unsignedp))
3698 quotient = 0;
3699 if (! rem_flag)
3700 /* Still no luck. If we are not computing the remainder,
3701 use a library call for the quotient. */
3702 quotient = sign_expand_binop (compute_mode,
3703 udiv_optab, sdiv_optab,
3704 op0, op1, target,
3705 unsignedp, OPTAB_LIB_WIDEN);
3710 if (rem_flag)
3712 if (target && GET_MODE (target) != compute_mode)
3713 target = 0;
3715 if (quotient == 0)
3716 /* No divide instruction either. Use library for remainder. */
3717 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
3718 op0, op1, target,
3719 unsignedp, OPTAB_LIB_WIDEN);
3720 else
3722 /* We divided. Now finish doing X - Y * (X / Y). */
3723 remainder = expand_mult (compute_mode, quotient, op1,
3724 NULL_RTX, unsignedp);
3725 remainder = expand_binop (compute_mode, sub_optab, op0,
3726 remainder, target, unsignedp,
3727 OPTAB_LIB_WIDEN);
3731 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3734 /* Return a tree node with data type TYPE, describing the value of X.
3735 Usually this is an RTL_EXPR, if there is no obvious better choice.
3736 X may be an expression, however we only support those expressions
3737 generated by loop.c. */
3739 tree
3740 make_tree (type, x)
3741 tree type;
3742 rtx x;
3744 tree t;
3746 switch (GET_CODE (x))
3748 case CONST_INT:
3749 t = build_int_2 (INTVAL (x),
3750 TREE_UNSIGNED (type) || INTVAL (x) >= 0 ? 0 : -1);
3751 TREE_TYPE (t) = type;
3752 return t;
3754 case CONST_DOUBLE:
3755 if (GET_MODE (x) == VOIDmode)
3757 t = build_int_2 (CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
3758 TREE_TYPE (t) = type;
3760 else
3762 REAL_VALUE_TYPE d;
3764 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
3765 t = build_real (type, d);
3768 return t;
3770 case PLUS:
3771 return fold (build (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
3772 make_tree (type, XEXP (x, 1))));
3774 case MINUS:
3775 return fold (build (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
3776 make_tree (type, XEXP (x, 1))));
3778 case NEG:
3779 return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
3781 case MULT:
3782 return fold (build (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
3783 make_tree (type, XEXP (x, 1))));
3785 case ASHIFT:
3786 return fold (build (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
3787 make_tree (type, XEXP (x, 1))));
3789 case LSHIFTRT:
3790 return fold (convert (type,
3791 build (RSHIFT_EXPR, unsigned_type (type),
3792 make_tree (unsigned_type (type),
3793 XEXP (x, 0)),
3794 make_tree (type, XEXP (x, 1)))));
3796 case ASHIFTRT:
3797 return fold (convert (type,
3798 build (RSHIFT_EXPR, signed_type (type),
3799 make_tree (signed_type (type), XEXP (x, 0)),
3800 make_tree (type, XEXP (x, 1)))));
3802 case DIV:
3803 if (TREE_CODE (type) != REAL_TYPE)
3804 t = signed_type (type);
3805 else
3806 t = type;
3808 return fold (convert (type,
3809 build (TRUNC_DIV_EXPR, t,
3810 make_tree (t, XEXP (x, 0)),
3811 make_tree (t, XEXP (x, 1)))));
3812 case UDIV:
3813 t = unsigned_type (type);
3814 return fold (convert (type,
3815 build (TRUNC_DIV_EXPR, t,
3816 make_tree (t, XEXP (x, 0)),
3817 make_tree (t, XEXP (x, 1)))));
3818 default:
3819 t = make_node (RTL_EXPR);
3820 TREE_TYPE (t) = type;
3821 RTL_EXPR_RTL (t) = x;
3822 /* There are no insns to be output
3823 when this rtl_expr is used. */
3824 RTL_EXPR_SEQUENCE (t) = 0;
3825 return t;
3829 /* Return an rtx representing the value of X * MULT + ADD.
3830 TARGET is a suggestion for where to store the result (an rtx).
3831 MODE is the machine mode for the computation.
3832 X and MULT must have mode MODE. ADD may have a different mode.
3833 So can X (defaults to same as MODE).
3834 UNSIGNEDP is non-zero to do unsigned multiplication.
3835 This may emit insns. */
3838 expand_mult_add (x, target, mult, add, mode, unsignedp)
3839 rtx x, target, mult, add;
3840 enum machine_mode mode;
3841 int unsignedp;
3843 tree type = type_for_mode (mode, unsignedp);
3844 tree add_type = (GET_MODE (add) == VOIDmode
3845 ? type : type_for_mode (GET_MODE (add), unsignedp));
3846 tree result = fold (build (PLUS_EXPR, type,
3847 fold (build (MULT_EXPR, type,
3848 make_tree (type, x),
3849 make_tree (type, mult))),
3850 make_tree (add_type, add)));
3852 return expand_expr (result, target, VOIDmode, 0);
3855 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
3856 and returning TARGET.
3858 If TARGET is 0, a pseudo-register or constant is returned. */
3861 expand_and (op0, op1, target)
3862 rtx op0, op1, target;
3864 enum machine_mode mode = VOIDmode;
3865 rtx tem;
3867 if (GET_MODE (op0) != VOIDmode)
3868 mode = GET_MODE (op0);
3869 else if (GET_MODE (op1) != VOIDmode)
3870 mode = GET_MODE (op1);
3872 if (mode != VOIDmode)
3873 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
3874 else if (GET_CODE (op0) == CONST_INT && GET_CODE (op1) == CONST_INT)
3875 tem = GEN_INT (INTVAL (op0) & INTVAL (op1));
3876 else
3877 abort ();
3879 if (target == 0)
3880 target = tem;
3881 else if (tem != target)
3882 emit_move_insn (target, tem);
3883 return target;
3886 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
3887 and storing in TARGET. Normally return TARGET.
3888 Return 0 if that cannot be done.
3890 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
3891 it is VOIDmode, they cannot both be CONST_INT.
3893 UNSIGNEDP is for the case where we have to widen the operands
3894 to perform the operation. It says to use zero-extension.
3896 NORMALIZEP is 1 if we should convert the result to be either zero
3897 or one. Normalize is -1 if we should convert the result to be
3898 either zero or -1. If NORMALIZEP is zero, the result will be left
3899 "raw" out of the scc insn. */
3902 emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep)
3903 rtx target;
3904 enum rtx_code code;
3905 rtx op0, op1;
3906 enum machine_mode mode;
3907 int unsignedp;
3908 int normalizep;
3910 rtx subtarget;
3911 enum insn_code icode;
3912 enum machine_mode compare_mode;
3913 enum machine_mode target_mode = GET_MODE (target);
3914 rtx tem;
3915 rtx last = get_last_insn ();
3916 rtx pattern, comparison;
3918 /* If one operand is constant, make it the second one. Only do this
3919 if the other operand is not constant as well. */
3921 if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
3922 || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
3924 tem = op0;
3925 op0 = op1;
3926 op1 = tem;
3927 code = swap_condition (code);
3930 if (mode == VOIDmode)
3931 mode = GET_MODE (op0);
3933 /* For some comparisons with 1 and -1, we can convert this to
3934 comparisons with zero. This will often produce more opportunities for
3935 store-flag insns. */
3937 switch (code)
3939 case LT:
3940 if (op1 == const1_rtx)
3941 op1 = const0_rtx, code = LE;
3942 break;
3943 case LE:
3944 if (op1 == constm1_rtx)
3945 op1 = const0_rtx, code = LT;
3946 break;
3947 case GE:
3948 if (op1 == const1_rtx)
3949 op1 = const0_rtx, code = GT;
3950 break;
3951 case GT:
3952 if (op1 == constm1_rtx)
3953 op1 = const0_rtx, code = GE;
3954 break;
3955 case GEU:
3956 if (op1 == const1_rtx)
3957 op1 = const0_rtx, code = NE;
3958 break;
3959 case LTU:
3960 if (op1 == const1_rtx)
3961 op1 = const0_rtx, code = EQ;
3962 break;
3963 default:
3964 break;
3967 /* From now on, we won't change CODE, so set ICODE now. */
3968 icode = setcc_gen_code[(int) code];
3970 /* If this is A < 0 or A >= 0, we can do this by taking the ones
3971 complement of A (for GE) and shifting the sign bit to the low bit. */
3972 if (op1 == const0_rtx && (code == LT || code == GE)
3973 && GET_MODE_CLASS (mode) == MODE_INT
3974 && (normalizep || STORE_FLAG_VALUE == 1
3975 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3976 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
3977 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
3979 subtarget = target;
3981 /* If the result is to be wider than OP0, it is best to convert it
3982 first. If it is to be narrower, it is *incorrect* to convert it
3983 first. */
3984 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
3986 op0 = protect_from_queue (op0, 0);
3987 op0 = convert_modes (target_mode, mode, op0, 0);
3988 mode = target_mode;
3991 if (target_mode != mode)
3992 subtarget = 0;
3994 if (code == GE)
3995 op0 = expand_unop (mode, one_cmpl_optab, op0,
3996 ((STORE_FLAG_VALUE == 1 || normalizep)
3997 ? 0 : subtarget), 0);
3999 if (STORE_FLAG_VALUE == 1 || normalizep)
4000 /* If we are supposed to produce a 0/1 value, we want to do
4001 a logical shift from the sign bit to the low-order bit; for
4002 a -1/0 value, we do an arithmetic shift. */
4003 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
4004 size_int (GET_MODE_BITSIZE (mode) - 1),
4005 subtarget, normalizep != -1);
4007 if (mode != target_mode)
4008 op0 = convert_modes (target_mode, mode, op0, 0);
4010 return op0;
4013 if (icode != CODE_FOR_nothing)
4015 /* We think we may be able to do this with a scc insn. Emit the
4016 comparison and then the scc insn.
4018 compare_from_rtx may call emit_queue, which would be deleted below
4019 if the scc insn fails. So call it ourselves before setting LAST. */
4021 emit_queue ();
4022 last = get_last_insn ();
4024 comparison
4025 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX, 0);
4026 if (GET_CODE (comparison) == CONST_INT)
4027 return (comparison == const0_rtx ? const0_rtx
4028 : normalizep == 1 ? const1_rtx
4029 : normalizep == -1 ? constm1_rtx
4030 : const_true_rtx);
4032 /* If the code of COMPARISON doesn't match CODE, something is
4033 wrong; we can no longer be sure that we have the operation.
4034 We could handle this case, but it should not happen. */
4036 if (GET_CODE (comparison) != code)
4037 abort ();
4039 /* Get a reference to the target in the proper mode for this insn. */
4040 compare_mode = insn_operand_mode[(int) icode][0];
4041 subtarget = target;
4042 if (preserve_subexpressions_p ()
4043 || ! (*insn_operand_predicate[(int) icode][0]) (subtarget, compare_mode))
4044 subtarget = gen_reg_rtx (compare_mode);
4046 pattern = GEN_FCN (icode) (subtarget);
4047 if (pattern)
4049 emit_insn (pattern);
4051 /* If we are converting to a wider mode, first convert to
4052 TARGET_MODE, then normalize. This produces better combining
4053 opportunities on machines that have a SIGN_EXTRACT when we are
4054 testing a single bit. This mostly benefits the 68k.
4056 If STORE_FLAG_VALUE does not have the sign bit set when
4057 interpreted in COMPARE_MODE, we can do this conversion as
4058 unsigned, which is usually more efficient. */
4059 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
4061 convert_move (target, subtarget,
4062 (GET_MODE_BITSIZE (compare_mode)
4063 <= HOST_BITS_PER_WIDE_INT)
4064 && 0 == (STORE_FLAG_VALUE
4065 & ((HOST_WIDE_INT) 1
4066 << (GET_MODE_BITSIZE (compare_mode) -1))));
4067 op0 = target;
4068 compare_mode = target_mode;
4070 else
4071 op0 = subtarget;
4073 /* If we want to keep subexpressions around, don't reuse our
4074 last target. */
4076 if (preserve_subexpressions_p ())
4077 subtarget = 0;
4079 /* Now normalize to the proper value in COMPARE_MODE. Sometimes
4080 we don't have to do anything. */
4081 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
4083 else if (normalizep == - STORE_FLAG_VALUE)
4084 op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
4086 /* We don't want to use STORE_FLAG_VALUE < 0 below since this
4087 makes it hard to use a value of just the sign bit due to
4088 ANSI integer constant typing rules. */
4089 else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
4090 && (STORE_FLAG_VALUE
4091 & ((HOST_WIDE_INT) 1
4092 << (GET_MODE_BITSIZE (compare_mode) - 1))))
4093 op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
4094 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
4095 subtarget, normalizep == 1);
4096 else if (STORE_FLAG_VALUE & 1)
4098 op0 = expand_and (op0, const1_rtx, subtarget);
4099 if (normalizep == -1)
4100 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
4102 else
4103 abort ();
4105 /* If we were converting to a smaller mode, do the
4106 conversion now. */
4107 if (target_mode != compare_mode)
4109 convert_move (target, op0, 0);
4110 return target;
4112 else
4113 return op0;
4117 delete_insns_since (last);
4119 /* If expensive optimizations, use different pseudo registers for each
4120 insn, instead of reusing the same pseudo. This leads to better CSE,
4121 but slows down the compiler, since there are more pseudos */
4122 subtarget = (!flag_expensive_optimizations
4123 && (target_mode == mode)) ? target : NULL_RTX;
4125 /* If we reached here, we can't do this with a scc insn. However, there
4126 are some comparisons that can be done directly. For example, if
4127 this is an equality comparison of integers, we can try to exclusive-or
4128 (or subtract) the two operands and use a recursive call to try the
4129 comparison with zero. Don't do any of these cases if branches are
4130 very cheap. */
4132 if (BRANCH_COST > 0
4133 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
4134 && op1 != const0_rtx)
4136 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
4137 OPTAB_WIDEN);
4139 if (tem == 0)
4140 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
4141 OPTAB_WIDEN);
4142 if (tem != 0)
4143 tem = emit_store_flag (target, code, tem, const0_rtx,
4144 mode, unsignedp, normalizep);
4145 if (tem == 0)
4146 delete_insns_since (last);
4147 return tem;
4150 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
4151 the constant zero. Reject all other comparisons at this point. Only
4152 do LE and GT if branches are expensive since they are expensive on
4153 2-operand machines. */
4155 if (BRANCH_COST == 0
4156 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
4157 || (code != EQ && code != NE
4158 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
4159 return 0;
4161 /* See what we need to return. We can only return a 1, -1, or the
4162 sign bit. */
4164 if (normalizep == 0)
4166 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4167 normalizep = STORE_FLAG_VALUE;
4169 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4170 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4171 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
4173 else
4174 return 0;
4177 /* Try to put the result of the comparison in the sign bit. Assume we can't
4178 do the necessary operation below. */
4180 tem = 0;
4182 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
4183 the sign bit set. */
4185 if (code == LE)
4187 /* This is destructive, so SUBTARGET can't be OP0. */
4188 if (rtx_equal_p (subtarget, op0))
4189 subtarget = 0;
4191 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
4192 OPTAB_WIDEN);
4193 if (tem)
4194 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
4195 OPTAB_WIDEN);
4198 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
4199 number of bits in the mode of OP0, minus one. */
4201 if (code == GT)
4203 if (rtx_equal_p (subtarget, op0))
4204 subtarget = 0;
4206 tem = expand_shift (RSHIFT_EXPR, mode, op0,
4207 size_int (GET_MODE_BITSIZE (mode) - 1),
4208 subtarget, 0);
4209 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
4210 OPTAB_WIDEN);
4213 if (code == EQ || code == NE)
4215 /* For EQ or NE, one way to do the comparison is to apply an operation
4216 that converts the operand into a positive number if it is non-zero
4217 or zero if it was originally zero. Then, for EQ, we subtract 1 and
4218 for NE we negate. This puts the result in the sign bit. Then we
4219 normalize with a shift, if needed.
4221 Two operations that can do the above actions are ABS and FFS, so try
4222 them. If that doesn't work, and MODE is smaller than a full word,
4223 we can use zero-extension to the wider mode (an unsigned conversion)
4224 as the operation. */
4226 if (abs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
4227 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
4228 else if (ffs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
4229 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
4230 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
4232 op0 = protect_from_queue (op0, 0);
4233 tem = convert_modes (word_mode, mode, op0, 1);
4234 mode = word_mode;
4237 if (tem != 0)
4239 if (code == EQ)
4240 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
4241 0, OPTAB_WIDEN);
4242 else
4243 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
4246 /* If we couldn't do it that way, for NE we can "or" the two's complement
4247 of the value with itself. For EQ, we take the one's complement of
4248 that "or", which is an extra insn, so we only handle EQ if branches
4249 are expensive. */
4251 if (tem == 0 && (code == NE || BRANCH_COST > 1))
4253 if (rtx_equal_p (subtarget, op0))
4254 subtarget = 0;
4256 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
4257 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
4258 OPTAB_WIDEN);
4260 if (tem && code == EQ)
4261 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
4265 if (tem && normalizep)
4266 tem = expand_shift (RSHIFT_EXPR, mode, tem,
4267 size_int (GET_MODE_BITSIZE (mode) - 1),
4268 subtarget, normalizep == 1);
4270 if (tem)
4272 if (GET_MODE (tem) != target_mode)
4274 convert_move (target, tem, 0);
4275 tem = target;
4277 else if (!subtarget)
4279 emit_move_insn (target, tem);
4280 tem = target;
4283 else
4284 delete_insns_since (last);
4286 return tem;
4289 /* Like emit_store_flag, but always succeeds. */
4292 emit_store_flag_force (target, code, op0, op1, mode, unsignedp, normalizep)
4293 rtx target;
4294 enum rtx_code code;
4295 rtx op0, op1;
4296 enum machine_mode mode;
4297 int unsignedp;
4298 int normalizep;
4300 rtx tem, label;
4302 /* First see if emit_store_flag can do the job. */
4303 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
4304 if (tem != 0)
4305 return tem;
4307 if (normalizep == 0)
4308 normalizep = 1;
4310 /* If this failed, we have to do this with set/compare/jump/set code. */
4312 if (GET_CODE (target) != REG
4313 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
4314 target = gen_reg_rtx (GET_MODE (target));
4316 emit_move_insn (target, const1_rtx);
4317 tem = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX, 0);
4318 if (GET_CODE (tem) == CONST_INT)
4319 return tem;
4321 label = gen_label_rtx ();
4322 if (bcc_gen_fctn[(int) code] == 0)
4323 abort ();
4325 emit_jump_insn ((*bcc_gen_fctn[(int) code]) (label));
4326 emit_move_insn (target, const0_rtx);
4327 emit_label (label);
4329 return target;