OSDN Git Service

* Check in merge from gcc2. See ChangeLog.11 and ChangeLog.12
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 88, 92-97, 1998 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 /* stdio.h must precede rtl.h for FFS.  */
85 #include "system.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;
469 #ifdef HAVE_cc0
470   register rtx prev;
471 #endif
472   register int i;
473   register rtx links, nextlinks;
474
475   combine_attempts = 0;
476   combine_merges = 0;
477   combine_extras = 0;
478   combine_successes = 0;
479   undobuf.undos = undobuf.previous_undos = 0;
480
481   combine_max_regno = nregs;
482
483   reg_nonzero_bits
484     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
485   reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
486
487   bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
488   bzero (reg_sign_bit_copies, nregs * sizeof (char));
489
490   reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
491   reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
492   reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
493   reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
494   reg_last_set_label = (int *) alloca (nregs * sizeof (int));
495   reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
496   reg_last_set_mode
497     = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
498   reg_last_set_nonzero_bits
499     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
500   reg_last_set_sign_bit_copies
501     = (char *) alloca (nregs * sizeof (char));
502
503   init_reg_last_arrays ();
504
505   init_recog_no_volatile ();
506
507   /* Compute maximum uid value so uid_cuid can be allocated.  */
508
509   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
510     if (INSN_UID (insn) > i)
511       i = INSN_UID (insn);
512
513   uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
514   max_uid_cuid = i;
515
516   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
517
518   /* Don't use reg_nonzero_bits when computing it.  This can cause problems
519      when, for example, we have j <<= 1 in a loop.  */
520
521   nonzero_sign_valid = 0;
522
523   /* Compute the mapping from uids to cuids.
524      Cuids are numbers assigned to insns, like uids,
525      except that cuids increase monotonically through the code. 
526
527      Scan all SETs and see if we can deduce anything about what
528      bits are known to be zero for some registers and how many copies
529      of the sign bit are known to exist for those registers.
530
531      Also set any known values so that we can use it while searching
532      for what bits are known to be set.  */
533
534   label_tick = 1;
535
536   /* We need to initialize it here, because record_dead_and_set_regs may call
537      get_last_value.  */
538   subst_prev_insn = NULL_RTX;
539
540   setup_incoming_promotions ();
541
542   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
543     {
544       uid_cuid[INSN_UID (insn)] = ++i;
545       subst_low_cuid = i;
546       subst_insn = insn;
547
548       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
549         {
550           note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
551           record_dead_and_set_regs (insn);
552
553 #ifdef AUTO_INC_DEC
554           for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
555             if (REG_NOTE_KIND (links) == REG_INC)
556               set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX);
557 #endif
558         }
559
560       if (GET_CODE (insn) == CODE_LABEL)
561         label_tick++;
562     }
563
564   nonzero_sign_valid = 1;
565
566   /* Now scan all the insns in forward order.  */
567
568   this_basic_block = -1;
569   label_tick = 1;
570   last_call_cuid = 0;
571   mem_last_set = 0;
572   init_reg_last_arrays ();
573   setup_incoming_promotions ();
574
575   for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
576     {
577       next = 0;
578
579       /* If INSN starts a new basic block, update our basic block number.  */
580       if (this_basic_block + 1 < n_basic_blocks
581           && basic_block_head[this_basic_block + 1] == insn)
582         this_basic_block++;
583
584       if (GET_CODE (insn) == CODE_LABEL)
585         label_tick++;
586
587       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
588         {
589           /* Try this insn with each insn it links back to.  */
590
591           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
592             if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
593               goto retry;
594
595           /* Try each sequence of three linked insns ending with this one.  */
596
597           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
598             for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
599                  nextlinks = XEXP (nextlinks, 1))
600               if ((next = try_combine (insn, XEXP (links, 0),
601                                        XEXP (nextlinks, 0))) != 0)
602                 goto retry;
603
604 #ifdef HAVE_cc0
605           /* Try to combine a jump insn that uses CC0
606              with a preceding insn that sets CC0, and maybe with its
607              logical predecessor as well.
608              This is how we make decrement-and-branch insns.
609              We need this special code because data flow connections
610              via CC0 do not get entered in LOG_LINKS.  */
611
612           if (GET_CODE (insn) == JUMP_INSN
613               && (prev = prev_nonnote_insn (insn)) != 0
614               && GET_CODE (prev) == INSN
615               && sets_cc0_p (PATTERN (prev)))
616             {
617               if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
618                 goto retry;
619
620               for (nextlinks = LOG_LINKS (prev); nextlinks;
621                    nextlinks = XEXP (nextlinks, 1))
622                 if ((next = try_combine (insn, prev,
623                                          XEXP (nextlinks, 0))) != 0)
624                   goto retry;
625             }
626
627           /* Do the same for an insn that explicitly references CC0.  */
628           if (GET_CODE (insn) == INSN
629               && (prev = prev_nonnote_insn (insn)) != 0
630               && GET_CODE (prev) == INSN
631               && sets_cc0_p (PATTERN (prev))
632               && GET_CODE (PATTERN (insn)) == SET
633               && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
634             {
635               if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
636                 goto retry;
637
638               for (nextlinks = LOG_LINKS (prev); nextlinks;
639                    nextlinks = XEXP (nextlinks, 1))
640                 if ((next = try_combine (insn, prev,
641                                          XEXP (nextlinks, 0))) != 0)
642                   goto retry;
643             }
644
645           /* Finally, see if any of the insns that this insn links to
646              explicitly references CC0.  If so, try this insn, that insn,
647              and its predecessor if it sets CC0.  */
648           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
649             if (GET_CODE (XEXP (links, 0)) == INSN
650                 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
651                 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
652                 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
653                 && GET_CODE (prev) == INSN
654                 && sets_cc0_p (PATTERN (prev))
655                 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
656               goto retry;
657 #endif
658
659           /* Try combining an insn with two different insns whose results it
660              uses.  */
661           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
662             for (nextlinks = XEXP (links, 1); nextlinks;
663                  nextlinks = XEXP (nextlinks, 1))
664               if ((next = try_combine (insn, XEXP (links, 0),
665                                        XEXP (nextlinks, 0))) != 0)
666                 goto retry;
667
668           if (GET_CODE (insn) != NOTE)
669             record_dead_and_set_regs (insn);
670
671         retry:
672           ;
673         }
674     }
675
676   total_attempts += combine_attempts;
677   total_merges += combine_merges;
678   total_extras += combine_extras;
679   total_successes += combine_successes;
680
681   nonzero_sign_valid = 0;
682 }
683
684 /* Wipe the reg_last_xxx arrays in preparation for another pass.  */
685
686 static void
687 init_reg_last_arrays ()
688 {
689   int nregs = combine_max_regno;
690
691   bzero ((char *) reg_last_death, nregs * sizeof (rtx));
692   bzero ((char *) reg_last_set, nregs * sizeof (rtx));
693   bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
694   bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
695   bzero ((char *) reg_last_set_label, nregs * sizeof (int));
696   bzero (reg_last_set_invalid, nregs * sizeof (char));
697   bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
698   bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
699   bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
700 }
701 \f
702 /* Set up any promoted values for incoming argument registers.  */
703
704 static void
705 setup_incoming_promotions ()
706 {
707 #ifdef PROMOTE_FUNCTION_ARGS
708   int regno;
709   rtx reg;
710   enum machine_mode mode;
711   int unsignedp;
712   rtx first = get_insns ();
713
714   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
715     if (FUNCTION_ARG_REGNO_P (regno)
716         && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
717       {
718         record_value_for_reg
719           (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
720                                        : SIGN_EXTEND),
721                                       GET_MODE (reg),
722                                       gen_rtx_CLOBBER (mode, const0_rtx)));
723       }
724 #endif
725 }
726 \f
727 /* Called via note_stores.  If X is a pseudo that is narrower than
728    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
729
730    If we are setting only a portion of X and we can't figure out what
731    portion, assume all bits will be used since we don't know what will
732    be happening.
733
734    Similarly, set how many bits of X are known to be copies of the sign bit
735    at all locations in the function.  This is the smallest number implied 
736    by any set of X.  */
737
738 static void
739 set_nonzero_bits_and_sign_copies (x, set)
740      rtx x;
741      rtx set;
742 {
743   int num;
744
745   if (GET_CODE (x) == REG
746       && REGNO (x) >= FIRST_PSEUDO_REGISTER
747       /* If this register is undefined at the start of the file, we can't
748          say what its contents were.  */
749       && ! REGNO_REG_SET_P (basic_block_live_at_start[0], REGNO (x))
750       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
751     {
752       if (set == 0 || GET_CODE (set) == CLOBBER)
753         {
754           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
755           reg_sign_bit_copies[REGNO (x)] = 1;
756           return;
757         }
758
759       /* If this is a complex assignment, see if we can convert it into a
760          simple assignment.  */
761       set = expand_field_assignment (set);
762
763       /* If this is a simple assignment, or we have a paradoxical SUBREG,
764          set what we know about X.  */
765
766       if (SET_DEST (set) == x
767           || (GET_CODE (SET_DEST (set)) == SUBREG
768               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
769                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
770               && SUBREG_REG (SET_DEST (set)) == x))
771         {
772           rtx src = SET_SRC (set);
773
774 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
775           /* If X is narrower than a word and SRC is a non-negative
776              constant that would appear negative in the mode of X,
777              sign-extend it for use in reg_nonzero_bits because some
778              machines (maybe most) will actually do the sign-extension
779              and this is the conservative approach. 
780
781              ??? For 2.5, try to tighten up the MD files in this regard
782              instead of this kludge.  */
783
784           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
785               && GET_CODE (src) == CONST_INT
786               && INTVAL (src) > 0
787               && 0 != (INTVAL (src)
788                        & ((HOST_WIDE_INT) 1
789                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
790             src = GEN_INT (INTVAL (src)
791                            | ((HOST_WIDE_INT) (-1)
792                               << GET_MODE_BITSIZE (GET_MODE (x))));
793 #endif
794
795           reg_nonzero_bits[REGNO (x)]
796             |= nonzero_bits (src, nonzero_bits_mode);
797           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
798           if (reg_sign_bit_copies[REGNO (x)] == 0
799               || reg_sign_bit_copies[REGNO (x)] > num)
800             reg_sign_bit_copies[REGNO (x)] = num;
801         }
802       else
803         {
804           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
805           reg_sign_bit_copies[REGNO (x)] = 1;
806         }
807     }
808 }
809 \f
810 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
811    insns that were previously combined into I3 or that will be combined
812    into the merger of INSN and I3.
813
814    Return 0 if the combination is not allowed for any reason.
815
816    If the combination is allowed, *PDEST will be set to the single 
817    destination of INSN and *PSRC to the single source, and this function
818    will return 1.  */
819
820 static int
821 can_combine_p (insn, i3, pred, succ, pdest, psrc)
822      rtx insn;
823      rtx i3;
824      rtx pred, succ;
825      rtx *pdest, *psrc;
826 {
827   int i;
828   rtx set = 0, src, dest;
829   rtx p;
830 #ifdef AUTO_INC_DEC
831   rtx link;
832 #endif
833   int all_adjacent = (succ ? (next_active_insn (insn) == succ
834                               && next_active_insn (succ) == i3)
835                       : next_active_insn (insn) == i3);
836
837   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
838      or a PARALLEL consisting of such a SET and CLOBBERs. 
839
840      If INSN has CLOBBER parallel parts, ignore them for our processing.
841      By definition, these happen during the execution of the insn.  When it
842      is merged with another insn, all bets are off.  If they are, in fact,
843      needed and aren't also supplied in I3, they may be added by
844      recog_for_combine.  Otherwise, it won't match. 
845
846      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
847      note.
848
849      Get the source and destination of INSN.  If more than one, can't 
850      combine.  */
851      
852   if (GET_CODE (PATTERN (insn)) == SET)
853     set = PATTERN (insn);
854   else if (GET_CODE (PATTERN (insn)) == PARALLEL
855            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
856     {
857       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
858         {
859           rtx elt = XVECEXP (PATTERN (insn), 0, i);
860
861           switch (GET_CODE (elt))
862             {
863             /* This is important to combine floating point insns
864                for the SH4 port.  */
865             case USE:
866               /* Combining an isolated USE doesn't make sense.
867                  We depend here on combinable_i3_pat to reject them.  */
868               /* The code below this loop only verifies that the inputs of
869                  the SET in INSN do not change.  We call reg_set_between_p
870                  to verify that the REG in the USE does not change betweeen
871                  I3 and INSN.
872                  If the USE in INSN was for a pseudo register, the matching
873                  insn pattern will likely match any register; combining this
874                  with any other USE would only be safe if we knew that the
875                  used registers have identical values, or if there was
876                  something to tell them apart, e.g. different modes.  For
877                  now, we forgo such compilcated tests and simply disallow
878                  combining of USES of pseudo registers with any other USE.  */
879               if (GET_CODE (XEXP (elt, 0)) == REG
880                   && GET_CODE (PATTERN (i3)) == PARALLEL)
881                 {
882                   rtx i3pat = PATTERN (i3);
883                   int i = XVECLEN (i3pat, 0) - 1;
884                   int regno = REGNO (XEXP (elt, 0));
885                   do
886                     {
887                       rtx i3elt = XVECEXP (i3pat, 0, i);
888                       if (GET_CODE (i3elt) == USE
889                           && GET_CODE (XEXP (i3elt, 0)) == REG
890                           && (REGNO (XEXP (i3elt, 0)) == regno
891                               ? reg_set_between_p (XEXP (elt, 0),
892                                                    PREV_INSN (insn), i3)
893                               : regno >= FIRST_PSEUDO_REGISTER))
894                         return 0;
895                     }
896                   while (--i >= 0);
897                 }
898               break;
899
900               /* We can ignore CLOBBERs.  */
901             case CLOBBER:
902               break;
903
904             case SET:
905               /* Ignore SETs whose result isn't used but not those that
906                  have side-effects.  */
907               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
908                   && ! side_effects_p (elt))
909                 break;
910
911               /* If we have already found a SET, this is a second one and
912                  so we cannot combine with this insn.  */
913               if (set)
914                 return 0;
915
916               set = elt;
917               break;
918
919             default:
920               /* Anything else means we can't combine.  */
921               return 0;
922             }
923         }
924
925       if (set == 0
926           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
927              so don't do anything with it.  */
928           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
929         return 0;
930     }
931   else
932     return 0;
933
934   if (set == 0)
935     return 0;
936
937   set = expand_field_assignment (set);
938   src = SET_SRC (set), dest = SET_DEST (set);
939
940   /* Don't eliminate a store in the stack pointer.  */
941   if (dest == stack_pointer_rtx
942       /* If we couldn't eliminate a field assignment, we can't combine.  */
943       || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
944       /* Don't combine with an insn that sets a register to itself if it has
945          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
946       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
947       /* Can't merge a function call.  */
948       || GET_CODE (src) == CALL
949       /* Don't eliminate a function call argument.  */
950       || (GET_CODE (i3) == CALL_INSN
951           && (find_reg_fusage (i3, USE, dest)
952               || (GET_CODE (dest) == REG
953                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
954                   && global_regs[REGNO (dest)])))
955       /* Don't substitute into an incremented register.  */
956       || FIND_REG_INC_NOTE (i3, dest)
957       || (succ && FIND_REG_INC_NOTE (succ, dest))
958       /* Don't combine the end of a libcall into anything.  */
959       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
960       /* Make sure that DEST is not used after SUCC but before I3.  */
961       || (succ && ! all_adjacent
962           && reg_used_between_p (dest, succ, i3))
963       /* Make sure that the value that is to be substituted for the register
964          does not use any registers whose values alter in between.  However,
965          If the insns are adjacent, a use can't cross a set even though we
966          think it might (this can happen for a sequence of insns each setting
967          the same destination; reg_last_set of that register might point to
968          a NOTE).  If INSN has a REG_EQUIV note, the register is always
969          equivalent to the memory so the substitution is valid even if there
970          are intervening stores.  Also, don't move a volatile asm or
971          UNSPEC_VOLATILE across any other insns.  */
972       || (! all_adjacent
973           && (((GET_CODE (src) != MEM
974                 || ! find_reg_note (insn, REG_EQUIV, src))
975                && use_crosses_set_p (src, INSN_CUID (insn)))
976               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
977               || GET_CODE (src) == UNSPEC_VOLATILE))
978       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
979          better register allocation by not doing the combine.  */
980       || find_reg_note (i3, REG_NO_CONFLICT, dest)
981       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
982       /* Don't combine across a CALL_INSN, because that would possibly
983          change whether the life span of some REGs crosses calls or not,
984          and it is a pain to update that information.
985          Exception: if source is a constant, moving it later can't hurt.
986          Accept that special case, because it helps -fforce-addr a lot.  */
987       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
988     return 0;
989
990   /* DEST must either be a REG or CC0.  */
991   if (GET_CODE (dest) == REG)
992     {
993       /* If register alignment is being enforced for multi-word items in all
994          cases except for parameters, it is possible to have a register copy
995          insn referencing a hard register that is not allowed to contain the
996          mode being copied and which would not be valid as an operand of most
997          insns.  Eliminate this problem by not combining with such an insn.
998
999          Also, on some machines we don't want to extend the life of a hard
1000          register.
1001
1002          This is the same test done in can_combine except that we don't test
1003          if SRC is a CALL operation to permit a hard register with
1004          SMALL_REGISTER_CLASSES, and that we have to take all_adjacent
1005          into account.  */
1006
1007       if (GET_CODE (src) == REG
1008           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1009                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1010               /* Don't extend the life of a hard register unless it is
1011                  user variable (if we have few registers) or it can't
1012                  fit into the desired register (meaning something special
1013                  is going on).
1014                  Also avoid substituting a return register into I3, because
1015                  reload can't handle a conflict with constraints of other
1016                  inputs.  */
1017               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1018                   && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
1019                       || (SMALL_REGISTER_CLASSES
1020                           && ((! all_adjacent && ! REG_USERVAR_P (src))
1021                               || (FUNCTION_VALUE_REGNO_P (REGNO (src))
1022                                   && ! REG_USERVAR_P (src))))))))
1023         return 0;
1024     }
1025   else if (GET_CODE (dest) != CC0)
1026     return 0;
1027
1028   /* Don't substitute for a register intended as a clobberable operand.
1029      Similarly, don't substitute an expression containing a register that
1030      will be clobbered in I3.  */
1031   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1032     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1033       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1034           && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1035                                        src)
1036               || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1037         return 0;
1038
1039   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1040      or not), reject, unless nothing volatile comes between it and I3,
1041      with the exception of SUCC.  */
1042
1043   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1044     for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1045       if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1046           && p != succ && volatile_refs_p (PATTERN (p)))
1047         return 0;
1048
1049   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1050      to be an explicit register variable, and was chosen for a reason.  */
1051
1052   if (GET_CODE (src) == ASM_OPERANDS
1053       && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1054     return 0;
1055
1056   /* If there are any volatile insns between INSN and I3, reject, because
1057      they might affect machine state.  */
1058
1059   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1060     if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1061         && p != succ && volatile_insn_p (PATTERN (p)))
1062       return 0;
1063
1064   /* If INSN or I2 contains an autoincrement or autodecrement,
1065      make sure that register is not used between there and I3,
1066      and not already used in I3 either.
1067      Also insist that I3 not be a jump; if it were one
1068      and the incremented register were spilled, we would lose.  */
1069
1070 #ifdef AUTO_INC_DEC
1071   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1072     if (REG_NOTE_KIND (link) == REG_INC
1073         && (GET_CODE (i3) == JUMP_INSN
1074             || reg_used_between_p (XEXP (link, 0), insn, i3)
1075             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1076       return 0;
1077 #endif
1078
1079 #ifdef HAVE_cc0
1080   /* Don't combine an insn that follows a CC0-setting insn.
1081      An insn that uses CC0 must not be separated from the one that sets it.
1082      We do, however, allow I2 to follow a CC0-setting insn if that insn
1083      is passed as I1; in that case it will be deleted also.
1084      We also allow combining in this case if all the insns are adjacent
1085      because that would leave the two CC0 insns adjacent as well.
1086      It would be more logical to test whether CC0 occurs inside I1 or I2,
1087      but that would be much slower, and this ought to be equivalent.  */
1088
1089   p = prev_nonnote_insn (insn);
1090   if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1091       && ! all_adjacent)
1092     return 0;
1093 #endif
1094
1095   /* If we get here, we have passed all the tests and the combination is
1096      to be allowed.  */
1097
1098   *pdest = dest;
1099   *psrc = src;
1100
1101   return 1;
1102 }
1103 \f
1104 /* Check if PAT is an insn - or a part of it - used to set up an
1105    argument for a function in a hard register.  */
1106
1107 static int
1108 sets_function_arg_p (pat)
1109      rtx pat;
1110 {
1111   int i;
1112   rtx inner_dest;
1113
1114   switch (GET_CODE (pat))
1115     {
1116     case INSN:
1117       return sets_function_arg_p (PATTERN (pat));
1118
1119     case PARALLEL:
1120       for (i = XVECLEN (pat, 0); --i >= 0;)
1121         if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1122           return 1;
1123
1124       break;
1125
1126     case SET:
1127       inner_dest = SET_DEST (pat);
1128       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1129              || GET_CODE (inner_dest) == SUBREG
1130              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1131         inner_dest = XEXP (inner_dest, 0);
1132
1133       return (GET_CODE (inner_dest) == REG
1134               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1135               && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1136
1137     default:
1138       break;
1139     }
1140
1141   return 0;
1142 }
1143
1144 /* LOC is the location within I3 that contains its pattern or the component
1145    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1146
1147    One problem is if I3 modifies its output, as opposed to replacing it
1148    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1149    so would produce an insn that is not equivalent to the original insns.
1150
1151    Consider:
1152
1153          (set (reg:DI 101) (reg:DI 100))
1154          (set (subreg:SI (reg:DI 101) 0) <foo>)
1155
1156    This is NOT equivalent to:
1157
1158          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1159                     (set (reg:DI 101) (reg:DI 100))])
1160
1161    Not only does this modify 100 (in which case it might still be valid
1162    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100. 
1163
1164    We can also run into a problem if I2 sets a register that I1
1165    uses and I1 gets directly substituted into I3 (not via I2).  In that
1166    case, we would be getting the wrong value of I2DEST into I3, so we
1167    must reject the combination.  This case occurs when I2 and I1 both
1168    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1169    If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1170    of a SET must prevent combination from occurring.
1171
1172    On machines where SMALL_REGISTER_CLASSES is non-zero, we don't combine
1173    if the destination of a SET is a hard register that isn't a user
1174    variable.
1175
1176    Before doing the above check, we first try to expand a field assignment
1177    into a set of logical operations.
1178
1179    If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1180    we place a register that is both set and used within I3.  If more than one
1181    such register is detected, we fail.
1182
1183    Return 1 if the combination is valid, zero otherwise.  */
1184
1185 static int
1186 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1187      rtx i3;
1188      rtx *loc;
1189      rtx i2dest;
1190      rtx i1dest;
1191      int i1_not_in_src;
1192      rtx *pi3dest_killed;
1193 {
1194   rtx x = *loc;
1195
1196   if (GET_CODE (x) == SET)
1197     {
1198       rtx set = expand_field_assignment (x);
1199       rtx dest = SET_DEST (set);
1200       rtx src = SET_SRC (set);
1201       rtx inner_dest = dest;
1202  
1203 #if 0
1204       rtx inner_src = src;
1205 #endif
1206
1207       SUBST (*loc, set);
1208
1209       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1210              || GET_CODE (inner_dest) == SUBREG
1211              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1212         inner_dest = XEXP (inner_dest, 0);
1213
1214   /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1215      was added.  */
1216 #if 0
1217       while (GET_CODE (inner_src) == STRICT_LOW_PART
1218              || GET_CODE (inner_src) == SUBREG
1219              || GET_CODE (inner_src) == ZERO_EXTRACT)
1220         inner_src = XEXP (inner_src, 0);
1221
1222       /* If it is better that two different modes keep two different pseudos,
1223          avoid combining them.  This avoids producing the following pattern
1224          on a 386:
1225           (set (subreg:SI (reg/v:QI 21) 0)
1226                (lshiftrt:SI (reg/v:SI 20)
1227                    (const_int 24)))
1228          If that were made, reload could not handle the pair of
1229          reg 20/21, since it would try to get any GENERAL_REGS
1230          but some of them don't handle QImode.  */
1231
1232       if (rtx_equal_p (inner_src, i2dest)
1233           && GET_CODE (inner_dest) == REG
1234           && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1235         return 0;
1236 #endif
1237
1238       /* Check for the case where I3 modifies its output, as
1239          discussed above.  */
1240       if ((inner_dest != dest
1241            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1242                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1243
1244           /* This is the same test done in can_combine_p except that we
1245              allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
1246              CALL operation. Moreover, we can't test all_adjacent; we don't
1247              have to, since this instruction will stay in place, thus we are
1248              not considering increasing the lifetime of INNER_DEST.
1249
1250              Also, if this insn sets a function argument, combining it with
1251              something that might need a spill could clobber a previous
1252              function argument; the all_adjacent test in can_combine_p also
1253              checks this; here, we do a more specific test for this case.  */
1254              
1255           || (GET_CODE (inner_dest) == REG
1256               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1257               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1258                                         GET_MODE (inner_dest))
1259                  || (SMALL_REGISTER_CLASSES && GET_CODE (src) != CALL
1260                      && ! REG_USERVAR_P (inner_dest)
1261                      && (FUNCTION_VALUE_REGNO_P (REGNO (inner_dest))
1262                          || (FUNCTION_ARG_REGNO_P (REGNO (inner_dest))
1263                              && i3 != 0
1264                              && sets_function_arg_p (prev_nonnote_insn (i3)))))))
1265           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1266         return 0;
1267
1268       /* If DEST is used in I3, it is being killed in this insn,
1269          so record that for later. 
1270          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1271          STACK_POINTER_REGNUM, since these are always considered to be
1272          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1273       if (pi3dest_killed && GET_CODE (dest) == REG
1274           && reg_referenced_p (dest, PATTERN (i3))
1275           && REGNO (dest) != FRAME_POINTER_REGNUM
1276 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1277           && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1278 #endif
1279 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1280           && (REGNO (dest) != ARG_POINTER_REGNUM
1281               || ! fixed_regs [REGNO (dest)])
1282 #endif
1283           && REGNO (dest) != STACK_POINTER_REGNUM)
1284         {
1285           if (*pi3dest_killed)
1286             return 0;
1287
1288           *pi3dest_killed = dest;
1289         }
1290     }
1291
1292   else if (GET_CODE (x) == PARALLEL)
1293     {
1294       int i;
1295
1296       for (i = 0; i < XVECLEN (x, 0); i++)
1297         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1298                                 i1_not_in_src, pi3dest_killed))
1299           return 0;
1300     }
1301
1302   return 1;
1303 }
1304 \f
1305 /* Try to combine the insns I1 and I2 into I3.
1306    Here I1 and I2 appear earlier than I3.
1307    I1 can be zero; then we combine just I2 into I3.
1308  
1309    It we are combining three insns and the resulting insn is not recognized,
1310    try splitting it into two insns.  If that happens, I2 and I3 are retained
1311    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1312    are pseudo-deleted.
1313
1314    Return 0 if the combination does not work.  Then nothing is changed. 
1315    If we did the combination, return the insn at which combine should
1316    resume scanning.  */
1317
1318 static rtx
1319 try_combine (i3, i2, i1)
1320      register rtx i3, i2, i1;
1321 {
1322   /* New patterns for I3 and I3, respectively.  */
1323   rtx newpat, newi2pat = 0;
1324   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1325   int added_sets_1, added_sets_2;
1326   /* Total number of SETs to put into I3.  */
1327   int total_sets;
1328   /* Nonzero is I2's body now appears in I3.  */
1329   int i2_is_used;
1330   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1331   int insn_code_number, i2_code_number, other_code_number;
1332   /* Contains I3 if the destination of I3 is used in its source, which means
1333      that the old life of I3 is being killed.  If that usage is placed into
1334      I2 and not in I3, a REG_DEAD note must be made.  */
1335   rtx i3dest_killed = 0;
1336   /* SET_DEST and SET_SRC of I2 and I1.  */
1337   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1338   /* PATTERN (I2), or a copy of it in certain cases.  */
1339   rtx i2pat;
1340   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1341   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1342   int i1_feeds_i3 = 0;
1343   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1344   rtx new_i3_notes, new_i2_notes;
1345   /* Notes that we substituted I3 into I2 instead of the normal case.  */
1346   int i3_subst_into_i2 = 0;
1347   /* Notes that I1, I2 or I3 is a MULT operation.  */
1348   int have_mult = 0;
1349   /* Number of clobbers of SCRATCH we had to add.  */
1350   int i3_scratches = 0, i2_scratches = 0, other_scratches = 0;
1351
1352   int maxreg;
1353   rtx temp;
1354   register rtx link;
1355   int i;
1356
1357   /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1358      This can occur when flow deletes an insn that it has merged into an
1359      auto-increment address.  We also can't do anything if I3 has a
1360      REG_LIBCALL note since we don't want to disrupt the contiguity of a
1361      libcall.  */
1362
1363   if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1364       || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1365       || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
1366       || find_reg_note (i3, REG_LIBCALL, NULL_RTX))
1367     return 0;
1368
1369   combine_attempts++;
1370
1371   undobuf.undos = undobuf.previous_undos = 0;
1372   undobuf.other_insn = 0;
1373
1374   /* Save the current high-water-mark so we can free storage if we didn't
1375      accept this combination.  */
1376   undobuf.storage = (char *) oballoc (0);
1377
1378   /* Reset the hard register usage information.  */
1379   CLEAR_HARD_REG_SET (newpat_used_regs);
1380
1381   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1382      code below, set I1 to be the earlier of the two insns.  */
1383   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1384     temp = i1, i1 = i2, i2 = temp;
1385
1386   added_links_insn = 0;
1387
1388   /* First check for one important special-case that the code below will
1389      not handle.  Namely, the case where I1 is zero, I2 has multiple sets,
1390      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1391      we may be able to replace that destination with the destination of I3.
1392      This occurs in the common code where we compute both a quotient and
1393      remainder into a structure, in which case we want to do the computation
1394      directly into the structure to avoid register-register copies.
1395
1396      We make very conservative checks below and only try to handle the
1397      most common cases of this.  For example, we only handle the case
1398      where I2 and I3 are adjacent to avoid making difficult register
1399      usage tests.  */
1400
1401   if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1402       && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1403       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1404       && (! SMALL_REGISTER_CLASSES
1405           || (GET_CODE (SET_DEST (PATTERN (i3))) != REG
1406               || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1407               || REG_USERVAR_P (SET_DEST (PATTERN (i3)))))
1408       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1409       && GET_CODE (PATTERN (i2)) == PARALLEL
1410       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1411       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1412          below would need to check what is inside (and reg_overlap_mentioned_p
1413          doesn't support those codes anyway).  Don't allow those destinations;
1414          the resulting insn isn't likely to be recognized anyway.  */
1415       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1416       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1417       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1418                                     SET_DEST (PATTERN (i3)))
1419       && next_real_insn (i2) == i3)
1420     {
1421       rtx p2 = PATTERN (i2);
1422
1423       /* Make sure that the destination of I3,
1424          which we are going to substitute into one output of I2,
1425          is not used within another output of I2.  We must avoid making this:
1426          (parallel [(set (mem (reg 69)) ...)
1427                     (set (reg 69) ...)])
1428          which is not well-defined as to order of actions.
1429          (Besides, reload can't handle output reloads for this.)
1430
1431          The problem can also happen if the dest of I3 is a memory ref,
1432          if another dest in I2 is an indirect memory ref.  */
1433       for (i = 0; i < XVECLEN (p2, 0); i++)
1434         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1435              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1436             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1437                                         SET_DEST (XVECEXP (p2, 0, i))))
1438           break;
1439
1440       if (i == XVECLEN (p2, 0))
1441         for (i = 0; i < XVECLEN (p2, 0); i++)
1442           if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1443             {
1444               combine_merges++;
1445
1446               subst_insn = i3;
1447               subst_low_cuid = INSN_CUID (i2);
1448
1449               added_sets_2 = added_sets_1 = 0;
1450               i2dest = SET_SRC (PATTERN (i3));
1451
1452               /* Replace the dest in I2 with our dest and make the resulting
1453                  insn the new pattern for I3.  Then skip to where we
1454                  validate the pattern.  Everything was set up above.  */
1455               SUBST (SET_DEST (XVECEXP (p2, 0, i)), 
1456                      SET_DEST (PATTERN (i3)));
1457
1458               newpat = p2;
1459               i3_subst_into_i2 = 1;
1460               goto validate_replacement;
1461             }
1462     }
1463
1464 #ifndef HAVE_cc0
1465   /* If we have no I1 and I2 looks like:
1466         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1467                    (set Y OP)])
1468      make up a dummy I1 that is
1469         (set Y OP)
1470      and change I2 to be
1471         (set (reg:CC X) (compare:CC Y (const_int 0)))
1472
1473      (We can ignore any trailing CLOBBERs.)
1474
1475      This undoes a previous combination and allows us to match a branch-and-
1476      decrement insn.  */
1477
1478   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1479       && XVECLEN (PATTERN (i2), 0) >= 2
1480       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1481       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1482           == MODE_CC)
1483       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1484       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1485       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1486       && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1487       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1488                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1489     {
1490       for (i =  XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1491         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1492           break;
1493
1494       if (i == 1)
1495         {
1496           /* We make I1 with the same INSN_UID as I2.  This gives it
1497              the same INSN_CUID for value tracking.  Our fake I1 will
1498              never appear in the insn stream so giving it the same INSN_UID
1499              as I2 will not cause a problem.  */
1500
1501           subst_prev_insn = i1
1502             = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1503                             XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1504                             NULL_RTX);
1505
1506           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1507           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1508                  SET_DEST (PATTERN (i1)));
1509         }
1510     }
1511 #endif
1512
1513   /* Verify that I2 and I1 are valid for combining.  */
1514   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1515       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1516     {
1517       undo_all ();
1518       return 0;
1519     }
1520
1521   /* Record whether I2DEST is used in I2SRC and similarly for the other
1522      cases.  Knowing this will help in register status updating below.  */
1523   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1524   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1525   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1526
1527   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
1528      in I2SRC.  */
1529   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1530
1531   /* Ensure that I3's pattern can be the destination of combines.  */
1532   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1533                           i1 && i2dest_in_i1src && i1_feeds_i3,
1534                           &i3dest_killed))
1535     {
1536       undo_all ();
1537       return 0;
1538     }
1539
1540   /* See if any of the insns is a MULT operation.  Unless one is, we will
1541      reject a combination that is, since it must be slower.  Be conservative
1542      here.  */
1543   if (GET_CODE (i2src) == MULT
1544       || (i1 != 0 && GET_CODE (i1src) == MULT)
1545       || (GET_CODE (PATTERN (i3)) == SET
1546           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1547     have_mult = 1;
1548
1549   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1550      We used to do this EXCEPT in one case: I3 has a post-inc in an
1551      output operand.  However, that exception can give rise to insns like
1552         mov r3,(r3)+
1553      which is a famous insn on the PDP-11 where the value of r3 used as the
1554      source was model-dependent.  Avoid this sort of thing.  */
1555
1556 #if 0
1557   if (!(GET_CODE (PATTERN (i3)) == SET
1558         && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1559         && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1560         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1561             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1562     /* It's not the exception.  */
1563 #endif
1564 #ifdef AUTO_INC_DEC
1565     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1566       if (REG_NOTE_KIND (link) == REG_INC
1567           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1568               || (i1 != 0
1569                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1570         {
1571           undo_all ();
1572           return 0;
1573         }
1574 #endif
1575
1576   /* See if the SETs in I1 or I2 need to be kept around in the merged
1577      instruction: whenever the value set there is still needed past I3.
1578      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1579
1580      For the SET in I1, we have two cases:  If I1 and I2 independently
1581      feed into I3, the set in I1 needs to be kept around if I1DEST dies
1582      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
1583      in I1 needs to be kept around unless I1DEST dies or is set in either
1584      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
1585      I1DEST.  If so, we know I1 feeds into I2.  */
1586
1587   added_sets_2 = ! dead_or_set_p (i3, i2dest);
1588
1589   added_sets_1
1590     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1591                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1592
1593   /* If the set in I2 needs to be kept around, we must make a copy of
1594      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1595      PATTERN (I2), we are only substituting for the original I1DEST, not into
1596      an already-substituted copy.  This also prevents making self-referential
1597      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1598      I2DEST.  */
1599
1600   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1601            ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1602            : PATTERN (i2));
1603
1604   if (added_sets_2)
1605     i2pat = copy_rtx (i2pat);
1606
1607   combine_merges++;
1608
1609   /* Substitute in the latest insn for the regs set by the earlier ones.  */
1610
1611   maxreg = max_reg_num ();
1612
1613   subst_insn = i3;
1614
1615   /* It is possible that the source of I2 or I1 may be performing an
1616      unneeded operation, such as a ZERO_EXTEND of something that is known
1617      to have the high part zero.  Handle that case by letting subst look at
1618      the innermost one of them.
1619
1620      Another way to do this would be to have a function that tries to
1621      simplify a single insn instead of merging two or more insns.  We don't
1622      do this because of the potential of infinite loops and because
1623      of the potential extra memory required.  However, doing it the way
1624      we are is a bit of a kludge and doesn't catch all cases.
1625
1626      But only do this if -fexpensive-optimizations since it slows things down
1627      and doesn't usually win.  */
1628
1629   if (flag_expensive_optimizations)
1630     {
1631       /* Pass pc_rtx so no substitutions are done, just simplifications.
1632          The cases that we are interested in here do not involve the few
1633          cases were is_replaced is checked.  */
1634       if (i1)
1635         {
1636           subst_low_cuid = INSN_CUID (i1);
1637           i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1638         }
1639       else
1640         {
1641           subst_low_cuid = INSN_CUID (i2);
1642           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1643         }
1644
1645       undobuf.previous_undos = undobuf.undos;
1646     }
1647
1648 #ifndef HAVE_cc0
1649   /* Many machines that don't use CC0 have insns that can both perform an
1650      arithmetic operation and set the condition code.  These operations will
1651      be represented as a PARALLEL with the first element of the vector
1652      being a COMPARE of an arithmetic operation with the constant zero.
1653      The second element of the vector will set some pseudo to the result
1654      of the same arithmetic operation.  If we simplify the COMPARE, we won't
1655      match such a pattern and so will generate an extra insn.   Here we test
1656      for this case, where both the comparison and the operation result are
1657      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1658      I2SRC.  Later we will make the PARALLEL that contains I2.  */
1659
1660   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1661       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1662       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1663       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1664     {
1665 #ifdef EXTRA_CC_MODES
1666       rtx *cc_use;
1667       enum machine_mode compare_mode;
1668 #endif
1669
1670       newpat = PATTERN (i3);
1671       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1672
1673       i2_is_used = 1;
1674
1675 #ifdef EXTRA_CC_MODES
1676       /* See if a COMPARE with the operand we substituted in should be done
1677          with the mode that is currently being used.  If not, do the same
1678          processing we do in `subst' for a SET; namely, if the destination
1679          is used only once, try to replace it with a register of the proper
1680          mode and also replace the COMPARE.  */
1681       if (undobuf.other_insn == 0
1682           && (cc_use = find_single_use (SET_DEST (newpat), i3,
1683                                         &undobuf.other_insn))
1684           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1685                                               i2src, const0_rtx))
1686               != GET_MODE (SET_DEST (newpat))))
1687         {
1688           int regno = REGNO (SET_DEST (newpat));
1689           rtx new_dest = gen_rtx_REG (compare_mode, regno);
1690
1691           if (regno < FIRST_PSEUDO_REGISTER
1692               || (REG_N_SETS (regno) == 1 && ! added_sets_2
1693                   && ! REG_USERVAR_P (SET_DEST (newpat))))
1694             {
1695               if (regno >= FIRST_PSEUDO_REGISTER)
1696                 SUBST (regno_reg_rtx[regno], new_dest);
1697
1698               SUBST (SET_DEST (newpat), new_dest);
1699               SUBST (XEXP (*cc_use, 0), new_dest);
1700               SUBST (SET_SRC (newpat),
1701                      gen_rtx_combine (COMPARE, compare_mode,
1702                                       i2src, const0_rtx));
1703             }
1704           else
1705             undobuf.other_insn = 0;
1706         }
1707 #endif    
1708     }
1709   else
1710 #endif
1711     {
1712       n_occurrences = 0;                /* `subst' counts here */
1713
1714       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1715          need to make a unique copy of I2SRC each time we substitute it
1716          to avoid self-referential rtl.  */
1717
1718       subst_low_cuid = INSN_CUID (i2);
1719       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1720                       ! i1_feeds_i3 && i1dest_in_i1src);
1721       undobuf.previous_undos = undobuf.undos;
1722
1723       /* Record whether i2's body now appears within i3's body.  */
1724       i2_is_used = n_occurrences;
1725     }
1726
1727   /* If we already got a failure, don't try to do more.  Otherwise,
1728      try to substitute in I1 if we have it.  */
1729
1730   if (i1 && GET_CODE (newpat) != CLOBBER)
1731     {
1732       /* Before we can do this substitution, we must redo the test done
1733          above (see detailed comments there) that ensures  that I1DEST
1734          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
1735
1736       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1737                               0, NULL_PTR))
1738         {
1739           undo_all ();
1740           return 0;
1741         }
1742
1743       n_occurrences = 0;
1744       subst_low_cuid = INSN_CUID (i1);
1745       newpat = subst (newpat, i1dest, i1src, 0, 0);
1746       undobuf.previous_undos = undobuf.undos;
1747     }
1748
1749   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
1750      to count all the ways that I2SRC and I1SRC can be used.  */
1751   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1752        && i2_is_used + added_sets_2 > 1)
1753       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1754           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1755               > 1))
1756       /* Fail if we tried to make a new register (we used to abort, but there's
1757          really no reason to).  */
1758       || max_reg_num () != maxreg
1759       /* Fail if we couldn't do something and have a CLOBBER.  */
1760       || GET_CODE (newpat) == CLOBBER
1761       /* Fail if this new pattern is a MULT and we didn't have one before
1762          at the outer level.  */
1763       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1764           && ! have_mult))
1765     {
1766       undo_all ();
1767       return 0;
1768     }
1769
1770   /* If the actions of the earlier insns must be kept
1771      in addition to substituting them into the latest one,
1772      we must make a new PARALLEL for the latest insn
1773      to hold additional the SETs.  */
1774
1775   if (added_sets_1 || added_sets_2)
1776     {
1777       combine_extras++;
1778
1779       if (GET_CODE (newpat) == PARALLEL)
1780         {
1781           rtvec old = XVEC (newpat, 0);
1782           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1783           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1784           bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
1785                  sizeof (old->elem[0]) * old->num_elem);
1786         }
1787       else
1788         {
1789           rtx old = newpat;
1790           total_sets = 1 + added_sets_1 + added_sets_2;
1791           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1792           XVECEXP (newpat, 0, 0) = old;
1793         }
1794
1795      if (added_sets_1)
1796        XVECEXP (newpat, 0, --total_sets)
1797          = (GET_CODE (PATTERN (i1)) == PARALLEL
1798             ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
1799
1800      if (added_sets_2)
1801         {
1802           /* If there is no I1, use I2's body as is.  We used to also not do
1803              the subst call below if I2 was substituted into I3,
1804              but that could lose a simplification.  */
1805           if (i1 == 0)
1806             XVECEXP (newpat, 0, --total_sets) = i2pat;
1807           else
1808             /* See comment where i2pat is assigned.  */
1809             XVECEXP (newpat, 0, --total_sets)
1810               = subst (i2pat, i1dest, i1src, 0, 0);
1811         }
1812     }
1813
1814   /* We come here when we are replacing a destination in I2 with the
1815      destination of I3.  */
1816  validate_replacement:
1817
1818   /* Note which hard regs this insn has as inputs.  */
1819   mark_used_regs_combine (newpat);
1820
1821   /* Is the result of combination a valid instruction?  */
1822   insn_code_number
1823     = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1824
1825   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1826      the second SET's destination is a register that is unused.  In that case,
1827      we just need the first SET.   This can occur when simplifying a divmod
1828      insn.  We *must* test for this case here because the code below that
1829      splits two independent SETs doesn't handle this case correctly when it
1830      updates the register status.  Also check the case where the first
1831      SET's destination is unused.  That would not cause incorrect code, but
1832      does cause an unneeded insn to remain.  */
1833
1834   if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1835       && XVECLEN (newpat, 0) == 2
1836       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1837       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1838       && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1839       && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1840       && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1841       && asm_noperands (newpat) < 0)
1842     {
1843       newpat = XVECEXP (newpat, 0, 0);
1844       insn_code_number
1845         = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1846     }
1847
1848   else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1849            && XVECLEN (newpat, 0) == 2
1850            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1851            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1852            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1853            && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1854            && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1855            && asm_noperands (newpat) < 0)
1856     {
1857       newpat = XVECEXP (newpat, 0, 1);
1858       insn_code_number
1859         = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1860     }
1861
1862   /* If we were combining three insns and the result is a simple SET
1863      with no ASM_OPERANDS that wasn't recognized, try to split it into two
1864      insns.  There are two ways to do this.  It can be split using a 
1865      machine-specific method (like when you have an addition of a large
1866      constant) or by combine in the function find_split_point.  */
1867
1868   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1869       && asm_noperands (newpat) < 0)
1870     {
1871       rtx m_split, *split;
1872       rtx ni2dest = i2dest;
1873
1874       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
1875          use I2DEST as a scratch register will help.  In the latter case,
1876          convert I2DEST to the mode of the source of NEWPAT if we can.  */
1877
1878       m_split = split_insns (newpat, i3);
1879
1880       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1881          inputs of NEWPAT.  */
1882
1883       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1884          possible to try that as a scratch reg.  This would require adding
1885          more code to make it work though.  */
1886
1887       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
1888         {
1889           /* If I2DEST is a hard register or the only use of a pseudo,
1890              we can change its mode.  */
1891           if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
1892               && GET_MODE (SET_DEST (newpat)) != VOIDmode
1893               && GET_CODE (i2dest) == REG
1894               && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1895                   || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
1896                       && ! REG_USERVAR_P (i2dest))))
1897             ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
1898                                REGNO (i2dest));
1899
1900           m_split = split_insns
1901             (gen_rtx_PARALLEL (VOIDmode,
1902                                gen_rtvec (2, newpat,
1903                                           gen_rtx_CLOBBER (VOIDmode,
1904                                                            ni2dest))),
1905              i3);
1906         }
1907
1908       if (m_split && GET_CODE (m_split) == SEQUENCE
1909           && XVECLEN (m_split, 0) == 2
1910           && (next_real_insn (i2) == i3
1911               || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1912                                       INSN_CUID (i2))))
1913         {
1914           rtx i2set, i3set;
1915           rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
1916           newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
1917
1918           i3set = single_set (XVECEXP (m_split, 0, 1));
1919           i2set = single_set (XVECEXP (m_split, 0, 0));
1920
1921           /* In case we changed the mode of I2DEST, replace it in the
1922              pseudo-register table here.  We can't do it above in case this
1923              code doesn't get executed and we do a split the other way.  */
1924
1925           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1926             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1927
1928           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes,
1929                                               &i2_scratches);
1930
1931           /* If I2 or I3 has multiple SETs, we won't know how to track
1932              register status, so don't use these insns.  If I2's destination
1933              is used between I2 and I3, we also can't use these insns.  */
1934
1935           if (i2_code_number >= 0 && i2set && i3set
1936               && (next_real_insn (i2) == i3
1937                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
1938             insn_code_number = recog_for_combine (&newi3pat, i3, &new_i3_notes,
1939                                                   &i3_scratches); 
1940           if (insn_code_number >= 0)
1941             newpat = newi3pat;
1942
1943           /* It is possible that both insns now set the destination of I3.
1944              If so, we must show an extra use of it.  */
1945
1946           if (insn_code_number >= 0)
1947             {
1948               rtx new_i3_dest = SET_DEST (i3set);
1949               rtx new_i2_dest = SET_DEST (i2set);
1950
1951               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
1952                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
1953                      || GET_CODE (new_i3_dest) == SUBREG)
1954                 new_i3_dest = XEXP (new_i3_dest, 0);
1955
1956               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
1957                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
1958                      || GET_CODE (new_i2_dest) == SUBREG)
1959                 new_i2_dest = XEXP (new_i2_dest, 0);
1960
1961               if (GET_CODE (new_i3_dest) == REG
1962                   && GET_CODE (new_i2_dest) == REG
1963                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
1964                 REG_N_SETS (REGNO (new_i2_dest))++;
1965             }
1966         }
1967
1968       /* If we can split it and use I2DEST, go ahead and see if that
1969          helps things be recognized.  Verify that none of the registers
1970          are set between I2 and I3.  */
1971       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
1972 #ifdef HAVE_cc0
1973           && GET_CODE (i2dest) == REG
1974 #endif
1975           /* We need I2DEST in the proper mode.  If it is a hard register
1976              or the only use of a pseudo, we can change its mode.  */
1977           && (GET_MODE (*split) == GET_MODE (i2dest)
1978               || GET_MODE (*split) == VOIDmode
1979               || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1980               || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
1981                   && ! REG_USERVAR_P (i2dest)))
1982           && (next_real_insn (i2) == i3
1983               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
1984           /* We can't overwrite I2DEST if its value is still used by
1985              NEWPAT.  */
1986           && ! reg_referenced_p (i2dest, newpat))
1987         {
1988           rtx newdest = i2dest;
1989           enum rtx_code split_code = GET_CODE (*split);
1990           enum machine_mode split_mode = GET_MODE (*split);
1991
1992           /* Get NEWDEST as a register in the proper mode.  We have already
1993              validated that we can do this.  */
1994           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
1995             {
1996               newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
1997
1998               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1999                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2000             }
2001
2002           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2003              an ASHIFT.  This can occur if it was inside a PLUS and hence
2004              appeared to be a memory address.  This is a kludge.  */
2005           if (split_code == MULT
2006               && GET_CODE (XEXP (*split, 1)) == CONST_INT
2007               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2008             {
2009               SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
2010                                               XEXP (*split, 0), GEN_INT (i)));
2011               /* Update split_code because we may not have a multiply
2012                  anymore.  */
2013               split_code = GET_CODE (*split);
2014             }
2015
2016 #ifdef INSN_SCHEDULING
2017           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2018              be written as a ZERO_EXTEND.  */
2019           if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2020             SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
2021                                             XEXP (*split, 0)));
2022 #endif
2023
2024           newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
2025           SUBST (*split, newdest);
2026           i2_code_number
2027             = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2028
2029           /* If the split point was a MULT and we didn't have one before,
2030              don't use one now.  */
2031           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2032             insn_code_number
2033               = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
2034         }
2035     }
2036
2037   /* Check for a case where we loaded from memory in a narrow mode and
2038      then sign extended it, but we need both registers.  In that case,
2039      we have a PARALLEL with both loads from the same memory location.
2040      We can split this into a load from memory followed by a register-register
2041      copy.  This saves at least one insn, more if register allocation can
2042      eliminate the copy.
2043
2044      We cannot do this if the destination of the second assignment is
2045      a register that we have already assumed is zero-extended.  Similarly
2046      for a SUBREG of such a register.  */
2047
2048   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2049            && GET_CODE (newpat) == PARALLEL
2050            && XVECLEN (newpat, 0) == 2
2051            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2052            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2053            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2054            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2055                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2056            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2057                                    INSN_CUID (i2))
2058            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2059            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2060            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2061                  (GET_CODE (temp) == REG
2062                   && reg_nonzero_bits[REGNO (temp)] != 0
2063                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2064                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2065                   && (reg_nonzero_bits[REGNO (temp)]
2066                       != GET_MODE_MASK (word_mode))))
2067            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2068                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2069                      (GET_CODE (temp) == REG
2070                       && reg_nonzero_bits[REGNO (temp)] != 0
2071                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2072                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2073                       && (reg_nonzero_bits[REGNO (temp)]
2074                           != GET_MODE_MASK (word_mode)))))
2075            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2076                                          SET_SRC (XVECEXP (newpat, 0, 1)))
2077            && ! find_reg_note (i3, REG_UNUSED,
2078                                SET_DEST (XVECEXP (newpat, 0, 0))))
2079     {
2080       rtx ni2dest;
2081
2082       newi2pat = XVECEXP (newpat, 0, 0);
2083       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2084       newpat = XVECEXP (newpat, 0, 1);
2085       SUBST (SET_SRC (newpat),
2086              gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2087       i2_code_number
2088         = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2089
2090       if (i2_code_number >= 0)
2091         insn_code_number
2092           = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
2093
2094       if (insn_code_number >= 0)
2095         {
2096           rtx insn;
2097           rtx link;
2098
2099           /* If we will be able to accept this, we have made a change to the
2100              destination of I3.  This can invalidate a LOG_LINKS pointing
2101              to I3.  No other part of combine.c makes such a transformation.
2102
2103              The new I3 will have a destination that was previously the
2104              destination of I1 or I2 and which was used in i2 or I3.  Call
2105              distribute_links to make a LOG_LINK from the next use of
2106              that destination.  */
2107
2108           PATTERN (i3) = newpat;
2109           distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
2110
2111           /* I3 now uses what used to be its destination and which is
2112              now I2's destination.  That means we need a LOG_LINK from
2113              I3 to I2.  But we used to have one, so we still will.
2114
2115              However, some later insn might be using I2's dest and have
2116              a LOG_LINK pointing at I3.  We must remove this link.
2117              The simplest way to remove the link is to point it at I1,
2118              which we know will be a NOTE.  */
2119
2120           for (insn = NEXT_INSN (i3);
2121                insn && (this_basic_block == n_basic_blocks - 1
2122                         || insn != basic_block_head[this_basic_block + 1]);
2123                insn = NEXT_INSN (insn))
2124             {
2125               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
2126                   && reg_referenced_p (ni2dest, PATTERN (insn)))
2127                 {
2128                   for (link = LOG_LINKS (insn); link;
2129                        link = XEXP (link, 1))
2130                     if (XEXP (link, 0) == i3)
2131                       XEXP (link, 0) = i1;
2132
2133                   break;
2134                 }
2135             }
2136         }
2137     }
2138             
2139   /* Similarly, check for a case where we have a PARALLEL of two independent
2140      SETs but we started with three insns.  In this case, we can do the sets
2141      as two separate insns.  This case occurs when some SET allows two
2142      other insns to combine, but the destination of that SET is still live.  */
2143
2144   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2145            && GET_CODE (newpat) == PARALLEL
2146            && XVECLEN (newpat, 0) == 2
2147            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2148            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2149            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2150            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2151            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2152            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2153            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2154                                    INSN_CUID (i2))
2155            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
2156            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2157            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2158            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2159                                   XVECEXP (newpat, 0, 0))
2160            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2161                                   XVECEXP (newpat, 0, 1)))
2162     {
2163       /* Normally, it doesn't matter which of the two is done first,
2164          but it does if one references cc0.  In that case, it has to
2165          be first.  */
2166 #ifdef HAVE_cc0
2167       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2168         {
2169           newi2pat = XVECEXP (newpat, 0, 0);
2170           newpat = XVECEXP (newpat, 0, 1);
2171         }
2172       else
2173 #endif
2174         {
2175           newi2pat = XVECEXP (newpat, 0, 1);
2176           newpat = XVECEXP (newpat, 0, 0);
2177         }
2178
2179       i2_code_number
2180         = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2181
2182       if (i2_code_number >= 0)
2183         insn_code_number
2184           = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
2185     }
2186
2187   /* If it still isn't recognized, fail and change things back the way they
2188      were.  */
2189   if ((insn_code_number < 0
2190        /* Is the result a reasonable ASM_OPERANDS?  */
2191        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2192     {
2193       undo_all ();
2194       return 0;
2195     }
2196
2197   /* If we had to change another insn, make sure it is valid also.  */
2198   if (undobuf.other_insn)
2199     {
2200       rtx other_pat = PATTERN (undobuf.other_insn);
2201       rtx new_other_notes;
2202       rtx note, next;
2203
2204       CLEAR_HARD_REG_SET (newpat_used_regs);
2205
2206       other_code_number
2207         = recog_for_combine (&other_pat, undobuf.other_insn,
2208                              &new_other_notes, &other_scratches);
2209
2210       if (other_code_number < 0 && ! check_asm_operands (other_pat))
2211         {
2212           undo_all ();
2213           return 0;
2214         }
2215
2216       PATTERN (undobuf.other_insn) = other_pat;
2217
2218       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2219          are still valid.  Then add any non-duplicate notes added by
2220          recog_for_combine.  */
2221       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2222         {
2223           next = XEXP (note, 1);
2224
2225           if (REG_NOTE_KIND (note) == REG_UNUSED
2226               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2227             {
2228               if (GET_CODE (XEXP (note, 0)) == REG)
2229                 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2230
2231               remove_note (undobuf.other_insn, note);
2232             }
2233         }
2234
2235       for (note = new_other_notes; note; note = XEXP (note, 1))
2236         if (GET_CODE (XEXP (note, 0)) == REG)
2237           REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2238
2239       distribute_notes (new_other_notes, undobuf.other_insn,
2240                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2241     }
2242
2243   /* We now know that we can do this combination.  Merge the insns and 
2244      update the status of registers and LOG_LINKS.  */
2245
2246   {
2247     rtx i3notes, i2notes, i1notes = 0;
2248     rtx i3links, i2links, i1links = 0;
2249     rtx midnotes = 0;
2250     register int regno;
2251     /* Compute which registers we expect to eliminate.  newi2pat may be setting
2252        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
2253        same as i3dest, in which case newi2pat may be setting i1dest.  */
2254     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2255                    || i2dest_in_i2src || i2dest_in_i1src
2256                    ? 0 : i2dest);
2257     rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2258                    || (newi2pat && reg_set_p (i1dest, newi2pat))
2259                    ? 0 : i1dest);
2260
2261     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2262        clear them.  */
2263     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2264     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2265     if (i1)
2266       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2267
2268     /* Ensure that we do not have something that should not be shared but
2269        occurs multiple times in the new insns.  Check this by first
2270        resetting all the `used' flags and then copying anything is shared.  */
2271
2272     reset_used_flags (i3notes);
2273     reset_used_flags (i2notes);
2274     reset_used_flags (i1notes);
2275     reset_used_flags (newpat);
2276     reset_used_flags (newi2pat);
2277     if (undobuf.other_insn)
2278       reset_used_flags (PATTERN (undobuf.other_insn));
2279
2280     i3notes = copy_rtx_if_shared (i3notes);
2281     i2notes = copy_rtx_if_shared (i2notes);
2282     i1notes = copy_rtx_if_shared (i1notes);
2283     newpat = copy_rtx_if_shared (newpat);
2284     newi2pat = copy_rtx_if_shared (newi2pat);
2285     if (undobuf.other_insn)
2286       reset_used_flags (PATTERN (undobuf.other_insn));
2287
2288     INSN_CODE (i3) = insn_code_number;
2289     PATTERN (i3) = newpat;
2290     if (undobuf.other_insn)
2291       INSN_CODE (undobuf.other_insn) = other_code_number;
2292
2293     /* We had one special case above where I2 had more than one set and
2294        we replaced a destination of one of those sets with the destination
2295        of I3.  In that case, we have to update LOG_LINKS of insns later
2296        in this basic block.  Note that this (expensive) case is rare.
2297
2298        Also, in this case, we must pretend that all REG_NOTEs for I2
2299        actually came from I3, so that REG_UNUSED notes from I2 will be
2300        properly handled.  */
2301
2302     if (i3_subst_into_i2)
2303       {
2304         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2305           if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2306               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2307               && ! find_reg_note (i2, REG_UNUSED,
2308                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2309             for (temp = NEXT_INSN (i2);
2310                  temp && (this_basic_block == n_basic_blocks - 1
2311                           || basic_block_head[this_basic_block] != temp);
2312                  temp = NEXT_INSN (temp))
2313               if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2314                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2315                   if (XEXP (link, 0) == i2)
2316                     XEXP (link, 0) = i3;
2317
2318         if (i3notes)
2319           {
2320             rtx link = i3notes;
2321             while (XEXP (link, 1))
2322               link = XEXP (link, 1);
2323             XEXP (link, 1) = i2notes;
2324           }
2325         else
2326           i3notes = i2notes;
2327         i2notes = 0;
2328       }
2329
2330     LOG_LINKS (i3) = 0;
2331     REG_NOTES (i3) = 0;
2332     LOG_LINKS (i2) = 0;
2333     REG_NOTES (i2) = 0;
2334
2335     if (newi2pat)
2336       {
2337         INSN_CODE (i2) = i2_code_number;
2338         PATTERN (i2) = newi2pat;
2339       }
2340     else
2341       {
2342         PUT_CODE (i2, NOTE);
2343         NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2344         NOTE_SOURCE_FILE (i2) = 0;
2345       }
2346
2347     if (i1)
2348       {
2349         LOG_LINKS (i1) = 0;
2350         REG_NOTES (i1) = 0;
2351         PUT_CODE (i1, NOTE);
2352         NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2353         NOTE_SOURCE_FILE (i1) = 0;
2354       }
2355
2356     /* Get death notes for everything that is now used in either I3 or
2357        I2 and used to die in a previous insn.  If we built two new 
2358        patterns, move from I1 to I2 then I2 to I3 so that we get the
2359        proper movement on registers that I2 modifies.  */
2360
2361     if (newi2pat)
2362       {
2363         move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2364         move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2365       }
2366     else
2367       move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2368                    i3, &midnotes);
2369
2370     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
2371     if (i3notes)
2372       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2373                         elim_i2, elim_i1);
2374     if (i2notes)
2375       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2376                         elim_i2, elim_i1);
2377     if (i1notes)
2378       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2379                         elim_i2, elim_i1);
2380     if (midnotes)
2381       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2382                         elim_i2, elim_i1);
2383
2384     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
2385        know these are REG_UNUSED and want them to go to the desired insn,
2386        so we always pass it as i3.  We have not counted the notes in 
2387        reg_n_deaths yet, so we need to do so now.  */
2388
2389     if (newi2pat && new_i2_notes)
2390       {
2391         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2392           if (GET_CODE (XEXP (temp, 0)) == REG)
2393             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2394         
2395         distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2396       }
2397
2398     if (new_i3_notes)
2399       {
2400         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2401           if (GET_CODE (XEXP (temp, 0)) == REG)
2402             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2403         
2404         distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2405       }
2406
2407     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
2408        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
2409        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
2410        in that case, it might delete I2.  Similarly for I2 and I1.
2411        Show an additional death due to the REG_DEAD note we make here.  If
2412        we discard it in distribute_notes, we will decrement it again.  */
2413
2414     if (i3dest_killed)
2415       {
2416         if (GET_CODE (i3dest_killed) == REG)
2417           REG_N_DEATHS (REGNO (i3dest_killed))++;
2418
2419         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2420           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2421                                                NULL_RTX),
2422                             NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
2423         else
2424           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2425                                                NULL_RTX),
2426                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2427                             elim_i2, elim_i1);
2428       }
2429
2430     if (i2dest_in_i2src)
2431       {
2432         if (GET_CODE (i2dest) == REG)
2433           REG_N_DEATHS (REGNO (i2dest))++;
2434
2435         if (newi2pat && reg_set_p (i2dest, newi2pat))
2436           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2437                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2438         else
2439           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2440                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2441                             NULL_RTX, NULL_RTX);
2442       }
2443
2444     if (i1dest_in_i1src)
2445       {
2446         if (GET_CODE (i1dest) == REG)
2447           REG_N_DEATHS (REGNO (i1dest))++;
2448
2449         if (newi2pat && reg_set_p (i1dest, newi2pat))
2450           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2451                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2452         else
2453           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2454                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2455                             NULL_RTX, NULL_RTX);
2456       }
2457
2458     distribute_links (i3links);
2459     distribute_links (i2links);
2460     distribute_links (i1links);
2461
2462     if (GET_CODE (i2dest) == REG)
2463       {
2464         rtx link;
2465         rtx i2_insn = 0, i2_val = 0, set;
2466
2467         /* The insn that used to set this register doesn't exist, and
2468            this life of the register may not exist either.  See if one of
2469            I3's links points to an insn that sets I2DEST.  If it does, 
2470            that is now the last known value for I2DEST. If we don't update
2471            this and I2 set the register to a value that depended on its old
2472            contents, we will get confused.  If this insn is used, thing
2473            will be set correctly in combine_instructions.  */
2474
2475         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2476           if ((set = single_set (XEXP (link, 0))) != 0
2477               && rtx_equal_p (i2dest, SET_DEST (set)))
2478             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2479
2480         record_value_for_reg (i2dest, i2_insn, i2_val);
2481
2482         /* If the reg formerly set in I2 died only once and that was in I3,
2483            zero its use count so it won't make `reload' do any work.  */
2484         if (! added_sets_2
2485             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2486             && ! i2dest_in_i2src)
2487           {
2488             regno = REGNO (i2dest);
2489             REG_N_SETS (regno)--;
2490             if (REG_N_SETS (regno) == 0
2491                 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
2492               REG_N_REFS (regno) = 0;
2493           }
2494       }
2495
2496     if (i1 && GET_CODE (i1dest) == REG)
2497       {
2498         rtx link;
2499         rtx i1_insn = 0, i1_val = 0, set;
2500
2501         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2502           if ((set = single_set (XEXP (link, 0))) != 0
2503               && rtx_equal_p (i1dest, SET_DEST (set)))
2504             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2505
2506         record_value_for_reg (i1dest, i1_insn, i1_val);
2507
2508         regno = REGNO (i1dest);
2509         if (! added_sets_1 && ! i1dest_in_i1src)
2510           {
2511             REG_N_SETS (regno)--;
2512             if (REG_N_SETS (regno) == 0
2513                 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
2514               REG_N_REFS (regno) = 0;
2515           }
2516       }
2517
2518     /* Update reg_nonzero_bits et al for any changes that may have been made
2519        to this insn.  */
2520
2521     note_stores (newpat, set_nonzero_bits_and_sign_copies);
2522     if (newi2pat)
2523       note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
2524
2525     /* If we added any (clobber (scratch)), add them to the max for a
2526        block.  This is a very pessimistic calculation, since we might
2527        have had them already and this might not be the worst block, but
2528        it's not worth doing any better.  */
2529     max_scratch += i3_scratches + i2_scratches + other_scratches;
2530
2531     /* If I3 is now an unconditional jump, ensure that it has a 
2532        BARRIER following it since it may have initially been a
2533        conditional jump.  It may also be the last nonnote insn.  */
2534
2535     if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
2536         && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2537             || GET_CODE (temp) != BARRIER))
2538       emit_barrier_after (i3);
2539   }
2540
2541   combine_successes++;
2542
2543   /* Clear this here, so that subsequent get_last_value calls are not
2544      affected.  */
2545   subst_prev_insn = NULL_RTX;
2546
2547   if (added_links_insn
2548       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2549       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2550     return added_links_insn;
2551   else
2552     return newi2pat ? i2 : i3;
2553 }
2554 \f
2555 /* Undo all the modifications recorded in undobuf.  */
2556
2557 static void
2558 undo_all ()
2559 {
2560   struct undo *undo, *next;
2561
2562   for (undo = undobuf.undos; undo; undo = next)
2563     {
2564       next = undo->next;
2565       if (undo->is_int)
2566         *undo->where.i = undo->old_contents.i;
2567       else
2568         *undo->where.r = undo->old_contents.r;
2569
2570       undo->next = undobuf.frees;
2571       undobuf.frees = undo;
2572     }
2573
2574   obfree (undobuf.storage);
2575   undobuf.undos = undobuf.previous_undos = 0;
2576
2577   /* Clear this here, so that subsequent get_last_value calls are not
2578      affected.  */
2579   subst_prev_insn = NULL_RTX;
2580 }
2581 \f
2582 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2583    where we have an arithmetic expression and return that point.  LOC will
2584    be inside INSN.
2585
2586    try_combine will call this function to see if an insn can be split into
2587    two insns.  */
2588
2589 static rtx *
2590 find_split_point (loc, insn)
2591      rtx *loc;
2592      rtx insn;
2593 {
2594   rtx x = *loc;
2595   enum rtx_code code = GET_CODE (x);
2596   rtx *split;
2597   int len = 0, pos, unsignedp;
2598   rtx inner;
2599
2600   /* First special-case some codes.  */
2601   switch (code)
2602     {
2603     case SUBREG:
2604 #ifdef INSN_SCHEDULING
2605       /* If we are making a paradoxical SUBREG invalid, it becomes a split
2606          point.  */
2607       if (GET_CODE (SUBREG_REG (x)) == MEM)
2608         return loc;
2609 #endif
2610       return find_split_point (&SUBREG_REG (x), insn);
2611
2612     case MEM:
2613 #ifdef HAVE_lo_sum
2614       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2615          using LO_SUM and HIGH.  */
2616       if (GET_CODE (XEXP (x, 0)) == CONST
2617           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2618         {
2619           SUBST (XEXP (x, 0),
2620                  gen_rtx_combine (LO_SUM, Pmode,
2621                                   gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2622                                   XEXP (x, 0)));
2623           return &XEXP (XEXP (x, 0), 0);
2624         }
2625 #endif
2626
2627       /* If we have a PLUS whose second operand is a constant and the
2628          address is not valid, perhaps will can split it up using
2629          the machine-specific way to split large constants.  We use
2630          the first pseudo-reg (one of the virtual regs) as a placeholder;
2631          it will not remain in the result.  */
2632       if (GET_CODE (XEXP (x, 0)) == PLUS
2633           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2634           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2635         {
2636           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2637           rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2638                                  subst_insn);
2639
2640           /* This should have produced two insns, each of which sets our
2641              placeholder.  If the source of the second is a valid address,
2642              we can make put both sources together and make a split point
2643              in the middle.  */
2644
2645           if (seq && XVECLEN (seq, 0) == 2
2646               && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2647               && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2648               && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2649               && ! reg_mentioned_p (reg,
2650                                     SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2651               && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2652               && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2653               && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2654               && memory_address_p (GET_MODE (x),
2655                                    SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2656             {
2657               rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2658               rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2659
2660               /* Replace the placeholder in SRC2 with SRC1.  If we can
2661                  find where in SRC2 it was placed, that can become our
2662                  split point and we can replace this address with SRC2.
2663                  Just try two obvious places.  */
2664
2665               src2 = replace_rtx (src2, reg, src1);
2666               split = 0;
2667               if (XEXP (src2, 0) == src1)
2668                 split = &XEXP (src2, 0);
2669               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2670                        && XEXP (XEXP (src2, 0), 0) == src1)
2671                 split = &XEXP (XEXP (src2, 0), 0);
2672
2673               if (split)
2674                 {
2675                   SUBST (XEXP (x, 0), src2);
2676                   return split;
2677                 }
2678             }
2679           
2680           /* If that didn't work, perhaps the first operand is complex and
2681              needs to be computed separately, so make a split point there.
2682              This will occur on machines that just support REG + CONST
2683              and have a constant moved through some previous computation.  */
2684
2685           else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2686                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2687                          && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2688                              == 'o')))
2689             return &XEXP (XEXP (x, 0), 0);
2690         }
2691       break;
2692
2693     case SET:
2694 #ifdef HAVE_cc0
2695       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2696          ZERO_EXTRACT, the most likely reason why this doesn't match is that
2697          we need to put the operand into a register.  So split at that
2698          point.  */
2699
2700       if (SET_DEST (x) == cc0_rtx
2701           && GET_CODE (SET_SRC (x)) != COMPARE
2702           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2703           && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2704           && ! (GET_CODE (SET_SRC (x)) == SUBREG
2705                 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2706         return &SET_SRC (x);
2707 #endif
2708
2709       /* See if we can split SET_SRC as it stands.  */
2710       split = find_split_point (&SET_SRC (x), insn);
2711       if (split && split != &SET_SRC (x))
2712         return split;
2713
2714       /* See if we can split SET_DEST as it stands.  */
2715       split = find_split_point (&SET_DEST (x), insn);
2716       if (split && split != &SET_DEST (x))
2717         return split;
2718
2719       /* See if this is a bitfield assignment with everything constant.  If
2720          so, this is an IOR of an AND, so split it into that.  */
2721       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2722           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2723               <= HOST_BITS_PER_WIDE_INT)
2724           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2725           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2726           && GET_CODE (SET_SRC (x)) == CONST_INT
2727           && ((INTVAL (XEXP (SET_DEST (x), 1))
2728               + INTVAL (XEXP (SET_DEST (x), 2)))
2729               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2730           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2731         {
2732           int pos = INTVAL (XEXP (SET_DEST (x), 2));
2733           int len = INTVAL (XEXP (SET_DEST (x), 1));
2734           int src = INTVAL (SET_SRC (x));
2735           rtx dest = XEXP (SET_DEST (x), 0);
2736           enum machine_mode mode = GET_MODE (dest);
2737           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2738
2739           if (BITS_BIG_ENDIAN)
2740             pos = GET_MODE_BITSIZE (mode) - len - pos;
2741
2742           if (src == mask)
2743             SUBST (SET_SRC (x),
2744                    gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2745           else
2746             SUBST (SET_SRC (x),
2747                    gen_binary (IOR, mode,
2748                                gen_binary (AND, mode, dest, 
2749                                            GEN_INT (~ (mask << pos)
2750                                                     & GET_MODE_MASK (mode))),
2751                                GEN_INT (src << pos)));
2752
2753           SUBST (SET_DEST (x), dest);
2754
2755           split = find_split_point (&SET_SRC (x), insn);
2756           if (split && split != &SET_SRC (x))
2757             return split;
2758         }
2759
2760       /* Otherwise, see if this is an operation that we can split into two.
2761          If so, try to split that.  */
2762       code = GET_CODE (SET_SRC (x));
2763
2764       switch (code)
2765         {
2766         case AND:
2767           /* If we are AND'ing with a large constant that is only a single
2768              bit and the result is only being used in a context where we
2769              need to know if it is zero or non-zero, replace it with a bit
2770              extraction.  This will avoid the large constant, which might
2771              have taken more than one insn to make.  If the constant were
2772              not a valid argument to the AND but took only one insn to make,
2773              this is no worse, but if it took more than one insn, it will
2774              be better.  */
2775
2776           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2777               && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2778               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2779               && GET_CODE (SET_DEST (x)) == REG
2780               && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2781               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2782               && XEXP (*split, 0) == SET_DEST (x)
2783               && XEXP (*split, 1) == const0_rtx)
2784             {
2785               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2786                                                 XEXP (SET_SRC (x), 0),
2787                                                 pos, NULL_RTX, 1, 1, 0, 0);
2788               if (extraction != 0)
2789                 {
2790                   SUBST (SET_SRC (x), extraction);
2791                   return find_split_point (loc, insn);
2792                 }
2793             }
2794           break;
2795
2796         case NE:
2797           /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
2798              is known to be on, this can be converted into a NEG of a shift. */
2799           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
2800               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
2801               && 1 <= (pos = exact_log2
2802                        (nonzero_bits (XEXP (SET_SRC (x), 0),
2803                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
2804             {
2805               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
2806
2807               SUBST (SET_SRC (x),
2808                      gen_rtx_combine (NEG, mode,
2809                                       gen_rtx_combine (LSHIFTRT, mode,
2810                                                        XEXP (SET_SRC (x), 0),
2811                                                        GEN_INT (pos))));
2812
2813               split = find_split_point (&SET_SRC (x), insn);
2814               if (split && split != &SET_SRC (x))
2815                 return split;
2816             }
2817           break;
2818
2819         case SIGN_EXTEND:
2820           inner = XEXP (SET_SRC (x), 0);
2821
2822           /* We can't optimize if either mode is a partial integer
2823              mode as we don't know how many bits are significant
2824              in those modes.  */
2825           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
2826               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
2827             break;
2828
2829           pos = 0;
2830           len = GET_MODE_BITSIZE (GET_MODE (inner));
2831           unsignedp = 0;
2832           break;
2833
2834         case SIGN_EXTRACT:
2835         case ZERO_EXTRACT:
2836           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2837               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2838             {
2839               inner = XEXP (SET_SRC (x), 0);
2840               len = INTVAL (XEXP (SET_SRC (x), 1));
2841               pos = INTVAL (XEXP (SET_SRC (x), 2));
2842
2843               if (BITS_BIG_ENDIAN)
2844                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
2845               unsignedp = (code == ZERO_EXTRACT);
2846             }
2847           break;
2848
2849         default:
2850           break;
2851         }
2852
2853       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2854         {
2855           enum machine_mode mode = GET_MODE (SET_SRC (x));
2856
2857           /* For unsigned, we have a choice of a shift followed by an
2858              AND or two shifts.  Use two shifts for field sizes where the
2859              constant might be too large.  We assume here that we can
2860              always at least get 8-bit constants in an AND insn, which is
2861              true for every current RISC.  */
2862
2863           if (unsignedp && len <= 8)
2864             {
2865               SUBST (SET_SRC (x),
2866                      gen_rtx_combine
2867                      (AND, mode,
2868                       gen_rtx_combine (LSHIFTRT, mode,
2869                                        gen_lowpart_for_combine (mode, inner),
2870                                        GEN_INT (pos)),
2871                       GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
2872
2873               split = find_split_point (&SET_SRC (x), insn);
2874               if (split && split != &SET_SRC (x))
2875                 return split;
2876             }
2877           else
2878             {
2879               SUBST (SET_SRC (x),
2880                      gen_rtx_combine
2881                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
2882                       gen_rtx_combine (ASHIFT, mode,
2883                                        gen_lowpart_for_combine (mode, inner),
2884                                        GEN_INT (GET_MODE_BITSIZE (mode)
2885                                                 - len - pos)),
2886                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
2887
2888               split = find_split_point (&SET_SRC (x), insn);
2889               if (split && split != &SET_SRC (x))
2890                 return split;
2891             }
2892         }
2893
2894       /* See if this is a simple operation with a constant as the second
2895          operand.  It might be that this constant is out of range and hence
2896          could be used as a split point.  */
2897       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2898            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2899            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2900           && CONSTANT_P (XEXP (SET_SRC (x), 1))
2901           && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2902               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2903                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2904                       == 'o'))))
2905         return &XEXP (SET_SRC (x), 1);
2906
2907       /* Finally, see if this is a simple operation with its first operand
2908          not in a register.  The operation might require this operand in a
2909          register, so return it as a split point.  We can always do this
2910          because if the first operand were another operation, we would have
2911          already found it as a split point.  */
2912       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2913            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2914            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2915            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2916           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2917         return &XEXP (SET_SRC (x), 0);
2918
2919       return 0;
2920
2921     case AND:
2922     case IOR:
2923       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2924          it is better to write this as (not (ior A B)) so we can split it.
2925          Similarly for IOR.  */
2926       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2927         {
2928           SUBST (*loc,
2929                  gen_rtx_combine (NOT, GET_MODE (x),
2930                                   gen_rtx_combine (code == IOR ? AND : IOR,
2931                                                    GET_MODE (x),
2932                                                    XEXP (XEXP (x, 0), 0),
2933                                                    XEXP (XEXP (x, 1), 0))));
2934           return find_split_point (loc, insn);
2935         }
2936
2937       /* Many RISC machines have a large set of logical insns.  If the
2938          second operand is a NOT, put it first so we will try to split the
2939          other operand first.  */
2940       if (GET_CODE (XEXP (x, 1)) == NOT)
2941         {
2942           rtx tem = XEXP (x, 0);
2943           SUBST (XEXP (x, 0), XEXP (x, 1));
2944           SUBST (XEXP (x, 1), tem);
2945         }
2946       break;
2947
2948     default:
2949       break;
2950     }
2951
2952   /* Otherwise, select our actions depending on our rtx class.  */
2953   switch (GET_RTX_CLASS (code))
2954     {
2955     case 'b':                   /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
2956     case '3':
2957       split = find_split_point (&XEXP (x, 2), insn);
2958       if (split)
2959         return split;
2960       /* ... fall through ...  */
2961     case '2':
2962     case 'c':
2963     case '<':
2964       split = find_split_point (&XEXP (x, 1), insn);
2965       if (split)
2966         return split;
2967       /* ... fall through ...  */
2968     case '1':
2969       /* Some machines have (and (shift ...) ...) insns.  If X is not
2970          an AND, but XEXP (X, 0) is, use it as our split point.  */
2971       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
2972         return &XEXP (x, 0);
2973
2974       split = find_split_point (&XEXP (x, 0), insn);
2975       if (split)
2976         return split;
2977       return loc;
2978     }
2979
2980   /* Otherwise, we don't have a split point.  */
2981   return 0;
2982 }
2983 \f
2984 /* Throughout X, replace FROM with TO, and return the result.
2985    The result is TO if X is FROM;
2986    otherwise the result is X, but its contents may have been modified.
2987    If they were modified, a record was made in undobuf so that
2988    undo_all will (among other things) return X to its original state.
2989
2990    If the number of changes necessary is too much to record to undo,
2991    the excess changes are not made, so the result is invalid.
2992    The changes already made can still be undone.
2993    undobuf.num_undo is incremented for such changes, so by testing that
2994    the caller can tell whether the result is valid.
2995
2996    `n_occurrences' is incremented each time FROM is replaced.
2997    
2998    IN_DEST is non-zero if we are processing the SET_DEST of a SET.
2999
3000    UNIQUE_COPY is non-zero if each substitution must be unique.  We do this
3001    by copying if `n_occurrences' is non-zero.  */
3002
3003 static rtx
3004 subst (x, from, to, in_dest, unique_copy)
3005      register rtx x, from, to;
3006      int in_dest;
3007      int unique_copy;
3008 {
3009   register enum rtx_code code = GET_CODE (x);
3010   enum machine_mode op0_mode = VOIDmode;
3011   register char *fmt;
3012   register int len, i;
3013   rtx new;
3014
3015 /* Two expressions are equal if they are identical copies of a shared
3016    RTX or if they are both registers with the same register number
3017    and mode.  */
3018
3019 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
3020   ((X) == (Y)                                           \
3021    || (GET_CODE (X) == REG && GET_CODE (Y) == REG       \
3022        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3023
3024   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3025     {
3026       n_occurrences++;
3027       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3028     }
3029
3030   /* If X and FROM are the same register but different modes, they will
3031      not have been seen as equal above.  However, flow.c will make a 
3032      LOG_LINKS entry for that case.  If we do nothing, we will try to
3033      rerecognize our original insn and, when it succeeds, we will
3034      delete the feeding insn, which is incorrect.
3035
3036      So force this insn not to match in this (rare) case.  */
3037   if (! in_dest && code == REG && GET_CODE (from) == REG
3038       && REGNO (x) == REGNO (from))
3039     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3040
3041   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3042      of which may contain things that can be combined.  */
3043   if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3044     return x;
3045
3046   /* It is possible to have a subexpression appear twice in the insn.
3047      Suppose that FROM is a register that appears within TO.
3048      Then, after that subexpression has been scanned once by `subst',
3049      the second time it is scanned, TO may be found.  If we were
3050      to scan TO here, we would find FROM within it and create a
3051      self-referent rtl structure which is completely wrong.  */
3052   if (COMBINE_RTX_EQUAL_P (x, to))
3053     return to;
3054
3055   len = GET_RTX_LENGTH (code);
3056   fmt = GET_RTX_FORMAT (code);
3057
3058   /* We don't need to process a SET_DEST that is a register, CC0, or PC, so
3059      set up to skip this common case.  All other cases where we want to
3060      suppress replacing something inside a SET_SRC are handled via the
3061      IN_DEST operand.  */
3062   if (code == SET
3063       && (GET_CODE (SET_DEST (x)) == REG
3064         || GET_CODE (SET_DEST (x)) == CC0
3065         || GET_CODE (SET_DEST (x)) == PC))
3066     fmt = "ie";
3067
3068   /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3069      constant.  */
3070   if (fmt[0] == 'e')
3071     op0_mode = GET_MODE (XEXP (x, 0));
3072
3073   for (i = 0; i < len; i++)
3074     {
3075       if (fmt[i] == 'E')
3076         {
3077           register int j;
3078           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3079             {
3080               if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3081                 {
3082                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3083                   n_occurrences++;
3084                 }
3085               else
3086                 {
3087                   new = subst (XVECEXP (x, i, j), from, to, 0, unique_copy);
3088
3089                   /* If this substitution failed, this whole thing fails.  */
3090                   if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3091                     return new;
3092                 }
3093
3094               SUBST (XVECEXP (x, i, j), new);
3095             }
3096         }
3097       else if (fmt[i] == 'e')
3098         {
3099           if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3100             {
3101               /* In general, don't install a subreg involving two modes not
3102                  tieable.  It can worsen register allocation, and can even
3103                  make invalid reload insns, since the reg inside may need to
3104                  be copied from in the outside mode, and that may be invalid
3105                  if it is an fp reg copied in integer mode.
3106
3107                  We allow two exceptions to this: It is valid if it is inside
3108                  another SUBREG and the mode of that SUBREG and the mode of
3109                  the inside of TO is tieable and it is valid if X is a SET
3110                  that copies FROM to CC0.  */
3111               if (GET_CODE (to) == SUBREG
3112                   && ! MODES_TIEABLE_P (GET_MODE (to),
3113                                         GET_MODE (SUBREG_REG (to)))
3114                   && ! (code == SUBREG
3115                         && MODES_TIEABLE_P (GET_MODE (x),
3116                                             GET_MODE (SUBREG_REG (to))))
3117 #ifdef HAVE_cc0
3118                   && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3119 #endif
3120                   )
3121                 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3122
3123               new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3124               n_occurrences++;
3125             }
3126           else
3127             /* If we are in a SET_DEST, suppress most cases unless we
3128                have gone inside a MEM, in which case we want to
3129                simplify the address.  We assume here that things that
3130                are actually part of the destination have their inner
3131                parts in the first expression.  This is true for SUBREG, 
3132                STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3133                things aside from REG and MEM that should appear in a
3134                SET_DEST.  */
3135             new = subst (XEXP (x, i), from, to,
3136                          (((in_dest
3137                             && (code == SUBREG || code == STRICT_LOW_PART
3138                                 || code == ZERO_EXTRACT))
3139                            || code == SET)
3140                           && i == 0), unique_copy);
3141
3142           /* If we found that we will have to reject this combination,
3143              indicate that by returning the CLOBBER ourselves, rather than
3144              an expression containing it.  This will speed things up as
3145              well as prevent accidents where two CLOBBERs are considered
3146              to be equal, thus producing an incorrect simplification.  */
3147
3148           if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3149             return new;
3150
3151           SUBST (XEXP (x, i), new);
3152         }
3153     }
3154
3155   /* Try to simplify X.  If the simplification changed the code, it is likely
3156      that further simplification will help, so loop, but limit the number
3157      of repetitions that will be performed.  */
3158
3159   for (i = 0; i < 4; i++)
3160     {
3161       /* If X is sufficiently simple, don't bother trying to do anything
3162          with it.  */
3163       if (code != CONST_INT && code != REG && code != CLOBBER)
3164         x = simplify_rtx (x, op0_mode, i == 3, in_dest);
3165
3166       if (GET_CODE (x) == code)
3167         break;
3168
3169       code = GET_CODE (x);
3170
3171       /* We no longer know the original mode of operand 0 since we
3172          have changed the form of X)  */
3173       op0_mode = VOIDmode;
3174     }
3175
3176   return x;
3177 }
3178 \f
3179 /* Simplify X, a piece of RTL.  We just operate on the expression at the
3180    outer level; call `subst' to simplify recursively.  Return the new
3181    expression.
3182
3183    OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3184    will be the iteration even if an expression with a code different from
3185    X is returned; IN_DEST is nonzero if we are inside a SET_DEST.  */
3186
3187 static rtx
3188 simplify_rtx (x, op0_mode, last, in_dest)
3189      rtx x;
3190      enum machine_mode op0_mode;
3191      int last;
3192      int in_dest;
3193 {
3194   enum rtx_code code = GET_CODE (x);
3195   enum machine_mode mode = GET_MODE (x);
3196   rtx temp;
3197   int i;
3198
3199   /* If this is a commutative operation, put a constant last and a complex
3200      expression first.  We don't need to do this for comparisons here.  */
3201   if (GET_RTX_CLASS (code) == 'c'
3202       && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3203           || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3204               && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3205           || (GET_CODE (XEXP (x, 0)) == SUBREG
3206               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3207               && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3208     {
3209       temp = XEXP (x, 0);
3210       SUBST (XEXP (x, 0), XEXP (x, 1));
3211       SUBST (XEXP (x, 1), temp);
3212     }
3213
3214   /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3215      sign extension of a PLUS with a constant, reverse the order of the sign
3216      extension and the addition. Note that this not the same as the original
3217      code, but overflow is undefined for signed values.  Also note that the
3218      PLUS will have been partially moved "inside" the sign-extension, so that
3219      the first operand of X will really look like:
3220          (ashiftrt (plus (ashift A C4) C5) C4).
3221      We convert this to
3222          (plus (ashiftrt (ashift A C4) C2) C4)
3223      and replace the first operand of X with that expression.  Later parts
3224      of this function may simplify the expression further.
3225
3226      For example, if we start with (mult (sign_extend (plus A C1)) C2),
3227      we swap the SIGN_EXTEND and PLUS.  Later code will apply the
3228      distributive law to produce (plus (mult (sign_extend X) C1) C3).
3229
3230      We do this to simplify address expressions.  */
3231
3232   if ((code == PLUS || code == MINUS || code == MULT)
3233       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3234       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3235       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3236       && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3237       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3238       && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3239       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3240       && (temp = simplify_binary_operation (ASHIFTRT, mode,
3241                                             XEXP (XEXP (XEXP (x, 0), 0), 1),
3242                                             XEXP (XEXP (x, 0), 1))) != 0)
3243     {
3244       rtx new
3245         = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3246                                 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3247                                 INTVAL (XEXP (XEXP (x, 0), 1)));
3248
3249       new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3250                                   INTVAL (XEXP (XEXP (x, 0), 1)));
3251
3252       SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3253     }
3254
3255   /* If this is a simple operation applied to an IF_THEN_ELSE, try 
3256      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
3257      things.  Check for cases where both arms are testing the same
3258      condition.
3259
3260      Don't do anything if all operands are very simple.  */
3261
3262   if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3263         || GET_RTX_CLASS (code) == '<')
3264        && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3265             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3266                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3267                       == 'o')))
3268            || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3269                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3270                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3271                          == 'o')))))
3272       || (GET_RTX_CLASS (code) == '1'
3273           && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3274                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3275                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3276                          == 'o'))))))
3277     {
3278       rtx cond, true, false;
3279
3280       cond = if_then_else_cond (x, &true, &false);
3281       if (cond != 0
3282           /* If everything is a comparison, what we have is highly unlikely
3283              to be simpler, so don't use it.  */
3284           && ! (GET_RTX_CLASS (code) == '<'
3285                 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3286                     || GET_RTX_CLASS (GET_CODE (false)) == '<')))
3287         {
3288           rtx cop1 = const0_rtx;
3289           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3290
3291           if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3292             return x;
3293
3294           /* Simplify the alternative arms; this may collapse the true and 
3295              false arms to store-flag values.  */
3296           true = subst (true, pc_rtx, pc_rtx, 0, 0);
3297           false = subst (false, pc_rtx, pc_rtx, 0, 0);
3298
3299           /* Restarting if we generate a store-flag expression will cause
3300              us to loop.  Just drop through in this case.  */
3301
3302           /* If the result values are STORE_FLAG_VALUE and zero, we can
3303              just make the comparison operation.  */
3304           if (true == const_true_rtx && false == const0_rtx)
3305             x = gen_binary (cond_code, mode, cond, cop1);
3306           else if (true == const0_rtx && false == const_true_rtx)
3307             x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3308
3309           /* Likewise, we can make the negate of a comparison operation
3310              if the result values are - STORE_FLAG_VALUE and zero.  */
3311           else if (GET_CODE (true) == CONST_INT
3312                    && INTVAL (true) == - STORE_FLAG_VALUE
3313                    && false == const0_rtx)
3314             x = gen_unary (NEG, mode, mode,
3315                            gen_binary (cond_code, mode, cond, cop1));
3316           else if (GET_CODE (false) == CONST_INT
3317                    && INTVAL (false) == - STORE_FLAG_VALUE
3318                    && true == const0_rtx)
3319             x = gen_unary (NEG, mode, mode,
3320                            gen_binary (reverse_condition (cond_code), 
3321                                        mode, cond, cop1));
3322           else
3323             return gen_rtx_IF_THEN_ELSE (mode,
3324                                          gen_binary (cond_code, VOIDmode,
3325                                                      cond, cop1),
3326                                          true, false);
3327
3328           code = GET_CODE (x);
3329           op0_mode = VOIDmode;
3330         }
3331     }
3332
3333   /* Try to fold this expression in case we have constants that weren't
3334      present before.  */
3335   temp = 0;
3336   switch (GET_RTX_CLASS (code))
3337     {
3338     case '1':
3339       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3340       break;
3341     case '<':
3342       temp = simplify_relational_operation (code, op0_mode,
3343                                             XEXP (x, 0), XEXP (x, 1));
3344 #ifdef FLOAT_STORE_FLAG_VALUE
3345       if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3346         temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3347                 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3348 #endif
3349       break;
3350     case 'c':
3351     case '2':
3352       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3353       break;
3354     case 'b':
3355     case '3':
3356       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3357                                          XEXP (x, 1), XEXP (x, 2));
3358       break;
3359     }
3360
3361   if (temp)
3362     x = temp, code = GET_CODE (temp);
3363
3364   /* First see if we can apply the inverse distributive law.  */
3365   if (code == PLUS || code == MINUS
3366       || code == AND || code == IOR || code == XOR)
3367     {
3368       x = apply_distributive_law (x);
3369       code = GET_CODE (x);
3370     }
3371
3372   /* If CODE is an associative operation not otherwise handled, see if we
3373      can associate some operands.  This can win if they are constants or
3374      if they are logically related (i.e. (a & b) & a.  */
3375   if ((code == PLUS || code == MINUS
3376        || code == MULT || code == AND || code == IOR || code == XOR
3377        || code == DIV || code == UDIV
3378        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3379       && INTEGRAL_MODE_P (mode))
3380     {
3381       if (GET_CODE (XEXP (x, 0)) == code)
3382         {
3383           rtx other = XEXP (XEXP (x, 0), 0);
3384           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3385           rtx inner_op1 = XEXP (x, 1);
3386           rtx inner;
3387           
3388           /* Make sure we pass the constant operand if any as the second
3389              one if this is a commutative operation.  */
3390           if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3391             {
3392               rtx tem = inner_op0;
3393               inner_op0 = inner_op1;
3394               inner_op1 = tem;
3395             }
3396           inner = simplify_binary_operation (code == MINUS ? PLUS
3397                                              : code == DIV ? MULT
3398                                              : code == UDIV ? MULT
3399                                              : code,
3400                                              mode, inner_op0, inner_op1);
3401
3402           /* For commutative operations, try the other pair if that one
3403              didn't simplify.  */
3404           if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3405             {
3406               other = XEXP (XEXP (x, 0), 1);
3407               inner = simplify_binary_operation (code, mode,
3408                                                  XEXP (XEXP (x, 0), 0),
3409                                                  XEXP (x, 1));
3410             }
3411
3412           if (inner)
3413             return gen_binary (code, mode, other, inner);
3414         }
3415     }
3416
3417   /* A little bit of algebraic simplification here.  */
3418   switch (code)
3419     {
3420     case MEM:
3421       /* Ensure that our address has any ASHIFTs converted to MULT in case
3422          address-recognizing predicates are called later.  */
3423       temp = make_compound_operation (XEXP (x, 0), MEM);
3424       SUBST (XEXP (x, 0), temp);
3425       break;
3426
3427     case SUBREG:
3428       /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3429          is paradoxical.  If we can't do that safely, then it becomes
3430          something nonsensical so that this combination won't take place.  */
3431
3432       if (GET_CODE (SUBREG_REG (x)) == MEM
3433           && (GET_MODE_SIZE (mode)
3434               <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3435         {
3436           rtx inner = SUBREG_REG (x);
3437           int endian_offset = 0;
3438           /* Don't change the mode of the MEM
3439              if that would change the meaning of the address.  */
3440           if (MEM_VOLATILE_P (SUBREG_REG (x))
3441               || mode_dependent_address_p (XEXP (inner, 0)))
3442             return gen_rtx_CLOBBER (mode, const0_rtx);
3443
3444           if (BYTES_BIG_ENDIAN)
3445             {
3446               if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3447                 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3448               if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3449                 endian_offset -= (UNITS_PER_WORD
3450                                   - GET_MODE_SIZE (GET_MODE (inner)));
3451             }
3452           /* Note if the plus_constant doesn't make a valid address
3453              then this combination won't be accepted.  */
3454           x = gen_rtx_MEM (mode,
3455                            plus_constant (XEXP (inner, 0),
3456                                           (SUBREG_WORD (x) * UNITS_PER_WORD
3457                                            + endian_offset)));
3458           MEM_VOLATILE_P (x) = MEM_VOLATILE_P (inner);
3459           RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3460           MEM_IN_STRUCT_P (x) = MEM_IN_STRUCT_P (inner);
3461           return x;
3462         }
3463
3464       /* If we are in a SET_DEST, these other cases can't apply.  */
3465       if (in_dest)
3466         return x;
3467
3468       /* Changing mode twice with SUBREG => just change it once,
3469          or not at all if changing back to starting mode.  */
3470       if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3471         {
3472           if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3473               && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3474             return SUBREG_REG (SUBREG_REG (x));
3475
3476           SUBST_INT (SUBREG_WORD (x),
3477                      SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3478           SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3479         }
3480
3481       /* SUBREG of a hard register => just change the register number
3482          and/or mode.  If the hard register is not valid in that mode,
3483          suppress this combination.  If the hard register is the stack,
3484          frame, or argument pointer, leave this as a SUBREG.  */
3485
3486       if (GET_CODE (SUBREG_REG (x)) == REG
3487           && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3488           && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3489 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3490           && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3491 #endif
3492 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3493           && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3494 #endif
3495           && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3496         {
3497           if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3498                                   mode))
3499             return gen_rtx_REG (mode,
3500                                 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3501           else
3502             return gen_rtx_CLOBBER (mode, const0_rtx);
3503         }
3504
3505       /* For a constant, try to pick up the part we want.  Handle a full
3506          word and low-order part.  Only do this if we are narrowing
3507          the constant; if it is being widened, we have no idea what
3508          the extra bits will have been set to.  */
3509
3510       if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3511           && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3512           && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
3513           && GET_MODE_CLASS (mode) == MODE_INT)
3514         {
3515           temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3516                                   0, op0_mode);
3517           if (temp)
3518             return temp;
3519         }
3520         
3521       /* If we want a subreg of a constant, at offset 0,
3522          take the low bits.  On a little-endian machine, that's
3523          always valid.  On a big-endian machine, it's valid
3524          only if the constant's mode fits in one word.   Note that we
3525          cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode.  */
3526       if (CONSTANT_P (SUBREG_REG (x))
3527           && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3528               || ! WORDS_BIG_ENDIAN)
3529               ? SUBREG_WORD (x) == 0
3530               : (SUBREG_WORD (x)
3531                  == ((GET_MODE_SIZE (op0_mode)
3532                       - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3533                      / UNITS_PER_WORD)))
3534           && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
3535           && (! WORDS_BIG_ENDIAN
3536               || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
3537         return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3538
3539       /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3540          since we are saying that the high bits don't matter.  */
3541       if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3542           && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3543         return SUBREG_REG (x);
3544
3545       /* Note that we cannot do any narrowing for non-constants since
3546          we might have been counting on using the fact that some bits were
3547          zero.  We now do this in the SET.  */
3548
3549       break;
3550
3551     case NOT:
3552       /* (not (plus X -1)) can become (neg X).  */
3553       if (GET_CODE (XEXP (x, 0)) == PLUS
3554           && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3555         return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3556
3557       /* Similarly, (not (neg X)) is (plus X -1).  */
3558       if (GET_CODE (XEXP (x, 0)) == NEG)
3559         return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3560                                 constm1_rtx);
3561
3562       /* (not (xor X C)) for C constant is (xor X D) with D = ~ C.  */
3563       if (GET_CODE (XEXP (x, 0)) == XOR
3564           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3565           && (temp = simplify_unary_operation (NOT, mode,
3566                                                XEXP (XEXP (x, 0), 1),
3567                                                mode)) != 0)
3568         return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3569               
3570       /* (not (ashift 1 X)) is (rotate ~1 X).  We used to do this for operands
3571          other than 1, but that is not valid.  We could do a similar
3572          simplification for (not (lshiftrt C X)) where C is just the sign bit,
3573          but this doesn't seem common enough to bother with.  */
3574       if (GET_CODE (XEXP (x, 0)) == ASHIFT
3575           && XEXP (XEXP (x, 0), 0) == const1_rtx)
3576         return gen_rtx_ROTATE (mode, gen_unary (NOT, mode, mode, const1_rtx),
3577                                XEXP (XEXP (x, 0), 1));
3578                                             
3579       if (GET_CODE (XEXP (x, 0)) == SUBREG
3580           && subreg_lowpart_p (XEXP (x, 0))
3581           && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3582               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3583           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3584           && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3585         {
3586           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3587
3588           x = gen_rtx_ROTATE (inner_mode,
3589                               gen_unary (NOT, inner_mode, inner_mode,
3590                                          const1_rtx),
3591                               XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3592           return gen_lowpart_for_combine (mode, x);
3593         }
3594                                             
3595       /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3596          reversing the comparison code if valid.  */
3597       if (STORE_FLAG_VALUE == -1
3598           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3599           && reversible_comparison_p (XEXP (x, 0)))
3600         return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3601                                 mode, XEXP (XEXP (x, 0), 0),
3602                                 XEXP (XEXP (x, 0), 1));
3603
3604       /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3605          is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3606          perform the above simplification.  */
3607
3608       if (STORE_FLAG_VALUE == -1
3609           && XEXP (x, 1) == const1_rtx
3610           && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3611           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3612           && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3613         return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3614
3615       /* Apply De Morgan's laws to reduce number of patterns for machines
3616          with negating logical insns (and-not, nand, etc.).  If result has
3617          only one NOT, put it first, since that is how the patterns are
3618          coded.  */
3619
3620       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3621         {
3622          rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3623
3624          if (GET_CODE (in1) == NOT)
3625            in1 = XEXP (in1, 0);
3626          else
3627            in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3628
3629          if (GET_CODE (in2) == NOT)
3630            in2 = XEXP (in2, 0);
3631          else if (GET_CODE (in2) == CONST_INT
3632                   && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3633            in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
3634          else
3635            in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3636
3637          if (GET_CODE (in2) == NOT)
3638            {
3639              rtx tem = in2;
3640              in2 = in1; in1 = tem;
3641            }
3642
3643          return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3644                                  mode, in1, in2);
3645        } 
3646       break;
3647
3648     case NEG:
3649       /* (neg (plus X 1)) can become (not X).  */
3650       if (GET_CODE (XEXP (x, 0)) == PLUS
3651           && XEXP (XEXP (x, 0), 1) == const1_rtx)
3652         return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3653
3654       /* Similarly, (neg (not X)) is (plus X 1).  */
3655       if (GET_CODE (XEXP (x, 0)) == NOT)
3656         return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3657
3658       /* (neg (minus X Y)) can become (minus Y X).  */
3659       if (GET_CODE (XEXP (x, 0)) == MINUS
3660           && (! FLOAT_MODE_P (mode)
3661               /* x-y != -(y-x) with IEEE floating point.  */
3662               || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3663               || flag_fast_math))
3664         return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3665                            XEXP (XEXP (x, 0), 0));
3666
3667       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1.  */
3668       if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3669           && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3670         return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3671
3672       /* NEG commutes with ASHIFT since it is multiplication.  Only do this
3673          if we can then eliminate the NEG (e.g.,
3674          if the operand is a constant).  */
3675
3676       if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3677         {
3678           temp = simplify_unary_operation (NEG, mode,
3679                                            XEXP (XEXP (x, 0), 0), mode);
3680           if (temp)
3681             {
3682               SUBST (XEXP (XEXP (x, 0), 0), temp);
3683               return XEXP (x, 0);
3684             }
3685         }
3686
3687       temp = expand_compound_operation (XEXP (x, 0));
3688
3689       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3690          replaced by (lshiftrt X C).  This will convert
3691          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
3692
3693       if (GET_CODE (temp) == ASHIFTRT
3694           && GET_CODE (XEXP (temp, 1)) == CONST_INT
3695           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3696         return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3697                                      INTVAL (XEXP (temp, 1)));
3698
3699       /* If X has only a single bit that might be nonzero, say, bit I, convert
3700          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3701          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
3702          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
3703          or a SUBREG of one since we'd be making the expression more
3704          complex if it was just a register.  */
3705
3706       if (GET_CODE (temp) != REG
3707           && ! (GET_CODE (temp) == SUBREG
3708                 && GET_CODE (SUBREG_REG (temp)) == REG)
3709           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3710         {
3711           rtx temp1 = simplify_shift_const
3712             (NULL_RTX, ASHIFTRT, mode,
3713              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3714                                    GET_MODE_BITSIZE (mode) - 1 - i),
3715              GET_MODE_BITSIZE (mode) - 1 - i);
3716
3717           /* If all we did was surround TEMP with the two shifts, we
3718              haven't improved anything, so don't use it.  Otherwise,
3719              we are better off with TEMP1.  */
3720           if (GET_CODE (temp1) != ASHIFTRT
3721               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3722               || XEXP (XEXP (temp1, 0), 0) != temp)
3723             return temp1;
3724         }
3725       break;
3726
3727     case TRUNCATE:
3728       /* We can't handle truncation to a partial integer mode here
3729          because we don't know the real bitsize of the partial
3730          integer mode.  */
3731       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3732         break;
3733
3734       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3735         SUBST (XEXP (x, 0),
3736                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3737                               GET_MODE_MASK (mode), NULL_RTX, 0));
3738
3739       /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI.  */
3740       if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3741            || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3742           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3743         return XEXP (XEXP (x, 0), 0);
3744
3745       /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3746          (OP:SI foo:SI) if OP is NEG or ABS.  */
3747       if ((GET_CODE (XEXP (x, 0)) == ABS
3748            || GET_CODE (XEXP (x, 0)) == NEG)
3749           && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3750               || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3751           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3752         return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3753                           XEXP (XEXP (XEXP (x, 0), 0), 0));
3754
3755       /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3756          (truncate:SI x).  */
3757       if (GET_CODE (XEXP (x, 0)) == SUBREG
3758           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3759           && subreg_lowpart_p (XEXP (x, 0)))
3760         return SUBREG_REG (XEXP (x, 0));
3761
3762       /* If we know that the value is already truncated, we can
3763          replace the TRUNCATE with a SUBREG.  */
3764       if (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) <= HOST_BITS_PER_WIDE_INT
3765           && (nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3766               &~ GET_MODE_MASK (mode)) == 0)
3767         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3768
3769       /* A truncate of a comparison can be replaced with a subreg if
3770          STORE_FLAG_VALUE permits.  This is like the previous test,
3771          but it works even if the comparison is done in a mode larger
3772          than HOST_BITS_PER_WIDE_INT.  */
3773       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3774           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3775           && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0)
3776         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3777
3778       /* Similarly, a truncate of a register whose value is a
3779          comparison can be replaced with a subreg if STORE_FLAG_VALUE
3780          permits.  */
3781       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3782           && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0
3783           && (temp = get_last_value (XEXP (x, 0)))
3784           && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3785         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3786
3787       break;
3788
3789     case FLOAT_TRUNCATE:
3790       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
3791       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3792           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3793         return XEXP (XEXP (x, 0), 0);
3794
3795       /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3796          (OP:SF foo:SF) if OP is NEG or ABS.  */
3797       if ((GET_CODE (XEXP (x, 0)) == ABS
3798            || GET_CODE (XEXP (x, 0)) == NEG)
3799           && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3800           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3801         return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3802                           XEXP (XEXP (XEXP (x, 0), 0), 0));
3803
3804       /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3805          is (float_truncate:SF x).  */
3806       if (GET_CODE (XEXP (x, 0)) == SUBREG
3807           && subreg_lowpart_p (XEXP (x, 0))
3808           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3809         return SUBREG_REG (XEXP (x, 0));
3810       break;  
3811
3812 #ifdef HAVE_cc0
3813     case COMPARE:
3814       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3815          using cc0, in which case we want to leave it as a COMPARE
3816          so we can distinguish it from a register-register-copy.  */
3817       if (XEXP (x, 1) == const0_rtx)
3818         return XEXP (x, 0);
3819
3820       /* In IEEE floating point, x-0 is not the same as x.  */
3821       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3822            || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3823            || flag_fast_math)
3824           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3825         return XEXP (x, 0);
3826       break;
3827 #endif
3828
3829     case CONST:
3830       /* (const (const X)) can become (const X).  Do it this way rather than
3831          returning the inner CONST since CONST can be shared with a
3832          REG_EQUAL note.  */
3833       if (GET_CODE (XEXP (x, 0)) == CONST)
3834         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3835       break;
3836
3837 #ifdef HAVE_lo_sum
3838     case LO_SUM:
3839       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
3840          can add in an offset.  find_split_point will split this address up
3841          again if it doesn't match.  */
3842       if (GET_CODE (XEXP (x, 0)) == HIGH
3843           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3844         return XEXP (x, 1);
3845       break;
3846 #endif
3847
3848     case PLUS:
3849       /* If we have (plus (plus (A const) B)), associate it so that CONST is
3850          outermost.  That's because that's the way indexed addresses are
3851          supposed to appear.  This code used to check many more cases, but
3852          they are now checked elsewhere.  */
3853       if (GET_CODE (XEXP (x, 0)) == PLUS
3854           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3855         return gen_binary (PLUS, mode,
3856                            gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3857                                        XEXP (x, 1)),
3858                            XEXP (XEXP (x, 0), 1));
3859
3860       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3861          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3862          bit-field and can be replaced by either a sign_extend or a
3863          sign_extract.  The `and' may be a zero_extend.  */
3864       if (GET_CODE (XEXP (x, 0)) == XOR
3865           && GET_CODE (XEXP (x, 1)) == CONST_INT
3866           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3867           && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
3868           && (i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
3869           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3870           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3871                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3872                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
3873                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
3874               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3875                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3876                       == i + 1))))
3877         return simplify_shift_const
3878           (NULL_RTX, ASHIFTRT, mode,
3879            simplify_shift_const (NULL_RTX, ASHIFT, mode,
3880                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
3881                                  GET_MODE_BITSIZE (mode) - (i + 1)),
3882            GET_MODE_BITSIZE (mode) - (i + 1));
3883
3884       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3885          C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3886          is 1.  This produces better code than the alternative immediately
3887          below.  */
3888       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3889           && reversible_comparison_p (XEXP (x, 0))
3890           && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3891               || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
3892         return
3893           gen_unary (NEG, mode, mode,
3894                      gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3895                                  mode, XEXP (XEXP (x, 0), 0),
3896                                  XEXP (XEXP (x, 0), 1)));
3897
3898       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
3899          can become (ashiftrt (ashift (xor x 1) C) C) where C is
3900          the bitsize of the mode - 1.  This allows simplification of
3901          "a = (b & 8) == 0;"  */
3902       if (XEXP (x, 1) == constm1_rtx
3903           && GET_CODE (XEXP (x, 0)) != REG
3904           && ! (GET_CODE (XEXP (x,0)) == SUBREG
3905                 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
3906           && nonzero_bits (XEXP (x, 0), mode) == 1)
3907         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
3908            simplify_shift_const (NULL_RTX, ASHIFT, mode,
3909                                  gen_rtx_combine (XOR, mode,
3910                                                   XEXP (x, 0), const1_rtx),
3911                                  GET_MODE_BITSIZE (mode) - 1),
3912            GET_MODE_BITSIZE (mode) - 1);
3913
3914       /* If we are adding two things that have no bits in common, convert
3915          the addition into an IOR.  This will often be further simplified,
3916          for example in cases like ((a & 1) + (a & 2)), which can
3917          become a & 3.  */
3918
3919       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3920           && (nonzero_bits (XEXP (x, 0), mode)
3921               & nonzero_bits (XEXP (x, 1), mode)) == 0)
3922         return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
3923       break;
3924
3925     case MINUS:
3926       /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3927          by reversing the comparison code if valid.  */
3928       if (STORE_FLAG_VALUE == 1
3929           && XEXP (x, 0) == const1_rtx
3930           && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
3931           && reversible_comparison_p (XEXP (x, 1)))
3932         return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
3933                            mode, XEXP (XEXP (x, 1), 0),
3934                                 XEXP (XEXP (x, 1), 1));
3935
3936       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
3937          (and <foo> (const_int pow2-1))  */
3938       if (GET_CODE (XEXP (x, 1)) == AND
3939           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3940           && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
3941           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3942         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
3943                                        - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
3944
3945       /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
3946          integers.  */
3947       if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
3948         return gen_binary (MINUS, mode,
3949                            gen_binary (MINUS, mode, XEXP (x, 0),
3950                                        XEXP (XEXP (x, 1), 0)),
3951                            XEXP (XEXP (x, 1), 1));
3952       break;
3953
3954     case MULT:
3955       /* If we have (mult (plus A B) C), apply the distributive law and then
3956          the inverse distributive law to see if things simplify.  This
3957          occurs mostly in addresses, often when unrolling loops.  */
3958
3959       if (GET_CODE (XEXP (x, 0)) == PLUS)
3960         {
3961           x = apply_distributive_law
3962             (gen_binary (PLUS, mode,
3963                          gen_binary (MULT, mode,
3964                                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
3965                          gen_binary (MULT, mode,
3966                                      XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
3967
3968           if (GET_CODE (x) != MULT)
3969             return x;
3970         }
3971       break;
3972
3973     case UDIV:
3974       /* If this is a divide by a power of two, treat it as a shift if
3975          its first operand is a shift.  */
3976       if (GET_CODE (XEXP (x, 1)) == CONST_INT
3977           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
3978           && (GET_CODE (XEXP (x, 0)) == ASHIFT
3979               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
3980               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
3981               || GET_CODE (XEXP (x, 0)) == ROTATE
3982               || GET_CODE (XEXP (x, 0)) == ROTATERT))
3983         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
3984       break;
3985
3986     case EQ:  case NE:
3987     case GT:  case GTU:  case GE:  case GEU:
3988     case LT:  case LTU:  case LE:  case LEU:
3989       /* If the first operand is a condition code, we can't do anything
3990          with it.  */
3991       if (GET_CODE (XEXP (x, 0)) == COMPARE
3992           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
3993 #ifdef HAVE_cc0
3994               && XEXP (x, 0) != cc0_rtx
3995 #endif
3996                ))
3997         {
3998           rtx op0 = XEXP (x, 0);
3999           rtx op1 = XEXP (x, 1);
4000           enum rtx_code new_code;
4001
4002           if (GET_CODE (op0) == COMPARE)
4003             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4004
4005           /* Simplify our comparison, if possible.  */
4006           new_code = simplify_comparison (code, &op0, &op1);
4007
4008           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4009              if only the low-order bit is possibly nonzero in X (such as when
4010              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
4011              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
4012              known to be either 0 or -1, NE becomes a NEG and EQ becomes
4013              (plus X 1).
4014
4015              Remove any ZERO_EXTRACT we made when thinking this was a
4016              comparison.  It may now be simpler to use, e.g., an AND.  If a
4017              ZERO_EXTRACT is indeed appropriate, it will be placed back by
4018              the call to make_compound_operation in the SET case.  */
4019
4020           if (STORE_FLAG_VALUE == 1
4021               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4022               && op1 == const0_rtx && nonzero_bits (op0, mode) == 1)
4023             return gen_lowpart_for_combine (mode,
4024                                             expand_compound_operation (op0));
4025
4026           else if (STORE_FLAG_VALUE == 1
4027                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4028                    && op1 == const0_rtx
4029                    && (num_sign_bit_copies (op0, mode)
4030                        == GET_MODE_BITSIZE (mode)))
4031             {
4032               op0 = expand_compound_operation (op0);
4033               return gen_unary (NEG, mode, mode,
4034                                 gen_lowpart_for_combine (mode, op0));
4035             }
4036
4037           else if (STORE_FLAG_VALUE == 1
4038                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4039                    && op1 == const0_rtx
4040                    && nonzero_bits (op0, mode) == 1)
4041             {
4042               op0 = expand_compound_operation (op0);
4043               return gen_binary (XOR, mode,
4044                                  gen_lowpart_for_combine (mode, op0),
4045                                  const1_rtx);
4046             }
4047
4048           else if (STORE_FLAG_VALUE == 1
4049                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4050                    && op1 == const0_rtx
4051                    && (num_sign_bit_copies (op0, mode)
4052                        == GET_MODE_BITSIZE (mode)))
4053             {
4054               op0 = expand_compound_operation (op0);
4055               return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4056             }
4057
4058           /* If STORE_FLAG_VALUE is -1, we have cases similar to
4059              those above.  */
4060           if (STORE_FLAG_VALUE == -1
4061               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4062               && op1 == const0_rtx
4063               && (num_sign_bit_copies (op0, mode)
4064                   == GET_MODE_BITSIZE (mode)))
4065             return gen_lowpart_for_combine (mode,
4066                                             expand_compound_operation (op0));
4067
4068           else if (STORE_FLAG_VALUE == -1
4069                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4070                    && op1 == const0_rtx
4071                    && nonzero_bits (op0, mode) == 1)
4072             {
4073               op0 = expand_compound_operation (op0);
4074               return gen_unary (NEG, mode, mode,
4075                                 gen_lowpart_for_combine (mode, op0));
4076             }
4077
4078           else if (STORE_FLAG_VALUE == -1
4079                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4080                    && op1 == const0_rtx
4081                    && (num_sign_bit_copies (op0, mode)
4082                        == GET_MODE_BITSIZE (mode)))
4083             {
4084               op0 = expand_compound_operation (op0);
4085               return gen_unary (NOT, mode, mode,
4086                                 gen_lowpart_for_combine (mode, op0));
4087             }
4088
4089           /* If X is 0/1, (eq X 0) is X-1.  */
4090           else if (STORE_FLAG_VALUE == -1
4091                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4092                    && op1 == const0_rtx
4093                    && nonzero_bits (op0, mode) == 1)
4094             {
4095               op0 = expand_compound_operation (op0);
4096               return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4097             }
4098
4099           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4100              one bit that might be nonzero, we can convert (ne x 0) to
4101              (ashift x c) where C puts the bit in the sign bit.  Remove any
4102              AND with STORE_FLAG_VALUE when we are done, since we are only
4103              going to test the sign bit.  */
4104           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4105               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4106               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4107                   == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4108               && op1 == const0_rtx
4109               && mode == GET_MODE (op0)
4110               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4111             {
4112               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4113                                         expand_compound_operation (op0),
4114                                         GET_MODE_BITSIZE (mode) - 1 - i);
4115               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4116                 return XEXP (x, 0);
4117               else
4118                 return x;
4119             }
4120
4121           /* If the code changed, return a whole new comparison.  */
4122           if (new_code != code)
4123             return gen_rtx_combine (new_code, mode, op0, op1);
4124
4125           /* Otherwise, keep this operation, but maybe change its operands.  
4126              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
4127           SUBST (XEXP (x, 0), op0);
4128           SUBST (XEXP (x, 1), op1);
4129         }
4130       break;
4131           
4132     case IF_THEN_ELSE:
4133       return simplify_if_then_else (x);
4134
4135     case ZERO_EXTRACT:
4136     case SIGN_EXTRACT:
4137     case ZERO_EXTEND:
4138     case SIGN_EXTEND:
4139       /* If we are processing SET_DEST, we are done.  */
4140       if (in_dest)
4141         return x;
4142
4143       return expand_compound_operation (x);
4144
4145     case SET:
4146       return simplify_set (x);
4147
4148     case AND:
4149     case IOR:
4150     case XOR:
4151       return simplify_logical (x, last);
4152
4153     case ABS:      
4154       /* (abs (neg <foo>)) -> (abs <foo>) */
4155       if (GET_CODE (XEXP (x, 0)) == NEG)
4156         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4157
4158       /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4159          do nothing.  */
4160       if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4161         break;
4162
4163       /* If operand is something known to be positive, ignore the ABS.  */
4164       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4165           || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4166                <= HOST_BITS_PER_WIDE_INT)
4167               && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4168                    & ((HOST_WIDE_INT) 1
4169                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4170                   == 0)))
4171         return XEXP (x, 0);
4172
4173
4174       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
4175       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4176         return gen_rtx_combine (NEG, mode, XEXP (x, 0));
4177
4178       break;
4179
4180     case FFS:
4181       /* (ffs (*_extend <X>)) = (ffs <X>) */
4182       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4183           || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4184         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4185       break;
4186
4187     case FLOAT:
4188       /* (float (sign_extend <X>)) = (float <X>).  */
4189       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4190         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4191       break;
4192
4193     case ASHIFT:
4194     case LSHIFTRT:
4195     case ASHIFTRT:
4196     case ROTATE:
4197     case ROTATERT:
4198       /* If this is a shift by a constant amount, simplify it.  */
4199       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4200         return simplify_shift_const (x, code, mode, XEXP (x, 0), 
4201                                      INTVAL (XEXP (x, 1)));
4202
4203 #ifdef SHIFT_COUNT_TRUNCATED
4204       else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4205         SUBST (XEXP (x, 1),
4206                force_to_mode (XEXP (x, 1), GET_MODE (x),
4207                               ((HOST_WIDE_INT) 1 
4208                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4209                               - 1,
4210                               NULL_RTX, 0));
4211 #endif
4212
4213       break;
4214
4215     default:
4216       break;
4217     }
4218
4219   return x;
4220 }
4221 \f
4222 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
4223
4224 static rtx
4225 simplify_if_then_else (x)
4226      rtx x;
4227 {
4228   enum machine_mode mode = GET_MODE (x);
4229   rtx cond = XEXP (x, 0);
4230   rtx true = XEXP (x, 1);
4231   rtx false = XEXP (x, 2);
4232   enum rtx_code true_code = GET_CODE (cond);
4233   int comparison_p = GET_RTX_CLASS (true_code) == '<';
4234   rtx temp;
4235   int i;
4236
4237   /* Simplify storing of the truth value.  */
4238   if (comparison_p && true == const_true_rtx && false == const0_rtx)
4239     return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4240       
4241   /* Also when the truth value has to be reversed.  */
4242   if (comparison_p && reversible_comparison_p (cond)
4243       && true == const0_rtx && false == const_true_rtx)
4244     return gen_binary (reverse_condition (true_code),
4245                        mode, XEXP (cond, 0), XEXP (cond, 1));
4246
4247   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4248      in it is being compared against certain values.  Get the true and false
4249      comparisons and see if that says anything about the value of each arm.  */
4250
4251   if (comparison_p && reversible_comparison_p (cond)
4252       && GET_CODE (XEXP (cond, 0)) == REG)
4253     {
4254       HOST_WIDE_INT nzb;
4255       rtx from = XEXP (cond, 0);
4256       enum rtx_code false_code = reverse_condition (true_code);
4257       rtx true_val = XEXP (cond, 1);
4258       rtx false_val = true_val;
4259       int swapped = 0;
4260
4261       /* If FALSE_CODE is EQ, swap the codes and arms.  */
4262
4263       if (false_code == EQ)
4264         {
4265           swapped = 1, true_code = EQ, false_code = NE;
4266           temp = true, true = false, false = temp;
4267         }
4268
4269       /* If we are comparing against zero and the expression being tested has
4270          only a single bit that might be nonzero, that is its value when it is
4271          not equal to zero.  Similarly if it is known to be -1 or 0.  */
4272
4273       if (true_code == EQ && true_val == const0_rtx
4274           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4275         false_code = EQ, false_val = GEN_INT (nzb);
4276       else if (true_code == EQ && true_val == const0_rtx
4277                && (num_sign_bit_copies (from, GET_MODE (from))
4278                    == GET_MODE_BITSIZE (GET_MODE (from))))
4279         false_code = EQ, false_val = constm1_rtx;
4280
4281       /* Now simplify an arm if we know the value of the register in the
4282          branch and it is used in the arm.  Be careful due to the potential
4283          of locally-shared RTL.  */
4284
4285       if (reg_mentioned_p (from, true))
4286         true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4287                       pc_rtx, pc_rtx, 0, 0);
4288       if (reg_mentioned_p (from, false))
4289         false = subst (known_cond (copy_rtx (false), false_code,
4290                                    from, false_val),
4291                        pc_rtx, pc_rtx, 0, 0);
4292
4293       SUBST (XEXP (x, 1), swapped ? false : true);
4294       SUBST (XEXP (x, 2), swapped ? true : false);
4295
4296       true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4297     }
4298
4299   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4300      reversed, do so to avoid needing two sets of patterns for
4301      subtract-and-branch insns.  Similarly if we have a constant in the true
4302      arm, the false arm is the same as the first operand of the comparison, or
4303      the false arm is more complicated than the true arm.  */
4304
4305   if (comparison_p && reversible_comparison_p (cond)
4306       && (true == pc_rtx 
4307           || (CONSTANT_P (true)
4308               && GET_CODE (false) != CONST_INT && false != pc_rtx)
4309           || true == const0_rtx
4310           || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4311               && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4312           || (GET_CODE (true) == SUBREG
4313               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4314               && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4315           || reg_mentioned_p (true, false)
4316           || rtx_equal_p (false, XEXP (cond, 0))))
4317     {
4318       true_code = reverse_condition (true_code);
4319       SUBST (XEXP (x, 0),
4320              gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4321                          XEXP (cond, 1)));
4322
4323       SUBST (XEXP (x, 1), false);
4324       SUBST (XEXP (x, 2), true);
4325
4326       temp = true, true = false, false = temp, cond = XEXP (x, 0);
4327
4328       /* It is possible that the conditional has been simplified out.  */
4329       true_code = GET_CODE (cond);
4330       comparison_p = GET_RTX_CLASS (true_code) == '<';
4331     }
4332
4333   /* If the two arms are identical, we don't need the comparison.  */
4334
4335   if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4336     return true;
4337
4338   /* Convert a == b ? b : a to "a".  */
4339   if (true_code == EQ && ! side_effects_p (cond)
4340       && rtx_equal_p (XEXP (cond, 0), false)
4341       && rtx_equal_p (XEXP (cond, 1), true))
4342     return false;
4343   else if (true_code == NE && ! side_effects_p (cond)
4344            && rtx_equal_p (XEXP (cond, 0), true)
4345            && rtx_equal_p (XEXP (cond, 1), false))
4346     return true;
4347
4348   /* Look for cases where we have (abs x) or (neg (abs X)).  */
4349
4350   if (GET_MODE_CLASS (mode) == MODE_INT
4351       && GET_CODE (false) == NEG
4352       && rtx_equal_p (true, XEXP (false, 0))
4353       && comparison_p
4354       && rtx_equal_p (true, XEXP (cond, 0))
4355       && ! side_effects_p (true))
4356     switch (true_code)
4357       {
4358       case GT:
4359       case GE:
4360         return gen_unary (ABS, mode, mode, true);
4361       case LT:
4362       case LE:
4363         return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
4364     default:
4365       break;
4366       }
4367
4368   /* Look for MIN or MAX.  */
4369
4370   if ((! FLOAT_MODE_P (mode) || flag_fast_math)
4371       && comparison_p
4372       && rtx_equal_p (XEXP (cond, 0), true)
4373       && rtx_equal_p (XEXP (cond, 1), false)
4374       && ! side_effects_p (cond))
4375     switch (true_code)
4376       {
4377       case GE:
4378       case GT:
4379         return gen_binary (SMAX, mode, true, false);
4380       case LE:
4381       case LT:
4382         return gen_binary (SMIN, mode, true, false);
4383       case GEU:
4384       case GTU:
4385         return gen_binary (UMAX, mode, true, false);
4386       case LEU:
4387       case LTU:
4388         return gen_binary (UMIN, mode, true, false);
4389       default:
4390         break;
4391       }
4392   
4393   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4394      second operand is zero, this can be done as (OP Z (mult COND C2)) where
4395      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4396      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4397      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4398      neither 1 or -1, but it isn't worth checking for.  */
4399
4400   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4401       && comparison_p && mode != VOIDmode && ! side_effects_p (x))
4402     {
4403       rtx t = make_compound_operation (true, SET);
4404       rtx f = make_compound_operation (false, SET);
4405       rtx cond_op0 = XEXP (cond, 0);
4406       rtx cond_op1 = XEXP (cond, 1);
4407       enum rtx_code op, extend_op = NIL;
4408       enum machine_mode m = mode;
4409       rtx z = 0, c1;
4410
4411       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4412            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4413            || GET_CODE (t) == ASHIFT
4414            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4415           && rtx_equal_p (XEXP (t, 0), f))
4416         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4417
4418       /* If an identity-zero op is commutative, check whether there
4419          would be a match if we swapped the operands.  */
4420       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4421                 || GET_CODE (t) == XOR)
4422                && rtx_equal_p (XEXP (t, 1), f))
4423         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4424       else if (GET_CODE (t) == SIGN_EXTEND
4425                && (GET_CODE (XEXP (t, 0)) == PLUS
4426                    || GET_CODE (XEXP (t, 0)) == MINUS
4427                    || GET_CODE (XEXP (t, 0)) == IOR
4428                    || GET_CODE (XEXP (t, 0)) == XOR
4429                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4430                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4431                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4432                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4433                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4434                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4435                && (num_sign_bit_copies (f, GET_MODE (f))
4436                    > (GET_MODE_BITSIZE (mode)
4437                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4438         {
4439           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4440           extend_op = SIGN_EXTEND;
4441           m = GET_MODE (XEXP (t, 0));
4442         }
4443       else if (GET_CODE (t) == SIGN_EXTEND
4444                && (GET_CODE (XEXP (t, 0)) == PLUS
4445                    || GET_CODE (XEXP (t, 0)) == IOR
4446                    || GET_CODE (XEXP (t, 0)) == XOR)
4447                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4448                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4449                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4450                && (num_sign_bit_copies (f, GET_MODE (f))
4451                    > (GET_MODE_BITSIZE (mode)
4452                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4453         {
4454           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4455           extend_op = SIGN_EXTEND;
4456           m = GET_MODE (XEXP (t, 0));
4457         }
4458       else if (GET_CODE (t) == ZERO_EXTEND
4459                && (GET_CODE (XEXP (t, 0)) == PLUS
4460                    || GET_CODE (XEXP (t, 0)) == MINUS
4461                    || GET_CODE (XEXP (t, 0)) == IOR
4462                    || GET_CODE (XEXP (t, 0)) == XOR
4463                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4464                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4465                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4466                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4467                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4468                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4469                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4470                && ((nonzero_bits (f, GET_MODE (f))
4471                     & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4472                    == 0))
4473         {
4474           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4475           extend_op = ZERO_EXTEND;
4476           m = GET_MODE (XEXP (t, 0));
4477         }
4478       else if (GET_CODE (t) == ZERO_EXTEND
4479                && (GET_CODE (XEXP (t, 0)) == PLUS
4480                    || GET_CODE (XEXP (t, 0)) == IOR
4481                    || GET_CODE (XEXP (t, 0)) == XOR)
4482                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4483                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4484                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4485                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4486                && ((nonzero_bits (f, GET_MODE (f))
4487                     & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4488                    == 0))
4489         {
4490           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4491           extend_op = ZERO_EXTEND;
4492           m = GET_MODE (XEXP (t, 0));
4493         }
4494       
4495       if (z)
4496         {
4497           temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4498                         pc_rtx, pc_rtx, 0, 0);
4499           temp = gen_binary (MULT, m, temp,
4500                              gen_binary (MULT, m, c1, const_true_rtx));
4501           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4502           temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4503
4504           if (extend_op != NIL)
4505             temp = gen_unary (extend_op, mode, m, temp);
4506
4507           return temp;
4508         }
4509     }
4510
4511   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4512      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4513      negation of a single bit, we can convert this operation to a shift.  We
4514      can actually do this more generally, but it doesn't seem worth it.  */
4515
4516   if (true_code == NE && XEXP (cond, 1) == const0_rtx
4517       && false == const0_rtx && GET_CODE (true) == CONST_INT
4518       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4519            && (i = exact_log2 (INTVAL (true))) >= 0)
4520           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4521                == GET_MODE_BITSIZE (mode))
4522               && (i = exact_log2 (- INTVAL (true))) >= 0)))
4523     return
4524       simplify_shift_const (NULL_RTX, ASHIFT, mode,
4525                             gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4526
4527   return x;
4528 }
4529 \f
4530 /* Simplify X, a SET expression.  Return the new expression.  */
4531
4532 static rtx
4533 simplify_set (x)
4534      rtx x;
4535 {
4536   rtx src = SET_SRC (x);
4537   rtx dest = SET_DEST (x);
4538   enum machine_mode mode
4539     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4540   rtx other_insn;
4541   rtx *cc_use;
4542
4543   /* (set (pc) (return)) gets written as (return).  */
4544   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4545     return src;
4546
4547   /* Now that we know for sure which bits of SRC we are using, see if we can
4548      simplify the expression for the object knowing that we only need the
4549      low-order bits.  */
4550
4551   if (GET_MODE_CLASS (mode) == MODE_INT)
4552     src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4553
4554   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4555      the comparison result and try to simplify it unless we already have used
4556      undobuf.other_insn.  */
4557   if ((GET_CODE (src) == COMPARE
4558 #ifdef HAVE_cc0
4559        || dest == cc0_rtx
4560 #endif
4561        )
4562       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4563       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4564       && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4565       && rtx_equal_p (XEXP (*cc_use, 0), dest))
4566     {
4567       enum rtx_code old_code = GET_CODE (*cc_use);
4568       enum rtx_code new_code;
4569       rtx op0, op1;
4570       int other_changed = 0;
4571       enum machine_mode compare_mode = GET_MODE (dest);
4572
4573       if (GET_CODE (src) == COMPARE)
4574         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4575       else
4576         op0 = src, op1 = const0_rtx;
4577
4578       /* Simplify our comparison, if possible.  */
4579       new_code = simplify_comparison (old_code, &op0, &op1);
4580
4581 #ifdef EXTRA_CC_MODES
4582       /* If this machine has CC modes other than CCmode, check to see if we
4583          need to use a different CC mode here.  */
4584       compare_mode = SELECT_CC_MODE (new_code, op0, op1);
4585 #endif /* EXTRA_CC_MODES */
4586
4587 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
4588       /* If the mode changed, we have to change SET_DEST, the mode in the
4589          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
4590          a hard register, just build new versions with the proper mode.  If it
4591          is a pseudo, we lose unless it is only time we set the pseudo, in
4592          which case we can safely change its mode.  */
4593       if (compare_mode != GET_MODE (dest))
4594         {
4595           int regno = REGNO (dest);
4596           rtx new_dest = gen_rtx_REG (compare_mode, regno);
4597
4598           if (regno < FIRST_PSEUDO_REGISTER
4599               || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
4600             {
4601               if (regno >= FIRST_PSEUDO_REGISTER)
4602                 SUBST (regno_reg_rtx[regno], new_dest);
4603
4604               SUBST (SET_DEST (x), new_dest);
4605               SUBST (XEXP (*cc_use, 0), new_dest);
4606               other_changed = 1;
4607
4608               dest = new_dest;
4609             }
4610         }
4611 #endif
4612
4613       /* If the code changed, we have to build a new comparison in
4614          undobuf.other_insn.  */
4615       if (new_code != old_code)
4616         {
4617           unsigned HOST_WIDE_INT mask;
4618
4619           SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4620                                            dest, const0_rtx));
4621
4622           /* If the only change we made was to change an EQ into an NE or
4623              vice versa, OP0 has only one bit that might be nonzero, and OP1
4624              is zero, check if changing the user of the condition code will
4625              produce a valid insn.  If it won't, we can keep the original code
4626              in that insn by surrounding our operation with an XOR.  */
4627
4628           if (((old_code == NE && new_code == EQ)
4629                || (old_code == EQ && new_code == NE))
4630               && ! other_changed && op1 == const0_rtx
4631               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4632               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
4633             {
4634               rtx pat = PATTERN (other_insn), note = 0;
4635               int scratches;
4636
4637               if ((recog_for_combine (&pat, other_insn, &note, &scratches) < 0
4638                    && ! check_asm_operands (pat)))
4639                 {
4640                   PUT_CODE (*cc_use, old_code);
4641                   other_insn = 0;
4642
4643                   op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
4644                 }
4645             }
4646
4647           other_changed = 1;
4648         }
4649
4650       if (other_changed)
4651         undobuf.other_insn = other_insn;
4652
4653 #ifdef HAVE_cc0
4654       /* If we are now comparing against zero, change our source if
4655          needed.  If we do not use cc0, we always have a COMPARE.  */
4656       if (op1 == const0_rtx && dest == cc0_rtx)
4657         {
4658           SUBST (SET_SRC (x), op0);
4659           src = op0;
4660         }
4661       else
4662 #endif
4663
4664       /* Otherwise, if we didn't previously have a COMPARE in the
4665          correct mode, we need one.  */
4666       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4667         {
4668           SUBST (SET_SRC (x),
4669                  gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4670           src = SET_SRC (x);
4671         }
4672       else
4673         {
4674           /* Otherwise, update the COMPARE if needed.  */
4675           SUBST (XEXP (src, 0), op0);
4676           SUBST (XEXP (src, 1), op1);
4677         }
4678     }
4679   else
4680     {
4681       /* Get SET_SRC in a form where we have placed back any
4682          compound expressions.  Then do the checks below.  */
4683       src = make_compound_operation (src, SET);
4684       SUBST (SET_SRC (x), src);
4685     }
4686
4687   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4688      and X being a REG or (subreg (reg)), we may be able to convert this to
4689      (set (subreg:m2 x) (op)). 
4690
4691      We can always do this if M1 is narrower than M2 because that means that
4692      we only care about the low bits of the result.
4693
4694      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
4695      perform a narrower operation that requested since the high-order bits will
4696      be undefined.  On machine where it is defined, this transformation is safe
4697      as long as M1 and M2 have the same number of words.  */
4698  
4699   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4700       && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4701       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4702            / UNITS_PER_WORD)
4703           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4704                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
4705 #ifndef WORD_REGISTER_OPERATIONS
4706       && (GET_MODE_SIZE (GET_MODE (src))
4707           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4708 #endif
4709 #ifdef CLASS_CANNOT_CHANGE_SIZE
4710       && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4711             && (TEST_HARD_REG_BIT
4712                 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4713                  REGNO (dest)))
4714             && (GET_MODE_SIZE (GET_MODE (src))
4715                 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4716 #endif                            
4717       && (GET_CODE (dest) == REG
4718           || (GET_CODE (dest) == SUBREG
4719               && GET_CODE (SUBREG_REG (dest)) == REG)))
4720     {
4721       SUBST (SET_DEST (x),
4722              gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4723                                       dest));
4724       SUBST (SET_SRC (x), SUBREG_REG (src));
4725
4726       src = SET_SRC (x), dest = SET_DEST (x);
4727     }
4728
4729 #ifdef LOAD_EXTEND_OP
4730   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4731      would require a paradoxical subreg.  Replace the subreg with a
4732      zero_extend to avoid the reload that would otherwise be required.  */
4733
4734   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4735       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4736       && SUBREG_WORD (src) == 0
4737       && (GET_MODE_SIZE (GET_MODE (src))
4738           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4739       && GET_CODE (SUBREG_REG (src)) == MEM)
4740     {
4741       SUBST (SET_SRC (x),
4742              gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4743                               GET_MODE (src), XEXP (src, 0)));
4744
4745       src = SET_SRC (x);
4746     }
4747 #endif
4748
4749   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4750      are comparing an item known to be 0 or -1 against 0, use a logical
4751      operation instead. Check for one of the arms being an IOR of the other
4752      arm with some value.  We compute three terms to be IOR'ed together.  In
4753      practice, at most two will be nonzero.  Then we do the IOR's.  */
4754
4755   if (GET_CODE (dest) != PC
4756       && GET_CODE (src) == IF_THEN_ELSE
4757       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
4758       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4759       && XEXP (XEXP (src, 0), 1) == const0_rtx
4760       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
4761 #ifdef HAVE_conditional_move
4762       && ! can_conditionally_move_p (GET_MODE (src))
4763 #endif
4764       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4765                                GET_MODE (XEXP (XEXP (src, 0), 0)))
4766           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4767       && ! side_effects_p (src))
4768     {
4769       rtx true = (GET_CODE (XEXP (src, 0)) == NE
4770                       ? XEXP (src, 1) : XEXP (src, 2));
4771       rtx false = (GET_CODE (XEXP (src, 0)) == NE
4772                    ? XEXP (src, 2) : XEXP (src, 1));
4773       rtx term1 = const0_rtx, term2, term3;
4774
4775       if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4776         term1 = false, true = XEXP (true, 1), false = const0_rtx;
4777       else if (GET_CODE (true) == IOR
4778                && rtx_equal_p (XEXP (true, 1), false))
4779         term1 = false, true = XEXP (true, 0), false = const0_rtx;
4780       else if (GET_CODE (false) == IOR
4781                && rtx_equal_p (XEXP (false, 0), true))
4782         term1 = true, false = XEXP (false, 1), true = const0_rtx;
4783       else if (GET_CODE (false) == IOR
4784                && rtx_equal_p (XEXP (false, 1), true))
4785         term1 = true, false = XEXP (false, 0), true = const0_rtx;
4786
4787       term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4788       term3 = gen_binary (AND, GET_MODE (src),
4789                           gen_unary (NOT, GET_MODE (src), GET_MODE (src),
4790                                      XEXP (XEXP (src, 0), 0)),
4791                           false);
4792
4793       SUBST (SET_SRC (x),
4794              gen_binary (IOR, GET_MODE (src),
4795                          gen_binary (IOR, GET_MODE (src), term1, term2),
4796                          term3));
4797
4798       src = SET_SRC (x);
4799     }
4800
4801   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
4802      whole thing fail.  */
4803   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
4804     return src;
4805   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
4806     return dest;
4807   else
4808     /* Convert this into a field assignment operation, if possible.  */
4809     return make_field_assignment (x);
4810 }
4811 \f
4812 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
4813    result.  LAST is nonzero if this is the last retry.  */
4814
4815 static rtx
4816 simplify_logical (x, last)
4817      rtx x;
4818      int last;
4819 {
4820   enum machine_mode mode = GET_MODE (x);
4821   rtx op0 = XEXP (x, 0);
4822   rtx op1 = XEXP (x, 1);
4823
4824   switch (GET_CODE (x))
4825     {
4826     case AND:
4827       /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4828          insn (and may simplify more).  */
4829       if (GET_CODE (op0) == XOR
4830           && rtx_equal_p (XEXP (op0, 0), op1)
4831           && ! side_effects_p (op1))
4832         x = gen_binary (AND, mode,
4833                         gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
4834
4835       if (GET_CODE (op0) == XOR
4836           && rtx_equal_p (XEXP (op0, 1), op1)
4837           && ! side_effects_p (op1))
4838         x = gen_binary (AND, mode,
4839                         gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
4840
4841       /* Similarly for (~ (A ^ B)) & A.  */
4842       if (GET_CODE (op0) == NOT
4843           && GET_CODE (XEXP (op0, 0)) == XOR
4844           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4845           && ! side_effects_p (op1))
4846         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4847
4848       if (GET_CODE (op0) == NOT
4849           && GET_CODE (XEXP (op0, 0)) == XOR
4850           && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4851           && ! side_effects_p (op1))
4852         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4853
4854       if (GET_CODE (op1) == CONST_INT)
4855         {
4856           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
4857
4858           /* If we have (ior (and (X C1) C2)) and the next restart would be
4859              the last, simplify this by making C1 as small as possible
4860              and then exit.  */
4861           if (last
4862               && GET_CODE (x) == IOR && GET_CODE (op0) == AND
4863               && GET_CODE (XEXP (op0, 1)) == CONST_INT
4864               && GET_CODE (op1) == CONST_INT)
4865             return gen_binary (IOR, mode,
4866                                gen_binary (AND, mode, XEXP (op0, 0),
4867                                            GEN_INT (INTVAL (XEXP (op0, 1))
4868                                                     & ~ INTVAL (op1))), op1);
4869
4870           if (GET_CODE (x) != AND)
4871             return x;
4872
4873           if (GET_RTX_CLASS (GET_CODE (x)) == 'c' 
4874               || GET_RTX_CLASS (GET_CODE (x)) == '2')
4875             op0 = XEXP (x, 0), op1 = XEXP (x, 1);
4876         }
4877
4878       /* Convert (A | B) & A to A.  */
4879       if (GET_CODE (op0) == IOR
4880           && (rtx_equal_p (XEXP (op0, 0), op1)
4881               || rtx_equal_p (XEXP (op0, 1), op1))
4882           && ! side_effects_p (XEXP (op0, 0))
4883           && ! side_effects_p (XEXP (op0, 1)))
4884         return op1;
4885
4886       /* In the following group of tests (and those in case IOR below),
4887          we start with some combination of logical operations and apply
4888          the distributive law followed by the inverse distributive law.
4889          Most of the time, this results in no change.  However, if some of
4890          the operands are the same or inverses of each other, simplifications
4891          will result.
4892
4893          For example, (and (ior A B) (not B)) can occur as the result of
4894          expanding a bit field assignment.  When we apply the distributive
4895          law to this, we get (ior (and (A (not B))) (and (B (not B)))),
4896          which then simplifies to (and (A (not B))). 
4897
4898          If we have (and (ior A B) C), apply the distributive law and then
4899          the inverse distributive law to see if things simplify.  */
4900
4901       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4902         {
4903           x = apply_distributive_law
4904             (gen_binary (GET_CODE (op0), mode,
4905                          gen_binary (AND, mode, XEXP (op0, 0), op1),
4906                          gen_binary (AND, mode, XEXP (op0, 1), op1)));
4907           if (GET_CODE (x) != AND)
4908             return x;
4909         }
4910
4911       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
4912         return apply_distributive_law
4913           (gen_binary (GET_CODE (op1), mode,
4914                        gen_binary (AND, mode, XEXP (op1, 0), op0),
4915                        gen_binary (AND, mode, XEXP (op1, 1), op0)));
4916
4917       /* Similarly, taking advantage of the fact that
4918          (and (not A) (xor B C)) == (xor (ior A B) (ior A C))  */
4919
4920       if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
4921         return apply_distributive_law
4922           (gen_binary (XOR, mode,
4923                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
4924                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
4925                                                             
4926       else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
4927         return apply_distributive_law
4928           (gen_binary (XOR, mode,
4929                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
4930                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
4931       break;
4932
4933     case IOR:
4934       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
4935       if (GET_CODE (op1) == CONST_INT
4936           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4937           && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
4938         return op1;
4939
4940       /* Convert (A & B) | A to A.  */
4941       if (GET_CODE (op0) == AND
4942           && (rtx_equal_p (XEXP (op0, 0), op1)
4943               || rtx_equal_p (XEXP (op0, 1), op1))
4944           && ! side_effects_p (XEXP (op0, 0))
4945           && ! side_effects_p (XEXP (op0, 1)))
4946         return op1;
4947
4948       /* If we have (ior (and A B) C), apply the distributive law and then
4949          the inverse distributive law to see if things simplify.  */
4950
4951       if (GET_CODE (op0) == AND)
4952         {
4953           x = apply_distributive_law
4954             (gen_binary (AND, mode,
4955                          gen_binary (IOR, mode, XEXP (op0, 0), op1),
4956                          gen_binary (IOR, mode, XEXP (op0, 1), op1)));
4957
4958           if (GET_CODE (x) != IOR)
4959             return x;
4960         }
4961
4962       if (GET_CODE (op1) == AND)
4963         {
4964           x = apply_distributive_law
4965             (gen_binary (AND, mode,
4966                          gen_binary (IOR, mode, XEXP (op1, 0), op0),
4967                          gen_binary (IOR, mode, XEXP (op1, 1), op0)));
4968
4969           if (GET_CODE (x) != IOR)
4970             return x;
4971         }
4972
4973       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4974          mode size to (rotate A CX).  */
4975
4976       if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
4977            || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
4978           && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
4979           && GET_CODE (XEXP (op0, 1)) == CONST_INT
4980           && GET_CODE (XEXP (op1, 1)) == CONST_INT
4981           && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
4982               == GET_MODE_BITSIZE (mode)))
4983         return gen_rtx_ROTATE (mode, XEXP (op0, 0),
4984                                (GET_CODE (op0) == ASHIFT
4985                                 ? XEXP (op0, 1) : XEXP (op1, 1)));
4986
4987       /* If OP0 is (ashiftrt (plus ...) C), it might actually be
4988          a (sign_extend (plus ...)).  If so, OP1 is a CONST_INT, and the PLUS
4989          does not affect any of the bits in OP1, it can really be done
4990          as a PLUS and we can associate.  We do this by seeing if OP1
4991          can be safely shifted left C bits.  */
4992       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
4993           && GET_CODE (XEXP (op0, 0)) == PLUS
4994           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
4995           && GET_CODE (XEXP (op0, 1)) == CONST_INT
4996           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
4997         {
4998           int count = INTVAL (XEXP (op0, 1));
4999           HOST_WIDE_INT mask = INTVAL (op1) << count;
5000
5001           if (mask >> count == INTVAL (op1)
5002               && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5003             {
5004               SUBST (XEXP (XEXP (op0, 0), 1),
5005                      GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5006               return op0;
5007             }
5008         }
5009       break;
5010
5011     case XOR:
5012       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5013          Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5014          (NOT y).  */
5015       {
5016         int num_negated = 0;
5017
5018         if (GET_CODE (op0) == NOT)
5019           num_negated++, op0 = XEXP (op0, 0);
5020         if (GET_CODE (op1) == NOT)
5021           num_negated++, op1 = XEXP (op1, 0);
5022
5023         if (num_negated == 2)
5024           {
5025             SUBST (XEXP (x, 0), op0);
5026             SUBST (XEXP (x, 1), op1);
5027           }
5028         else if (num_negated == 1)
5029           return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
5030       }
5031
5032       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
5033          correspond to a machine insn or result in further simplifications
5034          if B is a constant.  */
5035
5036       if (GET_CODE (op0) == AND
5037           && rtx_equal_p (XEXP (op0, 1), op1)
5038           && ! side_effects_p (op1))
5039         return gen_binary (AND, mode,
5040                            gen_unary (NOT, mode, mode, XEXP (op0, 0)),
5041                            op1);
5042
5043       else if (GET_CODE (op0) == AND
5044                && rtx_equal_p (XEXP (op0, 0), op1)
5045                && ! side_effects_p (op1))
5046         return gen_binary (AND, mode,
5047                            gen_unary (NOT, mode, mode, XEXP (op0, 1)),
5048                            op1);
5049
5050       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5051          comparison if STORE_FLAG_VALUE is 1.  */
5052       if (STORE_FLAG_VALUE == 1
5053           && op1 == const1_rtx
5054           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5055           && reversible_comparison_p (op0))
5056         return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5057                                 mode, XEXP (op0, 0), XEXP (op0, 1));
5058
5059       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5060          is (lt foo (const_int 0)), so we can perform the above
5061          simplification if STORE_FLAG_VALUE is 1.  */
5062
5063       if (STORE_FLAG_VALUE == 1
5064           && op1 == const1_rtx
5065           && GET_CODE (op0) == LSHIFTRT
5066           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5067           && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5068         return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
5069
5070       /* (xor (comparison foo bar) (const_int sign-bit))
5071          when STORE_FLAG_VALUE is the sign bit.  */
5072       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5073           && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5074               == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5075           && op1 == const_true_rtx
5076           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5077           && reversible_comparison_p (op0))
5078         return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5079                                 mode, XEXP (op0, 0), XEXP (op0, 1));
5080       break;
5081
5082     default:
5083       abort ();
5084     }
5085
5086   return x;
5087 }
5088 \f
5089 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5090    operations" because they can be replaced with two more basic operations.
5091    ZERO_EXTEND is also considered "compound" because it can be replaced with
5092    an AND operation, which is simpler, though only one operation.
5093
5094    The function expand_compound_operation is called with an rtx expression
5095    and will convert it to the appropriate shifts and AND operations, 
5096    simplifying at each stage.
5097
5098    The function make_compound_operation is called to convert an expression
5099    consisting of shifts and ANDs into the equivalent compound expression.
5100    It is the inverse of this function, loosely speaking.  */
5101
5102 static rtx
5103 expand_compound_operation (x)
5104      rtx x;
5105 {
5106   int pos = 0, len;
5107   int unsignedp = 0;
5108   int modewidth;
5109   rtx tem;
5110
5111   switch (GET_CODE (x))
5112     {
5113     case ZERO_EXTEND:
5114       unsignedp = 1;
5115     case SIGN_EXTEND:
5116       /* We can't necessarily use a const_int for a multiword mode;
5117          it depends on implicitly extending the value.
5118          Since we don't know the right way to extend it,
5119          we can't tell whether the implicit way is right.
5120
5121          Even for a mode that is no wider than a const_int,
5122          we can't win, because we need to sign extend one of its bits through
5123          the rest of it, and we don't know which bit.  */
5124       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5125         return x;
5126
5127       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5128          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
5129          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5130          reloaded. If not for that, MEM's would very rarely be safe.
5131
5132          Reject MODEs bigger than a word, because we might not be able
5133          to reference a two-register group starting with an arbitrary register
5134          (and currently gen_lowpart might crash for a SUBREG).  */
5135   
5136       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5137         return x;
5138
5139       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5140       /* If the inner object has VOIDmode (the only way this can happen
5141          is if it is a ASM_OPERANDS), we can't do anything since we don't
5142          know how much masking to do.  */
5143       if (len == 0)
5144         return x;
5145
5146       break;
5147
5148     case ZERO_EXTRACT:
5149       unsignedp = 1;
5150     case SIGN_EXTRACT:
5151       /* If the operand is a CLOBBER, just return it.  */
5152       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5153         return XEXP (x, 0);
5154
5155       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5156           || GET_CODE (XEXP (x, 2)) != CONST_INT
5157           || GET_MODE (XEXP (x, 0)) == VOIDmode)
5158         return x;
5159
5160       len = INTVAL (XEXP (x, 1));
5161       pos = INTVAL (XEXP (x, 2));
5162
5163       /* If this goes outside the object being extracted, replace the object
5164          with a (use (mem ...)) construct that only combine understands
5165          and is used only for this purpose.  */
5166       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5167         SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5168
5169       if (BITS_BIG_ENDIAN)
5170         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5171
5172       break;
5173
5174     default:
5175       return x;
5176     }
5177
5178   /* We can optimize some special cases of ZERO_EXTEND.  */
5179   if (GET_CODE (x) == ZERO_EXTEND)
5180     {
5181       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5182          know that the last value didn't have any inappropriate bits
5183          set.  */
5184       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5185           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5186           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5187           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5188               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5189         return XEXP (XEXP (x, 0), 0);
5190
5191       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5192       if (GET_CODE (XEXP (x, 0)) == SUBREG
5193           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5194           && subreg_lowpart_p (XEXP (x, 0))
5195           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5196           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5197               & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0)
5198         return SUBREG_REG (XEXP (x, 0));
5199
5200       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5201          is a comparison and STORE_FLAG_VALUE permits.  This is like
5202          the first case, but it works even when GET_MODE (x) is larger
5203          than HOST_WIDE_INT.  */
5204       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5205           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5206           && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5207           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5208               <= HOST_BITS_PER_WIDE_INT)
5209           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5210               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5211         return XEXP (XEXP (x, 0), 0);
5212
5213       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5214       if (GET_CODE (XEXP (x, 0)) == SUBREG
5215           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5216           && subreg_lowpart_p (XEXP (x, 0))
5217           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5218           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5219               <= HOST_BITS_PER_WIDE_INT)
5220           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5221               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5222         return SUBREG_REG (XEXP (x, 0));
5223
5224       /* If sign extension is cheaper than zero extension, then use it
5225          if we know that no extraneous bits are set, and that the high
5226          bit is not set.  */
5227       if (flag_expensive_optimizations
5228           && ((GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5229                && ((nonzero_bits (XEXP (x, 0), GET_MODE (x))
5230                     & ~ (((unsigned HOST_WIDE_INT)
5231                           GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5232                          >> 1))
5233                    == 0))
5234               || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5235                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5236                       <= HOST_BITS_PER_WIDE_INT)
5237                   && (((HOST_WIDE_INT) STORE_FLAG_VALUE
5238                        & ~ (((unsigned HOST_WIDE_INT)
5239                              GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5240                             >> 1))
5241                       == 0))))
5242         {
5243           rtx temp = gen_rtx_SIGN_EXTEND (GET_MODE (x), XEXP (x, 0));
5244
5245           if (rtx_cost (temp, SET) < rtx_cost (x, SET))
5246             return expand_compound_operation (temp);
5247         }
5248     }
5249
5250   /* If we reach here, we want to return a pair of shifts.  The inner
5251      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
5252      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
5253      logical depending on the value of UNSIGNEDP.
5254
5255      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5256      converted into an AND of a shift.
5257
5258      We must check for the case where the left shift would have a negative
5259      count.  This can happen in a case like (x >> 31) & 255 on machines
5260      that can't shift by a constant.  On those machines, we would first
5261      combine the shift with the AND to produce a variable-position 
5262      extraction.  Then the constant of 31 would be substituted in to produce
5263      a such a position.  */
5264
5265   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5266   if (modewidth >= pos - len)
5267     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5268                                 GET_MODE (x),
5269                                 simplify_shift_const (NULL_RTX, ASHIFT,
5270                                                       GET_MODE (x),
5271                                                       XEXP (x, 0),
5272                                                       modewidth - pos - len),
5273                                 modewidth - len);
5274
5275   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5276     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5277                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
5278                                                         GET_MODE (x),
5279                                                         XEXP (x, 0), pos),
5280                                   ((HOST_WIDE_INT) 1 << len) - 1);
5281   else
5282     /* Any other cases we can't handle.  */
5283     return x;
5284     
5285
5286   /* If we couldn't do this for some reason, return the original
5287      expression.  */
5288   if (GET_CODE (tem) == CLOBBER)
5289     return x;
5290
5291   return tem;
5292 }
5293 \f
5294 /* X is a SET which contains an assignment of one object into
5295    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5296    or certain SUBREGS). If possible, convert it into a series of
5297    logical operations.
5298
5299    We half-heartedly support variable positions, but do not at all
5300    support variable lengths.  */
5301
5302 static rtx
5303 expand_field_assignment (x)
5304      rtx x;
5305 {
5306   rtx inner;
5307   rtx pos;                      /* Always counts from low bit.  */
5308   int len;
5309   rtx mask;
5310   enum machine_mode compute_mode;
5311
5312   /* Loop until we find something we can't simplify.  */
5313   while (1)
5314     {
5315       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5316           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5317         {
5318           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5319           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5320           pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
5321         }
5322       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5323                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5324         {
5325           inner = XEXP (SET_DEST (x), 0);
5326           len = INTVAL (XEXP (SET_DEST (x), 1));
5327           pos = XEXP (SET_DEST (x), 2);
5328
5329           /* If the position is constant and spans the width of INNER,
5330              surround INNER  with a USE to indicate this.  */
5331           if (GET_CODE (pos) == CONST_INT
5332               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5333             inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5334
5335           if (BITS_BIG_ENDIAN)
5336             {
5337               if (GET_CODE (pos) == CONST_INT)
5338                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5339                                - INTVAL (pos));
5340               else if (GET_CODE (pos) == MINUS
5341                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
5342                        && (INTVAL (XEXP (pos, 1))
5343                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5344                 /* If position is ADJUST - X, new position is X.  */
5345                 pos = XEXP (pos, 0);
5346               else
5347                 pos = gen_binary (MINUS, GET_MODE (pos),
5348                                   GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5349                                            - len),
5350                                   pos);
5351             }
5352         }
5353
5354       /* A SUBREG between two modes that occupy the same numbers of words
5355          can be done by moving the SUBREG to the source.  */
5356       else if (GET_CODE (SET_DEST (x)) == SUBREG
5357                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5358                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5359                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5360                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5361         {
5362           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5363                            gen_lowpart_for_combine (GET_MODE (SUBREG_REG (SET_DEST (x))),
5364                                                     SET_SRC (x)));
5365           continue;
5366         }
5367       else
5368         break;
5369
5370       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5371         inner = SUBREG_REG (inner);
5372
5373       compute_mode = GET_MODE (inner);
5374
5375       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
5376       if (len < HOST_BITS_PER_WIDE_INT)
5377         mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5378       else
5379         break;
5380
5381       /* Now compute the equivalent expression.  Make a copy of INNER
5382          for the SET_DEST in case it is a MEM into which we will substitute;
5383          we don't want shared RTL in that case.  */
5384       x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
5385                        gen_binary (IOR, compute_mode,
5386                                    gen_binary (AND, compute_mode,
5387                                                gen_unary (NOT, compute_mode,
5388                                                           compute_mode,
5389                                                           gen_binary (ASHIFT,
5390                                                                       compute_mode,
5391                                                                       mask, pos)),
5392                                                inner),
5393                                    gen_binary (ASHIFT, compute_mode,
5394                                                gen_binary (AND, compute_mode,
5395                                                            gen_lowpart_for_combine
5396                                                            (compute_mode,
5397                                                             SET_SRC (x)),
5398                                                            mask),
5399                                                pos)));
5400     }
5401
5402   return x;
5403 }
5404 \f
5405 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
5406    it is an RTX that represents a variable starting position; otherwise,
5407    POS is the (constant) starting bit position (counted from the LSB).
5408
5409    INNER may be a USE.  This will occur when we started with a bitfield
5410    that went outside the boundary of the object in memory, which is
5411    allowed on most machines.  To isolate this case, we produce a USE
5412    whose mode is wide enough and surround the MEM with it.  The only
5413    code that understands the USE is this routine.  If it is not removed,
5414    it will cause the resulting insn not to match.
5415
5416    UNSIGNEDP is non-zero for an unsigned reference and zero for a 
5417    signed reference.
5418
5419    IN_DEST is non-zero if this is a reference in the destination of a
5420    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If non-zero,
5421    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5422    be used.
5423
5424    IN_COMPARE is non-zero if we are in a COMPARE.  This means that a
5425    ZERO_EXTRACT should be built even for bits starting at bit 0.
5426
5427    MODE is the desired mode of the result (if IN_DEST == 0).
5428
5429    The result is an RTX for the extraction or NULL_RTX if the target
5430    can't handle it.  */
5431
5432 static rtx
5433 make_extraction (mode, inner, pos, pos_rtx, len,
5434                  unsignedp, in_dest, in_compare)
5435      enum machine_mode mode;
5436      rtx inner;
5437      int pos;
5438      rtx pos_rtx;
5439      int len;
5440      int unsignedp;
5441      int in_dest, in_compare;
5442 {
5443   /* This mode describes the size of the storage area
5444      to fetch the overall value from.  Within that, we
5445      ignore the POS lowest bits, etc.  */
5446   enum machine_mode is_mode = GET_MODE (inner);
5447   enum machine_mode inner_mode;
5448   enum machine_mode wanted_inner_mode = byte_mode;
5449   enum machine_mode wanted_inner_reg_mode = word_mode;
5450   enum machine_mode pos_mode = word_mode;
5451   enum machine_mode extraction_mode = word_mode;
5452   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5453   int spans_byte = 0;
5454   rtx new = 0;
5455   rtx orig_pos_rtx = pos_rtx;
5456   int orig_pos;
5457
5458   /* Get some information about INNER and get the innermost object.  */
5459   if (GET_CODE (inner) == USE)
5460     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
5461     /* We don't need to adjust the position because we set up the USE
5462        to pretend that it was a full-word object.  */
5463     spans_byte = 1, inner = XEXP (inner, 0);
5464   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5465     {
5466       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5467          consider just the QI as the memory to extract from.
5468          The subreg adds or removes high bits; its mode is
5469          irrelevant to the meaning of this extraction,
5470          since POS and LEN count from the lsb.  */
5471       if (GET_CODE (SUBREG_REG (inner)) == MEM)
5472         is_mode = GET_MODE (SUBREG_REG (inner));
5473       inner = SUBREG_REG (inner);
5474     }
5475
5476   inner_mode = GET_MODE (inner);
5477
5478   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5479     pos = INTVAL (pos_rtx), pos_rtx = 0;
5480
5481   /* See if this can be done without an extraction.  We never can if the
5482      width of the field is not the same as that of some integer mode. For
5483      registers, we can only avoid the extraction if the position is at the
5484      low-order bit and this is either not in the destination or we have the
5485      appropriate STRICT_LOW_PART operation available.
5486
5487      For MEM, we can avoid an extract if the field starts on an appropriate
5488      boundary and we can change the mode of the memory reference.  However,
5489      we cannot directly access the MEM if we have a USE and the underlying
5490      MEM is not TMODE.  This combination means that MEM was being used in a
5491      context where bits outside its mode were being referenced; that is only
5492      valid in bit-field insns.  */
5493
5494   if (tmode != BLKmode
5495       && ! (spans_byte && inner_mode != tmode)
5496       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5497            && GET_CODE (inner) != MEM
5498            && (! in_dest
5499                || (GET_CODE (inner) == REG
5500                    && (movstrict_optab->handlers[(int) tmode].insn_code
5501                        != CODE_FOR_nothing))))
5502           || (GET_CODE (inner) == MEM && pos_rtx == 0
5503               && (pos
5504                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5505                      : BITS_PER_UNIT)) == 0
5506               /* We can't do this if we are widening INNER_MODE (it
5507                  may not be aligned, for one thing).  */
5508               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5509               && (inner_mode == tmode
5510                   || (! mode_dependent_address_p (XEXP (inner, 0))
5511                       && ! MEM_VOLATILE_P (inner))))))
5512     {
5513       /* If INNER is a MEM, make a new MEM that encompasses just the desired
5514          field.  If the original and current mode are the same, we need not
5515          adjust the offset.  Otherwise, we do if bytes big endian.  
5516
5517          If INNER is not a MEM, get a piece consisting of just the field
5518          of interest (in this case POS % BITS_PER_WORD must be 0).  */
5519
5520       if (GET_CODE (inner) == MEM)
5521         {
5522           int offset;
5523           /* POS counts from lsb, but make OFFSET count in memory order.  */
5524           if (BYTES_BIG_ENDIAN)
5525             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5526           else
5527             offset = pos / BITS_PER_UNIT;
5528
5529           new = gen_rtx_MEM (tmode, plus_constant (XEXP (inner, 0), offset));
5530           RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
5531           MEM_VOLATILE_P (new) = MEM_VOLATILE_P (inner);
5532           MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (inner);
5533         }
5534       else if (GET_CODE (inner) == REG)
5535         {
5536           /* We can't call gen_lowpart_for_combine here since we always want
5537              a SUBREG and it would sometimes return a new hard register.  */
5538           if (tmode != inner_mode)
5539             new = gen_rtx_SUBREG (tmode, inner,
5540                                   (WORDS_BIG_ENDIAN
5541                                    && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD
5542                                    ? (((GET_MODE_SIZE (inner_mode)
5543                                         - GET_MODE_SIZE (tmode))
5544                                        / UNITS_PER_WORD)
5545                                       - pos / BITS_PER_WORD)
5546                                    : pos / BITS_PER_WORD));
5547           else
5548             new = inner;
5549         }
5550       else
5551         new = force_to_mode (inner, tmode,
5552                              len >= HOST_BITS_PER_WIDE_INT
5553                              ? GET_MODE_MASK (tmode)
5554                              : ((HOST_WIDE_INT) 1 << len) - 1,
5555                              NULL_RTX, 0);
5556
5557       /* If this extraction is going into the destination of a SET, 
5558          make a STRICT_LOW_PART unless we made a MEM.  */
5559
5560       if (in_dest)
5561         return (GET_CODE (new) == MEM ? new
5562                 : (GET_CODE (new) != SUBREG
5563                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
5564                    : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
5565
5566       /* Otherwise, sign- or zero-extend unless we already are in the
5567          proper mode.  */
5568
5569       return (mode == tmode ? new
5570               : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5571                                  mode, new));
5572     }
5573
5574   /* Unless this is a COMPARE or we have a funny memory reference,
5575      don't do anything with zero-extending field extracts starting at
5576      the low-order bit since they are simple AND operations.  */
5577   if (pos_rtx == 0 && pos == 0 && ! in_dest
5578       && ! in_compare && ! spans_byte && unsignedp)
5579     return 0;
5580
5581   /* Unless we are allowed to span bytes, reject this if we would be
5582      spanning bytes or if the position is not a constant and the length
5583      is not 1.  In all other cases, we would only be going outside
5584      out object in cases when an original shift would have been
5585      undefined.  */
5586   if (! spans_byte
5587       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5588           || (pos_rtx != 0 && len != 1)))
5589     return 0;
5590
5591   /* Get the mode to use should INNER not be a MEM, the mode for the position,
5592      and the mode for the result.  */
5593 #ifdef HAVE_insv
5594   if (in_dest)
5595     {
5596       wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_insv][0];
5597       pos_mode = insn_operand_mode[(int) CODE_FOR_insv][2];
5598       extraction_mode = insn_operand_mode[(int) CODE_FOR_insv][3];
5599     }
5600 #endif
5601
5602 #ifdef HAVE_extzv
5603   if (! in_dest && unsignedp)
5604     {
5605       wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
5606       pos_mode = insn_operand_mode[(int) CODE_FOR_extzv][3];
5607       extraction_mode = insn_operand_mode[(int) CODE_FOR_extzv][0];
5608     }
5609 #endif
5610
5611 #ifdef HAVE_extv
5612   if (! in_dest && ! unsignedp)
5613     {
5614       wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
5615       pos_mode = insn_operand_mode[(int) CODE_FOR_extv][3];
5616       extraction_mode = insn_operand_mode[(int) CODE_FOR_extv][0];
5617     }
5618 #endif
5619
5620   /* Never narrow an object, since that might not be safe.  */
5621
5622   if (mode != VOIDmode
5623       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5624     extraction_mode = mode;
5625
5626   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5627       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5628     pos_mode = GET_MODE (pos_rtx);
5629
5630   /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5631      if we have to change the mode of memory and cannot, the desired mode is
5632      EXTRACTION_MODE.  */
5633   if (GET_CODE (inner) != MEM)
5634     wanted_inner_mode = wanted_inner_reg_mode;
5635   else if (inner_mode != wanted_inner_mode
5636            && (mode_dependent_address_p (XEXP (inner, 0))
5637                || MEM_VOLATILE_P (inner)))
5638     wanted_inner_mode = extraction_mode;
5639
5640   orig_pos = pos;
5641
5642   if (BITS_BIG_ENDIAN)
5643     {
5644       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5645          BITS_BIG_ENDIAN style.  If position is constant, compute new
5646          position.  Otherwise, build subtraction.
5647          Note that POS is relative to the mode of the original argument.
5648          If it's a MEM we need to recompute POS relative to that.
5649          However, if we're extracting from (or inserting into) a register,
5650          we want to recompute POS relative to wanted_inner_mode.  */
5651       int width = (GET_CODE (inner) == MEM
5652                    ? GET_MODE_BITSIZE (is_mode)
5653                    : GET_MODE_BITSIZE (wanted_inner_mode));
5654
5655       if (pos_rtx == 0)
5656         pos = width - len - pos;
5657       else
5658         pos_rtx
5659           = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
5660                              GEN_INT (width - len), pos_rtx);
5661       /* POS may be less than 0 now, but we check for that below.
5662          Note that it can only be less than 0 if GET_CODE (inner) != MEM.  */
5663     }
5664
5665   /* If INNER has a wider mode, make it smaller.  If this is a constant
5666      extract, try to adjust the byte to point to the byte containing
5667      the value.  */
5668   if (wanted_inner_mode != VOIDmode
5669       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
5670       && ((GET_CODE (inner) == MEM
5671            && (inner_mode == wanted_inner_mode
5672                || (! mode_dependent_address_p (XEXP (inner, 0))
5673                    && ! MEM_VOLATILE_P (inner))))))
5674     {
5675       int offset = 0;
5676
5677       /* The computations below will be correct if the machine is big
5678          endian in both bits and bytes or little endian in bits and bytes.
5679          If it is mixed, we must adjust.  */
5680              
5681       /* If bytes are big endian and we had a paradoxical SUBREG, we must
5682          adjust OFFSET to compensate.  */
5683       if (BYTES_BIG_ENDIAN
5684           && ! spans_byte
5685           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5686         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
5687
5688       /* If this is a constant position, we can move to the desired byte.  */
5689       if (pos_rtx == 0)
5690         {
5691           offset += pos / BITS_PER_UNIT;
5692           pos %= GET_MODE_BITSIZE (wanted_inner_mode);
5693         }
5694
5695       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5696           && ! spans_byte
5697           && is_mode != wanted_inner_mode)
5698         offset = (GET_MODE_SIZE (is_mode)
5699                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
5700
5701       if (offset != 0 || inner_mode != wanted_inner_mode)
5702         {
5703           rtx newmem = gen_rtx_MEM (wanted_inner_mode,
5704                                     plus_constant (XEXP (inner, 0), offset));
5705           RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
5706           MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (inner);
5707           MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (inner);
5708           inner = newmem;
5709         }
5710     }
5711
5712   /* If INNER is not memory, we can always get it into the proper mode.  If we
5713      are changing its mode, POS must be a constant and smaller than the size
5714      of the new mode.  */
5715   else if (GET_CODE (inner) != MEM)
5716     {
5717       if (GET_MODE (inner) != wanted_inner_mode
5718           && (pos_rtx != 0
5719               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
5720         return 0;
5721
5722       inner = force_to_mode (inner, wanted_inner_mode,
5723                              pos_rtx
5724                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
5725                              ? GET_MODE_MASK (wanted_inner_mode)
5726                              : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
5727                              NULL_RTX, 0);
5728     }
5729
5730   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
5731      have to zero extend.  Otherwise, we can just use a SUBREG.  */
5732   if (pos_rtx != 0
5733       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5734     pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
5735   else if (pos_rtx != 0
5736            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5737     pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5738
5739   /* Make POS_RTX unless we already have it and it is correct.  If we don't
5740      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
5741      be a CONST_INT.  */
5742   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5743     pos_rtx = orig_pos_rtx;
5744
5745   else if (pos_rtx == 0)
5746     pos_rtx = GEN_INT (pos);
5747
5748   /* Make the required operation.  See if we can use existing rtx.  */
5749   new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5750                          extraction_mode, inner, GEN_INT (len), pos_rtx);
5751   if (! in_dest)
5752     new = gen_lowpart_for_combine (mode, new);
5753
5754   return new;
5755 }
5756 \f
5757 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
5758    with any other operations in X.  Return X without that shift if so.  */
5759
5760 static rtx
5761 extract_left_shift (x, count)
5762      rtx x;
5763      int count;
5764 {
5765   enum rtx_code code = GET_CODE (x);
5766   enum machine_mode mode = GET_MODE (x);
5767   rtx tem;
5768
5769   switch (code)
5770     {
5771     case ASHIFT:
5772       /* This is the shift itself.  If it is wide enough, we will return
5773          either the value being shifted if the shift count is equal to
5774          COUNT or a shift for the difference.  */
5775       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5776           && INTVAL (XEXP (x, 1)) >= count)
5777         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
5778                                      INTVAL (XEXP (x, 1)) - count);
5779       break;
5780
5781     case NEG:  case NOT:
5782       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5783         return gen_unary (code, mode, mode, tem);
5784
5785       break;
5786
5787     case PLUS:  case IOR:  case XOR:  case AND:
5788       /* If we can safely shift this constant and we find the inner shift,
5789          make a new operation.  */
5790       if (GET_CODE (XEXP (x,1)) == CONST_INT
5791           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
5792           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5793         return gen_binary (code, mode, tem, 
5794                            GEN_INT (INTVAL (XEXP (x, 1)) >> count));
5795
5796       break;
5797       
5798     default:
5799       break;
5800     }
5801
5802   return 0;
5803 }
5804 \f
5805 /* Look at the expression rooted at X.  Look for expressions
5806    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
5807    Form these expressions.
5808
5809    Return the new rtx, usually just X.
5810
5811    Also, for machines like the Vax that don't have logical shift insns,
5812    try to convert logical to arithmetic shift operations in cases where
5813    they are equivalent.  This undoes the canonicalizations to logical
5814    shifts done elsewhere.
5815
5816    We try, as much as possible, to re-use rtl expressions to save memory.
5817
5818    IN_CODE says what kind of expression we are processing.  Normally, it is
5819    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
5820    being kludges), it is MEM.  When processing the arguments of a comparison
5821    or a COMPARE against zero, it is COMPARE.  */
5822
5823 static rtx
5824 make_compound_operation (x, in_code)
5825      rtx x;
5826      enum rtx_code in_code;
5827 {
5828   enum rtx_code code = GET_CODE (x);
5829   enum machine_mode mode = GET_MODE (x);
5830   int mode_width = GET_MODE_BITSIZE (mode);
5831   rtx rhs, lhs;
5832   enum rtx_code next_code;
5833   int i;
5834   rtx new = 0;
5835   rtx tem;
5836   char *fmt;
5837
5838   /* Select the code to be used in recursive calls.  Once we are inside an
5839      address, we stay there.  If we have a comparison, set to COMPARE,
5840      but once inside, go back to our default of SET.  */
5841
5842   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
5843                : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
5844                   && XEXP (x, 1) == const0_rtx) ? COMPARE
5845                : in_code == COMPARE ? SET : in_code);
5846
5847   /* Process depending on the code of this operation.  If NEW is set
5848      non-zero, it will be returned.  */
5849
5850   switch (code)
5851     {
5852     case ASHIFT:
5853       /* Convert shifts by constants into multiplications if inside
5854          an address.  */
5855       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
5856           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
5857           && INTVAL (XEXP (x, 1)) >= 0)
5858         {
5859           new = make_compound_operation (XEXP (x, 0), next_code);
5860           new = gen_rtx_combine (MULT, mode, new,
5861                                  GEN_INT ((HOST_WIDE_INT) 1
5862                                           << INTVAL (XEXP (x, 1))));
5863         }
5864       break;
5865
5866     case AND:
5867       /* If the second operand is not a constant, we can't do anything
5868          with it.  */
5869       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5870         break;
5871
5872       /* If the constant is a power of two minus one and the first operand
5873          is a logical right shift, make an extraction.  */
5874       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
5875           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5876         {
5877           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5878           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
5879                                  0, in_code == COMPARE);
5880         }
5881
5882       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
5883       else if (GET_CODE (XEXP (x, 0)) == SUBREG
5884                && subreg_lowpart_p (XEXP (x, 0))
5885                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
5886                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5887         {
5888           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
5889                                          next_code);
5890           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
5891                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
5892                                  0, in_code == COMPARE);
5893         }
5894       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
5895       else if ((GET_CODE (XEXP (x, 0)) == XOR
5896                 || GET_CODE (XEXP (x, 0)) == IOR)
5897                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
5898                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
5899                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5900         {
5901           /* Apply the distributive law, and then try to make extractions.  */
5902           new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
5903                                  gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
5904                                               XEXP (x, 1)),
5905                                  gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
5906                                               XEXP (x, 1)));
5907           new = make_compound_operation (new, in_code);
5908         }
5909
5910       /* If we are have (and (rotate X C) M) and C is larger than the number
5911          of bits in M, this is an extraction.  */
5912
5913       else if (GET_CODE (XEXP (x, 0)) == ROTATE
5914                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5915                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
5916                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
5917         {
5918           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5919           new = make_extraction (mode, new,
5920                                  (GET_MODE_BITSIZE (mode)
5921                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
5922                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
5923         }
5924
5925       /* On machines without logical shifts, if the operand of the AND is
5926          a logical shift and our mask turns off all the propagated sign
5927          bits, we can replace the logical shift with an arithmetic shift.  */
5928       else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5929                && (lshr_optab->handlers[(int) mode].insn_code
5930                    == CODE_FOR_nothing)
5931                && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5932                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5933                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
5934                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
5935                && mode_width <= HOST_BITS_PER_WIDE_INT)
5936         {
5937           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
5938
5939           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
5940           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
5941             SUBST (XEXP (x, 0),
5942                    gen_rtx_combine (ASHIFTRT, mode,
5943                                     make_compound_operation (XEXP (XEXP (x, 0), 0),
5944                                                              next_code),
5945                                     XEXP (XEXP (x, 0), 1)));
5946         }
5947
5948       /* If the constant is one less than a power of two, this might be
5949          representable by an extraction even if no shift is present.
5950          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
5951          we are in a COMPARE.  */
5952       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5953         new = make_extraction (mode,
5954                                make_compound_operation (XEXP (x, 0),
5955                                                         next_code),
5956                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
5957
5958       /* If we are in a comparison and this is an AND with a power of two,
5959          convert this into the appropriate bit extract.  */
5960       else if (in_code == COMPARE
5961                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
5962         new = make_extraction (mode,
5963                                make_compound_operation (XEXP (x, 0),
5964                                                         next_code),
5965                                i, NULL_RTX, 1, 1, 0, 1);
5966
5967       break;
5968
5969     case LSHIFTRT:
5970       /* If the sign bit is known to be zero, replace this with an
5971          arithmetic shift.  */
5972       if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
5973           && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5974           && mode_width <= HOST_BITS_PER_WIDE_INT
5975           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
5976         {
5977           new = gen_rtx_combine (ASHIFTRT, mode,
5978                                  make_compound_operation (XEXP (x, 0),
5979                                                           next_code),
5980                                  XEXP (x, 1));
5981           break;
5982         }
5983
5984       /* ... fall through ...  */
5985
5986     case ASHIFTRT:
5987       lhs = XEXP (x, 0);
5988       rhs = XEXP (x, 1);
5989
5990       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
5991          this is a SIGN_EXTRACT.  */
5992       if (GET_CODE (rhs) == CONST_INT
5993           && GET_CODE (lhs) == ASHIFT
5994           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
5995           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
5996         {
5997           new = make_compound_operation (XEXP (lhs, 0), next_code);
5998           new = make_extraction (mode, new,
5999                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6000                                  NULL_RTX, mode_width - INTVAL (rhs),
6001                                  code == LSHIFTRT, 0, in_code == COMPARE);
6002         }
6003
6004       /* See if we have operations between an ASHIFTRT and an ASHIFT.
6005          If so, try to merge the shifts into a SIGN_EXTEND.  We could
6006          also do this for some cases of SIGN_EXTRACT, but it doesn't
6007          seem worth the effort; the case checked for occurs on Alpha.  */
6008       
6009       if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6010           && ! (GET_CODE (lhs) == SUBREG
6011                 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6012           && GET_CODE (rhs) == CONST_INT
6013           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6014           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6015         new = make_extraction (mode, make_compound_operation (new, next_code),
6016                                0, NULL_RTX, mode_width - INTVAL (rhs),
6017                                code == LSHIFTRT, 0, in_code == COMPARE);
6018         
6019       break;
6020
6021     case SUBREG:
6022       /* Call ourselves recursively on the inner expression.  If we are
6023          narrowing the object and it has a different RTL code from
6024          what it originally did, do this SUBREG as a force_to_mode.  */
6025
6026       tem = make_compound_operation (SUBREG_REG (x), in_code);
6027       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6028           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6029           && subreg_lowpart_p (x))
6030         {
6031           rtx newer = force_to_mode (tem, mode,
6032                                      GET_MODE_MASK (mode), NULL_RTX, 0);
6033
6034           /* If we have something other than a SUBREG, we might have
6035              done an expansion, so rerun outselves.  */
6036           if (GET_CODE (newer) != SUBREG)
6037             newer = make_compound_operation (newer, in_code);
6038
6039           return newer;
6040         }
6041
6042       /* If this is a paradoxical subreg, and the new code is a sign or
6043          zero extension, omit the subreg and widen the extension.  If it
6044          is a regular subreg, we can still get rid of the subreg by not
6045          widening so much, or in fact removing the extension entirely.  */
6046       if ((GET_CODE (tem) == SIGN_EXTEND
6047            || GET_CODE (tem) == ZERO_EXTEND)
6048           && subreg_lowpart_p (x))
6049         {
6050           if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6051               || (GET_MODE_SIZE (mode) >
6052                   GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6053             tem = gen_rtx_combine (GET_CODE (tem), mode, XEXP (tem, 0));
6054           else
6055             tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6056           return tem;
6057         }
6058       break;
6059       
6060     default:
6061       break;
6062     }
6063
6064   if (new)
6065     {
6066       x = gen_lowpart_for_combine (mode, new);
6067       code = GET_CODE (x);
6068     }
6069
6070   /* Now recursively process each operand of this operation.  */
6071   fmt = GET_RTX_FORMAT (code);
6072   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6073     if (fmt[i] == 'e')
6074       {
6075         new = make_compound_operation (XEXP (x, i), next_code);
6076         SUBST (XEXP (x, i), new);
6077       }
6078
6079   return x;
6080 }
6081 \f
6082 /* Given M see if it is a value that would select a field of bits
6083     within an item, but not the entire word.  Return -1 if not.
6084     Otherwise, return the starting position of the field, where 0 is the
6085     low-order bit.
6086
6087    *PLEN is set to the length of the field.  */
6088
6089 static int
6090 get_pos_from_mask (m, plen)
6091      unsigned HOST_WIDE_INT m;
6092      int *plen;
6093 {
6094   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
6095   int pos = exact_log2 (m & - m);
6096
6097   if (pos < 0)
6098     return -1;
6099
6100   /* Now shift off the low-order zero bits and see if we have a power of
6101      two minus 1.  */
6102   *plen = exact_log2 ((m >> pos) + 1);
6103
6104   if (*plen <= 0)
6105     return -1;
6106
6107   return pos;
6108 }
6109 \f
6110 /* See if X can be simplified knowing that we will only refer to it in
6111    MODE and will only refer to those bits that are nonzero in MASK.
6112    If other bits are being computed or if masking operations are done
6113    that select a superset of the bits in MASK, they can sometimes be
6114    ignored.
6115
6116    Return a possibly simplified expression, but always convert X to
6117    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
6118
6119    Also, if REG is non-zero and X is a register equal in value to REG, 
6120    replace X with REG.
6121
6122    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6123    are all off in X.  This is used when X will be complemented, by either
6124    NOT, NEG, or XOR.  */
6125
6126 static rtx
6127 force_to_mode (x, mode, mask, reg, just_select)
6128      rtx x;
6129      enum machine_mode mode;
6130      unsigned HOST_WIDE_INT mask;
6131      rtx reg;
6132      int just_select;
6133 {
6134   enum rtx_code code = GET_CODE (x);
6135   int next_select = just_select || code == XOR || code == NOT || code == NEG;
6136   enum machine_mode op_mode;
6137   unsigned HOST_WIDE_INT fuller_mask, nonzero;
6138   rtx op0, op1, temp;
6139
6140   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
6141      code below will do the wrong thing since the mode of such an
6142      expression is VOIDmode. 
6143
6144      Also do nothing if X is a CLOBBER; this can happen if X was
6145      the return value from a call to gen_lowpart_for_combine.  */
6146   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6147     return x;
6148
6149   /* We want to perform the operation is its present mode unless we know
6150      that the operation is valid in MODE, in which case we do the operation
6151      in MODE.  */
6152   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6153               && code_to_optab[(int) code] != 0
6154               && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
6155                   != CODE_FOR_nothing))
6156              ? mode : GET_MODE (x));
6157
6158   /* It is not valid to do a right-shift in a narrower mode
6159      than the one it came in with.  */
6160   if ((code == LSHIFTRT || code == ASHIFTRT)
6161       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6162     op_mode = GET_MODE (x);
6163
6164   /* Truncate MASK to fit OP_MODE.  */
6165   if (op_mode)
6166     mask &= GET_MODE_MASK (op_mode);
6167
6168   /* When we have an arithmetic operation, or a shift whose count we
6169      do not know, we need to assume that all bit the up to the highest-order
6170      bit in MASK will be needed.  This is how we form such a mask.  */
6171   if (op_mode)
6172     fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6173                    ? GET_MODE_MASK (op_mode)
6174                    : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
6175   else
6176     fuller_mask = ~ (HOST_WIDE_INT) 0;
6177
6178   /* Determine what bits of X are guaranteed to be (non)zero.  */
6179   nonzero = nonzero_bits (x, mode);
6180
6181   /* If none of the bits in X are needed, return a zero.  */
6182   if (! just_select && (nonzero & mask) == 0)
6183     return const0_rtx;
6184
6185   /* If X is a CONST_INT, return a new one.  Do this here since the
6186      test below will fail.  */
6187   if (GET_CODE (x) == CONST_INT)
6188     {
6189       HOST_WIDE_INT cval = INTVAL (x) & mask;
6190       int width = GET_MODE_BITSIZE (mode);
6191
6192       /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6193          number, sign extend it.  */
6194       if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6195           && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6196         cval |= (HOST_WIDE_INT) -1 << width;
6197         
6198       return GEN_INT (cval);
6199     }
6200
6201   /* If X is narrower than MODE and we want all the bits in X's mode, just
6202      get X in the proper mode.  */
6203   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6204       && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
6205     return gen_lowpart_for_combine (mode, x);
6206
6207   /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6208      MASK are already known to be zero in X, we need not do anything.  */
6209   if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
6210     return x;
6211
6212   switch (code)
6213     {
6214     case CLOBBER:
6215       /* If X is a (clobber (const_int)), return it since we know we are
6216          generating something that won't match.  */
6217       return x;
6218
6219     case USE:
6220       /* X is a (use (mem ..)) that was made from a bit-field extraction that
6221          spanned the boundary of the MEM.  If we are now masking so it is
6222          within that boundary, we don't need the USE any more.  */
6223       if (! BITS_BIG_ENDIAN
6224           && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6225         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6226       break;
6227
6228     case SIGN_EXTEND:
6229     case ZERO_EXTEND:
6230     case ZERO_EXTRACT:
6231     case SIGN_EXTRACT:
6232       x = expand_compound_operation (x);
6233       if (GET_CODE (x) != code)
6234         return force_to_mode (x, mode, mask, reg, next_select);
6235       break;
6236
6237     case REG:
6238       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6239                        || rtx_equal_p (reg, get_last_value (x))))
6240         x = reg;
6241       break;
6242
6243     case SUBREG:
6244       if (subreg_lowpart_p (x)
6245           /* We can ignore the effect of this SUBREG if it narrows the mode or
6246              if the constant masks to zero all the bits the mode doesn't
6247              have.  */
6248           && ((GET_MODE_SIZE (GET_MODE (x))
6249                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6250               || (0 == (mask
6251                         & GET_MODE_MASK (GET_MODE (x))
6252                         & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6253         return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6254       break;
6255
6256     case AND:
6257       /* If this is an AND with a constant, convert it into an AND
6258          whose constant is the AND of that constant with MASK.  If it
6259          remains an AND of MASK, delete it since it is redundant.  */
6260
6261       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6262         {
6263           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6264                                       mask & INTVAL (XEXP (x, 1)));
6265
6266           /* If X is still an AND, see if it is an AND with a mask that
6267              is just some low-order bits.  If so, and it is MASK, we don't
6268              need it.  */
6269
6270           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6271               && INTVAL (XEXP (x, 1)) == mask)
6272             x = XEXP (x, 0);
6273
6274           /* If it remains an AND, try making another AND with the bits
6275              in the mode mask that aren't in MASK turned on.  If the
6276              constant in the AND is wide enough, this might make a
6277              cheaper constant.  */
6278
6279           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6280               && GET_MODE_MASK (GET_MODE (x)) != mask
6281               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6282             {
6283               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6284                                     | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
6285               int width = GET_MODE_BITSIZE (GET_MODE (x));
6286               rtx y;
6287
6288               /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6289                  number, sign extend it.  */
6290               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6291                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6292                 cval |= (HOST_WIDE_INT) -1 << width;
6293
6294               y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6295               if (rtx_cost (y, SET) < rtx_cost (x, SET))
6296                 x = y;
6297             }
6298
6299           break;
6300         }
6301
6302       goto binop;
6303
6304     case PLUS:
6305       /* In (and (plus FOO C1) M), if M is a mask that just turns off
6306          low-order bits (as in an alignment operation) and FOO is already
6307          aligned to that boundary, mask C1 to that boundary as well.
6308          This may eliminate that PLUS and, later, the AND.  */
6309
6310       {
6311         int width = GET_MODE_BITSIZE (mode);
6312         unsigned HOST_WIDE_INT smask = mask;
6313
6314         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6315            number, sign extend it.  */
6316
6317         if (width < HOST_BITS_PER_WIDE_INT
6318             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6319           smask |= (HOST_WIDE_INT) -1 << width;
6320
6321         if (GET_CODE (XEXP (x, 1)) == CONST_INT
6322             && exact_log2 (- smask) >= 0)
6323           {
6324 #ifdef STACK_BIAS
6325             if (STACK_BIAS
6326                 && (XEXP (x, 0) == stack_pointer_rtx
6327                     || XEXP (x, 0) == frame_pointer_rtx))
6328               {
6329                 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
6330                 unsigned HOST_WIDE_INT sp_mask = GET_MODE_MASK (mode);
6331           
6332                 sp_mask &= ~ (sp_alignment - 1);
6333                 if ((sp_mask & ~ mask) == 0
6334                     && ((INTVAL (XEXP (x, 1)) - STACK_BIAS) & ~ mask) != 0)
6335                   return force_to_mode (plus_constant (XEXP (x, 0),
6336                                                        ((INTVAL (XEXP (x, 1)) -
6337                                                          STACK_BIAS) & mask)
6338                                                        + STACK_BIAS),
6339                                         mode, mask, reg, next_select);
6340               }
6341 #endif
6342             if ((nonzero_bits (XEXP (x, 0), mode) & ~ mask) == 0
6343                 && (INTVAL (XEXP (x, 1)) & ~ mask) != 0)
6344               return force_to_mode (plus_constant (XEXP (x, 0),
6345                                                    INTVAL (XEXP (x, 1)) & mask),
6346                                     mode, mask, reg, next_select);
6347           }
6348       }
6349
6350       /* ... fall through ...  */
6351
6352     case MINUS:
6353     case MULT:
6354       /* For PLUS, MINUS and MULT, we need any bits less significant than the
6355          most significant bit in MASK since carries from those bits will
6356          affect the bits we are interested in.  */
6357       mask = fuller_mask;
6358       goto binop;
6359
6360     case IOR:
6361     case XOR:
6362       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6363          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6364          operation which may be a bitfield extraction.  Ensure that the
6365          constant we form is not wider than the mode of X.  */
6366
6367       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6368           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6369           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6370           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6371           && GET_CODE (XEXP (x, 1)) == CONST_INT
6372           && ((INTVAL (XEXP (XEXP (x, 0), 1))
6373                + floor_log2 (INTVAL (XEXP (x, 1))))
6374               < GET_MODE_BITSIZE (GET_MODE (x)))
6375           && (INTVAL (XEXP (x, 1))
6376               & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6377         {
6378           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6379                               << INTVAL (XEXP (XEXP (x, 0), 1)));
6380           temp = gen_binary (GET_CODE (x), GET_MODE (x),
6381                              XEXP (XEXP (x, 0), 0), temp);
6382           x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6383                           XEXP (XEXP (x, 0), 1));
6384           return force_to_mode (x, mode, mask, reg, next_select);
6385         }
6386
6387     binop:
6388       /* For most binary operations, just propagate into the operation and
6389          change the mode if we have an operation of that mode.   */
6390
6391       op0 = gen_lowpart_for_combine (op_mode,
6392                                      force_to_mode (XEXP (x, 0), mode, mask,
6393                                                     reg, next_select));
6394       op1 = gen_lowpart_for_combine (op_mode,
6395                                      force_to_mode (XEXP (x, 1), mode, mask,
6396                                                     reg, next_select));
6397
6398       /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6399          MASK since OP1 might have been sign-extended but we never want
6400          to turn on extra bits, since combine might have previously relied
6401          on them being off.  */
6402       if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6403           && (INTVAL (op1) & mask) != 0)
6404         op1 = GEN_INT (INTVAL (op1) & mask);
6405          
6406       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6407         x = gen_binary (code, op_mode, op0, op1);
6408       break;
6409
6410     case ASHIFT:
6411       /* For left shifts, do the same, but just for the first operand.
6412          However, we cannot do anything with shifts where we cannot
6413          guarantee that the counts are smaller than the size of the mode
6414          because such a count will have a different meaning in a
6415          wider mode.  */
6416
6417       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6418              && INTVAL (XEXP (x, 1)) >= 0
6419              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6420           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6421                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6422                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6423         break;
6424         
6425       /* If the shift count is a constant and we can do arithmetic in
6426          the mode of the shift, refine which bits we need.  Otherwise, use the
6427          conservative form of the mask.  */
6428       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6429           && INTVAL (XEXP (x, 1)) >= 0
6430           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6431           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6432         mask >>= INTVAL (XEXP (x, 1));
6433       else
6434         mask = fuller_mask;
6435
6436       op0 = gen_lowpart_for_combine (op_mode,
6437                                      force_to_mode (XEXP (x, 0), op_mode,
6438                                                     mask, reg, next_select));
6439
6440       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6441         x =  gen_binary (code, op_mode, op0, XEXP (x, 1));
6442       break;
6443
6444     case LSHIFTRT:
6445       /* Here we can only do something if the shift count is a constant,
6446          this shift constant is valid for the host, and we can do arithmetic
6447          in OP_MODE.  */
6448
6449       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6450           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6451           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6452         {
6453           rtx inner = XEXP (x, 0);
6454
6455           /* Select the mask of the bits we need for the shift operand.  */
6456           mask <<= INTVAL (XEXP (x, 1));
6457
6458           /* We can only change the mode of the shift if we can do arithmetic
6459              in the mode of the shift and MASK is no wider than the width of
6460              OP_MODE.  */
6461           if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6462               || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
6463             op_mode = GET_MODE (x);
6464
6465           inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6466
6467           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6468             x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
6469         }
6470
6471       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6472          shift and AND produces only copies of the sign bit (C2 is one less
6473          than a power of two), we can do this with just a shift.  */
6474
6475       if (GET_CODE (x) == LSHIFTRT
6476           && GET_CODE (XEXP (x, 1)) == CONST_INT
6477           && ((INTVAL (XEXP (x, 1))
6478                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6479               >= GET_MODE_BITSIZE (GET_MODE (x)))
6480           && exact_log2 (mask + 1) >= 0
6481           && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6482               >= exact_log2 (mask + 1)))
6483         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6484                         GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6485                                  - exact_log2 (mask + 1)));
6486       break;
6487
6488     case ASHIFTRT:
6489       /* If we are just looking for the sign bit, we don't need this shift at
6490          all, even if it has a variable count.  */
6491       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6492           && (mask == ((HOST_WIDE_INT) 1
6493                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
6494         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6495
6496       /* If this is a shift by a constant, get a mask that contains those bits
6497          that are not copies of the sign bit.  We then have two cases:  If
6498          MASK only includes those bits, this can be a logical shift, which may
6499          allow simplifications.  If MASK is a single-bit field not within
6500          those bits, we are requesting a copy of the sign bit and hence can
6501          shift the sign bit to the appropriate location.  */
6502
6503       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6504           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6505         {
6506           int i = -1;
6507
6508           /* If the considered data is wider then HOST_WIDE_INT, we can't
6509              represent a mask for all its bits in a single scalar.
6510              But we only care about the lower bits, so calculate these.  */
6511
6512           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
6513             {
6514               nonzero = ~ (HOST_WIDE_INT) 0;
6515
6516               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6517                  is the number of bits a full-width mask would have set.
6518                  We need only shift if these are fewer than nonzero can
6519                  hold.  If not, we must keep all bits set in nonzero.  */
6520
6521               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6522                   < HOST_BITS_PER_WIDE_INT)
6523                 nonzero >>= INTVAL (XEXP (x, 1))
6524                             + HOST_BITS_PER_WIDE_INT
6525                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
6526             }
6527           else
6528             {
6529               nonzero = GET_MODE_MASK (GET_MODE (x));
6530               nonzero >>= INTVAL (XEXP (x, 1));
6531             }
6532
6533           if ((mask & ~ nonzero) == 0
6534               || (i = exact_log2 (mask)) >= 0)
6535             {
6536               x = simplify_shift_const
6537                 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6538                  i < 0 ? INTVAL (XEXP (x, 1))
6539                  : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6540
6541               if (GET_CODE (x) != ASHIFTRT)
6542                 return force_to_mode (x, mode, mask, reg, next_select);
6543             }
6544         }
6545
6546       /* If MASK is 1, convert this to a LSHIFTRT.  This can be done
6547          even if the shift count isn't a constant.  */
6548       if (mask == 1)
6549         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6550
6551       /* If this is a sign-extension operation that just affects bits
6552          we don't care about, remove it.  Be sure the call above returned
6553          something that is still a shift.  */
6554
6555       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6556           && GET_CODE (XEXP (x, 1)) == CONST_INT
6557           && INTVAL (XEXP (x, 1)) >= 0
6558           && (INTVAL (XEXP (x, 1))
6559               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
6560           && GET_CODE (XEXP (x, 0)) == ASHIFT
6561           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6562           && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
6563         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6564                               reg, next_select);
6565
6566       break;
6567
6568     case ROTATE:
6569     case ROTATERT:
6570       /* If the shift count is constant and we can do computations
6571          in the mode of X, compute where the bits we care about are.
6572          Otherwise, we can't do anything.  Don't change the mode of
6573          the shift or propagate MODE into the shift, though.  */
6574       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6575           && INTVAL (XEXP (x, 1)) >= 0)
6576         {
6577           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6578                                             GET_MODE (x), GEN_INT (mask),
6579                                             XEXP (x, 1));
6580           if (temp && GET_CODE(temp) == CONST_INT)
6581             SUBST (XEXP (x, 0),
6582                    force_to_mode (XEXP (x, 0), GET_MODE (x),
6583                                   INTVAL (temp), reg, next_select));
6584         }
6585       break;
6586         
6587     case NEG:
6588       /* If we just want the low-order bit, the NEG isn't needed since it
6589          won't change the low-order bit.    */
6590       if (mask == 1)
6591         return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6592
6593       /* We need any bits less significant than the most significant bit in
6594          MASK since carries from those bits will affect the bits we are
6595          interested in.  */
6596       mask = fuller_mask;
6597       goto unop;
6598
6599     case NOT:
6600       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6601          same as the XOR case above.  Ensure that the constant we form is not
6602          wider than the mode of X.  */
6603
6604       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6605           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6606           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6607           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6608               < GET_MODE_BITSIZE (GET_MODE (x)))
6609           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6610         {
6611           temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6612           temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6613           x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6614
6615           return force_to_mode (x, mode, mask, reg, next_select);
6616         }
6617
6618       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6619          use the full mask inside the NOT.  */
6620       mask = fuller_mask;
6621
6622     unop:
6623       op0 = gen_lowpart_for_combine (op_mode,
6624                                      force_to_mode (XEXP (x, 0), mode, mask,
6625                                                     reg, next_select));
6626       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6627         x = gen_unary (code, op_mode, op_mode, op0);
6628       break;
6629
6630     case NE:
6631       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
6632          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
6633          which is equal to STORE_FLAG_VALUE.  */
6634       if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
6635           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
6636           && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
6637         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6638
6639       break;
6640
6641     case IF_THEN_ELSE:
6642       /* We have no way of knowing if the IF_THEN_ELSE can itself be
6643          written in a narrower mode.  We play it safe and do not do so.  */
6644
6645       SUBST (XEXP (x, 1),
6646              gen_lowpart_for_combine (GET_MODE (x),
6647                                       force_to_mode (XEXP (x, 1), mode,
6648                                                      mask, reg, next_select)));
6649       SUBST (XEXP (x, 2),
6650              gen_lowpart_for_combine (GET_MODE (x),
6651                                       force_to_mode (XEXP (x, 2), mode,
6652                                                      mask, reg,next_select)));
6653       break;
6654       
6655     default:
6656       break;
6657     }
6658
6659   /* Ensure we return a value of the proper mode.  */
6660   return gen_lowpart_for_combine (mode, x);
6661 }
6662 \f
6663 /* Return nonzero if X is an expression that has one of two values depending on
6664    whether some other value is zero or nonzero.  In that case, we return the
6665    value that is being tested, *PTRUE is set to the value if the rtx being
6666    returned has a nonzero value, and *PFALSE is set to the other alternative.
6667
6668    If we return zero, we set *PTRUE and *PFALSE to X.  */
6669
6670 static rtx
6671 if_then_else_cond (x, ptrue, pfalse)
6672      rtx x;
6673      rtx *ptrue, *pfalse;
6674 {
6675   enum machine_mode mode = GET_MODE (x);
6676   enum rtx_code code = GET_CODE (x);
6677   int size = GET_MODE_BITSIZE (mode);
6678   rtx cond0, cond1, true0, true1, false0, false1;
6679   unsigned HOST_WIDE_INT nz;
6680
6681   /* If this is a unary operation whose operand has one of two values, apply
6682      our opcode to compute those values.  */
6683   if (GET_RTX_CLASS (code) == '1'
6684       && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6685     {
6686       *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6687       *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
6688       return cond0;
6689     }
6690
6691   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
6692      make can't possibly match and would suppress other optimizations.  */
6693   else if (code == COMPARE)
6694     ;
6695
6696   /* If this is a binary operation, see if either side has only one of two
6697      values.  If either one does or if both do and they are conditional on
6698      the same value, compute the new true and false values.  */
6699   else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
6700            || GET_RTX_CLASS (code) == '<')
6701     {
6702       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
6703       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
6704
6705       if ((cond0 != 0 || cond1 != 0)
6706           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
6707         {
6708           /* If if_then_else_cond returned zero, then true/false are the
6709              same rtl.  We must copy one of them to prevent invalid rtl
6710              sharing.  */
6711           if (cond0 == 0)
6712             true0 = copy_rtx (true0);
6713           else if (cond1 == 0)
6714             true1 = copy_rtx (true1);
6715
6716           *ptrue = gen_binary (code, mode, true0, true1);
6717           *pfalse = gen_binary (code, mode, false0, false1);
6718           return cond0 ? cond0 : cond1;
6719         }
6720
6721       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
6722          operands is zero when the other is non-zero, and vice-versa,
6723          and STORE_FLAG_VALUE is 1 or -1.  */
6724
6725       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6726           && (code == PLUS || code == IOR || code == XOR || code == MINUS
6727            || code == UMAX)
6728           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6729         {
6730           rtx op0 = XEXP (XEXP (x, 0), 1);
6731           rtx op1 = XEXP (XEXP (x, 1), 1);
6732
6733           cond0 = XEXP (XEXP (x, 0), 0);
6734           cond1 = XEXP (XEXP (x, 1), 0);
6735
6736           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6737               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6738               && reversible_comparison_p (cond1)
6739               && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6740                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6741                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6742                   || ((swap_condition (GET_CODE (cond0))
6743                        == reverse_condition (GET_CODE (cond1)))
6744                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6745                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6746               && ! side_effects_p (x))
6747             {
6748               *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
6749               *pfalse = gen_binary (MULT, mode, 
6750                                     (code == MINUS 
6751                                      ? gen_unary (NEG, mode, mode, op1) : op1),
6752                                     const_true_rtx);
6753               return cond0;
6754             }
6755         }
6756
6757       /* Similarly for MULT, AND and UMIN, execpt that for these the result
6758          is always zero.  */
6759       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6760           && (code == MULT || code == AND || code == UMIN)
6761           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6762         {
6763           cond0 = XEXP (XEXP (x, 0), 0);
6764           cond1 = XEXP (XEXP (x, 1), 0);
6765
6766           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6767               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6768               && reversible_comparison_p (cond1)
6769               && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6770                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6771                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6772                   || ((swap_condition (GET_CODE (cond0))
6773                        == reverse_condition (GET_CODE (cond1)))
6774                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6775                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6776               && ! side_effects_p (x))
6777             {
6778               *ptrue = *pfalse = const0_rtx;
6779               return cond0;
6780             }
6781         }
6782     }
6783
6784   else if (code == IF_THEN_ELSE)
6785     {
6786       /* If we have IF_THEN_ELSE already, extract the condition and
6787          canonicalize it if it is NE or EQ.  */
6788       cond0 = XEXP (x, 0);
6789       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
6790       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
6791         return XEXP (cond0, 0);
6792       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
6793         {
6794           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
6795           return XEXP (cond0, 0);
6796         }
6797       else
6798         return cond0;
6799     }
6800
6801   /* If X is a normal SUBREG with both inner and outer modes integral,
6802      we can narrow both the true and false values of the inner expression,
6803      if there is a condition.  */
6804   else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
6805            && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
6806            && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
6807            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
6808                                                &true0, &false0)))
6809     {
6810       *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6811       *pfalse
6812         = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6813
6814       return cond0;
6815     }
6816
6817   /* If X is a constant, this isn't special and will cause confusions
6818      if we treat it as such.  Likewise if it is equivalent to a constant.  */
6819   else if (CONSTANT_P (x)
6820            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
6821     ;
6822
6823   /* If X is known to be either 0 or -1, those are the true and 
6824      false values when testing X.  */
6825   else if (num_sign_bit_copies (x, mode) == size)
6826     {
6827       *ptrue = constm1_rtx, *pfalse = const0_rtx;
6828       return x;
6829     }
6830
6831   /* Likewise for 0 or a single bit.  */
6832   else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
6833     {
6834       *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
6835       return x;
6836     }
6837
6838   /* Otherwise fail; show no condition with true and false values the same.  */
6839   *ptrue = *pfalse = x;
6840   return 0;
6841 }
6842 \f
6843 /* Return the value of expression X given the fact that condition COND
6844    is known to be true when applied to REG as its first operand and VAL
6845    as its second.  X is known to not be shared and so can be modified in
6846    place.
6847
6848    We only handle the simplest cases, and specifically those cases that
6849    arise with IF_THEN_ELSE expressions.  */
6850
6851 static rtx
6852 known_cond (x, cond, reg, val)
6853      rtx x;
6854      enum rtx_code cond;
6855      rtx reg, val;
6856 {
6857   enum rtx_code code = GET_CODE (x);
6858   rtx temp;
6859   char *fmt;
6860   int i, j;
6861
6862   if (side_effects_p (x))
6863     return x;
6864
6865   if (cond == EQ && rtx_equal_p (x, reg))
6866     return val;
6867
6868   /* If X is (abs REG) and we know something about REG's relationship
6869      with zero, we may be able to simplify this.  */
6870
6871   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
6872     switch (cond)
6873       {
6874       case GE:  case GT:  case EQ:
6875         return XEXP (x, 0);
6876       case LT:  case LE:
6877         return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
6878                           XEXP (x, 0));
6879       default:
6880         break;
6881       }
6882
6883   /* The only other cases we handle are MIN, MAX, and comparisons if the
6884      operands are the same as REG and VAL.  */
6885
6886   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
6887     {
6888       if (rtx_equal_p (XEXP (x, 0), val))
6889         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
6890
6891       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
6892         {
6893           if (GET_RTX_CLASS (code) == '<')
6894             return (comparison_dominates_p (cond, code) ? const_true_rtx
6895                     : (comparison_dominates_p (cond,
6896                                                reverse_condition (code))
6897                        ? const0_rtx : x));
6898
6899           else if (code == SMAX || code == SMIN
6900                    || code == UMIN || code == UMAX)
6901             {
6902               int unsignedp = (code == UMIN || code == UMAX);
6903
6904               if (code == SMAX || code == UMAX)
6905                 cond = reverse_condition (cond);
6906
6907               switch (cond)
6908                 {
6909                 case GE:   case GT:
6910                   return unsignedp ? x : XEXP (x, 1);
6911                 case LE:   case LT:
6912                   return unsignedp ? x : XEXP (x, 0);
6913                 case GEU:  case GTU:
6914                   return unsignedp ? XEXP (x, 1) : x;
6915                 case LEU:  case LTU:
6916                   return unsignedp ? XEXP (x, 0) : x;
6917                 default:
6918                   break;
6919                 }
6920             }
6921         }
6922     }
6923
6924   fmt = GET_RTX_FORMAT (code);
6925   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6926     {
6927       if (fmt[i] == 'e')
6928         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
6929       else if (fmt[i] == 'E')
6930         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6931           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
6932                                                 cond, reg, val));
6933     }
6934
6935   return x;
6936 }
6937 \f
6938 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
6939    assignment as a field assignment.  */
6940
6941 static int
6942 rtx_equal_for_field_assignment_p (x, y)
6943      rtx x;
6944      rtx y;
6945 {
6946   rtx last_x, last_y;
6947
6948   if (x == y || rtx_equal_p (x, y))
6949     return 1;
6950
6951   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
6952     return 0;
6953
6954   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
6955      Note that all SUBREGs of MEM are paradoxical; otherwise they
6956      would have been rewritten.  */
6957   if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
6958       && GET_CODE (SUBREG_REG (y)) == MEM
6959       && rtx_equal_p (SUBREG_REG (y),
6960                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
6961     return 1;
6962
6963   if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
6964       && GET_CODE (SUBREG_REG (x)) == MEM
6965       && rtx_equal_p (SUBREG_REG (x),
6966                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
6967     return 1;
6968
6969   last_x = get_last_value (x);
6970   last_y = get_last_value (y);
6971
6972   return ((last_x != 0
6973            && GET_CODE (last_x) != CLOBBER
6974            && rtx_equal_for_field_assignment_p (last_x, y))
6975           || (last_y != 0
6976               && GET_CODE (last_y) != CLOBBER
6977               && rtx_equal_for_field_assignment_p (x, last_y))
6978           || (last_x != 0 && last_y != 0
6979               && GET_CODE (last_x) != CLOBBER
6980               && GET_CODE (last_y) != CLOBBER
6981               && rtx_equal_for_field_assignment_p (last_x, last_y)));
6982 }
6983 \f
6984 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
6985    Return that assignment if so.
6986
6987    We only handle the most common cases.  */
6988
6989 static rtx
6990 make_field_assignment (x)
6991      rtx x;
6992 {
6993   rtx dest = SET_DEST (x);
6994   rtx src = SET_SRC (x);
6995   rtx assign;
6996   rtx rhs, lhs;
6997   HOST_WIDE_INT c1;
6998   int pos, len;
6999   rtx other;
7000   enum machine_mode mode;
7001
7002   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7003      a clear of a one-bit field.  We will have changed it to
7004      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
7005      for a SUBREG.  */
7006
7007   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7008       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7009       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7010       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7011     {
7012       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7013                                 1, 1, 1, 0);
7014       if (assign != 0)
7015         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7016       return x;
7017     }
7018
7019   else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7020            && subreg_lowpart_p (XEXP (src, 0))
7021            && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0))) 
7022                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7023            && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7024            && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7025            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7026     {
7027       assign = make_extraction (VOIDmode, dest, 0,
7028                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7029                                 1, 1, 1, 0);
7030       if (assign != 0)
7031         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7032       return x;
7033     }
7034
7035   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7036      one-bit field.  */
7037   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7038            && XEXP (XEXP (src, 0), 0) == const1_rtx
7039            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7040     {
7041       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7042                                 1, 1, 1, 0);
7043       if (assign != 0)
7044         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7045       return x;
7046     }
7047
7048   /* The other case we handle is assignments into a constant-position
7049      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
7050      a mask that has all one bits except for a group of zero bits and
7051      OTHER is known to have zeros where C1 has ones, this is such an
7052      assignment.  Compute the position and length from C1.  Shift OTHER
7053      to the appropriate position, force it to the required mode, and
7054      make the extraction.  Check for the AND in both operands.  */
7055
7056   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7057     return x;
7058
7059   rhs = expand_compound_operation (XEXP (src, 0));
7060   lhs = expand_compound_operation (XEXP (src, 1));
7061
7062   if (GET_CODE (rhs) == AND
7063       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7064       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7065     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7066   else if (GET_CODE (lhs) == AND
7067            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7068            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7069     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7070   else
7071     return x;
7072
7073   pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7074   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7075       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7076       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7077     return x;
7078
7079   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7080   if (assign == 0)
7081     return x;
7082
7083   /* The mode to use for the source is the mode of the assignment, or of
7084      what is inside a possible STRICT_LOW_PART.  */
7085   mode = (GET_CODE (assign) == STRICT_LOW_PART 
7086           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7087
7088   /* Shift OTHER right POS places and make it the source, restricting it
7089      to the proper length and mode.  */
7090
7091   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7092                                              GET_MODE (src), other, pos),
7093                        mode,
7094                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7095                        ? GET_MODE_MASK (mode)
7096                        : ((HOST_WIDE_INT) 1 << len) - 1,
7097                        dest, 0);
7098
7099   return gen_rtx_combine (SET, VOIDmode, assign, src);
7100 }
7101 \f
7102 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7103    if so.  */
7104
7105 static rtx
7106 apply_distributive_law (x)
7107      rtx x;
7108 {
7109   enum rtx_code code = GET_CODE (x);
7110   rtx lhs, rhs, other;
7111   rtx tem;
7112   enum rtx_code inner_code;
7113
7114   /* Distributivity is not true for floating point.
7115      It can change the value.  So don't do it.
7116      -- rms and moshier@world.std.com.  */
7117   if (FLOAT_MODE_P (GET_MODE (x)))
7118     return x;
7119
7120   /* The outer operation can only be one of the following:  */
7121   if (code != IOR && code != AND && code != XOR
7122       && code != PLUS && code != MINUS)
7123     return x;
7124
7125   lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7126
7127   /* If either operand is a primitive we can't do anything, so get out
7128      fast.  */
7129   if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7130       || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7131     return x;
7132
7133   lhs = expand_compound_operation (lhs);
7134   rhs = expand_compound_operation (rhs);
7135   inner_code = GET_CODE (lhs);
7136   if (inner_code != GET_CODE (rhs))
7137     return x;
7138
7139   /* See if the inner and outer operations distribute.  */
7140   switch (inner_code)
7141     {
7142     case LSHIFTRT:
7143     case ASHIFTRT:
7144     case AND:
7145     case IOR:
7146       /* These all distribute except over PLUS.  */
7147       if (code == PLUS || code == MINUS)
7148         return x;
7149       break;
7150
7151     case MULT:
7152       if (code != PLUS && code != MINUS)
7153         return x;
7154       break;
7155
7156     case ASHIFT:
7157       /* This is also a multiply, so it distributes over everything.  */
7158       break;
7159
7160     case SUBREG:
7161       /* Non-paradoxical SUBREGs distributes over all operations, provided
7162          the inner modes and word numbers are the same, this is an extraction
7163          of a low-order part, we don't convert an fp operation to int or
7164          vice versa, and we would not be converting a single-word
7165          operation into a multi-word operation.  The latter test is not
7166          required, but it prevents generating unneeded multi-word operations.
7167          Some of the previous tests are redundant given the latter test, but
7168          are retained because they are required for correctness.
7169
7170          We produce the result slightly differently in this case.  */
7171
7172       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7173           || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
7174           || ! subreg_lowpart_p (lhs)
7175           || (GET_MODE_CLASS (GET_MODE (lhs))
7176               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7177           || (GET_MODE_SIZE (GET_MODE (lhs))
7178               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7179           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7180         return x;
7181
7182       tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7183                         SUBREG_REG (lhs), SUBREG_REG (rhs));
7184       return gen_lowpart_for_combine (GET_MODE (x), tem);
7185
7186     default:
7187       return x;
7188     }
7189
7190   /* Set LHS and RHS to the inner operands (A and B in the example
7191      above) and set OTHER to the common operand (C in the example).
7192      These is only one way to do this unless the inner operation is
7193      commutative.  */
7194   if (GET_RTX_CLASS (inner_code) == 'c'
7195       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7196     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7197   else if (GET_RTX_CLASS (inner_code) == 'c'
7198            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7199     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7200   else if (GET_RTX_CLASS (inner_code) == 'c'
7201            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7202     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7203   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7204     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7205   else
7206     return x;
7207
7208   /* Form the new inner operation, seeing if it simplifies first.  */
7209   tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7210
7211   /* There is one exception to the general way of distributing:
7212      (a ^ b) | (a ^ c) -> (~a) & (b ^ c)  */
7213   if (code == XOR && inner_code == IOR)
7214     {
7215       inner_code = AND;
7216       other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
7217     }
7218
7219   /* We may be able to continuing distributing the result, so call
7220      ourselves recursively on the inner operation before forming the
7221      outer operation, which we return.  */
7222   return gen_binary (inner_code, GET_MODE (x),
7223                      apply_distributive_law (tem), other);
7224 }
7225 \f
7226 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7227    in MODE.
7228
7229    Return an equivalent form, if different from X.  Otherwise, return X.  If
7230    X is zero, we are to always construct the equivalent form.  */
7231
7232 static rtx
7233 simplify_and_const_int (x, mode, varop, constop)
7234      rtx x;
7235      enum machine_mode mode;
7236      rtx varop;
7237      unsigned HOST_WIDE_INT constop;
7238 {
7239   unsigned HOST_WIDE_INT nonzero;
7240   int width = GET_MODE_BITSIZE (mode);
7241   int i;
7242
7243   /* Simplify VAROP knowing that we will be only looking at some of the
7244      bits in it.  */
7245   varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7246
7247   /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7248      CONST_INT, we are done.  */
7249   if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7250     return varop;
7251
7252   /* See what bits may be nonzero in VAROP.  Unlike the general case of
7253      a call to nonzero_bits, here we don't care about bits outside
7254      MODE.  */
7255
7256   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7257
7258   /* If this would be an entire word for the target, but is not for
7259      the host, then sign-extend on the host so that the number will look
7260      the same way on the host that it would on the target.
7261
7262      For example, when building a 64 bit alpha hosted 32 bit sparc
7263      targeted compiler, then we want the 32 bit unsigned value -1 to be
7264      represented as a 64 bit value -1, and not as 0x00000000ffffffff.
7265      The later confuses the sparc backend.  */
7266
7267   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
7268       && (nonzero & ((HOST_WIDE_INT) 1 << (width - 1))))
7269     nonzero |= ((HOST_WIDE_INT) (-1) << width);
7270
7271   /* Turn off all bits in the constant that are known to already be zero.
7272      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7273      which is tested below.  */
7274
7275   constop &= nonzero;
7276
7277   /* If we don't have any bits left, return zero.  */
7278   if (constop == 0)
7279     return const0_rtx;
7280
7281   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7282      a power of two, we can replace this with a ASHIFT.  */
7283   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7284       && (i = exact_log2 (constop)) >= 0)
7285     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7286                                  
7287   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7288      or XOR, then try to apply the distributive law.  This may eliminate
7289      operations if either branch can be simplified because of the AND.
7290      It may also make some cases more complex, but those cases probably
7291      won't match a pattern either with or without this.  */
7292
7293   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7294     return
7295       gen_lowpart_for_combine
7296         (mode,
7297          apply_distributive_law
7298          (gen_binary (GET_CODE (varop), GET_MODE (varop),
7299                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7300                                               XEXP (varop, 0), constop),
7301                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7302                                               XEXP (varop, 1), constop))));
7303
7304   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
7305      if we already had one (just check for the simplest cases).  */
7306   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7307       && GET_MODE (XEXP (x, 0)) == mode
7308       && SUBREG_REG (XEXP (x, 0)) == varop)
7309     varop = XEXP (x, 0);
7310   else
7311     varop = gen_lowpart_for_combine (mode, varop);
7312
7313   /* If we can't make the SUBREG, try to return what we were given.  */
7314   if (GET_CODE (varop) == CLOBBER)
7315     return x ? x : varop;
7316
7317   /* If we are only masking insignificant bits, return VAROP.  */
7318   if (constop == nonzero)
7319     x = varop;
7320
7321   /* Otherwise, return an AND.  See how much, if any, of X we can use.  */
7322   else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
7323     x = gen_binary (AND, mode, varop, GEN_INT (constop));
7324
7325   else
7326     {
7327       if (GET_CODE (XEXP (x, 1)) != CONST_INT
7328           || INTVAL (XEXP (x, 1)) != constop)
7329         SUBST (XEXP (x, 1), GEN_INT (constop));
7330
7331       SUBST (XEXP (x, 0), varop);
7332     }
7333
7334   return x;
7335 }
7336 \f
7337 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7338    We don't let nonzero_bits recur into num_sign_bit_copies, because that
7339    is less useful.  We can't allow both, because that results in exponential
7340    run time recursion.  There is a nullstone testcase that triggered
7341    this.  This macro avoids accidental uses of num_sign_bit_copies.  */
7342 #define num_sign_bit_copies()
7343
7344 /* Given an expression, X, compute which bits in X can be non-zero.
7345    We don't care about bits outside of those defined in MODE.
7346
7347    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7348    a shift, AND, or zero_extract, we can do better.  */
7349
7350 static unsigned HOST_WIDE_INT
7351 nonzero_bits (x, mode)
7352      rtx x;
7353      enum machine_mode mode;
7354 {
7355   unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7356   unsigned HOST_WIDE_INT inner_nz;
7357   enum rtx_code code;
7358   int mode_width = GET_MODE_BITSIZE (mode);
7359   rtx tem;
7360
7361   /* For floating-point values, assume all bits are needed.  */
7362   if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7363     return nonzero;
7364
7365   /* If X is wider than MODE, use its mode instead.  */
7366   if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7367     {
7368       mode = GET_MODE (x);
7369       nonzero = GET_MODE_MASK (mode);
7370       mode_width = GET_MODE_BITSIZE (mode);
7371     }
7372
7373   if (mode_width > HOST_BITS_PER_WIDE_INT)
7374     /* Our only callers in this case look for single bit values.  So
7375        just return the mode mask.  Those tests will then be false.  */
7376     return nonzero;
7377
7378 #ifndef WORD_REGISTER_OPERATIONS
7379   /* If MODE is wider than X, but both are a single word for both the host
7380      and target machines, we can compute this from which bits of the 
7381      object might be nonzero in its own mode, taking into account the fact
7382      that on many CISC machines, accessing an object in a wider mode
7383      causes the high-order bits to become undefined.  So they are
7384      not known to be zero.  */
7385
7386   if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
7387       && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
7388       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7389       && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
7390     {
7391       nonzero &= nonzero_bits (x, GET_MODE (x));
7392       nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
7393       return nonzero;
7394     }
7395 #endif
7396
7397   code = GET_CODE (x);
7398   switch (code)
7399     {
7400     case REG:
7401 #ifdef POINTERS_EXTEND_UNSIGNED
7402       /* If pointers extend unsigned and this is a pointer in Pmode, say that
7403          all the bits above ptr_mode are known to be zero.  */
7404       if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
7405           && REGNO_POINTER_FLAG (REGNO (x)))
7406         nonzero &= GET_MODE_MASK (ptr_mode);
7407 #endif
7408
7409 #ifdef STACK_BOUNDARY
7410       /* If this is the stack pointer, we may know something about its
7411          alignment.  If PUSH_ROUNDING is defined, it is possible for the
7412          stack to be momentarily aligned only to that amount, so we pick
7413          the least alignment.  */
7414
7415       /* We can't check for arg_pointer_rtx here, because it is not
7416          guaranteed to have as much alignment as the stack pointer.
7417          In particular, in the Irix6 n64 ABI, the stack has 128 bit
7418          alignment but the argument pointer has only 64 bit alignment.  */
7419
7420       if ((x == frame_pointer_rtx
7421            || x == stack_pointer_rtx
7422            || x == hard_frame_pointer_rtx
7423            || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7424                && REGNO (x) <= LAST_VIRTUAL_REGISTER))
7425 #ifdef STACK_BIAS
7426           && !STACK_BIAS
7427 #endif        
7428               )
7429         {
7430           int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7431
7432 #ifdef PUSH_ROUNDING
7433           if (REGNO (x) == STACK_POINTER_REGNUM)
7434             sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
7435 #endif
7436
7437           /* We must return here, otherwise we may get a worse result from
7438              one of the choices below.  There is nothing useful below as
7439              far as the stack pointer is concerned.  */
7440           return nonzero &= ~ (sp_alignment - 1);
7441         }
7442 #endif
7443
7444       /* If X is a register whose nonzero bits value is current, use it.
7445          Otherwise, if X is a register whose value we can find, use that
7446          value.  Otherwise, use the previously-computed global nonzero bits
7447          for this register.  */
7448
7449       if (reg_last_set_value[REGNO (x)] != 0
7450           && reg_last_set_mode[REGNO (x)] == mode
7451           && (REG_N_SETS (REGNO (x)) == 1
7452               || reg_last_set_label[REGNO (x)] == label_tick)
7453           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7454         return reg_last_set_nonzero_bits[REGNO (x)];
7455
7456       tem = get_last_value (x);
7457
7458       if (tem)
7459         {
7460 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7461           /* If X is narrower than MODE and TEM is a non-negative
7462              constant that would appear negative in the mode of X,
7463              sign-extend it for use in reg_nonzero_bits because some
7464              machines (maybe most) will actually do the sign-extension
7465              and this is the conservative approach. 
7466
7467              ??? For 2.5, try to tighten up the MD files in this regard
7468              instead of this kludge.  */
7469
7470           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7471               && GET_CODE (tem) == CONST_INT
7472               && INTVAL (tem) > 0
7473               && 0 != (INTVAL (tem)
7474                        & ((HOST_WIDE_INT) 1
7475                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7476             tem = GEN_INT (INTVAL (tem)
7477                            | ((HOST_WIDE_INT) (-1)
7478                               << GET_MODE_BITSIZE (GET_MODE (x))));
7479 #endif
7480           return nonzero_bits (tem, mode);
7481         }
7482       else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7483         return reg_nonzero_bits[REGNO (x)] & nonzero;
7484       else
7485         return nonzero;
7486
7487     case CONST_INT:
7488 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7489       /* If X is negative in MODE, sign-extend the value.  */
7490       if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7491           && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7492         return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
7493 #endif
7494
7495       return INTVAL (x);
7496
7497     case MEM:
7498 #ifdef LOAD_EXTEND_OP
7499       /* In many, if not most, RISC machines, reading a byte from memory
7500          zeros the rest of the register.  Noticing that fact saves a lot
7501          of extra zero-extends.  */
7502       if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7503         nonzero &= GET_MODE_MASK (GET_MODE (x));
7504 #endif
7505       break;
7506
7507     case EQ:  case NE:
7508     case GT:  case GTU:
7509     case LT:  case LTU:
7510     case GE:  case GEU:
7511     case LE:  case LEU:
7512
7513       /* If this produces an integer result, we know which bits are set.
7514          Code here used to clear bits outside the mode of X, but that is
7515          now done above.  */
7516
7517       if (GET_MODE_CLASS (mode) == MODE_INT
7518           && mode_width <= HOST_BITS_PER_WIDE_INT)
7519         nonzero = STORE_FLAG_VALUE;
7520       break;
7521
7522     case NEG:
7523 #if 0
7524       /* Disabled to avoid exponential mutual recursion between nonzero_bits
7525          and num_sign_bit_copies.  */
7526       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7527           == GET_MODE_BITSIZE (GET_MODE (x)))
7528         nonzero = 1;
7529 #endif
7530
7531       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
7532         nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
7533       break;
7534
7535     case ABS:
7536 #if 0
7537       /* Disabled to avoid exponential mutual recursion between nonzero_bits
7538          and num_sign_bit_copies.  */
7539       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7540           == GET_MODE_BITSIZE (GET_MODE (x)))
7541         nonzero = 1;
7542 #endif
7543       break;
7544
7545     case TRUNCATE:
7546       nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
7547       break;
7548
7549     case ZERO_EXTEND:
7550       nonzero &= nonzero_bits (XEXP (x, 0), mode);
7551       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7552         nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7553       break;
7554
7555     case SIGN_EXTEND:
7556       /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7557          Otherwise, show all the bits in the outer mode but not the inner
7558          may be non-zero.  */
7559       inner_nz = nonzero_bits (XEXP (x, 0), mode);
7560       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7561         {
7562           inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7563           if (inner_nz
7564               & (((HOST_WIDE_INT) 1
7565                   << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
7566             inner_nz |= (GET_MODE_MASK (mode)
7567                           & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7568         }
7569
7570       nonzero &= inner_nz;
7571       break;
7572
7573     case AND:
7574       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7575                   & nonzero_bits (XEXP (x, 1), mode));
7576       break;
7577
7578     case XOR:   case IOR:
7579     case UMIN:  case UMAX:  case SMIN:  case SMAX:
7580       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7581                   | nonzero_bits (XEXP (x, 1), mode));
7582       break;
7583
7584     case PLUS:  case MINUS:
7585     case MULT:
7586     case DIV:   case UDIV:
7587     case MOD:   case UMOD:
7588       /* We can apply the rules of arithmetic to compute the number of
7589          high- and low-order zero bits of these operations.  We start by
7590          computing the width (position of the highest-order non-zero bit)
7591          and the number of low-order zero bits for each value.  */
7592       {
7593         unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7594         unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7595         int width0 = floor_log2 (nz0) + 1;
7596         int width1 = floor_log2 (nz1) + 1;
7597         int low0 = floor_log2 (nz0 & -nz0);
7598         int low1 = floor_log2 (nz1 & -nz1);
7599         HOST_WIDE_INT op0_maybe_minusp
7600           = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7601         HOST_WIDE_INT op1_maybe_minusp
7602           = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7603         int result_width = mode_width;
7604         int result_low = 0;
7605
7606         switch (code)
7607           {
7608           case PLUS:
7609 #ifdef STACK_BIAS
7610             if (STACK_BIAS
7611                 && (XEXP (x, 0) == stack_pointer_rtx
7612                     || XEXP (x, 0) == frame_pointer_rtx)
7613                 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7614               {
7615                 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7616
7617                 nz0 = (GET_MODE_MASK (mode) & ~ (sp_alignment - 1));
7618                 nz1 = INTVAL (XEXP (x, 1)) - STACK_BIAS;
7619                 width0 = floor_log2 (nz0) + 1;
7620                 width1 = floor_log2 (nz1) + 1;
7621                 low0 = floor_log2 (nz0 & -nz0);
7622                 low1 = floor_log2 (nz1 & -nz1);
7623               }
7624 #endif    
7625             result_width = MAX (width0, width1) + 1;
7626             result_low = MIN (low0, low1);
7627             break;
7628           case MINUS:
7629             result_low = MIN (low0, low1);
7630             break;
7631           case MULT:
7632             result_width = width0 + width1;
7633             result_low = low0 + low1;
7634             break;
7635           case DIV:
7636             if (! op0_maybe_minusp && ! op1_maybe_minusp)
7637               result_width = width0;
7638             break;
7639           case UDIV:
7640             result_width = width0;
7641             break;
7642           case MOD:
7643             if (! op0_maybe_minusp && ! op1_maybe_minusp)
7644               result_width = MIN (width0, width1);
7645             result_low = MIN (low0, low1);
7646             break;
7647           case UMOD:
7648             result_width = MIN (width0, width1);
7649             result_low = MIN (low0, low1);
7650             break;
7651           default:
7652             abort ();
7653           }
7654
7655         if (result_width < mode_width)
7656           nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
7657
7658         if (result_low > 0)
7659           nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
7660       }
7661       break;
7662
7663     case ZERO_EXTRACT:
7664       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7665           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7666         nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
7667       break;
7668
7669     case SUBREG:
7670       /* If this is a SUBREG formed for a promoted variable that has
7671          been zero-extended, we know that at least the high-order bits
7672          are zero, though others might be too.  */
7673
7674       if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
7675         nonzero = (GET_MODE_MASK (GET_MODE (x))
7676                    & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
7677
7678       /* If the inner mode is a single word for both the host and target
7679          machines, we can compute this from which bits of the inner
7680          object might be nonzero.  */
7681       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
7682           && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7683               <= HOST_BITS_PER_WIDE_INT))
7684         {
7685           nonzero &= nonzero_bits (SUBREG_REG (x), mode);
7686
7687 #ifndef WORD_REGISTER_OPERATIONS
7688           /* On many CISC machines, accessing an object in a wider mode
7689              causes the high-order bits to become undefined.  So they are
7690              not known to be zero.  */
7691           if (GET_MODE_SIZE (GET_MODE (x))
7692               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7693             nonzero |= (GET_MODE_MASK (GET_MODE (x))
7694                         & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
7695 #endif
7696         }
7697       break;
7698
7699     case ASHIFTRT:
7700     case LSHIFTRT:
7701     case ASHIFT:
7702     case ROTATE:
7703       /* The nonzero bits are in two classes: any bits within MODE
7704          that aren't in GET_MODE (x) are always significant.  The rest of the
7705          nonzero bits are those that are significant in the operand of
7706          the shift when shifted the appropriate number of bits.  This
7707          shows that high-order bits are cleared by the right shift and
7708          low-order bits by left shifts.  */
7709       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7710           && INTVAL (XEXP (x, 1)) >= 0
7711           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7712         {
7713           enum machine_mode inner_mode = GET_MODE (x);
7714           int width = GET_MODE_BITSIZE (inner_mode);
7715           int count = INTVAL (XEXP (x, 1));
7716           unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
7717           unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
7718           unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
7719           unsigned HOST_WIDE_INT outer = 0;
7720
7721           if (mode_width > width)
7722             outer = (op_nonzero & nonzero & ~ mode_mask);
7723
7724           if (code == LSHIFTRT)
7725             inner >>= count;
7726           else if (code == ASHIFTRT)
7727             {
7728               inner >>= count;
7729
7730               /* If the sign bit may have been nonzero before the shift, we
7731                  need to mark all the places it could have been copied to
7732                  by the shift as possibly nonzero.  */
7733               if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
7734                 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
7735             }
7736           else if (code == ASHIFT)
7737             inner <<= count;
7738           else
7739             inner = ((inner << (count % width)
7740                       | (inner >> (width - (count % width)))) & mode_mask);
7741
7742           nonzero &= (outer | inner);
7743         }
7744       break;
7745
7746     case FFS:
7747       /* This is at most the number of bits in the mode.  */
7748       nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
7749       break;
7750
7751     case IF_THEN_ELSE:
7752       nonzero &= (nonzero_bits (XEXP (x, 1), mode)
7753                   | nonzero_bits (XEXP (x, 2), mode));
7754       break;
7755       
7756     default:
7757       break;
7758     }
7759
7760   return nonzero;
7761 }
7762
7763 /* See the macro definition above.  */
7764 #undef num_sign_bit_copies
7765 \f
7766 /* Return the number of bits at the high-order end of X that are known to
7767    be equal to the sign bit.  X will be used in mode MODE; if MODE is
7768    VOIDmode, X will be used in its own mode.  The returned value  will always
7769    be between 1 and the number of bits in MODE.  */
7770
7771 static int
7772 num_sign_bit_copies (x, mode)
7773      rtx x;
7774      enum machine_mode mode;
7775 {
7776   enum rtx_code code = GET_CODE (x);
7777   int bitwidth;
7778   int num0, num1, result;
7779   unsigned HOST_WIDE_INT nonzero;
7780   rtx tem;
7781
7782   /* If we weren't given a mode, use the mode of X.  If the mode is still
7783      VOIDmode, we don't know anything.  Likewise if one of the modes is
7784      floating-point.  */
7785
7786   if (mode == VOIDmode)
7787     mode = GET_MODE (x);
7788
7789   if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
7790     return 1;
7791
7792   bitwidth = GET_MODE_BITSIZE (mode);
7793
7794   /* For a smaller object, just ignore the high bits.  */
7795   if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
7796     return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
7797                     - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
7798      
7799   if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
7800     {
7801 #ifndef WORD_REGISTER_OPERATIONS
7802   /* If this machine does not do all register operations on the entire
7803      register and MODE is wider than the mode of X, we can say nothing
7804      at all about the high-order bits.  */
7805       return 1;
7806 #else
7807       /* Likewise on machines that do, if the mode of the object is smaller
7808          than a word and loads of that size don't sign extend, we can say
7809          nothing about the high order bits.  */
7810       if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
7811 #ifdef LOAD_EXTEND_OP
7812           && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
7813 #endif
7814           )
7815         return 1;
7816 #endif
7817     }
7818
7819   switch (code)
7820     {
7821     case REG:
7822
7823 #ifdef POINTERS_EXTEND_UNSIGNED
7824       /* If pointers extend signed and this is a pointer in Pmode, say that
7825          all the bits above ptr_mode are known to be sign bit copies.  */
7826       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
7827           && REGNO_POINTER_FLAG (REGNO (x)))
7828         return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
7829 #endif
7830
7831       if (reg_last_set_value[REGNO (x)] != 0
7832           && reg_last_set_mode[REGNO (x)] == mode
7833           && (REG_N_SETS (REGNO (x)) == 1
7834               || reg_last_set_label[REGNO (x)] == label_tick)
7835           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7836         return reg_last_set_sign_bit_copies[REGNO (x)];
7837
7838       tem =  get_last_value (x);
7839       if (tem != 0)
7840         return num_sign_bit_copies (tem, mode);
7841
7842       if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
7843         return reg_sign_bit_copies[REGNO (x)];
7844       break;
7845
7846     case MEM:
7847 #ifdef LOAD_EXTEND_OP
7848       /* Some RISC machines sign-extend all loads of smaller than a word.  */
7849       if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
7850         return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
7851 #endif
7852       break;
7853
7854     case CONST_INT:
7855       /* If the constant is negative, take its 1's complement and remask.
7856          Then see how many zero bits we have.  */
7857       nonzero = INTVAL (x) & GET_MODE_MASK (mode);
7858       if (bitwidth <= HOST_BITS_PER_WIDE_INT
7859           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7860         nonzero = (~ nonzero) & GET_MODE_MASK (mode);
7861
7862       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
7863
7864     case SUBREG:
7865       /* If this is a SUBREG for a promoted object that is sign-extended
7866          and we are looking at it in a wider mode, we know that at least the
7867          high-order bits are known to be sign bit copies.  */
7868
7869       if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
7870         return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
7871                     num_sign_bit_copies (SUBREG_REG (x), mode));
7872
7873       /* For a smaller object, just ignore the high bits.  */
7874       if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
7875         {
7876           num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
7877           return MAX (1, (num0
7878                           - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7879                              - bitwidth)));
7880         }
7881
7882 #ifdef WORD_REGISTER_OPERATIONS
7883 #ifdef LOAD_EXTEND_OP
7884       /* For paradoxical SUBREGs on machines where all register operations
7885          affect the entire register, just look inside.  Note that we are
7886          passing MODE to the recursive call, so the number of sign bit copies
7887          will remain relative to that mode, not the inner mode.  */
7888
7889       /* This works only if loads sign extend.  Otherwise, if we get a
7890          reload for the inner part, it may be loaded from the stack, and
7891          then we lose all sign bit copies that existed before the store
7892          to the stack.  */
7893
7894       if ((GET_MODE_SIZE (GET_MODE (x))
7895            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7896           && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
7897         return num_sign_bit_copies (SUBREG_REG (x), mode);
7898 #endif
7899 #endif
7900       break;
7901
7902     case SIGN_EXTRACT:
7903       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7904         return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
7905       break;
7906
7907     case SIGN_EXTEND: 
7908       return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7909               + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
7910
7911     case TRUNCATE:
7912       /* For a smaller object, just ignore the high bits.  */
7913       num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
7914       return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7915                               - bitwidth)));
7916
7917     case NOT:
7918       return num_sign_bit_copies (XEXP (x, 0), mode);
7919
7920     case ROTATE:       case ROTATERT:
7921       /* If we are rotating left by a number of bits less than the number
7922          of sign bit copies, we can just subtract that amount from the
7923          number.  */
7924       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7925           && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
7926         {
7927           num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7928           return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
7929                                  : bitwidth - INTVAL (XEXP (x, 1))));
7930         }
7931       break;
7932
7933     case NEG:
7934       /* In general, this subtracts one sign bit copy.  But if the value
7935          is known to be positive, the number of sign bit copies is the
7936          same as that of the input.  Finally, if the input has just one bit
7937          that might be nonzero, all the bits are copies of the sign bit.  */
7938       nonzero = nonzero_bits (XEXP (x, 0), mode);
7939       if (nonzero == 1)
7940         return bitwidth;
7941
7942       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7943       if (num0 > 1
7944           && bitwidth <= HOST_BITS_PER_WIDE_INT
7945           && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
7946         num0--;
7947
7948       return num0;
7949
7950     case IOR:   case AND:   case XOR:
7951     case SMIN:  case SMAX:  case UMIN:  case UMAX:
7952       /* Logical operations will preserve the number of sign-bit copies.
7953          MIN and MAX operations always return one of the operands.  */
7954       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7955       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7956       return MIN (num0, num1);
7957
7958     case PLUS:  case MINUS:
7959       /* For addition and subtraction, we can have a 1-bit carry.  However,
7960          if we are subtracting 1 from a positive number, there will not
7961          be such a carry.  Furthermore, if the positive number is known to
7962          be 0 or 1, we know the result is either -1 or 0.  */
7963
7964       if (code == PLUS && XEXP (x, 1) == constm1_rtx
7965           && bitwidth <= HOST_BITS_PER_WIDE_INT)
7966         {
7967           nonzero = nonzero_bits (XEXP (x, 0), mode);
7968           if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
7969             return (nonzero == 1 || nonzero == 0 ? bitwidth
7970                     : bitwidth - floor_log2 (nonzero) - 1);
7971         }
7972
7973       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7974       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7975       return MAX (1, MIN (num0, num1) - 1);
7976       
7977     case MULT:
7978       /* The number of bits of the product is the sum of the number of
7979          bits of both terms.  However, unless one of the terms if known
7980          to be positive, we must allow for an additional bit since negating
7981          a negative number can remove one sign bit copy.  */
7982
7983       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7984       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7985
7986       result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
7987       if (result > 0
7988           && bitwidth <= HOST_BITS_PER_WIDE_INT
7989           && ((nonzero_bits (XEXP (x, 0), mode)
7990                & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7991           && ((nonzero_bits (XEXP (x, 1), mode)
7992               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
7993         result--;
7994
7995       return MAX (1, result);
7996
7997     case UDIV:
7998       /* The result must be <= the first operand.  */
7999       return num_sign_bit_copies (XEXP (x, 0), mode);
8000
8001     case UMOD:
8002       /* The result must be <= the scond operand.  */
8003       return num_sign_bit_copies (XEXP (x, 1), mode);
8004
8005     case DIV:
8006       /* Similar to unsigned division, except that we have to worry about
8007          the case where the divisor is negative, in which case we have
8008          to add 1.  */
8009       result = num_sign_bit_copies (XEXP (x, 0), mode);
8010       if (result > 1
8011           && bitwidth <= HOST_BITS_PER_WIDE_INT
8012           && (nonzero_bits (XEXP (x, 1), mode)
8013               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8014         result --;
8015
8016       return result;
8017
8018     case MOD:
8019       result = num_sign_bit_copies (XEXP (x, 1), mode);
8020       if (result > 1
8021           && bitwidth <= HOST_BITS_PER_WIDE_INT
8022           && (nonzero_bits (XEXP (x, 1), mode)
8023               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8024         result --;
8025
8026       return result;
8027
8028     case ASHIFTRT:
8029       /* Shifts by a constant add to the number of bits equal to the
8030          sign bit.  */
8031       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8032       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8033           && INTVAL (XEXP (x, 1)) > 0)
8034         num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
8035
8036       return num0;
8037
8038     case ASHIFT:
8039       /* Left shifts destroy copies.  */
8040       if (GET_CODE (XEXP (x, 1)) != CONST_INT
8041           || INTVAL (XEXP (x, 1)) < 0
8042           || INTVAL (XEXP (x, 1)) >= bitwidth)
8043         return 1;
8044
8045       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8046       return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8047
8048     case IF_THEN_ELSE:
8049       num0 = num_sign_bit_copies (XEXP (x, 1), mode);
8050       num1 = num_sign_bit_copies (XEXP (x, 2), mode);
8051       return MIN (num0, num1);
8052
8053     case EQ:  case NE:  case GE:  case GT:  case LE:  case LT:
8054     case GEU: case GTU: case LEU: case LTU:
8055       if (STORE_FLAG_VALUE == -1)
8056         return bitwidth;
8057       break;
8058       
8059     default:
8060       break;
8061     }
8062
8063   /* If we haven't been able to figure it out by one of the above rules,
8064      see if some of the high-order bits are known to be zero.  If so,
8065      count those bits and return one less than that amount.  If we can't
8066      safely compute the mask for this mode, always return BITWIDTH.  */
8067
8068   if (bitwidth > HOST_BITS_PER_WIDE_INT)
8069     return 1;
8070
8071   nonzero = nonzero_bits (x, mode);
8072   return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8073           ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8074 }
8075 \f
8076 /* Return the number of "extended" bits there are in X, when interpreted
8077    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
8078    unsigned quantities, this is the number of high-order zero bits.
8079    For signed quantities, this is the number of copies of the sign bit
8080    minus 1.  In both case, this function returns the number of "spare"
8081    bits.  For example, if two quantities for which this function returns
8082    at least 1 are added, the addition is known not to overflow.
8083
8084    This function will always return 0 unless called during combine, which
8085    implies that it must be called from a define_split.  */
8086
8087 int
8088 extended_count (x, mode, unsignedp)
8089      rtx x;
8090      enum machine_mode mode;
8091      int unsignedp;
8092 {
8093   if (nonzero_sign_valid == 0)
8094     return 0;
8095
8096   return (unsignedp
8097           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8098              && (GET_MODE_BITSIZE (mode) - 1
8099                  - floor_log2 (nonzero_bits (x, mode))))
8100           : num_sign_bit_copies (x, mode) - 1);
8101 }
8102 \f
8103 /* This function is called from `simplify_shift_const' to merge two
8104    outer operations.  Specifically, we have already found that we need
8105    to perform operation *POP0 with constant *PCONST0 at the outermost
8106    position.  We would now like to also perform OP1 with constant CONST1
8107    (with *POP0 being done last).
8108
8109    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8110    the resulting operation.  *PCOMP_P is set to 1 if we would need to 
8111    complement the innermost operand, otherwise it is unchanged.
8112
8113    MODE is the mode in which the operation will be done.  No bits outside
8114    the width of this mode matter.  It is assumed that the width of this mode
8115    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8116
8117    If *POP0 or OP1 are NIL, it means no operation is required.  Only NEG, PLUS,
8118    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
8119    result is simply *PCONST0.
8120
8121    If the resulting operation cannot be expressed as one operation, we
8122    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
8123
8124 static int
8125 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
8126      enum rtx_code *pop0;
8127      HOST_WIDE_INT *pconst0;
8128      enum rtx_code op1;
8129      HOST_WIDE_INT const1;
8130      enum machine_mode mode;
8131      int *pcomp_p;
8132 {
8133   enum rtx_code op0 = *pop0;
8134   HOST_WIDE_INT const0 = *pconst0;
8135   int width = GET_MODE_BITSIZE (mode);
8136
8137   const0 &= GET_MODE_MASK (mode);
8138   const1 &= GET_MODE_MASK (mode);
8139
8140   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
8141   if (op0 == AND)
8142     const1 &= const0;
8143
8144   /* If OP0 or OP1 is NIL, this is easy.  Similarly if they are the same or
8145      if OP0 is SET.  */
8146
8147   if (op1 == NIL || op0 == SET)
8148     return 1;
8149
8150   else if (op0 == NIL)
8151     op0 = op1, const0 = const1;
8152
8153   else if (op0 == op1)
8154     {
8155       switch (op0)
8156         {
8157         case AND:
8158           const0 &= const1;
8159           break;
8160         case IOR:
8161           const0 |= const1;
8162           break;
8163         case XOR:
8164           const0 ^= const1;
8165           break;
8166         case PLUS:
8167           const0 += const1;
8168           break;
8169         case NEG:
8170           op0 = NIL;
8171           break;
8172         default:
8173           break;
8174         }
8175     }
8176
8177   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
8178   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8179     return 0;
8180
8181   /* If the two constants aren't the same, we can't do anything.  The
8182      remaining six cases can all be done.  */
8183   else if (const0 != const1)
8184     return 0;
8185
8186   else
8187     switch (op0)
8188       {
8189       case IOR:
8190         if (op1 == AND)
8191           /* (a & b) | b == b */
8192           op0 = SET;
8193         else /* op1 == XOR */
8194           /* (a ^ b) | b == a | b */
8195           {;}
8196         break;
8197
8198       case XOR:
8199         if (op1 == AND)
8200           /* (a & b) ^ b == (~a) & b */
8201           op0 = AND, *pcomp_p = 1;
8202         else /* op1 == IOR */
8203           /* (a | b) ^ b == a & ~b */
8204           op0 = AND, *pconst0 = ~ const0;
8205         break;
8206
8207       case AND:
8208         if (op1 == IOR)
8209           /* (a | b) & b == b */
8210         op0 = SET;
8211         else /* op1 == XOR */
8212           /* (a ^ b) & b) == (~a) & b */
8213           *pcomp_p = 1;
8214         break;
8215       default:
8216         break;
8217       }
8218
8219   /* Check for NO-OP cases.  */
8220   const0 &= GET_MODE_MASK (mode);
8221   if (const0 == 0
8222       && (op0 == IOR || op0 == XOR || op0 == PLUS))
8223     op0 = NIL;
8224   else if (const0 == 0 && op0 == AND)
8225     op0 = SET;
8226   else if (const0 == GET_MODE_MASK (mode) && op0 == AND)
8227     op0 = NIL;
8228
8229   /* If this would be an entire word for the target, but is not for
8230      the host, then sign-extend on the host so that the number will look
8231      the same way on the host that it would on the target.
8232
8233      For example, when building a 64 bit alpha hosted 32 bit sparc
8234      targeted compiler, then we want the 32 bit unsigned value -1 to be
8235      represented as a 64 bit value -1, and not as 0x00000000ffffffff.
8236      The later confuses the sparc backend.  */
8237
8238   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
8239       && (const0 & ((HOST_WIDE_INT) 1 << (width - 1))))
8240     const0 |= ((HOST_WIDE_INT) (-1) << width);
8241
8242   *pop0 = op0;
8243   *pconst0 = const0;
8244
8245   return 1;
8246 }
8247 \f
8248 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
8249    The result of the shift is RESULT_MODE.  X, if non-zero, is an expression
8250    that we started with.
8251
8252    The shift is normally computed in the widest mode we find in VAROP, as
8253    long as it isn't a different number of words than RESULT_MODE.  Exceptions
8254    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
8255
8256 static rtx
8257 simplify_shift_const (x, code, result_mode, varop, count)
8258      rtx x;
8259      enum rtx_code code;
8260      enum machine_mode result_mode;
8261      rtx varop;
8262      int count;
8263 {
8264   enum rtx_code orig_code = code;
8265   int orig_count = count;
8266   enum machine_mode mode = result_mode;
8267   enum machine_mode shift_mode, tmode;
8268   int mode_words
8269     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8270   /* We form (outer_op (code varop count) (outer_const)).  */
8271   enum rtx_code outer_op = NIL;
8272   HOST_WIDE_INT outer_const = 0;
8273   rtx const_rtx;
8274   int complement_p = 0;
8275   rtx new;
8276
8277   /* If we were given an invalid count, don't do anything except exactly
8278      what was requested.  */
8279
8280   if (count < 0 || count > GET_MODE_BITSIZE (mode))
8281     {
8282       if (x)
8283         return x;
8284
8285       return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (count));
8286     }
8287
8288   /* Unless one of the branches of the `if' in this loop does a `continue',
8289      we will `break' the loop after the `if'.  */
8290
8291   while (count != 0)
8292     {
8293       /* If we have an operand of (clobber (const_int 0)), just return that
8294          value.  */
8295       if (GET_CODE (varop) == CLOBBER)
8296         return varop;
8297
8298       /* If we discovered we had to complement VAROP, leave.  Making a NOT
8299          here would cause an infinite loop.  */
8300       if (complement_p)
8301         break;
8302
8303       /* Convert ROTATERT to ROTATE.  */
8304       if (code == ROTATERT)
8305         code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8306
8307       /* We need to determine what mode we will do the shift in.  If the
8308          shift is a right shift or a ROTATE, we must always do it in the mode
8309          it was originally done in.  Otherwise, we can do it in MODE, the
8310          widest mode encountered.  */
8311       shift_mode
8312         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8313            ? result_mode : mode);
8314
8315       /* Handle cases where the count is greater than the size of the mode
8316          minus 1.  For ASHIFT, use the size minus one as the count (this can
8317          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
8318          take the count modulo the size.  For other shifts, the result is
8319          zero.
8320
8321          Since these shifts are being produced by the compiler by combining
8322          multiple operations, each of which are defined, we know what the
8323          result is supposed to be.  */
8324          
8325       if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8326         {
8327           if (code == ASHIFTRT)
8328             count = GET_MODE_BITSIZE (shift_mode) - 1;
8329           else if (code == ROTATE || code == ROTATERT)
8330             count %= GET_MODE_BITSIZE (shift_mode);
8331           else
8332             {
8333               /* We can't simply return zero because there may be an
8334                  outer op.  */
8335               varop = const0_rtx;
8336               count = 0;
8337               break;
8338             }
8339         }
8340
8341       /* Negative counts are invalid and should not have been made (a
8342          programmer-specified negative count should have been handled
8343          above).  */
8344       else if (count < 0)
8345         abort ();
8346
8347       /* An arithmetic right shift of a quantity known to be -1 or 0
8348          is a no-op.  */
8349       if (code == ASHIFTRT
8350           && (num_sign_bit_copies (varop, shift_mode)
8351               == GET_MODE_BITSIZE (shift_mode)))
8352         {
8353           count = 0;
8354           break;
8355         }
8356
8357       /* If we are doing an arithmetic right shift and discarding all but
8358          the sign bit copies, this is equivalent to doing a shift by the
8359          bitsize minus one.  Convert it into that shift because it will often
8360          allow other simplifications.  */
8361
8362       if (code == ASHIFTRT
8363           && (count + num_sign_bit_copies (varop, shift_mode)
8364               >= GET_MODE_BITSIZE (shift_mode)))
8365         count = GET_MODE_BITSIZE (shift_mode) - 1;
8366
8367       /* We simplify the tests below and elsewhere by converting
8368          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8369          `make_compound_operation' will convert it to a ASHIFTRT for
8370          those machines (such as Vax) that don't have a LSHIFTRT.  */
8371       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8372           && code == ASHIFTRT
8373           && ((nonzero_bits (varop, shift_mode)
8374                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8375               == 0))
8376         code = LSHIFTRT;
8377
8378       switch (GET_CODE (varop))
8379         {
8380         case SIGN_EXTEND:
8381         case ZERO_EXTEND:
8382         case SIGN_EXTRACT:
8383         case ZERO_EXTRACT:
8384           new = expand_compound_operation (varop);
8385           if (new != varop)
8386             {
8387               varop = new;
8388               continue;
8389             }
8390           break;
8391
8392         case MEM:
8393           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8394              minus the width of a smaller mode, we can do this with a
8395              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
8396           if ((code == ASHIFTRT || code == LSHIFTRT)
8397               && ! mode_dependent_address_p (XEXP (varop, 0))
8398               && ! MEM_VOLATILE_P (varop)
8399               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8400                                          MODE_INT, 1)) != BLKmode)
8401             {
8402               if (BYTES_BIG_ENDIAN)
8403                 new = gen_rtx_MEM (tmode, XEXP (varop, 0));
8404               else
8405                 new = gen_rtx_MEM (tmode,
8406                                    plus_constant (XEXP (varop, 0),
8407                                                   count / BITS_PER_UNIT));
8408               RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
8409               MEM_VOLATILE_P (new) = MEM_VOLATILE_P (varop);
8410               MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (varop);
8411               varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8412                                        : ZERO_EXTEND, mode, new);
8413               count = 0;
8414               continue;
8415             }
8416           break;
8417
8418         case USE:
8419           /* Similar to the case above, except that we can only do this if
8420              the resulting mode is the same as that of the underlying
8421              MEM and adjust the address depending on the *bits* endianness
8422              because of the way that bit-field extract insns are defined.  */
8423           if ((code == ASHIFTRT || code == LSHIFTRT)
8424               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8425                                          MODE_INT, 1)) != BLKmode
8426               && tmode == GET_MODE (XEXP (varop, 0)))
8427             {
8428               if (BITS_BIG_ENDIAN)
8429                 new = XEXP (varop, 0);
8430               else
8431                 {
8432                   new = copy_rtx (XEXP (varop, 0));
8433                   SUBST (XEXP (new, 0), 
8434                          plus_constant (XEXP (new, 0),
8435                                         count / BITS_PER_UNIT));
8436                 }
8437
8438               varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8439                                        : ZERO_EXTEND, mode, new);
8440               count = 0;
8441               continue;
8442             }
8443           break;
8444
8445         case SUBREG:
8446           /* If VAROP is a SUBREG, strip it as long as the inner operand has
8447              the same number of words as what we've seen so far.  Then store
8448              the widest mode in MODE.  */
8449           if (subreg_lowpart_p (varop)
8450               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8451                   > GET_MODE_SIZE (GET_MODE (varop)))
8452               && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8453                     + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8454                   == mode_words))
8455             {
8456               varop = SUBREG_REG (varop);
8457               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8458                 mode = GET_MODE (varop);
8459               continue;
8460             }
8461           break;
8462
8463         case MULT:
8464           /* Some machines use MULT instead of ASHIFT because MULT
8465              is cheaper.  But it is still better on those machines to
8466              merge two shifts into one.  */
8467           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8468               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8469             {
8470               varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
8471                                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));;
8472               continue;
8473             }
8474           break;
8475
8476         case UDIV:
8477           /* Similar, for when divides are cheaper.  */
8478           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8479               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8480             {
8481               varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
8482                                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8483               continue;
8484             }
8485           break;
8486
8487         case ASHIFTRT:
8488           /* If we are extracting just the sign bit of an arithmetic right 
8489              shift, that shift is not needed.  */
8490           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
8491             {
8492               varop = XEXP (varop, 0);
8493               continue;
8494             }
8495
8496           /* ... fall through ...  */
8497
8498         case LSHIFTRT:
8499         case ASHIFT:
8500         case ROTATE:
8501           /* Here we have two nested shifts.  The result is usually the
8502              AND of a new shift with a mask.  We compute the result below.  */
8503           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8504               && INTVAL (XEXP (varop, 1)) >= 0
8505               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8506               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8507               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8508             {
8509               enum rtx_code first_code = GET_CODE (varop);
8510               int first_count = INTVAL (XEXP (varop, 1));
8511               unsigned HOST_WIDE_INT mask;
8512               rtx mask_rtx;
8513
8514               /* We have one common special case.  We can't do any merging if
8515                  the inner code is an ASHIFTRT of a smaller mode.  However, if
8516                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8517                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8518                  we can convert it to
8519                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8520                  This simplifies certain SIGN_EXTEND operations.  */
8521               if (code == ASHIFT && first_code == ASHIFTRT
8522                   && (GET_MODE_BITSIZE (result_mode)
8523                       - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8524                 {
8525                   /* C3 has the low-order C1 bits zero.  */
8526                   
8527                   mask = (GET_MODE_MASK (mode)
8528                           & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
8529
8530                   varop = simplify_and_const_int (NULL_RTX, result_mode,
8531                                                   XEXP (varop, 0), mask);
8532                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8533                                                 varop, count);
8534                   count = first_count;
8535                   code = ASHIFTRT;
8536                   continue;
8537                 }
8538               
8539               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8540                  than C1 high-order bits equal to the sign bit, we can convert
8541                  this to either an ASHIFT or a ASHIFTRT depending on the
8542                  two counts. 
8543
8544                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
8545
8546               if (code == ASHIFTRT && first_code == ASHIFT
8547                   && GET_MODE (varop) == shift_mode
8548                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8549                       > first_count))
8550                 {
8551                   count -= first_count;
8552                   if (count < 0)
8553                     count = - count, code = ASHIFT;
8554                   varop = XEXP (varop, 0);
8555                   continue;
8556                 }
8557
8558               /* There are some cases we can't do.  If CODE is ASHIFTRT,
8559                  we can only do this if FIRST_CODE is also ASHIFTRT.
8560
8561                  We can't do the case when CODE is ROTATE and FIRST_CODE is
8562                  ASHIFTRT.
8563
8564                  If the mode of this shift is not the mode of the outer shift,
8565                  we can't do this if either shift is a right shift or ROTATE.
8566
8567                  Finally, we can't do any of these if the mode is too wide
8568                  unless the codes are the same.
8569
8570                  Handle the case where the shift codes are the same
8571                  first.  */
8572
8573               if (code == first_code)
8574                 {
8575                   if (GET_MODE (varop) != result_mode
8576                       && (code == ASHIFTRT || code == LSHIFTRT
8577                           || code == ROTATE))
8578                     break;
8579
8580                   count += first_count;
8581                   varop = XEXP (varop, 0);
8582                   continue;
8583                 }
8584
8585               if (code == ASHIFTRT
8586                   || (code == ROTATE && first_code == ASHIFTRT)
8587                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8588                   || (GET_MODE (varop) != result_mode
8589                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
8590                           || first_code == ROTATE
8591                           || code == ROTATE)))
8592                 break;
8593
8594               /* To compute the mask to apply after the shift, shift the
8595                  nonzero bits of the inner shift the same way the 
8596                  outer shift will.  */
8597
8598               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8599
8600               mask_rtx
8601                 = simplify_binary_operation (code, result_mode, mask_rtx,
8602                                              GEN_INT (count));
8603                                   
8604               /* Give up if we can't compute an outer operation to use.  */
8605               if (mask_rtx == 0
8606                   || GET_CODE (mask_rtx) != CONST_INT
8607                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
8608                                         INTVAL (mask_rtx),
8609                                         result_mode, &complement_p))
8610                 break;
8611
8612               /* If the shifts are in the same direction, we add the
8613                  counts.  Otherwise, we subtract them.  */
8614               if ((code == ASHIFTRT || code == LSHIFTRT)
8615                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8616                 count += first_count;
8617               else
8618                 count -= first_count;
8619
8620               /* If COUNT is positive, the new shift is usually CODE, 
8621                  except for the two exceptions below, in which case it is
8622                  FIRST_CODE.  If the count is negative, FIRST_CODE should
8623                  always be used  */
8624               if (count > 0
8625                   && ((first_code == ROTATE && code == ASHIFT)
8626                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
8627                 code = first_code;
8628               else if (count < 0)
8629                 code = first_code, count = - count;
8630
8631               varop = XEXP (varop, 0);
8632               continue;
8633             }
8634
8635           /* If we have (A << B << C) for any shift, we can convert this to
8636              (A << C << B).  This wins if A is a constant.  Only try this if
8637              B is not a constant.  */
8638
8639           else if (GET_CODE (varop) == code
8640                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
8641                    && 0 != (new
8642                             = simplify_binary_operation (code, mode,
8643                                                          XEXP (varop, 0),
8644                                                          GEN_INT (count))))
8645             {
8646               varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8647               count = 0;
8648               continue;
8649             }
8650           break;
8651
8652         case NOT:
8653           /* Make this fit the case below.  */
8654           varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
8655                                    GEN_INT (GET_MODE_MASK (mode)));
8656           continue;
8657
8658         case IOR:
8659         case AND:
8660         case XOR:
8661           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8662              with C the size of VAROP - 1 and the shift is logical if
8663              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8664              we have an (le X 0) operation.   If we have an arithmetic shift
8665              and STORE_FLAG_VALUE is 1 or we have a logical shift with
8666              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
8667
8668           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8669               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8670               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8671               && (code == LSHIFTRT || code == ASHIFTRT)
8672               && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8673               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8674             {
8675               count = 0;
8676               varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8677                                        const0_rtx);
8678
8679               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8680                 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8681
8682               continue;
8683             }
8684
8685           /* If we have (shift (logical)), move the logical to the outside
8686              to allow it to possibly combine with another logical and the
8687              shift to combine with another shift.  This also canonicalizes to
8688              what a ZERO_EXTRACT looks like.  Also, some machines have
8689              (and (shift)) insns.  */
8690
8691           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8692               && (new = simplify_binary_operation (code, result_mode,
8693                                                    XEXP (varop, 1),
8694                                                    GEN_INT (count))) != 0
8695               && GET_CODE(new) == CONST_INT
8696               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8697                                   INTVAL (new), result_mode, &complement_p))
8698             {
8699               varop = XEXP (varop, 0);
8700               continue;
8701             }
8702
8703           /* If we can't do that, try to simplify the shift in each arm of the
8704              logical expression, make a new logical expression, and apply
8705              the inverse distributive law.  */
8706           {
8707             rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8708                                             XEXP (varop, 0), count);
8709             rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8710                                             XEXP (varop, 1), count);
8711
8712             varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
8713             varop = apply_distributive_law (varop);
8714
8715             count = 0;
8716           }
8717           break;
8718
8719         case EQ:
8720           /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8721              says that the sign bit can be tested, FOO has mode MODE, C is
8722              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8723              that may be nonzero.  */
8724           if (code == LSHIFTRT
8725               && XEXP (varop, 1) == const0_rtx
8726               && GET_MODE (XEXP (varop, 0)) == result_mode
8727               && count == GET_MODE_BITSIZE (result_mode) - 1
8728               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8729               && ((STORE_FLAG_VALUE
8730                    & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
8731               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8732               && merge_outer_ops (&outer_op, &outer_const, XOR,
8733                                   (HOST_WIDE_INT) 1, result_mode,
8734                                   &complement_p))
8735             {
8736               varop = XEXP (varop, 0);
8737               count = 0;
8738               continue;
8739             }
8740           break;
8741
8742         case NEG:
8743           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8744              than the number of bits in the mode is equivalent to A.  */
8745           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8746               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
8747             {
8748               varop = XEXP (varop, 0);
8749               count = 0;
8750               continue;
8751             }
8752
8753           /* NEG commutes with ASHIFT since it is multiplication.  Move the
8754              NEG outside to allow shifts to combine.  */
8755           if (code == ASHIFT
8756               && merge_outer_ops (&outer_op, &outer_const, NEG,
8757                                   (HOST_WIDE_INT) 0, result_mode,
8758                                   &complement_p))
8759             {
8760               varop = XEXP (varop, 0);
8761               continue;
8762             }
8763           break;
8764
8765         case PLUS:
8766           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8767              is one less than the number of bits in the mode is
8768              equivalent to (xor A 1).  */
8769           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8770               && XEXP (varop, 1) == constm1_rtx
8771               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8772               && merge_outer_ops (&outer_op, &outer_const, XOR,
8773                                   (HOST_WIDE_INT) 1, result_mode,
8774                                   &complement_p))
8775             {
8776               count = 0;
8777               varop = XEXP (varop, 0);
8778               continue;
8779             }
8780
8781           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
8782              that might be nonzero in BAR are those being shifted out and those
8783              bits are known zero in FOO, we can replace the PLUS with FOO.
8784              Similarly in the other operand order.  This code occurs when
8785              we are computing the size of a variable-size array.  */
8786
8787           if ((code == ASHIFTRT || code == LSHIFTRT)
8788               && count < HOST_BITS_PER_WIDE_INT
8789               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8790               && (nonzero_bits (XEXP (varop, 1), result_mode)
8791                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
8792             {
8793               varop = XEXP (varop, 0);
8794               continue;
8795             }
8796           else if ((code == ASHIFTRT || code == LSHIFTRT)
8797                    && count < HOST_BITS_PER_WIDE_INT
8798                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8799                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8800                             >> count)
8801                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8802                             & nonzero_bits (XEXP (varop, 1),
8803                                                  result_mode)))
8804             {
8805               varop = XEXP (varop, 1);
8806               continue;
8807             }
8808
8809           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
8810           if (code == ASHIFT
8811               && GET_CODE (XEXP (varop, 1)) == CONST_INT
8812               && (new = simplify_binary_operation (ASHIFT, result_mode,
8813                                                    XEXP (varop, 1),
8814                                                    GEN_INT (count))) != 0
8815               && GET_CODE(new) == CONST_INT
8816               && merge_outer_ops (&outer_op, &outer_const, PLUS,
8817                                   INTVAL (new), result_mode, &complement_p))
8818             {
8819               varop = XEXP (varop, 0);
8820               continue;
8821             }
8822           break;
8823
8824         case MINUS:
8825           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
8826              with C the size of VAROP - 1 and the shift is logical if
8827              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8828              we have a (gt X 0) operation.  If the shift is arithmetic with
8829              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
8830              we have a (neg (gt X 0)) operation.  */
8831
8832           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8833               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
8834               && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8835               && (code == LSHIFTRT || code == ASHIFTRT)
8836               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8837               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
8838               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8839             {
8840               count = 0;
8841               varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
8842                                        const0_rtx);
8843
8844               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8845                 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8846
8847               continue;
8848             }
8849           break;
8850
8851         case TRUNCATE:
8852           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
8853              if the truncate does not affect the value.  */
8854           if (code == LSHIFTRT
8855               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
8856               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8857               && (INTVAL (XEXP (XEXP (varop, 0), 1))
8858                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
8859                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
8860             {
8861               rtx varop_inner = XEXP (varop, 0);
8862
8863               varop_inner = gen_rtx_combine (LSHIFTRT,
8864                                              GET_MODE (varop_inner),
8865                                              XEXP (varop_inner, 0),
8866                                              GEN_INT (count + INTVAL (XEXP (varop_inner, 1))));
8867               varop = gen_rtx_combine (TRUNCATE, GET_MODE (varop),
8868                                        varop_inner);
8869               count = 0;
8870               continue;
8871             }
8872           break;
8873           
8874         default:
8875           break;
8876         }
8877
8878       break;
8879     }
8880
8881   /* We need to determine what mode to do the shift in.  If the shift is
8882      a right shift or ROTATE, we must always do it in the mode it was
8883      originally done in.  Otherwise, we can do it in MODE, the widest mode
8884      encountered.  The code we care about is that of the shift that will
8885      actually be done, not the shift that was originally requested.  */
8886   shift_mode
8887     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8888        ? result_mode : mode);
8889
8890   /* We have now finished analyzing the shift.  The result should be
8891      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
8892      OUTER_OP is non-NIL, it is an operation that needs to be applied
8893      to the result of the shift.  OUTER_CONST is the relevant constant,
8894      but we must turn off all bits turned off in the shift.
8895
8896      If we were passed a value for X, see if we can use any pieces of
8897      it.  If not, make new rtx.  */
8898
8899   if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
8900       && GET_CODE (XEXP (x, 1)) == CONST_INT
8901       && INTVAL (XEXP (x, 1)) == count)
8902     const_rtx = XEXP (x, 1);
8903   else
8904     const_rtx = GEN_INT (count);
8905
8906   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8907       && GET_MODE (XEXP (x, 0)) == shift_mode
8908       && SUBREG_REG (XEXP (x, 0)) == varop)
8909     varop = XEXP (x, 0);
8910   else if (GET_MODE (varop) != shift_mode)
8911     varop = gen_lowpart_for_combine (shift_mode, varop);
8912
8913   /* If we can't make the SUBREG, try to return what we were given.  */
8914   if (GET_CODE (varop) == CLOBBER)
8915     return x ? x : varop;
8916
8917   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
8918   if (new != 0)
8919     x = new;
8920   else
8921     {
8922       if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
8923         x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
8924
8925       SUBST (XEXP (x, 0), varop);
8926       SUBST (XEXP (x, 1), const_rtx);
8927     }
8928
8929   /* If we have an outer operation and we just made a shift, it is
8930      possible that we could have simplified the shift were it not
8931      for the outer operation.  So try to do the simplification
8932      recursively.  */
8933
8934   if (outer_op != NIL && GET_CODE (x) == code
8935       && GET_CODE (XEXP (x, 1)) == CONST_INT)
8936     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
8937                               INTVAL (XEXP (x, 1)));
8938
8939   /* If we were doing a LSHIFTRT in a wider mode than it was originally,
8940      turn off all the bits that the shift would have turned off.  */
8941   if (orig_code == LSHIFTRT && result_mode != shift_mode)
8942     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
8943                                 GET_MODE_MASK (result_mode) >> orig_count);
8944       
8945   /* Do the remainder of the processing in RESULT_MODE.  */
8946   x = gen_lowpart_for_combine (result_mode, x);
8947
8948   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
8949      operation.  */
8950   if (complement_p)
8951     x = gen_unary (NOT, result_mode, result_mode, x);
8952
8953   if (outer_op != NIL)
8954     {
8955       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
8956         {
8957           int width = GET_MODE_BITSIZE (result_mode);
8958
8959           outer_const &= GET_MODE_MASK (result_mode);
8960
8961           /* If this would be an entire word for the target, but is not for
8962              the host, then sign-extend on the host so that the number will
8963              look the same way on the host that it would on the target.
8964
8965              For example, when building a 64 bit alpha hosted 32 bit sparc
8966              targeted compiler, then we want the 32 bit unsigned value -1 to be
8967              represented as a 64 bit value -1, and not as 0x00000000ffffffff.
8968              The later confuses the sparc backend.  */
8969
8970           if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
8971               && (outer_const & ((HOST_WIDE_INT) 1 << (width - 1))))
8972             outer_const |= ((HOST_WIDE_INT) (-1) << width);
8973         }
8974
8975       if (outer_op == AND)
8976         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
8977       else if (outer_op == SET)
8978         /* This means that we have determined that the result is
8979            equivalent to a constant.  This should be rare.  */
8980         x = GEN_INT (outer_const);
8981       else if (GET_RTX_CLASS (outer_op) == '1')
8982         x = gen_unary (outer_op, result_mode, result_mode, x);
8983       else
8984         x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
8985     }
8986
8987   return x;
8988 }  
8989 \f
8990 /* Like recog, but we receive the address of a pointer to a new pattern.
8991    We try to match the rtx that the pointer points to.
8992    If that fails, we may try to modify or replace the pattern,
8993    storing the replacement into the same pointer object.
8994
8995    Modifications include deletion or addition of CLOBBERs.
8996
8997    PNOTES is a pointer to a location where any REG_UNUSED notes added for
8998    the CLOBBERs are placed.
8999
9000    PADDED_SCRATCHES is set to the number of (clobber (scratch)) patterns
9001    we had to add.
9002
9003    The value is the final insn code from the pattern ultimately matched,
9004    or -1.  */
9005
9006 static int
9007 recog_for_combine (pnewpat, insn, pnotes, padded_scratches)
9008      rtx *pnewpat;
9009      rtx insn;
9010      rtx *pnotes;
9011      int *padded_scratches;
9012 {
9013   register rtx pat = *pnewpat;
9014   int insn_code_number;
9015   int num_clobbers_to_add = 0;
9016   int i;
9017   rtx notes = 0;
9018
9019   *padded_scratches = 0;
9020
9021   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9022      we use to indicate that something didn't match.  If we find such a
9023      thing, force rejection.  */
9024   if (GET_CODE (pat) == PARALLEL)
9025     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9026       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9027           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9028         return -1;
9029
9030   /* Is the result of combination a valid instruction?  */
9031   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9032
9033   /* If it isn't, there is the possibility that we previously had an insn
9034      that clobbered some register as a side effect, but the combined
9035      insn doesn't need to do that.  So try once more without the clobbers
9036      unless this represents an ASM insn.  */
9037
9038   if (insn_code_number < 0 && ! check_asm_operands (pat)
9039       && GET_CODE (pat) == PARALLEL)
9040     {
9041       int pos;
9042
9043       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9044         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9045           {
9046             if (i != pos)
9047               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9048             pos++;
9049           }
9050
9051       SUBST_INT (XVECLEN (pat, 0), pos);
9052
9053       if (pos == 1)
9054         pat = XVECEXP (pat, 0, 0);
9055
9056       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9057     }
9058
9059   /* If we had any clobbers to add, make a new pattern than contains
9060      them.  Then check to make sure that all of them are dead.  */
9061   if (num_clobbers_to_add)
9062     {
9063       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9064                                      gen_rtvec (GET_CODE (pat) == PARALLEL
9065                                                 ? XVECLEN (pat, 0) + num_clobbers_to_add
9066                                                 : num_clobbers_to_add + 1));
9067
9068       if (GET_CODE (pat) == PARALLEL)
9069         for (i = 0; i < XVECLEN (pat, 0); i++)
9070           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9071       else
9072         XVECEXP (newpat, 0, 0) = pat;
9073
9074       add_clobbers (newpat, insn_code_number);
9075
9076       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9077            i < XVECLEN (newpat, 0); i++)
9078         {
9079           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9080               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9081             return -1;
9082           else if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == SCRATCH)
9083             (*padded_scratches)++;
9084           notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9085                                      XEXP (XVECEXP (newpat, 0, i), 0), notes);
9086         }
9087       pat = newpat;
9088     }
9089
9090   *pnewpat = pat;
9091   *pnotes = notes;
9092
9093   return insn_code_number;
9094 }
9095 \f
9096 /* Like gen_lowpart but for use by combine.  In combine it is not possible
9097    to create any new pseudoregs.  However, it is safe to create
9098    invalid memory addresses, because combine will try to recognize
9099    them and all they will do is make the combine attempt fail.
9100
9101    If for some reason this cannot do its job, an rtx
9102    (clobber (const_int 0)) is returned.
9103    An insn containing that will not be recognized.  */
9104
9105 #undef gen_lowpart
9106
9107 static rtx
9108 gen_lowpart_for_combine (mode, x)
9109      enum machine_mode mode;
9110      register rtx x;
9111 {
9112   rtx result;
9113
9114   if (GET_MODE (x) == mode)
9115     return x;
9116
9117   /* We can only support MODE being wider than a word if X is a
9118      constant integer or has a mode the same size.  */
9119
9120   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9121       && ! ((GET_MODE (x) == VOIDmode
9122              && (GET_CODE (x) == CONST_INT
9123                  || GET_CODE (x) == CONST_DOUBLE))
9124             || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
9125     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9126
9127   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
9128      won't know what to do.  So we will strip off the SUBREG here and
9129      process normally.  */
9130   if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
9131     {
9132       x = SUBREG_REG (x);
9133       if (GET_MODE (x) == mode)
9134         return x;
9135     }
9136
9137   result = gen_lowpart_common (mode, x);
9138   if (result != 0
9139       && GET_CODE (result) == SUBREG
9140       && GET_CODE (SUBREG_REG (result)) == REG
9141       && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
9142       && (GET_MODE_SIZE (GET_MODE (result))
9143           != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
9144     REG_CHANGES_SIZE (REGNO (SUBREG_REG (result))) = 1;
9145
9146   if (result)
9147     return result;
9148
9149   if (GET_CODE (x) == MEM)
9150     {
9151       register int offset = 0;
9152       rtx new;
9153
9154       /* Refuse to work on a volatile memory ref or one with a mode-dependent
9155          address.  */
9156       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9157         return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9158
9159       /* If we want to refer to something bigger than the original memref,
9160          generate a perverse subreg instead.  That will force a reload
9161          of the original memref X.  */
9162       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
9163         return gen_rtx_SUBREG (mode, x, 0);
9164
9165       if (WORDS_BIG_ENDIAN)
9166         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
9167                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
9168       if (BYTES_BIG_ENDIAN)
9169         {
9170           /* Adjust the address so that the address-after-the-data is
9171              unchanged.  */
9172           offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
9173                      - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
9174         }
9175       new = gen_rtx_MEM (mode, plus_constant (XEXP (x, 0), offset));
9176       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
9177       MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
9178       MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
9179       return new;
9180     }
9181
9182   /* If X is a comparison operator, rewrite it in a new mode.  This
9183      probably won't match, but may allow further simplifications.  */
9184   else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9185     return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
9186
9187   /* If we couldn't simplify X any other way, just enclose it in a
9188      SUBREG.  Normally, this SUBREG won't match, but some patterns may
9189      include an explicit SUBREG or we may simplify it further in combine.  */
9190   else
9191     {
9192       int word = 0;
9193
9194       if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
9195         word = ((GET_MODE_SIZE (GET_MODE (x))
9196                  - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
9197                 / UNITS_PER_WORD);
9198       return gen_rtx_SUBREG (mode, x, word);
9199     }
9200 }
9201 \f
9202 /* Make an rtx expression.  This is a subset of gen_rtx and only supports
9203    expressions of 1, 2, or 3 operands, each of which are rtx expressions.
9204
9205    If the identical expression was previously in the insn (in the undobuf),
9206    it will be returned.  Only if it is not found will a new expression
9207    be made.  */
9208
9209 /*VARARGS2*/
9210 static rtx
9211 gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
9212 {
9213 #ifndef __STDC__
9214   enum rtx_code code;
9215   enum machine_mode mode;
9216 #endif
9217   va_list p;
9218   int n_args;
9219   rtx args[3];
9220   int j;
9221   char *fmt;
9222   rtx rt;
9223   struct undo *undo;
9224
9225   VA_START (p, mode);
9226
9227 #ifndef __STDC__
9228   code = va_arg (p, enum rtx_code);
9229   mode = va_arg (p, enum machine_mode);
9230 #endif
9231
9232   n_args = GET_RTX_LENGTH (code);
9233   fmt = GET_RTX_FORMAT (code);
9234
9235   if (n_args == 0 || n_args > 3)
9236     abort ();
9237
9238   /* Get each arg and verify that it is supposed to be an expression.  */
9239   for (j = 0; j < n_args; j++)
9240     {
9241       if (*fmt++ != 'e')
9242         abort ();
9243
9244       args[j] = va_arg (p, rtx);
9245     }
9246
9247   /* See if this is in undobuf.  Be sure we don't use objects that came
9248      from another insn; this could produce circular rtl structures.  */
9249
9250   for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
9251     if (!undo->is_int
9252         && GET_CODE (undo->old_contents.r) == code
9253         && GET_MODE (undo->old_contents.r) == mode)
9254       {
9255         for (j = 0; j < n_args; j++)
9256           if (XEXP (undo->old_contents.r, j) != args[j])
9257             break;
9258
9259         if (j == n_args)
9260           return undo->old_contents.r;
9261       }
9262
9263   /* Otherwise make a new rtx.  We know we have 1, 2, or 3 args.
9264      Use rtx_alloc instead of gen_rtx because it's faster on RISC.  */
9265   rt = rtx_alloc (code);
9266   PUT_MODE (rt, mode);
9267   XEXP (rt, 0) = args[0];
9268   if (n_args > 1)
9269     {
9270       XEXP (rt, 1) = args[1];
9271       if (n_args > 2)
9272         XEXP (rt, 2) = args[2];
9273     }
9274   return rt;
9275 }
9276
9277 /* These routines make binary and unary operations by first seeing if they
9278    fold; if not, a new expression is allocated.  */
9279
9280 static rtx
9281 gen_binary (code, mode, op0, op1)
9282      enum rtx_code code;
9283      enum machine_mode mode;
9284      rtx op0, op1;
9285 {
9286   rtx result;
9287   rtx tem;
9288
9289   if (GET_RTX_CLASS (code) == 'c'
9290       && (GET_CODE (op0) == CONST_INT
9291           || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9292     tem = op0, op0 = op1, op1 = tem;
9293
9294   if (GET_RTX_CLASS (code) == '<') 
9295     {
9296       enum machine_mode op_mode = GET_MODE (op0);
9297
9298       /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get 
9299          just (REL_OP X Y).  */
9300       if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9301         {
9302           op1 = XEXP (op0, 1);
9303           op0 = XEXP (op0, 0);
9304           op_mode = GET_MODE (op0);
9305         }
9306
9307       if (op_mode == VOIDmode)
9308         op_mode = GET_MODE (op1);
9309       result = simplify_relational_operation (code, op_mode, op0, op1);
9310     }
9311   else
9312     result = simplify_binary_operation (code, mode, op0, op1);
9313
9314   if (result)
9315     return result;
9316
9317   /* Put complex operands first and constants second.  */
9318   if (GET_RTX_CLASS (code) == 'c'
9319       && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9320           || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9321               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9322           || (GET_CODE (op0) == SUBREG
9323               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9324               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9325     return gen_rtx_combine (code, mode, op1, op0);
9326
9327   /* If we are turning off bits already known off in OP0, we need not do
9328      an AND.  */
9329   else if (code == AND && GET_CODE (op1) == CONST_INT
9330            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9331            && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
9332     return op0;
9333
9334   return gen_rtx_combine (code, mode, op0, op1);
9335 }
9336
9337 static rtx
9338 gen_unary (code, mode, op0_mode, op0)
9339      enum rtx_code code;
9340      enum machine_mode mode, op0_mode;
9341      rtx op0;
9342 {
9343   rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
9344
9345   if (result)
9346     return result;
9347
9348   return gen_rtx_combine (code, mode, op0);
9349 }
9350 \f
9351 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9352    comparison code that will be tested.
9353
9354    The result is a possibly different comparison code to use.  *POP0 and
9355    *POP1 may be updated.
9356
9357    It is possible that we might detect that a comparison is either always
9358    true or always false.  However, we do not perform general constant
9359    folding in combine, so this knowledge isn't useful.  Such tautologies
9360    should have been detected earlier.  Hence we ignore all such cases.  */
9361
9362 static enum rtx_code
9363 simplify_comparison (code, pop0, pop1)
9364      enum rtx_code code;
9365      rtx *pop0;
9366      rtx *pop1;
9367 {
9368   rtx op0 = *pop0;
9369   rtx op1 = *pop1;
9370   rtx tem, tem1;
9371   int i;
9372   enum machine_mode mode, tmode;
9373
9374   /* Try a few ways of applying the same transformation to both operands.  */
9375   while (1)
9376     {
9377 #ifndef WORD_REGISTER_OPERATIONS
9378       /* The test below this one won't handle SIGN_EXTENDs on these machines,
9379          so check specially.  */
9380       if (code != GTU && code != GEU && code != LTU && code != LEU
9381           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9382           && GET_CODE (XEXP (op0, 0)) == ASHIFT
9383           && GET_CODE (XEXP (op1, 0)) == ASHIFT
9384           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9385           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9386           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9387               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9388           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9389           && GET_CODE (XEXP (op1, 1)) == CONST_INT
9390           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9391           && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
9392           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
9393           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
9394           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
9395           && (INTVAL (XEXP (op0, 1))
9396               == (GET_MODE_BITSIZE (GET_MODE (op0))
9397                   - (GET_MODE_BITSIZE
9398                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9399         {
9400           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9401           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9402         }
9403 #endif
9404
9405       /* If both operands are the same constant shift, see if we can ignore the
9406          shift.  We can if the shift is a rotate or if the bits shifted out of
9407          this shift are known to be zero for both inputs and if the type of
9408          comparison is compatible with the shift.  */
9409       if (GET_CODE (op0) == GET_CODE (op1)
9410           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9411           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9412               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9413                   && (code != GT && code != LT && code != GE && code != LE))
9414               || (GET_CODE (op0) == ASHIFTRT
9415                   && (code != GTU && code != LTU
9416                       && code != GEU && code != GEU)))
9417           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9418           && INTVAL (XEXP (op0, 1)) >= 0
9419           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9420           && XEXP (op0, 1) == XEXP (op1, 1))
9421         {
9422           enum machine_mode mode = GET_MODE (op0);
9423           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9424           int shift_count = INTVAL (XEXP (op0, 1));
9425
9426           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9427             mask &= (mask >> shift_count) << shift_count;
9428           else if (GET_CODE (op0) == ASHIFT)
9429             mask = (mask & (mask << shift_count)) >> shift_count;
9430
9431           if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
9432               && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
9433             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9434           else
9435             break;
9436         }
9437
9438       /* If both operands are AND's of a paradoxical SUBREG by constant, the
9439          SUBREGs are of the same mode, and, in both cases, the AND would
9440          be redundant if the comparison was done in the narrower mode,
9441          do the comparison in the narrower mode (e.g., we are AND'ing with 1
9442          and the operand's possibly nonzero bits are 0xffffff01; in that case
9443          if we only care about QImode, we don't need the AND).  This case
9444          occurs if the output mode of an scc insn is not SImode and
9445          STORE_FLAG_VALUE == 1 (e.g., the 386).
9446
9447          Similarly, check for a case where the AND's are ZERO_EXTEND
9448          operations from some narrower mode even though a SUBREG is not
9449          present.  */
9450
9451       else if  (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9452                 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9453                 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9454         {
9455           rtx inner_op0 = XEXP (op0, 0);
9456           rtx inner_op1 = XEXP (op1, 0);
9457           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9458           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9459           int changed = 0;
9460                 
9461           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9462               && (GET_MODE_SIZE (GET_MODE (inner_op0))
9463                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9464               && (GET_MODE (SUBREG_REG (inner_op0))
9465                   == GET_MODE (SUBREG_REG (inner_op1)))
9466               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9467                   <= HOST_BITS_PER_WIDE_INT)
9468               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9469                                              GET_MODE (SUBREG_REG (op0)))))
9470               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9471                                              GET_MODE (SUBREG_REG (inner_op1))))))
9472             {
9473               op0 = SUBREG_REG (inner_op0);
9474               op1 = SUBREG_REG (inner_op1);
9475
9476               /* The resulting comparison is always unsigned since we masked
9477                  off the original sign bit.  */
9478               code = unsigned_condition (code);
9479
9480               changed = 1;
9481             }
9482
9483           else if (c0 == c1)
9484             for (tmode = GET_CLASS_NARROWEST_MODE
9485                  (GET_MODE_CLASS (GET_MODE (op0)));
9486                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9487               if (c0 == GET_MODE_MASK (tmode))
9488                 {
9489                   op0 = gen_lowpart_for_combine (tmode, inner_op0);
9490                   op1 = gen_lowpart_for_combine (tmode, inner_op1);
9491                   code = unsigned_condition (code);
9492                   changed = 1;
9493                   break;
9494                 }
9495
9496           if (! changed)
9497             break;
9498         }
9499
9500       /* If both operands are NOT, we can strip off the outer operation
9501          and adjust the comparison code for swapped operands; similarly for
9502          NEG, except that this must be an equality comparison.  */
9503       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9504                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9505                    && (code == EQ || code == NE)))
9506         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9507
9508       else
9509         break;
9510     }
9511      
9512   /* If the first operand is a constant, swap the operands and adjust the
9513      comparison code appropriately, but don't do this if the second operand
9514      is already a constant integer.  */
9515   if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9516     {
9517       tem = op0, op0 = op1, op1 = tem;
9518       code = swap_condition (code);
9519     }
9520
9521   /* We now enter a loop during which we will try to simplify the comparison.
9522      For the most part, we only are concerned with comparisons with zero,
9523      but some things may really be comparisons with zero but not start
9524      out looking that way.  */
9525
9526   while (GET_CODE (op1) == CONST_INT)
9527     {
9528       enum machine_mode mode = GET_MODE (op0);
9529       int mode_width = GET_MODE_BITSIZE (mode);
9530       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9531       int equality_comparison_p;
9532       int sign_bit_comparison_p;
9533       int unsigned_comparison_p;
9534       HOST_WIDE_INT const_op;
9535
9536       /* We only want to handle integral modes.  This catches VOIDmode,
9537          CCmode, and the floating-point modes.  An exception is that we
9538          can handle VOIDmode if OP0 is a COMPARE or a comparison
9539          operation.  */
9540
9541       if (GET_MODE_CLASS (mode) != MODE_INT
9542           && ! (mode == VOIDmode
9543                 && (GET_CODE (op0) == COMPARE
9544                     || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9545         break;
9546
9547       /* Get the constant we are comparing against and turn off all bits
9548          not on in our mode.  */
9549       const_op = INTVAL (op1);
9550       if (mode_width <= HOST_BITS_PER_WIDE_INT)
9551         const_op &= mask;
9552
9553       /* If we are comparing against a constant power of two and the value
9554          being compared can only have that single bit nonzero (e.g., it was
9555          `and'ed with that bit), we can replace this with a comparison
9556          with zero.  */
9557       if (const_op
9558           && (code == EQ || code == NE || code == GE || code == GEU
9559               || code == LT || code == LTU)
9560           && mode_width <= HOST_BITS_PER_WIDE_INT
9561           && exact_log2 (const_op) >= 0
9562           && nonzero_bits (op0, mode) == const_op)
9563         {
9564           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9565           op1 = const0_rtx, const_op = 0;
9566         }
9567
9568       /* Similarly, if we are comparing a value known to be either -1 or
9569          0 with -1, change it to the opposite comparison against zero.  */
9570
9571       if (const_op == -1
9572           && (code == EQ || code == NE || code == GT || code == LE
9573               || code == GEU || code == LTU)
9574           && num_sign_bit_copies (op0, mode) == mode_width)
9575         {
9576           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9577           op1 = const0_rtx, const_op = 0;
9578         }
9579
9580       /* Do some canonicalizations based on the comparison code.  We prefer
9581          comparisons against zero and then prefer equality comparisons.  
9582          If we can reduce the size of a constant, we will do that too.  */
9583
9584       switch (code)
9585         {
9586         case LT:
9587           /* < C is equivalent to <= (C - 1) */
9588           if (const_op > 0)
9589             {
9590               const_op -= 1;
9591               op1 = GEN_INT (const_op);
9592               code = LE;
9593               /* ... fall through to LE case below.  */
9594             }
9595           else
9596             break;
9597
9598         case LE:
9599           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
9600           if (const_op < 0)
9601             {
9602               const_op += 1;
9603               op1 = GEN_INT (const_op);
9604               code = LT;
9605             }
9606
9607           /* If we are doing a <= 0 comparison on a value known to have
9608              a zero sign bit, we can replace this with == 0.  */
9609           else if (const_op == 0
9610                    && mode_width <= HOST_BITS_PER_WIDE_INT
9611                    && (nonzero_bits (op0, mode)
9612                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9613             code = EQ;
9614           break;
9615
9616         case GE:
9617           /* >= C is equivalent to > (C - 1).  */
9618           if (const_op > 0)
9619             {
9620               const_op -= 1;
9621               op1 = GEN_INT (const_op);
9622               code = GT;
9623               /* ... fall through to GT below.  */
9624             }
9625           else
9626             break;
9627
9628         case GT:
9629           /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9630           if (const_op < 0)
9631             {
9632               const_op += 1;
9633               op1 = GEN_INT (const_op);
9634               code = GE;
9635             }
9636
9637           /* If we are doing a > 0 comparison on a value known to have
9638              a zero sign bit, we can replace this with != 0.  */
9639           else if (const_op == 0
9640                    && mode_width <= HOST_BITS_PER_WIDE_INT
9641                    && (nonzero_bits (op0, mode)
9642                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9643             code = NE;
9644           break;
9645
9646         case LTU:
9647           /* < C is equivalent to <= (C - 1).  */
9648           if (const_op > 0)
9649             {
9650               const_op -= 1;
9651               op1 = GEN_INT (const_op);
9652               code = LEU;
9653               /* ... fall through ...  */
9654             }
9655
9656           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
9657           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9658                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9659             {
9660               const_op = 0, op1 = const0_rtx;
9661               code = GE;
9662               break;
9663             }
9664           else
9665             break;
9666
9667         case LEU:
9668           /* unsigned <= 0 is equivalent to == 0 */
9669           if (const_op == 0)
9670             code = EQ;
9671
9672           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
9673           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9674                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9675             {
9676               const_op = 0, op1 = const0_rtx;
9677               code = GE;
9678             }
9679           break;
9680
9681         case GEU:
9682           /* >= C is equivalent to < (C - 1).  */
9683           if (const_op > 1)
9684             {
9685               const_op -= 1;
9686               op1 = GEN_INT (const_op);
9687               code = GTU;
9688               /* ... fall through ...  */
9689             }
9690
9691           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
9692           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9693                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9694             {
9695               const_op = 0, op1 = const0_rtx;
9696               code = LT;
9697               break;
9698             }
9699           else
9700             break;
9701
9702         case GTU:
9703           /* unsigned > 0 is equivalent to != 0 */
9704           if (const_op == 0)
9705             code = NE;
9706
9707           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
9708           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9709                     && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9710             {
9711               const_op = 0, op1 = const0_rtx;
9712               code = LT;
9713             }
9714           break;
9715
9716         default:
9717           break;
9718         }
9719
9720       /* Compute some predicates to simplify code below.  */
9721
9722       equality_comparison_p = (code == EQ || code == NE);
9723       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9724       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9725                                || code == LEU);
9726
9727       /* If this is a sign bit comparison and we can do arithmetic in
9728          MODE, say that we will only be needing the sign bit of OP0.  */
9729       if (sign_bit_comparison_p
9730           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9731         op0 = force_to_mode (op0, mode,
9732                              ((HOST_WIDE_INT) 1
9733                               << (GET_MODE_BITSIZE (mode) - 1)),
9734                              NULL_RTX, 0);
9735
9736       /* Now try cases based on the opcode of OP0.  If none of the cases
9737          does a "continue", we exit this loop immediately after the
9738          switch.  */
9739
9740       switch (GET_CODE (op0))
9741         {
9742         case ZERO_EXTRACT:
9743           /* If we are extracting a single bit from a variable position in
9744              a constant that has only a single bit set and are comparing it
9745              with zero, we can convert this into an equality comparison 
9746              between the position and the location of the single bit.  */
9747
9748           if (GET_CODE (XEXP (op0, 0)) == CONST_INT
9749               && XEXP (op0, 1) == const1_rtx
9750               && equality_comparison_p && const_op == 0
9751               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9752             {
9753               if (BITS_BIG_ENDIAN)
9754 #ifdef HAVE_extzv
9755                 i = (GET_MODE_BITSIZE
9756                      (insn_operand_mode[(int) CODE_FOR_extzv][1]) - 1 - i);
9757 #else
9758                 i = BITS_PER_WORD - 1 - i;
9759 #endif
9760
9761               op0 = XEXP (op0, 2);
9762               op1 = GEN_INT (i);
9763               const_op = i;
9764
9765               /* Result is nonzero iff shift count is equal to I.  */
9766               code = reverse_condition (code);
9767               continue;
9768             }
9769
9770           /* ... fall through ...  */
9771
9772         case SIGN_EXTRACT:
9773           tem = expand_compound_operation (op0);
9774           if (tem != op0)
9775             {
9776               op0 = tem;
9777               continue;
9778             }
9779           break;
9780
9781         case NOT:
9782           /* If testing for equality, we can take the NOT of the constant.  */
9783           if (equality_comparison_p
9784               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9785             {
9786               op0 = XEXP (op0, 0);
9787               op1 = tem;
9788               continue;
9789             }
9790
9791           /* If just looking at the sign bit, reverse the sense of the
9792              comparison.  */
9793           if (sign_bit_comparison_p)
9794             {
9795               op0 = XEXP (op0, 0);
9796               code = (code == GE ? LT : GE);
9797               continue;
9798             }
9799           break;
9800
9801         case NEG:
9802           /* If testing for equality, we can take the NEG of the constant.  */
9803           if (equality_comparison_p
9804               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9805             {
9806               op0 = XEXP (op0, 0);
9807               op1 = tem;
9808               continue;
9809             }
9810
9811           /* The remaining cases only apply to comparisons with zero.  */
9812           if (const_op != 0)
9813             break;
9814
9815           /* When X is ABS or is known positive,
9816              (neg X) is < 0 if and only if X != 0.  */
9817
9818           if (sign_bit_comparison_p
9819               && (GET_CODE (XEXP (op0, 0)) == ABS
9820                   || (mode_width <= HOST_BITS_PER_WIDE_INT
9821                       && (nonzero_bits (XEXP (op0, 0), mode)
9822                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
9823             {
9824               op0 = XEXP (op0, 0);
9825               code = (code == LT ? NE : EQ);
9826               continue;
9827             }
9828
9829           /* If we have NEG of something whose two high-order bits are the
9830              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
9831           if (num_sign_bit_copies (op0, mode) >= 2)
9832             {
9833               op0 = XEXP (op0, 0);
9834               code = swap_condition (code);
9835               continue;
9836             }
9837           break;
9838
9839         case ROTATE:
9840           /* If we are testing equality and our count is a constant, we
9841              can perform the inverse operation on our RHS.  */
9842           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9843               && (tem = simplify_binary_operation (ROTATERT, mode,
9844                                                    op1, XEXP (op0, 1))) != 0)
9845             {
9846               op0 = XEXP (op0, 0);
9847               op1 = tem;
9848               continue;
9849             }
9850
9851           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
9852              a particular bit.  Convert it to an AND of a constant of that
9853              bit.  This will be converted into a ZERO_EXTRACT.  */
9854           if (const_op == 0 && sign_bit_comparison_p
9855               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9856               && mode_width <= HOST_BITS_PER_WIDE_INT)
9857             {
9858               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9859                                             ((HOST_WIDE_INT) 1
9860                                              << (mode_width - 1
9861                                                  - INTVAL (XEXP (op0, 1)))));
9862               code = (code == LT ? NE : EQ);
9863               continue;
9864             }
9865
9866           /* ... fall through ...  */
9867
9868         case ABS:
9869           /* ABS is ignorable inside an equality comparison with zero.  */
9870           if (const_op == 0 && equality_comparison_p)
9871             {
9872               op0 = XEXP (op0, 0);
9873               continue;
9874             }
9875           break;
9876           
9877
9878         case SIGN_EXTEND:
9879           /* Can simplify (compare (zero/sign_extend FOO) CONST)
9880              to (compare FOO CONST) if CONST fits in FOO's mode and we 
9881              are either testing inequality or have an unsigned comparison
9882              with ZERO_EXTEND or a signed comparison with SIGN_EXTEND.  */
9883           if (! unsigned_comparison_p
9884               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9885                   <= HOST_BITS_PER_WIDE_INT)
9886               && ((unsigned HOST_WIDE_INT) const_op
9887                   < (((HOST_WIDE_INT) 1
9888                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
9889             {
9890               op0 = XEXP (op0, 0);
9891               continue;
9892             }
9893           break;
9894
9895         case SUBREG:
9896           /* Check for the case where we are comparing A - C1 with C2,
9897              both constants are smaller than 1/2 the maximum positive
9898              value in MODE, and the comparison is equality or unsigned.
9899              In that case, if A is either zero-extended to MODE or has
9900              sufficient sign bits so that the high-order bit in MODE
9901              is a copy of the sign in the inner mode, we can prove that it is
9902              safe to do the operation in the wider mode.  This simplifies
9903              many range checks.  */
9904
9905           if (mode_width <= HOST_BITS_PER_WIDE_INT
9906               && subreg_lowpart_p (op0)
9907               && GET_CODE (SUBREG_REG (op0)) == PLUS
9908               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
9909               && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
9910               && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
9911                   < GET_MODE_MASK (mode) / 2)
9912               && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
9913               && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
9914                                       GET_MODE (SUBREG_REG (op0)))
9915                         & ~ GET_MODE_MASK (mode))
9916                   || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
9917                                            GET_MODE (SUBREG_REG (op0)))
9918                       > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9919                          - GET_MODE_BITSIZE (mode)))))
9920             {
9921               op0 = SUBREG_REG (op0);
9922               continue;
9923             }
9924
9925           /* If the inner mode is narrower and we are extracting the low part,
9926              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
9927           if (subreg_lowpart_p (op0)
9928               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
9929             /* Fall through */ ;
9930           else
9931             break;
9932
9933           /* ... fall through ...  */
9934
9935         case ZERO_EXTEND:
9936           if ((unsigned_comparison_p || equality_comparison_p)
9937               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9938                   <= HOST_BITS_PER_WIDE_INT)
9939               && ((unsigned HOST_WIDE_INT) const_op
9940                   < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
9941             {
9942               op0 = XEXP (op0, 0);
9943               continue;
9944             }
9945           break;
9946
9947         case PLUS:
9948           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
9949              this for equality comparisons due to pathological cases involving
9950              overflows.  */
9951           if (equality_comparison_p
9952               && 0 != (tem = simplify_binary_operation (MINUS, mode,
9953                                                         op1, XEXP (op0, 1))))
9954             {
9955               op0 = XEXP (op0, 0);
9956               op1 = tem;
9957               continue;
9958             }
9959
9960           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
9961           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
9962               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
9963             {
9964               op0 = XEXP (XEXP (op0, 0), 0);
9965               code = (code == LT ? EQ : NE);
9966               continue;
9967             }
9968           break;
9969
9970         case MINUS:
9971           /* (eq (minus A B) C) -> (eq A (plus B C)) or
9972              (eq B (minus A C)), whichever simplifies.  We can only do
9973              this for equality comparisons due to pathological cases involving
9974              overflows.  */
9975           if (equality_comparison_p
9976               && 0 != (tem = simplify_binary_operation (PLUS, mode,
9977                                                         XEXP (op0, 1), op1)))
9978             {
9979               op0 = XEXP (op0, 0);
9980               op1 = tem;
9981               continue;
9982             }
9983
9984           if (equality_comparison_p
9985               && 0 != (tem = simplify_binary_operation (MINUS, mode,
9986                                                         XEXP (op0, 0), op1)))
9987             {
9988               op0 = XEXP (op0, 1);
9989               op1 = tem;
9990               continue;
9991             }
9992
9993           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
9994              of bits in X minus 1, is one iff X > 0.  */
9995           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
9996               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9997               && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
9998               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
9999             {
10000               op0 = XEXP (op0, 1);
10001               code = (code == GE ? LE : GT);
10002               continue;
10003             }
10004           break;
10005
10006         case XOR:
10007           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10008              if C is zero or B is a constant.  */
10009           if (equality_comparison_p
10010               && 0 != (tem = simplify_binary_operation (XOR, mode,
10011                                                         XEXP (op0, 1), op1)))
10012             {
10013               op0 = XEXP (op0, 0);
10014               op1 = tem;
10015               continue;
10016             }
10017           break;
10018
10019         case EQ:  case NE:
10020         case LT:  case LTU:  case LE:  case LEU:
10021         case GT:  case GTU:  case GE:  case GEU:
10022           /* We can't do anything if OP0 is a condition code value, rather
10023              than an actual data value.  */
10024           if (const_op != 0
10025 #ifdef HAVE_cc0
10026               || XEXP (op0, 0) == cc0_rtx
10027 #endif
10028               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10029             break;
10030
10031           /* Get the two operands being compared.  */
10032           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10033             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10034           else
10035             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10036
10037           /* Check for the cases where we simply want the result of the
10038              earlier test or the opposite of that result.  */
10039           if (code == NE
10040               || (code == EQ && reversible_comparison_p (op0))
10041               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10042                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10043                   && (STORE_FLAG_VALUE
10044                       & (((HOST_WIDE_INT) 1
10045                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10046                   && (code == LT
10047                       || (code == GE && reversible_comparison_p (op0)))))
10048             {
10049               code = (code == LT || code == NE
10050                       ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
10051               op0 = tem, op1 = tem1;
10052               continue;
10053             }
10054           break;
10055
10056         case IOR:
10057           /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
10058              iff X <= 0.  */
10059           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10060               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10061               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10062             {
10063               op0 = XEXP (op0, 1);
10064               code = (code == GE ? GT : LE);
10065               continue;
10066             }
10067           break;
10068
10069         case AND:
10070           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10071              will be converted to a ZERO_EXTRACT later.  */
10072           if (const_op == 0 && equality_comparison_p
10073               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10074               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10075             {
10076               op0 = simplify_and_const_int
10077                 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
10078                                              XEXP (op0, 1),
10079                                              XEXP (XEXP (op0, 0), 1)),
10080                  (HOST_WIDE_INT) 1);
10081               continue;
10082             }
10083
10084           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10085              zero and X is a comparison and C1 and C2 describe only bits set
10086              in STORE_FLAG_VALUE, we can compare with X.  */
10087           if (const_op == 0 && equality_comparison_p
10088               && mode_width <= HOST_BITS_PER_WIDE_INT
10089               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10090               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10091               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10092               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10093               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10094             {
10095               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10096                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10097               if ((~ STORE_FLAG_VALUE & mask) == 0
10098                   && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10099                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10100                           && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10101                 {
10102                   op0 = XEXP (XEXP (op0, 0), 0);
10103                   continue;
10104                 }
10105             }
10106
10107           /* If we are doing an equality comparison of an AND of a bit equal
10108              to the sign bit, replace this with a LT or GE comparison of
10109              the underlying value.  */
10110           if (equality_comparison_p
10111               && const_op == 0
10112               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10113               && mode_width <= HOST_BITS_PER_WIDE_INT
10114               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10115                   == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10116             {
10117               op0 = XEXP (op0, 0);
10118               code = (code == EQ ? GE : LT);
10119               continue;
10120             }
10121
10122           /* If this AND operation is really a ZERO_EXTEND from a narrower
10123              mode, the constant fits within that mode, and this is either an
10124              equality or unsigned comparison, try to do this comparison in
10125              the narrower mode.  */
10126           if ((equality_comparison_p || unsigned_comparison_p)
10127               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10128               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10129                                    & GET_MODE_MASK (mode))
10130                                   + 1)) >= 0
10131               && const_op >> i == 0
10132               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10133             {
10134               op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
10135               continue;
10136             }
10137
10138           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1 fits
10139              in both M1 and M2 and the SUBREG is either paradoxical or
10140              represents the low part, permute the SUBREG and the AND and
10141              try again.  */
10142           if (GET_CODE (XEXP (op0, 0)) == SUBREG
10143               && ((mode_width
10144                    >= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0)))))
10145                   || subreg_lowpart_p (XEXP (op0, 0)))
10146               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10147               && mode_width <= HOST_BITS_PER_WIDE_INT
10148               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10149                   <= HOST_BITS_PER_WIDE_INT)
10150               && (INTVAL (XEXP (op0, 1)) & ~ mask) == 0
10151               && 0 == (~ GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10152                        & INTVAL (XEXP (op0, 1))))
10153                        
10154             {
10155               op0
10156                 = gen_lowpart_for_combine
10157                   (mode,
10158                    gen_binary (AND, GET_MODE (SUBREG_REG (XEXP (op0, 0))),
10159                                SUBREG_REG (XEXP (op0, 0)), XEXP (op0, 1)));
10160               continue;
10161             }
10162
10163           break;
10164
10165         case ASHIFT:
10166           /* If we have (compare (ashift FOO N) (const_int C)) and
10167              the high order N bits of FOO (N+1 if an inequality comparison)
10168              are known to be zero, we can do this by comparing FOO with C
10169              shifted right N bits so long as the low-order N bits of C are
10170              zero.  */
10171           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10172               && INTVAL (XEXP (op0, 1)) >= 0
10173               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10174                   < HOST_BITS_PER_WIDE_INT)
10175               && ((const_op
10176                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10177               && mode_width <= HOST_BITS_PER_WIDE_INT
10178               && (nonzero_bits (XEXP (op0, 0), mode)
10179                   & ~ (mask >> (INTVAL (XEXP (op0, 1))
10180                                 + ! equality_comparison_p))) == 0)
10181             {
10182               const_op >>= INTVAL (XEXP (op0, 1));
10183               op1 = GEN_INT (const_op);
10184               op0 = XEXP (op0, 0);
10185               continue;
10186             }
10187
10188           /* If we are doing a sign bit comparison, it means we are testing
10189              a particular bit.  Convert it to the appropriate AND.  */
10190           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10191               && mode_width <= HOST_BITS_PER_WIDE_INT)
10192             {
10193               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10194                                             ((HOST_WIDE_INT) 1
10195                                              << (mode_width - 1
10196                                                  - INTVAL (XEXP (op0, 1)))));
10197               code = (code == LT ? NE : EQ);
10198               continue;
10199             }
10200
10201           /* If this an equality comparison with zero and we are shifting
10202              the low bit to the sign bit, we can convert this to an AND of the
10203              low-order bit.  */
10204           if (const_op == 0 && equality_comparison_p
10205               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10206               && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10207             {
10208               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10209                                             (HOST_WIDE_INT) 1);
10210               continue;
10211             }
10212           break;
10213
10214         case ASHIFTRT:
10215           /* If this is an equality comparison with zero, we can do this
10216              as a logical shift, which might be much simpler.  */
10217           if (equality_comparison_p && const_op == 0
10218               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10219             {
10220               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10221                                           XEXP (op0, 0),
10222                                           INTVAL (XEXP (op0, 1)));
10223               continue;
10224             }
10225
10226           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10227              do the comparison in a narrower mode.  */
10228           if (! unsigned_comparison_p
10229               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10230               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10231               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10232               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10233                                          MODE_INT, 1)) != BLKmode
10234               && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10235                   || ((unsigned HOST_WIDE_INT) - const_op
10236                       <= GET_MODE_MASK (tmode))))
10237             {
10238               op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
10239               continue;
10240             }
10241
10242           /* ... fall through ...  */
10243         case LSHIFTRT:
10244           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10245              the low order N bits of FOO are known to be zero, we can do this
10246              by comparing FOO with C shifted left N bits so long as no
10247              overflow occurs.  */
10248           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10249               && INTVAL (XEXP (op0, 1)) >= 0
10250               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10251               && mode_width <= HOST_BITS_PER_WIDE_INT
10252               && (nonzero_bits (XEXP (op0, 0), mode)
10253                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10254               && (const_op == 0
10255                   || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
10256                       < mode_width)))
10257             {
10258               const_op <<= INTVAL (XEXP (op0, 1));
10259               op1 = GEN_INT (const_op);
10260               op0 = XEXP (op0, 0);
10261               continue;
10262             }
10263
10264           /* If we are using this shift to extract just the sign bit, we
10265              can replace this with an LT or GE comparison.  */
10266           if (const_op == 0
10267               && (equality_comparison_p || sign_bit_comparison_p)
10268               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10269               && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10270             {
10271               op0 = XEXP (op0, 0);
10272               code = (code == NE || code == GT ? LT : GE);
10273               continue;
10274             }
10275           break;
10276           
10277         default:
10278           break;
10279         }
10280
10281       break;
10282     }
10283
10284   /* Now make any compound operations involved in this comparison.  Then,
10285      check for an outmost SUBREG on OP0 that is not doing anything or is
10286      paradoxical.  The latter case can only occur when it is known that the
10287      "extra" bits will be zero.  Therefore, it is safe to remove the SUBREG.
10288      We can never remove a SUBREG for a non-equality comparison because the
10289      sign bit is in a different place in the underlying object.  */
10290
10291   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10292   op1 = make_compound_operation (op1, SET);
10293
10294   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10295       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10296       && (code == NE || code == EQ)
10297       && ((GET_MODE_SIZE (GET_MODE (op0))
10298            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
10299     {
10300       op0 = SUBREG_REG (op0);
10301       op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
10302     }
10303
10304   else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10305            && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10306            && (code == NE || code == EQ)
10307            && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10308                <= HOST_BITS_PER_WIDE_INT)
10309            && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
10310                & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
10311            && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
10312                                               op1),
10313                (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10314                 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
10315     op0 = SUBREG_REG (op0), op1 = tem;
10316
10317   /* We now do the opposite procedure: Some machines don't have compare
10318      insns in all modes.  If OP0's mode is an integer mode smaller than a
10319      word and we can't do a compare in that mode, see if there is a larger
10320      mode for which we can do the compare.  There are a number of cases in
10321      which we can use the wider mode.  */
10322
10323   mode = GET_MODE (op0);
10324   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10325       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10326       && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
10327     for (tmode = GET_MODE_WIDER_MODE (mode);
10328          (tmode != VOIDmode
10329           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10330          tmode = GET_MODE_WIDER_MODE (tmode))
10331       if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
10332         {
10333           /* If the only nonzero bits in OP0 and OP1 are those in the
10334              narrower mode and this is an equality or unsigned comparison,
10335              we can use the wider mode.  Similarly for sign-extended
10336              values, in which case it is true for all comparisons.  */
10337           if (((code == EQ || code == NE
10338                 || code == GEU || code == GTU || code == LEU || code == LTU)
10339                && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
10340                && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
10341               || ((num_sign_bit_copies (op0, tmode)
10342                    > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
10343                   && (num_sign_bit_copies (op1, tmode)
10344                       > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
10345             {
10346               op0 = gen_lowpart_for_combine (tmode, op0);
10347               op1 = gen_lowpart_for_combine (tmode, op1);
10348               break;
10349             }
10350
10351           /* If this is a test for negative, we can make an explicit
10352              test of the sign bit.  */
10353
10354           if (op1 == const0_rtx && (code == LT || code == GE)
10355               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10356             {
10357               op0 = gen_binary (AND, tmode,
10358                                 gen_lowpart_for_combine (tmode, op0),
10359                                 GEN_INT ((HOST_WIDE_INT) 1
10360                                          << (GET_MODE_BITSIZE (mode) - 1)));
10361               code = (code == LT) ? NE : EQ;
10362               break;
10363             }
10364         }
10365
10366 #ifdef CANONICALIZE_COMPARISON
10367   /* If this machine only supports a subset of valid comparisons, see if we
10368      can convert an unsupported one into a supported one.  */
10369   CANONICALIZE_COMPARISON (code, op0, op1);
10370 #endif
10371
10372   *pop0 = op0;
10373   *pop1 = op1;
10374
10375   return code;
10376 }
10377 \f
10378 /* Return 1 if we know that X, a comparison operation, is not operating
10379    on a floating-point value or is EQ or NE, meaning that we can safely
10380    reverse it.  */
10381
10382 static int
10383 reversible_comparison_p (x)
10384      rtx x;
10385 {
10386   if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
10387       || flag_fast_math
10388       || GET_CODE (x) == NE || GET_CODE (x) == EQ)
10389     return 1;
10390
10391   switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
10392     {
10393     case MODE_INT:
10394     case MODE_PARTIAL_INT:
10395     case MODE_COMPLEX_INT:
10396       return 1;
10397
10398     case MODE_CC:
10399       /* If the mode of the condition codes tells us that this is safe,
10400          we need look no further.  */
10401       if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
10402         return 1;
10403
10404       /* Otherwise try and find where the condition codes were last set and
10405          use that.  */
10406       x = get_last_value (XEXP (x, 0));
10407       return (x && GET_CODE (x) == COMPARE
10408               && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
10409       
10410     default:
10411       return 0;
10412     }
10413 }
10414 \f
10415 /* Utility function for following routine.  Called when X is part of a value
10416    being stored into reg_last_set_value.  Sets reg_last_set_table_tick
10417    for each register mentioned.  Similar to mention_regs in cse.c  */
10418
10419 static void
10420 update_table_tick (x)
10421      rtx x;
10422 {
10423   register enum rtx_code code = GET_CODE (x);
10424   register char *fmt = GET_RTX_FORMAT (code);
10425   register int i;
10426
10427   if (code == REG)
10428     {
10429       int regno = REGNO (x);
10430       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10431                               ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10432
10433       for (i = regno; i < endregno; i++)
10434         reg_last_set_table_tick[i] = label_tick;
10435
10436       return;
10437     }
10438   
10439   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10440     /* Note that we can't have an "E" in values stored; see
10441        get_last_value_validate.  */
10442     if (fmt[i] == 'e')
10443       update_table_tick (XEXP (x, i));
10444 }
10445
10446 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
10447    are saying that the register is clobbered and we no longer know its
10448    value.  If INSN is zero, don't update reg_last_set; this is only permitted
10449    with VALUE also zero and is used to invalidate the register.  */
10450
10451 static void
10452 record_value_for_reg (reg, insn, value)
10453      rtx reg;
10454      rtx insn;
10455      rtx value;
10456 {
10457   int regno = REGNO (reg);
10458   int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10459                           ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
10460   int i;
10461
10462   /* If VALUE contains REG and we have a previous value for REG, substitute
10463      the previous value.  */
10464   if (value && insn && reg_overlap_mentioned_p (reg, value))
10465     {
10466       rtx tem;
10467
10468       /* Set things up so get_last_value is allowed to see anything set up to
10469          our insn.  */
10470       subst_low_cuid = INSN_CUID (insn);
10471       tem = get_last_value (reg);      
10472
10473       if (tem)
10474         value = replace_rtx (copy_rtx (value), reg, tem);
10475     }
10476
10477   /* For each register modified, show we don't know its value, that
10478      we don't know about its bitwise content, that its value has been
10479      updated, and that we don't know the location of the death of the
10480      register.  */
10481   for (i = regno; i < endregno; i ++)
10482     {
10483       if (insn)
10484         reg_last_set[i] = insn;
10485       reg_last_set_value[i] = 0;
10486       reg_last_set_mode[i] = 0;
10487       reg_last_set_nonzero_bits[i] = 0;
10488       reg_last_set_sign_bit_copies[i] = 0;
10489       reg_last_death[i] = 0;
10490     }
10491
10492   /* Mark registers that are being referenced in this value.  */
10493   if (value)
10494     update_table_tick (value);
10495
10496   /* Now update the status of each register being set.
10497      If someone is using this register in this block, set this register
10498      to invalid since we will get confused between the two lives in this
10499      basic block.  This makes using this register always invalid.  In cse, we
10500      scan the table to invalidate all entries using this register, but this
10501      is too much work for us.  */
10502
10503   for (i = regno; i < endregno; i++)
10504     {
10505       reg_last_set_label[i] = label_tick;
10506       if (value && reg_last_set_table_tick[i] == label_tick)
10507         reg_last_set_invalid[i] = 1;
10508       else
10509         reg_last_set_invalid[i] = 0;
10510     }
10511
10512   /* The value being assigned might refer to X (like in "x++;").  In that
10513      case, we must replace it with (clobber (const_int 0)) to prevent
10514      infinite loops.  */
10515   if (value && ! get_last_value_validate (&value, insn,
10516                                           reg_last_set_label[regno], 0))
10517     {
10518       value = copy_rtx (value);
10519       if (! get_last_value_validate (&value, insn,
10520                                      reg_last_set_label[regno], 1))
10521         value = 0;
10522     }
10523
10524   /* For the main register being modified, update the value, the mode, the
10525      nonzero bits, and the number of sign bit copies.  */
10526
10527   reg_last_set_value[regno] = value;
10528
10529   if (value)
10530     {
10531       subst_low_cuid = INSN_CUID (insn);
10532       reg_last_set_mode[regno] = GET_MODE (reg);
10533       reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
10534       reg_last_set_sign_bit_copies[regno]
10535         = num_sign_bit_copies (value, GET_MODE (reg));
10536     }
10537 }
10538
10539 /* Used for communication between the following two routines.  */
10540 static rtx record_dead_insn;
10541
10542 /* Called via note_stores from record_dead_and_set_regs to handle one
10543    SET or CLOBBER in an insn.  */
10544
10545 static void
10546 record_dead_and_set_regs_1 (dest, setter)
10547      rtx dest, setter;
10548 {
10549   if (GET_CODE (dest) == SUBREG)
10550     dest = SUBREG_REG (dest);
10551
10552   if (GET_CODE (dest) == REG)
10553     {
10554       /* If we are setting the whole register, we know its value.  Otherwise
10555          show that we don't know the value.  We can handle SUBREG in
10556          some cases.  */
10557       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10558         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10559       else if (GET_CODE (setter) == SET
10560                && GET_CODE (SET_DEST (setter)) == SUBREG
10561                && SUBREG_REG (SET_DEST (setter)) == dest
10562                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10563                && subreg_lowpart_p (SET_DEST (setter)))
10564         record_value_for_reg (dest, record_dead_insn,
10565                               gen_lowpart_for_combine (GET_MODE (dest),
10566                                                        SET_SRC (setter)));
10567       else
10568         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10569     }
10570   else if (GET_CODE (dest) == MEM
10571            /* Ignore pushes, they clobber nothing.  */
10572            && ! push_operand (dest, GET_MODE (dest)))
10573     mem_last_set = INSN_CUID (record_dead_insn);
10574 }
10575
10576 /* Update the records of when each REG was most recently set or killed
10577    for the things done by INSN.  This is the last thing done in processing
10578    INSN in the combiner loop.
10579
10580    We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10581    reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10582    and also the similar information mem_last_set (which insn most recently
10583    modified memory) and last_call_cuid (which insn was the most recent
10584    subroutine call).  */
10585
10586 static void
10587 record_dead_and_set_regs (insn)
10588      rtx insn;
10589 {
10590   register rtx link;
10591   int i;
10592
10593   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10594     {
10595       if (REG_NOTE_KIND (link) == REG_DEAD
10596           && GET_CODE (XEXP (link, 0)) == REG)
10597         {
10598           int regno = REGNO (XEXP (link, 0));
10599           int endregno
10600             = regno + (regno < FIRST_PSEUDO_REGISTER
10601                        ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10602                        : 1);
10603
10604           for (i = regno; i < endregno; i++)
10605             reg_last_death[i] = insn;
10606         }
10607       else if (REG_NOTE_KIND (link) == REG_INC)
10608         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10609     }
10610
10611   if (GET_CODE (insn) == CALL_INSN)
10612     {
10613       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10614         if (call_used_regs[i])
10615           {
10616             reg_last_set_value[i] = 0;
10617             reg_last_set_mode[i] = 0;
10618             reg_last_set_nonzero_bits[i] = 0;
10619             reg_last_set_sign_bit_copies[i] = 0;
10620             reg_last_death[i] = 0;
10621           }
10622
10623       last_call_cuid = mem_last_set = INSN_CUID (insn);
10624     }
10625
10626   record_dead_insn = insn;
10627   note_stores (PATTERN (insn), record_dead_and_set_regs_1);
10628 }
10629 \f
10630 /* Utility routine for the following function.  Verify that all the registers
10631    mentioned in *LOC are valid when *LOC was part of a value set when
10632    label_tick == TICK.  Return 0 if some are not.
10633
10634    If REPLACE is non-zero, replace the invalid reference with
10635    (clobber (const_int 0)) and return 1.  This replacement is useful because
10636    we often can get useful information about the form of a value (e.g., if
10637    it was produced by a shift that always produces -1 or 0) even though
10638    we don't know exactly what registers it was produced from.  */
10639
10640 static int
10641 get_last_value_validate (loc, insn, tick, replace)
10642      rtx *loc;
10643      rtx insn;
10644      int tick;
10645      int replace;
10646 {
10647   rtx x = *loc;
10648   char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10649   int len = GET_RTX_LENGTH (GET_CODE (x));
10650   int i;
10651
10652   if (GET_CODE (x) == REG)
10653     {
10654       int regno = REGNO (x);
10655       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10656                               ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10657       int j;
10658
10659       for (j = regno; j < endregno; j++)
10660         if (reg_last_set_invalid[j]
10661             /* If this is a pseudo-register that was only set once, it is
10662                always valid.  */
10663             || (! (regno >= FIRST_PSEUDO_REGISTER && REG_N_SETS (regno) == 1)
10664                 && reg_last_set_label[j] > tick))
10665           {
10666             if (replace)
10667               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10668             return replace;
10669           }
10670
10671       return 1;
10672     }
10673   /* If this is a memory reference, make sure that there were
10674      no stores after it that might have clobbered the value.  We don't
10675      have alias info, so we assume any store invalidates it.  */
10676   else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
10677            && INSN_CUID (insn) <= mem_last_set)
10678     {
10679       if (replace)
10680         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10681       return replace;
10682     }
10683
10684   for (i = 0; i < len; i++)
10685     if ((fmt[i] == 'e'
10686          && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
10687         /* Don't bother with these.  They shouldn't occur anyway.  */
10688         || fmt[i] == 'E')
10689       return 0;
10690
10691   /* If we haven't found a reason for it to be invalid, it is valid.  */
10692   return 1;
10693 }
10694
10695 /* Get the last value assigned to X, if known.  Some registers
10696    in the value may be replaced with (clobber (const_int 0)) if their value
10697    is known longer known reliably.  */
10698
10699 static rtx
10700 get_last_value (x)
10701      rtx x;
10702 {
10703   int regno;
10704   rtx value;
10705
10706   /* If this is a non-paradoxical SUBREG, get the value of its operand and
10707      then convert it to the desired mode.  If this is a paradoxical SUBREG,
10708      we cannot predict what values the "extra" bits might have.  */
10709   if (GET_CODE (x) == SUBREG
10710       && subreg_lowpart_p (x)
10711       && (GET_MODE_SIZE (GET_MODE (x))
10712           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
10713       && (value = get_last_value (SUBREG_REG (x))) != 0)
10714     return gen_lowpart_for_combine (GET_MODE (x), value);
10715
10716   if (GET_CODE (x) != REG)
10717     return 0;
10718
10719   regno = REGNO (x);
10720   value = reg_last_set_value[regno];
10721
10722   /* If we don't have a value or if it isn't for this basic block,
10723      return 0.  */
10724
10725   if (value == 0
10726       || (REG_N_SETS (regno) != 1
10727           && reg_last_set_label[regno] != label_tick))
10728     return 0;
10729
10730   /* If the value was set in a later insn than the ones we are processing,
10731      we can't use it even if the register was only set once, but make a quick
10732      check to see if the previous insn set it to something.  This is commonly
10733      the case when the same pseudo is used by repeated insns.
10734
10735      This does not work if there exists an instruction which is temporarily
10736      not on the insn chain.  */
10737
10738   if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
10739     {
10740       rtx insn, set;
10741
10742       /* We can not do anything useful in this case, because there is
10743          an instruction which is not on the insn chain.  */
10744       if (subst_prev_insn)
10745         return 0;
10746
10747       /* Skip over USE insns.  They are not useful here, and they may have
10748          been made by combine, in which case they do not have a INSN_CUID
10749          value.  We can't use prev_real_insn, because that would incorrectly
10750          take us backwards across labels.  Skip over BARRIERs also, since
10751          they could have been made by combine.  If we see one, we must be
10752          optimizing dead code, so it doesn't matter what we do.  */
10753       for (insn = prev_nonnote_insn (subst_insn);
10754            insn && ((GET_CODE (insn) == INSN
10755                      && GET_CODE (PATTERN (insn)) == USE)
10756                     || GET_CODE (insn) == BARRIER
10757                     || INSN_CUID (insn) >= subst_low_cuid);
10758            insn = prev_nonnote_insn (insn))
10759         ;
10760
10761       if (insn
10762           && (set = single_set (insn)) != 0
10763           && rtx_equal_p (SET_DEST (set), x))
10764         {
10765           value = SET_SRC (set);
10766
10767           /* Make sure that VALUE doesn't reference X.  Replace any
10768              explicit references with a CLOBBER.  If there are any remaining
10769              references (rare), don't use the value.  */
10770
10771           if (reg_mentioned_p (x, value))
10772             value = replace_rtx (copy_rtx (value), x,
10773                                  gen_rtx_CLOBBER (GET_MODE (x), const0_rtx));
10774
10775           if (reg_overlap_mentioned_p (x, value))
10776             return 0;
10777         }
10778       else
10779         return 0;
10780     }
10781
10782   /* If the value has all its registers valid, return it.  */
10783   if (get_last_value_validate (&value, reg_last_set[regno],
10784                                reg_last_set_label[regno], 0))
10785     return value;
10786
10787   /* Otherwise, make a copy and replace any invalid register with
10788      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
10789
10790   value = copy_rtx (value);
10791   if (get_last_value_validate (&value, reg_last_set[regno],
10792                                reg_last_set_label[regno], 1))
10793     return value;
10794
10795   return 0;
10796 }
10797 \f
10798 /* Return nonzero if expression X refers to a REG or to memory
10799    that is set in an instruction more recent than FROM_CUID.  */
10800
10801 static int
10802 use_crosses_set_p (x, from_cuid)
10803      register rtx x;
10804      int from_cuid;
10805 {
10806   register char *fmt;
10807   register int i;
10808   register enum rtx_code code = GET_CODE (x);
10809
10810   if (code == REG)
10811     {
10812       register int regno = REGNO (x);
10813       int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
10814                             ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10815       
10816 #ifdef PUSH_ROUNDING
10817       /* Don't allow uses of the stack pointer to be moved,
10818          because we don't know whether the move crosses a push insn.  */
10819       if (regno == STACK_POINTER_REGNUM)
10820         return 1;
10821 #endif
10822       for (;regno < endreg; regno++)
10823         if (reg_last_set[regno]
10824             && INSN_CUID (reg_last_set[regno]) > from_cuid)
10825           return 1;
10826       return 0;
10827     }
10828
10829   if (code == MEM && mem_last_set > from_cuid)
10830     return 1;
10831
10832   fmt = GET_RTX_FORMAT (code);
10833
10834   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10835     {
10836       if (fmt[i] == 'E')
10837         {
10838           register int j;
10839           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10840             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
10841               return 1;
10842         }
10843       else if (fmt[i] == 'e'
10844                && use_crosses_set_p (XEXP (x, i), from_cuid))
10845         return 1;
10846     }
10847   return 0;
10848 }
10849 \f
10850 /* Define three variables used for communication between the following
10851    routines.  */
10852
10853 static int reg_dead_regno, reg_dead_endregno;
10854 static int reg_dead_flag;
10855
10856 /* Function called via note_stores from reg_dead_at_p.
10857
10858    If DEST is within [reg_dead_regno, reg_dead_endregno), set 
10859    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
10860
10861 static void
10862 reg_dead_at_p_1 (dest, x)
10863      rtx dest;
10864      rtx x;
10865 {
10866   int regno, endregno;
10867
10868   if (GET_CODE (dest) != REG)
10869     return;
10870
10871   regno = REGNO (dest);
10872   endregno = regno + (regno < FIRST_PSEUDO_REGISTER 
10873                       ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
10874
10875   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
10876     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
10877 }
10878
10879 /* Return non-zero if REG is known to be dead at INSN.
10880
10881    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
10882    referencing REG, it is dead.  If we hit a SET referencing REG, it is
10883    live.  Otherwise, see if it is live or dead at the start of the basic
10884    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
10885    must be assumed to be always live.  */
10886
10887 static int
10888 reg_dead_at_p (reg, insn)
10889      rtx reg;
10890      rtx insn;
10891 {
10892   int block, i;
10893
10894   /* Set variables for reg_dead_at_p_1.  */
10895   reg_dead_regno = REGNO (reg);
10896   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
10897                                         ? HARD_REGNO_NREGS (reg_dead_regno,
10898                                                             GET_MODE (reg))
10899                                         : 1);
10900
10901   reg_dead_flag = 0;
10902
10903   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  */
10904   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
10905     {
10906       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10907         if (TEST_HARD_REG_BIT (newpat_used_regs, i))
10908           return 0;
10909     }
10910
10911   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
10912      beginning of function.  */
10913   for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
10914        insn = prev_nonnote_insn (insn))
10915     {
10916       note_stores (PATTERN (insn), reg_dead_at_p_1);
10917       if (reg_dead_flag)
10918         return reg_dead_flag == 1 ? 1 : 0;
10919
10920       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
10921         return 1;
10922     }
10923
10924   /* Get the basic block number that we were in.  */
10925   if (insn == 0)
10926     block = 0;
10927   else
10928     {
10929       for (block = 0; block < n_basic_blocks; block++)
10930         if (insn == basic_block_head[block])
10931           break;
10932
10933       if (block == n_basic_blocks)
10934         return 0;
10935     }
10936
10937   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10938     if (REGNO_REG_SET_P (basic_block_live_at_start[block], i))
10939       return 0;
10940
10941   return 1;
10942 }
10943 \f
10944 /* Note hard registers in X that are used.  This code is similar to
10945    that in flow.c, but much simpler since we don't care about pseudos.  */
10946
10947 static void
10948 mark_used_regs_combine (x)
10949      rtx x;
10950 {
10951   register RTX_CODE code = GET_CODE (x);
10952   register int regno;
10953   int i;
10954
10955   switch (code)
10956     {
10957     case LABEL_REF:
10958     case SYMBOL_REF:
10959     case CONST_INT:
10960     case CONST:
10961     case CONST_DOUBLE:
10962     case PC:
10963     case ADDR_VEC:
10964     case ADDR_DIFF_VEC:
10965     case ASM_INPUT:
10966 #ifdef HAVE_cc0
10967     /* CC0 must die in the insn after it is set, so we don't need to take
10968        special note of it here.  */
10969     case CC0:
10970 #endif
10971       return;
10972
10973     case CLOBBER:
10974       /* If we are clobbering a MEM, mark any hard registers inside the
10975          address as used.  */
10976       if (GET_CODE (XEXP (x, 0)) == MEM)
10977         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
10978       return;
10979
10980     case REG:
10981       regno = REGNO (x);
10982       /* A hard reg in a wide mode may really be multiple registers.
10983          If so, mark all of them just like the first.  */
10984       if (regno < FIRST_PSEUDO_REGISTER)
10985         {
10986           /* None of this applies to the stack, frame or arg pointers */
10987           if (regno == STACK_POINTER_REGNUM
10988 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10989               || regno == HARD_FRAME_POINTER_REGNUM
10990 #endif
10991 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
10992               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
10993 #endif
10994               || regno == FRAME_POINTER_REGNUM)
10995             return;
10996
10997           i = HARD_REGNO_NREGS (regno, GET_MODE (x));
10998           while (i-- > 0)
10999             SET_HARD_REG_BIT (newpat_used_regs, regno + i);
11000         }
11001       return;
11002
11003     case SET:
11004       {
11005         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11006            the address.  */
11007         register rtx testreg = SET_DEST (x);
11008
11009         while (GET_CODE (testreg) == SUBREG
11010                || GET_CODE (testreg) == ZERO_EXTRACT
11011                || GET_CODE (testreg) == SIGN_EXTRACT
11012                || GET_CODE (testreg) == STRICT_LOW_PART)
11013           testreg = XEXP (testreg, 0);
11014
11015         if (GET_CODE (testreg) == MEM)
11016           mark_used_regs_combine (XEXP (testreg, 0));
11017
11018         mark_used_regs_combine (SET_SRC (x));
11019       }
11020       return;
11021
11022     default:
11023       break;
11024     }
11025
11026   /* Recursively scan the operands of this expression.  */
11027
11028   {
11029     register char *fmt = GET_RTX_FORMAT (code);
11030
11031     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11032       {
11033         if (fmt[i] == 'e')
11034           mark_used_regs_combine (XEXP (x, i));
11035         else if (fmt[i] == 'E')
11036           {
11037             register int j;
11038
11039             for (j = 0; j < XVECLEN (x, i); j++)
11040               mark_used_regs_combine (XVECEXP (x, i, j));
11041           }
11042       }
11043   }
11044 }
11045
11046 \f
11047 /* Remove register number REGNO from the dead registers list of INSN.
11048
11049    Return the note used to record the death, if there was one.  */
11050
11051 rtx
11052 remove_death (regno, insn)
11053      int regno;
11054      rtx insn;
11055 {
11056   register rtx note = find_regno_note (insn, REG_DEAD, regno);
11057
11058   if (note)
11059     {
11060       REG_N_DEATHS (regno)--;
11061       remove_note (insn, note);
11062     }
11063
11064   return note;
11065 }
11066
11067 /* For each register (hardware or pseudo) used within expression X, if its
11068    death is in an instruction with cuid between FROM_CUID (inclusive) and
11069    TO_INSN (exclusive), put a REG_DEAD note for that register in the
11070    list headed by PNOTES. 
11071
11072    That said, don't move registers killed by maybe_kill_insn.
11073
11074    This is done when X is being merged by combination into TO_INSN.  These
11075    notes will then be distributed as needed.  */
11076
11077 static void
11078 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
11079      rtx x;
11080      rtx maybe_kill_insn;
11081      int from_cuid;
11082      rtx to_insn;
11083      rtx *pnotes;
11084 {
11085   register char *fmt;
11086   register int len, i;
11087   register enum rtx_code code = GET_CODE (x);
11088
11089   if (code == REG)
11090     {
11091       register int regno = REGNO (x);
11092       register rtx where_dead = reg_last_death[regno];
11093       register rtx before_dead, after_dead;
11094
11095       /* Don't move the register if it gets killed in between from and to */
11096       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11097           && !reg_referenced_p (x, maybe_kill_insn))
11098         return;
11099
11100       /* WHERE_DEAD could be a USE insn made by combine, so first we
11101          make sure that we have insns with valid INSN_CUID values.  */
11102       before_dead = where_dead;
11103       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11104         before_dead = PREV_INSN (before_dead);
11105       after_dead = where_dead;
11106       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11107         after_dead = NEXT_INSN (after_dead);
11108
11109       if (before_dead && after_dead
11110           && INSN_CUID (before_dead) >= from_cuid
11111           && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11112               || (where_dead != after_dead
11113                   && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11114         {
11115           rtx note = remove_death (regno, where_dead);
11116
11117           /* It is possible for the call above to return 0.  This can occur
11118              when reg_last_death points to I2 or I1 that we combined with.
11119              In that case make a new note.
11120
11121              We must also check for the case where X is a hard register
11122              and NOTE is a death note for a range of hard registers
11123              including X.  In that case, we must put REG_DEAD notes for
11124              the remaining registers in place of NOTE.  */
11125
11126           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11127               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11128                   > GET_MODE_SIZE (GET_MODE (x))))
11129             {
11130               int deadregno = REGNO (XEXP (note, 0));
11131               int deadend
11132                 = (deadregno + HARD_REGNO_NREGS (deadregno,
11133                                                  GET_MODE (XEXP (note, 0))));
11134               int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11135               int i;
11136
11137               for (i = deadregno; i < deadend; i++)
11138                 if (i < regno || i >= ourend)
11139                   REG_NOTES (where_dead)
11140                     = gen_rtx_EXPR_LIST (REG_DEAD,
11141                                          gen_rtx_REG (reg_raw_mode[i], i),
11142                                          REG_NOTES (where_dead));
11143             }
11144           /* If we didn't find any note, or if we found a REG_DEAD note that
11145              covers only part of the given reg, and we have a multi-reg hard
11146              register, then to be safe we must check for REG_DEAD notes
11147              for each register other than the first.  They could have
11148              their own REG_DEAD notes lying around.  */
11149           else if ((note == 0
11150                     || (note != 0
11151                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11152                             < GET_MODE_SIZE (GET_MODE (x)))))
11153                    && regno < FIRST_PSEUDO_REGISTER
11154                    && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
11155             {
11156               int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11157               int i, offset;
11158               rtx oldnotes = 0;
11159
11160               if (note)
11161                 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
11162               else
11163                 offset = 1;
11164
11165               for (i = regno + offset; i < ourend; i++)
11166                 move_deaths (gen_rtx_REG (reg_raw_mode[i], i),
11167                              maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11168             }
11169
11170           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11171             {
11172               XEXP (note, 1) = *pnotes;
11173               *pnotes = note;
11174             }
11175           else
11176             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11177
11178           REG_N_DEATHS (regno)++;
11179         }
11180
11181       return;
11182     }
11183
11184   else if (GET_CODE (x) == SET)
11185     {
11186       rtx dest = SET_DEST (x);
11187
11188       move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11189
11190       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11191          that accesses one word of a multi-word item, some
11192          piece of everything register in the expression is used by
11193          this insn, so remove any old death.  */
11194
11195       if (GET_CODE (dest) == ZERO_EXTRACT
11196           || GET_CODE (dest) == STRICT_LOW_PART
11197           || (GET_CODE (dest) == SUBREG
11198               && (((GET_MODE_SIZE (GET_MODE (dest))
11199                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11200                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11201                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11202         {
11203           move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11204           return;
11205         }
11206
11207       /* If this is some other SUBREG, we know it replaces the entire
11208          value, so use that as the destination.  */
11209       if (GET_CODE (dest) == SUBREG)
11210         dest = SUBREG_REG (dest);
11211
11212       /* If this is a MEM, adjust deaths of anything used in the address.
11213          For a REG (the only other possibility), the entire value is
11214          being replaced so the old value is not used in this insn.  */
11215
11216       if (GET_CODE (dest) == MEM)
11217         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11218                      to_insn, pnotes);
11219       return;
11220     }
11221
11222   else if (GET_CODE (x) == CLOBBER)
11223     return;
11224
11225   len = GET_RTX_LENGTH (code);
11226   fmt = GET_RTX_FORMAT (code);
11227
11228   for (i = 0; i < len; i++)
11229     {
11230       if (fmt[i] == 'E')
11231         {
11232           register int j;
11233           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11234             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11235                          to_insn, pnotes);
11236         }
11237       else if (fmt[i] == 'e')
11238         move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11239     }
11240 }
11241 \f
11242 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11243    pattern of an insn.  X must be a REG.  */
11244
11245 static int
11246 reg_bitfield_target_p (x, body)
11247      rtx x;
11248      rtx body;
11249 {
11250   int i;
11251
11252   if (GET_CODE (body) == SET)
11253     {
11254       rtx dest = SET_DEST (body);
11255       rtx target;
11256       int regno, tregno, endregno, endtregno;
11257
11258       if (GET_CODE (dest) == ZERO_EXTRACT)
11259         target = XEXP (dest, 0);
11260       else if (GET_CODE (dest) == STRICT_LOW_PART)
11261         target = SUBREG_REG (XEXP (dest, 0));
11262       else
11263         return 0;
11264
11265       if (GET_CODE (target) == SUBREG)
11266         target = SUBREG_REG (target);
11267
11268       if (GET_CODE (target) != REG)
11269         return 0;
11270
11271       tregno = REGNO (target), regno = REGNO (x);
11272       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11273         return target == x;
11274
11275       endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
11276       endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11277
11278       return endregno > tregno && regno < endtregno;
11279     }
11280
11281   else if (GET_CODE (body) == PARALLEL)
11282     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11283       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11284         return 1;
11285
11286   return 0;
11287 }      
11288 \f
11289 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11290    as appropriate.  I3 and I2 are the insns resulting from the combination
11291    insns including FROM (I2 may be zero).
11292
11293    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
11294    not need REG_DEAD notes because they are being substituted for.  This
11295    saves searching in the most common cases.
11296
11297    Each note in the list is either ignored or placed on some insns, depending
11298    on the type of note.  */
11299
11300 static void
11301 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
11302      rtx notes;
11303      rtx from_insn;
11304      rtx i3, i2;
11305      rtx elim_i2, elim_i1;
11306 {
11307   rtx note, next_note;
11308   rtx tem;
11309
11310   for (note = notes; note; note = next_note)
11311     {
11312       rtx place = 0, place2 = 0;
11313
11314       /* If this NOTE references a pseudo register, ensure it references
11315          the latest copy of that register.  */
11316       if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11317           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11318         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11319
11320       next_note = XEXP (note, 1);
11321       switch (REG_NOTE_KIND (note))
11322         {
11323         case REG_BR_PROB:
11324         case REG_EXEC_COUNT:
11325           /* Doesn't matter much where we put this, as long as it's somewhere.
11326              It is preferable to keep these notes on branches, which is most
11327              likely to be i3.  */
11328           place = i3;
11329           break;
11330
11331         case REG_UNUSED:
11332           /* Any clobbers for i3 may still exist, and so we must process
11333              REG_UNUSED notes from that insn.
11334
11335              Any clobbers from i2 or i1 can only exist if they were added by
11336              recog_for_combine.  In that case, recog_for_combine created the
11337              necessary REG_UNUSED notes.  Trying to keep any original
11338              REG_UNUSED notes from these insns can cause incorrect output
11339              if it is for the same register as the original i3 dest.
11340              In that case, we will notice that the register is set in i3,
11341              and then add a REG_UNUSED note for the destination of i3, which
11342              is wrong.  However, it is possible to have REG_UNUSED notes from
11343              i2 or i1 for register which were both used and clobbered, so
11344              we keep notes from i2 or i1 if they will turn into REG_DEAD
11345              notes.  */
11346
11347           /* If this register is set or clobbered in I3, put the note there
11348              unless there is one already.  */
11349           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11350             {
11351               if (from_insn != i3)
11352                 break;
11353
11354               if (! (GET_CODE (XEXP (note, 0)) == REG
11355                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11356                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11357                 place = i3;
11358             }
11359           /* Otherwise, if this register is used by I3, then this register
11360              now dies here, so we must put a REG_DEAD note here unless there
11361              is one already.  */
11362           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11363                    && ! (GET_CODE (XEXP (note, 0)) == REG
11364                          ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
11365                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11366             {
11367               PUT_REG_NOTE_KIND (note, REG_DEAD);
11368               place = i3;
11369             }
11370           break;
11371
11372         case REG_EQUAL:
11373         case REG_EQUIV:
11374         case REG_NONNEG:
11375         case REG_NOALIAS:
11376           /* These notes say something about results of an insn.  We can
11377              only support them if they used to be on I3 in which case they
11378              remain on I3.  Otherwise they are ignored.
11379
11380              If the note refers to an expression that is not a constant, we
11381              must also ignore the note since we cannot tell whether the
11382              equivalence is still true.  It might be possible to do
11383              slightly better than this (we only have a problem if I2DEST
11384              or I1DEST is present in the expression), but it doesn't
11385              seem worth the trouble.  */
11386
11387           if (from_insn == i3
11388               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11389             place = i3;
11390           break;
11391
11392         case REG_INC:
11393         case REG_NO_CONFLICT:
11394         case REG_LABEL:
11395           /* These notes say something about how a register is used.  They must
11396              be present on any use of the register in I2 or I3.  */
11397           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11398             place = i3;
11399
11400           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11401             {
11402               if (place)
11403                 place2 = i2;
11404               else
11405                 place = i2;
11406             }
11407           break;
11408
11409         case REG_WAS_0:
11410           /* It is too much trouble to try to see if this note is still
11411              correct in all situations.  It is better to simply delete it.  */
11412           break;
11413
11414         case REG_RETVAL:
11415           /* If the insn previously containing this note still exists,
11416              put it back where it was.  Otherwise move it to the previous
11417              insn.  Adjust the corresponding REG_LIBCALL note.  */
11418           if (GET_CODE (from_insn) != NOTE)
11419             place = from_insn;
11420           else
11421             {
11422               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
11423               place = prev_real_insn (from_insn);
11424               if (tem && place)
11425                 XEXP (tem, 0) = place;
11426             }
11427           break;
11428
11429         case REG_LIBCALL:
11430           /* This is handled similarly to REG_RETVAL.  */
11431           if (GET_CODE (from_insn) != NOTE)
11432             place = from_insn;
11433           else
11434             {
11435               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
11436               place = next_real_insn (from_insn);
11437               if (tem && place)
11438                 XEXP (tem, 0) = place;
11439             }
11440           break;
11441
11442         case REG_DEAD:
11443           /* If the register is used as an input in I3, it dies there.
11444              Similarly for I2, if it is non-zero and adjacent to I3.
11445
11446              If the register is not used as an input in either I3 or I2
11447              and it is not one of the registers we were supposed to eliminate,
11448              there are two possibilities.  We might have a non-adjacent I2
11449              or we might have somehow eliminated an additional register
11450              from a computation.  For example, we might have had A & B where
11451              we discover that B will always be zero.  In this case we will
11452              eliminate the reference to A.
11453
11454              In both cases, we must search to see if we can find a previous
11455              use of A and put the death note there.  */
11456
11457           if (from_insn
11458               && GET_CODE (from_insn) == CALL_INSN
11459               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11460             place = from_insn;
11461           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
11462             place = i3;
11463           else if (i2 != 0 && next_nonnote_insn (i2) == i3
11464                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11465             place = i2;
11466
11467           if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
11468             break;
11469
11470           /* If the register is used in both I2 and I3 and it dies in I3, 
11471              we might have added another reference to it.  If reg_n_refs
11472              was 2, bump it to 3.  This has to be correct since the 
11473              register must have been set somewhere.  The reason this is
11474              done is because local-alloc.c treats 2 references as a 
11475              special case.  */
11476
11477           if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
11478               && REG_N_REFS (REGNO (XEXP (note, 0)))== 2
11479               && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11480             REG_N_REFS (REGNO (XEXP (note, 0))) = 3;
11481
11482           if (place == 0)
11483             {
11484               for (tem = prev_nonnote_insn (i3);
11485                    place == 0 && tem
11486                    && (GET_CODE (tem) == INSN || GET_CODE (tem) == CALL_INSN);
11487                    tem = prev_nonnote_insn (tem))
11488                 {
11489                   /* If the register is being set at TEM, see if that is all
11490                      TEM is doing.  If so, delete TEM.  Otherwise, make this
11491                      into a REG_UNUSED note instead.  */
11492                   if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11493                     {
11494                       rtx set = single_set (tem);
11495                       rtx inner_dest = 0;
11496
11497                       if (set != 0)
11498                         for (inner_dest = SET_DEST (set);
11499                              GET_CODE (inner_dest) == STRICT_LOW_PART
11500                              || GET_CODE (inner_dest) == SUBREG
11501                              || GET_CODE (inner_dest) == ZERO_EXTRACT;
11502                              inner_dest = XEXP (inner_dest, 0))
11503                           ;
11504
11505                       /* Verify that it was the set, and not a clobber that
11506                          modified the register.  */
11507
11508                       if (set != 0 && ! side_effects_p (SET_SRC (set))
11509                           && rtx_equal_p (XEXP (note, 0), inner_dest))
11510                         {
11511                           /* Move the notes and links of TEM elsewhere.
11512                              This might delete other dead insns recursively. 
11513                              First set the pattern to something that won't use
11514                              any register.  */
11515
11516                           PATTERN (tem) = pc_rtx;
11517
11518                           distribute_notes (REG_NOTES (tem), tem, tem,
11519                                             NULL_RTX, NULL_RTX, NULL_RTX);
11520                           distribute_links (LOG_LINKS (tem));
11521
11522                           PUT_CODE (tem, NOTE);
11523                           NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11524                           NOTE_SOURCE_FILE (tem) = 0;
11525                         }
11526                       /* If the register is both set and used here, put the
11527                          REG_DEAD note here, but place a REG_UNUSED note
11528                          here too unless there already is one.  */
11529                       else if (reg_referenced_p (XEXP (note, 0),
11530                                                  PATTERN (tem)))
11531                         {
11532                           place = tem;
11533
11534                           if (! find_regno_note (tem, REG_UNUSED,
11535                                                  REGNO (XEXP (note, 0))))
11536                             REG_NOTES (tem)
11537                               = gen_rtx (EXPR_LIST, REG_UNUSED, XEXP (note, 0),
11538                                          REG_NOTES (tem));
11539                         }
11540                       else
11541                         {
11542                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
11543                           
11544                           /*  If there isn't already a REG_UNUSED note, put one
11545                               here.  */
11546                           if (! find_regno_note (tem, REG_UNUSED,
11547                                                  REGNO (XEXP (note, 0))))
11548                             place = tem;
11549                           break;
11550                       }
11551                   }
11552                 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11553                          || (GET_CODE (tem) == CALL_INSN
11554                              && find_reg_fusage (tem, USE, XEXP (note, 0))))
11555                   {
11556                     place = tem;
11557
11558                     /* If we are doing a 3->2 combination, and we have a
11559                        register which formerly died in i3 and was not used
11560                        by i2, which now no longer dies in i3 and is used in
11561                        i2 but does not die in i2, and place is between i2
11562                        and i3, then we may need to move a link from place to
11563                        i2.  */
11564                     if (i2 && INSN_UID (place) <= max_uid_cuid
11565                         && INSN_CUID (place) > INSN_CUID (i2)
11566                         && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
11567                         && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11568                       {
11569                         rtx links = LOG_LINKS (place);
11570                         LOG_LINKS (place) = 0;
11571                         distribute_links (links);
11572                       }
11573                     break;
11574                   }
11575                 }
11576               
11577               /* If we haven't found an insn for the death note and it
11578                  is still a REG_DEAD note, but we have hit a CODE_LABEL,
11579                  insert a USE insn for the register at that label and
11580                  put the death node there.  This prevents problems with
11581                  call-state tracking in caller-save.c.  */
11582               if (REG_NOTE_KIND (note) == REG_DEAD && place == 0 && tem != 0)
11583                 {
11584                   place
11585                     = emit_insn_after (gen_rtx_USE (VOIDmode, XEXP (note, 0)),
11586                                        tem);
11587
11588                   /* If this insn was emitted between blocks, then update
11589                      basic_block_head of the current block to include it.  */
11590                   if (basic_block_end[this_basic_block - 1] == tem)
11591                     basic_block_head[this_basic_block] = place;
11592                 }
11593             }
11594
11595           /* If the register is set or already dead at PLACE, we needn't do
11596              anything with this note if it is still a REG_DEAD note.
11597              We can here if it is set at all, not if is it totally replace,
11598              which is what `dead_or_set_p' checks, so also check for it being
11599              set partially.  */
11600
11601
11602           if (place && REG_NOTE_KIND (note) == REG_DEAD)
11603             {
11604               int regno = REGNO (XEXP (note, 0));
11605
11606               if (dead_or_set_p (place, XEXP (note, 0))
11607                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
11608                 {
11609                   /* Unless the register previously died in PLACE, clear
11610                      reg_last_death.  [I no longer understand why this is
11611                      being done.] */
11612                   if (reg_last_death[regno] != place)
11613                     reg_last_death[regno] = 0;
11614                   place = 0;
11615                 }
11616               else
11617                 reg_last_death[regno] = place;
11618
11619               /* If this is a death note for a hard reg that is occupying
11620                  multiple registers, ensure that we are still using all
11621                  parts of the object.  If we find a piece of the object
11622                  that is unused, we must add a USE for that piece before
11623                  PLACE and put the appropriate REG_DEAD note on it.
11624
11625                  An alternative would be to put a REG_UNUSED for the pieces
11626                  on the insn that set the register, but that can't be done if
11627                  it is not in the same block.  It is simpler, though less
11628                  efficient, to add the USE insns.  */
11629
11630               if (place && regno < FIRST_PSEUDO_REGISTER
11631                   && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
11632                 {
11633                   int endregno
11634                     = regno + HARD_REGNO_NREGS (regno,
11635                                                 GET_MODE (XEXP (note, 0)));
11636                   int all_used = 1;
11637                   int i;
11638
11639                   for (i = regno; i < endregno; i++)
11640                     if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
11641                         && ! find_regno_fusage (place, USE, i))
11642                       {
11643                         rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
11644                         rtx p;
11645
11646                         /* See if we already placed a USE note for this
11647                            register in front of PLACE.  */
11648                         for (p = place;
11649                              GET_CODE (PREV_INSN (p)) == INSN
11650                              && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
11651                              p = PREV_INSN (p))
11652                           if (rtx_equal_p (piece,
11653                                            XEXP (PATTERN (PREV_INSN (p)), 0)))
11654                             {
11655                               p = 0;
11656                               break;
11657                             }
11658
11659                         if (p)
11660                           {
11661                             rtx use_insn
11662                               = emit_insn_before (gen_rtx_USE (VOIDmode,
11663                                                                piece),
11664                                                   p);
11665                             REG_NOTES (use_insn)
11666                               = gen_rtx_EXPR_LIST (REG_DEAD, piece,
11667                                                    REG_NOTES (use_insn));
11668                           }
11669
11670                         all_used = 0;
11671                       }
11672
11673                   /* Check for the case where the register dying partially
11674                      overlaps the register set by this insn.  */
11675                   if (all_used)
11676                     for (i = regno; i < endregno; i++)
11677                       if (dead_or_set_regno_p (place, i))
11678                           {
11679                             all_used = 0;
11680                             break;
11681                           }
11682
11683                   if (! all_used)
11684                     {
11685                       /* Put only REG_DEAD notes for pieces that are
11686                          still used and that are not already dead or set.  */
11687
11688                       for (i = regno; i < endregno; i++)
11689                         {
11690                           rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
11691
11692                           if ((reg_referenced_p (piece, PATTERN (place))
11693                                || (GET_CODE (place) == CALL_INSN
11694                                    && find_reg_fusage (place, USE, piece)))
11695                               && ! dead_or_set_p (place, piece)
11696                               && ! reg_bitfield_target_p (piece,
11697                                                           PATTERN (place)))
11698                             REG_NOTES (place)
11699                               = gen_rtx_EXPR_LIST (REG_DEAD,
11700                                                    piece, REG_NOTES (place));
11701                         }
11702
11703                       place = 0;
11704                     }
11705                 }
11706             }
11707           break;
11708
11709         default:
11710           /* Any other notes should not be present at this point in the
11711              compilation.  */
11712           abort ();
11713         }
11714
11715       if (place)
11716         {
11717           XEXP (note, 1) = REG_NOTES (place);
11718           REG_NOTES (place) = note;
11719         }
11720       else if ((REG_NOTE_KIND (note) == REG_DEAD
11721                 || REG_NOTE_KIND (note) == REG_UNUSED)
11722                && GET_CODE (XEXP (note, 0)) == REG)
11723         REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
11724
11725       if (place2)
11726         {
11727           if ((REG_NOTE_KIND (note) == REG_DEAD
11728                || REG_NOTE_KIND (note) == REG_UNUSED)
11729               && GET_CODE (XEXP (note, 0)) == REG)
11730             REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
11731
11732           REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
11733                                                REG_NOTE_KIND (note),
11734                                                XEXP (note, 0),
11735                                                REG_NOTES (place2));
11736         }
11737     }
11738 }
11739 \f
11740 /* Similarly to above, distribute the LOG_LINKS that used to be present on
11741    I3, I2, and I1 to new locations.  This is also called in one case to
11742    add a link pointing at I3 when I3's destination is changed.  */
11743
11744 static void
11745 distribute_links (links)
11746      rtx links;
11747 {
11748   rtx link, next_link;
11749
11750   for (link = links; link; link = next_link)
11751     {
11752       rtx place = 0;
11753       rtx insn;
11754       rtx set, reg;
11755
11756       next_link = XEXP (link, 1);
11757
11758       /* If the insn that this link points to is a NOTE or isn't a single
11759          set, ignore it.  In the latter case, it isn't clear what we
11760          can do other than ignore the link, since we can't tell which 
11761          register it was for.  Such links wouldn't be used by combine
11762          anyway.
11763
11764          It is not possible for the destination of the target of the link to
11765          have been changed by combine.  The only potential of this is if we
11766          replace I3, I2, and I1 by I3 and I2.  But in that case the
11767          destination of I2 also remains unchanged.  */
11768
11769       if (GET_CODE (XEXP (link, 0)) == NOTE
11770           || (set = single_set (XEXP (link, 0))) == 0)
11771         continue;
11772
11773       reg = SET_DEST (set);
11774       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
11775              || GET_CODE (reg) == SIGN_EXTRACT
11776              || GET_CODE (reg) == STRICT_LOW_PART)
11777         reg = XEXP (reg, 0);
11778
11779       /* A LOG_LINK is defined as being placed on the first insn that uses
11780          a register and points to the insn that sets the register.  Start
11781          searching at the next insn after the target of the link and stop
11782          when we reach a set of the register or the end of the basic block.
11783
11784          Note that this correctly handles the link that used to point from
11785          I3 to I2.  Also note that not much searching is typically done here
11786          since most links don't point very far away.  */
11787
11788       for (insn = NEXT_INSN (XEXP (link, 0));
11789            (insn && (this_basic_block == n_basic_blocks - 1
11790                      || basic_block_head[this_basic_block + 1] != insn));
11791            insn = NEXT_INSN (insn))
11792         if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
11793             && reg_overlap_mentioned_p (reg, PATTERN (insn)))
11794           {
11795             if (reg_referenced_p (reg, PATTERN (insn)))
11796               place = insn;
11797             break;
11798           }
11799         else if (GET_CODE (insn) == CALL_INSN
11800               && find_reg_fusage (insn, USE, reg))
11801           {
11802             place = insn;
11803             break;
11804           }
11805
11806       /* If we found a place to put the link, place it there unless there
11807          is already a link to the same insn as LINK at that point.  */
11808
11809       if (place)
11810         {
11811           rtx link2;
11812
11813           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
11814             if (XEXP (link2, 0) == XEXP (link, 0))
11815               break;
11816
11817           if (link2 == 0)
11818             {
11819               XEXP (link, 1) = LOG_LINKS (place);
11820               LOG_LINKS (place) = link;
11821
11822               /* Set added_links_insn to the earliest insn we added a
11823                  link to.  */
11824               if (added_links_insn == 0 
11825                   || INSN_CUID (added_links_insn) > INSN_CUID (place))
11826                 added_links_insn = place;
11827             }
11828         }
11829     }
11830 }
11831 \f
11832 /* Compute INSN_CUID for INSN, which is an insn made by combine.  */
11833
11834 static int
11835 insn_cuid (insn)
11836      rtx insn;
11837 {
11838   while (insn != 0 && INSN_UID (insn) > max_uid_cuid
11839          && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
11840     insn = NEXT_INSN (insn);
11841
11842   if (INSN_UID (insn) > max_uid_cuid)
11843     abort ();
11844
11845   return INSN_CUID (insn);
11846 }
11847 \f
11848 void
11849 dump_combine_stats (file)
11850      FILE *file;
11851 {
11852   fprintf
11853     (file,
11854      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
11855      combine_attempts, combine_merges, combine_extras, combine_successes);
11856 }
11857
11858 void
11859 dump_combine_total_stats (file)
11860      FILE *file;
11861 {
11862   fprintf
11863     (file,
11864      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
11865      total_attempts, total_merges, total_extras, total_successes);
11866 }