OSDN Git Service

Include stdio.h before rtl.h.
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* This module is essentially the "combiner" phase of the U. of Arizona
22    Portable Optimizer, but redone to work on our list-structured
23    representation for RTL instead of their string representation.
24
25    The LOG_LINKS of each insn identify the most recent assignment
26    to each REG used in the insn.  It is a list of previous insns,
27    each of which contains a SET for a REG that is used in this insn
28    and not used or set in between.  LOG_LINKs never cross basic blocks.
29    They were set up by the preceding pass (lifetime analysis).
30
31    We try to combine each pair of insns joined by a logical link.
32    We also try to combine triples of insns A, B and C when
33    C has a link back to B and B has a link back to A.
34
35    LOG_LINKS does not have links for use of the CC0.  They don't
36    need to, because the insn that sets the CC0 is always immediately
37    before the insn that tests it.  So we always regard a branch
38    insn as having a logical link to the preceding insn.  The same is true
39    for an insn explicitly using CC0.
40
41    We check (with use_crosses_set_p) to avoid combining in such a way
42    as to move a computation to a place where its value would be different.
43
44    Combination is done by mathematically substituting the previous
45    insn(s) values for the regs they set into the expressions in
46    the later insns that refer to these regs.  If the result is a valid insn
47    for our target machine, according to the machine description,
48    we install it, delete the earlier insns, and update the data flow
49    information (LOG_LINKS and REG_NOTES) for what we did.
50
51    There are a few exceptions where the dataflow information created by
52    flow.c aren't completely updated:
53
54    - reg_live_length is not updated
55    - reg_n_refs is not adjusted in the rare case when a register is
56      no longer required in a computation
57    - there are extremely rare cases (see distribute_regnotes) when a
58      REG_DEAD note is lost
59    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
60      removed because there is no way to know which register it was 
61      linking
62
63    To simplify substitution, we combine only when the earlier insn(s)
64    consist of only a single assignment.  To simplify updating afterward,
65    we never combine when a subroutine call appears in the middle.
66
67    Since we do not represent assignments to CC0 explicitly except when that
68    is all an insn does, there is no LOG_LINKS entry in an insn that uses
69    the condition code for the insn that set the condition code.
70    Fortunately, these two insns must be consecutive.
71    Therefore, every JUMP_INSN is taken to have an implicit logical link
72    to the preceding insn.  This is not quite right, since non-jumps can
73    also use the condition code; but in practice such insns would not
74    combine anyway.  */
75
76 #include "config.h"
77 /* Must precede rtl.h for FFS.  */
78 #include <stdio.h>
79
80 #include "gvarargs.h"
81 #include "rtl.h"
82 #include "flags.h"
83 #include "regs.h"
84 #include "hard-reg-set.h"
85 #include "expr.h"
86 #include "basic-block.h"
87 #include "insn-config.h"
88 #include "insn-flags.h"
89 #include "insn-codes.h"
90 #include "insn-attr.h"
91 #include "recog.h"
92 #include "real.h"
93
94 /* It is not safe to use ordinary gen_lowpart in combine.
95    Use gen_lowpart_for_combine instead.  See comments there.  */
96 #define gen_lowpart dont_use_gen_lowpart_you_dummy
97
98 /* If byte loads either zero- or sign- extend, define BYTE_LOADS_EXTEND
99    for cases when we don't care which is true.  Define LOAD_EXTEND to
100    be ZERO_EXTEND or SIGN_EXTEND, depending on which was defined.  */
101
102 #ifdef BYTE_LOADS_ZERO_EXTEND
103 #define BYTE_LOADS_EXTEND
104 #define LOAD_EXTEND ZERO_EXTEND
105 #endif
106
107 #ifdef BYTE_LOADS_SIGN_EXTEND
108 #define BYTE_LOADS_EXTEND
109 #define LOAD_EXTEND SIGN_EXTEND
110 #endif
111
112 /* Number of attempts to combine instructions in this function.  */
113
114 static int combine_attempts;
115
116 /* Number of attempts that got as far as substitution in this function.  */
117
118 static int combine_merges;
119
120 /* Number of instructions combined with added SETs in this function.  */
121
122 static int combine_extras;
123
124 /* Number of instructions combined in this function.  */
125
126 static int combine_successes;
127
128 /* Totals over entire compilation.  */
129
130 static int total_attempts, total_merges, total_extras, total_successes;
131 \f
132 /* Vector mapping INSN_UIDs to cuids.
133    The cuids are like uids but increase monotonically always.
134    Combine always uses cuids so that it can compare them.
135    But actually renumbering the uids, which we used to do,
136    proves to be a bad idea because it makes it hard to compare
137    the dumps produced by earlier passes with those from later passes.  */
138
139 static int *uid_cuid;
140
141 /* Get the cuid of an insn.  */
142
143 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
144
145 /* Maximum register number, which is the size of the tables below.  */
146
147 static int combine_max_regno;
148
149 /* Record last point of death of (hard or pseudo) register n.  */
150
151 static rtx *reg_last_death;
152
153 /* Record last point of modification of (hard or pseudo) register n.  */
154
155 static rtx *reg_last_set;
156
157 /* Record the cuid of the last insn that invalidated memory
158    (anything that writes memory, and subroutine calls, but not pushes).  */
159
160 static int mem_last_set;
161
162 /* Record the cuid of the last CALL_INSN
163    so we can tell whether a potential combination crosses any calls.  */
164
165 static int last_call_cuid;
166
167 /* When `subst' is called, this is the insn that is being modified
168    (by combining in a previous insn).  The PATTERN of this insn
169    is still the old pattern partially modified and it should not be
170    looked at, but this may be used to examine the successors of the insn
171    to judge whether a simplification is valid.  */
172
173 static rtx subst_insn;
174
175 /* This is the lowest CUID that `subst' is currently dealing with.
176    get_last_value will not return a value if the register was set at or
177    after this CUID.  If not for this mechanism, we could get confused if
178    I2 or I1 in try_combine were an insn that used the old value of a register
179    to obtain a new value.  In that case, we might erroneously get the
180    new value of the register when we wanted the old one.  */
181
182 static int subst_low_cuid;
183
184 /* This is the value of undobuf.num_undo when we started processing this 
185    substitution.  This will prevent gen_rtx_combine from re-used a piece
186    from the previous expression.  Doing so can produce circular rtl
187    structures.  */
188
189 static int previous_num_undos;
190 \f
191 /* The next group of arrays allows the recording of the last value assigned
192    to (hard or pseudo) register n.  We use this information to see if a
193    operation being processed is redundant given a prior operation performed
194    on the register.  For example, an `and' with a constant is redundant if
195    all the zero bits are already known to be turned off.
196
197    We use an approach similar to that used by cse, but change it in the
198    following ways:
199
200    (1) We do not want to reinitialize at each label.
201    (2) It is useful, but not critical, to know the actual value assigned
202        to a register.  Often just its form is helpful.
203
204    Therefore, we maintain the following arrays:
205
206    reg_last_set_value           the last value assigned
207    reg_last_set_label           records the value of label_tick when the
208                                 register was assigned
209    reg_last_set_table_tick      records the value of label_tick when a
210                                 value using the register is assigned
211    reg_last_set_invalid         set to non-zero when it is not valid
212                                 to use the value of this register in some
213                                 register's value
214
215    To understand the usage of these tables, it is important to understand
216    the distinction between the value in reg_last_set_value being valid
217    and the register being validly contained in some other expression in the
218    table.
219
220    Entry I in reg_last_set_value is valid if it is non-zero, and either
221    reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
222
223    Register I may validly appear in any expression returned for the value
224    of another register if reg_n_sets[i] is 1.  It may also appear in the
225    value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
226    reg_last_set_invalid[j] is zero.
227
228    If an expression is found in the table containing a register which may
229    not validly appear in an expression, the register is replaced by
230    something that won't match, (clobber (const_int 0)).
231
232    reg_last_set_invalid[i] is set non-zero when register I is being assigned
233    to and reg_last_set_table_tick[i] == label_tick.  */
234
235 /* Record last value assigned to (hard or pseudo) register n. */
236
237 static rtx *reg_last_set_value;
238
239 /* Record the value of label_tick when the value for register n is placed in
240    reg_last_set_value[n].  */
241
242 static int *reg_last_set_label;
243
244 /* Record the value of label_tick when an expression involving register n
245    is placed in reg_last_set_value. */
246
247 static int *reg_last_set_table_tick;
248
249 /* Set non-zero if references to register n in expressions should not be
250    used.  */
251
252 static char *reg_last_set_invalid;
253
254 /* Incremented for each label. */
255
256 static int label_tick;
257
258 /* Some registers that are set more than once and used in more than one
259    basic block are nevertheless always set in similar ways.  For example,
260    a QImode register may be loaded from memory in two places on a machine
261    where byte loads zero extend.
262
263    We record in the following array what we know about the nonzero
264    bits of a register, specifically which bits are known to be zero.
265
266    If an entry is zero, it means that we don't know anything special.  */
267
268 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
269
270 /* Mode used to compute significance in reg_nonzero_bits.  It is the largest
271    integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
272
273 static enum machine_mode nonzero_bits_mode;
274
275 /* Nonzero if we know that a register has some leading bits that are always
276    equal to the sign bit.  */
277
278 static char *reg_sign_bit_copies;
279
280 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
281    It is zero while computing them and after combine has completed.  This
282    former test prevents propagating values based on previously set values,
283    which can be incorrect if a variable is modified in a loop.  */
284
285 static int nonzero_sign_valid;
286
287 /* These arrays are maintained in parallel with reg_last_set_value
288    and are used to store the mode in which the register was last set,
289    the bits that were known to be zero when it was last set, and the
290    number of sign bits copies it was known to have when it was last set.  */
291
292 static enum machine_mode *reg_last_set_mode;
293 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
294 static char *reg_last_set_sign_bit_copies;
295 \f
296 /* Record one modification to rtl structure
297    to be undone by storing old_contents into *where.
298    is_int is 1 if the contents are an int.  */
299
300 struct undo
301 {
302   int is_int;
303   union {rtx rtx; int i;} old_contents;
304   union {rtx *rtx; int *i;} where;
305 };
306
307 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
308    num_undo says how many are currently recorded.
309
310    storage is nonzero if we must undo the allocation of new storage.
311    The value of storage is what to pass to obfree.
312
313    other_insn is nonzero if we have modified some other insn in the process
314    of working on subst_insn.  It must be verified too.  */
315
316 #define MAX_UNDO 50
317
318 struct undobuf
319 {
320   int num_undo;
321   char *storage;
322   struct undo undo[MAX_UNDO];
323   rtx other_insn;
324 };
325
326 static struct undobuf undobuf;
327
328 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
329    insn.  The substitution can be undone by undo_all.  If INTO is already
330    set to NEWVAL, do not record this change.  Because computing NEWVAL might
331    also call SUBST, we have to compute it before we put anything into
332    the undo table.  */
333
334 #define SUBST(INTO, NEWVAL)  \
335  do { rtx _new = (NEWVAL);                                              \
336       if (undobuf.num_undo < MAX_UNDO)                                  \
337         {                                                               \
338           undobuf.undo[undobuf.num_undo].is_int = 0;                    \
339           undobuf.undo[undobuf.num_undo].where.rtx = &INTO;             \
340           undobuf.undo[undobuf.num_undo].old_contents.rtx = INTO;       \
341           INTO = _new;                                                  \
342           if (undobuf.undo[undobuf.num_undo].old_contents.rtx != INTO)  \
343             undobuf.num_undo++;                                         \
344         }                                                               \
345     } while (0)
346
347 /* Similar to SUBST, but NEWVAL is an int.  INTO will normally be an XINT
348    expression.
349    Note that substitution for the value of a CONST_INT is not safe.  */
350
351 #define SUBST_INT(INTO, NEWVAL)  \
352  do { if (undobuf.num_undo < MAX_UNDO)                                  \
353 {                                                                       \
354           undobuf.undo[undobuf.num_undo].is_int = 1;                    \
355           undobuf.undo[undobuf.num_undo].where.i = (int *) &INTO;       \
356           undobuf.undo[undobuf.num_undo].old_contents.i = INTO;         \
357           INTO = NEWVAL;                                                \
358           if (undobuf.undo[undobuf.num_undo].old_contents.i != INTO)    \
359             undobuf.num_undo++;                                         \
360         }                                                               \
361      } while (0)
362
363 /* Number of times the pseudo being substituted for
364    was found and replaced.  */
365
366 static int n_occurrences;
367
368 static void set_nonzero_bits_and_sign_copies ();
369 static void setup_incoming_promotions ();
370 static void move_deaths ();
371 rtx remove_death ();
372 static void record_value_for_reg ();
373 static void record_dead_and_set_regs ();
374 static int use_crosses_set_p ();
375 static rtx try_combine ();
376 static rtx *find_split_point ();
377 static rtx subst ();
378 static void undo_all ();
379 static int reg_dead_at_p ();
380 static rtx expand_compound_operation ();
381 static rtx expand_field_assignment ();
382 static rtx make_extraction ();
383 static int get_pos_from_mask ();
384 static rtx force_to_mode ();
385 static rtx known_cond ();
386 static rtx make_field_assignment ();
387 static rtx make_compound_operation ();
388 static rtx apply_distributive_law ();
389 static rtx simplify_and_const_int ();
390 static unsigned HOST_WIDE_INT nonzero_bits ();
391 static int num_sign_bit_copies ();
392 static int merge_outer_ops ();
393 static rtx simplify_shift_const ();
394 static int recog_for_combine ();
395 static rtx gen_lowpart_for_combine ();
396 static rtx gen_rtx_combine ();
397 static rtx gen_binary ();
398 static rtx gen_unary ();
399 static enum rtx_code simplify_comparison ();
400 static int reversible_comparison_p ();
401 static int get_last_value_validate ();
402 static rtx get_last_value ();
403 static void distribute_notes ();
404 static void distribute_links ();
405 \f
406 /* Main entry point for combiner.  F is the first insn of the function.
407    NREGS is the first unused pseudo-reg number.  */
408
409 void
410 combine_instructions (f, nregs)
411      rtx f;
412      int nregs;
413 {
414   register rtx insn, next, prev;
415   register int i;
416   register rtx links, nextlinks;
417
418   combine_attempts = 0;
419   combine_merges = 0;
420   combine_extras = 0;
421   combine_successes = 0;
422   undobuf.num_undo = previous_num_undos = 0;
423
424   combine_max_regno = nregs;
425
426   reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
427   reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
428   reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
429   reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
430   reg_last_set_label = (int *) alloca (nregs * sizeof (int));
431   reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
432   reg_last_set_mode
433     = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
434   reg_last_set_nonzero_bits
435     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
436   reg_last_set_sign_bit_copies
437     = (char *) alloca (nregs * sizeof (char));
438
439   reg_nonzero_bits
440     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
441   reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
442
443   bzero (reg_last_death, nregs * sizeof (rtx));
444   bzero (reg_last_set, nregs * sizeof (rtx));
445   bzero (reg_last_set_value, nregs * sizeof (rtx));
446   bzero (reg_last_set_table_tick, nregs * sizeof (int));
447   bzero (reg_last_set_label, nregs * sizeof (int));
448   bzero (reg_last_set_invalid, nregs * sizeof (char));
449   bzero (reg_last_set_mode, nregs * sizeof (enum machine_mode));
450   bzero (reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
451   bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
452   bzero (reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
453   bzero (reg_sign_bit_copies, nregs * sizeof (char));
454
455   init_recog_no_volatile ();
456
457   /* Compute maximum uid value so uid_cuid can be allocated.  */
458
459   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
460     if (INSN_UID (insn) > i)
461       i = INSN_UID (insn);
462
463   uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
464
465   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
466
467   /* Don't use reg_nonzero_bits when computing it.  This can cause problems
468      when, for example, we have j <<= 1 in a loop.  */
469
470   nonzero_sign_valid = 0;
471
472   /* Compute the mapping from uids to cuids.
473      Cuids are numbers assigned to insns, like uids,
474      except that cuids increase monotonically through the code. 
475
476      Scan all SETs and see if we can deduce anything about what
477      bits are known to be zero for some registers and how many copies
478      of the sign bit are known to exist for those registers.
479
480      Also set any known values so that we can use it while searching
481      for what bits are known to be set.  */
482
483   label_tick = 1;
484
485   setup_incoming_promotions ();
486
487   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
488     {
489       INSN_CUID (insn) = ++i;
490       subst_low_cuid = i;
491       subst_insn = insn;
492
493       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
494         {
495           note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
496           record_dead_and_set_regs (insn);
497         }
498
499       if (GET_CODE (insn) == CODE_LABEL)
500         label_tick++;
501     }
502
503   nonzero_sign_valid = 1;
504
505   /* Now scan all the insns in forward order.  */
506
507   label_tick = 1;
508   last_call_cuid = 0;
509   mem_last_set = 0;
510   bzero (reg_last_death, nregs * sizeof (rtx));
511   bzero (reg_last_set, nregs * sizeof (rtx));
512   bzero (reg_last_set_value, nregs * sizeof (rtx));
513   bzero (reg_last_set_table_tick, nregs * sizeof (int));
514   bzero (reg_last_set_label, nregs * sizeof (int));
515   bzero (reg_last_set_invalid, nregs * sizeof (char));
516
517   setup_incoming_promotions ();
518
519   for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
520     {
521       next = 0;
522
523       if (GET_CODE (insn) == CODE_LABEL)
524         label_tick++;
525
526       else if (GET_CODE (insn) == INSN
527                || GET_CODE (insn) == CALL_INSN
528                || GET_CODE (insn) == JUMP_INSN)
529         {
530           /* Try this insn with each insn it links back to.  */
531
532           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
533             if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
534               goto retry;
535
536           /* Try each sequence of three linked insns ending with this one.  */
537
538           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
539             for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
540                  nextlinks = XEXP (nextlinks, 1))
541               if ((next = try_combine (insn, XEXP (links, 0),
542                                        XEXP (nextlinks, 0))) != 0)
543                 goto retry;
544
545 #ifdef HAVE_cc0
546           /* Try to combine a jump insn that uses CC0
547              with a preceding insn that sets CC0, and maybe with its
548              logical predecessor as well.
549              This is how we make decrement-and-branch insns.
550              We need this special code because data flow connections
551              via CC0 do not get entered in LOG_LINKS.  */
552
553           if (GET_CODE (insn) == JUMP_INSN
554               && (prev = prev_nonnote_insn (insn)) != 0
555               && GET_CODE (prev) == INSN
556               && sets_cc0_p (PATTERN (prev)))
557             {
558               if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
559                 goto retry;
560
561               for (nextlinks = LOG_LINKS (prev); nextlinks;
562                    nextlinks = XEXP (nextlinks, 1))
563                 if ((next = try_combine (insn, prev,
564                                          XEXP (nextlinks, 0))) != 0)
565                   goto retry;
566             }
567
568           /* Do the same for an insn that explicitly references CC0.  */
569           if (GET_CODE (insn) == INSN
570               && (prev = prev_nonnote_insn (insn)) != 0
571               && GET_CODE (prev) == INSN
572               && sets_cc0_p (PATTERN (prev))
573               && GET_CODE (PATTERN (insn)) == SET
574               && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
575             {
576               if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
577                 goto retry;
578
579               for (nextlinks = LOG_LINKS (prev); nextlinks;
580                    nextlinks = XEXP (nextlinks, 1))
581                 if ((next = try_combine (insn, prev,
582                                          XEXP (nextlinks, 0))) != 0)
583                   goto retry;
584             }
585
586           /* Finally, see if any of the insns that this insn links to
587              explicitly references CC0.  If so, try this insn, that insn,
588              and its predecessor if it sets CC0.  */
589           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
590             if (GET_CODE (XEXP (links, 0)) == INSN
591                 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
592                 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
593                 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
594                 && GET_CODE (prev) == INSN
595                 && sets_cc0_p (PATTERN (prev))
596                 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
597               goto retry;
598 #endif
599
600           /* Try combining an insn with two different insns whose results it
601              uses.  */
602           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
603             for (nextlinks = XEXP (links, 1); nextlinks;
604                  nextlinks = XEXP (nextlinks, 1))
605               if ((next = try_combine (insn, XEXP (links, 0),
606                                        XEXP (nextlinks, 0))) != 0)
607                 goto retry;
608
609           if (GET_CODE (insn) != NOTE)
610             record_dead_and_set_regs (insn);
611
612         retry:
613           ;
614         }
615     }
616
617   total_attempts += combine_attempts;
618   total_merges += combine_merges;
619   total_extras += combine_extras;
620   total_successes += combine_successes;
621
622   nonzero_sign_valid = 0;
623 }
624 \f
625 /* Set up any promoted values for incoming argument registers.  */
626
627 static void
628 setup_incoming_promotions ()
629 {
630 #ifdef PROMOTE_FUNCTION_ARGS
631   int regno;
632   rtx reg;
633   enum machine_mode mode;
634   int unsignedp;
635   rtx first = get_insns ();
636
637   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
638     if (FUNCTION_ARG_REGNO_P (regno)
639         && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
640       record_value_for_reg (reg, first,
641                             gen_rtx (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
642                                      GET_MODE (reg),
643                                      gen_rtx (CLOBBER, mode, const0_rtx)));
644 #endif
645 }
646 \f
647 /* Called via note_stores.  If X is a pseudo that is used in more than
648    one basic block, is narrower that HOST_BITS_PER_WIDE_INT, and is being
649    set, record what bits are known zero.  If we are clobbering X,
650    ignore this "set" because the clobbered value won't be used. 
651
652    If we are setting only a portion of X and we can't figure out what
653    portion, assume all bits will be used since we don't know what will
654    be happening.
655
656    Similarly, set how many bits of X are known to be copies of the sign bit
657    at all locations in the function.  This is the smallest number implied 
658    by any set of X.  */
659
660 static void
661 set_nonzero_bits_and_sign_copies (x, set)
662      rtx x;
663      rtx set;
664 {
665   int num;
666
667   if (GET_CODE (x) == REG
668       && REGNO (x) >= FIRST_PSEUDO_REGISTER
669       && reg_n_sets[REGNO (x)] > 1
670       && reg_basic_block[REGNO (x)] < 0
671       /* If this register is undefined at the start of the file, we can't
672          say what its contents were.  */
673       && ! (basic_block_live_at_start[0][REGNO (x) / REGSET_ELT_BITS]
674             & ((REGSET_ELT_TYPE) 1 << (REGNO (x) % REGSET_ELT_BITS)))
675       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
676     {
677       if (GET_CODE (set) == CLOBBER)
678         {
679           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
680           reg_sign_bit_copies[REGNO (x)] = 0;
681           return;
682         }
683
684       /* If this is a complex assignment, see if we can convert it into a
685          simple assignment.  */
686       set = expand_field_assignment (set);
687
688       /* If this is a simple assignment, or we have a paradoxical SUBREG,
689          set what we know about X.  */
690
691       if (SET_DEST (set) == x
692           || (GET_CODE (SET_DEST (set)) == SUBREG
693               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
694                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
695               && SUBREG_REG (SET_DEST (set)) == x))
696         {
697           rtx src = SET_SRC (set);
698
699 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
700           /* If X is narrower than a word and SRC is a non-negative
701              constant that would appear negative in the mode of X,
702              sign-extend it for use in reg_nonzero_bits because some
703              machines (maybe most) will actually do the sign-extension
704              and this is the conservative approach. 
705
706              ??? For 2.5, try to tighten up the MD files in this regard
707              instead of this kludge.  */
708
709           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
710               && GET_CODE (src) == CONST_INT
711               && INTVAL (src) > 0
712               && 0 != (INTVAL (src)
713                        & ((HOST_WIDE_INT) 1
714                           << GET_MODE_BITSIZE (GET_MODE (x)))))
715             src = GEN_INT (INTVAL (src)
716                            | ((HOST_WIDE_INT) (-1)
717                               << GET_MODE_BITSIZE (GET_MODE (x))));
718 #endif
719
720           reg_nonzero_bits[REGNO (x)]
721             |= nonzero_bits (src, nonzero_bits_mode);
722           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
723           if (reg_sign_bit_copies[REGNO (x)] == 0
724               || reg_sign_bit_copies[REGNO (x)] > num)
725             reg_sign_bit_copies[REGNO (x)] = num;
726         }
727       else
728         {
729           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
730           reg_sign_bit_copies[REGNO (x)] = 0;
731         }
732     }
733 }
734 \f
735 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
736    insns that were previously combined into I3 or that will be combined
737    into the merger of INSN and I3.
738
739    Return 0 if the combination is not allowed for any reason.
740
741    If the combination is allowed, *PDEST will be set to the single 
742    destination of INSN and *PSRC to the single source, and this function
743    will return 1.  */
744
745 static int
746 can_combine_p (insn, i3, pred, succ, pdest, psrc)
747      rtx insn;
748      rtx i3;
749      rtx pred, succ;
750      rtx *pdest, *psrc;
751 {
752   int i;
753   rtx set = 0, src, dest;
754   rtx p, link;
755   int all_adjacent = (succ ? (next_active_insn (insn) == succ
756                               && next_active_insn (succ) == i3)
757                       : next_active_insn (insn) == i3);
758
759   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
760      or a PARALLEL consisting of such a SET and CLOBBERs. 
761
762      If INSN has CLOBBER parallel parts, ignore them for our processing.
763      By definition, these happen during the execution of the insn.  When it
764      is merged with another insn, all bets are off.  If they are, in fact,
765      needed and aren't also supplied in I3, they may be added by
766      recog_for_combine.  Otherwise, it won't match. 
767
768      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
769      note.
770
771      Get the source and destination of INSN.  If more than one, can't 
772      combine.  */
773      
774   if (GET_CODE (PATTERN (insn)) == SET)
775     set = PATTERN (insn);
776   else if (GET_CODE (PATTERN (insn)) == PARALLEL
777            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
778     {
779       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
780         {
781           rtx elt = XVECEXP (PATTERN (insn), 0, i);
782
783           switch (GET_CODE (elt))
784             {
785               /* We can ignore CLOBBERs.  */
786             case CLOBBER:
787               break;
788
789             case SET:
790               /* Ignore SETs whose result isn't used but not those that
791                  have side-effects.  */
792               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
793                   && ! side_effects_p (elt))
794                 break;
795
796               /* If we have already found a SET, this is a second one and
797                  so we cannot combine with this insn.  */
798               if (set)
799                 return 0;
800
801               set = elt;
802               break;
803
804             default:
805               /* Anything else means we can't combine.  */
806               return 0;
807             }
808         }
809
810       if (set == 0
811           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
812              so don't do anything with it.  */
813           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
814         return 0;
815     }
816   else
817     return 0;
818
819   if (set == 0)
820     return 0;
821
822   set = expand_field_assignment (set);
823   src = SET_SRC (set), dest = SET_DEST (set);
824
825   /* Don't eliminate a store in the stack pointer.  */
826   if (dest == stack_pointer_rtx
827       /* Don't install a subreg involving two modes not tieable.
828          It can worsen register allocation, and can even make invalid reload
829          insns, since the reg inside may need to be copied from in the
830          outside mode, and that may be invalid if it is an fp reg copied in
831          integer mode.  As a special exception, we can allow this if
832          I3 is simply copying DEST, a REG,  to CC0.  */
833       || (GET_CODE (src) == SUBREG
834           && ! MODES_TIEABLE_P (GET_MODE (src), GET_MODE (SUBREG_REG (src)))
835 #ifdef HAVE_cc0
836           && ! (GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
837                 && SET_DEST (PATTERN (i3)) == cc0_rtx
838                 && GET_CODE (dest) == REG && dest == SET_SRC (PATTERN (i3)))
839 #endif
840           )
841       /* If we couldn't eliminate a field assignment, we can't combine.  */
842       || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
843       /* Don't combine with an insn that sets a register to itself if it has
844          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
845       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
846       /* Can't merge a function call.  */
847       || GET_CODE (src) == CALL
848       /* Don't substitute into an incremented register.  */
849       || FIND_REG_INC_NOTE (i3, dest)
850       || (succ && FIND_REG_INC_NOTE (succ, dest))
851       /* Don't combine the end of a libcall into anything.  */
852       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
853       /* Make sure that DEST is not used after SUCC but before I3.  */
854       || (succ && ! all_adjacent
855           && reg_used_between_p (dest, succ, i3))
856       /* Make sure that the value that is to be substituted for the register
857          does not use any registers whose values alter in between.  However,
858          If the insns are adjacent, a use can't cross a set even though we
859          think it might (this can happen for a sequence of insns each setting
860          the same destination; reg_last_set of that register might point to
861          a NOTE).  Also, don't move a volatile asm or UNSPEC_VOLATILE across
862          any other insns.  */
863       || (! all_adjacent
864           && (use_crosses_set_p (src, INSN_CUID (insn))
865               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
866               || GET_CODE (src) == UNSPEC_VOLATILE))
867       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
868          better register allocation by not doing the combine.  */
869       || find_reg_note (i3, REG_NO_CONFLICT, dest)
870       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
871       /* Don't combine across a CALL_INSN, because that would possibly
872          change whether the life span of some REGs crosses calls or not,
873          and it is a pain to update that information.
874          Exception: if source is a constant, moving it later can't hurt.
875          Accept that special case, because it helps -fforce-addr a lot.  */
876       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
877     return 0;
878
879   /* DEST must either be a REG or CC0.  */
880   if (GET_CODE (dest) == REG)
881     {
882       /* If register alignment is being enforced for multi-word items in all
883          cases except for parameters, it is possible to have a register copy
884          insn referencing a hard register that is not allowed to contain the
885          mode being copied and which would not be valid as an operand of most
886          insns.  Eliminate this problem by not combining with such an insn.
887
888          Also, on some machines we don't want to extend the life of a hard
889          register.  */
890
891       if (GET_CODE (src) == REG
892           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
893                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
894 #ifdef SMALL_REGISTER_CLASSES
895               /* Don't extend the life of a hard register.  */
896               || REGNO (src) < FIRST_PSEUDO_REGISTER
897 #else
898               || (REGNO (src) < FIRST_PSEUDO_REGISTER
899                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))
900 #endif
901           ))
902         return 0;
903     }
904   else if (GET_CODE (dest) != CC0)
905     return 0;
906
907   /* Don't substitute for a register intended as a clobberable operand.
908      Similarly, don't substitute an expression containing a register that
909      will be clobbered in I3.  */
910   if (GET_CODE (PATTERN (i3)) == PARALLEL)
911     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
912       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
913           && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
914                                        src)
915               || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
916         return 0;
917
918   /* If INSN contains anything volatile, or is an `asm' (whether volatile
919      or not), reject, unless nothing volatile comes between it and I3,
920      with the exception of SUCC.  */
921
922   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
923     for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
924       if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
925           && p != succ && volatile_refs_p (PATTERN (p)))
926         return 0;
927
928   /* If INSN or I2 contains an autoincrement or autodecrement,
929      make sure that register is not used between there and I3,
930      and not already used in I3 either.
931      Also insist that I3 not be a jump; if it were one
932      and the incremented register were spilled, we would lose.  */
933
934 #ifdef AUTO_INC_DEC
935   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
936     if (REG_NOTE_KIND (link) == REG_INC
937         && (GET_CODE (i3) == JUMP_INSN
938             || reg_used_between_p (XEXP (link, 0), insn, i3)
939             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
940       return 0;
941 #endif
942
943 #ifdef HAVE_cc0
944   /* Don't combine an insn that follows a CC0-setting insn.
945      An insn that uses CC0 must not be separated from the one that sets it.
946      We do, however, allow I2 to follow a CC0-setting insn if that insn
947      is passed as I1; in that case it will be deleted also.
948      We also allow combining in this case if all the insns are adjacent
949      because that would leave the two CC0 insns adjacent as well.
950      It would be more logical to test whether CC0 occurs inside I1 or I2,
951      but that would be much slower, and this ought to be equivalent.  */
952
953   p = prev_nonnote_insn (insn);
954   if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
955       && ! all_adjacent)
956     return 0;
957 #endif
958
959   /* If we get here, we have passed all the tests and the combination is
960      to be allowed.  */
961
962   *pdest = dest;
963   *psrc = src;
964
965   return 1;
966 }
967 \f
968 /* LOC is the location within I3 that contains its pattern or the component
969    of a PARALLEL of the pattern.  We validate that it is valid for combining.
970
971    One problem is if I3 modifies its output, as opposed to replacing it
972    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
973    so would produce an insn that is not equivalent to the original insns.
974
975    Consider:
976
977          (set (reg:DI 101) (reg:DI 100))
978          (set (subreg:SI (reg:DI 101) 0) <foo>)
979
980    This is NOT equivalent to:
981
982          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
983                     (set (reg:DI 101) (reg:DI 100))])
984
985    Not only does this modify 100 (in which case it might still be valid
986    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100. 
987
988    We can also run into a problem if I2 sets a register that I1
989    uses and I1 gets directly substituted into I3 (not via I2).  In that
990    case, we would be getting the wrong value of I2DEST into I3, so we
991    must reject the combination.  This case occurs when I2 and I1 both
992    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
993    If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
994    of a SET must prevent combination from occurring.
995
996    On machines where SMALL_REGISTER_CLASSES is defined, we don't combine
997    if the destination of a SET is a hard register.
998
999    Before doing the above check, we first try to expand a field assignment
1000    into a set of logical operations.
1001
1002    If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1003    we place a register that is both set and used within I3.  If more than one
1004    such register is detected, we fail.
1005
1006    Return 1 if the combination is valid, zero otherwise.  */
1007
1008 static int
1009 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1010      rtx i3;
1011      rtx *loc;
1012      rtx i2dest;
1013      rtx i1dest;
1014      int i1_not_in_src;
1015      rtx *pi3dest_killed;
1016 {
1017   rtx x = *loc;
1018
1019   if (GET_CODE (x) == SET)
1020     {
1021       rtx set = expand_field_assignment (x);
1022       rtx dest = SET_DEST (set);
1023       rtx src = SET_SRC (set);
1024       rtx inner_dest = dest, inner_src = src;
1025
1026       SUBST (*loc, set);
1027
1028       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1029              || GET_CODE (inner_dest) == SUBREG
1030              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1031         inner_dest = XEXP (inner_dest, 0);
1032
1033   /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1034      was added.  */
1035 #if 0
1036       while (GET_CODE (inner_src) == STRICT_LOW_PART
1037              || GET_CODE (inner_src) == SUBREG
1038              || GET_CODE (inner_src) == ZERO_EXTRACT)
1039         inner_src = XEXP (inner_src, 0);
1040
1041       /* If it is better that two different modes keep two different pseudos,
1042          avoid combining them.  This avoids producing the following pattern
1043          on a 386:
1044           (set (subreg:SI (reg/v:QI 21) 0)
1045                (lshiftrt:SI (reg/v:SI 20)
1046                    (const_int 24)))
1047          If that were made, reload could not handle the pair of
1048          reg 20/21, since it would try to get any GENERAL_REGS
1049          but some of them don't handle QImode.  */
1050
1051       if (rtx_equal_p (inner_src, i2dest)
1052           && GET_CODE (inner_dest) == REG
1053           && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1054         return 0;
1055 #endif
1056
1057       /* Check for the case where I3 modifies its output, as
1058          discussed above.  */
1059       if ((inner_dest != dest
1060            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1061                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1062           /* This is the same test done in can_combine_p except that we
1063              allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
1064              CALL operation.  */
1065           || (GET_CODE (inner_dest) == REG
1066               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1067 #ifdef SMALL_REGISTER_CLASSES
1068               && GET_CODE (src) != CALL
1069 #else
1070               && ! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1071                                        GET_MODE (inner_dest))
1072 #endif
1073               )
1074
1075           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1076         return 0;
1077
1078       /* If DEST is used in I3, it is being killed in this insn,
1079          so record that for later. 
1080          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1081          STACK_POINTER_REGNUM, since these are always considered to be
1082          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1083       if (pi3dest_killed && GET_CODE (dest) == REG
1084           && reg_referenced_p (dest, PATTERN (i3))
1085           && REGNO (dest) != FRAME_POINTER_REGNUM
1086 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1087           && (REGNO (dest) != ARG_POINTER_REGNUM
1088               || ! fixed_regs [REGNO (dest)])
1089 #endif
1090           && REGNO (dest) != STACK_POINTER_REGNUM)
1091         {
1092           if (*pi3dest_killed)
1093             return 0;
1094
1095           *pi3dest_killed = dest;
1096         }
1097     }
1098
1099   else if (GET_CODE (x) == PARALLEL)
1100     {
1101       int i;
1102
1103       for (i = 0; i < XVECLEN (x, 0); i++)
1104         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1105                                 i1_not_in_src, pi3dest_killed))
1106           return 0;
1107     }
1108
1109   return 1;
1110 }
1111 \f
1112 /* Try to combine the insns I1 and I2 into I3.
1113    Here I1 and I2 appear earlier than I3.
1114    I1 can be zero; then we combine just I2 into I3.
1115  
1116    It we are combining three insns and the resulting insn is not recognized,
1117    try splitting it into two insns.  If that happens, I2 and I3 are retained
1118    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1119    are pseudo-deleted.
1120
1121    If we created two insns, return I2; otherwise return I3.
1122    Return 0 if the combination does not work.  Then nothing is changed.  */
1123
1124 static rtx
1125 try_combine (i3, i2, i1)
1126      register rtx i3, i2, i1;
1127 {
1128   /* New patterns for I3 and I3, respectively.  */
1129   rtx newpat, newi2pat = 0;
1130   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1131   int added_sets_1, added_sets_2;
1132   /* Total number of SETs to put into I3.  */
1133   int total_sets;
1134   /* Nonzero is I2's body now appears in I3.  */
1135   int i2_is_used;
1136   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1137   int insn_code_number, i2_code_number, other_code_number;
1138   /* Contains I3 if the destination of I3 is used in its source, which means
1139      that the old life of I3 is being killed.  If that usage is placed into
1140      I2 and not in I3, a REG_DEAD note must be made.  */
1141   rtx i3dest_killed = 0;
1142   /* SET_DEST and SET_SRC of I2 and I1.  */
1143   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1144   /* PATTERN (I2), or a copy of it in certain cases.  */
1145   rtx i2pat;
1146   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1147   int i2dest_in_i2src, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1148   int i1_feeds_i3 = 0;
1149   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1150   rtx new_i3_notes, new_i2_notes;
1151
1152   int maxreg;
1153   rtx temp;
1154   register rtx link;
1155   int i;
1156
1157   /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1158      This can occur when flow deletes an insn that it has merged into an
1159      auto-increment address.  We also can't do anything if I3 has a
1160      REG_LIBCALL note since we don't want to disrupt the contiguity of a
1161      libcall.  */
1162
1163   if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1164       || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1165       || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
1166       || find_reg_note (i3, REG_LIBCALL, NULL_RTX))
1167     return 0;
1168
1169   combine_attempts++;
1170
1171   undobuf.num_undo = previous_num_undos = 0;
1172   undobuf.other_insn = 0;
1173
1174   /* Save the current high-water-mark so we can free storage if we didn't
1175      accept this combination.  */
1176   undobuf.storage = (char *) oballoc (0);
1177
1178   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1179      code below, set I1 to be the earlier of the two insns.  */
1180   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1181     temp = i1, i1 = i2, i2 = temp;
1182
1183   /* First check for one important special-case that the code below will
1184      not handle.  Namely, the case where I1 is zero, I2 has multiple sets,
1185      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1186      we may be able to replace that destination with the destination of I3.
1187      This occurs in the common code where we compute both a quotient and
1188      remainder into a structure, in which case we want to do the computation
1189      directly into the structure to avoid register-register copies.
1190
1191      We make very conservative checks below and only try to handle the
1192      most common cases of this.  For example, we only handle the case
1193      where I2 and I3 are adjacent to avoid making difficult register
1194      usage tests.  */
1195
1196   if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1197       && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1198       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1199 #ifdef SMALL_REGISTER_CLASSES
1200       && (GET_CODE (SET_DEST (PATTERN (i3))) != REG
1201           || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER)
1202 #endif
1203       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1204       && GET_CODE (PATTERN (i2)) == PARALLEL
1205       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1206       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1207          below would need to check what is inside (and reg_overlap_mentioned_p
1208          doesn't support those codes anyway).  Don't allow those destinations;
1209          the resulting insn isn't likely to be recognized anyway.  */
1210       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1211       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1212       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1213                                     SET_DEST (PATTERN (i3)))
1214       && next_real_insn (i2) == i3)
1215     {
1216       rtx p2 = PATTERN (i2);
1217
1218       /* Make sure that the destination of I3,
1219          which we are going to substitute into one output of I2,
1220          is not used within another output of I2.  We must avoid making this:
1221          (parallel [(set (mem (reg 69)) ...)
1222                     (set (reg 69) ...)])
1223          which is not well-defined as to order of actions.
1224          (Besides, reload can't handle output reloads for this.)
1225
1226          The problem can also happen if the dest of I3 is a memory ref,
1227          if another dest in I2 is an indirect memory ref.  */
1228       for (i = 0; i < XVECLEN (p2, 0); i++)
1229         if (GET_CODE (XVECEXP (p2, 0, i)) == SET
1230             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1231                                         SET_DEST (XVECEXP (p2, 0, i))))
1232           break;
1233
1234       if (i == XVECLEN (p2, 0))
1235         for (i = 0; i < XVECLEN (p2, 0); i++)
1236           if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1237             {
1238               combine_merges++;
1239
1240               subst_insn = i3;
1241               subst_low_cuid = INSN_CUID (i2);
1242
1243               added_sets_2 = 0;
1244               i2dest = SET_SRC (PATTERN (i3));
1245
1246               /* Replace the dest in I2 with our dest and make the resulting
1247                  insn the new pattern for I3.  Then skip to where we
1248                  validate the pattern.  Everything was set up above.  */
1249               SUBST (SET_DEST (XVECEXP (p2, 0, i)), 
1250                      SET_DEST (PATTERN (i3)));
1251
1252               newpat = p2;
1253               goto validate_replacement;
1254             }
1255     }
1256
1257 #ifndef HAVE_cc0
1258   /* If we have no I1 and I2 looks like:
1259         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1260                    (set Y OP)])
1261      make up a dummy I1 that is
1262         (set Y OP)
1263      and change I2 to be
1264         (set (reg:CC X) (compare:CC Y (const_int 0)))
1265
1266      (We can ignore any trailing CLOBBERs.)
1267
1268      This undoes a previous combination and allows us to match a branch-and-
1269      decrement insn.  */
1270
1271   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1272       && XVECLEN (PATTERN (i2), 0) >= 2
1273       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1274       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1275           == MODE_CC)
1276       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1277       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1278       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1279       && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1280       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1281                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1282     {
1283       for (i =  XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1284         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1285           break;
1286
1287       if (i == 1)
1288         {
1289           /* We make I1 with the same INSN_UID as I2.  This gives it
1290              the same INSN_CUID for value tracking.  Our fake I1 will
1291              never appear in the insn stream so giving it the same INSN_UID
1292              as I2 will not cause a problem.  */
1293
1294           i1 = gen_rtx (INSN, VOIDmode, INSN_UID (i2), 0, i2,
1295                         XVECEXP (PATTERN (i2), 0, 1), -1, 0, 0);
1296
1297           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1298           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1299                  SET_DEST (PATTERN (i1)));
1300         }
1301     }
1302 #endif
1303
1304   /* Verify that I2 and I1 are valid for combining.  */
1305   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1306       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1307     {
1308       undo_all ();
1309       return 0;
1310     }
1311
1312   /* Record whether I2DEST is used in I2SRC and similarly for the other
1313      cases.  Knowing this will help in register status updating below.  */
1314   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1315   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1316   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1317
1318   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
1319      in I2SRC.  */
1320   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1321
1322   /* Ensure that I3's pattern can be the destination of combines.  */
1323   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1324                           i1 && i2dest_in_i1src && i1_feeds_i3,
1325                           &i3dest_killed))
1326     {
1327       undo_all ();
1328       return 0;
1329     }
1330
1331   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1332      We used to do this EXCEPT in one case: I3 has a post-inc in an
1333      output operand.  However, that exception can give rise to insns like
1334         mov r3,(r3)+
1335      which is a famous insn on the PDP-11 where the value of r3 used as the
1336      source was model-dependent.  Avoid this sort of thing.  */
1337
1338 #if 0
1339   if (!(GET_CODE (PATTERN (i3)) == SET
1340         && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1341         && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1342         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1343             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1344     /* It's not the exception.  */
1345 #endif
1346 #ifdef AUTO_INC_DEC
1347     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1348       if (REG_NOTE_KIND (link) == REG_INC
1349           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1350               || (i1 != 0
1351                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1352         {
1353           undo_all ();
1354           return 0;
1355         }
1356 #endif
1357
1358   /* See if the SETs in I1 or I2 need to be kept around in the merged
1359      instruction: whenever the value set there is still needed past I3.
1360      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1361
1362      For the SET in I1, we have two cases:  If I1 and I2 independently
1363      feed into I3, the set in I1 needs to be kept around if I1DEST dies
1364      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
1365      in I1 needs to be kept around unless I1DEST dies or is set in either
1366      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
1367      I1DEST.  If so, we know I1 feeds into I2.  */
1368
1369   added_sets_2 = ! dead_or_set_p (i3, i2dest);
1370
1371   added_sets_1
1372     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1373                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1374
1375   /* If the set in I2 needs to be kept around, we must make a copy of
1376      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1377      PATTERN (I2), we are only substituting for the original I1DEST, not into
1378      an already-substituted copy.  This also prevents making self-referential
1379      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1380      I2DEST.  */
1381
1382   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1383            ? gen_rtx (SET, VOIDmode, i2dest, i2src)
1384            : PATTERN (i2));
1385
1386   if (added_sets_2)
1387     i2pat = copy_rtx (i2pat);
1388
1389   combine_merges++;
1390
1391   /* Substitute in the latest insn for the regs set by the earlier ones.  */
1392
1393   maxreg = max_reg_num ();
1394
1395   subst_insn = i3;
1396
1397   /* It is possible that the source of I2 or I1 may be performing an
1398      unneeded operation, such as a ZERO_EXTEND of something that is known
1399      to have the high part zero.  Handle that case by letting subst look at
1400      the innermost one of them.
1401
1402      Another way to do this would be to have a function that tries to
1403      simplify a single insn instead of merging two or more insns.  We don't
1404      do this because of the potential of infinite loops and because
1405      of the potential extra memory required.  However, doing it the way
1406      we are is a bit of a kludge and doesn't catch all cases.
1407
1408      But only do this if -fexpensive-optimizations since it slows things down
1409      and doesn't usually win.  */
1410
1411   if (flag_expensive_optimizations)
1412     {
1413       /* Pass pc_rtx so no substitutions are done, just simplifications.
1414          The cases that we are interested in here do not involve the few
1415          cases were is_replaced is checked.  */
1416       if (i1)
1417         {
1418           subst_low_cuid = INSN_CUID (i1);
1419           i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1420         }
1421       else
1422         {
1423           subst_low_cuid = INSN_CUID (i2);
1424           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1425         }
1426
1427       previous_num_undos = undobuf.num_undo;
1428     }
1429
1430 #ifndef HAVE_cc0
1431   /* Many machines that don't use CC0 have insns that can both perform an
1432      arithmetic operation and set the condition code.  These operations will
1433      be represented as a PARALLEL with the first element of the vector
1434      being a COMPARE of an arithmetic operation with the constant zero.
1435      The second element of the vector will set some pseudo to the result
1436      of the same arithmetic operation.  If we simplify the COMPARE, we won't
1437      match such a pattern and so will generate an extra insn.   Here we test
1438      for this case, where both the comparison and the operation result are
1439      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1440      I2SRC.  Later we will make the PARALLEL that contains I2.  */
1441
1442   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1443       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1444       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1445       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1446     {
1447       rtx *cc_use;
1448       enum machine_mode compare_mode;
1449
1450       newpat = PATTERN (i3);
1451       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1452
1453       i2_is_used = 1;
1454
1455 #ifdef EXTRA_CC_MODES
1456       /* See if a COMPARE with the operand we substituted in should be done
1457          with the mode that is currently being used.  If not, do the same
1458          processing we do in `subst' for a SET; namely, if the destination
1459          is used only once, try to replace it with a register of the proper
1460          mode and also replace the COMPARE.  */
1461       if (undobuf.other_insn == 0
1462           && (cc_use = find_single_use (SET_DEST (newpat), i3,
1463                                         &undobuf.other_insn))
1464           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1465                                               i2src, const0_rtx))
1466               != GET_MODE (SET_DEST (newpat))))
1467         {
1468           int regno = REGNO (SET_DEST (newpat));
1469           rtx new_dest = gen_rtx (REG, compare_mode, regno);
1470
1471           if (regno < FIRST_PSEUDO_REGISTER
1472               || (reg_n_sets[regno] == 1 && ! added_sets_2
1473                   && ! REG_USERVAR_P (SET_DEST (newpat))))
1474             {
1475               if (regno >= FIRST_PSEUDO_REGISTER)
1476                 SUBST (regno_reg_rtx[regno], new_dest);
1477
1478               SUBST (SET_DEST (newpat), new_dest);
1479               SUBST (XEXP (*cc_use, 0), new_dest);
1480               SUBST (SET_SRC (newpat),
1481                      gen_rtx_combine (COMPARE, compare_mode,
1482                                       i2src, const0_rtx));
1483             }
1484           else
1485             undobuf.other_insn = 0;
1486         }
1487 #endif    
1488     }
1489   else
1490 #endif
1491     {
1492       n_occurrences = 0;                /* `subst' counts here */
1493
1494       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1495          need to make a unique copy of I2SRC each time we substitute it
1496          to avoid self-referential rtl.  */
1497
1498       subst_low_cuid = INSN_CUID (i2);
1499       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1500                       ! i1_feeds_i3 && i1dest_in_i1src);
1501       previous_num_undos = undobuf.num_undo;
1502
1503       /* Record whether i2's body now appears within i3's body.  */
1504       i2_is_used = n_occurrences;
1505     }
1506
1507   /* If we already got a failure, don't try to do more.  Otherwise,
1508      try to substitute in I1 if we have it.  */
1509
1510   if (i1 && GET_CODE (newpat) != CLOBBER)
1511     {
1512       /* Before we can do this substitution, we must redo the test done
1513          above (see detailed comments there) that ensures  that I1DEST
1514          isn't mentioned in any SETs in NEWPAT that are field assignments. */
1515
1516       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1517                               0, NULL_PTR))
1518         {
1519           undo_all ();
1520           return 0;
1521         }
1522
1523       n_occurrences = 0;
1524       subst_low_cuid = INSN_CUID (i1);
1525       newpat = subst (newpat, i1dest, i1src, 0, 0);
1526       previous_num_undos = undobuf.num_undo;
1527     }
1528
1529   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
1530      to count all the ways that I2SRC and I1SRC can be used.  */
1531   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1532        && i2_is_used + added_sets_2 > 1)
1533       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1534           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1535               > 1))
1536       /* Fail if we tried to make a new register (we used to abort, but there's
1537          really no reason to).  */
1538       || max_reg_num () != maxreg
1539       /* Fail if we couldn't do something and have a CLOBBER.  */
1540       || GET_CODE (newpat) == CLOBBER)
1541     {
1542       undo_all ();
1543       return 0;
1544     }
1545
1546   /* If the actions of the earlier insns must be kept
1547      in addition to substituting them into the latest one,
1548      we must make a new PARALLEL for the latest insn
1549      to hold additional the SETs.  */
1550
1551   if (added_sets_1 || added_sets_2)
1552     {
1553       combine_extras++;
1554
1555       if (GET_CODE (newpat) == PARALLEL)
1556         {
1557           rtvec old = XVEC (newpat, 0);
1558           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1559           newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
1560           bcopy (&old->elem[0], &XVECEXP (newpat, 0, 0),
1561                  sizeof (old->elem[0]) * old->num_elem);
1562         }
1563       else
1564         {
1565           rtx old = newpat;
1566           total_sets = 1 + added_sets_1 + added_sets_2;
1567           newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
1568           XVECEXP (newpat, 0, 0) = old;
1569         }
1570
1571      if (added_sets_1)
1572        XVECEXP (newpat, 0, --total_sets)
1573          = (GET_CODE (PATTERN (i1)) == PARALLEL
1574             ? gen_rtx (SET, VOIDmode, i1dest, i1src) : PATTERN (i1));
1575
1576      if (added_sets_2)
1577         {
1578           /* If there is no I1, use I2's body as is.  We used to also not do
1579              the subst call below if I2 was substituted into I3,
1580              but that could lose a simplification.  */
1581           if (i1 == 0)
1582             XVECEXP (newpat, 0, --total_sets) = i2pat;
1583           else
1584             /* See comment where i2pat is assigned.  */
1585             XVECEXP (newpat, 0, --total_sets)
1586               = subst (i2pat, i1dest, i1src, 0, 0);
1587         }
1588     }
1589
1590   /* We come here when we are replacing a destination in I2 with the
1591      destination of I3.  */
1592  validate_replacement:
1593
1594   /* Is the result of combination a valid instruction?  */
1595   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1596
1597   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1598      the second SET's destination is a register that is unused.  In that case,
1599      we just need the first SET.   This can occur when simplifying a divmod
1600      insn.  We *must* test for this case here because the code below that
1601      splits two independent SETs doesn't handle this case correctly when it
1602      updates the register status.  Also check the case where the first
1603      SET's destination is unused.  That would not cause incorrect code, but
1604      does cause an unneeded insn to remain.  */
1605
1606   if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1607       && XVECLEN (newpat, 0) == 2
1608       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1609       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1610       && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1611       && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1612       && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1613       && asm_noperands (newpat) < 0)
1614     {
1615       newpat = XVECEXP (newpat, 0, 0);
1616       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1617     }
1618
1619   else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1620            && XVECLEN (newpat, 0) == 2
1621            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1622            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1623            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1624            && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1625            && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1626            && asm_noperands (newpat) < 0)
1627     {
1628       newpat = XVECEXP (newpat, 0, 1);
1629       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1630     }
1631
1632   /* See if this is an XOR.  If so, perhaps the problem is that the
1633      constant is out of range.  Replace it with a complemented XOR with
1634      a complemented constant; it might be in range.  */
1635
1636   else if (insn_code_number < 0 && GET_CODE (newpat) == SET
1637            && GET_CODE (SET_SRC (newpat)) == XOR
1638            && GET_CODE (XEXP (SET_SRC (newpat), 1)) == CONST_INT
1639            && ((temp = simplify_unary_operation (NOT,
1640                                                  GET_MODE (SET_SRC (newpat)),
1641                                                  XEXP (SET_SRC (newpat), 1),
1642                                                  GET_MODE (SET_SRC (newpat))))
1643                != 0))
1644     {
1645       enum machine_mode i_mode = GET_MODE (SET_SRC (newpat));
1646       rtx pat
1647         = gen_rtx_combine (SET, VOIDmode, SET_DEST (newpat),
1648                            gen_unary (NOT, i_mode,
1649                                       gen_binary (XOR, i_mode,
1650                                                   XEXP (SET_SRC (newpat), 0),
1651                                                   temp)));
1652
1653       insn_code_number = recog_for_combine (&pat, i3, &new_i3_notes);
1654       if (insn_code_number >= 0)
1655         newpat = pat;
1656     }
1657                                                         
1658   /* If we were combining three insns and the result is a simple SET
1659      with no ASM_OPERANDS that wasn't recognized, try to split it into two
1660      insns.  There are two ways to do this.  It can be split using a 
1661      machine-specific method (like when you have an addition of a large
1662      constant) or by combine in the function find_split_point.  */
1663
1664   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1665       && asm_noperands (newpat) < 0)
1666     {
1667       rtx m_split, *split;
1668       rtx ni2dest = i2dest;
1669
1670       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
1671          use I2DEST as a scratch register will help.  In the latter case,
1672          convert I2DEST to the mode of the source of NEWPAT if we can.  */
1673
1674       m_split = split_insns (newpat, i3);
1675
1676       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1677          inputs of NEWPAT.  */
1678
1679       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1680          possible to try that as a scratch reg.  This would require adding
1681          more code to make it work though.  */
1682
1683       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
1684         {
1685           /* If I2DEST is a hard register or the only use of a pseudo,
1686              we can change its mode.  */
1687           if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
1688               && GET_MODE (SET_DEST (newpat)) != VOIDmode
1689               && GET_CODE (i2dest) == REG
1690               && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1691                   || (reg_n_sets[REGNO (i2dest)] == 1 && ! added_sets_2
1692                       && ! REG_USERVAR_P (i2dest))))
1693             ni2dest = gen_rtx (REG, GET_MODE (SET_DEST (newpat)),
1694                                REGNO (i2dest));
1695
1696           m_split = split_insns (gen_rtx (PARALLEL, VOIDmode,
1697                                           gen_rtvec (2, newpat,
1698                                                      gen_rtx (CLOBBER,
1699                                                               VOIDmode,
1700                                                               ni2dest))),
1701                                  i3);
1702         }
1703
1704       if (m_split && GET_CODE (m_split) == SEQUENCE
1705           && XVECLEN (m_split, 0) == 2
1706           && (next_real_insn (i2) == i3
1707               || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1708                                       INSN_CUID (i2))))
1709         {
1710           rtx i2set, i3set;
1711           rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
1712           newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
1713
1714           i3set = single_set (XVECEXP (m_split, 0, 1));
1715           i2set = single_set (XVECEXP (m_split, 0, 0));
1716
1717           /* In case we changed the mode of I2DEST, replace it in the
1718              pseudo-register table here.  We can't do it above in case this
1719              code doesn't get executed and we do a split the other way.  */
1720
1721           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1722             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1723
1724           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
1725
1726           /* If I2 or I3 has multiple SETs, we won't know how to track
1727              register status, so don't use these insns.  */
1728
1729           if (i2_code_number >= 0 && i2set && i3set)
1730             insn_code_number = recog_for_combine (&newi3pat, i3,
1731                                                   &new_i3_notes);
1732
1733           if (insn_code_number >= 0)
1734             newpat = newi3pat;
1735
1736           /* It is possible that both insns now set the destination of I3.
1737              If so, we must show an extra use of it.  */
1738
1739           if (insn_code_number >= 0 && GET_CODE (SET_DEST (i3set)) == REG
1740               && GET_CODE (SET_DEST (i2set)) == REG
1741               && REGNO (SET_DEST (i3set)) == REGNO (SET_DEST (i2set)))
1742             reg_n_sets[REGNO (SET_DEST (i2set))]++;
1743         }
1744
1745       /* If we can split it and use I2DEST, go ahead and see if that
1746          helps things be recognized.  Verify that none of the registers
1747          are set between I2 and I3.  */
1748       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
1749 #ifdef HAVE_cc0
1750           && GET_CODE (i2dest) == REG
1751 #endif
1752           /* We need I2DEST in the proper mode.  If it is a hard register
1753              or the only use of a pseudo, we can change its mode.  */
1754           && (GET_MODE (*split) == GET_MODE (i2dest)
1755               || GET_MODE (*split) == VOIDmode
1756               || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1757               || (reg_n_sets[REGNO (i2dest)] == 1 && ! added_sets_2
1758                   && ! REG_USERVAR_P (i2dest)))
1759           && (next_real_insn (i2) == i3
1760               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
1761           /* We can't overwrite I2DEST if its value is still used by
1762              NEWPAT.  */
1763           && ! reg_referenced_p (i2dest, newpat))
1764         {
1765           rtx newdest = i2dest;
1766
1767           /* Get NEWDEST as a register in the proper mode.  We have already
1768              validated that we can do this.  */
1769           if (GET_MODE (i2dest) != GET_MODE (*split)
1770               && GET_MODE (*split) != VOIDmode)
1771             {
1772               newdest = gen_rtx (REG, GET_MODE (*split), REGNO (i2dest));
1773
1774               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1775                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
1776             }
1777
1778           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
1779              an ASHIFT.  This can occur if it was inside a PLUS and hence
1780              appeared to be a memory address.  This is a kludge.  */
1781           if (GET_CODE (*split) == MULT
1782               && GET_CODE (XEXP (*split, 1)) == CONST_INT
1783               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
1784             SUBST (*split, gen_rtx_combine (ASHIFT, GET_MODE (*split),
1785                                             XEXP (*split, 0), GEN_INT (i)));
1786
1787 #ifdef INSN_SCHEDULING
1788           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
1789              be written as a ZERO_EXTEND.  */
1790           if (GET_CODE (*split) == SUBREG
1791               && GET_CODE (SUBREG_REG (*split)) == MEM)
1792             SUBST (*split, gen_rtx_combine (ZERO_EXTEND, GET_MODE (*split),
1793                                             XEXP (*split, 0)));
1794 #endif
1795
1796           newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
1797           SUBST (*split, newdest);
1798           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
1799           if (i2_code_number >= 0)
1800             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1801         }
1802     }
1803
1804   /* Check for a case where we loaded from memory in a narrow mode and
1805      then sign extended it, but we need both registers.  In that case,
1806      we have a PARALLEL with both loads from the same memory location.
1807      We can split this into a load from memory followed by a register-register
1808      copy.  This saves at least one insn, more if register allocation can
1809      eliminate the copy.  */
1810
1811   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
1812            && GET_CODE (newpat) == PARALLEL
1813            && XVECLEN (newpat, 0) == 2
1814            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1815            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
1816            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1817            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
1818                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
1819            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
1820                                    INSN_CUID (i2))
1821            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
1822            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
1823            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
1824                                          SET_SRC (XVECEXP (newpat, 0, 1)))
1825            && ! find_reg_note (i3, REG_UNUSED,
1826                                SET_DEST (XVECEXP (newpat, 0, 0))))
1827     {
1828       rtx ni2dest;
1829
1830       newi2pat = XVECEXP (newpat, 0, 0);
1831       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
1832       newpat = XVECEXP (newpat, 0, 1);
1833       SUBST (SET_SRC (newpat),
1834              gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
1835       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
1836       if (i2_code_number >= 0)
1837         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1838
1839       if (insn_code_number >= 0)
1840         {
1841           rtx insn;
1842           rtx link;
1843
1844           /* If we will be able to accept this, we have made a change to the
1845              destination of I3.  This can invalidate a LOG_LINKS pointing
1846              to I3.  No other part of combine.c makes such a transformation.
1847
1848              The new I3 will have a destination that was previously the
1849              destination of I1 or I2 and which was used in i2 or I3.  Call
1850              distribute_links to make a LOG_LINK from the next use of
1851              that destination.  */
1852
1853           PATTERN (i3) = newpat;
1854           distribute_links (gen_rtx (INSN_LIST, VOIDmode, i3, NULL_RTX));
1855
1856           /* I3 now uses what used to be its destination and which is
1857              now I2's destination.  That means we need a LOG_LINK from
1858              I3 to I2.  But we used to have one, so we still will.
1859
1860              However, some later insn might be using I2's dest and have
1861              a LOG_LINK pointing at I3.  We must remove this link.
1862              The simplest way to remove the link is to point it at I1,
1863              which we know will be a NOTE.  */
1864
1865           for (insn = NEXT_INSN (i3);
1866                insn && GET_CODE (insn) != CODE_LABEL
1867                && GET_CODE (PREV_INSN (insn)) != JUMP_INSN;
1868                insn = NEXT_INSN (insn))
1869             {
1870               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1871                   && reg_referenced_p (ni2dest, PATTERN (insn)))
1872                 {
1873                   for (link = LOG_LINKS (insn); link;
1874                        link = XEXP (link, 1))
1875                     if (XEXP (link, 0) == i3)
1876                       XEXP (link, 0) = i1;
1877
1878                   break;
1879                 }
1880             }
1881         }
1882     }
1883             
1884   /* Similarly, check for a case where we have a PARALLEL of two independent
1885      SETs but we started with three insns.  In this case, we can do the sets
1886      as two separate insns.  This case occurs when some SET allows two
1887      other insns to combine, but the destination of that SET is still live.  */
1888
1889   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
1890            && GET_CODE (newpat) == PARALLEL
1891            && XVECLEN (newpat, 0) == 2
1892            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1893            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
1894            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
1895            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1896            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
1897            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
1898            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
1899                                    INSN_CUID (i2))
1900            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
1901            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
1902            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
1903            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
1904                                   XVECEXP (newpat, 0, 0))
1905            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
1906                                   XVECEXP (newpat, 0, 1)))
1907     {
1908       newi2pat = XVECEXP (newpat, 0, 1);
1909       newpat = XVECEXP (newpat, 0, 0);
1910
1911       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
1912       if (i2_code_number >= 0)
1913         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1914     }
1915
1916   /* If it still isn't recognized, fail and change things back the way they
1917      were.  */
1918   if ((insn_code_number < 0
1919        /* Is the result a reasonable ASM_OPERANDS?  */
1920        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
1921     {
1922       undo_all ();
1923       return 0;
1924     }
1925
1926   /* If we had to change another insn, make sure it is valid also.  */
1927   if (undobuf.other_insn)
1928     {
1929       rtx other_notes = REG_NOTES (undobuf.other_insn);
1930       rtx other_pat = PATTERN (undobuf.other_insn);
1931       rtx new_other_notes;
1932       rtx note, next;
1933
1934       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
1935                                              &new_other_notes);
1936
1937       if (other_code_number < 0 && ! check_asm_operands (other_pat))
1938         {
1939           undo_all ();
1940           return 0;
1941         }
1942
1943       PATTERN (undobuf.other_insn) = other_pat;
1944
1945       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
1946          are still valid.  Then add any non-duplicate notes added by
1947          recog_for_combine.  */
1948       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
1949         {
1950           next = XEXP (note, 1);
1951
1952           if (REG_NOTE_KIND (note) == REG_UNUSED
1953               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
1954             {
1955               if (GET_CODE (XEXP (note, 0)) == REG)
1956                 reg_n_deaths[REGNO (XEXP (note, 0))]--;
1957
1958               remove_note (undobuf.other_insn, note);
1959             }
1960         }
1961
1962       for (note = new_other_notes; note; note = XEXP (note, 1))
1963         if (GET_CODE (XEXP (note, 0)) == REG)
1964           reg_n_deaths[REGNO (XEXP (note, 0))]++;
1965
1966       distribute_notes (new_other_notes, undobuf.other_insn,
1967                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
1968     }
1969
1970   /* We now know that we can do this combination.  Merge the insns and 
1971      update the status of registers and LOG_LINKS.  */
1972
1973   {
1974     rtx i3notes, i2notes, i1notes = 0;
1975     rtx i3links, i2links, i1links = 0;
1976     rtx midnotes = 0;
1977     int all_adjacent = (next_real_insn (i2) == i3
1978                         && (i1 == 0 || next_real_insn (i1) == i2));
1979     register int regno;
1980     /* Compute which registers we expect to eliminate.  */
1981     rtx elim_i2 = (newi2pat || i2dest_in_i2src || i2dest_in_i1src
1982                    ? 0 : i2dest);
1983     rtx elim_i1 = i1 == 0 || i1dest_in_i1src ? 0 : i1dest;
1984
1985     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
1986        clear them.  */
1987     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
1988     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
1989     if (i1)
1990       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
1991
1992     /* Ensure that we do not have something that should not be shared but
1993        occurs multiple times in the new insns.  Check this by first
1994        resetting all the `used' flags and then copying anything is shared.  */
1995
1996     reset_used_flags (i3notes);
1997     reset_used_flags (i2notes);
1998     reset_used_flags (i1notes);
1999     reset_used_flags (newpat);
2000     reset_used_flags (newi2pat);
2001     if (undobuf.other_insn)
2002       reset_used_flags (PATTERN (undobuf.other_insn));
2003
2004     i3notes = copy_rtx_if_shared (i3notes);
2005     i2notes = copy_rtx_if_shared (i2notes);
2006     i1notes = copy_rtx_if_shared (i1notes);
2007     newpat = copy_rtx_if_shared (newpat);
2008     newi2pat = copy_rtx_if_shared (newi2pat);
2009     if (undobuf.other_insn)
2010       reset_used_flags (PATTERN (undobuf.other_insn));
2011
2012     INSN_CODE (i3) = insn_code_number;
2013     PATTERN (i3) = newpat;
2014     if (undobuf.other_insn)
2015       INSN_CODE (undobuf.other_insn) = other_code_number;
2016
2017     /* We had one special case above where I2 had more than one set and
2018        we replaced a destination of one of those sets with the destination
2019        of I3.  In that case, we have to update LOG_LINKS of insns later
2020        in this basic block.  Note that this (expensive) case is rare.  */
2021
2022     if (GET_CODE (PATTERN (i2)) == PARALLEL)
2023       for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2024         if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2025             && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2026             && ! find_reg_note (i2, REG_UNUSED,
2027                                 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2028           {
2029             register rtx insn;
2030
2031             for (insn = NEXT_INSN (i2); insn; insn = NEXT_INSN (insn))
2032               {
2033                 if (insn != i3 && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2034                   for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
2035                     if (XEXP (link, 0) == i2)
2036                       XEXP (link, 0) = i3;
2037
2038                 if (GET_CODE (insn) == CODE_LABEL
2039                     || GET_CODE (insn) == JUMP_INSN)
2040                   break;
2041               }
2042           }
2043
2044     LOG_LINKS (i3) = 0;
2045     REG_NOTES (i3) = 0;
2046     LOG_LINKS (i2) = 0;
2047     REG_NOTES (i2) = 0;
2048
2049     if (newi2pat)
2050       {
2051         INSN_CODE (i2) = i2_code_number;
2052         PATTERN (i2) = newi2pat;
2053       }
2054     else
2055       {
2056         PUT_CODE (i2, NOTE);
2057         NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2058         NOTE_SOURCE_FILE (i2) = 0;
2059       }
2060
2061     if (i1)
2062       {
2063         LOG_LINKS (i1) = 0;
2064         REG_NOTES (i1) = 0;
2065         PUT_CODE (i1, NOTE);
2066         NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2067         NOTE_SOURCE_FILE (i1) = 0;
2068       }
2069
2070     /* Get death notes for everything that is now used in either I3 or
2071        I2 and used to die in a previous insn.  */
2072
2073     move_deaths (newpat, i1 ? INSN_CUID (i1) : INSN_CUID (i2), i3, &midnotes);
2074     if (newi2pat)
2075       move_deaths (newi2pat, INSN_CUID (i1), i2, &midnotes);
2076
2077     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
2078     if (i3notes)
2079       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2080                         elim_i2, elim_i1);
2081     if (i2notes)
2082       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2083                         elim_i2, elim_i1);
2084     if (i1notes)
2085       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2086                         elim_i2, elim_i1);
2087     if (midnotes)
2088       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2089                         elim_i2, elim_i1);
2090
2091     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
2092        know these are REG_UNUSED and want them to go to the desired insn,
2093        so we always pass it as i3.  We have not counted the notes in 
2094        reg_n_deaths yet, so we need to do so now.  */
2095
2096     if (newi2pat && new_i2_notes)
2097       {
2098         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2099           if (GET_CODE (XEXP (temp, 0)) == REG)
2100             reg_n_deaths[REGNO (XEXP (temp, 0))]++;
2101         
2102         distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2103       }
2104
2105     if (new_i3_notes)
2106       {
2107         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2108           if (GET_CODE (XEXP (temp, 0)) == REG)
2109             reg_n_deaths[REGNO (XEXP (temp, 0))]++;
2110         
2111         distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2112       }
2113
2114     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
2115        put a REG_DEAD note for it somewhere.  Similarly for I2 and I1.
2116        Show an additional death due to the REG_DEAD note we make here.  If
2117        we discard it in distribute_notes, we will decrement it again.  */
2118
2119     if (i3dest_killed)
2120       {
2121         if (GET_CODE (i3dest_killed) == REG)
2122           reg_n_deaths[REGNO (i3dest_killed)]++;
2123
2124         distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i3dest_killed,
2125                                    NULL_RTX),
2126                           NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2127                           NULL_RTX, NULL_RTX);
2128       }
2129
2130     /* For I2 and I1, we have to be careful.  If NEWI2PAT exists and sets
2131        I2DEST or I1DEST, the death must be somewhere before I2, not I3.  If
2132        we passed I3 in that case, it might delete I2.  */
2133
2134     if (i2dest_in_i2src)
2135       {
2136         if (GET_CODE (i2dest) == REG)
2137           reg_n_deaths[REGNO (i2dest)]++;
2138
2139         if (newi2pat && reg_set_p (i2dest, newi2pat))
2140           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
2141                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2142         else
2143           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
2144                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2145                             NULL_RTX, NULL_RTX);
2146       }
2147
2148     if (i1dest_in_i1src)
2149       {
2150         if (GET_CODE (i1dest) == REG)
2151           reg_n_deaths[REGNO (i1dest)]++;
2152
2153         if (newi2pat && reg_set_p (i1dest, newi2pat))
2154           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
2155                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2156         else
2157           distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
2158                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2159                             NULL_RTX, NULL_RTX);
2160       }
2161
2162     distribute_links (i3links);
2163     distribute_links (i2links);
2164     distribute_links (i1links);
2165
2166     if (GET_CODE (i2dest) == REG)
2167       {
2168         rtx link;
2169         rtx i2_insn = 0, i2_val = 0, set;
2170
2171         /* The insn that used to set this register doesn't exist, and
2172            this life of the register may not exist either.  See if one of
2173            I3's links points to an insn that sets I2DEST.  If it does, 
2174            that is now the last known value for I2DEST. If we don't update
2175            this and I2 set the register to a value that depended on its old
2176            contents, we will get confused.  If this insn is used, thing
2177            will be set correctly in combine_instructions.  */
2178
2179         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2180           if ((set = single_set (XEXP (link, 0))) != 0
2181               && rtx_equal_p (i2dest, SET_DEST (set)))
2182             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2183
2184         record_value_for_reg (i2dest, i2_insn, i2_val);
2185
2186         /* If the reg formerly set in I2 died only once and that was in I3,
2187            zero its use count so it won't make `reload' do any work.  */
2188         if (! added_sets_2 && newi2pat == 0)
2189           {
2190             regno = REGNO (i2dest);
2191             reg_n_sets[regno]--;
2192             if (reg_n_sets[regno] == 0
2193                 && ! (basic_block_live_at_start[0][regno / REGSET_ELT_BITS]
2194                       & ((REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS))))
2195               reg_n_refs[regno] = 0;
2196           }
2197       }
2198
2199     if (i1 && GET_CODE (i1dest) == REG)
2200       {
2201         rtx link;
2202         rtx i1_insn = 0, i1_val = 0, set;
2203
2204         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2205           if ((set = single_set (XEXP (link, 0))) != 0
2206               && rtx_equal_p (i1dest, SET_DEST (set)))
2207             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2208
2209         record_value_for_reg (i1dest, i1_insn, i1_val);
2210
2211         regno = REGNO (i1dest);
2212         if (! added_sets_1)
2213           {
2214             reg_n_sets[regno]--;
2215             if (reg_n_sets[regno] == 0
2216                 && ! (basic_block_live_at_start[0][regno / REGSET_ELT_BITS]
2217                       & ((REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS))))
2218               reg_n_refs[regno] = 0;
2219           }
2220       }
2221
2222     /* Update reg_nonzero_bits et al for any changes that may have been made
2223        to this insn.  */
2224
2225     note_stores (newpat, set_nonzero_bits_and_sign_copies);
2226     if (newi2pat)
2227       note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
2228
2229     /* If I3 is now an unconditional jump, ensure that it has a 
2230        BARRIER following it since it may have initially been a
2231        conditional jump.  It may also be the last nonnote insn.  */
2232
2233     if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
2234         && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2235             || GET_CODE (temp) != BARRIER))
2236       emit_barrier_after (i3);
2237   }
2238
2239   combine_successes++;
2240
2241   return newi2pat ? i2 : i3;
2242 }
2243 \f
2244 /* Undo all the modifications recorded in undobuf.  */
2245
2246 static void
2247 undo_all ()
2248 {
2249   register int i;
2250   if (undobuf.num_undo > MAX_UNDO)
2251     undobuf.num_undo = MAX_UNDO;
2252   for (i = undobuf.num_undo - 1; i >= 0; i--)
2253     {
2254       if (undobuf.undo[i].is_int)
2255         *undobuf.undo[i].where.i = undobuf.undo[i].old_contents.i;
2256       else
2257         *undobuf.undo[i].where.rtx = undobuf.undo[i].old_contents.rtx;
2258       
2259     }
2260
2261   obfree (undobuf.storage);
2262   undobuf.num_undo = 0;
2263 }
2264 \f
2265 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2266    where we have an arithmetic expression and return that point.  LOC will
2267    be inside INSN.
2268
2269    try_combine will call this function to see if an insn can be split into
2270    two insns.  */
2271
2272 static rtx *
2273 find_split_point (loc, insn)
2274      rtx *loc;
2275      rtx insn;
2276 {
2277   rtx x = *loc;
2278   enum rtx_code code = GET_CODE (x);
2279   rtx *split;
2280   int len = 0, pos, unsignedp;
2281   rtx inner;
2282
2283   /* First special-case some codes.  */
2284   switch (code)
2285     {
2286     case SUBREG:
2287 #ifdef INSN_SCHEDULING
2288       /* If we are making a paradoxical SUBREG invalid, it becomes a split
2289          point.  */
2290       if (GET_CODE (SUBREG_REG (x)) == MEM)
2291         return loc;
2292 #endif
2293       return find_split_point (&SUBREG_REG (x), insn);
2294
2295     case MEM:
2296 #ifdef HAVE_lo_sum
2297       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2298          using LO_SUM and HIGH.  */
2299       if (GET_CODE (XEXP (x, 0)) == CONST
2300           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2301         {
2302           SUBST (XEXP (x, 0),
2303                  gen_rtx_combine (LO_SUM, Pmode,
2304                                   gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2305                                   XEXP (x, 0)));
2306           return &XEXP (XEXP (x, 0), 0);
2307         }
2308 #endif
2309
2310       /* If we have a PLUS whose second operand is a constant and the
2311          address is not valid, perhaps will can split it up using
2312          the machine-specific way to split large constants.  We use
2313          the first psuedo-reg (one of the virtual regs) as a placeholder;
2314          it will not remain in the result.  */
2315       if (GET_CODE (XEXP (x, 0)) == PLUS
2316           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2317           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2318         {
2319           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2320           rtx seq = split_insns (gen_rtx (SET, VOIDmode, reg, XEXP (x, 0)),
2321                                  subst_insn);
2322
2323           /* This should have produced two insns, each of which sets our
2324              placeholder.  If the source of the second is a valid address,
2325              we can make put both sources together and make a split point
2326              in the middle.  */
2327
2328           if (seq && XVECLEN (seq, 0) == 2
2329               && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2330               && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2331               && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2332               && ! reg_mentioned_p (reg,
2333                                     SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2334               && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2335               && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2336               && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2337               && memory_address_p (GET_MODE (x),
2338                                    SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2339             {
2340               rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2341               rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2342
2343               /* Replace the placeholder in SRC2 with SRC1.  If we can
2344                  find where in SRC2 it was placed, that can become our
2345                  split point and we can replace this address with SRC2.
2346                  Just try two obvious places.  */
2347
2348               src2 = replace_rtx (src2, reg, src1);
2349               split = 0;
2350               if (XEXP (src2, 0) == src1)
2351                 split = &XEXP (src2, 0);
2352               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2353                        && XEXP (XEXP (src2, 0), 0) == src1)
2354                 split = &XEXP (XEXP (src2, 0), 0);
2355
2356               if (split)
2357                 {
2358                   SUBST (XEXP (x, 0), src2);
2359                   return split;
2360                 }
2361             }
2362           
2363           /* If that didn't work, perhaps the first operand is complex and
2364              needs to be computed separately, so make a split point there.
2365              This will occur on machines that just support REG + CONST
2366              and have a constant moved through some previous computation.  */
2367
2368           else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2369                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2370                          && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2371                              == 'o')))
2372             return &XEXP (XEXP (x, 0), 0);
2373         }
2374       break;
2375
2376     case SET:
2377 #ifdef HAVE_cc0
2378       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2379          ZERO_EXTRACT, the most likely reason why this doesn't match is that
2380          we need to put the operand into a register.  So split at that
2381          point.  */
2382
2383       if (SET_DEST (x) == cc0_rtx
2384           && GET_CODE (SET_SRC (x)) != COMPARE
2385           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2386           && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2387           && ! (GET_CODE (SET_SRC (x)) == SUBREG
2388                 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2389         return &SET_SRC (x);
2390 #endif
2391
2392       /* See if we can split SET_SRC as it stands.  */
2393       split = find_split_point (&SET_SRC (x), insn);
2394       if (split && split != &SET_SRC (x))
2395         return split;
2396
2397       /* See if this is a bitfield assignment with everything constant.  If
2398          so, this is an IOR of an AND, so split it into that.  */
2399       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2400           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2401               <= HOST_BITS_PER_WIDE_INT)
2402           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2403           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2404           && GET_CODE (SET_SRC (x)) == CONST_INT
2405           && ((INTVAL (XEXP (SET_DEST (x), 1))
2406               + INTVAL (XEXP (SET_DEST (x), 2)))
2407               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2408           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2409         {
2410           int pos = INTVAL (XEXP (SET_DEST (x), 2));
2411           int len = INTVAL (XEXP (SET_DEST (x), 1));
2412           int src = INTVAL (SET_SRC (x));
2413           rtx dest = XEXP (SET_DEST (x), 0);
2414           enum machine_mode mode = GET_MODE (dest);
2415           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2416
2417 #if BITS_BIG_ENDIAN
2418           pos = GET_MODE_BITSIZE (mode) - len - pos;
2419 #endif
2420
2421           if (src == mask)
2422             SUBST (SET_SRC (x),
2423                    gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2424           else
2425             SUBST (SET_SRC (x),
2426                    gen_binary (IOR, mode,
2427                                gen_binary (AND, mode, dest, 
2428                                            GEN_INT (~ (mask << pos)
2429                                                     & GET_MODE_MASK (mode))),
2430                                GEN_INT (src << pos)));
2431
2432           SUBST (SET_DEST (x), dest);
2433
2434           split = find_split_point (&SET_SRC (x), insn);
2435           if (split && split != &SET_SRC (x))
2436             return split;
2437         }
2438
2439       /* Otherwise, see if this is an operation that we can split into two.
2440          If so, try to split that.  */
2441       code = GET_CODE (SET_SRC (x));
2442
2443       switch (code)
2444         {
2445         case AND:
2446           /* If we are AND'ing with a large constant that is only a single
2447              bit and the result is only being used in a context where we
2448              need to know if it is zero or non-zero, replace it with a bit
2449              extraction.  This will avoid the large constant, which might
2450              have taken more than one insn to make.  If the constant were
2451              not a valid argument to the AND but took only one insn to make,
2452              this is no worse, but if it took more than one insn, it will
2453              be better.  */
2454
2455           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2456               && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2457               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2458               && GET_CODE (SET_DEST (x)) == REG
2459               && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2460               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2461               && XEXP (*split, 0) == SET_DEST (x)
2462               && XEXP (*split, 1) == const0_rtx)
2463             {
2464               SUBST (SET_SRC (x),
2465                      make_extraction (GET_MODE (SET_DEST (x)),
2466                                       XEXP (SET_SRC (x), 0),
2467                                       pos, NULL_RTX, 1, 1, 0, 0));
2468               return find_split_point (loc, insn);
2469             }
2470           break;
2471
2472         case SIGN_EXTEND:
2473           inner = XEXP (SET_SRC (x), 0);
2474           pos = 0;
2475           len = GET_MODE_BITSIZE (GET_MODE (inner));
2476           unsignedp = 0;
2477           break;
2478
2479         case SIGN_EXTRACT:
2480         case ZERO_EXTRACT:
2481           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2482               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2483             {
2484               inner = XEXP (SET_SRC (x), 0);
2485               len = INTVAL (XEXP (SET_SRC (x), 1));
2486               pos = INTVAL (XEXP (SET_SRC (x), 2));
2487
2488 #if BITS_BIG_ENDIAN
2489               pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
2490 #endif
2491               unsignedp = (code == ZERO_EXTRACT);
2492             }
2493           break;
2494         }
2495
2496       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2497         {
2498           enum machine_mode mode = GET_MODE (SET_SRC (x));
2499
2500           /* For unsigned, we have a choice of a shift followed by an
2501              AND or two shifts.  Use two shifts for field sizes where the
2502              constant might be too large.  We assume here that we can
2503              always at least get 8-bit constants in an AND insn, which is
2504              true for every current RISC.  */
2505
2506           if (unsignedp && len <= 8)
2507             {
2508               SUBST (SET_SRC (x),
2509                      gen_rtx_combine
2510                      (AND, mode,
2511                       gen_rtx_combine (LSHIFTRT, mode,
2512                                        gen_lowpart_for_combine (mode, inner),
2513                                        GEN_INT (pos)),
2514                       GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
2515
2516               split = find_split_point (&SET_SRC (x), insn);
2517               if (split && split != &SET_SRC (x))
2518                 return split;
2519             }
2520           else
2521             {
2522               SUBST (SET_SRC (x),
2523                      gen_rtx_combine
2524                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
2525                       gen_rtx_combine (ASHIFT, mode,
2526                                        gen_lowpart_for_combine (mode, inner),
2527                                        GEN_INT (GET_MODE_BITSIZE (mode)
2528                                                 - len - pos)),
2529                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
2530
2531               split = find_split_point (&SET_SRC (x), insn);
2532               if (split && split != &SET_SRC (x))
2533                 return split;
2534             }
2535         }
2536
2537       /* See if this is a simple operation with a constant as the second
2538          operand.  It might be that this constant is out of range and hence
2539          could be used as a split point.  */
2540       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2541            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2542            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2543           && CONSTANT_P (XEXP (SET_SRC (x), 1))
2544           && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2545               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2546                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2547                       == 'o'))))
2548         return &XEXP (SET_SRC (x), 1);
2549
2550       /* Finally, see if this is a simple operation with its first operand
2551          not in a register.  The operation might require this operand in a
2552          register, so return it as a split point.  We can always do this
2553          because if the first operand were another operation, we would have
2554          already found it as a split point.  */
2555       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2556            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2557            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2558            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2559           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2560         return &XEXP (SET_SRC (x), 0);
2561
2562       return 0;
2563
2564     case AND:
2565     case IOR:
2566       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2567          it is better to write this as (not (ior A B)) so we can split it.
2568          Similarly for IOR.  */
2569       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2570         {
2571           SUBST (*loc,
2572                  gen_rtx_combine (NOT, GET_MODE (x),
2573                                   gen_rtx_combine (code == IOR ? AND : IOR,
2574                                                    GET_MODE (x),
2575                                                    XEXP (XEXP (x, 0), 0),
2576                                                    XEXP (XEXP (x, 1), 0))));
2577           return find_split_point (loc, insn);
2578         }
2579
2580       /* Many RISC machines have a large set of logical insns.  If the
2581          second operand is a NOT, put it first so we will try to split the
2582          other operand first.  */
2583       if (GET_CODE (XEXP (x, 1)) == NOT)
2584         {
2585           rtx tem = XEXP (x, 0);
2586           SUBST (XEXP (x, 0), XEXP (x, 1));
2587           SUBST (XEXP (x, 1), tem);
2588         }
2589       break;
2590     }
2591
2592   /* Otherwise, select our actions depending on our rtx class.  */
2593   switch (GET_RTX_CLASS (code))
2594     {
2595     case 'b':                   /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
2596     case '3':
2597       split = find_split_point (&XEXP (x, 2), insn);
2598       if (split)
2599         return split;
2600       /* ... fall through ... */
2601     case '2':
2602     case 'c':
2603     case '<':
2604       split = find_split_point (&XEXP (x, 1), insn);
2605       if (split)
2606         return split;
2607       /* ... fall through ... */
2608     case '1':
2609       /* Some machines have (and (shift ...) ...) insns.  If X is not
2610          an AND, but XEXP (X, 0) is, use it as our split point.  */
2611       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
2612         return &XEXP (x, 0);
2613
2614       split = find_split_point (&XEXP (x, 0), insn);
2615       if (split)
2616         return split;
2617       return loc;
2618     }
2619
2620   /* Otherwise, we don't have a split point.  */
2621   return 0;
2622 }
2623 \f
2624 /* Throughout X, replace FROM with TO, and return the result.
2625    The result is TO if X is FROM;
2626    otherwise the result is X, but its contents may have been modified.
2627    If they were modified, a record was made in undobuf so that
2628    undo_all will (among other things) return X to its original state.
2629
2630    If the number of changes necessary is too much to record to undo,
2631    the excess changes are not made, so the result is invalid.
2632    The changes already made can still be undone.
2633    undobuf.num_undo is incremented for such changes, so by testing that
2634    the caller can tell whether the result is valid.
2635
2636    `n_occurrences' is incremented each time FROM is replaced.
2637    
2638    IN_DEST is non-zero if we are processing the SET_DEST of a SET.
2639
2640    UNIQUE_COPY is non-zero if each substitution must be unique.  We do this
2641    by copying if `n_occurrences' is non-zero.  */
2642
2643 static rtx
2644 subst (x, from, to, in_dest, unique_copy)
2645      register rtx x, from, to;
2646      int in_dest;
2647      int unique_copy;
2648 {
2649   register char *fmt;
2650   register int len, i;
2651   register enum rtx_code code = GET_CODE (x), orig_code = code;
2652   rtx temp;
2653   enum machine_mode mode = GET_MODE (x);
2654   enum machine_mode op0_mode = VOIDmode;
2655   rtx other_insn;
2656   rtx *cc_use;
2657   int n_restarts = 0;
2658
2659 /* FAKE_EXTEND_SAFE_P (MODE, FROM) is 1 if (subreg:MODE FROM 0) is a safe
2660    replacement for (zero_extend:MODE FROM) or (sign_extend:MODE FROM).
2661    If it is 0, that cannot be done.  We can now do this for any MEM
2662    because (SUBREG (MEM...)) is guaranteed to cause the MEM to be reloaded.
2663    If not for that, MEM's would very rarely be safe.  */
2664
2665 /* Reject MODEs bigger than a word, because we might not be able
2666    to reference a two-register group starting with an arbitrary register
2667    (and currently gen_lowpart might crash for a SUBREG).  */
2668
2669 #define FAKE_EXTEND_SAFE_P(MODE, FROM) \
2670   (GET_MODE_SIZE (MODE) <= UNITS_PER_WORD)
2671
2672 /* Two expressions are equal if they are identical copies of a shared
2673    RTX or if they are both registers with the same register number
2674    and mode.  */
2675
2676 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
2677   ((X) == (Y)                                           \
2678    || (GET_CODE (X) == REG && GET_CODE (Y) == REG       \
2679        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
2680
2681   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
2682     {
2683       n_occurrences++;
2684       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
2685     }
2686
2687   /* If X and FROM are the same register but different modes, they will
2688      not have been seen as equal above.  However, flow.c will make a 
2689      LOG_LINKS entry for that case.  If we do nothing, we will try to
2690      rerecognize our original insn and, when it succeeds, we will
2691      delete the feeding insn, which is incorrect.
2692
2693      So force this insn not to match in this (rare) case.  */
2694   if (! in_dest && code == REG && GET_CODE (from) == REG
2695       && REGNO (x) == REGNO (from))
2696     return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
2697
2698   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
2699      of which may contain things that can be combined.  */
2700   if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
2701     return x;
2702
2703   /* It is possible to have a subexpression appear twice in the insn.
2704      Suppose that FROM is a register that appears within TO.
2705      Then, after that subexpression has been scanned once by `subst',
2706      the second time it is scanned, TO may be found.  If we were
2707      to scan TO here, we would find FROM within it and create a
2708      self-referent rtl structure which is completely wrong.  */
2709   if (COMBINE_RTX_EQUAL_P (x, to))
2710     return to;
2711
2712   len = GET_RTX_LENGTH (code);
2713   fmt = GET_RTX_FORMAT (code);
2714
2715   /* We don't need to process a SET_DEST that is a register, CC0, or PC, so
2716      set up to skip this common case.  All other cases where we want to
2717      suppress replacing something inside a SET_SRC are handled via the
2718      IN_DEST operand.  */
2719   if (code == SET
2720       && (GET_CODE (SET_DEST (x)) == REG
2721         || GET_CODE (SET_DEST (x)) == CC0
2722         || GET_CODE (SET_DEST (x)) == PC))
2723     fmt = "ie";
2724
2725   /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a constant. */
2726   if (fmt[0] == 'e')
2727     op0_mode = GET_MODE (XEXP (x, 0));
2728
2729   for (i = 0; i < len; i++)
2730     {
2731       if (fmt[i] == 'E')
2732         {
2733           register int j;
2734           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2735             {
2736               register rtx new;
2737               if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
2738                 {
2739                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
2740                   n_occurrences++;
2741                 }
2742               else
2743                 {
2744                   new = subst (XVECEXP (x, i, j), from, to, 0, unique_copy);
2745
2746                   /* If this substitution failed, this whole thing fails.  */
2747                   if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
2748                     return new;
2749                 }
2750
2751               SUBST (XVECEXP (x, i, j), new);
2752             }
2753         }
2754       else if (fmt[i] == 'e')
2755         {
2756           register rtx new;
2757
2758           if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
2759             {
2760               new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
2761               n_occurrences++;
2762             }
2763           else
2764             /* If we are in a SET_DEST, suppress most cases unless we
2765                have gone inside a MEM, in which case we want to
2766                simplify the address.  We assume here that things that
2767                are actually part of the destination have their inner
2768                parts in the first expression.  This is true for SUBREG, 
2769                STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
2770                things aside from REG and MEM that should appear in a
2771                SET_DEST.  */
2772             new = subst (XEXP (x, i), from, to,
2773                          (((in_dest
2774                             && (code == SUBREG || code == STRICT_LOW_PART
2775                                 || code == ZERO_EXTRACT))
2776                            || code == SET)
2777                           && i == 0), unique_copy);
2778
2779           /* If we found that we will have to reject this combination,
2780              indicate that by returning the CLOBBER ourselves, rather than
2781              an expression containing it.  This will speed things up as
2782              well as prevent accidents where two CLOBBERs are considered
2783              to be equal, thus producing an incorrect simplification.  */
2784
2785           if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
2786             return new;
2787
2788           SUBST (XEXP (x, i), new);
2789         }
2790     }
2791
2792   /* We come back to here if we have replaced the expression with one of
2793      a different code and it is likely that further simplification will be
2794      possible.  */
2795
2796  restart:
2797
2798   /* If we have restarted more than 4 times, we are probably looping, so
2799      give up.  */
2800   if (++n_restarts > 4)
2801     return x;
2802
2803   /* If we are restarting at all, it means that we no longer know the
2804      original mode of operand 0 (since we have probably changed the
2805      form of X).  */
2806
2807   if (n_restarts > 1)
2808     op0_mode = VOIDmode;
2809
2810   code = GET_CODE (x);
2811
2812   /* If this is a commutative operation, put a constant last and a complex
2813      expression first.  We don't need to do this for comparisons here.  */
2814   if (GET_RTX_CLASS (code) == 'c'
2815       && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
2816           || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
2817               && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
2818           || (GET_CODE (XEXP (x, 0)) == SUBREG
2819               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
2820               && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
2821     {
2822       temp = XEXP (x, 0);
2823       SUBST (XEXP (x, 0), XEXP (x, 1));
2824       SUBST (XEXP (x, 1), temp);
2825     }
2826
2827   /* If this is a PLUS, MINUS, or MULT, and the first operand is the
2828      sign extension of a PLUS with a constant, reverse the order of the sign
2829      extension and the addition. Note that this not the same as the original
2830      code, but overflow is undefined for signed values.  Also note that the
2831      PLUS will have been partially moved "inside" the sign-extension, so that
2832      the first operand of X will really look like:
2833          (ashiftrt (plus (ashift A C4) C5) C4).
2834      We convert this to
2835          (plus (ashiftrt (ashift A C4) C2) C4)
2836      and replace the first operand of X with that expression.  Later parts
2837      of this function may simplify the expression further.
2838
2839      For example, if we start with (mult (sign_extend (plus A C1)) C2),
2840      we swap the SIGN_EXTEND and PLUS.  Later code will apply the
2841      distributive law to produce (plus (mult (sign_extend X) C1) C3).
2842
2843      We do this to simplify address expressions.  */
2844
2845   if ((code == PLUS || code == MINUS || code == MULT)
2846       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
2847       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
2848       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
2849       && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
2850       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2851       && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
2852       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
2853       && (temp = simplify_binary_operation (ASHIFTRT, mode,
2854                                             XEXP (XEXP (XEXP (x, 0), 0), 1),
2855                                             XEXP (XEXP (x, 0), 1))) != 0)
2856     {
2857       rtx new
2858         = simplify_shift_const (NULL_RTX, ASHIFT, mode,
2859                                 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
2860                                 INTVAL (XEXP (XEXP (x, 0), 1)));
2861
2862       new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
2863                                   INTVAL (XEXP (XEXP (x, 0), 1)));
2864
2865       SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
2866     }
2867
2868   /* If this is a simple operation applied to an IF_THEN_ELSE, try 
2869      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
2870      things.  Don't deal with operations that change modes here.  */
2871
2872   if ((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c')
2873       && GET_CODE (XEXP (x, 0)) == IF_THEN_ELSE)
2874     {
2875       /* Don't do this by using SUBST inside X since we might be messing
2876          up a shared expression.  */
2877       rtx cond = XEXP (XEXP (x, 0), 0);
2878       rtx t_arm = subst (gen_binary (code, mode, XEXP (XEXP (x, 0), 1),
2879                                      XEXP (x, 1)),
2880                          pc_rtx, pc_rtx, 0, 0);
2881       rtx f_arm = subst (gen_binary (code, mode, XEXP (XEXP (x, 0), 2),
2882                                      XEXP (x, 1)),
2883                          pc_rtx, pc_rtx, 0, 0);
2884
2885
2886       x = gen_rtx (IF_THEN_ELSE, mode, cond, t_arm, f_arm);
2887       goto restart;
2888     }
2889
2890   else if (GET_RTX_CLASS (code) == '1'
2891            && GET_CODE (XEXP (x, 0)) == IF_THEN_ELSE
2892            && GET_MODE (XEXP (x, 0)) == mode)
2893     {
2894       rtx cond = XEXP (XEXP (x, 0), 0);
2895       rtx t_arm = subst (gen_unary (code, mode, XEXP (XEXP (x, 0), 1)),
2896                          pc_rtx, pc_rtx, 0, 0);
2897       rtx f_arm = subst (gen_unary (code, mode, XEXP (XEXP (x, 0), 2)),
2898                          pc_rtx, pc_rtx, 0, 0);
2899
2900       x = gen_rtx_combine (IF_THEN_ELSE, mode, cond, t_arm, f_arm);
2901       goto restart;
2902     }
2903
2904   /* Try to fold this expression in case we have constants that weren't
2905      present before.  */
2906   temp = 0;
2907   switch (GET_RTX_CLASS (code))
2908     {
2909     case '1':
2910       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
2911       break;
2912     case '<':
2913       temp = simplify_relational_operation (code, op0_mode,
2914                                             XEXP (x, 0), XEXP (x, 1));
2915 #ifdef FLOAT_STORE_FLAG_VALUE
2916       if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2917         temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
2918                 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
2919 #endif
2920       break;
2921     case 'c':
2922     case '2':
2923       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
2924       break;
2925     case 'b':
2926     case '3':
2927       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
2928                                          XEXP (x, 1), XEXP (x, 2));
2929       break;
2930     }
2931
2932   if (temp)
2933     x = temp, code = GET_CODE (temp);
2934
2935   /* First see if we can apply the inverse distributive law.  */
2936   if (code == PLUS || code == MINUS
2937       || code == AND || code == IOR || code == XOR)
2938     {
2939       x = apply_distributive_law (x);
2940       code = GET_CODE (x);
2941     }
2942
2943   /* If CODE is an associative operation not otherwise handled, see if we
2944      can associate some operands.  This can win if they are constants or
2945      if they are logically related (i.e. (a & b) & a.  */
2946   if ((code == PLUS || code == MINUS
2947        || code == MULT || code == AND || code == IOR || code == XOR
2948        || code == DIV || code == UDIV
2949        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
2950       && INTEGRAL_MODE_P (mode))
2951     {
2952       if (GET_CODE (XEXP (x, 0)) == code)
2953         {
2954           rtx other = XEXP (XEXP (x, 0), 0);
2955           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
2956           rtx inner_op1 = XEXP (x, 1);
2957           rtx inner;
2958           
2959           /* Make sure we pass the constant operand if any as the second
2960              one if this is a commutative operation.  */
2961           if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
2962             {
2963               rtx tem = inner_op0;
2964               inner_op0 = inner_op1;
2965               inner_op1 = tem;
2966             }
2967           inner = simplify_binary_operation (code == MINUS ? PLUS
2968                                              : code == DIV ? MULT
2969                                              : code == UDIV ? MULT
2970                                              : code,
2971                                              mode, inner_op0, inner_op1);
2972
2973           /* For commutative operations, try the other pair if that one
2974              didn't simplify.  */
2975           if (inner == 0 && GET_RTX_CLASS (code) == 'c')
2976             {
2977               other = XEXP (XEXP (x, 0), 1);
2978               inner = simplify_binary_operation (code, mode,
2979                                                  XEXP (XEXP (x, 0), 0),
2980                                                  XEXP (x, 1));
2981             }
2982
2983           if (inner)
2984             {
2985               x = gen_binary (code, mode, other, inner);
2986               goto restart;
2987             
2988             }
2989         }
2990     }
2991
2992   /* A little bit of algebraic simplification here.  */
2993   switch (code)
2994     {
2995     case MEM:
2996       /* Ensure that our address has any ASHIFTs converted to MULT in case
2997          address-recognizing predicates are called later.  */
2998       temp = make_compound_operation (XEXP (x, 0), MEM);
2999       SUBST (XEXP (x, 0), temp);
3000       break;
3001
3002     case SUBREG:
3003       /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3004          is paradoxical.  If we can't do that safely, then it becomes
3005          something nonsensical so that this combination won't take place.  */
3006
3007       if (GET_CODE (SUBREG_REG (x)) == MEM
3008           && (GET_MODE_SIZE (mode)
3009               <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3010         {
3011           rtx inner = SUBREG_REG (x);
3012           int endian_offset = 0;
3013           /* Don't change the mode of the MEM
3014              if that would change the meaning of the address.  */
3015           if (MEM_VOLATILE_P (SUBREG_REG (x))
3016               || mode_dependent_address_p (XEXP (inner, 0)))
3017             return gen_rtx (CLOBBER, mode, const0_rtx);
3018
3019 #if BYTES_BIG_ENDIAN
3020           if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3021             endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3022           if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3023             endian_offset -= UNITS_PER_WORD - GET_MODE_SIZE (GET_MODE (inner));
3024 #endif
3025           /* Note if the plus_constant doesn't make a valid address
3026              then this combination won't be accepted.  */
3027           x = gen_rtx (MEM, mode,
3028                        plus_constant (XEXP (inner, 0),
3029                                       (SUBREG_WORD (x) * UNITS_PER_WORD
3030                                        + endian_offset)));
3031           MEM_VOLATILE_P (x) = MEM_VOLATILE_P (inner);
3032           RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3033           MEM_IN_STRUCT_P (x) = MEM_IN_STRUCT_P (inner);
3034           return x;
3035         }
3036
3037       /* If we are in a SET_DEST, these other cases can't apply.  */
3038       if (in_dest)
3039         return x;
3040
3041       /* Changing mode twice with SUBREG => just change it once,
3042          or not at all if changing back to starting mode.  */
3043       if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3044         {
3045           if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3046               && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3047             return SUBREG_REG (SUBREG_REG (x));
3048
3049           SUBST_INT (SUBREG_WORD (x),
3050                      SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3051           SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3052         }
3053
3054       /* SUBREG of a hard register => just change the register number
3055          and/or mode.  If the hard register is not valid in that mode,
3056          suppress this combination.  If the hard register is the stack,
3057          frame, or argument pointer, leave this as a SUBREG.  */
3058
3059       if (GET_CODE (SUBREG_REG (x)) == REG
3060           && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3061           && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3062 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3063           && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3064 #endif
3065           && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3066         {
3067           if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3068                                   mode))
3069             return gen_rtx (REG, mode,
3070                             REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3071           else
3072             return gen_rtx (CLOBBER, mode, const0_rtx);
3073         }
3074
3075       /* For a constant, try to pick up the part we want.  Handle a full
3076          word and low-order part.  Only do this if we are narrowing
3077          the constant; if it is being widened, we have no idea what
3078          the extra bits will have been set to.  */
3079
3080       if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3081           && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3082           && GET_MODE_SIZE (op0_mode) < UNITS_PER_WORD
3083           && GET_MODE_CLASS (mode) == MODE_INT)
3084         {
3085           temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3086                                   0, op0_mode);
3087           if (temp)
3088             return temp;
3089         }
3090         
3091       /* If we want a subreg of a constant, at offset 0,
3092          take the low bits.  On a little-endian machine, that's
3093          always valid.  On a big-endian machine, it's valid
3094          only if the constant's mode fits in one word.  */
3095       if (CONSTANT_P (SUBREG_REG (x)) && subreg_lowpart_p (x)
3096           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (op0_mode)
3097 #if WORDS_BIG_ENDIAN
3098           && GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD
3099 #endif
3100           )
3101         return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3102
3103       /* If we are narrowing the object, we need to see if we can simplify
3104          the expression for the object knowing that we only need the
3105          low-order bits.  */
3106
3107       if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
3108           && subreg_lowpart_p (x))
3109         return force_to_mode (SUBREG_REG (x), mode, GET_MODE_BITSIZE (mode),
3110                               NULL_RTX);
3111       break;
3112
3113     case NOT:
3114       /* (not (plus X -1)) can become (neg X).  */
3115       if (GET_CODE (XEXP (x, 0)) == PLUS
3116           && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3117         {
3118           x = gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3119           goto restart;
3120         }
3121
3122       /* Similarly, (not (neg X)) is (plus X -1).  */
3123       if (GET_CODE (XEXP (x, 0)) == NEG)
3124         {
3125           x = gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3126           goto restart;
3127         }
3128
3129       /* (not (xor X C)) for C constant is (xor X D) with D = ~ C.  */
3130       if (GET_CODE (XEXP (x, 0)) == XOR
3131           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3132           && (temp = simplify_unary_operation (NOT, mode,
3133                                                XEXP (XEXP (x, 0), 1),
3134                                                mode)) != 0)
3135         {
3136           SUBST (XEXP (XEXP (x, 0), 1), temp);
3137           return XEXP (x, 0);
3138         }
3139               
3140       /* (not (ashift 1 X)) is (rotate ~1 X).  We used to do this for operands
3141          other than 1, but that is not valid.  We could do a similar
3142          simplification for (not (lshiftrt C X)) where C is just the sign bit,
3143          but this doesn't seem common enough to bother with.  */
3144       if (GET_CODE (XEXP (x, 0)) == ASHIFT
3145           && XEXP (XEXP (x, 0), 0) == const1_rtx)
3146         {
3147           x = gen_rtx (ROTATE, mode, gen_unary (NOT, mode, const1_rtx),
3148                        XEXP (XEXP (x, 0), 1));
3149           goto restart;
3150         }
3151                                             
3152       if (GET_CODE (XEXP (x, 0)) == SUBREG
3153           && subreg_lowpart_p (XEXP (x, 0))
3154           && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3155               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3156           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3157           && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3158         {
3159           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3160
3161           x = gen_rtx (ROTATE, inner_mode,
3162                        gen_unary (NOT, inner_mode, const1_rtx),
3163                        XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3164           x = gen_lowpart_for_combine (mode, x);
3165           goto restart;
3166         }
3167                                             
3168 #if STORE_FLAG_VALUE == -1
3169       /* (not (comparison foo bar)) can be done by reversing the comparison
3170          code if valid.  */
3171       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3172           && reversible_comparison_p (XEXP (x, 0)))
3173         return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3174                                 mode, XEXP (XEXP (x, 0), 0),
3175                                 XEXP (XEXP (x, 0), 1));
3176
3177       /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3178          is (lt foo (const_int 0)), so we can perform the above
3179          simplification.  */
3180
3181       if (XEXP (x, 1) == const1_rtx
3182           && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3183           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3184           && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3185         return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3186 #endif
3187
3188       /* Apply De Morgan's laws to reduce number of patterns for machines
3189          with negating logical insns (and-not, nand, etc.).  If result has
3190          only one NOT, put it first, since that is how the patterns are
3191          coded.  */
3192
3193       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3194         {
3195          rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3196
3197          if (GET_CODE (in1) == NOT)
3198            in1 = XEXP (in1, 0);
3199          else
3200            in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3201
3202          if (GET_CODE (in2) == NOT)
3203            in2 = XEXP (in2, 0);
3204          else if (GET_CODE (in2) == CONST_INT
3205                   && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3206            in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
3207          else
3208            in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3209
3210          if (GET_CODE (in2) == NOT)
3211            {
3212              rtx tem = in2;
3213              in2 = in1; in1 = tem;
3214            }
3215
3216          x = gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3217                               mode, in1, in2);
3218          goto restart;
3219        } 
3220       break;
3221
3222     case NEG:
3223       /* (neg (plus X 1)) can become (not X).  */
3224       if (GET_CODE (XEXP (x, 0)) == PLUS
3225           && XEXP (XEXP (x, 0), 1) == const1_rtx)
3226         {
3227           x = gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3228           goto restart;
3229         }
3230
3231       /* Similarly, (neg (not X)) is (plus X 1).  */
3232       if (GET_CODE (XEXP (x, 0)) == NOT)
3233         {
3234           x = gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0), const1_rtx);
3235           goto restart;
3236         }
3237
3238       /* (neg (minus X Y)) can become (minus Y X).  */
3239       if (GET_CODE (XEXP (x, 0)) == MINUS
3240           && (! FLOAT_MODE_P (mode)
3241               /* x-y != -(y-x) with IEEE floating point. */
3242               || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT))
3243         {
3244           x = gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3245                           XEXP (XEXP (x, 0), 0));
3246           goto restart;
3247         }
3248
3249       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
3250       if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3251           && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3252         {
3253           x = gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3254           goto restart;
3255         }
3256
3257       /* NEG commutes with ASHIFT since it is multiplication.  Only do this
3258          if we can then eliminate the NEG (e.g.,
3259          if the operand is a constant).  */
3260
3261       if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3262         {
3263           temp = simplify_unary_operation (NEG, mode,
3264                                            XEXP (XEXP (x, 0), 0), mode);
3265           if (temp)
3266             {
3267               SUBST (XEXP (XEXP (x, 0), 0), temp);
3268               return XEXP (x, 0);
3269             }
3270         }
3271
3272       temp = expand_compound_operation (XEXP (x, 0));
3273
3274       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3275          replaced by (lshiftrt X C).  This will convert
3276          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
3277
3278       if (GET_CODE (temp) == ASHIFTRT
3279           && GET_CODE (XEXP (temp, 1)) == CONST_INT
3280           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3281         {
3282           x = simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3283                                     INTVAL (XEXP (temp, 1)));
3284           goto restart;
3285         }
3286
3287       /* If X has only a single bit that might be nonzero, say, bit I, convert
3288          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3289          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
3290          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
3291          or a SUBREG of one since we'd be making the expression more
3292          complex if it was just a register.  */
3293
3294       if (GET_CODE (temp) != REG
3295           && ! (GET_CODE (temp) == SUBREG
3296                 && GET_CODE (SUBREG_REG (temp)) == REG)
3297           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3298         {
3299           rtx temp1 = simplify_shift_const
3300             (NULL_RTX, ASHIFTRT, mode,
3301              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3302                                    GET_MODE_BITSIZE (mode) - 1 - i),
3303              GET_MODE_BITSIZE (mode) - 1 - i);
3304
3305           /* If all we did was surround TEMP with the two shifts, we
3306              haven't improved anything, so don't use it.  Otherwise,
3307              we are better off with TEMP1.  */
3308           if (GET_CODE (temp1) != ASHIFTRT
3309               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3310               || XEXP (XEXP (temp1, 0), 0) != temp)
3311             {
3312               x = temp1;
3313               goto restart;
3314             }
3315         }
3316       break;
3317
3318     case FLOAT_TRUNCATE:
3319       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
3320       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3321           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3322         return XEXP (XEXP (x, 0), 0);
3323       break;  
3324
3325 #ifdef HAVE_cc0
3326     case COMPARE:
3327       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3328          using cc0, in which case we want to leave it as a COMPARE
3329          so we can distinguish it from a register-register-copy.  */
3330       if (XEXP (x, 1) == const0_rtx)
3331         return XEXP (x, 0);
3332
3333       /* In IEEE floating point, x-0 is not the same as x.  */
3334       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3335            || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))))
3336           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3337         return XEXP (x, 0);
3338       break;
3339 #endif
3340
3341     case CONST:
3342       /* (const (const X)) can become (const X).  Do it this way rather than
3343          returning the inner CONST since CONST can be shared with a
3344          REG_EQUAL note.  */
3345       if (GET_CODE (XEXP (x, 0)) == CONST)
3346         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3347       break;
3348
3349 #ifdef HAVE_lo_sum
3350     case LO_SUM:
3351       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
3352          can add in an offset.  find_split_point will split this address up
3353          again if it doesn't match.  */
3354       if (GET_CODE (XEXP (x, 0)) == HIGH
3355           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3356         return XEXP (x, 1);
3357       break;
3358 #endif
3359
3360     case PLUS:
3361       /* If we have (plus (plus (A const) B)), associate it so that CONST is
3362          outermost.  That's because that's the way indexed addresses are
3363          supposed to appear.  This code used to check many more cases, but
3364          they are now checked elsewhere.  */
3365       if (GET_CODE (XEXP (x, 0)) == PLUS
3366           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3367         return gen_binary (PLUS, mode,
3368                            gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3369                                        XEXP (x, 1)),
3370                            XEXP (XEXP (x, 0), 1));
3371
3372       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3373          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3374          bit-field and can be replaced by either a sign_extend or a
3375          sign_extract.  The `and' may be a zero_extend.  */
3376       if (GET_CODE (XEXP (x, 0)) == XOR
3377           && GET_CODE (XEXP (x, 1)) == CONST_INT
3378           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3379           && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
3380           && (i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
3381           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3382           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3383                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3384                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
3385                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
3386               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3387                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3388                       == i + 1))))
3389         {
3390           x = simplify_shift_const
3391             (NULL_RTX, ASHIFTRT, mode,
3392              simplify_shift_const (NULL_RTX, ASHIFT, mode,
3393                                    XEXP (XEXP (XEXP (x, 0), 0), 0),
3394                                    GET_MODE_BITSIZE (mode) - (i + 1)),
3395              GET_MODE_BITSIZE (mode) - (i + 1));
3396           goto restart;
3397         }
3398
3399       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3400          C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3401          is 1.  This produces better code than the alternative immediately
3402          below.  */
3403       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3404           && reversible_comparison_p (XEXP (x, 0))
3405           && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3406               || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
3407         {
3408           x = gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3409                           mode, XEXP (XEXP (x, 0), 0), XEXP (XEXP (x, 0), 1));
3410           x = gen_unary (NEG, mode, x);
3411           goto restart;
3412         }
3413
3414       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
3415          can become (ashiftrt (ashift (xor x 1) C) C) where C is
3416          the bitsize of the mode - 1.  This allows simplification of
3417          "a = (b & 8) == 0;"  */
3418       if (XEXP (x, 1) == constm1_rtx
3419           && GET_CODE (XEXP (x, 0)) != REG
3420           && ! (GET_CODE (XEXP (x,0)) == SUBREG
3421                 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
3422           && nonzero_bits (XEXP (x, 0), mode) == 1)
3423         {
3424           x = simplify_shift_const
3425             (NULL_RTX, ASHIFTRT, mode,
3426              simplify_shift_const (NULL_RTX, ASHIFT, mode,
3427                                    gen_rtx_combine (XOR, mode,
3428                                                     XEXP (x, 0), const1_rtx),
3429                                    GET_MODE_BITSIZE (mode) - 1),
3430              GET_MODE_BITSIZE (mode) - 1);
3431           goto restart;
3432         }
3433
3434       /* If we are adding two things that have no bits in common, convert
3435          the addition into an IOR.  This will often be further simplified,
3436          for example in cases like ((a & 1) + (a & 2)), which can
3437          become a & 3.  */
3438
3439       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3440           && (nonzero_bits (XEXP (x, 0), mode)
3441               & nonzero_bits (XEXP (x, 1), mode)) == 0)
3442         {
3443           x = gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
3444           goto restart;
3445         }
3446       break;
3447
3448     case MINUS:
3449       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
3450          (and <foo> (const_int pow2-1))  */
3451       if (GET_CODE (XEXP (x, 1)) == AND
3452           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3453           && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
3454           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3455         {
3456           x = simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
3457                                       - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
3458           goto restart;
3459         }
3460       break;
3461
3462     case MULT:
3463       /* If we have (mult (plus A B) C), apply the distributive law and then
3464          the inverse distributive law to see if things simplify.  This
3465          occurs mostly in addresses, often when unrolling loops.  */
3466
3467       if (GET_CODE (XEXP (x, 0)) == PLUS)
3468         {
3469           x = apply_distributive_law
3470             (gen_binary (PLUS, mode,
3471                          gen_binary (MULT, mode,
3472                                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
3473                          gen_binary (MULT, mode,
3474                                      XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
3475
3476           if (GET_CODE (x) != MULT)
3477             goto restart;
3478         }
3479
3480       /* If this is multiplication by a power of two and its first operand is
3481          a shift, treat the multiply as a shift to allow the shifts to
3482          possibly combine.  */
3483       if (GET_CODE (XEXP (x, 1)) == CONST_INT
3484           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
3485           && (GET_CODE (XEXP (x, 0)) == ASHIFT
3486               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
3487               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
3488               || GET_CODE (XEXP (x, 0)) == ROTATE
3489               || GET_CODE (XEXP (x, 0)) == ROTATERT))
3490         {
3491           x = simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0), i);
3492           goto restart;
3493         }
3494
3495       /* Convert (mult (ashift (const_int 1) A) B) to (ashift B A).  */
3496       if (GET_CODE (XEXP (x, 0)) == ASHIFT
3497           && XEXP (XEXP (x, 0), 0) == const1_rtx)
3498         return gen_rtx_combine (ASHIFT, mode, XEXP (x, 1),
3499                                 XEXP (XEXP (x, 0), 1));
3500       break;
3501
3502     case UDIV:
3503       /* If this is a divide by a power of two, treat it as a shift if
3504          its first operand is a shift.  */
3505       if (GET_CODE (XEXP (x, 1)) == CONST_INT
3506           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
3507           && (GET_CODE (XEXP (x, 0)) == ASHIFT
3508               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
3509               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
3510               || GET_CODE (XEXP (x, 0)) == ROTATE
3511               || GET_CODE (XEXP (x, 0)) == ROTATERT))
3512         {
3513           x = simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
3514           goto restart;
3515         }
3516       break;
3517
3518     case EQ:  case NE:
3519     case GT:  case GTU:  case GE:  case GEU:
3520     case LT:  case LTU:  case LE:  case LEU:
3521       /* If the first operand is a condition code, we can't do anything
3522          with it.  */
3523       if (GET_CODE (XEXP (x, 0)) == COMPARE
3524           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
3525 #ifdef HAVE_cc0
3526               && XEXP (x, 0) != cc0_rtx
3527 #endif
3528                ))
3529         {
3530           rtx op0 = XEXP (x, 0);
3531           rtx op1 = XEXP (x, 1);
3532           enum rtx_code new_code;
3533
3534           if (GET_CODE (op0) == COMPARE)
3535             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
3536
3537           /* Simplify our comparison, if possible.  */
3538           new_code = simplify_comparison (code, &op0, &op1);
3539
3540 #if STORE_FLAG_VALUE == 1
3541           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
3542              if only the low-order bit is possibly nonzero in X (such as when
3543              X is a ZERO_EXTRACT of one bit.  Similarly, we can convert
3544              EQ to (xor X 1).  Remove any ZERO_EXTRACT we made when thinking
3545              this was a comparison.  It may now be simpler to use, e.g., an
3546              AND.  If a ZERO_EXTRACT is indeed appropriate, it will
3547              be placed back by the call to make_compound_operation in the
3548              SET case.  */
3549           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3550               && op1 == const0_rtx
3551               && nonzero_bits (op0, GET_MODE (op0)) == 1)
3552             return gen_lowpart_for_combine (mode,
3553                                             expand_compound_operation (op0));
3554           else if (new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3555                    && op1 == const0_rtx
3556                    && nonzero_bits (op0, GET_MODE (op0)) == 1)
3557             {
3558               op0 = expand_compound_operation (op0);
3559
3560               x = gen_rtx_combine (XOR, mode,
3561                                    gen_lowpart_for_combine (mode, op0),
3562                                    const1_rtx);
3563               goto restart;
3564             }
3565 #endif
3566
3567 #if STORE_FLAG_VALUE == -1
3568           /* If STORE_FLAG_VALUE is -1, we can convert (ne x 0)
3569              to (neg x) if only the low-order bit of X can be nonzero.
3570              This converts (ne (zero_extract X 1 Y) 0) to
3571              (sign_extract X 1 Y).  */
3572           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3573               && op1 == const0_rtx
3574               && nonzero_bits (op0, GET_MODE (op0)) == 1)
3575             {
3576               op0 = expand_compound_operation (op0);
3577               x = gen_rtx_combine (NEG, mode,
3578                                    gen_lowpart_for_combine (mode, op0));
3579               goto restart;
3580             }
3581 #endif
3582
3583           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
3584              one bit that might be nonzero, we can convert (ne x 0) to
3585              (ashift x c) where C puts the bit in the sign bit.  Remove any
3586              AND with STORE_FLAG_VALUE when we are done, since we are only
3587              going to test the sign bit.  */
3588           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3589               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3590               && (STORE_FLAG_VALUE
3591                   == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
3592               && op1 == const0_rtx
3593               && mode == GET_MODE (op0)
3594               && (i = exact_log2 (nonzero_bits (op0, GET_MODE (op0)))) >= 0)
3595             {
3596               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3597                                         expand_compound_operation (op0),
3598                                         GET_MODE_BITSIZE (mode) - 1 - i);
3599               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
3600                 return XEXP (x, 0);
3601               else
3602                 return x;
3603             }
3604
3605           /* If the code changed, return a whole new comparison.  */
3606           if (new_code != code)
3607             return gen_rtx_combine (new_code, mode, op0, op1);
3608
3609           /* Otherwise, keep this operation, but maybe change its operands.  
3610              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
3611           SUBST (XEXP (x, 0), op0);
3612           SUBST (XEXP (x, 1), op1);
3613         }
3614       break;
3615           
3616     case IF_THEN_ELSE:
3617       /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register
3618          used in it is being compared against certain values.  Get the
3619          true and false comparisons and see if that says anything about the
3620          value of each arm.  */
3621
3622       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3623           && reversible_comparison_p (XEXP (x, 0))
3624           && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG)
3625         {
3626           HOST_WIDE_INT nzb;
3627           rtx from = XEXP (XEXP (x, 0), 0);
3628           enum rtx_code true_code = GET_CODE (XEXP (x, 0));
3629           enum rtx_code false_code = reverse_condition (true_code);
3630           rtx true_val = XEXP (XEXP (x, 0), 1);
3631           rtx false_val = true_val;
3632           rtx true_arm = XEXP (x, 1);
3633           rtx false_arm = XEXP (x, 2);
3634           int swapped = 0;
3635
3636           /* If FALSE_CODE is EQ, swap the codes and arms.  */
3637
3638           if (false_code == EQ)
3639             {
3640               swapped = 1, true_code = EQ, false_code = NE;
3641               true_arm = XEXP (x, 2), false_arm = XEXP (x, 1);
3642             }
3643
3644           /* If we are comparing against zero and the expression being tested
3645              has only a single bit that might be nonzero, that is its value
3646              when it is not equal to zero.  Similarly if it is known to be
3647              -1 or 0.  */
3648
3649           if (true_code == EQ && true_val == const0_rtx
3650               && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
3651             false_code = EQ, false_val = GEN_INT (nzb);
3652           else if (true_code == EQ && true_val == const0_rtx
3653                    && (num_sign_bit_copies (from, GET_MODE (from))
3654                        == GET_MODE_BITSIZE (GET_MODE (from))))
3655             false_code = EQ, false_val = constm1_rtx;
3656
3657           /* Now simplify an arm if we know the value of the register
3658              in the branch and it is used in the arm.  Be carefull due to
3659              the potential of locally-shared RTL.  */
3660
3661           if (reg_mentioned_p (from, true_arm))
3662             true_arm = subst (known_cond (copy_rtx (true_arm), true_code,
3663                                           from, true_val),
3664                               pc_rtx, pc_rtx, 0, 0);
3665           if (reg_mentioned_p (from, false_arm))
3666             false_arm = subst (known_cond (copy_rtx (false_arm), false_code,
3667                                            from, false_val),
3668                                pc_rtx, pc_rtx, 0, 0);
3669
3670           SUBST (XEXP (x, 1), swapped ? false_arm : true_arm);
3671           SUBST (XEXP (x, 2), swapped ? true_arm : false_arm);
3672         }
3673       
3674       /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
3675          reversed, do so to avoid needing two sets of patterns for
3676          subtract-and-branch insns.  Similarly if we have a constant in that
3677          position or if the third operand is the same as the first operand
3678          of the comparison.  */
3679
3680       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3681           && reversible_comparison_p (XEXP (x, 0))
3682           && (XEXP (x, 1) == pc_rtx || GET_CODE (XEXP (x, 1)) == CONST_INT
3683               || rtx_equal_p (XEXP (x, 2), XEXP (XEXP (x, 0), 0))))
3684         {
3685           SUBST (XEXP (x, 0),
3686                  gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3687                              GET_MODE (XEXP (x, 0)),
3688                              XEXP (XEXP (x, 0), 0), XEXP (XEXP (x, 0), 1)));
3689
3690           temp = XEXP (x, 1);
3691           SUBST (XEXP (x, 1), XEXP (x, 2));
3692           SUBST (XEXP (x, 2), temp);
3693         }
3694
3695       /* If the two arms are identical, we don't need the comparison.  */
3696
3697       if (rtx_equal_p (XEXP (x, 1), XEXP (x, 2))
3698           && ! side_effects_p (XEXP (x, 0)))
3699         return XEXP (x, 1);
3700
3701       /* Look for cases where we have (abs x) or (neg (abs X)).  */
3702
3703       if (GET_MODE_CLASS (mode) == MODE_INT
3704           && GET_CODE (XEXP (x, 2)) == NEG
3705           && rtx_equal_p (XEXP (x, 1), XEXP (XEXP (x, 2), 0))
3706           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3707           && rtx_equal_p (XEXP (x, 1), XEXP (XEXP (x, 0), 0))
3708           && ! side_effects_p (XEXP (x, 1)))
3709         switch (GET_CODE (XEXP (x, 0)))
3710           {
3711           case GT:
3712           case GE:
3713             x = gen_unary (ABS, mode, XEXP (x, 1));
3714             goto restart;
3715           case LT:
3716           case LE:
3717             x = gen_unary (NEG, mode, gen_unary (ABS, mode, XEXP (x, 1)));
3718             goto restart;
3719           }
3720
3721       /* Look for MIN or MAX.  */
3722
3723       if (! FLOAT_MODE_P (mode)
3724           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3725           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1))
3726           && rtx_equal_p (XEXP (XEXP (x, 0), 1), XEXP (x, 2))
3727           && ! side_effects_p (XEXP (x, 0)))
3728         switch (GET_CODE (XEXP (x, 0)))
3729           {
3730           case GE:
3731           case GT:
3732             x = gen_binary (SMAX, mode, XEXP (x, 1), XEXP (x, 2));
3733             goto restart;
3734           case LE:
3735           case LT:
3736             x = gen_binary (SMIN, mode, XEXP (x, 1), XEXP (x, 2));
3737             goto restart;
3738           case GEU:
3739           case GTU:
3740             x = gen_binary (UMAX, mode, XEXP (x, 1), XEXP (x, 2));
3741             goto restart;
3742           case LEU:
3743           case LTU:
3744             x = gen_binary (UMIN, mode, XEXP (x, 1), XEXP (x, 2));
3745             goto restart;
3746           }
3747
3748       /* If we have something like (if_then_else (ne A 0) (OP X C) X),
3749          A is known to be either 0 or 1, and OP is an identity when its
3750          second operand is zero, this can be done as (OP X (mult A C)).
3751          Similarly if A is known to be 0 or -1 and also similarly if we have
3752          a ZERO_EXTEND or SIGN_EXTEND as long as X is already extended (so
3753          we don't destroy it).  */
3754
3755       if (mode != VOIDmode
3756           && (GET_CODE (XEXP (x, 0)) == EQ || GET_CODE (XEXP (x, 0)) == NE)
3757           && XEXP (XEXP (x, 0), 1) == const0_rtx
3758           && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1
3759               || (num_sign_bit_copies (XEXP (XEXP (x, 0), 0), mode)
3760                   == GET_MODE_BITSIZE (mode))))
3761         {
3762           rtx nz = make_compound_operation (GET_CODE (XEXP (x, 0)) == NE
3763                                             ? XEXP (x, 1) : XEXP (x, 2));
3764           rtx z = GET_CODE (XEXP (x, 0)) == NE ? XEXP (x, 2) : XEXP (x, 1);
3765           rtx dir = (nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1
3766                      ? const1_rtx : constm1_rtx);
3767           rtx c = 0;
3768           enum machine_mode m = mode;
3769           enum rtx_code op, extend_op = 0;
3770
3771           if ((GET_CODE (nz) == PLUS || GET_CODE (nz) == MINUS
3772                || GET_CODE (nz) == IOR || GET_CODE (nz) == XOR
3773                || GET_CODE (nz) == ASHIFT
3774                || GET_CODE (nz) == LSHIFTRT || GET_CODE (nz) == ASHIFTRT)
3775               && rtx_equal_p (XEXP (nz, 0), z))
3776             c = XEXP (nz, 1), op = GET_CODE (nz);
3777           else if (GET_CODE (nz) == SIGN_EXTEND
3778                    && (GET_CODE (XEXP (nz, 0)) == PLUS
3779                        || GET_CODE (XEXP (nz, 0)) == MINUS
3780                        || GET_CODE (XEXP (nz, 0)) == IOR
3781                        || GET_CODE (XEXP (nz, 0)) == XOR
3782                        || GET_CODE (XEXP (nz, 0)) == ASHIFT
3783                        || GET_CODE (XEXP (nz, 0)) == LSHIFTRT
3784                        || GET_CODE (XEXP (nz, 0)) == ASHIFTRT)
3785                    && GET_CODE (XEXP (XEXP (nz, 0), 0)) == SUBREG
3786                    && subreg_lowpart_p (XEXP (XEXP (nz, 0), 0))
3787                    && rtx_equal_p (SUBREG_REG (XEXP (XEXP (nz, 0), 0)), z)
3788                    && (num_sign_bit_copies (z, GET_MODE (z))
3789                        >= (GET_MODE_BITSIZE (mode)
3790                            - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (nz, 0), 0))))))
3791             {
3792               c = XEXP (XEXP (nz, 0), 1);
3793               op = GET_CODE (XEXP (nz, 0));
3794               extend_op = SIGN_EXTEND;
3795               m = GET_MODE (XEXP (nz, 0));
3796             }
3797           else if (GET_CODE (nz) == ZERO_EXTEND
3798                    && (GET_CODE (XEXP (nz, 0)) == PLUS
3799                        || GET_CODE (XEXP (nz, 0)) == MINUS
3800                        || GET_CODE (XEXP (nz, 0)) == IOR
3801                        || GET_CODE (XEXP (nz, 0)) == XOR
3802                        || GET_CODE (XEXP (nz, 0)) == ASHIFT
3803                        || GET_CODE (XEXP (nz, 0)) == LSHIFTRT
3804                        || GET_CODE (XEXP (nz, 0)) == ASHIFTRT)
3805                    && GET_CODE (XEXP (XEXP (nz, 0), 0)) == SUBREG
3806                    && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3807                    && subreg_lowpart_p (XEXP (XEXP (nz, 0), 0))
3808                    && rtx_equal_p (SUBREG_REG (XEXP (XEXP (nz, 0), 0)), z)
3809                    && ((nonzero_bits (z, GET_MODE (z))
3810                         & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (nz, 0), 0))))
3811                        == 0))
3812             {
3813               c = XEXP (XEXP (nz, 0), 1);
3814               op = GET_CODE (XEXP (nz, 0));
3815               extend_op = ZERO_EXTEND;
3816               m = GET_MODE (XEXP (nz, 0));
3817             }
3818
3819           if (c && ! side_effects_p (c) && ! side_effects_p (z))
3820             {
3821               temp
3822                 = gen_binary (MULT, m,
3823                               gen_lowpart_for_combine (m,
3824                                                        XEXP (XEXP (x, 0), 0)),
3825                               gen_binary (MULT, m, c, dir));
3826
3827               temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
3828
3829               if (extend_op != 0)
3830                 temp = gen_unary (extend_op, mode, temp);
3831
3832               return temp;
3833             }
3834         }
3835
3836       /* If we have (if_then_else (ne A 0) C1 0) and either A is known to 
3837          be 0 or 1 and C1 is a single bit or A is known to be 0 or -1 and
3838          C1 is the negation of a single bit, we can convert this operation
3839          to a shift.  We can actually do this in more general cases, but it
3840          doesn't seem worth it.  */
3841
3842       if (GET_CODE (XEXP (x, 0)) == NE && XEXP (XEXP (x, 0), 1) == const0_rtx
3843           && XEXP (x, 2) == const0_rtx && GET_CODE (XEXP (x, 1)) == CONST_INT
3844           && ((1 == nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
3845                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
3846               || ((num_sign_bit_copies (XEXP (XEXP (x, 0), 0), mode)
3847                    == GET_MODE_BITSIZE (mode))
3848                   && (i = exact_log2 (- INTVAL (XEXP (x, 1)))) >= 0)))
3849         return
3850           simplify_shift_const (NULL_RTX, ASHIFT, mode,
3851                                 gen_lowpart_for_combine (mode,
3852                                                          XEXP (XEXP (x, 0), 0)),
3853                                 i);
3854       break;
3855           
3856     case ZERO_EXTRACT:
3857     case SIGN_EXTRACT:
3858     case ZERO_EXTEND:
3859     case SIGN_EXTEND:
3860       /* If we are processing SET_DEST, we are done. */
3861       if (in_dest)
3862         return x;
3863
3864       x = expand_compound_operation (x);
3865       if (GET_CODE (x) != code)
3866         goto restart;
3867       break;
3868
3869     case SET:
3870       /* (set (pc) (return)) gets written as (return).  */
3871       if (GET_CODE (SET_DEST (x)) == PC && GET_CODE (SET_SRC (x)) == RETURN)
3872         return SET_SRC (x);
3873
3874       /* Convert this into a field assignment operation, if possible.  */
3875       x = make_field_assignment (x);
3876
3877       /* If we are setting CC0 or if the source is a COMPARE, look for the
3878          use of the comparison result and try to simplify it unless we already
3879          have used undobuf.other_insn.  */
3880       if ((GET_CODE (SET_SRC (x)) == COMPARE
3881 #ifdef HAVE_cc0
3882            || SET_DEST (x) == cc0_rtx
3883 #endif
3884            )
3885           && (cc_use = find_single_use (SET_DEST (x), subst_insn,
3886                                         &other_insn)) != 0
3887           && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
3888           && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
3889           && XEXP (*cc_use, 0) == SET_DEST (x))
3890         {
3891           enum rtx_code old_code = GET_CODE (*cc_use);
3892           enum rtx_code new_code;
3893           rtx op0, op1;
3894           int other_changed = 0;
3895           enum machine_mode compare_mode = GET_MODE (SET_DEST (x));
3896
3897           if (GET_CODE (SET_SRC (x)) == COMPARE)
3898             op0 = XEXP (SET_SRC (x), 0), op1 = XEXP (SET_SRC (x), 1);
3899           else
3900             op0 = SET_SRC (x), op1 = const0_rtx;
3901
3902           /* Simplify our comparison, if possible.  */
3903           new_code = simplify_comparison (old_code, &op0, &op1);
3904
3905 #ifdef EXTRA_CC_MODES
3906           /* If this machine has CC modes other than CCmode, check to see
3907              if we need to use a different CC mode here.  */
3908           compare_mode = SELECT_CC_MODE (new_code, op0, op1);
3909 #endif /* EXTRA_CC_MODES */
3910
3911 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
3912           /* If the mode changed, we have to change SET_DEST, the mode
3913              in the compare, and the mode in the place SET_DEST is used.
3914              If SET_DEST is a hard register, just build new versions with
3915              the proper mode.  If it is a pseudo, we lose unless it is only
3916              time we set the pseudo, in which case we can safely change
3917              its mode.  */
3918           if (compare_mode != GET_MODE (SET_DEST (x)))
3919             {
3920               int regno = REGNO (SET_DEST (x));
3921               rtx new_dest = gen_rtx (REG, compare_mode, regno);
3922
3923               if (regno < FIRST_PSEUDO_REGISTER
3924                   || (reg_n_sets[regno] == 1
3925                       && ! REG_USERVAR_P (SET_DEST (x))))
3926                 {
3927                   if (regno >= FIRST_PSEUDO_REGISTER)
3928                     SUBST (regno_reg_rtx[regno], new_dest);
3929
3930                   SUBST (SET_DEST (x), new_dest);
3931                   SUBST (XEXP (*cc_use, 0), new_dest);
3932                   other_changed = 1;
3933                 }
3934             }
3935 #endif
3936
3937           /* If the code changed, we have to build a new comparison
3938              in undobuf.other_insn.  */
3939           if (new_code != old_code)
3940             {
3941               unsigned HOST_WIDE_INT mask;
3942
3943               SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
3944                                                SET_DEST (x), const0_rtx));
3945
3946               /* If the only change we made was to change an EQ into an
3947                  NE or vice versa, OP0 has only one bit that might be nonzero,
3948                  and OP1 is zero, check if changing the user of the condition
3949                  code will produce a valid insn.  If it won't, we can keep
3950                  the original code in that insn by surrounding our operation
3951                  with an XOR.  */
3952
3953               if (((old_code == NE && new_code == EQ)
3954                    || (old_code == EQ && new_code == NE))
3955                   && ! other_changed && op1 == const0_rtx
3956                   && (GET_MODE_BITSIZE (GET_MODE (op0))
3957                       <= HOST_BITS_PER_WIDE_INT)
3958                   && (exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0)))
3959                       >= 0))
3960                 {
3961                   rtx pat = PATTERN (other_insn), note = 0;
3962
3963                   if ((recog_for_combine (&pat, other_insn, &note) < 0
3964                        && ! check_asm_operands (pat)))
3965                     {
3966                       PUT_CODE (*cc_use, old_code);
3967                       other_insn = 0;
3968
3969                       op0 = gen_binary (XOR, GET_MODE (op0), op0,
3970                                         GEN_INT (mask));
3971                     }
3972                 }
3973
3974               other_changed = 1;
3975             }
3976
3977           if (other_changed)
3978             undobuf.other_insn = other_insn;
3979
3980 #ifdef HAVE_cc0
3981           /* If we are now comparing against zero, change our source if
3982              needed.  If we do not use cc0, we always have a COMPARE.  */
3983           if (op1 == const0_rtx && SET_DEST (x) == cc0_rtx)
3984             SUBST (SET_SRC (x), op0);
3985           else
3986 #endif
3987
3988           /* Otherwise, if we didn't previously have a COMPARE in the
3989              correct mode, we need one.  */
3990           if (GET_CODE (SET_SRC (x)) != COMPARE
3991               || GET_MODE (SET_SRC (x)) != compare_mode)
3992             SUBST (SET_SRC (x), gen_rtx_combine (COMPARE, compare_mode,
3993                                                  op0, op1));
3994           else
3995             {
3996               /* Otherwise, update the COMPARE if needed.  */
3997               SUBST (XEXP (SET_SRC (x), 0), op0);
3998               SUBST (XEXP (SET_SRC (x), 1), op1);
3999             }
4000         }
4001       else
4002         {
4003           /* Get SET_SRC in a form where we have placed back any
4004              compound expressions.  Then do the checks below.  */
4005           temp = make_compound_operation (SET_SRC (x), SET);
4006           SUBST (SET_SRC (x), temp);
4007         }
4008
4009       /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some
4010          operation, and X being a REG or (subreg (reg)), we may be able to
4011          convert this to (set (subreg:m2 x) (op)).
4012
4013          We can always do this if M1 is narrower than M2 because that
4014          means that we only care about the low bits of the result.
4015
4016          However, on most machines (those with neither BYTE_LOADS_ZERO_EXTEND
4017          nor BYTE_LOADS_SIGN_EXTEND defined), we cannot perform a
4018          narrower operation that requested since the high-order bits will
4019          be undefined.  On machine where BYTE_LOADS_*_EXTEND is defined,
4020          however, this transformation is safe as long as M1 and M2 have
4021          the same number of words.  */
4022  
4023       if (GET_CODE (SET_SRC (x)) == SUBREG
4024           && subreg_lowpart_p (SET_SRC (x))
4025           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) != 'o'
4026           && (((GET_MODE_SIZE (GET_MODE (SET_SRC (x))) + (UNITS_PER_WORD - 1))
4027                / UNITS_PER_WORD)
4028               == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_SRC (x))))
4029                    + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
4030 #ifndef BYTE_LOADS_EXTEND
4031           && (GET_MODE_SIZE (GET_MODE (SET_SRC (x)))
4032               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_SRC (x)))))
4033 #endif
4034           && (GET_CODE (SET_DEST (x)) == REG
4035               || (GET_CODE (SET_DEST (x)) == SUBREG
4036                   && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG)))
4037         {
4038           SUBST (SET_DEST (x),
4039                  gen_lowpart_for_combine (GET_MODE (SUBREG_REG (SET_SRC (x))),
4040                                           SET_DEST (x)));
4041           SUBST (SET_SRC (x), SUBREG_REG (SET_SRC (x)));
4042         }
4043
4044 #ifdef BYTE_LOADS_EXTEND
4045       /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with
4046          M wider than N, this would require a paradoxical subreg.
4047          Replace the subreg with a zero_extend to avoid the reload that
4048          would otherwise be required. */
4049
4050       if (GET_CODE (SET_SRC (x)) == SUBREG
4051           && subreg_lowpart_p (SET_SRC (x))
4052           && SUBREG_WORD (SET_SRC (x)) == 0
4053           && (GET_MODE_SIZE (GET_MODE (SET_SRC (x)))
4054               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_SRC (x)))))
4055           && GET_CODE (SUBREG_REG (SET_SRC (x))) == MEM)
4056         SUBST (SET_SRC (x), gen_rtx_combine (LOAD_EXTEND,
4057                                              GET_MODE (SET_SRC (x)),
4058                                              XEXP (SET_SRC (x), 0)));
4059 #endif
4060
4061 #ifndef HAVE_conditional_move
4062
4063       /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE,
4064          and we are comparing an item known to be 0 or -1 against 0, use a
4065          logical operation instead. Check for one of the arms being an IOR
4066          of the other arm with some value.  We compute three terms to be
4067          IOR'ed together.  In practice, at most two will be nonzero.  Then
4068          we do the IOR's.  */
4069
4070       if (GET_CODE (SET_DEST (x)) != PC
4071           && GET_CODE (SET_SRC (x)) == IF_THEN_ELSE
4072           && (GET_CODE (XEXP (SET_SRC (x), 0)) == EQ
4073               || GET_CODE (XEXP (SET_SRC (x), 0)) == NE)
4074           && XEXP (XEXP (SET_SRC (x), 0), 1) == const0_rtx
4075           && (num_sign_bit_copies (XEXP (XEXP (SET_SRC (x), 0), 0),
4076                                    GET_MODE (XEXP (XEXP (SET_SRC (x), 0), 0)))
4077               == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (SET_SRC (x), 0), 0))))
4078           && ! side_effects_p (SET_SRC (x)))
4079         {
4080           rtx true = (GET_CODE (XEXP (SET_SRC (x), 0)) == NE
4081                       ? XEXP (SET_SRC (x), 1) : XEXP (SET_SRC (x), 2));
4082           rtx false = (GET_CODE (XEXP (SET_SRC (x), 0)) == NE
4083                        ? XEXP (SET_SRC (x), 2) : XEXP (SET_SRC (x), 1));
4084           rtx term1 = const0_rtx, term2, term3;
4085
4086           if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4087             term1 = false, true = XEXP (true, 1), false = const0_rtx;
4088           else if (GET_CODE (true) == IOR
4089                    && rtx_equal_p (XEXP (true, 1), false))
4090             term1 = false, true = XEXP (true, 0), false = const0_rtx;
4091           else if (GET_CODE (false) == IOR
4092                    && rtx_equal_p (XEXP (false, 0), true))
4093             term1 = true, false = XEXP (false, 1), true = const0_rtx;
4094           else if (GET_CODE (false) == IOR
4095                    && rtx_equal_p (XEXP (false, 1), true))
4096             term1 = true, false = XEXP (false, 0), true = const0_rtx;
4097
4098           term2 = gen_binary (AND, GET_MODE (SET_SRC (x)),
4099                               XEXP (XEXP (SET_SRC (x), 0), 0), true);
4100           term3 = gen_binary (AND, GET_MODE (SET_SRC (x)),
4101                               gen_unary (NOT, GET_MODE (SET_SRC (x)),
4102                                          XEXP (XEXP (SET_SRC (x), 0), 0)),
4103                               false);
4104
4105           SUBST (SET_SRC (x),
4106                  gen_binary (IOR, GET_MODE (SET_SRC (x)),
4107                              gen_binary (IOR, GET_MODE (SET_SRC (x)),
4108                                          term1, term2),
4109                              term3));
4110         }
4111 #endif
4112       break;
4113
4114     case AND:
4115       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4116         {
4117           x = simplify_and_const_int (x, mode, XEXP (x, 0),
4118                                       INTVAL (XEXP (x, 1)));
4119
4120           /* If we have (ior (and (X C1) C2)) and the next restart would be
4121              the last, simplify this by making C1 as small as possible
4122              and then exit. */
4123           if (n_restarts >= 3 && GET_CODE (x) == IOR
4124               && GET_CODE (XEXP (x, 0)) == AND
4125               && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4126               && GET_CODE (XEXP (x, 1)) == CONST_INT)
4127             {
4128               temp = gen_binary (AND, mode, XEXP (XEXP (x, 0), 0),
4129                                  GEN_INT (INTVAL (XEXP (XEXP (x, 0), 1))
4130                                           & ~ INTVAL (XEXP (x, 1))));
4131               return gen_binary (IOR, mode, temp, XEXP (x, 1));
4132             }
4133
4134           if (GET_CODE (x) != AND)
4135             goto restart;
4136         }
4137
4138       /* Convert (A | B) & A to A.  */
4139       if (GET_CODE (XEXP (x, 0)) == IOR
4140           && (rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1))
4141               || rtx_equal_p (XEXP (XEXP (x, 0), 1), XEXP (x, 1)))
4142           && ! side_effects_p (XEXP (XEXP (x, 0), 0))
4143           && ! side_effects_p (XEXP (XEXP (x, 0), 1)))
4144         return XEXP (x, 1);
4145
4146       /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4147          insn (and may simplify more).  */
4148       else if (GET_CODE (XEXP (x, 0)) == XOR
4149           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1))
4150           && ! side_effects_p (XEXP (x, 1)))
4151         {
4152           x = gen_binary (AND, mode,
4153                           gen_unary (NOT, mode, XEXP (XEXP (x, 0), 1)),
4154                           XEXP (x, 1));
4155           goto restart;
4156         }
4157       else if (GET_CODE (XEXP (x, 0)) == XOR
4158                && rtx_equal_p (XEXP (XEXP (x, 0), 1), XEXP (x, 1))
4159                && ! side_effects_p (XEXP (x, 1)))
4160         {
4161           x = gen_binary (AND, mode,
4162                           gen_unary (NOT, mode, XEXP (XEXP (x, 0), 0)),
4163                           XEXP (x, 1));
4164           goto restart;
4165         }
4166
4167       /* Similarly for (~ (A ^ B)) & A.  */
4168       else if (GET_CODE (XEXP (x, 0)) == NOT
4169                && GET_CODE (XEXP (XEXP (x, 0), 0)) == XOR
4170                && rtx_equal_p (XEXP (XEXP (XEXP (x, 0), 0), 0), XEXP (x, 1))
4171                && ! side_effects_p (XEXP (x, 1)))
4172         {
4173           x = gen_binary (AND, mode, XEXP (XEXP (XEXP (x, 0), 0), 1),
4174                           XEXP (x, 1));
4175           goto restart;
4176         }
4177       else if (GET_CODE (XEXP (x, 0)) == NOT
4178                && GET_CODE (XEXP (XEXP (x, 0), 0)) == XOR
4179                && rtx_equal_p (XEXP (XEXP (XEXP (x, 0), 0), 1), XEXP (x, 1))
4180                && ! side_effects_p (XEXP (x, 1)))
4181         {
4182           x = gen_binary (AND, mode, XEXP (XEXP (XEXP (x, 0), 0), 0),
4183                           XEXP (x, 1));
4184           goto restart;
4185         }
4186
4187       /* If we have (and A B) with A not an object but that is known to
4188          be -1 or 0, this is equivalent to the expression
4189          (if_then_else (ne A (const_int 0)) B (const_int 0))
4190          We make this conversion because it may allow further
4191          simplifications and then allow use of conditional move insns.
4192          If the machine doesn't have condition moves, code in case SET
4193          will convert the IF_THEN_ELSE back to the logical operation.
4194          We build the IF_THEN_ELSE here in case further simplification
4195          is possible (e.g., we can convert it to ABS).  */
4196
4197       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
4198           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4199                 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o')
4200           && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4201               == GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4202         {
4203           rtx op0 = XEXP (x, 0);
4204           rtx op1 = const0_rtx;
4205           enum rtx_code comp_code
4206             = simplify_comparison (NE, &op0, &op1);
4207
4208           x =  gen_rtx_combine (IF_THEN_ELSE, mode,
4209                                 gen_binary (comp_code, VOIDmode, op0, op1),
4210                                 XEXP (x, 1), const0_rtx);
4211           goto restart;
4212         }
4213
4214       /* In the following group of tests (and those in case IOR below),
4215          we start with some combination of logical operations and apply
4216          the distributive law followed by the inverse distributive law.
4217          Most of the time, this results in no change.  However, if some of
4218          the operands are the same or inverses of each other, simplifications
4219          will result.
4220
4221          For example, (and (ior A B) (not B)) can occur as the result of
4222          expanding a bit field assignment.  When we apply the distributive
4223          law to this, we get (ior (and (A (not B))) (and (B (not B)))),
4224          which then simplifies to (and (A (not B))).  */
4225
4226       /* If we have (and (ior A B) C), apply the distributive law and then
4227          the inverse distributive law to see if things simplify.  */
4228
4229       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == XOR)
4230         {
4231           x = apply_distributive_law
4232             (gen_binary (GET_CODE (XEXP (x, 0)), mode,
4233                          gen_binary (AND, mode,
4234                                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4235                          gen_binary (AND, mode,
4236                                      XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
4237           if (GET_CODE (x) != AND)
4238             goto restart;
4239         }
4240
4241       if (GET_CODE (XEXP (x, 1)) == IOR || GET_CODE (XEXP (x, 1)) == XOR)
4242         {
4243           x = apply_distributive_law
4244             (gen_binary (GET_CODE (XEXP (x, 1)), mode,
4245                          gen_binary (AND, mode,
4246                                      XEXP (XEXP (x, 1), 0), XEXP (x, 0)),
4247                          gen_binary (AND, mode,
4248                                      XEXP (XEXP (x, 1), 1), XEXP (x, 0))));
4249           if (GET_CODE (x) != AND)
4250             goto restart;
4251         }
4252
4253       /* Similarly, taking advantage of the fact that
4254          (and (not A) (xor B C)) == (xor (ior A B) (ior A C))  */
4255
4256       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == XOR)
4257         {
4258           x = apply_distributive_law
4259             (gen_binary (XOR, mode,
4260                          gen_binary (IOR, mode, XEXP (XEXP (x, 0), 0),
4261                                      XEXP (XEXP (x, 1), 0)),
4262                          gen_binary (IOR, mode, XEXP (XEXP (x, 0), 0),
4263                                      XEXP (XEXP (x, 1), 1))));
4264           if (GET_CODE (x) != AND)
4265             goto restart;
4266         }
4267                                                             
4268       else if (GET_CODE (XEXP (x, 1)) == NOT && GET_CODE (XEXP (x, 0)) == XOR)
4269         {
4270           x = apply_distributive_law
4271             (gen_binary (XOR, mode,
4272                          gen_binary (IOR, mode, XEXP (XEXP (x, 1), 0),
4273                                      XEXP (XEXP (x, 0), 0)),
4274                          gen_binary (IOR, mode, XEXP (XEXP (x, 1), 0),
4275                                      XEXP (XEXP (x, 0), 1))));
4276           if (GET_CODE (x) != AND)
4277             goto restart;
4278         }
4279       break;
4280
4281     case IOR:
4282       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
4283       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4284           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4285           && (nonzero_bits (XEXP (x, 0), mode) & ~ INTVAL (XEXP (x, 1))) == 0)
4286         return XEXP (x, 1);
4287
4288       /* Convert (A & B) | A to A.  */
4289       if (GET_CODE (XEXP (x, 0)) == AND
4290           && (rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1))
4291               || rtx_equal_p (XEXP (XEXP (x, 0), 1), XEXP (x, 1)))
4292           && ! side_effects_p (XEXP (XEXP (x, 0), 0))
4293           && ! side_effects_p (XEXP (XEXP (x, 0), 1)))
4294         return XEXP (x, 1);
4295
4296       /* If we have (ior (and A B) C), apply the distributive law and then
4297          the inverse distributive law to see if things simplify.  */
4298
4299       if (GET_CODE (XEXP (x, 0)) == AND)
4300         {
4301           x = apply_distributive_law
4302             (gen_binary (AND, mode,
4303                          gen_binary (IOR, mode,
4304                                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4305                          gen_binary (IOR, mode,
4306                                      XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
4307
4308           if (GET_CODE (x) != IOR)
4309             goto restart;
4310         }
4311
4312       if (GET_CODE (XEXP (x, 1)) == AND)
4313         {
4314           x = apply_distributive_law
4315             (gen_binary (AND, mode,
4316                          gen_binary (IOR, mode,
4317                                      XEXP (XEXP (x, 1), 0), XEXP (x, 0)),
4318                          gen_binary (IOR, mode,
4319                                      XEXP (XEXP (x, 1), 1), XEXP (x, 0))));
4320
4321           if (GET_CODE (x) != IOR)
4322             goto restart;
4323         }
4324
4325       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4326          mode size to (rotate A CX).  */
4327
4328       if (((GET_CODE (XEXP (x, 0)) == ASHIFT
4329             && GET_CODE (XEXP (x, 1)) == LSHIFTRT)
4330            || (GET_CODE (XEXP (x, 1)) == ASHIFT
4331                && GET_CODE (XEXP (x, 0)) == LSHIFTRT))
4332           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (XEXP (x, 1), 0))
4333           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4334           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4335           && (INTVAL (XEXP (XEXP (x, 0), 1)) + INTVAL (XEXP (XEXP (x, 1), 1))
4336               == GET_MODE_BITSIZE (mode)))
4337         {
4338           rtx shift_count;
4339
4340           if (GET_CODE (XEXP (x, 0)) == ASHIFT)
4341             shift_count = XEXP (XEXP (x, 0), 1);
4342           else
4343             shift_count = XEXP (XEXP (x, 1), 1);
4344           x = gen_rtx (ROTATE, mode, XEXP (XEXP (x, 0), 0), shift_count);
4345           goto restart;
4346         }
4347       break;
4348
4349     case XOR:
4350       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
4351          Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
4352          (NOT y).  */
4353       {
4354         int num_negated = 0;
4355         rtx in1 = XEXP (x, 0), in2 = XEXP (x, 1);
4356
4357         if (GET_CODE (in1) == NOT)
4358           num_negated++, in1 = XEXP (in1, 0);
4359         if (GET_CODE (in2) == NOT)
4360           num_negated++, in2 = XEXP (in2, 0);
4361
4362         if (num_negated == 2)
4363           {
4364             SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4365             SUBST (XEXP (x, 1), XEXP (XEXP (x, 1), 0));
4366           }
4367         else if (num_negated == 1)
4368           {
4369             x =  gen_unary (NOT, mode,
4370                             gen_binary (XOR, mode, in1, in2));
4371             goto restart;
4372           }
4373       }
4374
4375       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
4376          correspond to a machine insn or result in further simplifications
4377          if B is a constant.  */
4378
4379       if (GET_CODE (XEXP (x, 0)) == AND
4380           && rtx_equal_p (XEXP (XEXP (x, 0), 1), XEXP (x, 1))
4381           && ! side_effects_p (XEXP (x, 1)))
4382         {
4383           x = gen_binary (AND, mode,
4384                           gen_unary (NOT, mode, XEXP (XEXP (x, 0), 0)),
4385                           XEXP (x, 1));
4386           goto restart;
4387         }
4388       else if (GET_CODE (XEXP (x, 0)) == AND
4389                && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1))
4390                && ! side_effects_p (XEXP (x, 1)))
4391         {
4392           x = gen_binary (AND, mode,
4393                           gen_unary (NOT, mode, XEXP (XEXP (x, 0), 1)),
4394                           XEXP (x, 1));
4395           goto restart;
4396         }
4397
4398
4399 #if STORE_FLAG_VALUE == 1
4400       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
4401          comparison.  */
4402       if (XEXP (x, 1) == const1_rtx
4403           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4404           && reversible_comparison_p (XEXP (x, 0)))
4405         return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
4406                                 mode, XEXP (XEXP (x, 0), 0),
4407                                 XEXP (XEXP (x, 0), 1));
4408
4409       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
4410          is (lt foo (const_int 0)), so we can perform the above
4411          simplification.  */
4412
4413       if (XEXP (x, 1) == const1_rtx
4414           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4415           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4416           && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
4417         return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
4418 #endif
4419
4420       /* (xor (comparison foo bar) (const_int sign-bit))
4421          when STORE_FLAG_VALUE is the sign bit.  */
4422       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4423           && (STORE_FLAG_VALUE
4424               == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4425           && XEXP (x, 1) == const_true_rtx
4426           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4427           && reversible_comparison_p (XEXP (x, 0)))
4428         return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
4429                                 mode, XEXP (XEXP (x, 0), 0),
4430                                 XEXP (XEXP (x, 0), 1));
4431       break;
4432
4433     case ABS:
4434       /* (abs (neg <foo>)) -> (abs <foo>) */
4435       if (GET_CODE (XEXP (x, 0)) == NEG)
4436         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4437
4438       /* If operand is something known to be positive, ignore the ABS.  */
4439       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4440           || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4441                <= HOST_BITS_PER_WIDE_INT)
4442               && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4443                    & ((HOST_WIDE_INT) 1
4444                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4445                   == 0)))
4446         return XEXP (x, 0);
4447
4448
4449       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
4450       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4451         {
4452           x = gen_rtx_combine (NEG, mode, XEXP (x, 0));
4453           goto restart;
4454         }
4455       break;
4456
4457     case FFS:
4458       /* (ffs (*_extend <X>)) = (ffs <X>) */
4459       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4460           || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4461         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4462       break;
4463
4464     case FLOAT:
4465       /* (float (sign_extend <X>)) = (float <X>).  */
4466       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4467         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4468       break;
4469
4470     case LSHIFT:
4471     case ASHIFT:
4472     case LSHIFTRT:
4473     case ASHIFTRT:
4474     case ROTATE:
4475     case ROTATERT:
4476       /* If this is a shift by a constant amount, simplify it.  */
4477       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4478         {
4479           x = simplify_shift_const (x, code, mode, XEXP (x, 0), 
4480                                     INTVAL (XEXP (x, 1)));
4481           if (GET_CODE (x) != code)
4482             goto restart;
4483         }
4484
4485 #ifdef SHIFT_COUNT_TRUNCATED
4486       else if (GET_CODE (XEXP (x, 1)) != REG)
4487         SUBST (XEXP (x, 1),
4488                force_to_mode (XEXP (x, 1), GET_MODE (x),
4489                               exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))),
4490                               NULL_RTX));
4491 #endif
4492
4493       break;
4494     }
4495
4496   return x;
4497 }
4498 \f
4499 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
4500    operations" because they can be replaced with two more basic operations.
4501    ZERO_EXTEND is also considered "compound" because it can be replaced with
4502    an AND operation, which is simpler, though only one operation.
4503
4504    The function expand_compound_operation is called with an rtx expression
4505    and will convert it to the appropriate shifts and AND operations, 
4506    simplifying at each stage.
4507
4508    The function make_compound_operation is called to convert an expression
4509    consisting of shifts and ANDs into the equivalent compound expression.
4510    It is the inverse of this function, loosely speaking.  */
4511
4512 static rtx
4513 expand_compound_operation (x)
4514      rtx x;
4515 {
4516   int pos = 0, len;
4517   int unsignedp = 0;
4518   int modewidth;
4519   rtx tem;
4520
4521   switch (GET_CODE (x))
4522     {
4523     case ZERO_EXTEND:
4524       unsignedp = 1;
4525     case SIGN_EXTEND:
4526       /* We can't necessarily use a const_int for a multiword mode;
4527          it depends on implicitly extending the value.
4528          Since we don't know the right way to extend it,
4529          we can't tell whether the implicit way is right.
4530
4531          Even for a mode that is no wider than a const_int,
4532          we can't win, because we need to sign extend one of its bits through
4533          the rest of it, and we don't know which bit.  */
4534       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4535         return x;
4536
4537       if (! FAKE_EXTEND_SAFE_P (GET_MODE (XEXP (x, 0)), XEXP (x, 0)))
4538         return x;
4539
4540       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
4541       /* If the inner object has VOIDmode (the only way this can happen
4542          is if it is a ASM_OPERANDS), we can't do anything since we don't
4543          know how much masking to do.  */
4544       if (len == 0)
4545         return x;
4546
4547       break;
4548
4549     case ZERO_EXTRACT:
4550       unsignedp = 1;
4551     case SIGN_EXTRACT:
4552       /* If the operand is a CLOBBER, just return it.  */
4553       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
4554         return XEXP (x, 0);
4555
4556       if (GET_CODE (XEXP (x, 1)) != CONST_INT
4557           || GET_CODE (XEXP (x, 2)) != CONST_INT
4558           || GET_MODE (XEXP (x, 0)) == VOIDmode)
4559         return x;
4560
4561       len = INTVAL (XEXP (x, 1));
4562       pos = INTVAL (XEXP (x, 2));
4563
4564       /* If this goes outside the object being extracted, replace the object
4565          with a (use (mem ...)) construct that only combine understands
4566          and is used only for this purpose.  */
4567       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4568         SUBST (XEXP (x, 0), gen_rtx (USE, GET_MODE (x), XEXP (x, 0)));
4569
4570 #if BITS_BIG_ENDIAN
4571       pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
4572 #endif
4573       break;
4574
4575     default:
4576       return x;
4577     }
4578
4579   /* If we reach here, we want to return a pair of shifts.  The inner
4580      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
4581      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
4582      logical depending on the value of UNSIGNEDP.
4583
4584      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
4585      converted into an AND of a shift.
4586
4587      We must check for the case where the left shift would have a negative
4588      count.  This can happen in a case like (x >> 31) & 255 on machines
4589      that can't shift by a constant.  On those machines, we would first
4590      combine the shift with the AND to produce a variable-position 
4591      extraction.  Then the constant of 31 would be substituted in to produce
4592      a such a position.  */
4593
4594   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
4595   if (modewidth >= pos - len)
4596     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
4597                                 GET_MODE (x),
4598                                 simplify_shift_const (NULL_RTX, ASHIFT,
4599                                                       GET_MODE (x),
4600                                                       XEXP (x, 0),
4601                                                       modewidth - pos - len),
4602                                 modewidth - len);
4603
4604   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
4605     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
4606                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
4607                                                         GET_MODE (x),
4608                                                         XEXP (x, 0), pos),
4609                                   ((HOST_WIDE_INT) 1 << len) - 1);
4610   else
4611     /* Any other cases we can't handle.  */
4612     return x;
4613     
4614
4615   /* If we couldn't do this for some reason, return the original
4616      expression.  */
4617   if (GET_CODE (tem) == CLOBBER)
4618     return x;
4619
4620   return tem;
4621 }
4622 \f
4623 /* X is a SET which contains an assignment of one object into
4624    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
4625    or certain SUBREGS). If possible, convert it into a series of
4626    logical operations.
4627
4628    We half-heartedly support variable positions, but do not at all
4629    support variable lengths.  */
4630
4631 static rtx
4632 expand_field_assignment (x)
4633      rtx x;
4634 {
4635   rtx inner;
4636   rtx pos;                      /* Always counts from low bit. */
4637   int len;
4638   rtx mask;
4639   enum machine_mode compute_mode;
4640
4641   /* Loop until we find something we can't simplify.  */
4642   while (1)
4643     {
4644       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
4645           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
4646         {
4647           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
4648           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
4649           pos = const0_rtx;
4650         }
4651       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4652                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
4653         {
4654           inner = XEXP (SET_DEST (x), 0);
4655           len = INTVAL (XEXP (SET_DEST (x), 1));
4656           pos = XEXP (SET_DEST (x), 2);
4657
4658           /* If the position is constant and spans the width of INNER,
4659              surround INNER  with a USE to indicate this.  */
4660           if (GET_CODE (pos) == CONST_INT
4661               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
4662             inner = gen_rtx (USE, GET_MODE (SET_DEST (x)), inner);
4663
4664 #if BITS_BIG_ENDIAN
4665           if (GET_CODE (pos) == CONST_INT)
4666             pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
4667                            - INTVAL (pos));
4668           else if (GET_CODE (pos) == MINUS
4669                    && GET_CODE (XEXP (pos, 1)) == CONST_INT
4670                    && (INTVAL (XEXP (pos, 1))
4671                        == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
4672             /* If position is ADJUST - X, new position is X.  */
4673             pos = XEXP (pos, 0);
4674           else
4675             pos = gen_binary (MINUS, GET_MODE (pos),
4676                               GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
4677                                        - len),
4678                               pos);
4679 #endif
4680         }
4681
4682       /* A SUBREG between two modes that occupy the same numbers of words
4683          can be done by moving the SUBREG to the source.  */
4684       else if (GET_CODE (SET_DEST (x)) == SUBREG
4685                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
4686                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
4687                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
4688                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
4689         {
4690           x = gen_rtx (SET, VOIDmode, SUBREG_REG (SET_DEST (x)),
4691                        gen_lowpart_for_combine (GET_MODE (SUBREG_REG (SET_DEST (x))),
4692                                                 SET_SRC (x)));
4693           continue;
4694         }
4695       else
4696         break;
4697
4698       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
4699         inner = SUBREG_REG (inner);
4700
4701       compute_mode = GET_MODE (inner);
4702
4703       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
4704       if (len < HOST_BITS_PER_WIDE_INT)
4705         mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
4706       else
4707         break;
4708
4709       /* Now compute the equivalent expression.  Make a copy of INNER
4710          for the SET_DEST in case it is a MEM into which we will substitute;
4711          we don't want shared RTL in that case.  */
4712       x = gen_rtx (SET, VOIDmode, copy_rtx (inner),
4713                    gen_binary (IOR, compute_mode,
4714                                gen_binary (AND, compute_mode,
4715                                            gen_unary (NOT, compute_mode,
4716                                                       gen_binary (ASHIFT,
4717                                                                   compute_mode,
4718                                                                   mask, pos)),
4719                                            inner),
4720                                gen_binary (ASHIFT, compute_mode,
4721                                            gen_binary (AND, compute_mode,
4722                                                        gen_lowpart_for_combine
4723                                                        (compute_mode,
4724                                                         SET_SRC (x)),
4725                                                        mask),
4726                                            pos)));
4727     }
4728
4729   return x;
4730 }
4731 \f
4732 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
4733    it is an RTX that represents a variable starting position; otherwise,
4734    POS is the (constant) starting bit position (counted from the LSB).
4735
4736    INNER may be a USE.  This will occur when we started with a bitfield
4737    that went outside the boundary of the object in memory, which is
4738    allowed on most machines.  To isolate this case, we produce a USE
4739    whose mode is wide enough and surround the MEM with it.  The only
4740    code that understands the USE is this routine.  If it is not removed,
4741    it will cause the resulting insn not to match.
4742
4743    UNSIGNEDP is non-zero for an unsigned reference and zero for a 
4744    signed reference.
4745
4746    IN_DEST is non-zero if this is a reference in the destination of a
4747    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If non-zero,
4748    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
4749    be used.
4750
4751    IN_COMPARE is non-zero if we are in a COMPARE.  This means that a
4752    ZERO_EXTRACT should be built even for bits starting at bit 0.
4753
4754    MODE is the desired mode of the result (if IN_DEST == 0).  */
4755
4756 static rtx
4757 make_extraction (mode, inner, pos, pos_rtx, len,
4758                  unsignedp, in_dest, in_compare)
4759      enum machine_mode mode;
4760      rtx inner;
4761      int pos;
4762      rtx pos_rtx;
4763      int len;
4764      int unsignedp;
4765      int in_dest, in_compare;
4766 {
4767   /* This mode describes the size of the storage area
4768      to fetch the overall value from.  Within that, we
4769      ignore the POS lowest bits, etc.  */
4770   enum machine_mode is_mode = GET_MODE (inner);
4771   enum machine_mode inner_mode;
4772   enum machine_mode wanted_mem_mode = byte_mode;
4773   enum machine_mode pos_mode = word_mode;
4774   enum machine_mode extraction_mode = word_mode;
4775   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
4776   int spans_byte = 0;
4777   rtx new = 0;
4778   rtx orig_pos_rtx = pos_rtx;
4779
4780   /* Get some information about INNER and get the innermost object.  */
4781   if (GET_CODE (inner) == USE)
4782     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
4783     /* We don't need to adjust the position because we set up the USE
4784        to pretend that it was a full-word object.  */
4785     spans_byte = 1, inner = XEXP (inner, 0);
4786   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
4787     {
4788       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
4789          consider just the QI as the memory to extract from.
4790          The subreg adds or removes high bits; its mode is
4791          irrelevant to the meaning of this extraction,
4792          since POS and LEN count from the lsb.  */
4793       if (GET_CODE (SUBREG_REG (inner)) == MEM)
4794         is_mode = GET_MODE (SUBREG_REG (inner));
4795       inner = SUBREG_REG (inner);
4796     }
4797
4798   inner_mode = GET_MODE (inner);
4799
4800   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
4801     pos = INTVAL (pos_rtx), pos_rtx = 0;
4802
4803   /* See if this can be done without an extraction.  We never can if the
4804      width of the field is not the same as that of some integer mode. For
4805      registers, we can only avoid the extraction if the position is at the
4806      low-order bit and this is either not in the destination or we have the
4807      appropriate STRICT_LOW_PART operation available.
4808
4809      For MEM, we can avoid an extract if the field starts on an appropriate
4810      boundary and we can change the mode of the memory reference.  However,
4811      we cannot directly access the MEM if we have a USE and the underlying
4812      MEM is not TMODE.  This combination means that MEM was being used in a
4813      context where bits outside its mode were being referenced; that is only
4814      valid in bit-field insns.  */
4815
4816   if (tmode != BLKmode
4817       && ! (spans_byte && inner_mode != tmode)
4818       && ((pos_rtx == 0 && pos == 0 && GET_CODE (inner) != MEM
4819            && (! in_dest
4820                || (GET_CODE (inner) == REG
4821                    && (movstrict_optab->handlers[(int) tmode].insn_code
4822                        != CODE_FOR_nothing))))
4823           || (GET_CODE (inner) == MEM && pos_rtx == 0
4824               && (pos
4825                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
4826                      : BITS_PER_UNIT)) == 0
4827               /* We can't do this if we are widening INNER_MODE (it
4828                  may not be aligned, for one thing).  */
4829               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
4830               && (inner_mode == tmode
4831                   || (! mode_dependent_address_p (XEXP (inner, 0))
4832                       && ! MEM_VOLATILE_P (inner))))))
4833     {
4834       /* If INNER is a MEM, make a new MEM that encompasses just the desired
4835          field.  If the original and current mode are the same, we need not
4836          adjust the offset.  Otherwise, we do if bytes big endian.  
4837
4838          If INNER is not a MEM, get a piece consisting of the just the field
4839          of interest (in this case POS must be 0).  */
4840
4841       if (GET_CODE (inner) == MEM)
4842         {
4843           int offset;
4844           /* POS counts from lsb, but make OFFSET count in memory order.  */
4845           if (BYTES_BIG_ENDIAN)
4846             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
4847           else
4848             offset = pos / BITS_PER_UNIT;
4849
4850           new = gen_rtx (MEM, tmode, plus_constant (XEXP (inner, 0), offset));
4851           RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
4852           MEM_VOLATILE_P (new) = MEM_VOLATILE_P (inner);
4853           MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (inner);
4854         }
4855       else if (GET_CODE (inner) == REG)
4856         /* We can't call gen_lowpart_for_combine here since we always want
4857            a SUBREG and it would sometimes return a new hard register.  */
4858         new = gen_rtx (SUBREG, tmode, inner,
4859                        (WORDS_BIG_ENDIAN
4860                         && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD
4861                         ? ((GET_MODE_SIZE (inner_mode) - GET_MODE_SIZE (tmode))
4862                            / UNITS_PER_WORD)
4863                         : 0));
4864       else
4865         new = force_to_mode (inner, tmode, len, NULL_RTX);
4866
4867       /* If this extraction is going into the destination of a SET, 
4868          make a STRICT_LOW_PART unless we made a MEM.  */
4869
4870       if (in_dest)
4871         return (GET_CODE (new) == MEM ? new
4872                 : (GET_CODE (new) != SUBREG
4873                    ? gen_rtx (CLOBBER, tmode, const0_rtx)
4874                    : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
4875
4876       /* Otherwise, sign- or zero-extend unless we already are in the
4877          proper mode.  */
4878
4879       return (mode == tmode ? new
4880               : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
4881                                  mode, new));
4882     }
4883
4884   /* Unless this is a COMPARE or we have a funny memory reference,
4885      don't do anything with zero-extending field extracts starting at
4886      the low-order bit since they are simple AND operations.  */
4887   if (pos_rtx == 0 && pos == 0 && ! in_dest
4888       && ! in_compare && ! spans_byte && unsignedp)
4889     return 0;
4890
4891   /* Get the mode to use should INNER be a MEM, the mode for the position,
4892      and the mode for the result.  */
4893 #ifdef HAVE_insv
4894   if (in_dest)
4895     {
4896       wanted_mem_mode = insn_operand_mode[(int) CODE_FOR_insv][0];
4897       pos_mode = insn_operand_mode[(int) CODE_FOR_insv][2];
4898       extraction_mode = insn_operand_mode[(int) CODE_FOR_insv][3];
4899     }
4900 #endif
4901
4902 #ifdef HAVE_extzv
4903   if (! in_dest && unsignedp)
4904     {
4905       wanted_mem_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
4906       pos_mode = insn_operand_mode[(int) CODE_FOR_extzv][3];
4907       extraction_mode = insn_operand_mode[(int) CODE_FOR_extzv][0];
4908     }
4909 #endif
4910
4911 #ifdef HAVE_extv
4912   if (! in_dest && ! unsignedp)
4913     {
4914       wanted_mem_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
4915       pos_mode = insn_operand_mode[(int) CODE_FOR_extv][3];
4916       extraction_mode = insn_operand_mode[(int) CODE_FOR_extv][0];
4917     }
4918 #endif
4919
4920   /* Never narrow an object, since that might not be safe.  */
4921
4922   if (mode != VOIDmode
4923       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
4924     extraction_mode = mode;
4925
4926   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
4927       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
4928     pos_mode = GET_MODE (pos_rtx);
4929
4930   /* If this is not from memory or we have to change the mode of memory and
4931      cannot, the desired mode is EXTRACTION_MODE.  */
4932   if (GET_CODE (inner) != MEM
4933       || (inner_mode != wanted_mem_mode
4934           && (mode_dependent_address_p (XEXP (inner, 0))
4935               || MEM_VOLATILE_P (inner))))
4936     wanted_mem_mode = extraction_mode;
4937
4938 #if BITS_BIG_ENDIAN
4939   /* If position is constant, compute new position.  Otherwise, build
4940      subtraction.  */
4941   if (pos_rtx == 0)
4942     pos = (MAX (GET_MODE_BITSIZE (is_mode), GET_MODE_BITSIZE (wanted_mem_mode))
4943            - len - pos);
4944   else
4945     pos_rtx
4946       = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
4947                          GEN_INT (MAX (GET_MODE_BITSIZE (is_mode),
4948                                        GET_MODE_BITSIZE (wanted_mem_mode))
4949                                   - len),
4950                          pos_rtx);
4951 #endif
4952
4953   /* If INNER has a wider mode, make it smaller.  If this is a constant
4954      extract, try to adjust the byte to point to the byte containing
4955      the value.  */
4956   if (wanted_mem_mode != VOIDmode
4957       && GET_MODE_SIZE (wanted_mem_mode) < GET_MODE_SIZE (is_mode)
4958       && ((GET_CODE (inner) == MEM
4959            && (inner_mode == wanted_mem_mode
4960                || (! mode_dependent_address_p (XEXP (inner, 0))
4961                    && ! MEM_VOLATILE_P (inner))))))
4962     {
4963       int offset = 0;
4964
4965       /* The computations below will be correct if the machine is big
4966          endian in both bits and bytes or little endian in bits and bytes.
4967          If it is mixed, we must adjust.  */
4968              
4969       /* If bytes are big endian and we had a paradoxical SUBREG, we must
4970          adjust OFFSET to compensate. */
4971 #if BYTES_BIG_ENDIAN
4972       if (! spans_byte
4973           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
4974         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
4975 #endif
4976
4977       /* If this is a constant position, we can move to the desired byte.  */
4978       if (pos_rtx == 0)
4979         {
4980           offset += pos / BITS_PER_UNIT;
4981           pos %= GET_MODE_BITSIZE (wanted_mem_mode);
4982         }
4983
4984 #if BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
4985       if (! spans_byte && is_mode != wanted_mem_mode)
4986         offset = (GET_MODE_SIZE (is_mode)
4987                   - GET_MODE_SIZE (wanted_mem_mode) - offset);
4988 #endif
4989
4990       if (offset != 0 || inner_mode != wanted_mem_mode)
4991         {
4992           rtx newmem = gen_rtx (MEM, wanted_mem_mode,
4993                                 plus_constant (XEXP (inner, 0), offset));
4994           RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
4995           MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (inner);
4996           MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (inner);
4997           inner = newmem;
4998         }
4999     }
5000
5001   /* If INNER is not memory, we can always get it into the proper mode. */
5002   else if (GET_CODE (inner) != MEM)
5003     inner = force_to_mode (inner, extraction_mode,
5004                            (pos < 0 ? GET_MODE_BITSIZE (extraction_mode)
5005                             : len + pos),
5006                            NULL_RTX);
5007
5008   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
5009      have to zero extend.  Otherwise, we can just use a SUBREG.  */
5010   if (pos_rtx != 0
5011       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5012     pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
5013   else if (pos_rtx != 0
5014            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5015     pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5016
5017   /* Make POS_RTX unless we already have it and it is correct.  If we don't
5018      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
5019      be a CONST_INT. */
5020   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5021     pos_rtx = orig_pos_rtx;
5022
5023   else if (pos_rtx == 0)
5024     pos_rtx = GEN_INT (pos);
5025
5026   /* Make the required operation.  See if we can use existing rtx.  */
5027   new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5028                          extraction_mode, inner, GEN_INT (len), pos_rtx);
5029   if (! in_dest)
5030     new = gen_lowpart_for_combine (mode, new);
5031
5032   return new;
5033 }
5034 \f
5035 /* Look at the expression rooted at X.  Look for expressions
5036    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
5037    Form these expressions.
5038
5039    Return the new rtx, usually just X.
5040
5041    Also, for machines like the Vax that don't have logical shift insns,
5042    try to convert logical to arithmetic shift operations in cases where
5043    they are equivalent.  This undoes the canonicalizations to logical
5044    shifts done elsewhere.
5045
5046    We try, as much as possible, to re-use rtl expressions to save memory.
5047
5048    IN_CODE says what kind of expression we are processing.  Normally, it is
5049    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
5050    being kludges), it is MEM.  When processing the arguments of a comparison
5051    or a COMPARE against zero, it is COMPARE.  */
5052
5053 static rtx
5054 make_compound_operation (x, in_code)
5055      rtx x;
5056      enum rtx_code in_code;
5057 {
5058   enum rtx_code code = GET_CODE (x);
5059   enum machine_mode mode = GET_MODE (x);
5060   int mode_width = GET_MODE_BITSIZE (mode);
5061   enum rtx_code next_code;
5062   int i, count;
5063   rtx new = 0;
5064   rtx tem;
5065   char *fmt;
5066
5067   /* Select the code to be used in recursive calls.  Once we are inside an
5068      address, we stay there.  If we have a comparison, set to COMPARE,
5069      but once inside, go back to our default of SET.  */
5070
5071   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
5072                : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
5073                   && XEXP (x, 1) == const0_rtx) ? COMPARE
5074                : in_code == COMPARE ? SET : in_code);
5075
5076   /* Process depending on the code of this operation.  If NEW is set
5077      non-zero, it will be returned.  */
5078
5079   switch (code)
5080     {
5081     case ASHIFT:
5082     case LSHIFT:
5083       /* Convert shifts by constants into multiplications if inside
5084          an address.  */
5085       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
5086           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
5087           && INTVAL (XEXP (x, 1)) >= 0)
5088         {
5089           new = make_compound_operation (XEXP (x, 0), next_code);
5090           new = gen_rtx_combine (MULT, mode, new,
5091                                  GEN_INT ((HOST_WIDE_INT) 1
5092                                           << INTVAL (XEXP (x, 1))));
5093         }
5094       break;
5095
5096     case AND:
5097       /* If the second operand is not a constant, we can't do anything
5098          with it.  */
5099       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5100         break;
5101
5102       /* If the constant is a power of two minus one and the first operand
5103          is a logical right shift, make an extraction.  */
5104       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
5105           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5106         {
5107           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5108           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
5109                                  0, in_code == COMPARE);
5110         }
5111
5112       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
5113       else if (GET_CODE (XEXP (x, 0)) == SUBREG
5114                && subreg_lowpart_p (XEXP (x, 0))
5115                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
5116                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5117         {
5118           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
5119                                          next_code);
5120           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
5121                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
5122                                  0, in_code == COMPARE);
5123         }
5124
5125       /* If we are have (and (rotate X C) M) and C is larger than the number
5126          of bits in M, this is an extraction.  */
5127
5128       else if (GET_CODE (XEXP (x, 0)) == ROTATE
5129                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5130                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
5131                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
5132         {
5133           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5134           new = make_extraction (mode, new,
5135                                  (GET_MODE_BITSIZE (mode)
5136                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
5137                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
5138         }
5139
5140       /* On machines without logical shifts, if the operand of the AND is
5141          a logical shift and our mask turns off all the propagated sign
5142          bits, we can replace the logical shift with an arithmetic shift.  */
5143       else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5144                && (lshr_optab->handlers[(int) mode].insn_code
5145                    == CODE_FOR_nothing)
5146                && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5147                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5148                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
5149                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
5150                && mode_width <= HOST_BITS_PER_WIDE_INT)
5151         {
5152           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
5153
5154           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
5155           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
5156             SUBST (XEXP (x, 0),
5157                    gen_rtx_combine (ASHIFTRT, mode,
5158                                     make_compound_operation (XEXP (XEXP (x, 0), 0),
5159                                                              next_code),
5160                                     XEXP (XEXP (x, 0), 1)));
5161         }
5162
5163       /* If the constant is one less than a power of two, this might be
5164          representable by an extraction even if no shift is present.
5165          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
5166          we are in a COMPARE.  */
5167       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5168         new = make_extraction (mode,
5169                                make_compound_operation (XEXP (x, 0),
5170                                                         next_code),
5171                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
5172
5173       /* If we are in a comparison and this is an AND with a power of two,
5174          convert this into the appropriate bit extract.  */
5175       else if (in_code == COMPARE
5176                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
5177         new = make_extraction (mode,
5178                                make_compound_operation (XEXP (x, 0),
5179                                                         next_code),
5180                                i, NULL_RTX, 1, 1, 0, 1);
5181
5182       break;
5183
5184     case LSHIFTRT:
5185       /* If the sign bit is known to be zero, replace this with an
5186          arithmetic shift.  */
5187       if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
5188           && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5189           && mode_width <= HOST_BITS_PER_WIDE_INT
5190           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
5191         {
5192           new = gen_rtx_combine (ASHIFTRT, mode,
5193                                  make_compound_operation (XEXP (x, 0),
5194                                                           next_code),
5195                                  XEXP (x, 1));
5196           break;
5197         }
5198
5199       /* ... fall through ... */
5200
5201     case ASHIFTRT:
5202       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
5203          this is a SIGN_EXTRACT.  */
5204       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5205           && GET_CODE (XEXP (x, 0)) == ASHIFT
5206           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5207           && INTVAL (XEXP (x, 1)) >= INTVAL (XEXP (XEXP (x, 0), 1)))
5208         {
5209           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5210           new = make_extraction (mode, new,
5211                                  (INTVAL (XEXP (x, 1))
5212                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
5213                                  NULL_RTX, mode_width - INTVAL (XEXP (x, 1)),
5214                                  code == LSHIFTRT, 0, in_code == COMPARE);
5215         }
5216
5217       /* Similarly if we have (ashifrt (OP (ashift foo C1) C3) C2).  In these
5218          cases, we are better off returning a SIGN_EXTEND of the operation.  */
5219
5220       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5221           && (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND
5222               || GET_CODE (XEXP (x, 0)) == XOR
5223               || GET_CODE (XEXP (x, 0)) == PLUS)
5224           && GET_CODE (XEXP (XEXP (x, 0), 0)) == ASHIFT
5225           && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
5226           && INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1)) < HOST_BITS_PER_WIDE_INT
5227           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5228           && 0 == (INTVAL (XEXP (XEXP (x, 0), 1))
5229                    & (((HOST_WIDE_INT) 1
5230                        << (MIN (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1)),
5231                                 INTVAL (XEXP (x, 1)))
5232                            - 1)))))
5233         {
5234           rtx c1 = XEXP (XEXP (XEXP (x, 0), 0), 1);
5235           rtx c2 = XEXP (x, 1);
5236           rtx c3 = XEXP (XEXP (x, 0), 1);
5237           HOST_WIDE_INT newop1;
5238           rtx inner = XEXP (XEXP (XEXP (x, 0), 0), 0);
5239
5240           /* If C1 > C2, INNER needs to have the shift performed on it
5241              for C1-C2 bits.  */
5242           if (INTVAL (c1) > INTVAL (c2))
5243             {
5244               inner = gen_binary (ASHIFT, mode, inner,
5245                                   GEN_INT (INTVAL (c1) - INTVAL (c2)));
5246               c1 = c2;
5247             }
5248
5249           newop1 = INTVAL (c3) >> INTVAL (c1);
5250           new = make_compound_operation (inner,
5251                                          GET_CODE (XEXP (x, 0)) == PLUS
5252                                          ? MEM : GET_CODE (XEXP (x, 0)));
5253           new = make_extraction (mode,
5254                                  gen_binary (GET_CODE (XEXP (x, 0)), mode, new,
5255                                              GEN_INT (newop1)),
5256                                  INTVAL (c2) - INTVAL (c1),
5257                                  NULL_RTX, mode_width - INTVAL (c2),
5258                                  code == LSHIFTRT, 0, in_code == COMPARE);
5259         }
5260
5261       /* Similarly for (ashiftrt (neg (ashift FOO C1)) C2).  */
5262       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5263           && GET_CODE (XEXP (x, 0)) == NEG
5264           && GET_CODE (XEXP (XEXP (x, 0), 0)) == ASHIFT
5265           && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
5266           && INTVAL (XEXP (x, 1)) >= INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1)))
5267         {
5268           new = make_compound_operation (XEXP (XEXP (XEXP (x, 0), 0), 0),
5269                                          next_code);
5270           new = make_extraction (mode,
5271                                  gen_unary (GET_CODE (XEXP (x, 0)), mode,
5272                                             new, 0),
5273                                  (INTVAL (XEXP (x, 1))
5274                                   - INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))),
5275                                  NULL_RTX, mode_width - INTVAL (XEXP (x, 1)),
5276                                  code == LSHIFTRT, 0, in_code == COMPARE);
5277         }
5278       break;
5279
5280     case SUBREG:
5281       /* Call ourselves recursively on the inner expression.  If we are
5282          narrowing the object and it has a different RTL code from
5283          what it originally did, do this SUBREG as a force_to_mode.  */
5284
5285       tem = make_compound_operation (SUBREG_REG (x), in_code);
5286       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
5287           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
5288           && subreg_lowpart_p (x))
5289         {
5290           rtx newer = force_to_mode (tem, mode,
5291                                      GET_MODE_BITSIZE (mode), NULL_RTX);
5292
5293           /* If we have something other than a SUBREG, we might have
5294              done an expansion, so rerun outselves.  */
5295           if (GET_CODE (newer) != SUBREG)
5296             newer = make_compound_operation (newer, in_code);
5297
5298           return newer;
5299         }
5300     }
5301
5302   if (new)
5303     {
5304       x = gen_lowpart_for_combine (mode, new);
5305       code = GET_CODE (x);
5306     }
5307
5308   /* Now recursively process each operand of this operation.  */
5309   fmt = GET_RTX_FORMAT (code);
5310   for (i = 0; i < GET_RTX_LENGTH (code); i++)
5311     if (fmt[i] == 'e')
5312       {
5313         new = make_compound_operation (XEXP (x, i), next_code);
5314         SUBST (XEXP (x, i), new);
5315       }
5316
5317   return x;
5318 }
5319 \f
5320 /* Given M see if it is a value that would select a field of bits
5321     within an item, but not the entire word.  Return -1 if not.
5322     Otherwise, return the starting position of the field, where 0 is the
5323     low-order bit.
5324
5325    *PLEN is set to the length of the field.  */
5326
5327 static int
5328 get_pos_from_mask (m, plen)
5329      unsigned HOST_WIDE_INT m;
5330      int *plen;
5331 {
5332   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
5333   int pos = exact_log2 (m & - m);
5334
5335   if (pos < 0)
5336     return -1;
5337
5338   /* Now shift off the low-order zero bits and see if we have a power of
5339      two minus 1.  */
5340   *plen = exact_log2 ((m >> pos) + 1);
5341
5342   if (*plen <= 0)
5343     return -1;
5344
5345   return pos;
5346 }
5347 \f
5348 /* Rewrite X so that it is an expression in MODE.  We only care about the
5349    low-order BITS bits so we can ignore AND operations that just clear
5350    higher-order bits.
5351
5352    Also, if REG is non-zero and X is a register equal in value to REG, 
5353    replace X with REG.  */
5354
5355 static rtx
5356 force_to_mode (x, mode, bits, reg)
5357      rtx x;
5358      enum machine_mode mode;
5359      int bits;
5360      rtx reg;
5361 {
5362   enum rtx_code code = GET_CODE (x);
5363   enum machine_mode op_mode = mode;
5364
5365   /* If X is narrower than MODE or if BITS is larger than the size of MODE,
5366      just get X in the proper mode.  */
5367
5368   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
5369       || bits > GET_MODE_BITSIZE (mode))
5370     return gen_lowpart_for_combine (mode, x);
5371
5372   switch (code)
5373     {
5374     case SIGN_EXTEND:
5375     case ZERO_EXTEND:
5376     case ZERO_EXTRACT:
5377     case SIGN_EXTRACT:
5378       x = expand_compound_operation (x);
5379       if (GET_CODE (x) != code)
5380         return force_to_mode (x, mode, bits, reg);
5381       break;
5382
5383     case REG:
5384       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
5385                        || rtx_equal_p (reg, get_last_value (x))))
5386         x = reg;
5387       break;
5388
5389     case CONST_INT:
5390       if (bits < HOST_BITS_PER_WIDE_INT)
5391         x = GEN_INT (INTVAL (x) & (((HOST_WIDE_INT) 1 << bits) - 1));
5392       return x;
5393
5394     case SUBREG:
5395       /* Ignore low-order SUBREGs. */
5396       if (subreg_lowpart_p (x))
5397         return force_to_mode (SUBREG_REG (x), mode, bits, reg);
5398       break;
5399
5400     case AND:
5401       /* If this is an AND with a constant.  Otherwise, we fall through to
5402          do the general binary case.  */
5403
5404       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5405         {
5406           HOST_WIDE_INT mask = INTVAL (XEXP (x, 1));
5407           int len = exact_log2 (mask + 1);
5408           rtx op = XEXP (x, 0);
5409
5410           /* If this is masking some low-order bits, we may be able to
5411              impose a stricter constraint on what bits of the operand are
5412              required.  */
5413
5414           op = force_to_mode (op, mode, len > 0 ? MIN (len, bits) : bits,
5415                               reg);
5416
5417           if (bits < HOST_BITS_PER_WIDE_INT)
5418             mask &= ((HOST_WIDE_INT) 1 << bits) - 1;
5419
5420           /* If we have no AND in MODE, use the original mode for the
5421              operation.  */
5422
5423           if (and_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
5424             op_mode = GET_MODE (x);
5425
5426           x = simplify_and_const_int (x, op_mode, op, mask);
5427
5428           /* If X is still an AND, see if it is an AND with a mask that
5429              is just some low-order bits.  If so, and it is BITS wide (it
5430              can't be wider), we don't need it.  */
5431
5432           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
5433               && bits < HOST_BITS_PER_WIDE_INT
5434               && INTVAL (XEXP (x, 1)) == ((HOST_WIDE_INT) 1 << bits) - 1)
5435             x = XEXP (x, 0);
5436
5437           break;
5438         }
5439
5440       /* ... fall through ... */
5441
5442     case PLUS:
5443     case MINUS:
5444     case MULT:
5445     case IOR:
5446     case XOR:
5447       /* For most binary operations, just propagate into the operation and
5448          change the mode if we have an operation of that mode.  */
5449
5450       if ((code == PLUS
5451            && add_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
5452           || (code == MINUS
5453               && sub_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
5454           || (code == MULT && (smul_optab->handlers[(int) mode].insn_code
5455                                == CODE_FOR_nothing))
5456           || (code == AND
5457               && and_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
5458           || (code == IOR
5459               && ior_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
5460           || (code == XOR && (xor_optab->handlers[(int) mode].insn_code
5461                               == CODE_FOR_nothing)))
5462         op_mode = GET_MODE (x);
5463
5464       x = gen_binary (code, op_mode,
5465                       gen_lowpart_for_combine (op_mode,
5466                                                force_to_mode (XEXP (x, 0),
5467                                                               mode, bits,
5468                                                               reg)),
5469                       gen_lowpart_for_combine (op_mode,
5470                                                force_to_mode (XEXP (x, 1),
5471                                                               mode, bits,
5472                                                               reg)));
5473       break;
5474
5475     case ASHIFT:
5476     case LSHIFT:
5477       /* For left shifts, do the same, but just for the first operand.
5478          However, we cannot do anything with shifts where we cannot
5479          guarantee that the counts are smaller than the size of the mode
5480          because such a count will have a different meaning in a
5481          wider mode.
5482
5483          If we can narrow the shift and know the count, we need even fewer
5484          bits of the first operand.  */
5485
5486       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
5487              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
5488           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
5489                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
5490                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
5491         break;
5492         
5493       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) < bits)
5494         bits -= INTVAL (XEXP (x, 1));
5495
5496       if ((code == ASHIFT
5497            && ashl_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
5498           || (code == LSHIFT && (lshl_optab->handlers[(int) mode].insn_code
5499                                  == CODE_FOR_nothing)))
5500         op_mode = GET_MODE (x);
5501
5502       x =  gen_binary (code, op_mode,
5503                        gen_lowpart_for_combine (op_mode,
5504                                                 force_to_mode (XEXP (x, 0),
5505                                                                mode, bits,
5506                                                                reg)),
5507                        XEXP (x, 1));
5508       break;
5509
5510     case LSHIFTRT:
5511       /* Here we can only do something if the shift count is a constant and
5512          the count plus BITS is no larger than the width of MODE.  In that
5513          case, we can do the shift in MODE.  */
5514
5515       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5516           && INTVAL (XEXP (x, 1)) + bits <= GET_MODE_BITSIZE (mode))
5517         {
5518           rtx inner = force_to_mode (XEXP (x, 0), mode,
5519                                      bits + INTVAL (XEXP (x, 1)), reg);
5520
5521           if (lshr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
5522             op_mode = GET_MODE (x);
5523
5524           x = gen_binary (LSHIFTRT, op_mode,
5525                           gen_lowpart_for_combine (op_mode, inner),
5526                           XEXP (x, 1));
5527         }
5528       break;
5529
5530     case ASHIFTRT:
5531       /* If this is a sign-extension operation that just affects bits
5532          we don't care about, remove it.  */
5533
5534       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5535           && INTVAL (XEXP (x, 1)) >= 0
5536           && INTVAL (XEXP (x, 1)) <= GET_MODE_BITSIZE (GET_MODE (x)) - bits
5537           && GET_CODE (XEXP (x, 0)) == ASHIFT
5538           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5539           && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
5540         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, bits, reg);
5541       break;
5542
5543     case NEG:
5544     case NOT:
5545       if ((code == NEG
5546            && neg_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
5547           || (code == NOT && (one_cmpl_optab->handlers[(int) mode].insn_code
5548                               == CODE_FOR_nothing)))
5549         op_mode = GET_MODE (x);
5550
5551       /* Handle these similarly to the way we handle most binary operations. */
5552       x = gen_unary (code, op_mode,
5553                      gen_lowpart_for_combine (op_mode,
5554                                               force_to_mode (XEXP (x, 0), mode,
5555                                                              bits, reg)));
5556       break;
5557
5558     case IF_THEN_ELSE:
5559       /* We have no way of knowing if the IF_THEN_ELSE can itself be
5560          written in a narrower mode.  We play it safe and do not do so.  */
5561
5562       SUBST (XEXP (x, 1),
5563              gen_lowpart_for_combine (GET_MODE (x),
5564                                       force_to_mode (XEXP (x, 1), mode,
5565                                                      bits, reg)));
5566       SUBST (XEXP (x, 2),
5567              gen_lowpart_for_combine (GET_MODE (x),
5568                                       force_to_mode (XEXP (x, 2), mode,
5569                                                      bits, reg)));
5570       break;
5571     }
5572
5573   /* Ensure we return a value of the proper mode.  */
5574   return gen_lowpart_for_combine (mode, x);
5575 }
5576 \f
5577 /* Return the value of expression X given the fact that condition COND
5578    is known to be true when applied to REG as its first operand and VAL
5579    as its second.  X is known to not be shared and so can be modified in
5580    place.
5581
5582    We only handle the simplest cases, and specifically those cases that
5583    arise with IF_THEN_ELSE expressions.  */
5584
5585 static rtx
5586 known_cond (x, cond, reg, val)
5587      rtx x;
5588      enum rtx_code cond;
5589      rtx reg, val;
5590 {
5591   enum rtx_code code = GET_CODE (x);
5592   rtx new, temp;
5593   char *fmt;
5594   int i, j;
5595
5596   if (side_effects_p (x))
5597     return x;
5598
5599   if (cond == EQ && rtx_equal_p (x, reg))
5600     return val;
5601
5602   /* If X is (abs REG) and we know something about REG's relationship
5603      with zero, we may be able to simplify this.  */
5604
5605   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
5606     switch (cond)
5607       {
5608       case GE:  case GT:  case EQ:
5609         return XEXP (x, 0);
5610       case LT:  case LE:
5611         return gen_unary (NEG, GET_MODE (XEXP (x, 0)), XEXP (x, 0));
5612       }
5613
5614   /* The only other cases we handle are MIN, MAX, and comparisons if the
5615      operands are the same as REG and VAL.  */
5616
5617   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
5618     {
5619       if (rtx_equal_p (XEXP (x, 0), val))
5620         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
5621
5622       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
5623         {
5624           if (GET_RTX_CLASS (code) == '<')
5625             return (comparison_dominates_p (cond, code) ? const_true_rtx
5626                     : (comparison_dominates_p (cond,
5627                                                reverse_condition (code))
5628                        ? const0_rtx : x));
5629
5630           else if (code == SMAX || code == SMIN
5631                    || code == UMIN || code == UMAX)
5632             {
5633               int unsignedp = (code == UMIN || code == UMAX);
5634
5635               if (code == SMAX || code == UMAX)
5636                 cond = reverse_condition (cond);
5637
5638               switch (cond)
5639                 {
5640                 case GE:   case GT:
5641                   return unsignedp ? x : XEXP (x, 1);
5642                 case LE:   case LT:
5643                   return unsignedp ? x : XEXP (x, 0);
5644                 case GEU:  case GTU:
5645                   return unsignedp ? XEXP (x, 1) : x;
5646                 case LEU:  case LTU:
5647                   return unsignedp ? XEXP (x, 0) : x;
5648                 }
5649             }
5650         }
5651     }
5652
5653   fmt = GET_RTX_FORMAT (code);
5654   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5655     {
5656       if (fmt[i] == 'e')
5657         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
5658       else if (fmt[i] == 'E')
5659         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5660           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
5661                                                 cond, reg, val));
5662     }
5663
5664   return x;
5665 }
5666 \f
5667 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
5668    Return that assignment if so.
5669
5670    We only handle the most common cases.  */
5671
5672 static rtx
5673 make_field_assignment (x)
5674      rtx x;
5675 {
5676   rtx dest = SET_DEST (x);
5677   rtx src = SET_SRC (x);
5678   rtx ourdest;
5679   rtx assign;
5680   HOST_WIDE_INT c1;
5681   int pos, len;
5682   rtx other;
5683   enum machine_mode mode;
5684
5685   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
5686      a clear of a one-bit field.  We will have changed it to
5687      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
5688      for a SUBREG.  */
5689
5690   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
5691       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
5692       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
5693       && (rtx_equal_p (dest, XEXP (src, 1))
5694           || rtx_equal_p (dest, get_last_value (XEXP (src, 1)))
5695           || rtx_equal_p (get_last_value (dest), XEXP (src, 1))))
5696     {
5697       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
5698                                 1, 1, 1, 0);
5699       return gen_rtx (SET, VOIDmode, assign, const0_rtx);
5700     }
5701
5702   else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
5703            && subreg_lowpart_p (XEXP (src, 0))
5704            && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0))) 
5705                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
5706            && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
5707            && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
5708            && (rtx_equal_p (dest, XEXP (src, 1))
5709                || rtx_equal_p (dest, get_last_value (XEXP (src, 1)))
5710                || rtx_equal_p (get_last_value (dest), XEXP (src, 1))))
5711     {
5712       assign = make_extraction (VOIDmode, dest, 0,
5713                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
5714                                 1, 1, 1, 0);
5715       return gen_rtx (SET, VOIDmode, assign, const0_rtx);
5716     }
5717
5718   /* If SRC is (ior (ashift (const_int 1) POS DEST)), this is a set of a
5719      one-bit field.  */
5720   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
5721            && XEXP (XEXP (src, 0), 0) == const1_rtx
5722            && (rtx_equal_p (dest, XEXP (src, 1))
5723                || rtx_equal_p (dest, get_last_value (XEXP (src, 1)))
5724                || rtx_equal_p (get_last_value (dest), XEXP (src, 1))))
5725     {
5726       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
5727                                 1, 1, 1, 0);
5728       return gen_rtx (SET, VOIDmode, assign, const1_rtx);
5729     }
5730
5731   /* The other case we handle is assignments into a constant-position
5732      field.  They look like (ior (and DEST C1) OTHER).  If C1 represents
5733      a mask that has all one bits except for a group of zero bits and
5734      OTHER is known to have zeros where C1 has ones, this is such an
5735      assignment.  Compute the position and length from C1.  Shift OTHER
5736      to the appropriate position, force it to the required mode, and
5737      make the extraction.  Check for the AND in both operands.  */
5738
5739   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == AND
5740       && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT
5741       && (rtx_equal_p (XEXP (XEXP (src, 0), 0), dest)
5742           || rtx_equal_p (XEXP (XEXP (src, 0), 0), get_last_value (dest))
5743           || rtx_equal_p (get_last_value (XEXP (XEXP (src, 0), 1)), dest)))
5744     c1 = INTVAL (XEXP (XEXP (src, 0), 1)), other = XEXP (src, 1);
5745   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 1)) == AND
5746            && GET_CODE (XEXP (XEXP (src, 1), 1)) == CONST_INT
5747            && (rtx_equal_p (XEXP (XEXP (src, 1), 0), dest)
5748                || rtx_equal_p (XEXP (XEXP (src, 1), 0), get_last_value (dest))
5749                || rtx_equal_p (get_last_value (XEXP (XEXP (src, 1), 0)),
5750                                dest)))
5751     c1 = INTVAL (XEXP (XEXP (src, 1), 1)), other = XEXP (src, 0);
5752   else
5753     return x;
5754
5755   pos = get_pos_from_mask (~c1, &len);
5756   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
5757       || (GET_MODE_BITSIZE (GET_MODE (other)) <= HOST_BITS_PER_WIDE_INT
5758           && (c1 & nonzero_bits (other, GET_MODE (other))) != 0))
5759     return x;
5760
5761   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
5762
5763   /* The mode to use for the source is the mode of the assignment, or of
5764      what is inside a possible STRICT_LOW_PART.  */
5765   mode = (GET_CODE (assign) == STRICT_LOW_PART 
5766           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
5767
5768   /* Shift OTHER right POS places and make it the source, restricting it
5769      to the proper length and mode.  */
5770
5771   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
5772                                              GET_MODE (src), other, pos),
5773                        mode, len, dest);
5774
5775   return gen_rtx_combine (SET, VOIDmode, assign, src);
5776 }
5777 \f
5778 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
5779    if so.  */
5780
5781 static rtx
5782 apply_distributive_law (x)
5783      rtx x;
5784 {
5785   enum rtx_code code = GET_CODE (x);
5786   rtx lhs, rhs, other;
5787   rtx tem;
5788   enum rtx_code inner_code;
5789
5790   /* Distributivity is not true for floating point.
5791      It can change the value.  So don't do it.
5792      -- rms and moshier@world.std.com.  */
5793   if (FLOAT_MODE_P (GET_MODE (x)))
5794     return x;
5795
5796   /* The outer operation can only be one of the following:  */
5797   if (code != IOR && code != AND && code != XOR
5798       && code != PLUS && code != MINUS)
5799     return x;
5800
5801   lhs = XEXP (x, 0), rhs = XEXP (x, 1);
5802
5803   /* If either operand is a primitive we can't do anything, so get out fast. */
5804   if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
5805       || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
5806     return x;
5807
5808   lhs = expand_compound_operation (lhs);
5809   rhs = expand_compound_operation (rhs);
5810   inner_code = GET_CODE (lhs);
5811   if (inner_code != GET_CODE (rhs))
5812     return x;
5813
5814   /* See if the inner and outer operations distribute.  */
5815   switch (inner_code)
5816     {
5817     case LSHIFTRT:
5818     case ASHIFTRT:
5819     case AND:
5820     case IOR:
5821       /* These all distribute except over PLUS.  */
5822       if (code == PLUS || code == MINUS)
5823         return x;
5824       break;
5825
5826     case MULT:
5827       if (code != PLUS && code != MINUS)
5828         return x;
5829       break;
5830
5831     case ASHIFT:
5832     case LSHIFT:
5833       /* These are also multiplies, so they distribute over everything.  */
5834       break;
5835
5836     case SUBREG:
5837       /* Non-paradoxical SUBREGs distributes over all operations, provided
5838          the inner modes and word numbers are the same, this is an extraction
5839          of a low-order part, we don't convert an fp operation to int or
5840          vice versa, and we would not be converting a single-word
5841          operation into a multi-word operation.  The latter test is not
5842          required, but it prevents generating unneeded multi-word operations.
5843          Some of the previous tests are redundant given the latter test, but
5844          are retained because they are required for correctness.
5845
5846          We produce the result slightly differently in this case.  */
5847
5848       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
5849           || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
5850           || ! subreg_lowpart_p (lhs)
5851           || (GET_MODE_CLASS (GET_MODE (lhs))
5852               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
5853           || (GET_MODE_SIZE (GET_MODE (lhs))
5854               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
5855           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
5856         return x;
5857
5858       tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
5859                         SUBREG_REG (lhs), SUBREG_REG (rhs));
5860       return gen_lowpart_for_combine (GET_MODE (x), tem);
5861
5862     default:
5863       return x;
5864     }
5865
5866   /* Set LHS and RHS to the inner operands (A and B in the example
5867      above) and set OTHER to the common operand (C in the example).
5868      These is only one way to do this unless the inner operation is
5869      commutative.  */
5870   if (GET_RTX_CLASS (inner_code) == 'c'
5871       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
5872     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
5873   else if (GET_RTX_CLASS (inner_code) == 'c'
5874            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
5875     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
5876   else if (GET_RTX_CLASS (inner_code) == 'c'
5877            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
5878     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
5879   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
5880     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
5881   else
5882     return x;
5883
5884   /* Form the new inner operation, seeing if it simplifies first.  */
5885   tem = gen_binary (code, GET_MODE (x), lhs, rhs);
5886
5887   /* There is one exception to the general way of distributing:
5888      (a ^ b) | (a ^ c) -> (~a) & (b ^ c)  */
5889   if (code == XOR && inner_code == IOR)
5890     {
5891       inner_code = AND;
5892       other = gen_unary (NOT, GET_MODE (x), other);
5893     }
5894
5895   /* We may be able to continuing distributing the result, so call
5896      ourselves recursively on the inner operation before forming the
5897      outer operation, which we return.  */
5898   return gen_binary (inner_code, GET_MODE (x),
5899                      apply_distributive_law (tem), other);
5900 }
5901 \f
5902 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
5903    in MODE.
5904
5905    Return an equivalent form, if different from X.  Otherwise, return X.  If
5906    X is zero, we are to always construct the equivalent form.  */
5907
5908 static rtx
5909 simplify_and_const_int (x, mode, varop, constop)
5910      rtx x;
5911      enum machine_mode mode;
5912      rtx varop;
5913      unsigned HOST_WIDE_INT constop;
5914 {
5915   register enum machine_mode tmode;
5916   register rtx temp;
5917   unsigned HOST_WIDE_INT nonzero;
5918
5919   /* There is a large class of optimizations based on the principle that
5920      some operations produce results where certain bits are known to be zero,
5921      and hence are not significant to the AND.  For example, if we have just
5922      done a left shift of one bit, the low-order bit is known to be zero and
5923      hence an AND with a mask of ~1 would not do anything.
5924
5925      At the end of the following loop, we set:
5926
5927      VAROP to be the item to be AND'ed with;
5928      CONSTOP to the constant value to AND it with.  */
5929
5930   while (1)
5931     {
5932       /* If we ever encounter a mode wider than the host machine's widest
5933          integer size, we can't compute the masks accurately, so give up.  */
5934       if (GET_MODE_BITSIZE (GET_MODE (varop)) > HOST_BITS_PER_WIDE_INT)
5935         break;
5936
5937       /* Unless one of the cases below does a `continue',
5938          a `break' will be executed to exit the loop.  */
5939
5940       switch (GET_CODE (varop))
5941         {
5942         case CLOBBER:
5943           /* If VAROP is a (clobber (const_int)), return it since we know
5944              we are generating something that won't match. */
5945           return varop;
5946
5947 #if ! BITS_BIG_ENDIAN
5948         case USE:
5949           /* VAROP is a (use (mem ..)) that was made from a bit-field
5950              extraction that spanned the boundary of the MEM.  If we are
5951              now masking so it is within that boundary, we don't need the
5952              USE any more.  */
5953           if ((constop & ~ GET_MODE_MASK (GET_MODE (XEXP (varop, 0)))) == 0)
5954             {
5955               varop = XEXP (varop, 0);
5956               continue;
5957             }
5958           break;
5959 #endif
5960
5961         case SUBREG:
5962           if (subreg_lowpart_p (varop)
5963               /* We can ignore the effect this SUBREG if it narrows the mode
5964                  or, on machines where byte operations extend, if the
5965                  constant masks to zero all the bits the mode doesn't have.  */
5966               && ((GET_MODE_SIZE (GET_MODE (varop))
5967                    < GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop))))
5968 #ifdef BYTE_LOADS_EXTEND
5969                   || (0 == (constop
5970                             & GET_MODE_MASK (GET_MODE (varop))
5971                             & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (varop)))))
5972 #endif
5973                   ))
5974             {
5975               varop = SUBREG_REG (varop);
5976               continue;
5977             }
5978           break;
5979
5980         case ZERO_EXTRACT:
5981         case SIGN_EXTRACT:
5982         case ZERO_EXTEND:
5983         case SIGN_EXTEND:
5984           /* Try to expand these into a series of shifts and then work
5985              with that result.  If we can't, for example, if the extract
5986              isn't at a fixed position, give up.  */
5987           temp = expand_compound_operation (varop);
5988           if (temp != varop)
5989             {
5990               varop = temp;
5991               continue;
5992             }
5993           break;
5994
5995         case AND:
5996           if (GET_CODE (XEXP (varop, 1)) == CONST_INT)
5997             {
5998               constop &= INTVAL (XEXP (varop, 1));
5999               varop = XEXP (varop, 0);
6000               continue;
6001             }
6002           break;
6003
6004         case IOR:
6005         case XOR:
6006           /* If VAROP is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6007              LSHIFT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6008              operation which may be a bitfield extraction.  Ensure
6009              that the constant we form is not wider than the mode of
6010              VAROP.  */
6011
6012           if (GET_CODE (XEXP (varop, 0)) == LSHIFTRT
6013               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
6014               && INTVAL (XEXP (XEXP (varop, 0), 1)) >= 0
6015               && INTVAL (XEXP (XEXP (varop, 0), 1)) < HOST_BITS_PER_WIDE_INT
6016               && GET_CODE (XEXP (varop, 1)) == CONST_INT
6017               && ((INTVAL (XEXP (XEXP (varop, 0), 1))
6018                   + floor_log2 (INTVAL (XEXP (varop, 1))))
6019                   < GET_MODE_BITSIZE (GET_MODE (varop)))
6020               && (INTVAL (XEXP (varop, 1))
6021                   & ~ nonzero_bits (XEXP (varop, 0), GET_MODE (varop)) == 0))
6022             {
6023               temp = GEN_INT ((INTVAL (XEXP (varop, 1)) & constop)
6024                               << INTVAL (XEXP (XEXP (varop, 0), 1)));
6025               temp = gen_binary (GET_CODE (varop), GET_MODE (varop),
6026                                  XEXP (XEXP (varop, 0), 0), temp);
6027               varop = gen_rtx_combine (LSHIFTRT, GET_MODE (varop),
6028                                        temp, XEXP (varop, 1));
6029               continue;
6030             }
6031
6032           /* Apply the AND to both branches of the IOR or XOR, then try to
6033              apply the distributive law.  This may eliminate operations 
6034              if either branch can be simplified because of the AND.
6035              It may also make some cases more complex, but those cases
6036              probably won't match a pattern either with or without this.  */
6037           return 
6038             gen_lowpart_for_combine
6039               (mode, apply_distributive_law
6040                (gen_rtx_combine
6041                 (GET_CODE (varop), GET_MODE (varop),
6042                  simplify_and_const_int (NULL_RTX, GET_MODE (varop),
6043                                          XEXP (varop, 0), constop),
6044                  simplify_and_const_int (NULL_RTX, GET_MODE (varop),
6045                                          XEXP (varop, 1), constop))));
6046
6047         case NOT:
6048           /* (and (not FOO)) is (and (xor FOO CONST)), so if FOO is an
6049              LSHIFTRT, we can do the same as above.  Ensure that the constant
6050              we form is not wider than the mode of VAROP.  */
6051
6052           if (GET_CODE (XEXP (varop, 0)) == LSHIFTRT
6053               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
6054               && INTVAL (XEXP (XEXP (varop, 0), 1)) >= 0
6055               && (INTVAL (XEXP (XEXP (varop, 0), 1)) + floor_log2 (constop)
6056                   < GET_MODE_BITSIZE (GET_MODE (varop)))
6057               && INTVAL (XEXP (XEXP (varop, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6058             {
6059               temp = GEN_INT (constop << INTVAL (XEXP (XEXP (varop, 0), 1)));
6060               temp = gen_binary (XOR, GET_MODE (varop),
6061                                  XEXP (XEXP (varop, 0), 0), temp);
6062               varop = gen_rtx_combine (LSHIFTRT, GET_MODE (varop),
6063                                        temp, XEXP (XEXP (varop, 0), 1));
6064               continue;
6065             }
6066           break;
6067
6068         case ASHIFTRT:
6069           /* If we are just looking for the sign bit, we don't need this
6070              shift at all, even if it has a variable count.  */
6071           if (constop == ((HOST_WIDE_INT) 1
6072                           << (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)))
6073             {
6074               varop = XEXP (varop, 0);
6075               continue;
6076             }
6077
6078           /* If this is a shift by a constant, get a mask that contains
6079              those bits that are not copies of the sign bit.  We then have
6080              two cases:  If CONSTOP only includes those bits, this can be
6081              a logical shift, which may allow simplifications.  If CONSTOP
6082              is a single-bit field not within those bits, we are requesting
6083              a copy of the sign bit and hence can shift the sign bit to
6084              the appropriate location.  */
6085           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
6086               && INTVAL (XEXP (varop, 1)) >= 0
6087               && INTVAL (XEXP (varop, 1)) < HOST_BITS_PER_WIDE_INT)
6088             {
6089               int i = -1;
6090
6091               nonzero = GET_MODE_MASK (GET_MODE (varop));
6092               nonzero >>= INTVAL (XEXP (varop, 1));
6093
6094               if ((constop & ~ nonzero) == 0
6095                   || (i = exact_log2 (constop)) >= 0)
6096                 {
6097                   varop = simplify_shift_const
6098                     (varop, LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
6099                      i < 0 ? INTVAL (XEXP (varop, 1))
6100                      : GET_MODE_BITSIZE (GET_MODE (varop)) - 1 - i);
6101                   if (GET_CODE (varop) != ASHIFTRT)
6102                     continue;
6103                 }
6104             }
6105
6106           /* If our mask is 1, convert this to a LSHIFTRT.  This can be done
6107              even if the shift count isn't a constant.  */
6108           if (constop == 1)
6109             varop = gen_rtx_combine (LSHIFTRT, GET_MODE (varop),
6110                                      XEXP (varop, 0), XEXP (varop, 1));
6111           break;
6112
6113         case LSHIFTRT:
6114           /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6115              shift and AND produces only copies of the sign bit (C2 is one less
6116              than a power of two), we can do this with just a shift.  */
6117
6118           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
6119               && ((INTVAL (XEXP (varop, 1))
6120                    + num_sign_bit_copies (XEXP (varop, 0),
6121                                           GET_MODE (XEXP (varop, 0))))
6122                   >= GET_MODE_BITSIZE (GET_MODE (varop)))
6123               && exact_log2 (constop + 1) >= 0
6124               && (num_sign_bit_copies (XEXP (varop, 0),
6125                                        GET_MODE (XEXP (varop, 0)))
6126                   >= exact_log2 (constop + 1)))
6127             varop
6128               = gen_rtx_combine (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
6129                                  GEN_INT (GET_MODE_BITSIZE (GET_MODE (varop))
6130                                           - exact_log2 (constop + 1)));
6131           break;
6132
6133         case NE:
6134           /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is
6135              included in STORE_FLAG_VALUE and FOO has no bits that might be
6136              nonzero not in CONST.  */
6137           if ((constop & ~ STORE_FLAG_VALUE) == 0
6138               && XEXP (varop, 0) == const0_rtx
6139               && (nonzero_bits (XEXP (varop, 0), mode) & ~ constop) == 0)
6140             {
6141               varop = XEXP (varop, 0);
6142               continue;
6143             }
6144           break;
6145
6146         case PLUS:
6147           /* In (and (plus FOO C1) M), if M is a mask that just turns off
6148              low-order bits (as in an alignment operation) and FOO is already
6149              aligned to that boundary, we can convert remove this AND
6150              and possibly the PLUS if it is now adding zero.  */
6151           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
6152               && exact_log2 (-constop) >= 0
6153               && (nonzero_bits (XEXP (varop, 0), mode) & ~ constop) == 0)
6154             {
6155               varop = plus_constant (XEXP (varop, 0),
6156                                      INTVAL (XEXP (varop, 1)) & constop);
6157               constop = ~0;
6158               break;
6159             }
6160
6161           /* ... fall through ... */
6162
6163         case MINUS:
6164           /* In (and (plus (and FOO M1) BAR) M2), if M1 and M2 are one
6165              less than powers of two and M2 is narrower than M1, we can
6166              eliminate the inner AND.  This occurs when incrementing
6167              bit fields.  */
6168
6169           if (GET_CODE (XEXP (varop, 0)) == ZERO_EXTRACT
6170               || GET_CODE (XEXP (varop, 0)) == ZERO_EXTEND)
6171             SUBST (XEXP (varop, 0),
6172                    expand_compound_operation (XEXP (varop, 0)));
6173
6174           if (GET_CODE (XEXP (varop, 0)) == AND
6175               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
6176               && exact_log2 (constop + 1) >= 0
6177               && exact_log2 (INTVAL (XEXP (XEXP (varop, 0), 1)) + 1) >= 0
6178               && (~ INTVAL (XEXP (XEXP (varop, 0), 1)) & constop) == 0)
6179             SUBST (XEXP (varop, 0), XEXP (XEXP (varop, 0), 0));
6180           break;
6181         }
6182
6183       break;
6184     }
6185
6186   /* If we have reached a constant, this whole thing is constant.  */
6187   if (GET_CODE (varop) == CONST_INT)
6188     return GEN_INT (constop & INTVAL (varop));
6189
6190   /* See what bits may be nonzero in VAROP.  Unlike the general case of
6191      a call to nonzero_bits, here we don't care about bits outside
6192      MODE.  */
6193
6194   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
6195
6196   /* Turn off all bits in the constant that are known to already be zero.
6197      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
6198      which is tested below.  */
6199
6200   constop &= nonzero;
6201
6202   /* If we don't have any bits left, return zero.  */
6203   if (constop == 0)
6204     return const0_rtx;
6205
6206   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
6207      if we already had one (just check for the simplest cases).  */
6208   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
6209       && GET_MODE (XEXP (x, 0)) == mode
6210       && SUBREG_REG (XEXP (x, 0)) == varop)
6211     varop = XEXP (x, 0);
6212   else
6213     varop = gen_lowpart_for_combine (mode, varop);
6214
6215   /* If we can't make the SUBREG, try to return what we were given. */
6216   if (GET_CODE (varop) == CLOBBER)
6217     return x ? x : varop;
6218
6219   /* If we are only masking insignificant bits, return VAROP.  */
6220   if (constop == nonzero)
6221     x = varop;
6222
6223   /* Otherwise, return an AND.  See how much, if any, of X we can use.  */
6224   else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
6225     x = gen_rtx_combine (AND, mode, varop, GEN_INT (constop));
6226
6227   else
6228     {
6229       if (GET_CODE (XEXP (x, 1)) != CONST_INT
6230           || INTVAL (XEXP (x, 1)) != constop)
6231         SUBST (XEXP (x, 1), GEN_INT (constop));
6232
6233       SUBST (XEXP (x, 0), varop);
6234     }
6235
6236   return x;
6237 }
6238 \f
6239 /* Given an expression, X, compute which bits in X can be non-zero.
6240    We don't care about bits outside of those defined in MODE.
6241
6242    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
6243    a shift, AND, or zero_extract, we can do better.  */
6244
6245 static unsigned HOST_WIDE_INT
6246 nonzero_bits (x, mode)
6247      rtx x;
6248      enum machine_mode mode;
6249 {
6250   unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
6251   unsigned HOST_WIDE_INT inner_nz;
6252   enum rtx_code code;
6253   int mode_width = GET_MODE_BITSIZE (mode);
6254   rtx tem;
6255
6256   /* If X is wider than MODE, use its mode instead.  */
6257   if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
6258     {
6259       mode = GET_MODE (x);
6260       nonzero = GET_MODE_MASK (mode);
6261       mode_width = GET_MODE_BITSIZE (mode);
6262     }
6263
6264   if (mode_width > HOST_BITS_PER_WIDE_INT)
6265     /* Our only callers in this case look for single bit values.  So
6266        just return the mode mask.  Those tests will then be false.  */
6267     return nonzero;
6268
6269   code = GET_CODE (x);
6270   switch (code)
6271     {
6272     case REG:
6273 #ifdef STACK_BOUNDARY
6274       /* If this is the stack pointer, we may know something about its
6275          alignment.  If PUSH_ROUNDING is defined, it is possible for the
6276          stack to be momentarily aligned only to that amount, so we pick
6277          the least alignment.  */
6278
6279       if (x == stack_pointer_rtx)
6280         {
6281           int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
6282
6283 #ifdef PUSH_ROUNDING
6284           sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
6285 #endif
6286
6287           return nonzero & ~ (sp_alignment - 1);
6288         }
6289 #endif
6290
6291       /* If X is a register whose nonzero bits value is current, use it.
6292          Otherwise, if X is a register whose value we can find, use that
6293          value.  Otherwise, use the previously-computed global nonzero bits
6294          for this register.  */
6295
6296       if (reg_last_set_value[REGNO (x)] != 0
6297           && reg_last_set_mode[REGNO (x)] == mode
6298           && (reg_n_sets[REGNO (x)] == 1
6299               || reg_last_set_label[REGNO (x)] == label_tick)
6300           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
6301         return reg_last_set_nonzero_bits[REGNO (x)];
6302
6303       tem = get_last_value (x);
6304
6305       if (tem)
6306         {
6307 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
6308           /* If X is narrower than MODE and TEM is a non-negative
6309              constant that would appear negative in the mode of X,
6310              sign-extend it for use in reg_nonzero_bits because some
6311              machines (maybe most) will actually do the sign-extension
6312              and this is the conservative approach. 
6313
6314              ??? For 2.5, try to tighten up the MD files in this regard
6315              instead of this kludge.  */
6316
6317           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
6318               && GET_CODE (tem) == CONST_INT
6319               && INTVAL (tem) > 0
6320               && 0 != (INTVAL (tem)
6321                        & ((HOST_WIDE_INT) 1
6322                           << GET_MODE_BITSIZE (GET_MODE (x)))))
6323             tem = GEN_INT (INTVAL (tem)
6324                            | ((HOST_WIDE_INT) (-1)
6325                               << GET_MODE_BITSIZE (GET_MODE (x))));
6326 #endif
6327           return nonzero_bits (tem, mode);
6328         }
6329       else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
6330         return reg_nonzero_bits[REGNO (x)] & nonzero;
6331       else
6332         return nonzero;
6333
6334     case CONST_INT:
6335 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
6336       /* If X is negative in MODE, sign-extend the value.  */
6337       if (INTVAL (x) > 0
6338           && 0 != (INTVAL (x)
6339                    & ((HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (GET_MODE (x)))))
6340         return (INTVAL (x)
6341                 | ((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (GET_MODE (x))));
6342 #endif
6343
6344       return INTVAL (x);
6345
6346 #ifdef BYTE_LOADS_ZERO_EXTEND
6347     case MEM:
6348       /* In many, if not most, RISC machines, reading a byte from memory
6349          zeros the rest of the register.  Noticing that fact saves a lot
6350          of extra zero-extends.  */
6351       nonzero &= GET_MODE_MASK (GET_MODE (x));
6352       break;
6353 #endif
6354
6355 #if STORE_FLAG_VALUE == 1
6356     case EQ:  case NE:
6357     case GT:  case GTU:
6358     case LT:  case LTU:
6359     case GE:  case GEU:
6360     case LE:  case LEU:
6361
6362       if (GET_MODE_CLASS (mode) == MODE_INT)
6363         nonzero = 1;
6364
6365       /* A comparison operation only sets the bits given by its mode.  The
6366          rest are set undefined.  */
6367       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
6368         nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
6369       break;
6370 #endif
6371
6372     case NEG:
6373       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
6374           == GET_MODE_BITSIZE (GET_MODE (x)))
6375         nonzero = 1;
6376
6377       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
6378         nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
6379       break;
6380
6381     case ABS:
6382       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
6383           == GET_MODE_BITSIZE (GET_MODE (x)))
6384         nonzero = 1;
6385       break;
6386
6387     case TRUNCATE:
6388       nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
6389       break;
6390
6391     case ZERO_EXTEND:
6392       nonzero &= nonzero_bits (XEXP (x, 0), mode);
6393       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
6394         nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
6395       break;
6396
6397     case SIGN_EXTEND:
6398       /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
6399          Otherwise, show all the bits in the outer mode but not the inner
6400          may be non-zero.  */
6401       inner_nz = nonzero_bits (XEXP (x, 0), mode);
6402       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
6403         {
6404           inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
6405           if (inner_nz &
6406               (((HOST_WIDE_INT) 1
6407                 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
6408             inner_nz |= (GET_MODE_MASK (mode)
6409                           & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
6410         }
6411
6412       nonzero &= inner_nz;
6413       break;
6414
6415     case AND:
6416       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
6417                   & nonzero_bits (XEXP (x, 1), mode));
6418       break;
6419
6420     case XOR:   case IOR:
6421     case UMIN:  case UMAX:  case SMIN:  case SMAX:
6422       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
6423                   | nonzero_bits (XEXP (x, 1), mode));
6424       break;
6425
6426     case PLUS:  case MINUS:
6427     case MULT:
6428     case DIV:   case UDIV:
6429     case MOD:   case UMOD:
6430       /* We can apply the rules of arithmetic to compute the number of
6431          high- and low-order zero bits of these operations.  We start by
6432          computing the width (position of the highest-order non-zero bit)
6433          and the number of low-order zero bits for each value.  */
6434       {
6435         unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
6436         unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
6437         int width0 = floor_log2 (nz0) + 1;
6438         int width1 = floor_log2 (nz1) + 1;
6439         int low0 = floor_log2 (nz0 & -nz0);
6440         int low1 = floor_log2 (nz1 & -nz1);
6441         int op0_maybe_minusp = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
6442         int op1_maybe_minusp = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
6443         int result_width = mode_width;
6444         int result_low = 0;
6445
6446         switch (code)
6447           {
6448           case PLUS:
6449             result_width = MAX (width0, width1) + 1;
6450             result_low = MIN (low0, low1);
6451             break;
6452           case MINUS:
6453             result_low = MIN (low0, low1);
6454             break;
6455           case MULT:
6456             result_width = width0 + width1;
6457             result_low = low0 + low1;
6458             break;
6459           case DIV:
6460             if (! op0_maybe_minusp && ! op1_maybe_minusp)
6461               result_width = width0;
6462             break;
6463           case UDIV:
6464             result_width = width0;
6465             break;
6466           case MOD:
6467             if (! op0_maybe_minusp && ! op1_maybe_minusp)
6468               result_width = MIN (width0, width1);
6469             result_low = MIN (low0, low1);
6470             break;
6471           case UMOD:
6472             result_width = MIN (width0, width1);
6473             result_low = MIN (low0, low1);
6474             break;
6475           }
6476
6477         if (result_width < mode_width)
6478           nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
6479
6480         if (result_low > 0)
6481           nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
6482       }
6483       break;
6484
6485     case ZERO_EXTRACT:
6486       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6487           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6488         nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
6489       break;
6490
6491     case SUBREG:
6492       /* If this is a SUBREG formed for a promoted variable that has
6493          been zero-extended, we know that at least the high-order bits
6494          are zero, though others might be too.  */
6495
6496       if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
6497         nonzero = (GET_MODE_MASK (GET_MODE (x))
6498                    & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
6499
6500       /* If the inner mode is a single word for both the host and target
6501          machines, we can compute this from which bits of the inner
6502          object might be nonzero.  */
6503       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
6504           && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
6505               <= HOST_BITS_PER_WIDE_INT))
6506         {
6507           nonzero &= nonzero_bits (SUBREG_REG (x), mode);
6508 #ifndef BYTE_LOADS_EXTEND
6509           /* On many CISC machines, accessing an object in a wider mode
6510              causes the high-order bits to become undefined.  So they are
6511              not known to be zero.  */
6512           if (GET_MODE_SIZE (GET_MODE (x))
6513               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6514             nonzero |= (GET_MODE_MASK (GET_MODE (x))
6515                         & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
6516 #endif
6517         }
6518       break;
6519
6520     case ASHIFTRT:
6521     case LSHIFTRT:
6522     case ASHIFT:
6523     case LSHIFT:
6524     case ROTATE:
6525       /* The nonzero bits are in two classes: any bits within MODE
6526          that aren't in GET_MODE (x) are always significant.  The rest of the
6527          nonzero bits are those that are significant in the operand of
6528          the shift when shifted the appropriate number of bits.  This
6529          shows that high-order bits are cleared by the right shift and
6530          low-order bits by left shifts.  */
6531       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6532           && INTVAL (XEXP (x, 1)) >= 0
6533           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6534         {
6535           enum machine_mode inner_mode = GET_MODE (x);
6536           int width = GET_MODE_BITSIZE (inner_mode);
6537           int count = INTVAL (XEXP (x, 1));
6538           unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
6539           unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
6540           unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
6541           unsigned HOST_WIDE_INT outer = 0;
6542
6543           if (mode_width > width)
6544             outer = (op_nonzero & nonzero & ~ mode_mask);
6545
6546           if (code == LSHIFTRT)
6547             inner >>= count;
6548           else if (code == ASHIFTRT)
6549             {
6550               inner >>= count;
6551
6552               /* If the sign bit may have been nonzero before the shift, we
6553                  need to mark all the places it could have been copied to
6554                  by the shift as possibly nonzero.  */
6555               if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
6556                 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
6557             }
6558           else if (code == LSHIFT || code == ASHIFT)
6559             inner <<= count;
6560           else
6561             inner = ((inner << (count % width)
6562                       | (inner >> (width - (count % width)))) & mode_mask);
6563
6564           nonzero &= (outer | inner);
6565         }
6566       break;
6567
6568     case FFS:
6569       /* This is at most the number of bits in the mode.  */
6570       nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
6571       break;
6572
6573     case IF_THEN_ELSE:
6574       nonzero &= (nonzero_bits (XEXP (x, 1), mode)
6575                   | nonzero_bits (XEXP (x, 2), mode));
6576       break;
6577     }
6578
6579   return nonzero;
6580 }
6581 \f
6582 /* Return the number of bits at the high-order end of X that are known to
6583    be equal to the sign bit.  This number will always be between 1 and
6584    the number of bits in the mode of X.  MODE is the mode to be used
6585    if X is VOIDmode.  */
6586
6587 static int
6588 num_sign_bit_copies (x, mode)
6589      rtx x;
6590      enum machine_mode mode;
6591 {
6592   enum rtx_code code = GET_CODE (x);
6593   int bitwidth;
6594   int num0, num1, result;
6595   unsigned HOST_WIDE_INT nonzero;
6596   rtx tem;
6597
6598   /* If we weren't given a mode, use the mode of X.  If the mode is still
6599      VOIDmode, we don't know anything.  */
6600
6601   if (mode == VOIDmode)
6602     mode = GET_MODE (x);
6603
6604   if (mode == VOIDmode)
6605     return 1;
6606
6607   bitwidth = GET_MODE_BITSIZE (mode);
6608
6609   switch (code)
6610     {
6611     case REG:
6612
6613       if (reg_last_set_value[REGNO (x)] != 0
6614           && reg_last_set_mode[REGNO (x)] == mode
6615           && (reg_n_sets[REGNO (x)] == 1
6616               || reg_last_set_label[REGNO (x)] == label_tick)
6617           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
6618         return reg_last_set_sign_bit_copies[REGNO (x)];
6619
6620       tem =  get_last_value (x);
6621       if (tem != 0)
6622         return num_sign_bit_copies (tem, mode);
6623
6624       if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
6625         return reg_sign_bit_copies[REGNO (x)];
6626       break;
6627
6628 #ifdef BYTE_LOADS_SIGN_EXTEND
6629     case MEM:
6630       /* Some RISC machines sign-extend all loads of smaller than a word.  */
6631       return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
6632 #endif
6633
6634     case CONST_INT:
6635       /* If the constant is negative, take its 1's complement and remask.
6636          Then see how many zero bits we have.  */
6637       nonzero = INTVAL (x) & GET_MODE_MASK (mode);
6638       if (bitwidth <= HOST_BITS_PER_WIDE_INT
6639           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
6640         nonzero = (~ nonzero) & GET_MODE_MASK (mode);
6641
6642       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
6643
6644     case SUBREG:
6645       /* If this is a SUBREG for a promoted object that is sign-extended
6646          and we are looking at it in a wider mode, we know that at least the
6647          high-order bits are known to be sign bit copies.  */
6648
6649       if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
6650         return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
6651                     num_sign_bit_copies (SUBREG_REG (x), mode));
6652
6653       /* For a smaller object, just ignore the high bits. */
6654       if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
6655         {
6656           num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
6657           return MAX (1, (num0
6658                           - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
6659                              - bitwidth)));
6660         }
6661
6662 #ifdef BYTE_LOADS_EXTEND
6663       /* For paradoxical SUBREGs, just look inside since, on machines with
6664          one of these defined, we assume that operations are actually 
6665          performed on the full register.  Note that we are passing MODE
6666          to the recursive call, so the number of sign bit copies will
6667          remain relative to that mode, not the inner mode.  */
6668
6669       if (GET_MODE_SIZE (GET_MODE (x))
6670           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6671         return num_sign_bit_copies (SUBREG_REG (x), mode);
6672 #endif
6673
6674       break;
6675
6676     case SIGN_EXTRACT:
6677       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6678         return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
6679       break;
6680
6681     case SIGN_EXTEND: 
6682       return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6683               + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
6684
6685     case TRUNCATE:
6686       /* For a smaller object, just ignore the high bits. */
6687       num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
6688       return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6689                               - bitwidth)));
6690
6691     case NOT:
6692       return num_sign_bit_copies (XEXP (x, 0), mode);
6693
6694     case ROTATE:       case ROTATERT:
6695       /* If we are rotating left by a number of bits less than the number
6696          of sign bit copies, we can just subtract that amount from the
6697          number.  */
6698       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6699           && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
6700         {
6701           num0 = num_sign_bit_copies (XEXP (x, 0), mode);
6702           return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
6703                                  : bitwidth - INTVAL (XEXP (x, 1))));
6704         }
6705       break;
6706
6707     case NEG:
6708       /* In general, this subtracts one sign bit copy.  But if the value
6709          is known to be positive, the number of sign bit copies is the
6710          same as that of the input.  Finally, if the input has just one bit
6711          that might be nonzero, all the bits are copies of the sign bit.  */
6712       nonzero = nonzero_bits (XEXP (x, 0), mode);
6713       if (nonzero == 1)
6714         return bitwidth;
6715
6716       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
6717       if (num0 > 1
6718           && bitwidth <= HOST_BITS_PER_WIDE_INT
6719           && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
6720         num0--;
6721
6722       return num0;
6723
6724     case IOR:   case AND:   case XOR:
6725     case SMIN:  case SMAX:  case UMIN:  case UMAX:
6726       /* Logical operations will preserve the number of sign-bit copies.
6727          MIN and MAX operations always return one of the operands.  */
6728       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
6729       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
6730       return MIN (num0, num1);
6731
6732     case PLUS:  case MINUS:
6733       /* For addition and subtraction, we can have a 1-bit carry.  However,
6734          if we are subtracting 1 from a positive number, there will not
6735          be such a carry.  Furthermore, if the positive number is known to
6736          be 0 or 1, we know the result is either -1 or 0.  */
6737
6738       if (code == PLUS && XEXP (x, 1) == constm1_rtx
6739           && bitwidth <= HOST_BITS_PER_WIDE_INT)
6740         {
6741           nonzero = nonzero_bits (XEXP (x, 0), mode);
6742           if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
6743             return (nonzero == 1 || nonzero == 0 ? bitwidth
6744                     : bitwidth - floor_log2 (nonzero) - 1);
6745         }
6746
6747       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
6748       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
6749       return MAX (1, MIN (num0, num1) - 1);
6750       
6751     case MULT:
6752       /* The number of bits of the product is the sum of the number of
6753          bits of both terms.  However, unless one of the terms if known
6754          to be positive, we must allow for an additional bit since negating
6755          a negative number can remove one sign bit copy.  */
6756
6757       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
6758       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
6759
6760       result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
6761       if (result > 0
6762           && bitwidth <= HOST_BITS_PER_WIDE_INT
6763           && ((nonzero_bits (XEXP (x, 0), mode)
6764                & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
6765           && (nonzero_bits (XEXP (x, 1), mode)
6766               & ((HOST_WIDE_INT) 1 << (bitwidth - 1)) != 0))
6767         result--;
6768
6769       return MAX (1, result);
6770
6771     case UDIV:
6772       /* The result must be <= the first operand.  */
6773       return num_sign_bit_copies (XEXP (x, 0), mode);
6774
6775     case UMOD:
6776       /* The result must be <= the scond operand.  */
6777       return num_sign_bit_copies (XEXP (x, 1), mode);
6778
6779     case DIV:
6780       /* Similar to unsigned division, except that we have to worry about
6781          the case where the divisor is negative, in which case we have
6782          to add 1.  */
6783       result = num_sign_bit_copies (XEXP (x, 0), mode);
6784       if (result > 1
6785           && bitwidth <= HOST_BITS_PER_WIDE_INT
6786           && (nonzero_bits (XEXP (x, 1), mode)
6787               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
6788         result --;
6789
6790       return result;
6791
6792     case MOD:
6793       result = num_sign_bit_copies (XEXP (x, 1), mode);
6794       if (result > 1
6795           && bitwidth <= HOST_BITS_PER_WIDE_INT
6796           && (nonzero_bits (XEXP (x, 1), mode)
6797               & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
6798         result --;
6799
6800       return result;
6801
6802     case ASHIFTRT:
6803       /* Shifts by a constant add to the number of bits equal to the
6804          sign bit.  */
6805       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
6806       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6807           && INTVAL (XEXP (x, 1)) > 0)
6808         num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
6809
6810       return num0;
6811
6812     case ASHIFT:
6813     case LSHIFT:
6814       /* Left shifts destroy copies.  */
6815       if (GET_CODE (XEXP (x, 1)) != CONST_INT
6816           || INTVAL (XEXP (x, 1)) < 0
6817           || INTVAL (XEXP (x, 1)) >= bitwidth)
6818         return 1;
6819
6820       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
6821       return MAX (1, num0 - INTVAL (XEXP (x, 1)));
6822
6823     case IF_THEN_ELSE:
6824       num0 = num_sign_bit_copies (XEXP (x, 1), mode);
6825       num1 = num_sign_bit_copies (XEXP (x, 2), mode);
6826       return MIN (num0, num1);
6827
6828 #if STORE_FLAG_VALUE == -1
6829     case EQ:  case NE:  case GE:  case GT:  case LE:  case LT:
6830     case GEU: case GTU: case LEU: case LTU:
6831       return bitwidth;
6832 #endif
6833     }
6834
6835   /* If we haven't been able to figure it out by one of the above rules,
6836      see if some of the high-order bits are known to be zero.  If so,
6837      count those bits and return one less than that amount.  If we can't
6838      safely compute the mask for this mode, always return BITWIDTH.  */
6839
6840   if (bitwidth > HOST_BITS_PER_WIDE_INT)
6841     return 1;
6842
6843   nonzero = nonzero_bits (x, mode);
6844   return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
6845           ? 1 : bitwidth - floor_log2 (nonzero) - 1);
6846 }
6847 \f
6848 /* Return the number of "extended" bits there are in X, when interpreted
6849    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
6850    unsigned quantities, this is the number of high-order zero bits.
6851    For signed quantities, this is the number of copies of the sign bit
6852    minus 1.  In both case, this function returns the number of "spare"
6853    bits.  For example, if two quantities for which this function returns
6854    at least 1 are added, the addition is known not to overflow.
6855
6856    This function will always return 0 unless called during combine, which
6857    implies that it must be called from a define_split.  */
6858
6859 int
6860 extended_count (x, mode, unsignedp)
6861      rtx x;
6862      enum machine_mode mode;
6863      int unsignedp;
6864 {
6865   if (nonzero_sign_valid == 0)
6866     return 0;
6867
6868   return (unsignedp
6869           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6870              && (GET_MODE_BITSIZE (mode) - 1
6871                  - floor_log2 (nonzero_bits (x, mode))))
6872           : num_sign_bit_copies (x, mode) - 1);
6873 }
6874 \f
6875 /* This function is called from `simplify_shift_const' to merge two
6876    outer operations.  Specifically, we have already found that we need
6877    to perform operation *POP0 with constant *PCONST0 at the outermost
6878    position.  We would now like to also perform OP1 with constant CONST1
6879    (with *POP0 being done last).
6880
6881    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
6882    the resulting operation.  *PCOMP_P is set to 1 if we would need to 
6883    complement the innermost operand, otherwise it is unchanged.
6884
6885    MODE is the mode in which the operation will be done.  No bits outside
6886    the width of this mode matter.  It is assumed that the width of this mode
6887    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
6888
6889    If *POP0 or OP1 are NIL, it means no operation is required.  Only NEG, PLUS,
6890    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
6891    result is simply *PCONST0.
6892
6893    If the resulting operation cannot be expressed as one operation, we
6894    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
6895
6896 static int
6897 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
6898      enum rtx_code *pop0;
6899      HOST_WIDE_INT *pconst0;
6900      enum rtx_code op1;
6901      HOST_WIDE_INT const1;
6902      enum machine_mode mode;
6903      int *pcomp_p;
6904 {
6905   enum rtx_code op0 = *pop0;
6906   HOST_WIDE_INT const0 = *pconst0;
6907
6908   const0 &= GET_MODE_MASK (mode);
6909   const1 &= GET_MODE_MASK (mode);
6910
6911   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
6912   if (op0 == AND)
6913     const1 &= const0;
6914
6915   /* If OP0 or OP1 is NIL, this is easy.  Similarly if they are the same or
6916      if OP0 is SET.  */
6917
6918   if (op1 == NIL || op0 == SET)
6919     return 1;
6920
6921   else if (op0 == NIL)
6922     op0 = op1, const0 = const1;
6923
6924   else if (op0 == op1)
6925     {
6926       switch (op0)
6927         {
6928         case AND:
6929           const0 &= const1;
6930           break;
6931         case IOR:
6932           const0 |= const1;
6933           break;
6934         case XOR:
6935           const0 ^= const1;
6936           break;
6937         case PLUS:
6938           const0 += const1;
6939           break;
6940         case NEG:
6941           op0 = NIL;
6942           break;
6943         }
6944     }
6945
6946   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
6947   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
6948     return 0;
6949
6950   /* If the two constants aren't the same, we can't do anything.  The
6951      remaining six cases can all be done.  */
6952   else if (const0 != const1)
6953     return 0;
6954
6955   else
6956     switch (op0)
6957       {
6958       case IOR:
6959         if (op1 == AND)
6960           /* (a & b) | b == b */
6961           op0 = SET;
6962         else /* op1 == XOR */
6963           /* (a ^ b) | b == a | b */
6964           ;
6965         break;
6966
6967       case XOR:
6968         if (op1 == AND)
6969           /* (a & b) ^ b == (~a) & b */
6970           op0 = AND, *pcomp_p = 1;
6971         else /* op1 == IOR */
6972           /* (a | b) ^ b == a & ~b */
6973           op0 = AND, *pconst0 = ~ const0;
6974         break;
6975
6976       case AND:
6977         if (op1 == IOR)
6978           /* (a | b) & b == b */
6979         op0 = SET;
6980         else /* op1 == XOR */
6981           /* (a ^ b) & b) == (~a) & b */
6982           *pcomp_p = 1;
6983         break;
6984       }
6985
6986   /* Check for NO-OP cases.  */
6987   const0 &= GET_MODE_MASK (mode);
6988   if (const0 == 0
6989       && (op0 == IOR || op0 == XOR || op0 == PLUS))
6990     op0 = NIL;
6991   else if (const0 == 0 && op0 == AND)
6992     op0 = SET;
6993   else if (const0 == GET_MODE_MASK (mode) && op0 == AND)
6994     op0 = NIL;
6995
6996   *pop0 = op0;
6997   *pconst0 = const0;
6998
6999   return 1;
7000 }
7001 \f
7002 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
7003    The result of the shift is RESULT_MODE.  X, if non-zero, is an expression
7004    that we started with.
7005
7006    The shift is normally computed in the widest mode we find in VAROP, as
7007    long as it isn't a different number of words than RESULT_MODE.  Exceptions
7008    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
7009
7010 static rtx
7011 simplify_shift_const (x, code, result_mode, varop, count)
7012      rtx x;
7013      enum rtx_code code;
7014      enum machine_mode result_mode;
7015      rtx varop;
7016      int count;
7017 {
7018   enum rtx_code orig_code = code;
7019   int orig_count = count;
7020   enum machine_mode mode = result_mode;
7021   enum machine_mode shift_mode, tmode;
7022   int mode_words
7023     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
7024   /* We form (outer_op (code varop count) (outer_const)).  */
7025   enum rtx_code outer_op = NIL;
7026   HOST_WIDE_INT outer_const;
7027   rtx const_rtx;
7028   int complement_p = 0;
7029   rtx new;
7030
7031   /* If we were given an invalid count, don't do anything except exactly
7032      what was requested.  */
7033
7034   if (count < 0 || count > GET_MODE_BITSIZE (mode))
7035     {
7036       if (x)
7037         return x;
7038
7039       return gen_rtx (code, mode, varop, GEN_INT (count));
7040     }
7041
7042   /* Unless one of the branches of the `if' in this loop does a `continue',
7043      we will `break' the loop after the `if'.  */
7044
7045   while (count != 0)
7046     {
7047       /* If we have an operand of (clobber (const_int 0)), just return that
7048          value.  */
7049       if (GET_CODE (varop) == CLOBBER)
7050         return varop;
7051
7052       /* If we discovered we had to complement VAROP, leave.  Making a NOT
7053          here would cause an infinite loop.  */
7054       if (complement_p)
7055         break;
7056
7057       /* Convert ROTATETRT to ROTATE.  */
7058       if (code == ROTATERT)
7059         code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
7060
7061       /* Canonicalize LSHIFT to ASHIFT.  */
7062       if (code == LSHIFT)
7063         code = ASHIFT;
7064
7065       /* We need to determine what mode we will do the shift in.  If the
7066          shift is a ASHIFTRT or ROTATE, we must always do it in the mode it
7067          was originally done in.  Otherwise, we can do it in MODE, the widest
7068          mode encountered. */
7069       shift_mode = (code == ASHIFTRT || code == ROTATE ? result_mode : mode);
7070
7071       /* Handle cases where the count is greater than the size of the mode
7072          minus 1.  For ASHIFT, use the size minus one as the count (this can
7073          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
7074          take the count modulo the size.  For other shifts, the result is
7075          zero.
7076
7077          Since these shifts are being produced by the compiler by combining
7078          multiple operations, each of which are defined, we know what the
7079          result is supposed to be.  */
7080          
7081       if (count > GET_MODE_BITSIZE (shift_mode) - 1)
7082         {
7083           if (code == ASHIFTRT)
7084             count = GET_MODE_BITSIZE (shift_mode) - 1;
7085           else if (code == ROTATE || code == ROTATERT)
7086             count %= GET_MODE_BITSIZE (shift_mode);
7087           else
7088             {
7089               /* We can't simply return zero because there may be an
7090                  outer op.  */
7091               varop = const0_rtx;
7092               count = 0;
7093               break;
7094             }
7095         }
7096
7097       /* Negative counts are invalid and should not have been made (a
7098          programmer-specified negative count should have been handled
7099          above). */
7100       else if (count < 0)
7101         abort ();
7102
7103       /* An arithmetic right shift of a quantity known to be -1 or 0
7104          is a no-op.  */
7105       if (code == ASHIFTRT
7106           && (num_sign_bit_copies (varop, shift_mode)
7107               == GET_MODE_BITSIZE (shift_mode)))
7108         {
7109           count = 0;
7110           break;
7111         }
7112
7113       /* If we are doing an arithmetic right shift and discarding all but
7114          the sign bit copies, this is equivalent to doing a shift by the
7115          bitsize minus one.  Convert it into that shift because it will often
7116          allow other simplifications.  */
7117
7118       if (code == ASHIFTRT
7119           && (count + num_sign_bit_copies (varop, shift_mode)
7120               >= GET_MODE_BITSIZE (shift_mode)))
7121         count = GET_MODE_BITSIZE (shift_mode) - 1;
7122
7123       /* We simplify the tests below and elsewhere by converting
7124          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
7125          `make_compound_operation' will convert it to a ASHIFTRT for
7126          those machines (such as Vax) that don't have a LSHIFTRT.  */
7127       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
7128           && code == ASHIFTRT
7129           && ((nonzero_bits (varop, shift_mode)
7130                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
7131               == 0))
7132         code = LSHIFTRT;
7133
7134       switch (GET_CODE (varop))
7135         {
7136         case SIGN_EXTEND:
7137         case ZERO_EXTEND:
7138         case SIGN_EXTRACT:
7139         case ZERO_EXTRACT:
7140           new = expand_compound_operation (varop);
7141           if (new != varop)
7142             {
7143               varop = new;
7144               continue;
7145             }
7146           break;
7147
7148         case MEM:
7149           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
7150              minus the width of a smaller mode, we can do this with a
7151              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
7152           if ((code == ASHIFTRT || code == LSHIFTRT)
7153               && ! mode_dependent_address_p (XEXP (varop, 0))
7154               && ! MEM_VOLATILE_P (varop)
7155               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
7156                                          MODE_INT, 1)) != BLKmode)
7157             {
7158 #if BYTES_BIG_ENDIAN
7159               new = gen_rtx (MEM, tmode, XEXP (varop, 0));
7160 #else
7161               new = gen_rtx (MEM, tmode,
7162                              plus_constant (XEXP (varop, 0),
7163                                             count / BITS_PER_UNIT));
7164               RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
7165               MEM_VOLATILE_P (new) = MEM_VOLATILE_P (varop);
7166               MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (varop);
7167 #endif
7168               varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
7169                                        : ZERO_EXTEND, mode, new);
7170               count = 0;
7171               continue;
7172             }
7173           break;
7174
7175         case USE:
7176           /* Similar to the case above, except that we can only do this if
7177              the resulting mode is the same as that of the underlying
7178              MEM and adjust the address depending on the *bits* endianness
7179              because of the way that bit-field extract insns are defined.  */
7180           if ((code == ASHIFTRT || code == LSHIFTRT)
7181               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
7182                                          MODE_INT, 1)) != BLKmode
7183               && tmode == GET_MODE (XEXP (varop, 0)))
7184             {
7185 #if BITS_BIG_ENDIAN
7186               new = XEXP (varop, 0);
7187 #else
7188               new = copy_rtx (XEXP (varop, 0));
7189               SUBST (XEXP (new, 0), 
7190                      plus_constant (XEXP (new, 0),
7191                                     count / BITS_PER_UNIT));
7192 #endif
7193
7194               varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
7195                                        : ZERO_EXTEND, mode, new);
7196               count = 0;
7197               continue;
7198             }
7199           break;
7200
7201         case SUBREG:
7202           /* If VAROP is a SUBREG, strip it as long as the inner operand has
7203              the same number of words as what we've seen so far.  Then store
7204              the widest mode in MODE.  */
7205           if (subreg_lowpart_p (varop)
7206               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
7207                   > GET_MODE_SIZE (GET_MODE (varop)))
7208               && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
7209                     + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7210                   == mode_words))
7211             {
7212               varop = SUBREG_REG (varop);
7213               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
7214                 mode = GET_MODE (varop);
7215               continue;
7216             }
7217           break;
7218
7219         case MULT:
7220           /* Some machines use MULT instead of ASHIFT because MULT
7221              is cheaper.  But it is still better on those machines to
7222              merge two shifts into one.  */
7223           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
7224               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
7225             {
7226               varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
7227                                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));;
7228               continue;
7229             }
7230           break;
7231
7232         case UDIV:
7233           /* Similar, for when divides are cheaper.  */
7234           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
7235               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
7236             {
7237               varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
7238                                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
7239               continue;
7240             }
7241           break;
7242
7243         case ASHIFTRT:
7244           /* If we are extracting just the sign bit of an arithmetic right 
7245              shift, that shift is not needed.  */
7246           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
7247             {
7248               varop = XEXP (varop, 0);
7249               continue;
7250             }
7251
7252           /* ... fall through ... */
7253
7254         case LSHIFTRT:
7255         case ASHIFT:
7256         case LSHIFT:
7257         case ROTATE:
7258           /* Here we have two nested shifts.  The result is usually the
7259              AND of a new shift with a mask.  We compute the result below.  */
7260           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
7261               && INTVAL (XEXP (varop, 1)) >= 0
7262               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
7263               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
7264               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
7265             {
7266               enum rtx_code first_code = GET_CODE (varop);
7267               int first_count = INTVAL (XEXP (varop, 1));
7268               unsigned HOST_WIDE_INT mask;
7269               rtx mask_rtx;
7270               rtx inner;
7271
7272               if (first_code == LSHIFT)
7273                 first_code = ASHIFT;
7274
7275               /* We have one common special case.  We can't do any merging if
7276                  the inner code is an ASHIFTRT of a smaller mode.  However, if
7277                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
7278                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
7279                  we can convert it to
7280                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
7281                  This simplifies certain SIGN_EXTEND operations.  */
7282               if (code == ASHIFT && first_code == ASHIFTRT
7283                   && (GET_MODE_BITSIZE (result_mode)
7284                       - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
7285                 {
7286                   /* C3 has the low-order C1 bits zero.  */
7287                   
7288                   mask = (GET_MODE_MASK (mode)
7289                           & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
7290
7291                   varop = simplify_and_const_int (NULL_RTX, result_mode,
7292                                                   XEXP (varop, 0), mask);
7293                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
7294                                                 varop, count);
7295                   count = first_count;
7296                   code = ASHIFTRT;
7297                   continue;
7298                 }
7299               
7300               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
7301                  than C1 high-order bits equal to the sign bit, we can convert
7302                  this to either an ASHIFT or a ASHIFTRT depending on the
7303                  two counts. 
7304
7305                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
7306
7307               if (code == ASHIFTRT && first_code == ASHIFT
7308                   && GET_MODE (varop) == shift_mode
7309                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
7310                       > first_count))
7311                 {
7312                   count -= first_count;
7313                   if (count < 0)
7314                     count = - count, code = ASHIFT;
7315                   varop = XEXP (varop, 0);
7316                   continue;
7317                 }
7318
7319               /* There are some cases we can't do.  If CODE is ASHIFTRT,
7320                  we can only do this if FIRST_CODE is also ASHIFTRT.
7321
7322                  We can't do the case when CODE is ROTATE and FIRST_CODE is
7323                  ASHIFTRT.
7324
7325                  If the mode of this shift is not the mode of the outer shift,
7326                  we can't do this if either shift is ASHIFTRT or ROTATE.
7327
7328                  Finally, we can't do any of these if the mode is too wide
7329                  unless the codes are the same.
7330
7331                  Handle the case where the shift codes are the same
7332                  first.  */
7333
7334               if (code == first_code)
7335                 {
7336                   if (GET_MODE (varop) != result_mode
7337                       && (code == ASHIFTRT || code == ROTATE))
7338                     break;
7339
7340                   count += first_count;
7341                   varop = XEXP (varop, 0);
7342                   continue;
7343                 }
7344
7345               if (code == ASHIFTRT
7346                   || (code == ROTATE && first_code == ASHIFTRT)
7347                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
7348                   || (GET_MODE (varop) != result_mode
7349                       && (first_code == ASHIFTRT || first_code == ROTATE
7350                           || code == ROTATE)))
7351                 break;
7352
7353               /* To compute the mask to apply after the shift, shift the
7354                  nonzero bits of the inner shift the same way the 
7355                  outer shift will.  */
7356
7357               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
7358
7359               mask_rtx
7360                 = simplify_binary_operation (code, result_mode, mask_rtx,
7361                                              GEN_INT (count));
7362                                   
7363               /* Give up if we can't compute an outer operation to use.  */
7364               if (mask_rtx == 0
7365                   || GET_CODE (mask_rtx) != CONST_INT
7366                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
7367                                         INTVAL (mask_rtx),
7368                                         result_mode, &complement_p))
7369                 break;
7370
7371               /* If the shifts are in the same direction, we add the
7372                  counts.  Otherwise, we subtract them.  */
7373               if ((code == ASHIFTRT || code == LSHIFTRT)
7374                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
7375                 count += first_count;
7376               else
7377                 count -= first_count;
7378
7379               /* If COUNT is positive, the new shift is usually CODE, 
7380                  except for the two exceptions below, in which case it is
7381                  FIRST_CODE.  If the count is negative, FIRST_CODE should
7382                  always be used  */
7383               if (count > 0
7384                   && ((first_code == ROTATE && code == ASHIFT)
7385                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
7386                 code = first_code;
7387               else if (count < 0)
7388                 code = first_code, count = - count;
7389
7390               varop = XEXP (varop, 0);
7391               continue;
7392             }
7393
7394           /* If we have (A << B << C) for any shift, we can convert this to
7395              (A << C << B).  This wins if A is a constant.  Only try this if
7396              B is not a constant.  */
7397
7398           else if (GET_CODE (varop) == code
7399                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
7400                    && 0 != (new
7401                             = simplify_binary_operation (code, mode,
7402                                                          XEXP (varop, 0),
7403                                                          GEN_INT (count))))
7404             {
7405               varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
7406               count = 0;
7407               continue;
7408             }
7409           break;
7410
7411         case NOT:
7412           /* Make this fit the case below.  */
7413           varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
7414                                    GEN_INT (GET_MODE_MASK (mode)));
7415           continue;
7416
7417         case IOR:
7418         case AND:
7419         case XOR:
7420           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
7421              with C the size of VAROP - 1 and the shift is logical if
7422              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
7423              we have an (le X 0) operation.   If we have an arithmetic shift
7424              and STORE_FLAG_VALUE is 1 or we have a logical shift with
7425              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
7426
7427           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
7428               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
7429               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7430               && (code == LSHIFTRT || code == ASHIFTRT)
7431               && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
7432               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
7433             {
7434               count = 0;
7435               varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
7436                                        const0_rtx);
7437
7438               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
7439                 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
7440
7441               continue;
7442             }
7443
7444           /* If we have (shift (logical)), move the logical to the outside
7445              to allow it to possibly combine with another logical and the
7446              shift to combine with another shift.  This also canonicalizes to
7447              what a ZERO_EXTRACT looks like.  Also, some machines have
7448              (and (shift)) insns.  */
7449
7450           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
7451               && (new = simplify_binary_operation (code, result_mode,
7452                                                    XEXP (varop, 1),
7453                                                    GEN_INT (count))) != 0
7454               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
7455                                   INTVAL (new), result_mode, &complement_p))
7456             {
7457               varop = XEXP (varop, 0);
7458               continue;
7459             }
7460
7461           /* If we can't do that, try to simplify the shift in each arm of the
7462              logical expression, make a new logical expression, and apply
7463              the inverse distributive law.  */
7464           {
7465             rtx lhs = simplify_shift_const (NULL_RTX, code, result_mode,
7466                                             XEXP (varop, 0), count);
7467             rtx rhs = simplify_shift_const (NULL_RTX, code, result_mode,
7468                                             XEXP (varop, 1), count);
7469
7470             varop = gen_binary (GET_CODE (varop), result_mode, lhs, rhs);
7471             varop = apply_distributive_law (varop);
7472
7473             count = 0;
7474           }
7475           break;
7476
7477         case EQ:
7478           /* convert (lshift (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
7479              says that the sign bit can be tested, FOO has mode MODE, C is
7480              GET_MODE_BITSIZE (MODE) - 1, and FOO has only the low-order bit
7481              may be nonzero.  */
7482           if (code == LSHIFT
7483               && XEXP (varop, 1) == const0_rtx
7484               && GET_MODE (XEXP (varop, 0)) == result_mode
7485               && count == GET_MODE_BITSIZE (result_mode) - 1
7486               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
7487               && ((STORE_FLAG_VALUE
7488                    & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
7489               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
7490               && merge_outer_ops (&outer_op, &outer_const, XOR,
7491                                   (HOST_WIDE_INT) 1, result_mode,
7492                                   &complement_p))
7493             {
7494               varop = XEXP (varop, 0);
7495               count = 0;
7496               continue;
7497             }
7498           break;
7499
7500         case NEG:
7501           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
7502              than the number of bits in the mode is equivalent to A.  */
7503           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
7504               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
7505             {
7506               varop = XEXP (varop, 0);
7507               count = 0;
7508               continue;
7509             }
7510
7511           /* NEG commutes with ASHIFT since it is multiplication.  Move the
7512              NEG outside to allow shifts to combine.  */
7513           if (code == ASHIFT
7514               && merge_outer_ops (&outer_op, &outer_const, NEG,
7515                                   (HOST_WIDE_INT) 0, result_mode,
7516                                   &complement_p))
7517             {
7518               varop = XEXP (varop, 0);
7519               continue;
7520             }
7521           break;
7522
7523         case PLUS:
7524           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
7525              is one less than the number of bits in the mode is
7526              equivalent to (xor A 1).  */
7527           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
7528               && XEXP (varop, 1) == constm1_rtx
7529               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
7530               && merge_outer_ops (&outer_op, &outer_const, XOR,
7531                                   (HOST_WIDE_INT) 1, result_mode,
7532                                   &complement_p))
7533             {
7534               count = 0;
7535               varop = XEXP (varop, 0);
7536               continue;
7537             }
7538
7539           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
7540              that might be nonzero in BAR are those being shifted out and those
7541              bits are known zero in FOO, we can replace the PLUS with FOO.
7542              Similarly in the other operand order.  This code occurs when
7543              we are computing the size of a variable-size array.  */
7544
7545           if ((code == ASHIFTRT || code == LSHIFTRT)
7546               && count < HOST_BITS_PER_WIDE_INT
7547               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
7548               && (nonzero_bits (XEXP (varop, 1), result_mode)
7549                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
7550             {
7551               varop = XEXP (varop, 0);
7552               continue;
7553             }
7554           else if ((code == ASHIFTRT || code == LSHIFTRT)
7555                    && count < HOST_BITS_PER_WIDE_INT
7556                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
7557                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
7558                             >> count)
7559                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
7560                             & nonzero_bits (XEXP (varop, 1),
7561                                                  result_mode)))
7562             {
7563               varop = XEXP (varop, 1);
7564               continue;
7565             }
7566
7567           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
7568           if (code == ASHIFT
7569               && GET_CODE (XEXP (varop, 1)) == CONST_INT
7570               && (new = simplify_binary_operation (ASHIFT, result_mode,
7571                                                    XEXP (varop, 1),
7572                                                    GEN_INT (count))) != 0
7573               && merge_outer_ops (&outer_op, &outer_const, PLUS,
7574                                   INTVAL (new), result_mode, &complement_p))
7575             {
7576               varop = XEXP (varop, 0);
7577               continue;
7578             }
7579           break;
7580
7581         case MINUS:
7582           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
7583              with C the size of VAROP - 1 and the shift is logical if
7584              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
7585              we have a (gt X 0) operation.  If the shift is arithmetic with
7586              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
7587              we have a (neg (gt X 0)) operation.  */
7588
7589           if (GET_CODE (XEXP (varop, 0)) == ASHIFTRT
7590               && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
7591               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7592               && (code == LSHIFTRT || code == ASHIFTRT)
7593               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
7594               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
7595               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
7596             {
7597               count = 0;
7598               varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
7599                                        const0_rtx);
7600
7601               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
7602                 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
7603
7604               continue;
7605             }
7606           break;
7607         }
7608
7609       break;
7610     }
7611
7612   /* We need to determine what mode to do the shift in.  If the shift is
7613      a ASHIFTRT or ROTATE, we must always do it in the mode it was originally
7614      done in.  Otherwise, we can do it in MODE, the widest mode encountered.
7615      The code we care about is that of the shift that will actually be done,
7616      not the shift that was originally requested.  */
7617   shift_mode = (code == ASHIFTRT || code == ROTATE ? result_mode : mode);
7618
7619   /* We have now finished analyzing the shift.  The result should be
7620      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
7621      OUTER_OP is non-NIL, it is an operation that needs to be applied
7622      to the result of the shift.  OUTER_CONST is the relevant constant,
7623      but we must turn off all bits turned off in the shift.
7624
7625      If we were passed a value for X, see if we can use any pieces of
7626      it.  If not, make new rtx.  */
7627
7628   if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
7629       && GET_CODE (XEXP (x, 1)) == CONST_INT
7630       && INTVAL (XEXP (x, 1)) == count)
7631     const_rtx = XEXP (x, 1);
7632   else
7633     const_rtx = GEN_INT (count);
7634
7635   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7636       && GET_MODE (XEXP (x, 0)) == shift_mode
7637       && SUBREG_REG (XEXP (x, 0)) == varop)
7638     varop = XEXP (x, 0);
7639   else if (GET_MODE (varop) != shift_mode)
7640     varop = gen_lowpart_for_combine (shift_mode, varop);
7641
7642   /* If we can't make the SUBREG, try to return what we were given. */
7643   if (GET_CODE (varop) == CLOBBER)
7644     return x ? x : varop;
7645
7646   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
7647   if (new != 0)
7648     x = new;
7649   else
7650     {
7651       if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
7652         x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
7653
7654       SUBST (XEXP (x, 0), varop);
7655       SUBST (XEXP (x, 1), const_rtx);
7656     }
7657
7658   /* If we have an outer operation and we just made a shift, it is
7659      possible that we could have simplified the shift were it not
7660      for the outer operation.  So try to do the simplification
7661      recursively.  */
7662
7663   if (outer_op != NIL && GET_CODE (x) == code
7664       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7665     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
7666                               INTVAL (XEXP (x, 1)));
7667
7668   /* If we were doing a LSHIFTRT in a wider mode than it was originally,
7669      turn off all the bits that the shift would have turned off.  */
7670   if (orig_code == LSHIFTRT && result_mode != shift_mode)
7671     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
7672                                 GET_MODE_MASK (result_mode) >> orig_count);
7673       
7674   /* Do the remainder of the processing in RESULT_MODE.  */
7675   x = gen_lowpart_for_combine (result_mode, x);
7676
7677   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
7678      operation.  */
7679   if (complement_p)
7680     x = gen_unary (NOT, result_mode, x);
7681
7682   if (outer_op != NIL)
7683     {
7684       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
7685         outer_const &= GET_MODE_MASK (result_mode);
7686
7687       if (outer_op == AND)
7688         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
7689       else if (outer_op == SET)
7690         /* This means that we have determined that the result is
7691            equivalent to a constant.  This should be rare.  */
7692         x = GEN_INT (outer_const);
7693       else if (GET_RTX_CLASS (outer_op) == '1')
7694         x = gen_unary (outer_op, result_mode, x);
7695       else
7696         x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
7697     }
7698
7699   return x;
7700 }  
7701 \f
7702 /* Like recog, but we receive the address of a pointer to a new pattern.
7703    We try to match the rtx that the pointer points to.
7704    If that fails, we may try to modify or replace the pattern,
7705    storing the replacement into the same pointer object.
7706
7707    Modifications include deletion or addition of CLOBBERs.
7708
7709    PNOTES is a pointer to a location where any REG_UNUSED notes added for
7710    the CLOBBERs are placed.
7711
7712    The value is the final insn code from the pattern ultimately matched,
7713    or -1.  */
7714
7715 static int
7716 recog_for_combine (pnewpat, insn, pnotes)
7717      rtx *pnewpat;
7718      rtx insn;
7719      rtx *pnotes;
7720 {
7721   register rtx pat = *pnewpat;
7722   int insn_code_number;
7723   int num_clobbers_to_add = 0;
7724   int i;
7725   rtx notes = 0;
7726
7727   /* Is the result of combination a valid instruction?  */
7728   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
7729
7730   /* If it isn't, there is the possibility that we previously had an insn
7731      that clobbered some register as a side effect, but the combined
7732      insn doesn't need to do that.  So try once more without the clobbers
7733      unless this represents an ASM insn.  */
7734
7735   if (insn_code_number < 0 && ! check_asm_operands (pat)
7736       && GET_CODE (pat) == PARALLEL)
7737     {
7738       int pos;
7739
7740       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
7741         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
7742           {
7743             if (i != pos)
7744               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
7745             pos++;
7746           }
7747
7748       SUBST_INT (XVECLEN (pat, 0), pos);
7749
7750       if (pos == 1)
7751         pat = XVECEXP (pat, 0, 0);
7752
7753       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
7754     }
7755
7756   /* If we had any clobbers to add, make a new pattern than contains
7757      them.  Then check to make sure that all of them are dead.  */
7758   if (num_clobbers_to_add)
7759     {
7760       rtx newpat = gen_rtx (PARALLEL, VOIDmode,
7761                             gen_rtvec (GET_CODE (pat) == PARALLEL
7762                                        ? XVECLEN (pat, 0) + num_clobbers_to_add
7763                                        : num_clobbers_to_add + 1));
7764
7765       if (GET_CODE (pat) == PARALLEL)
7766         for (i = 0; i < XVECLEN (pat, 0); i++)
7767           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
7768       else
7769         XVECEXP (newpat, 0, 0) = pat;
7770
7771       add_clobbers (newpat, insn_code_number);
7772
7773       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
7774            i < XVECLEN (newpat, 0); i++)
7775         {
7776           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
7777               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
7778             return -1;
7779           notes = gen_rtx (EXPR_LIST, REG_UNUSED,
7780                            XEXP (XVECEXP (newpat, 0, i), 0), notes);
7781         }
7782       pat = newpat;
7783     }
7784
7785   *pnewpat = pat;
7786   *pnotes = notes;
7787
7788   return insn_code_number;
7789 }
7790 \f
7791 /* Like gen_lowpart but for use by combine.  In combine it is not possible
7792    to create any new pseudoregs.  However, it is safe to create
7793    invalid memory addresses, because combine will try to recognize
7794    them and all they will do is make the combine attempt fail.
7795
7796    If for some reason this cannot do its job, an rtx
7797    (clobber (const_int 0)) is returned.
7798    An insn containing that will not be recognized.  */
7799
7800 #undef gen_lowpart
7801
7802 static rtx
7803 gen_lowpart_for_combine (mode, x)
7804      enum machine_mode mode;
7805      register rtx x;
7806 {
7807   rtx result;
7808
7809   if (GET_MODE (x) == mode)
7810     return x;
7811
7812   /* We can only support MODE being wider than a word if X is a
7813      constant integer or has a mode the same size.  */
7814
7815   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
7816       && ! ((GET_MODE (x) == VOIDmode
7817              && (GET_CODE (x) == CONST_INT
7818                  || GET_CODE (x) == CONST_DOUBLE))
7819             || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
7820     return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
7821
7822   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
7823      won't know what to do.  So we will strip off the SUBREG here and
7824      process normally.  */
7825   if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
7826     {
7827       x = SUBREG_REG (x);
7828       if (GET_MODE (x) == mode)
7829         return x;
7830     }
7831
7832   result = gen_lowpart_common (mode, x);
7833   if (result)
7834     return result;
7835
7836   if (GET_CODE (x) == MEM)
7837     {
7838       register int offset = 0;
7839       rtx new;
7840
7841       /* Refuse to work on a volatile memory ref or one with a mode-dependent
7842          address.  */
7843       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
7844         return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
7845
7846       /* If we want to refer to something bigger than the original memref,
7847          generate a perverse subreg instead.  That will force a reload
7848          of the original memref X.  */
7849       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
7850         return gen_rtx (SUBREG, mode, x, 0);
7851
7852 #if WORDS_BIG_ENDIAN
7853       offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
7854                 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
7855 #endif
7856 #if BYTES_BIG_ENDIAN
7857       /* Adjust the address so that the address-after-the-data
7858          is unchanged.  */
7859       offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
7860                  - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
7861 #endif
7862       new = gen_rtx (MEM, mode, plus_constant (XEXP (x, 0), offset));
7863       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
7864       MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
7865       MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
7866       return new;
7867     }
7868
7869   /* If X is a comparison operator, rewrite it in a new mode.  This
7870      probably won't match, but may allow further simplifications.  */
7871   else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
7872     return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
7873
7874   /* If we couldn't simplify X any other way, just enclose it in a
7875      SUBREG.  Normally, this SUBREG won't match, but some patterns may
7876      include an explicit SUBREG or we may simplify it further in combine.  */
7877   else
7878     {
7879       int word = 0;
7880
7881       if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
7882         word = ((GET_MODE_SIZE (GET_MODE (x))
7883                  - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
7884                 / UNITS_PER_WORD);
7885       return gen_rtx (SUBREG, mode, x, word);
7886     }
7887 }
7888 \f
7889 /* Make an rtx expression.  This is a subset of gen_rtx and only supports
7890    expressions of 1, 2, or 3 operands, each of which are rtx expressions.
7891
7892    If the identical expression was previously in the insn (in the undobuf),
7893    it will be returned.  Only if it is not found will a new expression
7894    be made.  */
7895
7896 /*VARARGS2*/
7897 static rtx
7898 gen_rtx_combine (va_alist)
7899      va_dcl
7900 {
7901   va_list p;
7902   enum rtx_code code;
7903   enum machine_mode mode;
7904   int n_args;
7905   rtx args[3];
7906   int i, j;
7907   char *fmt;
7908   rtx rt;
7909
7910   va_start (p);
7911   code = va_arg (p, enum rtx_code);
7912   mode = va_arg (p, enum machine_mode);
7913   n_args = GET_RTX_LENGTH (code);
7914   fmt = GET_RTX_FORMAT (code);
7915
7916   if (n_args == 0 || n_args > 3)
7917     abort ();
7918
7919   /* Get each arg and verify that it is supposed to be an expression.  */
7920   for (j = 0; j < n_args; j++)
7921     {
7922       if (*fmt++ != 'e')
7923         abort ();
7924
7925       args[j] = va_arg (p, rtx);
7926     }
7927
7928   /* See if this is in undobuf.  Be sure we don't use objects that came
7929      from another insn; this could produce circular rtl structures.  */
7930
7931   for (i = previous_num_undos; i < undobuf.num_undo; i++)
7932     if (!undobuf.undo[i].is_int
7933         && GET_CODE (undobuf.undo[i].old_contents.rtx) == code
7934         && GET_MODE (undobuf.undo[i].old_contents.rtx) == mode)
7935       {
7936         for (j = 0; j < n_args; j++)
7937           if (XEXP (undobuf.undo[i].old_contents.rtx, j) != args[j])
7938             break;
7939
7940         if (j == n_args)
7941           return undobuf.undo[i].old_contents.rtx;
7942       }
7943
7944   /* Otherwise make a new rtx.  We know we have 1, 2, or 3 args.
7945      Use rtx_alloc instead of gen_rtx because it's faster on RISC.  */
7946   rt = rtx_alloc (code);
7947   PUT_MODE (rt, mode);
7948   XEXP (rt, 0) = args[0];
7949   if (n_args > 1)
7950     {
7951       XEXP (rt, 1) = args[1];
7952       if (n_args > 2)
7953         XEXP (rt, 2) = args[2];
7954     }
7955   return rt;
7956 }
7957
7958 /* These routines make binary and unary operations by first seeing if they
7959    fold; if not, a new expression is allocated.  */
7960
7961 static rtx
7962 gen_binary (code, mode, op0, op1)
7963      enum rtx_code code;
7964      enum machine_mode mode;
7965      rtx op0, op1;
7966 {
7967   rtx result;
7968   rtx tem;
7969
7970   if (GET_RTX_CLASS (code) == 'c'
7971       && (GET_CODE (op0) == CONST_INT
7972           || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
7973     tem = op0, op0 = op1, op1 = tem;
7974
7975   if (GET_RTX_CLASS (code) == '<') 
7976     {
7977       enum machine_mode op_mode = GET_MODE (op0);
7978       if (op_mode == VOIDmode)
7979         op_mode = GET_MODE (op1);
7980       result = simplify_relational_operation (code, op_mode, op0, op1);
7981     }
7982   else
7983     result = simplify_binary_operation (code, mode, op0, op1);
7984
7985   if (result)
7986     return result;
7987
7988   /* Put complex operands first and constants second.  */
7989   if (GET_RTX_CLASS (code) == 'c'
7990       && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
7991           || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
7992               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
7993           || (GET_CODE (op0) == SUBREG
7994               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
7995               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
7996     return gen_rtx_combine (code, mode, op1, op0);
7997
7998   return gen_rtx_combine (code, mode, op0, op1);
7999 }
8000
8001 static rtx
8002 gen_unary (code, mode, op0)
8003      enum rtx_code code;
8004      enum machine_mode mode;
8005      rtx op0;
8006 {
8007   rtx result = simplify_unary_operation (code, mode, op0, mode);
8008
8009   if (result)
8010     return result;
8011
8012   return gen_rtx_combine (code, mode, op0);
8013 }
8014 \f
8015 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
8016    comparison code that will be tested.
8017
8018    The result is a possibly different comparison code to use.  *POP0 and
8019    *POP1 may be updated.
8020
8021    It is possible that we might detect that a comparison is either always
8022    true or always false.  However, we do not perform general constant
8023    folding in combine, so this knowledge isn't useful.  Such tautologies
8024    should have been detected earlier.  Hence we ignore all such cases.  */
8025
8026 static enum rtx_code
8027 simplify_comparison (code, pop0, pop1)
8028      enum rtx_code code;
8029      rtx *pop0;
8030      rtx *pop1;
8031 {
8032   rtx op0 = *pop0;
8033   rtx op1 = *pop1;
8034   rtx tem, tem1;
8035   int i;
8036   enum machine_mode mode, tmode;
8037
8038   /* Try a few ways of applying the same transformation to both operands.  */
8039   while (1)
8040     {
8041       /* If both operands are the same constant shift, see if we can ignore the
8042          shift.  We can if the shift is a rotate or if the bits shifted out of
8043          this shift are known to be zero for both inputs and if the type of
8044          comparison is compatible with the shift.  */
8045       if (GET_CODE (op0) == GET_CODE (op1)
8046           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
8047           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
8048               || ((GET_CODE (op0) == LSHIFTRT
8049                    || GET_CODE (op0) == ASHIFT || GET_CODE (op0) == LSHIFT)
8050                   && (code != GT && code != LT && code != GE && code != LE))
8051               || (GET_CODE (op0) == ASHIFTRT
8052                   && (code != GTU && code != LTU
8053                       && code != GEU && code != GEU)))
8054           && GET_CODE (XEXP (op0, 1)) == CONST_INT
8055           && INTVAL (XEXP (op0, 1)) >= 0
8056           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
8057           && XEXP (op0, 1) == XEXP (op1, 1))
8058         {
8059           enum machine_mode mode = GET_MODE (op0);
8060           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8061           int shift_count = INTVAL (XEXP (op0, 1));
8062
8063           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
8064             mask &= (mask >> shift_count) << shift_count;
8065           else if (GET_CODE (op0) == ASHIFT || GET_CODE (op0) == LSHIFT)
8066             mask = (mask & (mask << shift_count)) >> shift_count;
8067
8068           if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
8069               && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
8070             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
8071           else
8072             break;
8073         }
8074
8075       /* If both operands are AND's of a paradoxical SUBREG by constant, the
8076          SUBREGs are of the same mode, and, in both cases, the AND would
8077          be redundant if the comparison was done in the narrower mode,
8078          do the comparison in the narrower mode (e.g., we are AND'ing with 1
8079          and the operand's possibly nonzero bits are 0xffffff01; in that case
8080          if we only care about QImode, we don't need the AND).  This case
8081          occurs if the output mode of an scc insn is not SImode and
8082          STORE_FLAG_VALUE == 1 (e.g., the 386).  */
8083
8084       else if  (GET_CODE (op0) == AND && GET_CODE (op1) == AND
8085                 && GET_CODE (XEXP (op0, 1)) == CONST_INT
8086                 && GET_CODE (XEXP (op1, 1)) == CONST_INT
8087                 && GET_CODE (XEXP (op0, 0)) == SUBREG
8088                 && GET_CODE (XEXP (op1, 0)) == SUBREG
8089                 && (GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)))
8090                     > GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0)))))
8091                 && (GET_MODE (SUBREG_REG (XEXP (op0, 0)))
8092                     == GET_MODE (SUBREG_REG (XEXP (op1, 0))))
8093                 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
8094                     <= HOST_BITS_PER_WIDE_INT)
8095                 && (nonzero_bits (SUBREG_REG (XEXP (op0, 0)),
8096                                       GET_MODE (SUBREG_REG (XEXP (op0, 0))))
8097                     & ~ INTVAL (XEXP (op0, 1))) == 0
8098                 && (nonzero_bits (SUBREG_REG (XEXP (op1, 0)),
8099                                       GET_MODE (SUBREG_REG (XEXP (op1, 0))))
8100                     & ~ INTVAL (XEXP (op1, 1))) == 0)
8101         {
8102           op0 = SUBREG_REG (XEXP (op0, 0));
8103           op1 = SUBREG_REG (XEXP (op1, 0));
8104
8105           /* the resulting comparison is always unsigned since we masked off
8106              the original sign bit. */
8107           code = unsigned_condition (code);
8108         }
8109       else
8110         break;
8111     }
8112      
8113   /* If the first operand is a constant, swap the operands and adjust the
8114      comparison code appropriately.  */
8115   if (CONSTANT_P (op0))
8116     {
8117       tem = op0, op0 = op1, op1 = tem;
8118       code = swap_condition (code);
8119     }
8120
8121   /* We now enter a loop during which we will try to simplify the comparison.
8122      For the most part, we only are concerned with comparisons with zero,
8123      but some things may really be comparisons with zero but not start
8124      out looking that way.  */
8125
8126   while (GET_CODE (op1) == CONST_INT)
8127     {
8128       enum machine_mode mode = GET_MODE (op0);
8129       int mode_width = GET_MODE_BITSIZE (mode);
8130       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8131       int equality_comparison_p;
8132       int sign_bit_comparison_p;
8133       int unsigned_comparison_p;
8134       HOST_WIDE_INT const_op;
8135
8136       /* We only want to handle integral modes.  This catches VOIDmode,
8137          CCmode, and the floating-point modes.  An exception is that we
8138          can handle VOIDmode if OP0 is a COMPARE or a comparison
8139          operation.  */
8140
8141       if (GET_MODE_CLASS (mode) != MODE_INT
8142           && ! (mode == VOIDmode
8143                 && (GET_CODE (op0) == COMPARE
8144                     || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
8145         break;
8146
8147       /* Get the constant we are comparing against and turn off all bits
8148          not on in our mode.  */
8149       const_op = INTVAL (op1);
8150       if (mode_width <= HOST_BITS_PER_WIDE_INT)
8151         const_op &= mask;
8152
8153       /* If we are comparing against a constant power of two and the value
8154          being compared can only have that single bit nonzero (e.g., it was
8155          `and'ed with that bit), we can replace this with a comparison
8156          with zero.  */
8157       if (const_op
8158           && (code == EQ || code == NE || code == GE || code == GEU
8159               || code == LT || code == LTU)
8160           && mode_width <= HOST_BITS_PER_WIDE_INT
8161           && exact_log2 (const_op) >= 0
8162           && nonzero_bits (op0, mode) == const_op)
8163         {
8164           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
8165           op1 = const0_rtx, const_op = 0;
8166         }
8167
8168       /* Similarly, if we are comparing a value known to be either -1 or
8169          0 with -1, change it to the opposite comparison against zero.  */
8170
8171       if (const_op == -1
8172           && (code == EQ || code == NE || code == GT || code == LE
8173               || code == GEU || code == LTU)
8174           && num_sign_bit_copies (op0, mode) == mode_width)
8175         {
8176           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
8177           op1 = const0_rtx, const_op = 0;
8178         }
8179
8180       /* Do some canonicalizations based on the comparison code.  We prefer
8181          comparisons against zero and then prefer equality comparisons.  
8182          If we can reduce the size of a constant, we will do that too.  */
8183
8184       switch (code)
8185         {
8186         case LT:
8187           /* < C is equivalent to <= (C - 1) */
8188           if (const_op > 0)
8189             {
8190               const_op -= 1;
8191               op1 = GEN_INT (const_op);
8192               code = LE;
8193               /* ... fall through to LE case below.  */
8194             }
8195           else
8196             break;
8197
8198         case LE:
8199           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
8200           if (const_op < 0)
8201             {
8202               const_op += 1;
8203               op1 = GEN_INT (const_op);
8204               code = LT;
8205             }
8206
8207           /* If we are doing a <= 0 comparison on a value known to have
8208              a zero sign bit, we can replace this with == 0.  */
8209           else if (const_op == 0
8210                    && mode_width <= HOST_BITS_PER_WIDE_INT
8211                    && (nonzero_bits (op0, mode)
8212                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
8213             code = EQ;
8214           break;
8215
8216         case GE:
8217           /* >= C is equivalent to > (C - 1). */
8218           if (const_op > 0)
8219             {
8220               const_op -= 1;
8221               op1 = GEN_INT (const_op);
8222               code = GT;
8223               /* ... fall through to GT below.  */
8224             }
8225           else
8226             break;
8227
8228         case GT:
8229           /* > C is equivalent to >= (C + 1); we do this for C < 0*/
8230           if (const_op < 0)
8231             {
8232               const_op += 1;
8233               op1 = GEN_INT (const_op);
8234               code = GE;
8235             }
8236
8237           /* If we are doing a > 0 comparison on a value known to have
8238              a zero sign bit, we can replace this with != 0.  */
8239           else if (const_op == 0
8240                    && mode_width <= HOST_BITS_PER_WIDE_INT
8241                    && (nonzero_bits (op0, mode)
8242                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
8243             code = NE;
8244           break;
8245
8246         case LTU:
8247           /* < C is equivalent to <= (C - 1).  */
8248           if (const_op > 0)
8249             {
8250               const_op -= 1;
8251               op1 = GEN_INT (const_op);
8252               code = LEU;
8253               /* ... fall through ... */
8254             }
8255
8256           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
8257           else if (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1))
8258             {
8259               const_op = 0, op1 = const0_rtx;
8260               code = GE;
8261               break;
8262             }
8263           else
8264             break;
8265
8266         case LEU:
8267           /* unsigned <= 0 is equivalent to == 0 */
8268           if (const_op == 0)
8269             code = EQ;
8270
8271           /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
8272           else if (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
8273             {
8274               const_op = 0, op1 = const0_rtx;
8275               code = GE;
8276             }
8277           break;
8278
8279         case GEU:
8280           /* >= C is equivalent to < (C - 1).  */
8281           if (const_op > 1)
8282             {
8283               const_op -= 1;
8284               op1 = GEN_INT (const_op);
8285               code = GTU;
8286               /* ... fall through ... */
8287             }
8288
8289           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
8290           else if (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1))
8291             {
8292               const_op = 0, op1 = const0_rtx;
8293               code = LT;
8294             }
8295           else
8296             break;
8297
8298         case GTU:
8299           /* unsigned > 0 is equivalent to != 0 */
8300           if (const_op == 0)
8301             code = NE;
8302
8303           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
8304           else if (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
8305             {
8306               const_op = 0, op1 = const0_rtx;
8307               code = LT;
8308             }
8309           break;
8310         }
8311
8312       /* Compute some predicates to simplify code below.  */
8313
8314       equality_comparison_p = (code == EQ || code == NE);
8315       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
8316       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
8317                                || code == LEU);
8318
8319       /* Now try cases based on the opcode of OP0.  If none of the cases
8320          does a "continue", we exit this loop immediately after the
8321          switch.  */
8322
8323       switch (GET_CODE (op0))
8324         {
8325         case ZERO_EXTRACT:
8326           /* If we are extracting a single bit from a variable position in
8327              a constant that has only a single bit set and are comparing it
8328              with zero, we can convert this into an equality comparison 
8329              between the position and the location of the single bit.  We can't
8330              do this if bit endian and we don't have an extzv since we then
8331              can't know what mode to use for the endianness adjustment.  */
8332
8333 #if ! BITS_BIG_ENDIAN || defined (HAVE_extzv)
8334           if (GET_CODE (XEXP (op0, 0)) == CONST_INT
8335               && XEXP (op0, 1) == const1_rtx
8336               && equality_comparison_p && const_op == 0
8337               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
8338             {
8339 #if BITS_BIG_ENDIAN
8340               i = (GET_MODE_BITSIZE
8341                    (insn_operand_mode[(int) CODE_FOR_extzv][1]) - 1 - i);
8342 #endif
8343
8344               op0 = XEXP (op0, 2);
8345               op1 = GEN_INT (i);
8346               const_op = i;
8347
8348               /* Result is nonzero iff shift count is equal to I.  */
8349               code = reverse_condition (code);
8350               continue;
8351             }
8352 #endif
8353
8354           /* ... fall through ... */
8355
8356         case SIGN_EXTRACT:
8357           tem = expand_compound_operation (op0);
8358           if (tem != op0)
8359             {
8360               op0 = tem;
8361               continue;
8362             }
8363           break;
8364
8365         case NOT:
8366           /* If testing for equality, we can take the NOT of the constant.  */
8367           if (equality_comparison_p
8368               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
8369             {
8370               op0 = XEXP (op0, 0);
8371               op1 = tem;
8372               continue;
8373             }
8374
8375           /* If just looking at the sign bit, reverse the sense of the
8376              comparison.  */
8377           if (sign_bit_comparison_p)
8378             {
8379               op0 = XEXP (op0, 0);
8380               code = (code == GE ? LT : GE);
8381               continue;
8382             }
8383           break;
8384
8385         case NEG:
8386           /* If testing for equality, we can take the NEG of the constant.  */
8387           if (equality_comparison_p
8388               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
8389             {
8390               op0 = XEXP (op0, 0);
8391               op1 = tem;
8392               continue;
8393             }
8394
8395           /* The remaining cases only apply to comparisons with zero.  */
8396           if (const_op != 0)
8397             break;
8398
8399           /* When X is ABS or is known positive,
8400              (neg X) is < 0 if and only if X != 0.  */
8401
8402           if (sign_bit_comparison_p
8403               && (GET_CODE (XEXP (op0, 0)) == ABS
8404                   || (mode_width <= HOST_BITS_PER_WIDE_INT
8405                       && (nonzero_bits (XEXP (op0, 0), mode)
8406                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
8407             {
8408               op0 = XEXP (op0, 0);
8409               code = (code == LT ? NE : EQ);
8410               continue;
8411             }
8412
8413           /* If we have NEG of something whose two high-order bits are the
8414              same, we know that "(-a) < 0" is equivalent to "a > 0". */
8415           if (num_sign_bit_copies (op0, mode) >= 2)
8416             {
8417               op0 = XEXP (op0, 0);
8418               code = swap_condition (code);
8419               continue;
8420             }
8421           break;
8422
8423         case ROTATE:
8424           /* If we are testing equality and our count is a constant, we
8425              can perform the inverse operation on our RHS.  */
8426           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
8427               && (tem = simplify_binary_operation (ROTATERT, mode,
8428                                                    op1, XEXP (op0, 1))) != 0)
8429             {
8430               op0 = XEXP (op0, 0);
8431               op1 = tem;
8432               continue;
8433             }
8434
8435           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
8436              a particular bit.  Convert it to an AND of a constant of that
8437              bit.  This will be converted into a ZERO_EXTRACT.  */
8438           if (const_op == 0 && sign_bit_comparison_p
8439               && GET_CODE (XEXP (op0, 1)) == CONST_INT
8440               && mode_width <= HOST_BITS_PER_WIDE_INT)
8441             {
8442               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
8443                                             ((HOST_WIDE_INT) 1
8444                                              << (mode_width - 1
8445                                                  - INTVAL (XEXP (op0, 1)))));
8446               code = (code == LT ? NE : EQ);
8447               continue;
8448             }
8449
8450           /* ... fall through ... */
8451
8452         case ABS:
8453           /* ABS is ignorable inside an equality comparison with zero.  */
8454           if (const_op == 0 && equality_comparison_p)
8455             {
8456               op0 = XEXP (op0, 0);
8457               continue;
8458             }
8459           break;
8460           
8461
8462         case SIGN_EXTEND:
8463           /* Can simplify (compare (zero/sign_extend FOO) CONST)
8464              to (compare FOO CONST) if CONST fits in FOO's mode and we 
8465              are either testing inequality or have an unsigned comparison
8466              with ZERO_EXTEND or a signed comparison with SIGN_EXTEND.  */
8467           if (! unsigned_comparison_p
8468               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
8469                   <= HOST_BITS_PER_WIDE_INT)
8470               && ((unsigned HOST_WIDE_INT) const_op
8471                   < (((HOST_WIDE_INT) 1
8472                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
8473             {
8474               op0 = XEXP (op0, 0);
8475               continue;
8476             }
8477           break;
8478
8479         case SUBREG:
8480           /* Check for the case where we are comparing A - C1 with C2,
8481              both constants are smaller than 1/2 the maxium positive
8482              value in MODE, and the comparison is equality or unsigned.
8483              In that case, if A is either zero-extended to MODE or has
8484              sufficient sign bits so that the high-order bit in MODE
8485              is a copy of the sign in the inner mode, we can prove that it is
8486              safe to do the operation in the wider mode.  This simplifies
8487              many range checks.  */
8488
8489           if (mode_width <= HOST_BITS_PER_WIDE_INT
8490               && subreg_lowpart_p (op0)
8491               && GET_CODE (SUBREG_REG (op0)) == PLUS
8492               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
8493               && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
8494               && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
8495                   < GET_MODE_MASK (mode) / 2)
8496               && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
8497               && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
8498                                       GET_MODE (SUBREG_REG (op0)))
8499                         & ~ GET_MODE_MASK (mode))
8500                   || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
8501                                            GET_MODE (SUBREG_REG (op0)))
8502                       > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
8503                          - GET_MODE_BITSIZE (mode)))))
8504             {
8505               op0 = SUBREG_REG (op0);
8506               continue;
8507             }
8508
8509           /* If the inner mode is narrower and we are extracting the low part,
8510              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
8511           if (subreg_lowpart_p (op0)
8512               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
8513             /* Fall through */ ;
8514           else
8515             break;
8516
8517           /* ... fall through ... */
8518
8519         case ZERO_EXTEND:
8520           if ((unsigned_comparison_p || equality_comparison_p)
8521               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
8522                   <= HOST_BITS_PER_WIDE_INT)
8523               && ((unsigned HOST_WIDE_INT) const_op
8524                   < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
8525             {
8526               op0 = XEXP (op0, 0);
8527               continue;
8528             }
8529           break;
8530
8531         case PLUS:
8532           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
8533              this for equality comparisons due to pathological cases involving
8534              overflows.  */
8535           if (equality_comparison_p
8536               && 0 != (tem = simplify_binary_operation (MINUS, mode,
8537                                                         op1, XEXP (op0, 1))))
8538             {
8539               op0 = XEXP (op0, 0);
8540               op1 = tem;
8541               continue;
8542             }
8543
8544           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
8545           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
8546               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
8547             {
8548               op0 = XEXP (XEXP (op0, 0), 0);
8549               code = (code == LT ? EQ : NE);
8550               continue;
8551             }
8552           break;
8553
8554         case MINUS:
8555           /* (eq (minus A B) C) -> (eq A (plus B C)) or
8556              (eq B (minus A C)), whichever simplifies.  We can only do
8557              this for equality comparisons due to pathological cases involving
8558              overflows.  */
8559           if (equality_comparison_p
8560               && 0 != (tem = simplify_binary_operation (PLUS, mode,
8561                                                         XEXP (op0, 1), op1)))
8562             {
8563               op0 = XEXP (op0, 0);
8564               op1 = tem;
8565               continue;
8566             }
8567
8568           if (equality_comparison_p
8569               && 0 != (tem = simplify_binary_operation (MINUS, mode,
8570                                                         XEXP (op0, 0), op1)))
8571             {
8572               op0 = XEXP (op0, 1);
8573               op1 = tem;
8574               continue;
8575             }
8576
8577           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
8578              of bits in X minus 1, is one iff X > 0.  */
8579           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
8580               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
8581               && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
8582               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
8583             {
8584               op0 = XEXP (op0, 1);
8585               code = (code == GE ? LE : GT);
8586               continue;
8587             }
8588           break;
8589
8590         case XOR:
8591           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
8592              if C is zero or B is a constant.  */
8593           if (equality_comparison_p
8594               && 0 != (tem = simplify_binary_operation (XOR, mode,
8595                                                         XEXP (op0, 1), op1)))
8596             {
8597               op0 = XEXP (op0, 0);
8598               op1 = tem;
8599               continue;
8600             }
8601           break;
8602
8603         case EQ:  case NE:
8604         case LT:  case LTU:  case LE:  case LEU:
8605         case GT:  case GTU:  case GE:  case GEU:
8606           /* We can't do anything if OP0 is a condition code value, rather
8607              than an actual data value.  */
8608           if (const_op != 0
8609 #ifdef HAVE_cc0
8610               || XEXP (op0, 0) == cc0_rtx
8611 #endif
8612               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
8613             break;
8614
8615           /* Get the two operands being compared.  */
8616           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
8617             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
8618           else
8619             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
8620
8621           /* Check for the cases where we simply want the result of the
8622              earlier test or the opposite of that result.  */
8623           if (code == NE
8624               || (code == EQ && reversible_comparison_p (op0))
8625               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
8626                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
8627                   && (STORE_FLAG_VALUE
8628                       & (((HOST_WIDE_INT) 1
8629                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
8630                   && (code == LT
8631                       || (code == GE && reversible_comparison_p (op0)))))
8632             {
8633               code = (code == LT || code == NE
8634                       ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
8635               op0 = tem, op1 = tem1;
8636               continue;
8637             }
8638           break;
8639
8640         case IOR:
8641           /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
8642              iff X <= 0.  */
8643           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
8644               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
8645               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
8646             {
8647               op0 = XEXP (op0, 1);
8648               code = (code == GE ? GT : LE);
8649               continue;
8650             }
8651           break;
8652
8653         case AND:
8654           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
8655              will be converted to a ZERO_EXTRACT later.  */
8656           if (const_op == 0 && equality_comparison_p
8657               && (GET_CODE (XEXP (op0, 0)) == ASHIFT
8658                   || GET_CODE (XEXP (op0, 0)) == LSHIFT)
8659               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
8660             {
8661               op0 = simplify_and_const_int
8662                 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
8663                                              XEXP (op0, 1),
8664                                              XEXP (XEXP (op0, 0), 1)),
8665                  (HOST_WIDE_INT) 1);
8666               continue;
8667             }
8668
8669           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
8670              zero and X is a comparison and C1 and C2 describe only bits set
8671              in STORE_FLAG_VALUE, we can compare with X.  */
8672           if (const_op == 0 && equality_comparison_p
8673               && mode_width <= HOST_BITS_PER_WIDE_INT
8674               && GET_CODE (XEXP (op0, 1)) == CONST_INT
8675               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
8676               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
8677               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
8678               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8679             {
8680               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
8681                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
8682               if ((~ STORE_FLAG_VALUE & mask) == 0
8683                   && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
8684                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
8685                           && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
8686                 {
8687                   op0 = XEXP (XEXP (op0, 0), 0);
8688                   continue;
8689                 }
8690             }
8691
8692           /* If we are doing an equality comparison of an AND of a bit equal
8693              to the sign bit, replace this with a LT or GE comparison of
8694              the underlying value.  */
8695           if (equality_comparison_p
8696               && const_op == 0
8697               && GET_CODE (XEXP (op0, 1)) == CONST_INT
8698               && mode_width <= HOST_BITS_PER_WIDE_INT
8699               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
8700                   == (HOST_WIDE_INT) 1 << (mode_width - 1)))
8701             {
8702               op0 = XEXP (op0, 0);
8703               code = (code == EQ ? GE : LT);
8704               continue;
8705             }
8706
8707           /* If this AND operation is really a ZERO_EXTEND from a narrower
8708              mode, the constant fits within that mode, and this is either an
8709              equality or unsigned comparison, try to do this comparison in
8710              the narrower mode.  */
8711           if ((equality_comparison_p || unsigned_comparison_p)
8712               && GET_CODE (XEXP (op0, 1)) == CONST_INT
8713               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
8714                                    & GET_MODE_MASK (mode))
8715                                   + 1)) >= 0
8716               && const_op >> i == 0
8717               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
8718             {
8719               op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
8720               continue;
8721             }
8722           break;
8723
8724         case ASHIFT:
8725         case LSHIFT:
8726           /* If we have (compare (xshift FOO N) (const_int C)) and
8727              the high order N bits of FOO (N+1 if an inequality comparison)
8728              are known to be zero, we can do this by comparing FOO with C
8729              shifted right N bits so long as the low-order N bits of C are
8730              zero.  */
8731           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
8732               && INTVAL (XEXP (op0, 1)) >= 0
8733               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
8734                   < HOST_BITS_PER_WIDE_INT)
8735               && ((const_op
8736                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
8737               && mode_width <= HOST_BITS_PER_WIDE_INT
8738               && (nonzero_bits (XEXP (op0, 0), mode)
8739                   & ~ (mask >> (INTVAL (XEXP (op0, 1))
8740                                 + ! equality_comparison_p))) == 0)
8741             {
8742               const_op >>= INTVAL (XEXP (op0, 1));
8743               op1 = GEN_INT (const_op);
8744               op0 = XEXP (op0, 0);
8745               continue;
8746             }
8747
8748           /* If we are doing a sign bit comparison, it means we are testing
8749              a particular bit.  Convert it to the appropriate AND.  */
8750           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
8751               && mode_width <= HOST_BITS_PER_WIDE_INT)
8752             {
8753               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
8754                                             ((HOST_WIDE_INT) 1
8755                                              << (mode_width - 1
8756                                                  - INTVAL (XEXP (op0, 1)))));
8757               code = (code == LT ? NE : EQ);
8758               continue;
8759             }
8760
8761           /* If this an equality comparison with zero and we are shifting
8762              the low bit to the sign bit, we can convert this to an AND of the
8763              low-order bit.  */
8764           if (const_op == 0 && equality_comparison_p
8765               && GET_CODE (XEXP (op0, 1)) == CONST_INT
8766               && INTVAL (XEXP (op0, 1)) == mode_width - 1)
8767             {
8768               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
8769                                             (HOST_WIDE_INT) 1);
8770               continue;
8771             }
8772           break;
8773
8774         case ASHIFTRT:
8775           /* If this is an equality comparison with zero, we can do this
8776              as a logical shift, which might be much simpler.  */
8777           if (equality_comparison_p && const_op == 0
8778               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
8779             {
8780               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
8781                                           XEXP (op0, 0),
8782                                           INTVAL (XEXP (op0, 1)));
8783               continue;
8784             }
8785
8786           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
8787              do the comparison in a narrower mode.  */
8788           if (! unsigned_comparison_p
8789               && GET_CODE (XEXP (op0, 1)) == CONST_INT
8790               && GET_CODE (XEXP (op0, 0)) == ASHIFT
8791               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
8792               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
8793                                          MODE_INT, 1)) != BLKmode
8794               && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
8795                   || ((unsigned HOST_WIDE_INT) - const_op
8796                       <= GET_MODE_MASK (tmode))))
8797             {
8798               op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
8799               continue;
8800             }
8801
8802           /* ... fall through ... */
8803         case LSHIFTRT:
8804           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
8805              the low order N bits of FOO are known to be zero, we can do this
8806              by comparing FOO with C shifted left N bits so long as no
8807              overflow occurs.  */
8808           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
8809               && INTVAL (XEXP (op0, 1)) >= 0
8810               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
8811               && mode_width <= HOST_BITS_PER_WIDE_INT
8812               && (nonzero_bits (XEXP (op0, 0), mode)
8813                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
8814               && (const_op == 0
8815                   || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
8816                       < mode_width)))
8817             {
8818               const_op <<= INTVAL (XEXP (op0, 1));
8819               op1 = GEN_INT (const_op);
8820               op0 = XEXP (op0, 0);
8821               continue;
8822             }
8823
8824           /* If we are using this shift to extract just the sign bit, we
8825              can replace this with an LT or GE comparison.  */
8826           if (const_op == 0
8827               && (equality_comparison_p || sign_bit_comparison_p)
8828               && GET_CODE (XEXP (op0, 1)) == CONST_INT
8829               && INTVAL (XEXP (op0, 1)) == mode_width - 1)
8830             {
8831               op0 = XEXP (op0, 0);
8832               code = (code == NE || code == GT ? LT : GE);
8833               continue;
8834             }
8835           break;
8836         }
8837
8838       break;
8839     }
8840
8841   /* Now make any compound operations involved in this comparison.  Then,
8842      check for an outmost SUBREG on OP0 that isn't doing anything or is
8843      paradoxical.  The latter case can only occur when it is known that the
8844      "extra" bits will be zero.  Therefore, it is safe to remove the SUBREG.
8845      We can never remove a SUBREG for a non-equality comparison because the
8846      sign bit is in a different place in the underlying object.  */
8847
8848   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
8849   op1 = make_compound_operation (op1, SET);
8850
8851   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
8852       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
8853       && (code == NE || code == EQ)
8854       && ((GET_MODE_SIZE (GET_MODE (op0))
8855            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
8856     {
8857       op0 = SUBREG_REG (op0);
8858       op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
8859     }
8860
8861   else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
8862            && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
8863            && (code == NE || code == EQ)
8864            && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
8865                <= HOST_BITS_PER_WIDE_INT)
8866            && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
8867                & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
8868            && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
8869                                               op1),
8870                (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
8871                 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
8872     op0 = SUBREG_REG (op0), op1 = tem;
8873
8874   /* We now do the opposite procedure: Some machines don't have compare
8875      insns in all modes.  If OP0's mode is an integer mode smaller than a
8876      word and we can't do a compare in that mode, see if there is a larger
8877      mode for which we can do the compare.  There are a number of cases in
8878      which we can use the wider mode.  */
8879
8880   mode = GET_MODE (op0);
8881   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
8882       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
8883       && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
8884     for (tmode = GET_MODE_WIDER_MODE (mode);
8885          (tmode != VOIDmode
8886           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
8887          tmode = GET_MODE_WIDER_MODE (tmode))
8888       if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
8889         {
8890           /* If the only nonzero bits in OP0 and OP1 are those in the
8891              narrower mode and this is an equality or unsigned comparison,
8892              we can use the wider mode.  Similarly for sign-extended
8893              values and equality or signed comparisons.  */
8894           if (((code == EQ || code == NE
8895                 || code == GEU || code == GTU || code == LEU || code == LTU)
8896                && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
8897                && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
8898               || ((code == EQ || code == NE
8899                    || code == GE || code == GT || code == LE || code == LT)
8900                   && (num_sign_bit_copies (op0, tmode)
8901                       > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
8902                   && (num_sign_bit_copies (op1, tmode)
8903                       > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
8904             {
8905               op0 = gen_lowpart_for_combine (tmode, op0);
8906               op1 = gen_lowpart_for_combine (tmode, op1);
8907               break;
8908             }
8909
8910           /* If this is a test for negative, we can make an explicit
8911              test of the sign bit.  */
8912
8913           if (op1 == const0_rtx && (code == LT || code == GE)
8914               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8915             {
8916               op0 = gen_binary (AND, tmode,
8917                                 gen_lowpart_for_combine (tmode, op0),
8918                                 GEN_INT ((HOST_WIDE_INT) 1
8919                                          << (GET_MODE_BITSIZE (mode) - 1)));
8920               code = (code == LT) ? NE : EQ;
8921               break;
8922             }
8923         }
8924
8925   *pop0 = op0;
8926   *pop1 = op1;
8927
8928   return code;
8929 }
8930 \f
8931 /* Return 1 if we know that X, a comparison operation, is not operating
8932    on a floating-point value or is EQ or NE, meaning that we can safely
8933    reverse it.  */
8934
8935 static int
8936 reversible_comparison_p (x)
8937      rtx x;
8938 {
8939   if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
8940       || GET_CODE (x) == NE || GET_CODE (x) == EQ)
8941     return 1;
8942
8943   switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
8944     {
8945     case MODE_INT:
8946     case MODE_PARTIAL_INT:
8947     case MODE_COMPLEX_INT:
8948       return 1;
8949
8950     case MODE_CC:
8951       x = get_last_value (XEXP (x, 0));
8952       return (x && GET_CODE (x) == COMPARE
8953               && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
8954     }
8955
8956   return 0;
8957 }
8958 \f
8959 /* Utility function for following routine.  Called when X is part of a value
8960    being stored into reg_last_set_value.  Sets reg_last_set_table_tick
8961    for each register mentioned.  Similar to mention_regs in cse.c  */
8962
8963 static void
8964 update_table_tick (x)
8965      rtx x;
8966 {
8967   register enum rtx_code code = GET_CODE (x);
8968   register char *fmt = GET_RTX_FORMAT (code);
8969   register int i;
8970
8971   if (code == REG)
8972     {
8973       int regno = REGNO (x);
8974       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
8975                               ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
8976
8977       for (i = regno; i < endregno; i++)
8978         reg_last_set_table_tick[i] = label_tick;
8979
8980       return;
8981     }
8982   
8983   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8984     /* Note that we can't have an "E" in values stored; see
8985        get_last_value_validate.  */
8986     if (fmt[i] == 'e')
8987       update_table_tick (XEXP (x, i));
8988 }
8989
8990 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
8991    are saying that the register is clobbered and we no longer know its
8992    value.  If INSN is zero, don't update reg_last_set; this is only permitted
8993    with VALUE also zero and is used to invalidate the register.  */
8994
8995 static void
8996 record_value_for_reg (reg, insn, value)
8997      rtx reg;
8998      rtx insn;
8999      rtx value;
9000 {
9001   int regno = REGNO (reg);
9002   int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
9003                           ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
9004   int i;
9005
9006   /* If VALUE contains REG and we have a previous value for REG, substitute
9007      the previous value.  */
9008   if (value && insn && reg_overlap_mentioned_p (reg, value))
9009     {
9010       rtx tem;
9011
9012       /* Set things up so get_last_value is allowed to see anything set up to
9013          our insn.  */
9014       subst_low_cuid = INSN_CUID (insn);
9015       tem = get_last_value (reg);      
9016
9017       if (tem)
9018         value = replace_rtx (copy_rtx (value), reg, tem);
9019     }
9020
9021   /* For each register modified, show we don't know its value, that
9022      its value has been updated, and that we don't know the location of
9023      the death of the register.  */
9024   for (i = regno; i < endregno; i ++)
9025     {
9026       if (insn)
9027         reg_last_set[i] = insn;
9028       reg_last_set_value[i] = 0;
9029       reg_last_death[i] = 0;
9030     }
9031
9032   /* Mark registers that are being referenced in this value.  */
9033   if (value)
9034     update_table_tick (value);
9035
9036   /* Now update the status of each register being set.
9037      If someone is using this register in this block, set this register
9038      to invalid since we will get confused between the two lives in this
9039      basic block.  This makes using this register always invalid.  In cse, we
9040      scan the table to invalidate all entries using this register, but this
9041      is too much work for us.  */
9042
9043   for (i = regno; i < endregno; i++)
9044     {
9045       reg_last_set_label[i] = label_tick;
9046       if (value && reg_last_set_table_tick[i] == label_tick)
9047         reg_last_set_invalid[i] = 1;
9048       else
9049         reg_last_set_invalid[i] = 0;
9050     }
9051
9052   /* The value being assigned might refer to X (like in "x++;").  In that
9053      case, we must replace it with (clobber (const_int 0)) to prevent
9054      infinite loops.  */
9055   if (value && ! get_last_value_validate (&value,
9056                                           reg_last_set_label[regno], 0))
9057     {
9058       value = copy_rtx (value);
9059       if (! get_last_value_validate (&value, reg_last_set_label[regno], 1))
9060         value = 0;
9061     }
9062
9063   /* For the main register being modified, update the value, the mode, the
9064      nonzero bits, and the number of sign bit copies.  */
9065
9066   reg_last_set_value[regno] = value;
9067
9068   if (value)
9069     {
9070       subst_low_cuid = INSN_CUID (insn);
9071       reg_last_set_mode[regno] = GET_MODE (reg);
9072       reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
9073       reg_last_set_sign_bit_copies[regno]
9074         = num_sign_bit_copies (value, GET_MODE (reg));
9075     }
9076 }
9077
9078 /* Used for communication between the following two routines.  */
9079 static rtx record_dead_insn;
9080
9081 /* Called via note_stores from record_dead_and_set_regs to handle one
9082    SET or CLOBBER in an insn.  */
9083
9084 static void
9085 record_dead_and_set_regs_1 (dest, setter)
9086      rtx dest, setter;
9087 {
9088   if (GET_CODE (dest) == REG)
9089     {
9090       /* If we are setting the whole register, we know its value.  Otherwise
9091          show that we don't know the value.  We can handle SUBREG in
9092          some cases.  */
9093       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
9094         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
9095       else if (GET_CODE (setter) == SET
9096                && GET_CODE (SET_DEST (setter)) == SUBREG
9097                && SUBREG_REG (SET_DEST (setter)) == dest
9098                && subreg_lowpart_p (SET_DEST (setter)))
9099         record_value_for_reg (dest, record_dead_insn,
9100                               gen_lowpart_for_combine (GET_MODE (dest),
9101                                                        SET_SRC (setter)));
9102       else
9103         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
9104     }
9105   else if (GET_CODE (dest) == MEM
9106            /* Ignore pushes, they clobber nothing.  */
9107            && ! push_operand (dest, GET_MODE (dest)))
9108     mem_last_set = INSN_CUID (record_dead_insn);
9109 }
9110
9111 /* Update the records of when each REG was most recently set or killed
9112    for the things done by INSN.  This is the last thing done in processing
9113    INSN in the combiner loop.
9114
9115    We update reg_last_set, reg_last_set_value, reg_last_death, and also the
9116    similar information mem_last_set (which insn most recently modified memory)
9117    and last_call_cuid (which insn was the most recent subroutine call).  */
9118
9119 static void
9120 record_dead_and_set_regs (insn)
9121      rtx insn;
9122 {
9123   register rtx link;
9124   int i;
9125
9126   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
9127     {
9128       if (REG_NOTE_KIND (link) == REG_DEAD
9129           && GET_CODE (XEXP (link, 0)) == REG)
9130         {
9131           int regno = REGNO (XEXP (link, 0));
9132           int endregno
9133             = regno + (regno < FIRST_PSEUDO_REGISTER
9134                        ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
9135                        : 1);
9136
9137           for (i = regno; i < endregno; i++)
9138             reg_last_death[i] = insn;
9139         }
9140       else if (REG_NOTE_KIND (link) == REG_INC)
9141         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
9142     }
9143
9144   if (GET_CODE (insn) == CALL_INSN)
9145     {
9146       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9147         if (call_used_regs[i])
9148           {
9149             reg_last_set_value[i] = 0;
9150             reg_last_death[i] = 0;
9151           }
9152
9153       last_call_cuid = mem_last_set = INSN_CUID (insn);
9154     }
9155
9156   record_dead_insn = insn;
9157   note_stores (PATTERN (insn), record_dead_and_set_regs_1);
9158 }
9159 \f
9160 /* Utility routine for the following function.  Verify that all the registers
9161    mentioned in *LOC are valid when *LOC was part of a value set when
9162    label_tick == TICK.  Return 0 if some are not.
9163
9164    If REPLACE is non-zero, replace the invalid reference with
9165    (clobber (const_int 0)) and return 1.  This replacement is useful because
9166    we often can get useful information about the form of a value (e.g., if
9167    it was produced by a shift that always produces -1 or 0) even though
9168    we don't know exactly what registers it was produced from.  */
9169
9170 static int
9171 get_last_value_validate (loc, tick, replace)
9172      rtx *loc;
9173      int tick;
9174      int replace;
9175 {
9176   rtx x = *loc;
9177   char *fmt = GET_RTX_FORMAT (GET_CODE (x));
9178   int len = GET_RTX_LENGTH (GET_CODE (x));
9179   int i;
9180
9181   if (GET_CODE (x) == REG)
9182     {
9183       int regno = REGNO (x);
9184       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
9185                               ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
9186       int j;
9187
9188       for (j = regno; j < endregno; j++)
9189         if (reg_last_set_invalid[j]
9190             /* If this is a pseudo-register that was only set once, it is
9191                always valid.  */
9192             || (! (regno >= FIRST_PSEUDO_REGISTER && reg_n_sets[regno] == 1)
9193                 && reg_last_set_label[j] > tick))
9194           {
9195             if (replace)
9196               *loc = gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
9197             return replace;
9198           }
9199
9200       return 1;
9201     }
9202
9203   for (i = 0; i < len; i++)
9204     if ((fmt[i] == 'e'
9205          && get_last_value_validate (&XEXP (x, i), tick, replace) == 0)
9206         /* Don't bother with these.  They shouldn't occur anyway.  */
9207         || fmt[i] == 'E')
9208       return 0;
9209
9210   /* If we haven't found a reason for it to be invalid, it is valid.  */
9211   return 1;
9212 }
9213
9214 /* Get the last value assigned to X, if known.  Some registers
9215    in the value may be replaced with (clobber (const_int 0)) if their value
9216    is known longer known reliably.  */
9217
9218 static rtx
9219 get_last_value (x)
9220      rtx x;
9221 {
9222   int regno;
9223   rtx value;
9224
9225   /* If this is a non-paradoxical SUBREG, get the value of its operand and
9226      then convert it to the desired mode.  If this is a paradoxical SUBREG,
9227      we cannot predict what values the "extra" bits might have. */
9228   if (GET_CODE (x) == SUBREG
9229       && subreg_lowpart_p (x)
9230       && (GET_MODE_SIZE (GET_MODE (x))
9231           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
9232       && (value = get_last_value (SUBREG_REG (x))) != 0)
9233     return gen_lowpart_for_combine (GET_MODE (x), value);
9234
9235   if (GET_CODE (x) != REG)
9236     return 0;
9237
9238   regno = REGNO (x);
9239   value = reg_last_set_value[regno];
9240
9241   /* If we don't have a value or if it isn't for this basic block, return 0. */
9242
9243   if (value == 0
9244       || (reg_n_sets[regno] != 1
9245           && reg_last_set_label[regno] != label_tick))
9246     return 0;
9247
9248   /* If the value was set in a later insn that the ones we are processing,
9249      we can't use it even if the register was only set once, but make a quick
9250      check to see if the previous insn set it to something.  This is commonly
9251      the case when the same pseudo is used by repeated insns.  */
9252
9253   if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
9254     {
9255       rtx insn, set;
9256
9257       for (insn = prev_nonnote_insn (subst_insn);
9258            insn && INSN_CUID (insn) >= subst_low_cuid;
9259            insn = prev_nonnote_insn (insn))
9260         ;
9261
9262       if (insn
9263           && (set = single_set (insn)) != 0
9264           && rtx_equal_p (SET_DEST (set), x))
9265         {
9266           value = SET_SRC (set);
9267
9268           /* Make sure that VALUE doesn't reference X.  Replace any
9269              expliit references with a CLOBBER.  If there are any remaining
9270              references (rare), don't use the value.  */
9271
9272           if (reg_mentioned_p (x, value))
9273             value = replace_rtx (copy_rtx (value), x,
9274                                  gen_rtx (CLOBBER, GET_MODE (x), const0_rtx));
9275
9276           if (reg_overlap_mentioned_p (x, value))
9277             return 0;
9278         }
9279       else
9280         return 0;
9281     }
9282
9283   /* If the value has all its registers valid, return it.  */
9284   if (get_last_value_validate (&value, reg_last_set_label[regno], 0))
9285     return value;
9286
9287   /* Otherwise, make a copy and replace any invalid register with
9288      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
9289
9290   value = copy_rtx (value);
9291   if (get_last_value_validate (&value, reg_last_set_label[regno], 1))
9292     return value;
9293
9294   return 0;
9295 }
9296 \f
9297 /* Return nonzero if expression X refers to a REG or to memory
9298    that is set in an instruction more recent than FROM_CUID.  */
9299
9300 static int
9301 use_crosses_set_p (x, from_cuid)
9302      register rtx x;
9303      int from_cuid;
9304 {
9305   register char *fmt;
9306   register int i;
9307   register enum rtx_code code = GET_CODE (x);
9308
9309   if (code == REG)
9310     {
9311       register int regno = REGNO (x);
9312 #ifdef PUSH_ROUNDING
9313       /* Don't allow uses of the stack pointer to be moved,
9314          because we don't know whether the move crosses a push insn.  */
9315       if (regno == STACK_POINTER_REGNUM)
9316         return 1;
9317 #endif
9318       return (reg_last_set[regno]
9319               && INSN_CUID (reg_last_set[regno]) > from_cuid);
9320     }
9321
9322   if (code == MEM && mem_last_set > from_cuid)
9323     return 1;
9324
9325   fmt = GET_RTX_FORMAT (code);
9326
9327   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9328     {
9329       if (fmt[i] == 'E')
9330         {
9331           register int j;
9332           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9333             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
9334               return 1;
9335         }
9336       else if (fmt[i] == 'e'
9337                && use_crosses_set_p (XEXP (x, i), from_cuid))
9338         return 1;
9339     }
9340   return 0;
9341 }
9342 \f
9343 /* Define three variables used for communication between the following
9344    routines.  */
9345
9346 static int reg_dead_regno, reg_dead_endregno;
9347 static int reg_dead_flag;
9348
9349 /* Function called via note_stores from reg_dead_at_p.
9350
9351    If DEST is within [reg_dead_rengno, reg_dead_endregno), set 
9352    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
9353
9354 static void
9355 reg_dead_at_p_1 (dest, x)
9356      rtx dest;
9357      rtx x;
9358 {
9359   int regno, endregno;
9360
9361   if (GET_CODE (dest) != REG)
9362     return;
9363
9364   regno = REGNO (dest);
9365   endregno = regno + (regno < FIRST_PSEUDO_REGISTER 
9366                       ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
9367
9368   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
9369     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
9370 }
9371
9372 /* Return non-zero if REG is known to be dead at INSN.
9373
9374    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
9375    referencing REG, it is dead.  If we hit a SET referencing REG, it is
9376    live.  Otherwise, see if it is live or dead at the start of the basic
9377    block we are in.  */
9378
9379 static int
9380 reg_dead_at_p (reg, insn)
9381      rtx reg;
9382      rtx insn;
9383 {
9384   int block, i;
9385
9386   /* Set variables for reg_dead_at_p_1.  */
9387   reg_dead_regno = REGNO (reg);
9388   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
9389                                         ? HARD_REGNO_NREGS (reg_dead_regno,
9390                                                             GET_MODE (reg))
9391                                         : 1);
9392
9393   reg_dead_flag = 0;
9394
9395   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
9396      beginning of function.  */
9397   for (; insn && GET_CODE (insn) != CODE_LABEL;
9398        insn = prev_nonnote_insn (insn))
9399     {
9400       note_stores (PATTERN (insn), reg_dead_at_p_1);
9401       if (reg_dead_flag)
9402         return reg_dead_flag == 1 ? 1 : 0;
9403
9404       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
9405         return 1;
9406     }
9407
9408   /* Get the basic block number that we were in.  */
9409   if (insn == 0)
9410     block = 0;
9411   else
9412     {
9413       for (block = 0; block < n_basic_blocks; block++)
9414         if (insn == basic_block_head[block])
9415           break;
9416
9417       if (block == n_basic_blocks)
9418         return 0;
9419     }
9420
9421   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
9422     if (basic_block_live_at_start[block][i / REGSET_ELT_BITS]
9423         & ((REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS)))
9424       return 0;
9425
9426   return 1;
9427 }
9428 \f
9429 /* Remove register number REGNO from the dead registers list of INSN.
9430
9431    Return the note used to record the death, if there was one.  */
9432
9433 rtx
9434 remove_death (regno, insn)
9435      int regno;
9436      rtx insn;
9437 {
9438   register rtx note = find_regno_note (insn, REG_DEAD, regno);
9439
9440   if (note)
9441     {
9442       reg_n_deaths[regno]--;
9443       remove_note (insn, note);
9444     }
9445
9446   return note;
9447 }
9448
9449 /* For each register (hardware or pseudo) used within expression X, if its
9450    death is in an instruction with cuid between FROM_CUID (inclusive) and
9451    TO_INSN (exclusive), put a REG_DEAD note for that register in the
9452    list headed by PNOTES. 
9453
9454    This is done when X is being merged by combination into TO_INSN.  These
9455    notes will then be distributed as needed.  */
9456
9457 static void
9458 move_deaths (x, from_cuid, to_insn, pnotes)
9459      rtx x;
9460      int from_cuid;
9461      rtx to_insn;
9462      rtx *pnotes;
9463 {
9464   register char *fmt;
9465   register int len, i;
9466   register enum rtx_code code = GET_CODE (x);
9467
9468   if (code == REG)
9469     {
9470       register int regno = REGNO (x);
9471       register rtx where_dead = reg_last_death[regno];
9472
9473       if (where_dead && INSN_CUID (where_dead) >= from_cuid
9474           && INSN_CUID (where_dead) < INSN_CUID (to_insn))
9475         {
9476           rtx note = remove_death (regno, where_dead);
9477
9478           /* It is possible for the call above to return 0.  This can occur
9479              when reg_last_death points to I2 or I1 that we combined with.
9480              In that case make a new note.
9481
9482              We must also check for the case where X is a hard register
9483              and NOTE is a death note for a range of hard registers
9484              including X.  In that case, we must put REG_DEAD notes for
9485              the remaining registers in place of NOTE.  */
9486
9487           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
9488               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
9489                   != GET_MODE_SIZE (GET_MODE (x))))
9490             {
9491               int deadregno = REGNO (XEXP (note, 0));
9492               int deadend
9493                 = (deadregno + HARD_REGNO_NREGS (deadregno,
9494                                                  GET_MODE (XEXP (note, 0))));
9495               int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
9496               int i;
9497
9498               for (i = deadregno; i < deadend; i++)
9499                 if (i < regno || i >= ourend)
9500                   REG_NOTES (where_dead)
9501                     = gen_rtx (EXPR_LIST, REG_DEAD,
9502                                gen_rtx (REG, word_mode, i),
9503                                REG_NOTES (where_dead));
9504             }
9505
9506           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
9507             {
9508               XEXP (note, 1) = *pnotes;
9509               *pnotes = note;
9510             }
9511           else
9512             *pnotes = gen_rtx (EXPR_LIST, REG_DEAD, x, *pnotes);
9513
9514           reg_n_deaths[regno]++;
9515         }
9516
9517       return;
9518     }
9519
9520   else if (GET_CODE (x) == SET)
9521     {
9522       rtx dest = SET_DEST (x);
9523
9524       move_deaths (SET_SRC (x), from_cuid, to_insn, pnotes);
9525
9526       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
9527          that accesses one word of a multi-word item, some
9528          piece of everything register in the expression is used by
9529          this insn, so remove any old death.  */
9530
9531       if (GET_CODE (dest) == ZERO_EXTRACT
9532           || GET_CODE (dest) == STRICT_LOW_PART
9533           || (GET_CODE (dest) == SUBREG
9534               && (((GET_MODE_SIZE (GET_MODE (dest))
9535                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
9536                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
9537                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
9538         {
9539           move_deaths (dest, from_cuid, to_insn, pnotes);
9540           return;
9541         }
9542
9543       /* If this is some other SUBREG, we know it replaces the entire
9544          value, so use that as the destination.  */
9545       if (GET_CODE (dest) == SUBREG)
9546         dest = SUBREG_REG (dest);
9547
9548       /* If this is a MEM, adjust deaths of anything used in the address.
9549          For a REG (the only other possibility), the entire value is
9550          being replaced so the old value is not used in this insn.  */
9551
9552       if (GET_CODE (dest) == MEM)
9553         move_deaths (XEXP (dest, 0), from_cuid, to_insn, pnotes);
9554       return;
9555     }
9556
9557   else if (GET_CODE (x) == CLOBBER)
9558     return;
9559
9560   len = GET_RTX_LENGTH (code);
9561   fmt = GET_RTX_FORMAT (code);
9562
9563   for (i = 0; i < len; i++)
9564     {
9565       if (fmt[i] == 'E')
9566         {
9567           register int j;
9568           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9569             move_deaths (XVECEXP (x, i, j), from_cuid, to_insn, pnotes);
9570         }
9571       else if (fmt[i] == 'e')
9572         move_deaths (XEXP (x, i), from_cuid, to_insn, pnotes);
9573     }
9574 }
9575 \f
9576 /* Return 1 if X is the target of a bit-field assignment in BODY, the
9577    pattern of an insn.  X must be a REG.  */
9578
9579 static int
9580 reg_bitfield_target_p (x, body)
9581      rtx x;
9582      rtx body;
9583 {
9584   int i;
9585
9586   if (GET_CODE (body) == SET)
9587     {
9588       rtx dest = SET_DEST (body);
9589       rtx target;
9590       int regno, tregno, endregno, endtregno;
9591
9592       if (GET_CODE (dest) == ZERO_EXTRACT)
9593         target = XEXP (dest, 0);
9594       else if (GET_CODE (dest) == STRICT_LOW_PART)
9595         target = SUBREG_REG (XEXP (dest, 0));
9596       else
9597         return 0;
9598
9599       if (GET_CODE (target) == SUBREG)
9600         target = SUBREG_REG (target);
9601
9602       if (GET_CODE (target) != REG)
9603         return 0;
9604
9605       tregno = REGNO (target), regno = REGNO (x);
9606       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
9607         return target == x;
9608
9609       endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
9610       endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
9611
9612       return endregno > tregno && regno < endtregno;
9613     }
9614
9615   else if (GET_CODE (body) == PARALLEL)
9616     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
9617       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
9618         return 1;
9619
9620   return 0;
9621 }      
9622 \f
9623 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
9624    as appropriate.  I3 and I2 are the insns resulting from the combination
9625    insns including FROM (I2 may be zero).
9626
9627    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
9628    not need REG_DEAD notes because they are being substituted for.  This
9629    saves searching in the most common cases.
9630
9631    Each note in the list is either ignored or placed on some insns, depending
9632    on the type of note.  */
9633
9634 static void
9635 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
9636      rtx notes;
9637      rtx from_insn;
9638      rtx i3, i2;
9639      rtx elim_i2, elim_i1;
9640 {
9641   rtx note, next_note;
9642   rtx tem;
9643
9644   for (note = notes; note; note = next_note)
9645     {
9646       rtx place = 0, place2 = 0;
9647
9648       /* If this NOTE references a pseudo register, ensure it references
9649          the latest copy of that register.  */
9650       if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
9651           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
9652         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
9653
9654       next_note = XEXP (note, 1);
9655       switch (REG_NOTE_KIND (note))
9656         {
9657         case REG_UNUSED:
9658           /* If this register is set or clobbered in I3, put the note there
9659              unless there is one already.  */
9660           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
9661             {
9662               if (! (GET_CODE (XEXP (note, 0)) == REG
9663                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
9664                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
9665                 place = i3;
9666             }
9667           /* Otherwise, if this register is used by I3, then this register
9668              now dies here, so we must put a REG_DEAD note here unless there
9669              is one already.  */
9670           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
9671                    && ! (GET_CODE (XEXP (note, 0)) == REG
9672                          ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
9673                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
9674             {
9675               PUT_REG_NOTE_KIND (note, REG_DEAD);
9676               place = i3;
9677             }
9678           break;
9679
9680         case REG_EQUAL:
9681         case REG_EQUIV:
9682         case REG_NONNEG:
9683           /* These notes say something about results of an insn.  We can
9684              only support them if they used to be on I3 in which case they
9685              remain on I3.  Otherwise they are ignored.
9686
9687              If the note refers to an expression that is not a constant, we
9688              must also ignore the note since we cannot tell whether the
9689              equivalence is still true.  It might be possible to do
9690              slightly better than this (we only have a problem if I2DEST
9691              or I1DEST is present in the expression), but it doesn't
9692              seem worth the trouble.  */
9693
9694           if (from_insn == i3
9695               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
9696             place = i3;
9697           break;
9698
9699         case REG_INC:
9700         case REG_NO_CONFLICT:
9701         case REG_LABEL:
9702           /* These notes say something about how a register is used.  They must
9703              be present on any use of the register in I2 or I3.  */
9704           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
9705             place = i3;
9706
9707           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
9708             {
9709               if (place)
9710                 place2 = i2;
9711               else
9712                 place = i2;
9713             }
9714           break;
9715
9716         case REG_WAS_0:
9717           /* It is too much trouble to try to see if this note is still
9718              correct in all situations.  It is better to simply delete it.  */
9719           break;
9720
9721         case REG_RETVAL:
9722           /* If the insn previously containing this note still exists,
9723              put it back where it was.  Otherwise move it to the previous
9724              insn.  Adjust the corresponding REG_LIBCALL note.  */
9725           if (GET_CODE (from_insn) != NOTE)
9726             place = from_insn;
9727           else
9728             {
9729               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
9730               place = prev_real_insn (from_insn);
9731               if (tem && place)
9732                 XEXP (tem, 0) = place;
9733             }
9734           break;
9735
9736         case REG_LIBCALL:
9737           /* This is handled similarly to REG_RETVAL.  */
9738           if (GET_CODE (from_insn) != NOTE)
9739             place = from_insn;
9740           else
9741             {
9742               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
9743               place = next_real_insn (from_insn);
9744               if (tem && place)
9745                 XEXP (tem, 0) = place;
9746             }
9747           break;
9748
9749         case REG_DEAD:
9750           /* If the register is used as an input in I3, it dies there.
9751              Similarly for I2, if it is non-zero and adjacent to I3.
9752
9753              If the register is not used as an input in either I3 or I2
9754              and it is not one of the registers we were supposed to eliminate,
9755              there are two possibilities.  We might have a non-adjacent I2
9756              or we might have somehow eliminated an additional register
9757              from a computation.  For example, we might have had A & B where
9758              we discover that B will always be zero.  In this case we will
9759              eliminate the reference to A.
9760
9761              In both cases, we must search to see if we can find a previous
9762              use of A and put the death note there.  */
9763
9764           if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
9765             place = i3;
9766           else if (i2 != 0 && next_nonnote_insn (i2) == i3
9767                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
9768             place = i2;
9769
9770           if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
9771             break;
9772
9773           /* If the register is used in both I2 and I3 and it dies in I3, 
9774              we might have added another reference to it.  If reg_n_refs
9775              was 2, bump it to 3.  This has to be correct since the 
9776              register must have been set somewhere.  The reason this is
9777              done is because local-alloc.c treats 2 references as a 
9778              special case.  */
9779
9780           if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
9781               && reg_n_refs[REGNO (XEXP (note, 0))]== 2
9782               && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
9783             reg_n_refs[REGNO (XEXP (note, 0))] = 3;
9784
9785           if (place == 0)
9786             for (tem = prev_nonnote_insn (i3);
9787                  tem && (GET_CODE (tem) == INSN
9788                          || GET_CODE (tem) == CALL_INSN);
9789                  tem = prev_nonnote_insn (tem))
9790               {
9791                 /* If the register is being set at TEM, see if that is all
9792                    TEM is doing.  If so, delete TEM.  Otherwise, make this
9793                    into a REG_UNUSED note instead.  */
9794                 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
9795                   {
9796                     rtx set = single_set (tem);
9797
9798                     /* Verify that it was the set, and not a clobber that
9799                        modified the register.  */
9800
9801                     if (set != 0 && ! side_effects_p (SET_SRC (set))
9802                         && rtx_equal_p (XEXP (note, 0), SET_DEST (set)))
9803                       {
9804                         /* Move the notes and links of TEM elsewhere.
9805                            This might delete other dead insns recursively. 
9806                            First set the pattern to something that won't use
9807                            any register.  */
9808
9809                         PATTERN (tem) = pc_rtx;
9810
9811                         distribute_notes (REG_NOTES (tem), tem, tem,
9812                                           NULL_RTX, NULL_RTX, NULL_RTX);
9813                         distribute_links (LOG_LINKS (tem));
9814
9815                         PUT_CODE (tem, NOTE);
9816                         NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
9817                         NOTE_SOURCE_FILE (tem) = 0;
9818                       }
9819                     else
9820                       {
9821                         PUT_REG_NOTE_KIND (note, REG_UNUSED);
9822
9823                         /*  If there isn't already a REG_UNUSED note, put one
9824                             here.  */
9825                         if (! find_regno_note (tem, REG_UNUSED,
9826                                                REGNO (XEXP (note, 0))))
9827                           place = tem;
9828                         break;
9829                       }
9830                   }
9831                 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem)))
9832                   {
9833                     place = tem;
9834                     break;
9835                   }
9836               }
9837
9838           /* If the register is set or already dead at PLACE, we needn't do
9839              anything with this note if it is still a REG_DEAD note.  
9840
9841              Note that we cannot use just `dead_or_set_p' here since we can
9842              convert an assignment to a register into a bit-field assignment.
9843              Therefore, we must also omit the note if the register is the 
9844              target of a bitfield assignment.  */
9845              
9846           if (place && REG_NOTE_KIND (note) == REG_DEAD)
9847             {
9848               int regno = REGNO (XEXP (note, 0));
9849
9850               if (dead_or_set_p (place, XEXP (note, 0))
9851                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
9852                 {
9853                   /* Unless the register previously died in PLACE, clear
9854                      reg_last_death.  [I no longer understand why this is
9855                      being done.] */
9856                   if (reg_last_death[regno] != place)
9857                     reg_last_death[regno] = 0;
9858                   place = 0;
9859                 }
9860               else
9861                 reg_last_death[regno] = place;
9862
9863               /* If this is a death note for a hard reg that is occupying
9864                  multiple registers, ensure that we are still using all
9865                  parts of the object.  If we find a piece of the object
9866                  that is unused, we must add a USE for that piece before
9867                  PLACE and put the appropriate REG_DEAD note on it.
9868
9869                  An alternative would be to put a REG_UNUSED for the pieces
9870                  on the insn that set the register, but that can't be done if
9871                  it is not in the same block.  It is simpler, though less
9872                  efficient, to add the USE insns.  */
9873
9874               if (place && regno < FIRST_PSEUDO_REGISTER
9875                   && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
9876                 {
9877                   int endregno
9878                     = regno + HARD_REGNO_NREGS (regno,
9879                                                 GET_MODE (XEXP (note, 0)));
9880                   int all_used = 1;
9881                   int i;
9882
9883                   for (i = regno; i < endregno; i++)
9884                     if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0))
9885                       {
9886                         rtx piece = gen_rtx (REG, word_mode, i);
9887                         rtx p;
9888
9889                         /* See if we already placed a USE note for this
9890                            register in front of PLACE.  */
9891                         for (p = place;
9892                              GET_CODE (PREV_INSN (p)) == INSN
9893                              && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
9894                              p = PREV_INSN (p))
9895                           if (rtx_equal_p (piece,
9896                                            XEXP (PATTERN (PREV_INSN (p)), 0)))
9897                             {
9898                               p = 0;
9899                               break;
9900                             }
9901
9902                         if (p)
9903                           {
9904                             rtx use_insn
9905                               = emit_insn_before (gen_rtx (USE, VOIDmode,
9906                                                            piece),
9907                                                   p);
9908                             REG_NOTES (use_insn)
9909                               = gen_rtx (EXPR_LIST, REG_DEAD, piece,
9910                                          REG_NOTES (use_insn));
9911                           }
9912
9913                         all_used = 0;
9914                       }
9915
9916                   /* Check for the case where the register dying partially
9917                      overlaps the register set by this insn.  */
9918                   if (all_used)
9919                     for (i = regno; i < endregno; i++)
9920                       if (dead_or_set_regno_p (place, i))
9921                           {
9922                             all_used = 0;
9923                             break;
9924                           }
9925
9926                   if (! all_used)
9927                     {
9928                       /* Put only REG_DEAD notes for pieces that are
9929                          still used and that are not already dead or set.  */
9930
9931                       for (i = regno; i < endregno; i++)
9932                         {
9933                           rtx piece = gen_rtx (REG, word_mode, i);
9934
9935                           if (reg_referenced_p (piece, PATTERN (place))
9936                               && ! dead_or_set_p (place, piece)
9937                               && ! reg_bitfield_target_p (piece,
9938                                                           PATTERN (place)))
9939                             REG_NOTES (place) = gen_rtx (EXPR_LIST, REG_DEAD,
9940                                                          piece,
9941                                                          REG_NOTES (place));
9942                         }
9943
9944                       place = 0;
9945                     }
9946                 }
9947             }
9948           break;
9949
9950         default:
9951           /* Any other notes should not be present at this point in the
9952              compilation.  */
9953           abort ();
9954         }
9955
9956       if (place)
9957         {
9958           XEXP (note, 1) = REG_NOTES (place);
9959           REG_NOTES (place) = note;
9960         }
9961       else if ((REG_NOTE_KIND (note) == REG_DEAD
9962                 || REG_NOTE_KIND (note) == REG_UNUSED)
9963                && GET_CODE (XEXP (note, 0)) == REG)
9964         reg_n_deaths[REGNO (XEXP (note, 0))]--;
9965
9966       if (place2)
9967         {
9968           if ((REG_NOTE_KIND (note) == REG_DEAD
9969                || REG_NOTE_KIND (note) == REG_UNUSED)
9970               && GET_CODE (XEXP (note, 0)) == REG)
9971             reg_n_deaths[REGNO (XEXP (note, 0))]++;
9972
9973           REG_NOTES (place2) = gen_rtx (GET_CODE (note), REG_NOTE_KIND (note),
9974                                         XEXP (note, 0), REG_NOTES (place2));
9975         }
9976     }
9977 }
9978 \f
9979 /* Similarly to above, distribute the LOG_LINKS that used to be present on
9980    I3, I2, and I1 to new locations.  This is also called in one case to
9981    add a link pointing at I3 when I3's destination is changed.  */
9982
9983 static void
9984 distribute_links (links)
9985      rtx links;
9986 {
9987   rtx link, next_link;
9988
9989   for (link = links; link; link = next_link)
9990     {
9991       rtx place = 0;
9992       rtx insn;
9993       rtx set, reg;
9994
9995       next_link = XEXP (link, 1);
9996
9997       /* If the insn that this link points to is a NOTE or isn't a single
9998          set, ignore it.  In the latter case, it isn't clear what we
9999          can do other than ignore the link, since we can't tell which 
10000          register it was for.  Such links wouldn't be used by combine
10001          anyway.
10002
10003          It is not possible for the destination of the target of the link to
10004          have been changed by combine.  The only potential of this is if we
10005          replace I3, I2, and I1 by I3 and I2.  But in that case the
10006          destination of I2 also remains unchanged.  */
10007
10008       if (GET_CODE (XEXP (link, 0)) == NOTE
10009           || (set = single_set (XEXP (link, 0))) == 0)
10010         continue;
10011
10012       reg = SET_DEST (set);
10013       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
10014              || GET_CODE (reg) == SIGN_EXTRACT
10015              || GET_CODE (reg) == STRICT_LOW_PART)
10016         reg = XEXP (reg, 0);
10017
10018       /* A LOG_LINK is defined as being placed on the first insn that uses
10019          a register and points to the insn that sets the register.  Start
10020          searching at the next insn after the target of the link and stop
10021          when we reach a set of the register or the end of the basic block.
10022
10023          Note that this correctly handles the link that used to point from
10024          I3 to I2.  Also note that not much searching is typically done here
10025          since most links don't point very far away.  */
10026
10027       for (insn = NEXT_INSN (XEXP (link, 0));
10028            (insn && GET_CODE (insn) != CODE_LABEL
10029             && GET_CODE (PREV_INSN (insn)) != JUMP_INSN);
10030            insn = NEXT_INSN (insn))
10031         if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
10032             && reg_overlap_mentioned_p (reg, PATTERN (insn)))
10033           {
10034             if (reg_referenced_p (reg, PATTERN (insn)))
10035               place = insn;
10036             break;
10037           }
10038
10039       /* If we found a place to put the link, place it there unless there
10040          is already a link to the same insn as LINK at that point.  */
10041
10042       if (place)
10043         {
10044           rtx link2;
10045
10046           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
10047             if (XEXP (link2, 0) == XEXP (link, 0))
10048               break;
10049
10050           if (link2 == 0)
10051             {
10052               XEXP (link, 1) = LOG_LINKS (place);
10053               LOG_LINKS (place) = link;
10054             }
10055         }
10056     }
10057 }
10058 \f
10059 void
10060 dump_combine_stats (file)
10061      FILE *file;
10062 {
10063   fprintf
10064     (file,
10065      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
10066      combine_attempts, combine_merges, combine_extras, combine_successes);
10067 }
10068
10069 void
10070 dump_combine_total_stats (file)
10071      FILE *file;
10072 {
10073   fprintf
10074     (file,
10075      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
10076      total_attempts, total_merges, total_extras, total_successes);
10077 }