OSDN Git Service

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