OSDN Git Service

(NEEDS_UNTYPED_CALL): Define.
[pf3gnuchains/gcc-fork.git] / gcc / expmed.c
1 /* Medium-level subroutines: convert bit-field store and extract
2    and shifts, multiplies and divides to rtl instructions.
3    Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
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.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21
22 #include "config.h"
23 #include "rtl.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "insn-flags.h"
27 #include "insn-codes.h"
28 #include "insn-config.h"
29 #include "expr.h"
30 #include "real.h"
31 #include "recog.h"
32
33 static rtx extract_split_bit_field ();
34 static rtx extract_fixed_bit_field ();
35 static void store_split_bit_field ();
36 static void store_fixed_bit_field ();
37 static rtx mask_rtx ();
38 static rtx lshift_value ();
39
40 #define CEIL(x,y) (((x) + (y) - 1) / (y))
41
42 /* Non-zero means multiply instructions are cheaper than shifts.  */
43 int mult_is_very_cheap;
44
45 /* Non-zero means divides or modulus operations are relatively cheap for
46    powers of two, so don't use branches; emit the operation instead. 
47    Usually, this will mean that the MD file will emit non-branch
48    sequences.  */
49
50 static int sdiv_pow2_cheap, smod_pow2_cheap;
51
52 /* Cost of various pieces of RTL.  */
53 static int add_cost, mult_cost, negate_cost, zero_cost;
54 static int shift_cost[BITS_PER_WORD];
55 static int shiftadd_cost[BITS_PER_WORD];
56 static int shiftsub_cost[BITS_PER_WORD];
57
58 void
59 init_expmed ()
60 {
61   char *free_point;
62   /* This is "some random pseudo register" for purposes of calling recog
63      to see what insns exist.  */
64   rtx reg = gen_rtx (REG, word_mode, FIRST_PSEUDO_REGISTER);
65   rtx shift_insn, shiftadd_insn, shiftsub_insn;
66   int dummy;
67   int m;
68
69   start_sequence ();
70
71   /* Since we are on the permanent obstack, we must be sure we save this
72      spot AFTER we call start_sequence, since it will reuse the rtl it
73      makes.  */
74
75   free_point = (char *) oballoc (0);
76
77   zero_cost = rtx_cost (const0_rtx);
78   add_cost = rtx_cost (gen_rtx (PLUS, word_mode, reg, reg), SET);
79
80   shift_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
81                                    gen_rtx (ASHIFT, word_mode, reg,
82                                             const0_rtx)));
83
84   shiftadd_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
85                                       gen_rtx (PLUS, word_mode,
86                                                gen_rtx (MULT, word_mode,
87                                                         reg, const0_rtx),
88                                                reg)));
89
90   shiftsub_insn = emit_insn (gen_rtx (SET, VOIDmode, reg,
91                                       gen_rtx (MINUS, word_mode,
92                                                gen_rtx (MULT, word_mode,
93                                                          reg, const0_rtx),
94                                                 reg)));
95
96   init_recog ();
97
98   shift_cost[0] = 0;
99   shiftadd_cost[0] = shiftsub_cost[0] = add_cost;
100
101   for (m = 1; m < BITS_PER_WORD; m++)
102     {
103       shift_cost[m] = shiftadd_cost[m] = shiftsub_cost[m] = 32000;
104
105       XEXP (SET_SRC (PATTERN (shift_insn)), 1) = GEN_INT (m);
106       if (recog (PATTERN (shift_insn), shift_insn, &dummy) >= 0)
107         shift_cost[m] = rtx_cost (SET_SRC (PATTERN (shift_insn)), SET);
108
109       XEXP (XEXP (SET_SRC (PATTERN (shiftadd_insn)), 0), 1)
110         = GEN_INT ((HOST_WIDE_INT) 1 << m);
111       if (recog (PATTERN (shiftadd_insn), shiftadd_insn, &dummy) >= 0)
112         shiftadd_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftadd_insn)), SET);
113
114       XEXP (XEXP (SET_SRC (PATTERN (shiftsub_insn)), 0), 1)
115         = GEN_INT ((HOST_WIDE_INT) 1 << m);
116       if (recog (PATTERN (shiftsub_insn), shiftsub_insn, &dummy) >= 0)
117         shiftsub_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftsub_insn)), SET);
118     }
119
120   mult_cost = rtx_cost (gen_rtx (MULT, word_mode, reg, reg), SET);
121   negate_cost = rtx_cost (gen_rtx (NEG, word_mode, reg), SET);
122
123   /* 999999 is chosen to avoid any plausible faster special case.  */
124   mult_is_very_cheap
125     = (rtx_cost (gen_rtx (MULT, word_mode, reg, GEN_INT (999999)), SET)
126        < rtx_cost (gen_rtx (ASHIFT, word_mode, reg, GEN_INT (7)), SET));
127
128   sdiv_pow2_cheap
129     = (rtx_cost (gen_rtx (DIV, word_mode, reg, GEN_INT (32)), SET)
130        <= 2 * add_cost);
131   smod_pow2_cheap
132     = (rtx_cost (gen_rtx (MOD, word_mode, reg, GEN_INT (32)), SET)
133        <= 2 * add_cost);
134
135   /* Free the objects we just allocated.  */
136   end_sequence ();
137   obfree (free_point);
138 }
139
140 /* Return an rtx representing minus the value of X.
141    MODE is the intended mode of the result,
142    useful if X is a CONST_INT.  */
143
144 rtx
145 negate_rtx (mode, x)
146      enum machine_mode mode;
147      rtx x;
148 {
149   if (GET_CODE (x) == CONST_INT)
150     {
151       HOST_WIDE_INT val = - INTVAL (x);
152       if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
153         {
154           /* Sign extend the value from the bits that are significant.  */
155           if (val & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
156             val |= (HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (mode);
157           else
158             val &= ((HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (mode)) - 1;
159         }
160       return GEN_INT (val);
161     }
162   else
163     return expand_unop (GET_MODE (x), neg_optab, x, NULL_RTX, 0);
164 }
165 \f
166 /* Generate code to store value from rtx VALUE
167    into a bit-field within structure STR_RTX
168    containing BITSIZE bits starting at bit BITNUM.
169    FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
170    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
171    TOTAL_SIZE is the size of the structure in bytes, or -1 if varying.  */
172
173 /* ??? Note that there are two different ideas here for how
174    to determine the size to count bits within, for a register.
175    One is BITS_PER_WORD, and the other is the size of operand 3
176    of the insv pattern.  (The latter assumes that an n-bit machine
177    will be able to insert bit fields up to n bits wide.)
178    It isn't certain that either of these is right.
179    extract_bit_field has the same quandary.  */
180
181 rtx
182 store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, align, total_size)
183      rtx str_rtx;
184      register int bitsize;
185      int bitnum;
186      enum machine_mode fieldmode;
187      rtx value;
188      int align;
189      int total_size;
190 {
191   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
192   register int offset = bitnum / unit;
193   register int bitpos = bitnum % unit;
194   register rtx op0 = str_rtx;
195
196   if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
197     abort ();
198
199   /* Discount the part of the structure before the desired byte.
200      We need to know how many bytes are safe to reference after it.  */
201   if (total_size >= 0)
202     total_size -= (bitpos / BIGGEST_ALIGNMENT
203                    * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
204
205   while (GET_CODE (op0) == SUBREG)
206     {
207       /* The following line once was done only if WORDS_BIG_ENDIAN,
208          but I think that is a mistake.  WORDS_BIG_ENDIAN is
209          meaningful at a much higher level; when structures are copied
210          between memory and regs, the higher-numbered regs
211          always get higher addresses.  */
212       offset += SUBREG_WORD (op0);
213       /* We used to adjust BITPOS here, but now we do the whole adjustment
214          right after the loop.  */
215       op0 = SUBREG_REG (op0);
216     }
217
218 #if BYTES_BIG_ENDIAN
219   /* If OP0 is a register, BITPOS must count within a word.
220      But as we have it, it counts within whatever size OP0 now has.
221      On a bigendian machine, these are not the same, so convert.  */
222   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
223     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
224 #endif
225
226   value = protect_from_queue (value, 0);
227
228   if (flag_force_mem)
229     value = force_not_mem (value);
230
231   /* Note that the adjustment of BITPOS above has no effect on whether
232      BITPOS is 0 in a REG bigger than a word.  */
233   if (GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
234       && (! STRICT_ALIGNMENT || GET_CODE (op0) != MEM)
235       && bitpos == 0 && bitsize == GET_MODE_BITSIZE (fieldmode))
236     {
237       /* Storing in a full-word or multi-word field in a register
238          can be done with just SUBREG.  */
239       if (GET_MODE (op0) != fieldmode)
240         if (GET_CODE (op0) == REG)
241           op0 = gen_rtx (SUBREG, fieldmode, op0, offset);
242         else
243           op0 = change_address (op0, fieldmode,
244                                 plus_constant (XEXP (op0, 0), offset));
245       emit_move_insn (op0, value);
246       return value;
247     }
248
249   /* Storing an lsb-aligned field in a register
250      can be done with a movestrict instruction.  */
251
252   if (GET_CODE (op0) != MEM
253 #if BYTES_BIG_ENDIAN
254       && bitpos + bitsize == unit
255 #else
256       && bitpos == 0
257 #endif
258       && bitsize == GET_MODE_BITSIZE (fieldmode)
259       && (GET_MODE (op0) == fieldmode
260           || (movstrict_optab->handlers[(int) fieldmode].insn_code
261               != CODE_FOR_nothing)))
262     {
263       /* Get appropriate low part of the value being stored.  */
264       if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
265         value = gen_lowpart (fieldmode, value);
266       else if (!(GET_CODE (value) == SYMBOL_REF
267                  || GET_CODE (value) == LABEL_REF
268                  || GET_CODE (value) == CONST))
269         value = convert_to_mode (fieldmode, value, 0);
270
271       if (GET_MODE (op0) == fieldmode)
272         emit_move_insn (op0, value);
273       else
274         {
275           int icode = movstrict_optab->handlers[(int) fieldmode].insn_code;
276           if(! (*insn_operand_predicate[icode][1]) (value, fieldmode))
277             value = copy_to_mode_reg (fieldmode, value);
278           emit_insn (GEN_FCN (icode)
279                    (gen_rtx (SUBREG, fieldmode, op0, offset), value));
280         }
281       return value;
282     }
283
284   /* Handle fields bigger than a word.  */
285
286   if (bitsize > BITS_PER_WORD)
287     {
288       /* Here we transfer the words of the field
289          in the order least significant first.
290          This is because the most significant word is the one which may
291          be less than full.  */
292
293       int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
294       int i;
295
296       /* This is the mode we must force value to, so that there will be enough
297          subwords to extract.  Note that fieldmode will often (always?) be
298          VOIDmode, because that is what store_field uses to indicate that this
299          is a bit field, but passing VOIDmode to operand_subword_force will
300          result in an abort.  */
301       fieldmode = mode_for_size (nwords * BITS_PER_WORD, MODE_INT, 0);
302
303       for (i = 0; i < nwords; i++)
304         {
305           /* If I is 0, use the low-order word in both field and target;
306              if I is 1, use the next to lowest word; and so on.  */
307           int wordnum = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
308           int bit_offset = (WORDS_BIG_ENDIAN
309                             ? MAX (bitsize - (i + 1) * BITS_PER_WORD, 0)
310                             : i * BITS_PER_WORD);
311           store_bit_field (op0, MIN (BITS_PER_WORD,
312                                      bitsize - i * BITS_PER_WORD),
313                            bitnum + bit_offset, word_mode,
314                            operand_subword_force (value, wordnum, fieldmode),
315                            align, total_size);
316         }
317       return value;
318     }
319
320   /* From here on we can assume that the field to be stored in is
321      a full-word (whatever type that is), since it is shorter than a word.  */
322
323   /* OFFSET is the number of words or bytes (UNIT says which)
324      from STR_RTX to the first word or byte containing part of the field.  */
325
326   if (GET_CODE (op0) == REG)
327     {
328       if (offset != 0
329           || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
330         op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
331                        op0, offset);
332       offset = 0;
333     }
334   else
335     {
336       op0 = protect_from_queue (op0, 1);
337     }
338
339   /* Now OFFSET is nonzero only if OP0 is memory
340      and is therefore always measured in bytes.  */
341
342 #ifdef HAVE_insv
343   if (HAVE_insv
344       && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
345       /* Ensure insv's size is wide enough for this field.  */
346       && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_insv][3])
347           >= bitsize))
348     {
349       int xbitpos = bitpos;
350       rtx value1;
351       rtx xop0 = op0;
352       rtx last = get_last_insn ();
353       rtx pat;
354       enum machine_mode maxmode
355         = insn_operand_mode[(int) CODE_FOR_insv][3];
356
357       int save_volatile_ok = volatile_ok;
358       volatile_ok = 1;
359
360       /* If this machine's insv can only insert into a register, or if we
361          are to force MEMs into a register, copy OP0 into a register and
362          save it back later.  */
363       if (GET_CODE (op0) == MEM
364           && (flag_force_mem
365               || ! ((*insn_operand_predicate[(int) CODE_FOR_insv][0])
366                     (op0, VOIDmode))))
367         {
368           rtx tempreg;
369           enum machine_mode bestmode;
370
371           /* Get the mode to use for inserting into this field.  If OP0 is
372              BLKmode, get the smallest mode consistent with the alignment. If
373              OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
374              mode. Otherwise, use the smallest mode containing the field.  */
375
376           if (GET_MODE (op0) == BLKmode
377               || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
378             bestmode
379               = get_best_mode (bitsize, bitnum, align * BITS_PER_UNIT, maxmode,
380                                MEM_VOLATILE_P (op0));
381           else
382             bestmode = GET_MODE (op0);
383
384           if (bestmode == VOIDmode)
385             goto insv_loses;
386
387           /* Adjust address to point to the containing unit of that mode.  */
388           unit = GET_MODE_BITSIZE (bestmode);
389           /* Compute offset as multiple of this unit, counting in bytes.  */
390           offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
391           bitpos = bitnum % unit;
392           op0 = change_address (op0, bestmode, 
393                                 plus_constant (XEXP (op0, 0), offset));
394
395           /* Fetch that unit, store the bitfield in it, then store the unit.  */
396           tempreg = copy_to_reg (op0);
397           store_bit_field (tempreg, bitsize, bitpos, fieldmode, value,
398                            align, total_size);
399           emit_move_insn (op0, tempreg);
400           return value;
401         }
402       volatile_ok = save_volatile_ok;
403
404       /* Add OFFSET into OP0's address.  */
405       if (GET_CODE (xop0) == MEM)
406         xop0 = change_address (xop0, byte_mode,
407                                plus_constant (XEXP (xop0, 0), offset));
408
409       /* If xop0 is a register, we need it in MAXMODE
410          to make it acceptable to the format of insv.  */
411       if (GET_CODE (xop0) == SUBREG)
412         PUT_MODE (xop0, maxmode);
413       if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
414         xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
415
416       /* On big-endian machines, we count bits from the most significant.
417          If the bit field insn does not, we must invert.  */
418
419 #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
420       xbitpos = unit - bitsize - xbitpos;
421 #endif
422       /* We have been counting XBITPOS within UNIT.
423          Count instead within the size of the register.  */
424 #if BITS_BIG_ENDIAN
425       if (GET_CODE (xop0) != MEM)
426         xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
427 #endif
428       unit = GET_MODE_BITSIZE (maxmode);
429
430       /* Convert VALUE to maxmode (which insv insn wants) in VALUE1.  */
431       value1 = value;
432       if (GET_MODE (value) != maxmode)
433         {
434           if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
435             {
436               /* Optimization: Don't bother really extending VALUE
437                  if it has all the bits we will actually use.  However,
438                  if we must narrow it, be sure we do it correctly.  */
439
440               if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
441                 {
442                   /* Avoid making subreg of a subreg, or of a mem.  */
443                   if (GET_CODE (value1) != REG)
444                 value1 = copy_to_reg (value1);
445                   value1 = gen_rtx (SUBREG, maxmode, value1, 0);
446                 }
447               else
448                 value1 = gen_lowpart (maxmode, value1);
449             }
450           else if (!CONSTANT_P (value))
451             /* Parse phase is supposed to make VALUE's data type
452                match that of the component reference, which is a type
453                at least as wide as the field; so VALUE should have
454                a mode that corresponds to that type.  */
455             abort ();
456         }
457
458       /* If this machine's insv insists on a register,
459          get VALUE1 into a register.  */
460       if (! ((*insn_operand_predicate[(int) CODE_FOR_insv][3])
461              (value1, maxmode)))
462         value1 = force_reg (maxmode, value1);
463
464       pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
465       if (pat)
466         emit_insn (pat);
467       else
468         {
469           delete_insns_since (last);
470           store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
471         }
472     }
473   else
474     insv_loses:
475 #endif
476     /* Insv is not available; store using shifts and boolean ops.  */
477     store_fixed_bit_field (op0, offset, bitsize, bitpos, value, align);
478   return value;
479 }
480 \f
481 /* Use shifts and boolean operations to store VALUE
482    into a bit field of width BITSIZE
483    in a memory location specified by OP0 except offset by OFFSET bytes.
484      (OFFSET must be 0 if OP0 is a register.)
485    The field starts at position BITPOS within the byte.
486     (If OP0 is a register, it may be a full word or a narrower mode,
487      but BITPOS still counts within a full word,
488      which is significant on bigendian machines.)
489    STRUCT_ALIGN is the alignment the structure is known to have (in bytes).
490
491    Note that protect_from_queue has already been done on OP0 and VALUE.  */
492
493 static void
494 store_fixed_bit_field (op0, offset, bitsize, bitpos, value, struct_align)
495      register rtx op0;
496      register int offset, bitsize, bitpos;
497      register rtx value;
498      int struct_align;
499 {
500   register enum machine_mode mode;
501   int total_bits = BITS_PER_WORD;
502   rtx subtarget, temp;
503   int all_zero = 0;
504   int all_one = 0;
505
506   /* Add OFFSET to OP0's address (if it is in memory)
507      and if a single byte contains the whole bit field
508      change OP0 to a byte.  */
509
510   /* There is a case not handled here:
511      a structure with a known alignment of just a halfword
512      and a field split across two aligned halfwords within the structure.
513      Or likewise a structure with a known alignment of just a byte
514      and a field split across two bytes.
515      Such cases are not supposed to be able to occur.  */
516
517   if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
518     {
519       if (offset != 0)
520         abort ();
521       /* Special treatment for a bit field split across two registers.  */
522       if (bitsize + bitpos > BITS_PER_WORD)
523         {
524           store_split_bit_field (op0, bitsize, bitpos, value, BITS_PER_WORD);
525           return;
526         }
527     }
528   else
529     {
530       /* Get the proper mode to use for this field.  We want a mode that
531          includes the entire field.  If such a mode would be larger than
532          a word, we won't be doing the extraction the normal way.  */
533
534       mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
535                             struct_align * BITS_PER_UNIT, word_mode,
536                             GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
537
538       if (mode == VOIDmode)
539         {
540           /* The only way this should occur is if the field spans word
541              boundaries.  */
542           store_split_bit_field (op0, bitsize, bitpos + offset * BITS_PER_UNIT,
543                                  value, struct_align);
544           return;
545         }
546
547       total_bits = GET_MODE_BITSIZE (mode);
548
549       /* Get ref to an aligned byte, halfword, or word containing the field.
550          Adjust BITPOS to be position within a word,
551          and OFFSET to be the offset of that word.
552          Then alter OP0 to refer to that word.  */
553       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
554       offset -= (offset % (total_bits / BITS_PER_UNIT));
555       op0 = change_address (op0, mode,
556                             plus_constant (XEXP (op0, 0), offset));
557     }
558
559   mode = GET_MODE (op0);
560
561   /* Now MODE is either some integral mode for a MEM as OP0,
562      or is a full-word for a REG as OP0.  TOTAL_BITS corresponds.
563      The bit field is contained entirely within OP0.
564      BITPOS is the starting bit number within OP0.
565      (OP0's mode may actually be narrower than MODE.)  */
566
567 #if BYTES_BIG_ENDIAN
568   /* BITPOS is the distance between our msb
569      and that of the containing datum.
570      Convert it to the distance from the lsb.  */
571
572   bitpos = total_bits - bitsize - bitpos;
573 #endif
574   /* Now BITPOS is always the distance between our lsb
575      and that of OP0.  */
576
577   /* Shift VALUE left by BITPOS bits.  If VALUE is not constant,
578      we must first convert its mode to MODE.  */
579
580   if (GET_CODE (value) == CONST_INT)
581     {
582       register HOST_WIDE_INT v = INTVAL (value);
583
584       if (bitsize < HOST_BITS_PER_WIDE_INT)
585         v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
586
587       if (v == 0)
588         all_zero = 1;
589       else if ((bitsize < HOST_BITS_PER_WIDE_INT
590                 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
591                || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
592         all_one = 1;
593
594       value = lshift_value (mode, value, bitpos, bitsize);
595     }
596   else
597     {
598       int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
599                       && bitpos + bitsize != GET_MODE_BITSIZE (mode));
600
601       if (GET_MODE (value) != mode)
602         {
603           /* If VALUE is a floating-point mode, access it as an integer
604              of the corresponding size, then convert it.  This can occur on
605              a machine with 64 bit registers that uses SFmode for float.  */
606           if (GET_MODE_CLASS (GET_MODE (value)) == MODE_FLOAT)
607             {
608               if (GET_CODE (value) != REG)
609                 value = copy_to_reg (value);
610               value
611                 = gen_rtx (SUBREG, word_mode, value, 0);
612             }
613
614           if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
615               && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
616             value = gen_lowpart (mode, value);
617           else
618             value = convert_to_mode (mode, value, 1);
619         }
620
621       if (must_and)
622         value = expand_binop (mode, and_optab, value,
623                               mask_rtx (mode, 0, bitsize, 0),
624                               NULL_RTX, 1, OPTAB_LIB_WIDEN);
625       if (bitpos > 0)
626         value = expand_shift (LSHIFT_EXPR, mode, value,
627                               build_int_2 (bitpos, 0), NULL_RTX, 1);
628     }
629
630   /* Now clear the chosen bits in OP0,
631      except that if VALUE is -1 we need not bother.  */
632
633   subtarget = (GET_CODE (op0) == REG || ! flag_force_mem) ? op0 : 0;
634
635   if (! all_one)
636     {
637       temp = expand_binop (mode, and_optab, op0,
638                            mask_rtx (mode, bitpos, bitsize, 1),
639                            subtarget, 1, OPTAB_LIB_WIDEN);
640       subtarget = temp;
641     }
642   else
643     temp = op0;
644
645   /* Now logical-or VALUE into OP0, unless it is zero.  */
646
647   if (! all_zero)
648     temp = expand_binop (mode, ior_optab, temp, value,
649                          subtarget, 1, OPTAB_LIB_WIDEN);
650   if (op0 != temp)
651     emit_move_insn (op0, temp);
652 }
653 \f
654 /* Store a bit field that is split across two words.
655
656    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
657    BITSIZE is the field width; BITPOS the position of its first bit
658    (within the word).
659    VALUE is the value to store.  */
660
661 static void
662 store_split_bit_field (op0, bitsize, bitpos, value, align)
663      rtx op0;
664      int bitsize, bitpos;
665      rtx value;
666      int align;
667 {
668   /* BITSIZE_1 is size of the part in the first word.  */
669   int bitsize_1 = BITS_PER_WORD - bitpos % BITS_PER_WORD;
670   /* BITSIZE_2 is size of the rest (in the following word).  */
671   int bitsize_2 = bitsize - bitsize_1;
672   rtx part1, part2;
673   int unit = GET_CODE (op0) == MEM ? BITS_PER_UNIT : BITS_PER_WORD;
674   int offset = bitpos / unit;
675   rtx word;
676
677   /* The field must span exactly one word boundary.  */
678   if (bitpos / BITS_PER_WORD != (bitpos + bitsize - 1) / BITS_PER_WORD - 1)
679     abort ();
680
681   if (GET_MODE (value) != VOIDmode)
682     value = convert_to_mode (word_mode, value, 1);
683   if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
684     value = copy_to_reg (value);
685
686   /* Split the value into two parts:
687      PART1 gets that which goes in the first word; PART2 the other.  */
688 #if BYTES_BIG_ENDIAN
689   /* PART1 gets the more significant part.  */
690   if (GET_CODE (value) == CONST_INT)
691     {
692       part1 = GEN_INT ((unsigned HOST_WIDE_INT) (INTVAL (value)) >> bitsize_2);
693       part2 = GEN_INT ((unsigned HOST_WIDE_INT) (INTVAL (value))
694                        & (((HOST_WIDE_INT) 1 << bitsize_2) - 1));
695     }
696   else
697     {
698       part1 = extract_fixed_bit_field (word_mode, value, 0, bitsize_1,
699                                        BITS_PER_WORD - bitsize, NULL_RTX, 1,
700                                        BITS_PER_WORD);
701       part2 = extract_fixed_bit_field (word_mode, value, 0, bitsize_2,
702                                        BITS_PER_WORD - bitsize_2, NULL_RTX, 1,
703                                        BITS_PER_WORD);
704     }
705 #else
706   /* PART1 gets the less significant part.  */
707   if (GET_CODE (value) == CONST_INT)
708     {
709       part1 = GEN_INT ((unsigned HOST_WIDE_INT) (INTVAL (value))
710                        & (((HOST_WIDE_INT) 1 << bitsize_1) - 1));
711       part2 = GEN_INT ((unsigned HOST_WIDE_INT) (INTVAL (value)) >> bitsize_1);
712     }
713   else
714     {
715       part1 = extract_fixed_bit_field (word_mode, value, 0, bitsize_1, 0,
716                                        NULL_RTX, 1, BITS_PER_WORD);
717       part2 = extract_fixed_bit_field (word_mode, value, 0, bitsize_2,
718                                        bitsize_1, NULL_RTX, 1, BITS_PER_WORD);
719     }
720 #endif
721
722   /* Store PART1 into the first word.  If OP0 is a MEM, pass OP0 and the
723      offset computed above.  Otherwise, get the proper word and pass an
724      offset of zero.  */
725   word = (GET_CODE (op0) == MEM ? op0
726           : operand_subword (op0, offset, 1, GET_MODE (op0)));
727   if (word == 0)
728     abort ();
729
730   store_fixed_bit_field (word, GET_CODE (op0) == MEM ? offset : 0,
731                          bitsize_1, bitpos % unit, part1, align);
732
733   /* Offset op0 by 1 word to get to the following one.  */
734   if (GET_CODE (op0) == SUBREG)
735     word = operand_subword (SUBREG_REG (op0), SUBREG_WORD (op0) + offset + 1,
736                             1, VOIDmode);
737   else if (GET_CODE (op0) == MEM)
738     word = op0;
739   else
740     word = operand_subword (op0, offset + 1, 1, GET_MODE (op0));
741
742   if (word == 0)
743     abort ();
744
745   /* Store PART2 into the second word.  */
746   store_fixed_bit_field (word,
747                          (GET_CODE (op0) == MEM
748                           ? CEIL (offset + 1, UNITS_PER_WORD) * UNITS_PER_WORD
749                           : 0),
750                          bitsize_2, 0, part2, align);
751 }
752 \f
753 /* Generate code to extract a byte-field from STR_RTX
754    containing BITSIZE bits, starting at BITNUM,
755    and put it in TARGET if possible (if TARGET is nonzero).
756    Regardless of TARGET, we return the rtx for where the value is placed.
757    It may be a QUEUED.
758
759    STR_RTX is the structure containing the byte (a REG or MEM).
760    UNSIGNEDP is nonzero if this is an unsigned bit field.
761    MODE is the natural mode of the field value once extracted.
762    TMODE is the mode the caller would like the value to have;
763    but the value may be returned with type MODE instead.
764
765    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.
766    TOTAL_SIZE is the size in bytes of the containing structure,
767    or -1 if varying.
768
769    If a TARGET is specified and we can store in it at no extra cost,
770    we do so, and return TARGET.
771    Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
772    if they are equally easy.  */
773
774 rtx
775 extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
776                    target, mode, tmode, align, total_size)
777      rtx str_rtx;
778      register int bitsize;
779      int bitnum;
780      int unsignedp;
781      rtx target;
782      enum machine_mode mode, tmode;
783      int align;
784      int total_size;
785 {
786   int unit = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
787   register int offset = bitnum / unit;
788   register int bitpos = bitnum % unit;
789   register rtx op0 = str_rtx;
790   rtx spec_target = target;
791   rtx spec_target_subreg = 0;
792
793   if (GET_CODE (str_rtx) == MEM && ! MEM_IN_STRUCT_P (str_rtx))
794     abort ();
795
796   /* Discount the part of the structure before the desired byte.
797      We need to know how many bytes are safe to reference after it.  */
798   if (total_size >= 0)
799     total_size -= (bitpos / BIGGEST_ALIGNMENT
800                    * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
801
802   if (tmode == VOIDmode)
803     tmode = mode;
804   while (GET_CODE (op0) == SUBREG)
805     {
806       offset += SUBREG_WORD (op0);
807       op0 = SUBREG_REG (op0);
808     }
809   
810 #if BYTES_BIG_ENDIAN
811   /* If OP0 is a register, BITPOS must count within a word.
812      But as we have it, it counts within whatever size OP0 now has.
813      On a bigendian machine, these are not the same, so convert.  */
814   if (GET_CODE (op0) != MEM && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
815     bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
816 #endif
817
818   /* Extracting a full-word or multi-word value
819      from a structure in a register.
820      This can be done with just SUBREG.
821      So too extracting a subword value in
822      the least significant part of the register.  */
823
824   if (GET_CODE (op0) == REG
825       && ((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
826            && bitpos % BITS_PER_WORD == 0)
827           || (mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0) != BLKmode
828 #if BYTES_BIG_ENDIAN
829               && bitpos + bitsize == BITS_PER_WORD
830 #else
831               && bitpos == 0
832 #endif
833               )))
834     {
835       enum machine_mode mode1
836         = mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0);
837
838       if (mode1 != GET_MODE (op0))
839         op0 = gen_rtx (SUBREG, mode1, op0, offset);
840
841       if (mode1 != mode)
842         return convert_to_mode (tmode, op0, unsignedp);
843       return op0;
844     }
845
846   /* Handle fields bigger than a word.  */
847   
848   if (bitsize > BITS_PER_WORD)
849     {
850       /* Here we transfer the words of the field
851          in the order least significant first.
852          This is because the most significant word is the one which may
853          be less than full.  */
854
855       int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
856       int i;
857
858       if (target == 0 || GET_CODE (target) != REG)
859         target = gen_reg_rtx (mode);
860
861       for (i = 0; i < nwords; i++)
862         {
863           /* If I is 0, use the low-order word in both field and target;
864              if I is 1, use the next to lowest word; and so on.  */
865           int wordnum = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
866           int bit_offset = (WORDS_BIG_ENDIAN
867                             ? MAX (0, bitsize - (i + 1) * BITS_PER_WORD)
868                             : i * BITS_PER_WORD);
869           rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
870           rtx result_part
871             = extract_bit_field (op0, MIN (BITS_PER_WORD,
872                                            bitsize - i * BITS_PER_WORD),
873                                  bitnum + bit_offset,
874                                  1, target_part, mode, word_mode,
875                                  align, total_size);
876
877           if (target_part == 0)
878             abort ();
879
880           if (result_part != target_part)
881             emit_move_insn (target_part, result_part);
882         }
883
884       return target;
885     }
886   
887   /* From here on we know the desired field is smaller than a word
888      so we can assume it is an integer.  So we can safely extract it as one
889      size of integer, if necessary, and then truncate or extend
890      to the size that is wanted.  */
891
892   /* OFFSET is the number of words or bytes (UNIT says which)
893      from STR_RTX to the first word or byte containing part of the field.  */
894
895   if (GET_CODE (op0) == REG)
896     {
897       if (offset != 0
898           || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
899         op0 = gen_rtx (SUBREG, TYPE_MODE (type_for_size (BITS_PER_WORD, 0)),
900                        op0, offset);
901       offset = 0;
902     }
903   else
904     {
905       op0 = protect_from_queue (str_rtx, 1);
906     }
907
908   /* Now OFFSET is nonzero only for memory operands.  */
909
910   if (unsignedp)
911     {
912 #ifdef HAVE_extzv
913       if (HAVE_extzv
914           && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extzv][0])
915               >= bitsize))
916         {
917           int xbitpos = bitpos, xoffset = offset;
918           rtx bitsize_rtx, bitpos_rtx;
919           rtx last = get_last_insn();
920           rtx xop0 = op0;
921           rtx xtarget = target;
922           rtx xspec_target = spec_target;
923           rtx xspec_target_subreg = spec_target_subreg;
924           rtx pat;
925           enum machine_mode maxmode
926             = insn_operand_mode[(int) CODE_FOR_extzv][0];
927
928           if (GET_CODE (xop0) == MEM)
929             {
930               int save_volatile_ok = volatile_ok;
931               volatile_ok = 1;
932
933               /* Is the memory operand acceptable?  */
934               if (flag_force_mem
935                   || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][1])
936                         (xop0, GET_MODE (xop0))))
937                 {
938                   /* No, load into a reg and extract from there.  */
939                   enum machine_mode bestmode;
940
941                   /* Get the mode to use for inserting into this field.  If
942                      OP0 is BLKmode, get the smallest mode consistent with the
943                      alignment. If OP0 is a non-BLKmode object that is no
944                      wider than MAXMODE, use its mode. Otherwise, use the
945                      smallest mode containing the field.  */
946
947                   if (GET_MODE (xop0) == BLKmode
948                       || (GET_MODE_SIZE (GET_MODE (op0))
949                           > GET_MODE_SIZE (maxmode)))
950                     bestmode = get_best_mode (bitsize, bitnum,
951                                               align * BITS_PER_UNIT, maxmode,
952                                               MEM_VOLATILE_P (xop0));
953                   else
954                     bestmode = GET_MODE (xop0);
955
956                   if (bestmode == VOIDmode)
957                     goto extzv_loses;
958
959                   /* Compute offset as multiple of this unit,
960                      counting in bytes.  */
961                   unit = GET_MODE_BITSIZE (bestmode);
962                   xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
963                   xbitpos = bitnum % unit;
964                   xop0 = change_address (xop0, bestmode,
965                                          plus_constant (XEXP (xop0, 0),
966                                                         xoffset));
967                   /* Fetch it to a register in that size.  */
968                   xop0 = force_reg (bestmode, xop0);
969
970                   /* XBITPOS counts within UNIT, which is what is expected.  */
971                 }
972               else
973                 /* Get ref to first byte containing part of the field.  */
974                 xop0 = change_address (xop0, byte_mode,
975                                        plus_constant (XEXP (xop0, 0), xoffset));
976
977               volatile_ok = save_volatile_ok;
978             }
979
980           /* If op0 is a register, we need it in MAXMODE (which is usually
981              SImode). to make it acceptable to the format of extzv.  */
982           if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
983             abort ();
984           if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
985             xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
986
987           /* On big-endian machines, we count bits from the most significant.
988              If the bit field insn does not, we must invert.  */
989 #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
990           xbitpos = unit - bitsize - xbitpos;
991 #endif
992           /* Now convert from counting within UNIT to counting in MAXMODE.  */
993 #if BITS_BIG_ENDIAN
994           if (GET_CODE (xop0) != MEM)
995             xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
996 #endif
997           unit = GET_MODE_BITSIZE (maxmode);
998
999           if (xtarget == 0
1000               || (flag_force_mem && GET_CODE (xtarget) == MEM))
1001             xtarget = xspec_target = gen_reg_rtx (tmode);
1002
1003           if (GET_MODE (xtarget) != maxmode)
1004             {
1005               if (GET_CODE (xtarget) == REG)
1006                 {
1007                   int wider = (GET_MODE_SIZE (maxmode)
1008                                > GET_MODE_SIZE (GET_MODE (xtarget)));
1009                   xtarget = gen_lowpart (maxmode, xtarget);
1010                   if (wider)
1011                     xspec_target_subreg = xtarget;
1012                 }
1013               else
1014                 xtarget = gen_reg_rtx (maxmode);
1015             }
1016
1017           /* If this machine's extzv insists on a register target,
1018              make sure we have one.  */
1019           if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
1020                  (xtarget, maxmode)))
1021             xtarget = gen_reg_rtx (maxmode);
1022
1023           bitsize_rtx = GEN_INT (bitsize);
1024           bitpos_rtx = GEN_INT (xbitpos);
1025
1026           pat = gen_extzv (protect_from_queue (xtarget, 1),
1027                            xop0, bitsize_rtx, bitpos_rtx);
1028           if (pat)
1029             {
1030               emit_insn (pat);
1031               target = xtarget;
1032               spec_target = xspec_target;
1033               spec_target_subreg = xspec_target_subreg;
1034             }
1035           else
1036             {
1037               delete_insns_since (last);
1038               target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
1039                                                 bitpos, target, 1, align);
1040             }
1041         }
1042       else
1043         extzv_loses:
1044 #endif
1045         target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1046                                           target, 1, align);
1047     }
1048   else
1049     {
1050 #ifdef HAVE_extv
1051       if (HAVE_extv
1052           && (GET_MODE_BITSIZE (insn_operand_mode[(int) CODE_FOR_extv][0])
1053               >= bitsize))
1054         {
1055           int xbitpos = bitpos, xoffset = offset;
1056           rtx bitsize_rtx, bitpos_rtx;
1057           rtx last = get_last_insn();
1058           rtx xop0 = op0, xtarget = target;
1059           rtx xspec_target = spec_target;
1060           rtx xspec_target_subreg = spec_target_subreg;
1061           rtx pat;
1062           enum machine_mode maxmode
1063             = insn_operand_mode[(int) CODE_FOR_extv][0];
1064
1065           if (GET_CODE (xop0) == MEM)
1066             {
1067               /* Is the memory operand acceptable?  */
1068               if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][1])
1069                      (xop0, GET_MODE (xop0))))
1070                 {
1071                   /* No, load into a reg and extract from there.  */
1072                   enum machine_mode bestmode;
1073
1074                   /* Get the mode to use for inserting into this field.  If
1075                      OP0 is BLKmode, get the smallest mode consistent with the
1076                      alignment. If OP0 is a non-BLKmode object that is no
1077                      wider than MAXMODE, use its mode. Otherwise, use the
1078                      smallest mode containing the field.  */
1079
1080                   if (GET_MODE (xop0) == BLKmode
1081                       || (GET_MODE_SIZE (GET_MODE (op0))
1082                           > GET_MODE_SIZE (maxmode)))
1083                     bestmode = get_best_mode (bitsize, bitnum,
1084                                               align * BITS_PER_UNIT, maxmode,
1085                                               MEM_VOLATILE_P (xop0));
1086                   else
1087                     bestmode = GET_MODE (xop0);
1088
1089                   if (bestmode == VOIDmode)
1090                     goto extv_loses;
1091
1092                   /* Compute offset as multiple of this unit,
1093                      counting in bytes.  */
1094                   unit = GET_MODE_BITSIZE (bestmode);
1095                   xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1096                   xbitpos = bitnum % unit;
1097                   xop0 = change_address (xop0, bestmode,
1098                                          plus_constant (XEXP (xop0, 0),
1099                                                         xoffset));
1100                   /* Fetch it to a register in that size.  */
1101                   xop0 = force_reg (bestmode, xop0);
1102
1103                   /* XBITPOS counts within UNIT, which is what is expected.  */
1104                 }
1105               else
1106                 /* Get ref to first byte containing part of the field.  */
1107                 xop0 = change_address (xop0, byte_mode,
1108                                        plus_constant (XEXP (xop0, 0), xoffset));
1109             }
1110
1111           /* If op0 is a register, we need it in MAXMODE (which is usually
1112              SImode) to make it acceptable to the format of extv.  */
1113           if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
1114             abort ();
1115           if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
1116             xop0 = gen_rtx (SUBREG, maxmode, xop0, 0);
1117
1118           /* On big-endian machines, we count bits from the most significant.
1119              If the bit field insn does not, we must invert.  */
1120 #if BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN
1121           xbitpos = unit - bitsize - xbitpos;
1122 #endif
1123           /* XBITPOS counts within a size of UNIT.
1124              Adjust to count within a size of MAXMODE.  */
1125 #if BITS_BIG_ENDIAN
1126           if (GET_CODE (xop0) != MEM)
1127             xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
1128 #endif
1129           unit = GET_MODE_BITSIZE (maxmode);
1130
1131           if (xtarget == 0
1132               || (flag_force_mem && GET_CODE (xtarget) == MEM))
1133             xtarget = xspec_target = gen_reg_rtx (tmode);
1134
1135           if (GET_MODE (xtarget) != maxmode)
1136             {
1137               if (GET_CODE (xtarget) == REG)
1138                 {
1139                   int wider = (GET_MODE_SIZE (maxmode)
1140                                > GET_MODE_SIZE (GET_MODE (xtarget)));
1141                   xtarget = gen_lowpart (maxmode, xtarget);
1142                   if (wider)
1143                     xspec_target_subreg = xtarget;
1144                 }
1145               else
1146                 xtarget = gen_reg_rtx (maxmode);
1147             }
1148
1149           /* If this machine's extv insists on a register target,
1150              make sure we have one.  */
1151           if (! ((*insn_operand_predicate[(int) CODE_FOR_extv][0])
1152                  (xtarget, maxmode)))
1153             xtarget = gen_reg_rtx (maxmode);
1154
1155           bitsize_rtx = GEN_INT (bitsize);
1156           bitpos_rtx = GEN_INT (xbitpos);
1157
1158           pat = gen_extv (protect_from_queue (xtarget, 1),
1159                           xop0, bitsize_rtx, bitpos_rtx);
1160           if (pat)
1161             {
1162               emit_insn (pat);
1163               target = xtarget;
1164               spec_target = xspec_target;
1165               spec_target_subreg = xspec_target_subreg;
1166             }
1167           else
1168             {
1169               delete_insns_since (last);
1170               target = extract_fixed_bit_field (tmode, op0, offset, bitsize,
1171                                                 bitpos, target, 0, align);
1172             }
1173         } 
1174       else
1175         extv_loses:
1176 #endif
1177         target = extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1178                                           target, 0, align);
1179     }
1180   if (target == spec_target)
1181     return target;
1182   if (target == spec_target_subreg)
1183     return spec_target;
1184   if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
1185     {
1186       /* If the target mode is floating-point, first convert to the
1187          integer mode of that size and then access it as a floating-point
1188          value via a SUBREG.  */
1189       if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1190         {
1191           target = convert_to_mode (mode_for_size (GET_MODE_BITSIZE (tmode),
1192                                                    MODE_INT, 0),
1193                                     target, unsignedp);
1194           if (GET_CODE (target) != REG)
1195             target = copy_to_reg (target);
1196           return gen_rtx (SUBREG, tmode, target, 0);
1197         }
1198       else
1199         return convert_to_mode (tmode, target, unsignedp);
1200     }
1201   return target;
1202 }
1203 \f
1204 /* Extract a bit field using shifts and boolean operations
1205    Returns an rtx to represent the value.
1206    OP0 addresses a register (word) or memory (byte).
1207    BITPOS says which bit within the word or byte the bit field starts in.
1208    OFFSET says how many bytes farther the bit field starts;
1209     it is 0 if OP0 is a register.
1210    BITSIZE says how many bits long the bit field is.
1211     (If OP0 is a register, it may be narrower than a full word,
1212      but BITPOS still counts within a full word,
1213      which is significant on bigendian machines.)
1214
1215    UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1216    If TARGET is nonzero, attempts to store the value there
1217    and return TARGET, but this is not guaranteed.
1218    If TARGET is not used, create a pseudo-reg of mode TMODE for the value.
1219
1220    ALIGN is the alignment that STR_RTX is known to have, measured in bytes.  */
1221
1222 static rtx
1223 extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
1224                          target, unsignedp, align)
1225      enum machine_mode tmode;
1226      register rtx op0, target;
1227      register int offset, bitsize, bitpos;
1228      int unsignedp;
1229      int align;
1230 {
1231   int total_bits = BITS_PER_WORD;
1232   enum machine_mode mode;
1233
1234   if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
1235     {
1236       /* Special treatment for a bit field split across two registers.  */
1237       if (bitsize + bitpos > BITS_PER_WORD)
1238         return extract_split_bit_field (op0, bitsize, bitpos,
1239                                         unsignedp, align);
1240     }
1241   else
1242     {
1243       /* Get the proper mode to use for this field.  We want a mode that
1244          includes the entire field.  If such a mode would be larger than
1245          a word, we won't be doing the extraction the normal way.  */
1246
1247       mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1248                             align * BITS_PER_UNIT, word_mode,
1249                             GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0));
1250
1251       if (mode == VOIDmode)
1252         /* The only way this should occur is if the field spans word
1253            boundaries.  */
1254         return extract_split_bit_field (op0, bitsize,
1255                                         bitpos + offset * BITS_PER_UNIT,
1256                                         unsignedp, align);
1257
1258       total_bits = GET_MODE_BITSIZE (mode);
1259
1260       /* Get ref to an aligned byte, halfword, or word containing the field.
1261          Adjust BITPOS to be position within a word,
1262          and OFFSET to be the offset of that word.
1263          Then alter OP0 to refer to that word.  */
1264       bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1265       offset -= (offset % (total_bits / BITS_PER_UNIT));
1266       op0 = change_address (op0, mode,
1267                             plus_constant (XEXP (op0, 0), offset));
1268     }
1269
1270   mode = GET_MODE (op0);
1271
1272 #if BYTES_BIG_ENDIAN
1273   /* BITPOS is the distance between our msb and that of OP0.
1274      Convert it to the distance from the lsb.  */
1275
1276   bitpos = total_bits - bitsize - bitpos;
1277 #endif
1278   /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1279      We have reduced the big-endian case to the little-endian case.  */
1280
1281   if (unsignedp)
1282     {
1283       if (bitpos)
1284         {
1285           /* If the field does not already start at the lsb,
1286              shift it so it does.  */
1287           tree amount = build_int_2 (bitpos, 0);
1288           /* Maybe propagate the target for the shift.  */
1289           /* But not if we will return it--could confuse integrate.c.  */
1290           rtx subtarget = (target != 0 && GET_CODE (target) == REG
1291                            && !REG_FUNCTION_VALUE_P (target)
1292                            ? target : 0);
1293           if (tmode != mode) subtarget = 0;
1294           op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1295         }
1296       /* Convert the value to the desired mode.  */
1297       if (mode != tmode)
1298         op0 = convert_to_mode (tmode, op0, 1);
1299
1300       /* Unless the msb of the field used to be the msb when we shifted,
1301          mask out the upper bits.  */
1302
1303       if (GET_MODE_BITSIZE (mode) != bitpos + bitsize
1304 #if 0
1305 #ifdef SLOW_ZERO_EXTEND
1306           /* Always generate an `and' if
1307              we just zero-extended op0 and SLOW_ZERO_EXTEND, since it
1308              will combine fruitfully with the zero-extend. */
1309           || tmode != mode
1310 #endif
1311 #endif
1312           )
1313         return expand_binop (GET_MODE (op0), and_optab, op0,
1314                              mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1315                              target, 1, OPTAB_LIB_WIDEN);
1316       return op0;
1317     }
1318
1319   /* To extract a signed bit-field, first shift its msb to the msb of the word,
1320      then arithmetic-shift its lsb to the lsb of the word.  */
1321   op0 = force_reg (mode, op0);
1322   if (mode != tmode)
1323     target = 0;
1324
1325   /* Find the narrowest integer mode that contains the field.  */
1326
1327   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1328        mode = GET_MODE_WIDER_MODE (mode))
1329     if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1330       {
1331         op0 = convert_to_mode (mode, op0, 0);
1332         break;
1333       }
1334
1335   if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1336     {
1337       tree amount = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
1338       /* Maybe propagate the target for the shift.  */
1339       /* But not if we will return the result--could confuse integrate.c.  */
1340       rtx subtarget = (target != 0 && GET_CODE (target) == REG
1341                        && ! REG_FUNCTION_VALUE_P (target)
1342                        ? target : 0);
1343       op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1344     }
1345
1346   return expand_shift (RSHIFT_EXPR, mode, op0,
1347                        build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0), 
1348                        target, 0);
1349 }
1350 \f
1351 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1352    of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1353    complement of that if COMPLEMENT.  The mask is truncated if
1354    necessary to the width of mode MODE.  */
1355
1356 static rtx
1357 mask_rtx (mode, bitpos, bitsize, complement)
1358      enum machine_mode mode;
1359      int bitpos, bitsize, complement;
1360 {
1361   HOST_WIDE_INT masklow, maskhigh;
1362
1363   if (bitpos < HOST_BITS_PER_WIDE_INT)
1364     masklow = (HOST_WIDE_INT) -1 << bitpos;
1365   else
1366     masklow = 0;
1367
1368   if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1369     masklow &= ((unsigned HOST_WIDE_INT) -1
1370                 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1371   
1372   if (bitpos <= HOST_BITS_PER_WIDE_INT)
1373     maskhigh = -1;
1374   else
1375     maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1376
1377   if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
1378     maskhigh &= ((unsigned HOST_WIDE_INT) -1
1379                  >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1380   else
1381     maskhigh = 0;
1382
1383   if (complement)
1384     {
1385       maskhigh = ~maskhigh;
1386       masklow = ~masklow;
1387     }
1388
1389   return immed_double_const (masklow, maskhigh, mode);
1390 }
1391
1392 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1393    VALUE truncated to BITSIZE bits and then shifted left BITPOS bits.  */
1394
1395 static rtx
1396 lshift_value (mode, value, bitpos, bitsize)
1397      enum machine_mode mode;
1398      rtx value;
1399      int bitpos, bitsize;
1400 {
1401   unsigned HOST_WIDE_INT v = INTVAL (value);
1402   HOST_WIDE_INT low, high;
1403
1404   if (bitsize < HOST_BITS_PER_WIDE_INT)
1405     v &= ~((HOST_WIDE_INT) -1 << bitsize);
1406
1407   if (bitpos < HOST_BITS_PER_WIDE_INT)
1408     {
1409       low = v << bitpos;
1410       high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
1411     }
1412   else
1413     {
1414       low = 0;
1415       high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
1416     }
1417
1418   return immed_double_const (low, high, mode);
1419 }
1420 \f
1421 /* Extract a bit field that is split across two words
1422    and return an RTX for the result.
1423
1424    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1425    BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1426    UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.  */
1427
1428 static rtx
1429 extract_split_bit_field (op0, bitsize, bitpos, unsignedp, align)
1430      rtx op0;
1431      int bitsize, bitpos, unsignedp, align;
1432 {
1433   /* BITSIZE_1 is size of the part in the first word.  */
1434   int bitsize_1 = BITS_PER_WORD - bitpos % BITS_PER_WORD;
1435   /* BITSIZE_2 is size of the rest (in the following word).  */
1436   int bitsize_2 = bitsize - bitsize_1;
1437   rtx part1, part2, result;
1438   int unit = GET_CODE (op0) == MEM ? BITS_PER_UNIT : BITS_PER_WORD;
1439   int offset = bitpos / unit;
1440   rtx word;
1441  
1442   /* The field must span exactly one word boundary.  */
1443   if (bitpos / BITS_PER_WORD != (bitpos + bitsize - 1) / BITS_PER_WORD - 1)
1444     abort ();
1445
1446   /* Get the part of the bit field from the first word.  If OP0 is a MEM,
1447      pass OP0 and the offset computed above.  Otherwise, get the proper
1448      word and pass an offset of zero.  */
1449   word = (GET_CODE (op0) == MEM ? op0
1450           : operand_subword_force (op0, offset, GET_MODE (op0)));
1451   part1 = extract_fixed_bit_field (word_mode, word,
1452                                    GET_CODE (op0) == MEM ? offset : 0,
1453                                    bitsize_1, bitpos % unit, NULL_RTX,
1454                                    1, align);
1455
1456   /* Offset op0 by 1 word to get to the following one.  */
1457   if (GET_CODE (op0) == SUBREG)
1458     word = operand_subword_force (SUBREG_REG (op0),
1459                                   SUBREG_WORD (op0) + offset + 1, VOIDmode);
1460   else if (GET_CODE (op0) == MEM)
1461     word = op0;
1462   else
1463     word = operand_subword_force (op0, offset + 1, GET_MODE (op0));
1464
1465   /* Get the part of the bit field from the second word.  */
1466   part2 = extract_fixed_bit_field (word_mode, word,
1467                                    (GET_CODE (op0) == MEM
1468                                     ? CEIL (offset + 1, UNITS_PER_WORD) * UNITS_PER_WORD
1469                                     : 0),
1470                                    bitsize_2, 0, NULL_RTX, 1, align);
1471
1472   /* Shift the more significant part up to fit above the other part.  */
1473 #if BYTES_BIG_ENDIAN
1474   part1 = expand_shift (LSHIFT_EXPR, word_mode, part1,
1475                         build_int_2 (bitsize_2, 0), 0, 1);
1476 #else
1477   part2 = expand_shift (LSHIFT_EXPR, word_mode, part2,
1478                         build_int_2 (bitsize_1, 0), 0, 1);
1479 #endif
1480
1481   /* Combine the two parts with bitwise or.  This works
1482      because we extracted both parts as unsigned bit fields.  */
1483   result = expand_binop (word_mode, ior_optab, part1, part2, NULL_RTX, 1,
1484                          OPTAB_LIB_WIDEN);
1485
1486   /* Unsigned bit field: we are done.  */
1487   if (unsignedp)
1488     return result;
1489   /* Signed bit field: sign-extend with two arithmetic shifts.  */
1490   result = expand_shift (LSHIFT_EXPR, word_mode, result,
1491                          build_int_2 (BITS_PER_WORD - bitsize, 0),
1492                          NULL_RTX, 0);
1493   return expand_shift (RSHIFT_EXPR, word_mode, result,
1494                        build_int_2 (BITS_PER_WORD - bitsize, 0), NULL_RTX, 0);
1495 }
1496 \f
1497 /* Add INC into TARGET.  */
1498
1499 void
1500 expand_inc (target, inc)
1501      rtx target, inc;
1502 {
1503   rtx value = expand_binop (GET_MODE (target), add_optab,
1504                             target, inc,
1505                             target, 0, OPTAB_LIB_WIDEN);
1506   if (value != target)
1507     emit_move_insn (target, value);
1508 }
1509
1510 /* Subtract DEC from TARGET.  */
1511
1512 void
1513 expand_dec (target, dec)
1514      rtx target, dec;
1515 {
1516   rtx value = expand_binop (GET_MODE (target), sub_optab,
1517                             target, dec,
1518                             target, 0, OPTAB_LIB_WIDEN);
1519   if (value != target)
1520     emit_move_insn (target, value);
1521 }
1522 \f
1523 /* Output a shift instruction for expression code CODE,
1524    with SHIFTED being the rtx for the value to shift,
1525    and AMOUNT the tree for the amount to shift by.
1526    Store the result in the rtx TARGET, if that is convenient.
1527    If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
1528    Return the rtx for where the value is.  */
1529
1530 rtx
1531 expand_shift (code, mode, shifted, amount, target, unsignedp)
1532      enum tree_code code;
1533      register enum machine_mode mode;
1534      rtx shifted;
1535      tree amount;
1536      register rtx target;
1537      int unsignedp;
1538 {
1539   register rtx op1, temp = 0;
1540   register int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
1541   register int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
1542   int try;
1543
1544   /* Previously detected shift-counts computed by NEGATE_EXPR
1545      and shifted in the other direction; but that does not work
1546      on all machines.  */
1547
1548   op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
1549
1550   if (op1 == const0_rtx)
1551     return shifted;
1552
1553   for (try = 0; temp == 0 && try < 3; try++)
1554     {
1555       enum optab_methods methods;
1556
1557       if (try == 0)
1558         methods = OPTAB_DIRECT;
1559       else if (try == 1)
1560         methods = OPTAB_WIDEN;
1561       else
1562         methods = OPTAB_LIB_WIDEN;
1563
1564       if (rotate)
1565         {
1566           /* Widening does not work for rotation.  */
1567           if (methods == OPTAB_WIDEN)
1568             continue;
1569           else if (methods == OPTAB_LIB_WIDEN)
1570             {
1571               /* If we are rotating by a constant that is valid and
1572                  we have been unable to open-code this by a rotation,
1573                  do it as the IOR of two shifts.  I.e., to rotate A
1574                  by N bits, compute (A << N) | ((unsigned) A >> (C - N))
1575                  where C is the bitsize of A.
1576
1577                  It is theoretically possible that the target machine might
1578                  not be able to perform either shift and hence we would
1579                  be making two libcalls rather than just the one for the
1580                  shift (similarly if IOR could not be done).  We will allow
1581                  this extremely unlikely lossage to avoid complicating the
1582                  code below.  */
1583
1584               if (GET_CODE (op1) == CONST_INT && INTVAL (op1) > 0
1585                   && INTVAL (op1) < GET_MODE_BITSIZE (mode))
1586                 {
1587                   rtx subtarget = target == shifted ? 0 : target;
1588                   rtx temp1;
1589                   tree other_amount
1590                     = build_int_2 (GET_MODE_BITSIZE (mode) - INTVAL (op1), 0);
1591
1592                   shifted = force_reg (mode, shifted);
1593
1594                   temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
1595                                        mode, shifted, amount, subtarget, 1);
1596                   temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
1597                                         mode, shifted, other_amount, 0, 1);
1598                   return expand_binop (mode, ior_optab, temp, temp1, target,
1599                                        unsignedp, methods);
1600                 }
1601               else
1602                 methods = OPTAB_LIB;
1603             }
1604
1605           temp = expand_binop (mode,
1606                                left ? rotl_optab : rotr_optab,
1607                                shifted, op1, target, unsignedp, methods);
1608
1609           /* If we don't have the rotate, but we are rotating by a constant
1610              that is in range, try a rotate in the opposite direction.  */
1611
1612           if (temp == 0 && GET_CODE (op1) == CONST_INT
1613               && INTVAL (op1) > 0 && INTVAL (op1) < GET_MODE_BITSIZE (mode))
1614             temp = expand_binop (mode,
1615                                  left ? rotr_optab : rotl_optab,
1616                                  shifted, 
1617                                  GEN_INT (GET_MODE_BITSIZE (mode)
1618                                           - INTVAL (op1)),
1619                                  target, unsignedp, methods);
1620         }
1621       else if (unsignedp)
1622         {
1623           temp = expand_binop (mode,
1624                                left ? lshl_optab : lshr_optab,
1625                                shifted, op1, target, unsignedp, methods);
1626           if (temp == 0 && left)
1627             temp = expand_binop (mode, ashl_optab,
1628                                  shifted, op1, target, unsignedp, methods);
1629         }
1630
1631       /* Do arithmetic shifts.
1632          Also, if we are going to widen the operand, we can just as well
1633          use an arithmetic right-shift instead of a logical one.  */
1634       if (temp == 0 && ! rotate
1635           && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
1636         {
1637           enum optab_methods methods1 = methods;
1638
1639           /* If trying to widen a log shift to an arithmetic shift,
1640              don't accept an arithmetic shift of the same size.  */
1641           if (unsignedp)
1642             methods1 = OPTAB_MUST_WIDEN;
1643
1644           /* Arithmetic shift */
1645
1646           temp = expand_binop (mode,
1647                                left ? ashl_optab : ashr_optab,
1648                                shifted, op1, target, unsignedp, methods1);
1649         }
1650
1651 #ifdef HAVE_extzv
1652       /* We can do a logical (unsigned) right shift with a bit-field
1653          extract insn.  But first check if one of the above methods worked.  */
1654       if (temp != 0)
1655         return temp;
1656
1657       if (unsignedp && code == RSHIFT_EXPR && ! BITS_BIG_ENDIAN && HAVE_extzv)
1658         {
1659           enum machine_mode output_mode
1660             = insn_operand_mode[(int) CODE_FOR_extzv][0];
1661
1662           if ((methods == OPTAB_DIRECT && mode == output_mode)
1663               || (methods == OPTAB_WIDEN
1664                   && GET_MODE_SIZE (mode) < GET_MODE_SIZE (output_mode)))
1665             {
1666               rtx shifted1 = convert_to_mode (output_mode,
1667                                               protect_from_queue (shifted, 0),
1668                                               1);
1669               enum machine_mode length_mode
1670                 = insn_operand_mode[(int) CODE_FOR_extzv][2];
1671               enum machine_mode pos_mode
1672                 = insn_operand_mode[(int) CODE_FOR_extzv][3];
1673               rtx target1 = 0;
1674               rtx last = get_last_insn ();
1675               rtx width;
1676               rtx xop1 = op1;
1677               rtx pat;
1678
1679               if (target != 0)
1680                 target1 = protect_from_queue (target, 1);
1681
1682               /* We define extract insns as having OUTPUT_MODE in a register
1683                  and the mode of operand 1 in memory.  Since we want
1684                  OUTPUT_MODE, we will always force the operand into a
1685                  register.  At some point we might want to support MEM
1686                  directly. */
1687               shifted1 = force_reg (output_mode, shifted1);
1688
1689               /* If we don't have or cannot use a suggested target,
1690                  make a place for the result, in the proper mode.  */
1691               if (methods == OPTAB_WIDEN || target1 == 0
1692                   || ! ((*insn_operand_predicate[(int) CODE_FOR_extzv][0])
1693                         (target1, output_mode)))
1694                 target1 = gen_reg_rtx (output_mode);
1695
1696               xop1 = protect_from_queue (xop1, 0);
1697               xop1 = convert_to_mode (pos_mode, xop1,
1698                                       TREE_UNSIGNED (TREE_TYPE (amount)));
1699
1700               /* If this machine's extzv insists on a register for
1701                  operand 3 (position), arrange for that.  */
1702               if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][3])
1703                      (xop1, pos_mode)))
1704                 xop1 = force_reg (pos_mode, xop1);
1705
1706               /* WIDTH gets the width of the bit field to extract:
1707                  wordsize minus # bits to shift by.  */
1708               if (GET_CODE (xop1) == CONST_INT)
1709                 width = GEN_INT (GET_MODE_BITSIZE (mode) - INTVAL (op1));
1710               else
1711                 {
1712                   /* Now get the width in the proper mode.  */
1713                   op1 = protect_from_queue (op1, 0);
1714                   width = convert_to_mode (length_mode, op1,
1715                                            TREE_UNSIGNED (TREE_TYPE (amount)));
1716
1717                   width = expand_binop (length_mode, sub_optab,
1718                                         GEN_INT (GET_MODE_BITSIZE (mode)),
1719                                         width, NULL_RTX, 0, OPTAB_LIB_WIDEN);
1720                 }
1721
1722               /* If this machine's extzv insists on a register for
1723                  operand 2 (length), arrange for that.  */
1724               if (! ((*insn_operand_predicate[(int) CODE_FOR_extzv][2])
1725                      (width, length_mode)))
1726                 width = force_reg (length_mode, width);
1727
1728               /* Now extract with WIDTH, omitting OP1 least sig bits.  */
1729               pat = gen_extzv (target1, shifted1, width, xop1);
1730               if (pat)
1731                 {
1732                   emit_insn (pat);
1733                   temp = convert_to_mode (mode, target1, 1);
1734                 }
1735               else
1736                 delete_insns_since (last);
1737             }
1738
1739           /* Can also do logical shift with signed bit-field extract
1740              followed by inserting the bit-field at a different position.
1741              That strategy is not yet implemented.  */
1742         }
1743 #endif /* HAVE_extzv */
1744     }
1745
1746   if (temp == 0)
1747     abort ();
1748   return temp;
1749 }
1750 \f
1751 enum alg_code { alg_zero, alg_m, alg_shift,
1752                   alg_add_t_m2, alg_sub_t_m2,
1753                   alg_add_factor, alg_sub_factor,
1754                   alg_add_t2_m, alg_sub_t2_m,
1755                   alg_add, alg_subtract, alg_factor, alg_shiftop };
1756
1757 /* This structure records a sequence of operations.
1758    `ops' is the number of operations recorded.
1759    `cost' is their total cost.
1760    The operations are stored in `op' and the corresponding
1761    logarithms of the integer coefficients in `log'.
1762
1763    These are the operations:
1764    alg_zero             total := 0;
1765    alg_m                total := multiplicand;
1766    alg_shift            total := total * coeff
1767    alg_add_t_m2         total := total + multiplicand * coeff;
1768    alg_sub_t_m2         total := total - multiplicand * coeff;
1769    alg_add_factor       total := total * coeff + total;
1770    alg_sub_factor       total := total * coeff - total;
1771    alg_add_t2_m         total := total * coeff + multiplicand;
1772    alg_sub_t2_m         total := total * coeff - multiplicand;
1773
1774    The first operand must be either alg_zero or alg_m.  */
1775
1776 #ifndef MAX_BITS_PER_WORD
1777 #define MAX_BITS_PER_WORD BITS_PER_WORD
1778 #endif
1779
1780 struct algorithm
1781 {
1782   short cost;
1783   short ops;
1784   /* The size of the OP and LOG fields are not directly related to the
1785      word size, but the worst-case algorithms will be if we have few
1786      consecutive ones or zeros, i.e., a multiplicand like 10101010101...
1787      In that case we will generate shift-by-2, add, shift-by-2, add,...,
1788      in total wordsize operations.  */
1789   enum alg_code op[MAX_BITS_PER_WORD];
1790   char log[MAX_BITS_PER_WORD];
1791 };
1792
1793 /* Compute and return the best algorithm for multiplying by T.
1794    The algorithm must cost less than cost_limit
1795    If retval.cost >= COST_LIMIT, no algorithm was found and all
1796    other field of the returned struct are undefined.  */
1797
1798 static struct algorithm
1799 synth_mult (t, cost_limit)
1800      unsigned HOST_WIDE_INT t;
1801      int cost_limit;
1802 {
1803   int m;
1804   struct algorithm *best_alg
1805     = (struct algorithm *)alloca (sizeof (struct algorithm));
1806   struct algorithm *alg_in
1807     = (struct algorithm *)alloca (sizeof (struct algorithm));
1808   unsigned int cost;
1809   unsigned HOST_WIDE_INT q;
1810
1811   /* Indicate that no algorithm is yet found.  If no algorithm
1812      is found, this value will be returned and indicate failure.  */
1813   best_alg->cost = cost_limit;
1814
1815   if (cost_limit <= 0)
1816     return *best_alg;
1817
1818   /* t == 1 can be done in zero cost.  */
1819   if (t == 1)
1820     {
1821       best_alg->ops = 1;
1822       best_alg->cost = 0;
1823       best_alg->op[0] = alg_m;
1824       return *best_alg;
1825     }
1826
1827   /* t == 0 sometimes has a cost.  If it does and it exceeds our limit,
1828      fail now.  */
1829
1830   else if (t == 0)
1831     {
1832       if (zero_cost >= cost_limit)
1833         return *best_alg;
1834       else
1835         {
1836           best_alg->ops = 1;
1837           best_alg->cost = zero_cost;
1838           best_alg->op[0] = alg_zero;
1839           return *best_alg;
1840         }
1841     }
1842
1843   /* If we have a group of zero bits at the low-order part of T, try
1844      multiplying by the remaining bits and then doing a shift.  */
1845
1846   if ((t & 1) == 0)
1847     {
1848       m = floor_log2 (t & -t);  /* m = number of low zero bits */
1849       q = t >> m;
1850       cost = shift_cost[m];
1851       if (cost < cost_limit)
1852         {
1853           *alg_in = synth_mult (q, cost_limit - cost);
1854
1855           cost += alg_in->cost;
1856           if (cost < best_alg->cost)
1857             {
1858               struct algorithm *x;
1859               x = alg_in, alg_in = best_alg, best_alg = x;
1860               best_alg->log[best_alg->ops] = m;
1861               best_alg->op[best_alg->ops++] = alg_shift;
1862               best_alg->cost = cost_limit = cost;
1863             }
1864         }
1865     }
1866
1867   /* Look for factors of t of the form
1868      t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
1869      If we find such a factor, we can multiply by t using an algorithm that
1870      multiplies by q, shift the result by m and add/subtract it to itself.
1871
1872      We search for large factors first and loop down, even if large factors
1873      are less probable than small; if we find a large factor we will find a
1874      good sequence quickly, and therefore be able to prune (by decreasing
1875      COST_LIMIT) the search.  */
1876
1877   for (m = floor_log2 (t - 1); m >= 2; m--)
1878     {
1879       unsigned HOST_WIDE_INT d;
1880
1881       d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
1882       if (t % d == 0 && t > d)
1883         {
1884           cost = MIN (shiftadd_cost[m], add_cost + shift_cost[m]);
1885           *alg_in = synth_mult (t / d, cost_limit - cost);
1886
1887           cost += alg_in->cost;
1888           if (cost < best_alg->cost)
1889             {
1890               struct algorithm *x;
1891               x = alg_in, alg_in = best_alg, best_alg = x;
1892               best_alg->log[best_alg->ops] = m;
1893               best_alg->op[best_alg->ops++] = alg_add_factor;
1894               best_alg->cost = cost_limit = cost;
1895             }
1896         }
1897
1898       d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
1899       if (t % d == 0 && t > d)
1900         {
1901           cost = MIN (shiftsub_cost[m], add_cost + shift_cost[m]);
1902           *alg_in = synth_mult (t / d, cost_limit - cost);
1903
1904           cost += alg_in->cost;
1905           if (cost < best_alg->cost)
1906             {
1907               struct algorithm *x;
1908               x = alg_in, alg_in = best_alg, best_alg = x;
1909               best_alg->log[best_alg->ops] = m;
1910               best_alg->op[best_alg->ops++] = alg_sub_factor;
1911               best_alg->cost = cost_limit = cost;
1912             }
1913         }
1914     }
1915
1916   /* Try shift-and-add (load effective address) instructions,
1917      i.e. do a*3, a*5, a*9.  */
1918   if ((t & 1) != 0)
1919     {
1920       q = t - 1;
1921       q = q & -q;
1922       m = exact_log2 (q);
1923       if (m >= 0)
1924         {
1925           cost = shiftadd_cost[m];
1926           *alg_in = synth_mult ((t - 1) >> m, cost_limit - cost);
1927
1928           cost += alg_in->cost;
1929           if (cost < best_alg->cost)
1930             {
1931               struct algorithm *x;
1932               x = alg_in, alg_in = best_alg, best_alg = x;
1933               best_alg->log[best_alg->ops] = m;
1934               best_alg->op[best_alg->ops++] = alg_add_t2_m;
1935               best_alg->cost = cost_limit = cost;
1936             }
1937         }
1938
1939       q = t + 1;
1940       q = q & -q;
1941       m = exact_log2 (q);
1942       if (m >= 0)
1943         {
1944           cost = shiftsub_cost[m];
1945           *alg_in = synth_mult ((t + 1) >> m, cost_limit - cost);
1946
1947           cost += alg_in->cost;
1948           if (cost < best_alg->cost)
1949             {
1950               struct algorithm *x;
1951               x = alg_in, alg_in = best_alg, best_alg = x;
1952               best_alg->log[best_alg->ops] = m;
1953               best_alg->op[best_alg->ops++] = alg_sub_t2_m;
1954               best_alg->cost = cost_limit = cost;
1955             }
1956         }
1957     }
1958
1959   /* Now, use the simple method of adding or subtracting at the leftmost
1960      1-bit.  */
1961   {
1962     unsigned HOST_WIDE_INT w;
1963
1964     q = t & -t;                 /* get out lsb */
1965     for (w = q; (w & t) != 0; w <<= 1)
1966       ;
1967     if ((w > q << 1)
1968         /* Reject the case where t has only two bits.
1969            Thus we prefer addition in that case.  */
1970         && !(t < w && w == q << 2))
1971       {
1972         /* There are many bits in a row.  Make 'em by subtraction.  */
1973
1974         m = exact_log2 (q);
1975
1976         /* Don't use shiftsub_cost here, this operation
1977            scales wrong operand.  */
1978         cost = add_cost + shift_cost[m];
1979         *alg_in = synth_mult (t + q, cost_limit - cost);
1980
1981         cost += alg_in->cost;
1982         if (cost < best_alg->cost)
1983           {
1984             struct algorithm *x;
1985             x = alg_in, alg_in = best_alg, best_alg = x;
1986             best_alg->log[best_alg->ops] = m;
1987             best_alg->op[best_alg->ops++] = alg_sub_t_m2;
1988             best_alg->cost = cost_limit = cost;
1989           }
1990       }
1991     else
1992       {
1993         /* There's only one or two bit at the left.  Make it by addition.  */
1994
1995         m = exact_log2 (q);
1996         cost = MIN (shiftadd_cost[m], add_cost + shift_cost[m]);
1997         *alg_in = synth_mult (t - q, cost_limit - cost);
1998
1999         cost += alg_in->cost;
2000         if (cost < best_alg->cost)
2001           {
2002             struct algorithm *x;
2003             x = alg_in, alg_in = best_alg, best_alg = x;
2004             best_alg->log[best_alg->ops] = m;
2005             best_alg->op[best_alg->ops++] = alg_add_t_m2;
2006             best_alg->cost = cost_limit = cost;
2007           }
2008       }
2009   }
2010
2011   /* If we are getting a too long sequence for `struct algorithm'
2012      to record, store a fake cost to make this search fail.  */
2013   if (best_alg->ops == MAX_BITS_PER_WORD)
2014     best_alg->cost = cost_limit;
2015
2016   return *best_alg;
2017 }
2018 \f
2019 /* Perform a multiplication and return an rtx for the result.
2020    MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
2021    TARGET is a suggestion for where to store the result (an rtx).
2022
2023    We check specially for a constant integer as OP1.
2024    If you want this check for OP0 as well, then before calling
2025    you should swap the two operands if OP0 would be constant.  */
2026
2027 rtx
2028 expand_mult (mode, op0, op1, target, unsignedp)
2029      enum machine_mode mode;
2030      register rtx op0, op1, target;
2031      int unsignedp;
2032 {
2033   rtx const_op1 = op1;
2034
2035   /* If we are multiplying in DImode, it may still be a win
2036      to try to work with shifts and adds.  */
2037   if (GET_CODE (op1) == CONST_DOUBLE
2038       && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
2039       && HOST_BITS_PER_INT <= BITS_PER_WORD)
2040     {
2041       if ((CONST_DOUBLE_HIGH (op1) == 0 && CONST_DOUBLE_LOW (op1) >= 0)
2042           || (CONST_DOUBLE_HIGH (op1) == -1 && CONST_DOUBLE_LOW (op1) < 0))
2043         const_op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2044     }
2045
2046   /* We used to test optimize here, on the grounds that it's better to
2047      produce a smaller program when -O is not used.
2048      But this causes such a terrible slowdown sometimes
2049      that it seems better to use synth_mult always.  */
2050
2051   if (GET_CODE (const_op1) == CONST_INT && ! mult_is_very_cheap)
2052     {
2053       struct algorithm alg;
2054       struct algorithm neg_alg;
2055       int negate = 0;
2056       HOST_WIDE_INT val = INTVAL (op1);
2057       HOST_WIDE_INT val_so_far;
2058       rtx insn;
2059
2060       /* Try to do the computation two ways: multiply by the negative of OP1
2061          and then negate, or do the multiplication directly.  The latter is
2062          usually faster for positive numbers and the former for negative
2063          numbers, but the opposite can be faster if the original value
2064          has a factor of 2**m +/- 1, while the negated value does not or
2065          vice versa.  */
2066
2067       alg = synth_mult (val, mult_cost);
2068       neg_alg = synth_mult (- val,
2069                             (alg.cost < mult_cost ? alg.cost : mult_cost)
2070                             - negate_cost);
2071
2072       if (neg_alg.cost + negate_cost < alg.cost)
2073         alg = neg_alg, negate = 1;
2074
2075       if (alg.cost < mult_cost)
2076         {
2077           /* We found something cheaper than a multiply insn.  */
2078           int opno;
2079           rtx accum, tem;
2080
2081           op0 = protect_from_queue (op0, 0);
2082
2083           /* Avoid referencing memory over and over.
2084              For speed, but also for correctness when mem is volatile.  */
2085           if (GET_CODE (op0) == MEM)
2086             op0 = force_reg (mode, op0);
2087
2088           /* ACCUM starts out either as OP0 or as a zero, depending on
2089              the first operation.  */
2090
2091           if (alg.op[0] == alg_zero)
2092             {
2093               accum = copy_to_mode_reg (mode, const0_rtx);
2094               val_so_far = 0;
2095             }
2096           else if (alg.op[0] == alg_m)
2097             {
2098               accum  = copy_to_mode_reg (mode, op0);
2099               val_so_far = 1;
2100             }
2101           else
2102             abort ();
2103
2104           for (opno = 1; opno < alg.ops; opno++)
2105             {
2106               int log = alg.log[opno];
2107               rtx shift_subtarget = preserve_subexpressions_p () ? 0 : accum;
2108               rtx add_target = opno == alg.ops - 1 && target != 0 ? target : 0;
2109
2110               switch (alg.op[opno])
2111                 {
2112                 case alg_shift:
2113                   accum = expand_shift (LSHIFT_EXPR, mode, accum,
2114                                         build_int_2 (log, 0), NULL_RTX, 0);
2115                   val_so_far <<= log;
2116                   break;
2117
2118                 case alg_add_t_m2:
2119                   tem = expand_shift (LSHIFT_EXPR, mode, op0,
2120                                       build_int_2 (log, 0), NULL_RTX, 0);
2121                   accum = force_operand (gen_rtx (PLUS, mode, accum, tem),
2122                                          add_target ? add_target : accum);
2123                   val_so_far += (HOST_WIDE_INT) 1 << log;
2124                   break;
2125
2126                 case alg_sub_t_m2:
2127                   tem = expand_shift (LSHIFT_EXPR, mode, op0,
2128                                       build_int_2 (log, 0), NULL_RTX, 0);
2129                   accum = force_operand (gen_rtx (MINUS, mode, accum, tem),
2130                                          add_target ? add_target : accum);
2131                   val_so_far -= (HOST_WIDE_INT) 1 << log;
2132                   break;
2133
2134                 case alg_add_t2_m:
2135                   accum = expand_shift (LSHIFT_EXPR, mode, accum,
2136                                         build_int_2 (log, 0), accum, 0);
2137                   accum = force_operand (gen_rtx (PLUS, mode, accum, op0),
2138                                          add_target ? add_target : accum);
2139                   val_so_far = (val_so_far << log) + 1;
2140                   break;
2141
2142                 case alg_sub_t2_m:
2143                   accum = expand_shift (LSHIFT_EXPR, mode, accum,
2144                                         build_int_2 (log, 0), accum, 0);
2145                   accum = force_operand (gen_rtx (MINUS, mode, accum, op0),
2146                                          add_target ? add_target : accum);
2147                   val_so_far = (val_so_far << log) - 1;
2148                   break;
2149
2150                 case alg_add_factor:
2151                   tem = expand_shift (LSHIFT_EXPR, mode, accum,
2152                                       build_int_2 (log, 0), NULL_RTX, 0);
2153                   accum = force_operand (gen_rtx (PLUS, mode, accum, tem),
2154                                          add_target ? add_target : accum);
2155                   val_so_far += val_so_far << log;
2156                   break;
2157
2158                 case alg_sub_factor:
2159                   tem = expand_shift (LSHIFT_EXPR, mode, accum,
2160                                       build_int_2 (log, 0), NULL_RTX, 0);
2161                   accum = force_operand (gen_rtx (MINUS, mode, tem, accum),
2162                                          add_target ? add_target : tem);
2163                   val_so_far = (val_so_far << log) - val_so_far;
2164                   break;
2165
2166                 default:
2167                   abort ();;
2168                 }
2169
2170               /* Write a REG_EQUAL note on the last insn so that we can cse
2171                  multiplication sequences.  */
2172
2173               insn = get_last_insn ();
2174               REG_NOTES (insn)
2175                 = gen_rtx (EXPR_LIST, REG_EQUAL,
2176                            gen_rtx (MULT, mode, op0, GEN_INT (val_so_far)),
2177                            REG_NOTES (insn));
2178             }
2179
2180           if (negate)
2181             {
2182               val_so_far = - val_so_far;
2183               accum = expand_unop (mode, neg_optab, accum, target, 0);
2184             }
2185
2186           if (val != val_so_far)
2187             abort ();
2188
2189           return accum;
2190         }
2191     }
2192
2193   /* This used to use umul_optab if unsigned,
2194      but for non-widening multiply there is no difference
2195      between signed and unsigned.  */
2196   op0 = expand_binop (mode, smul_optab,
2197                       op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
2198   if (op0 == 0)
2199     abort ();
2200   return op0;
2201 }
2202 \f
2203 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
2204    if that is convenient, and returning where the result is.
2205    You may request either the quotient or the remainder as the result;
2206    specify REM_FLAG nonzero to get the remainder.
2207
2208    CODE is the expression code for which kind of division this is;
2209    it controls how rounding is done.  MODE is the machine mode to use.
2210    UNSIGNEDP nonzero means do unsigned division.  */
2211
2212 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
2213    and then correct it by or'ing in missing high bits
2214    if result of ANDI is nonzero.
2215    For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
2216    This could optimize to a bfexts instruction.
2217    But C doesn't use these operations, so their optimizations are
2218    left for later.  */
2219
2220 rtx
2221 expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
2222      int rem_flag;
2223      enum tree_code code;
2224      enum machine_mode mode;
2225      register rtx op0, op1, target;
2226      int unsignedp;
2227 {
2228   register rtx result = 0;
2229   enum machine_mode compute_mode;
2230   int log = -1;
2231   int size;
2232   int can_clobber_op0;
2233   int mod_insn_no_good = 0;
2234   rtx adjusted_op0 = op0;
2235   optab optab1, optab2;
2236
2237   /* We shouldn't be called with op1 == const1_rtx, but some of the
2238      code below will malfunction if we are, so check here and handle
2239      the special case if so.  */
2240   if (op1 == const1_rtx)
2241     return rem_flag ? const0_rtx : op0;
2242
2243   /* Don't use the function value register as a target
2244      since we have to read it as well as write it,
2245      and function-inlining gets confused by this.  */
2246   if (target && REG_P (target) && REG_FUNCTION_VALUE_P (target))
2247     target = 0;
2248
2249   /* Don't clobber an operand while doing a multi-step calculation.  */
2250   if (target)
2251     if ((rem_flag && (reg_mentioned_p (target, op0)
2252                       || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
2253         || reg_mentioned_p (target, op1)
2254         || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM))
2255       target = 0;
2256
2257   can_clobber_op0 = (GET_CODE (op0) == REG && op0 == target);
2258
2259   if (GET_CODE (op1) == CONST_INT)
2260     log = exact_log2 (INTVAL (op1));
2261
2262   /* If log is >= 0, we are dividing by 2**log, and will do it by shifting,
2263      which is really floor-division.  Otherwise we will really do a divide,
2264      and we assume that is trunc-division.
2265
2266      We must correct the dividend by adding or subtracting something
2267      based on the divisor, in order to do the kind of rounding specified
2268      by CODE.  The correction depends on what kind of rounding is actually
2269      available, and that depends on whether we will shift or divide.
2270
2271      In many of these cases it is possible to perform the operation by a
2272      clever series of logical operations (shifts and/or exclusive-ors).
2273      Although avoiding the jump has the advantage that it extends the basic
2274      block and allows further optimization, the branch-free code is normally
2275      at least one instruction longer in the (most common) case where the
2276      dividend is non-negative.  Performance measurements of the two
2277      alternatives show that the branch-free code is slightly faster on the
2278      IBM ROMP but slower on CISC processors (significantly slower on the
2279      VAX).  Accordingly, the jump code has been retained.
2280
2281      On machines where the jump code is slower, the cost of a DIV or MOD
2282      operation can be set small (less than twice that of an addition); in 
2283      that case, we pretend that we don't have a power of two and perform
2284      a normal division or modulus operation.  */
2285
2286   if ((code == TRUNC_MOD_EXPR || code == TRUNC_DIV_EXPR)
2287       && ! unsignedp
2288       && (rem_flag ? smod_pow2_cheap : sdiv_pow2_cheap))
2289     log = -1;
2290
2291   /* Get the mode in which to perform this computation.  Normally it will
2292      be MODE, but sometimes we can't do the desired operation in MODE.
2293      If so, pick a wider mode in which we can do the operation.  Convert
2294      to that mode at the start to avoid repeated conversions.
2295
2296      First see what operations we need.  These depend on the expression
2297      we are evaluating.  (We assume that divxx3 insns exist under the
2298      same conditions that modxx3 insns and that these insns don't normally
2299      fail.  If these assumptions are not correct, we may generate less
2300      efficient code in some cases.)
2301
2302      Then see if we find a mode in which we can open-code that operation
2303      (either a division, modulus, or shift).  Finally, check for the smallest
2304      mode for which we can do the operation with a library call.  */
2305
2306   optab1 = (log >= 0 ? (unsignedp ? lshr_optab : ashr_optab)
2307             : (unsignedp ? udiv_optab : sdiv_optab));
2308   optab2 = (log >= 0 ? optab1 : (unsignedp ? udivmod_optab : sdivmod_optab));
2309
2310   for (compute_mode = mode; compute_mode != VOIDmode;
2311        compute_mode = GET_MODE_WIDER_MODE (compute_mode))
2312     if (optab1->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing
2313         || optab2->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing)
2314       break;
2315
2316   if (compute_mode == VOIDmode)
2317     for (compute_mode = mode; compute_mode != VOIDmode;
2318          compute_mode = GET_MODE_WIDER_MODE (compute_mode))
2319       if (optab1->handlers[(int) compute_mode].libfunc
2320           || optab2->handlers[(int) compute_mode].libfunc)
2321         break;
2322
2323   /* If we still couldn't find a mode, use MODE; we'll probably abort in
2324      expand_binop.  */
2325   if (compute_mode == VOIDmode)
2326     compute_mode = mode;
2327
2328   size = GET_MODE_BITSIZE (compute_mode);
2329
2330   /* Now convert to the best mode to use.  Show we made a copy of OP0
2331      and hence we can clobber it (we cannot use a SUBREG to widen
2332      something.  */
2333   if (compute_mode != mode)
2334     {
2335       adjusted_op0 = op0 = convert_to_mode (compute_mode, op0, unsignedp);
2336       can_clobber_op0 = 1;
2337       op1 = convert_to_mode (compute_mode, op1, unsignedp);
2338     }
2339
2340   /* If we are computing the remainder and one of the operands is a volatile
2341      MEM, copy it into a register.  */
2342
2343   if (rem_flag && GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0))
2344     adjusted_op0 = op0 = force_reg (compute_mode, op0), can_clobber_op0 = 1;
2345   if (rem_flag && GET_CODE (op1) == MEM && MEM_VOLATILE_P (op1))
2346     op1 = force_reg (compute_mode, op1);
2347
2348   /* If we are computing the remainder, op0 will be needed later to calculate
2349      X - Y * (X / Y), therefore cannot be clobbered. */
2350   if (rem_flag)
2351     can_clobber_op0 = 0;
2352
2353   if (target == 0 || GET_MODE (target) != compute_mode)
2354     target = gen_reg_rtx (compute_mode);
2355
2356   switch (code)
2357     {
2358     case TRUNC_MOD_EXPR:
2359     case TRUNC_DIV_EXPR:
2360       if (log >= 0 && ! unsignedp)
2361         {
2362           if (! can_clobber_op0)
2363             {
2364               adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
2365                                                     compute_mode);
2366               /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
2367                  which will screw up mem refs for autoincrements.  */
2368               op0 = force_reg (compute_mode, op0);
2369             }
2370           /* Here we need to add OP1-1 if OP0 is negative, 0 otherwise.
2371              This can be computed without jumps by arithmetically shifting
2372              OP0 right LOG-1 places and then shifting right logically
2373              SIZE-LOG bits.  The resulting value is unconditionally added
2374              to OP0.  */
2375           if (log == 1 || BRANCH_COST >= 3)
2376             {
2377               rtx temp = gen_reg_rtx (compute_mode);
2378               temp = copy_to_suggested_reg (adjusted_op0, temp, compute_mode);
2379               temp = expand_shift (RSHIFT_EXPR, compute_mode, temp,
2380                                    build_int_2 (log - 1, 0), NULL_RTX, 0);
2381               temp = expand_shift (RSHIFT_EXPR, compute_mode, temp,
2382                                    build_int_2 (size - log, 0),
2383                                    temp, 1);
2384               expand_inc (adjusted_op0, temp);
2385             }
2386           else
2387             {
2388               rtx label = gen_label_rtx ();
2389               emit_cmp_insn (adjusted_op0, const0_rtx, GE, 
2390                              NULL_RTX, compute_mode, 0, 0);
2391               emit_jump_insn (gen_bge (label));
2392               expand_inc (adjusted_op0, plus_constant (op1, -1));
2393               emit_label (label);
2394             }
2395           mod_insn_no_good = 1;
2396         }
2397       break;
2398
2399     case FLOOR_DIV_EXPR:
2400     case FLOOR_MOD_EXPR:
2401       if (log < 0 && ! unsignedp)
2402         {
2403           rtx label = gen_label_rtx ();
2404           if (! can_clobber_op0)
2405             {
2406               adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
2407                                                     compute_mode);
2408               /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
2409                  which will screw up mem refs for autoincrements.  */
2410               op0 = force_reg (compute_mode, op0);
2411             }
2412           emit_cmp_insn (adjusted_op0, const0_rtx, GE, 
2413                          NULL_RTX, compute_mode, 0, 0);
2414           emit_jump_insn (gen_bge (label));
2415           expand_dec (adjusted_op0, op1);
2416           expand_inc (adjusted_op0, const1_rtx);
2417           emit_label (label);
2418           mod_insn_no_good = 1;
2419         }
2420       break;
2421
2422     case CEIL_DIV_EXPR:
2423     case CEIL_MOD_EXPR:
2424       if (! can_clobber_op0)
2425         {
2426           adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
2427                                                 compute_mode);
2428           /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
2429              which will screw up mem refs for autoincrements.  */
2430           op0 = force_reg (compute_mode, op0);
2431         }
2432       if (log < 0)
2433         {
2434           rtx label = 0;
2435           if (! unsignedp)
2436             {
2437               label = gen_label_rtx ();
2438               emit_cmp_insn (adjusted_op0, const0_rtx, LE, 
2439                              NULL_RTX, compute_mode, 0, 0);
2440               emit_jump_insn (gen_ble (label));
2441             }
2442           expand_inc (adjusted_op0, op1);
2443           expand_dec (adjusted_op0, const1_rtx);
2444           if (! unsignedp)
2445             emit_label (label);
2446         }
2447       else
2448         {
2449           adjusted_op0 = expand_binop (compute_mode, add_optab,
2450                                        adjusted_op0, plus_constant (op1, -1),
2451                                        NULL_RTX, 0, OPTAB_LIB_WIDEN);
2452         }
2453       mod_insn_no_good = 1;
2454       break;
2455
2456     case ROUND_DIV_EXPR:
2457     case ROUND_MOD_EXPR:
2458       if (! can_clobber_op0)
2459         {
2460           adjusted_op0 = copy_to_suggested_reg (adjusted_op0, target,
2461                                                 compute_mode);
2462           /* Copy op0 to a reg, since emit_cmp_insn will call emit_queue
2463              which will screw up mem refs for autoincrements.  */
2464           op0 = force_reg (compute_mode, op0);
2465         }
2466       if (log < 0)
2467         {
2468           op1 = expand_shift (RSHIFT_EXPR, compute_mode, op1,
2469                               integer_one_node, NULL_RTX, 0);
2470           if (! unsignedp)
2471             {
2472               if (BRANCH_COST >= 2)
2473                 {
2474                   /* Negate OP1 if OP0 < 0.  Do this by computing a temporary
2475                      that has all bits equal to the sign bit and exclusive
2476                      or-ing it with OP1.  */
2477                   rtx temp = gen_reg_rtx (compute_mode);
2478                   temp = copy_to_suggested_reg (adjusted_op0, temp, compute_mode);
2479                   temp = expand_shift (RSHIFT_EXPR, compute_mode, temp,
2480                                        build_int_2 (size - 1, 0),
2481                                        NULL_RTX, 0);
2482                   op1 = expand_binop (compute_mode, xor_optab, op1, temp, op1,
2483                                       unsignedp, OPTAB_LIB_WIDEN);
2484                 }
2485               else
2486                 {
2487                   rtx label = gen_label_rtx ();
2488                   emit_cmp_insn (adjusted_op0, const0_rtx, GE, NULL_RTX,
2489                                  compute_mode, 0, 0);
2490                   emit_jump_insn (gen_bge (label));
2491                   expand_unop (compute_mode, neg_optab, op1, op1, 0);
2492                   emit_label (label);
2493                 }
2494             }
2495           expand_inc (adjusted_op0, op1);
2496         }
2497       else
2498         {
2499           op1 = GEN_INT (((HOST_WIDE_INT) 1 << log) / 2);
2500           expand_inc (adjusted_op0, op1);
2501         }
2502       mod_insn_no_good = 1;
2503       break;
2504     }
2505
2506   if (rem_flag && !mod_insn_no_good)
2507     {
2508       /* Try to produce the remainder directly */
2509       if (log >= 0)
2510         result = expand_binop (compute_mode, and_optab, adjusted_op0,
2511                                GEN_INT (((HOST_WIDE_INT) 1 << log) - 1),
2512                                target, 1, OPTAB_LIB_WIDEN);
2513       else
2514         {
2515           /* See if we can do remainder without a library call.  */
2516           result = sign_expand_binop (mode, umod_optab, smod_optab,
2517                                       adjusted_op0, op1, target,
2518                                       unsignedp, OPTAB_WIDEN);
2519           if (result == 0)
2520             {
2521               /* No luck there.  Can we do remainder and divide at once
2522                  without a library call?  */
2523               result = gen_reg_rtx (compute_mode);
2524               if (! expand_twoval_binop (unsignedp
2525                                          ? udivmod_optab : sdivmod_optab,
2526                                          adjusted_op0, op1,
2527                                          NULL_RTX, result, unsignedp))
2528                 result = 0;
2529             }
2530         }
2531     }
2532
2533   if (result)
2534     return gen_lowpart (mode, result);
2535
2536   /* Produce the quotient.  */
2537   if (log >= 0)
2538     result = expand_shift (RSHIFT_EXPR, compute_mode, adjusted_op0,
2539                            build_int_2 (log, 0), target, unsignedp);
2540   else if (rem_flag && !mod_insn_no_good)
2541     /* If producing quotient in order to subtract for remainder,
2542        and a remainder subroutine would be ok,
2543        don't use a divide subroutine.  */
2544     result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
2545                                 adjusted_op0, op1, NULL_RTX, unsignedp,
2546                                 OPTAB_WIDEN);
2547   else
2548     {
2549       /* Try a quotient insn, but not a library call.  */
2550       result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
2551                                   adjusted_op0, op1,
2552                                   rem_flag ? NULL_RTX : target,
2553                                   unsignedp, OPTAB_WIDEN);
2554       if (result == 0)
2555         {
2556           /* No luck there.  Try a quotient-and-remainder insn,
2557              keeping the quotient alone.  */
2558           result = gen_reg_rtx (mode);
2559           if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
2560                                      adjusted_op0, op1,
2561                                      result, NULL_RTX, unsignedp))
2562             result = 0;
2563         }
2564
2565       /* If still no luck, use a library call.  */
2566       if (result == 0)
2567         result = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
2568                                     adjusted_op0, op1,
2569                                     rem_flag ? NULL_RTX : target,
2570                                     unsignedp, OPTAB_LIB_WIDEN);
2571     }
2572
2573   /* If we really want the remainder, get it by subtraction.  */
2574   if (rem_flag)
2575     {
2576       if (result == 0)
2577         /* No divide instruction either.  Use library for remainder.  */
2578         result = sign_expand_binop (compute_mode, umod_optab, smod_optab,
2579                                     op0, op1, target,
2580                                     unsignedp, OPTAB_LIB_WIDEN);
2581       else
2582         {
2583           /* We divided.  Now finish doing X - Y * (X / Y).  */
2584           result = expand_mult (compute_mode, result, op1, target, unsignedp);
2585           if (! result) abort ();
2586           result = expand_binop (compute_mode, sub_optab, op0,
2587                                  result, target, unsignedp, OPTAB_LIB_WIDEN);
2588         }
2589     }
2590
2591   if (result == 0)
2592     abort ();
2593
2594   return gen_lowpart (mode, result);
2595 }
2596 \f
2597 /* Return a tree node with data type TYPE, describing the value of X.
2598    Usually this is an RTL_EXPR, if there is no obvious better choice.
2599    X may be an expression, however we only support those expressions
2600    generated by loop.c.   */
2601
2602 tree
2603 make_tree (type, x)
2604      tree type;
2605      rtx x;
2606 {
2607   tree t;
2608
2609   switch (GET_CODE (x))
2610     {
2611     case CONST_INT:
2612       t = build_int_2 (INTVAL (x),
2613                        ! TREE_UNSIGNED (type) && INTVAL (x) >= 0 ? 0 : -1);
2614       TREE_TYPE (t) = type;
2615       return t;
2616
2617     case CONST_DOUBLE:
2618       if (GET_MODE (x) == VOIDmode)
2619         {
2620           t = build_int_2 (CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
2621           TREE_TYPE (t) = type;
2622         }
2623       else
2624         {
2625           REAL_VALUE_TYPE d;
2626
2627           REAL_VALUE_FROM_CONST_DOUBLE (d, x);
2628           t = build_real (type, d);
2629         }
2630
2631       return t;
2632           
2633     case PLUS:
2634       return fold (build (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
2635                           make_tree (type, XEXP (x, 1))));
2636                                                        
2637     case MINUS:
2638       return fold (build (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
2639                           make_tree (type, XEXP (x, 1))));
2640                                                        
2641     case NEG:
2642       return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
2643
2644     case MULT:
2645       return fold (build (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
2646                           make_tree (type, XEXP (x, 1))));
2647                                                       
2648     case ASHIFT:
2649       return fold (build (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
2650                           make_tree (type, XEXP (x, 1))));
2651                                                       
2652     case LSHIFTRT:
2653       return fold (convert (type,
2654                             build (RSHIFT_EXPR, unsigned_type (type),
2655                                    make_tree (unsigned_type (type),
2656                                               XEXP (x, 0)),
2657                                    make_tree (type, XEXP (x, 1)))));
2658                                                       
2659     case ASHIFTRT:
2660       return fold (convert (type,
2661                             build (RSHIFT_EXPR, signed_type (type),
2662                                    make_tree (signed_type (type), XEXP (x, 0)),
2663                                    make_tree (type, XEXP (x, 1)))));
2664                                                       
2665     case DIV:
2666       if (TREE_CODE (type) != REAL_TYPE)
2667         t = signed_type (type);
2668       else
2669         t = type;
2670
2671       return fold (convert (type,
2672                             build (TRUNC_DIV_EXPR, t,
2673                                    make_tree (t, XEXP (x, 0)),
2674                                    make_tree (t, XEXP (x, 1)))));
2675     case UDIV:
2676       t = unsigned_type (type);
2677       return fold (convert (type,
2678                             build (TRUNC_DIV_EXPR, t,
2679                                    make_tree (t, XEXP (x, 0)),
2680                                    make_tree (t, XEXP (x, 1)))));
2681    default:
2682       t = make_node (RTL_EXPR);
2683       TREE_TYPE (t) = type;
2684       RTL_EXPR_RTL (t) = x;
2685       /* There are no insns to be output
2686          when this rtl_expr is used.  */
2687       RTL_EXPR_SEQUENCE (t) = 0;
2688       return t;
2689     }
2690 }
2691
2692 /* Return an rtx representing the value of X * MULT + ADD.
2693    TARGET is a suggestion for where to store the result (an rtx).
2694    MODE is the machine mode for the computation.
2695    X and MULT must have mode MODE.  ADD may have a different mode.
2696    So can X (defaults to same as MODE).
2697    UNSIGNEDP is non-zero to do unsigned multiplication.
2698    This may emit insns.  */
2699
2700 rtx
2701 expand_mult_add (x, target, mult, add, mode, unsignedp)
2702      rtx x, target, mult, add;
2703      enum machine_mode mode;
2704      int unsignedp;
2705 {
2706   tree type = type_for_mode (mode, unsignedp);
2707   tree add_type = (GET_MODE (add) == VOIDmode
2708                    ? type : type_for_mode (GET_MODE (add), unsignedp));
2709   tree result =  fold (build (PLUS_EXPR, type,
2710                               fold (build (MULT_EXPR, type,
2711                                            make_tree (type, x),
2712                                            make_tree (type, mult))),
2713                               make_tree (add_type, add)));
2714
2715   return expand_expr (result, target, VOIDmode, 0);
2716 }
2717 \f
2718 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
2719    and returning TARGET.
2720
2721    If TARGET is 0, a pseudo-register or constant is returned.  */
2722
2723 rtx
2724 expand_and (op0, op1, target)
2725      rtx op0, op1, target;
2726 {
2727   enum machine_mode mode = VOIDmode;
2728   rtx tem;
2729
2730   if (GET_MODE (op0) != VOIDmode)
2731     mode = GET_MODE (op0);
2732   else if (GET_MODE (op1) != VOIDmode)
2733     mode = GET_MODE (op1);
2734
2735   if (mode != VOIDmode)
2736     tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
2737   else if (GET_CODE (op0) == CONST_INT && GET_CODE (op1) == CONST_INT)
2738     tem = GEN_INT (INTVAL (op0) & INTVAL (op1));
2739   else
2740     abort ();
2741
2742   if (target == 0)
2743     target = tem;
2744   else if (tem != target)
2745     emit_move_insn (target, tem);
2746   return target;
2747 }
2748 \f
2749 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
2750    and storing in TARGET.  Normally return TARGET.
2751    Return 0 if that cannot be done.
2752
2753    MODE is the mode to use for OP0 and OP1 should they be CONST_INTs.  If
2754    it is VOIDmode, they cannot both be CONST_INT.  
2755
2756    UNSIGNEDP is for the case where we have to widen the operands
2757    to perform the operation.  It says to use zero-extension.
2758
2759    NORMALIZEP is 1 if we should convert the result to be either zero
2760    or one one.  Normalize is -1 if we should convert the result to be
2761    either zero or -1.  If NORMALIZEP is zero, the result will be left
2762    "raw" out of the scc insn.  */
2763
2764 rtx
2765 emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep)
2766      rtx target;
2767      enum rtx_code code;
2768      rtx op0, op1;
2769      enum machine_mode mode;
2770      int unsignedp;
2771      int normalizep;
2772 {
2773   rtx subtarget;
2774   enum insn_code icode;
2775   enum machine_mode compare_mode;
2776   enum machine_mode target_mode = GET_MODE (target);
2777   rtx tem;
2778   rtx last = 0;
2779   rtx pattern, comparison;
2780
2781   if (mode == VOIDmode)
2782     mode = GET_MODE (op0);
2783
2784   /* If one operand is constant, make it the second one.  Only do this
2785      if the other operand is not constant as well.  */
2786
2787   if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
2788       || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
2789     {
2790       tem = op0;
2791       op0 = op1;
2792       op1 = tem;
2793       code = swap_condition (code);
2794     }
2795
2796   /* For some comparisons with 1 and -1, we can convert this to 
2797      comparisons with zero.  This will often produce more opportunities for
2798      store-flag insns. */
2799
2800   switch (code)
2801     {
2802     case LT:
2803       if (op1 == const1_rtx)
2804         op1 = const0_rtx, code = LE;
2805       break;
2806     case LE:
2807       if (op1 == constm1_rtx)
2808         op1 = const0_rtx, code = LT;
2809       break;
2810     case GE:
2811       if (op1 == const1_rtx)
2812         op1 = const0_rtx, code = GT;
2813       break;
2814     case GT:
2815       if (op1 == constm1_rtx)
2816         op1 = const0_rtx, code = GE;
2817       break;
2818     case GEU:
2819       if (op1 == const1_rtx)
2820         op1 = const0_rtx, code = NE;
2821       break;
2822     case LTU:
2823       if (op1 == const1_rtx)
2824         op1 = const0_rtx, code = EQ;
2825       break;
2826     }
2827
2828   /* From now on, we won't change CODE, so set ICODE now.  */
2829   icode = setcc_gen_code[(int) code];
2830
2831   /* If this is A < 0 or A >= 0, we can do this by taking the ones
2832      complement of A (for GE) and shifting the sign bit to the low bit.  */
2833   if (op1 == const0_rtx && (code == LT || code == GE)
2834       && GET_MODE_CLASS (mode) == MODE_INT
2835       && (normalizep || STORE_FLAG_VALUE == 1
2836           || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
2837               && (STORE_FLAG_VALUE 
2838                   == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
2839     {
2840       subtarget = target;
2841
2842       /* If the result is to be wider than OP0, it is best to convert it
2843          first.  If it is to be narrower, it is *incorrect* to convert it
2844          first.  */
2845       if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
2846         {
2847           op0 = protect_from_queue (op0, 0);
2848           op0 = convert_to_mode (target_mode, op0, 0);
2849           mode = target_mode;
2850         }
2851
2852       if (target_mode != mode)
2853         subtarget = 0;
2854
2855       if (code == GE)
2856         op0 = expand_unop (mode, one_cmpl_optab, op0, subtarget, 0);
2857
2858       if (normalizep || STORE_FLAG_VALUE == 1)
2859         /* If we are supposed to produce a 0/1 value, we want to do
2860            a logical shift from the sign bit to the low-order bit; for
2861            a -1/0 value, we do an arithmetic shift.  */
2862         op0 = expand_shift (RSHIFT_EXPR, mode, op0,
2863                             size_int (GET_MODE_BITSIZE (mode) - 1),
2864                             subtarget, normalizep != -1);
2865
2866       if (mode != target_mode)
2867         op0 = convert_to_mode (target_mode, op0, 0);
2868
2869       return op0;
2870     }
2871
2872   if (icode != CODE_FOR_nothing)
2873     {
2874       /* We think we may be able to do this with a scc insn.  Emit the
2875          comparison and then the scc insn.
2876
2877          compare_from_rtx may call emit_queue, which would be deleted below
2878          if the scc insn fails.  So call it ourselves before setting LAST.  */
2879
2880       emit_queue ();
2881       last = get_last_insn ();
2882
2883       comparison
2884         = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX, 0);
2885       if (GET_CODE (comparison) == CONST_INT)
2886         return (comparison == const0_rtx ? const0_rtx
2887                 : normalizep == 1 ? const1_rtx
2888                 : normalizep == -1 ? constm1_rtx
2889                 : const_true_rtx);
2890
2891       /* If the code of COMPARISON doesn't match CODE, something is
2892          wrong; we can no longer be sure that we have the operation.  
2893          We could handle this case, but it should not happen.  */
2894
2895       if (GET_CODE (comparison) != code)
2896         abort ();
2897
2898       /* Get a reference to the target in the proper mode for this insn.  */
2899       compare_mode = insn_operand_mode[(int) icode][0];
2900       subtarget = target;
2901       if (preserve_subexpressions_p ()
2902           || ! (*insn_operand_predicate[(int) icode][0]) (subtarget, compare_mode))
2903         subtarget = gen_reg_rtx (compare_mode);
2904
2905       pattern = GEN_FCN (icode) (subtarget);
2906       if (pattern)
2907         {
2908           emit_insn (pattern);
2909
2910           /* If we are converting to a wider mode, first convert to
2911              TARGET_MODE, then normalize.  This produces better combining
2912              opportunities on machines that have a SIGN_EXTRACT when we are
2913              testing a single bit.  This mostly benefits the 68k.
2914
2915              If STORE_FLAG_VALUE does not have the sign bit set when
2916              interpreted in COMPARE_MODE, we can do this conversion as
2917              unsigned, which is usually more efficient.  */
2918           if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
2919             {
2920               convert_move (target, subtarget,
2921                             (GET_MODE_BITSIZE (compare_mode)
2922                              <= HOST_BITS_PER_WIDE_INT)
2923                             && 0 == (STORE_FLAG_VALUE
2924                                      & ((HOST_WIDE_INT) 1
2925                                         << (GET_MODE_BITSIZE (compare_mode) -1))));
2926               op0 = target;
2927               compare_mode = target_mode;
2928             }
2929           else
2930             op0 = subtarget;
2931
2932           /* If we want to keep subexpressions around, don't reuse our
2933              last target.  */
2934
2935           if (preserve_subexpressions_p ())
2936             subtarget = 0;
2937
2938           /* Now normalize to the proper value in COMPARE_MODE.  Sometimes
2939              we don't have to do anything.  */
2940           if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
2941             ;
2942           else if (normalizep == - STORE_FLAG_VALUE)
2943             op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
2944
2945           /* We don't want to use STORE_FLAG_VALUE < 0 below since this
2946              makes it hard to use a value of just the sign bit due to
2947              ANSI integer constant typing rules.  */
2948           else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
2949                    && (STORE_FLAG_VALUE
2950                        & ((HOST_WIDE_INT) 1
2951                           << (GET_MODE_BITSIZE (compare_mode) - 1))))
2952             op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
2953                                 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
2954                                 subtarget, normalizep == 1);
2955           else if (STORE_FLAG_VALUE & 1)
2956             {
2957               op0 = expand_and (op0, const1_rtx, subtarget);
2958               if (normalizep == -1)
2959                 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
2960             }
2961           else
2962             abort ();
2963
2964           /* If we were converting to a smaller mode, do the 
2965              conversion now.  */
2966           if (target_mode != compare_mode)
2967             {
2968               convert_move (target, op0, 0);
2969               return target;
2970             }
2971           else
2972             return op0;
2973         }
2974     }
2975
2976   if (last)
2977     delete_insns_since (last);
2978
2979   subtarget = target_mode == mode ? target : 0;
2980
2981   /* If we reached here, we can't do this with a scc insn.  However, there
2982      are some comparisons that can be done directly.  For example, if
2983      this is an equality comparison of integers, we can try to exclusive-or
2984      (or subtract) the two operands and use a recursive call to try the
2985      comparison with zero.  Don't do any of these cases if branches are
2986      very cheap.  */
2987
2988   if (BRANCH_COST > 0
2989       && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
2990       && op1 != const0_rtx)
2991     {
2992       tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
2993                           OPTAB_WIDEN);
2994
2995       if (tem == 0)
2996         tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
2997                             OPTAB_WIDEN);
2998       if (tem != 0)
2999         tem = emit_store_flag (target, code, tem, const0_rtx,
3000                                mode, unsignedp, normalizep);
3001       if (tem == 0)
3002         delete_insns_since (last);
3003       return tem;
3004     }
3005
3006   /* Some other cases we can do are EQ, NE, LE, and GT comparisons with 
3007      the constant zero.  Reject all other comparisons at this point.  Only
3008      do LE and GT if branches are expensive since they are expensive on
3009      2-operand machines.  */
3010
3011   if (BRANCH_COST == 0
3012       || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
3013       || (code != EQ && code != NE
3014           && (BRANCH_COST <= 1 || (code != LE && code != GT))))
3015     return 0;
3016
3017   /* See what we need to return.  We can only return a 1, -1, or the
3018      sign bit.  */
3019
3020   if (normalizep == 0)
3021     {
3022       if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
3023         normalizep = STORE_FLAG_VALUE;
3024
3025       else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3026                && (STORE_FLAG_VALUE
3027                    == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
3028         ;
3029       else
3030         return 0;
3031     }
3032
3033   /* Try to put the result of the comparison in the sign bit.  Assume we can't
3034      do the necessary operation below.  */
3035
3036   tem = 0;
3037
3038   /* To see if A <= 0, compute (A | (A - 1)).  A <= 0 iff that result has
3039      the sign bit set.  */
3040
3041   if (code == LE)
3042     {
3043       /* This is destructive, so SUBTARGET can't be OP0.  */
3044       if (rtx_equal_p (subtarget, op0))
3045         subtarget = 0;
3046
3047       tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
3048                           OPTAB_WIDEN);
3049       if (tem)
3050         tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
3051                             OPTAB_WIDEN);
3052     }
3053
3054   /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
3055      number of bits in the mode of OP0, minus one.  */
3056
3057   if (code == GT)
3058     {
3059       if (rtx_equal_p (subtarget, op0))
3060         subtarget = 0;
3061
3062       tem = expand_shift (RSHIFT_EXPR, mode, op0,
3063                           size_int (GET_MODE_BITSIZE (mode) - 1),
3064                           subtarget, 0);
3065       tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
3066                           OPTAB_WIDEN);
3067     }
3068                                     
3069   if (code == EQ || code == NE)
3070     {
3071       /* For EQ or NE, one way to do the comparison is to apply an operation
3072          that converts the operand into a positive number if it is non-zero
3073          or zero if it was originally zero.  Then, for EQ, we subtract 1 and
3074          for NE we negate.  This puts the result in the sign bit.  Then we
3075          normalize with a shift, if needed. 
3076
3077          Two operations that can do the above actions are ABS and FFS, so try
3078          them.  If that doesn't work, and MODE is smaller than a full word,
3079          we can use zero-extension to the wider mode (an unsigned conversion)
3080          as the operation.  */
3081
3082       if (abs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3083         tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
3084       else if (ffs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3085         tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
3086       else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3087         {
3088           mode = word_mode;
3089           op0 = protect_from_queue (op0, 0);
3090           tem = convert_to_mode (mode, op0, 1);
3091         }
3092
3093       if (tem != 0)
3094         {
3095           if (code == EQ)
3096             tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
3097                                 0, OPTAB_WIDEN);
3098           else
3099             tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
3100         }
3101
3102       /* If we couldn't do it that way, for NE we can "or" the two's complement
3103          of the value with itself.  For EQ, we take the one's complement of
3104          that "or", which is an extra insn, so we only handle EQ if branches
3105          are expensive.  */
3106
3107       if (tem == 0 && (code == NE || BRANCH_COST > 1))
3108         {
3109           if (rtx_equal_p (subtarget, op0))
3110             subtarget = 0;
3111
3112           tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
3113           tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
3114                               OPTAB_WIDEN);
3115
3116           if (tem && code == EQ)
3117             tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
3118         }
3119     }
3120
3121   if (tem && normalizep)
3122     tem = expand_shift (RSHIFT_EXPR, mode, tem,
3123                         size_int (GET_MODE_BITSIZE (mode) - 1),
3124                         tem, normalizep == 1);
3125
3126   if (tem && GET_MODE (tem) != target_mode)
3127     {
3128       convert_move (target, tem, 0);
3129       tem = target;
3130     }
3131
3132   if (tem == 0)
3133     delete_insns_since (last);
3134
3135   return tem;
3136 }
3137   emit_jump_insn ((*bcc_gen_fctn[(int) code]) (label));
3138   emit_move_insn (target, const1_rtx);
3139   emit_label (label);
3140
3141   return target;
3142 }