OSDN Git Service

(set_nonzero_bits_and_sign_copies): Set
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23    Portable Optimizer, but redone to work on our list-structured
24    representation for RTL instead of their string representation.
25
26    The LOG_LINKS of each insn identify the most recent assignment
27    to each REG used in the insn.  It is a list of previous insns,
28    each of which contains a SET for a REG that is used in this insn
29    and not used or set in between.  LOG_LINKs never cross basic blocks.
30    They were set up by the preceding pass (lifetime analysis).
31
32    We try to combine each pair of insns joined by a logical link.
33    We also try to combine triples of insns A, B and C when
34    C has a link back to B and B has a link back to A.
35
36    LOG_LINKS does not have links for use of the CC0.  They don't
37    need to, because the insn that sets the CC0 is always immediately
38    before the insn that tests it.  So we always regard a branch
39    insn as having a logical link to the preceding insn.  The same is true
40    for an insn explicitly using CC0.
41
42    We check (with use_crosses_set_p) to avoid combining in such a way
43    as to move a computation to a place where its value would be different.
44
45    Combination is done by mathematically substituting the previous
46    insn(s) values for the regs they set into the expressions in
47    the later insns that refer to these regs.  If the result is a valid insn
48    for our target machine, according to the machine description,
49    we install it, delete the earlier insns, and update the data flow
50    information (LOG_LINKS and REG_NOTES) for what we did.
51
52    There are a few exceptions where the dataflow information created by
53    flow.c aren't completely updated:
54
55    - reg_live_length is not updated
56    - reg_n_refs is not adjusted in the rare case when a register is
57      no longer required in a computation
58    - there are extremely rare cases (see distribute_regnotes) when a
59      REG_DEAD note is lost
60    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
61      removed because there is no way to know which register it was 
62      linking
63
64    To simplify substitution, we combine only when the earlier insn(s)
65    consist of only a single assignment.  To simplify updating afterward,
66    we never combine when a subroutine call appears in the middle.
67
68    Since we do not represent assignments to CC0 explicitly except when that
69    is all an insn does, there is no LOG_LINKS entry in an insn that uses
70    the condition code for the insn that set the condition code.
71    Fortunately, these two insns must be consecutive.
72    Therefore, every JUMP_INSN is taken to have an implicit logical link
73    to the preceding insn.  This is not quite right, since non-jumps can
74    also use the condition code; but in practice such insns would not
75    combine anyway.  */
76
77 #include "config.h"
78 #ifdef __STDC__
79 #include <stdarg.h>
80 #else
81 #include <varargs.h>
82 #endif
83
84 /* Must precede rtl.h for FFS.  */
85 #include <stdio.h>
86
87 #include "rtl.h"
88 #include "flags.h"
89 #include "regs.h"
90 #include "hard-reg-set.h"
91 #include "expr.h"
92 #include "basic-block.h"
93 #include "insn-config.h"
94 #include "insn-flags.h"
95 #include "insn-codes.h"
96 #include "insn-attr.h"
97 #include "recog.h"
98 #include "real.h"
99
100 /* It is not safe to use ordinary gen_lowpart in combine.
101    Use gen_lowpart_for_combine instead.  See comments there.  */
102 #define gen_lowpart dont_use_gen_lowpart_you_dummy
103
104 /* Number of attempts to combine instructions in this function.  */
105
106 static int combine_attempts;
107
108 /* Number of attempts that got as far as substitution in this function.  */
109
110 static int combine_merges;
111
112 /* Number of instructions combined with added SETs in this function.  */
113
114 static int combine_extras;
115
116 /* Number of instructions combined in this function.  */
117
118 static int combine_successes;
119
120 /* Totals over entire compilation.  */
121
122 static int total_attempts, total_merges, total_extras, total_successes;
123
124 /* Define a default value for REVERSIBLE_CC_MODE.
125    We can never assume that a condition code mode is safe to reverse unless
126    the md tells us so.  */
127 #ifndef REVERSIBLE_CC_MODE
128 #define REVERSIBLE_CC_MODE(MODE) 0
129 #endif
130 \f
131 /* Vector mapping INSN_UIDs to cuids.
132    The cuids are like uids but increase monotonically always.
133    Combine always uses cuids so that it can compare them.
134    But actually renumbering the uids, which we used to do,
135    proves to be a bad idea because it makes it hard to compare
136    the dumps produced by earlier passes with those from later passes.  */
137
138 static int *uid_cuid;
139 static int max_uid_cuid;
140
141 /* Get the cuid of an insn.  */
142
143 #define INSN_CUID(INSN) \
144 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
145
146 /* Maximum register number, which is the size of the tables below.  */
147
148 static int combine_max_regno;
149
150 /* Record last point of death of (hard or pseudo) register n.  */
151
152 static rtx *reg_last_death;
153
154 /* Record last point of modification of (hard or pseudo) register n.  */
155
156 static rtx *reg_last_set;
157
158 /* Record the cuid of the last insn that invalidated memory
159    (anything that writes memory, and subroutine calls, but not pushes).  */
160
161 static int mem_last_set;
162
163 /* Record the cuid of the last CALL_INSN
164    so we can tell whether a potential combination crosses any calls.  */
165
166 static int last_call_cuid;
167
168 /* When `subst' is called, this is the insn that is being modified
169    (by combining in a previous insn).  The PATTERN of this insn
170    is still the old pattern partially modified and it should not be
171    looked at, but this may be used to examine the successors of the insn
172    to judge whether a simplification is valid.  */
173
174 static rtx subst_insn;
175
176 /* This is an insn that belongs before subst_insn, but is not currently
177    on the insn chain.  */
178
179 static rtx subst_prev_insn;
180
181 /* This is the lowest CUID that `subst' is currently dealing with.
182    get_last_value will not return a value if the register was set at or
183    after this CUID.  If not for this mechanism, we could get confused if
184    I2 or I1 in try_combine were an insn that used the old value of a register
185    to obtain a new value.  In that case, we might erroneously get the
186    new value of the register when we wanted the old one.  */
187
188 static int subst_low_cuid;
189
190 /* This contains any hard registers that are used in newpat; reg_dead_at_p
191    must consider all these registers to be always live.  */
192
193 static HARD_REG_SET newpat_used_regs;
194
195 /* This is an insn to which a LOG_LINKS entry has been added.  If this
196    insn is the earlier than I2 or I3, combine should rescan starting at
197    that location.  */
198
199 static rtx added_links_insn;
200
201 /* This is the value of undobuf.num_undo when we started processing this 
202    substitution.  This will prevent gen_rtx_combine from re-used a piece
203    from the previous expression.  Doing so can produce circular rtl
204    structures.  */
205
206 static int previous_num_undos;
207
208 /* Basic block number of the block in which we are performing combines.  */
209 static int this_basic_block;
210 \f
211 /* The next group of arrays allows the recording of the last value assigned
212    to (hard or pseudo) register n.  We use this information to see if a
213    operation being processed is redundant given a prior operation performed
214    on the register.  For example, an `and' with a constant is redundant if
215    all the zero bits are already known to be turned off.
216
217    We use an approach similar to that used by cse, but change it in the
218    following ways:
219
220    (1) We do not want to reinitialize at each label.
221    (2) It is useful, but not critical, to know the actual value assigned
222        to a register.  Often just its form is helpful.
223
224    Therefore, we maintain the following arrays:
225
226    reg_last_set_value           the last value assigned
227    reg_last_set_label           records the value of label_tick when the
228                                 register was assigned
229    reg_last_set_table_tick      records the value of label_tick when a
230                                 value using the register is assigned
231    reg_last_set_invalid         set to non-zero when it is not valid
232                                 to use the value of this register in some
233                                 register's value
234
235    To understand the usage of these tables, it is important to understand
236    the distinction between the value in reg_last_set_value being valid
237    and the register being validly contained in some other expression in the
238    table.
239
240    Entry I in reg_last_set_value is valid if it is non-zero, and either
241    reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
242
243    Register I may validly appear in any expression returned for the value
244    of another register if reg_n_sets[i] is 1.  It may also appear in the
245    value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
246    reg_last_set_invalid[j] is zero.
247
248    If an expression is found in the table containing a register which may
249    not validly appear in an expression, the register is replaced by
250    something that won't match, (clobber (const_int 0)).
251
252    reg_last_set_invalid[i] is set non-zero when register I is being assigned
253    to and reg_last_set_table_tick[i] == label_tick.  */
254
255 /* Record last value assigned to (hard or pseudo) register n. */
256
257 static rtx *reg_last_set_value;
258
259 /* Record the value of label_tick when the value for register n is placed in
260    reg_last_set_value[n].  */
261
262 static int *reg_last_set_label;
263
264 /* Record the value of label_tick when an expression involving register n
265    is placed in reg_last_set_value. */
266
267 static int *reg_last_set_table_tick;
268
269 /* Set non-zero if references to register n in expressions should not be
270    used.  */
271
272 static char *reg_last_set_invalid;
273
274 /* Incremented for each label. */
275
276 static int label_tick;
277
278 /* Some registers that are set more than once and used in more than one
279    basic block are nevertheless always set in similar ways.  For example,
280    a QImode register may be loaded from memory in two places on a machine
281    where byte loads zero extend.
282
283    We record in the following array what we know about the nonzero
284    bits of a register, specifically which bits are known to be zero.
285
286    If an entry is zero, it means that we don't know anything special.  */
287
288 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
289
290 /* Mode used to compute significance in reg_nonzero_bits.  It is the largest
291    integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
292
293 static enum machine_mode nonzero_bits_mode;
294
295 /* Nonzero if we know that a register has some leading bits that are always
296    equal to the sign bit.  */
297
298 static char *reg_sign_bit_copies;
299
300 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
301    It is zero while computing them and after combine has completed.  This
302    former test prevents propagating values based on previously set values,
303    which can be incorrect if a variable is modified in a loop.  */
304
305 static int nonzero_sign_valid;
306
307 /* These arrays are maintained in parallel with reg_last_set_value
308    and are used to store the mode in which the register was last set,
309    the bits that were known to be zero when it was last set, and the
310    number of sign bits copies it was known to have when it was last set.  */
311
312 static enum machine_mode *reg_last_set_mode;
313 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
314 static char *reg_last_set_sign_bit_copies;
315 \f
316 /* Record one modification to rtl structure
317    to be undone by storing old_contents into *where.
318    is_int is 1 if the contents are an int.  */
319
320 struct undo
321 {
322   int is_int;
323   union {rtx r; int i;} old_contents;
324   union {rtx *r; int *i;} where;
325 };
326
327 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
328    num_undo says how many are currently recorded.
329
330    storage is nonzero if we must undo the allocation of new storage.
331    The value of storage is what to pass to obfree.
332
333    other_insn is nonzero if we have modified some other insn in the process
334    of working on subst_insn.  It must be verified too.  */
335
336 #define MAX_UNDO 50
337
338 struct undobuf
339 {
340   int num_undo;
341   char *storage;
342   struct undo undo[MAX_UNDO];
343   rtx other_insn;
344 };
345
346 static struct undobuf undobuf;
347
348 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
349    insn.  The substitution can be undone by undo_all.  If INTO is already
350    set to NEWVAL, do not record this change.  Because computing NEWVAL might
351    also call SUBST, we have to compute it before we put anything into
352    the undo table.  */
353
354 #define SUBST(INTO, NEWVAL)  \
355  do { rtx _new = (NEWVAL);                                              \
356       if (undobuf.num_undo < MAX_UNDO)                                  \
357         {                                                               \
358           undobuf.undo[undobuf.num_undo].is_int = 0;                    \
359           undobuf.undo[undobuf.num_undo].where.r = &INTO;               \
360           undobuf.undo[undobuf.num_undo].old_contents.r = INTO; \
361           INTO = _new;                                                  \
362           if (undobuf.undo[undobuf.num_undo].old_contents.r != INTO)    \
363             undobuf.num_undo++;                                         \
364         }                                                               \
365     } while (0)
366
367 /* Similar to SUBST, but NEWVAL is an int.  INTO will normally be an XINT
368    expression.
369    Note that substitution for the value of a CONST_INT is not safe.  */
370
371 #define SUBST_INT(INTO, NEWVAL)  \
372  do { if (undobuf.num_undo < MAX_UNDO)                                  \
373 {                                                                       \
374           undobuf.undo[undobuf.num_undo].is_int = 1;                    \
375           undobuf.undo[undobuf.num_undo].where.i = (int *) &INTO;       \
376           undobuf.undo[undobuf.num_undo].old_contents.i = INTO;         \
377           INTO = NEWVAL;                                                \
378           if (undobuf.undo[undobuf.num_undo].old_contents.i != INTO)    \
379             undobuf.num_undo++;                                         \
380         }                                                               \
381      } while (0)
382
383 /* Number of times the pseudo being substituted for
384    was found and replaced.  */
385
386 static int n_occurrences;
387
388 static void init_reg_last_arrays        PROTO((void));
389 static void setup_incoming_promotions   PROTO((void));
390 static void set_nonzero_bits_and_sign_copies  PROTO((rtx, rtx));
391 static int can_combine_p        PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
392 static int combinable_i3pat     PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
393 static rtx try_combine          PROTO((rtx, rtx, rtx));
394 static void undo_all            PROTO((void));
395 static rtx *find_split_point    PROTO((rtx *, rtx));
396 static rtx subst                PROTO((rtx, rtx, rtx, int, int));
397 static rtx simplify_rtx         PROTO((rtx, enum machine_mode, int, int));
398 static rtx simplify_if_then_else  PROTO((rtx));
399 static rtx simplify_set         PROTO((rtx));
400 static rtx simplify_logical     PROTO((rtx, int));
401 static rtx expand_compound_operation  PROTO((rtx));
402 static rtx expand_field_assignment  PROTO((rtx));
403 static rtx make_extraction      PROTO((enum machine_mode, rtx, int, rtx, int,
404                                        int, int, int));
405 static rtx extract_left_shift   PROTO((rtx, int));
406 static rtx make_compound_operation  PROTO((rtx, enum rtx_code));
407 static int get_pos_from_mask    PROTO((unsigned HOST_WIDE_INT, int *));
408 static rtx force_to_mode        PROTO((rtx, enum machine_mode,
409                                        unsigned HOST_WIDE_INT, rtx, int));
410 static rtx if_then_else_cond    PROTO((rtx, rtx *, rtx *));
411 static rtx known_cond           PROTO((rtx, enum rtx_code, rtx, rtx));
412 static int rtx_equal_for_field_assignment_p PROTO((rtx, rtx));
413 static rtx make_field_assignment  PROTO((rtx));
414 static rtx apply_distributive_law  PROTO((rtx));
415 static rtx simplify_and_const_int  PROTO((rtx, enum machine_mode, rtx,
416                                           unsigned HOST_WIDE_INT));
417 static unsigned HOST_WIDE_INT nonzero_bits  PROTO((rtx, enum machine_mode));
418 static int num_sign_bit_copies  PROTO((rtx, enum machine_mode));
419 static int merge_outer_ops      PROTO((enum rtx_code *, HOST_WIDE_INT *,
420                                        enum rtx_code, HOST_WIDE_INT,
421                                        enum machine_mode, int *));
422 static rtx simplify_shift_const PROTO((rtx, enum rtx_code, enum machine_mode,
423                                        rtx, int));
424 static int recog_for_combine    PROTO((rtx *, rtx, rtx *, int *));
425 static rtx gen_lowpart_for_combine  PROTO((enum machine_mode, rtx));
426 static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
427                                   ...));
428 static rtx gen_binary           PROTO((enum rtx_code, enum machine_mode,
429                                        rtx, rtx));
430 static rtx gen_unary            PROTO((enum rtx_code, enum machine_mode,
431                                        enum machine_mode, rtx));
432 static enum rtx_code simplify_comparison  PROTO((enum rtx_code, rtx *, rtx *));
433 static int reversible_comparison_p  PROTO((rtx));
434 static void update_table_tick   PROTO((rtx));
435 static void record_value_for_reg  PROTO((rtx, rtx, rtx));
436 static void record_dead_and_set_regs_1  PROTO((rtx, rtx));
437 static void record_dead_and_set_regs  PROTO((rtx));
438 static int get_last_value_validate  PROTO((rtx *, int, int));
439 static rtx get_last_value       PROTO((rtx));
440 static int use_crosses_set_p    PROTO((rtx, int));
441 static void reg_dead_at_p_1     PROTO((rtx, rtx));
442 static int reg_dead_at_p        PROTO((rtx, rtx));
443 static void move_deaths         PROTO((rtx, int, rtx, rtx *));
444 static int reg_bitfield_target_p  PROTO((rtx, rtx));
445 static void distribute_notes    PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
446 static void distribute_links    PROTO((rtx));
447 static void mark_used_regs_combine PROTO((rtx));
448 static int insn_cuid            PROTO((rtx));
449 \f
450 /* Main entry point for combiner.  F is the first insn of the function.
451    NREGS is the first unused pseudo-reg number.  */
452
453 void
454 combine_instructions (f, nregs)
455      rtx f;
456      int nregs;
457 {
458   register rtx insn, next, prev;
459   register int i;
460   register rtx links, nextlinks;
461
462   combine_attempts = 0;
463   combine_merges = 0;
464   combine_extras = 0;
465   combine_successes = 0;
466   undobuf.num_undo = previous_num_undos = 0;
467
468   combine_max_regno = nregs;
469
470   reg_nonzero_bits
471     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
472   reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
473
474   bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
475   bzero (reg_sign_bit_copies, nregs * sizeof (char));
476
477   reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
478   reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
479   reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
480   reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
481   reg_last_set_label = (int *) alloca (nregs * sizeof (int));
482   reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
483   reg_last_set_mode
484     = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
485   reg_last_set_nonzero_bits
486     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
487   reg_last_set_sign_bit_copies
488     = (char *) alloca (nregs * sizeof (char));
489
490   init_reg_last_arrays ();
491
492   init_recog_no_volatile ();
493
494   /* Compute maximum uid value so uid_cuid can be allocated.  */
495
496   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
497     if (INSN_UID (insn) > i)
498       i = INSN_UID (insn);
499
500   uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
501   max_uid_cuid = i;
502
503   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
504
505   /* Don't use reg_nonzero_bits when computing it.  This can cause problems
506      when, for example, we have j <<= 1 in a loop.  */
507
508   nonzero_sign_valid = 0;
509
510   /* Compute the mapping from uids to cuids.
511      Cuids are numbers assigned to insns, like uids,
512      except that cuids increase monotonically through the code. 
513
514      Scan all SETs and see if we can deduce anything about what
515      bits are known to be zero for some registers and how many copies
516      of the sign bit are known to exist for those registers.
517
518      Also set any known values so that we can use it while searching
519      for what bits are known to be set.  */
520
521   label_tick = 1;
522
523   /* We need to initialize it here, because record_dead_and_set_regs may call
524      get_last_value.  */
525   subst_prev_insn = NULL_RTX;
526
527   setup_incoming_promotions ();
528
529   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
530     {
531       uid_cuid[INSN_UID (insn)] = ++i;
532       subst_low_cuid = i;
533       subst_insn = insn;
534
535       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
536         {
537           note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
538           record_dead_and_set_regs (insn);
539
540 #ifdef AUTO_INC_DEC
541           for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
542             if (REG_NOTE_KIND (links) == REG_INC)
543               set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX);
544 #endif
545         }
546
547       if (GET_CODE (insn) == CODE_LABEL)
548         label_tick++;
549     }
550
551   nonzero_sign_valid = 1;
552
553   /* Now scan all the insns in forward order.  */
554
555   this_basic_block = -1;
556   label_tick = 1;
557   last_call_cuid = 0;
558   mem_last_set = 0;
559   init_reg_last_arrays ();
560   setup_incoming_promotions ();
561
562   for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
563     {
564       next = 0;
565
566       /* If INSN starts a new basic block, update our basic block number.  */
567       if (this_basic_block + 1 < n_basic_blocks
568           && basic_block_head[this_basic_block + 1] == insn)
569         this_basic_block++;
570
571       if (GET_CODE (insn) == CODE_LABEL)
572         label_tick++;
573
574       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
575         {
576           /* Try this insn with each insn it links back to.  */
577
578           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
579             if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
580               goto retry;
581
582           /* Try each sequence of three linked insns ending with this one.  */
583
584           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
585             for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
586                  nextlinks = XEXP (nextlinks, 1))
587               if ((next = try_combine (insn, XEXP (links, 0),
588                                        XEXP (nextlinks, 0))) != 0)
589                 goto retry;
590
591 #ifdef HAVE_cc0
592           /* Try to combine a jump insn that uses CC0
593              with a preceding insn that sets CC0, and maybe with its
594              logical predecessor as well.
595              This is how we make decrement-and-branch insns.
596              We need this special code because data flow connections
597              via CC0 do not get entered in LOG_LINKS.  */
598
599           if (GET_CODE (insn) == JUMP_INSN
600               && (prev = prev_nonnote_insn (insn)) != 0
601               && GET_CODE (prev) == INSN
602               && sets_cc0_p (PATTERN (prev)))
603             {
604               if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
605                 goto retry;
606
607               for (nextlinks = LOG_LINKS (prev); nextlinks;
608                    nextlinks = XEXP (nextlinks, 1))
609                 if ((next = try_combine (insn, prev,
610                                          XEXP (nextlinks, 0))) != 0)
611                   goto retry;
612             }
613
614           /* Do the same for an insn that explicitly references CC0.  */
615           if (GET_CODE (insn) == INSN
616               && (prev = prev_nonnote_insn (insn)) != 0
617               && GET_CODE (prev) == INSN
618               && sets_cc0_p (PATTERN (prev))
619               && GET_CODE (PATTERN (insn)) == SET
620               && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
621             {
622               if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
623                 goto retry;
624
625               for (nextlinks = LOG_LINKS (prev); nextlinks;
626                    nextlinks = XEXP (nextlinks, 1))
627                 if ((next = try_combine (insn, prev,
628                                          XEXP (nextlinks, 0))) != 0)
629                   goto retry;
630             }
631
632           /* Finally, see if any of the insns that this insn links to
633              explicitly references CC0.  If so, try this insn, that insn,
634              and its predecessor if it sets CC0.  */
635           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
636             if (GET_CODE (XEXP (links, 0)) == INSN
637                 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
638                 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
639                 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
640                 && GET_CODE (prev) == INSN
641                 && sets_cc0_p (PATTERN (prev))
642                 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
643               goto retry;
644 #endif
645
646           /* Try combining an insn with two different insns whose results it
647              uses.  */
648           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
649             for (nextlinks = XEXP (links, 1); nextlinks;
650                  nextlinks = XEXP (nextlinks, 1))
651               if ((next = try_combine (insn, XEXP (links, 0),
652                                        XEXP (nextlinks, 0))) != 0)
653                 goto retry;
654
655           if (GET_CODE (insn) != NOTE)
656             record_dead_and_set_regs (insn);
657
658         retry:
659           ;
660         }
661     }
662
663   total_attempts += combine_attempts;
664   total_merges += combine_merges;
665   total_extras += combine_extras;
666   total_successes += combine_successes;
667
668   nonzero_sign_valid = 0;
669 }
670
671 /* Wipe the reg_last_xxx arrays in preparation for another pass.  */
672
673 static void
674 init_reg_last_arrays ()
675 {
676   int nregs = combine_max_regno;
677
678   bzero ((char *) reg_last_death, nregs * sizeof (rtx));
679   bzero ((char *) reg_last_set, nregs * sizeof (rtx));
680   bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
681   bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
682   bzero ((char *) reg_last_set_label, nregs * sizeof (int));
683   bzero (reg_last_set_invalid, nregs * sizeof (char));
684   bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
685   bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
686   bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
687 }
688 \f
689 /* Set up any promoted values for incoming argument registers.  */
690
691 static void
692 setup_incoming_promotions ()
693 {
694 #ifdef PROMOTE_FUNCTION_ARGS
695   int regno;
696   rtx reg;
697   enum machine_mode mode;
698   int unsignedp;
699   rtx first = get_insns ();
700
701   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
702     if (FUNCTION_ARG_REGNO_P (regno)
703         && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
704       record_value_for_reg (reg, first,
705                             gen_rtx (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
706                                      GET_MODE (reg),
707                                      gen_rtx (CLOBBER, mode, const0_rtx)));
708 #endif
709 }
710 \f
711 /* Called via note_stores.  If X is a pseudo that is narrower than
712    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
713
714    If we are setting only a portion of X and we can't figure out what
715    portion, assume all bits will be used since we don't know what will
716    be happening.
717
718    Similarly, set how many bits of X are known to be copies of the sign bit
719    at all locations in the function.  This is the smallest number implied 
720    by any set of X.  */
721
722 static void
723 set_nonzero_bits_and_sign_copies (x, set)
724      rtx x;
725      rtx set;
726 {
727   int num;
728
729   if (GET_CODE (x) == REG
730       && REGNO (x) >= FIRST_PSEUDO_REGISTER
731       /* If this register is undefined at the start of the file, we can't
732          say what its contents were.  */
733       && ! (basic_block_live_at_start[0][REGNO (x) / REGSET_ELT_BITS]
734             & ((REGSET_ELT_TYPE) 1 << (REGNO (x) % REGSET_ELT_BITS)))
735       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
736     {
737       if (set == 0 || GET_CODE (set) == CLOBBER)
738         {
739           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
740           reg_sign_bit_copies[REGNO (x)] = 1;
741           return;
742         }
743
744       /* If this is a complex assignment, see if we can convert it into a
745          simple assignment.  */
746       set = expand_field_assignment (set);
747
748       /* If this is a simple assignment, or we have a paradoxical SUBREG,
749          set what we know about X.  */
750
751       if (SET_DEST (set) == x
752           || (GET_CODE (SET_DEST (set)) == SUBREG
753               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
754                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
755               && SUBREG_REG (SET_DEST (set)) == x))
756         {
757           rtx src = SET_SRC (set);
758
759 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
760           /* If X is narrower than a word and SRC is a non-negative
761              constant that would appear negative in the mode of X,
762              sign-extend it for use in reg_nonzero_bits because some
763              machines (maybe most) will actually do the sign-extension
764              and this is the conservative approach. 
765
766              ??? For 2.5, try to tighten up the MD files in this regard
767              instead of this kludge.  */
768
769           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
770               && GET_CODE (src) == CONST_INT
771               && INTVAL (src) > 0
772               && 0 != (INTVAL (src)
773                        & ((HOST_WIDE_INT) 1
774                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
775             src = GEN_INT (INTVAL (src)
776                            | ((HOST_WIDE_INT) (-1)
777                               << GET_MODE_BITSIZE (GET_MODE (x))));
778 #endif
779
780           reg_nonzero_bits[REGNO (x)]
781             |= nonzero_bits (src, nonzero_bits_mode);
782           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
783           if (reg_sign_bit_copies[REGNO (x)] == 0
784               || reg_sign_bit_copies[REGNO (x)] > num)
785             reg_sign_bit_copies[REGNO (x)] = num;
786         }
787       else
788         {
789           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
790           reg_sign_bit_copies[REGNO (x)] = 1;
791         }
792     }
793 }
794 \f
795 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
796    insns that were previously combined into I3 or that will be combined
797    into the merger of INSN and I3.
798
799    Return 0 if the combination is not allowed for any reason.
800
801    If the combination is allowed, *PDEST will be set to the single 
802    destination of INSN and *PSRC to the single source, and this function
803    will return 1.  */
804
805 static int
806 can_combine_p (insn, i3, pred, succ, pdest, psrc)
807      rtx insn;
808      rtx i3;
809      rtx pred, succ;
810      rtx *pdest, *psrc;
811 {
812   int i;
813   rtx set = 0, src, dest;
814   rtx p, link;
815   int all_adjacent = (succ ? (next_active_insn (insn) == succ
816                               && next_active_insn (succ) == i3)
817                       : next_active_insn (insn) == i3);
818
819   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
820      or a PARALLEL consisting of such a SET and CLOBBERs. 
821
822      If INSN has CLOBBER parallel parts, ignore them for our processing.
823      By definition, these happen during the execution of the insn.  When it
824      is merged with another insn, all bets are off.  If they are, in fact,
825      needed and aren't also supplied in I3, they may be added by
826      recog_for_combine.  Otherwise, it won't match. 
827
828      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
829      note.
830
831      Get the source and destination of INSN.  If more than one, can't 
832      combine.  */
833      
834   if (GET_CODE (PATTERN (insn)) == SET)
835     set = PATTERN (insn);
836   else if (GET_CODE (PATTERN (insn)) == PARALLEL
837            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
838     {
839       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
840         {
841           rtx elt = XVECEXP (PATTERN (insn), 0, i);
842
843           switch (GET_CODE (elt))
844             {
845               /* We can ignore CLOBBERs.  */
846             case CLOBBER:
847               break;
848
849             case SET:
850               /* Ignore SETs whose result isn't used but not those that
851                  have side-effects.  */
852               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
853                   && ! side_effects_p (elt))
854                 break;
855
856               /* If we have already found a SET, this is a second one and
857                  so we cannot combine with this insn.  */
858               if (set)
859                 return 0;
860
861               set = elt;
862               break;
863
864             default:
865               /* Anything else means we can't combine.  */
866               return 0;
867             }
868         }
869
870       if (set == 0
871           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
872              so don't do anything with it.  */
873           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
874         return 0;
875     }
876   else
877     return 0;
878
879   if (set == 0)
880     return 0;
881
882   set = expand_field_assignment (set);
883   src = SET_SRC (set), dest = SET_DEST (set);
884
885   /* Don't eliminate a store in the stack pointer.  */
886   if (dest == stack_pointer_rtx
887       /* If we couldn't eliminate a field assignment, we can't combine.  */
888       || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
889       /* Don't combine with an insn that sets a register to itself if it has
890          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
891       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
892       /* Can't merge a function call.  */
893       || GET_CODE (src) == CALL
894       /* Don't eliminate a function call argument.  */
895       || (GET_CODE (i3) == CALL_INSN
896           && (find_reg_fusage (i3, USE, dest)
897               || (GET_CODE (dest) == REG
898                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
899                   && global_regs[REGNO (dest)])))
900       /* Don't substitute into an incremented register.  */
901       || FIND_REG_INC_NOTE (i3, dest)
902       || (succ && FIND_REG_INC_NOTE (succ, dest))
903       /* Don't combine the end of a libcall into anything.  */
904       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
905       /* Make sure that DEST is not used after SUCC but before I3.  */
906       || (succ && ! all_adjacent
907           && reg_used_between_p (dest, succ, i3))
908       /* Make sure that the value that is to be substituted for the register
909          does not use any registers whose values alter in between.  However,
910          If the insns are adjacent, a use can't cross a set even though we
911          think it might (this can happen for a sequence of insns each setting
912          the same destination; reg_last_set of that register might point to
913          a NOTE).  If INSN has a REG_EQUIV note, the register is always
914          equivalent to the memory so the substitution is valid even if there
915          are intervening stores.  Also, don't move a volatile asm or
916          UNSPEC_VOLATILE across any other insns.  */
917       || (! all_adjacent
918           && (((GET_CODE (src) != MEM
919                 || ! find_reg_note (insn, REG_EQUIV, src))
920                && use_crosses_set_p (src, INSN_CUID (insn)))
921               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
922               || GET_CODE (src) == UNSPEC_VOLATILE))
923       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
924          better register allocation by not doing the combine.  */
925       || find_reg_note (i3, REG_NO_CONFLICT, dest)
926       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
927       /* Don't combine across a CALL_INSN, because that would possibly
928          change whether the life span of some REGs crosses calls or not,
929          and it is a pain to update that information.
930          Exception: if source is a constant, moving it later can't hurt.
931          Accept that special case, because it helps -fforce-addr a lot.  */
932       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
933     return 0;
934
935   /* DEST must either be a REG or CC0.  */
936   if (GET_CODE (dest) == REG)
937     {
938       /* If register alignment is being enforced for multi-word items in all
939          cases except for parameters, it is possible to have a register copy
940          insn referencing a hard register that is not allowed to contain the
941          mode being copied and which would not be valid as an operand of most
942          insns.  Eliminate this problem by not combining with such an insn.
943
944          Also, on some machines we don't want to extend the life of a hard
945          register.  */
946
947       if (GET_CODE (src) == REG
948           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
949                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
950               /* Don't extend the life of a hard register unless it is
951                  user variable (if we have few registers) or it can't
952                  fit into the desired register (meaning something special
953                  is going on).  */
954               || (REGNO (src) < FIRST_PSEUDO_REGISTER
955                   && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
956 #ifdef SMALL_REGISTER_CLASSES
957                       || ! REG_USERVAR_P (src)
958 #endif
959                       ))))
960         return 0;
961     }
962   else if (GET_CODE (dest) != CC0)
963     return 0;
964
965   /* Don't substitute for a register intended as a clobberable operand.
966      Similarly, don't substitute an expression containing a register that
967      will be clobbered in I3.  */
968   if (GET_CODE (PATTERN (i3)) == PARALLEL)
969     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
970       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
971           && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
972                                        src)
973               || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
974         return 0;
975
976   /* If INSN contains anything volatile, or is an `asm' (whether volatile
977      or not), reject, unless nothing volatile comes between it and I3,
978      with the exception of SUCC.  */
979
980   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
981     for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
982       if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
983           && p != succ && volatile_refs_p (PATTERN (p)))
984         return 0;
985
986   /* If there are any volatile insns between INSN and I3, reject, because
987      they might affect machine state.  */
988
989   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
990     if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
991         && p != succ && volatile_insn_p (PATTERN (p)))
992       return 0;
993
994   /* If INSN or I2 contains an autoincrement or autodecrement,
995      make sure that register is not used between there and I3,
996      and not already used in I3 either.
997      Also insist that I3 not be a jump; if it were one
998      and the incremented register were spilled, we would lose.  */
999
1000 #ifdef AUTO_INC_DEC
1001   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1002     if (REG_NOTE_KIND (link) == REG_INC
1003         && (GET_CODE (i3) == JUMP_INSN
1004             || reg_used_between_p (XEXP (link, 0), insn, i3)
1005             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1006       return 0;
1007 #endif
1008
1009 #ifdef HAVE_cc0
1010   /* Don't combine an insn that follows a CC0-setting insn.
1011      An insn that uses CC0 must not be separated from the one that sets it.
1012      We do, however, allow I2 to follow a CC0-setting insn if that insn
1013      is passed as I1; in that case it will be deleted also.
1014      We also allow combining in this case if all the insns are adjacent
1015      because that would leave the two CC0 insns adjacent as well.
1016      It would be more logical to test whether CC0 occurs inside I1 or I2,
1017      but that would be much slower, and this ought to be equivalent.  */
1018
1019   p = prev_nonnote_insn (insn);
1020   if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1021       && ! all_adjacent)
1022     return 0;
1023 #endif
1024
1025   /* If we get here, we have passed all the tests and the combination is
1026      to be allowed.  */
1027
1028   *pdest = dest;
1029   *psrc = src;
1030
1031   return 1;
1032 }
1033 \f
1034 /* LOC is the location within I3 that contains its pattern or the component
1035    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1036
1037    One problem is if I3 modifies its output, as opposed to replacing it
1038    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1039    so would produce an insn that is not equivalent to the original insns.
1040
1041    Consider:
1042
1043          (set (reg:DI 101) (reg:DI 100))
1044          (set (subreg:SI (reg:DI 101) 0) <foo>)
1045
1046    This is NOT equivalent to:
1047
1048          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1049                     (set (reg:DI 101) (reg:DI 100))])
1050
1051    Not only does this modify 100 (in which case it might still be valid
1052    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100. 
1053
1054    We can also run into a problem if I2 sets a register that I1
1055    uses and I1 gets directly substituted into I3 (not via I2).  In that
1056    case, we would be getting the wrong value of I2DEST into I3, so we
1057    must reject the combination.  This case occurs when I2 and I1 both
1058    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1059    If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1060    of a SET must prevent combination from occurring.
1061
1062    On machines where SMALL_REGISTER_CLASSES is defined, we don't combine
1063    if the destination of a SET is a hard register that isn't a user
1064    variable.
1065
1066    Before doing the above check, we first try to expand a field assignment
1067    into a set of logical operations.
1068
1069    If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1070    we place a register that is both set and used within I3.  If more than one
1071    such register is detected, we fail.
1072
1073    Return 1 if the combination is valid, zero otherwise.  */
1074
1075 static int
1076 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1077      rtx i3;
1078      rtx *loc;
1079      rtx i2dest;
1080      rtx i1dest;
1081      int i1_not_in_src;
1082      rtx *pi3dest_killed;
1083 {
1084   rtx x = *loc;
1085
1086   if (GET_CODE (x) == SET)
1087     {
1088       rtx set = expand_field_assignment (x);
1089       rtx dest = SET_DEST (set);
1090       rtx src = SET_SRC (set);
1091       rtx inner_dest = dest, inner_src = src;
1092
1093       SUBST (*loc, set);
1094
1095       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1096              || GET_CODE (inner_dest) == SUBREG
1097              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1098         inner_dest = XEXP (inner_dest, 0);
1099
1100   /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1101      was added.  */
1102 #if 0
1103       while (GET_CODE (inner_src) == STRICT_LOW_PART
1104              || GET_CODE (inner_src) == SUBREG
1105              || GET_CODE (inner_src) == ZERO_EXTRACT)
1106         inner_src = XEXP (inner_src, 0);
1107
1108       /* If it is better that two different modes keep two different pseudos,
1109          avoid combining them.  This avoids producing the following pattern
1110          on a 386:
1111           (set (subreg:SI (reg/v:QI 21) 0)
1112                (lshiftrt:SI (reg/v:SI 20)
1113                    (const_int 24)))
1114          If that were made, reload could not handle the pair of
1115          reg 20/21, since it would try to get any GENERAL_REGS
1116          but some of them don't handle QImode.  */
1117
1118       if (rtx_equal_p (inner_src, i2dest)
1119           && GET_CODE (inner_dest) == REG
1120           && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1121         return 0;
1122 #endif
1123
1124       /* Check for the case where I3 modifies its output, as
1125          discussed above.  */
1126       if ((inner_dest != dest
1127            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1128                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1129           /* This is the same test done in can_combine_p except that we
1130              allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
1131              CALL operation.  */
1132           || (GET_CODE (inner_dest) == REG
1133               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1134               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1135                                         GET_MODE (inner_dest))
1136 #ifdef SMALL_REGISTER_CLASSES
1137                  || (GET_CODE (src) != CALL && ! REG_USERVAR_P (inner_dest))
1138 #endif
1139                   ))
1140           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1141         return 0;
1142
1143       /* If DEST is used in I3, it is being killed in this insn,
1144          so record that for later. 
1145          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1146          STACK_POINTER_REGNUM, since these are always considered to be
1147          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1148       if (pi3dest_killed && GET_CODE (dest) == REG
1149           && reg_referenced_p (dest, PATTERN (i3))
1150           && REGNO (dest) != FRAME_POINTER_REGNUM
1151 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1152           && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1153 #endif
1154 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1155           && (REGNO (dest) != ARG_POINTER_REGNUM
1156               || ! fixed_regs [REGNO (dest)])
1157 #endif
1158           && REGNO (dest) != STACK_POINTER_REGNUM)
1159         {
1160           if (*pi3dest_killed)
1161             return 0;
1162
1163           *pi3dest_killed = dest;
1164         }
1165     }
1166
1167   else if (GET_CODE (x) == PARALLEL)
1168     {
1169       int i;
1170
1171       for (i = 0; i < XVECLEN (x, 0); i++)
1172         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1173                                 i1_not_in_src, pi3dest_killed))
1174           return 0;
1175     }
1176
1177   return 1;
1178 }
1179 \f
1180 /* Try to combine the insns I1 and I2 into I3.
1181    Here I1 and I2 appear earlier than I3.
1182    I1 can be zero; then we combine just I2 into I3.
1183  
1184    It we are combining three insns and the resulting insn is not recognized,
1185    try splitting it into two insns.  If that happens, I2 and I3 are retained
1186    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1187    are pseudo-deleted.
1188
1189    Return 0 if the combination does not work.  Then nothing is changed. 
1190    If we did the combination, return the insn at which combine should
1191    resume scanning.  */
1192
1193 static rtx
1194 try_combine (i3, i2, i1)
1195      register rtx i3, i2, i1;
1196 {
1197   /* New patterns for I3 and I3, respectively.  */
1198   rtx newpat, newi2pat = 0;
1199   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1200   int added_sets_1, added_sets_2;
1201   /* Total number of SETs to put into I3.  */
1202   int total_sets;
1203   /* Nonzero is I2's body now appears in I3.  */
1204   int i2_is_used;
1205   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1206   int insn_code_number, i2_code_number, other_code_number;
1207   /* Contains I3 if the destination of I3 is used in its source, which means
1208      that the old life of I3 is being killed.  If that usage is placed into
1209      I2 and not in I3, a REG_DEAD note must be made.  */
1210   rtx i3dest_killed = 0;
1211   /* SET_DEST and SET_SRC of I2 and I1.  */
1212   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1213   /* PATTERN (I2), or a copy of it in certain cases.  */
1214   rtx i2pat;
1215   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1216   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1217   int i1_feeds_i3 = 0;
1218   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1219   rtx new_i3_notes, new_i2_notes;
1220   /* Notes that we substituted I3 into I2 instead of the normal case.  */
1221   int i3_subst_into_i2 = 0;
1222   /* Notes that I1, I2 or I3 is a MULT operation.  */
1223   int have_mult = 0;
1224   /* Number of clobbers of SCRATCH we had to add.  */
1225   int i3_scratches = 0, i2_scratches = 0, other_scratches = 0;
1226
1227   int maxreg;
1228   rtx temp;
1229   register rtx link;
1230   int i;
1231
1232   /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1233      This can occur when flow deletes an insn that it has merged into an
1234      auto-increment address.  We also can't do anything if I3 has a
1235      REG_LIBCALL note since we don't want to disrupt the contiguity of a
1236      libcall.  */
1237
1238   if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1239       || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1240       || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
1241       || find_reg_note (i3, REG_LIBCALL, NULL_RTX))
1242     return 0;
1243
1244   combine_attempts++;
1245
1246   undobuf.num_undo = previous_num_undos = 0;
1247   undobuf.other_insn = 0;
1248
1249   /* Save the current high-water-mark so we can free storage if we didn't
1250      accept this combination.  */
1251   undobuf.storage = (char *) oballoc (0);
1252
1253   /* Reset the hard register usage information.  */
1254   CLEAR_HARD_REG_SET (newpat_used_regs);
1255
1256   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1257      code below, set I1 to be the earlier of the two insns.  */
1258   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1259     temp = i1, i1 = i2, i2 = temp;
1260
1261   added_links_insn = 0;
1262
1263   /* First check for one important special-case that the code below will
1264      not handle.  Namely, the case where I1 is zero, I2 has multiple sets,
1265      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1266      we may be able to replace that destination with the destination of I3.
1267      This occurs in the common code where we compute both a quotient and
1268      remainder into a structure, in which case we want to do the computation
1269      directly into the structure to avoid register-register copies.
1270
1271      We make very conservative checks below and only try to handle the
1272      most common cases of this.  For example, we only handle the case
1273      where I2 and I3 are adjacent to avoid making difficult register
1274      usage tests.  */
1275
1276   if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1277       && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1278       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1279 #ifdef SMALL_REGISTER_CLASSES
1280       && (GET_CODE (SET_DEST (PATTERN (i3))) != REG
1281           || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1282           || REG_USERVAR_P (SET_DEST (PATTERN (i3))))
1283 #endif
1284       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1285       && GET_CODE (PATTERN (i2)) == PARALLEL
1286       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1287       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1288          below would need to check what is inside (and reg_overlap_mentioned_p
1289          doesn't support those codes anyway).  Don't allow those destinations;
1290          the resulting insn isn't likely to be recognized anyway.  */
1291       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1292       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1293       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1294                                     SET_DEST (PATTERN (i3)))
1295       && next_real_insn (i2) == i3)
1296     {
1297       rtx p2 = PATTERN (i2);
1298
1299       /* Make sure that the destination of I3,
1300          which we are going to substitute into one output of I2,
1301          is not used within another output of I2.  We must avoid making this:
1302          (parallel [(set (mem (reg 69)) ...)
1303                     (set (reg 69) ...)])
1304          which is not well-defined as to order of actions.
1305          (Besides, reload can't handle output reloads for this.)
1306
1307          The problem can also happen if the dest of I3 is a memory ref,
1308          if another dest in I2 is an indirect memory ref.  */
1309       for (i = 0; i < XVECLEN (p2, 0); i++)
1310         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1311              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1312             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1313                                         SET_DEST (XVECEXP (p2, 0, i))))
1314           break;
1315
1316       if (i == XVECLEN (p2, 0))
1317         for (i = 0; i < XVECLEN (p2, 0); i++)
1318           if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1319             {
1320               combine_merges++;
1321
1322               subst_insn = i3;
1323               subst_low_cuid = INSN_CUID (i2);
1324
1325               added_sets_2 = added_sets_1 = 0;
1326               i2dest = SET_SRC (PATTERN (i3));
1327
1328               /* Replace the dest in I2 with our dest and make the resulting
1329                  insn the new pattern for I3.  Then skip to where we
1330                  validate the pattern.  Everything was set up above.  */
1331               SUBST (SET_DEST (XVECEXP (p2, 0, i)), 
1332                      SET_DEST (PATTERN (i3)));
1333
1334               newpat = p2;
1335               i3_subst_into_i2 = 1;
1336               goto validate_replacement;
1337             }
1338     }
1339
1340 #ifndef HAVE_cc0
1341   /* If we have no I1 and I2 looks like:
1342         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1343                    (set Y OP)])
1344      make up a dummy I1 that is
1345         (set Y OP)
1346      and change I2 to be
1347         (set (reg:CC X) (compare:CC Y (const_int 0)))
1348
1349      (We can ignore any trailing CLOBBERs.)
1350
1351      This undoes a previous combination and allows us to match a branch-and-
1352      decrement insn.  */
1353
1354   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1355       && XVECLEN (PATTERN (i2), 0) >= 2
1356       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1357       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1358           == MODE_CC)
1359       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1360       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1361       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1362       && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1363       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1364                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1365     {
1366       for (i =  XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1367         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1368           break;
1369
1370       if (i == 1)
1371         {
1372           /* We make I1 with the same INSN_UID as I2.  This gives it
1373              the same INSN_CUID for value tracking.  Our fake I1 will
1374              never appear in the insn stream so giving it the same INSN_UID
1375              as I2 will not cause a problem.  */
1376
1377           subst_prev_insn = i1
1378             = gen_rtx (INSN, VOIDmode, INSN_UID (i2), 0, i2,
1379                        XVECEXP (PATTERN (i2), 0, 1), -1, 0, 0);
1380
1381           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1382           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1383                  SET_DEST (PATTERN (i1)));
1384         }
1385     }
1386 #endif
1387
1388   /* Verify that I2 and I1 are valid for combining.  */
1389   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1390       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1391     {
1392       undo_all ();
1393       return 0;
1394     }
1395
1396   /* Record whether I2DEST is used in I2SRC and similarly for the other
1397      cases.  Knowing this will help in register status updating below.  */
1398   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1399   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1400   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1401
1402   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
1403      in I2SRC.  */
1404   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1405
1406   /* Ensure that I3's pattern can be the destination of combines.  */
1407   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1408                           i1 && i2dest_in_i1src && i1_feeds_i3,
1409                           &i3dest_killed))
1410     {
1411       undo_all ();
1412       return 0;
1413     }
1414
1415   /* See if any of the insns is a MULT operation.  Unless one is, we will
1416      reject a combination that is, since it must be slower.  Be conservative
1417      here.  */
1418   if (GET_CODE (i2src) == MULT
1419       || (i1 != 0 && GET_CODE (i1src) == MULT)
1420       || (GET_CODE (PATTERN (i3)) == SET
1421           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1422     have_mult = 1;
1423
1424   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1425      We used to do this EXCEPT in one case: I3 has a post-inc in an
1426      output operand.  However, that exception can give rise to insns like
1427         mov r3,(r3)+
1428      which is a famous insn on the PDP-11 where the value of r3 used as the
1429      source was model-dependent.  Avoid this sort of thing.  */
1430
1431 #if 0
1432   if (!(GET_CODE (PATTERN (i3)) == SET
1433         && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1434         && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1435         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1436             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1437     /* It's not the exception.  */
1438 #endif
1439 #ifdef AUTO_INC_DEC
1440     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1441       if (REG_NOTE_KIND (link) == REG_INC
1442           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1443               || (i1 != 0
1444                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1445         {
1446           undo_all ();
1447           return 0;
1448         }
1449 #endif
1450
1451   /* See if the SETs in I1 or I2 need to be kept around in the merged
1452      instruction: whenever the value set there is still needed past I3.
1453      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1454
1455      For the SET in I1, we have two cases:  If I1 and I2 independently
1456      feed into I3, the set in I1 needs to be kept around if I1DEST dies
1457      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
1458      in I1 needs to be kept around unless I1DEST dies or is set in either
1459      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
1460      I1DEST.  If so, we know I1 feeds into I2.  */
1461
1462   added_sets_2 = ! dead_or_set_p (i3, i2dest);
1463
1464   added_sets_1
1465     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1466                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1467
1468   /* If the set in I2 needs to be kept around, we must make a copy of
1469      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1470      PATTERN (I2), we are only substituting for the original I1DEST, not into
1471      an already-substituted copy.  This also prevents making self-referential
1472      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1473      I2DEST.  */
1474
1475   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1476            ? gen_rtx (SET, VOIDmode, i2dest, i2src)
1477            : PATTERN (i2));
1478
1479   if (added_sets_2)
1480     i2pat = copy_rtx (i2pat);
1481
1482   combine_merges++;
1483
1484   /* Substitute in the latest insn for the regs set by the earlier ones.  */
1485
1486   maxreg = max_reg_num ();
1487
1488   subst_insn = i3;
1489
1490   /* It is possible that the source of I2 or I1 may be performing an
1491      unneeded operation, such as a ZERO_EXTEND of something that is known
1492      to have the high part zero.  Handle that case by letting subst look at
1493      the innermost one of them.
1494
1495      Another way to do this would be to have a function that tries to
1496      simplify a single insn instead of merging two or more insns.  We don't
1497      do this because of the potential of infinite loops and because
1498      of the potential extra memory required.  However, doing it the way
1499      we are is a bit of a kludge and doesn't catch all cases.
1500
1501      But only do this if -fexpensive-optimizations since it slows things down
1502      and doesn't usually win.  */
1503
1504   if (flag_expensive_optimizations)
1505     {
1506       /* Pass pc_rtx so no substitutions are done, just simplifications.
1507          The cases that we are interested in here do not involve the few
1508          cases were is_replaced is checked.  */
1509       if (i1)
1510         {
1511           subst_low_cuid = INSN_CUID (i1);
1512           i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1513         }
1514       else
1515         {
1516           subst_low_cuid = INSN_CUID (i2);
1517           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1518         }
1519
1520       previous_num_undos = undobuf.num_undo;
1521     }
1522
1523 #ifndef HAVE_cc0
1524   /* Many machines that don't use CC0 have insns that can both perform an
1525      arithmetic operation and set the condition code.  These operations will
1526      be represented as a PARALLEL with the first element of the vector
1527      being a COMPARE of an arithmetic operation with the constant zero.
1528      The second element of the vector will set some pseudo to the result
1529      of the same arithmetic operation.  If we simplify the COMPARE, we won't
1530      match such a pattern and so will generate an extra insn.   Here we test
1531      for this case, where both the comparison and the operation result are
1532      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1533      I2SRC.  Later we will make the PARALLEL that contains I2.  */
1534
1535   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1536       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1537       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1538       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1539     {
1540       rtx *cc_use;
1541       enum machine_mode compare_mode;
1542
1543       newpat = PATTERN (i3);
1544       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1545
1546       i2_is_used = 1;
1547
1548 #ifdef EXTRA_CC_MODES
1549       /* See if a COMPARE with the operand we substituted in should be done
1550          with the mode that is currently being used.  If not, do the same
1551          processing we do in `subst' for a SET; namely, if the destination
1552          is used only once, try to replace it with a register of the proper
1553          mode and also replace the COMPARE.  */
1554       if (undobuf.other_insn == 0
1555           && (cc_use = find_single_use (SET_DEST (newpat), i3,
1556                                         &undobuf.other_insn))
1557           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1558                                               i2src, const0_rtx))
1559               != GET_MODE (SET_DEST (newpat))))
1560         {
1561           int regno = REGNO (SET_DEST (newpat));
1562           rtx new_dest = gen_rtx (REG, compare_mode, regno);
1563
1564           if (regno < FIRST_PSEUDO_REGISTER
1565               || (reg_n_sets[regno] == 1 && ! added_sets_2
1566                   && ! REG_USERVAR_P (SET_DEST (newpat))))
1567             {
1568               if (regno >= FIRST_PSEUDO_REGISTER)
1569                 SUBST (regno_reg_rtx[regno], new_dest);
1570
1571               SUBST (SET_DEST (newpat), new_dest);
1572               SUBST (XEXP (*cc_use, 0), new_dest);
1573               SUBST (SET_SRC (newpat),
1574                      gen_rtx_combine (COMPARE, compare_mode,
1575                                       i2src, const0_rtx));
1576             }
1577           else
1578             undobuf.other_insn = 0;
1579         }
1580 #endif    
1581     }
1582   else
1583 #endif
1584     {
1585       n_occurrences = 0;                /* `subst' counts here */
1586
1587       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1588          need to make a unique copy of I2SRC each time we substitute it
1589          to avoid self-referential rtl.  */
1590
1591       subst_low_cuid = INSN_CUID (i2);
1592       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1593                       ! i1_feeds_i3 && i1dest_in_i1src);
1594       previous_num_undos = undobuf.num_undo;
1595
1596       /* Record whether i2's body now appears within i3's body.  */
1597       i2_is_used = n_occurrences;
1598     }
1599
1600   /* If we already got a failure, don't try to do more.  Otherwise,
1601      try to substitute in I1 if we have it.  */
1602
1603   if (i1 && GET_CODE (newpat) != CLOBBER)
1604     {
1605       /* Before we can do this substitution, we must redo the test done
1606          above (see detailed comments there) that ensures  that I1DEST
1607          isn't mentioned in any SETs in NEWPAT that are field assignments. */
1608
1609       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1610                               0, NULL_PTR))
1611         {
1612           undo_all ();
1613           return 0;
1614         }
1615
1616       n_occurrences = 0;
1617       subst_low_cuid = INSN_CUID (i1);
1618       newpat = subst (newpat, i1dest, i1src, 0, 0);
1619       previous_num_undos = undobuf.num_undo;
1620     }
1621
1622   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
1623      to count all the ways that I2SRC and I1SRC can be used.  */
1624   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1625        && i2_is_used + added_sets_2 > 1)
1626       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1627           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1628               > 1))
1629       /* Fail if we tried to make a new register (we used to abort, but there's
1630          really no reason to).  */
1631       || max_reg_num () != maxreg
1632       /* Fail if we couldn't do something and have a CLOBBER.  */
1633       || GET_CODE (newpat) == CLOBBER
1634       /* Fail if this new pattern is a MULT and we didn't have one before
1635          at the outer level.  */
1636       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1637           && ! have_mult))
1638     {
1639       undo_all ();
1640       return 0;
1641     }
1642
1643   /* If the actions of the earlier insns must be kept
1644      in addition to substituting them into the latest one,
1645      we must make a new PARALLEL for the latest insn
1646      to hold additional the SETs.  */
1647
1648   if (added_sets_1 || added_sets_2)
1649     {
1650       combine_extras++;
1651
1652       if (GET_CODE (newpat) == PARALLEL)
1653         {
1654           rtvec old = XVEC (newpat, 0);
1655           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1656           newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
1657           bcopy ((char *) &old->elem[0], (char *) &XVECEXP (newpat, 0, 0),
1658                  sizeof (old->elem[0]) * old->num_elem);
1659         }
1660       else
1661         {
1662           rtx old = newpat;
1663           total_sets = 1 + added_sets_1 + added_sets_2;
1664           newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
1665           XVECEXP (newpat, 0, 0) = old;
1666         }
1667
1668      if (added_sets_1)
1669        XVECEXP (newpat, 0, --total_sets)
1670          = (GET_CODE (PATTERN (i1)) == PARALLEL
1671             ? gen_rtx (SET, VOIDmode, i1dest, i1src) : PATTERN (i1));
1672
1673      if (added_sets_2)
1674         {
1675           /* If there is no I1, use I2's body as is.  We used to also not do
1676              the subst call below if I2 was substituted into I3,
1677              but that could lose a simplification.  */
1678           if (i1 == 0)
1679             XVECEXP (newpat, 0, --total_sets) = i2pat;
1680           else
1681             /* See comment where i2pat is assigned.  */
1682             XVECEXP (newpat, 0, --total_sets)
1683               = subst (i2pat, i1dest, i1src, 0, 0);
1684         }
1685     }
1686
1687   /* We come here when we are replacing a destination in I2 with the
1688      destination of I3.  */
1689  validate_replacement:
1690
1691   /* Note which hard regs this insn has as inputs.  */
1692   mark_used_regs_combine (newpat);
1693
1694   /* Is the result of combination a valid instruction?  */
1695   insn_code_number
1696     = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1697
1698   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1699      the second SET's destination is a register that is unused.  In that case,
1700      we just need the first SET.   This can occur when simplifying a divmod
1701      insn.  We *must* test for this case here because the code below that
1702      splits two independent SETs doesn't handle this case correctly when it
1703      updates the register status.  Also check the case where the first
1704      SET's destination is unused.  That would not cause incorrect code, but
1705      does cause an unneeded insn to remain.  */
1706
1707   if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1708       && XVECLEN (newpat, 0) == 2
1709       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1710       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1711       && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1712       && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1713       && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1714       && asm_noperands (newpat) < 0)
1715     {
1716       newpat = XVECEXP (newpat, 0, 0);
1717       insn_code_number
1718         = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1719     }
1720
1721   else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1722            && XVECLEN (newpat, 0) == 2
1723            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1724            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1725            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1726            && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1727            && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1728            && asm_noperands (newpat) < 0)
1729     {
1730       newpat = XVECEXP (newpat, 0, 1);
1731       insn_code_number
1732         = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1733     }
1734
1735   /* If we were combining three insns and the result is a simple SET
1736      with no ASM_OPERANDS that wasn't recognized, try to split it into two
1737      insns.  There are two ways to do this.  It can be split using a 
1738      machine-specific method (like when you have an addition of a large
1739      constant) or by combine in the function find_split_point.  */
1740
1741   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1742       && asm_noperands (newpat) < 0)
1743     {
1744       rtx m_split, *split;
1745       rtx ni2dest = i2dest;
1746
1747       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
1748          use I2DEST as a scratch register will help.  In the latter case,
1749          convert I2DEST to the mode of the source of NEWPAT if we can.  */
1750
1751       m_split = split_insns (newpat, i3);
1752
1753       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1754          inputs of NEWPAT.  */
1755
1756       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1757          possible to try that as a scratch reg.  This would require adding
1758          more code to make it work though.  */
1759
1760       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
1761         {
1762           /* If I2DEST is a hard register or the only use of a pseudo,
1763              we can change its mode.  */
1764           if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
1765               && GET_MODE (SET_DEST (newpat)) != VOIDmode
1766               && GET_CODE (i2dest) == REG
1767               && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1768                   || (reg_n_sets[REGNO (i2dest)] == 1 && ! added_sets_2
1769                       && ! REG_USERVAR_P (i2dest))))
1770             ni2dest = gen_rtx (REG, GET_MODE (SET_DEST (newpat)),
1771                                REGNO (i2dest));
1772
1773           m_split = split_insns (gen_rtx (PARALLEL, VOIDmode,
1774                                           gen_rtvec (2, newpat,
1775                                                      gen_rtx (CLOBBER,
1776                                                               VOIDmode,
1777                                                               ni2dest))),
1778                                  i3);
1779         }
1780
1781       if (m_split && GET_CODE (m_split) == SEQUENCE
1782           && XVECLEN (m_split, 0) == 2
1783           && (next_real_insn (i2) == i3
1784               || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1785                                       INSN_CUID (i2))))
1786         {
1787           rtx i2set, i3set;
1788           rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
1789           newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
1790
1791           i3set = single_set (XVECEXP (m_split, 0, 1));
1792           i2set = single_set (XVECEXP (m_split, 0, 0));
1793
1794           /* In case we changed the mode of I2DEST, replace it in the
1795              pseudo-register table here.  We can't do it above in case this
1796              code doesn't get executed and we do a split the other way.  */
1797
1798           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1799             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1800
1801           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes,
1802                                               &i2_scratches);
1803
1804           /* If I2 or I3 has multiple SETs, we won't know how to track
1805              register status, so don't use these insns.  */
1806
1807           if (i2_code_number >= 0 && i2set && i3set)
1808             insn_code_number = recog_for_combine (&newi3pat, i3, &new_i3_notes,
1809                                                   &i3_scratches); 
1810           if (insn_code_number >= 0)
1811             newpat = newi3pat;
1812
1813           /* It is possible that both insns now set the destination of I3.
1814              If so, we must show an extra use of it.  */
1815
1816           if (insn_code_number >= 0)
1817             {
1818               rtx new_i3_dest = SET_DEST (i3set);
1819               rtx new_i2_dest = SET_DEST (i2set);
1820
1821               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
1822                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
1823                      || GET_CODE (new_i3_dest) == SUBREG)
1824                 new_i3_dest = XEXP (new_i3_dest, 0);
1825
1826               if (GET_CODE (new_i3_dest) == REG
1827                   && GET_CODE (new_i2_dest) == REG
1828                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
1829                 reg_n_sets[REGNO (SET_DEST (i2set))]++;
1830             }
1831         }
1832
1833       /* If we can split it and use I2DEST, go ahead and see if that
1834          helps things be recognized.  Verify that none of the registers
1835          are set between I2 and I3.  */
1836       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
1837 #ifdef HAVE_cc0
1838           && GET_CODE (i2dest) == REG
1839 #endif
1840           /* We need I2DEST in the proper mode.  If it is a hard register
1841              or the only use of a pseudo, we can change its mode.  */
1842           && (GET_MODE (*split) == GET_MODE (i2dest)
1843               || GET_MODE (*split) == VOIDmode
1844               || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1845               || (reg_n_sets[REGNO (i2dest)] == 1 && ! added_sets_2
1846                   && ! REG_USERVAR_P (i2dest)))
1847           && (next_real_insn (i2) == i3
1848               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
1849           /* We can't overwrite I2DEST if its value is still used by
1850              NEWPAT.  */
1851           && ! reg_referenced_p (i2dest, newpat))
1852         {
1853           rtx newdest = i2dest;
1854           enum rtx_code split_code = GET_CODE (*split);
1855           enum machine_mode split_mode = GET_MODE (*split);
1856
1857           /* Get NEWDEST as a register in the proper mode.  We have already
1858              validated that we can do this.  */
1859           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
1860             {
1861               newdest = gen_rtx (REG, split_mode, REGNO (i2dest));
1862
1863               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1864                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
1865             }
1866
1867           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
1868              an ASHIFT.  This can occur if it was inside a PLUS and hence
1869              appeared to be a memory address.  This is a kludge.  */
1870           if (split_code == MULT
1871               && GET_CODE (XEXP (*split, 1)) == CONST_INT
1872               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
1873             {
1874               SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
1875                                               XEXP (*split, 0), GEN_INT (i)));
1876               /* Update split_code because we may not have a multiply
1877                  anymore.  */
1878               split_code = GET_CODE (*split);
1879             }
1880
1881 #ifdef INSN_SCHEDULING
1882           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
1883              be written as a ZERO_EXTEND.  */
1884           if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
1885             SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
1886                                             XEXP (*split, 0)));
1887 #endif
1888
1889           newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
1890           SUBST (*split, newdest);
1891           i2_code_number
1892             = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
1893
1894           /* If the split point was a MULT and we didn't have one before,
1895              don't use one now.  */
1896           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
1897             insn_code_number
1898               = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1899         }
1900     }
1901
1902   /* Check for a case where we loaded from memory in a narrow mode and
1903      then sign extended it, but we need both registers.  In that case,
1904      we have a PARALLEL with both loads from the same memory location.
1905      We can split this into a load from memory followed by a register-register
1906      copy.  This saves at least one insn, more if register allocation can
1907      eliminate the copy.
1908
1909      We cannot do this if the destination of the second assignment is
1910      a register that we have already assumed is zero-extended.  Similarly
1911      for a SUBREG of such a register.  */
1912
1913   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
1914            && GET_CODE (newpat) == PARALLEL
1915            && XVECLEN (newpat, 0) == 2
1916            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1917            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
1918            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1919            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
1920                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
1921            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
1922                                    INSN_CUID (i2))
1923            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
1924            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
1925            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
1926                  (GET_CODE (temp) == REG
1927                   && reg_nonzero_bits[REGNO (temp)] != 0
1928                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
1929                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
1930                   && (reg_nonzero_bits[REGNO (temp)]
1931                       != GET_MODE_MASK (word_mode))))
1932            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
1933                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
1934                      (GET_CODE (temp) == REG
1935                       && reg_nonzero_bits[REGNO (temp)] != 0
1936                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
1937                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
1938                       && (reg_nonzero_bits[REGNO (temp)]
1939                           != GET_MODE_MASK (word_mode)))))
1940            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
1941                                          SET_SRC (XVECEXP (newpat, 0, 1)))
1942            && ! find_reg_note (i3, REG_UNUSED,
1943                                SET_DEST (XVECEXP (newpat, 0, 0))))
1944     {
1945       rtx ni2dest;
1946
1947       newi2pat = XVECEXP (newpat, 0, 0);
1948       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
1949       newpat = XVECEXP (newpat, 0, 1);
1950       SUBST (SET_SRC (newpat),
1951              gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
1952       i2_code_number
1953         = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
1954
1955       if (i2_code_number >= 0)
1956         insn_code_number
1957           = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1958
1959       if (insn_code_number >= 0)
1960         {
1961           rtx insn;
1962           rtx link;
1963
1964           /* If we will be able to accept this, we have made a change to the
1965              destination of I3.  This can invalidate a LOG_LINKS pointing
1966              to I3.  No other part of combine.c makes such a transformation.
1967
1968              The new I3 will have a destination that was previously the
1969              destination of I1 or I2 and which was used in i2 or I3.  Call
1970              distribute_links to make a LOG_LINK from the next use of
1971              that destination.  */
1972
1973           PATTERN (i3) = newpat;
1974           distribute_links (gen_rtx (INSN_LIST, VOIDmode, i3, NULL_RTX));
1975
1976           /* I3 now uses what used to be its destination and which is
1977              now I2's destination.  That means we need a LOG_LINK from
1978              I3 to I2.  But we used to have one, so we still will.
1979
1980              However, some later insn might be using I2's dest and have
1981              a LOG_LINK pointing at I3.  We must remove this link.
1982              The simplest way to remove the link is to point it at I1,
1983              which we know will be a NOTE.  */
1984
1985           for (insn = NEXT_INSN (i3);
1986                insn && (this_basic_block == n_basic_blocks - 1
1987                         || insn != basic_block_head[this_basic_block + 1]);
1988                insn = NEXT_INSN (insn))
1989             {
1990               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1991                   && reg_referenced_p (ni2dest, PATTERN (insn)))
1992                 {
1993                   for (link = LOG_LINKS (insn); link;
1994                        link = XEXP (link, 1))
1995                     if (XEXP (link, 0) == i3)
1996                       XEXP (link, 0) = i1;
1997
1998                   break;
1999                 }
2000             }
2001         }
2002     }
2003             
2004   /* Similarly, check for a case where we have a PARALLEL of two independent
2005      SETs but we started with three insns.  In this case, we can do the sets
2006      as two separate insns.  This case occurs when some SET allows two
2007      other insns to combine, but the destination of that SET is still live.  */
2008
2009   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2010            && GET_CODE (newpat) == PARALLEL
2011            && XVECLEN (newpat, 0) == 2
2012            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2013            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2014            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2015            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2016            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2017            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2018            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2019                                    INSN_CUID (i2))
2020            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
2021            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2022            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2023            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2024                                   XVECEXP (newpat, 0, 0))
2025            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2026                                   XVECEXP (newpat, 0, 1)))
2027     {
2028       newi2pat = XVECEXP (newpat, 0, 1);
2029       newpat = XVECEXP (newpat, 0, 0);
2030
2031       i2_code_number
2032         = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2033
2034       if (i2_code_number >= 0)
2035         insn_code_number
2036           = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
2037     }
2038
2039   /* If it still isn't recognized, fail and change things back the way they
2040      were.  */
2041   if ((insn_code_number < 0
2042        /* Is the result a reasonable ASM_OPERANDS?  */
2043        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2044     {
2045       undo_all ();
2046       return 0;
2047     }
2048
2049   /* If we had to change another insn, make sure it is valid also.  */
2050   if (undobuf.other_insn)
2051     {
2052       rtx other_pat = PATTERN (undobuf.other_insn);
2053       rtx new_other_notes;
2054       rtx note, next;
2055
2056       CLEAR_HARD_REG_SET (newpat_used_regs);
2057
2058       other_code_number
2059         = recog_for_combine (&other_pat, undobuf.other_insn,
2060                              &new_other_notes, &other_scratches);
2061
2062       if (other_code_number < 0 && ! check_asm_operands (other_pat))
2063         {
2064           undo_all ();
2065           return 0;
2066         }
2067
2068       PATTERN (undobuf.other_insn) = other_pat;
2069
2070       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2071          are still valid.  Then add any non-duplicate notes added by
2072          recog_for_combine.  */
2073       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2074         {
2075           next = XEXP (note, 1);
2076
2077           if (REG_NOTE_KIND (note) == REG_UNUSED
2078               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2079             {
2080               if (GET_CODE (XEXP (note, 0)) == REG)
2081                 reg_n_deaths[REGNO (XEXP (note, 0))]--;
2082
2083               remove_note (undobuf.other_insn, note);
2084             }
2085         }
2086
2087       for (note = new_other_notes; note; note = XEXP (note, 1))
2088         if (GET_CODE (XEXP (note, 0)) == REG)
2089           reg_n_deaths[REGNO (XEXP (note, 0))]++;
2090
2091       distribute_notes (new_other_notes, undobuf.other_insn,
2092                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2093     }
2094
2095   /* We now know that we can do this combination.  Merge the insns and 
2096      update the status of registers and LOG_LINKS.  */
2097
2098   {
2099     rtx i3notes, i2notes, i1notes = 0;
2100     rtx i3links, i2links, i1links = 0;
2101     rtx midnotes = 0;
2102     register int regno;
2103     /* Compute which registers we expect to eliminate.  */
2104     rtx elim_i2 = (newi2pat || i2dest_in_i2src || i2dest_in_i1src
2105                    ? 0 : i2dest);
2106     rtx elim_i1 = i1 == 0 || i1dest_in_i1src ? 0 : i1dest;
2107
2108     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2109        clear them.  */
2110     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2111     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2112     if (i1)
2113       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2114
2115     /* Ensure that we do not have something that should not be shared but
2116        occurs multiple times in the new insns.  Check this by first
2117        resetting all the `used' flags and then copying anything is shared.  */
2118
2119     reset_used_flags (i3notes);
2120     reset_used_flags (i2notes);
2121     reset_used_flags (i1notes);
2122     reset_used_flags (newpat);
2123     reset_used_flags (newi2pat);
2124     if (undobuf.other_insn)
2125       reset_used_flags (PATTERN (undobuf.other_insn));
2126
2127     i3notes = copy_rtx_if_shared (i3notes);
2128     i2notes = copy_rtx_if_shared (i2notes);
2129     i1notes = copy_rtx_if_shared (i1notes);
2130     newpat = copy_rtx_if_shared (newpat);
2131     newi2pat = copy_rtx_if_shared (newi2pat);
2132     if (undobuf.other_insn)
2133       reset_used_flags (PATTERN (undobuf.other_insn));
2134
2135     INSN_CODE (i3) = insn_code_number;
2136     PATTERN (i3) = newpat;
2137     if (undobuf.other_insn)
2138       INSN_CODE (undobuf.other_insn) = other_code_number;
2139
2140     /* We had one special case above where I2 had more than one set and
2141        we replaced a destination of one of those sets with the destination
2142        of I3.  In that case, we have to update LOG_LINKS of insns later
2143        in this basic block.  Note that this (expensive) case is rare.
2144
2145        Also, in this case, we must pretend that all REG_NOTEs for I2
2146        actually came from I3, so that REG_UNUSED notes from I2 will be
2147        properly handled.  */
2148
2149     if (i3_subst_into_i2)
2150       {
2151         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2152           if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2153               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2154               && ! find_reg_note (i2, REG_UNUSED,
2155                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2156             for (temp = NEXT_INSN (i2);
2157                  temp && (this_basic_block == n_basic_blocks - 1
2158                           || basic_block_head[this_basic_block] != temp);
2159                  temp = NEXT_INSN (temp))
2160               if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2161                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2162                   if (XEXP (link, 0) == i2)
2163                     XEXP (link, 0) = i3;
2164
2165         if (i3notes)
2166           {
2167             rtx link = i3notes;
2168             while (XEXP (link, 1))
2169               link = XEXP (link, 1);
2170             XEXP (link, 1) = i2notes;
2171           }
2172         else
2173           i3notes = i2notes;
2174         i2notes = 0;
2175       }
2176
2177     LOG_LINKS (i3) = 0;
2178     REG_NOTES (i3) = 0;
2179     LOG_LINKS (i2) = 0;
2180     REG_NOTES (i2) = 0;
2181
2182     if (newi2pat)
2183       {
2184         INSN_CODE (i2) = i2_code_number;
2185         PATTERN (i2) = newi2pat;
2186       }
2187     else
2188       {
2189         PUT_CODE (i2, NOTE);
2190         NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2191         NOTE_SOURCE_FILE (i2) = 0;
2192       }
2193
2194     if (i1)
2195       {
2196         LOG_LINKS (i1) = 0;
2197         REG_NOTES (i1) = 0;
2198         PUT_CODE (i1, NOTE);
2199         NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2200         NOTE_SOURCE_FILE (i1) = 0;
2201       }
2202
2203     /* Get death notes for everything that is now used in either I3 or
2204        I2 and used to die in a previous insn.  */
2205
2206     move_deaths (newpat, i1 ? INSN_CUID (i1) : INSN_CUID (i2), i3, &midnotes);
2207     if (newi2pat)
2208       move_deaths (newi2pat, INSN_CUID (i1), i2, &midnotes);
2209
2210     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
2211     if (i3notes)
2212       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2213                         elim_i2, elim_i1);
2214     if (i2notes)
2215       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2216                         elim_i2, elim_i1);
2217     if (i1notes)
2218       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2219                         elim_i2, elim_i1);
2220     if (midnotes)
2221       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2222                         elim_i2, elim_i1);
2223
2224     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
2225        know these are REG_UNUSED and want them to go to the desired insn,
2226        so we always pass it as i3.  We have not counted the notes in 
2227        reg_n_deaths yet, so we need to do so now.  */
2228
2229     if (newi2pat && new_i2_notes)
2230       {
2231         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2232           if (GET_CODE (XEXP (temp, 0)) == REG)
2233             reg_n_deaths[REGNO (XEXP (temp, 0))]++;
2234         
2235         distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2236       }
2237
2238     if (new_i3_notes)
2239       {
2240         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2241           if (GET_CODE (XEXP (temp, 0)) == REG)
2242             reg_n_deaths[REGNO (XEXP (temp, 0))]++;
2243         
2244         distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2245       }
2246
2247     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
2248        put a REG_DEAD note for it somewhere.  Similarly for I2 and I1.
2249        Show an additional death due to the REG_DEAD note we make here.  If
2250        we discard it in distribute_notes, we will decrement it again.  */
2251
2252     if (i3dest_killed)
2253       {
2254         if (GET_CODE (i3dest_killed) == REG)
2255           reg_n_deaths[REGNO (i3dest_killed)]++;
2256
2257         distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i3dest_killed,
2258                                    NULL_RTX),
2259                           NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2260                           NULL_RTX, NULL_RTX);
2261       }
2262
2263     /* For I2 and I1, we have to be careful.  If NEWI2PAT exists and sets
2264        I2DEST or I1DEST, the death must be somewhere before I2, not I3.  If
2265        we passed I3 in that case, it might delete I2.  */
2266
2267     if (i2dest_in_i2src)
2268       {
2269         if (GET_CODE (i2dest) == REG)
2270           reg_n_deaths[REGNO (i2dest)]++;
2271
2272         if (newi2pat && reg_set_p (i2dest, newi2pat))
2273           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
2274                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2275         else
2276           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
2277                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2278                             NULL_RTX, NULL_RTX);
2279       }
2280
2281     if (i1dest_in_i1src)
2282       {
2283         if (GET_CODE (i1dest) == REG)
2284           reg_n_deaths[REGNO (i1dest)]++;
2285
2286         if (newi2pat && reg_set_p (i1dest, newi2pat))
2287           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
2288                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2289         else
2290           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
2291                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2292                             NULL_RTX, NULL_RTX);
2293       }
2294
2295     distribute_links (i3links);
2296     distribute_links (i2links);
2297     distribute_links (i1links);
2298
2299     if (GET_CODE (i2dest) == REG)
2300       {
2301         rtx link;
2302         rtx i2_insn = 0, i2_val = 0, set;
2303
2304         /* The insn that used to set this register doesn't exist, and
2305            this life of the register may not exist either.  See if one of
2306            I3's links points to an insn that sets I2DEST.  If it does, 
2307            that is now the last known value for I2DEST. If we don't update
2308            this and I2 set the register to a value that depended on its old
2309            contents, we will get confused.  If this insn is used, thing
2310            will be set correctly in combine_instructions.  */
2311
2312         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2313           if ((set = single_set (XEXP (link, 0))) != 0
2314               && rtx_equal_p (i2dest, SET_DEST (set)))
2315             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2316
2317         record_value_for_reg (i2dest, i2_insn, i2_val);
2318
2319         /* If the reg formerly set in I2 died only once and that was in I3,
2320            zero its use count so it won't make `reload' do any work.  */
2321         if (! added_sets_2 && newi2pat == 0 && ! i2dest_in_i2src)
2322           {
2323             regno = REGNO (i2dest);
2324             reg_n_sets[regno]--;
2325             if (reg_n_sets[regno] == 0
2326                 && ! (basic_block_live_at_start[0][regno / REGSET_ELT_BITS]
2327                       & ((REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS))))
2328               reg_n_refs[regno] = 0;
2329           }
2330       }
2331
2332     if (i1 && GET_CODE (i1dest) == REG)
2333       {
2334         rtx link;
2335         rtx i1_insn = 0, i1_val = 0, set;
2336
2337         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2338           if ((set = single_set (XEXP (link, 0))) != 0
2339               && rtx_equal_p (i1dest, SET_DEST (set)))
2340             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2341
2342         record_value_for_reg (i1dest, i1_insn, i1_val);
2343
2344         regno = REGNO (i1dest);
2345         if (! added_sets_1 && ! i1dest_in_i1src)
2346           {
2347             reg_n_sets[regno]--;
2348             if (reg_n_sets[regno] == 0
2349                 && ! (basic_block_live_at_start[0][regno / REGSET_ELT_BITS]
2350                       & ((REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS))))
2351               reg_n_refs[regno] = 0;
2352           }
2353       }
2354
2355     /* Update reg_nonzero_bits et al for any changes that may have been made
2356        to this insn.  */
2357
2358     note_stores (newpat, set_nonzero_bits_and_sign_copies);
2359     if (newi2pat)
2360       note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
2361
2362     /* If we added any (clobber (scratch)), add them to the max for a
2363        block.  This is a very pessimistic calculation, since we might
2364        have had them already and this might not be the worst block, but
2365        it's not worth doing any better.  */
2366     max_scratch += i3_scratches + i2_scratches + other_scratches;
2367
2368     /* If I3 is now an unconditional jump, ensure that it has a 
2369        BARRIER following it since it may have initially been a
2370        conditional jump.  It may also be the last nonnote insn.  */
2371
2372     if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
2373         && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2374             || GET_CODE (temp) != BARRIER))
2375       emit_barrier_after (i3);
2376   }
2377
2378   combine_successes++;
2379
2380   /* Clear this here, so that subsequent get_last_value calls are not
2381      affected.  */
2382   subst_prev_insn = NULL_RTX;
2383
2384   if (added_links_insn
2385       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2386       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2387     return added_links_insn;
2388   else
2389     return newi2pat ? i2 : i3;
2390 }
2391 \f
2392 /* Undo all the modifications recorded in undobuf.  */
2393
2394 static void
2395 undo_all ()
2396 {
2397   register int i;
2398   if (undobuf.num_undo > MAX_UNDO)
2399     undobuf.num_undo = MAX_UNDO;
2400   for (i = undobuf.num_undo - 1; i >= 0; i--)
2401     {
2402       if (undobuf.undo[i].is_int)
2403         *undobuf.undo[i].where.i = undobuf.undo[i].old_contents.i;
2404       else
2405         *undobuf.undo[i].where.r = undobuf.undo[i].old_contents.r;
2406       
2407     }
2408
2409   obfree (undobuf.storage);
2410   undobuf.num_undo = 0;
2411
2412   /* Clear this here, so that subsequent get_last_value calls are not
2413      affected.  */
2414   subst_prev_insn = NULL_RTX;
2415 }
2416 \f
2417 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2418    where we have an arithmetic expression and return that point.  LOC will
2419    be inside INSN.
2420
2421    try_combine will call this function to see if an insn can be split into
2422    two insns.  */
2423
2424 static rtx *
2425 find_split_point (loc, insn)
2426      rtx *loc;
2427      rtx insn;
2428 {
2429   rtx x = *loc;
2430   enum rtx_code code = GET_CODE (x);
2431   rtx *split;
2432   int len = 0, pos, unsignedp;
2433   rtx inner;
2434
2435   /* First special-case some codes.  */
2436   switch (code)
2437     {
2438     case SUBREG:
2439 #ifdef INSN_SCHEDULING
2440       /* If we are making a paradoxical SUBREG invalid, it becomes a split
2441          point.  */
2442       if (GET_CODE (SUBREG_REG (x)) == MEM)
2443         return loc;
2444 #endif
2445       return find_split_point (&SUBREG_REG (x), insn);
2446
2447     case MEM:
2448 #ifdef HAVE_lo_sum
2449       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2450          using LO_SUM and HIGH.  */
2451       if (GET_CODE (XEXP (x, 0)) == CONST
2452           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2453         {
2454           SUBST (XEXP (x, 0),
2455                  gen_rtx_combine (LO_SUM, Pmode,
2456                                   gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2457                                   XEXP (x, 0)));
2458           return &XEXP (XEXP (x, 0), 0);
2459         }
2460 #endif
2461
2462       /* If we have a PLUS whose second operand is a constant and the
2463          address is not valid, perhaps will can split it up using
2464          the machine-specific way to split large constants.  We use
2465          the first pseudo-reg (one of the virtual regs) as a placeholder;
2466          it will not remain in the result.  */
2467       if (GET_CODE (XEXP (x, 0)) == PLUS
2468           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2469           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2470         {
2471           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2472           rtx seq = split_insns (gen_rtx (SET, VOIDmode, reg, XEXP (x, 0)),
2473                                  subst_insn);
2474
2475           /* This should have produced two insns, each of which sets our
2476              placeholder.  If the source of the second is a valid address,
2477              we can make put both sources together and make a split point
2478              in the middle.  */
2479
2480           if (seq && XVECLEN (seq, 0) == 2
2481               && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2482               && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2483               && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2484               && ! reg_mentioned_p (reg,
2485                                     SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2486               && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2487               && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2488               && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2489               && memory_address_p (GET_MODE (x),
2490                                    SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2491             {
2492               rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2493               rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2494
2495               /* Replace the placeholder in SRC2 with SRC1.  If we can
2496                  find where in SRC2 it was placed, that can become our
2497                  split point and we can replace this address with SRC2.
2498                  Just try two obvious places.  */
2499
2500               src2 = replace_rtx (src2, reg, src1);
2501               split = 0;
2502               if (XEXP (src2, 0) == src1)
2503                 split = &XEXP (src2, 0);
2504               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2505                        && XEXP (XEXP (src2, 0), 0) == src1)
2506                 split = &XEXP (XEXP (src2, 0), 0);
2507
2508               if (split)
2509                 {
2510                   SUBST (XEXP (x, 0), src2);
2511                   return split;
2512                 }
2513             }
2514           
2515           /* If that didn't work, perhaps the first operand is complex and
2516              needs to be computed separately, so make a split point there.
2517              This will occur on machines that just support REG + CONST
2518              and have a constant moved through some previous computation.  */
2519
2520           else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2521                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2522                          && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2523                              == 'o')))
2524             return &XEXP (XEXP (x, 0), 0);
2525         }
2526       break;
2527
2528     case SET:
2529 #ifdef HAVE_cc0
2530       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2531          ZERO_EXTRACT, the most likely reason why this doesn't match is that
2532          we need to put the operand into a register.  So split at that
2533          point.  */
2534
2535       if (SET_DEST (x) == cc0_rtx
2536           && GET_CODE (SET_SRC (x)) != COMPARE
2537           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2538           && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2539           && ! (GET_CODE (SET_SRC (x)) == SUBREG
2540                 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2541         return &SET_SRC (x);
2542 #endif
2543
2544       /* See if we can split SET_SRC as it stands.  */
2545       split = find_split_point (&SET_SRC (x), insn);
2546       if (split && split != &SET_SRC (x))
2547         return split;
2548
2549       /* See if we can split SET_DEST as it stands.  */
2550       split = find_split_point (&SET_DEST (x), insn);
2551       if (split && split != &SET_DEST (x))
2552         return split;
2553
2554       /* See if this is a bitfield assignment with everything constant.  If
2555          so, this is an IOR of an AND, so split it into that.  */
2556       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2557           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2558               <= HOST_BITS_PER_WIDE_INT)
2559           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2560           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2561           && GET_CODE (SET_SRC (x)) == CONST_INT
2562           && ((INTVAL (XEXP (SET_DEST (x), 1))
2563               + INTVAL (XEXP (SET_DEST (x), 2)))
2564               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2565           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2566         {
2567           int pos = INTVAL (XEXP (SET_DEST (x), 2));
2568           int len = INTVAL (XEXP (SET_DEST (x), 1));
2569           int src = INTVAL (SET_SRC (x));
2570           rtx dest = XEXP (SET_DEST (x), 0);
2571           enum machine_mode mode = GET_MODE (dest);
2572           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2573
2574           if (BITS_BIG_ENDIAN)
2575             pos = GET_MODE_BITSIZE (mode) - len - pos;
2576
2577           if (src == mask)
2578             SUBST (SET_SRC (x),
2579                    gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2580           else
2581             SUBST (SET_SRC (x),
2582                    gen_binary (IOR, mode,
2583                                gen_binary (AND, mode, dest, 
2584                                            GEN_INT (~ (mask << pos)
2585                                                     & GET_MODE_MASK (mode))),
2586                                GEN_INT (src << pos)));
2587
2588           SUBST (SET_DEST (x), dest);
2589
2590           split = find_split_point (&SET_SRC (x), insn);
2591           if (split && split != &SET_SRC (x))
2592             return split;
2593         }
2594
2595       /* Otherwise, see if this is an operation that we can split into two.
2596          If so, try to split that.  */
2597       code = GET_CODE (SET_SRC (x));
2598
2599       switch (code)
2600         {
2601         case AND:
2602           /* If we are AND'ing with a large constant that is only a single
2603              bit and the result is only being used in a context where we
2604              need to know if it is zero or non-zero, replace it with a bit
2605              extraction.  This will avoid the large constant, which might
2606              have taken more than one insn to make.  If the constant were
2607              not a valid argument to the AND but took only one insn to make,
2608              this is no worse, but if it took more than one insn, it will
2609              be better.  */
2610
2611           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2612               && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2613               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2614               && GET_CODE (SET_DEST (x)) == REG
2615               && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2616               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2617               && XEXP (*split, 0) == SET_DEST (x)
2618               && XEXP (*split, 1) == const0_rtx)
2619             {
2620               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2621                                                 XEXP (SET_SRC (x), 0),
2622                                                 pos, NULL_RTX, 1, 1, 0, 0);
2623               if (extraction != 0)
2624                 {
2625                   SUBST (SET_SRC (x), extraction);
2626                   return find_split_point (loc, insn);
2627                 }
2628             }
2629           break;
2630
2631         case SIGN_EXTEND:
2632           inner = XEXP (SET_SRC (x), 0);
2633           pos = 0;
2634           len = GET_MODE_BITSIZE (GET_MODE (inner));
2635           unsignedp = 0;
2636           break;
2637
2638         case SIGN_EXTRACT:
2639         case ZERO_EXTRACT:
2640           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2641               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2642             {
2643               inner = XEXP (SET_SRC (x), 0);
2644               len = INTVAL (XEXP (SET_SRC (x), 1));
2645               pos = INTVAL (XEXP (SET_SRC (x), 2));
2646
2647               if (BITS_BIG_ENDIAN)
2648                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
2649               unsignedp = (code == ZERO_EXTRACT);
2650             }
2651           break;
2652         }
2653
2654       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2655         {
2656           enum machine_mode mode = GET_MODE (SET_SRC (x));
2657
2658           /* For unsigned, we have a choice of a shift followed by an
2659              AND or two shifts.  Use two shifts for field sizes where the
2660              constant might be too large.  We assume here that we can
2661              always at least get 8-bit constants in an AND insn, which is
2662              true for every current RISC.  */
2663
2664           if (unsignedp && len <= 8)
2665             {
2666               SUBST (SET_SRC (x),
2667                      gen_rtx_combine
2668                      (AND, mode,
2669                       gen_rtx_combine (LSHIFTRT, mode,
2670                                        gen_lowpart_for_combine (mode, inner),
2671                                        GEN_INT (pos)),
2672                       GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
2673
2674               split = find_split_point (&SET_SRC (x), insn);
2675               if (split && split != &SET_SRC (x))
2676                 return split;
2677             }
2678           else
2679             {
2680               SUBST (SET_SRC (x),
2681                      gen_rtx_combine
2682                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
2683                       gen_rtx_combine (ASHIFT, mode,
2684                                        gen_lowpart_for_combine (mode, inner),
2685                                        GEN_INT (GET_MODE_BITSIZE (mode)
2686                                                 - len - pos)),
2687                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
2688
2689               split = find_split_point (&SET_SRC (x), insn);
2690               if (split && split != &SET_SRC (x))
2691                 return split;
2692             }
2693         }
2694
2695       /* See if this is a simple operation with a constant as the second
2696          operand.  It might be that this constant is out of range and hence
2697          could be used as a split point.  */
2698       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2699            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2700            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2701           && CONSTANT_P (XEXP (SET_SRC (x), 1))
2702           && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2703               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2704                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2705                       == 'o'))))
2706         return &XEXP (SET_SRC (x), 1);
2707
2708       /* Finally, see if this is a simple operation with its first operand
2709          not in a register.  The operation might require this operand in a
2710          register, so return it as a split point.  We can always do this
2711          because if the first operand were another operation, we would have
2712          already found it as a split point.  */
2713       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2714            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2715            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2716            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2717           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2718         return &XEXP (SET_SRC (x), 0);
2719
2720       return 0;
2721
2722     case AND:
2723     case IOR:
2724       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2725          it is better to write this as (not (ior A B)) so we can split it.
2726          Similarly for IOR.  */
2727       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2728         {
2729           SUBST (*loc,
2730                  gen_rtx_combine (NOT, GET_MODE (x),
2731                                   gen_rtx_combine (code == IOR ? AND : IOR,
2732                                                    GET_MODE (x),
2733                                                    XEXP (XEXP (x, 0), 0),
2734                                                    XEXP (XEXP (x, 1), 0))));
2735           return find_split_point (loc, insn);
2736         }
2737
2738       /* Many RISC machines have a large set of logical insns.  If the
2739          second operand is a NOT, put it first so we will try to split the
2740          other operand first.  */
2741       if (GET_CODE (XEXP (x, 1)) == NOT)
2742         {
2743           rtx tem = XEXP (x, 0);
2744           SUBST (XEXP (x, 0), XEXP (x, 1));
2745           SUBST (XEXP (x, 1), tem);
2746         }
2747       break;
2748     }
2749
2750   /* Otherwise, select our actions depending on our rtx class.  */
2751   switch (GET_RTX_CLASS (code))
2752     {
2753     case 'b':                   /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
2754     case '3':
2755       split = find_split_point (&XEXP (x, 2), insn);
2756       if (split)
2757         return split;
2758       /* ... fall through ... */
2759     case '2':
2760     case 'c':
2761     case '<':
2762       split = find_split_point (&XEXP (x, 1), insn);
2763       if (split)
2764         return split;
2765       /* ... fall through ... */
2766     case '1':
2767       /* Some machines have (and (shift ...) ...) insns.  If X is not
2768          an AND, but XEXP (X, 0) is, use it as our split point.  */
2769       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
2770         return &XEXP (x, 0);
2771
2772       split = find_split_point (&XEXP (x, 0), insn);
2773       if (split)
2774         return split;
2775       return loc;
2776     }
2777
2778   /* Otherwise, we don't have a split point.  */
2779   return 0;
2780 }
2781 \f
2782 /* Throughout X, replace FROM with TO, and return the result.
2783    The result is TO if X is FROM;
2784    otherwise the result is X, but its contents may have been modified.
2785    If they were modified, a record was made in undobuf so that
2786    undo_all will (among other things) return X to its original state.
2787
2788    If the number of changes necessary is too much to record to undo,
2789    the excess changes are not made, so the result is invalid.
2790    The changes already made can still be undone.
2791    undobuf.num_undo is incremented for such changes, so by testing that
2792    the caller can tell whether the result is valid.
2793
2794    `n_occurrences' is incremented each time FROM is replaced.
2795    
2796    IN_DEST is non-zero if we are processing the SET_DEST of a SET.
2797
2798    UNIQUE_COPY is non-zero if each substitution must be unique.  We do this
2799    by copying if `n_occurrences' is non-zero.  */
2800
2801 static rtx
2802 subst (x, from, to, in_dest, unique_copy)
2803      register rtx x, from, to;
2804      int in_dest;
2805      int unique_copy;
2806 {
2807   register enum rtx_code code = GET_CODE (x);
2808   enum machine_mode op0_mode = VOIDmode;
2809   register char *fmt;
2810   register int len, i;
2811   rtx new;
2812
2813 /* Two expressions are equal if they are identical copies of a shared
2814    RTX or if they are both registers with the same register number
2815    and mode.  */
2816
2817 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
2818   ((X) == (Y)                                           \
2819    || (GET_CODE (X) == REG && GET_CODE (Y) == REG       \
2820        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
2821
2822   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
2823     {
2824       n_occurrences++;
2825       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
2826     }
2827
2828   /* If X and FROM are the same register but different modes, they will
2829      not have been seen as equal above.  However, flow.c will make a 
2830      LOG_LINKS entry for that case.  If we do nothing, we will try to
2831      rerecognize our original insn and, when it succeeds, we will
2832      delete the feeding insn, which is incorrect.
2833
2834      So force this insn not to match in this (rare) case.  */
2835   if (! in_dest && code == REG && GET_CODE (from) == REG
2836       && REGNO (x) == REGNO (from))
2837     return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
2838
2839   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
2840      of which may contain things that can be combined.  */
2841   if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
2842     return x;
2843
2844   /* It is possible to have a subexpression appear twice in the insn.
2845      Suppose that FROM is a register that appears within TO.
2846      Then, after that subexpression has been scanned once by `subst',
2847      the second time it is scanned, TO may be found.  If we were
2848      to scan TO here, we would find FROM within it and create a
2849      self-referent rtl structure which is completely wrong.  */
2850   if (COMBINE_RTX_EQUAL_P (x, to))
2851     return to;
2852
2853   len = GET_RTX_LENGTH (code);
2854   fmt = GET_RTX_FORMAT (code);
2855
2856   /* We don't need to process a SET_DEST that is a register, CC0, or PC, so
2857      set up to skip this common case.  All other cases where we want to
2858      suppress replacing something inside a SET_SRC are handled via the
2859      IN_DEST operand.  */
2860   if (code == SET
2861       && (GET_CODE (SET_DEST (x)) == REG
2862         || GET_CODE (SET_DEST (x)) == CC0
2863         || GET_CODE (SET_DEST (x)) == PC))
2864     fmt = "ie";
2865
2866   /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a constant. */
2867   if (fmt[0] == 'e')
2868     op0_mode = GET_MODE (XEXP (x, 0));
2869
2870   for (i = 0; i < len; i++)
2871     {
2872       if (fmt[i] == 'E')
2873         {
2874           register int j;
2875           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2876             {
2877               if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
2878                 {
2879                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
2880                   n_occurrences++;
2881                 }
2882               else
2883                 {
2884                   new = subst (XVECEXP (x, i, j), from, to, 0, unique_copy);
2885
2886                   /* If this substitution failed, this whole thing fails.  */
2887                   if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
2888                     return new;
2889                 }
2890
2891               SUBST (XVECEXP (x, i, j), new);
2892             }
2893         }
2894       else if (fmt[i] == 'e')
2895         {
2896           if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
2897             {
2898               /* In general, don't install a subreg involving two modes not
2899                  tieable.  It can worsen register allocation, and can even
2900                  make invalid reload insns, since the reg inside may need to
2901                  be copied from in the outside mode, and that may be invalid
2902                  if it is an fp reg copied in integer mode.
2903
2904                  We allow two exceptions to this: It is valid if it is inside
2905                  another SUBREG and the mode of that SUBREG and the mode of
2906                  the inside of TO is tieable and it is valid if X is a SET
2907                  that copies FROM to CC0.  */
2908               if (GET_CODE (to) == SUBREG
2909                   && ! MODES_TIEABLE_P (GET_MODE (to),
2910                                         GET_MODE (SUBREG_REG (to)))
2911                   && ! (code == SUBREG
2912                         && MODES_TIEABLE_P (GET_MODE (x),
2913                                             GET_MODE (SUBREG_REG (to))))
2914 #ifdef HAVE_cc0
2915                   && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
2916 #endif
2917                   )
2918                 return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
2919
2920               new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
2921               n_occurrences++;
2922             }
2923           else
2924             /* If we are in a SET_DEST, suppress most cases unless we
2925                have gone inside a MEM, in which case we want to
2926                simplify the address.  We assume here that things that
2927                are actually part of the destination have their inner
2928                parts in the first expression.  This is true for SUBREG, 
2929                STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
2930                things aside from REG and MEM that should appear in a
2931                SET_DEST.  */
2932             new = subst (XEXP (x, i), from, to,
2933                          (((in_dest
2934                             && (code == SUBREG || code == STRICT_LOW_PART
2935                                 || code == ZERO_EXTRACT))
2936                            || code == SET)
2937                           && i == 0), unique_copy);
2938
2939           /* If we found that we will have to reject this combination,
2940              indicate that by returning the CLOBBER ourselves, rather than
2941              an expression containing it.  This will speed things up as
2942              well as prevent accidents where two CLOBBERs are considered
2943              to be equal, thus producing an incorrect simplification.  */
2944
2945           if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
2946             return new;
2947
2948           SUBST (XEXP (x, i), new);
2949         }
2950     }
2951
2952   /* Try to simplify X.  If the simplification changed the code, it is likely
2953      that further simplification will help, so loop, but limit the number
2954      of repetitions that will be performed.  */
2955
2956   for (i = 0; i < 4; i++)
2957     {
2958       /* If X is sufficiently simple, don't bother trying to do anything
2959          with it.  */
2960       if (code != CONST_INT && code != REG && code != CLOBBER)
2961         x = simplify_rtx (x, op0_mode, i == 3, in_dest);
2962
2963       if (GET_CODE (x) == code)
2964         break;
2965
2966       code = GET_CODE (x);
2967
2968       /* We no longer know the original mode of operand 0 since we
2969          have changed the form of X)  */
2970       op0_mode = VOIDmode;
2971     }
2972
2973   return x;
2974 }
2975 \f
2976 /* Simplify X, a piece of RTL.  We just operate on the expression at the
2977    outer level; call `subst' to simplify recursively.  Return the new
2978    expression.
2979
2980    OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
2981    will be the iteration even if an expression with a code different from
2982    X is returned; IN_DEST is nonzero if we are inside a SET_DEST.  */
2983
2984 static rtx
2985 simplify_rtx (x, op0_mode, last, in_dest)
2986      rtx x;
2987      enum machine_mode op0_mode;
2988      int last;
2989      int in_dest;
2990 {
2991   enum rtx_code code = GET_CODE (x);
2992   enum machine_mode mode = GET_MODE (x);
2993   rtx temp;
2994   int i;
2995
2996   /* If this is a commutative operation, put a constant last and a complex
2997      expression first.  We don't need to do this for comparisons here.  */
2998   if (GET_RTX_CLASS (code) == 'c'
2999       && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3000           || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3001               && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3002           || (GET_CODE (XEXP (x, 0)) == SUBREG
3003               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3004               && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3005     {
3006       temp = XEXP (x, 0);
3007       SUBST (XEXP (x, 0), XEXP (x, 1));
3008       SUBST (XEXP (x, 1), temp);
3009     }
3010
3011   /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3012      sign extension of a PLUS with a constant, reverse the order of the sign
3013      extension and the addition. Note that this not the same as the original
3014      code, but overflow is undefined for signed values.  Also note that the
3015      PLUS will have been partially moved "inside" the sign-extension, so that
3016      the first operand of X will really look like:
3017          (ashiftrt (plus (ashift A C4) C5) C4).
3018      We convert this to
3019          (plus (ashiftrt (ashift A C4) C2) C4)
3020      and replace the first operand of X with that expression.  Later parts
3021      of this function may simplify the expression further.
3022
3023      For example, if we start with (mult (sign_extend (plus A C1)) C2),
3024      we swap the SIGN_EXTEND and PLUS.  Later code will apply the
3025      distributive law to produce (plus (mult (sign_extend X) C1) C3).
3026
3027      We do this to simplify address expressions.  */
3028
3029   if ((code == PLUS || code == MINUS || code == MULT)
3030       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3031       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3032       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3033       && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3034       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3035       && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3036       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3037       && (temp = simplify_binary_operation (ASHIFTRT, mode,
3038                                             XEXP (XEXP (XEXP (x, 0), 0), 1),
3039                                             XEXP (XEXP (x, 0), 1))) != 0)
3040     {
3041       rtx new
3042         = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3043                                 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3044                                 INTVAL (XEXP (XEXP (x, 0), 1)));
3045
3046       new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3047                                   INTVAL (XEXP (XEXP (x, 0), 1)));
3048
3049       SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3050     }
3051
3052   /* If this is a simple operation applied to an IF_THEN_ELSE, try 
3053      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
3054      things.  Check for cases where both arms are testing the same
3055      condition.
3056
3057      Don't do anything if all operands are very simple.  */
3058
3059   if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3060         || GET_RTX_CLASS (code) == '<')
3061        && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3062             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3063                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3064                       == 'o')))
3065            || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3066                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3067                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3068                          == 'o')))))
3069       || (GET_RTX_CLASS (code) == '1'
3070           && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3071                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3072                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3073                          == 'o'))))))
3074     {
3075       rtx cond, true, false;
3076
3077       cond = if_then_else_cond (x, &true, &false);
3078       if (cond != 0)
3079         {
3080           rtx cop1 = const0_rtx;
3081           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3082
3083           if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3084             return x;
3085
3086           /* Simplify the alternative arms; this may collapse the true and 
3087              false arms to store-flag values.  */
3088           true = subst (true, pc_rtx, pc_rtx, 0, 0);
3089           false = subst (false, pc_rtx, pc_rtx, 0, 0);
3090
3091           /* Restarting if we generate a store-flag expression will cause
3092              us to loop.  Just drop through in this case.  */
3093
3094           /* If the result values are STORE_FLAG_VALUE and zero, we can
3095              just make the comparison operation.  */
3096           if (true == const_true_rtx && false == const0_rtx)
3097             x = gen_binary (cond_code, mode, cond, cop1);
3098           else if (true == const0_rtx && false == const_true_rtx)
3099             x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3100
3101           /* Likewise, we can make the negate of a comparison operation
3102              if the result values are - STORE_FLAG_VALUE and zero.  */
3103           else if (GET_CODE (true) == CONST_INT
3104                    && INTVAL (true) == - STORE_FLAG_VALUE
3105                    && false == const0_rtx)
3106             x = gen_unary (NEG, mode, mode,
3107                            gen_binary (cond_code, mode, cond, cop1));
3108           else if (GET_CODE (false) == CONST_INT
3109                    && INTVAL (false) == - STORE_FLAG_VALUE
3110                    && true == const0_rtx)
3111             x = gen_unary (NEG, mode, mode,
3112                            gen_binary (reverse_condition (cond_code), 
3113                                        mode, cond, cop1));
3114           else
3115             return gen_rtx (IF_THEN_ELSE, mode,
3116                             gen_binary (cond_code, VOIDmode, cond, cop1),
3117                             true, false);
3118
3119           code = GET_CODE (x);
3120           op0_mode = VOIDmode;
3121         }
3122     }
3123
3124   /* Try to fold this expression in case we have constants that weren't
3125      present before.  */
3126   temp = 0;
3127   switch (GET_RTX_CLASS (code))
3128     {
3129     case '1':
3130       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3131       break;
3132     case '<':
3133       temp = simplify_relational_operation (code, op0_mode,
3134                                             XEXP (x, 0), XEXP (x, 1));
3135 #ifdef FLOAT_STORE_FLAG_VALUE
3136       if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3137         temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3138                 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3139 #endif
3140       break;
3141     case 'c':
3142     case '2':
3143       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3144       break;
3145     case 'b':
3146     case '3':
3147       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3148                                          XEXP (x, 1), XEXP (x, 2));
3149       break;
3150     }
3151
3152   if (temp)
3153     x = temp, code = GET_CODE (temp);
3154
3155   /* First see if we can apply the inverse distributive law.  */
3156   if (code == PLUS || code == MINUS
3157       || code == AND || code == IOR || code == XOR)
3158     {
3159       x = apply_distributive_law (x);
3160       code = GET_CODE (x);
3161     }
3162
3163   /* If CODE is an associative operation not otherwise handled, see if we
3164      can associate some operands.  This can win if they are constants or
3165      if they are logically related (i.e. (a & b) & a.  */
3166   if ((code == PLUS || code == MINUS
3167        || code == MULT || code == AND || code == IOR || code == XOR
3168        || code == DIV || code == UDIV
3169        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3170       && INTEGRAL_MODE_P (mode))
3171     {
3172       if (GET_CODE (XEXP (x, 0)) == code)
3173         {
3174           rtx other = XEXP (XEXP (x, 0), 0);
3175           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3176           rtx inner_op1 = XEXP (x, 1);
3177           rtx inner;
3178           
3179           /* Make sure we pass the constant operand if any as the second
3180              one if this is a commutative operation.  */
3181           if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3182             {
3183               rtx tem = inner_op0;
3184               inner_op0 = inner_op1;
3185               inner_op1 = tem;
3186             }
3187           inner = simplify_binary_operation (code == MINUS ? PLUS
3188                                              : code == DIV ? MULT
3189                                              : code == UDIV ? MULT
3190                                              : code,
3191                                              mode, inner_op0, inner_op1);
3192
3193           /* For commutative operations, try the other pair if that one
3194              didn't simplify.  */
3195           if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3196             {
3197               other = XEXP (XEXP (x, 0), 1);
3198               inner = simplify_binary_operation (code, mode,
3199                                                  XEXP (XEXP (x, 0), 0),
3200                                                  XEXP (x, 1));
3201             }
3202
3203           if (inner)
3204             return gen_binary (code, mode, other, inner);
3205         }
3206     }
3207
3208   /* A little bit of algebraic simplification here.  */
3209   switch (code)
3210     {
3211     case MEM:
3212       /* Ensure that our address has any ASHIFTs converted to MULT in case
3213          address-recognizing predicates are called later.  */
3214       temp = make_compound_operation (XEXP (x, 0), MEM);
3215       SUBST (XEXP (x, 0), temp);
3216       break;
3217
3218     case SUBREG:
3219       /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3220          is paradoxical.  If we can't do that safely, then it becomes
3221          something nonsensical so that this combination won't take place.  */
3222
3223       if (GET_CODE (SUBREG_REG (x)) == MEM
3224           && (GET_MODE_SIZE (mode)
3225               <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3226         {
3227           rtx inner = SUBREG_REG (x);
3228           int endian_offset = 0;
3229           /* Don't change the mode of the MEM
3230              if that would change the meaning of the address.  */
3231           if (MEM_VOLATILE_P (SUBREG_REG (x))
3232               || mode_dependent_address_p (XEXP (inner, 0)))
3233             return gen_rtx (CLOBBER, mode, const0_rtx);
3234
3235           if (BYTES_BIG_ENDIAN)
3236             {
3237               if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3238                 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3239               if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3240                 endian_offset -= (UNITS_PER_WORD
3241                                   - GET_MODE_SIZE (GET_MODE (inner)));
3242             }
3243           /* Note if the plus_constant doesn't make a valid address
3244              then this combination won't be accepted.  */
3245           x = gen_rtx (MEM, mode,
3246                        plus_constant (XEXP (inner, 0),
3247                                       (SUBREG_WORD (x) * UNITS_PER_WORD
3248                                        + endian_offset)));
3249           MEM_VOLATILE_P (x) = MEM_VOLATILE_P (inner);
3250           RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3251           MEM_IN_STRUCT_P (x) = MEM_IN_STRUCT_P (inner);
3252           return x;
3253         }
3254
3255       /* If we are in a SET_DEST, these other cases can't apply.  */
3256       if (in_dest)
3257         return x;
3258
3259       /* Changing mode twice with SUBREG => just change it once,
3260          or not at all if changing back to starting mode.  */
3261       if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3262         {
3263           if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3264               && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3265             return SUBREG_REG (SUBREG_REG (x));
3266
3267           SUBST_INT (SUBREG_WORD (x),
3268                      SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3269           SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3270         }
3271
3272       /* SUBREG of a hard register => just change the register number
3273          and/or mode.  If the hard register is not valid in that mode,
3274          suppress this combination.  If the hard register is the stack,
3275          frame, or argument pointer, leave this as a SUBREG.  */
3276
3277       if (GET_CODE (SUBREG_REG (x)) == REG
3278           && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3279           && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3280 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3281           && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3282 #endif
3283 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3284           && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3285 #endif
3286           && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3287         {
3288           if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3289                                   mode))
3290             return gen_rtx (REG, mode,
3291                             REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3292           else
3293             return gen_rtx (CLOBBER, mode, const0_rtx);
3294         }
3295
3296       /* For a constant, try to pick up the part we want.  Handle a full
3297          word and low-order part.  Only do this if we are narrowing
3298          the constant; if it is being widened, we have no idea what
3299          the extra bits will have been set to.  */
3300
3301       if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3302           && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3303           && GET_MODE_SIZE (op0_mode) < UNITS_PER_WORD
3304           && GET_MODE_CLASS (mode) == MODE_INT)
3305         {
3306           temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3307                                   0, op0_mode);
3308           if (temp)
3309             return temp;
3310         }
3311         
3312       /* If we want a subreg of a constant, at offset 0,
3313          take the low bits.  On a little-endian machine, that's
3314          always valid.  On a big-endian machine, it's valid
3315          only if the constant's mode fits in one word.  */
3316       if (CONSTANT_P (SUBREG_REG (x)) && subreg_lowpart_p (x)
3317           && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
3318           && (! WORDS_BIG_ENDIAN
3319               || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
3320         return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3321
3322       /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3323          since we are saying that the high bits don't matter.  */
3324       if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3325           && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3326         return SUBREG_REG (x);
3327
3328       /* Note that we cannot do any narrowing for non-constants since
3329          we might have been counting on using the fact that some bits were
3330          zero.  We now do this in the SET.  */
3331
3332       break;
3333
3334     case NOT:
3335       /* (not (plus X -1)) can become (neg X).  */
3336       if (GET_CODE (XEXP (x, 0)) == PLUS
3337           && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3338         return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3339
3340       /* Similarly, (not (neg X)) is (plus X -1).  */
3341       if (GET_CODE (XEXP (x, 0)) == NEG)
3342         return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3343                                 constm1_rtx);
3344
3345       /* (not (xor X C)) for C constant is (xor X D) with D = ~ C.  */
3346       if (GET_CODE (XEXP (x, 0)) == XOR
3347           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3348           && (temp = simplify_unary_operation (NOT, mode,
3349                                                XEXP (XEXP (x, 0), 1),
3350                                                mode)) != 0)
3351         return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3352               
3353       /* (not (ashift 1 X)) is (rotate ~1 X).  We used to do this for operands
3354          other than 1, but that is not valid.  We could do a similar
3355          simplification for (not (lshiftrt C X)) where C is just the sign bit,
3356          but this doesn't seem common enough to bother with.  */
3357       if (GET_CODE (XEXP (x, 0)) == ASHIFT
3358           && XEXP (XEXP (x, 0), 0) == const1_rtx)
3359         return gen_rtx (ROTATE, mode, gen_unary (NOT, mode, mode, const1_rtx),
3360                         XEXP (XEXP (x, 0), 1));
3361                                             
3362       if (GET_CODE (XEXP (x, 0)) == SUBREG
3363           && subreg_lowpart_p (XEXP (x, 0))
3364           && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3365               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3366           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3367           && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3368         {
3369           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3370
3371           x = gen_rtx (ROTATE, inner_mode,
3372                        gen_unary (NOT, inner_mode, inner_mode, const1_rtx),
3373                        XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3374           return gen_lowpart_for_combine (mode, x);
3375         }
3376                                             
3377 #if STORE_FLAG_VALUE == -1
3378       /* (not (comparison foo bar)) can be done by reversing the comparison
3379          code if valid.  */
3380       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3381           && reversible_comparison_p (XEXP (x, 0)))
3382         return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3383                                 mode, XEXP (XEXP (x, 0), 0),
3384                                 XEXP (XEXP (x, 0), 1));
3385
3386       /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3387          is (lt foo (const_int 0)), so we can perform the above
3388          simplification.  */
3389
3390       if (XEXP (x, 1) == const1_rtx
3391           && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3392           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3393           && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3394         return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3395 #endif
3396
3397       /* Apply De Morgan's laws to reduce number of patterns for machines
3398          with negating logical insns (and-not, nand, etc.).  If result has
3399          only one NOT, put it first, since that is how the patterns are
3400          coded.  */
3401
3402       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3403         {
3404          rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3405
3406          if (GET_CODE (in1) == NOT)
3407            in1 = XEXP (in1, 0);
3408          else
3409            in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3410
3411          if (GET_CODE (in2) == NOT)
3412            in2 = XEXP (in2, 0);
3413          else if (GET_CODE (in2) == CONST_INT
3414                   && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3415            in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
3416          else
3417            in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3418
3419          if (GET_CODE (in2) == NOT)
3420            {
3421              rtx tem = in2;
3422              in2 = in1; in1 = tem;
3423            }
3424
3425          return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3426                                  mode, in1, in2);
3427        } 
3428       break;
3429
3430     case NEG:
3431       /* (neg (plus X 1)) can become (not X).  */
3432       if (GET_CODE (XEXP (x, 0)) == PLUS
3433           && XEXP (XEXP (x, 0), 1) == const1_rtx)
3434         return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3435
3436       /* Similarly, (neg (not X)) is (plus X 1).  */
3437       if (GET_CODE (XEXP (x, 0)) == NOT)
3438         return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3439
3440       /* (neg (minus X Y)) can become (minus Y X).  */
3441       if (GET_CODE (XEXP (x, 0)) == MINUS
3442           && (! FLOAT_MODE_P (mode)
3443               /* x-y != -(y-x) with IEEE floating point. */
3444               || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3445               || flag_fast_math))
3446         return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3447                            XEXP (XEXP (x, 0), 0));
3448
3449       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
3450       if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3451           && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3452         return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3453
3454       /* NEG commutes with ASHIFT since it is multiplication.  Only do this
3455          if we can then eliminate the NEG (e.g.,
3456          if the operand is a constant).  */
3457
3458       if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3459         {
3460           temp = simplify_unary_operation (NEG, mode,
3461                                            XEXP (XEXP (x, 0), 0), mode);
3462           if (temp)
3463             {
3464               SUBST (XEXP (XEXP (x, 0), 0), temp);
3465               return XEXP (x, 0);
3466             }
3467         }
3468
3469       temp = expand_compound_operation (XEXP (x, 0));
3470
3471       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3472          replaced by (lshiftrt X C).  This will convert
3473          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
3474
3475       if (GET_CODE (temp) == ASHIFTRT
3476           && GET_CODE (XEXP (temp, 1)) == CONST_INT
3477           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3478         return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3479                                      INTVAL (XEXP (temp, 1)));
3480
3481       /* If X has only a single bit that might be nonzero, say, bit I, convert
3482          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3483          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
3484          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
3485          or a SUBREG of one since we'd be making the expression more
3486          complex if it was just a register.  */
3487
3488       if (GET_CODE (temp) != REG
3489           && ! (GET_CODE (temp) == SUBREG
3490                 && GET_CODE (SUBREG_REG (temp)) == REG)
3491           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3492         {
3493           rtx temp1 = simplify_shift_const
3494             (NULL_RTX, ASHIFTRT, mode,
3495              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3496                                    GET_MODE_BITSIZE (mode) - 1 - i),
3497              GET_MODE_BITSIZE (mode) - 1 - i);
3498
3499           /* If all we did was surround TEMP with the two shifts, we
3500              haven't improved anything, so don't use it.  Otherwise,
3501              we are better off with TEMP1.  */
3502           if (GET_CODE (temp1) != ASHIFTRT
3503               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3504               || XEXP (XEXP (temp1, 0), 0) != temp)
3505             return temp1;
3506         }
3507       break;
3508
3509     case TRUNCATE:
3510       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3511         SUBST (XEXP (x, 0),
3512                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3513                               GET_MODE_MASK (mode), NULL_RTX, 0));
3514       break;
3515
3516     case FLOAT_TRUNCATE:
3517       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
3518       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3519           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3520         return XEXP (XEXP (x, 0), 0);
3521
3522       /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3523          (OP:SF foo:SF) if OP is NEG or ABS.  */
3524       if ((GET_CODE (XEXP (x, 0)) == ABS
3525            || GET_CODE (XEXP (x, 0)) == NEG)
3526           && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3527           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3528         return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3529                           XEXP (XEXP (XEXP (x, 0), 0), 0));
3530
3531       /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3532          is (float_truncate:SF x).  */
3533       if (GET_CODE (XEXP (x, 0)) == SUBREG
3534           && subreg_lowpart_p (XEXP (x, 0))
3535           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3536         return SUBREG_REG (XEXP (x, 0));
3537       break;  
3538
3539 #ifdef HAVE_cc0
3540     case COMPARE:
3541       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3542          using cc0, in which case we want to leave it as a COMPARE
3543          so we can distinguish it from a register-register-copy.  */
3544       if (XEXP (x, 1) == const0_rtx)
3545         return XEXP (x, 0);
3546
3547       /* In IEEE floating point, x-0 is not the same as x.  */
3548       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3549            || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3550            || flag_fast_math)
3551           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3552         return XEXP (x, 0);
3553       break;
3554 #endif
3555
3556     case CONST:
3557       /* (const (const X)) can become (const X).  Do it this way rather than
3558          returning the inner CONST since CONST can be shared with a
3559          REG_EQUAL note.  */
3560       if (GET_CODE (XEXP (x, 0)) == CONST)
3561         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3562       break;
3563
3564 #ifdef HAVE_lo_sum
3565     case LO_SUM:
3566       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
3567          can add in an offset.  find_split_point will split this address up
3568          again if it doesn't match.  */
3569       if (GET_CODE (XEXP (x, 0)) == HIGH
3570           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3571         return XEXP (x, 1);
3572       break;
3573 #endif
3574
3575     case PLUS:
3576       /* If we have (plus (plus (A const) B)), associate it so that CONST is
3577          outermost.  That's because that's the way indexed addresses are
3578          supposed to appear.  This code used to check many more cases, but
3579          they are now checked elsewhere.  */
3580       if (GET_CODE (XEXP (x, 0)) == PLUS
3581           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3582         return gen_binary (PLUS, mode,
3583                            gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3584                                        XEXP (x, 1)),
3585                            XEXP (XEXP (x, 0), 1));
3586
3587       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3588          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3589          bit-field and can be replaced by either a sign_extend or a
3590          sign_extract.  The `and' may be a zero_extend.  */
3591       if (GET_CODE (XEXP (x, 0)) == XOR
3592           && GET_CODE (XEXP (x, 1)) == CONST_INT
3593           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3594           && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
3595           && (i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
3596           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3597           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3598                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3599                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
3600                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
3601               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3602                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3603                       == i + 1))))
3604         return simplify_shift_const
3605           (NULL_RTX, ASHIFTRT, mode,
3606            simplify_shift_const (NULL_RTX, ASHIFT, mode,
3607                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
3608                                  GET_MODE_BITSIZE (mode) - (i + 1)),
3609            GET_MODE_BITSIZE (mode) - (i + 1));
3610
3611       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3612          C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3613          is 1.  This produces better code than the alternative immediately
3614          below.  */
3615       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3616           && reversible_comparison_p (XEXP (x, 0))
3617           && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3618               || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
3619         return
3620           gen_unary (NEG, mode, mode,
3621                      gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3622                                  mode, XEXP (XEXP (x, 0), 0),
3623                                  XEXP (XEXP (x, 0), 1)));
3624
3625       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
3626          can become (ashiftrt (ashift (xor x 1) C) C) where C is
3627          the bitsize of the mode - 1.  This allows simplification of
3628          "a = (b & 8) == 0;"  */
3629       if (XEXP (x, 1) == constm1_rtx
3630           && GET_CODE (XEXP (x, 0)) != REG
3631           && ! (GET_CODE (XEXP (x,0)) == SUBREG
3632                 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
3633           && nonzero_bits (XEXP (x, 0), mode) == 1)
3634         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
3635            simplify_shift_const (NULL_RTX, ASHIFT, mode,
3636                                  gen_rtx_combine (XOR, mode,
3637                                                   XEXP (x, 0), const1_rtx),
3638                                  GET_MODE_BITSIZE (mode) - 1),
3639            GET_MODE_BITSIZE (mode) - 1);
3640
3641       /* If we are adding two things that have no bits in common, convert
3642          the addition into an IOR.  This will often be further simplified,
3643          for example in cases like ((a & 1) + (a & 2)), which can
3644          become a & 3.  */
3645
3646       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3647           && (nonzero_bits (XEXP (x, 0), mode)
3648               & nonzero_bits (XEXP (x, 1), mode)) == 0)
3649         return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
3650       break;
3651
3652     case MINUS:
3653 #if STORE_FLAG_VALUE == 1
3654       /* (minus 1 (comparison foo bar)) can be done by reversing the comparison
3655          code if valid.  */
3656       if (XEXP (x, 0) == const1_rtx
3657           && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
3658           && reversible_comparison_p (XEXP (x, 1)))
3659         return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
3660                            mode, XEXP (XEXP (x, 1), 0),
3661                                 XEXP (XEXP (x, 1), 1));
3662 #endif
3663
3664       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
3665          (and <foo> (const_int pow2-1))  */
3666       if (GET_CODE (XEXP (x, 1)) == AND
3667           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3668           && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
3669           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3670         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
3671                                        - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
3672
3673       /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
3674          integers.  */
3675       if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
3676         return gen_binary (MINUS, mode,
3677                            gen_binary (MINUS, mode, XEXP (x, 0),
3678                                        XEXP (XEXP (x, 1), 0)),
3679                            XEXP (XEXP (x, 1), 1));
3680       break;
3681
3682     case MULT:
3683       /* If we have (mult (plus A B) C), apply the distributive law and then
3684          the inverse distributive law to see if things simplify.  This
3685          occurs mostly in addresses, often when unrolling loops.  */
3686
3687       if (GET_CODE (XEXP (x, 0)) == PLUS)
3688         {
3689           x = apply_distributive_law
3690             (gen_binary (PLUS, mode,
3691                          gen_binary (MULT, mode,
3692                                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
3693                          gen_binary (MULT, mode,
3694                                      XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
3695
3696           if (GET_CODE (x) != MULT)
3697             return x;
3698         }
3699       break;
3700
3701     case UDIV:
3702       /* If this is a divide by a power of two, treat it as a shift if
3703          its first operand is a shift.  */
3704       if (GET_CODE (XEXP (x, 1)) == CONST_INT
3705           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
3706           && (GET_CODE (XEXP (x, 0)) == ASHIFT
3707               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
3708               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
3709               || GET_CODE (XEXP (x, 0)) == ROTATE
3710               || GET_CODE (XEXP (x, 0)) == ROTATERT))
3711         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
3712       break;
3713
3714     case EQ:  case NE:
3715     case GT:  case GTU:  case GE:  case GEU:
3716     case LT:  case LTU:  case LE:  case LEU:
3717       /* If the first operand is a condition code, we can't do anything
3718          with it.  */
3719       if (GET_CODE (XEXP (x, 0)) == COMPARE
3720           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
3721 #ifdef HAVE_cc0
3722               && XEXP (x, 0) != cc0_rtx
3723 #endif
3724                ))
3725         {
3726           rtx op0 = XEXP (x, 0);
3727           rtx op1 = XEXP (x, 1);
3728           enum rtx_code new_code;
3729
3730           if (GET_CODE (op0) == COMPARE)
3731             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
3732
3733           /* Simplify our comparison, if possible.  */
3734           new_code = simplify_comparison (code, &op0, &op1);
3735
3736 #if STORE_FLAG_VALUE == 1
3737           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
3738              if only the low-order bit is possibly nonzero in X (such as when
3739              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
3740              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
3741              known to be either 0 or -1, NE becomes a NEG and EQ becomes
3742              (plus X 1).
3743
3744              Remove any ZERO_EXTRACT we made when thinking this was a
3745              comparison.  It may now be simpler to use, e.g., an AND.  If a
3746              ZERO_EXTRACT is indeed appropriate, it will be placed back by
3747              the call to make_compound_operation in the SET case.  */
3748
3749           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3750               && op1 == const0_rtx
3751               && nonzero_bits (op0, mode) == 1)
3752             return gen_lowpart_for_combine (mode,
3753                                             expand_compound_operation (op0));
3754
3755           else if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3756                    && op1 == const0_rtx
3757                    && (num_sign_bit_copies (op0, mode)
3758                        == GET_MODE_BITSIZE (mode)))
3759             {
3760               op0 = expand_compound_operation (op0);
3761               return gen_unary (NEG, mode, mode,
3762                                 gen_lowpart_for_combine (mode, op0));
3763             }
3764
3765           else if (new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3766                    && op1 == const0_rtx
3767                    && nonzero_bits (op0, mode) == 1)
3768             {
3769               op0 = expand_compound_operation (op0);
3770               return gen_binary (XOR, mode,
3771                                  gen_lowpart_for_combine (mode, op0),
3772                                  const1_rtx);
3773             }
3774
3775           else if (new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3776                    && op1 == const0_rtx
3777                    && (num_sign_bit_copies (op0, mode)
3778                        == GET_MODE_BITSIZE (mode)))
3779             {
3780               op0 = expand_compound_operation (op0);
3781               return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
3782             }
3783 #endif
3784
3785 #if STORE_FLAG_VALUE == -1
3786           /* If STORE_FLAG_VALUE is -1, we have cases similar to
3787              those above.  */
3788           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3789               && op1 == const0_rtx
3790               && (num_sign_bit_copies (op0, mode)
3791                   == GET_MODE_BITSIZE (mode)))
3792             return gen_lowpart_for_combine (mode,
3793                                             expand_compound_operation (op0));
3794
3795           else if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3796                    && op1 == const0_rtx
3797                    && nonzero_bits (op0, mode) == 1)
3798             {
3799               op0 = expand_compound_operation (op0);
3800               return gen_unary (NEG, mode, mode,
3801                                 gen_lowpart_for_combine (mode, op0));
3802             }
3803
3804           else if (new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3805                    && op1 == const0_rtx
3806                    && (num_sign_bit_copies (op0, mode)
3807                        == GET_MODE_BITSIZE (mode)))
3808             {
3809               op0 = expand_compound_operation (op0);
3810               return gen_unary (NOT, mode, mode,
3811                                 gen_lowpart_for_combine (mode, op0));
3812             }
3813
3814           /* If X is 0/1, (eq X 0) is X-1.  */
3815           else if (new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3816                    && op1 == const0_rtx
3817                    && nonzero_bits (op0, mode) == 1)
3818             {
3819               op0 = expand_compound_operation (op0);
3820               return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
3821             }
3822 #endif
3823
3824           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
3825              one bit that might be nonzero, we can convert (ne x 0) to
3826              (ashift x c) where C puts the bit in the sign bit.  Remove any
3827              AND with STORE_FLAG_VALUE when we are done, since we are only
3828              going to test the sign bit.  */
3829           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3830               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3831               && (STORE_FLAG_VALUE
3832                   == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
3833               && op1 == const0_rtx
3834               && mode == GET_MODE (op0)
3835               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
3836             {
3837               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3838                                         expand_compound_operation (op0),
3839                                         GET_MODE_BITSIZE (mode) - 1 - i);
3840               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
3841                 return XEXP (x, 0);
3842               else
3843                 return x;
3844             }
3845
3846           /* If the code changed, return a whole new comparison.  */
3847           if (new_code != code)
3848             return gen_rtx_combine (new_code, mode, op0, op1);
3849
3850           /* Otherwise, keep this operation, but maybe change its operands.  
3851              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
3852           SUBST (XEXP (x, 0), op0);
3853           SUBST (XEXP (x, 1), op1);
3854         }
3855       break;
3856           
3857     case IF_THEN_ELSE:
3858       return simplify_if_then_else (x);
3859
3860     case ZERO_EXTRACT:
3861     case SIGN_EXTRACT:
3862     case ZERO_EXTEND:
3863     case SIGN_EXTEND:
3864       /* If we are processing SET_DEST, we are done. */
3865       if (in_dest)
3866         return x;
3867
3868       return expand_compound_operation (x);
3869
3870     case SET:
3871       return simplify_set (x);
3872
3873     case AND:
3874     case IOR:
3875     case XOR:
3876       return simplify_logical (x, last);
3877
3878     case ABS:
3879       /* (abs (neg <foo>)) -> (abs <foo>) */
3880       if (GET_CODE (XEXP (x, 0)) == NEG)
3881         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3882
3883       /* If operand is something known to be positive, ignore the ABS.  */
3884       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
3885           || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
3886                <= HOST_BITS_PER_WIDE_INT)
3887               && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3888                    & ((HOST_WIDE_INT) 1
3889                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
3890                   == 0)))
3891         return XEXP (x, 0);
3892
3893
3894       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
3895       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
3896         return gen_rtx_combine (NEG, mode, XEXP (x, 0));
3897
3898       break;
3899
3900     case FFS:
3901       /* (ffs (*_extend <X>)) = (ffs <X>) */
3902       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3903           || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3904         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3905       break;
3906
3907     case FLOAT:
3908       /* (float (sign_extend <X>)) = (float <X>).  */
3909       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
3910         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3911       break;
3912
3913     case ASHIFT:
3914     case LSHIFTRT:
3915     case ASHIFTRT:
3916     case ROTATE:
3917     case ROTATERT:
3918       /* If this is a shift by a constant amount, simplify it.  */
3919       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3920         return simplify_shift_const (x, code, mode, XEXP (x, 0), 
3921                                      INTVAL (XEXP (x, 1)));
3922
3923 #ifdef SHIFT_COUNT_TRUNCATED
3924       else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
3925         SUBST (XEXP (x, 1),
3926                force_to_mode (XEXP (x, 1), GET_MODE (x),
3927                               ((HOST_WIDE_INT) 1 
3928                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
3929                               - 1,
3930                               NULL_RTX, 0));
3931 #endif
3932
3933       break;
3934     }
3935
3936   return x;
3937 }
3938 \f
3939 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
3940
3941 static rtx
3942 simplify_if_then_else (x)
3943      rtx x;
3944 {
3945   enum machine_mode mode = GET_MODE (x);
3946   rtx cond = XEXP (x, 0);
3947   rtx true = XEXP (x, 1);
3948   rtx false = XEXP (x, 2);
3949   enum rtx_code true_code = GET_CODE (cond);
3950   int comparison_p = GET_RTX_CLASS (true_code) == '<';
3951   rtx temp;
3952   int i;
3953
3954   /* Simplify storing of the truth value. */
3955   if (comparison_p && true == const_true_rtx && false == const0_rtx)
3956     return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
3957       
3958   /* Also when the truth value has to be reversed. */
3959   if (comparison_p && reversible_comparison_p (cond)
3960       && true == const0_rtx && false == const_true_rtx)
3961     return gen_binary (reverse_condition (true_code),
3962                        mode, XEXP (cond, 0), XEXP (cond, 1));
3963
3964   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
3965      in it is being compared against certain values.  Get the true and false
3966      comparisons and see if that says anything about the value of each arm.  */
3967
3968   if (comparison_p && reversible_comparison_p (cond)
3969       && GET_CODE (XEXP (cond, 0)) == REG)
3970     {
3971       HOST_WIDE_INT nzb;
3972       rtx from = XEXP (cond, 0);
3973       enum rtx_code false_code = reverse_condition (true_code);
3974       rtx true_val = XEXP (cond, 1);
3975       rtx false_val = true_val;
3976       int swapped = 0;
3977
3978       /* If FALSE_CODE is EQ, swap the codes and arms.  */
3979
3980       if (false_code == EQ)
3981         {
3982           swapped = 1, true_code = EQ, false_code = NE;
3983           temp = true, true = false, false = temp;
3984         }
3985
3986       /* If we are comparing against zero and the expression being tested has
3987          only a single bit that might be nonzero, that is its value when it is
3988          not equal to zero.  Similarly if it is known to be -1 or 0.  */
3989
3990       if (true_code == EQ && true_val == const0_rtx
3991           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
3992         false_code = EQ, false_val = GEN_INT (nzb);
3993       else if (true_code == EQ && true_val == const0_rtx
3994                && (num_sign_bit_copies (from, GET_MODE (from))
3995                    == GET_MODE_BITSIZE (GET_MODE (from))))
3996         false_code = EQ, false_val = constm1_rtx;
3997
3998       /* Now simplify an arm if we know the value of the register in the
3999          branch and it is used in the arm.  Be careful due to the potential
4000          of locally-shared RTL.  */
4001
4002       if (reg_mentioned_p (from, true))
4003         true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4004                       pc_rtx, pc_rtx, 0, 0);
4005       if (reg_mentioned_p (from, false))
4006         false = subst (known_cond (copy_rtx (false), false_code,
4007                                    from, false_val),
4008                        pc_rtx, pc_rtx, 0, 0);
4009
4010       SUBST (XEXP (x, 1), swapped ? false : true);
4011       SUBST (XEXP (x, 2), swapped ? true : false);
4012
4013       true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4014     }
4015
4016   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4017      reversed, do so to avoid needing two sets of patterns for
4018      subtract-and-branch insns.  Similarly if we have a constant in the true
4019      arm, the false arm is the same as the first operand of the comparison, or
4020      the false arm is more complicated than the true arm.  */
4021
4022   if (comparison_p && reversible_comparison_p (cond)
4023       && (true == pc_rtx 
4024           || (CONSTANT_P (true)
4025               && GET_CODE (false) != CONST_INT && false != pc_rtx)
4026           || true == const0_rtx
4027           || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4028               && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4029           || (GET_CODE (true) == SUBREG
4030               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4031               && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4032           || reg_mentioned_p (true, false)
4033           || rtx_equal_p (false, XEXP (cond, 0))))
4034     {
4035       true_code = reverse_condition (true_code);
4036       SUBST (XEXP (x, 0),
4037              gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4038                          XEXP (cond, 1)));
4039
4040       SUBST (XEXP (x, 1), false);
4041       SUBST (XEXP (x, 2), true);
4042
4043       temp = true, true = false, false = temp, cond = XEXP (x, 0);
4044
4045       /* It is possible that the conditional has been simplified out. */
4046       true_code = GET_CODE (cond);
4047       comparison_p = GET_RTX_CLASS (true_code) == '<';
4048     }
4049
4050   /* If the two arms are identical, we don't need the comparison.  */
4051
4052   if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4053     return true;
4054
4055   /* Convert a == b ? b : a to "a".  */
4056   if (true_code == EQ && ! side_effects_p (cond)
4057       && rtx_equal_p (XEXP (cond, 0), false)
4058       && rtx_equal_p (XEXP (cond, 1), true))
4059     return false;
4060   else if (true_code == NE && ! side_effects_p (cond)
4061            && rtx_equal_p (XEXP (cond, 0), true)
4062            && rtx_equal_p (XEXP (cond, 1), false))
4063     return true;
4064
4065   /* Look for cases where we have (abs x) or (neg (abs X)).  */
4066
4067   if (GET_MODE_CLASS (mode) == MODE_INT
4068       && GET_CODE (false) == NEG
4069       && rtx_equal_p (true, XEXP (false, 0))
4070       && comparison_p
4071       && rtx_equal_p (true, XEXP (cond, 0))
4072       && ! side_effects_p (true))
4073     switch (true_code)
4074       {
4075       case GT:
4076       case GE:
4077         return gen_unary (ABS, mode, mode, true);
4078       case LT:
4079       case LE:
4080         return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
4081       }
4082
4083   /* Look for MIN or MAX.  */
4084
4085   if ((! FLOAT_MODE_P (mode) || flag_fast_math)
4086       && comparison_p
4087       && rtx_equal_p (XEXP (cond, 0), true)
4088       && rtx_equal_p (XEXP (cond, 1), false)
4089       && ! side_effects_p (cond))
4090     switch (true_code)
4091       {
4092       case GE:
4093       case GT:
4094         return gen_binary (SMAX, mode, true, false);
4095       case LE:
4096       case LT:
4097         return gen_binary (SMIN, mode, true, false);
4098       case GEU:
4099       case GTU:
4100         return gen_binary (UMAX, mode, true, false);
4101       case LEU:
4102       case LTU:
4103         return gen_binary (UMIN, mode, true, false);
4104       }
4105   
4106 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
4107
4108   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4109      second operand is zero, this can be done as (OP Z (mult COND C2)) where
4110      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4111      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4112      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4113      neither of the above, but it isn't worth checking for.  */
4114
4115   if (comparison_p && mode != VOIDmode && ! side_effects_p (x))
4116     {
4117       rtx t = make_compound_operation (true, SET);
4118       rtx f = make_compound_operation (false, SET);
4119       rtx cond_op0 = XEXP (cond, 0);
4120       rtx cond_op1 = XEXP (cond, 1);
4121       enum rtx_code op, extend_op = NIL;
4122       enum machine_mode m = mode;
4123       rtx z = 0, c1;
4124
4125       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4126            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4127            || GET_CODE (t) == ASHIFT
4128            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4129           && rtx_equal_p (XEXP (t, 0), f))
4130         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4131
4132       /* If an identity-zero op is commutative, check whether there
4133          would be a match if we swapped the operands. */
4134       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4135                 || GET_CODE (t) == XOR)
4136                && rtx_equal_p (XEXP (t, 1), f))
4137         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4138       else if (GET_CODE (t) == SIGN_EXTEND
4139                && (GET_CODE (XEXP (t, 0)) == PLUS
4140                    || GET_CODE (XEXP (t, 0)) == MINUS
4141                    || GET_CODE (XEXP (t, 0)) == IOR
4142                    || GET_CODE (XEXP (t, 0)) == XOR
4143                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4144                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4145                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4146                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4147                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4148                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4149                && (num_sign_bit_copies (f, GET_MODE (f))
4150                    > (GET_MODE_BITSIZE (mode)
4151                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4152         {
4153           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4154           extend_op = SIGN_EXTEND;
4155           m = GET_MODE (XEXP (t, 0));
4156         }
4157       else if (GET_CODE (t) == SIGN_EXTEND
4158                && (GET_CODE (XEXP (t, 0)) == PLUS
4159                    || GET_CODE (XEXP (t, 0)) == IOR
4160                    || GET_CODE (XEXP (t, 0)) == XOR)
4161                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4162                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4163                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4164                && (num_sign_bit_copies (f, GET_MODE (f))
4165                    > (GET_MODE_BITSIZE (mode)
4166                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4167         {
4168           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4169           extend_op = SIGN_EXTEND;
4170           m = GET_MODE (XEXP (t, 0));
4171         }
4172       else if (GET_CODE (t) == ZERO_EXTEND
4173                && (GET_CODE (XEXP (t, 0)) == PLUS
4174                    || GET_CODE (XEXP (t, 0)) == MINUS
4175                    || GET_CODE (XEXP (t, 0)) == IOR
4176                    || GET_CODE (XEXP (t, 0)) == XOR
4177                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4178                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4179                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4180                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4181                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4182                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4183                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4184                && ((nonzero_bits (f, GET_MODE (f))
4185                     & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4186                    == 0))
4187         {
4188           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4189           extend_op = ZERO_EXTEND;
4190           m = GET_MODE (XEXP (t, 0));
4191         }
4192       else if (GET_CODE (t) == ZERO_EXTEND
4193                && (GET_CODE (XEXP (t, 0)) == PLUS
4194                    || GET_CODE (XEXP (t, 0)) == IOR
4195                    || GET_CODE (XEXP (t, 0)) == XOR)
4196                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4197                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4198                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4199                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4200                && ((nonzero_bits (f, GET_MODE (f))
4201                     & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4202                    == 0))
4203         {
4204           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4205           extend_op = ZERO_EXTEND;
4206           m = GET_MODE (XEXP (t, 0));
4207         }
4208       
4209       if (z)
4210         {
4211           temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4212                         pc_rtx, pc_rtx, 0, 0);
4213           temp = gen_binary (MULT, m, temp,
4214                              gen_binary (MULT, m, c1, const_true_rtx));
4215           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4216           temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4217
4218           if (extend_op != NIL)
4219             temp = gen_unary (extend_op, mode, m, temp);
4220
4221           return temp;
4222         }
4223     }
4224 #endif
4225
4226   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4227      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4228      negation of a single bit, we can convert this operation to a shift.  We
4229      can actually do this more generally, but it doesn't seem worth it.  */
4230
4231   if (true_code == NE && XEXP (cond, 1) == const0_rtx
4232       && false == const0_rtx && GET_CODE (true) == CONST_INT
4233       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4234            && (i = exact_log2 (INTVAL (true))) >= 0)
4235           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4236                == GET_MODE_BITSIZE (mode))
4237               && (i = exact_log2 (- INTVAL (true))) >= 0)))
4238     return
4239       simplify_shift_const (NULL_RTX, ASHIFT, mode,
4240                             gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4241
4242   return x;
4243 }
4244 \f
4245 /* Simplify X, a SET expression.  Return the new expression.  */
4246
4247 static rtx
4248 simplify_set (x)
4249      rtx x;
4250 {
4251   rtx src = SET_SRC (x);
4252   rtx dest = SET_DEST (x);
4253   enum machine_mode mode
4254     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4255   rtx other_insn;
4256   rtx *cc_use;
4257
4258   /* (set (pc) (return)) gets written as (return).  */
4259   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4260     return src;
4261
4262   /* Now that we know for sure which bits of SRC we are using, see if we can
4263      simplify the expression for the object knowing that we only need the
4264      low-order bits.  */
4265
4266   if (GET_MODE_CLASS (mode) == MODE_INT)
4267     src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4268
4269   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4270      the comparison result and try to simplify it unless we already have used
4271      undobuf.other_insn.  */
4272   if ((GET_CODE (src) == COMPARE
4273 #ifdef HAVE_cc0
4274        || dest == cc0_rtx
4275 #endif
4276        )
4277       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4278       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4279       && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4280       && rtx_equal_p (XEXP (*cc_use, 0), dest))
4281     {
4282       enum rtx_code old_code = GET_CODE (*cc_use);
4283       enum rtx_code new_code;
4284       rtx op0, op1;
4285       int other_changed = 0;
4286       enum machine_mode compare_mode = GET_MODE (dest);
4287
4288       if (GET_CODE (src) == COMPARE)
4289         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4290       else
4291         op0 = src, op1 = const0_rtx;
4292
4293       /* Simplify our comparison, if possible.  */
4294       new_code = simplify_comparison (old_code, &op0, &op1);
4295
4296 #ifdef EXTRA_CC_MODES
4297       /* If this machine has CC modes other than CCmode, check to see if we
4298          need to use a different CC mode here.  */
4299       compare_mode = SELECT_CC_MODE (new_code, op0, op1);
4300 #endif /* EXTRA_CC_MODES */
4301
4302 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
4303       /* If the mode changed, we have to change SET_DEST, the mode in the
4304          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
4305          a hard register, just build new versions with the proper mode.  If it
4306          is a pseudo, we lose unless it is only time we set the pseudo, in
4307          which case we can safely change its mode.  */
4308       if (compare_mode != GET_MODE (dest))
4309         {
4310           int regno = REGNO (dest);
4311           rtx new_dest = gen_rtx (REG, compare_mode, regno);
4312
4313           if (regno < FIRST_PSEUDO_REGISTER
4314               || (reg_n_sets[regno] == 1 && ! REG_USERVAR_P (dest)))
4315             {
4316               if (regno >= FIRST_PSEUDO_REGISTER)
4317                 SUBST (regno_reg_rtx[regno], new_dest);
4318
4319               SUBST (SET_DEST (x), new_dest);
4320               SUBST (XEXP (*cc_use, 0), new_dest);
4321               other_changed = 1;
4322
4323               dest = new_dest;
4324             }
4325         }
4326 #endif
4327
4328       /* If the code changed, we have to build a new comparison in
4329          undobuf.other_insn.  */
4330       if (new_code != old_code)
4331         {
4332           unsigned HOST_WIDE_INT mask;
4333
4334           SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4335                                            dest, const0_rtx));
4336
4337           /* If the only change we made was to change an EQ into an NE or
4338              vice versa, OP0 has only one bit that might be nonzero, and OP1
4339              is zero, check if changing the user of the condition code will
4340              produce a valid insn.  If it won't, we can keep the original code
4341              in that insn by surrounding our operation with an XOR.  */
4342
4343           if (((old_code == NE && new_code == EQ)
4344                || (old_code == EQ && new_code == NE))
4345               && ! other_changed && op1 == const0_rtx
4346               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4347               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
4348             {
4349               rtx pat = PATTERN (other_insn), note = 0;
4350               int scratches;
4351
4352               if ((recog_for_combine (&pat, other_insn, &note, &scratches) < 0
4353                    && ! check_asm_operands (pat)))
4354                 {
4355                   PUT_CODE (*cc_use, old_code);
4356                   other_insn = 0;
4357
4358                   op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
4359                 }
4360             }
4361
4362           other_changed = 1;
4363         }
4364
4365       if (other_changed)
4366         undobuf.other_insn = other_insn;
4367
4368 #ifdef HAVE_cc0
4369       /* If we are now comparing against zero, change our source if
4370          needed.  If we do not use cc0, we always have a COMPARE.  */
4371       if (op1 == const0_rtx && dest == cc0_rtx)
4372         {
4373           SUBST (SET_SRC (x), op0);
4374           src = op0;
4375         }
4376       else
4377 #endif
4378
4379       /* Otherwise, if we didn't previously have a COMPARE in the
4380          correct mode, we need one.  */
4381       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4382         {
4383           SUBST (SET_SRC (x),
4384                  gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4385           src = SET_SRC (x);
4386         }
4387       else
4388         {
4389           /* Otherwise, update the COMPARE if needed.  */
4390           SUBST (XEXP (src, 0), op0);
4391           SUBST (XEXP (src, 1), op1);
4392         }
4393     }
4394   else
4395     {
4396       /* Get SET_SRC in a form where we have placed back any
4397          compound expressions.  Then do the checks below.  */
4398       src = make_compound_operation (src, SET);
4399       SUBST (SET_SRC (x), src);
4400     }
4401
4402   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4403      and X being a REG or (subreg (reg)), we may be able to convert this to
4404      (set (subreg:m2 x) (op)). 
4405
4406      We can always do this if M1 is narrower than M2 because that means that
4407      we only care about the low bits of the result.
4408
4409      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
4410      perform a narrower operation that requested since the high-order bits will
4411      be undefined.  On machine where it is defined, this transformation is safe
4412      as long as M1 and M2 have the same number of words.  */
4413  
4414   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4415       && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4416       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4417            / UNITS_PER_WORD)
4418           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4419                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
4420 #ifndef WORD_REGISTER_OPERATIONS
4421       && (GET_MODE_SIZE (GET_MODE (src))
4422           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4423 #endif
4424 #ifdef CLASS_CANNOT_CHANGE_SIZE
4425       && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4426             && (TEST_HARD_REG_BIT
4427                 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4428                  REGNO (dest)))
4429             && (GET_MODE_SIZE (GET_MODE (src))
4430                 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4431 #endif                            
4432       && (GET_CODE (dest) == REG
4433           || (GET_CODE (dest) == SUBREG
4434               && GET_CODE (SUBREG_REG (dest)) == REG)))
4435     {
4436       SUBST (SET_DEST (x),
4437              gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4438                                       dest));
4439       SUBST (SET_SRC (x), SUBREG_REG (src));
4440
4441       src = SET_SRC (x), dest = SET_DEST (x);
4442     }
4443
4444 #ifdef LOAD_EXTEND_OP
4445   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4446      would require a paradoxical subreg.  Replace the subreg with a
4447      zero_extend to avoid the reload that would otherwise be required. */
4448
4449   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4450       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4451       && SUBREG_WORD (src) == 0
4452       && (GET_MODE_SIZE (GET_MODE (src))
4453           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4454       && GET_CODE (SUBREG_REG (src)) == MEM)
4455     {
4456       SUBST (SET_SRC (x),
4457              gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4458                               GET_MODE (src), XEXP (src, 0)));
4459
4460       src = SET_SRC (x);
4461     }
4462 #endif
4463
4464   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4465      are comparing an item known to be 0 or -1 against 0, use a logical
4466      operation instead. Check for one of the arms being an IOR of the other
4467      arm with some value.  We compute three terms to be IOR'ed together.  In
4468      practice, at most two will be nonzero.  Then we do the IOR's.  */
4469
4470   if (GET_CODE (dest) != PC
4471       && GET_CODE (src) == IF_THEN_ELSE
4472       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
4473       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4474       && XEXP (XEXP (src, 0), 1) == const0_rtx
4475       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
4476 #ifdef HAVE_conditional_move
4477       && ! can_conditionally_move_p (GET_MODE (src))
4478 #endif
4479       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4480                                GET_MODE (XEXP (XEXP (src, 0), 0)))
4481           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4482       && ! side_effects_p (src))
4483     {
4484       rtx true = (GET_CODE (XEXP (src, 0)) == NE
4485                       ? XEXP (src, 1) : XEXP (src, 2));
4486       rtx false = (GET_CODE (XEXP (src, 0)) == NE
4487                    ? XEXP (src, 2) : XEXP (src, 1));
4488       rtx term1 = const0_rtx, term2, term3;
4489
4490       if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4491         term1 = false, true = XEXP (true, 1), false = const0_rtx;
4492       else if (GET_CODE (true) == IOR
4493                && rtx_equal_p (XEXP (true, 1), false))
4494         term1 = false, true = XEXP (true, 0), false = const0_rtx;
4495       else if (GET_CODE (false) == IOR
4496                && rtx_equal_p (XEXP (false, 0), true))
4497         term1 = true, false = XEXP (false, 1), true = const0_rtx;
4498       else if (GET_CODE (false) == IOR
4499                && rtx_equal_p (XEXP (false, 1), true))
4500         term1 = true, false = XEXP (false, 0), true = const0_rtx;
4501
4502       term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4503       term3 = gen_binary (AND, GET_MODE (src),
4504                           gen_unary (NOT, GET_MODE (src), GET_MODE (src),
4505                                      XEXP (XEXP (src, 0), 0)),
4506                           false);
4507
4508       SUBST (SET_SRC (x),
4509              gen_binary (IOR, GET_MODE (src),
4510                          gen_binary (IOR, GET_MODE (src), term1, term2),
4511                          term3));
4512
4513       src = SET_SRC (x);
4514     }
4515
4516   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
4517      whole thing fail.  */
4518   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
4519     return src;
4520   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
4521     return dest;
4522   else
4523     /* Convert this into a field assignment operation, if possible.  */
4524     return make_field_assignment (x);
4525 }
4526 \f
4527 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
4528    result.  LAST is nonzero if this is the last retry.  */
4529
4530 static rtx
4531 simplify_logical (x, last)
4532      rtx x;
4533      int last;
4534 {
4535   enum machine_mode mode = GET_MODE (x);
4536   rtx op0 = XEXP (x, 0);
4537   rtx op1 = XEXP (x, 1);
4538
4539   switch (GET_CODE (x))
4540     {
4541     case AND:
4542       /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4543          insn (and may simplify more).  */
4544       if (GET_CODE (op0) == XOR
4545           && rtx_equal_p (XEXP (op0, 0), op1)
4546           && ! side_effects_p (op1))
4547         x = gen_binary (AND, mode,
4548                         gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
4549
4550       if (GET_CODE (op0) == XOR
4551           && rtx_equal_p (XEXP (op0, 1), op1)
4552           && ! side_effects_p (op1))
4553         x = gen_binary (AND, mode,
4554                         gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
4555
4556       /* Similarly for (~ (A ^ B)) & A.  */
4557       if (GET_CODE (op0) == NOT
4558           && GET_CODE (XEXP (op0, 0)) == XOR
4559           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4560           && ! side_effects_p (op1))
4561         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4562
4563       if (GET_CODE (op0) == NOT
4564           && GET_CODE (XEXP (op0, 0)) == XOR
4565           && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4566           && ! side_effects_p (op1))
4567         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4568
4569       if (GET_CODE (op1) == CONST_INT)
4570         {
4571           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
4572
4573           /* If we have (ior (and (X C1) C2)) and the next restart would be
4574              the last, simplify this by making C1 as small as possible
4575              and then exit. */
4576           if (last
4577               && GET_CODE (x) == IOR && GET_CODE (op0) == AND
4578               && GET_CODE (XEXP (op0, 1)) == CONST_INT
4579               && GET_CODE (op1) == CONST_INT)
4580             return gen_binary (IOR, mode,
4581                                gen_binary (AND, mode, XEXP (op0, 0),
4582                                            GEN_INT (INTVAL (XEXP (op0, 1))
4583                                                     & ~ INTVAL (op1))), op1);
4584
4585           if (GET_CODE (x) != AND)
4586             return x;
4587
4588           if (GET_RTX_CLASS (GET_CODE (x)) == 'c' 
4589               || GET_RTX_CLASS (GET_CODE (x)) == '2')
4590             op0 = XEXP (x, 0), op1 = XEXP (x, 1);
4591         }
4592
4593       /* Convert (A | B) & A to A.  */
4594       if (GET_CODE (op0) == IOR
4595           && (rtx_equal_p (XEXP (op0, 0), op1)
4596               || rtx_equal_p (XEXP (op0, 1), op1))
4597           && ! side_effects_p (XEXP (op0, 0))
4598           && ! side_effects_p (XEXP (op0, 1)))
4599         return op1;
4600
4601       /* In the following group of tests (and those in case IOR below),
4602          we start with some combination of logical operations and apply
4603          the distributive law followed by the inverse distributive law.
4604          Most of the time, this results in no change.  However, if some of
4605          the operands are the same or inverses of each other, simplifications
4606          will result.
4607
4608          For example, (and (ior A B) (not B)) can occur as the result of
4609          expanding a bit field assignment.  When we apply the distributive
4610          law to this, we get (ior (and (A (not B))) (and (B (not B)))),
4611          which then simplifies to (and (A (not B))). 
4612
4613          If we have (and (ior A B) C), apply the distributive law and then
4614          the inverse distributive law to see if things simplify.  */
4615
4616       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4617         {
4618           x = apply_distributive_law
4619             (gen_binary (GET_CODE (op0), mode,
4620                          gen_binary (AND, mode, XEXP (op0, 0), op1),
4621                          gen_binary (AND, mode, XEXP (op0, 1), op1)));
4622           if (GET_CODE (x) != AND)
4623             return x;
4624         }
4625
4626       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
4627         return apply_distributive_law
4628           (gen_binary (GET_CODE (op1), mode,
4629                        gen_binary (AND, mode, XEXP (op1, 0), op0),
4630                        gen_binary (AND, mode, XEXP (op1, 1), op0)));
4631
4632       /* Similarly, taking advantage of the fact that
4633          (and (not A) (xor B C)) == (xor (ior A B) (ior A C))  */
4634
4635       if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
4636         return apply_distributive_law
4637           (gen_binary (XOR, mode,
4638                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
4639                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
4640                                                             
4641       else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
4642         return apply_distributive_law
4643           (gen_binary (XOR, mode,
4644                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
4645                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
4646       break;
4647
4648     case IOR:
4649       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
4650       if (GET_CODE (op1) == CONST_INT
4651           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4652           && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
4653         return op1;
4654
4655       /* Convert (A & B) | A to A.  */
4656       if (GET_CODE (op0) == AND
4657           && (rtx_equal_p (XEXP (op0, 0), op1)
4658               || rtx_equal_p (XEXP (op0, 1), op1))
4659           && ! side_effects_p (XEXP (op0, 0))
4660           && ! side_effects_p (XEXP (op0, 1)))
4661         return op1;
4662
4663       /* If we have (ior (and A B) C), apply the distributive law and then
4664          the inverse distributive law to see if things simplify.  */
4665
4666       if (GET_CODE (op0) == AND)
4667         {
4668           x = apply_distributive_law
4669             (gen_binary (AND, mode,
4670                          gen_binary (IOR, mode, XEXP (op0, 0), op1),
4671                          gen_binary (IOR, mode, XEXP (op0, 1), op1)));
4672
4673           if (GET_CODE (x) != IOR)
4674             return x;
4675         }
4676
4677       if (GET_CODE (op1) == AND)
4678         {
4679           x = apply_distributive_law
4680             (gen_binary (AND, mode,
4681                          gen_binary (IOR, mode, XEXP (op1, 0), op0),
4682                          gen_binary (IOR, mode, XEXP (op1, 1), op0)));
4683
4684           if (GET_CODE (x) != IOR)
4685             return x;
4686         }
4687
4688       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4689          mode size to (rotate A CX).  */
4690
4691       if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
4692            || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
4693           && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
4694           && GET_CODE (XEXP (op0, 1)) == CONST_INT
4695           && GET_CODE (XEXP (op1, 1)) == CONST_INT
4696           && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
4697               == GET_MODE_BITSIZE (mode)))
4698         return gen_rtx (ROTATE, mode, XEXP (op0, 0),
4699                         (GET_CODE (op0) == ASHIFT
4700                          ? XEXP (op0, 1) : XEXP (op1, 1)));
4701
4702       /* If OP0 is (ashiftrt (plus ...) C), it might actually be
4703          a (sign_extend (plus ...)).  If so, OP1 is a CONST_INT, and the PLUS
4704          does not affect any of the bits in OP1, it can really be done
4705          as a PLUS and we can associate.  We do this by seeing if OP1
4706          can be safely shifted left C bits.  */
4707       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
4708           && GET_CODE (XEXP (op0, 0)) == PLUS
4709           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
4710           && GET_CODE (XEXP (op0, 1)) == CONST_INT
4711           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
4712         {
4713           int count = INTVAL (XEXP (op0, 1));
4714           HOST_WIDE_INT mask = INTVAL (op1) << count;
4715
4716           if (mask >> count == INTVAL (op1)
4717               && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
4718             {
4719               SUBST (XEXP (XEXP (op0, 0), 1),
4720                      GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
4721               return op0;
4722             }
4723         }
4724       break;
4725
4726     case XOR:
4727       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
4728          Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
4729          (NOT y).  */
4730       {
4731         int num_negated = 0;
4732
4733         if (GET_CODE (op0) == NOT)
4734           num_negated++, op0 = XEXP (op0, 0);
4735         if (GET_CODE (op1) == NOT)
4736           num_negated++, op1 = XEXP (op1, 0);
4737
4738         if (num_negated == 2)
4739           {
4740             SUBST (XEXP (x, 0), op0);
4741             SUBST (XEXP (x, 1), op1);
4742           }
4743         else if (num_negated == 1)
4744           return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
4745       }
4746
4747       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
4748          correspond to a machine insn or result in further simplifications
4749          if B is a constant.  */
4750
4751       if (GET_CODE (op0) == AND
4752           && rtx_equal_p (XEXP (op0, 1), op1)
4753           && ! side_effects_p (op1))
4754         return gen_binary (AND, mode,
4755                            gen_unary (NOT, mode, mode, XEXP (op0, 0)),
4756                            op1);
4757
4758       else if (GET_CODE (op0) == AND
4759                && rtx_equal_p (XEXP (op0, 0), op1)
4760                && ! side_effects_p (op1))
4761         return gen_binary (AND, mode,
4762                            gen_unary (NOT, mode, mode, XEXP (op0, 1)),
4763                            op1);
4764
4765 #if STORE_FLAG_VALUE == 1
4766       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
4767          comparison.  */
4768       if (op1 == const1_rtx
4769           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
4770           && reversible_comparison_p (op0))
4771         return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
4772                                 mode, XEXP (op0, 0), XEXP (op0, 1));
4773
4774       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
4775          is (lt foo (const_int 0)), so we can perform the above
4776          simplification.  */
4777
4778       if (op1 == const1_rtx
4779           && GET_CODE (op0) == LSHIFTRT
4780           && GET_CODE (XEXP (op0, 1)) == CONST_INT
4781           && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
4782         return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
4783 #endif
4784
4785       /* (xor (comparison foo bar) (const_int sign-bit))
4786          when STORE_FLAG_VALUE is the sign bit.  */
4787       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4788           && (STORE_FLAG_VALUE
4789               == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4790           && op1 == const_true_rtx
4791           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
4792           && reversible_comparison_p (op0))
4793         return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
4794                                 mode, XEXP (op0, 0), XEXP (op0, 1));
4795       break;
4796     }
4797
4798   return x;
4799 }
4800 \f
4801 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
4802    operations" because they can be replaced with two more basic operations.
4803    ZERO_EXTEND is also considered "compound" because it can be replaced with
4804    an AND operation, which is simpler, though only one operation.
4805
4806    The function expand_compound_operation is called with an rtx expression
4807    and will convert it to the appropriate shifts and AND operations, 
4808    simplifying at each stage.
4809
4810    The function make_compound_operation is called to convert an expression
4811    consisting of shifts and ANDs into the equivalent compound expression.
4812    It is the inverse of this function, loosely speaking.  */
4813
4814 static rtx
4815 expand_compound_operation (x)
4816      rtx x;
4817 {
4818   int pos = 0, len;
4819   int unsignedp = 0;
4820   int modewidth;
4821   rtx tem;
4822
4823   switch (GET_CODE (x))
4824     {
4825     case ZERO_EXTEND:
4826       unsignedp = 1;
4827     case SIGN_EXTEND:
4828       /* We can't necessarily use a const_int for a multiword mode;
4829          it depends on implicitly extending the value.
4830          Since we don't know the right way to extend it,
4831          we can't tell whether the implicit way is right.
4832
4833          Even for a mode that is no wider than a const_int,
4834          we can't win, because we need to sign extend one of its bits through
4835          the rest of it, and we don't know which bit.  */
4836       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4837         return x;
4838
4839       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
4840          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
4841          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
4842          reloaded. If not for that, MEM's would very rarely be safe.
4843
4844          Reject MODEs bigger than a word, because we might not be able
4845          to reference a two-register group starting with an arbitrary register
4846          (and currently gen_lowpart might crash for a SUBREG).  */
4847   
4848       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
4849         return x;
4850
4851       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
4852       /* If the inner object has VOIDmode (the only way this can happen
4853          is if it is a ASM_OPERANDS), we can't do anything since we don't
4854          know how much masking to do.  */
4855       if (len == 0)
4856         return x;
4857
4858       break;
4859
4860     case ZERO_EXTRACT:
4861       unsignedp = 1;
4862     case SIGN_EXTRACT:
4863       /* If the operand is a CLOBBER, just return it.  */
4864       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
4865         return XEXP (x, 0);
4866
4867       if (GET_CODE (XEXP (x, 1)) != CONST_INT
4868           || GET_CODE (XEXP (x, 2)) != CONST_INT
4869           || GET_MODE (XEXP (x, 0)) == VOIDmode)
4870         return x;
4871
4872       len = INTVAL (XEXP (x, 1));
4873       pos = INTVAL (XEXP (x, 2));
4874
4875       /* If this goes outside the object being extracted, replace the object
4876          with a (use (mem ...)) construct that only combine understands
4877          and is used only for this purpose.  */
4878       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4879         SUBST (XEXP (x, 0), gen_rtx (USE, GET_MODE (x), XEXP (x, 0)));
4880
4881       if (BITS_BIG_ENDIAN)
4882         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
4883
4884       break;
4885
4886     default:
4887       return x;
4888     }
4889
4890   /* If we reach here, we want to return a pair of shifts.  The inner
4891      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
4892      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
4893      logical depending on the value of UNSIGNEDP.
4894
4895      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
4896      converted into an AND of a shift.
4897
4898      We must check for the case where the left shift would have a negative
4899      count.  This can happen in a case like (x >> 31) & 255 on machines
4900      that can't shift by a constant.  On those machines, we would first
4901      combine the shift with the AND to produce a variable-position 
4902      extraction.  Then the constant of 31 would be substituted in to produce
4903      a such a position.  */
4904
4905   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
4906   if (modewidth >= pos - len)
4907     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
4908                                 GET_MODE (x),
4909                                 simplify_shift_const (NULL_RTX, ASHIFT,
4910                                                       GET_MODE (x),
4911                                                       XEXP (x, 0),
4912                                                       modewidth - pos - len),
4913                                 modewidth - len);
4914
4915   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
4916     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
4917                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
4918                                                         GET_MODE (x),
4919                                                         XEXP (x, 0), pos),
4920                                   ((HOST_WIDE_INT) 1 << len) - 1);
4921   else
4922     /* Any other cases we can't handle.  */
4923     return x;
4924     
4925
4926   /* If we couldn't do this for some reason, return the original
4927      expression.  */
4928   if (GET_CODE (tem) == CLOBBER)
4929     return x;
4930
4931   return tem;
4932 }
4933 \f
4934 /* X is a SET which contains an assignment of one object into
4935    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
4936    or certain SUBREGS). If possible, convert it into a series of
4937    logical operations.
4938
4939    We half-heartedly support variable positions, but do not at all
4940    support variable lengths.  */
4941
4942 static rtx
4943 expand_field_assignment (x)
4944      rtx x;
4945 {
4946   rtx inner;
4947   rtx pos;                      /* Always counts from low bit. */
4948   int len;
4949   rtx mask;
4950   enum machine_mode compute_mode;
4951
4952   /* Loop until we find something we can't simplify.  */
4953   while (1)
4954     {
4955       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
4956           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
4957         {
4958           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
4959           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
4960           pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
4961         }
4962       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4963                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
4964         {
4965           inner = XEXP (SET_DEST (x), 0);
4966           len = INTVAL (XEXP (SET_DEST (x), 1));
4967           pos = XEXP (SET_DEST (x), 2);
4968
4969           /* If the position is constant and spans the width of INNER,
4970              surround INNER  with a USE to indicate this.  */
4971           if (GET_CODE (pos) == CONST_INT
4972               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
4973             inner = gen_rtx (USE, GET_MODE (SET_DEST (x)), inner);
4974
4975           if (BITS_BIG_ENDIAN)
4976             {
4977               if (GET_CODE (pos) == CONST_INT)
4978                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
4979                                - INTVAL (pos));
4980               else if (GET_CODE (pos) == MINUS
4981                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
4982                        && (INTVAL (XEXP (pos, 1))
4983                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
4984                 /* If position is ADJUST - X, new position is X.  */
4985                 pos = XEXP (pos, 0);
4986               else
4987                 pos = gen_binary (MINUS, GET_MODE (pos),
4988                                   GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
4989                                            - len),
4990                                   pos);
4991             }
4992         }
4993
4994       /* A SUBREG between two modes that occupy the same numbers of words
4995          can be done by moving the SUBREG to the source.  */
4996       else if (GET_CODE (SET_DEST (x)) == SUBREG
4997                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
4998                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
4999                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5000                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5001         {
5002           x = gen_rtx (SET, VOIDmode, SUBREG_REG (SET_DEST (x)),
5003                        gen_lowpart_for_combine (GET_MODE (SUBREG_REG (SET_DEST (x))),
5004                                                 SET_SRC (x)));
5005           continue;
5006         }
5007       else
5008         break;
5009
5010       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5011         inner = SUBREG_REG (inner);
5012
5013       compute_mode = GET_MODE (inner);
5014
5015       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
5016       if (len < HOST_BITS_PER_WIDE_INT)
5017         mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5018       else
5019         break;
5020
5021       /* Now compute the equivalent expression.  Make a copy of INNER
5022          for the SET_DEST in case it is a MEM into which we will substitute;
5023          we don't want shared RTL in that case.  */
5024       x = gen_rtx (SET, VOIDmode, copy_rtx (inner),
5025                    gen_binary (IOR, compute_mode,
5026                                gen_binary (AND, compute_mode,
5027                                            gen_unary (NOT, compute_mode,
5028                                                       compute_mode,
5029                                                       gen_binary (ASHIFT,
5030                                                                   compute_mode,
5031                                                                   mask, pos)),
5032                                            inner),
5033                                gen_binary (ASHIFT, compute_mode,
5034                                            gen_binary (AND, compute_mode,
5035                                                        gen_lowpart_for_combine
5036                                                        (compute_mode,
5037                                                         SET_SRC (x)),
5038                                                        mask),
5039                                            pos)));
5040     }
5041
5042   return x;
5043 }
5044 \f
5045 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
5046    it is an RTX that represents a variable starting position; otherwise,
5047    POS is the (constant) starting bit position (counted from the LSB).
5048
5049    INNER may be a USE.  This will occur when we started with a bitfield
5050    that went outside the boundary of the object in memory, which is
5051    allowed on most machines.  To isolate this case, we produce a USE
5052    whose mode is wide enough and surround the MEM with it.  The only
5053    code that understands the USE is this routine.  If it is not removed,
5054    it will cause the resulting insn not to match.
5055
5056    UNSIGNEDP is non-zero for an unsigned reference and zero for a 
5057    signed reference.
5058
5059    IN_DEST is non-zero if this is a reference in the destination of a
5060    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If non-zero,
5061    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5062    be used.
5063
5064    IN_COMPARE is non-zero if we are in a COMPARE.  This means that a
5065    ZERO_EXTRACT should be built even for bits starting at bit 0.
5066
5067    MODE is the desired mode of the result (if IN_DEST == 0).
5068
5069    The result is an RTX for the extraction or NULL_RTX if the target
5070    can't handle it.  */
5071
5072 static rtx
5073 make_extraction (mode, inner, pos, pos_rtx, len,
5074                  unsignedp, in_dest, in_compare)
5075      enum machine_mode mode;
5076      rtx inner;
5077      int pos;
5078      rtx pos_rtx;
5079      int len;
5080      int unsignedp;
5081      int in_dest, in_compare;
5082 {
5083   /* This mode describes the size of the storage area
5084      to fetch the overall value from.  Within that, we
5085      ignore the POS lowest bits, etc.  */
5086   enum machine_mode is_mode = GET_MODE (inner);
5087   enum machine_mode inner_mode;
5088   enum machine_mode wanted_inner_mode = byte_mode;
5089   enum machine_mode wanted_inner_reg_mode = word_mode;
5090   enum machine_mode pos_mode = word_mode;
5091   enum machine_mode extraction_mode = word_mode;
5092   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5093   int spans_byte = 0;
5094   rtx new = 0;
5095   rtx orig_pos_rtx = pos_rtx;
5096   int orig_pos;
5097
5098   /* Get some information about INNER and get the innermost object.  */
5099   if (GET_CODE (inner) == USE)
5100     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
5101     /* We don't need to adjust the position because we set up the USE
5102        to pretend that it was a full-word object.  */
5103     spans_byte = 1, inner = XEXP (inner, 0);
5104   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5105     {
5106       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5107          consider just the QI as the memory to extract from.
5108          The subreg adds or removes high bits; its mode is
5109          irrelevant to the meaning of this extraction,
5110          since POS and LEN count from the lsb.  */
5111       if (GET_CODE (SUBREG_REG (inner)) == MEM)
5112         is_mode = GET_MODE (SUBREG_REG (inner));
5113       inner = SUBREG_REG (inner);
5114     }
5115
5116   inner_mode = GET_MODE (inner);
5117
5118   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5119     pos = INTVAL (pos_rtx), pos_rtx = 0;
5120
5121   /* See if this can be done without an extraction.  We never can if the
5122      width of the field is not the same as that of some integer mode. For
5123      registers, we can only avoid the extraction if the position is at the
5124      low-order bit and this is either not in the destination or we have the
5125      appropriate STRICT_LOW_PART operation available.
5126
5127      For MEM, we can avoid an extract if the field starts on an appropriate
5128      boundary and we can change the mode of the memory reference.  However,
5129      we cannot directly access the MEM if we have a USE and the underlying
5130      MEM is not TMODE.  This combination means that MEM was being used in a
5131      context where bits outside its mode were being referenced; that is only
5132      valid in bit-field insns.  */
5133
5134   if (tmode != BLKmode
5135       && ! (spans_byte && inner_mode != tmode)
5136       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5137            && GET_CODE (inner) != MEM
5138            && (! in_dest
5139                || (GET_CODE (inner) == REG
5140                    && (movstrict_optab->handlers[(int) tmode].insn_code
5141                        != CODE_FOR_nothing))))
5142           || (GET_CODE (inner) == MEM && pos_rtx == 0
5143               && (pos
5144                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5145                      : BITS_PER_UNIT)) == 0
5146               /* We can't do this if we are widening INNER_MODE (it
5147                  may not be aligned, for one thing).  */
5148               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5149               && (inner_mode == tmode
5150                   || (! mode_dependent_address_p (XEXP (inner, 0))
5151                       && ! MEM_VOLATILE_P (inner))))))
5152     {
5153       /* If INNER is a MEM, make a new MEM that encompasses just the desired
5154          field.  If the original and current mode are the same, we need not
5155          adjust the offset.  Otherwise, we do if bytes big endian.  
5156
5157          If INNER is not a MEM, get a piece consisting of just the field
5158          of interest (in this case POS % BITS_PER_WORD must be 0).  */
5159
5160       if (GET_CODE (inner) == MEM)
5161         {
5162           int offset;
5163           /* POS counts from lsb, but make OFFSET count in memory order.  */
5164           if (BYTES_BIG_ENDIAN)
5165             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5166           else
5167             offset = pos / BITS_PER_UNIT;
5168
5169           new = gen_rtx (MEM, tmode, plus_constant (XEXP (inner, 0), offset));
5170           RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
5171           MEM_VOLATILE_P (new) = MEM_VOLATILE_P (inner);
5172           MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (inner);
5173         }
5174       else if (GET_CODE (inner) == REG)
5175         {
5176           /* We can't call gen_lowpart_for_combine here since we always want
5177              a SUBREG and it would sometimes return a new hard register.  */
5178           if (tmode != inner_mode)
5179             new = gen_rtx (SUBREG, tmode, inner,
5180                            (WORDS_BIG_ENDIAN
5181                             && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD
5182                             ? (((GET_MODE_SIZE (inner_mode)
5183                                  - GET_MODE_SIZE (tmode))
5184                                 / UNITS_PER_WORD)
5185                                - pos / BITS_PER_WORD)
5186                             : pos / BITS_PER_WORD));
5187           else
5188             new = inner;
5189         }
5190       else
5191         new = force_to_mode (inner, tmode,
5192                              len >= HOST_BITS_PER_WIDE_INT
5193                              ? GET_MODE_MASK (tmode)
5194                              : ((HOST_WIDE_INT) 1 << len) - 1,
5195                              NULL_RTX, 0);
5196
5197       /* If this extraction is going into the destination of a SET, 
5198          make a STRICT_LOW_PART unless we made a MEM.  */
5199
5200       if (in_dest)
5201         return (GET_CODE (new) == MEM ? new
5202                 : (GET_CODE (new) != SUBREG
5203                    ? gen_rtx (CLOBBER, tmode, const0_rtx)
5204                    : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
5205
5206       /* Otherwise, sign- or zero-extend unless we already are in the
5207          proper mode.  */
5208
5209       return (mode == tmode ? new
5210               : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5211                                  mode, new));
5212     }
5213
5214   /* Unless this is a COMPARE or we have a funny memory reference,
5215      don't do anything with zero-extending field extracts starting at
5216      the low-order bit since they are simple AND operations.  */
5217   if (pos_rtx == 0 && pos == 0 && ! in_dest
5218       && ! in_compare && ! spans_byte && unsignedp)
5219     return 0;
5220
5221   /* Unless we are allowed to span bytes, reject this if we would be
5222      spanning bytes or if the position is not a constant and the length
5223      is not 1.  In all other cases, we would only be going outside
5224      out object in cases when an original shift would have been
5225      undefined.  */
5226   if (! spans_byte
5227       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5228           || (pos_rtx != 0 && len != 1)))
5229     return 0;
5230
5231   /* Get the mode to use should INNER not be a MEM, the mode for the position,
5232      and the mode for the result.  */
5233 #ifdef HAVE_insv
5234   if (in_dest)
5235     {
5236       wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_insv][0];
5237       pos_mode = insn_operand_mode[(int) CODE_FOR_insv][2];
5238       extraction_mode = insn_operand_mode[(int) CODE_FOR_insv][3];
5239     }
5240 #endif
5241
5242 #ifdef HAVE_extzv
5243   if (! in_dest && unsignedp)
5244     {
5245       wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
5246       pos_mode = insn_operand_mode[(int) CODE_FOR_extzv][3];
5247       extraction_mode = insn_operand_mode[(int) CODE_FOR_extzv][0];
5248     }
5249 #endif
5250
5251 #ifdef HAVE_extv
5252   if (! in_dest && ! unsignedp)
5253     {
5254       wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
5255       pos_mode = insn_operand_mode[(int) CODE_FOR_extv][3];
5256       extraction_mode = insn_operand_mode[(int) CODE_FOR_extv][0];
5257     }
5258 #endif
5259
5260   /* Never narrow an object, since that might not be safe.  */
5261
5262   if (mode != VOIDmode
5263       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5264     extraction_mode = mode;
5265
5266   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5267       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5268     pos_mode = GET_MODE (pos_rtx);
5269
5270   /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5271      if we have to change the mode of memory and cannot, the desired mode is
5272      EXTRACTION_MODE.  */
5273   if (GET_CODE (inner) != MEM)
5274     wanted_inner_mode = wanted_inner_reg_mode;
5275   else if (inner_mode != wanted_inner_mode
5276            && (mode_dependent_address_p (XEXP (inner, 0))
5277                || MEM_VOLATILE_P (inner)))
5278     wanted_inner_mode = extraction_mode;
5279
5280   orig_pos = pos;
5281
5282   if (BITS_BIG_ENDIAN)
5283     {
5284       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5285          BITS_BIG_ENDIAN style.  If position is constant, compute new
5286          position.  Otherwise, build subtraction.
5287          Note that POS is relative to the mode of the original argument.
5288          If it's a MEM we need to recompute POS relative to that.
5289          However, if we're extracting from (or inserting into) a register,
5290          we want to recompute POS relative to wanted_inner_mode.  */
5291       int width = (GET_CODE (inner) == MEM
5292                    ? GET_MODE_BITSIZE (is_mode)
5293                    : GET_MODE_BITSIZE (wanted_inner_mode));
5294
5295       if (pos_rtx == 0)
5296         pos = width - len - pos;
5297       else
5298         pos_rtx
5299           = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
5300                              GEN_INT (width - len), pos_rtx);
5301       /* POS may be less than 0 now, but we check for that below.
5302          Note that it can only be less than 0 if GET_CODE (inner) != MEM.  */
5303     }
5304
5305   /* If INNER has a wider mode, make it smaller.  If this is a constant
5306      extract, try to adjust the byte to point to the byte containing
5307      the value.  */
5308   if (wanted_inner_mode != VOIDmode
5309       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
5310       && ((GET_CODE (inner) == MEM
5311            && (inner_mode == wanted_inner_mode
5312                || (! mode_dependent_address_p (XEXP (inner, 0))
5313                    && ! MEM_VOLATILE_P (inner))))))
5314     {
5315       int offset = 0;
5316
5317       /* The computations below will be correct if the machine is big
5318          endian in both bits and bytes or little endian in bits and bytes.
5319          If it is mixed, we must adjust.  */
5320              
5321       /* If bytes are big endian and we had a paradoxical SUBREG, we must
5322          adjust OFFSET to compensate. */
5323       if (BYTES_BIG_ENDIAN
5324           && ! spans_byte
5325           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5326         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
5327
5328       /* If this is a constant position, we can move to the desired byte.  */
5329       if (pos_rtx == 0)
5330         {
5331           offset += pos / BITS_PER_UNIT;
5332           pos %= GET_MODE_BITSIZE (wanted_inner_mode);
5333         }
5334
5335       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5336           && ! spans_byte
5337           && is_mode != wanted_inner_mode)
5338         offset = (GET_MODE_SIZE (is_mode)
5339                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
5340
5341       if (offset != 0 || inner_mode != wanted_inner_mode)
5342         {
5343           rtx newmem = gen_rtx (MEM, wanted_inner_mode,
5344                                 plus_constant (XEXP (inner, 0), offset));
5345           RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
5346           MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (inner);
5347           MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (inner);
5348           inner = newmem;
5349         }
5350     }
5351
5352   /* If INNER is not memory, we can always get it into the proper mode.  If we
5353      are changing its mode, POS must be a constant and smaller than the size
5354      of the new mode.  */
5355   else if (GET_CODE (inner) != MEM)
5356     {
5357       if (GET_MODE (inner) != wanted_inner_mode
5358           && (pos_rtx != 0
5359               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
5360         return 0;
5361
5362       inner = force_to_mode (inner, wanted_inner_mode,
5363                              pos_rtx
5364                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
5365                              ? GET_MODE_MASK (wanted_inner_mode)
5366                              : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
5367                              NULL_RTX, 0);
5368     }
5369
5370   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
5371      have to zero extend.  Otherwise, we can just use a SUBREG.  */
5372   if (pos_rtx != 0
5373       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5374     pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
5375   else if (pos_rtx != 0
5376            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5377     pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5378
5379   /* Make POS_RTX unless we already have it and it is correct.  If we don't
5380      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
5381      be a CONST_INT. */
5382   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5383     pos_rtx = orig_pos_rtx;
5384
5385   else if (pos_rtx == 0)
5386     pos_rtx = GEN_INT (pos);
5387
5388   /* Make the required operation.  See if we can use existing rtx.  */
5389   new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5390                          extraction_mode, inner, GEN_INT (len), pos_rtx);
5391   if (! in_dest)
5392     new = gen_lowpart_for_combine (mode, new);
5393
5394   return new;
5395 }
5396 \f
5397 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
5398    with any other operations in X.  Return X without that shift if so.  */
5399
5400 static rtx
5401 extract_left_shift (x, count)
5402      rtx x;
5403      int count;
5404 {
5405   enum rtx_code code = GET_CODE (x);
5406   enum machine_mode mode = GET_MODE (x);
5407   rtx tem;
5408
5409   switch (code)
5410     {
5411     case ASHIFT:
5412       /* This is the shift itself.  If it is wide enough, we will return
5413          either the value being shifted if the shift count is equal to
5414          COUNT or a shift for the difference.  */
5415       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5416           && INTVAL (XEXP (x, 1)) >= count)
5417         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
5418                                      INTVAL (XEXP (x, 1)) - count);
5419       break;
5420
5421     case NEG:  case NOT:
5422       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5423         return gen_unary (code, mode, mode, tem);
5424
5425       break;
5426
5427     case PLUS:  case IOR:  case XOR:  case AND:
5428       /* If we can safely shift this constant and we find the inner shift,
5429          make a new operation.  */
5430       if (GET_CODE (XEXP (x,1)) == CONST_INT
5431           && (INTVAL (XEXP (x, 1)) & (((HOST_WIDE_INT) 1 << count)) - 1) == 0
5432           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5433         return gen_binary (code, mode, tem, 
5434                            GEN_INT (INTVAL (XEXP (x, 1)) >> count));
5435
5436       break;
5437     }
5438
5439   return 0;
5440 }
5441 \f
5442 /* Look at the expression rooted at X.  Look for expressions
5443    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
5444    Form these expressions.
5445
5446    Return the new rtx, usually just X.
5447
5448    Also, for machines like the Vax that don't have logical shift insns,
5449    try to convert logical to arithmetic shift operations in cases where
5450    they are equivalent.  This undoes the canonicalizations to logical
5451    shifts done elsewhere.
5452
5453    We try, as much as possible, to re-use rtl expressions to save memory.
5454
5455    IN_CODE says what kind of expression we are processing.  Normally, it is
5456    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
5457    being kludges), it is MEM.  When processing the arguments of a comparison
5458    or a COMPARE against zero, it is COMPARE.  */
5459
5460 static rtx
5461 make_compound_operation (x, in_code)
5462      rtx x;
5463      enum rtx_code in_code;
5464 {
5465   enum rtx_code code = GET_CODE (x);
5466   enum machine_mode mode = GET_MODE (x);
5467   int mode_width = GET_MODE_BITSIZE (mode);
5468   rtx rhs, lhs;
5469   enum rtx_code next_code;
5470   int i;
5471   rtx new = 0;
5472   rtx tem;
5473   char *fmt;
5474
5475   /* Select the code to be used in recursive calls.  Once we are inside an
5476      address, we stay there.  If we have a comparison, set to COMPARE,
5477      but once inside, go back to our default of SET.  */
5478
5479   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
5480                : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
5481                   && XEXP (x, 1) == const0_rtx) ? COMPARE
5482                : in_code == COMPARE ? SET : in_code);
5483
5484   /* Process depending on the code of this operation.  If NEW is set
5485      non-zero, it will be returned.  */
5486
5487   switch (code)
5488     {
5489     case ASHIFT:
5490       /* Convert shifts by constants into multiplications if inside
5491          an address.  */
5492       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
5493           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
5494           && INTVAL (XEXP (x, 1)) >= 0)
5495         {
5496           new = make_compound_operation (XEXP (x, 0), next_code);
5497           new = gen_rtx_combine (MULT, mode, new,
5498                                  GEN_INT ((HOST_WIDE_INT) 1
5499                                           << INTVAL (XEXP (x, 1))));
5500         }
5501       break;
5502
5503     case AND:
5504       /* If the second operand is not a constant, we can't do anything
5505          with it.  */
5506       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5507         break;
5508
5509       /* If the constant is a power of two minus one and the first operand
5510          is a logical right shift, make an extraction.  */
5511       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
5512           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5513         {
5514           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5515           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
5516                                  0, in_code == COMPARE);
5517         }
5518
5519       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
5520       else if (GET_CODE (XEXP (x, 0)) == SUBREG
5521                && subreg_lowpart_p (XEXP (x, 0))
5522                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
5523                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5524         {
5525           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
5526                                          next_code);
5527           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
5528                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
5529                                  0, in_code == COMPARE);
5530         }
5531       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
5532       else if ((GET_CODE (XEXP (x, 0)) == XOR
5533                 || GET_CODE (XEXP (x, 0)) == IOR)
5534                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
5535                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
5536                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5537         {
5538           /* Apply the distributive law, and then try to make extractions.  */
5539           new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
5540                                  gen_rtx (AND, mode, XEXP (XEXP (x, 0), 0),
5541                                           XEXP (x, 1)),
5542                                  gen_rtx (AND, mode, XEXP (XEXP (x, 0), 1),
5543                                           XEXP (x, 1)));
5544           new = make_compound_operation (new, in_code);
5545         }
5546
5547       /* If we are have (and (rotate X C) M) and C is larger than the number
5548          of bits in M, this is an extraction.  */
5549
5550       else if (GET_CODE (XEXP (x, 0)) == ROTATE
5551                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5552                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
5553                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
5554         {
5555           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5556           new = make_extraction (mode, new,
5557                                  (GET_MODE_BITSIZE (mode)
5558                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
5559                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
5560         }
5561
5562       /* On machines without logical shifts, if the operand of the AND is
5563          a logical shift and our mask turns off all the propagated sign
5564          bits, we can replace the logical shift with an arithmetic shift.  */
5565       else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5566                && (lshr_optab->handlers[(int) mode].insn_code
5567                    == CODE_FOR_nothing)
5568                && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5569                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5570                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
5571                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
5572                && mode_width <= HOST_BITS_PER_WIDE_INT)
5573         {
5574           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
5575
5576           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
5577           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
5578             SUBST (XEXP (x, 0),
5579                    gen_rtx_combine (ASHIFTRT, mode,
5580                                     make_compound_operation (XEXP (XEXP (x, 0), 0),
5581                                                              next_code),
5582                                     XEXP (XEXP (x, 0), 1)));
5583         }
5584
5585       /* If the constant is one less than a power of two, this might be
5586          representable by an extraction even if no shift is present.
5587          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
5588          we are in a COMPARE.  */
5589       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5590         new = make_extraction (mode,
5591                                make_compound_operation (XEXP (x, 0),
5592                                                         next_code),
5593                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
5594
5595       /* If we are in a comparison and this is an AND with a power of two,
5596          convert this into the appropriate bit extract.  */
5597       else if (in_code == COMPARE
5598                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
5599         new = make_extraction (mode,
5600                                make_compound_operation (XEXP (x, 0),
5601                                                         next_code),
5602                                i, NULL_RTX, 1, 1, 0, 1);
5603
5604       break;
5605
5606     case LSHIFTRT:
5607       /* If the sign bit is known to be zero, replace this with an
5608          arithmetic shift.  */
5609       if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
5610           && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5611           && mode_width <= HOST_BITS_PER_WIDE_INT
5612           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
5613         {
5614           new = gen_rtx_combine (ASHIFTRT, mode,
5615                                  make_compound_operation (XEXP (x, 0),
5616                                                           next_code),
5617                                  XEXP (x, 1));
5618           break;
5619         }
5620
5621       /* ... fall through ... */
5622
5623     case ASHIFTRT:
5624       lhs = XEXP (x, 0);
5625       rhs = XEXP (x, 1);
5626
5627       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
5628          this is a SIGN_EXTRACT.  */
5629       if (GET_CODE (rhs) == CONST_INT
5630           && GET_CODE (lhs) == ASHIFT
5631           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
5632           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
5633         {
5634           new = make_compound_operation (XEXP (lhs, 0), next_code);
5635           new = make_extraction (mode, new,
5636                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
5637                                  NULL_RTX, mode_width - INTVAL (rhs),
5638                                  code == LSHIFTRT, 0, in_code == COMPARE);
5639         }
5640
5641       /* See if we have operations between an ASHIFTRT and an ASHIFT.
5642          If so, try to merge the shifts into a SIGN_EXTEND.  We could
5643          also do this for some cases of SIGN_EXTRACT, but it doesn't
5644          seem worth the effort; the case checked for occurs on Alpha.  */
5645       
5646       if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
5647           && ! (GET_CODE (lhs) == SUBREG
5648                 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
5649           && GET_CODE (rhs) == CONST_INT
5650           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
5651           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
5652         new = make_extraction (mode, make_compound_operation (new, next_code),
5653                                0, NULL_RTX, mode_width - INTVAL (rhs),
5654                                code == LSHIFTRT, 0, in_code == COMPARE);
5655         
5656       break;
5657
5658     case SUBREG:
5659       /* Call ourselves recursively on the inner expression.  If we are
5660          narrowing the object and it has a different RTL code from
5661          what it originally did, do this SUBREG as a force_to_mode.  */
5662
5663       tem = make_compound_operation (SUBREG_REG (x), in_code);
5664       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
5665           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
5666           && subreg_lowpart_p (x))
5667         {
5668           rtx newer = force_to_mode (tem, mode,
5669                                      GET_MODE_MASK (mode), NULL_RTX, 0);
5670
5671           /* If we have something other than a SUBREG, we might have
5672              done an expansion, so rerun outselves.  */
5673           if (GET_CODE (newer) != SUBREG)
5674             newer = make_compound_operation (newer, in_code);
5675
5676           return newer;
5677         }
5678     }
5679
5680   if (new)
5681     {
5682       x = gen_lowpart_for_combine (mode, new);
5683       code = GET_CODE (x);
5684     }
5685
5686   /* Now recursively process each operand of this operation.  */
5687   fmt = GET_RTX_FORMAT (code);
5688   for (i = 0; i < GET_RTX_LENGTH (code); i++)
5689     if (fmt[i] == 'e')
5690       {
5691         new = make_compound_operation (XEXP (x, i), next_code);
5692         SUBST (XEXP (x, i), new);
5693       }
5694
5695   return x;
5696 }
5697 \f
5698 /* Given M see if it is a value that would select a field of bits
5699     within an item, but not the entire word.  Return -1 if not.
5700     Otherwise, return the starting position of the field, where 0 is the
5701     low-order bit.
5702
5703    *PLEN is set to the length of the field.  */
5704
5705 static int
5706 get_pos_from_mask (m, plen)
5707      unsigned HOST_WIDE_INT m;
5708      int *plen;
5709 {
5710   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
5711   int pos = exact_log2 (m & - m);
5712
5713   if (pos < 0)
5714     return -1;
5715
5716   /* Now shift off the low-order zero bits and see if we have a power of
5717      two minus 1.  */
5718   *plen = exact_log2 ((m >> pos) + 1);
5719
5720   if (*plen <= 0)
5721     return -1;
5722
5723   return pos;
5724 }
5725 \f
5726 /* See if X can be simplified knowing that we will only refer to it in
5727    MODE and will only refer to those bits that are nonzero in MASK.
5728    If other bits are being computed or if masking operations are done
5729    that select a superset of the bits in MASK, they can sometimes be
5730    ignored.
5731
5732    Return a possibly simplified expression, but always convert X to
5733    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
5734
5735    Also, if REG is non-zero and X is a register equal in value to REG, 
5736    replace X with REG.
5737
5738    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
5739    are all off in X.  This is used when X will be complemented, by either
5740    NOT, NEG, or XOR.  */
5741
5742 static rtx
5743 force_to_mode (x, mode, mask, reg, just_select)
5744      rtx x;
5745      enum machine_mode mode;
5746      unsigned HOST_WIDE_INT mask;
5747      rtx reg;
5748      int just_select;
5749 {
5750   enum rtx_code code = GET_CODE (x);
5751   int next_select = just_select || code == XOR || code == NOT || code == NEG;
5752   enum machine_mode op_mode;
5753   unsigned HOST_WIDE_INT fuller_mask, nonzero;
5754   rtx op0, op1, temp;
5755
5756   /* If this is a CALL, don't do anything.  Some of the code below
5757      will do the wrong thing since the mode of a CALL is VOIDmode.  */
5758   if (code == CALL)
5759     return x;
5760
5761   /* We want to perform the operation is its present mode unless we know
5762      that the operation is valid in MODE, in which case we do the operation
5763      in MODE.  */
5764   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
5765               && code_to_optab[(int) code] != 0
5766               && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
5767                   != CODE_FOR_nothing))
5768              ? mode : GET_MODE (x));
5769
5770   /* It is not valid to do a right-shift in a narrower mode
5771      than the one it came in with.  */
5772   if ((code == LSHIFTRT || code == ASHIFTRT)
5773       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
5774     op_mode = GET_MODE (x);
5775
5776   /* Truncate MASK to fit OP_MODE.  */
5777   if (op_mode)
5778     mask &= GET_MODE_MASK (op_mode);
5779
5780   /* When we have an arithmetic operation, or a shift whose count we
5781      do not know, we need to assume that all bit the up to the highest-order
5782      bit in MASK will be needed.  This is how we form such a mask.  */
5783   if (op_mode)
5784     fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
5785                    ? GET_MODE_MASK (op_mode)
5786                    : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
5787   else
5788     fuller_mask = ~ (HOST_WIDE_INT) 0;
5789
5790   /* Determine what bits of X are guaranteed to be (non)zero.  */
5791   nonzero = nonzero_bits (x, mode);
5792
5793   /* If none of the bits in X are needed, return a zero.  */
5794   if (! just_select && (nonzero & mask) == 0)
5795     return const0_rtx;
5796
5797   /* If X is a CONST_INT, return a new one.  Do this here since the
5798      test below will fail.  */
5799   if (GET_CODE (x) == CONST_INT)
5800     {
5801       HOST_WIDE_INT cval = INTVAL (x) & mask;
5802       int width = GET_MODE_BITSIZE (mode);
5803
5804       /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
5805          number, sign extend it.  */
5806       if (width > 0 && width < HOST_BITS_PER_WIDE_INT
5807           && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
5808         cval |= (HOST_WIDE_INT) -1 << width;
5809         
5810       return GEN_INT (cval);
5811     }
5812
5813   /* If X is narrower than MODE and we want all the bits in X's mode, just
5814      get X in the proper mode.  */
5815   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
5816       && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
5817     return gen_lowpart_for_combine (mode, x);
5818
5819   /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
5820      MASK are already known to be zero in X, we need not do anything.  */
5821   if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
5822     return x;
5823
5824   switch (code)
5825     {
5826     case CLOBBER:
5827       /* If X is a (clobber (const_int)), return it since we know we are
5828          generating something that won't match. */
5829       return x;
5830
5831     case USE:
5832       /* X is a (use (mem ..)) that was made from a bit-field extraction that
5833          spanned the boundary of the MEM.  If we are now masking so it is
5834          within that boundary, we don't need the USE any more.  */
5835       if (! BITS_BIG_ENDIAN
5836           && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5837         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
5838       break;
5839
5840     case SIGN_EXTEND:
5841     case ZERO_EXTEND:
5842     case ZERO_EXTRACT:
5843     case SIGN_EXTRACT:
5844       x = expand_compound_operation (x);
5845       if (GET_CODE (x) != code)
5846         return force_to_mode (x, mode, mask, reg, next_select);
5847       break;
5848
5849     case REG:
5850       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
5851                        || rtx_equal_p (reg, get_last_value (x))))
5852         x = reg;
5853       break;
5854
5855     case SUBREG:
5856       if (subreg_lowpart_p (x)
5857           /* We can ignore the effect of this SUBREG if it narrows the mode or
5858              if the constant masks to zero all the bits the mode doesn't
5859              have.  */
5860           && ((GET_MODE_SIZE (GET_MODE (x))
5861                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
5862               || (0 == (mask
5863                         & GET_MODE_MASK (GET_MODE (x))
5864                         & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
5865         return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
5866       break;
5867
5868     case AND:
5869       /* If this is an AND with a constant, convert it into an AND
5870          whose constant is the AND of that constant with MASK.  If it
5871          remains an AND of MASK, delete it since it is redundant.  */
5872
5873       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5874         {
5875           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
5876                                       mask & INTVAL (XEXP (x, 1)));
5877
5878           /* If X is still an AND, see if it is an AND with a mask that
5879              is just some low-order bits.  If so, and it is MASK, we don't
5880              need it.  */
5881
5882           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
5883               && INTVAL (XEXP (x, 1)) == mask)
5884             x = XEXP (x, 0);
5885
5886           /* If it remains an AND, try making another AND with the bits
5887              in the mode mask that aren't in MASK turned on.  If the
5888              constant in the AND is wide enough, this might make a
5889              cheaper constant.  */
5890
5891           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
5892               && GET_MODE_MASK (GET_MODE (x)) != mask
5893               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
5894             {
5895               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
5896                                     | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
5897               int width = GET_MODE_BITSIZE (GET_MODE (x));
5898               rtx y;
5899
5900               /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
5901                  number, sign extend it.  */
5902               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
5903                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
5904                 cval |= (HOST_WIDE_INT) -1 << width;
5905
5906               y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
5907               if (rtx_cost (y, SET) < rtx_cost (x, SET))
5908                 x = y;
5909             }
5910
5911           break;
5912         }
5913
5914       goto binop;
5915
5916     case PLUS:
5917       /* In (and (plus FOO C1) M), if M is a mask that just turns off
5918          low-order bits (as in an alignment operation) and FOO is already
5919          aligned to that boundary, mask C1 to that boundary as well.
5920          This may eliminate that PLUS and, later, the AND.  */
5921
5922       {
5923         int width = GET_MODE_BITSIZE (mode);
5924         unsigned HOST_WIDE_INT smask = mask;
5925
5926         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
5927            number, sign extend it.  */
5928
5929         if (width < HOST_BITS_PER_WIDE_INT
5930             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
5931           smask |= (HOST_WIDE_INT) -1 << width;
5932
5933         if (GET_CODE (XEXP (x, 1)) == CONST_INT
5934             && exact_log2 (- smask) >= 0
5935             && (nonzero_bits (XEXP (x, 0), mode) & ~ mask) == 0
5936             && (INTVAL (XEXP (x, 1)) & ~ mask) != 0)
5937           return force_to_mode (plus_constant (XEXP (x, 0),
5938                                                INTVAL (XEXP (x, 1)) & mask),
5939                                 mode, mask, reg, next_select);
5940       }
5941
5942       /* ... fall through ... */
5943
5944     case MINUS:
5945     case MULT:
5946       /* For PLUS, MINUS and MULT, we need any bits less significant than the
5947          most significant bit in MASK since carries from those bits will
5948          affect the bits we are interested in.  */
5949       mask = fuller_mask;
5950       goto binop;
5951
5952     case IOR:
5953     case XOR:
5954       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
5955          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
5956          operation which may be a bitfield extraction.  Ensure that the
5957          constant we form is not wider than the mode of X.  */
5958
5959       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
5960           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5961           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
5962           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
5963           && GET_CODE (XEXP (x, 1)) == CONST_INT
5964           && ((INTVAL (XEXP (XEXP (x, 0), 1))
5965                + floor_log2 (INTVAL (XEXP (x, 1))))
5966               < GET_MODE_BITSIZE (GET_MODE (x)))
5967           && (INTVAL (XEXP (x, 1))
5968               & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
5969         {
5970           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
5971                               << INTVAL (XEXP (XEXP (x, 0), 1)));
5972           temp = gen_binary (GET_CODE (x), GET_MODE (x),
5973                              XEXP (XEXP (x, 0), 0), temp);
5974           x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
5975                           XEXP (XEXP (x, 0), 1));
5976           return force_to_mode (x, mode, mask, reg, next_select);
5977         }
5978
5979     binop:
5980       /* For most binary operations, just propagate into the operation and
5981          change the mode if we have an operation of that mode.   */
5982
5983       op0 = gen_lowpart_for_combine (op_mode,
5984                                      force_to_mode (XEXP (x, 0), mode, mask,
5985                                                     reg, next_select));
5986       op1 = gen_lowpart_for_combine (op_mode,
5987                                      force_to_mode (XEXP (x, 1), mode, mask,
5988                                                     reg, next_select));
5989
5990       /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
5991          MASK since OP1 might have been sign-extended but we never want
5992          to turn on extra bits, since combine might have previously relied
5993          on them being off.  */
5994       if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
5995           && (INTVAL (op1) & mask) != 0)
5996         op1 = GEN_INT (INTVAL (op1) & mask);
5997          
5998       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
5999         x = gen_binary (code, op_mode, op0, op1);
6000       break;
6001
6002     case ASHIFT:
6003       /* For left shifts, do the same, but just for the first operand.
6004          However, we cannot do anything with shifts where we cannot
6005          guarantee that the counts are smaller than the size of the mode
6006          because such a count will have a different meaning in a
6007          wider mode.  */
6008
6009       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6010              && INTVAL (XEXP (x, 1)) >= 0
6011              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6012           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6013                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6014                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6015         break;
6016         
6017       /* If the shift count is a constant and we can do arithmetic in
6018          the mode of the shift, refine which bits we need.  Otherwise, use the
6019          conservative form of the mask.  */
6020       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6021           && INTVAL (XEXP (x, 1)) >= 0
6022           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6023           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6024         mask >>= INTVAL (XEXP (x, 1));
6025       else
6026         mask = fuller_mask;
6027
6028       op0 = gen_lowpart_for_combine (op_mode,
6029                                      force_to_mode (XEXP (x, 0), op_mode,
6030                                                     mask, reg, next_select));
6031
6032       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6033         x =  gen_binary (code, op_mode, op0, XEXP (x, 1));
6034       break;
6035
6036     case LSHIFTRT:
6037       /* Here we can only do something if the shift count is a constant,
6038          this shift constant is valid for the host, and we can do arithmetic
6039          in OP_MODE.  */
6040
6041       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6042           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6043           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6044         {
6045           rtx inner = XEXP (x, 0);
6046
6047           /* Select the mask of the bits we need for the shift operand.  */
6048           mask <<= INTVAL (XEXP (x, 1));
6049
6050           /* We can only change the mode of the shift if we can do arithmetic
6051              in the mode of the shift and MASK is no wider than the width of
6052              OP_MODE.  */
6053           if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6054               || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
6055             op_mode = GET_MODE (x);
6056
6057           inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6058
6059           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6060             x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
6061         }
6062
6063       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6064          shift and AND produces only copies of the sign bit (C2 is one less
6065          than a power of two), we can do this with just a shift.  */
6066
6067       if (GET_CODE (x) == LSHIFTRT
6068           && GET_CODE (XEXP (x, 1)) == CONST_INT
6069           && ((INTVAL (XEXP (x, 1))
6070                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6071               >= GET_MODE_BITSIZE (GET_MODE (x)))
6072           && exact_log2 (mask + 1) >= 0
6073           && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6074               >= exact_log2 (mask + 1)))
6075         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6076                         GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6077                                  - exact_log2 (mask + 1)));
6078       break;
6079
6080     case ASHIFTRT:
6081       /* If we are just looking for the sign bit, we don't need this shift at
6082          all, even if it has a variable count.  */
6083       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6084           && (mask == ((HOST_WIDE_INT) 1
6085                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
6086         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6087
6088       /* If this is a shift by a constant, get a mask that contains those bits
6089          that are not copies of the sign bit.  We then have two cases:  If
6090          MASK only includes those bits, this can be a logical shift, which may
6091          allow simplifications.  If MASK is a single-bit field not within
6092          those bits, we are requesting a copy of the sign bit and hence can
6093          shift the sign bit to the appropriate location.  */
6094
6095       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6096           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6097         {
6098           int i = -1;
6099
6100           /* If the considered data is wider then HOST_WIDE_INT, we can't
6101              represent a mask for all its bits in a single scalar.
6102              But we only care about the lower bits, so calculate these.  */
6103
6104           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
6105             {
6106               nonzero = ~(HOST_WIDE_INT)0;
6107
6108               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6109                  is the number of bits a full-width mask would have set.
6110                  We need only shift if these are fewer than nonzero can
6111                  hold.  If not, we must keep all bits set in nonzero.  */
6112
6113               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6114                   < HOST_BITS_PER_WIDE_INT)
6115                 nonzero >>= INTVAL (XEXP (x, 1))
6116                             + HOST_BITS_PER_WIDE_INT
6117                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
6118             }
6119           else
6120             {
6121               nonzero = GET_MODE_MASK (GET_MODE (x));
6122               nonzero >>= INTVAL (XEXP (x, 1));
6123             }
6124
6125           if ((mask & ~ nonzero) == 0
6126               || (i = exact_log2 (mask)) >= 0)
6127             {
6128               x = simplify_shift_const
6129                 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6130                  i < 0 ? INTVAL (XEXP (x, 1))
6131                  : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6132
6133               if (GET_CODE (x) != ASHIFTRT)
6134                 return force_to_mode (x, mode, mask, reg, next_select);
6135             }
6136         }
6137
6138       /* If MASK is 1, convert this to a LSHIFTRT.  This can be done
6139          even if the shift count isn't a constant.  */
6140       if (mask == 1)
6141         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6142
6143       /* If this is a sign-extension operation that just affects bits
6144          we don't care about, remove it.  Be sure the call above returned
6145          something that is still a shift.  */
6146
6147       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6148           && GET_CODE (XEXP (x, 1)) == CONST_INT
6149           && INTVAL (XEXP (x, 1)) >= 0
6150           && (INTVAL (XEXP (x, 1))
6151               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
6152           && GET_CODE (XEXP (x, 0)) == ASHIFT
6153           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6154           && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
6155         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6156                               reg, next_select);
6157
6158       break;
6159
6160     case ROTATE:
6161     case ROTATERT:
6162       /* If the shift count is constant and we can do computations
6163          in the mode of X, compute where the bits we care about are.
6164          Otherwise, we can't do anything.  Don't change the mode of
6165          the shift or propagate MODE into the shift, though.  */
6166       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6167           && INTVAL (XEXP (x, 1)) >= 0)
6168         {
6169           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6170                                             GET_MODE (x), GEN_INT (mask),
6171                                             XEXP (x, 1));
6172           if (temp && GET_CODE(temp) == CONST_INT)
6173             SUBST (XEXP (x, 0),
6174                    force_to_mode (XEXP (x, 0), GET_MODE (x),
6175                                   INTVAL (temp), reg, next_select));
6176         }
6177       break;
6178         
6179     case NEG:
6180       /* If we just want the low-order bit, the NEG isn't needed since it
6181          won't change the low-order bit.    */
6182       if (mask == 1)
6183         return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6184
6185       /* We need any bits less significant than the most significant bit in
6186          MASK since carries from those bits will affect the bits we are
6187          interested in.  */
6188       mask = fuller_mask;
6189       goto unop;
6190
6191     case NOT:
6192       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6193          same as the XOR case above.  Ensure that the constant we form is not
6194          wider than the mode of X.  */
6195
6196       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6197           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6198           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6199           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6200               < GET_MODE_BITSIZE (GET_MODE (x)))
6201           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6202         {
6203           temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6204           temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6205           x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6206
6207           return force_to_mode (x, mode, mask, reg, next_select);
6208         }
6209
6210       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6211          use the full mask inside the NOT.  */
6212       mask = fuller_mask;
6213
6214     unop:
6215       op0 = gen_lowpart_for_combine (op_mode,
6216                                      force_to_mode (XEXP (x, 0), mode, mask,
6217                                                     reg, next_select));
6218       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6219         x = gen_unary (code, op_mode, op_mode, op0);
6220       break;
6221
6222     case NE:
6223       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
6224          in STORE_FLAG_VALUE and FOO has no bits that might be nonzero not
6225          in CONST.  */
6226       if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 0) == const0_rtx
6227           && (nonzero_bits (XEXP (x, 0), mode) & ~ mask) == 0)
6228         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6229
6230       break;
6231
6232     case IF_THEN_ELSE:
6233       /* We have no way of knowing if the IF_THEN_ELSE can itself be
6234          written in a narrower mode.  We play it safe and do not do so.  */
6235
6236       SUBST (XEXP (x, 1),
6237              gen_lowpart_for_combine (GET_MODE (x),
6238                                       force_to_mode (XEXP (x, 1), mode,
6239                                                      mask, reg, next_select)));
6240       SUBST (XEXP (x, 2),
6241              gen_lowpart_for_combine (GET_MODE (x),
6242                                       force_to_mode (XEXP (x, 2), mode,
6243                                                      mask, reg,next_select)));
6244       break;
6245     }
6246
6247   /* Ensure we return a value of the proper mode.  */
6248   return gen_lowpart_for_combine (mode, x);
6249 }
6250 \f
6251 /* Return nonzero if X is an expression that has one of two values depending on
6252    whether some other value is zero or nonzero.  In that case, we return the
6253    value that is being tested, *PTRUE is set to the value if the rtx being
6254    returned has a nonzero value, and *PFALSE is set to the other alternative.
6255
6256    If we return zero, we set *PTRUE and *PFALSE to X.  */
6257
6258 static rtx
6259 if_then_else_cond (x, ptrue, pfalse)
6260      rtx x;
6261      rtx *ptrue, *pfalse;
6262 {
6263   enum machine_mode mode = GET_MODE (x);
6264   enum rtx_code code = GET_CODE (x);
6265   int size = GET_MODE_BITSIZE (mode);
6266   rtx cond0, cond1, true0, true1, false0, false1;
6267   unsigned HOST_WIDE_INT nz;
6268
6269   /* If this is a unary operation whose operand has one of two values, apply
6270      our opcode to compute those values.  */
6271   if (GET_RTX_CLASS (code) == '1'
6272       && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6273     {
6274       *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6275       *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
6276       return cond0;
6277     }
6278
6279   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
6280      make can't possibly match and would suppress other optimizations.  */
6281   else if (code == COMPARE)
6282     ;
6283
6284   /* If this is a binary operation, see if either side has only one of two
6285      values.  If either one does or if both do and they are conditional on
6286      the same value, compute the new true and false values.  */
6287   else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
6288            || GET_RTX_CLASS (code) == '<')
6289     {
6290       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
6291       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
6292
6293       if ((cond0 != 0 || cond1 != 0)
6294           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
6295         {
6296           *ptrue = gen_binary (code, mode, true0, true1);
6297           *pfalse = gen_binary (code, mode, false0, false1);
6298           return cond0 ? cond0 : cond1;
6299         }
6300
6301 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
6302
6303       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
6304          operands is zero when the other is non-zero, and vice-versa.  */
6305
6306       if ((code == PLUS || code == IOR || code == XOR || code == MINUS
6307            || code == UMAX)
6308           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6309         {
6310           rtx op0 = XEXP (XEXP (x, 0), 1);
6311           rtx op1 = XEXP (XEXP (x, 1), 1);
6312
6313           cond0 = XEXP (XEXP (x, 0), 0);
6314           cond1 = XEXP (XEXP (x, 1), 0);
6315
6316           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6317               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6318               && reversible_comparison_p (cond1)
6319               && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6320                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6321                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6322                   || ((swap_condition (GET_CODE (cond0))
6323                        == reverse_condition (GET_CODE (cond1)))
6324                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6325                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6326               && ! side_effects_p (x))
6327             {
6328               *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
6329               *pfalse = gen_binary (MULT, mode, 
6330                                     (code == MINUS 
6331                                      ? gen_unary (NEG, mode, mode, op1) : op1),
6332                                     const_true_rtx);
6333               return cond0;
6334             }
6335         }
6336
6337       /* Similarly for MULT, AND and UMIN, execpt that for these the result
6338          is always zero.  */
6339       if ((code == MULT || code == AND || code == UMIN)
6340           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6341         {
6342           cond0 = XEXP (XEXP (x, 0), 0);
6343           cond1 = XEXP (XEXP (x, 1), 0);
6344
6345           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6346               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6347               && reversible_comparison_p (cond1)
6348               && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6349                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6350                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6351                   || ((swap_condition (GET_CODE (cond0))
6352                        == reverse_condition (GET_CODE (cond1)))
6353                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6354                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6355               && ! side_effects_p (x))
6356             {
6357               *ptrue = *pfalse = const0_rtx;
6358               return cond0;
6359             }
6360         }
6361 #endif
6362     }
6363
6364   else if (code == IF_THEN_ELSE)
6365     {
6366       /* If we have IF_THEN_ELSE already, extract the condition and
6367          canonicalize it if it is NE or EQ.  */
6368       cond0 = XEXP (x, 0);
6369       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
6370       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
6371         return XEXP (cond0, 0);
6372       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
6373         {
6374           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
6375           return XEXP (cond0, 0);
6376         }
6377       else
6378         return cond0;
6379     }
6380
6381   /* If X is a normal SUBREG with both inner and outer modes integral,
6382      we can narrow both the true and false values of the inner expression,
6383      if there is a condition.  */
6384   else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
6385            && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
6386            && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
6387            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
6388                                                &true0, &false0)))
6389     {
6390       *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6391       *pfalse
6392         = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6393
6394       return cond0;
6395     }
6396
6397   /* If X is a constant, this isn't special and will cause confusions
6398      if we treat it as such.  Likewise if it is equivalent to a constant.  */
6399   else if (CONSTANT_P (x)
6400            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
6401     ;
6402
6403   /* If X is known to be either 0 or -1, those are the true and 
6404      false values when testing X.  */
6405   else if (num_sign_bit_copies (x, mode) == size)
6406     {
6407       *ptrue = constm1_rtx, *pfalse = const0_rtx;
6408       return x;
6409     }
6410
6411   /* Likewise for 0 or a single bit.  */
6412   else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
6413     {
6414       *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
6415       return x;
6416     }
6417
6418   /* Otherwise fail; show no condition with true and false values the same.  */
6419   *ptrue = *pfalse = x;
6420   return 0;
6421 }
6422 \f
6423 /* Return the value of expression X given the fact that condition COND
6424    is known to be true when applied to REG as its first operand and VAL
6425    as its second.  X is known to not be shared and so can be modified in
6426    place.
6427
6428    We only handle the simplest cases, and specifically those cases that
6429    arise with IF_THEN_ELSE expressions.  */
6430
6431 static rtx
6432 known_cond (x, cond, reg, val)
6433      rtx x;
6434      enum rtx_code cond;
6435      rtx reg, val;
6436 {
6437   enum rtx_code code = GET_CODE (x);
6438   rtx temp;
6439   char *fmt;
6440   int i, j;
6441
6442   if (side_effects_p (x))
6443     return x;
6444
6445   if (cond == EQ && rtx_equal_p (x, reg))
6446     return val;
6447
6448   /* If X is (abs REG) and we know something about REG's relationship
6449      with zero, we may be able to simplify this.  */
6450
6451   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
6452     switch (cond)
6453       {
6454       case GE:  case GT:  case EQ:
6455         return XEXP (x, 0);
6456       case LT:  case LE:
6457         return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
6458                           XEXP (x, 0));
6459       }
6460
6461   /* The only other cases we handle are MIN, MAX, and comparisons if the
6462      operands are the same as REG and VAL.  */
6463
6464   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
6465     {
6466       if (rtx_equal_p (XEXP (x, 0), val))
6467         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
6468
6469       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
6470         {
6471           if (GET_RTX_CLASS (code) == '<')
6472             return (comparison_dominates_p (cond, code) ? const_true_rtx
6473                     : (comparison_dominates_p (cond,
6474                                                reverse_condition (code))
6475                        ? const0_rtx : x));
6476
6477           else if (code == SMAX || code == SMIN
6478                    || code == UMIN || code == UMAX)
6479             {
6480               int unsignedp = (code == UMIN || code == UMAX);
6481
6482               if (code == SMAX || code == UMAX)
6483                 cond = reverse_condition (cond);
6484
6485               switch (cond)
6486                 {
6487                 case GE:   case GT:
6488                   return unsignedp ? x : XEXP (x, 1);
6489                 case LE:   case LT:
6490                   return unsignedp ? x : XEXP (x, 0);
6491                 case GEU:  case GTU:
6492                   return unsignedp ? XEXP (x, 1) : x;
6493                 case LEU:  case LTU:
6494                   return unsignedp ? XEXP (x, 0) : x;
6495                 }
6496             }
6497         }
6498     }
6499
6500   fmt = GET_RTX_FORMAT (code);
6501   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6502     {
6503       if (fmt[i] == 'e')
6504         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
6505       else if (fmt[i] == 'E')
6506         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6507           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
6508                                                 cond, reg, val));
6509     }
6510
6511   return x;
6512 }
6513 \f
6514 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
6515    assignment as a field assignment.  */
6516
6517 static int
6518 rtx_equal_for_field_assignment_p (x, y)
6519      rtx x;
6520      rtx y;
6521 {
6522   rtx last_x, last_y;
6523
6524   if (x == y || rtx_equal_p (x, y))
6525     return 1;
6526
6527   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
6528     return 0;
6529
6530   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
6531      Note that all SUBREGs of MEM are paradoxical; otherwise they
6532      would have been rewritten.  */
6533   if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
6534       && GET_CODE (SUBREG_REG (y)) == MEM
6535       && rtx_equal_p (SUBREG_REG (y),
6536                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
6537     return 1;
6538
6539   if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
6540       && GET_CODE (SUBREG_REG (x)) == MEM
6541       && rtx_equal_p (SUBREG_REG (x),
6542                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
6543     return 1;
6544
6545   last_x = get_last_value (x);
6546   last_y = get_last_value (y);
6547
6548   return ((last_x != 0 && rtx_equal_for_field_assignment_p (last_x, y))
6549           || (last_y != 0 && rtx_equal_for_field_assignment_p (x, last_y))
6550           || (last_x != 0 && last_y != 0
6551               && rtx_equal_for_field_assignment_p (last_x, last_y)));
6552 }
6553 \f
6554 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
6555    Return that assignment if so.
6556
6557    We only handle the most common cases.  */
6558
6559 static rtx
6560 make_field_assignment (x)
6561      rtx x;
6562 {
6563   rtx dest = SET_DEST (x);
6564   rtx src = SET_SRC (x);
6565   rtx assign;
6566   rtx rhs, lhs;
6567   HOST_WIDE_INT c1;
6568   int pos, len;
6569   rtx other;
6570   enum machine_mode mode;
6571
6572   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
6573      a clear of a one-bit field.  We will have changed it to
6574      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
6575      for a SUBREG.  */
6576
6577   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
6578       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
6579       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
6580       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
6581     {
6582       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
6583                                 1, 1, 1, 0);
6584       if (assign != 0)
6585         return gen_rtx (SET, VOIDmode, assign, const0_rtx);
6586       return x;
6587     }
6588
6589   else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
6590            && subreg_lowpart_p (XEXP (src, 0))
6591            && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0))) 
6592                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
6593            && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
6594            && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
6595            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
6596     {
6597       assign = make_extraction (VOIDmode, dest, 0,
6598                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
6599                                 1, 1, 1, 0);
6600       if (assign != 0)
6601         return gen_rtx (SET, VOIDmode, assign, const0_rtx);
6602       return x;
6603     }
6604
6605   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
6606      one-bit field.  */
6607   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
6608            && XEXP (XEXP (src, 0), 0) == const1_rtx
6609            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
6610     {
6611       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
6612                                 1, 1, 1, 0);
6613       if (assign != 0)
6614         return gen_rtx (SET, VOIDmode, assign, const1_rtx);
6615       return x;
6616     }
6617
6618   /* The other case we handle is assignments into a constant-position
6619      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
6620      a mask that has all one bits except for a group of zero bits and
6621      OTHER is known to have zeros where C1 has ones, this is such an
6622      assignment.  Compute the position and length from C1.  Shift OTHER
6623      to the appropriate position, force it to the required mode, and
6624      make the extraction.  Check for the AND in both operands.  */
6625
6626   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
6627     return x;
6628
6629   rhs = expand_compound_operation (XEXP (src, 0));
6630   lhs = expand_compound_operation (XEXP (src, 1));
6631
6632   if (GET_CODE (rhs) == AND
6633       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
6634       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
6635     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
6636   else if (GET_CODE (lhs) == AND
6637            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6638            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
6639     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
6640   else
6641     return x;
6642
6643   pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
6644   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
6645       || (GET_MODE_BITSIZE (GET_MODE (other)) <= HOST_BITS_PER_WIDE_INT
6646           && (c1 & nonzero_bits (other, GET_MODE (other))) != 0))
6647     return x;
6648
6649   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
6650   if (assign == 0)
6651     return x;
6652
6653   /* The mode to use for the source is the mode of the assignment, or of
6654      what is inside a possible STRICT_LOW_PART.  */
6655   mode = (GET_CODE (assign) == STRICT_LOW_PART 
6656           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
6657
6658   /* Shift OTHER right POS places and make it the source, restricting it
6659      to the proper length and mode.  */
6660
6661   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
6662                                              GET_MODE (src), other, pos),
6663                        mode,
6664                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
6665                        ? GET_MODE_MASK (mode)
6666                        : ((HOST_WIDE_INT) 1 << len) - 1,
6667                        dest, 0);
6668
6669   return gen_rtx_combine (SET, VOIDmode, assign, src);
6670 }
6671 \f
6672 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
6673    if so.  */
6674
6675 static rtx
6676 apply_distributive_law (x)
6677      rtx x;
6678 {
6679   enum rtx_code code = GET_CODE (x);
6680   rtx lhs, rhs, other;
6681   rtx tem;
6682   enum rtx_code inner_code;
6683
6684   /* Distributivity is not true for floating point.
6685      It can change the value.  So don't do it.
6686      -- rms and moshier@world.std.com.  */
6687   if (FLOAT_MODE_P (GET_MODE (x)))
6688     return x;
6689
6690   /* The outer operation can only be one of the following:  */
6691   if (code != IOR && code != AND && code != XOR
6692       && code != PLUS && code != MINUS)
6693     return x;
6694
6695   lhs = XEXP (x, 0), rhs = XEXP (x, 1);
6696
6697   /* If either operand is a primitive we can't do anything, so get out fast. */
6698   if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
6699       || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
6700     return x;
6701
6702   lhs = expand_compound_operation (lhs);
6703   rhs = expand_compound_operation (rhs);
6704   inner_code = GET_CODE (lhs);
6705   if (inner_code != GET_CODE (rhs))
6706     return x;
6707
6708   /* See if the inner and outer operations distribute.  */
6709   switch (inner_code)
6710     {
6711     case LSHIFTRT:
6712     case ASHIFTRT:
6713     case AND:
6714     case IOR:
6715       /* These all distribute except over PLUS.  */
6716       if (code == PLUS || code == MINUS)
6717         return x;
6718       break;
6719
6720     case MULT:
6721       if (code != PLUS && code != MINUS)
6722         return x;
6723       break;
6724
6725     case ASHIFT:
6726       /* This is also a multiply, so it distributes over everything.  */
6727       break;
6728
6729     case SUBREG:
6730       /* Non-paradoxical SUBREGs distributes over all operations, provided
6731          the inner modes and word numbers are the same, this is an extraction
6732          of a low-order part, we don't convert an fp operation to int or
6733          vice versa, and we would not be converting a single-word
6734          operation into a multi-word operation.  The latter test is not
6735          required, but it prevents generating unneeded multi-word operations.
6736          Some of the previous tests are redundant given the latter test, but
6737          are retained because they are required for correctness.
6738
6739          We produce the result slightly differently in this case.  */
6740
6741       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
6742           || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
6743           || ! subreg_lowpart_p (lhs)
6744           || (GET_MODE_CLASS (GET_MODE (lhs))
6745               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
6746           || (GET_MODE_SIZE (GET_MODE (lhs))
6747               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
6748           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
6749         return x;
6750
6751       tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
6752                         SUBREG_REG (lhs), SUBREG_REG (rhs));
6753       return gen_lowpart_for_combine (GET_MODE (x), tem);
6754
6755     default:
6756       return x;
6757     }
6758
6759   /* Set LHS and RHS to the inner operands (A and B in the example
6760      above) and set OTHER to the common operand (C in the example).
6761      These is only one way to do this unless the inner operation is
6762      commutative.  */
6763   if (GET_RTX_CLASS (inner_code) == 'c'
6764       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
6765     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
6766   else if (GET_RTX_CLASS (inner_code) == 'c'
6767            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
6768     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
6769   else if (GET_RTX_CLASS (inner_code) == 'c'
6770            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
6771     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
6772   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
6773     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
6774   else
6775     return x;
6776
6777   /* Form the new inner operation, seeing if it simplifies first.  */
6778   tem = gen_binary (code, GET_MODE (x), lhs, rhs);
6779
6780   /* There is one exception to the general way of distributing:
6781      (a ^ b) | (a ^ c) -> (~a) & (b ^ c)  */
6782   if (code == XOR && inner_code == IOR)
6783     {
6784       inner_code = AND;
6785       other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
6786     }
6787
6788   /* We may be able to continuing distributing the result, so call
6789      ourselves recursively on the inner operation before forming the
6790      outer operation, which we return.  */
6791   return gen_binary (inner_code, GET_MODE (x),
6792                      apply_distributive_law (tem), other);
6793 }
6794 \f
6795 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
6796    in MODE.
6797
6798    Return an equivalent form, if different from X.  Otherwise, return X.  If
6799    X is zero, we are to always construct the equivalent form.  */
6800
6801 static rtx
6802 simplify_and_const_int (x, mode, varop, constop)
6803      rtx x;
6804      enum machine_mode mode;
6805      rtx varop;
6806      unsigned HOST_WIDE_INT constop;
6807 {
6808   unsigned HOST_WIDE_INT nonzero;
6809   int width = GET_MODE_BITSIZE (mode);
6810   int i;
6811
6812   /* Simplify VAROP knowing that we will be only looking at some of the
6813      bits in it.  */
6814   varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
6815
6816   /* If VAROP is a CLOBBER, we will fail so return it; if it is a
6817      CONST_INT, we are done.  */
6818   if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
6819     return varop;
6820
6821   /* See what bits may be nonzero in VAROP.  Unlike the general case of
6822      a call to nonzero_bits, here we don't care about bits outside
6823      MODE.  */
6824
6825   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
6826
6827   /* If this would be an entire word for the target, but is not for
6828      the host, then sign-extend on the host so that the number will look
6829      the same way on the host that it would on the target.
6830
6831      For example, when building a 64 bit alpha hosted 32 bit sparc
6832      targeted compiler, then we want the 32 bit unsigned value -1 to be
6833      represented as a 64 bit value -1, and not as 0x00000000ffffffff.
6834      The later confuses the sparc backend.  */
6835
6836   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
6837       && (nonzero & ((HOST_WIDE_INT) 1 << (width - 1))))
6838     nonzero |= ((HOST_WIDE_INT) (-1) << width);
6839
6840   /* Turn off all bits in the constant that are known to already be zero.
6841      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
6842      which is tested below.  */
6843
6844   constop &= nonzero;
6845
6846   /* If we don't have any bits left, return zero.  */
6847   if (constop == 0)
6848     return const0_rtx;
6849
6850   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
6851      a power of two, we can replace this with a ASHIFT.  */
6852   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
6853       && (i = exact_log2 (constop)) >= 0)
6854     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
6855                                  
6856   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
6857      or XOR, then try to apply the distributive law.  This may eliminate
6858      operations if either branch can be simplified because of the AND.
6859      It may also make some cases more complex, but those cases probably
6860      won't match a pattern either with or without this.  */
6861
6862   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
6863     return
6864       gen_lowpart_for_combine
6865         (mode,
6866          apply_distributive_law
6867          (gen_binary (GET_CODE (varop), GET_MODE (varop),
6868                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
6869                                               XEXP (varop, 0), constop),
6870                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
6871                                               XEXP (varop, 1), constop))));
6872
6873   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
6874      if we already had one (just check for the simplest cases).  */
6875   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
6876       && GET_MODE (XEXP (x, 0)) == mode
6877       && SUBREG_REG (XEXP (x, 0)) == varop)
6878     varop = XEXP (x, 0);
6879   else
6880     varop = gen_lowpart_for_combine (mode, varop);
6881
6882   /* If we can't make the SUBREG, try to return what we were given. */
6883   if (GET_CODE (varop) == CLOBBER)
6884     return x ? x : varop;
6885
6886   /* If we are only masking insignificant bits, return VAROP.  */
6887   if (constop == nonzero)
6888     x = varop;
6889
6890   /* Otherwise, return an AND.  See how much, if any, of X we can use.  */
6891   else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
6892     x = gen_binary (AND, mode, varop, GEN_INT (constop));
6893
6894   else
6895     {
6896       if (GET_CODE (XEXP (x, 1)) != CONST_INT
6897           || INTVAL (XEXP (x, 1)) != constop)
6898         SUBST (XEXP (x, 1), GEN_INT (constop));
6899
6900       SUBST (XEXP (x, 0), varop);
6901     }
6902
6903   return x;
6904 }
6905 \f
6906 /* Given an expression, X, compute which bits in X can be non-zero.
6907    We don't care about bits outside of those defined in MODE.
6908
6909    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
6910    a shift, AND, or zero_extract, we can do better.  */
6911
6912 static unsigned HOST_WIDE_INT
6913 nonzero_bits (x, mode)
6914      rtx x;
6915      enum machine_mode mode;
6916 {
6917   unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
6918   unsigned HOST_WIDE_INT inner_nz;
6919   enum rtx_code code;
6920   int mode_width = GET_MODE_BITSIZE (mode);
6921   rtx tem;
6922
6923   /* For floating-point values, assume all bits are needed.  */
6924   if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
6925     return nonzero;
6926
6927   /* If X is wider than MODE, use its mode instead.  */
6928   if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
6929     {
6930       mode = GET_MODE (x);
6931       nonzero = GET_MODE_MASK (mode);
6932       mode_width = GET_MODE_BITSIZE (mode);
6933     }
6934
6935   if (mode_width > HOST_BITS_PER_WIDE_INT)
6936     /* Our only callers in this case look for single bit values.  So
6937        just return the mode mask.  Those tests will then be false.  */
6938     return nonzero;
6939
6940 #ifndef WORD_REGISTER_OPERATIONS
6941   /* If MODE is wider than X, but both are a single word for both the host
6942      and target machines, we can compute this from which bits of the 
6943      object might be nonzero in its own mode, taking into account the fact
6944      that on many CISC machines, accessing an object in a wider mode
6945      causes the high-order bits to become undefined.  So they are
6946      not known to be zero.  */
6947
6948   if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
6949       && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
6950       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6951       && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
6952     {
6953       nonzero &= nonzero_bits (x, GET_MODE (x));
6954       nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
6955       return nonzero;
6956     }
6957 #endif
6958
6959   code = GET_CODE (x);
6960   switch (code)
6961     {
6962     case REG:
6963 #ifdef POINTERS_EXTEND_UNSIGNED
6964       /* If pointers extend unsigned and this is a pointer in Pmode, say that
6965          all the bits above ptr_mode are known to be zero.  */
6966       if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
6967           && REGNO_POINTER_FLAG (REGNO (x)))
6968         nonzero &= GET_MODE_MASK (ptr_mode);
6969 #endif
6970
6971 #ifdef STACK_BOUNDARY
6972       /* If this is the stack pointer, we may know something about its
6973          alignment.  If PUSH_ROUNDING is defined, it is possible for the
6974          stack to be momentarily aligned only to that amount, so we pick
6975          the least alignment.  */
6976
6977       /* We can't check for arg_pointer_rtx here, because it is not
6978          guaranteed to have as much alignment as the stack pointer.
6979          In particular, in the Irix6 n64 ABI, the stack has 128 bit
6980          alignment but the argument pointer has only 64 bit alignment.  */
6981
6982       if (x == stack_pointer_rtx || x == frame_pointer_rtx
6983           || x == hard_frame_pointer_rtx
6984           || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
6985               && REGNO (x) <= LAST_VIRTUAL_REGISTER))
6986         {
6987           int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
6988
6989 #ifdef PUSH_ROUNDING
6990           if (REGNO (x) == STACK_POINTER_REGNUM)
6991             sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
6992 #endif
6993
6994           /* We must return here, otherwise we may get a worse result from
6995              one of the choices below.  There is nothing useful below as
6996              far as the stack pointer is concerned.  */
6997           return nonzero &= ~ (sp_alignment - 1);
6998         }
6999 #endif
7000
7001       /* If X is a register whose nonzero bits value is current, use it.
7002          Otherwise, if X is a register whose value we can find, use that
7003          value.  Otherwise, use the previously-computed global nonzero bits
7004          for this register.  */
7005
7006       if (reg_last_set_value[REGNO (x)] != 0
7007           && reg_last_set_mode[REGNO (x)] == mode
7008           && (reg_n_sets[REGNO (x)] == 1
7009               || reg_last_set_label[REGNO (x)] == label_tick)
7010           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7011         return reg_last_set_nonzero_bits[REGNO (x)];
7012
7013       tem = get_last_value (x);
7014
7015       if (tem)
7016         {
7017 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7018           /* If X is narrower than MODE and TEM is a non-negative
7019              constant that would appear negative in the mode of X,
7020              sign-extend it for use in reg_nonzero_bits because some
7021              machines (maybe most) will actually do the sign-extension
7022              and this is the conservative approach. 
7023
7024              ??? For 2.5, try to tighten up the MD files in this regard
7025              instead of this kludge.  */
7026
7027           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7028               && GET_CODE (tem) == CONST_INT
7029               && INTVAL (tem) > 0
7030               && 0 != (INTVAL (tem)
7031                        & ((HOST_WIDE_INT) 1
7032                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7033             tem = GEN_INT (INTVAL (tem)
7034                            | ((HOST_WIDE_INT) (-1)
7035                               << GET_MODE_BITSIZE (GET_MODE (x))));
7036 #endif
7037           return nonzero_bits (tem, mode);
7038         }
7039       else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7040         return reg_nonzero_bits[REGNO (x)] & nonzero;
7041       else
7042         return nonzero;
7043
7044     case CONST_INT:
7045 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7046       /* If X is negative in MODE, sign-extend the value.  */
7047       if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7048           && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7049         return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
7050 #endif
7051
7052       return INTVAL (x);
7053
7054     case MEM:
7055 #ifdef LOAD_EXTEND_OP
7056       /* In many, if not most, RISC machines, reading a byte from memory
7057          zeros the rest of the register.  Noticing that fact saves a lot
7058          of extra zero-extends.  */
7059       if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7060         nonzero &= GET_MODE_MASK (GET_MODE (x));
7061 #endif
7062       break;
7063
7064     case EQ:  case NE:
7065     case GT:  case GTU:
7066     case LT:  case LTU:
7067     case GE:  case GEU:
7068     case LE:  case LEU:
7069
7070       /* If this produces an integer result, we know which bits are set.
7071          Code here used to clear bits outside the mode of X, but that is
7072          now done above.  */
7073
7074       if (GET_MODE_CLASS (mode) == MODE_INT
7075           && mode_width <= HOST_BITS_PER_WIDE_INT)
7076         nonzero = STORE_FLAG_VALUE;
7077       break;
7078
7079     case NEG:
7080       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7081           == GET_MODE_BITSIZE (GET_MODE (x)))
7082         nonzero = 1;
7083
7084       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
7085         nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
7086       break;
7087
7088     case ABS:
7089       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7090           == GET_MODE_BITSIZE (GET_MODE (x)))
7091         nonzero = 1;
7092       break;
7093
7094     case TRUNCATE:
7095       nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
7096       break;
7097
7098     case ZERO_EXTEND:
7099       nonzero &= nonzero_bits (XEXP (x, 0), mode);
7100       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7101         nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7102       break;
7103
7104     case SIGN_EXTEND:
7105       /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7106          Otherwise, show all the bits in the outer mode but not the inner
7107          may be non-zero.  */
7108       inner_nz = nonzero_bits (XEXP (x, 0), mode);
7109       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7110         {
7111           inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7112           if (inner_nz &
7113               (((HOST_WIDE_INT) 1
7114                 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
7115             inner_nz |= (GET_MODE_MASK (mode)
7116                           & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7117         }
7118
7119       nonzero &= inner_nz;
7120       break;
7121
7122     case AND:
7123       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7124                   & nonzero_bits (XEXP (x, 1), mode));
7125       break;
7126
7127     case XOR:   case IOR:
7128     case UMIN:  case UMAX:  case SMIN:  case SMAX:
7129       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7130                   | nonzero_bits (XEXP (x, 1), mode));
7131       break;
7132
7133     case PLUS:  case MINUS:
7134     case MULT:
7135     case DIV:   case UDIV:
7136     case MOD:   case UMOD:
7137       /* We can apply the rules of arithmetic to compute the number of
7138          high- and low-order zero bits of these operations.  We start by
7139          computing the width (position of the highest-order non-zero bit)
7140          and the number of low-order zero bits for each value.  */
7141       {
7142         unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7143         unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7144         int width0 = floor_log2 (nz0) + 1;
7145         int width1 = floor_log2 (nz1) + 1;
7146         int low0 = floor_log2 (nz0 & -nz0);
7147         int low1 = floor_log2 (nz1 & -nz1);
7148         HOST_WIDE_INT op0_maybe_minusp
7149           = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7150         HOST_WIDE_INT op1_maybe_minusp
7151           = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7152         int result_width = mode_width;
7153         int result_low = 0;
7154
7155         switch (code)
7156           {
7157           case PLUS:
7158             result_width = MAX (width0, width1) + 1;
7159             result_low = MIN (low0, low1);
7160             break;
7161           case MINUS:
7162             result_low = MIN (low0, low1);
7163             break;
7164           case MULT:
7165             result_width = width0 + width1;
7166             result_low = low0 + low1;
7167             break;
7168           case DIV:
7169             if (! op0_maybe_minusp && ! op1_maybe_minusp)
7170               result_width = width0;
7171             break;
7172           case UDIV:
7173             result_width = width0;
7174             break;
7175           case MOD:
7176             if (! op0_maybe_minusp && ! op1_maybe_minusp)
7177               result_width = MIN (width0, width1);
7178             result_low = MIN (low0, low1);
7179             break;
7180           case UMOD:
7181             result_width = MIN (width0, width1);
7182             result_low = MIN (low0, low1);
7183             break;
7184           }
7185
7186         if (result_width < mode_width)
7187           nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
7188
7189         if (result_low > 0)
7190           nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
7191       }
7192       break;
7193
7194     case ZERO_EXTRACT:
7195       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7196           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7197         nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
7198       break;
7199
7200     case SUBREG:
7201       /* If this is a SUBREG formed for a promoted variable that has
7202          been zero-extended, we know that at least the high-order bits
7203          are zero, though others might be too.  */
7204
7205       if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
7206         nonzero = (GET_MODE_MASK (GET_MODE (x))
7207                    & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
7208
7209       /* If the inner mode is a single word for both the host and target
7210          machines, we can compute this from which bits of the inner
7211          object might be nonzero.  */
7212       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
7213           && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7214               <= HOST_BITS_PER_WIDE_INT))
7215         {
7216           nonzero &= nonzero_bits (SUBREG_REG (x), mode);
7217
7218 #ifndef WORD_REGISTER_OPERATIONS
7219           /* On many CISC machines, accessing an object in a wider mode
7220              causes the high-order bits to become undefined.  So they are
7221              not known to be zero.  */
7222           if (GET_MODE_SIZE (GET_MODE (x))
7223               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7224             nonzero |= (GET_MODE_MASK (GET_MODE (x))
7225                         & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
7226 #endif
7227         }
7228       break;
7229
7230     case ASHIFTRT:
7231     case LSHIFTRT:
7232     case ASHIFT:
7233     case ROTATE:
7234       /* The nonzero bits are in two classes: any bits within MODE
7235          that aren't in GET_MODE (x) are always significant.  The rest of the
7236          nonzero bits are those that are significant in the operand of
7237          the shift when shifted the appropriate number of bits.  This
7238          shows that high-order bits are cleared by the right shift and
7239          low-order bits by left shifts.  */
7240       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7241           && INTVAL (XEXP (x, 1)) >= 0
7242           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7243         {
7244           enum machine_mode inner_mode = GET_MODE (x);
7245           int width = GET_MODE_BITSIZE (inner_mode);
7246           int count = INTVAL (XEXP (x, 1));
7247           unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
7248           unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
7249           unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
7250           unsigned HOST_WIDE_INT outer = 0;
7251
7252           if (mode_width > width)
7253             outer = (op_nonzero & nonzero & ~ mode_mask);
7254
7255           if (code == LSHIFTRT)
7256             inner >>= count;
7257           else if (code == ASHIFTRT)
7258             {
7259               inner >>= count;
7260
7261               /* If the sign bit may have been nonzero before the shift, we
7262                  need to mark all the places it could have been copied to
7263                  by the shift as possibly nonzero.  */
7264               if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
7265                 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
7266             }
7267           else if (code == ASHIFT)
7268             inner <<= count;
7269           else
7270             inner = ((inner << (count % width)
7271                       | (inner >> (width - (count % width)))) & mode_mask);
7272
7273           nonzero &= (outer | inner);
7274         }
7275       break;
7276
7277     case FFS:
7278       /* This is at most the number of bits in the mode.  */
7279       nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
7280       break;
7281
7282     case IF_THEN_ELSE:
7283       nonzero &= (nonzero_bits (XEXP (x, 1), mode)
7284                   | nonzero_bits (XEXP (x, 2), mode));
7285       break;
7286     }
7287
7288   return nonzero;
7289 }
7290 \f
7291 /* Return the number of bits at the high-order end of X that are known to
7292    be equal to the sign bit.  X will be used in mode MODE; if MODE is
7293    VOIDmode, X will be used in its own mode.  The returned value  will always
7294    be between 1 and the number of bits in MODE.  */
7295
7296 static int
7297 num_sign_bit_copies (x, mode)
7298      rtx x;
7299      enum machine_mode mode;
7300 {
7301   enum rtx_code code = GET_CODE (x);
7302   int bitwidth;
7303   int num0, num1, result;
7304   unsigned HOST_WIDE_INT nonzero;
7305   rtx tem;
7306
7307   /* If we weren't given a mode, use the mode of X.  If the mode is still
7308      VOIDmode, we don't know anything.  Likewise if one of the modes is
7309      floating-point.  */
7310
7311   if (mode == VOIDmode)
7312     mode = GET_MODE (x);
7313
7314   if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
7315     return 1;
7316
7317   bitwidth = GET_MODE_BITSIZE (mode);
7318
7319   /* For a smaller object, just ignore the high bits. */
7320   if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
7321     return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
7322                     - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
7323      
7324 #ifndef WORD_REGISTER_OPERATIONS
7325   /* If this machine does not do all register operations on the entire
7326      register and MODE is wider than the mode of X, we can say nothing
7327      at all about the high-order bits.  */
7328   if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
7329     return 1;
7330 #endif
7331
7332   switch (code)
7333     {
7334     case REG:
7335
7336 #ifdef POINTERS_EXTEND_UNSIGNED
7337       /* If pointers extend signed and this is a pointer in Pmode, say that
7338          all the bits above ptr_mode are known to be sign bit copies.  */
7339       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
7340           && REGNO_POINTER_FLAG (REGNO (x)))
7341         return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
7342 #endif
7343
7344       if (reg_last_set_value[REGNO (x)] != 0
7345           && reg_last_set_mode[REGNO (x)] == mode
7346           && (reg_n_sets[REGNO (x)] == 1
7347               || reg_last_set_label[REGNO (x)] == label_tick)
7348           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7349         return reg_last_set_sign_bit_copies[REGNO (x)];
7350
7351       tem =  get_last_value (x);
7352       if (tem != 0)
7353         return num_sign_bit_copies (tem, mode);
7354
7355       if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
7356         return reg_sign_bit_copies[REGNO (x)];
7357       break;
7358
7359     case MEM:
7360 #ifdef LOAD_EXTEND_OP
7361       /* Some RISC machines sign-extend all loads of smaller than a word.  */
7362       if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
7363         return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
7364 #endif
7365       break;
7366
7367     case CONST_INT:
7368       /* If the constant is negative, take its 1's complement and remask.
7369          Then see how many zero bits we have.  */
7370       nonzero = INTVAL (x) & GET_MODE_MASK (mode);
7371       if (bitwidth <= HOST_BITS_PER_WIDE_INT
7372           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7373         nonzero = (~ nonzero) & GET_MODE_MASK (mode);
7374
7375       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
7376
7377     case SUBREG:
7378       /* If this is a SUBREG for a promoted object that is sign-extended
7379          and we are looking at it in a wider mode, we know that at least the
7380          high-order bits are known to be sign bit copies.  */
7381
7382       if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
7383         return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
7384                     num_sign_bit_copies (SUBREG_REG (x), mode));
7385
7386       /* For a smaller object, just ignore the high bits. */
7387       if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
7388         {
7389           num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
7390           return MAX (1, (num0
7391                           - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7392                              - bitwidth)));
7393         }
7394
7395 #ifdef WORD_REGISTER_OPERATIONS
7396 #ifdef LOAD_EXTEND_OP
7397       /* For paradoxical SUBREGs on machines where all register operations
7398          affect the entire register, just look inside.  Note that we are
7399          passing MODE to the recursive call, so the number of sign bit copies
7400          will remain relative to that mode, not the inner mode.  */
7401
7402       /* This works only if loads sign extend.  Otherwise, if we get a
7403          reload for the inner part, it may be loaded from the stack, and
7404          then we lose all sign bit copies that existed before the store
7405          to the stack.  */
7406
7407       if ((GET_MODE_SIZE (GET_MODE (x))
7408            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7409           && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
7410         return num_sign_bit_copies (SUBREG_REG (x), mode);
7411 #endif
7412 #endif
7413       break;
7414
7415     case SIGN_EXTRACT:
7416       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7417         return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
7418       break;
7419
7420     case SIGN_EXTEND: 
7421       return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7422               + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
7423
7424     case TRUNCATE:
7425       /* For a smaller object, just ignore the high bits. */
7426       num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
7427       return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7428                               - bitwidth)));
7429
7430     case NOT:
7431       return num_sign_bit_copies (XEXP (x, 0), mode);
7432
7433     case ROTATE:       case ROTATERT:
7434       /* If we are rotating left by a number of bits less than the number
7435          of sign bit copies, we can just subtract that amount from the
7436          number.  */
7437       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7438           && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
7439         {
7440           num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7441           return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
7442                                  : bitwidth - INTVAL (XEXP (x, 1))));
7443         }
7444       break;
7445
7446     case NEG:
7447       /* In general, this subtracts one sign bit copy.  But if the value
7448          is known to be positive, the number of sign bit copies is the
7449          same as that of the input.  Finally, if the input has just one bit
7450          that might be nonzero, all the bits are copies of the sign bit.  */
7451       nonzero = nonzero_bits (XEXP (x, 0), mode);
7452       if (nonzero == 1)
7453         return bitwidth;
7454
7455       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7456       if (num0 > 1
7457           && bitwidth <= HOST_BITS_PER_WIDE_INT
7458           && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
7459         num0--;
7460
7461       return num0;
7462
7463     case IOR:   case AND:   case XOR:
7464     case SMIN:  case SMAX:  case UMIN:  case UMAX:
7465       /* Logical operations will preserve the number of sign-bit copies.
7466          MIN and MAX operations always return one of the operands.  */
7467       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7468       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7469       return MIN (num0, num1);
7470
7471     case PLUS:  case MINUS:
7472       /* For addition and subtraction, we can have a 1-bit carry.  However,
7473          if we are subtracting 1 from a positive number, there will not
7474          be such a carry.  Furthermore, if the positive number is known to
7475          be 0 or 1, we know the result is either -1 or 0.  */
7476
7477       if (code == PLUS && XEXP (x, 1) == constm1_rtx
7478           && bitwidth <= HOST_BITS_PER_WIDE_INT)
7479         {
7480           nonzero = nonzero_bits (XEXP (x, 0), mode);
7481           if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
7482             return (nonzero == 1 || nonzero == 0 ? bitwidth
7483                     : bitwidth - floor_log2 (nonzero) - 1);
7484         }
7485
7486       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7487       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7488       return MAX (1, MIN (num0, num1) - 1);
7489       
7490     case MULT:
7491       /* The number of bits of the product is the sum of the number of
7492          bits of both terms.  However, unless one of the terms if known
7493          to be positive, we must allow for an additional bit since negating
7494          a negative number can remove one sign bit copy.  */
7495
7496       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7497       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7498
7499       result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
7500       if (result > 0
7501           && bitwidth <= HOST_BITS_PER_WIDE_INT
7502           && ((nonzero_bits (XEXP (x, 0), mode)
7503                & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7504           && ((nonzero_bits (XEXP (x, 1), mode)
7505               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
7506         result--;
7507
7508       return MAX (1, result);
7509
7510     case UDIV:
7511       /* The result must be <= the first operand.  */
7512       return num_sign_bit_copies (XEXP (x, 0), mode);
7513
7514     case UMOD:
7515       /* The result must be <= the scond operand.  */
7516       return num_sign_bit_copies (XEXP (x, 1), mode);
7517
7518     case DIV:
7519       /* Similar to unsigned division, except that we have to worry about
7520          the case where the divisor is negative, in which case we have
7521          to add 1.  */
7522       result = num_sign_bit_copies (XEXP (x, 0), mode);
7523       if (result > 1
7524           && bitwidth <= HOST_BITS_PER_WIDE_INT
7525           && (nonzero_bits (XEXP (x, 1), mode)
7526               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7527         result --;
7528
7529       return result;
7530
7531     case MOD:
7532       result = num_sign_bit_copies (XEXP (x, 1), mode);
7533       if (result > 1
7534           && bitwidth <= HOST_BITS_PER_WIDE_INT
7535           && (nonzero_bits (XEXP (x, 1), mode)
7536               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7537         result --;
7538
7539       return result;
7540
7541     case ASHIFTRT:
7542       /* Shifts by a constant add to the number of bits equal to the
7543          sign bit.  */
7544       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7545       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7546           && INTVAL (XEXP (x, 1)) > 0)
7547         num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
7548
7549       return num0;
7550
7551     case ASHIFT:
7552       /* Left shifts destroy copies.  */
7553       if (GET_CODE (XEXP (x, 1)) != CONST_INT
7554           || INTVAL (XEXP (x, 1)) < 0
7555           || INTVAL (XEXP (x, 1)) >= bitwidth)
7556         return 1;
7557
7558       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7559       return MAX (1, num0 - INTVAL (XEXP (x, 1)));
7560
7561     case IF_THEN_ELSE:
7562       num0 = num_sign_bit_copies (XEXP (x, 1), mode);
7563       num1 = num_sign_bit_copies (XEXP (x, 2), mode);
7564       return MIN (num0, num1);
7565
7566 #if STORE_FLAG_VALUE == -1
7567     case EQ:  case NE:  case GE:  case GT:  case LE:  case LT:
7568     case GEU: case GTU: case LEU: case LTU:
7569       return bitwidth;
7570 #endif
7571     }
7572
7573   /* If we haven't been able to figure it out by one of the above rules,
7574      see if some of the high-order bits are known to be zero.  If so,
7575      count those bits and return one less than that amount.  If we can't
7576      safely compute the mask for this mode, always return BITWIDTH.  */
7577
7578   if (bitwidth > HOST_BITS_PER_WIDE_INT)
7579     return 1;
7580
7581   nonzero = nonzero_bits (x, mode);
7582   return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
7583           ? 1 : bitwidth - floor_log2 (nonzero) - 1);
7584 }
7585 \f
7586 /* Return the number of "extended" bits there are in X, when interpreted
7587    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
7588    unsigned quantities, this is the number of high-order zero bits.
7589    For signed quantities, this is the number of copies of the sign bit
7590    minus 1.  In both case, this function returns the number of "spare"
7591    bits.  For example, if two quantities for which this function returns
7592    at least 1 are added, the addition is known not to overflow.
7593
7594    This function will always return 0 unless called during combine, which
7595    implies that it must be called from a define_split.  */
7596
7597 int
7598 extended_count (x, mode, unsignedp)
7599      rtx x;
7600      enum machine_mode mode;
7601      int unsignedp;
7602 {
7603   if (nonzero_sign_valid == 0)
7604     return 0;
7605
7606   return (unsignedp
7607           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7608              && (GET_MODE_BITSIZE (mode) - 1
7609                  - floor_log2 (nonzero_bits (x, mode))))
7610           : num_sign_bit_copies (x, mode) - 1);
7611 }
7612 \f
7613 /* This function is called from `simplify_shift_const' to merge two
7614    outer operations.  Specifically, we have already found that we need
7615    to perform operation *POP0 with constant *PCONST0 at the outermost
7616    position.  We would now like to also perform OP1 with constant CONST1
7617    (with *POP0 being done last).
7618
7619    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
7620    the resulting operation.  *PCOMP_P is set to 1 if we would need to 
7621    complement the innermost operand, otherwise it is unchanged.
7622
7623    MODE is the mode in which the operation will be done.  No bits outside
7624    the width of this mode matter.  It is assumed that the width of this mode
7625    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
7626
7627    If *POP0 or OP1 are NIL, it means no operation is required.  Only NEG, PLUS,
7628    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
7629    result is simply *PCONST0.
7630
7631    If the resulting operation cannot be expressed as one operation, we
7632    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
7633
7634 static int
7635 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
7636      enum rtx_code *pop0;
7637      HOST_WIDE_INT *pconst0;
7638      enum rtx_code op1;
7639      HOST_WIDE_INT const1;
7640      enum machine_mode mode;
7641      int *pcomp_p;
7642 {
7643   enum rtx_code op0 = *pop0;
7644   HOST_WIDE_INT const0 = *pconst0;
7645   int width = GET_MODE_BITSIZE (mode);
7646
7647   const0 &= GET_MODE_MASK (mode);
7648   const1 &= GET_MODE_MASK (mode);
7649
7650   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
7651   if (op0 == AND)
7652     const1 &= const0;
7653
7654   /* If OP0 or OP1 is NIL, this is easy.  Similarly if they are the same or
7655      if OP0 is SET.  */
7656
7657   if (op1 == NIL || op0 == SET)
7658     return 1;
7659
7660   else if (op0 == NIL)
7661     op0 = op1, const0 = const1;
7662
7663   else if (op0 == op1)
7664     {
7665       switch (op0)
7666         {
7667         case AND:
7668           const0 &= const1;
7669           break;
7670         case IOR:
7671           const0 |= const1;
7672           break;
7673         case XOR:
7674           const0 ^= const1;
7675           break;
7676         case PLUS:
7677           const0 += const1;
7678           break;
7679         case NEG:
7680           op0 = NIL;
7681           break;
7682         }
7683     }
7684
7685   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
7686   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
7687     return 0;
7688
7689   /* If the two constants aren't the same, we can't do anything.  The
7690      remaining six cases can all be done.  */
7691   else if (const0 != const1)
7692     return 0;
7693
7694   else
7695     switch (op0)
7696       {
7697       case IOR:
7698         if (op1 == AND)
7699           /* (a & b) | b == b */
7700           op0 = SET;
7701         else /* op1 == XOR */
7702           /* (a ^ b) | b == a | b */
7703           ;
7704         break;
7705
7706       case XOR:
7707         if (op1 == AND)
7708           /* (a & b) ^ b == (~a) & b */
7709           op0 = AND, *pcomp_p = 1;
7710         else /* op1 == IOR */
7711           /* (a | b) ^ b == a & ~b */
7712           op0 = AND, *pconst0 = ~ const0;
7713         break;
7714
7715       case AND:
7716         if (op1 == IOR)
7717           /* (a | b) & b == b */
7718         op0 = SET;
7719         else /* op1 == XOR */
7720           /* (a ^ b) & b) == (~a) & b */
7721           *pcomp_p = 1;
7722         break;
7723       }
7724
7725   /* Check for NO-OP cases.  */
7726   const0 &= GET_MODE_MASK (mode);
7727   if (const0 == 0
7728       && (op0 == IOR || op0 == XOR || op0 == PLUS))
7729     op0 = NIL;
7730   else if (const0 == 0 && op0 == AND)
7731     op0 = SET;
7732   else if (const0 == GET_MODE_MASK (mode) && op0 == AND)
7733     op0 = NIL;
7734
7735   /* If this would be an entire word for the target, but is not for
7736      the host, then sign-extend on the host so that the number will look
7737      the same way on the host that it would on the target.
7738
7739      For example, when building a 64 bit alpha hosted 32 bit sparc
7740      targeted compiler, then we want the 32 bit unsigned value -1 to be
7741      represented as a 64 bit value -1, and not as 0x00000000ffffffff.
7742      The later confuses the sparc backend.  */
7743
7744   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
7745       && (const0 & ((HOST_WIDE_INT) 1 << (width - 1))))
7746     const0 |= ((HOST_WIDE_INT) (-1) << width);
7747
7748   *pop0 = op0;
7749   *pconst0 = const0;
7750
7751   return 1;
7752 }
7753 \f
7754 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
7755    The result of the shift is RESULT_MODE.  X, if non-zero, is an expression
7756    that we started with.
7757
7758    The shift is normally computed in the widest mode we find in VAROP, as
7759    long as it isn't a different number of words than RESULT_MODE.  Exceptions
7760    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
7761
7762 static rtx
7763 simplify_shift_const (x, code, result_mode, varop, count)
7764      rtx x;
7765      enum rtx_code code;
7766      enum machine_mode result_mode;
7767      rtx varop;
7768      int count;
7769 {
7770   enum rtx_code orig_code = code;
7771   int orig_count = count;
7772   enum machine_mode mode = result_mode;
7773   enum machine_mode shift_mode, tmode;
7774   int mode_words
7775     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
7776   /* We form (outer_op (code varop count) (outer_const)).  */
7777   enum rtx_code outer_op = NIL;
7778   HOST_WIDE_INT outer_const = 0;
7779   rtx const_rtx;
7780   int complement_p = 0;
7781   rtx new;
7782
7783   /* If we were given an invalid count, don't do anything except exactly
7784      what was requested.  */
7785
7786   if (count < 0 || count > GET_MODE_BITSIZE (mode))
7787     {
7788       if (x)
7789         return x;
7790
7791       return gen_rtx (code, mode, varop, GEN_INT (count));
7792     }
7793
7794   /* Unless one of the branches of the `if' in this loop does a `continue',
7795      we will `break' the loop after the `if'.  */
7796
7797   while (count != 0)
7798     {
7799       /* If we have an operand of (clobber (const_int 0)), just return that
7800          value.  */
7801       if (GET_CODE (varop) == CLOBBER)
7802         return varop;
7803
7804       /* If we discovered we had to complement VAROP, leave.  Making a NOT
7805          here would cause an infinite loop.  */
7806       if (complement_p)
7807         break;
7808
7809       /* Convert ROTATERT to ROTATE.  */
7810       if (code == ROTATERT)
7811         code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
7812
7813       /* We need to determine what mode we will do the shift in.  If the
7814          shift is a right shift or a ROTATE, we must always do it in the mode
7815          it was originally done in.  Otherwise, we can do it in MODE, the
7816          widest mode encountered. */
7817       shift_mode
7818         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
7819            ? result_mode : mode);
7820
7821       /* Handle cases where the count is greater than the size of the mode
7822          minus 1.  For ASHIFT, use the size minus one as the count (this can
7823          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
7824          take the count modulo the size.  For other shifts, the result is
7825          zero.
7826
7827          Since these shifts are being produced by the compiler by combining
7828          multiple operations, each of which are defined, we know what the
7829          result is supposed to be.  */
7830          
7831       if (count > GET_MODE_BITSIZE (shift_mode) - 1)
7832         {
7833           if (code == ASHIFTRT)
7834             count = GET_MODE_BITSIZE (shift_mode) - 1;
7835           else if (code == ROTATE || code == ROTATERT)
7836             count %= GET_MODE_BITSIZE (shift_mode);
7837           else
7838             {
7839               /* We can't simply return zero because there may be an
7840                  outer op.  */
7841               varop = const0_rtx;
7842               count = 0;
7843               break;
7844             }
7845         }
7846
7847       /* Negative counts are invalid and should not have been made (a
7848          programmer-specified negative count should have been handled
7849          above). */
7850       else if (count < 0)
7851         abort ();
7852
7853       /* An arithmetic right shift of a quantity known to be -1 or 0
7854          is a no-op.  */
7855       if (code == ASHIFTRT
7856           && (num_sign_bit_copies (varop, shift_mode)
7857               == GET_MODE_BITSIZE (shift_mode)))
7858         {
7859           count = 0;
7860           break;
7861         }
7862
7863       /* If we are doing an arithmetic right shift and discarding all but
7864          the sign bit copies, this is equivalent to doing a shift by the
7865          bitsize minus one.  Convert it into that shift because it will often
7866          allow other simplifications.  */
7867
7868       if (code == ASHIFTRT
7869           && (count + num_sign_bit_copies (varop, shift_mode)
7870               >= GET_MODE_BITSIZE (shift_mode)))
7871         count = GET_MODE_BITSIZE (shift_mode) - 1;
7872
7873       /* We simplify the tests below and elsewhere by converting
7874          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
7875          `make_compound_operation' will convert it to a ASHIFTRT for
7876          those machines (such as Vax) that don't have a LSHIFTRT.  */
7877       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
7878           && code == ASHIFTRT
7879           && ((nonzero_bits (varop, shift_mode)
7880                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
7881               == 0))
7882         code = LSHIFTRT;
7883
7884       switch (GET_CODE (varop))
7885         {
7886         case SIGN_EXTEND:
7887         case ZERO_EXTEND:
7888         case SIGN_EXTRACT:
7889         case ZERO_EXTRACT:
7890           new = expand_compound_operation (varop);
7891           if (new != varop)
7892             {
7893               varop = new;
7894               continue;
7895             }
7896           break;
7897
7898         case MEM:
7899           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
7900              minus the width of a smaller mode, we can do this with a
7901              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
7902           if ((code == ASHIFTRT || code == LSHIFTRT)
7903               && ! mode_dependent_address_p (XEXP (varop, 0))
7904               && ! MEM_VOLATILE_P (varop)
7905               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
7906                                          MODE_INT, 1)) != BLKmode)
7907             {
7908               if (BYTES_BIG_ENDIAN)
7909                 new = gen_rtx (MEM, tmode, XEXP (varop, 0));
7910               else
7911                 new = gen_rtx (MEM, tmode,
7912                                plus_constant (XEXP (varop, 0),
7913                                               count / BITS_PER_UNIT));
7914               RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
7915               MEM_VOLATILE_P (new) = MEM_VOLATILE_P (varop);
7916               MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (varop);
7917               varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
7918                                        : ZERO_EXTEND, mode, new);
7919               count = 0;
7920               continue;
7921             }
7922           break;
7923
7924         case USE:
7925           /* Similar to the case above, except that we can only do this if
7926              the resulting mode is the same as that of the underlying
7927              MEM and adjust the address depending on the *bits* endianness
7928              because of the way that bit-field extract insns are defined.  */
7929           if ((code == ASHIFTRT || code == LSHIFTRT)
7930               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
7931                                          MODE_INT, 1)) != BLKmode
7932               && tmode == GET_MODE (XEXP (varop, 0)))
7933             {
7934               if (BITS_BIG_ENDIAN)
7935                 new = XEXP (varop, 0);
7936               else
7937                 {
7938                   new = copy_rtx (XEXP (varop, 0));
7939                   SUBST (XEXP (new, 0), 
7940                          plus_constant (XEXP (new, 0),
7941                                         count / BITS_PER_UNIT));
7942                 }
7943
7944               varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
7945                                        : ZERO_EXTEND, mode, new);
7946               count = 0;
7947               continue;
7948             }
7949           break;
7950
7951         case SUBREG:
7952           /* If VAROP is a SUBREG, strip it as long as the inner operand has
7953              the same number of words as what we've seen so far.  Then store
7954              the widest mode in MODE.  */
7955           if (subreg_lowpart_p (varop)
7956               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
7957                   > GET_MODE_SIZE (GET_MODE (varop)))
7958               && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
7959                     + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7960                   == mode_words))
7961             {
7962               varop = SUBREG_REG (varop);
7963               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
7964                 mode = GET_MODE (varop);
7965               continue;
7966             }
7967           break;
7968
7969         case MULT:
7970           /* Some machines use MULT instead of ASHIFT because MULT
7971              is cheaper.  But it is still better on those machines to
7972              merge two shifts into one.  */
7973           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
7974               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
7975             {
7976               varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
7977                                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));;
7978               continue;
7979             }
7980           break;
7981
7982         case UDIV:
7983           /* Similar, for when divides are cheaper.  */
7984           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
7985               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
7986             {
7987               varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
7988                                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
7989               continue;
7990             }
7991           break;
7992
7993         case ASHIFTRT:
7994           /* If we are extracting just the sign bit of an arithmetic right 
7995              shift, that shift is not needed.  */
7996           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
7997             {
7998               varop = XEXP (varop, 0);
7999               continue;
8000             }
8001
8002           /* ... fall through ... */
8003
8004         case LSHIFTRT:
8005         case ASHIFT:
8006         case ROTATE:
8007           /* Here we have two nested shifts.  The result is usually the
8008              AND of a new shift with a mask.  We compute the result below.  */
8009           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8010               && INTVAL (XEXP (varop, 1)) >= 0
8011               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8012               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8013               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8014             {
8015               enum rtx_code first_code = GET_CODE (varop);
8016               int first_count = INTVAL (XEXP (varop, 1));
8017               unsigned HOST_WIDE_INT mask;
8018               rtx mask_rtx;
8019
8020               /* We have one common special case.  We can't do any merging if
8021                  the inner code is an ASHIFTRT of a smaller mode.  However, if
8022                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8023                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8024                  we can convert it to
8025                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8026                  This simplifies certain SIGN_EXTEND operations.  */
8027               if (code == ASHIFT && first_code == ASHIFTRT
8028                   && (GET_MODE_BITSIZE (result_mode)
8029                       - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8030                 {
8031                   /* C3 has the low-order C1 bits zero.  */
8032                   
8033                   mask = (GET_MODE_MASK (mode)
8034                           & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
8035
8036                   varop = simplify_and_const_int (NULL_RTX, result_mode,
8037                                                   XEXP (varop, 0), mask);
8038                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8039                                                 varop, count);
8040                   count = first_count;
8041                   code = ASHIFTRT;
8042                   continue;
8043                 }
8044               
8045               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8046                  than C1 high-order bits equal to the sign bit, we can convert
8047                  this to either an ASHIFT or a ASHIFTRT depending on the
8048                  two counts. 
8049
8050                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
8051
8052               if (code == ASHIFTRT && first_code == ASHIFT
8053                   && GET_MODE (varop) == shift_mode
8054                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8055                       > first_count))
8056                 {
8057                   count -= first_count;
8058                   if (count < 0)
8059                     count = - count, code = ASHIFT;
8060                   varop = XEXP (varop, 0);
8061                   continue;
8062                 }
8063
8064               /* There are some cases we can't do.  If CODE is ASHIFTRT,
8065                  we can only do this if FIRST_CODE is also ASHIFTRT.
8066
8067                  We can't do the case when CODE is ROTATE and FIRST_CODE is
8068                  ASHIFTRT.
8069
8070                  If the mode of this shift is not the mode of the outer shift,
8071                  we can't do this if either shift is a right shift or ROTATE.
8072
8073                  Finally, we can't do any of these if the mode is too wide
8074                  unless the codes are the same.
8075
8076                  Handle the case where the shift codes are the same
8077                  first.  */
8078
8079               if (code == first_code)
8080                 {
8081                   if (GET_MODE (varop) != result_mode
8082                       && (code == ASHIFTRT || code == LSHIFTRT
8083                           || code == ROTATE))
8084                     break;
8085
8086                   count += first_count;
8087                   varop = XEXP (varop, 0);
8088                   continue;
8089                 }
8090
8091               if (code == ASHIFTRT
8092                   || (code == ROTATE && first_code == ASHIFTRT)
8093                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8094                   || (GET_MODE (varop) != result_mode
8095                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
8096                           || first_code == ROTATE
8097                           || code == ROTATE)))
8098                 break;
8099
8100               /* To compute the mask to apply after the shift, shift the
8101                  nonzero bits of the inner shift the same way the 
8102                  outer shift will.  */
8103
8104               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8105
8106               mask_rtx
8107                 = simplify_binary_operation (code, result_mode, mask_rtx,
8108                                              GEN_INT (count));
8109                                   
8110               /* Give up if we can't compute an outer operation to use.  */
8111               if (mask_rtx == 0
8112                   || GET_CODE (mask_rtx) != CONST_INT
8113                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
8114                                         INTVAL (mask_rtx),
8115                                         result_mode, &complement_p))
8116                 break;
8117
8118               /* If the shifts are in the same direction, we add the
8119                  counts.  Otherwise, we subtract them.  */
8120               if ((code == ASHIFTRT || code == LSHIFTRT)
8121                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8122                 count += first_count;
8123               else
8124                 count -= first_count;
8125
8126               /* If COUNT is positive, the new shift is usually CODE, 
8127                  except for the two exceptions below, in which case it is
8128                  FIRST_CODE.  If the count is negative, FIRST_CODE should
8129                  always be used  */
8130               if (count > 0
8131                   && ((first_code == ROTATE && code == ASHIFT)
8132                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
8133                 code = first_code;
8134               else if (count < 0)
8135                 code = first_code, count = - count;
8136
8137               varop = XEXP (varop, 0);
8138               continue;
8139             }
8140
8141           /* If we have (A << B << C) for any shift, we can convert this to
8142              (A << C << B).  This wins if A is a constant.  Only try this if
8143              B is not a constant.  */
8144
8145           else if (GET_CODE (varop) == code
8146                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
8147                    && 0 != (new
8148                             = simplify_binary_operation (code, mode,
8149                                                          XEXP (varop, 0),
8150                                                          GEN_INT (count))))
8151             {
8152               varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8153               count = 0;
8154               continue;
8155             }
8156           break;
8157
8158         case NOT:
8159           /* Make this fit the case below.  */
8160           varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
8161                                    GEN_INT (GET_MODE_MASK (mode)));
8162           continue;
8163
8164         case IOR:
8165         case AND:
8166         case XOR:
8167           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8168              with C the size of VAROP - 1 and the shift is logical if
8169              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8170              we have an (le X 0) operation.   If we have an arithmetic shift
8171              and STORE_FLAG_VALUE is 1 or we have a logical shift with
8172              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
8173
8174           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8175               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8176               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8177               && (code == LSHIFTRT || code == ASHIFTRT)
8178               && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8179               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8180             {
8181               count = 0;
8182               varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8183                                        const0_rtx);
8184
8185               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8186                 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8187
8188               continue;
8189             }
8190
8191           /* If we have (shift (logical)), move the logical to the outside
8192              to allow it to possibly combine with another logical and the
8193              shift to combine with another shift.  This also canonicalizes to
8194              what a ZERO_EXTRACT looks like.  Also, some machines have
8195              (and (shift)) insns.  */
8196
8197           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8198               && (new = simplify_binary_operation (code, result_mode,
8199                                                    XEXP (varop, 1),
8200                                                    GEN_INT (count))) != 0
8201               && GET_CODE(new) == CONST_INT
8202               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8203                                   INTVAL (new), result_mode, &complement_p))
8204             {
8205               varop = XEXP (varop, 0);
8206               continue;
8207             }
8208
8209           /* If we can't do that, try to simplify the shift in each arm of the
8210              logical expression, make a new logical expression, and apply
8211              the inverse distributive law.  */
8212           {
8213             rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8214                                             XEXP (varop, 0), count);
8215             rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8216                                             XEXP (varop, 1), count);
8217
8218             varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
8219             varop = apply_distributive_law (varop);
8220
8221             count = 0;
8222           }
8223           break;
8224
8225         case EQ:
8226           /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8227              says that the sign bit can be tested, FOO has mode MODE, C is
8228              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8229              that may be nonzero.  */
8230           if (code == LSHIFTRT
8231               && XEXP (varop, 1) == const0_rtx
8232               && GET_MODE (XEXP (varop, 0)) == result_mode
8233               && count == GET_MODE_BITSIZE (result_mode) - 1
8234               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8235               && ((STORE_FLAG_VALUE
8236                    & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
8237               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8238               && merge_outer_ops (&outer_op, &outer_const, XOR,
8239                                   (HOST_WIDE_INT) 1, result_mode,
8240                                   &complement_p))
8241             {
8242               varop = XEXP (varop, 0);
8243               count = 0;
8244               continue;
8245             }
8246           break;
8247
8248         case NEG:
8249           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8250              than the number of bits in the mode is equivalent to A.  */
8251           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8252               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
8253             {
8254               varop = XEXP (varop, 0);
8255               count = 0;
8256               continue;
8257             }
8258
8259           /* NEG commutes with ASHIFT since it is multiplication.  Move the
8260              NEG outside to allow shifts to combine.  */
8261           if (code == ASHIFT
8262               && merge_outer_ops (&outer_op, &outer_const, NEG,
8263                                   (HOST_WIDE_INT) 0, result_mode,
8264                                   &complement_p))
8265             {
8266               varop = XEXP (varop, 0);
8267               continue;
8268             }
8269           break;
8270
8271         case PLUS:
8272           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8273              is one less than the number of bits in the mode is
8274              equivalent to (xor A 1).  */
8275           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8276               && XEXP (varop, 1) == constm1_rtx
8277               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8278               && merge_outer_ops (&outer_op, &outer_const, XOR,
8279                                   (HOST_WIDE_INT) 1, result_mode,
8280                                   &complement_p))
8281             {
8282               count = 0;
8283               varop = XEXP (varop, 0);
8284               continue;
8285             }
8286
8287           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
8288              that might be nonzero in BAR are those being shifted out and those
8289              bits are known zero in FOO, we can replace the PLUS with FOO.
8290              Similarly in the other operand order.  This code occurs when
8291              we are computing the size of a variable-size array.  */
8292
8293           if ((code == ASHIFTRT || code == LSHIFTRT)
8294               && count < HOST_BITS_PER_WIDE_INT
8295               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8296               && (nonzero_bits (XEXP (varop, 1), result_mode)
8297                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
8298             {
8299               varop = XEXP (varop, 0);
8300               continue;
8301             }
8302           else if ((code == ASHIFTRT || code == LSHIFTRT)
8303                    && count < HOST_BITS_PER_WIDE_INT
8304                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8305                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8306                             >> count)
8307                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8308                             & nonzero_bits (XEXP (varop, 1),
8309                                                  result_mode)))
8310             {
8311               varop = XEXP (varop, 1);
8312               continue;
8313             }
8314
8315           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
8316           if (code == ASHIFT
8317               && GET_CODE (XEXP (varop, 1)) == CONST_INT
8318               && (new = simplify_binary_operation (ASHIFT, result_mode,
8319                                                    XEXP (varop, 1),
8320                                                    GEN_INT (count))) != 0
8321               && GET_CODE(new) == CONST_INT
8322               && merge_outer_ops (&outer_op, &outer_const, PLUS,
8323                                   INTVAL (new), result_mode, &complement_p))
8324             {
8325               varop = XEXP (varop, 0);
8326               continue;
8327             }
8328           break;
8329
8330         case MINUS:
8331           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
8332              with C the size of VAROP - 1 and the shift is logical if
8333              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8334              we have a (gt X 0) operation.  If the shift is arithmetic with
8335              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
8336              we have a (neg (gt X 0)) operation.  */
8337
8338           if (GET_CODE (XEXP (varop, 0)) == ASHIFTRT
8339               && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8340               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8341               && (code == LSHIFTRT || code == ASHIFTRT)
8342               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8343               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
8344               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8345             {
8346               count = 0;
8347               varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
8348                                        const0_rtx);
8349
8350               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8351                 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8352
8353               continue;
8354             }
8355           break;
8356         }
8357
8358       break;
8359     }
8360
8361   /* We need to determine what mode to do the shift in.  If the shift is
8362      a right shift or ROTATE, we must always do it in the mode it was
8363      originally done in.  Otherwise, we can do it in MODE, the widest mode
8364      encountered.  The code we care about is that of the shift that will
8365      actually be done, not the shift that was originally requested.  */
8366   shift_mode
8367     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8368        ? result_mode : mode);
8369
8370   /* We have now finished analyzing the shift.  The result should be
8371      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
8372      OUTER_OP is non-NIL, it is an operation that needs to be applied
8373      to the result of the shift.  OUTER_CONST is the relevant constant,
8374      but we must turn off all bits turned off in the shift.
8375
8376      If we were passed a value for X, see if we can use any pieces of
8377      it.  If not, make new rtx.  */
8378
8379   if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
8380       && GET_CODE (XEXP (x, 1)) == CONST_INT
8381       && INTVAL (XEXP (x, 1)) == count)
8382     const_rtx = XEXP (x, 1);
8383   else
8384     const_rtx = GEN_INT (count);
8385
8386   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8387       && GET_MODE (XEXP (x, 0)) == shift_mode
8388       && SUBREG_REG (XEXP (x, 0)) == varop)
8389     varop = XEXP (x, 0);
8390   else if (GET_MODE (varop) != shift_mode)
8391     varop = gen_lowpart_for_combine (shift_mode, varop);
8392
8393   /* If we can't make the SUBREG, try to return what we were given. */
8394   if (GET_CODE (varop) == CLOBBER)
8395     return x ? x : varop;
8396
8397   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
8398   if (new != 0)
8399     x = new;
8400   else
8401     {
8402       if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
8403         x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
8404
8405       SUBST (XEXP (x, 0), varop);
8406       SUBST (XEXP (x, 1), const_rtx);
8407     }
8408
8409   /* If we have an outer operation and we just made a shift, it is
8410      possible that we could have simplified the shift were it not
8411      for the outer operation.  So try to do the simplification
8412      recursively.  */
8413
8414   if (outer_op != NIL && GET_CODE (x) == code
8415       && GET_CODE (XEXP (x, 1)) == CONST_INT)
8416     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
8417                               INTVAL (XEXP (x, 1)));
8418
8419   /* If we were doing a LSHIFTRT in a wider mode than it was originally,
8420      turn off all the bits that the shift would have turned off.  */
8421   if (orig_code == LSHIFTRT && result_mode != shift_mode)
8422     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
8423                                 GET_MODE_MASK (result_mode) >> orig_count);
8424       
8425   /* Do the remainder of the processing in RESULT_MODE.  */
8426   x = gen_lowpart_for_combine (result_mode, x);
8427
8428   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
8429      operation.  */
8430   if (complement_p)
8431     x = gen_unary (NOT, result_mode, result_mode, x);
8432
8433   if (outer_op != NIL)
8434     {
8435       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
8436         {
8437           int width = GET_MODE_BITSIZE (result_mode);
8438
8439           outer_const &= GET_MODE_MASK (result_mode);
8440
8441           /* If this would be an entire word for the target, but is not for
8442              the host, then sign-extend on the host so that the number will
8443              look the same way on the host that it would on the target.
8444
8445              For example, when building a 64 bit alpha hosted 32 bit sparc
8446              targeted compiler, then we want the 32 bit unsigned value -1 to be
8447              represented as a 64 bit value -1, and not as 0x00000000ffffffff.
8448              The later confuses the sparc backend.  */
8449
8450           if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
8451               && (outer_const & ((HOST_WIDE_INT) 1 << (width - 1))))
8452             outer_const |= ((HOST_WIDE_INT) (-1) << width);
8453         }
8454
8455       if (outer_op == AND)
8456         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
8457       else if (outer_op == SET)
8458         /* This means that we have determined that the result is
8459            equivalent to a constant.  This should be rare.  */
8460         x = GEN_INT (outer_const);
8461       else if (GET_RTX_CLASS (outer_op) == '1')
8462         x = gen_unary (outer_op, result_mode, result_mode, x);
8463       else
8464         x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
8465     }
8466
8467   return x;
8468 }  
8469 \f
8470 /* Like recog, but we receive the address of a pointer to a new pattern.
8471    We try to match the rtx that the pointer points to.
8472    If that fails, we may try to modify or replace the pattern,
8473    storing the replacement into the same pointer object.
8474
8475    Modifications include deletion or addition of CLOBBERs.
8476
8477    PNOTES is a pointer to a location where any REG_UNUSED notes added for
8478    the CLOBBERs are placed.
8479
8480    PADDED_SCRATCHES is set to the number of (clobber (scratch)) patterns
8481    we had to add.
8482
8483    The value is the final insn code from the pattern ultimately matched,
8484    or -1.  */
8485
8486 static int
8487 recog_for_combine (pnewpat, insn, pnotes, padded_scratches)
8488      rtx *pnewpat;
8489      rtx insn;
8490      rtx *pnotes;
8491      int *padded_scratches;
8492 {
8493   register rtx pat = *pnewpat;
8494   int insn_code_number;
8495   int num_clobbers_to_add = 0;
8496   int i;
8497   rtx notes = 0;
8498
8499   *padded_scratches = 0;
8500
8501   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
8502      we use to indicate that something didn't match.  If we find such a
8503      thing, force rejection.  */
8504   if (GET_CODE (pat) == PARALLEL)
8505     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
8506       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
8507           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
8508         return -1;
8509
8510   /* Is the result of combination a valid instruction?  */
8511   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
8512
8513   /* If it isn't, there is the possibility that we previously had an insn
8514      that clobbered some register as a side effect, but the combined
8515      insn doesn't need to do that.  So try once more without the clobbers
8516      unless this represents an ASM insn.  */
8517
8518   if (insn_code_number < 0 && ! check_asm_operands (pat)
8519       && GET_CODE (pat) == PARALLEL)
8520     {
8521       int pos;
8522
8523       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
8524         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
8525           {
8526             if (i != pos)
8527               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
8528             pos++;
8529           }
8530
8531       SUBST_INT (XVECLEN (pat, 0), pos);
8532
8533       if (pos == 1)
8534         pat = XVECEXP (pat, 0, 0);
8535
8536       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
8537     }
8538
8539   /* If we had any clobbers to add, make a new pattern than contains
8540      them.  Then check to make sure that all of them are dead.  */
8541   if (num_clobbers_to_add)
8542     {
8543       rtx newpat = gen_rtx (PARALLEL, VOIDmode,
8544                             gen_rtvec (GET_CODE (pat) == PARALLEL
8545                                        ? XVECLEN (pat, 0) + num_clobbers_to_add
8546                                        : num_clobbers_to_add + 1));
8547
8548       if (GET_CODE (pat) == PARALLEL)
8549         for (i = 0; i < XVECLEN (pat, 0); i++)
8550           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
8551       else
8552         XVECEXP (newpat, 0, 0) = pat;
8553
8554       add_clobbers (newpat, insn_code_number);
8555
8556       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
8557            i < XVECLEN (newpat, 0); i++)
8558         {
8559           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
8560               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
8561             return -1;
8562           else if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == SCRATCH)
8563             (*padded_scratches)++;
8564           notes = gen_rtx (EXPR_LIST, REG_UNUSED,
8565                            XEXP (XVECEXP (newpat, 0, i), 0), notes);
8566         }
8567       pat = newpat;
8568     }
8569
8570   *pnewpat = pat;
8571   *pnotes = notes;
8572
8573   return insn_code_number;
8574 }
8575 \f
8576 /* Like gen_lowpart but for use by combine.  In combine it is not possible
8577    to create any new pseudoregs.  However, it is safe to create
8578    invalid memory addresses, because combine will try to recognize
8579    them and all they will do is make the combine attempt fail.
8580
8581    If for some reason this cannot do its job, an rtx
8582    (clobber (const_int 0)) is returned.
8583    An insn containing that will not be recognized.  */
8584
8585 #undef gen_lowpart
8586
8587 static rtx
8588 gen_lowpart_for_combine (mode, x)
8589      enum machine_mode mode;
8590      register rtx x;
8591 {
8592   rtx result;
8593
8594   if (GET_MODE (x) == mode)
8595     return x;
8596
8597   /* We can only support MODE being wider than a word if X is a
8598      constant integer or has a mode the same size.  */
8599
8600   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
8601       && ! ((GET_MODE (x) == VOIDmode
8602              && (GET_CODE (x) == CONST_INT
8603                  || GET_CODE (x) == CONST_DOUBLE))
8604             || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
8605     return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
8606
8607   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
8608      won't know what to do.  So we will strip off the SUBREG here and
8609      process normally.  */
8610   if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
8611     {
8612       x = SUBREG_REG (x);
8613       if (GET_MODE (x) == mode)
8614         return x;
8615     }
8616
8617   result = gen_lowpart_common (mode, x);
8618   if (result != 0
8619       && GET_CODE (result) == SUBREG
8620       && GET_CODE (SUBREG_REG (result)) == REG
8621       && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
8622       && (GET_MODE_SIZE (GET_MODE (result))
8623           != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
8624     reg_changes_size[REGNO (SUBREG_REG (result))] = 1;
8625
8626   if (result)
8627     return result;
8628
8629   if (GET_CODE (x) == MEM)
8630     {
8631       register int offset = 0;
8632       rtx new;
8633
8634       /* Refuse to work on a volatile memory ref or one with a mode-dependent
8635          address.  */
8636       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
8637         return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
8638
8639       /* If we want to refer to something bigger than the original memref,
8640          generate a perverse subreg instead.  That will force a reload
8641          of the original memref X.  */
8642       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
8643         return gen_rtx (SUBREG, mode, x, 0);
8644
8645       if (WORDS_BIG_ENDIAN)
8646         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
8647                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
8648       if (BYTES_BIG_ENDIAN)
8649         {
8650           /* Adjust the address so that the address-after-the-data is
8651              unchanged.  */
8652           offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
8653                      - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
8654         }
8655       new = gen_rtx (MEM, mode, plus_constant (XEXP (x, 0), offset));
8656       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
8657       MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
8658       MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
8659       return new;
8660     }
8661
8662   /* If X is a comparison operator, rewrite it in a new mode.  This
8663      probably won't match, but may allow further simplifications.  */
8664   else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
8665     return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
8666
8667   /* If we couldn't simplify X any other way, just enclose it in a
8668      SUBREG.  Normally, this SUBREG won't match, but some patterns may
8669      include an explicit SUBREG or we may simplify it further in combine.  */
8670   else
8671     {
8672       int word = 0;
8673
8674       if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
8675         word = ((GET_MODE_SIZE (GET_MODE (x))
8676                  - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
8677                 / UNITS_PER_WORD);
8678       return gen_rtx (SUBREG, mode, x, word);
8679     }
8680 }
8681 \f
8682 /* Make an rtx expression.  This is a subset of gen_rtx and only supports
8683    expressions of 1, 2, or 3 operands, each of which are rtx expressions.
8684
8685    If the identical expression was previously in the insn (in the undobuf),
8686    it will be returned.  Only if it is not found will a new expression
8687    be made.  */
8688
8689 /*VARARGS2*/
8690 static rtx
8691 gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
8692 {
8693 #ifndef __STDC__
8694   enum rtx_code code;
8695   enum machine_mode mode;
8696 #endif
8697   va_list p;
8698   int n_args;
8699   rtx args[3];
8700   int i, j;
8701   char *fmt;
8702   rtx rt;
8703
8704   VA_START (p, mode);
8705
8706 #ifndef __STDC__
8707   code = va_arg (p, enum rtx_code);
8708   mode = va_arg (p, enum machine_mode);
8709 #endif
8710
8711   n_args = GET_RTX_LENGTH (code);
8712   fmt = GET_RTX_FORMAT (code);
8713
8714   if (n_args == 0 || n_args > 3)
8715     abort ();
8716
8717   /* Get each arg and verify that it is supposed to be an expression.  */
8718   for (j = 0; j < n_args; j++)
8719     {
8720       if (*fmt++ != 'e')
8721         abort ();
8722
8723       args[j] = va_arg (p, rtx);
8724     }
8725
8726   /* See if this is in undobuf.  Be sure we don't use objects that came
8727      from another insn; this could produce circular rtl structures.  */
8728
8729   for (i = previous_num_undos; i < undobuf.num_undo; i++)
8730     if (!undobuf.undo[i].is_int
8731         && GET_CODE (undobuf.undo[i].old_contents.r) == code
8732         && GET_MODE (undobuf.undo[i].old_contents.r) == mode)
8733       {
8734         for (j = 0; j < n_args; j++)
8735           if (XEXP (undobuf.undo[i].old_contents.r, j) != args[j])
8736             break;
8737
8738         if (j == n_args)
8739           return undobuf.undo[i].old_contents.r;
8740       }
8741
8742   /* Otherwise make a new rtx.  We know we have 1, 2, or 3 args.
8743      Use rtx_alloc instead of gen_rtx because it's faster on RISC.  */
8744   rt = rtx_alloc (code);
8745   PUT_MODE (rt, mode);
8746   XEXP (rt, 0) = args[0];
8747   if (n_args > 1)
8748     {
8749       XEXP (rt, 1) = args[1];
8750       if (n_args > 2)
8751         XEXP (rt, 2) = args[2];
8752     }
8753   return rt;
8754 }
8755
8756 /* These routines make binary and unary operations by first seeing if they
8757    fold; if not, a new expression is allocated.  */
8758
8759 static rtx
8760 gen_binary (code, mode, op0, op1)
8761      enum rtx_code code;
8762      enum machine_mode mode;
8763      rtx op0, op1;
8764 {
8765   rtx result;
8766   rtx tem;
8767
8768   if (GET_RTX_CLASS (code) == 'c'
8769       && (GET_CODE (op0) == CONST_INT
8770           || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
8771     tem = op0, op0 = op1, op1 = tem;
8772
8773   if (GET_RTX_CLASS (code) == '<') 
8774     {
8775       enum machine_mode op_mode = GET_MODE (op0);
8776
8777       /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get 
8778          just (REL_OP X Y). */
8779       if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
8780         {
8781           op1 = XEXP (op0, 1);
8782           op0 = XEXP (op0, 0);
8783           op_mode = GET_MODE (op0);
8784         }
8785
8786       if (op_mode == VOIDmode)
8787         op_mode = GET_MODE (op1);
8788       result = simplify_relational_operation (code, op_mode, op0, op1);
8789     }
8790   else
8791     result = simplify_binary_operation (code, mode, op0, op1);
8792
8793   if (result)
8794     return result;
8795
8796   /* Put complex operands first and constants second.  */
8797   if (GET_RTX_CLASS (code) == 'c'
8798       && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
8799           || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
8800               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
8801           || (GET_CODE (op0) == SUBREG
8802               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
8803               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
8804     return gen_rtx_combine (code, mode, op1, op0);
8805
8806   return gen_rtx_combine (code, mode, op0, op1);
8807 }
8808
8809 static rtx
8810 gen_unary (code, mode, op0_mode, op0)
8811      enum rtx_code code;
8812      enum machine_mode mode, op0_mode;
8813      rtx op0;
8814 {
8815   rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
8816
8817   if (result)
8818     return result;
8819
8820   return gen_rtx_combine (code, mode, op0);
8821 }
8822 \f
8823 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
8824    comparison code that will be tested.
8825
8826    The result is a possibly different comparison code to use.  *POP0 and
8827    *POP1 may be updated.
8828
8829    It is possible that we might detect that a comparison is either always
8830    true or always false.  However, we do not perform general constant
8831    folding in combine, so this knowledge isn't useful.  Such tautologies
8832    should have been detected earlier.  Hence we ignore all such cases.  */
8833
8834 static enum rtx_code
8835 simplify_comparison (code, pop0, pop1)
8836      enum rtx_code code;
8837      rtx *pop0;
8838      rtx *pop1;
8839 {
8840   rtx op0 = *pop0;
8841   rtx op1 = *pop1;
8842   rtx tem, tem1;
8843   int i;
8844   enum machine_mode mode, tmode;
8845
8846   /* Try a few ways of applying the same transformation to both operands.  */
8847   while (1)
8848     {
8849 #ifndef WORD_REGISTER_OPERATIONS
8850       /* The test below this one won't handle SIGN_EXTENDs on these machines,
8851          so check specially.  */
8852       if (code != GTU && code != GEU && code != LTU && code != LEU
8853           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
8854           && GET_CODE (XEXP (op0, 0)) == ASHIFT
8855           && GET_CODE (XEXP (op1, 0)) == ASHIFT
8856           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
8857           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
8858           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
8859               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
8860           && GET_CODE (XEXP (op0, 1)) == CONST_INT
8861           && GET_CODE (XEXP (op1, 1)) == CONST_INT
8862           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
8863           && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
8864           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
8865           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
8866           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
8867           && (INTVAL (XEXP (op0, 1))
8868               == (GET_MODE_BITSIZE (GET_MODE (op0))
8869                   - (GET_MODE_BITSIZE
8870                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
8871         {
8872           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
8873           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
8874         }
8875 #endif
8876
8877       /* If both operands are the same constant shift, see if we can ignore the
8878          shift.  We can if the shift is a rotate or if the bits shifted out of
8879          this shift are known to be zero for both inputs and if the type of
8880          comparison is compatible with the shift.  */
8881       if (GET_CODE (op0) == GET_CODE (op1)
8882           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
8883           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
8884               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
8885                   && (code != GT && code != LT && code != GE && code != LE))
8886               || (GET_CODE (op0) == ASHIFTRT
8887                   && (code != GTU && code != LTU
8888                       && code != GEU && code != GEU)))
8889           && GET_CODE (XEXP (op0, 1)) == CONST_INT
8890           && INTVAL (XEXP (op0, 1)) >= 0
8891           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
8892           && XEXP (op0, 1) == XEXP (op1, 1))
8893         {
8894           enum machine_mode mode = GET_MODE (op0);
8895           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8896           int shift_count = INTVAL (XEXP (op0, 1));
8897
8898           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
8899             mask &= (mask >> shift_count) << shift_count;
8900           else if (GET_CODE (op0) == ASHIFT)
8901             mask = (mask & (mask << shift_count)) >> shift_count;
8902
8903           if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
8904               && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
8905             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
8906           else
8907             break;
8908         }
8909
8910       /* If both operands are AND's of a paradoxical SUBREG by constant, the
8911          SUBREGs are of the same mode, and, in both cases, the AND would
8912          be redundant if the comparison was done in the narrower mode,
8913          do the comparison in the narrower mode (e.g., we are AND'ing with 1
8914          and the operand's possibly nonzero bits are 0xffffff01; in that case
8915          if we only care about QImode, we don't need the AND).  This case
8916          occurs if the output mode of an scc insn is not SImode and
8917          STORE_FLAG_VALUE == 1 (e.g., the 386).
8918
8919          Similarly, check for a case where the AND's are ZERO_EXTEND
8920          operations from some narrower mode even though a SUBREG is not
8921          present.  */
8922
8923       else if  (GET_CODE (op0) == AND && GET_CODE (op1) == AND
8924                 && GET_CODE (XEXP (op0, 1)) == CONST_INT
8925                 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
8926         {
8927           rtx inner_op0 = XEXP (op0, 0);
8928           rtx inner_op1 = XEXP (op1, 0);
8929           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
8930           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
8931           int changed = 0;
8932                 
8933           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
8934               && (GET_MODE_SIZE (GET_MODE (inner_op0))
8935                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
8936               && (GET_MODE (SUBREG_REG (inner_op0))
8937                   == GET_MODE (SUBREG_REG (inner_op1)))
8938               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
8939                   <= HOST_BITS_PER_WIDE_INT)
8940               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
8941                                              GET_MODE (SUBREG_REG (op0)))))
8942               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
8943                                              GET_MODE (SUBREG_REG (inner_op1))))))
8944             {
8945               op0 = SUBREG_REG (inner_op0);
8946               op1 = SUBREG_REG (inner_op1);
8947
8948               /* The resulting comparison is always unsigned since we masked
8949                  off the original sign bit. */
8950               code = unsigned_condition (code);
8951
8952               changed = 1;
8953             }
8954
8955           else if (c0 == c1)
8956             for (tmode = GET_CLASS_NARROWEST_MODE
8957                  (GET_MODE_CLASS (GET_MODE (op0)));
8958                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
8959               if (c0 == GET_MODE_MASK (tmode))
8960                 {
8961                   op0 = gen_lowpart_for_combine (tmode, inner_op0);
8962                   op1 = gen_lowpart_for_combine (tmode, inner_op1);
8963                   code = unsigned_condition (code);
8964                   changed = 1;
8965                   break;
8966                 }
8967
8968           if (! changed)
8969             break;
8970         }
8971
8972       /* If both operands are NOT, we can strip off the outer operation
8973          and adjust the comparison code for swapped operands; similarly for
8974          NEG, except that this must be an equality comparison.  */
8975       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
8976                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
8977                    && (code == EQ || code == NE)))
8978         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
8979
8980       else
8981         break;
8982     }
8983      
8984   /* If the first operand is a constant, swap the operands and adjust the
8985      comparison code appropriately.  */
8986   if (CONSTANT_P (op0))
8987     {
8988       tem = op0, op0 = op1, op1 = tem;
8989       code = swap_condition (code);
8990     }
8991
8992   /* We now enter a loop during which we will try to simplify the comparison.
8993      For the most part, we only are concerned with comparisons with zero,
8994      but some things may really be comparisons with zero but not start
8995      out looking that way.  */
8996
8997   while (GET_CODE (op1) == CONST_INT)
8998     {
8999       enum machine_mode mode = GET_MODE (op0);
9000       int mode_width = GET_MODE_BITSIZE (mode);
9001       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9002       int equality_comparison_p;
9003       int sign_bit_comparison_p;
9004       int unsigned_comparison_p;
9005       HOST_WIDE_INT const_op;
9006
9007       /* We only want to handle integral modes.  This catches VOIDmode,
9008          CCmode, and the floating-point modes.  An exception is that we
9009          can handle VOIDmode if OP0 is a COMPARE or a comparison
9010          operation.  */
9011
9012       if (GET_MODE_CLASS (mode) != MODE_INT
9013           && ! (mode == VOIDmode
9014                 && (GET_CODE (op0) == COMPARE
9015                     || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9016         break;
9017
9018       /* Get the constant we are comparing against and turn off all bits
9019          not on in our mode.  */
9020       const_op = INTVAL (op1);
9021       if (mode_width <= HOST_BITS_PER_WIDE_INT)
9022         const_op &= mask;
9023
9024       /* If we are comparing against a constant power of two and the value
9025          being compared can only have that single bit nonzero (e.g., it was
9026          `and'ed with that bit), we can replace this with a comparison
9027          with zero.  */
9028       if (const_op
9029           && (code == EQ || code == NE || code == GE || code == GEU
9030               || code == LT || code == LTU)
9031           && mode_width <= HOST_BITS_PER_WIDE_INT
9032           && exact_log2 (const_op) >= 0
9033           && nonzero_bits (op0, mode) == const_op)
9034         {
9035           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9036           op1 = const0_rtx, const_op = 0;
9037         }
9038
9039       /* Similarly, if we are comparing a value known to be either -1 or
9040          0 with -1, change it to the opposite comparison against zero.  */
9041
9042       if (const_op == -1
9043           && (code == EQ || code == NE || code == GT || code == LE
9044               || code == GEU || code == LTU)
9045           && num_sign_bit_copies (op0, mode) == mode_width)
9046         {
9047           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9048           op1 = const0_rtx, const_op = 0;
9049         }
9050
9051       /* Do some canonicalizations based on the comparison code.  We prefer
9052          comparisons against zero and then prefer equality comparisons.  
9053          If we can reduce the size of a constant, we will do that too.  */
9054
9055       switch (code)
9056         {
9057         case LT:
9058           /* < C is equivalent to <= (C - 1) */
9059           if (const_op > 0)
9060             {
9061               const_op -= 1;
9062               op1 = GEN_INT (const_op);
9063               code = LE;
9064               /* ... fall through to LE case below.  */
9065             }
9066           else
9067             break;
9068
9069         case LE:
9070           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
9071           if (const_op < 0)
9072             {
9073               const_op += 1;
9074               op1 = GEN_INT (const_op);
9075               code = LT;
9076             }
9077
9078           /* If we are doing a <= 0 comparison on a value known to have
9079              a zero sign bit, we can replace this with == 0.  */
9080           else if (const_op == 0
9081                    && mode_width <= HOST_BITS_PER_WIDE_INT
9082                    && (nonzero_bits (op0, mode)
9083                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9084             code = EQ;
9085           break;
9086
9087         case GE:
9088           /* >= C is equivalent to > (C - 1). */
9089           if (const_op > 0)
9090             {
9091               const_op -= 1;
9092               op1 = GEN_INT (const_op);
9093               code = GT;
9094               /* ... fall through to GT below.  */
9095             }
9096           else
9097             break;
9098
9099         case GT:
9100           /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9101           if (const_op < 0)
9102             {
9103               const_op += 1;
9104               op1 = GEN_INT (const_op);
9105               code = GE;
9106             }
9107
9108           /* If we are doing a > 0 comparison on a value known to have
9109              a zero sign bit, we can replace this with != 0.  */
9110           else if (const_op == 0
9111                    && mode_width <= HOST_BITS_PER_WIDE_INT
9112                    && (nonzero_bits (op0, mode)
9113                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9114             code = NE;
9115           break;
9116
9117         case LTU:
9118           /* < C is equivalent to <= (C - 1).  */
9119           if (const_op > 0)
9120             {
9121               const_op -= 1;
9122               op1 = GEN_INT (const_op);
9123               code = LEU;
9124               /* ... fall through ... */
9125             }
9126
9127           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
9128           else if (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1))
9129             {
9130               const_op = 0, op1 = const0_rtx;
9131               code = GE;
9132               break;
9133             }
9134           else
9135             break;
9136
9137         case LEU:
9138           /* unsigned <= 0 is equivalent to == 0 */
9139           if (const_op == 0)
9140             code = EQ;
9141
9142           /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9143           else if (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
9144             {
9145               const_op = 0, op1 = const0_rtx;
9146               code = GE;
9147             }
9148           break;
9149
9150         case GEU:
9151           /* >= C is equivalent to < (C - 1).  */
9152           if (const_op > 1)
9153             {
9154               const_op -= 1;
9155               op1 = GEN_INT (const_op);
9156               code = GTU;
9157               /* ... fall through ... */
9158             }
9159
9160           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
9161           else if (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1))
9162             {
9163               const_op = 0, op1 = const0_rtx;
9164               code = LT;
9165               break;
9166             }
9167           else
9168             break;
9169
9170         case GTU:
9171           /* unsigned > 0 is equivalent to != 0 */
9172           if (const_op == 0)
9173             code = NE;
9174
9175           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
9176           else if (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
9177             {
9178               const_op = 0, op1 = const0_rtx;
9179               code = LT;
9180             }
9181           break;
9182         }
9183
9184       /* Compute some predicates to simplify code below.  */
9185
9186       equality_comparison_p = (code == EQ || code == NE);
9187       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9188       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9189                                || code == LEU);
9190
9191       /* If this is a sign bit comparison and we can do arithmetic in
9192          MODE, say that we will only be needing the sign bit of OP0.  */
9193       if (sign_bit_comparison_p
9194           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9195         op0 = force_to_mode (op0, mode,
9196                              ((HOST_WIDE_INT) 1
9197                               << (GET_MODE_BITSIZE (mode) - 1)),
9198                              NULL_RTX, 0);
9199
9200       /* Now try cases based on the opcode of OP0.  If none of the cases
9201          does a "continue", we exit this loop immediately after the
9202          switch.  */
9203
9204       switch (GET_CODE (op0))
9205         {
9206         case ZERO_EXTRACT:
9207           /* If we are extracting a single bit from a variable position in
9208              a constant that has only a single bit set and are comparing it
9209              with zero, we can convert this into an equality comparison 
9210              between the position and the location of the single bit.  */
9211
9212           if (GET_CODE (XEXP (op0, 0)) == CONST_INT
9213               && XEXP (op0, 1) == const1_rtx
9214               && equality_comparison_p && const_op == 0
9215               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9216             {
9217               if (BITS_BIG_ENDIAN)
9218 #ifdef HAVE_extzv
9219                 i = (GET_MODE_BITSIZE
9220                      (insn_operand_mode[(int) CODE_FOR_extzv][1]) - 1 - i);
9221 #else
9222                 i = BITS_PER_WORD - 1 - i;
9223 #endif
9224
9225               op0 = XEXP (op0, 2);
9226               op1 = GEN_INT (i);
9227               const_op = i;
9228
9229               /* Result is nonzero iff shift count is equal to I.  */
9230               code = reverse_condition (code);
9231               continue;
9232             }
9233
9234           /* ... fall through ... */
9235
9236         case SIGN_EXTRACT:
9237           tem = expand_compound_operation (op0);
9238           if (tem != op0)
9239             {
9240               op0 = tem;
9241               continue;
9242             }
9243           break;
9244
9245         case NOT:
9246           /* If testing for equality, we can take the NOT of the constant.  */
9247           if (equality_comparison_p
9248               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9249             {
9250               op0 = XEXP (op0, 0);
9251               op1 = tem;
9252               continue;
9253             }
9254
9255           /* If just looking at the sign bit, reverse the sense of the
9256              comparison.  */
9257           if (sign_bit_comparison_p)
9258             {
9259               op0 = XEXP (op0, 0);
9260               code = (code == GE ? LT : GE);
9261               continue;
9262             }
9263           break;
9264
9265         case NEG:
9266           /* If testing for equality, we can take the NEG of the constant.  */
9267           if (equality_comparison_p
9268               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9269             {
9270               op0 = XEXP (op0, 0);
9271               op1 = tem;
9272               continue;
9273             }
9274
9275           /* The remaining cases only apply to comparisons with zero.  */
9276           if (const_op != 0)
9277             break;
9278
9279           /* When X is ABS or is known positive,
9280              (neg X) is < 0 if and only if X != 0.  */
9281
9282           if (sign_bit_comparison_p
9283               && (GET_CODE (XEXP (op0, 0)) == ABS
9284                   || (mode_width <= HOST_BITS_PER_WIDE_INT
9285                       && (nonzero_bits (XEXP (op0, 0), mode)
9286                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
9287             {
9288               op0 = XEXP (op0, 0);
9289               code = (code == LT ? NE : EQ);
9290               continue;
9291             }
9292
9293           /* If we have NEG of something whose two high-order bits are the
9294              same, we know that "(-a) < 0" is equivalent to "a > 0". */
9295           if (num_sign_bit_copies (op0, mode) >= 2)
9296             {
9297               op0 = XEXP (op0, 0);
9298               code = swap_condition (code);
9299               continue;
9300             }
9301           break;
9302
9303         case ROTATE:
9304           /* If we are testing equality and our count is a constant, we
9305              can perform the inverse operation on our RHS.  */
9306           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9307               && (tem = simplify_binary_operation (ROTATERT, mode,
9308                                                    op1, XEXP (op0, 1))) != 0)
9309             {
9310               op0 = XEXP (op0, 0);
9311               op1 = tem;
9312               continue;
9313             }
9314
9315           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
9316              a particular bit.  Convert it to an AND of a constant of that
9317              bit.  This will be converted into a ZERO_EXTRACT.  */
9318           if (const_op == 0 && sign_bit_comparison_p
9319               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9320               && mode_width <= HOST_BITS_PER_WIDE_INT)
9321             {
9322               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9323                                             ((HOST_WIDE_INT) 1
9324                                              << (mode_width - 1
9325                                                  - INTVAL (XEXP (op0, 1)))));
9326               code = (code == LT ? NE : EQ);
9327               continue;
9328             }
9329
9330           /* ... fall through ... */
9331
9332         case ABS:
9333           /* ABS is ignorable inside an equality comparison with zero.  */
9334           if (const_op == 0 && equality_comparison_p)
9335             {
9336               op0 = XEXP (op0, 0);
9337               continue;
9338             }
9339           break;
9340           
9341
9342         case SIGN_EXTEND:
9343           /* Can simplify (compare (zero/sign_extend FOO) CONST)
9344              to (compare FOO CONST) if CONST fits in FOO's mode and we 
9345              are either testing inequality or have an unsigned comparison
9346              with ZERO_EXTEND or a signed comparison with SIGN_EXTEND.  */
9347           if (! unsigned_comparison_p
9348               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9349                   <= HOST_BITS_PER_WIDE_INT)
9350               && ((unsigned HOST_WIDE_INT) const_op
9351                   < (((HOST_WIDE_INT) 1
9352                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
9353             {
9354               op0 = XEXP (op0, 0);
9355               continue;
9356             }
9357           break;
9358
9359         case SUBREG:
9360           /* Check for the case where we are comparing A - C1 with C2,
9361              both constants are smaller than 1/2 the maximum positive
9362              value in MODE, and the comparison is equality or unsigned.
9363              In that case, if A is either zero-extended to MODE or has
9364              sufficient sign bits so that the high-order bit in MODE
9365              is a copy of the sign in the inner mode, we can prove that it is
9366              safe to do the operation in the wider mode.  This simplifies
9367              many range checks.  */
9368
9369           if (mode_width <= HOST_BITS_PER_WIDE_INT
9370               && subreg_lowpart_p (op0)
9371               && GET_CODE (SUBREG_REG (op0)) == PLUS
9372               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
9373               && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
9374               && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
9375                   < GET_MODE_MASK (mode) / 2)
9376               && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
9377               && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
9378                                       GET_MODE (SUBREG_REG (op0)))
9379                         & ~ GET_MODE_MASK (mode))
9380                   || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
9381                                            GET_MODE (SUBREG_REG (op0)))
9382                       > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9383                          - GET_MODE_BITSIZE (mode)))))
9384             {
9385               op0 = SUBREG_REG (op0);
9386               continue;
9387             }
9388
9389           /* If the inner mode is narrower and we are extracting the low part,
9390              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
9391           if (subreg_lowpart_p (op0)
9392               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
9393             /* Fall through */ ;
9394           else
9395             break;
9396
9397           /* ... fall through ... */
9398
9399         case ZERO_EXTEND:
9400           if ((unsigned_comparison_p || equality_comparison_p)
9401               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9402                   <= HOST_BITS_PER_WIDE_INT)
9403               && ((unsigned HOST_WIDE_INT) const_op
9404                   < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
9405             {
9406               op0 = XEXP (op0, 0);
9407               continue;
9408             }
9409           break;
9410
9411         case PLUS:
9412           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
9413              this for equality comparisons due to pathological cases involving
9414              overflows.  */
9415           if (equality_comparison_p
9416               && 0 != (tem = simplify_binary_operation (MINUS, mode,
9417                                                         op1, XEXP (op0, 1))))
9418             {
9419               op0 = XEXP (op0, 0);
9420               op1 = tem;
9421               continue;
9422             }
9423
9424           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
9425           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
9426               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
9427             {
9428               op0 = XEXP (XEXP (op0, 0), 0);
9429               code = (code == LT ? EQ : NE);
9430               continue;
9431             }
9432           break;
9433
9434         case MINUS:
9435           /* (eq (minus A B) C) -> (eq A (plus B C)) or
9436              (eq B (minus A C)), whichever simplifies.  We can only do
9437              this for equality comparisons due to pathological cases involving
9438              overflows.  */
9439           if (equality_comparison_p
9440               && 0 != (tem = simplify_binary_operation (PLUS, mode,
9441                                                         XEXP (op0, 1), op1)))
9442             {
9443               op0 = XEXP (op0, 0);
9444               op1 = tem;
9445               continue;
9446             }
9447
9448           if (equality_comparison_p
9449               && 0 != (tem = simplify_binary_operation (MINUS, mode,
9450                                                         XEXP (op0, 0), op1)))
9451             {
9452               op0 = XEXP (op0, 1);
9453               op1 = tem;
9454               continue;
9455             }
9456
9457           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
9458              of bits in X minus 1, is one iff X > 0.  */
9459           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
9460               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9461               && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
9462               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
9463             {
9464               op0 = XEXP (op0, 1);
9465               code = (code == GE ? LE : GT);
9466               continue;
9467             }
9468           break;
9469
9470         case XOR:
9471           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
9472              if C is zero or B is a constant.  */
9473           if (equality_comparison_p
9474               && 0 != (tem = simplify_binary_operation (XOR, mode,
9475                                                         XEXP (op0, 1), op1)))
9476             {
9477               op0 = XEXP (op0, 0);
9478               op1 = tem;
9479               continue;
9480             }
9481           break;
9482
9483         case EQ:  case NE:
9484         case LT:  case LTU:  case LE:  case LEU:
9485         case GT:  case GTU:  case GE:  case GEU:
9486           /* We can't do anything if OP0 is a condition code value, rather
9487              than an actual data value.  */
9488           if (const_op != 0
9489 #ifdef HAVE_cc0
9490               || XEXP (op0, 0) == cc0_rtx
9491 #endif
9492               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
9493             break;
9494
9495           /* Get the two operands being compared.  */
9496           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
9497             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
9498           else
9499             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
9500
9501           /* Check for the cases where we simply want the result of the
9502              earlier test or the opposite of that result.  */
9503           if (code == NE
9504               || (code == EQ && reversible_comparison_p (op0))
9505               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9506                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
9507                   && (STORE_FLAG_VALUE
9508                       & (((HOST_WIDE_INT) 1
9509                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
9510                   && (code == LT
9511                       || (code == GE && reversible_comparison_p (op0)))))
9512             {
9513               code = (code == LT || code == NE
9514                       ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
9515               op0 = tem, op1 = tem1;
9516               continue;
9517             }
9518           break;
9519
9520         case IOR:
9521           /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
9522              iff X <= 0.  */
9523           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
9524               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
9525               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
9526             {
9527               op0 = XEXP (op0, 1);
9528               code = (code == GE ? GT : LE);
9529               continue;
9530             }
9531           break;
9532
9533         case AND:
9534           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
9535              will be converted to a ZERO_EXTRACT later.  */
9536           if (const_op == 0 && equality_comparison_p
9537               && GET_CODE (XEXP (op0, 0)) == ASHIFT
9538               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
9539             {
9540               op0 = simplify_and_const_int
9541                 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
9542                                              XEXP (op0, 1),
9543                                              XEXP (XEXP (op0, 0), 1)),
9544                  (HOST_WIDE_INT) 1);
9545               continue;
9546             }
9547
9548           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
9549              zero and X is a comparison and C1 and C2 describe only bits set
9550              in STORE_FLAG_VALUE, we can compare with X.  */
9551           if (const_op == 0 && equality_comparison_p
9552               && mode_width <= HOST_BITS_PER_WIDE_INT
9553               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9554               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
9555               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9556               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
9557               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9558             {
9559               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
9560                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
9561               if ((~ STORE_FLAG_VALUE & mask) == 0
9562                   && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
9563                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
9564                           && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
9565                 {
9566                   op0 = XEXP (XEXP (op0, 0), 0);
9567                   continue;
9568                 }
9569             }
9570
9571           /* If we are doing an equality comparison of an AND of a bit equal
9572              to the sign bit, replace this with a LT or GE comparison of
9573              the underlying value.  */
9574           if (equality_comparison_p
9575               && const_op == 0
9576               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9577               && mode_width <= HOST_BITS_PER_WIDE_INT
9578               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
9579                   == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9580             {
9581               op0 = XEXP (op0, 0);
9582               code = (code == EQ ? GE : LT);
9583               continue;
9584             }
9585
9586           /* If this AND operation is really a ZERO_EXTEND from a narrower
9587              mode, the constant fits within that mode, and this is either an
9588              equality or unsigned comparison, try to do this comparison in
9589              the narrower mode.  */
9590           if ((equality_comparison_p || unsigned_comparison_p)
9591               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9592               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
9593                                    & GET_MODE_MASK (mode))
9594                                   + 1)) >= 0
9595               && const_op >> i == 0
9596               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
9597             {
9598               op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
9599               continue;
9600             }
9601           break;
9602
9603         case ASHIFT:
9604           /* If we have (compare (ashift FOO N) (const_int C)) and
9605              the high order N bits of FOO (N+1 if an inequality comparison)
9606              are known to be zero, we can do this by comparing FOO with C
9607              shifted right N bits so long as the low-order N bits of C are
9608              zero.  */
9609           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
9610               && INTVAL (XEXP (op0, 1)) >= 0
9611               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
9612                   < HOST_BITS_PER_WIDE_INT)
9613               && ((const_op
9614                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
9615               && mode_width <= HOST_BITS_PER_WIDE_INT
9616               && (nonzero_bits (XEXP (op0, 0), mode)
9617                   & ~ (mask >> (INTVAL (XEXP (op0, 1))
9618                                 + ! equality_comparison_p))) == 0)
9619             {
9620               const_op >>= INTVAL (XEXP (op0, 1));
9621               op1 = GEN_INT (const_op);
9622               op0 = XEXP (op0, 0);
9623               continue;
9624             }
9625
9626           /* If we are doing a sign bit comparison, it means we are testing
9627              a particular bit.  Convert it to the appropriate AND.  */
9628           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9629               && mode_width <= HOST_BITS_PER_WIDE_INT)
9630             {
9631               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9632                                             ((HOST_WIDE_INT) 1
9633                                              << (mode_width - 1
9634                                                  - INTVAL (XEXP (op0, 1)))));
9635               code = (code == LT ? NE : EQ);
9636               continue;
9637             }
9638
9639           /* If this an equality comparison with zero and we are shifting
9640              the low bit to the sign bit, we can convert this to an AND of the
9641              low-order bit.  */
9642           if (const_op == 0 && equality_comparison_p
9643               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9644               && INTVAL (XEXP (op0, 1)) == mode_width - 1)
9645             {
9646               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9647                                             (HOST_WIDE_INT) 1);
9648               continue;
9649             }
9650           break;
9651
9652         case ASHIFTRT:
9653           /* If this is an equality comparison with zero, we can do this
9654              as a logical shift, which might be much simpler.  */
9655           if (equality_comparison_p && const_op == 0
9656               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
9657             {
9658               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
9659                                           XEXP (op0, 0),
9660                                           INTVAL (XEXP (op0, 1)));
9661               continue;
9662             }
9663
9664           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
9665              do the comparison in a narrower mode.  */
9666           if (! unsigned_comparison_p
9667               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9668               && GET_CODE (XEXP (op0, 0)) == ASHIFT
9669               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9670               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
9671                                          MODE_INT, 1)) != BLKmode
9672               && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
9673                   || ((unsigned HOST_WIDE_INT) - const_op
9674                       <= GET_MODE_MASK (tmode))))
9675             {
9676               op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
9677               continue;
9678             }
9679
9680           /* ... fall through ... */
9681         case LSHIFTRT:
9682           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
9683              the low order N bits of FOO are known to be zero, we can do this
9684              by comparing FOO with C shifted left N bits so long as no
9685              overflow occurs.  */
9686           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
9687               && INTVAL (XEXP (op0, 1)) >= 0
9688               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9689               && mode_width <= HOST_BITS_PER_WIDE_INT
9690               && (nonzero_bits (XEXP (op0, 0), mode)
9691                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
9692               && (const_op == 0
9693                   || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
9694                       < mode_width)))
9695             {
9696               const_op <<= INTVAL (XEXP (op0, 1));
9697               op1 = GEN_INT (const_op);
9698               op0 = XEXP (op0, 0);
9699               continue;
9700             }
9701
9702           /* If we are using this shift to extract just the sign bit, we
9703              can replace this with an LT or GE comparison.  */
9704           if (const_op == 0
9705               && (equality_comparison_p || sign_bit_comparison_p)
9706               && GET_CODE (XEXP (op0, 1)) == CONST_INT
9707               && INTVAL (XEXP (op0, 1)) == mode_width - 1)
9708             {
9709               op0 = XEXP (op0, 0);
9710               code = (code == NE || code == GT ? LT : GE);
9711               continue;
9712             }
9713           break;
9714         }
9715
9716       break;
9717     }
9718
9719   /* Now make any compound operations involved in this comparison.  Then,
9720      check for an outmost SUBREG on OP0 that isn't doing anything or is
9721      paradoxical.  The latter case can only occur when it is known that the
9722      "extra" bits will be zero.  Therefore, it is safe to remove the SUBREG.
9723      We can never remove a SUBREG for a non-equality comparison because the
9724      sign bit is in a different place in the underlying object.  */
9725
9726   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
9727   op1 = make_compound_operation (op1, SET);
9728
9729   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
9730       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
9731       && (code == NE || code == EQ)
9732       && ((GET_MODE_SIZE (GET_MODE (op0))
9733            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
9734     {
9735       op0 = SUBREG_REG (op0);
9736       op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
9737     }
9738
9739   else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
9740            && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
9741            && (code == NE || code == EQ)
9742            && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9743                <= HOST_BITS_PER_WIDE_INT)
9744            && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
9745                & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
9746            && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
9747                                               op1),
9748                (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
9749                 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
9750     op0 = SUBREG_REG (op0), op1 = tem;
9751
9752   /* We now do the opposite procedure: Some machines don't have compare
9753      insns in all modes.  If OP0's mode is an integer mode smaller than a
9754      word and we can't do a compare in that mode, see if there is a larger
9755      mode for which we can do the compare.  There are a number of cases in
9756      which we can use the wider mode.  */
9757
9758   mode = GET_MODE (op0);
9759   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
9760       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
9761       && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
9762     for (tmode = GET_MODE_WIDER_MODE (mode);
9763          (tmode != VOIDmode
9764           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
9765          tmode = GET_MODE_WIDER_MODE (tmode))
9766       if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
9767         {
9768           /* If the only nonzero bits in OP0 and OP1 are those in the
9769              narrower mode and this is an equality or unsigned comparison,
9770              we can use the wider mode.  Similarly for sign-extended
9771              values, in which case it is true for all comparisons.  */
9772           if (((code == EQ || code == NE
9773                 || code == GEU || code == GTU || code == LEU || code == LTU)
9774                && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
9775                && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
9776               || ((num_sign_bit_copies (op0, tmode)
9777                    > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
9778                   && (num_sign_bit_copies (op1, tmode)
9779                       > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
9780             {
9781               op0 = gen_lowpart_for_combine (tmode, op0);
9782               op1 = gen_lowpart_for_combine (tmode, op1);
9783               break;
9784             }
9785
9786           /* If this is a test for negative, we can make an explicit
9787              test of the sign bit.  */
9788
9789           if (op1 == const0_rtx && (code == LT || code == GE)
9790               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9791             {
9792               op0 = gen_binary (AND, tmode,
9793                                 gen_lowpart_for_combine (tmode, op0),
9794                                 GEN_INT ((HOST_WIDE_INT) 1
9795                                          << (GET_MODE_BITSIZE (mode) - 1)));
9796               code = (code == LT) ? NE : EQ;
9797               break;
9798             }
9799         }
9800
9801 #ifdef CANONICALIZE_COMPARISON
9802   /* If this machine only supports a subset of valid comparisons, see if we
9803      can convert an unsupported one into a supported one.  */
9804   CANONICALIZE_COMPARISON (code, op0, op1);
9805 #endif
9806
9807   *pop0 = op0;
9808   *pop1 = op1;
9809
9810   return code;
9811 }
9812 \f
9813 /* Return 1 if we know that X, a comparison operation, is not operating
9814    on a floating-point value or is EQ or NE, meaning that we can safely
9815    reverse it.  */
9816
9817 static int
9818 reversible_comparison_p (x)
9819      rtx x;
9820 {
9821   if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
9822       || flag_fast_math
9823       || GET_CODE (x) == NE || GET_CODE (x) == EQ)
9824     return 1;
9825
9826   switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
9827     {
9828     case MODE_INT:
9829     case MODE_PARTIAL_INT:
9830     case MODE_COMPLEX_INT:
9831       return 1;
9832
9833     case MODE_CC:
9834       /* If the mode of the condition codes tells us that this is safe,
9835          we need look no further.  */
9836       if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
9837         return 1;
9838
9839       /* Otherwise try and find where the condition codes were last set and
9840          use that.  */
9841       x = get_last_value (XEXP (x, 0));
9842       return (x && GET_CODE (x) == COMPARE
9843               && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
9844     }
9845
9846   return 0;
9847 }
9848 \f
9849 /* Utility function for following routine.  Called when X is part of a value
9850    being stored into reg_last_set_value.  Sets reg_last_set_table_tick
9851    for each register mentioned.  Similar to mention_regs in cse.c  */
9852
9853 static void
9854 update_table_tick (x)
9855      rtx x;
9856 {
9857   register enum rtx_code code = GET_CODE (x);
9858   register char *fmt = GET_RTX_FORMAT (code);
9859   register int i;
9860
9861   if (code == REG)
9862     {
9863       int regno = REGNO (x);
9864       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
9865                               ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
9866
9867       for (i = regno; i < endregno; i++)
9868         reg_last_set_table_tick[i] = label_tick;
9869
9870       return;
9871     }
9872   
9873   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9874     /* Note that we can't have an "E" in values stored; see
9875        get_last_value_validate.  */
9876     if (fmt[i] == 'e')
9877       update_table_tick (XEXP (x, i));
9878 }
9879
9880 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
9881    are saying that the register is clobbered and we no longer know its
9882    value.  If INSN is zero, don't update reg_last_set; this is only permitted
9883    with VALUE also zero and is used to invalidate the register.  */
9884
9885 static void
9886 record_value_for_reg (reg, insn, value)
9887      rtx reg;
9888      rtx insn;
9889      rtx value;
9890 {
9891   int regno = REGNO (reg);
9892   int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
9893                           ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
9894   int i;
9895
9896   /* If VALUE contains REG and we have a previous value for REG, substitute
9897      the previous value.  */
9898   if (value && insn && reg_overlap_mentioned_p (reg, value))
9899     {
9900       rtx tem;
9901
9902       /* Set things up so get_last_value is allowed to see anything set up to
9903          our insn.  */
9904       subst_low_cuid = INSN_CUID (insn);
9905       tem = get_last_value (reg);      
9906
9907       if (tem)
9908         value = replace_rtx (copy_rtx (value), reg, tem);
9909     }
9910
9911   /* For each register modified, show we don't know its value, that
9912      we don't know about its bitwise content, that its value has been
9913      updated, and that we don't know the location of the death of the
9914      register.  */
9915   for (i = regno; i < endregno; i ++)
9916     {
9917       if (insn)
9918         reg_last_set[i] = insn;
9919       reg_last_set_value[i] = 0;
9920       reg_last_set_mode[i] = 0;
9921       reg_last_set_nonzero_bits[i] = 0;
9922       reg_last_set_sign_bit_copies[i] = 0;
9923       reg_last_death[i] = 0;
9924     }
9925
9926   /* Mark registers that are being referenced in this value.  */
9927   if (value)
9928     update_table_tick (value);
9929
9930   /* Now update the status of each register being set.
9931      If someone is using this register in this block, set this register
9932      to invalid since we will get confused between the two lives in this
9933      basic block.  This makes using this register always invalid.  In cse, we
9934      scan the table to invalidate all entries using this register, but this
9935      is too much work for us.  */
9936
9937   for (i = regno; i < endregno; i++)
9938     {
9939       reg_last_set_label[i] = label_tick;
9940       if (value && reg_last_set_table_tick[i] == label_tick)
9941         reg_last_set_invalid[i] = 1;
9942       else
9943         reg_last_set_invalid[i] = 0;
9944     }
9945
9946   /* The value being assigned might refer to X (like in "x++;").  In that
9947      case, we must replace it with (clobber (const_int 0)) to prevent
9948      infinite loops.  */
9949   if (value && ! get_last_value_validate (&value,
9950                                           reg_last_set_label[regno], 0))
9951     {
9952       value = copy_rtx (value);
9953       if (! get_last_value_validate (&value, reg_last_set_label[regno], 1))
9954         value = 0;
9955     }
9956
9957   /* For the main register being modified, update the value, the mode, the
9958      nonzero bits, and the number of sign bit copies.  */
9959
9960   reg_last_set_value[regno] = value;
9961
9962   if (value)
9963     {
9964       subst_low_cuid = INSN_CUID (insn);
9965       reg_last_set_mode[regno] = GET_MODE (reg);
9966       reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
9967       reg_last_set_sign_bit_copies[regno]
9968         = num_sign_bit_copies (value, GET_MODE (reg));
9969     }
9970 }
9971
9972 /* Used for communication between the following two routines.  */
9973 static rtx record_dead_insn;
9974
9975 /* Called via note_stores from record_dead_and_set_regs to handle one
9976    SET or CLOBBER in an insn.  */
9977
9978 static void
9979 record_dead_and_set_regs_1 (dest, setter)
9980      rtx dest, setter;
9981 {
9982   if (GET_CODE (dest) == SUBREG)
9983     dest = SUBREG_REG (dest);
9984
9985   if (GET_CODE (dest) == REG)
9986     {
9987       /* If we are setting the whole register, we know its value.  Otherwise
9988          show that we don't know the value.  We can handle SUBREG in
9989          some cases.  */
9990       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
9991         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
9992       else if (GET_CODE (setter) == SET
9993                && GET_CODE (SET_DEST (setter)) == SUBREG
9994                && SUBREG_REG (SET_DEST (setter)) == dest
9995                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
9996                && subreg_lowpart_p (SET_DEST (setter)))
9997         record_value_for_reg (dest, record_dead_insn,
9998                               gen_lowpart_for_combine (GET_MODE (dest),
9999                                                        SET_SRC (setter)));
10000       else
10001         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10002     }
10003   else if (GET_CODE (dest) == MEM
10004            /* Ignore pushes, they clobber nothing.  */
10005            && ! push_operand (dest, GET_MODE (dest)))
10006     mem_last_set = INSN_CUID (record_dead_insn);
10007 }
10008
10009 /* Update the records of when each REG was most recently set or killed
10010    for the things done by INSN.  This is the last thing done in processing
10011    INSN in the combiner loop.
10012
10013    We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10014    reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10015    and also the similar information mem_last_set (which insn most recently
10016    modified memory) and last_call_cuid (which insn was the most recent
10017    subroutine call).  */
10018
10019 static void
10020 record_dead_and_set_regs (insn)
10021      rtx insn;
10022 {
10023   register rtx link;
10024   int i;
10025
10026   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10027     {
10028       if (REG_NOTE_KIND (link) == REG_DEAD
10029           && GET_CODE (XEXP (link, 0)) == REG)
10030         {
10031           int regno = REGNO (XEXP (link, 0));
10032           int endregno
10033             = regno + (regno < FIRST_PSEUDO_REGISTER
10034                        ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10035                        : 1);
10036
10037           for (i = regno; i < endregno; i++)
10038             reg_last_death[i] = insn;
10039         }
10040       else if (REG_NOTE_KIND (link) == REG_INC)
10041         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10042     }
10043
10044   if (GET_CODE (insn) == CALL_INSN)
10045     {
10046       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10047         if (call_used_regs[i])
10048           {
10049             reg_last_set_value[i] = 0;
10050             reg_last_set_mode[i] = 0;
10051             reg_last_set_nonzero_bits[i] = 0;
10052             reg_last_set_sign_bit_copies[i] = 0;
10053             reg_last_death[i] = 0;
10054           }
10055
10056       last_call_cuid = mem_last_set = INSN_CUID (insn);
10057     }
10058
10059   record_dead_insn = insn;
10060   note_stores (PATTERN (insn), record_dead_and_set_regs_1);
10061 }
10062 \f
10063 /* Utility routine for the following function.  Verify that all the registers
10064    mentioned in *LOC are valid when *LOC was part of a value set when
10065    label_tick == TICK.  Return 0 if some are not.
10066
10067    If REPLACE is non-zero, replace the invalid reference with
10068    (clobber (const_int 0)) and return 1.  This replacement is useful because
10069    we often can get useful information about the form of a value (e.g., if
10070    it was produced by a shift that always produces -1 or 0) even though
10071    we don't know exactly what registers it was produced from.  */
10072
10073 static int
10074 get_last_value_validate (loc, tick, replace)
10075      rtx *loc;
10076      int tick;
10077      int replace;
10078 {
10079   rtx x = *loc;
10080   char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10081   int len = GET_RTX_LENGTH (GET_CODE (x));
10082   int i;
10083
10084   if (GET_CODE (x) == REG)
10085     {
10086       int regno = REGNO (x);
10087       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10088                               ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10089       int j;
10090
10091       for (j = regno; j < endregno; j++)
10092         if (reg_last_set_invalid[j]
10093             /* If this is a pseudo-register that was only set once, it is
10094                always valid.  */
10095             || (! (regno >= FIRST_PSEUDO_REGISTER && reg_n_sets[regno] == 1)
10096                 && reg_last_set_label[j] > tick))
10097           {
10098             if (replace)
10099               *loc = gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
10100             return replace;
10101           }
10102
10103       return 1;
10104     }
10105
10106   for (i = 0; i < len; i++)
10107     if ((fmt[i] == 'e'
10108          && get_last_value_validate (&XEXP (x, i), tick, replace) == 0)
10109         /* Don't bother with these.  They shouldn't occur anyway.  */
10110         || fmt[i] == 'E')
10111       return 0;
10112
10113   /* If we haven't found a reason for it to be invalid, it is valid.  */
10114   return 1;
10115 }
10116
10117 /* Get the last value assigned to X, if known.  Some registers
10118    in the value may be replaced with (clobber (const_int 0)) if their value
10119    is known longer known reliably.  */
10120
10121 static rtx
10122 get_last_value (x)
10123      rtx x;
10124 {
10125   int regno;
10126   rtx value;
10127
10128   /* If this is a non-paradoxical SUBREG, get the value of its operand and
10129      then convert it to the desired mode.  If this is a paradoxical SUBREG,
10130      we cannot predict what values the "extra" bits might have. */
10131   if (GET_CODE (x) == SUBREG
10132       && subreg_lowpart_p (x)
10133       && (GET_MODE_SIZE (GET_MODE (x))
10134           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
10135       && (value = get_last_value (SUBREG_REG (x))) != 0)
10136     return gen_lowpart_for_combine (GET_MODE (x), value);
10137
10138   if (GET_CODE (x) != REG)
10139     return 0;
10140
10141   regno = REGNO (x);
10142   value = reg_last_set_value[regno];
10143
10144   /* If we don't have a value or if it isn't for this basic block, return 0. */
10145
10146   if (value == 0
10147       || (reg_n_sets[regno] != 1
10148           && reg_last_set_label[regno] != label_tick))
10149     return 0;
10150
10151   /* If the value was set in a later insn than the ones we are processing,
10152      we can't use it even if the register was only set once, but make a quick
10153      check to see if the previous insn set it to something.  This is commonly
10154      the case when the same pseudo is used by repeated insns.
10155
10156      This does not work if there exists an instruction which is temporarily
10157      not on the insn chain.  */
10158
10159   if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
10160     {
10161       rtx insn, set;
10162
10163       /* We can not do anything useful in this case, because there is
10164          an instruction which is not on the insn chain.  */
10165       if (subst_prev_insn)
10166         return 0;
10167
10168       /* Skip over USE insns.  They are not useful here, and they may have
10169          been made by combine, in which case they do not have a INSN_CUID
10170          value.  We can't use prev_real_insn, because that would incorrectly
10171          take us backwards across labels.  Skip over BARRIERs also, since
10172          they could have been made by combine.  If we see one, we must be
10173          optimizing dead code, so it doesn't matter what we do.  */
10174       for (insn = prev_nonnote_insn (subst_insn);
10175            insn && ((GET_CODE (insn) == INSN
10176                      && GET_CODE (PATTERN (insn)) == USE)
10177                     || GET_CODE (insn) == BARRIER
10178                     || INSN_CUID (insn) >= subst_low_cuid);
10179            insn = prev_nonnote_insn (insn))
10180         ;
10181
10182       if (insn
10183           && (set = single_set (insn)) != 0
10184           && rtx_equal_p (SET_DEST (set), x))
10185         {
10186           value = SET_SRC (set);
10187
10188           /* Make sure that VALUE doesn't reference X.  Replace any
10189              explicit references with a CLOBBER.  If there are any remaining
10190              references (rare), don't use the value.  */
10191
10192           if (reg_mentioned_p (x, value))
10193             value = replace_rtx (copy_rtx (value), x,
10194                                  gen_rtx (CLOBBER, GET_MODE (x), const0_rtx));
10195
10196           if (reg_overlap_mentioned_p (x, value))
10197             return 0;
10198         }
10199       else
10200         return 0;
10201     }
10202
10203   /* If the value has all its registers valid, return it.  */
10204   if (get_last_value_validate (&value, reg_last_set_label[regno], 0))
10205     return value;
10206
10207   /* Otherwise, make a copy and replace any invalid register with
10208      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
10209
10210   value = copy_rtx (value);
10211   if (get_last_value_validate (&value, reg_last_set_label[regno], 1))
10212     return value;
10213
10214   return 0;
10215 }
10216 \f
10217 /* Return nonzero if expression X refers to a REG or to memory
10218    that is set in an instruction more recent than FROM_CUID.  */
10219
10220 static int
10221 use_crosses_set_p (x, from_cuid)
10222      register rtx x;
10223      int from_cuid;
10224 {
10225   register char *fmt;
10226   register int i;
10227   register enum rtx_code code = GET_CODE (x);
10228
10229   if (code == REG)
10230     {
10231       register int regno = REGNO (x);
10232       int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
10233                             ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10234       
10235 #ifdef PUSH_ROUNDING
10236       /* Don't allow uses of the stack pointer to be moved,
10237          because we don't know whether the move crosses a push insn.  */
10238       if (regno == STACK_POINTER_REGNUM)
10239         return 1;
10240 #endif
10241       for (;regno < endreg; regno++)
10242         if (reg_last_set[regno]
10243             && INSN_CUID (reg_last_set[regno]) > from_cuid)
10244           return 1;
10245       return 0;
10246     }
10247
10248   if (code == MEM && mem_last_set > from_cuid)
10249     return 1;
10250
10251   fmt = GET_RTX_FORMAT (code);
10252
10253   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10254     {
10255       if (fmt[i] == 'E')
10256         {
10257           register int j;
10258           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10259             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
10260               return 1;
10261         }
10262       else if (fmt[i] == 'e'
10263                && use_crosses_set_p (XEXP (x, i), from_cuid))
10264         return 1;
10265     }
10266   return 0;
10267 }
10268 \f
10269 /* Define three variables used for communication between the following
10270    routines.  */
10271
10272 static int reg_dead_regno, reg_dead_endregno;
10273 static int reg_dead_flag;
10274
10275 /* Function called via note_stores from reg_dead_at_p.
10276
10277    If DEST is within [reg_dead_regno, reg_dead_endregno), set 
10278    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
10279
10280 static void
10281 reg_dead_at_p_1 (dest, x)
10282      rtx dest;
10283      rtx x;
10284 {
10285   int regno, endregno;
10286
10287   if (GET_CODE (dest) != REG)
10288     return;
10289
10290   regno = REGNO (dest);
10291   endregno = regno + (regno < FIRST_PSEUDO_REGISTER 
10292                       ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
10293
10294   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
10295     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
10296 }
10297
10298 /* Return non-zero if REG is known to be dead at INSN.
10299
10300    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
10301    referencing REG, it is dead.  If we hit a SET referencing REG, it is
10302    live.  Otherwise, see if it is live or dead at the start of the basic
10303    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
10304    must be assumed to be always live.  */
10305
10306 static int
10307 reg_dead_at_p (reg, insn)
10308      rtx reg;
10309      rtx insn;
10310 {
10311   int block, i;
10312
10313   /* Set variables for reg_dead_at_p_1.  */
10314   reg_dead_regno = REGNO (reg);
10315   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
10316                                         ? HARD_REGNO_NREGS (reg_dead_regno,
10317                                                             GET_MODE (reg))
10318                                         : 1);
10319
10320   reg_dead_flag = 0;
10321
10322   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  */
10323   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
10324     {
10325       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10326         if (TEST_HARD_REG_BIT (newpat_used_regs, i))
10327           return 0;
10328     }
10329
10330   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
10331      beginning of function.  */
10332   for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
10333        insn = prev_nonnote_insn (insn))
10334     {
10335       note_stores (PATTERN (insn), reg_dead_at_p_1);
10336       if (reg_dead_flag)
10337         return reg_dead_flag == 1 ? 1 : 0;
10338
10339       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
10340         return 1;
10341     }
10342
10343   /* Get the basic block number that we were in.  */
10344   if (insn == 0)
10345     block = 0;
10346   else
10347     {
10348       for (block = 0; block < n_basic_blocks; block++)
10349         if (insn == basic_block_head[block])
10350           break;
10351
10352       if (block == n_basic_blocks)
10353         return 0;
10354     }
10355
10356   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10357     if (basic_block_live_at_start[block][i / REGSET_ELT_BITS]
10358         & ((REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS)))
10359       return 0;
10360
10361   return 1;
10362 }
10363 \f
10364 /* Note hard registers in X that are used.  This code is similar to
10365    that in flow.c, but much simpler since we don't care about pseudos.  */
10366
10367 static void
10368 mark_used_regs_combine (x)
10369      rtx x;
10370 {
10371   register RTX_CODE code = GET_CODE (x);
10372   register int regno;
10373   int i;
10374
10375   switch (code)
10376     {
10377     case LABEL_REF:
10378     case SYMBOL_REF:
10379     case CONST_INT:
10380     case CONST:
10381     case CONST_DOUBLE:
10382     case PC:
10383     case ADDR_VEC:
10384     case ADDR_DIFF_VEC:
10385     case ASM_INPUT:
10386 #ifdef HAVE_cc0
10387     /* CC0 must die in the insn after it is set, so we don't need to take
10388        special note of it here.  */
10389     case CC0:
10390 #endif
10391       return;
10392
10393     case CLOBBER:
10394       /* If we are clobbering a MEM, mark any hard registers inside the
10395          address as used.  */
10396       if (GET_CODE (XEXP (x, 0)) == MEM)
10397         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
10398       return;
10399
10400     case REG:
10401       regno = REGNO (x);
10402       /* A hard reg in a wide mode may really be multiple registers.
10403          If so, mark all of them just like the first.  */
10404       if (regno < FIRST_PSEUDO_REGISTER)
10405         {
10406           /* None of this applies to the stack, frame or arg pointers */
10407           if (regno == STACK_POINTER_REGNUM
10408 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10409               || regno == HARD_FRAME_POINTER_REGNUM
10410 #endif
10411 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
10412               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
10413 #endif
10414               || regno == FRAME_POINTER_REGNUM)
10415             return;
10416
10417           i = HARD_REGNO_NREGS (regno, GET_MODE (x));
10418           while (i-- > 0)
10419             SET_HARD_REG_BIT (newpat_used_regs, regno + i);
10420         }
10421       return;
10422
10423     case SET:
10424       {
10425         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
10426            the address.  */
10427         register rtx testreg = SET_DEST (x);
10428
10429         while (GET_CODE (testreg) == SUBREG
10430                || GET_CODE (testreg) == ZERO_EXTRACT
10431                || GET_CODE (testreg) == SIGN_EXTRACT
10432                || GET_CODE (testreg) == STRICT_LOW_PART)
10433           testreg = XEXP (testreg, 0);
10434
10435         if (GET_CODE (testreg) == MEM)
10436           mark_used_regs_combine (XEXP (testreg, 0));
10437
10438         mark_used_regs_combine (SET_SRC (x));
10439         return;
10440       }
10441     }
10442
10443   /* Recursively scan the operands of this expression.  */
10444
10445   {
10446     register char *fmt = GET_RTX_FORMAT (code);
10447
10448     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10449       {
10450         if (fmt[i] == 'e')
10451           mark_used_regs_combine (XEXP (x, i));
10452         else if (fmt[i] == 'E')
10453           {
10454             register int j;
10455
10456             for (j = 0; j < XVECLEN (x, i); j++)
10457               mark_used_regs_combine (XVECEXP (x, i, j));
10458           }
10459       }
10460   }
10461 }
10462
10463 \f
10464 /* Remove register number REGNO from the dead registers list of INSN.
10465
10466    Return the note used to record the death, if there was one.  */
10467
10468 rtx
10469 remove_death (regno, insn)
10470      int regno;
10471      rtx insn;
10472 {
10473   register rtx note = find_regno_note (insn, REG_DEAD, regno);
10474
10475   if (note)
10476     {
10477       reg_n_deaths[regno]--;
10478       remove_note (insn, note);
10479     }
10480
10481   return note;
10482 }
10483
10484 /* For each register (hardware or pseudo) used within expression X, if its
10485    death is in an instruction with cuid between FROM_CUID (inclusive) and
10486    TO_INSN (exclusive), put a REG_DEAD note for that register in the
10487    list headed by PNOTES. 
10488
10489    This is done when X is being merged by combination into TO_INSN.  These
10490    notes will then be distributed as needed.  */
10491
10492 static void
10493 move_deaths (x, from_cuid, to_insn, pnotes)
10494      rtx x;
10495      int from_cuid;
10496      rtx to_insn;
10497      rtx *pnotes;
10498 {
10499   register char *fmt;
10500   register int len, i;
10501   register enum rtx_code code = GET_CODE (x);
10502
10503   if (code == REG)
10504     {
10505       register int regno = REGNO (x);
10506       register rtx where_dead = reg_last_death[regno];
10507       register rtx before_dead, after_dead;
10508
10509       /* WHERE_DEAD could be a USE insn made by combine, so first we
10510          make sure that we have insns with valid INSN_CUID values.  */
10511       before_dead = where_dead;
10512       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
10513         before_dead = PREV_INSN (before_dead);
10514       after_dead = where_dead;
10515       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
10516         after_dead = NEXT_INSN (after_dead);
10517
10518       if (before_dead && after_dead
10519           && INSN_CUID (before_dead) >= from_cuid
10520           && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
10521               || (where_dead != after_dead
10522                   && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
10523         {
10524           rtx note = remove_death (regno, where_dead);
10525
10526           /* It is possible for the call above to return 0.  This can occur
10527              when reg_last_death points to I2 or I1 that we combined with.
10528              In that case make a new note.
10529
10530              We must also check for the case where X is a hard register
10531              and NOTE is a death note for a range of hard registers
10532              including X.  In that case, we must put REG_DEAD notes for
10533              the remaining registers in place of NOTE.  */
10534
10535           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
10536               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
10537                   != GET_MODE_SIZE (GET_MODE (x))))
10538             {
10539               int deadregno = REGNO (XEXP (note, 0));
10540               int deadend
10541                 = (deadregno + HARD_REGNO_NREGS (deadregno,
10542                                                  GET_MODE (XEXP (note, 0))));
10543               int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
10544               int i;
10545
10546               for (i = deadregno; i < deadend; i++)
10547                 if (i < regno || i >= ourend)
10548                   REG_NOTES (where_dead)
10549                     = gen_rtx (EXPR_LIST, REG_DEAD,
10550                                gen_rtx (REG, reg_raw_mode[i], i),
10551                                REG_NOTES (where_dead));
10552             }
10553           /* If we didn't find any note, and we have a multi-reg hard
10554              register, then to be safe we must check for REG_DEAD notes
10555              for each register other than the first.  They could have
10556              their own REG_DEAD notes lying around.  */
10557           else if (note == 0 && regno < FIRST_PSEUDO_REGISTER
10558                    && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
10559             {
10560               int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
10561               int i;
10562               rtx oldnotes = 0;
10563
10564               for (i = regno + 1; i < ourend; i++)
10565                 move_deaths (gen_rtx (REG, reg_raw_mode[i], i),
10566                              from_cuid, to_insn, &oldnotes);
10567             }
10568
10569           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
10570             {
10571               XEXP (note, 1) = *pnotes;
10572               *pnotes = note;
10573             }
10574           else
10575             *pnotes = gen_rtx (EXPR_LIST, REG_DEAD, x, *pnotes);
10576
10577           reg_n_deaths[regno]++;
10578         }
10579
10580       return;
10581     }
10582
10583   else if (GET_CODE (x) == SET)
10584     {
10585       rtx dest = SET_DEST (x);
10586
10587       move_deaths (SET_SRC (x), from_cuid, to_insn, pnotes);
10588
10589       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
10590          that accesses one word of a multi-word item, some
10591          piece of everything register in the expression is used by
10592          this insn, so remove any old death.  */
10593
10594       if (GET_CODE (dest) == ZERO_EXTRACT
10595           || GET_CODE (dest) == STRICT_LOW_PART
10596           || (GET_CODE (dest) == SUBREG
10597               && (((GET_MODE_SIZE (GET_MODE (dest))
10598                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
10599                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
10600                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
10601         {
10602           move_deaths (dest, from_cuid, to_insn, pnotes);
10603           return;
10604         }
10605
10606       /* If this is some other SUBREG, we know it replaces the entire
10607          value, so use that as the destination.  */
10608       if (GET_CODE (dest) == SUBREG)
10609         dest = SUBREG_REG (dest);
10610
10611       /* If this is a MEM, adjust deaths of anything used in the address.
10612          For a REG (the only other possibility), the entire value is
10613          being replaced so the old value is not used in this insn.  */
10614
10615       if (GET_CODE (dest) == MEM)
10616         move_deaths (XEXP (dest, 0), from_cuid, to_insn, pnotes);
10617       return;
10618     }
10619
10620   else if (GET_CODE (x) == CLOBBER)
10621     return;
10622
10623   len = GET_RTX_LENGTH (code);
10624   fmt = GET_RTX_FORMAT (code);
10625
10626   for (i = 0; i < len; i++)
10627     {
10628       if (fmt[i] == 'E')
10629         {
10630           register int j;
10631           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10632             move_deaths (XVECEXP (x, i, j), from_cuid, to_insn, pnotes);
10633         }
10634       else if (fmt[i] == 'e')
10635         move_deaths (XEXP (x, i), from_cuid, to_insn, pnotes);
10636     }
10637 }
10638 \f
10639 /* Return 1 if X is the target of a bit-field assignment in BODY, the
10640    pattern of an insn.  X must be a REG.  */
10641
10642 static int
10643 reg_bitfield_target_p (x, body)
10644      rtx x;
10645      rtx body;
10646 {
10647   int i;
10648
10649   if (GET_CODE (body) == SET)
10650     {
10651       rtx dest = SET_DEST (body);
10652       rtx target;
10653       int regno, tregno, endregno, endtregno;
10654
10655       if (GET_CODE (dest) == ZERO_EXTRACT)
10656         target = XEXP (dest, 0);
10657       else if (GET_CODE (dest) == STRICT_LOW_PART)
10658         target = SUBREG_REG (XEXP (dest, 0));
10659       else
10660         return 0;
10661
10662       if (GET_CODE (target) == SUBREG)
10663         target = SUBREG_REG (target);
10664
10665       if (GET_CODE (target) != REG)
10666         return 0;
10667
10668       tregno = REGNO (target), regno = REGNO (x);
10669       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
10670         return target == x;
10671
10672       endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
10673       endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
10674
10675       return endregno > tregno && regno < endtregno;
10676     }
10677
10678   else if (GET_CODE (body) == PARALLEL)
10679     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
10680       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
10681         return 1;
10682
10683   return 0;
10684 }      
10685 \f
10686 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
10687    as appropriate.  I3 and I2 are the insns resulting from the combination
10688    insns including FROM (I2 may be zero).
10689
10690    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
10691    not need REG_DEAD notes because they are being substituted for.  This
10692    saves searching in the most common cases.
10693
10694    Each note in the list is either ignored or placed on some insns, depending
10695    on the type of note.  */
10696
10697 static void
10698 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
10699      rtx notes;
10700      rtx from_insn;
10701      rtx i3, i2;
10702      rtx elim_i2, elim_i1;
10703 {
10704   rtx note, next_note;
10705   rtx tem;
10706
10707   for (note = notes; note; note = next_note)
10708     {
10709       rtx place = 0, place2 = 0;
10710
10711       /* If this NOTE references a pseudo register, ensure it references
10712          the latest copy of that register.  */
10713       if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
10714           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
10715         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
10716
10717       next_note = XEXP (note, 1);
10718       switch (REG_NOTE_KIND (note))
10719         {
10720         case REG_UNUSED:
10721           /* Any clobbers for i3 may still exist, and so we must process
10722              REG_UNUSED notes from that insn.
10723
10724              Any clobbers from i2 or i1 can only exist if they were added by
10725              recog_for_combine.  In that case, recog_for_combine created the
10726              necessary REG_UNUSED notes.  Trying to keep any original
10727              REG_UNUSED notes from these insns can cause incorrect output
10728              if it is for the same register as the original i3 dest.
10729              In that case, we will notice that the register is set in i3,
10730              and then add a REG_UNUSED note for the destination of i3, which
10731              is wrong.  However, it is possible to have REG_UNUSED notes from
10732              i2 or i1 for register which were both used and clobbered, so
10733              we keep notes from i2 or i1 if they will turn into REG_DEAD
10734              notes.  */
10735
10736           /* If this register is set or clobbered in I3, put the note there
10737              unless there is one already.  */
10738           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
10739             {
10740               if (from_insn != i3)
10741                 break;
10742
10743               if (! (GET_CODE (XEXP (note, 0)) == REG
10744                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
10745                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
10746                 place = i3;
10747             }
10748           /* Otherwise, if this register is used by I3, then this register
10749              now dies here, so we must put a REG_DEAD note here unless there
10750              is one already.  */
10751           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
10752                    && ! (GET_CODE (XEXP (note, 0)) == REG
10753                          ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
10754                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
10755             {
10756               PUT_REG_NOTE_KIND (note, REG_DEAD);
10757               place = i3;
10758             }
10759           break;
10760
10761         case REG_EQUAL:
10762         case REG_EQUIV:
10763         case REG_NONNEG:
10764           /* These notes say something about results of an insn.  We can
10765              only support them if they used to be on I3 in which case they
10766              remain on I3.  Otherwise they are ignored.
10767
10768              If the note refers to an expression that is not a constant, we
10769              must also ignore the note since we cannot tell whether the
10770              equivalence is still true.  It might be possible to do
10771              slightly better than this (we only have a problem if I2DEST
10772              or I1DEST is present in the expression), but it doesn't
10773              seem worth the trouble.  */
10774
10775           if (from_insn == i3
10776               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
10777             place = i3;
10778           break;
10779
10780         case REG_INC:
10781         case REG_NO_CONFLICT:
10782         case REG_LABEL:
10783           /* These notes say something about how a register is used.  They must
10784              be present on any use of the register in I2 or I3.  */
10785           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
10786             place = i3;
10787
10788           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
10789             {
10790               if (place)
10791                 place2 = i2;
10792               else
10793                 place = i2;
10794             }
10795           break;
10796
10797         case REG_WAS_0:
10798           /* It is too much trouble to try to see if this note is still
10799              correct in all situations.  It is better to simply delete it.  */
10800           break;
10801
10802         case REG_RETVAL:
10803           /* If the insn previously containing this note still exists,
10804              put it back where it was.  Otherwise move it to the previous
10805              insn.  Adjust the corresponding REG_LIBCALL note.  */
10806           if (GET_CODE (from_insn) != NOTE)
10807             place = from_insn;
10808           else
10809             {
10810               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
10811               place = prev_real_insn (from_insn);
10812               if (tem && place)
10813                 XEXP (tem, 0) = place;
10814             }
10815           break;
10816
10817         case REG_LIBCALL:
10818           /* This is handled similarly to REG_RETVAL.  */
10819           if (GET_CODE (from_insn) != NOTE)
10820             place = from_insn;
10821           else
10822             {
10823               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
10824               place = next_real_insn (from_insn);
10825               if (tem && place)
10826                 XEXP (tem, 0) = place;
10827             }
10828           break;
10829
10830         case REG_DEAD:
10831           /* If the register is used as an input in I3, it dies there.
10832              Similarly for I2, if it is non-zero and adjacent to I3.
10833
10834              If the register is not used as an input in either I3 or I2
10835              and it is not one of the registers we were supposed to eliminate,
10836              there are two possibilities.  We might have a non-adjacent I2
10837              or we might have somehow eliminated an additional register
10838              from a computation.  For example, we might have had A & B where
10839              we discover that B will always be zero.  In this case we will
10840              eliminate the reference to A.
10841
10842              In both cases, we must search to see if we can find a previous
10843              use of A and put the death note there.  */
10844
10845           if (from_insn
10846               && GET_CODE (from_insn) == CALL_INSN
10847               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
10848             place = from_insn;
10849           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
10850             place = i3;
10851           else if (i2 != 0 && next_nonnote_insn (i2) == i3
10852                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
10853             place = i2;
10854
10855           if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
10856             break;
10857
10858           /* If the register is used in both I2 and I3 and it dies in I3, 
10859              we might have added another reference to it.  If reg_n_refs
10860              was 2, bump it to 3.  This has to be correct since the 
10861              register must have been set somewhere.  The reason this is
10862              done is because local-alloc.c treats 2 references as a 
10863              special case.  */
10864
10865           if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
10866               && reg_n_refs[REGNO (XEXP (note, 0))]== 2
10867               && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
10868             reg_n_refs[REGNO (XEXP (note, 0))] = 3;
10869
10870           if (place == 0)
10871             {
10872               for (tem = prev_nonnote_insn (i3);
10873                    place == 0 && tem
10874                    && (GET_CODE (tem) == INSN || GET_CODE (tem) == CALL_INSN);
10875                    tem = prev_nonnote_insn (tem))
10876                 {
10877                   /* If the register is being set at TEM, see if that is all
10878                      TEM is doing.  If so, delete TEM.  Otherwise, make this
10879                      into a REG_UNUSED note instead.  */
10880                   if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
10881                     {
10882                       rtx set = single_set (tem);
10883
10884                       /* Verify that it was the set, and not a clobber that
10885                          modified the register.  */
10886
10887                       if (set != 0 && ! side_effects_p (SET_SRC (set))
10888                           && (rtx_equal_p (XEXP (note, 0), SET_DEST (set))
10889                               || (GET_CODE (SET_DEST (set)) == SUBREG
10890                                   && rtx_equal_p (XEXP (note, 0),
10891                                                   XEXP (SET_DEST (set), 0)))))
10892                         {
10893                           /* Move the notes and links of TEM elsewhere.
10894                              This might delete other dead insns recursively. 
10895                              First set the pattern to something that won't use
10896                              any register.  */
10897
10898                           PATTERN (tem) = pc_rtx;
10899
10900                           distribute_notes (REG_NOTES (tem), tem, tem,
10901                                             NULL_RTX, NULL_RTX, NULL_RTX);
10902                           distribute_links (LOG_LINKS (tem));
10903
10904                           PUT_CODE (tem, NOTE);
10905                           NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
10906                           NOTE_SOURCE_FILE (tem) = 0;
10907                         }
10908                       else
10909                         {
10910                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
10911                           
10912                           /*  If there isn't already a REG_UNUSED note, put one
10913                               here.  */
10914                           if (! find_regno_note (tem, REG_UNUSED,
10915                                                  REGNO (XEXP (note, 0))))
10916                             place = tem;
10917                           break;
10918                       }
10919                   }
10920                 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
10921                          || (GET_CODE (tem) == CALL_INSN
10922                              && find_reg_fusage (tem, USE, XEXP (note, 0))))
10923                   {
10924                     place = tem;
10925
10926                     /* If we are doing a 3->2 combination, and we have a
10927                        register which formerly died in i3 and was not used
10928                        by i2, which now no longer dies in i3 and is used in
10929                        i2 but does not die in i2, and place is between i2
10930                        and i3, then we may need to move a link from place to
10931                        i2.  */
10932                     if (i2 && INSN_UID (place) <= max_uid_cuid
10933                         && INSN_CUID (place) > INSN_CUID (i2)
10934                         && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
10935                         && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
10936                       {
10937                         rtx links = LOG_LINKS (place);
10938                         LOG_LINKS (place) = 0;
10939                         distribute_links (links);
10940                       }
10941                     break;
10942                   }
10943                 }
10944               
10945               /* If we haven't found an insn for the death note and it
10946                  is still a REG_DEAD note, but we have hit a CODE_LABEL,
10947                  insert a USE insn for the register at that label and
10948                  put the death node there.  This prevents problems with
10949                  call-state tracking in caller-save.c.  */
10950               if (REG_NOTE_KIND (note) == REG_DEAD && place == 0 && tem != 0)
10951                 {
10952                   place
10953                     = emit_insn_after (gen_rtx (USE, VOIDmode, XEXP (note, 0)),
10954                                        tem);
10955
10956                   /* If this insn was emitted between blocks, then update
10957                      basic_block_head of the current block to include it.  */
10958                   if (basic_block_end[this_basic_block - 1] == tem)
10959                     basic_block_head[this_basic_block] = place;
10960                 }
10961             }
10962
10963           /* If the register is set or already dead at PLACE, we needn't do
10964              anything with this note if it is still a REG_DEAD note.  
10965
10966              Note that we cannot use just `dead_or_set_p' here since we can
10967              convert an assignment to a register into a bit-field assignment.
10968              Therefore, we must also omit the note if the register is the 
10969              target of a bitfield assignment.  */
10970              
10971           if (place && REG_NOTE_KIND (note) == REG_DEAD)
10972             {
10973               int regno = REGNO (XEXP (note, 0));
10974
10975               if (dead_or_set_p (place, XEXP (note, 0))
10976                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
10977                 {
10978                   /* Unless the register previously died in PLACE, clear
10979                      reg_last_death.  [I no longer understand why this is
10980                      being done.] */
10981                   if (reg_last_death[regno] != place)
10982                     reg_last_death[regno] = 0;
10983                   place = 0;
10984                 }
10985               else
10986                 reg_last_death[regno] = place;
10987
10988               /* If this is a death note for a hard reg that is occupying
10989                  multiple registers, ensure that we are still using all
10990                  parts of the object.  If we find a piece of the object
10991                  that is unused, we must add a USE for that piece before
10992                  PLACE and put the appropriate REG_DEAD note on it.
10993
10994                  An alternative would be to put a REG_UNUSED for the pieces
10995                  on the insn that set the register, but that can't be done if
10996                  it is not in the same block.  It is simpler, though less
10997                  efficient, to add the USE insns.  */
10998
10999               if (place && regno < FIRST_PSEUDO_REGISTER
11000                   && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
11001                 {
11002                   int endregno
11003                     = regno + HARD_REGNO_NREGS (regno,
11004                                                 GET_MODE (XEXP (note, 0)));
11005                   int all_used = 1;
11006                   int i;
11007
11008                   for (i = regno; i < endregno; i++)
11009                     if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
11010                         && ! find_regno_fusage (place, USE, i))
11011                       {
11012                         rtx piece = gen_rtx (REG, reg_raw_mode[i], i);
11013                         rtx p;
11014
11015                         /* See if we already placed a USE note for this
11016                            register in front of PLACE.  */
11017                         for (p = place;
11018                              GET_CODE (PREV_INSN (p)) == INSN
11019                              && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
11020                              p = PREV_INSN (p))
11021                           if (rtx_equal_p (piece,
11022                                            XEXP (PATTERN (PREV_INSN (p)), 0)))
11023                             {
11024                               p = 0;
11025                               break;
11026                             }
11027
11028                         if (p)
11029                           {
11030                             rtx use_insn
11031                               = emit_insn_before (gen_rtx (USE, VOIDmode,
11032                                                            piece),
11033                                                   p);
11034                             REG_NOTES (use_insn)
11035                               = gen_rtx (EXPR_LIST, REG_DEAD, piece,
11036                                          REG_NOTES (use_insn));
11037                           }
11038
11039                         all_used = 0;
11040                       }
11041
11042                   /* Check for the case where the register dying partially
11043                      overlaps the register set by this insn.  */
11044                   if (all_used)
11045                     for (i = regno; i < endregno; i++)
11046                       if (dead_or_set_regno_p (place, i))
11047                           {
11048                             all_used = 0;
11049                             break;
11050                           }
11051
11052                   if (! all_used)
11053                     {
11054                       /* Put only REG_DEAD notes for pieces that are
11055                          still used and that are not already dead or set.  */
11056
11057                       for (i = regno; i < endregno; i++)
11058                         {
11059                           rtx piece = gen_rtx (REG, reg_raw_mode[i], i);
11060
11061                           if ((reg_referenced_p (piece, PATTERN (place))
11062                                || (GET_CODE (place) == CALL_INSN
11063                                    && find_reg_fusage (place, USE, piece)))
11064                               && ! dead_or_set_p (place, piece)
11065                               && ! reg_bitfield_target_p (piece,
11066                                                           PATTERN (place)))
11067                             REG_NOTES (place) = gen_rtx (EXPR_LIST, REG_DEAD,
11068                                                          piece,
11069                                                          REG_NOTES (place));
11070                         }
11071
11072                       place = 0;
11073                     }
11074                 }
11075             }
11076           break;
11077
11078         default:
11079           /* Any other notes should not be present at this point in the
11080              compilation.  */
11081           abort ();
11082         }
11083
11084       if (place)
11085         {
11086           XEXP (note, 1) = REG_NOTES (place);
11087           REG_NOTES (place) = note;
11088         }
11089       else if ((REG_NOTE_KIND (note) == REG_DEAD
11090                 || REG_NOTE_KIND (note) == REG_UNUSED)
11091                && GET_CODE (XEXP (note, 0)) == REG)
11092         reg_n_deaths[REGNO (XEXP (note, 0))]--;
11093
11094       if (place2)
11095         {
11096           if ((REG_NOTE_KIND (note) == REG_DEAD
11097                || REG_NOTE_KIND (note) == REG_UNUSED)
11098               && GET_CODE (XEXP (note, 0)) == REG)
11099             reg_n_deaths[REGNO (XEXP (note, 0))]++;
11100
11101           REG_NOTES (place2) = gen_rtx (GET_CODE (note), REG_NOTE_KIND (note),
11102                                         XEXP (note, 0), REG_NOTES (place2));
11103         }
11104     }
11105 }
11106 \f
11107 /* Similarly to above, distribute the LOG_LINKS that used to be present on
11108    I3, I2, and I1 to new locations.  This is also called in one case to
11109    add a link pointing at I3 when I3's destination is changed.  */
11110
11111 static void
11112 distribute_links (links)
11113      rtx links;
11114 {
11115   rtx link, next_link;
11116
11117   for (link = links; link; link = next_link)
11118     {
11119       rtx place = 0;
11120       rtx insn;
11121       rtx set, reg;
11122
11123       next_link = XEXP (link, 1);
11124
11125       /* If the insn that this link points to is a NOTE or isn't a single
11126          set, ignore it.  In the latter case, it isn't clear what we
11127          can do other than ignore the link, since we can't tell which 
11128          register it was for.  Such links wouldn't be used by combine
11129          anyway.
11130
11131          It is not possible for the destination of the target of the link to
11132          have been changed by combine.  The only potential of this is if we
11133          replace I3, I2, and I1 by I3 and I2.  But in that case the
11134          destination of I2 also remains unchanged.  */
11135
11136       if (GET_CODE (XEXP (link, 0)) == NOTE
11137           || (set = single_set (XEXP (link, 0))) == 0)
11138         continue;
11139
11140       reg = SET_DEST (set);
11141       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
11142              || GET_CODE (reg) == SIGN_EXTRACT
11143              || GET_CODE (reg) == STRICT_LOW_PART)
11144         reg = XEXP (reg, 0);
11145
11146       /* A LOG_LINK is defined as being placed on the first insn that uses
11147          a register and points to the insn that sets the register.  Start
11148          searching at the next insn after the target of the link and stop
11149          when we reach a set of the register or the end of the basic block.
11150
11151          Note that this correctly handles the link that used to point from
11152          I3 to I2.  Also note that not much searching is typically done here
11153          since most links don't point very far away.  */
11154
11155       for (insn = NEXT_INSN (XEXP (link, 0));
11156            (insn && (this_basic_block == n_basic_blocks - 1
11157                      || basic_block_head[this_basic_block + 1] != insn));
11158            insn = NEXT_INSN (insn))
11159         if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
11160             && reg_overlap_mentioned_p (reg, PATTERN (insn)))
11161           {
11162             if (reg_referenced_p (reg, PATTERN (insn)))
11163               place = insn;
11164             break;
11165           }
11166         else if (GET_CODE (insn) == CALL_INSN
11167               && find_reg_fusage (insn, USE, reg))
11168           {
11169             place = insn;
11170             break;
11171           }
11172
11173       /* If we found a place to put the link, place it there unless there
11174          is already a link to the same insn as LINK at that point.  */
11175
11176       if (place)
11177         {
11178           rtx link2;
11179
11180           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
11181             if (XEXP (link2, 0) == XEXP (link, 0))
11182               break;
11183
11184           if (link2 == 0)
11185             {
11186               XEXP (link, 1) = LOG_LINKS (place);
11187               LOG_LINKS (place) = link;
11188
11189               /* Set added_links_insn to the earliest insn we added a
11190                  link to.  */
11191               if (added_links_insn == 0 
11192                   || INSN_CUID (added_links_insn) > INSN_CUID (place))
11193                 added_links_insn = place;
11194             }
11195         }
11196     }
11197 }
11198 \f
11199 /* Compute INSN_CUID for INSN, which is an insn made by combine.  */
11200
11201 static int
11202 insn_cuid (insn)
11203      rtx insn;
11204 {
11205   while (insn != 0 && INSN_UID (insn) > max_uid_cuid
11206          && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
11207     insn = NEXT_INSN (insn);
11208
11209   if (INSN_UID (insn) > max_uid_cuid)
11210     abort ();
11211
11212   return INSN_CUID (insn);
11213 }
11214 \f
11215 void
11216 dump_combine_stats (file)
11217      FILE *file;
11218 {
11219   fprintf
11220     (file,
11221      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
11222      combine_attempts, combine_merges, combine_extras, combine_successes);
11223 }
11224
11225 void
11226 dump_combine_total_stats (file)
11227      FILE *file;
11228 {
11229   fprintf
11230     (file,
11231      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
11232      total_attempts, total_merges, total_extras, total_successes);
11233 }