OSDN Git Service

9d2b28b9af8443cb92ca13cc0ab48f3027dd2681
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23    Portable Optimizer, but redone to work on our list-structured
24    representation for RTL instead of their string representation.
25
26    The LOG_LINKS of each insn identify the most recent assignment
27    to each REG used in the insn.  It is a list of previous insns,
28    each of which contains a SET for a REG that is used in this insn
29    and not used or set in between.  LOG_LINKs never cross basic blocks.
30    They were set up by the preceding pass (lifetime analysis).
31
32    We try to combine each pair of insns joined by a logical link.
33    We also try to combine triples of insns A, B and C when
34    C has a link back to B and B has a link back to A.
35
36    LOG_LINKS does not have links for use of the CC0.  They don't
37    need to, because the insn that sets the CC0 is always immediately
38    before the insn that tests it.  So we always regard a branch
39    insn as having a logical link to the preceding insn.  The same is true
40    for an insn explicitly using CC0.
41
42    We check (with use_crosses_set_p) to avoid combining in such a way
43    as to move a computation to a place where its value would be different.
44
45    Combination is done by mathematically substituting the previous
46    insn(s) values for the regs they set into the expressions in
47    the later insns that refer to these regs.  If the result is a valid insn
48    for our target machine, according to the machine description,
49    we install it, delete the earlier insns, and update the data flow
50    information (LOG_LINKS and REG_NOTES) for what we did.
51
52    There are a few exceptions where the dataflow information created by
53    flow.c aren't completely updated:
54
55    - reg_live_length is not updated
56    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
57      removed because there is no way to know which register it was
58      linking
59
60    To simplify substitution, we combine only when the earlier insn(s)
61    consist of only a single assignment.  To simplify updating afterward,
62    we never combine when a subroutine call appears in the middle.
63
64    Since we do not represent assignments to CC0 explicitly except when that
65    is all an insn does, there is no LOG_LINKS entry in an insn that uses
66    the condition code for the insn that set the condition code.
67    Fortunately, these two insns must be consecutive.
68    Therefore, every JUMP_INSN is taken to have an implicit logical link
69    to the preceding insn.  This is not quite right, since non-jumps can
70    also use the condition code; but in practice such insns would not
71    combine anyway.  */
72
73 #include "config.h"
74 #include "system.h"
75 #include "coretypes.h"
76 #include "tm.h"
77 #include "rtl.h"
78 #include "tree.h"
79 #include "tm_p.h"
80 #include "flags.h"
81 #include "regs.h"
82 #include "hard-reg-set.h"
83 #include "basic-block.h"
84 #include "insn-config.h"
85 #include "function.h"
86 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
87 #include "expr.h"
88 #include "insn-attr.h"
89 #include "recog.h"
90 #include "real.h"
91 #include "toplev.h"
92 #include "target.h"
93 #include "optabs.h"
94 #include "insn-codes.h"
95 #include "rtlhooks-def.h"
96 /* Include output.h for dump_file.  */
97 #include "output.h"
98 #include "params.h"
99 #include "timevar.h"
100 #include "tree-pass.h"
101
102 /* Number of attempts to combine instructions in this function.  */
103
104 static int combine_attempts;
105
106 /* Number of attempts that got as far as substitution in this function.  */
107
108 static int combine_merges;
109
110 /* Number of instructions combined with added SETs in this function.  */
111
112 static int combine_extras;
113
114 /* Number of instructions combined in this function.  */
115
116 static int combine_successes;
117
118 /* Totals over entire compilation.  */
119
120 static int total_attempts, total_merges, total_extras, total_successes;
121
122 \f
123 /* Vector mapping INSN_UIDs to cuids.
124    The cuids are like uids but increase monotonically always.
125    Combine always uses cuids so that it can compare them.
126    But actually renumbering the uids, which we used to do,
127    proves to be a bad idea because it makes it hard to compare
128    the dumps produced by earlier passes with those from later passes.  */
129
130 static int *uid_cuid;
131 static int max_uid_cuid;
132
133 /* Get the cuid of an insn.  */
134
135 #define INSN_CUID(INSN) \
136 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
137
138 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
139    BITS_PER_WORD would invoke undefined behavior.  Work around it.  */
140
141 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
142   (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
143
144 /* Maximum register number, which is the size of the tables below.  */
145
146 static unsigned int combine_max_regno;
147
148 struct reg_stat {
149   /* Record last point of death of (hard or pseudo) register n.  */
150   rtx                           last_death;
151
152   /* Record last point of modification of (hard or pseudo) register n.  */
153   rtx                           last_set;
154
155   /* The next group of fields allows the recording of the last value assigned
156      to (hard or pseudo) register n.  We use this information to see if an
157      operation being processed is redundant given a prior operation performed
158      on the register.  For example, an `and' with a constant is redundant if
159      all the zero bits are already known to be turned off.
160
161      We use an approach similar to that used by cse, but change it in the
162      following ways:
163
164      (1) We do not want to reinitialize at each label.
165      (2) It is useful, but not critical, to know the actual value assigned
166          to a register.  Often just its form is helpful.
167
168      Therefore, we maintain the following fields:
169
170      last_set_value             the last value assigned
171      last_set_label             records the value of label_tick when the
172                                 register was assigned
173      last_set_table_tick        records the value of label_tick when a
174                                 value using the register is assigned
175      last_set_invalid           set to nonzero when it is not valid
176                                 to use the value of this register in some
177                                 register's value
178
179      To understand the usage of these tables, it is important to understand
180      the distinction between the value in last_set_value being valid and
181      the register being validly contained in some other expression in the
182      table.
183
184      (The next two parameters are out of date).
185
186      reg_stat[i].last_set_value is valid if it is nonzero, and either
187      reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
188
189      Register I may validly appear in any expression returned for the value
190      of another register if reg_n_sets[i] is 1.  It may also appear in the
191      value for register J if reg_stat[j].last_set_invalid is zero, or
192      reg_stat[i].last_set_label < reg_stat[j].last_set_label.
193
194      If an expression is found in the table containing a register which may
195      not validly appear in an expression, the register is replaced by
196      something that won't match, (clobber (const_int 0)).  */
197
198   /* Record last value assigned to (hard or pseudo) register n.  */
199
200   rtx                           last_set_value;
201
202   /* Record the value of label_tick when an expression involving register n
203      is placed in last_set_value.  */
204
205   int                           last_set_table_tick;
206
207   /* Record the value of label_tick when the value for register n is placed in
208      last_set_value.  */
209
210   int                           last_set_label;
211
212   /* These fields are maintained in parallel with last_set_value and are
213      used to store the mode in which the register was last set, the bits
214      that were known to be zero when it was last set, and the number of
215      sign bits copies it was known to have when it was last set.  */
216
217   unsigned HOST_WIDE_INT        last_set_nonzero_bits;
218   char                          last_set_sign_bit_copies;
219   ENUM_BITFIELD(machine_mode)   last_set_mode : 8; 
220
221   /* Set nonzero if references to register n in expressions should not be
222      used.  last_set_invalid is set nonzero when this register is being
223      assigned to and last_set_table_tick == label_tick.  */
224
225   char                          last_set_invalid;
226
227   /* Some registers that are set more than once and used in more than one
228      basic block are nevertheless always set in similar ways.  For example,
229      a QImode register may be loaded from memory in two places on a machine
230      where byte loads zero extend.
231
232      We record in the following fields if a register has some leading bits
233      that are always equal to the sign bit, and what we know about the
234      nonzero bits of a register, specifically which bits are known to be
235      zero.
236
237      If an entry is zero, it means that we don't know anything special.  */
238
239   unsigned char                 sign_bit_copies;
240
241   unsigned HOST_WIDE_INT        nonzero_bits;
242 };
243
244 static struct reg_stat *reg_stat;
245
246 /* Record the cuid of the last insn that invalidated memory
247    (anything that writes memory, and subroutine calls, but not pushes).  */
248
249 static int mem_last_set;
250
251 /* Record the cuid of the last CALL_INSN
252    so we can tell whether a potential combination crosses any calls.  */
253
254 static int last_call_cuid;
255
256 /* When `subst' is called, this is the insn that is being modified
257    (by combining in a previous insn).  The PATTERN of this insn
258    is still the old pattern partially modified and it should not be
259    looked at, but this may be used to examine the successors of the insn
260    to judge whether a simplification is valid.  */
261
262 static rtx subst_insn;
263
264 /* This is the lowest CUID that `subst' is currently dealing with.
265    get_last_value will not return a value if the register was set at or
266    after this CUID.  If not for this mechanism, we could get confused if
267    I2 or I1 in try_combine were an insn that used the old value of a register
268    to obtain a new value.  In that case, we might erroneously get the
269    new value of the register when we wanted the old one.  */
270
271 static int subst_low_cuid;
272
273 /* This contains any hard registers that are used in newpat; reg_dead_at_p
274    must consider all these registers to be always live.  */
275
276 static HARD_REG_SET newpat_used_regs;
277
278 /* This is an insn to which a LOG_LINKS entry has been added.  If this
279    insn is the earlier than I2 or I3, combine should rescan starting at
280    that location.  */
281
282 static rtx added_links_insn;
283
284 /* Basic block in which we are performing combines.  */
285 static basic_block this_basic_block;
286
287 /* A bitmap indicating which blocks had registers go dead at entry.
288    After combine, we'll need to re-do global life analysis with
289    those blocks as starting points.  */
290 static sbitmap refresh_blocks;
291 \f
292 /* The following array records the insn_rtx_cost for every insn
293    in the instruction stream.  */
294
295 static int *uid_insn_cost;
296
297 /* Length of the currently allocated uid_insn_cost array.  */
298
299 static int last_insn_cost;
300
301 /* Incremented for each label.  */
302
303 static int label_tick;
304
305 /* Mode used to compute significance in reg_stat[].nonzero_bits.  It is the
306    largest integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
307
308 static enum machine_mode nonzero_bits_mode;
309
310 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
311    be safely used.  It is zero while computing them and after combine has
312    completed.  This former test prevents propagating values based on
313    previously set values, which can be incorrect if a variable is modified
314    in a loop.  */
315
316 static int nonzero_sign_valid;
317
318 \f
319 /* Record one modification to rtl structure
320    to be undone by storing old_contents into *where.
321    is_int is 1 if the contents are an int.  */
322
323 struct undo
324 {
325   struct undo *next;
326   int is_int;
327   union {rtx r; int i;} old_contents;
328   union {rtx *r; int *i;} where;
329 };
330
331 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
332    num_undo says how many are currently recorded.
333
334    other_insn is nonzero if we have modified some other insn in the process
335    of working on subst_insn.  It must be verified too.  */
336
337 struct undobuf
338 {
339   struct undo *undos;
340   struct undo *frees;
341   rtx other_insn;
342 };
343
344 static struct undobuf undobuf;
345
346 /* Number of times the pseudo being substituted for
347    was found and replaced.  */
348
349 static int n_occurrences;
350
351 static rtx reg_nonzero_bits_for_combine (rtx, enum machine_mode, rtx,
352                                          enum machine_mode,
353                                          unsigned HOST_WIDE_INT,
354                                          unsigned HOST_WIDE_INT *);
355 static rtx reg_num_sign_bit_copies_for_combine (rtx, enum machine_mode, rtx,
356                                                 enum machine_mode,
357                                                 unsigned int, unsigned int *);
358 static void do_SUBST (rtx *, rtx);
359 static void do_SUBST_INT (int *, int);
360 static void init_reg_last (void);
361 static void setup_incoming_promotions (void);
362 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
363 static int cant_combine_insn_p (rtx);
364 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
365 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
366 static int contains_muldiv (rtx);
367 static rtx try_combine (rtx, rtx, rtx, int *);
368 static void undo_all (void);
369 static void undo_commit (void);
370 static rtx *find_split_point (rtx *, rtx);
371 static rtx subst (rtx, rtx, rtx, int, int);
372 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
373 static rtx simplify_if_then_else (rtx);
374 static rtx simplify_set (rtx);
375 static rtx simplify_logical (rtx);
376 static rtx expand_compound_operation (rtx);
377 static rtx expand_field_assignment (rtx);
378 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
379                             rtx, unsigned HOST_WIDE_INT, int, int, int);
380 static rtx extract_left_shift (rtx, int);
381 static rtx make_compound_operation (rtx, enum rtx_code);
382 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
383                               unsigned HOST_WIDE_INT *);
384 static rtx force_to_mode (rtx, enum machine_mode,
385                           unsigned HOST_WIDE_INT, rtx, int);
386 static rtx if_then_else_cond (rtx, rtx *, rtx *);
387 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
388 static int rtx_equal_for_field_assignment_p (rtx, rtx);
389 static rtx make_field_assignment (rtx);
390 static rtx apply_distributive_law (rtx);
391 static rtx distribute_and_simplify_rtx (rtx, int);
392 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
393                                    unsigned HOST_WIDE_INT);
394 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
395                             HOST_WIDE_INT, enum machine_mode, int *);
396 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
397                                  int);
398 static int recog_for_combine (rtx *, rtx, rtx *);
399 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
400 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
401 static void update_table_tick (rtx);
402 static void record_value_for_reg (rtx, rtx, rtx);
403 static void check_promoted_subreg (rtx, rtx);
404 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
405 static void record_dead_and_set_regs (rtx);
406 static int get_last_value_validate (rtx *, rtx, int, int);
407 static rtx get_last_value (rtx);
408 static int use_crosses_set_p (rtx, int);
409 static void reg_dead_at_p_1 (rtx, rtx, void *);
410 static int reg_dead_at_p (rtx, rtx);
411 static void move_deaths (rtx, rtx, int, rtx, rtx *);
412 static int reg_bitfield_target_p (rtx, rtx);
413 static void distribute_notes (rtx, rtx, rtx, rtx);
414 static void distribute_links (rtx);
415 static void mark_used_regs_combine (rtx);
416 static int insn_cuid (rtx);
417 static void record_promoted_value (rtx, rtx);
418 static int unmentioned_reg_p_1 (rtx *, void *);
419 static bool unmentioned_reg_p (rtx, rtx);
420 \f
421
422 /* It is not safe to use ordinary gen_lowpart in combine.
423    See comments in gen_lowpart_for_combine.  */
424 #undef RTL_HOOKS_GEN_LOWPART
425 #define RTL_HOOKS_GEN_LOWPART              gen_lowpart_for_combine
426
427 /* Our implementation of gen_lowpart never emits a new pseudo.  */
428 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
429 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT      gen_lowpart_for_combine
430
431 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
432 #define RTL_HOOKS_REG_NONZERO_REG_BITS     reg_nonzero_bits_for_combine
433
434 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
435 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES  reg_num_sign_bit_copies_for_combine
436
437 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
438
439 \f
440 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
441    insn.  The substitution can be undone by undo_all.  If INTO is already
442    set to NEWVAL, do not record this change.  Because computing NEWVAL might
443    also call SUBST, we have to compute it before we put anything into
444    the undo table.  */
445
446 static void
447 do_SUBST (rtx *into, rtx newval)
448 {
449   struct undo *buf;
450   rtx oldval = *into;
451
452   if (oldval == newval)
453     return;
454
455   /* We'd like to catch as many invalid transformations here as
456      possible.  Unfortunately, there are way too many mode changes
457      that are perfectly valid, so we'd waste too much effort for
458      little gain doing the checks here.  Focus on catching invalid
459      transformations involving integer constants.  */
460   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
461       && GET_CODE (newval) == CONST_INT)
462     {
463       /* Sanity check that we're replacing oldval with a CONST_INT
464          that is a valid sign-extension for the original mode.  */
465       gcc_assert (INTVAL (newval)
466                   == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
467
468       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
469          CONST_INT is not valid, because after the replacement, the
470          original mode would be gone.  Unfortunately, we can't tell
471          when do_SUBST is called to replace the operand thereof, so we
472          perform this test on oldval instead, checking whether an
473          invalid replacement took place before we got here.  */
474       gcc_assert (!(GET_CODE (oldval) == SUBREG
475                     && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
476       gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
477                     && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
478     }
479
480   if (undobuf.frees)
481     buf = undobuf.frees, undobuf.frees = buf->next;
482   else
483     buf = xmalloc (sizeof (struct undo));
484
485   buf->is_int = 0;
486   buf->where.r = into;
487   buf->old_contents.r = oldval;
488   *into = newval;
489
490   buf->next = undobuf.undos, undobuf.undos = buf;
491 }
492
493 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
494
495 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
496    for the value of a HOST_WIDE_INT value (including CONST_INT) is
497    not safe.  */
498
499 static void
500 do_SUBST_INT (int *into, int newval)
501 {
502   struct undo *buf;
503   int oldval = *into;
504
505   if (oldval == newval)
506     return;
507
508   if (undobuf.frees)
509     buf = undobuf.frees, undobuf.frees = buf->next;
510   else
511     buf = xmalloc (sizeof (struct undo));
512
513   buf->is_int = 1;
514   buf->where.i = into;
515   buf->old_contents.i = oldval;
516   *into = newval;
517
518   buf->next = undobuf.undos, undobuf.undos = buf;
519 }
520
521 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
522 \f
523 /* Subroutine of try_combine.  Determine whether the combine replacement
524    patterns NEWPAT and NEWI2PAT are cheaper according to insn_rtx_cost
525    that the original instruction sequence I1, I2 and I3.  Note that I1
526    and/or NEWI2PAT may be NULL_RTX.  This function returns false, if the
527    costs of all instructions can be estimated, and the replacements are
528    more expensive than the original sequence.  */
529
530 static bool
531 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat)
532 {
533   int i1_cost, i2_cost, i3_cost;
534   int new_i2_cost, new_i3_cost;
535   int old_cost, new_cost;
536
537   /* Lookup the original insn_rtx_costs.  */
538   i2_cost = INSN_UID (i2) <= last_insn_cost
539             ? uid_insn_cost[INSN_UID (i2)] : 0;
540   i3_cost = INSN_UID (i3) <= last_insn_cost
541             ? uid_insn_cost[INSN_UID (i3)] : 0;
542
543   if (i1)
544     {
545       i1_cost = INSN_UID (i1) <= last_insn_cost
546                 ? uid_insn_cost[INSN_UID (i1)] : 0;
547       old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
548                  ? i1_cost + i2_cost + i3_cost : 0;
549     }
550   else
551     {
552       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
553       i1_cost = 0;
554     }
555
556   /* Calculate the replacement insn_rtx_costs.  */
557   new_i3_cost = insn_rtx_cost (newpat);
558   if (newi2pat)
559     {
560       new_i2_cost = insn_rtx_cost (newi2pat);
561       new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
562                  ? new_i2_cost + new_i3_cost : 0;
563     }
564   else
565     {
566       new_cost = new_i3_cost;
567       new_i2_cost = 0;
568     }
569
570   if (undobuf.other_insn)
571     {
572       int old_other_cost, new_other_cost;
573
574       old_other_cost = (INSN_UID (undobuf.other_insn) <= last_insn_cost
575                         ? uid_insn_cost[INSN_UID (undobuf.other_insn)] : 0);
576       new_other_cost = insn_rtx_cost (PATTERN (undobuf.other_insn));
577       if (old_other_cost > 0 && new_other_cost > 0)
578         {
579           old_cost += old_other_cost;
580           new_cost += new_other_cost;
581         }
582       else
583         old_cost = 0;
584     }
585
586   /* Disallow this recombination if both new_cost and old_cost are
587      greater than zero, and new_cost is greater than old cost.  */
588   if (old_cost > 0
589       && new_cost > old_cost)
590     {
591       if (dump_file)
592         {
593           if (i1)
594             {
595               fprintf (dump_file,
596                        "rejecting combination of insns %d, %d and %d\n",
597                        INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
598               fprintf (dump_file, "original costs %d + %d + %d = %d\n",
599                        i1_cost, i2_cost, i3_cost, old_cost);
600             }
601           else
602             {
603               fprintf (dump_file,
604                        "rejecting combination of insns %d and %d\n",
605                        INSN_UID (i2), INSN_UID (i3));
606               fprintf (dump_file, "original costs %d + %d = %d\n",
607                        i2_cost, i3_cost, old_cost);
608             }
609
610           if (newi2pat)
611             {
612               fprintf (dump_file, "replacement costs %d + %d = %d\n",
613                        new_i2_cost, new_i3_cost, new_cost);
614             }
615           else
616             fprintf (dump_file, "replacement cost %d\n", new_cost);
617         }
618
619       return false;
620     }
621
622   /* Update the uid_insn_cost array with the replacement costs.  */
623   uid_insn_cost[INSN_UID (i2)] = new_i2_cost;
624   uid_insn_cost[INSN_UID (i3)] = new_i3_cost;
625   if (i1)
626     uid_insn_cost[INSN_UID (i1)] = 0;
627
628   return true;
629 }
630 \f
631 /* Main entry point for combiner.  F is the first insn of the function.
632    NREGS is the first unused pseudo-reg number.
633
634    Return nonzero if the combiner has turned an indirect jump
635    instruction into a direct jump.  */
636 int
637 combine_instructions (rtx f, unsigned int nregs)
638 {
639   rtx insn, next;
640 #ifdef HAVE_cc0
641   rtx prev;
642 #endif
643   int i;
644   unsigned int j;
645   rtx links, nextlinks;
646   sbitmap_iterator sbi;
647
648   int new_direct_jump_p = 0;
649
650   combine_attempts = 0;
651   combine_merges = 0;
652   combine_extras = 0;
653   combine_successes = 0;
654
655   combine_max_regno = nregs;
656
657   rtl_hooks = combine_rtl_hooks;
658
659   reg_stat = xcalloc (nregs, sizeof (struct reg_stat));
660
661   init_recog_no_volatile ();
662
663   /* Compute maximum uid value so uid_cuid can be allocated.  */
664
665   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
666     if (INSN_UID (insn) > i)
667       i = INSN_UID (insn);
668
669   uid_cuid = xmalloc ((i + 1) * sizeof (int));
670   max_uid_cuid = i;
671
672   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
673
674   /* Don't use reg_stat[].nonzero_bits when computing it.  This can cause
675      problems when, for example, we have j <<= 1 in a loop.  */
676
677   nonzero_sign_valid = 0;
678
679   /* Compute the mapping from uids to cuids.
680      Cuids are numbers assigned to insns, like uids,
681      except that cuids increase monotonically through the code.
682
683      Scan all SETs and see if we can deduce anything about what
684      bits are known to be zero for some registers and how many copies
685      of the sign bit are known to exist for those registers.
686
687      Also set any known values so that we can use it while searching
688      for what bits are known to be set.  */
689
690   label_tick = 1;
691
692   setup_incoming_promotions ();
693
694   refresh_blocks = sbitmap_alloc (last_basic_block);
695   sbitmap_zero (refresh_blocks);
696
697   /* Allocate array of current insn_rtx_costs.  */
698   uid_insn_cost = xcalloc (max_uid_cuid + 1, sizeof (int));
699   last_insn_cost = max_uid_cuid;
700
701   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
702     {
703       uid_cuid[INSN_UID (insn)] = ++i;
704       subst_low_cuid = i;
705       subst_insn = insn;
706
707       if (INSN_P (insn))
708         {
709           note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
710                        NULL);
711           record_dead_and_set_regs (insn);
712
713 #ifdef AUTO_INC_DEC
714           for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
715             if (REG_NOTE_KIND (links) == REG_INC)
716               set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
717                                                 NULL);
718 #endif
719
720           /* Record the current insn_rtx_cost of this instruction.  */
721           if (NONJUMP_INSN_P (insn))
722             uid_insn_cost[INSN_UID (insn)] = insn_rtx_cost (PATTERN (insn));
723           if (dump_file)
724             fprintf(dump_file, "insn_cost %d: %d\n",
725                     INSN_UID (insn), uid_insn_cost[INSN_UID (insn)]);
726         }
727
728       if (LABEL_P (insn))
729         label_tick++;
730     }
731
732   nonzero_sign_valid = 1;
733
734   /* Now scan all the insns in forward order.  */
735
736   label_tick = 1;
737   last_call_cuid = 0;
738   mem_last_set = 0;
739   init_reg_last ();
740   setup_incoming_promotions ();
741
742   FOR_EACH_BB (this_basic_block)
743     {
744       for (insn = BB_HEAD (this_basic_block);
745            insn != NEXT_INSN (BB_END (this_basic_block));
746            insn = next ? next : NEXT_INSN (insn))
747         {
748           next = 0;
749
750           if (LABEL_P (insn))
751             label_tick++;
752
753           else if (INSN_P (insn))
754             {
755               /* See if we know about function return values before this
756                  insn based upon SUBREG flags.  */
757               check_promoted_subreg (insn, PATTERN (insn));
758
759               /* Try this insn with each insn it links back to.  */
760
761               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
762                 if ((next = try_combine (insn, XEXP (links, 0),
763                                          NULL_RTX, &new_direct_jump_p)) != 0)
764                   goto retry;
765
766               /* Try each sequence of three linked insns ending with this one.  */
767
768               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
769                 {
770                   rtx link = XEXP (links, 0);
771
772                   /* If the linked insn has been replaced by a note, then there
773                      is no point in pursuing this chain any further.  */
774                   if (NOTE_P (link))
775                     continue;
776
777                   for (nextlinks = LOG_LINKS (link);
778                        nextlinks;
779                        nextlinks = XEXP (nextlinks, 1))
780                     if ((next = try_combine (insn, link,
781                                              XEXP (nextlinks, 0),
782                                              &new_direct_jump_p)) != 0)
783                       goto retry;
784                 }
785
786 #ifdef HAVE_cc0
787               /* Try to combine a jump insn that uses CC0
788                  with a preceding insn that sets CC0, and maybe with its
789                  logical predecessor as well.
790                  This is how we make decrement-and-branch insns.
791                  We need this special code because data flow connections
792                  via CC0 do not get entered in LOG_LINKS.  */
793
794               if (JUMP_P (insn)
795                   && (prev = prev_nonnote_insn (insn)) != 0
796                   && NONJUMP_INSN_P (prev)
797                   && sets_cc0_p (PATTERN (prev)))
798                 {
799                   if ((next = try_combine (insn, prev,
800                                            NULL_RTX, &new_direct_jump_p)) != 0)
801                     goto retry;
802
803                   for (nextlinks = LOG_LINKS (prev); nextlinks;
804                        nextlinks = XEXP (nextlinks, 1))
805                     if ((next = try_combine (insn, prev,
806                                              XEXP (nextlinks, 0),
807                                              &new_direct_jump_p)) != 0)
808                       goto retry;
809                 }
810
811               /* Do the same for an insn that explicitly references CC0.  */
812               if (NONJUMP_INSN_P (insn)
813                   && (prev = prev_nonnote_insn (insn)) != 0
814                   && NONJUMP_INSN_P (prev)
815                   && sets_cc0_p (PATTERN (prev))
816                   && GET_CODE (PATTERN (insn)) == SET
817                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
818                 {
819                   if ((next = try_combine (insn, prev,
820                                            NULL_RTX, &new_direct_jump_p)) != 0)
821                     goto retry;
822
823                   for (nextlinks = LOG_LINKS (prev); nextlinks;
824                        nextlinks = XEXP (nextlinks, 1))
825                     if ((next = try_combine (insn, prev,
826                                              XEXP (nextlinks, 0),
827                                              &new_direct_jump_p)) != 0)
828                       goto retry;
829                 }
830
831               /* Finally, see if any of the insns that this insn links to
832                  explicitly references CC0.  If so, try this insn, that insn,
833                  and its predecessor if it sets CC0.  */
834               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
835                 if (NONJUMP_INSN_P (XEXP (links, 0))
836                     && GET_CODE (PATTERN (XEXP (links, 0))) == SET
837                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
838                     && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
839                     && NONJUMP_INSN_P (prev)
840                     && sets_cc0_p (PATTERN (prev))
841                     && (next = try_combine (insn, XEXP (links, 0),
842                                             prev, &new_direct_jump_p)) != 0)
843                   goto retry;
844 #endif
845
846               /* Try combining an insn with two different insns whose results it
847                  uses.  */
848               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
849                 for (nextlinks = XEXP (links, 1); nextlinks;
850                      nextlinks = XEXP (nextlinks, 1))
851                   if ((next = try_combine (insn, XEXP (links, 0),
852                                            XEXP (nextlinks, 0),
853                                            &new_direct_jump_p)) != 0)
854                     goto retry;
855
856               /* Try this insn with each REG_EQUAL note it links back to.  */
857               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
858                 {
859                   rtx set, note;
860                   rtx temp = XEXP (links, 0);
861                   if ((set = single_set (temp)) != 0
862                       && (note = find_reg_equal_equiv_note (temp)) != 0
863                       && GET_CODE (XEXP (note, 0)) != EXPR_LIST
864                       /* Avoid using a register that may already been marked
865                          dead by an earlier instruction.  */
866                       && ! unmentioned_reg_p (XEXP (note, 0), SET_SRC (set)))
867                     {
868                       /* Temporarily replace the set's source with the
869                          contents of the REG_EQUAL note.  The insn will
870                          be deleted or recognized by try_combine.  */
871                       rtx orig = SET_SRC (set);
872                       SET_SRC (set) = XEXP (note, 0);
873                       next = try_combine (insn, temp, NULL_RTX,
874                                           &new_direct_jump_p);
875                       if (next)
876                         goto retry;
877                       SET_SRC (set) = orig;
878                     }
879                 }
880
881               if (!NOTE_P (insn))
882                 record_dead_and_set_regs (insn);
883
884             retry:
885               ;
886             }
887         }
888     }
889   clear_bb_flags ();
890
891   EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, j, sbi)
892     BASIC_BLOCK (j)->flags |= BB_DIRTY;
893   new_direct_jump_p |= purge_all_dead_edges ();
894   delete_noop_moves ();
895
896   update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
897                                     PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
898                                     | PROP_KILL_DEAD_CODE);
899
900   /* Clean up.  */
901   sbitmap_free (refresh_blocks);
902   free (uid_insn_cost);
903   free (reg_stat);
904   free (uid_cuid);
905
906   {
907     struct undo *undo, *next;
908     for (undo = undobuf.frees; undo; undo = next)
909       {
910         next = undo->next;
911         free (undo);
912       }
913     undobuf.frees = 0;
914   }
915
916   total_attempts += combine_attempts;
917   total_merges += combine_merges;
918   total_extras += combine_extras;
919   total_successes += combine_successes;
920
921   nonzero_sign_valid = 0;
922   rtl_hooks = general_rtl_hooks;
923
924   /* Make recognizer allow volatile MEMs again.  */
925   init_recog ();
926
927   return new_direct_jump_p;
928 }
929
930 /* Wipe the last_xxx fields of reg_stat in preparation for another pass.  */
931
932 static void
933 init_reg_last (void)
934 {
935   unsigned int i;
936   for (i = 0; i < combine_max_regno; i++)
937     memset (reg_stat + i, 0, offsetof (struct reg_stat, sign_bit_copies));
938 }
939 \f
940 /* Set up any promoted values for incoming argument registers.  */
941
942 static void
943 setup_incoming_promotions (void)
944 {
945   unsigned int regno;
946   rtx reg;
947   enum machine_mode mode;
948   int unsignedp;
949   rtx first = get_insns ();
950
951   if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
952     {
953       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
954         /* Check whether this register can hold an incoming pointer
955            argument.  FUNCTION_ARG_REGNO_P tests outgoing register
956            numbers, so translate if necessary due to register windows.  */
957         if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
958             && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
959           {
960             record_value_for_reg
961               (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
962                                            : SIGN_EXTEND),
963                                           GET_MODE (reg),
964                                           gen_rtx_CLOBBER (mode, const0_rtx)));
965           }
966     }
967 }
968 \f
969 /* Called via note_stores.  If X is a pseudo that is narrower than
970    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
971
972    If we are setting only a portion of X and we can't figure out what
973    portion, assume all bits will be used since we don't know what will
974    be happening.
975
976    Similarly, set how many bits of X are known to be copies of the sign bit
977    at all locations in the function.  This is the smallest number implied
978    by any set of X.  */
979
980 static void
981 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
982                                   void *data ATTRIBUTE_UNUSED)
983 {
984   unsigned int num;
985
986   if (REG_P (x)
987       && REGNO (x) >= FIRST_PSEUDO_REGISTER
988       /* If this register is undefined at the start of the file, we can't
989          say what its contents were.  */
990       && ! REGNO_REG_SET_P
991          (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start, REGNO (x))
992       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
993     {
994       if (set == 0 || GET_CODE (set) == CLOBBER)
995         {
996           reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
997           reg_stat[REGNO (x)].sign_bit_copies = 1;
998           return;
999         }
1000
1001       /* If this is a complex assignment, see if we can convert it into a
1002          simple assignment.  */
1003       set = expand_field_assignment (set);
1004
1005       /* If this is a simple assignment, or we have a paradoxical SUBREG,
1006          set what we know about X.  */
1007
1008       if (SET_DEST (set) == x
1009           || (GET_CODE (SET_DEST (set)) == SUBREG
1010               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1011                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1012               && SUBREG_REG (SET_DEST (set)) == x))
1013         {
1014           rtx src = SET_SRC (set);
1015
1016 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1017           /* If X is narrower than a word and SRC is a non-negative
1018              constant that would appear negative in the mode of X,
1019              sign-extend it for use in reg_stat[].nonzero_bits because some
1020              machines (maybe most) will actually do the sign-extension
1021              and this is the conservative approach.
1022
1023              ??? For 2.5, try to tighten up the MD files in this regard
1024              instead of this kludge.  */
1025
1026           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1027               && GET_CODE (src) == CONST_INT
1028               && INTVAL (src) > 0
1029               && 0 != (INTVAL (src)
1030                        & ((HOST_WIDE_INT) 1
1031                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1032             src = GEN_INT (INTVAL (src)
1033                            | ((HOST_WIDE_INT) (-1)
1034                               << GET_MODE_BITSIZE (GET_MODE (x))));
1035 #endif
1036
1037           /* Don't call nonzero_bits if it cannot change anything.  */
1038           if (reg_stat[REGNO (x)].nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1039             reg_stat[REGNO (x)].nonzero_bits
1040               |= nonzero_bits (src, nonzero_bits_mode);
1041           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1042           if (reg_stat[REGNO (x)].sign_bit_copies == 0
1043               || reg_stat[REGNO (x)].sign_bit_copies > num)
1044             reg_stat[REGNO (x)].sign_bit_copies = num;
1045         }
1046       else
1047         {
1048           reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1049           reg_stat[REGNO (x)].sign_bit_copies = 1;
1050         }
1051     }
1052 }
1053 \f
1054 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
1055    insns that were previously combined into I3 or that will be combined
1056    into the merger of INSN and I3.
1057
1058    Return 0 if the combination is not allowed for any reason.
1059
1060    If the combination is allowed, *PDEST will be set to the single
1061    destination of INSN and *PSRC to the single source, and this function
1062    will return 1.  */
1063
1064 static int
1065 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1066                rtx *pdest, rtx *psrc)
1067 {
1068   int i;
1069   rtx set = 0, src, dest;
1070   rtx p;
1071 #ifdef AUTO_INC_DEC
1072   rtx link;
1073 #endif
1074   int all_adjacent = (succ ? (next_active_insn (insn) == succ
1075                               && next_active_insn (succ) == i3)
1076                       : next_active_insn (insn) == i3);
1077
1078   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1079      or a PARALLEL consisting of such a SET and CLOBBERs.
1080
1081      If INSN has CLOBBER parallel parts, ignore them for our processing.
1082      By definition, these happen during the execution of the insn.  When it
1083      is merged with another insn, all bets are off.  If they are, in fact,
1084      needed and aren't also supplied in I3, they may be added by
1085      recog_for_combine.  Otherwise, it won't match.
1086
1087      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1088      note.
1089
1090      Get the source and destination of INSN.  If more than one, can't
1091      combine.  */
1092
1093   if (GET_CODE (PATTERN (insn)) == SET)
1094     set = PATTERN (insn);
1095   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1096            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1097     {
1098       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1099         {
1100           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1101           rtx note;
1102
1103           switch (GET_CODE (elt))
1104             {
1105             /* This is important to combine floating point insns
1106                for the SH4 port.  */
1107             case USE:
1108               /* Combining an isolated USE doesn't make sense.
1109                  We depend here on combinable_i3pat to reject them.  */
1110               /* The code below this loop only verifies that the inputs of
1111                  the SET in INSN do not change.  We call reg_set_between_p
1112                  to verify that the REG in the USE does not change between
1113                  I3 and INSN.
1114                  If the USE in INSN was for a pseudo register, the matching
1115                  insn pattern will likely match any register; combining this
1116                  with any other USE would only be safe if we knew that the
1117                  used registers have identical values, or if there was
1118                  something to tell them apart, e.g. different modes.  For
1119                  now, we forgo such complicated tests and simply disallow
1120                  combining of USES of pseudo registers with any other USE.  */
1121               if (REG_P (XEXP (elt, 0))
1122                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1123                 {
1124                   rtx i3pat = PATTERN (i3);
1125                   int i = XVECLEN (i3pat, 0) - 1;
1126                   unsigned int regno = REGNO (XEXP (elt, 0));
1127
1128                   do
1129                     {
1130                       rtx i3elt = XVECEXP (i3pat, 0, i);
1131
1132                       if (GET_CODE (i3elt) == USE
1133                           && REG_P (XEXP (i3elt, 0))
1134                           && (REGNO (XEXP (i3elt, 0)) == regno
1135                               ? reg_set_between_p (XEXP (elt, 0),
1136                                                    PREV_INSN (insn), i3)
1137                               : regno >= FIRST_PSEUDO_REGISTER))
1138                         return 0;
1139                     }
1140                   while (--i >= 0);
1141                 }
1142               break;
1143
1144               /* We can ignore CLOBBERs.  */
1145             case CLOBBER:
1146               break;
1147
1148             case SET:
1149               /* Ignore SETs whose result isn't used but not those that
1150                  have side-effects.  */
1151               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1152                   && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1153                       || INTVAL (XEXP (note, 0)) <= 0)
1154                   && ! side_effects_p (elt))
1155                 break;
1156
1157               /* If we have already found a SET, this is a second one and
1158                  so we cannot combine with this insn.  */
1159               if (set)
1160                 return 0;
1161
1162               set = elt;
1163               break;
1164
1165             default:
1166               /* Anything else means we can't combine.  */
1167               return 0;
1168             }
1169         }
1170
1171       if (set == 0
1172           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1173              so don't do anything with it.  */
1174           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1175         return 0;
1176     }
1177   else
1178     return 0;
1179
1180   if (set == 0)
1181     return 0;
1182
1183   set = expand_field_assignment (set);
1184   src = SET_SRC (set), dest = SET_DEST (set);
1185
1186   /* Don't eliminate a store in the stack pointer.  */
1187   if (dest == stack_pointer_rtx
1188       /* Don't combine with an insn that sets a register to itself if it has
1189          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
1190       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1191       /* Can't merge an ASM_OPERANDS.  */
1192       || GET_CODE (src) == ASM_OPERANDS
1193       /* Can't merge a function call.  */
1194       || GET_CODE (src) == CALL
1195       /* Don't eliminate a function call argument.  */
1196       || (CALL_P (i3)
1197           && (find_reg_fusage (i3, USE, dest)
1198               || (REG_P (dest)
1199                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1200                   && global_regs[REGNO (dest)])))
1201       /* Don't substitute into an incremented register.  */
1202       || FIND_REG_INC_NOTE (i3, dest)
1203       || (succ && FIND_REG_INC_NOTE (succ, dest))
1204       /* Don't substitute into a non-local goto, this confuses CFG.  */
1205       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1206 #if 0
1207       /* Don't combine the end of a libcall into anything.  */
1208       /* ??? This gives worse code, and appears to be unnecessary, since no
1209          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  Local-alloc does
1210          use REG_RETVAL notes for noconflict blocks, but other code here
1211          makes sure that those insns don't disappear.  */
1212       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1213 #endif
1214       /* Make sure that DEST is not used after SUCC but before I3.  */
1215       || (succ && ! all_adjacent
1216           && reg_used_between_p (dest, succ, i3))
1217       /* Make sure that the value that is to be substituted for the register
1218          does not use any registers whose values alter in between.  However,
1219          If the insns are adjacent, a use can't cross a set even though we
1220          think it might (this can happen for a sequence of insns each setting
1221          the same destination; last_set of that register might point to
1222          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1223          equivalent to the memory so the substitution is valid even if there
1224          are intervening stores.  Also, don't move a volatile asm or
1225          UNSPEC_VOLATILE across any other insns.  */
1226       || (! all_adjacent
1227           && (((!MEM_P (src)
1228                 || ! find_reg_note (insn, REG_EQUIV, src))
1229                && use_crosses_set_p (src, INSN_CUID (insn)))
1230               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1231               || GET_CODE (src) == UNSPEC_VOLATILE))
1232       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1233          better register allocation by not doing the combine.  */
1234       || find_reg_note (i3, REG_NO_CONFLICT, dest)
1235       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1236       /* Don't combine across a CALL_INSN, because that would possibly
1237          change whether the life span of some REGs crosses calls or not,
1238          and it is a pain to update that information.
1239          Exception: if source is a constant, moving it later can't hurt.
1240          Accept that special case, because it helps -fforce-addr a lot.  */
1241       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1242     return 0;
1243
1244   /* DEST must either be a REG or CC0.  */
1245   if (REG_P (dest))
1246     {
1247       /* If register alignment is being enforced for multi-word items in all
1248          cases except for parameters, it is possible to have a register copy
1249          insn referencing a hard register that is not allowed to contain the
1250          mode being copied and which would not be valid as an operand of most
1251          insns.  Eliminate this problem by not combining with such an insn.
1252
1253          Also, on some machines we don't want to extend the life of a hard
1254          register.  */
1255
1256       if (REG_P (src)
1257           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1258                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1259               /* Don't extend the life of a hard register unless it is
1260                  user variable (if we have few registers) or it can't
1261                  fit into the desired register (meaning something special
1262                  is going on).
1263                  Also avoid substituting a return register into I3, because
1264                  reload can't handle a conflict with constraints of other
1265                  inputs.  */
1266               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1267                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1268         return 0;
1269     }
1270   else if (GET_CODE (dest) != CC0)
1271     return 0;
1272
1273
1274   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1275     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1276       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1277         {
1278           /* Don't substitute for a register intended as a clobberable
1279              operand.  */
1280           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1281           if (rtx_equal_p (reg, dest))
1282             return 0;
1283
1284           /* If the clobber represents an earlyclobber operand, we must not
1285              substitute an expression containing the clobbered register.
1286              As we do not analyze the constraint strings here, we have to
1287              make the conservative assumption.  However, if the register is
1288              a fixed hard reg, the clobber cannot represent any operand;
1289              we leave it up to the machine description to either accept or
1290              reject use-and-clobber patterns.  */
1291           if (!REG_P (reg)
1292               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1293               || !fixed_regs[REGNO (reg)])
1294             if (reg_overlap_mentioned_p (reg, src))
1295               return 0;
1296         }
1297
1298   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1299      or not), reject, unless nothing volatile comes between it and I3 */
1300
1301   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1302     {
1303       /* Make sure succ doesn't contain a volatile reference.  */
1304       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1305         return 0;
1306
1307       for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1308         if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1309           return 0;
1310     }
1311
1312   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1313      to be an explicit register variable, and was chosen for a reason.  */
1314
1315   if (GET_CODE (src) == ASM_OPERANDS
1316       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1317     return 0;
1318
1319   /* If there are any volatile insns between INSN and I3, reject, because
1320      they might affect machine state.  */
1321
1322   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1323     if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1324       return 0;
1325
1326   /* If INSN contains an autoincrement or autodecrement, make sure that
1327      register is not used between there and I3, and not already used in
1328      I3 either.  Neither must it be used in PRED or SUCC, if they exist.
1329      Also insist that I3 not be a jump; if it were one
1330      and the incremented register were spilled, we would lose.  */
1331
1332 #ifdef AUTO_INC_DEC
1333   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1334     if (REG_NOTE_KIND (link) == REG_INC
1335         && (JUMP_P (i3)
1336             || reg_used_between_p (XEXP (link, 0), insn, i3)
1337             || (pred != NULL_RTX
1338                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1339             || (succ != NULL_RTX
1340                 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1341             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1342       return 0;
1343 #endif
1344
1345 #ifdef HAVE_cc0
1346   /* Don't combine an insn that follows a CC0-setting insn.
1347      An insn that uses CC0 must not be separated from the one that sets it.
1348      We do, however, allow I2 to follow a CC0-setting insn if that insn
1349      is passed as I1; in that case it will be deleted also.
1350      We also allow combining in this case if all the insns are adjacent
1351      because that would leave the two CC0 insns adjacent as well.
1352      It would be more logical to test whether CC0 occurs inside I1 or I2,
1353      but that would be much slower, and this ought to be equivalent.  */
1354
1355   p = prev_nonnote_insn (insn);
1356   if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1357       && ! all_adjacent)
1358     return 0;
1359 #endif
1360
1361   /* If we get here, we have passed all the tests and the combination is
1362      to be allowed.  */
1363
1364   *pdest = dest;
1365   *psrc = src;
1366
1367   return 1;
1368 }
1369 \f
1370 /* LOC is the location within I3 that contains its pattern or the component
1371    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1372
1373    One problem is if I3 modifies its output, as opposed to replacing it
1374    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1375    so would produce an insn that is not equivalent to the original insns.
1376
1377    Consider:
1378
1379          (set (reg:DI 101) (reg:DI 100))
1380          (set (subreg:SI (reg:DI 101) 0) <foo>)
1381
1382    This is NOT equivalent to:
1383
1384          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1385                     (set (reg:DI 101) (reg:DI 100))])
1386
1387    Not only does this modify 100 (in which case it might still be valid
1388    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1389
1390    We can also run into a problem if I2 sets a register that I1
1391    uses and I1 gets directly substituted into I3 (not via I2).  In that
1392    case, we would be getting the wrong value of I2DEST into I3, so we
1393    must reject the combination.  This case occurs when I2 and I1 both
1394    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1395    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1396    of a SET must prevent combination from occurring.
1397
1398    Before doing the above check, we first try to expand a field assignment
1399    into a set of logical operations.
1400
1401    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1402    we place a register that is both set and used within I3.  If more than one
1403    such register is detected, we fail.
1404
1405    Return 1 if the combination is valid, zero otherwise.  */
1406
1407 static int
1408 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1409                   int i1_not_in_src, rtx *pi3dest_killed)
1410 {
1411   rtx x = *loc;
1412
1413   if (GET_CODE (x) == SET)
1414     {
1415       rtx set = x ;
1416       rtx dest = SET_DEST (set);
1417       rtx src = SET_SRC (set);
1418       rtx inner_dest = dest;
1419
1420       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1421              || GET_CODE (inner_dest) == SUBREG
1422              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1423         inner_dest = XEXP (inner_dest, 0);
1424
1425       /* Check for the case where I3 modifies its output, as discussed
1426          above.  We don't want to prevent pseudos from being combined
1427          into the address of a MEM, so only prevent the combination if
1428          i1 or i2 set the same MEM.  */
1429       if ((inner_dest != dest &&
1430            (!MEM_P (inner_dest)
1431             || rtx_equal_p (i2dest, inner_dest)
1432             || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1433            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1434                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1435
1436           /* This is the same test done in can_combine_p except we can't test
1437              all_adjacent; we don't have to, since this instruction will stay
1438              in place, thus we are not considering increasing the lifetime of
1439              INNER_DEST.
1440
1441              Also, if this insn sets a function argument, combining it with
1442              something that might need a spill could clobber a previous
1443              function argument; the all_adjacent test in can_combine_p also
1444              checks this; here, we do a more specific test for this case.  */
1445
1446           || (REG_P (inner_dest)
1447               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1448               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1449                                         GET_MODE (inner_dest))))
1450           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1451         return 0;
1452
1453       /* If DEST is used in I3, it is being killed in this insn,
1454          so record that for later.
1455          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1456          STACK_POINTER_REGNUM, since these are always considered to be
1457          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1458       if (pi3dest_killed && REG_P (dest)
1459           && reg_referenced_p (dest, PATTERN (i3))
1460           && REGNO (dest) != FRAME_POINTER_REGNUM
1461 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1462           && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1463 #endif
1464 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1465           && (REGNO (dest) != ARG_POINTER_REGNUM
1466               || ! fixed_regs [REGNO (dest)])
1467 #endif
1468           && REGNO (dest) != STACK_POINTER_REGNUM)
1469         {
1470           if (*pi3dest_killed)
1471             return 0;
1472
1473           *pi3dest_killed = dest;
1474         }
1475     }
1476
1477   else if (GET_CODE (x) == PARALLEL)
1478     {
1479       int i;
1480
1481       for (i = 0; i < XVECLEN (x, 0); i++)
1482         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1483                                 i1_not_in_src, pi3dest_killed))
1484           return 0;
1485     }
1486
1487   return 1;
1488 }
1489 \f
1490 /* Return 1 if X is an arithmetic expression that contains a multiplication
1491    and division.  We don't count multiplications by powers of two here.  */
1492
1493 static int
1494 contains_muldiv (rtx x)
1495 {
1496   switch (GET_CODE (x))
1497     {
1498     case MOD:  case DIV:  case UMOD:  case UDIV:
1499       return 1;
1500
1501     case MULT:
1502       return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1503                 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1504     default:
1505       if (BINARY_P (x))
1506         return contains_muldiv (XEXP (x, 0))
1507             || contains_muldiv (XEXP (x, 1));
1508
1509       if (UNARY_P (x))
1510         return contains_muldiv (XEXP (x, 0));
1511
1512       return 0;
1513     }
1514 }
1515 \f
1516 /* Determine whether INSN can be used in a combination.  Return nonzero if
1517    not.  This is used in try_combine to detect early some cases where we
1518    can't perform combinations.  */
1519
1520 static int
1521 cant_combine_insn_p (rtx insn)
1522 {
1523   rtx set;
1524   rtx src, dest;
1525
1526   /* If this isn't really an insn, we can't do anything.
1527      This can occur when flow deletes an insn that it has merged into an
1528      auto-increment address.  */
1529   if (! INSN_P (insn))
1530     return 1;
1531
1532   /* Never combine loads and stores involving hard regs that are likely
1533      to be spilled.  The register allocator can usually handle such
1534      reg-reg moves by tying.  If we allow the combiner to make
1535      substitutions of likely-spilled regs, reload might die.
1536      As an exception, we allow combinations involving fixed regs; these are
1537      not available to the register allocator so there's no risk involved.  */
1538
1539   set = single_set (insn);
1540   if (! set)
1541     return 0;
1542   src = SET_SRC (set);
1543   dest = SET_DEST (set);
1544   if (GET_CODE (src) == SUBREG)
1545     src = SUBREG_REG (src);
1546   if (GET_CODE (dest) == SUBREG)
1547     dest = SUBREG_REG (dest);
1548   if (REG_P (src) && REG_P (dest)
1549       && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1550            && ! fixed_regs[REGNO (src)]
1551            && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1552           || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1553               && ! fixed_regs[REGNO (dest)]
1554               && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1555     return 1;
1556
1557   return 0;
1558 }
1559
1560 /* Adjust INSN after we made a change to its destination.
1561
1562    Changing the destination can invalidate notes that say something about
1563    the results of the insn and a LOG_LINK pointing to the insn.  */
1564
1565 static void
1566 adjust_for_new_dest (rtx insn)
1567 {
1568   rtx *loc;
1569
1570   /* For notes, be conservative and simply remove them.  */
1571   loc = &REG_NOTES (insn);
1572   while (*loc)
1573     {
1574       enum reg_note kind = REG_NOTE_KIND (*loc);
1575       if (kind == REG_EQUAL || kind == REG_EQUIV)
1576         *loc = XEXP (*loc, 1);
1577       else
1578         loc = &XEXP (*loc, 1);
1579     }
1580
1581   /* The new insn will have a destination that was previously the destination
1582      of an insn just above it.  Call distribute_links to make a LOG_LINK from
1583      the next use of that destination.  */
1584   distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1585 }
1586
1587 /* Try to combine the insns I1 and I2 into I3.
1588    Here I1 and I2 appear earlier than I3.
1589    I1 can be zero; then we combine just I2 into I3.
1590
1591    If we are combining three insns and the resulting insn is not recognized,
1592    try splitting it into two insns.  If that happens, I2 and I3 are retained
1593    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1594    are pseudo-deleted.
1595
1596    Return 0 if the combination does not work.  Then nothing is changed.
1597    If we did the combination, return the insn at which combine should
1598    resume scanning.
1599
1600    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1601    new direct jump instruction.  */
1602
1603 static rtx
1604 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1605 {
1606   /* New patterns for I3 and I2, respectively.  */
1607   rtx newpat, newi2pat = 0;
1608   rtvec newpat_vec_with_clobbers = 0;
1609   int substed_i2 = 0, substed_i1 = 0;
1610   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1611   int added_sets_1, added_sets_2;
1612   /* Total number of SETs to put into I3.  */
1613   int total_sets;
1614   /* Nonzero if I2's body now appears in I3.  */
1615   int i2_is_used;
1616   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1617   int insn_code_number, i2_code_number = 0, other_code_number = 0;
1618   /* Contains I3 if the destination of I3 is used in its source, which means
1619      that the old life of I3 is being killed.  If that usage is placed into
1620      I2 and not in I3, a REG_DEAD note must be made.  */
1621   rtx i3dest_killed = 0;
1622   /* SET_DEST and SET_SRC of I2 and I1.  */
1623   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1624   /* PATTERN (I2), or a copy of it in certain cases.  */
1625   rtx i2pat;
1626   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1627   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1628   int i1_feeds_i3 = 0;
1629   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1630   rtx new_i3_notes, new_i2_notes;
1631   /* Notes that we substituted I3 into I2 instead of the normal case.  */
1632   int i3_subst_into_i2 = 0;
1633   /* Notes that I1, I2 or I3 is a MULT operation.  */
1634   int have_mult = 0;
1635   int swap_i2i3 = 0;
1636
1637   int maxreg;
1638   rtx temp;
1639   rtx link;
1640   int i;
1641
1642   /* Exit early if one of the insns involved can't be used for
1643      combinations.  */
1644   if (cant_combine_insn_p (i3)
1645       || cant_combine_insn_p (i2)
1646       || (i1 && cant_combine_insn_p (i1))
1647       /* We also can't do anything if I3 has a
1648          REG_LIBCALL note since we don't want to disrupt the contiguity of a
1649          libcall.  */
1650 #if 0
1651       /* ??? This gives worse code, and appears to be unnecessary, since no
1652          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  */
1653       || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1654 #endif
1655       )
1656     return 0;
1657
1658   combine_attempts++;
1659   undobuf.other_insn = 0;
1660
1661   /* Reset the hard register usage information.  */
1662   CLEAR_HARD_REG_SET (newpat_used_regs);
1663
1664   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1665      code below, set I1 to be the earlier of the two insns.  */
1666   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1667     temp = i1, i1 = i2, i2 = temp;
1668
1669   added_links_insn = 0;
1670
1671   /* First check for one important special-case that the code below will
1672      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
1673      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1674      we may be able to replace that destination with the destination of I3.
1675      This occurs in the common code where we compute both a quotient and
1676      remainder into a structure, in which case we want to do the computation
1677      directly into the structure to avoid register-register copies.
1678
1679      Note that this case handles both multiple sets in I2 and also
1680      cases where I2 has a number of CLOBBER or PARALLELs.
1681
1682      We make very conservative checks below and only try to handle the
1683      most common cases of this.  For example, we only handle the case
1684      where I2 and I3 are adjacent to avoid making difficult register
1685      usage tests.  */
1686
1687   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
1688       && REG_P (SET_SRC (PATTERN (i3)))
1689       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1690       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1691       && GET_CODE (PATTERN (i2)) == PARALLEL
1692       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1693       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1694          below would need to check what is inside (and reg_overlap_mentioned_p
1695          doesn't support those codes anyway).  Don't allow those destinations;
1696          the resulting insn isn't likely to be recognized anyway.  */
1697       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1698       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1699       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1700                                     SET_DEST (PATTERN (i3)))
1701       && next_real_insn (i2) == i3)
1702     {
1703       rtx p2 = PATTERN (i2);
1704
1705       /* Make sure that the destination of I3,
1706          which we are going to substitute into one output of I2,
1707          is not used within another output of I2.  We must avoid making this:
1708          (parallel [(set (mem (reg 69)) ...)
1709                     (set (reg 69) ...)])
1710          which is not well-defined as to order of actions.
1711          (Besides, reload can't handle output reloads for this.)
1712
1713          The problem can also happen if the dest of I3 is a memory ref,
1714          if another dest in I2 is an indirect memory ref.  */
1715       for (i = 0; i < XVECLEN (p2, 0); i++)
1716         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1717              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1718             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1719                                         SET_DEST (XVECEXP (p2, 0, i))))
1720           break;
1721
1722       if (i == XVECLEN (p2, 0))
1723         for (i = 0; i < XVECLEN (p2, 0); i++)
1724           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1725                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1726               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1727             {
1728               combine_merges++;
1729
1730               subst_insn = i3;
1731               subst_low_cuid = INSN_CUID (i2);
1732
1733               added_sets_2 = added_sets_1 = 0;
1734               i2dest = SET_SRC (PATTERN (i3));
1735
1736               /* Replace the dest in I2 with our dest and make the resulting
1737                  insn the new pattern for I3.  Then skip to where we
1738                  validate the pattern.  Everything was set up above.  */
1739               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1740                      SET_DEST (PATTERN (i3)));
1741
1742               newpat = p2;
1743               i3_subst_into_i2 = 1;
1744               goto validate_replacement;
1745             }
1746     }
1747
1748   /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1749      one of those words to another constant, merge them by making a new
1750      constant.  */
1751   if (i1 == 0
1752       && (temp = single_set (i2)) != 0
1753       && (GET_CODE (SET_SRC (temp)) == CONST_INT
1754           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1755       && REG_P (SET_DEST (temp))
1756       && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1757       && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1758       && GET_CODE (PATTERN (i3)) == SET
1759       && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1760       && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1761       && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1762       && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1763       && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1764     {
1765       HOST_WIDE_INT lo, hi;
1766
1767       if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1768         lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1769       else
1770         {
1771           lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1772           hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1773         }
1774
1775       if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1776         {
1777           /* We don't handle the case of the target word being wider
1778              than a host wide int.  */
1779           gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD);
1780
1781           lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1782           lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1783                  & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1784         }
1785       else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1786         hi = INTVAL (SET_SRC (PATTERN (i3)));
1787       else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1788         {
1789           int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1790                              >> (HOST_BITS_PER_WIDE_INT - 1));
1791
1792           lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1793                    (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1794           lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1795                  (INTVAL (SET_SRC (PATTERN (i3)))));
1796           if (hi == sign)
1797             hi = lo < 0 ? -1 : 0;
1798         }
1799       else
1800         /* We don't handle the case of the higher word not fitting
1801            entirely in either hi or lo.  */
1802         gcc_unreachable ();
1803
1804       combine_merges++;
1805       subst_insn = i3;
1806       subst_low_cuid = INSN_CUID (i2);
1807       added_sets_2 = added_sets_1 = 0;
1808       i2dest = SET_DEST (temp);
1809
1810       SUBST (SET_SRC (temp),
1811              immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1812
1813       newpat = PATTERN (i2);
1814       goto validate_replacement;
1815     }
1816
1817 #ifndef HAVE_cc0
1818   /* If we have no I1 and I2 looks like:
1819         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1820                    (set Y OP)])
1821      make up a dummy I1 that is
1822         (set Y OP)
1823      and change I2 to be
1824         (set (reg:CC X) (compare:CC Y (const_int 0)))
1825
1826      (We can ignore any trailing CLOBBERs.)
1827
1828      This undoes a previous combination and allows us to match a branch-and-
1829      decrement insn.  */
1830
1831   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1832       && XVECLEN (PATTERN (i2), 0) >= 2
1833       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1834       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1835           == MODE_CC)
1836       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1837       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1838       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1839       && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
1840       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1841                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1842     {
1843       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1844         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1845           break;
1846
1847       if (i == 1)
1848         {
1849           /* We make I1 with the same INSN_UID as I2.  This gives it
1850              the same INSN_CUID for value tracking.  Our fake I1 will
1851              never appear in the insn stream so giving it the same INSN_UID
1852              as I2 will not cause a problem.  */
1853
1854           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1855                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1856                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1857                              NULL_RTX);
1858
1859           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1860           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1861                  SET_DEST (PATTERN (i1)));
1862         }
1863     }
1864 #endif
1865
1866   /* Verify that I2 and I1 are valid for combining.  */
1867   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1868       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1869     {
1870       undo_all ();
1871       return 0;
1872     }
1873
1874   /* Record whether I2DEST is used in I2SRC and similarly for the other
1875      cases.  Knowing this will help in register status updating below.  */
1876   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1877   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1878   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1879
1880   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
1881      in I2SRC.  */
1882   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1883
1884   /* Ensure that I3's pattern can be the destination of combines.  */
1885   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1886                           i1 && i2dest_in_i1src && i1_feeds_i3,
1887                           &i3dest_killed))
1888     {
1889       undo_all ();
1890       return 0;
1891     }
1892
1893   /* See if any of the insns is a MULT operation.  Unless one is, we will
1894      reject a combination that is, since it must be slower.  Be conservative
1895      here.  */
1896   if (GET_CODE (i2src) == MULT
1897       || (i1 != 0 && GET_CODE (i1src) == MULT)
1898       || (GET_CODE (PATTERN (i3)) == SET
1899           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1900     have_mult = 1;
1901
1902   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1903      We used to do this EXCEPT in one case: I3 has a post-inc in an
1904      output operand.  However, that exception can give rise to insns like
1905         mov r3,(r3)+
1906      which is a famous insn on the PDP-11 where the value of r3 used as the
1907      source was model-dependent.  Avoid this sort of thing.  */
1908
1909 #if 0
1910   if (!(GET_CODE (PATTERN (i3)) == SET
1911         && REG_P (SET_SRC (PATTERN (i3)))
1912         && MEM_P (SET_DEST (PATTERN (i3)))
1913         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1914             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1915     /* It's not the exception.  */
1916 #endif
1917 #ifdef AUTO_INC_DEC
1918     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1919       if (REG_NOTE_KIND (link) == REG_INC
1920           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1921               || (i1 != 0
1922                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1923         {
1924           undo_all ();
1925           return 0;
1926         }
1927 #endif
1928
1929   /* See if the SETs in I1 or I2 need to be kept around in the merged
1930      instruction: whenever the value set there is still needed past I3.
1931      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1932
1933      For the SET in I1, we have two cases:  If I1 and I2 independently
1934      feed into I3, the set in I1 needs to be kept around if I1DEST dies
1935      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
1936      in I1 needs to be kept around unless I1DEST dies or is set in either
1937      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
1938      I1DEST.  If so, we know I1 feeds into I2.  */
1939
1940   added_sets_2 = ! dead_or_set_p (i3, i2dest);
1941
1942   added_sets_1
1943     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1944                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1945
1946   /* If the set in I2 needs to be kept around, we must make a copy of
1947      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1948      PATTERN (I2), we are only substituting for the original I1DEST, not into
1949      an already-substituted copy.  This also prevents making self-referential
1950      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1951      I2DEST.  */
1952
1953   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1954            ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1955            : PATTERN (i2));
1956
1957   if (added_sets_2)
1958     i2pat = copy_rtx (i2pat);
1959
1960   combine_merges++;
1961
1962   /* Substitute in the latest insn for the regs set by the earlier ones.  */
1963
1964   maxreg = max_reg_num ();
1965
1966   subst_insn = i3;
1967
1968   /* It is possible that the source of I2 or I1 may be performing an
1969      unneeded operation, such as a ZERO_EXTEND of something that is known
1970      to have the high part zero.  Handle that case by letting subst look at
1971      the innermost one of them.
1972
1973      Another way to do this would be to have a function that tries to
1974      simplify a single insn instead of merging two or more insns.  We don't
1975      do this because of the potential of infinite loops and because
1976      of the potential extra memory required.  However, doing it the way
1977      we are is a bit of a kludge and doesn't catch all cases.
1978
1979      But only do this if -fexpensive-optimizations since it slows things down
1980      and doesn't usually win.  */
1981
1982   if (flag_expensive_optimizations)
1983     {
1984       /* Pass pc_rtx so no substitutions are done, just simplifications.  */
1985       if (i1)
1986         {
1987           subst_low_cuid = INSN_CUID (i1);
1988           i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1989         }
1990       else
1991         {
1992           subst_low_cuid = INSN_CUID (i2);
1993           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1994         }
1995     }
1996
1997 #ifndef HAVE_cc0
1998   /* Many machines that don't use CC0 have insns that can both perform an
1999      arithmetic operation and set the condition code.  These operations will
2000      be represented as a PARALLEL with the first element of the vector
2001      being a COMPARE of an arithmetic operation with the constant zero.
2002      The second element of the vector will set some pseudo to the result
2003      of the same arithmetic operation.  If we simplify the COMPARE, we won't
2004      match such a pattern and so will generate an extra insn.   Here we test
2005      for this case, where both the comparison and the operation result are
2006      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2007      I2SRC.  Later we will make the PARALLEL that contains I2.  */
2008
2009   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2010       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2011       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2012       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2013     {
2014 #ifdef SELECT_CC_MODE
2015       rtx *cc_use;
2016       enum machine_mode compare_mode;
2017 #endif
2018
2019       newpat = PATTERN (i3);
2020       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2021
2022       i2_is_used = 1;
2023
2024 #ifdef SELECT_CC_MODE
2025       /* See if a COMPARE with the operand we substituted in should be done
2026          with the mode that is currently being used.  If not, do the same
2027          processing we do in `subst' for a SET; namely, if the destination
2028          is used only once, try to replace it with a register of the proper
2029          mode and also replace the COMPARE.  */
2030       if (undobuf.other_insn == 0
2031           && (cc_use = find_single_use (SET_DEST (newpat), i3,
2032                                         &undobuf.other_insn))
2033           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2034                                               i2src, const0_rtx))
2035               != GET_MODE (SET_DEST (newpat))))
2036         {
2037           unsigned int regno = REGNO (SET_DEST (newpat));
2038           rtx new_dest = gen_rtx_REG (compare_mode, regno);
2039
2040           if (regno < FIRST_PSEUDO_REGISTER
2041               || (REG_N_SETS (regno) == 1 && ! added_sets_2
2042                   && ! REG_USERVAR_P (SET_DEST (newpat))))
2043             {
2044               if (regno >= FIRST_PSEUDO_REGISTER)
2045                 SUBST (regno_reg_rtx[regno], new_dest);
2046
2047               SUBST (SET_DEST (newpat), new_dest);
2048               SUBST (XEXP (*cc_use, 0), new_dest);
2049               SUBST (SET_SRC (newpat),
2050                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2051             }
2052           else
2053             undobuf.other_insn = 0;
2054         }
2055 #endif
2056     }
2057   else
2058 #endif
2059     {
2060       n_occurrences = 0;                /* `subst' counts here */
2061
2062       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2063          need to make a unique copy of I2SRC each time we substitute it
2064          to avoid self-referential rtl.  */
2065
2066       subst_low_cuid = INSN_CUID (i2);
2067       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2068                       ! i1_feeds_i3 && i1dest_in_i1src);
2069       substed_i2 = 1;
2070
2071       /* Record whether i2's body now appears within i3's body.  */
2072       i2_is_used = n_occurrences;
2073     }
2074
2075   /* If we already got a failure, don't try to do more.  Otherwise,
2076      try to substitute in I1 if we have it.  */
2077
2078   if (i1 && GET_CODE (newpat) != CLOBBER)
2079     {
2080       /* Before we can do this substitution, we must redo the test done
2081          above (see detailed comments there) that ensures  that I1DEST
2082          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
2083
2084       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2085                               0, (rtx*) 0))
2086         {
2087           undo_all ();
2088           return 0;
2089         }
2090
2091       n_occurrences = 0;
2092       subst_low_cuid = INSN_CUID (i1);
2093       newpat = subst (newpat, i1dest, i1src, 0, 0);
2094       substed_i1 = 1;
2095     }
2096
2097   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
2098      to count all the ways that I2SRC and I1SRC can be used.  */
2099   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2100        && i2_is_used + added_sets_2 > 1)
2101       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2102           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2103               > 1))
2104       /* Fail if we tried to make a new register.  */
2105       || max_reg_num () != maxreg
2106       /* Fail if we couldn't do something and have a CLOBBER.  */
2107       || GET_CODE (newpat) == CLOBBER
2108       /* Fail if this new pattern is a MULT and we didn't have one before
2109          at the outer level.  */
2110       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2111           && ! have_mult))
2112     {
2113       undo_all ();
2114       return 0;
2115     }
2116
2117   /* If the actions of the earlier insns must be kept
2118      in addition to substituting them into the latest one,
2119      we must make a new PARALLEL for the latest insn
2120      to hold additional the SETs.  */
2121
2122   if (added_sets_1 || added_sets_2)
2123     {
2124       combine_extras++;
2125
2126       if (GET_CODE (newpat) == PARALLEL)
2127         {
2128           rtvec old = XVEC (newpat, 0);
2129           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2130           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2131           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2132                   sizeof (old->elem[0]) * old->num_elem);
2133         }
2134       else
2135         {
2136           rtx old = newpat;
2137           total_sets = 1 + added_sets_1 + added_sets_2;
2138           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2139           XVECEXP (newpat, 0, 0) = old;
2140         }
2141
2142       if (added_sets_1)
2143         XVECEXP (newpat, 0, --total_sets)
2144           = (GET_CODE (PATTERN (i1)) == PARALLEL
2145              ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2146
2147       if (added_sets_2)
2148         {
2149           /* If there is no I1, use I2's body as is.  We used to also not do
2150              the subst call below if I2 was substituted into I3,
2151              but that could lose a simplification.  */
2152           if (i1 == 0)
2153             XVECEXP (newpat, 0, --total_sets) = i2pat;
2154           else
2155             /* See comment where i2pat is assigned.  */
2156             XVECEXP (newpat, 0, --total_sets)
2157               = subst (i2pat, i1dest, i1src, 0, 0);
2158         }
2159     }
2160
2161   /* We come here when we are replacing a destination in I2 with the
2162      destination of I3.  */
2163  validate_replacement:
2164
2165   /* Note which hard regs this insn has as inputs.  */
2166   mark_used_regs_combine (newpat);
2167
2168   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
2169      consider splitting this pattern, we might need these clobbers.  */
2170   if (i1 && GET_CODE (newpat) == PARALLEL
2171       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2172     {
2173       int len = XVECLEN (newpat, 0);
2174
2175       newpat_vec_with_clobbers = rtvec_alloc (len);
2176       for (i = 0; i < len; i++)
2177         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2178     }
2179
2180   /* Is the result of combination a valid instruction?  */
2181   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2182
2183   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2184      the second SET's destination is a register that is unused and isn't
2185      marked as an instruction that might trap in an EH region.  In that case,
2186      we just need the first SET.   This can occur when simplifying a divmod
2187      insn.  We *must* test for this case here because the code below that
2188      splits two independent SETs doesn't handle this case correctly when it
2189      updates the register status.
2190
2191      It's pointless doing this if we originally had two sets, one from
2192      i3, and one from i2.  Combining then splitting the parallel results
2193      in the original i2 again plus an invalid insn (which we delete).
2194      The net effect is only to move instructions around, which makes
2195      debug info less accurate.
2196
2197      Also check the case where the first SET's destination is unused.
2198      That would not cause incorrect code, but does cause an unneeded
2199      insn to remain.  */
2200
2201   if (insn_code_number < 0
2202       && !(added_sets_2 && i1 == 0)
2203       && GET_CODE (newpat) == PARALLEL
2204       && XVECLEN (newpat, 0) == 2
2205       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2206       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2207       && asm_noperands (newpat) < 0)
2208     {
2209       rtx set0 = XVECEXP (newpat, 0, 0);
2210       rtx set1 = XVECEXP (newpat, 0, 1);
2211       rtx note;
2212
2213       if (((REG_P (SET_DEST (set1))
2214             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2215            || (GET_CODE (SET_DEST (set1)) == SUBREG
2216                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2217           && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2218               || INTVAL (XEXP (note, 0)) <= 0)
2219           && ! side_effects_p (SET_SRC (set1)))
2220         {
2221           newpat = set0;
2222           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2223         }
2224
2225       else if (((REG_P (SET_DEST (set0))
2226                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2227                 || (GET_CODE (SET_DEST (set0)) == SUBREG
2228                     && find_reg_note (i3, REG_UNUSED,
2229                                       SUBREG_REG (SET_DEST (set0)))))
2230                && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2231                    || INTVAL (XEXP (note, 0)) <= 0)
2232                && ! side_effects_p (SET_SRC (set0)))
2233         {
2234           newpat = set1;
2235           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2236
2237           if (insn_code_number >= 0)
2238             {
2239               /* If we will be able to accept this, we have made a
2240                  change to the destination of I3.  This requires us to
2241                  do a few adjustments.  */
2242
2243               PATTERN (i3) = newpat;
2244               adjust_for_new_dest (i3);
2245             }
2246         }
2247     }
2248
2249   /* If we were combining three insns and the result is a simple SET
2250      with no ASM_OPERANDS that wasn't recognized, try to split it into two
2251      insns.  There are two ways to do this.  It can be split using a
2252      machine-specific method (like when you have an addition of a large
2253      constant) or by combine in the function find_split_point.  */
2254
2255   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2256       && asm_noperands (newpat) < 0)
2257     {
2258       rtx m_split, *split;
2259       rtx ni2dest = i2dest;
2260
2261       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
2262          use I2DEST as a scratch register will help.  In the latter case,
2263          convert I2DEST to the mode of the source of NEWPAT if we can.  */
2264
2265       m_split = split_insns (newpat, i3);
2266
2267       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2268          inputs of NEWPAT.  */
2269
2270       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2271          possible to try that as a scratch reg.  This would require adding
2272          more code to make it work though.  */
2273
2274       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2275         {
2276           /* If I2DEST is a hard register or the only use of a pseudo,
2277              we can change its mode.  */
2278           if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2279               && GET_MODE (SET_DEST (newpat)) != VOIDmode
2280               && REG_P (i2dest)
2281               && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2282                   || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2283                       && ! REG_USERVAR_P (i2dest))))
2284             ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2285                                    REGNO (i2dest));
2286
2287           m_split = split_insns (gen_rtx_PARALLEL
2288                                  (VOIDmode,
2289                                   gen_rtvec (2, newpat,
2290                                              gen_rtx_CLOBBER (VOIDmode,
2291                                                               ni2dest))),
2292                                  i3);
2293           /* If the split with the mode-changed register didn't work, try
2294              the original register.  */
2295           if (! m_split && ni2dest != i2dest)
2296             {
2297               ni2dest = i2dest;
2298               m_split = split_insns (gen_rtx_PARALLEL
2299                                      (VOIDmode,
2300                                       gen_rtvec (2, newpat,
2301                                                  gen_rtx_CLOBBER (VOIDmode,
2302                                                                   i2dest))),
2303                                      i3);
2304             }
2305         }
2306
2307       /* If recog_for_combine has discarded clobbers, try to use them
2308          again for the split.  */
2309       if (m_split == 0 && newpat_vec_with_clobbers)
2310         m_split
2311           = split_insns (gen_rtx_PARALLEL (VOIDmode,
2312                                            newpat_vec_with_clobbers), i3);
2313
2314       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2315         {
2316           m_split = PATTERN (m_split);
2317           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2318           if (insn_code_number >= 0)
2319             newpat = m_split;
2320         }
2321       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2322                && (next_real_insn (i2) == i3
2323                    || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2324         {
2325           rtx i2set, i3set;
2326           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2327           newi2pat = PATTERN (m_split);
2328
2329           i3set = single_set (NEXT_INSN (m_split));
2330           i2set = single_set (m_split);
2331
2332           /* In case we changed the mode of I2DEST, replace it in the
2333              pseudo-register table here.  We can't do it above in case this
2334              code doesn't get executed and we do a split the other way.  */
2335
2336           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2337             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2338
2339           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2340
2341           /* If I2 or I3 has multiple SETs, we won't know how to track
2342              register status, so don't use these insns.  If I2's destination
2343              is used between I2 and I3, we also can't use these insns.  */
2344
2345           if (i2_code_number >= 0 && i2set && i3set
2346               && (next_real_insn (i2) == i3
2347                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2348             insn_code_number = recog_for_combine (&newi3pat, i3,
2349                                                   &new_i3_notes);
2350           if (insn_code_number >= 0)
2351             newpat = newi3pat;
2352
2353           /* It is possible that both insns now set the destination of I3.
2354              If so, we must show an extra use of it.  */
2355
2356           if (insn_code_number >= 0)
2357             {
2358               rtx new_i3_dest = SET_DEST (i3set);
2359               rtx new_i2_dest = SET_DEST (i2set);
2360
2361               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2362                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2363                      || GET_CODE (new_i3_dest) == SUBREG)
2364                 new_i3_dest = XEXP (new_i3_dest, 0);
2365
2366               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2367                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2368                      || GET_CODE (new_i2_dest) == SUBREG)
2369                 new_i2_dest = XEXP (new_i2_dest, 0);
2370
2371               if (REG_P (new_i3_dest)
2372                   && REG_P (new_i2_dest)
2373                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2374                 REG_N_SETS (REGNO (new_i2_dest))++;
2375             }
2376         }
2377
2378       /* If we can split it and use I2DEST, go ahead and see if that
2379          helps things be recognized.  Verify that none of the registers
2380          are set between I2 and I3.  */
2381       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2382 #ifdef HAVE_cc0
2383           && REG_P (i2dest)
2384 #endif
2385           /* We need I2DEST in the proper mode.  If it is a hard register
2386              or the only use of a pseudo, we can change its mode.
2387              Make sure we don't change a hard register to have a mode that
2388              isn't valid for it, or change the number of registers.  */
2389           && (GET_MODE (*split) == GET_MODE (i2dest)
2390               || GET_MODE (*split) == VOIDmode
2391               || (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2392                   && HARD_REGNO_MODE_OK (REGNO (i2dest), GET_MODE (*split))
2393                   && (hard_regno_nregs[REGNO (i2dest)][GET_MODE (i2dest)]
2394                       == hard_regno_nregs[REGNO (i2dest)][GET_MODE (*split)]))
2395               || (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER
2396                   && REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2397                   && ! REG_USERVAR_P (i2dest)))
2398           && (next_real_insn (i2) == i3
2399               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2400           /* We can't overwrite I2DEST if its value is still used by
2401              NEWPAT.  */
2402           && ! reg_referenced_p (i2dest, newpat))
2403         {
2404           rtx newdest = i2dest;
2405           enum rtx_code split_code = GET_CODE (*split);
2406           enum machine_mode split_mode = GET_MODE (*split);
2407
2408           /* Get NEWDEST as a register in the proper mode.  We have already
2409              validated that we can do this.  */
2410           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2411             {
2412               newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2413
2414               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2415                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2416             }
2417
2418           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2419              an ASHIFT.  This can occur if it was inside a PLUS and hence
2420              appeared to be a memory address.  This is a kludge.  */
2421           if (split_code == MULT
2422               && GET_CODE (XEXP (*split, 1)) == CONST_INT
2423               && INTVAL (XEXP (*split, 1)) > 0
2424               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2425             {
2426               SUBST (*split, gen_rtx_ASHIFT (split_mode,
2427                                              XEXP (*split, 0), GEN_INT (i)));
2428               /* Update split_code because we may not have a multiply
2429                  anymore.  */
2430               split_code = GET_CODE (*split);
2431             }
2432
2433 #ifdef INSN_SCHEDULING
2434           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2435              be written as a ZERO_EXTEND.  */
2436           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
2437             {
2438 #ifdef LOAD_EXTEND_OP
2439               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2440                  what it really is.  */
2441               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2442                   == SIGN_EXTEND)
2443                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2444                                                     SUBREG_REG (*split)));
2445               else
2446 #endif
2447                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2448                                                     SUBREG_REG (*split)));
2449             }
2450 #endif
2451
2452           newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2453           SUBST (*split, newdest);
2454           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2455
2456           /* recog_for_combine might have added CLOBBERs to newi2pat.
2457              Make sure NEWPAT does not depend on the clobbered regs.  */
2458           if (GET_CODE (newi2pat) == PARALLEL)
2459             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
2460               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
2461                 {
2462                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
2463                   if (reg_overlap_mentioned_p (reg, newpat))
2464                     {
2465                       undo_all ();
2466                       return 0;
2467                     }
2468                 }
2469
2470           /* If the split point was a MULT and we didn't have one before,
2471              don't use one now.  */
2472           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2473             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2474         }
2475     }
2476
2477   /* Check for a case where we loaded from memory in a narrow mode and
2478      then sign extended it, but we need both registers.  In that case,
2479      we have a PARALLEL with both loads from the same memory location.
2480      We can split this into a load from memory followed by a register-register
2481      copy.  This saves at least one insn, more if register allocation can
2482      eliminate the copy.
2483
2484      We cannot do this if the destination of the first assignment is a
2485      condition code register or cc0.  We eliminate this case by making sure
2486      the SET_DEST and SET_SRC have the same mode.
2487
2488      We cannot do this if the destination of the second assignment is
2489      a register that we have already assumed is zero-extended.  Similarly
2490      for a SUBREG of such a register.  */
2491
2492   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2493            && GET_CODE (newpat) == PARALLEL
2494            && XVECLEN (newpat, 0) == 2
2495            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2496            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2497            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2498                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2499            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2500            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2501                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2502            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2503                                    INSN_CUID (i2))
2504            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2505            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2506            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2507                  (REG_P (temp)
2508                   && reg_stat[REGNO (temp)].nonzero_bits != 0
2509                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2510                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2511                   && (reg_stat[REGNO (temp)].nonzero_bits
2512                       != GET_MODE_MASK (word_mode))))
2513            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2514                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2515                      (REG_P (temp)
2516                       && reg_stat[REGNO (temp)].nonzero_bits != 0
2517                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2518                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2519                       && (reg_stat[REGNO (temp)].nonzero_bits
2520                           != GET_MODE_MASK (word_mode)))))
2521            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2522                                          SET_SRC (XVECEXP (newpat, 0, 1)))
2523            && ! find_reg_note (i3, REG_UNUSED,
2524                                SET_DEST (XVECEXP (newpat, 0, 0))))
2525     {
2526       rtx ni2dest;
2527
2528       newi2pat = XVECEXP (newpat, 0, 0);
2529       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2530       newpat = XVECEXP (newpat, 0, 1);
2531       SUBST (SET_SRC (newpat),
2532              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
2533       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2534
2535       if (i2_code_number >= 0)
2536         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2537
2538       if (insn_code_number >= 0)
2539         swap_i2i3 = 1;
2540     }
2541
2542   /* Similarly, check for a case where we have a PARALLEL of two independent
2543      SETs but we started with three insns.  In this case, we can do the sets
2544      as two separate insns.  This case occurs when some SET allows two
2545      other insns to combine, but the destination of that SET is still live.  */
2546
2547   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2548            && GET_CODE (newpat) == PARALLEL
2549            && XVECLEN (newpat, 0) == 2
2550            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2551            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2552            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2553            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2554            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2555            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2556            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2557                                    INSN_CUID (i2))
2558            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
2559            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2560            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2561            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2562                                   XVECEXP (newpat, 0, 0))
2563            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2564                                   XVECEXP (newpat, 0, 1))
2565            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2566                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2567     {
2568       /* Normally, it doesn't matter which of the two is done first,
2569          but it does if one references cc0.  In that case, it has to
2570          be first.  */
2571 #ifdef HAVE_cc0
2572       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2573         {
2574           newi2pat = XVECEXP (newpat, 0, 0);
2575           newpat = XVECEXP (newpat, 0, 1);
2576         }
2577       else
2578 #endif
2579         {
2580           newi2pat = XVECEXP (newpat, 0, 1);
2581           newpat = XVECEXP (newpat, 0, 0);
2582         }
2583
2584       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2585
2586       if (i2_code_number >= 0)
2587         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2588     }
2589
2590   /* If it still isn't recognized, fail and change things back the way they
2591      were.  */
2592   if ((insn_code_number < 0
2593        /* Is the result a reasonable ASM_OPERANDS?  */
2594        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2595     {
2596       undo_all ();
2597       return 0;
2598     }
2599
2600   /* If we had to change another insn, make sure it is valid also.  */
2601   if (undobuf.other_insn)
2602     {
2603       rtx other_pat = PATTERN (undobuf.other_insn);
2604       rtx new_other_notes;
2605       rtx note, next;
2606
2607       CLEAR_HARD_REG_SET (newpat_used_regs);
2608
2609       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2610                                              &new_other_notes);
2611
2612       if (other_code_number < 0 && ! check_asm_operands (other_pat))
2613         {
2614           undo_all ();
2615           return 0;
2616         }
2617
2618       PATTERN (undobuf.other_insn) = other_pat;
2619
2620       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2621          are still valid.  Then add any non-duplicate notes added by
2622          recog_for_combine.  */
2623       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2624         {
2625           next = XEXP (note, 1);
2626
2627           if (REG_NOTE_KIND (note) == REG_UNUSED
2628               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2629             {
2630               if (REG_P (XEXP (note, 0)))
2631                 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2632
2633               remove_note (undobuf.other_insn, note);
2634             }
2635         }
2636
2637       for (note = new_other_notes; note; note = XEXP (note, 1))
2638         if (REG_P (XEXP (note, 0)))
2639           REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2640
2641       distribute_notes (new_other_notes, undobuf.other_insn,
2642                         undobuf.other_insn, NULL_RTX);
2643     }
2644 #ifdef HAVE_cc0
2645   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
2646      they are adjacent to each other or not.  */
2647   {
2648     rtx p = prev_nonnote_insn (i3);
2649     if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
2650         && sets_cc0_p (newi2pat))
2651       {
2652         undo_all ();
2653         return 0;
2654       }
2655   }
2656 #endif
2657
2658   /* Only allow this combination if insn_rtx_costs reports that the
2659      replacement instructions are cheaper than the originals.  */
2660   if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat))
2661     {
2662       undo_all ();
2663       return 0;
2664     }
2665
2666   /* We now know that we can do this combination.  Merge the insns and
2667      update the status of registers and LOG_LINKS.  */
2668
2669   if (swap_i2i3)
2670     {
2671       rtx insn;
2672       rtx link;
2673       rtx ni2dest;
2674
2675       /* I3 now uses what used to be its destination and which is now
2676          I2's destination.  This requires us to do a few adjustments.  */
2677       PATTERN (i3) = newpat;
2678       adjust_for_new_dest (i3);
2679
2680       /* We need a LOG_LINK from I3 to I2.  But we used to have one,
2681          so we still will.
2682
2683          However, some later insn might be using I2's dest and have
2684          a LOG_LINK pointing at I3.  We must remove this link.
2685          The simplest way to remove the link is to point it at I1,
2686          which we know will be a NOTE.  */
2687
2688       /* newi2pat is usually a SET here; however, recog_for_combine might
2689          have added some clobbers.  */
2690       if (GET_CODE (newi2pat) == PARALLEL)
2691         ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
2692       else
2693         ni2dest = SET_DEST (newi2pat);
2694
2695       for (insn = NEXT_INSN (i3);
2696            insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2697                     || insn != BB_HEAD (this_basic_block->next_bb));
2698            insn = NEXT_INSN (insn))
2699         {
2700           if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2701             {
2702               for (link = LOG_LINKS (insn); link;
2703                    link = XEXP (link, 1))
2704                 if (XEXP (link, 0) == i3)
2705                   XEXP (link, 0) = i1;
2706
2707               break;
2708             }
2709         }
2710     }
2711
2712   {
2713     rtx i3notes, i2notes, i1notes = 0;
2714     rtx i3links, i2links, i1links = 0;
2715     rtx midnotes = 0;
2716     unsigned int regno;
2717
2718     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2719        clear them.  */
2720     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2721     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2722     if (i1)
2723       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2724
2725     /* Ensure that we do not have something that should not be shared but
2726        occurs multiple times in the new insns.  Check this by first
2727        resetting all the `used' flags and then copying anything is shared.  */
2728
2729     reset_used_flags (i3notes);
2730     reset_used_flags (i2notes);
2731     reset_used_flags (i1notes);
2732     reset_used_flags (newpat);
2733     reset_used_flags (newi2pat);
2734     if (undobuf.other_insn)
2735       reset_used_flags (PATTERN (undobuf.other_insn));
2736
2737     i3notes = copy_rtx_if_shared (i3notes);
2738     i2notes = copy_rtx_if_shared (i2notes);
2739     i1notes = copy_rtx_if_shared (i1notes);
2740     newpat = copy_rtx_if_shared (newpat);
2741     newi2pat = copy_rtx_if_shared (newi2pat);
2742     if (undobuf.other_insn)
2743       reset_used_flags (PATTERN (undobuf.other_insn));
2744
2745     INSN_CODE (i3) = insn_code_number;
2746     PATTERN (i3) = newpat;
2747
2748     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
2749       {
2750         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2751
2752         reset_used_flags (call_usage);
2753         call_usage = copy_rtx (call_usage);
2754
2755         if (substed_i2)
2756           replace_rtx (call_usage, i2dest, i2src);
2757
2758         if (substed_i1)
2759           replace_rtx (call_usage, i1dest, i1src);
2760
2761         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2762       }
2763
2764     if (undobuf.other_insn)
2765       INSN_CODE (undobuf.other_insn) = other_code_number;
2766
2767     /* We had one special case above where I2 had more than one set and
2768        we replaced a destination of one of those sets with the destination
2769        of I3.  In that case, we have to update LOG_LINKS of insns later
2770        in this basic block.  Note that this (expensive) case is rare.
2771
2772        Also, in this case, we must pretend that all REG_NOTEs for I2
2773        actually came from I3, so that REG_UNUSED notes from I2 will be
2774        properly handled.  */
2775
2776     if (i3_subst_into_i2)
2777       {
2778         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2779           if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2780               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
2781               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2782               && ! find_reg_note (i2, REG_UNUSED,
2783                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2784             for (temp = NEXT_INSN (i2);
2785                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2786                           || BB_HEAD (this_basic_block) != temp);
2787                  temp = NEXT_INSN (temp))
2788               if (temp != i3 && INSN_P (temp))
2789                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2790                   if (XEXP (link, 0) == i2)
2791                     XEXP (link, 0) = i3;
2792
2793         if (i3notes)
2794           {
2795             rtx link = i3notes;
2796             while (XEXP (link, 1))
2797               link = XEXP (link, 1);
2798             XEXP (link, 1) = i2notes;
2799           }
2800         else
2801           i3notes = i2notes;
2802         i2notes = 0;
2803       }
2804
2805     LOG_LINKS (i3) = 0;
2806     REG_NOTES (i3) = 0;
2807     LOG_LINKS (i2) = 0;
2808     REG_NOTES (i2) = 0;
2809
2810     if (newi2pat)
2811       {
2812         INSN_CODE (i2) = i2_code_number;
2813         PATTERN (i2) = newi2pat;
2814       }
2815     else
2816       SET_INSN_DELETED (i2);
2817
2818     if (i1)
2819       {
2820         LOG_LINKS (i1) = 0;
2821         REG_NOTES (i1) = 0;
2822         SET_INSN_DELETED (i1);
2823       }
2824
2825     /* Get death notes for everything that is now used in either I3 or
2826        I2 and used to die in a previous insn.  If we built two new
2827        patterns, move from I1 to I2 then I2 to I3 so that we get the
2828        proper movement on registers that I2 modifies.  */
2829
2830     if (newi2pat)
2831       {
2832         move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2833         move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2834       }
2835     else
2836       move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2837                    i3, &midnotes);
2838
2839     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
2840     if (i3notes)
2841       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2842     if (i2notes)
2843       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2844     if (i1notes)
2845       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2846     if (midnotes)
2847       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2848
2849     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
2850        know these are REG_UNUSED and want them to go to the desired insn,
2851        so we always pass it as i3.  We have not counted the notes in
2852        reg_n_deaths yet, so we need to do so now.  */
2853
2854     if (newi2pat && new_i2_notes)
2855       {
2856         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2857           if (REG_P (XEXP (temp, 0)))
2858             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2859
2860         distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2861       }
2862
2863     if (new_i3_notes)
2864       {
2865         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2866           if (REG_P (XEXP (temp, 0)))
2867             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2868
2869         distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2870       }
2871
2872     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
2873        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
2874        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
2875        in that case, it might delete I2.  Similarly for I2 and I1.
2876        Show an additional death due to the REG_DEAD note we make here.  If
2877        we discard it in distribute_notes, we will decrement it again.  */
2878
2879     if (i3dest_killed)
2880       {
2881         if (REG_P (i3dest_killed))
2882           REG_N_DEATHS (REGNO (i3dest_killed))++;
2883
2884         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2885           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2886                                                NULL_RTX),
2887                             NULL_RTX, i2, NULL_RTX);
2888         else
2889           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2890                                                NULL_RTX),
2891                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2892       }
2893
2894     if (i2dest_in_i2src)
2895       {
2896         if (REG_P (i2dest))
2897           REG_N_DEATHS (REGNO (i2dest))++;
2898
2899         if (newi2pat && reg_set_p (i2dest, newi2pat))
2900           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2901                             NULL_RTX, i2, NULL_RTX);
2902         else
2903           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2904                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2905       }
2906
2907     if (i1dest_in_i1src)
2908       {
2909         if (REG_P (i1dest))
2910           REG_N_DEATHS (REGNO (i1dest))++;
2911
2912         if (newi2pat && reg_set_p (i1dest, newi2pat))
2913           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2914                             NULL_RTX, i2, NULL_RTX);
2915         else
2916           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2917                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2918       }
2919
2920     distribute_links (i3links);
2921     distribute_links (i2links);
2922     distribute_links (i1links);
2923
2924     if (REG_P (i2dest))
2925       {
2926         rtx link;
2927         rtx i2_insn = 0, i2_val = 0, set;
2928
2929         /* The insn that used to set this register doesn't exist, and
2930            this life of the register may not exist either.  See if one of
2931            I3's links points to an insn that sets I2DEST.  If it does,
2932            that is now the last known value for I2DEST. If we don't update
2933            this and I2 set the register to a value that depended on its old
2934            contents, we will get confused.  If this insn is used, thing
2935            will be set correctly in combine_instructions.  */
2936
2937         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2938           if ((set = single_set (XEXP (link, 0))) != 0
2939               && rtx_equal_p (i2dest, SET_DEST (set)))
2940             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2941
2942         record_value_for_reg (i2dest, i2_insn, i2_val);
2943
2944         /* If the reg formerly set in I2 died only once and that was in I3,
2945            zero its use count so it won't make `reload' do any work.  */
2946         if (! added_sets_2
2947             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2948             && ! i2dest_in_i2src)
2949           {
2950             regno = REGNO (i2dest);
2951             REG_N_SETS (regno)--;
2952           }
2953       }
2954
2955     if (i1 && REG_P (i1dest))
2956       {
2957         rtx link;
2958         rtx i1_insn = 0, i1_val = 0, set;
2959
2960         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2961           if ((set = single_set (XEXP (link, 0))) != 0
2962               && rtx_equal_p (i1dest, SET_DEST (set)))
2963             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2964
2965         record_value_for_reg (i1dest, i1_insn, i1_val);
2966
2967         regno = REGNO (i1dest);
2968         if (! added_sets_1 && ! i1dest_in_i1src)
2969           REG_N_SETS (regno)--;
2970       }
2971
2972     /* Update reg_stat[].nonzero_bits et al for any changes that may have
2973        been made to this insn.  The order of
2974        set_nonzero_bits_and_sign_copies() is important.  Because newi2pat
2975        can affect nonzero_bits of newpat */
2976     if (newi2pat)
2977       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2978     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2979
2980     /* Set new_direct_jump_p if a new return or simple jump instruction
2981        has been created.
2982
2983        If I3 is now an unconditional jump, ensure that it has a
2984        BARRIER following it since it may have initially been a
2985        conditional jump.  It may also be the last nonnote insn.  */
2986
2987     if (returnjump_p (i3) || any_uncondjump_p (i3))
2988       {
2989         *new_direct_jump_p = 1;
2990         mark_jump_label (PATTERN (i3), i3, 0);
2991
2992         if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2993             || !BARRIER_P (temp))
2994           emit_barrier_after (i3);
2995       }
2996
2997     if (undobuf.other_insn != NULL_RTX
2998         && (returnjump_p (undobuf.other_insn)
2999             || any_uncondjump_p (undobuf.other_insn)))
3000       {
3001         *new_direct_jump_p = 1;
3002
3003         if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
3004             || !BARRIER_P (temp))
3005           emit_barrier_after (undobuf.other_insn);
3006       }
3007
3008     /* An NOOP jump does not need barrier, but it does need cleaning up
3009        of CFG.  */
3010     if (GET_CODE (newpat) == SET
3011         && SET_SRC (newpat) == pc_rtx
3012         && SET_DEST (newpat) == pc_rtx)
3013       *new_direct_jump_p = 1;
3014   }
3015
3016   combine_successes++;
3017   undo_commit ();
3018
3019   if (added_links_insn
3020       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
3021       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
3022     return added_links_insn;
3023   else
3024     return newi2pat ? i2 : i3;
3025 }
3026 \f
3027 /* Undo all the modifications recorded in undobuf.  */
3028
3029 static void
3030 undo_all (void)
3031 {
3032   struct undo *undo, *next;
3033
3034   for (undo = undobuf.undos; undo; undo = next)
3035     {
3036       next = undo->next;
3037       if (undo->is_int)
3038         *undo->where.i = undo->old_contents.i;
3039       else
3040         *undo->where.r = undo->old_contents.r;
3041
3042       undo->next = undobuf.frees;
3043       undobuf.frees = undo;
3044     }
3045
3046   undobuf.undos = 0;
3047 }
3048
3049 /* We've committed to accepting the changes we made.  Move all
3050    of the undos to the free list.  */
3051
3052 static void
3053 undo_commit (void)
3054 {
3055   struct undo *undo, *next;
3056
3057   for (undo = undobuf.undos; undo; undo = next)
3058     {
3059       next = undo->next;
3060       undo->next = undobuf.frees;
3061       undobuf.frees = undo;
3062     }
3063   undobuf.undos = 0;
3064 }
3065
3066 \f
3067 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3068    where we have an arithmetic expression and return that point.  LOC will
3069    be inside INSN.
3070
3071    try_combine will call this function to see if an insn can be split into
3072    two insns.  */
3073
3074 static rtx *
3075 find_split_point (rtx *loc, rtx insn)
3076 {
3077   rtx x = *loc;
3078   enum rtx_code code = GET_CODE (x);
3079   rtx *split;
3080   unsigned HOST_WIDE_INT len = 0;
3081   HOST_WIDE_INT pos = 0;
3082   int unsignedp = 0;
3083   rtx inner = NULL_RTX;
3084
3085   /* First special-case some codes.  */
3086   switch (code)
3087     {
3088     case SUBREG:
3089 #ifdef INSN_SCHEDULING
3090       /* If we are making a paradoxical SUBREG invalid, it becomes a split
3091          point.  */
3092       if (MEM_P (SUBREG_REG (x)))
3093         return loc;
3094 #endif
3095       return find_split_point (&SUBREG_REG (x), insn);
3096
3097     case MEM:
3098 #ifdef HAVE_lo_sum
3099       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3100          using LO_SUM and HIGH.  */
3101       if (GET_CODE (XEXP (x, 0)) == CONST
3102           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3103         {
3104           SUBST (XEXP (x, 0),
3105                  gen_rtx_LO_SUM (Pmode,
3106                                  gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3107                                  XEXP (x, 0)));
3108           return &XEXP (XEXP (x, 0), 0);
3109         }
3110 #endif
3111
3112       /* If we have a PLUS whose second operand is a constant and the
3113          address is not valid, perhaps will can split it up using
3114          the machine-specific way to split large constants.  We use
3115          the first pseudo-reg (one of the virtual regs) as a placeholder;
3116          it will not remain in the result.  */
3117       if (GET_CODE (XEXP (x, 0)) == PLUS
3118           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3119           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3120         {
3121           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3122           rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
3123                                  subst_insn);
3124
3125           /* This should have produced two insns, each of which sets our
3126              placeholder.  If the source of the second is a valid address,
3127              we can make put both sources together and make a split point
3128              in the middle.  */
3129
3130           if (seq
3131               && NEXT_INSN (seq) != NULL_RTX
3132               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3133               && NONJUMP_INSN_P (seq)
3134               && GET_CODE (PATTERN (seq)) == SET
3135               && SET_DEST (PATTERN (seq)) == reg
3136               && ! reg_mentioned_p (reg,
3137                                     SET_SRC (PATTERN (seq)))
3138               && NONJUMP_INSN_P (NEXT_INSN (seq))
3139               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3140               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3141               && memory_address_p (GET_MODE (x),
3142                                    SET_SRC (PATTERN (NEXT_INSN (seq)))))
3143             {
3144               rtx src1 = SET_SRC (PATTERN (seq));
3145               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3146
3147               /* Replace the placeholder in SRC2 with SRC1.  If we can
3148                  find where in SRC2 it was placed, that can become our
3149                  split point and we can replace this address with SRC2.
3150                  Just try two obvious places.  */
3151
3152               src2 = replace_rtx (src2, reg, src1);
3153               split = 0;
3154               if (XEXP (src2, 0) == src1)
3155                 split = &XEXP (src2, 0);
3156               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3157                        && XEXP (XEXP (src2, 0), 0) == src1)
3158                 split = &XEXP (XEXP (src2, 0), 0);
3159
3160               if (split)
3161                 {
3162                   SUBST (XEXP (x, 0), src2);
3163                   return split;
3164                 }
3165             }
3166
3167           /* If that didn't work, perhaps the first operand is complex and
3168              needs to be computed separately, so make a split point there.
3169              This will occur on machines that just support REG + CONST
3170              and have a constant moved through some previous computation.  */
3171
3172           else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3173                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3174                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3175             return &XEXP (XEXP (x, 0), 0);
3176         }
3177       break;
3178
3179     case SET:
3180 #ifdef HAVE_cc0
3181       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3182          ZERO_EXTRACT, the most likely reason why this doesn't match is that
3183          we need to put the operand into a register.  So split at that
3184          point.  */
3185
3186       if (SET_DEST (x) == cc0_rtx
3187           && GET_CODE (SET_SRC (x)) != COMPARE
3188           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3189           && !OBJECT_P (SET_SRC (x))
3190           && ! (GET_CODE (SET_SRC (x)) == SUBREG
3191                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3192         return &SET_SRC (x);
3193 #endif
3194
3195       /* See if we can split SET_SRC as it stands.  */
3196       split = find_split_point (&SET_SRC (x), insn);
3197       if (split && split != &SET_SRC (x))
3198         return split;
3199
3200       /* See if we can split SET_DEST as it stands.  */
3201       split = find_split_point (&SET_DEST (x), insn);
3202       if (split && split != &SET_DEST (x))
3203         return split;
3204
3205       /* See if this is a bitfield assignment with everything constant.  If
3206          so, this is an IOR of an AND, so split it into that.  */
3207       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3208           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3209               <= HOST_BITS_PER_WIDE_INT)
3210           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3211           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3212           && GET_CODE (SET_SRC (x)) == CONST_INT
3213           && ((INTVAL (XEXP (SET_DEST (x), 1))
3214                + INTVAL (XEXP (SET_DEST (x), 2)))
3215               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3216           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3217         {
3218           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3219           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3220           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3221           rtx dest = XEXP (SET_DEST (x), 0);
3222           enum machine_mode mode = GET_MODE (dest);
3223           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3224
3225           if (BITS_BIG_ENDIAN)
3226             pos = GET_MODE_BITSIZE (mode) - len - pos;
3227
3228           if (src == mask)
3229             SUBST (SET_SRC (x),
3230                    simplify_gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3231           else
3232             {
3233               rtx negmask = gen_int_mode (~(mask << pos), mode);
3234               SUBST (SET_SRC (x),
3235                      simplify_gen_binary (IOR, mode,
3236                                           simplify_gen_binary (AND, mode,
3237                                                                dest, negmask),
3238                                           GEN_INT (src << pos)));
3239             }
3240
3241           SUBST (SET_DEST (x), dest);
3242
3243           split = find_split_point (&SET_SRC (x), insn);
3244           if (split && split != &SET_SRC (x))
3245             return split;
3246         }
3247
3248       /* Otherwise, see if this is an operation that we can split into two.
3249          If so, try to split that.  */
3250       code = GET_CODE (SET_SRC (x));
3251
3252       switch (code)
3253         {
3254         case AND:
3255           /* If we are AND'ing with a large constant that is only a single
3256              bit and the result is only being used in a context where we
3257              need to know if it is zero or nonzero, replace it with a bit
3258              extraction.  This will avoid the large constant, which might
3259              have taken more than one insn to make.  If the constant were
3260              not a valid argument to the AND but took only one insn to make,
3261              this is no worse, but if it took more than one insn, it will
3262              be better.  */
3263
3264           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3265               && REG_P (XEXP (SET_SRC (x), 0))
3266               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3267               && REG_P (SET_DEST (x))
3268               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3269               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3270               && XEXP (*split, 0) == SET_DEST (x)
3271               && XEXP (*split, 1) == const0_rtx)
3272             {
3273               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3274                                                 XEXP (SET_SRC (x), 0),
3275                                                 pos, NULL_RTX, 1, 1, 0, 0);
3276               if (extraction != 0)
3277                 {
3278                   SUBST (SET_SRC (x), extraction);
3279                   return find_split_point (loc, insn);
3280                 }
3281             }
3282           break;
3283
3284         case NE:
3285           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3286              is known to be on, this can be converted into a NEG of a shift.  */
3287           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3288               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3289               && 1 <= (pos = exact_log2
3290                        (nonzero_bits (XEXP (SET_SRC (x), 0),
3291                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
3292             {
3293               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3294
3295               SUBST (SET_SRC (x),
3296                      gen_rtx_NEG (mode,
3297                                   gen_rtx_LSHIFTRT (mode,
3298                                                     XEXP (SET_SRC (x), 0),
3299                                                     GEN_INT (pos))));
3300
3301               split = find_split_point (&SET_SRC (x), insn);
3302               if (split && split != &SET_SRC (x))
3303                 return split;
3304             }
3305           break;
3306
3307         case SIGN_EXTEND:
3308           inner = XEXP (SET_SRC (x), 0);
3309
3310           /* We can't optimize if either mode is a partial integer
3311              mode as we don't know how many bits are significant
3312              in those modes.  */
3313           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3314               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3315             break;
3316
3317           pos = 0;
3318           len = GET_MODE_BITSIZE (GET_MODE (inner));
3319           unsignedp = 0;
3320           break;
3321
3322         case SIGN_EXTRACT:
3323         case ZERO_EXTRACT:
3324           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3325               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3326             {
3327               inner = XEXP (SET_SRC (x), 0);
3328               len = INTVAL (XEXP (SET_SRC (x), 1));
3329               pos = INTVAL (XEXP (SET_SRC (x), 2));
3330
3331               if (BITS_BIG_ENDIAN)
3332                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3333               unsignedp = (code == ZERO_EXTRACT);
3334             }
3335           break;
3336
3337         default:
3338           break;
3339         }
3340
3341       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3342         {
3343           enum machine_mode mode = GET_MODE (SET_SRC (x));
3344
3345           /* For unsigned, we have a choice of a shift followed by an
3346              AND or two shifts.  Use two shifts for field sizes where the
3347              constant might be too large.  We assume here that we can
3348              always at least get 8-bit constants in an AND insn, which is
3349              true for every current RISC.  */
3350
3351           if (unsignedp && len <= 8)
3352             {
3353               SUBST (SET_SRC (x),
3354                      gen_rtx_AND (mode,
3355                                   gen_rtx_LSHIFTRT
3356                                   (mode, gen_lowpart (mode, inner),
3357                                    GEN_INT (pos)),
3358                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3359
3360               split = find_split_point (&SET_SRC (x), insn);
3361               if (split && split != &SET_SRC (x))
3362                 return split;
3363             }
3364           else
3365             {
3366               SUBST (SET_SRC (x),
3367                      gen_rtx_fmt_ee
3368                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3369                       gen_rtx_ASHIFT (mode,
3370                                       gen_lowpart (mode, inner),
3371                                       GEN_INT (GET_MODE_BITSIZE (mode)
3372                                                - len - pos)),
3373                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3374
3375               split = find_split_point (&SET_SRC (x), insn);
3376               if (split && split != &SET_SRC (x))
3377                 return split;
3378             }
3379         }
3380
3381       /* See if this is a simple operation with a constant as the second
3382          operand.  It might be that this constant is out of range and hence
3383          could be used as a split point.  */
3384       if (BINARY_P (SET_SRC (x))
3385           && CONSTANT_P (XEXP (SET_SRC (x), 1))
3386           && (OBJECT_P (XEXP (SET_SRC (x), 0))
3387               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3388                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
3389         return &XEXP (SET_SRC (x), 1);
3390
3391       /* Finally, see if this is a simple operation with its first operand
3392          not in a register.  The operation might require this operand in a
3393          register, so return it as a split point.  We can always do this
3394          because if the first operand were another operation, we would have
3395          already found it as a split point.  */
3396       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
3397           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3398         return &XEXP (SET_SRC (x), 0);
3399
3400       return 0;
3401
3402     case AND:
3403     case IOR:
3404       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3405          it is better to write this as (not (ior A B)) so we can split it.
3406          Similarly for IOR.  */
3407       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3408         {
3409           SUBST (*loc,
3410                  gen_rtx_NOT (GET_MODE (x),
3411                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3412                                               GET_MODE (x),
3413                                               XEXP (XEXP (x, 0), 0),
3414                                               XEXP (XEXP (x, 1), 0))));
3415           return find_split_point (loc, insn);
3416         }
3417
3418       /* Many RISC machines have a large set of logical insns.  If the
3419          second operand is a NOT, put it first so we will try to split the
3420          other operand first.  */
3421       if (GET_CODE (XEXP (x, 1)) == NOT)
3422         {
3423           rtx tem = XEXP (x, 0);
3424           SUBST (XEXP (x, 0), XEXP (x, 1));
3425           SUBST (XEXP (x, 1), tem);
3426         }
3427       break;
3428
3429     default:
3430       break;
3431     }
3432
3433   /* Otherwise, select our actions depending on our rtx class.  */
3434   switch (GET_RTX_CLASS (code))
3435     {
3436     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
3437     case RTX_TERNARY:
3438       split = find_split_point (&XEXP (x, 2), insn);
3439       if (split)
3440         return split;
3441       /* ... fall through ...  */
3442     case RTX_BIN_ARITH:
3443     case RTX_COMM_ARITH:
3444     case RTX_COMPARE:
3445     case RTX_COMM_COMPARE:
3446       split = find_split_point (&XEXP (x, 1), insn);
3447       if (split)
3448         return split;
3449       /* ... fall through ...  */
3450     case RTX_UNARY:
3451       /* Some machines have (and (shift ...) ...) insns.  If X is not
3452          an AND, but XEXP (X, 0) is, use it as our split point.  */
3453       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3454         return &XEXP (x, 0);
3455
3456       split = find_split_point (&XEXP (x, 0), insn);
3457       if (split)
3458         return split;
3459       return loc;
3460
3461     default:
3462       /* Otherwise, we don't have a split point.  */
3463       return 0;
3464     }
3465 }
3466 \f
3467 /* Throughout X, replace FROM with TO, and return the result.
3468    The result is TO if X is FROM;
3469    otherwise the result is X, but its contents may have been modified.
3470    If they were modified, a record was made in undobuf so that
3471    undo_all will (among other things) return X to its original state.
3472
3473    If the number of changes necessary is too much to record to undo,
3474    the excess changes are not made, so the result is invalid.
3475    The changes already made can still be undone.
3476    undobuf.num_undo is incremented for such changes, so by testing that
3477    the caller can tell whether the result is valid.
3478
3479    `n_occurrences' is incremented each time FROM is replaced.
3480
3481    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3482
3483    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
3484    by copying if `n_occurrences' is nonzero.  */
3485
3486 static rtx
3487 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3488 {
3489   enum rtx_code code = GET_CODE (x);
3490   enum machine_mode op0_mode = VOIDmode;
3491   const char *fmt;
3492   int len, i;
3493   rtx new;
3494
3495 /* Two expressions are equal if they are identical copies of a shared
3496    RTX or if they are both registers with the same register number
3497    and mode.  */
3498
3499 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
3500   ((X) == (Y)                                           \
3501    || (REG_P (X) && REG_P (Y)   \
3502        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3503
3504   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3505     {
3506       n_occurrences++;
3507       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3508     }
3509
3510   /* If X and FROM are the same register but different modes, they will
3511      not have been seen as equal above.  However, flow.c will make a
3512      LOG_LINKS entry for that case.  If we do nothing, we will try to
3513      rerecognize our original insn and, when it succeeds, we will
3514      delete the feeding insn, which is incorrect.
3515
3516      So force this insn not to match in this (rare) case.  */
3517   if (! in_dest && code == REG && REG_P (from)
3518       && REGNO (x) == REGNO (from))
3519     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3520
3521   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3522      of which may contain things that can be combined.  */
3523   if (code != MEM && code != LO_SUM && OBJECT_P (x))
3524     return x;
3525
3526   /* It is possible to have a subexpression appear twice in the insn.
3527      Suppose that FROM is a register that appears within TO.
3528      Then, after that subexpression has been scanned once by `subst',
3529      the second time it is scanned, TO may be found.  If we were
3530      to scan TO here, we would find FROM within it and create a
3531      self-referent rtl structure which is completely wrong.  */
3532   if (COMBINE_RTX_EQUAL_P (x, to))
3533     return to;
3534
3535   /* Parallel asm_operands need special attention because all of the
3536      inputs are shared across the arms.  Furthermore, unsharing the
3537      rtl results in recognition failures.  Failure to handle this case
3538      specially can result in circular rtl.
3539
3540      Solve this by doing a normal pass across the first entry of the
3541      parallel, and only processing the SET_DESTs of the subsequent
3542      entries.  Ug.  */
3543
3544   if (code == PARALLEL
3545       && GET_CODE (XVECEXP (x, 0, 0)) == SET
3546       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3547     {
3548       new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3549
3550       /* If this substitution failed, this whole thing fails.  */
3551       if (GET_CODE (new) == CLOBBER
3552           && XEXP (new, 0) == const0_rtx)
3553         return new;
3554
3555       SUBST (XVECEXP (x, 0, 0), new);
3556
3557       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3558         {
3559           rtx dest = SET_DEST (XVECEXP (x, 0, i));
3560
3561           if (!REG_P (dest)
3562               && GET_CODE (dest) != CC0
3563               && GET_CODE (dest) != PC)
3564             {
3565               new = subst (dest, from, to, 0, unique_copy);
3566
3567               /* If this substitution failed, this whole thing fails.  */
3568               if (GET_CODE (new) == CLOBBER
3569                   && XEXP (new, 0) == const0_rtx)
3570                 return new;
3571
3572               SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3573             }
3574         }
3575     }
3576   else
3577     {
3578       len = GET_RTX_LENGTH (code);
3579       fmt = GET_RTX_FORMAT (code);
3580
3581       /* We don't need to process a SET_DEST that is a register, CC0,
3582          or PC, so set up to skip this common case.  All other cases
3583          where we want to suppress replacing something inside a
3584          SET_SRC are handled via the IN_DEST operand.  */
3585       if (code == SET
3586           && (REG_P (SET_DEST (x))
3587               || GET_CODE (SET_DEST (x)) == CC0
3588               || GET_CODE (SET_DEST (x)) == PC))
3589         fmt = "ie";
3590
3591       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3592          constant.  */
3593       if (fmt[0] == 'e')
3594         op0_mode = GET_MODE (XEXP (x, 0));
3595
3596       for (i = 0; i < len; i++)
3597         {
3598           if (fmt[i] == 'E')
3599             {
3600               int j;
3601               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3602                 {
3603                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3604                     {
3605                       new = (unique_copy && n_occurrences
3606                              ? copy_rtx (to) : to);
3607                       n_occurrences++;
3608                     }
3609                   else
3610                     {
3611                       new = subst (XVECEXP (x, i, j), from, to, 0,
3612                                    unique_copy);
3613
3614                       /* If this substitution failed, this whole thing
3615                          fails.  */
3616                       if (GET_CODE (new) == CLOBBER
3617                           && XEXP (new, 0) == const0_rtx)
3618                         return new;
3619                     }
3620
3621                   SUBST (XVECEXP (x, i, j), new);
3622                 }
3623             }
3624           else if (fmt[i] == 'e')
3625             {
3626               /* If this is a register being set, ignore it.  */
3627               new = XEXP (x, i);
3628               if (in_dest
3629                   && i == 0
3630                   && (((code == SUBREG || code == ZERO_EXTRACT)
3631                        && REG_P (new))
3632                       || code == STRICT_LOW_PART))
3633                 ;
3634
3635               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3636                 {
3637                   /* In general, don't install a subreg involving two
3638                      modes not tieable.  It can worsen register
3639                      allocation, and can even make invalid reload
3640                      insns, since the reg inside may need to be copied
3641                      from in the outside mode, and that may be invalid
3642                      if it is an fp reg copied in integer mode.
3643
3644                      We allow two exceptions to this: It is valid if
3645                      it is inside another SUBREG and the mode of that
3646                      SUBREG and the mode of the inside of TO is
3647                      tieable and it is valid if X is a SET that copies
3648                      FROM to CC0.  */
3649
3650                   if (GET_CODE (to) == SUBREG
3651                       && ! MODES_TIEABLE_P (GET_MODE (to),
3652                                             GET_MODE (SUBREG_REG (to)))
3653                       && ! (code == SUBREG
3654                             && MODES_TIEABLE_P (GET_MODE (x),
3655                                                 GET_MODE (SUBREG_REG (to))))
3656 #ifdef HAVE_cc0
3657                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3658 #endif
3659                       )
3660                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3661
3662 #ifdef CANNOT_CHANGE_MODE_CLASS
3663                   if (code == SUBREG
3664                       && REG_P (to)
3665                       && REGNO (to) < FIRST_PSEUDO_REGISTER
3666                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3667                                                    GET_MODE (to),
3668                                                    GET_MODE (x)))
3669                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3670 #endif
3671
3672                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3673                   n_occurrences++;
3674                 }
3675               else
3676                 /* If we are in a SET_DEST, suppress most cases unless we
3677                    have gone inside a MEM, in which case we want to
3678                    simplify the address.  We assume here that things that
3679                    are actually part of the destination have their inner
3680                    parts in the first expression.  This is true for SUBREG,
3681                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3682                    things aside from REG and MEM that should appear in a
3683                    SET_DEST.  */
3684                 new = subst (XEXP (x, i), from, to,
3685                              (((in_dest
3686                                 && (code == SUBREG || code == STRICT_LOW_PART
3687                                     || code == ZERO_EXTRACT))
3688                                || code == SET)
3689                               && i == 0), unique_copy);
3690
3691               /* If we found that we will have to reject this combination,
3692                  indicate that by returning the CLOBBER ourselves, rather than
3693                  an expression containing it.  This will speed things up as
3694                  well as prevent accidents where two CLOBBERs are considered
3695                  to be equal, thus producing an incorrect simplification.  */
3696
3697               if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3698                 return new;
3699
3700               if (GET_CODE (x) == SUBREG
3701                   && (GET_CODE (new) == CONST_INT
3702                       || GET_CODE (new) == CONST_DOUBLE))
3703                 {
3704                   enum machine_mode mode = GET_MODE (x);
3705
3706                   x = simplify_subreg (GET_MODE (x), new,
3707                                        GET_MODE (SUBREG_REG (x)),
3708                                        SUBREG_BYTE (x));
3709                   if (! x)
3710                     x = gen_rtx_CLOBBER (mode, const0_rtx);
3711                 }
3712               else if (GET_CODE (new) == CONST_INT
3713                        && GET_CODE (x) == ZERO_EXTEND)
3714                 {
3715                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3716                                                 new, GET_MODE (XEXP (x, 0)));
3717                   gcc_assert (x);
3718                 }
3719               else
3720                 SUBST (XEXP (x, i), new);
3721             }
3722         }
3723     }
3724
3725   /* Try to simplify X.  If the simplification changed the code, it is likely
3726      that further simplification will help, so loop, but limit the number
3727      of repetitions that will be performed.  */
3728
3729   for (i = 0; i < 4; i++)
3730     {
3731       /* If X is sufficiently simple, don't bother trying to do anything
3732          with it.  */
3733       if (code != CONST_INT && code != REG && code != CLOBBER)
3734         x = combine_simplify_rtx (x, op0_mode, in_dest);
3735
3736       if (GET_CODE (x) == code)
3737         break;
3738
3739       code = GET_CODE (x);
3740
3741       /* We no longer know the original mode of operand 0 since we
3742          have changed the form of X)  */
3743       op0_mode = VOIDmode;
3744     }
3745
3746   return x;
3747 }
3748 \f
3749 /* Simplify X, a piece of RTL.  We just operate on the expression at the
3750    outer level; call `subst' to simplify recursively.  Return the new
3751    expression.
3752
3753    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
3754    if we are inside a SET_DEST.  */
3755
3756 static rtx
3757 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
3758 {
3759   enum rtx_code code = GET_CODE (x);
3760   enum machine_mode mode = GET_MODE (x);
3761   rtx temp;
3762   rtx reversed;
3763   int i;
3764
3765   /* If this is a commutative operation, put a constant last and a complex
3766      expression first.  We don't need to do this for comparisons here.  */
3767   if (COMMUTATIVE_ARITH_P (x)
3768       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3769     {
3770       temp = XEXP (x, 0);
3771       SUBST (XEXP (x, 0), XEXP (x, 1));
3772       SUBST (XEXP (x, 1), temp);
3773     }
3774
3775   /* If this is a simple operation applied to an IF_THEN_ELSE, try
3776      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
3777      things.  Check for cases where both arms are testing the same
3778      condition.
3779
3780      Don't do anything if all operands are very simple.  */
3781
3782   if ((BINARY_P (x)
3783        && ((!OBJECT_P (XEXP (x, 0))
3784             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3785                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
3786            || (!OBJECT_P (XEXP (x, 1))
3787                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3788                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
3789       || (UNARY_P (x)
3790           && (!OBJECT_P (XEXP (x, 0))
3791                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3792                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
3793     {
3794       rtx cond, true_rtx, false_rtx;
3795
3796       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3797       if (cond != 0
3798           /* If everything is a comparison, what we have is highly unlikely
3799              to be simpler, so don't use it.  */
3800           && ! (COMPARISON_P (x)
3801                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
3802         {
3803           rtx cop1 = const0_rtx;
3804           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3805
3806           if (cond_code == NE && COMPARISON_P (cond))
3807             return x;
3808
3809           /* Simplify the alternative arms; this may collapse the true and
3810              false arms to store-flag values.  Be careful to use copy_rtx
3811              here since true_rtx or false_rtx might share RTL with x as a
3812              result of the if_then_else_cond call above.  */
3813           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
3814           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
3815
3816           /* If true_rtx and false_rtx are not general_operands, an if_then_else
3817              is unlikely to be simpler.  */
3818           if (general_operand (true_rtx, VOIDmode)
3819               && general_operand (false_rtx, VOIDmode))
3820             {
3821               enum rtx_code reversed;
3822
3823               /* Restarting if we generate a store-flag expression will cause
3824                  us to loop.  Just drop through in this case.  */
3825
3826               /* If the result values are STORE_FLAG_VALUE and zero, we can
3827                  just make the comparison operation.  */
3828               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3829                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
3830                                              cond, cop1);
3831               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3832                        && ((reversed = reversed_comparison_code_parts
3833                                         (cond_code, cond, cop1, NULL))
3834                            != UNKNOWN))
3835                 x = simplify_gen_relational (reversed, mode, VOIDmode,
3836                                              cond, cop1);
3837
3838               /* Likewise, we can make the negate of a comparison operation
3839                  if the result values are - STORE_FLAG_VALUE and zero.  */
3840               else if (GET_CODE (true_rtx) == CONST_INT
3841                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3842                        && false_rtx == const0_rtx)
3843                 x = simplify_gen_unary (NEG, mode,
3844                                         simplify_gen_relational (cond_code,
3845                                                                  mode, VOIDmode,
3846                                                                  cond, cop1),
3847                                         mode);
3848               else if (GET_CODE (false_rtx) == CONST_INT
3849                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3850                        && true_rtx == const0_rtx
3851                        && ((reversed = reversed_comparison_code_parts
3852                                         (cond_code, cond, cop1, NULL))
3853                            != UNKNOWN))
3854                 x = simplify_gen_unary (NEG, mode,
3855                                         simplify_gen_relational (reversed,
3856                                                                  mode, VOIDmode,
3857                                                                  cond, cop1),
3858                                         mode);
3859               else
3860                 return gen_rtx_IF_THEN_ELSE (mode,
3861                                              simplify_gen_relational (cond_code,
3862                                                                       mode,
3863                                                                       VOIDmode,
3864                                                                       cond,
3865                                                                       cop1),
3866                                              true_rtx, false_rtx);
3867
3868               code = GET_CODE (x);
3869               op0_mode = VOIDmode;
3870             }
3871         }
3872     }
3873
3874   /* Try to fold this expression in case we have constants that weren't
3875      present before.  */
3876   temp = 0;
3877   switch (GET_RTX_CLASS (code))
3878     {
3879     case RTX_UNARY:
3880       if (op0_mode == VOIDmode)
3881         op0_mode = GET_MODE (XEXP (x, 0));
3882       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3883       break;
3884     case RTX_COMPARE:
3885     case RTX_COMM_COMPARE:
3886       {
3887         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3888         if (cmp_mode == VOIDmode)
3889           {
3890             cmp_mode = GET_MODE (XEXP (x, 1));
3891             if (cmp_mode == VOIDmode)
3892               cmp_mode = op0_mode;
3893           }
3894         temp = simplify_relational_operation (code, mode, cmp_mode,
3895                                               XEXP (x, 0), XEXP (x, 1));
3896       }
3897       break;
3898     case RTX_COMM_ARITH:
3899     case RTX_BIN_ARITH:
3900       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3901       break;
3902     case RTX_BITFIELD_OPS:
3903     case RTX_TERNARY:
3904       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3905                                          XEXP (x, 1), XEXP (x, 2));
3906       break;
3907     default:
3908       break;
3909     }
3910
3911   if (temp)
3912     {
3913       x = temp;
3914       code = GET_CODE (temp);
3915       op0_mode = VOIDmode;
3916       mode = GET_MODE (temp);
3917     }
3918
3919   /* First see if we can apply the inverse distributive law.  */
3920   if (code == PLUS || code == MINUS
3921       || code == AND || code == IOR || code == XOR)
3922     {
3923       x = apply_distributive_law (x);
3924       code = GET_CODE (x);
3925       op0_mode = VOIDmode;
3926     }
3927
3928   /* If CODE is an associative operation not otherwise handled, see if we
3929      can associate some operands.  This can win if they are constants or
3930      if they are logically related (i.e. (a & b) & a).  */
3931   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3932        || code == AND || code == IOR || code == XOR
3933        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3934       && ((INTEGRAL_MODE_P (mode) && code != DIV)
3935           || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3936     {
3937       if (GET_CODE (XEXP (x, 0)) == code)
3938         {
3939           rtx other = XEXP (XEXP (x, 0), 0);
3940           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3941           rtx inner_op1 = XEXP (x, 1);
3942           rtx inner;
3943
3944           /* Make sure we pass the constant operand if any as the second
3945              one if this is a commutative operation.  */
3946           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
3947             {
3948               rtx tem = inner_op0;
3949               inner_op0 = inner_op1;
3950               inner_op1 = tem;
3951             }
3952           inner = simplify_binary_operation (code == MINUS ? PLUS
3953                                              : code == DIV ? MULT
3954                                              : code,
3955                                              mode, inner_op0, inner_op1);
3956
3957           /* For commutative operations, try the other pair if that one
3958              didn't simplify.  */
3959           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
3960             {
3961               other = XEXP (XEXP (x, 0), 1);
3962               inner = simplify_binary_operation (code, mode,
3963                                                  XEXP (XEXP (x, 0), 0),
3964                                                  XEXP (x, 1));
3965             }
3966
3967           if (inner)
3968             return simplify_gen_binary (code, mode, other, inner);
3969         }
3970     }
3971
3972   /* A little bit of algebraic simplification here.  */
3973   switch (code)
3974     {
3975     case MEM:
3976       /* Ensure that our address has any ASHIFTs converted to MULT in case
3977          address-recognizing predicates are called later.  */
3978       temp = make_compound_operation (XEXP (x, 0), MEM);
3979       SUBST (XEXP (x, 0), temp);
3980       break;
3981
3982     case SUBREG:
3983       if (op0_mode == VOIDmode)
3984         op0_mode = GET_MODE (SUBREG_REG (x));
3985
3986       /* See if this can be moved to simplify_subreg.  */
3987       if (CONSTANT_P (SUBREG_REG (x))
3988           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
3989              /* Don't call gen_lowpart if the inner mode
3990                 is VOIDmode and we cannot simplify it, as SUBREG without
3991                 inner mode is invalid.  */
3992           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
3993               || gen_lowpart_common (mode, SUBREG_REG (x))))
3994         return gen_lowpart (mode, SUBREG_REG (x));
3995
3996       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
3997         break;
3998       {
3999         rtx temp;
4000         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4001                                 SUBREG_BYTE (x));
4002         if (temp)
4003           return temp;
4004       }
4005
4006       /* Don't change the mode of the MEM if that would change the meaning
4007          of the address.  */
4008       if (MEM_P (SUBREG_REG (x))
4009           && (MEM_VOLATILE_P (SUBREG_REG (x))
4010               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4011         return gen_rtx_CLOBBER (mode, const0_rtx);
4012
4013       /* Note that we cannot do any narrowing for non-constants since
4014          we might have been counting on using the fact that some bits were
4015          zero.  We now do this in the SET.  */
4016
4017       break;
4018
4019     case NOT:
4020       if (GET_CODE (XEXP (x, 0)) == SUBREG
4021           && subreg_lowpart_p (XEXP (x, 0))
4022           && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
4023               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
4024           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
4025           && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
4026         {
4027           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
4028
4029           x = gen_rtx_ROTATE (inner_mode,
4030                               simplify_gen_unary (NOT, inner_mode, const1_rtx,
4031                                                   inner_mode),
4032                               XEXP (SUBREG_REG (XEXP (x, 0)), 1));
4033           return gen_lowpart (mode, x);
4034         }
4035
4036       /* Apply De Morgan's laws to reduce number of patterns for machines
4037          with negating logical insns (and-not, nand, etc.).  If result has
4038          only one NOT, put it first, since that is how the patterns are
4039          coded.  */
4040
4041       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
4042         {
4043           rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
4044           enum machine_mode op_mode;
4045
4046           op_mode = GET_MODE (in1);
4047           in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
4048
4049           op_mode = GET_MODE (in2);
4050           if (op_mode == VOIDmode)
4051             op_mode = mode;
4052           in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
4053
4054           if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
4055             {
4056               rtx tem = in2;
4057               in2 = in1; in1 = tem;
4058             }
4059
4060           return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
4061                                  mode, in1, in2);
4062         }
4063       break;
4064
4065     case NEG:
4066       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1.  */
4067       if (GET_CODE (XEXP (x, 0)) == XOR
4068           && XEXP (XEXP (x, 0), 1) == const1_rtx
4069           && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
4070         return simplify_gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4071                                     constm1_rtx);
4072
4073       temp = expand_compound_operation (XEXP (x, 0));
4074
4075       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4076          replaced by (lshiftrt X C).  This will convert
4077          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
4078
4079       if (GET_CODE (temp) == ASHIFTRT
4080           && GET_CODE (XEXP (temp, 1)) == CONST_INT
4081           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4082         return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
4083                                      INTVAL (XEXP (temp, 1)));
4084
4085       /* If X has only a single bit that might be nonzero, say, bit I, convert
4086          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4087          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
4088          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
4089          or a SUBREG of one since we'd be making the expression more
4090          complex if it was just a register.  */
4091
4092       if (!REG_P (temp)
4093           && ! (GET_CODE (temp) == SUBREG
4094                 && REG_P (SUBREG_REG (temp)))
4095           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4096         {
4097           rtx temp1 = simplify_shift_const
4098             (NULL_RTX, ASHIFTRT, mode,
4099              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4100                                    GET_MODE_BITSIZE (mode) - 1 - i),
4101              GET_MODE_BITSIZE (mode) - 1 - i);
4102
4103           /* If all we did was surround TEMP with the two shifts, we
4104              haven't improved anything, so don't use it.  Otherwise,
4105              we are better off with TEMP1.  */
4106           if (GET_CODE (temp1) != ASHIFTRT
4107               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4108               || XEXP (XEXP (temp1, 0), 0) != temp)
4109             return temp1;
4110         }
4111       break;
4112
4113     case TRUNCATE:
4114       /* We can't handle truncation to a partial integer mode here
4115          because we don't know the real bitsize of the partial
4116          integer mode.  */
4117       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4118         break;
4119
4120       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4121           && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4122                                     GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4123         SUBST (XEXP (x, 0),
4124                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4125                               GET_MODE_MASK (mode), NULL_RTX, 0));
4126
4127       /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI.  */
4128       if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4129            || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4130           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4131         return XEXP (XEXP (x, 0), 0);
4132
4133       /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
4134          (OP:SI foo:SI) if OP is NEG or ABS.  */
4135       if ((GET_CODE (XEXP (x, 0)) == ABS
4136            || GET_CODE (XEXP (x, 0)) == NEG)
4137           && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
4138               || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
4139           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4140         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4141                                    XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4142
4143       /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
4144          (truncate:SI x).  */
4145       if (GET_CODE (XEXP (x, 0)) == SUBREG
4146           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
4147           && subreg_lowpart_p (XEXP (x, 0)))
4148         return SUBREG_REG (XEXP (x, 0));
4149
4150       /* If we know that the value is already truncated, we can
4151          replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
4152          is nonzero for the corresponding modes.  But don't do this
4153          for an (LSHIFTRT (MULT ...)) since this will cause problems
4154          with the umulXi3_highpart patterns.  */
4155       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4156                                  GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4157           && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4158              >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
4159           && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4160                 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4161         return gen_lowpart (mode, XEXP (x, 0));
4162
4163       /* A truncate of a comparison can be replaced with a subreg if
4164          STORE_FLAG_VALUE permits.  This is like the previous test,
4165          but it works even if the comparison is done in a mode larger
4166          than HOST_BITS_PER_WIDE_INT.  */
4167       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4168           && COMPARISON_P (XEXP (x, 0))
4169           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4170         return gen_lowpart (mode, XEXP (x, 0));
4171
4172       /* Similarly, a truncate of a register whose value is a
4173          comparison can be replaced with a subreg if STORE_FLAG_VALUE
4174          permits.  */
4175       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4176           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4177           && (temp = get_last_value (XEXP (x, 0)))
4178           && COMPARISON_P (temp))
4179         return gen_lowpart (mode, XEXP (x, 0));
4180
4181       break;
4182
4183     case FLOAT_TRUNCATE:
4184       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
4185       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4186           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4187         return XEXP (XEXP (x, 0), 0);
4188
4189       /* (float_truncate:SF (float_truncate:DF foo:XF))
4190          = (float_truncate:SF foo:XF).
4191          This may eliminate double rounding, so it is unsafe.
4192
4193          (float_truncate:SF (float_extend:XF foo:DF))
4194          = (float_truncate:SF foo:DF).
4195
4196          (float_truncate:DF (float_extend:XF foo:SF))
4197          = (float_extend:SF foo:DF).  */
4198       if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4199            && flag_unsafe_math_optimizations)
4200           || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4201         return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4202                                                             0)))
4203                                    > GET_MODE_SIZE (mode)
4204                                    ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4205                                    mode,
4206                                    XEXP (XEXP (x, 0), 0), mode);
4207
4208       /*  (float_truncate (float x)) is (float x)  */
4209       if (GET_CODE (XEXP (x, 0)) == FLOAT
4210           && (flag_unsafe_math_optimizations
4211               || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4212                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4213                       - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4214                                              GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4215         return simplify_gen_unary (FLOAT, mode,
4216                                    XEXP (XEXP (x, 0), 0),
4217                                    GET_MODE (XEXP (XEXP (x, 0), 0)));
4218
4219       /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4220          (OP:SF foo:SF) if OP is NEG or ABS.  */
4221       if ((GET_CODE (XEXP (x, 0)) == ABS
4222            || GET_CODE (XEXP (x, 0)) == NEG)
4223           && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4224           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4225         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4226                                    XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4227
4228       /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4229          is (float_truncate:SF x).  */
4230       if (GET_CODE (XEXP (x, 0)) == SUBREG
4231           && subreg_lowpart_p (XEXP (x, 0))
4232           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4233         return SUBREG_REG (XEXP (x, 0));
4234       break;
4235     case FLOAT_EXTEND:
4236       /*  (float_extend (float_extend x)) is (float_extend x)
4237
4238           (float_extend (float x)) is (float x) assuming that double
4239           rounding can't happen.
4240           */
4241       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4242           || (GET_CODE (XEXP (x, 0)) == FLOAT
4243               && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4244                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4245                       - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4246                                              GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4247         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4248                                    XEXP (XEXP (x, 0), 0),
4249                                    GET_MODE (XEXP (XEXP (x, 0), 0)));
4250
4251       break;
4252 #ifdef HAVE_cc0
4253     case COMPARE:
4254       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4255          using cc0, in which case we want to leave it as a COMPARE
4256          so we can distinguish it from a register-register-copy.  */
4257       if (XEXP (x, 1) == const0_rtx)
4258         return XEXP (x, 0);
4259
4260       /* x - 0 is the same as x unless x's mode has signed zeros and
4261          allows rounding towards -infinity.  Under those conditions,
4262          0 - 0 is -0.  */
4263       if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4264             && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4265           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4266         return XEXP (x, 0);
4267       break;
4268 #endif
4269
4270     case CONST:
4271       /* (const (const X)) can become (const X).  Do it this way rather than
4272          returning the inner CONST since CONST can be shared with a
4273          REG_EQUAL note.  */
4274       if (GET_CODE (XEXP (x, 0)) == CONST)
4275         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4276       break;
4277
4278 #ifdef HAVE_lo_sum
4279     case LO_SUM:
4280       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
4281          can add in an offset.  find_split_point will split this address up
4282          again if it doesn't match.  */
4283       if (GET_CODE (XEXP (x, 0)) == HIGH
4284           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4285         return XEXP (x, 1);
4286       break;
4287 #endif
4288
4289     case PLUS:
4290       /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4291        */
4292       if (GET_CODE (XEXP (x, 0)) == MULT
4293           && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4294         {
4295           rtx in1, in2;
4296
4297           in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4298           in2 = XEXP (XEXP (x, 0), 1);
4299           return simplify_gen_binary (MINUS, mode, XEXP (x, 1),
4300                                       simplify_gen_binary (MULT, mode,
4301                                                            in1, in2));
4302         }
4303
4304       /* If we have (plus (plus (A const) B)), associate it so that CONST is
4305          outermost.  That's because that's the way indexed addresses are
4306          supposed to appear.  This code used to check many more cases, but
4307          they are now checked elsewhere.  */
4308       if (GET_CODE (XEXP (x, 0)) == PLUS
4309           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4310         return simplify_gen_binary (PLUS, mode,
4311                                     simplify_gen_binary (PLUS, mode,
4312                                                          XEXP (XEXP (x, 0), 0),
4313                                                          XEXP (x, 1)),
4314                                     XEXP (XEXP (x, 0), 1));
4315
4316       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4317          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4318          bit-field and can be replaced by either a sign_extend or a
4319          sign_extract.  The `and' may be a zero_extend and the two
4320          <c>, -<c> constants may be reversed.  */
4321       if (GET_CODE (XEXP (x, 0)) == XOR
4322           && GET_CODE (XEXP (x, 1)) == CONST_INT
4323           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4324           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4325           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4326               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4327           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4328           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4329                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4330                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4331                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4332               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4333                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4334                       == (unsigned int) i + 1))))
4335         return simplify_shift_const
4336           (NULL_RTX, ASHIFTRT, mode,
4337            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4338                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
4339                                  GET_MODE_BITSIZE (mode) - (i + 1)),
4340            GET_MODE_BITSIZE (mode) - (i + 1));
4341
4342       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4343          C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4344          is 1.  This produces better code than the alternative immediately
4345          below.  */
4346       if (COMPARISON_P (XEXP (x, 0))
4347           && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4348               || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4349           && (reversed = reversed_comparison (XEXP (x, 0), mode)))
4350         return
4351           simplify_gen_unary (NEG, mode, reversed, mode);
4352
4353       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4354          can become (ashiftrt (ashift (xor x 1) C) C) where C is
4355          the bitsize of the mode - 1.  This allows simplification of
4356          "a = (b & 8) == 0;"  */
4357       if (XEXP (x, 1) == constm1_rtx
4358           && !REG_P (XEXP (x, 0))
4359           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4360                 && REG_P (SUBREG_REG (XEXP (x, 0))))
4361           && nonzero_bits (XEXP (x, 0), mode) == 1)
4362         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4363            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4364                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4365                                  GET_MODE_BITSIZE (mode) - 1),
4366            GET_MODE_BITSIZE (mode) - 1);
4367
4368       /* If we are adding two things that have no bits in common, convert
4369          the addition into an IOR.  This will often be further simplified,
4370          for example in cases like ((a & 1) + (a & 2)), which can
4371          become a & 3.  */
4372
4373       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4374           && (nonzero_bits (XEXP (x, 0), mode)
4375               & nonzero_bits (XEXP (x, 1), mode)) == 0)
4376         {
4377           /* Try to simplify the expression further.  */
4378           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4379           temp = combine_simplify_rtx (tor, mode, in_dest);
4380
4381           /* If we could, great.  If not, do not go ahead with the IOR
4382              replacement, since PLUS appears in many special purpose
4383              address arithmetic instructions.  */
4384           if (GET_CODE (temp) != CLOBBER && temp != tor)
4385             return temp;
4386         }
4387       break;
4388
4389     case MINUS:
4390       /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4391          by reversing the comparison code if valid.  */
4392       if (STORE_FLAG_VALUE == 1
4393           && XEXP (x, 0) == const1_rtx
4394           && COMPARISON_P (XEXP (x, 1))
4395           && (reversed = reversed_comparison (XEXP (x, 1), mode)))
4396         return reversed;
4397
4398       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4399          (and <foo> (const_int pow2-1))  */
4400       if (GET_CODE (XEXP (x, 1)) == AND
4401           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4402           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4403           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4404         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4405                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4406
4407       /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4408        */
4409       if (GET_CODE (XEXP (x, 1)) == MULT
4410           && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4411         {
4412           rtx in1, in2;
4413
4414           in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4415           in2 = XEXP (XEXP (x, 1), 1);
4416           return simplify_gen_binary (PLUS, mode,
4417                                       simplify_gen_binary (MULT, mode,
4418                                                            in1, in2),
4419                                       XEXP (x, 0));
4420         }
4421
4422       /* Canonicalize (minus (neg A) (mult B C)) to
4423          (minus (mult (neg B) C) A).  */
4424       if (GET_CODE (XEXP (x, 1)) == MULT
4425           && GET_CODE (XEXP (x, 0)) == NEG)
4426         {
4427           rtx in1, in2;
4428
4429           in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4430           in2 = XEXP (XEXP (x, 1), 1);
4431           return simplify_gen_binary (MINUS, mode,
4432                                       simplify_gen_binary (MULT, mode,
4433                                                            in1, in2),
4434                                       XEXP (XEXP (x, 0), 0));
4435         }
4436
4437       /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4438          integers.  */
4439       if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4440         return simplify_gen_binary (MINUS, mode,
4441                                     simplify_gen_binary (MINUS, mode,
4442                                                          XEXP (x, 0),
4443                                                          XEXP (XEXP (x, 1), 0)),
4444                                     XEXP (XEXP (x, 1), 1));
4445       break;
4446
4447     case MULT:
4448       /* If we have (mult (plus A B) C), apply the distributive law and then
4449          the inverse distributive law to see if things simplify.  This
4450          occurs mostly in addresses, often when unrolling loops.  */
4451
4452       if (GET_CODE (XEXP (x, 0)) == PLUS)
4453         {
4454           rtx result = distribute_and_simplify_rtx (x, 0);
4455           if (result)
4456             return result;
4457         }
4458
4459       /* Try simplify a*(b/c) as (a*b)/c.  */
4460       if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4461           && GET_CODE (XEXP (x, 0)) == DIV)
4462         {
4463           rtx tem = simplify_binary_operation (MULT, mode,
4464                                                XEXP (XEXP (x, 0), 0),
4465                                                XEXP (x, 1));
4466           if (tem)
4467             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4468         }
4469       break;
4470
4471     case UDIV:
4472       /* If this is a divide by a power of two, treat it as a shift if
4473          its first operand is a shift.  */
4474       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4475           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4476           && (GET_CODE (XEXP (x, 0)) == ASHIFT
4477               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4478               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4479               || GET_CODE (XEXP (x, 0)) == ROTATE
4480               || GET_CODE (XEXP (x, 0)) == ROTATERT))
4481         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4482       break;
4483
4484     case EQ:  case NE:
4485     case GT:  case GTU:  case GE:  case GEU:
4486     case LT:  case LTU:  case LE:  case LEU:
4487     case UNEQ:  case LTGT:
4488     case UNGT:  case UNGE:
4489     case UNLT:  case UNLE:
4490     case UNORDERED: case ORDERED:
4491       /* If the first operand is a condition code, we can't do anything
4492          with it.  */
4493       if (GET_CODE (XEXP (x, 0)) == COMPARE
4494           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4495               && ! CC0_P (XEXP (x, 0))))
4496         {
4497           rtx op0 = XEXP (x, 0);
4498           rtx op1 = XEXP (x, 1);
4499           enum rtx_code new_code;
4500
4501           if (GET_CODE (op0) == COMPARE)
4502             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4503
4504           /* Simplify our comparison, if possible.  */
4505           new_code = simplify_comparison (code, &op0, &op1);
4506
4507           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4508              if only the low-order bit is possibly nonzero in X (such as when
4509              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
4510              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
4511              known to be either 0 or -1, NE becomes a NEG and EQ becomes
4512              (plus X 1).
4513
4514              Remove any ZERO_EXTRACT we made when thinking this was a
4515              comparison.  It may now be simpler to use, e.g., an AND.  If a
4516              ZERO_EXTRACT is indeed appropriate, it will be placed back by
4517              the call to make_compound_operation in the SET case.  */
4518
4519           if (STORE_FLAG_VALUE == 1
4520               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4521               && op1 == const0_rtx
4522               && mode == GET_MODE (op0)
4523               && nonzero_bits (op0, mode) == 1)
4524             return gen_lowpart (mode,
4525                                 expand_compound_operation (op0));
4526
4527           else if (STORE_FLAG_VALUE == 1
4528                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4529                    && op1 == const0_rtx
4530                    && mode == GET_MODE (op0)
4531                    && (num_sign_bit_copies (op0, mode)
4532                        == GET_MODE_BITSIZE (mode)))
4533             {
4534               op0 = expand_compound_operation (op0);
4535               return simplify_gen_unary (NEG, mode,
4536                                          gen_lowpart (mode, op0),
4537                                          mode);
4538             }
4539
4540           else if (STORE_FLAG_VALUE == 1
4541                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4542                    && op1 == const0_rtx
4543                    && mode == GET_MODE (op0)
4544                    && nonzero_bits (op0, mode) == 1)
4545             {
4546               op0 = expand_compound_operation (op0);
4547               return simplify_gen_binary (XOR, mode,
4548                                           gen_lowpart (mode, op0),
4549                                           const1_rtx);
4550             }
4551
4552           else if (STORE_FLAG_VALUE == 1
4553                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4554                    && op1 == const0_rtx
4555                    && mode == GET_MODE (op0)
4556                    && (num_sign_bit_copies (op0, mode)
4557                        == GET_MODE_BITSIZE (mode)))
4558             {
4559               op0 = expand_compound_operation (op0);
4560               return plus_constant (gen_lowpart (mode, op0), 1);
4561             }
4562
4563           /* If STORE_FLAG_VALUE is -1, we have cases similar to
4564              those above.  */
4565           if (STORE_FLAG_VALUE == -1
4566               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4567               && op1 == const0_rtx
4568               && (num_sign_bit_copies (op0, mode)
4569                   == GET_MODE_BITSIZE (mode)))
4570             return gen_lowpart (mode,
4571                                 expand_compound_operation (op0));
4572
4573           else if (STORE_FLAG_VALUE == -1
4574                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4575                    && op1 == const0_rtx
4576                    && mode == GET_MODE (op0)
4577                    && nonzero_bits (op0, mode) == 1)
4578             {
4579               op0 = expand_compound_operation (op0);
4580               return simplify_gen_unary (NEG, mode,
4581                                          gen_lowpart (mode, op0),
4582                                          mode);
4583             }
4584
4585           else if (STORE_FLAG_VALUE == -1
4586                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4587                    && op1 == const0_rtx
4588                    && mode == GET_MODE (op0)
4589                    && (num_sign_bit_copies (op0, mode)
4590                        == GET_MODE_BITSIZE (mode)))
4591             {
4592               op0 = expand_compound_operation (op0);
4593               return simplify_gen_unary (NOT, mode,
4594                                          gen_lowpart (mode, op0),
4595                                          mode);
4596             }
4597
4598           /* If X is 0/1, (eq X 0) is X-1.  */
4599           else if (STORE_FLAG_VALUE == -1
4600                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4601                    && op1 == const0_rtx
4602                    && mode == GET_MODE (op0)
4603                    && nonzero_bits (op0, mode) == 1)
4604             {
4605               op0 = expand_compound_operation (op0);
4606               return plus_constant (gen_lowpart (mode, op0), -1);
4607             }
4608
4609           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4610              one bit that might be nonzero, we can convert (ne x 0) to
4611              (ashift x c) where C puts the bit in the sign bit.  Remove any
4612              AND with STORE_FLAG_VALUE when we are done, since we are only
4613              going to test the sign bit.  */
4614           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4615               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4616               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4617                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4618               && op1 == const0_rtx
4619               && mode == GET_MODE (op0)
4620               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4621             {
4622               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4623                                         expand_compound_operation (op0),
4624                                         GET_MODE_BITSIZE (mode) - 1 - i);
4625               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4626                 return XEXP (x, 0);
4627               else
4628                 return x;
4629             }
4630
4631           /* If the code changed, return a whole new comparison.  */
4632           if (new_code != code)
4633             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4634
4635           /* Otherwise, keep this operation, but maybe change its operands.
4636              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
4637           SUBST (XEXP (x, 0), op0);
4638           SUBST (XEXP (x, 1), op1);
4639         }
4640       break;
4641
4642     case IF_THEN_ELSE:
4643       return simplify_if_then_else (x);
4644
4645     case ZERO_EXTRACT:
4646     case SIGN_EXTRACT:
4647     case ZERO_EXTEND:
4648     case SIGN_EXTEND:
4649       /* If we are processing SET_DEST, we are done.  */
4650       if (in_dest)
4651         return x;
4652
4653       return expand_compound_operation (x);
4654
4655     case SET:
4656       return simplify_set (x);
4657
4658     case AND:
4659     case IOR:
4660     case XOR:
4661       return simplify_logical (x);
4662
4663     case ABS:
4664       /* (abs (neg <foo>)) -> (abs <foo>) */
4665       if (GET_CODE (XEXP (x, 0)) == NEG)
4666         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4667
4668       /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4669          do nothing.  */
4670       if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4671         break;
4672
4673       /* If operand is something known to be positive, ignore the ABS.  */
4674       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4675           || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4676                <= HOST_BITS_PER_WIDE_INT)
4677               && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4678                    & ((HOST_WIDE_INT) 1
4679                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4680                   == 0)))
4681         return XEXP (x, 0);
4682
4683       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
4684       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4685         return gen_rtx_NEG (mode, XEXP (x, 0));
4686
4687       break;
4688
4689     case FFS:
4690       /* (ffs (*_extend <X>)) = (ffs <X>) */
4691       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4692           || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4693         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4694       break;
4695
4696     case POPCOUNT:
4697     case PARITY:
4698       /* (pop* (zero_extend <X>)) = (pop* <X>) */
4699       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4700         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4701       break;
4702
4703     case FLOAT:
4704       /* (float (sign_extend <X>)) = (float <X>).  */
4705       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4706         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4707       break;
4708
4709     case ASHIFT:
4710     case LSHIFTRT:
4711     case ASHIFTRT:
4712     case ROTATE:
4713     case ROTATERT:
4714       /* If this is a shift by a constant amount, simplify it.  */
4715       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4716         return simplify_shift_const (x, code, mode, XEXP (x, 0),
4717                                      INTVAL (XEXP (x, 1)));
4718
4719       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
4720         SUBST (XEXP (x, 1),
4721                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4722                               ((HOST_WIDE_INT) 1
4723                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4724                               - 1,
4725                               NULL_RTX, 0));
4726       break;
4727
4728     case VEC_SELECT:
4729       {
4730         rtx op0 = XEXP (x, 0);
4731         rtx op1 = XEXP (x, 1);
4732         int len;
4733
4734         gcc_assert (GET_CODE (op1) == PARALLEL);
4735         len = XVECLEN (op1, 0);
4736         if (len == 1
4737             && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4738             && GET_CODE (op0) == VEC_CONCAT)
4739           {
4740             int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4741
4742             /* Try to find the element in the VEC_CONCAT.  */
4743             for (;;)
4744               {
4745                 if (GET_MODE (op0) == GET_MODE (x))
4746                   return op0;
4747                 if (GET_CODE (op0) == VEC_CONCAT)
4748                   {
4749                     HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4750                     if (offset < op0_size)
4751                       op0 = XEXP (op0, 0);
4752                     else
4753                       {
4754                         offset -= op0_size;
4755                         op0 = XEXP (op0, 1);
4756                       }
4757                   }
4758                 else
4759                   break;
4760               }
4761           }
4762       }
4763
4764       break;
4765
4766     default:
4767       break;
4768     }
4769
4770   return x;
4771 }
4772 \f
4773 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
4774
4775 static rtx
4776 simplify_if_then_else (rtx x)
4777 {
4778   enum machine_mode mode = GET_MODE (x);
4779   rtx cond = XEXP (x, 0);
4780   rtx true_rtx = XEXP (x, 1);
4781   rtx false_rtx = XEXP (x, 2);
4782   enum rtx_code true_code = GET_CODE (cond);
4783   int comparison_p = COMPARISON_P (cond);
4784   rtx temp;
4785   int i;
4786   enum rtx_code false_code;
4787   rtx reversed;
4788
4789   /* Simplify storing of the truth value.  */
4790   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4791     return simplify_gen_relational (true_code, mode, VOIDmode,
4792                                     XEXP (cond, 0), XEXP (cond, 1));
4793
4794   /* Also when the truth value has to be reversed.  */
4795   if (comparison_p
4796       && true_rtx == const0_rtx && false_rtx == const_true_rtx
4797       && (reversed = reversed_comparison (cond, mode)))
4798     return reversed;
4799
4800   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4801      in it is being compared against certain values.  Get the true and false
4802      comparisons and see if that says anything about the value of each arm.  */
4803
4804   if (comparison_p
4805       && ((false_code = reversed_comparison_code (cond, NULL))
4806           != UNKNOWN)
4807       && REG_P (XEXP (cond, 0)))
4808     {
4809       HOST_WIDE_INT nzb;
4810       rtx from = XEXP (cond, 0);
4811       rtx true_val = XEXP (cond, 1);
4812       rtx false_val = true_val;
4813       int swapped = 0;
4814
4815       /* If FALSE_CODE is EQ, swap the codes and arms.  */
4816
4817       if (false_code == EQ)
4818         {
4819           swapped = 1, true_code = EQ, false_code = NE;
4820           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4821         }
4822
4823       /* If we are comparing against zero and the expression being tested has
4824          only a single bit that might be nonzero, that is its value when it is
4825          not equal to zero.  Similarly if it is known to be -1 or 0.  */
4826
4827       if (true_code == EQ && true_val == const0_rtx
4828           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4829         false_code = EQ, false_val = GEN_INT (nzb);
4830       else if (true_code == EQ && true_val == const0_rtx
4831                && (num_sign_bit_copies (from, GET_MODE (from))
4832                    == GET_MODE_BITSIZE (GET_MODE (from))))
4833         false_code = EQ, false_val = constm1_rtx;
4834
4835       /* Now simplify an arm if we know the value of the register in the
4836          branch and it is used in the arm.  Be careful due to the potential
4837          of locally-shared RTL.  */
4838
4839       if (reg_mentioned_p (from, true_rtx))
4840         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4841                                       from, true_val),
4842                       pc_rtx, pc_rtx, 0, 0);
4843       if (reg_mentioned_p (from, false_rtx))
4844         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4845                                    from, false_val),
4846                        pc_rtx, pc_rtx, 0, 0);
4847
4848       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4849       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4850
4851       true_rtx = XEXP (x, 1);
4852       false_rtx = XEXP (x, 2);
4853       true_code = GET_CODE (cond);
4854     }
4855
4856   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4857      reversed, do so to avoid needing two sets of patterns for
4858      subtract-and-branch insns.  Similarly if we have a constant in the true
4859      arm, the false arm is the same as the first operand of the comparison, or
4860      the false arm is more complicated than the true arm.  */
4861
4862   if (comparison_p
4863       && reversed_comparison_code (cond, NULL) != UNKNOWN
4864       && (true_rtx == pc_rtx
4865           || (CONSTANT_P (true_rtx)
4866               && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4867           || true_rtx == const0_rtx
4868           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
4869           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
4870               && !OBJECT_P (false_rtx))
4871           || reg_mentioned_p (true_rtx, false_rtx)
4872           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4873     {
4874       true_code = reversed_comparison_code (cond, NULL);
4875       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
4876       SUBST (XEXP (x, 1), false_rtx);
4877       SUBST (XEXP (x, 2), true_rtx);
4878
4879       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4880       cond = XEXP (x, 0);
4881
4882       /* It is possible that the conditional has been simplified out.  */
4883       true_code = GET_CODE (cond);
4884       comparison_p = COMPARISON_P (cond);
4885     }
4886
4887   /* If the two arms are identical, we don't need the comparison.  */
4888
4889   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4890     return true_rtx;
4891
4892   /* Convert a == b ? b : a to "a".  */
4893   if (true_code == EQ && ! side_effects_p (cond)
4894       && !HONOR_NANS (mode)
4895       && rtx_equal_p (XEXP (cond, 0), false_rtx)
4896       && rtx_equal_p (XEXP (cond, 1), true_rtx))
4897     return false_rtx;
4898   else if (true_code == NE && ! side_effects_p (cond)
4899            && !HONOR_NANS (mode)
4900            && rtx_equal_p (XEXP (cond, 0), true_rtx)
4901            && rtx_equal_p (XEXP (cond, 1), false_rtx))
4902     return true_rtx;
4903
4904   /* Look for cases where we have (abs x) or (neg (abs X)).  */
4905
4906   if (GET_MODE_CLASS (mode) == MODE_INT
4907       && GET_CODE (false_rtx) == NEG
4908       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4909       && comparison_p
4910       && rtx_equal_p (true_rtx, XEXP (cond, 0))
4911       && ! side_effects_p (true_rtx))
4912     switch (true_code)
4913       {
4914       case GT:
4915       case GE:
4916         return simplify_gen_unary (ABS, mode, true_rtx, mode);
4917       case LT:
4918       case LE:
4919         return
4920           simplify_gen_unary (NEG, mode,
4921                               simplify_gen_unary (ABS, mode, true_rtx, mode),
4922                               mode);
4923       default:
4924         break;
4925       }
4926
4927   /* Look for MIN or MAX.  */
4928
4929   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4930       && comparison_p
4931       && rtx_equal_p (XEXP (cond, 0), true_rtx)
4932       && rtx_equal_p (XEXP (cond, 1), false_rtx)
4933       && ! side_effects_p (cond))
4934     switch (true_code)
4935       {
4936       case GE:
4937       case GT:
4938         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
4939       case LE:
4940       case LT:
4941         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
4942       case GEU:
4943       case GTU:
4944         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
4945       case LEU:
4946       case LTU:
4947         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
4948       default:
4949         break;
4950       }
4951
4952   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4953      second operand is zero, this can be done as (OP Z (mult COND C2)) where
4954      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4955      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4956      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4957      neither 1 or -1, but it isn't worth checking for.  */
4958
4959   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4960       && comparison_p
4961       && GET_MODE_CLASS (mode) == MODE_INT
4962       && ! side_effects_p (x))
4963     {
4964       rtx t = make_compound_operation (true_rtx, SET);
4965       rtx f = make_compound_operation (false_rtx, SET);
4966       rtx cond_op0 = XEXP (cond, 0);
4967       rtx cond_op1 = XEXP (cond, 1);
4968       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
4969       enum machine_mode m = mode;
4970       rtx z = 0, c1 = NULL_RTX;
4971
4972       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4973            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4974            || GET_CODE (t) == ASHIFT
4975            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4976           && rtx_equal_p (XEXP (t, 0), f))
4977         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4978
4979       /* If an identity-zero op is commutative, check whether there
4980          would be a match if we swapped the operands.  */
4981       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4982                 || GET_CODE (t) == XOR)
4983                && rtx_equal_p (XEXP (t, 1), f))
4984         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4985       else if (GET_CODE (t) == SIGN_EXTEND
4986                && (GET_CODE (XEXP (t, 0)) == PLUS
4987                    || GET_CODE (XEXP (t, 0)) == MINUS
4988                    || GET_CODE (XEXP (t, 0)) == IOR
4989                    || GET_CODE (XEXP (t, 0)) == XOR
4990                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4991                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4992                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4993                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4994                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4995                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4996                && (num_sign_bit_copies (f, GET_MODE (f))
4997                    > (unsigned int)
4998                      (GET_MODE_BITSIZE (mode)
4999                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5000         {
5001           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5002           extend_op = SIGN_EXTEND;
5003           m = GET_MODE (XEXP (t, 0));
5004         }
5005       else if (GET_CODE (t) == SIGN_EXTEND
5006                && (GET_CODE (XEXP (t, 0)) == PLUS
5007                    || GET_CODE (XEXP (t, 0)) == IOR
5008                    || GET_CODE (XEXP (t, 0)) == XOR)
5009                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5010                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5011                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5012                && (num_sign_bit_copies (f, GET_MODE (f))
5013                    > (unsigned int)
5014                      (GET_MODE_BITSIZE (mode)
5015                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5016         {
5017           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5018           extend_op = SIGN_EXTEND;
5019           m = GET_MODE (XEXP (t, 0));
5020         }
5021       else if (GET_CODE (t) == ZERO_EXTEND
5022                && (GET_CODE (XEXP (t, 0)) == PLUS
5023                    || GET_CODE (XEXP (t, 0)) == MINUS
5024                    || GET_CODE (XEXP (t, 0)) == IOR
5025                    || GET_CODE (XEXP (t, 0)) == XOR
5026                    || GET_CODE (XEXP (t, 0)) == ASHIFT
5027                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5028                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5029                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5030                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5031                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5032                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5033                && ((nonzero_bits (f, GET_MODE (f))
5034                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5035                    == 0))
5036         {
5037           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5038           extend_op = ZERO_EXTEND;
5039           m = GET_MODE (XEXP (t, 0));
5040         }
5041       else if (GET_CODE (t) == ZERO_EXTEND
5042                && (GET_CODE (XEXP (t, 0)) == PLUS
5043                    || GET_CODE (XEXP (t, 0)) == IOR
5044                    || GET_CODE (XEXP (t, 0)) == XOR)
5045                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5046                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5047                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5048                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5049                && ((nonzero_bits (f, GET_MODE (f))
5050                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5051                    == 0))
5052         {
5053           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5054           extend_op = ZERO_EXTEND;
5055           m = GET_MODE (XEXP (t, 0));
5056         }
5057
5058       if (z)
5059         {
5060           temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5061                                                  cond_op0, cond_op1),
5062                         pc_rtx, pc_rtx, 0, 0);
5063           temp = simplify_gen_binary (MULT, m, temp,
5064                                       simplify_gen_binary (MULT, m, c1,
5065                                                            const_true_rtx));
5066           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5067           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5068
5069           if (extend_op != UNKNOWN)
5070             temp = simplify_gen_unary (extend_op, mode, temp, m);
5071
5072           return temp;
5073         }
5074     }
5075
5076   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5077      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5078      negation of a single bit, we can convert this operation to a shift.  We
5079      can actually do this more generally, but it doesn't seem worth it.  */
5080
5081   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5082       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5083       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5084            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5085           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5086                == GET_MODE_BITSIZE (mode))
5087               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5088     return
5089       simplify_shift_const (NULL_RTX, ASHIFT, mode,
5090                             gen_lowpart (mode, XEXP (cond, 0)), i);
5091
5092   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
5093   if (true_code == NE && XEXP (cond, 1) == const0_rtx
5094       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5095       && GET_MODE (XEXP (cond, 0)) == mode
5096       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5097           == nonzero_bits (XEXP (cond, 0), mode)
5098       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5099     return XEXP (cond, 0);
5100
5101   return x;
5102 }
5103 \f
5104 /* Simplify X, a SET expression.  Return the new expression.  */
5105
5106 static rtx
5107 simplify_set (rtx x)
5108 {
5109   rtx src = SET_SRC (x);
5110   rtx dest = SET_DEST (x);
5111   enum machine_mode mode
5112     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5113   rtx other_insn;
5114   rtx *cc_use;
5115
5116   /* (set (pc) (return)) gets written as (return).  */
5117   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5118     return src;
5119
5120   /* Now that we know for sure which bits of SRC we are using, see if we can
5121      simplify the expression for the object knowing that we only need the
5122      low-order bits.  */
5123
5124   if (GET_MODE_CLASS (mode) == MODE_INT
5125       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5126     {
5127       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
5128       SUBST (SET_SRC (x), src);
5129     }
5130
5131   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5132      the comparison result and try to simplify it unless we already have used
5133      undobuf.other_insn.  */
5134   if ((GET_MODE_CLASS (mode) == MODE_CC
5135        || GET_CODE (src) == COMPARE
5136        || CC0_P (dest))
5137       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5138       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5139       && COMPARISON_P (*cc_use)
5140       && rtx_equal_p (XEXP (*cc_use, 0), dest))
5141     {
5142       enum rtx_code old_code = GET_CODE (*cc_use);
5143       enum rtx_code new_code;
5144       rtx op0, op1, tmp;
5145       int other_changed = 0;
5146       enum machine_mode compare_mode = GET_MODE (dest);
5147
5148       if (GET_CODE (src) == COMPARE)
5149         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5150       else
5151         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5152
5153       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5154                                            op0, op1);
5155       if (!tmp)
5156         new_code = old_code;
5157       else if (!CONSTANT_P (tmp))
5158         {
5159           new_code = GET_CODE (tmp);
5160           op0 = XEXP (tmp, 0);
5161           op1 = XEXP (tmp, 1);
5162         }
5163       else
5164         {
5165           rtx pat = PATTERN (other_insn);
5166           undobuf.other_insn = other_insn;
5167           SUBST (*cc_use, tmp);
5168
5169           /* Attempt to simplify CC user.  */
5170           if (GET_CODE (pat) == SET)
5171             {
5172               rtx new = simplify_rtx (SET_SRC (pat));
5173               if (new != NULL_RTX)
5174                 SUBST (SET_SRC (pat), new);
5175             }
5176
5177           /* Convert X into a no-op move.  */
5178           SUBST (SET_DEST (x), pc_rtx);
5179           SUBST (SET_SRC (x), pc_rtx);
5180           return x;
5181         }
5182
5183       /* Simplify our comparison, if possible.  */
5184       new_code = simplify_comparison (new_code, &op0, &op1);
5185
5186 #ifdef SELECT_CC_MODE
5187       /* If this machine has CC modes other than CCmode, check to see if we
5188          need to use a different CC mode here.  */
5189       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5190         compare_mode = GET_MODE (op0);
5191       else
5192         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5193
5194 #ifndef HAVE_cc0
5195       /* If the mode changed, we have to change SET_DEST, the mode in the
5196          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5197          a hard register, just build new versions with the proper mode.  If it
5198          is a pseudo, we lose unless it is only time we set the pseudo, in
5199          which case we can safely change its mode.  */
5200       if (compare_mode != GET_MODE (dest))
5201         {
5202           unsigned int regno = REGNO (dest);
5203           rtx new_dest = gen_rtx_REG (compare_mode, regno);
5204
5205           if (regno < FIRST_PSEUDO_REGISTER
5206               || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5207             {
5208               if (regno >= FIRST_PSEUDO_REGISTER)
5209                 SUBST (regno_reg_rtx[regno], new_dest);
5210
5211               SUBST (SET_DEST (x), new_dest);
5212               SUBST (XEXP (*cc_use, 0), new_dest);
5213               other_changed = 1;
5214
5215               dest = new_dest;
5216             }
5217         }
5218 #endif  /* cc0 */
5219 #endif  /* SELECT_CC_MODE */
5220
5221       /* If the code changed, we have to build a new comparison in
5222          undobuf.other_insn.  */
5223       if (new_code != old_code)
5224         {
5225           int other_changed_previously = other_changed;
5226           unsigned HOST_WIDE_INT mask;
5227
5228           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5229                                           dest, const0_rtx));
5230           other_changed = 1;
5231
5232           /* If the only change we made was to change an EQ into an NE or
5233              vice versa, OP0 has only one bit that might be nonzero, and OP1
5234              is zero, check if changing the user of the condition code will
5235              produce a valid insn.  If it won't, we can keep the original code
5236              in that insn by surrounding our operation with an XOR.  */
5237
5238           if (((old_code == NE && new_code == EQ)
5239                || (old_code == EQ && new_code == NE))
5240               && ! other_changed_previously && op1 == const0_rtx
5241               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5242               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5243             {
5244               rtx pat = PATTERN (other_insn), note = 0;
5245
5246               if ((recog_for_combine (&pat, other_insn, &note) < 0
5247                    && ! check_asm_operands (pat)))
5248                 {
5249                   PUT_CODE (*cc_use, old_code);
5250                   other_changed = 0;
5251
5252                   op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5253                                              op0, GEN_INT (mask));
5254                 }
5255             }
5256         }
5257
5258       if (other_changed)
5259         undobuf.other_insn = other_insn;
5260
5261 #ifdef HAVE_cc0
5262       /* If we are now comparing against zero, change our source if
5263          needed.  If we do not use cc0, we always have a COMPARE.  */
5264       if (op1 == const0_rtx && dest == cc0_rtx)
5265         {
5266           SUBST (SET_SRC (x), op0);
5267           src = op0;
5268         }
5269       else
5270 #endif
5271
5272       /* Otherwise, if we didn't previously have a COMPARE in the
5273          correct mode, we need one.  */
5274       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5275         {
5276           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5277           src = SET_SRC (x);
5278         }
5279       else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5280         {
5281           SUBST(SET_SRC (x), op0);
5282           src = SET_SRC (x);
5283         }
5284       else
5285         {
5286           /* Otherwise, update the COMPARE if needed.  */
5287           SUBST (XEXP (src, 0), op0);
5288           SUBST (XEXP (src, 1), op1);
5289         }
5290     }
5291   else
5292     {
5293       /* Get SET_SRC in a form where we have placed back any
5294          compound expressions.  Then do the checks below.  */
5295       src = make_compound_operation (src, SET);
5296       SUBST (SET_SRC (x), src);
5297     }
5298
5299   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5300      and X being a REG or (subreg (reg)), we may be able to convert this to
5301      (set (subreg:m2 x) (op)).
5302
5303      We can always do this if M1 is narrower than M2 because that means that
5304      we only care about the low bits of the result.
5305
5306      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5307      perform a narrower operation than requested since the high-order bits will
5308      be undefined.  On machine where it is defined, this transformation is safe
5309      as long as M1 and M2 have the same number of words.  */
5310
5311   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5312       && !OBJECT_P (SUBREG_REG (src))
5313       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5314            / UNITS_PER_WORD)
5315           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5316                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5317 #ifndef WORD_REGISTER_OPERATIONS
5318       && (GET_MODE_SIZE (GET_MODE (src))
5319         < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5320 #endif
5321 #ifdef CANNOT_CHANGE_MODE_CLASS
5322       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5323             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5324                                          GET_MODE (SUBREG_REG (src)),
5325                                          GET_MODE (src)))
5326 #endif
5327       && (REG_P (dest)
5328           || (GET_CODE (dest) == SUBREG
5329               && REG_P (SUBREG_REG (dest)))))
5330     {
5331       SUBST (SET_DEST (x),
5332              gen_lowpart (GET_MODE (SUBREG_REG (src)),
5333                                       dest));
5334       SUBST (SET_SRC (x), SUBREG_REG (src));
5335
5336       src = SET_SRC (x), dest = SET_DEST (x);
5337     }
5338
5339 #ifdef HAVE_cc0
5340   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5341      in SRC.  */
5342   if (dest == cc0_rtx
5343       && GET_CODE (src) == SUBREG
5344       && subreg_lowpart_p (src)
5345       && (GET_MODE_BITSIZE (GET_MODE (src))
5346           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5347     {
5348       rtx inner = SUBREG_REG (src);
5349       enum machine_mode inner_mode = GET_MODE (inner);
5350
5351       /* Here we make sure that we don't have a sign bit on.  */
5352       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5353           && (nonzero_bits (inner, inner_mode)
5354               < ((unsigned HOST_WIDE_INT) 1
5355                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5356         {
5357           SUBST (SET_SRC (x), inner);
5358           src = SET_SRC (x);
5359         }
5360     }
5361 #endif
5362
5363 #ifdef LOAD_EXTEND_OP
5364   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5365      would require a paradoxical subreg.  Replace the subreg with a
5366      zero_extend to avoid the reload that would otherwise be required.  */
5367
5368   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5369       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5370       && SUBREG_BYTE (src) == 0
5371       && (GET_MODE_SIZE (GET_MODE (src))
5372           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5373       && MEM_P (SUBREG_REG (src)))
5374     {
5375       SUBST (SET_SRC (x),
5376              gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5377                             GET_MODE (src), SUBREG_REG (src)));
5378
5379       src = SET_SRC (x);
5380     }
5381 #endif
5382
5383   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5384      are comparing an item known to be 0 or -1 against 0, use a logical
5385      operation instead. Check for one of the arms being an IOR of the other
5386      arm with some value.  We compute three terms to be IOR'ed together.  In
5387      practice, at most two will be nonzero.  Then we do the IOR's.  */
5388
5389   if (GET_CODE (dest) != PC
5390       && GET_CODE (src) == IF_THEN_ELSE
5391       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5392       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5393       && XEXP (XEXP (src, 0), 1) == const0_rtx
5394       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5395 #ifdef HAVE_conditional_move
5396       && ! can_conditionally_move_p (GET_MODE (src))
5397 #endif
5398       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5399                                GET_MODE (XEXP (XEXP (src, 0), 0)))
5400           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5401       && ! side_effects_p (src))
5402     {
5403       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5404                       ? XEXP (src, 1) : XEXP (src, 2));
5405       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5406                    ? XEXP (src, 2) : XEXP (src, 1));
5407       rtx term1 = const0_rtx, term2, term3;
5408
5409       if (GET_CODE (true_rtx) == IOR
5410           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5411         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5412       else if (GET_CODE (true_rtx) == IOR
5413                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5414         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5415       else if (GET_CODE (false_rtx) == IOR
5416                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5417         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5418       else if (GET_CODE (false_rtx) == IOR
5419                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5420         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5421
5422       term2 = simplify_gen_binary (AND, GET_MODE (src),
5423                                    XEXP (XEXP (src, 0), 0), true_rtx);
5424       term3 = simplify_gen_binary (AND, GET_MODE (src),
5425                                    simplify_gen_unary (NOT, GET_MODE (src),
5426                                                        XEXP (XEXP (src, 0), 0),
5427                                                        GET_MODE (src)),
5428                                    false_rtx);
5429
5430       SUBST (SET_SRC (x),
5431              simplify_gen_binary (IOR, GET_MODE (src),
5432                                   simplify_gen_binary (IOR, GET_MODE (src),
5433                                                        term1, term2),
5434                                   term3));
5435
5436       src = SET_SRC (x);
5437     }
5438
5439   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5440      whole thing fail.  */
5441   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5442     return src;
5443   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5444     return dest;
5445   else
5446     /* Convert this into a field assignment operation, if possible.  */
5447     return make_field_assignment (x);
5448 }
5449 \f
5450 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5451    result.  */
5452
5453 static rtx
5454 simplify_logical (rtx x)
5455 {
5456   enum machine_mode mode = GET_MODE (x);
5457   rtx op0 = XEXP (x, 0);
5458   rtx op1 = XEXP (x, 1);
5459   rtx reversed;
5460
5461   switch (GET_CODE (x))
5462     {
5463     case AND:
5464       /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5465          insn (and may simplify more).  */
5466       if (GET_CODE (op0) == XOR
5467           && rtx_equal_p (XEXP (op0, 0), op1)
5468           && ! side_effects_p (op1))
5469         x = simplify_gen_binary (AND, mode,
5470                                  simplify_gen_unary (NOT, mode,
5471                                                      XEXP (op0, 1), mode),
5472                                  op1);
5473
5474       if (GET_CODE (op0) == XOR
5475           && rtx_equal_p (XEXP (op0, 1), op1)
5476           && ! side_effects_p (op1))
5477         x = simplify_gen_binary (AND, mode,
5478                                  simplify_gen_unary (NOT, mode,
5479                                                      XEXP (op0, 0), mode),
5480                                  op1);
5481
5482       /* Similarly for (~(A ^ B)) & A.  */
5483       if (GET_CODE (op0) == NOT
5484           && GET_CODE (XEXP (op0, 0)) == XOR
5485           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5486           && ! side_effects_p (op1))
5487         x = simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5488
5489       if (GET_CODE (op0) == NOT
5490           && GET_CODE (XEXP (op0, 0)) == XOR
5491           && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5492           && ! side_effects_p (op1))
5493         x = simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5494
5495       /* We can call simplify_and_const_int only if we don't lose
5496          any (sign) bits when converting INTVAL (op1) to
5497          "unsigned HOST_WIDE_INT".  */
5498       if (GET_CODE (op1) == CONST_INT
5499           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5500               || INTVAL (op1) > 0))
5501         {
5502           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5503
5504           /* If we have (ior (and (X C1) C2)) and the next restart would be
5505              the last, simplify this by making C1 as small as possible
5506              and then exit.  Only do this if C1 actually changes: for now
5507              this only saves memory but, should this transformation be
5508              moved to simplify-rtx.c, we'd risk unbounded recursion there.  */
5509           if (GET_CODE (x) == IOR && GET_CODE (op0) == AND
5510               && GET_CODE (XEXP (op0, 1)) == CONST_INT
5511               && GET_CODE (op1) == CONST_INT
5512               && (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) != 0)
5513             return simplify_gen_binary (IOR, mode,
5514                                         simplify_gen_binary
5515                                           (AND, mode, XEXP (op0, 0),
5516                                            GEN_INT (INTVAL (XEXP (op0, 1))
5517                                                     & ~INTVAL (op1))), op1);
5518
5519           if (GET_CODE (x) != AND)
5520             return x;
5521
5522           op0 = XEXP (x, 0);
5523           op1 = XEXP (x, 1);
5524         }
5525
5526       /* Convert (A | B) & A to A.  */
5527       if (GET_CODE (op0) == IOR
5528           && (rtx_equal_p (XEXP (op0, 0), op1)
5529               || rtx_equal_p (XEXP (op0, 1), op1))
5530           && ! side_effects_p (XEXP (op0, 0))
5531           && ! side_effects_p (XEXP (op0, 1)))
5532         return op1;
5533
5534       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5535          apply the distributive law and then the inverse distributive
5536          law to see if things simplify.  */
5537       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5538         {
5539           rtx result = distribute_and_simplify_rtx (x, 0);
5540           if (result)
5541             return result;
5542         }
5543       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5544         {
5545           rtx result = distribute_and_simplify_rtx (x, 1);
5546           if (result)
5547             return result;
5548         }
5549       break;
5550
5551     case IOR:
5552       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
5553       if (GET_CODE (op1) == CONST_INT
5554           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5555           && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5556         return op1;
5557
5558       /* Convert (A & B) | A to A.  */
5559       if (GET_CODE (op0) == AND
5560           && (rtx_equal_p (XEXP (op0, 0), op1)
5561               || rtx_equal_p (XEXP (op0, 1), op1))
5562           && ! side_effects_p (XEXP (op0, 0))
5563           && ! side_effects_p (XEXP (op0, 1)))
5564         return op1;
5565
5566       /* If we have (ior (and A B) C), apply the distributive law and then
5567          the inverse distributive law to see if things simplify.  */
5568
5569       if (GET_CODE (op0) == AND)
5570         {
5571           rtx result = distribute_and_simplify_rtx (x, 0);
5572           if (result)
5573             return result;
5574         }
5575
5576       if (GET_CODE (op1) == AND)
5577         {
5578           rtx result = distribute_and_simplify_rtx (x, 1);
5579           if (result)
5580             return result;
5581         }
5582
5583       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5584          mode size to (rotate A CX).  */
5585
5586       if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5587            || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5588           && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5589           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5590           && GET_CODE (XEXP (op1, 1)) == CONST_INT
5591           && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5592               == GET_MODE_BITSIZE (mode)))
5593         return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5594                                (GET_CODE (op0) == ASHIFT
5595                                 ? XEXP (op0, 1) : XEXP (op1, 1)));
5596
5597       /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5598          a (sign_extend (plus ...)).  If so, OP1 is a CONST_INT, and the PLUS
5599          does not affect any of the bits in OP1, it can really be done
5600          as a PLUS and we can associate.  We do this by seeing if OP1
5601          can be safely shifted left C bits.  */
5602       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5603           && GET_CODE (XEXP (op0, 0)) == PLUS
5604           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5605           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5606           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5607         {
5608           int count = INTVAL (XEXP (op0, 1));
5609           HOST_WIDE_INT mask = INTVAL (op1) << count;
5610
5611           if (mask >> count == INTVAL (op1)
5612               && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5613             {
5614               SUBST (XEXP (XEXP (op0, 0), 1),
5615                      GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5616               return op0;
5617             }
5618         }
5619       break;
5620
5621     case XOR:
5622       /* If we are XORing two things that have no bits in common,
5623          convert them into an IOR.  This helps to detect rotation encoded
5624          using those methods and possibly other simplifications.  */
5625
5626       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5627           && (nonzero_bits (op0, mode)
5628               & nonzero_bits (op1, mode)) == 0)
5629         return (simplify_gen_binary (IOR, mode, op0, op1));
5630
5631       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5632          Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5633          (NOT y).  */
5634       {
5635         int num_negated = 0;
5636
5637         if (GET_CODE (op0) == NOT)
5638           num_negated++, op0 = XEXP (op0, 0);
5639         if (GET_CODE (op1) == NOT)
5640           num_negated++, op1 = XEXP (op1, 0);
5641
5642         if (num_negated == 2)
5643           {
5644             SUBST (XEXP (x, 0), op0);
5645             SUBST (XEXP (x, 1), op1);
5646           }
5647         else if (num_negated == 1)
5648           return
5649             simplify_gen_unary (NOT, mode,
5650                                 simplify_gen_binary (XOR, mode, op0, op1),
5651                                 mode);
5652       }
5653
5654       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
5655          correspond to a machine insn or result in further simplifications
5656          if B is a constant.  */
5657
5658       if (GET_CODE (op0) == AND
5659           && rtx_equal_p (XEXP (op0, 1), op1)
5660           && ! side_effects_p (op1))
5661         return simplify_gen_binary (AND, mode,
5662                                     simplify_gen_unary (NOT, mode,
5663                                                         XEXP (op0, 0), mode),
5664                                     op1);
5665
5666       else if (GET_CODE (op0) == AND
5667                && rtx_equal_p (XEXP (op0, 0), op1)
5668                && ! side_effects_p (op1))
5669         return simplify_gen_binary (AND, mode,
5670                                     simplify_gen_unary (NOT, mode,
5671                                                         XEXP (op0, 1), mode),
5672                                     op1);
5673
5674       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5675          comparison if STORE_FLAG_VALUE is 1.  */
5676       if (STORE_FLAG_VALUE == 1
5677           && op1 == const1_rtx
5678           && COMPARISON_P (op0)
5679           && (reversed = reversed_comparison (op0, mode)))
5680         return reversed;
5681
5682       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5683          is (lt foo (const_int 0)), so we can perform the above
5684          simplification if STORE_FLAG_VALUE is 1.  */
5685
5686       if (STORE_FLAG_VALUE == 1
5687           && op1 == const1_rtx
5688           && GET_CODE (op0) == LSHIFTRT
5689           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5690           && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5691         return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5692
5693       /* (xor (comparison foo bar) (const_int sign-bit))
5694          when STORE_FLAG_VALUE is the sign bit.  */
5695       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5696           && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5697               == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5698           && op1 == const_true_rtx
5699           && COMPARISON_P (op0)
5700           && (reversed = reversed_comparison (op0, mode)))
5701         return reversed;
5702
5703       break;
5704
5705     default:
5706       gcc_unreachable ();
5707     }
5708
5709   return x;
5710 }
5711 \f
5712 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5713    operations" because they can be replaced with two more basic operations.
5714    ZERO_EXTEND is also considered "compound" because it can be replaced with
5715    an AND operation, which is simpler, though only one operation.
5716
5717    The function expand_compound_operation is called with an rtx expression
5718    and will convert it to the appropriate shifts and AND operations,
5719    simplifying at each stage.
5720
5721    The function make_compound_operation is called to convert an expression
5722    consisting of shifts and ANDs into the equivalent compound expression.
5723    It is the inverse of this function, loosely speaking.  */
5724
5725 static rtx
5726 expand_compound_operation (rtx x)
5727 {
5728   unsigned HOST_WIDE_INT pos = 0, len;
5729   int unsignedp = 0;
5730   unsigned int modewidth;
5731   rtx tem;
5732
5733   switch (GET_CODE (x))
5734     {
5735     case ZERO_EXTEND:
5736       unsignedp = 1;
5737     case SIGN_EXTEND:
5738       /* We can't necessarily use a const_int for a multiword mode;
5739          it depends on implicitly extending the value.
5740          Since we don't know the right way to extend it,
5741          we can't tell whether the implicit way is right.
5742
5743          Even for a mode that is no wider than a const_int,
5744          we can't win, because we need to sign extend one of its bits through
5745          the rest of it, and we don't know which bit.  */
5746       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5747         return x;
5748
5749       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5750          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
5751          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5752          reloaded. If not for that, MEM's would very rarely be safe.
5753
5754          Reject MODEs bigger than a word, because we might not be able
5755          to reference a two-register group starting with an arbitrary register
5756          (and currently gen_lowpart might crash for a SUBREG).  */
5757
5758       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5759         return x;
5760
5761       /* Reject MODEs that aren't scalar integers because turning vector
5762          or complex modes into shifts causes problems.  */
5763
5764       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5765         return x;
5766
5767       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5768       /* If the inner object has VOIDmode (the only way this can happen
5769          is if it is an ASM_OPERANDS), we can't do anything since we don't
5770          know how much masking to do.  */
5771       if (len == 0)
5772         return x;
5773
5774       break;
5775
5776     case ZERO_EXTRACT:
5777       unsignedp = 1;
5778
5779       /* ... fall through ...  */
5780
5781     case SIGN_EXTRACT:
5782       /* If the operand is a CLOBBER, just return it.  */
5783       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5784         return XEXP (x, 0);
5785
5786       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5787           || GET_CODE (XEXP (x, 2)) != CONST_INT
5788           || GET_MODE (XEXP (x, 0)) == VOIDmode)
5789         return x;
5790
5791       /* Reject MODEs that aren't scalar integers because turning vector
5792          or complex modes into shifts causes problems.  */
5793
5794       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5795         return x;
5796
5797       len = INTVAL (XEXP (x, 1));
5798       pos = INTVAL (XEXP (x, 2));
5799
5800       /* If this goes outside the object being extracted, replace the object
5801          with a (use (mem ...)) construct that only combine understands
5802          and is used only for this purpose.  */
5803       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5804         SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5805
5806       if (BITS_BIG_ENDIAN)
5807         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5808
5809       break;
5810
5811     default:
5812       return x;
5813     }
5814   /* Convert sign extension to zero extension, if we know that the high
5815      bit is not set, as this is easier to optimize.  It will be converted
5816      back to cheaper alternative in make_extraction.  */
5817   if (GET_CODE (x) == SIGN_EXTEND
5818       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5819           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5820                 & ~(((unsigned HOST_WIDE_INT)
5821                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5822                      >> 1))
5823                == 0)))
5824     {
5825       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5826       rtx temp2 = expand_compound_operation (temp);
5827
5828       /* Make sure this is a profitable operation.  */
5829       if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5830        return temp2;
5831       else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5832        return temp;
5833       else
5834        return x;
5835     }
5836
5837   /* We can optimize some special cases of ZERO_EXTEND.  */
5838   if (GET_CODE (x) == ZERO_EXTEND)
5839     {
5840       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5841          know that the last value didn't have any inappropriate bits
5842          set.  */
5843       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5844           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5845           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5846           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5847               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5848         return XEXP (XEXP (x, 0), 0);
5849
5850       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5851       if (GET_CODE (XEXP (x, 0)) == SUBREG
5852           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5853           && subreg_lowpart_p (XEXP (x, 0))
5854           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5855           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5856               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5857         return SUBREG_REG (XEXP (x, 0));
5858
5859       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5860          is a comparison and STORE_FLAG_VALUE permits.  This is like
5861          the first case, but it works even when GET_MODE (x) is larger
5862          than HOST_WIDE_INT.  */
5863       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5864           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5865           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
5866           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5867               <= HOST_BITS_PER_WIDE_INT)
5868           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5869               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5870         return XEXP (XEXP (x, 0), 0);
5871
5872       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5873       if (GET_CODE (XEXP (x, 0)) == SUBREG
5874           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5875           && subreg_lowpart_p (XEXP (x, 0))
5876           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
5877           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5878               <= HOST_BITS_PER_WIDE_INT)
5879           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5880               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5881         return SUBREG_REG (XEXP (x, 0));
5882
5883     }
5884
5885   /* If we reach here, we want to return a pair of shifts.  The inner
5886      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
5887      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
5888      logical depending on the value of UNSIGNEDP.
5889
5890      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5891      converted into an AND of a shift.
5892
5893      We must check for the case where the left shift would have a negative
5894      count.  This can happen in a case like (x >> 31) & 255 on machines
5895      that can't shift by a constant.  On those machines, we would first
5896      combine the shift with the AND to produce a variable-position
5897      extraction.  Then the constant of 31 would be substituted in to produce
5898      a such a position.  */
5899
5900   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5901   if (modewidth + len >= pos)
5902     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5903                                 GET_MODE (x),
5904                                 simplify_shift_const (NULL_RTX, ASHIFT,
5905                                                       GET_MODE (x),
5906                                                       XEXP (x, 0),
5907                                                       modewidth - pos - len),
5908                                 modewidth - len);
5909
5910   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5911     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5912                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
5913                                                         GET_MODE (x),
5914                                                         XEXP (x, 0), pos),
5915                                   ((HOST_WIDE_INT) 1 << len) - 1);
5916   else
5917     /* Any other cases we can't handle.  */
5918     return x;
5919
5920   /* If we couldn't do this for some reason, return the original
5921      expression.  */
5922   if (GET_CODE (tem) == CLOBBER)
5923     return x;
5924
5925   return tem;
5926 }
5927 \f
5928 /* X is a SET which contains an assignment of one object into
5929    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5930    or certain SUBREGS). If possible, convert it into a series of
5931    logical operations.
5932
5933    We half-heartedly support variable positions, but do not at all
5934    support variable lengths.  */
5935
5936 static rtx
5937 expand_field_assignment (rtx x)
5938 {
5939   rtx inner;
5940   rtx pos;                      /* Always counts from low bit.  */
5941   int len;
5942   rtx mask, cleared, masked;
5943   enum machine_mode compute_mode;
5944
5945   /* Loop until we find something we can't simplify.  */
5946   while (1)
5947     {
5948       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5949           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5950         {
5951           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5952           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5953           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5954         }
5955       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5956                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5957         {
5958           inner = XEXP (SET_DEST (x), 0);
5959           len = INTVAL (XEXP (SET_DEST (x), 1));
5960           pos = XEXP (SET_DEST (x), 2);
5961
5962           /* If the position is constant and spans the width of INNER,
5963              surround INNER  with a USE to indicate this.  */
5964           if (GET_CODE (pos) == CONST_INT
5965               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5966             inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5967
5968           if (BITS_BIG_ENDIAN)
5969             {
5970               if (GET_CODE (pos) == CONST_INT)
5971                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5972                                - INTVAL (pos));
5973               else if (GET_CODE (pos) == MINUS
5974                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
5975                        && (INTVAL (XEXP (pos, 1))
5976                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5977                 /* If position is ADJUST - X, new position is X.  */
5978                 pos = XEXP (pos, 0);
5979               else
5980                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
5981                                            GEN_INT (GET_MODE_BITSIZE (
5982                                                     GET_MODE (inner))
5983                                                     - len),
5984                                            pos);
5985             }
5986         }
5987
5988       /* A SUBREG between two modes that occupy the same numbers of words
5989          can be done by moving the SUBREG to the source.  */
5990       else if (GET_CODE (SET_DEST (x)) == SUBREG
5991                /* We need SUBREGs to compute nonzero_bits properly.  */
5992                && nonzero_sign_valid
5993                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5994                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5995                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5996                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5997         {
5998           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5999                            gen_lowpart
6000                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
6001                             SET_SRC (x)));
6002           continue;
6003         }
6004       else
6005         break;
6006
6007       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6008         inner = SUBREG_REG (inner);
6009
6010       compute_mode = GET_MODE (inner);
6011
6012       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
6013       if (! SCALAR_INT_MODE_P (compute_mode))
6014         {
6015           enum machine_mode imode;
6016
6017           /* Don't do anything for vector or complex integral types.  */
6018           if (! FLOAT_MODE_P (compute_mode))
6019             break;
6020
6021           /* Try to find an integral mode to pun with.  */
6022           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6023           if (imode == BLKmode)
6024             break;
6025
6026           compute_mode = imode;
6027           inner = gen_lowpart (imode, inner);
6028         }
6029
6030       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
6031       if (len >= HOST_BITS_PER_WIDE_INT)
6032         break;
6033
6034       /* Now compute the equivalent expression.  Make a copy of INNER
6035          for the SET_DEST in case it is a MEM into which we will substitute;
6036          we don't want shared RTL in that case.  */
6037       mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6038       cleared = simplify_gen_binary (AND, compute_mode,
6039                                      simplify_gen_unary (NOT, compute_mode,
6040                                        simplify_gen_binary (ASHIFT,
6041                                                             compute_mode,
6042                                                             mask, pos),
6043                                        compute_mode),
6044                                      inner);
6045       masked = simplify_gen_binary (ASHIFT, compute_mode,
6046                                     simplify_gen_binary (
6047                                       AND, compute_mode,
6048                                       gen_lowpart (compute_mode, SET_SRC (x)),
6049                                       mask),
6050                                     pos);
6051
6052       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6053                        simplify_gen_binary (IOR, compute_mode,
6054                                             cleared, masked));
6055     }
6056
6057   return x;
6058 }
6059 \f
6060 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
6061    it is an RTX that represents a variable starting position; otherwise,
6062    POS is the (constant) starting bit position (counted from the LSB).
6063
6064    INNER may be a USE.  This will occur when we started with a bitfield
6065    that went outside the boundary of the object in memory, which is
6066    allowed on most machines.  To isolate this case, we produce a USE
6067    whose mode is wide enough and surround the MEM with it.  The only
6068    code that understands the USE is this routine.  If it is not removed,
6069    it will cause the resulting insn not to match.
6070
6071    UNSIGNEDP is nonzero for an unsigned reference and zero for a
6072    signed reference.
6073
6074    IN_DEST is nonzero if this is a reference in the destination of a
6075    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
6076    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6077    be used.
6078
6079    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
6080    ZERO_EXTRACT should be built even for bits starting at bit 0.
6081
6082    MODE is the desired mode of the result (if IN_DEST == 0).
6083
6084    The result is an RTX for the extraction or NULL_RTX if the target
6085    can't handle it.  */
6086
6087 static rtx
6088 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6089                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6090                  int in_dest, int in_compare)
6091 {
6092   /* This mode describes the size of the storage area
6093      to fetch the overall value from.  Within that, we
6094      ignore the POS lowest bits, etc.  */
6095   enum machine_mode is_mode = GET_MODE (inner);
6096   enum machine_mode inner_mode;
6097   enum machine_mode wanted_inner_mode = byte_mode;
6098   enum machine_mode wanted_inner_reg_mode = word_mode;
6099   enum machine_mode pos_mode = word_mode;
6100   enum machine_mode extraction_mode = word_mode;
6101   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6102   int spans_byte = 0;
6103   rtx new = 0;
6104   rtx orig_pos_rtx = pos_rtx;
6105   HOST_WIDE_INT orig_pos;
6106
6107   /* Get some information about INNER and get the innermost object.  */
6108   if (GET_CODE (inner) == USE)
6109     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
6110     /* We don't need to adjust the position because we set up the USE
6111        to pretend that it was a full-word object.  */
6112     spans_byte = 1, inner = XEXP (inner, 0);
6113   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6114     {
6115       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6116          consider just the QI as the memory to extract from.
6117          The subreg adds or removes high bits; its mode is
6118          irrelevant to the meaning of this extraction,
6119          since POS and LEN count from the lsb.  */
6120       if (MEM_P (SUBREG_REG (inner)))
6121         is_mode = GET_MODE (SUBREG_REG (inner));
6122       inner = SUBREG_REG (inner);
6123     }
6124   else if (GET_CODE (inner) == ASHIFT
6125            && GET_CODE (XEXP (inner, 1)) == CONST_INT
6126            && pos_rtx == 0 && pos == 0
6127            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6128     {
6129       /* We're extracting the least significant bits of an rtx
6130          (ashift X (const_int C)), where LEN > C.  Extract the
6131          least significant (LEN - C) bits of X, giving an rtx
6132          whose mode is MODE, then shift it left C times.  */
6133       new = make_extraction (mode, XEXP (inner, 0),
6134                              0, 0, len - INTVAL (XEXP (inner, 1)),
6135                              unsignedp, in_dest, in_compare);
6136       if (new != 0)
6137         return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6138     }
6139
6140   inner_mode = GET_MODE (inner);
6141
6142   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6143     pos = INTVAL (pos_rtx), pos_rtx = 0;
6144
6145   /* See if this can be done without an extraction.  We never can if the
6146      width of the field is not the same as that of some integer mode. For
6147      registers, we can only avoid the extraction if the position is at the
6148      low-order bit and this is either not in the destination or we have the
6149      appropriate STRICT_LOW_PART operation available.
6150
6151      For MEM, we can avoid an extract if the field starts on an appropriate
6152      boundary and we can change the mode of the memory reference.  However,
6153      we cannot directly access the MEM if we have a USE and the underlying
6154      MEM is not TMODE.  This combination means that MEM was being used in a
6155      context where bits outside its mode were being referenced; that is only
6156      valid in bit-field insns.  */
6157
6158   if (tmode != BLKmode
6159       && ! (spans_byte && inner_mode != tmode)
6160       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6161            && !MEM_P (inner)
6162            && (! in_dest
6163                || (REG_P (inner)
6164                    && have_insn_for (STRICT_LOW_PART, tmode))))
6165           || (MEM_P (inner) && pos_rtx == 0
6166               && (pos
6167                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6168                      : BITS_PER_UNIT)) == 0
6169               /* We can't do this if we are widening INNER_MODE (it
6170                  may not be aligned, for one thing).  */
6171               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6172               && (inner_mode == tmode
6173                   || (! mode_dependent_address_p (XEXP (inner, 0))
6174                       && ! MEM_VOLATILE_P (inner))))))
6175     {
6176       /* If INNER is a MEM, make a new MEM that encompasses just the desired
6177          field.  If the original and current mode are the same, we need not
6178          adjust the offset.  Otherwise, we do if bytes big endian.
6179
6180          If INNER is not a MEM, get a piece consisting of just the field
6181          of interest (in this case POS % BITS_PER_WORD must be 0).  */
6182
6183       if (MEM_P (inner))
6184         {
6185           HOST_WIDE_INT offset;
6186
6187           /* POS counts from lsb, but make OFFSET count in memory order.  */
6188           if (BYTES_BIG_ENDIAN)
6189             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6190           else
6191             offset = pos / BITS_PER_UNIT;
6192
6193           new = adjust_address_nv (inner, tmode, offset);
6194         }
6195       else if (REG_P (inner))
6196         {
6197           if (tmode != inner_mode)
6198             {
6199               /* We can't call gen_lowpart in a DEST since we
6200                  always want a SUBREG (see below) and it would sometimes
6201                  return a new hard register.  */
6202               if (pos || in_dest)
6203                 {
6204                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6205
6206                   if (WORDS_BIG_ENDIAN
6207                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6208                     final_word = ((GET_MODE_SIZE (inner_mode)
6209                                    - GET_MODE_SIZE (tmode))
6210                                   / UNITS_PER_WORD) - final_word;
6211
6212                   final_word *= UNITS_PER_WORD;
6213                   if (BYTES_BIG_ENDIAN &&
6214                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6215                     final_word += (GET_MODE_SIZE (inner_mode)
6216                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6217
6218                   /* Avoid creating invalid subregs, for example when
6219                      simplifying (x>>32)&255.  */
6220                   if (final_word >= GET_MODE_SIZE (inner_mode))
6221                     return NULL_RTX;
6222
6223                   new = gen_rtx_SUBREG (tmode, inner, final_word);
6224                 }
6225               else
6226                 new = gen_lowpart (tmode, inner);
6227             }
6228           else
6229             new = inner;
6230         }
6231       else
6232         new = force_to_mode (inner, tmode,
6233                              len >= HOST_BITS_PER_WIDE_INT
6234                              ? ~(unsigned HOST_WIDE_INT) 0
6235                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6236                              NULL_RTX, 0);
6237
6238       /* If this extraction is going into the destination of a SET,
6239          make a STRICT_LOW_PART unless we made a MEM.  */
6240
6241       if (in_dest)
6242         return (MEM_P (new) ? new
6243                 : (GET_CODE (new) != SUBREG
6244                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
6245                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6246
6247       if (mode == tmode)
6248         return new;
6249
6250       if (GET_CODE (new) == CONST_INT)
6251         return gen_int_mode (INTVAL (new), mode);
6252
6253       /* If we know that no extraneous bits are set, and that the high
6254          bit is not set, convert the extraction to the cheaper of
6255          sign and zero extension, that are equivalent in these cases.  */
6256       if (flag_expensive_optimizations
6257           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6258               && ((nonzero_bits (new, tmode)
6259                    & ~(((unsigned HOST_WIDE_INT)
6260                         GET_MODE_MASK (tmode))
6261                        >> 1))
6262                   == 0)))
6263         {
6264           rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6265           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6266
6267           /* Prefer ZERO_EXTENSION, since it gives more information to
6268              backends.  */
6269           if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6270             return temp;
6271           return temp1;
6272         }
6273
6274       /* Otherwise, sign- or zero-extend unless we already are in the
6275          proper mode.  */
6276
6277       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6278                              mode, new));
6279     }
6280
6281   /* Unless this is a COMPARE or we have a funny memory reference,
6282      don't do anything with zero-extending field extracts starting at
6283      the low-order bit since they are simple AND operations.  */
6284   if (pos_rtx == 0 && pos == 0 && ! in_dest
6285       && ! in_compare && ! spans_byte && unsignedp)
6286     return 0;
6287
6288   /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6289      we would be spanning bytes or if the position is not a constant and the
6290      length is not 1.  In all other cases, we would only be going outside
6291      our object in cases when an original shift would have been
6292      undefined.  */
6293   if (! spans_byte && MEM_P (inner)
6294       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6295           || (pos_rtx != 0 && len != 1)))
6296     return 0;
6297
6298   /* Get the mode to use should INNER not be a MEM, the mode for the position,
6299      and the mode for the result.  */
6300   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6301     {
6302       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6303       pos_mode = mode_for_extraction (EP_insv, 2);
6304       extraction_mode = mode_for_extraction (EP_insv, 3);
6305     }
6306
6307   if (! in_dest && unsignedp
6308       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6309     {
6310       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6311       pos_mode = mode_for_extraction (EP_extzv, 3);
6312       extraction_mode = mode_for_extraction (EP_extzv, 0);
6313     }
6314
6315   if (! in_dest && ! unsignedp
6316       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6317     {
6318       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6319       pos_mode = mode_for_extraction (EP_extv, 3);
6320       extraction_mode = mode_for_extraction (EP_extv, 0);
6321     }
6322
6323   /* Never narrow an object, since that might not be safe.  */
6324
6325   if (mode != VOIDmode
6326       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6327     extraction_mode = mode;
6328
6329   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6330       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6331     pos_mode = GET_MODE (pos_rtx);
6332
6333   /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6334      if we have to change the mode of memory and cannot, the desired mode is
6335      EXTRACTION_MODE.  */
6336   if (!MEM_P (inner))
6337     wanted_inner_mode = wanted_inner_reg_mode;
6338   else if (inner_mode != wanted_inner_mode
6339            && (mode_dependent_address_p (XEXP (inner, 0))
6340                || MEM_VOLATILE_P (inner)))
6341     wanted_inner_mode = extraction_mode;
6342
6343   orig_pos = pos;
6344
6345   if (BITS_BIG_ENDIAN)
6346     {
6347       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6348          BITS_BIG_ENDIAN style.  If position is constant, compute new
6349          position.  Otherwise, build subtraction.
6350          Note that POS is relative to the mode of the original argument.
6351          If it's a MEM we need to recompute POS relative to that.
6352          However, if we're extracting from (or inserting into) a register,
6353          we want to recompute POS relative to wanted_inner_mode.  */
6354       int width = (MEM_P (inner)
6355                    ? GET_MODE_BITSIZE (is_mode)
6356                    : GET_MODE_BITSIZE (wanted_inner_mode));
6357
6358       if (pos_rtx == 0)
6359         pos = width - len - pos;
6360       else
6361         pos_rtx
6362           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6363       /* POS may be less than 0 now, but we check for that below.
6364          Note that it can only be less than 0 if !MEM_P (inner).  */
6365     }
6366
6367   /* If INNER has a wider mode, make it smaller.  If this is a constant
6368      extract, try to adjust the byte to point to the byte containing
6369      the value.  */
6370   if (wanted_inner_mode != VOIDmode
6371       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6372       && ((MEM_P (inner)
6373            && (inner_mode == wanted_inner_mode
6374                || (! mode_dependent_address_p (XEXP (inner, 0))
6375                    && ! MEM_VOLATILE_P (inner))))))
6376     {
6377       int offset = 0;
6378
6379       /* The computations below will be correct if the machine is big
6380          endian in both bits and bytes or little endian in bits and bytes.
6381          If it is mixed, we must adjust.  */
6382
6383       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6384          adjust OFFSET to compensate.  */
6385       if (BYTES_BIG_ENDIAN
6386           && ! spans_byte
6387           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6388         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6389
6390       /* If this is a constant position, we can move to the desired byte.  */
6391       if (pos_rtx == 0)
6392         {
6393           offset += pos / BITS_PER_UNIT;
6394           pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6395         }
6396
6397       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6398           && ! spans_byte
6399           && is_mode != wanted_inner_mode)
6400         offset = (GET_MODE_SIZE (is_mode)
6401                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6402
6403       if (offset != 0 || inner_mode != wanted_inner_mode)
6404         inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6405     }
6406
6407   /* If INNER is not memory, we can always get it into the proper mode.  If we
6408      are changing its mode, POS must be a constant and smaller than the size
6409      of the new mode.  */
6410   else if (!MEM_P (inner))
6411     {
6412       if (GET_MODE (inner) != wanted_inner_mode
6413           && (pos_rtx != 0
6414               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6415         return 0;
6416
6417       inner = force_to_mode (inner, wanted_inner_mode,
6418                              pos_rtx
6419                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6420                              ? ~(unsigned HOST_WIDE_INT) 0
6421                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6422                                 << orig_pos),
6423                              NULL_RTX, 0);
6424     }
6425
6426   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6427      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6428   if (pos_rtx != 0
6429       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6430     {
6431       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6432
6433       /* If we know that no extraneous bits are set, and that the high
6434          bit is not set, convert extraction to cheaper one - either
6435          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6436          cases.  */
6437       if (flag_expensive_optimizations
6438           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6439               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6440                    & ~(((unsigned HOST_WIDE_INT)
6441                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6442                        >> 1))
6443                   == 0)))
6444         {
6445           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6446
6447           /* Prefer ZERO_EXTENSION, since it gives more information to
6448              backends.  */
6449           if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6450             temp = temp1;
6451         }
6452       pos_rtx = temp;
6453     }
6454   else if (pos_rtx != 0
6455            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6456     pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6457
6458   /* Make POS_RTX unless we already have it and it is correct.  If we don't
6459      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6460      be a CONST_INT.  */
6461   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6462     pos_rtx = orig_pos_rtx;
6463
6464   else if (pos_rtx == 0)
6465     pos_rtx = GEN_INT (pos);
6466
6467   /* Make the required operation.  See if we can use existing rtx.  */
6468   new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6469                          extraction_mode, inner, GEN_INT (len), pos_rtx);
6470   if (! in_dest)
6471     new = gen_lowpart (mode, new);
6472
6473   return new;
6474 }
6475 \f
6476 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6477    with any other operations in X.  Return X without that shift if so.  */
6478
6479 static rtx
6480 extract_left_shift (rtx x, int count)
6481 {
6482   enum rtx_code code = GET_CODE (x);
6483   enum machine_mode mode = GET_MODE (x);
6484   rtx tem;
6485
6486   switch (code)
6487     {
6488     case ASHIFT:
6489       /* This is the shift itself.  If it is wide enough, we will return
6490          either the value being shifted if the shift count is equal to
6491          COUNT or a shift for the difference.  */
6492       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6493           && INTVAL (XEXP (x, 1)) >= count)
6494         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6495                                      INTVAL (XEXP (x, 1)) - count);
6496       break;
6497
6498     case NEG:  case NOT:
6499       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6500         return simplify_gen_unary (code, mode, tem, mode);
6501
6502       break;
6503
6504     case PLUS:  case IOR:  case XOR:  case AND:
6505       /* If we can safely shift this constant and we find the inner shift,
6506          make a new operation.  */
6507       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6508           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6509           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6510         return simplify_gen_binary (code, mode, tem,
6511                                     GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6512
6513       break;
6514
6515     default:
6516       break;
6517     }
6518
6519   return 0;
6520 }
6521 \f
6522 /* Look at the expression rooted at X.  Look for expressions
6523    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6524    Form these expressions.
6525
6526    Return the new rtx, usually just X.
6527
6528    Also, for machines like the VAX that don't have logical shift insns,
6529    try to convert logical to arithmetic shift operations in cases where
6530    they are equivalent.  This undoes the canonicalizations to logical
6531    shifts done elsewhere.
6532
6533    We try, as much as possible, to re-use rtl expressions to save memory.
6534
6535    IN_CODE says what kind of expression we are processing.  Normally, it is
6536    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
6537    being kludges), it is MEM.  When processing the arguments of a comparison
6538    or a COMPARE against zero, it is COMPARE.  */
6539
6540 static rtx
6541 make_compound_operation (rtx x, enum rtx_code in_code)
6542 {
6543   enum rtx_code code = GET_CODE (x);
6544   enum machine_mode mode = GET_MODE (x);
6545   int mode_width = GET_MODE_BITSIZE (mode);
6546   rtx rhs, lhs;
6547   enum rtx_code next_code;
6548   int i;
6549   rtx new = 0;
6550   rtx tem;
6551   const char *fmt;
6552
6553   /* Select the code to be used in recursive calls.  Once we are inside an
6554      address, we stay there.  If we have a comparison, set to COMPARE,
6555      but once inside, go back to our default of SET.  */
6556
6557   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6558                : ((code == COMPARE || COMPARISON_P (x))
6559                   && XEXP (x, 1) == const0_rtx) ? COMPARE
6560                : in_code == COMPARE ? SET : in_code);
6561
6562   /* Process depending on the code of this operation.  If NEW is set
6563      nonzero, it will be returned.  */
6564
6565   switch (code)
6566     {
6567     case ASHIFT:
6568       /* Convert shifts by constants into multiplications if inside
6569          an address.  */
6570       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6571           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6572           && INTVAL (XEXP (x, 1)) >= 0)
6573         {
6574           new = make_compound_operation (XEXP (x, 0), next_code);
6575           new = gen_rtx_MULT (mode, new,
6576                               GEN_INT ((HOST_WIDE_INT) 1
6577                                        << INTVAL (XEXP (x, 1))));
6578         }
6579       break;
6580
6581     case AND:
6582       /* If the second operand is not a constant, we can't do anything
6583          with it.  */
6584       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6585         break;
6586
6587       /* If the constant is a power of two minus one and the first operand
6588          is a logical right shift, make an extraction.  */
6589       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6590           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6591         {
6592           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6593           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6594                                  0, in_code == COMPARE);
6595         }
6596
6597       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
6598       else if (GET_CODE (XEXP (x, 0)) == SUBREG
6599                && subreg_lowpart_p (XEXP (x, 0))
6600                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6601                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6602         {
6603           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6604                                          next_code);
6605           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6606                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6607                                  0, in_code == COMPARE);
6608         }
6609       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
6610       else if ((GET_CODE (XEXP (x, 0)) == XOR
6611                 || GET_CODE (XEXP (x, 0)) == IOR)
6612                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6613                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6614                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6615         {
6616           /* Apply the distributive law, and then try to make extractions.  */
6617           new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6618                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6619                                              XEXP (x, 1)),
6620                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6621                                              XEXP (x, 1)));
6622           new = make_compound_operation (new, in_code);
6623         }
6624
6625       /* If we are have (and (rotate X C) M) and C is larger than the number
6626          of bits in M, this is an extraction.  */
6627
6628       else if (GET_CODE (XEXP (x, 0)) == ROTATE
6629                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6630                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6631                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6632         {
6633           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6634           new = make_extraction (mode, new,
6635                                  (GET_MODE_BITSIZE (mode)
6636                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
6637                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
6638         }
6639
6640       /* On machines without logical shifts, if the operand of the AND is
6641          a logical shift and our mask turns off all the propagated sign
6642          bits, we can replace the logical shift with an arithmetic shift.  */
6643       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6644                && !have_insn_for (LSHIFTRT, mode)
6645                && have_insn_for (ASHIFTRT, mode)
6646                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6647                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6648                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6649                && mode_width <= HOST_BITS_PER_WIDE_INT)
6650         {
6651           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6652
6653           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6654           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6655             SUBST (XEXP (x, 0),
6656                    gen_rtx_ASHIFTRT (mode,
6657                                      make_compound_operation
6658                                      (XEXP (XEXP (x, 0), 0), next_code),
6659                                      XEXP (XEXP (x, 0), 1)));
6660         }
6661
6662       /* If the constant is one less than a power of two, this might be
6663          representable by an extraction even if no shift is present.
6664          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6665          we are in a COMPARE.  */
6666       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6667         new = make_extraction (mode,
6668                                make_compound_operation (XEXP (x, 0),
6669                                                         next_code),
6670                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6671
6672       /* If we are in a comparison and this is an AND with a power of two,
6673          convert this into the appropriate bit extract.  */
6674       else if (in_code == COMPARE
6675                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6676         new = make_extraction (mode,
6677                                make_compound_operation (XEXP (x, 0),
6678                                                         next_code),
6679                                i, NULL_RTX, 1, 1, 0, 1);
6680
6681       break;
6682
6683     case LSHIFTRT:
6684       /* If the sign bit is known to be zero, replace this with an
6685          arithmetic shift.  */
6686       if (have_insn_for (ASHIFTRT, mode)
6687           && ! have_insn_for (LSHIFTRT, mode)
6688           && mode_width <= HOST_BITS_PER_WIDE_INT
6689           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6690         {
6691           new = gen_rtx_ASHIFTRT (mode,
6692                                   make_compound_operation (XEXP (x, 0),
6693                                                            next_code),
6694                                   XEXP (x, 1));
6695           break;
6696         }
6697
6698       /* ... fall through ...  */
6699
6700     case ASHIFTRT:
6701       lhs = XEXP (x, 0);
6702       rhs = XEXP (x, 1);
6703
6704       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6705          this is a SIGN_EXTRACT.  */
6706       if (GET_CODE (rhs) == CONST_INT
6707           && GET_CODE (lhs) == ASHIFT
6708           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6709           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6710         {
6711           new = make_compound_operation (XEXP (lhs, 0), next_code);
6712           new = make_extraction (mode, new,
6713                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6714                                  NULL_RTX, mode_width - INTVAL (rhs),
6715                                  code == LSHIFTRT, 0, in_code == COMPARE);
6716           break;
6717         }
6718
6719       /* See if we have operations between an ASHIFTRT and an ASHIFT.
6720          If so, try to merge the shifts into a SIGN_EXTEND.  We could
6721          also do this for some cases of SIGN_EXTRACT, but it doesn't
6722          seem worth the effort; the case checked for occurs on Alpha.  */
6723
6724       if (!OBJECT_P (lhs)
6725           && ! (GET_CODE (lhs) == SUBREG
6726                 && (OBJECT_P (SUBREG_REG (lhs))))
6727           && GET_CODE (rhs) == CONST_INT
6728           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6729           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6730         new = make_extraction (mode, make_compound_operation (new, next_code),
6731                                0, NULL_RTX, mode_width - INTVAL (rhs),
6732                                code == LSHIFTRT, 0, in_code == COMPARE);
6733
6734       break;
6735
6736     case SUBREG:
6737       /* Call ourselves recursively on the inner expression.  If we are
6738          narrowing the object and it has a different RTL code from
6739          what it originally did, do this SUBREG as a force_to_mode.  */
6740
6741       tem = make_compound_operation (SUBREG_REG (x), in_code);
6742
6743       {
6744         rtx simplified;
6745         simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
6746                                       SUBREG_BYTE (x));
6747
6748         if (simplified)
6749           tem = simplified;
6750
6751         if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6752             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6753             && subreg_lowpart_p (x))
6754           {
6755             rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6756                                        NULL_RTX, 0);
6757             
6758             /* If we have something other than a SUBREG, we might have
6759                done an expansion, so rerun ourselves.  */
6760             if (GET_CODE (newer) != SUBREG)
6761               newer = make_compound_operation (newer, in_code);
6762             
6763             return newer;
6764           }
6765
6766         if (simplified)
6767           return tem;
6768       }
6769       break;
6770
6771     default:
6772       break;
6773     }
6774
6775   if (new)
6776     {
6777       x = gen_lowpart (mode, new);
6778       code = GET_CODE (x);
6779     }
6780
6781   /* Now recursively process each operand of this operation.  */
6782   fmt = GET_RTX_FORMAT (code);
6783   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6784     if (fmt[i] == 'e')
6785       {
6786         new = make_compound_operation (XEXP (x, i), next_code);
6787         SUBST (XEXP (x, i), new);
6788       }
6789
6790   return x;
6791 }
6792 \f
6793 /* Given M see if it is a value that would select a field of bits
6794    within an item, but not the entire word.  Return -1 if not.
6795    Otherwise, return the starting position of the field, where 0 is the
6796    low-order bit.
6797
6798    *PLEN is set to the length of the field.  */
6799
6800 static int
6801 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6802 {
6803   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
6804   int pos = exact_log2 (m & -m);
6805   int len = 0;
6806
6807   if (pos >= 0)
6808     /* Now shift off the low-order zero bits and see if we have a
6809        power of two minus 1.  */
6810     len = exact_log2 ((m >> pos) + 1);
6811
6812   if (len <= 0)
6813     pos = -1;
6814
6815   *plen = len;
6816   return pos;
6817 }
6818 \f
6819 /* See if X can be simplified knowing that we will only refer to it in
6820    MODE and will only refer to those bits that are nonzero in MASK.
6821    If other bits are being computed or if masking operations are done
6822    that select a superset of the bits in MASK, they can sometimes be
6823    ignored.
6824
6825    Return a possibly simplified expression, but always convert X to
6826    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
6827
6828    Also, if REG is nonzero and X is a register equal in value to REG,
6829    replace X with REG.
6830
6831    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6832    are all off in X.  This is used when X will be complemented, by either
6833    NOT, NEG, or XOR.  */
6834
6835 static rtx
6836 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6837                rtx reg, int just_select)
6838 {
6839   enum rtx_code code = GET_CODE (x);
6840   int next_select = just_select || code == XOR || code == NOT || code == NEG;
6841   enum machine_mode op_mode;
6842   unsigned HOST_WIDE_INT fuller_mask, nonzero;
6843   rtx op0, op1, temp;
6844
6845   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
6846      code below will do the wrong thing since the mode of such an
6847      expression is VOIDmode.
6848
6849      Also do nothing if X is a CLOBBER; this can happen if X was
6850      the return value from a call to gen_lowpart.  */
6851   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6852     return x;
6853
6854   /* We want to perform the operation is its present mode unless we know
6855      that the operation is valid in MODE, in which case we do the operation
6856      in MODE.  */
6857   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6858               && have_insn_for (code, mode))
6859              ? mode : GET_MODE (x));
6860
6861   /* It is not valid to do a right-shift in a narrower mode
6862      than the one it came in with.  */
6863   if ((code == LSHIFTRT || code == ASHIFTRT)
6864       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6865     op_mode = GET_MODE (x);
6866
6867   /* Truncate MASK to fit OP_MODE.  */
6868   if (op_mode)
6869     mask &= GET_MODE_MASK (op_mode);
6870
6871   /* When we have an arithmetic operation, or a shift whose count we
6872      do not know, we need to assume that all bits up to the highest-order
6873      bit in MASK will be needed.  This is how we form such a mask.  */
6874   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6875     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6876   else
6877     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6878                    - 1);
6879
6880   /* Determine what bits of X are guaranteed to be (non)zero.  */
6881   nonzero = nonzero_bits (x, mode);
6882
6883   /* If none of the bits in X are needed, return a zero.  */
6884   if (! just_select && (nonzero & mask) == 0)
6885     x = const0_rtx;
6886
6887   /* If X is a CONST_INT, return a new one.  Do this here since the
6888      test below will fail.  */
6889   if (GET_CODE (x) == CONST_INT)
6890     {
6891       if (SCALAR_INT_MODE_P (mode))
6892         return gen_int_mode (INTVAL (x) & mask, mode);
6893       else
6894         {
6895           x = GEN_INT (INTVAL (x) & mask);
6896           return gen_lowpart_common (mode, x);
6897         }
6898     }
6899
6900   /* If X is narrower than MODE and we want all the bits in X's mode, just
6901      get X in the proper mode.  */
6902   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6903       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6904     return gen_lowpart (mode, x);
6905
6906   switch (code)
6907     {
6908     case CLOBBER:
6909       /* If X is a (clobber (const_int)), return it since we know we are
6910          generating something that won't match.  */
6911       return x;
6912
6913     case USE:
6914       /* X is a (use (mem ..)) that was made from a bit-field extraction that
6915          spanned the boundary of the MEM.  If we are now masking so it is
6916          within that boundary, we don't need the USE any more.  */
6917       if (! BITS_BIG_ENDIAN
6918           && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6919         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6920       break;
6921
6922     case SIGN_EXTEND:
6923     case ZERO_EXTEND:
6924     case ZERO_EXTRACT:
6925     case SIGN_EXTRACT:
6926       x = expand_compound_operation (x);
6927       if (GET_CODE (x) != code)
6928         return force_to_mode (x, mode, mask, reg, next_select);
6929       break;
6930
6931     case REG:
6932       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6933                        || rtx_equal_p (reg, get_last_value (x))))
6934         x = reg;
6935       break;
6936
6937     case SUBREG:
6938       if (subreg_lowpart_p (x)
6939           /* We can ignore the effect of this SUBREG if it narrows the mode or
6940              if the constant masks to zero all the bits the mode doesn't
6941              have.  */
6942           && ((GET_MODE_SIZE (GET_MODE (x))
6943                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6944               || (0 == (mask
6945                         & GET_MODE_MASK (GET_MODE (x))
6946                         & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6947         return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6948       break;
6949
6950     case AND:
6951       /* If this is an AND with a constant, convert it into an AND
6952          whose constant is the AND of that constant with MASK.  If it
6953          remains an AND of MASK, delete it since it is redundant.  */
6954
6955       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6956         {
6957           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6958                                       mask & INTVAL (XEXP (x, 1)));
6959
6960           /* If X is still an AND, see if it is an AND with a mask that
6961              is just some low-order bits.  If so, and it is MASK, we don't
6962              need it.  */
6963
6964           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6965               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6966                   == mask))
6967             x = XEXP (x, 0);
6968
6969           /* If it remains an AND, try making another AND with the bits
6970              in the mode mask that aren't in MASK turned on.  If the
6971              constant in the AND is wide enough, this might make a
6972              cheaper constant.  */
6973
6974           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6975               && GET_MODE_MASK (GET_MODE (x)) != mask
6976               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6977             {
6978               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6979                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6980               int width = GET_MODE_BITSIZE (GET_MODE (x));
6981               rtx y;
6982
6983               /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
6984                  number, sign extend it.  */
6985               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6986                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6987                 cval |= (HOST_WIDE_INT) -1 << width;
6988
6989               y = simplify_gen_binary (AND, GET_MODE (x),
6990                                        XEXP (x, 0), GEN_INT (cval));
6991               if (rtx_cost (y, SET) < rtx_cost (x, SET))
6992                 x = y;
6993             }
6994
6995           break;
6996         }
6997
6998       goto binop;
6999
7000     case PLUS:
7001       /* In (and (plus FOO C1) M), if M is a mask that just turns off
7002          low-order bits (as in an alignment operation) and FOO is already
7003          aligned to that boundary, mask C1 to that boundary as well.
7004          This may eliminate that PLUS and, later, the AND.  */
7005
7006       {
7007         unsigned int width = GET_MODE_BITSIZE (mode);
7008         unsigned HOST_WIDE_INT smask = mask;
7009
7010         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7011            number, sign extend it.  */
7012
7013         if (width < HOST_BITS_PER_WIDE_INT
7014             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7015           smask |= (HOST_WIDE_INT) -1 << width;
7016
7017         if (GET_CODE (XEXP (x, 1)) == CONST_INT
7018             && exact_log2 (- smask) >= 0
7019             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7020             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7021           return force_to_mode (plus_constant (XEXP (x, 0),
7022                                                (INTVAL (XEXP (x, 1)) & smask)),
7023                                 mode, smask, reg, next_select);
7024       }
7025
7026       /* ... fall through ...  */
7027
7028     case MULT:
7029       /* For PLUS, MINUS and MULT, we need any bits less significant than the
7030          most significant bit in MASK since carries from those bits will
7031          affect the bits we are interested in.  */
7032       mask = fuller_mask;
7033       goto binop;
7034
7035     case MINUS:
7036       /* If X is (minus C Y) where C's least set bit is larger than any bit
7037          in the mask, then we may replace with (neg Y).  */
7038       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7039           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7040                                         & -INTVAL (XEXP (x, 0))))
7041               > mask))
7042         {
7043           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7044                                   GET_MODE (x));
7045           return force_to_mode (x, mode, mask, reg, next_select);
7046         }
7047
7048       /* Similarly, if C contains every bit in the fuller_mask, then we may
7049          replace with (not Y).  */
7050       if (GET_CODE (XEXP (x, 0)) == CONST_INT
7051           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7052               == INTVAL (XEXP (x, 0))))
7053         {
7054           x = simplify_gen_unary (NOT, GET_MODE (x),
7055                                   XEXP (x, 1), GET_MODE (x));
7056           return force_to_mode (x, mode, mask, reg, next_select);
7057         }
7058
7059       mask = fuller_mask;
7060       goto binop;
7061
7062     case IOR:
7063     case XOR:
7064       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7065          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7066          operation which may be a bitfield extraction.  Ensure that the
7067          constant we form is not wider than the mode of X.  */
7068
7069       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7070           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7071           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7072           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7073           && GET_CODE (XEXP (x, 1)) == CONST_INT
7074           && ((INTVAL (XEXP (XEXP (x, 0), 1))
7075                + floor_log2 (INTVAL (XEXP (x, 1))))
7076               < GET_MODE_BITSIZE (GET_MODE (x)))
7077           && (INTVAL (XEXP (x, 1))
7078               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7079         {
7080           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7081                           << INTVAL (XEXP (XEXP (x, 0), 1)));
7082           temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7083                                       XEXP (XEXP (x, 0), 0), temp);
7084           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7085                                    XEXP (XEXP (x, 0), 1));
7086           return force_to_mode (x, mode, mask, reg, next_select);
7087         }
7088
7089     binop:
7090       /* For most binary operations, just propagate into the operation and
7091          change the mode if we have an operation of that mode.  */
7092
7093       op0 = gen_lowpart (op_mode,
7094                          force_to_mode (XEXP (x, 0), mode, mask,
7095                                         reg, next_select));
7096       op1 = gen_lowpart (op_mode,
7097                          force_to_mode (XEXP (x, 1), mode, mask,
7098                                         reg, next_select));
7099
7100       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7101         x = simplify_gen_binary (code, op_mode, op0, op1);
7102       break;
7103
7104     case ASHIFT:
7105       /* For left shifts, do the same, but just for the first operand.
7106          However, we cannot do anything with shifts where we cannot
7107          guarantee that the counts are smaller than the size of the mode
7108          because such a count will have a different meaning in a
7109          wider mode.  */
7110
7111       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7112              && INTVAL (XEXP (x, 1)) >= 0
7113              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7114           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7115                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7116                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7117         break;
7118
7119       /* If the shift count is a constant and we can do arithmetic in
7120          the mode of the shift, refine which bits we need.  Otherwise, use the
7121          conservative form of the mask.  */
7122       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7123           && INTVAL (XEXP (x, 1)) >= 0
7124           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7125           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7126         mask >>= INTVAL (XEXP (x, 1));
7127       else
7128         mask = fuller_mask;
7129
7130       op0 = gen_lowpart (op_mode,
7131                          force_to_mode (XEXP (x, 0), op_mode,
7132                                         mask, reg, next_select));
7133
7134       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7135         x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7136       break;
7137
7138     case LSHIFTRT:
7139       /* Here we can only do something if the shift count is a constant,
7140          this shift constant is valid for the host, and we can do arithmetic
7141          in OP_MODE.  */
7142
7143       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7144           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7145           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7146         {
7147           rtx inner = XEXP (x, 0);
7148           unsigned HOST_WIDE_INT inner_mask;
7149
7150           /* Select the mask of the bits we need for the shift operand.  */
7151           inner_mask = mask << INTVAL (XEXP (x, 1));
7152
7153           /* We can only change the mode of the shift if we can do arithmetic
7154              in the mode of the shift and INNER_MASK is no wider than the
7155              width of X's mode.  */
7156           if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7157             op_mode = GET_MODE (x);
7158
7159           inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7160
7161           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7162             x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7163         }
7164
7165       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7166          shift and AND produces only copies of the sign bit (C2 is one less
7167          than a power of two), we can do this with just a shift.  */
7168
7169       if (GET_CODE (x) == LSHIFTRT
7170           && GET_CODE (XEXP (x, 1)) == CONST_INT
7171           /* The shift puts one of the sign bit copies in the least significant
7172              bit.  */
7173           && ((INTVAL (XEXP (x, 1))
7174                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7175               >= GET_MODE_BITSIZE (GET_MODE (x)))
7176           && exact_log2 (mask + 1) >= 0
7177           /* Number of bits left after the shift must be more than the mask
7178              needs.  */
7179           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7180               <= GET_MODE_BITSIZE (GET_MODE (x)))
7181           /* Must be more sign bit copies than the mask needs.  */
7182           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7183               >= exact_log2 (mask + 1)))
7184         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7185                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7186                                           - exact_log2 (mask + 1)));
7187
7188       goto shiftrt;
7189
7190     case ASHIFTRT:
7191       /* If we are just looking for the sign bit, we don't need this shift at
7192          all, even if it has a variable count.  */
7193       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7194           && (mask == ((unsigned HOST_WIDE_INT) 1
7195                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7196         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7197
7198       /* If this is a shift by a constant, get a mask that contains those bits
7199          that are not copies of the sign bit.  We then have two cases:  If
7200          MASK only includes those bits, this can be a logical shift, which may
7201          allow simplifications.  If MASK is a single-bit field not within
7202          those bits, we are requesting a copy of the sign bit and hence can
7203          shift the sign bit to the appropriate location.  */
7204
7205       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7206           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7207         {
7208           int i = -1;
7209
7210           /* If the considered data is wider than HOST_WIDE_INT, we can't
7211              represent a mask for all its bits in a single scalar.
7212              But we only care about the lower bits, so calculate these.  */
7213
7214           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7215             {
7216               nonzero = ~(HOST_WIDE_INT) 0;
7217
7218               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7219                  is the number of bits a full-width mask would have set.
7220                  We need only shift if these are fewer than nonzero can
7221                  hold.  If not, we must keep all bits set in nonzero.  */
7222
7223               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7224                   < HOST_BITS_PER_WIDE_INT)
7225                 nonzero >>= INTVAL (XEXP (x, 1))
7226                             + HOST_BITS_PER_WIDE_INT
7227                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7228             }
7229           else
7230             {
7231               nonzero = GET_MODE_MASK (GET_MODE (x));
7232               nonzero >>= INTVAL (XEXP (x, 1));
7233             }
7234
7235           if ((mask & ~nonzero) == 0
7236               || (i = exact_log2 (mask)) >= 0)
7237             {
7238               x = simplify_shift_const
7239                 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7240                  i < 0 ? INTVAL (XEXP (x, 1))
7241                  : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7242
7243               if (GET_CODE (x) != ASHIFTRT)
7244                 return force_to_mode (x, mode, mask, reg, next_select);
7245             }
7246         }
7247
7248       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7249          even if the shift count isn't a constant.  */
7250       if (mask == 1)
7251         x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7252                                  XEXP (x, 0), XEXP (x, 1));
7253
7254     shiftrt:
7255
7256       /* If this is a zero- or sign-extension operation that just affects bits
7257          we don't care about, remove it.  Be sure the call above returned
7258          something that is still a shift.  */
7259
7260       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7261           && GET_CODE (XEXP (x, 1)) == CONST_INT
7262           && INTVAL (XEXP (x, 1)) >= 0
7263           && (INTVAL (XEXP (x, 1))
7264               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7265           && GET_CODE (XEXP (x, 0)) == ASHIFT
7266           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7267         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7268                               reg, next_select);
7269
7270       break;
7271
7272     case ROTATE:
7273     case ROTATERT:
7274       /* If the shift count is constant and we can do computations
7275          in the mode of X, compute where the bits we care about are.
7276          Otherwise, we can't do anything.  Don't change the mode of
7277          the shift or propagate MODE into the shift, though.  */
7278       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7279           && INTVAL (XEXP (x, 1)) >= 0)
7280         {
7281           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7282                                             GET_MODE (x), GEN_INT (mask),
7283                                             XEXP (x, 1));
7284           if (temp && GET_CODE (temp) == CONST_INT)
7285             SUBST (XEXP (x, 0),
7286                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7287                                   INTVAL (temp), reg, next_select));
7288         }
7289       break;
7290
7291     case NEG:
7292       /* If we just want the low-order bit, the NEG isn't needed since it
7293          won't change the low-order bit.  */
7294       if (mask == 1)
7295         return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7296
7297       /* We need any bits less significant than the most significant bit in
7298          MASK since carries from those bits will affect the bits we are
7299          interested in.  */
7300       mask = fuller_mask;
7301       goto unop;
7302
7303     case NOT:
7304       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7305          same as the XOR case above.  Ensure that the constant we form is not
7306          wider than the mode of X.  */
7307
7308       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7309           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7310           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7311           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7312               < GET_MODE_BITSIZE (GET_MODE (x)))
7313           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7314         {
7315           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7316                                GET_MODE (x));
7317           temp = simplify_gen_binary (XOR, GET_MODE (x),
7318                                       XEXP (XEXP (x, 0), 0), temp);
7319           x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7320                                    temp, XEXP (XEXP (x, 0), 1));
7321
7322           return force_to_mode (x, mode, mask, reg, next_select);
7323         }
7324
7325       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7326          use the full mask inside the NOT.  */
7327       mask = fuller_mask;
7328
7329     unop:
7330       op0 = gen_lowpart (op_mode,
7331                          force_to_mode (XEXP (x, 0), mode, mask,
7332                                         reg, next_select));
7333       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7334         x = simplify_gen_unary (code, op_mode, op0, op_mode);
7335       break;
7336
7337     case NE:
7338       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7339          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7340          which is equal to STORE_FLAG_VALUE.  */
7341       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7342           && GET_MODE (XEXP (x, 0)) == mode
7343           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7344           && (nonzero_bits (XEXP (x, 0), mode)
7345               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7346         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7347
7348       break;
7349
7350     case IF_THEN_ELSE:
7351       /* We have no way of knowing if the IF_THEN_ELSE can itself be
7352          written in a narrower mode.  We play it safe and do not do so.  */
7353
7354       SUBST (XEXP (x, 1),
7355              gen_lowpart (GET_MODE (x),
7356                                       force_to_mode (XEXP (x, 1), mode,
7357                                                      mask, reg, next_select)));
7358       SUBST (XEXP (x, 2),
7359              gen_lowpart (GET_MODE (x),
7360                                       force_to_mode (XEXP (x, 2), mode,
7361                                                      mask, reg, next_select)));
7362       break;
7363
7364     default:
7365       break;
7366     }
7367
7368   /* Ensure we return a value of the proper mode.  */
7369   return gen_lowpart (mode, x);
7370 }
7371 \f
7372 /* Return nonzero if X is an expression that has one of two values depending on
7373    whether some other value is zero or nonzero.  In that case, we return the
7374    value that is being tested, *PTRUE is set to the value if the rtx being
7375    returned has a nonzero value, and *PFALSE is set to the other alternative.
7376
7377    If we return zero, we set *PTRUE and *PFALSE to X.  */
7378
7379 static rtx
7380 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7381 {
7382   enum machine_mode mode = GET_MODE (x);
7383   enum rtx_code code = GET_CODE (x);
7384   rtx cond0, cond1, true0, true1, false0, false1;
7385   unsigned HOST_WIDE_INT nz;
7386
7387   /* If we are comparing a value against zero, we are done.  */
7388   if ((code == NE || code == EQ)
7389       && XEXP (x, 1) == const0_rtx)
7390     {
7391       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7392       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7393       return XEXP (x, 0);
7394     }
7395
7396   /* If this is a unary operation whose operand has one of two values, apply
7397      our opcode to compute those values.  */
7398   else if (UNARY_P (x)
7399            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7400     {
7401       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7402       *pfalse = simplify_gen_unary (code, mode, false0,
7403                                     GET_MODE (XEXP (x, 0)));
7404       return cond0;
7405     }
7406
7407   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7408      make can't possibly match and would suppress other optimizations.  */
7409   else if (code == COMPARE)
7410     ;
7411
7412   /* If this is a binary operation, see if either side has only one of two
7413      values.  If either one does or if both do and they are conditional on
7414      the same value, compute the new true and false values.  */
7415   else if (BINARY_P (x))
7416     {
7417       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7418       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7419
7420       if ((cond0 != 0 || cond1 != 0)
7421           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7422         {
7423           /* If if_then_else_cond returned zero, then true/false are the
7424              same rtl.  We must copy one of them to prevent invalid rtl
7425              sharing.  */
7426           if (cond0 == 0)
7427             true0 = copy_rtx (true0);
7428           else if (cond1 == 0)
7429             true1 = copy_rtx (true1);
7430
7431           if (COMPARISON_P (x))
7432             {
7433               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7434                                                 true0, true1);
7435               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7436                                                  false0, false1);
7437              }
7438           else
7439             {
7440               *ptrue = simplify_gen_binary (code, mode, true0, true1);
7441               *pfalse = simplify_gen_binary (code, mode, false0, false1);
7442             }
7443
7444           return cond0 ? cond0 : cond1;
7445         }
7446
7447       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7448          operands is zero when the other is nonzero, and vice-versa,
7449          and STORE_FLAG_VALUE is 1 or -1.  */
7450
7451       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7452           && (code == PLUS || code == IOR || code == XOR || code == MINUS
7453               || code == UMAX)
7454           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7455         {
7456           rtx op0 = XEXP (XEXP (x, 0), 1);
7457           rtx op1 = XEXP (XEXP (x, 1), 1);
7458
7459           cond0 = XEXP (XEXP (x, 0), 0);
7460           cond1 = XEXP (XEXP (x, 1), 0);
7461
7462           if (COMPARISON_P (cond0)
7463               && COMPARISON_P (cond1)
7464               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7465                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7466                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7467                   || ((swap_condition (GET_CODE (cond0))
7468                        == reversed_comparison_code (cond1, NULL))
7469                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7470                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7471               && ! side_effects_p (x))
7472             {
7473               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7474               *pfalse = simplify_gen_binary (MULT, mode,
7475                                              (code == MINUS
7476                                               ? simplify_gen_unary (NEG, mode,
7477                                                                     op1, mode)
7478                                               : op1),
7479                                               const_true_rtx);
7480               return cond0;
7481             }
7482         }
7483
7484       /* Similarly for MULT, AND and UMIN, except that for these the result
7485          is always zero.  */
7486       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7487           && (code == MULT || code == AND || code == UMIN)
7488           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7489         {
7490           cond0 = XEXP (XEXP (x, 0), 0);
7491           cond1 = XEXP (XEXP (x, 1), 0);
7492
7493           if (COMPARISON_P (cond0)
7494               && COMPARISON_P (cond1)
7495               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7496                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7497                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7498                   || ((swap_condition (GET_CODE (cond0))
7499                        == reversed_comparison_code (cond1, NULL))
7500                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7501                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7502               && ! side_effects_p (x))
7503             {
7504               *ptrue = *pfalse = const0_rtx;
7505               return cond0;
7506             }
7507         }
7508     }
7509
7510   else if (code == IF_THEN_ELSE)
7511     {
7512       /* If we have IF_THEN_ELSE already, extract the condition and
7513          canonicalize it if it is NE or EQ.  */
7514       cond0 = XEXP (x, 0);
7515       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7516       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7517         return XEXP (cond0, 0);
7518       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7519         {
7520           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7521           return XEXP (cond0, 0);
7522         }
7523       else
7524         return cond0;
7525     }
7526
7527   /* If X is a SUBREG, we can narrow both the true and false values
7528      if the inner expression, if there is a condition.  */
7529   else if (code == SUBREG
7530            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7531                                                &true0, &false0)))
7532     {
7533       true0 = simplify_gen_subreg (mode, true0,
7534                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7535       false0 = simplify_gen_subreg (mode, false0,
7536                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7537       if (true0 && false0)
7538         {
7539           *ptrue = true0;
7540           *pfalse = false0;
7541           return cond0;
7542         }
7543     }
7544
7545   /* If X is a constant, this isn't special and will cause confusions
7546      if we treat it as such.  Likewise if it is equivalent to a constant.  */
7547   else if (CONSTANT_P (x)
7548            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7549     ;
7550
7551   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7552      will be least confusing to the rest of the compiler.  */
7553   else if (mode == BImode)
7554     {
7555       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7556       return x;
7557     }
7558
7559   /* If X is known to be either 0 or -1, those are the true and
7560      false values when testing X.  */
7561   else if (x == constm1_rtx || x == const0_rtx
7562            || (mode != VOIDmode
7563                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7564     {
7565       *ptrue = constm1_rtx, *pfalse = const0_rtx;
7566       return x;
7567     }
7568
7569   /* Likewise for 0 or a single bit.  */
7570   else if (SCALAR_INT_MODE_P (mode)
7571            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7572            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7573     {
7574       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7575       return x;
7576     }
7577
7578   /* Otherwise fail; show no condition with true and false values the same.  */
7579   *ptrue = *pfalse = x;
7580   return 0;
7581 }
7582 \f
7583 /* Return the value of expression X given the fact that condition COND
7584    is known to be true when applied to REG as its first operand and VAL
7585    as its second.  X is known to not be shared and so can be modified in
7586    place.
7587
7588    We only handle the simplest cases, and specifically those cases that
7589    arise with IF_THEN_ELSE expressions.  */
7590
7591 static rtx
7592 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7593 {
7594   enum rtx_code code = GET_CODE (x);
7595   rtx temp;
7596   const char *fmt;
7597   int i, j;
7598
7599   if (side_effects_p (x))
7600     return x;
7601
7602   /* If either operand of the condition is a floating point value,
7603      then we have to avoid collapsing an EQ comparison.  */
7604   if (cond == EQ
7605       && rtx_equal_p (x, reg)
7606       && ! FLOAT_MODE_P (GET_MODE (x))
7607       && ! FLOAT_MODE_P (GET_MODE (val)))
7608     return val;
7609
7610   if (cond == UNEQ && rtx_equal_p (x, reg))
7611     return val;
7612
7613   /* If X is (abs REG) and we know something about REG's relationship
7614      with zero, we may be able to simplify this.  */
7615
7616   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7617     switch (cond)
7618       {
7619       case GE:  case GT:  case EQ:
7620         return XEXP (x, 0);
7621       case LT:  case LE:
7622         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7623                                    XEXP (x, 0),
7624                                    GET_MODE (XEXP (x, 0)));
7625       default:
7626         break;
7627       }
7628
7629   /* The only other cases we handle are MIN, MAX, and comparisons if the
7630      operands are the same as REG and VAL.  */
7631
7632   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7633     {
7634       if (rtx_equal_p (XEXP (x, 0), val))
7635         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7636
7637       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7638         {
7639           if (COMPARISON_P (x))
7640             {
7641               if (comparison_dominates_p (cond, code))
7642                 return const_true_rtx;
7643
7644               code = reversed_comparison_code (x, NULL);
7645               if (code != UNKNOWN
7646                   && comparison_dominates_p (cond, code))
7647                 return const0_rtx;
7648               else
7649                 return x;
7650             }
7651           else if (code == SMAX || code == SMIN
7652                    || code == UMIN || code == UMAX)
7653             {
7654               int unsignedp = (code == UMIN || code == UMAX);
7655
7656               /* Do not reverse the condition when it is NE or EQ.
7657                  This is because we cannot conclude anything about
7658                  the value of 'SMAX (x, y)' when x is not equal to y,
7659                  but we can when x equals y.  */
7660               if ((code == SMAX || code == UMAX)
7661                   && ! (cond == EQ || cond == NE))
7662                 cond = reverse_condition (cond);
7663
7664               switch (cond)
7665                 {
7666                 case GE:   case GT:
7667                   return unsignedp ? x : XEXP (x, 1);
7668                 case LE:   case LT:
7669                   return unsignedp ? x : XEXP (x, 0);
7670                 case GEU:  case GTU:
7671                   return unsignedp ? XEXP (x, 1) : x;
7672                 case LEU:  case LTU:
7673                   return unsignedp ? XEXP (x, 0) : x;
7674                 default:
7675                   break;
7676                 }
7677             }
7678         }
7679     }
7680   else if (code == SUBREG)
7681     {
7682       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7683       rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7684
7685       if (SUBREG_REG (x) != r)
7686         {
7687           /* We must simplify subreg here, before we lose track of the
7688              original inner_mode.  */
7689           new = simplify_subreg (GET_MODE (x), r,
7690                                  inner_mode, SUBREG_BYTE (x));
7691           if (new)
7692             return new;
7693           else
7694             SUBST (SUBREG_REG (x), r);
7695         }
7696
7697       return x;
7698     }
7699   /* We don't have to handle SIGN_EXTEND here, because even in the
7700      case of replacing something with a modeless CONST_INT, a
7701      CONST_INT is already (supposed to be) a valid sign extension for
7702      its narrower mode, which implies it's already properly
7703      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
7704      story is different.  */
7705   else if (code == ZERO_EXTEND)
7706     {
7707       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7708       rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7709
7710       if (XEXP (x, 0) != r)
7711         {
7712           /* We must simplify the zero_extend here, before we lose
7713              track of the original inner_mode.  */
7714           new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7715                                           r, inner_mode);
7716           if (new)
7717             return new;
7718           else
7719             SUBST (XEXP (x, 0), r);
7720         }
7721
7722       return x;
7723     }
7724
7725   fmt = GET_RTX_FORMAT (code);
7726   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7727     {
7728       if (fmt[i] == 'e')
7729         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7730       else if (fmt[i] == 'E')
7731         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7732           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7733                                                 cond, reg, val));
7734     }
7735
7736   return x;
7737 }
7738 \f
7739 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7740    assignment as a field assignment.  */
7741
7742 static int
7743 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7744 {
7745   if (x == y || rtx_equal_p (x, y))
7746     return 1;
7747
7748   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7749     return 0;
7750
7751   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7752      Note that all SUBREGs of MEM are paradoxical; otherwise they
7753      would have been rewritten.  */
7754   if (MEM_P (x) && GET_CODE (y) == SUBREG
7755       && MEM_P (SUBREG_REG (y))
7756       && rtx_equal_p (SUBREG_REG (y),
7757                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
7758     return 1;
7759
7760   if (MEM_P (y) && GET_CODE (x) == SUBREG
7761       && MEM_P (SUBREG_REG (x))
7762       && rtx_equal_p (SUBREG_REG (x),
7763                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
7764     return 1;
7765
7766   /* We used to see if get_last_value of X and Y were the same but that's
7767      not correct.  In one direction, we'll cause the assignment to have
7768      the wrong destination and in the case, we'll import a register into this
7769      insn that might have already have been dead.   So fail if none of the
7770      above cases are true.  */
7771   return 0;
7772 }
7773 \f
7774 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7775    Return that assignment if so.
7776
7777    We only handle the most common cases.  */
7778
7779 static rtx
7780 make_field_assignment (rtx x)
7781 {
7782   rtx dest = SET_DEST (x);
7783   rtx src = SET_SRC (x);
7784   rtx assign;
7785   rtx rhs, lhs;
7786   HOST_WIDE_INT c1;
7787   HOST_WIDE_INT pos;
7788   unsigned HOST_WIDE_INT len;
7789   rtx other;
7790   enum machine_mode mode;
7791
7792   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7793      a clear of a one-bit field.  We will have changed it to
7794      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
7795      for a SUBREG.  */
7796
7797   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7798       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7799       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7800       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7801     {
7802       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7803                                 1, 1, 1, 0);
7804       if (assign != 0)
7805         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7806       return x;
7807     }
7808
7809   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7810       && subreg_lowpart_p (XEXP (src, 0))
7811       && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7812           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7813       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7814       && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7815       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7816       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7817     {
7818       assign = make_extraction (VOIDmode, dest, 0,
7819                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7820                                 1, 1, 1, 0);
7821       if (assign != 0)
7822         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7823       return x;
7824     }
7825
7826   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7827      one-bit field.  */
7828   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7829       && XEXP (XEXP (src, 0), 0) == const1_rtx
7830       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7831     {
7832       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7833                                 1, 1, 1, 0);
7834       if (assign != 0)
7835         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7836       return x;
7837     }
7838
7839   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
7840      SRC is an AND with all bits of that field set, then we can discard
7841      the AND.  */
7842   if (GET_CODE (dest) == ZERO_EXTRACT
7843       && GET_CODE (XEXP (dest, 1)) == CONST_INT
7844       && GET_CODE (src) == AND
7845       && GET_CODE (XEXP (src, 1)) == CONST_INT)
7846     {
7847       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
7848       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
7849       unsigned HOST_WIDE_INT ze_mask;
7850
7851       if (width >= HOST_BITS_PER_WIDE_INT)
7852         ze_mask = -1;
7853       else
7854         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
7855
7856       /* Complete overlap.  We can remove the source AND.  */
7857       if ((and_mask & ze_mask) == ze_mask)
7858         return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
7859
7860       /* Partial overlap.  We can reduce the source AND.  */
7861       if ((and_mask & ze_mask) != and_mask)
7862         {
7863           mode = GET_MODE (src);
7864           src = gen_rtx_AND (mode, XEXP (src, 0),
7865                              gen_int_mode (and_mask & ze_mask, mode));
7866           return gen_rtx_SET (VOIDmode, dest, src);
7867         }
7868     }
7869
7870   /* The other case we handle is assignments into a constant-position
7871      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
7872      a mask that has all one bits except for a group of zero bits and
7873      OTHER is known to have zeros where C1 has ones, this is such an
7874      assignment.  Compute the position and length from C1.  Shift OTHER
7875      to the appropriate position, force it to the required mode, and
7876      make the extraction.  Check for the AND in both operands.  */
7877
7878   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7879     return x;
7880
7881   rhs = expand_compound_operation (XEXP (src, 0));
7882   lhs = expand_compound_operation (XEXP (src, 1));
7883
7884   if (GET_CODE (rhs) == AND
7885       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7886       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7887     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7888   else if (GET_CODE (lhs) == AND
7889            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7890            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7891     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7892   else
7893     return x;
7894
7895   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7896   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7897       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7898       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7899     return x;
7900
7901   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7902   if (assign == 0)
7903     return x;
7904
7905   /* The mode to use for the source is the mode of the assignment, or of
7906      what is inside a possible STRICT_LOW_PART.  */
7907   mode = (GET_CODE (assign) == STRICT_LOW_PART
7908           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7909
7910   /* Shift OTHER right POS places and make it the source, restricting it
7911      to the proper length and mode.  */
7912
7913   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7914                                              GET_MODE (src), other, pos),
7915                        mode,
7916                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7917                        ? ~(unsigned HOST_WIDE_INT) 0
7918                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7919                        dest, 0);
7920
7921   /* If SRC is masked by an AND that does not make a difference in
7922      the value being stored, strip it.  */
7923   if (GET_CODE (assign) == ZERO_EXTRACT
7924       && GET_CODE (XEXP (assign, 1)) == CONST_INT
7925       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7926       && GET_CODE (src) == AND
7927       && GET_CODE (XEXP (src, 1)) == CONST_INT
7928       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7929           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7930     src = XEXP (src, 0);
7931
7932   return gen_rtx_SET (VOIDmode, assign, src);
7933 }
7934 \f
7935 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7936    if so.  */
7937
7938 static rtx
7939 apply_distributive_law (rtx x)
7940 {
7941   enum rtx_code code = GET_CODE (x);
7942   enum rtx_code inner_code;
7943   rtx lhs, rhs, other;
7944   rtx tem;
7945
7946   /* Distributivity is not true for floating point as it can change the
7947      value.  So we don't do it unless -funsafe-math-optimizations.  */
7948   if (FLOAT_MODE_P (GET_MODE (x))
7949       && ! flag_unsafe_math_optimizations)
7950     return x;
7951
7952   /* The outer operation can only be one of the following:  */
7953   if (code != IOR && code != AND && code != XOR
7954       && code != PLUS && code != MINUS)
7955     return x;
7956
7957   lhs = XEXP (x, 0);
7958   rhs = XEXP (x, 1);
7959
7960   /* If either operand is a primitive we can't do anything, so get out
7961      fast.  */
7962   if (OBJECT_P (lhs) || OBJECT_P (rhs))
7963     return x;
7964
7965   lhs = expand_compound_operation (lhs);
7966   rhs = expand_compound_operation (rhs);
7967   inner_code = GET_CODE (lhs);
7968   if (inner_code != GET_CODE (rhs))
7969     return x;
7970
7971   /* See if the inner and outer operations distribute.  */
7972   switch (inner_code)
7973     {
7974     case LSHIFTRT:
7975     case ASHIFTRT:
7976     case AND:
7977     case IOR:
7978       /* These all distribute except over PLUS.  */
7979       if (code == PLUS || code == MINUS)
7980         return x;
7981       break;
7982
7983     case MULT:
7984       if (code != PLUS && code != MINUS)
7985         return x;
7986       break;
7987
7988     case ASHIFT:
7989       /* This is also a multiply, so it distributes over everything.  */
7990       break;
7991
7992     case SUBREG:
7993       /* Non-paradoxical SUBREGs distributes over all operations, provided
7994          the inner modes and byte offsets are the same, this is an extraction
7995          of a low-order part, we don't convert an fp operation to int or
7996          vice versa, and we would not be converting a single-word
7997          operation into a multi-word operation.  The latter test is not
7998          required, but it prevents generating unneeded multi-word operations.
7999          Some of the previous tests are redundant given the latter test, but
8000          are retained because they are required for correctness.
8001
8002          We produce the result slightly differently in this case.  */
8003
8004       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8005           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8006           || ! subreg_lowpart_p (lhs)
8007           || (GET_MODE_CLASS (GET_MODE (lhs))
8008               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8009           || (GET_MODE_SIZE (GET_MODE (lhs))
8010               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8011           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
8012         return x;
8013
8014       tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8015                                  SUBREG_REG (lhs), SUBREG_REG (rhs));
8016       return gen_lowpart (GET_MODE (x), tem);
8017
8018     default:
8019       return x;
8020     }
8021
8022   /* Set LHS and RHS to the inner operands (A and B in the example
8023      above) and set OTHER to the common operand (C in the example).
8024      There is only one way to do this unless the inner operation is
8025      commutative.  */
8026   if (COMMUTATIVE_ARITH_P (lhs)
8027       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8028     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8029   else if (COMMUTATIVE_ARITH_P (lhs)
8030            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8031     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8032   else if (COMMUTATIVE_ARITH_P (lhs)
8033            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8034     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8035   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8036     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8037   else
8038     return x;
8039
8040   /* Form the new inner operation, seeing if it simplifies first.  */
8041   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8042
8043   /* There is one exception to the general way of distributing:
8044      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
8045   if (code == XOR && inner_code == IOR)
8046     {
8047       inner_code = AND;
8048       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8049     }
8050
8051   /* We may be able to continuing distributing the result, so call
8052      ourselves recursively on the inner operation before forming the
8053      outer operation, which we return.  */
8054   return simplify_gen_binary (inner_code, GET_MODE (x),
8055                               apply_distributive_law (tem), other);
8056 }
8057
8058 /* See if X is of the form (* (+ A B) C), and if so convert to
8059    (+ (* A C) (* B C)) and try to simplify.
8060
8061    Most of the time, this results in no change.  However, if some of
8062    the operands are the same or inverses of each other, simplifications
8063    will result.
8064
8065    For example, (and (ior A B) (not B)) can occur as the result of
8066    expanding a bit field assignment.  When we apply the distributive
8067    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8068    which then simplifies to (and (A (not B))).
8069  
8070    Note that no checks happen on the validity of applying the inverse
8071    distributive law.  This is pointless since we can do it in the
8072    few places where this routine is called.
8073
8074    N is the index of the term that is decomposed (the arithmetic operation,
8075    i.e. (+ A B) in the first example above).  !N is the index of the term that
8076    is distributed, i.e. of C in the first example above.  */
8077 static rtx
8078 distribute_and_simplify_rtx (rtx x, int n)
8079 {
8080   enum machine_mode mode;
8081   enum rtx_code outer_code, inner_code;
8082   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8083
8084   decomposed = XEXP (x, n);
8085   if (!ARITHMETIC_P (decomposed))
8086     return NULL_RTX;
8087
8088   mode = GET_MODE (x);
8089   outer_code = GET_CODE (x);
8090   distributed = XEXP (x, !n);
8091
8092   inner_code = GET_CODE (decomposed);
8093   inner_op0 = XEXP (decomposed, 0);
8094   inner_op1 = XEXP (decomposed, 1);
8095
8096   /* Special case (and (xor B C) (not A)), which is equivalent to
8097      (xor (ior A B) (ior A C))  */
8098   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8099     {
8100       distributed = XEXP (distributed, 0);
8101       outer_code = IOR;
8102     }
8103
8104   if (n == 0)
8105     {
8106       /* Distribute the second term.  */
8107       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8108       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8109     }
8110   else
8111     {
8112       /* Distribute the first term.  */
8113       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8114       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8115     }
8116
8117   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8118                                                      new_op0, new_op1));
8119   if (GET_CODE (tmp) != outer_code
8120       && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8121     return tmp;
8122
8123   return NULL_RTX;
8124 }
8125 \f
8126 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8127    in MODE.
8128
8129    Return an equivalent form, if different from X.  Otherwise, return X.  If
8130    X is zero, we are to always construct the equivalent form.  */
8131
8132 static rtx
8133 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8134                         unsigned HOST_WIDE_INT constop)
8135 {
8136   unsigned HOST_WIDE_INT nonzero;
8137   int i;
8138
8139   /* Simplify VAROP knowing that we will be only looking at some of the
8140      bits in it.
8141
8142      Note by passing in CONSTOP, we guarantee that the bits not set in
8143      CONSTOP are not significant and will never be examined.  We must
8144      ensure that is the case by explicitly masking out those bits
8145      before returning.  */
8146   varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
8147
8148   /* If VAROP is a CLOBBER, we will fail so return it.  */
8149   if (GET_CODE (varop) == CLOBBER)
8150     return varop;
8151
8152   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8153      to VAROP and return the new constant.  */
8154   if (GET_CODE (varop) == CONST_INT)
8155     return gen_int_mode (INTVAL (varop) & constop, mode);
8156
8157   /* See what bits may be nonzero in VAROP.  Unlike the general case of
8158      a call to nonzero_bits, here we don't care about bits outside
8159      MODE.  */
8160
8161   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8162
8163   /* Turn off all bits in the constant that are known to already be zero.
8164      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8165      which is tested below.  */
8166
8167   constop &= nonzero;
8168
8169   /* If we don't have any bits left, return zero.  */
8170   if (constop == 0)
8171     return const0_rtx;
8172
8173   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8174      a power of two, we can replace this with an ASHIFT.  */
8175   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8176       && (i = exact_log2 (constop)) >= 0)
8177     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8178
8179   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8180      or XOR, then try to apply the distributive law.  This may eliminate
8181      operations if either branch can be simplified because of the AND.
8182      It may also make some cases more complex, but those cases probably
8183      won't match a pattern either with or without this.  */
8184
8185   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8186     return
8187       gen_lowpart
8188         (mode,
8189          apply_distributive_law
8190          (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8191                                simplify_and_const_int (NULL_RTX,
8192                                                        GET_MODE (varop),
8193                                                        XEXP (varop, 0),
8194                                                        constop),
8195                                simplify_and_const_int (NULL_RTX,
8196                                                        GET_MODE (varop),
8197                                                        XEXP (varop, 1),
8198                                                        constop))));
8199
8200   /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
8201      the AND and see if one of the operands simplifies to zero.  If so, we
8202      may eliminate it.  */
8203
8204   if (GET_CODE (varop) == PLUS
8205       && exact_log2 (constop + 1) >= 0)
8206     {
8207       rtx o0, o1;
8208
8209       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8210       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8211       if (o0 == const0_rtx)
8212         return o1;
8213       if (o1 == const0_rtx)
8214         return o0;
8215     }
8216
8217   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
8218      if we already had one (just check for the simplest cases).  */
8219   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8220       && GET_MODE (XEXP (x, 0)) == mode
8221       && SUBREG_REG (XEXP (x, 0)) == varop)
8222     varop = XEXP (x, 0);
8223   else
8224     varop = gen_lowpart (mode, varop);
8225
8226   /* If we can't make the SUBREG, try to return what we were given.  */
8227   if (GET_CODE (varop) == CLOBBER)
8228     return x ? x : varop;
8229
8230   /* If we are only masking insignificant bits, return VAROP.  */
8231   if (constop == nonzero)
8232     x = varop;
8233   else
8234     {
8235       /* Otherwise, return an AND.  */
8236       constop = trunc_int_for_mode (constop, mode);
8237       /* See how much, if any, of X we can use.  */
8238       if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8239         x = simplify_gen_binary (AND, mode, varop, GEN_INT (constop));
8240
8241       else
8242         {
8243           if (GET_CODE (XEXP (x, 1)) != CONST_INT
8244               || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8245             SUBST (XEXP (x, 1), GEN_INT (constop));
8246
8247           SUBST (XEXP (x, 0), varop);
8248         }
8249     }
8250
8251   return x;
8252 }
8253 \f
8254 /* Given a REG, X, compute which bits in X can be nonzero.
8255    We don't care about bits outside of those defined in MODE.
8256
8257    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8258    a shift, AND, or zero_extract, we can do better.  */
8259
8260 static rtx
8261 reg_nonzero_bits_for_combine (rtx x, enum machine_mode mode,
8262                               rtx known_x ATTRIBUTE_UNUSED,
8263                               enum machine_mode known_mode ATTRIBUTE_UNUSED,
8264                               unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8265                               unsigned HOST_WIDE_INT *nonzero)
8266 {
8267   rtx tem;
8268
8269   /* If X is a register whose nonzero bits value is current, use it.
8270      Otherwise, if X is a register whose value we can find, use that
8271      value.  Otherwise, use the previously-computed global nonzero bits
8272      for this register.  */
8273
8274   if (reg_stat[REGNO (x)].last_set_value != 0
8275       && (reg_stat[REGNO (x)].last_set_mode == mode
8276           || (GET_MODE_CLASS (reg_stat[REGNO (x)].last_set_mode) == MODE_INT
8277               && GET_MODE_CLASS (mode) == MODE_INT))
8278       && (reg_stat[REGNO (x)].last_set_label == label_tick
8279           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8280               && REG_N_SETS (REGNO (x)) == 1
8281               && ! REGNO_REG_SET_P
8282                  (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
8283                   REGNO (x))))
8284       && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8285     {
8286       *nonzero &= reg_stat[REGNO (x)].last_set_nonzero_bits;
8287       return NULL;
8288     }
8289
8290   tem = get_last_value (x);
8291
8292   if (tem)
8293     {
8294 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8295       /* If X is narrower than MODE and TEM is a non-negative
8296          constant that would appear negative in the mode of X,
8297          sign-extend it for use in reg_nonzero_bits because some
8298          machines (maybe most) will actually do the sign-extension
8299          and this is the conservative approach.
8300
8301          ??? For 2.5, try to tighten up the MD files in this regard
8302          instead of this kludge.  */
8303
8304       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8305           && GET_CODE (tem) == CONST_INT
8306           && INTVAL (tem) > 0
8307           && 0 != (INTVAL (tem)
8308                    & ((HOST_WIDE_INT) 1
8309                       << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8310         tem = GEN_INT (INTVAL (tem)
8311                        | ((HOST_WIDE_INT) (-1)
8312                           << GET_MODE_BITSIZE (GET_MODE (x))));
8313 #endif
8314       return tem;
8315     }
8316   else if (nonzero_sign_valid && reg_stat[REGNO (x)].nonzero_bits)
8317     {
8318       unsigned HOST_WIDE_INT mask = reg_stat[REGNO (x)].nonzero_bits;
8319
8320       if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8321         /* We don't know anything about the upper bits.  */
8322         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8323       *nonzero &= mask;
8324     }
8325
8326   return NULL;
8327 }
8328
8329 /* Return the number of bits at the high-order end of X that are known to
8330    be equal to the sign bit.  X will be used in mode MODE; if MODE is
8331    VOIDmode, X will be used in its own mode.  The returned value  will always
8332    be between 1 and the number of bits in MODE.  */
8333
8334 static rtx
8335 reg_num_sign_bit_copies_for_combine (rtx x, enum machine_mode mode,
8336                                      rtx known_x ATTRIBUTE_UNUSED,
8337                                      enum machine_mode known_mode
8338                                      ATTRIBUTE_UNUSED,
8339                                      unsigned int known_ret ATTRIBUTE_UNUSED,
8340                                      unsigned int *result)
8341 {
8342   rtx tem;
8343
8344   if (reg_stat[REGNO (x)].last_set_value != 0
8345       && reg_stat[REGNO (x)].last_set_mode == mode
8346       && (reg_stat[REGNO (x)].last_set_label == label_tick
8347           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8348               && REG_N_SETS (REGNO (x)) == 1
8349               && ! REGNO_REG_SET_P
8350                  (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
8351                   REGNO (x))))
8352       && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8353     {
8354       *result = reg_stat[REGNO (x)].last_set_sign_bit_copies;
8355       return NULL;
8356     }
8357
8358   tem = get_last_value (x);
8359   if (tem != 0)
8360     return tem;
8361
8362   if (nonzero_sign_valid && reg_stat[REGNO (x)].sign_bit_copies != 0
8363       && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8364     *result = reg_stat[REGNO (x)].sign_bit_copies;
8365       
8366   return NULL;
8367 }
8368 \f
8369 /* Return the number of "extended" bits there are in X, when interpreted
8370    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
8371    unsigned quantities, this is the number of high-order zero bits.
8372    For signed quantities, this is the number of copies of the sign bit
8373    minus 1.  In both case, this function returns the number of "spare"
8374    bits.  For example, if two quantities for which this function returns
8375    at least 1 are added, the addition is known not to overflow.
8376
8377    This function will always return 0 unless called during combine, which
8378    implies that it must be called from a define_split.  */
8379
8380 unsigned int
8381 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8382 {
8383   if (nonzero_sign_valid == 0)
8384     return 0;
8385
8386   return (unsignedp
8387           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8388              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8389                                - floor_log2 (nonzero_bits (x, mode)))
8390              : 0)
8391           : num_sign_bit_copies (x, mode) - 1);
8392 }
8393 \f
8394 /* This function is called from `simplify_shift_const' to merge two
8395    outer operations.  Specifically, we have already found that we need
8396    to perform operation *POP0 with constant *PCONST0 at the outermost
8397    position.  We would now like to also perform OP1 with constant CONST1
8398    (with *POP0 being done last).
8399
8400    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8401    the resulting operation.  *PCOMP_P is set to 1 if we would need to
8402    complement the innermost operand, otherwise it is unchanged.
8403
8404    MODE is the mode in which the operation will be done.  No bits outside
8405    the width of this mode matter.  It is assumed that the width of this mode
8406    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8407
8408    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
8409    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
8410    result is simply *PCONST0.
8411
8412    If the resulting operation cannot be expressed as one operation, we
8413    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
8414
8415 static int
8416 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, enum machine_mode mode, int *pcomp_p)
8417 {
8418   enum rtx_code op0 = *pop0;
8419   HOST_WIDE_INT const0 = *pconst0;
8420
8421   const0 &= GET_MODE_MASK (mode);
8422   const1 &= GET_MODE_MASK (mode);
8423
8424   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
8425   if (op0 == AND)
8426     const1 &= const0;
8427
8428   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
8429      if OP0 is SET.  */
8430
8431   if (op1 == UNKNOWN || op0 == SET)
8432     return 1;
8433
8434   else if (op0 == UNKNOWN)
8435     op0 = op1, const0 = const1;
8436
8437   else if (op0 == op1)
8438     {
8439       switch (op0)
8440         {
8441         case AND:
8442           const0 &= const1;
8443           break;
8444         case IOR:
8445           const0 |= const1;
8446           break;
8447         case XOR:
8448           const0 ^= const1;
8449           break;
8450         case PLUS:
8451           const0 += const1;
8452           break;
8453         case NEG:
8454           op0 = UNKNOWN;
8455           break;
8456         default:
8457           break;
8458         }
8459     }
8460
8461   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
8462   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8463     return 0;
8464
8465   /* If the two constants aren't the same, we can't do anything.  The
8466      remaining six cases can all be done.  */
8467   else if (const0 != const1)
8468     return 0;
8469
8470   else
8471     switch (op0)
8472       {
8473       case IOR:
8474         if (op1 == AND)
8475           /* (a & b) | b == b */
8476           op0 = SET;
8477         else /* op1 == XOR */
8478           /* (a ^ b) | b == a | b */
8479           {;}
8480         break;
8481
8482       case XOR:
8483         if (op1 == AND)
8484           /* (a & b) ^ b == (~a) & b */
8485           op0 = AND, *pcomp_p = 1;
8486         else /* op1 == IOR */
8487           /* (a | b) ^ b == a & ~b */
8488           op0 = AND, const0 = ~const0;
8489         break;
8490
8491       case AND:
8492         if (op1 == IOR)
8493           /* (a | b) & b == b */
8494         op0 = SET;
8495         else /* op1 == XOR */
8496           /* (a ^ b) & b) == (~a) & b */
8497           *pcomp_p = 1;
8498         break;
8499       default:
8500         break;
8501       }
8502
8503   /* Check for NO-OP cases.  */
8504   const0 &= GET_MODE_MASK (mode);
8505   if (const0 == 0
8506       && (op0 == IOR || op0 == XOR || op0 == PLUS))
8507     op0 = UNKNOWN;
8508   else if (const0 == 0 && op0 == AND)
8509     op0 = SET;
8510   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8511            && op0 == AND)
8512     op0 = UNKNOWN;
8513
8514   /* ??? Slightly redundant with the above mask, but not entirely.
8515      Moving this above means we'd have to sign-extend the mode mask
8516      for the final test.  */
8517   const0 = trunc_int_for_mode (const0, mode);
8518
8519   *pop0 = op0;
8520   *pconst0 = const0;
8521
8522   return 1;
8523 }
8524 \f
8525 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
8526    The result of the shift is RESULT_MODE.  X, if nonzero, is an expression
8527    that we started with.
8528
8529    The shift is normally computed in the widest mode we find in VAROP, as
8530    long as it isn't a different number of words than RESULT_MODE.  Exceptions
8531    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
8532
8533 static rtx
8534 simplify_shift_const (rtx x, enum rtx_code code,
8535                       enum machine_mode result_mode, rtx varop,
8536                       int orig_count)
8537 {
8538   enum rtx_code orig_code = code;
8539   unsigned int count;
8540   int signed_count;
8541   enum machine_mode mode = result_mode;
8542   enum machine_mode shift_mode, tmode;
8543   unsigned int mode_words
8544     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8545   /* We form (outer_op (code varop count) (outer_const)).  */
8546   enum rtx_code outer_op = UNKNOWN;
8547   HOST_WIDE_INT outer_const = 0;
8548   rtx const_rtx;
8549   int complement_p = 0;
8550   rtx new;
8551
8552   /* Make sure and truncate the "natural" shift on the way in.  We don't
8553      want to do this inside the loop as it makes it more difficult to
8554      combine shifts.  */
8555   if (SHIFT_COUNT_TRUNCATED)
8556     orig_count &= GET_MODE_BITSIZE (mode) - 1;
8557
8558   /* If we were given an invalid count, don't do anything except exactly
8559      what was requested.  */
8560
8561   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8562     {
8563       if (x)
8564         return x;
8565
8566       return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
8567     }
8568
8569   count = orig_count;
8570
8571   /* Unless one of the branches of the `if' in this loop does a `continue',
8572      we will `break' the loop after the `if'.  */
8573
8574   while (count != 0)
8575     {
8576       /* If we have an operand of (clobber (const_int 0)), just return that
8577          value.  */
8578       if (GET_CODE (varop) == CLOBBER)
8579         return varop;
8580
8581       /* If we discovered we had to complement VAROP, leave.  Making a NOT
8582          here would cause an infinite loop.  */
8583       if (complement_p)
8584         break;
8585
8586       /* Convert ROTATERT to ROTATE.  */
8587       if (code == ROTATERT)
8588         {
8589           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8590           code = ROTATE;
8591           if (VECTOR_MODE_P (result_mode))
8592             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8593           else
8594             count = bitsize - count;
8595         }
8596
8597       /* We need to determine what mode we will do the shift in.  If the
8598          shift is a right shift or a ROTATE, we must always do it in the mode
8599          it was originally done in.  Otherwise, we can do it in MODE, the
8600          widest mode encountered.  */
8601       shift_mode
8602         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8603            ? result_mode : mode);
8604
8605       /* Handle cases where the count is greater than the size of the mode
8606          minus 1.  For ASHIFT, use the size minus one as the count (this can
8607          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
8608          take the count modulo the size.  For other shifts, the result is
8609          zero.
8610
8611          Since these shifts are being produced by the compiler by combining
8612          multiple operations, each of which are defined, we know what the
8613          result is supposed to be.  */
8614
8615       if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
8616         {
8617           if (code == ASHIFTRT)
8618             count = GET_MODE_BITSIZE (shift_mode) - 1;
8619           else if (code == ROTATE || code == ROTATERT)
8620             count %= GET_MODE_BITSIZE (shift_mode);
8621           else
8622             {
8623               /* We can't simply return zero because there may be an
8624                  outer op.  */
8625               varop = const0_rtx;
8626               count = 0;
8627               break;
8628             }
8629         }
8630
8631       /* An arithmetic right shift of a quantity known to be -1 or 0
8632          is a no-op.  */
8633       if (code == ASHIFTRT
8634           && (num_sign_bit_copies (varop, shift_mode)
8635               == GET_MODE_BITSIZE (shift_mode)))
8636         {
8637           count = 0;
8638           break;
8639         }
8640
8641       /* If we are doing an arithmetic right shift and discarding all but
8642          the sign bit copies, this is equivalent to doing a shift by the
8643          bitsize minus one.  Convert it into that shift because it will often
8644          allow other simplifications.  */
8645
8646       if (code == ASHIFTRT
8647           && (count + num_sign_bit_copies (varop, shift_mode)
8648               >= GET_MODE_BITSIZE (shift_mode)))
8649         count = GET_MODE_BITSIZE (shift_mode) - 1;
8650
8651       /* We simplify the tests below and elsewhere by converting
8652          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8653          `make_compound_operation' will convert it to an ASHIFTRT for
8654          those machines (such as VAX) that don't have an LSHIFTRT.  */
8655       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8656           && code == ASHIFTRT
8657           && ((nonzero_bits (varop, shift_mode)
8658                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8659               == 0))
8660         code = LSHIFTRT;
8661
8662       if (code == LSHIFTRT
8663           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8664           && !(nonzero_bits (varop, shift_mode) >> count))
8665         varop = const0_rtx;
8666       if (code == ASHIFT
8667           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8668           && !((nonzero_bits (varop, shift_mode) << count)
8669                & GET_MODE_MASK (shift_mode)))
8670         varop = const0_rtx;
8671
8672       switch (GET_CODE (varop))
8673         {
8674         case SIGN_EXTEND:
8675         case ZERO_EXTEND:
8676         case SIGN_EXTRACT:
8677         case ZERO_EXTRACT:
8678           new = expand_compound_operation (varop);
8679           if (new != varop)
8680             {
8681               varop = new;
8682               continue;
8683             }
8684           break;
8685
8686         case MEM:
8687           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8688              minus the width of a smaller mode, we can do this with a
8689              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
8690           if ((code == ASHIFTRT || code == LSHIFTRT)
8691               && ! mode_dependent_address_p (XEXP (varop, 0))
8692               && ! MEM_VOLATILE_P (varop)
8693               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8694                                          MODE_INT, 1)) != BLKmode)
8695             {
8696               new = adjust_address_nv (varop, tmode,
8697                                        BYTES_BIG_ENDIAN ? 0
8698                                        : count / BITS_PER_UNIT);
8699
8700               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8701                                      : ZERO_EXTEND, mode, new);
8702               count = 0;
8703               continue;
8704             }
8705           break;
8706
8707         case USE:
8708           /* Similar to the case above, except that we can only do this if
8709              the resulting mode is the same as that of the underlying
8710              MEM and adjust the address depending on the *bits* endianness
8711              because of the way that bit-field extract insns are defined.  */
8712           if ((code == ASHIFTRT || code == LSHIFTRT)
8713               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8714                                          MODE_INT, 1)) != BLKmode
8715               && tmode == GET_MODE (XEXP (varop, 0)))
8716             {
8717               if (BITS_BIG_ENDIAN)
8718                 new = XEXP (varop, 0);
8719               else
8720                 {
8721                   new = copy_rtx (XEXP (varop, 0));
8722                   SUBST (XEXP (new, 0),
8723                          plus_constant (XEXP (new, 0),
8724                                         count / BITS_PER_UNIT));
8725                 }
8726
8727               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8728                                      : ZERO_EXTEND, mode, new);
8729               count = 0;
8730               continue;
8731             }
8732           break;
8733
8734         case SUBREG:
8735           /* If VAROP is a SUBREG, strip it as long as the inner operand has
8736              the same number of words as what we've seen so far.  Then store
8737              the widest mode in MODE.  */
8738           if (subreg_lowpart_p (varop)
8739               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8740                   > GET_MODE_SIZE (GET_MODE (varop)))
8741               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8742                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8743                  == mode_words)
8744             {
8745               varop = SUBREG_REG (varop);
8746               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8747                 mode = GET_MODE (varop);
8748               continue;
8749             }
8750           break;
8751
8752         case MULT:
8753           /* Some machines use MULT instead of ASHIFT because MULT
8754              is cheaper.  But it is still better on those machines to
8755              merge two shifts into one.  */
8756           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8757               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8758             {
8759               varop
8760                 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
8761                                        XEXP (varop, 0),
8762                                        GEN_INT (exact_log2 (
8763                                                 INTVAL (XEXP (varop, 1)))));
8764               continue;
8765             }
8766           break;
8767
8768         case UDIV:
8769           /* Similar, for when divides are cheaper.  */
8770           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8771               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8772             {
8773               varop
8774                 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
8775                                        XEXP (varop, 0),
8776                                        GEN_INT (exact_log2 (
8777                                                 INTVAL (XEXP (varop, 1)))));
8778               continue;
8779             }
8780           break;
8781
8782         case ASHIFTRT:
8783           /* If we are extracting just the sign bit of an arithmetic
8784              right shift, that shift is not needed.  However, the sign
8785              bit of a wider mode may be different from what would be
8786              interpreted as the sign bit in a narrower mode, so, if
8787              the result is narrower, don't discard the shift.  */
8788           if (code == LSHIFTRT
8789               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8790               && (GET_MODE_BITSIZE (result_mode)
8791                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
8792             {
8793               varop = XEXP (varop, 0);
8794               continue;
8795             }
8796
8797           /* ... fall through ...  */
8798
8799         case LSHIFTRT:
8800         case ASHIFT:
8801         case ROTATE:
8802           /* Here we have two nested shifts.  The result is usually the
8803              AND of a new shift with a mask.  We compute the result below.  */
8804           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8805               && INTVAL (XEXP (varop, 1)) >= 0
8806               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8807               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8808               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8809             {
8810               enum rtx_code first_code = GET_CODE (varop);
8811               unsigned int first_count = INTVAL (XEXP (varop, 1));
8812               unsigned HOST_WIDE_INT mask;
8813               rtx mask_rtx;
8814
8815               /* We have one common special case.  We can't do any merging if
8816                  the inner code is an ASHIFTRT of a smaller mode.  However, if
8817                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8818                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8819                  we can convert it to
8820                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8821                  This simplifies certain SIGN_EXTEND operations.  */
8822               if (code == ASHIFT && first_code == ASHIFTRT
8823                   && count == (unsigned int)
8824                               (GET_MODE_BITSIZE (result_mode)
8825                                - GET_MODE_BITSIZE (GET_MODE (varop))))
8826                 {
8827                   /* C3 has the low-order C1 bits zero.  */
8828
8829                   mask = (GET_MODE_MASK (mode)
8830                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
8831
8832                   varop = simplify_and_const_int (NULL_RTX, result_mode,
8833                                                   XEXP (varop, 0), mask);
8834                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8835                                                 varop, count);
8836                   count = first_count;
8837                   code = ASHIFTRT;
8838                   continue;
8839                 }
8840
8841               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8842                  than C1 high-order bits equal to the sign bit, we can convert
8843                  this to either an ASHIFT or an ASHIFTRT depending on the
8844                  two counts.
8845
8846                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
8847
8848               if (code == ASHIFTRT && first_code == ASHIFT
8849                   && GET_MODE (varop) == shift_mode
8850                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8851                       > first_count))
8852                 {
8853                   varop = XEXP (varop, 0);
8854
8855                   signed_count = count - first_count;
8856                   if (signed_count < 0)
8857                     count = -signed_count, code = ASHIFT;
8858                   else
8859                     count = signed_count;
8860
8861                   continue;
8862                 }
8863
8864               /* There are some cases we can't do.  If CODE is ASHIFTRT,
8865                  we can only do this if FIRST_CODE is also ASHIFTRT.
8866
8867                  We can't do the case when CODE is ROTATE and FIRST_CODE is
8868                  ASHIFTRT.
8869
8870                  If the mode of this shift is not the mode of the outer shift,
8871                  we can't do this if either shift is a right shift or ROTATE.
8872
8873                  Finally, we can't do any of these if the mode is too wide
8874                  unless the codes are the same.
8875
8876                  Handle the case where the shift codes are the same
8877                  first.  */
8878
8879               if (code == first_code)
8880                 {
8881                   if (GET_MODE (varop) != result_mode
8882                       && (code == ASHIFTRT || code == LSHIFTRT
8883                           || code == ROTATE))
8884                     break;
8885
8886                   count += first_count;
8887                   varop = XEXP (varop, 0);
8888                   continue;
8889                 }
8890
8891               if (code == ASHIFTRT
8892                   || (code == ROTATE && first_code == ASHIFTRT)
8893                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8894                   || (GET_MODE (varop) != result_mode
8895                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
8896                           || first_code == ROTATE
8897                           || code == ROTATE)))
8898                 break;
8899
8900               /* To compute the mask to apply after the shift, shift the
8901                  nonzero bits of the inner shift the same way the
8902                  outer shift will.  */
8903
8904               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8905
8906               mask_rtx
8907                 = simplify_binary_operation (code, result_mode, mask_rtx,
8908                                              GEN_INT (count));
8909
8910               /* Give up if we can't compute an outer operation to use.  */
8911               if (mask_rtx == 0
8912                   || GET_CODE (mask_rtx) != CONST_INT
8913                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
8914                                         INTVAL (mask_rtx),
8915                                         result_mode, &complement_p))
8916                 break;
8917
8918               /* If the shifts are in the same direction, we add the
8919                  counts.  Otherwise, we subtract them.  */
8920               signed_count = count;
8921               if ((code == ASHIFTRT || code == LSHIFTRT)
8922                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8923                 signed_count += first_count;
8924               else
8925                 signed_count -= first_count;
8926
8927               /* If COUNT is positive, the new shift is usually CODE,
8928                  except for the two exceptions below, in which case it is
8929                  FIRST_CODE.  If the count is negative, FIRST_CODE should
8930                  always be used  */
8931               if (signed_count > 0
8932                   && ((first_code == ROTATE && code == ASHIFT)
8933                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
8934                 code = first_code, count = signed_count;
8935               else if (signed_count < 0)
8936                 code = first_code, count = -signed_count;
8937               else
8938                 count = signed_count;
8939
8940               varop = XEXP (varop, 0);
8941               continue;
8942             }
8943
8944           /* If we have (A << B << C) for any shift, we can convert this to
8945              (A << C << B).  This wins if A is a constant.  Only try this if
8946              B is not a constant.  */
8947
8948           else if (GET_CODE (varop) == code
8949                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
8950                    && 0 != (new
8951                             = simplify_binary_operation (code, mode,
8952                                                          XEXP (varop, 0),
8953                                                          GEN_INT (count))))
8954             {
8955               varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
8956               count = 0;
8957               continue;
8958             }
8959           break;
8960
8961         case NOT:
8962           /* Make this fit the case below.  */
8963           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
8964                                GEN_INT (GET_MODE_MASK (mode)));
8965           continue;
8966
8967         case IOR:
8968         case AND:
8969         case XOR:
8970           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8971              with C the size of VAROP - 1 and the shift is logical if
8972              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8973              we have an (le X 0) operation.   If we have an arithmetic shift
8974              and STORE_FLAG_VALUE is 1 or we have a logical shift with
8975              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
8976
8977           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8978               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8979               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8980               && (code == LSHIFTRT || code == ASHIFTRT)
8981               && count == (unsigned int)
8982                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
8983               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8984             {
8985               count = 0;
8986               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
8987                                   const0_rtx);
8988
8989               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8990                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
8991
8992               continue;
8993             }
8994
8995           /* If we have (shift (logical)), move the logical to the outside
8996              to allow it to possibly combine with another logical and the
8997              shift to combine with another shift.  This also canonicalizes to
8998              what a ZERO_EXTRACT looks like.  Also, some machines have
8999              (and (shift)) insns.  */
9000
9001           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9002               /* We can't do this if we have (ashiftrt (xor))  and the
9003                  constant has its sign bit set in shift_mode.  */
9004               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9005                    && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9006                                               shift_mode))
9007               && (new = simplify_binary_operation (code, result_mode,
9008                                                    XEXP (varop, 1),
9009                                                    GEN_INT (count))) != 0
9010               && GET_CODE (new) == CONST_INT
9011               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9012                                   INTVAL (new), result_mode, &complement_p))
9013             {
9014               varop = XEXP (varop, 0);
9015               continue;
9016             }
9017
9018           /* If we can't do that, try to simplify the shift in each arm of the
9019              logical expression, make a new logical expression, and apply
9020              the inverse distributive law.  This also can't be done
9021              for some (ashiftrt (xor)).  */
9022           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9023              && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9024                   && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9025                                              shift_mode)))
9026             {
9027               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9028                                               XEXP (varop, 0), count);
9029               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9030                                               XEXP (varop, 1), count);
9031
9032               varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9033                                            lhs, rhs);
9034               varop = apply_distributive_law (varop);
9035
9036               count = 0;
9037               continue; 
9038             }
9039           break;
9040
9041         case EQ:
9042           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9043              says that the sign bit can be tested, FOO has mode MODE, C is
9044              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9045              that may be nonzero.  */
9046           if (code == LSHIFTRT
9047               && XEXP (varop, 1) == const0_rtx
9048               && GET_MODE (XEXP (varop, 0)) == result_mode
9049               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9050               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9051               && ((STORE_FLAG_VALUE
9052                    & ((HOST_WIDE_INT) 1
9053                       < (GET_MODE_BITSIZE (result_mode) - 1))))
9054               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9055               && merge_outer_ops (&outer_op, &outer_const, XOR,
9056                                   (HOST_WIDE_INT) 1, result_mode,
9057                                   &complement_p))
9058             {
9059               varop = XEXP (varop, 0);
9060               count = 0;
9061               continue;
9062             }
9063           break;
9064
9065         case NEG:
9066           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9067              than the number of bits in the mode is equivalent to A.  */
9068           if (code == LSHIFTRT
9069               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9070               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9071             {
9072               varop = XEXP (varop, 0);
9073               count = 0;
9074               continue;
9075             }
9076
9077           /* NEG commutes with ASHIFT since it is multiplication.  Move the
9078              NEG outside to allow shifts to combine.  */
9079           if (code == ASHIFT
9080               && merge_outer_ops (&outer_op, &outer_const, NEG,
9081                                   (HOST_WIDE_INT) 0, result_mode,
9082                                   &complement_p))
9083             {
9084               varop = XEXP (varop, 0);
9085               continue;
9086             }
9087           break;
9088
9089         case PLUS:
9090           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9091              is one less than the number of bits in the mode is
9092              equivalent to (xor A 1).  */
9093           if (code == LSHIFTRT
9094               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9095               && XEXP (varop, 1) == constm1_rtx
9096               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9097               && merge_outer_ops (&outer_op, &outer_const, XOR,
9098                                   (HOST_WIDE_INT) 1, result_mode,
9099                                   &complement_p))
9100             {
9101               count = 0;
9102               varop = XEXP (varop, 0);
9103               continue;
9104             }
9105
9106           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9107              that might be nonzero in BAR are those being shifted out and those
9108              bits are known zero in FOO, we can replace the PLUS with FOO.
9109              Similarly in the other operand order.  This code occurs when
9110              we are computing the size of a variable-size array.  */
9111
9112           if ((code == ASHIFTRT || code == LSHIFTRT)
9113               && count < HOST_BITS_PER_WIDE_INT
9114               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9115               && (nonzero_bits (XEXP (varop, 1), result_mode)
9116                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9117             {
9118               varop = XEXP (varop, 0);
9119               continue;
9120             }
9121           else if ((code == ASHIFTRT || code == LSHIFTRT)
9122                    && count < HOST_BITS_PER_WIDE_INT
9123                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9124                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9125                             >> count)
9126                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9127                             & nonzero_bits (XEXP (varop, 1),
9128                                                  result_mode)))
9129             {
9130               varop = XEXP (varop, 1);
9131               continue;
9132             }
9133
9134           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
9135           if (code == ASHIFT
9136               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9137               && (new = simplify_binary_operation (ASHIFT, result_mode,
9138                                                    XEXP (varop, 1),
9139                                                    GEN_INT (count))) != 0
9140               && GET_CODE (new) == CONST_INT
9141               && merge_outer_ops (&outer_op, &outer_const, PLUS,
9142                                   INTVAL (new), result_mode, &complement_p))
9143             {
9144               varop = XEXP (varop, 0);
9145               continue;
9146             }
9147
9148           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9149              signbit', and attempt to change the PLUS to an XOR and move it to
9150              the outer operation as is done above in the AND/IOR/XOR case
9151              leg for shift(logical). See details in logical handling above
9152              for reasoning in doing so.  */
9153           if (code == LSHIFTRT
9154               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9155               && mode_signbit_p (result_mode, XEXP (varop, 1))
9156               && (new = simplify_binary_operation (code, result_mode,
9157                                                    XEXP (varop, 1),
9158                                                    GEN_INT (count))) != 0
9159               && GET_CODE (new) == CONST_INT
9160               && merge_outer_ops (&outer_op, &outer_const, XOR,
9161                                   INTVAL (new), result_mode, &complement_p))
9162             {
9163               varop = XEXP (varop, 0);
9164               continue;
9165             }
9166
9167           break;
9168
9169         case MINUS:
9170           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9171              with C the size of VAROP - 1 and the shift is logical if
9172              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9173              we have a (gt X 0) operation.  If the shift is arithmetic with
9174              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9175              we have a (neg (gt X 0)) operation.  */
9176
9177           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9178               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9179               && count == (unsigned int)
9180                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9181               && (code == LSHIFTRT || code == ASHIFTRT)
9182               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9183               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9184                  == count
9185               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9186             {
9187               count = 0;
9188               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9189                                   const0_rtx);
9190
9191               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9192                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9193
9194               continue;
9195             }
9196           break;
9197
9198         case TRUNCATE:
9199           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9200              if the truncate does not affect the value.  */
9201           if (code == LSHIFTRT
9202               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9203               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9204               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9205                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9206                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9207             {
9208               rtx varop_inner = XEXP (varop, 0);
9209
9210               varop_inner
9211                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9212                                     XEXP (varop_inner, 0),
9213                                     GEN_INT
9214                                     (count + INTVAL (XEXP (varop_inner, 1))));
9215               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9216               count = 0;
9217               continue;
9218             }
9219           break;
9220
9221         default:
9222           break;
9223         }
9224
9225       break;
9226     }
9227
9228   /* We need to determine what mode to do the shift in.  If the shift is
9229      a right shift or ROTATE, we must always do it in the mode it was
9230      originally done in.  Otherwise, we can do it in MODE, the widest mode
9231      encountered.  The code we care about is that of the shift that will
9232      actually be done, not the shift that was originally requested.  */
9233   shift_mode
9234     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9235        ? result_mode : mode);
9236
9237   /* We have now finished analyzing the shift.  The result should be
9238      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9239      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9240      to the result of the shift.  OUTER_CONST is the relevant constant,
9241      but we must turn off all bits turned off in the shift.
9242
9243      If we were passed a value for X, see if we can use any pieces of
9244      it.  If not, make new rtx.  */
9245
9246   if (x && GET_RTX_CLASS (GET_CODE (x)) == RTX_BIN_ARITH
9247       && GET_CODE (XEXP (x, 1)) == CONST_INT
9248       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9249     const_rtx = XEXP (x, 1);
9250   else
9251     const_rtx = GEN_INT (count);
9252
9253   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9254       && GET_MODE (XEXP (x, 0)) == shift_mode
9255       && SUBREG_REG (XEXP (x, 0)) == varop)
9256     varop = XEXP (x, 0);
9257   else if (GET_MODE (varop) != shift_mode)
9258     varop = gen_lowpart (shift_mode, varop);
9259
9260   /* If we can't make the SUBREG, try to return what we were given.  */
9261   if (GET_CODE (varop) == CLOBBER)
9262     return x ? x : varop;
9263
9264   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9265   if (new != 0)
9266     x = new;
9267   else
9268     x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9269
9270   /* If we have an outer operation and we just made a shift, it is
9271      possible that we could have simplified the shift were it not
9272      for the outer operation.  So try to do the simplification
9273      recursively.  */
9274
9275   if (outer_op != UNKNOWN && GET_CODE (x) == code
9276       && GET_CODE (XEXP (x, 1)) == CONST_INT)
9277     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9278                               INTVAL (XEXP (x, 1)));
9279
9280   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9281      turn off all the bits that the shift would have turned off.  */
9282   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9283     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9284                                 GET_MODE_MASK (result_mode) >> orig_count);
9285
9286   /* Do the remainder of the processing in RESULT_MODE.  */
9287   x = gen_lowpart (result_mode, x);
9288
9289   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9290      operation.  */
9291   if (complement_p)
9292     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9293
9294   if (outer_op != UNKNOWN)
9295     {
9296       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9297         outer_const = trunc_int_for_mode (outer_const, result_mode);
9298
9299       if (outer_op == AND)
9300         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9301       else if (outer_op == SET)
9302         /* This means that we have determined that the result is
9303            equivalent to a constant.  This should be rare.  */
9304         x = GEN_INT (outer_const);
9305       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9306         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9307       else
9308         x = simplify_gen_binary (outer_op, result_mode, x,
9309                                  GEN_INT (outer_const));
9310     }
9311
9312   return x;
9313 }
9314 \f
9315 /* Like recog, but we receive the address of a pointer to a new pattern.
9316    We try to match the rtx that the pointer points to.
9317    If that fails, we may try to modify or replace the pattern,
9318    storing the replacement into the same pointer object.
9319
9320    Modifications include deletion or addition of CLOBBERs.
9321
9322    PNOTES is a pointer to a location where any REG_UNUSED notes added for
9323    the CLOBBERs are placed.
9324
9325    The value is the final insn code from the pattern ultimately matched,
9326    or -1.  */
9327
9328 static int
9329 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9330 {
9331   rtx pat = *pnewpat;
9332   int insn_code_number;
9333   int num_clobbers_to_add = 0;
9334   int i;
9335   rtx notes = 0;
9336   rtx old_notes, old_pat;
9337
9338   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9339      we use to indicate that something didn't match.  If we find such a
9340      thing, force rejection.  */
9341   if (GET_CODE (pat) == PARALLEL)
9342     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9343       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9344           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9345         return -1;
9346
9347   old_pat = PATTERN (insn);
9348   old_notes = REG_NOTES (insn);
9349   PATTERN (insn) = pat;
9350   REG_NOTES (insn) = 0;
9351
9352   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9353
9354   /* If it isn't, there is the possibility that we previously had an insn
9355      that clobbered some register as a side effect, but the combined
9356      insn doesn't need to do that.  So try once more without the clobbers
9357      unless this represents an ASM insn.  */
9358
9359   if (insn_code_number < 0 && ! check_asm_operands (pat)
9360       && GET_CODE (pat) == PARALLEL)
9361     {
9362       int pos;
9363
9364       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9365         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9366           {
9367             if (i != pos)
9368               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9369             pos++;
9370           }
9371
9372       SUBST_INT (XVECLEN (pat, 0), pos);
9373
9374       if (pos == 1)
9375         pat = XVECEXP (pat, 0, 0);
9376
9377       PATTERN (insn) = pat;
9378       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9379     }
9380   PATTERN (insn) = old_pat;
9381   REG_NOTES (insn) = old_notes;
9382
9383   /* Recognize all noop sets, these will be killed by followup pass.  */
9384   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9385     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9386
9387   /* If we had any clobbers to add, make a new pattern than contains
9388      them.  Then check to make sure that all of them are dead.  */
9389   if (num_clobbers_to_add)
9390     {
9391       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9392                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
9393                                                   ? (XVECLEN (pat, 0)
9394                                                      + num_clobbers_to_add)
9395                                                   : num_clobbers_to_add + 1));
9396
9397       if (GET_CODE (pat) == PARALLEL)
9398         for (i = 0; i < XVECLEN (pat, 0); i++)
9399           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9400       else
9401         XVECEXP (newpat, 0, 0) = pat;
9402
9403       add_clobbers (newpat, insn_code_number);
9404
9405       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9406            i < XVECLEN (newpat, 0); i++)
9407         {
9408           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9409               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9410             return -1;
9411           notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9412                                      XEXP (XVECEXP (newpat, 0, i), 0), notes);
9413         }
9414       pat = newpat;
9415     }
9416
9417   *pnewpat = pat;
9418   *pnotes = notes;
9419
9420   return insn_code_number;
9421 }
9422 \f
9423 /* Like gen_lowpart_general but for use by combine.  In combine it
9424    is not possible to create any new pseudoregs.  However, it is
9425    safe to create invalid memory addresses, because combine will
9426    try to recognize them and all they will do is make the combine
9427    attempt fail.
9428
9429    If for some reason this cannot do its job, an rtx
9430    (clobber (const_int 0)) is returned.
9431    An insn containing that will not be recognized.  */
9432
9433 static rtx
9434 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9435 {
9436   enum machine_mode imode = GET_MODE (x);
9437   unsigned int osize = GET_MODE_SIZE (omode);
9438   unsigned int isize = GET_MODE_SIZE (imode);
9439   rtx result;
9440
9441   if (omode == imode)
9442     return x;
9443
9444   /* Return identity if this is a CONST or symbolic reference.  */
9445   if (omode == Pmode
9446       && (GET_CODE (x) == CONST
9447           || GET_CODE (x) == SYMBOL_REF
9448           || GET_CODE (x) == LABEL_REF))
9449     return x;
9450
9451   /* We can only support MODE being wider than a word if X is a
9452      constant integer or has a mode the same size.  */
9453   if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9454       && ! ((imode == VOIDmode
9455              && (GET_CODE (x) == CONST_INT
9456                  || GET_CODE (x) == CONST_DOUBLE))
9457             || isize == osize))
9458     goto fail;
9459
9460   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
9461      won't know what to do.  So we will strip off the SUBREG here and
9462      process normally.  */
9463   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9464     {
9465       x = SUBREG_REG (x);
9466
9467       /* For use in case we fall down into the address adjustments
9468          further below, we need to adjust the known mode and size of
9469          x; imode and isize, since we just adjusted x.  */
9470       imode = GET_MODE (x);
9471
9472       if (imode == omode)
9473         return x;
9474
9475       isize = GET_MODE_SIZE (imode);
9476     }
9477
9478   result = gen_lowpart_common (omode, x);
9479
9480 #ifdef CANNOT_CHANGE_MODE_CLASS
9481   if (result != 0 && GET_CODE (result) == SUBREG)
9482     record_subregs_of_mode (result);
9483 #endif
9484
9485   if (result)
9486     return result;
9487
9488   if (MEM_P (x))
9489     {
9490       int offset = 0;
9491
9492       /* Refuse to work on a volatile memory ref or one with a mode-dependent
9493          address.  */
9494       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9495         goto fail;
9496
9497       /* If we want to refer to something bigger than the original memref,
9498          generate a paradoxical subreg instead.  That will force a reload
9499          of the original memref X.  */
9500       if (isize < osize)
9501         return gen_rtx_SUBREG (omode, x, 0);
9502
9503       if (WORDS_BIG_ENDIAN)
9504         offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9505
9506       /* Adjust the address so that the address-after-the-data is
9507          unchanged.  */
9508       if (BYTES_BIG_ENDIAN)
9509         offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9510
9511       return adjust_address_nv (x, omode, offset);
9512     }
9513
9514   /* If X is a comparison operator, rewrite it in a new mode.  This
9515      probably won't match, but may allow further simplifications.  */
9516   else if (COMPARISON_P (x))
9517     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9518
9519   /* If we couldn't simplify X any other way, just enclose it in a
9520      SUBREG.  Normally, this SUBREG won't match, but some patterns may
9521      include an explicit SUBREG or we may simplify it further in combine.  */
9522   else
9523     {
9524       int offset = 0;
9525       rtx res;
9526
9527       offset = subreg_lowpart_offset (omode, imode);
9528       if (imode == VOIDmode)
9529         {
9530           imode = int_mode_for_mode (omode);
9531           x = gen_lowpart_common (imode, x);
9532           if (x == NULL)
9533             goto fail;
9534         }
9535       res = simplify_gen_subreg (omode, x, imode, offset);
9536       if (res)
9537         return res;
9538     }
9539
9540  fail:
9541   return gen_rtx_CLOBBER (imode, const0_rtx);
9542 }
9543 \f
9544 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9545    comparison code that will be tested.
9546
9547    The result is a possibly different comparison code to use.  *POP0 and
9548    *POP1 may be updated.
9549
9550    It is possible that we might detect that a comparison is either always
9551    true or always false.  However, we do not perform general constant
9552    folding in combine, so this knowledge isn't useful.  Such tautologies
9553    should have been detected earlier.  Hence we ignore all such cases.  */
9554
9555 static enum rtx_code
9556 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9557 {
9558   rtx op0 = *pop0;
9559   rtx op1 = *pop1;
9560   rtx tem, tem1;
9561   int i;
9562   enum machine_mode mode, tmode;
9563
9564   /* Try a few ways of applying the same transformation to both operands.  */
9565   while (1)
9566     {
9567 #ifndef WORD_REGISTER_OPERATIONS
9568       /* The test below this one won't handle SIGN_EXTENDs on these machines,
9569          so check specially.  */
9570       if (code != GTU && code != GEU && code != LTU && code != LEU
9571           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9572           && GET_CODE (XEXP (op0, 0)) == ASHIFT
9573           && GET_CODE (XEXP (op1, 0)) == ASHIFT
9574           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9575           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9576           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9577               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9578           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9579           && XEXP (op0, 1) == XEXP (op1, 1)
9580           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9581           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9582           && (INTVAL (XEXP (op0, 1))
9583               == (GET_MODE_BITSIZE (GET_MODE (op0))
9584                   - (GET_MODE_BITSIZE
9585                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9586         {
9587           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9588           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9589         }
9590 #endif
9591
9592       /* If both operands are the same constant shift, see if we can ignore the
9593          shift.  We can if the shift is a rotate or if the bits shifted out of
9594          this shift are known to be zero for both inputs and if the type of
9595          comparison is compatible with the shift.  */
9596       if (GET_CODE (op0) == GET_CODE (op1)
9597           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9598           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9599               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9600                   && (code != GT && code != LT && code != GE && code != LE))
9601               || (GET_CODE (op0) == ASHIFTRT
9602                   && (code != GTU && code != LTU
9603                       && code != GEU && code != LEU)))
9604           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9605           && INTVAL (XEXP (op0, 1)) >= 0
9606           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9607           && XEXP (op0, 1) == XEXP (op1, 1))
9608         {
9609           enum machine_mode mode = GET_MODE (op0);
9610           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9611           int shift_count = INTVAL (XEXP (op0, 1));
9612
9613           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9614             mask &= (mask >> shift_count) << shift_count;
9615           else if (GET_CODE (op0) == ASHIFT)
9616             mask = (mask & (mask << shift_count)) >> shift_count;
9617
9618           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9619               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9620             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9621           else
9622             break;
9623         }
9624
9625       /* If both operands are AND's of a paradoxical SUBREG by constant, the
9626          SUBREGs are of the same mode, and, in both cases, the AND would
9627          be redundant if the comparison was done in the narrower mode,
9628          do the comparison in the narrower mode (e.g., we are AND'ing with 1
9629          and the operand's possibly nonzero bits are 0xffffff01; in that case
9630          if we only care about QImode, we don't need the AND).  This case
9631          occurs if the output mode of an scc insn is not SImode and
9632          STORE_FLAG_VALUE == 1 (e.g., the 386).
9633
9634          Similarly, check for a case where the AND's are ZERO_EXTEND
9635          operations from some narrower mode even though a SUBREG is not
9636          present.  */
9637
9638       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9639                && GET_CODE (XEXP (op0, 1)) == CONST_INT
9640                && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9641         {
9642           rtx inner_op0 = XEXP (op0, 0);
9643           rtx inner_op1 = XEXP (op1, 0);
9644           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9645           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9646           int changed = 0;
9647
9648           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9649               && (GET_MODE_SIZE (GET_MODE (inner_op0))
9650                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9651               && (GET_MODE (SUBREG_REG (inner_op0))
9652                   == GET_MODE (SUBREG_REG (inner_op1)))
9653               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9654                   <= HOST_BITS_PER_WIDE_INT)
9655               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9656                                              GET_MODE (SUBREG_REG (inner_op0)))))
9657               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9658                                              GET_MODE (SUBREG_REG (inner_op1))))))
9659             {
9660               op0 = SUBREG_REG (inner_op0);
9661               op1 = SUBREG_REG (inner_op1);
9662
9663               /* The resulting comparison is always unsigned since we masked
9664                  off the original sign bit.  */
9665               code = unsigned_condition (code);
9666
9667               changed = 1;
9668             }
9669
9670           else if (c0 == c1)
9671             for (tmode = GET_CLASS_NARROWEST_MODE
9672                  (GET_MODE_CLASS (GET_MODE (op0)));
9673                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9674               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9675                 {
9676                   op0 = gen_lowpart (tmode, inner_op0);
9677                   op1 = gen_lowpart (tmode, inner_op1);
9678                   code = unsigned_condition (code);
9679                   changed = 1;
9680                   break;
9681                 }
9682
9683           if (! changed)
9684             break;
9685         }
9686
9687       /* If both operands are NOT, we can strip off the outer operation
9688          and adjust the comparison code for swapped operands; similarly for
9689          NEG, except that this must be an equality comparison.  */
9690       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9691                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9692                    && (code == EQ || code == NE)))
9693         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9694
9695       else
9696         break;
9697     }
9698
9699   /* If the first operand is a constant, swap the operands and adjust the
9700      comparison code appropriately, but don't do this if the second operand
9701      is already a constant integer.  */
9702   if (swap_commutative_operands_p (op0, op1))
9703     {
9704       tem = op0, op0 = op1, op1 = tem;
9705       code = swap_condition (code);
9706     }
9707
9708   /* We now enter a loop during which we will try to simplify the comparison.
9709      For the most part, we only are concerned with comparisons with zero,
9710      but some things may really be comparisons with zero but not start
9711      out looking that way.  */
9712
9713   while (GET_CODE (op1) == CONST_INT)
9714     {
9715       enum machine_mode mode = GET_MODE (op0);
9716       unsigned int mode_width = GET_MODE_BITSIZE (mode);
9717       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9718       int equality_comparison_p;
9719       int sign_bit_comparison_p;
9720       int unsigned_comparison_p;
9721       HOST_WIDE_INT const_op;
9722
9723       /* We only want to handle integral modes.  This catches VOIDmode,
9724          CCmode, and the floating-point modes.  An exception is that we
9725          can handle VOIDmode if OP0 is a COMPARE or a comparison
9726          operation.  */
9727
9728       if (GET_MODE_CLASS (mode) != MODE_INT
9729           && ! (mode == VOIDmode
9730                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
9731         break;
9732
9733       /* Get the constant we are comparing against and turn off all bits
9734          not on in our mode.  */
9735       const_op = INTVAL (op1);
9736       if (mode != VOIDmode)
9737         const_op = trunc_int_for_mode (const_op, mode);
9738       op1 = GEN_INT (const_op);
9739
9740       /* If we are comparing against a constant power of two and the value
9741          being compared can only have that single bit nonzero (e.g., it was
9742          `and'ed with that bit), we can replace this with a comparison
9743          with zero.  */
9744       if (const_op
9745           && (code == EQ || code == NE || code == GE || code == GEU
9746               || code == LT || code == LTU)
9747           && mode_width <= HOST_BITS_PER_WIDE_INT
9748           && exact_log2 (const_op) >= 0
9749           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9750         {
9751           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9752           op1 = const0_rtx, const_op = 0;
9753         }
9754
9755       /* Similarly, if we are comparing a value known to be either -1 or
9756          0 with -1, change it to the opposite comparison against zero.  */
9757
9758       if (const_op == -1
9759           && (code == EQ || code == NE || code == GT || code == LE
9760               || code == GEU || code == LTU)
9761           && num_sign_bit_copies (op0, mode) == mode_width)
9762         {
9763           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9764           op1 = const0_rtx, const_op = 0;
9765         }
9766
9767       /* Do some canonicalizations based on the comparison code.  We prefer
9768          comparisons against zero and then prefer equality comparisons.
9769          If we can reduce the size of a constant, we will do that too.  */
9770
9771       switch (code)
9772         {
9773         case LT:
9774           /* < C is equivalent to <= (C - 1) */
9775           if (const_op > 0)
9776             {
9777               const_op -= 1;
9778               op1 = GEN_INT (const_op);
9779               code = LE;
9780               /* ... fall through to LE case below.  */
9781             }
9782           else
9783             break;
9784
9785         case LE:
9786           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
9787           if (const_op < 0)
9788             {
9789               const_op += 1;
9790               op1 = GEN_INT (const_op);
9791               code = LT;
9792             }
9793
9794           /* If we are doing a <= 0 comparison on a value known to have
9795              a zero sign bit, we can replace this with == 0.  */
9796           else if (const_op == 0
9797                    && mode_width <= HOST_BITS_PER_WIDE_INT
9798                    && (nonzero_bits (op0, mode)
9799                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9800             code = EQ;
9801           break;
9802
9803         case GE:
9804           /* >= C is equivalent to > (C - 1).  */
9805           if (const_op > 0)
9806             {
9807               const_op -= 1;
9808               op1 = GEN_INT (const_op);
9809               code = GT;
9810               /* ... fall through to GT below.  */
9811             }
9812           else
9813             break;
9814
9815         case GT:
9816           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
9817           if (const_op < 0)
9818             {
9819               const_op += 1;
9820               op1 = GEN_INT (const_op);
9821               code = GE;
9822             }
9823
9824           /* If we are doing a > 0 comparison on a value known to have
9825              a zero sign bit, we can replace this with != 0.  */
9826           else if (const_op == 0
9827                    && mode_width <= HOST_BITS_PER_WIDE_INT
9828                    && (nonzero_bits (op0, mode)
9829                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9830             code = NE;
9831           break;
9832
9833         case LTU:
9834           /* < C is equivalent to <= (C - 1).  */
9835           if (const_op > 0)
9836             {
9837               const_op -= 1;
9838               op1 = GEN_INT (const_op);
9839               code = LEU;
9840               /* ... fall through ...  */
9841             }
9842
9843           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
9844           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9845                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9846             {
9847               const_op = 0, op1 = const0_rtx;
9848               code = GE;
9849               break;
9850             }
9851           else
9852             break;
9853
9854         case LEU:
9855           /* unsigned <= 0 is equivalent to == 0 */
9856           if (const_op == 0)
9857             code = EQ;
9858
9859           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
9860           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9861                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9862             {
9863               const_op = 0, op1 = const0_rtx;
9864               code = GE;
9865             }
9866           break;
9867
9868         case GEU:
9869           /* >= C is equivalent to > (C - 1).  */
9870           if (const_op > 1)
9871             {
9872               const_op -= 1;
9873               op1 = GEN_INT (const_op);
9874               code = GTU;
9875               /* ... fall through ...  */
9876             }
9877
9878           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
9879           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9880                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9881             {
9882               const_op = 0, op1 = const0_rtx;
9883               code = LT;
9884               break;
9885             }
9886           else
9887             break;
9888
9889         case GTU:
9890           /* unsigned > 0 is equivalent to != 0 */
9891           if (const_op == 0)
9892             code = NE;
9893
9894           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
9895           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9896                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9897             {
9898               const_op = 0, op1 = const0_rtx;
9899               code = LT;
9900             }
9901           break;
9902
9903         default:
9904           break;
9905         }
9906
9907       /* Compute some predicates to simplify code below.  */
9908
9909       equality_comparison_p = (code == EQ || code == NE);
9910       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9911       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9912                                || code == GEU);
9913
9914       /* If this is a sign bit comparison and we can do arithmetic in
9915          MODE, say that we will only be needing the sign bit of OP0.  */
9916       if (sign_bit_comparison_p
9917           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9918         op0 = force_to_mode (op0, mode,
9919                              ((HOST_WIDE_INT) 1
9920                               << (GET_MODE_BITSIZE (mode) - 1)),
9921                              NULL_RTX, 0);
9922
9923       /* Now try cases based on the opcode of OP0.  If none of the cases
9924          does a "continue", we exit this loop immediately after the
9925          switch.  */
9926
9927       switch (GET_CODE (op0))
9928         {
9929         case ZERO_EXTRACT:
9930           /* If we are extracting a single bit from a variable position in
9931              a constant that has only a single bit set and are comparing it
9932              with zero, we can convert this into an equality comparison
9933              between the position and the location of the single bit.  */
9934           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
9935              have already reduced the shift count modulo the word size.  */
9936           if (!SHIFT_COUNT_TRUNCATED
9937               && GET_CODE (XEXP (op0, 0)) == CONST_INT
9938               && XEXP (op0, 1) == const1_rtx
9939               && equality_comparison_p && const_op == 0
9940               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9941             {
9942               if (BITS_BIG_ENDIAN)
9943                 {
9944                   enum machine_mode new_mode
9945                     = mode_for_extraction (EP_extzv, 1);
9946                   if (new_mode == MAX_MACHINE_MODE)
9947                     i = BITS_PER_WORD - 1 - i;
9948                   else
9949                     {
9950                       mode = new_mode;
9951                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
9952                     }
9953                 }
9954
9955               op0 = XEXP (op0, 2);
9956               op1 = GEN_INT (i);
9957               const_op = i;
9958
9959               /* Result is nonzero iff shift count is equal to I.  */
9960               code = reverse_condition (code);
9961               continue;
9962             }
9963
9964           /* ... fall through ...  */
9965
9966         case SIGN_EXTRACT:
9967           tem = expand_compound_operation (op0);
9968           if (tem != op0)
9969             {
9970               op0 = tem;
9971               continue;
9972             }
9973           break;
9974
9975         case NOT:
9976           /* If testing for equality, we can take the NOT of the constant.  */
9977           if (equality_comparison_p
9978               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9979             {
9980               op0 = XEXP (op0, 0);
9981               op1 = tem;
9982               continue;
9983             }
9984
9985           /* If just looking at the sign bit, reverse the sense of the
9986              comparison.  */
9987           if (sign_bit_comparison_p)
9988             {
9989               op0 = XEXP (op0, 0);
9990               code = (code == GE ? LT : GE);
9991               continue;
9992             }
9993           break;
9994
9995         case NEG:
9996           /* If testing for equality, we can take the NEG of the constant.  */
9997           if (equality_comparison_p
9998               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9999             {
10000               op0 = XEXP (op0, 0);
10001               op1 = tem;
10002               continue;
10003             }
10004
10005           /* The remaining cases only apply to comparisons with zero.  */
10006           if (const_op != 0)
10007             break;
10008
10009           /* When X is ABS or is known positive,
10010              (neg X) is < 0 if and only if X != 0.  */
10011
10012           if (sign_bit_comparison_p
10013               && (GET_CODE (XEXP (op0, 0)) == ABS
10014                   || (mode_width <= HOST_BITS_PER_WIDE_INT
10015                       && (nonzero_bits (XEXP (op0, 0), mode)
10016                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10017             {
10018               op0 = XEXP (op0, 0);
10019               code = (code == LT ? NE : EQ);
10020               continue;
10021             }
10022
10023           /* If we have NEG of something whose two high-order bits are the
10024              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
10025           if (num_sign_bit_copies (op0, mode) >= 2)
10026             {
10027               op0 = XEXP (op0, 0);
10028               code = swap_condition (code);
10029               continue;
10030             }
10031           break;
10032
10033         case ROTATE:
10034           /* If we are testing equality and our count is a constant, we
10035              can perform the inverse operation on our RHS.  */
10036           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10037               && (tem = simplify_binary_operation (ROTATERT, mode,
10038                                                    op1, XEXP (op0, 1))) != 0)
10039             {
10040               op0 = XEXP (op0, 0);
10041               op1 = tem;
10042               continue;
10043             }
10044
10045           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10046              a particular bit.  Convert it to an AND of a constant of that
10047              bit.  This will be converted into a ZERO_EXTRACT.  */
10048           if (const_op == 0 && sign_bit_comparison_p
10049               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10050               && mode_width <= HOST_BITS_PER_WIDE_INT)
10051             {
10052               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10053                                             ((HOST_WIDE_INT) 1
10054                                              << (mode_width - 1
10055                                                  - INTVAL (XEXP (op0, 1)))));
10056               code = (code == LT ? NE : EQ);
10057               continue;
10058             }
10059
10060           /* Fall through.  */
10061
10062         case ABS:
10063           /* ABS is ignorable inside an equality comparison with zero.  */
10064           if (const_op == 0 && equality_comparison_p)
10065             {
10066               op0 = XEXP (op0, 0);
10067               continue;
10068             }
10069           break;
10070
10071         case SIGN_EXTEND:
10072           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10073              (compare FOO CONST) if CONST fits in FOO's mode and we
10074              are either testing inequality or have an unsigned
10075              comparison with ZERO_EXTEND or a signed comparison with
10076              SIGN_EXTEND.  But don't do it if we don't have a compare
10077              insn of the given mode, since we'd have to revert it
10078              later on, and then we wouldn't know whether to sign- or
10079              zero-extend.  */
10080           mode = GET_MODE (XEXP (op0, 0));
10081           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10082               && ! unsigned_comparison_p
10083               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10084               && ((unsigned HOST_WIDE_INT) const_op
10085                   < (((unsigned HOST_WIDE_INT) 1 
10086                       << (GET_MODE_BITSIZE (mode) - 1))))
10087               && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10088             {
10089               op0 = XEXP (op0, 0);
10090               continue;
10091             }
10092           break;
10093
10094         case SUBREG:
10095           /* Check for the case where we are comparing A - C1 with C2, that is
10096
10097                (subreg:MODE (plus (A) (-C1))) op (C2)
10098
10099              with C1 a constant, and try to lift the SUBREG, i.e. to do the
10100              comparison in the wider mode.  One of the following two conditions
10101              must be true in order for this to be valid:
10102
10103                1. The mode extension results in the same bit pattern being added
10104                   on both sides and the comparison is equality or unsigned.  As
10105                   C2 has been truncated to fit in MODE, the pattern can only be
10106                   all 0s or all 1s.
10107
10108                2. The mode extension results in the sign bit being copied on
10109                   each side.
10110
10111              The difficulty here is that we have predicates for A but not for
10112              (A - C1) so we need to check that C1 is within proper bounds so
10113              as to perturbate A as little as possible.  */
10114
10115           if (mode_width <= HOST_BITS_PER_WIDE_INT
10116               && subreg_lowpart_p (op0)
10117               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10118               && GET_CODE (SUBREG_REG (op0)) == PLUS
10119               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10120             {
10121               enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10122               rtx a = XEXP (SUBREG_REG (op0), 0);
10123               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10124
10125               if ((c1 > 0
10126                    && (unsigned HOST_WIDE_INT) c1
10127                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10128                    && (equality_comparison_p || unsigned_comparison_p)
10129                    /* (A - C1) zero-extends if it is positive and sign-extends
10130                       if it is negative, C2 both zero- and sign-extends.  */
10131                    && ((0 == (nonzero_bits (a, inner_mode)
10132                               & ~GET_MODE_MASK (mode))
10133                         && const_op >= 0)
10134                        /* (A - C1) sign-extends if it is positive and 1-extends
10135                           if it is negative, C2 both sign- and 1-extends.  */
10136                        || (num_sign_bit_copies (a, inner_mode)
10137                            > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10138                                              - mode_width)
10139                            && const_op < 0)))
10140                   || ((unsigned HOST_WIDE_INT) c1
10141                        < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10142                       /* (A - C1) always sign-extends, like C2.  */
10143                       && num_sign_bit_copies (a, inner_mode)
10144                          > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10145                                            - mode_width - 1)))
10146                 {
10147                   op0 = SUBREG_REG (op0);
10148                   continue;
10149                 }
10150             }
10151
10152           /* If the inner mode is narrower and we are extracting the low part,
10153              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10154           if (subreg_lowpart_p (op0)
10155               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10156             /* Fall through */ ;
10157           else
10158             break;
10159
10160           /* ... fall through ...  */
10161
10162         case ZERO_EXTEND:
10163           mode = GET_MODE (XEXP (op0, 0));
10164           if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10165               && (unsigned_comparison_p || equality_comparison_p)
10166               && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10167               && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10168               && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10169             {
10170               op0 = XEXP (op0, 0);
10171               continue;
10172             }
10173           break;
10174
10175         case PLUS:
10176           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10177              this for equality comparisons due to pathological cases involving
10178              overflows.  */
10179           if (equality_comparison_p
10180               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10181                                                         op1, XEXP (op0, 1))))
10182             {
10183               op0 = XEXP (op0, 0);
10184               op1 = tem;
10185               continue;
10186             }
10187
10188           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10189           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10190               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10191             {
10192               op0 = XEXP (XEXP (op0, 0), 0);
10193               code = (code == LT ? EQ : NE);
10194               continue;
10195             }
10196           break;
10197
10198         case MINUS:
10199           /* We used to optimize signed comparisons against zero, but that
10200              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
10201              arrive here as equality comparisons, or (GEU, LTU) are
10202              optimized away.  No need to special-case them.  */
10203
10204           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10205              (eq B (minus A C)), whichever simplifies.  We can only do
10206              this for equality comparisons due to pathological cases involving
10207              overflows.  */
10208           if (equality_comparison_p
10209               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10210                                                         XEXP (op0, 1), op1)))
10211             {
10212               op0 = XEXP (op0, 0);
10213               op1 = tem;
10214               continue;
10215             }
10216
10217           if (equality_comparison_p
10218               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10219                                                         XEXP (op0, 0), op1)))
10220             {
10221               op0 = XEXP (op0, 1);
10222               op1 = tem;
10223               continue;
10224             }
10225
10226           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10227              of bits in X minus 1, is one iff X > 0.  */
10228           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10229               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10230               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10231                  == mode_width - 1
10232               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10233             {
10234               op0 = XEXP (op0, 1);
10235               code = (code == GE ? LE : GT);
10236               continue;
10237             }
10238           break;
10239
10240         case XOR:
10241           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10242              if C is zero or B is a constant.  */
10243           if (equality_comparison_p
10244               && 0 != (tem = simplify_binary_operation (XOR, mode,
10245                                                         XEXP (op0, 1), op1)))
10246             {
10247               op0 = XEXP (op0, 0);
10248               op1 = tem;
10249               continue;
10250             }
10251           break;
10252
10253         case EQ:  case NE:
10254         case UNEQ:  case LTGT:
10255         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
10256         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
10257         case UNORDERED: case ORDERED:
10258           /* We can't do anything if OP0 is a condition code value, rather
10259              than an actual data value.  */
10260           if (const_op != 0
10261               || CC0_P (XEXP (op0, 0))
10262               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10263             break;
10264
10265           /* Get the two operands being compared.  */
10266           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10267             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10268           else
10269             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10270
10271           /* Check for the cases where we simply want the result of the
10272              earlier test or the opposite of that result.  */
10273           if (code == NE || code == EQ
10274               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10275                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10276                   && (STORE_FLAG_VALUE
10277                       & (((HOST_WIDE_INT) 1
10278                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10279                   && (code == LT || code == GE)))
10280             {
10281               enum rtx_code new_code;
10282               if (code == LT || code == NE)
10283                 new_code = GET_CODE (op0);
10284               else
10285                 new_code = reversed_comparison_code (op0, NULL);
10286
10287               if (new_code != UNKNOWN)
10288                 {
10289                   code = new_code;
10290                   op0 = tem;
10291                   op1 = tem1;
10292                   continue;
10293                 }
10294             }
10295           break;
10296
10297         case IOR:
10298           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10299              iff X <= 0.  */
10300           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10301               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10302               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10303             {
10304               op0 = XEXP (op0, 1);
10305               code = (code == GE ? GT : LE);
10306               continue;
10307             }
10308           break;
10309
10310         case AND:
10311           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10312              will be converted to a ZERO_EXTRACT later.  */
10313           if (const_op == 0 && equality_comparison_p
10314               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10315               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10316             {
10317               op0 = simplify_and_const_int
10318                 (op0, mode, gen_rtx_LSHIFTRT (mode,
10319                                               XEXP (op0, 1),
10320                                               XEXP (XEXP (op0, 0), 1)),
10321                  (HOST_WIDE_INT) 1);
10322               continue;
10323             }
10324
10325           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10326              zero and X is a comparison and C1 and C2 describe only bits set
10327              in STORE_FLAG_VALUE, we can compare with X.  */
10328           if (const_op == 0 && equality_comparison_p
10329               && mode_width <= HOST_BITS_PER_WIDE_INT
10330               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10331               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10332               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10333               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10334               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10335             {
10336               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10337                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10338               if ((~STORE_FLAG_VALUE & mask) == 0
10339                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10340                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10341                           && COMPARISON_P (tem))))
10342                 {
10343                   op0 = XEXP (XEXP (op0, 0), 0);
10344                   continue;
10345                 }
10346             }
10347
10348           /* If we are doing an equality comparison of an AND of a bit equal
10349              to the sign bit, replace this with a LT or GE comparison of
10350              the underlying value.  */
10351           if (equality_comparison_p
10352               && const_op == 0
10353               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10354               && mode_width <= HOST_BITS_PER_WIDE_INT
10355               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10356                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10357             {
10358               op0 = XEXP (op0, 0);
10359               code = (code == EQ ? GE : LT);
10360               continue;
10361             }
10362
10363           /* If this AND operation is really a ZERO_EXTEND from a narrower
10364              mode, the constant fits within that mode, and this is either an
10365              equality or unsigned comparison, try to do this comparison in
10366              the narrower mode.  */
10367           if ((equality_comparison_p || unsigned_comparison_p)
10368               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10369               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10370                                    & GET_MODE_MASK (mode))
10371                                   + 1)) >= 0
10372               && const_op >> i == 0
10373               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10374             {
10375               op0 = gen_lowpart (tmode, XEXP (op0, 0));
10376               continue;
10377             }
10378
10379           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10380              fits in both M1 and M2 and the SUBREG is either paradoxical
10381              or represents the low part, permute the SUBREG and the AND
10382              and try again.  */
10383           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10384             {
10385               unsigned HOST_WIDE_INT c1;
10386               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10387               /* Require an integral mode, to avoid creating something like
10388                  (AND:SF ...).  */
10389               if (SCALAR_INT_MODE_P (tmode)
10390                   /* It is unsafe to commute the AND into the SUBREG if the
10391                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10392                      not defined.  As originally written the upper bits
10393                      have a defined value due to the AND operation.
10394                      However, if we commute the AND inside the SUBREG then
10395                      they no longer have defined values and the meaning of
10396                      the code has been changed.  */
10397                   && (0
10398 #ifdef WORD_REGISTER_OPERATIONS
10399                       || (mode_width > GET_MODE_BITSIZE (tmode)
10400                           && mode_width <= BITS_PER_WORD)
10401 #endif
10402                       || (mode_width <= GET_MODE_BITSIZE (tmode)
10403                           && subreg_lowpart_p (XEXP (op0, 0))))
10404                   && GET_CODE (XEXP (op0, 1)) == CONST_INT
10405                   && mode_width <= HOST_BITS_PER_WIDE_INT
10406                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10407                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10408                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
10409                   && c1 != mask
10410                   && c1 != GET_MODE_MASK (tmode))
10411                 {
10412                   op0 = simplify_gen_binary (AND, tmode,
10413                                              SUBREG_REG (XEXP (op0, 0)),
10414                                              gen_int_mode (c1, tmode));
10415                   op0 = gen_lowpart (mode, op0);
10416                   continue;
10417                 }
10418             }
10419
10420           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
10421           if (const_op == 0 && equality_comparison_p
10422               && XEXP (op0, 1) == const1_rtx
10423               && GET_CODE (XEXP (op0, 0)) == NOT)
10424             {
10425               op0 = simplify_and_const_int
10426                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10427               code = (code == NE ? EQ : NE);
10428               continue;
10429             }
10430
10431           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10432              (eq (and (lshiftrt X) 1) 0).
10433              Also handle the case where (not X) is expressed using xor.  */
10434           if (const_op == 0 && equality_comparison_p
10435               && XEXP (op0, 1) == const1_rtx
10436               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10437             {
10438               rtx shift_op = XEXP (XEXP (op0, 0), 0);
10439               rtx shift_count = XEXP (XEXP (op0, 0), 1);
10440
10441               if (GET_CODE (shift_op) == NOT
10442                   || (GET_CODE (shift_op) == XOR
10443                       && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10444                       && GET_CODE (shift_count) == CONST_INT
10445                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10446                       && (INTVAL (XEXP (shift_op, 1))
10447                           == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10448                 {
10449                   op0 = simplify_and_const_int
10450                     (NULL_RTX, mode,
10451                      gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10452                      (HOST_WIDE_INT) 1);
10453                   code = (code == NE ? EQ : NE);
10454                   continue;
10455                 }
10456             }
10457           break;
10458
10459         case ASHIFT:
10460           /* If we have (compare (ashift FOO N) (const_int C)) and
10461              the high order N bits of FOO (N+1 if an inequality comparison)
10462              are known to be zero, we can do this by comparing FOO with C
10463              shifted right N bits so long as the low-order N bits of C are
10464              zero.  */
10465           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10466               && INTVAL (XEXP (op0, 1)) >= 0
10467               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10468                   < HOST_BITS_PER_WIDE_INT)
10469               && ((const_op
10470                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10471               && mode_width <= HOST_BITS_PER_WIDE_INT
10472               && (nonzero_bits (XEXP (op0, 0), mode)
10473                   & ~(mask >> (INTVAL (XEXP (op0, 1))
10474                                + ! equality_comparison_p))) == 0)
10475             {
10476               /* We must perform a logical shift, not an arithmetic one,
10477                  as we want the top N bits of C to be zero.  */
10478               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10479
10480               temp >>= INTVAL (XEXP (op0, 1));
10481               op1 = gen_int_mode (temp, mode);
10482               op0 = XEXP (op0, 0);
10483               continue;
10484             }
10485
10486           /* If we are doing a sign bit comparison, it means we are testing
10487              a particular bit.  Convert it to the appropriate AND.  */
10488           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10489               && mode_width <= HOST_BITS_PER_WIDE_INT)
10490             {
10491               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10492                                             ((HOST_WIDE_INT) 1
10493                                              << (mode_width - 1
10494                                                  - INTVAL (XEXP (op0, 1)))));
10495               code = (code == LT ? NE : EQ);
10496               continue;
10497             }
10498
10499           /* If this an equality comparison with zero and we are shifting
10500              the low bit to the sign bit, we can convert this to an AND of the
10501              low-order bit.  */
10502           if (const_op == 0 && equality_comparison_p
10503               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10504               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10505                  == mode_width - 1)
10506             {
10507               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10508                                             (HOST_WIDE_INT) 1);
10509               continue;
10510             }
10511           break;
10512
10513         case ASHIFTRT:
10514           /* If this is an equality comparison with zero, we can do this
10515              as a logical shift, which might be much simpler.  */
10516           if (equality_comparison_p && const_op == 0
10517               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10518             {
10519               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10520                                           XEXP (op0, 0),
10521                                           INTVAL (XEXP (op0, 1)));
10522               continue;
10523             }
10524
10525           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10526              do the comparison in a narrower mode.  */
10527           if (! unsigned_comparison_p
10528               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10529               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10530               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10531               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10532                                          MODE_INT, 1)) != BLKmode
10533               && (((unsigned HOST_WIDE_INT) const_op
10534                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10535                   <= GET_MODE_MASK (tmode)))
10536             {
10537               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10538               continue;
10539             }
10540
10541           /* Likewise if OP0 is a PLUS of a sign extension with a
10542              constant, which is usually represented with the PLUS
10543              between the shifts.  */
10544           if (! unsigned_comparison_p
10545               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10546               && GET_CODE (XEXP (op0, 0)) == PLUS
10547               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10548               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10549               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10550               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10551                                          MODE_INT, 1)) != BLKmode
10552               && (((unsigned HOST_WIDE_INT) const_op
10553                    + (GET_MODE_MASK (tmode) >> 1) + 1)
10554                   <= GET_MODE_MASK (tmode)))
10555             {
10556               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10557               rtx add_const = XEXP (XEXP (op0, 0), 1);
10558               rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10559                                                    add_const, XEXP (op0, 1));
10560
10561               op0 = simplify_gen_binary (PLUS, tmode,
10562                                          gen_lowpart (tmode, inner),
10563                                          new_const);
10564               continue;
10565             }
10566
10567           /* ... fall through ...  */
10568         case LSHIFTRT:
10569           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10570              the low order N bits of FOO are known to be zero, we can do this
10571              by comparing FOO with C shifted left N bits so long as no
10572              overflow occurs.  */
10573           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10574               && INTVAL (XEXP (op0, 1)) >= 0
10575               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10576               && mode_width <= HOST_BITS_PER_WIDE_INT
10577               && (nonzero_bits (XEXP (op0, 0), mode)
10578                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10579               && (((unsigned HOST_WIDE_INT) const_op
10580                    + (GET_CODE (op0) != LSHIFTRT
10581                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10582                          + 1)
10583                       : 0))
10584                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10585             {
10586               /* If the shift was logical, then we must make the condition
10587                  unsigned.  */
10588               if (GET_CODE (op0) == LSHIFTRT)
10589                 code = unsigned_condition (code);
10590
10591               const_op <<= INTVAL (XEXP (op0, 1));
10592               op1 = GEN_INT (const_op);
10593               op0 = XEXP (op0, 0);
10594               continue;
10595             }
10596
10597           /* If we are using this shift to extract just the sign bit, we
10598              can replace this with an LT or GE comparison.  */
10599           if (const_op == 0
10600               && (equality_comparison_p || sign_bit_comparison_p)
10601               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10602               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10603                  == mode_width - 1)
10604             {
10605               op0 = XEXP (op0, 0);
10606               code = (code == NE || code == GT ? LT : GE);
10607               continue;
10608             }
10609           break;
10610
10611         default:
10612           break;
10613         }
10614
10615       break;
10616     }
10617
10618   /* Now make any compound operations involved in this comparison.  Then,
10619      check for an outmost SUBREG on OP0 that is not doing anything or is
10620      paradoxical.  The latter transformation must only be performed when
10621      it is known that the "extra" bits will be the same in op0 and op1 or
10622      that they don't matter.  There are three cases to consider:
10623
10624      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
10625      care bits and we can assume they have any convenient value.  So
10626      making the transformation is safe.
10627
10628      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10629      In this case the upper bits of op0 are undefined.  We should not make
10630      the simplification in that case as we do not know the contents of
10631      those bits.
10632
10633      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10634      UNKNOWN.  In that case we know those bits are zeros or ones.  We must
10635      also be sure that they are the same as the upper bits of op1.
10636
10637      We can never remove a SUBREG for a non-equality comparison because
10638      the sign bit is in a different place in the underlying object.  */
10639
10640   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10641   op1 = make_compound_operation (op1, SET);
10642
10643   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10644       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10645       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
10646       && (code == NE || code == EQ))
10647     {
10648       if (GET_MODE_SIZE (GET_MODE (op0))
10649           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
10650         {
10651           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
10652              implemented.  */
10653           if (REG_P (SUBREG_REG (op0)))
10654             {
10655               op0 = SUBREG_REG (op0);
10656               op1 = gen_lowpart (GET_MODE (op0), op1);
10657             }
10658         }
10659       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10660                 <= HOST_BITS_PER_WIDE_INT)
10661                && (nonzero_bits (SUBREG_REG (op0),
10662                                  GET_MODE (SUBREG_REG (op0)))
10663                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10664         {
10665           tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
10666
10667           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10668                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10669             op0 = SUBREG_REG (op0), op1 = tem;
10670         }
10671     }
10672
10673   /* We now do the opposite procedure: Some machines don't have compare
10674      insns in all modes.  If OP0's mode is an integer mode smaller than a
10675      word and we can't do a compare in that mode, see if there is a larger
10676      mode for which we can do the compare.  There are a number of cases in
10677      which we can use the wider mode.  */
10678
10679   mode = GET_MODE (op0);
10680   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10681       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10682       && ! have_insn_for (COMPARE, mode))
10683     for (tmode = GET_MODE_WIDER_MODE (mode);
10684          (tmode != VOIDmode
10685           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10686          tmode = GET_MODE_WIDER_MODE (tmode))
10687       if (have_insn_for (COMPARE, tmode))
10688         {
10689           int zero_extended;
10690
10691           /* If the only nonzero bits in OP0 and OP1 are those in the
10692              narrower mode and this is an equality or unsigned comparison,
10693              we can use the wider mode.  Similarly for sign-extended
10694              values, in which case it is true for all comparisons.  */
10695           zero_extended = ((code == EQ || code == NE
10696                             || code == GEU || code == GTU
10697                             || code == LEU || code == LTU)
10698                            && (nonzero_bits (op0, tmode)
10699                                & ~GET_MODE_MASK (mode)) == 0
10700                            && ((GET_CODE (op1) == CONST_INT
10701                                 || (nonzero_bits (op1, tmode)
10702                                     & ~GET_MODE_MASK (mode)) == 0)));
10703
10704           if (zero_extended
10705               || ((num_sign_bit_copies (op0, tmode)
10706                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
10707                                      - GET_MODE_BITSIZE (mode)))
10708                   && (num_sign_bit_copies (op1, tmode)
10709                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
10710                                         - GET_MODE_BITSIZE (mode)))))
10711             {
10712               /* If OP0 is an AND and we don't have an AND in MODE either,
10713                  make a new AND in the proper mode.  */
10714               if (GET_CODE (op0) == AND
10715                   && !have_insn_for (AND, mode))
10716                 op0 = simplify_gen_binary (AND, tmode,
10717                                            gen_lowpart (tmode,
10718                                                         XEXP (op0, 0)),
10719                                            gen_lowpart (tmode,
10720                                                         XEXP (op0, 1)));
10721
10722               op0 = gen_lowpart (tmode, op0);
10723               if (zero_extended && GET_CODE (op1) == CONST_INT)
10724                 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
10725               op1 = gen_lowpart (tmode, op1);
10726               break;
10727             }
10728
10729           /* If this is a test for negative, we can make an explicit
10730              test of the sign bit.  */
10731
10732           if (op1 == const0_rtx && (code == LT || code == GE)
10733               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10734             {
10735               op0 = simplify_gen_binary (AND, tmode,
10736                                          gen_lowpart (tmode, op0),
10737                                          GEN_INT ((HOST_WIDE_INT) 1
10738                                                   << (GET_MODE_BITSIZE (mode)
10739                                                       - 1)));
10740               code = (code == LT) ? NE : EQ;
10741               break;
10742             }
10743         }
10744
10745 #ifdef CANONICALIZE_COMPARISON
10746   /* If this machine only supports a subset of valid comparisons, see if we
10747      can convert an unsupported one into a supported one.  */
10748   CANONICALIZE_COMPARISON (code, op0, op1);
10749 #endif
10750
10751   *pop0 = op0;
10752   *pop1 = op1;
10753
10754   return code;
10755 }
10756 \f
10757 /* Utility function for record_value_for_reg.  Count number of
10758    rtxs in X.  */
10759 static int
10760 count_rtxs (rtx x)
10761 {
10762   enum rtx_code code = GET_CODE (x);
10763   const char *fmt;
10764   int i, ret = 1;
10765
10766   if (GET_RTX_CLASS (code) == '2'
10767       || GET_RTX_CLASS (code) == 'c')
10768     {
10769       rtx x0 = XEXP (x, 0);
10770       rtx x1 = XEXP (x, 1);
10771
10772       if (x0 == x1)
10773         return 1 + 2 * count_rtxs (x0);
10774
10775       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
10776            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
10777           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10778         return 2 + 2 * count_rtxs (x0)
10779                + count_rtxs (x == XEXP (x1, 0)
10780                              ? XEXP (x1, 1) : XEXP (x1, 0));
10781
10782       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
10783            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
10784           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10785         return 2 + 2 * count_rtxs (x1)
10786                + count_rtxs (x == XEXP (x0, 0)
10787                              ? XEXP (x0, 1) : XEXP (x0, 0));
10788     }
10789
10790   fmt = GET_RTX_FORMAT (code);
10791   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10792     if (fmt[i] == 'e')
10793       ret += count_rtxs (XEXP (x, i));
10794
10795   return ret;
10796 }
10797 \f
10798 /* Utility function for following routine.  Called when X is part of a value
10799    being stored into last_set_value.  Sets last_set_table_tick
10800    for each register mentioned.  Similar to mention_regs in cse.c  */
10801
10802 static void
10803 update_table_tick (rtx x)
10804 {
10805   enum rtx_code code = GET_CODE (x);
10806   const char *fmt = GET_RTX_FORMAT (code);
10807   int i;
10808
10809   if (code == REG)
10810     {
10811       unsigned int regno = REGNO (x);
10812       unsigned int endregno
10813         = regno + (regno < FIRST_PSEUDO_REGISTER
10814                    ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
10815       unsigned int r;
10816
10817       for (r = regno; r < endregno; r++)
10818         reg_stat[r].last_set_table_tick = label_tick;
10819
10820       return;
10821     }
10822
10823   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10824     /* Note that we can't have an "E" in values stored; see
10825        get_last_value_validate.  */
10826     if (fmt[i] == 'e')
10827       {
10828         /* Check for identical subexpressions.  If x contains
10829            identical subexpression we only have to traverse one of
10830            them.  */
10831         if (i == 0 && ARITHMETIC_P (x))
10832           {
10833             /* Note that at this point x1 has already been
10834                processed.  */
10835             rtx x0 = XEXP (x, 0);
10836             rtx x1 = XEXP (x, 1);
10837
10838             /* If x0 and x1 are identical then there is no need to
10839                process x0.  */
10840             if (x0 == x1)
10841               break;
10842
10843             /* If x0 is identical to a subexpression of x1 then while
10844                processing x1, x0 has already been processed.  Thus we
10845                are done with x.  */
10846             if (ARITHMETIC_P (x1)
10847                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10848               break;
10849
10850             /* If x1 is identical to a subexpression of x0 then we
10851                still have to process the rest of x0.  */
10852             if (ARITHMETIC_P (x0)
10853                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10854               {
10855                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
10856                 break;
10857               }
10858           }
10859
10860         update_table_tick (XEXP (x, i));
10861       }
10862 }
10863
10864 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
10865    are saying that the register is clobbered and we no longer know its
10866    value.  If INSN is zero, don't update reg_stat[].last_set; this is
10867    only permitted with VALUE also zero and is used to invalidate the
10868    register.  */
10869
10870 static void
10871 record_value_for_reg (rtx reg, rtx insn, rtx value)
10872 {
10873   unsigned int regno = REGNO (reg);
10874   unsigned int endregno
10875     = regno + (regno < FIRST_PSEUDO_REGISTER
10876                ? hard_regno_nregs[regno][GET_MODE (reg)] : 1);
10877   unsigned int i;
10878
10879   /* If VALUE contains REG and we have a previous value for REG, substitute
10880      the previous value.  */
10881   if (value && insn && reg_overlap_mentioned_p (reg, value))
10882     {
10883       rtx tem;
10884
10885       /* Set things up so get_last_value is allowed to see anything set up to
10886          our insn.  */
10887       subst_low_cuid = INSN_CUID (insn);
10888       tem = get_last_value (reg);
10889
10890       /* If TEM is simply a binary operation with two CLOBBERs as operands,
10891          it isn't going to be useful and will take a lot of time to process,
10892          so just use the CLOBBER.  */
10893
10894       if (tem)
10895         {
10896           if (ARITHMETIC_P (tem)
10897               && GET_CODE (XEXP (tem, 0)) == CLOBBER
10898               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
10899             tem = XEXP (tem, 0);
10900           else if (count_occurrences (value, reg, 1) >= 2)
10901             {
10902               /* If there are two or more occurrences of REG in VALUE,
10903                  prevent the value from growing too much.  */
10904               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
10905                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
10906             }
10907
10908           value = replace_rtx (copy_rtx (value), reg, tem);
10909         }
10910     }
10911
10912   /* For each register modified, show we don't know its value, that
10913      we don't know about its bitwise content, that its value has been
10914      updated, and that we don't know the location of the death of the
10915      register.  */
10916   for (i = regno; i < endregno; i++)
10917     {
10918       if (insn)
10919         reg_stat[i].last_set = insn;
10920
10921       reg_stat[i].last_set_value = 0;
10922       reg_stat[i].last_set_mode = 0;
10923       reg_stat[i].last_set_nonzero_bits = 0;
10924       reg_stat[i].last_set_sign_bit_copies = 0;
10925       reg_stat[i].last_death = 0;
10926     }
10927
10928   /* Mark registers that are being referenced in this value.  */
10929   if (value)
10930     update_table_tick (value);
10931
10932   /* Now update the status of each register being set.
10933      If someone is using this register in this block, set this register
10934      to invalid since we will get confused between the two lives in this
10935      basic block.  This makes using this register always invalid.  In cse, we
10936      scan the table to invalidate all entries using this register, but this
10937      is too much work for us.  */
10938
10939   for (i = regno; i < endregno; i++)
10940     {
10941       reg_stat[i].last_set_label = label_tick;
10942       if (value && reg_stat[i].last_set_table_tick == label_tick)
10943         reg_stat[i].last_set_invalid = 1;
10944       else
10945         reg_stat[i].last_set_invalid = 0;
10946     }
10947
10948   /* The value being assigned might refer to X (like in "x++;").  In that
10949      case, we must replace it with (clobber (const_int 0)) to prevent
10950      infinite loops.  */
10951   if (value && ! get_last_value_validate (&value, insn,
10952                                           reg_stat[regno].last_set_label, 0))
10953     {
10954       value = copy_rtx (value);
10955       if (! get_last_value_validate (&value, insn,
10956                                      reg_stat[regno].last_set_label, 1))
10957         value = 0;
10958     }
10959
10960   /* For the main register being modified, update the value, the mode, the
10961      nonzero bits, and the number of sign bit copies.  */
10962
10963   reg_stat[regno].last_set_value = value;
10964
10965   if (value)
10966     {
10967       enum machine_mode mode = GET_MODE (reg);
10968       subst_low_cuid = INSN_CUID (insn);
10969       reg_stat[regno].last_set_mode = mode;
10970       if (GET_MODE_CLASS (mode) == MODE_INT
10971           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10972         mode = nonzero_bits_mode;
10973       reg_stat[regno].last_set_nonzero_bits = nonzero_bits (value, mode);
10974       reg_stat[regno].last_set_sign_bit_copies
10975         = num_sign_bit_copies (value, GET_MODE (reg));
10976     }
10977 }
10978
10979 /* Called via note_stores from record_dead_and_set_regs to handle one
10980    SET or CLOBBER in an insn.  DATA is the instruction in which the
10981    set is occurring.  */
10982
10983 static void
10984 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
10985 {
10986   rtx record_dead_insn = (rtx) data;
10987
10988   if (GET_CODE (dest) == SUBREG)
10989     dest = SUBREG_REG (dest);
10990
10991   if (REG_P (dest))
10992     {
10993       /* If we are setting the whole register, we know its value.  Otherwise
10994          show that we don't know the value.  We can handle SUBREG in
10995          some cases.  */
10996       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10997         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10998       else if (GET_CODE (setter) == SET
10999                && GET_CODE (SET_DEST (setter)) == SUBREG
11000                && SUBREG_REG (SET_DEST (setter)) == dest
11001                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11002                && subreg_lowpart_p (SET_DEST (setter)))
11003         record_value_for_reg (dest, record_dead_insn,
11004                               gen_lowpart (GET_MODE (dest),
11005                                                        SET_SRC (setter)));
11006       else
11007         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11008     }
11009   else if (MEM_P (dest)
11010            /* Ignore pushes, they clobber nothing.  */
11011            && ! push_operand (dest, GET_MODE (dest)))
11012     mem_last_set = INSN_CUID (record_dead_insn);
11013 }
11014
11015 /* Update the records of when each REG was most recently set or killed
11016    for the things done by INSN.  This is the last thing done in processing
11017    INSN in the combiner loop.
11018
11019    We update reg_stat[], in particular fields last_set, last_set_value,
11020    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11021    last_death, and also the similar information mem_last_set (which insn
11022    most recently modified memory) and last_call_cuid (which insn was the
11023    most recent subroutine call).  */
11024
11025 static void
11026 record_dead_and_set_regs (rtx insn)
11027 {
11028   rtx link;
11029   unsigned int i;
11030
11031   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11032     {
11033       if (REG_NOTE_KIND (link) == REG_DEAD
11034           && REG_P (XEXP (link, 0)))
11035         {
11036           unsigned int regno = REGNO (XEXP (link, 0));
11037           unsigned int endregno
11038             = regno + (regno < FIRST_PSEUDO_REGISTER
11039                        ? hard_regno_nregs[regno][GET_MODE (XEXP (link, 0))]
11040                        : 1);
11041
11042           for (i = regno; i < endregno; i++)
11043             reg_stat[i].last_death = insn;
11044         }
11045       else if (REG_NOTE_KIND (link) == REG_INC)
11046         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11047     }
11048
11049   if (CALL_P (insn))
11050     {
11051       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11052         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11053           {
11054             reg_stat[i].last_set_value = 0;
11055             reg_stat[i].last_set_mode = 0;
11056             reg_stat[i].last_set_nonzero_bits = 0;
11057             reg_stat[i].last_set_sign_bit_copies = 0;
11058             reg_stat[i].last_death = 0;
11059           }
11060
11061       last_call_cuid = mem_last_set = INSN_CUID (insn);
11062
11063       /* Don't bother recording what this insn does.  It might set the
11064          return value register, but we can't combine into a call
11065          pattern anyway, so there's no point trying (and it may cause
11066          a crash, if e.g. we wind up asking for last_set_value of a
11067          SUBREG of the return value register).  */
11068       return;
11069     }
11070
11071   note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11072 }
11073
11074 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11075    register present in the SUBREG, so for each such SUBREG go back and
11076    adjust nonzero and sign bit information of the registers that are
11077    known to have some zero/sign bits set.
11078
11079    This is needed because when combine blows the SUBREGs away, the
11080    information on zero/sign bits is lost and further combines can be
11081    missed because of that.  */
11082
11083 static void
11084 record_promoted_value (rtx insn, rtx subreg)
11085 {
11086   rtx links, set;
11087   unsigned int regno = REGNO (SUBREG_REG (subreg));
11088   enum machine_mode mode = GET_MODE (subreg);
11089
11090   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11091     return;
11092
11093   for (links = LOG_LINKS (insn); links;)
11094     {
11095       insn = XEXP (links, 0);
11096       set = single_set (insn);
11097
11098       if (! set || !REG_P (SET_DEST (set))
11099           || REGNO (SET_DEST (set)) != regno
11100           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11101         {
11102           links = XEXP (links, 1);
11103           continue;
11104         }
11105
11106       if (reg_stat[regno].last_set == insn)
11107         {
11108           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11109             reg_stat[regno].last_set_nonzero_bits &= GET_MODE_MASK (mode);
11110         }
11111
11112       if (REG_P (SET_SRC (set)))
11113         {
11114           regno = REGNO (SET_SRC (set));
11115           links = LOG_LINKS (insn);
11116         }
11117       else
11118         break;
11119     }
11120 }
11121
11122 /* Scan X for promoted SUBREGs.  For each one found,
11123    note what it implies to the registers used in it.  */
11124
11125 static void
11126 check_promoted_subreg (rtx insn, rtx x)
11127 {
11128   if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11129       && REG_P (SUBREG_REG (x)))
11130     record_promoted_value (insn, x);
11131   else
11132     {
11133       const char *format = GET_RTX_FORMAT (GET_CODE (x));
11134       int i, j;
11135
11136       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11137         switch (format[i])
11138           {
11139           case 'e':
11140             check_promoted_subreg (insn, XEXP (x, i));
11141             break;
11142           case 'V':
11143           case 'E':
11144             if (XVEC (x, i) != 0)
11145               for (j = 0; j < XVECLEN (x, i); j++)
11146                 check_promoted_subreg (insn, XVECEXP (x, i, j));
11147             break;
11148           }
11149     }
11150 }
11151 \f
11152 /* Utility routine for the following function.  Verify that all the registers
11153    mentioned in *LOC are valid when *LOC was part of a value set when
11154    label_tick == TICK.  Return 0 if some are not.
11155
11156    If REPLACE is nonzero, replace the invalid reference with
11157    (clobber (const_int 0)) and return 1.  This replacement is useful because
11158    we often can get useful information about the form of a value (e.g., if
11159    it was produced by a shift that always produces -1 or 0) even though
11160    we don't know exactly what registers it was produced from.  */
11161
11162 static int
11163 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11164 {
11165   rtx x = *loc;
11166   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11167   int len = GET_RTX_LENGTH (GET_CODE (x));
11168   int i;
11169
11170   if (REG_P (x))
11171     {
11172       unsigned int regno = REGNO (x);
11173       unsigned int endregno
11174         = regno + (regno < FIRST_PSEUDO_REGISTER
11175                    ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11176       unsigned int j;
11177
11178       for (j = regno; j < endregno; j++)
11179         if (reg_stat[j].last_set_invalid
11180             /* If this is a pseudo-register that was only set once and not
11181                live at the beginning of the function, it is always valid.  */
11182             || (! (regno >= FIRST_PSEUDO_REGISTER
11183                    && REG_N_SETS (regno) == 1
11184                    && (! REGNO_REG_SET_P
11185                        (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
11186                         regno)))
11187                 && reg_stat[j].last_set_label > tick))
11188           {
11189             if (replace)
11190               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11191             return replace;
11192           }
11193
11194       return 1;
11195     }
11196   /* If this is a memory reference, make sure that there were
11197      no stores after it that might have clobbered the value.  We don't
11198      have alias info, so we assume any store invalidates it.  */
11199   else if (MEM_P (x) && !MEM_READONLY_P (x)
11200            && INSN_CUID (insn) <= mem_last_set)
11201     {
11202       if (replace)
11203         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11204       return replace;
11205     }
11206
11207   for (i = 0; i < len; i++)
11208     {
11209       if (fmt[i] == 'e')
11210         {
11211           /* Check for identical subexpressions.  If x contains
11212              identical subexpression we only have to traverse one of
11213              them.  */
11214           if (i == 1 && ARITHMETIC_P (x))
11215             {
11216               /* Note that at this point x0 has already been checked
11217                  and found valid.  */
11218               rtx x0 = XEXP (x, 0);
11219               rtx x1 = XEXP (x, 1);
11220
11221               /* If x0 and x1 are identical then x is also valid.  */
11222               if (x0 == x1)
11223                 return 1;
11224
11225               /* If x1 is identical to a subexpression of x0 then
11226                  while checking x0, x1 has already been checked.  Thus
11227                  it is valid and so as x.  */
11228               if (ARITHMETIC_P (x0)
11229                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11230                 return 1;
11231
11232               /* If x0 is identical to a subexpression of x1 then x is
11233                  valid iff the rest of x1 is valid.  */
11234               if (ARITHMETIC_P (x1)
11235                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11236                 return
11237                   get_last_value_validate (&XEXP (x1,
11238                                                   x0 == XEXP (x1, 0) ? 1 : 0),
11239                                            insn, tick, replace);
11240             }
11241
11242           if (get_last_value_validate (&XEXP (x, i), insn, tick,
11243                                        replace) == 0)
11244             return 0;
11245         }
11246       /* Don't bother with these.  They shouldn't occur anyway.  */
11247       else if (fmt[i] == 'E')
11248         return 0;
11249     }
11250
11251   /* If we haven't found a reason for it to be invalid, it is valid.  */
11252   return 1;
11253 }
11254
11255 /* Get the last value assigned to X, if known.  Some registers
11256    in the value may be replaced with (clobber (const_int 0)) if their value
11257    is known longer known reliably.  */
11258
11259 static rtx
11260 get_last_value (rtx x)
11261 {
11262   unsigned int regno;
11263   rtx value;
11264
11265   /* If this is a non-paradoxical SUBREG, get the value of its operand and
11266      then convert it to the desired mode.  If this is a paradoxical SUBREG,
11267      we cannot predict what values the "extra" bits might have.  */
11268   if (GET_CODE (x) == SUBREG
11269       && subreg_lowpart_p (x)
11270       && (GET_MODE_SIZE (GET_MODE (x))
11271           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11272       && (value = get_last_value (SUBREG_REG (x))) != 0)
11273     return gen_lowpart (GET_MODE (x), value);
11274
11275   if (!REG_P (x))
11276     return 0;
11277
11278   regno = REGNO (x);
11279   value = reg_stat[regno].last_set_value;
11280
11281   /* If we don't have a value, or if it isn't for this basic block and
11282      it's either a hard register, set more than once, or it's a live
11283      at the beginning of the function, return 0.
11284
11285      Because if it's not live at the beginning of the function then the reg
11286      is always set before being used (is never used without being set).
11287      And, if it's set only once, and it's always set before use, then all
11288      uses must have the same last value, even if it's not from this basic
11289      block.  */
11290
11291   if (value == 0
11292       || (reg_stat[regno].last_set_label != label_tick
11293           && (regno < FIRST_PSEUDO_REGISTER
11294               || REG_N_SETS (regno) != 1
11295               || (REGNO_REG_SET_P
11296                   (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
11297                    regno)))))
11298     return 0;
11299
11300   /* If the value was set in a later insn than the ones we are processing,
11301      we can't use it even if the register was only set once.  */
11302   if (INSN_CUID (reg_stat[regno].last_set) >= subst_low_cuid)
11303     return 0;
11304
11305   /* If the value has all its registers valid, return it.  */
11306   if (get_last_value_validate (&value, reg_stat[regno].last_set,
11307                                reg_stat[regno].last_set_label, 0))
11308     return value;
11309
11310   /* Otherwise, make a copy and replace any invalid register with
11311      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
11312
11313   value = copy_rtx (value);
11314   if (get_last_value_validate (&value, reg_stat[regno].last_set,
11315                                reg_stat[regno].last_set_label, 1))
11316     return value;
11317
11318   return 0;
11319 }
11320 \f
11321 /* Return nonzero if expression X refers to a REG or to memory
11322    that is set in an instruction more recent than FROM_CUID.  */
11323
11324 static int
11325 use_crosses_set_p (rtx x, int from_cuid)
11326 {
11327   const char *fmt;
11328   int i;
11329   enum rtx_code code = GET_CODE (x);
11330
11331   if (code == REG)
11332     {
11333       unsigned int regno = REGNO (x);
11334       unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11335                                  ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11336
11337 #ifdef PUSH_ROUNDING
11338       /* Don't allow uses of the stack pointer to be moved,
11339          because we don't know whether the move crosses a push insn.  */
11340       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11341         return 1;
11342 #endif
11343       for (; regno < endreg; regno++)
11344         if (reg_stat[regno].last_set
11345             && INSN_CUID (reg_stat[regno].last_set) > from_cuid)
11346           return 1;
11347       return 0;
11348     }
11349
11350   if (code == MEM && mem_last_set > from_cuid)
11351     return 1;
11352
11353   fmt = GET_RTX_FORMAT (code);
11354
11355   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11356     {
11357       if (fmt[i] == 'E')
11358         {
11359           int j;
11360           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11361             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11362               return 1;
11363         }
11364       else if (fmt[i] == 'e'
11365                && use_crosses_set_p (XEXP (x, i), from_cuid))
11366         return 1;
11367     }
11368   return 0;
11369 }
11370 \f
11371 /* Define three variables used for communication between the following
11372    routines.  */
11373
11374 static unsigned int reg_dead_regno, reg_dead_endregno;
11375 static int reg_dead_flag;
11376
11377 /* Function called via note_stores from reg_dead_at_p.
11378
11379    If DEST is within [reg_dead_regno, reg_dead_endregno), set
11380    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
11381
11382 static void
11383 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11384 {
11385   unsigned int regno, endregno;
11386
11387   if (!REG_P (dest))
11388     return;
11389
11390   regno = REGNO (dest);
11391   endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11392                       ? hard_regno_nregs[regno][GET_MODE (dest)] : 1);
11393
11394   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11395     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11396 }
11397
11398 /* Return nonzero if REG is known to be dead at INSN.
11399
11400    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
11401    referencing REG, it is dead.  If we hit a SET referencing REG, it is
11402    live.  Otherwise, see if it is live or dead at the start of the basic
11403    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
11404    must be assumed to be always live.  */
11405
11406 static int
11407 reg_dead_at_p (rtx reg, rtx insn)
11408 {
11409   basic_block block;
11410   unsigned int i;
11411
11412   /* Set variables for reg_dead_at_p_1.  */
11413   reg_dead_regno = REGNO (reg);
11414   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11415                                         ? hard_regno_nregs[reg_dead_regno]
11416                                                           [GET_MODE (reg)]
11417                                         : 1);
11418
11419   reg_dead_flag = 0;
11420
11421   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
11422      we allow the machine description to decide whether use-and-clobber
11423      patterns are OK.  */
11424   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11425     {
11426       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11427         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11428           return 0;
11429     }
11430
11431   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11432      beginning of function.  */
11433   for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11434        insn = prev_nonnote_insn (insn))
11435     {
11436       note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11437       if (reg_dead_flag)
11438         return reg_dead_flag == 1 ? 1 : 0;
11439
11440       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11441         return 1;
11442     }
11443
11444   /* Get the basic block that we were in.  */
11445   if (insn == 0)
11446     block = ENTRY_BLOCK_PTR->next_bb;
11447   else
11448     {
11449       FOR_EACH_BB (block)
11450         if (insn == BB_HEAD (block))
11451           break;
11452
11453       if (block == EXIT_BLOCK_PTR)
11454         return 0;
11455     }
11456
11457   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11458     if (REGNO_REG_SET_P (block->il.rtl->global_live_at_start, i))
11459       return 0;
11460
11461   return 1;
11462 }
11463 \f
11464 /* Note hard registers in X that are used.  This code is similar to
11465    that in flow.c, but much simpler since we don't care about pseudos.  */
11466
11467 static void
11468 mark_used_regs_combine (rtx x)
11469 {
11470   RTX_CODE code = GET_CODE (x);
11471   unsigned int regno;
11472   int i;
11473
11474   switch (code)
11475     {
11476     case LABEL_REF:
11477     case SYMBOL_REF:
11478     case CONST_INT:
11479     case CONST:
11480     case CONST_DOUBLE:
11481     case CONST_VECTOR:
11482     case PC:
11483     case ADDR_VEC:
11484     case ADDR_DIFF_VEC:
11485     case ASM_INPUT:
11486 #ifdef HAVE_cc0
11487     /* CC0 must die in the insn after it is set, so we don't need to take
11488        special note of it here.  */
11489     case CC0:
11490 #endif
11491       return;
11492
11493     case CLOBBER:
11494       /* If we are clobbering a MEM, mark any hard registers inside the
11495          address as used.  */
11496       if (MEM_P (XEXP (x, 0)))
11497         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11498       return;
11499
11500     case REG:
11501       regno = REGNO (x);
11502       /* A hard reg in a wide mode may really be multiple registers.
11503          If so, mark all of them just like the first.  */
11504       if (regno < FIRST_PSEUDO_REGISTER)
11505         {
11506           unsigned int endregno, r;
11507
11508           /* None of this applies to the stack, frame or arg pointers.  */
11509           if (regno == STACK_POINTER_REGNUM
11510 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11511               || regno == HARD_FRAME_POINTER_REGNUM
11512 #endif
11513 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11514               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11515 #endif
11516               || regno == FRAME_POINTER_REGNUM)
11517             return;
11518
11519           endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11520           for (r = regno; r < endregno; r++)
11521             SET_HARD_REG_BIT (newpat_used_regs, r);
11522         }
11523       return;
11524
11525     case SET:
11526       {
11527         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11528            the address.  */
11529         rtx testreg = SET_DEST (x);
11530
11531         while (GET_CODE (testreg) == SUBREG
11532                || GET_CODE (testreg) == ZERO_EXTRACT
11533                || GET_CODE (testreg) == STRICT_LOW_PART)
11534           testreg = XEXP (testreg, 0);
11535
11536         if (MEM_P (testreg))
11537           mark_used_regs_combine (XEXP (testreg, 0));
11538
11539         mark_used_regs_combine (SET_SRC (x));
11540       }
11541       return;
11542
11543     default:
11544       break;
11545     }
11546
11547   /* Recursively scan the operands of this expression.  */
11548
11549   {
11550     const char *fmt = GET_RTX_FORMAT (code);
11551
11552     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11553       {
11554         if (fmt[i] == 'e')
11555           mark_used_regs_combine (XEXP (x, i));
11556         else if (fmt[i] == 'E')
11557           {
11558             int j;
11559
11560             for (j = 0; j < XVECLEN (x, i); j++)
11561               mark_used_regs_combine (XVECEXP (x, i, j));
11562           }
11563       }
11564   }
11565 }
11566 \f
11567 /* Remove register number REGNO from the dead registers list of INSN.
11568
11569    Return the note used to record the death, if there was one.  */
11570
11571 rtx
11572 remove_death (unsigned int regno, rtx insn)
11573 {
11574   rtx note = find_regno_note (insn, REG_DEAD, regno);
11575
11576   if (note)
11577     {
11578       REG_N_DEATHS (regno)--;
11579       remove_note (insn, note);
11580     }
11581
11582   return note;
11583 }
11584
11585 /* For each register (hardware or pseudo) used within expression X, if its
11586    death is in an instruction with cuid between FROM_CUID (inclusive) and
11587    TO_INSN (exclusive), put a REG_DEAD note for that register in the
11588    list headed by PNOTES.
11589
11590    That said, don't move registers killed by maybe_kill_insn.
11591
11592    This is done when X is being merged by combination into TO_INSN.  These
11593    notes will then be distributed as needed.  */
11594
11595 static void
11596 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
11597              rtx *pnotes)
11598 {
11599   const char *fmt;
11600   int len, i;
11601   enum rtx_code code = GET_CODE (x);
11602
11603   if (code == REG)
11604     {
11605       unsigned int regno = REGNO (x);
11606       rtx where_dead = reg_stat[regno].last_death;
11607       rtx before_dead, after_dead;
11608
11609       /* Don't move the register if it gets killed in between from and to.  */
11610       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11611           && ! reg_referenced_p (x, maybe_kill_insn))
11612         return;
11613
11614       /* WHERE_DEAD could be a USE insn made by combine, so first we
11615          make sure that we have insns with valid INSN_CUID values.  */
11616       before_dead = where_dead;
11617       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11618         before_dead = PREV_INSN (before_dead);
11619
11620       after_dead = where_dead;
11621       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11622         after_dead = NEXT_INSN (after_dead);
11623
11624       if (before_dead && after_dead
11625           && INSN_CUID (before_dead) >= from_cuid
11626           && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11627               || (where_dead != after_dead
11628                   && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11629         {
11630           rtx note = remove_death (regno, where_dead);
11631
11632           /* It is possible for the call above to return 0.  This can occur
11633              when last_death points to I2 or I1 that we combined with.
11634              In that case make a new note.
11635
11636              We must also check for the case where X is a hard register
11637              and NOTE is a death note for a range of hard registers
11638              including X.  In that case, we must put REG_DEAD notes for
11639              the remaining registers in place of NOTE.  */
11640
11641           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11642               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11643                   > GET_MODE_SIZE (GET_MODE (x))))
11644             {
11645               unsigned int deadregno = REGNO (XEXP (note, 0));
11646               unsigned int deadend
11647                 = (deadregno + hard_regno_nregs[deadregno]
11648                                                [GET_MODE (XEXP (note, 0))]);
11649               unsigned int ourend
11650                 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11651               unsigned int i;
11652
11653               for (i = deadregno; i < deadend; i++)
11654                 if (i < regno || i >= ourend)
11655                   REG_NOTES (where_dead)
11656                     = gen_rtx_EXPR_LIST (REG_DEAD,
11657                                          regno_reg_rtx[i],
11658                                          REG_NOTES (where_dead));
11659             }
11660
11661           /* If we didn't find any note, or if we found a REG_DEAD note that
11662              covers only part of the given reg, and we have a multi-reg hard
11663              register, then to be safe we must check for REG_DEAD notes
11664              for each register other than the first.  They could have
11665              their own REG_DEAD notes lying around.  */
11666           else if ((note == 0
11667                     || (note != 0
11668                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11669                             < GET_MODE_SIZE (GET_MODE (x)))))
11670                    && regno < FIRST_PSEUDO_REGISTER
11671                    && hard_regno_nregs[regno][GET_MODE (x)] > 1)
11672             {
11673               unsigned int ourend
11674                 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11675               unsigned int i, offset;
11676               rtx oldnotes = 0;
11677
11678               if (note)
11679                 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
11680               else
11681                 offset = 1;
11682
11683               for (i = regno + offset; i < ourend; i++)
11684                 move_deaths (regno_reg_rtx[i],
11685                              maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11686             }
11687
11688           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11689             {
11690               XEXP (note, 1) = *pnotes;
11691               *pnotes = note;
11692             }
11693           else
11694             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11695
11696           REG_N_DEATHS (regno)++;
11697         }
11698
11699       return;
11700     }
11701
11702   else if (GET_CODE (x) == SET)
11703     {
11704       rtx dest = SET_DEST (x);
11705
11706       move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11707
11708       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11709          that accesses one word of a multi-word item, some
11710          piece of everything register in the expression is used by
11711          this insn, so remove any old death.  */
11712       /* ??? So why do we test for equality of the sizes?  */
11713
11714       if (GET_CODE (dest) == ZERO_EXTRACT
11715           || GET_CODE (dest) == STRICT_LOW_PART
11716           || (GET_CODE (dest) == SUBREG
11717               && (((GET_MODE_SIZE (GET_MODE (dest))
11718                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11719                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11720                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11721         {
11722           move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11723           return;
11724         }
11725
11726       /* If this is some other SUBREG, we know it replaces the entire
11727          value, so use that as the destination.  */
11728       if (GET_CODE (dest) == SUBREG)
11729         dest = SUBREG_REG (dest);
11730
11731       /* If this is a MEM, adjust deaths of anything used in the address.
11732          For a REG (the only other possibility), the entire value is
11733          being replaced so the old value is not used in this insn.  */
11734
11735       if (MEM_P (dest))
11736         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11737                      to_insn, pnotes);
11738       return;
11739     }
11740
11741   else if (GET_CODE (x) == CLOBBER)
11742     return;
11743
11744   len = GET_RTX_LENGTH (code);
11745   fmt = GET_RTX_FORMAT (code);
11746
11747   for (i = 0; i < len; i++)
11748     {
11749       if (fmt[i] == 'E')
11750         {
11751           int j;
11752           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11753             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11754                          to_insn, pnotes);
11755         }
11756       else if (fmt[i] == 'e')
11757         move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11758     }
11759 }
11760 \f
11761 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11762    pattern of an insn.  X must be a REG.  */
11763
11764 static int
11765 reg_bitfield_target_p (rtx x, rtx body)
11766 {
11767   int i;
11768
11769   if (GET_CODE (body) == SET)
11770     {
11771       rtx dest = SET_DEST (body);
11772       rtx target;
11773       unsigned int regno, tregno, endregno, endtregno;
11774
11775       if (GET_CODE (dest) == ZERO_EXTRACT)
11776         target = XEXP (dest, 0);
11777       else if (GET_CODE (dest) == STRICT_LOW_PART)
11778         target = SUBREG_REG (XEXP (dest, 0));
11779       else
11780         return 0;
11781
11782       if (GET_CODE (target) == SUBREG)
11783         target = SUBREG_REG (target);
11784
11785       if (!REG_P (target))
11786         return 0;
11787
11788       tregno = REGNO (target), regno = REGNO (x);
11789       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11790         return target == x;
11791
11792       endtregno = tregno + hard_regno_nregs[tregno][GET_MODE (target)];
11793       endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11794
11795       return endregno > tregno && regno < endtregno;
11796     }
11797
11798   else if (GET_CODE (body) == PARALLEL)
11799     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11800       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11801         return 1;
11802
11803   return 0;
11804 }
11805 \f
11806 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11807    as appropriate.  I3 and I2 are the insns resulting from the combination
11808    insns including FROM (I2 may be zero).
11809
11810    Each note in the list is either ignored or placed on some insns, depending
11811    on the type of note.  */
11812
11813 static void
11814 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
11815 {
11816   rtx note, next_note;
11817   rtx tem;
11818
11819   for (note = notes; note; note = next_note)
11820     {
11821       rtx place = 0, place2 = 0;
11822
11823       /* If this NOTE references a pseudo register, ensure it references
11824          the latest copy of that register.  */
11825       if (XEXP (note, 0) && REG_P (XEXP (note, 0))
11826           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11827         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11828
11829       next_note = XEXP (note, 1);
11830       switch (REG_NOTE_KIND (note))
11831         {
11832         case REG_BR_PROB:
11833         case REG_BR_PRED:
11834           /* Doesn't matter much where we put this, as long as it's somewhere.
11835              It is preferable to keep these notes on branches, which is most
11836              likely to be i3.  */
11837           place = i3;
11838           break;
11839
11840         case REG_VALUE_PROFILE:
11841           /* Just get rid of this note, as it is unused later anyway.  */
11842           break;
11843
11844         case REG_NON_LOCAL_GOTO:
11845           if (JUMP_P (i3))
11846             place = i3;
11847           else
11848             {
11849               gcc_assert (i2 && JUMP_P (i2));
11850               place = i2;
11851             }
11852           break;
11853
11854         case REG_EH_REGION:
11855           /* These notes must remain with the call or trapping instruction.  */
11856           if (CALL_P (i3))
11857             place = i3;
11858           else if (i2 && CALL_P (i2))
11859             place = i2;
11860           else
11861             {
11862               gcc_assert (flag_non_call_exceptions);
11863               if (may_trap_p (i3))
11864                 place = i3;
11865               else if (i2 && may_trap_p (i2))
11866                 place = i2;
11867               /* ??? Otherwise assume we've combined things such that we
11868                  can now prove that the instructions can't trap.  Drop the
11869                  note in this case.  */
11870             }
11871           break;
11872
11873         case REG_NORETURN:
11874         case REG_SETJMP:
11875           /* These notes must remain with the call.  It should not be
11876              possible for both I2 and I3 to be a call.  */
11877           if (CALL_P (i3))
11878             place = i3;
11879           else
11880             {
11881               gcc_assert (i2 && CALL_P (i2));
11882               place = i2;
11883             }
11884           break;
11885
11886         case REG_UNUSED:
11887           /* Any clobbers for i3 may still exist, and so we must process
11888              REG_UNUSED notes from that insn.
11889
11890              Any clobbers from i2 or i1 can only exist if they were added by
11891              recog_for_combine.  In that case, recog_for_combine created the
11892              necessary REG_UNUSED notes.  Trying to keep any original
11893              REG_UNUSED notes from these insns can cause incorrect output
11894              if it is for the same register as the original i3 dest.
11895              In that case, we will notice that the register is set in i3,
11896              and then add a REG_UNUSED note for the destination of i3, which
11897              is wrong.  However, it is possible to have REG_UNUSED notes from
11898              i2 or i1 for register which were both used and clobbered, so
11899              we keep notes from i2 or i1 if they will turn into REG_DEAD
11900              notes.  */
11901
11902           /* If this register is set or clobbered in I3, put the note there
11903              unless there is one already.  */
11904           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11905             {
11906               if (from_insn != i3)
11907                 break;
11908
11909               if (! (REG_P (XEXP (note, 0))
11910                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11911                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11912                 place = i3;
11913             }
11914           /* Otherwise, if this register is used by I3, then this register
11915              now dies here, so we must put a REG_DEAD note here unless there
11916              is one already.  */
11917           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11918                    && ! (REG_P (XEXP (note, 0))
11919                          ? find_regno_note (i3, REG_DEAD,
11920                                             REGNO (XEXP (note, 0)))
11921                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11922             {
11923               PUT_REG_NOTE_KIND (note, REG_DEAD);
11924               place = i3;
11925             }
11926           break;
11927
11928         case REG_EQUAL:
11929         case REG_EQUIV:
11930         case REG_NOALIAS:
11931           /* These notes say something about results of an insn.  We can
11932              only support them if they used to be on I3 in which case they
11933              remain on I3.  Otherwise they are ignored.
11934
11935              If the note refers to an expression that is not a constant, we
11936              must also ignore the note since we cannot tell whether the
11937              equivalence is still true.  It might be possible to do
11938              slightly better than this (we only have a problem if I2DEST
11939              or I1DEST is present in the expression), but it doesn't
11940              seem worth the trouble.  */
11941
11942           if (from_insn == i3
11943               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11944             place = i3;
11945           break;
11946
11947         case REG_INC:
11948         case REG_NO_CONFLICT:
11949           /* These notes say something about how a register is used.  They must
11950              be present on any use of the register in I2 or I3.  */
11951           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11952             place = i3;
11953
11954           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11955             {
11956               if (place)
11957                 place2 = i2;
11958               else
11959                 place = i2;
11960             }
11961           break;
11962
11963         case REG_LABEL:
11964           /* This can show up in several ways -- either directly in the
11965              pattern, or hidden off in the constant pool with (or without?)
11966              a REG_EQUAL note.  */
11967           /* ??? Ignore the without-reg_equal-note problem for now.  */
11968           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
11969               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
11970                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11971                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
11972             place = i3;
11973
11974           if (i2
11975               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
11976                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
11977                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11978                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
11979             {
11980               if (place)
11981                 place2 = i2;
11982               else
11983                 place = i2;
11984             }
11985
11986           /* Don't attach REG_LABEL note to a JUMP_INSN.  Add
11987              a JUMP_LABEL instead or decrement LABEL_NUSES.  */
11988           if (place && JUMP_P (place))
11989             {
11990               rtx label = JUMP_LABEL (place);
11991               
11992               if (!label)
11993                 JUMP_LABEL (place) = XEXP (note, 0);
11994               else
11995                 {
11996                   gcc_assert (label == XEXP (note, 0));
11997                   if (LABEL_P (label))
11998                     LABEL_NUSES (label)--;
11999                 }
12000               place = 0;
12001             }
12002           if (place2 && JUMP_P (place2))
12003             {
12004               rtx label = JUMP_LABEL (place2);
12005               
12006               if (!label)
12007                 JUMP_LABEL (place2) = XEXP (note, 0);
12008               else
12009                 {
12010                   gcc_assert (label == XEXP (note, 0));
12011                   if (LABEL_P (label))
12012                     LABEL_NUSES (label)--;
12013                 }
12014               place2 = 0;
12015             }
12016           break;
12017
12018         case REG_NONNEG:
12019           /* This note says something about the value of a register prior
12020              to the execution of an insn.  It is too much trouble to see
12021              if the note is still correct in all situations.  It is better
12022              to simply delete it.  */
12023           break;
12024
12025         case REG_RETVAL:
12026           /* If the insn previously containing this note still exists,
12027              put it back where it was.  Otherwise move it to the previous
12028              insn.  Adjust the corresponding REG_LIBCALL note.  */
12029           if (!NOTE_P (from_insn))
12030             place = from_insn;
12031           else
12032             {
12033               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12034               place = prev_real_insn (from_insn);
12035               if (tem && place)
12036                 XEXP (tem, 0) = place;
12037               /* If we're deleting the last remaining instruction of a
12038                  libcall sequence, don't add the notes.  */
12039               else if (XEXP (note, 0) == from_insn)
12040                 tem = place = 0;
12041               /* Don't add the dangling REG_RETVAL note.  */
12042               else if (! tem)
12043                 place = 0;
12044             }
12045           break;
12046
12047         case REG_LIBCALL:
12048           /* This is handled similarly to REG_RETVAL.  */
12049           if (!NOTE_P (from_insn))
12050             place = from_insn;
12051           else
12052             {
12053               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12054               place = next_real_insn (from_insn);
12055               if (tem && place)
12056                 XEXP (tem, 0) = place;
12057               /* If we're deleting the last remaining instruction of a
12058                  libcall sequence, don't add the notes.  */
12059               else if (XEXP (note, 0) == from_insn)
12060                 tem = place = 0;
12061               /* Don't add the dangling REG_LIBCALL note.  */
12062               else if (! tem)
12063                 place = 0;
12064             }
12065           break;
12066
12067         case REG_DEAD:
12068           /* If the register is used as an input in I3, it dies there.
12069              Similarly for I2, if it is nonzero and adjacent to I3.
12070
12071              If the register is not used as an input in either I3 or I2
12072              and it is not one of the registers we were supposed to eliminate,
12073              there are two possibilities.  We might have a non-adjacent I2
12074              or we might have somehow eliminated an additional register
12075              from a computation.  For example, we might have had A & B where
12076              we discover that B will always be zero.  In this case we will
12077              eliminate the reference to A.
12078
12079              In both cases, we must search to see if we can find a previous
12080              use of A and put the death note there.  */
12081
12082           if (from_insn
12083               && CALL_P (from_insn)
12084               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12085             place = from_insn;
12086           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12087             place = i3;
12088           else if (i2 != 0 && next_nonnote_insn (i2) == i3
12089                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12090             place = i2;
12091
12092           if (place == 0)
12093             {
12094               basic_block bb = this_basic_block;
12095
12096               for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12097                 {
12098                   if (! INSN_P (tem))
12099                     {
12100                       if (tem == BB_HEAD (bb))
12101                         break;
12102                       continue;
12103                     }
12104
12105                   /* If the register is being set at TEM, see if that is all
12106                      TEM is doing.  If so, delete TEM.  Otherwise, make this
12107                      into a REG_UNUSED note instead. Don't delete sets to
12108                      global register vars.  */
12109                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12110                        || !global_regs[REGNO (XEXP (note, 0))])
12111                       && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12112                     {
12113                       rtx set = single_set (tem);
12114                       rtx inner_dest = 0;
12115 #ifdef HAVE_cc0
12116                       rtx cc0_setter = NULL_RTX;
12117 #endif
12118
12119                       if (set != 0)
12120                         for (inner_dest = SET_DEST (set);
12121                              (GET_CODE (inner_dest) == STRICT_LOW_PART
12122                               || GET_CODE (inner_dest) == SUBREG
12123                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
12124                              inner_dest = XEXP (inner_dest, 0))
12125                           ;
12126
12127                       /* Verify that it was the set, and not a clobber that
12128                          modified the register.
12129
12130                          CC0 targets must be careful to maintain setter/user
12131                          pairs.  If we cannot delete the setter due to side
12132                          effects, mark the user with an UNUSED note instead
12133                          of deleting it.  */
12134
12135                       if (set != 0 && ! side_effects_p (SET_SRC (set))
12136                           && rtx_equal_p (XEXP (note, 0), inner_dest)
12137 #ifdef HAVE_cc0
12138                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12139                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12140                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12141 #endif
12142                           )
12143                         {
12144                           /* Move the notes and links of TEM elsewhere.
12145                              This might delete other dead insns recursively.
12146                              First set the pattern to something that won't use
12147                              any register.  */
12148                           rtx old_notes = REG_NOTES (tem);
12149
12150                           PATTERN (tem) = pc_rtx;
12151                           REG_NOTES (tem) = NULL;
12152
12153                           distribute_notes (old_notes, tem, tem, NULL_RTX);
12154                           distribute_links (LOG_LINKS (tem));
12155
12156                           SET_INSN_DELETED (tem);
12157
12158 #ifdef HAVE_cc0
12159                           /* Delete the setter too.  */
12160                           if (cc0_setter)
12161                             {
12162                               PATTERN (cc0_setter) = pc_rtx;
12163                               old_notes = REG_NOTES (cc0_setter);
12164                               REG_NOTES (cc0_setter) = NULL;
12165
12166                               distribute_notes (old_notes, cc0_setter,
12167                                                 cc0_setter, NULL_RTX);
12168                               distribute_links (LOG_LINKS (cc0_setter));
12169
12170                               SET_INSN_DELETED (cc0_setter);
12171                             }
12172 #endif
12173                         }
12174                       else
12175                         {
12176                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
12177
12178                           /*  If there isn't already a REG_UNUSED note, put one
12179                               here.  Do not place a REG_DEAD note, even if
12180                               the register is also used here; that would not
12181                               match the algorithm used in lifetime analysis
12182                               and can cause the consistency check in the
12183                               scheduler to fail.  */
12184                           if (! find_regno_note (tem, REG_UNUSED,
12185                                                  REGNO (XEXP (note, 0))))
12186                             place = tem;
12187                           break;
12188                         }
12189                     }
12190                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12191                            || (CALL_P (tem)
12192                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
12193                     {
12194                       place = tem;
12195
12196                       /* If we are doing a 3->2 combination, and we have a
12197                          register which formerly died in i3 and was not used
12198                          by i2, which now no longer dies in i3 and is used in
12199                          i2 but does not die in i2, and place is between i2
12200                          and i3, then we may need to move a link from place to
12201                          i2.  */
12202                       if (i2 && INSN_UID (place) <= max_uid_cuid
12203                           && INSN_CUID (place) > INSN_CUID (i2)
12204                           && from_insn
12205                           && INSN_CUID (from_insn) > INSN_CUID (i2)
12206                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12207                         {
12208                           rtx links = LOG_LINKS (place);
12209                           LOG_LINKS (place) = 0;
12210                           distribute_links (links);
12211                         }
12212                       break;
12213                     }
12214
12215                   if (tem == BB_HEAD (bb))
12216                     break;
12217                 }
12218
12219               /* We haven't found an insn for the death note and it
12220                  is still a REG_DEAD note, but we have hit the beginning
12221                  of the block.  If the existing life info says the reg
12222                  was dead, there's nothing left to do.  Otherwise, we'll
12223                  need to do a global life update after combine.  */
12224               if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12225                   && REGNO_REG_SET_P (bb->il.rtl->global_live_at_start,
12226                                       REGNO (XEXP (note, 0))))
12227                 SET_BIT (refresh_blocks, this_basic_block->index);
12228             }
12229
12230           /* If the register is set or already dead at PLACE, we needn't do
12231              anything with this note if it is still a REG_DEAD note.
12232              We check here if it is set at all, not if is it totally replaced,
12233              which is what `dead_or_set_p' checks, so also check for it being
12234              set partially.  */
12235
12236           if (place && REG_NOTE_KIND (note) == REG_DEAD)
12237             {
12238               unsigned int regno = REGNO (XEXP (note, 0));
12239
12240               /* Similarly, if the instruction on which we want to place
12241                  the note is a noop, we'll need do a global live update
12242                  after we remove them in delete_noop_moves.  */
12243               if (noop_move_p (place))
12244                 SET_BIT (refresh_blocks, this_basic_block->index);
12245
12246               if (dead_or_set_p (place, XEXP (note, 0))
12247                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12248                 {
12249                   /* Unless the register previously died in PLACE, clear
12250                      last_death.  [I no longer understand why this is
12251                      being done.] */
12252                   if (reg_stat[regno].last_death != place)
12253                     reg_stat[regno].last_death = 0;
12254                   place = 0;
12255                 }
12256               else
12257                 reg_stat[regno].last_death = place;
12258
12259               /* If this is a death note for a hard reg that is occupying
12260                  multiple registers, ensure that we are still using all
12261                  parts of the object.  If we find a piece of the object
12262                  that is unused, we must arrange for an appropriate REG_DEAD
12263                  note to be added for it.  However, we can't just emit a USE
12264                  and tag the note to it, since the register might actually
12265                  be dead; so we recourse, and the recursive call then finds
12266                  the previous insn that used this register.  */
12267
12268               if (place && regno < FIRST_PSEUDO_REGISTER
12269                   && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12270                 {
12271                   unsigned int endregno
12272                     = regno + hard_regno_nregs[regno]
12273                                               [GET_MODE (XEXP (note, 0))];
12274                   int all_used = 1;
12275                   unsigned int i;
12276
12277                   for (i = regno; i < endregno; i++)
12278                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12279                          && ! find_regno_fusage (place, USE, i))
12280                         || dead_or_set_regno_p (place, i))
12281                       all_used = 0;
12282
12283                   if (! all_used)
12284                     {
12285                       /* Put only REG_DEAD notes for pieces that are
12286                          not already dead or set.  */
12287
12288                       for (i = regno; i < endregno;
12289                            i += hard_regno_nregs[i][reg_raw_mode[i]])
12290                         {
12291                           rtx piece = regno_reg_rtx[i];
12292                           basic_block bb = this_basic_block;
12293
12294                           if (! dead_or_set_p (place, piece)
12295                               && ! reg_bitfield_target_p (piece,
12296                                                           PATTERN (place)))
12297                             {
12298                               rtx new_note
12299                                 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12300
12301                               distribute_notes (new_note, place, place,
12302                                                 NULL_RTX);
12303                             }
12304                           else if (! refers_to_regno_p (i, i + 1,
12305                                                         PATTERN (place), 0)
12306                                    && ! find_regno_fusage (place, USE, i))
12307                             for (tem = PREV_INSN (place); ;
12308                                  tem = PREV_INSN (tem))
12309                               {
12310                                 if (! INSN_P (tem))
12311                                   {
12312                                     if (tem == BB_HEAD (bb))
12313                                       {
12314                                         SET_BIT (refresh_blocks,
12315                                                  this_basic_block->index);
12316                                         break;
12317                                       }
12318                                     continue;
12319                                   }
12320                                 if (dead_or_set_p (tem, piece)
12321                                     || reg_bitfield_target_p (piece,
12322                                                               PATTERN (tem)))
12323                                   {
12324                                     REG_NOTES (tem)
12325                                       = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12326                                                            REG_NOTES (tem));
12327                                     break;
12328                                   }
12329                               }
12330
12331                         }
12332
12333                       place = 0;
12334                     }
12335                 }
12336             }
12337           break;
12338
12339         default:
12340           /* Any other notes should not be present at this point in the
12341              compilation.  */
12342           gcc_unreachable ();
12343         }
12344
12345       if (place)
12346         {
12347           XEXP (note, 1) = REG_NOTES (place);
12348           REG_NOTES (place) = note;
12349         }
12350       else if ((REG_NOTE_KIND (note) == REG_DEAD
12351                 || REG_NOTE_KIND (note) == REG_UNUSED)
12352                && REG_P (XEXP (note, 0)))
12353         REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12354
12355       if (place2)
12356         {
12357           if ((REG_NOTE_KIND (note) == REG_DEAD
12358                || REG_NOTE_KIND (note) == REG_UNUSED)
12359               && REG_P (XEXP (note, 0)))
12360             REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12361
12362           REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12363                                                REG_NOTE_KIND (note),
12364                                                XEXP (note, 0),
12365                                                REG_NOTES (place2));
12366         }
12367     }
12368 }
12369 \f
12370 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12371    I3, I2, and I1 to new locations.  This is also called to add a link
12372    pointing at I3 when I3's destination is changed.  */
12373
12374 static void
12375 distribute_links (rtx links)
12376 {
12377   rtx link, next_link;
12378
12379   for (link = links; link; link = next_link)
12380     {
12381       rtx place = 0;
12382       rtx insn;
12383       rtx set, reg;
12384
12385       next_link = XEXP (link, 1);
12386
12387       /* If the insn that this link points to is a NOTE or isn't a single
12388          set, ignore it.  In the latter case, it isn't clear what we
12389          can do other than ignore the link, since we can't tell which
12390          register it was for.  Such links wouldn't be used by combine
12391          anyway.
12392
12393          It is not possible for the destination of the target of the link to
12394          have been changed by combine.  The only potential of this is if we
12395          replace I3, I2, and I1 by I3 and I2.  But in that case the
12396          destination of I2 also remains unchanged.  */
12397
12398       if (NOTE_P (XEXP (link, 0))
12399           || (set = single_set (XEXP (link, 0))) == 0)
12400         continue;
12401
12402       reg = SET_DEST (set);
12403       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12404              || GET_CODE (reg) == STRICT_LOW_PART)
12405         reg = XEXP (reg, 0);
12406
12407       /* A LOG_LINK is defined as being placed on the first insn that uses
12408          a register and points to the insn that sets the register.  Start
12409          searching at the next insn after the target of the link and stop
12410          when we reach a set of the register or the end of the basic block.
12411
12412          Note that this correctly handles the link that used to point from
12413          I3 to I2.  Also note that not much searching is typically done here
12414          since most links don't point very far away.  */
12415
12416       for (insn = NEXT_INSN (XEXP (link, 0));
12417            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12418                      || BB_HEAD (this_basic_block->next_bb) != insn));
12419            insn = NEXT_INSN (insn))
12420         if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12421           {
12422             if (reg_referenced_p (reg, PATTERN (insn)))
12423               place = insn;
12424             break;
12425           }
12426         else if (CALL_P (insn)
12427                  && find_reg_fusage (insn, USE, reg))
12428           {
12429             place = insn;
12430             break;
12431           }
12432         else if (INSN_P (insn) && reg_set_p (reg, insn))
12433           break;
12434
12435       /* If we found a place to put the link, place it there unless there
12436          is already a link to the same insn as LINK at that point.  */
12437
12438       if (place)
12439         {
12440           rtx link2;
12441
12442           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12443             if (XEXP (link2, 0) == XEXP (link, 0))
12444               break;
12445
12446           if (link2 == 0)
12447             {
12448               XEXP (link, 1) = LOG_LINKS (place);
12449               LOG_LINKS (place) = link;
12450
12451               /* Set added_links_insn to the earliest insn we added a
12452                  link to.  */
12453               if (added_links_insn == 0
12454                   || INSN_CUID (added_links_insn) > INSN_CUID (place))
12455                 added_links_insn = place;
12456             }
12457         }
12458     }
12459 }
12460 \f
12461 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12462    Check whether the expression pointer to by LOC is a register or
12463    memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12464    Otherwise return zero.  */
12465
12466 static int
12467 unmentioned_reg_p_1 (rtx *loc, void *expr)
12468 {
12469   rtx x = *loc;
12470
12471   if (x != NULL_RTX
12472       && (REG_P (x) || MEM_P (x))
12473       && ! reg_mentioned_p (x, (rtx) expr))
12474     return 1;
12475   return 0;
12476 }
12477
12478 /* Check for any register or memory mentioned in EQUIV that is not
12479    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
12480    of EXPR where some registers may have been replaced by constants.  */
12481
12482 static bool
12483 unmentioned_reg_p (rtx equiv, rtx expr)
12484 {
12485   return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12486 }
12487 \f
12488 /* Compute INSN_CUID for INSN, which is an insn made by combine.  */
12489
12490 static int
12491 insn_cuid (rtx insn)
12492 {
12493   while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12494          && NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE)
12495     insn = NEXT_INSN (insn);
12496
12497   gcc_assert (INSN_UID (insn) <= max_uid_cuid);
12498
12499   return INSN_CUID (insn);
12500 }
12501 \f
12502 void
12503 dump_combine_stats (FILE *file)
12504 {
12505   fprintf
12506     (file,
12507      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12508      combine_attempts, combine_merges, combine_extras, combine_successes);
12509 }
12510
12511 void
12512 dump_combine_total_stats (FILE *file)
12513 {
12514   fprintf
12515     (file,
12516      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12517      total_attempts, total_merges, total_extras, total_successes);
12518 }
12519 \f
12520
12521 static bool
12522 gate_handle_combine (void)
12523 {
12524   return (optimize > 0);
12525 }
12526
12527 /* Try combining insns through substitution.  */
12528 static void
12529 rest_of_handle_combine (void)
12530 {
12531   int rebuild_jump_labels_after_combine
12532     = combine_instructions (get_insns (), max_reg_num ());
12533
12534   /* Combining insns may have turned an indirect jump into a
12535      direct jump.  Rebuild the JUMP_LABEL fields of jumping
12536      instructions.  */
12537   if (rebuild_jump_labels_after_combine)
12538     {
12539       timevar_push (TV_JUMP);
12540       rebuild_jump_labels (get_insns ());
12541       timevar_pop (TV_JUMP);
12542
12543       delete_dead_jumptables ();
12544       cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
12545     }
12546 }
12547
12548 struct tree_opt_pass pass_combine =
12549 {
12550   "combine",                            /* name */
12551   gate_handle_combine,                  /* gate */
12552   rest_of_handle_combine,               /* execute */
12553   NULL,                                 /* sub */
12554   NULL,                                 /* next */
12555   0,                                    /* static_pass_number */
12556   TV_COMBINE,                           /* tv_id */
12557   0,                                    /* properties_required */
12558   0,                                    /* properties_provided */
12559   0,                                    /* properties_destroyed */
12560   0,                                    /* todo_flags_start */
12561   TODO_dump_func |
12562   TODO_ggc_collect,                     /* todo_flags_finish */
12563   'c'                                   /* letter */
12564 };
12565