OSDN Git Service

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