OSDN Git Service

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