OSDN Git Service

* semantics.c (finish_id_expression): Refactor the code to handle
[pf3gnuchains/gcc-fork.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
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    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
57      removed because there is no way to know which register it was
58      linking
59
60    To simplify substitution, we combine only when the earlier insn(s)
61    consist of only a single assignment.  To simplify updating afterward,
62    we never combine when a subroutine call appears in the middle.
63
64    Since we do not represent assignments to CC0 explicitly except when that
65    is all an insn does, there is no LOG_LINKS entry in an insn that uses
66    the condition code for the insn that set the condition code.
67    Fortunately, these two insns must be consecutive.
68    Therefore, every JUMP_INSN is taken to have an implicit logical link
69    to the preceding insn.  This is not quite right, since non-jumps can
70    also use the condition code; but in practice such insns would not
71    combine anyway.  */
72
73 #include "config.h"
74 #include "system.h"
75 #include "coretypes.h"
76 #include "tm.h"
77 #include "rtl.h"
78 #include "tree.h"
79 #include "tm_p.h"
80 #include "flags.h"
81 #include "regs.h"
82 #include "hard-reg-set.h"
83 #include "basic-block.h"
84 #include "insn-config.h"
85 #include "function.h"
86 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
87 #include "expr.h"
88 #include "insn-attr.h"
89 #include "recog.h"
90 #include "real.h"
91 #include "toplev.h"
92 #include "target.h"
93
94 /* It is not safe to use ordinary gen_lowpart in combine.
95    Use gen_lowpart_for_combine instead.  See comments there.  */
96 #define gen_lowpart dont_use_gen_lowpart_you_dummy
97
98 /* Number of attempts to combine instructions in this function.  */
99
100 static int combine_attempts;
101
102 /* Number of attempts that got as far as substitution in this function.  */
103
104 static int combine_merges;
105
106 /* Number of instructions combined with added SETs in this function.  */
107
108 static int combine_extras;
109
110 /* Number of instructions combined in this function.  */
111
112 static int combine_successes;
113
114 /* Totals over entire compilation.  */
115
116 static int total_attempts, total_merges, total_extras, total_successes;
117
118 \f
119 /* Vector mapping INSN_UIDs to cuids.
120    The cuids are like uids but increase monotonically always.
121    Combine always uses cuids so that it can compare them.
122    But actually renumbering the uids, which we used to do,
123    proves to be a bad idea because it makes it hard to compare
124    the dumps produced by earlier passes with those from later passes.  */
125
126 static int *uid_cuid;
127 static int max_uid_cuid;
128
129 /* Get the cuid of an insn.  */
130
131 #define INSN_CUID(INSN) \
132 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
133
134 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
135    BITS_PER_WORD would invoke undefined behavior.  Work around it.  */
136
137 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
138   (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
139
140 #define nonzero_bits(X, M) \
141   cached_nonzero_bits (X, M, NULL_RTX, VOIDmode, 0)
142
143 #define num_sign_bit_copies(X, M) \
144   cached_num_sign_bit_copies (X, M, NULL_RTX, VOIDmode, 0)
145
146 /* Maximum register number, which is the size of the tables below.  */
147
148 static unsigned int combine_max_regno;
149
150 /* Record last point of death of (hard or pseudo) register n.  */
151
152 static rtx *reg_last_death;
153
154 /* Record last point of modification of (hard or pseudo) register n.  */
155
156 static rtx *reg_last_set;
157
158 /* Record the cuid of the last insn that invalidated memory
159    (anything that writes memory, and subroutine calls, but not pushes).  */
160
161 static int mem_last_set;
162
163 /* Record the cuid of the last CALL_INSN
164    so we can tell whether a potential combination crosses any calls.  */
165
166 static int last_call_cuid;
167
168 /* When `subst' is called, this is the insn that is being modified
169    (by combining in a previous insn).  The PATTERN of this insn
170    is still the old pattern partially modified and it should not be
171    looked at, but this may be used to examine the successors of the insn
172    to judge whether a simplification is valid.  */
173
174 static rtx subst_insn;
175
176 /* This is the lowest CUID that `subst' is currently dealing with.
177    get_last_value will not return a value if the register was set at or
178    after this CUID.  If not for this mechanism, we could get confused if
179    I2 or I1 in try_combine were an insn that used the old value of a register
180    to obtain a new value.  In that case, we might erroneously get the
181    new value of the register when we wanted the old one.  */
182
183 static int subst_low_cuid;
184
185 /* This contains any hard registers that are used in newpat; reg_dead_at_p
186    must consider all these registers to be always live.  */
187
188 static HARD_REG_SET newpat_used_regs;
189
190 /* This is an insn to which a LOG_LINKS entry has been added.  If this
191    insn is the earlier than I2 or I3, combine should rescan starting at
192    that location.  */
193
194 static rtx added_links_insn;
195
196 /* Basic block in which we are performing combines.  */
197 static basic_block this_basic_block;
198
199 /* A bitmap indicating which blocks had registers go dead at entry.
200    After combine, we'll need to re-do global life analysis with
201    those blocks as starting points.  */
202 static sbitmap refresh_blocks;
203 \f
204 /* The next group of arrays allows the recording of the last value assigned
205    to (hard or pseudo) register n.  We use this information to see if an
206    operation being processed is redundant given a prior operation performed
207    on the register.  For example, an `and' with a constant is redundant if
208    all the zero bits are already known to be turned off.
209
210    We use an approach similar to that used by cse, but change it in the
211    following ways:
212
213    (1) We do not want to reinitialize at each label.
214    (2) It is useful, but not critical, to know the actual value assigned
215        to a register.  Often just its form is helpful.
216
217    Therefore, we maintain the following arrays:
218
219    reg_last_set_value           the last value assigned
220    reg_last_set_label           records the value of label_tick when the
221                                 register was assigned
222    reg_last_set_table_tick      records the value of label_tick when a
223                                 value using the register is assigned
224    reg_last_set_invalid         set to nonzero when it is not valid
225                                 to use the value of this register in some
226                                 register's value
227
228    To understand the usage of these tables, it is important to understand
229    the distinction between the value in reg_last_set_value being valid
230    and the register being validly contained in some other expression in the
231    table.
232
233    Entry I in reg_last_set_value is valid if it is nonzero, and either
234    reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
235
236    Register I may validly appear in any expression returned for the value
237    of another register if reg_n_sets[i] is 1.  It may also appear in the
238    value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
239    reg_last_set_invalid[j] is zero.
240
241    If an expression is found in the table containing a register which may
242    not validly appear in an expression, the register is replaced by
243    something that won't match, (clobber (const_int 0)).
244
245    reg_last_set_invalid[i] is set nonzero when register I is being assigned
246    to and reg_last_set_table_tick[i] == label_tick.  */
247
248 /* Record last value assigned to (hard or pseudo) register n.  */
249
250 static rtx *reg_last_set_value;
251
252 /* Record the value of label_tick when the value for register n is placed in
253    reg_last_set_value[n].  */
254
255 static int *reg_last_set_label;
256
257 /* Record the value of label_tick when an expression involving register n
258    is placed in reg_last_set_value.  */
259
260 static int *reg_last_set_table_tick;
261
262 /* Set nonzero if references to register n in expressions should not be
263    used.  */
264
265 static char *reg_last_set_invalid;
266
267 /* Incremented for each label.  */
268
269 static int label_tick;
270
271 /* Some registers that are set more than once and used in more than one
272    basic block are nevertheless always set in similar ways.  For example,
273    a QImode register may be loaded from memory in two places on a machine
274    where byte loads zero extend.
275
276    We record in the following array what we know about the nonzero
277    bits of a register, specifically which bits are known to be zero.
278
279    If an entry is zero, it means that we don't know anything special.  */
280
281 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
282
283 /* Mode used to compute significance in reg_nonzero_bits.  It is the largest
284    integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
285
286 static enum machine_mode nonzero_bits_mode;
287
288 /* Nonzero if we know that a register has some leading bits that are always
289    equal to the sign bit.  */
290
291 static unsigned char *reg_sign_bit_copies;
292
293 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
294    It is zero while computing them and after combine has completed.  This
295    former test prevents propagating values based on previously set values,
296    which can be incorrect if a variable is modified in a loop.  */
297
298 static int nonzero_sign_valid;
299
300 /* These arrays are maintained in parallel with reg_last_set_value
301    and are used to store the mode in which the register was last set,
302    the bits that were known to be zero when it was last set, and the
303    number of sign bits copies it was known to have when it was last set.  */
304
305 static enum machine_mode *reg_last_set_mode;
306 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
307 static char *reg_last_set_sign_bit_copies;
308 \f
309 /* Record one modification to rtl structure
310    to be undone by storing old_contents into *where.
311    is_int is 1 if the contents are an int.  */
312
313 struct undo
314 {
315   struct undo *next;
316   int is_int;
317   union {rtx r; int i;} old_contents;
318   union {rtx *r; int *i;} where;
319 };
320
321 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
322    num_undo says how many are currently recorded.
323
324    other_insn is nonzero if we have modified some other insn in the process
325    of working on subst_insn.  It must be verified too.  */
326
327 struct undobuf
328 {
329   struct undo *undos;
330   struct undo *frees;
331   rtx other_insn;
332 };
333
334 static struct undobuf undobuf;
335
336 /* Number of times the pseudo being substituted for
337    was found and replaced.  */
338
339 static int n_occurrences;
340
341 static void do_SUBST (rtx *, rtx);
342 static void do_SUBST_INT (int *, int);
343 static void init_reg_last_arrays (void);
344 static void setup_incoming_promotions (void);
345 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
346 static int cant_combine_insn_p (rtx);
347 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
348 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
349 static int contains_muldiv (rtx);
350 static rtx try_combine (rtx, rtx, rtx, int *);
351 static void undo_all (void);
352 static void undo_commit (void);
353 static rtx *find_split_point (rtx *, rtx);
354 static rtx subst (rtx, rtx, rtx, int, int);
355 static rtx combine_simplify_rtx (rtx, enum machine_mode, int, int);
356 static rtx simplify_if_then_else (rtx);
357 static rtx simplify_set (rtx);
358 static rtx simplify_logical (rtx, int);
359 static rtx expand_compound_operation (rtx);
360 static rtx expand_field_assignment (rtx);
361 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
362                             rtx, unsigned HOST_WIDE_INT, int, int, int);
363 static rtx extract_left_shift (rtx, int);
364 static rtx make_compound_operation (rtx, enum rtx_code);
365 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
366                               unsigned HOST_WIDE_INT *);
367 static rtx force_to_mode (rtx, enum machine_mode,
368                           unsigned HOST_WIDE_INT, rtx, int);
369 static rtx if_then_else_cond (rtx, rtx *, rtx *);
370 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
371 static int rtx_equal_for_field_assignment_p (rtx, rtx);
372 static rtx make_field_assignment (rtx);
373 static rtx apply_distributive_law (rtx);
374 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
375                                    unsigned HOST_WIDE_INT);
376 static unsigned HOST_WIDE_INT cached_nonzero_bits (rtx, enum machine_mode,
377                                                    rtx, enum machine_mode,
378                                                    unsigned HOST_WIDE_INT);
379 static unsigned HOST_WIDE_INT nonzero_bits1 (rtx, enum machine_mode, rtx,
380                                              enum machine_mode,
381                                              unsigned HOST_WIDE_INT);
382 static unsigned int cached_num_sign_bit_copies (rtx, enum machine_mode, rtx,
383                                                 enum machine_mode,
384                                                 unsigned int);
385 static unsigned int num_sign_bit_copies1 (rtx, enum machine_mode, rtx,
386                                           enum machine_mode, unsigned int);
387 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
388                             HOST_WIDE_INT, enum machine_mode, int *);
389 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
390                                  int);
391 static int recog_for_combine (rtx *, rtx, rtx *);
392 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
393 static rtx gen_binary (enum rtx_code, enum machine_mode, rtx, rtx);
394 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
395 static void update_table_tick (rtx);
396 static void record_value_for_reg (rtx, rtx, rtx);
397 static void check_promoted_subreg (rtx, rtx);
398 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
399 static void record_dead_and_set_regs (rtx);
400 static int get_last_value_validate (rtx *, rtx, int, int);
401 static rtx get_last_value (rtx);
402 static int use_crosses_set_p (rtx, int);
403 static void reg_dead_at_p_1 (rtx, rtx, void *);
404 static int reg_dead_at_p (rtx, rtx);
405 static void move_deaths (rtx, rtx, int, rtx, rtx *);
406 static int reg_bitfield_target_p (rtx, rtx);
407 static void distribute_notes (rtx, rtx, rtx, rtx);
408 static void distribute_links (rtx);
409 static void mark_used_regs_combine (rtx);
410 static int insn_cuid (rtx);
411 static void record_promoted_value (rtx, rtx);
412 static rtx reversed_comparison (rtx, enum machine_mode, rtx, rtx);
413 static enum rtx_code combine_reversed_comparison_code (rtx);
414 \f
415 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
416    insn.  The substitution can be undone by undo_all.  If INTO is already
417    set to NEWVAL, do not record this change.  Because computing NEWVAL might
418    also call SUBST, we have to compute it before we put anything into
419    the undo table.  */
420
421 static void
422 do_SUBST (rtx *into, rtx newval)
423 {
424   struct undo *buf;
425   rtx oldval = *into;
426
427   if (oldval == newval)
428     return;
429
430   /* We'd like to catch as many invalid transformations here as
431      possible.  Unfortunately, there are way too many mode changes
432      that are perfectly valid, so we'd waste too much effort for
433      little gain doing the checks here.  Focus on catching invalid
434      transformations involving integer constants.  */
435   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
436       && GET_CODE (newval) == CONST_INT)
437     {
438       /* Sanity check that we're replacing oldval with a CONST_INT
439          that is a valid sign-extension for the original mode.  */
440       if (INTVAL (newval) != trunc_int_for_mode (INTVAL (newval),
441                                                  GET_MODE (oldval)))
442         abort ();
443
444       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
445          CONST_INT is not valid, because after the replacement, the
446          original mode would be gone.  Unfortunately, we can't tell
447          when do_SUBST is called to replace the operand thereof, so we
448          perform this test on oldval instead, checking whether an
449          invalid replacement took place before we got here.  */
450       if ((GET_CODE (oldval) == SUBREG
451            && GET_CODE (SUBREG_REG (oldval)) == CONST_INT)
452           || (GET_CODE (oldval) == ZERO_EXTEND
453               && GET_CODE (XEXP (oldval, 0)) == CONST_INT))
454         abort ();
455     }
456
457   if (undobuf.frees)
458     buf = undobuf.frees, undobuf.frees = buf->next;
459   else
460     buf = xmalloc (sizeof (struct undo));
461
462   buf->is_int = 0;
463   buf->where.r = into;
464   buf->old_contents.r = oldval;
465   *into = newval;
466
467   buf->next = undobuf.undos, undobuf.undos = buf;
468 }
469
470 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
471
472 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
473    for the value of a HOST_WIDE_INT value (including CONST_INT) is
474    not safe.  */
475
476 static void
477 do_SUBST_INT (int *into, int newval)
478 {
479   struct undo *buf;
480   int oldval = *into;
481
482   if (oldval == newval)
483     return;
484
485   if (undobuf.frees)
486     buf = undobuf.frees, undobuf.frees = buf->next;
487   else
488     buf = xmalloc (sizeof (struct undo));
489
490   buf->is_int = 1;
491   buf->where.i = into;
492   buf->old_contents.i = oldval;
493   *into = newval;
494
495   buf->next = undobuf.undos, undobuf.undos = buf;
496 }
497
498 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
499 \f
500 /* Main entry point for combiner.  F is the first insn of the function.
501    NREGS is the first unused pseudo-reg number.
502
503    Return nonzero if the combiner has turned an indirect jump
504    instruction into a direct jump.  */
505 int
506 combine_instructions (rtx f, unsigned int nregs)
507 {
508   rtx insn, next;
509 #ifdef HAVE_cc0
510   rtx prev;
511 #endif
512   int i;
513   rtx links, nextlinks;
514
515   int new_direct_jump_p = 0;
516
517   combine_attempts = 0;
518   combine_merges = 0;
519   combine_extras = 0;
520   combine_successes = 0;
521
522   combine_max_regno = nregs;
523
524   reg_nonzero_bits = xcalloc (nregs, sizeof (unsigned HOST_WIDE_INT));
525   reg_sign_bit_copies = xcalloc (nregs, sizeof (unsigned char));
526
527   reg_last_death = xmalloc (nregs * sizeof (rtx));
528   reg_last_set = xmalloc (nregs * sizeof (rtx));
529   reg_last_set_value = xmalloc (nregs * sizeof (rtx));
530   reg_last_set_table_tick = xmalloc (nregs * sizeof (int));
531   reg_last_set_label = xmalloc (nregs * sizeof (int));
532   reg_last_set_invalid = xmalloc (nregs * sizeof (char));
533   reg_last_set_mode = xmalloc (nregs * sizeof (enum machine_mode));
534   reg_last_set_nonzero_bits = xmalloc (nregs * sizeof (HOST_WIDE_INT));
535   reg_last_set_sign_bit_copies = xmalloc (nregs * sizeof (char));
536
537   init_reg_last_arrays ();
538
539   init_recog_no_volatile ();
540
541   /* Compute maximum uid value so uid_cuid can be allocated.  */
542
543   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
544     if (INSN_UID (insn) > i)
545       i = INSN_UID (insn);
546
547   uid_cuid = xmalloc ((i + 1) * sizeof (int));
548   max_uid_cuid = i;
549
550   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
551
552   /* Don't use reg_nonzero_bits when computing it.  This can cause problems
553      when, for example, we have j <<= 1 in a loop.  */
554
555   nonzero_sign_valid = 0;
556
557   /* Compute the mapping from uids to cuids.
558      Cuids are numbers assigned to insns, like uids,
559      except that cuids increase monotonically through the code.
560
561      Scan all SETs and see if we can deduce anything about what
562      bits are known to be zero for some registers and how many copies
563      of the sign bit are known to exist for those registers.
564
565      Also set any known values so that we can use it while searching
566      for what bits are known to be set.  */
567
568   label_tick = 1;
569
570   setup_incoming_promotions ();
571
572   refresh_blocks = sbitmap_alloc (last_basic_block);
573   sbitmap_zero (refresh_blocks);
574
575   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
576     {
577       uid_cuid[INSN_UID (insn)] = ++i;
578       subst_low_cuid = i;
579       subst_insn = insn;
580
581       if (INSN_P (insn))
582         {
583           note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
584                        NULL);
585           record_dead_and_set_regs (insn);
586
587 #ifdef AUTO_INC_DEC
588           for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
589             if (REG_NOTE_KIND (links) == REG_INC)
590               set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
591                                                 NULL);
592 #endif
593         }
594
595       if (GET_CODE (insn) == CODE_LABEL)
596         label_tick++;
597     }
598
599   nonzero_sign_valid = 1;
600
601   /* Now scan all the insns in forward order.  */
602
603   label_tick = 1;
604   last_call_cuid = 0;
605   mem_last_set = 0;
606   init_reg_last_arrays ();
607   setup_incoming_promotions ();
608
609   FOR_EACH_BB (this_basic_block)
610     {
611       for (insn = BB_HEAD (this_basic_block);
612            insn != NEXT_INSN (BB_END (this_basic_block));
613            insn = next ? next : NEXT_INSN (insn))
614         {
615           next = 0;
616
617           if (GET_CODE (insn) == CODE_LABEL)
618             label_tick++;
619
620           else if (INSN_P (insn))
621             {
622               /* See if we know about function return values before this
623                  insn based upon SUBREG flags.  */
624               check_promoted_subreg (insn, PATTERN (insn));
625
626               /* Try this insn with each insn it links back to.  */
627
628               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
629                 if ((next = try_combine (insn, XEXP (links, 0),
630                                          NULL_RTX, &new_direct_jump_p)) != 0)
631                   goto retry;
632
633               /* Try each sequence of three linked insns ending with this one.  */
634
635               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
636                 {
637                   rtx link = XEXP (links, 0);
638
639                   /* If the linked insn has been replaced by a note, then there
640                      is no point in pursuing this chain any further.  */
641                   if (GET_CODE (link) == NOTE)
642                     continue;
643
644                   for (nextlinks = LOG_LINKS (link);
645                        nextlinks;
646                        nextlinks = XEXP (nextlinks, 1))
647                     if ((next = try_combine (insn, link,
648                                              XEXP (nextlinks, 0),
649                                              &new_direct_jump_p)) != 0)
650                       goto retry;
651                 }
652
653 #ifdef HAVE_cc0
654               /* Try to combine a jump insn that uses CC0
655                  with a preceding insn that sets CC0, and maybe with its
656                  logical predecessor as well.
657                  This is how we make decrement-and-branch insns.
658                  We need this special code because data flow connections
659                  via CC0 do not get entered in LOG_LINKS.  */
660
661               if (GET_CODE (insn) == JUMP_INSN
662                   && (prev = prev_nonnote_insn (insn)) != 0
663                   && GET_CODE (prev) == INSN
664                   && sets_cc0_p (PATTERN (prev)))
665                 {
666                   if ((next = try_combine (insn, prev,
667                                            NULL_RTX, &new_direct_jump_p)) != 0)
668                     goto retry;
669
670                   for (nextlinks = LOG_LINKS (prev); nextlinks;
671                        nextlinks = XEXP (nextlinks, 1))
672                     if ((next = try_combine (insn, prev,
673                                              XEXP (nextlinks, 0),
674                                              &new_direct_jump_p)) != 0)
675                       goto retry;
676                 }
677
678               /* Do the same for an insn that explicitly references CC0.  */
679               if (GET_CODE (insn) == INSN
680                   && (prev = prev_nonnote_insn (insn)) != 0
681                   && GET_CODE (prev) == INSN
682                   && sets_cc0_p (PATTERN (prev))
683                   && GET_CODE (PATTERN (insn)) == SET
684                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
685                 {
686                   if ((next = try_combine (insn, prev,
687                                            NULL_RTX, &new_direct_jump_p)) != 0)
688                     goto retry;
689
690                   for (nextlinks = LOG_LINKS (prev); nextlinks;
691                        nextlinks = XEXP (nextlinks, 1))
692                     if ((next = try_combine (insn, prev,
693                                              XEXP (nextlinks, 0),
694                                              &new_direct_jump_p)) != 0)
695                       goto retry;
696                 }
697
698               /* Finally, see if any of the insns that this insn links to
699                  explicitly references CC0.  If so, try this insn, that insn,
700                  and its predecessor if it sets CC0.  */
701               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
702                 if (GET_CODE (XEXP (links, 0)) == INSN
703                     && GET_CODE (PATTERN (XEXP (links, 0))) == SET
704                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
705                     && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
706                     && GET_CODE (prev) == INSN
707                     && sets_cc0_p (PATTERN (prev))
708                     && (next = try_combine (insn, XEXP (links, 0),
709                                             prev, &new_direct_jump_p)) != 0)
710                   goto retry;
711 #endif
712
713               /* Try combining an insn with two different insns whose results it
714                  uses.  */
715               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
716                 for (nextlinks = XEXP (links, 1); nextlinks;
717                      nextlinks = XEXP (nextlinks, 1))
718                   if ((next = try_combine (insn, XEXP (links, 0),
719                                            XEXP (nextlinks, 0),
720                                            &new_direct_jump_p)) != 0)
721                     goto retry;
722
723               if (GET_CODE (insn) != NOTE)
724                 record_dead_and_set_regs (insn);
725
726             retry:
727               ;
728             }
729         }
730     }
731   clear_bb_flags ();
732
733   EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, i,
734                              BASIC_BLOCK (i)->flags |= BB_DIRTY);
735   new_direct_jump_p |= purge_all_dead_edges (0);
736   delete_noop_moves (f);
737
738   update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
739                                     PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
740                                     | PROP_KILL_DEAD_CODE);
741
742   /* Clean up.  */
743   sbitmap_free (refresh_blocks);
744   free (reg_nonzero_bits);
745   free (reg_sign_bit_copies);
746   free (reg_last_death);
747   free (reg_last_set);
748   free (reg_last_set_value);
749   free (reg_last_set_table_tick);
750   free (reg_last_set_label);
751   free (reg_last_set_invalid);
752   free (reg_last_set_mode);
753   free (reg_last_set_nonzero_bits);
754   free (reg_last_set_sign_bit_copies);
755   free (uid_cuid);
756
757   {
758     struct undo *undo, *next;
759     for (undo = undobuf.frees; undo; undo = next)
760       {
761         next = undo->next;
762         free (undo);
763       }
764     undobuf.frees = 0;
765   }
766
767   total_attempts += combine_attempts;
768   total_merges += combine_merges;
769   total_extras += combine_extras;
770   total_successes += combine_successes;
771
772   nonzero_sign_valid = 0;
773
774   /* Make recognizer allow volatile MEMs again.  */
775   init_recog ();
776
777   return new_direct_jump_p;
778 }
779
780 /* Wipe the reg_last_xxx arrays in preparation for another pass.  */
781
782 static void
783 init_reg_last_arrays (void)
784 {
785   unsigned int nregs = combine_max_regno;
786
787   memset (reg_last_death, 0, nregs * sizeof (rtx));
788   memset (reg_last_set, 0, nregs * sizeof (rtx));
789   memset (reg_last_set_value, 0, nregs * sizeof (rtx));
790   memset (reg_last_set_table_tick, 0, nregs * sizeof (int));
791   memset (reg_last_set_label, 0, nregs * sizeof (int));
792   memset (reg_last_set_invalid, 0, nregs * sizeof (char));
793   memset (reg_last_set_mode, 0, nregs * sizeof (enum machine_mode));
794   memset (reg_last_set_nonzero_bits, 0, nregs * sizeof (HOST_WIDE_INT));
795   memset (reg_last_set_sign_bit_copies, 0, nregs * sizeof (char));
796 }
797 \f
798 /* Set up any promoted values for incoming argument registers.  */
799
800 static void
801 setup_incoming_promotions (void)
802 {
803   unsigned int regno;
804   rtx reg;
805   enum machine_mode mode;
806   int unsignedp;
807   rtx first = get_insns ();
808
809   if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
810     {
811 #ifndef OUTGOING_REGNO
812 #define OUTGOING_REGNO(N) N
813 #endif
814       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
815         /* Check whether this register can hold an incoming pointer
816            argument.  FUNCTION_ARG_REGNO_P tests outgoing register
817            numbers, so translate if necessary due to register windows.  */
818         if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
819             && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
820           {
821             record_value_for_reg
822               (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
823                                            : SIGN_EXTEND),
824                                           GET_MODE (reg),
825                                           gen_rtx_CLOBBER (mode, const0_rtx)));
826           }
827     }
828 }
829 \f
830 /* Called via note_stores.  If X is a pseudo that is narrower than
831    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
832
833    If we are setting only a portion of X and we can't figure out what
834    portion, assume all bits will be used since we don't know what will
835    be happening.
836
837    Similarly, set how many bits of X are known to be copies of the sign bit
838    at all locations in the function.  This is the smallest number implied
839    by any set of X.  */
840
841 static void
842 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
843                                   void *data ATTRIBUTE_UNUSED)
844 {
845   unsigned int num;
846
847   if (GET_CODE (x) == REG
848       && REGNO (x) >= FIRST_PSEUDO_REGISTER
849       /* If this register is undefined at the start of the file, we can't
850          say what its contents were.  */
851       && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, REGNO (x))
852       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
853     {
854       if (set == 0 || GET_CODE (set) == CLOBBER)
855         {
856           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
857           reg_sign_bit_copies[REGNO (x)] = 1;
858           return;
859         }
860
861       /* If this is a complex assignment, see if we can convert it into a
862          simple assignment.  */
863       set = expand_field_assignment (set);
864
865       /* If this is a simple assignment, or we have a paradoxical SUBREG,
866          set what we know about X.  */
867
868       if (SET_DEST (set) == x
869           || (GET_CODE (SET_DEST (set)) == SUBREG
870               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
871                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
872               && SUBREG_REG (SET_DEST (set)) == x))
873         {
874           rtx src = SET_SRC (set);
875
876 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
877           /* If X is narrower than a word and SRC is a non-negative
878              constant that would appear negative in the mode of X,
879              sign-extend it for use in reg_nonzero_bits because some
880              machines (maybe most) will actually do the sign-extension
881              and this is the conservative approach.
882
883              ??? For 2.5, try to tighten up the MD files in this regard
884              instead of this kludge.  */
885
886           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
887               && GET_CODE (src) == CONST_INT
888               && INTVAL (src) > 0
889               && 0 != (INTVAL (src)
890                        & ((HOST_WIDE_INT) 1
891                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
892             src = GEN_INT (INTVAL (src)
893                            | ((HOST_WIDE_INT) (-1)
894                               << GET_MODE_BITSIZE (GET_MODE (x))));
895 #endif
896
897           /* Don't call nonzero_bits if it cannot change anything.  */
898           if (reg_nonzero_bits[REGNO (x)] != ~(unsigned HOST_WIDE_INT) 0)
899             reg_nonzero_bits[REGNO (x)]
900               |= nonzero_bits (src, nonzero_bits_mode);
901           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
902           if (reg_sign_bit_copies[REGNO (x)] == 0
903               || reg_sign_bit_copies[REGNO (x)] > num)
904             reg_sign_bit_copies[REGNO (x)] = num;
905         }
906       else
907         {
908           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
909           reg_sign_bit_copies[REGNO (x)] = 1;
910         }
911     }
912 }
913 \f
914 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
915    insns that were previously combined into I3 or that will be combined
916    into the merger of INSN and I3.
917
918    Return 0 if the combination is not allowed for any reason.
919
920    If the combination is allowed, *PDEST will be set to the single
921    destination of INSN and *PSRC to the single source, and this function
922    will return 1.  */
923
924 static int
925 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
926                rtx *pdest, rtx *psrc)
927 {
928   int i;
929   rtx set = 0, src, dest;
930   rtx p;
931 #ifdef AUTO_INC_DEC
932   rtx link;
933 #endif
934   int all_adjacent = (succ ? (next_active_insn (insn) == succ
935                               && next_active_insn (succ) == i3)
936                       : next_active_insn (insn) == i3);
937
938   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
939      or a PARALLEL consisting of such a SET and CLOBBERs.
940
941      If INSN has CLOBBER parallel parts, ignore them for our processing.
942      By definition, these happen during the execution of the insn.  When it
943      is merged with another insn, all bets are off.  If they are, in fact,
944      needed and aren't also supplied in I3, they may be added by
945      recog_for_combine.  Otherwise, it won't match.
946
947      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
948      note.
949
950      Get the source and destination of INSN.  If more than one, can't
951      combine.  */
952
953   if (GET_CODE (PATTERN (insn)) == SET)
954     set = PATTERN (insn);
955   else if (GET_CODE (PATTERN (insn)) == PARALLEL
956            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
957     {
958       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
959         {
960           rtx elt = XVECEXP (PATTERN (insn), 0, i);
961
962           switch (GET_CODE (elt))
963             {
964             /* This is important to combine floating point insns
965                for the SH4 port.  */
966             case USE:
967               /* Combining an isolated USE doesn't make sense.
968                  We depend here on combinable_i3pat to reject them.  */
969               /* The code below this loop only verifies that the inputs of
970                  the SET in INSN do not change.  We call reg_set_between_p
971                  to verify that the REG in the USE does not change between
972                  I3 and INSN.
973                  If the USE in INSN was for a pseudo register, the matching
974                  insn pattern will likely match any register; combining this
975                  with any other USE would only be safe if we knew that the
976                  used registers have identical values, or if there was
977                  something to tell them apart, e.g. different modes.  For
978                  now, we forgo such complicated tests and simply disallow
979                  combining of USES of pseudo registers with any other USE.  */
980               if (GET_CODE (XEXP (elt, 0)) == REG
981                   && GET_CODE (PATTERN (i3)) == PARALLEL)
982                 {
983                   rtx i3pat = PATTERN (i3);
984                   int i = XVECLEN (i3pat, 0) - 1;
985                   unsigned int regno = REGNO (XEXP (elt, 0));
986
987                   do
988                     {
989                       rtx i3elt = XVECEXP (i3pat, 0, i);
990
991                       if (GET_CODE (i3elt) == USE
992                           && GET_CODE (XEXP (i3elt, 0)) == REG
993                           && (REGNO (XEXP (i3elt, 0)) == regno
994                               ? reg_set_between_p (XEXP (elt, 0),
995                                                    PREV_INSN (insn), i3)
996                               : regno >= FIRST_PSEUDO_REGISTER))
997                         return 0;
998                     }
999                   while (--i >= 0);
1000                 }
1001               break;
1002
1003               /* We can ignore CLOBBERs.  */
1004             case CLOBBER:
1005               break;
1006
1007             case SET:
1008               /* Ignore SETs whose result isn't used but not those that
1009                  have side-effects.  */
1010               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1011                   && ! side_effects_p (elt))
1012                 break;
1013
1014               /* If we have already found a SET, this is a second one and
1015                  so we cannot combine with this insn.  */
1016               if (set)
1017                 return 0;
1018
1019               set = elt;
1020               break;
1021
1022             default:
1023               /* Anything else means we can't combine.  */
1024               return 0;
1025             }
1026         }
1027
1028       if (set == 0
1029           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1030              so don't do anything with it.  */
1031           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1032         return 0;
1033     }
1034   else
1035     return 0;
1036
1037   if (set == 0)
1038     return 0;
1039
1040   set = expand_field_assignment (set);
1041   src = SET_SRC (set), dest = SET_DEST (set);
1042
1043   /* Don't eliminate a store in the stack pointer.  */
1044   if (dest == stack_pointer_rtx
1045       /* Don't combine with an insn that sets a register to itself if it has
1046          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
1047       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1048       /* Can't merge an ASM_OPERANDS.  */
1049       || GET_CODE (src) == ASM_OPERANDS
1050       /* Can't merge a function call.  */
1051       || GET_CODE (src) == CALL
1052       /* Don't eliminate a function call argument.  */
1053       || (GET_CODE (i3) == CALL_INSN
1054           && (find_reg_fusage (i3, USE, dest)
1055               || (GET_CODE (dest) == REG
1056                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1057                   && global_regs[REGNO (dest)])))
1058       /* Don't substitute into an incremented register.  */
1059       || FIND_REG_INC_NOTE (i3, dest)
1060       || (succ && FIND_REG_INC_NOTE (succ, dest))
1061 #if 0
1062       /* Don't combine the end of a libcall into anything.  */
1063       /* ??? This gives worse code, and appears to be unnecessary, since no
1064          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  Local-alloc does
1065          use REG_RETVAL notes for noconflict blocks, but other code here
1066          makes sure that those insns don't disappear.  */
1067       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1068 #endif
1069       /* Make sure that DEST is not used after SUCC but before I3.  */
1070       || (succ && ! all_adjacent
1071           && reg_used_between_p (dest, succ, i3))
1072       /* Make sure that the value that is to be substituted for the register
1073          does not use any registers whose values alter in between.  However,
1074          If the insns are adjacent, a use can't cross a set even though we
1075          think it might (this can happen for a sequence of insns each setting
1076          the same destination; reg_last_set of that register might point to
1077          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1078          equivalent to the memory so the substitution is valid even if there
1079          are intervening stores.  Also, don't move a volatile asm or
1080          UNSPEC_VOLATILE across any other insns.  */
1081       || (! all_adjacent
1082           && (((GET_CODE (src) != MEM
1083                 || ! find_reg_note (insn, REG_EQUIV, src))
1084                && use_crosses_set_p (src, INSN_CUID (insn)))
1085               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1086               || GET_CODE (src) == UNSPEC_VOLATILE))
1087       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1088          better register allocation by not doing the combine.  */
1089       || find_reg_note (i3, REG_NO_CONFLICT, dest)
1090       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1091       /* Don't combine across a CALL_INSN, because that would possibly
1092          change whether the life span of some REGs crosses calls or not,
1093          and it is a pain to update that information.
1094          Exception: if source is a constant, moving it later can't hurt.
1095          Accept that special case, because it helps -fforce-addr a lot.  */
1096       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1097     return 0;
1098
1099   /* DEST must either be a REG or CC0.  */
1100   if (GET_CODE (dest) == REG)
1101     {
1102       /* If register alignment is being enforced for multi-word items in all
1103          cases except for parameters, it is possible to have a register copy
1104          insn referencing a hard register that is not allowed to contain the
1105          mode being copied and which would not be valid as an operand of most
1106          insns.  Eliminate this problem by not combining with such an insn.
1107
1108          Also, on some machines we don't want to extend the life of a hard
1109          register.  */
1110
1111       if (GET_CODE (src) == REG
1112           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1113                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1114               /* Don't extend the life of a hard register unless it is
1115                  user variable (if we have few registers) or it can't
1116                  fit into the desired register (meaning something special
1117                  is going on).
1118                  Also avoid substituting a return register into I3, because
1119                  reload can't handle a conflict with constraints of other
1120                  inputs.  */
1121               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1122                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1123         return 0;
1124     }
1125   else if (GET_CODE (dest) != CC0)
1126     return 0;
1127
1128   /* Don't substitute for a register intended as a clobberable operand.
1129      Similarly, don't substitute an expression containing a register that
1130      will be clobbered in I3.  */
1131   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1132     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1133       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1134           && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1135                                        src)
1136               || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1137         return 0;
1138
1139   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1140      or not), reject, unless nothing volatile comes between it and I3 */
1141
1142   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1143     {
1144       /* Make sure succ doesn't contain a volatile reference.  */
1145       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1146         return 0;
1147
1148       for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1149         if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1150           return 0;
1151     }
1152
1153   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1154      to be an explicit register variable, and was chosen for a reason.  */
1155
1156   if (GET_CODE (src) == ASM_OPERANDS
1157       && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1158     return 0;
1159
1160   /* If there are any volatile insns between INSN and I3, reject, because
1161      they might affect machine state.  */
1162
1163   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1164     if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1165       return 0;
1166
1167   /* If INSN or I2 contains an autoincrement or autodecrement,
1168      make sure that register is not used between there and I3,
1169      and not already used in I3 either.
1170      Also insist that I3 not be a jump; if it were one
1171      and the incremented register were spilled, we would lose.  */
1172
1173 #ifdef AUTO_INC_DEC
1174   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1175     if (REG_NOTE_KIND (link) == REG_INC
1176         && (GET_CODE (i3) == JUMP_INSN
1177             || reg_used_between_p (XEXP (link, 0), insn, i3)
1178             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1179       return 0;
1180 #endif
1181
1182 #ifdef HAVE_cc0
1183   /* Don't combine an insn that follows a CC0-setting insn.
1184      An insn that uses CC0 must not be separated from the one that sets it.
1185      We do, however, allow I2 to follow a CC0-setting insn if that insn
1186      is passed as I1; in that case it will be deleted also.
1187      We also allow combining in this case if all the insns are adjacent
1188      because that would leave the two CC0 insns adjacent as well.
1189      It would be more logical to test whether CC0 occurs inside I1 or I2,
1190      but that would be much slower, and this ought to be equivalent.  */
1191
1192   p = prev_nonnote_insn (insn);
1193   if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1194       && ! all_adjacent)
1195     return 0;
1196 #endif
1197
1198   /* If we get here, we have passed all the tests and the combination is
1199      to be allowed.  */
1200
1201   *pdest = dest;
1202   *psrc = src;
1203
1204   return 1;
1205 }
1206 \f
1207 /* LOC is the location within I3 that contains its pattern or the component
1208    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1209
1210    One problem is if I3 modifies its output, as opposed to replacing it
1211    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1212    so would produce an insn that is not equivalent to the original insns.
1213
1214    Consider:
1215
1216          (set (reg:DI 101) (reg:DI 100))
1217          (set (subreg:SI (reg:DI 101) 0) <foo>)
1218
1219    This is NOT equivalent to:
1220
1221          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1222                     (set (reg:DI 101) (reg:DI 100))])
1223
1224    Not only does this modify 100 (in which case it might still be valid
1225    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1226
1227    We can also run into a problem if I2 sets a register that I1
1228    uses and I1 gets directly substituted into I3 (not via I2).  In that
1229    case, we would be getting the wrong value of I2DEST into I3, so we
1230    must reject the combination.  This case occurs when I2 and I1 both
1231    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1232    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1233    of a SET must prevent combination from occurring.
1234
1235    Before doing the above check, we first try to expand a field assignment
1236    into a set of logical operations.
1237
1238    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1239    we place a register that is both set and used within I3.  If more than one
1240    such register is detected, we fail.
1241
1242    Return 1 if the combination is valid, zero otherwise.  */
1243
1244 static int
1245 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1246                   int i1_not_in_src, rtx *pi3dest_killed)
1247 {
1248   rtx x = *loc;
1249
1250   if (GET_CODE (x) == SET)
1251     {
1252       rtx set = x ;
1253       rtx dest = SET_DEST (set);
1254       rtx src = SET_SRC (set);
1255       rtx inner_dest = dest;
1256
1257       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1258              || GET_CODE (inner_dest) == SUBREG
1259              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1260         inner_dest = XEXP (inner_dest, 0);
1261
1262       /* Check for the case where I3 modifies its output, as discussed
1263          above.  We don't want to prevent pseudos from being combined
1264          into the address of a MEM, so only prevent the combination if
1265          i1 or i2 set the same MEM.  */
1266       if ((inner_dest != dest &&
1267            (GET_CODE (inner_dest) != MEM
1268             || rtx_equal_p (i2dest, inner_dest)
1269             || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1270            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1271                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1272
1273           /* This is the same test done in can_combine_p except we can't test
1274              all_adjacent; we don't have to, since this instruction will stay
1275              in place, thus we are not considering increasing the lifetime of
1276              INNER_DEST.
1277
1278              Also, if this insn sets a function argument, combining it with
1279              something that might need a spill could clobber a previous
1280              function argument; the all_adjacent test in can_combine_p also
1281              checks this; here, we do a more specific test for this case.  */
1282
1283           || (GET_CODE (inner_dest) == REG
1284               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1285               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1286                                         GET_MODE (inner_dest))))
1287           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1288         return 0;
1289
1290       /* If DEST is used in I3, it is being killed in this insn,
1291          so record that for later.
1292          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1293          STACK_POINTER_REGNUM, since these are always considered to be
1294          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1295       if (pi3dest_killed && GET_CODE (dest) == REG
1296           && reg_referenced_p (dest, PATTERN (i3))
1297           && REGNO (dest) != FRAME_POINTER_REGNUM
1298 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1299           && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1300 #endif
1301 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1302           && (REGNO (dest) != ARG_POINTER_REGNUM
1303               || ! fixed_regs [REGNO (dest)])
1304 #endif
1305           && REGNO (dest) != STACK_POINTER_REGNUM)
1306         {
1307           if (*pi3dest_killed)
1308             return 0;
1309
1310           *pi3dest_killed = dest;
1311         }
1312     }
1313
1314   else if (GET_CODE (x) == PARALLEL)
1315     {
1316       int i;
1317
1318       for (i = 0; i < XVECLEN (x, 0); i++)
1319         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1320                                 i1_not_in_src, pi3dest_killed))
1321           return 0;
1322     }
1323
1324   return 1;
1325 }
1326 \f
1327 /* Return 1 if X is an arithmetic expression that contains a multiplication
1328    and division.  We don't count multiplications by powers of two here.  */
1329
1330 static int
1331 contains_muldiv (rtx x)
1332 {
1333   switch (GET_CODE (x))
1334     {
1335     case MOD:  case DIV:  case UMOD:  case UDIV:
1336       return 1;
1337
1338     case MULT:
1339       return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1340                 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1341     default:
1342       switch (GET_RTX_CLASS (GET_CODE (x)))
1343         {
1344         case 'c':  case '<':  case '2':
1345           return contains_muldiv (XEXP (x, 0))
1346             || contains_muldiv (XEXP (x, 1));
1347
1348         case '1':
1349           return contains_muldiv (XEXP (x, 0));
1350
1351         default:
1352           return 0;
1353         }
1354     }
1355 }
1356 \f
1357 /* Determine whether INSN can be used in a combination.  Return nonzero if
1358    not.  This is used in try_combine to detect early some cases where we
1359    can't perform combinations.  */
1360
1361 static int
1362 cant_combine_insn_p (rtx insn)
1363 {
1364   rtx set;
1365   rtx src, dest;
1366
1367   /* If this isn't really an insn, we can't do anything.
1368      This can occur when flow deletes an insn that it has merged into an
1369      auto-increment address.  */
1370   if (! INSN_P (insn))
1371     return 1;
1372
1373   /* Never combine loads and stores involving hard regs that are likely
1374      to be spilled.  The register allocator can usually handle such
1375      reg-reg moves by tying.  If we allow the combiner to make
1376      substitutions of likely-spilled regs, we may abort in reload.
1377      As an exception, we allow combinations involving fixed regs; these are
1378      not available to the register allocator so there's no risk involved.  */
1379
1380   set = single_set (insn);
1381   if (! set)
1382     return 0;
1383   src = SET_SRC (set);
1384   dest = SET_DEST (set);
1385   if (GET_CODE (src) == SUBREG)
1386     src = SUBREG_REG (src);
1387   if (GET_CODE (dest) == SUBREG)
1388     dest = SUBREG_REG (dest);
1389   if (REG_P (src) && REG_P (dest)
1390       && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1391            && ! fixed_regs[REGNO (src)]
1392            && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1393           || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1394               && ! fixed_regs[REGNO (dest)]
1395               && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1396     return 1;
1397
1398   return 0;
1399 }
1400
1401 /* Adjust INSN after we made a change to its destination.
1402
1403    Changing the destination can invalidate notes that say something about
1404    the results of the insn and a LOG_LINK pointing to the insn.  */
1405
1406 static void
1407 adjust_for_new_dest (rtx insn)
1408 {
1409   rtx *loc;
1410
1411   /* For notes, be conservative and simply remove them.  */
1412   loc = &REG_NOTES (insn);
1413   while (*loc)
1414     {
1415       enum reg_note kind = REG_NOTE_KIND (*loc);
1416       if (kind == REG_EQUAL || kind == REG_EQUIV)
1417         *loc = XEXP (*loc, 1);
1418       else
1419         loc = &XEXP (*loc, 1);
1420     }
1421
1422   /* The new insn will have a destination that was previously the destination
1423      of an insn just above it.  Call distribute_links to make a LOG_LINK from
1424      the next use of that destination.  */
1425   distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1426 }
1427
1428 /* Try to combine the insns I1 and I2 into I3.
1429    Here I1 and I2 appear earlier than I3.
1430    I1 can be zero; then we combine just I2 into I3.
1431
1432    If we are combining three insns and the resulting insn is not recognized,
1433    try splitting it into two insns.  If that happens, I2 and I3 are retained
1434    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1435    are pseudo-deleted.
1436
1437    Return 0 if the combination does not work.  Then nothing is changed.
1438    If we did the combination, return the insn at which combine should
1439    resume scanning.
1440
1441    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1442    new direct jump instruction.  */
1443
1444 static rtx
1445 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1446 {
1447   /* New patterns for I3 and I2, respectively.  */
1448   rtx newpat, newi2pat = 0;
1449   int substed_i2 = 0, substed_i1 = 0;
1450   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1451   int added_sets_1, added_sets_2;
1452   /* Total number of SETs to put into I3.  */
1453   int total_sets;
1454   /* Nonzero is I2's body now appears in I3.  */
1455   int i2_is_used;
1456   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1457   int insn_code_number, i2_code_number = 0, other_code_number = 0;
1458   /* Contains I3 if the destination of I3 is used in its source, which means
1459      that the old life of I3 is being killed.  If that usage is placed into
1460      I2 and not in I3, a REG_DEAD note must be made.  */
1461   rtx i3dest_killed = 0;
1462   /* SET_DEST and SET_SRC of I2 and I1.  */
1463   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1464   /* PATTERN (I2), or a copy of it in certain cases.  */
1465   rtx i2pat;
1466   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1467   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1468   int i1_feeds_i3 = 0;
1469   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1470   rtx new_i3_notes, new_i2_notes;
1471   /* Notes that we substituted I3 into I2 instead of the normal case.  */
1472   int i3_subst_into_i2 = 0;
1473   /* Notes that I1, I2 or I3 is a MULT operation.  */
1474   int have_mult = 0;
1475
1476   int maxreg;
1477   rtx temp;
1478   rtx link;
1479   int i;
1480
1481   /* Exit early if one of the insns involved can't be used for
1482      combinations.  */
1483   if (cant_combine_insn_p (i3)
1484       || cant_combine_insn_p (i2)
1485       || (i1 && cant_combine_insn_p (i1))
1486       /* We also can't do anything if I3 has a
1487          REG_LIBCALL note since we don't want to disrupt the contiguity of a
1488          libcall.  */
1489 #if 0
1490       /* ??? This gives worse code, and appears to be unnecessary, since no
1491          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  */
1492       || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1493 #endif
1494       )
1495     return 0;
1496
1497   combine_attempts++;
1498   undobuf.other_insn = 0;
1499
1500   /* Reset the hard register usage information.  */
1501   CLEAR_HARD_REG_SET (newpat_used_regs);
1502
1503   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1504      code below, set I1 to be the earlier of the two insns.  */
1505   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1506     temp = i1, i1 = i2, i2 = temp;
1507
1508   added_links_insn = 0;
1509
1510   /* First check for one important special-case that the code below will
1511      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
1512      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1513      we may be able to replace that destination with the destination of I3.
1514      This occurs in the common code where we compute both a quotient and
1515      remainder into a structure, in which case we want to do the computation
1516      directly into the structure to avoid register-register copies.
1517
1518      Note that this case handles both multiple sets in I2 and also
1519      cases where I2 has a number of CLOBBER or PARALLELs.
1520
1521      We make very conservative checks below and only try to handle the
1522      most common cases of this.  For example, we only handle the case
1523      where I2 and I3 are adjacent to avoid making difficult register
1524      usage tests.  */
1525
1526   if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1527       && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1528       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1529       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1530       && GET_CODE (PATTERN (i2)) == PARALLEL
1531       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1532       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1533          below would need to check what is inside (and reg_overlap_mentioned_p
1534          doesn't support those codes anyway).  Don't allow those destinations;
1535          the resulting insn isn't likely to be recognized anyway.  */
1536       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1537       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1538       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1539                                     SET_DEST (PATTERN (i3)))
1540       && next_real_insn (i2) == i3)
1541     {
1542       rtx p2 = PATTERN (i2);
1543
1544       /* Make sure that the destination of I3,
1545          which we are going to substitute into one output of I2,
1546          is not used within another output of I2.  We must avoid making this:
1547          (parallel [(set (mem (reg 69)) ...)
1548                     (set (reg 69) ...)])
1549          which is not well-defined as to order of actions.
1550          (Besides, reload can't handle output reloads for this.)
1551
1552          The problem can also happen if the dest of I3 is a memory ref,
1553          if another dest in I2 is an indirect memory ref.  */
1554       for (i = 0; i < XVECLEN (p2, 0); i++)
1555         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1556              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1557             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1558                                         SET_DEST (XVECEXP (p2, 0, i))))
1559           break;
1560
1561       if (i == XVECLEN (p2, 0))
1562         for (i = 0; i < XVECLEN (p2, 0); i++)
1563           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1564                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1565               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1566             {
1567               combine_merges++;
1568
1569               subst_insn = i3;
1570               subst_low_cuid = INSN_CUID (i2);
1571
1572               added_sets_2 = added_sets_1 = 0;
1573               i2dest = SET_SRC (PATTERN (i3));
1574
1575               /* Replace the dest in I2 with our dest and make the resulting
1576                  insn the new pattern for I3.  Then skip to where we
1577                  validate the pattern.  Everything was set up above.  */
1578               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1579                      SET_DEST (PATTERN (i3)));
1580
1581               newpat = p2;
1582               i3_subst_into_i2 = 1;
1583               goto validate_replacement;
1584             }
1585     }
1586
1587   /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1588      one of those words to another constant, merge them by making a new
1589      constant.  */
1590   if (i1 == 0
1591       && (temp = single_set (i2)) != 0
1592       && (GET_CODE (SET_SRC (temp)) == CONST_INT
1593           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1594       && GET_CODE (SET_DEST (temp)) == REG
1595       && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1596       && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1597       && GET_CODE (PATTERN (i3)) == SET
1598       && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1599       && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1600       && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1601       && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1602       && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1603     {
1604       HOST_WIDE_INT lo, hi;
1605
1606       if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1607         lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1608       else
1609         {
1610           lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1611           hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1612         }
1613
1614       if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1615         {
1616           /* We don't handle the case of the target word being wider
1617              than a host wide int.  */
1618           if (HOST_BITS_PER_WIDE_INT < BITS_PER_WORD)
1619             abort ();
1620
1621           lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1622           lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1623                  & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1624         }
1625       else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1626         hi = INTVAL (SET_SRC (PATTERN (i3)));
1627       else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1628         {
1629           int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1630                              >> (HOST_BITS_PER_WIDE_INT - 1));
1631
1632           lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1633                    (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1634           lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1635                  (INTVAL (SET_SRC (PATTERN (i3)))));
1636           if (hi == sign)
1637             hi = lo < 0 ? -1 : 0;
1638         }
1639       else
1640         /* We don't handle the case of the higher word not fitting
1641            entirely in either hi or lo.  */
1642         abort ();
1643
1644       combine_merges++;
1645       subst_insn = i3;
1646       subst_low_cuid = INSN_CUID (i2);
1647       added_sets_2 = added_sets_1 = 0;
1648       i2dest = SET_DEST (temp);
1649
1650       SUBST (SET_SRC (temp),
1651              immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1652
1653       newpat = PATTERN (i2);
1654       goto validate_replacement;
1655     }
1656
1657 #ifndef HAVE_cc0
1658   /* If we have no I1 and I2 looks like:
1659         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1660                    (set Y OP)])
1661      make up a dummy I1 that is
1662         (set Y OP)
1663      and change I2 to be
1664         (set (reg:CC X) (compare:CC Y (const_int 0)))
1665
1666      (We can ignore any trailing CLOBBERs.)
1667
1668      This undoes a previous combination and allows us to match a branch-and-
1669      decrement insn.  */
1670
1671   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1672       && XVECLEN (PATTERN (i2), 0) >= 2
1673       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1674       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1675           == MODE_CC)
1676       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1677       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1678       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1679       && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1680       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1681                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1682     {
1683       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1684         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1685           break;
1686
1687       if (i == 1)
1688         {
1689           /* We make I1 with the same INSN_UID as I2.  This gives it
1690              the same INSN_CUID for value tracking.  Our fake I1 will
1691              never appear in the insn stream so giving it the same INSN_UID
1692              as I2 will not cause a problem.  */
1693
1694           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1695                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1696                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1697                              NULL_RTX);
1698
1699           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1700           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1701                  SET_DEST (PATTERN (i1)));
1702         }
1703     }
1704 #endif
1705
1706   /* Verify that I2 and I1 are valid for combining.  */
1707   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1708       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1709     {
1710       undo_all ();
1711       return 0;
1712     }
1713
1714   /* Record whether I2DEST is used in I2SRC and similarly for the other
1715      cases.  Knowing this will help in register status updating below.  */
1716   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1717   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1718   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1719
1720   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
1721      in I2SRC.  */
1722   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1723
1724   /* Ensure that I3's pattern can be the destination of combines.  */
1725   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1726                           i1 && i2dest_in_i1src && i1_feeds_i3,
1727                           &i3dest_killed))
1728     {
1729       undo_all ();
1730       return 0;
1731     }
1732
1733   /* See if any of the insns is a MULT operation.  Unless one is, we will
1734      reject a combination that is, since it must be slower.  Be conservative
1735      here.  */
1736   if (GET_CODE (i2src) == MULT
1737       || (i1 != 0 && GET_CODE (i1src) == MULT)
1738       || (GET_CODE (PATTERN (i3)) == SET
1739           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1740     have_mult = 1;
1741
1742   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1743      We used to do this EXCEPT in one case: I3 has a post-inc in an
1744      output operand.  However, that exception can give rise to insns like
1745         mov r3,(r3)+
1746      which is a famous insn on the PDP-11 where the value of r3 used as the
1747      source was model-dependent.  Avoid this sort of thing.  */
1748
1749 #if 0
1750   if (!(GET_CODE (PATTERN (i3)) == SET
1751         && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1752         && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1753         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1754             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1755     /* It's not the exception.  */
1756 #endif
1757 #ifdef AUTO_INC_DEC
1758     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1759       if (REG_NOTE_KIND (link) == REG_INC
1760           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1761               || (i1 != 0
1762                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1763         {
1764           undo_all ();
1765           return 0;
1766         }
1767 #endif
1768
1769   /* See if the SETs in I1 or I2 need to be kept around in the merged
1770      instruction: whenever the value set there is still needed past I3.
1771      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1772
1773      For the SET in I1, we have two cases:  If I1 and I2 independently
1774      feed into I3, the set in I1 needs to be kept around if I1DEST dies
1775      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
1776      in I1 needs to be kept around unless I1DEST dies or is set in either
1777      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
1778      I1DEST.  If so, we know I1 feeds into I2.  */
1779
1780   added_sets_2 = ! dead_or_set_p (i3, i2dest);
1781
1782   added_sets_1
1783     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1784                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1785
1786   /* If the set in I2 needs to be kept around, we must make a copy of
1787      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1788      PATTERN (I2), we are only substituting for the original I1DEST, not into
1789      an already-substituted copy.  This also prevents making self-referential
1790      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1791      I2DEST.  */
1792
1793   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1794            ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1795            : PATTERN (i2));
1796
1797   if (added_sets_2)
1798     i2pat = copy_rtx (i2pat);
1799
1800   combine_merges++;
1801
1802   /* Substitute in the latest insn for the regs set by the earlier ones.  */
1803
1804   maxreg = max_reg_num ();
1805
1806   subst_insn = i3;
1807
1808   /* It is possible that the source of I2 or I1 may be performing an
1809      unneeded operation, such as a ZERO_EXTEND of something that is known
1810      to have the high part zero.  Handle that case by letting subst look at
1811      the innermost one of them.
1812
1813      Another way to do this would be to have a function that tries to
1814      simplify a single insn instead of merging two or more insns.  We don't
1815      do this because of the potential of infinite loops and because
1816      of the potential extra memory required.  However, doing it the way
1817      we are is a bit of a kludge and doesn't catch all cases.
1818
1819      But only do this if -fexpensive-optimizations since it slows things down
1820      and doesn't usually win.  */
1821
1822   if (flag_expensive_optimizations)
1823     {
1824       /* Pass pc_rtx so no substitutions are done, just simplifications.
1825          The cases that we are interested in here do not involve the few
1826          cases were is_replaced is checked.  */
1827       if (i1)
1828         {
1829           subst_low_cuid = INSN_CUID (i1);
1830           i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1831         }
1832       else
1833         {
1834           subst_low_cuid = INSN_CUID (i2);
1835           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1836         }
1837     }
1838
1839 #ifndef HAVE_cc0
1840   /* Many machines that don't use CC0 have insns that can both perform an
1841      arithmetic operation and set the condition code.  These operations will
1842      be represented as a PARALLEL with the first element of the vector
1843      being a COMPARE of an arithmetic operation with the constant zero.
1844      The second element of the vector will set some pseudo to the result
1845      of the same arithmetic operation.  If we simplify the COMPARE, we won't
1846      match such a pattern and so will generate an extra insn.   Here we test
1847      for this case, where both the comparison and the operation result are
1848      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1849      I2SRC.  Later we will make the PARALLEL that contains I2.  */
1850
1851   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1852       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1853       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1854       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1855     {
1856 #ifdef SELECT_CC_MODE
1857       rtx *cc_use;
1858       enum machine_mode compare_mode;
1859 #endif
1860
1861       newpat = PATTERN (i3);
1862       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1863
1864       i2_is_used = 1;
1865
1866 #ifdef SELECT_CC_MODE
1867       /* See if a COMPARE with the operand we substituted in should be done
1868          with the mode that is currently being used.  If not, do the same
1869          processing we do in `subst' for a SET; namely, if the destination
1870          is used only once, try to replace it with a register of the proper
1871          mode and also replace the COMPARE.  */
1872       if (undobuf.other_insn == 0
1873           && (cc_use = find_single_use (SET_DEST (newpat), i3,
1874                                         &undobuf.other_insn))
1875           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1876                                               i2src, const0_rtx))
1877               != GET_MODE (SET_DEST (newpat))))
1878         {
1879           unsigned int regno = REGNO (SET_DEST (newpat));
1880           rtx new_dest = gen_rtx_REG (compare_mode, regno);
1881
1882           if (regno < FIRST_PSEUDO_REGISTER
1883               || (REG_N_SETS (regno) == 1 && ! added_sets_2
1884                   && ! REG_USERVAR_P (SET_DEST (newpat))))
1885             {
1886               if (regno >= FIRST_PSEUDO_REGISTER)
1887                 SUBST (regno_reg_rtx[regno], new_dest);
1888
1889               SUBST (SET_DEST (newpat), new_dest);
1890               SUBST (XEXP (*cc_use, 0), new_dest);
1891               SUBST (SET_SRC (newpat),
1892                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
1893             }
1894           else
1895             undobuf.other_insn = 0;
1896         }
1897 #endif
1898     }
1899   else
1900 #endif
1901     {
1902       n_occurrences = 0;                /* `subst' counts here */
1903
1904       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1905          need to make a unique copy of I2SRC each time we substitute it
1906          to avoid self-referential rtl.  */
1907
1908       subst_low_cuid = INSN_CUID (i2);
1909       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1910                       ! i1_feeds_i3 && i1dest_in_i1src);
1911       substed_i2 = 1;
1912
1913       /* Record whether i2's body now appears within i3's body.  */
1914       i2_is_used = n_occurrences;
1915     }
1916
1917   /* If we already got a failure, don't try to do more.  Otherwise,
1918      try to substitute in I1 if we have it.  */
1919
1920   if (i1 && GET_CODE (newpat) != CLOBBER)
1921     {
1922       /* Before we can do this substitution, we must redo the test done
1923          above (see detailed comments there) that ensures  that I1DEST
1924          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
1925
1926       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1927                               0, (rtx*) 0))
1928         {
1929           undo_all ();
1930           return 0;
1931         }
1932
1933       n_occurrences = 0;
1934       subst_low_cuid = INSN_CUID (i1);
1935       newpat = subst (newpat, i1dest, i1src, 0, 0);
1936       substed_i1 = 1;
1937     }
1938
1939   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
1940      to count all the ways that I2SRC and I1SRC can be used.  */
1941   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1942        && i2_is_used + added_sets_2 > 1)
1943       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1944           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1945               > 1))
1946       /* Fail if we tried to make a new register (we used to abort, but there's
1947          really no reason to).  */
1948       || max_reg_num () != maxreg
1949       /* Fail if we couldn't do something and have a CLOBBER.  */
1950       || GET_CODE (newpat) == CLOBBER
1951       /* Fail if this new pattern is a MULT and we didn't have one before
1952          at the outer level.  */
1953       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1954           && ! have_mult))
1955     {
1956       undo_all ();
1957       return 0;
1958     }
1959
1960   /* If the actions of the earlier insns must be kept
1961      in addition to substituting them into the latest one,
1962      we must make a new PARALLEL for the latest insn
1963      to hold additional the SETs.  */
1964
1965   if (added_sets_1 || added_sets_2)
1966     {
1967       combine_extras++;
1968
1969       if (GET_CODE (newpat) == PARALLEL)
1970         {
1971           rtvec old = XVEC (newpat, 0);
1972           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1973           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1974           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
1975                   sizeof (old->elem[0]) * old->num_elem);
1976         }
1977       else
1978         {
1979           rtx old = newpat;
1980           total_sets = 1 + added_sets_1 + added_sets_2;
1981           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1982           XVECEXP (newpat, 0, 0) = old;
1983         }
1984
1985       if (added_sets_1)
1986         XVECEXP (newpat, 0, --total_sets)
1987           = (GET_CODE (PATTERN (i1)) == PARALLEL
1988              ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
1989
1990       if (added_sets_2)
1991         {
1992           /* If there is no I1, use I2's body as is.  We used to also not do
1993              the subst call below if I2 was substituted into I3,
1994              but that could lose a simplification.  */
1995           if (i1 == 0)
1996             XVECEXP (newpat, 0, --total_sets) = i2pat;
1997           else
1998             /* See comment where i2pat is assigned.  */
1999             XVECEXP (newpat, 0, --total_sets)
2000               = subst (i2pat, i1dest, i1src, 0, 0);
2001         }
2002     }
2003
2004   /* We come here when we are replacing a destination in I2 with the
2005      destination of I3.  */
2006  validate_replacement:
2007
2008   /* Note which hard regs this insn has as inputs.  */
2009   mark_used_regs_combine (newpat);
2010
2011   /* Is the result of combination a valid instruction?  */
2012   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2013
2014   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2015      the second SET's destination is a register that is unused.  In that case,
2016      we just need the first SET.   This can occur when simplifying a divmod
2017      insn.  We *must* test for this case here because the code below that
2018      splits two independent SETs doesn't handle this case correctly when it
2019      updates the register status.  Also check the case where the first
2020      SET's destination is unused.  That would not cause incorrect code, but
2021      does cause an unneeded insn to remain.  */
2022
2023   if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2024       && XVECLEN (newpat, 0) == 2
2025       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2026       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2027       && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
2028       && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
2029       && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
2030       && asm_noperands (newpat) < 0)
2031     {
2032       newpat = XVECEXP (newpat, 0, 0);
2033       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2034     }
2035
2036   else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2037            && XVECLEN (newpat, 0) == 2
2038            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2039            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2040            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
2041            && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
2042            && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
2043            && asm_noperands (newpat) < 0)
2044     {
2045       newpat = XVECEXP (newpat, 0, 1);
2046       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2047  
2048       if (insn_code_number >= 0)
2049         {
2050           /* If we will be able to accept this, we have made a change to the
2051              destination of I3.  This requires us to do a few adjustments.  */
2052           PATTERN (i3) = newpat;
2053           adjust_for_new_dest (i3);
2054         }
2055     }
2056
2057   /* If we were combining three insns and the result is a simple SET
2058      with no ASM_OPERANDS that wasn't recognized, try to split it into two
2059      insns.  There are two ways to do this.  It can be split using a
2060      machine-specific method (like when you have an addition of a large
2061      constant) or by combine in the function find_split_point.  */
2062
2063   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2064       && asm_noperands (newpat) < 0)
2065     {
2066       rtx m_split, *split;
2067       rtx ni2dest = i2dest;
2068
2069       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
2070          use I2DEST as a scratch register will help.  In the latter case,
2071          convert I2DEST to the mode of the source of NEWPAT if we can.  */
2072
2073       m_split = split_insns (newpat, i3);
2074
2075       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2076          inputs of NEWPAT.  */
2077
2078       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2079          possible to try that as a scratch reg.  This would require adding
2080          more code to make it work though.  */
2081
2082       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2083         {
2084           /* If I2DEST is a hard register or the only use of a pseudo,
2085              we can change its mode.  */
2086           if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2087               && GET_MODE (SET_DEST (newpat)) != VOIDmode
2088               && GET_CODE (i2dest) == REG
2089               && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2090                   || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2091                       && ! REG_USERVAR_P (i2dest))))
2092             ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2093                                    REGNO (i2dest));
2094
2095           m_split = split_insns (gen_rtx_PARALLEL
2096                                  (VOIDmode,
2097                                   gen_rtvec (2, newpat,
2098                                              gen_rtx_CLOBBER (VOIDmode,
2099                                                               ni2dest))),
2100                                  i3);
2101           /* If the split with the mode-changed register didn't work, try
2102              the original register.  */
2103           if (! m_split && ni2dest != i2dest)
2104             {
2105               ni2dest = i2dest;
2106               m_split = split_insns (gen_rtx_PARALLEL
2107                                      (VOIDmode,
2108                                       gen_rtvec (2, newpat,
2109                                                  gen_rtx_CLOBBER (VOIDmode,
2110                                                                   i2dest))),
2111                                      i3);
2112             }
2113         }
2114
2115       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2116         {
2117           m_split = PATTERN (m_split);
2118           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2119           if (insn_code_number >= 0)
2120             newpat = m_split;
2121         }
2122       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2123                && (next_real_insn (i2) == i3
2124                    || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2125         {
2126           rtx i2set, i3set;
2127           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2128           newi2pat = PATTERN (m_split);
2129
2130           i3set = single_set (NEXT_INSN (m_split));
2131           i2set = single_set (m_split);
2132
2133           /* In case we changed the mode of I2DEST, replace it in the
2134              pseudo-register table here.  We can't do it above in case this
2135              code doesn't get executed and we do a split the other way.  */
2136
2137           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2138             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2139
2140           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2141
2142           /* If I2 or I3 has multiple SETs, we won't know how to track
2143              register status, so don't use these insns.  If I2's destination
2144              is used between I2 and I3, we also can't use these insns.  */
2145
2146           if (i2_code_number >= 0 && i2set && i3set
2147               && (next_real_insn (i2) == i3
2148                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2149             insn_code_number = recog_for_combine (&newi3pat, i3,
2150                                                   &new_i3_notes);
2151           if (insn_code_number >= 0)
2152             newpat = newi3pat;
2153
2154           /* It is possible that both insns now set the destination of I3.
2155              If so, we must show an extra use of it.  */
2156
2157           if (insn_code_number >= 0)
2158             {
2159               rtx new_i3_dest = SET_DEST (i3set);
2160               rtx new_i2_dest = SET_DEST (i2set);
2161
2162               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2163                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2164                      || GET_CODE (new_i3_dest) == SUBREG)
2165                 new_i3_dest = XEXP (new_i3_dest, 0);
2166
2167               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2168                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2169                      || GET_CODE (new_i2_dest) == SUBREG)
2170                 new_i2_dest = XEXP (new_i2_dest, 0);
2171
2172               if (GET_CODE (new_i3_dest) == REG
2173                   && GET_CODE (new_i2_dest) == REG
2174                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2175                 REG_N_SETS (REGNO (new_i2_dest))++;
2176             }
2177         }
2178
2179       /* If we can split it and use I2DEST, go ahead and see if that
2180          helps things be recognized.  Verify that none of the registers
2181          are set between I2 and I3.  */
2182       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2183 #ifdef HAVE_cc0
2184           && GET_CODE (i2dest) == REG
2185 #endif
2186           /* We need I2DEST in the proper mode.  If it is a hard register
2187              or the only use of a pseudo, we can change its mode.  */
2188           && (GET_MODE (*split) == GET_MODE (i2dest)
2189               || GET_MODE (*split) == VOIDmode
2190               || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2191               || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2192                   && ! REG_USERVAR_P (i2dest)))
2193           && (next_real_insn (i2) == i3
2194               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2195           /* We can't overwrite I2DEST if its value is still used by
2196              NEWPAT.  */
2197           && ! reg_referenced_p (i2dest, newpat))
2198         {
2199           rtx newdest = i2dest;
2200           enum rtx_code split_code = GET_CODE (*split);
2201           enum machine_mode split_mode = GET_MODE (*split);
2202
2203           /* Get NEWDEST as a register in the proper mode.  We have already
2204              validated that we can do this.  */
2205           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2206             {
2207               newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2208
2209               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2210                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2211             }
2212
2213           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2214              an ASHIFT.  This can occur if it was inside a PLUS and hence
2215              appeared to be a memory address.  This is a kludge.  */
2216           if (split_code == MULT
2217               && GET_CODE (XEXP (*split, 1)) == CONST_INT
2218               && INTVAL (XEXP (*split, 1)) > 0
2219               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2220             {
2221               SUBST (*split, gen_rtx_ASHIFT (split_mode,
2222                                              XEXP (*split, 0), GEN_INT (i)));
2223               /* Update split_code because we may not have a multiply
2224                  anymore.  */
2225               split_code = GET_CODE (*split);
2226             }
2227
2228 #ifdef INSN_SCHEDULING
2229           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2230              be written as a ZERO_EXTEND.  */
2231           if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2232             {
2233 #ifdef LOAD_EXTEND_OP
2234               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2235                  what it really is.  */
2236               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2237                   == SIGN_EXTEND)
2238                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2239                                                     SUBREG_REG (*split)));
2240               else
2241 #endif
2242                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2243                                                     SUBREG_REG (*split)));
2244             }
2245 #endif
2246
2247           newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2248           SUBST (*split, newdest);
2249           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2250
2251           /* If the split point was a MULT and we didn't have one before,
2252              don't use one now.  */
2253           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2254             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2255         }
2256     }
2257
2258   /* Check for a case where we loaded from memory in a narrow mode and
2259      then sign extended it, but we need both registers.  In that case,
2260      we have a PARALLEL with both loads from the same memory location.
2261      We can split this into a load from memory followed by a register-register
2262      copy.  This saves at least one insn, more if register allocation can
2263      eliminate the copy.
2264
2265      We cannot do this if the destination of the first assignment is a
2266      condition code register or cc0.  We eliminate this case by making sure
2267      the SET_DEST and SET_SRC have the same mode.
2268
2269      We cannot do this if the destination of the second assignment is
2270      a register that we have already assumed is zero-extended.  Similarly
2271      for a SUBREG of such a register.  */
2272
2273   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2274            && GET_CODE (newpat) == PARALLEL
2275            && XVECLEN (newpat, 0) == 2
2276            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2277            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2278            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2279                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2280            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2281            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2282                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2283            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2284                                    INSN_CUID (i2))
2285            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2286            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2287            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2288                  (GET_CODE (temp) == REG
2289                   && reg_nonzero_bits[REGNO (temp)] != 0
2290                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2291                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2292                   && (reg_nonzero_bits[REGNO (temp)]
2293                       != GET_MODE_MASK (word_mode))))
2294            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2295                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2296                      (GET_CODE (temp) == REG
2297                       && reg_nonzero_bits[REGNO (temp)] != 0
2298                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2299                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2300                       && (reg_nonzero_bits[REGNO (temp)]
2301                           != GET_MODE_MASK (word_mode)))))
2302            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2303                                          SET_SRC (XVECEXP (newpat, 0, 1)))
2304            && ! find_reg_note (i3, REG_UNUSED,
2305                                SET_DEST (XVECEXP (newpat, 0, 0))))
2306     {
2307       rtx ni2dest;
2308
2309       newi2pat = XVECEXP (newpat, 0, 0);
2310       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2311       newpat = XVECEXP (newpat, 0, 1);
2312       SUBST (SET_SRC (newpat),
2313              gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2314       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2315
2316       if (i2_code_number >= 0)
2317         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2318
2319       if (insn_code_number >= 0)
2320         {
2321           rtx insn;
2322           rtx link;
2323
2324           /* If we will be able to accept this, we have made a change to the
2325              destination of I3.  This requires us to do a few adjustments.  */
2326           PATTERN (i3) = newpat;
2327           adjust_for_new_dest (i3);
2328
2329           /* I3 now uses what used to be its destination and which is
2330              now I2's destination.  That means we need a LOG_LINK from
2331              I3 to I2.  But we used to have one, so we still will.
2332
2333              However, some later insn might be using I2's dest and have
2334              a LOG_LINK pointing at I3.  We must remove this link.
2335              The simplest way to remove the link is to point it at I1,
2336              which we know will be a NOTE.  */
2337
2338           for (insn = NEXT_INSN (i3);
2339                insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2340                         || insn != BB_HEAD (this_basic_block->next_bb));
2341                insn = NEXT_INSN (insn))
2342             {
2343               if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2344                 {
2345                   for (link = LOG_LINKS (insn); link;
2346                        link = XEXP (link, 1))
2347                     if (XEXP (link, 0) == i3)
2348                       XEXP (link, 0) = i1;
2349
2350                   break;
2351                 }
2352             }
2353         }
2354     }
2355
2356   /* Similarly, check for a case where we have a PARALLEL of two independent
2357      SETs but we started with three insns.  In this case, we can do the sets
2358      as two separate insns.  This case occurs when some SET allows two
2359      other insns to combine, but the destination of that SET is still live.  */
2360
2361   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2362            && GET_CODE (newpat) == PARALLEL
2363            && XVECLEN (newpat, 0) == 2
2364            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2365            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2366            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2367            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2368            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2369            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2370            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2371                                    INSN_CUID (i2))
2372            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
2373            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2374            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2375            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2376                                   XVECEXP (newpat, 0, 0))
2377            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2378                                   XVECEXP (newpat, 0, 1))
2379            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2380                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2381     {
2382       /* Normally, it doesn't matter which of the two is done first,
2383          but it does if one references cc0.  In that case, it has to
2384          be first.  */
2385 #ifdef HAVE_cc0
2386       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2387         {
2388           newi2pat = XVECEXP (newpat, 0, 0);
2389           newpat = XVECEXP (newpat, 0, 1);
2390         }
2391       else
2392 #endif
2393         {
2394           newi2pat = XVECEXP (newpat, 0, 1);
2395           newpat = XVECEXP (newpat, 0, 0);
2396         }
2397
2398       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2399
2400       if (i2_code_number >= 0)
2401         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2402     }
2403
2404   /* If it still isn't recognized, fail and change things back the way they
2405      were.  */
2406   if ((insn_code_number < 0
2407        /* Is the result a reasonable ASM_OPERANDS?  */
2408        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2409     {
2410       undo_all ();
2411       return 0;
2412     }
2413
2414   /* If we had to change another insn, make sure it is valid also.  */
2415   if (undobuf.other_insn)
2416     {
2417       rtx other_pat = PATTERN (undobuf.other_insn);
2418       rtx new_other_notes;
2419       rtx note, next;
2420
2421       CLEAR_HARD_REG_SET (newpat_used_regs);
2422
2423       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2424                                              &new_other_notes);
2425
2426       if (other_code_number < 0 && ! check_asm_operands (other_pat))
2427         {
2428           undo_all ();
2429           return 0;
2430         }
2431
2432       PATTERN (undobuf.other_insn) = other_pat;
2433
2434       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2435          are still valid.  Then add any non-duplicate notes added by
2436          recog_for_combine.  */
2437       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2438         {
2439           next = XEXP (note, 1);
2440
2441           if (REG_NOTE_KIND (note) == REG_UNUSED
2442               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2443             {
2444               if (GET_CODE (XEXP (note, 0)) == REG)
2445                 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2446
2447               remove_note (undobuf.other_insn, note);
2448             }
2449         }
2450
2451       for (note = new_other_notes; note; note = XEXP (note, 1))
2452         if (GET_CODE (XEXP (note, 0)) == REG)
2453           REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2454
2455       distribute_notes (new_other_notes, undobuf.other_insn,
2456                         undobuf.other_insn, NULL_RTX);
2457     }
2458 #ifdef HAVE_cc0
2459   /* If I2 is the setter CC0 and I3 is the user CC0 then check whether
2460      they are adjacent to each other or not.  */
2461   {
2462     rtx p = prev_nonnote_insn (i3);
2463     if (p && p != i2 && GET_CODE (p) == INSN && newi2pat
2464         && sets_cc0_p (newi2pat))
2465       {
2466         undo_all ();
2467         return 0;
2468       }
2469   }
2470 #endif
2471
2472   /* We now know that we can do this combination.  Merge the insns and
2473      update the status of registers and LOG_LINKS.  */
2474
2475   {
2476     rtx i3notes, i2notes, i1notes = 0;
2477     rtx i3links, i2links, i1links = 0;
2478     rtx midnotes = 0;
2479     unsigned int regno;
2480
2481     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2482        clear them.  */
2483     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2484     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2485     if (i1)
2486       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2487
2488     /* Ensure that we do not have something that should not be shared but
2489        occurs multiple times in the new insns.  Check this by first
2490        resetting all the `used' flags and then copying anything is shared.  */
2491
2492     reset_used_flags (i3notes);
2493     reset_used_flags (i2notes);
2494     reset_used_flags (i1notes);
2495     reset_used_flags (newpat);
2496     reset_used_flags (newi2pat);
2497     if (undobuf.other_insn)
2498       reset_used_flags (PATTERN (undobuf.other_insn));
2499
2500     i3notes = copy_rtx_if_shared (i3notes);
2501     i2notes = copy_rtx_if_shared (i2notes);
2502     i1notes = copy_rtx_if_shared (i1notes);
2503     newpat = copy_rtx_if_shared (newpat);
2504     newi2pat = copy_rtx_if_shared (newi2pat);
2505     if (undobuf.other_insn)
2506       reset_used_flags (PATTERN (undobuf.other_insn));
2507
2508     INSN_CODE (i3) = insn_code_number;
2509     PATTERN (i3) = newpat;
2510
2511     if (GET_CODE (i3) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (i3))
2512       {
2513         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2514
2515         reset_used_flags (call_usage);
2516         call_usage = copy_rtx (call_usage);
2517
2518         if (substed_i2)
2519           replace_rtx (call_usage, i2dest, i2src);
2520
2521         if (substed_i1)
2522           replace_rtx (call_usage, i1dest, i1src);
2523
2524         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2525       }
2526
2527     if (undobuf.other_insn)
2528       INSN_CODE (undobuf.other_insn) = other_code_number;
2529
2530     /* We had one special case above where I2 had more than one set and
2531        we replaced a destination of one of those sets with the destination
2532        of I3.  In that case, we have to update LOG_LINKS of insns later
2533        in this basic block.  Note that this (expensive) case is rare.
2534
2535        Also, in this case, we must pretend that all REG_NOTEs for I2
2536        actually came from I3, so that REG_UNUSED notes from I2 will be
2537        properly handled.  */
2538
2539     if (i3_subst_into_i2)
2540       {
2541         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2542           if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2543               && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2544               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2545               && ! find_reg_note (i2, REG_UNUSED,
2546                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2547             for (temp = NEXT_INSN (i2);
2548                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2549                           || BB_HEAD (this_basic_block) != temp);
2550                  temp = NEXT_INSN (temp))
2551               if (temp != i3 && INSN_P (temp))
2552                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2553                   if (XEXP (link, 0) == i2)
2554                     XEXP (link, 0) = i3;
2555
2556         if (i3notes)
2557           {
2558             rtx link = i3notes;
2559             while (XEXP (link, 1))
2560               link = XEXP (link, 1);
2561             XEXP (link, 1) = i2notes;
2562           }
2563         else
2564           i3notes = i2notes;
2565         i2notes = 0;
2566       }
2567
2568     LOG_LINKS (i3) = 0;
2569     REG_NOTES (i3) = 0;
2570     LOG_LINKS (i2) = 0;
2571     REG_NOTES (i2) = 0;
2572
2573     if (newi2pat)
2574       {
2575         INSN_CODE (i2) = i2_code_number;
2576         PATTERN (i2) = newi2pat;
2577       }
2578     else
2579       {
2580         PUT_CODE (i2, NOTE);
2581         NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2582         NOTE_SOURCE_FILE (i2) = 0;
2583       }
2584
2585     if (i1)
2586       {
2587         LOG_LINKS (i1) = 0;
2588         REG_NOTES (i1) = 0;
2589         PUT_CODE (i1, NOTE);
2590         NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2591         NOTE_SOURCE_FILE (i1) = 0;
2592       }
2593
2594     /* Get death notes for everything that is now used in either I3 or
2595        I2 and used to die in a previous insn.  If we built two new
2596        patterns, move from I1 to I2 then I2 to I3 so that we get the
2597        proper movement on registers that I2 modifies.  */
2598
2599     if (newi2pat)
2600       {
2601         move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2602         move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2603       }
2604     else
2605       move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2606                    i3, &midnotes);
2607
2608     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
2609     if (i3notes)
2610       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2611     if (i2notes)
2612       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2613     if (i1notes)
2614       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2615     if (midnotes)
2616       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2617
2618     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
2619        know these are REG_UNUSED and want them to go to the desired insn,
2620        so we always pass it as i3.  We have not counted the notes in
2621        reg_n_deaths yet, so we need to do so now.  */
2622
2623     if (newi2pat && new_i2_notes)
2624       {
2625         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2626           if (GET_CODE (XEXP (temp, 0)) == REG)
2627             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2628
2629         distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2630       }
2631
2632     if (new_i3_notes)
2633       {
2634         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2635           if (GET_CODE (XEXP (temp, 0)) == REG)
2636             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2637
2638         distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2639       }
2640
2641     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
2642        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
2643        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
2644        in that case, it might delete I2.  Similarly for I2 and I1.
2645        Show an additional death due to the REG_DEAD note we make here.  If
2646        we discard it in distribute_notes, we will decrement it again.  */
2647
2648     if (i3dest_killed)
2649       {
2650         if (GET_CODE (i3dest_killed) == REG)
2651           REG_N_DEATHS (REGNO (i3dest_killed))++;
2652
2653         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2654           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2655                                                NULL_RTX),
2656                             NULL_RTX, i2, NULL_RTX);
2657         else
2658           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2659                                                NULL_RTX),
2660                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2661       }
2662
2663     if (i2dest_in_i2src)
2664       {
2665         if (GET_CODE (i2dest) == REG)
2666           REG_N_DEATHS (REGNO (i2dest))++;
2667
2668         if (newi2pat && reg_set_p (i2dest, newi2pat))
2669           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2670                             NULL_RTX, i2, NULL_RTX);
2671         else
2672           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2673                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2674       }
2675
2676     if (i1dest_in_i1src)
2677       {
2678         if (GET_CODE (i1dest) == REG)
2679           REG_N_DEATHS (REGNO (i1dest))++;
2680
2681         if (newi2pat && reg_set_p (i1dest, newi2pat))
2682           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2683                             NULL_RTX, i2, NULL_RTX);
2684         else
2685           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2686                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2687       }
2688
2689     distribute_links (i3links);
2690     distribute_links (i2links);
2691     distribute_links (i1links);
2692
2693     if (GET_CODE (i2dest) == REG)
2694       {
2695         rtx link;
2696         rtx i2_insn = 0, i2_val = 0, set;
2697
2698         /* The insn that used to set this register doesn't exist, and
2699            this life of the register may not exist either.  See if one of
2700            I3's links points to an insn that sets I2DEST.  If it does,
2701            that is now the last known value for I2DEST. If we don't update
2702            this and I2 set the register to a value that depended on its old
2703            contents, we will get confused.  If this insn is used, thing
2704            will be set correctly in combine_instructions.  */
2705
2706         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2707           if ((set = single_set (XEXP (link, 0))) != 0
2708               && rtx_equal_p (i2dest, SET_DEST (set)))
2709             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2710
2711         record_value_for_reg (i2dest, i2_insn, i2_val);
2712
2713         /* If the reg formerly set in I2 died only once and that was in I3,
2714            zero its use count so it won't make `reload' do any work.  */
2715         if (! added_sets_2
2716             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2717             && ! i2dest_in_i2src)
2718           {
2719             regno = REGNO (i2dest);
2720             REG_N_SETS (regno)--;
2721           }
2722       }
2723
2724     if (i1 && GET_CODE (i1dest) == REG)
2725       {
2726         rtx link;
2727         rtx i1_insn = 0, i1_val = 0, set;
2728
2729         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2730           if ((set = single_set (XEXP (link, 0))) != 0
2731               && rtx_equal_p (i1dest, SET_DEST (set)))
2732             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2733
2734         record_value_for_reg (i1dest, i1_insn, i1_val);
2735
2736         regno = REGNO (i1dest);
2737         if (! added_sets_1 && ! i1dest_in_i1src)
2738           REG_N_SETS (regno)--;
2739       }
2740
2741     /* Update reg_nonzero_bits et al for any changes that may have been made
2742        to this insn.  The order of set_nonzero_bits_and_sign_copies() is
2743        important.  Because newi2pat can affect nonzero_bits of newpat */
2744     if (newi2pat)
2745       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2746     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2747
2748     /* Set new_direct_jump_p if a new return or simple jump instruction
2749        has been created.
2750
2751        If I3 is now an unconditional jump, ensure that it has a
2752        BARRIER following it since it may have initially been a
2753        conditional jump.  It may also be the last nonnote insn.  */
2754
2755     if (returnjump_p (i3) || any_uncondjump_p (i3))
2756       {
2757         *new_direct_jump_p = 1;
2758         mark_jump_label (PATTERN (i3), i3, 0);
2759
2760         if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2761             || GET_CODE (temp) != BARRIER)
2762           emit_barrier_after (i3);
2763       }
2764
2765     if (undobuf.other_insn != NULL_RTX
2766         && (returnjump_p (undobuf.other_insn)
2767             || any_uncondjump_p (undobuf.other_insn)))
2768       {
2769         *new_direct_jump_p = 1;
2770
2771         if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
2772             || GET_CODE (temp) != BARRIER)
2773           emit_barrier_after (undobuf.other_insn);
2774       }
2775
2776     /* An NOOP jump does not need barrier, but it does need cleaning up
2777        of CFG.  */
2778     if (GET_CODE (newpat) == SET
2779         && SET_SRC (newpat) == pc_rtx
2780         && SET_DEST (newpat) == pc_rtx)
2781       *new_direct_jump_p = 1;
2782   }
2783
2784   combine_successes++;
2785   undo_commit ();
2786
2787   if (added_links_insn
2788       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2789       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2790     return added_links_insn;
2791   else
2792     return newi2pat ? i2 : i3;
2793 }
2794 \f
2795 /* Undo all the modifications recorded in undobuf.  */
2796
2797 static void
2798 undo_all (void)
2799 {
2800   struct undo *undo, *next;
2801
2802   for (undo = undobuf.undos; undo; undo = next)
2803     {
2804       next = undo->next;
2805       if (undo->is_int)
2806         *undo->where.i = undo->old_contents.i;
2807       else
2808         *undo->where.r = undo->old_contents.r;
2809
2810       undo->next = undobuf.frees;
2811       undobuf.frees = undo;
2812     }
2813
2814   undobuf.undos = 0;
2815 }
2816
2817 /* We've committed to accepting the changes we made.  Move all
2818    of the undos to the free list.  */
2819
2820 static void
2821 undo_commit (void)
2822 {
2823   struct undo *undo, *next;
2824
2825   for (undo = undobuf.undos; undo; undo = next)
2826     {
2827       next = undo->next;
2828       undo->next = undobuf.frees;
2829       undobuf.frees = undo;
2830     }
2831   undobuf.undos = 0;
2832 }
2833
2834 \f
2835 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2836    where we have an arithmetic expression and return that point.  LOC will
2837    be inside INSN.
2838
2839    try_combine will call this function to see if an insn can be split into
2840    two insns.  */
2841
2842 static rtx *
2843 find_split_point (rtx *loc, rtx insn)
2844 {
2845   rtx x = *loc;
2846   enum rtx_code code = GET_CODE (x);
2847   rtx *split;
2848   unsigned HOST_WIDE_INT len = 0;
2849   HOST_WIDE_INT pos = 0;
2850   int unsignedp = 0;
2851   rtx inner = NULL_RTX;
2852
2853   /* First special-case some codes.  */
2854   switch (code)
2855     {
2856     case SUBREG:
2857 #ifdef INSN_SCHEDULING
2858       /* If we are making a paradoxical SUBREG invalid, it becomes a split
2859          point.  */
2860       if (GET_CODE (SUBREG_REG (x)) == MEM)
2861         return loc;
2862 #endif
2863       return find_split_point (&SUBREG_REG (x), insn);
2864
2865     case MEM:
2866 #ifdef HAVE_lo_sum
2867       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2868          using LO_SUM and HIGH.  */
2869       if (GET_CODE (XEXP (x, 0)) == CONST
2870           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2871         {
2872           SUBST (XEXP (x, 0),
2873                  gen_rtx_LO_SUM (Pmode,
2874                                  gen_rtx_HIGH (Pmode, XEXP (x, 0)),
2875                                  XEXP (x, 0)));
2876           return &XEXP (XEXP (x, 0), 0);
2877         }
2878 #endif
2879
2880       /* If we have a PLUS whose second operand is a constant and the
2881          address is not valid, perhaps will can split it up using
2882          the machine-specific way to split large constants.  We use
2883          the first pseudo-reg (one of the virtual regs) as a placeholder;
2884          it will not remain in the result.  */
2885       if (GET_CODE (XEXP (x, 0)) == PLUS
2886           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2887           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2888         {
2889           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2890           rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2891                                  subst_insn);
2892
2893           /* This should have produced two insns, each of which sets our
2894              placeholder.  If the source of the second is a valid address,
2895              we can make put both sources together and make a split point
2896              in the middle.  */
2897
2898           if (seq
2899               && NEXT_INSN (seq) != NULL_RTX
2900               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
2901               && GET_CODE (seq) == INSN
2902               && GET_CODE (PATTERN (seq)) == SET
2903               && SET_DEST (PATTERN (seq)) == reg
2904               && ! reg_mentioned_p (reg,
2905                                     SET_SRC (PATTERN (seq)))
2906               && GET_CODE (NEXT_INSN (seq)) == INSN
2907               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
2908               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
2909               && memory_address_p (GET_MODE (x),
2910                                    SET_SRC (PATTERN (NEXT_INSN (seq)))))
2911             {
2912               rtx src1 = SET_SRC (PATTERN (seq));
2913               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
2914
2915               /* Replace the placeholder in SRC2 with SRC1.  If we can
2916                  find where in SRC2 it was placed, that can become our
2917                  split point and we can replace this address with SRC2.
2918                  Just try two obvious places.  */
2919
2920               src2 = replace_rtx (src2, reg, src1);
2921               split = 0;
2922               if (XEXP (src2, 0) == src1)
2923                 split = &XEXP (src2, 0);
2924               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2925                        && XEXP (XEXP (src2, 0), 0) == src1)
2926                 split = &XEXP (XEXP (src2, 0), 0);
2927
2928               if (split)
2929                 {
2930                   SUBST (XEXP (x, 0), src2);
2931                   return split;
2932                 }
2933             }
2934
2935           /* If that didn't work, perhaps the first operand is complex and
2936              needs to be computed separately, so make a split point there.
2937              This will occur on machines that just support REG + CONST
2938              and have a constant moved through some previous computation.  */
2939
2940           else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2941                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2942                          && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2943                              == 'o')))
2944             return &XEXP (XEXP (x, 0), 0);
2945         }
2946       break;
2947
2948     case SET:
2949 #ifdef HAVE_cc0
2950       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2951          ZERO_EXTRACT, the most likely reason why this doesn't match is that
2952          we need to put the operand into a register.  So split at that
2953          point.  */
2954
2955       if (SET_DEST (x) == cc0_rtx
2956           && GET_CODE (SET_SRC (x)) != COMPARE
2957           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2958           && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2959           && ! (GET_CODE (SET_SRC (x)) == SUBREG
2960                 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2961         return &SET_SRC (x);
2962 #endif
2963
2964       /* See if we can split SET_SRC as it stands.  */
2965       split = find_split_point (&SET_SRC (x), insn);
2966       if (split && split != &SET_SRC (x))
2967         return split;
2968
2969       /* See if we can split SET_DEST as it stands.  */
2970       split = find_split_point (&SET_DEST (x), insn);
2971       if (split && split != &SET_DEST (x))
2972         return split;
2973
2974       /* See if this is a bitfield assignment with everything constant.  If
2975          so, this is an IOR of an AND, so split it into that.  */
2976       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2977           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2978               <= HOST_BITS_PER_WIDE_INT)
2979           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2980           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2981           && GET_CODE (SET_SRC (x)) == CONST_INT
2982           && ((INTVAL (XEXP (SET_DEST (x), 1))
2983                + INTVAL (XEXP (SET_DEST (x), 2)))
2984               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2985           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2986         {
2987           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
2988           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
2989           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
2990           rtx dest = XEXP (SET_DEST (x), 0);
2991           enum machine_mode mode = GET_MODE (dest);
2992           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2993
2994           if (BITS_BIG_ENDIAN)
2995             pos = GET_MODE_BITSIZE (mode) - len - pos;
2996
2997           if (src == mask)
2998             SUBST (SET_SRC (x),
2999                    gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3000           else
3001             SUBST (SET_SRC (x),
3002                    gen_binary (IOR, mode,
3003                                gen_binary (AND, mode, dest,
3004                                            gen_int_mode (~(mask << pos),
3005                                                          mode)),
3006                                GEN_INT (src << pos)));
3007
3008           SUBST (SET_DEST (x), dest);
3009
3010           split = find_split_point (&SET_SRC (x), insn);
3011           if (split && split != &SET_SRC (x))
3012             return split;
3013         }
3014
3015       /* Otherwise, see if this is an operation that we can split into two.
3016          If so, try to split that.  */
3017       code = GET_CODE (SET_SRC (x));
3018
3019       switch (code)
3020         {
3021         case AND:
3022           /* If we are AND'ing with a large constant that is only a single
3023              bit and the result is only being used in a context where we
3024              need to know if it is zero or nonzero, replace it with a bit
3025              extraction.  This will avoid the large constant, which might
3026              have taken more than one insn to make.  If the constant were
3027              not a valid argument to the AND but took only one insn to make,
3028              this is no worse, but if it took more than one insn, it will
3029              be better.  */
3030
3031           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3032               && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
3033               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3034               && GET_CODE (SET_DEST (x)) == REG
3035               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3036               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3037               && XEXP (*split, 0) == SET_DEST (x)
3038               && XEXP (*split, 1) == const0_rtx)
3039             {
3040               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3041                                                 XEXP (SET_SRC (x), 0),
3042                                                 pos, NULL_RTX, 1, 1, 0, 0);
3043               if (extraction != 0)
3044                 {
3045                   SUBST (SET_SRC (x), extraction);
3046                   return find_split_point (loc, insn);
3047                 }
3048             }
3049           break;
3050
3051         case NE:
3052           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3053              is known to be on, this can be converted into a NEG of a shift.  */
3054           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3055               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3056               && 1 <= (pos = exact_log2
3057                        (nonzero_bits (XEXP (SET_SRC (x), 0),
3058                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
3059             {
3060               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3061
3062               SUBST (SET_SRC (x),
3063                      gen_rtx_NEG (mode,
3064                                   gen_rtx_LSHIFTRT (mode,
3065                                                     XEXP (SET_SRC (x), 0),
3066                                                     GEN_INT (pos))));
3067
3068               split = find_split_point (&SET_SRC (x), insn);
3069               if (split && split != &SET_SRC (x))
3070                 return split;
3071             }
3072           break;
3073
3074         case SIGN_EXTEND:
3075           inner = XEXP (SET_SRC (x), 0);
3076
3077           /* We can't optimize if either mode is a partial integer
3078              mode as we don't know how many bits are significant
3079              in those modes.  */
3080           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3081               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3082             break;
3083
3084           pos = 0;
3085           len = GET_MODE_BITSIZE (GET_MODE (inner));
3086           unsignedp = 0;
3087           break;
3088
3089         case SIGN_EXTRACT:
3090         case ZERO_EXTRACT:
3091           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3092               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3093             {
3094               inner = XEXP (SET_SRC (x), 0);
3095               len = INTVAL (XEXP (SET_SRC (x), 1));
3096               pos = INTVAL (XEXP (SET_SRC (x), 2));
3097
3098               if (BITS_BIG_ENDIAN)
3099                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3100               unsignedp = (code == ZERO_EXTRACT);
3101             }
3102           break;
3103
3104         default:
3105           break;
3106         }
3107
3108       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3109         {
3110           enum machine_mode mode = GET_MODE (SET_SRC (x));
3111
3112           /* For unsigned, we have a choice of a shift followed by an
3113              AND or two shifts.  Use two shifts for field sizes where the
3114              constant might be too large.  We assume here that we can
3115              always at least get 8-bit constants in an AND insn, which is
3116              true for every current RISC.  */
3117
3118           if (unsignedp && len <= 8)
3119             {
3120               SUBST (SET_SRC (x),
3121                      gen_rtx_AND (mode,
3122                                   gen_rtx_LSHIFTRT
3123                                   (mode, gen_lowpart_for_combine (mode, inner),
3124                                    GEN_INT (pos)),
3125                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3126
3127               split = find_split_point (&SET_SRC (x), insn);
3128               if (split && split != &SET_SRC (x))
3129                 return split;
3130             }
3131           else
3132             {
3133               SUBST (SET_SRC (x),
3134                      gen_rtx_fmt_ee
3135                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3136                       gen_rtx_ASHIFT (mode,
3137                                       gen_lowpart_for_combine (mode, inner),
3138                                       GEN_INT (GET_MODE_BITSIZE (mode)
3139                                                - len - pos)),
3140                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3141
3142               split = find_split_point (&SET_SRC (x), insn);
3143               if (split && split != &SET_SRC (x))
3144                 return split;
3145             }
3146         }
3147
3148       /* See if this is a simple operation with a constant as the second
3149          operand.  It might be that this constant is out of range and hence
3150          could be used as a split point.  */
3151       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3152            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3153            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
3154           && CONSTANT_P (XEXP (SET_SRC (x), 1))
3155           && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
3156               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3157                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
3158                       == 'o'))))
3159         return &XEXP (SET_SRC (x), 1);
3160
3161       /* Finally, see if this is a simple operation with its first operand
3162          not in a register.  The operation might require this operand in a
3163          register, so return it as a split point.  We can always do this
3164          because if the first operand were another operation, we would have
3165          already found it as a split point.  */
3166       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3167            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3168            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
3169            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
3170           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3171         return &XEXP (SET_SRC (x), 0);
3172
3173       return 0;
3174
3175     case AND:
3176     case IOR:
3177       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3178          it is better to write this as (not (ior A B)) so we can split it.
3179          Similarly for IOR.  */
3180       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3181         {
3182           SUBST (*loc,
3183                  gen_rtx_NOT (GET_MODE (x),
3184                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3185                                               GET_MODE (x),
3186                                               XEXP (XEXP (x, 0), 0),
3187                                               XEXP (XEXP (x, 1), 0))));
3188           return find_split_point (loc, insn);
3189         }
3190
3191       /* Many RISC machines have a large set of logical insns.  If the
3192          second operand is a NOT, put it first so we will try to split the
3193          other operand first.  */
3194       if (GET_CODE (XEXP (x, 1)) == NOT)
3195         {
3196           rtx tem = XEXP (x, 0);
3197           SUBST (XEXP (x, 0), XEXP (x, 1));
3198           SUBST (XEXP (x, 1), tem);
3199         }
3200       break;
3201
3202     default:
3203       break;
3204     }
3205
3206   /* Otherwise, select our actions depending on our rtx class.  */
3207   switch (GET_RTX_CLASS (code))
3208     {
3209     case 'b':                   /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
3210     case '3':
3211       split = find_split_point (&XEXP (x, 2), insn);
3212       if (split)
3213         return split;
3214       /* ... fall through ...  */
3215     case '2':
3216     case 'c':
3217     case '<':
3218       split = find_split_point (&XEXP (x, 1), insn);
3219       if (split)
3220         return split;
3221       /* ... fall through ...  */
3222     case '1':
3223       /* Some machines have (and (shift ...) ...) insns.  If X is not
3224          an AND, but XEXP (X, 0) is, use it as our split point.  */
3225       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3226         return &XEXP (x, 0);
3227
3228       split = find_split_point (&XEXP (x, 0), insn);
3229       if (split)
3230         return split;
3231       return loc;
3232     }
3233
3234   /* Otherwise, we don't have a split point.  */
3235   return 0;
3236 }
3237 \f
3238 /* Throughout X, replace FROM with TO, and return the result.
3239    The result is TO if X is FROM;
3240    otherwise the result is X, but its contents may have been modified.
3241    If they were modified, a record was made in undobuf so that
3242    undo_all will (among other things) return X to its original state.
3243
3244    If the number of changes necessary is too much to record to undo,
3245    the excess changes are not made, so the result is invalid.
3246    The changes already made can still be undone.
3247    undobuf.num_undo is incremented for such changes, so by testing that
3248    the caller can tell whether the result is valid.
3249
3250    `n_occurrences' is incremented each time FROM is replaced.
3251
3252    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3253
3254    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
3255    by copying if `n_occurrences' is nonzero.  */
3256
3257 static rtx
3258 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3259 {
3260   enum rtx_code code = GET_CODE (x);
3261   enum machine_mode op0_mode = VOIDmode;
3262   const char *fmt;
3263   int len, i;
3264   rtx new;
3265
3266 /* Two expressions are equal if they are identical copies of a shared
3267    RTX or if they are both registers with the same register number
3268    and mode.  */
3269
3270 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
3271   ((X) == (Y)                                           \
3272    || (GET_CODE (X) == REG && GET_CODE (Y) == REG       \
3273        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3274
3275   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3276     {
3277       n_occurrences++;
3278       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3279     }
3280
3281   /* If X and FROM are the same register but different modes, they will
3282      not have been seen as equal above.  However, flow.c will make a
3283      LOG_LINKS entry for that case.  If we do nothing, we will try to
3284      rerecognize our original insn and, when it succeeds, we will
3285      delete the feeding insn, which is incorrect.
3286
3287      So force this insn not to match in this (rare) case.  */
3288   if (! in_dest && code == REG && GET_CODE (from) == REG
3289       && REGNO (x) == REGNO (from))
3290     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3291
3292   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3293      of which may contain things that can be combined.  */
3294   if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3295     return x;
3296
3297   /* It is possible to have a subexpression appear twice in the insn.
3298      Suppose that FROM is a register that appears within TO.
3299      Then, after that subexpression has been scanned once by `subst',
3300      the second time it is scanned, TO may be found.  If we were
3301      to scan TO here, we would find FROM within it and create a
3302      self-referent rtl structure which is completely wrong.  */
3303   if (COMBINE_RTX_EQUAL_P (x, to))
3304     return to;
3305
3306   /* Parallel asm_operands need special attention because all of the
3307      inputs are shared across the arms.  Furthermore, unsharing the
3308      rtl results in recognition failures.  Failure to handle this case
3309      specially can result in circular rtl.
3310
3311      Solve this by doing a normal pass across the first entry of the
3312      parallel, and only processing the SET_DESTs of the subsequent
3313      entries.  Ug.  */
3314
3315   if (code == PARALLEL
3316       && GET_CODE (XVECEXP (x, 0, 0)) == SET
3317       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3318     {
3319       new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3320
3321       /* If this substitution failed, this whole thing fails.  */
3322       if (GET_CODE (new) == CLOBBER
3323           && XEXP (new, 0) == const0_rtx)
3324         return new;
3325
3326       SUBST (XVECEXP (x, 0, 0), new);
3327
3328       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3329         {
3330           rtx dest = SET_DEST (XVECEXP (x, 0, i));
3331
3332           if (GET_CODE (dest) != REG
3333               && GET_CODE (dest) != CC0
3334               && GET_CODE (dest) != PC)
3335             {
3336               new = subst (dest, from, to, 0, unique_copy);
3337
3338               /* If this substitution failed, this whole thing fails.  */
3339               if (GET_CODE (new) == CLOBBER
3340                   && XEXP (new, 0) == const0_rtx)
3341                 return new;
3342
3343               SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3344             }
3345         }
3346     }
3347   else
3348     {
3349       len = GET_RTX_LENGTH (code);
3350       fmt = GET_RTX_FORMAT (code);
3351
3352       /* We don't need to process a SET_DEST that is a register, CC0,
3353          or PC, so set up to skip this common case.  All other cases
3354          where we want to suppress replacing something inside a
3355          SET_SRC are handled via the IN_DEST operand.  */
3356       if (code == SET
3357           && (GET_CODE (SET_DEST (x)) == REG
3358               || GET_CODE (SET_DEST (x)) == CC0
3359               || GET_CODE (SET_DEST (x)) == PC))
3360         fmt = "ie";
3361
3362       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3363          constant.  */
3364       if (fmt[0] == 'e')
3365         op0_mode = GET_MODE (XEXP (x, 0));
3366
3367       for (i = 0; i < len; i++)
3368         {
3369           if (fmt[i] == 'E')
3370             {
3371               int j;
3372               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3373                 {
3374                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3375                     {
3376                       new = (unique_copy && n_occurrences
3377                              ? copy_rtx (to) : to);
3378                       n_occurrences++;
3379                     }
3380                   else
3381                     {
3382                       new = subst (XVECEXP (x, i, j), from, to, 0,
3383                                    unique_copy);
3384
3385                       /* If this substitution failed, this whole thing
3386                          fails.  */
3387                       if (GET_CODE (new) == CLOBBER
3388                           && XEXP (new, 0) == const0_rtx)
3389                         return new;
3390                     }
3391
3392                   SUBST (XVECEXP (x, i, j), new);
3393                 }
3394             }
3395           else if (fmt[i] == 'e')
3396             {
3397               /* If this is a register being set, ignore it.  */
3398               new = XEXP (x, i);
3399               if (in_dest
3400                   && (code == SUBREG || code == STRICT_LOW_PART
3401                       || code == ZERO_EXTRACT)
3402                   && i == 0
3403                   && GET_CODE (new) == REG)
3404                 ;
3405
3406               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3407                 {
3408                   /* In general, don't install a subreg involving two
3409                      modes not tieable.  It can worsen register
3410                      allocation, and can even make invalid reload
3411                      insns, since the reg inside may need to be copied
3412                      from in the outside mode, and that may be invalid
3413                      if it is an fp reg copied in integer mode.
3414
3415                      We allow two exceptions to this: It is valid if
3416                      it is inside another SUBREG and the mode of that
3417                      SUBREG and the mode of the inside of TO is
3418                      tieable and it is valid if X is a SET that copies
3419                      FROM to CC0.  */
3420
3421                   if (GET_CODE (to) == SUBREG
3422                       && ! MODES_TIEABLE_P (GET_MODE (to),
3423                                             GET_MODE (SUBREG_REG (to)))
3424                       && ! (code == SUBREG
3425                             && MODES_TIEABLE_P (GET_MODE (x),
3426                                                 GET_MODE (SUBREG_REG (to))))
3427 #ifdef HAVE_cc0
3428                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3429 #endif
3430                       )
3431                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3432
3433 #ifdef CANNOT_CHANGE_MODE_CLASS
3434                   if (code == SUBREG
3435                       && GET_CODE (to) == REG
3436                       && REGNO (to) < FIRST_PSEUDO_REGISTER
3437                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3438                                                    GET_MODE (to),
3439                                                    GET_MODE (x)))
3440                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3441 #endif
3442
3443                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3444                   n_occurrences++;
3445                 }
3446               else
3447                 /* If we are in a SET_DEST, suppress most cases unless we
3448                    have gone inside a MEM, in which case we want to
3449                    simplify the address.  We assume here that things that
3450                    are actually part of the destination have their inner
3451                    parts in the first expression.  This is true for SUBREG,
3452                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3453                    things aside from REG and MEM that should appear in a
3454                    SET_DEST.  */
3455                 new = subst (XEXP (x, i), from, to,
3456                              (((in_dest
3457                                 && (code == SUBREG || code == STRICT_LOW_PART
3458                                     || code == ZERO_EXTRACT))
3459                                || code == SET)
3460                               && i == 0), unique_copy);
3461
3462               /* If we found that we will have to reject this combination,
3463                  indicate that by returning the CLOBBER ourselves, rather than
3464                  an expression containing it.  This will speed things up as
3465                  well as prevent accidents where two CLOBBERs are considered
3466                  to be equal, thus producing an incorrect simplification.  */
3467
3468               if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3469                 return new;
3470
3471               if (GET_CODE (x) == SUBREG
3472                   && (GET_CODE (new) == CONST_INT
3473                       || GET_CODE (new) == CONST_DOUBLE))
3474                 {
3475                   enum machine_mode mode = GET_MODE (x);
3476
3477                   x = simplify_subreg (GET_MODE (x), new,
3478                                        GET_MODE (SUBREG_REG (x)),
3479                                        SUBREG_BYTE (x));
3480                   if (! x)
3481                     x = gen_rtx_CLOBBER (mode, const0_rtx);
3482                 }
3483               else if (GET_CODE (new) == CONST_INT
3484                        && GET_CODE (x) == ZERO_EXTEND)
3485                 {
3486                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3487                                                 new, GET_MODE (XEXP (x, 0)));
3488                   if (! x)
3489                     abort ();
3490                 }
3491               else
3492                 SUBST (XEXP (x, i), new);
3493             }
3494         }
3495     }
3496
3497   /* Try to simplify X.  If the simplification changed the code, it is likely
3498      that further simplification will help, so loop, but limit the number
3499      of repetitions that will be performed.  */
3500
3501   for (i = 0; i < 4; i++)
3502     {
3503       /* If X is sufficiently simple, don't bother trying to do anything
3504          with it.  */
3505       if (code != CONST_INT && code != REG && code != CLOBBER)
3506         x = combine_simplify_rtx (x, op0_mode, i == 3, in_dest);
3507
3508       if (GET_CODE (x) == code)
3509         break;
3510
3511       code = GET_CODE (x);
3512
3513       /* We no longer know the original mode of operand 0 since we
3514          have changed the form of X)  */
3515       op0_mode = VOIDmode;
3516     }
3517
3518   return x;
3519 }
3520 \f
3521 /* Simplify X, a piece of RTL.  We just operate on the expression at the
3522    outer level; call `subst' to simplify recursively.  Return the new
3523    expression.
3524
3525    OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3526    will be the iteration even if an expression with a code different from
3527    X is returned; IN_DEST is nonzero if we are inside a SET_DEST.  */
3528
3529 static rtx
3530 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int last,
3531                       int in_dest)
3532 {
3533   enum rtx_code code = GET_CODE (x);
3534   enum machine_mode mode = GET_MODE (x);
3535   rtx temp;
3536   rtx reversed;
3537   int i;
3538
3539   /* If this is a commutative operation, put a constant last and a complex
3540      expression first.  We don't need to do this for comparisons here.  */
3541   if (GET_RTX_CLASS (code) == 'c'
3542       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3543     {
3544       temp = XEXP (x, 0);
3545       SUBST (XEXP (x, 0), XEXP (x, 1));
3546       SUBST (XEXP (x, 1), temp);
3547     }
3548
3549   /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3550      sign extension of a PLUS with a constant, reverse the order of the sign
3551      extension and the addition. Note that this not the same as the original
3552      code, but overflow is undefined for signed values.  Also note that the
3553      PLUS will have been partially moved "inside" the sign-extension, so that
3554      the first operand of X will really look like:
3555          (ashiftrt (plus (ashift A C4) C5) C4).
3556      We convert this to
3557          (plus (ashiftrt (ashift A C4) C2) C4)
3558      and replace the first operand of X with that expression.  Later parts
3559      of this function may simplify the expression further.
3560
3561      For example, if we start with (mult (sign_extend (plus A C1)) C2),
3562      we swap the SIGN_EXTEND and PLUS.  Later code will apply the
3563      distributive law to produce (plus (mult (sign_extend X) C1) C3).
3564
3565      We do this to simplify address expressions.  */
3566
3567   if ((code == PLUS || code == MINUS || code == MULT)
3568       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3569       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3570       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3571       && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3572       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3573       && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3574       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3575       && (temp = simplify_binary_operation (ASHIFTRT, mode,
3576                                             XEXP (XEXP (XEXP (x, 0), 0), 1),
3577                                             XEXP (XEXP (x, 0), 1))) != 0)
3578     {
3579       rtx new
3580         = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3581                                 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3582                                 INTVAL (XEXP (XEXP (x, 0), 1)));
3583
3584       new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3585                                   INTVAL (XEXP (XEXP (x, 0), 1)));
3586
3587       SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3588     }
3589
3590   /* If this is a simple operation applied to an IF_THEN_ELSE, try
3591      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
3592      things.  Check for cases where both arms are testing the same
3593      condition.
3594
3595      Don't do anything if all operands are very simple.  */
3596
3597   if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3598         || GET_RTX_CLASS (code) == '<')
3599        && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3600             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3601                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3602                       == 'o')))
3603            || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3604                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3605                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3606                          == 'o')))))
3607       || (GET_RTX_CLASS (code) == '1'
3608           && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3609                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3610                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3611                          == 'o'))))))
3612     {
3613       rtx cond, true_rtx, false_rtx;
3614
3615       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3616       if (cond != 0
3617           /* If everything is a comparison, what we have is highly unlikely
3618              to be simpler, so don't use it.  */
3619           && ! (GET_RTX_CLASS (code) == '<'
3620                 && (GET_RTX_CLASS (GET_CODE (true_rtx)) == '<'
3621                     || GET_RTX_CLASS (GET_CODE (false_rtx)) == '<')))
3622         {
3623           rtx cop1 = const0_rtx;
3624           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3625
3626           if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3627             return x;
3628
3629           /* Simplify the alternative arms; this may collapse the true and
3630              false arms to store-flag values.  Be careful to use copy_rtx
3631              here since true_rtx or false_rtx might share RTL with x as a
3632              result of the if_then_else_cond call above.  */
3633           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
3634           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
3635
3636           /* If true_rtx and false_rtx are not general_operands, an if_then_else
3637              is unlikely to be simpler.  */
3638           if (general_operand (true_rtx, VOIDmode)
3639               && general_operand (false_rtx, VOIDmode))
3640             {
3641               enum rtx_code reversed;
3642
3643               /* Restarting if we generate a store-flag expression will cause
3644                  us to loop.  Just drop through in this case.  */
3645
3646               /* If the result values are STORE_FLAG_VALUE and zero, we can
3647                  just make the comparison operation.  */
3648               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3649                 x = gen_binary (cond_code, mode, cond, cop1);
3650               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3651                        && ((reversed = reversed_comparison_code_parts
3652                                         (cond_code, cond, cop1, NULL))
3653                            != UNKNOWN))
3654                 x = gen_binary (reversed, mode, cond, cop1);
3655
3656               /* Likewise, we can make the negate of a comparison operation
3657                  if the result values are - STORE_FLAG_VALUE and zero.  */
3658               else if (GET_CODE (true_rtx) == CONST_INT
3659                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3660                        && false_rtx == const0_rtx)
3661                 x = simplify_gen_unary (NEG, mode,
3662                                         gen_binary (cond_code, mode, cond,
3663                                                     cop1),
3664                                         mode);
3665               else if (GET_CODE (false_rtx) == CONST_INT
3666                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3667                        && true_rtx == const0_rtx
3668                        && ((reversed = reversed_comparison_code_parts
3669                                         (cond_code, cond, cop1, NULL))
3670                            != UNKNOWN))
3671                 x = simplify_gen_unary (NEG, mode,
3672                                         gen_binary (reversed, mode,
3673                                                     cond, cop1),
3674                                         mode);
3675               else
3676                 return gen_rtx_IF_THEN_ELSE (mode,
3677                                              gen_binary (cond_code, VOIDmode,
3678                                                          cond, cop1),
3679                                              true_rtx, false_rtx);
3680
3681               code = GET_CODE (x);
3682               op0_mode = VOIDmode;
3683             }
3684         }
3685     }
3686
3687   /* Try to fold this expression in case we have constants that weren't
3688      present before.  */
3689   temp = 0;
3690   switch (GET_RTX_CLASS (code))
3691     {
3692     case '1':
3693       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3694       break;
3695     case '<':
3696       {
3697         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3698         if (cmp_mode == VOIDmode)
3699           {
3700             cmp_mode = GET_MODE (XEXP (x, 1));
3701             if (cmp_mode == VOIDmode)
3702               cmp_mode = op0_mode;
3703           }
3704         temp = simplify_relational_operation (code, cmp_mode,
3705                                               XEXP (x, 0), XEXP (x, 1));
3706       }
3707 #ifdef FLOAT_STORE_FLAG_VALUE
3708       if (temp != 0 && GET_MODE_CLASS (mode) == MODE_FLOAT)
3709         {
3710           if (temp == const0_rtx)
3711             temp = CONST0_RTX (mode);
3712           else
3713             temp = CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE (mode),
3714                                                  mode);
3715         }
3716 #endif
3717       break;
3718     case 'c':
3719     case '2':
3720       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3721       break;
3722     case 'b':
3723     case '3':
3724       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3725                                          XEXP (x, 1), XEXP (x, 2));
3726       break;
3727     }
3728
3729   if (temp)
3730     {
3731       x = temp;
3732       code = GET_CODE (temp);
3733       op0_mode = VOIDmode;
3734       mode = GET_MODE (temp);
3735     }
3736
3737   /* First see if we can apply the inverse distributive law.  */
3738   if (code == PLUS || code == MINUS
3739       || code == AND || code == IOR || code == XOR)
3740     {
3741       x = apply_distributive_law (x);
3742       code = GET_CODE (x);
3743       op0_mode = VOIDmode;
3744     }
3745
3746   /* If CODE is an associative operation not otherwise handled, see if we
3747      can associate some operands.  This can win if they are constants or
3748      if they are logically related (i.e. (a & b) & a).  */
3749   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3750        || code == AND || code == IOR || code == XOR
3751        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3752       && ((INTEGRAL_MODE_P (mode) && code != DIV)
3753           || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3754     {
3755       if (GET_CODE (XEXP (x, 0)) == code)
3756         {
3757           rtx other = XEXP (XEXP (x, 0), 0);
3758           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3759           rtx inner_op1 = XEXP (x, 1);
3760           rtx inner;
3761
3762           /* Make sure we pass the constant operand if any as the second
3763              one if this is a commutative operation.  */
3764           if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3765             {
3766               rtx tem = inner_op0;
3767               inner_op0 = inner_op1;
3768               inner_op1 = tem;
3769             }
3770           inner = simplify_binary_operation (code == MINUS ? PLUS
3771                                              : code == DIV ? MULT
3772                                              : code,
3773                                              mode, inner_op0, inner_op1);
3774
3775           /* For commutative operations, try the other pair if that one
3776              didn't simplify.  */
3777           if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3778             {
3779               other = XEXP (XEXP (x, 0), 1);
3780               inner = simplify_binary_operation (code, mode,
3781                                                  XEXP (XEXP (x, 0), 0),
3782                                                  XEXP (x, 1));
3783             }
3784
3785           if (inner)
3786             return gen_binary (code, mode, other, inner);
3787         }
3788     }
3789
3790   /* A little bit of algebraic simplification here.  */
3791   switch (code)
3792     {
3793     case MEM:
3794       /* Ensure that our address has any ASHIFTs converted to MULT in case
3795          address-recognizing predicates are called later.  */
3796       temp = make_compound_operation (XEXP (x, 0), MEM);
3797       SUBST (XEXP (x, 0), temp);
3798       break;
3799
3800     case SUBREG:
3801       if (op0_mode == VOIDmode)
3802         op0_mode = GET_MODE (SUBREG_REG (x));
3803
3804       /* simplify_subreg can't use gen_lowpart_for_combine.  */
3805       if (CONSTANT_P (SUBREG_REG (x))
3806           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
3807              /* Don't call gen_lowpart_for_combine if the inner mode
3808                 is VOIDmode and we cannot simplify it, as SUBREG without
3809                 inner mode is invalid.  */
3810           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
3811               || gen_lowpart_common (mode, SUBREG_REG (x))))
3812         return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3813
3814       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
3815         break;
3816       {
3817         rtx temp;
3818         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
3819                                 SUBREG_BYTE (x));
3820         if (temp)
3821           return temp;
3822       }
3823
3824       /* Don't change the mode of the MEM if that would change the meaning
3825          of the address.  */
3826       if (GET_CODE (SUBREG_REG (x)) == MEM
3827           && (MEM_VOLATILE_P (SUBREG_REG (x))
3828               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
3829         return gen_rtx_CLOBBER (mode, const0_rtx);
3830
3831       /* Note that we cannot do any narrowing for non-constants since
3832          we might have been counting on using the fact that some bits were
3833          zero.  We now do this in the SET.  */
3834
3835       break;
3836
3837     case NOT:
3838       if (GET_CODE (XEXP (x, 0)) == SUBREG
3839           && subreg_lowpart_p (XEXP (x, 0))
3840           && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3841               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3842           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3843           && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3844         {
3845           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3846
3847           x = gen_rtx_ROTATE (inner_mode,
3848                               simplify_gen_unary (NOT, inner_mode, const1_rtx,
3849                                                   inner_mode),
3850                               XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3851           return gen_lowpart_for_combine (mode, x);
3852         }
3853
3854       /* Apply De Morgan's laws to reduce number of patterns for machines
3855          with negating logical insns (and-not, nand, etc.).  If result has
3856          only one NOT, put it first, since that is how the patterns are
3857          coded.  */
3858
3859       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3860         {
3861           rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3862           enum machine_mode op_mode;
3863
3864           op_mode = GET_MODE (in1);
3865           in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
3866
3867           op_mode = GET_MODE (in2);
3868           if (op_mode == VOIDmode)
3869             op_mode = mode;
3870           in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
3871
3872           if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
3873             {
3874               rtx tem = in2;
3875               in2 = in1; in1 = tem;
3876             }
3877
3878           return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3879                                  mode, in1, in2);
3880         }
3881       break;
3882
3883     case NEG:
3884       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1.  */
3885       if (GET_CODE (XEXP (x, 0)) == XOR
3886           && XEXP (XEXP (x, 0), 1) == const1_rtx
3887           && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3888         return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3889
3890       temp = expand_compound_operation (XEXP (x, 0));
3891
3892       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3893          replaced by (lshiftrt X C).  This will convert
3894          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
3895
3896       if (GET_CODE (temp) == ASHIFTRT
3897           && GET_CODE (XEXP (temp, 1)) == CONST_INT
3898           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3899         return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3900                                      INTVAL (XEXP (temp, 1)));
3901
3902       /* If X has only a single bit that might be nonzero, say, bit I, convert
3903          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3904          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
3905          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
3906          or a SUBREG of one since we'd be making the expression more
3907          complex if it was just a register.  */
3908
3909       if (GET_CODE (temp) != REG
3910           && ! (GET_CODE (temp) == SUBREG
3911                 && GET_CODE (SUBREG_REG (temp)) == REG)
3912           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3913         {
3914           rtx temp1 = simplify_shift_const
3915             (NULL_RTX, ASHIFTRT, mode,
3916              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3917                                    GET_MODE_BITSIZE (mode) - 1 - i),
3918              GET_MODE_BITSIZE (mode) - 1 - i);
3919
3920           /* If all we did was surround TEMP with the two shifts, we
3921              haven't improved anything, so don't use it.  Otherwise,
3922              we are better off with TEMP1.  */
3923           if (GET_CODE (temp1) != ASHIFTRT
3924               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3925               || XEXP (XEXP (temp1, 0), 0) != temp)
3926             return temp1;
3927         }
3928       break;
3929
3930     case TRUNCATE:
3931       /* We can't handle truncation to a partial integer mode here
3932          because we don't know the real bitsize of the partial
3933          integer mode.  */
3934       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3935         break;
3936
3937       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3938           && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3939                                     GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
3940         SUBST (XEXP (x, 0),
3941                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3942                               GET_MODE_MASK (mode), NULL_RTX, 0));
3943
3944       /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI.  */
3945       if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3946            || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3947           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3948         return XEXP (XEXP (x, 0), 0);
3949
3950       /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3951          (OP:SI foo:SI) if OP is NEG or ABS.  */
3952       if ((GET_CODE (XEXP (x, 0)) == ABS
3953            || GET_CODE (XEXP (x, 0)) == NEG)
3954           && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3955               || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3956           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3957         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
3958                                    XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
3959
3960       /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3961          (truncate:SI x).  */
3962       if (GET_CODE (XEXP (x, 0)) == SUBREG
3963           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3964           && subreg_lowpart_p (XEXP (x, 0)))
3965         return SUBREG_REG (XEXP (x, 0));
3966
3967       /* If we know that the value is already truncated, we can
3968          replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
3969          is nonzero for the corresponding modes.  But don't do this
3970          for an (LSHIFTRT (MULT ...)) since this will cause problems
3971          with the umulXi3_highpart patterns.  */
3972       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3973                                  GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
3974           && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3975              >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
3976           && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
3977                 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
3978         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3979
3980       /* A truncate of a comparison can be replaced with a subreg if
3981          STORE_FLAG_VALUE permits.  This is like the previous test,
3982          but it works even if the comparison is done in a mode larger
3983          than HOST_BITS_PER_WIDE_INT.  */
3984       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3985           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3986           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
3987         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3988
3989       /* Similarly, a truncate of a register whose value is a
3990          comparison can be replaced with a subreg if STORE_FLAG_VALUE
3991          permits.  */
3992       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3993           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
3994           && (temp = get_last_value (XEXP (x, 0)))
3995           && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3996         return gen_lowpart_for_combine (mode, XEXP (x, 0));
3997
3998       break;
3999
4000     case FLOAT_TRUNCATE:
4001       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
4002       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4003           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4004         return XEXP (XEXP (x, 0), 0);
4005
4006       /* (float_truncate:SF (float_truncate:DF foo:XF))
4007          = (float_truncate:SF foo:XF).
4008          This may eliminate double rounding, so it is unsafe.
4009
4010          (float_truncate:SF (float_extend:XF foo:DF))
4011          = (float_truncate:SF foo:DF).
4012
4013          (float_truncate:DF (float_extend:XF foo:SF))
4014          = (float_extend:SF foo:DF).  */
4015       if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4016            && flag_unsafe_math_optimizations)
4017           || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4018         return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4019                                                             0)))
4020                                    > GET_MODE_SIZE (mode)
4021                                    ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4022                                    mode,
4023                                    XEXP (XEXP (x, 0), 0), mode);
4024
4025       /*  (float_truncate (float x)) is (float x)  */
4026       if (GET_CODE (XEXP (x, 0)) == FLOAT
4027           && (flag_unsafe_math_optimizations
4028               || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4029                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4030                       - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4031                                              GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4032         return simplify_gen_unary (FLOAT, mode,
4033                                    XEXP (XEXP (x, 0), 0),
4034                                    GET_MODE (XEXP (XEXP (x, 0), 0)));
4035
4036       /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4037          (OP:SF foo:SF) if OP is NEG or ABS.  */
4038       if ((GET_CODE (XEXP (x, 0)) == ABS
4039            || GET_CODE (XEXP (x, 0)) == NEG)
4040           && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4041           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4042         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4043                                    XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4044
4045       /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4046          is (float_truncate:SF x).  */
4047       if (GET_CODE (XEXP (x, 0)) == SUBREG
4048           && subreg_lowpart_p (XEXP (x, 0))
4049           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4050         return SUBREG_REG (XEXP (x, 0));
4051       break;
4052     case FLOAT_EXTEND:
4053       /*  (float_extend (float_extend x)) is (float_extend x)
4054
4055           (float_extend (float x)) is (float x) assuming that double
4056           rounding can't happen.
4057           */
4058       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4059           || (GET_CODE (XEXP (x, 0)) == FLOAT
4060               && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4061                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4062                       - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4063                                              GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4064         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4065                                    XEXP (XEXP (x, 0), 0),
4066                                    GET_MODE (XEXP (XEXP (x, 0), 0)));
4067
4068       break;
4069 #ifdef HAVE_cc0
4070     case COMPARE:
4071       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4072          using cc0, in which case we want to leave it as a COMPARE
4073          so we can distinguish it from a register-register-copy.  */
4074       if (XEXP (x, 1) == const0_rtx)
4075         return XEXP (x, 0);
4076
4077       /* x - 0 is the same as x unless x's mode has signed zeros and
4078          allows rounding towards -infinity.  Under those conditions,
4079          0 - 0 is -0.  */
4080       if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4081             && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4082           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4083         return XEXP (x, 0);
4084       break;
4085 #endif
4086
4087     case CONST:
4088       /* (const (const X)) can become (const X).  Do it this way rather than
4089          returning the inner CONST since CONST can be shared with a
4090          REG_EQUAL note.  */
4091       if (GET_CODE (XEXP (x, 0)) == CONST)
4092         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4093       break;
4094
4095 #ifdef HAVE_lo_sum
4096     case LO_SUM:
4097       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
4098          can add in an offset.  find_split_point will split this address up
4099          again if it doesn't match.  */
4100       if (GET_CODE (XEXP (x, 0)) == HIGH
4101           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4102         return XEXP (x, 1);
4103       break;
4104 #endif
4105
4106     case PLUS:
4107       /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4108        */
4109       if (GET_CODE (XEXP (x, 0)) == MULT
4110           && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4111         {
4112           rtx in1, in2;
4113
4114           in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4115           in2 = XEXP (XEXP (x, 0), 1);
4116           return gen_binary (MINUS, mode, XEXP (x, 1),
4117                              gen_binary (MULT, mode, in1, in2));
4118         }
4119
4120       /* If we have (plus (plus (A const) B)), associate it so that CONST is
4121          outermost.  That's because that's the way indexed addresses are
4122          supposed to appear.  This code used to check many more cases, but
4123          they are now checked elsewhere.  */
4124       if (GET_CODE (XEXP (x, 0)) == PLUS
4125           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4126         return gen_binary (PLUS, mode,
4127                            gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4128                                        XEXP (x, 1)),
4129                            XEXP (XEXP (x, 0), 1));
4130
4131       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4132          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4133          bit-field and can be replaced by either a sign_extend or a
4134          sign_extract.  The `and' may be a zero_extend and the two
4135          <c>, -<c> constants may be reversed.  */
4136       if (GET_CODE (XEXP (x, 0)) == XOR
4137           && GET_CODE (XEXP (x, 1)) == CONST_INT
4138           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4139           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4140           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4141               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4142           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4143           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4144                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4145                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4146                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4147               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4148                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4149                       == (unsigned int) i + 1))))
4150         return simplify_shift_const
4151           (NULL_RTX, ASHIFTRT, mode,
4152            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4153                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
4154                                  GET_MODE_BITSIZE (mode) - (i + 1)),
4155            GET_MODE_BITSIZE (mode) - (i + 1));
4156
4157       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4158          C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4159          is 1.  This produces better code than the alternative immediately
4160          below.  */
4161       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4162           && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4163               || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4164           && (reversed = reversed_comparison (XEXP (x, 0), mode,
4165                                               XEXP (XEXP (x, 0), 0),
4166                                               XEXP (XEXP (x, 0), 1))))
4167         return
4168           simplify_gen_unary (NEG, mode, reversed, mode);
4169
4170       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4171          can become (ashiftrt (ashift (xor x 1) C) C) where C is
4172          the bitsize of the mode - 1.  This allows simplification of
4173          "a = (b & 8) == 0;"  */
4174       if (XEXP (x, 1) == constm1_rtx
4175           && GET_CODE (XEXP (x, 0)) != REG
4176           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4177                 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
4178           && nonzero_bits (XEXP (x, 0), mode) == 1)
4179         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4180            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4181                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4182                                  GET_MODE_BITSIZE (mode) - 1),
4183            GET_MODE_BITSIZE (mode) - 1);
4184
4185       /* If we are adding two things that have no bits in common, convert
4186          the addition into an IOR.  This will often be further simplified,
4187          for example in cases like ((a & 1) + (a & 2)), which can
4188          become a & 3.  */
4189
4190       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4191           && (nonzero_bits (XEXP (x, 0), mode)
4192               & nonzero_bits (XEXP (x, 1), mode)) == 0)
4193         {
4194           /* Try to simplify the expression further.  */
4195           rtx tor = gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4196           temp = combine_simplify_rtx (tor, mode, last, in_dest);
4197
4198           /* If we could, great.  If not, do not go ahead with the IOR
4199              replacement, since PLUS appears in many special purpose
4200              address arithmetic instructions.  */
4201           if (GET_CODE (temp) != CLOBBER && temp != tor)
4202             return temp;
4203         }
4204       break;
4205
4206     case MINUS:
4207       /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4208          by reversing the comparison code if valid.  */
4209       if (STORE_FLAG_VALUE == 1
4210           && XEXP (x, 0) == const1_rtx
4211           && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
4212           && (reversed = reversed_comparison (XEXP (x, 1), mode,
4213                                               XEXP (XEXP (x, 1), 0),
4214                                               XEXP (XEXP (x, 1), 1))))
4215         return reversed;
4216
4217       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4218          (and <foo> (const_int pow2-1))  */
4219       if (GET_CODE (XEXP (x, 1)) == AND
4220           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4221           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4222           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4223         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4224                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4225
4226       /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4227        */
4228       if (GET_CODE (XEXP (x, 1)) == MULT
4229           && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4230         {
4231           rtx in1, in2;
4232
4233           in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4234           in2 = XEXP (XEXP (x, 1), 1);
4235           return gen_binary (PLUS, mode, gen_binary (MULT, mode, in1, in2),
4236                              XEXP (x, 0));
4237         }
4238
4239       /* Canonicalize (minus (neg A) (mult B C)) to
4240          (minus (mult (neg B) C) A).  */
4241       if (GET_CODE (XEXP (x, 1)) == MULT
4242           && GET_CODE (XEXP (x, 0)) == NEG)
4243         {
4244           rtx in1, in2;
4245
4246           in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4247           in2 = XEXP (XEXP (x, 1), 1);
4248           return gen_binary (MINUS, mode, gen_binary (MULT, mode, in1, in2),
4249                              XEXP (XEXP (x, 0), 0));
4250         }
4251
4252       /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4253          integers.  */
4254       if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4255         return gen_binary (MINUS, mode,
4256                            gen_binary (MINUS, mode, XEXP (x, 0),
4257                                        XEXP (XEXP (x, 1), 0)),
4258                            XEXP (XEXP (x, 1), 1));
4259       break;
4260
4261     case MULT:
4262       /* If we have (mult (plus A B) C), apply the distributive law and then
4263          the inverse distributive law to see if things simplify.  This
4264          occurs mostly in addresses, often when unrolling loops.  */
4265
4266       if (GET_CODE (XEXP (x, 0)) == PLUS)
4267         {
4268           x = apply_distributive_law
4269             (gen_binary (PLUS, mode,
4270                          gen_binary (MULT, mode,
4271                                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4272                          gen_binary (MULT, mode,
4273                                      XEXP (XEXP (x, 0), 1),
4274                                      copy_rtx (XEXP (x, 1)))));
4275
4276           if (GET_CODE (x) != MULT)
4277             return x;
4278         }
4279       /* Try simplify a*(b/c) as (a*b)/c.  */
4280       if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4281           && GET_CODE (XEXP (x, 0)) == DIV)
4282         {
4283           rtx tem = simplify_binary_operation (MULT, mode,
4284                                                XEXP (XEXP (x, 0), 0),
4285                                                XEXP (x, 1));
4286           if (tem)
4287             return gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4288         }
4289       break;
4290
4291     case UDIV:
4292       /* If this is a divide by a power of two, treat it as a shift if
4293          its first operand is a shift.  */
4294       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4295           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4296           && (GET_CODE (XEXP (x, 0)) == ASHIFT
4297               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4298               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4299               || GET_CODE (XEXP (x, 0)) == ROTATE
4300               || GET_CODE (XEXP (x, 0)) == ROTATERT))
4301         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4302       break;
4303
4304     case EQ:  case NE:
4305     case GT:  case GTU:  case GE:  case GEU:
4306     case LT:  case LTU:  case LE:  case LEU:
4307     case UNEQ:  case LTGT:
4308     case UNGT:  case UNGE:
4309     case UNLT:  case UNLE:
4310     case UNORDERED: case ORDERED:
4311       /* If the first operand is a condition code, we can't do anything
4312          with it.  */
4313       if (GET_CODE (XEXP (x, 0)) == COMPARE
4314           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4315               && ! CC0_P (XEXP (x, 0))))
4316         {
4317           rtx op0 = XEXP (x, 0);
4318           rtx op1 = XEXP (x, 1);
4319           enum rtx_code new_code;
4320
4321           if (GET_CODE (op0) == COMPARE)
4322             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4323
4324           /* Simplify our comparison, if possible.  */
4325           new_code = simplify_comparison (code, &op0, &op1);
4326
4327           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4328              if only the low-order bit is possibly nonzero in X (such as when
4329              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
4330              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
4331              known to be either 0 or -1, NE becomes a NEG and EQ becomes
4332              (plus X 1).
4333
4334              Remove any ZERO_EXTRACT we made when thinking this was a
4335              comparison.  It may now be simpler to use, e.g., an AND.  If a
4336              ZERO_EXTRACT is indeed appropriate, it will be placed back by
4337              the call to make_compound_operation in the SET case.  */
4338
4339           if (STORE_FLAG_VALUE == 1
4340               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4341               && op1 == const0_rtx
4342               && mode == GET_MODE (op0)
4343               && nonzero_bits (op0, mode) == 1)
4344             return gen_lowpart_for_combine (mode,
4345                                             expand_compound_operation (op0));
4346
4347           else if (STORE_FLAG_VALUE == 1
4348                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4349                    && op1 == const0_rtx
4350                    && mode == GET_MODE (op0)
4351                    && (num_sign_bit_copies (op0, mode)
4352                        == GET_MODE_BITSIZE (mode)))
4353             {
4354               op0 = expand_compound_operation (op0);
4355               return simplify_gen_unary (NEG, mode,
4356                                          gen_lowpart_for_combine (mode, op0),
4357                                          mode);
4358             }
4359
4360           else if (STORE_FLAG_VALUE == 1
4361                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4362                    && op1 == const0_rtx
4363                    && mode == GET_MODE (op0)
4364                    && nonzero_bits (op0, mode) == 1)
4365             {
4366               op0 = expand_compound_operation (op0);
4367               return gen_binary (XOR, mode,
4368                                  gen_lowpart_for_combine (mode, op0),
4369                                  const1_rtx);
4370             }
4371
4372           else if (STORE_FLAG_VALUE == 1
4373                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4374                    && op1 == const0_rtx
4375                    && mode == GET_MODE (op0)
4376                    && (num_sign_bit_copies (op0, mode)
4377                        == GET_MODE_BITSIZE (mode)))
4378             {
4379               op0 = expand_compound_operation (op0);
4380               return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4381             }
4382
4383           /* If STORE_FLAG_VALUE is -1, we have cases similar to
4384              those above.  */
4385           if (STORE_FLAG_VALUE == -1
4386               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4387               && op1 == const0_rtx
4388               && (num_sign_bit_copies (op0, mode)
4389                   == GET_MODE_BITSIZE (mode)))
4390             return gen_lowpart_for_combine (mode,
4391                                             expand_compound_operation (op0));
4392
4393           else if (STORE_FLAG_VALUE == -1
4394                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4395                    && op1 == const0_rtx
4396                    && mode == GET_MODE (op0)
4397                    && nonzero_bits (op0, mode) == 1)
4398             {
4399               op0 = expand_compound_operation (op0);
4400               return simplify_gen_unary (NEG, mode,
4401                                          gen_lowpart_for_combine (mode, op0),
4402                                          mode);
4403             }
4404
4405           else if (STORE_FLAG_VALUE == -1
4406                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4407                    && op1 == const0_rtx
4408                    && mode == GET_MODE (op0)
4409                    && (num_sign_bit_copies (op0, mode)
4410                        == GET_MODE_BITSIZE (mode)))
4411             {
4412               op0 = expand_compound_operation (op0);
4413               return simplify_gen_unary (NOT, mode,
4414                                          gen_lowpart_for_combine (mode, op0),
4415                                          mode);
4416             }
4417
4418           /* If X is 0/1, (eq X 0) is X-1.  */
4419           else if (STORE_FLAG_VALUE == -1
4420                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4421                    && op1 == const0_rtx
4422                    && mode == GET_MODE (op0)
4423                    && nonzero_bits (op0, mode) == 1)
4424             {
4425               op0 = expand_compound_operation (op0);
4426               return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4427             }
4428
4429           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4430              one bit that might be nonzero, we can convert (ne x 0) to
4431              (ashift x c) where C puts the bit in the sign bit.  Remove any
4432              AND with STORE_FLAG_VALUE when we are done, since we are only
4433              going to test the sign bit.  */
4434           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4435               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4436               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4437                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4438               && op1 == const0_rtx
4439               && mode == GET_MODE (op0)
4440               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4441             {
4442               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4443                                         expand_compound_operation (op0),
4444                                         GET_MODE_BITSIZE (mode) - 1 - i);
4445               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4446                 return XEXP (x, 0);
4447               else
4448                 return x;
4449             }
4450
4451           /* If the code changed, return a whole new comparison.  */
4452           if (new_code != code)
4453             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4454
4455           /* Otherwise, keep this operation, but maybe change its operands.
4456              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
4457           SUBST (XEXP (x, 0), op0);
4458           SUBST (XEXP (x, 1), op1);
4459         }
4460       break;
4461
4462     case IF_THEN_ELSE:
4463       return simplify_if_then_else (x);
4464
4465     case ZERO_EXTRACT:
4466     case SIGN_EXTRACT:
4467     case ZERO_EXTEND:
4468     case SIGN_EXTEND:
4469       /* If we are processing SET_DEST, we are done.  */
4470       if (in_dest)
4471         return x;
4472
4473       return expand_compound_operation (x);
4474
4475     case SET:
4476       return simplify_set (x);
4477
4478     case AND:
4479     case IOR:
4480     case XOR:
4481       return simplify_logical (x, last);
4482
4483     case ABS:
4484       /* (abs (neg <foo>)) -> (abs <foo>) */
4485       if (GET_CODE (XEXP (x, 0)) == NEG)
4486         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4487
4488       /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4489          do nothing.  */
4490       if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4491         break;
4492
4493       /* If operand is something known to be positive, ignore the ABS.  */
4494       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4495           || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4496                <= HOST_BITS_PER_WIDE_INT)
4497               && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4498                    & ((HOST_WIDE_INT) 1
4499                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4500                   == 0)))
4501         return XEXP (x, 0);
4502
4503       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
4504       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4505         return gen_rtx_NEG (mode, XEXP (x, 0));
4506
4507       break;
4508
4509     case FFS:
4510       /* (ffs (*_extend <X>)) = (ffs <X>) */
4511       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4512           || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4513         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4514       break;
4515
4516     case POPCOUNT:
4517     case PARITY:
4518       /* (pop* (zero_extend <X>)) = (pop* <X>) */
4519       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4520         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4521       break;
4522
4523     case FLOAT:
4524       /* (float (sign_extend <X>)) = (float <X>).  */
4525       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4526         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4527       break;
4528
4529     case ASHIFT:
4530     case LSHIFTRT:
4531     case ASHIFTRT:
4532     case ROTATE:
4533     case ROTATERT:
4534       /* If this is a shift by a constant amount, simplify it.  */
4535       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4536         return simplify_shift_const (x, code, mode, XEXP (x, 0),
4537                                      INTVAL (XEXP (x, 1)));
4538
4539 #ifdef SHIFT_COUNT_TRUNCATED
4540       else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4541         SUBST (XEXP (x, 1),
4542                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4543                               ((HOST_WIDE_INT) 1
4544                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4545                               - 1,
4546                               NULL_RTX, 0));
4547 #endif
4548
4549       break;
4550
4551     case VEC_SELECT:
4552       {
4553         rtx op0 = XEXP (x, 0);
4554         rtx op1 = XEXP (x, 1);
4555         int len;
4556
4557         if (GET_CODE (op1) != PARALLEL)
4558           abort ();
4559         len = XVECLEN (op1, 0);
4560         if (len == 1
4561             && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4562             && GET_CODE (op0) == VEC_CONCAT)
4563           {
4564             int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4565
4566             /* Try to find the element in the VEC_CONCAT.  */
4567             for (;;)
4568               {
4569                 if (GET_MODE (op0) == GET_MODE (x))
4570                   return op0;
4571                 if (GET_CODE (op0) == VEC_CONCAT)
4572                   {
4573                     HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4574                     if (op0_size < offset)
4575                       op0 = XEXP (op0, 0);
4576                     else
4577                       {
4578                         offset -= op0_size;
4579                         op0 = XEXP (op0, 1);
4580                       }
4581                   }
4582                 else
4583                   break;
4584               }
4585           }
4586       }
4587
4588       break;
4589
4590     default:
4591       break;
4592     }
4593
4594   return x;
4595 }
4596 \f
4597 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
4598
4599 static rtx
4600 simplify_if_then_else (rtx x)
4601 {
4602   enum machine_mode mode = GET_MODE (x);
4603   rtx cond = XEXP (x, 0);
4604   rtx true_rtx = XEXP (x, 1);
4605   rtx false_rtx = XEXP (x, 2);
4606   enum rtx_code true_code = GET_CODE (cond);
4607   int comparison_p = GET_RTX_CLASS (true_code) == '<';
4608   rtx temp;
4609   int i;
4610   enum rtx_code false_code;
4611   rtx reversed;
4612
4613   /* Simplify storing of the truth value.  */
4614   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4615     return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4616
4617   /* Also when the truth value has to be reversed.  */
4618   if (comparison_p
4619       && true_rtx == const0_rtx && false_rtx == const_true_rtx
4620       && (reversed = reversed_comparison (cond, mode, XEXP (cond, 0),
4621                                           XEXP (cond, 1))))
4622     return reversed;
4623
4624   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4625      in it is being compared against certain values.  Get the true and false
4626      comparisons and see if that says anything about the value of each arm.  */
4627
4628   if (comparison_p
4629       && ((false_code = combine_reversed_comparison_code (cond))
4630           != UNKNOWN)
4631       && GET_CODE (XEXP (cond, 0)) == REG)
4632     {
4633       HOST_WIDE_INT nzb;
4634       rtx from = XEXP (cond, 0);
4635       rtx true_val = XEXP (cond, 1);
4636       rtx false_val = true_val;
4637       int swapped = 0;
4638
4639       /* If FALSE_CODE is EQ, swap the codes and arms.  */
4640
4641       if (false_code == EQ)
4642         {
4643           swapped = 1, true_code = EQ, false_code = NE;
4644           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4645         }
4646
4647       /* If we are comparing against zero and the expression being tested has
4648          only a single bit that might be nonzero, that is its value when it is
4649          not equal to zero.  Similarly if it is known to be -1 or 0.  */
4650
4651       if (true_code == EQ && true_val == const0_rtx
4652           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4653         false_code = EQ, false_val = GEN_INT (nzb);
4654       else if (true_code == EQ && true_val == const0_rtx
4655                && (num_sign_bit_copies (from, GET_MODE (from))
4656                    == GET_MODE_BITSIZE (GET_MODE (from))))
4657         false_code = EQ, false_val = constm1_rtx;
4658
4659       /* Now simplify an arm if we know the value of the register in the
4660          branch and it is used in the arm.  Be careful due to the potential
4661          of locally-shared RTL.  */
4662
4663       if (reg_mentioned_p (from, true_rtx))
4664         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4665                                       from, true_val),
4666                       pc_rtx, pc_rtx, 0, 0);
4667       if (reg_mentioned_p (from, false_rtx))
4668         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4669                                    from, false_val),
4670                        pc_rtx, pc_rtx, 0, 0);
4671
4672       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4673       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4674
4675       true_rtx = XEXP (x, 1);
4676       false_rtx = XEXP (x, 2);
4677       true_code = GET_CODE (cond);
4678     }
4679
4680   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4681      reversed, do so to avoid needing two sets of patterns for
4682      subtract-and-branch insns.  Similarly if we have a constant in the true
4683      arm, the false arm is the same as the first operand of the comparison, or
4684      the false arm is more complicated than the true arm.  */
4685
4686   if (comparison_p
4687       && combine_reversed_comparison_code (cond) != UNKNOWN
4688       && (true_rtx == pc_rtx
4689           || (CONSTANT_P (true_rtx)
4690               && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4691           || true_rtx == const0_rtx
4692           || (GET_RTX_CLASS (GET_CODE (true_rtx)) == 'o'
4693               && GET_RTX_CLASS (GET_CODE (false_rtx)) != 'o')
4694           || (GET_CODE (true_rtx) == SUBREG
4695               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true_rtx))) == 'o'
4696               && GET_RTX_CLASS (GET_CODE (false_rtx)) != 'o')
4697           || reg_mentioned_p (true_rtx, false_rtx)
4698           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4699     {
4700       true_code = reversed_comparison_code (cond, NULL);
4701       SUBST (XEXP (x, 0),
4702              reversed_comparison (cond, GET_MODE (cond), XEXP (cond, 0),
4703                                   XEXP (cond, 1)));
4704
4705       SUBST (XEXP (x, 1), false_rtx);
4706       SUBST (XEXP (x, 2), true_rtx);
4707
4708       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4709       cond = XEXP (x, 0);
4710
4711       /* It is possible that the conditional has been simplified out.  */
4712       true_code = GET_CODE (cond);
4713       comparison_p = GET_RTX_CLASS (true_code) == '<';
4714     }
4715
4716   /* If the two arms are identical, we don't need the comparison.  */
4717
4718   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4719     return true_rtx;
4720
4721   /* Convert a == b ? b : a to "a".  */
4722   if (true_code == EQ && ! side_effects_p (cond)
4723       && !HONOR_NANS (mode)
4724       && rtx_equal_p (XEXP (cond, 0), false_rtx)
4725       && rtx_equal_p (XEXP (cond, 1), true_rtx))
4726     return false_rtx;
4727   else if (true_code == NE && ! side_effects_p (cond)
4728            && !HONOR_NANS (mode)
4729            && rtx_equal_p (XEXP (cond, 0), true_rtx)
4730            && rtx_equal_p (XEXP (cond, 1), false_rtx))
4731     return true_rtx;
4732
4733   /* Look for cases where we have (abs x) or (neg (abs X)).  */
4734
4735   if (GET_MODE_CLASS (mode) == MODE_INT
4736       && GET_CODE (false_rtx) == NEG
4737       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4738       && comparison_p
4739       && rtx_equal_p (true_rtx, XEXP (cond, 0))
4740       && ! side_effects_p (true_rtx))
4741     switch (true_code)
4742       {
4743       case GT:
4744       case GE:
4745         return simplify_gen_unary (ABS, mode, true_rtx, mode);
4746       case LT:
4747       case LE:
4748         return
4749           simplify_gen_unary (NEG, mode,
4750                               simplify_gen_unary (ABS, mode, true_rtx, mode),
4751                               mode);
4752       default:
4753         break;
4754       }
4755
4756   /* Look for MIN or MAX.  */
4757
4758   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4759       && comparison_p
4760       && rtx_equal_p (XEXP (cond, 0), true_rtx)
4761       && rtx_equal_p (XEXP (cond, 1), false_rtx)
4762       && ! side_effects_p (cond))
4763     switch (true_code)
4764       {
4765       case GE:
4766       case GT:
4767         return gen_binary (SMAX, mode, true_rtx, false_rtx);
4768       case LE:
4769       case LT:
4770         return gen_binary (SMIN, mode, true_rtx, false_rtx);
4771       case GEU:
4772       case GTU:
4773         return gen_binary (UMAX, mode, true_rtx, false_rtx);
4774       case LEU:
4775       case LTU:
4776         return gen_binary (UMIN, mode, true_rtx, false_rtx);
4777       default:
4778         break;
4779       }
4780
4781   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4782      second operand is zero, this can be done as (OP Z (mult COND C2)) where
4783      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4784      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4785      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4786      neither 1 or -1, but it isn't worth checking for.  */
4787
4788   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4789       && comparison_p
4790       && GET_MODE_CLASS (mode) == MODE_INT
4791       && ! side_effects_p (x))
4792     {
4793       rtx t = make_compound_operation (true_rtx, SET);
4794       rtx f = make_compound_operation (false_rtx, SET);
4795       rtx cond_op0 = XEXP (cond, 0);
4796       rtx cond_op1 = XEXP (cond, 1);
4797       enum rtx_code op = NIL, extend_op = NIL;
4798       enum machine_mode m = mode;
4799       rtx z = 0, c1 = NULL_RTX;
4800
4801       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4802            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4803            || GET_CODE (t) == ASHIFT
4804            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4805           && rtx_equal_p (XEXP (t, 0), f))
4806         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4807
4808       /* If an identity-zero op is commutative, check whether there
4809          would be a match if we swapped the operands.  */
4810       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4811                 || GET_CODE (t) == XOR)
4812                && rtx_equal_p (XEXP (t, 1), f))
4813         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4814       else if (GET_CODE (t) == SIGN_EXTEND
4815                && (GET_CODE (XEXP (t, 0)) == PLUS
4816                    || GET_CODE (XEXP (t, 0)) == MINUS
4817                    || GET_CODE (XEXP (t, 0)) == IOR
4818                    || GET_CODE (XEXP (t, 0)) == XOR
4819                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4820                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4821                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4822                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4823                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4824                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4825                && (num_sign_bit_copies (f, GET_MODE (f))
4826                    > (unsigned int)
4827                      (GET_MODE_BITSIZE (mode)
4828                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4829         {
4830           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4831           extend_op = SIGN_EXTEND;
4832           m = GET_MODE (XEXP (t, 0));
4833         }
4834       else if (GET_CODE (t) == SIGN_EXTEND
4835                && (GET_CODE (XEXP (t, 0)) == PLUS
4836                    || GET_CODE (XEXP (t, 0)) == IOR
4837                    || GET_CODE (XEXP (t, 0)) == XOR)
4838                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4839                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4840                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4841                && (num_sign_bit_copies (f, GET_MODE (f))
4842                    > (unsigned int)
4843                      (GET_MODE_BITSIZE (mode)
4844                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4845         {
4846           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4847           extend_op = SIGN_EXTEND;
4848           m = GET_MODE (XEXP (t, 0));
4849         }
4850       else if (GET_CODE (t) == ZERO_EXTEND
4851                && (GET_CODE (XEXP (t, 0)) == PLUS
4852                    || GET_CODE (XEXP (t, 0)) == MINUS
4853                    || GET_CODE (XEXP (t, 0)) == IOR
4854                    || GET_CODE (XEXP (t, 0)) == XOR
4855                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4856                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4857                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4858                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4859                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4860                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4861                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4862                && ((nonzero_bits (f, GET_MODE (f))
4863                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4864                    == 0))
4865         {
4866           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4867           extend_op = ZERO_EXTEND;
4868           m = GET_MODE (XEXP (t, 0));
4869         }
4870       else if (GET_CODE (t) == ZERO_EXTEND
4871                && (GET_CODE (XEXP (t, 0)) == PLUS
4872                    || GET_CODE (XEXP (t, 0)) == IOR
4873                    || GET_CODE (XEXP (t, 0)) == XOR)
4874                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4875                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4876                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4877                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4878                && ((nonzero_bits (f, GET_MODE (f))
4879                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4880                    == 0))
4881         {
4882           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4883           extend_op = ZERO_EXTEND;
4884           m = GET_MODE (XEXP (t, 0));
4885         }
4886
4887       if (z)
4888         {
4889           temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4890                         pc_rtx, pc_rtx, 0, 0);
4891           temp = gen_binary (MULT, m, temp,
4892                              gen_binary (MULT, m, c1, const_true_rtx));
4893           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4894           temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4895
4896           if (extend_op != NIL)
4897             temp = simplify_gen_unary (extend_op, mode, temp, m);
4898
4899           return temp;
4900         }
4901     }
4902
4903   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4904      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4905      negation of a single bit, we can convert this operation to a shift.  We
4906      can actually do this more generally, but it doesn't seem worth it.  */
4907
4908   if (true_code == NE && XEXP (cond, 1) == const0_rtx
4909       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
4910       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4911            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
4912           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4913                == GET_MODE_BITSIZE (mode))
4914               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
4915     return
4916       simplify_shift_const (NULL_RTX, ASHIFT, mode,
4917                             gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4918
4919   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
4920   if (true_code == NE && XEXP (cond, 1) == const0_rtx
4921       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
4922       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
4923           == nonzero_bits (XEXP (cond, 0), mode)
4924       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
4925     return XEXP (cond, 0);
4926
4927   return x;
4928 }
4929 \f
4930 /* Simplify X, a SET expression.  Return the new expression.  */
4931
4932 static rtx
4933 simplify_set (rtx x)
4934 {
4935   rtx src = SET_SRC (x);
4936   rtx dest = SET_DEST (x);
4937   enum machine_mode mode
4938     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4939   rtx other_insn;
4940   rtx *cc_use;
4941
4942   /* (set (pc) (return)) gets written as (return).  */
4943   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4944     return src;
4945
4946   /* Now that we know for sure which bits of SRC we are using, see if we can
4947      simplify the expression for the object knowing that we only need the
4948      low-order bits.  */
4949
4950   if (GET_MODE_CLASS (mode) == MODE_INT
4951       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
4952     {
4953       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
4954       SUBST (SET_SRC (x), src);
4955     }
4956
4957   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4958      the comparison result and try to simplify it unless we already have used
4959      undobuf.other_insn.  */
4960   if ((GET_MODE_CLASS (mode) == MODE_CC
4961        || GET_CODE (src) == COMPARE
4962        || CC0_P (dest))
4963       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4964       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4965       && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4966       && rtx_equal_p (XEXP (*cc_use, 0), dest))
4967     {
4968       enum rtx_code old_code = GET_CODE (*cc_use);
4969       enum rtx_code new_code;
4970       rtx op0, op1, tmp;
4971       int other_changed = 0;
4972       enum machine_mode compare_mode = GET_MODE (dest);
4973       enum machine_mode tmp_mode;
4974
4975       if (GET_CODE (src) == COMPARE)
4976         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4977       else
4978         op0 = src, op1 = const0_rtx;
4979
4980       /* Check whether the comparison is known at compile time.  */
4981       if (GET_MODE (op0) != VOIDmode)
4982         tmp_mode = GET_MODE (op0);
4983       else if (GET_MODE (op1) != VOIDmode)
4984         tmp_mode = GET_MODE (op1);
4985       else
4986         tmp_mode = compare_mode;
4987       tmp = simplify_relational_operation (old_code, tmp_mode, op0, op1);
4988       if (tmp != NULL_RTX)
4989         {
4990           rtx pat = PATTERN (other_insn);
4991           undobuf.other_insn = other_insn;
4992           SUBST (*cc_use, tmp);
4993
4994           /* Attempt to simplify CC user.  */
4995           if (GET_CODE (pat) == SET)
4996             {
4997               rtx new = simplify_rtx (SET_SRC (pat));
4998               if (new != NULL_RTX)
4999                 SUBST (SET_SRC (pat), new);
5000             }
5001
5002           /* Convert X into a no-op move.  */
5003           SUBST (SET_DEST (x), pc_rtx);
5004           SUBST (SET_SRC (x), pc_rtx);
5005           return x;
5006         }
5007
5008       /* Simplify our comparison, if possible.  */
5009       new_code = simplify_comparison (old_code, &op0, &op1);
5010
5011 #ifdef SELECT_CC_MODE
5012       /* If this machine has CC modes other than CCmode, check to see if we
5013          need to use a different CC mode here.  */
5014       compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5015
5016 #ifndef HAVE_cc0
5017       /* If the mode changed, we have to change SET_DEST, the mode in the
5018          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5019          a hard register, just build new versions with the proper mode.  If it
5020          is a pseudo, we lose unless it is only time we set the pseudo, in
5021          which case we can safely change its mode.  */
5022       if (compare_mode != GET_MODE (dest))
5023         {
5024           unsigned int regno = REGNO (dest);
5025           rtx new_dest = gen_rtx_REG (compare_mode, regno);
5026
5027           if (regno < FIRST_PSEUDO_REGISTER
5028               || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5029             {
5030               if (regno >= FIRST_PSEUDO_REGISTER)
5031                 SUBST (regno_reg_rtx[regno], new_dest);
5032
5033               SUBST (SET_DEST (x), new_dest);
5034               SUBST (XEXP (*cc_use, 0), new_dest);
5035               other_changed = 1;
5036
5037               dest = new_dest;
5038             }
5039         }
5040 #endif  /* cc0 */
5041 #endif  /* SELECT_CC_MODE */
5042
5043       /* If the code changed, we have to build a new comparison in
5044          undobuf.other_insn.  */
5045       if (new_code != old_code)
5046         {
5047           int other_changed_previously = other_changed;
5048           unsigned HOST_WIDE_INT mask;
5049
5050           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5051                                           dest, const0_rtx));
5052           other_changed = 1;
5053
5054           /* If the only change we made was to change an EQ into an NE or
5055              vice versa, OP0 has only one bit that might be nonzero, and OP1
5056              is zero, check if changing the user of the condition code will
5057              produce a valid insn.  If it won't, we can keep the original code
5058              in that insn by surrounding our operation with an XOR.  */
5059
5060           if (((old_code == NE && new_code == EQ)
5061                || (old_code == EQ && new_code == NE))
5062               && ! other_changed_previously && op1 == const0_rtx
5063               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5064               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5065             {
5066               rtx pat = PATTERN (other_insn), note = 0;
5067
5068               if ((recog_for_combine (&pat, other_insn, &note) < 0
5069                    && ! check_asm_operands (pat)))
5070                 {
5071                   PUT_CODE (*cc_use, old_code);
5072                   other_changed = 0;
5073
5074                   op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
5075                 }
5076             }
5077         }
5078
5079       if (other_changed)
5080         undobuf.other_insn = other_insn;
5081
5082 #ifdef HAVE_cc0
5083       /* If we are now comparing against zero, change our source if
5084          needed.  If we do not use cc0, we always have a COMPARE.  */
5085       if (op1 == const0_rtx && dest == cc0_rtx)
5086         {
5087           SUBST (SET_SRC (x), op0);
5088           src = op0;
5089         }
5090       else
5091 #endif
5092
5093       /* Otherwise, if we didn't previously have a COMPARE in the
5094          correct mode, we need one.  */
5095       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5096         {
5097           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5098           src = SET_SRC (x);
5099         }
5100       else
5101         {
5102           /* Otherwise, update the COMPARE if needed.  */
5103           SUBST (XEXP (src, 0), op0);
5104           SUBST (XEXP (src, 1), op1);
5105         }
5106     }
5107   else
5108     {
5109       /* Get SET_SRC in a form where we have placed back any
5110          compound expressions.  Then do the checks below.  */
5111       src = make_compound_operation (src, SET);
5112       SUBST (SET_SRC (x), src);
5113     }
5114
5115 #ifdef WORD_REGISTER_OPERATIONS
5116   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5117      and X being a REG or (subreg (reg)), we may be able to convert this to
5118      (set (subreg:m2 x) (op)).
5119
5120      On a machine where WORD_REGISTER_OPERATIONS is defined, this
5121      transformation is safe as long as M1 and M2 have the same number
5122      of words.
5123
5124      However, on a machine without WORD_REGISTER_OPERATIONS defined,
5125      we cannot apply this transformation because it would create a
5126      paradoxical subreg in SET_DEST.  */
5127
5128   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5129       && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
5130       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5131            / UNITS_PER_WORD)
5132           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5133                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5134 #ifdef CANNOT_CHANGE_MODE_CLASS
5135       && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
5136             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5137                                          GET_MODE (SUBREG_REG (src)),
5138                                          GET_MODE (src)))
5139 #endif
5140       && (GET_CODE (dest) == REG
5141           || (GET_CODE (dest) == SUBREG
5142               && GET_CODE (SUBREG_REG (dest)) == REG)))
5143     {
5144       SUBST (SET_DEST (x),
5145              gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
5146                                       dest));
5147       SUBST (SET_SRC (x), SUBREG_REG (src));
5148
5149       src = SET_SRC (x), dest = SET_DEST (x);
5150     }
5151 #endif
5152
5153 #ifdef HAVE_cc0
5154   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5155      in SRC.  */
5156   if (dest == cc0_rtx
5157       && GET_CODE (src) == SUBREG
5158       && subreg_lowpart_p (src)
5159       && (GET_MODE_BITSIZE (GET_MODE (src))
5160           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5161     {
5162       rtx inner = SUBREG_REG (src);
5163       enum machine_mode inner_mode = GET_MODE (inner);
5164
5165       /* Here we make sure that we don't have a sign bit on.  */
5166       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5167           && (nonzero_bits (inner, inner_mode)
5168               < ((unsigned HOST_WIDE_INT) 1
5169                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5170         {
5171           SUBST (SET_SRC (x), inner);
5172           src = SET_SRC (x);
5173         }
5174     }
5175 #endif
5176
5177 #ifdef LOAD_EXTEND_OP
5178   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5179      would require a paradoxical subreg.  Replace the subreg with a
5180      zero_extend to avoid the reload that would otherwise be required.  */
5181
5182   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5183       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
5184       && SUBREG_BYTE (src) == 0
5185       && (GET_MODE_SIZE (GET_MODE (src))
5186           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5187       && GET_CODE (SUBREG_REG (src)) == MEM)
5188     {
5189       SUBST (SET_SRC (x),
5190              gen_rtx (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5191                       GET_MODE (src), SUBREG_REG (src)));
5192
5193       src = SET_SRC (x);
5194     }
5195 #endif
5196
5197   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5198      are comparing an item known to be 0 or -1 against 0, use a logical
5199      operation instead. Check for one of the arms being an IOR of the other
5200      arm with some value.  We compute three terms to be IOR'ed together.  In
5201      practice, at most two will be nonzero.  Then we do the IOR's.  */
5202
5203   if (GET_CODE (dest) != PC
5204       && GET_CODE (src) == IF_THEN_ELSE
5205       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5206       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5207       && XEXP (XEXP (src, 0), 1) == const0_rtx
5208       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5209 #ifdef HAVE_conditional_move
5210       && ! can_conditionally_move_p (GET_MODE (src))
5211 #endif
5212       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5213                                GET_MODE (XEXP (XEXP (src, 0), 0)))
5214           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5215       && ! side_effects_p (src))
5216     {
5217       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5218                       ? XEXP (src, 1) : XEXP (src, 2));
5219       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5220                    ? XEXP (src, 2) : XEXP (src, 1));
5221       rtx term1 = const0_rtx, term2, term3;
5222
5223       if (GET_CODE (true_rtx) == IOR
5224           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5225         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5226       else if (GET_CODE (true_rtx) == IOR
5227                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5228         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5229       else if (GET_CODE (false_rtx) == IOR
5230                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5231         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5232       else if (GET_CODE (false_rtx) == IOR
5233                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5234         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5235
5236       term2 = gen_binary (AND, GET_MODE (src),
5237                           XEXP (XEXP (src, 0), 0), true_rtx);
5238       term3 = gen_binary (AND, GET_MODE (src),
5239                           simplify_gen_unary (NOT, GET_MODE (src),
5240                                               XEXP (XEXP (src, 0), 0),
5241                                               GET_MODE (src)),
5242                           false_rtx);
5243
5244       SUBST (SET_SRC (x),
5245              gen_binary (IOR, GET_MODE (src),
5246                          gen_binary (IOR, GET_MODE (src), term1, term2),
5247                          term3));
5248
5249       src = SET_SRC (x);
5250     }
5251
5252   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5253      whole thing fail.  */
5254   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5255     return src;
5256   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5257     return dest;
5258   else
5259     /* Convert this into a field assignment operation, if possible.  */
5260     return make_field_assignment (x);
5261 }
5262 \f
5263 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5264    result.  LAST is nonzero if this is the last retry.  */
5265
5266 static rtx
5267 simplify_logical (rtx x, int last)
5268 {
5269   enum machine_mode mode = GET_MODE (x);
5270   rtx op0 = XEXP (x, 0);
5271   rtx op1 = XEXP (x, 1);
5272   rtx reversed;
5273
5274   switch (GET_CODE (x))
5275     {
5276     case AND:
5277       /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5278          insn (and may simplify more).  */
5279       if (GET_CODE (op0) == XOR
5280           && rtx_equal_p (XEXP (op0, 0), op1)
5281           && ! side_effects_p (op1))
5282         x = gen_binary (AND, mode,
5283                         simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5284                         op1);
5285
5286       if (GET_CODE (op0) == XOR
5287           && rtx_equal_p (XEXP (op0, 1), op1)
5288           && ! side_effects_p (op1))
5289         x = gen_binary (AND, mode,
5290                         simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5291                         op1);
5292
5293       /* Similarly for (~(A ^ B)) & A.  */
5294       if (GET_CODE (op0) == NOT
5295           && GET_CODE (XEXP (op0, 0)) == XOR
5296           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5297           && ! side_effects_p (op1))
5298         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5299
5300       if (GET_CODE (op0) == NOT
5301           && GET_CODE (XEXP (op0, 0)) == XOR
5302           && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5303           && ! side_effects_p (op1))
5304         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5305
5306       /* We can call simplify_and_const_int only if we don't lose
5307          any (sign) bits when converting INTVAL (op1) to
5308          "unsigned HOST_WIDE_INT".  */
5309       if (GET_CODE (op1) == CONST_INT
5310           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5311               || INTVAL (op1) > 0))
5312         {
5313           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5314
5315           /* If we have (ior (and (X C1) C2)) and the next restart would be
5316              the last, simplify this by making C1 as small as possible
5317              and then exit.  */
5318           if (last
5319               && GET_CODE (x) == IOR && GET_CODE (op0) == AND
5320               && GET_CODE (XEXP (op0, 1)) == CONST_INT
5321               && GET_CODE (op1) == CONST_INT)
5322             return gen_binary (IOR, mode,
5323                                gen_binary (AND, mode, XEXP (op0, 0),
5324                                            GEN_INT (INTVAL (XEXP (op0, 1))
5325                                                     & ~INTVAL (op1))), op1);
5326
5327           if (GET_CODE (x) != AND)
5328             return x;
5329
5330           if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
5331               || GET_RTX_CLASS (GET_CODE (x)) == '2')
5332             op0 = XEXP (x, 0), op1 = XEXP (x, 1);
5333         }
5334
5335       /* Convert (A | B) & A to A.  */
5336       if (GET_CODE (op0) == IOR
5337           && (rtx_equal_p (XEXP (op0, 0), op1)
5338               || rtx_equal_p (XEXP (op0, 1), op1))
5339           && ! side_effects_p (XEXP (op0, 0))
5340           && ! side_effects_p (XEXP (op0, 1)))
5341         return op1;
5342
5343       /* In the following group of tests (and those in case IOR below),
5344          we start with some combination of logical operations and apply
5345          the distributive law followed by the inverse distributive law.
5346          Most of the time, this results in no change.  However, if some of
5347          the operands are the same or inverses of each other, simplifications
5348          will result.
5349
5350          For example, (and (ior A B) (not B)) can occur as the result of
5351          expanding a bit field assignment.  When we apply the distributive
5352          law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5353          which then simplifies to (and (A (not B))).
5354
5355          If we have (and (ior A B) C), apply the distributive law and then
5356          the inverse distributive law to see if things simplify.  */
5357
5358       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5359         {
5360           x = apply_distributive_law
5361             (gen_binary (GET_CODE (op0), mode,
5362                          gen_binary (AND, mode, XEXP (op0, 0), op1),
5363                          gen_binary (AND, mode, XEXP (op0, 1),
5364                                      copy_rtx (op1))));
5365           if (GET_CODE (x) != AND)
5366             return x;
5367         }
5368
5369       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5370         return apply_distributive_law
5371           (gen_binary (GET_CODE (op1), mode,
5372                        gen_binary (AND, mode, XEXP (op1, 0), op0),
5373                        gen_binary (AND, mode, XEXP (op1, 1),
5374                                    copy_rtx (op0))));
5375
5376       /* Similarly, taking advantage of the fact that
5377          (and (not A) (xor B C)) == (xor (ior A B) (ior A C))  */
5378
5379       if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5380         return apply_distributive_law
5381           (gen_binary (XOR, mode,
5382                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5383                        gen_binary (IOR, mode, copy_rtx (XEXP (op0, 0)),
5384                                    XEXP (op1, 1))));
5385
5386       else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5387         return apply_distributive_law
5388           (gen_binary (XOR, mode,
5389                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5390                        gen_binary (IOR, mode, copy_rtx (XEXP (op1, 0)), XEXP (op0, 1))));
5391       break;
5392
5393     case IOR:
5394       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
5395       if (GET_CODE (op1) == CONST_INT
5396           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5397           && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5398         return op1;
5399
5400       /* Convert (A & B) | A to A.  */
5401       if (GET_CODE (op0) == AND
5402           && (rtx_equal_p (XEXP (op0, 0), op1)
5403               || rtx_equal_p (XEXP (op0, 1), op1))
5404           && ! side_effects_p (XEXP (op0, 0))
5405           && ! side_effects_p (XEXP (op0, 1)))
5406         return op1;
5407
5408       /* If we have (ior (and A B) C), apply the distributive law and then
5409          the inverse distributive law to see if things simplify.  */
5410
5411       if (GET_CODE (op0) == AND)
5412         {
5413           x = apply_distributive_law
5414             (gen_binary (AND, mode,
5415                          gen_binary (IOR, mode, XEXP (op0, 0), op1),
5416                          gen_binary (IOR, mode, XEXP (op0, 1),
5417                                      copy_rtx (op1))));
5418
5419           if (GET_CODE (x) != IOR)
5420             return x;
5421         }
5422
5423       if (GET_CODE (op1) == AND)
5424         {
5425           x = apply_distributive_law
5426             (gen_binary (AND, mode,
5427                          gen_binary (IOR, mode, XEXP (op1, 0), op0),
5428                          gen_binary (IOR, mode, XEXP (op1, 1),
5429                                      copy_rtx (op0))));
5430
5431           if (GET_CODE (x) != IOR)
5432             return x;
5433         }
5434
5435       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5436          mode size to (rotate A CX).  */
5437
5438       if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5439            || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5440           && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5441           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5442           && GET_CODE (XEXP (op1, 1)) == CONST_INT
5443           && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5444               == GET_MODE_BITSIZE (mode)))
5445         return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5446                                (GET_CODE (op0) == ASHIFT
5447                                 ? XEXP (op0, 1) : XEXP (op1, 1)));
5448
5449       /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5450          a (sign_extend (plus ...)).  If so, OP1 is a CONST_INT, and the PLUS
5451          does not affect any of the bits in OP1, it can really be done
5452          as a PLUS and we can associate.  We do this by seeing if OP1
5453          can be safely shifted left C bits.  */
5454       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5455           && GET_CODE (XEXP (op0, 0)) == PLUS
5456           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5457           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5458           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5459         {
5460           int count = INTVAL (XEXP (op0, 1));
5461           HOST_WIDE_INT mask = INTVAL (op1) << count;
5462
5463           if (mask >> count == INTVAL (op1)
5464               && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5465             {
5466               SUBST (XEXP (XEXP (op0, 0), 1),
5467                      GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5468               return op0;
5469             }
5470         }
5471       break;
5472
5473     case XOR:
5474       /* If we are XORing two things that have no bits in common,
5475          convert them into an IOR.  This helps to detect rotation encoded
5476          using those methods and possibly other simplifications.  */
5477
5478       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5479           && (nonzero_bits (op0, mode)
5480               & nonzero_bits (op1, mode)) == 0)
5481         return (gen_binary (IOR, mode, op0, op1));
5482
5483       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5484          Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5485          (NOT y).  */
5486       {
5487         int num_negated = 0;
5488
5489         if (GET_CODE (op0) == NOT)
5490           num_negated++, op0 = XEXP (op0, 0);
5491         if (GET_CODE (op1) == NOT)
5492           num_negated++, op1 = XEXP (op1, 0);
5493
5494         if (num_negated == 2)
5495           {
5496             SUBST (XEXP (x, 0), op0);
5497             SUBST (XEXP (x, 1), op1);
5498           }
5499         else if (num_negated == 1)
5500           return
5501             simplify_gen_unary (NOT, mode, gen_binary (XOR, mode, op0, op1),
5502                                 mode);
5503       }
5504
5505       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
5506          correspond to a machine insn or result in further simplifications
5507          if B is a constant.  */
5508
5509       if (GET_CODE (op0) == AND
5510           && rtx_equal_p (XEXP (op0, 1), op1)
5511           && ! side_effects_p (op1))
5512         return gen_binary (AND, mode,
5513                            simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5514                            op1);
5515
5516       else if (GET_CODE (op0) == AND
5517                && rtx_equal_p (XEXP (op0, 0), op1)
5518                && ! side_effects_p (op1))
5519         return gen_binary (AND, mode,
5520                            simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5521                            op1);
5522
5523       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5524          comparison if STORE_FLAG_VALUE is 1.  */
5525       if (STORE_FLAG_VALUE == 1
5526           && op1 == const1_rtx
5527           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5528           && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5529                                               XEXP (op0, 1))))
5530         return reversed;
5531
5532       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5533          is (lt foo (const_int 0)), so we can perform the above
5534          simplification if STORE_FLAG_VALUE is 1.  */
5535
5536       if (STORE_FLAG_VALUE == 1
5537           && op1 == const1_rtx
5538           && GET_CODE (op0) == LSHIFTRT
5539           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5540           && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5541         return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5542
5543       /* (xor (comparison foo bar) (const_int sign-bit))
5544          when STORE_FLAG_VALUE is the sign bit.  */
5545       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5546           && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5547               == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5548           && op1 == const_true_rtx
5549           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5550           && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5551                                               XEXP (op0, 1))))
5552         return reversed;
5553
5554       break;
5555
5556     default:
5557       abort ();
5558     }
5559
5560   return x;
5561 }
5562 \f
5563 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5564    operations" because they can be replaced with two more basic operations.
5565    ZERO_EXTEND is also considered "compound" because it can be replaced with
5566    an AND operation, which is simpler, though only one operation.
5567
5568    The function expand_compound_operation is called with an rtx expression
5569    and will convert it to the appropriate shifts and AND operations,
5570    simplifying at each stage.
5571
5572    The function make_compound_operation is called to convert an expression
5573    consisting of shifts and ANDs into the equivalent compound expression.
5574    It is the inverse of this function, loosely speaking.  */
5575
5576 static rtx
5577 expand_compound_operation (rtx x)
5578 {
5579   unsigned HOST_WIDE_INT pos = 0, len;
5580   int unsignedp = 0;
5581   unsigned int modewidth;
5582   rtx tem;
5583
5584   switch (GET_CODE (x))
5585     {
5586     case ZERO_EXTEND:
5587       unsignedp = 1;
5588     case SIGN_EXTEND:
5589       /* We can't necessarily use a const_int for a multiword mode;
5590          it depends on implicitly extending the value.
5591          Since we don't know the right way to extend it,
5592          we can't tell whether the implicit way is right.
5593
5594          Even for a mode that is no wider than a const_int,
5595          we can't win, because we need to sign extend one of its bits through
5596          the rest of it, and we don't know which bit.  */
5597       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5598         return x;
5599
5600       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5601          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
5602          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5603          reloaded. If not for that, MEM's would very rarely be safe.
5604
5605          Reject MODEs bigger than a word, because we might not be able
5606          to reference a two-register group starting with an arbitrary register
5607          (and currently gen_lowpart might crash for a SUBREG).  */
5608
5609       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5610         return x;
5611
5612       /* Reject MODEs that aren't scalar integers because turning vector
5613          or complex modes into shifts causes problems.  */
5614
5615       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5616         return x;
5617
5618       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5619       /* If the inner object has VOIDmode (the only way this can happen
5620          is if it is an ASM_OPERANDS), we can't do anything since we don't
5621          know how much masking to do.  */
5622       if (len == 0)
5623         return x;
5624
5625       break;
5626
5627     case ZERO_EXTRACT:
5628       unsignedp = 1;
5629     case SIGN_EXTRACT:
5630       /* If the operand is a CLOBBER, just return it.  */
5631       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5632         return XEXP (x, 0);
5633
5634       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5635           || GET_CODE (XEXP (x, 2)) != CONST_INT
5636           || GET_MODE (XEXP (x, 0)) == VOIDmode)
5637         return x;
5638
5639       /* Reject MODEs that aren't scalar integers because turning vector
5640          or complex modes into shifts causes problems.  */
5641
5642       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5643         return x;
5644
5645       len = INTVAL (XEXP (x, 1));
5646       pos = INTVAL (XEXP (x, 2));
5647
5648       /* If this goes outside the object being extracted, replace the object
5649          with a (use (mem ...)) construct that only combine understands
5650          and is used only for this purpose.  */
5651       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5652         SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5653
5654       if (BITS_BIG_ENDIAN)
5655         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5656
5657       break;
5658
5659     default:
5660       return x;
5661     }
5662   /* Convert sign extension to zero extension, if we know that the high
5663      bit is not set, as this is easier to optimize.  It will be converted
5664      back to cheaper alternative in make_extraction.  */
5665   if (GET_CODE (x) == SIGN_EXTEND
5666       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5667           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5668                 & ~(((unsigned HOST_WIDE_INT)
5669                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5670                      >> 1))
5671                == 0)))
5672     {
5673       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5674       rtx temp2 = expand_compound_operation (temp);
5675
5676       /* Make sure this is a profitable operation.  */
5677       if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5678        return temp2;
5679       else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5680        return temp;
5681       else
5682        return x;
5683     }
5684
5685   /* We can optimize some special cases of ZERO_EXTEND.  */
5686   if (GET_CODE (x) == ZERO_EXTEND)
5687     {
5688       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5689          know that the last value didn't have any inappropriate bits
5690          set.  */
5691       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5692           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5693           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5694           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5695               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5696         return XEXP (XEXP (x, 0), 0);
5697
5698       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5699       if (GET_CODE (XEXP (x, 0)) == SUBREG
5700           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5701           && subreg_lowpart_p (XEXP (x, 0))
5702           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5703           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5704               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5705         return SUBREG_REG (XEXP (x, 0));
5706
5707       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5708          is a comparison and STORE_FLAG_VALUE permits.  This is like
5709          the first case, but it works even when GET_MODE (x) is larger
5710          than HOST_WIDE_INT.  */
5711       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5712           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5713           && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5714           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5715               <= HOST_BITS_PER_WIDE_INT)
5716           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5717               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5718         return XEXP (XEXP (x, 0), 0);
5719
5720       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5721       if (GET_CODE (XEXP (x, 0)) == SUBREG
5722           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5723           && subreg_lowpart_p (XEXP (x, 0))
5724           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5725           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5726               <= HOST_BITS_PER_WIDE_INT)
5727           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5728               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5729         return SUBREG_REG (XEXP (x, 0));
5730
5731     }
5732
5733   /* If we reach here, we want to return a pair of shifts.  The inner
5734      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
5735      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
5736      logical depending on the value of UNSIGNEDP.
5737
5738      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5739      converted into an AND of a shift.
5740
5741      We must check for the case where the left shift would have a negative
5742      count.  This can happen in a case like (x >> 31) & 255 on machines
5743      that can't shift by a constant.  On those machines, we would first
5744      combine the shift with the AND to produce a variable-position
5745      extraction.  Then the constant of 31 would be substituted in to produce
5746      a such a position.  */
5747
5748   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5749   if (modewidth + len >= pos)
5750     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5751                                 GET_MODE (x),
5752                                 simplify_shift_const (NULL_RTX, ASHIFT,
5753                                                       GET_MODE (x),
5754                                                       XEXP (x, 0),
5755                                                       modewidth - pos - len),
5756                                 modewidth - len);
5757
5758   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5759     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5760                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
5761                                                         GET_MODE (x),
5762                                                         XEXP (x, 0), pos),
5763                                   ((HOST_WIDE_INT) 1 << len) - 1);
5764   else
5765     /* Any other cases we can't handle.  */
5766     return x;
5767
5768   /* If we couldn't do this for some reason, return the original
5769      expression.  */
5770   if (GET_CODE (tem) == CLOBBER)
5771     return x;
5772
5773   return tem;
5774 }
5775 \f
5776 /* X is a SET which contains an assignment of one object into
5777    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5778    or certain SUBREGS). If possible, convert it into a series of
5779    logical operations.
5780
5781    We half-heartedly support variable positions, but do not at all
5782    support variable lengths.  */
5783
5784 static rtx
5785 expand_field_assignment (rtx x)
5786 {
5787   rtx inner;
5788   rtx pos;                      /* Always counts from low bit.  */
5789   int len;
5790   rtx mask;
5791   enum machine_mode compute_mode;
5792
5793   /* Loop until we find something we can't simplify.  */
5794   while (1)
5795     {
5796       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5797           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5798         {
5799           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5800           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5801           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5802         }
5803       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5804                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5805         {
5806           inner = XEXP (SET_DEST (x), 0);
5807           len = INTVAL (XEXP (SET_DEST (x), 1));
5808           pos = XEXP (SET_DEST (x), 2);
5809
5810           /* If the position is constant and spans the width of INNER,
5811              surround INNER  with a USE to indicate this.  */
5812           if (GET_CODE (pos) == CONST_INT
5813               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5814             inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5815
5816           if (BITS_BIG_ENDIAN)
5817             {
5818               if (GET_CODE (pos) == CONST_INT)
5819                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5820                                - INTVAL (pos));
5821               else if (GET_CODE (pos) == MINUS
5822                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
5823                        && (INTVAL (XEXP (pos, 1))
5824                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5825                 /* If position is ADJUST - X, new position is X.  */
5826                 pos = XEXP (pos, 0);
5827               else
5828                 pos = gen_binary (MINUS, GET_MODE (pos),
5829                                   GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5830                                            - len),
5831                                   pos);
5832             }
5833         }
5834
5835       /* A SUBREG between two modes that occupy the same numbers of words
5836          can be done by moving the SUBREG to the source.  */
5837       else if (GET_CODE (SET_DEST (x)) == SUBREG
5838                /* We need SUBREGs to compute nonzero_bits properly.  */
5839                && nonzero_sign_valid
5840                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5841                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5842                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5843                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5844         {
5845           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5846                            gen_lowpart_for_combine
5847                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
5848                             SET_SRC (x)));
5849           continue;
5850         }
5851       else
5852         break;
5853
5854       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5855         inner = SUBREG_REG (inner);
5856
5857       compute_mode = GET_MODE (inner);
5858
5859       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
5860       if (! SCALAR_INT_MODE_P (compute_mode))
5861         {
5862           enum machine_mode imode;
5863
5864           /* Don't do anything for vector or complex integral types.  */
5865           if (! FLOAT_MODE_P (compute_mode))
5866             break;
5867
5868           /* Try to find an integral mode to pun with.  */
5869           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5870           if (imode == BLKmode)
5871             break;
5872
5873           compute_mode = imode;
5874           inner = gen_lowpart_for_combine (imode, inner);
5875         }
5876
5877       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
5878       if (len < HOST_BITS_PER_WIDE_INT)
5879         mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5880       else
5881         break;
5882
5883       /* Now compute the equivalent expression.  Make a copy of INNER
5884          for the SET_DEST in case it is a MEM into which we will substitute;
5885          we don't want shared RTL in that case.  */
5886       x = gen_rtx_SET
5887         (VOIDmode, copy_rtx (inner),
5888          gen_binary (IOR, compute_mode,
5889                      gen_binary (AND, compute_mode,
5890                                  simplify_gen_unary (NOT, compute_mode,
5891                                                      gen_binary (ASHIFT,
5892                                                                  compute_mode,
5893                                                                  mask, pos),
5894                                                      compute_mode),
5895                                  inner),
5896                      gen_binary (ASHIFT, compute_mode,
5897                                  gen_binary (AND, compute_mode,
5898                                              gen_lowpart_for_combine
5899                                              (compute_mode, SET_SRC (x)),
5900                                              mask),
5901                                  pos)));
5902     }
5903
5904   return x;
5905 }
5906 \f
5907 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
5908    it is an RTX that represents a variable starting position; otherwise,
5909    POS is the (constant) starting bit position (counted from the LSB).
5910
5911    INNER may be a USE.  This will occur when we started with a bitfield
5912    that went outside the boundary of the object in memory, which is
5913    allowed on most machines.  To isolate this case, we produce a USE
5914    whose mode is wide enough and surround the MEM with it.  The only
5915    code that understands the USE is this routine.  If it is not removed,
5916    it will cause the resulting insn not to match.
5917
5918    UNSIGNEDP is nonzero for an unsigned reference and zero for a
5919    signed reference.
5920
5921    IN_DEST is nonzero if this is a reference in the destination of a
5922    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
5923    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5924    be used.
5925
5926    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
5927    ZERO_EXTRACT should be built even for bits starting at bit 0.
5928
5929    MODE is the desired mode of the result (if IN_DEST == 0).
5930
5931    The result is an RTX for the extraction or NULL_RTX if the target
5932    can't handle it.  */
5933
5934 static rtx
5935 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
5936                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
5937                  int in_dest, int in_compare)
5938 {
5939   /* This mode describes the size of the storage area
5940      to fetch the overall value from.  Within that, we
5941      ignore the POS lowest bits, etc.  */
5942   enum machine_mode is_mode = GET_MODE (inner);
5943   enum machine_mode inner_mode;
5944   enum machine_mode wanted_inner_mode = byte_mode;
5945   enum machine_mode wanted_inner_reg_mode = word_mode;
5946   enum machine_mode pos_mode = word_mode;
5947   enum machine_mode extraction_mode = word_mode;
5948   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5949   int spans_byte = 0;
5950   rtx new = 0;
5951   rtx orig_pos_rtx = pos_rtx;
5952   HOST_WIDE_INT orig_pos;
5953
5954   /* Get some information about INNER and get the innermost object.  */
5955   if (GET_CODE (inner) == USE)
5956     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
5957     /* We don't need to adjust the position because we set up the USE
5958        to pretend that it was a full-word object.  */
5959     spans_byte = 1, inner = XEXP (inner, 0);
5960   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5961     {
5962       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5963          consider just the QI as the memory to extract from.
5964          The subreg adds or removes high bits; its mode is
5965          irrelevant to the meaning of this extraction,
5966          since POS and LEN count from the lsb.  */
5967       if (GET_CODE (SUBREG_REG (inner)) == MEM)
5968         is_mode = GET_MODE (SUBREG_REG (inner));
5969       inner = SUBREG_REG (inner);
5970     }
5971   else if (GET_CODE (inner) == ASHIFT
5972            && GET_CODE (XEXP (inner, 1)) == CONST_INT
5973            && pos_rtx == 0 && pos == 0
5974            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
5975     {
5976       /* We're extracting the least significant bits of an rtx
5977          (ashift X (const_int C)), where LEN > C.  Extract the
5978          least significant (LEN - C) bits of X, giving an rtx
5979          whose mode is MODE, then shift it left C times.  */
5980       new = make_extraction (mode, XEXP (inner, 0),
5981                              0, 0, len - INTVAL (XEXP (inner, 1)),
5982                              unsignedp, in_dest, in_compare);
5983       if (new != 0)
5984         return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
5985     }
5986
5987   inner_mode = GET_MODE (inner);
5988
5989   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5990     pos = INTVAL (pos_rtx), pos_rtx = 0;
5991
5992   /* See if this can be done without an extraction.  We never can if the
5993      width of the field is not the same as that of some integer mode. For
5994      registers, we can only avoid the extraction if the position is at the
5995      low-order bit and this is either not in the destination or we have the
5996      appropriate STRICT_LOW_PART operation available.
5997
5998      For MEM, we can avoid an extract if the field starts on an appropriate
5999      boundary and we can change the mode of the memory reference.  However,
6000      we cannot directly access the MEM if we have a USE and the underlying
6001      MEM is not TMODE.  This combination means that MEM was being used in a
6002      context where bits outside its mode were being referenced; that is only
6003      valid in bit-field insns.  */
6004
6005   if (tmode != BLKmode
6006       && ! (spans_byte && inner_mode != tmode)
6007       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6008            && GET_CODE (inner) != MEM
6009            && (! in_dest
6010                || (GET_CODE (inner) == REG
6011                    && have_insn_for (STRICT_LOW_PART, tmode))))
6012           || (GET_CODE (inner) == MEM && pos_rtx == 0
6013               && (pos
6014                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6015                      : BITS_PER_UNIT)) == 0
6016               /* We can't do this if we are widening INNER_MODE (it
6017                  may not be aligned, for one thing).  */
6018               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6019               && (inner_mode == tmode
6020                   || (! mode_dependent_address_p (XEXP (inner, 0))
6021                       && ! MEM_VOLATILE_P (inner))))))
6022     {
6023       /* If INNER is a MEM, make a new MEM that encompasses just the desired
6024          field.  If the original and current mode are the same, we need not
6025          adjust the offset.  Otherwise, we do if bytes big endian.
6026
6027          If INNER is not a MEM, get a piece consisting of just the field
6028          of interest (in this case POS % BITS_PER_WORD must be 0).  */
6029
6030       if (GET_CODE (inner) == MEM)
6031         {
6032           HOST_WIDE_INT offset;
6033
6034           /* POS counts from lsb, but make OFFSET count in memory order.  */
6035           if (BYTES_BIG_ENDIAN)
6036             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6037           else
6038             offset = pos / BITS_PER_UNIT;
6039
6040           new = adjust_address_nv (inner, tmode, offset);
6041         }
6042       else if (GET_CODE (inner) == REG)
6043         {
6044           if (tmode != inner_mode)
6045             {
6046               /* We can't call gen_lowpart_for_combine in a DEST since we
6047                  always want a SUBREG (see below) and it would sometimes
6048                  return a new hard register.  */
6049               if (pos || in_dest)
6050                 {
6051                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6052
6053                   if (WORDS_BIG_ENDIAN
6054                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6055                     final_word = ((GET_MODE_SIZE (inner_mode)
6056                                    - GET_MODE_SIZE (tmode))
6057                                   / UNITS_PER_WORD) - final_word;
6058
6059                   final_word *= UNITS_PER_WORD;
6060                   if (BYTES_BIG_ENDIAN &&
6061                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6062                     final_word += (GET_MODE_SIZE (inner_mode)
6063                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6064
6065                   /* Avoid creating invalid subregs, for example when
6066                      simplifying (x>>32)&255.  */
6067                   if (final_word >= GET_MODE_SIZE (inner_mode))
6068                     return NULL_RTX;
6069
6070                   new = gen_rtx_SUBREG (tmode, inner, final_word);
6071                 }
6072               else
6073                 new = gen_lowpart_for_combine (tmode, inner);
6074             }
6075           else
6076             new = inner;
6077         }
6078       else
6079         new = force_to_mode (inner, tmode,
6080                              len >= HOST_BITS_PER_WIDE_INT
6081                              ? ~(unsigned HOST_WIDE_INT) 0
6082                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6083                              NULL_RTX, 0);
6084
6085       /* If this extraction is going into the destination of a SET,
6086          make a STRICT_LOW_PART unless we made a MEM.  */
6087
6088       if (in_dest)
6089         return (GET_CODE (new) == MEM ? new
6090                 : (GET_CODE (new) != SUBREG
6091                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
6092                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6093
6094       if (mode == tmode)
6095         return new;
6096
6097       if (GET_CODE (new) == CONST_INT)
6098         return gen_int_mode (INTVAL (new), mode);
6099
6100       /* If we know that no extraneous bits are set, and that the high
6101          bit is not set, convert the extraction to the cheaper of
6102          sign and zero extension, that are equivalent in these cases.  */
6103       if (flag_expensive_optimizations
6104           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6105               && ((nonzero_bits (new, tmode)
6106                    & ~(((unsigned HOST_WIDE_INT)
6107                         GET_MODE_MASK (tmode))
6108                        >> 1))
6109                   == 0)))
6110         {
6111           rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6112           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6113
6114           /* Prefer ZERO_EXTENSION, since it gives more information to
6115              backends.  */
6116           if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6117             return temp;
6118           return temp1;
6119         }
6120
6121       /* Otherwise, sign- or zero-extend unless we already are in the
6122          proper mode.  */
6123
6124       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6125                              mode, new));
6126     }
6127
6128   /* Unless this is a COMPARE or we have a funny memory reference,
6129      don't do anything with zero-extending field extracts starting at
6130      the low-order bit since they are simple AND operations.  */
6131   if (pos_rtx == 0 && pos == 0 && ! in_dest
6132       && ! in_compare && ! spans_byte && unsignedp)
6133     return 0;
6134
6135   /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6136      we would be spanning bytes or if the position is not a constant and the
6137      length is not 1.  In all other cases, we would only be going outside
6138      our object in cases when an original shift would have been
6139      undefined.  */
6140   if (! spans_byte && GET_CODE (inner) == MEM
6141       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6142           || (pos_rtx != 0 && len != 1)))
6143     return 0;
6144
6145   /* Get the mode to use should INNER not be a MEM, the mode for the position,
6146      and the mode for the result.  */
6147   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6148     {
6149       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6150       pos_mode = mode_for_extraction (EP_insv, 2);
6151       extraction_mode = mode_for_extraction (EP_insv, 3);
6152     }
6153
6154   if (! in_dest && unsignedp
6155       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6156     {
6157       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6158       pos_mode = mode_for_extraction (EP_extzv, 3);
6159       extraction_mode = mode_for_extraction (EP_extzv, 0);
6160     }
6161
6162   if (! in_dest && ! unsignedp
6163       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6164     {
6165       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6166       pos_mode = mode_for_extraction (EP_extv, 3);
6167       extraction_mode = mode_for_extraction (EP_extv, 0);
6168     }
6169
6170   /* Never narrow an object, since that might not be safe.  */
6171
6172   if (mode != VOIDmode
6173       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6174     extraction_mode = mode;
6175
6176   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6177       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6178     pos_mode = GET_MODE (pos_rtx);
6179
6180   /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6181      if we have to change the mode of memory and cannot, the desired mode is
6182      EXTRACTION_MODE.  */
6183   if (GET_CODE (inner) != MEM)
6184     wanted_inner_mode = wanted_inner_reg_mode;
6185   else if (inner_mode != wanted_inner_mode
6186            && (mode_dependent_address_p (XEXP (inner, 0))
6187                || MEM_VOLATILE_P (inner)))
6188     wanted_inner_mode = extraction_mode;
6189
6190   orig_pos = pos;
6191
6192   if (BITS_BIG_ENDIAN)
6193     {
6194       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6195          BITS_BIG_ENDIAN style.  If position is constant, compute new
6196          position.  Otherwise, build subtraction.
6197          Note that POS is relative to the mode of the original argument.
6198          If it's a MEM we need to recompute POS relative to that.
6199          However, if we're extracting from (or inserting into) a register,
6200          we want to recompute POS relative to wanted_inner_mode.  */
6201       int width = (GET_CODE (inner) == MEM
6202                    ? GET_MODE_BITSIZE (is_mode)
6203                    : GET_MODE_BITSIZE (wanted_inner_mode));
6204
6205       if (pos_rtx == 0)
6206         pos = width - len - pos;
6207       else
6208         pos_rtx
6209           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6210       /* POS may be less than 0 now, but we check for that below.
6211          Note that it can only be less than 0 if GET_CODE (inner) != MEM.  */
6212     }
6213
6214   /* If INNER has a wider mode, make it smaller.  If this is a constant
6215      extract, try to adjust the byte to point to the byte containing
6216      the value.  */
6217   if (wanted_inner_mode != VOIDmode
6218       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6219       && ((GET_CODE (inner) == MEM
6220            && (inner_mode == wanted_inner_mode
6221                || (! mode_dependent_address_p (XEXP (inner, 0))
6222                    && ! MEM_VOLATILE_P (inner))))))
6223     {
6224       int offset = 0;
6225
6226       /* The computations below will be correct if the machine is big
6227          endian in both bits and bytes or little endian in bits and bytes.
6228          If it is mixed, we must adjust.  */
6229
6230       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6231          adjust OFFSET to compensate.  */
6232       if (BYTES_BIG_ENDIAN
6233           && ! spans_byte
6234           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6235         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6236
6237       /* If this is a constant position, we can move to the desired byte.  */
6238       if (pos_rtx == 0)
6239         {
6240           offset += pos / BITS_PER_UNIT;
6241           pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6242         }
6243
6244       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6245           && ! spans_byte
6246           && is_mode != wanted_inner_mode)
6247         offset = (GET_MODE_SIZE (is_mode)
6248                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6249
6250       if (offset != 0 || inner_mode != wanted_inner_mode)
6251         inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6252     }
6253
6254   /* If INNER is not memory, we can always get it into the proper mode.  If we
6255      are changing its mode, POS must be a constant and smaller than the size
6256      of the new mode.  */
6257   else if (GET_CODE (inner) != MEM)
6258     {
6259       if (GET_MODE (inner) != wanted_inner_mode
6260           && (pos_rtx != 0
6261               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6262         return 0;
6263
6264       inner = force_to_mode (inner, wanted_inner_mode,
6265                              pos_rtx
6266                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6267                              ? ~(unsigned HOST_WIDE_INT) 0
6268                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6269                                 << orig_pos),
6270                              NULL_RTX, 0);
6271     }
6272
6273   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6274      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6275   if (pos_rtx != 0
6276       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6277     {
6278       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6279
6280       /* If we know that no extraneous bits are set, and that the high
6281          bit is not set, convert extraction to cheaper one - either
6282          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6283          cases.  */
6284       if (flag_expensive_optimizations
6285           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6286               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6287                    & ~(((unsigned HOST_WIDE_INT)
6288                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6289                        >> 1))
6290                   == 0)))
6291         {
6292           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6293
6294           /* Prefer ZERO_EXTENSION, since it gives more information to
6295              backends.  */
6296           if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6297             temp = temp1;
6298         }
6299       pos_rtx = temp;
6300     }
6301   else if (pos_rtx != 0
6302            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6303     pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
6304
6305   /* Make POS_RTX unless we already have it and it is correct.  If we don't
6306      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6307      be a CONST_INT.  */
6308   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6309     pos_rtx = orig_pos_rtx;
6310
6311   else if (pos_rtx == 0)
6312     pos_rtx = GEN_INT (pos);
6313
6314   /* Make the required operation.  See if we can use existing rtx.  */
6315   new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6316                          extraction_mode, inner, GEN_INT (len), pos_rtx);
6317   if (! in_dest)
6318     new = gen_lowpart_for_combine (mode, new);
6319
6320   return new;
6321 }
6322 \f
6323 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6324    with any other operations in X.  Return X without that shift if so.  */
6325
6326 static rtx
6327 extract_left_shift (rtx x, int count)
6328 {
6329   enum rtx_code code = GET_CODE (x);
6330   enum machine_mode mode = GET_MODE (x);
6331   rtx tem;
6332
6333   switch (code)
6334     {
6335     case ASHIFT:
6336       /* This is the shift itself.  If it is wide enough, we will return
6337          either the value being shifted if the shift count is equal to
6338          COUNT or a shift for the difference.  */
6339       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6340           && INTVAL (XEXP (x, 1)) >= count)
6341         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6342                                      INTVAL (XEXP (x, 1)) - count);
6343       break;
6344
6345     case NEG:  case NOT:
6346       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6347         return simplify_gen_unary (code, mode, tem, mode);
6348
6349       break;
6350
6351     case PLUS:  case IOR:  case XOR:  case AND:
6352       /* If we can safely shift this constant and we find the inner shift,
6353          make a new operation.  */
6354       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6355           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6356           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6357         return gen_binary (code, mode, tem,
6358                            GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6359
6360       break;
6361
6362     default:
6363       break;
6364     }
6365
6366   return 0;
6367 }
6368 \f
6369 /* Look at the expression rooted at X.  Look for expressions
6370    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6371    Form these expressions.
6372
6373    Return the new rtx, usually just X.
6374
6375    Also, for machines like the VAX that don't have logical shift insns,
6376    try to convert logical to arithmetic shift operations in cases where
6377    they are equivalent.  This undoes the canonicalizations to logical
6378    shifts done elsewhere.
6379
6380    We try, as much as possible, to re-use rtl expressions to save memory.
6381
6382    IN_CODE says what kind of expression we are processing.  Normally, it is
6383    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
6384    being kludges), it is MEM.  When processing the arguments of a comparison
6385    or a COMPARE against zero, it is COMPARE.  */
6386
6387 static rtx
6388 make_compound_operation (rtx x, enum rtx_code in_code)
6389 {
6390   enum rtx_code code = GET_CODE (x);
6391   enum machine_mode mode = GET_MODE (x);
6392   int mode_width = GET_MODE_BITSIZE (mode);
6393   rtx rhs, lhs;
6394   enum rtx_code next_code;
6395   int i;
6396   rtx new = 0;
6397   rtx tem;
6398   const char *fmt;
6399
6400   /* Select the code to be used in recursive calls.  Once we are inside an
6401      address, we stay there.  If we have a comparison, set to COMPARE,
6402      but once inside, go back to our default of SET.  */
6403
6404   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6405                : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6406                   && XEXP (x, 1) == const0_rtx) ? COMPARE
6407                : in_code == COMPARE ? SET : in_code);
6408
6409   /* Process depending on the code of this operation.  If NEW is set
6410      nonzero, it will be returned.  */
6411
6412   switch (code)
6413     {
6414     case ASHIFT:
6415       /* Convert shifts by constants into multiplications if inside
6416          an address.  */
6417       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6418           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6419           && INTVAL (XEXP (x, 1)) >= 0)
6420         {
6421           new = make_compound_operation (XEXP (x, 0), next_code);
6422           new = gen_rtx_MULT (mode, new,
6423                               GEN_INT ((HOST_WIDE_INT) 1
6424                                        << INTVAL (XEXP (x, 1))));
6425         }
6426       break;
6427
6428     case AND:
6429       /* If the second operand is not a constant, we can't do anything
6430          with it.  */
6431       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6432         break;
6433
6434       /* If the constant is a power of two minus one and the first operand
6435          is a logical right shift, make an extraction.  */
6436       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6437           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6438         {
6439           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6440           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6441                                  0, in_code == COMPARE);
6442         }
6443
6444       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
6445       else if (GET_CODE (XEXP (x, 0)) == SUBREG
6446                && subreg_lowpart_p (XEXP (x, 0))
6447                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6448                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6449         {
6450           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6451                                          next_code);
6452           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6453                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6454                                  0, in_code == COMPARE);
6455         }
6456       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
6457       else if ((GET_CODE (XEXP (x, 0)) == XOR
6458                 || GET_CODE (XEXP (x, 0)) == IOR)
6459                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6460                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6461                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6462         {
6463           /* Apply the distributive law, and then try to make extractions.  */
6464           new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6465                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6466                                              XEXP (x, 1)),
6467                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6468                                              XEXP (x, 1)));
6469           new = make_compound_operation (new, in_code);
6470         }
6471
6472       /* If we are have (and (rotate X C) M) and C is larger than the number
6473          of bits in M, this is an extraction.  */
6474
6475       else if (GET_CODE (XEXP (x, 0)) == ROTATE
6476                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6477                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6478                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6479         {
6480           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6481           new = make_extraction (mode, new,
6482                                  (GET_MODE_BITSIZE (mode)
6483                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
6484                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
6485         }
6486
6487       /* On machines without logical shifts, if the operand of the AND is
6488          a logical shift and our mask turns off all the propagated sign
6489          bits, we can replace the logical shift with an arithmetic shift.  */
6490       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6491                && !have_insn_for (LSHIFTRT, mode)
6492                && have_insn_for (ASHIFTRT, mode)
6493                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6494                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6495                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6496                && mode_width <= HOST_BITS_PER_WIDE_INT)
6497         {
6498           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6499
6500           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6501           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6502             SUBST (XEXP (x, 0),
6503                    gen_rtx_ASHIFTRT (mode,
6504                                      make_compound_operation
6505                                      (XEXP (XEXP (x, 0), 0), next_code),
6506                                      XEXP (XEXP (x, 0), 1)));
6507         }
6508
6509       /* If the constant is one less than a power of two, this might be
6510          representable by an extraction even if no shift is present.
6511          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6512          we are in a COMPARE.  */
6513       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6514         new = make_extraction (mode,
6515                                make_compound_operation (XEXP (x, 0),
6516                                                         next_code),
6517                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6518
6519       /* If we are in a comparison and this is an AND with a power of two,
6520          convert this into the appropriate bit extract.  */
6521       else if (in_code == COMPARE
6522                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6523         new = make_extraction (mode,
6524                                make_compound_operation (XEXP (x, 0),
6525                                                         next_code),
6526                                i, NULL_RTX, 1, 1, 0, 1);
6527
6528       break;
6529
6530     case LSHIFTRT:
6531       /* If the sign bit is known to be zero, replace this with an
6532          arithmetic shift.  */
6533       if (have_insn_for (ASHIFTRT, mode)
6534           && ! have_insn_for (LSHIFTRT, mode)
6535           && mode_width <= HOST_BITS_PER_WIDE_INT
6536           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6537         {
6538           new = gen_rtx_ASHIFTRT (mode,
6539                                   make_compound_operation (XEXP (x, 0),
6540                                                            next_code),
6541                                   XEXP (x, 1));
6542           break;
6543         }
6544
6545       /* ... fall through ...  */
6546
6547     case ASHIFTRT:
6548       lhs = XEXP (x, 0);
6549       rhs = XEXP (x, 1);
6550
6551       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6552          this is a SIGN_EXTRACT.  */
6553       if (GET_CODE (rhs) == CONST_INT
6554           && GET_CODE (lhs) == ASHIFT
6555           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6556           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6557         {
6558           new = make_compound_operation (XEXP (lhs, 0), next_code);
6559           new = make_extraction (mode, new,
6560                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6561                                  NULL_RTX, mode_width - INTVAL (rhs),
6562                                  code == LSHIFTRT, 0, in_code == COMPARE);
6563           break;
6564         }
6565
6566       /* See if we have operations between an ASHIFTRT and an ASHIFT.
6567          If so, try to merge the shifts into a SIGN_EXTEND.  We could
6568          also do this for some cases of SIGN_EXTRACT, but it doesn't
6569          seem worth the effort; the case checked for occurs on Alpha.  */
6570
6571       if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6572           && ! (GET_CODE (lhs) == SUBREG
6573                 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6574           && GET_CODE (rhs) == CONST_INT
6575           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6576           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6577         new = make_extraction (mode, make_compound_operation (new, next_code),
6578                                0, NULL_RTX, mode_width - INTVAL (rhs),
6579                                code == LSHIFTRT, 0, in_code == COMPARE);
6580
6581       break;
6582
6583     case SUBREG:
6584       /* Call ourselves recursively on the inner expression.  If we are
6585          narrowing the object and it has a different RTL code from
6586          what it originally did, do this SUBREG as a force_to_mode.  */
6587
6588       tem = make_compound_operation (SUBREG_REG (x), in_code);
6589       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6590           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6591           && subreg_lowpart_p (x))
6592         {
6593           rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6594                                      NULL_RTX, 0);
6595
6596           /* If we have something other than a SUBREG, we might have
6597              done an expansion, so rerun ourselves.  */
6598           if (GET_CODE (newer) != SUBREG)
6599             newer = make_compound_operation (newer, in_code);
6600
6601           return newer;
6602         }
6603
6604       /* If this is a paradoxical subreg, and the new code is a sign or
6605          zero extension, omit the subreg and widen the extension.  If it
6606          is a regular subreg, we can still get rid of the subreg by not
6607          widening so much, or in fact removing the extension entirely.  */
6608       if ((GET_CODE (tem) == SIGN_EXTEND
6609            || GET_CODE (tem) == ZERO_EXTEND)
6610           && subreg_lowpart_p (x))
6611         {
6612           if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6613               || (GET_MODE_SIZE (mode) >
6614                   GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6615             {
6616               if (! SCALAR_INT_MODE_P (mode))
6617                 break;
6618               tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6619             }
6620           else
6621             tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6622           return tem;
6623         }
6624       break;
6625
6626     default:
6627       break;
6628     }
6629
6630   if (new)
6631     {
6632       x = gen_lowpart_for_combine (mode, new);
6633       code = GET_CODE (x);
6634     }
6635
6636   /* Now recursively process each operand of this operation.  */
6637   fmt = GET_RTX_FORMAT (code);
6638   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6639     if (fmt[i] == 'e')
6640       {
6641         new = make_compound_operation (XEXP (x, i), next_code);
6642         SUBST (XEXP (x, i), new);
6643       }
6644
6645   return x;
6646 }
6647 \f
6648 /* Given M see if it is a value that would select a field of bits
6649    within an item, but not the entire word.  Return -1 if not.
6650    Otherwise, return the starting position of the field, where 0 is the
6651    low-order bit.
6652
6653    *PLEN is set to the length of the field.  */
6654
6655 static int
6656 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6657 {
6658   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
6659   int pos = exact_log2 (m & -m);
6660   int len;
6661
6662   if (pos < 0)
6663     return -1;
6664
6665   /* Now shift off the low-order zero bits and see if we have a power of
6666      two minus 1.  */
6667   len = exact_log2 ((m >> pos) + 1);
6668
6669   if (len <= 0)
6670     return -1;
6671
6672   *plen = len;
6673   return pos;
6674 }
6675 \f
6676 /* See if X can be simplified knowing that we will only refer to it in
6677    MODE and will only refer to those bits that are nonzero in MASK.
6678    If other bits are being computed or if masking operations are done
6679    that select a superset of the bits in MASK, they can sometimes be
6680    ignored.
6681
6682    Return a possibly simplified expression, but always convert X to
6683    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
6684
6685    Also, if REG is nonzero and X is a register equal in value to REG,
6686    replace X with REG.
6687
6688    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6689    are all off in X.  This is used when X will be complemented, by either
6690    NOT, NEG, or XOR.  */
6691
6692 static rtx
6693 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6694                rtx reg, int just_select)
6695 {
6696   enum rtx_code code = GET_CODE (x);
6697   int next_select = just_select || code == XOR || code == NOT || code == NEG;
6698   enum machine_mode op_mode;
6699   unsigned HOST_WIDE_INT fuller_mask, nonzero;
6700   rtx op0, op1, temp;
6701
6702   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
6703      code below will do the wrong thing since the mode of such an
6704      expression is VOIDmode.
6705
6706      Also do nothing if X is a CLOBBER; this can happen if X was
6707      the return value from a call to gen_lowpart_for_combine.  */
6708   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6709     return x;
6710
6711   /* We want to perform the operation is its present mode unless we know
6712      that the operation is valid in MODE, in which case we do the operation
6713      in MODE.  */
6714   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6715               && have_insn_for (code, mode))
6716              ? mode : GET_MODE (x));
6717
6718   /* It is not valid to do a right-shift in a narrower mode
6719      than the one it came in with.  */
6720   if ((code == LSHIFTRT || code == ASHIFTRT)
6721       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6722     op_mode = GET_MODE (x);
6723
6724   /* Truncate MASK to fit OP_MODE.  */
6725   if (op_mode)
6726     mask &= GET_MODE_MASK (op_mode);
6727
6728   /* When we have an arithmetic operation, or a shift whose count we
6729      do not know, we need to assume that all bits up to the highest-order
6730      bit in MASK will be needed.  This is how we form such a mask.  */
6731   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6732     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6733   else
6734     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6735                    - 1);
6736
6737   /* Determine what bits of X are guaranteed to be (non)zero.  */
6738   nonzero = nonzero_bits (x, mode);
6739
6740   /* If none of the bits in X are needed, return a zero.  */
6741   if (! just_select && (nonzero & mask) == 0)
6742     x = const0_rtx;
6743
6744   /* If X is a CONST_INT, return a new one.  Do this here since the
6745      test below will fail.  */
6746   if (GET_CODE (x) == CONST_INT)
6747     {
6748       if (SCALAR_INT_MODE_P (mode))
6749         return gen_int_mode (INTVAL (x) & mask, mode);
6750       else
6751         {
6752           x = GEN_INT (INTVAL (x) & mask);
6753           return gen_lowpart_common (mode, x);
6754         }
6755     }
6756
6757   /* If X is narrower than MODE and we want all the bits in X's mode, just
6758      get X in the proper mode.  */
6759   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6760       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6761     return gen_lowpart_for_combine (mode, x);
6762
6763   /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6764      MASK are already known to be zero in X, we need not do anything.  */
6765   if (GET_MODE (x) == mode && code != SUBREG && (~mask & nonzero) == 0)
6766     return x;
6767
6768   switch (code)
6769     {
6770     case CLOBBER:
6771       /* If X is a (clobber (const_int)), return it since we know we are
6772          generating something that won't match.  */
6773       return x;
6774
6775     case USE:
6776       /* X is a (use (mem ..)) that was made from a bit-field extraction that
6777          spanned the boundary of the MEM.  If we are now masking so it is
6778          within that boundary, we don't need the USE any more.  */
6779       if (! BITS_BIG_ENDIAN
6780           && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6781         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6782       break;
6783
6784     case SIGN_EXTEND:
6785     case ZERO_EXTEND:
6786     case ZERO_EXTRACT:
6787     case SIGN_EXTRACT:
6788       x = expand_compound_operation (x);
6789       if (GET_CODE (x) != code)
6790         return force_to_mode (x, mode, mask, reg, next_select);
6791       break;
6792
6793     case REG:
6794       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6795                        || rtx_equal_p (reg, get_last_value (x))))
6796         x = reg;
6797       break;
6798
6799     case SUBREG:
6800       if (subreg_lowpart_p (x)
6801           /* We can ignore the effect of this SUBREG if it narrows the mode or
6802              if the constant masks to zero all the bits the mode doesn't
6803              have.  */
6804           && ((GET_MODE_SIZE (GET_MODE (x))
6805                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6806               || (0 == (mask
6807                         & GET_MODE_MASK (GET_MODE (x))
6808                         & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6809         return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6810       break;
6811
6812     case AND:
6813       /* If this is an AND with a constant, convert it into an AND
6814          whose constant is the AND of that constant with MASK.  If it
6815          remains an AND of MASK, delete it since it is redundant.  */
6816
6817       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6818         {
6819           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6820                                       mask & INTVAL (XEXP (x, 1)));
6821
6822           /* If X is still an AND, see if it is an AND with a mask that
6823              is just some low-order bits.  If so, and it is MASK, we don't
6824              need it.  */
6825
6826           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6827               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6828                   == mask))
6829             x = XEXP (x, 0);
6830
6831           /* If it remains an AND, try making another AND with the bits
6832              in the mode mask that aren't in MASK turned on.  If the
6833              constant in the AND is wide enough, this might make a
6834              cheaper constant.  */
6835
6836           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6837               && GET_MODE_MASK (GET_MODE (x)) != mask
6838               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6839             {
6840               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6841                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6842               int width = GET_MODE_BITSIZE (GET_MODE (x));
6843               rtx y;
6844
6845               /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6846                  number, sign extend it.  */
6847               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6848                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6849                 cval |= (HOST_WIDE_INT) -1 << width;
6850
6851               y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6852               if (rtx_cost (y, SET) < rtx_cost (x, SET))
6853                 x = y;
6854             }
6855
6856           break;
6857         }
6858
6859       goto binop;
6860
6861     case PLUS:
6862       /* In (and (plus FOO C1) M), if M is a mask that just turns off
6863          low-order bits (as in an alignment operation) and FOO is already
6864          aligned to that boundary, mask C1 to that boundary as well.
6865          This may eliminate that PLUS and, later, the AND.  */
6866
6867       {
6868         unsigned int width = GET_MODE_BITSIZE (mode);
6869         unsigned HOST_WIDE_INT smask = mask;
6870
6871         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6872            number, sign extend it.  */
6873
6874         if (width < HOST_BITS_PER_WIDE_INT
6875             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6876           smask |= (HOST_WIDE_INT) -1 << width;
6877
6878         if (GET_CODE (XEXP (x, 1)) == CONST_INT
6879             && exact_log2 (- smask) >= 0
6880             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
6881             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
6882           return force_to_mode (plus_constant (XEXP (x, 0),
6883                                                (INTVAL (XEXP (x, 1)) & smask)),
6884                                 mode, smask, reg, next_select);
6885       }
6886
6887       /* ... fall through ...  */
6888
6889     case MULT:
6890       /* For PLUS, MINUS and MULT, we need any bits less significant than the
6891          most significant bit in MASK since carries from those bits will
6892          affect the bits we are interested in.  */
6893       mask = fuller_mask;
6894       goto binop;
6895
6896     case MINUS:
6897       /* If X is (minus C Y) where C's least set bit is larger than any bit
6898          in the mask, then we may replace with (neg Y).  */
6899       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6900           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
6901                                         & -INTVAL (XEXP (x, 0))))
6902               > mask))
6903         {
6904           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
6905                                   GET_MODE (x));
6906           return force_to_mode (x, mode, mask, reg, next_select);
6907         }
6908
6909       /* Similarly, if C contains every bit in the fuller_mask, then we may
6910          replace with (not Y).  */
6911       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6912           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
6913               == INTVAL (XEXP (x, 0))))
6914         {
6915           x = simplify_gen_unary (NOT, GET_MODE (x),
6916                                   XEXP (x, 1), GET_MODE (x));
6917           return force_to_mode (x, mode, mask, reg, next_select);
6918         }
6919
6920       mask = fuller_mask;
6921       goto binop;
6922
6923     case IOR:
6924     case XOR:
6925       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6926          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6927          operation which may be a bitfield extraction.  Ensure that the
6928          constant we form is not wider than the mode of X.  */
6929
6930       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6931           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6932           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6933           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6934           && GET_CODE (XEXP (x, 1)) == CONST_INT
6935           && ((INTVAL (XEXP (XEXP (x, 0), 1))
6936                + floor_log2 (INTVAL (XEXP (x, 1))))
6937               < GET_MODE_BITSIZE (GET_MODE (x)))
6938           && (INTVAL (XEXP (x, 1))
6939               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6940         {
6941           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6942                           << INTVAL (XEXP (XEXP (x, 0), 1)));
6943           temp = gen_binary (GET_CODE (x), GET_MODE (x),
6944                              XEXP (XEXP (x, 0), 0), temp);
6945           x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6946                           XEXP (XEXP (x, 0), 1));
6947           return force_to_mode (x, mode, mask, reg, next_select);
6948         }
6949
6950     binop:
6951       /* For most binary operations, just propagate into the operation and
6952          change the mode if we have an operation of that mode.  */
6953
6954       op0 = gen_lowpart_for_combine (op_mode,
6955                                      force_to_mode (XEXP (x, 0), mode, mask,
6956                                                     reg, next_select));
6957       op1 = gen_lowpart_for_combine (op_mode,
6958                                      force_to_mode (XEXP (x, 1), mode, mask,
6959                                                     reg, next_select));
6960
6961       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6962         x = gen_binary (code, op_mode, op0, op1);
6963       break;
6964
6965     case ASHIFT:
6966       /* For left shifts, do the same, but just for the first operand.
6967          However, we cannot do anything with shifts where we cannot
6968          guarantee that the counts are smaller than the size of the mode
6969          because such a count will have a different meaning in a
6970          wider mode.  */
6971
6972       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6973              && INTVAL (XEXP (x, 1)) >= 0
6974              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6975           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6976                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6977                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6978         break;
6979
6980       /* If the shift count is a constant and we can do arithmetic in
6981          the mode of the shift, refine which bits we need.  Otherwise, use the
6982          conservative form of the mask.  */
6983       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6984           && INTVAL (XEXP (x, 1)) >= 0
6985           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6986           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6987         mask >>= INTVAL (XEXP (x, 1));
6988       else
6989         mask = fuller_mask;
6990
6991       op0 = gen_lowpart_for_combine (op_mode,
6992                                      force_to_mode (XEXP (x, 0), op_mode,
6993                                                     mask, reg, next_select));
6994
6995       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6996         x = gen_binary (code, op_mode, op0, XEXP (x, 1));
6997       break;
6998
6999     case LSHIFTRT:
7000       /* Here we can only do something if the shift count is a constant,
7001          this shift constant is valid for the host, and we can do arithmetic
7002          in OP_MODE.  */
7003
7004       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7005           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7006           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7007         {
7008           rtx inner = XEXP (x, 0);
7009           unsigned HOST_WIDE_INT inner_mask;
7010
7011           /* Select the mask of the bits we need for the shift operand.  */
7012           inner_mask = mask << INTVAL (XEXP (x, 1));
7013
7014           /* We can only change the mode of the shift if we can do arithmetic
7015              in the mode of the shift and INNER_MASK is no wider than the
7016              width of OP_MODE.  */
7017           if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
7018               || (inner_mask & ~GET_MODE_MASK (op_mode)) != 0)
7019             op_mode = GET_MODE (x);
7020
7021           inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7022
7023           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7024             x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7025         }
7026
7027       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7028          shift and AND produces only copies of the sign bit (C2 is one less
7029          than a power of two), we can do this with just a shift.  */
7030
7031       if (GET_CODE (x) == LSHIFTRT
7032           && GET_CODE (XEXP (x, 1)) == CONST_INT
7033           /* The shift puts one of the sign bit copies in the least significant
7034              bit.  */
7035           && ((INTVAL (XEXP (x, 1))
7036                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7037               >= GET_MODE_BITSIZE (GET_MODE (x)))
7038           && exact_log2 (mask + 1) >= 0
7039           /* Number of bits left after the shift must be more than the mask
7040              needs.  */
7041           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7042               <= GET_MODE_BITSIZE (GET_MODE (x)))
7043           /* Must be more sign bit copies than the mask needs.  */
7044           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7045               >= exact_log2 (mask + 1)))
7046         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7047                         GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7048                                  - exact_log2 (mask + 1)));
7049
7050       goto shiftrt;
7051
7052     case ASHIFTRT:
7053       /* If we are just looking for the sign bit, we don't need this shift at
7054          all, even if it has a variable count.  */
7055       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7056           && (mask == ((unsigned HOST_WIDE_INT) 1
7057                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7058         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7059
7060       /* If this is a shift by a constant, get a mask that contains those bits
7061          that are not copies of the sign bit.  We then have two cases:  If
7062          MASK only includes those bits, this can be a logical shift, which may
7063          allow simplifications.  If MASK is a single-bit field not within
7064          those bits, we are requesting a copy of the sign bit and hence can
7065          shift the sign bit to the appropriate location.  */
7066
7067       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7068           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7069         {
7070           int i = -1;
7071
7072           /* If the considered data is wider than HOST_WIDE_INT, we can't
7073              represent a mask for all its bits in a single scalar.
7074              But we only care about the lower bits, so calculate these.  */
7075
7076           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7077             {
7078               nonzero = ~(HOST_WIDE_INT) 0;
7079
7080               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7081                  is the number of bits a full-width mask would have set.
7082                  We need only shift if these are fewer than nonzero can
7083                  hold.  If not, we must keep all bits set in nonzero.  */
7084
7085               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7086                   < HOST_BITS_PER_WIDE_INT)
7087                 nonzero >>= INTVAL (XEXP (x, 1))
7088                             + HOST_BITS_PER_WIDE_INT
7089                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7090             }
7091           else
7092             {
7093               nonzero = GET_MODE_MASK (GET_MODE (x));
7094               nonzero >>= INTVAL (XEXP (x, 1));
7095             }
7096
7097           if ((mask & ~nonzero) == 0
7098               || (i = exact_log2 (mask)) >= 0)
7099             {
7100               x = simplify_shift_const
7101                 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7102                  i < 0 ? INTVAL (XEXP (x, 1))
7103                  : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7104
7105               if (GET_CODE (x) != ASHIFTRT)
7106                 return force_to_mode (x, mode, mask, reg, next_select);
7107             }
7108         }
7109
7110       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7111          even if the shift count isn't a constant.  */
7112       if (mask == 1)
7113         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
7114
7115     shiftrt:
7116
7117       /* If this is a zero- or sign-extension operation that just affects bits
7118          we don't care about, remove it.  Be sure the call above returned
7119          something that is still a shift.  */
7120
7121       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7122           && GET_CODE (XEXP (x, 1)) == CONST_INT
7123           && INTVAL (XEXP (x, 1)) >= 0
7124           && (INTVAL (XEXP (x, 1))
7125               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7126           && GET_CODE (XEXP (x, 0)) == ASHIFT
7127           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7128         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7129                               reg, next_select);
7130
7131       break;
7132
7133     case ROTATE:
7134     case ROTATERT:
7135       /* If the shift count is constant and we can do computations
7136          in the mode of X, compute where the bits we care about are.
7137          Otherwise, we can't do anything.  Don't change the mode of
7138          the shift or propagate MODE into the shift, though.  */
7139       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7140           && INTVAL (XEXP (x, 1)) >= 0)
7141         {
7142           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7143                                             GET_MODE (x), GEN_INT (mask),
7144                                             XEXP (x, 1));
7145           if (temp && GET_CODE (temp) == CONST_INT)
7146             SUBST (XEXP (x, 0),
7147                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7148                                   INTVAL (temp), reg, next_select));
7149         }
7150       break;
7151
7152     case NEG:
7153       /* If we just want the low-order bit, the NEG isn't needed since it
7154          won't change the low-order bit.  */
7155       if (mask == 1)
7156         return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7157
7158       /* We need any bits less significant than the most significant bit in
7159          MASK since carries from those bits will affect the bits we are
7160          interested in.  */
7161       mask = fuller_mask;
7162       goto unop;
7163
7164     case NOT:
7165       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7166          same as the XOR case above.  Ensure that the constant we form is not
7167          wider than the mode of X.  */
7168
7169       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7170           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7171           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7172           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7173               < GET_MODE_BITSIZE (GET_MODE (x)))
7174           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7175         {
7176           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7177                                GET_MODE (x));
7178           temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
7179           x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
7180
7181           return force_to_mode (x, mode, mask, reg, next_select);
7182         }
7183
7184       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7185          use the full mask inside the NOT.  */
7186       mask = fuller_mask;
7187
7188     unop:
7189       op0 = gen_lowpart_for_combine (op_mode,
7190                                      force_to_mode (XEXP (x, 0), mode, mask,
7191                                                     reg, next_select));
7192       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7193         x = simplify_gen_unary (code, op_mode, op0, op_mode);
7194       break;
7195
7196     case NE:
7197       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7198          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7199          which is equal to STORE_FLAG_VALUE.  */
7200       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7201           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7202           && (nonzero_bits (XEXP (x, 0), mode)
7203               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7204         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7205
7206       break;
7207
7208     case IF_THEN_ELSE:
7209       /* We have no way of knowing if the IF_THEN_ELSE can itself be
7210          written in a narrower mode.  We play it safe and do not do so.  */
7211
7212       SUBST (XEXP (x, 1),
7213              gen_lowpart_for_combine (GET_MODE (x),
7214                                       force_to_mode (XEXP (x, 1), mode,
7215                                                      mask, reg, next_select)));
7216       SUBST (XEXP (x, 2),
7217              gen_lowpart_for_combine (GET_MODE (x),
7218                                       force_to_mode (XEXP (x, 2), mode,
7219                                                      mask, reg, next_select)));
7220       break;
7221
7222     default:
7223       break;
7224     }
7225
7226   /* Ensure we return a value of the proper mode.  */
7227   return gen_lowpart_for_combine (mode, x);
7228 }
7229 \f
7230 /* Return nonzero if X is an expression that has one of two values depending on
7231    whether some other value is zero or nonzero.  In that case, we return the
7232    value that is being tested, *PTRUE is set to the value if the rtx being
7233    returned has a nonzero value, and *PFALSE is set to the other alternative.
7234
7235    If we return zero, we set *PTRUE and *PFALSE to X.  */
7236
7237 static rtx
7238 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7239 {
7240   enum machine_mode mode = GET_MODE (x);
7241   enum rtx_code code = GET_CODE (x);
7242   rtx cond0, cond1, true0, true1, false0, false1;
7243   unsigned HOST_WIDE_INT nz;
7244
7245   /* If we are comparing a value against zero, we are done.  */
7246   if ((code == NE || code == EQ)
7247       && XEXP (x, 1) == const0_rtx)
7248     {
7249       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7250       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7251       return XEXP (x, 0);
7252     }
7253
7254   /* If this is a unary operation whose operand has one of two values, apply
7255      our opcode to compute those values.  */
7256   else if (GET_RTX_CLASS (code) == '1'
7257            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7258     {
7259       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7260       *pfalse = simplify_gen_unary (code, mode, false0,
7261                                     GET_MODE (XEXP (x, 0)));
7262       return cond0;
7263     }
7264
7265   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7266      make can't possibly match and would suppress other optimizations.  */
7267   else if (code == COMPARE)
7268     ;
7269
7270   /* If this is a binary operation, see if either side has only one of two
7271      values.  If either one does or if both do and they are conditional on
7272      the same value, compute the new true and false values.  */
7273   else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
7274            || GET_RTX_CLASS (code) == '<')
7275     {
7276       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7277       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7278
7279       if ((cond0 != 0 || cond1 != 0)
7280           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7281         {
7282           /* If if_then_else_cond returned zero, then true/false are the
7283              same rtl.  We must copy one of them to prevent invalid rtl
7284              sharing.  */
7285           if (cond0 == 0)
7286             true0 = copy_rtx (true0);
7287           else if (cond1 == 0)
7288             true1 = copy_rtx (true1);
7289
7290           *ptrue = gen_binary (code, mode, true0, true1);
7291           *pfalse = gen_binary (code, mode, false0, false1);
7292           return cond0 ? cond0 : cond1;
7293         }
7294
7295       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7296          operands is zero when the other is nonzero, and vice-versa,
7297          and STORE_FLAG_VALUE is 1 or -1.  */
7298
7299       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7300           && (code == PLUS || code == IOR || code == XOR || code == MINUS
7301               || code == UMAX)
7302           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7303         {
7304           rtx op0 = XEXP (XEXP (x, 0), 1);
7305           rtx op1 = XEXP (XEXP (x, 1), 1);
7306
7307           cond0 = XEXP (XEXP (x, 0), 0);
7308           cond1 = XEXP (XEXP (x, 1), 0);
7309
7310           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7311               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7312               && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7313                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7314                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7315                   || ((swap_condition (GET_CODE (cond0))
7316                        == combine_reversed_comparison_code (cond1))
7317                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7318                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7319               && ! side_effects_p (x))
7320             {
7321               *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
7322               *pfalse = gen_binary (MULT, mode,
7323                                     (code == MINUS
7324                                      ? simplify_gen_unary (NEG, mode, op1,
7325                                                            mode)
7326                                      : op1),
7327                                     const_true_rtx);
7328               return cond0;
7329             }
7330         }
7331
7332       /* Similarly for MULT, AND and UMIN, except that for these the result
7333          is always zero.  */
7334       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7335           && (code == MULT || code == AND || code == UMIN)
7336           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7337         {
7338           cond0 = XEXP (XEXP (x, 0), 0);
7339           cond1 = XEXP (XEXP (x, 1), 0);
7340
7341           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7342               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7343               && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7344                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7345                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7346                   || ((swap_condition (GET_CODE (cond0))
7347                        == combine_reversed_comparison_code (cond1))
7348                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7349                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7350               && ! side_effects_p (x))
7351             {
7352               *ptrue = *pfalse = const0_rtx;
7353               return cond0;
7354             }
7355         }
7356     }
7357
7358   else if (code == IF_THEN_ELSE)
7359     {
7360       /* If we have IF_THEN_ELSE already, extract the condition and
7361          canonicalize it if it is NE or EQ.  */
7362       cond0 = XEXP (x, 0);
7363       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7364       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7365         return XEXP (cond0, 0);
7366       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7367         {
7368           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7369           return XEXP (cond0, 0);
7370         }
7371       else
7372         return cond0;
7373     }
7374
7375   /* If X is a SUBREG, we can narrow both the true and false values
7376      if the inner expression, if there is a condition.  */
7377   else if (code == SUBREG
7378            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7379                                                &true0, &false0)))
7380     {
7381       *ptrue = simplify_gen_subreg (mode, true0,
7382                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7383       *pfalse = simplify_gen_subreg (mode, false0,
7384                                      GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7385
7386       return cond0;
7387     }
7388
7389   /* If X is a constant, this isn't special and will cause confusions
7390      if we treat it as such.  Likewise if it is equivalent to a constant.  */
7391   else if (CONSTANT_P (x)
7392            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7393     ;
7394
7395   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7396      will be least confusing to the rest of the compiler.  */
7397   else if (mode == BImode)
7398     {
7399       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7400       return x;
7401     }
7402
7403   /* If X is known to be either 0 or -1, those are the true and
7404      false values when testing X.  */
7405   else if (x == constm1_rtx || x == const0_rtx
7406            || (mode != VOIDmode
7407                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7408     {
7409       *ptrue = constm1_rtx, *pfalse = const0_rtx;
7410       return x;
7411     }
7412
7413   /* Likewise for 0 or a single bit.  */
7414   else if (SCALAR_INT_MODE_P (mode)
7415            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7416            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7417     {
7418       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7419       return x;
7420     }
7421
7422   /* Otherwise fail; show no condition with true and false values the same.  */
7423   *ptrue = *pfalse = x;
7424   return 0;
7425 }
7426 \f
7427 /* Return the value of expression X given the fact that condition COND
7428    is known to be true when applied to REG as its first operand and VAL
7429    as its second.  X is known to not be shared and so can be modified in
7430    place.
7431
7432    We only handle the simplest cases, and specifically those cases that
7433    arise with IF_THEN_ELSE expressions.  */
7434
7435 static rtx
7436 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7437 {
7438   enum rtx_code code = GET_CODE (x);
7439   rtx temp;
7440   const char *fmt;
7441   int i, j;
7442
7443   if (side_effects_p (x))
7444     return x;
7445
7446   /* If either operand of the condition is a floating point value,
7447      then we have to avoid collapsing an EQ comparison.  */
7448   if (cond == EQ
7449       && rtx_equal_p (x, reg)
7450       && ! FLOAT_MODE_P (GET_MODE (x))
7451       && ! FLOAT_MODE_P (GET_MODE (val)))
7452     return val;
7453
7454   if (cond == UNEQ && rtx_equal_p (x, reg))
7455     return val;
7456
7457   /* If X is (abs REG) and we know something about REG's relationship
7458      with zero, we may be able to simplify this.  */
7459
7460   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7461     switch (cond)
7462       {
7463       case GE:  case GT:  case EQ:
7464         return XEXP (x, 0);
7465       case LT:  case LE:
7466         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7467                                    XEXP (x, 0),
7468                                    GET_MODE (XEXP (x, 0)));
7469       default:
7470         break;
7471       }
7472
7473   /* The only other cases we handle are MIN, MAX, and comparisons if the
7474      operands are the same as REG and VAL.  */
7475
7476   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7477     {
7478       if (rtx_equal_p (XEXP (x, 0), val))
7479         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7480
7481       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7482         {
7483           if (GET_RTX_CLASS (code) == '<')
7484             {
7485               if (comparison_dominates_p (cond, code))
7486                 return const_true_rtx;
7487
7488               code = combine_reversed_comparison_code (x);
7489               if (code != UNKNOWN
7490                   && comparison_dominates_p (cond, code))
7491                 return const0_rtx;
7492               else
7493                 return x;
7494             }
7495           else if (code == SMAX || code == SMIN
7496                    || code == UMIN || code == UMAX)
7497             {
7498               int unsignedp = (code == UMIN || code == UMAX);
7499
7500               /* Do not reverse the condition when it is NE or EQ.
7501                  This is because we cannot conclude anything about
7502                  the value of 'SMAX (x, y)' when x is not equal to y,
7503                  but we can when x equals y.  */
7504               if ((code == SMAX || code == UMAX)
7505                   && ! (cond == EQ || cond == NE))
7506                 cond = reverse_condition (cond);
7507
7508               switch (cond)
7509                 {
7510                 case GE:   case GT:
7511                   return unsignedp ? x : XEXP (x, 1);
7512                 case LE:   case LT:
7513                   return unsignedp ? x : XEXP (x, 0);
7514                 case GEU:  case GTU:
7515                   return unsignedp ? XEXP (x, 1) : x;
7516                 case LEU:  case LTU:
7517                   return unsignedp ? XEXP (x, 0) : x;
7518                 default:
7519                   break;
7520                 }
7521             }
7522         }
7523     }
7524   else if (code == SUBREG)
7525     {
7526       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7527       rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7528
7529       if (SUBREG_REG (x) != r)
7530         {
7531           /* We must simplify subreg here, before we lose track of the
7532              original inner_mode.  */
7533           new = simplify_subreg (GET_MODE (x), r,
7534                                  inner_mode, SUBREG_BYTE (x));
7535           if (new)
7536             return new;
7537           else
7538             SUBST (SUBREG_REG (x), r);
7539         }
7540
7541       return x;
7542     }
7543   /* We don't have to handle SIGN_EXTEND here, because even in the
7544      case of replacing something with a modeless CONST_INT, a
7545      CONST_INT is already (supposed to be) a valid sign extension for
7546      its narrower mode, which implies it's already properly
7547      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
7548      story is different.  */
7549   else if (code == ZERO_EXTEND)
7550     {
7551       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7552       rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7553
7554       if (XEXP (x, 0) != r)
7555         {
7556           /* We must simplify the zero_extend here, before we lose
7557              track of the original inner_mode.  */
7558           new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7559                                           r, inner_mode);
7560           if (new)
7561             return new;
7562           else
7563             SUBST (XEXP (x, 0), r);
7564         }
7565
7566       return x;
7567     }
7568
7569   fmt = GET_RTX_FORMAT (code);
7570   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7571     {
7572       if (fmt[i] == 'e')
7573         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7574       else if (fmt[i] == 'E')
7575         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7576           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7577                                                 cond, reg, val));
7578     }
7579
7580   return x;
7581 }
7582 \f
7583 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7584    assignment as a field assignment.  */
7585
7586 static int
7587 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7588 {
7589   if (x == y || rtx_equal_p (x, y))
7590     return 1;
7591
7592   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7593     return 0;
7594
7595   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7596      Note that all SUBREGs of MEM are paradoxical; otherwise they
7597      would have been rewritten.  */
7598   if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7599       && GET_CODE (SUBREG_REG (y)) == MEM
7600       && rtx_equal_p (SUBREG_REG (y),
7601                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7602     return 1;
7603
7604   if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7605       && GET_CODE (SUBREG_REG (x)) == MEM
7606       && rtx_equal_p (SUBREG_REG (x),
7607                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7608     return 1;
7609
7610   /* We used to see if get_last_value of X and Y were the same but that's
7611      not correct.  In one direction, we'll cause the assignment to have
7612      the wrong destination and in the case, we'll import a register into this
7613      insn that might have already have been dead.   So fail if none of the
7614      above cases are true.  */
7615   return 0;
7616 }
7617 \f
7618 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7619    Return that assignment if so.
7620
7621    We only handle the most common cases.  */
7622
7623 static rtx
7624 make_field_assignment (rtx x)
7625 {
7626   rtx dest = SET_DEST (x);
7627   rtx src = SET_SRC (x);
7628   rtx assign;
7629   rtx rhs, lhs;
7630   HOST_WIDE_INT c1;
7631   HOST_WIDE_INT pos;
7632   unsigned HOST_WIDE_INT len;
7633   rtx other;
7634   enum machine_mode mode;
7635
7636   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7637      a clear of a one-bit field.  We will have changed it to
7638      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
7639      for a SUBREG.  */
7640
7641   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7642       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7643       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7644       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7645     {
7646       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7647                                 1, 1, 1, 0);
7648       if (assign != 0)
7649         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7650       return x;
7651     }
7652
7653   else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7654            && subreg_lowpart_p (XEXP (src, 0))
7655            && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7656                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7657            && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7658            && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7659            && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7660            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7661     {
7662       assign = make_extraction (VOIDmode, dest, 0,
7663                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7664                                 1, 1, 1, 0);
7665       if (assign != 0)
7666         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7667       return x;
7668     }
7669
7670   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7671      one-bit field.  */
7672   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7673            && XEXP (XEXP (src, 0), 0) == const1_rtx
7674            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7675     {
7676       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7677                                 1, 1, 1, 0);
7678       if (assign != 0)
7679         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7680       return x;
7681     }
7682
7683   /* The other case we handle is assignments into a constant-position
7684      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
7685      a mask that has all one bits except for a group of zero bits and
7686      OTHER is known to have zeros where C1 has ones, this is such an
7687      assignment.  Compute the position and length from C1.  Shift OTHER
7688      to the appropriate position, force it to the required mode, and
7689      make the extraction.  Check for the AND in both operands.  */
7690
7691   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7692     return x;
7693
7694   rhs = expand_compound_operation (XEXP (src, 0));
7695   lhs = expand_compound_operation (XEXP (src, 1));
7696
7697   if (GET_CODE (rhs) == AND
7698       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7699       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7700     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7701   else if (GET_CODE (lhs) == AND
7702            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7703            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7704     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7705   else
7706     return x;
7707
7708   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7709   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7710       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7711       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7712     return x;
7713
7714   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7715   if (assign == 0)
7716     return x;
7717
7718   /* The mode to use for the source is the mode of the assignment, or of
7719      what is inside a possible STRICT_LOW_PART.  */
7720   mode = (GET_CODE (assign) == STRICT_LOW_PART
7721           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7722
7723   /* Shift OTHER right POS places and make it the source, restricting it
7724      to the proper length and mode.  */
7725
7726   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7727                                              GET_MODE (src), other, pos),
7728                        mode,
7729                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7730                        ? ~(unsigned HOST_WIDE_INT) 0
7731                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7732                        dest, 0);
7733
7734   /* If SRC is masked by an AND that does not make a difference in
7735      the value being stored, strip it.  */
7736   if (GET_CODE (assign) == ZERO_EXTRACT
7737       && GET_CODE (XEXP (assign, 1)) == CONST_INT
7738       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7739       && GET_CODE (src) == AND
7740       && GET_CODE (XEXP (src, 1)) == CONST_INT
7741       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7742           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7743     src = XEXP (src, 0);
7744
7745   return gen_rtx_SET (VOIDmode, assign, src);
7746 }
7747 \f
7748 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7749    if so.  */
7750
7751 static rtx
7752 apply_distributive_law (rtx x)
7753 {
7754   enum rtx_code code = GET_CODE (x);
7755   enum rtx_code inner_code;
7756   rtx lhs, rhs, other;
7757   rtx tem;
7758
7759   /* Distributivity is not true for floating point as it can change the
7760      value.  So we don't do it unless -funsafe-math-optimizations.  */
7761   if (FLOAT_MODE_P (GET_MODE (x))
7762       && ! flag_unsafe_math_optimizations)
7763     return x;
7764
7765   /* The outer operation can only be one of the following:  */
7766   if (code != IOR && code != AND && code != XOR
7767       && code != PLUS && code != MINUS)
7768     return x;
7769
7770   lhs = XEXP (x, 0);
7771   rhs = XEXP (x, 1);
7772
7773   /* If either operand is a primitive we can't do anything, so get out
7774      fast.  */
7775   if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7776       || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7777     return x;
7778
7779   lhs = expand_compound_operation (lhs);
7780   rhs = expand_compound_operation (rhs);
7781   inner_code = GET_CODE (lhs);
7782   if (inner_code != GET_CODE (rhs))
7783     return x;
7784
7785   /* See if the inner and outer operations distribute.  */
7786   switch (inner_code)
7787     {
7788     case LSHIFTRT:
7789     case ASHIFTRT:
7790     case AND:
7791     case IOR:
7792       /* These all distribute except over PLUS.  */
7793       if (code == PLUS || code == MINUS)
7794         return x;
7795       break;
7796
7797     case MULT:
7798       if (code != PLUS && code != MINUS)
7799         return x;
7800       break;
7801
7802     case ASHIFT:
7803       /* This is also a multiply, so it distributes over everything.  */
7804       break;
7805
7806     case SUBREG:
7807       /* Non-paradoxical SUBREGs distributes over all operations, provided
7808          the inner modes and byte offsets are the same, this is an extraction
7809          of a low-order part, we don't convert an fp operation to int or
7810          vice versa, and we would not be converting a single-word
7811          operation into a multi-word operation.  The latter test is not
7812          required, but it prevents generating unneeded multi-word operations.
7813          Some of the previous tests are redundant given the latter test, but
7814          are retained because they are required for correctness.
7815
7816          We produce the result slightly differently in this case.  */
7817
7818       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7819           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
7820           || ! subreg_lowpart_p (lhs)
7821           || (GET_MODE_CLASS (GET_MODE (lhs))
7822               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7823           || (GET_MODE_SIZE (GET_MODE (lhs))
7824               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7825           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7826         return x;
7827
7828       tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7829                         SUBREG_REG (lhs), SUBREG_REG (rhs));
7830       return gen_lowpart_for_combine (GET_MODE (x), tem);
7831
7832     default:
7833       return x;
7834     }
7835
7836   /* Set LHS and RHS to the inner operands (A and B in the example
7837      above) and set OTHER to the common operand (C in the example).
7838      These is only one way to do this unless the inner operation is
7839      commutative.  */
7840   if (GET_RTX_CLASS (inner_code) == 'c'
7841       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7842     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7843   else if (GET_RTX_CLASS (inner_code) == 'c'
7844            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7845     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7846   else if (GET_RTX_CLASS (inner_code) == 'c'
7847            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7848     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7849   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7850     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7851   else
7852     return x;
7853
7854   /* Form the new inner operation, seeing if it simplifies first.  */
7855   tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7856
7857   /* There is one exception to the general way of distributing:
7858      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
7859   if (code == XOR && inner_code == IOR)
7860     {
7861       inner_code = AND;
7862       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
7863     }
7864
7865   /* We may be able to continuing distributing the result, so call
7866      ourselves recursively on the inner operation before forming the
7867      outer operation, which we return.  */
7868   return gen_binary (inner_code, GET_MODE (x),
7869                      apply_distributive_law (tem), other);
7870 }
7871 \f
7872 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7873    in MODE.
7874
7875    Return an equivalent form, if different from X.  Otherwise, return X.  If
7876    X is zero, we are to always construct the equivalent form.  */
7877
7878 static rtx
7879 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
7880                         unsigned HOST_WIDE_INT constop)
7881 {
7882   unsigned HOST_WIDE_INT nonzero;
7883   int i;
7884
7885   /* Simplify VAROP knowing that we will be only looking at some of the
7886      bits in it.
7887
7888      Note by passing in CONSTOP, we guarantee that the bits not set in
7889      CONSTOP are not significant and will never be examined.  We must
7890      ensure that is the case by explicitly masking out those bits
7891      before returning.  */
7892   varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7893
7894   /* If VAROP is a CLOBBER, we will fail so return it.  */
7895   if (GET_CODE (varop) == CLOBBER)
7896     return varop;
7897
7898   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
7899      to VAROP and return the new constant.  */
7900   if (GET_CODE (varop) == CONST_INT)
7901     return GEN_INT (trunc_int_for_mode (INTVAL (varop) & constop, mode));
7902
7903   /* See what bits may be nonzero in VAROP.  Unlike the general case of
7904      a call to nonzero_bits, here we don't care about bits outside
7905      MODE.  */
7906
7907   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7908
7909   /* Turn off all bits in the constant that are known to already be zero.
7910      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7911      which is tested below.  */
7912
7913   constop &= nonzero;
7914
7915   /* If we don't have any bits left, return zero.  */
7916   if (constop == 0)
7917     return const0_rtx;
7918
7919   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7920      a power of two, we can replace this with an ASHIFT.  */
7921   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7922       && (i = exact_log2 (constop)) >= 0)
7923     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7924
7925   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7926      or XOR, then try to apply the distributive law.  This may eliminate
7927      operations if either branch can be simplified because of the AND.
7928      It may also make some cases more complex, but those cases probably
7929      won't match a pattern either with or without this.  */
7930
7931   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7932     return
7933       gen_lowpart_for_combine
7934         (mode,
7935          apply_distributive_law
7936          (gen_binary (GET_CODE (varop), GET_MODE (varop),
7937                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7938                                               XEXP (varop, 0), constop),
7939                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7940                                               XEXP (varop, 1), constop))));
7941
7942   /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
7943      the AND and see if one of the operands simplifies to zero.  If so, we
7944      may eliminate it.  */
7945
7946   if (GET_CODE (varop) == PLUS
7947       && exact_log2 (constop + 1) >= 0)
7948     {
7949       rtx o0, o1;
7950
7951       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
7952       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
7953       if (o0 == const0_rtx)
7954         return o1;
7955       if (o1 == const0_rtx)
7956         return o0;
7957     }
7958
7959   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
7960      if we already had one (just check for the simplest cases).  */
7961   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7962       && GET_MODE (XEXP (x, 0)) == mode
7963       && SUBREG_REG (XEXP (x, 0)) == varop)
7964     varop = XEXP (x, 0);
7965   else
7966     varop = gen_lowpart_for_combine (mode, varop);
7967
7968   /* If we can't make the SUBREG, try to return what we were given.  */
7969   if (GET_CODE (varop) == CLOBBER)
7970     return x ? x : varop;
7971
7972   /* If we are only masking insignificant bits, return VAROP.  */
7973   if (constop == nonzero)
7974     x = varop;
7975   else
7976     {
7977       /* Otherwise, return an AND.  */
7978       constop = trunc_int_for_mode (constop, mode);
7979       /* See how much, if any, of X we can use.  */
7980       if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
7981         x = gen_binary (AND, mode, varop, GEN_INT (constop));
7982
7983       else
7984         {
7985           if (GET_CODE (XEXP (x, 1)) != CONST_INT
7986               || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
7987             SUBST (XEXP (x, 1), GEN_INT (constop));
7988
7989           SUBST (XEXP (x, 0), varop);
7990         }
7991     }
7992
7993   return x;
7994 }
7995 \f
7996 #define nonzero_bits_with_known(X, MODE) \
7997   cached_nonzero_bits (X, MODE, known_x, known_mode, known_ret)
7998
7999 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
8000    It avoids exponential behavior in nonzero_bits1 when X has
8001    identical subexpressions on the first or the second level.  */
8002
8003 static unsigned HOST_WIDE_INT
8004 cached_nonzero_bits (rtx x, enum machine_mode mode, rtx known_x,
8005                      enum machine_mode known_mode,
8006                      unsigned HOST_WIDE_INT known_ret)
8007 {
8008   if (x == known_x && mode == known_mode)
8009     return known_ret;
8010
8011   /* Try to find identical subexpressions.  If found call
8012      nonzero_bits1 on X with the subexpressions as KNOWN_X and the
8013      precomputed value for the subexpression as KNOWN_RET.  */
8014
8015   if (GET_RTX_CLASS (GET_CODE (x)) == '2'
8016       || GET_RTX_CLASS (GET_CODE (x)) == 'c')
8017     {
8018       rtx x0 = XEXP (x, 0);
8019       rtx x1 = XEXP (x, 1);
8020
8021       /* Check the first level.  */
8022       if (x0 == x1)
8023         return nonzero_bits1 (x, mode, x0, mode,
8024                               nonzero_bits_with_known (x0, mode));
8025
8026       /* Check the second level.  */
8027       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
8028            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
8029           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8030         return nonzero_bits1 (x, mode, x1, mode,
8031                               nonzero_bits_with_known (x1, mode));
8032
8033       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
8034            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
8035           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8036         return nonzero_bits1 (x, mode, x0, mode,
8037                          nonzero_bits_with_known (x0, mode));
8038     }
8039
8040   return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
8041 }
8042
8043 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
8044    We don't let nonzero_bits recur into num_sign_bit_copies, because that
8045    is less useful.  We can't allow both, because that results in exponential
8046    run time recursion.  There is a nullstone testcase that triggered
8047    this.  This macro avoids accidental uses of num_sign_bit_copies.  */
8048 #define cached_num_sign_bit_copies()
8049
8050 /* Given an expression, X, compute which bits in X can be nonzero.
8051    We don't care about bits outside of those defined in MODE.
8052
8053    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8054    a shift, AND, or zero_extract, we can do better.  */
8055
8056 static unsigned HOST_WIDE_INT
8057 nonzero_bits1 (rtx x, enum machine_mode mode, rtx known_x,
8058                enum machine_mode known_mode,
8059                unsigned HOST_WIDE_INT known_ret)
8060 {
8061   unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
8062   unsigned HOST_WIDE_INT inner_nz;
8063   enum rtx_code code;
8064   unsigned int mode_width = GET_MODE_BITSIZE (mode);
8065   rtx tem;
8066
8067   /* For floating-point values, assume all bits are needed.  */
8068   if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
8069     return nonzero;
8070
8071   /* If X is wider than MODE, use its mode instead.  */
8072   if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
8073     {
8074       mode = GET_MODE (x);
8075       nonzero = GET_MODE_MASK (mode);
8076       mode_width = GET_MODE_BITSIZE (mode);
8077     }
8078
8079   if (mode_width > HOST_BITS_PER_WIDE_INT)
8080     /* Our only callers in this case look for single bit values.  So
8081        just return the mode mask.  Those tests will then be false.  */
8082     return nonzero;
8083
8084 #ifndef WORD_REGISTER_OPERATIONS
8085   /* If MODE is wider than X, but both are a single word for both the host
8086      and target machines, we can compute this from which bits of the
8087      object might be nonzero in its own mode, taking into account the fact
8088      that on many CISC machines, accessing an object in a wider mode
8089      causes the high-order bits to become undefined.  So they are
8090      not known to be zero.  */
8091
8092   if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
8093       && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
8094       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8095       && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
8096     {
8097       nonzero &= nonzero_bits_with_known (x, GET_MODE (x));
8098       nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
8099       return nonzero;
8100     }
8101 #endif
8102
8103   code = GET_CODE (x);
8104   switch (code)
8105     {
8106     case REG:
8107 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8108       /* If pointers extend unsigned and this is a pointer in Pmode, say that
8109          all the bits above ptr_mode are known to be zero.  */
8110       if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8111           && REG_POINTER (x))
8112         nonzero &= GET_MODE_MASK (ptr_mode);
8113 #endif
8114
8115       /* Include declared information about alignment of pointers.  */
8116       /* ??? We don't properly preserve REG_POINTER changes across
8117          pointer-to-integer casts, so we can't trust it except for
8118          things that we know must be pointers.  See execute/960116-1.c.  */
8119       if ((x == stack_pointer_rtx
8120            || x == frame_pointer_rtx
8121            || x == arg_pointer_rtx)
8122           && REGNO_POINTER_ALIGN (REGNO (x)))
8123         {
8124           unsigned HOST_WIDE_INT alignment
8125             = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
8126
8127 #ifdef PUSH_ROUNDING
8128           /* If PUSH_ROUNDING is defined, it is possible for the
8129              stack to be momentarily aligned only to that amount,
8130              so we pick the least alignment.  */
8131           if (x == stack_pointer_rtx && PUSH_ARGS)
8132             alignment = MIN ((unsigned HOST_WIDE_INT) PUSH_ROUNDING (1),
8133                              alignment);
8134 #endif
8135
8136           nonzero &= ~(alignment - 1);
8137         }
8138
8139       /* If X is a register whose nonzero bits value is current, use it.
8140          Otherwise, if X is a register whose value we can find, use that
8141          value.  Otherwise, use the previously-computed global nonzero bits
8142          for this register.  */
8143
8144       if (reg_last_set_value[REGNO (x)] != 0
8145           && (reg_last_set_mode[REGNO (x)] == mode
8146               || (GET_MODE_CLASS (reg_last_set_mode[REGNO (x)]) == MODE_INT
8147                   && GET_MODE_CLASS (mode) == MODE_INT))
8148           && (reg_last_set_label[REGNO (x)] == label_tick
8149               || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8150                   && REG_N_SETS (REGNO (x)) == 1
8151                   && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8152                                         REGNO (x))))
8153           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8154         return reg_last_set_nonzero_bits[REGNO (x)] & nonzero;
8155
8156       tem = get_last_value (x);
8157
8158       if (tem)
8159         {
8160 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8161           /* If X is narrower than MODE and TEM is a non-negative
8162              constant that would appear negative in the mode of X,
8163              sign-extend it for use in reg_nonzero_bits because some
8164              machines (maybe most) will actually do the sign-extension
8165              and this is the conservative approach.
8166
8167              ??? For 2.5, try to tighten up the MD files in this regard
8168              instead of this kludge.  */
8169
8170           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
8171               && GET_CODE (tem) == CONST_INT
8172               && INTVAL (tem) > 0
8173               && 0 != (INTVAL (tem)
8174                        & ((HOST_WIDE_INT) 1
8175                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8176             tem = GEN_INT (INTVAL (tem)
8177                            | ((HOST_WIDE_INT) (-1)
8178                               << GET_MODE_BITSIZE (GET_MODE (x))));
8179 #endif
8180           return nonzero_bits_with_known (tem, mode) & nonzero;
8181         }
8182       else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
8183         {
8184           unsigned HOST_WIDE_INT mask = reg_nonzero_bits[REGNO (x)];
8185
8186           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width)
8187             /* We don't know anything about the upper bits.  */
8188             mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8189           return nonzero & mask;
8190         }
8191       else
8192         return nonzero;
8193
8194     case CONST_INT:
8195 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8196       /* If X is negative in MODE, sign-extend the value.  */
8197       if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
8198           && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
8199         return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
8200 #endif
8201
8202       return INTVAL (x);
8203
8204     case MEM:
8205 #ifdef LOAD_EXTEND_OP
8206       /* In many, if not most, RISC machines, reading a byte from memory
8207          zeros the rest of the register.  Noticing that fact saves a lot
8208          of extra zero-extends.  */
8209       if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
8210         nonzero &= GET_MODE_MASK (GET_MODE (x));
8211 #endif
8212       break;
8213
8214     case EQ:  case NE:
8215     case UNEQ:  case LTGT:
8216     case GT:  case GTU:  case UNGT:
8217     case LT:  case LTU:  case UNLT:
8218     case GE:  case GEU:  case UNGE:
8219     case LE:  case LEU:  case UNLE:
8220     case UNORDERED: case ORDERED:
8221
8222       /* If this produces an integer result, we know which bits are set.
8223          Code here used to clear bits outside the mode of X, but that is
8224          now done above.  */
8225
8226       if (GET_MODE_CLASS (mode) == MODE_INT
8227           && mode_width <= HOST_BITS_PER_WIDE_INT)
8228         nonzero = STORE_FLAG_VALUE;
8229       break;
8230
8231     case NEG:
8232 #if 0
8233       /* Disabled to avoid exponential mutual recursion between nonzero_bits
8234          and num_sign_bit_copies.  */
8235       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8236           == GET_MODE_BITSIZE (GET_MODE (x)))
8237         nonzero = 1;
8238 #endif
8239
8240       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
8241         nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
8242       break;
8243
8244     case ABS:
8245 #if 0
8246       /* Disabled to avoid exponential mutual recursion between nonzero_bits
8247          and num_sign_bit_copies.  */
8248       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8249           == GET_MODE_BITSIZE (GET_MODE (x)))
8250         nonzero = 1;
8251 #endif
8252       break;
8253
8254     case TRUNCATE:
8255       nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8256                   & GET_MODE_MASK (mode));
8257       break;
8258
8259     case ZERO_EXTEND:
8260       nonzero &= nonzero_bits_with_known (XEXP (x, 0), mode);
8261       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8262         nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8263       break;
8264
8265     case SIGN_EXTEND:
8266       /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
8267          Otherwise, show all the bits in the outer mode but not the inner
8268          may be nonzero.  */
8269       inner_nz = nonzero_bits_with_known (XEXP (x, 0), mode);
8270       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8271         {
8272           inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8273           if (inner_nz
8274               & (((HOST_WIDE_INT) 1
8275                   << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
8276             inner_nz |= (GET_MODE_MASK (mode)
8277                          & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
8278         }
8279
8280       nonzero &= inner_nz;
8281       break;
8282
8283     case AND:
8284       nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8285                   & nonzero_bits_with_known (XEXP (x, 1), mode));
8286       break;
8287
8288     case XOR:   case IOR:
8289     case UMIN:  case UMAX:  case SMIN:  case SMAX:
8290       {
8291         unsigned HOST_WIDE_INT nonzero0 =
8292           nonzero_bits_with_known (XEXP (x, 0), mode);
8293
8294         /* Don't call nonzero_bits for the second time if it cannot change
8295            anything.  */
8296         if ((nonzero & nonzero0) != nonzero)
8297           nonzero &= (nonzero0
8298                       | nonzero_bits_with_known (XEXP (x, 1), mode));
8299       }
8300       break;
8301
8302     case PLUS:  case MINUS:
8303     case MULT:
8304     case DIV:   case UDIV:
8305     case MOD:   case UMOD:
8306       /* We can apply the rules of arithmetic to compute the number of
8307          high- and low-order zero bits of these operations.  We start by
8308          computing the width (position of the highest-order nonzero bit)
8309          and the number of low-order zero bits for each value.  */
8310       {
8311         unsigned HOST_WIDE_INT nz0 =
8312           nonzero_bits_with_known (XEXP (x, 0), mode);
8313         unsigned HOST_WIDE_INT nz1 =
8314           nonzero_bits_with_known (XEXP (x, 1), mode);
8315         int sign_index = GET_MODE_BITSIZE (GET_MODE (x)) - 1;
8316         int width0 = floor_log2 (nz0) + 1;
8317         int width1 = floor_log2 (nz1) + 1;
8318         int low0 = floor_log2 (nz0 & -nz0);
8319         int low1 = floor_log2 (nz1 & -nz1);
8320         HOST_WIDE_INT op0_maybe_minusp
8321           = (nz0 & ((HOST_WIDE_INT) 1 << sign_index));
8322         HOST_WIDE_INT op1_maybe_minusp
8323           = (nz1 & ((HOST_WIDE_INT) 1 << sign_index));
8324         unsigned int result_width = mode_width;
8325         int result_low = 0;
8326
8327         switch (code)
8328           {
8329           case PLUS:
8330             result_width = MAX (width0, width1) + 1;
8331             result_low = MIN (low0, low1);
8332             break;
8333           case MINUS:
8334             result_low = MIN (low0, low1);
8335             break;
8336           case MULT:
8337             result_width = width0 + width1;
8338             result_low = low0 + low1;
8339             break;
8340           case DIV:
8341             if (width1 == 0)
8342               break;
8343             if (! op0_maybe_minusp && ! op1_maybe_minusp)
8344               result_width = width0;
8345             break;
8346           case UDIV:
8347             if (width1 == 0)
8348               break;
8349             result_width = width0;
8350             break;
8351           case MOD:
8352             if (width1 == 0)
8353               break;
8354             if (! op0_maybe_minusp && ! op1_maybe_minusp)
8355               result_width = MIN (width0, width1);
8356             result_low = MIN (low0, low1);
8357             break;
8358           case UMOD:
8359             if (width1 == 0)
8360               break;
8361             result_width = MIN (width0, width1);
8362             result_low = MIN (low0, low1);
8363             break;
8364           default:
8365             abort ();
8366           }
8367
8368         if (result_width < mode_width)
8369           nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
8370
8371         if (result_low > 0)
8372           nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
8373
8374 #ifdef POINTERS_EXTEND_UNSIGNED
8375         /* If pointers extend unsigned and this is an addition or subtraction
8376            to a pointer in Pmode, all the bits above ptr_mode are known to be
8377            zero.  */
8378         if (POINTERS_EXTEND_UNSIGNED > 0 && GET_MODE (x) == Pmode
8379             && (code == PLUS || code == MINUS)
8380             && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8381           nonzero &= GET_MODE_MASK (ptr_mode);
8382 #endif
8383       }
8384       break;
8385
8386     case ZERO_EXTRACT:
8387       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8388           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8389         nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
8390       break;
8391
8392     case SUBREG:
8393       /* If this is a SUBREG formed for a promoted variable that has
8394          been zero-extended, we know that at least the high-order bits
8395          are zero, though others might be too.  */
8396
8397       if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x) > 0)
8398         nonzero = (GET_MODE_MASK (GET_MODE (x))
8399                    & nonzero_bits_with_known (SUBREG_REG (x), GET_MODE (x)));
8400
8401       /* If the inner mode is a single word for both the host and target
8402          machines, we can compute this from which bits of the inner
8403          object might be nonzero.  */
8404       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
8405           && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8406               <= HOST_BITS_PER_WIDE_INT))
8407         {
8408           nonzero &= nonzero_bits_with_known (SUBREG_REG (x), mode);
8409
8410 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
8411           /* If this is a typical RISC machine, we only have to worry
8412              about the way loads are extended.  */
8413           if ((LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8414                ? (((nonzero
8415                     & (((unsigned HOST_WIDE_INT) 1
8416                         << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
8417                    != 0))
8418                : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
8419               || GET_CODE (SUBREG_REG (x)) != MEM)
8420 #endif
8421             {
8422               /* On many CISC machines, accessing an object in a wider mode
8423                  causes the high-order bits to become undefined.  So they are
8424                  not known to be zero.  */
8425               if (GET_MODE_SIZE (GET_MODE (x))
8426                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8427                 nonzero |= (GET_MODE_MASK (GET_MODE (x))
8428                             & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
8429             }
8430         }
8431       break;
8432
8433     case ASHIFTRT:
8434     case LSHIFTRT:
8435     case ASHIFT:
8436     case ROTATE:
8437       /* The nonzero bits are in two classes: any bits within MODE
8438          that aren't in GET_MODE (x) are always significant.  The rest of the
8439          nonzero bits are those that are significant in the operand of
8440          the shift when shifted the appropriate number of bits.  This
8441          shows that high-order bits are cleared by the right shift and
8442          low-order bits by left shifts.  */
8443       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8444           && INTVAL (XEXP (x, 1)) >= 0
8445           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8446         {
8447           enum machine_mode inner_mode = GET_MODE (x);
8448           unsigned int width = GET_MODE_BITSIZE (inner_mode);
8449           int count = INTVAL (XEXP (x, 1));
8450           unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
8451           unsigned HOST_WIDE_INT op_nonzero =
8452             nonzero_bits_with_known (XEXP (x, 0), mode);
8453           unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
8454           unsigned HOST_WIDE_INT outer = 0;
8455
8456           if (mode_width > width)
8457             outer = (op_nonzero & nonzero & ~mode_mask);
8458
8459           if (code == LSHIFTRT)
8460             inner >>= count;
8461           else if (code == ASHIFTRT)
8462             {
8463               inner >>= count;
8464
8465               /* If the sign bit may have been nonzero before the shift, we
8466                  need to mark all the places it could have been copied to
8467                  by the shift as possibly nonzero.  */
8468               if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
8469                 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
8470             }
8471           else if (code == ASHIFT)
8472             inner <<= count;
8473           else
8474             inner = ((inner << (count % width)
8475                       | (inner >> (width - (count % width)))) & mode_mask);
8476
8477           nonzero &= (outer | inner);
8478         }
8479       break;
8480
8481     case FFS:
8482     case POPCOUNT:
8483       /* This is at most the number of bits in the mode.  */
8484       nonzero = ((HOST_WIDE_INT) 2 << (floor_log2 (mode_width))) - 1;
8485       break;
8486
8487     case CLZ:
8488       /* If CLZ has a known value at zero, then the nonzero bits are
8489          that value, plus the number of bits in the mode minus one.  */
8490       if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8491         nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8492       else
8493         nonzero = -1;
8494       break;
8495
8496     case CTZ:
8497       /* If CTZ has a known value at zero, then the nonzero bits are
8498          that value, plus the number of bits in the mode minus one.  */
8499       if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8500         nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8501       else
8502         nonzero = -1;
8503       break;
8504
8505     case PARITY:
8506       nonzero = 1;
8507       break;
8508
8509     case IF_THEN_ELSE:
8510       nonzero &= (nonzero_bits_with_known (XEXP (x, 1), mode)
8511                   | nonzero_bits_with_known (XEXP (x, 2), mode));
8512       break;
8513
8514     default:
8515       break;
8516     }
8517
8518   return nonzero;
8519 }
8520
8521 /* See the macro definition above.  */
8522 #undef cached_num_sign_bit_copies
8523 \f
8524 #define num_sign_bit_copies_with_known(X, M) \
8525   cached_num_sign_bit_copies (X, M, known_x, known_mode, known_ret)
8526
8527 /* The function cached_num_sign_bit_copies is a wrapper around
8528    num_sign_bit_copies1.  It avoids exponential behavior in
8529    num_sign_bit_copies1 when X has identical subexpressions on the
8530    first or the second level.  */
8531
8532 static unsigned int
8533 cached_num_sign_bit_copies (rtx x, enum machine_mode mode, rtx known_x,
8534                             enum machine_mode known_mode,
8535                             unsigned int known_ret)
8536 {
8537   if (x == known_x && mode == known_mode)
8538     return known_ret;
8539
8540   /* Try to find identical subexpressions.  If found call
8541      num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
8542      the precomputed value for the subexpression as KNOWN_RET.  */
8543
8544   if (GET_RTX_CLASS (GET_CODE (x)) == '2'
8545       || GET_RTX_CLASS (GET_CODE (x)) == 'c')
8546     {
8547       rtx x0 = XEXP (x, 0);
8548       rtx x1 = XEXP (x, 1);
8549
8550       /* Check the first level.  */
8551       if (x0 == x1)
8552         return
8553           num_sign_bit_copies1 (x, mode, x0, mode,
8554                                 num_sign_bit_copies_with_known (x0, mode));
8555
8556       /* Check the second level.  */
8557       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
8558            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
8559           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8560         return
8561           num_sign_bit_copies1 (x, mode, x1, mode,
8562                                 num_sign_bit_copies_with_known (x1, mode));
8563
8564       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
8565            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
8566           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8567         return
8568           num_sign_bit_copies1 (x, mode, x0, mode,
8569                                 num_sign_bit_copies_with_known (x0, mode));
8570     }
8571
8572   return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
8573 }
8574
8575 /* Return the number of bits at the high-order end of X that are known to
8576    be equal to the sign bit.  X will be used in mode MODE; if MODE is
8577    VOIDmode, X will be used in its own mode.  The returned value  will always
8578    be between 1 and the number of bits in MODE.  */
8579
8580 static unsigned int
8581 num_sign_bit_copies1 (rtx x, enum machine_mode mode, rtx known_x,
8582                       enum machine_mode known_mode,
8583                       unsigned int known_ret)
8584 {
8585   enum rtx_code code = GET_CODE (x);
8586   unsigned int bitwidth;
8587   int num0, num1, result;
8588   unsigned HOST_WIDE_INT nonzero;
8589   rtx tem;
8590
8591   /* If we weren't given a mode, use the mode of X.  If the mode is still
8592      VOIDmode, we don't know anything.  Likewise if one of the modes is
8593      floating-point.  */
8594
8595   if (mode == VOIDmode)
8596     mode = GET_MODE (x);
8597
8598   if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
8599     return 1;
8600
8601   bitwidth = GET_MODE_BITSIZE (mode);
8602
8603   /* For a smaller object, just ignore the high bits.  */
8604   if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
8605     {
8606       num0 = num_sign_bit_copies_with_known (x, GET_MODE (x));
8607       return MAX (1,
8608                   num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
8609     }
8610
8611   if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
8612     {
8613 #ifndef WORD_REGISTER_OPERATIONS
8614   /* If this machine does not do all register operations on the entire
8615      register and MODE is wider than the mode of X, we can say nothing
8616      at all about the high-order bits.  */
8617       return 1;
8618 #else
8619       /* Likewise on machines that do, if the mode of the object is smaller
8620          than a word and loads of that size don't sign extend, we can say
8621          nothing about the high order bits.  */
8622       if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
8623 #ifdef LOAD_EXTEND_OP
8624           && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
8625 #endif
8626           )
8627         return 1;
8628 #endif
8629     }
8630
8631   switch (code)
8632     {
8633     case REG:
8634
8635 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8636       /* If pointers extend signed and this is a pointer in Pmode, say that
8637          all the bits above ptr_mode are known to be sign bit copies.  */
8638       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8639           && REG_POINTER (x))
8640         return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8641 #endif
8642
8643       if (reg_last_set_value[REGNO (x)] != 0
8644           && reg_last_set_mode[REGNO (x)] == mode
8645           && (reg_last_set_label[REGNO (x)] == label_tick
8646               || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8647                   && REG_N_SETS (REGNO (x)) == 1
8648                   && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8649                                         REGNO (x))))
8650           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8651         return reg_last_set_sign_bit_copies[REGNO (x)];
8652
8653       tem = get_last_value (x);
8654       if (tem != 0)
8655         return num_sign_bit_copies_with_known (tem, mode);
8656
8657       if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0
8658           && GET_MODE_BITSIZE (GET_MODE (x)) == bitwidth)
8659         return reg_sign_bit_copies[REGNO (x)];
8660       break;
8661
8662     case MEM:
8663 #ifdef LOAD_EXTEND_OP
8664       /* Some RISC machines sign-extend all loads of smaller than a word.  */
8665       if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8666         return MAX (1, ((int) bitwidth
8667                         - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
8668 #endif
8669       break;
8670
8671     case CONST_INT:
8672       /* If the constant is negative, take its 1's complement and remask.
8673          Then see how many zero bits we have.  */
8674       nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8675       if (bitwidth <= HOST_BITS_PER_WIDE_INT
8676           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8677         nonzero = (~nonzero) & GET_MODE_MASK (mode);
8678
8679       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8680
8681     case SUBREG:
8682       /* If this is a SUBREG for a promoted object that is sign-extended
8683          and we are looking at it in a wider mode, we know that at least the
8684          high-order bits are known to be sign bit copies.  */
8685
8686       if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8687         {
8688           num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8689           return MAX ((int) bitwidth
8690                       - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8691                       num0);
8692         }
8693
8694       /* For a smaller object, just ignore the high bits.  */
8695       if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8696         {
8697           num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), VOIDmode);
8698           return MAX (1, (num0
8699                           - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8700                                    - bitwidth)));
8701         }
8702
8703 #ifdef WORD_REGISTER_OPERATIONS
8704 #ifdef LOAD_EXTEND_OP
8705       /* For paradoxical SUBREGs on machines where all register operations
8706          affect the entire register, just look inside.  Note that we are
8707          passing MODE to the recursive call, so the number of sign bit copies
8708          will remain relative to that mode, not the inner mode.  */
8709
8710       /* This works only if loads sign extend.  Otherwise, if we get a
8711          reload for the inner part, it may be loaded from the stack, and
8712          then we lose all sign bit copies that existed before the store
8713          to the stack.  */
8714
8715       if ((GET_MODE_SIZE (GET_MODE (x))
8716            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8717           && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8718           && GET_CODE (SUBREG_REG (x)) == MEM)
8719         return num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8720 #endif
8721 #endif
8722       break;
8723
8724     case SIGN_EXTRACT:
8725       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8726         return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
8727       break;
8728
8729     case SIGN_EXTEND:
8730       return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8731               + num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode));
8732
8733     case TRUNCATE:
8734       /* For a smaller object, just ignore the high bits.  */
8735       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode);
8736       return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8737                                     - bitwidth)));
8738
8739     case NOT:
8740       return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8741
8742     case ROTATE:       case ROTATERT:
8743       /* If we are rotating left by a number of bits less than the number
8744          of sign bit copies, we can just subtract that amount from the
8745          number.  */
8746       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8747           && INTVAL (XEXP (x, 1)) >= 0
8748           && INTVAL (XEXP (x, 1)) < (int) bitwidth)
8749         {
8750           num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8751           return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8752                                  : (int) bitwidth - INTVAL (XEXP (x, 1))));
8753         }
8754       break;
8755
8756     case NEG:
8757       /* In general, this subtracts one sign bit copy.  But if the value
8758          is known to be positive, the number of sign bit copies is the
8759          same as that of the input.  Finally, if the input has just one bit
8760          that might be nonzero, all the bits are copies of the sign bit.  */
8761       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8762       if (bitwidth > HOST_BITS_PER_WIDE_INT)
8763         return num0 > 1 ? num0 - 1 : 1;
8764
8765       nonzero = nonzero_bits (XEXP (x, 0), mode);
8766       if (nonzero == 1)
8767         return bitwidth;
8768
8769       if (num0 > 1
8770           && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8771         num0--;
8772
8773       return num0;
8774
8775     case IOR:   case AND:   case XOR:
8776     case SMIN:  case SMAX:  case UMIN:  case UMAX:
8777       /* Logical operations will preserve the number of sign-bit copies.
8778          MIN and MAX operations always return one of the operands.  */
8779       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8780       num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8781       return MIN (num0, num1);
8782
8783     case PLUS:  case MINUS:
8784       /* For addition and subtraction, we can have a 1-bit carry.  However,
8785          if we are subtracting 1 from a positive number, there will not
8786          be such a carry.  Furthermore, if the positive number is known to
8787          be 0 or 1, we know the result is either -1 or 0.  */
8788
8789       if (code == PLUS && XEXP (x, 1) == constm1_rtx
8790           && bitwidth <= HOST_BITS_PER_WIDE_INT)
8791         {
8792           nonzero = nonzero_bits (XEXP (x, 0), mode);
8793           if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8794             return (nonzero == 1 || nonzero == 0 ? bitwidth
8795                     : bitwidth - floor_log2 (nonzero) - 1);
8796         }
8797
8798       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8799       num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8800       result = MAX (1, MIN (num0, num1) - 1);
8801
8802 #ifdef POINTERS_EXTEND_UNSIGNED
8803       /* If pointers extend signed and this is an addition or subtraction
8804          to a pointer in Pmode, all the bits above ptr_mode are known to be
8805          sign bit copies.  */
8806       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8807           && (code == PLUS || code == MINUS)
8808           && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8809         result = MAX ((int) (GET_MODE_BITSIZE (Pmode)
8810                              - GET_MODE_BITSIZE (ptr_mode) + 1),
8811                       result);
8812 #endif
8813       return result;
8814
8815     case MULT:
8816       /* The number of bits of the product is the sum of the number of
8817          bits of both terms.  However, unless one of the terms if known
8818          to be positive, we must allow for an additional bit since negating
8819          a negative number can remove one sign bit copy.  */
8820
8821       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8822       num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8823
8824       result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8825       if (result > 0
8826           && (bitwidth > HOST_BITS_PER_WIDE_INT
8827               || (((nonzero_bits (XEXP (x, 0), mode)
8828                     & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8829                   && ((nonzero_bits (XEXP (x, 1), mode)
8830                        & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8831         result--;
8832
8833       return MAX (1, result);
8834
8835     case UDIV:
8836       /* The result must be <= the first operand.  If the first operand
8837          has the high bit set, we know nothing about the number of sign
8838          bit copies.  */
8839       if (bitwidth > HOST_BITS_PER_WIDE_INT)
8840         return 1;
8841       else if ((nonzero_bits (XEXP (x, 0), mode)
8842                 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8843         return 1;
8844       else
8845         return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8846
8847     case UMOD:
8848       /* The result must be <= the second operand.  */
8849       return num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8850
8851     case DIV:
8852       /* Similar to unsigned division, except that we have to worry about
8853          the case where the divisor is negative, in which case we have
8854          to add 1.  */
8855       result = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8856       if (result > 1
8857           && (bitwidth > HOST_BITS_PER_WIDE_INT
8858               || (nonzero_bits (XEXP (x, 1), mode)
8859                   & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8860         result--;
8861
8862       return result;
8863
8864     case MOD:
8865       result = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8866       if (result > 1
8867           && (bitwidth > HOST_BITS_PER_WIDE_INT
8868               || (nonzero_bits (XEXP (x, 1), mode)
8869                   & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8870         result--;
8871
8872       return result;
8873
8874     case ASHIFTRT:
8875       /* Shifts by a constant add to the number of bits equal to the
8876          sign bit.  */
8877       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8878       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8879           && INTVAL (XEXP (x, 1)) > 0)
8880         num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
8881
8882       return num0;
8883
8884     case ASHIFT:
8885       /* Left shifts destroy copies.  */
8886       if (GET_CODE (XEXP (x, 1)) != CONST_INT
8887           || INTVAL (XEXP (x, 1)) < 0
8888           || INTVAL (XEXP (x, 1)) >= (int) bitwidth)
8889         return 1;
8890
8891       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8892       return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8893
8894     case IF_THEN_ELSE:
8895       num0 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8896       num1 = num_sign_bit_copies_with_known (XEXP (x, 2), mode);
8897       return MIN (num0, num1);
8898
8899     case EQ:  case NE:  case GE:  case GT:  case LE:  case LT:
8900     case UNEQ:  case LTGT:  case UNGE:  case UNGT:  case UNLE:  case UNLT:
8901     case GEU: case GTU: case LEU: case LTU:
8902     case UNORDERED: case ORDERED:
8903       /* If the constant is negative, take its 1's complement and remask.
8904          Then see how many zero bits we have.  */
8905       nonzero = STORE_FLAG_VALUE;
8906       if (bitwidth <= HOST_BITS_PER_WIDE_INT
8907           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8908         nonzero = (~nonzero) & GET_MODE_MASK (mode);
8909
8910       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8911       break;
8912
8913     default:
8914       break;
8915     }
8916
8917   /* If we haven't been able to figure it out by one of the above rules,
8918      see if some of the high-order bits are known to be zero.  If so,
8919      count those bits and return one less than that amount.  If we can't
8920      safely compute the mask for this mode, always return BITWIDTH.  */
8921
8922   if (bitwidth > HOST_BITS_PER_WIDE_INT)
8923     return 1;
8924
8925   nonzero = nonzero_bits (x, mode);
8926   return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8927           ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8928 }
8929 \f
8930 /* Return the number of "extended" bits there are in X, when interpreted
8931    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
8932    unsigned quantities, this is the number of high-order zero bits.
8933    For signed quantities, this is the number of copies of the sign bit
8934    minus 1.  In both case, this function returns the number of "spare"
8935    bits.  For example, if two quantities for which this function returns
8936    at least 1 are added, the addition is known not to overflow.
8937
8938    This function will always return 0 unless called during combine, which
8939    implies that it must be called from a define_split.  */
8940
8941 unsigned int
8942 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8943 {
8944   if (nonzero_sign_valid == 0)
8945     return 0;
8946
8947   return (unsignedp
8948           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8949              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8950                                - floor_log2 (nonzero_bits (x, mode)))
8951              : 0)
8952           : num_sign_bit_copies (x, mode) - 1);
8953 }
8954 \f
8955 /* This function is called from `simplify_shift_const' to merge two
8956    outer operations.  Specifically, we have already found that we need
8957    to perform operation *POP0 with constant *PCONST0 at the outermost
8958    position.  We would now like to also perform OP1 with constant CONST1
8959    (with *POP0 being done last).
8960
8961    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8962    the resulting operation.  *PCOMP_P is set to 1 if we would need to
8963    complement the innermost operand, otherwise it is unchanged.
8964
8965    MODE is the mode in which the operation will be done.  No bits outside
8966    the width of this mode matter.  It is assumed that the width of this mode
8967    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8968
8969    If *POP0 or OP1 are NIL, it means no operation is required.  Only NEG, PLUS,
8970    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
8971    result is simply *PCONST0.
8972
8973    If the resulting operation cannot be expressed as one operation, we
8974    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
8975
8976 static int
8977 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, enum machine_mode mode, int *pcomp_p)
8978 {
8979   enum rtx_code op0 = *pop0;
8980   HOST_WIDE_INT const0 = *pconst0;
8981
8982   const0 &= GET_MODE_MASK (mode);
8983   const1 &= GET_MODE_MASK (mode);
8984
8985   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
8986   if (op0 == AND)
8987     const1 &= const0;
8988
8989   /* If OP0 or OP1 is NIL, this is easy.  Similarly if they are the same or
8990      if OP0 is SET.  */
8991
8992   if (op1 == NIL || op0 == SET)
8993     return 1;
8994
8995   else if (op0 == NIL)
8996     op0 = op1, const0 = const1;
8997
8998   else if (op0 == op1)
8999     {
9000       switch (op0)
9001         {
9002         case AND:
9003           const0 &= const1;
9004           break;
9005         case IOR:
9006           const0 |= const1;
9007           break;
9008         case XOR:
9009           const0 ^= const1;
9010           break;
9011         case PLUS:
9012           const0 += const1;
9013           break;
9014         case NEG:
9015           op0 = NIL;
9016           break;
9017         default:
9018           break;
9019         }
9020     }
9021
9022   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
9023   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9024     return 0;
9025
9026   /* If the two constants aren't the same, we can't do anything.  The
9027      remaining six cases can all be done.  */
9028   else if (const0 != const1)
9029     return 0;
9030
9031   else
9032     switch (op0)
9033       {
9034       case IOR:
9035         if (op1 == AND)
9036           /* (a & b) | b == b */
9037           op0 = SET;
9038         else /* op1 == XOR */
9039           /* (a ^ b) | b == a | b */
9040           {;}
9041         break;
9042
9043       case XOR:
9044         if (op1 == AND)
9045           /* (a & b) ^ b == (~a) & b */
9046           op0 = AND, *pcomp_p = 1;
9047         else /* op1 == IOR */
9048           /* (a | b) ^ b == a & ~b */
9049           op0 = AND, const0 = ~const0;
9050         break;
9051
9052       case AND:
9053         if (op1 == IOR)
9054           /* (a | b) & b == b */
9055         op0 = SET;
9056         else /* op1 == XOR */
9057           /* (a ^ b) & b) == (~a) & b */
9058           *pcomp_p = 1;
9059         break;
9060       default:
9061         break;
9062       }
9063
9064   /* Check for NO-OP cases.  */
9065   const0 &= GET_MODE_MASK (mode);
9066   if (const0 == 0
9067       && (op0 == IOR || op0 == XOR || op0 == PLUS))
9068     op0 = NIL;
9069   else if (const0 == 0 && op0 == AND)
9070     op0 = SET;
9071   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9072            && op0 == AND)
9073     op0 = NIL;
9074
9075   /* ??? Slightly redundant with the above mask, but not entirely.
9076      Moving this above means we'd have to sign-extend the mode mask
9077      for the final test.  */
9078   const0 = trunc_int_for_mode (const0, mode);
9079
9080   *pop0 = op0;
9081   *pconst0 = const0;
9082
9083   return 1;
9084 }
9085 \f
9086 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
9087    The result of the shift is RESULT_MODE.  X, if nonzero, is an expression
9088    that we started with.
9089
9090    The shift is normally computed in the widest mode we find in VAROP, as
9091    long as it isn't a different number of words than RESULT_MODE.  Exceptions
9092    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
9093
9094 static rtx
9095 simplify_shift_const (rtx x, enum rtx_code code,
9096                       enum machine_mode result_mode, rtx varop,
9097                       int orig_count)
9098 {
9099   enum rtx_code orig_code = code;
9100   unsigned int count;
9101   int signed_count;
9102   enum machine_mode mode = result_mode;
9103   enum machine_mode shift_mode, tmode;
9104   unsigned int mode_words
9105     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9106   /* We form (outer_op (code varop count) (outer_const)).  */
9107   enum rtx_code outer_op = NIL;
9108   HOST_WIDE_INT outer_const = 0;
9109   rtx const_rtx;
9110   int complement_p = 0;
9111   rtx new;
9112
9113   /* Make sure and truncate the "natural" shift on the way in.  We don't
9114      want to do this inside the loop as it makes it more difficult to
9115      combine shifts.  */
9116 #ifdef SHIFT_COUNT_TRUNCATED
9117   if (SHIFT_COUNT_TRUNCATED)
9118     orig_count &= GET_MODE_BITSIZE (mode) - 1;
9119 #endif
9120
9121   /* If we were given an invalid count, don't do anything except exactly
9122      what was requested.  */
9123
9124   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9125     {
9126       if (x)
9127         return x;
9128
9129       return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
9130     }
9131
9132   count = orig_count;
9133
9134   /* Unless one of the branches of the `if' in this loop does a `continue',
9135      we will `break' the loop after the `if'.  */
9136
9137   while (count != 0)
9138     {
9139       /* If we have an operand of (clobber (const_int 0)), just return that
9140          value.  */
9141       if (GET_CODE (varop) == CLOBBER)
9142         return varop;
9143
9144       /* If we discovered we had to complement VAROP, leave.  Making a NOT
9145          here would cause an infinite loop.  */
9146       if (complement_p)
9147         break;
9148
9149       /* Convert ROTATERT to ROTATE.  */
9150       if (code == ROTATERT)
9151         {
9152           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9153           code = ROTATE;
9154           if (VECTOR_MODE_P (result_mode))
9155             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9156           else
9157             count = bitsize - count;
9158         }
9159
9160       /* We need to determine what mode we will do the shift in.  If the
9161          shift is a right shift or a ROTATE, we must always do it in the mode
9162          it was originally done in.  Otherwise, we can do it in MODE, the
9163          widest mode encountered.  */
9164       shift_mode
9165         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9166            ? result_mode : mode);
9167
9168       /* Handle cases where the count is greater than the size of the mode
9169          minus 1.  For ASHIFT, use the size minus one as the count (this can
9170          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
9171          take the count modulo the size.  For other shifts, the result is
9172          zero.
9173
9174          Since these shifts are being produced by the compiler by combining
9175          multiple operations, each of which are defined, we know what the
9176          result is supposed to be.  */
9177
9178       if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
9179         {
9180           if (code == ASHIFTRT)
9181             count = GET_MODE_BITSIZE (shift_mode) - 1;
9182           else if (code == ROTATE || code == ROTATERT)
9183             count %= GET_MODE_BITSIZE (shift_mode);
9184           else
9185             {
9186               /* We can't simply return zero because there may be an
9187                  outer op.  */
9188               varop = const0_rtx;
9189               count = 0;
9190               break;
9191             }
9192         }
9193
9194       /* An arithmetic right shift of a quantity known to be -1 or 0
9195          is a no-op.  */
9196       if (code == ASHIFTRT
9197           && (num_sign_bit_copies (varop, shift_mode)
9198               == GET_MODE_BITSIZE (shift_mode)))
9199         {
9200           count = 0;
9201           break;
9202         }
9203
9204       /* If we are doing an arithmetic right shift and discarding all but
9205          the sign bit copies, this is equivalent to doing a shift by the
9206          bitsize minus one.  Convert it into that shift because it will often
9207          allow other simplifications.  */
9208
9209       if (code == ASHIFTRT
9210           && (count + num_sign_bit_copies (varop, shift_mode)
9211               >= GET_MODE_BITSIZE (shift_mode)))
9212         count = GET_MODE_BITSIZE (shift_mode) - 1;
9213
9214       /* We simplify the tests below and elsewhere by converting
9215          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9216          `make_compound_operation' will convert it to an ASHIFTRT for
9217          those machines (such as VAX) that don't have an LSHIFTRT.  */
9218       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9219           && code == ASHIFTRT
9220           && ((nonzero_bits (varop, shift_mode)
9221                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9222               == 0))
9223         code = LSHIFTRT;
9224
9225       if (code == LSHIFTRT
9226           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9227           && !(nonzero_bits (varop, shift_mode) >> count))
9228         varop = const0_rtx;
9229       if (code == ASHIFT
9230           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9231           && !((nonzero_bits (varop, shift_mode) << count)
9232                & GET_MODE_MASK (shift_mode)))
9233         varop = const0_rtx;
9234
9235       switch (GET_CODE (varop))
9236         {
9237         case SIGN_EXTEND:
9238         case ZERO_EXTEND:
9239         case SIGN_EXTRACT:
9240         case ZERO_EXTRACT:
9241           new = expand_compound_operation (varop);
9242           if (new != varop)
9243             {
9244               varop = new;
9245               continue;
9246             }
9247           break;
9248
9249         case MEM:
9250           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9251              minus the width of a smaller mode, we can do this with a
9252              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
9253           if ((code == ASHIFTRT || code == LSHIFTRT)
9254               && ! mode_dependent_address_p (XEXP (varop, 0))
9255               && ! MEM_VOLATILE_P (varop)
9256               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9257                                          MODE_INT, 1)) != BLKmode)
9258             {
9259               new = adjust_address_nv (varop, tmode,
9260                                        BYTES_BIG_ENDIAN ? 0
9261                                        : count / BITS_PER_UNIT);
9262
9263               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9264                                      : ZERO_EXTEND, mode, new);
9265               count = 0;
9266               continue;
9267             }
9268           break;
9269
9270         case USE:
9271           /* Similar to the case above, except that we can only do this if
9272              the resulting mode is the same as that of the underlying
9273              MEM and adjust the address depending on the *bits* endianness
9274              because of the way that bit-field extract insns are defined.  */
9275           if ((code == ASHIFTRT || code == LSHIFTRT)
9276               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9277                                          MODE_INT, 1)) != BLKmode
9278               && tmode == GET_MODE (XEXP (varop, 0)))
9279             {
9280               if (BITS_BIG_ENDIAN)
9281                 new = XEXP (varop, 0);
9282               else
9283                 {
9284                   new = copy_rtx (XEXP (varop, 0));
9285                   SUBST (XEXP (new, 0),
9286                          plus_constant (XEXP (new, 0),
9287                                         count / BITS_PER_UNIT));
9288                 }
9289
9290               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9291                                      : ZERO_EXTEND, mode, new);
9292               count = 0;
9293               continue;
9294             }
9295           break;
9296
9297         case SUBREG:
9298           /* If VAROP is a SUBREG, strip it as long as the inner operand has
9299              the same number of words as what we've seen so far.  Then store
9300              the widest mode in MODE.  */
9301           if (subreg_lowpart_p (varop)
9302               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9303                   > GET_MODE_SIZE (GET_MODE (varop)))
9304               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9305                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9306                  == mode_words)
9307             {
9308               varop = SUBREG_REG (varop);
9309               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9310                 mode = GET_MODE (varop);
9311               continue;
9312             }
9313           break;
9314
9315         case MULT:
9316           /* Some machines use MULT instead of ASHIFT because MULT
9317              is cheaper.  But it is still better on those machines to
9318              merge two shifts into one.  */
9319           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9320               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9321             {
9322               varop
9323                 = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
9324                               GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9325               continue;
9326             }
9327           break;
9328
9329         case UDIV:
9330           /* Similar, for when divides are cheaper.  */
9331           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9332               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9333             {
9334               varop
9335                 = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
9336                               GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9337               continue;
9338             }
9339           break;
9340
9341         case ASHIFTRT:
9342           /* If we are extracting just the sign bit of an arithmetic
9343              right shift, that shift is not needed.  However, the sign
9344              bit of a wider mode may be different from what would be
9345              interpreted as the sign bit in a narrower mode, so, if
9346              the result is narrower, don't discard the shift.  */
9347           if (code == LSHIFTRT
9348               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9349               && (GET_MODE_BITSIZE (result_mode)
9350                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
9351             {
9352               varop = XEXP (varop, 0);
9353               continue;
9354             }
9355
9356           /* ... fall through ...  */
9357
9358         case LSHIFTRT:
9359         case ASHIFT:
9360         case ROTATE:
9361           /* Here we have two nested shifts.  The result is usually the
9362              AND of a new shift with a mask.  We compute the result below.  */
9363           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9364               && INTVAL (XEXP (varop, 1)) >= 0
9365               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9366               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9367               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9368             {
9369               enum rtx_code first_code = GET_CODE (varop);
9370               unsigned int first_count = INTVAL (XEXP (varop, 1));
9371               unsigned HOST_WIDE_INT mask;
9372               rtx mask_rtx;
9373
9374               /* We have one common special case.  We can't do any merging if
9375                  the inner code is an ASHIFTRT of a smaller mode.  However, if
9376                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9377                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9378                  we can convert it to
9379                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9380                  This simplifies certain SIGN_EXTEND operations.  */
9381               if (code == ASHIFT && first_code == ASHIFTRT
9382                   && count == (unsigned int)
9383                               (GET_MODE_BITSIZE (result_mode)
9384                                - GET_MODE_BITSIZE (GET_MODE (varop))))
9385                 {
9386                   /* C3 has the low-order C1 bits zero.  */
9387
9388                   mask = (GET_MODE_MASK (mode)
9389                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9390
9391                   varop = simplify_and_const_int (NULL_RTX, result_mode,
9392                                                   XEXP (varop, 0), mask);
9393                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9394                                                 varop, count);
9395                   count = first_count;
9396                   code = ASHIFTRT;
9397                   continue;
9398                 }
9399
9400               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9401                  than C1 high-order bits equal to the sign bit, we can convert
9402                  this to either an ASHIFT or an ASHIFTRT depending on the
9403                  two counts.
9404
9405                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
9406
9407               if (code == ASHIFTRT && first_code == ASHIFT
9408                   && GET_MODE (varop) == shift_mode
9409                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9410                       > first_count))
9411                 {
9412                   varop = XEXP (varop, 0);
9413
9414                   signed_count = count - first_count;
9415                   if (signed_count < 0)
9416                     count = -signed_count, code = ASHIFT;
9417                   else
9418                     count = signed_count;
9419
9420                   continue;
9421                 }
9422
9423               /* There are some cases we can't do.  If CODE is ASHIFTRT,
9424                  we can only do this if FIRST_CODE is also ASHIFTRT.
9425
9426                  We can't do the case when CODE is ROTATE and FIRST_CODE is
9427                  ASHIFTRT.
9428
9429                  If the mode of this shift is not the mode of the outer shift,
9430                  we can't do this if either shift is a right shift or ROTATE.
9431
9432                  Finally, we can't do any of these if the mode is too wide
9433                  unless the codes are the same.
9434
9435                  Handle the case where the shift codes are the same
9436                  first.  */
9437
9438               if (code == first_code)
9439                 {
9440                   if (GET_MODE (varop) != result_mode
9441                       && (code == ASHIFTRT || code == LSHIFTRT
9442                           || code == ROTATE))
9443                     break;
9444
9445                   count += first_count;
9446                   varop = XEXP (varop, 0);
9447                   continue;
9448                 }
9449
9450               if (code == ASHIFTRT
9451                   || (code == ROTATE && first_code == ASHIFTRT)
9452                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9453                   || (GET_MODE (varop) != result_mode
9454                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
9455                           || first_code == ROTATE
9456                           || code == ROTATE)))
9457                 break;
9458
9459               /* To compute the mask to apply after the shift, shift the
9460                  nonzero bits of the inner shift the same way the
9461                  outer shift will.  */
9462
9463               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9464
9465               mask_rtx
9466                 = simplify_binary_operation (code, result_mode, mask_rtx,
9467                                              GEN_INT (count));
9468
9469               /* Give up if we can't compute an outer operation to use.  */
9470               if (mask_rtx == 0
9471                   || GET_CODE (mask_rtx) != CONST_INT
9472                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
9473                                         INTVAL (mask_rtx),
9474                                         result_mode, &complement_p))
9475                 break;
9476
9477               /* If the shifts are in the same direction, we add the
9478                  counts.  Otherwise, we subtract them.  */
9479               signed_count = count;
9480               if ((code == ASHIFTRT || code == LSHIFTRT)
9481                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9482                 signed_count += first_count;
9483               else
9484                 signed_count -= first_count;
9485
9486               /* If COUNT is positive, the new shift is usually CODE,
9487                  except for the two exceptions below, in which case it is
9488                  FIRST_CODE.  If the count is negative, FIRST_CODE should
9489                  always be used  */
9490               if (signed_count > 0
9491                   && ((first_code == ROTATE && code == ASHIFT)
9492                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
9493                 code = first_code, count = signed_count;
9494               else if (signed_count < 0)
9495                 code = first_code, count = -signed_count;
9496               else
9497                 count = signed_count;
9498
9499               varop = XEXP (varop, 0);
9500               continue;
9501             }
9502
9503           /* If we have (A << B << C) for any shift, we can convert this to
9504              (A << C << B).  This wins if A is a constant.  Only try this if
9505              B is not a constant.  */
9506
9507           else if (GET_CODE (varop) == code
9508                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
9509                    && 0 != (new
9510                             = simplify_binary_operation (code, mode,
9511                                                          XEXP (varop, 0),
9512                                                          GEN_INT (count))))
9513             {
9514               varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9515               count = 0;
9516               continue;
9517             }
9518           break;
9519
9520         case NOT:
9521           /* Make this fit the case below.  */
9522           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9523                                GEN_INT (GET_MODE_MASK (mode)));
9524           continue;
9525
9526         case IOR:
9527         case AND:
9528         case XOR:
9529           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9530              with C the size of VAROP - 1 and the shift is logical if
9531              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9532              we have an (le X 0) operation.   If we have an arithmetic shift
9533              and STORE_FLAG_VALUE is 1 or we have a logical shift with
9534              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
9535
9536           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9537               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9538               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9539               && (code == LSHIFTRT || code == ASHIFTRT)
9540               && count == (unsigned int)
9541                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9542               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9543             {
9544               count = 0;
9545               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9546                                   const0_rtx);
9547
9548               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9549                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9550
9551               continue;
9552             }
9553
9554           /* If we have (shift (logical)), move the logical to the outside
9555              to allow it to possibly combine with another logical and the
9556              shift to combine with another shift.  This also canonicalizes to
9557              what a ZERO_EXTRACT looks like.  Also, some machines have
9558              (and (shift)) insns.  */
9559
9560           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9561               && (new = simplify_binary_operation (code, result_mode,
9562                                                    XEXP (varop, 1),
9563                                                    GEN_INT (count))) != 0
9564               && GET_CODE (new) == CONST_INT
9565               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9566                                   INTVAL (new), result_mode, &complement_p))
9567             {
9568               varop = XEXP (varop, 0);
9569               continue;
9570             }
9571
9572           /* If we can't do that, try to simplify the shift in each arm of the
9573              logical expression, make a new logical expression, and apply
9574              the inverse distributive law.  */
9575           {
9576             rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9577                                             XEXP (varop, 0), count);
9578             rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9579                                             XEXP (varop, 1), count);
9580
9581             varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
9582             varop = apply_distributive_law (varop);
9583
9584             count = 0;
9585           }
9586           break;
9587
9588         case EQ:
9589           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9590              says that the sign bit can be tested, FOO has mode MODE, C is
9591              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9592              that may be nonzero.  */
9593           if (code == LSHIFTRT
9594               && XEXP (varop, 1) == const0_rtx
9595               && GET_MODE (XEXP (varop, 0)) == result_mode
9596               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9597               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9598               && ((STORE_FLAG_VALUE
9599                    & ((HOST_WIDE_INT) 1
9600                       < (GET_MODE_BITSIZE (result_mode) - 1))))
9601               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9602               && merge_outer_ops (&outer_op, &outer_const, XOR,
9603                                   (HOST_WIDE_INT) 1, result_mode,
9604                                   &complement_p))
9605             {
9606               varop = XEXP (varop, 0);
9607               count = 0;
9608               continue;
9609             }
9610           break;
9611
9612         case NEG:
9613           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9614              than the number of bits in the mode is equivalent to A.  */
9615           if (code == LSHIFTRT
9616               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9617               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9618             {
9619               varop = XEXP (varop, 0);
9620               count = 0;
9621               continue;
9622             }
9623
9624           /* NEG commutes with ASHIFT since it is multiplication.  Move the
9625              NEG outside to allow shifts to combine.  */
9626           if (code == ASHIFT
9627               && merge_outer_ops (&outer_op, &outer_const, NEG,
9628                                   (HOST_WIDE_INT) 0, result_mode,
9629                                   &complement_p))
9630             {
9631               varop = XEXP (varop, 0);
9632               continue;
9633             }
9634           break;
9635
9636         case PLUS:
9637           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9638              is one less than the number of bits in the mode is
9639              equivalent to (xor A 1).  */
9640           if (code == LSHIFTRT
9641               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9642               && XEXP (varop, 1) == constm1_rtx
9643               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9644               && merge_outer_ops (&outer_op, &outer_const, XOR,
9645                                   (HOST_WIDE_INT) 1, result_mode,
9646                                   &complement_p))
9647             {
9648               count = 0;
9649               varop = XEXP (varop, 0);
9650               continue;
9651             }
9652
9653           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9654              that might be nonzero in BAR are those being shifted out and those
9655              bits are known zero in FOO, we can replace the PLUS with FOO.
9656              Similarly in the other operand order.  This code occurs when
9657              we are computing the size of a variable-size array.  */
9658
9659           if ((code == ASHIFTRT || code == LSHIFTRT)
9660               && count < HOST_BITS_PER_WIDE_INT
9661               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9662               && (nonzero_bits (XEXP (varop, 1), result_mode)
9663                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9664             {
9665               varop = XEXP (varop, 0);
9666               continue;
9667             }
9668           else if ((code == ASHIFTRT || code == LSHIFTRT)
9669                    && count < HOST_BITS_PER_WIDE_INT
9670                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9671                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9672                             >> count)
9673                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9674                             & nonzero_bits (XEXP (varop, 1),
9675                                                  result_mode)))
9676             {
9677               varop = XEXP (varop, 1);
9678               continue;
9679             }
9680
9681           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
9682           if (code == ASHIFT
9683               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9684               && (new = simplify_binary_operation (ASHIFT, result_mode,
9685                                                    XEXP (varop, 1),
9686                                                    GEN_INT (count))) != 0
9687               && GET_CODE (new) == CONST_INT
9688               && merge_outer_ops (&outer_op, &outer_const, PLUS,
9689                                   INTVAL (new), result_mode, &complement_p))
9690             {
9691               varop = XEXP (varop, 0);
9692               continue;
9693             }
9694           break;
9695
9696         case MINUS:
9697           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9698              with C the size of VAROP - 1 and the shift is logical if
9699              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9700              we have a (gt X 0) operation.  If the shift is arithmetic with
9701              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9702              we have a (neg (gt X 0)) operation.  */
9703
9704           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9705               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9706               && count == (unsigned int)
9707                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9708               && (code == LSHIFTRT || code == ASHIFTRT)
9709               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9710               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9711                  == count
9712               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9713             {
9714               count = 0;
9715               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9716                                   const0_rtx);
9717
9718               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9719                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9720
9721               continue;
9722             }
9723           break;
9724
9725         case TRUNCATE:
9726           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9727              if the truncate does not affect the value.  */
9728           if (code == LSHIFTRT
9729               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9730               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9731               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9732                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9733                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9734             {
9735               rtx varop_inner = XEXP (varop, 0);
9736
9737               varop_inner
9738                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9739                                     XEXP (varop_inner, 0),
9740                                     GEN_INT
9741                                     (count + INTVAL (XEXP (varop_inner, 1))));
9742               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9743               count = 0;
9744               continue;
9745             }
9746           break;
9747
9748         default:
9749           break;
9750         }
9751
9752       break;
9753     }
9754
9755   /* We need to determine what mode to do the shift in.  If the shift is
9756      a right shift or ROTATE, we must always do it in the mode it was
9757      originally done in.  Otherwise, we can do it in MODE, the widest mode
9758      encountered.  The code we care about is that of the shift that will
9759      actually be done, not the shift that was originally requested.  */
9760   shift_mode
9761     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9762        ? result_mode : mode);
9763
9764   /* We have now finished analyzing the shift.  The result should be
9765      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9766      OUTER_OP is non-NIL, it is an operation that needs to be applied
9767      to the result of the shift.  OUTER_CONST is the relevant constant,
9768      but we must turn off all bits turned off in the shift.
9769
9770      If we were passed a value for X, see if we can use any pieces of
9771      it.  If not, make new rtx.  */
9772
9773   if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9774       && GET_CODE (XEXP (x, 1)) == CONST_INT
9775       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9776     const_rtx = XEXP (x, 1);
9777   else
9778     const_rtx = GEN_INT (count);
9779
9780   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9781       && GET_MODE (XEXP (x, 0)) == shift_mode
9782       && SUBREG_REG (XEXP (x, 0)) == varop)
9783     varop = XEXP (x, 0);
9784   else if (GET_MODE (varop) != shift_mode)
9785     varop = gen_lowpart_for_combine (shift_mode, varop);
9786
9787   /* If we can't make the SUBREG, try to return what we were given.  */
9788   if (GET_CODE (varop) == CLOBBER)
9789     return x ? x : varop;
9790
9791   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9792   if (new != 0)
9793     x = new;
9794   else
9795     x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9796
9797   /* If we have an outer operation and we just made a shift, it is
9798      possible that we could have simplified the shift were it not
9799      for the outer operation.  So try to do the simplification
9800      recursively.  */
9801
9802   if (outer_op != NIL && GET_CODE (x) == code
9803       && GET_CODE (XEXP (x, 1)) == CONST_INT)
9804     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9805                               INTVAL (XEXP (x, 1)));
9806
9807   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9808      turn off all the bits that the shift would have turned off.  */
9809   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9810     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9811                                 GET_MODE_MASK (result_mode) >> orig_count);
9812
9813   /* Do the remainder of the processing in RESULT_MODE.  */
9814   x = gen_lowpart_for_combine (result_mode, x);
9815
9816   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9817      operation.  */
9818   if (complement_p)
9819     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9820
9821   if (outer_op != NIL)
9822     {
9823       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9824         outer_const = trunc_int_for_mode (outer_const, result_mode);
9825
9826       if (outer_op == AND)
9827         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9828       else if (outer_op == SET)
9829         /* This means that we have determined that the result is
9830            equivalent to a constant.  This should be rare.  */
9831         x = GEN_INT (outer_const);
9832       else if (GET_RTX_CLASS (outer_op) == '1')
9833         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9834       else
9835         x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9836     }
9837
9838   return x;
9839 }
9840 \f
9841 /* Like recog, but we receive the address of a pointer to a new pattern.
9842    We try to match the rtx that the pointer points to.
9843    If that fails, we may try to modify or replace the pattern,
9844    storing the replacement into the same pointer object.
9845
9846    Modifications include deletion or addition of CLOBBERs.
9847
9848    PNOTES is a pointer to a location where any REG_UNUSED notes added for
9849    the CLOBBERs are placed.
9850
9851    The value is the final insn code from the pattern ultimately matched,
9852    or -1.  */
9853
9854 static int
9855 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9856 {
9857   rtx pat = *pnewpat;
9858   int insn_code_number;
9859   int num_clobbers_to_add = 0;
9860   int i;
9861   rtx notes = 0;
9862   rtx dummy_insn;
9863
9864   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9865      we use to indicate that something didn't match.  If we find such a
9866      thing, force rejection.  */
9867   if (GET_CODE (pat) == PARALLEL)
9868     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9869       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9870           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9871         return -1;
9872
9873   /* *pnewpat does not have to be actual PATTERN (insn), so make a dummy
9874      instruction for pattern recognition.  */
9875   dummy_insn = shallow_copy_rtx (insn);
9876   PATTERN (dummy_insn) = pat;
9877   REG_NOTES (dummy_insn) = 0;
9878
9879   insn_code_number = recog (pat, dummy_insn, &num_clobbers_to_add);
9880
9881   /* If it isn't, there is the possibility that we previously had an insn
9882      that clobbered some register as a side effect, but the combined
9883      insn doesn't need to do that.  So try once more without the clobbers
9884      unless this represents an ASM insn.  */
9885
9886   if (insn_code_number < 0 && ! check_asm_operands (pat)
9887       && GET_CODE (pat) == PARALLEL)
9888     {
9889       int pos;
9890
9891       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9892         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9893           {
9894             if (i != pos)
9895               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9896             pos++;
9897           }
9898
9899       SUBST_INT (XVECLEN (pat, 0), pos);
9900
9901       if (pos == 1)
9902         pat = XVECEXP (pat, 0, 0);
9903
9904       PATTERN (dummy_insn) = pat;
9905       insn_code_number = recog (pat, dummy_insn, &num_clobbers_to_add);
9906     }
9907
9908   /* Recognize all noop sets, these will be killed by followup pass.  */
9909   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9910     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9911
9912   /* If we had any clobbers to add, make a new pattern than contains
9913      them.  Then check to make sure that all of them are dead.  */
9914   if (num_clobbers_to_add)
9915     {
9916       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9917                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
9918                                                   ? (XVECLEN (pat, 0)
9919                                                      + num_clobbers_to_add)
9920                                                   : num_clobbers_to_add + 1));
9921
9922       if (GET_CODE (pat) == PARALLEL)
9923         for (i = 0; i < XVECLEN (pat, 0); i++)
9924           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9925       else
9926         XVECEXP (newpat, 0, 0) = pat;
9927
9928       add_clobbers (newpat, insn_code_number);
9929
9930       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9931            i < XVECLEN (newpat, 0); i++)
9932         {
9933           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9934               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9935             return -1;
9936           notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9937                                      XEXP (XVECEXP (newpat, 0, i), 0), notes);
9938         }
9939       pat = newpat;
9940     }
9941
9942   *pnewpat = pat;
9943   *pnotes = notes;
9944
9945   return insn_code_number;
9946 }
9947 \f
9948 /* Like gen_lowpart but for use by combine.  In combine it is not possible
9949    to create any new pseudoregs.  However, it is safe to create
9950    invalid memory addresses, because combine will try to recognize
9951    them and all they will do is make the combine attempt fail.
9952
9953    If for some reason this cannot do its job, an rtx
9954    (clobber (const_int 0)) is returned.
9955    An insn containing that will not be recognized.  */
9956
9957 #undef gen_lowpart
9958
9959 static rtx
9960 gen_lowpart_for_combine (enum machine_mode mode, rtx x)
9961 {
9962   rtx result;
9963
9964   if (GET_MODE (x) == mode)
9965     return x;
9966
9967   /* Return identity if this is a CONST or symbolic
9968      reference.  */
9969   if (mode == Pmode
9970       && (GET_CODE (x) == CONST
9971           || GET_CODE (x) == SYMBOL_REF
9972           || GET_CODE (x) == LABEL_REF))
9973     return x;
9974
9975   /* We can only support MODE being wider than a word if X is a
9976      constant integer or has a mode the same size.  */
9977
9978   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9979       && ! ((GET_MODE (x) == VOIDmode
9980              && (GET_CODE (x) == CONST_INT
9981                  || GET_CODE (x) == CONST_DOUBLE))
9982             || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
9983     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9984
9985   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
9986      won't know what to do.  So we will strip off the SUBREG here and
9987      process normally.  */
9988   if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
9989     {
9990       x = SUBREG_REG (x);
9991       if (GET_MODE (x) == mode)
9992         return x;
9993     }
9994
9995   result = gen_lowpart_common (mode, x);
9996 #ifdef CANNOT_CHANGE_MODE_CLASS
9997   if (result != 0
9998       && GET_CODE (result) == SUBREG
9999       && GET_CODE (SUBREG_REG (result)) == REG
10000       && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER)
10001     bitmap_set_bit (&subregs_of_mode, REGNO (SUBREG_REG (result))
10002                                       * MAX_MACHINE_MODE
10003                                       + GET_MODE (result));
10004 #endif
10005
10006   if (result)
10007     return result;
10008
10009   if (GET_CODE (x) == MEM)
10010     {
10011       int offset = 0;
10012
10013       /* Refuse to work on a volatile memory ref or one with a mode-dependent
10014          address.  */
10015       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10016         return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10017
10018       /* If we want to refer to something bigger than the original memref,
10019          generate a perverse subreg instead.  That will force a reload
10020          of the original memref X.  */
10021       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
10022         return gen_rtx_SUBREG (mode, x, 0);
10023
10024       if (WORDS_BIG_ENDIAN)
10025         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
10026                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
10027
10028       if (BYTES_BIG_ENDIAN)
10029         {
10030           /* Adjust the address so that the address-after-the-data is
10031              unchanged.  */
10032           offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
10033                      - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
10034         }
10035
10036       return adjust_address_nv (x, mode, offset);
10037     }
10038
10039   /* If X is a comparison operator, rewrite it in a new mode.  This
10040      probably won't match, but may allow further simplifications.  */
10041   else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
10042     return gen_rtx_fmt_ee (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
10043
10044   /* If we couldn't simplify X any other way, just enclose it in a
10045      SUBREG.  Normally, this SUBREG won't match, but some patterns may
10046      include an explicit SUBREG or we may simplify it further in combine.  */
10047   else
10048     {
10049       int offset = 0;
10050       rtx res;
10051       enum machine_mode sub_mode = GET_MODE (x);
10052
10053       offset = subreg_lowpart_offset (mode, sub_mode);
10054       if (sub_mode == VOIDmode)
10055         {
10056           sub_mode = int_mode_for_mode (mode);
10057           x = gen_lowpart_common (sub_mode, x);
10058           if (x == 0)
10059             return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
10060         }
10061       res = simplify_gen_subreg (mode, x, sub_mode, offset);
10062       if (res)
10063         return res;
10064       return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10065     }
10066 }
10067 \f
10068 /* These routines make binary and unary operations by first seeing if they
10069    fold; if not, a new expression is allocated.  */
10070
10071 static rtx
10072 gen_binary (enum rtx_code code, enum machine_mode mode, rtx op0, rtx op1)
10073 {
10074   rtx result;
10075   rtx tem;
10076
10077   if (GET_CODE (op0) == CLOBBER)
10078     return op0;
10079   else if (GET_CODE (op1) == CLOBBER)
10080     return op1;
10081   
10082   if (GET_RTX_CLASS (code) == 'c'
10083       && swap_commutative_operands_p (op0, op1))
10084     tem = op0, op0 = op1, op1 = tem;
10085
10086   if (GET_RTX_CLASS (code) == '<')
10087     {
10088       enum machine_mode op_mode = GET_MODE (op0);
10089
10090       /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
10091          just (REL_OP X Y).  */
10092       if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
10093         {
10094           op1 = XEXP (op0, 1);
10095           op0 = XEXP (op0, 0);
10096           op_mode = GET_MODE (op0);
10097         }
10098
10099       if (op_mode == VOIDmode)
10100         op_mode = GET_MODE (op1);
10101       result = simplify_relational_operation (code, op_mode, op0, op1);
10102     }
10103   else
10104     result = simplify_binary_operation (code, mode, op0, op1);
10105
10106   if (result)
10107     return result;
10108
10109   /* Put complex operands first and constants second.  */
10110   if (GET_RTX_CLASS (code) == 'c'
10111       && swap_commutative_operands_p (op0, op1))
10112     return gen_rtx_fmt_ee (code, mode, op1, op0);
10113
10114   /* If we are turning off bits already known off in OP0, we need not do
10115      an AND.  */
10116   else if (code == AND && GET_CODE (op1) == CONST_INT
10117            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10118            && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
10119     return op0;
10120
10121   return gen_rtx_fmt_ee (code, mode, op0, op1);
10122 }
10123 \f
10124 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10125    comparison code that will be tested.
10126
10127    The result is a possibly different comparison code to use.  *POP0 and
10128    *POP1 may be updated.
10129
10130    It is possible that we might detect that a comparison is either always
10131    true or always false.  However, we do not perform general constant
10132    folding in combine, so this knowledge isn't useful.  Such tautologies
10133    should have been detected earlier.  Hence we ignore all such cases.  */
10134
10135 static enum rtx_code
10136 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10137 {
10138   rtx op0 = *pop0;
10139   rtx op1 = *pop1;
10140   rtx tem, tem1;
10141   int i;
10142   enum machine_mode mode, tmode;
10143
10144   /* Try a few ways of applying the same transformation to both operands.  */
10145   while (1)
10146     {
10147 #ifndef WORD_REGISTER_OPERATIONS
10148       /* The test below this one won't handle SIGN_EXTENDs on these machines,
10149          so check specially.  */
10150       if (code != GTU && code != GEU && code != LTU && code != LEU
10151           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10152           && GET_CODE (XEXP (op0, 0)) == ASHIFT
10153           && GET_CODE (XEXP (op1, 0)) == ASHIFT
10154           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10155           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10156           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10157               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10158           && GET_CODE (XEXP (op0, 1)) == CONST_INT
10159           && XEXP (op0, 1) == XEXP (op1, 1)
10160           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10161           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10162           && (INTVAL (XEXP (op0, 1))
10163               == (GET_MODE_BITSIZE (GET_MODE (op0))
10164                   - (GET_MODE_BITSIZE
10165                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10166         {
10167           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10168           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10169         }
10170 #endif
10171
10172       /* If both operands are the same constant shift, see if we can ignore the
10173          shift.  We can if the shift is a rotate or if the bits shifted out of
10174          this shift are known to be zero for both inputs and if the type of
10175          comparison is compatible with the shift.  */
10176       if (GET_CODE (op0) == GET_CODE (op1)
10177           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10178           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10179               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10180                   && (code != GT && code != LT && code != GE && code != LE))
10181               || (GET_CODE (op0) == ASHIFTRT
10182                   && (code != GTU && code != LTU
10183                       && code != GEU && code != LEU)))
10184           && GET_CODE (XEXP (op0, 1)) == CONST_INT
10185           && INTVAL (XEXP (op0, 1)) >= 0
10186           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10187           && XEXP (op0, 1) == XEXP (op1, 1))
10188         {
10189           enum machine_mode mode = GET_MODE (op0);
10190           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10191           int shift_count = INTVAL (XEXP (op0, 1));
10192
10193           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10194             mask &= (mask >> shift_count) << shift_count;
10195           else if (GET_CODE (op0) == ASHIFT)
10196             mask = (mask & (mask << shift_count)) >> shift_count;
10197
10198           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10199               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10200             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10201           else
10202             break;
10203         }
10204
10205       /* If both operands are AND's of a paradoxical SUBREG by constant, the
10206          SUBREGs are of the same mode, and, in both cases, the AND would
10207          be redundant if the comparison was done in the narrower mode,
10208          do the comparison in the narrower mode (e.g., we are AND'ing with 1
10209          and the operand's possibly nonzero bits are 0xffffff01; in that case
10210          if we only care about QImode, we don't need the AND).  This case
10211          occurs if the output mode of an scc insn is not SImode and
10212          STORE_FLAG_VALUE == 1 (e.g., the 386).
10213
10214          Similarly, check for a case where the AND's are ZERO_EXTEND
10215          operations from some narrower mode even though a SUBREG is not
10216          present.  */
10217
10218       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10219                && GET_CODE (XEXP (op0, 1)) == CONST_INT
10220                && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10221         {
10222           rtx inner_op0 = XEXP (op0, 0);
10223           rtx inner_op1 = XEXP (op1, 0);
10224           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10225           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10226           int changed = 0;
10227
10228           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10229               && (GET_MODE_SIZE (GET_MODE (inner_op0))
10230                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10231               && (GET_MODE (SUBREG_REG (inner_op0))
10232                   == GET_MODE (SUBREG_REG (inner_op1)))
10233               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10234                   <= HOST_BITS_PER_WIDE_INT)
10235               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10236                                              GET_MODE (SUBREG_REG (inner_op0)))))
10237               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10238                                              GET_MODE (SUBREG_REG (inner_op1))))))
10239             {
10240               op0 = SUBREG_REG (inner_op0);
10241               op1 = SUBREG_REG (inner_op1);
10242
10243               /* The resulting comparison is always unsigned since we masked
10244                  off the original sign bit.  */
10245               code = unsigned_condition (code);
10246
10247               changed = 1;
10248             }
10249
10250           else if (c0 == c1)
10251             for (tmode = GET_CLASS_NARROWEST_MODE
10252                  (GET_MODE_CLASS (GET_MODE (op0)));
10253                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10254               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10255                 {
10256                   op0 = gen_lowpart_for_combine (tmode, inner_op0);
10257                   op1 = gen_lowpart_for_combine (tmode, inner_op1);
10258                   code = unsigned_condition (code);
10259                   changed = 1;
10260                   break;
10261                 }
10262
10263           if (! changed)
10264             break;
10265         }
10266
10267       /* If both operands are NOT, we can strip off the outer operation
10268          and adjust the comparison code for swapped operands; similarly for
10269          NEG, except that this must be an equality comparison.  */
10270       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10271                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10272                    && (code == EQ || code == NE)))
10273         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10274
10275       else
10276         break;
10277     }
10278
10279   /* If the first operand is a constant, swap the operands and adjust the
10280      comparison code appropriately, but don't do this if the second operand
10281      is already a constant integer.  */
10282   if (swap_commutative_operands_p (op0, op1))
10283     {
10284       tem = op0, op0 = op1, op1 = tem;
10285       code = swap_condition (code);
10286     }
10287
10288   /* We now enter a loop during which we will try to simplify the comparison.
10289      For the most part, we only are concerned with comparisons with zero,
10290      but some things may really be comparisons with zero but not start
10291      out looking that way.  */
10292
10293   while (GET_CODE (op1) == CONST_INT)
10294     {
10295       enum machine_mode mode = GET_MODE (op0);
10296       unsigned int mode_width = GET_MODE_BITSIZE (mode);
10297       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10298       int equality_comparison_p;
10299       int sign_bit_comparison_p;
10300       int unsigned_comparison_p;
10301       HOST_WIDE_INT const_op;
10302
10303       /* We only want to handle integral modes.  This catches VOIDmode,
10304          CCmode, and the floating-point modes.  An exception is that we
10305          can handle VOIDmode if OP0 is a COMPARE or a comparison
10306          operation.  */
10307
10308       if (GET_MODE_CLASS (mode) != MODE_INT
10309           && ! (mode == VOIDmode
10310                 && (GET_CODE (op0) == COMPARE
10311                     || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
10312         break;
10313
10314       /* Get the constant we are comparing against and turn off all bits
10315          not on in our mode.  */
10316       const_op = INTVAL (op1);
10317       if (mode != VOIDmode)
10318         const_op = trunc_int_for_mode (const_op, mode);
10319       op1 = GEN_INT (const_op);
10320
10321       /* If we are comparing against a constant power of two and the value
10322          being compared can only have that single bit nonzero (e.g., it was
10323          `and'ed with that bit), we can replace this with a comparison
10324          with zero.  */
10325       if (const_op
10326           && (code == EQ || code == NE || code == GE || code == GEU
10327               || code == LT || code == LTU)
10328           && mode_width <= HOST_BITS_PER_WIDE_INT
10329           && exact_log2 (const_op) >= 0
10330           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10331         {
10332           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10333           op1 = const0_rtx, const_op = 0;
10334         }
10335
10336       /* Similarly, if we are comparing a value known to be either -1 or
10337          0 with -1, change it to the opposite comparison against zero.  */
10338
10339       if (const_op == -1
10340           && (code == EQ || code == NE || code == GT || code == LE
10341               || code == GEU || code == LTU)
10342           && num_sign_bit_copies (op0, mode) == mode_width)
10343         {
10344           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10345           op1 = const0_rtx, const_op = 0;
10346         }
10347
10348       /* Do some canonicalizations based on the comparison code.  We prefer
10349          comparisons against zero and then prefer equality comparisons.
10350          If we can reduce the size of a constant, we will do that too.  */
10351
10352       switch (code)
10353         {
10354         case LT:
10355           /* < C is equivalent to <= (C - 1) */
10356           if (const_op > 0)
10357             {
10358               const_op -= 1;
10359               op1 = GEN_INT (const_op);
10360               code = LE;
10361               /* ... fall through to LE case below.  */
10362             }
10363           else
10364             break;
10365
10366         case LE:
10367           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
10368           if (const_op < 0)
10369             {
10370               const_op += 1;
10371               op1 = GEN_INT (const_op);
10372               code = LT;
10373             }
10374
10375           /* If we are doing a <= 0 comparison on a value known to have
10376              a zero sign bit, we can replace this with == 0.  */
10377           else if (const_op == 0
10378                    && mode_width <= HOST_BITS_PER_WIDE_INT
10379                    && (nonzero_bits (op0, mode)
10380                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10381             code = EQ;
10382           break;
10383
10384         case GE:
10385           /* >= C is equivalent to > (C - 1).  */
10386           if (const_op > 0)
10387             {
10388               const_op -= 1;
10389               op1 = GEN_INT (const_op);
10390               code = GT;
10391               /* ... fall through to GT below.  */
10392             }
10393           else
10394             break;
10395
10396         case GT:
10397           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
10398           if (const_op < 0)
10399             {
10400               const_op += 1;
10401               op1 = GEN_INT (const_op);
10402               code = GE;
10403             }
10404
10405           /* If we are doing a > 0 comparison on a value known to have
10406              a zero sign bit, we can replace this with != 0.  */
10407           else if (const_op == 0
10408                    && mode_width <= HOST_BITS_PER_WIDE_INT
10409                    && (nonzero_bits (op0, mode)
10410                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10411             code = NE;
10412           break;
10413
10414         case LTU:
10415           /* < C is equivalent to <= (C - 1).  */
10416           if (const_op > 0)
10417             {
10418               const_op -= 1;
10419               op1 = GEN_INT (const_op);
10420               code = LEU;
10421               /* ... fall through ...  */
10422             }
10423
10424           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
10425           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10426                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10427             {
10428               const_op = 0, op1 = const0_rtx;
10429               code = GE;
10430               break;
10431             }
10432           else
10433             break;
10434
10435         case LEU:
10436           /* unsigned <= 0 is equivalent to == 0 */
10437           if (const_op == 0)
10438             code = EQ;
10439
10440           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
10441           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10442                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10443             {
10444               const_op = 0, op1 = const0_rtx;
10445               code = GE;
10446             }
10447           break;
10448
10449         case GEU:
10450           /* >= C is equivalent to < (C - 1).  */
10451           if (const_op > 1)
10452             {
10453               const_op -= 1;
10454               op1 = GEN_INT (const_op);
10455               code = GTU;
10456               /* ... fall through ...  */
10457             }
10458
10459           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
10460           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10461                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10462             {
10463               const_op = 0, op1 = const0_rtx;
10464               code = LT;
10465               break;
10466             }
10467           else
10468             break;
10469
10470         case GTU:
10471           /* unsigned > 0 is equivalent to != 0 */
10472           if (const_op == 0)
10473             code = NE;
10474
10475           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
10476           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10477                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10478             {
10479               const_op = 0, op1 = const0_rtx;
10480               code = LT;
10481             }
10482           break;
10483
10484         default:
10485           break;
10486         }
10487
10488       /* Compute some predicates to simplify code below.  */
10489
10490       equality_comparison_p = (code == EQ || code == NE);
10491       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10492       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10493                                || code == GEU);
10494
10495       /* If this is a sign bit comparison and we can do arithmetic in
10496          MODE, say that we will only be needing the sign bit of OP0.  */
10497       if (sign_bit_comparison_p
10498           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10499         op0 = force_to_mode (op0, mode,
10500                              ((HOST_WIDE_INT) 1
10501                               << (GET_MODE_BITSIZE (mode) - 1)),
10502                              NULL_RTX, 0);
10503
10504       /* Now try cases based on the opcode of OP0.  If none of the cases
10505          does a "continue", we exit this loop immediately after the
10506          switch.  */
10507
10508       switch (GET_CODE (op0))
10509         {
10510         case ZERO_EXTRACT:
10511           /* If we are extracting a single bit from a variable position in
10512              a constant that has only a single bit set and are comparing it
10513              with zero, we can convert this into an equality comparison
10514              between the position and the location of the single bit.  */
10515
10516           if (GET_CODE (XEXP (op0, 0)) == CONST_INT
10517               && XEXP (op0, 1) == const1_rtx
10518               && equality_comparison_p && const_op == 0
10519               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10520             {
10521               if (BITS_BIG_ENDIAN)
10522                 {
10523                   enum machine_mode new_mode
10524                     = mode_for_extraction (EP_extzv, 1);
10525                   if (new_mode == MAX_MACHINE_MODE)
10526                     i = BITS_PER_WORD - 1 - i;
10527                   else
10528                     {
10529                       mode = new_mode;
10530                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
10531                     }
10532                 }
10533
10534               op0 = XEXP (op0, 2);
10535               op1 = GEN_INT (i);
10536               const_op = i;
10537
10538               /* Result is nonzero iff shift count is equal to I.  */
10539               code = reverse_condition (code);
10540               continue;
10541             }
10542
10543           /* ... fall through ...  */
10544
10545         case SIGN_EXTRACT:
10546           tem = expand_compound_operation (op0);
10547           if (tem != op0)
10548             {
10549               op0 = tem;
10550               continue;
10551             }
10552           break;
10553
10554         case NOT:
10555           /* If testing for equality, we can take the NOT of the constant.  */
10556           if (equality_comparison_p
10557               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10558             {
10559               op0 = XEXP (op0, 0);
10560               op1 = tem;
10561               continue;
10562             }
10563
10564           /* If just looking at the sign bit, reverse the sense of the
10565              comparison.  */
10566           if (sign_bit_comparison_p)
10567             {
10568               op0 = XEXP (op0, 0);
10569               code = (code == GE ? LT : GE);
10570               continue;
10571             }
10572           break;
10573
10574         case NEG:
10575           /* If testing for equality, we can take the NEG of the constant.  */
10576           if (equality_comparison_p
10577               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10578             {
10579               op0 = XEXP (op0, 0);
10580               op1 = tem;
10581               continue;
10582             }
10583
10584           /* The remaining cases only apply to comparisons with zero.  */
10585           if (const_op != 0)
10586             break;
10587
10588           /* When X is ABS or is known positive,
10589              (neg X) is < 0 if and only if X != 0.  */
10590
10591           if (sign_bit_comparison_p
10592               && (GET_CODE (XEXP (op0, 0)) == ABS
10593                   || (mode_width <= HOST_BITS_PER_WIDE_INT
10594                       && (nonzero_bits (XEXP (op0, 0), mode)
10595                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10596             {
10597               op0 = XEXP (op0, 0);
10598               code = (code == LT ? NE : EQ);
10599               continue;
10600             }
10601
10602           /* If we have NEG of something whose two high-order bits are the
10603              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
10604           if (num_sign_bit_copies (op0, mode) >= 2)
10605             {
10606               op0 = XEXP (op0, 0);
10607               code = swap_condition (code);
10608               continue;
10609             }
10610           break;
10611
10612         case ROTATE:
10613           /* If we are testing equality and our count is a constant, we
10614              can perform the inverse operation on our RHS.  */
10615           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10616               && (tem = simplify_binary_operation (ROTATERT, mode,
10617                                                    op1, XEXP (op0, 1))) != 0)
10618             {
10619               op0 = XEXP (op0, 0);
10620               op1 = tem;
10621               continue;
10622             }
10623
10624           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10625              a particular bit.  Convert it to an AND of a constant of that
10626              bit.  This will be converted into a ZERO_EXTRACT.  */
10627           if (const_op == 0 && sign_bit_comparison_p
10628               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10629               && mode_width <= HOST_BITS_PER_WIDE_INT)
10630             {
10631               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10632                                             ((HOST_WIDE_INT) 1
10633                                              << (mode_width - 1
10634                                                  - INTVAL (XEXP (op0, 1)))));
10635               code = (code == LT ? NE : EQ);
10636               continue;
10637             }
10638
10639           /* Fall through.  */
10640
10641         case ABS:
10642           /* ABS is ignorable inside an equality comparison with zero.  */
10643           if (const_op == 0 && equality_comparison_p)
10644             {
10645               op0 = XEXP (op0, 0);
10646               continue;
10647             }
10648           break;
10649
10650         case SIGN_EXTEND:
10651           /* Can simplify (compare (zero/sign_extend FOO) CONST)
10652              to (compare FOO CONST) if CONST fits in FOO's mode and we
10653              are either testing inequality or have an unsigned comparison
10654              with ZERO_EXTEND or a signed comparison with SIGN_EXTEND.  */
10655           if (! unsigned_comparison_p
10656               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10657                   <= HOST_BITS_PER_WIDE_INT)
10658               && ((unsigned HOST_WIDE_INT) const_op
10659                   < (((unsigned HOST_WIDE_INT) 1
10660                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10661             {
10662               op0 = XEXP (op0, 0);
10663               continue;
10664             }
10665           break;
10666
10667         case SUBREG:
10668           /* Check for the case where we are comparing A - C1 with C2,
10669              both constants are smaller than 1/2 the maximum positive
10670              value in MODE, and the comparison is equality or unsigned.
10671              In that case, if A is either zero-extended to MODE or has
10672              sufficient sign bits so that the high-order bit in MODE
10673              is a copy of the sign in the inner mode, we can prove that it is
10674              safe to do the operation in the wider mode.  This simplifies
10675              many range checks.  */
10676
10677           if (mode_width <= HOST_BITS_PER_WIDE_INT
10678               && subreg_lowpart_p (op0)
10679               && GET_CODE (SUBREG_REG (op0)) == PLUS
10680               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10681               && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10682               && (-INTVAL (XEXP (SUBREG_REG (op0), 1))
10683                   < (HOST_WIDE_INT) (GET_MODE_MASK (mode) / 2))
10684               && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10685               && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10686                                       GET_MODE (SUBREG_REG (op0)))
10687                         & ~GET_MODE_MASK (mode))
10688                   || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10689                                            GET_MODE (SUBREG_REG (op0)))
10690                       > (unsigned int)
10691                         (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10692                          - GET_MODE_BITSIZE (mode)))))
10693             {
10694               op0 = SUBREG_REG (op0);
10695               continue;
10696             }
10697
10698           /* If the inner mode is narrower and we are extracting the low part,
10699              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10700           if (subreg_lowpart_p (op0)
10701               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10702             /* Fall through */ ;
10703           else
10704             break;
10705
10706           /* ... fall through ...  */
10707
10708         case ZERO_EXTEND:
10709           if ((unsigned_comparison_p || equality_comparison_p)
10710               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10711                   <= HOST_BITS_PER_WIDE_INT)
10712               && ((unsigned HOST_WIDE_INT) const_op
10713                   < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10714             {
10715               op0 = XEXP (op0, 0);
10716               continue;
10717             }
10718           break;
10719
10720         case PLUS:
10721           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10722              this for equality comparisons due to pathological cases involving
10723              overflows.  */
10724           if (equality_comparison_p
10725               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10726                                                         op1, XEXP (op0, 1))))
10727             {
10728               op0 = XEXP (op0, 0);
10729               op1 = tem;
10730               continue;
10731             }
10732
10733           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10734           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10735               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10736             {
10737               op0 = XEXP (XEXP (op0, 0), 0);
10738               code = (code == LT ? EQ : NE);
10739               continue;
10740             }
10741           break;
10742
10743         case MINUS:
10744           /* We used to optimize signed comparisons against zero, but that
10745              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
10746              arrive here as equality comparisons, or (GEU, LTU) are
10747              optimized away.  No need to special-case them.  */
10748
10749           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10750              (eq B (minus A C)), whichever simplifies.  We can only do
10751              this for equality comparisons due to pathological cases involving
10752              overflows.  */
10753           if (equality_comparison_p
10754               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10755                                                         XEXP (op0, 1), op1)))
10756             {
10757               op0 = XEXP (op0, 0);
10758               op1 = tem;
10759               continue;
10760             }
10761
10762           if (equality_comparison_p
10763               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10764                                                         XEXP (op0, 0), op1)))
10765             {
10766               op0 = XEXP (op0, 1);
10767               op1 = tem;
10768               continue;
10769             }
10770
10771           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10772              of bits in X minus 1, is one iff X > 0.  */
10773           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10774               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10775               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10776                  == mode_width - 1
10777               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10778             {
10779               op0 = XEXP (op0, 1);
10780               code = (code == GE ? LE : GT);
10781               continue;
10782             }
10783           break;
10784
10785         case XOR:
10786           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10787              if C is zero or B is a constant.  */
10788           if (equality_comparison_p
10789               && 0 != (tem = simplify_binary_operation (XOR, mode,
10790                                                         XEXP (op0, 1), op1)))
10791             {
10792               op0 = XEXP (op0, 0);
10793               op1 = tem;
10794               continue;
10795             }
10796           break;
10797
10798         case EQ:  case NE:
10799         case UNEQ:  case LTGT:
10800         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
10801         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
10802         case UNORDERED: case ORDERED:
10803           /* We can't do anything if OP0 is a condition code value, rather
10804              than an actual data value.  */
10805           if (const_op != 0
10806               || CC0_P (XEXP (op0, 0))
10807               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10808             break;
10809
10810           /* Get the two operands being compared.  */
10811           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10812             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10813           else
10814             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10815
10816           /* Check for the cases where we simply want the result of the
10817              earlier test or the opposite of that result.  */
10818           if (code == NE || code == EQ
10819               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10820                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10821                   && (STORE_FLAG_VALUE
10822                       & (((HOST_WIDE_INT) 1
10823                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10824                   && (code == LT || code == GE)))
10825             {
10826               enum rtx_code new_code;
10827               if (code == LT || code == NE)
10828                 new_code = GET_CODE (op0);
10829               else
10830                 new_code = combine_reversed_comparison_code (op0);
10831
10832               if (new_code != UNKNOWN)
10833                 {
10834                   code = new_code;
10835                   op0 = tem;
10836                   op1 = tem1;
10837                   continue;
10838                 }
10839             }
10840           break;
10841
10842         case IOR:
10843           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10844              iff X <= 0.  */
10845           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10846               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10847               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10848             {
10849               op0 = XEXP (op0, 1);
10850               code = (code == GE ? GT : LE);
10851               continue;
10852             }
10853           break;
10854
10855         case AND:
10856           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10857              will be converted to a ZERO_EXTRACT later.  */
10858           if (const_op == 0 && equality_comparison_p
10859               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10860               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10861             {
10862               op0 = simplify_and_const_int
10863                 (op0, mode, gen_rtx_LSHIFTRT (mode,
10864                                               XEXP (op0, 1),
10865                                               XEXP (XEXP (op0, 0), 1)),
10866                  (HOST_WIDE_INT) 1);
10867               continue;
10868             }
10869
10870           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10871              zero and X is a comparison and C1 and C2 describe only bits set
10872              in STORE_FLAG_VALUE, we can compare with X.  */
10873           if (const_op == 0 && equality_comparison_p
10874               && mode_width <= HOST_BITS_PER_WIDE_INT
10875               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10876               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10877               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10878               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10879               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10880             {
10881               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10882                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10883               if ((~STORE_FLAG_VALUE & mask) == 0
10884                   && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10885                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10886                           && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10887                 {
10888                   op0 = XEXP (XEXP (op0, 0), 0);
10889                   continue;
10890                 }
10891             }
10892
10893           /* If we are doing an equality comparison of an AND of a bit equal
10894              to the sign bit, replace this with a LT or GE comparison of
10895              the underlying value.  */
10896           if (equality_comparison_p
10897               && const_op == 0
10898               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10899               && mode_width <= HOST_BITS_PER_WIDE_INT
10900               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10901                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10902             {
10903               op0 = XEXP (op0, 0);
10904               code = (code == EQ ? GE : LT);
10905               continue;
10906             }
10907
10908           /* If this AND operation is really a ZERO_EXTEND from a narrower
10909              mode, the constant fits within that mode, and this is either an
10910              equality or unsigned comparison, try to do this comparison in
10911              the narrower mode.  */
10912           if ((equality_comparison_p || unsigned_comparison_p)
10913               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10914               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10915                                    & GET_MODE_MASK (mode))
10916                                   + 1)) >= 0
10917               && const_op >> i == 0
10918               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10919             {
10920               op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
10921               continue;
10922             }
10923
10924           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10925              fits in both M1 and M2 and the SUBREG is either paradoxical
10926              or represents the low part, permute the SUBREG and the AND
10927              and try again.  */
10928           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10929             {
10930               unsigned HOST_WIDE_INT c1;
10931               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10932               /* Require an integral mode, to avoid creating something like
10933                  (AND:SF ...).  */
10934               if (SCALAR_INT_MODE_P (tmode)
10935                   /* It is unsafe to commute the AND into the SUBREG if the
10936                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10937                      not defined.  As originally written the upper bits
10938                      have a defined value due to the AND operation.
10939                      However, if we commute the AND inside the SUBREG then
10940                      they no longer have defined values and the meaning of
10941                      the code has been changed.  */
10942                   && (0
10943 #ifdef WORD_REGISTER_OPERATIONS
10944                       || (mode_width > GET_MODE_BITSIZE (tmode)
10945                           && mode_width <= BITS_PER_WORD)
10946 #endif
10947                       || (mode_width <= GET_MODE_BITSIZE (tmode)
10948                           && subreg_lowpart_p (XEXP (op0, 0))))
10949                   && GET_CODE (XEXP (op0, 1)) == CONST_INT
10950                   && mode_width <= HOST_BITS_PER_WIDE_INT
10951                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10952                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10953                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
10954                   && c1 != mask
10955                   && c1 != GET_MODE_MASK (tmode))
10956                 {
10957                   op0 = gen_binary (AND, tmode,
10958                                     SUBREG_REG (XEXP (op0, 0)),
10959                                     gen_int_mode (c1, tmode));
10960                   op0 = gen_lowpart_for_combine (mode, op0);
10961                   continue;
10962                 }
10963             }
10964
10965           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
10966           if (const_op == 0 && equality_comparison_p
10967               && XEXP (op0, 1) == const1_rtx
10968               && GET_CODE (XEXP (op0, 0)) == NOT)
10969             {
10970               op0 = simplify_and_const_int
10971                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10972               code = (code == NE ? EQ : NE);
10973               continue;
10974             }
10975
10976           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10977              (eq (and (lshiftrt X) 1) 0).
10978              Also handle the case where (not X) is expressed using xor.  */
10979           if (const_op == 0 && equality_comparison_p
10980               && XEXP (op0, 1) == const1_rtx
10981               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10982             {
10983               rtx shift_op = XEXP (XEXP (op0, 0), 0);
10984               rtx shift_count = XEXP (XEXP (op0, 0), 1);
10985
10986               if (GET_CODE (shift_op) == NOT
10987                   || (GET_CODE (shift_op) == XOR
10988                       && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10989                       && GET_CODE (shift_count) == CONST_INT
10990                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10991                       && (INTVAL (XEXP (shift_op, 1))
10992                           == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10993                 {
10994                   op0 = simplify_and_const_int
10995                     (NULL_RTX, mode,
10996                      gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10997                      (HOST_WIDE_INT) 1);
10998                   code = (code == NE ? EQ : NE);
10999                   continue;
11000                 }
11001             }
11002           break;
11003
11004         case ASHIFT:
11005           /* If we have (compare (ashift FOO N) (const_int C)) and
11006              the high order N bits of FOO (N+1 if an inequality comparison)
11007              are known to be zero, we can do this by comparing FOO with C
11008              shifted right N bits so long as the low-order N bits of C are
11009              zero.  */
11010           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11011               && INTVAL (XEXP (op0, 1)) >= 0
11012               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11013                   < HOST_BITS_PER_WIDE_INT)
11014               && ((const_op
11015                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
11016               && mode_width <= HOST_BITS_PER_WIDE_INT
11017               && (nonzero_bits (XEXP (op0, 0), mode)
11018                   & ~(mask >> (INTVAL (XEXP (op0, 1))
11019                                + ! equality_comparison_p))) == 0)
11020             {
11021               /* We must perform a logical shift, not an arithmetic one,
11022                  as we want the top N bits of C to be zero.  */
11023               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11024
11025               temp >>= INTVAL (XEXP (op0, 1));
11026               op1 = gen_int_mode (temp, mode);
11027               op0 = XEXP (op0, 0);
11028               continue;
11029             }
11030
11031           /* If we are doing a sign bit comparison, it means we are testing
11032              a particular bit.  Convert it to the appropriate AND.  */
11033           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
11034               && mode_width <= HOST_BITS_PER_WIDE_INT)
11035             {
11036               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11037                                             ((HOST_WIDE_INT) 1
11038                                              << (mode_width - 1
11039                                                  - INTVAL (XEXP (op0, 1)))));
11040               code = (code == LT ? NE : EQ);
11041               continue;
11042             }
11043
11044           /* If this an equality comparison with zero and we are shifting
11045              the low bit to the sign bit, we can convert this to an AND of the
11046              low-order bit.  */
11047           if (const_op == 0 && equality_comparison_p
11048               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11049               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11050                  == mode_width - 1)
11051             {
11052               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11053                                             (HOST_WIDE_INT) 1);
11054               continue;
11055             }
11056           break;
11057
11058         case ASHIFTRT:
11059           /* If this is an equality comparison with zero, we can do this
11060              as a logical shift, which might be much simpler.  */
11061           if (equality_comparison_p && const_op == 0
11062               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
11063             {
11064               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11065                                           XEXP (op0, 0),
11066                                           INTVAL (XEXP (op0, 1)));
11067               continue;
11068             }
11069
11070           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11071              do the comparison in a narrower mode.  */
11072           if (! unsigned_comparison_p
11073               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11074               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11075               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11076               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11077                                          MODE_INT, 1)) != BLKmode
11078               && (((unsigned HOST_WIDE_INT) const_op
11079                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11080                   <= GET_MODE_MASK (tmode)))
11081             {
11082               op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
11083               continue;
11084             }
11085
11086           /* Likewise if OP0 is a PLUS of a sign extension with a
11087              constant, which is usually represented with the PLUS
11088              between the shifts.  */
11089           if (! unsigned_comparison_p
11090               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11091               && GET_CODE (XEXP (op0, 0)) == PLUS
11092               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
11093               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11094               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11095               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11096                                          MODE_INT, 1)) != BLKmode
11097               && (((unsigned HOST_WIDE_INT) const_op
11098                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11099                   <= GET_MODE_MASK (tmode)))
11100             {
11101               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11102               rtx add_const = XEXP (XEXP (op0, 0), 1);
11103               rtx new_const = gen_binary (ASHIFTRT, GET_MODE (op0), add_const,
11104                                           XEXP (op0, 1));
11105
11106               op0 = gen_binary (PLUS, tmode,
11107                                 gen_lowpart_for_combine (tmode, inner),
11108                                 new_const);
11109               continue;
11110             }
11111
11112           /* ... fall through ...  */
11113         case LSHIFTRT:
11114           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11115              the low order N bits of FOO are known to be zero, we can do this
11116              by comparing FOO with C shifted left N bits so long as no
11117              overflow occurs.  */
11118           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11119               && INTVAL (XEXP (op0, 1)) >= 0
11120               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11121               && mode_width <= HOST_BITS_PER_WIDE_INT
11122               && (nonzero_bits (XEXP (op0, 0), mode)
11123                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11124               && (((unsigned HOST_WIDE_INT) const_op
11125                    + (GET_CODE (op0) != LSHIFTRT
11126                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11127                          + 1)
11128                       : 0))
11129                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11130             {
11131               /* If the shift was logical, then we must make the condition
11132                  unsigned.  */
11133               if (GET_CODE (op0) == LSHIFTRT)
11134                 code = unsigned_condition (code);
11135
11136               const_op <<= INTVAL (XEXP (op0, 1));
11137               op1 = GEN_INT (const_op);
11138               op0 = XEXP (op0, 0);
11139               continue;
11140             }
11141
11142           /* If we are using this shift to extract just the sign bit, we
11143              can replace this with an LT or GE comparison.  */
11144           if (const_op == 0
11145               && (equality_comparison_p || sign_bit_comparison_p)
11146               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11147               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11148                  == mode_width - 1)
11149             {
11150               op0 = XEXP (op0, 0);
11151               code = (code == NE || code == GT ? LT : GE);
11152               continue;
11153             }
11154           break;
11155
11156         default:
11157           break;
11158         }
11159
11160       break;
11161     }
11162
11163   /* Now make any compound operations involved in this comparison.  Then,
11164      check for an outmost SUBREG on OP0 that is not doing anything or is
11165      paradoxical.  The latter transformation must only be performed when
11166      it is known that the "extra" bits will be the same in op0 and op1 or
11167      that they don't matter.  There are three cases to consider:
11168
11169      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
11170      care bits and we can assume they have any convenient value.  So
11171      making the transformation is safe.
11172
11173      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11174      In this case the upper bits of op0 are undefined.  We should not make
11175      the simplification in that case as we do not know the contents of
11176      those bits.
11177
11178      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11179      NIL.  In that case we know those bits are zeros or ones.  We must
11180      also be sure that they are the same as the upper bits of op1.
11181
11182      We can never remove a SUBREG for a non-equality comparison because
11183      the sign bit is in a different place in the underlying object.  */
11184
11185   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11186   op1 = make_compound_operation (op1, SET);
11187
11188   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11189       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11190       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11191       && (code == NE || code == EQ))
11192     {
11193       if (GET_MODE_SIZE (GET_MODE (op0))
11194           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11195         {
11196           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
11197              implemented.  */
11198           if (GET_CODE (SUBREG_REG (op0)) == REG)
11199             {
11200               op0 = SUBREG_REG (op0);
11201               op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
11202             }
11203         }
11204       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11205                 <= HOST_BITS_PER_WIDE_INT)
11206                && (nonzero_bits (SUBREG_REG (op0),
11207                                  GET_MODE (SUBREG_REG (op0)))
11208                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11209         {
11210           tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)), op1);
11211
11212           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11213                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11214             op0 = SUBREG_REG (op0), op1 = tem;
11215         }
11216     }
11217
11218   /* We now do the opposite procedure: Some machines don't have compare
11219      insns in all modes.  If OP0's mode is an integer mode smaller than a
11220      word and we can't do a compare in that mode, see if there is a larger
11221      mode for which we can do the compare.  There are a number of cases in
11222      which we can use the wider mode.  */
11223
11224   mode = GET_MODE (op0);
11225   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11226       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11227       && ! have_insn_for (COMPARE, mode))
11228     for (tmode = GET_MODE_WIDER_MODE (mode);
11229          (tmode != VOIDmode
11230           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11231          tmode = GET_MODE_WIDER_MODE (tmode))
11232       if (have_insn_for (COMPARE, tmode))
11233         {
11234           int zero_extended;
11235
11236           /* If the only nonzero bits in OP0 and OP1 are those in the
11237              narrower mode and this is an equality or unsigned comparison,
11238              we can use the wider mode.  Similarly for sign-extended
11239              values, in which case it is true for all comparisons.  */
11240           zero_extended = ((code == EQ || code == NE
11241                             || code == GEU || code == GTU
11242                             || code == LEU || code == LTU)
11243                            && (nonzero_bits (op0, tmode)
11244                                & ~GET_MODE_MASK (mode)) == 0
11245                            && ((GET_CODE (op1) == CONST_INT
11246                                 || (nonzero_bits (op1, tmode)
11247                                     & ~GET_MODE_MASK (mode)) == 0)));
11248
11249           if (zero_extended
11250               || ((num_sign_bit_copies (op0, tmode)
11251                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
11252                                      - GET_MODE_BITSIZE (mode)))
11253                   && (num_sign_bit_copies (op1, tmode)
11254                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
11255                                         - GET_MODE_BITSIZE (mode)))))
11256             {
11257               /* If OP0 is an AND and we don't have an AND in MODE either,
11258                  make a new AND in the proper mode.  */
11259               if (GET_CODE (op0) == AND
11260                   && !have_insn_for (AND, mode))
11261                 op0 = gen_binary (AND, tmode,
11262                                   gen_lowpart_for_combine (tmode,
11263                                                            XEXP (op0, 0)),
11264                                   gen_lowpart_for_combine (tmode,
11265                                                            XEXP (op0, 1)));
11266
11267               op0 = gen_lowpart_for_combine (tmode, op0);
11268               if (zero_extended && GET_CODE (op1) == CONST_INT)
11269                 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11270               op1 = gen_lowpart_for_combine (tmode, op1);
11271               break;
11272             }
11273
11274           /* If this is a test for negative, we can make an explicit
11275              test of the sign bit.  */
11276
11277           if (op1 == const0_rtx && (code == LT || code == GE)
11278               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11279             {
11280               op0 = gen_binary (AND, tmode,
11281                                 gen_lowpart_for_combine (tmode, op0),
11282                                 GEN_INT ((HOST_WIDE_INT) 1
11283                                          << (GET_MODE_BITSIZE (mode) - 1)));
11284               code = (code == LT) ? NE : EQ;
11285               break;
11286             }
11287         }
11288
11289 #ifdef CANONICALIZE_COMPARISON
11290   /* If this machine only supports a subset of valid comparisons, see if we
11291      can convert an unsupported one into a supported one.  */
11292   CANONICALIZE_COMPARISON (code, op0, op1);
11293 #endif
11294
11295   *pop0 = op0;
11296   *pop1 = op1;
11297
11298   return code;
11299 }
11300 \f
11301 /* Like jump.c' reversed_comparison_code, but use combine infrastructure for
11302    searching backward.  */
11303 static enum rtx_code
11304 combine_reversed_comparison_code (rtx exp)
11305 {
11306   enum rtx_code code1 = reversed_comparison_code (exp, NULL);
11307   rtx x;
11308
11309   if (code1 != UNKNOWN
11310       || GET_MODE_CLASS (GET_MODE (XEXP (exp, 0))) != MODE_CC)
11311     return code1;
11312   /* Otherwise try and find where the condition codes were last set and
11313      use that.  */
11314   x = get_last_value (XEXP (exp, 0));
11315   if (!x || GET_CODE (x) != COMPARE)
11316     return UNKNOWN;
11317   return reversed_comparison_code_parts (GET_CODE (exp),
11318                                          XEXP (x, 0), XEXP (x, 1), NULL);
11319 }
11320
11321 /* Return comparison with reversed code of EXP and operands OP0 and OP1.
11322    Return NULL_RTX in case we fail to do the reversal.  */
11323 static rtx
11324 reversed_comparison (rtx exp, enum machine_mode mode, rtx op0, rtx op1)
11325 {
11326   enum rtx_code reversed_code = combine_reversed_comparison_code (exp);
11327   if (reversed_code == UNKNOWN)
11328     return NULL_RTX;
11329   else
11330     return gen_binary (reversed_code, mode, op0, op1);
11331 }
11332 \f
11333 /* Utility function for following routine.  Called when X is part of a value
11334    being stored into reg_last_set_value.  Sets reg_last_set_table_tick
11335    for each register mentioned.  Similar to mention_regs in cse.c  */
11336
11337 static void
11338 update_table_tick (rtx x)
11339 {
11340   enum rtx_code code = GET_CODE (x);
11341   const char *fmt = GET_RTX_FORMAT (code);
11342   int i;
11343
11344   if (code == REG)
11345     {
11346       unsigned int regno = REGNO (x);
11347       unsigned int endregno
11348         = regno + (regno < FIRST_PSEUDO_REGISTER
11349                    ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11350       unsigned int r;
11351
11352       for (r = regno; r < endregno; r++)
11353         reg_last_set_table_tick[r] = label_tick;
11354
11355       return;
11356     }
11357
11358   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11359     /* Note that we can't have an "E" in values stored; see
11360        get_last_value_validate.  */
11361     if (fmt[i] == 'e')
11362       {
11363         /* Check for identical subexpressions.  If x contains
11364            identical subexpression we only have to traverse one of
11365            them.  */
11366         if (i == 0
11367             && (GET_RTX_CLASS (code) == '2'
11368                 || GET_RTX_CLASS (code) == 'c'))
11369           {
11370             /* Note that at this point x1 has already been
11371                processed.  */
11372             rtx x0 = XEXP (x, 0);
11373             rtx x1 = XEXP (x, 1);
11374
11375             /* If x0 and x1 are identical then there is no need to
11376                process x0.  */
11377             if (x0 == x1)
11378               break;
11379
11380             /* If x0 is identical to a subexpression of x1 then while
11381                processing x1, x0 has already been processed.  Thus we
11382                are done with x.  */
11383             if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11384                  || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11385                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11386               break;
11387
11388             /* If x1 is identical to a subexpression of x0 then we
11389                still have to process the rest of x0.  */
11390             if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11391                  || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11392                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11393               {
11394                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11395                 break;
11396               }
11397           }
11398
11399         update_table_tick (XEXP (x, i));
11400       }
11401 }
11402
11403 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
11404    are saying that the register is clobbered and we no longer know its
11405    value.  If INSN is zero, don't update reg_last_set; this is only permitted
11406    with VALUE also zero and is used to invalidate the register.  */
11407
11408 static void
11409 record_value_for_reg (rtx reg, rtx insn, rtx value)
11410 {
11411   unsigned int regno = REGNO (reg);
11412   unsigned int endregno
11413     = regno + (regno < FIRST_PSEUDO_REGISTER
11414                ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
11415   unsigned int i;
11416
11417   /* If VALUE contains REG and we have a previous value for REG, substitute
11418      the previous value.  */
11419   if (value && insn && reg_overlap_mentioned_p (reg, value))
11420     {
11421       rtx tem;
11422
11423       /* Set things up so get_last_value is allowed to see anything set up to
11424          our insn.  */
11425       subst_low_cuid = INSN_CUID (insn);
11426       tem = get_last_value (reg);
11427
11428       /* If TEM is simply a binary operation with two CLOBBERs as operands,
11429          it isn't going to be useful and will take a lot of time to process,
11430          so just use the CLOBBER.  */
11431
11432       if (tem)
11433         {
11434           if ((GET_RTX_CLASS (GET_CODE (tem)) == '2'
11435                || GET_RTX_CLASS (GET_CODE (tem)) == 'c')
11436               && GET_CODE (XEXP (tem, 0)) == CLOBBER
11437               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11438             tem = XEXP (tem, 0);
11439
11440           value = replace_rtx (copy_rtx (value), reg, tem);
11441         }
11442     }
11443
11444   /* For each register modified, show we don't know its value, that
11445      we don't know about its bitwise content, that its value has been
11446      updated, and that we don't know the location of the death of the
11447      register.  */
11448   for (i = regno; i < endregno; i++)
11449     {
11450       if (insn)
11451         reg_last_set[i] = insn;
11452
11453       reg_last_set_value[i] = 0;
11454       reg_last_set_mode[i] = 0;
11455       reg_last_set_nonzero_bits[i] = 0;
11456       reg_last_set_sign_bit_copies[i] = 0;
11457       reg_last_death[i] = 0;
11458     }
11459
11460   /* Mark registers that are being referenced in this value.  */
11461   if (value)
11462     update_table_tick (value);
11463
11464   /* Now update the status of each register being set.
11465      If someone is using this register in this block, set this register
11466      to invalid since we will get confused between the two lives in this
11467      basic block.  This makes using this register always invalid.  In cse, we
11468      scan the table to invalidate all entries using this register, but this
11469      is too much work for us.  */
11470
11471   for (i = regno; i < endregno; i++)
11472     {
11473       reg_last_set_label[i] = label_tick;
11474       if (value && reg_last_set_table_tick[i] == label_tick)
11475         reg_last_set_invalid[i] = 1;
11476       else
11477         reg_last_set_invalid[i] = 0;
11478     }
11479
11480   /* The value being assigned might refer to X (like in "x++;").  In that
11481      case, we must replace it with (clobber (const_int 0)) to prevent
11482      infinite loops.  */
11483   if (value && ! get_last_value_validate (&value, insn,
11484                                           reg_last_set_label[regno], 0))
11485     {
11486       value = copy_rtx (value);
11487       if (! get_last_value_validate (&value, insn,
11488                                      reg_last_set_label[regno], 1))
11489         value = 0;
11490     }
11491
11492   /* For the main register being modified, update the value, the mode, the
11493      nonzero bits, and the number of sign bit copies.  */
11494
11495   reg_last_set_value[regno] = value;
11496
11497   if (value)
11498     {
11499       enum machine_mode mode = GET_MODE (reg);
11500       subst_low_cuid = INSN_CUID (insn);
11501       reg_last_set_mode[regno] = mode;
11502       if (GET_MODE_CLASS (mode) == MODE_INT
11503           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11504         mode = nonzero_bits_mode;
11505       reg_last_set_nonzero_bits[regno] = nonzero_bits (value, mode);
11506       reg_last_set_sign_bit_copies[regno]
11507         = num_sign_bit_copies (value, GET_MODE (reg));
11508     }
11509 }
11510
11511 /* Called via note_stores from record_dead_and_set_regs to handle one
11512    SET or CLOBBER in an insn.  DATA is the instruction in which the
11513    set is occurring.  */
11514
11515 static void
11516 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
11517 {
11518   rtx record_dead_insn = (rtx) data;
11519
11520   if (GET_CODE (dest) == SUBREG)
11521     dest = SUBREG_REG (dest);
11522
11523   if (GET_CODE (dest) == REG)
11524     {
11525       /* If we are setting the whole register, we know its value.  Otherwise
11526          show that we don't know the value.  We can handle SUBREG in
11527          some cases.  */
11528       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11529         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11530       else if (GET_CODE (setter) == SET
11531                && GET_CODE (SET_DEST (setter)) == SUBREG
11532                && SUBREG_REG (SET_DEST (setter)) == dest
11533                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11534                && subreg_lowpart_p (SET_DEST (setter)))
11535         record_value_for_reg (dest, record_dead_insn,
11536                               gen_lowpart_for_combine (GET_MODE (dest),
11537                                                        SET_SRC (setter)));
11538       else
11539         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11540     }
11541   else if (GET_CODE (dest) == MEM
11542            /* Ignore pushes, they clobber nothing.  */
11543            && ! push_operand (dest, GET_MODE (dest)))
11544     mem_last_set = INSN_CUID (record_dead_insn);
11545 }
11546
11547 /* Update the records of when each REG was most recently set or killed
11548    for the things done by INSN.  This is the last thing done in processing
11549    INSN in the combiner loop.
11550
11551    We update reg_last_set, reg_last_set_value, reg_last_set_mode,
11552    reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
11553    and also the similar information mem_last_set (which insn most recently
11554    modified memory) and last_call_cuid (which insn was the most recent
11555    subroutine call).  */
11556
11557 static void
11558 record_dead_and_set_regs (rtx insn)
11559 {
11560   rtx link;
11561   unsigned int i;
11562
11563   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11564     {
11565       if (REG_NOTE_KIND (link) == REG_DEAD
11566           && GET_CODE (XEXP (link, 0)) == REG)
11567         {
11568           unsigned int regno = REGNO (XEXP (link, 0));
11569           unsigned int endregno
11570             = regno + (regno < FIRST_PSEUDO_REGISTER
11571                        ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
11572                        : 1);
11573
11574           for (i = regno; i < endregno; i++)
11575             reg_last_death[i] = insn;
11576         }
11577       else if (REG_NOTE_KIND (link) == REG_INC)
11578         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11579     }
11580
11581   if (GET_CODE (insn) == CALL_INSN)
11582     {
11583       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11584         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11585           {
11586             reg_last_set_value[i] = 0;
11587             reg_last_set_mode[i] = 0;
11588             reg_last_set_nonzero_bits[i] = 0;
11589             reg_last_set_sign_bit_copies[i] = 0;
11590             reg_last_death[i] = 0;
11591           }
11592
11593       last_call_cuid = mem_last_set = INSN_CUID (insn);
11594
11595       /* Don't bother recording what this insn does.  It might set the
11596          return value register, but we can't combine into a call
11597          pattern anyway, so there's no point trying (and it may cause
11598          a crash, if e.g. we wind up asking for last_set_value of a
11599          SUBREG of the return value register).  */
11600       return;
11601     }
11602
11603   note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11604 }
11605
11606 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11607    register present in the SUBREG, so for each such SUBREG go back and
11608    adjust nonzero and sign bit information of the registers that are
11609    known to have some zero/sign bits set.
11610
11611    This is needed because when combine blows the SUBREGs away, the
11612    information on zero/sign bits is lost and further combines can be
11613    missed because of that.  */
11614
11615 static void
11616 record_promoted_value (rtx insn, rtx subreg)
11617 {
11618   rtx links, set;
11619   unsigned int regno = REGNO (SUBREG_REG (subreg));
11620   enum machine_mode mode = GET_MODE (subreg);
11621
11622   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11623     return;
11624
11625   for (links = LOG_LINKS (insn); links;)
11626     {
11627       insn = XEXP (links, 0);
11628       set = single_set (insn);
11629
11630       if (! set || GET_CODE (SET_DEST (set)) != REG
11631           || REGNO (SET_DEST (set)) != regno
11632           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11633         {
11634           links = XEXP (links, 1);
11635           continue;
11636         }
11637
11638       if (reg_last_set[regno] == insn)
11639         {
11640           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11641             reg_last_set_nonzero_bits[regno] &= GET_MODE_MASK (mode);
11642         }
11643
11644       if (GET_CODE (SET_SRC (set)) == REG)
11645         {
11646           regno = REGNO (SET_SRC (set));
11647           links = LOG_LINKS (insn);
11648         }
11649       else
11650         break;
11651     }
11652 }
11653
11654 /* Scan X for promoted SUBREGs.  For each one found,
11655    note what it implies to the registers used in it.  */
11656
11657 static void
11658 check_promoted_subreg (rtx insn, rtx x)
11659 {
11660   if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11661       && GET_CODE (SUBREG_REG (x)) == REG)
11662     record_promoted_value (insn, x);
11663   else
11664     {
11665       const char *format = GET_RTX_FORMAT (GET_CODE (x));
11666       int i, j;
11667
11668       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11669         switch (format[i])
11670           {
11671           case 'e':
11672             check_promoted_subreg (insn, XEXP (x, i));
11673             break;
11674           case 'V':
11675           case 'E':
11676             if (XVEC (x, i) != 0)
11677               for (j = 0; j < XVECLEN (x, i); j++)
11678                 check_promoted_subreg (insn, XVECEXP (x, i, j));
11679             break;
11680           }
11681     }
11682 }
11683 \f
11684 /* Utility routine for the following function.  Verify that all the registers
11685    mentioned in *LOC are valid when *LOC was part of a value set when
11686    label_tick == TICK.  Return 0 if some are not.
11687
11688    If REPLACE is nonzero, replace the invalid reference with
11689    (clobber (const_int 0)) and return 1.  This replacement is useful because
11690    we often can get useful information about the form of a value (e.g., if
11691    it was produced by a shift that always produces -1 or 0) even though
11692    we don't know exactly what registers it was produced from.  */
11693
11694 static int
11695 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11696 {
11697   rtx x = *loc;
11698   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11699   int len = GET_RTX_LENGTH (GET_CODE (x));
11700   int i;
11701
11702   if (GET_CODE (x) == REG)
11703     {
11704       unsigned int regno = REGNO (x);
11705       unsigned int endregno
11706         = regno + (regno < FIRST_PSEUDO_REGISTER
11707                    ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11708       unsigned int j;
11709
11710       for (j = regno; j < endregno; j++)
11711         if (reg_last_set_invalid[j]
11712             /* If this is a pseudo-register that was only set once and not
11713                live at the beginning of the function, it is always valid.  */
11714             || (! (regno >= FIRST_PSEUDO_REGISTER
11715                    && REG_N_SETS (regno) == 1
11716                    && (! REGNO_REG_SET_P
11717                        (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
11718                 && reg_last_set_label[j] > tick))
11719           {
11720             if (replace)
11721               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11722             return replace;
11723           }
11724
11725       return 1;
11726     }
11727   /* If this is a memory reference, make sure that there were
11728      no stores after it that might have clobbered the value.  We don't
11729      have alias info, so we assume any store invalidates it.  */
11730   else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
11731            && INSN_CUID (insn) <= mem_last_set)
11732     {
11733       if (replace)
11734         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11735       return replace;
11736     }
11737
11738   for (i = 0; i < len; i++)
11739     {
11740       if (fmt[i] == 'e')
11741         {
11742           /* Check for identical subexpressions.  If x contains
11743              identical subexpression we only have to traverse one of
11744              them.  */
11745           if (i == 1
11746               && (GET_RTX_CLASS (GET_CODE (x)) == '2'
11747                   || GET_RTX_CLASS (GET_CODE (x)) == 'c'))
11748             {
11749               /* Note that at this point x0 has already been checked
11750                  and found valid.  */
11751               rtx x0 = XEXP (x, 0);
11752               rtx x1 = XEXP (x, 1);
11753
11754               /* If x0 and x1 are identical then x is also valid.  */
11755               if (x0 == x1)
11756                 return 1;
11757
11758               /* If x1 is identical to a subexpression of x0 then
11759                  while checking x0, x1 has already been checked.  Thus
11760                  it is valid and so as x.  */
11761               if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11762                    || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11763                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11764                 return 1;
11765
11766               /* If x0 is identical to a subexpression of x1 then x is
11767                  valid iff the rest of x1 is valid.  */
11768               if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11769                    || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11770                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11771                 return
11772                   get_last_value_validate (&XEXP (x1,
11773                                                   x0 == XEXP (x1, 0) ? 1 : 0),
11774                                            insn, tick, replace);
11775             }
11776
11777           if (get_last_value_validate (&XEXP (x, i), insn, tick,
11778                                        replace) == 0)
11779             return 0;
11780         }
11781       /* Don't bother with these.  They shouldn't occur anyway.  */
11782       else if (fmt[i] == 'E')
11783         return 0;
11784     }
11785
11786   /* If we haven't found a reason for it to be invalid, it is valid.  */
11787   return 1;
11788 }
11789
11790 /* Get the last value assigned to X, if known.  Some registers
11791    in the value may be replaced with (clobber (const_int 0)) if their value
11792    is known longer known reliably.  */
11793
11794 static rtx
11795 get_last_value (rtx x)
11796 {
11797   unsigned int regno;
11798   rtx value;
11799
11800   /* If this is a non-paradoxical SUBREG, get the value of its operand and
11801      then convert it to the desired mode.  If this is a paradoxical SUBREG,
11802      we cannot predict what values the "extra" bits might have.  */
11803   if (GET_CODE (x) == SUBREG
11804       && subreg_lowpart_p (x)
11805       && (GET_MODE_SIZE (GET_MODE (x))
11806           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11807       && (value = get_last_value (SUBREG_REG (x))) != 0)
11808     return gen_lowpart_for_combine (GET_MODE (x), value);
11809
11810   if (GET_CODE (x) != REG)
11811     return 0;
11812
11813   regno = REGNO (x);
11814   value = reg_last_set_value[regno];
11815
11816   /* If we don't have a value, or if it isn't for this basic block and
11817      it's either a hard register, set more than once, or it's a live
11818      at the beginning of the function, return 0.
11819
11820      Because if it's not live at the beginning of the function then the reg
11821      is always set before being used (is never used without being set).
11822      And, if it's set only once, and it's always set before use, then all
11823      uses must have the same last value, even if it's not from this basic
11824      block.  */
11825
11826   if (value == 0
11827       || (reg_last_set_label[regno] != label_tick
11828           && (regno < FIRST_PSEUDO_REGISTER
11829               || REG_N_SETS (regno) != 1
11830               || (REGNO_REG_SET_P
11831                   (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
11832     return 0;
11833
11834   /* If the value was set in a later insn than the ones we are processing,
11835      we can't use it even if the register was only set once.  */
11836   if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
11837     return 0;
11838
11839   /* If the value has all its registers valid, return it.  */
11840   if (get_last_value_validate (&value, reg_last_set[regno],
11841                                reg_last_set_label[regno], 0))
11842     return value;
11843
11844   /* Otherwise, make a copy and replace any invalid register with
11845      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
11846
11847   value = copy_rtx (value);
11848   if (get_last_value_validate (&value, reg_last_set[regno],
11849                                reg_last_set_label[regno], 1))
11850     return value;
11851
11852   return 0;
11853 }
11854 \f
11855 /* Return nonzero if expression X refers to a REG or to memory
11856    that is set in an instruction more recent than FROM_CUID.  */
11857
11858 static int
11859 use_crosses_set_p (rtx x, int from_cuid)
11860 {
11861   const char *fmt;
11862   int i;
11863   enum rtx_code code = GET_CODE (x);
11864
11865   if (code == REG)
11866     {
11867       unsigned int regno = REGNO (x);
11868       unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11869                                  ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11870
11871 #ifdef PUSH_ROUNDING
11872       /* Don't allow uses of the stack pointer to be moved,
11873          because we don't know whether the move crosses a push insn.  */
11874       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11875         return 1;
11876 #endif
11877       for (; regno < endreg; regno++)
11878         if (reg_last_set[regno]
11879             && INSN_CUID (reg_last_set[regno]) > from_cuid)
11880           return 1;
11881       return 0;
11882     }
11883
11884   if (code == MEM && mem_last_set > from_cuid)
11885     return 1;
11886
11887   fmt = GET_RTX_FORMAT (code);
11888
11889   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11890     {
11891       if (fmt[i] == 'E')
11892         {
11893           int j;
11894           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11895             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11896               return 1;
11897         }
11898       else if (fmt[i] == 'e'
11899                && use_crosses_set_p (XEXP (x, i), from_cuid))
11900         return 1;
11901     }
11902   return 0;
11903 }
11904 \f
11905 /* Define three variables used for communication between the following
11906    routines.  */
11907
11908 static unsigned int reg_dead_regno, reg_dead_endregno;
11909 static int reg_dead_flag;
11910
11911 /* Function called via note_stores from reg_dead_at_p.
11912
11913    If DEST is within [reg_dead_regno, reg_dead_endregno), set
11914    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
11915
11916 static void
11917 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11918 {
11919   unsigned int regno, endregno;
11920
11921   if (GET_CODE (dest) != REG)
11922     return;
11923
11924   regno = REGNO (dest);
11925   endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11926                       ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
11927
11928   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11929     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11930 }
11931
11932 /* Return nonzero if REG is known to be dead at INSN.
11933
11934    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
11935    referencing REG, it is dead.  If we hit a SET referencing REG, it is
11936    live.  Otherwise, see if it is live or dead at the start of the basic
11937    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
11938    must be assumed to be always live.  */
11939
11940 static int
11941 reg_dead_at_p (rtx reg, rtx insn)
11942 {
11943   basic_block block;
11944   unsigned int i;
11945
11946   /* Set variables for reg_dead_at_p_1.  */
11947   reg_dead_regno = REGNO (reg);
11948   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11949                                         ? HARD_REGNO_NREGS (reg_dead_regno,
11950                                                             GET_MODE (reg))
11951                                         : 1);
11952
11953   reg_dead_flag = 0;
11954
11955   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  */
11956   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11957     {
11958       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11959         if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11960           return 0;
11961     }
11962
11963   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11964      beginning of function.  */
11965   for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
11966        insn = prev_nonnote_insn (insn))
11967     {
11968       note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11969       if (reg_dead_flag)
11970         return reg_dead_flag == 1 ? 1 : 0;
11971
11972       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11973         return 1;
11974     }
11975
11976   /* Get the basic block that we were in.  */
11977   if (insn == 0)
11978     block = ENTRY_BLOCK_PTR->next_bb;
11979   else
11980     {
11981       FOR_EACH_BB (block)
11982         if (insn == BB_HEAD (block))
11983           break;
11984
11985       if (block == EXIT_BLOCK_PTR)
11986         return 0;
11987     }
11988
11989   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11990     if (REGNO_REG_SET_P (block->global_live_at_start, i))
11991       return 0;
11992
11993   return 1;
11994 }
11995 \f
11996 /* Note hard registers in X that are used.  This code is similar to
11997    that in flow.c, but much simpler since we don't care about pseudos.  */
11998
11999 static void
12000 mark_used_regs_combine (rtx x)
12001 {
12002   RTX_CODE code = GET_CODE (x);
12003   unsigned int regno;
12004   int i;
12005
12006   switch (code)
12007     {
12008     case LABEL_REF:
12009     case SYMBOL_REF:
12010     case CONST_INT:
12011     case CONST:
12012     case CONST_DOUBLE:
12013     case CONST_VECTOR:
12014     case PC:
12015     case ADDR_VEC:
12016     case ADDR_DIFF_VEC:
12017     case ASM_INPUT:
12018 #ifdef HAVE_cc0
12019     /* CC0 must die in the insn after it is set, so we don't need to take
12020        special note of it here.  */
12021     case CC0:
12022 #endif
12023       return;
12024
12025     case CLOBBER:
12026       /* If we are clobbering a MEM, mark any hard registers inside the
12027          address as used.  */
12028       if (GET_CODE (XEXP (x, 0)) == MEM)
12029         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12030       return;
12031
12032     case REG:
12033       regno = REGNO (x);
12034       /* A hard reg in a wide mode may really be multiple registers.
12035          If so, mark all of them just like the first.  */
12036       if (regno < FIRST_PSEUDO_REGISTER)
12037         {
12038           unsigned int endregno, r;
12039
12040           /* None of this applies to the stack, frame or arg pointers.  */
12041           if (regno == STACK_POINTER_REGNUM
12042 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12043               || regno == HARD_FRAME_POINTER_REGNUM
12044 #endif
12045 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12046               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12047 #endif
12048               || regno == FRAME_POINTER_REGNUM)
12049             return;
12050
12051           endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12052           for (r = regno; r < endregno; r++)
12053             SET_HARD_REG_BIT (newpat_used_regs, r);
12054         }
12055       return;
12056
12057     case SET:
12058       {
12059         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12060            the address.  */
12061         rtx testreg = SET_DEST (x);
12062
12063         while (GET_CODE (testreg) == SUBREG
12064                || GET_CODE (testreg) == ZERO_EXTRACT
12065                || GET_CODE (testreg) == SIGN_EXTRACT
12066                || GET_CODE (testreg) == STRICT_LOW_PART)
12067           testreg = XEXP (testreg, 0);
12068
12069         if (GET_CODE (testreg) == MEM)
12070           mark_used_regs_combine (XEXP (testreg, 0));
12071
12072         mark_used_regs_combine (SET_SRC (x));
12073       }
12074       return;
12075
12076     default:
12077       break;
12078     }
12079
12080   /* Recursively scan the operands of this expression.  */
12081
12082   {
12083     const char *fmt = GET_RTX_FORMAT (code);
12084
12085     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12086       {
12087         if (fmt[i] == 'e')
12088           mark_used_regs_combine (XEXP (x, i));
12089         else if (fmt[i] == 'E')
12090           {
12091             int j;
12092
12093             for (j = 0; j < XVECLEN (x, i); j++)
12094               mark_used_regs_combine (XVECEXP (x, i, j));
12095           }
12096       }
12097   }
12098 }
12099 \f
12100 /* Remove register number REGNO from the dead registers list of INSN.
12101
12102    Return the note used to record the death, if there was one.  */
12103
12104 rtx
12105 remove_death (unsigned int regno, rtx insn)
12106 {
12107   rtx note = find_regno_note (insn, REG_DEAD, regno);
12108
12109   if (note)
12110     {
12111       REG_N_DEATHS (regno)--;
12112       remove_note (insn, note);
12113     }
12114
12115   return note;
12116 }
12117
12118 /* For each register (hardware or pseudo) used within expression X, if its
12119    death is in an instruction with cuid between FROM_CUID (inclusive) and
12120    TO_INSN (exclusive), put a REG_DEAD note for that register in the
12121    list headed by PNOTES.
12122
12123    That said, don't move registers killed by maybe_kill_insn.
12124
12125    This is done when X is being merged by combination into TO_INSN.  These
12126    notes will then be distributed as needed.  */
12127
12128 static void
12129 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
12130              rtx *pnotes)
12131 {
12132   const char *fmt;
12133   int len, i;
12134   enum rtx_code code = GET_CODE (x);
12135
12136   if (code == REG)
12137     {
12138       unsigned int regno = REGNO (x);
12139       rtx where_dead = reg_last_death[regno];
12140       rtx before_dead, after_dead;
12141
12142       /* Don't move the register if it gets killed in between from and to.  */
12143       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12144           && ! reg_referenced_p (x, maybe_kill_insn))
12145         return;
12146
12147       /* WHERE_DEAD could be a USE insn made by combine, so first we
12148          make sure that we have insns with valid INSN_CUID values.  */
12149       before_dead = where_dead;
12150       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
12151         before_dead = PREV_INSN (before_dead);
12152
12153       after_dead = where_dead;
12154       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
12155         after_dead = NEXT_INSN (after_dead);
12156
12157       if (before_dead && after_dead
12158           && INSN_CUID (before_dead) >= from_cuid
12159           && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
12160               || (where_dead != after_dead
12161                   && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
12162         {
12163           rtx note = remove_death (regno, where_dead);
12164
12165           /* It is possible for the call above to return 0.  This can occur
12166              when reg_last_death points to I2 or I1 that we combined with.
12167              In that case make a new note.
12168
12169              We must also check for the case where X is a hard register
12170              and NOTE is a death note for a range of hard registers
12171              including X.  In that case, we must put REG_DEAD notes for
12172              the remaining registers in place of NOTE.  */
12173
12174           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12175               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12176                   > GET_MODE_SIZE (GET_MODE (x))))
12177             {
12178               unsigned int deadregno = REGNO (XEXP (note, 0));
12179               unsigned int deadend
12180                 = (deadregno + HARD_REGNO_NREGS (deadregno,
12181                                                  GET_MODE (XEXP (note, 0))));
12182               unsigned int ourend
12183                 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12184               unsigned int i;
12185
12186               for (i = deadregno; i < deadend; i++)
12187                 if (i < regno || i >= ourend)
12188                   REG_NOTES (where_dead)
12189                     = gen_rtx_EXPR_LIST (REG_DEAD,
12190                                          regno_reg_rtx[i],
12191                                          REG_NOTES (where_dead));
12192             }
12193
12194           /* If we didn't find any note, or if we found a REG_DEAD note that
12195              covers only part of the given reg, and we have a multi-reg hard
12196              register, then to be safe we must check for REG_DEAD notes
12197              for each register other than the first.  They could have
12198              their own REG_DEAD notes lying around.  */
12199           else if ((note == 0
12200                     || (note != 0
12201                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12202                             < GET_MODE_SIZE (GET_MODE (x)))))
12203                    && regno < FIRST_PSEUDO_REGISTER
12204                    && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
12205             {
12206               unsigned int ourend
12207                 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12208               unsigned int i, offset;
12209               rtx oldnotes = 0;
12210
12211               if (note)
12212                 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
12213               else
12214                 offset = 1;
12215
12216               for (i = regno + offset; i < ourend; i++)
12217                 move_deaths (regno_reg_rtx[i],
12218                              maybe_kill_insn, from_cuid, to_insn, &oldnotes);
12219             }
12220
12221           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12222             {
12223               XEXP (note, 1) = *pnotes;
12224               *pnotes = note;
12225             }
12226           else
12227             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12228
12229           REG_N_DEATHS (regno)++;
12230         }
12231
12232       return;
12233     }
12234
12235   else if (GET_CODE (x) == SET)
12236     {
12237       rtx dest = SET_DEST (x);
12238
12239       move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
12240
12241       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12242          that accesses one word of a multi-word item, some
12243          piece of everything register in the expression is used by
12244          this insn, so remove any old death.  */
12245       /* ??? So why do we test for equality of the sizes?  */
12246
12247       if (GET_CODE (dest) == ZERO_EXTRACT
12248           || GET_CODE (dest) == STRICT_LOW_PART
12249           || (GET_CODE (dest) == SUBREG
12250               && (((GET_MODE_SIZE (GET_MODE (dest))
12251                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12252                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12253                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12254         {
12255           move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
12256           return;
12257         }
12258
12259       /* If this is some other SUBREG, we know it replaces the entire
12260          value, so use that as the destination.  */
12261       if (GET_CODE (dest) == SUBREG)
12262         dest = SUBREG_REG (dest);
12263
12264       /* If this is a MEM, adjust deaths of anything used in the address.
12265          For a REG (the only other possibility), the entire value is
12266          being replaced so the old value is not used in this insn.  */
12267
12268       if (GET_CODE (dest) == MEM)
12269         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
12270                      to_insn, pnotes);
12271       return;
12272     }
12273
12274   else if (GET_CODE (x) == CLOBBER)
12275     return;
12276
12277   len = GET_RTX_LENGTH (code);
12278   fmt = GET_RTX_FORMAT (code);
12279
12280   for (i = 0; i < len; i++)
12281     {
12282       if (fmt[i] == 'E')
12283         {
12284           int j;
12285           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12286             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
12287                          to_insn, pnotes);
12288         }
12289       else if (fmt[i] == 'e')
12290         move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
12291     }
12292 }
12293 \f
12294 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12295    pattern of an insn.  X must be a REG.  */
12296
12297 static int
12298 reg_bitfield_target_p (rtx x, rtx body)
12299 {
12300   int i;
12301
12302   if (GET_CODE (body) == SET)
12303     {
12304       rtx dest = SET_DEST (body);
12305       rtx target;
12306       unsigned int regno, tregno, endregno, endtregno;
12307
12308       if (GET_CODE (dest) == ZERO_EXTRACT)
12309         target = XEXP (dest, 0);
12310       else if (GET_CODE (dest) == STRICT_LOW_PART)
12311         target = SUBREG_REG (XEXP (dest, 0));
12312       else
12313         return 0;
12314
12315       if (GET_CODE (target) == SUBREG)
12316         target = SUBREG_REG (target);
12317
12318       if (GET_CODE (target) != REG)
12319         return 0;
12320
12321       tregno = REGNO (target), regno = REGNO (x);
12322       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12323         return target == x;
12324
12325       endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
12326       endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12327
12328       return endregno > tregno && regno < endtregno;
12329     }
12330
12331   else if (GET_CODE (body) == PARALLEL)
12332     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12333       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12334         return 1;
12335
12336   return 0;
12337 }
12338 \f
12339 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12340    as appropriate.  I3 and I2 are the insns resulting from the combination
12341    insns including FROM (I2 may be zero).
12342
12343    Each note in the list is either ignored or placed on some insns, depending
12344    on the type of note.  */
12345
12346 static void
12347 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
12348 {
12349   rtx note, next_note;
12350   rtx tem;
12351
12352   for (note = notes; note; note = next_note)
12353     {
12354       rtx place = 0, place2 = 0;
12355
12356       /* If this NOTE references a pseudo register, ensure it references
12357          the latest copy of that register.  */
12358       if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
12359           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
12360         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
12361
12362       next_note = XEXP (note, 1);
12363       switch (REG_NOTE_KIND (note))
12364         {
12365         case REG_BR_PROB:
12366         case REG_BR_PRED:
12367           /* Doesn't matter much where we put this, as long as it's somewhere.
12368              It is preferable to keep these notes on branches, which is most
12369              likely to be i3.  */
12370           place = i3;
12371           break;
12372
12373         case REG_VALUE_PROFILE:
12374           /* Just get rid of this note, as it is unused later anyway.  */
12375           break;
12376
12377         case REG_VTABLE_REF:
12378           /* ??? Should remain with *a particular* memory load.  Given the
12379              nature of vtable data, the last insn seems relatively safe.  */
12380           place = i3;
12381           break;
12382
12383         case REG_NON_LOCAL_GOTO:
12384           if (GET_CODE (i3) == JUMP_INSN)
12385             place = i3;
12386           else if (i2 && GET_CODE (i2) == JUMP_INSN)
12387             place = i2;
12388           else
12389             abort ();
12390           break;
12391
12392         case REG_EH_REGION:
12393           /* These notes must remain with the call or trapping instruction.  */
12394           if (GET_CODE (i3) == CALL_INSN)
12395             place = i3;
12396           else if (i2 && GET_CODE (i2) == CALL_INSN)
12397             place = i2;
12398           else if (flag_non_call_exceptions)
12399             {
12400               if (may_trap_p (i3))
12401                 place = i3;
12402               else if (i2 && may_trap_p (i2))
12403                 place = i2;
12404               /* ??? Otherwise assume we've combined things such that we
12405                  can now prove that the instructions can't trap.  Drop the
12406                  note in this case.  */
12407             }
12408           else
12409             abort ();
12410           break;
12411
12412         case REG_ALWAYS_RETURN:
12413         case REG_NORETURN:
12414         case REG_SETJMP:
12415           /* These notes must remain with the call.  It should not be
12416              possible for both I2 and I3 to be a call.  */
12417           if (GET_CODE (i3) == CALL_INSN)
12418             place = i3;
12419           else if (i2 && GET_CODE (i2) == CALL_INSN)
12420             place = i2;
12421           else
12422             abort ();
12423           break;
12424
12425         case REG_UNUSED:
12426           /* Any clobbers for i3 may still exist, and so we must process
12427              REG_UNUSED notes from that insn.
12428
12429              Any clobbers from i2 or i1 can only exist if they were added by
12430              recog_for_combine.  In that case, recog_for_combine created the
12431              necessary REG_UNUSED notes.  Trying to keep any original
12432              REG_UNUSED notes from these insns can cause incorrect output
12433              if it is for the same register as the original i3 dest.
12434              In that case, we will notice that the register is set in i3,
12435              and then add a REG_UNUSED note for the destination of i3, which
12436              is wrong.  However, it is possible to have REG_UNUSED notes from
12437              i2 or i1 for register which were both used and clobbered, so
12438              we keep notes from i2 or i1 if they will turn into REG_DEAD
12439              notes.  */
12440
12441           /* If this register is set or clobbered in I3, put the note there
12442              unless there is one already.  */
12443           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12444             {
12445               if (from_insn != i3)
12446                 break;
12447
12448               if (! (GET_CODE (XEXP (note, 0)) == REG
12449                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12450                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12451                 place = i3;
12452             }
12453           /* Otherwise, if this register is used by I3, then this register
12454              now dies here, so we must put a REG_DEAD note here unless there
12455              is one already.  */
12456           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12457                    && ! (GET_CODE (XEXP (note, 0)) == REG
12458                          ? find_regno_note (i3, REG_DEAD,
12459                                             REGNO (XEXP (note, 0)))
12460                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12461             {
12462               PUT_REG_NOTE_KIND (note, REG_DEAD);
12463               place = i3;
12464             }
12465           break;
12466
12467         case REG_EQUAL:
12468         case REG_EQUIV:
12469         case REG_NOALIAS:
12470           /* These notes say something about results of an insn.  We can
12471              only support them if they used to be on I3 in which case they
12472              remain on I3.  Otherwise they are ignored.
12473
12474              If the note refers to an expression that is not a constant, we
12475              must also ignore the note since we cannot tell whether the
12476              equivalence is still true.  It might be possible to do
12477              slightly better than this (we only have a problem if I2DEST
12478              or I1DEST is present in the expression), but it doesn't
12479              seem worth the trouble.  */
12480
12481           if (from_insn == i3
12482               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12483             place = i3;
12484           break;
12485
12486         case REG_INC:
12487         case REG_NO_CONFLICT:
12488           /* These notes say something about how a register is used.  They must
12489              be present on any use of the register in I2 or I3.  */
12490           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12491             place = i3;
12492
12493           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12494             {
12495               if (place)
12496                 place2 = i2;
12497               else
12498                 place = i2;
12499             }
12500           break;
12501
12502         case REG_LABEL:
12503           /* This can show up in several ways -- either directly in the
12504              pattern, or hidden off in the constant pool with (or without?)
12505              a REG_EQUAL note.  */
12506           /* ??? Ignore the without-reg_equal-note problem for now.  */
12507           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12508               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12509                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12510                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12511             place = i3;
12512
12513           if (i2
12514               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12515                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12516                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12517                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12518             {
12519               if (place)
12520                 place2 = i2;
12521               else
12522                 place = i2;
12523             }
12524
12525           /* Don't attach REG_LABEL note to a JUMP_INSN which has
12526              JUMP_LABEL already.  Instead, decrement LABEL_NUSES.  */
12527           if (place && GET_CODE (place) == JUMP_INSN && JUMP_LABEL (place))
12528             {
12529               if (JUMP_LABEL (place) != XEXP (note, 0))
12530                 abort ();
12531               if (GET_CODE (JUMP_LABEL (place)) == CODE_LABEL)
12532                 LABEL_NUSES (JUMP_LABEL (place))--;
12533               place = 0;
12534             }
12535           if (place2 && GET_CODE (place2) == JUMP_INSN && JUMP_LABEL (place2))
12536             {
12537               if (JUMP_LABEL (place2) != XEXP (note, 0))
12538                 abort ();
12539               if (GET_CODE (JUMP_LABEL (place2)) == CODE_LABEL)
12540                 LABEL_NUSES (JUMP_LABEL (place2))--;
12541               place2 = 0;
12542             }
12543           break;
12544
12545         case REG_NONNEG:
12546           /* This note says something about the value of a register prior
12547              to the execution of an insn.  It is too much trouble to see
12548              if the note is still correct in all situations.  It is better
12549              to simply delete it.  */
12550           break;
12551
12552         case REG_RETVAL:
12553           /* If the insn previously containing this note still exists,
12554              put it back where it was.  Otherwise move it to the previous
12555              insn.  Adjust the corresponding REG_LIBCALL note.  */
12556           if (GET_CODE (from_insn) != NOTE)
12557             place = from_insn;
12558           else
12559             {
12560               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12561               place = prev_real_insn (from_insn);
12562               if (tem && place)
12563                 XEXP (tem, 0) = place;
12564               /* If we're deleting the last remaining instruction of a
12565                  libcall sequence, don't add the notes.  */
12566               else if (XEXP (note, 0) == from_insn)
12567                 tem = place = 0;
12568             }
12569           break;
12570
12571         case REG_LIBCALL:
12572           /* This is handled similarly to REG_RETVAL.  */
12573           if (GET_CODE (from_insn) != NOTE)
12574             place = from_insn;
12575           else
12576             {
12577               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12578               place = next_real_insn (from_insn);
12579               if (tem && place)
12580                 XEXP (tem, 0) = place;
12581               /* If we're deleting the last remaining instruction of a
12582                  libcall sequence, don't add the notes.  */
12583               else if (XEXP (note, 0) == from_insn)
12584                 tem = place = 0;
12585             }
12586           break;
12587
12588         case REG_DEAD:
12589           /* If the register is used as an input in I3, it dies there.
12590              Similarly for I2, if it is nonzero and adjacent to I3.
12591
12592              If the register is not used as an input in either I3 or I2
12593              and it is not one of the registers we were supposed to eliminate,
12594              there are two possibilities.  We might have a non-adjacent I2
12595              or we might have somehow eliminated an additional register
12596              from a computation.  For example, we might have had A & B where
12597              we discover that B will always be zero.  In this case we will
12598              eliminate the reference to A.
12599
12600              In both cases, we must search to see if we can find a previous
12601              use of A and put the death note there.  */
12602
12603           if (from_insn
12604               && GET_CODE (from_insn) == CALL_INSN
12605               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12606             place = from_insn;
12607           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12608             place = i3;
12609           else if (i2 != 0 && next_nonnote_insn (i2) == i3
12610                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12611             place = i2;
12612
12613           if (place == 0)
12614             {
12615               basic_block bb = this_basic_block;
12616
12617               for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12618                 {
12619                   if (! INSN_P (tem))
12620                     {
12621                       if (tem == BB_HEAD (bb))
12622                         break;
12623                       continue;
12624                     }
12625
12626                   /* If the register is being set at TEM, see if that is all
12627                      TEM is doing.  If so, delete TEM.  Otherwise, make this
12628                      into a REG_UNUSED note instead.  */
12629                   if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
12630                     {
12631                       rtx set = single_set (tem);
12632                       rtx inner_dest = 0;
12633 #ifdef HAVE_cc0
12634                       rtx cc0_setter = NULL_RTX;
12635 #endif
12636
12637                       if (set != 0)
12638                         for (inner_dest = SET_DEST (set);
12639                              (GET_CODE (inner_dest) == STRICT_LOW_PART
12640                               || GET_CODE (inner_dest) == SUBREG
12641                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
12642                              inner_dest = XEXP (inner_dest, 0))
12643                           ;
12644
12645                       /* Verify that it was the set, and not a clobber that
12646                          modified the register.
12647
12648                          CC0 targets must be careful to maintain setter/user
12649                          pairs.  If we cannot delete the setter due to side
12650                          effects, mark the user with an UNUSED note instead
12651                          of deleting it.  */
12652
12653                       if (set != 0 && ! side_effects_p (SET_SRC (set))
12654                           && rtx_equal_p (XEXP (note, 0), inner_dest)
12655 #ifdef HAVE_cc0
12656                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12657                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12658                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12659 #endif
12660                           )
12661                         {
12662                           /* Move the notes and links of TEM elsewhere.
12663                              This might delete other dead insns recursively.
12664                              First set the pattern to something that won't use
12665                              any register.  */
12666                           rtx old_notes = REG_NOTES (tem);
12667
12668                           PATTERN (tem) = pc_rtx;
12669                           REG_NOTES (tem) = NULL;
12670
12671                           distribute_notes (old_notes, tem, tem, NULL_RTX);
12672                           distribute_links (LOG_LINKS (tem));
12673
12674                           PUT_CODE (tem, NOTE);
12675                           NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
12676                           NOTE_SOURCE_FILE (tem) = 0;
12677
12678 #ifdef HAVE_cc0
12679                           /* Delete the setter too.  */
12680                           if (cc0_setter)
12681                             {
12682                               PATTERN (cc0_setter) = pc_rtx;
12683                               old_notes = REG_NOTES (cc0_setter);
12684                               REG_NOTES (cc0_setter) = NULL;
12685
12686                               distribute_notes (old_notes, cc0_setter,
12687                                                 cc0_setter, NULL_RTX);
12688                               distribute_links (LOG_LINKS (cc0_setter));
12689
12690                               PUT_CODE (cc0_setter, NOTE);
12691                               NOTE_LINE_NUMBER (cc0_setter)
12692                                 = NOTE_INSN_DELETED;
12693                               NOTE_SOURCE_FILE (cc0_setter) = 0;
12694                             }
12695 #endif
12696                         }
12697                       /* If the register is both set and used here, put the
12698                          REG_DEAD note here, but place a REG_UNUSED note
12699                          here too unless there already is one.  */
12700                       else if (reg_referenced_p (XEXP (note, 0),
12701                                                  PATTERN (tem)))
12702                         {
12703                           place = tem;
12704
12705                           if (! find_regno_note (tem, REG_UNUSED,
12706                                                  REGNO (XEXP (note, 0))))
12707                             REG_NOTES (tem)
12708                               = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
12709                                                    REG_NOTES (tem));
12710                         }
12711                       else
12712                         {
12713                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
12714
12715                           /*  If there isn't already a REG_UNUSED note, put one
12716                               here.  */
12717                           if (! find_regno_note (tem, REG_UNUSED,
12718                                                  REGNO (XEXP (note, 0))))
12719                             place = tem;
12720                           break;
12721                         }
12722                     }
12723                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12724                            || (GET_CODE (tem) == CALL_INSN
12725                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
12726                     {
12727                       place = tem;
12728
12729                       /* If we are doing a 3->2 combination, and we have a
12730                          register which formerly died in i3 and was not used
12731                          by i2, which now no longer dies in i3 and is used in
12732                          i2 but does not die in i2, and place is between i2
12733                          and i3, then we may need to move a link from place to
12734                          i2.  */
12735                       if (i2 && INSN_UID (place) <= max_uid_cuid
12736                           && INSN_CUID (place) > INSN_CUID (i2)
12737                           && from_insn
12738                           && INSN_CUID (from_insn) > INSN_CUID (i2)
12739                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12740                         {
12741                           rtx links = LOG_LINKS (place);
12742                           LOG_LINKS (place) = 0;
12743                           distribute_links (links);
12744                         }
12745                       break;
12746                     }
12747
12748                   if (tem == BB_HEAD (bb))
12749                     break;
12750                 }
12751
12752               /* We haven't found an insn for the death note and it
12753                  is still a REG_DEAD note, but we have hit the beginning
12754                  of the block.  If the existing life info says the reg
12755                  was dead, there's nothing left to do.  Otherwise, we'll
12756                  need to do a global life update after combine.  */
12757               if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12758                   && REGNO_REG_SET_P (bb->global_live_at_start,
12759                                       REGNO (XEXP (note, 0))))
12760                 SET_BIT (refresh_blocks, this_basic_block->index);
12761             }
12762
12763           /* If the register is set or already dead at PLACE, we needn't do
12764              anything with this note if it is still a REG_DEAD note.
12765              We can here if it is set at all, not if is it totally replace,
12766              which is what `dead_or_set_p' checks, so also check for it being
12767              set partially.  */
12768
12769           if (place && REG_NOTE_KIND (note) == REG_DEAD)
12770             {
12771               unsigned int regno = REGNO (XEXP (note, 0));
12772
12773               /* Similarly, if the instruction on which we want to place
12774                  the note is a noop, we'll need do a global live update
12775                  after we remove them in delete_noop_moves.  */
12776               if (noop_move_p (place))
12777                 SET_BIT (refresh_blocks, this_basic_block->index);
12778
12779               if (dead_or_set_p (place, XEXP (note, 0))
12780                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12781                 {
12782                   /* Unless the register previously died in PLACE, clear
12783                      reg_last_death.  [I no longer understand why this is
12784                      being done.] */
12785                   if (reg_last_death[regno] != place)
12786                     reg_last_death[regno] = 0;
12787                   place = 0;
12788                 }
12789               else
12790                 reg_last_death[regno] = place;
12791
12792               /* If this is a death note for a hard reg that is occupying
12793                  multiple registers, ensure that we are still using all
12794                  parts of the object.  If we find a piece of the object
12795                  that is unused, we must arrange for an appropriate REG_DEAD
12796                  note to be added for it.  However, we can't just emit a USE
12797                  and tag the note to it, since the register might actually
12798                  be dead; so we recourse, and the recursive call then finds
12799                  the previous insn that used this register.  */
12800
12801               if (place && regno < FIRST_PSEUDO_REGISTER
12802                   && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
12803                 {
12804                   unsigned int endregno
12805                     = regno + HARD_REGNO_NREGS (regno,
12806                                                 GET_MODE (XEXP (note, 0)));
12807                   int all_used = 1;
12808                   unsigned int i;
12809
12810                   for (i = regno; i < endregno; i++)
12811                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12812                          && ! find_regno_fusage (place, USE, i))
12813                         || dead_or_set_regno_p (place, i))
12814                       all_used = 0;
12815
12816                   if (! all_used)
12817                     {
12818                       /* Put only REG_DEAD notes for pieces that are
12819                          not already dead or set.  */
12820
12821                       for (i = regno; i < endregno;
12822                            i += HARD_REGNO_NREGS (i, reg_raw_mode[i]))
12823                         {
12824                           rtx piece = regno_reg_rtx[i];
12825                           basic_block bb = this_basic_block;
12826
12827                           if (! dead_or_set_p (place, piece)
12828                               && ! reg_bitfield_target_p (piece,
12829                                                           PATTERN (place)))
12830                             {
12831                               rtx new_note
12832                                 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12833
12834                               distribute_notes (new_note, place, place,
12835                                                 NULL_RTX);
12836                             }
12837                           else if (! refers_to_regno_p (i, i + 1,
12838                                                         PATTERN (place), 0)
12839                                    && ! find_regno_fusage (place, USE, i))
12840                             for (tem = PREV_INSN (place); ;
12841                                  tem = PREV_INSN (tem))
12842                               {
12843                                 if (! INSN_P (tem))
12844                                   {
12845                                     if (tem == BB_HEAD (bb))
12846                                       {
12847                                         SET_BIT (refresh_blocks,
12848                                                  this_basic_block->index);
12849                                         break;
12850                                       }
12851                                     continue;
12852                                   }
12853                                 if (dead_or_set_p (tem, piece)
12854                                     || reg_bitfield_target_p (piece,
12855                                                               PATTERN (tem)))
12856                                   {
12857                                     REG_NOTES (tem)
12858                                       = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12859                                                            REG_NOTES (tem));
12860                                     break;
12861                                   }
12862                               }
12863
12864                         }
12865
12866                       place = 0;
12867                     }
12868                 }
12869             }
12870           break;
12871
12872         default:
12873           /* Any other notes should not be present at this point in the
12874              compilation.  */
12875           abort ();
12876         }
12877
12878       if (place)
12879         {
12880           XEXP (note, 1) = REG_NOTES (place);
12881           REG_NOTES (place) = note;
12882         }
12883       else if ((REG_NOTE_KIND (note) == REG_DEAD
12884                 || REG_NOTE_KIND (note) == REG_UNUSED)
12885                && GET_CODE (XEXP (note, 0)) == REG)
12886         REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12887
12888       if (place2)
12889         {
12890           if ((REG_NOTE_KIND (note) == REG_DEAD
12891                || REG_NOTE_KIND (note) == REG_UNUSED)
12892               && GET_CODE (XEXP (note, 0)) == REG)
12893             REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12894
12895           REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12896                                                REG_NOTE_KIND (note),
12897                                                XEXP (note, 0),
12898                                                REG_NOTES (place2));
12899         }
12900     }
12901 }
12902 \f
12903 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12904    I3, I2, and I1 to new locations.  This is also called to add a link
12905    pointing at I3 when I3's destination is changed.  */
12906
12907 static void
12908 distribute_links (rtx links)
12909 {
12910   rtx link, next_link;
12911
12912   for (link = links; link; link = next_link)
12913     {
12914       rtx place = 0;
12915       rtx insn;
12916       rtx set, reg;
12917
12918       next_link = XEXP (link, 1);
12919
12920       /* If the insn that this link points to is a NOTE or isn't a single
12921          set, ignore it.  In the latter case, it isn't clear what we
12922          can do other than ignore the link, since we can't tell which
12923          register it was for.  Such links wouldn't be used by combine
12924          anyway.
12925
12926          It is not possible for the destination of the target of the link to
12927          have been changed by combine.  The only potential of this is if we
12928          replace I3, I2, and I1 by I3 and I2.  But in that case the
12929          destination of I2 also remains unchanged.  */
12930
12931       if (GET_CODE (XEXP (link, 0)) == NOTE
12932           || (set = single_set (XEXP (link, 0))) == 0)
12933         continue;
12934
12935       reg = SET_DEST (set);
12936       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12937              || GET_CODE (reg) == SIGN_EXTRACT
12938              || GET_CODE (reg) == STRICT_LOW_PART)
12939         reg = XEXP (reg, 0);
12940
12941       /* A LOG_LINK is defined as being placed on the first insn that uses
12942          a register and points to the insn that sets the register.  Start
12943          searching at the next insn after the target of the link and stop
12944          when we reach a set of the register or the end of the basic block.
12945
12946          Note that this correctly handles the link that used to point from
12947          I3 to I2.  Also note that not much searching is typically done here
12948          since most links don't point very far away.  */
12949
12950       for (insn = NEXT_INSN (XEXP (link, 0));
12951            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12952                      || BB_HEAD (this_basic_block->next_bb) != insn));
12953            insn = NEXT_INSN (insn))
12954         if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12955           {
12956             if (reg_referenced_p (reg, PATTERN (insn)))
12957               place = insn;
12958             break;
12959           }
12960         else if (GET_CODE (insn) == CALL_INSN
12961                  && find_reg_fusage (insn, USE, reg))
12962           {
12963             place = insn;
12964             break;
12965           }
12966         else if (INSN_P (insn) && reg_set_p (reg, insn))
12967           break;
12968
12969       /* If we found a place to put the link, place it there unless there
12970          is already a link to the same insn as LINK at that point.  */
12971
12972       if (place)
12973         {
12974           rtx link2;
12975
12976           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12977             if (XEXP (link2, 0) == XEXP (link, 0))
12978               break;
12979
12980           if (link2 == 0)
12981             {
12982               XEXP (link, 1) = LOG_LINKS (place);
12983               LOG_LINKS (place) = link;
12984
12985               /* Set added_links_insn to the earliest insn we added a
12986                  link to.  */
12987               if (added_links_insn == 0
12988                   || INSN_CUID (added_links_insn) > INSN_CUID (place))
12989                 added_links_insn = place;
12990             }
12991         }
12992     }
12993 }
12994 \f
12995 /* Compute INSN_CUID for INSN, which is an insn made by combine.  */
12996
12997 static int
12998 insn_cuid (rtx insn)
12999 {
13000   while (insn != 0 && INSN_UID (insn) > max_uid_cuid
13001          && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
13002     insn = NEXT_INSN (insn);
13003
13004   if (INSN_UID (insn) > max_uid_cuid)
13005     abort ();
13006
13007   return INSN_CUID (insn);
13008 }
13009 \f
13010 void
13011 dump_combine_stats (FILE *file)
13012 {
13013   fnotice
13014     (file,
13015      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13016      combine_attempts, combine_merges, combine_extras, combine_successes);
13017 }
13018
13019 void
13020 dump_combine_total_stats (FILE *file)
13021 {
13022   fnotice
13023     (file,
13024      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13025      total_attempts, total_merges, total_extras, total_successes);
13026 }