OSDN Git Service

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