(distribute_notes, case REG_DEAD): If a call uses a
[official-gcc.git] / gcc / expmed.c
blob9abd37250bd87e98b6979d763e6143959ad6aa45
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, 93, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "config.h"
23 #include "rtl.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "insn-flags.h"
27 #include "insn-codes.h"
28 #include "insn-config.h"
29 #include "expr.h"
30 #include "real.h"
31 #include "recog.h"
33 static void store_fixed_bit_field PROTO((rtx, int, int, int, rtx, int));
34 static void store_split_bit_field PROTO((rtx, int, int, rtx, int));
35 static rtx extract_fixed_bit_field PROTO((enum machine_mode, rtx, int,
36 int, int, rtx, int, int));
37 static rtx mask_rtx PROTO((enum machine_mode, int,
38 int, int));
39 static rtx lshift_value PROTO((enum machine_mode, rtx,
40 int, int));
41 static rtx extract_split_bit_field PROTO((rtx, int, int, int, int));
43 #define CEIL(x,y) (((x) + (y) - 1) / (y))
45 /* Non-zero means divides or modulus operations are relatively cheap for
46 powers of two, so don't use branches; emit the operation instead.
47 Usually, this will mean that the MD file will emit non-branch
48 sequences. */
50 static int sdiv_pow2_cheap, smod_pow2_cheap;
52 #ifndef SLOW_UNALIGNED_ACCESS
53 #define SLOW_UNALIGNED_ACCESS STRICT_ALIGNMENT
54 #endif
56 /* For compilers that support multiple targets with different word sizes,
57 MAX_BITS_PER_WORD contains the biggest value of BITS_PER_WORD. An example
58 is the H8/300(H) compiler. */
60 #ifndef MAX_BITS_PER_WORD
61 #define MAX_BITS_PER_WORD BITS_PER_WORD
62 #endif
64 /* Cost of various pieces of RTL. */
65 static int add_cost, negate_cost, zero_cost;
66 static int shift_cost[MAX_BITS_PER_WORD];
67 static int shiftadd_cost[MAX_BITS_PER_WORD];
68 static int shiftsub_cost[MAX_BITS_PER_WORD];
70 void
71 init_expmed ()
73 char *free_point;
74 /* This is "some random pseudo register" for purposes of calling recog
75 to see what insns exist. */
76 rtx reg = gen_rtx (REG, word_mode, 10000);
77 rtx shift_insn, shiftadd_insn, shiftsub_insn;
78 int dummy;
79 int m;
81 start_sequence ();
83 /* Since we are on the permanent obstack, we must be sure we save this
84 spot AFTER we call start_sequence, since it will reuse the rtl it
85 makes. */
87 free_point = (char *) oballoc (0);
89 zero_cost = rtx_cost (const0_rtx, 0);
90 add_cost = rtx_cost (gen_rtx (PLUS, word_mode, reg, reg), SET);
92 shift_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
93 gen_rtx (ASHIFT, word_mode, reg,
94 const0_rtx)));
96 shiftadd_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
97 gen_rtx (PLUS, word_mode,
98 gen_rtx (MULT, word_mode,
99 reg, const0_rtx),
100 reg)));
102 shiftsub_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
103 gen_rtx (MINUS, word_mode,
104 gen_rtx (MULT, word_mode,
105 reg, const0_rtx),
106 reg)));
108 init_recog ();
110 shift_cost[0] = 0;
111 shiftadd_cost[0] = shiftsub_cost[0] = add_cost;
113 for (m = 1; m < BITS_PER_WORD; m++)
115 shift_cost[m] = shiftadd_cost[m] = shiftsub_cost[m] = 32000;
117 XEXP (SET_SRC (PATTERN (shift_insn)), 1) = GEN_INT (m);
118 if (recog (PATTERN (shift_insn), shift_insn, &dummy) >= 0)
119 shift_cost[m] = rtx_cost (SET_SRC (PATTERN (shift_insn)), SET);
121 XEXP (XEXP (SET_SRC (PATTERN (shiftadd_insn)), 0), 1)
122 = GEN_INT ((HOST_WIDE_INT) 1 << m);
123 if (recog (PATTERN (shiftadd_insn), shiftadd_insn, &dummy) >= 0)
124 shiftadd_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftadd_insn)), SET);
126 XEXP (XEXP (SET_SRC (PATTERN (shiftsub_insn)), 0), 1)
127 = GEN_INT ((HOST_WIDE_INT) 1 << m);
128 if (recog (PATTERN (shiftsub_insn), shiftsub_insn, &dummy) >= 0)
129 shiftsub_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftsub_insn)), SET);
132 negate_cost = rtx_cost (gen_rtx (NEG, word_mode, reg), SET);
134 sdiv_pow2_cheap
135 = (rtx_cost (gen_rtx (DIV, word_mode, reg, GEN_INT (32)), SET)
136 <= 2 * add_cost);
137 smod_pow2_cheap
138 = (rtx_cost (gen_rtx (MOD, word_mode, reg, GEN_INT (32)), SET)
139 <= 2 * add_cost);
141 /* Free the objects we just allocated. */
142 end_sequence ();
143 obfree (free_point);
146 /* Return an rtx representing minus the value of X.
147 MODE is the intended mode of the result,
148 useful if X is a CONST_INT. */
151 negate_rtx (mode, x)
152 enum machine_mode mode;
153 rtx x;
155 if (GET_CODE (x) == CONST_INT)
157 HOST_WIDE_INT val = - INTVAL (x);
158 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
160 /* Sign extend the value from the bits that are significant. */
161 if (val & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
162 val |= (HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (mode);
163 else
164 val &= ((HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (mode)) - 1;
166 return GEN_INT (val);
168 else
169 return expand_unop (GET_MODE (x), neg_optab, x, NULL_RTX, 0);
172 /* Generate code to store value from rtx VALUE
173 into a bit-field within structure STR_RTX
174 containing BITSIZE bits starting at bit BITNUM.
175 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
176 ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
177 TOTAL_SIZE is the size of the structure in bytes, or -1 if varying. */
179 /* ??? Note that there are two different ideas here for how
180 to determine the size to count bits within, for a register.
181 One is BITS_PER_WORD, and the other is the size of operand 3
182 of the insv pattern. (The latter assumes that an n-bit machine
183 will be able to insert bit fields up to n bits wide.)
184 It isn't certain that either of these is right.
185 extract_bit_field has the same quandary. */
188 store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, align, total_size)
189 rtx str_rtx;
190 register int bitsize;
191 int bitnum;
192 enum machine_mode fieldmode;
193 rtx value;
194 int align;
195 int total_size;
197 int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
198 register int offset = bitnum / unit;
199 register int bitpos = bitnum % unit;
200 register rtx op0 = str_rtx;
202 if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
203 abort ();
205 /* Discount the part of the structure before the desired byte.
206 We need to know how many bytes are safe to reference after it. */
207 if (total_size >= 0)
208 total_size -= (bitpos / BIGGEST_ALIGNMENT
209 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
211 while (GET_CODE (op0) == SUBREG)
213 /* The following line once was done only if WORDS_BIG_ENDIAN,
214 but I think that is a mistake. WORDS_BIG_ENDIAN is
215 meaningful at a much higher level; when structures are copied
216 between memory and regs, the higher-numbered regs
217 always get higher addresses. */
218 offset += SUBREG_WORD (op0);
219 /* We used to adjust BITPOS here, but now we do the whole adjustment
220 right after the loop. */
221 op0 = SUBREG_REG (op0);
224 #if BYTES_BIG_ENDIAN
225 /* If OP0 is a register, BITPOS must count within a word.
226 But as we have it, it counts within whatever size OP0 now has.
227 On a bigendian machine, these are not the same, so convert. */
228 if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
229 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
230 #endif
232 value = protect_from_queue (value, 0);
234 if (flag_force_mem)
235 value = force_not_mem (value);
237 /* Note that the adjustment of BITPOS above has no effect on whether
238 BITPOS is 0 in a REG bigger than a word. */
239 if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
240 && (GET_CODE (op0) != MEM
241 || ! SLOW_UNALIGNED_ACCESS
242 || (offset * BITS_PER_UNIT % bitsize == 0
243 && align % GET_MODE_SIZE (fieldmode) == 0))
244 && bitpos == 0 && bitsize == GET_MODE_BITSIZE (fieldmode))
246 /* Storing in a full-word or multi-word field in a register
247 can be done with just SUBREG. */
248 if (GET_MODE (op0) != fieldmode)
250 if (GET_CODE (op0) == REG)
251 op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
252 else
253 op0 = change_address (op0, fieldmode,
254 plus_constant (XEXP (op0, 0), offset));
256 emit_move_insn (op0, value);
257 return value;
260 /* Storing an lsb-aligned field in a register
261 can be done with a movestrict instruction. */
263 if (GET_CODE (op0) != MEM
264 #if BYTES_BIG_ENDIAN
265 && bitpos + bitsize == unit
266 #else
267 && bitpos == 0
268 #endif
269 && bitsize == GET_MODE_BITSIZE (fieldmode)
270 && (GET_MODE (op0) == fieldmode
271 || (movstrict_optab->handlers[(int) fieldmode].insn_code
272 != CODE_FOR_nothing)))
274 /* Get appropriate low part of the value being stored. */
275 if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
276 value = gen_lowpart (fieldmode, value);
277 else if (!(GET_CODE (value) == SYMBOL_REF
278 || GET_CODE (value) == LABEL_REF
279 || GET_CODE (value) == CONST))
280 value = convert_to_mode (fieldmode, value, 0);
282 if (GET_MODE (op0) == fieldmode)
283 emit_move_insn (op0, value);
284 else
286 int icode = movstrict_optab->handlers[(int) fieldmode].insn_code;
287 if(! (*insn_operand_predicate[icode][1]) (value, fieldmode))
288 value = copy_to_mode_reg (fieldmode, value);
289 emit_insn (GEN_FCN (icode)
290 (gen_rtx (SUBREG, fieldmode, op0, offset), value));
292 return value;
295 /* Handle fields bigger than a word. */
297 if (bitsize > BITS_PER_WORD)
299 /* Here we transfer the words of the field
300 in the order least significant first.
301 This is because the most significant word is the one which may
302 be less than full. */
304 int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
305 int i;
307 /* This is the mode we must force value to, so that there will be enough
308 subwords to extract. Note that fieldmode will often (always?) be
309 VOIDmode, because that is what store_field uses to indicate that this
310 is a bit field, but passing VOIDmode to operand_subword_force will
311 result in an abort. */
312 fieldmode = mode_for_size (nwords * BITS_PER_WORD, MODE_INT, 0);
314 for (i = 0; i < nwords; i++)
316 /* If I is 0, use the low-order word in both field and target;
317 if I is 1, use the next to lowest word; and so on. */
318 int wordnum = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
319 int bit_offset = (WORDS_BIG_ENDIAN
320 ? MAX (bitsize - (i + 1) * BITS_PER_WORD, 0)
321 : i * BITS_PER_WORD);
322 store_bit_field (op0, MIN (BITS_PER_WORD,
323 bitsize - i * BITS_PER_WORD),
324 bitnum + bit_offset, word_mode,
325 operand_subword_force (value, wordnum,
326 (GET_MODE (value) == VOIDmode
327 ? fieldmode
328 : GET_MODE (value))),
329 align, total_size);
331 return value;
334 /* From here on we can assume that the field to be stored in is
335 a full-word (whatever type that is), since it is shorter than a word. */
337 /* OFFSET is the number of words or bytes (UNIT says which)
338 from STR_RTX to the first word or byte containing part of the field. */
340 if (GET_CODE (op0) == REG)
342 if (offset != 0
343 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
344 op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
345 op0, offset);
346 offset = 0;
348 else
350 op0 = protect_from_queue (op0, 1);
353 /* If VALUE is a floating-point mode, access it as an integer of the
354 corresponding size. This can occur on a machine with 64 bit registers
355 that uses SFmode for float. This can also occur for unaligned float
356 structure fields. */
357 if (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT)
359 if (GET_CODE (value) != REG)
360 value = copy_to_reg (value);
361 value = gen_rtx (SUBREG, word_mode, value, 0);
364 /* Now OFFSET is nonzero only if OP0 is memory
365 and is therefore always measured in bytes. */
367 #ifdef HAVE_insv
368 if (HAVE_insv
369 && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
370 /* Ensure insv's size is wide enough for this field. */
371 && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_insv][3])
372 >= bitsize))
374 int xbitpos = bitpos;
375 rtx value1;
376 rtx xop0 = op0;
377 rtx last = get_last_insn ();
378 rtx pat;
379 enum machine_mode maxmode
380 = insn_operand_mode[(int) CODE_FOR_insv][3];
382 int save_volatile_ok = volatile_ok;
383 volatile_ok = 1;
385 /* If this machine's insv can only insert into a register, or if we
386 are to force MEMs into a register, copy OP0 into a register and
387 save it back later. */
388 if (GET_CODE (op0) == MEM
389 && (flag_force_mem
390 || ! ((*insn_operand_predicate[(int) CODE_FOR_insv][0])
391 (op0, VOIDmode))))
393 rtx tempreg;
394 enum machine_mode bestmode;
396 /* Get the mode to use for inserting into this field. If OP0 is
397 BLKmode, get the smallest mode consistent with the alignment. If
398 OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
399 mode. Otherwise, use the smallest mode containing the field. */
401 if (GET_MODE (op0) == BLKmode
402 || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
403 bestmode
404 = get_best_mode (bitsize, bitnum, align * BITS_PER_UNIT, maxmode,
405 MEM_VOLATILE_P (op0));
406 else
407 bestmode = GET_MODE (op0);
409 if (bestmode == VOIDmode
410 || (STRICT_ALIGNMENT && GET_MODE_SIZE (bestmode) > align))
411 goto insv_loses;
413 /* Adjust address to point to the containing unit of that mode. */
414 unit = GET_MODE_BITSIZE (bestmode);
415 /* Compute offset as multiple of this unit, counting in bytes. */
416 offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
417 bitpos = bitnum % unit;
418 op0 = change_address (op0, bestmode,
419 plus_constant (XEXP (op0, 0), offset));
421 /* Fetch that unit, store the bitfield in it, then store the unit. */
422 tempreg = copy_to_reg (op0);
423 store_bit_field (tempreg, bitsize, bitpos, fieldmode, value,
424 align, total_size);
425 emit_move_insn (op0, tempreg);
426 return value;
428 volatile_ok = save_volatile_ok;
430 /* Add OFFSET into OP0's address. */
431 if (GET_CODE (xop0) == MEM)
432 xop0 = change_address (xop0, byte_mode,
433 plus_constant (XEXP (xop0, 0), offset));
435 /* If xop0 is a register, we need it in MAXMODE
436 to make it acceptable to the format of insv. */
437 if (GET_CODE (xop0) == SUBREG)
438 /* We can't just change the mode, because this might clobber op0,
439 and we will need the original value of op0 if insv fails. */
440 xop0 = gen_rtx (SUBREG, maxmode, SUBREG_REG (xop0), SUBREG_WORD (xop0));
441 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
442 xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
444 /* On big-endian machines, we count bits from the most significant.
445 If the bit field insn does not, we must invert. */
447 #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
448 xbitpos = unit - bitsize - xbitpos;
449 #endif
450 /* We have been counting XBITPOS within UNIT.
451 Count instead within the size of the register. */
452 #if BITS_BIG_ENDIAN
453 if (GET_CODE (xop0) != MEM)
454 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
455 #endif
456 unit = GET_MODE_BITSIZE (maxmode);
458 /* Convert VALUE to maxmode (which insv insn wants) in VALUE1. */
459 value1 = value;
460 if (GET_MODE (value) != maxmode)
462 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
464 /* Optimization: Don't bother really extending VALUE
465 if it has all the bits we will actually use. However,
466 if we must narrow it, be sure we do it correctly. */
468 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
470 /* Avoid making subreg of a subreg, or of a mem. */
471 if (GET_CODE (value1) != REG)
472 value1 = copy_to_reg (value1);
473 value1 = gen_rtx (SUBREG, maxmode, value1, 0);
475 else
476 value1 = gen_lowpart (maxmode, value1);
478 else if (!CONSTANT_P (value))
479 /* Parse phase is supposed to make VALUE's data type
480 match that of the component reference, which is a type
481 at least as wide as the field; so VALUE should have
482 a mode that corresponds to that type. */
483 abort ();
486 /* If this machine's insv insists on a register,
487 get VALUE1 into a register. */
488 if (! ((*insn_operand_predicate[(int) CODE_FOR_insv][3])
489 (value1, maxmode)))
490 value1 = force_reg (maxmode, value1);
492 pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
493 if (pat)
494 emit_insn (pat);
495 else
497 delete_insns_since (last);
498 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
501 else
502 insv_loses:
503 #endif
504 /* Insv is not available; store using shifts and boolean ops. */
505 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
506 return value;
509 /* Use shifts and boolean operations to store VALUE
510 into a bit field of width BITSIZE
511 in a memory location specified by OP0 except offset by OFFSET bytes.
512 (OFFSET must be 0 if OP0 is a register.)
513 The field starts at position BITPOS within the byte.
514 (If OP0 is a register, it may be a full word or a narrower mode,
515 but BITPOS still counts within a full word,
516 which is significant on bigendian machines.)
517 STRUCT_ALIGN is the alignment the structure is known to have (in bytes).
519 Note that protect_from_queue has already been done on OP0 and VALUE. */
521 static void
522 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, struct_align)
523 register rtx op0;
524 register int offset, bitsize, bitpos;
525 register rtx value;
526 int struct_align;
528 register enum machine_mode mode;
529 int total_bits = BITS_PER_WORD;
530 rtx subtarget, temp;
531 int all_zero = 0;
532 int all_one = 0;
534 /* There is a case not handled here:
535 a structure with a known alignment of just a halfword
536 and a field split across two aligned halfwords within the structure.
537 Or likewise a structure with a known alignment of just a byte
538 and a field split across two bytes.
539 Such cases are not supposed to be able to occur. */
541 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
543 if (offset != 0)
544 abort ();
545 /* Special treatment for a bit field split across two registers. */
546 if (bitsize + bitpos > BITS_PER_WORD)
548 store_split_bit_field (op0, bitsize, bitpos,
549 value, BITS_PER_WORD);
550 return;
553 else
555 /* Get the proper mode to use for this field. We want a mode that
556 includes the entire field. If such a mode would be larger than
557 a word, we won't be doing the extraction the normal way. */
559 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
560 struct_align * BITS_PER_UNIT, word_mode,
561 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
563 if (mode == VOIDmode)
565 /* The only way this should occur is if the field spans word
566 boundaries. */
567 store_split_bit_field (op0,
568 bitsize, bitpos + offset * BITS_PER_UNIT,
569 value, struct_align);
570 return;
573 total_bits = GET_MODE_BITSIZE (mode);
575 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
576 be be in the range 0 to total_bits-1, and put any excess bytes in
577 OFFSET. */
578 if (bitpos >= total_bits)
580 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
581 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
582 * BITS_PER_UNIT);
585 /* Get ref to an aligned byte, halfword, or word containing the field.
586 Adjust BITPOS to be position within a word,
587 and OFFSET to be the offset of that word.
588 Then alter OP0 to refer to that word. */
589 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
590 offset -= (offset % (total_bits / BITS_PER_UNIT));
591 op0 = change_address (op0, mode,
592 plus_constant (XEXP (op0, 0), offset));
595 mode = GET_MODE (op0);
597 /* Now MODE is either some integral mode for a MEM as OP0,
598 or is a full-word for a REG as OP0. TOTAL_BITS corresponds.
599 The bit field is contained entirely within OP0.
600 BITPOS is the starting bit number within OP0.
601 (OP0's mode may actually be narrower than MODE.) */
603 #if BYTES_BIG_ENDIAN
604 /* BITPOS is the distance between our msb
605 and that of the containing datum.
606 Convert it to the distance from the lsb. */
608 bitpos = total_bits - bitsize - bitpos;
609 #endif
610 /* Now BITPOS is always the distance between our lsb
611 and that of OP0. */
613 /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
614 we must first convert its mode to MODE. */
616 if (GET_CODE (value) == CONST_INT)
618 register HOST_WIDE_INT v = INTVAL (value);
620 if (bitsize < HOST_BITS_PER_WIDE_INT)
621 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
623 if (v == 0)
624 all_zero = 1;
625 else if ((bitsize < HOST_BITS_PER_WIDE_INT
626 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
627 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
628 all_one = 1;
630 value = lshift_value (mode, value, bitpos, bitsize);
632 else
634 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
635 && bitpos + bitsize != GET_MODE_BITSIZE (mode));
637 if (GET_MODE (value) != mode)
639 if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
640 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
641 value = gen_lowpart (mode, value);
642 else
643 value = convert_to_mode (mode, value, 1);
646 if (must_and)
647 value = expand_binop (mode, and_optab, value,
648 mask_rtx (mode, 0, bitsize, 0),
649 NULL_RTX, 1, OPTAB_LIB_WIDEN);
650 if (bitpos > 0)
651 value = expand_shift (LSHIFT_EXPR, mode, value,
652 build_int_2 (bitpos, 0), NULL_RTX, 1);
655 /* Now clear the chosen bits in OP0,
656 except that if VALUE is -1 we need not bother. */
658 subtarget = (GET_CODE (op0) == REG || ! flag_force_mem) ? op0 : 0;
660 if (! all_one)
662 temp = expand_binop (mode, and_optab, op0,
663 mask_rtx (mode, bitpos, bitsize, 1),
664 subtarget, 1, OPTAB_LIB_WIDEN);
665 subtarget = temp;
667 else
668 temp = op0;
670 /* Now logical-or VALUE into OP0, unless it is zero. */
672 if (! all_zero)
673 temp = expand_binop (mode, ior_optab, temp, value,
674 subtarget, 1, OPTAB_LIB_WIDEN);
675 if (op0 != temp)
676 emit_move_insn (op0, temp);
679 /* Store a bit field that is split across multiple accessible memory objects.
681 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
682 BITSIZE is the field width; BITPOS the position of its first bit
683 (within the word).
684 VALUE is the value to store.
685 ALIGN is the known alignment of OP0, measured in bytes.
686 This is also the size of the memory objects to be used.
688 This does not yet handle fields wider than BITS_PER_WORD. */
690 static void
691 store_split_bit_field (op0, bitsize, bitpos, value, align)
692 rtx op0;
693 int bitsize, bitpos;
694 rtx value;
695 int align;
697 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
698 much at a time. */
699 int unit = MIN (align * BITS_PER_UNIT, BITS_PER_WORD);
700 int bitsdone = 0;
702 /* If VALUE is a constant other than a CONST_INT, get it into a register in
703 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
704 that VALUE might be a floating-point constant. */
705 if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
707 rtx word = gen_lowpart_common (word_mode, value);
709 if (word && (value != word))
710 value = word;
711 else
712 value = gen_lowpart_common (word_mode,
713 force_reg (GET_MODE (value), value));
716 while (bitsdone < bitsize)
718 int thissize;
719 rtx part, word;
720 int thispos;
721 int offset;
723 offset = (bitpos + bitsdone) / unit;
724 thispos = (bitpos + bitsdone) % unit;
726 /* THISSIZE must not overrun a word boundary. Otherwise,
727 store_fixed_bit_field will call us again, and we will mutually
728 recurse forever. */
729 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
730 thissize = MIN (thissize, unit - thispos);
732 #if BYTES_BIG_ENDIAN
733 /* Fetch successively less significant portions. */
734 if (GET_CODE (value) == CONST_INT)
735 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
736 >> (bitsize - bitsdone - thissize))
737 & (((HOST_WIDE_INT) 1 << thissize) - 1));
738 else
740 /* The args are chosen so that the last part
741 includes the lsb. */
742 int bit_offset = 0;
743 /* If the value isn't in memory, then it must be right aligned
744 if a register, so skip past the padding on the left. If it
745 is in memory, then there is no padding on the left. */
746 if (GET_CODE (value) != MEM)
747 bit_offset = BITS_PER_WORD - bitsize;
748 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
749 bit_offset + bitsdone,
750 NULL_RTX, 1, align);
752 #else
753 /* Fetch successively more significant portions. */
754 if (GET_CODE (value) == CONST_INT)
755 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value)) >> bitsdone)
756 & (((HOST_WIDE_INT) 1 << thissize) - 1));
757 else
758 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
759 bitsdone, NULL_RTX, 1, align);
760 #endif
762 /* If OP0 is a register, then handle OFFSET here.
764 When handling multiword bitfields, extract_bit_field may pass
765 down a word_mode SUBREG of a larger REG for a bitfield that actually
766 crosses a word boundary. Thus, for a SUBREG, we must find
767 the current word starting from the base register. */
768 if (GET_CODE (op0) == SUBREG)
770 word = operand_subword (SUBREG_REG (op0),
771 SUBREG_WORD (op0) + offset, 1,
772 GET_MODE (SUBREG_REG (op0)));
773 offset = 0;
775 else if (GET_CODE (op0) == REG)
777 word = operand_subword (op0, offset, 1, GET_MODE (op0));
778 offset = 0;
780 else
781 word = op0;
783 if (word == 0)
784 abort ();
786 /* OFFSET is in UNITs, and UNIT is in bits.
787 store_fixed_bit_field wants offset in bytes. */
788 store_fixed_bit_field (word, offset * unit / BITS_PER_UNIT,
789 thissize, thispos, part, align);
790 bitsdone += thissize;
794 /* Generate code to extract a byte-field from STR_RTX
795 containing BITSIZE bits, starting at BITNUM,
796 and put it in TARGET if possible (if TARGET is nonzero).
797 Regardless of TARGET, we return the rtx for where the value is placed.
798 It may be a QUEUED.
800 STR_RTX is the structure containing the byte (a REG or MEM).
801 UNSIGNEDP is nonzero if this is an unsigned bit field.
802 MODE is the natural mode of the field value once extracted.
803 TMODE is the mode the caller would like the value to have;
804 but the value may be returned with type MODE instead.
806 ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
807 TOTAL_SIZE is the size in bytes of the containing structure,
808 or -1 if varying.
810 If a TARGET is specified and we can store in it at no extra cost,
811 we do so, and return TARGET.
812 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
813 if they are equally easy. */
816 extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
817 target, mode, tmode, align, total_size)
818 rtx str_rtx;
819 register int bitsize;
820 int bitnum;
821 int unsignedp;
822 rtx target;
823 enum machine_mode mode, tmode;
824 int align;
825 int total_size;
827 int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
828 register int offset = bitnum / unit;
829 register int bitpos = bitnum % unit;
830 register rtx op0 = str_rtx;
831 rtx spec_target = target;
832 rtx spec_target_subreg = 0;
834 if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
835 abort ();
837 /* Discount the part of the structure before the desired byte.
838 We need to know how many bytes are safe to reference after it. */
839 if (total_size >= 0)
840 total_size -= (bitpos / BIGGEST_ALIGNMENT
841 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
843 if (tmode == VOIDmode)
844 tmode = mode;
845 while (GET_CODE (op0) == SUBREG)
847 offset += SUBREG_WORD (op0);
848 op0 = SUBREG_REG (op0);
851 #if BYTES_BIG_ENDIAN
852 /* If OP0 is a register, BITPOS must count within a word.
853 But as we have it, it counts within whatever size OP0 now has.
854 On a bigendian machine, these are not the same, so convert. */
855 if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
856 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
857 #endif
859 /* Extracting a full-word or multi-word value
860 from a structure in a register or aligned memory.
861 This can be done with just SUBREG.
862 So too extracting a subword value in
863 the least significant part of the register. */
865 if ((GET_CODE (op0) == REG
866 || (GET_CODE (op0) == MEM
867 && (! SLOW_UNALIGNED_ACCESS
868 || (offset * BITS_PER_UNIT % bitsize == 0
869 && align * BITS_PER_UNIT % bitsize == 0))))
870 && ((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
871 && bitpos % BITS_PER_WORD == 0)
872 || (mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0) != BLKmode
873 #if BYTES_BIG_ENDIAN
874 && bitpos + bitsize == BITS_PER_WORD
875 #else
876 && bitpos == 0
877 #endif
880 enum machine_mode mode1
881 = mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0);
883 if (mode1 != GET_MODE (op0))
885 if (GET_CODE (op0) == REG)
886 op0 = gen_rtx (SUBREG, mode1, op0, offset);
887 else
888 op0 = change_address (op0, mode1,
889 plus_constant (XEXP (op0, 0), offset));
891 if (mode1 != mode)
892 return convert_to_mode (tmode, op0, unsignedp);
893 return op0;
896 /* Handle fields bigger than a word. */
898 if (bitsize > BITS_PER_WORD)
900 /* Here we transfer the words of the field
901 in the order least significant first.
902 This is because the most significant word is the one which may
903 be less than full. */
905 int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
906 int i;
908 if (target == 0 || GET_CODE (target) != REG)
909 target = gen_reg_rtx (mode);
911 for (i = 0; i < nwords; i++)
913 /* If I is 0, use the low-order word in both field and target;
914 if I is 1, use the next to lowest word; and so on. */
915 int wordnum = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
916 int bit_offset = (WORDS_BIG_ENDIAN
917 ? MAX (0, bitsize - (i + 1) * BITS_PER_WORD)
918 : i * BITS_PER_WORD);
919 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
920 rtx result_part
921 = extract_bit_field (op0, MIN (BITS_PER_WORD,
922 bitsize - i * BITS_PER_WORD),
923 bitnum + bit_offset,
924 1, target_part, mode, word_mode,
925 align, total_size);
927 if (target_part == 0)
928 abort ();
930 if (result_part != target_part)
931 emit_move_insn (target_part, result_part);
934 if (unsignedp)
935 return target;
936 /* Signed bit field: sign-extend with two arithmetic shifts. */
937 target = expand_shift (LSHIFT_EXPR, mode, target,
938 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
939 NULL_RTX, 0);
940 return expand_shift (RSHIFT_EXPR, mode, target,
941 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
942 NULL_RTX, 0);
945 /* From here on we know the desired field is smaller than a word
946 so we can assume it is an integer. So we can safely extract it as one
947 size of integer, if necessary, and then truncate or extend
948 to the size that is wanted. */
950 /* OFFSET is the number of words or bytes (UNIT says which)
951 from STR_RTX to the first word or byte containing part of the field. */
953 if (GET_CODE (op0) == REG)
955 if (offset != 0
956 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
957 op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
958 op0, offset);
959 offset = 0;
961 else
963 op0 = protect_from_queue (str_rtx, 1);
966 /* Now OFFSET is nonzero only for memory operands. */
968 if (unsignedp)
970 #ifdef HAVE_extzv
971 if (HAVE_extzv
972 && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extzv][0])
973 >= bitsize))
975 int xbitpos = bitpos, xoffset = offset;
976 rtx bitsize_rtx, bitpos_rtx;
977 rtx last = get_last_insn();
978 rtx xop0 = op0;
979 rtx xtarget = target;
980 rtx xspec_target = spec_target;
981 rtx xspec_target_subreg = spec_target_subreg;
982 rtx pat;
983 enum machine_mode maxmode
984 = insn_operand_mode[(int) CODE_FOR_extzv][0];
986 if (GET_CODE (xop0) == MEM)
988 int save_volatile_ok = volatile_ok;
989 volatile_ok = 1;
991 /* Is the memory operand acceptable? */
992 if (flag_force_mem
993 || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
994 (xop0, GET_MODE (xop0))))
996 /* No, load into a reg and extract from there. */
997 enum machine_mode bestmode;
999 /* Get the mode to use for inserting into this field. If
1000 OP0 is BLKmode, get the smallest mode consistent with the
1001 alignment. If OP0 is a non-BLKmode object that is no
1002 wider than MAXMODE, use its mode. Otherwise, use the
1003 smallest mode containing the field. */
1005 if (GET_MODE (xop0) == BLKmode
1006 || (GET_MODE_SIZE (GET_MODE (op0))
1007 > GET_MODE_SIZE (maxmode)))
1008 bestmode = get_best_mode (bitsize, bitnum,
1009 align * BITS_PER_UNIT, maxmode,
1010 MEM_VOLATILE_P (xop0));
1011 else
1012 bestmode = GET_MODE (xop0);
1014 if (bestmode == VOIDmode
1015 || (STRICT_ALIGNMENT && GET_MODE_SIZE (bestmode) > align))
1016 goto extzv_loses;
1018 /* Compute offset as multiple of this unit,
1019 counting in bytes. */
1020 unit = GET_MODE_BITSIZE (bestmode);
1021 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1022 xbitpos = bitnum % unit;
1023 xop0 = change_address (xop0, bestmode,
1024 plus_constant (XEXP (xop0, 0),
1025 xoffset));
1026 /* Fetch it to a register in that size. */
1027 xop0 = force_reg (bestmode, xop0);
1029 /* XBITPOS counts within UNIT, which is what is expected. */
1031 else
1032 /* Get ref to first byte containing part of the field. */
1033 xop0 = change_address (xop0, byte_mode,
1034 plus_constant (XEXP (xop0, 0), xoffset));
1036 volatile_ok = save_volatile_ok;
1039 /* If op0 is a register, we need it in MAXMODE (which is usually
1040 SImode). to make it acceptable to the format of extzv. */
1041 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1042 abort ();
1043 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
1044 xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
1046 /* On big-endian machines, we count bits from the most significant.
1047 If the bit field insn does not, we must invert. */
1048 #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
1049 xbitpos = unit - bitsize - xbitpos;
1050 #endif
1051 /* Now convert from counting within UNIT to counting in MAXMODE. */
1052 #if BITS_BIG_ENDIAN
1053 if (GET_CODE (xop0) != MEM)
1054 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
1055 #endif
1056 unit = GET_MODE_BITSIZE (maxmode);
1058 if (xtarget == 0
1059 || (flag_force_mem && GET_CODE (xtarget) == MEM))
1060 xtarget = xspec_target = gen_reg_rtx (tmode);
1062 if (GET_MODE (xtarget) != maxmode)
1064 if (GET_CODE (xtarget) == REG)
1066 int wider = (GET_MODE_SIZE (maxmode)
1067 > GET_MODE_SIZE (GET_MODE (xtarget)));
1068 xtarget = gen_lowpart (maxmode, xtarget);
1069 if (wider)
1070 xspec_target_subreg = xtarget;
1072 else
1073 xtarget = gen_reg_rtx (maxmode);
1076 /* If this machine's extzv insists on a register target,
1077 make sure we have one. */
1078 if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
1079 (xtarget, maxmode)))
1080 xtarget = gen_reg_rtx (maxmode);
1082 bitsize_rtx = GEN_INT (bitsize);
1083 bitpos_rtx = GEN_INT (xbitpos);
1085 pat = gen_extzv (protect_from_queue (xtarget, 1),
1086 xop0, bitsize_rtx, bitpos_rtx);
1087 if (pat)
1089 emit_insn (pat);
1090 target = xtarget;
1091 spec_target = xspec_target;
1092 spec_target_subreg = xspec_target_subreg;
1094 else
1096 delete_insns_since (last);
1097 target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
1098 bitpos, target, 1, align);
1101 else
1102 extzv_loses:
1103 #endif
1104 target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1105 target, 1, align);
1107 else
1109 #ifdef HAVE_extv
1110 if (HAVE_extv
1111 && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extv][0])
1112 >= bitsize))
1114 int xbitpos = bitpos, xoffset = offset;
1115 rtx bitsize_rtx, bitpos_rtx;
1116 rtx last = get_last_insn();
1117 rtx xop0 = op0, xtarget = target;
1118 rtx xspec_target = spec_target;
1119 rtx xspec_target_subreg = spec_target_subreg;
1120 rtx pat;
1121 enum machine_mode maxmode
1122 = insn_operand_mode[(int) CODE_FOR_extv][0];
1124 if (GET_CODE (xop0) == MEM)
1126 /* Is the memory operand acceptable? */
1127 if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][1])
1128 (xop0, GET_MODE (xop0))))
1130 /* No, load into a reg and extract from there. */
1131 enum machine_mode bestmode;
1133 /* Get the mode to use for inserting into this field. If
1134 OP0 is BLKmode, get the smallest mode consistent with the
1135 alignment. If OP0 is a non-BLKmode object that is no
1136 wider than MAXMODE, use its mode. Otherwise, use the
1137 smallest mode containing the field. */
1139 if (GET_MODE (xop0) == BLKmode
1140 || (GET_MODE_SIZE (GET_MODE (op0))
1141 > GET_MODE_SIZE (maxmode)))
1142 bestmode = get_best_mode (bitsize, bitnum,
1143 align * BITS_PER_UNIT, maxmode,
1144 MEM_VOLATILE_P (xop0));
1145 else
1146 bestmode = GET_MODE (xop0);
1148 if (bestmode == VOIDmode
1149 || (STRICT_ALIGNMENT && GET_MODE_SIZE (bestmode) > align))
1150 goto extv_loses;
1152 /* Compute offset as multiple of this unit,
1153 counting in bytes. */
1154 unit = GET_MODE_BITSIZE (bestmode);
1155 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1156 xbitpos = bitnum % unit;
1157 xop0 = change_address (xop0, bestmode,
1158 plus_constant (XEXP (xop0, 0),
1159 xoffset));
1160 /* Fetch it to a register in that size. */
1161 xop0 = force_reg (bestmode, xop0);
1163 /* XBITPOS counts within UNIT, which is what is expected. */
1165 else
1166 /* Get ref to first byte containing part of the field. */
1167 xop0 = change_address (xop0, byte_mode,
1168 plus_constant (XEXP (xop0, 0), xoffset));
1171 /* If op0 is a register, we need it in MAXMODE (which is usually
1172 SImode) to make it acceptable to the format of extv. */
1173 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1174 abort ();
1175 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
1176 xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
1178 /* On big-endian machines, we count bits from the most significant.
1179 If the bit field insn does not, we must invert. */
1180 #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
1181 xbitpos = unit - bitsize - xbitpos;
1182 #endif
1183 /* XBITPOS counts within a size of UNIT.
1184 Adjust to count within a size of MAXMODE. */
1185 #if BITS_BIG_ENDIAN
1186 if (GET_CODE (xop0) != MEM)
1187 xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
1188 #endif
1189 unit = GET_MODE_BITSIZE (maxmode);
1191 if (xtarget == 0
1192 || (flag_force_mem && GET_CODE (xtarget) == MEM))
1193 xtarget = xspec_target = gen_reg_rtx (tmode);
1195 if (GET_MODE (xtarget) != maxmode)
1197 if (GET_CODE (xtarget) == REG)
1199 int wider = (GET_MODE_SIZE (maxmode)
1200 > GET_MODE_SIZE (GET_MODE (xtarget)));
1201 xtarget = gen_lowpart (maxmode, xtarget);
1202 if (wider)
1203 xspec_target_subreg = xtarget;
1205 else
1206 xtarget = gen_reg_rtx (maxmode);
1209 /* If this machine's extv insists on a register target,
1210 make sure we have one. */
1211 if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][0])
1212 (xtarget, maxmode)))
1213 xtarget = gen_reg_rtx (maxmode);
1215 bitsize_rtx = GEN_INT (bitsize);
1216 bitpos_rtx = GEN_INT (xbitpos);
1218 pat = gen_extv (protect_from_queue (xtarget, 1),
1219 xop0, bitsize_rtx, bitpos_rtx);
1220 if (pat)
1222 emit_insn (pat);
1223 target = xtarget;
1224 spec_target = xspec_target;
1225 spec_target_subreg = xspec_target_subreg;
1227 else
1229 delete_insns_since (last);
1230 target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
1231 bitpos, target, 0, align);
1234 else
1235 extv_loses:
1236 #endif
1237 target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1238 target, 0, align);
1240 if (target == spec_target)
1241 return target;
1242 if (target == spec_target_subreg)
1243 return spec_target;
1244 if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
1246 /* If the target mode is floating-point, first convert to the
1247 integer mode of that size and then access it as a floating-point
1248 value via a SUBREG. */
1249 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1251 target = convert_to_mode (mode_for_size (GET_MODE_BITSIZE (tmode),
1252 MODE_INT, 0),
1253 target, unsignedp);
1254 if (GET_CODE (target) != REG)
1255 target = copy_to_reg (target);
1256 return gen_rtx (SUBREG, tmode, target, 0);
1258 else
1259 return convert_to_mode (tmode, target, unsignedp);
1261 return target;
1264 /* Extract a bit field using shifts and boolean operations
1265 Returns an rtx to represent the value.
1266 OP0 addresses a register (word) or memory (byte).
1267 BITPOS says which bit within the word or byte the bit field starts in.
1268 OFFSET says how many bytes farther the bit field starts;
1269 it is 0 if OP0 is a register.
1270 BITSIZE says how many bits long the bit field is.
1271 (If OP0 is a register, it may be narrower than a full word,
1272 but BITPOS still counts within a full word,
1273 which is significant on bigendian machines.)
1275 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1276 If TARGET is nonzero, attempts to store the value there
1277 and return TARGET, but this is not guaranteed.
1278 If TARGET is not used, create a pseudo-reg of mode TMODE for the value.
1280 ALIGN is the alignment that STR_RTX is known to have, measured in bytes. */
1282 static rtx
1283 extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1284 target, unsignedp, align)
1285 enum machine_mode tmode;
1286 register rtx op0, target;
1287 register int offset, bitsize, bitpos;
1288 int unsignedp;
1289 int align;
1291 int total_bits = BITS_PER_WORD;
1292 enum machine_mode mode;
1294 if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
1296 /* Special treatment for a bit field split across two registers. */
1297 if (bitsize + bitpos > BITS_PER_WORD)
1298 return extract_split_bit_field (op0, bitsize, bitpos,
1299 unsignedp, align);
1301 else
1303 /* Get the proper mode to use for this field. We want a mode that
1304 includes the entire field. If such a mode would be larger than
1305 a word, we won't be doing the extraction the normal way. */
1307 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1308 align * BITS_PER_UNIT, word_mode,
1309 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
1311 if (mode == VOIDmode)
1312 /* The only way this should occur is if the field spans word
1313 boundaries. */
1314 return extract_split_bit_field (op0, bitsize,
1315 bitpos + offset * BITS_PER_UNIT,
1316 unsignedp, align);
1318 total_bits = GET_MODE_BITSIZE (mode);
1320 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
1321 be be in the range 0 to total_bits-1, and put any excess bytes in
1322 OFFSET. */
1323 if (bitpos >= total_bits)
1325 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
1326 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
1327 * BITS_PER_UNIT);
1330 /* Get ref to an aligned byte, halfword, or word containing the field.
1331 Adjust BITPOS to be position within a word,
1332 and OFFSET to be the offset of that word.
1333 Then alter OP0 to refer to that word. */
1334 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1335 offset -= (offset % (total_bits / BITS_PER_UNIT));
1336 op0 = change_address (op0, mode,
1337 plus_constant (XEXP (op0, 0), offset));
1340 mode = GET_MODE (op0);
1342 #if BYTES_BIG_ENDIAN
1343 /* BITPOS is the distance between our msb and that of OP0.
1344 Convert it to the distance from the lsb. */
1346 bitpos = total_bits - bitsize - bitpos;
1347 #endif
1348 /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1349 We have reduced the big-endian case to the little-endian case. */
1351 if (unsignedp)
1353 if (bitpos)
1355 /* If the field does not already start at the lsb,
1356 shift it so it does. */
1357 tree amount = build_int_2 (bitpos, 0);
1358 /* Maybe propagate the target for the shift. */
1359 /* But not if we will return it--could confuse integrate.c. */
1360 rtx subtarget = (target != 0 && GET_CODE (target) == REG
1361 && !REG_FUNCTION_VALUE_P (target)
1362 ? target : 0);
1363 if (tmode != mode) subtarget = 0;
1364 op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1366 /* Convert the value to the desired mode. */
1367 if (mode != tmode)
1368 op0 = convert_to_mode (tmode, op0, 1);
1370 /* Unless the msb of the field used to be the msb when we shifted,
1371 mask out the upper bits. */
1373 if (GET_MODE_BITSIZE (mode) != bitpos + bitsize
1374 #if 0
1375 #ifdef SLOW_ZERO_EXTEND
1376 /* Always generate an `and' if
1377 we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
1378 will combine fruitfully with the zero-extend. */
1379 || tmode != mode
1380 #endif
1381 #endif
1383 return expand_binop (GET_MODE (op0), and_optab, op0,
1384 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1385 target, 1, OPTAB_LIB_WIDEN);
1386 return op0;
1389 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1390 then arithmetic-shift its lsb to the lsb of the word. */
1391 op0 = force_reg (mode, op0);
1392 if (mode != tmode)
1393 target = 0;
1395 /* Find the narrowest integer mode that contains the field. */
1397 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1398 mode = GET_MODE_WIDER_MODE (mode))
1399 if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1401 op0 = convert_to_mode (mode, op0, 0);
1402 break;
1405 if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1407 tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
1408 /* Maybe propagate the target for the shift. */
1409 /* But not if we will return the result--could confuse integrate.c. */
1410 rtx subtarget = (target != 0 && GET_CODE (target) == REG
1411 && ! REG_FUNCTION_VALUE_P (target)
1412 ? target : 0);
1413 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1416 return expand_shift (RSHIFT_EXPR, mode, op0,
1417 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1418 target, 0);
1421 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1422 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1423 complement of that if COMPLEMENT. The mask is truncated if
1424 necessary to the width of mode MODE. */
1426 static rtx
1427 mask_rtx (mode, bitpos, bitsize, complement)
1428 enum machine_mode mode;
1429 int bitpos, bitsize, complement;
1431 HOST_WIDE_INT masklow, maskhigh;
1433 if (bitpos < HOST_BITS_PER_WIDE_INT)
1434 masklow = (HOST_WIDE_INT) -1 << bitpos;
1435 else
1436 masklow = 0;
1438 if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1439 masklow &= ((unsigned HOST_WIDE_INT) -1
1440 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1442 if (bitpos <= HOST_BITS_PER_WIDE_INT)
1443 maskhigh = -1;
1444 else
1445 maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1447 if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
1448 maskhigh &= ((unsigned HOST_WIDE_INT) -1
1449 >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1450 else
1451 maskhigh = 0;
1453 if (complement)
1455 maskhigh = ~maskhigh;
1456 masklow = ~masklow;
1459 return immed_double_const (masklow, maskhigh, mode);
1462 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1463 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1465 static rtx
1466 lshift_value (mode, value, bitpos, bitsize)
1467 enum machine_mode mode;
1468 rtx value;
1469 int bitpos, bitsize;
1471 unsigned HOST_WIDE_INT v = INTVAL (value);
1472 HOST_WIDE_INT low, high;
1474 if (bitsize < HOST_BITS_PER_WIDE_INT)
1475 v &= ~((HOST_WIDE_INT) -1 << bitsize);
1477 if (bitpos < HOST_BITS_PER_WIDE_INT)
1479 low = v << bitpos;
1480 high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
1482 else
1484 low = 0;
1485 high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
1488 return immed_double_const (low, high, mode);
1491 /* Extract a bit field that is split across two words
1492 and return an RTX for the result.
1494 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1495 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1496 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.
1498 ALIGN is the known alignment of OP0, measured in bytes.
1499 This is also the size of the memory objects to be used. */
1501 static rtx
1502 extract_split_bit_field (op0, bitsize, bitpos, unsignedp, align)
1503 rtx op0;
1504 int bitsize, bitpos, unsignedp, align;
1506 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1507 much at a time. */
1508 int unit = MIN (align * BITS_PER_UNIT, BITS_PER_WORD);
1509 int bitsdone = 0;
1510 rtx result;
1511 int first = 1;
1513 while (bitsdone < bitsize)
1515 int thissize;
1516 rtx part, word;
1517 int thispos;
1518 int offset;
1520 offset = (bitpos + bitsdone) / unit;
1521 thispos = (bitpos + bitsdone) % unit;
1523 /* THISSIZE must not overrun a word boundary. Otherwise,
1524 extract_fixed_bit_field will call us again, and we will mutually
1525 recurse forever. */
1526 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1527 thissize = MIN (thissize, unit - thispos);
1529 /* If OP0 is a register, then handle OFFSET here.
1531 When handling multiword bitfields, extract_bit_field may pass
1532 down a word_mode SUBREG of a larger REG for a bitfield that actually
1533 crosses a word boundary. Thus, for a SUBREG, we must find
1534 the current word starting from the base register. */
1535 if (GET_CODE (op0) == SUBREG)
1537 word = operand_subword_force (SUBREG_REG (op0),
1538 SUBREG_WORD (op0) + offset,
1539 GET_MODE (SUBREG_REG (op0)));
1540 offset = 0;
1542 else if (GET_CODE (op0) == REG)
1544 word = operand_subword_force (op0, offset, GET_MODE (op0));
1545 offset = 0;
1547 else
1548 word = op0;
1550 if (word == 0)
1551 abort ();
1553 /* Extract the parts in bit-counting order,
1554 whose meaning is determined by BYTES_PER_UNIT.
1555 OFFSET is in UNITs, and UNIT is in bits.
1556 extract_fixed_bit_field wants offset in bytes. */
1557 part = extract_fixed_bit_field (word_mode, word,
1558 offset * unit / BITS_PER_UNIT,
1559 thissize, thispos, 0, 1, align);
1560 bitsdone += thissize;
1562 /* Shift this part into place for the result. */
1563 #if BYTES_BIG_ENDIAN
1564 if (bitsize != bitsdone)
1565 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1566 build_int_2 (bitsize - bitsdone, 0), 0, 1);
1567 #else
1568 if (bitsdone != thissize)
1569 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1570 build_int_2 (bitsdone - thissize, 0), 0, 1);
1571 #endif
1573 if (first)
1574 result = part;
1575 else
1576 /* Combine the parts with bitwise or. This works
1577 because we extracted each part as an unsigned bit field. */
1578 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
1579 OPTAB_LIB_WIDEN);
1581 first = 0;
1584 /* Unsigned bit field: we are done. */
1585 if (unsignedp)
1586 return result;
1587 /* Signed bit field: sign-extend with two arithmetic shifts. */
1588 result = expand_shift (LSHIFT_EXPR, word_mode, result,
1589 build_int_2 (BITS_PER_WORD - bitsize, 0),
1590 NULL_RTX, 0);
1591 return expand_shift (RSHIFT_EXPR, word_mode, result,
1592 build_int_2 (BITS_PER_WORD - bitsize, 0), NULL_RTX, 0);
1595 /* Add INC into TARGET. */
1597 void
1598 expand_inc (target, inc)
1599 rtx target, inc;
1601 rtx value = expand_binop (GET_MODE (target), add_optab,
1602 target, inc,
1603 target, 0, OPTAB_LIB_WIDEN);
1604 if (value != target)
1605 emit_move_insn (target, value);
1608 /* Subtract DEC from TARGET. */
1610 void
1611 expand_dec (target, dec)
1612 rtx target, dec;
1614 rtx value = expand_binop (GET_MODE (target), sub_optab,
1615 target, dec,
1616 target, 0, OPTAB_LIB_WIDEN);
1617 if (value != target)
1618 emit_move_insn (target, value);
1621 /* Output a shift instruction for expression code CODE,
1622 with SHIFTED being the rtx for the value to shift,
1623 and AMOUNT the tree for the amount to shift by.
1624 Store the result in the rtx TARGET, if that is convenient.
1625 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
1626 Return the rtx for where the value is. */
1629 expand_shift (code, mode, shifted, amount, target, unsignedp)
1630 enum tree_code code;
1631 register enum machine_mode mode;
1632 rtx shifted;
1633 tree amount;
1634 register rtx target;
1635 int unsignedp;
1637 register rtx op1, temp = 0;
1638 register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
1639 register int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
1640 int try;
1642 /* Previously detected shift-counts computed by NEGATE_EXPR
1643 and shifted in the other direction; but that does not work
1644 on all machines. */
1646 op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
1648 #if SHIFT_COUNT_TRUNCATED
1649 if (SHIFT_COUNT_TRUNCATED
1650 && GET_CODE (op1) == CONST_INT
1651 && (unsigned HOST_WIDE_INT) INTVAL (op1) >= GET_MODE_BITSIZE (mode))
1652 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
1653 % GET_MODE_BITSIZE (mode));
1654 #endif
1656 if (op1 == const0_rtx)
1657 return shifted;
1659 for (try = 0; temp == 0 && try < 3; try++)
1661 enum optab_methods methods;
1663 if (try == 0)
1664 methods = OPTAB_DIRECT;
1665 else if (try == 1)
1666 methods = OPTAB_WIDEN;
1667 else
1668 methods = OPTAB_LIB_WIDEN;
1670 if (rotate)
1672 /* Widening does not work for rotation. */
1673 if (methods == OPTAB_WIDEN)
1674 continue;
1675 else if (methods == OPTAB_LIB_WIDEN)
1677 /* If we are rotating by a constant that is valid and
1678 we have been unable to open-code this by a rotation,
1679 do it as the IOR of two shifts. I.e., to rotate A
1680 by N bits, compute (A << N) | ((unsigned) A >> (C - N))
1681 where C is the bitsize of A.
1683 It is theoretically possible that the target machine might
1684 not be able to perform either shift and hence we would
1685 be making two libcalls rather than just the one for the
1686 shift (similarly if IOR could not be done). We will allow
1687 this extremely unlikely lossage to avoid complicating the
1688 code below. */
1690 if (GET_CODE (op1) == CONST_INT && INTVAL (op1) > 0
1691 && INTVAL (op1) < GET_MODE_BITSIZE (mode))
1693 rtx subtarget = target == shifted ? 0 : target;
1694 rtx temp1;
1695 tree other_amount
1696 = build_int_2 (GET_MODE_BITSIZE (mode) - INTVAL (op1), 0);
1698 shifted = force_reg (mode, shifted);
1700 temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
1701 mode, shifted, amount, subtarget, 1);
1702 temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
1703 mode, shifted, other_amount, 0, 1);
1704 return expand_binop (mode, ior_optab, temp, temp1, target,
1705 unsignedp, methods);
1707 else
1708 methods = OPTAB_LIB;
1711 temp = expand_binop (mode,
1712 left ? rotl_optab : rotr_optab,
1713 shifted, op1, target, unsignedp, methods);
1715 /* If we don't have the rotate, but we are rotating by a constant
1716 that is in range, try a rotate in the opposite direction. */
1718 if (temp == 0 && GET_CODE (op1) == CONST_INT
1719 && INTVAL (op1) > 0 && INTVAL (op1) < GET_MODE_BITSIZE (mode))
1720 temp = expand_binop (mode,
1721 left ? rotr_optab : rotl_optab,
1722 shifted,
1723 GEN_INT (GET_MODE_BITSIZE (mode)
1724 - INTVAL (op1)),
1725 target, unsignedp, methods);
1727 else if (unsignedp)
1728 temp = expand_binop (mode,
1729 left ? ashl_optab : lshr_optab,
1730 shifted, op1, target, unsignedp, methods);
1732 /* Do arithmetic shifts.
1733 Also, if we are going to widen the operand, we can just as well
1734 use an arithmetic right-shift instead of a logical one. */
1735 if (temp == 0 && ! rotate
1736 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
1738 enum optab_methods methods1 = methods;
1740 /* If trying to widen a log shift to an arithmetic shift,
1741 don't accept an arithmetic shift of the same size. */
1742 if (unsignedp)
1743 methods1 = OPTAB_MUST_WIDEN;
1745 /* Arithmetic shift */
1747 temp = expand_binop (mode,
1748 left ? ashl_optab : ashr_optab,
1749 shifted, op1, target, unsignedp, methods1);
1752 /* We used to try extzv here for logical right shifts, but that was
1753 only useful for one machine, the VAX, and caused poor code
1754 generation there for lshrdi3, so the code was deleted and a
1755 define_expand for lshrsi3 was added to vax.md. */
1758 if (temp == 0)
1759 abort ();
1760 return temp;
1763 enum alg_code { alg_zero, alg_m, alg_shift,
1764 alg_add_t_m2, alg_sub_t_m2,
1765 alg_add_factor, alg_sub_factor,
1766 alg_add_t2_m, alg_sub_t2_m,
1767 alg_add, alg_subtract, alg_factor, alg_shiftop };
1769 /* This structure records a sequence of operations.
1770 `ops' is the number of operations recorded.
1771 `cost' is their total cost.
1772 The operations are stored in `op' and the corresponding
1773 logarithms of the integer coefficients in `log'.
1775 These are the operations:
1776 alg_zero total := 0;
1777 alg_m total := multiplicand;
1778 alg_shift total := total * coeff
1779 alg_add_t_m2 total := total + multiplicand * coeff;
1780 alg_sub_t_m2 total := total - multiplicand * coeff;
1781 alg_add_factor total := total * coeff + total;
1782 alg_sub_factor total := total * coeff - total;
1783 alg_add_t2_m total := total * coeff + multiplicand;
1784 alg_sub_t2_m total := total * coeff - multiplicand;
1786 The first operand must be either alg_zero or alg_m. */
1788 struct algorithm
1790 short cost;
1791 short ops;
1792 /* The size of the OP and LOG fields are not directly related to the
1793 word size, but the worst-case algorithms will be if we have few
1794 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
1795 In that case we will generate shift-by-2, add, shift-by-2, add,...,
1796 in total wordsize operations. */
1797 enum alg_code op[MAX_BITS_PER_WORD];
1798 char log[MAX_BITS_PER_WORD];
1801 /* Compute and return the best algorithm for multiplying by T.
1802 The algorithm must cost less than cost_limit
1803 If retval.cost >= COST_LIMIT, no algorithm was found and all
1804 other field of the returned struct are undefined. */
1806 static void
1807 synth_mult (alg_out, t, cost_limit)
1808 struct algorithm *alg_out;
1809 unsigned HOST_WIDE_INT t;
1810 int cost_limit;
1812 int m;
1813 struct algorithm *alg_in, *best_alg;
1814 unsigned int cost;
1815 unsigned HOST_WIDE_INT q;
1817 /* Indicate that no algorithm is yet found. If no algorithm
1818 is found, this value will be returned and indicate failure. */
1819 alg_out->cost = cost_limit;
1821 if (cost_limit <= 0)
1822 return;
1824 /* t == 1 can be done in zero cost. */
1825 if (t == 1)
1827 alg_out->ops = 1;
1828 alg_out->cost = 0;
1829 alg_out->op[0] = alg_m;
1830 return;
1833 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
1834 fail now. */
1835 if (t == 0)
1837 if (zero_cost >= cost_limit)
1838 return;
1839 else
1841 alg_out->ops = 1;
1842 alg_out->cost = zero_cost;
1843 alg_out->op[0] = alg_zero;
1844 return;
1848 /* We'll be needing a couple extra algorithm structures now. */
1850 alg_in = (struct algorithm *)alloca (sizeof (struct algorithm));
1851 best_alg = (struct algorithm *)alloca (sizeof (struct algorithm));
1853 /* If we have a group of zero bits at the low-order part of T, try
1854 multiplying by the remaining bits and then doing a shift. */
1856 if ((t & 1) == 0)
1858 m = floor_log2 (t & -t); /* m = number of low zero bits */
1859 q = t >> m;
1860 cost = shift_cost[m];
1861 synth_mult (alg_in, q, cost_limit - cost);
1863 cost += alg_in->cost;
1864 if (cost < cost_limit)
1866 struct algorithm *x;
1867 x = alg_in, alg_in = best_alg, best_alg = x;
1868 best_alg->log[best_alg->ops] = m;
1869 best_alg->op[best_alg->ops] = alg_shift;
1870 cost_limit = cost;
1874 /* If we have an odd number, add or subtract one. */
1875 if ((t & 1) != 0)
1877 unsigned HOST_WIDE_INT w;
1879 for (w = 1; (w & t) != 0; w <<= 1)
1881 if (w > 2
1882 /* Reject the case where t is 3.
1883 Thus we prefer addition in that case. */
1884 && t != 3)
1886 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
1888 cost = add_cost;
1889 synth_mult (alg_in, t + 1, cost_limit - cost);
1891 cost += alg_in->cost;
1892 if (cost < cost_limit)
1894 struct algorithm *x;
1895 x = alg_in, alg_in = best_alg, best_alg = x;
1896 best_alg->log[best_alg->ops] = 0;
1897 best_alg->op[best_alg->ops] = alg_sub_t_m2;
1898 cost_limit = cost;
1901 else
1903 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
1905 cost = add_cost;
1906 synth_mult (alg_in, t - 1, cost_limit - cost);
1908 cost += alg_in->cost;
1909 if (cost < cost_limit)
1911 struct algorithm *x;
1912 x = alg_in, alg_in = best_alg, best_alg = x;
1913 best_alg->log[best_alg->ops] = 0;
1914 best_alg->op[best_alg->ops] = alg_add_t_m2;
1915 cost_limit = cost;
1920 /* Look for factors of t of the form
1921 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
1922 If we find such a factor, we can multiply by t using an algorithm that
1923 multiplies by q, shift the result by m and add/subtract it to itself.
1925 We search for large factors first and loop down, even if large factors
1926 are less probable than small; if we find a large factor we will find a
1927 good sequence quickly, and therefore be able to prune (by decreasing
1928 COST_LIMIT) the search. */
1930 for (m = floor_log2 (t - 1); m >= 2; m--)
1932 unsigned HOST_WIDE_INT d;
1934 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
1935 if (t % d == 0 && t > d)
1937 cost = MIN (shiftadd_cost[m], add_cost + shift_cost[m]);
1938 synth_mult (alg_in, t / d, cost_limit - cost);
1940 cost += alg_in->cost;
1941 if (cost < cost_limit)
1943 struct algorithm *x;
1944 x = alg_in, alg_in = best_alg, best_alg = x;
1945 best_alg->log[best_alg->ops] = m;
1946 best_alg->op[best_alg->ops] = alg_add_factor;
1947 cost_limit = cost;
1949 /* Other factors will have been taken care of in the recursion. */
1950 break;
1953 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
1954 if (t % d == 0 && t > d)
1956 cost = MIN (shiftsub_cost[m], add_cost + shift_cost[m]);
1957 synth_mult (alg_in, t / d, cost_limit - cost);
1959 cost += alg_in->cost;
1960 if (cost < cost_limit)
1962 struct algorithm *x;
1963 x = alg_in, alg_in = best_alg, best_alg = x;
1964 best_alg->log[best_alg->ops] = m;
1965 best_alg->op[best_alg->ops] = alg_sub_factor;
1966 cost_limit = cost;
1968 break;
1972 /* Try shift-and-add (load effective address) instructions,
1973 i.e. do a*3, a*5, a*9. */
1974 if ((t & 1) != 0)
1976 q = t - 1;
1977 q = q & -q;
1978 m = exact_log2 (q);
1979 if (m >= 0)
1981 cost = shiftadd_cost[m];
1982 synth_mult (alg_in, (t - 1) >> m, cost_limit - cost);
1984 cost += alg_in->cost;
1985 if (cost < cost_limit)
1987 struct algorithm *x;
1988 x = alg_in, alg_in = best_alg, best_alg = x;
1989 best_alg->log[best_alg->ops] = m;
1990 best_alg->op[best_alg->ops] = alg_add_t2_m;
1991 cost_limit = cost;
1995 q = t + 1;
1996 q = q & -q;
1997 m = exact_log2 (q);
1998 if (m >= 0)
2000 cost = shiftsub_cost[m];
2001 synth_mult (alg_in, (t + 1) >> m, cost_limit - cost);
2003 cost += alg_in->cost;
2004 if (cost < cost_limit)
2006 struct algorithm *x;
2007 x = alg_in, alg_in = best_alg, best_alg = x;
2008 best_alg->log[best_alg->ops] = m;
2009 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2010 cost_limit = cost;
2015 /* If cost_limit has not decreased since we stored it in alg_out->cost,
2016 we have not found any algorithm. */
2017 if (cost_limit == alg_out->cost)
2018 return;
2020 /* If we are getting a too long sequence for `struct algorithm'
2021 to record, make this search fail. */
2022 if (best_alg->ops == MAX_BITS_PER_WORD)
2023 return;
2025 /* Copy the algorithm from temporary space to the space at alg_out.
2026 We avoid using structure assignment because the majority of
2027 best_alg is normally undefined, and this is a critical function. */
2028 alg_out->ops = best_alg->ops + 1;
2029 alg_out->cost = cost_limit;
2030 bcopy ((char *) best_alg->op, (char *) alg_out->op,
2031 alg_out->ops * sizeof *alg_out->op);
2032 bcopy ((char *) best_alg->log, (char *) alg_out->log,
2033 alg_out->ops * sizeof *alg_out->log);
2036 /* Perform a multiplication and return an rtx for the result.
2037 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
2038 TARGET is a suggestion for where to store the result (an rtx).
2040 We check specially for a constant integer as OP1.
2041 If you want this check for OP0 as well, then before calling
2042 you should swap the two operands if OP0 would be constant. */
2045 expand_mult (mode, op0, op1, target, unsignedp)
2046 enum machine_mode mode;
2047 register rtx op0, op1, target;
2048 int unsignedp;
2050 rtx const_op1 = op1;
2052 /* If we are multiplying in DImode, it may still be a win
2053 to try to work with shifts and adds. */
2054 if (GET_CODE (op1) == CONST_DOUBLE
2055 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
2056 && HOST_BITS_PER_INT <= BITS_PER_WORD)
2058 if ((CONST_DOUBLE_HIGH (op1) == 0 && CONST_DOUBLE_LOW (op1) >= 0)
2059 || (CONST_DOUBLE_HIGH (op1) == -1 && CONST_DOUBLE_LOW (op1) < 0))
2060 const_op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2063 /* We used to test optimize here, on the grounds that it's better to
2064 produce a smaller program when -O is not used.
2065 But this causes such a terrible slowdown sometimes
2066 that it seems better to use synth_mult always. */
2068 if (GET_CODE (const_op1) == CONST_INT)
2070 struct algorithm alg;
2071 struct algorithm neg_alg;
2072 int negate = 0;
2073 HOST_WIDE_INT val = INTVAL (op1);
2074 HOST_WIDE_INT val_so_far;
2075 rtx insn;
2076 int mult_cost;
2078 /* Try to do the computation two ways: multiply by the negative of OP1
2079 and then negate, or do the multiplication directly. The latter is
2080 usually faster for positive numbers and the former for negative
2081 numbers, but the opposite can be faster if the original value
2082 has a factor of 2**m +/- 1, while the negated value does not or
2083 vice versa. */
2085 mult_cost = rtx_cost (gen_rtx (MULT, mode, op0, op1), SET);
2086 mult_cost = MIN (12 * add_cost, mult_cost);
2088 synth_mult (&alg, val, mult_cost);
2089 synth_mult (&neg_alg, - val,
2090 (alg.cost < mult_cost ? alg.cost : mult_cost) - negate_cost);
2092 if (neg_alg.cost + negate_cost < alg.cost)
2093 alg = neg_alg, negate = 1;
2095 if (alg.cost < mult_cost)
2097 /* We found something cheaper than a multiply insn. */
2098 int opno;
2099 rtx accum, tem;
2101 op0 = protect_from_queue (op0, 0);
2103 /* Avoid referencing memory over and over.
2104 For speed, but also for correctness when mem is volatile. */
2105 if (GET_CODE (op0) == MEM)
2106 op0 = force_reg (mode, op0);
2108 /* ACCUM starts out either as OP0 or as a zero, depending on
2109 the first operation. */
2111 if (alg.op[0] == alg_zero)
2113 accum = copy_to_mode_reg (mode, const0_rtx);
2114 val_so_far = 0;
2116 else if (alg.op[0] == alg_m)
2118 accum = copy_to_mode_reg (mode, op0);
2119 val_so_far = 1;
2121 else
2122 abort ();
2124 for (opno = 1; opno < alg.ops; opno++)
2126 int log = alg.log[opno];
2127 int preserve = preserve_subexpressions_p ();
2128 rtx shift_subtarget = preserve ? 0 : accum;
2129 rtx add_target = opno == alg.ops - 1 && target != 0 ? target : 0;
2130 rtx accum_target = preserve ? 0 : accum;
2132 switch (alg.op[opno])
2134 case alg_shift:
2135 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2136 build_int_2 (log, 0), NULL_RTX, 0);
2137 val_so_far <<= log;
2138 break;
2140 case alg_add_t_m2:
2141 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2142 build_int_2 (log, 0), NULL_RTX, 0);
2143 accum = force_operand (gen_rtx (PLUS, mode, accum, tem),
2144 add_target ? add_target : accum_target);
2145 val_so_far += (HOST_WIDE_INT) 1 << log;
2146 break;
2148 case alg_sub_t_m2:
2149 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2150 build_int_2 (log, 0), NULL_RTX, 0);
2151 accum = force_operand (gen_rtx (MINUS, mode, accum, tem),
2152 add_target ? add_target : accum_target);
2153 val_so_far -= (HOST_WIDE_INT) 1 << log;
2154 break;
2156 case alg_add_t2_m:
2157 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2158 build_int_2 (log, 0), shift_subtarget,
2160 accum = force_operand (gen_rtx (PLUS, mode, accum, op0),
2161 add_target ? add_target : accum_target);
2162 val_so_far = (val_so_far << log) + 1;
2163 break;
2165 case alg_sub_t2_m:
2166 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2167 build_int_2 (log, 0), shift_subtarget,
2169 accum = force_operand (gen_rtx (MINUS, mode, accum, op0),
2170 add_target ? add_target : accum_target);
2171 val_so_far = (val_so_far << log) - 1;
2172 break;
2174 case alg_add_factor:
2175 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2176 build_int_2 (log, 0), NULL_RTX, 0);
2177 accum = force_operand (gen_rtx (PLUS, mode, accum, tem),
2178 add_target ? add_target : accum_target);
2179 val_so_far += val_so_far << log;
2180 break;
2182 case alg_sub_factor:
2183 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2184 build_int_2 (log, 0), NULL_RTX, 0);
2185 accum = force_operand (gen_rtx (MINUS, mode, tem, accum),
2186 (add_target ? add_target
2187 : preserve ? 0 : tem));
2188 val_so_far = (val_so_far << log) - val_so_far;
2189 break;
2191 default:
2192 abort ();;
2195 /* Write a REG_EQUAL note on the last insn so that we can cse
2196 multiplication sequences. */
2198 insn = get_last_insn ();
2199 REG_NOTES (insn)
2200 = gen_rtx (EXPR_LIST, REG_EQUAL,
2201 gen_rtx (MULT, mode, op0, GEN_INT (val_so_far)),
2202 REG_NOTES (insn));
2205 if (negate)
2207 val_so_far = - val_so_far;
2208 accum = expand_unop (mode, neg_optab, accum, target, 0);
2211 if (val != val_so_far)
2212 abort ();
2214 return accum;
2218 /* This used to use umul_optab if unsigned, but for non-widening multiply
2219 there is no difference between signed and unsigned. */
2220 op0 = expand_binop (mode, smul_optab,
2221 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
2222 if (op0 == 0)
2223 abort ();
2224 return op0;
2227 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
2228 if that is convenient, and returning where the result is.
2229 You may request either the quotient or the remainder as the result;
2230 specify REM_FLAG nonzero to get the remainder.
2232 CODE is the expression code for which kind of division this is;
2233 it controls how rounding is done. MODE is the machine mode to use.
2234 UNSIGNEDP nonzero means do unsigned division. */
2236 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
2237 and then correct it by or'ing in missing high bits
2238 if result of ANDI is nonzero.
2239 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
2240 This could optimize to a bfexts instruction.
2241 But C doesn't use these operations, so their optimizations are
2242 left for later. */
2245 expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
2246 int rem_flag;
2247 enum tree_code code;
2248 enum machine_mode mode;
2249 register rtx op0, op1, target;
2250 int unsignedp;
2252 register rtx result = 0;
2253 enum machine_mode compute_mode;
2254 int log = -1;
2255 int size;
2256 int can_clobber_op0;
2257 int mod_insn_no_good = 0;
2258 rtx adjusted_op0 = op0;
2259 optab optab1, optab2;
2261 /* We shouldn't be called with op1 == const1_rtx, but some of the
2262 code below will malfunction if we are, so check here and handle
2263 the special case if so. */
2264 if (op1 == const1_rtx)
2265 return rem_flag ? const0_rtx : op0;
2267 if (target
2268 /* Don't use the function value register as a target
2269 since we have to read it as well as write it,
2270 and function-inlining gets confused by this. */
2271 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
2272 /* Don't clobber an operand while doing a multi-step calculation. */
2273 || (rem_flag
2274 && (reg_mentioned_p (target, op0)
2275 || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
2276 || reg_mentioned_p (target, op1)
2277 || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM)))
2278 target = 0;
2280 /* See if we are dividing by 2**log, and hence will do it by shifting,
2281 which is really floor-division, or if we will really do a divide,
2282 and we assume that is trunc-division.
2284 We must correct the dividend by adding or subtracting something
2285 based on the divisor, in order to do the kind of rounding specified
2286 by CODE. The correction depends on what kind of rounding is actually
2287 available, and that depends on whether we will shift or divide.
2289 In many of these cases it is possible to perform the operation by a
2290 clever series of logical operations (shifts and/or exclusive-ors).
2291 Although avoiding the jump has the advantage that it extends the basic
2292 block and allows further optimization, the branch-free code is normally
2293 at least one instruction longer in the (most common) case where the
2294 dividend is non-negative. Performance measurements of the two
2295 alternatives show that the branch-free code is slightly faster on the
2296 IBM ROMP but slower on CISC processors (significantly slower on the
2297 VAX). Accordingly, the jump code has been retained when BRANCH_COST
2298 is small.
2300 On machines where the jump code is slower, the cost of a DIV or MOD
2301 operation can be set small (less than twice that of an addition); in
2302 that case, we pretend that we don't have a power of two and perform
2303 a normal division or modulus operation. */
2305 if (GET_CODE (op1) == CONST_INT
2306 && ! ((code == TRUNC_MOD_EXPR || code == TRUNC_DIV_EXPR)
2307 && ! unsignedp
2308 && (rem_flag ? smod_pow2_cheap : sdiv_pow2_cheap)))
2309 log = exact_log2 (INTVAL (op1));
2311 /* Get the mode in which to perform this computation. Normally it will
2312 be MODE, but sometimes we can't do the desired operation in MODE.
2313 If so, pick a wider mode in which we can do the operation. Convert
2314 to that mode at the start to avoid repeated conversions.
2316 First see what operations we need. These depend on the expression
2317 we are evaluating. (We assume that divxx3 insns exist under the
2318 same conditions that modxx3 insns and that these insns don't normally
2319 fail. If these assumptions are not correct, we may generate less
2320 efficient code in some cases.)
2322 Then see if we find a mode in which we can open-code that operation
2323 (either a division, modulus, or shift). Finally, check for the smallest
2324 mode for which we can do the operation with a library call. */
2326 optab1 = (log >= 0 ? (unsignedp ? lshr_optab : ashr_optab)
2327 : (unsignedp ? udiv_optab : sdiv_optab));
2328 optab2 = (log >= 0 ? optab1 : (unsignedp ? udivmod_optab : sdivmod_optab));
2330 for (compute_mode = mode; compute_mode != VOIDmode;
2331 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
2332 if (optab1->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing
2333 || optab2->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing)
2334 break;
2336 if (compute_mode == VOIDmode)
2337 for (compute_mode = mode; compute_mode != VOIDmode;
2338 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
2339 if (optab1->handlers[(int) compute_mode].libfunc
2340 || optab2->handlers[(int) compute_mode].libfunc)
2341 break;
2343 /* If we still couldn't find a mode, use MODE, but we'll probably abort
2344 in expand_binop. */
2345 if (compute_mode == VOIDmode)
2346 compute_mode = mode;
2348 size = GET_MODE_BITSIZE (compute_mode);
2350 /* If OP0 is a register that is used as the target, we can modify
2351 it in place; otherwise, we have to ensure we copy OP0 before
2352 modifying it. */
2353 can_clobber_op0 = (GET_CODE (op0) == REG && op0 == target);
2355 /* Now convert to the best mode to use. Normally show we made a copy of OP0
2356 and hence we can clobber it (we cannot use a SUBREG to widen
2357 something), but check that the conversion wasn't a no-op due to
2358 promotion. */
2359 if (compute_mode != mode)
2361 adjusted_op0 = convert_modes (compute_mode, mode, op0, unsignedp);
2362 can_clobber_op0 = ! (GET_CODE (op0) == SUBREG
2363 && SUBREG_REG (op0) == adjusted_op0);
2364 op0 = adjusted_op0;
2365 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
2368 /* If we are computing the remainder and one of the operands is a volatile
2369 MEM, copy it into a register. */
2371 if (rem_flag && GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0))
2372 adjusted_op0 = op0 = force_reg (compute_mode, op0), can_clobber_op0 = 1;
2373 if (rem_flag && GET_CODE (op1) == MEM && MEM_VOLATILE_P (op1))
2374 op1 = force_reg (compute_mode, op1);
2376 /* If we are computing the remainder, op0 will be needed later to calculate
2377 X - Y * (X / Y), therefore cannot be clobbered. */
2378 if (rem_flag)
2379 can_clobber_op0 = 0;
2381 /* See if we will need to modify ADJUSTED_OP0. Note that this code
2382 must agree with that in the switch below. */
2383 if (((code == TRUNC_MOD_EXPR || code == TRUNC_DIV_EXPR)
2384 && log >= 0 && ! unsignedp)
2385 || ((code == FLOOR_MOD_EXPR || code == FLOOR_DIV_EXPR)
2386 && log < 0 && ! unsignedp)
2387 || code == CEIL_MOD_EXPR || code == CEIL_DIV_EXPR
2388 || code == ROUND_MOD_EXPR || code == ROUND_DIV_EXPR)
2390 /* If we want the remainder, we may need to use OP0, so make sure
2391 it and ADJUSTED_OP0 are in different registers. We force OP0
2392 to a register in case it has any queued subexpressions, because
2393 emit_cmp_insn will call emit_queue.
2395 If we don't want the remainder, we aren't going to use OP0 anymore.
2396 However, if we cannot clobber OP0 (and hence ADJUSTED_OP0), we must
2397 make a copy of it, hopefully to TARGET.
2399 This code is somewhat tricky. Note that if REM_FLAG is nonzero,
2400 CAN_CLOBBER_OP0 will be zero and we know that OP0 cannot
2401 equal TARGET. */
2403 if (rem_flag)
2404 op0 = force_reg (compute_mode, op0);
2406 if (! can_clobber_op0)
2408 if (target && GET_MODE (target) == compute_mode)
2409 adjusted_op0 = target;
2410 else
2411 adjusted_op0 = 0;
2412 adjusted_op0 = copy_to_suggested_reg (op0, adjusted_op0,
2413 compute_mode);
2417 /* Adjust ADJUSTED_OP0 as described above. Unless CAN_CLOBBER_OP0
2418 is now non-zero, OP0 will retain it's original value. */
2420 switch (code)
2422 case TRUNC_MOD_EXPR:
2423 case TRUNC_DIV_EXPR:
2424 if (log >= 0 && ! unsignedp)
2426 /* Here we need to add OP1-1 if OP0 is negative, 0 otherwise.
2427 This can be computed without jumps by arithmetically shifting
2428 OP0 right LOG-1 places and then shifting right logically
2429 SIZE-LOG bits. The resulting value is unconditionally added
2430 to OP0.
2432 If OP0 cannot be modified in place, copy it, possibly to
2433 TARGET. Note that we will have previously only allowed
2434 it to be modified in place if it is a register, so that
2435 after this `if', ADJUSTED_OP0 is known to be a
2436 register. */
2437 if (log == 1 || BRANCH_COST >= 3)
2439 rtx temp;
2441 temp = expand_shift (RSHIFT_EXPR, compute_mode, adjusted_op0,
2442 build_int_2 (log - 1, 0), NULL_RTX, 0);
2444 /* We cannot allow TEMP to be ADJUSTED_OP0 here. */
2445 temp = expand_shift (RSHIFT_EXPR, compute_mode, temp,
2446 build_int_2 (size - log, 0),
2447 temp != adjusted_op0 ? temp : NULL_RTX, 1);
2449 adjusted_op0 = expand_binop (compute_mode, add_optab,
2450 adjusted_op0, temp, adjusted_op0,
2451 0, OPTAB_LIB_WIDEN);
2453 else
2455 rtx label = gen_label_rtx ();
2457 emit_cmp_insn (adjusted_op0, const0_rtx, GE,
2458 NULL_RTX, compute_mode, 0, 0);
2459 emit_jump_insn (gen_bge (label));
2460 expand_inc (adjusted_op0, plus_constant (op1, -1));
2461 emit_label (label);
2463 mod_insn_no_good = 1;
2465 break;
2467 case FLOOR_DIV_EXPR:
2468 case FLOOR_MOD_EXPR:
2469 if (log < 0 && ! unsignedp)
2471 rtx label = gen_label_rtx ();
2473 emit_cmp_insn (adjusted_op0, const0_rtx, GE,
2474 NULL_RTX, compute_mode, 0, 0);
2475 emit_jump_insn (gen_bge (label));
2476 expand_dec (adjusted_op0, op1);
2477 expand_inc (adjusted_op0, const1_rtx);
2478 emit_label (label);
2479 mod_insn_no_good = 1;
2481 break;
2483 case CEIL_DIV_EXPR:
2484 case CEIL_MOD_EXPR:
2485 if (log < 0)
2487 rtx label = 0;
2488 if (! unsignedp)
2490 label = gen_label_rtx ();
2491 emit_cmp_insn (adjusted_op0, const0_rtx, LE,
2492 NULL_RTX, compute_mode, 0, 0);
2493 emit_jump_insn (gen_ble (label));
2495 expand_inc (adjusted_op0, op1);
2496 expand_dec (adjusted_op0, const1_rtx);
2497 if (! unsignedp)
2498 emit_label (label);
2500 else
2501 adjusted_op0 = expand_binop (compute_mode, add_optab,
2502 adjusted_op0, plus_constant (op1, -1),
2503 adjusted_op0, 0, OPTAB_LIB_WIDEN);
2505 mod_insn_no_good = 1;
2506 break;
2508 case ROUND_DIV_EXPR:
2509 case ROUND_MOD_EXPR:
2510 if (log < 0)
2512 op1 = expand_shift (RSHIFT_EXPR, compute_mode, op1,
2513 integer_one_node, NULL_RTX, 0);
2514 if (! unsignedp)
2516 if (BRANCH_COST >= 2)
2518 /* Negate OP1 if OP0 < 0. Do this by computing a temporary
2519 that has all bits equal to the sign bit and exclusive
2520 or-ing it with OP1. */
2521 rtx temp = expand_shift (RSHIFT_EXPR, compute_mode,
2522 adjusted_op0,
2523 build_int_2 (size - 1, 0),
2524 NULL_RTX, 0);
2525 op1 = expand_binop (compute_mode, xor_optab, op1, temp, op1,
2526 unsignedp, OPTAB_LIB_WIDEN);
2528 else
2530 rtx label = gen_label_rtx ();
2531 emit_cmp_insn (adjusted_op0, const0_rtx, GE, NULL_RTX,
2532 compute_mode, 0, 0);
2533 emit_jump_insn (gen_bge (label));
2534 expand_unop (compute_mode, neg_optab, op1, op1, 0);
2535 emit_label (label);
2538 expand_inc (adjusted_op0, op1);
2540 else
2541 expand_inc (adjusted_op0, GEN_INT (((HOST_WIDE_INT) 1 << log) / 2));
2543 mod_insn_no_good = 1;
2544 break;
2547 if (rem_flag && !mod_insn_no_good)
2549 /* Try to produce the remainder directly */
2550 if (log >= 0)
2551 result = expand_binop (compute_mode, and_optab, adjusted_op0,
2552 GEN_INT (((HOST_WIDE_INT) 1 << log) - 1),
2553 target, 1, OPTAB_LIB_WIDEN);
2554 else
2556 /* See if we can do remainder without a library call. */
2557 result = sign_expand_binop (mode, umod_optab, smod_optab,
2558 adjusted_op0, op1, target,
2559 unsignedp, OPTAB_WIDEN);
2560 if (result == 0)
2562 /* No luck there. Can we do remainder and divide at once
2563 without a library call? */
2564 result = gen_reg_rtx (compute_mode);
2565 if (! expand_twoval_binop (unsignedp
2566 ? udivmod_optab : sdivmod_optab,
2567 adjusted_op0, op1,
2568 NULL_RTX, result, unsignedp))
2569 result = 0;
2574 if (result)
2575 return gen_lowpart (mode, result);
2577 /* Produce the quotient. */
2578 if (log >= 0)
2579 result = expand_shift (RSHIFT_EXPR, compute_mode, adjusted_op0,
2580 build_int_2 (log, 0), target, unsignedp);
2581 else if (rem_flag && !mod_insn_no_good)
2582 /* If producing quotient in order to subtract for remainder,
2583 and a remainder subroutine would be ok,
2584 don't use a divide subroutine. */
2585 result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
2586 adjusted_op0, op1, NULL_RTX, unsignedp,
2587 OPTAB_WIDEN);
2588 else
2590 /* Try a quotient insn, but not a library call. */
2591 result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
2592 adjusted_op0, op1,
2593 rem_flag ? NULL_RTX : target,
2594 unsignedp, OPTAB_WIDEN);
2595 if (result == 0)
2597 /* No luck there. Try a quotient-and-remainder insn,
2598 keeping the quotient alone. */
2599 result = gen_reg_rtx (compute_mode);
2600 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
2601 adjusted_op0, op1,
2602 result, NULL_RTX, unsignedp))
2603 result = 0;
2606 /* If still no luck, use a library call. */
2607 if (result == 0)
2608 result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
2609 adjusted_op0, op1,
2610 rem_flag ? NULL_RTX : target,
2611 unsignedp, OPTAB_LIB_WIDEN);
2614 /* If we really want the remainder, get it by subtraction. */
2615 if (rem_flag)
2617 if (result == 0)
2618 /* No divide instruction either. Use library for remainder. */
2619 result = sign_expand_binop (compute_mode, umod_optab, smod_optab,
2620 op0, op1, target,
2621 unsignedp, OPTAB_LIB_WIDEN);
2622 else
2624 /* We divided. Now finish doing X - Y * (X / Y). */
2625 result = expand_mult (compute_mode, result, op1, target, unsignedp);
2626 if (! result) abort ();
2627 result = expand_binop (compute_mode, sub_optab, op0,
2628 result, target, unsignedp, OPTAB_LIB_WIDEN);
2632 if (result == 0)
2633 abort ();
2635 return gen_lowpart (mode, result);
2638 /* Return a tree node with data type TYPE, describing the value of X.
2639 Usually this is an RTL_EXPR, if there is no obvious better choice.
2640 X may be an expression, however we only support those expressions
2641 generated by loop.c. */
2643 tree
2644 make_tree (type, x)
2645 tree type;
2646 rtx x;
2648 tree t;
2650 switch (GET_CODE (x))
2652 case CONST_INT:
2653 t = build_int_2 (INTVAL (x),
2654 TREE_UNSIGNED (type) || INTVAL (x) >= 0 ? 0 : -1);
2655 TREE_TYPE (t) = type;
2656 return t;
2658 case CONST_DOUBLE:
2659 if (GET_MODE (x) == VOIDmode)
2661 t = build_int_2 (CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
2662 TREE_TYPE (t) = type;
2664 else
2666 REAL_VALUE_TYPE d;
2668 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
2669 t = build_real (type, d);
2672 return t;
2674 case PLUS:
2675 return fold (build (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
2676 make_tree (type, XEXP (x, 1))));
2678 case MINUS:
2679 return fold (build (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
2680 make_tree (type, XEXP (x, 1))));
2682 case NEG:
2683 return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
2685 case MULT:
2686 return fold (build (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
2687 make_tree (type, XEXP (x, 1))));
2689 case ASHIFT:
2690 return fold (build (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
2691 make_tree (type, XEXP (x, 1))));
2693 case LSHIFTRT:
2694 return fold (convert (type,
2695 build (RSHIFT_EXPR, unsigned_type (type),
2696 make_tree (unsigned_type (type),
2697 XEXP (x, 0)),
2698 make_tree (type, XEXP (x, 1)))));
2700 case ASHIFTRT:
2701 return fold (convert (type,
2702 build (RSHIFT_EXPR, signed_type (type),
2703 make_tree (signed_type (type), XEXP (x, 0)),
2704 make_tree (type, XEXP (x, 1)))));
2706 case DIV:
2707 if (TREE_CODE (type) != REAL_TYPE)
2708 t = signed_type (type);
2709 else
2710 t = type;
2712 return fold (convert (type,
2713 build (TRUNC_DIV_EXPR, t,
2714 make_tree (t, XEXP (x, 0)),
2715 make_tree (t, XEXP (x, 1)))));
2716 case UDIV:
2717 t = unsigned_type (type);
2718 return fold (convert (type,
2719 build (TRUNC_DIV_EXPR, t,
2720 make_tree (t, XEXP (x, 0)),
2721 make_tree (t, XEXP (x, 1)))));
2722 default:
2723 t = make_node (RTL_EXPR);
2724 TREE_TYPE (t) = type;
2725 RTL_EXPR_RTL (t) = x;
2726 /* There are no insns to be output
2727 when this rtl_expr is used. */
2728 RTL_EXPR_SEQUENCE (t) = 0;
2729 return t;
2733 /* Return an rtx representing the value of X * MULT + ADD.
2734 TARGET is a suggestion for where to store the result (an rtx).
2735 MODE is the machine mode for the computation.
2736 X and MULT must have mode MODE. ADD may have a different mode.
2737 So can X (defaults to same as MODE).
2738 UNSIGNEDP is non-zero to do unsigned multiplication.
2739 This may emit insns. */
2742 expand_mult_add (x, target, mult, add, mode, unsignedp)
2743 rtx x, target, mult, add;
2744 enum machine_mode mode;
2745 int unsignedp;
2747 tree type = type_for_mode (mode, unsignedp);
2748 tree add_type = (GET_MODE (add) == VOIDmode
2749 ? type : type_for_mode (GET_MODE (add), unsignedp));
2750 tree result = fold (build (PLUS_EXPR, type,
2751 fold (build (MULT_EXPR, type,
2752 make_tree (type, x),
2753 make_tree (type, mult))),
2754 make_tree (add_type, add)));
2756 return expand_expr (result, target, VOIDmode, 0);
2759 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
2760 and returning TARGET.
2762 If TARGET is 0, a pseudo-register or constant is returned. */
2765 expand_and (op0, op1, target)
2766 rtx op0, op1, target;
2768 enum machine_mode mode = VOIDmode;
2769 rtx tem;
2771 if (GET_MODE (op0) != VOIDmode)
2772 mode = GET_MODE (op0);
2773 else if (GET_MODE (op1) != VOIDmode)
2774 mode = GET_MODE (op1);
2776 if (mode != VOIDmode)
2777 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
2778 else if (GET_CODE (op0) == CONST_INT && GET_CODE (op1) == CONST_INT)
2779 tem = GEN_INT (INTVAL (op0) & INTVAL (op1));
2780 else
2781 abort ();
2783 if (target == 0)
2784 target = tem;
2785 else if (tem != target)
2786 emit_move_insn (target, tem);
2787 return target;
2790 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
2791 and storing in TARGET. Normally return TARGET.
2792 Return 0 if that cannot be done.
2794 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
2795 it is VOIDmode, they cannot both be CONST_INT.
2797 UNSIGNEDP is for the case where we have to widen the operands
2798 to perform the operation. It says to use zero-extension.
2800 NORMALIZEP is 1 if we should convert the result to be either zero
2801 or one one. Normalize is -1 if we should convert the result to be
2802 either zero or -1. If NORMALIZEP is zero, the result will be left
2803 "raw" out of the scc insn. */
2806 emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep)
2807 rtx target;
2808 enum rtx_code code;
2809 rtx op0, op1;
2810 enum machine_mode mode;
2811 int unsignedp;
2812 int normalizep;
2814 rtx subtarget;
2815 enum insn_code icode;
2816 enum machine_mode compare_mode;
2817 enum machine_mode target_mode = GET_MODE (target);
2818 rtx tem;
2819 rtx last = 0;
2820 rtx pattern, comparison;
2822 if (mode == VOIDmode)
2823 mode = GET_MODE (op0);
2825 /* If one operand is constant, make it the second one. Only do this
2826 if the other operand is not constant as well. */
2828 if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
2829 || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
2831 tem = op0;
2832 op0 = op1;
2833 op1 = tem;
2834 code = swap_condition (code);
2837 /* For some comparisons with 1 and -1, we can convert this to
2838 comparisons with zero. This will often produce more opportunities for
2839 store-flag insns. */
2841 switch (code)
2843 case LT:
2844 if (op1 == const1_rtx)
2845 op1 = const0_rtx, code = LE;
2846 break;
2847 case LE:
2848 if (op1 == constm1_rtx)
2849 op1 = const0_rtx, code = LT;
2850 break;
2851 case GE:
2852 if (op1 == const1_rtx)
2853 op1 = const0_rtx, code = GT;
2854 break;
2855 case GT:
2856 if (op1 == constm1_rtx)
2857 op1 = const0_rtx, code = GE;
2858 break;
2859 case GEU:
2860 if (op1 == const1_rtx)
2861 op1 = const0_rtx, code = NE;
2862 break;
2863 case LTU:
2864 if (op1 == const1_rtx)
2865 op1 = const0_rtx, code = EQ;
2866 break;
2869 /* From now on, we won't change CODE, so set ICODE now. */
2870 icode = setcc_gen_code[(int) code];
2872 /* If this is A < 0 or A >= 0, we can do this by taking the ones
2873 complement of A (for GE) and shifting the sign bit to the low bit. */
2874 if (op1 == const0_rtx && (code == LT || code == GE)
2875 && GET_MODE_CLASS (mode) == MODE_INT
2876 && (normalizep || STORE_FLAG_VALUE == 1
2877 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
2878 && (STORE_FLAG_VALUE
2879 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
2881 subtarget = target;
2883 /* If the result is to be wider than OP0, it is best to convert it
2884 first. If it is to be narrower, it is *incorrect* to convert it
2885 first. */
2886 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
2888 op0 = protect_from_queue (op0, 0);
2889 op0 = convert_modes (target_mode, mode, op0, 0);
2890 mode = target_mode;
2893 if (target_mode != mode)
2894 subtarget = 0;
2896 if (code == GE)
2897 op0 = expand_unop (mode, one_cmpl_optab, op0, subtarget, 0);
2899 if (normalizep || STORE_FLAG_VALUE == 1)
2900 /* If we are supposed to produce a 0/1 value, we want to do
2901 a logical shift from the sign bit to the low-order bit; for
2902 a -1/0 value, we do an arithmetic shift. */
2903 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
2904 size_int (GET_MODE_BITSIZE (mode) - 1),
2905 subtarget, normalizep != -1);
2907 if (mode != target_mode)
2908 op0 = convert_modes (target_mode, mode, op0, 0);
2910 return op0;
2913 if (icode != CODE_FOR_nothing)
2915 /* We think we may be able to do this with a scc insn. Emit the
2916 comparison and then the scc insn.
2918 compare_from_rtx may call emit_queue, which would be deleted below
2919 if the scc insn fails. So call it ourselves before setting LAST. */
2921 emit_queue ();
2922 last = get_last_insn ();
2924 comparison
2925 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX, 0);
2926 if (GET_CODE (comparison) == CONST_INT)
2927 return (comparison == const0_rtx ? const0_rtx
2928 : normalizep == 1 ? const1_rtx
2929 : normalizep == -1 ? constm1_rtx
2930 : const_true_rtx);
2932 /* If the code of COMPARISON doesn't match CODE, something is
2933 wrong; we can no longer be sure that we have the operation.
2934 We could handle this case, but it should not happen. */
2936 if (GET_CODE (comparison) != code)
2937 abort ();
2939 /* Get a reference to the target in the proper mode for this insn. */
2940 compare_mode = insn_operand_mode[(int) icode][0];
2941 subtarget = target;
2942 if (preserve_subexpressions_p ()
2943 || ! (*insn_operand_predicate[(int) icode][0]) (subtarget, compare_mode))
2944 subtarget = gen_reg_rtx (compare_mode);
2946 pattern = GEN_FCN (icode) (subtarget);
2947 if (pattern)
2949 emit_insn (pattern);
2951 /* If we are converting to a wider mode, first convert to
2952 TARGET_MODE, then normalize. This produces better combining
2953 opportunities on machines that have a SIGN_EXTRACT when we are
2954 testing a single bit. This mostly benefits the 68k.
2956 If STORE_FLAG_VALUE does not have the sign bit set when
2957 interpreted in COMPARE_MODE, we can do this conversion as
2958 unsigned, which is usually more efficient. */
2959 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
2961 convert_move (target, subtarget,
2962 (GET_MODE_BITSIZE (compare_mode)
2963 <= HOST_BITS_PER_WIDE_INT)
2964 && 0 == (STORE_FLAG_VALUE
2965 & ((HOST_WIDE_INT) 1
2966 << (GET_MODE_BITSIZE (compare_mode) -1))));
2967 op0 = target;
2968 compare_mode = target_mode;
2970 else
2971 op0 = subtarget;
2973 /* If we want to keep subexpressions around, don't reuse our
2974 last target. */
2976 if (preserve_subexpressions_p ())
2977 subtarget = 0;
2979 /* Now normalize to the proper value in COMPARE_MODE. Sometimes
2980 we don't have to do anything. */
2981 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
2983 else if (normalizep == - STORE_FLAG_VALUE)
2984 op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
2986 /* We don't want to use STORE_FLAG_VALUE < 0 below since this
2987 makes it hard to use a value of just the sign bit due to
2988 ANSI integer constant typing rules. */
2989 else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
2990 && (STORE_FLAG_VALUE
2991 & ((HOST_WIDE_INT) 1
2992 << (GET_MODE_BITSIZE (compare_mode) - 1))))
2993 op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
2994 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
2995 subtarget, normalizep == 1);
2996 else if (STORE_FLAG_VALUE & 1)
2998 op0 = expand_and (op0, const1_rtx, subtarget);
2999 if (normalizep == -1)
3000 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
3002 else
3003 abort ();
3005 /* If we were converting to a smaller mode, do the
3006 conversion now. */
3007 if (target_mode != compare_mode)
3009 convert_move (target, op0, 0);
3010 return target;
3012 else
3013 return op0;
3017 if (last)
3018 delete_insns_since (last);
3020 subtarget = target_mode == mode ? target : 0;
3022 /* If we reached here, we can't do this with a scc insn. However, there
3023 are some comparisons that can be done directly. For example, if
3024 this is an equality comparison of integers, we can try to exclusive-or
3025 (or subtract) the two operands and use a recursive call to try the
3026 comparison with zero. Don't do any of these cases if branches are
3027 very cheap. */
3029 if (BRANCH_COST > 0
3030 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
3031 && op1 != const0_rtx)
3033 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
3034 OPTAB_WIDEN);
3036 if (tem == 0)
3037 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
3038 OPTAB_WIDEN);
3039 if (tem != 0)
3040 tem = emit_store_flag (target, code, tem, const0_rtx,
3041 mode, unsignedp, normalizep);
3042 if (tem == 0)
3043 delete_insns_since (last);
3044 return tem;
3047 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
3048 the constant zero. Reject all other comparisons at this point. Only
3049 do LE and GT if branches are expensive since they are expensive on
3050 2-operand machines. */
3052 if (BRANCH_COST == 0
3053 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
3054 || (code != EQ && code != NE
3055 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
3056 return 0;
3058 /* See what we need to return. We can only return a 1, -1, or the
3059 sign bit. */
3061 if (normalizep == 0)
3063 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
3064 normalizep = STORE_FLAG_VALUE;
3066 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3067 && (STORE_FLAG_VALUE
3068 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
3070 else
3071 return 0;
3074 /* Try to put the result of the comparison in the sign bit. Assume we can't
3075 do the necessary operation below. */
3077 tem = 0;
3079 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
3080 the sign bit set. */
3082 if (code == LE)
3084 /* This is destructive, so SUBTARGET can't be OP0. */
3085 if (rtx_equal_p (subtarget, op0))
3086 subtarget = 0;
3088 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
3089 OPTAB_WIDEN);
3090 if (tem)
3091 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
3092 OPTAB_WIDEN);
3095 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
3096 number of bits in the mode of OP0, minus one. */
3098 if (code == GT)
3100 if (rtx_equal_p (subtarget, op0))
3101 subtarget = 0;
3103 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3104 size_int (GET_MODE_BITSIZE (mode) - 1),
3105 subtarget, 0);
3106 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
3107 OPTAB_WIDEN);
3110 if (code == EQ || code == NE)
3112 /* For EQ or NE, one way to do the comparison is to apply an operation
3113 that converts the operand into a positive number if it is non-zero
3114 or zero if it was originally zero. Then, for EQ, we subtract 1 and
3115 for NE we negate. This puts the result in the sign bit. Then we
3116 normalize with a shift, if needed.
3118 Two operations that can do the above actions are ABS and FFS, so try
3119 them. If that doesn't work, and MODE is smaller than a full word,
3120 we can use zero-extension to the wider mode (an unsigned conversion)
3121 as the operation. */
3123 if (abs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3124 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
3125 else if (ffs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3126 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
3127 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3129 op0 = protect_from_queue (op0, 0);
3130 tem = convert_modes (word_mode, mode, op0, 1);
3131 mode = word_mode;
3134 if (tem != 0)
3136 if (code == EQ)
3137 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
3138 0, OPTAB_WIDEN);
3139 else
3140 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
3143 /* If we couldn't do it that way, for NE we can "or" the two's complement
3144 of the value with itself. For EQ, we take the one's complement of
3145 that "or", which is an extra insn, so we only handle EQ if branches
3146 are expensive. */
3148 if (tem == 0 && (code == NE || BRANCH_COST > 1))
3150 if (rtx_equal_p (subtarget, op0))
3151 subtarget = 0;
3153 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
3154 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
3155 OPTAB_WIDEN);
3157 if (tem && code == EQ)
3158 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
3162 if (tem && normalizep)
3163 tem = expand_shift (RSHIFT_EXPR, mode, tem,
3164 size_int (GET_MODE_BITSIZE (mode) - 1),
3165 tem, normalizep == 1);
3167 if (tem && GET_MODE (tem) != target_mode)
3169 convert_move (target, tem, 0);
3170 tem = target;
3173 if (tem == 0)
3174 delete_insns_since (last);
3176 return tem;
3178 emit_jump_insn ((*bcc_gen_fctn[(int) code]) (label));
3179 emit_move_insn (target, const1_rtx);
3180 emit_label (label);
3182 return target;