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