OSDN Git Service

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