OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 88, 92-98, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23    Portable Optimizer, but redone to work on our list-structured
24    representation for RTL instead of their string representation.
25
26    The LOG_LINKS of each insn identify the most recent assignment
27    to each REG used in the insn.  It is a list of previous insns,
28    each of which contains a SET for a REG that is used in this insn
29    and not used or set in between.  LOG_LINKs never cross basic blocks.
30    They were set up by the preceding pass (lifetime analysis).
31
32    We try to combine each pair of insns joined by a logical link.
33    We also try to combine triples of insns A, B and C when
34    C has a link back to B and B has a link back to A.
35
36    LOG_LINKS does not have links for use of the CC0.  They don't
37    need to, because the insn that sets the CC0 is always immediately
38    before the insn that tests it.  So we always regard a branch
39    insn as having a logical link to the preceding insn.  The same is true
40    for an insn explicitly using CC0.
41
42    We check (with use_crosses_set_p) to avoid combining in such a way
43    as to move a computation to a place where its value would be different.
44
45    Combination is done by mathematically substituting the previous
46    insn(s) values for the regs they set into the expressions in
47    the later insns that refer to these regs.  If the result is a valid insn
48    for our target machine, according to the machine description,
49    we install it, delete the earlier insns, and update the data flow
50    information (LOG_LINKS and REG_NOTES) for what we did.
51
52    There are a few exceptions where the dataflow information created by
53    flow.c aren't completely updated:
54
55    - reg_live_length is not updated
56    - reg_n_refs is not adjusted in the rare case when a register is
57      no longer required in a computation
58    - there are extremely rare cases (see distribute_regnotes) when a
59      REG_DEAD note is lost
60    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
61      removed because there is no way to know which register it was 
62      linking
63
64    To simplify substitution, we combine only when the earlier insn(s)
65    consist of only a single assignment.  To simplify updating afterward,
66    we never combine when a subroutine call appears in the middle.
67
68    Since we do not represent assignments to CC0 explicitly except when that
69    is all an insn does, there is no LOG_LINKS entry in an insn that uses
70    the condition code for the insn that set the condition code.
71    Fortunately, these two insns must be consecutive.
72    Therefore, every JUMP_INSN is taken to have an implicit logical link
73    to the preceding insn.  This is not quite right, since non-jumps can
74    also use the condition code; but in practice such insns would not
75    combine anyway.  */
76
77 #include "config.h"
78 #include "system.h"
79 #include "rtl.h"
80 #include "tm_p.h"
81 #include "flags.h"
82 #include "regs.h"
83 #include "hard-reg-set.h"
84 #include "basic-block.h"
85 #include "insn-config.h"
86 #include "function.h"
87 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
88 #include "expr.h"
89 #include "insn-flags.h"
90 #include "insn-codes.h"
91 #include "insn-attr.h"
92 #include "recog.h"
93 #include "real.h"
94 #include "toplev.h"
95
96 /* It is not safe to use ordinary gen_lowpart in combine.
97    Use gen_lowpart_for_combine instead.  See comments there.  */
98 #define gen_lowpart dont_use_gen_lowpart_you_dummy
99
100 /* Number of attempts to combine instructions in this function.  */
101
102 static int combine_attempts;
103
104 /* Number of attempts that got as far as substitution in this function.  */
105
106 static int combine_merges;
107
108 /* Number of instructions combined with added SETs in this function.  */
109
110 static int combine_extras;
111
112 /* Number of instructions combined in this function.  */
113
114 static int combine_successes;
115
116 /* Totals over entire compilation.  */
117
118 static int total_attempts, total_merges, total_extras, total_successes;
119
120 /* Define a default value for REVERSIBLE_CC_MODE.
121    We can never assume that a condition code mode is safe to reverse unless
122    the md tells us so.  */
123 #ifndef REVERSIBLE_CC_MODE
124 #define REVERSIBLE_CC_MODE(MODE) 0
125 #endif
126 \f
127 /* Vector mapping INSN_UIDs to cuids.
128    The cuids are like uids but increase monotonically always.
129    Combine always uses cuids so that it can compare them.
130    But actually renumbering the uids, which we used to do,
131    proves to be a bad idea because it makes it hard to compare
132    the dumps produced by earlier passes with those from later passes.  */
133
134 static int *uid_cuid;
135 static int max_uid_cuid;
136
137 /* Get the cuid of an insn.  */
138
139 #define INSN_CUID(INSN) \
140 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
141
142 /* Maximum register number, which is the size of the tables below.  */
143
144 static int combine_max_regno;
145
146 /* Record last point of death of (hard or pseudo) register n.  */
147
148 static rtx *reg_last_death;
149
150 /* Record last point of modification of (hard or pseudo) register n.  */
151
152 static rtx *reg_last_set;
153
154 /* Record the cuid of the last insn that invalidated memory
155    (anything that writes memory, and subroutine calls, but not pushes).  */
156
157 static int mem_last_set;
158
159 /* Record the cuid of the last CALL_INSN
160    so we can tell whether a potential combination crosses any calls.  */
161
162 static int last_call_cuid;
163
164 /* When `subst' is called, this is the insn that is being modified
165    (by combining in a previous insn).  The PATTERN of this insn
166    is still the old pattern partially modified and it should not be
167    looked at, but this may be used to examine the successors of the insn
168    to judge whether a simplification is valid.  */
169
170 static rtx subst_insn;
171
172 /* This is an insn that belongs before subst_insn, but is not currently
173    on the insn chain.  */
174
175 static rtx subst_prev_insn;
176
177 /* This is the lowest CUID that `subst' is currently dealing with.
178    get_last_value will not return a value if the register was set at or
179    after this CUID.  If not for this mechanism, we could get confused if
180    I2 or I1 in try_combine were an insn that used the old value of a register
181    to obtain a new value.  In that case, we might erroneously get the
182    new value of the register when we wanted the old one.  */
183
184 static int subst_low_cuid;
185
186 /* This contains any hard registers that are used in newpat; reg_dead_at_p
187    must consider all these registers to be always live.  */
188
189 static HARD_REG_SET newpat_used_regs;
190
191 /* This is an insn to which a LOG_LINKS entry has been added.  If this
192    insn is the earlier than I2 or I3, combine should rescan starting at
193    that location.  */
194
195 static rtx added_links_insn;
196
197 /* Basic block number of the block in which we are performing combines.  */
198 static int this_basic_block;
199 \f
200 /* The next group of arrays allows the recording of the last value assigned
201    to (hard or pseudo) register n.  We use this information to see if a
202    operation being processed is redundant given a prior operation performed
203    on the register.  For example, an `and' with a constant is redundant if
204    all the zero bits are already known to be turned off.
205
206    We use an approach similar to that used by cse, but change it in the
207    following ways:
208
209    (1) We do not want to reinitialize at each label.
210    (2) It is useful, but not critical, to know the actual value assigned
211        to a register.  Often just its form is helpful.
212
213    Therefore, we maintain the following arrays:
214
215    reg_last_set_value           the last value assigned
216    reg_last_set_label           records the value of label_tick when the
217                                 register was assigned
218    reg_last_set_table_tick      records the value of label_tick when a
219                                 value using the register is assigned
220    reg_last_set_invalid         set to non-zero when it is not valid
221                                 to use the value of this register in some
222                                 register's value
223
224    To understand the usage of these tables, it is important to understand
225    the distinction between the value in reg_last_set_value being valid
226    and the register being validly contained in some other expression in the
227    table.
228
229    Entry I in reg_last_set_value is valid if it is non-zero, and either
230    reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
231
232    Register I may validly appear in any expression returned for the value
233    of another register if reg_n_sets[i] is 1.  It may also appear in the
234    value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
235    reg_last_set_invalid[j] is zero.
236
237    If an expression is found in the table containing a register which may
238    not validly appear in an expression, the register is replaced by
239    something that won't match, (clobber (const_int 0)).
240
241    reg_last_set_invalid[i] is set non-zero when register I is being assigned
242    to and reg_last_set_table_tick[i] == label_tick.  */
243
244 /* Record last value assigned to (hard or pseudo) register n.  */
245
246 static rtx *reg_last_set_value;
247
248 /* Record the value of label_tick when the value for register n is placed in
249    reg_last_set_value[n].  */
250
251 static int *reg_last_set_label;
252
253 /* Record the value of label_tick when an expression involving register n
254    is placed in reg_last_set_value.  */
255
256 static int *reg_last_set_table_tick;
257
258 /* Set non-zero if references to register n in expressions should not be
259    used.  */
260
261 static char *reg_last_set_invalid;
262
263 /* Incremented for each label.  */
264
265 static int label_tick;
266
267 /* Some registers that are set more than once and used in more than one
268    basic block are nevertheless always set in similar ways.  For example,
269    a QImode register may be loaded from memory in two places on a machine
270    where byte loads zero extend.
271
272    We record in the following array what we know about the nonzero
273    bits of a register, specifically which bits are known to be zero.
274
275    If an entry is zero, it means that we don't know anything special.  */
276
277 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
278
279 /* Mode used to compute significance in reg_nonzero_bits.  It is the largest
280    integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
281
282 static enum machine_mode nonzero_bits_mode;
283
284 /* Nonzero if we know that a register has some leading bits that are always
285    equal to the sign bit.  */
286
287 static char *reg_sign_bit_copies;
288
289 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
290    It is zero while computing them and after combine has completed.  This
291    former test prevents propagating values based on previously set values,
292    which can be incorrect if a variable is modified in a loop.  */
293
294 static int nonzero_sign_valid;
295
296 /* These arrays are maintained in parallel with reg_last_set_value
297    and are used to store the mode in which the register was last set,
298    the bits that were known to be zero when it was last set, and the
299    number of sign bits copies it was known to have when it was last set.  */
300
301 static enum machine_mode *reg_last_set_mode;
302 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
303 static char *reg_last_set_sign_bit_copies;
304 \f
305 /* Record one modification to rtl structure
306    to be undone by storing old_contents into *where.
307    is_int is 1 if the contents are an int.  */
308
309 struct undo
310 {
311   struct undo *next;
312   int is_int;
313   union {rtx r; int i;} old_contents;
314   union {rtx *r; int *i;} where;
315 };
316
317 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
318    num_undo says how many are currently recorded.
319
320    storage is nonzero if we must undo the allocation of new storage.
321    The value of storage is what to pass to obfree.
322
323    other_insn is nonzero if we have modified some other insn in the process
324    of working on subst_insn.  It must be verified too.
325
326    previous_undos is the value of undobuf.undos when we started processing
327    this substitution.  This will prevent gen_rtx_combine from re-used a piece
328    from the previous expression.  Doing so can produce circular rtl
329    structures.  */
330
331 struct undobuf
332 {
333   char *storage;
334   struct undo *undos;
335   struct undo *frees;
336   struct undo *previous_undos;
337   rtx other_insn;
338 };
339
340 static struct undobuf undobuf;
341
342 /* Number of times the pseudo being substituted for
343    was found and replaced.  */
344
345 static int n_occurrences;
346
347 static void do_SUBST                    PROTO((rtx *, rtx));
348 static void do_SUBST_INT                PROTO((int *, int));
349 static void init_reg_last_arrays        PROTO((void));
350 static void setup_incoming_promotions   PROTO((void));
351 static void set_nonzero_bits_and_sign_copies  PROTO((rtx, rtx));
352 static int can_combine_p        PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
353 static int sets_function_arg_p  PROTO((rtx));
354 static int combinable_i3pat     PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
355 static rtx try_combine          PROTO((rtx, rtx, rtx));
356 static void undo_all            PROTO((void));
357 static rtx *find_split_point    PROTO((rtx *, rtx));
358 static rtx subst                PROTO((rtx, rtx, rtx, int, int));
359 static rtx simplify_rtx         PROTO((rtx, enum machine_mode, int, int));
360 static rtx simplify_if_then_else  PROTO((rtx));
361 static rtx simplify_set         PROTO((rtx));
362 static rtx simplify_logical     PROTO((rtx, int));
363 static rtx expand_compound_operation  PROTO((rtx));
364 static rtx expand_field_assignment  PROTO((rtx));
365 static rtx make_extraction      PROTO((enum machine_mode, rtx, int, rtx, int,
366                                        int, int, int));
367 static rtx extract_left_shift   PROTO((rtx, int));
368 static rtx make_compound_operation  PROTO((rtx, enum rtx_code));
369 static int get_pos_from_mask    PROTO((unsigned HOST_WIDE_INT, int *));
370 static rtx force_to_mode        PROTO((rtx, enum machine_mode,
371                                        unsigned HOST_WIDE_INT, rtx, int));
372 static rtx if_then_else_cond    PROTO((rtx, rtx *, rtx *));
373 static rtx known_cond           PROTO((rtx, enum rtx_code, rtx, rtx));
374 static int rtx_equal_for_field_assignment_p PROTO((rtx, rtx));
375 static rtx make_field_assignment  PROTO((rtx));
376 static rtx apply_distributive_law  PROTO((rtx));
377 static rtx simplify_and_const_int  PROTO((rtx, enum machine_mode, rtx,
378                                           unsigned HOST_WIDE_INT));
379 static unsigned HOST_WIDE_INT nonzero_bits  PROTO((rtx, enum machine_mode));
380 static int num_sign_bit_copies  PROTO((rtx, enum machine_mode));
381 static int merge_outer_ops      PROTO((enum rtx_code *, HOST_WIDE_INT *,
382                                        enum rtx_code, HOST_WIDE_INT,
383                                        enum machine_mode, int *));
384 static rtx simplify_shift_const PROTO((rtx, enum rtx_code, enum machine_mode,
385                                        rtx, int));
386 static int recog_for_combine    PROTO((rtx *, rtx, rtx *));
387 static rtx gen_lowpart_for_combine  PROTO((enum machine_mode, rtx));
388 static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
389                                   ...));
390 static rtx gen_binary           PROTO((enum rtx_code, enum machine_mode,
391                                        rtx, rtx));
392 static rtx gen_unary            PROTO((enum rtx_code, enum machine_mode,
393                                        enum machine_mode, rtx));
394 static enum rtx_code simplify_comparison  PROTO((enum rtx_code, rtx *, rtx *));
395 static int reversible_comparison_p  PROTO((rtx));
396 static void update_table_tick   PROTO((rtx));
397 static void record_value_for_reg  PROTO((rtx, rtx, rtx));
398 static void record_dead_and_set_regs_1  PROTO((rtx, rtx));
399 static void record_dead_and_set_regs  PROTO((rtx));
400 static int get_last_value_validate  PROTO((rtx *, rtx, int, int));
401 static rtx get_last_value       PROTO((rtx));
402 static int use_crosses_set_p    PROTO((rtx, int));
403 static void reg_dead_at_p_1     PROTO((rtx, rtx));
404 static int reg_dead_at_p        PROTO((rtx, rtx));
405 static void move_deaths         PROTO((rtx, rtx, int, rtx, rtx *));
406 static int reg_bitfield_target_p  PROTO((rtx, rtx));
407 static void distribute_notes    PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
408 static void distribute_links    PROTO((rtx));
409 static void mark_used_regs_combine PROTO((rtx));
410 static int insn_cuid            PROTO((rtx));
411 \f
412 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
413    insn.  The substitution can be undone by undo_all.  If INTO is already
414    set to NEWVAL, do not record this change.  Because computing NEWVAL might
415    also call SUBST, we have to compute it before we put anything into
416    the undo table.  */
417
418 static void
419 do_SUBST(into, newval)
420      rtx *into, newval;
421 {
422   struct undo *buf;
423   rtx oldval = *into;
424
425   if (oldval == newval)
426     return;
427
428   if (undobuf.frees)
429     buf = undobuf.frees, undobuf.frees = buf->next;
430   else
431     buf = (struct undo *) xmalloc (sizeof (struct undo));
432
433   buf->is_int = 0;
434   buf->where.r = into;
435   buf->old_contents.r = oldval;
436   *into = newval;
437
438   buf->next = undobuf.undos, undobuf.undos = buf;
439 }
440
441 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
442
443 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
444    for the value of a HOST_WIDE_INT value (including CONST_INT) is
445    not safe.  */
446
447 static void
448 do_SUBST_INT(into, newval)
449      int *into, newval;
450 {
451   struct undo *buf;
452   int oldval = *into;
453
454   if (oldval == newval)
455     return;
456
457   if (undobuf.frees)
458     buf = undobuf.frees, undobuf.frees = buf->next;
459   else
460     buf = (struct undo *) xmalloc (sizeof (struct undo));
461
462   buf->is_int = 1;
463   buf->where.i = into;
464   buf->old_contents.i = oldval;
465   *into = newval;
466
467   buf->next = undobuf.undos, undobuf.undos = buf;
468 }
469
470 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
471 \f
472 /* Main entry point for combiner.  F is the first insn of the function.
473    NREGS is the first unused pseudo-reg number.  */
474
475 void
476 combine_instructions (f, nregs)
477      rtx f;
478      int nregs;
479 {
480   register rtx insn, next;
481 #ifdef HAVE_cc0
482   register rtx prev;
483 #endif
484   register int i;
485   register rtx links, nextlinks;
486
487   combine_attempts = 0;
488   combine_merges = 0;
489   combine_extras = 0;
490   combine_successes = 0;
491   undobuf.undos = undobuf.previous_undos = 0;
492
493   combine_max_regno = nregs;
494
495   reg_nonzero_bits
496     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
497   reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
498
499   bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
500   bzero (reg_sign_bit_copies, nregs * sizeof (char));
501
502   reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
503   reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
504   reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
505   reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
506   reg_last_set_label = (int *) alloca (nregs * sizeof (int));
507   reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
508   reg_last_set_mode
509     = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
510   reg_last_set_nonzero_bits
511     = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
512   reg_last_set_sign_bit_copies
513     = (char *) alloca (nregs * sizeof (char));
514
515   init_reg_last_arrays ();
516
517   init_recog_no_volatile ();
518
519   /* Compute maximum uid value so uid_cuid can be allocated.  */
520
521   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
522     if (INSN_UID (insn) > i)
523       i = INSN_UID (insn);
524
525   uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
526   max_uid_cuid = i;
527
528   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
529
530   /* Don't use reg_nonzero_bits when computing it.  This can cause problems
531      when, for example, we have j <<= 1 in a loop.  */
532
533   nonzero_sign_valid = 0;
534
535   /* Compute the mapping from uids to cuids.
536      Cuids are numbers assigned to insns, like uids,
537      except that cuids increase monotonically through the code. 
538
539      Scan all SETs and see if we can deduce anything about what
540      bits are known to be zero for some registers and how many copies
541      of the sign bit are known to exist for those registers.
542
543      Also set any known values so that we can use it while searching
544      for what bits are known to be set.  */
545
546   label_tick = 1;
547
548   /* We need to initialize it here, because record_dead_and_set_regs may call
549      get_last_value.  */
550   subst_prev_insn = NULL_RTX;
551
552   setup_incoming_promotions ();
553
554   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
555     {
556       uid_cuid[INSN_UID (insn)] = ++i;
557       subst_low_cuid = i;
558       subst_insn = insn;
559
560       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
561         {
562           note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
563           record_dead_and_set_regs (insn);
564
565 #ifdef AUTO_INC_DEC
566           for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
567             if (REG_NOTE_KIND (links) == REG_INC)
568               set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX);
569 #endif
570         }
571
572       if (GET_CODE (insn) == CODE_LABEL)
573         label_tick++;
574     }
575
576   nonzero_sign_valid = 1;
577
578   /* Now scan all the insns in forward order.  */
579
580   this_basic_block = -1;
581   label_tick = 1;
582   last_call_cuid = 0;
583   mem_last_set = 0;
584   init_reg_last_arrays ();
585   setup_incoming_promotions ();
586
587   for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
588     {
589       next = 0;
590
591       /* If INSN starts a new basic block, update our basic block number.  */
592       if (this_basic_block + 1 < n_basic_blocks
593           && BLOCK_HEAD (this_basic_block + 1) == insn)
594         this_basic_block++;
595
596       if (GET_CODE (insn) == CODE_LABEL)
597         label_tick++;
598
599       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
600         {
601           /* Try this insn with each insn it links back to.  */
602
603           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
604             if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
605               goto retry;
606
607           /* Try each sequence of three linked insns ending with this one.  */
608
609           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
610             for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
611                  nextlinks = XEXP (nextlinks, 1))
612               if ((next = try_combine (insn, XEXP (links, 0),
613                                        XEXP (nextlinks, 0))) != 0)
614                 goto retry;
615
616 #ifdef HAVE_cc0
617           /* Try to combine a jump insn that uses CC0
618              with a preceding insn that sets CC0, and maybe with its
619              logical predecessor as well.
620              This is how we make decrement-and-branch insns.
621              We need this special code because data flow connections
622              via CC0 do not get entered in LOG_LINKS.  */
623
624           if (GET_CODE (insn) == JUMP_INSN
625               && (prev = prev_nonnote_insn (insn)) != 0
626               && GET_CODE (prev) == INSN
627               && sets_cc0_p (PATTERN (prev)))
628             {
629               if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
630                 goto retry;
631
632               for (nextlinks = LOG_LINKS (prev); nextlinks;
633                    nextlinks = XEXP (nextlinks, 1))
634                 if ((next = try_combine (insn, prev,
635                                          XEXP (nextlinks, 0))) != 0)
636                   goto retry;
637             }
638
639           /* Do the same for an insn that explicitly references CC0.  */
640           if (GET_CODE (insn) == INSN
641               && (prev = prev_nonnote_insn (insn)) != 0
642               && GET_CODE (prev) == INSN
643               && sets_cc0_p (PATTERN (prev))
644               && GET_CODE (PATTERN (insn)) == SET
645               && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
646             {
647               if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
648                 goto retry;
649
650               for (nextlinks = LOG_LINKS (prev); nextlinks;
651                    nextlinks = XEXP (nextlinks, 1))
652                 if ((next = try_combine (insn, prev,
653                                          XEXP (nextlinks, 0))) != 0)
654                   goto retry;
655             }
656
657           /* Finally, see if any of the insns that this insn links to
658              explicitly references CC0.  If so, try this insn, that insn,
659              and its predecessor if it sets CC0.  */
660           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
661             if (GET_CODE (XEXP (links, 0)) == INSN
662                 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
663                 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
664                 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
665                 && GET_CODE (prev) == INSN
666                 && sets_cc0_p (PATTERN (prev))
667                 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
668               goto retry;
669 #endif
670
671           /* Try combining an insn with two different insns whose results it
672              uses.  */
673           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
674             for (nextlinks = XEXP (links, 1); nextlinks;
675                  nextlinks = XEXP (nextlinks, 1))
676               if ((next = try_combine (insn, XEXP (links, 0),
677                                        XEXP (nextlinks, 0))) != 0)
678                 goto retry;
679
680           if (GET_CODE (insn) != NOTE)
681             record_dead_and_set_regs (insn);
682
683         retry:
684           ;
685         }
686     }
687
688   total_attempts += combine_attempts;
689   total_merges += combine_merges;
690   total_extras += combine_extras;
691   total_successes += combine_successes;
692
693   nonzero_sign_valid = 0;
694
695   /* Make recognizer allow volatile MEMs again.  */
696   init_recog ();
697 }
698
699 /* Wipe the reg_last_xxx arrays in preparation for another pass.  */
700
701 static void
702 init_reg_last_arrays ()
703 {
704   int nregs = combine_max_regno;
705
706   bzero ((char *) reg_last_death, nregs * sizeof (rtx));
707   bzero ((char *) reg_last_set, nregs * sizeof (rtx));
708   bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
709   bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
710   bzero ((char *) reg_last_set_label, nregs * sizeof (int));
711   bzero (reg_last_set_invalid, nregs * sizeof (char));
712   bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
713   bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
714   bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
715 }
716 \f
717 /* Set up any promoted values for incoming argument registers.  */
718
719 static void
720 setup_incoming_promotions ()
721 {
722 #ifdef PROMOTE_FUNCTION_ARGS
723   int regno;
724   rtx reg;
725   enum machine_mode mode;
726   int unsignedp;
727   rtx first = get_insns ();
728
729   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
730     if (FUNCTION_ARG_REGNO_P (regno)
731         && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
732       {
733         record_value_for_reg
734           (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
735                                        : SIGN_EXTEND),
736                                       GET_MODE (reg),
737                                       gen_rtx_CLOBBER (mode, const0_rtx)));
738       }
739 #endif
740 }
741 \f
742 /* Called via note_stores.  If X is a pseudo that is narrower than
743    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
744
745    If we are setting only a portion of X and we can't figure out what
746    portion, assume all bits will be used since we don't know what will
747    be happening.
748
749    Similarly, set how many bits of X are known to be copies of the sign bit
750    at all locations in the function.  This is the smallest number implied 
751    by any set of X.  */
752
753 static void
754 set_nonzero_bits_and_sign_copies (x, set)
755      rtx x;
756      rtx set;
757 {
758   int num;
759
760   if (GET_CODE (x) == REG
761       && REGNO (x) >= FIRST_PSEUDO_REGISTER
762       /* If this register is undefined at the start of the file, we can't
763          say what its contents were.  */
764       && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, REGNO (x))
765       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
766     {
767       if (set == 0 || GET_CODE (set) == CLOBBER)
768         {
769           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
770           reg_sign_bit_copies[REGNO (x)] = 1;
771           return;
772         }
773
774       /* If this is a complex assignment, see if we can convert it into a
775          simple assignment.  */
776       set = expand_field_assignment (set);
777
778       /* If this is a simple assignment, or we have a paradoxical SUBREG,
779          set what we know about X.  */
780
781       if (SET_DEST (set) == x
782           || (GET_CODE (SET_DEST (set)) == SUBREG
783               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
784                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
785               && SUBREG_REG (SET_DEST (set)) == x))
786         {
787           rtx src = SET_SRC (set);
788
789 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
790           /* If X is narrower than a word and SRC is a non-negative
791              constant that would appear negative in the mode of X,
792              sign-extend it for use in reg_nonzero_bits because some
793              machines (maybe most) will actually do the sign-extension
794              and this is the conservative approach. 
795
796              ??? For 2.5, try to tighten up the MD files in this regard
797              instead of this kludge.  */
798
799           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
800               && GET_CODE (src) == CONST_INT
801               && INTVAL (src) > 0
802               && 0 != (INTVAL (src)
803                        & ((HOST_WIDE_INT) 1
804                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
805             src = GEN_INT (INTVAL (src)
806                            | ((HOST_WIDE_INT) (-1)
807                               << GET_MODE_BITSIZE (GET_MODE (x))));
808 #endif
809
810           reg_nonzero_bits[REGNO (x)]
811             |= nonzero_bits (src, nonzero_bits_mode);
812           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
813           if (reg_sign_bit_copies[REGNO (x)] == 0
814               || reg_sign_bit_copies[REGNO (x)] > num)
815             reg_sign_bit_copies[REGNO (x)] = num;
816         }
817       else
818         {
819           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
820           reg_sign_bit_copies[REGNO (x)] = 1;
821         }
822     }
823 }
824 \f
825 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
826    insns that were previously combined into I3 or that will be combined
827    into the merger of INSN and I3.
828
829    Return 0 if the combination is not allowed for any reason.
830
831    If the combination is allowed, *PDEST will be set to the single 
832    destination of INSN and *PSRC to the single source, and this function
833    will return 1.  */
834
835 static int
836 can_combine_p (insn, i3, pred, succ, pdest, psrc)
837      rtx insn;
838      rtx i3;
839      rtx pred ATTRIBUTE_UNUSED;
840      rtx succ;
841      rtx *pdest, *psrc;
842 {
843   int i;
844   rtx set = 0, src, dest;
845   rtx p;
846 #ifdef AUTO_INC_DEC
847   rtx link;
848 #endif
849   int all_adjacent = (succ ? (next_active_insn (insn) == succ
850                               && next_active_insn (succ) == i3)
851                       : next_active_insn (insn) == i3);
852
853   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
854      or a PARALLEL consisting of such a SET and CLOBBERs. 
855
856      If INSN has CLOBBER parallel parts, ignore them for our processing.
857      By definition, these happen during the execution of the insn.  When it
858      is merged with another insn, all bets are off.  If they are, in fact,
859      needed and aren't also supplied in I3, they may be added by
860      recog_for_combine.  Otherwise, it won't match. 
861
862      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
863      note.
864
865      Get the source and destination of INSN.  If more than one, can't 
866      combine.  */
867      
868   if (GET_CODE (PATTERN (insn)) == SET)
869     set = PATTERN (insn);
870   else if (GET_CODE (PATTERN (insn)) == PARALLEL
871            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
872     {
873       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
874         {
875           rtx elt = XVECEXP (PATTERN (insn), 0, i);
876
877           switch (GET_CODE (elt))
878             {
879             /* This is important to combine floating point insns
880                for the SH4 port.  */
881             case USE:
882               /* Combining an isolated USE doesn't make sense.
883                  We depend here on combinable_i3_pat to reject them.  */
884               /* The code below this loop only verifies that the inputs of
885                  the SET in INSN do not change.  We call reg_set_between_p
886                  to verify that the REG in the USE does not change betweeen
887                  I3 and INSN.
888                  If the USE in INSN was for a pseudo register, the matching
889                  insn pattern will likely match any register; combining this
890                  with any other USE would only be safe if we knew that the
891                  used registers have identical values, or if there was
892                  something to tell them apart, e.g. different modes.  For
893                  now, we forgo such compilcated tests and simply disallow
894                  combining of USES of pseudo registers with any other USE.  */
895               if (GET_CODE (XEXP (elt, 0)) == REG
896                   && GET_CODE (PATTERN (i3)) == PARALLEL)
897                 {
898                   rtx i3pat = PATTERN (i3);
899                   int i = XVECLEN (i3pat, 0) - 1;
900                   int regno = REGNO (XEXP (elt, 0));
901                   do
902                     {
903                       rtx i3elt = XVECEXP (i3pat, 0, i);
904                       if (GET_CODE (i3elt) == USE
905                           && GET_CODE (XEXP (i3elt, 0)) == REG
906                           && (REGNO (XEXP (i3elt, 0)) == regno
907                               ? reg_set_between_p (XEXP (elt, 0),
908                                                    PREV_INSN (insn), i3)
909                               : regno >= FIRST_PSEUDO_REGISTER))
910                         return 0;
911                     }
912                   while (--i >= 0);
913                 }
914               break;
915
916               /* We can ignore CLOBBERs.  */
917             case CLOBBER:
918               break;
919
920             case SET:
921               /* Ignore SETs whose result isn't used but not those that
922                  have side-effects.  */
923               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
924                   && ! side_effects_p (elt))
925                 break;
926
927               /* If we have already found a SET, this is a second one and
928                  so we cannot combine with this insn.  */
929               if (set)
930                 return 0;
931
932               set = elt;
933               break;
934
935             default:
936               /* Anything else means we can't combine.  */
937               return 0;
938             }
939         }
940
941       if (set == 0
942           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
943              so don't do anything with it.  */
944           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
945         return 0;
946     }
947   else
948     return 0;
949
950   if (set == 0)
951     return 0;
952
953   set = expand_field_assignment (set);
954   src = SET_SRC (set), dest = SET_DEST (set);
955
956   /* Don't eliminate a store in the stack pointer.  */
957   if (dest == stack_pointer_rtx
958       /* If we couldn't eliminate a field assignment, we can't combine.  */
959       || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
960       /* Don't combine with an insn that sets a register to itself if it has
961          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
962       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
963       /* Can't merge a function call.  */
964       || GET_CODE (src) == CALL
965       /* Don't eliminate a function call argument.  */
966       || (GET_CODE (i3) == CALL_INSN
967           && (find_reg_fusage (i3, USE, dest)
968               || (GET_CODE (dest) == REG
969                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
970                   && global_regs[REGNO (dest)])))
971       /* Don't substitute into an incremented register.  */
972       || FIND_REG_INC_NOTE (i3, dest)
973       || (succ && FIND_REG_INC_NOTE (succ, dest))
974 #if 0
975       /* Don't combine the end of a libcall into anything.  */
976       /* ??? This gives worse code, and appears to be unnecessary, since no
977          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  Local-alloc does
978          use REG_RETVAL notes for noconflict blocks, but other code here
979          makes sure that those insns don't disappear.  */
980       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
981 #endif
982       /* Make sure that DEST is not used after SUCC but before I3.  */
983       || (succ && ! all_adjacent
984           && reg_used_between_p (dest, succ, i3))
985       /* Make sure that the value that is to be substituted for the register
986          does not use any registers whose values alter in between.  However,
987          If the insns are adjacent, a use can't cross a set even though we
988          think it might (this can happen for a sequence of insns each setting
989          the same destination; reg_last_set of that register might point to
990          a NOTE).  If INSN has a REG_EQUIV note, the register is always
991          equivalent to the memory so the substitution is valid even if there
992          are intervening stores.  Also, don't move a volatile asm or
993          UNSPEC_VOLATILE across any other insns.  */
994       || (! all_adjacent
995           && (((GET_CODE (src) != MEM
996                 || ! find_reg_note (insn, REG_EQUIV, src))
997                && use_crosses_set_p (src, INSN_CUID (insn)))
998               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
999               || GET_CODE (src) == UNSPEC_VOLATILE))
1000       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1001          better register allocation by not doing the combine.  */
1002       || find_reg_note (i3, REG_NO_CONFLICT, dest)
1003       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1004       /* Don't combine across a CALL_INSN, because that would possibly
1005          change whether the life span of some REGs crosses calls or not,
1006          and it is a pain to update that information.
1007          Exception: if source is a constant, moving it later can't hurt.
1008          Accept that special case, because it helps -fforce-addr a lot.  */
1009       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1010     return 0;
1011
1012   /* DEST must either be a REG or CC0.  */
1013   if (GET_CODE (dest) == REG)
1014     {
1015       /* If register alignment is being enforced for multi-word items in all
1016          cases except for parameters, it is possible to have a register copy
1017          insn referencing a hard register that is not allowed to contain the
1018          mode being copied and which would not be valid as an operand of most
1019          insns.  Eliminate this problem by not combining with such an insn.
1020
1021          Also, on some machines we don't want to extend the life of a hard
1022          register.
1023
1024          This is the same test done in can_combine except that we don't test
1025          if SRC is a CALL operation to permit a hard register with
1026          SMALL_REGISTER_CLASSES, and that we have to take all_adjacent
1027          into account.  */
1028
1029       if (GET_CODE (src) == REG
1030           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1031                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1032               /* Don't extend the life of a hard register unless it is
1033                  user variable (if we have few registers) or it can't
1034                  fit into the desired register (meaning something special
1035                  is going on).
1036                  Also avoid substituting a return register into I3, because
1037                  reload can't handle a conflict with constraints of other
1038                  inputs.  */
1039               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1040                   && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
1041                       || (SMALL_REGISTER_CLASSES
1042                           && ((! all_adjacent && ! REG_USERVAR_P (src))
1043                               || (FUNCTION_VALUE_REGNO_P (REGNO (src))
1044                                   && ! REG_USERVAR_P (src))))))))
1045         return 0;
1046     }
1047   else if (GET_CODE (dest) != CC0)
1048     return 0;
1049
1050   /* Don't substitute for a register intended as a clobberable operand.
1051      Similarly, don't substitute an expression containing a register that
1052      will be clobbered in I3.  */
1053   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1054     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1055       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1056           && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1057                                        src)
1058               || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1059         return 0;
1060
1061   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1062      or not), reject, unless nothing volatile comes between it and I3 */
1063
1064   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1065     {
1066       /* Make sure succ doesn't contain a volatile reference.  */
1067       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1068         return 0;
1069   
1070       for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1071         if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1072           && p != succ && volatile_refs_p (PATTERN (p)))
1073         return 0;
1074     }
1075
1076   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1077      to be an explicit register variable, and was chosen for a reason.  */
1078
1079   if (GET_CODE (src) == ASM_OPERANDS
1080       && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1081     return 0;
1082
1083   /* If there are any volatile insns between INSN and I3, reject, because
1084      they might affect machine state.  */
1085
1086   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1087     if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1088         && p != succ && volatile_insn_p (PATTERN (p)))
1089       return 0;
1090
1091   /* If INSN or I2 contains an autoincrement or autodecrement,
1092      make sure that register is not used between there and I3,
1093      and not already used in I3 either.
1094      Also insist that I3 not be a jump; if it were one
1095      and the incremented register were spilled, we would lose.  */
1096
1097 #ifdef AUTO_INC_DEC
1098   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1099     if (REG_NOTE_KIND (link) == REG_INC
1100         && (GET_CODE (i3) == JUMP_INSN
1101             || reg_used_between_p (XEXP (link, 0), insn, i3)
1102             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1103       return 0;
1104 #endif
1105
1106 #ifdef HAVE_cc0
1107   /* Don't combine an insn that follows a CC0-setting insn.
1108      An insn that uses CC0 must not be separated from the one that sets it.
1109      We do, however, allow I2 to follow a CC0-setting insn if that insn
1110      is passed as I1; in that case it will be deleted also.
1111      We also allow combining in this case if all the insns are adjacent
1112      because that would leave the two CC0 insns adjacent as well.
1113      It would be more logical to test whether CC0 occurs inside I1 or I2,
1114      but that would be much slower, and this ought to be equivalent.  */
1115
1116   p = prev_nonnote_insn (insn);
1117   if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1118       && ! all_adjacent)
1119     return 0;
1120 #endif
1121
1122   /* If we get here, we have passed all the tests and the combination is
1123      to be allowed.  */
1124
1125   *pdest = dest;
1126   *psrc = src;
1127
1128   return 1;
1129 }
1130 \f
1131 /* Check if PAT is an insn - or a part of it - used to set up an
1132    argument for a function in a hard register.  */
1133
1134 static int
1135 sets_function_arg_p (pat)
1136      rtx pat;
1137 {
1138   int i;
1139   rtx inner_dest;
1140
1141   switch (GET_CODE (pat))
1142     {
1143     case INSN:
1144       return sets_function_arg_p (PATTERN (pat));
1145
1146     case PARALLEL:
1147       for (i = XVECLEN (pat, 0); --i >= 0;)
1148         if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1149           return 1;
1150
1151       break;
1152
1153     case SET:
1154       inner_dest = SET_DEST (pat);
1155       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1156              || GET_CODE (inner_dest) == SUBREG
1157              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1158         inner_dest = XEXP (inner_dest, 0);
1159
1160       return (GET_CODE (inner_dest) == REG
1161               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1162               && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1163
1164     default:
1165       break;
1166     }
1167
1168   return 0;
1169 }
1170
1171 /* LOC is the location within I3 that contains its pattern or the component
1172    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1173
1174    One problem is if I3 modifies its output, as opposed to replacing it
1175    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1176    so would produce an insn that is not equivalent to the original insns.
1177
1178    Consider:
1179
1180          (set (reg:DI 101) (reg:DI 100))
1181          (set (subreg:SI (reg:DI 101) 0) <foo>)
1182
1183    This is NOT equivalent to:
1184
1185          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1186                     (set (reg:DI 101) (reg:DI 100))])
1187
1188    Not only does this modify 100 (in which case it might still be valid
1189    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100. 
1190
1191    We can also run into a problem if I2 sets a register that I1
1192    uses and I1 gets directly substituted into I3 (not via I2).  In that
1193    case, we would be getting the wrong value of I2DEST into I3, so we
1194    must reject the combination.  This case occurs when I2 and I1 both
1195    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1196    If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1197    of a SET must prevent combination from occurring.
1198
1199    On machines where SMALL_REGISTER_CLASSES is non-zero, we don't combine
1200    if the destination of a SET is a hard register that isn't a user
1201    variable.
1202
1203    Before doing the above check, we first try to expand a field assignment
1204    into a set of logical operations.
1205
1206    If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1207    we place a register that is both set and used within I3.  If more than one
1208    such register is detected, we fail.
1209
1210    Return 1 if the combination is valid, zero otherwise.  */
1211
1212 static int
1213 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1214      rtx i3;
1215      rtx *loc;
1216      rtx i2dest;
1217      rtx i1dest;
1218      int i1_not_in_src;
1219      rtx *pi3dest_killed;
1220 {
1221   rtx x = *loc;
1222
1223   if (GET_CODE (x) == SET)
1224     {
1225       rtx set = expand_field_assignment (x);
1226       rtx dest = SET_DEST (set);
1227       rtx src = SET_SRC (set);
1228       rtx inner_dest = dest;
1229  
1230 #if 0
1231       rtx inner_src = src;
1232 #endif
1233
1234       SUBST (*loc, set);
1235
1236       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1237              || GET_CODE (inner_dest) == SUBREG
1238              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1239         inner_dest = XEXP (inner_dest, 0);
1240
1241   /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1242      was added.  */
1243 #if 0
1244       while (GET_CODE (inner_src) == STRICT_LOW_PART
1245              || GET_CODE (inner_src) == SUBREG
1246              || GET_CODE (inner_src) == ZERO_EXTRACT)
1247         inner_src = XEXP (inner_src, 0);
1248
1249       /* If it is better that two different modes keep two different pseudos,
1250          avoid combining them.  This avoids producing the following pattern
1251          on a 386:
1252           (set (subreg:SI (reg/v:QI 21) 0)
1253                (lshiftrt:SI (reg/v:SI 20)
1254                    (const_int 24)))
1255          If that were made, reload could not handle the pair of
1256          reg 20/21, since it would try to get any GENERAL_REGS
1257          but some of them don't handle QImode.  */
1258
1259       if (rtx_equal_p (inner_src, i2dest)
1260           && GET_CODE (inner_dest) == REG
1261           && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1262         return 0;
1263 #endif
1264
1265       /* Check for the case where I3 modifies its output, as
1266          discussed above.  */
1267       if ((inner_dest != dest
1268            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1269                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1270
1271           /* This is the same test done in can_combine_p except that we
1272              allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
1273              CALL operation. Moreover, we can't test all_adjacent; we don't
1274              have to, since this instruction will stay in place, thus we are
1275              not considering increasing the lifetime of INNER_DEST.
1276
1277              Also, if this insn sets a function argument, combining it with
1278              something that might need a spill could clobber a previous
1279              function argument; the all_adjacent test in can_combine_p also
1280              checks this; here, we do a more specific test for this case.  */
1281              
1282           || (GET_CODE (inner_dest) == REG
1283               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1284               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1285                                         GET_MODE (inner_dest))
1286                  || (SMALL_REGISTER_CLASSES && GET_CODE (src) != CALL
1287                      && ! REG_USERVAR_P (inner_dest)
1288                      && (FUNCTION_VALUE_REGNO_P (REGNO (inner_dest))
1289                          || (FUNCTION_ARG_REGNO_P (REGNO (inner_dest))
1290                              && i3 != 0
1291                              && sets_function_arg_p (prev_nonnote_insn (i3)))))))
1292           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1293         return 0;
1294
1295       /* If DEST is used in I3, it is being killed in this insn,
1296          so record that for later. 
1297          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1298          STACK_POINTER_REGNUM, since these are always considered to be
1299          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1300       if (pi3dest_killed && GET_CODE (dest) == REG
1301           && reg_referenced_p (dest, PATTERN (i3))
1302           && REGNO (dest) != FRAME_POINTER_REGNUM
1303 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1304           && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1305 #endif
1306 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1307           && (REGNO (dest) != ARG_POINTER_REGNUM
1308               || ! fixed_regs [REGNO (dest)])
1309 #endif
1310           && REGNO (dest) != STACK_POINTER_REGNUM)
1311         {
1312           if (*pi3dest_killed)
1313             return 0;
1314
1315           *pi3dest_killed = dest;
1316         }
1317     }
1318
1319   else if (GET_CODE (x) == PARALLEL)
1320     {
1321       int i;
1322
1323       for (i = 0; i < XVECLEN (x, 0); i++)
1324         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1325                                 i1_not_in_src, pi3dest_killed))
1326           return 0;
1327     }
1328
1329   return 1;
1330 }
1331 \f
1332 /* Try to combine the insns I1 and I2 into I3.
1333    Here I1 and I2 appear earlier than I3.
1334    I1 can be zero; then we combine just I2 into I3.
1335  
1336    It we are combining three insns and the resulting insn is not recognized,
1337    try splitting it into two insns.  If that happens, I2 and I3 are retained
1338    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1339    are pseudo-deleted.
1340
1341    Return 0 if the combination does not work.  Then nothing is changed. 
1342    If we did the combination, return the insn at which combine should
1343    resume scanning.  */
1344
1345 static rtx
1346 try_combine (i3, i2, i1)
1347      register rtx i3, i2, i1;
1348 {
1349   /* New patterns for I3 and I3, respectively.  */
1350   rtx newpat, newi2pat = 0;
1351   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1352   int added_sets_1, added_sets_2;
1353   /* Total number of SETs to put into I3.  */
1354   int total_sets;
1355   /* Nonzero is I2's body now appears in I3.  */
1356   int i2_is_used;
1357   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1358   int insn_code_number, i2_code_number = 0, other_code_number = 0;
1359   /* Contains I3 if the destination of I3 is used in its source, which means
1360      that the old life of I3 is being killed.  If that usage is placed into
1361      I2 and not in I3, a REG_DEAD note must be made.  */
1362   rtx i3dest_killed = 0;
1363   /* SET_DEST and SET_SRC of I2 and I1.  */
1364   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1365   /* PATTERN (I2), or a copy of it in certain cases.  */
1366   rtx i2pat;
1367   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1368   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1369   int i1_feeds_i3 = 0;
1370   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1371   rtx new_i3_notes, new_i2_notes;
1372   /* Notes that we substituted I3 into I2 instead of the normal case.  */
1373   int i3_subst_into_i2 = 0;
1374   /* Notes that I1, I2 or I3 is a MULT operation.  */
1375   int have_mult = 0;
1376
1377   int maxreg;
1378   rtx temp;
1379   register rtx link;
1380   int i;
1381
1382   /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1383      This can occur when flow deletes an insn that it has merged into an
1384      auto-increment address.  We also can't do anything if I3 has a
1385      REG_LIBCALL note since we don't want to disrupt the contiguity of a
1386      libcall.  */
1387
1388   if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1389       || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1390       || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
1391 #if 0
1392       /* ??? This gives worse code, and appears to be unnecessary, since no
1393          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  */
1394       || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1395 #endif
1396 )
1397     return 0;
1398
1399   combine_attempts++;
1400
1401   undobuf.undos = undobuf.previous_undos = 0;
1402   undobuf.other_insn = 0;
1403
1404   /* Save the current high-water-mark so we can free storage if we didn't
1405      accept this combination.  */
1406   undobuf.storage = (char *) oballoc (0);
1407
1408   /* Reset the hard register usage information.  */
1409   CLEAR_HARD_REG_SET (newpat_used_regs);
1410
1411   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1412      code below, set I1 to be the earlier of the two insns.  */
1413   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1414     temp = i1, i1 = i2, i2 = temp;
1415
1416   added_links_insn = 0;
1417
1418   /* First check for one important special-case that the code below will
1419      not handle.  Namely, the case where I1 is zero, I2 has multiple sets,
1420      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1421      we may be able to replace that destination with the destination of I3.
1422      This occurs in the common code where we compute both a quotient and
1423      remainder into a structure, in which case we want to do the computation
1424      directly into the structure to avoid register-register copies.
1425
1426      We make very conservative checks below and only try to handle the
1427      most common cases of this.  For example, we only handle the case
1428      where I2 and I3 are adjacent to avoid making difficult register
1429      usage tests.  */
1430
1431   if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1432       && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1433       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1434       && (! SMALL_REGISTER_CLASSES
1435           || (GET_CODE (SET_DEST (PATTERN (i3))) != REG
1436               || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1437               || REG_USERVAR_P (SET_DEST (PATTERN (i3)))))
1438       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1439       && GET_CODE (PATTERN (i2)) == PARALLEL
1440       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1441       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1442          below would need to check what is inside (and reg_overlap_mentioned_p
1443          doesn't support those codes anyway).  Don't allow those destinations;
1444          the resulting insn isn't likely to be recognized anyway.  */
1445       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1446       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1447       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1448                                     SET_DEST (PATTERN (i3)))
1449       && next_real_insn (i2) == i3)
1450     {
1451       rtx p2 = PATTERN (i2);
1452
1453       /* Make sure that the destination of I3,
1454          which we are going to substitute into one output of I2,
1455          is not used within another output of I2.  We must avoid making this:
1456          (parallel [(set (mem (reg 69)) ...)
1457                     (set (reg 69) ...)])
1458          which is not well-defined as to order of actions.
1459          (Besides, reload can't handle output reloads for this.)
1460
1461          The problem can also happen if the dest of I3 is a memory ref,
1462          if another dest in I2 is an indirect memory ref.  */
1463       for (i = 0; i < XVECLEN (p2, 0); i++)
1464         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1465              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1466             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1467                                         SET_DEST (XVECEXP (p2, 0, i))))
1468           break;
1469
1470       if (i == XVECLEN (p2, 0))
1471         for (i = 0; i < XVECLEN (p2, 0); i++)
1472           if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1473             {
1474               combine_merges++;
1475
1476               subst_insn = i3;
1477               subst_low_cuid = INSN_CUID (i2);
1478
1479               added_sets_2 = added_sets_1 = 0;
1480               i2dest = SET_SRC (PATTERN (i3));
1481
1482               /* Replace the dest in I2 with our dest and make the resulting
1483                  insn the new pattern for I3.  Then skip to where we
1484                  validate the pattern.  Everything was set up above.  */
1485               SUBST (SET_DEST (XVECEXP (p2, 0, i)), 
1486                      SET_DEST (PATTERN (i3)));
1487
1488               newpat = p2;
1489               i3_subst_into_i2 = 1;
1490               goto validate_replacement;
1491             }
1492     }
1493
1494 #ifndef HAVE_cc0
1495   /* If we have no I1 and I2 looks like:
1496         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1497                    (set Y OP)])
1498      make up a dummy I1 that is
1499         (set Y OP)
1500      and change I2 to be
1501         (set (reg:CC X) (compare:CC Y (const_int 0)))
1502
1503      (We can ignore any trailing CLOBBERs.)
1504
1505      This undoes a previous combination and allows us to match a branch-and-
1506      decrement insn.  */
1507
1508   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1509       && XVECLEN (PATTERN (i2), 0) >= 2
1510       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1511       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1512           == MODE_CC)
1513       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1514       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1515       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1516       && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1517       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1518                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1519     {
1520       for (i =  XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1521         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1522           break;
1523
1524       if (i == 1)
1525         {
1526           /* We make I1 with the same INSN_UID as I2.  This gives it
1527              the same INSN_CUID for value tracking.  Our fake I1 will
1528              never appear in the insn stream so giving it the same INSN_UID
1529              as I2 will not cause a problem.  */
1530
1531           subst_prev_insn = i1
1532             = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1533                             XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1534                             NULL_RTX);
1535
1536           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1537           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1538                  SET_DEST (PATTERN (i1)));
1539         }
1540     }
1541 #endif
1542
1543   /* Verify that I2 and I1 are valid for combining.  */
1544   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1545       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1546     {
1547       undo_all ();
1548       return 0;
1549     }
1550
1551   /* Record whether I2DEST is used in I2SRC and similarly for the other
1552      cases.  Knowing this will help in register status updating below.  */
1553   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1554   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1555   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1556
1557   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
1558      in I2SRC.  */
1559   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1560
1561   /* Ensure that I3's pattern can be the destination of combines.  */
1562   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1563                           i1 && i2dest_in_i1src && i1_feeds_i3,
1564                           &i3dest_killed))
1565     {
1566       undo_all ();
1567       return 0;
1568     }
1569
1570   /* See if any of the insns is a MULT operation.  Unless one is, we will
1571      reject a combination that is, since it must be slower.  Be conservative
1572      here.  */
1573   if (GET_CODE (i2src) == MULT
1574       || (i1 != 0 && GET_CODE (i1src) == MULT)
1575       || (GET_CODE (PATTERN (i3)) == SET
1576           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1577     have_mult = 1;
1578
1579   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1580      We used to do this EXCEPT in one case: I3 has a post-inc in an
1581      output operand.  However, that exception can give rise to insns like
1582         mov r3,(r3)+
1583      which is a famous insn on the PDP-11 where the value of r3 used as the
1584      source was model-dependent.  Avoid this sort of thing.  */
1585
1586 #if 0
1587   if (!(GET_CODE (PATTERN (i3)) == SET
1588         && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1589         && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1590         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1591             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1592     /* It's not the exception.  */
1593 #endif
1594 #ifdef AUTO_INC_DEC
1595     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1596       if (REG_NOTE_KIND (link) == REG_INC
1597           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1598               || (i1 != 0
1599                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1600         {
1601           undo_all ();
1602           return 0;
1603         }
1604 #endif
1605
1606   /* See if the SETs in I1 or I2 need to be kept around in the merged
1607      instruction: whenever the value set there is still needed past I3.
1608      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1609
1610      For the SET in I1, we have two cases:  If I1 and I2 independently
1611      feed into I3, the set in I1 needs to be kept around if I1DEST dies
1612      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
1613      in I1 needs to be kept around unless I1DEST dies or is set in either
1614      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
1615      I1DEST.  If so, we know I1 feeds into I2.  */
1616
1617   added_sets_2 = ! dead_or_set_p (i3, i2dest);
1618
1619   added_sets_1
1620     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1621                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1622
1623   /* If the set in I2 needs to be kept around, we must make a copy of
1624      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1625      PATTERN (I2), we are only substituting for the original I1DEST, not into
1626      an already-substituted copy.  This also prevents making self-referential
1627      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1628      I2DEST.  */
1629
1630   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1631            ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1632            : PATTERN (i2));
1633
1634   if (added_sets_2)
1635     i2pat = copy_rtx (i2pat);
1636
1637   combine_merges++;
1638
1639   /* Substitute in the latest insn for the regs set by the earlier ones.  */
1640
1641   maxreg = max_reg_num ();
1642
1643   subst_insn = i3;
1644
1645   /* It is possible that the source of I2 or I1 may be performing an
1646      unneeded operation, such as a ZERO_EXTEND of something that is known
1647      to have the high part zero.  Handle that case by letting subst look at
1648      the innermost one of them.
1649
1650      Another way to do this would be to have a function that tries to
1651      simplify a single insn instead of merging two or more insns.  We don't
1652      do this because of the potential of infinite loops and because
1653      of the potential extra memory required.  However, doing it the way
1654      we are is a bit of a kludge and doesn't catch all cases.
1655
1656      But only do this if -fexpensive-optimizations since it slows things down
1657      and doesn't usually win.  */
1658
1659   if (flag_expensive_optimizations)
1660     {
1661       /* Pass pc_rtx so no substitutions are done, just simplifications.
1662          The cases that we are interested in here do not involve the few
1663          cases were is_replaced is checked.  */
1664       if (i1)
1665         {
1666           subst_low_cuid = INSN_CUID (i1);
1667           i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1668         }
1669       else
1670         {
1671           subst_low_cuid = INSN_CUID (i2);
1672           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1673         }
1674
1675       undobuf.previous_undos = undobuf.undos;
1676     }
1677
1678 #ifndef HAVE_cc0
1679   /* Many machines that don't use CC0 have insns that can both perform an
1680      arithmetic operation and set the condition code.  These operations will
1681      be represented as a PARALLEL with the first element of the vector
1682      being a COMPARE of an arithmetic operation with the constant zero.
1683      The second element of the vector will set some pseudo to the result
1684      of the same arithmetic operation.  If we simplify the COMPARE, we won't
1685      match such a pattern and so will generate an extra insn.   Here we test
1686      for this case, where both the comparison and the operation result are
1687      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1688      I2SRC.  Later we will make the PARALLEL that contains I2.  */
1689
1690   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1691       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1692       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1693       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1694     {
1695 #ifdef EXTRA_CC_MODES
1696       rtx *cc_use;
1697       enum machine_mode compare_mode;
1698 #endif
1699
1700       newpat = PATTERN (i3);
1701       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1702
1703       i2_is_used = 1;
1704
1705 #ifdef EXTRA_CC_MODES
1706       /* See if a COMPARE with the operand we substituted in should be done
1707          with the mode that is currently being used.  If not, do the same
1708          processing we do in `subst' for a SET; namely, if the destination
1709          is used only once, try to replace it with a register of the proper
1710          mode and also replace the COMPARE.  */
1711       if (undobuf.other_insn == 0
1712           && (cc_use = find_single_use (SET_DEST (newpat), i3,
1713                                         &undobuf.other_insn))
1714           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1715                                               i2src, const0_rtx))
1716               != GET_MODE (SET_DEST (newpat))))
1717         {
1718           int regno = REGNO (SET_DEST (newpat));
1719           rtx new_dest = gen_rtx_REG (compare_mode, regno);
1720
1721           if (regno < FIRST_PSEUDO_REGISTER
1722               || (REG_N_SETS (regno) == 1 && ! added_sets_2
1723                   && ! REG_USERVAR_P (SET_DEST (newpat))))
1724             {
1725               if (regno >= FIRST_PSEUDO_REGISTER)
1726                 SUBST (regno_reg_rtx[regno], new_dest);
1727
1728               SUBST (SET_DEST (newpat), new_dest);
1729               SUBST (XEXP (*cc_use, 0), new_dest);
1730               SUBST (SET_SRC (newpat),
1731                      gen_rtx_combine (COMPARE, compare_mode,
1732                                       i2src, const0_rtx));
1733             }
1734           else
1735             undobuf.other_insn = 0;
1736         }
1737 #endif    
1738     }
1739   else
1740 #endif
1741     {
1742       n_occurrences = 0;                /* `subst' counts here */
1743
1744       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1745          need to make a unique copy of I2SRC each time we substitute it
1746          to avoid self-referential rtl.  */
1747
1748       subst_low_cuid = INSN_CUID (i2);
1749       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1750                       ! i1_feeds_i3 && i1dest_in_i1src);
1751       undobuf.previous_undos = undobuf.undos;
1752
1753       /* Record whether i2's body now appears within i3's body.  */
1754       i2_is_used = n_occurrences;
1755     }
1756
1757   /* If we already got a failure, don't try to do more.  Otherwise,
1758      try to substitute in I1 if we have it.  */
1759
1760   if (i1 && GET_CODE (newpat) != CLOBBER)
1761     {
1762       /* Before we can do this substitution, we must redo the test done
1763          above (see detailed comments there) that ensures  that I1DEST
1764          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
1765
1766       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1767                               0, NULL_PTR))
1768         {
1769           undo_all ();
1770           return 0;
1771         }
1772
1773       n_occurrences = 0;
1774       subst_low_cuid = INSN_CUID (i1);
1775       newpat = subst (newpat, i1dest, i1src, 0, 0);
1776       undobuf.previous_undos = undobuf.undos;
1777     }
1778
1779   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
1780      to count all the ways that I2SRC and I1SRC can be used.  */
1781   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1782        && i2_is_used + added_sets_2 > 1)
1783       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1784           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1785               > 1))
1786       /* Fail if we tried to make a new register (we used to abort, but there's
1787          really no reason to).  */
1788       || max_reg_num () != maxreg
1789       /* Fail if we couldn't do something and have a CLOBBER.  */
1790       || GET_CODE (newpat) == CLOBBER
1791       /* Fail if this new pattern is a MULT and we didn't have one before
1792          at the outer level.  */
1793       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1794           && ! have_mult))
1795     {
1796       undo_all ();
1797       return 0;
1798     }
1799
1800   /* If the actions of the earlier insns must be kept
1801      in addition to substituting them into the latest one,
1802      we must make a new PARALLEL for the latest insn
1803      to hold additional the SETs.  */
1804
1805   if (added_sets_1 || added_sets_2)
1806     {
1807       combine_extras++;
1808
1809       if (GET_CODE (newpat) == PARALLEL)
1810         {
1811           rtvec old = XVEC (newpat, 0);
1812           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1813           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1814           bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
1815                  sizeof (old->elem[0]) * old->num_elem);
1816         }
1817       else
1818         {
1819           rtx old = newpat;
1820           total_sets = 1 + added_sets_1 + added_sets_2;
1821           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1822           XVECEXP (newpat, 0, 0) = old;
1823         }
1824
1825      if (added_sets_1)
1826        XVECEXP (newpat, 0, --total_sets)
1827          = (GET_CODE (PATTERN (i1)) == PARALLEL
1828             ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
1829
1830      if (added_sets_2)
1831        {
1832          /* If there is no I1, use I2's body as is.  We used to also not do
1833             the subst call below if I2 was substituted into I3,
1834             but that could lose a simplification.  */
1835          if (i1 == 0)
1836            XVECEXP (newpat, 0, --total_sets) = i2pat;
1837          else
1838            /* See comment where i2pat is assigned.  */
1839            XVECEXP (newpat, 0, --total_sets)
1840              = subst (i2pat, i1dest, i1src, 0, 0);
1841        }
1842     }
1843
1844   /* We come here when we are replacing a destination in I2 with the
1845      destination of I3.  */
1846  validate_replacement:
1847
1848   /* Note which hard regs this insn has as inputs.  */
1849   mark_used_regs_combine (newpat);
1850
1851   /* Is the result of combination a valid instruction?  */
1852   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1853
1854   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1855      the second SET's destination is a register that is unused.  In that case,
1856      we just need the first SET.   This can occur when simplifying a divmod
1857      insn.  We *must* test for this case here because the code below that
1858      splits two independent SETs doesn't handle this case correctly when it
1859      updates the register status.  Also check the case where the first
1860      SET's destination is unused.  That would not cause incorrect code, but
1861      does cause an unneeded insn to remain.  */
1862
1863   if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1864       && XVECLEN (newpat, 0) == 2
1865       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1866       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1867       && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1868       && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1869       && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1870       && asm_noperands (newpat) < 0)
1871     {
1872       newpat = XVECEXP (newpat, 0, 0);
1873       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1874     }
1875
1876   else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1877            && XVECLEN (newpat, 0) == 2
1878            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1879            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1880            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1881            && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1882            && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1883            && asm_noperands (newpat) < 0)
1884     {
1885       newpat = XVECEXP (newpat, 0, 1);
1886       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1887     }
1888
1889   /* If we were combining three insns and the result is a simple SET
1890      with no ASM_OPERANDS that wasn't recognized, try to split it into two
1891      insns.  There are two ways to do this.  It can be split using a 
1892      machine-specific method (like when you have an addition of a large
1893      constant) or by combine in the function find_split_point.  */
1894
1895   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1896       && asm_noperands (newpat) < 0)
1897     {
1898       rtx m_split, *split;
1899       rtx ni2dest = i2dest;
1900
1901       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
1902          use I2DEST as a scratch register will help.  In the latter case,
1903          convert I2DEST to the mode of the source of NEWPAT if we can.  */
1904
1905       m_split = split_insns (newpat, i3);
1906
1907       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1908          inputs of NEWPAT.  */
1909
1910       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1911          possible to try that as a scratch reg.  This would require adding
1912          more code to make it work though.  */
1913
1914       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
1915         {
1916           /* If I2DEST is a hard register or the only use of a pseudo,
1917              we can change its mode.  */
1918           if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
1919               && GET_MODE (SET_DEST (newpat)) != VOIDmode
1920               && GET_CODE (i2dest) == REG
1921               && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1922                   || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
1923                       && ! REG_USERVAR_P (i2dest))))
1924             ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
1925                                    REGNO (i2dest));
1926
1927           m_split = split_insns (gen_rtx_PARALLEL
1928                                  (VOIDmode,
1929                                   gen_rtvec (2, newpat,
1930                                              gen_rtx_CLOBBER (VOIDmode,
1931                                                               ni2dest))),
1932                                  i3);
1933         }
1934
1935       if (m_split && GET_CODE (m_split) == SEQUENCE
1936           && XVECLEN (m_split, 0) == 2
1937           && (next_real_insn (i2) == i3
1938               || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1939                                       INSN_CUID (i2))))
1940         {
1941           rtx i2set, i3set;
1942           rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
1943           newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
1944
1945           i3set = single_set (XVECEXP (m_split, 0, 1));
1946           i2set = single_set (XVECEXP (m_split, 0, 0));
1947
1948           /* In case we changed the mode of I2DEST, replace it in the
1949              pseudo-register table here.  We can't do it above in case this
1950              code doesn't get executed and we do a split the other way.  */
1951
1952           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1953             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1954
1955           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
1956
1957           /* If I2 or I3 has multiple SETs, we won't know how to track
1958              register status, so don't use these insns.  If I2's destination
1959              is used between I2 and I3, we also can't use these insns.  */
1960
1961           if (i2_code_number >= 0 && i2set && i3set
1962               && (next_real_insn (i2) == i3
1963                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
1964             insn_code_number = recog_for_combine (&newi3pat, i3,
1965                                                   &new_i3_notes);
1966           if (insn_code_number >= 0)
1967             newpat = newi3pat;
1968
1969           /* It is possible that both insns now set the destination of I3.
1970              If so, we must show an extra use of it.  */
1971
1972           if (insn_code_number >= 0)
1973             {
1974               rtx new_i3_dest = SET_DEST (i3set);
1975               rtx new_i2_dest = SET_DEST (i2set);
1976
1977               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
1978                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
1979                      || GET_CODE (new_i3_dest) == SUBREG)
1980                 new_i3_dest = XEXP (new_i3_dest, 0);
1981
1982               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
1983                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
1984                      || GET_CODE (new_i2_dest) == SUBREG)
1985                 new_i2_dest = XEXP (new_i2_dest, 0);
1986
1987               if (GET_CODE (new_i3_dest) == REG
1988                   && GET_CODE (new_i2_dest) == REG
1989                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
1990                 REG_N_SETS (REGNO (new_i2_dest))++;
1991             }
1992         }
1993
1994       /* If we can split it and use I2DEST, go ahead and see if that
1995          helps things be recognized.  Verify that none of the registers
1996          are set between I2 and I3.  */
1997       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
1998 #ifdef HAVE_cc0
1999           && GET_CODE (i2dest) == REG
2000 #endif
2001           /* We need I2DEST in the proper mode.  If it is a hard register
2002              or the only use of a pseudo, we can change its mode.  */
2003           && (GET_MODE (*split) == GET_MODE (i2dest)
2004               || GET_MODE (*split) == VOIDmode
2005               || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2006               || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2007                   && ! REG_USERVAR_P (i2dest)))
2008           && (next_real_insn (i2) == i3
2009               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2010           /* We can't overwrite I2DEST if its value is still used by
2011              NEWPAT.  */
2012           && ! reg_referenced_p (i2dest, newpat))
2013         {
2014           rtx newdest = i2dest;
2015           enum rtx_code split_code = GET_CODE (*split);
2016           enum machine_mode split_mode = GET_MODE (*split);
2017
2018           /* Get NEWDEST as a register in the proper mode.  We have already
2019              validated that we can do this.  */
2020           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2021             {
2022               newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2023
2024               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2025                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2026             }
2027
2028           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2029              an ASHIFT.  This can occur if it was inside a PLUS and hence
2030              appeared to be a memory address.  This is a kludge.  */
2031           if (split_code == MULT
2032               && GET_CODE (XEXP (*split, 1)) == CONST_INT
2033               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2034             {
2035               SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
2036                                               XEXP (*split, 0), GEN_INT (i)));
2037               /* Update split_code because we may not have a multiply
2038                  anymore.  */
2039               split_code = GET_CODE (*split);
2040             }
2041
2042 #ifdef INSN_SCHEDULING
2043           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2044              be written as a ZERO_EXTEND.  */
2045           if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2046             SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
2047                                             XEXP (*split, 0)));
2048 #endif
2049
2050           newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
2051           SUBST (*split, newdest);
2052           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2053
2054           /* If the split point was a MULT and we didn't have one before,
2055              don't use one now.  */
2056           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2057             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2058         }
2059     }
2060
2061   /* Check for a case where we loaded from memory in a narrow mode and
2062      then sign extended it, but we need both registers.  In that case,
2063      we have a PARALLEL with both loads from the same memory location.
2064      We can split this into a load from memory followed by a register-register
2065      copy.  This saves at least one insn, more if register allocation can
2066      eliminate the copy.
2067
2068      We cannot do this if the destination of the second assignment is
2069      a register that we have already assumed is zero-extended.  Similarly
2070      for a SUBREG of such a register.  */
2071
2072   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2073            && GET_CODE (newpat) == PARALLEL
2074            && XVECLEN (newpat, 0) == 2
2075            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2076            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2077            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2078            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2079                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2080            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2081                                    INSN_CUID (i2))
2082            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2083            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2084            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2085                  (GET_CODE (temp) == REG
2086                   && reg_nonzero_bits[REGNO (temp)] != 0
2087                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2088                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2089                   && (reg_nonzero_bits[REGNO (temp)]
2090                       != GET_MODE_MASK (word_mode))))
2091            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2092                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2093                      (GET_CODE (temp) == REG
2094                       && reg_nonzero_bits[REGNO (temp)] != 0
2095                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2096                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2097                       && (reg_nonzero_bits[REGNO (temp)]
2098                           != GET_MODE_MASK (word_mode)))))
2099            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2100                                          SET_SRC (XVECEXP (newpat, 0, 1)))
2101            && ! find_reg_note (i3, REG_UNUSED,
2102                                SET_DEST (XVECEXP (newpat, 0, 0))))
2103     {
2104       rtx ni2dest;
2105
2106       newi2pat = XVECEXP (newpat, 0, 0);
2107       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2108       newpat = XVECEXP (newpat, 0, 1);
2109       SUBST (SET_SRC (newpat),
2110              gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2111       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2112
2113       if (i2_code_number >= 0)
2114         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2115
2116       if (insn_code_number >= 0)
2117         {
2118           rtx insn;
2119           rtx link;
2120
2121           /* If we will be able to accept this, we have made a change to the
2122              destination of I3.  This can invalidate a LOG_LINKS pointing
2123              to I3.  No other part of combine.c makes such a transformation.
2124
2125              The new I3 will have a destination that was previously the
2126              destination of I1 or I2 and which was used in i2 or I3.  Call
2127              distribute_links to make a LOG_LINK from the next use of
2128              that destination.  */
2129
2130           PATTERN (i3) = newpat;
2131           distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
2132
2133           /* I3 now uses what used to be its destination and which is
2134              now I2's destination.  That means we need a LOG_LINK from
2135              I3 to I2.  But we used to have one, so we still will.
2136
2137              However, some later insn might be using I2's dest and have
2138              a LOG_LINK pointing at I3.  We must remove this link.
2139              The simplest way to remove the link is to point it at I1,
2140              which we know will be a NOTE.  */
2141
2142           for (insn = NEXT_INSN (i3);
2143                insn && (this_basic_block == n_basic_blocks - 1
2144                         || insn != BLOCK_HEAD (this_basic_block + 1));
2145                insn = NEXT_INSN (insn))
2146             {
2147               if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
2148                   && reg_referenced_p (ni2dest, PATTERN (insn)))
2149                 {
2150                   for (link = LOG_LINKS (insn); link;
2151                        link = XEXP (link, 1))
2152                     if (XEXP (link, 0) == i3)
2153                       XEXP (link, 0) = i1;
2154
2155                   break;
2156                 }
2157             }
2158         }
2159     }
2160             
2161   /* Similarly, check for a case where we have a PARALLEL of two independent
2162      SETs but we started with three insns.  In this case, we can do the sets
2163      as two separate insns.  This case occurs when some SET allows two
2164      other insns to combine, but the destination of that SET is still live.  */
2165
2166   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2167            && GET_CODE (newpat) == PARALLEL
2168            && XVECLEN (newpat, 0) == 2
2169            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2170            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2171            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2172            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2173            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2174            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2175            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2176                                    INSN_CUID (i2))
2177            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
2178            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2179            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2180            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2181                                   XVECEXP (newpat, 0, 0))
2182            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2183                                   XVECEXP (newpat, 0, 1)))
2184     {
2185       /* Normally, it doesn't matter which of the two is done first,
2186          but it does if one references cc0.  In that case, it has to
2187          be first.  */
2188 #ifdef HAVE_cc0
2189       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2190         {
2191           newi2pat = XVECEXP (newpat, 0, 0);
2192           newpat = XVECEXP (newpat, 0, 1);
2193         }
2194       else
2195 #endif
2196         {
2197           newi2pat = XVECEXP (newpat, 0, 1);
2198           newpat = XVECEXP (newpat, 0, 0);
2199         }
2200
2201       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2202
2203       if (i2_code_number >= 0)
2204         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2205     }
2206
2207   /* If it still isn't recognized, fail and change things back the way they
2208      were.  */
2209   if ((insn_code_number < 0
2210        /* Is the result a reasonable ASM_OPERANDS?  */
2211        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2212     {
2213       undo_all ();
2214       return 0;
2215     }
2216
2217   /* If we had to change another insn, make sure it is valid also.  */
2218   if (undobuf.other_insn)
2219     {
2220       rtx other_pat = PATTERN (undobuf.other_insn);
2221       rtx new_other_notes;
2222       rtx note, next;
2223
2224       CLEAR_HARD_REG_SET (newpat_used_regs);
2225
2226       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2227                                              &new_other_notes);
2228
2229       if (other_code_number < 0 && ! check_asm_operands (other_pat))
2230         {
2231           undo_all ();
2232           return 0;
2233         }
2234
2235       PATTERN (undobuf.other_insn) = other_pat;
2236
2237       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2238          are still valid.  Then add any non-duplicate notes added by
2239          recog_for_combine.  */
2240       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2241         {
2242           next = XEXP (note, 1);
2243
2244           if (REG_NOTE_KIND (note) == REG_UNUSED
2245               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2246             {
2247               if (GET_CODE (XEXP (note, 0)) == REG)
2248                 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2249
2250               remove_note (undobuf.other_insn, note);
2251             }
2252         }
2253
2254       for (note = new_other_notes; note; note = XEXP (note, 1))
2255         if (GET_CODE (XEXP (note, 0)) == REG)
2256           REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2257
2258       distribute_notes (new_other_notes, undobuf.other_insn,
2259                         undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2260     }
2261
2262   /* We now know that we can do this combination.  Merge the insns and 
2263      update the status of registers and LOG_LINKS.  */
2264
2265   {
2266     rtx i3notes, i2notes, i1notes = 0;
2267     rtx i3links, i2links, i1links = 0;
2268     rtx midnotes = 0;
2269     register int regno;
2270     /* Compute which registers we expect to eliminate.  newi2pat may be setting
2271        either i3dest or i2dest, so we must check it.  Also, i1dest may be the
2272        same as i3dest, in which case newi2pat may be setting i1dest.  */
2273     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2274                    || i2dest_in_i2src || i2dest_in_i1src
2275                    ? 0 : i2dest);
2276     rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2277                    || (newi2pat && reg_set_p (i1dest, newi2pat))
2278                    ? 0 : i1dest);
2279
2280     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2281        clear them.  */
2282     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2283     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2284     if (i1)
2285       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2286
2287     /* Ensure that we do not have something that should not be shared but
2288        occurs multiple times in the new insns.  Check this by first
2289        resetting all the `used' flags and then copying anything is shared.  */
2290
2291     reset_used_flags (i3notes);
2292     reset_used_flags (i2notes);
2293     reset_used_flags (i1notes);
2294     reset_used_flags (newpat);
2295     reset_used_flags (newi2pat);
2296     if (undobuf.other_insn)
2297       reset_used_flags (PATTERN (undobuf.other_insn));
2298
2299     i3notes = copy_rtx_if_shared (i3notes);
2300     i2notes = copy_rtx_if_shared (i2notes);
2301     i1notes = copy_rtx_if_shared (i1notes);
2302     newpat = copy_rtx_if_shared (newpat);
2303     newi2pat = copy_rtx_if_shared (newi2pat);
2304     if (undobuf.other_insn)
2305       reset_used_flags (PATTERN (undobuf.other_insn));
2306
2307     INSN_CODE (i3) = insn_code_number;
2308     PATTERN (i3) = newpat;
2309     if (undobuf.other_insn)
2310       INSN_CODE (undobuf.other_insn) = other_code_number;
2311
2312     /* We had one special case above where I2 had more than one set and
2313        we replaced a destination of one of those sets with the destination
2314        of I3.  In that case, we have to update LOG_LINKS of insns later
2315        in this basic block.  Note that this (expensive) case is rare.
2316
2317        Also, in this case, we must pretend that all REG_NOTEs for I2
2318        actually came from I3, so that REG_UNUSED notes from I2 will be
2319        properly handled.  */
2320
2321     if (i3_subst_into_i2)
2322       {
2323         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2324           if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2325               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2326               && ! find_reg_note (i2, REG_UNUSED,
2327                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2328             for (temp = NEXT_INSN (i2);
2329                  temp && (this_basic_block == n_basic_blocks - 1
2330                           || BLOCK_HEAD (this_basic_block) != temp);
2331                  temp = NEXT_INSN (temp))
2332               if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2333                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2334                   if (XEXP (link, 0) == i2)
2335                     XEXP (link, 0) = i3;
2336
2337         if (i3notes)
2338           {
2339             rtx link = i3notes;
2340             while (XEXP (link, 1))
2341               link = XEXP (link, 1);
2342             XEXP (link, 1) = i2notes;
2343           }
2344         else
2345           i3notes = i2notes;
2346         i2notes = 0;
2347       }
2348
2349     LOG_LINKS (i3) = 0;
2350     REG_NOTES (i3) = 0;
2351     LOG_LINKS (i2) = 0;
2352     REG_NOTES (i2) = 0;
2353
2354     if (newi2pat)
2355       {
2356         INSN_CODE (i2) = i2_code_number;
2357         PATTERN (i2) = newi2pat;
2358       }
2359     else
2360       {
2361         PUT_CODE (i2, NOTE);
2362         NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2363         NOTE_SOURCE_FILE (i2) = 0;
2364       }
2365
2366     if (i1)
2367       {
2368         LOG_LINKS (i1) = 0;
2369         REG_NOTES (i1) = 0;
2370         PUT_CODE (i1, NOTE);
2371         NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2372         NOTE_SOURCE_FILE (i1) = 0;
2373       }
2374
2375     /* Get death notes for everything that is now used in either I3 or
2376        I2 and used to die in a previous insn.  If we built two new 
2377        patterns, move from I1 to I2 then I2 to I3 so that we get the
2378        proper movement on registers that I2 modifies.  */
2379
2380     if (newi2pat)
2381       {
2382         move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2383         move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2384       }
2385     else
2386       move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2387                    i3, &midnotes);
2388
2389     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
2390     if (i3notes)
2391       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2392                         elim_i2, elim_i1);
2393     if (i2notes)
2394       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2395                         elim_i2, elim_i1);
2396     if (i1notes)
2397       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2398                         elim_i2, elim_i1);
2399     if (midnotes)
2400       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2401                         elim_i2, elim_i1);
2402
2403     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
2404        know these are REG_UNUSED and want them to go to the desired insn,
2405        so we always pass it as i3.  We have not counted the notes in 
2406        reg_n_deaths yet, so we need to do so now.  */
2407
2408     if (newi2pat && new_i2_notes)
2409       {
2410         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2411           if (GET_CODE (XEXP (temp, 0)) == REG)
2412             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2413         
2414         distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2415       }
2416
2417     if (new_i3_notes)
2418       {
2419         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2420           if (GET_CODE (XEXP (temp, 0)) == REG)
2421             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2422         
2423         distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2424       }
2425
2426     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
2427        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
2428        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
2429        in that case, it might delete I2.  Similarly for I2 and I1.
2430        Show an additional death due to the REG_DEAD note we make here.  If
2431        we discard it in distribute_notes, we will decrement it again.  */
2432
2433     if (i3dest_killed)
2434       {
2435         if (GET_CODE (i3dest_killed) == REG)
2436           REG_N_DEATHS (REGNO (i3dest_killed))++;
2437
2438         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2439           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2440                                                NULL_RTX),
2441                             NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
2442         else
2443           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2444                                                NULL_RTX),
2445                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2446                             elim_i2, elim_i1);
2447       }
2448
2449     if (i2dest_in_i2src)
2450       {
2451         if (GET_CODE (i2dest) == REG)
2452           REG_N_DEATHS (REGNO (i2dest))++;
2453
2454         if (newi2pat && reg_set_p (i2dest, newi2pat))
2455           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2456                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2457         else
2458           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2459                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2460                             NULL_RTX, NULL_RTX);
2461       }
2462
2463     if (i1dest_in_i1src)
2464       {
2465         if (GET_CODE (i1dest) == REG)
2466           REG_N_DEATHS (REGNO (i1dest))++;
2467
2468         if (newi2pat && reg_set_p (i1dest, newi2pat))
2469           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2470                             NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2471         else
2472           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2473                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2474                             NULL_RTX, NULL_RTX);
2475       }
2476
2477     distribute_links (i3links);
2478     distribute_links (i2links);
2479     distribute_links (i1links);
2480
2481     if (GET_CODE (i2dest) == REG)
2482       {
2483         rtx link;
2484         rtx i2_insn = 0, i2_val = 0, set;
2485
2486         /* The insn that used to set this register doesn't exist, and
2487            this life of the register may not exist either.  See if one of
2488            I3's links points to an insn that sets I2DEST.  If it does, 
2489            that is now the last known value for I2DEST. If we don't update
2490            this and I2 set the register to a value that depended on its old
2491            contents, we will get confused.  If this insn is used, thing
2492            will be set correctly in combine_instructions.  */
2493
2494         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2495           if ((set = single_set (XEXP (link, 0))) != 0
2496               && rtx_equal_p (i2dest, SET_DEST (set)))
2497             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2498
2499         record_value_for_reg (i2dest, i2_insn, i2_val);
2500
2501         /* If the reg formerly set in I2 died only once and that was in I3,
2502            zero its use count so it won't make `reload' do any work.  */
2503         if (! added_sets_2
2504             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2505             && ! i2dest_in_i2src)
2506           {
2507             regno = REGNO (i2dest);
2508             REG_N_SETS (regno)--;
2509             if (REG_N_SETS (regno) == 0
2510                 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
2511                                       regno))
2512               REG_N_REFS (regno) = 0;
2513           }
2514       }
2515
2516     if (i1 && GET_CODE (i1dest) == REG)
2517       {
2518         rtx link;
2519         rtx i1_insn = 0, i1_val = 0, set;
2520
2521         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2522           if ((set = single_set (XEXP (link, 0))) != 0
2523               && rtx_equal_p (i1dest, SET_DEST (set)))
2524             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2525
2526         record_value_for_reg (i1dest, i1_insn, i1_val);
2527
2528         regno = REGNO (i1dest);
2529         if (! added_sets_1 && ! i1dest_in_i1src)
2530           {
2531             REG_N_SETS (regno)--;
2532             if (REG_N_SETS (regno) == 0
2533                 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
2534                                       regno))
2535               REG_N_REFS (regno) = 0;
2536           }
2537       }
2538
2539     /* Update reg_nonzero_bits et al for any changes that may have been made
2540        to this insn.  */
2541
2542     note_stores (newpat, set_nonzero_bits_and_sign_copies);
2543     if (newi2pat)
2544       note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
2545
2546     /* If I3 is now an unconditional jump, ensure that it has a 
2547        BARRIER following it since it may have initially been a
2548        conditional jump.  It may also be the last nonnote insn.  */
2549
2550     if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
2551         && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2552             || GET_CODE (temp) != BARRIER))
2553       emit_barrier_after (i3);
2554   }
2555
2556   combine_successes++;
2557
2558   /* Clear this here, so that subsequent get_last_value calls are not
2559      affected.  */
2560   subst_prev_insn = NULL_RTX;
2561
2562   if (added_links_insn
2563       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2564       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2565     return added_links_insn;
2566   else
2567     return newi2pat ? i2 : i3;
2568 }
2569 \f
2570 /* Undo all the modifications recorded in undobuf.  */
2571
2572 static void
2573 undo_all ()
2574 {
2575   struct undo *undo, *next;
2576
2577   for (undo = undobuf.undos; undo; undo = next)
2578     {
2579       next = undo->next;
2580       if (undo->is_int)
2581         *undo->where.i = undo->old_contents.i;
2582       else
2583         *undo->where.r = undo->old_contents.r;
2584
2585       undo->next = undobuf.frees;
2586       undobuf.frees = undo;
2587     }
2588
2589   obfree (undobuf.storage);
2590   undobuf.undos = undobuf.previous_undos = 0;
2591
2592   /* Clear this here, so that subsequent get_last_value calls are not
2593      affected.  */
2594   subst_prev_insn = NULL_RTX;
2595 }
2596 \f
2597 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2598    where we have an arithmetic expression and return that point.  LOC will
2599    be inside INSN.
2600
2601    try_combine will call this function to see if an insn can be split into
2602    two insns.  */
2603
2604 static rtx *
2605 find_split_point (loc, insn)
2606      rtx *loc;
2607      rtx insn;
2608 {
2609   rtx x = *loc;
2610   enum rtx_code code = GET_CODE (x);
2611   rtx *split;
2612   int len = 0, pos = 0, unsignedp = 0;
2613   rtx inner = NULL_RTX;
2614
2615   /* First special-case some codes.  */
2616   switch (code)
2617     {
2618     case SUBREG:
2619 #ifdef INSN_SCHEDULING
2620       /* If we are making a paradoxical SUBREG invalid, it becomes a split
2621          point.  */
2622       if (GET_CODE (SUBREG_REG (x)) == MEM)
2623         return loc;
2624 #endif
2625       return find_split_point (&SUBREG_REG (x), insn);
2626
2627     case MEM:
2628 #ifdef HAVE_lo_sum
2629       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2630          using LO_SUM and HIGH.  */
2631       if (GET_CODE (XEXP (x, 0)) == CONST
2632           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2633         {
2634           SUBST (XEXP (x, 0),
2635                  gen_rtx_combine (LO_SUM, Pmode,
2636                                   gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2637                                   XEXP (x, 0)));
2638           return &XEXP (XEXP (x, 0), 0);
2639         }
2640 #endif
2641
2642       /* If we have a PLUS whose second operand is a constant and the
2643          address is not valid, perhaps will can split it up using
2644          the machine-specific way to split large constants.  We use
2645          the first pseudo-reg (one of the virtual regs) as a placeholder;
2646          it will not remain in the result.  */
2647       if (GET_CODE (XEXP (x, 0)) == PLUS
2648           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2649           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2650         {
2651           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2652           rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2653                                  subst_insn);
2654
2655           /* This should have produced two insns, each of which sets our
2656              placeholder.  If the source of the second is a valid address,
2657              we can make put both sources together and make a split point
2658              in the middle.  */
2659
2660           if (seq && XVECLEN (seq, 0) == 2
2661               && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2662               && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2663               && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2664               && ! reg_mentioned_p (reg,
2665                                     SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2666               && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2667               && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2668               && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2669               && memory_address_p (GET_MODE (x),
2670                                    SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2671             {
2672               rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2673               rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2674
2675               /* Replace the placeholder in SRC2 with SRC1.  If we can
2676                  find where in SRC2 it was placed, that can become our
2677                  split point and we can replace this address with SRC2.
2678                  Just try two obvious places.  */
2679
2680               src2 = replace_rtx (src2, reg, src1);
2681               split = 0;
2682               if (XEXP (src2, 0) == src1)
2683                 split = &XEXP (src2, 0);
2684               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2685                        && XEXP (XEXP (src2, 0), 0) == src1)
2686                 split = &XEXP (XEXP (src2, 0), 0);
2687
2688               if (split)
2689                 {
2690                   SUBST (XEXP (x, 0), src2);
2691                   return split;
2692                 }
2693             }
2694           
2695           /* If that didn't work, perhaps the first operand is complex and
2696              needs to be computed separately, so make a split point there.
2697              This will occur on machines that just support REG + CONST
2698              and have a constant moved through some previous computation.  */
2699
2700           else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2701                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2702                          && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2703                              == 'o')))
2704             return &XEXP (XEXP (x, 0), 0);
2705         }
2706       break;
2707
2708     case SET:
2709 #ifdef HAVE_cc0
2710       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2711          ZERO_EXTRACT, the most likely reason why this doesn't match is that
2712          we need to put the operand into a register.  So split at that
2713          point.  */
2714
2715       if (SET_DEST (x) == cc0_rtx
2716           && GET_CODE (SET_SRC (x)) != COMPARE
2717           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2718           && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2719           && ! (GET_CODE (SET_SRC (x)) == SUBREG
2720                 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2721         return &SET_SRC (x);
2722 #endif
2723
2724       /* See if we can split SET_SRC as it stands.  */
2725       split = find_split_point (&SET_SRC (x), insn);
2726       if (split && split != &SET_SRC (x))
2727         return split;
2728
2729       /* See if we can split SET_DEST as it stands.  */
2730       split = find_split_point (&SET_DEST (x), insn);
2731       if (split && split != &SET_DEST (x))
2732         return split;
2733
2734       /* See if this is a bitfield assignment with everything constant.  If
2735          so, this is an IOR of an AND, so split it into that.  */
2736       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2737           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2738               <= HOST_BITS_PER_WIDE_INT)
2739           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2740           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2741           && GET_CODE (SET_SRC (x)) == CONST_INT
2742           && ((INTVAL (XEXP (SET_DEST (x), 1))
2743               + INTVAL (XEXP (SET_DEST (x), 2)))
2744               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2745           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2746         {
2747           int pos = INTVAL (XEXP (SET_DEST (x), 2));
2748           int len = INTVAL (XEXP (SET_DEST (x), 1));
2749           int src = INTVAL (SET_SRC (x));
2750           rtx dest = XEXP (SET_DEST (x), 0);
2751           enum machine_mode mode = GET_MODE (dest);
2752           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2753
2754           if (BITS_BIG_ENDIAN)
2755             pos = GET_MODE_BITSIZE (mode) - len - pos;
2756
2757           if ((unsigned HOST_WIDE_INT) src == mask)
2758             SUBST (SET_SRC (x),
2759                    gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2760           else
2761             SUBST (SET_SRC (x),
2762                    gen_binary (IOR, mode,
2763                                gen_binary (AND, mode, dest, 
2764                                            GEN_INT (~ (mask << pos)
2765                                                     & GET_MODE_MASK (mode))),
2766                                GEN_INT (src << pos)));
2767
2768           SUBST (SET_DEST (x), dest);
2769
2770           split = find_split_point (&SET_SRC (x), insn);
2771           if (split && split != &SET_SRC (x))
2772             return split;
2773         }
2774
2775       /* Otherwise, see if this is an operation that we can split into two.
2776          If so, try to split that.  */
2777       code = GET_CODE (SET_SRC (x));
2778
2779       switch (code)
2780         {
2781         case AND:
2782           /* If we are AND'ing with a large constant that is only a single
2783              bit and the result is only being used in a context where we
2784              need to know if it is zero or non-zero, replace it with a bit
2785              extraction.  This will avoid the large constant, which might
2786              have taken more than one insn to make.  If the constant were
2787              not a valid argument to the AND but took only one insn to make,
2788              this is no worse, but if it took more than one insn, it will
2789              be better.  */
2790
2791           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2792               && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2793               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2794               && GET_CODE (SET_DEST (x)) == REG
2795               && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2796               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2797               && XEXP (*split, 0) == SET_DEST (x)
2798               && XEXP (*split, 1) == const0_rtx)
2799             {
2800               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2801                                                 XEXP (SET_SRC (x), 0),
2802                                                 pos, NULL_RTX, 1, 1, 0, 0);
2803               if (extraction != 0)
2804                 {
2805                   SUBST (SET_SRC (x), extraction);
2806                   return find_split_point (loc, insn);
2807                 }
2808             }
2809           break;
2810
2811         case NE:
2812           /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
2813              is known to be on, this can be converted into a NEG of a shift. */
2814           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
2815               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
2816               && 1 <= (pos = exact_log2
2817                        (nonzero_bits (XEXP (SET_SRC (x), 0),
2818                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
2819             {
2820               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
2821
2822               SUBST (SET_SRC (x),
2823                      gen_rtx_combine (NEG, mode,
2824                                       gen_rtx_combine (LSHIFTRT, mode,
2825                                                        XEXP (SET_SRC (x), 0),
2826                                                        GEN_INT (pos))));
2827
2828               split = find_split_point (&SET_SRC (x), insn);
2829               if (split && split != &SET_SRC (x))
2830                 return split;
2831             }
2832           break;
2833
2834         case SIGN_EXTEND:
2835           inner = XEXP (SET_SRC (x), 0);
2836
2837           /* We can't optimize if either mode is a partial integer
2838              mode as we don't know how many bits are significant
2839              in those modes.  */
2840           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
2841               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
2842             break;
2843
2844           pos = 0;
2845           len = GET_MODE_BITSIZE (GET_MODE (inner));
2846           unsignedp = 0;
2847           break;
2848
2849         case SIGN_EXTRACT:
2850         case ZERO_EXTRACT:
2851           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2852               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2853             {
2854               inner = XEXP (SET_SRC (x), 0);
2855               len = INTVAL (XEXP (SET_SRC (x), 1));
2856               pos = INTVAL (XEXP (SET_SRC (x), 2));
2857
2858               if (BITS_BIG_ENDIAN)
2859                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
2860               unsignedp = (code == ZERO_EXTRACT);
2861             }
2862           break;
2863
2864         default:
2865           break;
2866         }
2867
2868       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2869         {
2870           enum machine_mode mode = GET_MODE (SET_SRC (x));
2871
2872           /* For unsigned, we have a choice of a shift followed by an
2873              AND or two shifts.  Use two shifts for field sizes where the
2874              constant might be too large.  We assume here that we can
2875              always at least get 8-bit constants in an AND insn, which is
2876              true for every current RISC.  */
2877
2878           if (unsignedp && len <= 8)
2879             {
2880               SUBST (SET_SRC (x),
2881                      gen_rtx_combine
2882                      (AND, mode,
2883                       gen_rtx_combine (LSHIFTRT, mode,
2884                                        gen_lowpart_for_combine (mode, inner),
2885                                        GEN_INT (pos)),
2886                       GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
2887
2888               split = find_split_point (&SET_SRC (x), insn);
2889               if (split && split != &SET_SRC (x))
2890                 return split;
2891             }
2892           else
2893             {
2894               SUBST (SET_SRC (x),
2895                      gen_rtx_combine
2896                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
2897                       gen_rtx_combine (ASHIFT, mode,
2898                                        gen_lowpart_for_combine (mode, inner),
2899                                        GEN_INT (GET_MODE_BITSIZE (mode)
2900                                                 - len - pos)),
2901                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
2902
2903               split = find_split_point (&SET_SRC (x), insn);
2904               if (split && split != &SET_SRC (x))
2905                 return split;
2906             }
2907         }
2908
2909       /* See if this is a simple operation with a constant as the second
2910          operand.  It might be that this constant is out of range and hence
2911          could be used as a split point.  */
2912       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2913            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2914            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2915           && CONSTANT_P (XEXP (SET_SRC (x), 1))
2916           && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2917               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2918                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2919                       == 'o'))))
2920         return &XEXP (SET_SRC (x), 1);
2921
2922       /* Finally, see if this is a simple operation with its first operand
2923          not in a register.  The operation might require this operand in a
2924          register, so return it as a split point.  We can always do this
2925          because if the first operand were another operation, we would have
2926          already found it as a split point.  */
2927       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2928            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2929            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2930            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2931           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2932         return &XEXP (SET_SRC (x), 0);
2933
2934       return 0;
2935
2936     case AND:
2937     case IOR:
2938       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2939          it is better to write this as (not (ior A B)) so we can split it.
2940          Similarly for IOR.  */
2941       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2942         {
2943           SUBST (*loc,
2944                  gen_rtx_combine (NOT, GET_MODE (x),
2945                                   gen_rtx_combine (code == IOR ? AND : IOR,
2946                                                    GET_MODE (x),
2947                                                    XEXP (XEXP (x, 0), 0),
2948                                                    XEXP (XEXP (x, 1), 0))));
2949           return find_split_point (loc, insn);
2950         }
2951
2952       /* Many RISC machines have a large set of logical insns.  If the
2953          second operand is a NOT, put it first so we will try to split the
2954          other operand first.  */
2955       if (GET_CODE (XEXP (x, 1)) == NOT)
2956         {
2957           rtx tem = XEXP (x, 0);
2958           SUBST (XEXP (x, 0), XEXP (x, 1));
2959           SUBST (XEXP (x, 1), tem);
2960         }
2961       break;
2962
2963     default:
2964       break;
2965     }
2966
2967   /* Otherwise, select our actions depending on our rtx class.  */
2968   switch (GET_RTX_CLASS (code))
2969     {
2970     case 'b':                   /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
2971     case '3':
2972       split = find_split_point (&XEXP (x, 2), insn);
2973       if (split)
2974         return split;
2975       /* ... fall through ...  */
2976     case '2':
2977     case 'c':
2978     case '<':
2979       split = find_split_point (&XEXP (x, 1), insn);
2980       if (split)
2981         return split;
2982       /* ... fall through ...  */
2983     case '1':
2984       /* Some machines have (and (shift ...) ...) insns.  If X is not
2985          an AND, but XEXP (X, 0) is, use it as our split point.  */
2986       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
2987         return &XEXP (x, 0);
2988
2989       split = find_split_point (&XEXP (x, 0), insn);
2990       if (split)
2991         return split;
2992       return loc;
2993     }
2994
2995   /* Otherwise, we don't have a split point.  */
2996   return 0;
2997 }
2998 \f
2999 /* Throughout X, replace FROM with TO, and return the result.
3000    The result is TO if X is FROM;
3001    otherwise the result is X, but its contents may have been modified.
3002    If they were modified, a record was made in undobuf so that
3003    undo_all will (among other things) return X to its original state.
3004
3005    If the number of changes necessary is too much to record to undo,
3006    the excess changes are not made, so the result is invalid.
3007    The changes already made can still be undone.
3008    undobuf.num_undo is incremented for such changes, so by testing that
3009    the caller can tell whether the result is valid.
3010
3011    `n_occurrences' is incremented each time FROM is replaced.
3012    
3013    IN_DEST is non-zero if we are processing the SET_DEST of a SET.
3014
3015    UNIQUE_COPY is non-zero if each substitution must be unique.  We do this
3016    by copying if `n_occurrences' is non-zero.  */
3017
3018 static rtx
3019 subst (x, from, to, in_dest, unique_copy)
3020      register rtx x, from, to;
3021      int in_dest;
3022      int unique_copy;
3023 {
3024   register enum rtx_code code = GET_CODE (x);
3025   enum machine_mode op0_mode = VOIDmode;
3026   register const char *fmt;
3027   register int len, i;
3028   rtx new;
3029
3030 /* Two expressions are equal if they are identical copies of a shared
3031    RTX or if they are both registers with the same register number
3032    and mode.  */
3033
3034 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
3035   ((X) == (Y)                                           \
3036    || (GET_CODE (X) == REG && GET_CODE (Y) == REG       \
3037        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3038
3039   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3040     {
3041       n_occurrences++;
3042       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3043     }
3044
3045   /* If X and FROM are the same register but different modes, they will
3046      not have been seen as equal above.  However, flow.c will make a 
3047      LOG_LINKS entry for that case.  If we do nothing, we will try to
3048      rerecognize our original insn and, when it succeeds, we will
3049      delete the feeding insn, which is incorrect.
3050
3051      So force this insn not to match in this (rare) case.  */
3052   if (! in_dest && code == REG && GET_CODE (from) == REG
3053       && REGNO (x) == REGNO (from))
3054     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3055
3056   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3057      of which may contain things that can be combined.  */
3058   if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3059     return x;
3060
3061   /* It is possible to have a subexpression appear twice in the insn.
3062      Suppose that FROM is a register that appears within TO.
3063      Then, after that subexpression has been scanned once by `subst',
3064      the second time it is scanned, TO may be found.  If we were
3065      to scan TO here, we would find FROM within it and create a
3066      self-referent rtl structure which is completely wrong.  */
3067   if (COMBINE_RTX_EQUAL_P (x, to))
3068     return to;
3069
3070   /* Parallel asm_operands need special attention because all of the
3071      inputs are shared across the arms.  Furthermore, unsharing the
3072      rtl results in recognition failures.  Failure to handle this case
3073      specially can result in circular rtl.
3074
3075      Solve this by doing a normal pass across the first entry of the
3076      parallel, and only processing the SET_DESTs of the subsequent
3077      entries.  Ug.  */
3078
3079   if (code == PARALLEL
3080       && GET_CODE (XVECEXP (x, 0, 0)) == SET
3081       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3082     {
3083       new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3084
3085       /* If this substitution failed, this whole thing fails.  */
3086       if (GET_CODE (new) == CLOBBER
3087           && XEXP (new, 0) == const0_rtx)
3088         return new;
3089
3090       SUBST (XVECEXP (x, 0, 0), new);
3091
3092       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3093         {
3094           rtx dest = SET_DEST (XVECEXP (x, 0, i));
3095           
3096           if (GET_CODE (dest) != REG
3097               && GET_CODE (dest) != CC0
3098               && GET_CODE (dest) != PC)
3099             {
3100               new = subst (dest, from, to, 0, unique_copy);
3101
3102               /* If this substitution failed, this whole thing fails.  */
3103               if (GET_CODE (new) == CLOBBER
3104                   && XEXP (new, 0) == const0_rtx)
3105                 return new;
3106
3107               SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3108             }
3109         }
3110     }
3111   else
3112     {
3113       len = GET_RTX_LENGTH (code);
3114       fmt = GET_RTX_FORMAT (code);
3115
3116       /* We don't need to process a SET_DEST that is a register, CC0,
3117          or PC, so set up to skip this common case.  All other cases
3118          where we want to suppress replacing something inside a
3119          SET_SRC are handled via the IN_DEST operand.  */
3120       if (code == SET
3121           && (GET_CODE (SET_DEST (x)) == REG
3122               || GET_CODE (SET_DEST (x)) == CC0
3123               || GET_CODE (SET_DEST (x)) == PC))
3124         fmt = "ie";
3125
3126       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3127          constant.  */
3128       if (fmt[0] == 'e')
3129         op0_mode = GET_MODE (XEXP (x, 0));
3130
3131       for (i = 0; i < len; i++)
3132         {
3133           if (fmt[i] == 'E')
3134             {
3135               register int j;
3136               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3137                 {
3138                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3139                     {
3140                       new = (unique_copy && n_occurrences
3141                              ? copy_rtx (to) : to);
3142                       n_occurrences++;
3143                     }
3144                   else
3145                     {
3146                       new = subst (XVECEXP (x, i, j), from, to, 0,
3147                                    unique_copy);
3148
3149                       /* If this substitution failed, this whole thing
3150                          fails.  */
3151                       if (GET_CODE (new) == CLOBBER
3152                           && XEXP (new, 0) == const0_rtx)
3153                         return new;
3154                     }
3155
3156                   SUBST (XVECEXP (x, i, j), new);
3157                 }
3158             }
3159           else if (fmt[i] == 'e')
3160             {
3161               if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3162                 {
3163                   /* In general, don't install a subreg involving two
3164                      modes not tieable.  It can worsen register
3165                      allocation, and can even make invalid reload
3166                      insns, since the reg inside may need to be copied
3167                      from in the outside mode, and that may be invalid
3168                      if it is an fp reg copied in integer mode.
3169
3170                      We allow two exceptions to this: It is valid if
3171                      it is inside another SUBREG and the mode of that
3172                      SUBREG and the mode of the inside of TO is
3173                      tieable and it is valid if X is a SET that copies
3174                      FROM to CC0.  */
3175
3176                   if (GET_CODE (to) == SUBREG
3177                       && ! MODES_TIEABLE_P (GET_MODE (to),
3178                                             GET_MODE (SUBREG_REG (to)))
3179                       && ! (code == SUBREG
3180                             && MODES_TIEABLE_P (GET_MODE (x),
3181                                                 GET_MODE (SUBREG_REG (to))))
3182 #ifdef HAVE_cc0
3183                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3184 #endif
3185                       )
3186                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3187
3188                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3189                   n_occurrences++;
3190                 }
3191               else
3192                 /* If we are in a SET_DEST, suppress most cases unless we
3193                    have gone inside a MEM, in which case we want to
3194                    simplify the address.  We assume here that things that
3195                    are actually part of the destination have their inner
3196                    parts in the first expression.  This is true for SUBREG, 
3197                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3198                    things aside from REG and MEM that should appear in a
3199                    SET_DEST.  */
3200                 new = subst (XEXP (x, i), from, to,
3201                              (((in_dest
3202                                 && (code == SUBREG || code == STRICT_LOW_PART
3203                                     || code == ZERO_EXTRACT))
3204                                || code == SET)
3205                               && i == 0), unique_copy);
3206
3207               /* If we found that we will have to reject this combination,
3208                  indicate that by returning the CLOBBER ourselves, rather than
3209                  an expression containing it.  This will speed things up as
3210                  well as prevent accidents where two CLOBBERs are considered
3211                  to be equal, thus producing an incorrect simplification.  */
3212
3213               if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3214                 return new;
3215
3216               SUBST (XEXP (x, i), new);
3217             }
3218         }
3219     }
3220
3221   /* Try to simplify X.  If the simplification changed the code, it is likely
3222      that further simplification will help, so loop, but limit the number
3223      of repetitions that will be performed.  */
3224
3225   for (i = 0; i < 4; i++)
3226     {
3227       /* If X is sufficiently simple, don't bother trying to do anything
3228          with it.  */
3229       if (code != CONST_INT && code != REG && code != CLOBBER)
3230         x = simplify_rtx (x, op0_mode, i == 3, in_dest);
3231
3232       if (GET_CODE (x) == code)
3233         break;
3234
3235       code = GET_CODE (x);
3236
3237       /* We no longer know the original mode of operand 0 since we
3238          have changed the form of X)  */
3239       op0_mode = VOIDmode;
3240     }
3241
3242   return x;
3243 }
3244 \f
3245 /* Simplify X, a piece of RTL.  We just operate on the expression at the
3246    outer level; call `subst' to simplify recursively.  Return the new
3247    expression.
3248
3249    OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3250    will be the iteration even if an expression with a code different from
3251    X is returned; IN_DEST is nonzero if we are inside a SET_DEST.  */
3252
3253 static rtx
3254 simplify_rtx (x, op0_mode, last, in_dest)
3255      rtx x;
3256      enum machine_mode op0_mode;
3257      int last;
3258      int in_dest;
3259 {
3260   enum rtx_code code = GET_CODE (x);
3261   enum machine_mode mode = GET_MODE (x);
3262   rtx temp;
3263   int i;
3264
3265   /* If this is a commutative operation, put a constant last and a complex
3266      expression first.  We don't need to do this for comparisons here.  */
3267   if (GET_RTX_CLASS (code) == 'c'
3268       && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3269           || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3270               && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3271           || (GET_CODE (XEXP (x, 0)) == SUBREG
3272               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3273               && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3274     {
3275       temp = XEXP (x, 0);
3276       SUBST (XEXP (x, 0), XEXP (x, 1));
3277       SUBST (XEXP (x, 1), temp);
3278     }
3279
3280   /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3281      sign extension of a PLUS with a constant, reverse the order of the sign
3282      extension and the addition. Note that this not the same as the original
3283      code, but overflow is undefined for signed values.  Also note that the
3284      PLUS will have been partially moved "inside" the sign-extension, so that
3285      the first operand of X will really look like:
3286          (ashiftrt (plus (ashift A C4) C5) C4).
3287      We convert this to
3288          (plus (ashiftrt (ashift A C4) C2) C4)
3289      and replace the first operand of X with that expression.  Later parts
3290      of this function may simplify the expression further.
3291
3292      For example, if we start with (mult (sign_extend (plus A C1)) C2),
3293      we swap the SIGN_EXTEND and PLUS.  Later code will apply the
3294      distributive law to produce (plus (mult (sign_extend X) C1) C3).
3295
3296      We do this to simplify address expressions.  */
3297
3298   if ((code == PLUS || code == MINUS || code == MULT)
3299       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3300       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3301       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3302       && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3303       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3304       && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3305       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3306       && (temp = simplify_binary_operation (ASHIFTRT, mode,
3307                                             XEXP (XEXP (XEXP (x, 0), 0), 1),
3308                                             XEXP (XEXP (x, 0), 1))) != 0)
3309     {
3310       rtx new
3311         = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3312                                 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3313                                 INTVAL (XEXP (XEXP (x, 0), 1)));
3314
3315       new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3316                                   INTVAL (XEXP (XEXP (x, 0), 1)));
3317
3318       SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3319     }
3320
3321   /* If this is a simple operation applied to an IF_THEN_ELSE, try 
3322      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
3323      things.  Check for cases where both arms are testing the same
3324      condition.
3325
3326      Don't do anything if all operands are very simple.  */
3327
3328   if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3329         || GET_RTX_CLASS (code) == '<')
3330        && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3331             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3332                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3333                       == 'o')))
3334            || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3335                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3336                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3337                          == 'o')))))
3338       || (GET_RTX_CLASS (code) == '1'
3339           && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3340                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3341                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3342                          == 'o'))))))
3343     {
3344       rtx cond, true, false;
3345
3346       cond = if_then_else_cond (x, &true, &false);
3347       if (cond != 0
3348           /* If everything is a comparison, what we have is highly unlikely
3349              to be simpler, so don't use it.  */
3350           && ! (GET_RTX_CLASS (code) == '<'
3351                 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3352                     || GET_RTX_CLASS (GET_CODE (false)) == '<')))
3353         {
3354           rtx cop1 = const0_rtx;
3355           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3356
3357           if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3358             return x;
3359
3360           /* Simplify the alternative arms; this may collapse the true and 
3361              false arms to store-flag values.  */
3362           true = subst (true, pc_rtx, pc_rtx, 0, 0);
3363           false = subst (false, pc_rtx, pc_rtx, 0, 0);
3364
3365           /* Restarting if we generate a store-flag expression will cause
3366              us to loop.  Just drop through in this case.  */
3367
3368           /* If the result values are STORE_FLAG_VALUE and zero, we can
3369              just make the comparison operation.  */
3370           if (true == const_true_rtx && false == const0_rtx)
3371             x = gen_binary (cond_code, mode, cond, cop1);
3372           else if (true == const0_rtx && false == const_true_rtx)
3373             x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3374
3375           /* Likewise, we can make the negate of a comparison operation
3376              if the result values are - STORE_FLAG_VALUE and zero.  */
3377           else if (GET_CODE (true) == CONST_INT
3378                    && INTVAL (true) == - STORE_FLAG_VALUE
3379                    && false == const0_rtx)
3380             x = gen_unary (NEG, mode, mode,
3381                            gen_binary (cond_code, mode, cond, cop1));
3382           else if (GET_CODE (false) == CONST_INT
3383                    && INTVAL (false) == - STORE_FLAG_VALUE
3384                    && true == const0_rtx)
3385             x = gen_unary (NEG, mode, mode,
3386                            gen_binary (reverse_condition (cond_code), 
3387                                        mode, cond, cop1));
3388           else
3389             return gen_rtx_IF_THEN_ELSE (mode,
3390                                          gen_binary (cond_code, VOIDmode,
3391                                                      cond, cop1),
3392                                          true, false);
3393
3394           code = GET_CODE (x);
3395           op0_mode = VOIDmode;
3396         }
3397     }
3398
3399   /* Try to fold this expression in case we have constants that weren't
3400      present before.  */
3401   temp = 0;
3402   switch (GET_RTX_CLASS (code))
3403     {
3404     case '1':
3405       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3406       break;
3407     case '<':
3408       temp = simplify_relational_operation (code, op0_mode,
3409                                             XEXP (x, 0), XEXP (x, 1));
3410 #ifdef FLOAT_STORE_FLAG_VALUE
3411       if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3412         temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3413                 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3414 #endif
3415       break;
3416     case 'c':
3417     case '2':
3418       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3419       break;
3420     case 'b':
3421     case '3':
3422       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3423                                          XEXP (x, 1), XEXP (x, 2));
3424       break;
3425     }
3426
3427   if (temp)
3428     x = temp, code = GET_CODE (temp);
3429
3430   /* First see if we can apply the inverse distributive law.  */
3431   if (code == PLUS || code == MINUS
3432       || code == AND || code == IOR || code == XOR)
3433     {
3434       x = apply_distributive_law (x);
3435       code = GET_CODE (x);
3436     }
3437
3438   /* If CODE is an associative operation not otherwise handled, see if we
3439      can associate some operands.  This can win if they are constants or
3440      if they are logically related (i.e. (a & b) & a.  */
3441   if ((code == PLUS || code == MINUS
3442        || code == MULT || code == AND || code == IOR || code == XOR
3443        || code == DIV || code == UDIV
3444        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3445       && INTEGRAL_MODE_P (mode))
3446     {
3447       if (GET_CODE (XEXP (x, 0)) == code)
3448         {
3449           rtx other = XEXP (XEXP (x, 0), 0);
3450           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3451           rtx inner_op1 = XEXP (x, 1);
3452           rtx inner;
3453           
3454           /* Make sure we pass the constant operand if any as the second
3455              one if this is a commutative operation.  */
3456           if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3457             {
3458               rtx tem = inner_op0;
3459               inner_op0 = inner_op1;
3460               inner_op1 = tem;
3461             }
3462           inner = simplify_binary_operation (code == MINUS ? PLUS
3463                                              : code == DIV ? MULT
3464                                              : code == UDIV ? MULT
3465                                              : code,
3466                                              mode, inner_op0, inner_op1);
3467
3468           /* For commutative operations, try the other pair if that one
3469              didn't simplify.  */
3470           if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3471             {
3472               other = XEXP (XEXP (x, 0), 1);
3473               inner = simplify_binary_operation (code, mode,
3474                                                  XEXP (XEXP (x, 0), 0),
3475                                                  XEXP (x, 1));
3476             }
3477
3478           if (inner)
3479             return gen_binary (code, mode, other, inner);
3480         }
3481     }
3482
3483   /* A little bit of algebraic simplification here.  */
3484   switch (code)
3485     {
3486     case MEM:
3487       /* Ensure that our address has any ASHIFTs converted to MULT in case
3488          address-recognizing predicates are called later.  */
3489       temp = make_compound_operation (XEXP (x, 0), MEM);
3490       SUBST (XEXP (x, 0), temp);
3491       break;
3492
3493     case SUBREG:
3494       /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3495          is paradoxical.  If we can't do that safely, then it becomes
3496          something nonsensical so that this combination won't take place.  */
3497
3498       if (GET_CODE (SUBREG_REG (x)) == MEM
3499           && (GET_MODE_SIZE (mode)
3500               <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3501         {
3502           rtx inner = SUBREG_REG (x);
3503           int endian_offset = 0;
3504           /* Don't change the mode of the MEM
3505              if that would change the meaning of the address.  */
3506           if (MEM_VOLATILE_P (SUBREG_REG (x))
3507               || mode_dependent_address_p (XEXP (inner, 0)))
3508             return gen_rtx_CLOBBER (mode, const0_rtx);
3509
3510           if (BYTES_BIG_ENDIAN)
3511             {
3512               if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3513                 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3514               if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3515                 endian_offset -= (UNITS_PER_WORD
3516                                   - GET_MODE_SIZE (GET_MODE (inner)));
3517             }
3518           /* Note if the plus_constant doesn't make a valid address
3519              then this combination won't be accepted.  */
3520           x = gen_rtx_MEM (mode,
3521                            plus_constant (XEXP (inner, 0),
3522                                           (SUBREG_WORD (x) * UNITS_PER_WORD
3523                                            + endian_offset)));
3524           RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3525           MEM_COPY_ATTRIBUTES (x, inner);
3526           return x;
3527         }
3528
3529       /* If we are in a SET_DEST, these other cases can't apply.  */
3530       if (in_dest)
3531         return x;
3532
3533       /* Changing mode twice with SUBREG => just change it once,
3534          or not at all if changing back to starting mode.  */
3535       if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3536         {
3537           if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3538               && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3539             return SUBREG_REG (SUBREG_REG (x));
3540
3541           SUBST_INT (SUBREG_WORD (x),
3542                      SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3543           SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3544         }
3545
3546       /* SUBREG of a hard register => just change the register number
3547          and/or mode.  If the hard register is not valid in that mode,
3548          suppress this combination.  If the hard register is the stack,
3549          frame, or argument pointer, leave this as a SUBREG.  */
3550
3551       if (GET_CODE (SUBREG_REG (x)) == REG
3552           && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3553           && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3554 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3555           && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3556 #endif
3557 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3558           && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3559 #endif
3560           && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3561         {
3562           if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3563                                   mode))
3564             return gen_rtx_REG (mode,
3565                                 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3566           else
3567             return gen_rtx_CLOBBER (mode, const0_rtx);
3568         }
3569
3570       /* For a constant, try to pick up the part we want.  Handle a full
3571          word and low-order part.  Only do this if we are narrowing
3572          the constant; if it is being widened, we have no idea what
3573          the extra bits will have been set to.  */
3574
3575       if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3576           && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3577           && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
3578           && GET_MODE_CLASS (mode) == MODE_INT)
3579         {
3580           temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3581                                   0, op0_mode);
3582           if (temp)
3583             return temp;
3584         }
3585         
3586       /* If we want a subreg of a constant, at offset 0,
3587          take the low bits.  On a little-endian machine, that's
3588          always valid.  On a big-endian machine, it's valid
3589          only if the constant's mode fits in one word.   Note that we
3590          cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode.  */
3591       if (CONSTANT_P (SUBREG_REG (x))
3592           && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3593               || ! WORDS_BIG_ENDIAN)
3594               ? SUBREG_WORD (x) == 0
3595               : (SUBREG_WORD (x)
3596                  == ((GET_MODE_SIZE (op0_mode)
3597                       - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3598                      / UNITS_PER_WORD)))
3599           && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
3600           && (! WORDS_BIG_ENDIAN
3601               || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
3602         return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3603
3604       /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3605          since we are saying that the high bits don't matter.  */
3606       if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3607           && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3608         return SUBREG_REG (x);
3609
3610       /* Note that we cannot do any narrowing for non-constants since
3611          we might have been counting on using the fact that some bits were
3612          zero.  We now do this in the SET.  */
3613
3614       break;
3615
3616     case NOT:
3617       /* (not (plus X -1)) can become (neg X).  */
3618       if (GET_CODE (XEXP (x, 0)) == PLUS
3619           && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3620         return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3621
3622       /* Similarly, (not (neg X)) is (plus X -1).  */
3623       if (GET_CODE (XEXP (x, 0)) == NEG)
3624         return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3625                                 constm1_rtx);
3626
3627       /* (not (xor X C)) for C constant is (xor X D) with D = ~ C.  */
3628       if (GET_CODE (XEXP (x, 0)) == XOR
3629           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3630           && (temp = simplify_unary_operation (NOT, mode,
3631                                                XEXP (XEXP (x, 0), 1),
3632                                                mode)) != 0)
3633         return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3634               
3635       /* (not (ashift 1 X)) is (rotate ~1 X).  We used to do this for operands
3636          other than 1, but that is not valid.  We could do a similar
3637          simplification for (not (lshiftrt C X)) where C is just the sign bit,
3638          but this doesn't seem common enough to bother with.  */
3639       if (GET_CODE (XEXP (x, 0)) == ASHIFT
3640           && XEXP (XEXP (x, 0), 0) == const1_rtx)
3641         return gen_rtx_ROTATE (mode, gen_unary (NOT, mode, mode, const1_rtx),
3642                                XEXP (XEXP (x, 0), 1));
3643                                             
3644       if (GET_CODE (XEXP (x, 0)) == SUBREG
3645           && subreg_lowpart_p (XEXP (x, 0))
3646           && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3647               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3648           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3649           && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3650         {
3651           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3652
3653           x = gen_rtx_ROTATE (inner_mode,
3654                               gen_unary (NOT, inner_mode, inner_mode,
3655                                          const1_rtx),
3656                               XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3657           return gen_lowpart_for_combine (mode, x);
3658         }
3659                                             
3660       /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3661          reversing the comparison code if valid.  */
3662       if (STORE_FLAG_VALUE == -1
3663           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3664           && reversible_comparison_p (XEXP (x, 0)))
3665         return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3666                                 mode, XEXP (XEXP (x, 0), 0),
3667                                 XEXP (XEXP (x, 0), 1));
3668
3669       /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3670          is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3671          perform the above simplification.  */
3672
3673       if (STORE_FLAG_VALUE == -1
3674           && XEXP (x, 1) == const1_rtx
3675           && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3676           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3677           && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3678         return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3679
3680       /* Apply De Morgan's laws to reduce number of patterns for machines
3681          with negating logical insns (and-not, nand, etc.).  If result has
3682          only one NOT, put it first, since that is how the patterns are
3683          coded.  */
3684
3685       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3686         {
3687          rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3688
3689          if (GET_CODE (in1) == NOT)
3690            in1 = XEXP (in1, 0);
3691          else
3692            in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3693
3694          if (GET_CODE (in2) == NOT)
3695            in2 = XEXP (in2, 0);
3696          else if (GET_CODE (in2) == CONST_INT
3697                   && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3698            in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
3699          else
3700            in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3701
3702          if (GET_CODE (in2) == NOT)
3703            {
3704              rtx tem = in2;
3705              in2 = in1; in1 = tem;
3706            }
3707
3708          return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3709                                  mode, in1, in2);
3710        } 
3711       break;
3712
3713     case NEG:
3714       /* (neg (plus X 1)) can become (not X).  */
3715       if (GET_CODE (XEXP (x, 0)) == PLUS
3716           && XEXP (XEXP (x, 0), 1) == const1_rtx)
3717         return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3718
3719       /* Similarly, (neg (not X)) is (plus X 1).  */
3720       if (GET_CODE (XEXP (x, 0)) == NOT)
3721         return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3722
3723       /* (neg (minus X Y)) can become (minus Y X).  */
3724       if (GET_CODE (XEXP (x, 0)) == MINUS
3725           && (! FLOAT_MODE_P (mode)
3726               /* x-y != -(y-x) with IEEE floating point.  */
3727               || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3728               || flag_fast_math))
3729         return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3730                            XEXP (XEXP (x, 0), 0));
3731
3732       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1.  */
3733       if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3734           && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3735         return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3736
3737       /* NEG commutes with ASHIFT since it is multiplication.  Only do this
3738          if we can then eliminate the NEG (e.g.,
3739          if the operand is a constant).  */
3740
3741       if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3742         {
3743           temp = simplify_unary_operation (NEG, mode,
3744                                            XEXP (XEXP (x, 0), 0), mode);
3745           if (temp)
3746             {
3747               SUBST (XEXP (XEXP (x, 0), 0), temp);
3748               return XEXP (x, 0);
3749             }
3750         }
3751
3752       temp = expand_compound_operation (XEXP (x, 0));
3753
3754       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3755          replaced by (lshiftrt X C).  This will convert
3756          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
3757
3758       if (GET_CODE (temp) == ASHIFTRT
3759           && GET_CODE (XEXP (temp, 1)) == CONST_INT
3760           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3761         return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3762                                      INTVAL (XEXP (temp, 1)));
3763
3764       /* If X has only a single bit that might be nonzero, say, bit I, convert
3765          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3766          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
3767          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
3768          or a SUBREG of one since we'd be making the expression more
3769          complex if it was just a register.  */
3770
3771       if (GET_CODE (temp) != REG
3772           && ! (GET_CODE (temp) == SUBREG
3773                 && GET_CODE (SUBREG_REG (temp)) == REG)
3774           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3775         {
3776           rtx temp1 = simplify_shift_const
3777             (NULL_RTX, ASHIFTRT, mode,
3778              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3779                                    GET_MODE_BITSIZE (mode) - 1 - i),
3780              GET_MODE_BITSIZE (mode) - 1 - i);
3781
3782           /* If all we did was surround TEMP with the two shifts, we
3783              haven't improved anything, so don't use it.  Otherwise,
3784              we are better off with TEMP1.  */
3785           if (GET_CODE (temp1) != ASHIFTRT
3786               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3787               || XEXP (XEXP (temp1, 0), 0) != temp)
3788             return temp1;
3789         }
3790       break;
3791
3792     case TRUNCATE:
3793       /* We can't handle truncation to a partial integer mode here
3794          because we don't know the real bitsize of the partial
3795          integer mode.  */
3796       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3797         break;
3798
3799       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3800           && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3801                                     GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
3802         SUBST (XEXP (x, 0),
3803                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3804                               GET_MODE_MASK (mode), NULL_RTX, 0));
3805
3806       /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI.  */
3807       if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3808            || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3809           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3810         return XEXP (XEXP (x, 0), 0);
3811
3812       /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3813          (OP:SI foo:SI) if OP is NEG or ABS.  */
3814       if ((GET_CODE (XEXP (x, 0)) == ABS
3815            || GET_CODE (XEXP (x, 0)) == NEG)
3816           && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3817               || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3818           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3819         return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3820                           XEXP (XEXP (XEXP (x, 0), 0), 0));
3821
3822       /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3823          (truncate:SI x).  */
3824       if (GET_CODE (XEXP (x, 0)) == SUBREG
3825           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3826           && subreg_lowpart_p (XEXP (x, 0)))
3827         return SUBREG_REG (XEXP (x, 0));
3828
3829       /* If we know that the value is already truncated, we can
3830          replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION is
3831          nonzero for the corresponding modes.  */
3832       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3833                                  GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
3834           && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3835              >= GET_MODE_BITSIZE (mode) + 1)
3836         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3837
3838       /* A truncate of a comparison can be replaced with a subreg if
3839          STORE_FLAG_VALUE permits.  This is like the previous test,
3840          but it works even if the comparison is done in a mode larger
3841          than HOST_BITS_PER_WIDE_INT.  */
3842       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3843           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3844           && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0)
3845         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3846
3847       /* Similarly, a truncate of a register whose value is a
3848          comparison can be replaced with a subreg if STORE_FLAG_VALUE
3849          permits.  */
3850       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3851           && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0
3852           && (temp = get_last_value (XEXP (x, 0)))
3853           && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3854         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3855
3856       break;
3857
3858     case FLOAT_TRUNCATE:
3859       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
3860       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3861           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3862         return XEXP (XEXP (x, 0), 0);
3863
3864       /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3865          (OP:SF foo:SF) if OP is NEG or ABS.  */
3866       if ((GET_CODE (XEXP (x, 0)) == ABS
3867            || GET_CODE (XEXP (x, 0)) == NEG)
3868           && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3869           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3870         return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3871                           XEXP (XEXP (XEXP (x, 0), 0), 0));
3872
3873       /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3874          is (float_truncate:SF x).  */
3875       if (GET_CODE (XEXP (x, 0)) == SUBREG
3876           && subreg_lowpart_p (XEXP (x, 0))
3877           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3878         return SUBREG_REG (XEXP (x, 0));
3879       break;  
3880
3881 #ifdef HAVE_cc0
3882     case COMPARE:
3883       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3884          using cc0, in which case we want to leave it as a COMPARE
3885          so we can distinguish it from a register-register-copy.  */
3886       if (XEXP (x, 1) == const0_rtx)
3887         return XEXP (x, 0);
3888
3889       /* In IEEE floating point, x-0 is not the same as x.  */
3890       if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3891            || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3892            || flag_fast_math)
3893           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3894         return XEXP (x, 0);
3895       break;
3896 #endif
3897
3898     case CONST:
3899       /* (const (const X)) can become (const X).  Do it this way rather than
3900          returning the inner CONST since CONST can be shared with a
3901          REG_EQUAL note.  */
3902       if (GET_CODE (XEXP (x, 0)) == CONST)
3903         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3904       break;
3905
3906 #ifdef HAVE_lo_sum
3907     case LO_SUM:
3908       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
3909          can add in an offset.  find_split_point will split this address up
3910          again if it doesn't match.  */
3911       if (GET_CODE (XEXP (x, 0)) == HIGH
3912           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3913         return XEXP (x, 1);
3914       break;
3915 #endif
3916
3917     case PLUS:
3918       /* If we have (plus (plus (A const) B)), associate it so that CONST is
3919          outermost.  That's because that's the way indexed addresses are
3920          supposed to appear.  This code used to check many more cases, but
3921          they are now checked elsewhere.  */
3922       if (GET_CODE (XEXP (x, 0)) == PLUS
3923           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3924         return gen_binary (PLUS, mode,
3925                            gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3926                                        XEXP (x, 1)),
3927                            XEXP (XEXP (x, 0), 1));
3928
3929       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3930          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3931          bit-field and can be replaced by either a sign_extend or a
3932          sign_extract.  The `and' may be a zero_extend and the two
3933          <c>, -<c> constants may be reversed.  */
3934       if (GET_CODE (XEXP (x, 0)) == XOR
3935           && GET_CODE (XEXP (x, 1)) == CONST_INT
3936           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3937           && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
3938           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
3939               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
3940           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3941           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3942                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3943                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
3944                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
3945               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3946                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3947                       == i + 1))))
3948         return simplify_shift_const
3949           (NULL_RTX, ASHIFTRT, mode,
3950            simplify_shift_const (NULL_RTX, ASHIFT, mode,
3951                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
3952                                  GET_MODE_BITSIZE (mode) - (i + 1)),
3953            GET_MODE_BITSIZE (mode) - (i + 1));
3954
3955       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3956          C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3957          is 1.  This produces better code than the alternative immediately
3958          below.  */
3959       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3960           && reversible_comparison_p (XEXP (x, 0))
3961           && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3962               || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
3963         return
3964           gen_unary (NEG, mode, mode,
3965                      gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3966                                  mode, XEXP (XEXP (x, 0), 0),
3967                                  XEXP (XEXP (x, 0), 1)));
3968
3969       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
3970          can become (ashiftrt (ashift (xor x 1) C) C) where C is
3971          the bitsize of the mode - 1.  This allows simplification of
3972          "a = (b & 8) == 0;"  */
3973       if (XEXP (x, 1) == constm1_rtx
3974           && GET_CODE (XEXP (x, 0)) != REG
3975           && ! (GET_CODE (XEXP (x,0)) == SUBREG
3976                 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
3977           && nonzero_bits (XEXP (x, 0), mode) == 1)
3978         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
3979            simplify_shift_const (NULL_RTX, ASHIFT, mode,
3980                                  gen_rtx_combine (XOR, mode,
3981                                                   XEXP (x, 0), const1_rtx),
3982                                  GET_MODE_BITSIZE (mode) - 1),
3983            GET_MODE_BITSIZE (mode) - 1);
3984
3985       /* If we are adding two things that have no bits in common, convert
3986          the addition into an IOR.  This will often be further simplified,
3987          for example in cases like ((a & 1) + (a & 2)), which can
3988          become a & 3.  */
3989
3990       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3991           && (nonzero_bits (XEXP (x, 0), mode)
3992               & nonzero_bits (XEXP (x, 1), mode)) == 0)
3993         return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
3994       break;
3995
3996     case MINUS:
3997       /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3998          by reversing the comparison code if valid.  */
3999       if (STORE_FLAG_VALUE == 1
4000           && XEXP (x, 0) == const1_rtx
4001           && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
4002           && reversible_comparison_p (XEXP (x, 1)))
4003         return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
4004                            mode, XEXP (XEXP (x, 1), 0),
4005                                 XEXP (XEXP (x, 1), 1));
4006
4007       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4008          (and <foo> (const_int pow2-1))  */
4009       if (GET_CODE (XEXP (x, 1)) == AND
4010           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4011           && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4012           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4013         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4014                                        - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4015
4016       /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4017          integers.  */
4018       if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4019         return gen_binary (MINUS, mode,
4020                            gen_binary (MINUS, mode, XEXP (x, 0),
4021                                        XEXP (XEXP (x, 1), 0)),
4022                            XEXP (XEXP (x, 1), 1));
4023       break;
4024
4025     case MULT:
4026       /* If we have (mult (plus A B) C), apply the distributive law and then
4027          the inverse distributive law to see if things simplify.  This
4028          occurs mostly in addresses, often when unrolling loops.  */
4029
4030       if (GET_CODE (XEXP (x, 0)) == PLUS)
4031         {
4032           x = apply_distributive_law
4033             (gen_binary (PLUS, mode,
4034                          gen_binary (MULT, mode,
4035                                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4036                          gen_binary (MULT, mode,
4037                                      XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
4038
4039           if (GET_CODE (x) != MULT)
4040             return x;
4041         }
4042       break;
4043
4044     case UDIV:
4045       /* If this is a divide by a power of two, treat it as a shift if
4046          its first operand is a shift.  */
4047       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4048           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4049           && (GET_CODE (XEXP (x, 0)) == ASHIFT
4050               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4051               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4052               || GET_CODE (XEXP (x, 0)) == ROTATE
4053               || GET_CODE (XEXP (x, 0)) == ROTATERT))
4054         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4055       break;
4056
4057     case EQ:  case NE:
4058     case GT:  case GTU:  case GE:  case GEU:
4059     case LT:  case LTU:  case LE:  case LEU:
4060       /* If the first operand is a condition code, we can't do anything
4061          with it.  */
4062       if (GET_CODE (XEXP (x, 0)) == COMPARE
4063           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4064 #ifdef HAVE_cc0
4065               && XEXP (x, 0) != cc0_rtx
4066 #endif
4067                ))
4068         {
4069           rtx op0 = XEXP (x, 0);
4070           rtx op1 = XEXP (x, 1);
4071           enum rtx_code new_code;
4072
4073           if (GET_CODE (op0) == COMPARE)
4074             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4075
4076           /* Simplify our comparison, if possible.  */
4077           new_code = simplify_comparison (code, &op0, &op1);
4078
4079           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4080              if only the low-order bit is possibly nonzero in X (such as when
4081              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
4082              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
4083              known to be either 0 or -1, NE becomes a NEG and EQ becomes
4084              (plus X 1).
4085
4086              Remove any ZERO_EXTRACT we made when thinking this was a
4087              comparison.  It may now be simpler to use, e.g., an AND.  If a
4088              ZERO_EXTRACT is indeed appropriate, it will be placed back by
4089              the call to make_compound_operation in the SET case.  */
4090
4091           if (STORE_FLAG_VALUE == 1
4092               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4093               && op1 == const0_rtx && nonzero_bits (op0, mode) == 1)
4094             return gen_lowpart_for_combine (mode,
4095                                             expand_compound_operation (op0));
4096
4097           else if (STORE_FLAG_VALUE == 1
4098                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4099                    && op1 == const0_rtx
4100                    && (num_sign_bit_copies (op0, mode)
4101                        == GET_MODE_BITSIZE (mode)))
4102             {
4103               op0 = expand_compound_operation (op0);
4104               return gen_unary (NEG, mode, mode,
4105                                 gen_lowpart_for_combine (mode, op0));
4106             }
4107
4108           else if (STORE_FLAG_VALUE == 1
4109                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4110                    && op1 == const0_rtx
4111                    && nonzero_bits (op0, mode) == 1)
4112             {
4113               op0 = expand_compound_operation (op0);
4114               return gen_binary (XOR, mode,
4115                                  gen_lowpart_for_combine (mode, op0),
4116                                  const1_rtx);
4117             }
4118
4119           else if (STORE_FLAG_VALUE == 1
4120                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4121                    && op1 == const0_rtx
4122                    && (num_sign_bit_copies (op0, mode)
4123                        == GET_MODE_BITSIZE (mode)))
4124             {
4125               op0 = expand_compound_operation (op0);
4126               return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4127             }
4128
4129           /* If STORE_FLAG_VALUE is -1, we have cases similar to
4130              those above.  */
4131           if (STORE_FLAG_VALUE == -1
4132               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4133               && op1 == const0_rtx
4134               && (num_sign_bit_copies (op0, mode)
4135                   == GET_MODE_BITSIZE (mode)))
4136             return gen_lowpart_for_combine (mode,
4137                                             expand_compound_operation (op0));
4138
4139           else if (STORE_FLAG_VALUE == -1
4140                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4141                    && op1 == const0_rtx
4142                    && nonzero_bits (op0, mode) == 1)
4143             {
4144               op0 = expand_compound_operation (op0);
4145               return gen_unary (NEG, mode, mode,
4146                                 gen_lowpart_for_combine (mode, op0));
4147             }
4148
4149           else if (STORE_FLAG_VALUE == -1
4150                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4151                    && op1 == const0_rtx
4152                    && (num_sign_bit_copies (op0, mode)
4153                        == GET_MODE_BITSIZE (mode)))
4154             {
4155               op0 = expand_compound_operation (op0);
4156               return gen_unary (NOT, mode, mode,
4157                                 gen_lowpart_for_combine (mode, op0));
4158             }
4159
4160           /* If X is 0/1, (eq X 0) is X-1.  */
4161           else if (STORE_FLAG_VALUE == -1
4162                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4163                    && op1 == const0_rtx
4164                    && nonzero_bits (op0, mode) == 1)
4165             {
4166               op0 = expand_compound_operation (op0);
4167               return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4168             }
4169
4170           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4171              one bit that might be nonzero, we can convert (ne x 0) to
4172              (ashift x c) where C puts the bit in the sign bit.  Remove any
4173              AND with STORE_FLAG_VALUE when we are done, since we are only
4174              going to test the sign bit.  */
4175           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4176               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4177               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4178                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE(mode)-1))
4179               && op1 == const0_rtx
4180               && mode == GET_MODE (op0)
4181               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4182             {
4183               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4184                                         expand_compound_operation (op0),
4185                                         GET_MODE_BITSIZE (mode) - 1 - i);
4186               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4187                 return XEXP (x, 0);
4188               else
4189                 return x;
4190             }
4191
4192           /* If the code changed, return a whole new comparison.  */
4193           if (new_code != code)
4194             return gen_rtx_combine (new_code, mode, op0, op1);
4195
4196           /* Otherwise, keep this operation, but maybe change its operands.  
4197              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
4198           SUBST (XEXP (x, 0), op0);
4199           SUBST (XEXP (x, 1), op1);
4200         }
4201       break;
4202           
4203     case IF_THEN_ELSE:
4204       return simplify_if_then_else (x);
4205
4206     case ZERO_EXTRACT:
4207     case SIGN_EXTRACT:
4208     case ZERO_EXTEND:
4209     case SIGN_EXTEND:
4210       /* If we are processing SET_DEST, we are done.  */
4211       if (in_dest)
4212         return x;
4213
4214       return expand_compound_operation (x);
4215
4216     case SET:
4217       return simplify_set (x);
4218
4219     case AND:
4220     case IOR:
4221     case XOR:
4222       return simplify_logical (x, last);
4223
4224     case ABS:      
4225       /* (abs (neg <foo>)) -> (abs <foo>) */
4226       if (GET_CODE (XEXP (x, 0)) == NEG)
4227         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4228
4229       /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4230          do nothing.  */
4231       if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4232         break;
4233
4234       /* If operand is something known to be positive, ignore the ABS.  */
4235       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4236           || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4237                <= HOST_BITS_PER_WIDE_INT)
4238               && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4239                    & ((HOST_WIDE_INT) 1
4240                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4241                   == 0)))
4242         return XEXP (x, 0);
4243
4244
4245       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
4246       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4247         return gen_rtx_combine (NEG, mode, XEXP (x, 0));
4248
4249       break;
4250
4251     case FFS:
4252       /* (ffs (*_extend <X>)) = (ffs <X>) */
4253       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4254           || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4255         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4256       break;
4257
4258     case FLOAT:
4259       /* (float (sign_extend <X>)) = (float <X>).  */
4260       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4261         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4262       break;
4263
4264     case ASHIFT:
4265     case LSHIFTRT:
4266     case ASHIFTRT:
4267     case ROTATE:
4268     case ROTATERT:
4269       /* If this is a shift by a constant amount, simplify it.  */
4270       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4271         return simplify_shift_const (x, code, mode, XEXP (x, 0), 
4272                                      INTVAL (XEXP (x, 1)));
4273
4274 #ifdef SHIFT_COUNT_TRUNCATED
4275       else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4276         SUBST (XEXP (x, 1),
4277                force_to_mode (XEXP (x, 1), GET_MODE (x),
4278                               ((HOST_WIDE_INT) 1 
4279                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4280                               - 1,
4281                               NULL_RTX, 0));
4282 #endif
4283
4284       break;
4285
4286     default:
4287       break;
4288     }
4289
4290   return x;
4291 }
4292 \f
4293 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
4294
4295 static rtx
4296 simplify_if_then_else (x)
4297      rtx x;
4298 {
4299   enum machine_mode mode = GET_MODE (x);
4300   rtx cond = XEXP (x, 0);
4301   rtx true = XEXP (x, 1);
4302   rtx false = XEXP (x, 2);
4303   enum rtx_code true_code = GET_CODE (cond);
4304   int comparison_p = GET_RTX_CLASS (true_code) == '<';
4305   rtx temp;
4306   int i;
4307
4308   /* Simplify storing of the truth value.  */
4309   if (comparison_p && true == const_true_rtx && false == const0_rtx)
4310     return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4311       
4312   /* Also when the truth value has to be reversed.  */
4313   if (comparison_p && reversible_comparison_p (cond)
4314       && true == const0_rtx && false == const_true_rtx)
4315     return gen_binary (reverse_condition (true_code),
4316                        mode, XEXP (cond, 0), XEXP (cond, 1));
4317
4318   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4319      in it is being compared against certain values.  Get the true and false
4320      comparisons and see if that says anything about the value of each arm.  */
4321
4322   if (comparison_p && reversible_comparison_p (cond)
4323       && GET_CODE (XEXP (cond, 0)) == REG)
4324     {
4325       HOST_WIDE_INT nzb;
4326       rtx from = XEXP (cond, 0);
4327       enum rtx_code false_code = reverse_condition (true_code);
4328       rtx true_val = XEXP (cond, 1);
4329       rtx false_val = true_val;
4330       int swapped = 0;
4331
4332       /* If FALSE_CODE is EQ, swap the codes and arms.  */
4333
4334       if (false_code == EQ)
4335         {
4336           swapped = 1, true_code = EQ, false_code = NE;
4337           temp = true, true = false, false = temp;
4338         }
4339
4340       /* If we are comparing against zero and the expression being tested has
4341          only a single bit that might be nonzero, that is its value when it is
4342          not equal to zero.  Similarly if it is known to be -1 or 0.  */
4343
4344       if (true_code == EQ && true_val == const0_rtx
4345           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4346         false_code = EQ, false_val = GEN_INT (nzb);
4347       else if (true_code == EQ && true_val == const0_rtx
4348                && (num_sign_bit_copies (from, GET_MODE (from))
4349                    == GET_MODE_BITSIZE (GET_MODE (from))))
4350         false_code = EQ, false_val = constm1_rtx;
4351
4352       /* Now simplify an arm if we know the value of the register in the
4353          branch and it is used in the arm.  Be careful due to the potential
4354          of locally-shared RTL.  */
4355
4356       if (reg_mentioned_p (from, true))
4357         true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4358                       pc_rtx, pc_rtx, 0, 0);
4359       if (reg_mentioned_p (from, false))
4360         false = subst (known_cond (copy_rtx (false), false_code,
4361                                    from, false_val),
4362                        pc_rtx, pc_rtx, 0, 0);
4363
4364       SUBST (XEXP (x, 1), swapped ? false : true);
4365       SUBST (XEXP (x, 2), swapped ? true : false);
4366
4367       true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4368     }
4369
4370   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4371      reversed, do so to avoid needing two sets of patterns for
4372      subtract-and-branch insns.  Similarly if we have a constant in the true
4373      arm, the false arm is the same as the first operand of the comparison, or
4374      the false arm is more complicated than the true arm.  */
4375
4376   if (comparison_p && reversible_comparison_p (cond)
4377       && (true == pc_rtx 
4378           || (CONSTANT_P (true)
4379               && GET_CODE (false) != CONST_INT && false != pc_rtx)
4380           || true == const0_rtx
4381           || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4382               && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4383           || (GET_CODE (true) == SUBREG
4384               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4385               && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4386           || reg_mentioned_p (true, false)
4387           || rtx_equal_p (false, XEXP (cond, 0))))
4388     {
4389       true_code = reverse_condition (true_code);
4390       SUBST (XEXP (x, 0),
4391              gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4392                          XEXP (cond, 1)));
4393
4394       SUBST (XEXP (x, 1), false);
4395       SUBST (XEXP (x, 2), true);
4396
4397       temp = true, true = false, false = temp, cond = XEXP (x, 0);
4398
4399       /* It is possible that the conditional has been simplified out.  */
4400       true_code = GET_CODE (cond);
4401       comparison_p = GET_RTX_CLASS (true_code) == '<';
4402     }
4403
4404   /* If the two arms are identical, we don't need the comparison.  */
4405
4406   if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4407     return true;
4408
4409   /* Convert a == b ? b : a to "a".  */
4410   if (true_code == EQ && ! side_effects_p (cond)
4411       && rtx_equal_p (XEXP (cond, 0), false)
4412       && rtx_equal_p (XEXP (cond, 1), true))
4413     return false;
4414   else if (true_code == NE && ! side_effects_p (cond)
4415            && rtx_equal_p (XEXP (cond, 0), true)
4416            && rtx_equal_p (XEXP (cond, 1), false))
4417     return true;
4418
4419   /* Look for cases where we have (abs x) or (neg (abs X)).  */
4420
4421   if (GET_MODE_CLASS (mode) == MODE_INT
4422       && GET_CODE (false) == NEG
4423       && rtx_equal_p (true, XEXP (false, 0))
4424       && comparison_p
4425       && rtx_equal_p (true, XEXP (cond, 0))
4426       && ! side_effects_p (true))
4427     switch (true_code)
4428       {
4429       case GT:
4430       case GE:
4431         return gen_unary (ABS, mode, mode, true);
4432       case LT:
4433       case LE:
4434         return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
4435     default:
4436       break;
4437       }
4438
4439   /* Look for MIN or MAX.  */
4440
4441   if ((! FLOAT_MODE_P (mode) || flag_fast_math)
4442       && comparison_p
4443       && rtx_equal_p (XEXP (cond, 0), true)
4444       && rtx_equal_p (XEXP (cond, 1), false)
4445       && ! side_effects_p (cond))
4446     switch (true_code)
4447       {
4448       case GE:
4449       case GT:
4450         return gen_binary (SMAX, mode, true, false);
4451       case LE:
4452       case LT:
4453         return gen_binary (SMIN, mode, true, false);
4454       case GEU:
4455       case GTU:
4456         return gen_binary (UMAX, mode, true, false);
4457       case LEU:
4458       case LTU:
4459         return gen_binary (UMIN, mode, true, false);
4460       default:
4461         break;
4462       }
4463   
4464   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4465      second operand is zero, this can be done as (OP Z (mult COND C2)) where
4466      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4467      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4468      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4469      neither 1 or -1, but it isn't worth checking for.  */
4470
4471   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4472       && comparison_p && mode != VOIDmode && ! side_effects_p (x))
4473     {
4474       rtx t = make_compound_operation (true, SET);
4475       rtx f = make_compound_operation (false, SET);
4476       rtx cond_op0 = XEXP (cond, 0);
4477       rtx cond_op1 = XEXP (cond, 1);
4478       enum rtx_code op = NIL, extend_op = NIL;
4479       enum machine_mode m = mode;
4480       rtx z = 0, c1 = NULL_RTX;
4481
4482       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4483            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4484            || GET_CODE (t) == ASHIFT
4485            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4486           && rtx_equal_p (XEXP (t, 0), f))
4487         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4488
4489       /* If an identity-zero op is commutative, check whether there
4490          would be a match if we swapped the operands.  */
4491       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4492                 || GET_CODE (t) == XOR)
4493                && rtx_equal_p (XEXP (t, 1), f))
4494         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4495       else if (GET_CODE (t) == SIGN_EXTEND
4496                && (GET_CODE (XEXP (t, 0)) == PLUS
4497                    || GET_CODE (XEXP (t, 0)) == MINUS
4498                    || GET_CODE (XEXP (t, 0)) == IOR
4499                    || GET_CODE (XEXP (t, 0)) == XOR
4500                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4501                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4502                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4503                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4504                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4505                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4506                && (num_sign_bit_copies (f, GET_MODE (f))
4507                    > (GET_MODE_BITSIZE (mode)
4508                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4509         {
4510           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4511           extend_op = SIGN_EXTEND;
4512           m = GET_MODE (XEXP (t, 0));
4513         }
4514       else if (GET_CODE (t) == SIGN_EXTEND
4515                && (GET_CODE (XEXP (t, 0)) == PLUS
4516                    || GET_CODE (XEXP (t, 0)) == IOR
4517                    || GET_CODE (XEXP (t, 0)) == XOR)
4518                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4519                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4520                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4521                && (num_sign_bit_copies (f, GET_MODE (f))
4522                    > (GET_MODE_BITSIZE (mode)
4523                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4524         {
4525           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4526           extend_op = SIGN_EXTEND;
4527           m = GET_MODE (XEXP (t, 0));
4528         }
4529       else if (GET_CODE (t) == ZERO_EXTEND
4530                && (GET_CODE (XEXP (t, 0)) == PLUS
4531                    || GET_CODE (XEXP (t, 0)) == MINUS
4532                    || GET_CODE (XEXP (t, 0)) == IOR
4533                    || GET_CODE (XEXP (t, 0)) == XOR
4534                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4535                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4536                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4537                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4538                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4539                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4540                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4541                && ((nonzero_bits (f, GET_MODE (f))
4542                     & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4543                    == 0))
4544         {
4545           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4546           extend_op = ZERO_EXTEND;
4547           m = GET_MODE (XEXP (t, 0));
4548         }
4549       else if (GET_CODE (t) == ZERO_EXTEND
4550                && (GET_CODE (XEXP (t, 0)) == PLUS
4551                    || GET_CODE (XEXP (t, 0)) == IOR
4552                    || GET_CODE (XEXP (t, 0)) == XOR)
4553                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4554                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4555                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4556                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4557                && ((nonzero_bits (f, GET_MODE (f))
4558                     & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4559                    == 0))
4560         {
4561           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4562           extend_op = ZERO_EXTEND;
4563           m = GET_MODE (XEXP (t, 0));
4564         }
4565       
4566       if (z)
4567         {
4568           temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4569                         pc_rtx, pc_rtx, 0, 0);
4570           temp = gen_binary (MULT, m, temp,
4571                              gen_binary (MULT, m, c1, const_true_rtx));
4572           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4573           temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4574
4575           if (extend_op != NIL)
4576             temp = gen_unary (extend_op, mode, m, temp);
4577
4578           return temp;
4579         }
4580     }
4581
4582   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4583      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4584      negation of a single bit, we can convert this operation to a shift.  We
4585      can actually do this more generally, but it doesn't seem worth it.  */
4586
4587   if (true_code == NE && XEXP (cond, 1) == const0_rtx
4588       && false == const0_rtx && GET_CODE (true) == CONST_INT
4589       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4590            && (i = exact_log2 (INTVAL (true))) >= 0)
4591           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4592                == GET_MODE_BITSIZE (mode))
4593               && (i = exact_log2 (- INTVAL (true))) >= 0)))
4594     return
4595       simplify_shift_const (NULL_RTX, ASHIFT, mode,
4596                             gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4597
4598   return x;
4599 }
4600 \f
4601 /* Simplify X, a SET expression.  Return the new expression.  */
4602
4603 static rtx
4604 simplify_set (x)
4605      rtx x;
4606 {
4607   rtx src = SET_SRC (x);
4608   rtx dest = SET_DEST (x);
4609   enum machine_mode mode
4610     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4611   rtx other_insn;
4612   rtx *cc_use;
4613
4614   /* (set (pc) (return)) gets written as (return).  */
4615   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4616     return src;
4617
4618   /* Now that we know for sure which bits of SRC we are using, see if we can
4619      simplify the expression for the object knowing that we only need the
4620      low-order bits.  */
4621
4622   if (GET_MODE_CLASS (mode) == MODE_INT)
4623     {
4624       src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4625       SUBST (SET_SRC (x), src);
4626     }
4627
4628   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4629      the comparison result and try to simplify it unless we already have used
4630      undobuf.other_insn.  */
4631   if ((GET_CODE (src) == COMPARE
4632 #ifdef HAVE_cc0
4633        || dest == cc0_rtx
4634 #endif
4635        )
4636       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4637       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4638       && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4639       && rtx_equal_p (XEXP (*cc_use, 0), dest))
4640     {
4641       enum rtx_code old_code = GET_CODE (*cc_use);
4642       enum rtx_code new_code;
4643       rtx op0, op1;
4644       int other_changed = 0;
4645       enum machine_mode compare_mode = GET_MODE (dest);
4646
4647       if (GET_CODE (src) == COMPARE)
4648         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4649       else
4650         op0 = src, op1 = const0_rtx;
4651
4652       /* Simplify our comparison, if possible.  */
4653       new_code = simplify_comparison (old_code, &op0, &op1);
4654
4655 #ifdef EXTRA_CC_MODES
4656       /* If this machine has CC modes other than CCmode, check to see if we
4657          need to use a different CC mode here.  */
4658       compare_mode = SELECT_CC_MODE (new_code, op0, op1);
4659 #endif /* EXTRA_CC_MODES */
4660
4661 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
4662       /* If the mode changed, we have to change SET_DEST, the mode in the
4663          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
4664          a hard register, just build new versions with the proper mode.  If it
4665          is a pseudo, we lose unless it is only time we set the pseudo, in
4666          which case we can safely change its mode.  */
4667       if (compare_mode != GET_MODE (dest))
4668         {
4669           int regno = REGNO (dest);
4670           rtx new_dest = gen_rtx_REG (compare_mode, regno);
4671
4672           if (regno < FIRST_PSEUDO_REGISTER
4673               || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
4674             {
4675               if (regno >= FIRST_PSEUDO_REGISTER)
4676                 SUBST (regno_reg_rtx[regno], new_dest);
4677
4678               SUBST (SET_DEST (x), new_dest);
4679               SUBST (XEXP (*cc_use, 0), new_dest);
4680               other_changed = 1;
4681
4682               dest = new_dest;
4683             }
4684         }
4685 #endif
4686
4687       /* If the code changed, we have to build a new comparison in
4688          undobuf.other_insn.  */
4689       if (new_code != old_code)
4690         {
4691           unsigned HOST_WIDE_INT mask;
4692
4693           SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4694                                            dest, const0_rtx));
4695
4696           /* If the only change we made was to change an EQ into an NE or
4697              vice versa, OP0 has only one bit that might be nonzero, and OP1
4698              is zero, check if changing the user of the condition code will
4699              produce a valid insn.  If it won't, we can keep the original code
4700              in that insn by surrounding our operation with an XOR.  */
4701
4702           if (((old_code == NE && new_code == EQ)
4703                || (old_code == EQ && new_code == NE))
4704               && ! other_changed && op1 == const0_rtx
4705               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4706               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
4707             {
4708               rtx pat = PATTERN (other_insn), note = 0;
4709
4710               if ((recog_for_combine (&pat, other_insn, &note) < 0
4711                    && ! check_asm_operands (pat)))
4712                 {
4713                   PUT_CODE (*cc_use, old_code);
4714                   other_insn = 0;
4715
4716                   op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
4717                 }
4718             }
4719
4720           other_changed = 1;
4721         }
4722
4723       if (other_changed)
4724         undobuf.other_insn = other_insn;
4725
4726 #ifdef HAVE_cc0
4727       /* If we are now comparing against zero, change our source if
4728          needed.  If we do not use cc0, we always have a COMPARE.  */
4729       if (op1 == const0_rtx && dest == cc0_rtx)
4730         {
4731           SUBST (SET_SRC (x), op0);
4732           src = op0;
4733         }
4734       else
4735 #endif
4736
4737       /* Otherwise, if we didn't previously have a COMPARE in the
4738          correct mode, we need one.  */
4739       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4740         {
4741           SUBST (SET_SRC (x),
4742                  gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4743           src = SET_SRC (x);
4744         }
4745       else
4746         {
4747           /* Otherwise, update the COMPARE if needed.  */
4748           SUBST (XEXP (src, 0), op0);
4749           SUBST (XEXP (src, 1), op1);
4750         }
4751     }
4752   else
4753     {
4754       /* Get SET_SRC in a form where we have placed back any
4755          compound expressions.  Then do the checks below.  */
4756       src = make_compound_operation (src, SET);
4757       SUBST (SET_SRC (x), src);
4758     }
4759
4760   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4761      and X being a REG or (subreg (reg)), we may be able to convert this to
4762      (set (subreg:m2 x) (op)). 
4763
4764      We can always do this if M1 is narrower than M2 because that means that
4765      we only care about the low bits of the result.
4766
4767      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
4768      perform a narrower operation than requested since the high-order bits will
4769      be undefined.  On machine where it is defined, this transformation is safe
4770      as long as M1 and M2 have the same number of words.  */
4771  
4772   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4773       && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4774       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4775            / UNITS_PER_WORD)
4776           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4777                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
4778 #ifndef WORD_REGISTER_OPERATIONS
4779       && (GET_MODE_SIZE (GET_MODE (src))
4780           < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4781 #endif
4782 #ifdef CLASS_CANNOT_CHANGE_SIZE
4783       && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4784             && (TEST_HARD_REG_BIT
4785                 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4786                  REGNO (dest)))
4787             && (GET_MODE_SIZE (GET_MODE (src))
4788                 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4789 #endif                            
4790       && (GET_CODE (dest) == REG
4791           || (GET_CODE (dest) == SUBREG
4792               && GET_CODE (SUBREG_REG (dest)) == REG)))
4793     {
4794       SUBST (SET_DEST (x),
4795              gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4796                                       dest));
4797       SUBST (SET_SRC (x), SUBREG_REG (src));
4798
4799       src = SET_SRC (x), dest = SET_DEST (x);
4800     }
4801
4802 #ifdef LOAD_EXTEND_OP
4803   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4804      would require a paradoxical subreg.  Replace the subreg with a
4805      zero_extend to avoid the reload that would otherwise be required.  */
4806
4807   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4808       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4809       && SUBREG_WORD (src) == 0
4810       && (GET_MODE_SIZE (GET_MODE (src))
4811           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4812       && GET_CODE (SUBREG_REG (src)) == MEM)
4813     {
4814       SUBST (SET_SRC (x),
4815              gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4816                               GET_MODE (src), XEXP (src, 0)));
4817
4818       src = SET_SRC (x);
4819     }
4820 #endif
4821
4822   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4823      are comparing an item known to be 0 or -1 against 0, use a logical
4824      operation instead. Check for one of the arms being an IOR of the other
4825      arm with some value.  We compute three terms to be IOR'ed together.  In
4826      practice, at most two will be nonzero.  Then we do the IOR's.  */
4827
4828   if (GET_CODE (dest) != PC
4829       && GET_CODE (src) == IF_THEN_ELSE
4830       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
4831       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4832       && XEXP (XEXP (src, 0), 1) == const0_rtx
4833       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
4834 #ifdef HAVE_conditional_move
4835       && ! can_conditionally_move_p (GET_MODE (src))
4836 #endif
4837       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4838                                GET_MODE (XEXP (XEXP (src, 0), 0)))
4839           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4840       && ! side_effects_p (src))
4841     {
4842       rtx true = (GET_CODE (XEXP (src, 0)) == NE
4843                       ? XEXP (src, 1) : XEXP (src, 2));
4844       rtx false = (GET_CODE (XEXP (src, 0)) == NE
4845                    ? XEXP (src, 2) : XEXP (src, 1));
4846       rtx term1 = const0_rtx, term2, term3;
4847
4848       if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4849         term1 = false, true = XEXP (true, 1), false = const0_rtx;
4850       else if (GET_CODE (true) == IOR
4851                && rtx_equal_p (XEXP (true, 1), false))
4852         term1 = false, true = XEXP (true, 0), false = const0_rtx;
4853       else if (GET_CODE (false) == IOR
4854                && rtx_equal_p (XEXP (false, 0), true))
4855         term1 = true, false = XEXP (false, 1), true = const0_rtx;
4856       else if (GET_CODE (false) == IOR
4857                && rtx_equal_p (XEXP (false, 1), true))
4858         term1 = true, false = XEXP (false, 0), true = const0_rtx;
4859
4860       term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4861       term3 = gen_binary (AND, GET_MODE (src),
4862                           gen_unary (NOT, GET_MODE (src), GET_MODE (src),
4863                                      XEXP (XEXP (src, 0), 0)),
4864                           false);
4865
4866       SUBST (SET_SRC (x),
4867              gen_binary (IOR, GET_MODE (src),
4868                          gen_binary (IOR, GET_MODE (src), term1, term2),
4869                          term3));
4870
4871       src = SET_SRC (x);
4872     }
4873
4874 #ifdef HAVE_conditional_arithmetic
4875   /* If we have conditional arithmetic and the operand of a SET is
4876      a conditional expression, replace this with an IF_THEN_ELSE.
4877      We can either have a conditional expression or a MULT of that expression
4878      with a constant.  */
4879   if ((GET_RTX_CLASS (GET_CODE (src)) == '1'
4880        || GET_RTX_CLASS (GET_CODE (src)) == '2'
4881        || GET_RTX_CLASS (GET_CODE (src)) == 'c')
4882       && (GET_RTX_CLASS (GET_CODE (XEXP (src, 0))) == '<'
4883           || (GET_CODE (XEXP (src, 0)) == MULT
4884               && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (src, 0), 0))) == '<'
4885               && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT)))
4886     {
4887       rtx cond = XEXP (src, 0);
4888       rtx true_val = const1_rtx;
4889       rtx false_arm, true_arm;
4890
4891       if (GET_CODE (cond) == MULT)
4892         {
4893           true_val = XEXP (cond, 1);
4894           cond = XEXP (cond, 0);
4895         }
4896
4897       if (GET_RTX_CLASS (GET_CODE (src)) == '1')
4898         {
4899           true_arm = gen_unary (GET_CODE (src), GET_MODE (src),
4900                                 GET_MODE (XEXP (src, 0)), true_val);
4901           false_arm = gen_unary (GET_CODE (src), GET_MODE (src),
4902                                  GET_MODE (XEXP (src, 0)), const0_rtx);
4903         }
4904       else
4905         {
4906           true_arm = gen_binary (GET_CODE (src), GET_MODE (src),
4907                                  true_val, XEXP (src, 1));
4908           false_arm = gen_binary (GET_CODE (src), GET_MODE (src),
4909                                   const0_rtx, XEXP (src, 1));
4910         }
4911
4912       /* Canonicalize if true_arm is the simpler one.  */
4913       if (GET_RTX_CLASS (GET_CODE (true_arm)) == 'o'
4914           && GET_RTX_CLASS (GET_CODE (false_arm)) != 'o'
4915           && reversible_comparison_p (cond))
4916         {
4917           rtx temp = true_arm;
4918
4919           true_arm = false_arm;
4920           false_arm = temp;
4921
4922           cond = gen_rtx_combine (reverse_condition (GET_CODE (cond)),
4923                                   GET_MODE (cond), XEXP (cond, 0),
4924                                   XEXP (cond, 1));
4925         }
4926
4927       src = gen_rtx_combine (IF_THEN_ELSE, GET_MODE (src),
4928                              gen_rtx_combine (GET_CODE (cond), VOIDmode,
4929                                               XEXP (cond, 0),
4930                                               XEXP (cond, 1)),
4931                              true_arm, false_arm);
4932       SUBST (SET_SRC (x), src);
4933     }
4934 #endif
4935
4936   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
4937      whole thing fail.  */
4938   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
4939     return src;
4940   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
4941     return dest;
4942   else
4943     /* Convert this into a field assignment operation, if possible.  */
4944     return make_field_assignment (x);
4945 }
4946 \f
4947 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
4948    result.  LAST is nonzero if this is the last retry.  */
4949
4950 static rtx
4951 simplify_logical (x, last)
4952      rtx x;
4953      int last;
4954 {
4955   enum machine_mode mode = GET_MODE (x);
4956   rtx op0 = XEXP (x, 0);
4957   rtx op1 = XEXP (x, 1);
4958
4959   switch (GET_CODE (x))
4960     {
4961     case AND:
4962       /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4963          insn (and may simplify more).  */
4964       if (GET_CODE (op0) == XOR
4965           && rtx_equal_p (XEXP (op0, 0), op1)
4966           && ! side_effects_p (op1))
4967         x = gen_binary (AND, mode,
4968                         gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
4969
4970       if (GET_CODE (op0) == XOR
4971           && rtx_equal_p (XEXP (op0, 1), op1)
4972           && ! side_effects_p (op1))
4973         x = gen_binary (AND, mode,
4974                         gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
4975
4976       /* Similarly for (~ (A ^ B)) & A.  */
4977       if (GET_CODE (op0) == NOT
4978           && GET_CODE (XEXP (op0, 0)) == XOR
4979           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4980           && ! side_effects_p (op1))
4981         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4982
4983       if (GET_CODE (op0) == NOT
4984           && GET_CODE (XEXP (op0, 0)) == XOR
4985           && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4986           && ! side_effects_p (op1))
4987         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4988
4989       /* We can call simplify_and_const_int only if we don't lose
4990          any (sign) bits when converting INTVAL (op1) to
4991          "unsigned HOST_WIDE_INT".  */
4992       if (GET_CODE (op1) == CONST_INT
4993           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4994               || INTVAL (op1) > 0))
4995         {
4996           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
4997
4998           /* If we have (ior (and (X C1) C2)) and the next restart would be
4999              the last, simplify this by making C1 as small as possible
5000              and then exit.  */
5001           if (last
5002               && GET_CODE (x) == IOR && GET_CODE (op0) == AND
5003               && GET_CODE (XEXP (op0, 1)) == CONST_INT
5004               && GET_CODE (op1) == CONST_INT)
5005             return gen_binary (IOR, mode,
5006                                gen_binary (AND, mode, XEXP (op0, 0),
5007                                            GEN_INT (INTVAL (XEXP (op0, 1))
5008                                                     & ~ INTVAL (op1))), op1);
5009
5010           if (GET_CODE (x) != AND)
5011             return x;
5012
5013           if (GET_RTX_CLASS (GET_CODE (x)) == 'c' 
5014               || GET_RTX_CLASS (GET_CODE (x)) == '2')
5015             op0 = XEXP (x, 0), op1 = XEXP (x, 1);
5016         }
5017
5018       /* Convert (A | B) & A to A.  */
5019       if (GET_CODE (op0) == IOR
5020           && (rtx_equal_p (XEXP (op0, 0), op1)
5021               || rtx_equal_p (XEXP (op0, 1), op1))
5022           && ! side_effects_p (XEXP (op0, 0))
5023           && ! side_effects_p (XEXP (op0, 1)))
5024         return op1;
5025
5026       /* In the following group of tests (and those in case IOR below),
5027          we start with some combination of logical operations and apply
5028          the distributive law followed by the inverse distributive law.
5029          Most of the time, this results in no change.  However, if some of
5030          the operands are the same or inverses of each other, simplifications
5031          will result.
5032
5033          For example, (and (ior A B) (not B)) can occur as the result of
5034          expanding a bit field assignment.  When we apply the distributive
5035          law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5036          which then simplifies to (and (A (not B))). 
5037
5038          If we have (and (ior A B) C), apply the distributive law and then
5039          the inverse distributive law to see if things simplify.  */
5040
5041       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5042         {
5043           x = apply_distributive_law
5044             (gen_binary (GET_CODE (op0), mode,
5045                          gen_binary (AND, mode, XEXP (op0, 0), op1),
5046                          gen_binary (AND, mode, XEXP (op0, 1), op1)));
5047           if (GET_CODE (x) != AND)
5048             return x;
5049         }
5050
5051       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5052         return apply_distributive_law
5053           (gen_binary (GET_CODE (op1), mode,
5054                        gen_binary (AND, mode, XEXP (op1, 0), op0),
5055                        gen_binary (AND, mode, XEXP (op1, 1), op0)));
5056
5057       /* Similarly, taking advantage of the fact that
5058          (and (not A) (xor B C)) == (xor (ior A B) (ior A C))  */
5059
5060       if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5061         return apply_distributive_law
5062           (gen_binary (XOR, mode,
5063                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5064                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
5065                                                             
5066       else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5067         return apply_distributive_law
5068           (gen_binary (XOR, mode,
5069                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5070                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
5071       break;
5072
5073     case IOR:
5074       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
5075       if (GET_CODE (op1) == CONST_INT
5076           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5077           && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
5078         return op1;
5079
5080       /* Convert (A & B) | A to A.  */
5081       if (GET_CODE (op0) == AND
5082           && (rtx_equal_p (XEXP (op0, 0), op1)
5083               || rtx_equal_p (XEXP (op0, 1), op1))
5084           && ! side_effects_p (XEXP (op0, 0))
5085           && ! side_effects_p (XEXP (op0, 1)))
5086         return op1;
5087
5088       /* If we have (ior (and A B) C), apply the distributive law and then
5089          the inverse distributive law to see if things simplify.  */
5090
5091       if (GET_CODE (op0) == AND)
5092         {
5093           x = apply_distributive_law
5094             (gen_binary (AND, mode,
5095                          gen_binary (IOR, mode, XEXP (op0, 0), op1),
5096                          gen_binary (IOR, mode, XEXP (op0, 1), op1)));
5097
5098           if (GET_CODE (x) != IOR)
5099             return x;
5100         }
5101
5102       if (GET_CODE (op1) == AND)
5103         {
5104           x = apply_distributive_law
5105             (gen_binary (AND, mode,
5106                          gen_binary (IOR, mode, XEXP (op1, 0), op0),
5107                          gen_binary (IOR, mode, XEXP (op1, 1), op0)));
5108
5109           if (GET_CODE (x) != IOR)
5110             return x;
5111         }
5112
5113       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5114          mode size to (rotate A CX).  */
5115
5116       if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5117            || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5118           && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5119           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5120           && GET_CODE (XEXP (op1, 1)) == CONST_INT
5121           && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5122               == GET_MODE_BITSIZE (mode)))
5123         return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5124                                (GET_CODE (op0) == ASHIFT
5125                                 ? XEXP (op0, 1) : XEXP (op1, 1)));
5126
5127       /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5128          a (sign_extend (plus ...)).  If so, OP1 is a CONST_INT, and the PLUS
5129          does not affect any of the bits in OP1, it can really be done
5130          as a PLUS and we can associate.  We do this by seeing if OP1
5131          can be safely shifted left C bits.  */
5132       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5133           && GET_CODE (XEXP (op0, 0)) == PLUS
5134           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5135           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5136           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5137         {
5138           int count = INTVAL (XEXP (op0, 1));
5139           HOST_WIDE_INT mask = INTVAL (op1) << count;
5140
5141           if (mask >> count == INTVAL (op1)
5142               && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5143             {
5144               SUBST (XEXP (XEXP (op0, 0), 1),
5145                      GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5146               return op0;
5147             }
5148         }
5149       break;
5150
5151     case XOR:
5152       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5153          Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5154          (NOT y).  */
5155       {
5156         int num_negated = 0;
5157
5158         if (GET_CODE (op0) == NOT)
5159           num_negated++, op0 = XEXP (op0, 0);
5160         if (GET_CODE (op1) == NOT)
5161           num_negated++, op1 = XEXP (op1, 0);
5162
5163         if (num_negated == 2)
5164           {
5165             SUBST (XEXP (x, 0), op0);
5166             SUBST (XEXP (x, 1), op1);
5167           }
5168         else if (num_negated == 1)
5169           return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
5170       }
5171
5172       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
5173          correspond to a machine insn or result in further simplifications
5174          if B is a constant.  */
5175
5176       if (GET_CODE (op0) == AND
5177           && rtx_equal_p (XEXP (op0, 1), op1)
5178           && ! side_effects_p (op1))
5179         return gen_binary (AND, mode,
5180                            gen_unary (NOT, mode, mode, XEXP (op0, 0)),
5181                            op1);
5182
5183       else if (GET_CODE (op0) == AND
5184                && rtx_equal_p (XEXP (op0, 0), op1)
5185                && ! side_effects_p (op1))
5186         return gen_binary (AND, mode,
5187                            gen_unary (NOT, mode, mode, XEXP (op0, 1)),
5188                            op1);
5189
5190       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5191          comparison if STORE_FLAG_VALUE is 1.  */
5192       if (STORE_FLAG_VALUE == 1
5193           && op1 == const1_rtx
5194           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5195           && reversible_comparison_p (op0))
5196         return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5197                                 mode, XEXP (op0, 0), XEXP (op0, 1));
5198
5199       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5200          is (lt foo (const_int 0)), so we can perform the above
5201          simplification if STORE_FLAG_VALUE is 1.  */
5202
5203       if (STORE_FLAG_VALUE == 1
5204           && op1 == const1_rtx
5205           && GET_CODE (op0) == LSHIFTRT
5206           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5207           && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5208         return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
5209
5210       /* (xor (comparison foo bar) (const_int sign-bit))
5211          when STORE_FLAG_VALUE is the sign bit.  */
5212       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5213           && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5214               == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5215           && op1 == const_true_rtx
5216           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5217           && reversible_comparison_p (op0))
5218         return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5219                                 mode, XEXP (op0, 0), XEXP (op0, 1));
5220       break;
5221
5222     default:
5223       abort ();
5224     }
5225
5226   return x;
5227 }
5228 \f
5229 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5230    operations" because they can be replaced with two more basic operations.
5231    ZERO_EXTEND is also considered "compound" because it can be replaced with
5232    an AND operation, which is simpler, though only one operation.
5233
5234    The function expand_compound_operation is called with an rtx expression
5235    and will convert it to the appropriate shifts and AND operations, 
5236    simplifying at each stage.
5237
5238    The function make_compound_operation is called to convert an expression
5239    consisting of shifts and ANDs into the equivalent compound expression.
5240    It is the inverse of this function, loosely speaking.  */
5241
5242 static rtx
5243 expand_compound_operation (x)
5244      rtx x;
5245 {
5246   int pos = 0, len;
5247   int unsignedp = 0;
5248   int modewidth;
5249   rtx tem;
5250
5251   switch (GET_CODE (x))
5252     {
5253     case ZERO_EXTEND:
5254       unsignedp = 1;
5255     case SIGN_EXTEND:
5256       /* We can't necessarily use a const_int for a multiword mode;
5257          it depends on implicitly extending the value.
5258          Since we don't know the right way to extend it,
5259          we can't tell whether the implicit way is right.
5260
5261          Even for a mode that is no wider than a const_int,
5262          we can't win, because we need to sign extend one of its bits through
5263          the rest of it, and we don't know which bit.  */
5264       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5265         return x;
5266
5267       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5268          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
5269          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5270          reloaded. If not for that, MEM's would very rarely be safe.
5271
5272          Reject MODEs bigger than a word, because we might not be able
5273          to reference a two-register group starting with an arbitrary register
5274          (and currently gen_lowpart might crash for a SUBREG).  */
5275   
5276       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5277         return x;
5278
5279       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5280       /* If the inner object has VOIDmode (the only way this can happen
5281          is if it is a ASM_OPERANDS), we can't do anything since we don't
5282          know how much masking to do.  */
5283       if (len == 0)
5284         return x;
5285
5286       break;
5287
5288     case ZERO_EXTRACT:
5289       unsignedp = 1;
5290     case SIGN_EXTRACT:
5291       /* If the operand is a CLOBBER, just return it.  */
5292       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5293         return XEXP (x, 0);
5294
5295       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5296           || GET_CODE (XEXP (x, 2)) != CONST_INT
5297           || GET_MODE (XEXP (x, 0)) == VOIDmode)
5298         return x;
5299
5300       len = INTVAL (XEXP (x, 1));
5301       pos = INTVAL (XEXP (x, 2));
5302
5303       /* If this goes outside the object being extracted, replace the object
5304          with a (use (mem ...)) construct that only combine understands
5305          and is used only for this purpose.  */
5306       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5307         SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5308
5309       if (BITS_BIG_ENDIAN)
5310         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5311
5312       break;
5313
5314     default:
5315       return x;
5316     }
5317
5318   /* We can optimize some special cases of ZERO_EXTEND.  */
5319   if (GET_CODE (x) == ZERO_EXTEND)
5320     {
5321       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5322          know that the last value didn't have any inappropriate bits
5323          set.  */
5324       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5325           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5326           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5327           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5328               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5329         return XEXP (XEXP (x, 0), 0);
5330
5331       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5332       if (GET_CODE (XEXP (x, 0)) == SUBREG
5333           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5334           && subreg_lowpart_p (XEXP (x, 0))
5335           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5336           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5337               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5338         return SUBREG_REG (XEXP (x, 0));
5339
5340       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5341          is a comparison and STORE_FLAG_VALUE permits.  This is like
5342          the first case, but it works even when GET_MODE (x) is larger
5343          than HOST_WIDE_INT.  */
5344       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5345           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5346           && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5347           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5348               <= HOST_BITS_PER_WIDE_INT)
5349           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5350               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5351         return XEXP (XEXP (x, 0), 0);
5352
5353       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5354       if (GET_CODE (XEXP (x, 0)) == SUBREG
5355           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5356           && subreg_lowpart_p (XEXP (x, 0))
5357           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5358           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5359               <= HOST_BITS_PER_WIDE_INT)
5360           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5361               & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5362         return SUBREG_REG (XEXP (x, 0));
5363
5364       /* If sign extension is cheaper than zero extension, then use it
5365          if we know that no extraneous bits are set, and that the high
5366          bit is not set.  */
5367       if (flag_expensive_optimizations
5368           && ((GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5369                && ((nonzero_bits (XEXP (x, 0), GET_MODE (x))
5370                     & ~ (((unsigned HOST_WIDE_INT)
5371                           GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5372                          >> 1))
5373                    == 0))
5374               || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5375                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5376                       <= HOST_BITS_PER_WIDE_INT)
5377                   && (((HOST_WIDE_INT) STORE_FLAG_VALUE
5378                        & ~ (((unsigned HOST_WIDE_INT)
5379                              GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5380                             >> 1))
5381                       == 0))))
5382         {
5383           rtx temp = gen_rtx_SIGN_EXTEND (GET_MODE (x), XEXP (x, 0));
5384
5385           if (rtx_cost (temp, SET) < rtx_cost (x, SET))
5386             return expand_compound_operation (temp);
5387         }
5388     }
5389
5390   /* If we reach here, we want to return a pair of shifts.  The inner
5391      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
5392      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
5393      logical depending on the value of UNSIGNEDP.
5394
5395      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5396      converted into an AND of a shift.
5397
5398      We must check for the case where the left shift would have a negative
5399      count.  This can happen in a case like (x >> 31) & 255 on machines
5400      that can't shift by a constant.  On those machines, we would first
5401      combine the shift with the AND to produce a variable-position 
5402      extraction.  Then the constant of 31 would be substituted in to produce
5403      a such a position.  */
5404
5405   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5406   if (modewidth >= pos - len)
5407     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5408                                 GET_MODE (x),
5409                                 simplify_shift_const (NULL_RTX, ASHIFT,
5410                                                       GET_MODE (x),
5411                                                       XEXP (x, 0),
5412                                                       modewidth - pos - len),
5413                                 modewidth - len);
5414
5415   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5416     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5417                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
5418                                                         GET_MODE (x),
5419                                                         XEXP (x, 0), pos),
5420                                   ((HOST_WIDE_INT) 1 << len) - 1);
5421   else
5422     /* Any other cases we can't handle.  */
5423     return x;
5424     
5425
5426   /* If we couldn't do this for some reason, return the original
5427      expression.  */
5428   if (GET_CODE (tem) == CLOBBER)
5429     return x;
5430
5431   return tem;
5432 }
5433 \f
5434 /* X is a SET which contains an assignment of one object into
5435    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5436    or certain SUBREGS). If possible, convert it into a series of
5437    logical operations.
5438
5439    We half-heartedly support variable positions, but do not at all
5440    support variable lengths.  */
5441
5442 static rtx
5443 expand_field_assignment (x)
5444      rtx x;
5445 {
5446   rtx inner;
5447   rtx pos;                      /* Always counts from low bit.  */
5448   int len;
5449   rtx mask;
5450   enum machine_mode compute_mode;
5451
5452   /* Loop until we find something we can't simplify.  */
5453   while (1)
5454     {
5455       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5456           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5457         {
5458           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5459           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5460           pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
5461         }
5462       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5463                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5464         {
5465           inner = XEXP (SET_DEST (x), 0);
5466           len = INTVAL (XEXP (SET_DEST (x), 1));
5467           pos = XEXP (SET_DEST (x), 2);
5468
5469           /* If the position is constant and spans the width of INNER,
5470              surround INNER  with a USE to indicate this.  */
5471           if (GET_CODE (pos) == CONST_INT
5472               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5473             inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5474
5475           if (BITS_BIG_ENDIAN)
5476             {
5477               if (GET_CODE (pos) == CONST_INT)
5478                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5479                                - INTVAL (pos));
5480               else if (GET_CODE (pos) == MINUS
5481                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
5482                        && (INTVAL (XEXP (pos, 1))
5483                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5484                 /* If position is ADJUST - X, new position is X.  */
5485                 pos = XEXP (pos, 0);
5486               else
5487                 pos = gen_binary (MINUS, GET_MODE (pos),
5488                                   GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5489                                            - len),
5490                                   pos);
5491             }
5492         }
5493
5494       /* A SUBREG between two modes that occupy the same numbers of words
5495          can be done by moving the SUBREG to the source.  */
5496       else if (GET_CODE (SET_DEST (x)) == SUBREG
5497                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5498                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5499                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5500                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5501         {
5502           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5503                            gen_lowpart_for_combine
5504                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
5505                             SET_SRC (x)));
5506           continue;
5507         }
5508       else
5509         break;
5510
5511       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5512         inner = SUBREG_REG (inner);
5513
5514       compute_mode = GET_MODE (inner);
5515
5516       /* Don't attempt bitwise arithmetic on non-integral modes.  */
5517       if (! INTEGRAL_MODE_P (compute_mode))
5518         {
5519           enum machine_mode imode;
5520
5521           /* Something is probably seriously wrong if this matches.  */
5522           if (! FLOAT_MODE_P (compute_mode))
5523             break;
5524
5525           /* Try to find an integral mode to pun with.  */
5526           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5527           if (imode == BLKmode)
5528             break;
5529
5530           compute_mode = imode;
5531           inner = gen_lowpart_for_combine (imode, inner);
5532         }
5533
5534       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
5535       if (len < HOST_BITS_PER_WIDE_INT)
5536         mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5537       else
5538         break;
5539
5540       /* Now compute the equivalent expression.  Make a copy of INNER
5541          for the SET_DEST in case it is a MEM into which we will substitute;
5542          we don't want shared RTL in that case.  */
5543       x = gen_rtx_SET
5544         (VOIDmode, copy_rtx (inner),
5545          gen_binary (IOR, compute_mode,
5546                      gen_binary (AND, compute_mode,
5547                                  gen_unary (NOT, compute_mode,
5548                                             compute_mode,
5549                                             gen_binary (ASHIFT,
5550                                                         compute_mode,
5551                                                         mask, pos)),
5552                                  inner),
5553                      gen_binary (ASHIFT, compute_mode,
5554                                  gen_binary (AND, compute_mode,
5555                                              gen_lowpart_for_combine
5556                                              (compute_mode, SET_SRC (x)),
5557                                              mask),
5558                                  pos)));
5559     }
5560
5561   return x;
5562 }
5563 \f
5564 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
5565    it is an RTX that represents a variable starting position; otherwise,
5566    POS is the (constant) starting bit position (counted from the LSB).
5567
5568    INNER may be a USE.  This will occur when we started with a bitfield
5569    that went outside the boundary of the object in memory, which is
5570    allowed on most machines.  To isolate this case, we produce a USE
5571    whose mode is wide enough and surround the MEM with it.  The only
5572    code that understands the USE is this routine.  If it is not removed,
5573    it will cause the resulting insn not to match.
5574
5575    UNSIGNEDP is non-zero for an unsigned reference and zero for a 
5576    signed reference.
5577
5578    IN_DEST is non-zero if this is a reference in the destination of a
5579    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If non-zero,
5580    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5581    be used.
5582
5583    IN_COMPARE is non-zero if we are in a COMPARE.  This means that a
5584    ZERO_EXTRACT should be built even for bits starting at bit 0.
5585
5586    MODE is the desired mode of the result (if IN_DEST == 0).
5587
5588    The result is an RTX for the extraction or NULL_RTX if the target
5589    can't handle it.  */
5590
5591 static rtx
5592 make_extraction (mode, inner, pos, pos_rtx, len,
5593                  unsignedp, in_dest, in_compare)
5594      enum machine_mode mode;
5595      rtx inner;
5596      int pos;
5597      rtx pos_rtx;
5598      int len;
5599      int unsignedp;
5600      int in_dest, in_compare;
5601 {
5602   /* This mode describes the size of the storage area
5603      to fetch the overall value from.  Within that, we
5604      ignore the POS lowest bits, etc.  */
5605   enum machine_mode is_mode = GET_MODE (inner);
5606   enum machine_mode inner_mode;
5607   enum machine_mode wanted_inner_mode = byte_mode;
5608   enum machine_mode wanted_inner_reg_mode = word_mode;
5609   enum machine_mode pos_mode = word_mode;
5610   enum machine_mode extraction_mode = word_mode;
5611   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5612   int spans_byte = 0;
5613   rtx new = 0;
5614   rtx orig_pos_rtx = pos_rtx;
5615   int orig_pos;
5616
5617   /* Get some information about INNER and get the innermost object.  */
5618   if (GET_CODE (inner) == USE)
5619     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
5620     /* We don't need to adjust the position because we set up the USE
5621        to pretend that it was a full-word object.  */
5622     spans_byte = 1, inner = XEXP (inner, 0);
5623   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5624     {
5625       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5626          consider just the QI as the memory to extract from.
5627          The subreg adds or removes high bits; its mode is
5628          irrelevant to the meaning of this extraction,
5629          since POS and LEN count from the lsb.  */
5630       if (GET_CODE (SUBREG_REG (inner)) == MEM)
5631         is_mode = GET_MODE (SUBREG_REG (inner));
5632       inner = SUBREG_REG (inner);
5633     }
5634
5635   inner_mode = GET_MODE (inner);
5636
5637   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5638     pos = INTVAL (pos_rtx), pos_rtx = 0;
5639
5640   /* See if this can be done without an extraction.  We never can if the
5641      width of the field is not the same as that of some integer mode. For
5642      registers, we can only avoid the extraction if the position is at the
5643      low-order bit and this is either not in the destination or we have the
5644      appropriate STRICT_LOW_PART operation available.
5645
5646      For MEM, we can avoid an extract if the field starts on an appropriate
5647      boundary and we can change the mode of the memory reference.  However,
5648      we cannot directly access the MEM if we have a USE and the underlying
5649      MEM is not TMODE.  This combination means that MEM was being used in a
5650      context where bits outside its mode were being referenced; that is only
5651      valid in bit-field insns.  */
5652
5653   if (tmode != BLKmode
5654       && ! (spans_byte && inner_mode != tmode)
5655       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5656            && GET_CODE (inner) != MEM
5657            && (! in_dest
5658                || (GET_CODE (inner) == REG
5659                    && (movstrict_optab->handlers[(int) tmode].insn_code
5660                        != CODE_FOR_nothing))))
5661           || (GET_CODE (inner) == MEM && pos_rtx == 0
5662               && (pos
5663                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5664                      : BITS_PER_UNIT)) == 0
5665               /* We can't do this if we are widening INNER_MODE (it
5666                  may not be aligned, for one thing).  */
5667               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5668               && (inner_mode == tmode
5669                   || (! mode_dependent_address_p (XEXP (inner, 0))
5670                       && ! MEM_VOLATILE_P (inner))))))
5671     {
5672       /* If INNER is a MEM, make a new MEM that encompasses just the desired
5673          field.  If the original and current mode are the same, we need not
5674          adjust the offset.  Otherwise, we do if bytes big endian.  
5675
5676          If INNER is not a MEM, get a piece consisting of just the field
5677          of interest (in this case POS % BITS_PER_WORD must be 0).  */
5678
5679       if (GET_CODE (inner) == MEM)
5680         {
5681           int offset;
5682           /* POS counts from lsb, but make OFFSET count in memory order.  */
5683           if (BYTES_BIG_ENDIAN)
5684             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5685           else
5686             offset = pos / BITS_PER_UNIT;
5687
5688           new = gen_rtx_MEM (tmode, plus_constant (XEXP (inner, 0), offset));
5689           RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
5690           MEM_COPY_ATTRIBUTES (new, inner);
5691         }
5692       else if (GET_CODE (inner) == REG)
5693         {
5694           /* We can't call gen_lowpart_for_combine here since we always want
5695              a SUBREG and it would sometimes return a new hard register.  */
5696           if (tmode != inner_mode)
5697             new = gen_rtx_SUBREG (tmode, inner,
5698                                   (WORDS_BIG_ENDIAN
5699                                    && (GET_MODE_SIZE (inner_mode)
5700                                        > UNITS_PER_WORD)
5701                                    ? (((GET_MODE_SIZE (inner_mode)
5702                                         - GET_MODE_SIZE (tmode))
5703                                        / UNITS_PER_WORD)
5704                                       - pos / BITS_PER_WORD)
5705                                    : pos / BITS_PER_WORD));
5706           else
5707             new = inner;
5708         }
5709       else
5710         new = force_to_mode (inner, tmode,
5711                              len >= HOST_BITS_PER_WIDE_INT
5712                              ? GET_MODE_MASK (tmode)
5713                              : ((HOST_WIDE_INT) 1 << len) - 1,
5714                              NULL_RTX, 0);
5715
5716       /* If this extraction is going into the destination of a SET, 
5717          make a STRICT_LOW_PART unless we made a MEM.  */
5718
5719       if (in_dest)
5720         return (GET_CODE (new) == MEM ? new
5721                 : (GET_CODE (new) != SUBREG
5722                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
5723                    : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
5724
5725       /* Otherwise, sign- or zero-extend unless we already are in the
5726          proper mode.  */
5727
5728       return (mode == tmode ? new
5729               : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5730                                  mode, new));
5731     }
5732
5733   /* Unless this is a COMPARE or we have a funny memory reference,
5734      don't do anything with zero-extending field extracts starting at
5735      the low-order bit since they are simple AND operations.  */
5736   if (pos_rtx == 0 && pos == 0 && ! in_dest
5737       && ! in_compare && ! spans_byte && unsignedp)
5738     return 0;
5739
5740   /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
5741      we would be spanning bytes or if the position is not a constant and the
5742      length is not 1.  In all other cases, we would only be going outside
5743      our object in cases when an original shift would have been
5744      undefined.  */
5745   if (! spans_byte && GET_CODE (inner) == MEM
5746       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5747           || (pos_rtx != 0 && len != 1)))
5748     return 0;
5749
5750   /* Get the mode to use should INNER not be a MEM, the mode for the position,
5751      and the mode for the result.  */
5752 #ifdef HAVE_insv
5753   if (in_dest)
5754     {
5755       wanted_inner_reg_mode
5756         = insn_data[(int) CODE_FOR_insv].operand[0].mode;
5757       if (wanted_inner_reg_mode == VOIDmode)
5758         wanted_inner_reg_mode = word_mode;
5759
5760       pos_mode = insn_data[(int) CODE_FOR_insv].operand[2].mode;
5761       if (pos_mode == VOIDmode)
5762         pos_mode = word_mode;
5763
5764       extraction_mode = insn_data[(int) CODE_FOR_insv].operand[3].mode;
5765       if (extraction_mode == VOIDmode)
5766         extraction_mode = word_mode;
5767     }
5768 #endif
5769
5770 #ifdef HAVE_extzv
5771   if (! in_dest && unsignedp)
5772     {
5773       wanted_inner_reg_mode
5774         = insn_data[(int) CODE_FOR_extzv].operand[1].mode;
5775       if (wanted_inner_reg_mode == VOIDmode)
5776         wanted_inner_reg_mode = word_mode;
5777
5778       pos_mode = insn_data[(int) CODE_FOR_extzv].operand[3].mode;
5779       if (pos_mode == VOIDmode)
5780         pos_mode = word_mode;
5781
5782       extraction_mode = insn_data[(int) CODE_FOR_extzv].operand[0].mode;
5783       if (extraction_mode == VOIDmode)
5784         extraction_mode = word_mode;
5785     }
5786 #endif
5787
5788 #ifdef HAVE_extv
5789   if (! in_dest && ! unsignedp)
5790     {
5791       wanted_inner_reg_mode
5792         = insn_data[(int) CODE_FOR_extv].operand[1].mode;
5793       if (wanted_inner_reg_mode == VOIDmode)
5794         wanted_inner_reg_mode = word_mode;
5795
5796       pos_mode = insn_data[(int) CODE_FOR_extv].operand[3].mode;
5797       if (pos_mode == VOIDmode)
5798         pos_mode = word_mode;
5799
5800       extraction_mode = insn_data[(int) CODE_FOR_extv].operand[0].mode;
5801       if (extraction_mode == VOIDmode)
5802         extraction_mode = word_mode;
5803     }
5804 #endif
5805
5806   /* Never narrow an object, since that might not be safe.  */
5807
5808   if (mode != VOIDmode
5809       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5810     extraction_mode = mode;
5811
5812   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5813       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5814     pos_mode = GET_MODE (pos_rtx);
5815
5816   /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5817      if we have to change the mode of memory and cannot, the desired mode is
5818      EXTRACTION_MODE.  */
5819   if (GET_CODE (inner) != MEM)
5820     wanted_inner_mode = wanted_inner_reg_mode;
5821   else if (inner_mode != wanted_inner_mode
5822            && (mode_dependent_address_p (XEXP (inner, 0))
5823                || MEM_VOLATILE_P (inner)))
5824     wanted_inner_mode = extraction_mode;
5825
5826   orig_pos = pos;
5827
5828   if (BITS_BIG_ENDIAN)
5829     {
5830       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5831          BITS_BIG_ENDIAN style.  If position is constant, compute new
5832          position.  Otherwise, build subtraction.
5833          Note that POS is relative to the mode of the original argument.
5834          If it's a MEM we need to recompute POS relative to that.
5835          However, if we're extracting from (or inserting into) a register,
5836          we want to recompute POS relative to wanted_inner_mode.  */
5837       int width = (GET_CODE (inner) == MEM
5838                    ? GET_MODE_BITSIZE (is_mode)
5839                    : GET_MODE_BITSIZE (wanted_inner_mode));
5840
5841       if (pos_rtx == 0)
5842         pos = width - len - pos;
5843       else
5844         pos_rtx
5845           = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
5846                              GEN_INT (width - len), pos_rtx);
5847       /* POS may be less than 0 now, but we check for that below.
5848          Note that it can only be less than 0 if GET_CODE (inner) != MEM.  */
5849     }
5850
5851   /* If INNER has a wider mode, make it smaller.  If this is a constant
5852      extract, try to adjust the byte to point to the byte containing
5853      the value.  */
5854   if (wanted_inner_mode != VOIDmode
5855       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
5856       && ((GET_CODE (inner) == MEM
5857            && (inner_mode == wanted_inner_mode
5858                || (! mode_dependent_address_p (XEXP (inner, 0))
5859                    && ! MEM_VOLATILE_P (inner))))))
5860     {
5861       int offset = 0;
5862
5863       /* The computations below will be correct if the machine is big
5864          endian in both bits and bytes or little endian in bits and bytes.
5865          If it is mixed, we must adjust.  */
5866              
5867       /* If bytes are big endian and we had a paradoxical SUBREG, we must
5868          adjust OFFSET to compensate.  */
5869       if (BYTES_BIG_ENDIAN
5870           && ! spans_byte
5871           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5872         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
5873
5874       /* If this is a constant position, we can move to the desired byte.  */
5875       if (pos_rtx == 0)
5876         {
5877           offset += pos / BITS_PER_UNIT;
5878           pos %= GET_MODE_BITSIZE (wanted_inner_mode);
5879         }
5880
5881       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5882           && ! spans_byte
5883           && is_mode != wanted_inner_mode)
5884         offset = (GET_MODE_SIZE (is_mode)
5885                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
5886
5887       if (offset != 0 || inner_mode != wanted_inner_mode)
5888         {
5889           rtx newmem = gen_rtx_MEM (wanted_inner_mode,
5890                                     plus_constant (XEXP (inner, 0), offset));
5891           RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
5892           MEM_COPY_ATTRIBUTES (newmem, inner);
5893           inner = newmem;
5894         }
5895     }
5896
5897   /* If INNER is not memory, we can always get it into the proper mode.  If we
5898      are changing its mode, POS must be a constant and smaller than the size
5899      of the new mode.  */
5900   else if (GET_CODE (inner) != MEM)
5901     {
5902       if (GET_MODE (inner) != wanted_inner_mode
5903           && (pos_rtx != 0
5904               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
5905         return 0;
5906
5907       inner = force_to_mode (inner, wanted_inner_mode,
5908                              pos_rtx
5909                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
5910                              ? GET_MODE_MASK (wanted_inner_mode)
5911                              : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
5912                              NULL_RTX, 0);
5913     }
5914
5915   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
5916      have to zero extend.  Otherwise, we can just use a SUBREG.  */
5917   if (pos_rtx != 0
5918       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5919     pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
5920   else if (pos_rtx != 0
5921            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5922     pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5923
5924   /* Make POS_RTX unless we already have it and it is correct.  If we don't
5925      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
5926      be a CONST_INT.  */
5927   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5928     pos_rtx = orig_pos_rtx;
5929
5930   else if (pos_rtx == 0)
5931     pos_rtx = GEN_INT (pos);
5932
5933   /* Make the required operation.  See if we can use existing rtx.  */
5934   new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5935                          extraction_mode, inner, GEN_INT (len), pos_rtx);
5936   if (! in_dest)
5937     new = gen_lowpart_for_combine (mode, new);
5938
5939   return new;
5940 }
5941 \f
5942 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
5943    with any other operations in X.  Return X without that shift if so.  */
5944
5945 static rtx
5946 extract_left_shift (x, count)
5947      rtx x;
5948      int count;
5949 {
5950   enum rtx_code code = GET_CODE (x);
5951   enum machine_mode mode = GET_MODE (x);
5952   rtx tem;
5953
5954   switch (code)
5955     {
5956     case ASHIFT:
5957       /* This is the shift itself.  If it is wide enough, we will return
5958          either the value being shifted if the shift count is equal to
5959          COUNT or a shift for the difference.  */
5960       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5961           && INTVAL (XEXP (x, 1)) >= count)
5962         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
5963                                      INTVAL (XEXP (x, 1)) - count);
5964       break;
5965
5966     case NEG:  case NOT:
5967       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5968         return gen_unary (code, mode, mode, tem);
5969
5970       break;
5971
5972     case PLUS:  case IOR:  case XOR:  case AND:
5973       /* If we can safely shift this constant and we find the inner shift,
5974          make a new operation.  */
5975       if (GET_CODE (XEXP (x,1)) == CONST_INT
5976           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
5977           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5978         return gen_binary (code, mode, tem, 
5979                            GEN_INT (INTVAL (XEXP (x, 1)) >> count));
5980
5981       break;
5982       
5983     default:
5984       break;
5985     }
5986
5987   return 0;
5988 }
5989 \f
5990 /* Look at the expression rooted at X.  Look for expressions
5991    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
5992    Form these expressions.
5993
5994    Return the new rtx, usually just X.
5995
5996    Also, for machines like the Vax that don't have logical shift insns,
5997    try to convert logical to arithmetic shift operations in cases where
5998    they are equivalent.  This undoes the canonicalizations to logical
5999    shifts done elsewhere.
6000
6001    We try, as much as possible, to re-use rtl expressions to save memory.
6002
6003    IN_CODE says what kind of expression we are processing.  Normally, it is
6004    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
6005    being kludges), it is MEM.  When processing the arguments of a comparison
6006    or a COMPARE against zero, it is COMPARE.  */
6007
6008 static rtx
6009 make_compound_operation (x, in_code)
6010      rtx x;
6011      enum rtx_code in_code;
6012 {
6013   enum rtx_code code = GET_CODE (x);
6014   enum machine_mode mode = GET_MODE (x);
6015   int mode_width = GET_MODE_BITSIZE (mode);
6016   rtx rhs, lhs;
6017   enum rtx_code next_code;
6018   int i;
6019   rtx new = 0;
6020   rtx tem;
6021   const char *fmt;
6022
6023   /* Select the code to be used in recursive calls.  Once we are inside an
6024      address, we stay there.  If we have a comparison, set to COMPARE,
6025      but once inside, go back to our default of SET.  */
6026
6027   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6028                : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6029                   && XEXP (x, 1) == const0_rtx) ? COMPARE
6030                : in_code == COMPARE ? SET : in_code);
6031
6032   /* Process depending on the code of this operation.  If NEW is set
6033      non-zero, it will be returned.  */
6034
6035   switch (code)
6036     {
6037     case ASHIFT:
6038       /* Convert shifts by constants into multiplications if inside
6039          an address.  */
6040       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6041           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6042           && INTVAL (XEXP (x, 1)) >= 0)
6043         {
6044           new = make_compound_operation (XEXP (x, 0), next_code);
6045           new = gen_rtx_combine (MULT, mode, new,
6046                                  GEN_INT ((HOST_WIDE_INT) 1
6047                                           << INTVAL (XEXP (x, 1))));
6048         }
6049       break;
6050
6051     case AND:
6052       /* If the second operand is not a constant, we can't do anything
6053          with it.  */
6054       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6055         break;
6056
6057       /* If the constant is a power of two minus one and the first operand
6058          is a logical right shift, make an extraction.  */
6059       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6060           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6061         {
6062           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6063           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6064                                  0, in_code == COMPARE);
6065         }
6066
6067       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
6068       else if (GET_CODE (XEXP (x, 0)) == SUBREG
6069                && subreg_lowpart_p (XEXP (x, 0))
6070                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6071                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6072         {
6073           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6074                                          next_code);
6075           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6076                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6077                                  0, in_code == COMPARE);
6078         }
6079       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
6080       else if ((GET_CODE (XEXP (x, 0)) == XOR
6081                 || GET_CODE (XEXP (x, 0)) == IOR)
6082                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6083                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6084                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6085         {
6086           /* Apply the distributive law, and then try to make extractions.  */
6087           new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
6088                                  gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6089                                               XEXP (x, 1)),
6090                                  gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6091                                               XEXP (x, 1)));
6092           new = make_compound_operation (new, in_code);
6093         }
6094
6095       /* If we are have (and (rotate X C) M) and C is larger than the number
6096          of bits in M, this is an extraction.  */
6097
6098       else if (GET_CODE (XEXP (x, 0)) == ROTATE
6099                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6100                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6101                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6102         {
6103           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6104           new = make_extraction (mode, new,
6105                                  (GET_MODE_BITSIZE (mode)
6106                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
6107                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
6108         }
6109
6110       /* On machines without logical shifts, if the operand of the AND is
6111          a logical shift and our mask turns off all the propagated sign
6112          bits, we can replace the logical shift with an arithmetic shift.  */
6113       else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6114                && (lshr_optab->handlers[(int) mode].insn_code
6115                    == CODE_FOR_nothing)
6116                && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6117                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6118                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6119                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6120                && mode_width <= HOST_BITS_PER_WIDE_INT)
6121         {
6122           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6123
6124           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6125           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6126             SUBST (XEXP (x, 0),
6127                    gen_rtx_combine (ASHIFTRT, mode,
6128                                     make_compound_operation (XEXP (XEXP (x, 0), 0),
6129                                                              next_code),
6130                                     XEXP (XEXP (x, 0), 1)));
6131         }
6132
6133       /* If the constant is one less than a power of two, this might be
6134          representable by an extraction even if no shift is present.
6135          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6136          we are in a COMPARE.  */
6137       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6138         new = make_extraction (mode,
6139                                make_compound_operation (XEXP (x, 0),
6140                                                         next_code),
6141                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6142
6143       /* If we are in a comparison and this is an AND with a power of two,
6144          convert this into the appropriate bit extract.  */
6145       else if (in_code == COMPARE
6146                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6147         new = make_extraction (mode,
6148                                make_compound_operation (XEXP (x, 0),
6149                                                         next_code),
6150                                i, NULL_RTX, 1, 1, 0, 1);
6151
6152       break;
6153
6154     case LSHIFTRT:
6155       /* If the sign bit is known to be zero, replace this with an
6156          arithmetic shift.  */
6157       if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
6158           && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6159           && mode_width <= HOST_BITS_PER_WIDE_INT
6160           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6161         {
6162           new = gen_rtx_combine (ASHIFTRT, mode,
6163                                  make_compound_operation (XEXP (x, 0),
6164                                                           next_code),
6165                                  XEXP (x, 1));
6166           break;
6167         }
6168
6169       /* ... fall through ...  */
6170
6171     case ASHIFTRT:
6172       lhs = XEXP (x, 0);
6173       rhs = XEXP (x, 1);
6174
6175       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6176          this is a SIGN_EXTRACT.  */
6177       if (GET_CODE (rhs) == CONST_INT
6178           && GET_CODE (lhs) == ASHIFT
6179           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6180           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6181         {
6182           new = make_compound_operation (XEXP (lhs, 0), next_code);
6183           new = make_extraction (mode, new,
6184                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6185                                  NULL_RTX, mode_width - INTVAL (rhs),
6186                                  code == LSHIFTRT, 0, in_code == COMPARE);
6187         }
6188
6189       /* See if we have operations between an ASHIFTRT and an ASHIFT.
6190          If so, try to merge the shifts into a SIGN_EXTEND.  We could
6191          also do this for some cases of SIGN_EXTRACT, but it doesn't
6192          seem worth the effort; the case checked for occurs on Alpha.  */
6193       
6194       if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6195           && ! (GET_CODE (lhs) == SUBREG
6196                 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6197           && GET_CODE (rhs) == CONST_INT
6198           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6199           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6200         new = make_extraction (mode, make_compound_operation (new, next_code),
6201                                0, NULL_RTX, mode_width - INTVAL (rhs),
6202                                code == LSHIFTRT, 0, in_code == COMPARE);
6203         
6204       break;
6205
6206     case SUBREG:
6207       /* Call ourselves recursively on the inner expression.  If we are
6208          narrowing the object and it has a different RTL code from
6209          what it originally did, do this SUBREG as a force_to_mode.  */
6210
6211       tem = make_compound_operation (SUBREG_REG (x), in_code);
6212       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6213           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6214           && subreg_lowpart_p (x))
6215         {
6216           rtx newer = force_to_mode (tem, mode,
6217                                      GET_MODE_MASK (mode), NULL_RTX, 0);
6218
6219           /* If we have something other than a SUBREG, we might have
6220              done an expansion, so rerun outselves.  */
6221           if (GET_CODE (newer) != SUBREG)
6222             newer = make_compound_operation (newer, in_code);
6223
6224           return newer;
6225         }
6226
6227       /* If this is a paradoxical subreg, and the new code is a sign or
6228          zero extension, omit the subreg and widen the extension.  If it
6229          is a regular subreg, we can still get rid of the subreg by not
6230          widening so much, or in fact removing the extension entirely.  */
6231       if ((GET_CODE (tem) == SIGN_EXTEND
6232            || GET_CODE (tem) == ZERO_EXTEND)
6233           && subreg_lowpart_p (x))
6234         {
6235           if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6236               || (GET_MODE_SIZE (mode) >
6237                   GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6238             tem = gen_rtx_combine (GET_CODE (tem), mode, XEXP (tem, 0));
6239           else
6240             tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6241           return tem;
6242         }
6243       break;
6244       
6245     default:
6246       break;
6247     }
6248
6249   if (new)
6250     {
6251       x = gen_lowpart_for_combine (mode, new);
6252       code = GET_CODE (x);
6253     }
6254
6255   /* Now recursively process each operand of this operation.  */
6256   fmt = GET_RTX_FORMAT (code);
6257   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6258     if (fmt[i] == 'e')
6259       {
6260         new = make_compound_operation (XEXP (x, i), next_code);
6261         SUBST (XEXP (x, i), new);
6262       }
6263
6264   return x;
6265 }
6266 \f
6267 /* Given M see if it is a value that would select a field of bits
6268     within an item, but not the entire word.  Return -1 if not.
6269     Otherwise, return the starting position of the field, where 0 is the
6270     low-order bit.
6271
6272    *PLEN is set to the length of the field.  */
6273
6274 static int
6275 get_pos_from_mask (m, plen)
6276      unsigned HOST_WIDE_INT m;
6277      int *plen;
6278 {
6279   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
6280   int pos = exact_log2 (m & - m);
6281
6282   if (pos < 0)
6283     return -1;
6284
6285   /* Now shift off the low-order zero bits and see if we have a power of
6286      two minus 1.  */
6287   *plen = exact_log2 ((m >> pos) + 1);
6288
6289   if (*plen <= 0)
6290     return -1;
6291
6292   return pos;
6293 }
6294 \f
6295 /* See if X can be simplified knowing that we will only refer to it in
6296    MODE and will only refer to those bits that are nonzero in MASK.
6297    If other bits are being computed or if masking operations are done
6298    that select a superset of the bits in MASK, they can sometimes be
6299    ignored.
6300
6301    Return a possibly simplified expression, but always convert X to
6302    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
6303
6304    Also, if REG is non-zero and X is a register equal in value to REG, 
6305    replace X with REG.
6306
6307    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6308    are all off in X.  This is used when X will be complemented, by either
6309    NOT, NEG, or XOR.  */
6310
6311 static rtx
6312 force_to_mode (x, mode, mask, reg, just_select)
6313      rtx x;
6314      enum machine_mode mode;
6315      unsigned HOST_WIDE_INT mask;
6316      rtx reg;
6317      int just_select;
6318 {
6319   enum rtx_code code = GET_CODE (x);
6320   int next_select = just_select || code == XOR || code == NOT || code == NEG;
6321   enum machine_mode op_mode;
6322   unsigned HOST_WIDE_INT fuller_mask, nonzero;
6323   rtx op0, op1, temp;
6324
6325   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
6326      code below will do the wrong thing since the mode of such an
6327      expression is VOIDmode. 
6328
6329      Also do nothing if X is a CLOBBER; this can happen if X was
6330      the return value from a call to gen_lowpart_for_combine.  */
6331   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6332     return x;
6333
6334   /* We want to perform the operation is its present mode unless we know
6335      that the operation is valid in MODE, in which case we do the operation
6336      in MODE.  */
6337   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6338               && code_to_optab[(int) code] != 0
6339               && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
6340                   != CODE_FOR_nothing))
6341              ? mode : GET_MODE (x));
6342
6343   /* It is not valid to do a right-shift in a narrower mode
6344      than the one it came in with.  */
6345   if ((code == LSHIFTRT || code == ASHIFTRT)
6346       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6347     op_mode = GET_MODE (x);
6348
6349   /* Truncate MASK to fit OP_MODE.  */
6350   if (op_mode)
6351     mask &= GET_MODE_MASK (op_mode);
6352
6353   /* When we have an arithmetic operation, or a shift whose count we
6354      do not know, we need to assume that all bit the up to the highest-order
6355      bit in MASK will be needed.  This is how we form such a mask.  */
6356   if (op_mode)
6357     fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6358                    ? GET_MODE_MASK (op_mode)
6359                    : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
6360   else
6361     fuller_mask = ~ (HOST_WIDE_INT) 0;
6362
6363   /* Determine what bits of X are guaranteed to be (non)zero.  */
6364   nonzero = nonzero_bits (x, mode);
6365
6366   /* If none of the bits in X are needed, return a zero.  */
6367   if (! just_select && (nonzero & mask) == 0)
6368     return const0_rtx;
6369
6370   /* If X is a CONST_INT, return a new one.  Do this here since the
6371      test below will fail.  */
6372   if (GET_CODE (x) == CONST_INT)
6373     {
6374       HOST_WIDE_INT cval = INTVAL (x) & mask;
6375       int width = GET_MODE_BITSIZE (mode);
6376
6377       /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6378          number, sign extend it.  */
6379       if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6380           && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6381         cval |= (HOST_WIDE_INT) -1 << width;
6382         
6383       return GEN_INT (cval);
6384     }
6385
6386   /* If X is narrower than MODE and we want all the bits in X's mode, just
6387      get X in the proper mode.  */
6388   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6389       && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
6390     return gen_lowpart_for_combine (mode, x);
6391
6392   /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6393      MASK are already known to be zero in X, we need not do anything.  */
6394   if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
6395     return x;
6396
6397   switch (code)
6398     {
6399     case CLOBBER:
6400       /* If X is a (clobber (const_int)), return it since we know we are
6401          generating something that won't match.  */
6402       return x;
6403
6404     case USE:
6405       /* X is a (use (mem ..)) that was made from a bit-field extraction that
6406          spanned the boundary of the MEM.  If we are now masking so it is
6407          within that boundary, we don't need the USE any more.  */
6408       if (! BITS_BIG_ENDIAN
6409           && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6410         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6411       break;
6412
6413     case SIGN_EXTEND:
6414     case ZERO_EXTEND:
6415     case ZERO_EXTRACT:
6416     case SIGN_EXTRACT:
6417       x = expand_compound_operation (x);
6418       if (GET_CODE (x) != code)
6419         return force_to_mode (x, mode, mask, reg, next_select);
6420       break;
6421
6422     case REG:
6423       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6424                        || rtx_equal_p (reg, get_last_value (x))))
6425         x = reg;
6426       break;
6427
6428     case SUBREG:
6429       if (subreg_lowpart_p (x)
6430           /* We can ignore the effect of this SUBREG if it narrows the mode or
6431              if the constant masks to zero all the bits the mode doesn't
6432              have.  */
6433           && ((GET_MODE_SIZE (GET_MODE (x))
6434                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6435               || (0 == (mask
6436                         & GET_MODE_MASK (GET_MODE (x))
6437                         & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6438         return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6439       break;
6440
6441     case AND:
6442       /* If this is an AND with a constant, convert it into an AND
6443          whose constant is the AND of that constant with MASK.  If it
6444          remains an AND of MASK, delete it since it is redundant.  */
6445
6446       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6447         {
6448           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6449                                       mask & INTVAL (XEXP (x, 1)));
6450
6451           /* If X is still an AND, see if it is an AND with a mask that
6452              is just some low-order bits.  If so, and it is MASK, we don't
6453              need it.  */
6454
6455           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6456               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == mask)
6457             x = XEXP (x, 0);
6458
6459           /* If it remains an AND, try making another AND with the bits
6460              in the mode mask that aren't in MASK turned on.  If the
6461              constant in the AND is wide enough, this might make a
6462              cheaper constant.  */
6463
6464           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6465               && GET_MODE_MASK (GET_MODE (x)) != mask
6466               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6467             {
6468               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6469                                     | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
6470               int width = GET_MODE_BITSIZE (GET_MODE (x));
6471               rtx y;
6472
6473               /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6474                  number, sign extend it.  */
6475               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6476                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6477                 cval |= (HOST_WIDE_INT) -1 << width;
6478
6479               y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6480               if (rtx_cost (y, SET) < rtx_cost (x, SET))
6481                 x = y;
6482             }
6483
6484           break;
6485         }
6486
6487       goto binop;
6488
6489     case PLUS:
6490       /* In (and (plus FOO C1) M), if M is a mask that just turns off
6491          low-order bits (as in an alignment operation) and FOO is already
6492          aligned to that boundary, mask C1 to that boundary as well.
6493          This may eliminate that PLUS and, later, the AND.  */
6494
6495       {
6496         int width = GET_MODE_BITSIZE (mode);
6497         unsigned HOST_WIDE_INT smask = mask;
6498
6499         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6500            number, sign extend it.  */
6501
6502         if (width < HOST_BITS_PER_WIDE_INT
6503             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6504           smask |= (HOST_WIDE_INT) -1 << width;
6505
6506         if (GET_CODE (XEXP (x, 1)) == CONST_INT
6507             && exact_log2 (- smask) >= 0)
6508           {
6509 #ifdef STACK_BIAS
6510             if (STACK_BIAS
6511                 && (XEXP (x, 0) == stack_pointer_rtx
6512                     || XEXP (x, 0) == frame_pointer_rtx))
6513               {
6514                 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
6515                 unsigned HOST_WIDE_INT sp_mask = GET_MODE_MASK (mode);
6516           
6517                 sp_mask &= ~ (sp_alignment - 1);
6518                 if ((sp_mask & ~ smask) == 0
6519                     && ((INTVAL (XEXP (x, 1)) - STACK_BIAS) & ~ smask) != 0)
6520                   return force_to_mode (plus_constant (XEXP (x, 0),
6521                                                        ((INTVAL (XEXP (x, 1)) -
6522                                                          STACK_BIAS) & smask)
6523                                                        + STACK_BIAS),
6524                                         mode, smask, reg, next_select);
6525               }
6526 #endif
6527             if ((nonzero_bits (XEXP (x, 0), mode) & ~ smask) == 0
6528                 && (INTVAL (XEXP (x, 1)) & ~ smask) != 0)
6529               return force_to_mode (plus_constant (XEXP (x, 0),
6530                                                    (INTVAL (XEXP (x, 1))
6531                                                     & smask)),
6532                                     mode, smask, reg, next_select);
6533           }
6534       }
6535
6536       /* ... fall through ...  */
6537
6538     case MINUS:
6539     case MULT:
6540       /* For PLUS, MINUS and MULT, we need any bits less significant than the
6541          most significant bit in MASK since carries from those bits will
6542          affect the bits we are interested in.  */
6543       mask = fuller_mask;
6544       goto binop;
6545
6546     case IOR:
6547     case XOR:
6548       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6549          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6550          operation which may be a bitfield extraction.  Ensure that the
6551          constant we form is not wider than the mode of X.  */
6552
6553       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6554           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6555           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6556           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6557           && GET_CODE (XEXP (x, 1)) == CONST_INT
6558           && ((INTVAL (XEXP (XEXP (x, 0), 1))
6559                + floor_log2 (INTVAL (XEXP (x, 1))))
6560               < GET_MODE_BITSIZE (GET_MODE (x)))
6561           && (INTVAL (XEXP (x, 1))
6562               & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6563         {
6564           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6565                               << INTVAL (XEXP (XEXP (x, 0), 1)));
6566           temp = gen_binary (GET_CODE (x), GET_MODE (x),
6567                              XEXP (XEXP (x, 0), 0), temp);
6568           x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6569                           XEXP (XEXP (x, 0), 1));
6570           return force_to_mode (x, mode, mask, reg, next_select);
6571         }
6572
6573     binop:
6574       /* For most binary operations, just propagate into the operation and
6575          change the mode if we have an operation of that mode.   */
6576
6577       op0 = gen_lowpart_for_combine (op_mode,
6578                                      force_to_mode (XEXP (x, 0), mode, mask,
6579                                                     reg, next_select));
6580       op1 = gen_lowpart_for_combine (op_mode,
6581                                      force_to_mode (XEXP (x, 1), mode, mask,
6582                                                     reg, next_select));
6583
6584       /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6585          MASK since OP1 might have been sign-extended but we never want
6586          to turn on extra bits, since combine might have previously relied
6587          on them being off.  */
6588       if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6589           && (INTVAL (op1) & mask) != 0)
6590         op1 = GEN_INT (INTVAL (op1) & mask);
6591          
6592       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6593         x = gen_binary (code, op_mode, op0, op1);
6594       break;
6595
6596     case ASHIFT:
6597       /* For left shifts, do the same, but just for the first operand.
6598          However, we cannot do anything with shifts where we cannot
6599          guarantee that the counts are smaller than the size of the mode
6600          because such a count will have a different meaning in a
6601          wider mode.  */
6602
6603       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6604              && INTVAL (XEXP (x, 1)) >= 0
6605              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6606           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6607                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6608                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6609         break;
6610         
6611       /* If the shift count is a constant and we can do arithmetic in
6612          the mode of the shift, refine which bits we need.  Otherwise, use the
6613          conservative form of the mask.  */
6614       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6615           && INTVAL (XEXP (x, 1)) >= 0
6616           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6617           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6618         mask >>= INTVAL (XEXP (x, 1));
6619       else
6620         mask = fuller_mask;
6621
6622       op0 = gen_lowpart_for_combine (op_mode,
6623                                      force_to_mode (XEXP (x, 0), op_mode,
6624                                                     mask, reg, next_select));
6625
6626       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6627         x =  gen_binary (code, op_mode, op0, XEXP (x, 1));
6628       break;
6629
6630     case LSHIFTRT:
6631       /* Here we can only do something if the shift count is a constant,
6632          this shift constant is valid for the host, and we can do arithmetic
6633          in OP_MODE.  */
6634
6635       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6636           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6637           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6638         {
6639           rtx inner = XEXP (x, 0);
6640
6641           /* Select the mask of the bits we need for the shift operand.  */
6642           mask <<= INTVAL (XEXP (x, 1));
6643
6644           /* We can only change the mode of the shift if we can do arithmetic
6645              in the mode of the shift and MASK is no wider than the width of
6646              OP_MODE.  */
6647           if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6648               || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
6649             op_mode = GET_MODE (x);
6650
6651           inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6652
6653           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6654             x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
6655         }
6656
6657       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6658          shift and AND produces only copies of the sign bit (C2 is one less
6659          than a power of two), we can do this with just a shift.  */
6660
6661       if (GET_CODE (x) == LSHIFTRT
6662           && GET_CODE (XEXP (x, 1)) == CONST_INT
6663           && ((INTVAL (XEXP (x, 1))
6664                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6665               >= GET_MODE_BITSIZE (GET_MODE (x)))
6666           && exact_log2 (mask + 1) >= 0
6667           && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6668               >= exact_log2 (mask + 1)))
6669         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6670                         GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6671                                  - exact_log2 (mask + 1)));
6672
6673       goto shiftrt;
6674
6675     case ASHIFTRT:
6676       /* If we are just looking for the sign bit, we don't need this shift at
6677          all, even if it has a variable count.  */
6678       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6679           && (mask == ((unsigned HOST_WIDE_INT) 1
6680                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
6681         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6682
6683       /* If this is a shift by a constant, get a mask that contains those bits
6684          that are not copies of the sign bit.  We then have two cases:  If
6685          MASK only includes those bits, this can be a logical shift, which may
6686          allow simplifications.  If MASK is a single-bit field not within
6687          those bits, we are requesting a copy of the sign bit and hence can
6688          shift the sign bit to the appropriate location.  */
6689
6690       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6691           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6692         {
6693           int i = -1;
6694
6695           /* If the considered data is wider then HOST_WIDE_INT, we can't
6696              represent a mask for all its bits in a single scalar.
6697              But we only care about the lower bits, so calculate these.  */
6698
6699           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
6700             {
6701               nonzero = ~ (HOST_WIDE_INT) 0;
6702
6703               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6704                  is the number of bits a full-width mask would have set.
6705                  We need only shift if these are fewer than nonzero can
6706                  hold.  If not, we must keep all bits set in nonzero.  */
6707
6708               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6709                   < HOST_BITS_PER_WIDE_INT)
6710                 nonzero >>= INTVAL (XEXP (x, 1))
6711                             + HOST_BITS_PER_WIDE_INT
6712                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
6713             }
6714           else
6715             {
6716               nonzero = GET_MODE_MASK (GET_MODE (x));
6717               nonzero >>= INTVAL (XEXP (x, 1));
6718             }
6719
6720           if ((mask & ~ nonzero) == 0
6721               || (i = exact_log2 (mask)) >= 0)
6722             {
6723               x = simplify_shift_const
6724                 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6725                  i < 0 ? INTVAL (XEXP (x, 1))
6726                  : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6727
6728               if (GET_CODE (x) != ASHIFTRT)
6729                 return force_to_mode (x, mode, mask, reg, next_select);
6730             }
6731         }
6732
6733       /* If MASK is 1, convert this to a LSHIFTRT.  This can be done
6734          even if the shift count isn't a constant.  */
6735       if (mask == 1)
6736         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6737
6738     shiftrt:
6739
6740       /* If this is a zero- or sign-extension operation that just affects bits
6741          we don't care about, remove it.  Be sure the call above returned
6742          something that is still a shift.  */
6743
6744       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6745           && GET_CODE (XEXP (x, 1)) == CONST_INT
6746           && INTVAL (XEXP (x, 1)) >= 0
6747           && (INTVAL (XEXP (x, 1))
6748               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
6749           && GET_CODE (XEXP (x, 0)) == ASHIFT
6750           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6751           && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
6752         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6753                               reg, next_select);
6754
6755       break;
6756
6757     case ROTATE:
6758     case ROTATERT:
6759       /* If the shift count is constant and we can do computations
6760          in the mode of X, compute where the bits we care about are.
6761          Otherwise, we can't do anything.  Don't change the mode of
6762          the shift or propagate MODE into the shift, though.  */
6763       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6764           && INTVAL (XEXP (x, 1)) >= 0)
6765         {
6766           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6767                                             GET_MODE (x), GEN_INT (mask),
6768                                             XEXP (x, 1));
6769           if (temp && GET_CODE(temp) == CONST_INT)
6770             SUBST (XEXP (x, 0),
6771                    force_to_mode (XEXP (x, 0), GET_MODE (x),
6772                                   INTVAL (temp), reg, next_select));
6773         }
6774       break;
6775         
6776     case NEG:
6777       /* If we just want the low-order bit, the NEG isn't needed since it
6778          won't change the low-order bit.    */
6779       if (mask == 1)
6780         return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6781
6782       /* We need any bits less significant than the most significant bit in
6783          MASK since carries from those bits will affect the bits we are
6784          interested in.  */
6785       mask = fuller_mask;
6786       goto unop;
6787
6788     case NOT:
6789       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6790          same as the XOR case above.  Ensure that the constant we form is not
6791          wider than the mode of X.  */
6792
6793       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6794           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6795           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6796           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6797               < GET_MODE_BITSIZE (GET_MODE (x)))
6798           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6799         {
6800           temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6801           temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6802           x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6803
6804           return force_to_mode (x, mode, mask, reg, next_select);
6805         }
6806
6807       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6808          use the full mask inside the NOT.  */
6809       mask = fuller_mask;
6810
6811     unop:
6812       op0 = gen_lowpart_for_combine (op_mode,
6813                                      force_to_mode (XEXP (x, 0), mode, mask,
6814                                                     reg, next_select));
6815       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6816         x = gen_unary (code, op_mode, op_mode, op0);
6817       break;
6818
6819     case NE:
6820       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
6821          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
6822          which is equal to STORE_FLAG_VALUE.  */
6823       if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
6824           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
6825           && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
6826         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6827
6828       break;
6829
6830     case IF_THEN_ELSE:
6831       /* We have no way of knowing if the IF_THEN_ELSE can itself be
6832          written in a narrower mode.  We play it safe and do not do so.  */
6833
6834       SUBST (XEXP (x, 1),
6835              gen_lowpart_for_combine (GET_MODE (x),
6836                                       force_to_mode (XEXP (x, 1), mode,
6837                                                      mask, reg, next_select)));
6838       SUBST (XEXP (x, 2),
6839              gen_lowpart_for_combine (GET_MODE (x),
6840                                       force_to_mode (XEXP (x, 2), mode,
6841                                                      mask, reg,next_select)));
6842       break;
6843       
6844     default:
6845       break;
6846     }
6847
6848   /* Ensure we return a value of the proper mode.  */
6849   return gen_lowpart_for_combine (mode, x);
6850 }
6851 \f
6852 /* Return nonzero if X is an expression that has one of two values depending on
6853    whether some other value is zero or nonzero.  In that case, we return the
6854    value that is being tested, *PTRUE is set to the value if the rtx being
6855    returned has a nonzero value, and *PFALSE is set to the other alternative.
6856
6857    If we return zero, we set *PTRUE and *PFALSE to X.  */
6858
6859 static rtx
6860 if_then_else_cond (x, ptrue, pfalse)
6861      rtx x;
6862      rtx *ptrue, *pfalse;
6863 {
6864   enum machine_mode mode = GET_MODE (x);
6865   enum rtx_code code = GET_CODE (x);
6866   int size = GET_MODE_BITSIZE (mode);
6867   rtx cond0, cond1, true0, true1, false0, false1;
6868   unsigned HOST_WIDE_INT nz;
6869
6870   /* If this is a unary operation whose operand has one of two values, apply
6871      our opcode to compute those values.  */
6872   if (GET_RTX_CLASS (code) == '1'
6873       && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6874     {
6875       *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6876       *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
6877       return cond0;
6878     }
6879
6880   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
6881      make can't possibly match and would suppress other optimizations.  */
6882   else if (code == COMPARE)
6883     ;
6884
6885   /* If this is a binary operation, see if either side has only one of two
6886      values.  If either one does or if both do and they are conditional on
6887      the same value, compute the new true and false values.  */
6888   else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
6889            || GET_RTX_CLASS (code) == '<')
6890     {
6891       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
6892       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
6893
6894       if ((cond0 != 0 || cond1 != 0)
6895           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
6896         {
6897           /* If if_then_else_cond returned zero, then true/false are the
6898              same rtl.  We must copy one of them to prevent invalid rtl
6899              sharing.  */
6900           if (cond0 == 0)
6901             true0 = copy_rtx (true0);
6902           else if (cond1 == 0)
6903             true1 = copy_rtx (true1);
6904
6905           *ptrue = gen_binary (code, mode, true0, true1);
6906           *pfalse = gen_binary (code, mode, false0, false1);
6907           return cond0 ? cond0 : cond1;
6908         }
6909
6910       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
6911          operands is zero when the other is non-zero, and vice-versa,
6912          and STORE_FLAG_VALUE is 1 or -1.  */
6913
6914       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6915           && (code == PLUS || code == IOR || code == XOR || code == MINUS
6916            || code == UMAX)
6917           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6918         {
6919           rtx op0 = XEXP (XEXP (x, 0), 1);
6920           rtx op1 = XEXP (XEXP (x, 1), 1);
6921
6922           cond0 = XEXP (XEXP (x, 0), 0);
6923           cond1 = XEXP (XEXP (x, 1), 0);
6924
6925           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6926               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6927               && reversible_comparison_p (cond1)
6928               && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6929                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6930                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6931                   || ((swap_condition (GET_CODE (cond0))
6932                        == reverse_condition (GET_CODE (cond1)))
6933                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6934                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6935               && ! side_effects_p (x))
6936             {
6937               *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
6938               *pfalse = gen_binary (MULT, mode, 
6939                                     (code == MINUS 
6940                                      ? gen_unary (NEG, mode, mode, op1) : op1),
6941                                     const_true_rtx);
6942               return cond0;
6943             }
6944         }
6945
6946       /* Similarly for MULT, AND and UMIN, execpt that for these the result
6947          is always zero.  */
6948       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6949           && (code == MULT || code == AND || code == UMIN)
6950           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6951         {
6952           cond0 = XEXP (XEXP (x, 0), 0);
6953           cond1 = XEXP (XEXP (x, 1), 0);
6954
6955           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6956               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6957               && reversible_comparison_p (cond1)
6958               && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6959                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6960                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6961                   || ((swap_condition (GET_CODE (cond0))
6962                        == reverse_condition (GET_CODE (cond1)))
6963                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6964                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6965               && ! side_effects_p (x))
6966             {
6967               *ptrue = *pfalse = const0_rtx;
6968               return cond0;
6969             }
6970         }
6971     }
6972
6973   else if (code == IF_THEN_ELSE)
6974     {
6975       /* If we have IF_THEN_ELSE already, extract the condition and
6976          canonicalize it if it is NE or EQ.  */
6977       cond0 = XEXP (x, 0);
6978       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
6979       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
6980         return XEXP (cond0, 0);
6981       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
6982         {
6983           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
6984           return XEXP (cond0, 0);
6985         }
6986       else
6987         return cond0;
6988     }
6989
6990   /* If X is a normal SUBREG with both inner and outer modes integral,
6991      we can narrow both the true and false values of the inner expression,
6992      if there is a condition.  */
6993   else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
6994            && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
6995            && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
6996            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
6997                                                &true0, &false0)))
6998     {
6999       *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
7000       *pfalse
7001         = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
7002
7003       return cond0;
7004     }
7005
7006   /* If X is a constant, this isn't special and will cause confusions
7007      if we treat it as such.  Likewise if it is equivalent to a constant.  */
7008   else if (CONSTANT_P (x)
7009            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7010     ;
7011
7012   /* If X is known to be either 0 or -1, those are the true and 
7013      false values when testing X.  */
7014   else if (num_sign_bit_copies (x, mode) == size)
7015     {
7016       *ptrue = constm1_rtx, *pfalse = const0_rtx;
7017       return x;
7018     }
7019
7020   /* Likewise for 0 or a single bit.  */
7021   else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7022     {
7023       *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
7024       return x;
7025     }
7026
7027   /* Otherwise fail; show no condition with true and false values the same.  */
7028   *ptrue = *pfalse = x;
7029   return 0;
7030 }
7031 \f
7032 /* Return the value of expression X given the fact that condition COND
7033    is known to be true when applied to REG as its first operand and VAL
7034    as its second.  X is known to not be shared and so can be modified in
7035    place.
7036
7037    We only handle the simplest cases, and specifically those cases that
7038    arise with IF_THEN_ELSE expressions.  */
7039
7040 static rtx
7041 known_cond (x, cond, reg, val)
7042      rtx x;
7043      enum rtx_code cond;
7044      rtx reg, val;
7045 {
7046   enum rtx_code code = GET_CODE (x);
7047   rtx temp;
7048   const char *fmt;
7049   int i, j;
7050
7051   if (side_effects_p (x))
7052     return x;
7053
7054   if (cond == EQ && rtx_equal_p (x, reg))
7055     return val;
7056
7057   /* If X is (abs REG) and we know something about REG's relationship
7058      with zero, we may be able to simplify this.  */
7059
7060   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7061     switch (cond)
7062       {
7063       case GE:  case GT:  case EQ:
7064         return XEXP (x, 0);
7065       case LT:  case LE:
7066         return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
7067                           XEXP (x, 0));
7068       default:
7069         break;
7070       }
7071
7072   /* The only other cases we handle are MIN, MAX, and comparisons if the
7073      operands are the same as REG and VAL.  */
7074
7075   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7076     {
7077       if (rtx_equal_p (XEXP (x, 0), val))
7078         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7079
7080       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7081         {
7082           if (GET_RTX_CLASS (code) == '<')
7083             return (comparison_dominates_p (cond, code) ? const_true_rtx
7084                     : (comparison_dominates_p (cond,
7085                                                reverse_condition (code))
7086                        ? const0_rtx : x));
7087
7088           else if (code == SMAX || code == SMIN
7089                    || code == UMIN || code == UMAX)
7090             {
7091               int unsignedp = (code == UMIN || code == UMAX);
7092
7093               if (code == SMAX || code == UMAX)
7094                 cond = reverse_condition (cond);
7095
7096               switch (cond)
7097                 {
7098                 case GE:   case GT:
7099                   return unsignedp ? x : XEXP (x, 1);
7100                 case LE:   case LT:
7101                   return unsignedp ? x : XEXP (x, 0);
7102                 case GEU:  case GTU:
7103                   return unsignedp ? XEXP (x, 1) : x;
7104                 case LEU:  case LTU:
7105                   return unsignedp ? XEXP (x, 0) : x;
7106                 default:
7107                   break;
7108                 }
7109             }
7110         }
7111     }
7112
7113   fmt = GET_RTX_FORMAT (code);
7114   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7115     {
7116       if (fmt[i] == 'e')
7117         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7118       else if (fmt[i] == 'E')
7119         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7120           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7121                                                 cond, reg, val));
7122     }
7123
7124   return x;
7125 }
7126 \f
7127 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7128    assignment as a field assignment.  */
7129
7130 static int
7131 rtx_equal_for_field_assignment_p (x, y)
7132      rtx x;
7133      rtx y;
7134 {
7135   if (x == y || rtx_equal_p (x, y))
7136     return 1;
7137
7138   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7139     return 0;
7140
7141   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7142      Note that all SUBREGs of MEM are paradoxical; otherwise they
7143      would have been rewritten.  */
7144   if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7145       && GET_CODE (SUBREG_REG (y)) == MEM
7146       && rtx_equal_p (SUBREG_REG (y),
7147                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7148     return 1;
7149
7150   if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7151       && GET_CODE (SUBREG_REG (x)) == MEM
7152       && rtx_equal_p (SUBREG_REG (x),
7153                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7154     return 1;
7155
7156   /* We used to see if get_last_value of X and Y were the same but that's
7157      not correct.  In one direction, we'll cause the assignment to have
7158      the wrong destination and in the case, we'll import a register into this
7159      insn that might have already have been dead.   So fail if none of the
7160      above cases are true.  */
7161   return 0;
7162 }
7163 \f
7164 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7165    Return that assignment if so.
7166
7167    We only handle the most common cases.  */
7168
7169 static rtx
7170 make_field_assignment (x)
7171      rtx x;
7172 {
7173   rtx dest = SET_DEST (x);
7174   rtx src = SET_SRC (x);
7175   rtx assign;
7176   rtx rhs, lhs;
7177   HOST_WIDE_INT c1;
7178   int pos, len;
7179   rtx other;
7180   enum machine_mode mode;
7181
7182   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7183      a clear of a one-bit field.  We will have changed it to
7184      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
7185      for a SUBREG.  */
7186
7187   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7188       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7189       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7190       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7191     {
7192       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7193                                 1, 1, 1, 0);
7194       if (assign != 0)
7195         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7196       return x;
7197     }
7198
7199   else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7200            && subreg_lowpart_p (XEXP (src, 0))
7201            && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0))) 
7202                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7203            && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7204            && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7205            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7206     {
7207       assign = make_extraction (VOIDmode, dest, 0,
7208                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7209                                 1, 1, 1, 0);
7210       if (assign != 0)
7211         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7212       return x;
7213     }
7214
7215   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7216      one-bit field.  */
7217   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7218            && XEXP (XEXP (src, 0), 0) == const1_rtx
7219            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7220     {
7221       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7222                                 1, 1, 1, 0);
7223       if (assign != 0)
7224         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7225       return x;
7226     }
7227
7228   /* The other case we handle is assignments into a constant-position
7229      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
7230      a mask that has all one bits except for a group of zero bits and
7231      OTHER is known to have zeros where C1 has ones, this is such an
7232      assignment.  Compute the position and length from C1.  Shift OTHER
7233      to the appropriate position, force it to the required mode, and
7234      make the extraction.  Check for the AND in both operands.  */
7235
7236   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7237     return x;
7238
7239   rhs = expand_compound_operation (XEXP (src, 0));
7240   lhs = expand_compound_operation (XEXP (src, 1));
7241
7242   if (GET_CODE (rhs) == AND
7243       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7244       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7245     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7246   else if (GET_CODE (lhs) == AND
7247            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7248            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7249     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7250   else
7251     return x;
7252
7253   pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7254   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7255       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7256       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7257     return x;
7258
7259   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7260   if (assign == 0)
7261     return x;
7262
7263   /* The mode to use for the source is the mode of the assignment, or of
7264      what is inside a possible STRICT_LOW_PART.  */
7265   mode = (GET_CODE (assign) == STRICT_LOW_PART 
7266           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7267
7268   /* Shift OTHER right POS places and make it the source, restricting it
7269      to the proper length and mode.  */
7270
7271   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7272                                              GET_MODE (src), other, pos),
7273                        mode,
7274                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7275                        ? GET_MODE_MASK (mode)
7276                        : ((HOST_WIDE_INT) 1 << len) - 1,
7277                        dest, 0);
7278
7279   return gen_rtx_combine (SET, VOIDmode, assign, src);
7280 }
7281 \f
7282 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7283    if so.  */
7284
7285 static rtx
7286 apply_distributive_law (x)
7287      rtx x;
7288 {
7289   enum rtx_code code = GET_CODE (x);
7290   rtx lhs, rhs, other;
7291   rtx tem;
7292   enum rtx_code inner_code;
7293
7294   /* Distributivity is not true for floating point.
7295      It can change the value.  So don't do it.
7296      -- rms and moshier@world.std.com.  */
7297   if (FLOAT_MODE_P (GET_MODE (x)))
7298     return x;
7299
7300   /* The outer operation can only be one of the following:  */
7301   if (code != IOR && code != AND && code != XOR
7302       && code != PLUS && code != MINUS)
7303     return x;
7304
7305   lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7306
7307   /* If either operand is a primitive we can't do anything, so get out
7308      fast.  */
7309   if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7310       || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7311     return x;
7312
7313   lhs = expand_compound_operation (lhs);
7314   rhs = expand_compound_operation (rhs);
7315   inner_code = GET_CODE (lhs);
7316   if (inner_code != GET_CODE (rhs))
7317     return x;
7318
7319   /* See if the inner and outer operations distribute.  */
7320   switch (inner_code)
7321     {
7322     case LSHIFTRT:
7323     case ASHIFTRT:
7324     case AND:
7325     case IOR:
7326       /* These all distribute except over PLUS.  */
7327       if (code == PLUS || code == MINUS)
7328         return x;
7329       break;
7330
7331     case MULT:
7332       if (code != PLUS && code != MINUS)
7333         return x;
7334       break;
7335
7336     case ASHIFT:
7337       /* This is also a multiply, so it distributes over everything.  */
7338       break;
7339
7340     case SUBREG:
7341       /* Non-paradoxical SUBREGs distributes over all operations, provided
7342          the inner modes and word numbers are the same, this is an extraction
7343          of a low-order part, we don't convert an fp operation to int or
7344          vice versa, and we would not be converting a single-word
7345          operation into a multi-word operation.  The latter test is not
7346          required, but it prevents generating unneeded multi-word operations.
7347          Some of the previous tests are redundant given the latter test, but
7348          are retained because they are required for correctness.
7349
7350          We produce the result slightly differently in this case.  */
7351
7352       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7353           || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
7354           || ! subreg_lowpart_p (lhs)
7355           || (GET_MODE_CLASS (GET_MODE (lhs))
7356               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7357           || (GET_MODE_SIZE (GET_MODE (lhs))
7358               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7359           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7360         return x;
7361
7362       tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7363                         SUBREG_REG (lhs), SUBREG_REG (rhs));
7364       return gen_lowpart_for_combine (GET_MODE (x), tem);
7365
7366     default:
7367       return x;
7368     }
7369
7370   /* Set LHS and RHS to the inner operands (A and B in the example
7371      above) and set OTHER to the common operand (C in the example).
7372      These is only one way to do this unless the inner operation is
7373      commutative.  */
7374   if (GET_RTX_CLASS (inner_code) == 'c'
7375       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7376     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7377   else if (GET_RTX_CLASS (inner_code) == 'c'
7378            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7379     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7380   else if (GET_RTX_CLASS (inner_code) == 'c'
7381            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7382     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7383   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7384     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7385   else
7386     return x;
7387
7388   /* Form the new inner operation, seeing if it simplifies first.  */
7389   tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7390
7391   /* There is one exception to the general way of distributing:
7392      (a ^ b) | (a ^ c) -> (~a) & (b ^ c)  */
7393   if (code == XOR && inner_code == IOR)
7394     {
7395       inner_code = AND;
7396       other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
7397     }
7398
7399   /* We may be able to continuing distributing the result, so call
7400      ourselves recursively on the inner operation before forming the
7401      outer operation, which we return.  */
7402   return gen_binary (inner_code, GET_MODE (x),
7403                      apply_distributive_law (tem), other);
7404 }
7405 \f
7406 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7407    in MODE.
7408
7409    Return an equivalent form, if different from X.  Otherwise, return X.  If
7410    X is zero, we are to always construct the equivalent form.  */
7411
7412 static rtx
7413 simplify_and_const_int (x, mode, varop, constop)
7414      rtx x;
7415      enum machine_mode mode;
7416      rtx varop;
7417      unsigned HOST_WIDE_INT constop;
7418 {
7419   unsigned HOST_WIDE_INT nonzero;
7420   int i;
7421
7422   /* Simplify VAROP knowing that we will be only looking at some of the
7423      bits in it.  */
7424   varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7425
7426   /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7427      CONST_INT, we are done.  */
7428   if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7429     return varop;
7430
7431   /* See what bits may be nonzero in VAROP.  Unlike the general case of
7432      a call to nonzero_bits, here we don't care about bits outside
7433      MODE.  */
7434
7435   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7436   nonzero = trunc_int_for_mode (nonzero, mode);
7437
7438   /* Turn off all bits in the constant that are known to already be zero.
7439      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7440      which is tested below.  */
7441
7442   constop &= nonzero;
7443
7444   /* If we don't have any bits left, return zero.  */
7445   if (constop == 0)
7446     return const0_rtx;
7447
7448   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7449      a power of two, we can replace this with a ASHIFT.  */
7450   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7451       && (i = exact_log2 (constop)) >= 0)
7452     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7453                                  
7454   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7455      or XOR, then try to apply the distributive law.  This may eliminate
7456      operations if either branch can be simplified because of the AND.
7457      It may also make some cases more complex, but those cases probably
7458      won't match a pattern either with or without this.  */
7459
7460   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7461     return
7462       gen_lowpart_for_combine
7463         (mode,
7464          apply_distributive_law
7465          (gen_binary (GET_CODE (varop), GET_MODE (varop),
7466                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7467                                               XEXP (varop, 0), constop),
7468                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7469                                               XEXP (varop, 1), constop))));
7470
7471   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
7472      if we already had one (just check for the simplest cases).  */
7473   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7474       && GET_MODE (XEXP (x, 0)) == mode
7475       && SUBREG_REG (XEXP (x, 0)) == varop)
7476     varop = XEXP (x, 0);
7477   else
7478     varop = gen_lowpart_for_combine (mode, varop);
7479
7480   /* If we can't make the SUBREG, try to return what we were given.  */
7481   if (GET_CODE (varop) == CLOBBER)
7482     return x ? x : varop;
7483
7484   /* If we are only masking insignificant bits, return VAROP.  */
7485   if (constop == nonzero)
7486     x = varop;
7487
7488   /* Otherwise, return an AND.  See how much, if any, of X we can use.  */
7489   else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
7490     x = gen_binary (AND, mode, varop, GEN_INT (constop));
7491
7492   else
7493     {
7494       if (GET_CODE (XEXP (x, 1)) != CONST_INT
7495           || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
7496         SUBST (XEXP (x, 1), GEN_INT (constop));
7497
7498       SUBST (XEXP (x, 0), varop);
7499     }
7500
7501   return x;
7502 }
7503 \f
7504 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7505    We don't let nonzero_bits recur into num_sign_bit_copies, because that
7506    is less useful.  We can't allow both, because that results in exponential
7507    run time recursion.  There is a nullstone testcase that triggered
7508    this.  This macro avoids accidental uses of num_sign_bit_copies.  */
7509 #define num_sign_bit_copies()
7510
7511 /* Given an expression, X, compute which bits in X can be non-zero.
7512    We don't care about bits outside of those defined in MODE.
7513
7514    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7515    a shift, AND, or zero_extract, we can do better.  */
7516
7517 static unsigned HOST_WIDE_INT
7518 nonzero_bits (x, mode)
7519      rtx x;
7520      enum machine_mode mode;
7521 {
7522   unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7523   unsigned HOST_WIDE_INT inner_nz;
7524   enum rtx_code code;
7525   int mode_width = GET_MODE_BITSIZE (mode);
7526   rtx tem;
7527
7528   /* For floating-point values, assume all bits are needed.  */
7529   if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7530     return nonzero;
7531
7532   /* If X is wider than MODE, use its mode instead.  */
7533   if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7534     {
7535       mode = GET_MODE (x);
7536       nonzero = GET_MODE_MASK (mode);
7537       mode_width = GET_MODE_BITSIZE (mode);
7538     }
7539
7540   if (mode_width > HOST_BITS_PER_WIDE_INT)
7541     /* Our only callers in this case look for single bit values.  So
7542        just return the mode mask.  Those tests will then be false.  */
7543     return nonzero;
7544
7545 #ifndef WORD_REGISTER_OPERATIONS
7546   /* If MODE is wider than X, but both are a single word for both the host
7547      and target machines, we can compute this from which bits of the 
7548      object might be nonzero in its own mode, taking into account the fact
7549      that on many CISC machines, accessing an object in a wider mode
7550      causes the high-order bits to become undefined.  So they are
7551      not known to be zero.  */
7552
7553   if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
7554       && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
7555       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7556       && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
7557     {
7558       nonzero &= nonzero_bits (x, GET_MODE (x));
7559       nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
7560       return nonzero;
7561     }
7562 #endif
7563
7564   code = GET_CODE (x);
7565   switch (code)
7566     {
7567     case REG:
7568 #ifdef POINTERS_EXTEND_UNSIGNED
7569       /* If pointers extend unsigned and this is a pointer in Pmode, say that
7570          all the bits above ptr_mode are known to be zero.  */
7571       if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
7572           && REGNO_POINTER_FLAG (REGNO (x)))
7573         nonzero &= GET_MODE_MASK (ptr_mode);
7574 #endif
7575
7576 #ifdef STACK_BOUNDARY
7577       /* If this is the stack pointer, we may know something about its
7578          alignment.  If PUSH_ROUNDING is defined, it is possible for the
7579          stack to be momentarily aligned only to that amount, so we pick
7580          the least alignment.  */
7581
7582       /* We can't check for arg_pointer_rtx here, because it is not
7583          guaranteed to have as much alignment as the stack pointer.
7584          In particular, in the Irix6 n64 ABI, the stack has 128 bit
7585          alignment but the argument pointer has only 64 bit alignment.  */
7586
7587       if ((x == frame_pointer_rtx
7588            || x == stack_pointer_rtx
7589            || x == hard_frame_pointer_rtx
7590            || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7591                && REGNO (x) <= LAST_VIRTUAL_REGISTER))
7592 #ifdef STACK_BIAS
7593           && !STACK_BIAS
7594 #endif        
7595               )
7596         {
7597           int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7598
7599 #ifdef PUSH_ROUNDING
7600           if (REGNO (x) == STACK_POINTER_REGNUM)
7601             sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
7602 #endif
7603
7604           /* We must return here, otherwise we may get a worse result from
7605              one of the choices below.  There is nothing useful below as
7606              far as the stack pointer is concerned.  */
7607           return nonzero &= ~ (sp_alignment - 1);
7608         }
7609 #endif
7610
7611       /* If X is a register whose nonzero bits value is current, use it.
7612          Otherwise, if X is a register whose value we can find, use that
7613          value.  Otherwise, use the previously-computed global nonzero bits
7614          for this register.  */
7615
7616       if (reg_last_set_value[REGNO (x)] != 0
7617           && reg_last_set_mode[REGNO (x)] == mode
7618           && (reg_last_set_label[REGNO (x)] == label_tick
7619               || (REGNO (x) >= FIRST_PSEUDO_REGISTER
7620                   && REG_N_SETS (REGNO (x)) == 1
7621                   && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, 
7622                                         REGNO (x))))
7623           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7624         return reg_last_set_nonzero_bits[REGNO (x)];
7625
7626       tem = get_last_value (x);
7627
7628       if (tem)
7629         {
7630 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7631           /* If X is narrower than MODE and TEM is a non-negative
7632              constant that would appear negative in the mode of X,
7633              sign-extend it for use in reg_nonzero_bits because some
7634              machines (maybe most) will actually do the sign-extension
7635              and this is the conservative approach. 
7636
7637              ??? For 2.5, try to tighten up the MD files in this regard
7638              instead of this kludge.  */
7639
7640           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7641               && GET_CODE (tem) == CONST_INT
7642               && INTVAL (tem) > 0
7643               && 0 != (INTVAL (tem)
7644                        & ((HOST_WIDE_INT) 1
7645                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7646             tem = GEN_INT (INTVAL (tem)
7647                            | ((HOST_WIDE_INT) (-1)
7648                               << GET_MODE_BITSIZE (GET_MODE (x))));
7649 #endif
7650           return nonzero_bits (tem, mode);
7651         }
7652       else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7653         return reg_nonzero_bits[REGNO (x)] & nonzero;
7654       else
7655         return nonzero;
7656
7657     case CONST_INT:
7658 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7659       /* If X is negative in MODE, sign-extend the value.  */
7660       if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7661           && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7662         return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
7663 #endif
7664
7665       return INTVAL (x);
7666
7667     case MEM:
7668 #ifdef LOAD_EXTEND_OP
7669       /* In many, if not most, RISC machines, reading a byte from memory
7670          zeros the rest of the register.  Noticing that fact saves a lot
7671          of extra zero-extends.  */
7672       if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7673         nonzero &= GET_MODE_MASK (GET_MODE (x));
7674 #endif
7675       break;
7676
7677     case EQ:  case NE:
7678     case GT:  case GTU:
7679     case LT:  case LTU:
7680     case GE:  case GEU:
7681     case LE:  case LEU:
7682
7683       /* If this produces an integer result, we know which bits are set.
7684          Code here used to clear bits outside the mode of X, but that is
7685          now done above.  */
7686
7687       if (GET_MODE_CLASS (mode) == MODE_INT
7688           && mode_width <= HOST_BITS_PER_WIDE_INT)
7689         nonzero = STORE_FLAG_VALUE;
7690       break;
7691
7692     case NEG:
7693 #if 0
7694       /* Disabled to avoid exponential mutual recursion between nonzero_bits
7695          and num_sign_bit_copies.  */
7696       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7697           == GET_MODE_BITSIZE (GET_MODE (x)))
7698         nonzero = 1;
7699 #endif
7700
7701       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
7702         nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
7703       break;
7704
7705     case ABS:
7706 #if 0
7707       /* Disabled to avoid exponential mutual recursion between nonzero_bits
7708          and num_sign_bit_copies.  */
7709       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7710           == GET_MODE_BITSIZE (GET_MODE (x)))
7711         nonzero = 1;
7712 #endif
7713       break;
7714
7715     case TRUNCATE:
7716       nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
7717       break;
7718
7719     case ZERO_EXTEND:
7720       nonzero &= nonzero_bits (XEXP (x, 0), mode);
7721       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7722         nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7723       break;
7724
7725     case SIGN_EXTEND:
7726       /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7727          Otherwise, show all the bits in the outer mode but not the inner
7728          may be non-zero.  */
7729       inner_nz = nonzero_bits (XEXP (x, 0), mode);
7730       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7731         {
7732           inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7733           if (inner_nz
7734               & (((HOST_WIDE_INT) 1
7735                   << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
7736             inner_nz |= (GET_MODE_MASK (mode)
7737                           & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7738         }
7739
7740       nonzero &= inner_nz;
7741       break;
7742
7743     case AND:
7744       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7745                   & nonzero_bits (XEXP (x, 1), mode));
7746       break;
7747
7748     case XOR:   case IOR:
7749     case UMIN:  case UMAX:  case SMIN:  case SMAX:
7750       nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7751                   | nonzero_bits (XEXP (x, 1), mode));
7752       break;
7753
7754     case PLUS:  case MINUS:
7755     case MULT:
7756     case DIV:   case UDIV:
7757     case MOD:   case UMOD:
7758       /* We can apply the rules of arithmetic to compute the number of
7759          high- and low-order zero bits of these operations.  We start by
7760          computing the width (position of the highest-order non-zero bit)
7761          and the number of low-order zero bits for each value.  */
7762       {
7763         unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7764         unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7765         int width0 = floor_log2 (nz0) + 1;
7766         int width1 = floor_log2 (nz1) + 1;
7767         int low0 = floor_log2 (nz0 & -nz0);
7768         int low1 = floor_log2 (nz1 & -nz1);
7769         HOST_WIDE_INT op0_maybe_minusp
7770           = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7771         HOST_WIDE_INT op1_maybe_minusp
7772           = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7773         int result_width = mode_width;
7774         int result_low = 0;
7775
7776         switch (code)
7777           {
7778           case PLUS:
7779 #ifdef STACK_BIAS
7780             if (STACK_BIAS
7781                 && (XEXP (x, 0) == stack_pointer_rtx
7782                     || XEXP (x, 0) == frame_pointer_rtx)
7783                 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7784               {
7785                 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7786
7787                 nz0 = (GET_MODE_MASK (mode) & ~ (sp_alignment - 1));
7788                 nz1 = INTVAL (XEXP (x, 1)) - STACK_BIAS;
7789                 width0 = floor_log2 (nz0) + 1;
7790                 width1 = floor_log2 (nz1) + 1;
7791                 low0 = floor_log2 (nz0 & -nz0);
7792                 low1 = floor_log2 (nz1 & -nz1);
7793               }
7794 #endif    
7795             result_width = MAX (width0, width1) + 1;
7796             result_low = MIN (low0, low1);
7797             break;
7798           case MINUS:
7799             result_low = MIN (low0, low1);
7800             break;
7801           case MULT:
7802             result_width = width0 + width1;
7803             result_low = low0 + low1;
7804             break;
7805           case DIV:
7806             if (! op0_maybe_minusp && ! op1_maybe_minusp)
7807               result_width = width0;
7808             break;
7809           case UDIV:
7810             result_width = width0;
7811             break;
7812           case MOD:
7813             if (! op0_maybe_minusp && ! op1_maybe_minusp)
7814               result_width = MIN (width0, width1);
7815             result_low = MIN (low0, low1);
7816             break;
7817           case UMOD:
7818             result_width = MIN (width0, width1);
7819             result_low = MIN (low0, low1);
7820             break;
7821           default:
7822             abort ();
7823           }
7824
7825         if (result_width < mode_width)
7826           nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
7827
7828         if (result_low > 0)
7829           nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
7830       }
7831       break;
7832
7833     case ZERO_EXTRACT:
7834       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7835           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7836         nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
7837       break;
7838
7839     case SUBREG:
7840       /* If this is a SUBREG formed for a promoted variable that has
7841          been zero-extended, we know that at least the high-order bits
7842          are zero, though others might be too.  */
7843
7844       if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
7845         nonzero = (GET_MODE_MASK (GET_MODE (x))
7846                    & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
7847
7848       /* If the inner mode is a single word for both the host and target
7849          machines, we can compute this from which bits of the inner
7850          object might be nonzero.  */
7851       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
7852           && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7853               <= HOST_BITS_PER_WIDE_INT))
7854         {
7855           nonzero &= nonzero_bits (SUBREG_REG (x), mode);
7856
7857 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
7858           /* If this is a typical RISC machine, we only have to worry
7859              about the way loads are extended.  */
7860           if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
7861               ? (nonzero
7862                  & (1L << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1)))
7863               : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
7864 #endif
7865             {
7866               /* On many CISC machines, accessing an object in a wider mode
7867                  causes the high-order bits to become undefined.  So they are
7868                  not known to be zero.  */
7869               if (GET_MODE_SIZE (GET_MODE (x))
7870                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7871                 nonzero |= (GET_MODE_MASK (GET_MODE (x))
7872                             & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
7873             }
7874         }
7875       break;
7876
7877     case ASHIFTRT:
7878     case LSHIFTRT:
7879     case ASHIFT:
7880     case ROTATE:
7881       /* The nonzero bits are in two classes: any bits within MODE
7882          that aren't in GET_MODE (x) are always significant.  The rest of the
7883          nonzero bits are those that are significant in the operand of
7884          the shift when shifted the appropriate number of bits.  This
7885          shows that high-order bits are cleared by the right shift and
7886          low-order bits by left shifts.  */
7887       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7888           && INTVAL (XEXP (x, 1)) >= 0
7889           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7890         {
7891           enum machine_mode inner_mode = GET_MODE (x);
7892           int width = GET_MODE_BITSIZE (inner_mode);
7893           int count = INTVAL (XEXP (x, 1));
7894           unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
7895           unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
7896           unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
7897           unsigned HOST_WIDE_INT outer = 0;
7898
7899           if (mode_width > width)
7900             outer = (op_nonzero & nonzero & ~ mode_mask);
7901
7902           if (code == LSHIFTRT)
7903             inner >>= count;
7904           else if (code == ASHIFTRT)
7905             {
7906               inner >>= count;
7907
7908               /* If the sign bit may have been nonzero before the shift, we
7909                  need to mark all the places it could have been copied to
7910                  by the shift as possibly nonzero.  */
7911               if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
7912                 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
7913             }
7914           else if (code == ASHIFT)
7915             inner <<= count;
7916           else
7917             inner = ((inner << (count % width)
7918                       | (inner >> (width - (count % width)))) & mode_mask);
7919
7920           nonzero &= (outer | inner);
7921         }
7922       break;
7923
7924     case FFS:
7925       /* This is at most the number of bits in the mode.  */
7926       nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
7927       break;
7928
7929     case IF_THEN_ELSE:
7930       nonzero &= (nonzero_bits (XEXP (x, 1), mode)
7931                   | nonzero_bits (XEXP (x, 2), mode));
7932       break;
7933       
7934     default:
7935       break;
7936     }
7937
7938   return nonzero;
7939 }
7940
7941 /* See the macro definition above.  */
7942 #undef num_sign_bit_copies
7943 \f
7944 /* Return the number of bits at the high-order end of X that are known to
7945    be equal to the sign bit.  X will be used in mode MODE; if MODE is
7946    VOIDmode, X will be used in its own mode.  The returned value  will always
7947    be between 1 and the number of bits in MODE.  */
7948
7949 static int
7950 num_sign_bit_copies (x, mode)
7951      rtx x;
7952      enum machine_mode mode;
7953 {
7954   enum rtx_code code = GET_CODE (x);
7955   int bitwidth;
7956   int num0, num1, result;
7957   unsigned HOST_WIDE_INT nonzero;
7958   rtx tem;
7959
7960   /* If we weren't given a mode, use the mode of X.  If the mode is still
7961      VOIDmode, we don't know anything.  Likewise if one of the modes is
7962      floating-point.  */
7963
7964   if (mode == VOIDmode)
7965     mode = GET_MODE (x);
7966
7967   if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
7968     return 1;
7969
7970   bitwidth = GET_MODE_BITSIZE (mode);
7971
7972   /* For a smaller object, just ignore the high bits.  */
7973   if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
7974     return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
7975                     - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
7976      
7977   if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
7978     {
7979 #ifndef WORD_REGISTER_OPERATIONS
7980   /* If this machine does not do all register operations on the entire
7981      register and MODE is wider than the mode of X, we can say nothing
7982      at all about the high-order bits.  */
7983       return 1;
7984 #else
7985       /* Likewise on machines that do, if the mode of the object is smaller
7986          than a word and loads of that size don't sign extend, we can say
7987          nothing about the high order bits.  */
7988       if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
7989 #ifdef LOAD_EXTEND_OP
7990           && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
7991 #endif
7992           )
7993         return 1;
7994 #endif
7995     }
7996
7997   switch (code)
7998     {
7999     case REG:
8000
8001 #ifdef POINTERS_EXTEND_UNSIGNED
8002       /* If pointers extend signed and this is a pointer in Pmode, say that
8003          all the bits above ptr_mode are known to be sign bit copies.  */
8004       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8005           && REGNO_POINTER_FLAG (REGNO (x)))
8006         return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8007 #endif
8008
8009       if (reg_last_set_value[REGNO (x)] != 0
8010           && reg_last_set_mode[REGNO (x)] == mode
8011           && (reg_last_set_label[REGNO (x)] == label_tick
8012               || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8013                   && REG_N_SETS (REGNO (x)) == 1
8014                   && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
8015                                         REGNO (x))))
8016           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8017         return reg_last_set_sign_bit_copies[REGNO (x)];
8018
8019       tem =  get_last_value (x);
8020       if (tem != 0)
8021         return num_sign_bit_copies (tem, mode);
8022
8023       if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
8024         return reg_sign_bit_copies[REGNO (x)];
8025       break;
8026
8027     case MEM:
8028 #ifdef LOAD_EXTEND_OP
8029       /* Some RISC machines sign-extend all loads of smaller than a word.  */
8030       if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8031         return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
8032 #endif
8033       break;
8034
8035     case CONST_INT:
8036       /* If the constant is negative, take its 1's complement and remask.
8037          Then see how many zero bits we have.  */
8038       nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8039       if (bitwidth <= HOST_BITS_PER_WIDE_INT
8040           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8041         nonzero = (~ nonzero) & GET_MODE_MASK (mode);
8042
8043       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8044
8045     case SUBREG:
8046       /* If this is a SUBREG for a promoted object that is sign-extended
8047          and we are looking at it in a wider mode, we know that at least the
8048          high-order bits are known to be sign bit copies.  */
8049
8050       if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8051         return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8052                     num_sign_bit_copies (SUBREG_REG (x), mode));
8053
8054       /* For a smaller object, just ignore the high bits.  */
8055       if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8056         {
8057           num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
8058           return MAX (1, (num0
8059                           - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8060                              - bitwidth)));
8061         }
8062
8063 #ifdef WORD_REGISTER_OPERATIONS
8064 #ifdef LOAD_EXTEND_OP
8065       /* For paradoxical SUBREGs on machines where all register operations
8066          affect the entire register, just look inside.  Note that we are
8067          passing MODE to the recursive call, so the number of sign bit copies
8068          will remain relative to that mode, not the inner mode.  */
8069
8070       /* This works only if loads sign extend.  Otherwise, if we get a
8071          reload for the inner part, it may be loaded from the stack, and
8072          then we lose all sign bit copies that existed before the store
8073          to the stack.  */
8074
8075       if ((GET_MODE_SIZE (GET_MODE (x))
8076            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8077           && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
8078         return num_sign_bit_copies (SUBREG_REG (x), mode);
8079 #endif
8080 #endif
8081       break;
8082
8083     case SIGN_EXTRACT:
8084       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8085         return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
8086       break;
8087
8088     case SIGN_EXTEND: 
8089       return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8090               + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
8091
8092     case TRUNCATE:
8093       /* For a smaller object, just ignore the high bits.  */
8094       num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
8095       return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8096                               - bitwidth)));
8097
8098     case NOT:
8099       return num_sign_bit_copies (XEXP (x, 0), mode);
8100
8101     case ROTATE:       case ROTATERT:
8102       /* If we are rotating left by a number of bits less than the number
8103          of sign bit copies, we can just subtract that amount from the
8104          number.  */
8105       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8106           && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
8107         {
8108           num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8109           return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8110                                  : bitwidth - INTVAL (XEXP (x, 1))));
8111         }
8112       break;
8113
8114     case NEG:
8115       /* In general, this subtracts one sign bit copy.  But if the value
8116          is known to be positive, the number of sign bit copies is the
8117          same as that of the input.  Finally, if the input has just one bit
8118          that might be nonzero, all the bits are copies of the sign bit.  */
8119       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8120       if (bitwidth > HOST_BITS_PER_WIDE_INT)
8121         return num0 > 1 ? num0 - 1 : 1;
8122
8123       nonzero = nonzero_bits (XEXP (x, 0), mode);
8124       if (nonzero == 1)
8125         return bitwidth;
8126
8127       if (num0 > 1
8128           && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8129         num0--;
8130
8131       return num0;
8132
8133     case IOR:   case AND:   case XOR:
8134     case SMIN:  case SMAX:  case UMIN:  case UMAX:
8135       /* Logical operations will preserve the number of sign-bit copies.
8136          MIN and MAX operations always return one of the operands.  */
8137       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8138       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8139       return MIN (num0, num1);
8140
8141     case PLUS:  case MINUS:
8142       /* For addition and subtraction, we can have a 1-bit carry.  However,
8143          if we are subtracting 1 from a positive number, there will not
8144          be such a carry.  Furthermore, if the positive number is known to
8145          be 0 or 1, we know the result is either -1 or 0.  */
8146
8147       if (code == PLUS && XEXP (x, 1) == constm1_rtx
8148           && bitwidth <= HOST_BITS_PER_WIDE_INT)
8149         {
8150           nonzero = nonzero_bits (XEXP (x, 0), mode);
8151           if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8152             return (nonzero == 1 || nonzero == 0 ? bitwidth
8153                     : bitwidth - floor_log2 (nonzero) - 1);
8154         }
8155
8156       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8157       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8158       return MAX (1, MIN (num0, num1) - 1);
8159       
8160     case MULT:
8161       /* The number of bits of the product is the sum of the number of
8162          bits of both terms.  However, unless one of the terms if known
8163          to be positive, we must allow for an additional bit since negating
8164          a negative number can remove one sign bit copy.  */
8165
8166       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8167       num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8168
8169       result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8170       if (result > 0
8171           && (bitwidth > HOST_BITS_PER_WIDE_INT
8172               || (((nonzero_bits (XEXP (x, 0), mode)
8173                     & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8174                   && ((nonzero_bits (XEXP (x, 1), mode)
8175                        & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8176         result--;
8177
8178       return MAX (1, result);
8179
8180     case UDIV:
8181       /* The result must be <= the first operand.  If the first operand
8182          has the high bit set, we know nothing about the number of sign
8183          bit copies.  */
8184       if (bitwidth > HOST_BITS_PER_WIDE_INT)
8185         return 1;
8186       else if ((nonzero_bits (XEXP (x, 0), mode)
8187                 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8188         return 1;
8189       else
8190         return num_sign_bit_copies (XEXP (x, 0), mode);
8191                                     
8192     case UMOD:
8193       /* The result must be <= the scond operand.  */
8194       return num_sign_bit_copies (XEXP (x, 1), mode);
8195
8196     case DIV:
8197       /* Similar to unsigned division, except that we have to worry about
8198          the case where the divisor is negative, in which case we have
8199          to add 1.  */
8200       result = num_sign_bit_copies (XEXP (x, 0), mode);
8201       if (result > 1
8202           && (bitwidth > HOST_BITS_PER_WIDE_INT
8203               || (nonzero_bits (XEXP (x, 1), mode)
8204                   & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8205         result--;
8206
8207       return result;
8208
8209     case MOD:
8210       result = num_sign_bit_copies (XEXP (x, 1), mode);
8211       if (result > 1
8212           && (bitwidth > HOST_BITS_PER_WIDE_INT
8213               || (nonzero_bits (XEXP (x, 1), mode)
8214                   & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8215         result--;
8216
8217       return result;
8218
8219     case ASHIFTRT:
8220       /* Shifts by a constant add to the number of bits equal to the
8221          sign bit.  */
8222       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8223       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8224           && INTVAL (XEXP (x, 1)) > 0)
8225         num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
8226
8227       return num0;
8228
8229     case ASHIFT:
8230       /* Left shifts destroy copies.  */
8231       if (GET_CODE (XEXP (x, 1)) != CONST_INT
8232           || INTVAL (XEXP (x, 1)) < 0
8233           || INTVAL (XEXP (x, 1)) >= bitwidth)
8234         return 1;
8235
8236       num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8237       return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8238
8239     case IF_THEN_ELSE:
8240       num0 = num_sign_bit_copies (XEXP (x, 1), mode);
8241       num1 = num_sign_bit_copies (XEXP (x, 2), mode);
8242       return MIN (num0, num1);
8243
8244     case EQ:  case NE:  case GE:  case GT:  case LE:  case LT:
8245     case GEU: case GTU: case LEU: case LTU:
8246       if (STORE_FLAG_VALUE == -1)
8247         return bitwidth;
8248       break;
8249       
8250     default:
8251       break;
8252     }
8253
8254   /* If we haven't been able to figure it out by one of the above rules,
8255      see if some of the high-order bits are known to be zero.  If so,
8256      count those bits and return one less than that amount.  If we can't
8257      safely compute the mask for this mode, always return BITWIDTH.  */
8258
8259   if (bitwidth > HOST_BITS_PER_WIDE_INT)
8260     return 1;
8261
8262   nonzero = nonzero_bits (x, mode);
8263   return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8264           ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8265 }
8266 \f
8267 /* Return the number of "extended" bits there are in X, when interpreted
8268    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
8269    unsigned quantities, this is the number of high-order zero bits.
8270    For signed quantities, this is the number of copies of the sign bit
8271    minus 1.  In both case, this function returns the number of "spare"
8272    bits.  For example, if two quantities for which this function returns
8273    at least 1 are added, the addition is known not to overflow.
8274
8275    This function will always return 0 unless called during combine, which
8276    implies that it must be called from a define_split.  */
8277
8278 int
8279 extended_count (x, mode, unsignedp)
8280      rtx x;
8281      enum machine_mode mode;
8282      int unsignedp;
8283 {
8284   if (nonzero_sign_valid == 0)
8285     return 0;
8286
8287   return (unsignedp
8288           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8289              && (GET_MODE_BITSIZE (mode) - 1
8290                  - floor_log2 (nonzero_bits (x, mode))))
8291           : num_sign_bit_copies (x, mode) - 1);
8292 }
8293 \f
8294 /* This function is called from `simplify_shift_const' to merge two
8295    outer operations.  Specifically, we have already found that we need
8296    to perform operation *POP0 with constant *PCONST0 at the outermost
8297    position.  We would now like to also perform OP1 with constant CONST1
8298    (with *POP0 being done last).
8299
8300    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8301    the resulting operation.  *PCOMP_P is set to 1 if we would need to 
8302    complement the innermost operand, otherwise it is unchanged.
8303
8304    MODE is the mode in which the operation will be done.  No bits outside
8305    the width of this mode matter.  It is assumed that the width of this mode
8306    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8307
8308    If *POP0 or OP1 are NIL, it means no operation is required.  Only NEG, PLUS,
8309    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
8310    result is simply *PCONST0.
8311
8312    If the resulting operation cannot be expressed as one operation, we
8313    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
8314
8315 static int
8316 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
8317      enum rtx_code *pop0;
8318      HOST_WIDE_INT *pconst0;
8319      enum rtx_code op1;
8320      HOST_WIDE_INT const1;
8321      enum machine_mode mode;
8322      int *pcomp_p;
8323 {
8324   enum rtx_code op0 = *pop0;
8325   HOST_WIDE_INT const0 = *pconst0;
8326
8327   const0 &= GET_MODE_MASK (mode);
8328   const1 &= GET_MODE_MASK (mode);
8329
8330   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
8331   if (op0 == AND)
8332     const1 &= const0;
8333
8334   /* If OP0 or OP1 is NIL, this is easy.  Similarly if they are the same or
8335      if OP0 is SET.  */
8336
8337   if (op1 == NIL || op0 == SET)
8338     return 1;
8339
8340   else if (op0 == NIL)
8341     op0 = op1, const0 = const1;
8342
8343   else if (op0 == op1)
8344     {
8345       switch (op0)
8346         {
8347         case AND:
8348           const0 &= const1;
8349           break;
8350         case IOR:
8351           const0 |= const1;
8352           break;
8353         case XOR:
8354           const0 ^= const1;
8355           break;
8356         case PLUS:
8357           const0 += const1;
8358           break;
8359         case NEG:
8360           op0 = NIL;
8361           break;
8362         default:
8363           break;
8364         }
8365     }
8366
8367   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
8368   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8369     return 0;
8370
8371   /* If the two constants aren't the same, we can't do anything.  The
8372      remaining six cases can all be done.  */
8373   else if (const0 != const1)
8374     return 0;
8375
8376   else
8377     switch (op0)
8378       {
8379       case IOR:
8380         if (op1 == AND)
8381           /* (a & b) | b == b */
8382           op0 = SET;
8383         else /* op1 == XOR */
8384           /* (a ^ b) | b == a | b */
8385           {;}
8386         break;
8387
8388       case XOR:
8389         if (op1 == AND)
8390           /* (a & b) ^ b == (~a) & b */
8391           op0 = AND, *pcomp_p = 1;
8392         else /* op1 == IOR */
8393           /* (a | b) ^ b == a & ~b */
8394           op0 = AND, *pconst0 = ~ const0;
8395         break;
8396
8397       case AND:
8398         if (op1 == IOR)
8399           /* (a | b) & b == b */
8400         op0 = SET;
8401         else /* op1 == XOR */
8402           /* (a ^ b) & b) == (~a) & b */
8403           *pcomp_p = 1;
8404         break;
8405       default:
8406         break;
8407       }
8408
8409   /* Check for NO-OP cases.  */
8410   const0 &= GET_MODE_MASK (mode);
8411   if (const0 == 0
8412       && (op0 == IOR || op0 == XOR || op0 == PLUS))
8413     op0 = NIL;
8414   else if (const0 == 0 && op0 == AND)
8415     op0 = SET;
8416   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8417            && op0 == AND)
8418     op0 = NIL;
8419
8420   /* ??? Slightly redundant with the above mask, but not entirely.
8421      Moving this above means we'd have to sign-extend the mode mask
8422      for the final test.  */
8423   const0 = trunc_int_for_mode (const0, mode);
8424
8425   *pop0 = op0;
8426   *pconst0 = const0;
8427
8428   return 1;
8429 }
8430 \f
8431 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
8432    The result of the shift is RESULT_MODE.  X, if non-zero, is an expression
8433    that we started with.
8434
8435    The shift is normally computed in the widest mode we find in VAROP, as
8436    long as it isn't a different number of words than RESULT_MODE.  Exceptions
8437    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
8438
8439 static rtx
8440 simplify_shift_const (x, code, result_mode, varop, count)
8441      rtx x;
8442      enum rtx_code code;
8443      enum machine_mode result_mode;
8444      rtx varop;
8445      int count;
8446 {
8447   enum rtx_code orig_code = code;
8448   int orig_count = count;
8449   enum machine_mode mode = result_mode;
8450   enum machine_mode shift_mode, tmode;
8451   int mode_words
8452     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8453   /* We form (outer_op (code varop count) (outer_const)).  */
8454   enum rtx_code outer_op = NIL;
8455   HOST_WIDE_INT outer_const = 0;
8456   rtx const_rtx;
8457   int complement_p = 0;
8458   rtx new;
8459
8460   /* If we were given an invalid count, don't do anything except exactly
8461      what was requested.  */
8462
8463   if (count < 0 || count > GET_MODE_BITSIZE (mode))
8464     {
8465       if (x)
8466         return x;
8467
8468       return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (count));
8469     }
8470
8471   /* Unless one of the branches of the `if' in this loop does a `continue',
8472      we will `break' the loop after the `if'.  */
8473
8474   while (count != 0)
8475     {
8476       /* If we have an operand of (clobber (const_int 0)), just return that
8477          value.  */
8478       if (GET_CODE (varop) == CLOBBER)
8479         return varop;
8480
8481       /* If we discovered we had to complement VAROP, leave.  Making a NOT
8482          here would cause an infinite loop.  */
8483       if (complement_p)
8484         break;
8485
8486       /* Convert ROTATERT to ROTATE.  */
8487       if (code == ROTATERT)
8488         code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8489
8490       /* We need to determine what mode we will do the shift in.  If the
8491          shift is a right shift or a ROTATE, we must always do it in the mode
8492          it was originally done in.  Otherwise, we can do it in MODE, the
8493          widest mode encountered.  */
8494       shift_mode
8495         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8496            ? result_mode : mode);
8497
8498       /* Handle cases where the count is greater than the size of the mode
8499          minus 1.  For ASHIFT, use the size minus one as the count (this can
8500          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
8501          take the count modulo the size.  For other shifts, the result is
8502          zero.
8503
8504          Since these shifts are being produced by the compiler by combining
8505          multiple operations, each of which are defined, we know what the
8506          result is supposed to be.  */
8507          
8508       if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8509         {
8510           if (code == ASHIFTRT)
8511             count = GET_MODE_BITSIZE (shift_mode) - 1;
8512           else if (code == ROTATE || code == ROTATERT)
8513             count %= GET_MODE_BITSIZE (shift_mode);
8514           else
8515             {
8516               /* We can't simply return zero because there may be an
8517                  outer op.  */
8518               varop = const0_rtx;
8519               count = 0;
8520               break;
8521             }
8522         }
8523
8524       /* Negative counts are invalid and should not have been made (a
8525          programmer-specified negative count should have been handled
8526          above).  */
8527       else if (count < 0)
8528         abort ();
8529
8530       /* An arithmetic right shift of a quantity known to be -1 or 0
8531          is a no-op.  */
8532       if (code == ASHIFTRT
8533           && (num_sign_bit_copies (varop, shift_mode)
8534               == GET_MODE_BITSIZE (shift_mode)))
8535         {
8536           count = 0;
8537           break;
8538         }
8539
8540       /* If we are doing an arithmetic right shift and discarding all but
8541          the sign bit copies, this is equivalent to doing a shift by the
8542          bitsize minus one.  Convert it into that shift because it will often
8543          allow other simplifications.  */
8544
8545       if (code == ASHIFTRT
8546           && (count + num_sign_bit_copies (varop, shift_mode)
8547               >= GET_MODE_BITSIZE (shift_mode)))
8548         count = GET_MODE_BITSIZE (shift_mode) - 1;
8549
8550       /* We simplify the tests below and elsewhere by converting
8551          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8552          `make_compound_operation' will convert it to a ASHIFTRT for
8553          those machines (such as Vax) that don't have a LSHIFTRT.  */
8554       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8555           && code == ASHIFTRT
8556           && ((nonzero_bits (varop, shift_mode)
8557                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8558               == 0))
8559         code = LSHIFTRT;
8560
8561       switch (GET_CODE (varop))
8562         {
8563         case SIGN_EXTEND:
8564         case ZERO_EXTEND:
8565         case SIGN_EXTRACT:
8566         case ZERO_EXTRACT:
8567           new = expand_compound_operation (varop);
8568           if (new != varop)
8569             {
8570               varop = new;
8571               continue;
8572             }
8573           break;
8574
8575         case MEM:
8576           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8577              minus the width of a smaller mode, we can do this with a
8578              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
8579           if ((code == ASHIFTRT || code == LSHIFTRT)
8580               && ! mode_dependent_address_p (XEXP (varop, 0))
8581               && ! MEM_VOLATILE_P (varop)
8582               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8583                                          MODE_INT, 1)) != BLKmode)
8584             {
8585               if (BYTES_BIG_ENDIAN)
8586                 new = gen_rtx_MEM (tmode, XEXP (varop, 0));
8587               else
8588                 new = gen_rtx_MEM (tmode,
8589                                    plus_constant (XEXP (varop, 0),
8590                                                   count / BITS_PER_UNIT));
8591               RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
8592               MEM_COPY_ATTRIBUTES (new, varop);
8593               varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8594                                        : ZERO_EXTEND, mode, new);
8595               count = 0;
8596               continue;
8597             }
8598           break;
8599
8600         case USE:
8601           /* Similar to the case above, except that we can only do this if
8602              the resulting mode is the same as that of the underlying
8603              MEM and adjust the address depending on the *bits* endianness
8604              because of the way that bit-field extract insns are defined.  */
8605           if ((code == ASHIFTRT || code == LSHIFTRT)
8606               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8607                                          MODE_INT, 1)) != BLKmode
8608               && tmode == GET_MODE (XEXP (varop, 0)))
8609             {
8610               if (BITS_BIG_ENDIAN)
8611                 new = XEXP (varop, 0);
8612               else
8613                 {
8614                   new = copy_rtx (XEXP (varop, 0));
8615                   SUBST (XEXP (new, 0), 
8616                          plus_constant (XEXP (new, 0),
8617                                         count / BITS_PER_UNIT));
8618                 }
8619
8620               varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8621                                        : ZERO_EXTEND, mode, new);
8622               count = 0;
8623               continue;
8624             }
8625           break;
8626
8627         case SUBREG:
8628           /* If VAROP is a SUBREG, strip it as long as the inner operand has
8629              the same number of words as what we've seen so far.  Then store
8630              the widest mode in MODE.  */
8631           if (subreg_lowpart_p (varop)
8632               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8633                   > GET_MODE_SIZE (GET_MODE (varop)))
8634               && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8635                     + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8636                   == mode_words))
8637             {
8638               varop = SUBREG_REG (varop);
8639               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8640                 mode = GET_MODE (varop);
8641               continue;
8642             }
8643           break;
8644
8645         case MULT:
8646           /* Some machines use MULT instead of ASHIFT because MULT
8647              is cheaper.  But it is still better on those machines to
8648              merge two shifts into one.  */
8649           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8650               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8651             {
8652               varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
8653                                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8654               continue;
8655             }
8656           break;
8657
8658         case UDIV:
8659           /* Similar, for when divides are cheaper.  */
8660           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8661               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8662             {
8663               varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
8664                                   GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8665               continue;
8666             }
8667           break;
8668
8669         case ASHIFTRT:
8670           /* If we are extracting just the sign bit of an arithmetic right 
8671              shift, that shift is not needed.  */
8672           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
8673             {
8674               varop = XEXP (varop, 0);
8675               continue;
8676             }
8677
8678           /* ... fall through ...  */
8679
8680         case LSHIFTRT:
8681         case ASHIFT:
8682         case ROTATE:
8683           /* Here we have two nested shifts.  The result is usually the
8684              AND of a new shift with a mask.  We compute the result below.  */
8685           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8686               && INTVAL (XEXP (varop, 1)) >= 0
8687               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8688               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8689               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8690             {
8691               enum rtx_code first_code = GET_CODE (varop);
8692               int first_count = INTVAL (XEXP (varop, 1));
8693               unsigned HOST_WIDE_INT mask;
8694               rtx mask_rtx;
8695
8696               /* We have one common special case.  We can't do any merging if
8697                  the inner code is an ASHIFTRT of a smaller mode.  However, if
8698                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8699                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8700                  we can convert it to
8701                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8702                  This simplifies certain SIGN_EXTEND operations.  */
8703               if (code == ASHIFT && first_code == ASHIFTRT
8704                   && (GET_MODE_BITSIZE (result_mode)
8705                       - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8706                 {
8707                   /* C3 has the low-order C1 bits zero.  */
8708                   
8709                   mask = (GET_MODE_MASK (mode)
8710                           & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
8711
8712                   varop = simplify_and_const_int (NULL_RTX, result_mode,
8713                                                   XEXP (varop, 0), mask);
8714                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8715                                                 varop, count);
8716                   count = first_count;
8717                   code = ASHIFTRT;
8718                   continue;
8719                 }
8720               
8721               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8722                  than C1 high-order bits equal to the sign bit, we can convert
8723                  this to either an ASHIFT or a ASHIFTRT depending on the
8724                  two counts. 
8725
8726                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
8727
8728               if (code == ASHIFTRT && first_code == ASHIFT
8729                   && GET_MODE (varop) == shift_mode
8730                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8731                       > first_count))
8732                 {
8733                   count -= first_count;
8734                   if (count < 0)
8735                     count = - count, code = ASHIFT;
8736                   varop = XEXP (varop, 0);
8737                   continue;
8738                 }
8739
8740               /* There are some cases we can't do.  If CODE is ASHIFTRT,
8741                  we can only do this if FIRST_CODE is also ASHIFTRT.
8742
8743                  We can't do the case when CODE is ROTATE and FIRST_CODE is
8744                  ASHIFTRT.
8745
8746                  If the mode of this shift is not the mode of the outer shift,
8747                  we can't do this if either shift is a right shift or ROTATE.
8748
8749                  Finally, we can't do any of these if the mode is too wide
8750                  unless the codes are the same.
8751
8752                  Handle the case where the shift codes are the same
8753                  first.  */
8754
8755               if (code == first_code)
8756                 {
8757                   if (GET_MODE (varop) != result_mode
8758                       && (code == ASHIFTRT || code == LSHIFTRT
8759                           || code == ROTATE))
8760                     break;
8761
8762                   count += first_count;
8763                   varop = XEXP (varop, 0);
8764                   continue;
8765                 }
8766
8767               if (code == ASHIFTRT
8768                   || (code == ROTATE && first_code == ASHIFTRT)
8769                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8770                   || (GET_MODE (varop) != result_mode
8771                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
8772                           || first_code == ROTATE
8773                           || code == ROTATE)))
8774                 break;
8775
8776               /* To compute the mask to apply after the shift, shift the
8777                  nonzero bits of the inner shift the same way the 
8778                  outer shift will.  */
8779
8780               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8781
8782               mask_rtx
8783                 = simplify_binary_operation (code, result_mode, mask_rtx,
8784                                              GEN_INT (count));
8785                                   
8786               /* Give up if we can't compute an outer operation to use.  */
8787               if (mask_rtx == 0
8788                   || GET_CODE (mask_rtx) != CONST_INT
8789                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
8790                                         INTVAL (mask_rtx),
8791                                         result_mode, &complement_p))
8792                 break;
8793
8794               /* If the shifts are in the same direction, we add the
8795                  counts.  Otherwise, we subtract them.  */
8796               if ((code == ASHIFTRT || code == LSHIFTRT)
8797                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8798                 count += first_count;
8799               else
8800                 count -= first_count;
8801
8802               /* If COUNT is positive, the new shift is usually CODE, 
8803                  except for the two exceptions below, in which case it is
8804                  FIRST_CODE.  If the count is negative, FIRST_CODE should
8805                  always be used  */
8806               if (count > 0
8807                   && ((first_code == ROTATE && code == ASHIFT)
8808                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
8809                 code = first_code;
8810               else if (count < 0)
8811                 code = first_code, count = - count;
8812
8813               varop = XEXP (varop, 0);
8814               continue;
8815             }
8816
8817           /* If we have (A << B << C) for any shift, we can convert this to
8818              (A << C << B).  This wins if A is a constant.  Only try this if
8819              B is not a constant.  */
8820
8821           else if (GET_CODE (varop) == code
8822                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
8823                    && 0 != (new
8824                             = simplify_binary_operation (code, mode,
8825                                                          XEXP (varop, 0),
8826                                                          GEN_INT (count))))
8827             {
8828               varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8829               count = 0;
8830               continue;
8831             }
8832           break;
8833
8834         case NOT:
8835           /* Make this fit the case below.  */
8836           varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
8837                                    GEN_INT (GET_MODE_MASK (mode)));
8838           continue;
8839
8840         case IOR:
8841         case AND:
8842         case XOR:
8843           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8844              with C the size of VAROP - 1 and the shift is logical if
8845              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8846              we have an (le X 0) operation.   If we have an arithmetic shift
8847              and STORE_FLAG_VALUE is 1 or we have a logical shift with
8848              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
8849
8850           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8851               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8852               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8853               && (code == LSHIFTRT || code == ASHIFTRT)
8854               && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8855               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8856             {
8857               count = 0;
8858               varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8859                                        const0_rtx);
8860
8861               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8862                 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8863
8864               continue;
8865             }
8866
8867           /* If we have (shift (logical)), move the logical to the outside
8868              to allow it to possibly combine with another logical and the
8869              shift to combine with another shift.  This also canonicalizes to
8870              what a ZERO_EXTRACT looks like.  Also, some machines have
8871              (and (shift)) insns.  */
8872
8873           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8874               && (new = simplify_binary_operation (code, result_mode,
8875                                                    XEXP (varop, 1),
8876                                                    GEN_INT (count))) != 0
8877               && GET_CODE(new) == CONST_INT
8878               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8879                                   INTVAL (new), result_mode, &complement_p))
8880             {
8881               varop = XEXP (varop, 0);
8882               continue;
8883             }
8884
8885           /* If we can't do that, try to simplify the shift in each arm of the
8886              logical expression, make a new logical expression, and apply
8887              the inverse distributive law.  */
8888           {
8889             rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8890                                             XEXP (varop, 0), count);
8891             rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8892                                             XEXP (varop, 1), count);
8893
8894             varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
8895             varop = apply_distributive_law (varop);
8896
8897             count = 0;
8898           }
8899           break;
8900
8901         case EQ:
8902           /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8903              says that the sign bit can be tested, FOO has mode MODE, C is
8904              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8905              that may be nonzero.  */
8906           if (code == LSHIFTRT
8907               && XEXP (varop, 1) == const0_rtx
8908               && GET_MODE (XEXP (varop, 0)) == result_mode
8909               && count == GET_MODE_BITSIZE (result_mode) - 1
8910               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8911               && ((STORE_FLAG_VALUE
8912                    & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
8913               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8914               && merge_outer_ops (&outer_op, &outer_const, XOR,
8915                                   (HOST_WIDE_INT) 1, result_mode,
8916                                   &complement_p))
8917             {
8918               varop = XEXP (varop, 0);
8919               count = 0;
8920               continue;
8921             }
8922           break;
8923
8924         case NEG:
8925           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8926              than the number of bits in the mode is equivalent to A.  */
8927           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8928               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
8929             {
8930               varop = XEXP (varop, 0);
8931               count = 0;
8932               continue;
8933             }
8934
8935           /* NEG commutes with ASHIFT since it is multiplication.  Move the
8936              NEG outside to allow shifts to combine.  */
8937           if (code == ASHIFT
8938               && merge_outer_ops (&outer_op, &outer_const, NEG,
8939                                   (HOST_WIDE_INT) 0, result_mode,
8940                                   &complement_p))
8941             {
8942               varop = XEXP (varop, 0);
8943               continue;
8944             }
8945           break;
8946
8947         case PLUS:
8948           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8949              is one less than the number of bits in the mode is
8950              equivalent to (xor A 1).  */
8951           if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8952               && XEXP (varop, 1) == constm1_rtx
8953               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8954               && merge_outer_ops (&outer_op, &outer_const, XOR,
8955                                   (HOST_WIDE_INT) 1, result_mode,
8956                                   &complement_p))
8957             {
8958               count = 0;
8959               varop = XEXP (varop, 0);
8960               continue;
8961             }
8962
8963           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
8964              that might be nonzero in BAR are those being shifted out and those
8965              bits are known zero in FOO, we can replace the PLUS with FOO.
8966              Similarly in the other operand order.  This code occurs when
8967              we are computing the size of a variable-size array.  */
8968
8969           if ((code == ASHIFTRT || code == LSHIFTRT)
8970               && count < HOST_BITS_PER_WIDE_INT
8971               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8972               && (nonzero_bits (XEXP (varop, 1), result_mode)
8973                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
8974             {
8975               varop = XEXP (varop, 0);
8976               continue;
8977             }
8978           else if ((code == ASHIFTRT || code == LSHIFTRT)
8979                    && count < HOST_BITS_PER_WIDE_INT
8980                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8981                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8982                             >> count)
8983                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8984                             & nonzero_bits (XEXP (varop, 1),
8985                                                  result_mode)))
8986             {
8987               varop = XEXP (varop, 1);
8988               continue;
8989             }
8990
8991           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
8992           if (code == ASHIFT
8993               && GET_CODE (XEXP (varop, 1)) == CONST_INT
8994               && (new = simplify_binary_operation (ASHIFT, result_mode,
8995                                                    XEXP (varop, 1),
8996                                                    GEN_INT (count))) != 0
8997               && GET_CODE(new) == CONST_INT
8998               && merge_outer_ops (&outer_op, &outer_const, PLUS,
8999                                   INTVAL (new), result_mode, &complement_p))
9000             {
9001               varop = XEXP (varop, 0);
9002               continue;
9003             }
9004           break;
9005
9006         case MINUS:
9007           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9008              with C the size of VAROP - 1 and the shift is logical if
9009              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9010              we have a (gt X 0) operation.  If the shift is arithmetic with
9011              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9012              we have a (neg (gt X 0)) operation.  */
9013
9014           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9015               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9016               && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
9017               && (code == LSHIFTRT || code == ASHIFTRT)
9018               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9019               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9020               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9021             {
9022               count = 0;
9023               varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
9024                                        const0_rtx);
9025
9026               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9027                 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
9028
9029               continue;
9030             }
9031           break;
9032
9033         case TRUNCATE:
9034           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9035              if the truncate does not affect the value.  */
9036           if (code == LSHIFTRT
9037               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9038               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9039               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9040                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9041                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9042             {
9043               rtx varop_inner = XEXP (varop, 0);
9044
9045               varop_inner = gen_rtx_combine (LSHIFTRT,
9046                                              GET_MODE (varop_inner),
9047                                              XEXP (varop_inner, 0),
9048                                              GEN_INT (count + INTVAL (XEXP (varop_inner, 1))));
9049               varop = gen_rtx_combine (TRUNCATE, GET_MODE (varop),
9050                                        varop_inner);
9051               count = 0;
9052               continue;
9053             }
9054           break;
9055           
9056         default:
9057           break;
9058         }
9059
9060       break;
9061     }
9062
9063   /* We need to determine what mode to do the shift in.  If the shift is
9064      a right shift or ROTATE, we must always do it in the mode it was
9065      originally done in.  Otherwise, we can do it in MODE, the widest mode
9066      encountered.  The code we care about is that of the shift that will
9067      actually be done, not the shift that was originally requested.  */
9068   shift_mode
9069     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9070        ? result_mode : mode);
9071
9072   /* We have now finished analyzing the shift.  The result should be
9073      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9074      OUTER_OP is non-NIL, it is an operation that needs to be applied
9075      to the result of the shift.  OUTER_CONST is the relevant constant,
9076      but we must turn off all bits turned off in the shift.
9077
9078      If we were passed a value for X, see if we can use any pieces of
9079      it.  If not, make new rtx.  */
9080
9081   if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9082       && GET_CODE (XEXP (x, 1)) == CONST_INT
9083       && INTVAL (XEXP (x, 1)) == count)
9084     const_rtx = XEXP (x, 1);
9085   else
9086     const_rtx = GEN_INT (count);
9087
9088   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9089       && GET_MODE (XEXP (x, 0)) == shift_mode
9090       && SUBREG_REG (XEXP (x, 0)) == varop)
9091     varop = XEXP (x, 0);
9092   else if (GET_MODE (varop) != shift_mode)
9093     varop = gen_lowpart_for_combine (shift_mode, varop);
9094
9095   /* If we can't make the SUBREG, try to return what we were given.  */
9096   if (GET_CODE (varop) == CLOBBER)
9097     return x ? x : varop;
9098
9099   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9100   if (new != 0)
9101     x = new;
9102   else
9103     {
9104       if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
9105         x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
9106
9107       SUBST (XEXP (x, 0), varop);
9108       SUBST (XEXP (x, 1), const_rtx);
9109     }
9110
9111   /* If we have an outer operation and we just made a shift, it is
9112      possible that we could have simplified the shift were it not
9113      for the outer operation.  So try to do the simplification
9114      recursively.  */
9115
9116   if (outer_op != NIL && GET_CODE (x) == code
9117       && GET_CODE (XEXP (x, 1)) == CONST_INT)
9118     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9119                               INTVAL (XEXP (x, 1)));
9120
9121   /* If we were doing a LSHIFTRT in a wider mode than it was originally,
9122      turn off all the bits that the shift would have turned off.  */
9123   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9124     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9125                                 GET_MODE_MASK (result_mode) >> orig_count);
9126       
9127   /* Do the remainder of the processing in RESULT_MODE.  */
9128   x = gen_lowpart_for_combine (result_mode, x);
9129
9130   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9131      operation.  */
9132   if (complement_p)
9133     x = gen_unary (NOT, result_mode, result_mode, x);
9134
9135   if (outer_op != NIL)
9136     {
9137       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9138         outer_const = trunc_int_for_mode (outer_const, result_mode);
9139
9140       if (outer_op == AND)
9141         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9142       else if (outer_op == SET)
9143         /* This means that we have determined that the result is
9144            equivalent to a constant.  This should be rare.  */
9145         x = GEN_INT (outer_const);
9146       else if (GET_RTX_CLASS (outer_op) == '1')
9147         x = gen_unary (outer_op, result_mode, result_mode, x);
9148       else
9149         x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9150     }
9151
9152   return x;
9153 }  
9154 \f
9155 /* Like recog, but we receive the address of a pointer to a new pattern.
9156    We try to match the rtx that the pointer points to.
9157    If that fails, we may try to modify or replace the pattern,
9158    storing the replacement into the same pointer object.
9159
9160    Modifications include deletion or addition of CLOBBERs.
9161
9162    PNOTES is a pointer to a location where any REG_UNUSED notes added for
9163    the CLOBBERs are placed.
9164
9165    The value is the final insn code from the pattern ultimately matched,
9166    or -1.  */
9167
9168 static int
9169 recog_for_combine (pnewpat, insn, pnotes)
9170      rtx *pnewpat;
9171      rtx insn;
9172      rtx *pnotes;
9173 {
9174   register rtx pat = *pnewpat;
9175   int insn_code_number;
9176   int num_clobbers_to_add = 0;
9177   int i;
9178   rtx notes = 0;
9179
9180   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9181      we use to indicate that something didn't match.  If we find such a
9182      thing, force rejection.  */
9183   if (GET_CODE (pat) == PARALLEL)
9184     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9185       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9186           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9187         return -1;
9188
9189   /* Is the result of combination a valid instruction?  */
9190   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9191
9192   /* If it isn't, there is the possibility that we previously had an insn
9193      that clobbered some register as a side effect, but the combined
9194      insn doesn't need to do that.  So try once more without the clobbers
9195      unless this represents an ASM insn.  */
9196
9197   if (insn_code_number < 0 && ! check_asm_operands (pat)
9198       && GET_CODE (pat) == PARALLEL)
9199     {
9200       int pos;
9201
9202       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9203         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9204           {
9205             if (i != pos)
9206               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9207             pos++;
9208           }
9209
9210       SUBST_INT (XVECLEN (pat, 0), pos);
9211
9212       if (pos == 1)
9213         pat = XVECEXP (pat, 0, 0);
9214
9215       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9216     }
9217
9218   /* If we had any clobbers to add, make a new pattern than contains
9219      them.  Then check to make sure that all of them are dead.  */
9220   if (num_clobbers_to_add)
9221     {
9222       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9223                                      gen_rtvec (GET_CODE (pat) == PARALLEL
9224                                                 ? (XVECLEN (pat, 0)
9225                                                    + num_clobbers_to_add)
9226                                                 : num_clobbers_to_add + 1));
9227
9228       if (GET_CODE (pat) == PARALLEL)
9229         for (i = 0; i < XVECLEN (pat, 0); i++)
9230           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9231       else
9232         XVECEXP (newpat, 0, 0) = pat;
9233
9234       add_clobbers (newpat, insn_code_number);
9235
9236       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9237            i < XVECLEN (newpat, 0); i++)
9238         {
9239           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9240               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9241             return -1;
9242           notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9243                                      XEXP (XVECEXP (newpat, 0, i), 0), notes);
9244         }
9245       pat = newpat;
9246     }
9247
9248   *pnewpat = pat;
9249   *pnotes = notes;
9250
9251   return insn_code_number;
9252 }
9253 \f
9254 /* Like gen_lowpart but for use by combine.  In combine it is not possible
9255    to create any new pseudoregs.  However, it is safe to create
9256    invalid memory addresses, because combine will try to recognize
9257    them and all they will do is make the combine attempt fail.
9258
9259    If for some reason this cannot do its job, an rtx
9260    (clobber (const_int 0)) is returned.
9261    An insn containing that will not be recognized.  */
9262
9263 #undef gen_lowpart
9264
9265 static rtx
9266 gen_lowpart_for_combine (mode, x)
9267      enum machine_mode mode;
9268      register rtx x;
9269 {
9270   rtx result;
9271
9272   if (GET_MODE (x) == mode)
9273     return x;
9274
9275   /* We can only support MODE being wider than a word if X is a
9276      constant integer or has a mode the same size.  */
9277
9278   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9279       && ! ((GET_MODE (x) == VOIDmode
9280              && (GET_CODE (x) == CONST_INT
9281                  || GET_CODE (x) == CONST_DOUBLE))
9282             || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
9283     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9284
9285   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
9286      won't know what to do.  So we will strip off the SUBREG here and
9287      process normally.  */
9288   if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
9289     {
9290       x = SUBREG_REG (x);
9291       if (GET_MODE (x) == mode)
9292         return x;
9293     }
9294
9295   result = gen_lowpart_common (mode, x);
9296   if (result != 0
9297       && GET_CODE (result) == SUBREG
9298       && GET_CODE (SUBREG_REG (result)) == REG
9299       && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
9300       && (GET_MODE_SIZE (GET_MODE (result))
9301           != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
9302     REG_CHANGES_SIZE (REGNO (SUBREG_REG (result))) = 1;
9303
9304   if (result)
9305     return result;
9306
9307   if (GET_CODE (x) == MEM)
9308     {
9309       register int offset = 0;
9310       rtx new;
9311
9312       /* Refuse to work on a volatile memory ref or one with a mode-dependent
9313          address.  */
9314       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9315         return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9316
9317       /* If we want to refer to something bigger than the original memref,
9318          generate a perverse subreg instead.  That will force a reload
9319          of the original memref X.  */
9320       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
9321         return gen_rtx_SUBREG (mode, x, 0);
9322
9323       if (WORDS_BIG_ENDIAN)
9324         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
9325                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
9326
9327       if (BYTES_BIG_ENDIAN)
9328         {
9329           /* Adjust the address so that the address-after-the-data is
9330              unchanged.  */
9331           offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
9332                      - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
9333         }
9334       new = gen_rtx_MEM (mode, plus_constant (XEXP (x, 0), offset));
9335       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
9336       MEM_COPY_ATTRIBUTES (new, x);
9337       return new;
9338     }
9339
9340   /* If X is a comparison operator, rewrite it in a new mode.  This
9341      probably won't match, but may allow further simplifications.  */
9342   else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9343     return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
9344
9345   /* If we couldn't simplify X any other way, just enclose it in a
9346      SUBREG.  Normally, this SUBREG won't match, but some patterns may
9347      include an explicit SUBREG or we may simplify it further in combine.  */
9348   else
9349     {
9350       int word = 0;
9351
9352       if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
9353         word = ((GET_MODE_SIZE (GET_MODE (x))
9354                  - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
9355                 / UNITS_PER_WORD);
9356       return gen_rtx_SUBREG (mode, x, word);
9357     }
9358 }
9359 \f
9360 /* Make an rtx expression.  This is a subset of gen_rtx and only supports
9361    expressions of 1, 2, or 3 operands, each of which are rtx expressions.
9362
9363    If the identical expression was previously in the insn (in the undobuf),
9364    it will be returned.  Only if it is not found will a new expression
9365    be made.  */
9366
9367 /*VARARGS2*/
9368 static rtx
9369 gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
9370 {
9371 #ifndef ANSI_PROTOTYPES
9372   enum rtx_code code;
9373   enum machine_mode mode;
9374 #endif
9375   va_list p;
9376   int n_args;
9377   rtx args[3];
9378   int j;
9379   const char *fmt;
9380   rtx rt;
9381   struct undo *undo;
9382
9383   VA_START (p, mode);
9384
9385 #ifndef ANSI_PROTOTYPES
9386   code = va_arg (p, enum rtx_code);
9387   mode = va_arg (p, enum machine_mode);
9388 #endif
9389
9390   n_args = GET_RTX_LENGTH (code);
9391   fmt = GET_RTX_FORMAT (code);
9392
9393   if (n_args == 0 || n_args > 3)
9394     abort ();
9395
9396   /* Get each arg and verify that it is supposed to be an expression.  */
9397   for (j = 0; j < n_args; j++)
9398     {
9399       if (*fmt++ != 'e')
9400         abort ();
9401
9402       args[j] = va_arg (p, rtx);
9403     }
9404
9405   va_end (p);
9406
9407   /* See if this is in undobuf.  Be sure we don't use objects that came
9408      from another insn; this could produce circular rtl structures.  */
9409
9410   for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
9411     if (!undo->is_int
9412         && GET_CODE (undo->old_contents.r) == code
9413         && GET_MODE (undo->old_contents.r) == mode)
9414       {
9415         for (j = 0; j < n_args; j++)
9416           if (XEXP (undo->old_contents.r, j) != args[j])
9417             break;
9418
9419         if (j == n_args)
9420           return undo->old_contents.r;
9421       }
9422
9423   /* Otherwise make a new rtx.  We know we have 1, 2, or 3 args.
9424      Use rtx_alloc instead of gen_rtx because it's faster on RISC.  */
9425   rt = rtx_alloc (code);
9426   PUT_MODE (rt, mode);
9427   XEXP (rt, 0) = args[0];
9428   if (n_args > 1)
9429     {
9430       XEXP (rt, 1) = args[1];
9431       if (n_args > 2)
9432         XEXP (rt, 2) = args[2];
9433     }
9434   return rt;
9435 }
9436
9437 /* These routines make binary and unary operations by first seeing if they
9438    fold; if not, a new expression is allocated.  */
9439
9440 static rtx
9441 gen_binary (code, mode, op0, op1)
9442      enum rtx_code code;
9443      enum machine_mode mode;
9444      rtx op0, op1;
9445 {
9446   rtx result;
9447   rtx tem;
9448
9449   if (GET_RTX_CLASS (code) == 'c'
9450       && (GET_CODE (op0) == CONST_INT
9451           || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9452     tem = op0, op0 = op1, op1 = tem;
9453
9454   if (GET_RTX_CLASS (code) == '<') 
9455     {
9456       enum machine_mode op_mode = GET_MODE (op0);
9457
9458       /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get 
9459          just (REL_OP X Y).  */
9460       if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9461         {
9462           op1 = XEXP (op0, 1);
9463           op0 = XEXP (op0, 0);
9464           op_mode = GET_MODE (op0);
9465         }
9466
9467       if (op_mode == VOIDmode)
9468         op_mode = GET_MODE (op1);
9469       result = simplify_relational_operation (code, op_mode, op0, op1);
9470     }
9471   else
9472     result = simplify_binary_operation (code, mode, op0, op1);
9473
9474   if (result)
9475     return result;
9476
9477   /* Put complex operands first and constants second.  */
9478   if (GET_RTX_CLASS (code) == 'c'
9479       && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9480           || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9481               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9482           || (GET_CODE (op0) == SUBREG
9483               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9484               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9485     return gen_rtx_combine (code, mode, op1, op0);
9486
9487   /* If we are turning off bits already known off in OP0, we need not do
9488      an AND.  */
9489   else if (code == AND && GET_CODE (op1) == CONST_INT
9490            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9491            && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
9492     return op0;
9493
9494   return gen_rtx_combine (code, mode, op0, op1);
9495 }
9496
9497 static rtx
9498 gen_unary (code, mode, op0_mode, op0)
9499      enum rtx_code code;
9500      enum machine_mode mode, op0_mode;
9501      rtx op0;
9502 {
9503   rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
9504
9505   if (result)
9506     return result;
9507
9508   return gen_rtx_combine (code, mode, op0);
9509 }
9510 \f
9511 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9512    comparison code that will be tested.
9513
9514    The result is a possibly different comparison code to use.  *POP0 and
9515    *POP1 may be updated.
9516
9517    It is possible that we might detect that a comparison is either always
9518    true or always false.  However, we do not perform general constant
9519    folding in combine, so this knowledge isn't useful.  Such tautologies
9520    should have been detected earlier.  Hence we ignore all such cases.  */
9521
9522 static enum rtx_code
9523 simplify_comparison (code, pop0, pop1)
9524      enum rtx_code code;
9525      rtx *pop0;
9526      rtx *pop1;
9527 {
9528   rtx op0 = *pop0;
9529   rtx op1 = *pop1;
9530   rtx tem, tem1;
9531   int i;
9532   enum machine_mode mode, tmode;
9533
9534   /* Try a few ways of applying the same transformation to both operands.  */
9535   while (1)
9536     {
9537 #ifndef WORD_REGISTER_OPERATIONS
9538       /* The test below this one won't handle SIGN_EXTENDs on these machines,
9539          so check specially.  */
9540       if (code != GTU && code != GEU && code != LTU && code != LEU
9541           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9542           && GET_CODE (XEXP (op0, 0)) == ASHIFT
9543           && GET_CODE (XEXP (op1, 0)) == ASHIFT
9544           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9545           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9546           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9547               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9548           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9549           && GET_CODE (XEXP (op1, 1)) == CONST_INT
9550           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9551           && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
9552           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
9553           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
9554           && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
9555           && (INTVAL (XEXP (op0, 1))
9556               == (GET_MODE_BITSIZE (GET_MODE (op0))
9557                   - (GET_MODE_BITSIZE
9558                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9559         {
9560           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9561           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9562         }
9563 #endif
9564
9565       /* If both operands are the same constant shift, see if we can ignore the
9566          shift.  We can if the shift is a rotate or if the bits shifted out of
9567          this shift are known to be zero for both inputs and if the type of
9568          comparison is compatible with the shift.  */
9569       if (GET_CODE (op0) == GET_CODE (op1)
9570           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9571           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9572               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9573                   && (code != GT && code != LT && code != GE && code != LE))
9574               || (GET_CODE (op0) == ASHIFTRT
9575                   && (code != GTU && code != LTU
9576                       && code != GEU && code != GEU)))
9577           && GET_CODE (XEXP (op0, 1)) == CONST_INT
9578           && INTVAL (XEXP (op0, 1)) >= 0
9579           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9580           && XEXP (op0, 1) == XEXP (op1, 1))
9581         {
9582           enum machine_mode mode = GET_MODE (op0);
9583           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9584           int shift_count = INTVAL (XEXP (op0, 1));
9585
9586           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9587             mask &= (mask >> shift_count) << shift_count;
9588           else if (GET_CODE (op0) == ASHIFT)
9589             mask = (mask & (mask << shift_count)) >> shift_count;
9590
9591           if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
9592               && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
9593             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9594           else
9595             break;
9596         }
9597
9598       /* If both operands are AND's of a paradoxical SUBREG by constant, the
9599          SUBREGs are of the same mode, and, in both cases, the AND would
9600          be redundant if the comparison was done in the narrower mode,
9601          do the comparison in the narrower mode (e.g., we are AND'ing with 1
9602          and the operand's possibly nonzero bits are 0xffffff01; in that case
9603          if we only care about QImode, we don't need the AND).  This case
9604          occurs if the output mode of an scc insn is not SImode and
9605          STORE_FLAG_VALUE == 1 (e.g., the 386).
9606
9607          Similarly, check for a case where the AND's are ZERO_EXTEND
9608          operations from some narrower mode even though a SUBREG is not
9609          present.  */
9610
9611       else if  (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9612                 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9613                 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9614         {
9615           rtx inner_op0 = XEXP (op0, 0);
9616           rtx inner_op1 = XEXP (op1, 0);
9617           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9618           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9619           int changed = 0;
9620                 
9621           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9622               && (GET_MODE_SIZE (GET_MODE (inner_op0))
9623                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9624               && (GET_MODE (SUBREG_REG (inner_op0))
9625                   == GET_MODE (SUBREG_REG (inner_op1)))
9626               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9627                   <= HOST_BITS_PER_WIDE_INT)
9628               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9629                                              GET_MODE (SUBREG_REG (inner_op0)))))
9630               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9631                                              GET_MODE (SUBREG_REG (inner_op1))))))
9632             {
9633               op0 = SUBREG_REG (inner_op0);
9634               op1 = SUBREG_REG (inner_op1);
9635
9636               /* The resulting comparison is always unsigned since we masked
9637                  off the original sign bit.  */
9638               code = unsigned_condition (code);
9639
9640               changed = 1;
9641             }
9642
9643           else if (c0 == c1)
9644             for (tmode = GET_CLASS_NARROWEST_MODE
9645                  (GET_MODE_CLASS (GET_MODE (op0)));
9646                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9647               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9648                 {
9649                   op0 = gen_lowpart_for_combine (tmode, inner_op0);
9650                   op1 = gen_lowpart_for_combine (tmode, inner_op1);
9651                   code = unsigned_condition (code);
9652                   changed = 1;
9653                   break;
9654                 }
9655
9656           if (! changed)
9657             break;
9658         }
9659
9660       /* If both operands are NOT, we can strip off the outer operation
9661          and adjust the comparison code for swapped operands; similarly for
9662          NEG, except that this must be an equality comparison.  */
9663       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9664                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9665                    && (code == EQ || code == NE)))
9666         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9667
9668       else
9669         break;
9670     }
9671      
9672   /* If the first operand is a constant, swap the operands and adjust the
9673      comparison code appropriately, but don't do this if the second operand
9674      is already a constant integer.  */
9675   if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9676     {
9677       tem = op0, op0 = op1, op1 = tem;
9678       code = swap_condition (code);
9679     }
9680
9681   /* We now enter a loop during which we will try to simplify the comparison.
9682      For the most part, we only are concerned with comparisons with zero,
9683      but some things may really be comparisons with zero but not start
9684      out looking that way.  */
9685
9686   while (GET_CODE (op1) == CONST_INT)
9687     {
9688       enum machine_mode mode = GET_MODE (op0);
9689       int mode_width = GET_MODE_BITSIZE (mode);
9690       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9691       int equality_comparison_p;
9692       int sign_bit_comparison_p;
9693       int unsigned_comparison_p;
9694       HOST_WIDE_INT const_op;
9695
9696       /* We only want to handle integral modes.  This catches VOIDmode,
9697          CCmode, and the floating-point modes.  An exception is that we
9698          can handle VOIDmode if OP0 is a COMPARE or a comparison
9699          operation.  */
9700
9701       if (GET_MODE_CLASS (mode) != MODE_INT
9702           && ! (mode == VOIDmode
9703                 && (GET_CODE (op0) == COMPARE
9704                     || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9705         break;
9706
9707       /* Get the constant we are comparing against and turn off all bits
9708          not on in our mode.  */
9709       const_op = INTVAL (op1);
9710       if (mode_width <= HOST_BITS_PER_WIDE_INT)
9711         const_op &= mask;
9712
9713       /* If we are comparing against a constant power of two and the value
9714          being compared can only have that single bit nonzero (e.g., it was
9715          `and'ed with that bit), we can replace this with a comparison
9716          with zero.  */
9717       if (const_op
9718           && (code == EQ || code == NE || code == GE || code == GEU
9719               || code == LT || code == LTU)
9720           && mode_width <= HOST_BITS_PER_WIDE_INT
9721           && exact_log2 (const_op) >= 0
9722           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9723         {
9724           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9725           op1 = const0_rtx, const_op = 0;
9726         }
9727
9728       /* Similarly, if we are comparing a value known to be either -1 or
9729          0 with -1, change it to the opposite comparison against zero.  */
9730
9731       if (const_op == -1
9732           && (code == EQ || code == NE || code == GT || code == LE
9733               || code == GEU || code == LTU)
9734           && num_sign_bit_copies (op0, mode) == mode_width)
9735         {
9736           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9737           op1 = const0_rtx, const_op = 0;
9738         }
9739
9740       /* Do some canonicalizations based on the comparison code.  We prefer
9741          comparisons against zero and then prefer equality comparisons.  
9742          If we can reduce the size of a constant, we will do that too.  */
9743
9744       switch (code)
9745         {
9746         case LT:
9747           /* < C is equivalent to <= (C - 1) */
9748           if (const_op > 0)
9749             {
9750               const_op -= 1;
9751               op1 = GEN_INT (const_op);
9752               code = LE;
9753               /* ... fall through to LE case below.  */
9754             }
9755           else
9756             break;
9757
9758         case LE:
9759           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
9760           if (const_op < 0)
9761             {
9762               const_op += 1;
9763               op1 = GEN_INT (const_op);
9764               code = LT;
9765             }
9766
9767           /* If we are doing a <= 0 comparison on a value known to have
9768              a zero sign bit, we can replace this with == 0.  */
9769           else if (const_op == 0
9770                    && mode_width <= HOST_BITS_PER_WIDE_INT
9771                    && (nonzero_bits (op0, mode)
9772                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9773             code = EQ;
9774           break;
9775
9776         case GE:
9777           /* >= C is equivalent to > (C - 1).  */
9778           if (const_op > 0)
9779             {
9780               const_op -= 1;
9781               op1 = GEN_INT (const_op);
9782               code = GT;
9783               /* ... fall through to GT below.  */
9784             }
9785           else
9786             break;
9787
9788         case GT:
9789           /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9790           if (const_op < 0)
9791             {
9792               const_op += 1;
9793               op1 = GEN_INT (const_op);
9794               code = GE;
9795             }
9796
9797           /* If we are doing a > 0 comparison on a value known to have
9798              a zero sign bit, we can replace this with != 0.  */
9799           else if (const_op == 0
9800                    && mode_width <= HOST_BITS_PER_WIDE_INT
9801                    && (nonzero_bits (op0, mode)
9802                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9803             code = NE;
9804           break;
9805
9806         case LTU:
9807           /* < C is equivalent to <= (C - 1).  */
9808           if (const_op > 0)
9809             {
9810               const_op -= 1;
9811               op1 = GEN_INT (const_op);
9812               code = LEU;
9813               /* ... fall through ...  */
9814             }
9815
9816           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
9817           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9818                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9819             {
9820               const_op = 0, op1 = const0_rtx;
9821               code = GE;
9822               break;
9823             }
9824           else
9825             break;
9826
9827         case LEU:
9828           /* unsigned <= 0 is equivalent to == 0 */
9829           if (const_op == 0)
9830             code = EQ;
9831
9832           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
9833           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9834                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9835             {
9836               const_op = 0, op1 = const0_rtx;
9837               code = GE;
9838             }
9839           break;
9840
9841         case GEU:
9842           /* >= C is equivalent to < (C - 1).  */
9843           if (const_op > 1)
9844             {
9845               const_op -= 1;
9846               op1 = GEN_INT (const_op);
9847               code = GTU;
9848               /* ... fall through ...  */
9849             }
9850
9851           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
9852           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9853                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9854             {
9855               const_op = 0, op1 = const0_rtx;
9856               code = LT;
9857               break;
9858             }
9859           else
9860             break;
9861
9862         case GTU:
9863           /* unsigned > 0 is equivalent to != 0 */
9864           if (const_op == 0)
9865             code = NE;
9866
9867           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
9868           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9869                     && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9870             {
9871               const_op = 0, op1 = const0_rtx;
9872               code = LT;
9873             }
9874           break;
9875
9876         default:
9877           break;
9878         }
9879
9880       /* Compute some predicates to simplify code below.  */
9881
9882       equality_comparison_p = (code == EQ || code == NE);
9883       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9884       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9885                                || code == LEU);
9886
9887       /* If this is a sign bit comparison and we can do arithmetic in
9888          MODE, say that we will only be needing the sign bit of OP0.  */
9889       if (sign_bit_comparison_p
9890           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9891         op0 = force_to_mode (op0, mode,
9892                              ((HOST_WIDE_INT) 1
9893                               << (GET_MODE_BITSIZE (mode) - 1)),
9894                              NULL_RTX, 0);
9895
9896       /* Now try cases based on the opcode of OP0.  If none of the cases
9897          does a "continue", we exit this loop immediately after the
9898          switch.  */
9899
9900       switch (GET_CODE (op0))
9901         {
9902         case ZERO_EXTRACT:
9903           /* If we are extracting a single bit from a variable position in
9904              a constant that has only a single bit set and are comparing it
9905              with zero, we can convert this into an equality comparison 
9906              between the position and the location of the single bit.  */
9907
9908           if (GET_CODE (XEXP (op0, 0)) == CONST_INT
9909               && XEXP (op0, 1) == const1_rtx
9910               && equality_comparison_p && const_op == 0
9911               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9912             {
9913               if (BITS_BIG_ENDIAN)
9914                 {
9915 #ifdef HAVE_extzv
9916                   mode = insn_data[(int) CODE_FOR_extzv].operand[1].mode;
9917                   if (mode == VOIDmode)
9918                     mode = word_mode;
9919                   i = (GET_MODE_BITSIZE (mode) - 1 - i);
9920 #else
9921                   i = BITS_PER_WORD - 1 - i;
9922 #endif
9923                 }
9924
9925               op0 = XEXP (op0, 2);
9926               op1 = GEN_INT (i);
9927               const_op = i;
9928
9929               /* Result is nonzero iff shift count is equal to I.  */
9930               code = reverse_condition (code);
9931               continue;
9932             }
9933
9934           /* ... fall through ...  */
9935
9936         case SIGN_EXTRACT:
9937           tem = expand_compound_operation (op0);
9938           if (tem != op0)
9939             {
9940               op0 = tem;
9941               continue;
9942             }
9943           break;
9944
9945         case NOT:
9946           /* If testing for equality, we can take the NOT of the constant.  */
9947           if (equality_comparison_p
9948               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9949             {
9950               op0 = XEXP (op0, 0);
9951               op1 = tem;
9952               continue;
9953             }
9954
9955           /* If just looking at the sign bit, reverse the sense of the
9956              comparison.  */
9957           if (sign_bit_comparison_p)
9958             {
9959               op0 = XEXP (op0, 0);
9960               code = (code == GE ? LT : GE);
9961               continue;
9962             }
9963           break;
9964
9965         case NEG:
9966           /* If testing for equality, we can take the NEG of the constant.  */
9967           if (equality_comparison_p
9968               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9969             {
9970               op0 = XEXP (op0, 0);
9971               op1 = tem;
9972               continue;
9973             }
9974
9975           /* The remaining cases only apply to comparisons with zero.  */
9976           if (const_op != 0)
9977             break;
9978
9979           /* When X is ABS or is known positive,
9980              (neg X) is < 0 if and only if X != 0.  */
9981
9982           if (sign_bit_comparison_p
9983               && (GET_CODE (XEXP (op0, 0)) == ABS
9984                   || (mode_width <= HOST_BITS_PER_WIDE_INT
9985                       && (nonzero_bits (XEXP (op0, 0), mode)
9986                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
9987             {
9988               op0 = XEXP (op0, 0);
9989               code = (code == LT ? NE : EQ);
9990               continue;
9991             }
9992
9993           /* If we have NEG of something whose two high-order bits are the
9994              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
9995           if (num_sign_bit_copies (op0, mode) >= 2)
9996             {
9997               op0 = XEXP (op0, 0);
9998               code = swap_condition (code);
9999               continue;
10000             }
10001           break;
10002
10003         case ROTATE:
10004           /* If we are testing equality and our count is a constant, we
10005              can perform the inverse operation on our RHS.  */
10006           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10007               && (tem = simplify_binary_operation (ROTATERT, mode,
10008                                                    op1, XEXP (op0, 1))) != 0)
10009             {
10010               op0 = XEXP (op0, 0);
10011               op1 = tem;
10012               continue;
10013             }
10014
10015           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10016              a particular bit.  Convert it to an AND of a constant of that
10017              bit.  This will be converted into a ZERO_EXTRACT.  */
10018           if (const_op == 0 && sign_bit_comparison_p
10019               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10020               && mode_width <= HOST_BITS_PER_WIDE_INT)
10021             {
10022               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10023                                             ((HOST_WIDE_INT) 1
10024                                              << (mode_width - 1
10025                                                  - INTVAL (XEXP (op0, 1)))));
10026               code = (code == LT ? NE : EQ);
10027               continue;
10028             }
10029
10030           /* ... fall through ...  */
10031
10032         case ABS:
10033           /* ABS is ignorable inside an equality comparison with zero.  */
10034           if (const_op == 0 && equality_comparison_p)
10035             {
10036               op0 = XEXP (op0, 0);
10037               continue;
10038             }
10039           break;
10040           
10041
10042         case SIGN_EXTEND:
10043           /* Can simplify (compare (zero/sign_extend FOO) CONST)
10044              to (compare FOO CONST) if CONST fits in FOO's mode and we 
10045              are either testing inequality or have an unsigned comparison
10046              with ZERO_EXTEND or a signed comparison with SIGN_EXTEND.  */
10047           if (! unsigned_comparison_p
10048               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10049                   <= HOST_BITS_PER_WIDE_INT)
10050               && ((unsigned HOST_WIDE_INT) const_op
10051                   < (((unsigned HOST_WIDE_INT) 1
10052                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10053             {
10054               op0 = XEXP (op0, 0);
10055               continue;
10056             }
10057           break;
10058
10059         case SUBREG:
10060           /* Check for the case where we are comparing A - C1 with C2,
10061              both constants are smaller than 1/2 the maximum positive
10062              value in MODE, and the comparison is equality or unsigned.
10063              In that case, if A is either zero-extended to MODE or has
10064              sufficient sign bits so that the high-order bit in MODE
10065              is a copy of the sign in the inner mode, we can prove that it is
10066              safe to do the operation in the wider mode.  This simplifies
10067              many range checks.  */
10068
10069           if (mode_width <= HOST_BITS_PER_WIDE_INT
10070               && subreg_lowpart_p (op0)
10071               && GET_CODE (SUBREG_REG (op0)) == PLUS
10072               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10073               && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10074               && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
10075                   < (HOST_WIDE_INT)(GET_MODE_MASK (mode) / 2))
10076               && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10077               && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10078                                       GET_MODE (SUBREG_REG (op0)))
10079                         & ~ GET_MODE_MASK (mode))
10080                   || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10081                                            GET_MODE (SUBREG_REG (op0)))
10082                       > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10083                          - GET_MODE_BITSIZE (mode)))))
10084             {
10085               op0 = SUBREG_REG (op0);
10086               continue;
10087             }
10088
10089           /* If the inner mode is narrower and we are extracting the low part,
10090              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10091           if (subreg_lowpart_p (op0)
10092               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10093             /* Fall through */ ;
10094           else
10095             break;
10096
10097           /* ... fall through ...  */
10098
10099         case ZERO_EXTEND:
10100           if ((unsigned_comparison_p || equality_comparison_p)
10101               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10102                   <= HOST_BITS_PER_WIDE_INT)
10103               && ((unsigned HOST_WIDE_INT) const_op
10104                   < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10105             {
10106               op0 = XEXP (op0, 0);
10107               continue;
10108             }
10109           break;
10110
10111         case PLUS:
10112           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10113              this for equality comparisons due to pathological cases involving
10114              overflows.  */
10115           if (equality_comparison_p
10116               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10117                                                         op1, XEXP (op0, 1))))
10118             {
10119               op0 = XEXP (op0, 0);
10120               op1 = tem;
10121               continue;
10122             }
10123
10124           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10125           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10126               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10127             {
10128               op0 = XEXP (XEXP (op0, 0), 0);
10129               code = (code == LT ? EQ : NE);
10130               continue;
10131             }
10132           break;
10133
10134         case MINUS:
10135           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10136              (eq B (minus A C)), whichever simplifies.  We can only do
10137              this for equality comparisons due to pathological cases involving
10138              overflows.  */
10139           if (equality_comparison_p
10140               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10141                                                         XEXP (op0, 1), op1)))
10142             {
10143               op0 = XEXP (op0, 0);
10144               op1 = tem;
10145               continue;
10146             }
10147
10148           if (equality_comparison_p
10149               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10150                                                         XEXP (op0, 0), op1)))
10151             {
10152               op0 = XEXP (op0, 1);
10153               op1 = tem;
10154               continue;
10155             }
10156
10157           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10158              of bits in X minus 1, is one iff X > 0.  */
10159           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10160               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10161               && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
10162               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10163             {
10164               op0 = XEXP (op0, 1);
10165               code = (code == GE ? LE : GT);
10166               continue;
10167             }
10168           break;
10169
10170         case XOR:
10171           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10172              if C is zero or B is a constant.  */
10173           if (equality_comparison_p
10174               && 0 != (tem = simplify_binary_operation (XOR, mode,
10175                                                         XEXP (op0, 1), op1)))
10176             {
10177               op0 = XEXP (op0, 0);
10178               op1 = tem;
10179               continue;
10180             }
10181           break;
10182
10183         case EQ:  case NE:
10184         case LT:  case LTU:  case LE:  case LEU:
10185         case GT:  case GTU:  case GE:  case GEU:
10186           /* We can't do anything if OP0 is a condition code value, rather
10187              than an actual data value.  */
10188           if (const_op != 0
10189 #ifdef HAVE_cc0
10190               || XEXP (op0, 0) == cc0_rtx
10191 #endif
10192               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10193             break;
10194
10195           /* Get the two operands being compared.  */
10196           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10197             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10198           else
10199             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10200
10201           /* Check for the cases where we simply want the result of the
10202              earlier test or the opposite of that result.  */
10203           if (code == NE
10204               || (code == EQ && reversible_comparison_p (op0))
10205               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10206                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10207                   && (STORE_FLAG_VALUE
10208                       & (((HOST_WIDE_INT) 1
10209                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10210                   && (code == LT
10211                       || (code == GE && reversible_comparison_p (op0)))))
10212             {
10213               code = (code == LT || code == NE
10214                       ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
10215               op0 = tem, op1 = tem1;
10216               continue;
10217             }
10218           break;
10219
10220         case IOR:
10221           /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
10222              iff X <= 0.  */
10223           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10224               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10225               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10226             {
10227               op0 = XEXP (op0, 1);
10228               code = (code == GE ? GT : LE);
10229               continue;
10230             }
10231           break;
10232
10233         case AND:
10234           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10235              will be converted to a ZERO_EXTRACT later.  */
10236           if (const_op == 0 && equality_comparison_p
10237               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10238               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10239             {
10240               op0 = simplify_and_const_int
10241                 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
10242                                              XEXP (op0, 1),
10243                                              XEXP (XEXP (op0, 0), 1)),
10244                  (HOST_WIDE_INT) 1);
10245               continue;
10246             }
10247
10248           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10249              zero and X is a comparison and C1 and C2 describe only bits set
10250              in STORE_FLAG_VALUE, we can compare with X.  */
10251           if (const_op == 0 && equality_comparison_p
10252               && mode_width <= HOST_BITS_PER_WIDE_INT
10253               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10254               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10255               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10256               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10257               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10258             {
10259               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10260                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10261               if ((~ STORE_FLAG_VALUE & mask) == 0
10262                   && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10263                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10264                           && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10265                 {
10266                   op0 = XEXP (XEXP (op0, 0), 0);
10267                   continue;
10268                 }
10269             }
10270
10271           /* If we are doing an equality comparison of an AND of a bit equal
10272              to the sign bit, replace this with a LT or GE comparison of
10273              the underlying value.  */
10274           if (equality_comparison_p
10275               && const_op == 0
10276               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10277               && mode_width <= HOST_BITS_PER_WIDE_INT
10278               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10279                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10280             {
10281               op0 = XEXP (op0, 0);
10282               code = (code == EQ ? GE : LT);
10283               continue;
10284             }
10285
10286           /* If this AND operation is really a ZERO_EXTEND from a narrower
10287              mode, the constant fits within that mode, and this is either an
10288              equality or unsigned comparison, try to do this comparison in
10289              the narrower mode.  */
10290           if ((equality_comparison_p || unsigned_comparison_p)
10291               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10292               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10293                                    & GET_MODE_MASK (mode))
10294                                   + 1)) >= 0
10295               && const_op >> i == 0
10296               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10297             {
10298               op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
10299               continue;
10300             }
10301
10302           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1 fits
10303              in both M1 and M2 and the SUBREG is either paradoxical or
10304              represents the low part, permute the SUBREG and the AND and
10305              try again.  */
10306           if (GET_CODE (XEXP (op0, 0)) == SUBREG
10307               && (0
10308 #ifdef WORD_REGISTER_OPERATIONS
10309                   || ((mode_width
10310                        > (GET_MODE_BITSIZE
10311                            (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10312                       && mode_width <= BITS_PER_WORD)
10313 #endif
10314                   || ((mode_width
10315                        <= (GET_MODE_BITSIZE
10316                            (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10317                       && subreg_lowpart_p (XEXP (op0, 0))))
10318 #ifndef WORD_REGISTER_OPERATIONS
10319               /* It is unsafe to commute the AND into the SUBREG if the SUBREG
10320                  is paradoxical and WORD_REGISTER_OPERATIONS is not defined.
10321                  As originally written the upper bits have a defined value
10322                  due to the AND operation.  However, if we commute the AND
10323                  inside the SUBREG then they no longer have defined values
10324                  and the meaning of the code has been changed.  */
10325               && (GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)))
10326                   <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0)))))
10327 #endif
10328               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10329               && mode_width <= HOST_BITS_PER_WIDE_INT
10330               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10331                   <= HOST_BITS_PER_WIDE_INT)
10332               && (INTVAL (XEXP (op0, 1)) & ~ mask) == 0
10333               && 0 == (~ GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10334                        & INTVAL (XEXP (op0, 1)))
10335               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1)) != mask
10336               && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10337                   != GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10338                        
10339             {
10340               op0
10341                 = gen_lowpart_for_combine
10342                   (mode,
10343                    gen_binary (AND, GET_MODE (SUBREG_REG (XEXP (op0, 0))),
10344                                SUBREG_REG (XEXP (op0, 0)), XEXP (op0, 1)));
10345               continue;
10346             }
10347
10348           break;
10349
10350         case ASHIFT:
10351           /* If we have (compare (ashift FOO N) (const_int C)) and
10352              the high order N bits of FOO (N+1 if an inequality comparison)
10353              are known to be zero, we can do this by comparing FOO with C
10354              shifted right N bits so long as the low-order N bits of C are
10355              zero.  */
10356           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10357               && INTVAL (XEXP (op0, 1)) >= 0
10358               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10359                   < HOST_BITS_PER_WIDE_INT)
10360               && ((const_op
10361                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10362               && mode_width <= HOST_BITS_PER_WIDE_INT
10363               && (nonzero_bits (XEXP (op0, 0), mode)
10364                   & ~ (mask >> (INTVAL (XEXP (op0, 1))
10365                                 + ! equality_comparison_p))) == 0)
10366             {
10367               /* We must perform a logical shift, not an arithmetic one,
10368                  as we want the top N bits of C to be zero.  */
10369               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10370               
10371               temp >>= INTVAL (XEXP (op0, 1));
10372               op1 = GEN_INT (trunc_int_for_mode (temp, mode));
10373               op0 = XEXP (op0, 0);
10374               continue;
10375             }
10376
10377           /* If we are doing a sign bit comparison, it means we are testing
10378              a particular bit.  Convert it to the appropriate AND.  */
10379           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10380               && mode_width <= HOST_BITS_PER_WIDE_INT)
10381             {
10382               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10383                                             ((HOST_WIDE_INT) 1
10384                                              << (mode_width - 1
10385                                                  - INTVAL (XEXP (op0, 1)))));
10386               code = (code == LT ? NE : EQ);
10387               continue;
10388             }
10389
10390           /* If this an equality comparison with zero and we are shifting
10391              the low bit to the sign bit, we can convert this to an AND of the
10392              low-order bit.  */
10393           if (const_op == 0 && equality_comparison_p
10394               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10395               && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10396             {
10397               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10398                                             (HOST_WIDE_INT) 1);
10399               continue;
10400             }
10401           break;
10402
10403         case ASHIFTRT:
10404           /* If this is an equality comparison with zero, we can do this
10405              as a logical shift, which might be much simpler.  */
10406           if (equality_comparison_p && const_op == 0
10407               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10408             {
10409               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10410                                           XEXP (op0, 0),
10411                                           INTVAL (XEXP (op0, 1)));
10412               continue;
10413             }
10414
10415           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10416              do the comparison in a narrower mode.  */
10417           if (! unsigned_comparison_p
10418               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10419               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10420               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10421               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10422                                          MODE_INT, 1)) != BLKmode
10423               && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10424                   || ((unsigned HOST_WIDE_INT) - const_op
10425                       <= GET_MODE_MASK (tmode))))
10426             {
10427               op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
10428               continue;
10429             }
10430
10431           /* ... fall through ...  */
10432         case LSHIFTRT:
10433           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10434              the low order N bits of FOO are known to be zero, we can do this
10435              by comparing FOO with C shifted left N bits so long as no
10436              overflow occurs.  */
10437           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10438               && INTVAL (XEXP (op0, 1)) >= 0
10439               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10440               && mode_width <= HOST_BITS_PER_WIDE_INT
10441               && (nonzero_bits (XEXP (op0, 0), mode)
10442                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10443               && (const_op == 0
10444                   || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
10445                       < mode_width)))
10446             {
10447               const_op <<= INTVAL (XEXP (op0, 1));
10448               op1 = GEN_INT (const_op);
10449               op0 = XEXP (op0, 0);
10450               continue;
10451             }
10452
10453           /* If we are using this shift to extract just the sign bit, we
10454              can replace this with an LT or GE comparison.  */
10455           if (const_op == 0
10456               && (equality_comparison_p || sign_bit_comparison_p)
10457               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10458               && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10459             {
10460               op0 = XEXP (op0, 0);
10461               code = (code == NE || code == GT ? LT : GE);
10462               continue;
10463             }
10464           break;
10465           
10466         default:
10467           break;
10468         }
10469
10470       break;
10471     }
10472
10473   /* Now make any compound operations involved in this comparison.  Then,
10474      check for an outmost SUBREG on OP0 that is not doing anything or is
10475      paradoxical.  The latter case can only occur when it is known that the
10476      "extra" bits will be zero.  Therefore, it is safe to remove the SUBREG.
10477      We can never remove a SUBREG for a non-equality comparison because the
10478      sign bit is in a different place in the underlying object.  */
10479
10480   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10481   op1 = make_compound_operation (op1, SET);
10482
10483   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10484       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10485       && (code == NE || code == EQ)
10486       && ((GET_MODE_SIZE (GET_MODE (op0))
10487            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
10488     {
10489       op0 = SUBREG_REG (op0);
10490       op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
10491     }
10492
10493   else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10494            && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10495            && (code == NE || code == EQ)
10496            && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10497                <= HOST_BITS_PER_WIDE_INT)
10498            && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
10499                & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
10500            && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
10501                                               op1),
10502                (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10503                 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
10504     op0 = SUBREG_REG (op0), op1 = tem;
10505
10506   /* We now do the opposite procedure: Some machines don't have compare
10507      insns in all modes.  If OP0's mode is an integer mode smaller than a
10508      word and we can't do a compare in that mode, see if there is a larger
10509      mode for which we can do the compare.  There are a number of cases in
10510      which we can use the wider mode.  */
10511
10512   mode = GET_MODE (op0);
10513   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10514       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10515       && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
10516     for (tmode = GET_MODE_WIDER_MODE (mode);
10517          (tmode != VOIDmode
10518           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10519          tmode = GET_MODE_WIDER_MODE (tmode))
10520       if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
10521         {
10522           /* If the only nonzero bits in OP0 and OP1 are those in the
10523              narrower mode and this is an equality or unsigned comparison,
10524              we can use the wider mode.  Similarly for sign-extended
10525              values, in which case it is true for all comparisons.  */
10526           if (((code == EQ || code == NE
10527                 || code == GEU || code == GTU || code == LEU || code == LTU)
10528                && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
10529                && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
10530               || ((num_sign_bit_copies (op0, tmode)
10531                    > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
10532                   && (num_sign_bit_copies (op1, tmode)
10533                       > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
10534             {
10535               op0 = gen_lowpart_for_combine (tmode, op0);
10536               op1 = gen_lowpart_for_combine (tmode, op1);
10537               break;
10538             }
10539
10540           /* If this is a test for negative, we can make an explicit
10541              test of the sign bit.  */
10542
10543           if (op1 == const0_rtx && (code == LT || code == GE)
10544               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10545             {
10546               op0 = gen_binary (AND, tmode,
10547                                 gen_lowpart_for_combine (tmode, op0),
10548                                 GEN_INT ((HOST_WIDE_INT) 1
10549                                          << (GET_MODE_BITSIZE (mode) - 1)));
10550               code = (code == LT) ? NE : EQ;
10551               break;
10552             }
10553         }
10554
10555 #ifdef CANONICALIZE_COMPARISON
10556   /* If this machine only supports a subset of valid comparisons, see if we
10557      can convert an unsupported one into a supported one.  */
10558   CANONICALIZE_COMPARISON (code, op0, op1);
10559 #endif
10560
10561   *pop0 = op0;
10562   *pop1 = op1;
10563
10564   return code;
10565 }
10566 \f
10567 /* Return 1 if we know that X, a comparison operation, is not operating
10568    on a floating-point value or is EQ or NE, meaning that we can safely
10569    reverse it.  */
10570
10571 static int
10572 reversible_comparison_p (x)
10573      rtx x;
10574 {
10575   if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
10576       || flag_fast_math
10577       || GET_CODE (x) == NE || GET_CODE (x) == EQ)
10578     return 1;
10579
10580   switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
10581     {
10582     case MODE_INT:
10583     case MODE_PARTIAL_INT:
10584     case MODE_COMPLEX_INT:
10585       return 1;
10586
10587     case MODE_CC:
10588       /* If the mode of the condition codes tells us that this is safe,
10589          we need look no further.  */
10590       if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
10591         return 1;
10592
10593       /* Otherwise try and find where the condition codes were last set and
10594          use that.  */
10595       x = get_last_value (XEXP (x, 0));
10596       return (x && GET_CODE (x) == COMPARE
10597               && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
10598       
10599     default:
10600       return 0;
10601     }
10602 }
10603 \f
10604 /* Utility function for following routine.  Called when X is part of a value
10605    being stored into reg_last_set_value.  Sets reg_last_set_table_tick
10606    for each register mentioned.  Similar to mention_regs in cse.c  */
10607
10608 static void
10609 update_table_tick (x)
10610      rtx x;
10611 {
10612   register enum rtx_code code = GET_CODE (x);
10613   register const char *fmt = GET_RTX_FORMAT (code);
10614   register int i;
10615
10616   if (code == REG)
10617     {
10618       int regno = REGNO (x);
10619       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10620                               ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10621
10622       for (i = regno; i < endregno; i++)
10623         reg_last_set_table_tick[i] = label_tick;
10624
10625       return;
10626     }
10627   
10628   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10629     /* Note that we can't have an "E" in values stored; see
10630        get_last_value_validate.  */
10631     if (fmt[i] == 'e')
10632       update_table_tick (XEXP (x, i));
10633 }
10634
10635 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
10636    are saying that the register is clobbered and we no longer know its
10637    value.  If INSN is zero, don't update reg_last_set; this is only permitted
10638    with VALUE also zero and is used to invalidate the register.  */
10639
10640 static void
10641 record_value_for_reg (reg, insn, value)
10642      rtx reg;
10643      rtx insn;
10644      rtx value;
10645 {
10646   int regno = REGNO (reg);
10647   int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10648                           ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
10649   int i;
10650
10651   /* If VALUE contains REG and we have a previous value for REG, substitute
10652      the previous value.  */
10653   if (value && insn && reg_overlap_mentioned_p (reg, value))
10654     {
10655       rtx tem;
10656
10657       /* Set things up so get_last_value is allowed to see anything set up to
10658          our insn.  */
10659       subst_low_cuid = INSN_CUID (insn);
10660       tem = get_last_value (reg);      
10661
10662       if (tem)
10663         value = replace_rtx (copy_rtx (value), reg, tem);
10664     }
10665
10666   /* For each register modified, show we don't know its value, that
10667      we don't know about its bitwise content, that its value has been
10668      updated, and that we don't know the location of the death of the
10669      register.  */
10670   for (i = regno; i < endregno; i ++)
10671     {
10672       if (insn)
10673         reg_last_set[i] = insn;
10674       reg_last_set_value[i] = 0;
10675       reg_last_set_mode[i] = 0;
10676       reg_last_set_nonzero_bits[i] = 0;
10677       reg_last_set_sign_bit_copies[i] = 0;
10678       reg_last_death[i] = 0;
10679     }
10680
10681   /* Mark registers that are being referenced in this value.  */
10682   if (value)
10683     update_table_tick (value);
10684
10685   /* Now update the status of each register being set.
10686      If someone is using this register in this block, set this register
10687      to invalid since we will get confused between the two lives in this
10688      basic block.  This makes using this register always invalid.  In cse, we
10689      scan the table to invalidate all entries using this register, but this
10690      is too much work for us.  */
10691
10692   for (i = regno; i < endregno; i++)
10693     {
10694       reg_last_set_label[i] = label_tick;
10695       if (value && reg_last_set_table_tick[i] == label_tick)
10696         reg_last_set_invalid[i] = 1;
10697       else
10698         reg_last_set_invalid[i] = 0;
10699     }
10700
10701   /* The value being assigned might refer to X (like in "x++;").  In that
10702      case, we must replace it with (clobber (const_int 0)) to prevent
10703      infinite loops.  */
10704   if (value && ! get_last_value_validate (&value, insn,
10705                                           reg_last_set_label[regno], 0))
10706     {
10707       value = copy_rtx (value);
10708       if (! get_last_value_validate (&value, insn,
10709                                      reg_last_set_label[regno], 1))
10710         value = 0;
10711     }
10712
10713   /* For the main register being modified, update the value, the mode, the
10714      nonzero bits, and the number of sign bit copies.  */
10715
10716   reg_last_set_value[regno] = value;
10717
10718   if (value)
10719     {
10720       subst_low_cuid = INSN_CUID (insn);
10721       reg_last_set_mode[regno] = GET_MODE (reg);
10722       reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
10723       reg_last_set_sign_bit_copies[regno]
10724         = num_sign_bit_copies (value, GET_MODE (reg));
10725     }
10726 }
10727
10728 /* Used for communication between the following two routines.  */
10729 static rtx record_dead_insn;
10730
10731 /* Called via note_stores from record_dead_and_set_regs to handle one
10732    SET or CLOBBER in an insn.  */
10733
10734 static void
10735 record_dead_and_set_regs_1 (dest, setter)
10736      rtx dest, setter;
10737 {
10738   if (GET_CODE (dest) == SUBREG)
10739     dest = SUBREG_REG (dest);
10740
10741   if (GET_CODE (dest) == REG)
10742     {
10743       /* If we are setting the whole register, we know its value.  Otherwise
10744          show that we don't know the value.  We can handle SUBREG in
10745          some cases.  */
10746       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10747         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10748       else if (GET_CODE (setter) == SET
10749                && GET_CODE (SET_DEST (setter)) == SUBREG
10750                && SUBREG_REG (SET_DEST (setter)) == dest
10751                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10752                && subreg_lowpart_p (SET_DEST (setter)))
10753         record_value_for_reg (dest, record_dead_insn,
10754                               gen_lowpart_for_combine (GET_MODE (dest),
10755                                                        SET_SRC (setter)));
10756       else
10757         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10758     }
10759   else if (GET_CODE (dest) == MEM
10760            /* Ignore pushes, they clobber nothing.  */
10761            && ! push_operand (dest, GET_MODE (dest)))
10762     mem_last_set = INSN_CUID (record_dead_insn);
10763 }
10764
10765 /* Update the records of when each REG was most recently set or killed
10766    for the things done by INSN.  This is the last thing done in processing
10767    INSN in the combiner loop.
10768
10769    We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10770    reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10771    and also the similar information mem_last_set (which insn most recently
10772    modified memory) and last_call_cuid (which insn was the most recent
10773    subroutine call).  */
10774
10775 static void
10776 record_dead_and_set_regs (insn)
10777      rtx insn;
10778 {
10779   register rtx link;
10780   int i;
10781
10782   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10783     {
10784       if (REG_NOTE_KIND (link) == REG_DEAD
10785           && GET_CODE (XEXP (link, 0)) == REG)
10786         {
10787           int regno = REGNO (XEXP (link, 0));
10788           int endregno
10789             = regno + (regno < FIRST_PSEUDO_REGISTER
10790                        ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10791                        : 1);
10792
10793           for (i = regno; i < endregno; i++)
10794             reg_last_death[i] = insn;
10795         }
10796       else if (REG_NOTE_KIND (link) == REG_INC)
10797         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10798     }
10799
10800   if (GET_CODE (insn) == CALL_INSN)
10801     {
10802       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10803         if (call_used_regs[i])
10804           {
10805             reg_last_set_value[i] = 0;
10806             reg_last_set_mode[i] = 0;
10807             reg_last_set_nonzero_bits[i] = 0;
10808             reg_last_set_sign_bit_copies[i] = 0;
10809             reg_last_death[i] = 0;
10810           }
10811
10812       last_call_cuid = mem_last_set = INSN_CUID (insn);
10813     }
10814
10815   record_dead_insn = insn;
10816   note_stores (PATTERN (insn), record_dead_and_set_regs_1);
10817 }
10818 \f
10819 /* Utility routine for the following function.  Verify that all the registers
10820    mentioned in *LOC are valid when *LOC was part of a value set when
10821    label_tick == TICK.  Return 0 if some are not.
10822
10823    If REPLACE is non-zero, replace the invalid reference with
10824    (clobber (const_int 0)) and return 1.  This replacement is useful because
10825    we often can get useful information about the form of a value (e.g., if
10826    it was produced by a shift that always produces -1 or 0) even though
10827    we don't know exactly what registers it was produced from.  */
10828
10829 static int
10830 get_last_value_validate (loc, insn, tick, replace)
10831      rtx *loc;
10832      rtx insn;
10833      int tick;
10834      int replace;
10835 {
10836   rtx x = *loc;
10837   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10838   int len = GET_RTX_LENGTH (GET_CODE (x));
10839   int i;
10840
10841   if (GET_CODE (x) == REG)
10842     {
10843       int regno = REGNO (x);
10844       int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10845                               ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10846       int j;
10847
10848       for (j = regno; j < endregno; j++)
10849         if (reg_last_set_invalid[j]
10850             /* If this is a pseudo-register that was only set once and not
10851                live at the beginning of the function, it is always valid.  */
10852             || (! (regno >= FIRST_PSEUDO_REGISTER 
10853                    && REG_N_SETS (regno) == 1
10854                    && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
10855                 && reg_last_set_label[j] > tick))
10856           {
10857             if (replace)
10858               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10859             return replace;
10860           }
10861
10862       return 1;
10863     }
10864   /* If this is a memory reference, make sure that there were
10865      no stores after it that might have clobbered the value.  We don't
10866      have alias info, so we assume any store invalidates it.  */
10867   else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
10868            && INSN_CUID (insn) <= mem_last_set)
10869     {
10870       if (replace)
10871         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10872       return replace;
10873     }
10874
10875   for (i = 0; i < len; i++)
10876     if ((fmt[i] == 'e'
10877          && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
10878         /* Don't bother with these.  They shouldn't occur anyway.  */
10879         || fmt[i] == 'E')
10880       return 0;
10881
10882   /* If we haven't found a reason for it to be invalid, it is valid.  */
10883   return 1;
10884 }
10885
10886 /* Get the last value assigned to X, if known.  Some registers
10887    in the value may be replaced with (clobber (const_int 0)) if their value
10888    is known longer known reliably.  */
10889
10890 static rtx
10891 get_last_value (x)
10892      rtx x;
10893 {
10894   int regno;
10895   rtx value;
10896
10897   /* If this is a non-paradoxical SUBREG, get the value of its operand and
10898      then convert it to the desired mode.  If this is a paradoxical SUBREG,
10899      we cannot predict what values the "extra" bits might have.  */
10900   if (GET_CODE (x) == SUBREG
10901       && subreg_lowpart_p (x)
10902       && (GET_MODE_SIZE (GET_MODE (x))
10903           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
10904       && (value = get_last_value (SUBREG_REG (x))) != 0)
10905     return gen_lowpart_for_combine (GET_MODE (x), value);
10906
10907   if (GET_CODE (x) != REG)
10908     return 0;
10909
10910   regno = REGNO (x);
10911   value = reg_last_set_value[regno];
10912
10913   /* If we don't have a value, or if it isn't for this basic block and
10914      it's either a hard register, set more than once, or it's a live
10915      at the beginning of the function, return 0.  
10916
10917      Because if it's not live at the beginnning of the function then the reg 
10918      is always set before being used (is never used without being set).
10919      And, if it's set only once, and it's always set before use, then all
10920      uses must have the same last value, even if it's not from this basic
10921      block.  */
10922
10923   if (value == 0
10924       || (reg_last_set_label[regno] != label_tick
10925           && (regno < FIRST_PSEUDO_REGISTER
10926               || REG_N_SETS (regno) != 1
10927               || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))))
10928     return 0;
10929
10930   /* If the value was set in a later insn than the ones we are processing,
10931      we can't use it even if the register was only set once, but make a quick
10932      check to see if the previous insn set it to something.  This is commonly
10933      the case when the same pseudo is used by repeated insns.
10934
10935      This does not work if there exists an instruction which is temporarily
10936      not on the insn chain.  */
10937
10938   if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
10939     {
10940       rtx insn, set;
10941
10942       /* We can't do anything if the value is set in between the insns we are
10943          processing.  */
10944       if (INSN_CUID (reg_last_set[regno]) <= INSN_CUID (subst_insn))
10945         return 0;
10946
10947       /* We can not do anything useful in this case, because there is
10948          an instruction which is not on the insn chain.  */
10949       if (subst_prev_insn)
10950         return 0;
10951
10952       /* Skip over USE insns.  They are not useful here, and they may have
10953          been made by combine, in which case they do not have a INSN_CUID
10954          value.  We can't use prev_real_insn, because that would incorrectly
10955          take us backwards across labels.  Skip over BARRIERs also, since
10956          they could have been made by combine.  If we see one, we must be
10957          optimizing dead code, so it doesn't matter what we do.  */
10958       for (insn = prev_nonnote_insn (subst_insn);
10959            insn && ((GET_CODE (insn) == INSN
10960                      && GET_CODE (PATTERN (insn)) == USE)
10961                     || GET_CODE (insn) == BARRIER
10962                     || INSN_CUID (insn) >= subst_low_cuid);
10963            insn = prev_nonnote_insn (insn))
10964         ;
10965
10966       if (insn
10967           && (set = single_set (insn)) != 0
10968           && rtx_equal_p (SET_DEST (set), x))
10969         {
10970           value = SET_SRC (set);
10971
10972           /* Make sure that VALUE doesn't reference X.  Replace any
10973              explicit references with a CLOBBER.  If there are any remaining
10974              references (rare), don't use the value.  */
10975
10976           if (reg_mentioned_p (x, value))
10977             value = replace_rtx (copy_rtx (value), x,
10978                                  gen_rtx_CLOBBER (GET_MODE (x), const0_rtx));
10979
10980           if (reg_overlap_mentioned_p (x, value))
10981             return 0;
10982         }
10983       else
10984         return 0;
10985     }
10986
10987   /* If the value has all its registers valid, return it.  */
10988   if (get_last_value_validate (&value, reg_last_set[regno],
10989                                reg_last_set_label[regno], 0))
10990     return value;
10991
10992   /* Otherwise, make a copy and replace any invalid register with
10993      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
10994
10995   value = copy_rtx (value);
10996   if (get_last_value_validate (&value, reg_last_set[regno],
10997                                reg_last_set_label[regno], 1))
10998     return value;
10999
11000   return 0;
11001 }
11002 \f
11003 /* Return nonzero if expression X refers to a REG or to memory
11004    that is set in an instruction more recent than FROM_CUID.  */
11005
11006 static int
11007 use_crosses_set_p (x, from_cuid)
11008      register rtx x;
11009      int from_cuid;
11010 {
11011   register const char *fmt;
11012   register int i;
11013   register enum rtx_code code = GET_CODE (x);
11014
11015   if (code == REG)
11016     {
11017       register int regno = REGNO (x);
11018       int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11019                             ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11020       
11021 #ifdef PUSH_ROUNDING
11022       /* Don't allow uses of the stack pointer to be moved,
11023          because we don't know whether the move crosses a push insn.  */
11024       if (regno == STACK_POINTER_REGNUM)
11025         return 1;
11026 #endif
11027       for (;regno < endreg; regno++)
11028         if (reg_last_set[regno]
11029             && INSN_CUID (reg_last_set[regno]) > from_cuid)
11030           return 1;
11031       return 0;
11032     }
11033
11034   if (code == MEM && mem_last_set > from_cuid)
11035     return 1;
11036
11037   fmt = GET_RTX_FORMAT (code);
11038
11039   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11040     {
11041       if (fmt[i] == 'E')
11042         {
11043           register int j;
11044           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11045             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11046               return 1;
11047         }
11048       else if (fmt[i] == 'e'
11049                && use_crosses_set_p (XEXP (x, i), from_cuid))
11050         return 1;
11051     }
11052   return 0;
11053 }
11054 \f
11055 /* Define three variables used for communication between the following
11056    routines.  */
11057
11058 static int reg_dead_regno, reg_dead_endregno;
11059 static int reg_dead_flag;
11060
11061 /* Function called via note_stores from reg_dead_at_p.
11062
11063    If DEST is within [reg_dead_regno, reg_dead_endregno), set 
11064    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
11065
11066 static void
11067 reg_dead_at_p_1 (dest, x)
11068      rtx dest;
11069      rtx x;
11070 {
11071   int regno, endregno;
11072
11073   if (GET_CODE (dest) != REG)
11074     return;
11075
11076   regno = REGNO (dest);
11077   endregno = regno + (regno < FIRST_PSEUDO_REGISTER 
11078                       ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
11079
11080   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11081     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11082 }
11083
11084 /* Return non-zero if REG is known to be dead at INSN.
11085
11086    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
11087    referencing REG, it is dead.  If we hit a SET referencing REG, it is
11088    live.  Otherwise, see if it is live or dead at the start of the basic
11089    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
11090    must be assumed to be always live.  */
11091
11092 static int
11093 reg_dead_at_p (reg, insn)
11094      rtx reg;
11095      rtx insn;
11096 {
11097   int block, i;
11098
11099   /* Set variables for reg_dead_at_p_1.  */
11100   reg_dead_regno = REGNO (reg);
11101   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11102                                         ? HARD_REGNO_NREGS (reg_dead_regno,
11103                                                             GET_MODE (reg))
11104                                         : 1);
11105
11106   reg_dead_flag = 0;
11107
11108   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  */
11109   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11110     {
11111       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11112         if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11113           return 0;
11114     }
11115
11116   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11117      beginning of function.  */
11118   for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
11119        insn = prev_nonnote_insn (insn))
11120     {
11121       note_stores (PATTERN (insn), reg_dead_at_p_1);
11122       if (reg_dead_flag)
11123         return reg_dead_flag == 1 ? 1 : 0;
11124
11125       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11126         return 1;
11127     }
11128
11129   /* Get the basic block number that we were in.  */
11130   if (insn == 0)
11131     block = 0;
11132   else
11133     {
11134       for (block = 0; block < n_basic_blocks; block++)
11135         if (insn == BLOCK_HEAD (block))
11136           break;
11137
11138       if (block == n_basic_blocks)
11139         return 0;
11140     }
11141
11142   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11143     if (REGNO_REG_SET_P (BASIC_BLOCK (block)->global_live_at_start, i))
11144       return 0;
11145
11146   return 1;
11147 }
11148 \f
11149 /* Note hard registers in X that are used.  This code is similar to
11150    that in flow.c, but much simpler since we don't care about pseudos.  */
11151
11152 static void
11153 mark_used_regs_combine (x)
11154      rtx x;
11155 {
11156   register RTX_CODE code = GET_CODE (x);
11157   register int regno;
11158   int i;
11159
11160   switch (code)
11161     {
11162     case LABEL_REF:
11163     case SYMBOL_REF:
11164     case CONST_INT:
11165     case CONST:
11166     case CONST_DOUBLE:
11167     case PC:
11168     case ADDR_VEC:
11169     case ADDR_DIFF_VEC:
11170     case ASM_INPUT:
11171 #ifdef HAVE_cc0
11172     /* CC0 must die in the insn after it is set, so we don't need to take
11173        special note of it here.  */
11174     case CC0:
11175 #endif
11176       return;
11177
11178     case CLOBBER:
11179       /* If we are clobbering a MEM, mark any hard registers inside the
11180          address as used.  */
11181       if (GET_CODE (XEXP (x, 0)) == MEM)
11182         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11183       return;
11184
11185     case REG:
11186       regno = REGNO (x);
11187       /* A hard reg in a wide mode may really be multiple registers.
11188          If so, mark all of them just like the first.  */
11189       if (regno < FIRST_PSEUDO_REGISTER)
11190         {
11191           /* None of this applies to the stack, frame or arg pointers */
11192           if (regno == STACK_POINTER_REGNUM
11193 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11194               || regno == HARD_FRAME_POINTER_REGNUM
11195 #endif
11196 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11197               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11198 #endif
11199               || regno == FRAME_POINTER_REGNUM)
11200             return;
11201
11202           i = HARD_REGNO_NREGS (regno, GET_MODE (x));
11203           while (i-- > 0)
11204             SET_HARD_REG_BIT (newpat_used_regs, regno + i);
11205         }
11206       return;
11207
11208     case SET:
11209       {
11210         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11211            the address.  */
11212         register rtx testreg = SET_DEST (x);
11213
11214         while (GET_CODE (testreg) == SUBREG
11215                || GET_CODE (testreg) == ZERO_EXTRACT
11216                || GET_CODE (testreg) == SIGN_EXTRACT
11217                || GET_CODE (testreg) == STRICT_LOW_PART)
11218           testreg = XEXP (testreg, 0);
11219
11220         if (GET_CODE (testreg) == MEM)
11221           mark_used_regs_combine (XEXP (testreg, 0));
11222
11223         mark_used_regs_combine (SET_SRC (x));
11224       }
11225       return;
11226
11227     default:
11228       break;
11229     }
11230
11231   /* Recursively scan the operands of this expression.  */
11232
11233   {
11234     register const char *fmt = GET_RTX_FORMAT (code);
11235
11236     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11237       {
11238         if (fmt[i] == 'e')
11239           mark_used_regs_combine (XEXP (x, i));
11240         else if (fmt[i] == 'E')
11241           {
11242             register int j;
11243
11244             for (j = 0; j < XVECLEN (x, i); j++)
11245               mark_used_regs_combine (XVECEXP (x, i, j));
11246           }
11247       }
11248   }
11249 }
11250
11251 \f
11252 /* Remove register number REGNO from the dead registers list of INSN.
11253
11254    Return the note used to record the death, if there was one.  */
11255
11256 rtx
11257 remove_death (regno, insn)
11258      int regno;
11259      rtx insn;
11260 {
11261   register rtx note = find_regno_note (insn, REG_DEAD, regno);
11262
11263   if (note)
11264     {
11265       REG_N_DEATHS (regno)--;
11266       remove_note (insn, note);
11267     }
11268
11269   return note;
11270 }
11271
11272 /* For each register (hardware or pseudo) used within expression X, if its
11273    death is in an instruction with cuid between FROM_CUID (inclusive) and
11274    TO_INSN (exclusive), put a REG_DEAD note for that register in the
11275    list headed by PNOTES. 
11276
11277    That said, don't move registers killed by maybe_kill_insn.
11278
11279    This is done when X is being merged by combination into TO_INSN.  These
11280    notes will then be distributed as needed.  */
11281
11282 static void
11283 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
11284      rtx x;
11285      rtx maybe_kill_insn;
11286      int from_cuid;
11287      rtx to_insn;
11288      rtx *pnotes;
11289 {
11290   register const char *fmt;
11291   register int len, i;
11292   register enum rtx_code code = GET_CODE (x);
11293
11294   if (code == REG)
11295     {
11296       register int regno = REGNO (x);
11297       register rtx where_dead = reg_last_death[regno];
11298       register rtx before_dead, after_dead;
11299
11300       /* Don't move the register if it gets killed in between from and to */
11301       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11302           && !reg_referenced_p (x, maybe_kill_insn))
11303         return;
11304
11305       /* WHERE_DEAD could be a USE insn made by combine, so first we
11306          make sure that we have insns with valid INSN_CUID values.  */
11307       before_dead = where_dead;
11308       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11309         before_dead = PREV_INSN (before_dead);
11310       after_dead = where_dead;
11311       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11312         after_dead = NEXT_INSN (after_dead);
11313
11314       if (before_dead && after_dead
11315           && INSN_CUID (before_dead) >= from_cuid
11316           && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11317               || (where_dead != after_dead
11318                   && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11319         {
11320           rtx note = remove_death (regno, where_dead);
11321
11322           /* It is possible for the call above to return 0.  This can occur
11323              when reg_last_death points to I2 or I1 that we combined with.
11324              In that case make a new note.
11325
11326              We must also check for the case where X is a hard register
11327              and NOTE is a death note for a range of hard registers
11328              including X.  In that case, we must put REG_DEAD notes for
11329              the remaining registers in place of NOTE.  */
11330
11331           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11332               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11333                   > GET_MODE_SIZE (GET_MODE (x))))
11334             {
11335               int deadregno = REGNO (XEXP (note, 0));
11336               int deadend
11337                 = (deadregno + HARD_REGNO_NREGS (deadregno,
11338                                                  GET_MODE (XEXP (note, 0))));
11339               int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11340               int i;
11341
11342               for (i = deadregno; i < deadend; i++)
11343                 if (i < regno || i >= ourend)
11344                   REG_NOTES (where_dead)
11345                     = gen_rtx_EXPR_LIST (REG_DEAD,
11346                                          gen_rtx_REG (reg_raw_mode[i], i),
11347                                          REG_NOTES (where_dead));
11348             }
11349           /* If we didn't find any note, or if we found a REG_DEAD note that
11350              covers only part of the given reg, and we have a multi-reg hard
11351              register, then to be safe we must check for REG_DEAD notes
11352              for each register other than the first.  They could have
11353              their own REG_DEAD notes lying around.  */
11354           else if ((note == 0
11355                     || (note != 0
11356                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11357                             < GET_MODE_SIZE (GET_MODE (x)))))
11358                    && regno < FIRST_PSEUDO_REGISTER
11359                    && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
11360             {
11361               int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11362               int i, offset;
11363               rtx oldnotes = 0;
11364
11365               if (note)
11366                 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
11367               else
11368                 offset = 1;
11369
11370               for (i = regno + offset; i < ourend; i++)
11371                 move_deaths (gen_rtx_REG (reg_raw_mode[i], i),
11372                              maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11373             }
11374
11375           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11376             {
11377               XEXP (note, 1) = *pnotes;
11378               *pnotes = note;
11379             }
11380           else
11381             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11382
11383           REG_N_DEATHS (regno)++;
11384         }
11385
11386       return;
11387     }
11388
11389   else if (GET_CODE (x) == SET)
11390     {
11391       rtx dest = SET_DEST (x);
11392
11393       move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11394
11395       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11396          that accesses one word of a multi-word item, some
11397          piece of everything register in the expression is used by
11398          this insn, so remove any old death.  */
11399
11400       if (GET_CODE (dest) == ZERO_EXTRACT
11401           || GET_CODE (dest) == STRICT_LOW_PART
11402           || (GET_CODE (dest) == SUBREG
11403               && (((GET_MODE_SIZE (GET_MODE (dest))
11404                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11405                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11406                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11407         {
11408           move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11409           return;
11410         }
11411
11412       /* If this is some other SUBREG, we know it replaces the entire
11413          value, so use that as the destination.  */
11414       if (GET_CODE (dest) == SUBREG)
11415         dest = SUBREG_REG (dest);
11416
11417       /* If this is a MEM, adjust deaths of anything used in the address.
11418          For a REG (the only other possibility), the entire value is
11419          being replaced so the old value is not used in this insn.  */
11420
11421       if (GET_CODE (dest) == MEM)
11422         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11423                      to_insn, pnotes);
11424       return;
11425     }
11426
11427   else if (GET_CODE (x) == CLOBBER)
11428     return;
11429
11430   len = GET_RTX_LENGTH (code);
11431   fmt = GET_RTX_FORMAT (code);
11432
11433   for (i = 0; i < len; i++)
11434     {
11435       if (fmt[i] == 'E')
11436         {
11437           register int j;
11438           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11439             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11440                          to_insn, pnotes);
11441         }
11442       else if (fmt[i] == 'e')
11443         move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11444     }
11445 }
11446 \f
11447 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11448    pattern of an insn.  X must be a REG.  */
11449
11450 static int
11451 reg_bitfield_target_p (x, body)
11452      rtx x;
11453      rtx body;
11454 {
11455   int i;
11456
11457   if (GET_CODE (body) == SET)
11458     {
11459       rtx dest = SET_DEST (body);
11460       rtx target;
11461       int regno, tregno, endregno, endtregno;
11462
11463       if (GET_CODE (dest) == ZERO_EXTRACT)
11464         target = XEXP (dest, 0);
11465       else if (GET_CODE (dest) == STRICT_LOW_PART)
11466         target = SUBREG_REG (XEXP (dest, 0));
11467       else
11468         return 0;
11469
11470       if (GET_CODE (target) == SUBREG)
11471         target = SUBREG_REG (target);
11472
11473       if (GET_CODE (target) != REG)
11474         return 0;
11475
11476       tregno = REGNO (target), regno = REGNO (x);
11477       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11478         return target == x;
11479
11480       endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
11481       endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11482
11483       return endregno > tregno && regno < endtregno;
11484     }
11485
11486   else if (GET_CODE (body) == PARALLEL)
11487     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11488       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11489         return 1;
11490
11491   return 0;
11492 }      
11493 \f
11494 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11495    as appropriate.  I3 and I2 are the insns resulting from the combination
11496    insns including FROM (I2 may be zero).
11497
11498    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
11499    not need REG_DEAD notes because they are being substituted for.  This
11500    saves searching in the most common cases.
11501
11502    Each note in the list is either ignored or placed on some insns, depending
11503    on the type of note.  */
11504
11505 static void
11506 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
11507      rtx notes;
11508      rtx from_insn;
11509      rtx i3, i2;
11510      rtx elim_i2, elim_i1;
11511 {
11512   rtx note, next_note;
11513   rtx tem;
11514
11515   for (note = notes; note; note = next_note)
11516     {
11517       rtx place = 0, place2 = 0;
11518
11519       /* If this NOTE references a pseudo register, ensure it references
11520          the latest copy of that register.  */
11521       if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11522           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11523         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11524
11525       next_note = XEXP (note, 1);
11526       switch (REG_NOTE_KIND (note))
11527         {
11528         case REG_BR_PROB:
11529         case REG_EXEC_COUNT:
11530           /* Doesn't matter much where we put this, as long as it's somewhere.
11531              It is preferable to keep these notes on branches, which is most
11532              likely to be i3.  */
11533           place = i3;
11534           break;
11535
11536         case REG_EH_REGION:
11537         case REG_EH_RETHROW:
11538           /* These notes must remain with the call.  It should not be
11539              possible for both I2 and I3 to be a call.  */
11540           if (GET_CODE (i3) == CALL_INSN) 
11541             place = i3;
11542           else if (i2 && GET_CODE (i2) == CALL_INSN)
11543             place = i2;
11544           else
11545             abort ();
11546           break;
11547
11548         case REG_UNUSED:
11549           /* Any clobbers for i3 may still exist, and so we must process
11550              REG_UNUSED notes from that insn.
11551
11552              Any clobbers from i2 or i1 can only exist if they were added by
11553              recog_for_combine.  In that case, recog_for_combine created the
11554              necessary REG_UNUSED notes.  Trying to keep any original
11555              REG_UNUSED notes from these insns can cause incorrect output
11556              if it is for the same register as the original i3 dest.
11557              In that case, we will notice that the register is set in i3,
11558              and then add a REG_UNUSED note for the destination of i3, which
11559              is wrong.  However, it is possible to have REG_UNUSED notes from
11560              i2 or i1 for register which were both used and clobbered, so
11561              we keep notes from i2 or i1 if they will turn into REG_DEAD
11562              notes.  */
11563
11564           /* If this register is set or clobbered in I3, put the note there
11565              unless there is one already.  */
11566           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11567             {
11568               if (from_insn != i3)
11569                 break;
11570
11571               if (! (GET_CODE (XEXP (note, 0)) == REG
11572                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11573                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11574                 place = i3;
11575             }
11576           /* Otherwise, if this register is used by I3, then this register
11577              now dies here, so we must put a REG_DEAD note here unless there
11578              is one already.  */
11579           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11580                    && ! (GET_CODE (XEXP (note, 0)) == REG
11581                          ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
11582                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11583             {
11584               PUT_REG_NOTE_KIND (note, REG_DEAD);
11585               place = i3;
11586             }
11587           break;
11588
11589         case REG_EQUAL:
11590         case REG_EQUIV:
11591         case REG_NONNEG:
11592         case REG_NOALIAS:
11593           /* These notes say something about results of an insn.  We can
11594              only support them if they used to be on I3 in which case they
11595              remain on I3.  Otherwise they are ignored.
11596
11597              If the note refers to an expression that is not a constant, we
11598              must also ignore the note since we cannot tell whether the
11599              equivalence is still true.  It might be possible to do
11600              slightly better than this (we only have a problem if I2DEST
11601              or I1DEST is present in the expression), but it doesn't
11602              seem worth the trouble.  */
11603
11604           if (from_insn == i3
11605               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11606             place = i3;
11607           break;
11608
11609         case REG_INC:
11610         case REG_NO_CONFLICT:
11611           /* These notes say something about how a register is used.  They must
11612              be present on any use of the register in I2 or I3.  */
11613           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11614             place = i3;
11615
11616           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11617             {
11618               if (place)
11619                 place2 = i2;
11620               else
11621                 place = i2;
11622             }
11623           break;
11624
11625         case REG_LABEL:
11626           /* This can show up in several ways -- either directly in the
11627              pattern, or hidden off in the constant pool with (or without?)
11628              a REG_EQUAL note.  */
11629           /* ??? Ignore the without-reg_equal-note problem for now.  */
11630           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
11631               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
11632                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11633                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
11634             place = i3;
11635
11636           if (i2
11637               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
11638                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
11639                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11640                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
11641             {
11642               if (place)
11643                 place2 = i2;
11644               else
11645                 place = i2;
11646             }
11647           break;
11648
11649         case REG_WAS_0:
11650           /* It is too much trouble to try to see if this note is still
11651              correct in all situations.  It is better to simply delete it.  */
11652           break;
11653
11654         case REG_RETVAL:
11655           /* If the insn previously containing this note still exists,
11656              put it back where it was.  Otherwise move it to the previous
11657              insn.  Adjust the corresponding REG_LIBCALL note.  */
11658           if (GET_CODE (from_insn) != NOTE)
11659             place = from_insn;
11660           else
11661             {
11662               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
11663               place = prev_real_insn (from_insn);
11664               if (tem && place)
11665                 XEXP (tem, 0) = place;
11666             }
11667           break;
11668
11669         case REG_LIBCALL:
11670           /* This is handled similarly to REG_RETVAL.  */
11671           if (GET_CODE (from_insn) != NOTE)
11672             place = from_insn;
11673           else
11674             {
11675               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
11676               place = next_real_insn (from_insn);
11677               if (tem && place)
11678                 XEXP (tem, 0) = place;
11679             }
11680           break;
11681
11682         case REG_DEAD:
11683           /* If the register is used as an input in I3, it dies there.
11684              Similarly for I2, if it is non-zero and adjacent to I3.
11685
11686              If the register is not used as an input in either I3 or I2
11687              and it is not one of the registers we were supposed to eliminate,
11688              there are two possibilities.  We might have a non-adjacent I2
11689              or we might have somehow eliminated an additional register
11690              from a computation.  For example, we might have had A & B where
11691              we discover that B will always be zero.  In this case we will
11692              eliminate the reference to A.
11693
11694              In both cases, we must search to see if we can find a previous
11695              use of A and put the death note there.  */
11696
11697           if (from_insn
11698               && GET_CODE (from_insn) == CALL_INSN
11699               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11700             place = from_insn;
11701           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
11702             place = i3;
11703           else if (i2 != 0 && next_nonnote_insn (i2) == i3
11704                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11705             place = i2;
11706
11707           if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
11708             break;
11709
11710           /* If the register is used in both I2 and I3 and it dies in I3, 
11711              we might have added another reference to it.  If reg_n_refs
11712              was 2, bump it to 3.  This has to be correct since the 
11713              register must have been set somewhere.  The reason this is
11714              done is because local-alloc.c treats 2 references as a 
11715              special case.  */
11716
11717           if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
11718               && REG_N_REFS (REGNO (XEXP (note, 0)))== 2
11719               && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11720             REG_N_REFS (REGNO (XEXP (note, 0))) = 3;
11721
11722           if (place == 0)
11723             {
11724               for (tem = prev_nonnote_insn (i3);
11725                    place == 0 && tem
11726                    && (GET_CODE (tem) == INSN || GET_CODE (tem) == CALL_INSN);
11727                    tem = prev_nonnote_insn (tem))
11728                 {
11729                   /* If the register is being set at TEM, see if that is all
11730                      TEM is doing.  If so, delete TEM.  Otherwise, make this
11731                      into a REG_UNUSED note instead.  */
11732                   if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11733                     {
11734                       rtx set = single_set (tem);
11735                       rtx inner_dest = 0;
11736 #ifdef HAVE_cc0
11737                       rtx cc0_setter = NULL_RTX;
11738 #endif
11739
11740                       if (set != 0)
11741                         for (inner_dest = SET_DEST (set);
11742                              GET_CODE (inner_dest) == STRICT_LOW_PART
11743                              || GET_CODE (inner_dest) == SUBREG
11744                              || GET_CODE (inner_dest) == ZERO_EXTRACT;
11745                              inner_dest = XEXP (inner_dest, 0))
11746                           ;
11747
11748                       /* Verify that it was the set, and not a clobber that
11749                          modified the register. 
11750
11751                          CC0 targets must be careful to maintain setter/user
11752                          pairs.  If we cannot delete the setter due to side
11753                          effects, mark the user with an UNUSED note instead
11754                          of deleting it.  */
11755
11756                       if (set != 0 && ! side_effects_p (SET_SRC (set))
11757                           && rtx_equal_p (XEXP (note, 0), inner_dest)
11758 #ifdef HAVE_cc0
11759                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
11760                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
11761                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
11762 #endif
11763                           )
11764                         {
11765                           /* Move the notes and links of TEM elsewhere.
11766                              This might delete other dead insns recursively. 
11767                              First set the pattern to something that won't use
11768                              any register.  */
11769
11770                           PATTERN (tem) = pc_rtx;
11771
11772                           distribute_notes (REG_NOTES (tem), tem, tem,
11773                                             NULL_RTX, NULL_RTX, NULL_RTX);
11774                           distribute_links (LOG_LINKS (tem));
11775
11776                           PUT_CODE (tem, NOTE);
11777                           NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11778                           NOTE_SOURCE_FILE (tem) = 0;
11779
11780 #ifdef HAVE_cc0
11781                           /* Delete the setter too.  */
11782                           if (cc0_setter)
11783                             {
11784                               PATTERN (cc0_setter) = pc_rtx;
11785
11786                               distribute_notes (REG_NOTES (cc0_setter),
11787                                                 cc0_setter, cc0_setter,
11788                                                 NULL_RTX, NULL_RTX, NULL_RTX);
11789                               distribute_links (LOG_LINKS (cc0_setter));
11790
11791                               PUT_CODE (cc0_setter, NOTE);
11792                               NOTE_LINE_NUMBER (cc0_setter) = NOTE_INSN_DELETED;
11793                               NOTE_SOURCE_FILE (cc0_setter) = 0;
11794                             }
11795 #endif
11796                         }
11797                       /* If the register is both set and used here, put the
11798                          REG_DEAD note here, but place a REG_UNUSED note
11799                          here too unless there already is one.  */
11800                       else if (reg_referenced_p (XEXP (note, 0),
11801                                                  PATTERN (tem)))
11802                         {
11803                           place = tem;
11804
11805                           if (! find_regno_note (tem, REG_UNUSED,
11806                                                  REGNO (XEXP (note, 0))))
11807                             REG_NOTES (tem)
11808                               = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
11809                                                    REG_NOTES (tem));
11810                         }
11811                       else
11812                         {
11813                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
11814                           
11815                           /*  If there isn't already a REG_UNUSED note, put one
11816                               here.  */
11817                           if (! find_regno_note (tem, REG_UNUSED,
11818                                                  REGNO (XEXP (note, 0))))
11819                             place = tem;
11820                           break;
11821                       }
11822                   }
11823                 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11824                          || (GET_CODE (tem) == CALL_INSN
11825                              && find_reg_fusage (tem, USE, XEXP (note, 0))))
11826                   {
11827                     place = tem;
11828
11829                     /* If we are doing a 3->2 combination, and we have a
11830                        register which formerly died in i3 and was not used
11831                        by i2, which now no longer dies in i3 and is used in
11832                        i2 but does not die in i2, and place is between i2
11833                        and i3, then we may need to move a link from place to
11834                        i2.  */
11835                     if (i2 && INSN_UID (place) <= max_uid_cuid
11836                         && INSN_CUID (place) > INSN_CUID (i2)
11837                         && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
11838                         && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11839                       {
11840                         rtx links = LOG_LINKS (place);
11841                         LOG_LINKS (place) = 0;
11842                         distribute_links (links);
11843                       }
11844                     break;
11845                   }
11846                 }
11847               
11848               /* If we haven't found an insn for the death note and it
11849                  is still a REG_DEAD note, but we have hit a CODE_LABEL,
11850                  insert a USE insn for the register at that label and
11851                  put the death node there.  This prevents problems with
11852                  call-state tracking in caller-save.c.  */
11853               if (REG_NOTE_KIND (note) == REG_DEAD && place == 0 && tem != 0)
11854                 {
11855                   place
11856                     = emit_insn_after (gen_rtx_USE (VOIDmode, XEXP (note, 0)),
11857                                        tem);
11858
11859                   /* If this insn was emitted between blocks, then update
11860                      BLOCK_HEAD of the current block to include it.  */
11861                   if (BLOCK_END (this_basic_block - 1) == tem)
11862                     BLOCK_HEAD (this_basic_block) = place;
11863                 }
11864             }
11865
11866           /* If the register is set or already dead at PLACE, we needn't do
11867              anything with this note if it is still a REG_DEAD note.
11868              We can here if it is set at all, not if is it totally replace,
11869              which is what `dead_or_set_p' checks, so also check for it being
11870              set partially.  */
11871
11872
11873           if (place && REG_NOTE_KIND (note) == REG_DEAD)
11874             {
11875               int regno = REGNO (XEXP (note, 0));
11876
11877               if (dead_or_set_p (place, XEXP (note, 0))
11878                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
11879                 {
11880                   /* Unless the register previously died in PLACE, clear
11881                      reg_last_death.  [I no longer understand why this is
11882                      being done.] */
11883                   if (reg_last_death[regno] != place)
11884                     reg_last_death[regno] = 0;
11885                   place = 0;
11886                 }
11887               else
11888                 reg_last_death[regno] = place;
11889
11890               /* If this is a death note for a hard reg that is occupying
11891                  multiple registers, ensure that we are still using all
11892                  parts of the object.  If we find a piece of the object
11893                  that is unused, we must add a USE for that piece before
11894                  PLACE and put the appropriate REG_DEAD note on it.
11895
11896                  An alternative would be to put a REG_UNUSED for the pieces
11897                  on the insn that set the register, but that can't be done if
11898                  it is not in the same block.  It is simpler, though less
11899                  efficient, to add the USE insns.  */
11900
11901               if (place && regno < FIRST_PSEUDO_REGISTER
11902                   && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
11903                 {
11904                   int endregno
11905                     = regno + HARD_REGNO_NREGS (regno,
11906                                                 GET_MODE (XEXP (note, 0)));
11907                   int all_used = 1;
11908                   int i;
11909
11910                   for (i = regno; i < endregno; i++)
11911                     if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
11912                         && ! find_regno_fusage (place, USE, i))
11913                       {
11914                         rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
11915                         rtx p;
11916
11917                         /* See if we already placed a USE note for this
11918                            register in front of PLACE.  */
11919                         for (p = place;
11920                              GET_CODE (PREV_INSN (p)) == INSN
11921                              && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
11922                              p = PREV_INSN (p))
11923                           if (rtx_equal_p (piece,
11924                                            XEXP (PATTERN (PREV_INSN (p)), 0)))
11925                             {
11926                               p = 0;
11927                               break;
11928                             }
11929
11930                         if (p)
11931                           {
11932                             rtx use_insn
11933                               = emit_insn_before (gen_rtx_USE (VOIDmode,
11934                                                                piece),
11935                                                   p);
11936                             REG_NOTES (use_insn)
11937                               = gen_rtx_EXPR_LIST (REG_DEAD, piece,
11938                                                    REG_NOTES (use_insn));
11939                           }
11940
11941                         all_used = 0;
11942                       }
11943
11944                   /* Check for the case where the register dying partially
11945                      overlaps the register set by this insn.  */
11946                   if (all_used)
11947                     for (i = regno; i < endregno; i++)
11948                       if (dead_or_set_regno_p (place, i))
11949                           {
11950                             all_used = 0;
11951                             break;
11952                           }
11953
11954                   if (! all_used)
11955                     {
11956                       /* Put only REG_DEAD notes for pieces that are
11957                          still used and that are not already dead or set.  */
11958
11959                       for (i = regno; i < endregno; i++)
11960                         {
11961                           rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
11962
11963                           if ((reg_referenced_p (piece, PATTERN (place))
11964                                || (GET_CODE (place) == CALL_INSN
11965                                    && find_reg_fusage (place, USE, piece)))
11966                               && ! dead_or_set_p (place, piece)
11967                               && ! reg_bitfield_target_p (piece,
11968                                                           PATTERN (place)))
11969                             REG_NOTES (place)
11970                               = gen_rtx_EXPR_LIST (REG_DEAD, piece,
11971                                                    REG_NOTES (place));
11972                         }
11973
11974                       place = 0;
11975                     }
11976                 }
11977             }
11978           break;
11979
11980         default:
11981           /* Any other notes should not be present at this point in the
11982              compilation.  */
11983           abort ();
11984         }
11985
11986       if (place)
11987         {
11988           XEXP (note, 1) = REG_NOTES (place);
11989           REG_NOTES (place) = note;
11990         }
11991       else if ((REG_NOTE_KIND (note) == REG_DEAD
11992                 || REG_NOTE_KIND (note) == REG_UNUSED)
11993                && GET_CODE (XEXP (note, 0)) == REG)
11994         REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
11995
11996       if (place2)
11997         {
11998           if ((REG_NOTE_KIND (note) == REG_DEAD
11999                || REG_NOTE_KIND (note) == REG_UNUSED)
12000               && GET_CODE (XEXP (note, 0)) == REG)
12001             REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12002
12003           REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12004                                                REG_NOTE_KIND (note),
12005                                                XEXP (note, 0),
12006                                                REG_NOTES (place2));
12007         }
12008     }
12009 }
12010 \f
12011 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12012    I3, I2, and I1 to new locations.  This is also called in one case to
12013    add a link pointing at I3 when I3's destination is changed.  */
12014
12015 static void
12016 distribute_links (links)
12017      rtx links;
12018 {
12019   rtx link, next_link;
12020
12021   for (link = links; link; link = next_link)
12022     {
12023       rtx place = 0;
12024       rtx insn;
12025       rtx set, reg;
12026
12027       next_link = XEXP (link, 1);
12028
12029       /* If the insn that this link points to is a NOTE or isn't a single
12030          set, ignore it.  In the latter case, it isn't clear what we
12031          can do other than ignore the link, since we can't tell which 
12032          register it was for.  Such links wouldn't be used by combine
12033          anyway.
12034
12035          It is not possible for the destination of the target of the link to
12036          have been changed by combine.  The only potential of this is if we
12037          replace I3, I2, and I1 by I3 and I2.  But in that case the
12038          destination of I2 also remains unchanged.  */
12039
12040       if (GET_CODE (XEXP (link, 0)) == NOTE
12041           || (set = single_set (XEXP (link, 0))) == 0)
12042         continue;
12043
12044       reg = SET_DEST (set);
12045       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12046              || GET_CODE (reg) == SIGN_EXTRACT
12047              || GET_CODE (reg) == STRICT_LOW_PART)
12048         reg = XEXP (reg, 0);
12049
12050       /* A LOG_LINK is defined as being placed on the first insn that uses
12051          a register and points to the insn that sets the register.  Start
12052          searching at the next insn after the target of the link and stop
12053          when we reach a set of the register or the end of the basic block.
12054
12055          Note that this correctly handles the link that used to point from
12056          I3 to I2.  Also note that not much searching is typically done here
12057          since most links don't point very far away.  */
12058
12059       for (insn = NEXT_INSN (XEXP (link, 0));
12060            (insn && (this_basic_block == n_basic_blocks - 1
12061                      || BLOCK_HEAD (this_basic_block + 1) != insn));
12062            insn = NEXT_INSN (insn))
12063         if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
12064             && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12065           {
12066             if (reg_referenced_p (reg, PATTERN (insn)))
12067               place = insn;
12068             break;
12069           }
12070         else if (GET_CODE (insn) == CALL_INSN
12071               && find_reg_fusage (insn, USE, reg))
12072           {
12073             place = insn;
12074             break;
12075           }
12076
12077       /* If we found a place to put the link, place it there unless there
12078          is already a link to the same insn as LINK at that point.  */
12079
12080       if (place)
12081         {
12082           rtx link2;
12083
12084           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12085             if (XEXP (link2, 0) == XEXP (link, 0))
12086               break;
12087
12088           if (link2 == 0)
12089             {
12090               XEXP (link, 1) = LOG_LINKS (place);
12091               LOG_LINKS (place) = link;
12092
12093               /* Set added_links_insn to the earliest insn we added a
12094                  link to.  */
12095               if (added_links_insn == 0 
12096                   || INSN_CUID (added_links_insn) > INSN_CUID (place))
12097                 added_links_insn = place;
12098             }
12099         }
12100     }
12101 }
12102 \f
12103 /* Compute INSN_CUID for INSN, which is an insn made by combine.  */
12104
12105 static int
12106 insn_cuid (insn)
12107      rtx insn;
12108 {
12109   while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12110          && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
12111     insn = NEXT_INSN (insn);
12112
12113   if (INSN_UID (insn) > max_uid_cuid)
12114     abort ();
12115
12116   return INSN_CUID (insn);
12117 }
12118 \f
12119 void
12120 dump_combine_stats (file)
12121      FILE *file;
12122 {
12123   fnotice
12124     (file,
12125      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12126      combine_attempts, combine_merges, combine_extras, combine_successes);
12127 }
12128
12129 void
12130 dump_combine_total_stats (file)
12131      FILE *file;
12132 {
12133   fnotice
12134     (file,
12135      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12136      total_attempts, total_merges, total_extras, total_successes);
12137 }