Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / expmed.c
blob62fe884ebc9b2ea2a2f401363cf169fec9f66026
1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987, 88, 89, 92-97, 1998 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "insn-flags.h"
29 #include "insn-codes.h"
30 #include "insn-config.h"
31 #include "expr.h"
32 #include "real.h"
33 #include "recog.h"
35 static void store_fixed_bit_field PROTO((rtx, int, int, int, rtx, int));
36 static void store_split_bit_field PROTO((rtx, int, int, rtx, int));
37 static rtx extract_fixed_bit_field PROTO((enum machine_mode, rtx, int,
38 int, int, rtx, int, int));
39 static rtx mask_rtx PROTO((enum machine_mode, int,
40 int, int));
41 static rtx lshift_value PROTO((enum machine_mode, rtx,
42 int, int));
43 static rtx extract_split_bit_field PROTO((rtx, int, int, int, int));
44 static void do_cmp_and_jump PROTO((rtx, rtx, enum rtx_code,
45 enum machine_mode, rtx));
47 #define CEIL(x,y) (((x) + (y) - 1) / (y))
49 /* Non-zero means divides or modulus operations are relatively cheap for
50 powers of two, so don't use branches; emit the operation instead.
51 Usually, this will mean that the MD file will emit non-branch
52 sequences. */
54 static int sdiv_pow2_cheap, smod_pow2_cheap;
56 #ifndef SLOW_UNALIGNED_ACCESS
57 #define SLOW_UNALIGNED_ACCESS STRICT_ALIGNMENT
58 #endif
60 /* For compilers that support multiple targets with different word sizes,
61 MAX_BITS_PER_WORD contains the biggest value of BITS_PER_WORD. An example
62 is the H8/300(H) compiler. */
64 #ifndef MAX_BITS_PER_WORD
65 #define MAX_BITS_PER_WORD BITS_PER_WORD
66 #endif
68 /* Cost of various pieces of RTL. Note that some of these are indexed by
69 shift count and some by mode. */
70 static int add_cost, negate_cost, zero_cost;
71 static int shift_cost[MAX_BITS_PER_WORD];
72 static int shiftadd_cost[MAX_BITS_PER_WORD];
73 static int shiftsub_cost[MAX_BITS_PER_WORD];
74 static int mul_cost[NUM_MACHINE_MODES];
75 static int div_cost[NUM_MACHINE_MODES];
76 static int mul_widen_cost[NUM_MACHINE_MODES];
77 static int mul_highpart_cost[NUM_MACHINE_MODES];
79 void
80 init_expmed ()
82 char *free_point;
83 /* This is "some random pseudo register" for purposes of calling recog
84 to see what insns exist. */
85 rtx reg;
86 rtx shift_insn, shiftadd_insn, shiftsub_insn;
87 int dummy;
88 int m;
89 enum machine_mode mode, wider_mode;
91 start_sequence ();
93 /* Since we are on the permanent obstack, we must be sure we save this
94 spot AFTER we call start_sequence, since it will reuse the rtl it
95 makes. */
96 free_point = (char *) oballoc (0);
98 reg = gen_rtx_REG (word_mode, 10000);
100 zero_cost = rtx_cost (const0_rtx, 0);
101 add_cost = rtx_cost (gen_rtx_PLUS (word_mode, reg, reg), SET);
103 shift_insn = emit_insn (gen_rtx_SET (VOIDmode, reg,
104 gen_rtx_ASHIFT (word_mode, reg,
105 const0_rtx)));
107 shiftadd_insn
108 = emit_insn (gen_rtx_SET (VOIDmode, reg,
109 gen_rtx_PLUS (word_mode,
110 gen_rtx_MULT (word_mode,
111 reg, const0_rtx),
112 reg)));
114 shiftsub_insn
115 = emit_insn (gen_rtx_SET (VOIDmode, reg,
116 gen_rtx_MINUS (word_mode,
117 gen_rtx_MULT (word_mode,
118 reg, const0_rtx),
119 reg)));
121 init_recog ();
123 shift_cost[0] = 0;
124 shiftadd_cost[0] = shiftsub_cost[0] = add_cost;
126 for (m = 1; m < BITS_PER_WORD; m++)
128 shift_cost[m] = shiftadd_cost[m] = shiftsub_cost[m] = 32000;
130 XEXP (SET_SRC (PATTERN (shift_insn)), 1) = GEN_INT (m);
131 if (recog (PATTERN (shift_insn), shift_insn, &dummy) >= 0)
132 shift_cost[m] = rtx_cost (SET_SRC (PATTERN (shift_insn)), SET);
134 XEXP (XEXP (SET_SRC (PATTERN (shiftadd_insn)), 0), 1)
135 = GEN_INT ((HOST_WIDE_INT) 1 << m);
136 if (recog (PATTERN (shiftadd_insn), shiftadd_insn, &dummy) >= 0)
137 shiftadd_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftadd_insn)), SET);
139 XEXP (XEXP (SET_SRC (PATTERN (shiftsub_insn)), 0), 1)
140 = GEN_INT ((HOST_WIDE_INT) 1 << m);
141 if (recog (PATTERN (shiftsub_insn), shiftsub_insn, &dummy) >= 0)
142 shiftsub_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftsub_insn)), SET);
145 negate_cost = rtx_cost (gen_rtx_NEG (word_mode, reg), SET);
147 sdiv_pow2_cheap
148 = (rtx_cost (gen_rtx_DIV (word_mode, reg, GEN_INT (32)), SET)
149 <= 2 * add_cost);
150 smod_pow2_cheap
151 = (rtx_cost (gen_rtx_MOD (word_mode, reg, GEN_INT (32)), SET)
152 <= 2 * add_cost);
154 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
155 mode != VOIDmode;
156 mode = GET_MODE_WIDER_MODE (mode))
158 reg = gen_rtx (REG, mode, 10000);
159 div_cost[(int) mode] = rtx_cost (gen_rtx (UDIV, mode, reg, reg), SET);
160 mul_cost[(int) mode] = rtx_cost (gen_rtx (MULT, mode, reg, reg), SET);
161 wider_mode = GET_MODE_WIDER_MODE (mode);
162 if (wider_mode != VOIDmode)
164 mul_widen_cost[(int) wider_mode]
165 = rtx_cost (gen_rtx_MULT (wider_mode,
166 gen_rtx_ZERO_EXTEND (wider_mode, reg),
167 gen_rtx_ZERO_EXTEND (wider_mode, reg)),
168 SET);
169 mul_highpart_cost[(int) mode]
170 = rtx_cost (gen_rtx_TRUNCATE
171 (mode,
172 gen_rtx_LSHIFTRT (wider_mode,
173 gen_rtx_MULT (wider_mode,
174 gen_rtx_ZERO_EXTEND
175 (wider_mode, reg),
176 gen_rtx_ZERO_EXTEND
177 (wider_mode, reg)),
178 GEN_INT (GET_MODE_BITSIZE (mode)))),
179 SET);
183 /* Free the objects we just allocated. */
184 end_sequence ();
185 obfree (free_point);
188 /* Return an rtx representing minus the value of X.
189 MODE is the intended mode of the result,
190 useful if X is a CONST_INT. */
193 negate_rtx (mode, x)
194 enum machine_mode mode;
195 rtx x;
197 rtx result = simplify_unary_operation (NEG, mode, x, mode);
199 if (result == 0)
200 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
202 return result;
205 /* Generate code to store value from rtx VALUE
206 into a bit-field within structure STR_RTX
207 containing BITSIZE bits starting at bit BITNUM.
208 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
209 ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
210 TOTAL_SIZE is the size of the structure in bytes, or -1 if varying. */
212 /* ??? Note that there are two different ideas here for how
213 to determine the size to count bits within, for a register.
214 One is BITS_PER_WORD, and the other is the size of operand 3
215 of the insv pattern. (The latter assumes that an n-bit machine
216 will be able to insert bit fields up to n bits wide.)
217 It isn't certain that either of these is right.
218 extract_bit_field has the same quandary. */
221 store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, align, total_size)
222 rtx str_rtx;
223 register int bitsize;
224 int bitnum;
225 enum machine_mode fieldmode;
226 rtx value;
227 int align;
228 int total_size;
230 int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
231 register int offset = bitnum / unit;
232 register int bitpos = bitnum % unit;
233 register rtx op0 = str_rtx;
235 if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
236 abort ();
238 /* Discount the part of the structure before the desired byte.
239 We need to know how many bytes are safe to reference after it. */
240 if (total_size >= 0)
241 total_size -= (bitpos / BIGGEST_ALIGNMENT
242 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
244 while (GET_CODE (op0) == SUBREG)
246 /* The following line once was done only if WORDS_BIG_ENDIAN,
247 but I think that is a mistake. WORDS_BIG_ENDIAN is
248 meaningful at a much higher level; when structures are copied
249 between memory and regs, the higher-numbered regs
250 always get higher addresses. */
251 offset += SUBREG_WORD (op0);
252 /* We used to adjust BITPOS here, but now we do the whole adjustment
253 right after the loop. */
254 op0 = SUBREG_REG (op0);
257 /* If OP0 is a register, BITPOS must count within a word.
258 But as we have it, it counts within whatever size OP0 now has.
259 On a bigendian machine, these are not the same, so convert. */
260 if (BYTES_BIG_ENDIAN
261 && GET_CODE (op0) != MEM
262 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
263 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
265 value = protect_from_queue (value, 0);
267 if (flag_force_mem)
268 value = force_not_mem (value);
270 /* Note that the adjustment of BITPOS above has no effect on whether
271 BITPOS is 0 in a REG bigger than a word. */
272 if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
273 && (GET_CODE (op0) != MEM
274 || ! SLOW_UNALIGNED_ACCESS
275 || (offset * BITS_PER_UNIT % bitsize == 0
276 && align % GET_MODE_SIZE (fieldmode) == 0))
277 && bitpos == 0 && bitsize == GET_MODE_BITSIZE (fieldmode))
279 /* Storing in a full-word or multi-word field in a register
280 can be done with just SUBREG. */
281 if (GET_MODE (op0) != fieldmode)
283 if (GET_CODE (op0) == REG)
284 op0 = gen_rtx_SUBREG (fieldmode, op0, offset);
285 else
286 op0 = change_address (op0, fieldmode,
287 plus_constant (XEXP (op0, 0), offset));
289 emit_move_insn (op0, value);
290 return value;
293 /* Storing an lsb-aligned field in a register
294 can be done with a movestrict instruction. */
296 if (GET_CODE (op0) != MEM
297 && (BYTES_BIG_ENDIAN ? bitpos + bitsize == unit : bitpos == 0)
298 && bitsize == GET_MODE_BITSIZE (fieldmode)
299 && (GET_MODE (op0) == fieldmode
300 || (movstrict_optab->handlers[(int) fieldmode].insn_code
301 != CODE_FOR_nothing)))
303 /* Get appropriate low part of the value being stored. */
304 if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
305 value = gen_lowpart (fieldmode, value);
306 else if (!(GET_CODE (value) == SYMBOL_REF
307 || GET_CODE (value) == LABEL_REF
308 || GET_CODE (value) == CONST))
309 value = convert_to_mode (fieldmode, value, 0);
311 if (GET_MODE (op0) == fieldmode)
312 emit_move_insn (op0, value);
313 else
315 int icode = movstrict_optab->handlers[(int) fieldmode].insn_code;
316 if(! (*insn_operand_predicate[icode][1]) (value, fieldmode))
317 value = copy_to_mode_reg (fieldmode, value);
318 emit_insn (GEN_FCN (icode)
319 (gen_rtx_SUBREG (fieldmode, op0, offset), value));
321 return value;
324 /* Handle fields bigger than a word. */
326 if (bitsize > BITS_PER_WORD)
328 /* Here we transfer the words of the field
329 in the order least significant first.
330 This is because the most significant word is the one which may
331 be less than full.
332 However, only do that if the value is not BLKmode. */
334 int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
336 int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
337 int i;
339 /* This is the mode we must force value to, so that there will be enough
340 subwords to extract. Note that fieldmode will often (always?) be
341 VOIDmode, because that is what store_field uses to indicate that this
342 is a bit field, but passing VOIDmode to operand_subword_force will
343 result in an abort. */
344 fieldmode = mode_for_size (nwords * BITS_PER_WORD, MODE_INT, 0);
346 for (i = 0; i < nwords; i++)
348 /* If I is 0, use the low-order word in both field and target;
349 if I is 1, use the next to lowest word; and so on. */
350 int wordnum = (backwards ? nwords - i - 1 : i);
351 int bit_offset = (backwards
352 ? MAX (bitsize - (i + 1) * BITS_PER_WORD, 0)
353 : i * BITS_PER_WORD);
354 store_bit_field (op0, MIN (BITS_PER_WORD,
355 bitsize - i * BITS_PER_WORD),
356 bitnum + bit_offset, word_mode,
357 operand_subword_force (value, wordnum,
358 (GET_MODE (value) == VOIDmode
359 ? fieldmode
360 : GET_MODE (value))),
361 align, total_size);
363 return value;
366 /* From here on we can assume that the field to be stored in is
367 a full-word (whatever type that is), since it is shorter than a word. */
369 /* OFFSET is the number of words or bytes (UNIT says which)
370 from STR_RTX to the first word or byte containing part of the field. */
372 if (GET_CODE (op0) == REG)
374 if (offset != 0
375 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
376 op0 = gen_rtx_SUBREG (TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
377 op0, offset);
378 offset = 0;
380 else
382 op0 = protect_from_queue (op0, 1);
385 /* If VALUE is a floating-point mode, access it as an integer of the
386 corresponding size. This can occur on a machine with 64 bit registers
387 that uses SFmode for float. This can also occur for unaligned float
388 structure fields. */
389 if (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT)
391 if (GET_CODE (value) != REG)
392 value = copy_to_reg (value);
393 value = gen_rtx_SUBREG (word_mode, value, 0);
396 /* Now OFFSET is nonzero only if OP0 is memory
397 and is therefore always measured in bytes. */
399 #ifdef HAVE_insv
400 if (HAVE_insv
401 && GET_MODE (value) != BLKmode
402 && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
403 /* Ensure insv's size is wide enough for this field. */
404 && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_insv][3])
405 >= bitsize)
406 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
407 && (bitsize + bitpos
408 > GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_insv][3]))))
410 int xbitpos = bitpos;
411 rtx value1;
412 rtx xop0 = op0;
413 rtx last = get_last_insn ();
414 rtx pat;
415 enum machine_mode maxmode
416 = insn_operand_mode[(int) CODE_FOR_insv][3];
418 int save_volatile_ok = volatile_ok;
419 volatile_ok = 1;
421 /* If this machine's insv can only insert into a register, copy OP0
422 into a register and save it back later. */
423 /* This used to check flag_force_mem, but that was a serious
424 de-optimization now that flag_force_mem is enabled by -O2. */
425 if (GET_CODE (op0) == MEM
426 && ! ((*insn_operand_predicate[(int) CODE_FOR_insv][0])
427 (op0, VOIDmode)))
429 rtx tempreg;
430 enum machine_mode bestmode;
432 /* Get the mode to use for inserting into this field. If OP0 is
433 BLKmode, get the smallest mode consistent with the alignment. If
434 OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
435 mode. Otherwise, use the smallest mode containing the field. */
437 if (GET_MODE (op0) == BLKmode
438 || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
439 bestmode
440 = get_best_mode (bitsize, bitnum, align * BITS_PER_UNIT, maxmode,
441 MEM_VOLATILE_P (op0));
442 else
443 bestmode = GET_MODE (op0);
445 if (bestmode == VOIDmode
446 || (SLOW_UNALIGNED_ACCESS && GET_MODE_SIZE (bestmode) > align))
447 goto insv_loses;
449 /* Adjust address to point to the containing unit of that mode. */
450 unit = GET_MODE_BITSIZE (bestmode);
451 /* Compute offset as multiple of this unit, counting in bytes. */
452 offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
453 bitpos = bitnum % unit;
454 op0 = change_address (op0, bestmode,
455 plus_constant (XEXP (op0, 0), offset));
457 /* Fetch that unit, store the bitfield in it, then store the unit. */
458 tempreg = copy_to_reg (op0);
459 store_bit_field (tempreg, bitsize, bitpos, fieldmode, value,
460 align, total_size);
461 emit_move_insn (op0, tempreg);
462 return value;
464 volatile_ok = save_volatile_ok;
466 /* Add OFFSET into OP0's address. */
467 if (GET_CODE (xop0) == MEM)
468 xop0 = change_address (xop0, byte_mode,
469 plus_constant (XEXP (xop0, 0), offset));
471 /* If xop0 is a register, we need it in MAXMODE
472 to make it acceptable to the format of insv. */
473 if (GET_CODE (xop0) == SUBREG)
474 /* We can't just change the mode, because this might clobber op0,
475 and we will need the original value of op0 if insv fails. */
476 xop0 = gen_rtx_SUBREG (maxmode, SUBREG_REG (xop0), SUBREG_WORD (xop0));
477 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
478 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
480 /* On big-endian machines, we count bits from the most significant.
481 If the bit field insn does not, we must invert. */
483 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
484 xbitpos = unit - bitsize - xbitpos;
486 /* We have been counting XBITPOS within UNIT.
487 Count instead within the size of the register. */
488 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
489 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
491 unit = GET_MODE_BITSIZE (maxmode);
493 /* Convert VALUE to maxmode (which insv insn wants) in VALUE1. */
494 value1 = value;
495 if (GET_MODE (value) != maxmode)
497 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
499 /* Optimization: Don't bother really extending VALUE
500 if it has all the bits we will actually use. However,
501 if we must narrow it, be sure we do it correctly. */
503 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
505 /* Avoid making subreg of a subreg, or of a mem. */
506 if (GET_CODE (value1) != REG)
507 value1 = copy_to_reg (value1);
508 value1 = gen_rtx_SUBREG (maxmode, value1, 0);
510 else
511 value1 = gen_lowpart (maxmode, value1);
513 else if (!CONSTANT_P (value))
514 /* Parse phase is supposed to make VALUE's data type
515 match that of the component reference, which is a type
516 at least as wide as the field; so VALUE should have
517 a mode that corresponds to that type. */
518 abort ();
521 /* If this machine's insv insists on a register,
522 get VALUE1 into a register. */
523 if (! ((*insn_operand_predicate[(int) CODE_FOR_insv][3])
524 (value1, maxmode)))
525 value1 = force_reg (maxmode, value1);
527 pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
528 if (pat)
529 emit_insn (pat);
530 else
532 delete_insns_since (last);
533 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
536 else
537 insv_loses:
538 #endif
539 /* Insv is not available; store using shifts and boolean ops. */
540 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
541 return value;
544 /* Use shifts and boolean operations to store VALUE
545 into a bit field of width BITSIZE
546 in a memory location specified by OP0 except offset by OFFSET bytes.
547 (OFFSET must be 0 if OP0 is a register.)
548 The field starts at position BITPOS within the byte.
549 (If OP0 is a register, it may be a full word or a narrower mode,
550 but BITPOS still counts within a full word,
551 which is significant on bigendian machines.)
552 STRUCT_ALIGN is the alignment the structure is known to have (in bytes).
554 Note that protect_from_queue has already been done on OP0 and VALUE. */
556 static void
557 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, struct_align)
558 register rtx op0;
559 register int offset, bitsize, bitpos;
560 register rtx value;
561 int struct_align;
563 register enum machine_mode mode;
564 int total_bits = BITS_PER_WORD;
565 rtx subtarget, temp;
566 int all_zero = 0;
567 int all_one = 0;
569 if (! SLOW_UNALIGNED_ACCESS)
570 struct_align = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
572 /* There is a case not handled here:
573 a structure with a known alignment of just a halfword
574 and a field split across two aligned halfwords within the structure.
575 Or likewise a structure with a known alignment of just a byte
576 and a field split across two bytes.
577 Such cases are not supposed to be able to occur. */
579 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
581 if (offset != 0)
582 abort ();
583 /* Special treatment for a bit field split across two registers. */
584 if (bitsize + bitpos > BITS_PER_WORD)
586 store_split_bit_field (op0, bitsize, bitpos,
587 value, BITS_PER_WORD);
588 return;
591 else
593 /* Get the proper mode to use for this field. We want a mode that
594 includes the entire field. If such a mode would be larger than
595 a word, we won't be doing the extraction the normal way. */
597 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
598 struct_align * BITS_PER_UNIT, word_mode,
599 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
601 if (mode == VOIDmode)
603 /* The only way this should occur is if the field spans word
604 boundaries. */
605 store_split_bit_field (op0,
606 bitsize, bitpos + offset * BITS_PER_UNIT,
607 value, struct_align);
608 return;
611 total_bits = GET_MODE_BITSIZE (mode);
613 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
614 be be in the range 0 to total_bits-1, and put any excess bytes in
615 OFFSET. */
616 if (bitpos >= total_bits)
618 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
619 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
620 * BITS_PER_UNIT);
623 /* Get ref to an aligned byte, halfword, or word containing the field.
624 Adjust BITPOS to be position within a word,
625 and OFFSET to be the offset of that word.
626 Then alter OP0 to refer to that word. */
627 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
628 offset -= (offset % (total_bits / BITS_PER_UNIT));
629 op0 = change_address (op0, mode,
630 plus_constant (XEXP (op0, 0), offset));
633 mode = GET_MODE (op0);
635 /* Now MODE is either some integral mode for a MEM as OP0,
636 or is a full-word for a REG as OP0. TOTAL_BITS corresponds.
637 The bit field is contained entirely within OP0.
638 BITPOS is the starting bit number within OP0.
639 (OP0's mode may actually be narrower than MODE.) */
641 if (BYTES_BIG_ENDIAN)
642 /* BITPOS is the distance between our msb
643 and that of the containing datum.
644 Convert it to the distance from the lsb. */
645 bitpos = total_bits - bitsize - bitpos;
647 /* Now BITPOS is always the distance between our lsb
648 and that of OP0. */
650 /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
651 we must first convert its mode to MODE. */
653 if (GET_CODE (value) == CONST_INT)
655 register HOST_WIDE_INT v = INTVAL (value);
657 if (bitsize < HOST_BITS_PER_WIDE_INT)
658 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
660 if (v == 0)
661 all_zero = 1;
662 else if ((bitsize < HOST_BITS_PER_WIDE_INT
663 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
664 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
665 all_one = 1;
667 value = lshift_value (mode, value, bitpos, bitsize);
669 else
671 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
672 && bitpos + bitsize != GET_MODE_BITSIZE (mode));
674 if (GET_MODE (value) != mode)
676 if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
677 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
678 value = gen_lowpart (mode, value);
679 else
680 value = convert_to_mode (mode, value, 1);
683 if (must_and)
684 value = expand_binop (mode, and_optab, value,
685 mask_rtx (mode, 0, bitsize, 0),
686 NULL_RTX, 1, OPTAB_LIB_WIDEN);
687 if (bitpos > 0)
688 value = expand_shift (LSHIFT_EXPR, mode, value,
689 build_int_2 (bitpos, 0), NULL_RTX, 1);
692 /* Now clear the chosen bits in OP0,
693 except that if VALUE is -1 we need not bother. */
695 subtarget = (GET_CODE (op0) == REG || ! flag_force_mem) ? op0 : 0;
697 if (! all_one)
699 temp = expand_binop (mode, and_optab, op0,
700 mask_rtx (mode, bitpos, bitsize, 1),
701 subtarget, 1, OPTAB_LIB_WIDEN);
702 subtarget = temp;
704 else
705 temp = op0;
707 /* Now logical-or VALUE into OP0, unless it is zero. */
709 if (! all_zero)
710 temp = expand_binop (mode, ior_optab, temp, value,
711 subtarget, 1, OPTAB_LIB_WIDEN);
712 if (op0 != temp)
713 emit_move_insn (op0, temp);
716 /* Store a bit field that is split across multiple accessible memory objects.
718 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
719 BITSIZE is the field width; BITPOS the position of its first bit
720 (within the word).
721 VALUE is the value to store.
722 ALIGN is the known alignment of OP0, measured in bytes.
723 This is also the size of the memory objects to be used.
725 This does not yet handle fields wider than BITS_PER_WORD. */
727 static void
728 store_split_bit_field (op0, bitsize, bitpos, value, align)
729 rtx op0;
730 int bitsize, bitpos;
731 rtx value;
732 int align;
734 int unit;
735 int bitsdone = 0;
737 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
738 much at a time. */
739 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
740 unit = BITS_PER_WORD;
741 else
742 unit = MIN (align * BITS_PER_UNIT, BITS_PER_WORD);
744 /* If VALUE is a constant other than a CONST_INT, get it into a register in
745 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
746 that VALUE might be a floating-point constant. */
747 if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
749 rtx word = gen_lowpart_common (word_mode, value);
751 if (word && (value != word))
752 value = word;
753 else
754 value = gen_lowpart_common (word_mode,
755 force_reg (GET_MODE (value) != VOIDmode
756 ? GET_MODE (value)
757 : word_mode, value));
759 else if (GET_CODE (value) == ADDRESSOF)
760 value = copy_to_reg (value);
762 while (bitsdone < bitsize)
764 int thissize;
765 rtx part, word;
766 int thispos;
767 int offset;
769 offset = (bitpos + bitsdone) / unit;
770 thispos = (bitpos + bitsdone) % unit;
772 /* THISSIZE must not overrun a word boundary. Otherwise,
773 store_fixed_bit_field will call us again, and we will mutually
774 recurse forever. */
775 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
776 thissize = MIN (thissize, unit - thispos);
778 if (BYTES_BIG_ENDIAN)
780 int total_bits;
782 /* We must do an endian conversion exactly the same way as it is
783 done in extract_bit_field, so that the two calls to
784 extract_fixed_bit_field will have comparable arguments. */
785 if (GET_CODE (value) != MEM || GET_MODE (value) == BLKmode)
786 total_bits = BITS_PER_WORD;
787 else
788 total_bits = GET_MODE_BITSIZE (GET_MODE (value));
790 /* Fetch successively less significant portions. */
791 if (GET_CODE (value) == CONST_INT)
792 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
793 >> (bitsize - bitsdone - thissize))
794 & (((HOST_WIDE_INT) 1 << thissize) - 1));
795 else
796 /* The args are chosen so that the last part includes the
797 lsb. Give extract_bit_field the value it needs (with
798 endianness compensation) to fetch the piece we want.
800 ??? We have no idea what the alignment of VALUE is, so
801 we have to use a guess. */
802 part
803 = extract_fixed_bit_field
804 (word_mode, value, 0, thissize,
805 total_bits - bitsize + bitsdone, NULL_RTX, 1,
806 GET_MODE (value) == VOIDmode
807 ? UNITS_PER_WORD
808 : (GET_MODE (value) == BLKmode
810 : GET_MODE_ALIGNMENT (GET_MODE (value)) / BITS_PER_UNIT));
812 else
814 /* Fetch successively more significant portions. */
815 if (GET_CODE (value) == CONST_INT)
816 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
817 >> bitsdone)
818 & (((HOST_WIDE_INT) 1 << thissize) - 1));
819 else
820 part
821 = extract_fixed_bit_field
822 (word_mode, value, 0, thissize, bitsdone, NULL_RTX, 1,
823 GET_MODE (value) == VOIDmode
824 ? UNITS_PER_WORD
825 : (GET_MODE (value) == BLKmode
827 : GET_MODE_ALIGNMENT (GET_MODE (value)) / BITS_PER_UNIT));
830 /* If OP0 is a register, then handle OFFSET here.
832 When handling multiword bitfields, extract_bit_field may pass
833 down a word_mode SUBREG of a larger REG for a bitfield that actually
834 crosses a word boundary. Thus, for a SUBREG, we must find
835 the current word starting from the base register. */
836 if (GET_CODE (op0) == SUBREG)
838 word = operand_subword_force (SUBREG_REG (op0),
839 SUBREG_WORD (op0) + offset,
840 GET_MODE (SUBREG_REG (op0)));
841 offset = 0;
843 else if (GET_CODE (op0) == REG)
845 word = operand_subword_force (op0, offset, GET_MODE (op0));
846 offset = 0;
848 else
849 word = op0;
851 /* OFFSET is in UNITs, and UNIT is in bits.
852 store_fixed_bit_field wants offset in bytes. */
853 store_fixed_bit_field (word, offset * unit / BITS_PER_UNIT,
854 thissize, thispos, part, align);
855 bitsdone += thissize;
859 /* Generate code to extract a byte-field from STR_RTX
860 containing BITSIZE bits, starting at BITNUM,
861 and put it in TARGET if possible (if TARGET is nonzero).
862 Regardless of TARGET, we return the rtx for where the value is placed.
863 It may be a QUEUED.
865 STR_RTX is the structure containing the byte (a REG or MEM).
866 UNSIGNEDP is nonzero if this is an unsigned bit field.
867 MODE is the natural mode of the field value once extracted.
868 TMODE is the mode the caller would like the value to have;
869 but the value may be returned with type MODE instead.
871 ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
872 TOTAL_SIZE is the size in bytes of the containing structure,
873 or -1 if varying.
875 If a TARGET is specified and we can store in it at no extra cost,
876 we do so, and return TARGET.
877 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
878 if they are equally easy. */
881 extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
882 target, mode, tmode, align, total_size)
883 rtx str_rtx;
884 register int bitsize;
885 int bitnum;
886 int unsignedp;
887 rtx target;
888 enum machine_mode mode, tmode;
889 int align;
890 int total_size;
892 int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
893 register int offset = bitnum / unit;
894 register int bitpos = bitnum % unit;
895 register rtx op0 = str_rtx;
896 rtx spec_target = target;
897 rtx spec_target_subreg = 0;
899 /* Discount the part of the structure before the desired byte.
900 We need to know how many bytes are safe to reference after it. */
901 if (total_size >= 0)
902 total_size -= (bitpos / BIGGEST_ALIGNMENT
903 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
905 if (tmode == VOIDmode)
906 tmode = mode;
907 while (GET_CODE (op0) == SUBREG)
909 int outer_size = GET_MODE_BITSIZE (GET_MODE (op0));
910 int inner_size = GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)));
912 offset += SUBREG_WORD (op0);
914 inner_size = MIN (inner_size, BITS_PER_WORD);
916 if (BYTES_BIG_ENDIAN && (outer_size < inner_size))
918 bitpos += inner_size - outer_size;
919 if (bitpos > unit)
921 offset += (bitpos / unit);
922 bitpos %= unit;
926 op0 = SUBREG_REG (op0);
929 /* ??? We currently assume TARGET is at least as big as BITSIZE.
930 If that's wrong, the solution is to test for it and set TARGET to 0
931 if needed. */
933 /* If OP0 is a register, BITPOS must count within a word.
934 But as we have it, it counts within whatever size OP0 now has.
935 On a bigendian machine, these are not the same, so convert. */
936 if (BYTES_BIG_ENDIAN
937 && GET_CODE (op0) != MEM
938 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
939 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
941 /* Extracting a full-word or multi-word value
942 from a structure in a register or aligned memory.
943 This can be done with just SUBREG.
944 So too extracting a subword value in
945 the least significant part of the register. */
947 if (((GET_CODE (op0) == REG
948 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
949 GET_MODE_BITSIZE (GET_MODE (op0))))
950 || (GET_CODE (op0) == MEM
951 && (! SLOW_UNALIGNED_ACCESS
952 || (offset * BITS_PER_UNIT % bitsize == 0
953 && align * BITS_PER_UNIT % bitsize == 0))))
954 && ((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
955 && bitpos % BITS_PER_WORD == 0)
956 || (mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0) != BLKmode
957 && (BYTES_BIG_ENDIAN
958 ? bitpos + bitsize == BITS_PER_WORD
959 : bitpos == 0))))
961 enum machine_mode mode1
962 = mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0);
964 if (mode1 != GET_MODE (op0))
966 if (GET_CODE (op0) == REG)
967 op0 = gen_rtx_SUBREG (mode1, op0, offset);
968 else
969 op0 = change_address (op0, mode1,
970 plus_constant (XEXP (op0, 0), offset));
972 if (mode1 != mode)
973 return convert_to_mode (tmode, op0, unsignedp);
974 return op0;
977 /* Handle fields bigger than a word. */
979 if (bitsize > BITS_PER_WORD)
981 /* Here we transfer the words of the field
982 in the order least significant first.
983 This is because the most significant word is the one which may
984 be less than full. */
986 int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
987 int i;
989 if (target == 0 || GET_CODE (target) != REG)
990 target = gen_reg_rtx (mode);
992 /* Indicate for flow that the entire target reg is being set. */
993 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
995 for (i = 0; i < nwords; i++)
997 /* If I is 0, use the low-order word in both field and target;
998 if I is 1, use the next to lowest word; and so on. */
999 /* Word number in TARGET to use. */
1000 int wordnum = (WORDS_BIG_ENDIAN
1001 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1002 : i);
1003 /* Offset from start of field in OP0. */
1004 int bit_offset = (WORDS_BIG_ENDIAN
1005 ? MAX (0, bitsize - (i + 1) * BITS_PER_WORD)
1006 : i * BITS_PER_WORD);
1007 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1008 rtx result_part
1009 = extract_bit_field (op0, MIN (BITS_PER_WORD,
1010 bitsize - i * BITS_PER_WORD),
1011 bitnum + bit_offset,
1012 1, target_part, mode, word_mode,
1013 align, total_size);
1015 if (target_part == 0)
1016 abort ();
1018 if (result_part != target_part)
1019 emit_move_insn (target_part, result_part);
1022 if (unsignedp)
1024 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1025 need to be zero'd out. */
1026 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1028 int i,total_words;
1030 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1031 for (i = nwords; i < total_words; i++)
1033 int wordnum = WORDS_BIG_ENDIAN ? total_words - i - 1 : i;
1034 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1035 emit_move_insn (target_part, const0_rtx);
1038 return target;
1041 /* Signed bit field: sign-extend with two arithmetic shifts. */
1042 target = expand_shift (LSHIFT_EXPR, mode, target,
1043 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1044 NULL_RTX, 0);
1045 return expand_shift (RSHIFT_EXPR, mode, target,
1046 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1047 NULL_RTX, 0);
1050 /* From here on we know the desired field is smaller than a word
1051 so we can assume it is an integer. So we can safely extract it as one
1052 size of integer, if necessary, and then truncate or extend
1053 to the size that is wanted. */
1055 /* OFFSET is the number of words or bytes (UNIT says which)
1056 from STR_RTX to the first word or byte containing part of the field. */
1058 if (GET_CODE (op0) == REG)
1060 if (offset != 0
1061 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1062 op0 = gen_rtx_SUBREG (TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
1063 op0, offset);
1064 offset = 0;
1066 else
1068 op0 = protect_from_queue (str_rtx, 1);
1071 /* Now OFFSET is nonzero only for memory operands. */
1073 if (unsignedp)
1075 #ifdef HAVE_extzv
1076 if (HAVE_extzv
1077 && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extzv][0])
1078 >= bitsize)
1079 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
1080 && (bitsize + bitpos
1081 > GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extzv][0]))))
1083 int xbitpos = bitpos, xoffset = offset;
1084 rtx bitsize_rtx, bitpos_rtx;
1085 rtx last = get_last_insn ();
1086 rtx xop0 = op0;
1087 rtx xtarget = target;
1088 rtx xspec_target = spec_target;
1089 rtx xspec_target_subreg = spec_target_subreg;
1090 rtx pat;
1091 enum machine_mode maxmode
1092 = insn_operand_mode[(int) CODE_FOR_extzv][0];
1094 if (GET_CODE (xop0) == MEM)
1096 int save_volatile_ok = volatile_ok;
1097 volatile_ok = 1;
1099 /* Is the memory operand acceptable? */
1100 if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
1101 (xop0, GET_MODE (xop0))))
1103 /* No, load into a reg and extract from there. */
1104 enum machine_mode bestmode;
1106 /* Get the mode to use for inserting into this field. If
1107 OP0 is BLKmode, get the smallest mode consistent with the
1108 alignment. If OP0 is a non-BLKmode object that is no
1109 wider than MAXMODE, use its mode. Otherwise, use the
1110 smallest mode containing the field. */
1112 if (GET_MODE (xop0) == BLKmode
1113 || (GET_MODE_SIZE (GET_MODE (op0))
1114 > GET_MODE_SIZE (maxmode)))
1115 bestmode = get_best_mode (bitsize, bitnum,
1116 align * BITS_PER_UNIT, maxmode,
1117 MEM_VOLATILE_P (xop0));
1118 else
1119 bestmode = GET_MODE (xop0);
1121 if (bestmode == VOIDmode
1122 || (SLOW_UNALIGNED_ACCESS && GET_MODE_SIZE (bestmode) > align))
1123 goto extzv_loses;
1125 /* Compute offset as multiple of this unit,
1126 counting in bytes. */
1127 unit = GET_MODE_BITSIZE (bestmode);
1128 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1129 xbitpos = bitnum % unit;
1130 xop0 = change_address (xop0, bestmode,
1131 plus_constant (XEXP (xop0, 0),
1132 xoffset));
1133 /* Fetch it to a register in that size. */
1134 xop0 = force_reg (bestmode, xop0);
1136 /* XBITPOS counts within UNIT, which is what is expected. */
1138 else
1139 /* Get ref to first byte containing part of the field. */
1140 xop0 = change_address (xop0, byte_mode,
1141 plus_constant (XEXP (xop0, 0), xoffset));
1143 volatile_ok = save_volatile_ok;
1146 /* If op0 is a register, we need it in MAXMODE (which is usually
1147 SImode). to make it acceptable to the format of extzv. */
1148 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1149 goto extzv_loses;
1150 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
1151 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1153 /* On big-endian machines, we count bits from the most significant.
1154 If the bit field insn does not, we must invert. */
1155 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1156 xbitpos = unit - bitsize - xbitpos;
1158 /* Now convert from counting within UNIT to counting in MAXMODE. */
1159 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
1160 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
1162 unit = GET_MODE_BITSIZE (maxmode);
1164 if (xtarget == 0
1165 || (flag_force_mem && GET_CODE (xtarget) == MEM))
1166 xtarget = xspec_target = gen_reg_rtx (tmode);
1168 if (GET_MODE (xtarget) != maxmode)
1170 if (GET_CODE (xtarget) == REG)
1172 int wider = (GET_MODE_SIZE (maxmode)
1173 > GET_MODE_SIZE (GET_MODE (xtarget)));
1174 xtarget = gen_lowpart (maxmode, xtarget);
1175 if (wider)
1176 xspec_target_subreg = xtarget;
1178 else
1179 xtarget = gen_reg_rtx (maxmode);
1182 /* If this machine's extzv insists on a register target,
1183 make sure we have one. */
1184 if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
1185 (xtarget, maxmode)))
1186 xtarget = gen_reg_rtx (maxmode);
1188 bitsize_rtx = GEN_INT (bitsize);
1189 bitpos_rtx = GEN_INT (xbitpos);
1191 pat = gen_extzv (protect_from_queue (xtarget, 1),
1192 xop0, bitsize_rtx, bitpos_rtx);
1193 if (pat)
1195 emit_insn (pat);
1196 target = xtarget;
1197 spec_target = xspec_target;
1198 spec_target_subreg = xspec_target_subreg;
1200 else
1202 delete_insns_since (last);
1203 target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
1204 bitpos, target, 1, align);
1207 else
1208 extzv_loses:
1209 #endif
1210 target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1211 target, 1, align);
1213 else
1215 #ifdef HAVE_extv
1216 if (HAVE_extv
1217 && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extv][0])
1218 >= bitsize)
1219 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
1220 && (bitsize + bitpos
1221 > GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extv][0]))))
1223 int xbitpos = bitpos, xoffset = offset;
1224 rtx bitsize_rtx, bitpos_rtx;
1225 rtx last = get_last_insn ();
1226 rtx xop0 = op0, xtarget = target;
1227 rtx xspec_target = spec_target;
1228 rtx xspec_target_subreg = spec_target_subreg;
1229 rtx pat;
1230 enum machine_mode maxmode
1231 = insn_operand_mode[(int) CODE_FOR_extv][0];
1233 if (GET_CODE (xop0) == MEM)
1235 /* Is the memory operand acceptable? */
1236 if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][1])
1237 (xop0, GET_MODE (xop0))))
1239 /* No, load into a reg and extract from there. */
1240 enum machine_mode bestmode;
1242 /* Get the mode to use for inserting into this field. If
1243 OP0 is BLKmode, get the smallest mode consistent with the
1244 alignment. If OP0 is a non-BLKmode object that is no
1245 wider than MAXMODE, use its mode. Otherwise, use the
1246 smallest mode containing the field. */
1248 if (GET_MODE (xop0) == BLKmode
1249 || (GET_MODE_SIZE (GET_MODE (op0))
1250 > GET_MODE_SIZE (maxmode)))
1251 bestmode = get_best_mode (bitsize, bitnum,
1252 align * BITS_PER_UNIT, maxmode,
1253 MEM_VOLATILE_P (xop0));
1254 else
1255 bestmode = GET_MODE (xop0);
1257 if (bestmode == VOIDmode
1258 || (SLOW_UNALIGNED_ACCESS && GET_MODE_SIZE (bestmode) > align))
1259 goto extv_loses;
1261 /* Compute offset as multiple of this unit,
1262 counting in bytes. */
1263 unit = GET_MODE_BITSIZE (bestmode);
1264 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1265 xbitpos = bitnum % unit;
1266 xop0 = change_address (xop0, bestmode,
1267 plus_constant (XEXP (xop0, 0),
1268 xoffset));
1269 /* Fetch it to a register in that size. */
1270 xop0 = force_reg (bestmode, xop0);
1272 /* XBITPOS counts within UNIT, which is what is expected. */
1274 else
1275 /* Get ref to first byte containing part of the field. */
1276 xop0 = change_address (xop0, byte_mode,
1277 plus_constant (XEXP (xop0, 0), xoffset));
1280 /* If op0 is a register, we need it in MAXMODE (which is usually
1281 SImode) to make it acceptable to the format of extv. */
1282 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1283 goto extv_loses;
1284 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
1285 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
1287 /* On big-endian machines, we count bits from the most significant.
1288 If the bit field insn does not, we must invert. */
1289 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1290 xbitpos = unit - bitsize - xbitpos;
1292 /* XBITPOS counts within a size of UNIT.
1293 Adjust to count within a size of MAXMODE. */
1294 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
1295 xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
1297 unit = GET_MODE_BITSIZE (maxmode);
1299 if (xtarget == 0
1300 || (flag_force_mem && GET_CODE (xtarget) == MEM))
1301 xtarget = xspec_target = gen_reg_rtx (tmode);
1303 if (GET_MODE (xtarget) != maxmode)
1305 if (GET_CODE (xtarget) == REG)
1307 int wider = (GET_MODE_SIZE (maxmode)
1308 > GET_MODE_SIZE (GET_MODE (xtarget)));
1309 xtarget = gen_lowpart (maxmode, xtarget);
1310 if (wider)
1311 xspec_target_subreg = xtarget;
1313 else
1314 xtarget = gen_reg_rtx (maxmode);
1317 /* If this machine's extv insists on a register target,
1318 make sure we have one. */
1319 if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][0])
1320 (xtarget, maxmode)))
1321 xtarget = gen_reg_rtx (maxmode);
1323 bitsize_rtx = GEN_INT (bitsize);
1324 bitpos_rtx = GEN_INT (xbitpos);
1326 pat = gen_extv (protect_from_queue (xtarget, 1),
1327 xop0, bitsize_rtx, bitpos_rtx);
1328 if (pat)
1330 emit_insn (pat);
1331 target = xtarget;
1332 spec_target = xspec_target;
1333 spec_target_subreg = xspec_target_subreg;
1335 else
1337 delete_insns_since (last);
1338 target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
1339 bitpos, target, 0, align);
1342 else
1343 extv_loses:
1344 #endif
1345 target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1346 target, 0, align);
1348 if (target == spec_target)
1349 return target;
1350 if (target == spec_target_subreg)
1351 return spec_target;
1352 if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
1354 /* If the target mode is floating-point, first convert to the
1355 integer mode of that size and then access it as a floating-point
1356 value via a SUBREG. */
1357 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1359 target = convert_to_mode (mode_for_size (GET_MODE_BITSIZE (tmode),
1360 MODE_INT, 0),
1361 target, unsignedp);
1362 if (GET_CODE (target) != REG)
1363 target = copy_to_reg (target);
1364 return gen_rtx_SUBREG (tmode, target, 0);
1366 else
1367 return convert_to_mode (tmode, target, unsignedp);
1369 return target;
1372 /* Extract a bit field using shifts and boolean operations
1373 Returns an rtx to represent the value.
1374 OP0 addresses a register (word) or memory (byte).
1375 BITPOS says which bit within the word or byte the bit field starts in.
1376 OFFSET says how many bytes farther the bit field starts;
1377 it is 0 if OP0 is a register.
1378 BITSIZE says how many bits long the bit field is.
1379 (If OP0 is a register, it may be narrower than a full word,
1380 but BITPOS still counts within a full word,
1381 which is significant on bigendian machines.)
1383 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1384 If TARGET is nonzero, attempts to store the value there
1385 and return TARGET, but this is not guaranteed.
1386 If TARGET is not used, create a pseudo-reg of mode TMODE for the value.
1388 ALIGN is the alignment that STR_RTX is known to have, measured in bytes. */
1390 static rtx
1391 extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1392 target, unsignedp, align)
1393 enum machine_mode tmode;
1394 register rtx op0, target;
1395 register int offset, bitsize, bitpos;
1396 int unsignedp;
1397 int align;
1399 int total_bits = BITS_PER_WORD;
1400 enum machine_mode mode;
1402 if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
1404 /* Special treatment for a bit field split across two registers. */
1405 if (bitsize + bitpos > BITS_PER_WORD)
1406 return extract_split_bit_field (op0, bitsize, bitpos,
1407 unsignedp, align);
1409 else
1411 /* Get the proper mode to use for this field. We want a mode that
1412 includes the entire field. If such a mode would be larger than
1413 a word, we won't be doing the extraction the normal way. */
1415 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1416 align * BITS_PER_UNIT, word_mode,
1417 GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
1419 if (mode == VOIDmode)
1420 /* The only way this should occur is if the field spans word
1421 boundaries. */
1422 return extract_split_bit_field (op0, bitsize,
1423 bitpos + offset * BITS_PER_UNIT,
1424 unsignedp, align);
1426 total_bits = GET_MODE_BITSIZE (mode);
1428 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
1429 be be in the range 0 to total_bits-1, and put any excess bytes in
1430 OFFSET. */
1431 if (bitpos >= total_bits)
1433 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
1434 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
1435 * BITS_PER_UNIT);
1438 /* Get ref to an aligned byte, halfword, or word containing the field.
1439 Adjust BITPOS to be position within a word,
1440 and OFFSET to be the offset of that word.
1441 Then alter OP0 to refer to that word. */
1442 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1443 offset -= (offset % (total_bits / BITS_PER_UNIT));
1444 op0 = change_address (op0, mode,
1445 plus_constant (XEXP (op0, 0), offset));
1448 mode = GET_MODE (op0);
1450 if (BYTES_BIG_ENDIAN)
1452 /* BITPOS is the distance between our msb and that of OP0.
1453 Convert it to the distance from the lsb. */
1455 bitpos = total_bits - bitsize - bitpos;
1458 /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1459 We have reduced the big-endian case to the little-endian case. */
1461 if (unsignedp)
1463 if (bitpos)
1465 /* If the field does not already start at the lsb,
1466 shift it so it does. */
1467 tree amount = build_int_2 (bitpos, 0);
1468 /* Maybe propagate the target for the shift. */
1469 /* But not if we will return it--could confuse integrate.c. */
1470 rtx subtarget = (target != 0 && GET_CODE (target) == REG
1471 && !REG_FUNCTION_VALUE_P (target)
1472 ? target : 0);
1473 if (tmode != mode) subtarget = 0;
1474 op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1476 /* Convert the value to the desired mode. */
1477 if (mode != tmode)
1478 op0 = convert_to_mode (tmode, op0, 1);
1480 /* Unless the msb of the field used to be the msb when we shifted,
1481 mask out the upper bits. */
1483 if (GET_MODE_BITSIZE (mode) != bitpos + bitsize
1484 #if 0
1485 #ifdef SLOW_ZERO_EXTEND
1486 /* Always generate an `and' if
1487 we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
1488 will combine fruitfully with the zero-extend. */
1489 || tmode != mode
1490 #endif
1491 #endif
1493 return expand_binop (GET_MODE (op0), and_optab, op0,
1494 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1495 target, 1, OPTAB_LIB_WIDEN);
1496 return op0;
1499 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1500 then arithmetic-shift its lsb to the lsb of the word. */
1501 op0 = force_reg (mode, op0);
1502 if (mode != tmode)
1503 target = 0;
1505 /* Find the narrowest integer mode that contains the field. */
1507 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1508 mode = GET_MODE_WIDER_MODE (mode))
1509 if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1511 op0 = convert_to_mode (mode, op0, 0);
1512 break;
1515 if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1517 tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
1518 /* Maybe propagate the target for the shift. */
1519 /* But not if we will return the result--could confuse integrate.c. */
1520 rtx subtarget = (target != 0 && GET_CODE (target) == REG
1521 && ! REG_FUNCTION_VALUE_P (target)
1522 ? target : 0);
1523 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1526 return expand_shift (RSHIFT_EXPR, mode, op0,
1527 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1528 target, 0);
1531 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1532 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1533 complement of that if COMPLEMENT. The mask is truncated if
1534 necessary to the width of mode MODE. The mask is zero-extended if
1535 BITSIZE+BITPOS is too small for MODE. */
1537 static rtx
1538 mask_rtx (mode, bitpos, bitsize, complement)
1539 enum machine_mode mode;
1540 int bitpos, bitsize, complement;
1542 HOST_WIDE_INT masklow, maskhigh;
1544 if (bitpos < HOST_BITS_PER_WIDE_INT)
1545 masklow = (HOST_WIDE_INT) -1 << bitpos;
1546 else
1547 masklow = 0;
1549 if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1550 masklow &= ((unsigned HOST_WIDE_INT) -1
1551 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1553 if (bitpos <= HOST_BITS_PER_WIDE_INT)
1554 maskhigh = -1;
1555 else
1556 maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1558 if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
1559 maskhigh &= ((unsigned HOST_WIDE_INT) -1
1560 >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1561 else
1562 maskhigh = 0;
1564 if (complement)
1566 maskhigh = ~maskhigh;
1567 masklow = ~masklow;
1570 return immed_double_const (masklow, maskhigh, mode);
1573 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1574 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1576 static rtx
1577 lshift_value (mode, value, bitpos, bitsize)
1578 enum machine_mode mode;
1579 rtx value;
1580 int bitpos, bitsize;
1582 unsigned HOST_WIDE_INT v = INTVAL (value);
1583 HOST_WIDE_INT low, high;
1585 if (bitsize < HOST_BITS_PER_WIDE_INT)
1586 v &= ~((HOST_WIDE_INT) -1 << bitsize);
1588 if (bitpos < HOST_BITS_PER_WIDE_INT)
1590 low = v << bitpos;
1591 high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
1593 else
1595 low = 0;
1596 high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
1599 return immed_double_const (low, high, mode);
1602 /* Extract a bit field that is split across two words
1603 and return an RTX for the result.
1605 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1606 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1607 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.
1609 ALIGN is the known alignment of OP0, measured in bytes.
1610 This is also the size of the memory objects to be used. */
1612 static rtx
1613 extract_split_bit_field (op0, bitsize, bitpos, unsignedp, align)
1614 rtx op0;
1615 int bitsize, bitpos, unsignedp, align;
1617 int unit;
1618 int bitsdone = 0;
1619 rtx result;
1620 int first = 1;
1622 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1623 much at a time. */
1624 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
1625 unit = BITS_PER_WORD;
1626 else
1627 unit = MIN (align * BITS_PER_UNIT, BITS_PER_WORD);
1629 while (bitsdone < bitsize)
1631 int thissize;
1632 rtx part, word;
1633 int thispos;
1634 int offset;
1636 offset = (bitpos + bitsdone) / unit;
1637 thispos = (bitpos + bitsdone) % unit;
1639 /* THISSIZE must not overrun a word boundary. Otherwise,
1640 extract_fixed_bit_field will call us again, and we will mutually
1641 recurse forever. */
1642 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1643 thissize = MIN (thissize, unit - thispos);
1645 /* If OP0 is a register, then handle OFFSET here.
1647 When handling multiword bitfields, extract_bit_field may pass
1648 down a word_mode SUBREG of a larger REG for a bitfield that actually
1649 crosses a word boundary. Thus, for a SUBREG, we must find
1650 the current word starting from the base register. */
1651 if (GET_CODE (op0) == SUBREG)
1653 word = operand_subword_force (SUBREG_REG (op0),
1654 SUBREG_WORD (op0) + offset,
1655 GET_MODE (SUBREG_REG (op0)));
1656 offset = 0;
1658 else if (GET_CODE (op0) == REG)
1660 word = operand_subword_force (op0, offset, GET_MODE (op0));
1661 offset = 0;
1663 else
1664 word = op0;
1666 /* Extract the parts in bit-counting order,
1667 whose meaning is determined by BYTES_PER_UNIT.
1668 OFFSET is in UNITs, and UNIT is in bits.
1669 extract_fixed_bit_field wants offset in bytes. */
1670 part = extract_fixed_bit_field (word_mode, word,
1671 offset * unit / BITS_PER_UNIT,
1672 thissize, thispos, 0, 1, align);
1673 bitsdone += thissize;
1675 /* Shift this part into place for the result. */
1676 if (BYTES_BIG_ENDIAN)
1678 if (bitsize != bitsdone)
1679 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1680 build_int_2 (bitsize - bitsdone, 0), 0, 1);
1682 else
1684 if (bitsdone != thissize)
1685 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1686 build_int_2 (bitsdone - thissize, 0), 0, 1);
1689 if (first)
1690 result = part;
1691 else
1692 /* Combine the parts with bitwise or. This works
1693 because we extracted each part as an unsigned bit field. */
1694 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
1695 OPTAB_LIB_WIDEN);
1697 first = 0;
1700 /* Unsigned bit field: we are done. */
1701 if (unsignedp)
1702 return result;
1703 /* Signed bit field: sign-extend with two arithmetic shifts. */
1704 result = expand_shift (LSHIFT_EXPR, word_mode, result,
1705 build_int_2 (BITS_PER_WORD - bitsize, 0),
1706 NULL_RTX, 0);
1707 return expand_shift (RSHIFT_EXPR, word_mode, result,
1708 build_int_2 (BITS_PER_WORD - bitsize, 0), NULL_RTX, 0);
1711 /* Add INC into TARGET. */
1713 void
1714 expand_inc (target, inc)
1715 rtx target, inc;
1717 rtx value = expand_binop (GET_MODE (target), add_optab,
1718 target, inc,
1719 target, 0, OPTAB_LIB_WIDEN);
1720 if (value != target)
1721 emit_move_insn (target, value);
1724 /* Subtract DEC from TARGET. */
1726 void
1727 expand_dec (target, dec)
1728 rtx target, dec;
1730 rtx value = expand_binop (GET_MODE (target), sub_optab,
1731 target, dec,
1732 target, 0, OPTAB_LIB_WIDEN);
1733 if (value != target)
1734 emit_move_insn (target, value);
1737 /* Output a shift instruction for expression code CODE,
1738 with SHIFTED being the rtx for the value to shift,
1739 and AMOUNT the tree for the amount to shift by.
1740 Store the result in the rtx TARGET, if that is convenient.
1741 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
1742 Return the rtx for where the value is. */
1745 expand_shift (code, mode, shifted, amount, target, unsignedp)
1746 enum tree_code code;
1747 register enum machine_mode mode;
1748 rtx shifted;
1749 tree amount;
1750 register rtx target;
1751 int unsignedp;
1753 register rtx op1, temp = 0;
1754 register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
1755 register int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
1756 int try;
1758 /* Previously detected shift-counts computed by NEGATE_EXPR
1759 and shifted in the other direction; but that does not work
1760 on all machines. */
1762 op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
1764 #ifdef SHIFT_COUNT_TRUNCATED
1765 if (SHIFT_COUNT_TRUNCATED
1766 && GET_CODE (op1) == CONST_INT
1767 && (unsigned HOST_WIDE_INT) INTVAL (op1) >= GET_MODE_BITSIZE (mode))
1768 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
1769 % GET_MODE_BITSIZE (mode));
1770 #endif
1772 if (op1 == const0_rtx)
1773 return shifted;
1775 for (try = 0; temp == 0 && try < 3; try++)
1777 enum optab_methods methods;
1779 if (try == 0)
1780 methods = OPTAB_DIRECT;
1781 else if (try == 1)
1782 methods = OPTAB_WIDEN;
1783 else
1784 methods = OPTAB_LIB_WIDEN;
1786 if (rotate)
1788 /* Widening does not work for rotation. */
1789 if (methods == OPTAB_WIDEN)
1790 continue;
1791 else if (methods == OPTAB_LIB_WIDEN)
1793 /* If we have been unable to open-code this by a rotation,
1794 do it as the IOR of two shifts. I.e., to rotate A
1795 by N bits, compute (A << N) | ((unsigned) A >> (C - N))
1796 where C is the bitsize of A.
1798 It is theoretically possible that the target machine might
1799 not be able to perform either shift and hence we would
1800 be making two libcalls rather than just the one for the
1801 shift (similarly if IOR could not be done). We will allow
1802 this extremely unlikely lossage to avoid complicating the
1803 code below. */
1805 rtx subtarget = target == shifted ? 0 : target;
1806 rtx temp1;
1807 tree type = TREE_TYPE (amount);
1808 tree new_amount = make_tree (type, op1);
1809 tree other_amount
1810 = fold (build (MINUS_EXPR, type,
1811 convert (type,
1812 build_int_2 (GET_MODE_BITSIZE (mode),
1813 0)),
1814 amount));
1816 shifted = force_reg (mode, shifted);
1818 temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
1819 mode, shifted, new_amount, subtarget, 1);
1820 temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
1821 mode, shifted, other_amount, 0, 1);
1822 return expand_binop (mode, ior_optab, temp, temp1, target,
1823 unsignedp, methods);
1826 temp = expand_binop (mode,
1827 left ? rotl_optab : rotr_optab,
1828 shifted, op1, target, unsignedp, methods);
1830 /* If we don't have the rotate, but we are rotating by a constant
1831 that is in range, try a rotate in the opposite direction. */
1833 if (temp == 0 && GET_CODE (op1) == CONST_INT
1834 && INTVAL (op1) > 0 && INTVAL (op1) < GET_MODE_BITSIZE (mode))
1835 temp = expand_binop (mode,
1836 left ? rotr_optab : rotl_optab,
1837 shifted,
1838 GEN_INT (GET_MODE_BITSIZE (mode)
1839 - INTVAL (op1)),
1840 target, unsignedp, methods);
1842 else if (unsignedp)
1843 temp = expand_binop (mode,
1844 left ? ashl_optab : lshr_optab,
1845 shifted, op1, target, unsignedp, methods);
1847 /* Do arithmetic shifts.
1848 Also, if we are going to widen the operand, we can just as well
1849 use an arithmetic right-shift instead of a logical one. */
1850 if (temp == 0 && ! rotate
1851 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
1853 enum optab_methods methods1 = methods;
1855 /* If trying to widen a log shift to an arithmetic shift,
1856 don't accept an arithmetic shift of the same size. */
1857 if (unsignedp)
1858 methods1 = OPTAB_MUST_WIDEN;
1860 /* Arithmetic shift */
1862 temp = expand_binop (mode,
1863 left ? ashl_optab : ashr_optab,
1864 shifted, op1, target, unsignedp, methods1);
1867 /* We used to try extzv here for logical right shifts, but that was
1868 only useful for one machine, the VAX, and caused poor code
1869 generation there for lshrdi3, so the code was deleted and a
1870 define_expand for lshrsi3 was added to vax.md. */
1873 if (temp == 0)
1874 abort ();
1875 return temp;
1878 enum alg_code { alg_zero, alg_m, alg_shift,
1879 alg_add_t_m2, alg_sub_t_m2,
1880 alg_add_factor, alg_sub_factor,
1881 alg_add_t2_m, alg_sub_t2_m,
1882 alg_add, alg_subtract, alg_factor, alg_shiftop };
1884 /* This structure records a sequence of operations.
1885 `ops' is the number of operations recorded.
1886 `cost' is their total cost.
1887 The operations are stored in `op' and the corresponding
1888 logarithms of the integer coefficients in `log'.
1890 These are the operations:
1891 alg_zero total := 0;
1892 alg_m total := multiplicand;
1893 alg_shift total := total * coeff
1894 alg_add_t_m2 total := total + multiplicand * coeff;
1895 alg_sub_t_m2 total := total - multiplicand * coeff;
1896 alg_add_factor total := total * coeff + total;
1897 alg_sub_factor total := total * coeff - total;
1898 alg_add_t2_m total := total * coeff + multiplicand;
1899 alg_sub_t2_m total := total * coeff - multiplicand;
1901 The first operand must be either alg_zero or alg_m. */
1903 struct algorithm
1905 short cost;
1906 short ops;
1907 /* The size of the OP and LOG fields are not directly related to the
1908 word size, but the worst-case algorithms will be if we have few
1909 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
1910 In that case we will generate shift-by-2, add, shift-by-2, add,...,
1911 in total wordsize operations. */
1912 enum alg_code op[MAX_BITS_PER_WORD];
1913 char log[MAX_BITS_PER_WORD];
1916 /* Compute and return the best algorithm for multiplying by T.
1917 The algorithm must cost less than cost_limit
1918 If retval.cost >= COST_LIMIT, no algorithm was found and all
1919 other field of the returned struct are undefined. */
1921 static void
1922 synth_mult (alg_out, t, cost_limit)
1923 struct algorithm *alg_out;
1924 unsigned HOST_WIDE_INT t;
1925 int cost_limit;
1927 int m;
1928 struct algorithm *alg_in, *best_alg;
1929 unsigned int cost;
1930 unsigned HOST_WIDE_INT q;
1932 /* Indicate that no algorithm is yet found. If no algorithm
1933 is found, this value will be returned and indicate failure. */
1934 alg_out->cost = cost_limit;
1936 if (cost_limit <= 0)
1937 return;
1939 /* t == 1 can be done in zero cost. */
1940 if (t == 1)
1942 alg_out->ops = 1;
1943 alg_out->cost = 0;
1944 alg_out->op[0] = alg_m;
1945 return;
1948 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
1949 fail now. */
1950 if (t == 0)
1952 if (zero_cost >= cost_limit)
1953 return;
1954 else
1956 alg_out->ops = 1;
1957 alg_out->cost = zero_cost;
1958 alg_out->op[0] = alg_zero;
1959 return;
1963 /* We'll be needing a couple extra algorithm structures now. */
1965 alg_in = (struct algorithm *)alloca (sizeof (struct algorithm));
1966 best_alg = (struct algorithm *)alloca (sizeof (struct algorithm));
1968 /* If we have a group of zero bits at the low-order part of T, try
1969 multiplying by the remaining bits and then doing a shift. */
1971 if ((t & 1) == 0)
1973 m = floor_log2 (t & -t); /* m = number of low zero bits */
1974 q = t >> m;
1975 cost = shift_cost[m];
1976 synth_mult (alg_in, q, cost_limit - cost);
1978 cost += alg_in->cost;
1979 if (cost < cost_limit)
1981 struct algorithm *x;
1982 x = alg_in, alg_in = best_alg, best_alg = x;
1983 best_alg->log[best_alg->ops] = m;
1984 best_alg->op[best_alg->ops] = alg_shift;
1985 cost_limit = cost;
1989 /* If we have an odd number, add or subtract one. */
1990 if ((t & 1) != 0)
1992 unsigned HOST_WIDE_INT w;
1994 for (w = 1; (w & t) != 0; w <<= 1)
1996 if (w > 2
1997 /* Reject the case where t is 3.
1998 Thus we prefer addition in that case. */
1999 && t != 3)
2001 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2003 cost = add_cost;
2004 synth_mult (alg_in, t + 1, cost_limit - cost);
2006 cost += alg_in->cost;
2007 if (cost < cost_limit)
2009 struct algorithm *x;
2010 x = alg_in, alg_in = best_alg, best_alg = x;
2011 best_alg->log[best_alg->ops] = 0;
2012 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2013 cost_limit = cost;
2016 else
2018 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2020 cost = add_cost;
2021 synth_mult (alg_in, t - 1, cost_limit - cost);
2023 cost += alg_in->cost;
2024 if (cost < cost_limit)
2026 struct algorithm *x;
2027 x = alg_in, alg_in = best_alg, best_alg = x;
2028 best_alg->log[best_alg->ops] = 0;
2029 best_alg->op[best_alg->ops] = alg_add_t_m2;
2030 cost_limit = cost;
2035 /* Look for factors of t of the form
2036 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2037 If we find such a factor, we can multiply by t using an algorithm that
2038 multiplies by q, shift the result by m and add/subtract it to itself.
2040 We search for large factors first and loop down, even if large factors
2041 are less probable than small; if we find a large factor we will find a
2042 good sequence quickly, and therefore be able to prune (by decreasing
2043 COST_LIMIT) the search. */
2045 for (m = floor_log2 (t - 1); m >= 2; m--)
2047 unsigned HOST_WIDE_INT d;
2049 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2050 if (t % d == 0 && t > d)
2052 cost = MIN (shiftadd_cost[m], add_cost + shift_cost[m]);
2053 synth_mult (alg_in, t / d, cost_limit - cost);
2055 cost += alg_in->cost;
2056 if (cost < cost_limit)
2058 struct algorithm *x;
2059 x = alg_in, alg_in = best_alg, best_alg = x;
2060 best_alg->log[best_alg->ops] = m;
2061 best_alg->op[best_alg->ops] = alg_add_factor;
2062 cost_limit = cost;
2064 /* Other factors will have been taken care of in the recursion. */
2065 break;
2068 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2069 if (t % d == 0 && t > d)
2071 cost = MIN (shiftsub_cost[m], add_cost + shift_cost[m]);
2072 synth_mult (alg_in, t / d, cost_limit - cost);
2074 cost += alg_in->cost;
2075 if (cost < cost_limit)
2077 struct algorithm *x;
2078 x = alg_in, alg_in = best_alg, best_alg = x;
2079 best_alg->log[best_alg->ops] = m;
2080 best_alg->op[best_alg->ops] = alg_sub_factor;
2081 cost_limit = cost;
2083 break;
2087 /* Try shift-and-add (load effective address) instructions,
2088 i.e. do a*3, a*5, a*9. */
2089 if ((t & 1) != 0)
2091 q = t - 1;
2092 q = q & -q;
2093 m = exact_log2 (q);
2094 if (m >= 0)
2096 cost = shiftadd_cost[m];
2097 synth_mult (alg_in, (t - 1) >> m, cost_limit - cost);
2099 cost += alg_in->cost;
2100 if (cost < cost_limit)
2102 struct algorithm *x;
2103 x = alg_in, alg_in = best_alg, best_alg = x;
2104 best_alg->log[best_alg->ops] = m;
2105 best_alg->op[best_alg->ops] = alg_add_t2_m;
2106 cost_limit = cost;
2110 q = t + 1;
2111 q = q & -q;
2112 m = exact_log2 (q);
2113 if (m >= 0)
2115 cost = shiftsub_cost[m];
2116 synth_mult (alg_in, (t + 1) >> m, cost_limit - cost);
2118 cost += alg_in->cost;
2119 if (cost < cost_limit)
2121 struct algorithm *x;
2122 x = alg_in, alg_in = best_alg, best_alg = x;
2123 best_alg->log[best_alg->ops] = m;
2124 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2125 cost_limit = cost;
2130 /* If cost_limit has not decreased since we stored it in alg_out->cost,
2131 we have not found any algorithm. */
2132 if (cost_limit == alg_out->cost)
2133 return;
2135 /* If we are getting a too long sequence for `struct algorithm'
2136 to record, make this search fail. */
2137 if (best_alg->ops == MAX_BITS_PER_WORD)
2138 return;
2140 /* Copy the algorithm from temporary space to the space at alg_out.
2141 We avoid using structure assignment because the majority of
2142 best_alg is normally undefined, and this is a critical function. */
2143 alg_out->ops = best_alg->ops + 1;
2144 alg_out->cost = cost_limit;
2145 bcopy ((char *) best_alg->op, (char *) alg_out->op,
2146 alg_out->ops * sizeof *alg_out->op);
2147 bcopy ((char *) best_alg->log, (char *) alg_out->log,
2148 alg_out->ops * sizeof *alg_out->log);
2151 /* Perform a multiplication and return an rtx for the result.
2152 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
2153 TARGET is a suggestion for where to store the result (an rtx).
2155 We check specially for a constant integer as OP1.
2156 If you want this check for OP0 as well, then before calling
2157 you should swap the two operands if OP0 would be constant. */
2160 expand_mult (mode, op0, op1, target, unsignedp)
2161 enum machine_mode mode;
2162 register rtx op0, op1, target;
2163 int unsignedp;
2165 rtx const_op1 = op1;
2167 /* synth_mult does an `unsigned int' multiply. As long as the mode is
2168 less than or equal in size to `unsigned int' this doesn't matter.
2169 If the mode is larger than `unsigned int', then synth_mult works only
2170 if the constant value exactly fits in an `unsigned int' without any
2171 truncation. This means that multiplying by negative values does
2172 not work; results are off by 2^32 on a 32 bit machine. */
2174 /* If we are multiplying in DImode, it may still be a win
2175 to try to work with shifts and adds. */
2176 if (GET_CODE (op1) == CONST_DOUBLE
2177 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
2178 && HOST_BITS_PER_INT >= BITS_PER_WORD
2179 && CONST_DOUBLE_HIGH (op1) == 0)
2180 const_op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2181 else if (HOST_BITS_PER_INT < GET_MODE_BITSIZE (mode)
2182 && GET_CODE (op1) == CONST_INT
2183 && INTVAL (op1) < 0)
2184 const_op1 = 0;
2186 /* We used to test optimize here, on the grounds that it's better to
2187 produce a smaller program when -O is not used.
2188 But this causes such a terrible slowdown sometimes
2189 that it seems better to use synth_mult always. */
2191 if (const_op1 && GET_CODE (const_op1) == CONST_INT)
2193 struct algorithm alg;
2194 struct algorithm alg2;
2195 HOST_WIDE_INT val = INTVAL (op1);
2196 HOST_WIDE_INT val_so_far;
2197 rtx insn;
2198 int mult_cost;
2199 enum {basic_variant, negate_variant, add_variant} variant = basic_variant;
2201 /* Try to do the computation three ways: multiply by the negative of OP1
2202 and then negate, do the multiplication directly, or do multiplication
2203 by OP1 - 1. */
2205 mult_cost = rtx_cost (gen_rtx_MULT (mode, op0, op1), SET);
2206 mult_cost = MIN (12 * add_cost, mult_cost);
2208 synth_mult (&alg, val, mult_cost);
2210 /* This works only if the inverted value actually fits in an
2211 `unsigned int' */
2212 if (HOST_BITS_PER_INT >= GET_MODE_BITSIZE (mode))
2214 synth_mult (&alg2, - val,
2215 (alg.cost < mult_cost ? alg.cost : mult_cost) - negate_cost);
2216 if (alg2.cost + negate_cost < alg.cost)
2217 alg = alg2, variant = negate_variant;
2220 /* This proves very useful for division-by-constant. */
2221 synth_mult (&alg2, val - 1,
2222 (alg.cost < mult_cost ? alg.cost : mult_cost) - add_cost);
2223 if (alg2.cost + add_cost < alg.cost)
2224 alg = alg2, variant = add_variant;
2226 if (alg.cost < mult_cost)
2228 /* We found something cheaper than a multiply insn. */
2229 int opno;
2230 rtx accum, tem;
2232 op0 = protect_from_queue (op0, 0);
2234 /* Avoid referencing memory over and over.
2235 For speed, but also for correctness when mem is volatile. */
2236 if (GET_CODE (op0) == MEM)
2237 op0 = force_reg (mode, op0);
2239 /* ACCUM starts out either as OP0 or as a zero, depending on
2240 the first operation. */
2242 if (alg.op[0] == alg_zero)
2244 accum = copy_to_mode_reg (mode, const0_rtx);
2245 val_so_far = 0;
2247 else if (alg.op[0] == alg_m)
2249 accum = copy_to_mode_reg (mode, op0);
2250 val_so_far = 1;
2252 else
2253 abort ();
2255 for (opno = 1; opno < alg.ops; opno++)
2257 int log = alg.log[opno];
2258 int preserve = preserve_subexpressions_p ();
2259 rtx shift_subtarget = preserve ? 0 : accum;
2260 rtx add_target
2261 = (opno == alg.ops - 1 && target != 0 && variant != add_variant
2262 && ! preserve)
2263 ? target : 0;
2264 rtx accum_target = preserve ? 0 : accum;
2266 switch (alg.op[opno])
2268 case alg_shift:
2269 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2270 build_int_2 (log, 0), NULL_RTX, 0);
2271 val_so_far <<= log;
2272 break;
2274 case alg_add_t_m2:
2275 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2276 build_int_2 (log, 0), NULL_RTX, 0);
2277 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2278 add_target
2279 ? add_target : accum_target);
2280 val_so_far += (HOST_WIDE_INT) 1 << log;
2281 break;
2283 case alg_sub_t_m2:
2284 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2285 build_int_2 (log, 0), NULL_RTX, 0);
2286 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
2287 add_target
2288 ? add_target : accum_target);
2289 val_so_far -= (HOST_WIDE_INT) 1 << log;
2290 break;
2292 case alg_add_t2_m:
2293 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2294 build_int_2 (log, 0), shift_subtarget,
2296 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
2297 add_target
2298 ? add_target : accum_target);
2299 val_so_far = (val_so_far << log) + 1;
2300 break;
2302 case alg_sub_t2_m:
2303 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2304 build_int_2 (log, 0), shift_subtarget,
2306 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
2307 add_target
2308 ? add_target : accum_target);
2309 val_so_far = (val_so_far << log) - 1;
2310 break;
2312 case alg_add_factor:
2313 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2314 build_int_2 (log, 0), NULL_RTX, 0);
2315 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2316 add_target
2317 ? add_target : accum_target);
2318 val_so_far += val_so_far << log;
2319 break;
2321 case alg_sub_factor:
2322 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2323 build_int_2 (log, 0), NULL_RTX, 0);
2324 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
2325 (add_target ? add_target
2326 : preserve ? 0 : tem));
2327 val_so_far = (val_so_far << log) - val_so_far;
2328 break;
2330 default:
2331 abort ();;
2334 /* Write a REG_EQUAL note on the last insn so that we can cse
2335 multiplication sequences. */
2337 insn = get_last_insn ();
2338 REG_NOTES (insn)
2339 = gen_rtx_EXPR_LIST (REG_EQUAL,
2340 gen_rtx_MULT (mode, op0,
2341 GEN_INT (val_so_far)),
2342 REG_NOTES (insn));
2345 if (variant == negate_variant)
2347 val_so_far = - val_so_far;
2348 accum = expand_unop (mode, neg_optab, accum, target, 0);
2350 else if (variant == add_variant)
2352 val_so_far = val_so_far + 1;
2353 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
2356 if (val != val_so_far)
2357 abort ();
2359 return accum;
2363 /* This used to use umul_optab if unsigned, but for non-widening multiply
2364 there is no difference between signed and unsigned. */
2365 op0 = expand_binop (mode, smul_optab,
2366 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
2367 if (op0 == 0)
2368 abort ();
2369 return op0;
2372 /* Return the smallest n such that 2**n >= X. */
2375 ceil_log2 (x)
2376 unsigned HOST_WIDE_INT x;
2378 return floor_log2 (x - 1) + 1;
2381 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
2382 replace division by D, and put the least significant N bits of the result
2383 in *MULTIPLIER_PTR and return the most significant bit.
2385 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
2386 needed precision is in PRECISION (should be <= N).
2388 PRECISION should be as small as possible so this function can choose
2389 multiplier more freely.
2391 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
2392 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
2394 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
2395 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
2397 static
2398 unsigned HOST_WIDE_INT
2399 choose_multiplier (d, n, precision, multiplier_ptr, post_shift_ptr, lgup_ptr)
2400 unsigned HOST_WIDE_INT d;
2401 int n;
2402 int precision;
2403 unsigned HOST_WIDE_INT *multiplier_ptr;
2404 int *post_shift_ptr;
2405 int *lgup_ptr;
2407 unsigned HOST_WIDE_INT mhigh_hi, mhigh_lo;
2408 unsigned HOST_WIDE_INT mlow_hi, mlow_lo;
2409 int lgup, post_shift;
2410 int pow, pow2;
2411 unsigned HOST_WIDE_INT nh, nl, dummy1, dummy2;
2413 /* lgup = ceil(log2(divisor)); */
2414 lgup = ceil_log2 (d);
2416 if (lgup > n)
2417 abort ();
2419 pow = n + lgup;
2420 pow2 = n + lgup - precision;
2422 if (pow == 2 * HOST_BITS_PER_WIDE_INT)
2424 /* We could handle this with some effort, but this case is much better
2425 handled directly with a scc insn, so rely on caller using that. */
2426 abort ();
2429 /* mlow = 2^(N + lgup)/d */
2430 if (pow >= HOST_BITS_PER_WIDE_INT)
2432 nh = (unsigned HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
2433 nl = 0;
2435 else
2437 nh = 0;
2438 nl = (unsigned HOST_WIDE_INT) 1 << pow;
2440 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
2441 &mlow_lo, &mlow_hi, &dummy1, &dummy2);
2443 /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
2444 if (pow2 >= HOST_BITS_PER_WIDE_INT)
2445 nh |= (unsigned HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
2446 else
2447 nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
2448 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
2449 &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
2451 if (mhigh_hi && nh - d >= d)
2452 abort ();
2453 if (mhigh_hi > 1 || mlow_hi > 1)
2454 abort ();
2455 /* assert that mlow < mhigh. */
2456 if (! (mlow_hi < mhigh_hi || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo)))
2457 abort();
2459 /* If precision == N, then mlow, mhigh exceed 2^N
2460 (but they do not exceed 2^(N+1)). */
2462 /* Reduce to lowest terms */
2463 for (post_shift = lgup; post_shift > 0; post_shift--)
2465 unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
2466 unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
2467 if (ml_lo >= mh_lo)
2468 break;
2470 mlow_hi = 0;
2471 mlow_lo = ml_lo;
2472 mhigh_hi = 0;
2473 mhigh_lo = mh_lo;
2476 *post_shift_ptr = post_shift;
2477 *lgup_ptr = lgup;
2478 if (n < HOST_BITS_PER_WIDE_INT)
2480 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
2481 *multiplier_ptr = mhigh_lo & mask;
2482 return mhigh_lo >= mask;
2484 else
2486 *multiplier_ptr = mhigh_lo;
2487 return mhigh_hi;
2491 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
2492 congruent to 1 (mod 2**N). */
2494 static unsigned HOST_WIDE_INT
2495 invert_mod2n (x, n)
2496 unsigned HOST_WIDE_INT x;
2497 int n;
2499 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
2501 /* The algorithm notes that the choice y = x satisfies
2502 x*y == 1 mod 2^3, since x is assumed odd.
2503 Each iteration doubles the number of bits of significance in y. */
2505 unsigned HOST_WIDE_INT mask;
2506 unsigned HOST_WIDE_INT y = x;
2507 int nbit = 3;
2509 mask = (n == HOST_BITS_PER_WIDE_INT
2510 ? ~(unsigned HOST_WIDE_INT) 0
2511 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
2513 while (nbit < n)
2515 y = y * (2 - x*y) & mask; /* Modulo 2^N */
2516 nbit *= 2;
2518 return y;
2521 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
2522 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
2523 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
2524 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
2525 become signed.
2527 The result is put in TARGET if that is convenient.
2529 MODE is the mode of operation. */
2532 expand_mult_highpart_adjust (mode, adj_operand, op0, op1, target, unsignedp)
2533 enum machine_mode mode;
2534 register rtx adj_operand, op0, op1, target;
2535 int unsignedp;
2537 rtx tem;
2538 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
2540 tem = expand_shift (RSHIFT_EXPR, mode, op0,
2541 build_int_2 (GET_MODE_BITSIZE (mode) - 1, 0),
2542 NULL_RTX, 0);
2543 tem = expand_and (tem, op1, NULL_RTX);
2544 adj_operand = force_operand (gen_rtx (adj_code, mode, adj_operand, tem),
2545 adj_operand);
2547 tem = expand_shift (RSHIFT_EXPR, mode, op1,
2548 build_int_2 (GET_MODE_BITSIZE (mode) - 1, 0),
2549 NULL_RTX, 0);
2550 tem = expand_and (tem, op0, NULL_RTX);
2551 target = force_operand (gen_rtx (adj_code, mode, adj_operand, tem), target);
2553 return target;
2556 /* Emit code to multiply OP0 and CNST1, putting the high half of the result
2557 in TARGET if that is convenient, and return where the result is. If the
2558 operation can not be performed, 0 is returned.
2560 MODE is the mode of operation and result.
2562 UNSIGNEDP nonzero means unsigned multiply.
2564 MAX_COST is the total allowed cost for the expanded RTL. */
2567 expand_mult_highpart (mode, op0, cnst1, target, unsignedp, max_cost)
2568 enum machine_mode mode;
2569 register rtx op0, target;
2570 unsigned HOST_WIDE_INT cnst1;
2571 int unsignedp;
2572 int max_cost;
2574 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
2575 optab mul_highpart_optab;
2576 optab moptab;
2577 rtx tem;
2578 int size = GET_MODE_BITSIZE (mode);
2579 rtx op1, wide_op1;
2581 /* We can't support modes wider than HOST_BITS_PER_INT. */
2582 if (size > HOST_BITS_PER_WIDE_INT)
2583 abort ();
2585 op1 = GEN_INT (cnst1);
2587 if (GET_MODE_BITSIZE (wider_mode) <= HOST_BITS_PER_INT)
2588 wide_op1 = op1;
2589 else
2590 wide_op1
2591 = immed_double_const (cnst1,
2592 (unsignedp
2593 ? (HOST_WIDE_INT) 0
2594 : -(cnst1 >> (HOST_BITS_PER_WIDE_INT - 1))),
2595 wider_mode);
2597 /* expand_mult handles constant multiplication of word_mode
2598 or narrower. It does a poor job for large modes. */
2599 if (size < BITS_PER_WORD
2600 && mul_cost[(int) wider_mode] + shift_cost[size-1] < max_cost)
2602 /* We have to do this, since expand_binop doesn't do conversion for
2603 multiply. Maybe change expand_binop to handle widening multiply? */
2604 op0 = convert_to_mode (wider_mode, op0, unsignedp);
2606 tem = expand_mult (wider_mode, op0, wide_op1, NULL_RTX, unsignedp);
2607 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2608 build_int_2 (size, 0), NULL_RTX, 1);
2609 return convert_modes (mode, wider_mode, tem, unsignedp);
2612 if (target == 0)
2613 target = gen_reg_rtx (mode);
2615 /* Firstly, try using a multiplication insn that only generates the needed
2616 high part of the product, and in the sign flavor of unsignedp. */
2617 if (mul_highpart_cost[(int) mode] < max_cost)
2619 mul_highpart_optab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
2620 target = expand_binop (mode, mul_highpart_optab,
2621 op0, wide_op1, target, unsignedp, OPTAB_DIRECT);
2622 if (target)
2623 return target;
2626 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
2627 Need to adjust the result after the multiplication. */
2628 if (mul_highpart_cost[(int) mode] + 2 * shift_cost[size-1] + 4 * add_cost < max_cost)
2630 mul_highpart_optab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
2631 target = expand_binop (mode, mul_highpart_optab,
2632 op0, wide_op1, target, unsignedp, OPTAB_DIRECT);
2633 if (target)
2634 /* We used the wrong signedness. Adjust the result. */
2635 return expand_mult_highpart_adjust (mode, target, op0,
2636 op1, target, unsignedp);
2639 /* Try widening multiplication. */
2640 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
2641 if (moptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
2642 && mul_widen_cost[(int) wider_mode] < max_cost)
2644 op1 = force_reg (mode, op1);
2645 goto try;
2648 /* Try widening the mode and perform a non-widening multiplication. */
2649 moptab = smul_optab;
2650 if (smul_optab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
2651 && mul_cost[(int) wider_mode] + shift_cost[size-1] < max_cost)
2653 op1 = wide_op1;
2654 goto try;
2657 /* Try widening multiplication of opposite signedness, and adjust. */
2658 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
2659 if (moptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
2660 && (mul_widen_cost[(int) wider_mode]
2661 + 2 * shift_cost[size-1] + 4 * add_cost < max_cost))
2663 rtx regop1 = force_reg (mode, op1);
2664 tem = expand_binop (wider_mode, moptab, op0, regop1,
2665 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
2666 if (tem != 0)
2668 /* Extract the high half of the just generated product. */
2669 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2670 build_int_2 (size, 0), NULL_RTX, 1);
2671 tem = convert_modes (mode, wider_mode, tem, unsignedp);
2672 /* We used the wrong signedness. Adjust the result. */
2673 return expand_mult_highpart_adjust (mode, tem, op0, op1,
2674 target, unsignedp);
2678 return 0;
2680 try:
2681 /* Pass NULL_RTX as target since TARGET has wrong mode. */
2682 tem = expand_binop (wider_mode, moptab, op0, op1,
2683 NULL_RTX, unsignedp, OPTAB_WIDEN);
2684 if (tem == 0)
2685 return 0;
2687 /* Extract the high half of the just generated product. */
2688 if (mode == word_mode)
2690 return gen_highpart (mode, tem);
2692 else
2694 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2695 build_int_2 (size, 0), NULL_RTX, 1);
2696 return convert_modes (mode, wider_mode, tem, unsignedp);
2700 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
2701 if that is convenient, and returning where the result is.
2702 You may request either the quotient or the remainder as the result;
2703 specify REM_FLAG nonzero to get the remainder.
2705 CODE is the expression code for which kind of division this is;
2706 it controls how rounding is done. MODE is the machine mode to use.
2707 UNSIGNEDP nonzero means do unsigned division. */
2709 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
2710 and then correct it by or'ing in missing high bits
2711 if result of ANDI is nonzero.
2712 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
2713 This could optimize to a bfexts instruction.
2714 But C doesn't use these operations, so their optimizations are
2715 left for later. */
2717 #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
2720 expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
2721 int rem_flag;
2722 enum tree_code code;
2723 enum machine_mode mode;
2724 register rtx op0, op1, target;
2725 int unsignedp;
2727 enum machine_mode compute_mode;
2728 register rtx tquotient;
2729 rtx quotient = 0, remainder = 0;
2730 rtx last;
2731 int size;
2732 rtx insn, set;
2733 optab optab1, optab2;
2734 int op1_is_constant, op1_is_pow2;
2735 int max_cost, extra_cost;
2736 static HOST_WIDE_INT last_div_const = 0;
2738 op1_is_constant = GET_CODE (op1) == CONST_INT;
2739 op1_is_pow2 = (op1_is_constant
2740 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
2741 || EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))));
2744 This is the structure of expand_divmod:
2746 First comes code to fix up the operands so we can perform the operations
2747 correctly and efficiently.
2749 Second comes a switch statement with code specific for each rounding mode.
2750 For some special operands this code emits all RTL for the desired
2751 operation, for other cases, it generates only a quotient and stores it in
2752 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
2753 to indicate that it has not done anything.
2755 Last comes code that finishes the operation. If QUOTIENT is set and
2756 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
2757 QUOTIENT is not set, it is computed using trunc rounding.
2759 We try to generate special code for division and remainder when OP1 is a
2760 constant. If |OP1| = 2**n we can use shifts and some other fast
2761 operations. For other values of OP1, we compute a carefully selected
2762 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
2763 by m.
2765 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
2766 half of the product. Different strategies for generating the product are
2767 implemented in expand_mult_highpart.
2769 If what we actually want is the remainder, we generate that by another
2770 by-constant multiplication and a subtraction. */
2772 /* We shouldn't be called with OP1 == const1_rtx, but some of the
2773 code below will malfunction if we are, so check here and handle
2774 the special case if so. */
2775 if (op1 == const1_rtx)
2776 return rem_flag ? const0_rtx : op0;
2778 if (target
2779 /* Don't use the function value register as a target
2780 since we have to read it as well as write it,
2781 and function-inlining gets confused by this. */
2782 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
2783 /* Don't clobber an operand while doing a multi-step calculation. */
2784 || ((rem_flag || op1_is_constant)
2785 && (reg_mentioned_p (target, op0)
2786 || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
2787 || reg_mentioned_p (target, op1)
2788 || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM)))
2789 target = 0;
2791 /* Get the mode in which to perform this computation. Normally it will
2792 be MODE, but sometimes we can't do the desired operation in MODE.
2793 If so, pick a wider mode in which we can do the operation. Convert
2794 to that mode at the start to avoid repeated conversions.
2796 First see what operations we need. These depend on the expression
2797 we are evaluating. (We assume that divxx3 insns exist under the
2798 same conditions that modxx3 insns and that these insns don't normally
2799 fail. If these assumptions are not correct, we may generate less
2800 efficient code in some cases.)
2802 Then see if we find a mode in which we can open-code that operation
2803 (either a division, modulus, or shift). Finally, check for the smallest
2804 mode for which we can do the operation with a library call. */
2806 /* We might want to refine this now that we have division-by-constant
2807 optimization. Since expand_mult_highpart tries so many variants, it is
2808 not straightforward to generalize this. Maybe we should make an array
2809 of possible modes in init_expmed? Save this for GCC 2.7. */
2811 optab1 = (op1_is_pow2 ? (unsignedp ? lshr_optab : ashr_optab)
2812 : (unsignedp ? udiv_optab : sdiv_optab));
2813 optab2 = (op1_is_pow2 ? optab1 : (unsignedp ? udivmod_optab : sdivmod_optab));
2815 for (compute_mode = mode; compute_mode != VOIDmode;
2816 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
2817 if (optab1->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing
2818 || optab2->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing)
2819 break;
2821 if (compute_mode == VOIDmode)
2822 for (compute_mode = mode; compute_mode != VOIDmode;
2823 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
2824 if (optab1->handlers[(int) compute_mode].libfunc
2825 || optab2->handlers[(int) compute_mode].libfunc)
2826 break;
2828 /* If we still couldn't find a mode, use MODE, but we'll probably abort
2829 in expand_binop. */
2830 if (compute_mode == VOIDmode)
2831 compute_mode = mode;
2833 if (target && GET_MODE (target) == compute_mode)
2834 tquotient = target;
2835 else
2836 tquotient = gen_reg_rtx (compute_mode);
2838 size = GET_MODE_BITSIZE (compute_mode);
2839 #if 0
2840 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
2841 (mode), and thereby get better code when OP1 is a constant. Do that
2842 later. It will require going over all usages of SIZE below. */
2843 size = GET_MODE_BITSIZE (mode);
2844 #endif
2846 /* Only deduct something for a REM if the last divide done was
2847 for a different constant. Then set the constant of the last
2848 divide. */
2849 max_cost = div_cost[(int) compute_mode]
2850 - (rem_flag && ! (last_div_const != 0 && op1_is_constant
2851 && INTVAL (op1) == last_div_const)
2852 ? mul_cost[(int) compute_mode] + add_cost : 0);
2854 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
2856 /* Now convert to the best mode to use. */
2857 if (compute_mode != mode)
2859 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
2860 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
2862 /* convert_modes may have placed op1 into a register, so we
2863 must recompute the following. */
2864 op1_is_constant = GET_CODE (op1) == CONST_INT;
2865 op1_is_pow2 = (op1_is_constant
2866 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
2867 || (! unsignedp
2868 && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
2871 /* If one of the operands is a volatile MEM, copy it into a register. */
2873 if (GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0))
2874 op0 = force_reg (compute_mode, op0);
2875 if (GET_CODE (op1) == MEM && MEM_VOLATILE_P (op1))
2876 op1 = force_reg (compute_mode, op1);
2878 /* If we need the remainder or if OP1 is constant, we need to
2879 put OP0 in a register in case it has any queued subexpressions. */
2880 if (rem_flag || op1_is_constant)
2881 op0 = force_reg (compute_mode, op0);
2883 last = get_last_insn ();
2885 /* Promote floor rounding to trunc rounding for unsigned operations. */
2886 if (unsignedp)
2888 if (code == FLOOR_DIV_EXPR)
2889 code = TRUNC_DIV_EXPR;
2890 if (code == FLOOR_MOD_EXPR)
2891 code = TRUNC_MOD_EXPR;
2894 if (op1 != const0_rtx)
2895 switch (code)
2897 case TRUNC_MOD_EXPR:
2898 case TRUNC_DIV_EXPR:
2899 if (op1_is_constant)
2901 if (unsignedp)
2903 unsigned HOST_WIDE_INT mh, ml;
2904 int pre_shift, post_shift;
2905 int dummy;
2906 unsigned HOST_WIDE_INT d = INTVAL (op1);
2908 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
2910 pre_shift = floor_log2 (d);
2911 if (rem_flag)
2913 remainder
2914 = expand_binop (compute_mode, and_optab, op0,
2915 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
2916 remainder, 1,
2917 OPTAB_LIB_WIDEN);
2918 if (remainder)
2919 return gen_lowpart (mode, remainder);
2921 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
2922 build_int_2 (pre_shift, 0),
2923 tquotient, 1);
2925 else if (size <= HOST_BITS_PER_WIDE_INT)
2927 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
2929 /* Most significant bit of divisor is set; emit an scc
2930 insn. */
2931 quotient = emit_store_flag (tquotient, GEU, op0, op1,
2932 compute_mode, 1, 1);
2933 if (quotient == 0)
2934 goto fail1;
2936 else
2938 /* Find a suitable multiplier and right shift count
2939 instead of multiplying with D. */
2941 mh = choose_multiplier (d, size, size,
2942 &ml, &post_shift, &dummy);
2944 /* If the suggested multiplier is more than SIZE bits,
2945 we can do better for even divisors, using an
2946 initial right shift. */
2947 if (mh != 0 && (d & 1) == 0)
2949 pre_shift = floor_log2 (d & -d);
2950 mh = choose_multiplier (d >> pre_shift, size,
2951 size - pre_shift,
2952 &ml, &post_shift, &dummy);
2953 if (mh)
2954 abort ();
2956 else
2957 pre_shift = 0;
2959 if (mh != 0)
2961 rtx t1, t2, t3, t4;
2963 extra_cost = (shift_cost[post_shift - 1]
2964 + shift_cost[1] + 2 * add_cost);
2965 t1 = expand_mult_highpart (compute_mode, op0, ml,
2966 NULL_RTX, 1,
2967 max_cost - extra_cost);
2968 if (t1 == 0)
2969 goto fail1;
2970 t2 = force_operand (gen_rtx_MINUS (compute_mode,
2971 op0, t1),
2972 NULL_RTX);
2973 t3 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
2974 build_int_2 (1, 0), NULL_RTX,1);
2975 t4 = force_operand (gen_rtx_PLUS (compute_mode,
2976 t1, t3),
2977 NULL_RTX);
2978 quotient
2979 = expand_shift (RSHIFT_EXPR, compute_mode, t4,
2980 build_int_2 (post_shift - 1, 0),
2981 tquotient, 1);
2983 else
2985 rtx t1, t2;
2987 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
2988 build_int_2 (pre_shift, 0),
2989 NULL_RTX, 1);
2990 extra_cost = (shift_cost[pre_shift]
2991 + shift_cost[post_shift]);
2992 t2 = expand_mult_highpart (compute_mode, t1, ml,
2993 NULL_RTX, 1,
2994 max_cost - extra_cost);
2995 if (t2 == 0)
2996 goto fail1;
2997 quotient
2998 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
2999 build_int_2 (post_shift, 0),
3000 tquotient, 1);
3004 else /* Too wide mode to use tricky code */
3005 break;
3007 insn = get_last_insn ();
3008 if (insn != last
3009 && (set = single_set (insn)) != 0
3010 && SET_DEST (set) == quotient)
3011 REG_NOTES (insn)
3012 = gen_rtx_EXPR_LIST (REG_EQUAL,
3013 gen_rtx_UDIV (compute_mode, op0, op1),
3014 REG_NOTES (insn));
3016 else /* TRUNC_DIV, signed */
3018 unsigned HOST_WIDE_INT ml;
3019 int lgup, post_shift;
3020 HOST_WIDE_INT d = INTVAL (op1);
3021 unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
3023 /* n rem d = n rem -d */
3024 if (rem_flag && d < 0)
3026 d = abs_d;
3027 op1 = GEN_INT (abs_d);
3030 if (d == 1)
3031 quotient = op0;
3032 else if (d == -1)
3033 quotient = expand_unop (compute_mode, neg_optab, op0,
3034 tquotient, 0);
3035 else if (abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
3037 /* This case is not handled correctly below. */
3038 quotient = emit_store_flag (tquotient, EQ, op0, op1,
3039 compute_mode, 1, 1);
3040 if (quotient == 0)
3041 goto fail1;
3043 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
3044 && (rem_flag ? smod_pow2_cheap : sdiv_pow2_cheap))
3046 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
3048 lgup = floor_log2 (abs_d);
3049 if (abs_d != 2 && BRANCH_COST < 3)
3051 rtx label = gen_label_rtx ();
3052 rtx t1;
3054 t1 = copy_to_mode_reg (compute_mode, op0);
3055 do_cmp_and_jump (t1, const0_rtx, GE,
3056 compute_mode, label);
3057 expand_inc (t1, GEN_INT (abs_d - 1));
3058 emit_label (label);
3059 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3060 build_int_2 (lgup, 0),
3061 tquotient, 0);
3063 else
3065 rtx t1, t2, t3;
3066 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3067 build_int_2 (size - 1, 0),
3068 NULL_RTX, 0);
3069 t2 = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3070 build_int_2 (size - lgup, 0),
3071 NULL_RTX, 1);
3072 t3 = force_operand (gen_rtx_PLUS (compute_mode,
3073 op0, t2),
3074 NULL_RTX);
3075 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t3,
3076 build_int_2 (lgup, 0),
3077 tquotient, 0);
3080 /* We have computed OP0 / abs(OP1). If OP1 is negative, negate
3081 the quotient. */
3082 if (d < 0)
3084 insn = get_last_insn ();
3085 if (insn != last
3086 && (set = single_set (insn)) != 0
3087 && SET_DEST (set) == quotient)
3088 REG_NOTES (insn)
3089 = gen_rtx_EXPR_LIST (REG_EQUAL,
3090 gen_rtx_DIV (compute_mode,
3091 op0,
3092 GEN_INT (abs_d)),
3093 REG_NOTES (insn));
3095 quotient = expand_unop (compute_mode, neg_optab,
3096 quotient, quotient, 0);
3099 else if (size <= HOST_BITS_PER_WIDE_INT)
3101 choose_multiplier (abs_d, size, size - 1,
3102 &ml, &post_shift, &lgup);
3103 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
3105 rtx t1, t2, t3;
3107 extra_cost = (shift_cost[post_shift]
3108 + shift_cost[size - 1] + add_cost);
3109 t1 = expand_mult_highpart (compute_mode, op0, ml,
3110 NULL_RTX, 0,
3111 max_cost - extra_cost);
3112 if (t1 == 0)
3113 goto fail1;
3114 t2 = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3115 build_int_2 (post_shift, 0), NULL_RTX, 0);
3116 t3 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3117 build_int_2 (size - 1, 0), NULL_RTX, 0);
3118 if (d < 0)
3119 quotient
3120 = force_operand (gen_rtx_MINUS (compute_mode,
3121 t3, t2),
3122 tquotient);
3123 else
3124 quotient
3125 = force_operand (gen_rtx_MINUS (compute_mode,
3126 t2, t3),
3127 tquotient);
3129 else
3131 rtx t1, t2, t3, t4;
3133 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
3134 extra_cost = (shift_cost[post_shift]
3135 + shift_cost[size - 1] + 2 * add_cost);
3136 t1 = expand_mult_highpart (compute_mode, op0, ml,
3137 NULL_RTX, 0,
3138 max_cost - extra_cost);
3139 if (t1 == 0)
3140 goto fail1;
3141 t2 = force_operand (gen_rtx_PLUS (compute_mode,
3142 t1, op0),
3143 NULL_RTX);
3144 t3 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3145 build_int_2 (post_shift, 0),
3146 NULL_RTX, 0);
3147 t4 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3148 build_int_2 (size - 1, 0),
3149 NULL_RTX, 0);
3150 if (d < 0)
3151 quotient
3152 = force_operand (gen_rtx_MINUS (compute_mode,
3153 t4, t3),
3154 tquotient);
3155 else
3156 quotient
3157 = force_operand (gen_rtx_MINUS (compute_mode,
3158 t3, t4),
3159 tquotient);
3162 else /* Too wide mode to use tricky code */
3163 break;
3165 insn = get_last_insn ();
3166 if (insn != last
3167 && (set = single_set (insn)) != 0
3168 && SET_DEST (set) == quotient)
3169 REG_NOTES (insn)
3170 = gen_rtx_EXPR_LIST (REG_EQUAL,
3171 gen_rtx_DIV (compute_mode, op0, op1),
3172 REG_NOTES (insn));
3174 break;
3176 fail1:
3177 delete_insns_since (last);
3178 break;
3180 case FLOOR_DIV_EXPR:
3181 case FLOOR_MOD_EXPR:
3182 /* We will come here only for signed operations. */
3183 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
3185 unsigned HOST_WIDE_INT mh, ml;
3186 int pre_shift, lgup, post_shift;
3187 HOST_WIDE_INT d = INTVAL (op1);
3189 if (d > 0)
3191 /* We could just as easily deal with negative constants here,
3192 but it does not seem worth the trouble for GCC 2.6. */
3193 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3195 pre_shift = floor_log2 (d);
3196 if (rem_flag)
3198 remainder = expand_binop (compute_mode, and_optab, op0,
3199 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3200 remainder, 0, OPTAB_LIB_WIDEN);
3201 if (remainder)
3202 return gen_lowpart (mode, remainder);
3204 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3205 build_int_2 (pre_shift, 0),
3206 tquotient, 0);
3208 else
3210 rtx t1, t2, t3, t4;
3212 mh = choose_multiplier (d, size, size - 1,
3213 &ml, &post_shift, &lgup);
3214 if (mh)
3215 abort ();
3217 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3218 build_int_2 (size - 1, 0), NULL_RTX, 0);
3219 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
3220 NULL_RTX, 0, OPTAB_WIDEN);
3221 extra_cost = (shift_cost[post_shift]
3222 + shift_cost[size - 1] + 2 * add_cost);
3223 t3 = expand_mult_highpart (compute_mode, t2, ml,
3224 NULL_RTX, 1,
3225 max_cost - extra_cost);
3226 if (t3 != 0)
3228 t4 = expand_shift (RSHIFT_EXPR, compute_mode, t3,
3229 build_int_2 (post_shift, 0),
3230 NULL_RTX, 1);
3231 quotient = expand_binop (compute_mode, xor_optab,
3232 t4, t1, tquotient, 0,
3233 OPTAB_WIDEN);
3237 else
3239 rtx nsign, t1, t2, t3, t4;
3240 t1 = force_operand (gen_rtx_PLUS (compute_mode,
3241 op0, constm1_rtx), NULL_RTX);
3242 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
3243 0, OPTAB_WIDEN);
3244 nsign = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3245 build_int_2 (size - 1, 0), NULL_RTX, 0);
3246 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
3247 NULL_RTX);
3248 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
3249 NULL_RTX, 0);
3250 if (t4)
3252 rtx t5;
3253 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
3254 NULL_RTX, 0);
3255 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3256 t4, t5),
3257 tquotient);
3262 if (quotient != 0)
3263 break;
3264 delete_insns_since (last);
3266 /* Try using an instruction that produces both the quotient and
3267 remainder, using truncation. We can easily compensate the quotient
3268 or remainder to get floor rounding, once we have the remainder.
3269 Notice that we compute also the final remainder value here,
3270 and return the result right away. */
3271 if (target == 0 || GET_MODE (target) != compute_mode)
3272 target = gen_reg_rtx (compute_mode);
3274 if (rem_flag)
3276 remainder
3277 = GET_CODE (target) == REG ? target : gen_reg_rtx (compute_mode);
3278 quotient = gen_reg_rtx (compute_mode);
3280 else
3282 quotient
3283 = GET_CODE (target) == REG ? target : gen_reg_rtx (compute_mode);
3284 remainder = gen_reg_rtx (compute_mode);
3287 if (expand_twoval_binop (sdivmod_optab, op0, op1,
3288 quotient, remainder, 0))
3290 /* This could be computed with a branch-less sequence.
3291 Save that for later. */
3292 rtx tem;
3293 rtx label = gen_label_rtx ();
3294 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
3295 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3296 NULL_RTX, 0, OPTAB_WIDEN);
3297 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
3298 expand_dec (quotient, const1_rtx);
3299 expand_inc (remainder, op1);
3300 emit_label (label);
3301 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3304 /* No luck with division elimination or divmod. Have to do it
3305 by conditionally adjusting op0 *and* the result. */
3307 rtx label1, label2, label3, label4, label5;
3308 rtx adjusted_op0;
3309 rtx tem;
3311 quotient = gen_reg_rtx (compute_mode);
3312 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3313 label1 = gen_label_rtx ();
3314 label2 = gen_label_rtx ();
3315 label3 = gen_label_rtx ();
3316 label4 = gen_label_rtx ();
3317 label5 = gen_label_rtx ();
3318 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
3319 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
3320 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3321 quotient, 0, OPTAB_LIB_WIDEN);
3322 if (tem != quotient)
3323 emit_move_insn (quotient, tem);
3324 emit_jump_insn (gen_jump (label5));
3325 emit_barrier ();
3326 emit_label (label1);
3327 expand_inc (adjusted_op0, const1_rtx);
3328 emit_jump_insn (gen_jump (label4));
3329 emit_barrier ();
3330 emit_label (label2);
3331 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
3332 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3333 quotient, 0, OPTAB_LIB_WIDEN);
3334 if (tem != quotient)
3335 emit_move_insn (quotient, tem);
3336 emit_jump_insn (gen_jump (label5));
3337 emit_barrier ();
3338 emit_label (label3);
3339 expand_dec (adjusted_op0, const1_rtx);
3340 emit_label (label4);
3341 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3342 quotient, 0, OPTAB_LIB_WIDEN);
3343 if (tem != quotient)
3344 emit_move_insn (quotient, tem);
3345 expand_dec (quotient, const1_rtx);
3346 emit_label (label5);
3348 break;
3350 case CEIL_DIV_EXPR:
3351 case CEIL_MOD_EXPR:
3352 if (unsignedp)
3354 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
3356 rtx t1, t2, t3;
3357 unsigned HOST_WIDE_INT d = INTVAL (op1);
3358 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3359 build_int_2 (floor_log2 (d), 0),
3360 tquotient, 1);
3361 t2 = expand_binop (compute_mode, and_optab, op0,
3362 GEN_INT (d - 1),
3363 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3364 t3 = gen_reg_rtx (compute_mode);
3365 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
3366 compute_mode, 1, 1);
3367 if (t3 == 0)
3369 rtx lab;
3370 lab = gen_label_rtx ();
3371 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
3372 expand_inc (t1, const1_rtx);
3373 emit_label (lab);
3374 quotient = t1;
3376 else
3377 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3378 t1, t3),
3379 tquotient);
3380 break;
3383 /* Try using an instruction that produces both the quotient and
3384 remainder, using truncation. We can easily compensate the
3385 quotient or remainder to get ceiling rounding, once we have the
3386 remainder. Notice that we compute also the final remainder
3387 value here, and return the result right away. */
3388 if (target == 0 || GET_MODE (target) != compute_mode)
3389 target = gen_reg_rtx (compute_mode);
3391 if (rem_flag)
3393 remainder = (GET_CODE (target) == REG
3394 ? target : gen_reg_rtx (compute_mode));
3395 quotient = gen_reg_rtx (compute_mode);
3397 else
3399 quotient = (GET_CODE (target) == REG
3400 ? target : gen_reg_rtx (compute_mode));
3401 remainder = gen_reg_rtx (compute_mode);
3404 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
3405 remainder, 1))
3407 /* This could be computed with a branch-less sequence.
3408 Save that for later. */
3409 rtx label = gen_label_rtx ();
3410 do_cmp_and_jump (remainder, const0_rtx, EQ,
3411 compute_mode, label);
3412 expand_inc (quotient, const1_rtx);
3413 expand_dec (remainder, op1);
3414 emit_label (label);
3415 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3418 /* No luck with division elimination or divmod. Have to do it
3419 by conditionally adjusting op0 *and* the result. */
3421 rtx label1, label2;
3422 rtx adjusted_op0, tem;
3424 quotient = gen_reg_rtx (compute_mode);
3425 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3426 label1 = gen_label_rtx ();
3427 label2 = gen_label_rtx ();
3428 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
3429 compute_mode, label1);
3430 emit_move_insn (quotient, const0_rtx);
3431 emit_jump_insn (gen_jump (label2));
3432 emit_barrier ();
3433 emit_label (label1);
3434 expand_dec (adjusted_op0, const1_rtx);
3435 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
3436 quotient, 1, OPTAB_LIB_WIDEN);
3437 if (tem != quotient)
3438 emit_move_insn (quotient, tem);
3439 expand_inc (quotient, const1_rtx);
3440 emit_label (label2);
3443 else /* signed */
3445 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3446 && INTVAL (op1) >= 0)
3448 /* This is extremely similar to the code for the unsigned case
3449 above. For 2.7 we should merge these variants, but for
3450 2.6.1 I don't want to touch the code for unsigned since that
3451 get used in C. The signed case will only be used by other
3452 languages (Ada). */
3454 rtx t1, t2, t3;
3455 unsigned HOST_WIDE_INT d = INTVAL (op1);
3456 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3457 build_int_2 (floor_log2 (d), 0),
3458 tquotient, 0);
3459 t2 = expand_binop (compute_mode, and_optab, op0,
3460 GEN_INT (d - 1),
3461 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3462 t3 = gen_reg_rtx (compute_mode);
3463 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
3464 compute_mode, 1, 1);
3465 if (t3 == 0)
3467 rtx lab;
3468 lab = gen_label_rtx ();
3469 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
3470 expand_inc (t1, const1_rtx);
3471 emit_label (lab);
3472 quotient = t1;
3474 else
3475 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3476 t1, t3),
3477 tquotient);
3478 break;
3481 /* Try using an instruction that produces both the quotient and
3482 remainder, using truncation. We can easily compensate the
3483 quotient or remainder to get ceiling rounding, once we have the
3484 remainder. Notice that we compute also the final remainder
3485 value here, and return the result right away. */
3486 if (target == 0 || GET_MODE (target) != compute_mode)
3487 target = gen_reg_rtx (compute_mode);
3488 if (rem_flag)
3490 remainder= (GET_CODE (target) == REG
3491 ? target : gen_reg_rtx (compute_mode));
3492 quotient = gen_reg_rtx (compute_mode);
3494 else
3496 quotient = (GET_CODE (target) == REG
3497 ? target : gen_reg_rtx (compute_mode));
3498 remainder = gen_reg_rtx (compute_mode);
3501 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
3502 remainder, 0))
3504 /* This could be computed with a branch-less sequence.
3505 Save that for later. */
3506 rtx tem;
3507 rtx label = gen_label_rtx ();
3508 do_cmp_and_jump (remainder, const0_rtx, EQ,
3509 compute_mode, label);
3510 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3511 NULL_RTX, 0, OPTAB_WIDEN);
3512 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
3513 expand_inc (quotient, const1_rtx);
3514 expand_dec (remainder, op1);
3515 emit_label (label);
3516 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3519 /* No luck with division elimination or divmod. Have to do it
3520 by conditionally adjusting op0 *and* the result. */
3522 rtx label1, label2, label3, label4, label5;
3523 rtx adjusted_op0;
3524 rtx tem;
3526 quotient = gen_reg_rtx (compute_mode);
3527 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3528 label1 = gen_label_rtx ();
3529 label2 = gen_label_rtx ();
3530 label3 = gen_label_rtx ();
3531 label4 = gen_label_rtx ();
3532 label5 = gen_label_rtx ();
3533 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
3534 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
3535 compute_mode, label1);
3536 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3537 quotient, 0, OPTAB_LIB_WIDEN);
3538 if (tem != quotient)
3539 emit_move_insn (quotient, tem);
3540 emit_jump_insn (gen_jump (label5));
3541 emit_barrier ();
3542 emit_label (label1);
3543 expand_dec (adjusted_op0, const1_rtx);
3544 emit_jump_insn (gen_jump (label4));
3545 emit_barrier ();
3546 emit_label (label2);
3547 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
3548 compute_mode, label3);
3549 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3550 quotient, 0, OPTAB_LIB_WIDEN);
3551 if (tem != quotient)
3552 emit_move_insn (quotient, tem);
3553 emit_jump_insn (gen_jump (label5));
3554 emit_barrier ();
3555 emit_label (label3);
3556 expand_inc (adjusted_op0, const1_rtx);
3557 emit_label (label4);
3558 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3559 quotient, 0, OPTAB_LIB_WIDEN);
3560 if (tem != quotient)
3561 emit_move_insn (quotient, tem);
3562 expand_inc (quotient, const1_rtx);
3563 emit_label (label5);
3566 break;
3568 case EXACT_DIV_EXPR:
3569 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
3571 HOST_WIDE_INT d = INTVAL (op1);
3572 unsigned HOST_WIDE_INT ml;
3573 int post_shift;
3574 rtx t1;
3576 post_shift = floor_log2 (d & -d);
3577 ml = invert_mod2n (d >> post_shift, size);
3578 t1 = expand_mult (compute_mode, op0, GEN_INT (ml), NULL_RTX,
3579 unsignedp);
3580 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3581 build_int_2 (post_shift, 0),
3582 NULL_RTX, unsignedp);
3584 insn = get_last_insn ();
3585 REG_NOTES (insn)
3586 = gen_rtx_EXPR_LIST (REG_EQUAL,
3587 gen_rtx (unsignedp ? UDIV : DIV,
3588 compute_mode, op0, op1),
3589 REG_NOTES (insn));
3591 break;
3593 case ROUND_DIV_EXPR:
3594 case ROUND_MOD_EXPR:
3595 if (unsignedp)
3597 rtx tem;
3598 rtx label;
3599 label = gen_label_rtx ();
3600 quotient = gen_reg_rtx (compute_mode);
3601 remainder = gen_reg_rtx (compute_mode);
3602 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
3604 rtx tem;
3605 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
3606 quotient, 1, OPTAB_LIB_WIDEN);
3607 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
3608 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
3609 remainder, 1, OPTAB_LIB_WIDEN);
3611 tem = plus_constant (op1, -1);
3612 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
3613 build_int_2 (1, 0), NULL_RTX, 1);
3614 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
3615 expand_inc (quotient, const1_rtx);
3616 expand_dec (remainder, op1);
3617 emit_label (label);
3619 else
3621 rtx abs_rem, abs_op1, tem, mask;
3622 rtx label;
3623 label = gen_label_rtx ();
3624 quotient = gen_reg_rtx (compute_mode);
3625 remainder = gen_reg_rtx (compute_mode);
3626 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
3628 rtx tem;
3629 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
3630 quotient, 0, OPTAB_LIB_WIDEN);
3631 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
3632 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
3633 remainder, 0, OPTAB_LIB_WIDEN);
3635 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 0, 0);
3636 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 0, 0);
3637 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
3638 build_int_2 (1, 0), NULL_RTX, 1);
3639 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
3640 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3641 NULL_RTX, 0, OPTAB_WIDEN);
3642 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
3643 build_int_2 (size - 1, 0), NULL_RTX, 0);
3644 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
3645 NULL_RTX, 0, OPTAB_WIDEN);
3646 tem = expand_binop (compute_mode, sub_optab, tem, mask,
3647 NULL_RTX, 0, OPTAB_WIDEN);
3648 expand_inc (quotient, tem);
3649 tem = expand_binop (compute_mode, xor_optab, mask, op1,
3650 NULL_RTX, 0, OPTAB_WIDEN);
3651 tem = expand_binop (compute_mode, sub_optab, tem, mask,
3652 NULL_RTX, 0, OPTAB_WIDEN);
3653 expand_dec (remainder, tem);
3654 emit_label (label);
3656 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3658 default:
3659 abort ();
3662 if (quotient == 0)
3664 if (target && GET_MODE (target) != compute_mode)
3665 target = 0;
3667 if (rem_flag)
3669 /* Try to produce the remainder directly without a library call. */
3670 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
3671 op0, op1, target,
3672 unsignedp, OPTAB_WIDEN);
3673 if (remainder == 0)
3675 /* No luck there. Can we do remainder and divide at once
3676 without a library call? */
3677 remainder = gen_reg_rtx (compute_mode);
3678 if (! expand_twoval_binop ((unsignedp
3679 ? udivmod_optab
3680 : sdivmod_optab),
3681 op0, op1,
3682 NULL_RTX, remainder, unsignedp))
3683 remainder = 0;
3686 if (remainder)
3687 return gen_lowpart (mode, remainder);
3690 /* Produce the quotient. Try a quotient insn, but not a library call.
3691 If we have a divmod in this mode, use it in preference to widening
3692 the div (for this test we assume it will not fail). Note that optab2
3693 is set to the one of the two optabs that the call below will use. */
3694 quotient
3695 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
3696 op0, op1, rem_flag ? NULL_RTX : target,
3697 unsignedp,
3698 ((optab2->handlers[(int) compute_mode].insn_code
3699 != CODE_FOR_nothing)
3700 ? OPTAB_DIRECT : OPTAB_WIDEN));
3702 if (quotient == 0)
3704 /* No luck there. Try a quotient-and-remainder insn,
3705 keeping the quotient alone. */
3706 quotient = gen_reg_rtx (compute_mode);
3707 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
3708 op0, op1,
3709 quotient, NULL_RTX, unsignedp))
3711 quotient = 0;
3712 if (! rem_flag)
3713 /* Still no luck. If we are not computing the remainder,
3714 use a library call for the quotient. */
3715 quotient = sign_expand_binop (compute_mode,
3716 udiv_optab, sdiv_optab,
3717 op0, op1, target,
3718 unsignedp, OPTAB_LIB_WIDEN);
3723 if (rem_flag)
3725 if (target && GET_MODE (target) != compute_mode)
3726 target = 0;
3728 if (quotient == 0)
3729 /* No divide instruction either. Use library for remainder. */
3730 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
3731 op0, op1, target,
3732 unsignedp, OPTAB_LIB_WIDEN);
3733 else
3735 /* We divided. Now finish doing X - Y * (X / Y). */
3736 remainder = expand_mult (compute_mode, quotient, op1,
3737 NULL_RTX, unsignedp);
3738 remainder = expand_binop (compute_mode, sub_optab, op0,
3739 remainder, target, unsignedp,
3740 OPTAB_LIB_WIDEN);
3744 return gen_lowpart (mode, rem_flag ? remainder : quotient);
3747 /* Return a tree node with data type TYPE, describing the value of X.
3748 Usually this is an RTL_EXPR, if there is no obvious better choice.
3749 X may be an expression, however we only support those expressions
3750 generated by loop.c. */
3752 tree
3753 make_tree (type, x)
3754 tree type;
3755 rtx x;
3757 tree t;
3759 switch (GET_CODE (x))
3761 case CONST_INT:
3762 t = build_int_2 (INTVAL (x),
3763 TREE_UNSIGNED (type) || INTVAL (x) >= 0 ? 0 : -1);
3764 TREE_TYPE (t) = type;
3765 return t;
3767 case CONST_DOUBLE:
3768 if (GET_MODE (x) == VOIDmode)
3770 t = build_int_2 (CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
3771 TREE_TYPE (t) = type;
3773 else
3775 REAL_VALUE_TYPE d;
3777 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
3778 t = build_real (type, d);
3781 return t;
3783 case PLUS:
3784 return fold (build (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
3785 make_tree (type, XEXP (x, 1))));
3787 case MINUS:
3788 return fold (build (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
3789 make_tree (type, XEXP (x, 1))));
3791 case NEG:
3792 return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
3794 case MULT:
3795 return fold (build (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
3796 make_tree (type, XEXP (x, 1))));
3798 case ASHIFT:
3799 return fold (build (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
3800 make_tree (type, XEXP (x, 1))));
3802 case LSHIFTRT:
3803 return fold (convert (type,
3804 build (RSHIFT_EXPR, unsigned_type (type),
3805 make_tree (unsigned_type (type),
3806 XEXP (x, 0)),
3807 make_tree (type, XEXP (x, 1)))));
3809 case ASHIFTRT:
3810 return fold (convert (type,
3811 build (RSHIFT_EXPR, signed_type (type),
3812 make_tree (signed_type (type), XEXP (x, 0)),
3813 make_tree (type, XEXP (x, 1)))));
3815 case DIV:
3816 if (TREE_CODE (type) != REAL_TYPE)
3817 t = signed_type (type);
3818 else
3819 t = type;
3821 return fold (convert (type,
3822 build (TRUNC_DIV_EXPR, t,
3823 make_tree (t, XEXP (x, 0)),
3824 make_tree (t, XEXP (x, 1)))));
3825 case UDIV:
3826 t = unsigned_type (type);
3827 return fold (convert (type,
3828 build (TRUNC_DIV_EXPR, t,
3829 make_tree (t, XEXP (x, 0)),
3830 make_tree (t, XEXP (x, 1)))));
3831 default:
3832 t = make_node (RTL_EXPR);
3833 TREE_TYPE (t) = type;
3834 RTL_EXPR_RTL (t) = x;
3835 /* There are no insns to be output
3836 when this rtl_expr is used. */
3837 RTL_EXPR_SEQUENCE (t) = 0;
3838 return t;
3842 /* Return an rtx representing the value of X * MULT + ADD.
3843 TARGET is a suggestion for where to store the result (an rtx).
3844 MODE is the machine mode for the computation.
3845 X and MULT must have mode MODE. ADD may have a different mode.
3846 So can X (defaults to same as MODE).
3847 UNSIGNEDP is non-zero to do unsigned multiplication.
3848 This may emit insns. */
3851 expand_mult_add (x, target, mult, add, mode, unsignedp)
3852 rtx x, target, mult, add;
3853 enum machine_mode mode;
3854 int unsignedp;
3856 tree type = type_for_mode (mode, unsignedp);
3857 tree add_type = (GET_MODE (add) == VOIDmode
3858 ? type : type_for_mode (GET_MODE (add), unsignedp));
3859 tree result = fold (build (PLUS_EXPR, type,
3860 fold (build (MULT_EXPR, type,
3861 make_tree (type, x),
3862 make_tree (type, mult))),
3863 make_tree (add_type, add)));
3865 return expand_expr (result, target, VOIDmode, 0);
3868 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
3869 and returning TARGET.
3871 If TARGET is 0, a pseudo-register or constant is returned. */
3874 expand_and (op0, op1, target)
3875 rtx op0, op1, target;
3877 enum machine_mode mode = VOIDmode;
3878 rtx tem;
3880 if (GET_MODE (op0) != VOIDmode)
3881 mode = GET_MODE (op0);
3882 else if (GET_MODE (op1) != VOIDmode)
3883 mode = GET_MODE (op1);
3885 if (mode != VOIDmode)
3886 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
3887 else if (GET_CODE (op0) == CONST_INT && GET_CODE (op1) == CONST_INT)
3888 tem = GEN_INT (INTVAL (op0) & INTVAL (op1));
3889 else
3890 abort ();
3892 if (target == 0)
3893 target = tem;
3894 else if (tem != target)
3895 emit_move_insn (target, tem);
3896 return target;
3899 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
3900 and storing in TARGET. Normally return TARGET.
3901 Return 0 if that cannot be done.
3903 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
3904 it is VOIDmode, they cannot both be CONST_INT.
3906 UNSIGNEDP is for the case where we have to widen the operands
3907 to perform the operation. It says to use zero-extension.
3909 NORMALIZEP is 1 if we should convert the result to be either zero
3910 or one. Normalize is -1 if we should convert the result to be
3911 either zero or -1. If NORMALIZEP is zero, the result will be left
3912 "raw" out of the scc insn. */
3915 emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep)
3916 rtx target;
3917 enum rtx_code code;
3918 rtx op0, op1;
3919 enum machine_mode mode;
3920 int unsignedp;
3921 int normalizep;
3923 rtx subtarget;
3924 enum insn_code icode;
3925 enum machine_mode compare_mode;
3926 enum machine_mode target_mode = GET_MODE (target);
3927 rtx tem;
3928 rtx last = get_last_insn ();
3929 rtx pattern, comparison;
3931 /* If one operand is constant, make it the second one. Only do this
3932 if the other operand is not constant as well. */
3934 if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
3935 || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
3937 tem = op0;
3938 op0 = op1;
3939 op1 = tem;
3940 code = swap_condition (code);
3943 if (mode == VOIDmode)
3944 mode = GET_MODE (op0);
3946 /* For some comparisons with 1 and -1, we can convert this to
3947 comparisons with zero. This will often produce more opportunities for
3948 store-flag insns. */
3950 switch (code)
3952 case LT:
3953 if (op1 == const1_rtx)
3954 op1 = const0_rtx, code = LE;
3955 break;
3956 case LE:
3957 if (op1 == constm1_rtx)
3958 op1 = const0_rtx, code = LT;
3959 break;
3960 case GE:
3961 if (op1 == const1_rtx)
3962 op1 = const0_rtx, code = GT;
3963 break;
3964 case GT:
3965 if (op1 == constm1_rtx)
3966 op1 = const0_rtx, code = GE;
3967 break;
3968 case GEU:
3969 if (op1 == const1_rtx)
3970 op1 = const0_rtx, code = NE;
3971 break;
3972 case LTU:
3973 if (op1 == const1_rtx)
3974 op1 = const0_rtx, code = EQ;
3975 break;
3976 default:
3977 break;
3980 /* From now on, we won't change CODE, so set ICODE now. */
3981 icode = setcc_gen_code[(int) code];
3983 /* If this is A < 0 or A >= 0, we can do this by taking the ones
3984 complement of A (for GE) and shifting the sign bit to the low bit. */
3985 if (op1 == const0_rtx && (code == LT || code == GE)
3986 && GET_MODE_CLASS (mode) == MODE_INT
3987 && (normalizep || STORE_FLAG_VALUE == 1
3988 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3989 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
3990 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
3992 subtarget = target;
3994 /* If the result is to be wider than OP0, it is best to convert it
3995 first. If it is to be narrower, it is *incorrect* to convert it
3996 first. */
3997 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
3999 op0 = protect_from_queue (op0, 0);
4000 op0 = convert_modes (target_mode, mode, op0, 0);
4001 mode = target_mode;
4004 if (target_mode != mode)
4005 subtarget = 0;
4007 if (code == GE)
4008 op0 = expand_unop (mode, one_cmpl_optab, op0,
4009 ((STORE_FLAG_VALUE == 1 || normalizep)
4010 ? 0 : subtarget), 0);
4012 if (STORE_FLAG_VALUE == 1 || normalizep)
4013 /* If we are supposed to produce a 0/1 value, we want to do
4014 a logical shift from the sign bit to the low-order bit; for
4015 a -1/0 value, we do an arithmetic shift. */
4016 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
4017 size_int (GET_MODE_BITSIZE (mode) - 1),
4018 subtarget, normalizep != -1);
4020 if (mode != target_mode)
4021 op0 = convert_modes (target_mode, mode, op0, 0);
4023 return op0;
4026 if (icode != CODE_FOR_nothing)
4028 /* We think we may be able to do this with a scc insn. Emit the
4029 comparison and then the scc insn.
4031 compare_from_rtx may call emit_queue, which would be deleted below
4032 if the scc insn fails. So call it ourselves before setting LAST. */
4034 emit_queue ();
4035 last = get_last_insn ();
4037 comparison
4038 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX, 0);
4039 if (GET_CODE (comparison) == CONST_INT)
4040 return (comparison == const0_rtx ? const0_rtx
4041 : normalizep == 1 ? const1_rtx
4042 : normalizep == -1 ? constm1_rtx
4043 : const_true_rtx);
4045 /* If the code of COMPARISON doesn't match CODE, something is
4046 wrong; we can no longer be sure that we have the operation.
4047 We could handle this case, but it should not happen. */
4049 if (GET_CODE (comparison) != code)
4050 abort ();
4052 /* Get a reference to the target in the proper mode for this insn. */
4053 compare_mode = insn_operand_mode[(int) icode][0];
4054 subtarget = target;
4055 if (preserve_subexpressions_p ()
4056 || ! (*insn_operand_predicate[(int) icode][0]) (subtarget, compare_mode))
4057 subtarget = gen_reg_rtx (compare_mode);
4059 pattern = GEN_FCN (icode) (subtarget);
4060 if (pattern)
4062 emit_insn (pattern);
4064 /* If we are converting to a wider mode, first convert to
4065 TARGET_MODE, then normalize. This produces better combining
4066 opportunities on machines that have a SIGN_EXTRACT when we are
4067 testing a single bit. This mostly benefits the 68k.
4069 If STORE_FLAG_VALUE does not have the sign bit set when
4070 interpreted in COMPARE_MODE, we can do this conversion as
4071 unsigned, which is usually more efficient. */
4072 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
4074 convert_move (target, subtarget,
4075 (GET_MODE_BITSIZE (compare_mode)
4076 <= HOST_BITS_PER_WIDE_INT)
4077 && 0 == (STORE_FLAG_VALUE
4078 & ((HOST_WIDE_INT) 1
4079 << (GET_MODE_BITSIZE (compare_mode) -1))));
4080 op0 = target;
4081 compare_mode = target_mode;
4083 else
4084 op0 = subtarget;
4086 /* If we want to keep subexpressions around, don't reuse our
4087 last target. */
4089 if (preserve_subexpressions_p ())
4090 subtarget = 0;
4092 /* Now normalize to the proper value in COMPARE_MODE. Sometimes
4093 we don't have to do anything. */
4094 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
4096 /* STORE_FLAG_VALUE might be the most negative number, so write
4097 the comparison this way to avoid a compiler-time warning. */
4098 else if (- normalizep == STORE_FLAG_VALUE)
4099 op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
4101 /* We don't want to use STORE_FLAG_VALUE < 0 below since this
4102 makes it hard to use a value of just the sign bit due to
4103 ANSI integer constant typing rules. */
4104 else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
4105 && (STORE_FLAG_VALUE
4106 & ((HOST_WIDE_INT) 1
4107 << (GET_MODE_BITSIZE (compare_mode) - 1))))
4108 op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
4109 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
4110 subtarget, normalizep == 1);
4111 else if (STORE_FLAG_VALUE & 1)
4113 op0 = expand_and (op0, const1_rtx, subtarget);
4114 if (normalizep == -1)
4115 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
4117 else
4118 abort ();
4120 /* If we were converting to a smaller mode, do the
4121 conversion now. */
4122 if (target_mode != compare_mode)
4124 convert_move (target, op0, 0);
4125 return target;
4127 else
4128 return op0;
4132 delete_insns_since (last);
4134 /* If expensive optimizations, use different pseudo registers for each
4135 insn, instead of reusing the same pseudo. This leads to better CSE,
4136 but slows down the compiler, since there are more pseudos */
4137 subtarget = (!flag_expensive_optimizations
4138 && (target_mode == mode)) ? target : NULL_RTX;
4140 /* If we reached here, we can't do this with a scc insn. However, there
4141 are some comparisons that can be done directly. For example, if
4142 this is an equality comparison of integers, we can try to exclusive-or
4143 (or subtract) the two operands and use a recursive call to try the
4144 comparison with zero. Don't do any of these cases if branches are
4145 very cheap. */
4147 if (BRANCH_COST > 0
4148 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
4149 && op1 != const0_rtx)
4151 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
4152 OPTAB_WIDEN);
4154 if (tem == 0)
4155 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
4156 OPTAB_WIDEN);
4157 if (tem != 0)
4158 tem = emit_store_flag (target, code, tem, const0_rtx,
4159 mode, unsignedp, normalizep);
4160 if (tem == 0)
4161 delete_insns_since (last);
4162 return tem;
4165 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
4166 the constant zero. Reject all other comparisons at this point. Only
4167 do LE and GT if branches are expensive since they are expensive on
4168 2-operand machines. */
4170 if (BRANCH_COST == 0
4171 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
4172 || (code != EQ && code != NE
4173 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
4174 return 0;
4176 /* See what we need to return. We can only return a 1, -1, or the
4177 sign bit. */
4179 if (normalizep == 0)
4181 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4182 normalizep = STORE_FLAG_VALUE;
4184 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4185 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4186 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
4188 else
4189 return 0;
4192 /* Try to put the result of the comparison in the sign bit. Assume we can't
4193 do the necessary operation below. */
4195 tem = 0;
4197 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
4198 the sign bit set. */
4200 if (code == LE)
4202 /* This is destructive, so SUBTARGET can't be OP0. */
4203 if (rtx_equal_p (subtarget, op0))
4204 subtarget = 0;
4206 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
4207 OPTAB_WIDEN);
4208 if (tem)
4209 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
4210 OPTAB_WIDEN);
4213 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
4214 number of bits in the mode of OP0, minus one. */
4216 if (code == GT)
4218 if (rtx_equal_p (subtarget, op0))
4219 subtarget = 0;
4221 tem = expand_shift (RSHIFT_EXPR, mode, op0,
4222 size_int (GET_MODE_BITSIZE (mode) - 1),
4223 subtarget, 0);
4224 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
4225 OPTAB_WIDEN);
4228 if (code == EQ || code == NE)
4230 /* For EQ or NE, one way to do the comparison is to apply an operation
4231 that converts the operand into a positive number if it is non-zero
4232 or zero if it was originally zero. Then, for EQ, we subtract 1 and
4233 for NE we negate. This puts the result in the sign bit. Then we
4234 normalize with a shift, if needed.
4236 Two operations that can do the above actions are ABS and FFS, so try
4237 them. If that doesn't work, and MODE is smaller than a full word,
4238 we can use zero-extension to the wider mode (an unsigned conversion)
4239 as the operation. */
4241 if (abs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
4242 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
4243 else if (ffs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
4244 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
4245 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
4247 op0 = protect_from_queue (op0, 0);
4248 tem = convert_modes (word_mode, mode, op0, 1);
4249 mode = word_mode;
4252 if (tem != 0)
4254 if (code == EQ)
4255 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
4256 0, OPTAB_WIDEN);
4257 else
4258 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
4261 /* If we couldn't do it that way, for NE we can "or" the two's complement
4262 of the value with itself. For EQ, we take the one's complement of
4263 that "or", which is an extra insn, so we only handle EQ if branches
4264 are expensive. */
4266 if (tem == 0 && (code == NE || BRANCH_COST > 1))
4268 if (rtx_equal_p (subtarget, op0))
4269 subtarget = 0;
4271 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
4272 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
4273 OPTAB_WIDEN);
4275 if (tem && code == EQ)
4276 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
4280 if (tem && normalizep)
4281 tem = expand_shift (RSHIFT_EXPR, mode, tem,
4282 size_int (GET_MODE_BITSIZE (mode) - 1),
4283 subtarget, normalizep == 1);
4285 if (tem)
4287 if (GET_MODE (tem) != target_mode)
4289 convert_move (target, tem, 0);
4290 tem = target;
4292 else if (!subtarget)
4294 emit_move_insn (target, tem);
4295 tem = target;
4298 else
4299 delete_insns_since (last);
4301 return tem;
4304 /* Like emit_store_flag, but always succeeds. */
4307 emit_store_flag_force (target, code, op0, op1, mode, unsignedp, normalizep)
4308 rtx target;
4309 enum rtx_code code;
4310 rtx op0, op1;
4311 enum machine_mode mode;
4312 int unsignedp;
4313 int normalizep;
4315 rtx tem, label;
4317 /* First see if emit_store_flag can do the job. */
4318 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
4319 if (tem != 0)
4320 return tem;
4322 if (normalizep == 0)
4323 normalizep = 1;
4325 /* If this failed, we have to do this with set/compare/jump/set code. */
4327 if (GET_CODE (target) != REG
4328 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
4329 target = gen_reg_rtx (GET_MODE (target));
4331 emit_move_insn (target, const1_rtx);
4332 tem = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX, 0);
4333 if (GET_CODE (tem) == CONST_INT)
4334 return tem;
4336 label = gen_label_rtx ();
4337 if (bcc_gen_fctn[(int) code] == 0)
4338 abort ();
4340 emit_jump_insn ((*bcc_gen_fctn[(int) code]) (label));
4341 emit_move_insn (target, const0_rtx);
4342 emit_label (label);
4344 return target;
4347 /* Perform possibly multi-word comparison and conditional jump to LABEL
4348 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE
4350 The algorithm is based on the code in expr.c:do_jump.
4352 Note that this does not perform a general comparison. Only variants
4353 generated within expmed.c are correctly handled, others abort (but could
4354 be handled if needed). */
4356 static void
4357 do_cmp_and_jump (arg1, arg2, op, mode, label)
4358 rtx arg1, arg2, label;
4359 enum rtx_code op;
4360 enum machine_mode mode;
4362 /* If this mode is an integer too wide to compare properly,
4363 compare word by word. Rely on cse to optimize constant cases. */
4365 if (GET_MODE_CLASS (mode) == MODE_INT && !can_compare_p (mode))
4367 rtx label2 = gen_label_rtx ();
4369 switch (op)
4371 case LTU:
4372 do_jump_by_parts_greater_rtx (mode, 1, arg2, arg1, label2, label);
4373 break;
4375 case LEU:
4376 do_jump_by_parts_greater_rtx (mode, 1, arg1, arg2, label, label2);
4377 break;
4379 case LT:
4380 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label2, label);
4381 break;
4383 case GT:
4384 do_jump_by_parts_greater_rtx (mode, 0, arg1, arg2, label2, label);
4385 break;
4387 case GE:
4388 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label, label2);
4389 break;
4391 /* do_jump_by_parts_equality_rtx compares with zero. Luckily
4392 that's the only equality operations we do */
4393 case EQ:
4394 if (arg2 != const0_rtx || mode != GET_MODE(arg1))
4395 abort();
4396 do_jump_by_parts_equality_rtx (arg1, label2, label);
4397 break;
4399 case NE:
4400 if (arg2 != const0_rtx || mode != GET_MODE(arg1))
4401 abort();
4402 do_jump_by_parts_equality_rtx (arg1, label, label2);
4403 break;
4405 default:
4406 abort();
4409 emit_label (label2);
4411 else
4413 emit_cmp_insn(arg1, arg2, op, NULL_RTX, mode, 0, 0);
4414 if (bcc_gen_fctn[(int) op] == 0)
4415 abort ();
4416 emit_jump_insn ((*bcc_gen_fctn[(int) op]) (label));