OSDN Git Service

a108efaa4eea3650231bf27c2f6d155d6adf2bd5
[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       base = force_operand (base, NULL);
1895       bitpos %= BITS_PER_UNIT;
1896
1897       /* Force alignment of the address; load two sequential values.  */
1898       a1 = expand_simple_binop (Pmode, AND, base,
1899                                 GEN_INT (-(HOST_WIDE_INT)m_size),
1900                                 NULL, true, OPTAB_LIB_WIDEN);
1901       mark_reg_pointer (a1, m_bitsize);
1902       v1 = gen_rtx_MEM (mode, a1);
1903       set_mem_align (v1, m_bitsize);
1904       v1 = force_reg (mode, validize_mem (v1));
1905
1906       a2 = plus_constant (a1, GET_MODE_SIZE (mode));
1907       v2 = gen_rtx_MEM (mode, a2);
1908       set_mem_align (v2, m_bitsize);
1909       v2 = force_reg (mode, validize_mem (v2));
1910
1911       /* Combine these two values into a double-word value.  */
1912       if (m_bitsize == BITS_PER_WORD)
1913         {
1914           comb = gen_reg_rtx (dmode);
1915           emit_insn (gen_rtx_CLOBBER (VOIDmode, comb));
1916           emit_move_insn (gen_rtx_SUBREG (mode, comb, 0), v1);
1917           emit_move_insn (gen_rtx_SUBREG (mode, comb, m_size), v2);
1918         }
1919       else
1920         {
1921           if (BYTES_BIG_ENDIAN)
1922             comb = v1, v1 = v2, v2 = comb;
1923           v1 = convert_modes (dmode, mode, v1, true);
1924           if (v1 == NULL)
1925             goto fail;
1926           v2 = convert_modes (dmode, mode, v2, true);
1927           v2 = expand_simple_binop (dmode, ASHIFT, v2, GEN_INT (m_bitsize),
1928                                     NULL, true, OPTAB_LIB_WIDEN);
1929           if (v2 == NULL)
1930             goto fail;
1931           comb = expand_simple_binop (dmode, IOR, v1, v2, NULL,
1932                                       true, OPTAB_LIB_WIDEN);
1933           if (comb == NULL)
1934             goto fail;
1935         }
1936
1937       shift = expand_simple_binop (Pmode, AND, base, GEN_INT (m_size - 1),
1938                                    NULL, true, OPTAB_LIB_WIDEN);
1939       shift = expand_mult (Pmode, shift, GEN_INT (BITS_PER_UNIT), NULL, 1);
1940
1941       if (bitpos != 0)
1942         {
1943           if (sign_shift_up <= bitpos)
1944             bitpos -= sign_shift_up, sign_shift_up = 0;
1945           shift = expand_simple_binop (Pmode, PLUS, shift, GEN_INT (bitpos),
1946                                        NULL, true, OPTAB_LIB_WIDEN);
1947         }
1948     }
1949   else
1950     {
1951       unsigned HOST_WIDE_INT offset = bitpos / BITS_PER_UNIT;
1952       bitpos %= BITS_PER_UNIT;
1953
1954       /* When strict alignment is not required, we can just load directly
1955          from memory without masking.  If the remaining BITPOS offset is
1956          small enough, we may be able to do all operations in MODE as 
1957          opposed to DMODE.  */
1958       if (bitpos + bitsize <= m_bitsize)
1959         dmode = mode;
1960       comb = adjust_address (op0, dmode, offset);
1961
1962       if (sign_shift_up <= bitpos)
1963         bitpos -= sign_shift_up, sign_shift_up = 0;
1964       shift = GEN_INT (bitpos);
1965     }
1966
1967   /* Shift down the double-word such that the requested value is at bit 0.  */
1968   if (shift != const0_rtx)
1969     comb = expand_simple_binop (dmode, unsignedp ? LSHIFTRT : ASHIFTRT,
1970                                 comb, shift, NULL, unsignedp, OPTAB_LIB_WIDEN);
1971   if (comb == NULL)
1972     goto fail;
1973
1974   /* If the field exactly matches MODE, then all we need to do is return the
1975      lowpart.  Otherwise, shift to get the sign bits set properly.  */
1976   result = force_reg (mode, gen_lowpart (mode, comb));
1977
1978   if (sign_shift_up)
1979     result = expand_simple_binop (mode, ASHIFT, result,
1980                                   GEN_INT (sign_shift_up),
1981                                   NULL_RTX, 0, OPTAB_LIB_WIDEN);
1982   if (sign_shift_dn)
1983     result = expand_simple_binop (mode, unsignedp ? LSHIFTRT : ASHIFTRT,
1984                                   result, GEN_INT (sign_shift_dn),
1985                                   NULL_RTX, 0, OPTAB_LIB_WIDEN);
1986
1987   return result;
1988
1989  fail:
1990   delete_insns_since (start);
1991   return NULL;
1992 }
1993
1994 /* Extract a bit field that is split across two words
1995    and return an RTX for the result.
1996
1997    OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1998    BITSIZE is the field width; BITPOS, position of its first bit, in the word.
1999    UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.  */
2000
2001 static rtx
2002 extract_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
2003                          unsigned HOST_WIDE_INT bitpos, int unsignedp)
2004 {
2005   unsigned int unit;
2006   unsigned int bitsdone = 0;
2007   rtx result = NULL_RTX;
2008   int first = 1;
2009
2010   /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
2011      much at a time.  */
2012   if (REG_P (op0) || GET_CODE (op0) == SUBREG)
2013     unit = BITS_PER_WORD;
2014   else
2015     {
2016       unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
2017       if (bitsize / unit > 2)
2018         {
2019           rtx tmp = extract_force_align_mem_bit_field (op0, bitsize, bitpos,
2020                                                        unsignedp);
2021           if (tmp)
2022             return tmp;
2023         }
2024     }
2025
2026   while (bitsdone < bitsize)
2027     {
2028       unsigned HOST_WIDE_INT thissize;
2029       rtx part, word;
2030       unsigned HOST_WIDE_INT thispos;
2031       unsigned HOST_WIDE_INT offset;
2032
2033       offset = (bitpos + bitsdone) / unit;
2034       thispos = (bitpos + bitsdone) % unit;
2035
2036       /* THISSIZE must not overrun a word boundary.  Otherwise,
2037          extract_fixed_bit_field will call us again, and we will mutually
2038          recurse forever.  */
2039       thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
2040       thissize = MIN (thissize, unit - thispos);
2041
2042       /* If OP0 is a register, then handle OFFSET here.
2043
2044          When handling multiword bitfields, extract_bit_field may pass
2045          down a word_mode SUBREG of a larger REG for a bitfield that actually
2046          crosses a word boundary.  Thus, for a SUBREG, we must find
2047          the current word starting from the base register.  */
2048       if (GET_CODE (op0) == SUBREG)
2049         {
2050           int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
2051           word = operand_subword_force (SUBREG_REG (op0), word_offset,
2052                                         GET_MODE (SUBREG_REG (op0)));
2053           offset = 0;
2054         }
2055       else if (REG_P (op0))
2056         {
2057           word = operand_subword_force (op0, offset, GET_MODE (op0));
2058           offset = 0;
2059         }
2060       else
2061         word = op0;
2062
2063       /* Extract the parts in bit-counting order,
2064          whose meaning is determined by BYTES_PER_UNIT.
2065          OFFSET is in UNITs, and UNIT is in bits.
2066          extract_fixed_bit_field wants offset in bytes.  */
2067       part = extract_fixed_bit_field (word_mode, word,
2068                                       offset * unit / BITS_PER_UNIT,
2069                                       thissize, thispos, 0, 1);
2070       bitsdone += thissize;
2071
2072       /* Shift this part into place for the result.  */
2073       if (BYTES_BIG_ENDIAN)
2074         {
2075           if (bitsize != bitsdone)
2076             part = expand_shift (LSHIFT_EXPR, word_mode, part,
2077                                  build_int_cst (NULL_TREE, bitsize - bitsdone),
2078                                  0, 1);
2079         }
2080       else
2081         {
2082           if (bitsdone != thissize)
2083             part = expand_shift (LSHIFT_EXPR, word_mode, part,
2084                                  build_int_cst (NULL_TREE,
2085                                                 bitsdone - thissize), 0, 1);
2086         }
2087
2088       if (first)
2089         result = part;
2090       else
2091         /* Combine the parts with bitwise or.  This works
2092            because we extracted each part as an unsigned bit field.  */
2093         result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
2094                                OPTAB_LIB_WIDEN);
2095
2096       first = 0;
2097     }
2098
2099   /* Unsigned bit field: we are done.  */
2100   if (unsignedp)
2101     return result;
2102   /* Signed bit field: sign-extend with two arithmetic shifts.  */
2103   result = expand_shift (LSHIFT_EXPR, word_mode, result,
2104                          build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
2105                          NULL_RTX, 0);
2106   return expand_shift (RSHIFT_EXPR, word_mode, result,
2107                        build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
2108                        NULL_RTX, 0);
2109 }
2110 \f
2111 /* Add INC into TARGET.  */
2112
2113 void
2114 expand_inc (rtx target, rtx inc)
2115 {
2116   rtx value = expand_binop (GET_MODE (target), add_optab,
2117                             target, inc,
2118                             target, 0, OPTAB_LIB_WIDEN);
2119   if (value != target)
2120     emit_move_insn (target, value);
2121 }
2122
2123 /* Subtract DEC from TARGET.  */
2124
2125 void
2126 expand_dec (rtx target, rtx dec)
2127 {
2128   rtx value = expand_binop (GET_MODE (target), sub_optab,
2129                             target, dec,
2130                             target, 0, OPTAB_LIB_WIDEN);
2131   if (value != target)
2132     emit_move_insn (target, value);
2133 }
2134 \f
2135 /* Output a shift instruction for expression code CODE,
2136    with SHIFTED being the rtx for the value to shift,
2137    and AMOUNT the tree for the amount to shift by.
2138    Store the result in the rtx TARGET, if that is convenient.
2139    If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2140    Return the rtx for where the value is.  */
2141
2142 rtx
2143 expand_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2144               tree amount, rtx target, int unsignedp)
2145 {
2146   rtx op1, temp = 0;
2147   int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
2148   int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2149   int try;
2150
2151   /* Previously detected shift-counts computed by NEGATE_EXPR
2152      and shifted in the other direction; but that does not work
2153      on all machines.  */
2154
2155   op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
2156
2157   if (SHIFT_COUNT_TRUNCATED)
2158     {
2159       if (GET_CODE (op1) == CONST_INT
2160           && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2161               (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode)))
2162         op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2163                        % GET_MODE_BITSIZE (mode));
2164       else if (GET_CODE (op1) == SUBREG
2165                && subreg_lowpart_p (op1))
2166         op1 = SUBREG_REG (op1);
2167     }
2168
2169   if (op1 == const0_rtx)
2170     return shifted;
2171
2172   /* Check whether its cheaper to implement a left shift by a constant
2173      bit count by a sequence of additions.  */
2174   if (code == LSHIFT_EXPR
2175       && GET_CODE (op1) == CONST_INT
2176       && INTVAL (op1) > 0
2177       && INTVAL (op1) < GET_MODE_BITSIZE (mode)
2178       && shift_cost[mode][INTVAL (op1)] > INTVAL (op1) * add_cost[mode])
2179     {
2180       int i;
2181       for (i = 0; i < INTVAL (op1); i++)
2182         {
2183           temp = force_reg (mode, shifted);
2184           shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2185                                   unsignedp, OPTAB_LIB_WIDEN);
2186         }
2187       return shifted;
2188     }
2189
2190   for (try = 0; temp == 0 && try < 3; try++)
2191     {
2192       enum optab_methods methods;
2193
2194       if (try == 0)
2195         methods = OPTAB_DIRECT;
2196       else if (try == 1)
2197         methods = OPTAB_WIDEN;
2198       else
2199         methods = OPTAB_LIB_WIDEN;
2200
2201       if (rotate)
2202         {
2203           /* Widening does not work for rotation.  */
2204           if (methods == OPTAB_WIDEN)
2205             continue;
2206           else if (methods == OPTAB_LIB_WIDEN)
2207             {
2208               /* If we have been unable to open-code this by a rotation,
2209                  do it as the IOR of two shifts.  I.e., to rotate A
2210                  by N bits, compute (A << N) | ((unsigned) A >> (C - N))
2211                  where C is the bitsize of A.
2212
2213                  It is theoretically possible that the target machine might
2214                  not be able to perform either shift and hence we would
2215                  be making two libcalls rather than just the one for the
2216                  shift (similarly if IOR could not be done).  We will allow
2217                  this extremely unlikely lossage to avoid complicating the
2218                  code below.  */
2219
2220               rtx subtarget = target == shifted ? 0 : target;
2221               rtx temp1;
2222               tree type = TREE_TYPE (amount);
2223               tree new_amount = make_tree (type, op1);
2224               tree other_amount
2225                 = fold (build2 (MINUS_EXPR, type, convert
2226                                 (type, build_int_cst
2227                                  (NULL_TREE, GET_MODE_BITSIZE (mode))),
2228                                 amount));
2229
2230               shifted = force_reg (mode, shifted);
2231
2232               temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2233                                    mode, shifted, new_amount, subtarget, 1);
2234               temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2235                                     mode, shifted, other_amount, 0, 1);
2236               return expand_binop (mode, ior_optab, temp, temp1, target,
2237                                    unsignedp, methods);
2238             }
2239
2240           temp = expand_binop (mode,
2241                                left ? rotl_optab : rotr_optab,
2242                                shifted, op1, target, unsignedp, methods);
2243
2244           /* If we don't have the rotate, but we are rotating by a constant
2245              that is in range, try a rotate in the opposite direction.  */
2246
2247           if (temp == 0 && GET_CODE (op1) == CONST_INT
2248               && INTVAL (op1) > 0
2249               && (unsigned int) INTVAL (op1) < GET_MODE_BITSIZE (mode))
2250             temp = expand_binop (mode,
2251                                  left ? rotr_optab : rotl_optab,
2252                                  shifted,
2253                                  GEN_INT (GET_MODE_BITSIZE (mode)
2254                                           - INTVAL (op1)),
2255                                  target, unsignedp, methods);
2256         }
2257       else if (unsignedp)
2258         temp = expand_binop (mode,
2259                              left ? ashl_optab : lshr_optab,
2260                              shifted, op1, target, unsignedp, methods);
2261
2262       /* Do arithmetic shifts.
2263          Also, if we are going to widen the operand, we can just as well
2264          use an arithmetic right-shift instead of a logical one.  */
2265       if (temp == 0 && ! rotate
2266           && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2267         {
2268           enum optab_methods methods1 = methods;
2269
2270           /* If trying to widen a log shift to an arithmetic shift,
2271              don't accept an arithmetic shift of the same size.  */
2272           if (unsignedp)
2273             methods1 = OPTAB_MUST_WIDEN;
2274
2275           /* Arithmetic shift */
2276
2277           temp = expand_binop (mode,
2278                                left ? ashl_optab : ashr_optab,
2279                                shifted, op1, target, unsignedp, methods1);
2280         }
2281
2282       /* We used to try extzv here for logical right shifts, but that was
2283          only useful for one machine, the VAX, and caused poor code
2284          generation there for lshrdi3, so the code was deleted and a
2285          define_expand for lshrsi3 was added to vax.md.  */
2286     }
2287
2288   gcc_assert (temp);
2289   return temp;
2290 }
2291 \f
2292 enum alg_code { alg_zero, alg_m, alg_shift,
2293                   alg_add_t_m2, alg_sub_t_m2,
2294                   alg_add_factor, alg_sub_factor,
2295                   alg_add_t2_m, alg_sub_t2_m };
2296
2297 /* This structure holds the "cost" of a multiply sequence.  The
2298    "cost" field holds the total rtx_cost of every operator in the
2299    synthetic multiplication sequence, hence cost(a op b) is defined
2300    as rtx_cost(op) + cost(a) + cost(b), where cost(leaf) is zero.
2301    The "latency" field holds the minimum possible latency of the
2302    synthetic multiply, on a hypothetical infinitely parallel CPU.
2303    This is the critical path, or the maximum height, of the expression
2304    tree which is the sum of rtx_costs on the most expensive path from
2305    any leaf to the root.  Hence latency(a op b) is defined as zero for
2306    leaves and rtx_cost(op) + max(latency(a), latency(b)) otherwise.  */
2307
2308 struct mult_cost {
2309   short cost;     /* Total rtx_cost of the multiplication sequence.  */
2310   short latency;  /* The latency of the multiplication sequence.  */
2311 };
2312
2313 /* This macro is used to compare a pointer to a mult_cost against an
2314    single integer "rtx_cost" value.  This is equivalent to the macro
2315    CHEAPER_MULT_COST(X,Z) where Z = {Y,Y}.  */
2316 #define MULT_COST_LESS(X,Y) ((X)->cost < (Y)    \
2317                              || ((X)->cost == (Y) && (X)->latency < (Y)))
2318
2319 /* This macro is used to compare two pointers to mult_costs against
2320    each other.  The macro returns true if X is cheaper than Y.
2321    Currently, the cheaper of two mult_costs is the one with the
2322    lower "cost".  If "cost"s are tied, the lower latency is cheaper.  */
2323 #define CHEAPER_MULT_COST(X,Y)  ((X)->cost < (Y)->cost          \
2324                                  || ((X)->cost == (Y)->cost     \
2325                                      && (X)->latency < (Y)->latency))
2326
2327 /* This structure records a sequence of operations.
2328    `ops' is the number of operations recorded.
2329    `cost' is their total cost.
2330    The operations are stored in `op' and the corresponding
2331    logarithms of the integer coefficients in `log'.
2332
2333    These are the operations:
2334    alg_zero             total := 0;
2335    alg_m                total := multiplicand;
2336    alg_shift            total := total * coeff
2337    alg_add_t_m2         total := total + multiplicand * coeff;
2338    alg_sub_t_m2         total := total - multiplicand * coeff;
2339    alg_add_factor       total := total * coeff + total;
2340    alg_sub_factor       total := total * coeff - total;
2341    alg_add_t2_m         total := total * coeff + multiplicand;
2342    alg_sub_t2_m         total := total * coeff - multiplicand;
2343
2344    The first operand must be either alg_zero or alg_m.  */
2345
2346 struct algorithm
2347 {
2348   struct mult_cost cost;
2349   short ops;
2350   /* The size of the OP and LOG fields are not directly related to the
2351      word size, but the worst-case algorithms will be if we have few
2352      consecutive ones or zeros, i.e., a multiplicand like 10101010101...
2353      In that case we will generate shift-by-2, add, shift-by-2, add,...,
2354      in total wordsize operations.  */
2355   enum alg_code op[MAX_BITS_PER_WORD];
2356   char log[MAX_BITS_PER_WORD];
2357 };
2358
2359 /* Indicates the type of fixup needed after a constant multiplication.
2360    BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2361    the result should be negated, and ADD_VARIANT means that the
2362    multiplicand should be added to the result.  */
2363 enum mult_variant {basic_variant, negate_variant, add_variant};
2364
2365 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2366                         const struct mult_cost *, enum machine_mode mode);
2367 static bool choose_mult_variant (enum machine_mode, HOST_WIDE_INT,
2368                                  struct algorithm *, enum mult_variant *, int);
2369 static rtx expand_mult_const (enum machine_mode, rtx, HOST_WIDE_INT, rtx,
2370                               const struct algorithm *, enum mult_variant);
2371 static unsigned HOST_WIDE_INT choose_multiplier (unsigned HOST_WIDE_INT, int,
2372                                                  int, unsigned HOST_WIDE_INT *,
2373                                                  int *, int *);
2374 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2375 static rtx extract_high_half (enum machine_mode, rtx);
2376 static rtx expand_mult_highpart_optab (enum machine_mode, rtx, rtx, rtx,
2377                                        int, int);
2378 /* Compute and return the best algorithm for multiplying by T.
2379    The algorithm must cost less than cost_limit
2380    If retval.cost >= COST_LIMIT, no algorithm was found and all
2381    other field of the returned struct are undefined.
2382    MODE is the machine mode of the multiplication.  */
2383
2384 static void
2385 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2386             const struct mult_cost *cost_limit, enum machine_mode mode)
2387 {
2388   int m;
2389   struct algorithm *alg_in, *best_alg;
2390   struct mult_cost best_cost;
2391   struct mult_cost new_limit;
2392   int op_cost, op_latency;
2393   unsigned HOST_WIDE_INT q;
2394   int maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (mode));
2395
2396   /* Indicate that no algorithm is yet found.  If no algorithm
2397      is found, this value will be returned and indicate failure.  */
2398   alg_out->cost.cost = cost_limit->cost + 1;
2399   alg_out->cost.latency = cost_limit->latency + 1;
2400
2401   if (cost_limit->cost < 0
2402       || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2403     return;
2404
2405   /* Restrict the bits of "t" to the multiplication's mode.  */
2406   t &= GET_MODE_MASK (mode);
2407
2408   /* t == 1 can be done in zero cost.  */
2409   if (t == 1)
2410     {
2411       alg_out->ops = 1;
2412       alg_out->cost.cost = 0;
2413       alg_out->cost.latency = 0;
2414       alg_out->op[0] = alg_m;
2415       return;
2416     }
2417
2418   /* t == 0 sometimes has a cost.  If it does and it exceeds our limit,
2419      fail now.  */
2420   if (t == 0)
2421     {
2422       if (MULT_COST_LESS (cost_limit, zero_cost))
2423         return;
2424       else
2425         {
2426           alg_out->ops = 1;
2427           alg_out->cost.cost = zero_cost;
2428           alg_out->cost.latency = zero_cost;
2429           alg_out->op[0] = alg_zero;
2430           return;
2431         }
2432     }
2433
2434   /* We'll be needing a couple extra algorithm structures now.  */
2435
2436   alg_in = alloca (sizeof (struct algorithm));
2437   best_alg = alloca (sizeof (struct algorithm));
2438   best_cost = *cost_limit;
2439
2440   /* If we have a group of zero bits at the low-order part of T, try
2441      multiplying by the remaining bits and then doing a shift.  */
2442
2443   if ((t & 1) == 0)
2444     {
2445       m = floor_log2 (t & -t);  /* m = number of low zero bits */
2446       if (m < maxm)
2447         {
2448           q = t >> m;
2449           /* The function expand_shift will choose between a shift and
2450              a sequence of additions, so the observed cost is given as
2451              MIN (m * add_cost[mode], shift_cost[mode][m]).  */
2452           op_cost = m * add_cost[mode];
2453           if (shift_cost[mode][m] < op_cost)
2454             op_cost = shift_cost[mode][m];
2455           new_limit.cost = best_cost.cost - op_cost;
2456           new_limit.latency = best_cost.latency - op_cost;
2457           synth_mult (alg_in, q, &new_limit, mode);
2458
2459           alg_in->cost.cost += op_cost;
2460           alg_in->cost.latency += op_cost;
2461           if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2462             {
2463               struct algorithm *x;
2464               best_cost = alg_in->cost;
2465               x = alg_in, alg_in = best_alg, best_alg = x;
2466               best_alg->log[best_alg->ops] = m;
2467               best_alg->op[best_alg->ops] = alg_shift;
2468             }
2469         }
2470     }
2471
2472   /* If we have an odd number, add or subtract one.  */
2473   if ((t & 1) != 0)
2474     {
2475       unsigned HOST_WIDE_INT w;
2476
2477       for (w = 1; (w & t) != 0; w <<= 1)
2478         ;
2479       /* If T was -1, then W will be zero after the loop.  This is another
2480          case where T ends with ...111.  Handling this with (T + 1) and
2481          subtract 1 produces slightly better code and results in algorithm
2482          selection much faster than treating it like the ...0111 case
2483          below.  */
2484       if (w == 0
2485           || (w > 2
2486               /* Reject the case where t is 3.
2487                  Thus we prefer addition in that case.  */
2488               && t != 3))
2489         {
2490           /* T ends with ...111.  Multiply by (T + 1) and subtract 1.  */
2491
2492           op_cost = add_cost[mode];
2493           new_limit.cost = best_cost.cost - op_cost;
2494           new_limit.latency = best_cost.latency - op_cost;
2495           synth_mult (alg_in, t + 1, &new_limit, mode);
2496
2497           alg_in->cost.cost += op_cost;
2498           alg_in->cost.latency += op_cost;
2499           if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2500             {
2501               struct algorithm *x;
2502               best_cost = alg_in->cost;
2503               x = alg_in, alg_in = best_alg, best_alg = x;
2504               best_alg->log[best_alg->ops] = 0;
2505               best_alg->op[best_alg->ops] = alg_sub_t_m2;
2506             }
2507         }
2508       else
2509         {
2510           /* T ends with ...01 or ...011.  Multiply by (T - 1) and add 1.  */
2511
2512           op_cost = add_cost[mode];
2513           new_limit.cost = best_cost.cost - op_cost;
2514           new_limit.latency = best_cost.latency - op_cost;
2515           synth_mult (alg_in, t - 1, &new_limit, mode);
2516
2517           alg_in->cost.cost += op_cost;
2518           alg_in->cost.latency += op_cost;
2519           if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2520             {
2521               struct algorithm *x;
2522               best_cost = alg_in->cost;
2523               x = alg_in, alg_in = best_alg, best_alg = x;
2524               best_alg->log[best_alg->ops] = 0;
2525               best_alg->op[best_alg->ops] = alg_add_t_m2;
2526             }
2527         }
2528     }
2529
2530   /* Look for factors of t of the form
2531      t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2532      If we find such a factor, we can multiply by t using an algorithm that
2533      multiplies by q, shift the result by m and add/subtract it to itself.
2534
2535      We search for large factors first and loop down, even if large factors
2536      are less probable than small; if we find a large factor we will find a
2537      good sequence quickly, and therefore be able to prune (by decreasing
2538      COST_LIMIT) the search.  */
2539
2540   for (m = floor_log2 (t - 1); m >= 2; m--)
2541     {
2542       unsigned HOST_WIDE_INT d;
2543
2544       d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2545       if (t % d == 0 && t > d && m < maxm)
2546         {
2547           /* If the target has a cheap shift-and-add instruction use
2548              that in preference to a shift insn followed by an add insn.
2549              Assume that the shift-and-add is "atomic" with a latency
2550              equal to it's cost, otherwise assume that on superscalar
2551              hardware the shift may be executed concurrently with the
2552              earlier steps in the algorithm.  */
2553           op_cost = add_cost[mode] + shift_cost[mode][m];
2554           if (shiftadd_cost[mode][m] < op_cost)
2555             {
2556               op_cost = shiftadd_cost[mode][m];
2557               op_latency = op_cost;
2558             }
2559           else
2560             op_latency = add_cost[mode];
2561
2562           new_limit.cost = best_cost.cost - op_cost;
2563           new_limit.latency = best_cost.latency - op_latency;
2564           synth_mult (alg_in, t / d, &new_limit, mode);
2565
2566           alg_in->cost.cost += op_cost;
2567           alg_in->cost.latency += op_latency;
2568           if (alg_in->cost.latency < op_cost)
2569             alg_in->cost.latency = op_cost;
2570           if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2571             {
2572               struct algorithm *x;
2573               best_cost = alg_in->cost;
2574               x = alg_in, alg_in = best_alg, best_alg = x;
2575               best_alg->log[best_alg->ops] = m;
2576               best_alg->op[best_alg->ops] = alg_add_factor;
2577             }
2578           /* Other factors will have been taken care of in the recursion.  */
2579           break;
2580         }
2581
2582       d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2583       if (t % d == 0 && t > d && m < maxm)
2584         {
2585           /* If the target has a cheap shift-and-subtract insn use
2586              that in preference to a shift insn followed by a sub insn.
2587              Assume that the shift-and-sub is "atomic" with a latency
2588              equal to it's cost, otherwise assume that on superscalar
2589              hardware the shift may be executed concurrently with the
2590              earlier steps in the algorithm.  */
2591           op_cost = add_cost[mode] + shift_cost[mode][m];
2592           if (shiftsub_cost[mode][m] < op_cost)
2593             {
2594               op_cost = shiftsub_cost[mode][m];
2595               op_latency = op_cost;
2596             }
2597           else
2598             op_latency = add_cost[mode];
2599
2600           new_limit.cost = best_cost.cost - op_cost;
2601           new_limit.cost = best_cost.cost - op_latency;
2602           synth_mult (alg_in, t / d, &new_limit, mode);
2603
2604           alg_in->cost.cost += op_cost;
2605           alg_in->cost.latency += op_latency;
2606           if (alg_in->cost.latency < op_cost)
2607             alg_in->cost.latency = op_cost;
2608           if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2609             {
2610               struct algorithm *x;
2611               best_cost = alg_in->cost;
2612               x = alg_in, alg_in = best_alg, best_alg = x;
2613               best_alg->log[best_alg->ops] = m;
2614               best_alg->op[best_alg->ops] = alg_sub_factor;
2615             }
2616           break;
2617         }
2618     }
2619
2620   /* Try shift-and-add (load effective address) instructions,
2621      i.e. do a*3, a*5, a*9.  */
2622   if ((t & 1) != 0)
2623     {
2624       q = t - 1;
2625       q = q & -q;
2626       m = exact_log2 (q);
2627       if (m >= 0 && m < maxm)
2628         {
2629           op_cost = shiftadd_cost[mode][m];
2630           new_limit.cost = best_cost.cost - op_cost;
2631           new_limit.latency = best_cost.latency - op_cost;
2632           synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2633
2634           alg_in->cost.cost += op_cost;
2635           alg_in->cost.latency += op_cost;
2636           if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2637             {
2638               struct algorithm *x;
2639               best_cost = alg_in->cost;
2640               x = alg_in, alg_in = best_alg, best_alg = x;
2641               best_alg->log[best_alg->ops] = m;
2642               best_alg->op[best_alg->ops] = alg_add_t2_m;
2643             }
2644         }
2645
2646       q = t + 1;
2647       q = q & -q;
2648       m = exact_log2 (q);
2649       if (m >= 0 && m < maxm)
2650         {
2651           op_cost = shiftsub_cost[mode][m];
2652           new_limit.cost = best_cost.cost - op_cost;
2653           new_limit.latency = best_cost.latency - op_cost;
2654           synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2655
2656           alg_in->cost.cost += op_cost;
2657           alg_in->cost.latency += op_cost;
2658           if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2659             {
2660               struct algorithm *x;
2661               best_cost = alg_in->cost;
2662               x = alg_in, alg_in = best_alg, best_alg = x;
2663               best_alg->log[best_alg->ops] = m;
2664               best_alg->op[best_alg->ops] = alg_sub_t2_m;
2665             }
2666         }
2667     }
2668
2669   /* If best_cost has not decreased, we have not found any algorithm.  */
2670   if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
2671     return;
2672
2673   /* If we are getting a too long sequence for `struct algorithm'
2674      to record, make this search fail.  */
2675   if (best_alg->ops == MAX_BITS_PER_WORD)
2676     return;
2677
2678   /* Copy the algorithm from temporary space to the space at alg_out.
2679      We avoid using structure assignment because the majority of
2680      best_alg is normally undefined, and this is a critical function.  */
2681   alg_out->ops = best_alg->ops + 1;
2682   alg_out->cost = best_cost;
2683   memcpy (alg_out->op, best_alg->op,
2684           alg_out->ops * sizeof *alg_out->op);
2685   memcpy (alg_out->log, best_alg->log,
2686           alg_out->ops * sizeof *alg_out->log);
2687 }
2688 \f
2689 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2690    Try three variations:
2691
2692        - a shift/add sequence based on VAL itself
2693        - a shift/add sequence based on -VAL, followed by a negation
2694        - a shift/add sequence based on VAL - 1, followed by an addition.
2695
2696    Return true if the cheapest of these cost less than MULT_COST,
2697    describing the algorithm in *ALG and final fixup in *VARIANT.  */
2698
2699 static bool
2700 choose_mult_variant (enum machine_mode mode, HOST_WIDE_INT val,
2701                      struct algorithm *alg, enum mult_variant *variant,
2702                      int mult_cost)
2703 {
2704   struct algorithm alg2;
2705   struct mult_cost limit;
2706   int op_cost;
2707
2708   *variant = basic_variant;
2709   limit.cost = mult_cost;
2710   limit.latency = mult_cost;
2711   synth_mult (alg, val, &limit, mode);
2712
2713   /* This works only if the inverted value actually fits in an
2714      `unsigned int' */
2715   if (HOST_BITS_PER_INT >= GET_MODE_BITSIZE (mode))
2716     {
2717       op_cost = neg_cost[mode];
2718       if (MULT_COST_LESS (&alg->cost, mult_cost))
2719         {
2720           limit.cost = alg->cost.cost - op_cost;
2721           limit.latency = alg->cost.latency - op_cost;
2722         }
2723       else
2724         {
2725           limit.cost = mult_cost - op_cost;
2726           limit.latency = mult_cost - op_cost;
2727         }
2728
2729       synth_mult (&alg2, -val, &limit, mode);
2730       alg2.cost.cost += op_cost;
2731       alg2.cost.latency += op_cost;
2732       if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2733         *alg = alg2, *variant = negate_variant;
2734     }
2735
2736   /* This proves very useful for division-by-constant.  */
2737   op_cost = add_cost[mode];
2738   if (MULT_COST_LESS (&alg->cost, mult_cost))
2739     {
2740       limit.cost = alg->cost.cost - op_cost;
2741       limit.latency = alg->cost.latency - op_cost;
2742     }
2743   else
2744     {
2745       limit.cost = mult_cost - op_cost;
2746       limit.latency = mult_cost - op_cost;
2747     }
2748
2749   synth_mult (&alg2, val - 1, &limit, mode);
2750   alg2.cost.cost += op_cost;
2751   alg2.cost.latency += op_cost;
2752   if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2753     *alg = alg2, *variant = add_variant;
2754
2755   return MULT_COST_LESS (&alg->cost, mult_cost);
2756 }
2757
2758 /* A subroutine of expand_mult, used for constant multiplications.
2759    Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2760    convenient.  Use the shift/add sequence described by ALG and apply
2761    the final fixup specified by VARIANT.  */
2762
2763 static rtx
2764 expand_mult_const (enum machine_mode mode, rtx op0, HOST_WIDE_INT val,
2765                    rtx target, const struct algorithm *alg,
2766                    enum mult_variant variant)
2767 {
2768   HOST_WIDE_INT val_so_far;
2769   rtx insn, accum, tem;
2770   int opno;
2771   enum machine_mode nmode;
2772
2773   /* Avoid referencing memory over and over.
2774      For speed, but also for correctness when mem is volatile.  */
2775   if (MEM_P (op0))
2776     op0 = force_reg (mode, op0);
2777
2778   /* ACCUM starts out either as OP0 or as a zero, depending on
2779      the first operation.  */
2780
2781   if (alg->op[0] == alg_zero)
2782     {
2783       accum = copy_to_mode_reg (mode, const0_rtx);
2784       val_so_far = 0;
2785     }
2786   else if (alg->op[0] == alg_m)
2787     {
2788       accum = copy_to_mode_reg (mode, op0);
2789       val_so_far = 1;
2790     }
2791   else
2792     gcc_unreachable ();
2793
2794   for (opno = 1; opno < alg->ops; opno++)
2795     {
2796       int log = alg->log[opno];
2797       rtx shift_subtarget = optimize ? 0 : accum;
2798       rtx add_target
2799         = (opno == alg->ops - 1 && target != 0 && variant != add_variant
2800            && !optimize)
2801           ? target : 0;
2802       rtx accum_target = optimize ? 0 : accum;
2803
2804       switch (alg->op[opno])
2805         {
2806         case alg_shift:
2807           accum = expand_shift (LSHIFT_EXPR, mode, accum,
2808                                 build_int_cst (NULL_TREE, log),
2809                                 NULL_RTX, 0);
2810           val_so_far <<= log;
2811           break;
2812
2813         case alg_add_t_m2:
2814           tem = expand_shift (LSHIFT_EXPR, mode, op0,
2815                               build_int_cst (NULL_TREE, log),
2816                               NULL_RTX, 0);
2817           accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2818                                  add_target ? add_target : accum_target);
2819           val_so_far += (HOST_WIDE_INT) 1 << log;
2820           break;
2821
2822         case alg_sub_t_m2:
2823           tem = expand_shift (LSHIFT_EXPR, mode, op0,
2824                               build_int_cst (NULL_TREE, log),
2825                               NULL_RTX, 0);
2826           accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
2827                                  add_target ? add_target : accum_target);
2828           val_so_far -= (HOST_WIDE_INT) 1 << log;
2829           break;
2830
2831         case alg_add_t2_m:
2832           accum = expand_shift (LSHIFT_EXPR, mode, accum,
2833                                 build_int_cst (NULL_TREE, log),
2834                                 shift_subtarget,
2835                                 0);
2836           accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
2837                                  add_target ? add_target : accum_target);
2838           val_so_far = (val_so_far << log) + 1;
2839           break;
2840
2841         case alg_sub_t2_m:
2842           accum = expand_shift (LSHIFT_EXPR, mode, accum,
2843                                 build_int_cst (NULL_TREE, log),
2844                                 shift_subtarget, 0);
2845           accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
2846                                  add_target ? add_target : accum_target);
2847           val_so_far = (val_so_far << log) - 1;
2848           break;
2849
2850         case alg_add_factor:
2851           tem = expand_shift (LSHIFT_EXPR, mode, accum,
2852                               build_int_cst (NULL_TREE, log),
2853                               NULL_RTX, 0);
2854           accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2855                                  add_target ? add_target : accum_target);
2856           val_so_far += val_so_far << log;
2857           break;
2858
2859         case alg_sub_factor:
2860           tem = expand_shift (LSHIFT_EXPR, mode, accum,
2861                               build_int_cst (NULL_TREE, log),
2862                               NULL_RTX, 0);
2863           accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
2864                                  (add_target
2865                                   ? add_target : (optimize ? 0 : tem)));
2866           val_so_far = (val_so_far << log) - val_so_far;
2867           break;
2868
2869         default:
2870           gcc_unreachable ();
2871         }
2872
2873       /* Write a REG_EQUAL note on the last insn so that we can cse
2874          multiplication sequences.  Note that if ACCUM is a SUBREG,
2875          we've set the inner register and must properly indicate
2876          that.  */
2877
2878       tem = op0, nmode = mode;
2879       if (GET_CODE (accum) == SUBREG)
2880         {
2881           nmode = GET_MODE (SUBREG_REG (accum));
2882           tem = gen_lowpart (nmode, op0);
2883         }
2884
2885       insn = get_last_insn ();
2886       set_unique_reg_note (insn, REG_EQUAL,
2887                            gen_rtx_MULT (nmode, tem, GEN_INT (val_so_far)));
2888     }
2889
2890   if (variant == negate_variant)
2891     {
2892       val_so_far = -val_so_far;
2893       accum = expand_unop (mode, neg_optab, accum, target, 0);
2894     }
2895   else if (variant == add_variant)
2896     {
2897       val_so_far = val_so_far + 1;
2898       accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
2899     }
2900
2901   /* Compare only the bits of val and val_so_far that are significant
2902      in the result mode, to avoid sign-/zero-extension confusion.  */
2903   val &= GET_MODE_MASK (mode);
2904   val_so_far &= GET_MODE_MASK (mode);
2905   gcc_assert (val == val_so_far);
2906
2907   return accum;
2908 }
2909
2910 /* Perform a multiplication and return an rtx for the result.
2911    MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
2912    TARGET is a suggestion for where to store the result (an rtx).
2913
2914    We check specially for a constant integer as OP1.
2915    If you want this check for OP0 as well, then before calling
2916    you should swap the two operands if OP0 would be constant.  */
2917
2918 rtx
2919 expand_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
2920              int unsignedp)
2921 {
2922   rtx const_op1 = op1;
2923   enum mult_variant variant;
2924   struct algorithm algorithm;
2925
2926   /* synth_mult does an `unsigned int' multiply.  As long as the mode is
2927      less than or equal in size to `unsigned int' this doesn't matter.
2928      If the mode is larger than `unsigned int', then synth_mult works only
2929      if the constant value exactly fits in an `unsigned int' without any
2930      truncation.  This means that multiplying by negative values does
2931      not work; results are off by 2^32 on a 32 bit machine.  */
2932
2933   /* If we are multiplying in DImode, it may still be a win
2934      to try to work with shifts and adds.  */
2935   if (GET_CODE (op1) == CONST_DOUBLE
2936       && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
2937       && HOST_BITS_PER_INT >= BITS_PER_WORD
2938       && CONST_DOUBLE_HIGH (op1) == 0)
2939     const_op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2940   else if (HOST_BITS_PER_INT < GET_MODE_BITSIZE (mode)
2941            && GET_CODE (op1) == CONST_INT
2942            && INTVAL (op1) < 0)
2943     const_op1 = 0;
2944
2945   /* We used to test optimize here, on the grounds that it's better to
2946      produce a smaller program when -O is not used.
2947      But this causes such a terrible slowdown sometimes
2948      that it seems better to use synth_mult always.  */
2949
2950   if (const_op1 && GET_CODE (const_op1) == CONST_INT
2951       && (unsignedp || !flag_trapv))
2952     {
2953       int mult_cost = rtx_cost (gen_rtx_MULT (mode, op0, op1), SET);
2954
2955       if (choose_mult_variant (mode, INTVAL (const_op1), &algorithm, &variant,
2956                                mult_cost))
2957         return expand_mult_const (mode, op0, INTVAL (const_op1), target,
2958                                   &algorithm, variant);
2959     }
2960
2961   if (GET_CODE (op0) == CONST_DOUBLE)
2962     {
2963       rtx temp = op0;
2964       op0 = op1;
2965       op1 = temp;
2966     }
2967
2968   /* Expand x*2.0 as x+x.  */
2969   if (GET_CODE (op1) == CONST_DOUBLE
2970       && GET_MODE_CLASS (mode) == MODE_FLOAT)
2971     {
2972       REAL_VALUE_TYPE d;
2973       REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
2974
2975       if (REAL_VALUES_EQUAL (d, dconst2))
2976         {
2977           op0 = force_reg (GET_MODE (op0), op0);
2978           return expand_binop (mode, add_optab, op0, op0,
2979                                target, unsignedp, OPTAB_LIB_WIDEN);
2980         }
2981     }
2982
2983   /* This used to use umul_optab if unsigned, but for non-widening multiply
2984      there is no difference between signed and unsigned.  */
2985   op0 = expand_binop (mode,
2986                       ! unsignedp
2987                       && flag_trapv && (GET_MODE_CLASS(mode) == MODE_INT)
2988                       ? smulv_optab : smul_optab,
2989                       op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
2990   gcc_assert (op0);
2991   return op0;
2992 }
2993 \f
2994 /* Return the smallest n such that 2**n >= X.  */
2995
2996 int
2997 ceil_log2 (unsigned HOST_WIDE_INT x)
2998 {
2999   return floor_log2 (x - 1) + 1;
3000 }
3001
3002 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3003    replace division by D, and put the least significant N bits of the result
3004    in *MULTIPLIER_PTR and return the most significant bit.
3005
3006    The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3007    needed precision is in PRECISION (should be <= N).
3008
3009    PRECISION should be as small as possible so this function can choose
3010    multiplier more freely.
3011
3012    The rounded-up logarithm of D is placed in *lgup_ptr.  A shift count that
3013    is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3014
3015    Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3016    where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier.  */
3017
3018 static
3019 unsigned HOST_WIDE_INT
3020 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
3021                    unsigned HOST_WIDE_INT *multiplier_ptr,
3022                    int *post_shift_ptr, int *lgup_ptr)
3023 {
3024   HOST_WIDE_INT mhigh_hi, mlow_hi;
3025   unsigned HOST_WIDE_INT mhigh_lo, mlow_lo;
3026   int lgup, post_shift;
3027   int pow, pow2;
3028   unsigned HOST_WIDE_INT nl, dummy1;
3029   HOST_WIDE_INT nh, dummy2;
3030
3031   /* lgup = ceil(log2(divisor)); */
3032   lgup = ceil_log2 (d);
3033
3034   gcc_assert (lgup <= n);
3035
3036   pow = n + lgup;
3037   pow2 = n + lgup - precision;
3038
3039   /* We could handle this with some effort, but this case is much
3040      better handled directly with a scc insn, so rely on caller using
3041      that.  */
3042   gcc_assert (pow != 2 * HOST_BITS_PER_WIDE_INT);
3043
3044   /* mlow = 2^(N + lgup)/d */
3045  if (pow >= HOST_BITS_PER_WIDE_INT)
3046     {
3047       nh = (HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
3048       nl = 0;
3049     }
3050   else
3051     {
3052       nh = 0;
3053       nl = (unsigned HOST_WIDE_INT) 1 << pow;
3054     }
3055   div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
3056                         &mlow_lo, &mlow_hi, &dummy1, &dummy2);
3057
3058   /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
3059   if (pow2 >= HOST_BITS_PER_WIDE_INT)
3060     nh |= (HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
3061   else
3062     nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
3063   div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
3064                         &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
3065
3066   gcc_assert (!mhigh_hi || nh - d < d);
3067   gcc_assert (mhigh_hi <= 1 && mlow_hi <= 1);
3068   /* Assert that mlow < mhigh.  */
3069   gcc_assert (mlow_hi < mhigh_hi
3070               || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo));
3071
3072   /* If precision == N, then mlow, mhigh exceed 2^N
3073      (but they do not exceed 2^(N+1)).  */
3074
3075   /* Reduce to lowest terms.  */
3076   for (post_shift = lgup; post_shift > 0; post_shift--)
3077     {
3078       unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
3079       unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
3080       if (ml_lo >= mh_lo)
3081         break;
3082
3083       mlow_hi = 0;
3084       mlow_lo = ml_lo;
3085       mhigh_hi = 0;
3086       mhigh_lo = mh_lo;
3087     }
3088
3089   *post_shift_ptr = post_shift;
3090   *lgup_ptr = lgup;
3091   if (n < HOST_BITS_PER_WIDE_INT)
3092     {
3093       unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
3094       *multiplier_ptr = mhigh_lo & mask;
3095       return mhigh_lo >= mask;
3096     }
3097   else
3098     {
3099       *multiplier_ptr = mhigh_lo;
3100       return mhigh_hi;
3101     }
3102 }
3103
3104 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3105    congruent to 1 (mod 2**N).  */
3106
3107 static unsigned HOST_WIDE_INT
3108 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
3109 {
3110   /* Solve x*y == 1 (mod 2^n), where x is odd.  Return y.  */
3111
3112   /* The algorithm notes that the choice y = x satisfies
3113      x*y == 1 mod 2^3, since x is assumed odd.
3114      Each iteration doubles the number of bits of significance in y.  */
3115
3116   unsigned HOST_WIDE_INT mask;
3117   unsigned HOST_WIDE_INT y = x;
3118   int nbit = 3;
3119
3120   mask = (n == HOST_BITS_PER_WIDE_INT
3121           ? ~(unsigned HOST_WIDE_INT) 0
3122           : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
3123
3124   while (nbit < n)
3125     {
3126       y = y * (2 - x*y) & mask;         /* Modulo 2^N */
3127       nbit *= 2;
3128     }
3129   return y;
3130 }
3131
3132 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3133    flavor of OP0 and OP1.  ADJ_OPERAND is already the high half of the
3134    product OP0 x OP1.  If UNSIGNEDP is nonzero, adjust the signed product
3135    to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3136    become signed.
3137
3138    The result is put in TARGET if that is convenient.
3139
3140    MODE is the mode of operation.  */
3141
3142 rtx
3143 expand_mult_highpart_adjust (enum machine_mode mode, rtx adj_operand, rtx op0,
3144                              rtx op1, rtx target, int unsignedp)
3145 {
3146   rtx tem;
3147   enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3148
3149   tem = expand_shift (RSHIFT_EXPR, mode, op0,
3150                       build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3151                       NULL_RTX, 0);
3152   tem = expand_and (mode, tem, op1, NULL_RTX);
3153   adj_operand
3154     = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3155                      adj_operand);
3156
3157   tem = expand_shift (RSHIFT_EXPR, mode, op1,
3158                       build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3159                       NULL_RTX, 0);
3160   tem = expand_and (mode, tem, op0, NULL_RTX);
3161   target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3162                           target);
3163
3164   return target;
3165 }
3166
3167 /* Subroutine of expand_mult_highpart.  Return the MODE high part of OP.  */
3168
3169 static rtx
3170 extract_high_half (enum machine_mode mode, rtx op)
3171 {
3172   enum machine_mode wider_mode;
3173
3174   if (mode == word_mode)
3175     return gen_highpart (mode, op);
3176
3177   wider_mode = GET_MODE_WIDER_MODE (mode);
3178   op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3179                      build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode)), 0, 1);
3180   return convert_modes (mode, wider_mode, op, 0);
3181 }
3182
3183 /* Like expand_mult_highpart, but only consider using a multiplication
3184    optab.  OP1 is an rtx for the constant operand.  */
3185
3186 static rtx
3187 expand_mult_highpart_optab (enum machine_mode mode, rtx op0, rtx op1,
3188                             rtx target, int unsignedp, int max_cost)
3189 {
3190   rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3191   enum machine_mode wider_mode;
3192   optab moptab;
3193   rtx tem;
3194   int size;
3195
3196   wider_mode = GET_MODE_WIDER_MODE (mode);
3197   size = GET_MODE_BITSIZE (mode);
3198
3199   /* Firstly, try using a multiplication insn that only generates the needed
3200      high part of the product, and in the sign flavor of unsignedp.  */
3201   if (mul_highpart_cost[mode] < max_cost)
3202     {
3203       moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3204       tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3205                           unsignedp, OPTAB_DIRECT);
3206       if (tem)
3207         return tem;
3208     }
3209
3210   /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3211      Need to adjust the result after the multiplication.  */
3212   if (size - 1 < BITS_PER_WORD
3213       && (mul_highpart_cost[mode] + 2 * shift_cost[mode][size-1]
3214           + 4 * add_cost[mode] < max_cost))
3215     {
3216       moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3217       tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3218                           unsignedp, OPTAB_DIRECT);
3219       if (tem)
3220         /* We used the wrong signedness.  Adjust the result.  */
3221         return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3222                                             tem, unsignedp);
3223     }
3224
3225   /* Try widening multiplication.  */
3226   moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3227   if (moptab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3228       && mul_widen_cost[wider_mode] < max_cost)
3229     {
3230       tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3231                           unsignedp, OPTAB_WIDEN);
3232       if (tem)
3233         return extract_high_half (mode, tem);
3234     }
3235
3236   /* Try widening the mode and perform a non-widening multiplication.  */
3237   moptab = smul_optab;
3238   if (smul_optab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3239       && size - 1 < BITS_PER_WORD
3240       && mul_cost[wider_mode] + shift_cost[mode][size-1] < max_cost)
3241     {
3242       tem = expand_binop (wider_mode, moptab, op0, op1, 0,
3243                           unsignedp, OPTAB_WIDEN);
3244       if (tem)
3245         return extract_high_half (mode, tem);
3246     }
3247
3248   /* Try widening multiplication of opposite signedness, and adjust.  */
3249   moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3250   if (moptab->handlers[wider_mode].insn_code != CODE_FOR_nothing
3251       && size - 1 < BITS_PER_WORD
3252       && (mul_widen_cost[wider_mode] + 2 * shift_cost[mode][size-1]
3253           + 4 * add_cost[mode] < max_cost))
3254     {
3255       tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3256                           NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3257       if (tem != 0)
3258         {
3259           tem = extract_high_half (mode, tem);
3260           /* We used the wrong signedness.  Adjust the result.  */
3261           return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3262                                               target, unsignedp);
3263         }
3264     }
3265
3266   return 0;
3267 }
3268
3269 /* Emit code to multiply OP0 and CNST1, putting the high half of the result
3270    in TARGET if that is convenient, and return where the result is.  If the
3271    operation can not be performed, 0 is returned.
3272
3273    MODE is the mode of operation and result.
3274
3275    UNSIGNEDP nonzero means unsigned multiply.
3276
3277    MAX_COST is the total allowed cost for the expanded RTL.  */
3278
3279 rtx
3280 expand_mult_highpart (enum machine_mode mode, rtx op0,
3281                       unsigned HOST_WIDE_INT cnst1, rtx target,
3282                       int unsignedp, int max_cost)
3283 {
3284   enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
3285   int extra_cost;
3286   bool sign_adjust = false;
3287   enum mult_variant variant;
3288   struct algorithm alg;
3289   rtx op1, tem;
3290
3291   /* We can't support modes wider than HOST_BITS_PER_INT.  */
3292   gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT);
3293
3294   op1 = gen_int_mode (cnst1, wider_mode);
3295   cnst1 &= GET_MODE_MASK (mode);
3296
3297   /* We can't optimize modes wider than BITS_PER_WORD. 
3298      ??? We might be able to perform double-word arithmetic if 
3299      mode == word_mode, however all the cost calculations in
3300      synth_mult etc. assume single-word operations.  */
3301   if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3302     return expand_mult_highpart_optab (mode, op0, op1, target,
3303                                        unsignedp, max_cost);
3304
3305   extra_cost = shift_cost[mode][GET_MODE_BITSIZE (mode) - 1];
3306
3307   /* Check whether we try to multiply by a negative constant.  */
3308   if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3309     {
3310       sign_adjust = true;
3311       extra_cost += add_cost[mode];
3312     }
3313
3314   /* See whether shift/add multiplication is cheap enough.  */
3315   if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3316                            max_cost - extra_cost))
3317     {
3318       /* See whether the specialized multiplication optabs are
3319          cheaper than the shift/add version.  */
3320       tem = expand_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3321                                         alg.cost.cost + extra_cost);
3322       if (tem)
3323         return tem;
3324
3325       tem = convert_to_mode (wider_mode, op0, unsignedp);
3326       tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3327       tem = extract_high_half (mode, tem);
3328
3329       /* Adjust result for signedness.  */
3330       if (sign_adjust)
3331         tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3332
3333       return tem;
3334     }
3335   return expand_mult_highpart_optab (mode, op0, op1, target,
3336                                      unsignedp, max_cost);
3337 }
3338
3339
3340 /* Expand signed modulus of OP0 by a power of two D in mode MODE.  */
3341
3342 static rtx
3343 expand_smod_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3344 {
3345   unsigned HOST_WIDE_INT masklow, maskhigh;
3346   rtx result, temp, shift, label;
3347   int logd;
3348
3349   logd = floor_log2 (d);
3350   result = gen_reg_rtx (mode);
3351
3352   /* Avoid conditional branches when they're expensive.  */
3353   if (BRANCH_COST >= 2
3354       && !optimize_size)
3355     {
3356       rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3357                                       mode, 0, -1);
3358       if (signmask)
3359         {
3360           signmask = force_reg (mode, signmask);
3361           masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3362           shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3363
3364           /* Use the rtx_cost of a LSHIFTRT instruction to determine
3365              which instruction sequence to use.  If logical right shifts
3366              are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3367              use a LSHIFTRT, 1 ADD, 1 SUB and an AND.  */
3368
3369           temp = gen_rtx_LSHIFTRT (mode, result, shift);
3370           if (lshr_optab->handlers[mode].insn_code == CODE_FOR_nothing
3371               || rtx_cost (temp, SET) > COSTS_N_INSNS (2))
3372             {
3373               temp = expand_binop (mode, xor_optab, op0, signmask,
3374                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
3375               temp = expand_binop (mode, sub_optab, temp, signmask,
3376                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
3377               temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3378                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
3379               temp = expand_binop (mode, xor_optab, temp, signmask,
3380                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
3381               temp = expand_binop (mode, sub_optab, temp, signmask,
3382                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
3383             }
3384           else
3385             {
3386               signmask = expand_binop (mode, lshr_optab, signmask, shift,
3387                                        NULL_RTX, 1, OPTAB_LIB_WIDEN);
3388               signmask = force_reg (mode, signmask);
3389
3390               temp = expand_binop (mode, add_optab, op0, signmask,
3391                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
3392               temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3393                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
3394               temp = expand_binop (mode, sub_optab, temp, signmask,
3395                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
3396             }
3397           return temp;
3398         }
3399     }
3400
3401   /* Mask contains the mode's signbit and the significant bits of the
3402      modulus.  By including the signbit in the operation, many targets
3403      can avoid an explicit compare operation in the following comparison
3404      against zero.  */
3405
3406   masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3407   if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3408     {
3409       masklow |= (HOST_WIDE_INT) -1 << (GET_MODE_BITSIZE (mode) - 1);
3410       maskhigh = -1;
3411     }
3412   else
3413     maskhigh = (HOST_WIDE_INT) -1
3414                  << (GET_MODE_BITSIZE (mode) - HOST_BITS_PER_WIDE_INT - 1);
3415
3416   temp = expand_binop (mode, and_optab, op0,
3417                        immed_double_const (masklow, maskhigh, mode),
3418                        result, 1, OPTAB_LIB_WIDEN);
3419   if (temp != result)
3420     emit_move_insn (result, temp);
3421
3422   label = gen_label_rtx ();
3423   do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3424
3425   temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3426                        0, OPTAB_LIB_WIDEN);
3427   masklow = (HOST_WIDE_INT) -1 << logd;
3428   maskhigh = -1;
3429   temp = expand_binop (mode, ior_optab, temp,
3430                        immed_double_const (masklow, maskhigh, mode),
3431                        result, 1, OPTAB_LIB_WIDEN);
3432   temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3433                        0, OPTAB_LIB_WIDEN);
3434   if (temp != result)
3435     emit_move_insn (result, temp);
3436   emit_label (label);
3437   return result;
3438 }
3439
3440 /* Expand signed division of OP0 by a power of two D in mode MODE.
3441    This routine is only called for positive values of D.  */
3442
3443 static rtx
3444 expand_sdiv_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3445 {
3446   rtx temp, label;
3447   tree shift;
3448   int logd;
3449
3450   logd = floor_log2 (d);
3451   shift = build_int_cst (NULL_TREE, logd);
3452
3453   if (d == 2 && BRANCH_COST >= 1)
3454     {
3455       temp = gen_reg_rtx (mode);
3456       temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3457       temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3458                            0, OPTAB_LIB_WIDEN);
3459       return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3460     }
3461
3462 #ifdef HAVE_conditional_move
3463   if (BRANCH_COST >= 2)
3464     {
3465       rtx temp2;
3466
3467       /* ??? emit_conditional_move forces a stack adjustment via
3468          compare_from_rtx so, if the sequence is discarded, it will
3469          be lost.  Do it now instead.  */
3470       do_pending_stack_adjust ();
3471
3472       start_sequence ();
3473       temp2 = copy_to_mode_reg (mode, op0);
3474       temp = expand_binop (mode, add_optab, temp2, GEN_INT (d-1),
3475                            NULL_RTX, 0, OPTAB_LIB_WIDEN);
3476       temp = force_reg (mode, temp);
3477
3478       /* Construct "temp2 = (temp2 < 0) ? temp : temp2".  */
3479       temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3480                                      mode, temp, temp2, mode, 0);
3481       if (temp2)
3482         {
3483           rtx seq = get_insns ();
3484           end_sequence ();
3485           emit_insn (seq);
3486           return expand_shift (RSHIFT_EXPR, mode, temp2, shift, NULL_RTX, 0);
3487         }
3488       end_sequence ();
3489     }
3490 #endif
3491
3492   if (BRANCH_COST >= 2)
3493     {
3494       int ushift = GET_MODE_BITSIZE (mode) - logd;
3495
3496       temp = gen_reg_rtx (mode);
3497       temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3498       if (shift_cost[mode][ushift] > COSTS_N_INSNS (1))
3499         temp = expand_binop (mode, and_optab, temp, GEN_INT (d - 1),
3500                              NULL_RTX, 0, OPTAB_LIB_WIDEN);
3501       else
3502         temp = expand_shift (RSHIFT_EXPR, mode, temp,
3503                              build_int_cst (NULL_TREE, ushift),
3504                              NULL_RTX, 1);
3505       temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3506                            0, OPTAB_LIB_WIDEN);
3507       return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3508     }
3509
3510   label = gen_label_rtx ();
3511   temp = copy_to_mode_reg (mode, op0);
3512   do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3513   expand_inc (temp, GEN_INT (d - 1));
3514   emit_label (label);
3515   return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3516 }
3517 \f
3518 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3519    if that is convenient, and returning where the result is.
3520    You may request either the quotient or the remainder as the result;
3521    specify REM_FLAG nonzero to get the remainder.
3522
3523    CODE is the expression code for which kind of division this is;
3524    it controls how rounding is done.  MODE is the machine mode to use.
3525    UNSIGNEDP nonzero means do unsigned division.  */
3526
3527 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3528    and then correct it by or'ing in missing high bits
3529    if result of ANDI is nonzero.
3530    For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3531    This could optimize to a bfexts instruction.
3532    But C doesn't use these operations, so their optimizations are
3533    left for later.  */
3534 /* ??? For modulo, we don't actually need the highpart of the first product,
3535    the low part will do nicely.  And for small divisors, the second multiply
3536    can also be a low-part only multiply or even be completely left out.
3537    E.g. to calculate the remainder of a division by 3 with a 32 bit
3538    multiply, multiply with 0x55555556 and extract the upper two bits;
3539    the result is exact for inputs up to 0x1fffffff.
3540    The input range can be reduced by using cross-sum rules.
3541    For odd divisors >= 3, the following table gives right shift counts
3542    so that if a number is shifted by an integer multiple of the given
3543    amount, the remainder stays the same:
3544    2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3545    14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3546    0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3547    20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3548    0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3549
3550    Cross-sum rules for even numbers can be derived by leaving as many bits
3551    to the right alone as the divisor has zeros to the right.
3552    E.g. if x is an unsigned 32 bit number:
3553    (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3554    */
3555
3556 #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
3557
3558 rtx
3559 expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
3560                rtx op0, rtx op1, rtx target, int unsignedp)
3561 {
3562   enum machine_mode compute_mode;
3563   rtx tquotient;
3564   rtx quotient = 0, remainder = 0;
3565   rtx last;
3566   int size;
3567   rtx insn, set;
3568   optab optab1, optab2;
3569   int op1_is_constant, op1_is_pow2 = 0;
3570   int max_cost, extra_cost;
3571   static HOST_WIDE_INT last_div_const = 0;
3572   static HOST_WIDE_INT ext_op1;
3573
3574   op1_is_constant = GET_CODE (op1) == CONST_INT;
3575   if (op1_is_constant)
3576     {
3577       ext_op1 = INTVAL (op1);
3578       if (unsignedp)
3579         ext_op1 &= GET_MODE_MASK (mode);
3580       op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
3581                      || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
3582     }
3583
3584   /*
3585      This is the structure of expand_divmod:
3586
3587      First comes code to fix up the operands so we can perform the operations
3588      correctly and efficiently.
3589
3590      Second comes a switch statement with code specific for each rounding mode.
3591      For some special operands this code emits all RTL for the desired
3592      operation, for other cases, it generates only a quotient and stores it in
3593      QUOTIENT.  The case for trunc division/remainder might leave quotient = 0,
3594      to indicate that it has not done anything.
3595
3596      Last comes code that finishes the operation.  If QUOTIENT is set and
3597      REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1.  If
3598      QUOTIENT is not set, it is computed using trunc rounding.
3599
3600      We try to generate special code for division and remainder when OP1 is a
3601      constant.  If |OP1| = 2**n we can use shifts and some other fast
3602      operations.  For other values of OP1, we compute a carefully selected
3603      fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3604      by m.
3605
3606      In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3607      half of the product.  Different strategies for generating the product are
3608      implemented in expand_mult_highpart.
3609
3610      If what we actually want is the remainder, we generate that by another
3611      by-constant multiplication and a subtraction.  */
3612
3613   /* We shouldn't be called with OP1 == const1_rtx, but some of the
3614      code below will malfunction if we are, so check here and handle
3615      the special case if so.  */
3616   if (op1 == const1_rtx)
3617     return rem_flag ? const0_rtx : op0;
3618
3619     /* When dividing by -1, we could get an overflow.
3620      negv_optab can handle overflows.  */
3621   if (! unsignedp && op1 == constm1_rtx)
3622     {
3623       if (rem_flag)
3624         return const0_rtx;
3625       return expand_unop (mode, flag_trapv && GET_MODE_CLASS(mode) == MODE_INT
3626                           ? negv_optab : neg_optab, op0, target, 0);
3627     }
3628
3629   if (target
3630       /* Don't use the function value register as a target
3631          since we have to read it as well as write it,
3632          and function-inlining gets confused by this.  */
3633       && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3634           /* Don't clobber an operand while doing a multi-step calculation.  */
3635           || ((rem_flag || op1_is_constant)
3636               && (reg_mentioned_p (target, op0)
3637                   || (MEM_P (op0) && MEM_P (target))))
3638           || reg_mentioned_p (target, op1)
3639           || (MEM_P (op1) && MEM_P (target))))
3640     target = 0;
3641
3642   /* Get the mode in which to perform this computation.  Normally it will
3643      be MODE, but sometimes we can't do the desired operation in MODE.
3644      If so, pick a wider mode in which we can do the operation.  Convert
3645      to that mode at the start to avoid repeated conversions.
3646
3647      First see what operations we need.  These depend on the expression
3648      we are evaluating.  (We assume that divxx3 insns exist under the
3649      same conditions that modxx3 insns and that these insns don't normally
3650      fail.  If these assumptions are not correct, we may generate less
3651      efficient code in some cases.)
3652
3653      Then see if we find a mode in which we can open-code that operation
3654      (either a division, modulus, or shift).  Finally, check for the smallest
3655      mode for which we can do the operation with a library call.  */
3656
3657   /* We might want to refine this now that we have division-by-constant
3658      optimization.  Since expand_mult_highpart tries so many variants, it is
3659      not straightforward to generalize this.  Maybe we should make an array
3660      of possible modes in init_expmed?  Save this for GCC 2.7.  */
3661
3662   optab1 = ((op1_is_pow2 && op1 != const0_rtx)
3663             ? (unsignedp ? lshr_optab : ashr_optab)
3664             : (unsignedp ? udiv_optab : sdiv_optab));
3665   optab2 = ((op1_is_pow2 && op1 != const0_rtx)
3666             ? optab1
3667             : (unsignedp ? udivmod_optab : sdivmod_optab));
3668
3669   for (compute_mode = mode; compute_mode != VOIDmode;
3670        compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3671     if (optab1->handlers[compute_mode].insn_code != CODE_FOR_nothing
3672         || optab2->handlers[compute_mode].insn_code != CODE_FOR_nothing)
3673       break;
3674
3675   if (compute_mode == VOIDmode)
3676     for (compute_mode = mode; compute_mode != VOIDmode;
3677          compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3678       if (optab1->handlers[compute_mode].libfunc
3679           || optab2->handlers[compute_mode].libfunc)
3680         break;
3681
3682   /* If we still couldn't find a mode, use MODE, but we'll probably abort
3683      in expand_binop.  */
3684   if (compute_mode == VOIDmode)
3685     compute_mode = mode;
3686
3687   if (target && GET_MODE (target) == compute_mode)
3688     tquotient = target;
3689   else
3690     tquotient = gen_reg_rtx (compute_mode);
3691
3692   size = GET_MODE_BITSIZE (compute_mode);
3693 #if 0
3694   /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3695      (mode), and thereby get better code when OP1 is a constant.  Do that
3696      later.  It will require going over all usages of SIZE below.  */
3697   size = GET_MODE_BITSIZE (mode);
3698 #endif
3699
3700   /* Only deduct something for a REM if the last divide done was
3701      for a different constant.   Then set the constant of the last
3702      divide.  */
3703   max_cost = div_cost[compute_mode]
3704     - (rem_flag && ! (last_div_const != 0 && op1_is_constant
3705                       && INTVAL (op1) == last_div_const)
3706        ? mul_cost[compute_mode] + add_cost[compute_mode]
3707        : 0);
3708
3709   last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
3710
3711   /* Now convert to the best mode to use.  */
3712   if (compute_mode != mode)
3713     {
3714       op0 = convert_modes (compute_mode, mode, op0, unsignedp);
3715       op1 = convert_modes (compute_mode, mode, op1, unsignedp);
3716
3717       /* convert_modes may have placed op1 into a register, so we
3718          must recompute the following.  */
3719       op1_is_constant = GET_CODE (op1) == CONST_INT;
3720       op1_is_pow2 = (op1_is_constant
3721                      && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3722                           || (! unsignedp
3723                               && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
3724     }
3725
3726   /* If one of the operands is a volatile MEM, copy it into a register.  */
3727
3728   if (MEM_P (op0) && MEM_VOLATILE_P (op0))
3729     op0 = force_reg (compute_mode, op0);
3730   if (MEM_P (op1) && MEM_VOLATILE_P (op1))
3731     op1 = force_reg (compute_mode, op1);
3732
3733   /* If we need the remainder or if OP1 is constant, we need to
3734      put OP0 in a register in case it has any queued subexpressions.  */
3735   if (rem_flag || op1_is_constant)
3736     op0 = force_reg (compute_mode, op0);
3737
3738   last = get_last_insn ();
3739
3740   /* Promote floor rounding to trunc rounding for unsigned operations.  */
3741   if (unsignedp)
3742     {
3743       if (code == FLOOR_DIV_EXPR)
3744         code = TRUNC_DIV_EXPR;
3745       if (code == FLOOR_MOD_EXPR)
3746         code = TRUNC_MOD_EXPR;
3747       if (code == EXACT_DIV_EXPR && op1_is_pow2)
3748         code = TRUNC_DIV_EXPR;
3749     }
3750
3751   if (op1 != const0_rtx)
3752     switch (code)
3753       {
3754       case TRUNC_MOD_EXPR:
3755       case TRUNC_DIV_EXPR:
3756         if (op1_is_constant)
3757           {
3758             if (unsignedp)
3759               {
3760                 unsigned HOST_WIDE_INT mh, ml;
3761                 int pre_shift, post_shift;
3762                 int dummy;
3763                 unsigned HOST_WIDE_INT d = (INTVAL (op1)
3764                                             & GET_MODE_MASK (compute_mode));
3765
3766                 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3767                   {
3768                     pre_shift = floor_log2 (d);
3769                     if (rem_flag)
3770                       {
3771                         remainder
3772                           = expand_binop (compute_mode, and_optab, op0,
3773                                           GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3774                                           remainder, 1,
3775                                           OPTAB_LIB_WIDEN);
3776                         if (remainder)
3777                           return gen_lowpart (mode, remainder);
3778                       }
3779                     quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3780                                              build_int_cst (NULL_TREE,
3781                                                             pre_shift),
3782                                              tquotient, 1);
3783                   }
3784                 else if (size <= HOST_BITS_PER_WIDE_INT)
3785                   {
3786                     if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
3787                       {
3788                         /* Most significant bit of divisor is set; emit an scc
3789                            insn.  */
3790                         quotient = emit_store_flag (tquotient, GEU, op0, op1,
3791                                                     compute_mode, 1, 1);
3792                         if (quotient == 0)
3793                           goto fail1;
3794                       }
3795                     else
3796                       {
3797                         /* Find a suitable multiplier and right shift count
3798                            instead of multiplying with D.  */
3799
3800                         mh = choose_multiplier (d, size, size,
3801                                                 &ml, &post_shift, &dummy);
3802
3803                         /* If the suggested multiplier is more than SIZE bits,
3804                            we can do better for even divisors, using an
3805                            initial right shift.  */
3806                         if (mh != 0 && (d & 1) == 0)
3807                           {
3808                             pre_shift = floor_log2 (d & -d);
3809                             mh = choose_multiplier (d >> pre_shift, size,
3810                                                     size - pre_shift,
3811                                                     &ml, &post_shift, &dummy);
3812                             gcc_assert (!mh);
3813                           }
3814                         else
3815                           pre_shift = 0;
3816
3817                         if (mh != 0)
3818                           {
3819                             rtx t1, t2, t3, t4;
3820
3821                             if (post_shift - 1 >= BITS_PER_WORD)
3822                               goto fail1;
3823
3824                             extra_cost
3825                               = (shift_cost[compute_mode][post_shift - 1]
3826                                  + shift_cost[compute_mode][1]
3827                                  + 2 * add_cost[compute_mode]);
3828                             t1 = expand_mult_highpart (compute_mode, op0, ml,
3829                                                        NULL_RTX, 1,
3830                                                        max_cost - extra_cost);
3831                             if (t1 == 0)
3832                               goto fail1;
3833                             t2 = force_operand (gen_rtx_MINUS (compute_mode,
3834                                                                op0, t1),
3835                                                 NULL_RTX);
3836                             t3 = expand_shift
3837                               (RSHIFT_EXPR, compute_mode, t2,
3838                                build_int_cst (NULL_TREE, 1),
3839                                NULL_RTX,1);
3840                             t4 = force_operand (gen_rtx_PLUS (compute_mode,
3841                                                               t1, t3),
3842                                                 NULL_RTX);
3843                             quotient = expand_shift
3844                               (RSHIFT_EXPR, compute_mode, t4,
3845                                build_int_cst (NULL_TREE, post_shift - 1),
3846                                tquotient, 1);
3847                           }
3848                         else
3849                           {
3850                             rtx t1, t2;
3851
3852                             if (pre_shift >= BITS_PER_WORD
3853                                 || post_shift >= BITS_PER_WORD)
3854                               goto fail1;
3855
3856                             t1 = expand_shift
3857                               (RSHIFT_EXPR, compute_mode, op0,
3858                                build_int_cst (NULL_TREE, pre_shift),
3859                                NULL_RTX, 1);
3860                             extra_cost
3861                               = (shift_cost[compute_mode][pre_shift]
3862                                  + shift_cost[compute_mode][post_shift]);
3863                             t2 = expand_mult_highpart (compute_mode, t1, ml,
3864                                                        NULL_RTX, 1,
3865                                                        max_cost - extra_cost);
3866                             if (t2 == 0)
3867                               goto fail1;
3868                             quotient = expand_shift
3869                               (RSHIFT_EXPR, compute_mode, t2,
3870                                build_int_cst (NULL_TREE, post_shift),
3871                                tquotient, 1);
3872                           }
3873                       }
3874                   }
3875                 else            /* Too wide mode to use tricky code */
3876                   break;
3877
3878                 insn = get_last_insn ();
3879                 if (insn != last
3880                     && (set = single_set (insn)) != 0
3881                     && SET_DEST (set) == quotient)
3882                   set_unique_reg_note (insn,
3883                                        REG_EQUAL,
3884                                        gen_rtx_UDIV (compute_mode, op0, op1));
3885               }
3886             else                /* TRUNC_DIV, signed */
3887               {
3888                 unsigned HOST_WIDE_INT ml;
3889                 int lgup, post_shift;
3890                 HOST_WIDE_INT d = INTVAL (op1);
3891                 unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
3892
3893                 /* n rem d = n rem -d */
3894                 if (rem_flag && d < 0)
3895                   {
3896                     d = abs_d;
3897                     op1 = gen_int_mode (abs_d, compute_mode);
3898                   }
3899
3900                 if (d == 1)
3901                   quotient = op0;
3902                 else if (d == -1)
3903                   quotient = expand_unop (compute_mode, neg_optab, op0,
3904                                           tquotient, 0);
3905                 else if (abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
3906                   {
3907                     /* This case is not handled correctly below.  */
3908                     quotient = emit_store_flag (tquotient, EQ, op0, op1,
3909                                                 compute_mode, 1, 1);
3910                     if (quotient == 0)
3911                       goto fail1;
3912                   }
3913                 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
3914                          && (rem_flag ? smod_pow2_cheap[compute_mode]
3915                                       : sdiv_pow2_cheap[compute_mode])
3916                          /* We assume that cheap metric is true if the
3917                             optab has an expander for this mode.  */
3918                          && (((rem_flag ? smod_optab : sdiv_optab)
3919                               ->handlers[compute_mode].insn_code
3920                               != CODE_FOR_nothing)
3921                              || (sdivmod_optab->handlers[compute_mode]
3922                                  .insn_code != CODE_FOR_nothing)))
3923                   ;
3924                 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
3925                   {
3926                     if (rem_flag)
3927                       {
3928                         remainder = expand_smod_pow2 (compute_mode, op0, d);
3929                         if (remainder)
3930                           return gen_lowpart (mode, remainder);
3931                       }
3932
3933                     if (sdiv_pow2_cheap[compute_mode]
3934                         && ((sdiv_optab->handlers[compute_mode].insn_code
3935                              != CODE_FOR_nothing)
3936                             || (sdivmod_optab->handlers[compute_mode].insn_code
3937                                 != CODE_FOR_nothing)))
3938                       quotient = expand_divmod (0, TRUNC_DIV_EXPR,
3939                                                 compute_mode, op0,
3940                                                 gen_int_mode (abs_d,
3941                                                               compute_mode),
3942                                                 NULL_RTX, 0);
3943                     else
3944                       quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
3945
3946                     /* We have computed OP0 / abs(OP1).  If OP1 is negative,
3947                        negate the quotient.  */
3948                     if (d < 0)
3949                       {
3950                         insn = get_last_insn ();
3951                         if (insn != last
3952                             && (set = single_set (insn)) != 0
3953                             && SET_DEST (set) == quotient
3954                             && abs_d < ((unsigned HOST_WIDE_INT) 1
3955                                         << (HOST_BITS_PER_WIDE_INT - 1)))
3956                           set_unique_reg_note (insn,
3957                                                REG_EQUAL,
3958                                                gen_rtx_DIV (compute_mode,
3959                                                             op0,
3960                                                             GEN_INT
3961                                                             (trunc_int_for_mode
3962                                                              (abs_d,
3963                                                               compute_mode))));
3964
3965                         quotient = expand_unop (compute_mode, neg_optab,
3966                                                 quotient, quotient, 0);
3967                       }
3968                   }
3969                 else if (size <= HOST_BITS_PER_WIDE_INT)
3970                   {
3971                     choose_multiplier (abs_d, size, size - 1,
3972                                        &ml, &post_shift, &lgup);
3973                     if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
3974                       {
3975                         rtx t1, t2, t3;
3976
3977                         if (post_shift >= BITS_PER_WORD
3978                             || size - 1 >= BITS_PER_WORD)
3979                           goto fail1;
3980
3981                         extra_cost = (shift_cost[compute_mode][post_shift]
3982                                       + shift_cost[compute_mode][size - 1]
3983                                       + add_cost[compute_mode]);
3984                         t1 = expand_mult_highpart (compute_mode, op0, ml,
3985                                                    NULL_RTX, 0,
3986                                                    max_cost - extra_cost);
3987                         if (t1 == 0)
3988                           goto fail1;
3989                         t2 = expand_shift
3990                           (RSHIFT_EXPR, compute_mode, t1,
3991                            build_int_cst (NULL_TREE, post_shift),
3992                            NULL_RTX, 0);
3993                         t3 = expand_shift
3994                           (RSHIFT_EXPR, compute_mode, op0,
3995                            build_int_cst (NULL_TREE, size - 1),
3996                            NULL_RTX, 0);
3997                         if (d < 0)
3998                           quotient
3999                             = force_operand (gen_rtx_MINUS (compute_mode,
4000                                                             t3, t2),
4001                                              tquotient);
4002                         else
4003                           quotient
4004                             = force_operand (gen_rtx_MINUS (compute_mode,
4005                                                             t2, t3),
4006                                              tquotient);
4007                       }
4008                     else
4009                       {
4010                         rtx t1, t2, t3, t4;
4011
4012                         if (post_shift >= BITS_PER_WORD
4013                             || size - 1 >= BITS_PER_WORD)
4014                           goto fail1;
4015
4016                         ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
4017                         extra_cost = (shift_cost[compute_mode][post_shift]
4018                                       + shift_cost[compute_mode][size - 1]
4019                                       + 2 * add_cost[compute_mode]);
4020                         t1 = expand_mult_highpart (compute_mode, op0, ml,
4021                                                    NULL_RTX, 0,
4022                                                    max_cost - extra_cost);
4023                         if (t1 == 0)
4024                           goto fail1;
4025                         t2 = force_operand (gen_rtx_PLUS (compute_mode,
4026                                                           t1, op0),
4027                                             NULL_RTX);
4028                         t3 = expand_shift
4029                           (RSHIFT_EXPR, compute_mode, t2,
4030                            build_int_cst (NULL_TREE, post_shift),
4031                            NULL_RTX, 0);
4032                         t4 = expand_shift
4033                           (RSHIFT_EXPR, compute_mode, op0,
4034                            build_int_cst (NULL_TREE, size - 1),
4035                            NULL_RTX, 0);
4036                         if (d < 0)
4037                           quotient
4038                             = force_operand (gen_rtx_MINUS (compute_mode,
4039                                                             t4, t3),
4040                                              tquotient);
4041                         else
4042                           quotient
4043                             = force_operand (gen_rtx_MINUS (compute_mode,
4044                                                             t3, t4),
4045                                              tquotient);
4046                       }
4047                   }
4048                 else            /* Too wide mode to use tricky code */
4049                   break;
4050
4051                 insn = get_last_insn ();
4052                 if (insn != last
4053                     && (set = single_set (insn)) != 0
4054                     && SET_DEST (set) == quotient)
4055                   set_unique_reg_note (insn,
4056                                        REG_EQUAL,
4057                                        gen_rtx_DIV (compute_mode, op0, op1));
4058               }
4059             break;
4060           }
4061       fail1:
4062         delete_insns_since (last);
4063         break;
4064
4065       case FLOOR_DIV_EXPR:
4066       case FLOOR_MOD_EXPR:
4067       /* We will come here only for signed operations.  */
4068         if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4069           {
4070             unsigned HOST_WIDE_INT mh, ml;
4071             int pre_shift, lgup, post_shift;
4072             HOST_WIDE_INT d = INTVAL (op1);
4073
4074             if (d > 0)
4075               {
4076                 /* We could just as easily deal with negative constants here,
4077                    but it does not seem worth the trouble for GCC 2.6.  */
4078                 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4079                   {
4080                     pre_shift = floor_log2 (d);
4081                     if (rem_flag)
4082                       {
4083                         remainder = expand_binop (compute_mode, and_optab, op0,
4084                                                   GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4085                                                   remainder, 0, OPTAB_LIB_WIDEN);
4086                         if (remainder)
4087                           return gen_lowpart (mode, remainder);
4088                       }
4089                     quotient = expand_shift
4090                       (RSHIFT_EXPR, compute_mode, op0,
4091                        build_int_cst (NULL_TREE, pre_shift),
4092                        tquotient, 0);
4093                   }
4094                 else
4095                   {
4096                     rtx t1, t2, t3, t4;
4097
4098                     mh = choose_multiplier (d, size, size - 1,
4099                                             &ml, &post_shift, &lgup);
4100                     gcc_assert (!mh);
4101
4102                     if (post_shift < BITS_PER_WORD
4103                         && size - 1 < BITS_PER_WORD)
4104                       {
4105                         t1 = expand_shift
4106                           (RSHIFT_EXPR, compute_mode, op0,
4107                            build_int_cst (NULL_TREE, size - 1),
4108                            NULL_RTX, 0);
4109                         t2 = expand_binop (compute_mode, xor_optab, op0, t1,
4110                                            NULL_RTX, 0, OPTAB_WIDEN);
4111                         extra_cost = (shift_cost[compute_mode][post_shift]
4112                                       + shift_cost[compute_mode][size - 1]
4113                                       + 2 * add_cost[compute_mode]);
4114                         t3 = expand_mult_highpart (compute_mode, t2, ml,
4115                                                    NULL_RTX, 1,
4116                                                    max_cost - extra_cost);
4117                         if (t3 != 0)
4118                           {
4119                             t4 = expand_shift
4120                               (RSHIFT_EXPR, compute_mode, t3,
4121                                build_int_cst (NULL_TREE, post_shift),
4122                                NULL_RTX, 1);
4123                             quotient = expand_binop (compute_mode, xor_optab,
4124                                                      t4, t1, tquotient, 0,
4125                                                      OPTAB_WIDEN);
4126                           }
4127                       }
4128                   }
4129               }
4130             else
4131               {
4132                 rtx nsign, t1, t2, t3, t4;
4133                 t1 = force_operand (gen_rtx_PLUS (compute_mode,
4134                                                   op0, constm1_rtx), NULL_RTX);
4135                 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
4136                                    0, OPTAB_WIDEN);
4137                 nsign = expand_shift
4138                   (RSHIFT_EXPR, compute_mode, t2,
4139                    build_int_cst (NULL_TREE, size - 1),
4140                    NULL_RTX, 0);
4141                 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
4142                                     NULL_RTX);
4143                 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
4144                                     NULL_RTX, 0);
4145                 if (t4)
4146                   {
4147                     rtx t5;
4148                     t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
4149                                       NULL_RTX, 0);
4150                     quotient = force_operand (gen_rtx_PLUS (compute_mode,
4151                                                             t4, t5),
4152                                               tquotient);
4153                   }
4154               }
4155           }
4156
4157         if (quotient != 0)
4158           break;
4159         delete_insns_since (last);
4160
4161         /* Try using an instruction that produces both the quotient and
4162            remainder, using truncation.  We can easily compensate the quotient
4163            or remainder to get floor rounding, once we have the remainder.
4164            Notice that we compute also the final remainder value here,
4165            and return the result right away.  */
4166         if (target == 0 || GET_MODE (target) != compute_mode)
4167           target = gen_reg_rtx (compute_mode);
4168
4169         if (rem_flag)
4170           {
4171             remainder
4172               = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4173             quotient = gen_reg_rtx (compute_mode);
4174           }
4175         else
4176           {
4177             quotient
4178               = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4179             remainder = gen_reg_rtx (compute_mode);
4180           }
4181
4182         if (expand_twoval_binop (sdivmod_optab, op0, op1,
4183                                  quotient, remainder, 0))
4184           {
4185             /* This could be computed with a branch-less sequence.
4186                Save that for later.  */
4187             rtx tem;
4188             rtx label = gen_label_rtx ();
4189             do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4190             tem = expand_binop (compute_mode, xor_optab, op0, op1,
4191                                 NULL_RTX, 0, OPTAB_WIDEN);
4192             do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4193             expand_dec (quotient, const1_rtx);
4194             expand_inc (remainder, op1);
4195             emit_label (label);
4196             return gen_lowpart (mode, rem_flag ? remainder : quotient);
4197           }
4198
4199         /* No luck with division elimination or divmod.  Have to do it
4200            by conditionally adjusting op0 *and* the result.  */
4201         {
4202           rtx label1, label2, label3, label4, label5;
4203           rtx adjusted_op0;
4204           rtx tem;
4205
4206           quotient = gen_reg_rtx (compute_mode);
4207           adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4208           label1 = gen_label_rtx ();
4209           label2 = gen_label_rtx ();
4210           label3 = gen_label_rtx ();
4211           label4 = gen_label_rtx ();
4212           label5 = gen_label_rtx ();
4213           do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4214           do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4215           tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4216                               quotient, 0, OPTAB_LIB_WIDEN);
4217           if (tem != quotient)
4218             emit_move_insn (quotient, tem);
4219           emit_jump_insn (gen_jump (label5));
4220           emit_barrier ();
4221           emit_label (label1);
4222           expand_inc (adjusted_op0, const1_rtx);
4223           emit_jump_insn (gen_jump (label4));
4224           emit_barrier ();
4225           emit_label (label2);
4226           do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4227           tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4228                               quotient, 0, OPTAB_LIB_WIDEN);
4229           if (tem != quotient)
4230             emit_move_insn (quotient, tem);
4231           emit_jump_insn (gen_jump (label5));
4232           emit_barrier ();
4233           emit_label (label3);
4234           expand_dec (adjusted_op0, const1_rtx);
4235           emit_label (label4);
4236           tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4237                               quotient, 0, OPTAB_LIB_WIDEN);
4238           if (tem != quotient)
4239             emit_move_insn (quotient, tem);
4240           expand_dec (quotient, const1_rtx);
4241           emit_label (label5);
4242         }
4243         break;
4244
4245       case CEIL_DIV_EXPR:
4246       case CEIL_MOD_EXPR:
4247         if (unsignedp)
4248           {
4249             if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
4250               {
4251                 rtx t1, t2, t3;
4252                 unsigned HOST_WIDE_INT d = INTVAL (op1);
4253                 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4254                                    build_int_cst (NULL_TREE, floor_log2 (d)),
4255                                    tquotient, 1);
4256                 t2 = expand_binop (compute_mode, and_optab, op0,
4257                                    GEN_INT (d - 1),
4258                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
4259                 t3 = gen_reg_rtx (compute_mode);
4260                 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4261                                       compute_mode, 1, 1);
4262                 if (t3 == 0)
4263                   {
4264                     rtx lab;
4265                     lab = gen_label_rtx ();
4266                     do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4267                     expand_inc (t1, const1_rtx);
4268                     emit_label (lab);
4269                     quotient = t1;
4270                   }
4271                 else
4272                   quotient = force_operand (gen_rtx_PLUS (compute_mode,
4273                                                           t1, t3),
4274                                             tquotient);
4275                 break;
4276               }
4277
4278             /* Try using an instruction that produces both the quotient and
4279                remainder, using truncation.  We can easily compensate the
4280                quotient or remainder to get ceiling rounding, once we have the
4281                remainder.  Notice that we compute also the final remainder
4282                value here, and return the result right away.  */
4283             if (target == 0 || GET_MODE (target) != compute_mode)
4284               target = gen_reg_rtx (compute_mode);
4285
4286             if (rem_flag)
4287               {
4288                 remainder = (REG_P (target)
4289                              ? target : gen_reg_rtx (compute_mode));
4290                 quotient = gen_reg_rtx (compute_mode);
4291               }
4292             else
4293               {
4294                 quotient = (REG_P (target)
4295                             ? target : gen_reg_rtx (compute_mode));
4296                 remainder = gen_reg_rtx (compute_mode);
4297               }
4298
4299             if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4300                                      remainder, 1))
4301               {
4302                 /* This could be computed with a branch-less sequence.
4303                    Save that for later.  */
4304                 rtx label = gen_label_rtx ();
4305                 do_cmp_and_jump (remainder, const0_rtx, EQ,
4306                                  compute_mode, label);
4307                 expand_inc (quotient, const1_rtx);
4308                 expand_dec (remainder, op1);
4309                 emit_label (label);
4310                 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4311               }
4312
4313             /* No luck with division elimination or divmod.  Have to do it
4314                by conditionally adjusting op0 *and* the result.  */
4315             {
4316               rtx label1, label2;
4317               rtx adjusted_op0, tem;
4318
4319               quotient = gen_reg_rtx (compute_mode);
4320               adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4321               label1 = gen_label_rtx ();
4322               label2 = gen_label_rtx ();
4323               do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4324                                compute_mode, label1);
4325               emit_move_insn  (quotient, const0_rtx);
4326               emit_jump_insn (gen_jump (label2));
4327               emit_barrier ();
4328               emit_label (label1);
4329               expand_dec (adjusted_op0, const1_rtx);
4330               tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4331                                   quotient, 1, OPTAB_LIB_WIDEN);
4332               if (tem != quotient)
4333                 emit_move_insn (quotient, tem);
4334               expand_inc (quotient, const1_rtx);
4335               emit_label (label2);
4336             }
4337           }
4338         else /* signed */
4339           {
4340             if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4341                 && INTVAL (op1) >= 0)
4342               {
4343                 /* This is extremely similar to the code for the unsigned case
4344                    above.  For 2.7 we should merge these variants, but for
4345                    2.6.1 I don't want to touch the code for unsigned since that
4346                    get used in C.  The signed case will only be used by other
4347                    languages (Ada).  */
4348
4349                 rtx t1, t2, t3;
4350                 unsigned HOST_WIDE_INT d = INTVAL (op1);
4351                 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4352                                    build_int_cst (NULL_TREE, floor_log2 (d)),
4353                                    tquotient, 0);
4354                 t2 = expand_binop (compute_mode, and_optab, op0,
4355                                    GEN_INT (d - 1),
4356                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
4357                 t3 = gen_reg_rtx (compute_mode);
4358                 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4359                                       compute_mode, 1, 1);
4360                 if (t3 == 0)
4361                   {
4362                     rtx lab;
4363                     lab = gen_label_rtx ();
4364                     do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4365                     expand_inc (t1, const1_rtx);
4366                     emit_label (lab);
4367                     quotient = t1;
4368                   }
4369                 else
4370                   quotient = force_operand (gen_rtx_PLUS (compute_mode,
4371                                                           t1, t3),
4372                                             tquotient);
4373                 break;
4374               }
4375
4376             /* Try using an instruction that produces both the quotient and
4377                remainder, using truncation.  We can easily compensate the
4378                quotient or remainder to get ceiling rounding, once we have the
4379                remainder.  Notice that we compute also the final remainder
4380                value here, and return the result right away.  */
4381             if (target == 0 || GET_MODE (target) != compute_mode)
4382               target = gen_reg_rtx (compute_mode);
4383             if (rem_flag)
4384               {
4385                 remainder= (REG_P (target)
4386                             ? target : gen_reg_rtx (compute_mode));
4387                 quotient = gen_reg_rtx (compute_mode);
4388               }
4389             else
4390               {
4391                 quotient = (REG_P (target)
4392                             ? target : gen_reg_rtx (compute_mode));
4393                 remainder = gen_reg_rtx (compute_mode);
4394               }
4395
4396             if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4397                                      remainder, 0))
4398               {
4399                 /* This could be computed with a branch-less sequence.
4400                    Save that for later.  */
4401                 rtx tem;
4402                 rtx label = gen_label_rtx ();
4403                 do_cmp_and_jump (remainder, const0_rtx, EQ,
4404                                  compute_mode, label);
4405                 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4406                                     NULL_RTX, 0, OPTAB_WIDEN);
4407                 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4408                 expand_inc (quotient, const1_rtx);
4409                 expand_dec (remainder, op1);
4410                 emit_label (label);
4411                 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4412               }
4413
4414             /* No luck with division elimination or divmod.  Have to do it
4415                by conditionally adjusting op0 *and* the result.  */
4416             {
4417               rtx label1, label2, label3, label4, label5;
4418               rtx adjusted_op0;
4419               rtx tem;
4420
4421               quotient = gen_reg_rtx (compute_mode);
4422               adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4423               label1 = gen_label_rtx ();
4424               label2 = gen_label_rtx ();
4425               label3 = gen_label_rtx ();
4426               label4 = gen_label_rtx ();
4427               label5 = gen_label_rtx ();
4428               do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4429               do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4430                                compute_mode, label1);
4431               tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4432                                   quotient, 0, OPTAB_LIB_WIDEN);
4433               if (tem != quotient)
4434                 emit_move_insn (quotient, tem);
4435               emit_jump_insn (gen_jump (label5));
4436               emit_barrier ();
4437               emit_label (label1);
4438               expand_dec (adjusted_op0, const1_rtx);
4439               emit_jump_insn (gen_jump (label4));
4440               emit_barrier ();
4441               emit_label (label2);
4442               do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4443                                compute_mode, label3);
4444               tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4445                                   quotient, 0, OPTAB_LIB_WIDEN);
4446               if (tem != quotient)
4447                 emit_move_insn (quotient, tem);
4448               emit_jump_insn (gen_jump (label5));
4449               emit_barrier ();
4450               emit_label (label3);
4451               expand_inc (adjusted_op0, const1_rtx);
4452               emit_label (label4);
4453               tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4454                                   quotient, 0, OPTAB_LIB_WIDEN);
4455               if (tem != quotient)
4456                 emit_move_insn (quotient, tem);
4457               expand_inc (quotient, const1_rtx);
4458               emit_label (label5);
4459             }
4460           }
4461         break;
4462
4463       case EXACT_DIV_EXPR:
4464         if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4465           {
4466             HOST_WIDE_INT d = INTVAL (op1);
4467             unsigned HOST_WIDE_INT ml;
4468             int pre_shift;
4469             rtx t1;
4470
4471             pre_shift = floor_log2 (d & -d);
4472             ml = invert_mod2n (d >> pre_shift, size);
4473             t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4474                                build_int_cst (NULL_TREE, pre_shift),
4475                                NULL_RTX, unsignedp);
4476             quotient = expand_mult (compute_mode, t1,
4477                                     gen_int_mode (ml, compute_mode),
4478                                     NULL_RTX, 1);
4479
4480             insn = get_last_insn ();
4481             set_unique_reg_note (insn,
4482                                  REG_EQUAL,
4483                                  gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4484                                                  compute_mode,
4485                                                  op0, op1));
4486           }
4487         break;
4488
4489       case ROUND_DIV_EXPR:
4490       case ROUND_MOD_EXPR:
4491         if (unsignedp)
4492           {
4493             rtx tem;
4494             rtx label;
4495             label = gen_label_rtx ();
4496             quotient = gen_reg_rtx (compute_mode);
4497             remainder = gen_reg_rtx (compute_mode);
4498             if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4499               {
4500                 rtx tem;
4501                 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
4502                                          quotient, 1, OPTAB_LIB_WIDEN);
4503                 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
4504                 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4505                                           remainder, 1, OPTAB_LIB_WIDEN);
4506               }
4507             tem = plus_constant (op1, -1);
4508             tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4509                                 build_int_cst (NULL_TREE, 1),
4510                                 NULL_RTX, 1);
4511             do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
4512             expand_inc (quotient, const1_rtx);
4513             expand_dec (remainder, op1);
4514             emit_label (label);
4515           }
4516         else
4517           {
4518             rtx abs_rem, abs_op1, tem, mask;
4519             rtx label;
4520             label = gen_label_rtx ();
4521             quotient = gen_reg_rtx (compute_mode);
4522             remainder = gen_reg_rtx (compute_mode);
4523             if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
4524               {
4525                 rtx tem;
4526                 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
4527                                          quotient, 0, OPTAB_LIB_WIDEN);
4528                 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
4529                 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4530                                           remainder, 0, OPTAB_LIB_WIDEN);
4531               }
4532             abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
4533             abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
4534             tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
4535                                 build_int_cst (NULL_TREE, 1),
4536                                 NULL_RTX, 1);
4537             do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
4538             tem = expand_binop (compute_mode, xor_optab, op0, op1,
4539                                 NULL_RTX, 0, OPTAB_WIDEN);
4540             mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4541                                  build_int_cst (NULL_TREE, size - 1),
4542                                  NULL_RTX, 0);
4543             tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
4544                                 NULL_RTX, 0, OPTAB_WIDEN);
4545             tem = expand_binop (compute_mode, sub_optab, tem, mask,
4546                                 NULL_RTX, 0, OPTAB_WIDEN);
4547             expand_inc (quotient, tem);
4548             tem = expand_binop (compute_mode, xor_optab, mask, op1,
4549                                 NULL_RTX, 0, OPTAB_WIDEN);
4550             tem = expand_binop (compute_mode, sub_optab, tem, mask,
4551                                 NULL_RTX, 0, OPTAB_WIDEN);
4552             expand_dec (remainder, tem);
4553             emit_label (label);
4554           }
4555         return gen_lowpart (mode, rem_flag ? remainder : quotient);
4556
4557       default:
4558         gcc_unreachable ();
4559       }
4560
4561   if (quotient == 0)
4562     {
4563       if (target && GET_MODE (target) != compute_mode)
4564         target = 0;
4565
4566       if (rem_flag)
4567         {
4568           /* Try to produce the remainder without producing the quotient.
4569              If we seem to have a divmod pattern that does not require widening,
4570              don't try widening here.  We should really have a WIDEN argument
4571              to expand_twoval_binop, since what we'd really like to do here is
4572              1) try a mod insn in compute_mode
4573              2) try a divmod insn in compute_mode
4574              3) try a div insn in compute_mode and multiply-subtract to get
4575                 remainder
4576              4) try the same things with widening allowed.  */
4577           remainder
4578             = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4579                                  op0, op1, target,
4580                                  unsignedp,
4581                                  ((optab2->handlers[compute_mode].insn_code
4582                                    != CODE_FOR_nothing)
4583                                   ? OPTAB_DIRECT : OPTAB_WIDEN));
4584           if (remainder == 0)
4585             {
4586               /* No luck there.  Can we do remainder and divide at once
4587                  without a library call?  */
4588               remainder = gen_reg_rtx (compute_mode);
4589               if (! expand_twoval_binop ((unsignedp
4590                                           ? udivmod_optab
4591                                           : sdivmod_optab),
4592                                          op0, op1,
4593                                          NULL_RTX, remainder, unsignedp))
4594                 remainder = 0;
4595             }
4596
4597           if (remainder)
4598             return gen_lowpart (mode, remainder);
4599         }
4600
4601       /* Produce the quotient.  Try a quotient insn, but not a library call.
4602          If we have a divmod in this mode, use it in preference to widening
4603          the div (for this test we assume it will not fail). Note that optab2
4604          is set to the one of the two optabs that the call below will use.  */
4605       quotient
4606         = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
4607                              op0, op1, rem_flag ? NULL_RTX : target,
4608                              unsignedp,
4609                              ((optab2->handlers[compute_mode].insn_code
4610                                != CODE_FOR_nothing)
4611                               ? OPTAB_DIRECT : OPTAB_WIDEN));
4612
4613       if (quotient == 0)
4614         {
4615           /* No luck there.  Try a quotient-and-remainder insn,
4616              keeping the quotient alone.  */
4617           quotient = gen_reg_rtx (compute_mode);
4618           if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
4619                                      op0, op1,
4620                                      quotient, NULL_RTX, unsignedp))
4621             {
4622               quotient = 0;
4623               if (! rem_flag)
4624                 /* Still no luck.  If we are not computing the remainder,
4625                    use a library call for the quotient.  */
4626                 quotient = sign_expand_binop (compute_mode,
4627                                               udiv_optab, sdiv_optab,
4628                                               op0, op1, target,
4629                                               unsignedp, OPTAB_LIB_WIDEN);
4630             }
4631         }
4632     }
4633
4634   if (rem_flag)
4635     {
4636       if (target && GET_MODE (target) != compute_mode)
4637         target = 0;
4638
4639       if (quotient == 0)
4640         {
4641           /* No divide instruction either.  Use library for remainder.  */
4642           remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4643                                          op0, op1, target,
4644                                          unsignedp, OPTAB_LIB_WIDEN);
4645           /* No remainder function.  Try a quotient-and-remainder
4646              function, keeping the remainder.  */
4647           if (!remainder)
4648             {
4649               remainder = gen_reg_rtx (compute_mode);
4650               if (!expand_twoval_binop_libfunc 
4651                   (unsignedp ? udivmod_optab : sdivmod_optab,
4652                    op0, op1,
4653                    NULL_RTX, remainder,
4654                    unsignedp ? UMOD : MOD))
4655                 remainder = NULL_RTX;
4656             }
4657         }
4658       else
4659         {
4660           /* We divided.  Now finish doing X - Y * (X / Y).  */
4661           remainder = expand_mult (compute_mode, quotient, op1,
4662                                    NULL_RTX, unsignedp);
4663           remainder = expand_binop (compute_mode, sub_optab, op0,
4664                                     remainder, target, unsignedp,
4665                                     OPTAB_LIB_WIDEN);
4666         }
4667     }
4668
4669   return gen_lowpart (mode, rem_flag ? remainder : quotient);
4670 }
4671 \f
4672 /* Return a tree node with data type TYPE, describing the value of X.
4673    Usually this is an VAR_DECL, if there is no obvious better choice.
4674    X may be an expression, however we only support those expressions
4675    generated by loop.c.  */
4676
4677 tree
4678 make_tree (tree type, rtx x)
4679 {
4680   tree t;
4681
4682   switch (GET_CODE (x))
4683     {
4684     case CONST_INT:
4685       {
4686         HOST_WIDE_INT hi = 0;
4687
4688         if (INTVAL (x) < 0
4689             && !(TYPE_UNSIGNED (type)
4690                  && (GET_MODE_BITSIZE (TYPE_MODE (type))
4691                      < HOST_BITS_PER_WIDE_INT)))
4692           hi = -1;
4693       
4694         t = build_int_cst_wide (type, INTVAL (x), hi);
4695         
4696         return t;
4697       }
4698       
4699     case CONST_DOUBLE:
4700       if (GET_MODE (x) == VOIDmode)
4701         t = build_int_cst_wide (type,
4702                                 CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
4703       else
4704         {
4705           REAL_VALUE_TYPE d;
4706
4707           REAL_VALUE_FROM_CONST_DOUBLE (d, x);
4708           t = build_real (type, d);
4709         }
4710
4711       return t;
4712
4713     case CONST_VECTOR:
4714       {
4715         int i, units;
4716         rtx elt;
4717         tree t = NULL_TREE;
4718
4719         units = CONST_VECTOR_NUNITS (x);
4720
4721         /* Build a tree with vector elements.  */
4722         for (i = units - 1; i >= 0; --i)
4723           {
4724             elt = CONST_VECTOR_ELT (x, i);
4725             t = tree_cons (NULL_TREE, make_tree (type, elt), t);
4726           }
4727
4728         return build_vector (type, t);
4729       }
4730
4731     case PLUS:
4732       return fold (build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4733                            make_tree (type, XEXP (x, 1))));
4734
4735     case MINUS:
4736       return fold (build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4737                            make_tree (type, XEXP (x, 1))));
4738
4739     case NEG:
4740       return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
4741
4742     case MULT:
4743       return fold (build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
4744                            make_tree (type, XEXP (x, 1))));
4745
4746     case ASHIFT:
4747       return fold (build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
4748                            make_tree (type, XEXP (x, 1))));
4749
4750     case LSHIFTRT:
4751       t = lang_hooks.types.unsigned_type (type);
4752       return fold (convert (type,
4753                             build2 (RSHIFT_EXPR, t,
4754                                     make_tree (t, XEXP (x, 0)),
4755                                     make_tree (type, XEXP (x, 1)))));
4756
4757     case ASHIFTRT:
4758       t = lang_hooks.types.signed_type (type);
4759       return fold (convert (type,
4760                             build2 (RSHIFT_EXPR, t,
4761                                     make_tree (t, XEXP (x, 0)),
4762                                     make_tree (type, XEXP (x, 1)))));
4763
4764     case DIV:
4765       if (TREE_CODE (type) != REAL_TYPE)
4766         t = lang_hooks.types.signed_type (type);
4767       else
4768         t = type;
4769
4770       return fold (convert (type,
4771                             build2 (TRUNC_DIV_EXPR, t,
4772                                     make_tree (t, XEXP (x, 0)),
4773                                     make_tree (t, XEXP (x, 1)))));
4774     case UDIV:
4775       t = lang_hooks.types.unsigned_type (type);
4776       return fold (convert (type,
4777                             build2 (TRUNC_DIV_EXPR, t,
4778                                     make_tree (t, XEXP (x, 0)),
4779                                     make_tree (t, XEXP (x, 1)))));
4780
4781     case SIGN_EXTEND:
4782     case ZERO_EXTEND:
4783       t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
4784                                           GET_CODE (x) == ZERO_EXTEND);
4785       return fold (convert (type, make_tree (t, XEXP (x, 0))));
4786
4787     default:
4788       t = build_decl (VAR_DECL, NULL_TREE, type);
4789
4790       /* If TYPE is a POINTER_TYPE, X might be Pmode with TYPE_MODE being
4791          ptr_mode.  So convert.  */
4792       if (POINTER_TYPE_P (type))
4793         x = convert_memory_address (TYPE_MODE (type), x);
4794
4795       /* Note that we do *not* use SET_DECL_RTL here, because we do not
4796          want set_decl_rtl to go adjusting REG_ATTRS for this temporary.  */
4797       t->decl.rtl = x;
4798
4799       return t;
4800     }
4801 }
4802
4803 /* Check whether the multiplication X * MULT + ADD overflows.
4804    X, MULT and ADD must be CONST_*.
4805    MODE is the machine mode for the computation.
4806    X and MULT must have mode MODE.  ADD may have a different mode.
4807    So can X (defaults to same as MODE).
4808    UNSIGNEDP is nonzero to do unsigned multiplication.  */
4809
4810 bool
4811 const_mult_add_overflow_p (rtx x, rtx mult, rtx add,
4812                            enum machine_mode mode, int unsignedp)
4813 {
4814   tree type, mult_type, add_type, result;
4815
4816   type = lang_hooks.types.type_for_mode (mode, unsignedp);
4817
4818   /* In order to get a proper overflow indication from an unsigned
4819      type, we have to pretend that it's a sizetype.  */
4820   mult_type = type;
4821   if (unsignedp)
4822     {
4823       /* FIXME:It would be nice if we could step directly from this
4824          type to its sizetype equivalent.  */
4825       mult_type = build_distinct_type_copy (type);
4826       TYPE_IS_SIZETYPE (mult_type) = 1;
4827     }
4828
4829   add_type = (GET_MODE (add) == VOIDmode ? mult_type
4830               : lang_hooks.types.type_for_mode (GET_MODE (add), unsignedp));
4831
4832   result = fold (build2 (PLUS_EXPR, mult_type,
4833                          fold (build2 (MULT_EXPR, mult_type,
4834                                        make_tree (mult_type, x),
4835                                        make_tree (mult_type, mult))),
4836                          make_tree (add_type, add)));
4837
4838   return TREE_CONSTANT_OVERFLOW (result);
4839 }
4840
4841 /* Return an rtx representing the value of X * MULT + ADD.
4842    TARGET is a suggestion for where to store the result (an rtx).
4843    MODE is the machine mode for the computation.
4844    X and MULT must have mode MODE.  ADD may have a different mode.
4845    So can X (defaults to same as MODE).
4846    UNSIGNEDP is nonzero to do unsigned multiplication.
4847    This may emit insns.  */
4848
4849 rtx
4850 expand_mult_add (rtx x, rtx target, rtx mult, rtx add, enum machine_mode mode,
4851                  int unsignedp)
4852 {
4853   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
4854   tree add_type = (GET_MODE (add) == VOIDmode
4855                    ? type: lang_hooks.types.type_for_mode (GET_MODE (add),
4856                                                            unsignedp));
4857   tree result =  fold (build2 (PLUS_EXPR, type,
4858                                fold (build2 (MULT_EXPR, type,
4859                                              make_tree (type, x),
4860                                              make_tree (type, mult))),
4861                                make_tree (add_type, add)));
4862
4863   return expand_expr (result, target, VOIDmode, 0);
4864 }
4865 \f
4866 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
4867    and returning TARGET.
4868
4869    If TARGET is 0, a pseudo-register or constant is returned.  */
4870
4871 rtx
4872 expand_and (enum machine_mode mode, rtx op0, rtx op1, rtx target)
4873 {
4874   rtx tem = 0;
4875
4876   if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
4877     tem = simplify_binary_operation (AND, mode, op0, op1);
4878   if (tem == 0)
4879     tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
4880
4881   if (target == 0)
4882     target = tem;
4883   else if (tem != target)
4884     emit_move_insn (target, tem);
4885   return target;
4886 }
4887 \f
4888 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
4889    and storing in TARGET.  Normally return TARGET.
4890    Return 0 if that cannot be done.
4891
4892    MODE is the mode to use for OP0 and OP1 should they be CONST_INTs.  If
4893    it is VOIDmode, they cannot both be CONST_INT.
4894
4895    UNSIGNEDP is for the case where we have to widen the operands
4896    to perform the operation.  It says to use zero-extension.
4897
4898    NORMALIZEP is 1 if we should convert the result to be either zero
4899    or one.  Normalize is -1 if we should convert the result to be
4900    either zero or -1.  If NORMALIZEP is zero, the result will be left
4901    "raw" out of the scc insn.  */
4902
4903 rtx
4904 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
4905                  enum machine_mode mode, int unsignedp, int normalizep)
4906 {
4907   rtx subtarget;
4908   enum insn_code icode;
4909   enum machine_mode compare_mode;
4910   enum machine_mode target_mode = GET_MODE (target);
4911   rtx tem;
4912   rtx last = get_last_insn ();
4913   rtx pattern, comparison;
4914
4915   if (unsignedp)
4916     code = unsigned_condition (code);
4917
4918   /* If one operand is constant, make it the second one.  Only do this
4919      if the other operand is not constant as well.  */
4920
4921   if (swap_commutative_operands_p (op0, op1))
4922     {
4923       tem = op0;
4924       op0 = op1;
4925       op1 = tem;
4926       code = swap_condition (code);
4927     }
4928
4929   if (mode == VOIDmode)
4930     mode = GET_MODE (op0);
4931
4932   /* For some comparisons with 1 and -1, we can convert this to
4933      comparisons with zero.  This will often produce more opportunities for
4934      store-flag insns.  */
4935
4936   switch (code)
4937     {
4938     case LT:
4939       if (op1 == const1_rtx)
4940         op1 = const0_rtx, code = LE;
4941       break;
4942     case LE:
4943       if (op1 == constm1_rtx)
4944         op1 = const0_rtx, code = LT;
4945       break;
4946     case GE:
4947       if (op1 == const1_rtx)
4948         op1 = const0_rtx, code = GT;
4949       break;
4950     case GT:
4951       if (op1 == constm1_rtx)
4952         op1 = const0_rtx, code = GE;
4953       break;
4954     case GEU:
4955       if (op1 == const1_rtx)
4956         op1 = const0_rtx, code = NE;
4957       break;
4958     case LTU:
4959       if (op1 == const1_rtx)
4960         op1 = const0_rtx, code = EQ;
4961       break;
4962     default:
4963       break;
4964     }
4965
4966   /* If we are comparing a double-word integer with zero or -1, we can
4967      convert the comparison into one involving a single word.  */
4968   if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
4969       && GET_MODE_CLASS (mode) == MODE_INT
4970       && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
4971     {
4972       if ((code == EQ || code == NE)
4973           && (op1 == const0_rtx || op1 == constm1_rtx))
4974         {
4975           rtx op00, op01, op0both;
4976
4977           /* Do a logical OR or AND of the two words and compare the result.  */
4978           op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
4979           op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
4980           op0both = expand_binop (word_mode,
4981                                   op1 == const0_rtx ? ior_optab : and_optab,
4982                                   op00, op01, NULL_RTX, unsignedp, OPTAB_DIRECT);
4983
4984           if (op0both != 0)
4985             return emit_store_flag (target, code, op0both, op1, word_mode,
4986                                     unsignedp, normalizep);
4987         }
4988       else if ((code == LT || code == GE) && op1 == const0_rtx)
4989         {
4990           rtx op0h;
4991
4992           /* If testing the sign bit, can just test on high word.  */
4993           op0h = simplify_gen_subreg (word_mode, op0, mode,
4994                                       subreg_highpart_offset (word_mode, mode));
4995           return emit_store_flag (target, code, op0h, op1, word_mode,
4996                                   unsignedp, normalizep);
4997         }
4998     }
4999
5000   /* From now on, we won't change CODE, so set ICODE now.  */
5001   icode = setcc_gen_code[(int) code];
5002
5003   /* If this is A < 0 or A >= 0, we can do this by taking the ones
5004      complement of A (for GE) and shifting the sign bit to the low bit.  */
5005   if (op1 == const0_rtx && (code == LT || code == GE)
5006       && GET_MODE_CLASS (mode) == MODE_INT
5007       && (normalizep || STORE_FLAG_VALUE == 1
5008           || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5009               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5010                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
5011     {
5012       subtarget = target;
5013
5014       /* If the result is to be wider than OP0, it is best to convert it
5015          first.  If it is to be narrower, it is *incorrect* to convert it
5016          first.  */
5017       if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
5018         {
5019           op0 = convert_modes (target_mode, mode, op0, 0);
5020           mode = target_mode;
5021         }
5022
5023       if (target_mode != mode)
5024         subtarget = 0;
5025
5026       if (code == GE)
5027         op0 = expand_unop (mode, one_cmpl_optab, op0,
5028                            ((STORE_FLAG_VALUE == 1 || normalizep)
5029                             ? 0 : subtarget), 0);
5030
5031       if (STORE_FLAG_VALUE == 1 || normalizep)
5032         /* If we are supposed to produce a 0/1 value, we want to do
5033            a logical shift from the sign bit to the low-order bit; for
5034            a -1/0 value, we do an arithmetic shift.  */
5035         op0 = expand_shift (RSHIFT_EXPR, mode, op0,
5036                             size_int (GET_MODE_BITSIZE (mode) - 1),
5037                             subtarget, normalizep != -1);
5038
5039       if (mode != target_mode)
5040         op0 = convert_modes (target_mode, mode, op0, 0);
5041
5042       return op0;
5043     }
5044
5045   if (icode != CODE_FOR_nothing)
5046     {
5047       insn_operand_predicate_fn pred;
5048
5049       /* We think we may be able to do this with a scc insn.  Emit the
5050          comparison and then the scc insn.  */
5051
5052       do_pending_stack_adjust ();
5053       last = get_last_insn ();
5054
5055       comparison
5056         = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX);
5057       if (CONSTANT_P (comparison))
5058         {
5059           switch (GET_CODE (comparison))
5060             {
5061             case CONST_INT:
5062               if (comparison == const0_rtx)
5063                 return const0_rtx;
5064               break;
5065               
5066 #ifdef FLOAT_STORE_FLAG_VALUE
5067             case CONST_DOUBLE:
5068               if (comparison == CONST0_RTX (GET_MODE (comparison)))
5069                 return const0_rtx;
5070               break;
5071 #endif
5072             default:
5073               gcc_unreachable ();
5074             }
5075           
5076           if (normalizep == 1)
5077             return const1_rtx;
5078           if (normalizep == -1)
5079             return constm1_rtx;
5080           return const_true_rtx;
5081         }
5082
5083       /* The code of COMPARISON may not match CODE if compare_from_rtx
5084          decided to swap its operands and reverse the original code.
5085
5086          We know that compare_from_rtx returns either a CONST_INT or
5087          a new comparison code, so it is safe to just extract the
5088          code from COMPARISON.  */
5089       code = GET_CODE (comparison);
5090
5091       /* Get a reference to the target in the proper mode for this insn.  */
5092       compare_mode = insn_data[(int) icode].operand[0].mode;
5093       subtarget = target;
5094       pred = insn_data[(int) icode].operand[0].predicate;
5095       if (optimize || ! (*pred) (subtarget, compare_mode))
5096         subtarget = gen_reg_rtx (compare_mode);
5097
5098       pattern = GEN_FCN (icode) (subtarget);
5099       if (pattern)
5100         {
5101           emit_insn (pattern);
5102
5103           /* If we are converting to a wider mode, first convert to
5104              TARGET_MODE, then normalize.  This produces better combining
5105              opportunities on machines that have a SIGN_EXTRACT when we are
5106              testing a single bit.  This mostly benefits the 68k.
5107
5108              If STORE_FLAG_VALUE does not have the sign bit set when
5109              interpreted in COMPARE_MODE, we can do this conversion as
5110              unsigned, which is usually more efficient.  */
5111           if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
5112             {
5113               convert_move (target, subtarget,
5114                             (GET_MODE_BITSIZE (compare_mode)
5115                              <= HOST_BITS_PER_WIDE_INT)
5116                             && 0 == (STORE_FLAG_VALUE
5117                                      & ((HOST_WIDE_INT) 1
5118                                         << (GET_MODE_BITSIZE (compare_mode) -1))));
5119               op0 = target;
5120               compare_mode = target_mode;
5121             }
5122           else
5123             op0 = subtarget;
5124
5125           /* If we want to keep subexpressions around, don't reuse our
5126              last target.  */
5127
5128           if (optimize)
5129             subtarget = 0;
5130
5131           /* Now normalize to the proper value in COMPARE_MODE.  Sometimes
5132              we don't have to do anything.  */
5133           if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
5134             ;
5135           /* STORE_FLAG_VALUE might be the most negative number, so write
5136              the comparison this way to avoid a compiler-time warning.  */
5137           else if (- normalizep == STORE_FLAG_VALUE)
5138             op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
5139
5140           /* We don't want to use STORE_FLAG_VALUE < 0 below since this
5141              makes it hard to use a value of just the sign bit due to
5142              ANSI integer constant typing rules.  */
5143           else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
5144                    && (STORE_FLAG_VALUE
5145                        & ((HOST_WIDE_INT) 1
5146                           << (GET_MODE_BITSIZE (compare_mode) - 1))))
5147             op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
5148                                 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
5149                                 subtarget, normalizep == 1);
5150           else
5151             {
5152               gcc_assert (STORE_FLAG_VALUE & 1);
5153               
5154               op0 = expand_and (compare_mode, op0, const1_rtx, subtarget);
5155               if (normalizep == -1)
5156                 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
5157             }
5158
5159           /* If we were converting to a smaller mode, do the
5160              conversion now.  */
5161           if (target_mode != compare_mode)
5162             {
5163               convert_move (target, op0, 0);
5164               return target;
5165             }
5166           else
5167             return op0;
5168         }
5169     }
5170
5171   delete_insns_since (last);
5172
5173   /* If optimizing, use different pseudo registers for each insn, instead
5174      of reusing the same pseudo.  This leads to better CSE, but slows
5175      down the compiler, since there are more pseudos */
5176   subtarget = (!optimize
5177                && (target_mode == mode)) ? target : NULL_RTX;
5178
5179   /* If we reached here, we can't do this with a scc insn.  However, there
5180      are some comparisons that can be done directly.  For example, if
5181      this is an equality comparison of integers, we can try to exclusive-or
5182      (or subtract) the two operands and use a recursive call to try the
5183      comparison with zero.  Don't do any of these cases if branches are
5184      very cheap.  */
5185
5186   if (BRANCH_COST > 0
5187       && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
5188       && op1 != const0_rtx)
5189     {
5190       tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5191                           OPTAB_WIDEN);
5192
5193       if (tem == 0)
5194         tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5195                             OPTAB_WIDEN);
5196       if (tem != 0)
5197         tem = emit_store_flag (target, code, tem, const0_rtx,
5198                                mode, unsignedp, normalizep);
5199       if (tem == 0)
5200         delete_insns_since (last);
5201       return tem;
5202     }
5203
5204   /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5205      the constant zero.  Reject all other comparisons at this point.  Only
5206      do LE and GT if branches are expensive since they are expensive on
5207      2-operand machines.  */
5208
5209   if (BRANCH_COST == 0
5210       || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
5211       || (code != EQ && code != NE
5212           && (BRANCH_COST <= 1 || (code != LE && code != GT))))
5213     return 0;
5214
5215   /* See what we need to return.  We can only return a 1, -1, or the
5216      sign bit.  */
5217
5218   if (normalizep == 0)
5219     {
5220       if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5221         normalizep = STORE_FLAG_VALUE;
5222
5223       else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5224                && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5225                    == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
5226         ;
5227       else
5228         return 0;
5229     }
5230
5231   /* Try to put the result of the comparison in the sign bit.  Assume we can't
5232      do the necessary operation below.  */
5233
5234   tem = 0;
5235
5236   /* To see if A <= 0, compute (A | (A - 1)).  A <= 0 iff that result has
5237      the sign bit set.  */
5238
5239   if (code == LE)
5240     {
5241       /* This is destructive, so SUBTARGET can't be OP0.  */
5242       if (rtx_equal_p (subtarget, op0))
5243         subtarget = 0;
5244
5245       tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5246                           OPTAB_WIDEN);
5247       if (tem)
5248         tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5249                             OPTAB_WIDEN);
5250     }
5251
5252   /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5253      number of bits in the mode of OP0, minus one.  */
5254
5255   if (code == GT)
5256     {
5257       if (rtx_equal_p (subtarget, op0))
5258         subtarget = 0;
5259
5260       tem = expand_shift (RSHIFT_EXPR, mode, op0,
5261                           size_int (GET_MODE_BITSIZE (mode) - 1),
5262                           subtarget, 0);
5263       tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5264                           OPTAB_WIDEN);
5265     }
5266
5267   if (code == EQ || code == NE)
5268     {
5269       /* For EQ or NE, one way to do the comparison is to apply an operation
5270          that converts the operand into a positive number if it is nonzero
5271          or zero if it was originally zero.  Then, for EQ, we subtract 1 and
5272          for NE we negate.  This puts the result in the sign bit.  Then we
5273          normalize with a shift, if needed.
5274
5275          Two operations that can do the above actions are ABS and FFS, so try
5276          them.  If that doesn't work, and MODE is smaller than a full word,
5277          we can use zero-extension to the wider mode (an unsigned conversion)
5278          as the operation.  */
5279
5280       /* Note that ABS doesn't yield a positive number for INT_MIN, but
5281          that is compensated by the subsequent overflow when subtracting
5282          one / negating.  */
5283
5284       if (abs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5285         tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5286       else if (ffs_optab->handlers[mode].insn_code != CODE_FOR_nothing)
5287         tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5288       else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5289         {
5290           tem = convert_modes (word_mode, mode, op0, 1);
5291           mode = word_mode;
5292         }
5293
5294       if (tem != 0)
5295         {
5296           if (code == EQ)
5297             tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5298                                 0, OPTAB_WIDEN);
5299           else
5300             tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5301         }
5302
5303       /* If we couldn't do it that way, for NE we can "or" the two's complement
5304          of the value with itself.  For EQ, we take the one's complement of
5305          that "or", which is an extra insn, so we only handle EQ if branches
5306          are expensive.  */
5307
5308       if (tem == 0 && (code == NE || BRANCH_COST > 1))
5309         {
5310           if (rtx_equal_p (subtarget, op0))
5311             subtarget = 0;
5312
5313           tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5314           tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5315                               OPTAB_WIDEN);
5316
5317           if (tem && code == EQ)
5318             tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5319         }
5320     }
5321
5322   if (tem && normalizep)
5323     tem = expand_shift (RSHIFT_EXPR, mode, tem,
5324                         size_int (GET_MODE_BITSIZE (mode) - 1),
5325                         subtarget, normalizep == 1);
5326
5327   if (tem)
5328     {
5329       if (GET_MODE (tem) != target_mode)
5330         {
5331           convert_move (target, tem, 0);
5332           tem = target;
5333         }
5334       else if (!subtarget)
5335         {
5336           emit_move_insn (target, tem);
5337           tem = target;
5338         }
5339     }
5340   else
5341     delete_insns_since (last);
5342
5343   return tem;
5344 }
5345
5346 /* Like emit_store_flag, but always succeeds.  */
5347
5348 rtx
5349 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5350                        enum machine_mode mode, int unsignedp, int normalizep)
5351 {
5352   rtx tem, label;
5353
5354   /* First see if emit_store_flag can do the job.  */
5355   tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5356   if (tem != 0)
5357     return tem;
5358
5359   if (normalizep == 0)
5360     normalizep = 1;
5361
5362   /* If this failed, we have to do this with set/compare/jump/set code.  */
5363
5364   if (!REG_P (target)
5365       || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5366     target = gen_reg_rtx (GET_MODE (target));
5367
5368   emit_move_insn (target, const1_rtx);
5369   label = gen_label_rtx ();
5370   do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
5371                            NULL_RTX, label);
5372
5373   emit_move_insn (target, const0_rtx);
5374   emit_label (label);
5375
5376   return target;
5377 }
5378 \f
5379 /* Perform possibly multi-word comparison and conditional jump to LABEL
5380    if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE
5381
5382    The algorithm is based on the code in expr.c:do_jump.
5383
5384    Note that this does not perform a general comparison.  Only variants
5385    generated within expmed.c are correctly handled, others abort (but could
5386    be handled if needed).  */
5387
5388 static void
5389 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, enum machine_mode mode,
5390                  rtx label)
5391 {
5392   /* If this mode is an integer too wide to compare properly,
5393      compare word by word.  Rely on cse to optimize constant cases.  */
5394
5395   if (GET_MODE_CLASS (mode) == MODE_INT
5396       && ! can_compare_p (op, mode, ccp_jump))
5397     {
5398       rtx label2 = gen_label_rtx ();
5399
5400       switch (op)
5401         {
5402         case LTU:
5403           do_jump_by_parts_greater_rtx (mode, 1, arg2, arg1, label2, label);
5404           break;
5405
5406         case LEU:
5407           do_jump_by_parts_greater_rtx (mode, 1, arg1, arg2, label, label2);
5408           break;
5409
5410         case LT:
5411           do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label2, label);
5412           break;
5413
5414         case GT:
5415           do_jump_by_parts_greater_rtx (mode, 0, arg1, arg2, label2, label);
5416           break;
5417
5418         case GE:
5419           do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label, label2);
5420           break;
5421
5422           /* do_jump_by_parts_equality_rtx compares with zero.  Luckily
5423              that's the only equality operations we do */
5424         case EQ:
5425           gcc_assert (arg2 == const0_rtx && mode == GET_MODE(arg1));
5426           do_jump_by_parts_equality_rtx (arg1, label2, label);
5427           break;
5428
5429         case NE:
5430           gcc_assert (arg2 == const0_rtx && mode == GET_MODE(arg1));
5431           do_jump_by_parts_equality_rtx (arg1, label, label2);
5432           break;
5433
5434         default:
5435           gcc_unreachable ();
5436         }
5437
5438       emit_label (label2);
5439     }
5440   else
5441     emit_cmp_and_jump_insns (arg1, arg2, op, NULL_RTX, mode, 0, label);
5442 }