OSDN Git Service

CLIX patch from Thomas Dickey via urs@akk.uni-karlsruhe.de (Urs Janssen).
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 88, 92-96, 1997 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
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    - reg_n_refs is not adjusted in the rare case when a register is
57      no longer required in a computation
58    - there are extremely rare cases (see distribute_regnotes) when a
59      REG_DEAD note is lost
60    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
61      removed because there is no way to know which register it was 
62      linking
63
64    To simplify substitution, we combine only when the earlier insn(s)
65    consist of only a single assignment.  To simplify updating afterward,
66    we never combine when a subroutine call appears in the middle.
67
68    Since we do not represent assignments to CC0 explicitly except when that
69    is all an insn does, there is no LOG_LINKS entry in an insn that uses
70    the condition code for the insn that set the condition code.
71    Fortunately, these two insns must be consecutive.
72    Therefore, every JUMP_INSN is taken to have an implicit logical link
73    to the preceding insn.  This is not quite right, since non-jumps can
74    also use the condition code; but in practice such insns would not
75    combine anyway.  */
76
77 #include "config.h"
78 #ifdef __STDC__
79 #include <stdarg.h>
80 #else
81 #include <varargs.h>
82 #endif
83
84 /* Must precede rtl.h for FFS.  */
85 #include <stdio.h>
86
87 #include "rtl.h"
88 #include "flags.h"
89 #include "regs.h"
90 #include "hard-reg-set.h"
91 #include "expr.h"
92 #include "basic-block.h"
93 #include "insn-config.h"
94 #include "insn-flags.h"
95 #include "insn-codes.h"
96 #include "insn-attr.h"
97 #include "recog.h"
98 #include "real.h"
99
100 /* It is not safe to use ordinary gen_lowpart in combine.
101    Use gen_lowpart_for_combine instead.  See comments there.  */
102 #define gen_lowpart dont_use_gen_lowpart_you_dummy
103
104 /* Number of attempts to combine instructions in this function.  */
105
106 static int combine_attempts;
107
108 /* Number of attempts that got as far as substitution in this function.  */
109
110 static int combine_merges;
111
112 /* Number of instructions combined with added SETs in this function.  */
113
114 static int combine_extras;
115
116 /* Number of instructions combined in this function.  */
117
118 static int combine_successes;
119
120 /* Totals over entire compilation.  */
121
122 static int total_attempts, total_merges, total_extras, total_successes;
123
124 /* Define a default value for REVERSIBLE_CC_MODE.
125    We can never assume that a condition code mode is safe to reverse unless
126    the md tells us so.  */
127 #ifndef REVERSIBLE_CC_MODE
128 #define REVERSIBLE_CC_MODE(MODE) 0
129 #endif
130 \f
131 /* Vector mapping INSN_UIDs to cuids.
132    The cuids are like uids but increase monotonically always.
133    Combine always uses cuids so that it can compare them.
134    But actually renumbering the uids, which we used to do,
135    proves to be a bad idea because it makes it hard to compare
136    the dumps produced by earlier passes with those from later passes.  */
137
138 static int *uid_cuid;
139 static int max_uid_cuid;
140
141 /* Get the cuid of an insn.  */
142
143 #define INSN_CUID(INSN) \
144 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
145
146 /* Maximum register number, which is the size of the tables below.  */
147
148 static int combine_max_regno;
149
150 /* Record last point of death of (hard or pseudo) register n.  */
151
152 static rtx *reg_last_death;
153
154 /* Record last point of modification of (hard or pseudo) register n.  */
155
156 static rtx *reg_last_set;
157
158 /* Record the cuid of the last insn that invalidated memory
159    (anything that writes memory, and subroutine calls, but not pushes).  */
160
161 static int mem_last_set;
162
163 /* Record the cuid of the last CALL_INSN
164    so we can tell whether a potential combination crosses any calls.  */
165
166 static int last_call_cuid;
167
168 /* When `subst' is called, this is the insn that is being modified
169    (by combining in a previous insn).  The PATTERN of this insn
170    is still the old pattern partially modified and it should not be
171    looked at, but this may be used to examine the successors of the insn
172    to judge whether a simplification is valid.  */
173
174 static rtx subst_insn;
175
176 /* This is an insn that belongs before subst_insn, but is not currently
177    on the insn chain.  */
178
179 static rtx subst_prev_insn;
180
181 /* This is the lowest CUID that `subst' is currently dealing with.
182    get_last_value will not return a value if the register was set at or
183    after this CUID.  If not for this mechanism, we could get confused if
184    I2 or I1 in try_combine were an insn that used the old value of a register
185    to obtain a new value.  In that case, we might erroneously get the
186    new value of the register when we wanted the old one.  */
187
188 static int subst_low_cuid;
189
190 /* This contains any hard registers that are used in newpat; reg_dead_at_p
191    must consider all these registers to be always live.  */
192
193 static HARD_REG_SET newpat_used_regs;
194
195 /* This is an insn to which a LOG_LINKS entry has been added.  If this
196    insn is the earlier than I2 or I3, combine should rescan starting at
197    that location.  */
198
199 static rtx added_links_insn;
200
201 /* Basic block number of the block in which we are performing combines.  */
202 static int this_basic_block;
203 \f
204 /* The next group of arrays allows the recording of the last value assigned
205    to (hard or pseudo) register n.  We use this information to see if a
206    operation being processed is redundant given a prior operation performed
207    on the register.  For example, an `and' with a constant is redundant if
208    all the zero bits are already known to be turned off.
209
210    We use an approach similar to that used by cse, but change it in the
211    following ways:
212
213    (1) We do not want to reinitialize at each label.
214    (2) It is useful, but not critical, to know the actual value assigned
215        to a register.  Often just its form is helpful.
216
217    Therefore, we maintain the following arrays:
218
219    reg_last_set_value           the last value assigned
220    reg_last_set_label           records the value of label_tick when the
221                                 register was assigned
222    reg_last_set_table_tick      records the value of label_tick when a
223                                 value using the register is assigned
224    reg_last_set_invalid         set to non-zero when it is not valid
225                                 to use the value of this register in some
226                                 register's value
227
228    To understand the usage of these tables, it is important to understand
229    the distinction between the value in reg_last_set_value being valid
230    and the register being validly contained in some other expression in the
231    table.
232
233    Entry I in reg_last_set_value is valid if it is non-zero, and either
234    reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
235
236    Register I may validly appear in any expression returned for the value
237    of another register if reg_n_sets[i] is 1.  It may also appear in the
238    value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
239    reg_last_set_invalid[j] is zero.
240
241    If an expression is found in the table containing a register which may
242    not validly appear in an expression, the register is replaced by
243    something that won't match, (clobber (const_int 0)).
244
245    reg_last_set_invalid[i] is set non-zero when register I is being assigned
246    to and reg_last_set_table_tick[i] == label_tick.  */
247
248 /* Record last value assigned to (hard or pseudo) register n.  */
249
250 static rtx *reg_last_set_value;
251
252 /* Record the value of label_tick when the value for register n is placed in
253    reg_last_set_value[n].  */
254
255 static int *reg_last_set_label;
256
257 /* Record the value of label_tick when an expression involving register n
258    is placed in reg_last_set_value.  */
259
260 static int *reg_last_set_table_tick;
261
262 /* Set non-zero if references to register n in expressions should not be
263    used.  */
264
265 static char *reg_last_set_invalid;
266
267 /* Incremented for each label.  */
268
269 static int label_tick;
270
271 /* Some registers that are set more than once and used in more than one
272    basic block are nevertheless always set in similar ways.  For example,
273    a QImode register may be loaded from memory in two places on a machine
274    where byte loads zero extend.
275
276    We record in the following array what we know about the nonzero
277    bits of a register, specifically which bits are known to be zero.
278
279    If an entry is zero, it means that we don't know anything special.  */
280
281 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
282
283 /* Mode used to compute significance in reg_nonzero_bits.  It is the largest
284    integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
285
286 static enum machine_mode nonzero_bits_mode;
287
288 /* Nonzero if we know that a register has some leading bits that are always
289    equal to the sign bit.  */
290
291 static char *reg_sign_bit_copies;
292
293 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
294    It is zero while computing them and after combine has completed.  This
295    former test prevents propagating values based on previously set values,
296    which can be incorrect if a variable is modified in a loop.  */
297
298 static int nonzero_sign_valid;
299
300 /* These arrays are maintained in parallel with reg_last_set_value
301    and are used to store the mode in which the register was last set,
302    the bits that were known to be zero when it was last set, and the
303    number of sign bits copies it was known to have when it was last set.  */
304
305 static enum machine_mode *reg_last_set_mode;
306 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
307 static char *reg_last_set_sign_bit_copies;
308 \f
309 /* Record one modification to rtl structure
310    to be undone by storing old_contents into *where.
311    is_int is 1 if the contents are an int.  */
312
313 struct undo
314 {
315   struct undo *next;
316   int is_int;
317   union {rtx r; int i;} old_contents;
318   union {rtx *r; int *i;} where;
319 };
320
321 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
322    num_undo says how many are currently recorded.
323
324    storage is nonzero if we must undo the allocation of new storage.
325    The value of storage is what to pass to obfree.
326
327    other_insn is nonzero if we have modified some other insn in the process
328    of working on subst_insn.  It must be verified too.
329
330    previous_undos is the value of undobuf.undos when we started processing
331    this substitution.  This will prevent gen_rtx_combine from re-used a piece
332    from the previous expression.  Doing so can produce circular rtl
333    structures.  */
334
335 struct undobuf
336 {
337   char *storage;
338   struct undo *undos;
339   struct undo *frees;
340   struct undo *previous_undos;
341   rtx other_insn;
342 };
343
344 static struct undobuf undobuf;
345
346 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
347    insn.  The substitution can be undone by undo_all.  If INTO is already
348    set to NEWVAL, do not record this change.  Because computing NEWVAL might
349    also call SUBST, we have to compute it before we put anything into
350    the undo table.  */
351
352 #define SUBST(INTO, NEWVAL)  \
353  do { rtx _new = (NEWVAL);                                      \
354       struct undo *_buf;                                        \
355                                                                 \
356       if (undobuf.frees)                                        \
357         _buf = undobuf.frees, undobuf.frees = _buf->next;       \
358       else                                                      \
359         _buf = (struct undo *) xmalloc (sizeof (struct undo));  \
360                                                                 \
361       _buf->is_int = 0;                                         \
362       _buf->where.r = &INTO;                                    \
363       _buf->old_contents.r = INTO;                              \
364       INTO = _new;                                              \
365       if (_buf->old_contents.r == INTO)                         \
366         _buf->next = undobuf.frees, undobuf.frees = _buf;       \
367       else                                                      \
368         _buf->next = undobuf.undos, undobuf.undos = _buf;       \
369     } while (0)
370
371 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
372    for the value of a HOST_WIDE_INT value (including CONST_INT) is
373    not safe.  */
374
375 #define SUBST_INT(INTO, NEWVAL)  \
376  do { struct undo *_buf;                                        \
377                                                                 \
378       if (undobuf.frees)                                        \
379         _buf = undobuf.frees, undobuf.frees = _buf->next;       \
380       else                                                      \
381         _buf = (struct undo *) xmalloc (sizeof (struct undo));  \
382                                                                 \
383       _buf->is_int = 1;                                         \
384       _buf->where.i = (int *) &INTO;                            \
385       _buf->old_contents.i = INTO;                              \
386       INTO = NEWVAL;                                            \
387       if (_buf->old_contents.i == INTO)                         \
388         _buf->next = undobuf.frees, undobuf.frees = _buf;       \
389       else                                                      \
390         _buf->next = undobuf.undos, undobuf.undos = _buf;       \
391      } while (0)
392
393 /* Number of times the pseudo being substituted for
394    was found and replaced.  */
395
396 static int n_occurrences;
397
398 static void init_reg_last_arrays        PROTO((void));
399 static void setup_incoming_promotions   PROTO((void));
400 static void set_nonzero_bits_and_sign_copies  PROTO((rtx, rtx));
401 static int can_combine_p        PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
402 static int combinable_i3pat     PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
403 static rtx try_combine          PROTO((rtx, rtx, rtx));
404 static void undo_all            PROTO((void));
405 static rtx *find_split_point    PROTO((rtx *, rtx));
406 static rtx subst                PROTO((rtx, rtx, rtx, int, int));
407 static rtx simplify_rtx         PROTO((rtx, enum machine_mode, int, int));
408 static rtx simplify_if_then_else  PROTO((rtx));
409 static rtx simplify_set         PROTO((rtx));
410 static rtx simplify_logical     PROTO((rtx, int));
411 static rtx expand_compound_operation  PROTO((rtx));
412 static rtx expand_field_assignment  PROTO((rtx));
413 static rtx make_extraction      PROTO((enum machine_mode, rtx, int, rtx, int,
414                                        int, int, int));
415 static rtx extract_left_shift   PROTO((rtx, int));
416 static rtx make_compound_operation  PROTO((rtx, enum rtx_code));
417 static int get_pos_from_mask    PROTO((unsigned HOST_WIDE_INT, int *));
418 static rtx force_to_mode        PROTO((rtx, enum machine_mode,
419                                        unsigned HOST_WIDE_INT, rtx, int));
420 static rtx if_then_else_cond    PROTO((rtx, rtx *, rtx *));
421 static rtx known_cond           PROTO((rtx, enum rtx_code, rtx, rtx));
422 static int rtx_equal_for_field_assignment_p PROTO((rtx, rtx));
423 static rtx make_field_assignment  PROTO((rtx));
424 static rtx apply_distributive_law  PROTO((rtx));
425 static rtx simplify_and_const_int  PROTO((rtx, enum machine_mode, rtx,
426                                           unsigned HOST_WIDE_INT));
427 static unsigned HOST_WIDE_INT nonzero_bits  PROTO((rtx, enum machine_mode));
428 static int num_sign_bit_copies  PROTO((rtx, enum machine_mode));
429 static int merge_outer_ops      PROTO((enum rtx_code *, HOST_WIDE_INT *,
430                                        enum rtx_code, HOST_WIDE_INT,
431                                        enum machine_mode, int *));
432 static rtx simplify_shift_const PROTO((rtx, enum rtx_code, enum machine_mode,
433                                        rtx, int));
434 static int recog_for_combine    PROTO((rtx *, rtx, rtx *, int *));
435 static rtx gen_lowpart_for_combine  PROTO((enum machine_mode, rtx));
436 static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
437                                   ...));
438 static rtx gen_binary           PROTO((enum rtx_code, enum machine_mode,
439                                        rtx, rtx));
440 static rtx gen_unary            PROTO((enum rtx_code, enum machine_mode,
441                                        enum machine_mode, rtx));
442 static enum rtx_code simplify_comparison  PROTO((enum rtx_code, rtx *, rtx *));
443 static int reversible_comparison_p  PROTO((rtx));
444 static void update_table_tick   PROTO((rtx));
445 static void record_value_for_reg  PROTO((rtx, rtx, rtx));
446 static void record_dead_and_set_regs_1  PROTO((rtx, rtx));
447 static void record_dead_and_set_regs  PROTO((rtx));
448 static int get_last_value_validate  PROTO((rtx *, rtx, int, int));
449 static rtx get_last_value       PROTO((rtx));
450 static int use_crosses_set_p    PROTO((rtx, int));
451 static void reg_dead_at_p_1     PROTO((rtx, rtx));
452 static int reg_dead_at_p        PROTO((rtx, rtx));
453 static void move_deaths         PROTO((rtx, rtx, int, rtx, rtx *));
454 static int reg_bitfield_target_p  PROTO((rtx, rtx));
455 static void distribute_notes    PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
456 static void distribute_links    PROTO((rtx));
457 static void mark_used_regs_combine PROTO((rtx));
458 static int insn_cuid            PROTO((rtx));
459 \f
460 /* Main entry point for combiner.  F is the first insn of the function.
461    NREGS is the first unused pseudo-reg number.  */
462
463 void
464 combine_instructions (f, nregs)
465      rtx f;
466      int nregs;
467 {
468   register rtx insn, next, prev;
469   register int i;
470   register rtx links, nextlinks;
471
472   combine_attempts = 0;
473   combine_merges = 0;
474   combine_extras = 0;
475   combine_successes = 0;
476   undobuf.undos = undobuf.previous_undos = 0;
477
478   combine_max_regno = nregs;
479
480   reg_nonzero_bits
481     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
482   reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
483
484   bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
485   bzero (reg_sign_bit_copies, nregs * sizeof (char));
486
487   reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
488   reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
489   reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
490   reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
491   reg_last_set_label = (int *) alloca (nregs * sizeof (int));
492   reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
493   reg_last_set_mode
494     = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
495   reg_last_set_nonzero_bits
496     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
497   reg_last_set_sign_bit_copies
498     = (char *) alloca (nregs * sizeof (char));
499
500   init_reg_last_arrays ();
501
502   init_recog_no_volatile ();
503
504   /* Compute maximum uid value so uid_cuid can be allocated.  */
505
506   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
507     if (INSN_UID (insn) > i)
508       i = INSN_UID (insn);
509
510   uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
511   max_uid_cuid = i;
512
513   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
514
515   /* Don't use reg_nonzero_bits when computing it.  This can cause problems
516      when, for example, we have j <<= 1 in a loop.  */
517
518   nonzero_sign_valid = 0;
519
520   /* Compute the mapping from uids to cuids.
521      Cuids are numbers assigned to insns, like uids,
522      except that cuids increase monotonically through the code. 
523
524      Scan all SETs and see if we can deduce anything about what
525      bits are known to be zero for some registers and how many copies
526      of the sign bit are known to exist for those registers.
527
528      Also set any known values so that we can use it while searching
529      for what bits are known to be set.  */
530
531   label_tick = 1;
532
533   /* We need to initialize it here, because record_dead_and_set_regs may call
534      get_last_value.  */
535   subst_prev_insn = NULL_RTX;
536
537   setup_incoming_promotions ();
538
539   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
540     {
541       uid_cuid[INSN_UID (insn)] = ++i;
542       subst_low_cuid = i;
543       subst_insn = insn;
544
545       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
546         {
547           note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
548           record_dead_and_set_regs (insn);
549
550 #ifdef AUTO_INC_DEC
551           for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
552             if (REG_NOTE_KIND (links) == REG_INC)
553               set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX);
554 #endif
555         }
556
557       if (GET_CODE (insn) == CODE_LABEL)
558         label_tick++;
559     }
560
561   nonzero_sign_valid = 1;
562
563   /* Now scan all the insns in forward order.  */
564
565   this_basic_block = -1;
566   label_tick = 1;
567   last_call_cuid = 0;
568   mem_last_set = 0;
569   init_reg_last_arrays ();
570   setup_incoming_promotions ();
571
572   for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
573     {
574       next = 0;
575
576       /* If INSN starts a new basic block, update our basic block number.  */
577       if (this_basic_block + 1 < n_basic_blocks
578           && basic_block_head[this_basic_block + 1] == insn)
579         this_basic_block++;
580
581       if (GET_CODE (insn) == CODE_LABEL)
582         label_tick++;
583
584       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
585         {
586           /* Try this insn with each insn it links back to.  */
587
588           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
589             if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
590               goto retry;
591
592           /* Try each sequence of three linked insns ending with this one.  */
593
594           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
595             for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
596                  nextlinks = XEXP (nextlinks, 1))
597               if ((next = try_combine (insn, XEXP (links, 0),
598                                        XEXP (nextlinks, 0))) != 0)
599                 goto retry;
600
601 #ifdef HAVE_cc0
602           /* Try to combine a jump insn that uses CC0
603              with a preceding insn that sets CC0, and maybe with its
604              logical predecessor as well.
605              This is how we make decrement-and-branch insns.
606              We need this special code because data flow connections
607              via CC0 do not get entered in LOG_LINKS.  */
608
609           if (GET_CODE (insn) == JUMP_INSN
610               && (prev = prev_nonnote_insn (insn)) != 0
611               && GET_CODE (prev) == INSN
612               && sets_cc0_p (PATTERN (prev)))
613             {
614               if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
615                 goto retry;
616
617               for (nextlinks = LOG_LINKS (prev); nextlinks;
618                    nextlinks = XEXP (nextlinks, 1))
619                 if ((next = try_combine (insn, prev,
620                                          XEXP (nextlinks, 0))) != 0)
621                   goto retry;
622             }
623
624           /* Do the same for an insn that explicitly references CC0.  */
625           if (GET_CODE (insn) == INSN
626               && (prev = prev_nonnote_insn (insn)) != 0
627               && GET_CODE (prev) == INSN
628               && sets_cc0_p (PATTERN (prev))
629               && GET_CODE (PATTERN (insn)) == SET
630               && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
631             {
632               if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
633                 goto retry;
634
635               for (nextlinks = LOG_LINKS (prev); nextlinks;
636                    nextlinks = XEXP (nextlinks, 1))
637                 if ((next = try_combine (insn, prev,
638                                          XEXP (nextlinks, 0))) != 0)
639                   goto retry;
640             }
641
642           /* Finally, see if any of the insns that this insn links to
643              explicitly references CC0.  If so, try this insn, that insn,
644              and its predecessor if it sets CC0.  */
645           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
646             if (GET_CODE (XEXP (links, 0)) == INSN
647                 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
648                 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
649                 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
650                 && GET_CODE (prev) == INSN
651                 && sets_cc0_p (PATTERN (prev))
652                 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
653               goto retry;
654 #endif
655
656           /* Try combining an insn with two different insns whose results it
657              uses.  */
658           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
659             for (nextlinks = XEXP (links, 1); nextlinks;
660                  nextlinks = XEXP (nextlinks, 1))
661               if ((next = try_combine (insn, XEXP (links, 0),
662                                        XEXP (nextlinks, 0))) != 0)
663                 goto retry;
664
665           if (GET_CODE (insn) != NOTE)
666             record_dead_and_set_regs (insn);
667
668         retry:
669           ;
670         }
671     }
672
673   total_attempts += combine_attempts;
674   total_merges += combine_merges;
675   total_extras += combine_extras;
676   total_successes += combine_successes;
677
678   nonzero_sign_valid = 0;
679 }
680
681 /* Wipe the reg_last_xxx arrays in preparation for another pass.  */
682
683 static void
684 init_reg_last_arrays ()
685 {
686   int nregs = combine_max_regno;
687
688   bzero ((char *) reg_last_death, nregs * sizeof (rtx));
689   bzero ((char *) reg_last_set, nregs * sizeof (rtx));
690   bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
691   bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
692   bzero ((char *) reg_last_set_label, nregs * sizeof (int));
693   bzero (reg_last_set_invalid, nregs * sizeof (char));
694   bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
695   bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
696   bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
697 }
698 \f
699 /* Set up any promoted values for incoming argument registers.  */
700
701 static void
702 setup_incoming_promotions ()
703 {
704 #ifdef PROMOTE_FUNCTION_ARGS
705   int regno;
706   rtx reg;
707   enum machine_mode mode;
708   int unsignedp;
709   rtx first = get_insns ();
710
711   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
712     if (FUNCTION_ARG_REGNO_P (regno)
713         && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
714       record_value_for_reg (reg, first,
715                             gen_rtx (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
716                                      GET_MODE (reg),
717                                      gen_rtx (CLOBBER, mode, const0_rtx)));
718 #endif
719 }
720 \f
721 /* Called via note_stores.  If X is a pseudo that is narrower than
722    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
723
724    If we are setting only a portion of X and we can't figure out what
725    portion, assume all bits will be used since we don't know what will
726    be happening.
727
728    Similarly, set how many bits of X are known to be copies of the sign bit
729    at all locations in the function.  This is the smallest number implied 
730    by any set of X.  */
731
732 static void
733 set_nonzero_bits_and_sign_copies (x, set)
734      rtx x;
735      rtx set;
736 {
737   int num;
738
739   if (GET_CODE (x) == REG
740       && REGNO (x) >= FIRST_PSEUDO_REGISTER
741       /* If this register is undefined at the start of the file, we can't
742          say what its contents were.  */
743       && ! (basic_block_live_at_start[0][REGNO (x) / REGSET_ELT_BITS]
744             & ((REGSET_ELT_TYPE) 1 << (REGNO (x) % REGSET_ELT_BITS)))
745       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
746     {
747       if (set == 0 || GET_CODE (set) == CLOBBER)
748         {
749           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
750           reg_sign_bit_copies[REGNO (x)] = 1;
751           return;
752         }
753
754       /* If this is a complex assignment, see if we can convert it into a
755          simple assignment.  */
756       set = expand_field_assignment (set);
757
758       /* If this is a simple assignment, or we have a paradoxical SUBREG,
759          set what we know about X.  */
760
761       if (SET_DEST (set) == x
762           || (GET_CODE (SET_DEST (set)) == SUBREG
763               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
764                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
765               && SUBREG_REG (SET_DEST (set)) == x))
766         {
767           rtx src = SET_SRC (set);
768
769 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
770           /* If X is narrower than a word and SRC is a non-negative
771              constant that would appear negative in the mode of X,
772              sign-extend it for use in reg_nonzero_bits because some
773              machines (maybe most) will actually do the sign-extension
774              and this is the conservative approach. 
775
776              ??? For 2.5, try to tighten up the MD files in this regard
777              instead of this kludge.  */
778
779           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
780               && GET_CODE (src) == CONST_INT
781               && INTVAL (src) > 0
782               && 0 != (INTVAL (src)
783                        & ((HOST_WIDE_INT) 1
784                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
785             src = GEN_INT (INTVAL (src)
786                            | ((HOST_WIDE_INT) (-1)
787                               << GET_MODE_BITSIZE (GET_MODE (x))));
788 #endif
789
790           reg_nonzero_bits[REGNO (x)]
791             |= nonzero_bits (src, nonzero_bits_mode);
792           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
793           if (reg_sign_bit_copies[REGNO (x)] == 0
794               || reg_sign_bit_copies[REGNO (x)] > num)
795             reg_sign_bit_copies[REGNO (x)] = num;
796         }
797       else
798         {
799           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
800           reg_sign_bit_copies[REGNO (x)] = 1;
801         }
802     }
803 }
804 \f
805 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
806    insns that were previously combined into I3 or that will be combined
807    into the merger of INSN and I3.
808
809    Return 0 if the combination is not allowed for any reason.
810
811    If the combination is allowed, *PDEST will be set to the single 
812    destination of INSN and *PSRC to the single source, and this function
813    will return 1.  */
814
815 static int
816 can_combine_p (insn, i3, pred, succ, pdest, psrc)
817      rtx insn;
818      rtx i3;
819      rtx pred, succ;
820      rtx *pdest, *psrc;
821 {
822   int i;
823   rtx set = 0, src, dest;
824   rtx p, link;
825   int all_adjacent = (succ ? (next_active_insn (insn) == succ
826                               && next_active_insn (succ) == i3)
827                       : next_active_insn (insn) == i3);
828
829   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
830      or a PARALLEL consisting of such a SET and CLOBBERs. 
831
832      If INSN has CLOBBER parallel parts, ignore them for our processing.
833      By definition, these happen during the execution of the insn.  When it
834      is merged with another insn, all bets are off.  If they are, in fact,
835      needed and aren't also supplied in I3, they may be added by
836      recog_for_combine.  Otherwise, it won't match. 
837
838      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
839      note.
840
841      Get the source and destination of INSN.  If more than one, can't 
842      combine.  */
843      
844   if (GET_CODE (PATTERN (insn)) == SET)
845     set = PATTERN (insn);
846   else if (GET_CODE (PATTERN (insn)) == PARALLEL
847            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
848     {
849       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
850         {
851           rtx elt = XVECEXP (PATTERN (insn), 0, i);
852
853           switch (GET_CODE (elt))
854             {
855               /* We can ignore CLOBBERs.  */
856             case CLOBBER:
857               break;
858
859             case SET:
860               /* Ignore SETs whose result isn't used but not those that
861                  have side-effects.  */
862               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
863                   && ! side_effects_p (elt))
864                 break;
865
866               /* If we have already found a SET, this is a second one and
867                  so we cannot combine with this insn.  */
868               if (set)
869                 return 0;
870
871               set = elt;
872               break;
873
874             default:
875               /* Anything else means we can't combine.  */
876               return 0;
877             }
878         }
879
880       if (set == 0
881           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
882              so don't do anything with it.  */
883           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
884         return 0;
885     }
886   else
887     return 0;
888
889   if (set == 0)
890     return 0;
891
892   set = expand_field_assignment (set);
893   src = SET_SRC (set), dest = SET_DEST (set);
894
895   /* Don't eliminate a store in the stack pointer.  */
896   if (dest == stack_pointer_rtx
897       /* If we couldn't eliminate a field assignment, we can't combine.  */
898       || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
899       /* Don't combine with an insn that sets a register to itself if it has
900          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
901       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
902       /* Can't merge a function call.  */
903       || GET_CODE (src) == CALL
904       /* Don't eliminate a function call argument.  */
905       || (GET_CODE (i3) == CALL_INSN
906           && (find_reg_fusage (i3, USE, dest)
907               || (GET_CODE (dest) == REG
908                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
909                   && global_regs[REGNO (dest)])))
910       /* Don't substitute into an incremented register.  */
911       || FIND_REG_INC_NOTE (i3, dest)
912       || (succ && FIND_REG_INC_NOTE (succ, dest))
913       /* Don't combine the end of a libcall into anything.  */
914       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
915       /* Make sure that DEST is not used after SUCC but before I3.  */
916       || (succ && ! all_adjacent
917           && reg_used_between_p (dest, succ, i3))
918       /* Make sure that the value that is to be substituted for the register
919          does not use any registers whose values alter in between.  However,
920          If the insns are adjacent, a use can't cross a set even though we
921          think it might (this can happen for a sequence of insns each setting
922          the same destination; reg_last_set of that register might point to
923          a NOTE).  If INSN has a REG_EQUIV note, the register is always
924          equivalent to the memory so the substitution is valid even if there
925          are intervening stores.  Also, don't move a volatile asm or
926          UNSPEC_VOLATILE across any other insns.  */
927       || (! all_adjacent
928           && (((GET_CODE (src) != MEM
929                 || ! find_reg_note (insn, REG_EQUIV, src))
930                && use_crosses_set_p (src, INSN_CUID (insn)))
931               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
932               || GET_CODE (src) == UNSPEC_VOLATILE))
933       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
934          better register allocation by not doing the combine.  */
935       || find_reg_note (i3, REG_NO_CONFLICT, dest)
936       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
937       /* Don't combine across a CALL_INSN, because that would possibly
938          change whether the life span of some REGs crosses calls or not,
939          and it is a pain to update that information.
940          Exception: if source is a constant, moving it later can't hurt.
941          Accept that special case, because it helps -fforce-addr a lot.  */
942       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
943     return 0;
944
945   /* DEST must either be a REG or CC0.  */
946   if (GET_CODE (dest) == REG)
947     {
948       /* If register alignment is being enforced for multi-word items in all
949          cases except for parameters, it is possible to have a register copy
950          insn referencing a hard register that is not allowed to contain the
951          mode being copied and which would not be valid as an operand of most
952          insns.  Eliminate this problem by not combining with such an insn.
953
954          Also, on some machines we don't want to extend the life of a hard
955          register.
956
957          This is the same test done in can_combine except that we don't test
958          if SRC is a CALL operation to permit a hard register with
959          SMALL_REGISTER_CLASSES, and that we have to take all_adjacent
960          into account.  */
961
962       if (GET_CODE (src) == REG
963           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
964                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
965               /* Don't extend the life of a hard register unless it is
966                  user variable (if we have few registers) or it can't
967                  fit into the desired register (meaning something special
968                  is going on).
969                  Also avoid substituting a return register into I3, because
970                  reload can't handle a conflict with constraints of other
971                  inputs.  */
972               || (REGNO (src) < FIRST_PSEUDO_REGISTER
973                   && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
974 #ifdef SMALL_REGISTER_CLASSES
975                       || (SMALL_REGISTER_CLASSES
976                           && ((! all_adjacent && ! REG_USERVAR_P (src))
977                               || (FUNCTION_VALUE_REGNO_P (REGNO (src))
978                                   && ! REG_USERVAR_P (src))))
979 #endif
980                       ))))
981         return 0;
982     }
983   else if (GET_CODE (dest) != CC0)
984     return 0;
985
986   /* Don't substitute for a register intended as a clobberable operand.
987      Similarly, don't substitute an expression containing a register that
988      will be clobbered in I3.  */
989   if (GET_CODE (PATTERN (i3)) == PARALLEL)
990     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
991       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
992           && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
993                                        src)
994               || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
995         return 0;
996
997   /* If INSN contains anything volatile, or is an `asm' (whether volatile
998      or not), reject, unless nothing volatile comes between it and I3,
999      with the exception of SUCC.  */
1000
1001   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1002     for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1003       if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1004           && p != succ && volatile_refs_p (PATTERN (p)))
1005         return 0;
1006
1007   /* If there are any volatile insns between INSN and I3, reject, because
1008      they might affect machine state.  */
1009
1010   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1011     if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1012         && p != succ && volatile_insn_p (PATTERN (p)))
1013       return 0;
1014
1015   /* If INSN or I2 contains an autoincrement or autodecrement,
1016      make sure that register is not used between there and I3,
1017      and not already used in I3 either.
1018      Also insist that I3 not be a jump; if it were one
1019      and the incremented register were spilled, we would lose.  */
1020
1021 #ifdef AUTO_INC_DEC
1022   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1023     if (REG_NOTE_KIND (link) == REG_INC
1024         && (GET_CODE (i3) == JUMP_INSN
1025             || reg_used_between_p (XEXP (link, 0), insn, i3)
1026             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1027       return 0;
1028 #endif
1029
1030 #ifdef HAVE_cc0
1031   /* Don't combine an insn that follows a CC0-setting insn.
1032      An insn that uses CC0 must not be separated from the one that sets it.
1033      We do, however, allow I2 to follow a CC0-setting insn if that insn
1034      is passed as I1; in that case it will be deleted also.
1035      We also allow combining in this case if all the insns are adjacent
1036      because that would leave the two CC0 insns adjacent as well.
1037      It would be more logical to test whether CC0 occurs inside I1 or I2,
1038      but that would be much slower, and this ought to be equivalent.  */
1039
1040   p = prev_nonnote_insn (insn);
1041   if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1042       && ! all_adjacent)
1043     return 0;
1044 #endif
1045
1046   /* If we get here, we have passed all the tests and the combination is
1047      to be allowed.  */
1048
1049   *pdest = dest;
1050   *psrc = src;
1051
1052   return 1;
1053 }
1054 \f
1055 /* LOC is the location within I3 that contains its pattern or the component
1056    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1057
1058    One problem is if I3 modifies its output, as opposed to replacing it
1059    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1060    so would produce an insn that is not equivalent to the original insns.
1061
1062    Consider:
1063
1064          (set (reg:DI 101) (reg:DI 100))
1065          (set (subreg:SI (reg:DI 101) 0) <foo>)
1066
1067    This is NOT equivalent to:
1068
1069          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1070                     (set (reg:DI 101) (reg:DI 100))])
1071
1072    Not only does this modify 100 (in which case it might still be valid
1073    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100. 
1074
1075    We can also run into a problem if I2 sets a register that I1
1076    uses and I1 gets directly substituted into I3 (not via I2).  In that
1077    case, we would be getting the wrong value of I2DEST into I3, so we
1078    must reject the combination.  This case occurs when I2 and I1 both
1079    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1080    If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1081    of a SET must prevent combination from occurring.
1082
1083    On machines where SMALL_REGISTER_CLASSES is defined, we don't combine
1084    if the destination of a SET is a hard register that isn't a user
1085    variable.
1086
1087    Before doing the above check, we first try to expand a field assignment
1088    into a set of logical operations.
1089
1090    If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1091    we place a register that is both set and used within I3.  If more than one
1092    such register is detected, we fail.
1093
1094    Return 1 if the combination is valid, zero otherwise.  */
1095
1096 static int
1097 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1098      rtx i3;
1099      rtx *loc;
1100      rtx i2dest;
1101      rtx i1dest;
1102      int i1_not_in_src;
1103      rtx *pi3dest_killed;
1104 {
1105   rtx x = *loc;
1106
1107   if (GET_CODE (x) == SET)
1108     {
1109       rtx set = expand_field_assignment (x);
1110       rtx dest = SET_DEST (set);
1111       rtx src = SET_SRC (set);
1112       rtx inner_dest = dest, inner_src = src;
1113
1114       SUBST (*loc, set);
1115
1116       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1117              || GET_CODE (inner_dest) == SUBREG
1118              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1119         inner_dest = XEXP (inner_dest, 0);
1120
1121   /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1122      was added.  */
1123 #if 0
1124       while (GET_CODE (inner_src) == STRICT_LOW_PART
1125              || GET_CODE (inner_src) == SUBREG
1126              || GET_CODE (inner_src) == ZERO_EXTRACT)
1127         inner_src = XEXP (inner_src, 0);
1128
1129       /* If it is better that two different modes keep two different pseudos,
1130          avoid combining them.  This avoids producing the following pattern
1131          on a 386:
1132           (set (subreg:SI (reg/v:QI 21) 0)
1133                (lshiftrt:SI (reg/v:SI 20)
1134                    (const_int 24)))
1135          If that were made, reload could not handle the pair of
1136          reg 20/21, since it would try to get any GENERAL_REGS
1137          but some of them don't handle QImode.  */
1138
1139       if (rtx_equal_p (inner_src, i2dest)
1140           && GET_CODE (inner_dest) == REG
1141           && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1142         return 0;
1143 #endif
1144
1145       /* Check for the case where I3 modifies its output, as
1146          discussed above.  */
1147       if ((inner_dest != dest
1148            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1149                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1150           /* This is the same test done in can_combine_p except that we
1151              allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
1152              CALL operation.
1153              Moreover, we can't test all_adjacent; we don't have to, since
1154              this instruction will stay in place, thus we are not considering
1155              to increase the lifetime of INNER_DEST.  */
1156           || (GET_CODE (inner_dest) == REG
1157               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1158               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1159                                         GET_MODE (inner_dest))
1160 #ifdef SMALL_REGISTER_CLASSES
1161                  || (SMALL_REGISTER_CLASSES
1162                      && GET_CODE (src) != CALL && ! REG_USERVAR_P (inner_dest)
1163                      && FUNCTION_VALUE_REGNO_P (REGNO (inner_dest)))
1164 #endif
1165                   ))
1166           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1167         return 0;
1168
1169       /* If DEST is used in I3, it is being killed in this insn,
1170          so record that for later. 
1171          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1172          STACK_POINTER_REGNUM, since these are always considered to be
1173          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1174       if (pi3dest_killed && GET_CODE (dest) == REG
1175           && reg_referenced_p (dest, PATTERN (i3))
1176           && REGNO (dest) != FRAME_POINTER_REGNUM
1177 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1178           && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1179 #endif
1180 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1181           && (REGNO (dest) != ARG_POINTER_REGNUM
1182               || ! fixed_regs [REGNO (dest)])
1183 #endif
1184           && REGNO (dest) != STACK_POINTER_REGNUM)
1185         {
1186           if (*pi3dest_killed)
1187             return 0;
1188
1189           *pi3dest_killed = dest;
1190         }
1191     }
1192
1193   else if (GET_CODE (x) == PARALLEL)
1194     {
1195       int i;
1196
1197       for (i = 0; i < XVECLEN (x, 0); i++)
1198         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1199                                 i1_not_in_src, pi3dest_killed))
1200           return 0;
1201     }
1202
1203   return 1;
1204 }
1205 \f
1206 /* Try to combine the insns I1 and I2 into I3.
1207    Here I1 and I2 appear earlier than I3.
1208    I1 can be zero; then we combine just I2 into I3.
1209  
1210    It we are combining three insns and the resulting insn is not recognized,
1211    try splitting it into two insns.  If that happens, I2 and I3 are retained
1212    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1213    are pseudo-deleted.
1214
1215    Return 0 if the combination does not work.  Then nothing is changed. 
1216    If we did the combination, return the insn at which combine should
1217    resume scanning.  */
1218
1219 static rtx
1220 try_combine (i3, i2, i1)
1221      register rtx i3, i2, i1;
1222 {
1223   /* New patterns for I3 and I3, respectively.  */
1224   rtx newpat, newi2pat = 0;
1225   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1226   int added_sets_1, added_sets_2;
1227   /* Total number of SETs to put into I3.  */
1228   int total_sets;
1229   /* Nonzero is I2's body now appears in I3.  */
1230   int i2_is_used;
1231   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1232   int insn_code_number, i2_code_number, other_code_number;
1233   /* Contains I3 if the destination of I3 is used in its source, which means
1234      that the old life of I3 is being killed.  If that usage is placed into
1235      I2 and not in I3, a REG_DEAD note must be made.  */
1236   rtx i3dest_killed = 0;
1237   /* SET_DEST and SET_SRC of I2 and I1.  */
1238   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1239   /* PATTERN (I2), or a copy of it in certain cases.  */
1240   rtx i2pat;
1241   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1242   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1243   int i1_feeds_i3 = 0;
1244   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1245   rtx new_i3_notes, new_i2_notes;
1246   /* Notes that we substituted I3 into I2 instead of the normal case.  */
1247   int i3_subst_into_i2 = 0;
1248   /* Notes that I1, I2 or I3 is a MULT operation.  */
1249   int have_mult = 0;
1250   /* Number of clobbers of SCRATCH we had to add.  */
1251   int i3_scratches = 0, i2_scratches = 0, other_scratches = 0;
1252
1253   int maxreg;
1254   rtx temp;
1255   register rtx link;
1256   int i;
1257
1258   /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1259      This can occur when flow deletes an insn that it has merged into an
1260      auto-increment address.  We also can't do anything if I3 has a
1261      REG_LIBCALL note since we don't want to disrupt the contiguity of a
1262      libcall.  */
1263
1264   if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1265       || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1266       || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
1267       || find_reg_note (i3, REG_LIBCALL, NULL_RTX))
1268     return 0;
1269
1270   combine_attempts++;
1271
1272   undobuf.undos = undobuf.previous_undos = 0;
1273   undobuf.other_insn = 0;
1274
1275   /* Save the current high-water-mark so we can free storage if we didn't
1276      accept this combination.  */
1277   undobuf.storage = (char *) oballoc (0);
1278
1279   /* Reset the hard register usage information.  */
1280   CLEAR_HARD_REG_SET (newpat_used_regs);
1281
1282   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1283      code below, set I1 to be the earlier of the two insns.  */
1284   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1285     temp = i1, i1 = i2, i2 = temp;
1286
1287   added_links_insn = 0;
1288
1289   /* First check for one important special-case that the code below will
1290      not handle.  Namely, the case where I1 is zero, I2 has multiple sets,
1291      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1292      we may be able to replace that destination with the destination of I3.
1293      This occurs in the common code where we compute both a quotient and
1294      remainder into a structure, in which case we want to do the computation
1295      directly into the structure to avoid register-register copies.
1296
1297      We make very conservative checks below and only try to handle the
1298      most common cases of this.  For example, we only handle the case
1299      where I2 and I3 are adjacent to avoid making difficult register
1300      usage tests.  */
1301
1302   if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1303       && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1304       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1305 #ifdef SMALL_REGISTER_CLASSES
1306       && (! SMALL_REGISTER_CLASSES
1307           || GET_CODE (SET_DEST (PATTERN (i3))) != REG
1308           || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1309           || REG_USERVAR_P (SET_DEST (PATTERN (i3))))
1310 #endif
1311       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1312       && GET_CODE (PATTERN (i2)) == PARALLEL
1313       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1314       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1315          below would need to check what is inside (and reg_overlap_mentioned_p
1316          doesn't support those codes anyway).  Don't allow those destinations;
1317          the resulting insn isn't likely to be recognized anyway.  */
1318       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1319       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1320       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1321                                     SET_DEST (PATTERN (i3)))
1322       && next_real_insn (i2) == i3)
1323     {
1324       rtx p2 = PATTERN (i2);
1325
1326       /* Make sure that the destination of I3,
1327          which we are going to substitute into one output of I2,
1328          is not used within another output of I2.  We must avoid making this:
1329          (parallel [(set (mem (reg 69)) ...)
1330                     (set (reg 69) ...)])
1331          which is not well-defined as to order of actions.
1332          (Besides, reload can't handle output reloads for this.)
1333
1334          The problem can also happen if the dest of I3 is a memory ref,
1335          if another dest in I2 is an indirect memory ref.  */
1336       for (i = 0; i < XVECLEN (p2, 0); i++)
1337         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1338              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1339             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1340                                         SET_DEST (XVECEXP (p2, 0, i))))
1341           break;
1342
1343       if (i == XVECLEN (p2, 0))
1344         for (i = 0; i < XVECLEN (p2, 0); i++)
1345           if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1346             {
1347               combine_merges++;
1348
1349               subst_insn = i3;
1350               subst_low_cuid = INSN_CUID (i2);
1351
1352               added_sets_2 = added_sets_1 = 0;
1353               i2dest = SET_SRC (PATTERN (i3));
1354
1355               /* Replace the dest in I2 with our dest and make the resulting
1356                  insn the new pattern for I3.  Then skip to where we
1357                  validate the pattern.  Everything was set up above.  */
1358               SUBST (SET_DEST (XVECEXP (p2, 0, i)), 
1359                      SET_DEST (PATTERN (i3)));
1360
1361               newpat = p2;
1362               i3_subst_into_i2 = 1;
1363               goto validate_replacement;
1364             }
1365     }
1366
1367 #ifndef HAVE_cc0
1368   /* If we have no I1 and I2 looks like:
1369         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1370                    (set Y OP)])
1371      make up a dummy I1 that is
1372         (set Y OP)
1373      and change I2 to be
1374         (set (reg:CC X) (compare:CC Y (const_int 0)))
1375
1376      (We can ignore any trailing CLOBBERs.)
1377
1378      This undoes a previous combination and allows us to match a branch-and-
1379      decrement insn.  */
1380
1381   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1382       && XVECLEN (PATTERN (i2), 0) >= 2
1383       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1384       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1385           == MODE_CC)
1386       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1387       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1388       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1389       && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1390       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1391                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1392     {
1393       for (i =  XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1394         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1395           break;
1396
1397       if (i == 1)
1398         {
1399           /* We make I1 with the same INSN_UID as I2.  This gives it
1400              the same INSN_CUID for value tracking.  Our fake I1 will
1401              never appear in the insn stream so giving it the same INSN_UID
1402              as I2 will not cause a problem.  */
1403
1404           subst_prev_insn = i1
1405             = gen_rtx (INSN, VOIDmode, INSN_UID (i2), 0, i2,
1406                        XVECEXP (PATTERN (i2), 0, 1), -1, 0, 0);
1407
1408           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1409           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1410                  SET_DEST (PATTERN (i1)));
1411         }
1412     }
1413 #endif
1414
1415   /* Verify that I2 and I1 are valid for combining.  */
1416   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1417       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1418     {
1419       undo_all ();
1420       return 0;
1421     }
1422
1423   /* Record whether I2DEST is used in I2SRC and similarly for the other
1424      cases.  Knowing this will help in register status updating below.  */
1425   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1426   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1427   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1428
1429   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
1430      in I2SRC.  */
1431   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1432
1433   /* Ensure that I3's pattern can be the destination of combines.  */
1434   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1435                           i1 && i2dest_in_i1src && i1_feeds_i3,
1436                           &i3dest_killed))
1437     {
1438       undo_all ();
1439       return 0;
1440     }
1441
1442   /* See if any of the insns is a MULT operation.  Unless one is, we will
1443      reject a combination that is, since it must be slower.  Be conservative
1444      here.  */
1445   if (GET_CODE (i2src) == MULT
1446       || (i1 != 0 && GET_CODE (i1src) == MULT)
1447       || (GET_CODE (PATTERN (i3)) == SET
1448           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1449     have_mult = 1;
1450
1451   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1452      We used to do this EXCEPT in one case: I3 has a post-inc in an
1453      output operand.  However, that exception can give rise to insns like
1454         mov r3,(r3)+
1455      which is a famous insn on the PDP-11 where the value of r3 used as the
1456      source was model-dependent.  Avoid this sort of thing.  */
1457
1458 #if 0
1459   if (!(GET_CODE (PATTERN (i3)) == SET
1460         && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1461         && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1462         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1463             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1464     /* It's not the exception.  */
1465 #endif
1466 #ifdef AUTO_INC_DEC
1467     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1468       if (REG_NOTE_KIND (link) == REG_INC
1469           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1470               || (i1 != 0
1471                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1472         {
1473           undo_all ();
1474           return 0;
1475         }
1476 #endif
1477
1478   /* See if the SETs in I1 or I2 need to be kept around in the merged
1479      instruction: whenever the value set there is still needed past I3.
1480      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1481
1482      For the SET in I1, we have two cases:  If I1 and I2 independently
1483      feed into I3, the set in I1 needs to be kept around if I1DEST dies
1484      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
1485      in I1 needs to be kept around unless I1DEST dies or is set in either
1486      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
1487      I1DEST.  If so, we know I1 feeds into I2.  */
1488
1489   added_sets_2 = ! dead_or_set_p (i3, i2dest);
1490
1491   added_sets_1
1492     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1493                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1494
1495   /* If the set in I2 needs to be kept around, we must make a copy of
1496      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1497      PATTERN (I2), we are only substituting for the original I1DEST, not into
1498      an already-substituted copy.  This also prevents making self-referential
1499      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1500      I2DEST.  */
1501
1502   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1503            ? gen_rtx (SET, VOIDmode, i2dest, i2src)
1504            : PATTERN (i2));
1505
1506   if (added_sets_2)
1507     i2pat = copy_rtx (i2pat);
1508
1509   combine_merges++;
1510
1511   /* Substitute in the latest insn for the regs set by the earlier ones.  */
1512
1513   maxreg = max_reg_num ();
1514
1515   subst_insn = i3;
1516
1517   /* It is possible that the source of I2 or I1 may be performing an
1518      unneeded operation, such as a ZERO_EXTEND of something that is known
1519      to have the high part zero.  Handle that case by letting subst look at
1520      the innermost one of them.
1521
1522      Another way to do this would be to have a function that tries to
1523      simplify a single insn instead of merging two or more insns.  We don't
1524      do this because of the potential of infinite loops and because
1525      of the potential extra memory required.  However, doing it the way
1526      we are is a bit of a kludge and doesn't catch all cases.
1527
1528      But only do this if -fexpensive-optimizations since it slows things down
1529      and doesn't usually win.  */
1530
1531   if (flag_expensive_optimizations)
1532     {
1533       /* Pass pc_rtx so no substitutions are done, just simplifications.
1534          The cases that we are interested in here do not involve the few
1535          cases were is_replaced is checked.  */
1536       if (i1)
1537         {
1538           subst_low_cuid = INSN_CUID (i1);
1539           i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1540         }
1541       else
1542         {
1543           subst_low_cuid = INSN_CUID (i2);
1544           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1545         }
1546
1547       undobuf.previous_undos = undobuf.undos;
1548     }
1549
1550 #ifndef HAVE_cc0
1551   /* Many machines that don't use CC0 have insns that can both perform an
1552      arithmetic operation and set the condition code.  These operations will
1553      be represented as a PARALLEL with the first element of the vector
1554      being a COMPARE of an arithmetic operation with the constant zero.
1555      The second element of the vector will set some pseudo to the result
1556      of the same arithmetic operation.  If we simplify the COMPARE, we won't
1557      match such a pattern and so will generate an extra insn.   Here we test
1558      for this case, where both the comparison and the operation result are
1559      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1560      I2SRC.  Later we will make the PARALLEL that contains I2.  */
1561
1562   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1563       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1564       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1565       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1566     {
1567       rtx *cc_use;
1568       enum machine_mode compare_mode;
1569
1570       newpat = PATTERN (i3);
1571       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1572
1573       i2_is_used = 1;
1574
1575 #ifdef EXTRA_CC_MODES
1576       /* See if a COMPARE with the operand we substituted in should be done
1577          with the mode that is currently being used.  If not, do the same
1578          processing we do in `subst' for a SET; namely, if the destination
1579          is used only once, try to replace it with a register of the proper
1580          mode and also replace the COMPARE.  */
1581       if (undobuf.other_insn == 0
1582           && (cc_use = find_single_use (SET_DEST (newpat), i3,
1583                                         &undobuf.other_insn))
1584           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1585                                               i2src, const0_rtx))
1586               != GET_MODE (SET_DEST (newpat))))
1587         {
1588           int regno = REGNO (SET_DEST (newpat));
1589           rtx new_dest = gen_rtx (REG, compare_mode, regno);
1590
1591           if (regno < FIRST_PSEUDO_REGISTER
1592               || (reg_n_sets[regno] == 1 && ! added_sets_2
1593                   && ! REG_USERVAR_P (SET_DEST (newpat))))
1594             {
1595               if (regno >= FIRST_PSEUDO_REGISTER)
1596                 SUBST (regno_reg_rtx[regno], new_dest);
1597
1598               SUBST (SET_DEST (newpat), new_dest);
1599               SUBST (XEXP (*cc_use, 0), new_dest);
1600               SUBST (SET_SRC (newpat),
1601                      gen_rtx_combine (COMPARE, compare_mode,
1602                                       i2src, const0_rtx));
1603             }
1604           else
1605             undobuf.other_insn = 0;
1606         }
1607 #endif    
1608     }
1609   else
1610 #endif
1611     {
1612       n_occurrences = 0;                /* `subst' counts here */
1613
1614       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1615          need to make a unique copy of I2SRC each time we substitute it
1616          to avoid self-referential rtl.  */
1617
1618       subst_low_cuid = INSN_CUID (i2);
1619       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1620                       ! i1_feeds_i3 && i1dest_in_i1src);
1621       undobuf.previous_undos = undobuf.undos;
1622
1623       /* Record whether i2's body now appears within i3's body.  */
1624       i2_is_used = n_occurrences;
1625     }
1626
1627   /* If we already got a failure, don't try to do more.  Otherwise,
1628      try to substitute in I1 if we have it.  */
1629
1630   if (i1 && GET_CODE (newpat) != CLOBBER)
1631     {
1632       /* Before we can do this substitution, we must redo the test done
1633          above (see detailed comments there) that ensures  that I1DEST
1634          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
1635
1636       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1637                               0, NULL_PTR))
1638         {
1639           undo_all ();
1640           return 0;
1641         }
1642
1643       n_occurrences = 0;
1644       subst_low_cuid = INSN_CUID (i1);
1645       newpat = subst (newpat, i1dest, i1src, 0, 0);
1646       undobuf.previous_undos = undobuf.undos;
1647     }
1648
1649   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
1650      to count all the ways that I2SRC and I1SRC can be used.  */
1651   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1652        && i2_is_used + added_sets_2 > 1)
1653       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1654           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1655               > 1))
1656       /* Fail if we tried to make a new register (we used to abort, but there's
1657          really no reason to).  */
1658       || max_reg_num () != maxreg
1659       /* Fail if we couldn't do something and have a CLOBBER.  */
1660       || GET_CODE (newpat) == CLOBBER
1661       /* Fail if this new pattern is a MULT and we didn't have one before
1662          at the outer level.  */
1663       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1664           && ! have_mult))
1665     {
1666       undo_all ();
1667       return 0;
1668     }
1669
1670   /* If the actions of the earlier insns must be kept
1671      in addition to substituting them into the latest one,
1672      we must make a new PARALLEL for the latest insn
1673      to hold additional the SETs.  */
1674
1675   if (added_sets_1 || added_sets_2)
1676     {
1677       combine_extras++;
1678
1679       if (GET_CODE (newpat) == PARALLEL)
1680         {
1681           rtvec old = XVEC (newpat, 0);
1682           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1683           newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
1684           bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
1685                  sizeof (old->elem[0]) * old->num_elem);
1686         }
1687       else
1688         {
1689           rtx old = newpat;
1690           total_sets = 1 + added_sets_1 + added_sets_2;
1691           newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
1692           XVECEXP (newpat, 0, 0) = old;
1693         }
1694
1695      if (added_sets_1)
1696        XVECEXP (newpat, 0, --total_sets)
1697          = (GET_CODE (PATTERN (i1)) == PARALLEL
1698             ? gen_rtx (SET, VOIDmode, i1dest, i1src) : PATTERN (i1));
1699
1700      if (added_sets_2)
1701         {
1702           /* If there is no I1, use I2's body as is.  We used to also not do
1703              the subst call below if I2 was substituted into I3,
1704              but that could lose a simplification.  */
1705           if (i1 == 0)
1706             XVECEXP (newpat, 0, --total_sets) = i2pat;
1707           else
1708             /* See comment where i2pat is assigned.  */
1709             XVECEXP (newpat, 0, --total_sets)
1710               = subst (i2pat, i1dest, i1src, 0, 0);
1711         }
1712     }
1713
1714   /* We come here when we are replacing a destination in I2 with the
1715      destination of I3.  */
1716  validate_replacement:
1717
1718   /* Note which hard regs this insn has as inputs.  */
1719   mark_used_regs_combine (newpat);
1720
1721   /* Is the result of combination a valid instruction?  */
1722   insn_code_number
1723     = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1724
1725   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1726      the second SET's destination is a register that is unused.  In that case,
1727      we just need the first SET.   This can occur when simplifying a divmod
1728      insn.  We *must* test for this case here because the code below that
1729      splits two independent SETs doesn't handle this case correctly when it
1730      updates the register status.  Also check the case where the first
1731      SET's destination is unused.  That would not cause incorrect code, but
1732      does cause an unneeded insn to remain.  */
1733
1734   if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1735       && XVECLEN (newpat, 0) == 2
1736       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1737       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1738       && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1739       && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1740       && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1741       && asm_noperands (newpat) < 0)
1742     {
1743       newpat = XVECEXP (newpat, 0, 0);
1744       insn_code_number
1745         = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1746     }
1747
1748   else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1749            && XVECLEN (newpat, 0) == 2
1750            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1751            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1752            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1753            && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1754            && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1755            && asm_noperands (newpat) < 0)
1756     {
1757       newpat = XVECEXP (newpat, 0, 1);
1758       insn_code_number
1759         = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1760     }
1761
1762   /* If we were combining three insns and the result is a simple SET
1763      with no ASM_OPERANDS that wasn't recognized, try to split it into two
1764      insns.  There are two ways to do this.  It can be split using a 
1765      machine-specific method (like when you have an addition of a large
1766      constant) or by combine in the function find_split_point.  */
1767
1768   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1769       && asm_noperands (newpat) < 0)
1770     {
1771       rtx m_split, *split;
1772       rtx ni2dest = i2dest;
1773
1774       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
1775          use I2DEST as a scratch register will help.  In the latter case,
1776          convert I2DEST to the mode of the source of NEWPAT if we can.  */
1777
1778       m_split = split_insns (newpat, i3);
1779
1780       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1781          inputs of NEWPAT.  */
1782
1783       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1784          possible to try that as a scratch reg.  This would require adding
1785          more code to make it work though.  */
1786
1787       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
1788         {
1789           /* If I2DEST is a hard register or the only use of a pseudo,
1790              we can change its mode.  */
1791           if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
1792               && GET_MODE (SET_DEST (newpat)) != VOIDmode
1793               && GET_CODE (i2dest) == REG
1794               && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1795                   || (reg_n_sets[REGNO (i2dest)] == 1 && ! added_sets_2
1796                       && ! REG_USERVAR_P (i2dest))))
1797             ni2dest = gen_rtx (REG, GET_MODE (SET_DEST (newpat)),
1798                                REGNO (i2dest));
1799
1800           m_split = split_insns (gen_rtx (PARALLEL, VOIDmode,
1801                                           gen_rtvec (2, newpat,
1802                                                      gen_rtx (CLOBBER,
1803                                                               VOIDmode,
1804                                                               ni2dest))),
1805                                  i3);
1806         }
1807
1808       if (m_split && GET_CODE (m_split) == SEQUENCE
1809           && XVECLEN (m_split, 0) == 2
1810           && (next_real_insn (i2) == i3
1811               || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1812                                       INSN_CUID (i2))))
1813         {
1814           rtx i2set, i3set;
1815           rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
1816           newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
1817
1818           i3set = single_set (XVECEXP (m_split, 0, 1));
1819           i2set = single_set (XVECEXP (m_split, 0, 0));
1820
1821           /* In case we changed the mode of I2DEST, replace it in the
1822              pseudo-register table here.  We can't do it above in case this
1823              code doesn't get executed and we do a split the other way.  */
1824
1825           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1826             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1827
1828           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes,
1829                                               &i2_scratches);
1830
1831           /* If I2 or I3 has multiple SETs, we won't know how to track
1832              register status, so don't use these insns.  If I2's destination
1833              is used between I2 and I3, we also can't use these insns.  */
1834
1835           if (i2_code_number >= 0 && i2set && i3set
1836               && (next_real_insn (i2) == i3
1837                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
1838             insn_code_number = recog_for_combine (&newi3pat, i3, &new_i3_notes,
1839                                                   &i3_scratches); 
1840           if (insn_code_number >= 0)
1841             newpat = newi3pat;
1842
1843           /* It is possible that both insns now set the destination of I3.
1844              If so, we must show an extra use of it.  */
1845
1846           if (insn_code_number >= 0)
1847             {
1848               rtx new_i3_dest = SET_DEST (i3set);
1849               rtx new_i2_dest = SET_DEST (i2set);
1850
1851               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
1852                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
1853                      || GET_CODE (new_i3_dest) == SUBREG)
1854                 new_i3_dest = XEXP (new_i3_dest, 0);
1855
1856               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
1857                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
1858                      || GET_CODE (new_i2_dest) == SUBREG)
1859                 new_i2_dest = XEXP (new_i2_dest, 0);
1860
1861               if (GET_CODE (new_i3_dest) == REG
1862                   && GET_CODE (new_i2_dest) == REG
1863                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
1864                 reg_n_sets[REGNO (new_i2_dest)]++;
1865             }
1866         }
1867
1868       /* If we can split it and use I2DEST, go ahead and see if that
1869          helps things be recognized.  Verify that none of the registers
1870          are set between I2 and I3.  */
1871       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
1872 #ifdef HAVE_cc0
1873           && GET_CODE (i2dest) == REG
1874 #endif
1875           /* We need I2DEST in the proper mode.  If it is a hard register
1876              or the only use of a pseudo, we can change its mode.  */
1877           && (GET_MODE (*split) == GET_MODE (i2dest)
1878               || GET_MODE (*split) == VOIDmode
1879               || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1880               || (reg_n_sets[REGNO (i2dest)] == 1 && ! added_sets_2
1881                   && ! REG_USERVAR_P (i2dest)))
1882           && (next_real_insn (i2) == i3
1883               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
1884           /* We can't overwrite I2DEST if its value is still used by
1885              NEWPAT.  */
1886           && ! reg_referenced_p (i2dest, newpat))
1887         {
1888           rtx newdest = i2dest;
1889           enum rtx_code split_code = GET_CODE (*split);
1890           enum machine_mode split_mode = GET_MODE (*split);
1891
1892           /* Get NEWDEST as a register in the proper mode.  We have already
1893              validated that we can do this.  */
1894           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
1895             {
1896               newdest = gen_rtx (REG, split_mode, REGNO (i2dest));
1897
1898               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1899                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
1900             }
1901
1902           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
1903              an ASHIFT.  This can occur if it was inside a PLUS and hence
1904              appeared to be a memory address.  This is a kludge.  */
1905           if (split_code == MULT
1906               && GET_CODE (XEXP (*split, 1)) == CONST_INT
1907               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
1908             {
1909               SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
1910                                               XEXP (*split, 0), GEN_INT (i)));
1911               /* Update split_code because we may not have a multiply
1912                  anymore.  */
1913               split_code = GET_CODE (*split);
1914             }
1915
1916 #ifdef INSN_SCHEDULING
1917           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
1918              be written as a ZERO_EXTEND.  */
1919           if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
1920             SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
1921                                             XEXP (*split, 0)));
1922 #endif
1923
1924           newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
1925           SUBST (*split, newdest);
1926           i2_code_number
1927             = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
1928
1929           /* If the split point was a MULT and we didn't have one before,
1930              don't use one now.  */
1931           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
1932             insn_code_number
1933               = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1934         }
1935     }
1936
1937   /* Check for a case where we loaded from memory in a narrow mode and
1938      then sign extended it, but we need both registers.  In that case,
1939      we have a PARALLEL with both loads from the same memory location.
1940      We can split this into a load from memory followed by a register-register
1941      copy.  This saves at least one insn, more if register allocation can
1942      eliminate the copy.
1943
1944      We cannot do this if the destination of the second assignment is
1945      a register that we have already assumed is zero-extended.  Similarly
1946      for a SUBREG of such a register.  */
1947
1948   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
1949            && GET_CODE (newpat) == PARALLEL
1950            && XVECLEN (newpat, 0) == 2
1951            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1952            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
1953            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1954            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
1955                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
1956            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
1957                                    INSN_CUID (i2))
1958            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
1959            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
1960            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
1961                  (GET_CODE (temp) == REG
1962                   && reg_nonzero_bits[REGNO (temp)] != 0
1963                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
1964                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
1965                   && (reg_nonzero_bits[REGNO (temp)]
1966                       != GET_MODE_MASK (word_mode))))
1967            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
1968                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
1969                      (GET_CODE (temp) == REG
1970                       && reg_nonzero_bits[REGNO (temp)] != 0
1971                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
1972                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
1973                       && (reg_nonzero_bits[REGNO (temp)]
1974                           != GET_MODE_MASK (word_mode)))))
1975            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
1976                                          SET_SRC (XVECEXP (newpat, 0, 1)))
1977            && ! find_reg_note (i3, REG_UNUSED,
1978                                SET_DEST (XVECEXP (newpat, 0, 0))))
1979     {
1980       rtx ni2dest;
1981
1982       newi2pat = XVECEXP (newpat, 0, 0);
1983       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
1984       newpat = XVECEXP (newpat, 0, 1);
1985       SUBST (SET_SRC (newpat),
1986              gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
1987       i2_code_number
1988         = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
1989
1990       if (i2_code_number >= 0)
1991         insn_code_number
1992           = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1993
1994       if (insn_code_number >= 0)
1995         {
1996           rtx insn;
1997           rtx link;
1998
1999           /* If we will be able to accept this, we have made a change to the
2000              destination of I3.  This can invalidate a LOG_LINKS pointing
2001              to I3.  No other part of combine.c makes such a transformation.
2002
2003              The new I3 will have a destination that was previously the
2004              destination of I1 or I2 and which was used in i2 or I3.  Call
2005              distribute_links to make a LOG_LINK from the next use of
2006              that destination.  */
2007
2008           PATTERN (i3) = newpat;
2009           distribute_links (gen_rtx (INSN_LIST, VOIDmode, i3, NULL_RTX));
2010
2011           /* I3 now uses what used to be its destination and which is
2012              now I2's destination.  That means we need a LOG_LINK from
2013              I3 to I2.  But we used to have one, so we still will.
2014
2015              However, some later insn might be using I2's dest and have
2016              a LOG_LINK pointing at I3.  We must remove this link.
2017              The simplest way to remove the link is to point it at I1,
2018              which we know will be a NOTE.  */
2019
2020           for (insn = NEXT_INSN (i3);
2021                insn && (this_basic_block == n_basic_blocks - 1
2022                         || insn != basic_block_head[this_basic_block + 1]);
2023                insn = NEXT_INSN (insn))
2024             {
2025               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
2026                   && reg_referenced_p (ni2dest, PATTERN (insn)))
2027                 {
2028                   for (link = LOG_LINKS (insn); link;
2029                        link = XEXP (link, 1))
2030                     if (XEXP (link, 0) == i3)
2031                       XEXP (link, 0) = i1;
2032
2033                   break;
2034                 }
2035             }
2036         }
2037     }
2038             
2039   /* Similarly, check for a case where we have a PARALLEL of two independent
2040      SETs but we started with three insns.  In this case, we can do the sets
2041      as two separate insns.  This case occurs when some SET allows two
2042      other insns to combine, but the destination of that SET is still live.  */
2043
2044   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2045            && GET_CODE (newpat) == PARALLEL
2046            && XVECLEN (newpat, 0) == 2
2047            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2048            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2049            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2050            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2051            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2052            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2053            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2054                                    INSN_CUID (i2))
2055            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
2056            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2057            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2058            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2059                                   XVECEXP (newpat, 0, 0))
2060            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2061                                   XVECEXP (newpat, 0, 1)))
2062     {
2063       newi2pat = XVECEXP (newpat, 0, 1);
2064       newpat = XVECEXP (newpat, 0, 0);
2065
2066       i2_code_number
2067         = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2068
2069       if (i2_code_number >= 0)
2070         insn_code_number
2071           = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
2072     }
2073
2074   /* If it still isn't recognized, fail and change things back the way they
2075      were.  */
2076   if ((insn_code_number < 0
2077        /* Is the result a reasonable ASM_OPERANDS?  */
2078        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2079     {
2080       undo_all ();
2081       return 0;
2082     }
2083
2084   /* If we had to change another insn, make sure it is valid also.  */
2085   if (undobuf.other_insn)
2086     {
2087       rtx other_pat = PATTERN (undobuf.other_insn);
2088       rtx new_other_notes;
2089       rtx note, next;
2090
2091       CLEAR_HARD_REG_SET (newpat_used_regs);
2092
2093       other_code_number
2094         = recog_for_combine (&other_pat, undobuf.other_insn,
2095                              &new_other_notes, &other_scratches);
2096
2097       if (other_code_number < 0 && ! check_asm_operands (other_pat))
2098         {
2099           undo_all ();
2100           return 0;
2101         }
2102
2103       PATTERN (undobuf.other_insn) = other_pat;
2104
2105       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2106          are still valid.  Then add any non-duplicate notes added by
2107          recog_for_combine.  */
2108       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2109         {
2110           next = XEXP (note, 1);
2111
2112           if (REG_NOTE_KIND (note) == REG_UNUSED
2113               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2114             {
2115               if (GET_CODE (XEXP (note, 0)) == REG)
2116                 reg_n_deaths[REGNO (XEXP (note, 0))]--;
2117
2118               remove_note (undobuf.other_insn, note);
2119             }
2120         }
2121
2122       for (note = new_other_notes; note; note = XEXP (note, 1))
2123         if (GET_CODE (XEXP (note, 0)) == REG)
2124           reg_n_deaths[REGNO (XEXP (note, 0))]++;
2125
2126       distribute_notes (new_other_notes, undobuf.other_insn,
2127                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2128     }
2129
2130   /* We now know that we can do this combination.  Merge the insns and 
2131      update the status of registers and LOG_LINKS.  */
2132
2133   {
2134     rtx i3notes, i2notes, i1notes = 0;
2135     rtx i3links, i2links, i1links = 0;
2136     rtx midnotes = 0;
2137     register int regno;
2138     /* Compute which registers we expect to eliminate.  */
2139     rtx elim_i2 = (newi2pat || i2dest_in_i2src || i2dest_in_i1src
2140                    ? 0 : i2dest);
2141     rtx elim_i1 = i1 == 0 || i1dest_in_i1src ? 0 : i1dest;
2142
2143     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2144        clear them.  */
2145     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2146     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2147     if (i1)
2148       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2149
2150     /* Ensure that we do not have something that should not be shared but
2151        occurs multiple times in the new insns.  Check this by first
2152        resetting all the `used' flags and then copying anything is shared.  */
2153
2154     reset_used_flags (i3notes);
2155     reset_used_flags (i2notes);
2156     reset_used_flags (i1notes);
2157     reset_used_flags (newpat);
2158     reset_used_flags (newi2pat);
2159     if (undobuf.other_insn)
2160       reset_used_flags (PATTERN (undobuf.other_insn));
2161
2162     i3notes = copy_rtx_if_shared (i3notes);
2163     i2notes = copy_rtx_if_shared (i2notes);
2164     i1notes = copy_rtx_if_shared (i1notes);
2165     newpat = copy_rtx_if_shared (newpat);
2166     newi2pat = copy_rtx_if_shared (newi2pat);
2167     if (undobuf.other_insn)
2168       reset_used_flags (PATTERN (undobuf.other_insn));
2169
2170     INSN_CODE (i3) = insn_code_number;
2171     PATTERN (i3) = newpat;
2172     if (undobuf.other_insn)
2173       INSN_CODE (undobuf.other_insn) = other_code_number;
2174
2175     /* We had one special case above where I2 had more than one set and
2176        we replaced a destination of one of those sets with the destination
2177        of I3.  In that case, we have to update LOG_LINKS of insns later
2178        in this basic block.  Note that this (expensive) case is rare.
2179
2180        Also, in this case, we must pretend that all REG_NOTEs for I2
2181        actually came from I3, so that REG_UNUSED notes from I2 will be
2182        properly handled.  */
2183
2184     if (i3_subst_into_i2)
2185       {
2186         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2187           if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2188               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2189               && ! find_reg_note (i2, REG_UNUSED,
2190                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2191             for (temp = NEXT_INSN (i2);
2192                  temp && (this_basic_block == n_basic_blocks - 1
2193                           || basic_block_head[this_basic_block] != temp);
2194                  temp = NEXT_INSN (temp))
2195               if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2196                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2197                   if (XEXP (link, 0) == i2)
2198                     XEXP (link, 0) = i3;
2199
2200         if (i3notes)
2201           {
2202             rtx link = i3notes;
2203             while (XEXP (link, 1))
2204               link = XEXP (link, 1);
2205             XEXP (link, 1) = i2notes;
2206           }
2207         else
2208           i3notes = i2notes;
2209         i2notes = 0;
2210       }
2211
2212     LOG_LINKS (i3) = 0;
2213     REG_NOTES (i3) = 0;
2214     LOG_LINKS (i2) = 0;
2215     REG_NOTES (i2) = 0;
2216
2217     if (newi2pat)
2218       {
2219         INSN_CODE (i2) = i2_code_number;
2220         PATTERN (i2) = newi2pat;
2221       }
2222     else
2223       {
2224         PUT_CODE (i2, NOTE);
2225         NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2226         NOTE_SOURCE_FILE (i2) = 0;
2227       }
2228
2229     if (i1)
2230       {
2231         LOG_LINKS (i1) = 0;
2232         REG_NOTES (i1) = 0;
2233         PUT_CODE (i1, NOTE);
2234         NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2235         NOTE_SOURCE_FILE (i1) = 0;
2236       }
2237
2238     /* Get death notes for everything that is now used in either I3 or
2239        I2 and used to die in a previous insn.  If we built two new 
2240        patterns, move from I1 to I2 then I2 to I3 so that we get the
2241        proper movement on registers that I2 modifies.  */
2242
2243     if (newi2pat)
2244       {
2245         move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2246         move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2247       }
2248     else
2249       move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2250                    i3, &midnotes);
2251
2252     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
2253     if (i3notes)
2254       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2255                         elim_i2, elim_i1);
2256     if (i2notes)
2257       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2258                         elim_i2, elim_i1);
2259     if (i1notes)
2260       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2261                         elim_i2, elim_i1);
2262     if (midnotes)
2263       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2264                         elim_i2, elim_i1);
2265
2266     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
2267        know these are REG_UNUSED and want them to go to the desired insn,
2268        so we always pass it as i3.  We have not counted the notes in 
2269        reg_n_deaths yet, so we need to do so now.  */
2270
2271     if (newi2pat && new_i2_notes)
2272       {
2273         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2274           if (GET_CODE (XEXP (temp, 0)) == REG)
2275             reg_n_deaths[REGNO (XEXP (temp, 0))]++;
2276         
2277         distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2278       }
2279
2280     if (new_i3_notes)
2281       {
2282         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2283           if (GET_CODE (XEXP (temp, 0)) == REG)
2284             reg_n_deaths[REGNO (XEXP (temp, 0))]++;
2285         
2286         distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2287       }
2288
2289     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
2290        put a REG_DEAD note for it somewhere.  Similarly for I2 and I1.
2291        Show an additional death due to the REG_DEAD note we make here.  If
2292        we discard it in distribute_notes, we will decrement it again.  */
2293
2294     if (i3dest_killed)
2295       {
2296         if (GET_CODE (i3dest_killed) == REG)
2297           reg_n_deaths[REGNO (i3dest_killed)]++;
2298
2299         distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i3dest_killed,
2300                                    NULL_RTX),
2301                           NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2302                           NULL_RTX, NULL_RTX);
2303       }
2304
2305     /* For I2 and I1, we have to be careful.  If NEWI2PAT exists and sets
2306        I2DEST or I1DEST, the death must be somewhere before I2, not I3.  If
2307        we passed I3 in that case, it might delete I2.  */
2308
2309     if (i2dest_in_i2src)
2310       {
2311         if (GET_CODE (i2dest) == REG)
2312           reg_n_deaths[REGNO (i2dest)]++;
2313
2314         if (newi2pat && reg_set_p (i2dest, newi2pat))
2315           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
2316                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2317         else
2318           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
2319                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2320                             NULL_RTX, NULL_RTX);
2321       }
2322
2323     if (i1dest_in_i1src)
2324       {
2325         if (GET_CODE (i1dest) == REG)
2326           reg_n_deaths[REGNO (i1dest)]++;
2327
2328         if (newi2pat && reg_set_p (i1dest, newi2pat))
2329           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
2330                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2331         else
2332           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
2333                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2334                             NULL_RTX, NULL_RTX);
2335       }
2336
2337     distribute_links (i3links);
2338     distribute_links (i2links);
2339     distribute_links (i1links);
2340
2341     if (GET_CODE (i2dest) == REG)
2342       {
2343         rtx link;
2344         rtx i2_insn = 0, i2_val = 0, set;
2345
2346         /* The insn that used to set this register doesn't exist, and
2347            this life of the register may not exist either.  See if one of
2348            I3's links points to an insn that sets I2DEST.  If it does, 
2349            that is now the last known value for I2DEST. If we don't update
2350            this and I2 set the register to a value that depended on its old
2351            contents, we will get confused.  If this insn is used, thing
2352            will be set correctly in combine_instructions.  */
2353
2354         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2355           if ((set = single_set (XEXP (link, 0))) != 0
2356               && rtx_equal_p (i2dest, SET_DEST (set)))
2357             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2358
2359         record_value_for_reg (i2dest, i2_insn, i2_val);
2360
2361         /* If the reg formerly set in I2 died only once and that was in I3,
2362            zero its use count so it won't make `reload' do any work.  */
2363         if (! added_sets_2
2364             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2365             && ! i2dest_in_i2src)
2366           {
2367             regno = REGNO (i2dest);
2368             reg_n_sets[regno]--;
2369             if (reg_n_sets[regno] == 0
2370                 && ! (basic_block_live_at_start[0][regno / REGSET_ELT_BITS]
2371                       & ((REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS))))
2372               reg_n_refs[regno] = 0;
2373           }
2374       }
2375
2376     if (i1 && GET_CODE (i1dest) == REG)
2377       {
2378         rtx link;
2379         rtx i1_insn = 0, i1_val = 0, set;
2380
2381         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2382           if ((set = single_set (XEXP (link, 0))) != 0
2383               && rtx_equal_p (i1dest, SET_DEST (set)))
2384             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2385
2386         record_value_for_reg (i1dest, i1_insn, i1_val);
2387
2388         regno = REGNO (i1dest);
2389         if (! added_sets_1 && ! i1dest_in_i1src)
2390           {
2391             reg_n_sets[regno]--;
2392             if (reg_n_sets[regno] == 0
2393                 && ! (basic_block_live_at_start[0][regno / REGSET_ELT_BITS]
2394                       & ((REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS))))
2395               reg_n_refs[regno] = 0;
2396           }
2397       }
2398
2399     /* Update reg_nonzero_bits et al for any changes that may have been made
2400        to this insn.  */
2401
2402     note_stores (newpat, set_nonzero_bits_and_sign_copies);
2403     if (newi2pat)
2404       note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
2405
2406     /* If we added any (clobber (scratch)), add them to the max for a
2407        block.  This is a very pessimistic calculation, since we might
2408        have had them already and this might not be the worst block, but
2409        it's not worth doing any better.  */
2410     max_scratch += i3_scratches + i2_scratches + other_scratches;
2411
2412     /* If I3 is now an unconditional jump, ensure that it has a 
2413        BARRIER following it since it may have initially been a
2414        conditional jump.  It may also be the last nonnote insn.  */
2415
2416     if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
2417         && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2418             || GET_CODE (temp) != BARRIER))
2419       emit_barrier_after (i3);
2420   }
2421
2422   combine_successes++;
2423
2424   /* Clear this here, so that subsequent get_last_value calls are not
2425      affected.  */
2426   subst_prev_insn = NULL_RTX;
2427
2428   if (added_links_insn
2429       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2430       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2431     return added_links_insn;
2432   else
2433     return newi2pat ? i2 : i3;
2434 }
2435 \f
2436 /* Undo all the modifications recorded in undobuf.  */
2437
2438 static void
2439 undo_all ()
2440 {
2441   struct undo *undo, *next;
2442
2443   for (undo = undobuf.undos; undo; undo = next)
2444     {
2445       next = undo->next;
2446       if (undo->is_int)
2447         *undo->where.i = undo->old_contents.i;
2448       else
2449         *undo->where.r = undo->old_contents.r;
2450
2451       undo->next = undobuf.frees;
2452       undobuf.frees = undo;
2453     }
2454
2455   obfree (undobuf.storage);
2456   undobuf.undos = undobuf.previous_undos = 0;
2457
2458   /* Clear this here, so that subsequent get_last_value calls are not
2459      affected.  */
2460   subst_prev_insn = NULL_RTX;
2461 }
2462 \f
2463 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2464    where we have an arithmetic expression and return that point.  LOC will
2465    be inside INSN.
2466
2467    try_combine will call this function to see if an insn can be split into
2468    two insns.  */
2469
2470 static rtx *
2471 find_split_point (loc, insn)
2472      rtx *loc;
2473      rtx insn;
2474 {
2475   rtx x = *loc;
2476   enum rtx_code code = GET_CODE (x);
2477   rtx *split;
2478   int len = 0, pos, unsignedp;
2479   rtx inner;
2480
2481   /* First special-case some codes.  */
2482   switch (code)
2483     {
2484     case SUBREG:
2485 #ifdef INSN_SCHEDULING
2486       /* If we are making a paradoxical SUBREG invalid, it becomes a split
2487          point.  */
2488       if (GET_CODE (SUBREG_REG (x)) == MEM)
2489         return loc;
2490 #endif
2491       return find_split_point (&SUBREG_REG (x), insn);
2492
2493     case MEM:
2494 #ifdef HAVE_lo_sum
2495       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2496          using LO_SUM and HIGH.  */
2497       if (GET_CODE (XEXP (x, 0)) == CONST
2498           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2499         {
2500           SUBST (XEXP (x, 0),
2501                  gen_rtx_combine (LO_SUM, Pmode,
2502                                   gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2503                                   XEXP (x, 0)));
2504           return &XEXP (XEXP (x, 0), 0);
2505         }
2506 #endif
2507
2508       /* If we have a PLUS whose second operand is a constant and the
2509          address is not valid, perhaps will can split it up using
2510          the machine-specific way to split large constants.  We use
2511          the first pseudo-reg (one of the virtual regs) as a placeholder;
2512          it will not remain in the result.  */
2513       if (GET_CODE (XEXP (x, 0)) == PLUS
2514           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2515           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2516         {
2517           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2518           rtx seq = split_insns (gen_rtx (SET, VOIDmode, reg, XEXP (x, 0)),
2519                                  subst_insn);
2520
2521           /* This should have produced two insns, each of which sets our
2522              placeholder.  If the source of the second is a valid address,
2523              we can make put both sources together and make a split point
2524              in the middle.  */
2525
2526           if (seq && XVECLEN (seq, 0) == 2
2527               && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2528               && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2529               && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2530               && ! reg_mentioned_p (reg,
2531                                     SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2532               && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2533               && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2534               && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2535               && memory_address_p (GET_MODE (x),
2536                                    SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2537             {
2538               rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2539               rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2540
2541               /* Replace the placeholder in SRC2 with SRC1.  If we can
2542                  find where in SRC2 it was placed, that can become our
2543                  split point and we can replace this address with SRC2.
2544                  Just try two obvious places.  */
2545
2546               src2 = replace_rtx (src2, reg, src1);
2547               split = 0;
2548               if (XEXP (src2, 0) == src1)
2549                 split = &XEXP (src2, 0);
2550               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2551                        && XEXP (XEXP (src2, 0), 0) == src1)
2552                 split = &XEXP (XEXP (src2, 0), 0);
2553
2554               if (split)
2555                 {
2556                   SUBST (XEXP (x, 0), src2);
2557                   return split;
2558                 }
2559             }
2560           
2561           /* If that didn't work, perhaps the first operand is complex and
2562              needs to be computed separately, so make a split point there.
2563              This will occur on machines that just support REG + CONST
2564              and have a constant moved through some previous computation.  */
2565
2566           else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2567                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2568                          && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2569                              == 'o')))
2570             return &XEXP (XEXP (x, 0), 0);
2571         }
2572       break;
2573
2574     case SET:
2575 #ifdef HAVE_cc0
2576       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2577          ZERO_EXTRACT, the most likely reason why this doesn't match is that
2578          we need to put the operand into a register.  So split at that
2579          point.  */
2580
2581       if (SET_DEST (x) == cc0_rtx
2582           && GET_CODE (SET_SRC (x)) != COMPARE
2583           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2584           && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2585           && ! (GET_CODE (SET_SRC (x)) == SUBREG
2586                 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2587         return &SET_SRC (x);
2588 #endif
2589
2590       /* See if we can split SET_SRC as it stands.  */
2591       split = find_split_point (&SET_SRC (x), insn);
2592       if (split && split != &SET_SRC (x))
2593         return split;
2594
2595       /* See if we can split SET_DEST as it stands.  */
2596       split = find_split_point (&SET_DEST (x), insn);
2597       if (split && split != &SET_DEST (x))
2598         return split;
2599
2600       /* See if this is a bitfield assignment with everything constant.  If
2601          so, this is an IOR of an AND, so split it into that.  */
2602       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2603           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2604               <= HOST_BITS_PER_WIDE_INT)
2605           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2606           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2607           && GET_CODE (SET_SRC (x)) == CONST_INT
2608           && ((INTVAL (XEXP (SET_DEST (x), 1))
2609               + INTVAL (XEXP (SET_DEST (x), 2)))
2610               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2611           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2612         {
2613           int pos = INTVAL (XEXP (SET_DEST (x), 2));
2614           int len = INTVAL (XEXP (SET_DEST (x), 1));
2615           int src = INTVAL (SET_SRC (x));
2616           rtx dest = XEXP (SET_DEST (x), 0);
2617           enum machine_mode mode = GET_MODE (dest);
2618           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2619
2620           if (BITS_BIG_ENDIAN)
2621             pos = GET_MODE_BITSIZE (mode) - len - pos;
2622
2623           if (src == mask)
2624             SUBST (SET_SRC (x),
2625                    gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2626           else
2627             SUBST (SET_SRC (x),
2628                    gen_binary (IOR, mode,
2629                                gen_binary (AND, mode, dest, 
2630                                            GEN_INT (~ (mask << pos)
2631                                                     & GET_MODE_MASK (mode))),
2632                                GEN_INT (src << pos)));
2633
2634           SUBST (SET_DEST (x), dest);
2635
2636           split = find_split_point (&SET_SRC (x), insn);
2637           if (split && split != &SET_SRC (x))
2638             return split;
2639         }
2640
2641       /* Otherwise, see if this is an operation that we can split into two.
2642          If so, try to split that.  */
2643       code = GET_CODE (SET_SRC (x));
2644
2645       switch (code)
2646         {
2647         case AND:
2648           /* If we are AND'ing with a large constant that is only a single
2649              bit and the result is only being used in a context where we
2650              need to know if it is zero or non-zero, replace it with a bit
2651              extraction.  This will avoid the large constant, which might
2652              have taken more than one insn to make.  If the constant were
2653              not a valid argument to the AND but took only one insn to make,
2654              this is no worse, but if it took more than one insn, it will
2655              be better.  */
2656
2657           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2658               && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2659               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2660               && GET_CODE (SET_DEST (x)) == REG
2661               && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2662               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2663               && XEXP (*split, 0) == SET_DEST (x)
2664               && XEXP (*split, 1) == const0_rtx)
2665             {
2666               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2667                                                 XEXP (SET_SRC (x), 0),
2668                                                 pos, NULL_RTX, 1, 1, 0, 0);
2669               if (extraction != 0)
2670                 {
2671                   SUBST (SET_SRC (x), extraction);
2672                   return find_split_point (loc, insn);
2673                 }
2674             }
2675           break;
2676
2677         case NE:
2678           /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
2679              is known to be on, this can be converted into a NEG of a shift. */
2680           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
2681               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
2682               && 1 <= (pos = exact_log2
2683                        (nonzero_bits (XEXP (SET_SRC (x), 0),
2684                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
2685             {
2686               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
2687
2688               SUBST (SET_SRC (x),
2689                      gen_rtx_combine (NEG, mode,
2690                                       gen_rtx_combine (LSHIFTRT, mode,
2691                                                        XEXP (SET_SRC (x), 0),
2692                                                        GEN_INT (pos))));
2693
2694               split = find_split_point (&SET_SRC (x), insn);
2695               if (split && split != &SET_SRC (x))
2696                 return split;
2697             }
2698           break;
2699
2700         case SIGN_EXTEND:
2701           inner = XEXP (SET_SRC (x), 0);
2702
2703           /* We can't optimize if either mode is a partial integer
2704              mode as we don't know how many bits are significant
2705              in those modes.  */
2706           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
2707               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
2708             break;
2709
2710           pos = 0;
2711           len = GET_MODE_BITSIZE (GET_MODE (inner));
2712           unsignedp = 0;
2713           break;
2714
2715         case SIGN_EXTRACT:
2716         case ZERO_EXTRACT:
2717           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2718               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2719             {
2720               inner = XEXP (SET_SRC (x), 0);
2721               len = INTVAL (XEXP (SET_SRC (x), 1));
2722               pos = INTVAL (XEXP (SET_SRC (x), 2));
2723
2724               if (BITS_BIG_ENDIAN)
2725                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
2726               unsignedp = (code == ZERO_EXTRACT);
2727             }
2728           break;
2729         }
2730
2731       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2732         {
2733           enum machine_mode mode = GET_MODE (SET_SRC (x));
2734
2735           /* For unsigned, we have a choice of a shift followed by an
2736              AND or two shifts.  Use two shifts for field sizes where the
2737              constant might be too large.  We assume here that we can
2738              always at least get 8-bit constants in an AND insn, which is
2739              true for every current RISC.  */
2740
2741           if (unsignedp && len <= 8)
2742             {
2743               SUBST (SET_SRC (x),
2744                      gen_rtx_combine
2745                      (AND, mode,
2746                       gen_rtx_combine (LSHIFTRT, mode,
2747                                        gen_lowpart_for_combine (mode, inner),
2748                                        GEN_INT (pos)),
2749                       GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
2750
2751               split = find_split_point (&SET_SRC (x), insn);
2752               if (split && split != &SET_SRC (x))
2753                 return split;
2754             }
2755           else
2756             {
2757               SUBST (SET_SRC (x),
2758                      gen_rtx_combine
2759                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
2760                       gen_rtx_combine (ASHIFT, mode,
2761                                        gen_lowpart_for_combine (mode, inner),
2762                                        GEN_INT (GET_MODE_BITSIZE (mode)
2763                                                 - len - pos)),
2764                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
2765
2766               split = find_split_point (&SET_SRC (x), insn);
2767               if (split && split != &SET_SRC (x))
2768                 return split;
2769             }
2770         }
2771
2772       /* See if this is a simple operation with a constant as the second
2773          operand.  It might be that this constant is out of range and hence
2774          could be used as a split point.  */
2775       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2776            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2777            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2778           && CONSTANT_P (XEXP (SET_SRC (x), 1))
2779           && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2780               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2781                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2782                       == 'o'))))
2783         return &XEXP (SET_SRC (x), 1);
2784
2785       /* Finally, see if this is a simple operation with its first operand
2786          not in a register.  The operation might require this operand in a
2787          register, so return it as a split point.  We can always do this
2788          because if the first operand were another operation, we would have
2789          already found it as a split point.  */
2790       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2791            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2792            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2793            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2794           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2795         return &XEXP (SET_SRC (x), 0);
2796
2797       return 0;
2798
2799     case AND:
2800     case IOR:
2801       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2802          it is better to write this as (not (ior A B)) so we can split it.
2803          Similarly for IOR.  */
2804       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2805         {
2806           SUBST (*loc,
2807                  gen_rtx_combine (NOT, GET_MODE (x),
2808                                   gen_rtx_combine (code == IOR ? AND : IOR,
2809                                                    GET_MODE (x),
2810                                                    XEXP (XEXP (x, 0), 0),
2811                                                    XEXP (XEXP (x, 1), 0))));
2812           return find_split_point (loc, insn);
2813         }
2814
2815       /* Many RISC machines have a large set of logical insns.  If the
2816          second operand is a NOT, put it first so we will try to split the
2817          other operand first.  */
2818       if (GET_CODE (XEXP (x, 1)) == NOT)
2819         {
2820           rtx tem = XEXP (x, 0);
2821           SUBST (XEXP (x, 0), XEXP (x, 1));
2822           SUBST (XEXP (x, 1), tem);
2823         }
2824       break;
2825     }
2826
2827   /* Otherwise, select our actions depending on our rtx class.  */
2828   switch (GET_RTX_CLASS (code))
2829     {
2830     case 'b':                   /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
2831     case '3':
2832       split = find_split_point (&XEXP (x, 2), insn);
2833       if (split)
2834         return split;
2835       /* ... fall through ...  */
2836     case '2':
2837     case 'c':
2838     case '<':
2839       split = find_split_point (&XEXP (x, 1), insn);
2840       if (split)
2841         return split;
2842       /* ... fall through ...  */
2843     case '1':
2844       /* Some machines have (and (shift ...) ...) insns.  If X is not
2845          an AND, but XEXP (X, 0) is, use it as our split point.  */
2846       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
2847         return &XEXP (x, 0);
2848
2849       split = find_split_point (&XEXP (x, 0), insn);
2850       if (split)
2851         return split;
2852       return loc;
2853     }
2854
2855   /* Otherwise, we don't have a split point.  */
2856   return 0;
2857 }
2858 \f
2859 /* Throughout X, replace FROM with TO, and return the result.
2860    The result is TO if X is FROM;
2861    otherwise the result is X, but its contents may have been modified.
2862    If they were modified, a record was made in undobuf so that
2863    undo_all will (among other things) return X to its original state.
2864
2865    If the number of changes necessary is too much to record to undo,
2866    the excess changes are not made, so the result is invalid.
2867    The changes already made can still be undone.
2868    undobuf.num_undo is incremented for such changes, so by testing that
2869    the caller can tell whether the result is valid.
2870
2871    `n_occurrences' is incremented each time FROM is replaced.
2872    
2873    IN_DEST is non-zero if we are processing the SET_DEST of a SET.
2874
2875    UNIQUE_COPY is non-zero if each substitution must be unique.  We do this
2876    by copying if `n_occurrences' is non-zero.  */
2877
2878 static rtx
2879 subst (x, from, to, in_dest, unique_copy)
2880      register rtx x, from, to;
2881      int in_dest;
2882      int unique_copy;
2883 {
2884   register enum rtx_code code = GET_CODE (x);
2885   enum machine_mode op0_mode = VOIDmode;
2886   register char *fmt;
2887   register int len, i;
2888   rtx new;
2889
2890 /* Two expressions are equal if they are identical copies of a shared
2891    RTX or if they are both registers with the same register number
2892    and mode.  */
2893
2894 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
2895   ((X) == (Y)                                           \
2896    || (GET_CODE (X) == REG && GET_CODE (Y) == REG       \
2897        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
2898
2899   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
2900     {
2901       n_occurrences++;
2902       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
2903     }
2904
2905   /* If X and FROM are the same register but different modes, they will
2906      not have been seen as equal above.  However, flow.c will make a 
2907      LOG_LINKS entry for that case.  If we do nothing, we will try to
2908      rerecognize our original insn and, when it succeeds, we will
2909      delete the feeding insn, which is incorrect.
2910
2911      So force this insn not to match in this (rare) case.  */
2912   if (! in_dest && code == REG && GET_CODE (from) == REG
2913       && REGNO (x) == REGNO (from))
2914     return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
2915
2916   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
2917      of which may contain things that can be combined.  */
2918   if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
2919     return x;
2920
2921   /* It is possible to have a subexpression appear twice in the insn.
2922      Suppose that FROM is a register that appears within TO.
2923      Then, after that subexpression has been scanned once by `subst',
2924      the second time it is scanned, TO may be found.  If we were
2925      to scan TO here, we would find FROM within it and create a
2926      self-referent rtl structure which is completely wrong.  */
2927   if (COMBINE_RTX_EQUAL_P (x, to))
2928     return to;
2929
2930   len = GET_RTX_LENGTH (code);
2931   fmt = GET_RTX_FORMAT (code);
2932
2933   /* We don't need to process a SET_DEST that is a register, CC0, or PC, so
2934      set up to skip this common case.  All other cases where we want to
2935      suppress replacing something inside a SET_SRC are handled via the
2936      IN_DEST operand.  */
2937   if (code == SET
2938       && (GET_CODE (SET_DEST (x)) == REG
2939         || GET_CODE (SET_DEST (x)) == CC0
2940         || GET_CODE (SET_DEST (x)) == PC))
2941     fmt = "ie";
2942
2943   /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
2944      constant.  */
2945   if (fmt[0] == 'e')
2946     op0_mode = GET_MODE (XEXP (x, 0));
2947
2948   for (i = 0; i < len; i++)
2949     {
2950       if (fmt[i] == 'E')
2951         {
2952           register int j;
2953           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2954             {
2955               if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
2956                 {
2957                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
2958                   n_occurrences++;
2959                 }
2960               else
2961                 {
2962                   new = subst (XVECEXP (x, i, j), from, to, 0, unique_copy);
2963
2964                   /* If this substitution failed, this whole thing fails.  */
2965                   if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
2966                     return new;
2967                 }
2968
2969               SUBST (XVECEXP (x, i, j), new);
2970             }
2971         }
2972       else if (fmt[i] == 'e')
2973         {
2974           if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
2975             {
2976               /* In general, don't install a subreg involving two modes not
2977                  tieable.  It can worsen register allocation, and can even
2978                  make invalid reload insns, since the reg inside may need to
2979                  be copied from in the outside mode, and that may be invalid
2980                  if it is an fp reg copied in integer mode.
2981
2982                  We allow two exceptions to this: It is valid if it is inside
2983                  another SUBREG and the mode of that SUBREG and the mode of
2984                  the inside of TO is tieable and it is valid if X is a SET
2985                  that copies FROM to CC0.  */
2986               if (GET_CODE (to) == SUBREG
2987                   && ! MODES_TIEABLE_P (GET_MODE (to),
2988                                         GET_MODE (SUBREG_REG (to)))
2989                   && ! (code == SUBREG
2990                         && MODES_TIEABLE_P (GET_MODE (x),
2991                                             GET_MODE (SUBREG_REG (to))))
2992 #ifdef HAVE_cc0
2993                   && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
2994 #endif
2995                   )
2996                 return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
2997
2998               new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
2999               n_occurrences++;
3000             }
3001           else
3002             /* If we are in a SET_DEST, suppress most cases unless we
3003                have gone inside a MEM, in which case we want to
3004                simplify the address.  We assume here that things that
3005                are actually part of the destination have their inner
3006                parts in the first expression.  This is true for SUBREG, 
3007                STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3008                things aside from REG and MEM that should appear in a
3009                SET_DEST.  */
3010             new = subst (XEXP (x, i), from, to,
3011                          (((in_dest
3012                             && (code == SUBREG || code == STRICT_LOW_PART
3013                                 || code == ZERO_EXTRACT))
3014                            || code == SET)
3015                           && i == 0), unique_copy);
3016
3017           /* If we found that we will have to reject this combination,
3018              indicate that by returning the CLOBBER ourselves, rather than
3019              an expression containing it.  This will speed things up as
3020              well as prevent accidents where two CLOBBERs are considered
3021              to be equal, thus producing an incorrect simplification.  */
3022
3023           if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3024             return new;
3025
3026           SUBST (XEXP (x, i), new);
3027         }
3028     }
3029
3030   /* Try to simplify X.  If the simplification changed the code, it is likely
3031      that further simplification will help, so loop, but limit the number
3032      of repetitions that will be performed.  */
3033
3034   for (i = 0; i < 4; i++)
3035     {
3036       /* If X is sufficiently simple, don't bother trying to do anything
3037          with it.  */
3038       if (code != CONST_INT && code != REG && code != CLOBBER)
3039         x = simplify_rtx (x, op0_mode, i == 3, in_dest);
3040
3041       if (GET_CODE (x) == code)
3042         break;
3043
3044       code = GET_CODE (x);
3045
3046       /* We no longer know the original mode of operand 0 since we
3047          have changed the form of X)  */
3048       op0_mode = VOIDmode;
3049     }
3050
3051   return x;
3052 }
3053 \f
3054 /* Simplify X, a piece of RTL.  We just operate on the expression at the
3055    outer level; call `subst' to simplify recursively.  Return the new
3056    expression.
3057
3058    OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3059    will be the iteration even if an expression with a code different from
3060    X is returned; IN_DEST is nonzero if we are inside a SET_DEST.  */
3061
3062 static rtx
3063 simplify_rtx (x, op0_mode, last, in_dest)
3064      rtx x;
3065      enum machine_mode op0_mode;
3066      int last;
3067      int in_dest;
3068 {
3069   enum rtx_code code = GET_CODE (x);
3070   enum machine_mode mode = GET_MODE (x);
3071   rtx temp;
3072   int i;
3073
3074   /* If this is a commutative operation, put a constant last and a complex
3075      expression first.  We don't need to do this for comparisons here.  */
3076   if (GET_RTX_CLASS (code) == 'c'
3077       && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3078           || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3079               && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3080           || (GET_CODE (XEXP (x, 0)) == SUBREG
3081               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3082               && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3083     {
3084       temp = XEXP (x, 0);
3085       SUBST (XEXP (x, 0), XEXP (x, 1));
3086       SUBST (XEXP (x, 1), temp);
3087     }
3088
3089   /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3090      sign extension of a PLUS with a constant, reverse the order of the sign
3091      extension and the addition. Note that this not the same as the original
3092      code, but overflow is undefined for signed values.  Also note that the
3093      PLUS will have been partially moved "inside" the sign-extension, so that
3094      the first operand of X will really look like:
3095          (ashiftrt (plus (ashift A C4) C5) C4).
3096      We convert this to
3097          (plus (ashiftrt (ashift A C4) C2) C4)
3098      and replace the first operand of X with that expression.  Later parts
3099      of this function may simplify the expression further.
3100
3101      For example, if we start with (mult (sign_extend (plus A C1)) C2),
3102      we swap the SIGN_EXTEND and PLUS.  Later code will apply the
3103      distributive law to produce (plus (mult (sign_extend X) C1) C3).
3104
3105      We do this to simplify address expressions.  */
3106
3107   if ((code == PLUS || code == MINUS || code == MULT)
3108       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3109       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3110       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3111       && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3112       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3113       && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3114       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3115       && (temp = simplify_binary_operation (ASHIFTRT, mode,
3116                                             XEXP (XEXP (XEXP (x, 0), 0), 1),
3117                                             XEXP (XEXP (x, 0), 1))) != 0)
3118     {
3119       rtx new
3120         = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3121                                 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3122                                 INTVAL (XEXP (XEXP (x, 0), 1)));
3123
3124       new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3125                                   INTVAL (XEXP (XEXP (x, 0), 1)));
3126
3127       SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3128     }
3129
3130   /* If this is a simple operation applied to an IF_THEN_ELSE, try 
3131      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
3132      things.  Check for cases where both arms are testing the same
3133      condition.
3134
3135      Don't do anything if all operands are very simple.  */
3136
3137   if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3138         || GET_RTX_CLASS (code) == '<')
3139        && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3140             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3141                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3142                       == 'o')))
3143            || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3144                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3145                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3146                          == 'o')))))
3147       || (GET_RTX_CLASS (code) == '1'
3148           && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3149                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3150                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3151                          == 'o'))))))
3152     {
3153       rtx cond, true, false;
3154
3155       cond = if_then_else_cond (x, &true, &false);
3156       if (cond != 0
3157           /* If everything is a comparison, what we have is highly unlikely
3158              to be simpler, so don't use it.  */
3159           && ! (GET_RTX_CLASS (code) == '<'
3160                 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3161                     || GET_RTX_CLASS (GET_CODE (false)) == '<')))
3162         {
3163           rtx cop1 = const0_rtx;
3164           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3165
3166           if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3167             return x;
3168
3169           /* Simplify the alternative arms; this may collapse the true and 
3170              false arms to store-flag values.  */
3171           true = subst (true, pc_rtx, pc_rtx, 0, 0);
3172           false = subst (false, pc_rtx, pc_rtx, 0, 0);
3173
3174           /* Restarting if we generate a store-flag expression will cause
3175              us to loop.  Just drop through in this case.  */
3176
3177           /* If the result values are STORE_FLAG_VALUE and zero, we can
3178              just make the comparison operation.  */
3179           if (true == const_true_rtx && false == const0_rtx)
3180             x = gen_binary (cond_code, mode, cond, cop1);
3181           else if (true == const0_rtx && false == const_true_rtx)
3182             x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3183
3184           /* Likewise, we can make the negate of a comparison operation
3185              if the result values are - STORE_FLAG_VALUE and zero.  */
3186           else if (GET_CODE (true) == CONST_INT
3187                    && INTVAL (true) == - STORE_FLAG_VALUE
3188                    && false == const0_rtx)
3189             x = gen_unary (NEG, mode, mode,
3190                            gen_binary (cond_code, mode, cond, cop1));
3191           else if (GET_CODE (false) == CONST_INT
3192                    && INTVAL (false) == - STORE_FLAG_VALUE
3193                    && true == const0_rtx)
3194             x = gen_unary (NEG, mode, mode,
3195                            gen_binary (reverse_condition (cond_code), 
3196                                        mode, cond, cop1));
3197           else
3198             return gen_rtx (IF_THEN_ELSE, mode,
3199                             gen_binary (cond_code, VOIDmode, cond, cop1),
3200                             true, false);
3201
3202           code = GET_CODE (x);
3203           op0_mode = VOIDmode;
3204         }
3205     }
3206
3207   /* Try to fold this expression in case we have constants that weren't
3208      present before.  */
3209   temp = 0;
3210   switch (GET_RTX_CLASS (code))
3211     {
3212     case '1':
3213       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3214       break;
3215     case '<':
3216       temp = simplify_relational_operation (code, op0_mode,
3217                                             XEXP (x, 0), XEXP (x, 1));
3218 #ifdef FLOAT_STORE_FLAG_VALUE
3219       if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3220         temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3221                 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3222 #endif
3223       break;
3224     case 'c':
3225     case '2':
3226       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3227       break;
3228     case 'b':
3229     case '3':
3230       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3231                                          XEXP (x, 1), XEXP (x, 2));
3232       break;
3233     }
3234
3235   if (temp)
3236     x = temp, code = GET_CODE (temp);
3237
3238   /* First see if we can apply the inverse distributive law.  */
3239   if (code == PLUS || code == MINUS
3240       || code == AND || code == IOR || code == XOR)
3241     {
3242       x = apply_distributive_law (x);
3243       code = GET_CODE (x);
3244     }
3245
3246   /* If CODE is an associative operation not otherwise handled, see if we
3247      can associate some operands.  This can win if they are constants or
3248      if they are logically related (i.e. (a & b) & a.  */
3249   if ((code == PLUS || code == MINUS
3250        || code == MULT || code == AND || code == IOR || code == XOR
3251        || code == DIV || code == UDIV
3252        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3253       && INTEGRAL_MODE_P (mode))
3254     {
3255       if (GET_CODE (XEXP (x, 0)) == code)
3256         {
3257           rtx other = XEXP (XEXP (x, 0), 0);
3258           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3259           rtx inner_op1 = XEXP (x, 1);
3260           rtx inner;
3261           
3262           /* Make sure we pass the constant operand if any as the second
3263              one if this is a commutative operation.  */
3264           if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3265             {
3266               rtx tem = inner_op0;
3267               inner_op0 = inner_op1;
3268               inner_op1 = tem;
3269             }
3270           inner = simplify_binary_operation (code == MINUS ? PLUS
3271                                              : code == DIV ? MULT
3272                                              : code == UDIV ? MULT
3273                                              : code,
3274                                              mode, inner_op0, inner_op1);
3275
3276           /* For commutative operations, try the other pair if that one
3277              didn't simplify.  */
3278           if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3279             {
3280               other = XEXP (XEXP (x, 0), 1);
3281               inner = simplify_binary_operation (code, mode,
3282                                                  XEXP (XEXP (x, 0), 0),
3283                                                  XEXP (x, 1));
3284             }
3285
3286           if (inner)
3287             return gen_binary (code, mode, other, inner);
3288         }
3289     }
3290
3291   /* A little bit of algebraic simplification here.  */
3292   switch (code)
3293     {
3294     case MEM:
3295       /* Ensure that our address has any ASHIFTs converted to MULT in case
3296          address-recognizing predicates are called later.  */
3297       temp = make_compound_operation (XEXP (x, 0), MEM);
3298       SUBST (XEXP (x, 0), temp);
3299       break;
3300
3301     case SUBREG:
3302       /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3303          is paradoxical.  If we can't do that safely, then it becomes
3304          something nonsensical so that this combination won't take place.  */
3305
3306       if (GET_CODE (SUBREG_REG (x)) == MEM
3307           && (GET_MODE_SIZE (mode)
3308               <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3309         {
3310           rtx inner = SUBREG_REG (x);
3311           int endian_offset = 0;
3312           /* Don't change the mode of the MEM
3313              if that would change the meaning of the address.  */
3314           if (MEM_VOLATILE_P (SUBREG_REG (x))
3315               || mode_dependent_address_p (XEXP (inner, 0)))
3316             return gen_rtx (CLOBBER, mode, const0_rtx);
3317
3318           if (BYTES_BIG_ENDIAN)
3319             {
3320               if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3321                 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3322               if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3323                 endian_offset -= (UNITS_PER_WORD
3324                                   - GET_MODE_SIZE (GET_MODE (inner)));
3325             }
3326           /* Note if the plus_constant doesn't make a valid address
3327              then this combination won't be accepted.  */
3328           x = gen_rtx (MEM, mode,
3329                        plus_constant (XEXP (inner, 0),
3330                                       (SUBREG_WORD (x) * UNITS_PER_WORD
3331                                        + endian_offset)));
3332           MEM_VOLATILE_P (x) = MEM_VOLATILE_P (inner);
3333           RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3334           MEM_IN_STRUCT_P (x) = MEM_IN_STRUCT_P (inner);
3335           return x;
3336         }
3337
3338       /* If we are in a SET_DEST, these other cases can't apply.  */
3339       if (in_dest)
3340         return x;
3341
3342       /* Changing mode twice with SUBREG => just change it once,
3343          or not at all if changing back to starting mode.  */
3344       if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3345         {
3346           if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3347               && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3348             return SUBREG_REG (SUBREG_REG (x));
3349
3350           SUBST_INT (SUBREG_WORD (x),
3351                      SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3352           SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3353         }
3354
3355       /* SUBREG of a hard register => just change the register number
3356          and/or mode.  If the hard register is not valid in that mode,
3357          suppress this combination.  If the hard register is the stack,
3358          frame, or argument pointer, leave this as a SUBREG.  */
3359
3360       if (GET_CODE (SUBREG_REG (x)) == REG
3361           && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3362           && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3363 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3364           && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3365 #endif
3366 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3367           && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3368 #endif
3369           && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3370         {
3371           if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3372                                   mode))
3373             return gen_rtx (REG, mode,
3374                             REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3375           else
3376             return gen_rtx (CLOBBER, mode, const0_rtx);
3377         }
3378
3379       /* For a constant, try to pick up the part we want.  Handle a full
3380          word and low-order part.  Only do this if we are narrowing
3381          the constant; if it is being widened, we have no idea what
3382          the extra bits will have been set to.  */
3383
3384       if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3385           && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3386           && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
3387           && GET_MODE_CLASS (mode) == MODE_INT)
3388         {
3389           temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3390                                   0, op0_mode);
3391           if (temp)
3392             return temp;
3393         }
3394         
3395       /* If we want a subreg of a constant, at offset 0,
3396          take the low bits.  On a little-endian machine, that's
3397          always valid.  On a big-endian machine, it's valid
3398          only if the constant's mode fits in one word.   Note that we
3399          cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode.  */
3400       if (CONSTANT_P (SUBREG_REG (x))
3401           && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3402               || ! WORDS_BIG_ENDIAN)
3403               ? SUBREG_WORD (x) == 0
3404               : (SUBREG_WORD (x)
3405                  == ((GET_MODE_SIZE (op0_mode)
3406                       - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3407                      / UNITS_PER_WORD)))
3408           && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
3409           && (! WORDS_BIG_ENDIAN
3410               || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
3411         return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3412
3413       /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3414          since we are saying that the high bits don't matter.  */
3415       if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3416           && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3417         return SUBREG_REG (x);
3418
3419       /* Note that we cannot do any narrowing for non-constants since
3420          we might have been counting on using the fact that some bits were
3421          zero.  We now do this in the SET.  */
3422
3423       break;
3424
3425     case NOT:
3426       /* (not (plus X -1)) can become (neg X).  */
3427       if (GET_CODE (XEXP (x, 0)) == PLUS
3428           && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3429         return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3430
3431       /* Similarly, (not (neg X)) is (plus X -1).  */
3432       if (GET_CODE (XEXP (x, 0)) == NEG)
3433         return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3434                                 constm1_rtx);
3435
3436       /* (not (xor X C)) for C constant is (xor X D) with D = ~ C.  */
3437       if (GET_CODE (XEXP (x, 0)) == XOR
3438           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3439           && (temp = simplify_unary_operation (NOT, mode,
3440                                                XEXP (XEXP (x, 0), 1),
3441                                                mode)) != 0)
3442         return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3443               
3444       /* (not (ashift 1 X)) is (rotate ~1 X).  We used to do this for operands
3445          other than 1, but that is not valid.  We could do a similar
3446          simplification for (not (lshiftrt C X)) where C is just the sign bit,
3447          but this doesn't seem common enough to bother with.  */
3448       if (GET_CODE (XEXP (x, 0)) == ASHIFT
3449           && XEXP (XEXP (x, 0), 0) == const1_rtx)
3450         return gen_rtx (ROTATE, mode, gen_unary (NOT, mode, mode, const1_rtx),
3451                         XEXP (XEXP (x, 0), 1));
3452                                             
3453       if (GET_CODE (XEXP (x, 0)) == SUBREG
3454           && subreg_lowpart_p (XEXP (x, 0))
3455           && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3456               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3457           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3458           && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3459         {
3460           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3461
3462           x = gen_rtx (ROTATE, inner_mode,
3463                        gen_unary (NOT, inner_mode, inner_mode, const1_rtx),
3464                        XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3465           return gen_lowpart_for_combine (mode, x);
3466         }
3467                                             
3468       /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3469          reversing the comparison code if valid.  */
3470       if (STORE_FLAG_VALUE == -1
3471           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3472           && reversible_comparison_p (XEXP (x, 0)))
3473         return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3474                                 mode, XEXP (XEXP (x, 0), 0),
3475                                 XEXP (XEXP (x, 0), 1));
3476
3477       /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3478          is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3479          perform the above simplification.  */
3480
3481       if (STORE_FLAG_VALUE == -1
3482           && XEXP (x, 1) == const1_rtx
3483           && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3484           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3485           && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3486         return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3487
3488       /* Apply De Morgan's laws to reduce number of patterns for machines
3489          with negating logical insns (and-not, nand, etc.).  If result has
3490          only one NOT, put it first, since that is how the patterns are
3491          coded.  */
3492
3493       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3494         {
3495          rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3496
3497          if (GET_CODE (in1) == NOT)
3498            in1 = XEXP (in1, 0);
3499          else
3500            in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3501
3502          if (GET_CODE (in2) == NOT)
3503            in2 = XEXP (in2, 0);
3504          else if (GET_CODE (in2) == CONST_INT
3505                   && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3506            in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
3507          else
3508            in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3509
3510          if (GET_CODE (in2) == NOT)
3511            {
3512              rtx tem = in2;
3513              in2 = in1; in1 = tem;
3514            }
3515
3516          return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3517                                  mode, in1, in2);
3518        } 
3519       break;
3520
3521     case NEG:
3522       /* (neg (plus X 1)) can become (not X).  */
3523       if (GET_CODE (XEXP (x, 0)) == PLUS
3524           && XEXP (XEXP (x, 0), 1) == const1_rtx)
3525         return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3526
3527       /* Similarly, (neg (not X)) is (plus X 1).  */
3528       if (GET_CODE (XEXP (x, 0)) == NOT)
3529         return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3530
3531       /* (neg (minus X Y)) can become (minus Y X).  */
3532       if (GET_CODE (XEXP (x, 0)) == MINUS
3533           && (! FLOAT_MODE_P (mode)
3534               /* x-y != -(y-x) with IEEE floating point.  */
3535               || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3536               || flag_fast_math))
3537         return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3538                            XEXP (XEXP (x, 0), 0));
3539
3540       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1.  */
3541       if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3542           && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3543         return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3544
3545       /* NEG commutes with ASHIFT since it is multiplication.  Only do this
3546          if we can then eliminate the NEG (e.g.,
3547          if the operand is a constant).  */
3548
3549       if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3550         {
3551           temp = simplify_unary_operation (NEG, mode,
3552                                            XEXP (XEXP (x, 0), 0), mode);
3553           if (temp)
3554             {
3555               SUBST (XEXP (XEXP (x, 0), 0), temp);
3556               return XEXP (x, 0);
3557             }
3558         }
3559
3560       temp = expand_compound_operation (XEXP (x, 0));
3561
3562       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3563          replaced by (lshiftrt X C).  This will convert
3564          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
3565
3566       if (GET_CODE (temp) == ASHIFTRT
3567           && GET_CODE (XEXP (temp, 1)) == CONST_INT
3568           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3569         return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3570                                      INTVAL (XEXP (temp, 1)));
3571
3572       /* If X has only a single bit that might be nonzero, say, bit I, convert
3573          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3574          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
3575          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
3576          or a SUBREG of one since we'd be making the expression more
3577          complex if it was just a register.  */
3578
3579       if (GET_CODE (temp) != REG
3580           && ! (GET_CODE (temp) == SUBREG
3581                 && GET_CODE (SUBREG_REG (temp)) == REG)
3582           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3583         {
3584           rtx temp1 = simplify_shift_const
3585             (NULL_RTX, ASHIFTRT, mode,
3586              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3587                                    GET_MODE_BITSIZE (mode) - 1 - i),
3588              GET_MODE_BITSIZE (mode) - 1 - i);
3589
3590           /* If all we did was surround TEMP with the two shifts, we
3591              haven't improved anything, so don't use it.  Otherwise,
3592              we are better off with TEMP1.  */
3593           if (GET_CODE (temp1) != ASHIFTRT
3594               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3595               || XEXP (XEXP (temp1, 0), 0) != temp)
3596             return temp1;
3597         }
3598       break;
3599
3600     case TRUNCATE:
3601       /* We can't handle truncation to a partial integer mode here
3602          because we don't know the real bitsize of the partial
3603          integer mode.  */
3604       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3605         break;
3606
3607       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3608         SUBST (XEXP (x, 0),
3609                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3610                               GET_MODE_MASK (mode), NULL_RTX, 0));
3611
3612       /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI.  */
3613       if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3614            || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3615           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3616         return XEXP (XEXP (x, 0), 0);
3617
3618       /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3619          (OP:SI foo:SI) if OP is NEG or ABS.  */
3620       if ((GET_CODE (XEXP (x, 0)) == ABS
3621            || GET_CODE (XEXP (x, 0)) == NEG)
3622           && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3623               || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3624           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3625         return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3626                           XEXP (XEXP (XEXP (x, 0), 0), 0));
3627
3628       /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3629          (truncate:SI x).  */
3630       if (GET_CODE (XEXP (x, 0)) == SUBREG
3631           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3632           && subreg_lowpart_p (XEXP (x, 0)))
3633         return SUBREG_REG (XEXP (x, 0));
3634
3635       /* If we know that the value is already truncated, we can
3636          replace the TRUNCATE with a SUBREG.  */
3637       if (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) <= HOST_BITS_PER_WIDE_INT
3638           && (nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3639               &~ GET_MODE_MASK (mode)) == 0)
3640         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3641
3642       /* A truncate of a comparison can be replaced with a subreg if
3643          STORE_FLAG_VALUE permits.  This is like the previous test,
3644          but it works even if the comparison is done in a mode larger
3645          than HOST_BITS_PER_WIDE_INT.  */
3646       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3647           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3648           && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0)
3649         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3650
3651       /* Similarly, a truncate of a register whose value is a
3652          comparison can be replaced with a subreg if STORE_FLAG_VALUE
3653          permits.  */
3654       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3655           && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0
3656           && (temp = get_last_value (XEXP (x, 0)))
3657           && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3658         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3659
3660       break;
3661
3662     case FLOAT_TRUNCATE:
3663       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
3664       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3665           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3666         return XEXP (XEXP (x, 0), 0);
3667
3668       /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3669          (OP:SF foo:SF) if OP is NEG or ABS.  */
3670       if ((GET_CODE (XEXP (x, 0)) == ABS
3671            || GET_CODE (XEXP (x, 0)) == NEG)
3672           && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3673           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3674         return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3675                           XEXP (XEXP (XEXP (x, 0), 0), 0));
3676
3677       /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3678          is (float_truncate:SF x).  */
3679       if (GET_CODE (XEXP (x, 0)) == SUBREG
3680           && subreg_lowpart_p (XEXP (x, 0))
3681           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3682         return SUBREG_REG (XEXP (x, 0));
3683       break;  
3684
3685 #ifdef HAVE_cc0
3686     case COMPARE:
3687       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3688          using cc0, in which case we want to leave it as a COMPARE
3689          so we can distinguish it from a register-register-copy.  */
3690       if (XEXP (x, 1) == const0_rtx)
3691         return XEXP (x, 0);
3692
3693       /* In IEEE floating point, x-0 is not the same as x.  */
3694       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3695            || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3696            || flag_fast_math)
3697           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3698         return XEXP (x, 0);
3699       break;
3700 #endif
3701
3702     case CONST:
3703       /* (const (const X)) can become (const X).  Do it this way rather than
3704          returning the inner CONST since CONST can be shared with a
3705          REG_EQUAL note.  */
3706       if (GET_CODE (XEXP (x, 0)) == CONST)
3707         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3708       break;
3709
3710 #ifdef HAVE_lo_sum
3711     case LO_SUM:
3712       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
3713          can add in an offset.  find_split_point will split this address up
3714          again if it doesn't match.  */
3715       if (GET_CODE (XEXP (x, 0)) == HIGH
3716           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3717         return XEXP (x, 1);
3718       break;
3719 #endif
3720
3721     case PLUS:
3722       /* If we have (plus (plus (A const) B)), associate it so that CONST is
3723          outermost.  That's because that's the way indexed addresses are
3724          supposed to appear.  This code used to check many more cases, but
3725          they are now checked elsewhere.  */
3726       if (GET_CODE (XEXP (x, 0)) == PLUS
3727           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3728         return gen_binary (PLUS, mode,
3729                            gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3730                                        XEXP (x, 1)),
3731                            XEXP (XEXP (x, 0), 1));
3732
3733       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3734          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3735          bit-field and can be replaced by either a sign_extend or a
3736          sign_extract.  The `and' may be a zero_extend.  */
3737       if (GET_CODE (XEXP (x, 0)) == XOR
3738           && GET_CODE (XEXP (x, 1)) == CONST_INT
3739           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3740           && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
3741           && (i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
3742           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3743           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3744                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3745                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
3746                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
3747               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3748                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3749                       == i + 1))))
3750         return simplify_shift_const
3751           (NULL_RTX, ASHIFTRT, mode,
3752            simplify_shift_const (NULL_RTX, ASHIFT, mode,
3753                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
3754                                  GET_MODE_BITSIZE (mode) - (i + 1)),
3755            GET_MODE_BITSIZE (mode) - (i + 1));
3756
3757       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3758          C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3759          is 1.  This produces better code than the alternative immediately
3760          below.  */
3761       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3762           && reversible_comparison_p (XEXP (x, 0))
3763           && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3764               || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
3765         return
3766           gen_unary (NEG, mode, mode,
3767                      gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3768                                  mode, XEXP (XEXP (x, 0), 0),
3769                                  XEXP (XEXP (x, 0), 1)));
3770
3771       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
3772          can become (ashiftrt (ashift (xor x 1) C) C) where C is
3773          the bitsize of the mode - 1.  This allows simplification of
3774          "a = (b & 8) == 0;"  */
3775       if (XEXP (x, 1) == constm1_rtx
3776           && GET_CODE (XEXP (x, 0)) != REG
3777           && ! (GET_CODE (XEXP (x,0)) == SUBREG
3778                 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
3779           && nonzero_bits (XEXP (x, 0), mode) == 1)
3780         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
3781            simplify_shift_const (NULL_RTX, ASHIFT, mode,
3782                                  gen_rtx_combine (XOR, mode,
3783                                                   XEXP (x, 0), const1_rtx),
3784                                  GET_MODE_BITSIZE (mode) - 1),
3785            GET_MODE_BITSIZE (mode) - 1);
3786
3787       /* If we are adding two things that have no bits in common, convert
3788          the addition into an IOR.  This will often be further simplified,
3789          for example in cases like ((a & 1) + (a & 2)), which can
3790          become a & 3.  */
3791
3792       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3793           && (nonzero_bits (XEXP (x, 0), mode)
3794               & nonzero_bits (XEXP (x, 1), mode)) == 0)
3795         return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
3796       break;
3797
3798     case MINUS:
3799       /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3800          by reversing the comparison code if valid.  */
3801       if (STORE_FLAG_VALUE == 1
3802           && XEXP (x, 0) == const1_rtx
3803           && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
3804           && reversible_comparison_p (XEXP (x, 1)))
3805         return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
3806                            mode, XEXP (XEXP (x, 1), 0),
3807                                 XEXP (XEXP (x, 1), 1));
3808
3809       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
3810          (and <foo> (const_int pow2-1))  */
3811       if (GET_CODE (XEXP (x, 1)) == AND
3812           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3813           && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
3814           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3815         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
3816                                        - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
3817
3818       /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
3819          integers.  */
3820       if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
3821         return gen_binary (MINUS, mode,
3822                            gen_binary (MINUS, mode, XEXP (x, 0),
3823                                        XEXP (XEXP (x, 1), 0)),
3824                            XEXP (XEXP (x, 1), 1));
3825       break;
3826
3827     case MULT:
3828       /* If we have (mult (plus A B) C), apply the distributive law and then
3829          the inverse distributive law to see if things simplify.  This
3830          occurs mostly in addresses, often when unrolling loops.  */
3831
3832       if (GET_CODE (XEXP (x, 0)) == PLUS)
3833         {
3834           x = apply_distributive_law
3835             (gen_binary (PLUS, mode,
3836                          gen_binary (MULT, mode,
3837                                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
3838                          gen_binary (MULT, mode,
3839                                      XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
3840
3841           if (GET_CODE (x) != MULT)
3842             return x;
3843         }
3844       break;
3845
3846     case UDIV:
3847       /* If this is a divide by a power of two, treat it as a shift if
3848          its first operand is a shift.  */
3849       if (GET_CODE (XEXP (x, 1)) == CONST_INT
3850           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
3851           && (GET_CODE (XEXP (x, 0)) == ASHIFT
3852               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
3853               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
3854               || GET_CODE (XEXP (x, 0)) == ROTATE
3855               || GET_CODE (XEXP (x, 0)) == ROTATERT))
3856         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
3857       break;
3858
3859     case EQ:  case NE:
3860     case GT:  case GTU:  case GE:  case GEU:
3861     case LT:  case LTU:  case LE:  case LEU:
3862       /* If the first operand is a condition code, we can't do anything
3863          with it.  */
3864       if (GET_CODE (XEXP (x, 0)) == COMPARE
3865           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
3866 #ifdef HAVE_cc0
3867               && XEXP (x, 0) != cc0_rtx
3868 #endif
3869                ))
3870         {
3871           rtx op0 = XEXP (x, 0);
3872           rtx op1 = XEXP (x, 1);
3873           enum rtx_code new_code;
3874
3875           if (GET_CODE (op0) == COMPARE)
3876             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
3877
3878           /* Simplify our comparison, if possible.  */
3879           new_code = simplify_comparison (code, &op0, &op1);
3880
3881           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
3882              if only the low-order bit is possibly nonzero in X (such as when
3883              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
3884              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
3885              known to be either 0 or -1, NE becomes a NEG and EQ becomes
3886              (plus X 1).
3887
3888              Remove any ZERO_EXTRACT we made when thinking this was a
3889              comparison.  It may now be simpler to use, e.g., an AND.  If a
3890              ZERO_EXTRACT is indeed appropriate, it will be placed back by
3891              the call to make_compound_operation in the SET case.  */
3892
3893           if (STORE_FLAG_VALUE == 1
3894               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3895               && op1 == const0_rtx && nonzero_bits (op0, mode) == 1)
3896             return gen_lowpart_for_combine (mode,
3897                                             expand_compound_operation (op0));
3898
3899           else if (STORE_FLAG_VALUE == 1
3900                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3901                    && op1 == const0_rtx
3902                    && (num_sign_bit_copies (op0, mode)
3903                        == GET_MODE_BITSIZE (mode)))
3904             {
3905               op0 = expand_compound_operation (op0);
3906               return gen_unary (NEG, mode, mode,
3907                                 gen_lowpart_for_combine (mode, op0));
3908             }
3909
3910           else if (STORE_FLAG_VALUE == 1
3911                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3912                    && op1 == const0_rtx
3913                    && nonzero_bits (op0, mode) == 1)
3914             {
3915               op0 = expand_compound_operation (op0);
3916               return gen_binary (XOR, mode,
3917                                  gen_lowpart_for_combine (mode, op0),
3918                                  const1_rtx);
3919             }
3920
3921           else if (STORE_FLAG_VALUE == 1
3922                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3923                    && op1 == const0_rtx
3924                    && (num_sign_bit_copies (op0, mode)
3925                        == GET_MODE_BITSIZE (mode)))
3926             {
3927               op0 = expand_compound_operation (op0);
3928               return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
3929             }
3930
3931           /* If STORE_FLAG_VALUE is -1, we have cases similar to
3932              those above.  */
3933           if (STORE_FLAG_VALUE == -1
3934               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3935               && op1 == const0_rtx
3936               && (num_sign_bit_copies (op0, mode)
3937                   == GET_MODE_BITSIZE (mode)))
3938             return gen_lowpart_for_combine (mode,
3939                                             expand_compound_operation (op0));
3940
3941           else if (STORE_FLAG_VALUE == -1
3942                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3943                    && op1 == const0_rtx
3944                    && nonzero_bits (op0, mode) == 1)
3945             {
3946               op0 = expand_compound_operation (op0);
3947               return gen_unary (NEG, mode, mode,
3948                                 gen_lowpart_for_combine (mode, op0));
3949             }
3950
3951           else if (STORE_FLAG_VALUE == -1
3952                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3953                    && op1 == const0_rtx
3954                    && (num_sign_bit_copies (op0, mode)
3955                        == GET_MODE_BITSIZE (mode)))
3956             {
3957               op0 = expand_compound_operation (op0);
3958               return gen_unary (NOT, mode, mode,
3959                                 gen_lowpart_for_combine (mode, op0));
3960             }
3961
3962           /* If X is 0/1, (eq X 0) is X-1.  */
3963           else if (STORE_FLAG_VALUE == -1
3964                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3965                    && op1 == const0_rtx
3966                    && nonzero_bits (op0, mode) == 1)
3967             {
3968               op0 = expand_compound_operation (op0);
3969               return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
3970             }
3971
3972           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
3973              one bit that might be nonzero, we can convert (ne x 0) to
3974              (ashift x c) where C puts the bit in the sign bit.  Remove any
3975              AND with STORE_FLAG_VALUE when we are done, since we are only
3976              going to test the sign bit.  */
3977           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3978               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3979               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
3980                   == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
3981               && op1 == const0_rtx
3982               && mode == GET_MODE (op0)
3983               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
3984             {
3985               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3986                                         expand_compound_operation (op0),
3987                                         GET_MODE_BITSIZE (mode) - 1 - i);
3988               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
3989                 return XEXP (x, 0);
3990               else
3991                 return x;
3992             }
3993
3994           /* If the code changed, return a whole new comparison.  */
3995           if (new_code != code)
3996             return gen_rtx_combine (new_code, mode, op0, op1);
3997
3998           /* Otherwise, keep this operation, but maybe change its operands.  
3999              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
4000           SUBST (XEXP (x, 0), op0);
4001           SUBST (XEXP (x, 1), op1);
4002         }
4003       break;
4004           
4005     case IF_THEN_ELSE:
4006       return simplify_if_then_else (x);
4007
4008     case ZERO_EXTRACT:
4009     case SIGN_EXTRACT:
4010     case ZERO_EXTEND:
4011     case SIGN_EXTEND:
4012       /* If we are processing SET_DEST, we are done.  */
4013       if (in_dest)
4014         return x;
4015
4016       return expand_compound_operation (x);
4017
4018     case SET:
4019       return simplify_set (x);
4020
4021     case AND:
4022     case IOR:
4023     case XOR:
4024       return simplify_logical (x, last);
4025
4026     case ABS:
4027       /* (abs (neg <foo>)) -> (abs <foo>) */
4028       if (GET_CODE (XEXP (x, 0)) == NEG)
4029         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4030
4031       /* If operand is something known to be positive, ignore the ABS.  */
4032       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4033           || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4034                <= HOST_BITS_PER_WIDE_INT)
4035               && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4036                    & ((HOST_WIDE_INT) 1
4037                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4038                   == 0)))
4039         return XEXP (x, 0);
4040
4041
4042       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
4043       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4044         return gen_rtx_combine (NEG, mode, XEXP (x, 0));
4045
4046       break;
4047
4048     case FFS:
4049       /* (ffs (*_extend <X>)) = (ffs <X>) */
4050       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4051           || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4052         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4053       break;
4054
4055     case FLOAT:
4056       /* (float (sign_extend <X>)) = (float <X>).  */
4057       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4058         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4059       break;
4060
4061     case ASHIFT:
4062     case LSHIFTRT:
4063     case ASHIFTRT:
4064     case ROTATE:
4065     case ROTATERT:
4066       /* If this is a shift by a constant amount, simplify it.  */
4067       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4068         return simplify_shift_const (x, code, mode, XEXP (x, 0), 
4069                                      INTVAL (XEXP (x, 1)));
4070
4071 #ifdef SHIFT_COUNT_TRUNCATED
4072       else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4073         SUBST (XEXP (x, 1),
4074                force_to_mode (XEXP (x, 1), GET_MODE (x),
4075                               ((HOST_WIDE_INT) 1 
4076                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4077                               - 1,
4078                               NULL_RTX, 0));
4079 #endif
4080
4081       break;
4082     }
4083
4084   return x;
4085 }
4086 \f
4087 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
4088
4089 static rtx
4090 simplify_if_then_else (x)
4091      rtx x;
4092 {
4093   enum machine_mode mode = GET_MODE (x);
4094   rtx cond = XEXP (x, 0);
4095   rtx true = XEXP (x, 1);
4096   rtx false = XEXP (x, 2);
4097   enum rtx_code true_code = GET_CODE (cond);
4098   int comparison_p = GET_RTX_CLASS (true_code) == '<';
4099   rtx temp;
4100   int i;
4101
4102   /* Simplify storing of the truth value.  */
4103   if (comparison_p && true == const_true_rtx && false == const0_rtx)
4104     return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4105       
4106   /* Also when the truth value has to be reversed.  */
4107   if (comparison_p && reversible_comparison_p (cond)
4108       && true == const0_rtx && false == const_true_rtx)
4109     return gen_binary (reverse_condition (true_code),
4110                        mode, XEXP (cond, 0), XEXP (cond, 1));
4111
4112   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4113      in it is being compared against certain values.  Get the true and false
4114      comparisons and see if that says anything about the value of each arm.  */
4115
4116   if (comparison_p && reversible_comparison_p (cond)
4117       && GET_CODE (XEXP (cond, 0)) == REG)
4118     {
4119       HOST_WIDE_INT nzb;
4120       rtx from = XEXP (cond, 0);
4121       enum rtx_code false_code = reverse_condition (true_code);
4122       rtx true_val = XEXP (cond, 1);
4123       rtx false_val = true_val;
4124       int swapped = 0;
4125
4126       /* If FALSE_CODE is EQ, swap the codes and arms.  */
4127
4128       if (false_code == EQ)
4129         {
4130           swapped = 1, true_code = EQ, false_code = NE;
4131           temp = true, true = false, false = temp;
4132         }
4133
4134       /* If we are comparing against zero and the expression being tested has
4135          only a single bit that might be nonzero, that is its value when it is
4136          not equal to zero.  Similarly if it is known to be -1 or 0.  */
4137
4138       if (true_code == EQ && true_val == const0_rtx
4139           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4140         false_code = EQ, false_val = GEN_INT (nzb);
4141       else if (true_code == EQ && true_val == const0_rtx
4142                && (num_sign_bit_copies (from, GET_MODE (from))
4143                    == GET_MODE_BITSIZE (GET_MODE (from))))
4144         false_code = EQ, false_val = constm1_rtx;
4145
4146       /* Now simplify an arm if we know the value of the register in the
4147          branch and it is used in the arm.  Be careful due to the potential
4148          of locally-shared RTL.  */
4149
4150       if (reg_mentioned_p (from, true))
4151         true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4152                       pc_rtx, pc_rtx, 0, 0);
4153       if (reg_mentioned_p (from, false))
4154         false = subst (known_cond (copy_rtx (false), false_code,
4155                                    from, false_val),
4156                        pc_rtx, pc_rtx, 0, 0);
4157
4158       SUBST (XEXP (x, 1), swapped ? false : true);
4159       SUBST (XEXP (x, 2), swapped ? true : false);
4160
4161       true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4162     }
4163
4164   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4165      reversed, do so to avoid needing two sets of patterns for
4166      subtract-and-branch insns.  Similarly if we have a constant in the true
4167      arm, the false arm is the same as the first operand of the comparison, or
4168      the false arm is more complicated than the true arm.  */
4169
4170   if (comparison_p && reversible_comparison_p (cond)
4171       && (true == pc_rtx 
4172           || (CONSTANT_P (true)
4173               && GET_CODE (false) != CONST_INT && false != pc_rtx)
4174           || true == const0_rtx
4175           || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4176               && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4177           || (GET_CODE (true) == SUBREG
4178               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4179               && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4180           || reg_mentioned_p (true, false)
4181           || rtx_equal_p (false, XEXP (cond, 0))))
4182     {
4183       true_code = reverse_condition (true_code);
4184       SUBST (XEXP (x, 0),
4185              gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4186                          XEXP (cond, 1)));
4187
4188       SUBST (XEXP (x, 1), false);
4189       SUBST (XEXP (x, 2), true);
4190
4191       temp = true, true = false, false = temp, cond = XEXP (x, 0);
4192
4193       /* It is possible that the conditional has been simplified out.  */
4194       true_code = GET_CODE (cond);
4195       comparison_p = GET_RTX_CLASS (true_code) == '<';
4196     }
4197
4198   /* If the two arms are identical, we don't need the comparison.  */
4199
4200   if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4201     return true;
4202
4203   /* Convert a == b ? b : a to "a".  */
4204   if (true_code == EQ && ! side_effects_p (cond)
4205       && rtx_equal_p (XEXP (cond, 0), false)
4206       && rtx_equal_p (XEXP (cond, 1), true))
4207     return false;
4208   else if (true_code == NE && ! side_effects_p (cond)
4209            && rtx_equal_p (XEXP (cond, 0), true)
4210            && rtx_equal_p (XEXP (cond, 1), false))
4211     return true;
4212
4213   /* Look for cases where we have (abs x) or (neg (abs X)).  */
4214
4215   if (GET_MODE_CLASS (mode) == MODE_INT
4216       && GET_CODE (false) == NEG
4217       && rtx_equal_p (true, XEXP (false, 0))
4218       && comparison_p
4219       && rtx_equal_p (true, XEXP (cond, 0))
4220       && ! side_effects_p (true))
4221     switch (true_code)
4222       {
4223       case GT:
4224       case GE:
4225         return gen_unary (ABS, mode, mode, true);
4226       case LT:
4227       case LE:
4228         return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
4229       }
4230
4231   /* Look for MIN or MAX.  */
4232
4233   if ((! FLOAT_MODE_P (mode) || flag_fast_math)
4234       && comparison_p
4235       && rtx_equal_p (XEXP (cond, 0), true)
4236       && rtx_equal_p (XEXP (cond, 1), false)
4237       && ! side_effects_p (cond))
4238     switch (true_code)
4239       {
4240       case GE:
4241       case GT:
4242         return gen_binary (SMAX, mode, true, false);
4243       case LE:
4244       case LT:
4245         return gen_binary (SMIN, mode, true, false);
4246       case GEU:
4247       case GTU:
4248         return gen_binary (UMAX, mode, true, false);
4249       case LEU:
4250       case LTU:
4251         return gen_binary (UMIN, mode, true, false);
4252       }
4253   
4254   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4255      second operand is zero, this can be done as (OP Z (mult COND C2)) where
4256      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4257      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4258      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4259      neither 1 or -1, but it isn't worth checking for.  */
4260
4261   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4262       && comparison_p && mode != VOIDmode && ! side_effects_p (x))
4263     {
4264       rtx t = make_compound_operation (true, SET);
4265       rtx f = make_compound_operation (false, SET);
4266       rtx cond_op0 = XEXP (cond, 0);
4267       rtx cond_op1 = XEXP (cond, 1);
4268       enum rtx_code op, extend_op = NIL;
4269       enum machine_mode m = mode;
4270       rtx z = 0, c1;
4271
4272       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4273            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4274            || GET_CODE (t) == ASHIFT
4275            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4276           && rtx_equal_p (XEXP (t, 0), f))
4277         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4278
4279       /* If an identity-zero op is commutative, check whether there
4280          would be a match if we swapped the operands.  */
4281       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4282                 || GET_CODE (t) == XOR)
4283                && rtx_equal_p (XEXP (t, 1), f))
4284         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4285       else if (GET_CODE (t) == SIGN_EXTEND
4286                && (GET_CODE (XEXP (t, 0)) == PLUS
4287                    || GET_CODE (XEXP (t, 0)) == MINUS
4288                    || GET_CODE (XEXP (t, 0)) == IOR
4289                    || GET_CODE (XEXP (t, 0)) == XOR
4290                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4291                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4292                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4293                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4294                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4295                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4296                && (num_sign_bit_copies (f, GET_MODE (f))
4297                    > (GET_MODE_BITSIZE (mode)
4298                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4299         {
4300           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4301           extend_op = SIGN_EXTEND;
4302           m = GET_MODE (XEXP (t, 0));
4303         }
4304       else if (GET_CODE (t) == SIGN_EXTEND
4305                && (GET_CODE (XEXP (t, 0)) == PLUS
4306                    || GET_CODE (XEXP (t, 0)) == IOR
4307                    || GET_CODE (XEXP (t, 0)) == XOR)
4308                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4309                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4310                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4311                && (num_sign_bit_copies (f, GET_MODE (f))
4312                    > (GET_MODE_BITSIZE (mode)
4313                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4314         {
4315           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4316           extend_op = SIGN_EXTEND;
4317           m = GET_MODE (XEXP (t, 0));
4318         }
4319       else if (GET_CODE (t) == ZERO_EXTEND
4320                && (GET_CODE (XEXP (t, 0)) == PLUS
4321                    || GET_CODE (XEXP (t, 0)) == MINUS
4322                    || GET_CODE (XEXP (t, 0)) == IOR
4323                    || GET_CODE (XEXP (t, 0)) == XOR
4324                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4325                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4326                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4327                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4328                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4329                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4330                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4331                && ((nonzero_bits (f, GET_MODE (f))
4332                     & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4333                    == 0))
4334         {
4335           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4336           extend_op = ZERO_EXTEND;
4337           m = GET_MODE (XEXP (t, 0));
4338         }
4339       else if (GET_CODE (t) == ZERO_EXTEND
4340                && (GET_CODE (XEXP (t, 0)) == PLUS
4341                    || GET_CODE (XEXP (t, 0)) == IOR
4342                    || GET_CODE (XEXP (t, 0)) == XOR)
4343                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4344                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4345                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4346                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4347                && ((nonzero_bits (f, GET_MODE (f))
4348                     & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4349                    == 0))
4350         {
4351           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4352           extend_op = ZERO_EXTEND;
4353           m = GET_MODE (XEXP (t, 0));
4354         }
4355       
4356       if (z)
4357         {
4358           temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4359                         pc_rtx, pc_rtx, 0, 0);
4360           temp = gen_binary (MULT, m, temp,
4361                              gen_binary (MULT, m, c1, const_true_rtx));
4362           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4363           temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4364
4365           if (extend_op != NIL)
4366             temp = gen_unary (extend_op, mode, m, temp);
4367
4368           return temp;
4369         }
4370     }
4371
4372   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4373      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4374      negation of a single bit, we can convert this operation to a shift.  We
4375      can actually do this more generally, but it doesn't seem worth it.  */
4376
4377   if (true_code == NE && XEXP (cond, 1) == const0_rtx
4378       && false == const0_rtx && GET_CODE (true) == CONST_INT
4379       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4380            && (i = exact_log2 (INTVAL (true))) >= 0)
4381           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4382                == GET_MODE_BITSIZE (mode))
4383               && (i = exact_log2 (- INTVAL (true))) >= 0)))
4384     return
4385       simplify_shift_const (NULL_RTX, ASHIFT, mode,
4386                             gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4387
4388   return x;
4389 }
4390 \f
4391 /* Simplify X, a SET expression.  Return the new expression.  */
4392
4393 static rtx
4394 simplify_set (x)
4395      rtx x;
4396 {
4397   rtx src = SET_SRC (x);
4398   rtx dest = SET_DEST (x);
4399   enum machine_mode mode
4400     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4401   rtx other_insn;
4402   rtx *cc_use;
4403
4404   /* (set (pc) (return)) gets written as (return).  */
4405   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4406     return src;
4407
4408   /* Now that we know for sure which bits of SRC we are using, see if we can
4409      simplify the expression for the object knowing that we only need the
4410      low-order bits.  */
4411
4412   if (GET_MODE_CLASS (mode) == MODE_INT)
4413     src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4414
4415   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4416      the comparison result and try to simplify it unless we already have used
4417      undobuf.other_insn.  */
4418   if ((GET_CODE (src) == COMPARE
4419 #ifdef HAVE_cc0
4420        || dest == cc0_rtx
4421 #endif
4422        )
4423       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4424       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4425       && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4426       && rtx_equal_p (XEXP (*cc_use, 0), dest))
4427     {
4428       enum rtx_code old_code = GET_CODE (*cc_use);
4429       enum rtx_code new_code;
4430       rtx op0, op1;
4431       int other_changed = 0;
4432       enum machine_mode compare_mode = GET_MODE (dest);
4433
4434       if (GET_CODE (src) == COMPARE)
4435         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4436       else
4437         op0 = src, op1 = const0_rtx;
4438
4439       /* Simplify our comparison, if possible.  */
4440       new_code = simplify_comparison (old_code, &op0, &op1);
4441
4442 #ifdef EXTRA_CC_MODES
4443       /* If this machine has CC modes other than CCmode, check to see if we
4444          need to use a different CC mode here.  */
4445       compare_mode = SELECT_CC_MODE (new_code, op0, op1);
4446 #endif /* EXTRA_CC_MODES */
4447
4448 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
4449       /* If the mode changed, we have to change SET_DEST, the mode in the
4450          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
4451          a hard register, just build new versions with the proper mode.  If it
4452          is a pseudo, we lose unless it is only time we set the pseudo, in
4453          which case we can safely change its mode.  */
4454       if (compare_mode != GET_MODE (dest))
4455         {
4456           int regno = REGNO (dest);
4457           rtx new_dest = gen_rtx (REG, compare_mode, regno);
4458
4459           if (regno < FIRST_PSEUDO_REGISTER
4460               || (reg_n_sets[regno] == 1 && ! REG_USERVAR_P (dest)))
4461             {
4462               if (regno >= FIRST_PSEUDO_REGISTER)
4463                 SUBST (regno_reg_rtx[regno], new_dest);
4464
4465               SUBST (SET_DEST (x), new_dest);
4466               SUBST (XEXP (*cc_use, 0), new_dest);
4467               other_changed = 1;
4468
4469               dest = new_dest;
4470             }
4471         }
4472 #endif
4473
4474       /* If the code changed, we have to build a new comparison in
4475          undobuf.other_insn.  */
4476       if (new_code != old_code)
4477         {
4478           unsigned HOST_WIDE_INT mask;
4479
4480           SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4481                                            dest, const0_rtx));
4482
4483           /* If the only change we made was to change an EQ into an NE or
4484              vice versa, OP0 has only one bit that might be nonzero, and OP1
4485              is zero, check if changing the user of the condition code will
4486              produce a valid insn.  If it won't, we can keep the original code
4487              in that insn by surrounding our operation with an XOR.  */
4488
4489           if (((old_code == NE && new_code == EQ)
4490                || (old_code == EQ && new_code == NE))
4491               && ! other_changed && op1 == const0_rtx
4492               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4493               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
4494             {
4495               rtx pat = PATTERN (other_insn), note = 0;
4496               int scratches;
4497
4498               if ((recog_for_combine (&pat, other_insn, &note, &scratches) < 0
4499                    && ! check_asm_operands (pat)))
4500                 {
4501                   PUT_CODE (*cc_use, old_code);
4502                   other_insn = 0;
4503
4504                   op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
4505                 }
4506             }
4507
4508           other_changed = 1;
4509         }
4510
4511       if (other_changed)
4512         undobuf.other_insn = other_insn;
4513
4514 #ifdef HAVE_cc0
4515       /* If we are now comparing against zero, change our source if
4516          needed.  If we do not use cc0, we always have a COMPARE.  */
4517       if (op1 == const0_rtx && dest == cc0_rtx)
4518         {
4519           SUBST (SET_SRC (x), op0);
4520           src = op0;
4521         }
4522       else
4523 #endif
4524
4525       /* Otherwise, if we didn't previously have a COMPARE in the
4526          correct mode, we need one.  */
4527       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4528         {
4529           SUBST (SET_SRC (x),
4530                  gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4531           src = SET_SRC (x);
4532         }
4533       else
4534         {
4535           /* Otherwise, update the COMPARE if needed.  */
4536           SUBST (XEXP (src, 0), op0);
4537           SUBST (XEXP (src, 1), op1);
4538         }
4539     }
4540   else
4541     {
4542       /* Get SET_SRC in a form where we have placed back any
4543          compound expressions.  Then do the checks below.  */
4544       src = make_compound_operation (src, SET);
4545       SUBST (SET_SRC (x), src);
4546     }
4547
4548   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4549      and X being a REG or (subreg (reg)), we may be able to convert this to
4550      (set (subreg:m2 x) (op)). 
4551
4552      We can always do this if M1 is narrower than M2 because that means that
4553      we only care about the low bits of the result.
4554
4555      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
4556      perform a narrower operation that requested since the high-order bits will
4557      be undefined.  On machine where it is defined, this transformation is safe
4558      as long as M1 and M2 have the same number of words.  */
4559  
4560   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4561       && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4562       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4563            / UNITS_PER_WORD)
4564           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4565                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
4566 #ifndef WORD_REGISTER_OPERATIONS
4567       && (GET_MODE_SIZE (GET_MODE (src))
4568           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4569 #endif
4570 #ifdef CLASS_CANNOT_CHANGE_SIZE
4571       && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4572             && (TEST_HARD_REG_BIT
4573                 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4574                  REGNO (dest)))
4575             && (GET_MODE_SIZE (GET_MODE (src))
4576                 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4577 #endif                            
4578       && (GET_CODE (dest) == REG
4579           || (GET_CODE (dest) == SUBREG
4580               && GET_CODE (SUBREG_REG (dest)) == REG)))
4581     {
4582       SUBST (SET_DEST (x),
4583              gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4584                                       dest));
4585       SUBST (SET_SRC (x), SUBREG_REG (src));
4586
4587       src = SET_SRC (x), dest = SET_DEST (x);
4588     }
4589
4590 #ifdef LOAD_EXTEND_OP
4591   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4592      would require a paradoxical subreg.  Replace the subreg with a
4593      zero_extend to avoid the reload that would otherwise be required.  */
4594
4595   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4596       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4597       && SUBREG_WORD (src) == 0
4598       && (GET_MODE_SIZE (GET_MODE (src))
4599           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4600       && GET_CODE (SUBREG_REG (src)) == MEM)
4601     {
4602       SUBST (SET_SRC (x),
4603              gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4604                               GET_MODE (src), XEXP (src, 0)));
4605
4606       src = SET_SRC (x);
4607     }
4608 #endif
4609
4610   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4611      are comparing an item known to be 0 or -1 against 0, use a logical
4612      operation instead. Check for one of the arms being an IOR of the other
4613      arm with some value.  We compute three terms to be IOR'ed together.  In
4614      practice, at most two will be nonzero.  Then we do the IOR's.  */
4615
4616   if (GET_CODE (dest) != PC
4617       && GET_CODE (src) == IF_THEN_ELSE
4618       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
4619       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4620       && XEXP (XEXP (src, 0), 1) == const0_rtx
4621       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
4622 #ifdef HAVE_conditional_move
4623       && ! can_conditionally_move_p (GET_MODE (src))
4624 #endif
4625       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4626                                GET_MODE (XEXP (XEXP (src, 0), 0)))
4627           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4628       && ! side_effects_p (src))
4629     {
4630       rtx true = (GET_CODE (XEXP (src, 0)) == NE
4631                       ? XEXP (src, 1) : XEXP (src, 2));
4632       rtx false = (GET_CODE (XEXP (src, 0)) == NE
4633                    ? XEXP (src, 2) : XEXP (src, 1));
4634       rtx term1 = const0_rtx, term2, term3;
4635
4636       if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4637         term1 = false, true = XEXP (true, 1), false = const0_rtx;
4638       else if (GET_CODE (true) == IOR
4639                && rtx_equal_p (XEXP (true, 1), false))
4640         term1 = false, true = XEXP (true, 0), false = const0_rtx;
4641       else if (GET_CODE (false) == IOR
4642                && rtx_equal_p (XEXP (false, 0), true))
4643         term1 = true, false = XEXP (false, 1), true = const0_rtx;
4644       else if (GET_CODE (false) == IOR
4645                && rtx_equal_p (XEXP (false, 1), true))
4646         term1 = true, false = XEXP (false, 0), true = const0_rtx;
4647
4648       term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4649       term3 = gen_binary (AND, GET_MODE (src),
4650                           gen_unary (NOT, GET_MODE (src), GET_MODE (src),
4651                                      XEXP (XEXP (src, 0), 0)),
4652                           false);
4653
4654       SUBST (SET_SRC (x),
4655              gen_binary (IOR, GET_MODE (src),
4656                          gen_binary (IOR, GET_MODE (src), term1, term2),
4657                          term3));
4658
4659       src = SET_SRC (x);
4660     }
4661
4662   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
4663      whole thing fail.  */
4664   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
4665     return src;
4666   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
4667     return dest;
4668   else
4669     /* Convert this into a field assignment operation, if possible.  */
4670     return make_field_assignment (x);
4671 }
4672 \f
4673 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
4674    result.  LAST is nonzero if this is the last retry.  */
4675
4676 static rtx
4677 simplify_logical (x, last)
4678      rtx x;
4679      int last;
4680 {
4681   enum machine_mode mode = GET_MODE (x);
4682   rtx op0 = XEXP (x, 0);
4683   rtx op1 = XEXP (x, 1);
4684
4685   switch (GET_CODE (x))
4686     {
4687     case AND:
4688       /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4689          insn (and may simplify more).  */
4690       if (GET_CODE (op0) == XOR
4691           && rtx_equal_p (XEXP (op0, 0), op1)
4692           && ! side_effects_p (op1))
4693         x = gen_binary (AND, mode,
4694                         gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
4695
4696       if (GET_CODE (op0) == XOR
4697           && rtx_equal_p (XEXP (op0, 1), op1)
4698           && ! side_effects_p (op1))
4699         x = gen_binary (AND, mode,
4700                         gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
4701
4702       /* Similarly for (~ (A ^ B)) & A.  */
4703       if (GET_CODE (op0) == NOT
4704           && GET_CODE (XEXP (op0, 0)) == XOR
4705           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4706           && ! side_effects_p (op1))
4707         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4708
4709       if (GET_CODE (op0) == NOT
4710           && GET_CODE (XEXP (op0, 0)) == XOR
4711           && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4712           && ! side_effects_p (op1))
4713         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4714
4715       if (GET_CODE (op1) == CONST_INT)
4716         {
4717           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
4718
4719           /* If we have (ior (and (X C1) C2)) and the next restart would be
4720              the last, simplify this by making C1 as small as possible
4721              and then exit.  */
4722           if (last
4723               && GET_CODE (x) == IOR && GET_CODE (op0) == AND
4724               && GET_CODE (XEXP (op0, 1)) == CONST_INT
4725               && GET_CODE (op1) == CONST_INT)
4726             return gen_binary (IOR, mode,
4727                                gen_binary (AND, mode, XEXP (op0, 0),
4728                                            GEN_INT (INTVAL (XEXP (op0, 1))
4729                                                     & ~ INTVAL (op1))), op1);
4730
4731           if (GET_CODE (x) != AND)
4732             return x;
4733
4734           if (GET_RTX_CLASS (GET_CODE (x)) == 'c' 
4735               || GET_RTX_CLASS (GET_CODE (x)) == '2')
4736             op0 = XEXP (x, 0), op1 = XEXP (x, 1);
4737         }
4738
4739       /* Convert (A | B) & A to A.  */
4740       if (GET_CODE (op0) == IOR
4741           && (rtx_equal_p (XEXP (op0, 0), op1)
4742               || rtx_equal_p (XEXP (op0, 1), op1))
4743           && ! side_effects_p (XEXP (op0, 0))
4744           && ! side_effects_p (XEXP (op0, 1)))
4745         return op1;
4746
4747       /* In the following group of tests (and those in case IOR below),
4748          we start with some combination of logical operations and apply
4749          the distributive law followed by the inverse distributive law.
4750          Most of the time, this results in no change.  However, if some of
4751          the operands are the same or inverses of each other, simplifications
4752          will result.
4753
4754          For example, (and (ior A B) (not B)) can occur as the result of
4755          expanding a bit field assignment.  When we apply the distributive
4756          law to this, we get (ior (and (A (not B))) (and (B (not B)))),
4757          which then simplifies to (and (A (not B))). 
4758
4759          If we have (and (ior A B) C), apply the distributive law and then
4760          the inverse distributive law to see if things simplify.  */
4761
4762       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4763         {
4764           x = apply_distributive_law
4765             (gen_binary (GET_CODE (op0), mode,
4766                          gen_binary (AND, mode, XEXP (op0, 0), op1),
4767                          gen_binary (AND, mode, XEXP (op0, 1), op1)));
4768           if (GET_CODE (x) != AND)
4769             return x;
4770         }
4771
4772       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
4773         return apply_distributive_law
4774           (gen_binary (GET_CODE (op1), mode,
4775                        gen_binary (AND, mode, XEXP (op1, 0), op0),
4776                        gen_binary (AND, mode, XEXP (op1, 1), op0)));
4777
4778       /* Similarly, taking advantage of the fact that
4779          (and (not A) (xor B C)) == (xor (ior A B) (ior A C))  */
4780
4781       if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
4782         return apply_distributive_law
4783           (gen_binary (XOR, mode,
4784                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
4785                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
4786                                                             
4787       else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
4788         return apply_distributive_law
4789           (gen_binary (XOR, mode,
4790                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
4791                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
4792       break;
4793
4794     case IOR:
4795       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
4796       if (GET_CODE (op1) == CONST_INT
4797           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4798           && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
4799         return op1;
4800
4801       /* Convert (A & B) | A to A.  */
4802       if (GET_CODE (op0) == AND
4803           && (rtx_equal_p (XEXP (op0, 0), op1)
4804               || rtx_equal_p (XEXP (op0, 1), op1))
4805           && ! side_effects_p (XEXP (op0, 0))
4806           && ! side_effects_p (XEXP (op0, 1)))
4807         return op1;
4808
4809       /* If we have (ior (and A B) C), apply the distributive law and then
4810          the inverse distributive law to see if things simplify.  */
4811
4812       if (GET_CODE (op0) == AND)
4813         {
4814           x = apply_distributive_law
4815             (gen_binary (AND, mode,
4816                          gen_binary (IOR, mode, XEXP (op0, 0), op1),
4817                          gen_binary (IOR, mode, XEXP (op0, 1), op1)));
4818
4819           if (GET_CODE (x) != IOR)
4820             return x;
4821         }
4822
4823       if (GET_CODE (op1) == AND)
4824         {
4825           x = apply_distributive_law
4826             (gen_binary (AND, mode,
4827                          gen_binary (IOR, mode, XEXP (op1, 0), op0),
4828                          gen_binary (IOR, mode, XEXP (op1, 1), op0)));
4829
4830           if (GET_CODE (x) != IOR)
4831             return x;
4832         }
4833
4834       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4835          mode size to (rotate A CX).  */
4836
4837       if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
4838            || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
4839           && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
4840           && GET_CODE (XEXP (op0, 1)) == CONST_INT
4841           && GET_CODE (XEXP (op1, 1)) == CONST_INT
4842           && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
4843               == GET_MODE_BITSIZE (mode)))
4844         return gen_rtx (ROTATE, mode, XEXP (op0, 0),
4845                         (GET_CODE (op0) == ASHIFT
4846                          ? XEXP (op0, 1) : XEXP (op1, 1)));
4847
4848       /* If OP0 is (ashiftrt (plus ...) C), it might actually be
4849          a (sign_extend (plus ...)).  If so, OP1 is a CONST_INT, and the PLUS
4850          does not affect any of the bits in OP1, it can really be done
4851          as a PLUS and we can associate.  We do this by seeing if OP1
4852          can be safely shifted left C bits.  */
4853       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
4854           && GET_CODE (XEXP (op0, 0)) == PLUS
4855           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
4856           && GET_CODE (XEXP (op0, 1)) == CONST_INT
4857           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
4858         {
4859           int count = INTVAL (XEXP (op0, 1));
4860           HOST_WIDE_INT mask = INTVAL (op1) << count;
4861
4862           if (mask >> count == INTVAL (op1)
4863               && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
4864             {
4865               SUBST (XEXP (XEXP (op0, 0), 1),
4866                      GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
4867               return op0;
4868             }
4869         }
4870       break;
4871
4872     case XOR:
4873       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
4874          Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
4875          (NOT y).  */
4876       {
4877         int num_negated = 0;
4878
4879         if (GET_CODE (op0) == NOT)
4880           num_negated++, op0 = XEXP (op0, 0);
4881         if (GET_CODE (op1) == NOT)
4882           num_negated++, op1 = XEXP (op1, 0);
4883
4884         if (num_negated == 2)
4885           {
4886             SUBST (XEXP (x, 0), op0);
4887             SUBST (XEXP (x, 1), op1);
4888           }
4889         else if (num_negated == 1)
4890           return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
4891       }
4892
4893       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
4894          correspond to a machine insn or result in further simplifications
4895          if B is a constant.  */
4896
4897       if (GET_CODE (op0) == AND
4898           && rtx_equal_p (XEXP (op0, 1), op1)
4899           && ! side_effects_p (op1))
4900         return gen_binary (AND, mode,
4901                            gen_unary (NOT, mode, mode, XEXP (op0, 0)),
4902                            op1);
4903
4904       else if (GET_CODE (op0) == AND
4905                && rtx_equal_p (XEXP (op0, 0), op1)
4906                && ! side_effects_p (op1))
4907         return gen_binary (AND, mode,
4908                            gen_unary (NOT, mode, mode, XEXP (op0, 1)),
4909                            op1);
4910
4911       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
4912          comparison if STORE_FLAG_VALUE is 1.  */
4913       if (STORE_FLAG_VALUE == 1
4914           && op1 == const1_rtx
4915           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
4916           && reversible_comparison_p (op0))
4917         return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
4918                                 mode, XEXP (op0, 0), XEXP (op0, 1));
4919
4920       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
4921          is (lt foo (const_int 0)), so we can perform the above
4922          simplification if STORE_FLAG_VALUE is 1.  */
4923
4924       if (STORE_FLAG_VALUE == 1
4925           && op1 == const1_rtx
4926           && GET_CODE (op0) == LSHIFTRT
4927           && GET_CODE (XEXP (op0, 1)) == CONST_INT
4928           && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
4929         return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
4930
4931       /* (xor (comparison foo bar) (const_int sign-bit))
4932          when STORE_FLAG_VALUE is the sign bit.  */
4933       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4934           && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4935               == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4936           && op1 == const_true_rtx
4937           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
4938           && reversible_comparison_p (op0))
4939         return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
4940                                 mode, XEXP (op0, 0), XEXP (op0, 1));
4941       break;
4942     }
4943
4944   return x;
4945 }
4946 \f
4947 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
4948    operations" because they can be replaced with two more basic operations.
4949    ZERO_EXTEND is also considered "compound" because it can be replaced with
4950    an AND operation, which is simpler, though only one operation.
4951
4952    The function expand_compound_operation is called with an rtx expression
4953    and will convert it to the appropriate shifts and AND operations, 
4954    simplifying at each stage.
4955
4956    The function make_compound_operation is called to convert an expression
4957    consisting of shifts and ANDs into the equivalent compound expression.
4958    It is the inverse of this function, loosely speaking.  */
4959
4960 static rtx
4961 expand_compound_operation (x)
4962      rtx x;
4963 {
4964   int pos = 0, len;
4965   int unsignedp = 0;
4966   int modewidth;
4967   rtx tem;
4968
4969   switch (GET_CODE (x))
4970     {
4971     case ZERO_EXTEND:
4972       unsignedp = 1;
4973     case SIGN_EXTEND:
4974       /* We can't necessarily use a const_int for a multiword mode;
4975          it depends on implicitly extending the value.
4976          Since we don't know the right way to extend it,
4977          we can't tell whether the implicit way is right.
4978
4979          Even for a mode that is no wider than a const_int,
4980          we can't win, because we need to sign extend one of its bits through
4981          the rest of it, and we don't know which bit.  */
4982       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4983         return x;
4984
4985       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
4986          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
4987          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
4988          reloaded. If not for that, MEM's would very rarely be safe.
4989
4990          Reject MODEs bigger than a word, because we might not be able
4991          to reference a two-register group starting with an arbitrary register
4992          (and currently gen_lowpart might crash for a SUBREG).  */
4993   
4994       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
4995         return x;
4996
4997       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
4998       /* If the inner object has VOIDmode (the only way this can happen
4999          is if it is a ASM_OPERANDS), we can't do anything since we don't
5000          know how much masking to do.  */
5001       if (len == 0)
5002         return x;
5003
5004       break;
5005
5006     case ZERO_EXTRACT:
5007       unsignedp = 1;
5008     case SIGN_EXTRACT:
5009       /* If the operand is a CLOBBER, just return it.  */
5010       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5011         return XEXP (x, 0);
5012
5013       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5014           || GET_CODE (XEXP (x, 2)) != CONST_INT
5015           || GET_MODE (XEXP (x, 0)) == VOIDmode)
5016         return x;
5017
5018       len = INTVAL (XEXP (x, 1));
5019       pos = INTVAL (XEXP (x, 2));
5020
5021       /* If this goes outside the object being extracted, replace the object
5022          with a (use (mem ...)) construct that only combine understands
5023          and is used only for this purpose.  */
5024       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5025         SUBST (XEXP (x, 0), gen_rtx (USE, GET_MODE (x), XEXP (x, 0)));
5026
5027       if (BITS_BIG_ENDIAN)
5028         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5029
5030       break;
5031
5032     default:
5033       return x;
5034     }
5035
5036   /* We can optimize some special cases of ZERO_EXTEND.  */
5037   if (GET_CODE (x) == ZERO_EXTEND)
5038     {
5039       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5040          know that the last value didn't have any inappropriate bits
5041          set.  */
5042       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5043           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5044           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5045           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5046               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5047         return XEXP (XEXP (x, 0), 0);
5048
5049       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5050       if (GET_CODE (XEXP (x, 0)) == SUBREG
5051           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5052           && subreg_lowpart_p (XEXP (x, 0))
5053           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5054           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5055               & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0)
5056         return SUBREG_REG (XEXP (x, 0));
5057
5058       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5059          is a comparison and STORE_FLAG_VALUE permits.  This is like
5060          the first case, but it works even when GET_MODE (x) is larger
5061          than HOST_WIDE_INT.  */
5062       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5063           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5064           && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5065           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5066               <= HOST_BITS_PER_WIDE_INT)
5067           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5068               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5069         return XEXP (XEXP (x, 0), 0);
5070
5071       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5072       if (GET_CODE (XEXP (x, 0)) == SUBREG
5073           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5074           && subreg_lowpart_p (XEXP (x, 0))
5075           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5076           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5077               <= HOST_BITS_PER_WIDE_INT)
5078           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5079               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5080         return SUBREG_REG (XEXP (x, 0));
5081
5082       /* If sign extension is cheaper than zero extension, then use it
5083          if we know that no extraneous bits are set, and that the high
5084          bit is not set.  */
5085       if (flag_expensive_optimizations
5086           && ((GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5087                && ((nonzero_bits (XEXP (x, 0), GET_MODE (x))
5088                     & ~ (((unsigned HOST_WIDE_INT)
5089                           GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5090                          >> 1))
5091                    == 0))
5092               || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5093                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5094                       <= HOST_BITS_PER_WIDE_INT)
5095                   && (((HOST_WIDE_INT) STORE_FLAG_VALUE
5096                        & ~ (((unsigned HOST_WIDE_INT)
5097                              GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5098                             >> 1))
5099                       == 0))))
5100         {
5101           rtx temp = gen_rtx (SIGN_EXTEND, GET_MODE (x), XEXP (x, 0));
5102
5103           if (rtx_cost (temp, SET) < rtx_cost (x, SET))
5104             return expand_compound_operation (temp);
5105         }
5106     }
5107
5108   /* If we reach here, we want to return a pair of shifts.  The inner
5109      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
5110      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
5111      logical depending on the value of UNSIGNEDP.
5112
5113      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5114      converted into an AND of a shift.
5115
5116      We must check for the case where the left shift would have a negative
5117      count.  This can happen in a case like (x >> 31) & 255 on machines
5118      that can't shift by a constant.  On those machines, we would first
5119      combine the shift with the AND to produce a variable-position 
5120      extraction.  Then the constant of 31 would be substituted in to produce
5121      a such a position.  */
5122
5123   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5124   if (modewidth >= pos - len)
5125     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5126                                 GET_MODE (x),
5127                                 simplify_shift_const (NULL_RTX, ASHIFT,
5128                                                       GET_MODE (x),
5129                                                       XEXP (x, 0),
5130                                                       modewidth - pos - len),
5131                                 modewidth - len);
5132
5133   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5134     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5135                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
5136                                                         GET_MODE (x),
5137                                                         XEXP (x, 0), pos),
5138                                   ((HOST_WIDE_INT) 1 << len) - 1);
5139   else
5140     /* Any other cases we can't handle.  */
5141     return x;
5142     
5143
5144   /* If we couldn't do this for some reason, return the original
5145      expression.  */
5146   if (GET_CODE (tem) == CLOBBER)
5147     return x;
5148
5149   return tem;
5150 }
5151 \f
5152 /* X is a SET which contains an assignment of one object into
5153    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5154    or certain SUBREGS). If possible, convert it into a series of
5155    logical operations.
5156
5157    We half-heartedly support variable positions, but do not at all
5158    support variable lengths.  */
5159
5160 static rtx
5161 expand_field_assignment (x)
5162      rtx x;
5163 {
5164   rtx inner;
5165   rtx pos;                      /* Always counts from low bit.  */
5166   int len;
5167   rtx mask;
5168   enum machine_mode compute_mode;
5169
5170   /* Loop until we find something we can't simplify.  */
5171   while (1)
5172     {
5173       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5174           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5175         {
5176           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5177           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5178           pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
5179         }
5180       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5181                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5182         {
5183           inner = XEXP (SET_DEST (x), 0);
5184           len = INTVAL (XEXP (SET_DEST (x), 1));
5185           pos = XEXP (SET_DEST (x), 2);
5186
5187           /* If the position is constant and spans the width of INNER,
5188              surround INNER  with a USE to indicate this.  */
5189           if (GET_CODE (pos) == CONST_INT
5190               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5191             inner = gen_rtx (USE, GET_MODE (SET_DEST (x)), inner);
5192
5193           if (BITS_BIG_ENDIAN)
5194             {
5195               if (GET_CODE (pos) == CONST_INT)
5196                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5197                                - INTVAL (pos));
5198               else if (GET_CODE (pos) == MINUS
5199                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
5200                        && (INTVAL (XEXP (pos, 1))
5201                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5202                 /* If position is ADJUST - X, new position is X.  */
5203                 pos = XEXP (pos, 0);
5204               else
5205                 pos = gen_binary (MINUS, GET_MODE (pos),
5206                                   GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5207                                            - len),
5208                                   pos);
5209             }
5210         }
5211
5212       /* A SUBREG between two modes that occupy the same numbers of words
5213          can be done by moving the SUBREG to the source.  */
5214       else if (GET_CODE (SET_DEST (x)) == SUBREG
5215                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5216                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5217                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5218                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5219         {
5220           x = gen_rtx (SET, VOIDmode, SUBREG_REG (SET_DEST (x)),
5221                        gen_lowpart_for_combine (GET_MODE (SUBREG_REG (SET_DEST (x))),
5222                                                 SET_SRC (x)));
5223           continue;
5224         }
5225       else
5226         break;
5227
5228       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5229         inner = SUBREG_REG (inner);
5230
5231       compute_mode = GET_MODE (inner);
5232
5233       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
5234       if (len < HOST_BITS_PER_WIDE_INT)
5235         mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5236       else
5237         break;
5238
5239       /* Now compute the equivalent expression.  Make a copy of INNER
5240          for the SET_DEST in case it is a MEM into which we will substitute;
5241          we don't want shared RTL in that case.  */
5242       x = gen_rtx (SET, VOIDmode, copy_rtx (inner),
5243                    gen_binary (IOR, compute_mode,
5244                                gen_binary (AND, compute_mode,
5245                                            gen_unary (NOT, compute_mode,
5246                                                       compute_mode,
5247                                                       gen_binary (ASHIFT,
5248                                                                   compute_mode,
5249                                                                   mask, pos)),
5250                                            inner),
5251                                gen_binary (ASHIFT, compute_mode,
5252                                            gen_binary (AND, compute_mode,
5253                                                        gen_lowpart_for_combine
5254                                                        (compute_mode,
5255                                                         SET_SRC (x)),
5256                                                        mask),
5257                                            pos)));
5258     }
5259
5260   return x;
5261 }
5262 \f
5263 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
5264    it is an RTX that represents a variable starting position; otherwise,
5265    POS is the (constant) starting bit position (counted from the LSB).
5266
5267    INNER may be a USE.  This will occur when we started with a bitfield
5268    that went outside the boundary of the object in memory, which is
5269    allowed on most machines.  To isolate this case, we produce a USE
5270    whose mode is wide enough and surround the MEM with it.  The only
5271    code that understands the USE is this routine.  If it is not removed,
5272    it will cause the resulting insn not to match.
5273
5274    UNSIGNEDP is non-zero for an unsigned reference and zero for a 
5275    signed reference.
5276
5277    IN_DEST is non-zero if this is a reference in the destination of a
5278    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If non-zero,
5279    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5280    be used.
5281
5282    IN_COMPARE is non-zero if we are in a COMPARE.  This means that a
5283    ZERO_EXTRACT should be built even for bits starting at bit 0.
5284
5285    MODE is the desired mode of the result (if IN_DEST == 0).
5286
5287    The result is an RTX for the extraction or NULL_RTX if the target
5288    can't handle it.  */
5289
5290 static rtx
5291 make_extraction (mode, inner, pos, pos_rtx, len,
5292                  unsignedp, in_dest, in_compare)
5293      enum machine_mode mode;
5294      rtx inner;
5295      int pos;
5296      rtx pos_rtx;
5297      int len;
5298      int unsignedp;
5299      int in_dest, in_compare;
5300 {
5301   /* This mode describes the size of the storage area
5302      to fetch the overall value from.  Within that, we
5303      ignore the POS lowest bits, etc.  */
5304   enum machine_mode is_mode = GET_MODE (inner);
5305   enum machine_mode inner_mode;
5306   enum machine_mode wanted_inner_mode = byte_mode;
5307   enum machine_mode wanted_inner_reg_mode = word_mode;
5308   enum machine_mode pos_mode = word_mode;
5309   enum machine_mode extraction_mode = word_mode;
5310   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5311   int spans_byte = 0;
5312   rtx new = 0;
5313   rtx orig_pos_rtx = pos_rtx;
5314   int orig_pos;
5315
5316   /* Get some information about INNER and get the innermost object.  */
5317   if (GET_CODE (inner) == USE)
5318     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
5319     /* We don't need to adjust the position because we set up the USE
5320        to pretend that it was a full-word object.  */
5321     spans_byte = 1, inner = XEXP (inner, 0);
5322   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5323     {
5324       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5325          consider just the QI as the memory to extract from.
5326          The subreg adds or removes high bits; its mode is
5327          irrelevant to the meaning of this extraction,
5328          since POS and LEN count from the lsb.  */
5329       if (GET_CODE (SUBREG_REG (inner)) == MEM)
5330         is_mode = GET_MODE (SUBREG_REG (inner));
5331       inner = SUBREG_REG (inner);
5332     }
5333
5334   inner_mode = GET_MODE (inner);
5335
5336   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5337     pos = INTVAL (pos_rtx), pos_rtx = 0;
5338
5339   /* See if this can be done without an extraction.  We never can if the
5340      width of the field is not the same as that of some integer mode. For
5341      registers, we can only avoid the extraction if the position is at the
5342      low-order bit and this is either not in the destination or we have the
5343      appropriate STRICT_LOW_PART operation available.
5344
5345      For MEM, we can avoid an extract if the field starts on an appropriate
5346      boundary and we can change the mode of the memory reference.  However,
5347      we cannot directly access the MEM if we have a USE and the underlying
5348      MEM is not TMODE.  This combination means that MEM was being used in a
5349      context where bits outside its mode were being referenced; that is only
5350      valid in bit-field insns.  */
5351
5352   if (tmode != BLKmode
5353       && ! (spans_byte && inner_mode != tmode)
5354       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5355            && GET_CODE (inner) != MEM
5356            && (! in_dest
5357                || (GET_CODE (inner) == REG
5358                    && (movstrict_optab->handlers[(int) tmode].insn_code
5359                        != CODE_FOR_nothing))))
5360           || (GET_CODE (inner) == MEM && pos_rtx == 0
5361               && (pos
5362                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5363                      : BITS_PER_UNIT)) == 0
5364               /* We can't do this if we are widening INNER_MODE (it
5365                  may not be aligned, for one thing).  */
5366               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5367               && (inner_mode == tmode
5368                   || (! mode_dependent_address_p (XEXP (inner, 0))
5369                       && ! MEM_VOLATILE_P (inner))))))
5370     {
5371       /* If INNER is a MEM, make a new MEM that encompasses just the desired
5372          field.  If the original and current mode are the same, we need not
5373          adjust the offset.  Otherwise, we do if bytes big endian.  
5374
5375          If INNER is not a MEM, get a piece consisting of just the field
5376          of interest (in this case POS % BITS_PER_WORD must be 0).  */
5377
5378       if (GET_CODE (inner) == MEM)
5379         {
5380           int offset;
5381           /* POS counts from lsb, but make OFFSET count in memory order.  */
5382           if (BYTES_BIG_ENDIAN)
5383             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5384           else
5385             offset = pos / BITS_PER_UNIT;
5386
5387           new = gen_rtx (MEM, tmode, plus_constant (XEXP (inner, 0), offset));
5388           RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
5389           MEM_VOLATILE_P (new) = MEM_VOLATILE_P (inner);
5390           MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (inner);
5391         }
5392       else if (GET_CODE (inner) == REG)
5393         {
5394           /* We can't call gen_lowpart_for_combine here since we always want
5395              a SUBREG and it would sometimes return a new hard register.  */
5396           if (tmode != inner_mode)
5397             new = gen_rtx (SUBREG, tmode, inner,
5398                            (WORDS_BIG_ENDIAN
5399                             && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD
5400                             ? (((GET_MODE_SIZE (inner_mode)
5401                                  - GET_MODE_SIZE (tmode))
5402                                 / UNITS_PER_WORD)
5403                                - pos / BITS_PER_WORD)
5404                             : pos / BITS_PER_WORD));
5405           else
5406             new = inner;
5407         }
5408       else
5409         new = force_to_mode (inner, tmode,
5410                              len >= HOST_BITS_PER_WIDE_INT
5411                              ? GET_MODE_MASK (tmode)
5412                              : ((HOST_WIDE_INT) 1 << len) - 1,
5413                              NULL_RTX, 0);
5414
5415       /* If this extraction is going into the destination of a SET, 
5416          make a STRICT_LOW_PART unless we made a MEM.  */
5417
5418       if (in_dest)
5419         return (GET_CODE (new) == MEM ? new
5420                 : (GET_CODE (new) != SUBREG
5421                    ? gen_rtx (CLOBBER, tmode, const0_rtx)
5422                    : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
5423
5424       /* Otherwise, sign- or zero-extend unless we already are in the
5425          proper mode.  */
5426
5427       return (mode == tmode ? new
5428               : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5429                                  mode, new));
5430     }
5431
5432   /* Unless this is a COMPARE or we have a funny memory reference,
5433      don't do anything with zero-extending field extracts starting at
5434      the low-order bit since they are simple AND operations.  */
5435   if (pos_rtx == 0 && pos == 0 && ! in_dest
5436       && ! in_compare && ! spans_byte && unsignedp)
5437     return 0;
5438
5439   /* Unless we are allowed to span bytes, reject this if we would be
5440      spanning bytes or if the position is not a constant and the length
5441      is not 1.  In all other cases, we would only be going outside
5442      out object in cases when an original shift would have been
5443      undefined.  */
5444   if (! spans_byte
5445       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5446           || (pos_rtx != 0 && len != 1)))
5447     return 0;
5448
5449   /* Get the mode to use should INNER not be a MEM, the mode for the position,
5450      and the mode for the result.  */
5451 #ifdef HAVE_insv
5452   if (in_dest)
5453     {
5454       wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_insv][0];
5455       pos_mode = insn_operand_mode[(int) CODE_FOR_insv][2];
5456       extraction_mode = insn_operand_mode[(int) CODE_FOR_insv][3];
5457     }
5458 #endif
5459
5460 #ifdef HAVE_extzv
5461   if (! in_dest && unsignedp)
5462     {
5463       wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
5464       pos_mode = insn_operand_mode[(int) CODE_FOR_extzv][3];
5465       extraction_mode = insn_operand_mode[(int) CODE_FOR_extzv][0];
5466     }
5467 #endif
5468
5469 #ifdef HAVE_extv
5470   if (! in_dest && ! unsignedp)
5471     {
5472       wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
5473       pos_mode = insn_operand_mode[(int) CODE_FOR_extv][3];
5474       extraction_mode = insn_operand_mode[(int) CODE_FOR_extv][0];
5475     }
5476 #endif
5477
5478   /* Never narrow an object, since that might not be safe.  */
5479
5480   if (mode != VOIDmode
5481       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5482     extraction_mode = mode;
5483
5484   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5485       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5486     pos_mode = GET_MODE (pos_rtx);
5487
5488   /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5489      if we have to change the mode of memory and cannot, the desired mode is
5490      EXTRACTION_MODE.  */
5491   if (GET_CODE (inner) != MEM)
5492     wanted_inner_mode = wanted_inner_reg_mode;
5493   else if (inner_mode != wanted_inner_mode
5494            && (mode_dependent_address_p (XEXP (inner, 0))
5495                || MEM_VOLATILE_P (inner)))
5496     wanted_inner_mode = extraction_mode;
5497
5498   orig_pos = pos;
5499
5500   if (BITS_BIG_ENDIAN)
5501     {
5502       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5503          BITS_BIG_ENDIAN style.  If position is constant, compute new
5504          position.  Otherwise, build subtraction.
5505          Note that POS is relative to the mode of the original argument.
5506          If it's a MEM we need to recompute POS relative to that.
5507          However, if we're extracting from (or inserting into) a register,
5508          we want to recompute POS relative to wanted_inner_mode.  */
5509       int width = (GET_CODE (inner) == MEM
5510                    ? GET_MODE_BITSIZE (is_mode)
5511                    : GET_MODE_BITSIZE (wanted_inner_mode));
5512
5513       if (pos_rtx == 0)
5514         pos = width - len - pos;
5515       else
5516         pos_rtx
5517           = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
5518                              GEN_INT (width - len), pos_rtx);
5519       /* POS may be less than 0 now, but we check for that below.
5520          Note that it can only be less than 0 if GET_CODE (inner) != MEM.  */
5521     }
5522
5523   /* If INNER has a wider mode, make it smaller.  If this is a constant
5524      extract, try to adjust the byte to point to the byte containing
5525      the value.  */
5526   if (wanted_inner_mode != VOIDmode
5527       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
5528       && ((GET_CODE (inner) == MEM
5529            && (inner_mode == wanted_inner_mode
5530                || (! mode_dependent_address_p (XEXP (inner, 0))
5531                    && ! MEM_VOLATILE_P (inner))))))
5532     {
5533       int offset = 0;
5534
5535       /* The computations below will be correct if the machine is big
5536          endian in both bits and bytes or little endian in bits and bytes.
5537          If it is mixed, we must adjust.  */
5538              
5539       /* If bytes are big endian and we had a paradoxical SUBREG, we must
5540          adjust OFFSET to compensate.  */
5541       if (BYTES_BIG_ENDIAN
5542           && ! spans_byte
5543           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5544         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
5545
5546       /* If this is a constant position, we can move to the desired byte.  */
5547       if (pos_rtx == 0)
5548         {
5549           offset += pos / BITS_PER_UNIT;
5550           pos %= GET_MODE_BITSIZE (wanted_inner_mode);
5551         }
5552
5553       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5554           && ! spans_byte
5555           && is_mode != wanted_inner_mode)
5556         offset = (GET_MODE_SIZE (is_mode)
5557                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
5558
5559       if (offset != 0 || inner_mode != wanted_inner_mode)
5560         {
5561           rtx newmem = gen_rtx (MEM, wanted_inner_mode,
5562                                 plus_constant (XEXP (inner, 0), offset));
5563           RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
5564           MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (inner);
5565           MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (inner);
5566           inner = newmem;
5567         }
5568     }
5569
5570   /* If INNER is not memory, we can always get it into the proper mode.  If we
5571      are changing its mode, POS must be a constant and smaller than the size
5572      of the new mode.  */
5573   else if (GET_CODE (inner) != MEM)
5574     {
5575       if (GET_MODE (inner) != wanted_inner_mode
5576           && (pos_rtx != 0
5577               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
5578         return 0;
5579
5580       inner = force_to_mode (inner, wanted_inner_mode,
5581                              pos_rtx
5582                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
5583                              ? GET_MODE_MASK (wanted_inner_mode)
5584                              : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
5585                              NULL_RTX, 0);
5586     }
5587
5588   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
5589      have to zero extend.  Otherwise, we can just use a SUBREG.  */
5590   if (pos_rtx != 0
5591       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5592     pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
5593   else if (pos_rtx != 0
5594            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5595     pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5596
5597   /* Make POS_RTX unless we already have it and it is correct.  If we don't
5598      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
5599      be a CONST_INT.  */
5600   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5601     pos_rtx = orig_pos_rtx;
5602
5603   else if (pos_rtx == 0)
5604     pos_rtx = GEN_INT (pos);
5605
5606   /* Make the required operation.  See if we can use existing rtx.  */
5607   new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5608                          extraction_mode, inner, GEN_INT (len), pos_rtx);
5609   if (! in_dest)
5610     new = gen_lowpart_for_combine (mode, new);
5611
5612   return new;
5613 }
5614 \f
5615 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
5616    with any other operations in X.  Return X without that shift if so.  */
5617
5618 static rtx
5619 extract_left_shift (x, count)
5620      rtx x;
5621      int count;
5622 {
5623   enum rtx_code code = GET_CODE (x);
5624   enum machine_mode mode = GET_MODE (x);
5625   rtx tem;
5626
5627   switch (code)
5628     {
5629     case ASHIFT:
5630       /* This is the shift itself.  If it is wide enough, we will return
5631          either the value being shifted if the shift count is equal to
5632          COUNT or a shift for the difference.  */
5633       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5634           && INTVAL (XEXP (x, 1)) >= count)
5635         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
5636                                      INTVAL (XEXP (x, 1)) - count);
5637       break;
5638
5639     case NEG:  case NOT:
5640       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5641         return gen_unary (code, mode, mode, tem);
5642
5643       break;
5644
5645     case PLUS:  case IOR:  case XOR:  case AND:
5646       /* If we can safely shift this constant and we find the inner shift,
5647          make a new operation.  */
5648       if (GET_CODE (XEXP (x,1)) == CONST_INT
5649           && (INTVAL (XEXP (x, 1)) & (((HOST_WIDE_INT) 1 << count)) - 1) == 0
5650           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5651         return gen_binary (code, mode, tem, 
5652                            GEN_INT (INTVAL (XEXP (x, 1)) >> count));
5653
5654       break;
5655     }
5656
5657   return 0;
5658 }
5659 \f
5660 /* Look at the expression rooted at X.  Look for expressions
5661    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
5662    Form these expressions.
5663
5664    Return the new rtx, usually just X.
5665
5666    Also, for machines like the Vax that don't have logical shift insns,
5667    try to convert logical to arithmetic shift operations in cases where
5668    they are equivalent.  This undoes the canonicalizations to logical
5669    shifts done elsewhere.
5670
5671    We try, as much as possible, to re-use rtl expressions to save memory.
5672
5673    IN_CODE says what kind of expression we are processing.  Normally, it is
5674    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
5675    being kludges), it is MEM.  When processing the arguments of a comparison
5676    or a COMPARE against zero, it is COMPARE.  */
5677
5678 static rtx
5679 make_compound_operation (x, in_code)
5680      rtx x;
5681      enum rtx_code in_code;
5682 {
5683   enum rtx_code code = GET_CODE (x);
5684   enum machine_mode mode = GET_MODE (x);
5685   int mode_width = GET_MODE_BITSIZE (mode);
5686   rtx rhs, lhs;
5687   enum rtx_code next_code;
5688   int i;
5689   rtx new = 0;
5690   rtx tem;
5691   char *fmt;
5692
5693   /* Select the code to be used in recursive calls.  Once we are inside an
5694      address, we stay there.  If we have a comparison, set to COMPARE,
5695      but once inside, go back to our default of SET.  */
5696
5697   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
5698                : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
5699                   && XEXP (x, 1) == const0_rtx) ? COMPARE
5700                : in_code == COMPARE ? SET : in_code);
5701
5702   /* Process depending on the code of this operation.  If NEW is set
5703      non-zero, it will be returned.  */
5704
5705   switch (code)
5706     {
5707     case ASHIFT:
5708       /* Convert shifts by constants into multiplications if inside
5709          an address.  */
5710       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
5711           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
5712           && INTVAL (XEXP (x, 1)) >= 0)
5713         {
5714           new = make_compound_operation (XEXP (x, 0), next_code);
5715           new = gen_rtx_combine (MULT, mode, new,
5716                                  GEN_INT ((HOST_WIDE_INT) 1
5717                                           << INTVAL (XEXP (x, 1))));
5718         }
5719       break;
5720
5721     case AND:
5722       /* If the second operand is not a constant, we can't do anything
5723          with it.  */
5724       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5725         break;
5726
5727       /* If the constant is a power of two minus one and the first operand
5728          is a logical right shift, make an extraction.  */
5729       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
5730           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5731         {
5732           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5733           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
5734                                  0, in_code == COMPARE);
5735         }
5736
5737       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
5738       else if (GET_CODE (XEXP (x, 0)) == SUBREG
5739                && subreg_lowpart_p (XEXP (x, 0))
5740                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
5741                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5742         {
5743           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
5744                                          next_code);
5745           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
5746                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
5747                                  0, in_code == COMPARE);
5748         }
5749       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
5750       else if ((GET_CODE (XEXP (x, 0)) == XOR
5751                 || GET_CODE (XEXP (x, 0)) == IOR)
5752                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
5753                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
5754                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5755         {
5756           /* Apply the distributive law, and then try to make extractions.  */
5757           new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
5758                                  gen_rtx (AND, mode, XEXP (XEXP (x, 0), 0),
5759                                           XEXP (x, 1)),
5760                                  gen_rtx (AND, mode, XEXP (XEXP (x, 0), 1),
5761                                           XEXP (x, 1)));
5762           new = make_compound_operation (new, in_code);
5763         }
5764
5765       /* If we are have (and (rotate X C) M) and C is larger than the number
5766          of bits in M, this is an extraction.  */
5767
5768       else if (GET_CODE (XEXP (x, 0)) == ROTATE
5769                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5770                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
5771                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
5772         {
5773           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5774           new = make_extraction (mode, new,
5775                                  (GET_MODE_BITSIZE (mode)
5776                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
5777                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
5778         }
5779
5780       /* On machines without logical shifts, if the operand of the AND is
5781          a logical shift and our mask turns off all the propagated sign
5782          bits, we can replace the logical shift with an arithmetic shift.  */
5783       else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5784                && (lshr_optab->handlers[(int) mode].insn_code
5785                    == CODE_FOR_nothing)
5786                && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5787                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5788                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
5789                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
5790                && mode_width <= HOST_BITS_PER_WIDE_INT)
5791         {
5792           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
5793
5794           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
5795           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
5796             SUBST (XEXP (x, 0),
5797                    gen_rtx_combine (ASHIFTRT, mode,
5798                                     make_compound_operation (XEXP (XEXP (x, 0), 0),
5799                                                              next_code),
5800                                     XEXP (XEXP (x, 0), 1)));
5801         }
5802
5803       /* If the constant is one less than a power of two, this might be
5804          representable by an extraction even if no shift is present.
5805          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
5806          we are in a COMPARE.  */
5807       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5808         new = make_extraction (mode,
5809                                make_compound_operation (XEXP (x, 0),
5810                                                         next_code),
5811                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
5812
5813       /* If we are in a comparison and this is an AND with a power of two,
5814          convert this into the appropriate bit extract.  */
5815       else if (in_code == COMPARE
5816                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
5817         new = make_extraction (mode,
5818                                make_compound_operation (XEXP (x, 0),
5819                                                         next_code),
5820                                i, NULL_RTX, 1, 1, 0, 1);
5821
5822       break;
5823
5824     case LSHIFTRT:
5825       /* If the sign bit is known to be zero, replace this with an
5826          arithmetic shift.  */
5827       if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
5828           && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5829           && mode_width <= HOST_BITS_PER_WIDE_INT
5830           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
5831         {
5832           new = gen_rtx_combine (ASHIFTRT, mode,
5833                                  make_compound_operation (XEXP (x, 0),
5834                                                           next_code),
5835                                  XEXP (x, 1));
5836           break;
5837         }
5838
5839       /* ... fall through ...  */
5840
5841     case ASHIFTRT:
5842       lhs = XEXP (x, 0);
5843       rhs = XEXP (x, 1);
5844
5845       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
5846          this is a SIGN_EXTRACT.  */
5847       if (GET_CODE (rhs) == CONST_INT
5848           && GET_CODE (lhs) == ASHIFT
5849           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
5850           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
5851         {
5852           new = make_compound_operation (XEXP (lhs, 0), next_code);
5853           new = make_extraction (mode, new,
5854                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
5855                                  NULL_RTX, mode_width - INTVAL (rhs),
5856                                  code == LSHIFTRT, 0, in_code == COMPARE);
5857         }
5858
5859       /* See if we have operations between an ASHIFTRT and an ASHIFT.
5860          If so, try to merge the shifts into a SIGN_EXTEND.  We could
5861          also do this for some cases of SIGN_EXTRACT, but it doesn't
5862          seem worth the effort; the case checked for occurs on Alpha.  */
5863       
5864       if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
5865           && ! (GET_CODE (lhs) == SUBREG
5866                 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
5867           && GET_CODE (rhs) == CONST_INT
5868           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
5869           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
5870         new = make_extraction (mode, make_compound_operation (new, next_code),
5871                                0, NULL_RTX, mode_width - INTVAL (rhs),
5872                                code == LSHIFTRT, 0, in_code == COMPARE);
5873         
5874       break;
5875
5876     case SUBREG:
5877       /* Call ourselves recursively on the inner expression.  If we are
5878          narrowing the object and it has a different RTL code from
5879          what it originally did, do this SUBREG as a force_to_mode.  */
5880
5881       tem = make_compound_operation (SUBREG_REG (x), in_code);
5882       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
5883           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
5884           && subreg_lowpart_p (x))
5885         {
5886           rtx newer = force_to_mode (tem, mode,
5887                                      GET_MODE_MASK (mode), NULL_RTX, 0);
5888
5889           /* If we have something other than a SUBREG, we might have
5890              done an expansion, so rerun outselves.  */
5891           if (GET_CODE (newer) != SUBREG)
5892             newer = make_compound_operation (newer, in_code);
5893
5894           return newer;
5895         }
5896     }
5897
5898   if (new)
5899     {
5900       x = gen_lowpart_for_combine (mode, new);
5901       code = GET_CODE (x);
5902     }
5903
5904   /* Now recursively process each operand of this operation.  */
5905   fmt = GET_RTX_FORMAT (code);
5906   for (i = 0; i < GET_RTX_LENGTH (code); i++)
5907     if (fmt[i] == 'e')
5908       {
5909         new = make_compound_operation (XEXP (x, i), next_code);
5910         SUBST (XEXP (x, i), new);
5911       }
5912
5913   return x;
5914 }
5915 \f
5916 /* Given M see if it is a value that would select a field of bits
5917     within an item, but not the entire word.  Return -1 if not.
5918     Otherwise, return the starting position of the field, where 0 is the
5919     low-order bit.
5920
5921    *PLEN is set to the length of the field.  */
5922
5923 static int
5924 get_pos_from_mask (m, plen)
5925      unsigned HOST_WIDE_INT m;
5926      int *plen;
5927 {
5928   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
5929   int pos = exact_log2 (m & - m);
5930
5931   if (pos < 0)
5932     return -1;
5933
5934   /* Now shift off the low-order zero bits and see if we have a power of
5935      two minus 1.  */
5936   *plen = exact_log2 ((m >> pos) + 1);
5937
5938   if (*plen <= 0)
5939     return -1;
5940
5941   return pos;
5942 }
5943 \f
5944 /* See if X can be simplified knowing that we will only refer to it in
5945    MODE and will only refer to those bits that are nonzero in MASK.
5946    If other bits are being computed or if masking operations are done
5947    that select a superset of the bits in MASK, they can sometimes be
5948    ignored.
5949
5950    Return a possibly simplified expression, but always convert X to
5951    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
5952
5953    Also, if REG is non-zero and X is a register equal in value to REG, 
5954    replace X with REG.
5955
5956    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
5957    are all off in X.  This is used when X will be complemented, by either
5958    NOT, NEG, or XOR.  */
5959
5960 static rtx
5961 force_to_mode (x, mode, mask, reg, just_select)
5962      rtx x;
5963      enum machine_mode mode;
5964      unsigned HOST_WIDE_INT mask;
5965      rtx reg;
5966      int just_select;
5967 {
5968   enum rtx_code code = GET_CODE (x);
5969   int next_select = just_select || code == XOR || code == NOT || code == NEG;
5970   enum machine_mode op_mode;
5971   unsigned HOST_WIDE_INT fuller_mask, nonzero;
5972   rtx op0, op1, temp;
5973
5974   /* If this is a CALL, don't do anything.  Some of the code below
5975      will do the wrong thing since the mode of a CALL is VOIDmode.  */
5976   if (code == CALL)
5977     return x;
5978
5979   /* We want to perform the operation is its present mode unless we know
5980      that the operation is valid in MODE, in which case we do the operation
5981      in MODE.  */
5982   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
5983               && code_to_optab[(int) code] != 0
5984               && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
5985                   != CODE_FOR_nothing))
5986              ? mode : GET_MODE (x));
5987
5988   /* It is not valid to do a right-shift in a narrower mode
5989      than the one it came in with.  */
5990   if ((code == LSHIFTRT || code == ASHIFTRT)
5991       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
5992     op_mode = GET_MODE (x);
5993
5994   /* Truncate MASK to fit OP_MODE.  */
5995   if (op_mode)
5996     mask &= GET_MODE_MASK (op_mode);
5997
5998   /* When we have an arithmetic operation, or a shift whose count we
5999      do not know, we need to assume that all bit the up to the highest-order
6000      bit in MASK will be needed.  This is how we form such a mask.  */
6001   if (op_mode)
6002     fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6003                    ? GET_MODE_MASK (op_mode)
6004                    : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
6005   else
6006     fuller_mask = ~ (HOST_WIDE_INT) 0;
6007
6008   /* Determine what bits of X are guaranteed to be (non)zero.  */
6009   nonzero = nonzero_bits (x, mode);
6010
6011   /* If none of the bits in X are needed, return a zero.  */
6012   if (! just_select && (nonzero & mask) == 0)
6013     return const0_rtx;
6014
6015   /* If X is a CONST_INT, return a new one.  Do this here since the
6016      test below will fail.  */
6017   if (GET_CODE (x) == CONST_INT)
6018     {
6019       HOST_WIDE_INT cval = INTVAL (x) & mask;
6020       int width = GET_MODE_BITSIZE (mode);
6021
6022       /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6023          number, sign extend it.  */
6024       if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6025           && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6026         cval |= (HOST_WIDE_INT) -1 << width;
6027         
6028       return GEN_INT (cval);
6029     }
6030
6031   /* If X is narrower than MODE and we want all the bits in X's mode, just
6032      get X in the proper mode.  */
6033   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6034       && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
6035     return gen_lowpart_for_combine (mode, x);
6036
6037   /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6038      MASK are already known to be zero in X, we need not do anything.  */
6039   if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
6040     return x;
6041
6042   switch (code)
6043     {
6044     case CLOBBER:
6045       /* If X is a (clobber (const_int)), return it since we know we are
6046          generating something that won't match.  */
6047       return x;
6048
6049     case USE:
6050       /* X is a (use (mem ..)) that was made from a bit-field extraction that
6051          spanned the boundary of the MEM.  If we are now masking so it is
6052          within that boundary, we don't need the USE any more.  */
6053       if (! BITS_BIG_ENDIAN
6054           && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6055         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6056       break;
6057
6058     case SIGN_EXTEND:
6059     case ZERO_EXTEND:
6060     case ZERO_EXTRACT:
6061     case SIGN_EXTRACT:
6062       x = expand_compound_operation (x);
6063       if (GET_CODE (x) != code)
6064         return force_to_mode (x, mode, mask, reg, next_select);
6065       break;
6066
6067     case REG:
6068       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6069                        || rtx_equal_p (reg, get_last_value (x))))
6070         x = reg;
6071       break;
6072
6073     case SUBREG:
6074       if (subreg_lowpart_p (x)
6075           /* We can ignore the effect of this SUBREG if it narrows the mode or
6076              if the constant masks to zero all the bits the mode doesn't
6077              have.  */
6078           && ((GET_MODE_SIZE (GET_MODE (x))
6079                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6080               || (0 == (mask
6081                         & GET_MODE_MASK (GET_MODE (x))
6082                         & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6083         return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6084       break;
6085
6086     case AND:
6087       /* If this is an AND with a constant, convert it into an AND
6088          whose constant is the AND of that constant with MASK.  If it
6089          remains an AND of MASK, delete it since it is redundant.  */
6090
6091       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6092         {
6093           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6094                                       mask & INTVAL (XEXP (x, 1)));
6095
6096           /* If X is still an AND, see if it is an AND with a mask that
6097              is just some low-order bits.  If so, and it is MASK, we don't
6098              need it.  */
6099
6100           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6101               && INTVAL (XEXP (x, 1)) == mask)
6102             x = XEXP (x, 0);
6103
6104           /* If it remains an AND, try making another AND with the bits
6105              in the mode mask that aren't in MASK turned on.  If the
6106              constant in the AND is wide enough, this might make a
6107              cheaper constant.  */
6108
6109           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6110               && GET_MODE_MASK (GET_MODE (x)) != mask
6111               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6112             {
6113               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6114                                     | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
6115               int width = GET_MODE_BITSIZE (GET_MODE (x));
6116               rtx y;
6117
6118               /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6119                  number, sign extend it.  */
6120               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6121                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6122                 cval |= (HOST_WIDE_INT) -1 << width;
6123
6124               y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6125               if (rtx_cost (y, SET) < rtx_cost (x, SET))
6126                 x = y;
6127             }
6128
6129           break;
6130         }
6131
6132       goto binop;
6133
6134     case PLUS:
6135       /* In (and (plus FOO C1) M), if M is a mask that just turns off
6136          low-order bits (as in an alignment operation) and FOO is already
6137          aligned to that boundary, mask C1 to that boundary as well.
6138          This may eliminate that PLUS and, later, the AND.  */
6139
6140       {
6141         int width = GET_MODE_BITSIZE (mode);
6142         unsigned HOST_WIDE_INT smask = mask;
6143
6144         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6145            number, sign extend it.  */
6146
6147         if (width < HOST_BITS_PER_WIDE_INT
6148             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6149           smask |= (HOST_WIDE_INT) -1 << width;
6150
6151         if (GET_CODE (XEXP (x, 1)) == CONST_INT
6152             && exact_log2 (- smask) >= 0
6153             && (nonzero_bits (XEXP (x, 0), mode) & ~ mask) == 0
6154             && (INTVAL (XEXP (x, 1)) & ~ mask) != 0)
6155           return force_to_mode (plus_constant (XEXP (x, 0),
6156                                                INTVAL (XEXP (x, 1)) & mask),
6157                                 mode, mask, reg, next_select);
6158       }
6159
6160       /* ... fall through ...  */
6161
6162     case MINUS:
6163     case MULT:
6164       /* For PLUS, MINUS and MULT, we need any bits less significant than the
6165          most significant bit in MASK since carries from those bits will
6166          affect the bits we are interested in.  */
6167       mask = fuller_mask;
6168       goto binop;
6169
6170     case IOR:
6171     case XOR:
6172       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6173          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6174          operation which may be a bitfield extraction.  Ensure that the
6175          constant we form is not wider than the mode of X.  */
6176
6177       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6178           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6179           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6180           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6181           && GET_CODE (XEXP (x, 1)) == CONST_INT
6182           && ((INTVAL (XEXP (XEXP (x, 0), 1))
6183                + floor_log2 (INTVAL (XEXP (x, 1))))
6184               < GET_MODE_BITSIZE (GET_MODE (x)))
6185           && (INTVAL (XEXP (x, 1))
6186               & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6187         {
6188           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6189                               << INTVAL (XEXP (XEXP (x, 0), 1)));
6190           temp = gen_binary (GET_CODE (x), GET_MODE (x),
6191                              XEXP (XEXP (x, 0), 0), temp);
6192           x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6193                           XEXP (XEXP (x, 0), 1));
6194           return force_to_mode (x, mode, mask, reg, next_select);
6195         }
6196
6197     binop:
6198       /* For most binary operations, just propagate into the operation and
6199          change the mode if we have an operation of that mode.   */
6200
6201       op0 = gen_lowpart_for_combine (op_mode,
6202                                      force_to_mode (XEXP (x, 0), mode, mask,
6203                                                     reg, next_select));
6204       op1 = gen_lowpart_for_combine (op_mode,
6205                                      force_to_mode (XEXP (x, 1), mode, mask,
6206                                                     reg, next_select));
6207
6208       /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6209          MASK since OP1 might have been sign-extended but we never want
6210          to turn on extra bits, since combine might have previously relied
6211          on them being off.  */
6212       if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6213           && (INTVAL (op1) & mask) != 0)
6214         op1 = GEN_INT (INTVAL (op1) & mask);
6215          
6216       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6217         x = gen_binary (code, op_mode, op0, op1);
6218       break;
6219
6220     case ASHIFT:
6221       /* For left shifts, do the same, but just for the first operand.
6222          However, we cannot do anything with shifts where we cannot
6223          guarantee that the counts are smaller than the size of the mode
6224          because such a count will have a different meaning in a
6225          wider mode.  */
6226
6227       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6228              && INTVAL (XEXP (x, 1)) >= 0
6229              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6230           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6231                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6232                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6233         break;
6234         
6235       /* If the shift count is a constant and we can do arithmetic in
6236          the mode of the shift, refine which bits we need.  Otherwise, use the
6237          conservative form of the mask.  */
6238       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6239           && INTVAL (XEXP (x, 1)) >= 0
6240           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6241           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6242         mask >>= INTVAL (XEXP (x, 1));
6243       else
6244         mask = fuller_mask;
6245
6246       op0 = gen_lowpart_for_combine (op_mode,
6247                                      force_to_mode (XEXP (x, 0), op_mode,
6248                                                     mask, reg, next_select));
6249
6250       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6251         x =  gen_binary (code, op_mode, op0, XEXP (x, 1));
6252       break;
6253
6254     case LSHIFTRT:
6255       /* Here we can only do something if the shift count is a constant,
6256          this shift constant is valid for the host, and we can do arithmetic
6257          in OP_MODE.  */
6258
6259       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6260           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6261           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6262         {
6263           rtx inner = XEXP (x, 0);
6264
6265           /* Select the mask of the bits we need for the shift operand.  */
6266           mask <<= INTVAL (XEXP (x, 1));
6267
6268           /* We can only change the mode of the shift if we can do arithmetic
6269              in the mode of the shift and MASK is no wider than the width of
6270              OP_MODE.  */
6271           if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6272               || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
6273             op_mode = GET_MODE (x);
6274
6275           inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6276
6277           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6278             x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
6279         }
6280
6281       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6282          shift and AND produces only copies of the sign bit (C2 is one less
6283          than a power of two), we can do this with just a shift.  */
6284
6285       if (GET_CODE (x) == LSHIFTRT
6286           && GET_CODE (XEXP (x, 1)) == CONST_INT
6287           && ((INTVAL (XEXP (x, 1))
6288                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6289               >= GET_MODE_BITSIZE (GET_MODE (x)))
6290           && exact_log2 (mask + 1) >= 0
6291           && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6292               >= exact_log2 (mask + 1)))
6293         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6294                         GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6295                                  - exact_log2 (mask + 1)));
6296       break;
6297
6298     case ASHIFTRT:
6299       /* If we are just looking for the sign bit, we don't need this shift at
6300          all, even if it has a variable count.  */
6301       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6302           && (mask == ((HOST_WIDE_INT) 1
6303                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
6304         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6305
6306       /* If this is a shift by a constant, get a mask that contains those bits
6307          that are not copies of the sign bit.  We then have two cases:  If
6308          MASK only includes those bits, this can be a logical shift, which may
6309          allow simplifications.  If MASK is a single-bit field not within
6310          those bits, we are requesting a copy of the sign bit and hence can
6311          shift the sign bit to the appropriate location.  */
6312
6313       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6314           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6315         {
6316           int i = -1;
6317
6318           /* If the considered data is wider then HOST_WIDE_INT, we can't
6319              represent a mask for all its bits in a single scalar.
6320              But we only care about the lower bits, so calculate these.  */
6321
6322           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
6323             {
6324               nonzero = ~ (HOST_WIDE_INT) 0;
6325
6326               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6327                  is the number of bits a full-width mask would have set.
6328                  We need only shift if these are fewer than nonzero can
6329                  hold.  If not, we must keep all bits set in nonzero.  */
6330
6331               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6332                   < HOST_BITS_PER_WIDE_INT)
6333                 nonzero >>= INTVAL (XEXP (x, 1))
6334                             + HOST_BITS_PER_WIDE_INT
6335                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
6336             }
6337           else
6338             {
6339               nonzero = GET_MODE_MASK (GET_MODE (x));
6340               nonzero >>= INTVAL (XEXP (x, 1));
6341             }
6342
6343           if ((mask & ~ nonzero) == 0
6344               || (i = exact_log2 (mask)) >= 0)
6345             {
6346               x = simplify_shift_const
6347                 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6348                  i < 0 ? INTVAL (XEXP (x, 1))
6349                  : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6350
6351               if (GET_CODE (x) != ASHIFTRT)
6352                 return force_to_mode (x, mode, mask, reg, next_select);
6353             }
6354         }
6355
6356       /* If MASK is 1, convert this to a LSHIFTRT.  This can be done
6357          even if the shift count isn't a constant.  */
6358       if (mask == 1)
6359         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6360
6361       /* If this is a sign-extension operation that just affects bits
6362          we don't care about, remove it.  Be sure the call above returned
6363          something that is still a shift.  */
6364
6365       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6366           && GET_CODE (XEXP (x, 1)) == CONST_INT
6367           && INTVAL (XEXP (x, 1)) >= 0
6368           && (INTVAL (XEXP (x, 1))
6369               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
6370           && GET_CODE (XEXP (x, 0)) == ASHIFT
6371           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6372           && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
6373         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6374                               reg, next_select);
6375
6376       break;
6377
6378     case ROTATE:
6379     case ROTATERT:
6380       /* If the shift count is constant and we can do computations
6381          in the mode of X, compute where the bits we care about are.
6382          Otherwise, we can't do anything.  Don't change the mode of
6383          the shift or propagate MODE into the shift, though.  */
6384       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6385           && INTVAL (XEXP (x, 1)) >= 0)
6386         {
6387           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6388                                             GET_MODE (x), GEN_INT (mask),
6389                                             XEXP (x, 1));
6390           if (temp && GET_CODE(temp) == CONST_INT)
6391             SUBST (XEXP (x, 0),
6392                    force_to_mode (XEXP (x, 0), GET_MODE (x),
6393                                   INTVAL (temp), reg, next_select));
6394         }
6395       break;
6396         
6397     case NEG:
6398       /* If we just want the low-order bit, the NEG isn't needed since it
6399          won't change the low-order bit.    */
6400       if (mask == 1)
6401         return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6402
6403       /* We need any bits less significant than the most significant bit in
6404          MASK since carries from those bits will affect the bits we are
6405          interested in.  */
6406       mask = fuller_mask;
6407       goto unop;
6408
6409     case NOT:
6410       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6411          same as the XOR case above.  Ensure that the constant we form is not
6412          wider than the mode of X.  */
6413
6414       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6415           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6416           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6417           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6418               < GET_MODE_BITSIZE (GET_MODE (x)))
6419           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6420         {
6421           temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6422           temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6423           x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6424
6425           return force_to_mode (x, mode, mask, reg, next_select);
6426         }
6427
6428       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6429          use the full mask inside the NOT.  */
6430       mask = fuller_mask;
6431
6432     unop:
6433       op0 = gen_lowpart_for_combine (op_mode,
6434                                      force_to_mode (XEXP (x, 0), mode, mask,
6435                                                     reg, next_select));
6436       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6437         x = gen_unary (code, op_mode, op_mode, op0);
6438       break;
6439
6440     case NE:
6441       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
6442          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
6443          which is equal to STORE_FLAG_VALUE.  */
6444       if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
6445           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
6446           && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
6447         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6448
6449       break;
6450
6451     case IF_THEN_ELSE:
6452       /* We have no way of knowing if the IF_THEN_ELSE can itself be
6453          written in a narrower mode.  We play it safe and do not do so.  */
6454
6455       SUBST (XEXP (x, 1),
6456              gen_lowpart_for_combine (GET_MODE (x),
6457                                       force_to_mode (XEXP (x, 1), mode,
6458                                                      mask, reg, next_select)));
6459       SUBST (XEXP (x, 2),
6460              gen_lowpart_for_combine (GET_MODE (x),
6461                                       force_to_mode (XEXP (x, 2), mode,
6462                                                      mask, reg,next_select)));
6463       break;
6464     }
6465
6466   /* Ensure we return a value of the proper mode.  */
6467   return gen_lowpart_for_combine (mode, x);
6468 }
6469 \f
6470 /* Return nonzero if X is an expression that has one of two values depending on
6471    whether some other value is zero or nonzero.  In that case, we return the
6472    value that is being tested, *PTRUE is set to the value if the rtx being
6473    returned has a nonzero value, and *PFALSE is set to the other alternative.
6474
6475    If we return zero, we set *PTRUE and *PFALSE to X.  */
6476
6477 static rtx
6478 if_then_else_cond (x, ptrue, pfalse)
6479      rtx x;
6480      rtx *ptrue, *pfalse;
6481 {
6482   enum machine_mode mode = GET_MODE (x);
6483   enum rtx_code code = GET_CODE (x);
6484   int size = GET_MODE_BITSIZE (mode);
6485   rtx cond0, cond1, true0, true1, false0, false1;
6486   unsigned HOST_WIDE_INT nz;
6487
6488   /* If this is a unary operation whose operand has one of two values, apply
6489      our opcode to compute those values.  */
6490   if (GET_RTX_CLASS (code) == '1'
6491       && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6492     {
6493       *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6494       *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
6495       return cond0;
6496     }
6497
6498   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
6499      make can't possibly match and would suppress other optimizations.  */
6500   else if (code == COMPARE)
6501     ;
6502
6503   /* If this is a binary operation, see if either side has only one of two
6504      values.  If either one does or if both do and they are conditional on
6505      the same value, compute the new true and false values.  */
6506   else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
6507            || GET_RTX_CLASS (code) == '<')
6508     {
6509       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
6510       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
6511
6512       if ((cond0 != 0 || cond1 != 0)
6513           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
6514         {
6515           /* If if_then_else_cond returned zero, then true/false are the
6516              same rtl.  We must copy one of them to prevent invalid rtl
6517              sharing.  */
6518           if (cond0 == 0)
6519             true0 = copy_rtx (true0);
6520           else if (cond1 == 0)
6521             true1 = copy_rtx (true1);
6522
6523           *ptrue = gen_binary (code, mode, true0, true1);
6524           *pfalse = gen_binary (code, mode, false0, false1);
6525           return cond0 ? cond0 : cond1;
6526         }
6527
6528       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
6529          operands is zero when the other is non-zero, and vice-versa,
6530          and STORE_FLAG_VALUE is 1 or -1.  */
6531
6532       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6533           && (code == PLUS || code == IOR || code == XOR || code == MINUS
6534            || code == UMAX)
6535           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6536         {
6537           rtx op0 = XEXP (XEXP (x, 0), 1);
6538           rtx op1 = XEXP (XEXP (x, 1), 1);
6539
6540           cond0 = XEXP (XEXP (x, 0), 0);
6541           cond1 = XEXP (XEXP (x, 1), 0);
6542
6543           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6544               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6545               && reversible_comparison_p (cond1)
6546               && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6547                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6548                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6549                   || ((swap_condition (GET_CODE (cond0))
6550                        == reverse_condition (GET_CODE (cond1)))
6551                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6552                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6553               && ! side_effects_p (x))
6554             {
6555               *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
6556               *pfalse = gen_binary (MULT, mode, 
6557                                     (code == MINUS 
6558                                      ? gen_unary (NEG, mode, mode, op1) : op1),
6559                                     const_true_rtx);
6560               return cond0;
6561             }
6562         }
6563
6564       /* Similarly for MULT, AND and UMIN, execpt that for these the result
6565          is always zero.  */
6566       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6567           && (code == MULT || code == AND || code == UMIN)
6568           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6569         {
6570           cond0 = XEXP (XEXP (x, 0), 0);
6571           cond1 = XEXP (XEXP (x, 1), 0);
6572
6573           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6574               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6575               && reversible_comparison_p (cond1)
6576               && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6577                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6578                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6579                   || ((swap_condition (GET_CODE (cond0))
6580                        == reverse_condition (GET_CODE (cond1)))
6581                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6582                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6583               && ! side_effects_p (x))
6584             {
6585               *ptrue = *pfalse = const0_rtx;
6586               return cond0;
6587             }
6588         }
6589     }
6590
6591   else if (code == IF_THEN_ELSE)
6592     {
6593       /* If we have IF_THEN_ELSE already, extract the condition and
6594          canonicalize it if it is NE or EQ.  */
6595       cond0 = XEXP (x, 0);
6596       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
6597       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
6598         return XEXP (cond0, 0);
6599       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
6600         {
6601           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
6602           return XEXP (cond0, 0);
6603         }
6604       else
6605         return cond0;
6606     }
6607
6608   /* If X is a normal SUBREG with both inner and outer modes integral,
6609      we can narrow both the true and false values of the inner expression,
6610      if there is a condition.  */
6611   else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
6612            && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
6613            && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
6614            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
6615                                                &true0, &false0)))
6616     {
6617       *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6618       *pfalse
6619         = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6620
6621       return cond0;
6622     }
6623
6624   /* If X is a constant, this isn't special and will cause confusions
6625      if we treat it as such.  Likewise if it is equivalent to a constant.  */
6626   else if (CONSTANT_P (x)
6627            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
6628     ;
6629
6630   /* If X is known to be either 0 or -1, those are the true and 
6631      false values when testing X.  */
6632   else if (num_sign_bit_copies (x, mode) == size)
6633     {
6634       *ptrue = constm1_rtx, *pfalse = const0_rtx;
6635       return x;
6636     }
6637
6638   /* Likewise for 0 or a single bit.  */
6639   else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
6640     {
6641       *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
6642       return x;
6643     }
6644
6645   /* Otherwise fail; show no condition with true and false values the same.  */
6646   *ptrue = *pfalse = x;
6647   return 0;
6648 }
6649 \f
6650 /* Return the value of expression X given the fact that condition COND
6651    is known to be true when applied to REG as its first operand and VAL
6652    as its second.  X is known to not be shared and so can be modified in
6653    place.
6654
6655    We only handle the simplest cases, and specifically those cases that
6656    arise with IF_THEN_ELSE expressions.  */
6657
6658 static rtx
6659 known_cond (x, cond, reg, val)
6660      rtx x;
6661      enum rtx_code cond;
6662      rtx reg, val;
6663 {
6664   enum rtx_code code = GET_CODE (x);
6665   rtx temp;
6666   char *fmt;
6667   int i, j;
6668
6669   if (side_effects_p (x))
6670     return x;
6671
6672   if (cond == EQ && rtx_equal_p (x, reg))
6673     return val;
6674
6675   /* If X is (abs REG) and we know something about REG's relationship
6676      with zero, we may be able to simplify this.  */
6677
6678   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
6679     switch (cond)
6680       {
6681       case GE:  case GT:  case EQ:
6682         return XEXP (x, 0);
6683       case LT:  case LE:
6684         return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
6685                           XEXP (x, 0));
6686       }
6687
6688   /* The only other cases we handle are MIN, MAX, and comparisons if the
6689      operands are the same as REG and VAL.  */
6690
6691   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
6692     {
6693       if (rtx_equal_p (XEXP (x, 0), val))
6694         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
6695
6696       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
6697         {
6698           if (GET_RTX_CLASS (code) == '<')
6699             return (comparison_dominates_p (cond, code) ? const_true_rtx
6700                     : (comparison_dominates_p (cond,
6701                                                reverse_condition (code))
6702                        ? const0_rtx : x));
6703
6704           else if (code == SMAX || code == SMIN
6705                    || code == UMIN || code == UMAX)
6706             {
6707               int unsignedp = (code == UMIN || code == UMAX);
6708
6709               if (code == SMAX || code == UMAX)
6710                 cond = reverse_condition (cond);
6711
6712               switch (cond)
6713                 {
6714                 case GE:   case GT:
6715                   return unsignedp ? x : XEXP (x, 1);
6716                 case LE:   case LT:
6717                   return unsignedp ? x : XEXP (x, 0);
6718                 case GEU:  case GTU:
6719                   return unsignedp ? XEXP (x, 1) : x;
6720                 case LEU:  case LTU:
6721                   return unsignedp ? XEXP (x, 0) : x;
6722                 }
6723             }
6724         }
6725     }
6726
6727   fmt = GET_RTX_FORMAT (code);
6728   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6729     {
6730       if (fmt[i] == 'e')
6731         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
6732       else if (fmt[i] == 'E')
6733         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6734           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
6735                                                 cond, reg, val));
6736     }
6737
6738   return x;
6739 }
6740 \f
6741 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
6742    assignment as a field assignment.  */
6743
6744 static int
6745 rtx_equal_for_field_assignment_p (x, y)
6746      rtx x;
6747      rtx y;
6748 {
6749   rtx last_x, last_y;
6750
6751   if (x == y || rtx_equal_p (x, y))
6752     return 1;
6753
6754   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
6755     return 0;
6756
6757   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
6758      Note that all SUBREGs of MEM are paradoxical; otherwise they
6759      would have been rewritten.  */
6760   if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
6761       && GET_CODE (SUBREG_REG (y)) == MEM
6762       && rtx_equal_p (SUBREG_REG (y),
6763                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
6764     return 1;
6765
6766   if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
6767       && GET_CODE (SUBREG_REG (x)) == MEM
6768       && rtx_equal_p (SUBREG_REG (x),
6769                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
6770     return 1;
6771
6772   last_x = get_last_value (x);
6773   last_y = get_last_value (y);
6774
6775   return ((last_x != 0
6776            && GET_CODE (last_x) != CLOBBER
6777            && rtx_equal_for_field_assignment_p (last_x, y))
6778           || (last_y != 0
6779               && GET_CODE (last_y) != CLOBBER
6780               && rtx_equal_for_field_assignment_p (x, last_y))
6781           || (last_x != 0 && last_y != 0
6782               && GET_CODE (last_x) != CLOBBER
6783               && GET_CODE (last_y) != CLOBBER
6784               && rtx_equal_for_field_assignment_p (last_x, last_y)));
6785 }
6786 \f
6787 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
6788    Return that assignment if so.
6789
6790    We only handle the most common cases.  */
6791
6792 static rtx
6793 make_field_assignment (x)
6794      rtx x;
6795 {
6796   rtx dest = SET_DEST (x);
6797   rtx src = SET_SRC (x);
6798   rtx assign;
6799   rtx rhs, lhs;
6800   HOST_WIDE_INT c1;
6801   int pos, len;
6802   rtx other;
6803   enum machine_mode mode;
6804
6805   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
6806      a clear of a one-bit field.  We will have changed it to
6807      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
6808      for a SUBREG.  */
6809
6810   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
6811       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
6812       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
6813       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
6814     {
6815       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
6816                                 1, 1, 1, 0);
6817       if (assign != 0)
6818         return gen_rtx (SET, VOIDmode, assign, const0_rtx);
6819       return x;
6820     }
6821
6822   else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
6823            && subreg_lowpart_p (XEXP (src, 0))
6824            && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0))) 
6825                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
6826            && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
6827            && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
6828            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
6829     {
6830       assign = make_extraction (VOIDmode, dest, 0,
6831                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
6832                                 1, 1, 1, 0);
6833       if (assign != 0)
6834         return gen_rtx (SET, VOIDmode, assign, const0_rtx);
6835       return x;
6836     }
6837
6838   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
6839      one-bit field.  */
6840   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
6841            && XEXP (XEXP (src, 0), 0) == const1_rtx
6842            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
6843     {
6844       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
6845                                 1, 1, 1, 0);
6846       if (assign != 0)
6847         return gen_rtx (SET, VOIDmode, assign, const1_rtx);
6848       return x;
6849     }
6850
6851   /* The other case we handle is assignments into a constant-position
6852      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
6853      a mask that has all one bits except for a group of zero bits and
6854      OTHER is known to have zeros where C1 has ones, this is such an
6855      assignment.  Compute the position and length from C1.  Shift OTHER
6856      to the appropriate position, force it to the required mode, and
6857      make the extraction.  Check for the AND in both operands.  */
6858
6859   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
6860     return x;
6861
6862   rhs = expand_compound_operation (XEXP (src, 0));
6863   lhs = expand_compound_operation (XEXP (src, 1));
6864
6865   if (GET_CODE (rhs) == AND
6866       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
6867       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
6868     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
6869   else if (GET_CODE (lhs) == AND
6870            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6871            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
6872     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
6873   else
6874     return x;
6875
6876   pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
6877   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
6878       || (GET_MODE_BITSIZE (GET_MODE (other)) <= HOST_BITS_PER_WIDE_INT
6879           && (c1 & nonzero_bits (other, GET_MODE (other))) != 0))
6880     return x;
6881
6882   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
6883   if (assign == 0)
6884     return x;
6885
6886   /* The mode to use for the source is the mode of the assignment, or of
6887      what is inside a possible STRICT_LOW_PART.  */
6888   mode = (GET_CODE (assign) == STRICT_LOW_PART 
6889           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
6890
6891   /* Shift OTHER right POS places and make it the source, restricting it
6892      to the proper length and mode.  */
6893
6894   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
6895                                              GET_MODE (src), other, pos),
6896                        mode,
6897                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
6898                        ? GET_MODE_MASK (mode)
6899                        : ((HOST_WIDE_INT) 1 << len) - 1,
6900                        dest, 0);
6901
6902   return gen_rtx_combine (SET, VOIDmode, assign, src);
6903 }
6904 \f
6905 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
6906    if so.  */
6907
6908 static rtx
6909 apply_distributive_law (x)
6910      rtx x;
6911 {
6912   enum rtx_code code = GET_CODE (x);
6913   rtx lhs, rhs, other;
6914   rtx tem;
6915   enum rtx_code inner_code;
6916
6917   /* Distributivity is not true for floating point.
6918      It can change the value.  So don't do it.
6919      -- rms and moshier@world.std.com.  */
6920   if (FLOAT_MODE_P (GET_MODE (x)))
6921     return x;
6922
6923   /* The outer operation can only be one of the following:  */
6924   if (code != IOR && code != AND && code != XOR
6925       && code != PLUS && code != MINUS)
6926     return x;
6927
6928   lhs = XEXP (x, 0), rhs = XEXP (x, 1);
6929
6930   /* If either operand is a primitive we can't do anything, so get out
6931      fast.  */
6932   if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
6933       || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
6934     return x;
6935
6936   lhs = expand_compound_operation (lhs);
6937   rhs = expand_compound_operation (rhs);
6938   inner_code = GET_CODE (lhs);
6939   if (inner_code != GET_CODE (rhs))
6940     return x;
6941
6942   /* See if the inner and outer operations distribute.  */
6943   switch (inner_code)
6944     {
6945     case LSHIFTRT:
6946     case ASHIFTRT:
6947     case AND:
6948     case IOR:
6949       /* These all distribute except over PLUS.  */
6950       if (code == PLUS || code == MINUS)
6951         return x;
6952       break;
6953
6954     case MULT:
6955       if (code != PLUS && code != MINUS)
6956         return x;
6957       break;
6958
6959     case ASHIFT:
6960       /* This is also a multiply, so it distributes over everything.  */
6961       break;
6962
6963     case SUBREG:
6964       /* Non-paradoxical SUBREGs distributes over all operations, provided
6965          the inner modes and word numbers are the same, this is an extraction
6966          of a low-order part, we don't convert an fp operation to int or
6967          vice versa, and we would not be converting a single-word
6968          operation into a multi-word operation.  The latter test is not
6969          required, but it prevents generating unneeded multi-word operations.
6970          Some of the previous tests are redundant given the latter test, but
6971          are retained because they are required for correctness.
6972
6973          We produce the result slightly differently in this case.  */
6974
6975       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
6976           || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
6977           || ! subreg_lowpart_p (lhs)
6978           || (GET_MODE_CLASS (GET_MODE (lhs))
6979               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
6980           || (GET_MODE_SIZE (GET_MODE (lhs))
6981               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
6982           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
6983         return x;
6984
6985       tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
6986                         SUBREG_REG (lhs), SUBREG_REG (rhs));
6987       return gen_lowpart_for_combine (GET_MODE (x), tem);
6988
6989     default:
6990       return x;
6991     }
6992
6993   /* Set LHS and RHS to the inner operands (A and B in the example
6994      above) and set OTHER to the common operand (C in the example).
6995      These is only one way to do this unless the inner operation is
6996      commutative.  */
6997   if (GET_RTX_CLASS (inner_code) == 'c'
6998       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
6999     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7000   else if (GET_RTX_CLASS (inner_code) == 'c'
7001            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7002     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7003   else if (GET_RTX_CLASS (inner_code) == 'c'
7004            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7005     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7006   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7007     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7008   else
7009     return x;
7010
7011   /* Form the new inner operation, seeing if it simplifies first.  */
7012   tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7013
7014   /* There is one exception to the general way of distributing:
7015      (a ^ b) | (a ^ c) -> (~a) & (b ^ c)  */
7016   if (code == XOR && inner_code == IOR)
7017     {
7018       inner_code = AND;
7019       other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
7020     }
7021
7022   /* We may be able to continuing distributing the result, so call
7023      ourselves recursively on the inner operation before forming the
7024      outer operation, which we return.  */
7025   return gen_binary (inner_code, GET_MODE (x),
7026                      apply_distributive_law (tem), other);
7027 }
7028 \f
7029 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7030    in MODE.
7031
7032    Return an equivalent form, if different from X.  Otherwise, return X.  If
7033    X is zero, we are to always construct the equivalent form.  */
7034
7035 static rtx
7036 simplify_and_const_int (x, mode, varop, constop)
7037      rtx x;
7038      enum machine_mode mode;
7039      rtx varop;
7040      unsigned HOST_WIDE_INT constop;
7041 {
7042   unsigned HOST_WIDE_INT nonzero;
7043   int width = GET_MODE_BITSIZE (mode);
7044   int i;
7045
7046   /* Simplify VAROP knowing that we will be only looking at some of the
7047      bits in it.  */
7048   varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7049
7050   /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7051      CONST_INT, we are done.  */
7052   if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7053     return varop;
7054
7055   /* See what bits may be nonzero in VAROP.  Unlike the general case of
7056      a call to nonzero_bits, here we don't care about bits outside
7057      MODE.  */
7058
7059   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7060
7061   /* If this would be an entire word for the target, but is not for
7062      the host, then sign-extend on the host so that the number will look
7063      the same way on the host that it would on the target.
7064
7065      For example, when building a 64 bit alpha hosted 32 bit sparc
7066      targeted compiler, then we want the 32 bit unsigned value -1 to be
7067      represented as a 64 bit value -1, and not as 0x00000000ffffffff.
7068      The later confuses the sparc backend.  */
7069
7070   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
7071       && (nonzero & ((HOST_WIDE_INT) 1 << (width - 1))))
7072     nonzero |= ((HOST_WIDE_INT) (-1) << width);
7073
7074   /* Turn off all bits in the constant that are known to already be zero.
7075      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7076      which is tested below.  */
7077
7078   constop &= nonzero;
7079
7080   /* If we don't have any bits left, return zero.  */
7081   if (constop == 0)
7082     return const0_rtx;
7083
7084   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7085      a power of two, we can replace this with a ASHIFT.  */
7086   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7087       && (i = exact_log2 (constop)) >= 0)
7088     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7089                                  
7090   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7091      or XOR, then try to apply the distributive law.  This may eliminate
7092      operations if either branch can be simplified because of the AND.
7093      It may also make some cases more complex, but those cases probably
7094      won't match a pattern either with or without this.  */
7095
7096   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7097     return
7098       gen_lowpart_for_combine
7099         (mode,
7100          apply_distributive_law
7101          (gen_binary (GET_CODE (varop), GET_MODE (varop),
7102                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7103                                               XEXP (varop, 0), constop),
7104                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7105                                               XEXP (varop, 1), constop))));
7106
7107   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
7108      if we already had one (just check for the simplest cases).  */
7109   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7110       && GET_MODE (XEXP (x, 0)) == mode
7111       && SUBREG_REG (XEXP (x, 0)) == varop)
7112     varop = XEXP (x, 0);
7113   else
7114     varop = gen_lowpart_for_combine (mode, varop);
7115
7116   /* If we can't make the SUBREG, try to return what we were given.  */
7117   if (GET_CODE (varop) == CLOBBER)
7118     return x ? x : varop;
7119
7120   /* If we are only masking insignificant bits, return VAROP.  */
7121   if (constop == nonzero)
7122     x = varop;
7123
7124   /* Otherwise, return an AND.  See how much, if any, of X we can use.  */
7125   else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
7126     x = gen_binary (AND, mode, varop, GEN_INT (constop));
7127
7128   else
7129     {
7130       if (GET_CODE (XEXP (x, 1)) != CONST_INT
7131           || INTVAL (XEXP (x, 1)) != constop)
7132         SUBST (XEXP (x, 1), GEN_INT (constop));
7133
7134       SUBST (XEXP (x, 0), varop);
7135     }
7136
7137   return x;
7138 }
7139 \f
7140 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7141    We don't let nonzero_bits recur into num_sign_bit_copies, because that
7142    is less useful.  We can't allow both, because that results in exponential
7143    run time recusion.  There is a nullstone testcase that triggered
7144    this.  This macro avoids accidental uses of num_sign_bit_copies.  */
7145 #define num_sign_bit_copies()
7146
7147 /* Given an expression, X, compute which bits in X can be non-zero.
7148    We don't care about bits outside of those defined in MODE.
7149
7150    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7151    a shift, AND, or zero_extract, we can do better.  */
7152
7153 static unsigned HOST_WIDE_INT
7154 nonzero_bits (x, mode)
7155      rtx x;
7156      enum machine_mode mode;
7157 {
7158   unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7159   unsigned HOST_WIDE_INT inner_nz;
7160   enum rtx_code code;
7161   int mode_width = GET_MODE_BITSIZE (mode);
7162   rtx tem;
7163
7164   /* For floating-point values, assume all bits are needed.  */
7165   if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7166     return nonzero;
7167
7168   /* If X is wider than MODE, use its mode instead.  */
7169   if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7170     {
7171       mode = GET_MODE (x);
7172       nonzero = GET_MODE_MASK (mode);
7173       mode_width = GET_MODE_BITSIZE (mode);
7174     }
7175
7176   if (mode_width > HOST_BITS_PER_WIDE_INT)
7177     /* Our only callers in this case look for single bit values.  So
7178        just return the mode mask.  Those tests will then be false.  */
7179     return nonzero;
7180
7181 #ifndef WORD_REGISTER_OPERATIONS
7182   /* If MODE is wider than X, but both are a single word for both the host
7183      and target machines, we can compute this from which bits of the 
7184      object might be nonzero in its own mode, taking into account the fact
7185      that on many CISC machines, accessing an object in a wider mode
7186      causes the high-order bits to become undefined.  So they are
7187      not known to be zero.  */
7188
7189   if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
7190       && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
7191       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7192       && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
7193     {
7194       nonzero &= nonzero_bits (x, GET_MODE (x));
7195       nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
7196       return nonzero;
7197     }
7198 #endif
7199
7200   code = GET_CODE (x);
7201   switch (code)
7202     {
7203     case REG:
7204 #ifdef POINTERS_EXTEND_UNSIGNED
7205       /* If pointers extend unsigned and this is a pointer in Pmode, say that
7206          all the bits above ptr_mode are known to be zero.  */
7207       if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
7208           && REGNO_POINTER_FLAG (REGNO (x)))
7209         nonzero &= GET_MODE_MASK (ptr_mode);
7210 #endif
7211
7212 #ifdef STACK_BOUNDARY
7213       /* If this is the stack pointer, we may know something about its
7214          alignment.  If PUSH_ROUNDING is defined, it is possible for the
7215          stack to be momentarily aligned only to that amount, so we pick
7216          the least alignment.  */
7217
7218       /* We can't check for arg_pointer_rtx here, because it is not
7219          guaranteed to have as much alignment as the stack pointer.
7220          In particular, in the Irix6 n64 ABI, the stack has 128 bit
7221          alignment but the argument pointer has only 64 bit alignment.  */
7222
7223       if (x == stack_pointer_rtx || x == frame_pointer_rtx
7224           || x == hard_frame_pointer_rtx
7225           || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7226               && REGNO (x) <= LAST_VIRTUAL_REGISTER))
7227         {
7228           int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7229
7230 #ifdef PUSH_ROUNDING
7231           if (REGNO (x) == STACK_POINTER_REGNUM)
7232             sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
7233 #endif
7234
7235           /* We must return here, otherwise we may get a worse result from
7236              one of the choices below.  There is nothing useful below as
7237              far as the stack pointer is concerned.  */
7238           return nonzero &= ~ (sp_alignment - 1);
7239         }
7240 #endif
7241
7242       /* If X is a register whose nonzero bits value is current, use it.
7243          Otherwise, if X is a register whose value we can find, use that
7244          value.  Otherwise, use the previously-computed global nonzero bits
7245          for this register.  */
7246
7247       if (reg_last_set_value[REGNO (x)] != 0
7248           && reg_last_set_mode[REGNO (x)] == mode
7249           && (reg_n_sets[REGNO (x)] == 1
7250               || reg_last_set_label[REGNO (x)] == label_tick)
7251           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7252         return reg_last_set_nonzero_bits[REGNO (x)];
7253
7254       tem = get_last_value (x);
7255
7256       if (tem)
7257         {
7258 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7259           /* If X is narrower than MODE and TEM is a non-negative
7260              constant that would appear negative in the mode of X,
7261              sign-extend it for use in reg_nonzero_bits because some
7262              machines (maybe most) will actually do the sign-extension
7263              and this is the conservative approach. 
7264
7265              ??? For 2.5, try to tighten up the MD files in this regard
7266              instead of this kludge.  */
7267
7268           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7269               && GET_CODE (tem) == CONST_INT
7270               && INTVAL (tem) > 0
7271               && 0 != (INTVAL (tem)
7272                        & ((HOST_WIDE_INT) 1
7273                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7274             tem = GEN_INT (INTVAL (tem)
7275                            | ((HOST_WIDE_INT) (-1)
7276                               << GET_MODE_BITSIZE (GET_MODE (x))));
7277 #endif
7278           return nonzero_bits (tem, mode);
7279         }
7280       else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7281         return reg_nonzero_bits[REGNO (x)] & nonzero;
7282       else
7283         return nonzero;
7284
7285     case CONST_INT:
7286 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7287       /* If X is negative in MODE, sign-extend the value.  */
7288       if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7289           && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7290         return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
7291 #endif
7292
7293       return INTVAL (x);
7294
7295     case MEM:
7296 #ifdef LOAD_EXTEND_OP
7297       /* In many, if not most, RISC machines, reading a byte from memory
7298          zeros the rest of the register.  Noticing that fact saves a lot
7299          of extra zero-extends.  */
7300       if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7301         nonzero &= GET_MODE_MASK (GET_MODE (x));
7302 #endif
7303       break;
7304
7305     case EQ:  case NE:
7306     case GT:  case GTU:
7307     case LT:  case LTU:
7308     case GE:  case GEU:
7309     case LE:  case LEU:
7310
7311       /* If this produces an integer result, we know which bits are set.
7312          Code here used to clear bits outside the mode of X, but that is
7313          now done above.  */
7314
7315       if (GET_MODE_CLASS (mode) == MODE_INT
7316           && mode_width <= HOST_BITS_PER_WIDE_INT)
7317         nonzero = STORE_FLAG_VALUE;
7318       break;
7319
7320     case NEG:
7321 #if 0
7322       /* Disabled to avoid exponential mutual recursion between nonzero_bits
7323          and num_sign_bit_copies.  */
7324       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7325           == GET_MODE_BITSIZE (GET_MODE (x)))
7326         nonzero = 1;
7327 #endif
7328
7329       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
7330         nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
7331       break;
7332
7333     case ABS:
7334 #if 0
7335       /* Disabled to avoid exponential mutual recursion between nonzero_bits
7336          and num_sign_bit_copies.  */
7337       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7338           == GET_MODE_BITSIZE (GET_MODE (x)))
7339         nonzero = 1;
7340 #endif
7341       break;
7342
7343     case TRUNCATE:
7344       nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
7345       break;
7346
7347     case ZERO_EXTEND:
7348       nonzero &= nonzero_bits (XEXP (x, 0), mode);
7349       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7350         nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7351       break;
7352
7353     case SIGN_EXTEND:
7354       /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7355          Otherwise, show all the bits in the outer mode but not the inner
7356          may be non-zero.  */
7357       inner_nz = nonzero_bits (XEXP (x, 0), mode);
7358       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7359         {
7360           inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7361           if (inner_nz &
7362               (((HOST_WIDE_INT) 1
7363                 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
7364             inner_nz |= (GET_MODE_MASK (mode)
7365                           & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7366         }
7367
7368       nonzero &= inner_nz;
7369       break;
7370
7371     case AND:
7372       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7373                   & nonzero_bits (XEXP (x, 1), mode));
7374       break;
7375
7376     case XOR:   case IOR:
7377     case UMIN:  case UMAX:  case SMIN:  case SMAX:
7378       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7379                   | nonzero_bits (XEXP (x, 1), mode));
7380       break;
7381
7382     case PLUS:  case MINUS:
7383     case MULT:
7384     case DIV:   case UDIV:
7385     case MOD:   case UMOD:
7386       /* We can apply the rules of arithmetic to compute the number of
7387          high- and low-order zero bits of these operations.  We start by
7388          computing the width (position of the highest-order non-zero bit)
7389          and the number of low-order zero bits for each value.  */
7390       {
7391         unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7392         unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7393         int width0 = floor_log2 (nz0) + 1;
7394         int width1 = floor_log2 (nz1) + 1;
7395         int low0 = floor_log2 (nz0 & -nz0);
7396         int low1 = floor_log2 (nz1 & -nz1);
7397         HOST_WIDE_INT op0_maybe_minusp
7398           = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7399         HOST_WIDE_INT op1_maybe_minusp
7400           = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7401         int result_width = mode_width;
7402         int result_low = 0;
7403
7404         switch (code)
7405           {
7406           case PLUS:
7407             result_width = MAX (width0, width1) + 1;
7408             result_low = MIN (low0, low1);
7409             break;
7410           case MINUS:
7411             result_low = MIN (low0, low1);
7412             break;
7413           case MULT:
7414             result_width = width0 + width1;
7415             result_low = low0 + low1;
7416             break;
7417           case DIV:
7418             if (! op0_maybe_minusp && ! op1_maybe_minusp)
7419               result_width = width0;
7420             break;
7421           case UDIV:
7422             result_width = width0;
7423             break;
7424           case MOD:
7425             if (! op0_maybe_minusp && ! op1_maybe_minusp)
7426               result_width = MIN (width0, width1);
7427             result_low = MIN (low0, low1);
7428             break;
7429           case UMOD:
7430             result_width = MIN (width0, width1);
7431             result_low = MIN (low0, low1);
7432             break;
7433           }
7434
7435         if (result_width < mode_width)
7436           nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
7437
7438         if (result_low > 0)
7439           nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
7440       }
7441       break;
7442
7443     case ZERO_EXTRACT:
7444       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7445           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7446         nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
7447       break;
7448
7449     case SUBREG:
7450       /* If this is a SUBREG formed for a promoted variable that has
7451          been zero-extended, we know that at least the high-order bits
7452          are zero, though others might be too.  */
7453
7454       if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
7455         nonzero = (GET_MODE_MASK (GET_MODE (x))
7456                    & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
7457
7458       /* If the inner mode is a single word for both the host and target
7459          machines, we can compute this from which bits of the inner
7460          object might be nonzero.  */
7461       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
7462           && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7463               <= HOST_BITS_PER_WIDE_INT))
7464         {
7465           nonzero &= nonzero_bits (SUBREG_REG (x), mode);
7466
7467 #ifndef WORD_REGISTER_OPERATIONS
7468           /* On many CISC machines, accessing an object in a wider mode
7469              causes the high-order bits to become undefined.  So they are
7470              not known to be zero.  */
7471           if (GET_MODE_SIZE (GET_MODE (x))
7472               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7473             nonzero |= (GET_MODE_MASK (GET_MODE (x))
7474                         & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
7475 #endif
7476         }
7477       break;
7478
7479     case ASHIFTRT:
7480     case LSHIFTRT:
7481     case ASHIFT:
7482     case ROTATE:
7483       /* The nonzero bits are in two classes: any bits within MODE
7484          that aren't in GET_MODE (x) are always significant.  The rest of the
7485          nonzero bits are those that are significant in the operand of
7486          the shift when shifted the appropriate number of bits.  This
7487          shows that high-order bits are cleared by the right shift and
7488          low-order bits by left shifts.  */
7489       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7490           && INTVAL (XEXP (x, 1)) >= 0
7491           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7492         {
7493           enum machine_mode inner_mode = GET_MODE (x);
7494           int width = GET_MODE_BITSIZE (inner_mode);
7495           int count = INTVAL (XEXP (x, 1));
7496           unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
7497           unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
7498           unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
7499           unsigned HOST_WIDE_INT outer = 0;
7500
7501           if (mode_width > width)
7502             outer = (op_nonzero & nonzero & ~ mode_mask);
7503
7504           if (code == LSHIFTRT)
7505             inner >>= count;
7506           else if (code == ASHIFTRT)
7507             {
7508               inner >>= count;
7509
7510               /* If the sign bit may have been nonzero before the shift, we
7511                  need to mark all the places it could have been copied to
7512                  by the shift as possibly nonzero.  */
7513               if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
7514                 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
7515             }
7516           else if (code == ASHIFT)
7517             inner <<= count;
7518           else
7519             inner = ((inner << (count % width)
7520                       | (inner >> (width - (count % width)))) & mode_mask);
7521
7522           nonzero &= (outer | inner);
7523         }
7524       break;
7525
7526     case FFS:
7527       /* This is at most the number of bits in the mode.  */
7528       nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
7529       break;
7530
7531     case IF_THEN_ELSE:
7532       nonzero &= (nonzero_bits (XEXP (x, 1), mode)
7533                   | nonzero_bits (XEXP (x, 2), mode));
7534       break;
7535     }
7536
7537   return nonzero;
7538 }
7539
7540 /* See the macro definition above.  */
7541 #undef num_sign_bit_copies
7542 \f
7543 /* Return the number of bits at the high-order end of X that are known to
7544    be equal to the sign bit.  X will be used in mode MODE; if MODE is
7545    VOIDmode, X will be used in its own mode.  The returned value  will always
7546    be between 1 and the number of bits in MODE.  */
7547
7548 static int
7549 num_sign_bit_copies (x, mode)
7550      rtx x;
7551      enum machine_mode mode;
7552 {
7553   enum rtx_code code = GET_CODE (x);
7554   int bitwidth;
7555   int num0, num1, result;
7556   unsigned HOST_WIDE_INT nonzero;
7557   rtx tem;
7558
7559   /* If we weren't given a mode, use the mode of X.  If the mode is still
7560      VOIDmode, we don't know anything.  Likewise if one of the modes is
7561      floating-point.  */
7562
7563   if (mode == VOIDmode)
7564     mode = GET_MODE (x);
7565
7566   if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
7567     return 1;
7568
7569   bitwidth = GET_MODE_BITSIZE (mode);
7570
7571   /* For a smaller object, just ignore the high bits.  */
7572   if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
7573     return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
7574                     - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
7575      
7576 #ifndef WORD_REGISTER_OPERATIONS
7577   /* If this machine does not do all register operations on the entire
7578      register and MODE is wider than the mode of X, we can say nothing
7579      at all about the high-order bits.  */
7580   if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
7581     return 1;
7582 #endif
7583
7584   switch (code)
7585     {
7586     case REG:
7587
7588 #ifdef POINTERS_EXTEND_UNSIGNED
7589       /* If pointers extend signed and this is a pointer in Pmode, say that
7590          all the bits above ptr_mode are known to be sign bit copies.  */
7591       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
7592           && REGNO_POINTER_FLAG (REGNO (x)))
7593         return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
7594 #endif
7595
7596       if (reg_last_set_value[REGNO (x)] != 0
7597           && reg_last_set_mode[REGNO (x)] == mode
7598           && (reg_n_sets[REGNO (x)] == 1
7599               || reg_last_set_label[REGNO (x)] == label_tick)
7600           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7601         return reg_last_set_sign_bit_copies[REGNO (x)];
7602
7603       tem =  get_last_value (x);
7604       if (tem != 0)
7605         return num_sign_bit_copies (tem, mode);
7606
7607       if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
7608         return reg_sign_bit_copies[REGNO (x)];
7609       break;
7610
7611     case MEM:
7612 #ifdef LOAD_EXTEND_OP
7613       /* Some RISC machines sign-extend all loads of smaller than a word.  */
7614       if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
7615         return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
7616 #endif
7617       break;
7618
7619     case CONST_INT:
7620       /* If the constant is negative, take its 1's complement and remask.
7621          Then see how many zero bits we have.  */
7622       nonzero = INTVAL (x) & GET_MODE_MASK (mode);
7623       if (bitwidth <= HOST_BITS_PER_WIDE_INT
7624           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7625         nonzero = (~ nonzero) & GET_MODE_MASK (mode);
7626
7627       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
7628
7629     case SUBREG:
7630       /* If this is a SUBREG for a promoted object that is sign-extended
7631          and we are looking at it in a wider mode, we know that at least the
7632          high-order bits are known to be sign bit copies.  */
7633
7634       if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
7635         return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
7636                     num_sign_bit_copies (SUBREG_REG (x), mode));
7637
7638       /* For a smaller object, just ignore the high bits.  */
7639       if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
7640         {
7641           num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
7642           return MAX (1, (num0
7643                           - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7644                              - bitwidth)));
7645         }
7646
7647 #ifdef WORD_REGISTER_OPERATIONS
7648 #ifdef LOAD_EXTEND_OP
7649       /* For paradoxical SUBREGs on machines where all register operations
7650          affect the entire register, just look inside.  Note that we are
7651          passing MODE to the recursive call, so the number of sign bit copies
7652          will remain relative to that mode, not the inner mode.  */
7653
7654       /* This works only if loads sign extend.  Otherwise, if we get a
7655          reload for the inner part, it may be loaded from the stack, and
7656          then we lose all sign bit copies that existed before the store
7657          to the stack.  */
7658
7659       if ((GET_MODE_SIZE (GET_MODE (x))
7660            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7661           && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
7662         return num_sign_bit_copies (SUBREG_REG (x), mode);
7663 #endif
7664 #endif
7665       break;
7666
7667     case SIGN_EXTRACT:
7668       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7669         return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
7670       break;
7671
7672     case SIGN_EXTEND: 
7673       return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7674               + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
7675
7676     case TRUNCATE:
7677       /* For a smaller object, just ignore the high bits.  */
7678       num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
7679       return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7680                               - bitwidth)));
7681
7682     case NOT:
7683       return num_sign_bit_copies (XEXP (x, 0), mode);
7684
7685     case ROTATE:       case ROTATERT:
7686       /* If we are rotating left by a number of bits less than the number
7687          of sign bit copies, we can just subtract that amount from the
7688          number.  */
7689       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7690           && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
7691         {
7692           num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7693           return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
7694                                  : bitwidth - INTVAL (XEXP (x, 1))));
7695         }
7696       break;
7697
7698     case NEG:
7699       /* In general, this subtracts one sign bit copy.  But if the value
7700          is known to be positive, the number of sign bit copies is the
7701          same as that of the input.  Finally, if the input has just one bit
7702          that might be nonzero, all the bits are copies of the sign bit.  */
7703       nonzero = nonzero_bits (XEXP (x, 0), mode);
7704       if (nonzero == 1)
7705         return bitwidth;
7706
7707       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7708       if (num0 > 1
7709           && bitwidth <= HOST_BITS_PER_WIDE_INT
7710           && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
7711         num0--;
7712
7713       return num0;
7714
7715     case IOR:   case AND:   case XOR:
7716     case SMIN:  case SMAX:  case UMIN:  case UMAX:
7717       /* Logical operations will preserve the number of sign-bit copies.
7718          MIN and MAX operations always return one of the operands.  */
7719       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7720       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7721       return MIN (num0, num1);
7722
7723     case PLUS:  case MINUS:
7724       /* For addition and subtraction, we can have a 1-bit carry.  However,
7725          if we are subtracting 1 from a positive number, there will not
7726          be such a carry.  Furthermore, if the positive number is known to
7727          be 0 or 1, we know the result is either -1 or 0.  */
7728
7729       if (code == PLUS && XEXP (x, 1) == constm1_rtx
7730           && bitwidth <= HOST_BITS_PER_WIDE_INT)
7731         {
7732           nonzero = nonzero_bits (XEXP (x, 0), mode);
7733           if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
7734             return (nonzero == 1 || nonzero == 0 ? bitwidth
7735                     : bitwidth - floor_log2 (nonzero) - 1);
7736         }
7737
7738       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7739       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7740       return MAX (1, MIN (num0, num1) - 1);
7741       
7742     case MULT:
7743       /* The number of bits of the product is the sum of the number of
7744          bits of both terms.  However, unless one of the terms if known
7745          to be positive, we must allow for an additional bit since negating
7746          a negative number can remove one sign bit copy.  */
7747
7748       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7749       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7750
7751       result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
7752       if (result > 0
7753           && bitwidth <= HOST_BITS_PER_WIDE_INT
7754           && ((nonzero_bits (XEXP (x, 0), mode)
7755                & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7756           && ((nonzero_bits (XEXP (x, 1), mode)
7757               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
7758         result--;
7759
7760       return MAX (1, result);
7761
7762     case UDIV:
7763       /* The result must be <= the first operand.  */
7764       return num_sign_bit_copies (XEXP (x, 0), mode);
7765
7766     case UMOD:
7767       /* The result must be <= the scond operand.  */
7768       return num_sign_bit_copies (XEXP (x, 1), mode);
7769
7770     case DIV:
7771       /* Similar to unsigned division, except that we have to worry about
7772          the case where the divisor is negative, in which case we have
7773          to add 1.  */
7774       result = num_sign_bit_copies (XEXP (x, 0), mode);
7775       if (result > 1
7776           && bitwidth <= HOST_BITS_PER_WIDE_INT
7777           && (nonzero_bits (XEXP (x, 1), mode)
7778               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7779         result --;
7780
7781       return result;
7782
7783     case MOD:
7784       result = num_sign_bit_copies (XEXP (x, 1), mode);
7785       if (result > 1
7786           && bitwidth <= HOST_BITS_PER_WIDE_INT
7787           && (nonzero_bits (XEXP (x, 1), mode)
7788               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7789         result --;
7790
7791       return result;
7792
7793     case ASHIFTRT:
7794       /* Shifts by a constant add to the number of bits equal to the
7795          sign bit.  */
7796       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7797       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7798           && INTVAL (XEXP (x, 1)) > 0)
7799         num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
7800
7801       return num0;
7802
7803     case ASHIFT:
7804       /* Left shifts destroy copies.  */
7805       if (GET_CODE (XEXP (x, 1)) != CONST_INT
7806           || INTVAL (XEXP (x, 1)) < 0
7807           || INTVAL (XEXP (x, 1)) >= bitwidth)
7808         return 1;
7809
7810       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7811       return MAX (1, num0 - INTVAL (XEXP (x, 1)));
7812
7813     case IF_THEN_ELSE:
7814       num0 = num_sign_bit_copies (XEXP (x, 1), mode);
7815       num1 = num_sign_bit_copies (XEXP (x, 2), mode);
7816       return MIN (num0, num1);
7817
7818     case EQ:  case NE:  case GE:  case GT:  case LE:  case LT:
7819     case GEU: case GTU: case LEU: case LTU:
7820       if (STORE_FLAG_VALUE == -1)
7821         return bitwidth;
7822     }
7823
7824   /* If we haven't been able to figure it out by one of the above rules,
7825      see if some of the high-order bits are known to be zero.  If so,
7826      count those bits and return one less than that amount.  If we can't
7827      safely compute the mask for this mode, always return BITWIDTH.  */
7828
7829   if (bitwidth > HOST_BITS_PER_WIDE_INT)
7830     return 1;
7831
7832   nonzero = nonzero_bits (x, mode);
7833   return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
7834           ? 1 : bitwidth - floor_log2 (nonzero) - 1);
7835 }
7836 \f
7837 /* Return the number of "extended" bits there are in X, when interpreted
7838    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
7839    unsigned quantities, this is the number of high-order zero bits.
7840    For signed quantities, this is the number of copies of the sign bit
7841    minus 1.  In both case, this function returns the number of "spare"
7842    bits.  For example, if two quantities for which this function returns
7843    at least 1 are added, the addition is known not to overflow.
7844
7845    This function will always return 0 unless called during combine, which
7846    implies that it must be called from a define_split.  */
7847
7848 int
7849 extended_count (x, mode, unsignedp)
7850      rtx x;
7851      enum machine_mode mode;
7852      int unsignedp;
7853 {
7854   if (nonzero_sign_valid == 0)
7855     return 0;
7856
7857   return (unsignedp
7858           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7859              && (GET_MODE_BITSIZE (mode) - 1
7860                  - floor_log2 (nonzero_bits (x, mode))))
7861           : num_sign_bit_copies (x, mode) - 1);
7862 }
7863 \f
7864 /* This function is called from `simplify_shift_const' to merge two
7865    outer operations.  Specifically, we have already found that we need
7866    to perform operation *POP0 with constant *PCONST0 at the outermost
7867    position.  We would now like to also perform OP1 with constant CONST1
7868    (with *POP0 being done last).
7869
7870    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
7871    the resulting operation.  *PCOMP_P is set to 1 if we would need to 
7872    complement the innermost operand, otherwise it is unchanged.
7873
7874    MODE is the mode in which the operation will be done.  No bits outside
7875    the width of this mode matter.  It is assumed that the width of this mode
7876    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
7877
7878    If *POP0 or OP1 are NIL, it means no operation is required.  Only NEG, PLUS,
7879    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
7880    result is simply *PCONST0.
7881
7882    If the resulting operation cannot be expressed as one operation, we
7883    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
7884
7885 static int
7886 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
7887      enum rtx_code *pop0;
7888      HOST_WIDE_INT *pconst0;
7889      enum rtx_code op1;
7890      HOST_WIDE_INT const1;
7891      enum machine_mode mode;
7892      int *pcomp_p;
7893 {
7894   enum rtx_code op0 = *pop0;
7895   HOST_WIDE_INT const0 = *pconst0;
7896   int width = GET_MODE_BITSIZE (mode);
7897
7898   const0 &= GET_MODE_MASK (mode);
7899   const1 &= GET_MODE_MASK (mode);
7900
7901   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
7902   if (op0 == AND)
7903     const1 &= const0;
7904
7905   /* If OP0 or OP1 is NIL, this is easy.  Similarly if they are the same or
7906      if OP0 is SET.  */
7907
7908   if (op1 == NIL || op0 == SET)
7909     return 1;
7910
7911   else if (op0 == NIL)
7912     op0 = op1, const0 = const1;
7913
7914   else if (op0 == op1)
7915     {
7916       switch (op0)
7917         {
7918         case AND:
7919           const0 &= const1;
7920           break;
7921         case IOR:
7922           const0 |= const1;
7923           break;
7924         case XOR:
7925           const0 ^= const1;
7926           break;
7927         case PLUS:
7928           const0 += const1;
7929           break;
7930         case NEG:
7931           op0 = NIL;
7932           break;
7933         }
7934     }
7935
7936   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
7937   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
7938     return 0;
7939
7940   /* If the two constants aren't the same, we can't do anything.  The
7941      remaining six cases can all be done.  */
7942   else if (const0 != const1)
7943     return 0;
7944
7945   else
7946     switch (op0)
7947       {
7948       case IOR:
7949         if (op1 == AND)
7950           /* (a & b) | b == b */
7951           op0 = SET;
7952         else /* op1 == XOR */
7953           /* (a ^ b) | b == a | b */
7954           ;
7955         break;
7956
7957       case XOR:
7958         if (op1 == AND)
7959           /* (a & b) ^ b == (~a) & b */
7960           op0 = AND, *pcomp_p = 1;
7961         else /* op1 == IOR */
7962           /* (a | b) ^ b == a & ~b */
7963           op0 = AND, *pconst0 = ~ const0;
7964         break;
7965
7966       case AND:
7967         if (op1 == IOR)
7968           /* (a | b) & b == b */
7969         op0 = SET;
7970         else /* op1 == XOR */
7971           /* (a ^ b) & b) == (~a) & b */
7972           *pcomp_p = 1;
7973         break;
7974       }
7975
7976   /* Check for NO-OP cases.  */
7977   const0 &= GET_MODE_MASK (mode);
7978   if (const0 == 0
7979       && (op0 == IOR || op0 == XOR || op0 == PLUS))
7980     op0 = NIL;
7981   else if (const0 == 0 && op0 == AND)
7982     op0 = SET;
7983   else if (const0 == GET_MODE_MASK (mode) && op0 == AND)
7984     op0 = NIL;
7985
7986   /* If this would be an entire word for the target, but is not for
7987      the host, then sign-extend on the host so that the number will look
7988      the same way on the host that it would on the target.
7989
7990      For example, when building a 64 bit alpha hosted 32 bit sparc
7991      targeted compiler, then we want the 32 bit unsigned value -1 to be
7992      represented as a 64 bit value -1, and not as 0x00000000ffffffff.
7993      The later confuses the sparc backend.  */
7994
7995   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
7996       && (const0 & ((HOST_WIDE_INT) 1 << (width - 1))))
7997     const0 |= ((HOST_WIDE_INT) (-1) << width);
7998
7999   *pop0 = op0;
8000   *pconst0 = const0;
8001
8002   return 1;
8003 }
8004 \f
8005 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
8006    The result of the shift is RESULT_MODE.  X, if non-zero, is an expression
8007    that we started with.
8008
8009    The shift is normally computed in the widest mode we find in VAROP, as
8010    long as it isn't a different number of words than RESULT_MODE.  Exceptions
8011    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
8012
8013 static rtx
8014 simplify_shift_const (x, code, result_mode, varop, count)
8015      rtx x;
8016      enum rtx_code code;
8017      enum machine_mode result_mode;
8018      rtx varop;
8019      int count;
8020 {
8021   enum rtx_code orig_code = code;
8022   int orig_count = count;
8023   enum machine_mode mode = result_mode;
8024   enum machine_mode shift_mode, tmode;
8025   int mode_words
8026     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8027   /* We form (outer_op (code varop count) (outer_const)).  */
8028   enum rtx_code outer_op = NIL;
8029   HOST_WIDE_INT outer_const = 0;
8030   rtx const_rtx;
8031   int complement_p = 0;
8032   rtx new;
8033
8034   /* If we were given an invalid count, don't do anything except exactly
8035      what was requested.  */
8036
8037   if (count < 0 || count > GET_MODE_BITSIZE (mode))
8038     {
8039       if (x)
8040         return x;
8041
8042       return gen_rtx (code, mode, varop, GEN_INT (count));
8043     }
8044
8045   /* Unless one of the branches of the `if' in this loop does a `continue',
8046      we will `break' the loop after the `if'.  */
8047
8048   while (count != 0)
8049     {
8050       /* If we have an operand of (clobber (const_int 0)), just return that
8051          value.  */
8052       if (GET_CODE (varop) == CLOBBER)
8053         return varop;
8054
8055       /* If we discovered we had to complement VAROP, leave.  Making a NOT
8056          here would cause an infinite loop.  */
8057       if (complement_p)
8058         break;
8059
8060       /* Convert ROTATERT to ROTATE.  */
8061       if (code == ROTATERT)
8062         code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8063
8064       /* We need to determine what mode we will do the shift in.  If the
8065          shift is a right shift or a ROTATE, we must always do it in the mode
8066          it was originally done in.  Otherwise, we can do it in MODE, the
8067          widest mode encountered.  */
8068       shift_mode
8069         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8070            ? result_mode : mode);
8071
8072       /* Handle cases where the count is greater than the size of the mode
8073          minus 1.  For ASHIFT, use the size minus one as the count (this can
8074          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
8075          take the count modulo the size.  For other shifts, the result is
8076          zero.
8077
8078          Since these shifts are being produced by the compiler by combining
8079          multiple operations, each of which are defined, we know what the
8080          result is supposed to be.  */
8081          
8082       if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8083         {
8084           if (code == ASHIFTRT)
8085             count = GET_MODE_BITSIZE (shift_mode) - 1;
8086           else if (code == ROTATE || code == ROTATERT)
8087             count %= GET_MODE_BITSIZE (shift_mode);
8088           else
8089             {
8090               /* We can't simply return zero because there may be an
8091                  outer op.  */
8092               varop = const0_rtx;
8093               count = 0;
8094               break;
8095             }
8096         }
8097
8098       /* Negative counts are invalid and should not have been made (a
8099          programmer-specified negative count should have been handled
8100          above).  */
8101       else if (count < 0)
8102         abort ();
8103
8104       /* An arithmetic right shift of a quantity known to be -1 or 0
8105          is a no-op.  */
8106       if (code == ASHIFTRT
8107           && (num_sign_bit_copies (varop, shift_mode)
8108               == GET_MODE_BITSIZE (shift_mode)))
8109         {
8110           count = 0;
8111           break;
8112         }
8113
8114       /* If we are doing an arithmetic right shift and discarding all but
8115          the sign bit copies, this is equivalent to doing a shift by the
8116          bitsize minus one.  Convert it into that shift because it will often
8117          allow other simplifications.  */
8118
8119       if (code == ASHIFTRT
8120           && (count + num_sign_bit_copies (varop, shift_mode)
8121               >= GET_MODE_BITSIZE (shift_mode)))
8122         count = GET_MODE_BITSIZE (shift_mode) - 1;
8123
8124       /* We simplify the tests below and elsewhere by converting
8125          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8126          `make_compound_operation' will convert it to a ASHIFTRT for
8127          those machines (such as Vax) that don't have a LSHIFTRT.  */
8128       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8129           && code == ASHIFTRT
8130           && ((nonzero_bits (varop, shift_mode)
8131                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8132               == 0))
8133         code = LSHIFTRT;
8134
8135       switch (GET_CODE (varop))
8136         {
8137         case SIGN_EXTEND:
8138         case ZERO_EXTEND:
8139         case SIGN_EXTRACT:
8140         case ZERO_EXTRACT:
8141           new = expand_compound_operation (varop);
8142           if (new != varop)
8143             {
8144               varop = new;
8145               continue;
8146             }
8147           break;
8148
8149         case MEM:
8150           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8151              minus the width of a smaller mode, we can do this with a
8152              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
8153           if ((code == ASHIFTRT || code == LSHIFTRT)
8154               && ! mode_dependent_address_p (XEXP (varop, 0))
8155               && ! MEM_VOLATILE_P (varop)
8156               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8157                                          MODE_INT, 1)) != BLKmode)
8158             {
8159               if (BYTES_BIG_ENDIAN)
8160                 new = gen_rtx (MEM, tmode, XEXP (varop, 0));
8161               else
8162                 new = gen_rtx (MEM, tmode,
8163                                plus_constant (XEXP (varop, 0),
8164                                               count / BITS_PER_UNIT));
8165               RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
8166               MEM_VOLATILE_P (new) = MEM_VOLATILE_P (varop);
8167               MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (varop);
8168               varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8169                                        : ZERO_EXTEND, mode, new);
8170               count = 0;
8171               continue;
8172             }
8173           break;
8174
8175         case USE:
8176           /* Similar to the case above, except that we can only do this if
8177              the resulting mode is the same as that of the underlying
8178              MEM and adjust the address depending on the *bits* endianness
8179              because of the way that bit-field extract insns are defined.  */
8180           if ((code == ASHIFTRT || code == LSHIFTRT)
8181               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8182                                          MODE_INT, 1)) != BLKmode
8183               && tmode == GET_MODE (XEXP (varop, 0)))
8184             {
8185               if (BITS_BIG_ENDIAN)
8186                 new = XEXP (varop, 0);
8187               else
8188                 {
8189                   new = copy_rtx (XEXP (varop, 0));
8190                   SUBST (XEXP (new, 0), 
8191                          plus_constant (XEXP (new, 0),
8192                                         count / BITS_PER_UNIT));
8193                 }
8194
8195               varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8196                                        : ZERO_EXTEND, mode, new);
8197               count = 0;
8198               continue;
8199             }
8200           break;
8201
8202         case SUBREG:
8203           /* If VAROP is a SUBREG, strip it as long as the inner operand has
8204              the same number of words as what we've seen so far.  Then store
8205              the widest mode in MODE.  */
8206           if (subreg_lowpart_p (varop)
8207               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8208                   > GET_MODE_SIZE (GET_MODE (varop)))
8209               && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8210                     + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8211                   == mode_words))
8212             {
8213               varop = SUBREG_REG (varop);
8214               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8215                 mode = GET_MODE (varop);
8216               continue;
8217             }
8218           break;
8219
8220         case MULT:
8221           /* Some machines use MULT instead of ASHIFT because MULT
8222              is cheaper.  But it is still better on those machines to
8223              merge two shifts into one.  */
8224           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8225               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8226             {
8227               varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
8228                                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));;
8229               continue;
8230             }
8231           break;
8232
8233         case UDIV:
8234           /* Similar, for when divides are cheaper.  */
8235           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8236               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8237             {
8238               varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
8239                                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8240               continue;
8241             }
8242           break;
8243
8244         case ASHIFTRT:
8245           /* If we are extracting just the sign bit of an arithmetic right 
8246              shift, that shift is not needed.  */
8247           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
8248             {
8249               varop = XEXP (varop, 0);
8250               continue;
8251             }
8252
8253           /* ... fall through ...  */
8254
8255         case LSHIFTRT:
8256         case ASHIFT:
8257         case ROTATE:
8258           /* Here we have two nested shifts.  The result is usually the
8259              AND of a new shift with a mask.  We compute the result below.  */
8260           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8261               && INTVAL (XEXP (varop, 1)) >= 0
8262               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8263               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8264               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8265             {
8266               enum rtx_code first_code = GET_CODE (varop);
8267               int first_count = INTVAL (XEXP (varop, 1));
8268               unsigned HOST_WIDE_INT mask;
8269               rtx mask_rtx;
8270
8271               /* We have one common special case.  We can't do any merging if
8272                  the inner code is an ASHIFTRT of a smaller mode.  However, if
8273                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8274                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8275                  we can convert it to
8276                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8277                  This simplifies certain SIGN_EXTEND operations.  */
8278               if (code == ASHIFT && first_code == ASHIFTRT
8279                   && (GET_MODE_BITSIZE (result_mode)
8280                       - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8281                 {
8282                   /* C3 has the low-order C1 bits zero.  */
8283                   
8284                   mask = (GET_MODE_MASK (mode)
8285                           & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
8286
8287                   varop = simplify_and_const_int (NULL_RTX, result_mode,
8288                                                   XEXP (varop, 0), mask);
8289                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8290                                                 varop, count);
8291                   count = first_count;
8292                   code = ASHIFTRT;
8293                   continue;
8294                 }
8295               
8296               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8297                  than C1 high-order bits equal to the sign bit, we can convert
8298                  this to either an ASHIFT or a ASHIFTRT depending on the
8299                  two counts. 
8300
8301                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
8302
8303               if (code == ASHIFTRT && first_code == ASHIFT
8304                   && GET_MODE (varop) == shift_mode
8305                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8306                       > first_count))
8307                 {
8308                   count -= first_count;
8309                   if (count < 0)
8310                     count = - count, code = ASHIFT;
8311                   varop = XEXP (varop, 0);
8312                   continue;
8313                 }
8314
8315               /* There are some cases we can't do.  If CODE is ASHIFTRT,
8316                  we can only do this if FIRST_CODE is also ASHIFTRT.
8317
8318                  We can't do the case when CODE is ROTATE and FIRST_CODE is
8319                  ASHIFTRT.
8320
8321                  If the mode of this shift is not the mode of the outer shift,
8322                  we can't do this if either shift is a right shift or ROTATE.
8323
8324                  Finally, we can't do any of these if the mode is too wide
8325                  unless the codes are the same.
8326
8327                  Handle the case where the shift codes are the same
8328                  first.  */
8329
8330               if (code == first_code)
8331                 {
8332                   if (GET_MODE (varop) != result_mode
8333                       && (code == ASHIFTRT || code == LSHIFTRT
8334                           || code == ROTATE))
8335                     break;
8336
8337                   count += first_count;
8338                   varop = XEXP (varop, 0);
8339                   continue;
8340                 }
8341
8342               if (code == ASHIFTRT
8343                   || (code == ROTATE && first_code == ASHIFTRT)
8344                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8345                   || (GET_MODE (varop) != result_mode
8346                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
8347                           || first_code == ROTATE
8348                           || code == ROTATE)))
8349                 break;
8350
8351               /* To compute the mask to apply after the shift, shift the
8352                  nonzero bits of the inner shift the same way the 
8353                  outer shift will.  */
8354
8355               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8356
8357               mask_rtx
8358                 = simplify_binary_operation (code, result_mode, mask_rtx,
8359                                              GEN_INT (count));
8360                                   
8361               /* Give up if we can't compute an outer operation to use.  */
8362               if (mask_rtx == 0
8363                   || GET_CODE (mask_rtx) != CONST_INT
8364                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
8365                                         INTVAL (mask_rtx),
8366                                         result_mode, &complement_p))
8367                 break;
8368
8369               /* If the shifts are in the same direction, we add the
8370                  counts.  Otherwise, we subtract them.  */
8371               if ((code == ASHIFTRT || code == LSHIFTRT)
8372                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8373                 count += first_count;
8374               else
8375                 count -= first_count;
8376
8377               /* If COUNT is positive, the new shift is usually CODE, 
8378                  except for the two exceptions below, in which case it is
8379                  FIRST_CODE.  If the count is negative, FIRST_CODE should
8380                  always be used  */
8381               if (count > 0
8382                   && ((first_code == ROTATE && code == ASHIFT)
8383                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
8384                 code = first_code;
8385               else if (count < 0)
8386                 code = first_code, count = - count;
8387
8388               varop = XEXP (varop, 0);
8389               continue;
8390             }
8391
8392           /* If we have (A << B << C) for any shift, we can convert this to
8393              (A << C << B).  This wins if A is a constant.  Only try this if
8394              B is not a constant.  */
8395
8396           else if (GET_CODE (varop) == code
8397                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
8398                    && 0 != (new
8399                             = simplify_binary_operation (code, mode,
8400                                                          XEXP (varop, 0),
8401                                                          GEN_INT (count))))
8402             {
8403               varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8404               count = 0;
8405               continue;
8406             }
8407           break;
8408
8409         case NOT:
8410           /* Make this fit the case below.  */
8411           varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
8412                                    GEN_INT (GET_MODE_MASK (mode)));
8413           continue;
8414
8415         case IOR:
8416         case AND:
8417         case XOR:
8418           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8419              with C the size of VAROP - 1 and the shift is logical if
8420              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8421              we have an (le X 0) operation.   If we have an arithmetic shift
8422              and STORE_FLAG_VALUE is 1 or we have a logical shift with
8423              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
8424
8425           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8426               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8427               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8428               && (code == LSHIFTRT || code == ASHIFTRT)
8429               && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8430               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8431             {
8432               count = 0;
8433               varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8434                                        const0_rtx);
8435
8436               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8437                 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8438
8439               continue;
8440             }
8441
8442           /* If we have (shift (logical)), move the logical to the outside
8443              to allow it to possibly combine with another logical and the
8444              shift to combine with another shift.  This also canonicalizes to
8445              what a ZERO_EXTRACT looks like.  Also, some machines have
8446              (and (shift)) insns.  */
8447
8448           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8449               && (new = simplify_binary_operation (code, result_mode,
8450                                                    XEXP (varop, 1),
8451                                                    GEN_INT (count))) != 0
8452               && GET_CODE(new) == CONST_INT
8453               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8454                                   INTVAL (new), result_mode, &complement_p))
8455             {
8456               varop = XEXP (varop, 0);
8457               continue;
8458             }
8459
8460           /* If we can't do that, try to simplify the shift in each arm of the
8461              logical expression, make a new logical expression, and apply
8462              the inverse distributive law.  */
8463           {
8464             rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8465                                             XEXP (varop, 0), count);
8466             rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8467                                             XEXP (varop, 1), count);
8468
8469             varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
8470             varop = apply_distributive_law (varop);
8471
8472             count = 0;
8473           }
8474           break;
8475
8476         case EQ:
8477           /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8478              says that the sign bit can be tested, FOO has mode MODE, C is
8479              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8480              that may be nonzero.  */
8481           if (code == LSHIFTRT
8482               && XEXP (varop, 1) == const0_rtx
8483               && GET_MODE (XEXP (varop, 0)) == result_mode
8484               && count == GET_MODE_BITSIZE (result_mode) - 1
8485               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8486               && ((STORE_FLAG_VALUE
8487                    & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
8488               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8489               && merge_outer_ops (&outer_op, &outer_const, XOR,
8490                                   (HOST_WIDE_INT) 1, result_mode,
8491                                   &complement_p))
8492             {
8493               varop = XEXP (varop, 0);
8494               count = 0;
8495               continue;
8496             }
8497           break;
8498
8499         case NEG:
8500           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8501              than the number of bits in the mode is equivalent to A.  */
8502           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8503               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
8504             {
8505               varop = XEXP (varop, 0);
8506               count = 0;
8507               continue;
8508             }
8509
8510           /* NEG commutes with ASHIFT since it is multiplication.  Move the
8511              NEG outside to allow shifts to combine.  */
8512           if (code == ASHIFT
8513               && merge_outer_ops (&outer_op, &outer_const, NEG,
8514                                   (HOST_WIDE_INT) 0, result_mode,
8515                                   &complement_p))
8516             {
8517               varop = XEXP (varop, 0);
8518               continue;
8519             }
8520           break;
8521
8522         case PLUS:
8523           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8524              is one less than the number of bits in the mode is
8525              equivalent to (xor A 1).  */
8526           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8527               && XEXP (varop, 1) == constm1_rtx
8528               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8529               && merge_outer_ops (&outer_op, &outer_const, XOR,
8530                                   (HOST_WIDE_INT) 1, result_mode,
8531                                   &complement_p))
8532             {
8533               count = 0;
8534               varop = XEXP (varop, 0);
8535               continue;
8536             }
8537
8538           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
8539              that might be nonzero in BAR are those being shifted out and those
8540              bits are known zero in FOO, we can replace the PLUS with FOO.
8541              Similarly in the other operand order.  This code occurs when
8542              we are computing the size of a variable-size array.  */
8543
8544           if ((code == ASHIFTRT || code == LSHIFTRT)
8545               && count < HOST_BITS_PER_WIDE_INT
8546               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8547               && (nonzero_bits (XEXP (varop, 1), result_mode)
8548                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
8549             {
8550               varop = XEXP (varop, 0);
8551               continue;
8552             }
8553           else if ((code == ASHIFTRT || code == LSHIFTRT)
8554                    && count < HOST_BITS_PER_WIDE_INT
8555                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8556                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8557                             >> count)
8558                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8559                             & nonzero_bits (XEXP (varop, 1),
8560                                                  result_mode)))
8561             {
8562               varop = XEXP (varop, 1);
8563               continue;
8564             }
8565
8566           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
8567           if (code == ASHIFT
8568               && GET_CODE (XEXP (varop, 1)) == CONST_INT
8569               && (new = simplify_binary_operation (ASHIFT, result_mode,
8570                                                    XEXP (varop, 1),
8571                                                    GEN_INT (count))) != 0
8572               && GET_CODE(new) == CONST_INT
8573               && merge_outer_ops (&outer_op, &outer_const, PLUS,
8574                                   INTVAL (new), result_mode, &complement_p))
8575             {
8576               varop = XEXP (varop, 0);
8577               continue;
8578             }
8579           break;
8580
8581         case MINUS:
8582           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
8583              with C the size of VAROP - 1 and the shift is logical if
8584              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8585              we have a (gt X 0) operation.  If the shift is arithmetic with
8586              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
8587              we have a (neg (gt X 0)) operation.  */
8588
8589           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8590               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
8591               && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8592               && (code == LSHIFTRT || code == ASHIFTRT)
8593               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8594               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
8595               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8596             {
8597               count = 0;
8598               varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
8599                                        const0_rtx);
8600
8601               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8602                 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8603
8604               continue;
8605             }
8606           break;
8607         }
8608
8609       break;
8610     }
8611
8612   /* We need to determine what mode to do the shift in.  If the shift is
8613      a right shift or ROTATE, we must always do it in the mode it was
8614      originally done in.  Otherwise, we can do it in MODE, the widest mode
8615      encountered.  The code we care about is that of the shift that will
8616      actually be done, not the shift that was originally requested.  */
8617   shift_mode
8618     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8619        ? result_mode : mode);
8620
8621   /* We have now finished analyzing the shift.  The result should be
8622      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
8623      OUTER_OP is non-NIL, it is an operation that needs to be applied
8624      to the result of the shift.  OUTER_CONST is the relevant constant,
8625      but we must turn off all bits turned off in the shift.
8626
8627      If we were passed a value for X, see if we can use any pieces of
8628      it.  If not, make new rtx.  */
8629
8630   if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
8631       && GET_CODE (XEXP (x, 1)) == CONST_INT
8632       && INTVAL (XEXP (x, 1)) == count)
8633     const_rtx = XEXP (x, 1);
8634   else
8635     const_rtx = GEN_INT (count);
8636
8637   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8638       && GET_MODE (XEXP (x, 0)) == shift_mode
8639       && SUBREG_REG (XEXP (x, 0)) == varop)
8640     varop = XEXP (x, 0);
8641   else if (GET_MODE (varop) != shift_mode)
8642     varop = gen_lowpart_for_combine (shift_mode, varop);
8643
8644   /* If we can't make the SUBREG, try to return what we were given.  */
8645   if (GET_CODE (varop) == CLOBBER)
8646     return x ? x : varop;
8647
8648   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
8649   if (new != 0)
8650     x = new;
8651   else
8652     {
8653       if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
8654         x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
8655
8656       SUBST (XEXP (x, 0), varop);
8657       SUBST (XEXP (x, 1), const_rtx);
8658     }
8659
8660   /* If we have an outer operation and we just made a shift, it is
8661      possible that we could have simplified the shift were it not
8662      for the outer operation.  So try to do the simplification
8663      recursively.  */
8664
8665   if (outer_op != NIL && GET_CODE (x) == code
8666       && GET_CODE (XEXP (x, 1)) == CONST_INT)
8667     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
8668                               INTVAL (XEXP (x, 1)));
8669
8670   /* If we were doing a LSHIFTRT in a wider mode than it was originally,
8671      turn off all the bits that the shift would have turned off.  */
8672   if (orig_code == LSHIFTRT && result_mode != shift_mode)
8673     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
8674                                 GET_MODE_MASK (result_mode) >> orig_count);
8675       
8676   /* Do the remainder of the processing in RESULT_MODE.  */
8677   x = gen_lowpart_for_combine (result_mode, x);
8678
8679   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
8680      operation.  */
8681   if (complement_p)
8682     x = gen_unary (NOT, result_mode, result_mode, x);
8683
8684   if (outer_op != NIL)
8685     {
8686       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
8687         {
8688           int width = GET_MODE_BITSIZE (result_mode);
8689
8690           outer_const &= GET_MODE_MASK (result_mode);
8691
8692           /* If this would be an entire word for the target, but is not for
8693              the host, then sign-extend on the host so that the number will
8694              look the same way on the host that it would on the target.
8695
8696              For example, when building a 64 bit alpha hosted 32 bit sparc
8697              targeted compiler, then we want the 32 bit unsigned value -1 to be
8698              represented as a 64 bit value -1, and not as 0x00000000ffffffff.
8699              The later confuses the sparc backend.  */
8700
8701           if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
8702               && (outer_const & ((HOST_WIDE_INT) 1 << (width - 1))))
8703             outer_const |= ((HOST_WIDE_INT) (-1) << width);
8704         }
8705
8706       if (outer_op == AND)
8707         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
8708       else if (outer_op == SET)
8709         /* This means that we have determined that the result is
8710            equivalent to a constant.  This should be rare.  */
8711         x = GEN_INT (outer_const);
8712       else if (GET_RTX_CLASS (outer_op) == '1')
8713         x = gen_unary (outer_op, result_mode, result_mode, x);
8714       else
8715         x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
8716     }
8717
8718   return x;
8719 }  
8720 \f
8721 /* Like recog, but we receive the address of a pointer to a new pattern.
8722    We try to match the rtx that the pointer points to.
8723    If that fails, we may try to modify or replace the pattern,
8724    storing the replacement into the same pointer object.
8725
8726    Modifications include deletion or addition of CLOBBERs.
8727
8728    PNOTES is a pointer to a location where any REG_UNUSED notes added for
8729    the CLOBBERs are placed.
8730
8731    PADDED_SCRATCHES is set to the number of (clobber (scratch)) patterns
8732    we had to add.
8733
8734    The value is the final insn code from the pattern ultimately matched,
8735    or -1.  */
8736
8737 static int
8738 recog_for_combine (pnewpat, insn, pnotes, padded_scratches)
8739      rtx *pnewpat;
8740      rtx insn;
8741      rtx *pnotes;
8742      int *padded_scratches;
8743 {
8744   register rtx pat = *pnewpat;
8745   int insn_code_number;
8746   int num_clobbers_to_add = 0;
8747   int i;
8748   rtx notes = 0;
8749
8750   *padded_scratches = 0;
8751
8752   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
8753      we use to indicate that something didn't match.  If we find such a
8754      thing, force rejection.  */
8755   if (GET_CODE (pat) == PARALLEL)
8756     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
8757       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
8758           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
8759         return -1;
8760
8761   /* Is the result of combination a valid instruction?  */
8762   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
8763
8764   /* If it isn't, there is the possibility that we previously had an insn
8765      that clobbered some register as a side effect, but the combined
8766      insn doesn't need to do that.  So try once more without the clobbers
8767      unless this represents an ASM insn.  */
8768
8769   if (insn_code_number < 0 && ! check_asm_operands (pat)
8770       && GET_CODE (pat) == PARALLEL)
8771     {
8772       int pos;
8773
8774       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
8775         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
8776           {
8777             if (i != pos)
8778               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
8779             pos++;
8780           }
8781
8782       SUBST_INT (XVECLEN (pat, 0), pos);
8783
8784       if (pos == 1)
8785         pat = XVECEXP (pat, 0, 0);
8786
8787       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
8788     }
8789
8790   /* If we had any clobbers to add, make a new pattern than contains
8791      them.  Then check to make sure that all of them are dead.  */
8792   if (num_clobbers_to_add)
8793     {
8794       rtx newpat = gen_rtx (PARALLEL, VOIDmode,
8795                             gen_rtvec (GET_CODE (pat) == PARALLEL
8796                                        ? XVECLEN (pat, 0) + num_clobbers_to_add
8797                                        : num_clobbers_to_add + 1));
8798
8799       if (GET_CODE (pat) == PARALLEL)
8800         for (i = 0; i < XVECLEN (pat, 0); i++)
8801           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
8802       else
8803         XVECEXP (newpat, 0, 0) = pat;
8804
8805       add_clobbers (newpat, insn_code_number);
8806
8807       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
8808            i < XVECLEN (newpat, 0); i++)
8809         {
8810           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
8811               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
8812             return -1;
8813           else if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == SCRATCH)
8814             (*padded_scratches)++;
8815           notes = gen_rtx (EXPR_LIST, REG_UNUSED,
8816                            XEXP (XVECEXP (newpat, 0, i), 0), notes);
8817         }
8818       pat = newpat;
8819     }
8820
8821   *pnewpat = pat;
8822   *pnotes = notes;
8823
8824   return insn_code_number;
8825 }
8826 \f
8827 /* Like gen_lowpart but for use by combine.  In combine it is not possible
8828    to create any new pseudoregs.  However, it is safe to create
8829    invalid memory addresses, because combine will try to recognize
8830    them and all they will do is make the combine attempt fail.
8831
8832    If for some reason this cannot do its job, an rtx
8833    (clobber (const_int 0)) is returned.
8834    An insn containing that will not be recognized.  */
8835
8836 #undef gen_lowpart
8837
8838 static rtx
8839 gen_lowpart_for_combine (mode, x)
8840      enum machine_mode mode;
8841      register rtx x;
8842 {
8843   rtx result;
8844
8845   if (GET_MODE (x) == mode)
8846     return x;
8847
8848   /* We can only support MODE being wider than a word if X is a
8849      constant integer or has a mode the same size.  */
8850
8851   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
8852       && ! ((GET_MODE (x) == VOIDmode
8853              && (GET_CODE (x) == CONST_INT
8854                  || GET_CODE (x) == CONST_DOUBLE))
8855             || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
8856     return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
8857
8858   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
8859      won't know what to do.  So we will strip off the SUBREG here and
8860      process normally.  */
8861   if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
8862     {
8863       x = SUBREG_REG (x);
8864       if (GET_MODE (x) == mode)
8865         return x;
8866     }
8867
8868   result = gen_lowpart_common (mode, x);
8869   if (result != 0
8870       && GET_CODE (result) == SUBREG
8871       && GET_CODE (SUBREG_REG (result)) == REG
8872       && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
8873       && (GET_MODE_SIZE (GET_MODE (result))
8874           != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
8875     reg_changes_size[REGNO (SUBREG_REG (result))] = 1;
8876
8877   if (result)
8878     return result;
8879
8880   if (GET_CODE (x) == MEM)
8881     {
8882       register int offset = 0;
8883       rtx new;
8884
8885       /* Refuse to work on a volatile memory ref or one with a mode-dependent
8886          address.  */
8887       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
8888         return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
8889
8890       /* If we want to refer to something bigger than the original memref,
8891          generate a perverse subreg instead.  That will force a reload
8892          of the original memref X.  */
8893       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
8894         return gen_rtx (SUBREG, mode, x, 0);
8895
8896       if (WORDS_BIG_ENDIAN)
8897         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
8898                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
8899       if (BYTES_BIG_ENDIAN)
8900         {
8901           /* Adjust the address so that the address-after-the-data is
8902              unchanged.  */
8903           offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
8904                      - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
8905         }
8906       new = gen_rtx (MEM, mode, plus_constant (XEXP (x, 0), offset));
8907       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
8908       MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
8909       MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
8910       return new;
8911     }
8912
8913   /* If X is a comparison operator, rewrite it in a new mode.  This
8914      probably won't match, but may allow further simplifications.  */
8915   else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
8916     return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
8917
8918   /* If we couldn't simplify X any other way, just enclose it in a
8919      SUBREG.  Normally, this SUBREG won't match, but some patterns may
8920      include an explicit SUBREG or we may simplify it further in combine.  */
8921   else
8922     {
8923       int word = 0;
8924
8925       if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
8926         word = ((GET_MODE_SIZE (GET_MODE (x))
8927                  - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
8928                 / UNITS_PER_WORD);
8929       return gen_rtx (SUBREG, mode, x, word);
8930     }
8931 }
8932 \f
8933 /* Make an rtx expression.  This is a subset of gen_rtx and only supports
8934    expressions of 1, 2, or 3 operands, each of which are rtx expressions.
8935
8936    If the identical expression was previously in the insn (in the undobuf),
8937    it will be returned.  Only if it is not found will a new expression
8938    be made.  */
8939
8940 /*VARARGS2*/
8941 static rtx
8942 gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
8943 {
8944 #ifndef __STDC__
8945   enum rtx_code code;
8946   enum machine_mode mode;
8947 #endif
8948   va_list p;
8949   int n_args;
8950   rtx args[3];
8951   int i, j;
8952   char *fmt;
8953   rtx rt;
8954   struct undo *undo;
8955
8956   VA_START (p, mode);
8957
8958 #ifndef __STDC__
8959   code = va_arg (p, enum rtx_code);
8960   mode = va_arg (p, enum machine_mode);
8961 #endif
8962
8963   n_args = GET_RTX_LENGTH (code);
8964   fmt = GET_RTX_FORMAT (code);
8965
8966   if (n_args == 0 || n_args > 3)
8967     abort ();
8968
8969   /* Get each arg and verify that it is supposed to be an expression.  */
8970   for (j = 0; j < n_args; j++)
8971     {
8972       if (*fmt++ != 'e')
8973         abort ();
8974
8975       args[j] = va_arg (p, rtx);
8976     }
8977
8978   /* See if this is in undobuf.  Be sure we don't use objects that came
8979      from another insn; this could produce circular rtl structures.  */
8980
8981   for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
8982     if (!undo->is_int
8983         && GET_CODE (undo->old_contents.r) == code
8984         && GET_MODE (undo->old_contents.r) == mode)
8985       {
8986         for (j = 0; j < n_args; j++)
8987           if (XEXP (undo->old_contents.r, j) != args[j])
8988             break;
8989
8990         if (j == n_args)
8991           return undo->old_contents.r;
8992       }
8993
8994   /* Otherwise make a new rtx.  We know we have 1, 2, or 3 args.
8995      Use rtx_alloc instead of gen_rtx because it's faster on RISC.  */
8996   rt = rtx_alloc (code);
8997   PUT_MODE (rt, mode);
8998   XEXP (rt, 0) = args[0];
8999   if (n_args > 1)
9000     {
9001       XEXP (rt, 1) = args[1];
9002       if (n_args > 2)
9003         XEXP (rt, 2) = args[2];
9004     }
9005   return rt;
9006 }
9007
9008 /* These routines make binary and unary operations by first seeing if they
9009    fold; if not, a new expression is allocated.  */
9010
9011 static rtx
9012 gen_binary (code, mode, op0, op1)
9013      enum rtx_code code;
9014      enum machine_mode mode;
9015      rtx op0, op1;
9016 {
9017   rtx result;
9018   rtx tem;
9019
9020   if (GET_RTX_CLASS (code) == 'c'
9021       && (GET_CODE (op0) == CONST_INT
9022           || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9023     tem = op0, op0 = op1, op1 = tem;
9024
9025   if (GET_RTX_CLASS (code) == '<') 
9026     {
9027       enum machine_mode op_mode = GET_MODE (op0);
9028
9029       /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get 
9030          just (REL_OP X Y).  */
9031       if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9032         {
9033           op1 = XEXP (op0, 1);
9034           op0 = XEXP (op0, 0);
9035           op_mode = GET_MODE (op0);
9036         }
9037
9038       if (op_mode == VOIDmode)
9039         op_mode = GET_MODE (op1);
9040       result = simplify_relational_operation (code, op_mode, op0, op1);
9041     }
9042   else
9043     result = simplify_binary_operation (code, mode, op0, op1);
9044
9045   if (result)
9046     return result;
9047
9048   /* Put complex operands first and constants second.  */
9049   if (GET_RTX_CLASS (code) == 'c'
9050       && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9051           || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9052               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9053           || (GET_CODE (op0) == SUBREG
9054               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9055               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9056     return gen_rtx_combine (code, mode, op1, op0);
9057
9058   return gen_rtx_combine (code, mode, op0, op1);
9059 }
9060
9061 static rtx
9062 gen_unary (code, mode, op0_mode, op0)
9063      enum rtx_code code;
9064      enum machine_mode mode, op0_mode;
9065      rtx op0;
9066 {
9067   rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
9068
9069   if (result)
9070     return result;
9071
9072   return gen_rtx_combine (code, mode, op0);
9073 }
9074 \f
9075 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9076    comparison code that will be tested.
9077
9078    The result is a possibly different comparison code to use.  *POP0 and
9079    *POP1 may be updated.
9080
9081    It is possible that we might detect that a comparison is either always
9082    true or always false.  However, we do not perform general constant
9083    folding in combine, so this knowledge isn't useful.  Such tautologies
9084    should have been detected earlier.  Hence we ignore all such cases.  */
9085
9086 static enum rtx_code
9087 simplify_comparison (code, pop0, pop1)
9088      enum rtx_code code;
9089      rtx *pop0;
9090      rtx *pop1;
9091 {
9092   rtx op0 = *pop0;
9093   rtx op1 = *pop1;
9094   rtx tem, tem1;
9095   int i;
9096   enum machine_mode mode, tmode;
9097
9098   /* Try a few ways of applying the same transformation to both operands.  */
9099   while (1)
9100     {
9101 #ifndef WORD_REGISTER_OPERATIONS
9102       /* The test below this one won't handle SIGN_EXTENDs on these machines,
9103          so check specially.  */
9104       if (code != GTU && code != GEU && code != LTU && code != LEU
9105           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9106           && GET_CODE (XEXP (op0, 0)) == ASHIFT
9107           && GET_CODE (XEXP (op1, 0)) == ASHIFT
9108           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9109           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9110           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9111               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9112           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9113           && GET_CODE (XEXP (op1, 1)) == CONST_INT
9114           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9115           && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
9116           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
9117           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
9118           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
9119           && (INTVAL (XEXP (op0, 1))
9120               == (GET_MODE_BITSIZE (GET_MODE (op0))
9121                   - (GET_MODE_BITSIZE
9122                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9123         {
9124           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9125           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9126         }
9127 #endif
9128
9129       /* If both operands are the same constant shift, see if we can ignore the
9130          shift.  We can if the shift is a rotate or if the bits shifted out of
9131          this shift are known to be zero for both inputs and if the type of
9132          comparison is compatible with the shift.  */
9133       if (GET_CODE (op0) == GET_CODE (op1)
9134           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9135           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9136               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9137                   && (code != GT && code != LT && code != GE && code != LE))
9138               || (GET_CODE (op0) == ASHIFTRT
9139                   && (code != GTU && code != LTU
9140                       && code != GEU && code != GEU)))
9141           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9142           && INTVAL (XEXP (op0, 1)) >= 0
9143           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9144           && XEXP (op0, 1) == XEXP (op1, 1))
9145         {
9146           enum machine_mode mode = GET_MODE (op0);
9147           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9148           int shift_count = INTVAL (XEXP (op0, 1));
9149
9150           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9151             mask &= (mask >> shift_count) << shift_count;
9152           else if (GET_CODE (op0) == ASHIFT)
9153             mask = (mask & (mask << shift_count)) >> shift_count;
9154
9155           if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
9156               && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
9157             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9158           else
9159             break;
9160         }
9161
9162       /* If both operands are AND's of a paradoxical SUBREG by constant, the
9163          SUBREGs are of the same mode, and, in both cases, the AND would
9164          be redundant if the comparison was done in the narrower mode,
9165          do the comparison in the narrower mode (e.g., we are AND'ing with 1
9166          and the operand's possibly nonzero bits are 0xffffff01; in that case
9167          if we only care about QImode, we don't need the AND).  This case
9168          occurs if the output mode of an scc insn is not SImode and
9169          STORE_FLAG_VALUE == 1 (e.g., the 386).
9170
9171          Similarly, check for a case where the AND's are ZERO_EXTEND
9172          operations from some narrower mode even though a SUBREG is not
9173          present.  */
9174
9175       else if  (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9176                 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9177                 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9178         {
9179           rtx inner_op0 = XEXP (op0, 0);
9180           rtx inner_op1 = XEXP (op1, 0);
9181           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9182           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9183           int changed = 0;
9184                 
9185           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9186               && (GET_MODE_SIZE (GET_MODE (inner_op0))
9187                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9188               && (GET_MODE (SUBREG_REG (inner_op0))
9189                   == GET_MODE (SUBREG_REG (inner_op1)))
9190               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9191                   <= HOST_BITS_PER_WIDE_INT)
9192               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9193                                              GET_MODE (SUBREG_REG (op0)))))
9194               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9195                                              GET_MODE (SUBREG_REG (inner_op1))))))
9196             {
9197               op0 = SUBREG_REG (inner_op0);
9198               op1 = SUBREG_REG (inner_op1);
9199
9200               /* The resulting comparison is always unsigned since we masked
9201                  off the original sign bit.  */
9202               code = unsigned_condition (code);
9203
9204               changed = 1;
9205             }
9206
9207           else if (c0 == c1)
9208             for (tmode = GET_CLASS_NARROWEST_MODE
9209                  (GET_MODE_CLASS (GET_MODE (op0)));
9210                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9211               if (c0 == GET_MODE_MASK (tmode))
9212                 {
9213                   op0 = gen_lowpart_for_combine (tmode, inner_op0);
9214                   op1 = gen_lowpart_for_combine (tmode, inner_op1);
9215                   code = unsigned_condition (code);
9216                   changed = 1;
9217                   break;
9218                 }
9219
9220           if (! changed)
9221             break;
9222         }
9223
9224       /* If both operands are NOT, we can strip off the outer operation
9225          and adjust the comparison code for swapped operands; similarly for
9226          NEG, except that this must be an equality comparison.  */
9227       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9228                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9229                    && (code == EQ || code == NE)))
9230         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9231
9232       else
9233         break;
9234     }
9235      
9236   /* If the first operand is a constant, swap the operands and adjust the
9237      comparison code appropriately, but don't do this if the second operand
9238      is already a constant integer.  */
9239   if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9240     {
9241       tem = op0, op0 = op1, op1 = tem;
9242       code = swap_condition (code);
9243     }
9244
9245   /* We now enter a loop during which we will try to simplify the comparison.
9246      For the most part, we only are concerned with comparisons with zero,
9247      but some things may really be comparisons with zero but not start
9248      out looking that way.  */
9249
9250   while (GET_CODE (op1) == CONST_INT)
9251     {
9252       enum machine_mode mode = GET_MODE (op0);
9253       int mode_width = GET_MODE_BITSIZE (mode);
9254       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9255       int equality_comparison_p;
9256       int sign_bit_comparison_p;
9257       int unsigned_comparison_p;
9258       HOST_WIDE_INT const_op;
9259
9260       /* We only want to handle integral modes.  This catches VOIDmode,
9261          CCmode, and the floating-point modes.  An exception is that we
9262          can handle VOIDmode if OP0 is a COMPARE or a comparison
9263          operation.  */
9264
9265       if (GET_MODE_CLASS (mode) != MODE_INT
9266           && ! (mode == VOIDmode
9267                 && (GET_CODE (op0) == COMPARE
9268                     || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9269         break;
9270
9271       /* Get the constant we are comparing against and turn off all bits
9272          not on in our mode.  */
9273       const_op = INTVAL (op1);
9274       if (mode_width <= HOST_BITS_PER_WIDE_INT)
9275         const_op &= mask;
9276
9277       /* If we are comparing against a constant power of two and the value
9278          being compared can only have that single bit nonzero (e.g., it was
9279          `and'ed with that bit), we can replace this with a comparison
9280          with zero.  */
9281       if (const_op
9282           && (code == EQ || code == NE || code == GE || code == GEU
9283               || code == LT || code == LTU)
9284           && mode_width <= HOST_BITS_PER_WIDE_INT
9285           && exact_log2 (const_op) >= 0
9286           && nonzero_bits (op0, mode) == const_op)
9287         {
9288           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9289           op1 = const0_rtx, const_op = 0;
9290         }
9291
9292       /* Similarly, if we are comparing a value known to be either -1 or
9293          0 with -1, change it to the opposite comparison against zero.  */
9294
9295       if (const_op == -1
9296           && (code == EQ || code == NE || code == GT || code == LE
9297               || code == GEU || code == LTU)
9298           && num_sign_bit_copies (op0, mode) == mode_width)
9299         {
9300           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9301           op1 = const0_rtx, const_op = 0;
9302         }
9303
9304       /* Do some canonicalizations based on the comparison code.  We prefer
9305          comparisons against zero and then prefer equality comparisons.  
9306          If we can reduce the size of a constant, we will do that too.  */
9307
9308       switch (code)
9309         {
9310         case LT:
9311           /* < C is equivalent to <= (C - 1) */
9312           if (const_op > 0)
9313             {
9314               const_op -= 1;
9315               op1 = GEN_INT (const_op);
9316               code = LE;
9317               /* ... fall through to LE case below.  */
9318             }
9319           else
9320             break;
9321
9322         case LE:
9323           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
9324           if (const_op < 0)
9325             {
9326               const_op += 1;
9327               op1 = GEN_INT (const_op);
9328               code = LT;
9329             }
9330
9331           /* If we are doing a <= 0 comparison on a value known to have
9332              a zero sign bit, we can replace this with == 0.  */
9333           else if (const_op == 0
9334                    && mode_width <= HOST_BITS_PER_WIDE_INT
9335                    && (nonzero_bits (op0, mode)
9336                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9337             code = EQ;
9338           break;
9339
9340         case GE:
9341           /* >= C is equivalent to > (C - 1).  */
9342           if (const_op > 0)
9343             {
9344               const_op -= 1;
9345               op1 = GEN_INT (const_op);
9346               code = GT;
9347               /* ... fall through to GT below.  */
9348             }
9349           else
9350             break;
9351
9352         case GT:
9353           /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9354           if (const_op < 0)
9355             {
9356               const_op += 1;
9357               op1 = GEN_INT (const_op);
9358               code = GE;
9359             }
9360
9361           /* If we are doing a > 0 comparison on a value known to have
9362              a zero sign bit, we can replace this with != 0.  */
9363           else if (const_op == 0
9364                    && mode_width <= HOST_BITS_PER_WIDE_INT
9365                    && (nonzero_bits (op0, mode)
9366                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9367             code = NE;
9368           break;
9369
9370         case LTU:
9371           /* < C is equivalent to <= (C - 1).  */
9372           if (const_op > 0)
9373             {
9374               const_op -= 1;
9375               op1 = GEN_INT (const_op);
9376               code = LEU;
9377               /* ... fall through ...  */
9378             }
9379
9380           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
9381           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9382                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9383             {
9384               const_op = 0, op1 = const0_rtx;
9385               code = GE;
9386               break;
9387             }
9388           else
9389             break;
9390
9391         case LEU:
9392           /* unsigned <= 0 is equivalent to == 0 */
9393           if (const_op == 0)
9394             code = EQ;
9395
9396           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
9397           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9398                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9399             {
9400               const_op = 0, op1 = const0_rtx;
9401               code = GE;
9402             }
9403           break;
9404
9405         case GEU:
9406           /* >= C is equivalent to < (C - 1).  */
9407           if (const_op > 1)
9408             {
9409               const_op -= 1;
9410               op1 = GEN_INT (const_op);
9411               code = GTU;
9412               /* ... fall through ...  */
9413             }
9414
9415           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
9416           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9417                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9418             {
9419               const_op = 0, op1 = const0_rtx;
9420               code = LT;
9421               break;
9422             }
9423           else
9424             break;
9425
9426         case GTU:
9427           /* unsigned > 0 is equivalent to != 0 */
9428           if (const_op == 0)
9429             code = NE;
9430
9431           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
9432           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9433                     && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9434             {
9435               const_op = 0, op1 = const0_rtx;
9436               code = LT;
9437             }
9438           break;
9439         }
9440
9441       /* Compute some predicates to simplify code below.  */
9442
9443       equality_comparison_p = (code == EQ || code == NE);
9444       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9445       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9446                                || code == LEU);
9447
9448       /* If this is a sign bit comparison and we can do arithmetic in
9449          MODE, say that we will only be needing the sign bit of OP0.  */
9450       if (sign_bit_comparison_p
9451           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9452         op0 = force_to_mode (op0, mode,
9453                              ((HOST_WIDE_INT) 1
9454                               << (GET_MODE_BITSIZE (mode) - 1)),
9455                              NULL_RTX, 0);
9456
9457       /* Now try cases based on the opcode of OP0.  If none of the cases
9458          does a "continue", we exit this loop immediately after the
9459          switch.  */
9460
9461       switch (GET_CODE (op0))
9462         {
9463         case ZERO_EXTRACT:
9464           /* If we are extracting a single bit from a variable position in
9465              a constant that has only a single bit set and are comparing it
9466              with zero, we can convert this into an equality comparison 
9467              between the position and the location of the single bit.  */
9468
9469           if (GET_CODE (XEXP (op0, 0)) == CONST_INT
9470               && XEXP (op0, 1) == const1_rtx
9471               && equality_comparison_p && const_op == 0
9472               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9473             {
9474               if (BITS_BIG_ENDIAN)
9475 #ifdef HAVE_extzv
9476                 i = (GET_MODE_BITSIZE
9477                      (insn_operand_mode[(int) CODE_FOR_extzv][1]) - 1 - i);
9478 #else
9479                 i = BITS_PER_WORD - 1 - i;
9480 #endif
9481
9482               op0 = XEXP (op0, 2);
9483               op1 = GEN_INT (i);
9484               const_op = i;
9485
9486               /* Result is nonzero iff shift count is equal to I.  */
9487               code = reverse_condition (code);
9488               continue;
9489             }
9490
9491           /* ... fall through ...  */
9492
9493         case SIGN_EXTRACT:
9494           tem = expand_compound_operation (op0);
9495           if (tem != op0)
9496             {
9497               op0 = tem;
9498               continue;
9499             }
9500           break;
9501
9502         case NOT:
9503           /* If testing for equality, we can take the NOT of the constant.  */
9504           if (equality_comparison_p
9505               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9506             {
9507               op0 = XEXP (op0, 0);
9508               op1 = tem;
9509               continue;
9510             }
9511
9512           /* If just looking at the sign bit, reverse the sense of the
9513              comparison.  */
9514           if (sign_bit_comparison_p)
9515             {
9516               op0 = XEXP (op0, 0);
9517               code = (code == GE ? LT : GE);
9518               continue;
9519             }
9520           break;
9521
9522         case NEG:
9523           /* If testing for equality, we can take the NEG of the constant.  */
9524           if (equality_comparison_p
9525               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9526             {
9527               op0 = XEXP (op0, 0);
9528               op1 = tem;
9529               continue;
9530             }
9531
9532           /* The remaining cases only apply to comparisons with zero.  */
9533           if (const_op != 0)
9534             break;
9535
9536           /* When X is ABS or is known positive,
9537              (neg X) is < 0 if and only if X != 0.  */
9538
9539           if (sign_bit_comparison_p
9540               && (GET_CODE (XEXP (op0, 0)) == ABS
9541                   || (mode_width <= HOST_BITS_PER_WIDE_INT
9542                       && (nonzero_bits (XEXP (op0, 0), mode)
9543                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
9544             {
9545               op0 = XEXP (op0, 0);
9546               code = (code == LT ? NE : EQ);
9547               continue;
9548             }
9549
9550           /* If we have NEG of something whose two high-order bits are the
9551              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
9552           if (num_sign_bit_copies (op0, mode) >= 2)
9553             {
9554               op0 = XEXP (op0, 0);
9555               code = swap_condition (code);
9556               continue;
9557             }
9558           break;
9559
9560         case ROTATE:
9561           /* If we are testing equality and our count is a constant, we
9562              can perform the inverse operation on our RHS.  */
9563           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9564               && (tem = simplify_binary_operation (ROTATERT, mode,
9565                                                    op1, XEXP (op0, 1))) != 0)
9566             {
9567               op0 = XEXP (op0, 0);
9568               op1 = tem;
9569               continue;
9570             }
9571
9572           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
9573              a particular bit.  Convert it to an AND of a constant of that
9574              bit.  This will be converted into a ZERO_EXTRACT.  */
9575           if (const_op == 0 && sign_bit_comparison_p
9576               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9577               && mode_width <= HOST_BITS_PER_WIDE_INT)
9578             {
9579               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9580                                             ((HOST_WIDE_INT) 1
9581                                              << (mode_width - 1
9582                                                  - INTVAL (XEXP (op0, 1)))));
9583               code = (code == LT ? NE : EQ);
9584               continue;
9585             }
9586
9587           /* ... fall through ...  */
9588
9589         case ABS:
9590           /* ABS is ignorable inside an equality comparison with zero.  */
9591           if (const_op == 0 && equality_comparison_p)
9592             {
9593               op0 = XEXP (op0, 0);
9594               continue;
9595             }
9596           break;
9597           
9598
9599         case SIGN_EXTEND:
9600           /* Can simplify (compare (zero/sign_extend FOO) CONST)
9601              to (compare FOO CONST) if CONST fits in FOO's mode and we 
9602              are either testing inequality or have an unsigned comparison
9603              with ZERO_EXTEND or a signed comparison with SIGN_EXTEND.  */
9604           if (! unsigned_comparison_p
9605               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9606                   <= HOST_BITS_PER_WIDE_INT)
9607               && ((unsigned HOST_WIDE_INT) const_op
9608                   < (((HOST_WIDE_INT) 1
9609                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
9610             {
9611               op0 = XEXP (op0, 0);
9612               continue;
9613             }
9614           break;
9615
9616         case SUBREG:
9617           /* Check for the case where we are comparing A - C1 with C2,
9618              both constants are smaller than 1/2 the maximum positive
9619              value in MODE, and the comparison is equality or unsigned.
9620              In that case, if A is either zero-extended to MODE or has
9621              sufficient sign bits so that the high-order bit in MODE
9622              is a copy of the sign in the inner mode, we can prove that it is
9623              safe to do the operation in the wider mode.  This simplifies
9624              many range checks.  */
9625
9626           if (mode_width <= HOST_BITS_PER_WIDE_INT
9627               && subreg_lowpart_p (op0)
9628               && GET_CODE (SUBREG_REG (op0)) == PLUS
9629               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
9630               && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
9631               && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
9632                   < GET_MODE_MASK (mode) / 2)
9633               && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
9634               && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
9635                                       GET_MODE (SUBREG_REG (op0)))
9636                         & ~ GET_MODE_MASK (mode))
9637                   || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
9638                                            GET_MODE (SUBREG_REG (op0)))
9639                       > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9640                          - GET_MODE_BITSIZE (mode)))))
9641             {
9642               op0 = SUBREG_REG (op0);
9643               continue;
9644             }
9645
9646           /* If the inner mode is narrower and we are extracting the low part,
9647              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
9648           if (subreg_lowpart_p (op0)
9649               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
9650             /* Fall through */ ;
9651           else
9652             break;
9653
9654           /* ... fall through ...  */
9655
9656         case ZERO_EXTEND:
9657           if ((unsigned_comparison_p || equality_comparison_p)
9658               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9659                   <= HOST_BITS_PER_WIDE_INT)
9660               && ((unsigned HOST_WIDE_INT) const_op
9661                   < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
9662             {
9663               op0 = XEXP (op0, 0);
9664               continue;
9665             }
9666           break;
9667
9668         case PLUS:
9669           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
9670              this for equality comparisons due to pathological cases involving
9671              overflows.  */
9672           if (equality_comparison_p
9673               && 0 != (tem = simplify_binary_operation (MINUS, mode,
9674                                                         op1, XEXP (op0, 1))))
9675             {
9676               op0 = XEXP (op0, 0);
9677               op1 = tem;
9678               continue;
9679             }
9680
9681           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
9682           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
9683               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
9684             {
9685               op0 = XEXP (XEXP (op0, 0), 0);
9686               code = (code == LT ? EQ : NE);
9687               continue;
9688             }
9689           break;
9690
9691         case MINUS:
9692           /* (eq (minus A B) C) -> (eq A (plus B C)) or
9693              (eq B (minus A C)), whichever simplifies.  We can only do
9694              this for equality comparisons due to pathological cases involving
9695              overflows.  */
9696           if (equality_comparison_p
9697               && 0 != (tem = simplify_binary_operation (PLUS, mode,
9698                                                         XEXP (op0, 1), op1)))
9699             {
9700               op0 = XEXP (op0, 0);
9701               op1 = tem;
9702               continue;
9703             }
9704
9705           if (equality_comparison_p
9706               && 0 != (tem = simplify_binary_operation (MINUS, mode,
9707                                                         XEXP (op0, 0), op1)))
9708             {
9709               op0 = XEXP (op0, 1);
9710               op1 = tem;
9711               continue;
9712             }
9713
9714           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
9715              of bits in X minus 1, is one iff X > 0.  */
9716           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
9717               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9718               && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
9719               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
9720             {
9721               op0 = XEXP (op0, 1);
9722               code = (code == GE ? LE : GT);
9723               continue;
9724             }
9725           break;
9726
9727         case XOR:
9728           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
9729              if C is zero or B is a constant.  */
9730           if (equality_comparison_p
9731               && 0 != (tem = simplify_binary_operation (XOR, mode,
9732                                                         XEXP (op0, 1), op1)))
9733             {
9734               op0 = XEXP (op0, 0);
9735               op1 = tem;
9736               continue;
9737             }
9738           break;
9739
9740         case EQ:  case NE:
9741         case LT:  case LTU:  case LE:  case LEU:
9742         case GT:  case GTU:  case GE:  case GEU:
9743           /* We can't do anything if OP0 is a condition code value, rather
9744              than an actual data value.  */
9745           if (const_op != 0
9746 #ifdef HAVE_cc0
9747               || XEXP (op0, 0) == cc0_rtx
9748 #endif
9749               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
9750             break;
9751
9752           /* Get the two operands being compared.  */
9753           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
9754             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
9755           else
9756             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
9757
9758           /* Check for the cases where we simply want the result of the
9759              earlier test or the opposite of that result.  */
9760           if (code == NE
9761               || (code == EQ && reversible_comparison_p (op0))
9762               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9763                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
9764                   && (STORE_FLAG_VALUE
9765                       & (((HOST_WIDE_INT) 1
9766                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
9767                   && (code == LT
9768                       || (code == GE && reversible_comparison_p (op0)))))
9769             {
9770               code = (code == LT || code == NE
9771                       ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
9772               op0 = tem, op1 = tem1;
9773               continue;
9774             }
9775           break;
9776
9777         case IOR:
9778           /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
9779              iff X <= 0.  */
9780           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
9781               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
9782               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
9783             {
9784               op0 = XEXP (op0, 1);
9785               code = (code == GE ? GT : LE);
9786               continue;
9787             }
9788           break;
9789
9790         case AND:
9791           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
9792              will be converted to a ZERO_EXTRACT later.  */
9793           if (const_op == 0 && equality_comparison_p
9794               && GET_CODE (XEXP (op0, 0)) == ASHIFT
9795               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
9796             {
9797               op0 = simplify_and_const_int
9798                 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
9799                                              XEXP (op0, 1),
9800                                              XEXP (XEXP (op0, 0), 1)),
9801                  (HOST_WIDE_INT) 1);
9802               continue;
9803             }
9804
9805           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
9806              zero and X is a comparison and C1 and C2 describe only bits set
9807              in STORE_FLAG_VALUE, we can compare with X.  */
9808           if (const_op == 0 && equality_comparison_p
9809               && mode_width <= HOST_BITS_PER_WIDE_INT
9810               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9811               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
9812               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9813               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
9814               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9815             {
9816               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
9817                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
9818               if ((~ STORE_FLAG_VALUE & mask) == 0
9819                   && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
9820                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
9821                           && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
9822                 {
9823                   op0 = XEXP (XEXP (op0, 0), 0);
9824                   continue;
9825                 }
9826             }
9827
9828           /* If we are doing an equality comparison of an AND of a bit equal
9829              to the sign bit, replace this with a LT or GE comparison of
9830              the underlying value.  */
9831           if (equality_comparison_p
9832               && const_op == 0
9833               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9834               && mode_width <= HOST_BITS_PER_WIDE_INT
9835               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
9836                   == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9837             {
9838               op0 = XEXP (op0, 0);
9839               code = (code == EQ ? GE : LT);
9840               continue;
9841             }
9842
9843           /* If this AND operation is really a ZERO_EXTEND from a narrower
9844              mode, the constant fits within that mode, and this is either an
9845              equality or unsigned comparison, try to do this comparison in
9846              the narrower mode.  */
9847           if ((equality_comparison_p || unsigned_comparison_p)
9848               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9849               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
9850                                    & GET_MODE_MASK (mode))
9851                                   + 1)) >= 0
9852               && const_op >> i == 0
9853               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
9854             {
9855               op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
9856               continue;
9857             }
9858           break;
9859
9860         case ASHIFT:
9861           /* If we have (compare (ashift FOO N) (const_int C)) and
9862              the high order N bits of FOO (N+1 if an inequality comparison)
9863              are known to be zero, we can do this by comparing FOO with C
9864              shifted right N bits so long as the low-order N bits of C are
9865              zero.  */
9866           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
9867               && INTVAL (XEXP (op0, 1)) >= 0
9868               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
9869                   < HOST_BITS_PER_WIDE_INT)
9870               && ((const_op
9871                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
9872               && mode_width <= HOST_BITS_PER_WIDE_INT
9873               && (nonzero_bits (XEXP (op0, 0), mode)
9874                   & ~ (mask >> (INTVAL (XEXP (op0, 1))
9875                                 + ! equality_comparison_p))) == 0)
9876             {
9877               const_op >>= INTVAL (XEXP (op0, 1));
9878               op1 = GEN_INT (const_op);
9879               op0 = XEXP (op0, 0);
9880               continue;
9881             }
9882
9883           /* If we are doing a sign bit comparison, it means we are testing
9884              a particular bit.  Convert it to the appropriate AND.  */
9885           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9886               && mode_width <= HOST_BITS_PER_WIDE_INT)
9887             {
9888               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9889                                             ((HOST_WIDE_INT) 1
9890                                              << (mode_width - 1
9891                                                  - INTVAL (XEXP (op0, 1)))));
9892               code = (code == LT ? NE : EQ);
9893               continue;
9894             }
9895
9896           /* If this an equality comparison with zero and we are shifting
9897              the low bit to the sign bit, we can convert this to an AND of the
9898              low-order bit.  */
9899           if (const_op == 0 && equality_comparison_p
9900               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9901               && INTVAL (XEXP (op0, 1)) == mode_width - 1)
9902             {
9903               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9904                                             (HOST_WIDE_INT) 1);
9905               continue;
9906             }
9907           break;
9908
9909         case ASHIFTRT:
9910           /* If this is an equality comparison with zero, we can do this
9911              as a logical shift, which might be much simpler.  */
9912           if (equality_comparison_p && const_op == 0
9913               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
9914             {
9915               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
9916                                           XEXP (op0, 0),
9917                                           INTVAL (XEXP (op0, 1)));
9918               continue;
9919             }
9920
9921           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
9922              do the comparison in a narrower mode.  */
9923           if (! unsigned_comparison_p
9924               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9925               && GET_CODE (XEXP (op0, 0)) == ASHIFT
9926               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9927               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
9928                                          MODE_INT, 1)) != BLKmode
9929               && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
9930                   || ((unsigned HOST_WIDE_INT) - const_op
9931                       <= GET_MODE_MASK (tmode))))
9932             {
9933               op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
9934               continue;
9935             }
9936
9937           /* ... fall through ...  */
9938         case LSHIFTRT:
9939           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
9940              the low order N bits of FOO are known to be zero, we can do this
9941              by comparing FOO with C shifted left N bits so long as no
9942              overflow occurs.  */
9943           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
9944               && INTVAL (XEXP (op0, 1)) >= 0
9945               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9946               && mode_width <= HOST_BITS_PER_WIDE_INT
9947               && (nonzero_bits (XEXP (op0, 0), mode)
9948                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
9949               && (const_op == 0
9950                   || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
9951                       < mode_width)))
9952             {
9953               const_op <<= INTVAL (XEXP (op0, 1));
9954               op1 = GEN_INT (const_op);
9955               op0 = XEXP (op0, 0);
9956               continue;
9957             }
9958
9959           /* If we are using this shift to extract just the sign bit, we
9960              can replace this with an LT or GE comparison.  */
9961           if (const_op == 0
9962               && (equality_comparison_p || sign_bit_comparison_p)
9963               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9964               && INTVAL (XEXP (op0, 1)) == mode_width - 1)
9965             {
9966               op0 = XEXP (op0, 0);
9967               code = (code == NE || code == GT ? LT : GE);
9968               continue;
9969             }
9970           break;
9971         }
9972
9973       break;
9974     }
9975
9976   /* Now make any compound operations involved in this comparison.  Then,
9977      check for an outmost SUBREG on OP0 that isn't doing anything or is
9978      paradoxical.  The latter case can only occur when it is known that the
9979      "extra" bits will be zero.  Therefore, it is safe to remove the SUBREG.
9980      We can never remove a SUBREG for a non-equality comparison because the
9981      sign bit is in a different place in the underlying object.  */
9982
9983   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
9984   op1 = make_compound_operation (op1, SET);
9985
9986   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
9987       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
9988       && (code == NE || code == EQ)
9989       && ((GET_MODE_SIZE (GET_MODE (op0))
9990            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
9991     {
9992       op0 = SUBREG_REG (op0);
9993       op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
9994     }
9995
9996   else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
9997            && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
9998            && (code == NE || code == EQ)
9999            && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10000                <= HOST_BITS_PER_WIDE_INT)
10001            && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
10002                & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
10003            && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
10004                                               op1),
10005                (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10006                 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
10007     op0 = SUBREG_REG (op0), op1 = tem;
10008
10009   /* We now do the opposite procedure: Some machines don't have compare
10010      insns in all modes.  If OP0's mode is an integer mode smaller than a
10011      word and we can't do a compare in that mode, see if there is a larger
10012      mode for which we can do the compare.  There are a number of cases in
10013      which we can use the wider mode.  */
10014
10015   mode = GET_MODE (op0);
10016   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10017       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10018       && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
10019     for (tmode = GET_MODE_WIDER_MODE (mode);
10020          (tmode != VOIDmode
10021           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10022          tmode = GET_MODE_WIDER_MODE (tmode))
10023       if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
10024         {
10025           /* If the only nonzero bits in OP0 and OP1 are those in the
10026              narrower mode and this is an equality or unsigned comparison,
10027              we can use the wider mode.  Similarly for sign-extended
10028              values, in which case it is true for all comparisons.  */
10029           if (((code == EQ || code == NE
10030                 || code == GEU || code == GTU || code == LEU || code == LTU)
10031                && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
10032                && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
10033               || ((num_sign_bit_copies (op0, tmode)
10034                    > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
10035                   && (num_sign_bit_copies (op1, tmode)
10036                       > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
10037             {
10038               op0 = gen_lowpart_for_combine (tmode, op0);
10039               op1 = gen_lowpart_for_combine (tmode, op1);
10040               break;
10041             }
10042
10043           /* If this is a test for negative, we can make an explicit
10044              test of the sign bit.  */
10045
10046           if (op1 == const0_rtx && (code == LT || code == GE)
10047               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10048             {
10049               op0 = gen_binary (AND, tmode,
10050                                 gen_lowpart_for_combine (tmode, op0),
10051                                 GEN_INT ((HOST_WIDE_INT) 1
10052                                          << (GET_MODE_BITSIZE (mode) - 1)));
10053               code = (code == LT) ? NE : EQ;
10054               break;
10055             }
10056         }
10057
10058 #ifdef CANONICALIZE_COMPARISON
10059   /* If this machine only supports a subset of valid comparisons, see if we
10060      can convert an unsupported one into a supported one.  */
10061   CANONICALIZE_COMPARISON (code, op0, op1);
10062 #endif
10063
10064   *pop0 = op0;
10065   *pop1 = op1;
10066
10067   return code;
10068 }
10069 \f
10070 /* Return 1 if we know that X, a comparison operation, is not operating
10071    on a floating-point value or is EQ or NE, meaning that we can safely
10072    reverse it.  */
10073
10074 static int
10075 reversible_comparison_p (x)
10076      rtx x;
10077 {
10078   if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
10079       || flag_fast_math
10080       || GET_CODE (x) == NE || GET_CODE (x) == EQ)
10081     return 1;
10082
10083   switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
10084     {
10085     case MODE_INT:
10086     case MODE_PARTIAL_INT:
10087     case MODE_COMPLEX_INT:
10088       return 1;
10089
10090     case MODE_CC:
10091       /* If the mode of the condition codes tells us that this is safe,
10092          we need look no further.  */
10093       if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
10094         return 1;
10095
10096       /* Otherwise try and find where the condition codes were last set and
10097          use that.  */
10098       x = get_last_value (XEXP (x, 0));
10099       return (x && GET_CODE (x) == COMPARE
10100               && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
10101     }
10102
10103   return 0;
10104 }
10105 \f
10106 /* Utility function for following routine.  Called when X is part of a value
10107    being stored into reg_last_set_value.  Sets reg_last_set_table_tick
10108    for each register mentioned.  Similar to mention_regs in cse.c  */
10109
10110 static void
10111 update_table_tick (x)
10112      rtx x;
10113 {
10114   register enum rtx_code code = GET_CODE (x);
10115   register char *fmt = GET_RTX_FORMAT (code);
10116   register int i;
10117
10118   if (code == REG)
10119     {
10120       int regno = REGNO (x);
10121       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10122                               ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10123
10124       for (i = regno; i < endregno; i++)
10125         reg_last_set_table_tick[i] = label_tick;
10126
10127       return;
10128     }
10129   
10130   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10131     /* Note that we can't have an "E" in values stored; see
10132        get_last_value_validate.  */
10133     if (fmt[i] == 'e')
10134       update_table_tick (XEXP (x, i));
10135 }
10136
10137 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
10138    are saying that the register is clobbered and we no longer know its
10139    value.  If INSN is zero, don't update reg_last_set; this is only permitted
10140    with VALUE also zero and is used to invalidate the register.  */
10141
10142 static void
10143 record_value_for_reg (reg, insn, value)
10144      rtx reg;
10145      rtx insn;
10146      rtx value;
10147 {
10148   int regno = REGNO (reg);
10149   int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10150                           ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
10151   int i;
10152
10153   /* If VALUE contains REG and we have a previous value for REG, substitute
10154      the previous value.  */
10155   if (value && insn && reg_overlap_mentioned_p (reg, value))
10156     {
10157       rtx tem;
10158
10159       /* Set things up so get_last_value is allowed to see anything set up to
10160          our insn.  */
10161       subst_low_cuid = INSN_CUID (insn);
10162       tem = get_last_value (reg);      
10163
10164       if (tem)
10165         value = replace_rtx (copy_rtx (value), reg, tem);
10166     }
10167
10168   /* For each register modified, show we don't know its value, that
10169      we don't know about its bitwise content, that its value has been
10170      updated, and that we don't know the location of the death of the
10171      register.  */
10172   for (i = regno; i < endregno; i ++)
10173     {
10174       if (insn)
10175         reg_last_set[i] = insn;
10176       reg_last_set_value[i] = 0;
10177       reg_last_set_mode[i] = 0;
10178       reg_last_set_nonzero_bits[i] = 0;
10179       reg_last_set_sign_bit_copies[i] = 0;
10180       reg_last_death[i] = 0;
10181     }
10182
10183   /* Mark registers that are being referenced in this value.  */
10184   if (value)
10185     update_table_tick (value);
10186
10187   /* Now update the status of each register being set.
10188      If someone is using this register in this block, set this register
10189      to invalid since we will get confused between the two lives in this
10190      basic block.  This makes using this register always invalid.  In cse, we
10191      scan the table to invalidate all entries using this register, but this
10192      is too much work for us.  */
10193
10194   for (i = regno; i < endregno; i++)
10195     {
10196       reg_last_set_label[i] = label_tick;
10197       if (value && reg_last_set_table_tick[i] == label_tick)
10198         reg_last_set_invalid[i] = 1;
10199       else
10200         reg_last_set_invalid[i] = 0;
10201     }
10202
10203   /* The value being assigned might refer to X (like in "x++;").  In that
10204      case, we must replace it with (clobber (const_int 0)) to prevent
10205      infinite loops.  */
10206   if (value && ! get_last_value_validate (&value, insn,
10207                                           reg_last_set_label[regno], 0))
10208     {
10209       value = copy_rtx (value);
10210       if (! get_last_value_validate (&value, insn,
10211                                      reg_last_set_label[regno], 1))
10212         value = 0;
10213     }
10214
10215   /* For the main register being modified, update the value, the mode, the
10216      nonzero bits, and the number of sign bit copies.  */
10217
10218   reg_last_set_value[regno] = value;
10219
10220   if (value)
10221     {
10222       subst_low_cuid = INSN_CUID (insn);
10223       reg_last_set_mode[regno] = GET_MODE (reg);
10224       reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
10225       reg_last_set_sign_bit_copies[regno]
10226         = num_sign_bit_copies (value, GET_MODE (reg));
10227     }
10228 }
10229
10230 /* Used for communication between the following two routines.  */
10231 static rtx record_dead_insn;
10232
10233 /* Called via note_stores from record_dead_and_set_regs to handle one
10234    SET or CLOBBER in an insn.  */
10235
10236 static void
10237 record_dead_and_set_regs_1 (dest, setter)
10238      rtx dest, setter;
10239 {
10240   if (GET_CODE (dest) == SUBREG)
10241     dest = SUBREG_REG (dest);
10242
10243   if (GET_CODE (dest) == REG)
10244     {
10245       /* If we are setting the whole register, we know its value.  Otherwise
10246          show that we don't know the value.  We can handle SUBREG in
10247          some cases.  */
10248       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10249         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10250       else if (GET_CODE (setter) == SET
10251                && GET_CODE (SET_DEST (setter)) == SUBREG
10252                && SUBREG_REG (SET_DEST (setter)) == dest
10253                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10254                && subreg_lowpart_p (SET_DEST (setter)))
10255         record_value_for_reg (dest, record_dead_insn,
10256                               gen_lowpart_for_combine (GET_MODE (dest),
10257                                                        SET_SRC (setter)));
10258       else
10259         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10260     }
10261   else if (GET_CODE (dest) == MEM
10262            /* Ignore pushes, they clobber nothing.  */
10263            && ! push_operand (dest, GET_MODE (dest)))
10264     mem_last_set = INSN_CUID (record_dead_insn);
10265 }
10266
10267 /* Update the records of when each REG was most recently set or killed
10268    for the things done by INSN.  This is the last thing done in processing
10269    INSN in the combiner loop.
10270
10271    We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10272    reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10273    and also the similar information mem_last_set (which insn most recently
10274    modified memory) and last_call_cuid (which insn was the most recent
10275    subroutine call).  */
10276
10277 static void
10278 record_dead_and_set_regs (insn)
10279      rtx insn;
10280 {
10281   register rtx link;
10282   int i;
10283
10284   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10285     {
10286       if (REG_NOTE_KIND (link) == REG_DEAD
10287           && GET_CODE (XEXP (link, 0)) == REG)
10288         {
10289           int regno = REGNO (XEXP (link, 0));
10290           int endregno
10291             = regno + (regno < FIRST_PSEUDO_REGISTER
10292                        ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10293                        : 1);
10294
10295           for (i = regno; i < endregno; i++)
10296             reg_last_death[i] = insn;
10297         }
10298       else if (REG_NOTE_KIND (link) == REG_INC)
10299         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10300     }
10301
10302   if (GET_CODE (insn) == CALL_INSN)
10303     {
10304       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10305         if (call_used_regs[i])
10306           {
10307             reg_last_set_value[i] = 0;
10308             reg_last_set_mode[i] = 0;
10309             reg_last_set_nonzero_bits[i] = 0;
10310             reg_last_set_sign_bit_copies[i] = 0;
10311             reg_last_death[i] = 0;
10312           }
10313
10314       last_call_cuid = mem_last_set = INSN_CUID (insn);
10315     }
10316
10317   record_dead_insn = insn;
10318   note_stores (PATTERN (insn), record_dead_and_set_regs_1);
10319 }
10320 \f
10321 /* Utility routine for the following function.  Verify that all the registers
10322    mentioned in *LOC are valid when *LOC was part of a value set when
10323    label_tick == TICK.  Return 0 if some are not.
10324
10325    If REPLACE is non-zero, replace the invalid reference with
10326    (clobber (const_int 0)) and return 1.  This replacement is useful because
10327    we often can get useful information about the form of a value (e.g., if
10328    it was produced by a shift that always produces -1 or 0) even though
10329    we don't know exactly what registers it was produced from.  */
10330
10331 static int
10332 get_last_value_validate (loc, insn, tick, replace)
10333      rtx *loc;
10334      rtx insn;
10335      int tick;
10336      int replace;
10337 {
10338   rtx x = *loc;
10339   char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10340   int len = GET_RTX_LENGTH (GET_CODE (x));
10341   int i;
10342
10343   if (GET_CODE (x) == REG)
10344     {
10345       int regno = REGNO (x);
10346       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10347                               ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10348       int j;
10349
10350       for (j = regno; j < endregno; j++)
10351         if (reg_last_set_invalid[j]
10352             /* If this is a pseudo-register that was only set once, it is
10353                always valid.  */
10354             || (! (regno >= FIRST_PSEUDO_REGISTER && reg_n_sets[regno] == 1)
10355                 && reg_last_set_label[j] > tick))
10356           {
10357             if (replace)
10358               *loc = gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
10359             return replace;
10360           }
10361
10362       return 1;
10363     }
10364   /* If this is a memory reference, make sure that there were
10365      no stores after it that might have clobbered the value.  We don't
10366      have alias info, so we assume any store invalidates it.  */
10367   else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
10368            && INSN_CUID (insn) <= mem_last_set)
10369     {
10370       if (replace)
10371         *loc = gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
10372       return replace;
10373     }
10374
10375   for (i = 0; i < len; i++)
10376     if ((fmt[i] == 'e'
10377          && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
10378         /* Don't bother with these.  They shouldn't occur anyway.  */
10379         || fmt[i] == 'E')
10380       return 0;
10381
10382   /* If we haven't found a reason for it to be invalid, it is valid.  */
10383   return 1;
10384 }
10385
10386 /* Get the last value assigned to X, if known.  Some registers
10387    in the value may be replaced with (clobber (const_int 0)) if their value
10388    is known longer known reliably.  */
10389
10390 static rtx
10391 get_last_value (x)
10392      rtx x;
10393 {
10394   int regno;
10395   rtx value;
10396
10397   /* If this is a non-paradoxical SUBREG, get the value of its operand and
10398      then convert it to the desired mode.  If this is a paradoxical SUBREG,
10399      we cannot predict what values the "extra" bits might have.  */
10400   if (GET_CODE (x) == SUBREG
10401       && subreg_lowpart_p (x)
10402       && (GET_MODE_SIZE (GET_MODE (x))
10403           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
10404       && (value = get_last_value (SUBREG_REG (x))) != 0)
10405     return gen_lowpart_for_combine (GET_MODE (x), value);
10406
10407   if (GET_CODE (x) != REG)
10408     return 0;
10409
10410   regno = REGNO (x);
10411   value = reg_last_set_value[regno];
10412
10413   /* If we don't have a value or if it isn't for this basic block,
10414      return 0.  */
10415
10416   if (value == 0
10417       || (reg_n_sets[regno] != 1
10418           && reg_last_set_label[regno] != label_tick))
10419     return 0;
10420
10421   /* If the value was set in a later insn than the ones we are processing,
10422      we can't use it even if the register was only set once, but make a quick
10423      check to see if the previous insn set it to something.  This is commonly
10424      the case when the same pseudo is used by repeated insns.
10425
10426      This does not work if there exists an instruction which is temporarily
10427      not on the insn chain.  */
10428
10429   if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
10430     {
10431       rtx insn, set;
10432
10433       /* We can not do anything useful in this case, because there is
10434          an instruction which is not on the insn chain.  */
10435       if (subst_prev_insn)
10436         return 0;
10437
10438       /* Skip over USE insns.  They are not useful here, and they may have
10439          been made by combine, in which case they do not have a INSN_CUID
10440          value.  We can't use prev_real_insn, because that would incorrectly
10441          take us backwards across labels.  Skip over BARRIERs also, since
10442          they could have been made by combine.  If we see one, we must be
10443          optimizing dead code, so it doesn't matter what we do.  */
10444       for (insn = prev_nonnote_insn (subst_insn);
10445            insn && ((GET_CODE (insn) == INSN
10446                      && GET_CODE (PATTERN (insn)) == USE)
10447                     || GET_CODE (insn) == BARRIER
10448                     || INSN_CUID (insn) >= subst_low_cuid);
10449            insn = prev_nonnote_insn (insn))
10450         ;
10451
10452       if (insn
10453           && (set = single_set (insn)) != 0
10454           && rtx_equal_p (SET_DEST (set), x))
10455         {
10456           value = SET_SRC (set);
10457
10458           /* Make sure that VALUE doesn't reference X.  Replace any
10459              explicit references with a CLOBBER.  If there are any remaining
10460              references (rare), don't use the value.  */
10461
10462           if (reg_mentioned_p (x, value))
10463             value = replace_rtx (copy_rtx (value), x,
10464                                  gen_rtx (CLOBBER, GET_MODE (x), const0_rtx));
10465
10466           if (reg_overlap_mentioned_p (x, value))
10467             return 0;
10468         }
10469       else
10470         return 0;
10471     }
10472
10473   /* If the value has all its registers valid, return it.  */
10474   if (get_last_value_validate (&value, reg_last_set[regno],
10475                                reg_last_set_label[regno], 0))
10476     return value;
10477
10478   /* Otherwise, make a copy and replace any invalid register with
10479      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
10480
10481   value = copy_rtx (value);
10482   if (get_last_value_validate (&value, reg_last_set[regno],
10483                                reg_last_set_label[regno], 1))
10484     return value;
10485
10486   return 0;
10487 }
10488 \f
10489 /* Return nonzero if expression X refers to a REG or to memory
10490    that is set in an instruction more recent than FROM_CUID.  */
10491
10492 static int
10493 use_crosses_set_p (x, from_cuid)
10494      register rtx x;
10495      int from_cuid;
10496 {
10497   register char *fmt;
10498   register int i;
10499   register enum rtx_code code = GET_CODE (x);
10500
10501   if (code == REG)
10502     {
10503       register int regno = REGNO (x);
10504       int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
10505                             ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10506       
10507 #ifdef PUSH_ROUNDING
10508       /* Don't allow uses of the stack pointer to be moved,
10509          because we don't know whether the move crosses a push insn.  */
10510       if (regno == STACK_POINTER_REGNUM)
10511         return 1;
10512 #endif
10513       for (;regno < endreg; regno++)
10514         if (reg_last_set[regno]
10515             && INSN_CUID (reg_last_set[regno]) > from_cuid)
10516           return 1;
10517       return 0;
10518     }
10519
10520   if (code == MEM && mem_last_set > from_cuid)
10521     return 1;
10522
10523   fmt = GET_RTX_FORMAT (code);
10524
10525   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10526     {
10527       if (fmt[i] == 'E')
10528         {
10529           register int j;
10530           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10531             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
10532               return 1;
10533         }
10534       else if (fmt[i] == 'e'
10535                && use_crosses_set_p (XEXP (x, i), from_cuid))
10536         return 1;
10537     }
10538   return 0;
10539 }
10540 \f
10541 /* Define three variables used for communication between the following
10542    routines.  */
10543
10544 static int reg_dead_regno, reg_dead_endregno;
10545 static int reg_dead_flag;
10546
10547 /* Function called via note_stores from reg_dead_at_p.
10548
10549    If DEST is within [reg_dead_regno, reg_dead_endregno), set 
10550    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
10551
10552 static void
10553 reg_dead_at_p_1 (dest, x)
10554      rtx dest;
10555      rtx x;
10556 {
10557   int regno, endregno;
10558
10559   if (GET_CODE (dest) != REG)
10560     return;
10561
10562   regno = REGNO (dest);
10563   endregno = regno + (regno < FIRST_PSEUDO_REGISTER 
10564                       ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
10565
10566   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
10567     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
10568 }
10569
10570 /* Return non-zero if REG is known to be dead at INSN.
10571
10572    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
10573    referencing REG, it is dead.  If we hit a SET referencing REG, it is
10574    live.  Otherwise, see if it is live or dead at the start of the basic
10575    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
10576    must be assumed to be always live.  */
10577
10578 static int
10579 reg_dead_at_p (reg, insn)
10580      rtx reg;
10581      rtx insn;
10582 {
10583   int block, i;
10584
10585   /* Set variables for reg_dead_at_p_1.  */
10586   reg_dead_regno = REGNO (reg);
10587   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
10588                                         ? HARD_REGNO_NREGS (reg_dead_regno,
10589                                                             GET_MODE (reg))
10590                                         : 1);
10591
10592   reg_dead_flag = 0;
10593
10594   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  */
10595   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
10596     {
10597       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10598         if (TEST_HARD_REG_BIT (newpat_used_regs, i))
10599           return 0;
10600     }
10601
10602   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
10603      beginning of function.  */
10604   for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
10605        insn = prev_nonnote_insn (insn))
10606     {
10607       note_stores (PATTERN (insn), reg_dead_at_p_1);
10608       if (reg_dead_flag)
10609         return reg_dead_flag == 1 ? 1 : 0;
10610
10611       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
10612         return 1;
10613     }
10614
10615   /* Get the basic block number that we were in.  */
10616   if (insn == 0)
10617     block = 0;
10618   else
10619     {
10620       for (block = 0; block < n_basic_blocks; block++)
10621         if (insn == basic_block_head[block])
10622           break;
10623
10624       if (block == n_basic_blocks)
10625         return 0;
10626     }
10627
10628   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10629     if (basic_block_live_at_start[block][i / REGSET_ELT_BITS]
10630         & ((REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS)))
10631       return 0;
10632
10633   return 1;
10634 }
10635 \f
10636 /* Note hard registers in X that are used.  This code is similar to
10637    that in flow.c, but much simpler since we don't care about pseudos.  */
10638
10639 static void
10640 mark_used_regs_combine (x)
10641      rtx x;
10642 {
10643   register RTX_CODE code = GET_CODE (x);
10644   register int regno;
10645   int i;
10646
10647   switch (code)
10648     {
10649     case LABEL_REF:
10650     case SYMBOL_REF:
10651     case CONST_INT:
10652     case CONST:
10653     case CONST_DOUBLE:
10654     case PC:
10655     case ADDR_VEC:
10656     case ADDR_DIFF_VEC:
10657     case ASM_INPUT:
10658 #ifdef HAVE_cc0
10659     /* CC0 must die in the insn after it is set, so we don't need to take
10660        special note of it here.  */
10661     case CC0:
10662 #endif
10663       return;
10664
10665     case CLOBBER:
10666       /* If we are clobbering a MEM, mark any hard registers inside the
10667          address as used.  */
10668       if (GET_CODE (XEXP (x, 0)) == MEM)
10669         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
10670       return;
10671
10672     case REG:
10673       regno = REGNO (x);
10674       /* A hard reg in a wide mode may really be multiple registers.
10675          If so, mark all of them just like the first.  */
10676       if (regno < FIRST_PSEUDO_REGISTER)
10677         {
10678           /* None of this applies to the stack, frame or arg pointers */
10679           if (regno == STACK_POINTER_REGNUM
10680 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10681               || regno == HARD_FRAME_POINTER_REGNUM
10682 #endif
10683 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
10684               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
10685 #endif
10686               || regno == FRAME_POINTER_REGNUM)
10687             return;
10688
10689           i = HARD_REGNO_NREGS (regno, GET_MODE (x));
10690           while (i-- > 0)
10691             SET_HARD_REG_BIT (newpat_used_regs, regno + i);
10692         }
10693       return;
10694
10695     case SET:
10696       {
10697         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
10698            the address.  */
10699         register rtx testreg = SET_DEST (x);
10700
10701         while (GET_CODE (testreg) == SUBREG
10702                || GET_CODE (testreg) == ZERO_EXTRACT
10703                || GET_CODE (testreg) == SIGN_EXTRACT
10704                || GET_CODE (testreg) == STRICT_LOW_PART)
10705           testreg = XEXP (testreg, 0);
10706
10707         if (GET_CODE (testreg) == MEM)
10708           mark_used_regs_combine (XEXP (testreg, 0));
10709
10710         mark_used_regs_combine (SET_SRC (x));
10711         return;
10712       }
10713     }
10714
10715   /* Recursively scan the operands of this expression.  */
10716
10717   {
10718     register char *fmt = GET_RTX_FORMAT (code);
10719
10720     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10721       {
10722         if (fmt[i] == 'e')
10723           mark_used_regs_combine (XEXP (x, i));
10724         else if (fmt[i] == 'E')
10725           {
10726             register int j;
10727
10728             for (j = 0; j < XVECLEN (x, i); j++)
10729               mark_used_regs_combine (XVECEXP (x, i, j));
10730           }
10731       }
10732   }
10733 }
10734
10735 \f
10736 /* Remove register number REGNO from the dead registers list of INSN.
10737
10738    Return the note used to record the death, if there was one.  */
10739
10740 rtx
10741 remove_death (regno, insn)
10742      int regno;
10743      rtx insn;
10744 {
10745   register rtx note = find_regno_note (insn, REG_DEAD, regno);
10746
10747   if (note)
10748     {
10749       reg_n_deaths[regno]--;
10750       remove_note (insn, note);
10751     }
10752
10753   return note;
10754 }
10755
10756 /* For each register (hardware or pseudo) used within expression X, if its
10757    death is in an instruction with cuid between FROM_CUID (inclusive) and
10758    TO_INSN (exclusive), put a REG_DEAD note for that register in the
10759    list headed by PNOTES. 
10760
10761    That said, don't move registers killed by maybe_kill_insn.
10762
10763    This is done when X is being merged by combination into TO_INSN.  These
10764    notes will then be distributed as needed.  */
10765
10766 static void
10767 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
10768      rtx x;
10769      rtx maybe_kill_insn;
10770      int from_cuid;
10771      rtx to_insn;
10772      rtx *pnotes;
10773 {
10774   register char *fmt;
10775   register int len, i;
10776   register enum rtx_code code = GET_CODE (x);
10777
10778   if (code == REG)
10779     {
10780       register int regno = REGNO (x);
10781       register rtx where_dead = reg_last_death[regno];
10782       register rtx before_dead, after_dead;
10783
10784       /* Don't move the register if it gets killed in between from and to */
10785       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
10786           && !reg_referenced_p (x, maybe_kill_insn))
10787         return;
10788
10789       /* WHERE_DEAD could be a USE insn made by combine, so first we
10790          make sure that we have insns with valid INSN_CUID values.  */
10791       before_dead = where_dead;
10792       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
10793         before_dead = PREV_INSN (before_dead);
10794       after_dead = where_dead;
10795       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
10796         after_dead = NEXT_INSN (after_dead);
10797
10798       if (before_dead && after_dead
10799           && INSN_CUID (before_dead) >= from_cuid
10800           && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
10801               || (where_dead != after_dead
10802                   && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
10803         {
10804           rtx note = remove_death (regno, where_dead);
10805
10806           /* It is possible for the call above to return 0.  This can occur
10807              when reg_last_death points to I2 or I1 that we combined with.
10808              In that case make a new note.
10809
10810              We must also check for the case where X is a hard register
10811              and NOTE is a death note for a range of hard registers
10812              including X.  In that case, we must put REG_DEAD notes for
10813              the remaining registers in place of NOTE.  */
10814
10815           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
10816               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
10817                   > GET_MODE_SIZE (GET_MODE (x))))
10818             {
10819               int deadregno = REGNO (XEXP (note, 0));
10820               int deadend
10821                 = (deadregno + HARD_REGNO_NREGS (deadregno,
10822                                                  GET_MODE (XEXP (note, 0))));
10823               int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
10824               int i;
10825
10826               for (i = deadregno; i < deadend; i++)
10827                 if (i < regno || i >= ourend)
10828                   REG_NOTES (where_dead)
10829                     = gen_rtx (EXPR_LIST, REG_DEAD,
10830                                gen_rtx (REG, reg_raw_mode[i], i),
10831                                REG_NOTES (where_dead));
10832             }
10833           /* If we didn't find any note, or if we found a REG_DEAD note that
10834              covers only part of the given reg, and we have a multi-reg hard
10835              register, then to be safe we must check for REG_DEAD notes
10836              for each register other than the first.  They could have
10837              their own REG_DEAD notes lying around.  */
10838           else if ((note == 0
10839                     || (note != 0
10840                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
10841                             < GET_MODE_SIZE (GET_MODE (x)))))
10842                    && regno < FIRST_PSEUDO_REGISTER
10843                    && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
10844             {
10845               int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
10846               int i, offset;
10847               rtx oldnotes = 0;
10848
10849               if (note)
10850                 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
10851               else
10852                 offset = 1;
10853
10854               for (i = regno + offset; i < ourend; i++)
10855                 move_deaths (gen_rtx (REG, reg_raw_mode[i], i),
10856                              maybe_kill_insn, from_cuid, to_insn, &oldnotes);
10857             }
10858
10859           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
10860             {
10861               XEXP (note, 1) = *pnotes;
10862               *pnotes = note;
10863             }
10864           else
10865             *pnotes = gen_rtx (EXPR_LIST, REG_DEAD, x, *pnotes);
10866
10867           reg_n_deaths[regno]++;
10868         }
10869
10870       return;
10871     }
10872
10873   else if (GET_CODE (x) == SET)
10874     {
10875       rtx dest = SET_DEST (x);
10876
10877       move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
10878
10879       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
10880          that accesses one word of a multi-word item, some
10881          piece of everything register in the expression is used by
10882          this insn, so remove any old death.  */
10883
10884       if (GET_CODE (dest) == ZERO_EXTRACT
10885           || GET_CODE (dest) == STRICT_LOW_PART
10886           || (GET_CODE (dest) == SUBREG
10887               && (((GET_MODE_SIZE (GET_MODE (dest))
10888                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
10889                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
10890                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
10891         {
10892           move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
10893           return;
10894         }
10895
10896       /* If this is some other SUBREG, we know it replaces the entire
10897          value, so use that as the destination.  */
10898       if (GET_CODE (dest) == SUBREG)
10899         dest = SUBREG_REG (dest);
10900
10901       /* If this is a MEM, adjust deaths of anything used in the address.
10902          For a REG (the only other possibility), the entire value is
10903          being replaced so the old value is not used in this insn.  */
10904
10905       if (GET_CODE (dest) == MEM)
10906         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
10907                      to_insn, pnotes);
10908       return;
10909     }
10910
10911   else if (GET_CODE (x) == CLOBBER)
10912     return;
10913
10914   len = GET_RTX_LENGTH (code);
10915   fmt = GET_RTX_FORMAT (code);
10916
10917   for (i = 0; i < len; i++)
10918     {
10919       if (fmt[i] == 'E')
10920         {
10921           register int j;
10922           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10923             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
10924                          to_insn, pnotes);
10925         }
10926       else if (fmt[i] == 'e')
10927         move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
10928     }
10929 }
10930 \f
10931 /* Return 1 if X is the target of a bit-field assignment in BODY, the
10932    pattern of an insn.  X must be a REG.  */
10933
10934 static int
10935 reg_bitfield_target_p (x, body)
10936      rtx x;
10937      rtx body;
10938 {
10939   int i;
10940
10941   if (GET_CODE (body) == SET)
10942     {
10943       rtx dest = SET_DEST (body);
10944       rtx target;
10945       int regno, tregno, endregno, endtregno;
10946
10947       if (GET_CODE (dest) == ZERO_EXTRACT)
10948         target = XEXP (dest, 0);
10949       else if (GET_CODE (dest) == STRICT_LOW_PART)
10950         target = SUBREG_REG (XEXP (dest, 0));
10951       else
10952         return 0;
10953
10954       if (GET_CODE (target) == SUBREG)
10955         target = SUBREG_REG (target);
10956
10957       if (GET_CODE (target) != REG)
10958         return 0;
10959
10960       tregno = REGNO (target), regno = REGNO (x);
10961       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
10962         return target == x;
10963
10964       endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
10965       endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
10966
10967       return endregno > tregno && regno < endtregno;
10968     }
10969
10970   else if (GET_CODE (body) == PARALLEL)
10971     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
10972       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
10973         return 1;
10974
10975   return 0;
10976 }      
10977 \f
10978 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
10979    as appropriate.  I3 and I2 are the insns resulting from the combination
10980    insns including FROM (I2 may be zero).
10981
10982    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
10983    not need REG_DEAD notes because they are being substituted for.  This
10984    saves searching in the most common cases.
10985
10986    Each note in the list is either ignored or placed on some insns, depending
10987    on the type of note.  */
10988
10989 static void
10990 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
10991      rtx notes;
10992      rtx from_insn;
10993      rtx i3, i2;
10994      rtx elim_i2, elim_i1;
10995 {
10996   rtx note, next_note;
10997   rtx tem;
10998
10999   for (note = notes; note; note = next_note)
11000     {
11001       rtx place = 0, place2 = 0;
11002
11003       /* If this NOTE references a pseudo register, ensure it references
11004          the latest copy of that register.  */
11005       if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11006           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11007         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11008
11009       next_note = XEXP (note, 1);
11010       switch (REG_NOTE_KIND (note))
11011         {
11012         case REG_BR_PROB:
11013         case REG_EXEC_COUNT:
11014           /* Doesn't matter much where we put this, as long as it's somewhere.
11015              It is preferable to keep these notes on branches, which is most
11016              likely to be i3.  */
11017           place = i3;
11018           break;
11019
11020         case REG_UNUSED:
11021           /* Any clobbers for i3 may still exist, and so we must process
11022              REG_UNUSED notes from that insn.
11023
11024              Any clobbers from i2 or i1 can only exist if they were added by
11025              recog_for_combine.  In that case, recog_for_combine created the
11026              necessary REG_UNUSED notes.  Trying to keep any original
11027              REG_UNUSED notes from these insns can cause incorrect output
11028              if it is for the same register as the original i3 dest.
11029              In that case, we will notice that the register is set in i3,
11030              and then add a REG_UNUSED note for the destination of i3, which
11031              is wrong.  However, it is possible to have REG_UNUSED notes from
11032              i2 or i1 for register which were both used and clobbered, so
11033              we keep notes from i2 or i1 if they will turn into REG_DEAD
11034              notes.  */
11035
11036           /* If this register is set or clobbered in I3, put the note there
11037              unless there is one already.  */
11038           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11039             {
11040               if (from_insn != i3)
11041                 break;
11042
11043               if (! (GET_CODE (XEXP (note, 0)) == REG
11044                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11045                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11046                 place = i3;
11047             }
11048           /* Otherwise, if this register is used by I3, then this register
11049              now dies here, so we must put a REG_DEAD note here unless there
11050              is one already.  */
11051           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11052                    && ! (GET_CODE (XEXP (note, 0)) == REG
11053                          ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
11054                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11055             {
11056               PUT_REG_NOTE_KIND (note, REG_DEAD);
11057               place = i3;
11058             }
11059           break;
11060
11061         case REG_EQUAL:
11062         case REG_EQUIV:
11063         case REG_NONNEG:
11064           /* These notes say something about results of an insn.  We can
11065              only support them if they used to be on I3 in which case they
11066              remain on I3.  Otherwise they are ignored.
11067
11068              If the note refers to an expression that is not a constant, we
11069              must also ignore the note since we cannot tell whether the
11070              equivalence is still true.  It might be possible to do
11071              slightly better than this (we only have a problem if I2DEST
11072              or I1DEST is present in the expression), but it doesn't
11073              seem worth the trouble.  */
11074
11075           if (from_insn == i3
11076               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11077             place = i3;
11078           break;
11079
11080         case REG_INC:
11081         case REG_NO_CONFLICT:
11082         case REG_LABEL:
11083           /* These notes say something about how a register is used.  They must
11084              be present on any use of the register in I2 or I3.  */
11085           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11086             place = i3;
11087
11088           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11089             {
11090               if (place)
11091                 place2 = i2;
11092               else
11093                 place = i2;
11094             }
11095           break;
11096
11097         case REG_WAS_0:
11098           /* It is too much trouble to try to see if this note is still
11099              correct in all situations.  It is better to simply delete it.  */
11100           break;
11101
11102         case REG_RETVAL:
11103           /* If the insn previously containing this note still exists,
11104              put it back where it was.  Otherwise move it to the previous
11105              insn.  Adjust the corresponding REG_LIBCALL note.  */
11106           if (GET_CODE (from_insn) != NOTE)
11107             place = from_insn;
11108           else
11109             {
11110               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
11111               place = prev_real_insn (from_insn);
11112               if (tem && place)
11113                 XEXP (tem, 0) = place;
11114             }
11115           break;
11116
11117         case REG_LIBCALL:
11118           /* This is handled similarly to REG_RETVAL.  */
11119           if (GET_CODE (from_insn) != NOTE)
11120             place = from_insn;
11121           else
11122             {
11123               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
11124               place = next_real_insn (from_insn);
11125               if (tem && place)
11126                 XEXP (tem, 0) = place;
11127             }
11128           break;
11129
11130         case REG_DEAD:
11131           /* If the register is used as an input in I3, it dies there.
11132              Similarly for I2, if it is non-zero and adjacent to I3.
11133
11134              If the register is not used as an input in either I3 or I2
11135              and it is not one of the registers we were supposed to eliminate,
11136              there are two possibilities.  We might have a non-adjacent I2
11137              or we might have somehow eliminated an additional register
11138              from a computation.  For example, we might have had A & B where
11139              we discover that B will always be zero.  In this case we will
11140              eliminate the reference to A.
11141
11142              In both cases, we must search to see if we can find a previous
11143              use of A and put the death note there.  */
11144
11145           if (from_insn
11146               && GET_CODE (from_insn) == CALL_INSN
11147               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11148             place = from_insn;
11149           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
11150             place = i3;
11151           else if (i2 != 0 && next_nonnote_insn (i2) == i3
11152                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11153             place = i2;
11154
11155           if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
11156             break;
11157
11158           /* If the register is used in both I2 and I3 and it dies in I3, 
11159              we might have added another reference to it.  If reg_n_refs
11160              was 2, bump it to 3.  This has to be correct since the 
11161              register must have been set somewhere.  The reason this is
11162              done is because local-alloc.c treats 2 references as a 
11163              special case.  */
11164
11165           if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
11166               && reg_n_refs[REGNO (XEXP (note, 0))]== 2
11167               && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11168             reg_n_refs[REGNO (XEXP (note, 0))] = 3;
11169
11170           if (place == 0)
11171             {
11172               for (tem = prev_nonnote_insn (i3);
11173                    place == 0 && tem
11174                    && (GET_CODE (tem) == INSN || GET_CODE (tem) == CALL_INSN);
11175                    tem = prev_nonnote_insn (tem))
11176                 {
11177                   /* If the register is being set at TEM, see if that is all
11178                      TEM is doing.  If so, delete TEM.  Otherwise, make this
11179                      into a REG_UNUSED note instead.  */
11180                   if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11181                     {
11182                       rtx set = single_set (tem);
11183
11184                       /* Verify that it was the set, and not a clobber that
11185                          modified the register.  */
11186
11187                       if (set != 0 && ! side_effects_p (SET_SRC (set))
11188                           && (rtx_equal_p (XEXP (note, 0), SET_DEST (set))
11189                               || (GET_CODE (SET_DEST (set)) == SUBREG
11190                                   && rtx_equal_p (XEXP (note, 0),
11191                                                   XEXP (SET_DEST (set), 0)))))
11192                         {
11193                           /* Move the notes and links of TEM elsewhere.
11194                              This might delete other dead insns recursively. 
11195                              First set the pattern to something that won't use
11196                              any register.  */
11197
11198                           PATTERN (tem) = pc_rtx;
11199
11200                           distribute_notes (REG_NOTES (tem), tem, tem,
11201                                             NULL_RTX, NULL_RTX, NULL_RTX);
11202                           distribute_links (LOG_LINKS (tem));
11203
11204                           PUT_CODE (tem, NOTE);
11205                           NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11206                           NOTE_SOURCE_FILE (tem) = 0;
11207                         }
11208                       else
11209                         {
11210                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
11211                           
11212                           /*  If there isn't already a REG_UNUSED note, put one
11213                               here.  */
11214                           if (! find_regno_note (tem, REG_UNUSED,
11215                                                  REGNO (XEXP (note, 0))))
11216                             place = tem;
11217                           break;
11218                       }
11219                   }
11220                 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11221                          || (GET_CODE (tem) == CALL_INSN
11222                              && find_reg_fusage (tem, USE, XEXP (note, 0))))
11223                   {
11224                     place = tem;
11225
11226                     /* If we are doing a 3->2 combination, and we have a
11227                        register which formerly died in i3 and was not used
11228                        by i2, which now no longer dies in i3 and is used in
11229                        i2 but does not die in i2, and place is between i2
11230                        and i3, then we may need to move a link from place to
11231                        i2.  */
11232                     if (i2 && INSN_UID (place) <= max_uid_cuid
11233                         && INSN_CUID (place) > INSN_CUID (i2)
11234                         && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
11235                         && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11236                       {
11237                         rtx links = LOG_LINKS (place);
11238                         LOG_LINKS (place) = 0;
11239                         distribute_links (links);
11240                       }
11241                     break;
11242                   }
11243                 }
11244               
11245               /* If we haven't found an insn for the death note and it
11246                  is still a REG_DEAD note, but we have hit a CODE_LABEL,
11247                  insert a USE insn for the register at that label and
11248                  put the death node there.  This prevents problems with
11249                  call-state tracking in caller-save.c.  */
11250               if (REG_NOTE_KIND (note) == REG_DEAD && place == 0 && tem != 0)
11251                 {
11252                   place
11253                     = emit_insn_after (gen_rtx (USE, VOIDmode, XEXP (note, 0)),
11254                                        tem);
11255
11256                   /* If this insn was emitted between blocks, then update
11257                      basic_block_head of the current block to include it.  */
11258                   if (basic_block_end[this_basic_block - 1] == tem)
11259                     basic_block_head[this_basic_block] = place;
11260                 }
11261             }
11262
11263           /* If the register is set or already dead at PLACE, we needn't do
11264              anything with this note if it is still a REG_DEAD note.  
11265
11266              Note that we cannot use just `dead_or_set_p' here since we can
11267              convert an assignment to a register into a bit-field assignment.
11268              Therefore, we must also omit the note if the register is the 
11269              target of a bitfield assignment.  */
11270              
11271           if (place && REG_NOTE_KIND (note) == REG_DEAD)
11272             {
11273               int regno = REGNO (XEXP (note, 0));
11274
11275               if (dead_or_set_p (place, XEXP (note, 0))
11276                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
11277                 {
11278                   /* Unless the register previously died in PLACE, clear
11279                      reg_last_death.  [I no longer understand why this is
11280                      being done.] */
11281                   if (reg_last_death[regno] != place)
11282                     reg_last_death[regno] = 0;
11283                   place = 0;
11284                 }
11285               else
11286                 reg_last_death[regno] = place;
11287
11288               /* If this is a death note for a hard reg that is occupying
11289                  multiple registers, ensure that we are still using all
11290                  parts of the object.  If we find a piece of the object
11291                  that is unused, we must add a USE for that piece before
11292                  PLACE and put the appropriate REG_DEAD note on it.
11293
11294                  An alternative would be to put a REG_UNUSED for the pieces
11295                  on the insn that set the register, but that can't be done if
11296                  it is not in the same block.  It is simpler, though less
11297                  efficient, to add the USE insns.  */
11298
11299               if (place && regno < FIRST_PSEUDO_REGISTER
11300                   && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
11301                 {
11302                   int endregno
11303                     = regno + HARD_REGNO_NREGS (regno,
11304                                                 GET_MODE (XEXP (note, 0)));
11305                   int all_used = 1;
11306                   int i;
11307
11308                   for (i = regno; i < endregno; i++)
11309                     if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
11310                         && ! find_regno_fusage (place, USE, i))
11311                       {
11312                         rtx piece = gen_rtx (REG, reg_raw_mode[i], i);
11313                         rtx p;
11314
11315                         /* See if we already placed a USE note for this
11316                            register in front of PLACE.  */
11317                         for (p = place;
11318                              GET_CODE (PREV_INSN (p)) == INSN
11319                              && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
11320                              p = PREV_INSN (p))
11321                           if (rtx_equal_p (piece,
11322                                            XEXP (PATTERN (PREV_INSN (p)), 0)))
11323                             {
11324                               p = 0;
11325                               break;
11326                             }
11327
11328                         if (p)
11329                           {
11330                             rtx use_insn
11331                               = emit_insn_before (gen_rtx (USE, VOIDmode,
11332                                                            piece),
11333                                                   p);
11334                             REG_NOTES (use_insn)
11335                               = gen_rtx (EXPR_LIST, REG_DEAD, piece,
11336                                          REG_NOTES (use_insn));
11337                           }
11338
11339                         all_used = 0;
11340                       }
11341
11342                   /* Check for the case where the register dying partially
11343                      overlaps the register set by this insn.  */
11344                   if (all_used)
11345                     for (i = regno; i < endregno; i++)
11346                       if (dead_or_set_regno_p (place, i))
11347                           {
11348                             all_used = 0;
11349                             break;
11350                           }
11351
11352                   if (! all_used)
11353                     {
11354                       /* Put only REG_DEAD notes for pieces that are
11355                          still used and that are not already dead or set.  */
11356
11357                       for (i = regno; i < endregno; i++)
11358                         {
11359                           rtx piece = gen_rtx (REG, reg_raw_mode[i], i);
11360
11361                           if ((reg_referenced_p (piece, PATTERN (place))
11362                                || (GET_CODE (place) == CALL_INSN
11363                                    && find_reg_fusage (place, USE, piece)))
11364                               && ! dead_or_set_p (place, piece)
11365                               && ! reg_bitfield_target_p (piece,
11366                                                           PATTERN (place)))
11367                             REG_NOTES (place) = gen_rtx (EXPR_LIST, REG_DEAD,
11368                                                          piece,
11369                                                          REG_NOTES (place));
11370                         }
11371
11372                       place = 0;
11373                     }
11374                 }
11375             }
11376           break;
11377
11378         default:
11379           /* Any other notes should not be present at this point in the
11380              compilation.  */
11381           abort ();
11382         }
11383
11384       if (place)
11385         {
11386           XEXP (note, 1) = REG_NOTES (place);
11387           REG_NOTES (place) = note;
11388         }
11389       else if ((REG_NOTE_KIND (note) == REG_DEAD
11390                 || REG_NOTE_KIND (note) == REG_UNUSED)
11391                && GET_CODE (XEXP (note, 0)) == REG)
11392         reg_n_deaths[REGNO (XEXP (note, 0))]--;
11393
11394       if (place2)
11395         {
11396           if ((REG_NOTE_KIND (note) == REG_DEAD
11397                || REG_NOTE_KIND (note) == REG_UNUSED)
11398               && GET_CODE (XEXP (note, 0)) == REG)
11399             reg_n_deaths[REGNO (XEXP (note, 0))]++;
11400
11401           REG_NOTES (place2) = gen_rtx (GET_CODE (note), REG_NOTE_KIND (note),
11402                                         XEXP (note, 0), REG_NOTES (place2));
11403         }
11404     }
11405 }
11406 \f
11407 /* Similarly to above, distribute the LOG_LINKS that used to be present on
11408    I3, I2, and I1 to new locations.  This is also called in one case to
11409    add a link pointing at I3 when I3's destination is changed.  */
11410
11411 static void
11412 distribute_links (links)
11413      rtx links;
11414 {
11415   rtx link, next_link;
11416
11417   for (link = links; link; link = next_link)
11418     {
11419       rtx place = 0;
11420       rtx insn;
11421       rtx set, reg;
11422
11423       next_link = XEXP (link, 1);
11424
11425       /* If the insn that this link points to is a NOTE or isn't a single
11426          set, ignore it.  In the latter case, it isn't clear what we
11427          can do other than ignore the link, since we can't tell which 
11428          register it was for.  Such links wouldn't be used by combine
11429          anyway.
11430
11431          It is not possible for the destination of the target of the link to
11432          have been changed by combine.  The only potential of this is if we
11433          replace I3, I2, and I1 by I3 and I2.  But in that case the
11434          destination of I2 also remains unchanged.  */
11435
11436       if (GET_CODE (XEXP (link, 0)) == NOTE
11437           || (set = single_set (XEXP (link, 0))) == 0)
11438         continue;
11439
11440       reg = SET_DEST (set);
11441       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
11442              || GET_CODE (reg) == SIGN_EXTRACT
11443              || GET_CODE (reg) == STRICT_LOW_PART)
11444         reg = XEXP (reg, 0);
11445
11446       /* A LOG_LINK is defined as being placed on the first insn that uses
11447          a register and points to the insn that sets the register.  Start
11448          searching at the next insn after the target of the link and stop
11449          when we reach a set of the register or the end of the basic block.
11450
11451          Note that this correctly handles the link that used to point from
11452          I3 to I2.  Also note that not much searching is typically done here
11453          since most links don't point very far away.  */
11454
11455       for (insn = NEXT_INSN (XEXP (link, 0));
11456            (insn && (this_basic_block == n_basic_blocks - 1
11457                      || basic_block_head[this_basic_block + 1] != insn));
11458            insn = NEXT_INSN (insn))
11459         if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
11460             && reg_overlap_mentioned_p (reg, PATTERN (insn)))
11461           {
11462             if (reg_referenced_p (reg, PATTERN (insn)))
11463               place = insn;
11464             break;
11465           }
11466         else if (GET_CODE (insn) == CALL_INSN
11467               && find_reg_fusage (insn, USE, reg))
11468           {
11469             place = insn;
11470             break;
11471           }
11472
11473       /* If we found a place to put the link, place it there unless there
11474          is already a link to the same insn as LINK at that point.  */
11475
11476       if (place)
11477         {
11478           rtx link2;
11479
11480           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
11481             if (XEXP (link2, 0) == XEXP (link, 0))
11482               break;
11483
11484           if (link2 == 0)
11485             {
11486               XEXP (link, 1) = LOG_LINKS (place);
11487               LOG_LINKS (place) = link;
11488
11489               /* Set added_links_insn to the earliest insn we added a
11490                  link to.  */
11491               if (added_links_insn == 0 
11492                   || INSN_CUID (added_links_insn) > INSN_CUID (place))
11493                 added_links_insn = place;
11494             }
11495         }
11496     }
11497 }
11498 \f
11499 /* Compute INSN_CUID for INSN, which is an insn made by combine.  */
11500
11501 static int
11502 insn_cuid (insn)
11503      rtx insn;
11504 {
11505   while (insn != 0 && INSN_UID (insn) > max_uid_cuid
11506          && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
11507     insn = NEXT_INSN (insn);
11508
11509   if (INSN_UID (insn) > max_uid_cuid)
11510     abort ();
11511
11512   return INSN_CUID (insn);
11513 }
11514 \f
11515 void
11516 dump_combine_stats (file)
11517      FILE *file;
11518 {
11519   fprintf
11520     (file,
11521      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
11522      combine_attempts, combine_merges, combine_extras, combine_successes);
11523 }
11524
11525 void
11526 dump_combine_total_stats (file)
11527      FILE *file;
11528 {
11529   fprintf
11530     (file,
11531      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
11532      total_attempts, total_merges, total_extras, total_successes);
11533 }