OSDN Git Service

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