OSDN Git Service

* stmt.c (resolve_asm_operand_names): Call check_unique_operand_names
[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 sets_function_arg_p (rtx);
349 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
350 static int contains_muldiv (rtx);
351 static rtx try_combine (rtx, rtx, rtx, int *);
352 static void undo_all (void);
353 static void undo_commit (void);
354 static rtx *find_split_point (rtx *, rtx);
355 static rtx subst (rtx, rtx, rtx, int, int);
356 static rtx combine_simplify_rtx (rtx, enum machine_mode, int, int);
357 static rtx simplify_if_then_else (rtx);
358 static rtx simplify_set (rtx);
359 static rtx simplify_logical (rtx, int);
360 static rtx expand_compound_operation (rtx);
361 static rtx expand_field_assignment (rtx);
362 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
363                             rtx, unsigned HOST_WIDE_INT, int, int, int);
364 static rtx extract_left_shift (rtx, int);
365 static rtx make_compound_operation (rtx, enum rtx_code);
366 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
367                               unsigned HOST_WIDE_INT *);
368 static rtx force_to_mode (rtx, enum machine_mode,
369                           unsigned HOST_WIDE_INT, rtx, int);
370 static rtx if_then_else_cond (rtx, rtx *, rtx *);
371 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
372 static int rtx_equal_for_field_assignment_p (rtx, rtx);
373 static rtx make_field_assignment (rtx);
374 static rtx apply_distributive_law (rtx);
375 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
376                                    unsigned HOST_WIDE_INT);
377 static unsigned HOST_WIDE_INT cached_nonzero_bits (rtx, enum machine_mode,
378                                                    rtx, enum machine_mode,
379                                                    unsigned HOST_WIDE_INT);
380 static unsigned HOST_WIDE_INT nonzero_bits1 (rtx, enum machine_mode, rtx,
381                                              enum machine_mode,
382                                              unsigned HOST_WIDE_INT);
383 static unsigned int cached_num_sign_bit_copies (rtx, enum machine_mode, rtx,
384                                                 enum machine_mode,
385                                                 unsigned int);
386 static unsigned int num_sign_bit_copies1 (rtx, enum machine_mode, rtx,
387                                           enum machine_mode, unsigned int);
388 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
389                             HOST_WIDE_INT, enum machine_mode, int *);
390 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
391                                  int);
392 static int recog_for_combine (rtx *, rtx, rtx *);
393 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
394 static rtx gen_binary (enum rtx_code, enum machine_mode, rtx, rtx);
395 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
396 static void update_table_tick (rtx);
397 static void record_value_for_reg (rtx, rtx, rtx);
398 static void check_promoted_subreg (rtx, rtx);
399 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
400 static void record_dead_and_set_regs (rtx);
401 static int get_last_value_validate (rtx *, rtx, int, int);
402 static rtx get_last_value (rtx);
403 static int use_crosses_set_p (rtx, int);
404 static void reg_dead_at_p_1 (rtx, rtx, void *);
405 static int reg_dead_at_p (rtx, rtx);
406 static void move_deaths (rtx, rtx, int, rtx, rtx *);
407 static int reg_bitfield_target_p (rtx, rtx);
408 static void distribute_notes (rtx, rtx, rtx, rtx);
409 static void distribute_links (rtx);
410 static void mark_used_regs_combine (rtx);
411 static int insn_cuid (rtx);
412 static void record_promoted_value (rtx, rtx);
413 static rtx reversed_comparison (rtx, enum machine_mode, rtx, rtx);
414 static enum rtx_code combine_reversed_comparison_code (rtx);
415 \f
416 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
417    insn.  The substitution can be undone by undo_all.  If INTO is already
418    set to NEWVAL, do not record this change.  Because computing NEWVAL might
419    also call SUBST, we have to compute it before we put anything into
420    the undo table.  */
421
422 static void
423 do_SUBST (rtx *into, rtx newval)
424 {
425   struct undo *buf;
426   rtx oldval = *into;
427
428   if (oldval == newval)
429     return;
430
431   /* We'd like to catch as many invalid transformations here as
432      possible.  Unfortunately, there are way too many mode changes
433      that are perfectly valid, so we'd waste too much effort for
434      little gain doing the checks here.  Focus on catching invalid
435      transformations involving integer constants.  */
436   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
437       && GET_CODE (newval) == CONST_INT)
438     {
439       /* Sanity check that we're replacing oldval with a CONST_INT
440          that is a valid sign-extension for the original mode.  */
441       if (INTVAL (newval) != trunc_int_for_mode (INTVAL (newval),
442                                                  GET_MODE (oldval)))
443         abort ();
444
445       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
446          CONST_INT is not valid, because after the replacement, the
447          original mode would be gone.  Unfortunately, we can't tell
448          when do_SUBST is called to replace the operand thereof, so we
449          perform this test on oldval instead, checking whether an
450          invalid replacement took place before we got here.  */
451       if ((GET_CODE (oldval) == SUBREG
452            && GET_CODE (SUBREG_REG (oldval)) == CONST_INT)
453           || (GET_CODE (oldval) == ZERO_EXTEND
454               && GET_CODE (XEXP (oldval, 0)) == CONST_INT))
455         abort ();
456     }
457
458   if (undobuf.frees)
459     buf = undobuf.frees, undobuf.frees = buf->next;
460   else
461     buf = xmalloc (sizeof (struct undo));
462
463   buf->is_int = 0;
464   buf->where.r = into;
465   buf->old_contents.r = oldval;
466   *into = newval;
467
468   buf->next = undobuf.undos, undobuf.undos = buf;
469 }
470
471 #define SUBST(INTO, NEWVAL)     do_SUBST(&(INTO), (NEWVAL))
472
473 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
474    for the value of a HOST_WIDE_INT value (including CONST_INT) is
475    not safe.  */
476
477 static void
478 do_SUBST_INT (int *into, int newval)
479 {
480   struct undo *buf;
481   int oldval = *into;
482
483   if (oldval == newval)
484     return;
485
486   if (undobuf.frees)
487     buf = undobuf.frees, undobuf.frees = buf->next;
488   else
489     buf = xmalloc (sizeof (struct undo));
490
491   buf->is_int = 1;
492   buf->where.i = into;
493   buf->old_contents.i = oldval;
494   *into = newval;
495
496   buf->next = undobuf.undos, undobuf.undos = buf;
497 }
498
499 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT(&(INTO), (NEWVAL))
500 \f
501 /* Main entry point for combiner.  F is the first insn of the function.
502    NREGS is the first unused pseudo-reg number.
503
504    Return nonzero if the combiner has turned an indirect jump
505    instruction into a direct jump.  */
506 int
507 combine_instructions (rtx f, unsigned int nregs)
508 {
509   rtx insn, next;
510 #ifdef HAVE_cc0
511   rtx prev;
512 #endif
513   int i;
514   rtx links, nextlinks;
515
516   int new_direct_jump_p = 0;
517
518   combine_attempts = 0;
519   combine_merges = 0;
520   combine_extras = 0;
521   combine_successes = 0;
522
523   combine_max_regno = nregs;
524
525   reg_nonzero_bits = xcalloc (nregs, sizeof (unsigned HOST_WIDE_INT));
526   reg_sign_bit_copies = xcalloc (nregs, sizeof (unsigned char));
527
528   reg_last_death = xmalloc (nregs * sizeof (rtx));
529   reg_last_set = xmalloc (nregs * sizeof (rtx));
530   reg_last_set_value = xmalloc (nregs * sizeof (rtx));
531   reg_last_set_table_tick = xmalloc (nregs * sizeof (int));
532   reg_last_set_label = xmalloc (nregs * sizeof (int));
533   reg_last_set_invalid = xmalloc (nregs * sizeof (char));
534   reg_last_set_mode = xmalloc (nregs * sizeof (enum machine_mode));
535   reg_last_set_nonzero_bits = xmalloc (nregs * sizeof (HOST_WIDE_INT));
536   reg_last_set_sign_bit_copies = xmalloc (nregs * sizeof (char));
537
538   init_reg_last_arrays ();
539
540   init_recog_no_volatile ();
541
542   /* Compute maximum uid value so uid_cuid can be allocated.  */
543
544   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
545     if (INSN_UID (insn) > i)
546       i = INSN_UID (insn);
547
548   uid_cuid = xmalloc ((i + 1) * sizeof (int));
549   max_uid_cuid = i;
550
551   nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
552
553   /* Don't use reg_nonzero_bits when computing it.  This can cause problems
554      when, for example, we have j <<= 1 in a loop.  */
555
556   nonzero_sign_valid = 0;
557
558   /* Compute the mapping from uids to cuids.
559      Cuids are numbers assigned to insns, like uids,
560      except that cuids increase monotonically through the code.
561
562      Scan all SETs and see if we can deduce anything about what
563      bits are known to be zero for some registers and how many copies
564      of the sign bit are known to exist for those registers.
565
566      Also set any known values so that we can use it while searching
567      for what bits are known to be set.  */
568
569   label_tick = 1;
570
571   setup_incoming_promotions ();
572
573   refresh_blocks = sbitmap_alloc (last_basic_block);
574   sbitmap_zero (refresh_blocks);
575
576   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
577     {
578       uid_cuid[INSN_UID (insn)] = ++i;
579       subst_low_cuid = i;
580       subst_insn = insn;
581
582       if (INSN_P (insn))
583         {
584           note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
585                        NULL);
586           record_dead_and_set_regs (insn);
587
588 #ifdef AUTO_INC_DEC
589           for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
590             if (REG_NOTE_KIND (links) == REG_INC)
591               set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
592                                                 NULL);
593 #endif
594         }
595
596       if (GET_CODE (insn) == CODE_LABEL)
597         label_tick++;
598     }
599
600   nonzero_sign_valid = 1;
601
602   /* Now scan all the insns in forward order.  */
603
604   label_tick = 1;
605   last_call_cuid = 0;
606   mem_last_set = 0;
607   init_reg_last_arrays ();
608   setup_incoming_promotions ();
609
610   FOR_EACH_BB (this_basic_block)
611     {
612       for (insn = this_basic_block->head;
613            insn != NEXT_INSN (this_basic_block->end);
614            insn = next ? next : NEXT_INSN (insn))
615         {
616           next = 0;
617
618           if (GET_CODE (insn) == CODE_LABEL)
619             label_tick++;
620
621           else if (INSN_P (insn))
622             {
623               /* See if we know about function return values before this
624                  insn based upon SUBREG flags.  */
625               check_promoted_subreg (insn, PATTERN (insn));
626
627               /* Try this insn with each insn it links back to.  */
628
629               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
630                 if ((next = try_combine (insn, XEXP (links, 0),
631                                          NULL_RTX, &new_direct_jump_p)) != 0)
632                   goto retry;
633
634               /* Try each sequence of three linked insns ending with this one.  */
635
636               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
637                 {
638                   rtx link = XEXP (links, 0);
639
640                   /* If the linked insn has been replaced by a note, then there
641                      is no point in pursuing this chain any further.  */
642                   if (GET_CODE (link) == NOTE)
643                     continue;
644
645                   for (nextlinks = LOG_LINKS (link);
646                        nextlinks;
647                        nextlinks = XEXP (nextlinks, 1))
648                     if ((next = try_combine (insn, link,
649                                              XEXP (nextlinks, 0),
650                                              &new_direct_jump_p)) != 0)
651                       goto retry;
652                 }
653
654 #ifdef HAVE_cc0
655               /* Try to combine a jump insn that uses CC0
656                  with a preceding insn that sets CC0, and maybe with its
657                  logical predecessor as well.
658                  This is how we make decrement-and-branch insns.
659                  We need this special code because data flow connections
660                  via CC0 do not get entered in LOG_LINKS.  */
661
662               if (GET_CODE (insn) == JUMP_INSN
663                   && (prev = prev_nonnote_insn (insn)) != 0
664                   && GET_CODE (prev) == INSN
665                   && sets_cc0_p (PATTERN (prev)))
666                 {
667                   if ((next = try_combine (insn, prev,
668                                            NULL_RTX, &new_direct_jump_p)) != 0)
669                     goto retry;
670
671                   for (nextlinks = LOG_LINKS (prev); nextlinks;
672                        nextlinks = XEXP (nextlinks, 1))
673                     if ((next = try_combine (insn, prev,
674                                              XEXP (nextlinks, 0),
675                                              &new_direct_jump_p)) != 0)
676                       goto retry;
677                 }
678
679               /* Do the same for an insn that explicitly references CC0.  */
680               if (GET_CODE (insn) == INSN
681                   && (prev = prev_nonnote_insn (insn)) != 0
682                   && GET_CODE (prev) == INSN
683                   && sets_cc0_p (PATTERN (prev))
684                   && GET_CODE (PATTERN (insn)) == SET
685                   && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
686                 {
687                   if ((next = try_combine (insn, prev,
688                                            NULL_RTX, &new_direct_jump_p)) != 0)
689                     goto retry;
690
691                   for (nextlinks = LOG_LINKS (prev); nextlinks;
692                        nextlinks = XEXP (nextlinks, 1))
693                     if ((next = try_combine (insn, prev,
694                                              XEXP (nextlinks, 0),
695                                              &new_direct_jump_p)) != 0)
696                       goto retry;
697                 }
698
699               /* Finally, see if any of the insns that this insn links to
700                  explicitly references CC0.  If so, try this insn, that insn,
701                  and its predecessor if it sets CC0.  */
702               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
703                 if (GET_CODE (XEXP (links, 0)) == INSN
704                     && GET_CODE (PATTERN (XEXP (links, 0))) == SET
705                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
706                     && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
707                     && GET_CODE (prev) == INSN
708                     && sets_cc0_p (PATTERN (prev))
709                     && (next = try_combine (insn, XEXP (links, 0),
710                                             prev, &new_direct_jump_p)) != 0)
711                   goto retry;
712 #endif
713
714               /* Try combining an insn with two different insns whose results it
715                  uses.  */
716               for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
717                 for (nextlinks = XEXP (links, 1); nextlinks;
718                      nextlinks = XEXP (nextlinks, 1))
719                   if ((next = try_combine (insn, XEXP (links, 0),
720                                            XEXP (nextlinks, 0),
721                                            &new_direct_jump_p)) != 0)
722                     goto retry;
723
724               if (GET_CODE (insn) != NOTE)
725                 record_dead_and_set_regs (insn);
726
727             retry:
728               ;
729             }
730         }
731     }
732   clear_bb_flags ();
733
734   EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, i,
735                              BASIC_BLOCK (i)->flags |= BB_DIRTY);
736   new_direct_jump_p |= purge_all_dead_edges (0);
737   delete_noop_moves (f);
738
739   update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
740                                     PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
741                                     | PROP_KILL_DEAD_CODE);
742
743   /* Clean up.  */
744   sbitmap_free (refresh_blocks);
745   free (reg_nonzero_bits);
746   free (reg_sign_bit_copies);
747   free (reg_last_death);
748   free (reg_last_set);
749   free (reg_last_set_value);
750   free (reg_last_set_table_tick);
751   free (reg_last_set_label);
752   free (reg_last_set_invalid);
753   free (reg_last_set_mode);
754   free (reg_last_set_nonzero_bits);
755   free (reg_last_set_sign_bit_copies);
756   free (uid_cuid);
757
758   {
759     struct undo *undo, *next;
760     for (undo = undobuf.frees; undo; undo = next)
761       {
762         next = undo->next;
763         free (undo);
764       }
765     undobuf.frees = 0;
766   }
767
768   total_attempts += combine_attempts;
769   total_merges += combine_merges;
770   total_extras += combine_extras;
771   total_successes += combine_successes;
772
773   nonzero_sign_valid = 0;
774
775   /* Make recognizer allow volatile MEMs again.  */
776   init_recog ();
777
778   return new_direct_jump_p;
779 }
780
781 /* Wipe the reg_last_xxx arrays in preparation for another pass.  */
782
783 static void
784 init_reg_last_arrays (void)
785 {
786   unsigned int nregs = combine_max_regno;
787
788   memset (reg_last_death, 0, nregs * sizeof (rtx));
789   memset (reg_last_set, 0, nregs * sizeof (rtx));
790   memset (reg_last_set_value, 0, nregs * sizeof (rtx));
791   memset (reg_last_set_table_tick, 0, nregs * sizeof (int));
792   memset (reg_last_set_label, 0, nregs * sizeof (int));
793   memset (reg_last_set_invalid, 0, nregs * sizeof (char));
794   memset (reg_last_set_mode, 0, nregs * sizeof (enum machine_mode));
795   memset (reg_last_set_nonzero_bits, 0, nregs * sizeof (HOST_WIDE_INT));
796   memset (reg_last_set_sign_bit_copies, 0, nregs * sizeof (char));
797 }
798 \f
799 /* Set up any promoted values for incoming argument registers.  */
800
801 static void
802 setup_incoming_promotions (void)
803 {
804   unsigned int regno;
805   rtx reg;
806   enum machine_mode mode;
807   int unsignedp;
808   rtx first = get_insns ();
809
810   if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
811     {
812 #ifndef OUTGOING_REGNO
813 #define OUTGOING_REGNO(N) N
814 #endif
815       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
816         /* Check whether this register can hold an incoming pointer
817            argument.  FUNCTION_ARG_REGNO_P tests outgoing register
818            numbers, so translate if necessary due to register windows.  */
819         if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
820             && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
821           {
822             record_value_for_reg
823               (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
824                                            : SIGN_EXTEND),
825                                           GET_MODE (reg),
826                                           gen_rtx_CLOBBER (mode, const0_rtx)));
827           }
828     }
829 }
830 \f
831 /* Called via note_stores.  If X is a pseudo that is narrower than
832    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
833
834    If we are setting only a portion of X and we can't figure out what
835    portion, assume all bits will be used since we don't know what will
836    be happening.
837
838    Similarly, set how many bits of X are known to be copies of the sign bit
839    at all locations in the function.  This is the smallest number implied
840    by any set of X.  */
841
842 static void
843 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
844                                   void *data ATTRIBUTE_UNUSED)
845 {
846   unsigned int num;
847
848   if (GET_CODE (x) == REG
849       && REGNO (x) >= FIRST_PSEUDO_REGISTER
850       /* If this register is undefined at the start of the file, we can't
851          say what its contents were.  */
852       && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, REGNO (x))
853       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
854     {
855       if (set == 0 || GET_CODE (set) == CLOBBER)
856         {
857           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
858           reg_sign_bit_copies[REGNO (x)] = 1;
859           return;
860         }
861
862       /* If this is a complex assignment, see if we can convert it into a
863          simple assignment.  */
864       set = expand_field_assignment (set);
865
866       /* If this is a simple assignment, or we have a paradoxical SUBREG,
867          set what we know about X.  */
868
869       if (SET_DEST (set) == x
870           || (GET_CODE (SET_DEST (set)) == SUBREG
871               && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
872                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
873               && SUBREG_REG (SET_DEST (set)) == x))
874         {
875           rtx src = SET_SRC (set);
876
877 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
878           /* If X is narrower than a word and SRC is a non-negative
879              constant that would appear negative in the mode of X,
880              sign-extend it for use in reg_nonzero_bits because some
881              machines (maybe most) will actually do the sign-extension
882              and this is the conservative approach.
883
884              ??? For 2.5, try to tighten up the MD files in this regard
885              instead of this kludge.  */
886
887           if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
888               && GET_CODE (src) == CONST_INT
889               && INTVAL (src) > 0
890               && 0 != (INTVAL (src)
891                        & ((HOST_WIDE_INT) 1
892                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
893             src = GEN_INT (INTVAL (src)
894                            | ((HOST_WIDE_INT) (-1)
895                               << GET_MODE_BITSIZE (GET_MODE (x))));
896 #endif
897
898           /* Don't call nonzero_bits if it cannot change anything.  */
899           if (reg_nonzero_bits[REGNO (x)] != ~(unsigned HOST_WIDE_INT) 0)
900             reg_nonzero_bits[REGNO (x)]
901               |= nonzero_bits (src, nonzero_bits_mode);
902           num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
903           if (reg_sign_bit_copies[REGNO (x)] == 0
904               || reg_sign_bit_copies[REGNO (x)] > num)
905             reg_sign_bit_copies[REGNO (x)] = num;
906         }
907       else
908         {
909           reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
910           reg_sign_bit_copies[REGNO (x)] = 1;
911         }
912     }
913 }
914 \f
915 /* See if INSN can be combined into I3.  PRED and SUCC are optionally
916    insns that were previously combined into I3 or that will be combined
917    into the merger of INSN and I3.
918
919    Return 0 if the combination is not allowed for any reason.
920
921    If the combination is allowed, *PDEST will be set to the single
922    destination of INSN and *PSRC to the single source, and this function
923    will return 1.  */
924
925 static int
926 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
927                rtx *pdest, rtx *psrc)
928 {
929   int i;
930   rtx set = 0, src, dest;
931   rtx p;
932 #ifdef AUTO_INC_DEC
933   rtx link;
934 #endif
935   int all_adjacent = (succ ? (next_active_insn (insn) == succ
936                               && next_active_insn (succ) == i3)
937                       : next_active_insn (insn) == i3);
938
939   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
940      or a PARALLEL consisting of such a SET and CLOBBERs.
941
942      If INSN has CLOBBER parallel parts, ignore them for our processing.
943      By definition, these happen during the execution of the insn.  When it
944      is merged with another insn, all bets are off.  If they are, in fact,
945      needed and aren't also supplied in I3, they may be added by
946      recog_for_combine.  Otherwise, it won't match.
947
948      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
949      note.
950
951      Get the source and destination of INSN.  If more than one, can't
952      combine.  */
953
954   if (GET_CODE (PATTERN (insn)) == SET)
955     set = PATTERN (insn);
956   else if (GET_CODE (PATTERN (insn)) == PARALLEL
957            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
958     {
959       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
960         {
961           rtx elt = XVECEXP (PATTERN (insn), 0, i);
962
963           switch (GET_CODE (elt))
964             {
965             /* This is important to combine floating point insns
966                for the SH4 port.  */
967             case USE:
968               /* Combining an isolated USE doesn't make sense.
969                  We depend here on combinable_i3pat to reject them.  */
970               /* The code below this loop only verifies that the inputs of
971                  the SET in INSN do not change.  We call reg_set_between_p
972                  to verify that the REG in the USE does not change between
973                  I3 and INSN.
974                  If the USE in INSN was for a pseudo register, the matching
975                  insn pattern will likely match any register; combining this
976                  with any other USE would only be safe if we knew that the
977                  used registers have identical values, or if there was
978                  something to tell them apart, e.g. different modes.  For
979                  now, we forgo such complicated tests and simply disallow
980                  combining of USES of pseudo registers with any other USE.  */
981               if (GET_CODE (XEXP (elt, 0)) == REG
982                   && GET_CODE (PATTERN (i3)) == PARALLEL)
983                 {
984                   rtx i3pat = PATTERN (i3);
985                   int i = XVECLEN (i3pat, 0) - 1;
986                   unsigned int regno = REGNO (XEXP (elt, 0));
987
988                   do
989                     {
990                       rtx i3elt = XVECEXP (i3pat, 0, i);
991
992                       if (GET_CODE (i3elt) == USE
993                           && GET_CODE (XEXP (i3elt, 0)) == REG
994                           && (REGNO (XEXP (i3elt, 0)) == regno
995                               ? reg_set_between_p (XEXP (elt, 0),
996                                                    PREV_INSN (insn), i3)
997                               : regno >= FIRST_PSEUDO_REGISTER))
998                         return 0;
999                     }
1000                   while (--i >= 0);
1001                 }
1002               break;
1003
1004               /* We can ignore CLOBBERs.  */
1005             case CLOBBER:
1006               break;
1007
1008             case SET:
1009               /* Ignore SETs whose result isn't used but not those that
1010                  have side-effects.  */
1011               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1012                   && ! side_effects_p (elt))
1013                 break;
1014
1015               /* If we have already found a SET, this is a second one and
1016                  so we cannot combine with this insn.  */
1017               if (set)
1018                 return 0;
1019
1020               set = elt;
1021               break;
1022
1023             default:
1024               /* Anything else means we can't combine.  */
1025               return 0;
1026             }
1027         }
1028
1029       if (set == 0
1030           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1031              so don't do anything with it.  */
1032           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1033         return 0;
1034     }
1035   else
1036     return 0;
1037
1038   if (set == 0)
1039     return 0;
1040
1041   set = expand_field_assignment (set);
1042   src = SET_SRC (set), dest = SET_DEST (set);
1043
1044   /* Don't eliminate a store in the stack pointer.  */
1045   if (dest == stack_pointer_rtx
1046       /* Don't combine with an insn that sets a register to itself if it has
1047          a REG_EQUAL note.  This may be part of a REG_NO_CONFLICT sequence.  */
1048       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1049       /* Can't merge an ASM_OPERANDS.  */
1050       || GET_CODE (src) == ASM_OPERANDS
1051       /* Can't merge a function call.  */
1052       || GET_CODE (src) == CALL
1053       /* Don't eliminate a function call argument.  */
1054       || (GET_CODE (i3) == CALL_INSN
1055           && (find_reg_fusage (i3, USE, dest)
1056               || (GET_CODE (dest) == REG
1057                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1058                   && global_regs[REGNO (dest)])))
1059       /* Don't substitute into an incremented register.  */
1060       || FIND_REG_INC_NOTE (i3, dest)
1061       || (succ && FIND_REG_INC_NOTE (succ, dest))
1062 #if 0
1063       /* Don't combine the end of a libcall into anything.  */
1064       /* ??? This gives worse code, and appears to be unnecessary, since no
1065          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  Local-alloc does
1066          use REG_RETVAL notes for noconflict blocks, but other code here
1067          makes sure that those insns don't disappear.  */
1068       || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1069 #endif
1070       /* Make sure that DEST is not used after SUCC but before I3.  */
1071       || (succ && ! all_adjacent
1072           && reg_used_between_p (dest, succ, i3))
1073       /* Make sure that the value that is to be substituted for the register
1074          does not use any registers whose values alter in between.  However,
1075          If the insns are adjacent, a use can't cross a set even though we
1076          think it might (this can happen for a sequence of insns each setting
1077          the same destination; reg_last_set of that register might point to
1078          a NOTE).  If INSN has a REG_EQUIV note, the register is always
1079          equivalent to the memory so the substitution is valid even if there
1080          are intervening stores.  Also, don't move a volatile asm or
1081          UNSPEC_VOLATILE across any other insns.  */
1082       || (! all_adjacent
1083           && (((GET_CODE (src) != MEM
1084                 || ! find_reg_note (insn, REG_EQUIV, src))
1085                && use_crosses_set_p (src, INSN_CUID (insn)))
1086               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1087               || GET_CODE (src) == UNSPEC_VOLATILE))
1088       /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1089          better register allocation by not doing the combine.  */
1090       || find_reg_note (i3, REG_NO_CONFLICT, dest)
1091       || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1092       /* Don't combine across a CALL_INSN, because that would possibly
1093          change whether the life span of some REGs crosses calls or not,
1094          and it is a pain to update that information.
1095          Exception: if source is a constant, moving it later can't hurt.
1096          Accept that special case, because it helps -fforce-addr a lot.  */
1097       || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1098     return 0;
1099
1100   /* DEST must either be a REG or CC0.  */
1101   if (GET_CODE (dest) == REG)
1102     {
1103       /* If register alignment is being enforced for multi-word items in all
1104          cases except for parameters, it is possible to have a register copy
1105          insn referencing a hard register that is not allowed to contain the
1106          mode being copied and which would not be valid as an operand of most
1107          insns.  Eliminate this problem by not combining with such an insn.
1108
1109          Also, on some machines we don't want to extend the life of a hard
1110          register.  */
1111
1112       if (GET_CODE (src) == REG
1113           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1114                && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1115               /* Don't extend the life of a hard register unless it is
1116                  user variable (if we have few registers) or it can't
1117                  fit into the desired register (meaning something special
1118                  is going on).
1119                  Also avoid substituting a return register into I3, because
1120                  reload can't handle a conflict with constraints of other
1121                  inputs.  */
1122               || (REGNO (src) < FIRST_PSEUDO_REGISTER
1123                   && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1124         return 0;
1125     }
1126   else if (GET_CODE (dest) != CC0)
1127     return 0;
1128
1129   /* Don't substitute for a register intended as a clobberable operand.
1130      Similarly, don't substitute an expression containing a register that
1131      will be clobbered in I3.  */
1132   if (GET_CODE (PATTERN (i3)) == PARALLEL)
1133     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1134       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1135           && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1136                                        src)
1137               || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1138         return 0;
1139
1140   /* If INSN contains anything volatile, or is an `asm' (whether volatile
1141      or not), reject, unless nothing volatile comes between it and I3 */
1142
1143   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1144     {
1145       /* Make sure succ doesn't contain a volatile reference.  */
1146       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1147         return 0;
1148
1149       for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1150         if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1151           return 0;
1152     }
1153
1154   /* If INSN is an asm, and DEST is a hard register, reject, since it has
1155      to be an explicit register variable, and was chosen for a reason.  */
1156
1157   if (GET_CODE (src) == ASM_OPERANDS
1158       && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1159     return 0;
1160
1161   /* If there are any volatile insns between INSN and I3, reject, because
1162      they might affect machine state.  */
1163
1164   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1165     if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1166       return 0;
1167
1168   /* If INSN or I2 contains an autoincrement or autodecrement,
1169      make sure that register is not used between there and I3,
1170      and not already used in I3 either.
1171      Also insist that I3 not be a jump; if it were one
1172      and the incremented register were spilled, we would lose.  */
1173
1174 #ifdef AUTO_INC_DEC
1175   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1176     if (REG_NOTE_KIND (link) == REG_INC
1177         && (GET_CODE (i3) == JUMP_INSN
1178             || reg_used_between_p (XEXP (link, 0), insn, i3)
1179             || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1180       return 0;
1181 #endif
1182
1183 #ifdef HAVE_cc0
1184   /* Don't combine an insn that follows a CC0-setting insn.
1185      An insn that uses CC0 must not be separated from the one that sets it.
1186      We do, however, allow I2 to follow a CC0-setting insn if that insn
1187      is passed as I1; in that case it will be deleted also.
1188      We also allow combining in this case if all the insns are adjacent
1189      because that would leave the two CC0 insns adjacent as well.
1190      It would be more logical to test whether CC0 occurs inside I1 or I2,
1191      but that would be much slower, and this ought to be equivalent.  */
1192
1193   p = prev_nonnote_insn (insn);
1194   if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1195       && ! all_adjacent)
1196     return 0;
1197 #endif
1198
1199   /* If we get here, we have passed all the tests and the combination is
1200      to be allowed.  */
1201
1202   *pdest = dest;
1203   *psrc = src;
1204
1205   return 1;
1206 }
1207 \f
1208 /* Check if PAT is an insn - or a part of it - used to set up an
1209    argument for a function in a hard register.  */
1210
1211 static int
1212 sets_function_arg_p (rtx pat)
1213 {
1214   int i;
1215   rtx inner_dest;
1216
1217   switch (GET_CODE (pat))
1218     {
1219     case INSN:
1220       return sets_function_arg_p (PATTERN (pat));
1221
1222     case PARALLEL:
1223       for (i = XVECLEN (pat, 0); --i >= 0;)
1224         if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1225           return 1;
1226
1227       break;
1228
1229     case SET:
1230       inner_dest = SET_DEST (pat);
1231       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1232              || GET_CODE (inner_dest) == SUBREG
1233              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1234         inner_dest = XEXP (inner_dest, 0);
1235
1236       return (GET_CODE (inner_dest) == REG
1237               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1238               && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1239
1240     default:
1241       break;
1242     }
1243
1244   return 0;
1245 }
1246
1247 /* LOC is the location within I3 that contains its pattern or the component
1248    of a PARALLEL of the pattern.  We validate that it is valid for combining.
1249
1250    One problem is if I3 modifies its output, as opposed to replacing it
1251    entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1252    so would produce an insn that is not equivalent to the original insns.
1253
1254    Consider:
1255
1256          (set (reg:DI 101) (reg:DI 100))
1257          (set (subreg:SI (reg:DI 101) 0) <foo>)
1258
1259    This is NOT equivalent to:
1260
1261          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1262                     (set (reg:DI 101) (reg:DI 100))])
1263
1264    Not only does this modify 100 (in which case it might still be valid
1265    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1266
1267    We can also run into a problem if I2 sets a register that I1
1268    uses and I1 gets directly substituted into I3 (not via I2).  In that
1269    case, we would be getting the wrong value of I2DEST into I3, so we
1270    must reject the combination.  This case occurs when I2 and I1 both
1271    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1272    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1273    of a SET must prevent combination from occurring.
1274
1275    Before doing the above check, we first try to expand a field assignment
1276    into a set of logical operations.
1277
1278    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1279    we place a register that is both set and used within I3.  If more than one
1280    such register is detected, we fail.
1281
1282    Return 1 if the combination is valid, zero otherwise.  */
1283
1284 static int
1285 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1286                   int i1_not_in_src, rtx *pi3dest_killed)
1287 {
1288   rtx x = *loc;
1289
1290   if (GET_CODE (x) == SET)
1291     {
1292       rtx set = x ;
1293       rtx dest = SET_DEST (set);
1294       rtx src = SET_SRC (set);
1295       rtx inner_dest = dest;
1296
1297       while (GET_CODE (inner_dest) == STRICT_LOW_PART
1298              || GET_CODE (inner_dest) == SUBREG
1299              || GET_CODE (inner_dest) == ZERO_EXTRACT)
1300         inner_dest = XEXP (inner_dest, 0);
1301
1302       /* Check for the case where I3 modifies its output, as discussed
1303          above.  We don't want to prevent pseudos from being combined
1304          into the address of a MEM, so only prevent the combination if
1305          i1 or i2 set the same MEM.  */
1306       if ((inner_dest != dest &&
1307            (GET_CODE (inner_dest) != MEM
1308             || rtx_equal_p (i2dest, inner_dest)
1309             || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1310            && (reg_overlap_mentioned_p (i2dest, inner_dest)
1311                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1312
1313           /* This is the same test done in can_combine_p except we can't test
1314              all_adjacent; we don't have to, since this instruction will stay
1315              in place, thus we are not considering increasing the lifetime of
1316              INNER_DEST.
1317
1318              Also, if this insn sets a function argument, combining it with
1319              something that might need a spill could clobber a previous
1320              function argument; the all_adjacent test in can_combine_p also
1321              checks this; here, we do a more specific test for this case.  */
1322
1323           || (GET_CODE (inner_dest) == REG
1324               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1325               && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1326                                         GET_MODE (inner_dest))))
1327           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1328         return 0;
1329
1330       /* If DEST is used in I3, it is being killed in this insn,
1331          so record that for later.
1332          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1333          STACK_POINTER_REGNUM, since these are always considered to be
1334          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
1335       if (pi3dest_killed && GET_CODE (dest) == REG
1336           && reg_referenced_p (dest, PATTERN (i3))
1337           && REGNO (dest) != FRAME_POINTER_REGNUM
1338 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1339           && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1340 #endif
1341 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1342           && (REGNO (dest) != ARG_POINTER_REGNUM
1343               || ! fixed_regs [REGNO (dest)])
1344 #endif
1345           && REGNO (dest) != STACK_POINTER_REGNUM)
1346         {
1347           if (*pi3dest_killed)
1348             return 0;
1349
1350           *pi3dest_killed = dest;
1351         }
1352     }
1353
1354   else if (GET_CODE (x) == PARALLEL)
1355     {
1356       int i;
1357
1358       for (i = 0; i < XVECLEN (x, 0); i++)
1359         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1360                                 i1_not_in_src, pi3dest_killed))
1361           return 0;
1362     }
1363
1364   return 1;
1365 }
1366 \f
1367 /* Return 1 if X is an arithmetic expression that contains a multiplication
1368    and division.  We don't count multiplications by powers of two here.  */
1369
1370 static int
1371 contains_muldiv (rtx x)
1372 {
1373   switch (GET_CODE (x))
1374     {
1375     case MOD:  case DIV:  case UMOD:  case UDIV:
1376       return 1;
1377
1378     case MULT:
1379       return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1380                 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1381     default:
1382       switch (GET_RTX_CLASS (GET_CODE (x)))
1383         {
1384         case 'c':  case '<':  case '2':
1385           return contains_muldiv (XEXP (x, 0))
1386             || contains_muldiv (XEXP (x, 1));
1387
1388         case '1':
1389           return contains_muldiv (XEXP (x, 0));
1390
1391         default:
1392           return 0;
1393         }
1394     }
1395 }
1396 \f
1397 /* Determine whether INSN can be used in a combination.  Return nonzero if
1398    not.  This is used in try_combine to detect early some cases where we
1399    can't perform combinations.  */
1400
1401 static int
1402 cant_combine_insn_p (rtx insn)
1403 {
1404   rtx set;
1405   rtx src, dest;
1406
1407   /* If this isn't really an insn, we can't do anything.
1408      This can occur when flow deletes an insn that it has merged into an
1409      auto-increment address.  */
1410   if (! INSN_P (insn))
1411     return 1;
1412
1413   /* Never combine loads and stores involving hard regs that are likely
1414      to be spilled.  The register allocator can usually handle such
1415      reg-reg moves by tying.  If we allow the combiner to make
1416      substitutions of likely-spilled regs, we may abort in reload.
1417      As an exception, we allow combinations involving fixed regs; these are
1418      not available to the register allocator so there's no risk involved.  */
1419
1420   set = single_set (insn);
1421   if (! set)
1422     return 0;
1423   src = SET_SRC (set);
1424   dest = SET_DEST (set);
1425   if (GET_CODE (src) == SUBREG)
1426     src = SUBREG_REG (src);
1427   if (GET_CODE (dest) == SUBREG)
1428     dest = SUBREG_REG (dest);
1429   if (REG_P (src) && REG_P (dest)
1430       && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1431            && ! fixed_regs[REGNO (src)]
1432            && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1433           || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1434               && ! fixed_regs[REGNO (dest)]
1435               && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1436     return 1;
1437
1438   return 0;
1439 }
1440
1441 /* Adjust INSN after we made a change to its destination.
1442
1443    Changing the destination can invalidate notes that say something about
1444    the results of the insn and a LOG_LINK pointing to the insn.  */
1445
1446 static void
1447 adjust_for_new_dest (rtx insn)
1448 {
1449   rtx *loc;
1450
1451   /* For notes, be conservative and simply remove them.  */
1452   loc = &REG_NOTES (insn);
1453   while (*loc)
1454     {
1455       enum reg_note kind = REG_NOTE_KIND (*loc);
1456       if (kind == REG_EQUAL || kind == REG_EQUIV)
1457         *loc = XEXP (*loc, 1);
1458       else
1459         loc = &XEXP (*loc, 1);
1460     }
1461
1462   /* The new insn will have a destination that was previously the destination
1463      of an insn just above it.  Call distribute_links to make a LOG_LINK from
1464      the next use of that destination.  */
1465   distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1466 }
1467
1468 /* Try to combine the insns I1 and I2 into I3.
1469    Here I1 and I2 appear earlier than I3.
1470    I1 can be zero; then we combine just I2 into I3.
1471
1472    If we are combining three insns and the resulting insn is not recognized,
1473    try splitting it into two insns.  If that happens, I2 and I3 are retained
1474    and I1 is pseudo-deleted by turning it into a NOTE.  Otherwise, I1 and I2
1475    are pseudo-deleted.
1476
1477    Return 0 if the combination does not work.  Then nothing is changed.
1478    If we did the combination, return the insn at which combine should
1479    resume scanning.
1480
1481    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1482    new direct jump instruction.  */
1483
1484 static rtx
1485 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1486 {
1487   /* New patterns for I3 and I2, respectively.  */
1488   rtx newpat, newi2pat = 0;
1489   int substed_i2 = 0, substed_i1 = 0;
1490   /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead.  */
1491   int added_sets_1, added_sets_2;
1492   /* Total number of SETs to put into I3.  */
1493   int total_sets;
1494   /* Nonzero is I2's body now appears in I3.  */
1495   int i2_is_used;
1496   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
1497   int insn_code_number, i2_code_number = 0, other_code_number = 0;
1498   /* Contains I3 if the destination of I3 is used in its source, which means
1499      that the old life of I3 is being killed.  If that usage is placed into
1500      I2 and not in I3, a REG_DEAD note must be made.  */
1501   rtx i3dest_killed = 0;
1502   /* SET_DEST and SET_SRC of I2 and I1.  */
1503   rtx i2dest, i2src, i1dest = 0, i1src = 0;
1504   /* PATTERN (I2), or a copy of it in certain cases.  */
1505   rtx i2pat;
1506   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
1507   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1508   int i1_feeds_i3 = 0;
1509   /* Notes that must be added to REG_NOTES in I3 and I2.  */
1510   rtx new_i3_notes, new_i2_notes;
1511   /* Notes that we substituted I3 into I2 instead of the normal case.  */
1512   int i3_subst_into_i2 = 0;
1513   /* Notes that I1, I2 or I3 is a MULT operation.  */
1514   int have_mult = 0;
1515
1516   int maxreg;
1517   rtx temp;
1518   rtx link;
1519   int i;
1520
1521   /* Exit early if one of the insns involved can't be used for
1522      combinations.  */
1523   if (cant_combine_insn_p (i3)
1524       || cant_combine_insn_p (i2)
1525       || (i1 && cant_combine_insn_p (i1))
1526       /* We also can't do anything if I3 has a
1527          REG_LIBCALL note since we don't want to disrupt the contiguity of a
1528          libcall.  */
1529 #if 0
1530       /* ??? This gives worse code, and appears to be unnecessary, since no
1531          pass after flow uses REG_LIBCALL/REG_RETVAL notes.  */
1532       || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1533 #endif
1534       )
1535     return 0;
1536
1537   combine_attempts++;
1538   undobuf.other_insn = 0;
1539
1540   /* Reset the hard register usage information.  */
1541   CLEAR_HARD_REG_SET (newpat_used_regs);
1542
1543   /* If I1 and I2 both feed I3, they can be in any order.  To simplify the
1544      code below, set I1 to be the earlier of the two insns.  */
1545   if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1546     temp = i1, i1 = i2, i2 = temp;
1547
1548   added_links_insn = 0;
1549
1550   /* First check for one important special-case that the code below will
1551      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
1552      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
1553      we may be able to replace that destination with the destination of I3.
1554      This occurs in the common code where we compute both a quotient and
1555      remainder into a structure, in which case we want to do the computation
1556      directly into the structure to avoid register-register copies.
1557
1558      Note that this case handles both multiple sets in I2 and also
1559      cases where I2 has a number of CLOBBER or PARALLELs.
1560
1561      We make very conservative checks below and only try to handle the
1562      most common cases of this.  For example, we only handle the case
1563      where I2 and I3 are adjacent to avoid making difficult register
1564      usage tests.  */
1565
1566   if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1567       && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1568       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1569       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1570       && GET_CODE (PATTERN (i2)) == PARALLEL
1571       && ! side_effects_p (SET_DEST (PATTERN (i3)))
1572       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1573          below would need to check what is inside (and reg_overlap_mentioned_p
1574          doesn't support those codes anyway).  Don't allow those destinations;
1575          the resulting insn isn't likely to be recognized anyway.  */
1576       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1577       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1578       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1579                                     SET_DEST (PATTERN (i3)))
1580       && next_real_insn (i2) == i3)
1581     {
1582       rtx p2 = PATTERN (i2);
1583
1584       /* Make sure that the destination of I3,
1585          which we are going to substitute into one output of I2,
1586          is not used within another output of I2.  We must avoid making this:
1587          (parallel [(set (mem (reg 69)) ...)
1588                     (set (reg 69) ...)])
1589          which is not well-defined as to order of actions.
1590          (Besides, reload can't handle output reloads for this.)
1591
1592          The problem can also happen if the dest of I3 is a memory ref,
1593          if another dest in I2 is an indirect memory ref.  */
1594       for (i = 0; i < XVECLEN (p2, 0); i++)
1595         if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1596              || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1597             && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1598                                         SET_DEST (XVECEXP (p2, 0, i))))
1599           break;
1600
1601       if (i == XVECLEN (p2, 0))
1602         for (i = 0; i < XVECLEN (p2, 0); i++)
1603           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1604                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1605               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1606             {
1607               combine_merges++;
1608
1609               subst_insn = i3;
1610               subst_low_cuid = INSN_CUID (i2);
1611
1612               added_sets_2 = added_sets_1 = 0;
1613               i2dest = SET_SRC (PATTERN (i3));
1614
1615               /* Replace the dest in I2 with our dest and make the resulting
1616                  insn the new pattern for I3.  Then skip to where we
1617                  validate the pattern.  Everything was set up above.  */
1618               SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1619                      SET_DEST (PATTERN (i3)));
1620
1621               newpat = p2;
1622               i3_subst_into_i2 = 1;
1623               goto validate_replacement;
1624             }
1625     }
1626
1627   /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1628      one of those words to another constant, merge them by making a new
1629      constant.  */
1630   if (i1 == 0
1631       && (temp = single_set (i2)) != 0
1632       && (GET_CODE (SET_SRC (temp)) == CONST_INT
1633           || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1634       && GET_CODE (SET_DEST (temp)) == REG
1635       && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1636       && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1637       && GET_CODE (PATTERN (i3)) == SET
1638       && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1639       && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1640       && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1641       && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1642       && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1643     {
1644       HOST_WIDE_INT lo, hi;
1645
1646       if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1647         lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1648       else
1649         {
1650           lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1651           hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1652         }
1653
1654       if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1655         {
1656           /* We don't handle the case of the target word being wider
1657              than a host wide int.  */
1658           if (HOST_BITS_PER_WIDE_INT < BITS_PER_WORD)
1659             abort ();
1660
1661           lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1662           lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1663                  & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1664         }
1665       else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1666         hi = INTVAL (SET_SRC (PATTERN (i3)));
1667       else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1668         {
1669           int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1670                              >> (HOST_BITS_PER_WIDE_INT - 1));
1671
1672           lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1673                    (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1674           lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1675                  (INTVAL (SET_SRC (PATTERN (i3)))));
1676           if (hi == sign)
1677             hi = lo < 0 ? -1 : 0;
1678         }
1679       else
1680         /* We don't handle the case of the higher word not fitting
1681            entirely in either hi or lo.  */
1682         abort ();
1683
1684       combine_merges++;
1685       subst_insn = i3;
1686       subst_low_cuid = INSN_CUID (i2);
1687       added_sets_2 = added_sets_1 = 0;
1688       i2dest = SET_DEST (temp);
1689
1690       SUBST (SET_SRC (temp),
1691              immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1692
1693       newpat = PATTERN (i2);
1694       goto validate_replacement;
1695     }
1696
1697 #ifndef HAVE_cc0
1698   /* If we have no I1 and I2 looks like:
1699         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1700                    (set Y OP)])
1701      make up a dummy I1 that is
1702         (set Y OP)
1703      and change I2 to be
1704         (set (reg:CC X) (compare:CC Y (const_int 0)))
1705
1706      (We can ignore any trailing CLOBBERs.)
1707
1708      This undoes a previous combination and allows us to match a branch-and-
1709      decrement insn.  */
1710
1711   if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1712       && XVECLEN (PATTERN (i2), 0) >= 2
1713       && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1714       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1715           == MODE_CC)
1716       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1717       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1718       && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1719       && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1720       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1721                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1722     {
1723       for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1724         if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1725           break;
1726
1727       if (i == 1)
1728         {
1729           /* We make I1 with the same INSN_UID as I2.  This gives it
1730              the same INSN_CUID for value tracking.  Our fake I1 will
1731              never appear in the insn stream so giving it the same INSN_UID
1732              as I2 will not cause a problem.  */
1733
1734           i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1735                              BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1736                              XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1737                              NULL_RTX);
1738
1739           SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1740           SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1741                  SET_DEST (PATTERN (i1)));
1742         }
1743     }
1744 #endif
1745
1746   /* Verify that I2 and I1 are valid for combining.  */
1747   if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1748       || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1749     {
1750       undo_all ();
1751       return 0;
1752     }
1753
1754   /* Record whether I2DEST is used in I2SRC and similarly for the other
1755      cases.  Knowing this will help in register status updating below.  */
1756   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1757   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1758   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1759
1760   /* See if I1 directly feeds into I3.  It does if I1DEST is not used
1761      in I2SRC.  */
1762   i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1763
1764   /* Ensure that I3's pattern can be the destination of combines.  */
1765   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1766                           i1 && i2dest_in_i1src && i1_feeds_i3,
1767                           &i3dest_killed))
1768     {
1769       undo_all ();
1770       return 0;
1771     }
1772
1773   /* See if any of the insns is a MULT operation.  Unless one is, we will
1774      reject a combination that is, since it must be slower.  Be conservative
1775      here.  */
1776   if (GET_CODE (i2src) == MULT
1777       || (i1 != 0 && GET_CODE (i1src) == MULT)
1778       || (GET_CODE (PATTERN (i3)) == SET
1779           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1780     have_mult = 1;
1781
1782   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1783      We used to do this EXCEPT in one case: I3 has a post-inc in an
1784      output operand.  However, that exception can give rise to insns like
1785         mov r3,(r3)+
1786      which is a famous insn on the PDP-11 where the value of r3 used as the
1787      source was model-dependent.  Avoid this sort of thing.  */
1788
1789 #if 0
1790   if (!(GET_CODE (PATTERN (i3)) == SET
1791         && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1792         && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1793         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1794             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1795     /* It's not the exception.  */
1796 #endif
1797 #ifdef AUTO_INC_DEC
1798     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1799       if (REG_NOTE_KIND (link) == REG_INC
1800           && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1801               || (i1 != 0
1802                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1803         {
1804           undo_all ();
1805           return 0;
1806         }
1807 #endif
1808
1809   /* See if the SETs in I1 or I2 need to be kept around in the merged
1810      instruction: whenever the value set there is still needed past I3.
1811      For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1812
1813      For the SET in I1, we have two cases:  If I1 and I2 independently
1814      feed into I3, the set in I1 needs to be kept around if I1DEST dies
1815      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
1816      in I1 needs to be kept around unless I1DEST dies or is set in either
1817      I2 or I3.  We can distinguish these cases by seeing if I2SRC mentions
1818      I1DEST.  If so, we know I1 feeds into I2.  */
1819
1820   added_sets_2 = ! dead_or_set_p (i3, i2dest);
1821
1822   added_sets_1
1823     = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1824                : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1825
1826   /* If the set in I2 needs to be kept around, we must make a copy of
1827      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1828      PATTERN (I2), we are only substituting for the original I1DEST, not into
1829      an already-substituted copy.  This also prevents making self-referential
1830      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1831      I2DEST.  */
1832
1833   i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1834            ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1835            : PATTERN (i2));
1836
1837   if (added_sets_2)
1838     i2pat = copy_rtx (i2pat);
1839
1840   combine_merges++;
1841
1842   /* Substitute in the latest insn for the regs set by the earlier ones.  */
1843
1844   maxreg = max_reg_num ();
1845
1846   subst_insn = i3;
1847
1848   /* It is possible that the source of I2 or I1 may be performing an
1849      unneeded operation, such as a ZERO_EXTEND of something that is known
1850      to have the high part zero.  Handle that case by letting subst look at
1851      the innermost one of them.
1852
1853      Another way to do this would be to have a function that tries to
1854      simplify a single insn instead of merging two or more insns.  We don't
1855      do this because of the potential of infinite loops and because
1856      of the potential extra memory required.  However, doing it the way
1857      we are is a bit of a kludge and doesn't catch all cases.
1858
1859      But only do this if -fexpensive-optimizations since it slows things down
1860      and doesn't usually win.  */
1861
1862   if (flag_expensive_optimizations)
1863     {
1864       /* Pass pc_rtx so no substitutions are done, just simplifications.
1865          The cases that we are interested in here do not involve the few
1866          cases were is_replaced is checked.  */
1867       if (i1)
1868         {
1869           subst_low_cuid = INSN_CUID (i1);
1870           i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1871         }
1872       else
1873         {
1874           subst_low_cuid = INSN_CUID (i2);
1875           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1876         }
1877     }
1878
1879 #ifndef HAVE_cc0
1880   /* Many machines that don't use CC0 have insns that can both perform an
1881      arithmetic operation and set the condition code.  These operations will
1882      be represented as a PARALLEL with the first element of the vector
1883      being a COMPARE of an arithmetic operation with the constant zero.
1884      The second element of the vector will set some pseudo to the result
1885      of the same arithmetic operation.  If we simplify the COMPARE, we won't
1886      match such a pattern and so will generate an extra insn.   Here we test
1887      for this case, where both the comparison and the operation result are
1888      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1889      I2SRC.  Later we will make the PARALLEL that contains I2.  */
1890
1891   if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1892       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1893       && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1894       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1895     {
1896 #ifdef EXTRA_CC_MODES
1897       rtx *cc_use;
1898       enum machine_mode compare_mode;
1899 #endif
1900
1901       newpat = PATTERN (i3);
1902       SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1903
1904       i2_is_used = 1;
1905
1906 #ifdef EXTRA_CC_MODES
1907       /* See if a COMPARE with the operand we substituted in should be done
1908          with the mode that is currently being used.  If not, do the same
1909          processing we do in `subst' for a SET; namely, if the destination
1910          is used only once, try to replace it with a register of the proper
1911          mode and also replace the COMPARE.  */
1912       if (undobuf.other_insn == 0
1913           && (cc_use = find_single_use (SET_DEST (newpat), i3,
1914                                         &undobuf.other_insn))
1915           && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1916                                               i2src, const0_rtx))
1917               != GET_MODE (SET_DEST (newpat))))
1918         {
1919           unsigned int regno = REGNO (SET_DEST (newpat));
1920           rtx new_dest = gen_rtx_REG (compare_mode, regno);
1921
1922           if (regno < FIRST_PSEUDO_REGISTER
1923               || (REG_N_SETS (regno) == 1 && ! added_sets_2
1924                   && ! REG_USERVAR_P (SET_DEST (newpat))))
1925             {
1926               if (regno >= FIRST_PSEUDO_REGISTER)
1927                 SUBST (regno_reg_rtx[regno], new_dest);
1928
1929               SUBST (SET_DEST (newpat), new_dest);
1930               SUBST (XEXP (*cc_use, 0), new_dest);
1931               SUBST (SET_SRC (newpat),
1932                      gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
1933             }
1934           else
1935             undobuf.other_insn = 0;
1936         }
1937 #endif
1938     }
1939   else
1940 #endif
1941     {
1942       n_occurrences = 0;                /* `subst' counts here */
1943
1944       /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1945          need to make a unique copy of I2SRC each time we substitute it
1946          to avoid self-referential rtl.  */
1947
1948       subst_low_cuid = INSN_CUID (i2);
1949       newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1950                       ! i1_feeds_i3 && i1dest_in_i1src);
1951       substed_i2 = 1;
1952
1953       /* Record whether i2's body now appears within i3's body.  */
1954       i2_is_used = n_occurrences;
1955     }
1956
1957   /* If we already got a failure, don't try to do more.  Otherwise,
1958      try to substitute in I1 if we have it.  */
1959
1960   if (i1 && GET_CODE (newpat) != CLOBBER)
1961     {
1962       /* Before we can do this substitution, we must redo the test done
1963          above (see detailed comments there) that ensures  that I1DEST
1964          isn't mentioned in any SETs in NEWPAT that are field assignments.  */
1965
1966       if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1967                               0, (rtx*) 0))
1968         {
1969           undo_all ();
1970           return 0;
1971         }
1972
1973       n_occurrences = 0;
1974       subst_low_cuid = INSN_CUID (i1);
1975       newpat = subst (newpat, i1dest, i1src, 0, 0);
1976       substed_i1 = 1;
1977     }
1978
1979   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
1980      to count all the ways that I2SRC and I1SRC can be used.  */
1981   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1982        && i2_is_used + added_sets_2 > 1)
1983       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1984           && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1985               > 1))
1986       /* Fail if we tried to make a new register (we used to abort, but there's
1987          really no reason to).  */
1988       || max_reg_num () != maxreg
1989       /* Fail if we couldn't do something and have a CLOBBER.  */
1990       || GET_CODE (newpat) == CLOBBER
1991       /* Fail if this new pattern is a MULT and we didn't have one before
1992          at the outer level.  */
1993       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1994           && ! have_mult))
1995     {
1996       undo_all ();
1997       return 0;
1998     }
1999
2000   /* If the actions of the earlier insns must be kept
2001      in addition to substituting them into the latest one,
2002      we must make a new PARALLEL for the latest insn
2003      to hold additional the SETs.  */
2004
2005   if (added_sets_1 || added_sets_2)
2006     {
2007       combine_extras++;
2008
2009       if (GET_CODE (newpat) == PARALLEL)
2010         {
2011           rtvec old = XVEC (newpat, 0);
2012           total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2013           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2014           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2015                   sizeof (old->elem[0]) * old->num_elem);
2016         }
2017       else
2018         {
2019           rtx old = newpat;
2020           total_sets = 1 + added_sets_1 + added_sets_2;
2021           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2022           XVECEXP (newpat, 0, 0) = old;
2023         }
2024
2025       if (added_sets_1)
2026         XVECEXP (newpat, 0, --total_sets)
2027           = (GET_CODE (PATTERN (i1)) == PARALLEL
2028              ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2029
2030       if (added_sets_2)
2031         {
2032           /* If there is no I1, use I2's body as is.  We used to also not do
2033              the subst call below if I2 was substituted into I3,
2034              but that could lose a simplification.  */
2035           if (i1 == 0)
2036             XVECEXP (newpat, 0, --total_sets) = i2pat;
2037           else
2038             /* See comment where i2pat is assigned.  */
2039             XVECEXP (newpat, 0, --total_sets)
2040               = subst (i2pat, i1dest, i1src, 0, 0);
2041         }
2042     }
2043
2044   /* We come here when we are replacing a destination in I2 with the
2045      destination of I3.  */
2046  validate_replacement:
2047
2048   /* Note which hard regs this insn has as inputs.  */
2049   mark_used_regs_combine (newpat);
2050
2051   /* Is the result of combination a valid instruction?  */
2052   insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2053
2054   /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2055      the second SET's destination is a register that is unused.  In that case,
2056      we just need the first SET.   This can occur when simplifying a divmod
2057      insn.  We *must* test for this case here because the code below that
2058      splits two independent SETs doesn't handle this case correctly when it
2059      updates the register status.  Also check the case where the first
2060      SET's destination is unused.  That would not cause incorrect code, but
2061      does cause an unneeded insn to remain.  */
2062
2063   if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2064       && XVECLEN (newpat, 0) == 2
2065       && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2066       && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2067       && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
2068       && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
2069       && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
2070       && asm_noperands (newpat) < 0)
2071     {
2072       newpat = XVECEXP (newpat, 0, 0);
2073       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2074     }
2075
2076   else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2077            && XVECLEN (newpat, 0) == 2
2078            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2079            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2080            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
2081            && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
2082            && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
2083            && asm_noperands (newpat) < 0)
2084     {
2085       newpat = XVECEXP (newpat, 0, 1);
2086       insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2087  
2088       if (insn_code_number >= 0)
2089         {
2090           /* If we will be able to accept this, we have made a change to the
2091              destination of I3.  This requires us to do a few adjustments.  */
2092           PATTERN (i3) = newpat;
2093           adjust_for_new_dest (i3);
2094         }
2095     }
2096
2097   /* If we were combining three insns and the result is a simple SET
2098      with no ASM_OPERANDS that wasn't recognized, try to split it into two
2099      insns.  There are two ways to do this.  It can be split using a
2100      machine-specific method (like when you have an addition of a large
2101      constant) or by combine in the function find_split_point.  */
2102
2103   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2104       && asm_noperands (newpat) < 0)
2105     {
2106       rtx m_split, *split;
2107       rtx ni2dest = i2dest;
2108
2109       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
2110          use I2DEST as a scratch register will help.  In the latter case,
2111          convert I2DEST to the mode of the source of NEWPAT if we can.  */
2112
2113       m_split = split_insns (newpat, i3);
2114
2115       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2116          inputs of NEWPAT.  */
2117
2118       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2119          possible to try that as a scratch reg.  This would require adding
2120          more code to make it work though.  */
2121
2122       if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2123         {
2124           /* If I2DEST is a hard register or the only use of a pseudo,
2125              we can change its mode.  */
2126           if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2127               && GET_MODE (SET_DEST (newpat)) != VOIDmode
2128               && GET_CODE (i2dest) == REG
2129               && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2130                   || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2131                       && ! REG_USERVAR_P (i2dest))))
2132             ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2133                                    REGNO (i2dest));
2134
2135           m_split = split_insns (gen_rtx_PARALLEL
2136                                  (VOIDmode,
2137                                   gen_rtvec (2, newpat,
2138                                              gen_rtx_CLOBBER (VOIDmode,
2139                                                               ni2dest))),
2140                                  i3);
2141           /* If the split with the mode-changed register didn't work, try
2142              the original register.  */
2143           if (! m_split && ni2dest != i2dest)
2144             {
2145               ni2dest = i2dest;
2146               m_split = split_insns (gen_rtx_PARALLEL
2147                                      (VOIDmode,
2148                                       gen_rtvec (2, newpat,
2149                                                  gen_rtx_CLOBBER (VOIDmode,
2150                                                                   i2dest))),
2151                                      i3);
2152             }
2153         }
2154
2155       if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2156         {
2157           m_split = PATTERN (m_split);
2158           insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2159           if (insn_code_number >= 0)
2160             newpat = m_split;
2161         }
2162       else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2163                && (next_real_insn (i2) == i3
2164                    || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2165         {
2166           rtx i2set, i3set;
2167           rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2168           newi2pat = PATTERN (m_split);
2169
2170           i3set = single_set (NEXT_INSN (m_split));
2171           i2set = single_set (m_split);
2172
2173           /* In case we changed the mode of I2DEST, replace it in the
2174              pseudo-register table here.  We can't do it above in case this
2175              code doesn't get executed and we do a split the other way.  */
2176
2177           if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2178             SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2179
2180           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2181
2182           /* If I2 or I3 has multiple SETs, we won't know how to track
2183              register status, so don't use these insns.  If I2's destination
2184              is used between I2 and I3, we also can't use these insns.  */
2185
2186           if (i2_code_number >= 0 && i2set && i3set
2187               && (next_real_insn (i2) == i3
2188                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2189             insn_code_number = recog_for_combine (&newi3pat, i3,
2190                                                   &new_i3_notes);
2191           if (insn_code_number >= 0)
2192             newpat = newi3pat;
2193
2194           /* It is possible that both insns now set the destination of I3.
2195              If so, we must show an extra use of it.  */
2196
2197           if (insn_code_number >= 0)
2198             {
2199               rtx new_i3_dest = SET_DEST (i3set);
2200               rtx new_i2_dest = SET_DEST (i2set);
2201
2202               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2203                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2204                      || GET_CODE (new_i3_dest) == SUBREG)
2205                 new_i3_dest = XEXP (new_i3_dest, 0);
2206
2207               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2208                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2209                      || GET_CODE (new_i2_dest) == SUBREG)
2210                 new_i2_dest = XEXP (new_i2_dest, 0);
2211
2212               if (GET_CODE (new_i3_dest) == REG
2213                   && GET_CODE (new_i2_dest) == REG
2214                   && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2215                 REG_N_SETS (REGNO (new_i2_dest))++;
2216             }
2217         }
2218
2219       /* If we can split it and use I2DEST, go ahead and see if that
2220          helps things be recognized.  Verify that none of the registers
2221          are set between I2 and I3.  */
2222       if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2223 #ifdef HAVE_cc0
2224           && GET_CODE (i2dest) == REG
2225 #endif
2226           /* We need I2DEST in the proper mode.  If it is a hard register
2227              or the only use of a pseudo, we can change its mode.  */
2228           && (GET_MODE (*split) == GET_MODE (i2dest)
2229               || GET_MODE (*split) == VOIDmode
2230               || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2231               || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2232                   && ! REG_USERVAR_P (i2dest)))
2233           && (next_real_insn (i2) == i3
2234               || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2235           /* We can't overwrite I2DEST if its value is still used by
2236              NEWPAT.  */
2237           && ! reg_referenced_p (i2dest, newpat))
2238         {
2239           rtx newdest = i2dest;
2240           enum rtx_code split_code = GET_CODE (*split);
2241           enum machine_mode split_mode = GET_MODE (*split);
2242
2243           /* Get NEWDEST as a register in the proper mode.  We have already
2244              validated that we can do this.  */
2245           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2246             {
2247               newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2248
2249               if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2250                 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2251             }
2252
2253           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2254              an ASHIFT.  This can occur if it was inside a PLUS and hence
2255              appeared to be a memory address.  This is a kludge.  */
2256           if (split_code == MULT
2257               && GET_CODE (XEXP (*split, 1)) == CONST_INT
2258               && INTVAL (XEXP (*split, 1)) > 0
2259               && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2260             {
2261               SUBST (*split, gen_rtx_ASHIFT (split_mode,
2262                                              XEXP (*split, 0), GEN_INT (i)));
2263               /* Update split_code because we may not have a multiply
2264                  anymore.  */
2265               split_code = GET_CODE (*split);
2266             }
2267
2268 #ifdef INSN_SCHEDULING
2269           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2270              be written as a ZERO_EXTEND.  */
2271           if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2272             {
2273 #ifdef LOAD_EXTEND_OP
2274               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2275                  what it really is.  */
2276               if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2277                   == SIGN_EXTEND)
2278                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2279                                                     SUBREG_REG (*split)));
2280               else
2281 #endif
2282                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2283                                                     SUBREG_REG (*split)));
2284             }
2285 #endif
2286
2287           newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2288           SUBST (*split, newdest);
2289           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2290
2291           /* If the split point was a MULT and we didn't have one before,
2292              don't use one now.  */
2293           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2294             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2295         }
2296     }
2297
2298   /* Check for a case where we loaded from memory in a narrow mode and
2299      then sign extended it, but we need both registers.  In that case,
2300      we have a PARALLEL with both loads from the same memory location.
2301      We can split this into a load from memory followed by a register-register
2302      copy.  This saves at least one insn, more if register allocation can
2303      eliminate the copy.
2304
2305      We cannot do this if the destination of the first assignment is a
2306      condition code register or cc0.  We eliminate this case by making sure
2307      the SET_DEST and SET_SRC have the same mode.
2308
2309      We cannot do this if the destination of the second assignment is
2310      a register that we have already assumed is zero-extended.  Similarly
2311      for a SUBREG of such a register.  */
2312
2313   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2314            && GET_CODE (newpat) == PARALLEL
2315            && XVECLEN (newpat, 0) == 2
2316            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2317            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2318            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2319                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2320            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2321            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2322                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2323            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2324                                    INSN_CUID (i2))
2325            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2326            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2327            && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2328                  (GET_CODE (temp) == REG
2329                   && reg_nonzero_bits[REGNO (temp)] != 0
2330                   && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2331                   && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2332                   && (reg_nonzero_bits[REGNO (temp)]
2333                       != GET_MODE_MASK (word_mode))))
2334            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2335                  && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2336                      (GET_CODE (temp) == REG
2337                       && reg_nonzero_bits[REGNO (temp)] != 0
2338                       && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2339                       && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2340                       && (reg_nonzero_bits[REGNO (temp)]
2341                           != GET_MODE_MASK (word_mode)))))
2342            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2343                                          SET_SRC (XVECEXP (newpat, 0, 1)))
2344            && ! find_reg_note (i3, REG_UNUSED,
2345                                SET_DEST (XVECEXP (newpat, 0, 0))))
2346     {
2347       rtx ni2dest;
2348
2349       newi2pat = XVECEXP (newpat, 0, 0);
2350       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2351       newpat = XVECEXP (newpat, 0, 1);
2352       SUBST (SET_SRC (newpat),
2353              gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2354       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2355
2356       if (i2_code_number >= 0)
2357         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2358
2359       if (insn_code_number >= 0)
2360         {
2361           rtx insn;
2362           rtx link;
2363
2364           /* If we will be able to accept this, we have made a change to the
2365              destination of I3.  This requires us to do a few adjustments.  */
2366           PATTERN (i3) = newpat;
2367           adjust_for_new_dest (i3);
2368
2369           /* I3 now uses what used to be its destination and which is
2370              now I2's destination.  That means we need a LOG_LINK from
2371              I3 to I2.  But we used to have one, so we still will.
2372
2373              However, some later insn might be using I2's dest and have
2374              a LOG_LINK pointing at I3.  We must remove this link.
2375              The simplest way to remove the link is to point it at I1,
2376              which we know will be a NOTE.  */
2377
2378           for (insn = NEXT_INSN (i3);
2379                insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2380                         || insn != this_basic_block->next_bb->head);
2381                insn = NEXT_INSN (insn))
2382             {
2383               if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2384                 {
2385                   for (link = LOG_LINKS (insn); link;
2386                        link = XEXP (link, 1))
2387                     if (XEXP (link, 0) == i3)
2388                       XEXP (link, 0) = i1;
2389
2390                   break;
2391                 }
2392             }
2393         }
2394     }
2395
2396   /* Similarly, check for a case where we have a PARALLEL of two independent
2397      SETs but we started with three insns.  In this case, we can do the sets
2398      as two separate insns.  This case occurs when some SET allows two
2399      other insns to combine, but the destination of that SET is still live.  */
2400
2401   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2402            && GET_CODE (newpat) == PARALLEL
2403            && XVECLEN (newpat, 0) == 2
2404            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2405            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2406            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2407            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2408            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2409            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2410            && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2411                                    INSN_CUID (i2))
2412            /* Don't pass sets with (USE (MEM ...)) dests to the following.  */
2413            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2414            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2415            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2416                                   XVECEXP (newpat, 0, 0))
2417            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2418                                   XVECEXP (newpat, 0, 1))
2419            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2420                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2421     {
2422       /* Normally, it doesn't matter which of the two is done first,
2423          but it does if one references cc0.  In that case, it has to
2424          be first.  */
2425 #ifdef HAVE_cc0
2426       if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2427         {
2428           newi2pat = XVECEXP (newpat, 0, 0);
2429           newpat = XVECEXP (newpat, 0, 1);
2430         }
2431       else
2432 #endif
2433         {
2434           newi2pat = XVECEXP (newpat, 0, 1);
2435           newpat = XVECEXP (newpat, 0, 0);
2436         }
2437
2438       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2439
2440       if (i2_code_number >= 0)
2441         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2442     }
2443
2444   /* If it still isn't recognized, fail and change things back the way they
2445      were.  */
2446   if ((insn_code_number < 0
2447        /* Is the result a reasonable ASM_OPERANDS?  */
2448        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2449     {
2450       undo_all ();
2451       return 0;
2452     }
2453
2454   /* If we had to change another insn, make sure it is valid also.  */
2455   if (undobuf.other_insn)
2456     {
2457       rtx other_pat = PATTERN (undobuf.other_insn);
2458       rtx new_other_notes;
2459       rtx note, next;
2460
2461       CLEAR_HARD_REG_SET (newpat_used_regs);
2462
2463       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2464                                              &new_other_notes);
2465
2466       if (other_code_number < 0 && ! check_asm_operands (other_pat))
2467         {
2468           undo_all ();
2469           return 0;
2470         }
2471
2472       PATTERN (undobuf.other_insn) = other_pat;
2473
2474       /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2475          are still valid.  Then add any non-duplicate notes added by
2476          recog_for_combine.  */
2477       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2478         {
2479           next = XEXP (note, 1);
2480
2481           if (REG_NOTE_KIND (note) == REG_UNUSED
2482               && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2483             {
2484               if (GET_CODE (XEXP (note, 0)) == REG)
2485                 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2486
2487               remove_note (undobuf.other_insn, note);
2488             }
2489         }
2490
2491       for (note = new_other_notes; note; note = XEXP (note, 1))
2492         if (GET_CODE (XEXP (note, 0)) == REG)
2493           REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2494
2495       distribute_notes (new_other_notes, undobuf.other_insn,
2496                         undobuf.other_insn, NULL_RTX);
2497     }
2498 #ifdef HAVE_cc0
2499   /* If I2 is the setter CC0 and I3 is the user CC0 then check whether
2500      they are adjacent to each other or not.  */
2501   {
2502     rtx p = prev_nonnote_insn (i3);
2503     if (p && p != i2 && GET_CODE (p) == INSN && newi2pat
2504         && sets_cc0_p (newi2pat))
2505       {
2506         undo_all ();
2507         return 0;
2508       }
2509   }
2510 #endif
2511
2512   /* We now know that we can do this combination.  Merge the insns and
2513      update the status of registers and LOG_LINKS.  */
2514
2515   {
2516     rtx i3notes, i2notes, i1notes = 0;
2517     rtx i3links, i2links, i1links = 0;
2518     rtx midnotes = 0;
2519     unsigned int regno;
2520
2521     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2522        clear them.  */
2523     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2524     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2525     if (i1)
2526       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2527
2528     /* Ensure that we do not have something that should not be shared but
2529        occurs multiple times in the new insns.  Check this by first
2530        resetting all the `used' flags and then copying anything is shared.  */
2531
2532     reset_used_flags (i3notes);
2533     reset_used_flags (i2notes);
2534     reset_used_flags (i1notes);
2535     reset_used_flags (newpat);
2536     reset_used_flags (newi2pat);
2537     if (undobuf.other_insn)
2538       reset_used_flags (PATTERN (undobuf.other_insn));
2539
2540     i3notes = copy_rtx_if_shared (i3notes);
2541     i2notes = copy_rtx_if_shared (i2notes);
2542     i1notes = copy_rtx_if_shared (i1notes);
2543     newpat = copy_rtx_if_shared (newpat);
2544     newi2pat = copy_rtx_if_shared (newi2pat);
2545     if (undobuf.other_insn)
2546       reset_used_flags (PATTERN (undobuf.other_insn));
2547
2548     INSN_CODE (i3) = insn_code_number;
2549     PATTERN (i3) = newpat;
2550
2551     if (GET_CODE (i3) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (i3))
2552       {
2553         rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2554
2555         reset_used_flags (call_usage);
2556         call_usage = copy_rtx (call_usage);
2557
2558         if (substed_i2)
2559           replace_rtx (call_usage, i2dest, i2src);
2560
2561         if (substed_i1)
2562           replace_rtx (call_usage, i1dest, i1src);
2563
2564         CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2565       }
2566
2567     if (undobuf.other_insn)
2568       INSN_CODE (undobuf.other_insn) = other_code_number;
2569
2570     /* We had one special case above where I2 had more than one set and
2571        we replaced a destination of one of those sets with the destination
2572        of I3.  In that case, we have to update LOG_LINKS of insns later
2573        in this basic block.  Note that this (expensive) case is rare.
2574
2575        Also, in this case, we must pretend that all REG_NOTEs for I2
2576        actually came from I3, so that REG_UNUSED notes from I2 will be
2577        properly handled.  */
2578
2579     if (i3_subst_into_i2)
2580       {
2581         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2582           if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2583               && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2584               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2585               && ! find_reg_note (i2, REG_UNUSED,
2586                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2587             for (temp = NEXT_INSN (i2);
2588                  temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2589                           || this_basic_block->head != temp);
2590                  temp = NEXT_INSN (temp))
2591               if (temp != i3 && INSN_P (temp))
2592                 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2593                   if (XEXP (link, 0) == i2)
2594                     XEXP (link, 0) = i3;
2595
2596         if (i3notes)
2597           {
2598             rtx link = i3notes;
2599             while (XEXP (link, 1))
2600               link = XEXP (link, 1);
2601             XEXP (link, 1) = i2notes;
2602           }
2603         else
2604           i3notes = i2notes;
2605         i2notes = 0;
2606       }
2607
2608     LOG_LINKS (i3) = 0;
2609     REG_NOTES (i3) = 0;
2610     LOG_LINKS (i2) = 0;
2611     REG_NOTES (i2) = 0;
2612
2613     if (newi2pat)
2614       {
2615         INSN_CODE (i2) = i2_code_number;
2616         PATTERN (i2) = newi2pat;
2617       }
2618     else
2619       {
2620         PUT_CODE (i2, NOTE);
2621         NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2622         NOTE_SOURCE_FILE (i2) = 0;
2623       }
2624
2625     if (i1)
2626       {
2627         LOG_LINKS (i1) = 0;
2628         REG_NOTES (i1) = 0;
2629         PUT_CODE (i1, NOTE);
2630         NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2631         NOTE_SOURCE_FILE (i1) = 0;
2632       }
2633
2634     /* Get death notes for everything that is now used in either I3 or
2635        I2 and used to die in a previous insn.  If we built two new
2636        patterns, move from I1 to I2 then I2 to I3 so that we get the
2637        proper movement on registers that I2 modifies.  */
2638
2639     if (newi2pat)
2640       {
2641         move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2642         move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2643       }
2644     else
2645       move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2646                    i3, &midnotes);
2647
2648     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
2649     if (i3notes)
2650       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2651     if (i2notes)
2652       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2653     if (i1notes)
2654       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2655     if (midnotes)
2656       distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2657
2658     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
2659        know these are REG_UNUSED and want them to go to the desired insn,
2660        so we always pass it as i3.  We have not counted the notes in
2661        reg_n_deaths yet, so we need to do so now.  */
2662
2663     if (newi2pat && new_i2_notes)
2664       {
2665         for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2666           if (GET_CODE (XEXP (temp, 0)) == REG)
2667             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2668
2669         distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2670       }
2671
2672     if (new_i3_notes)
2673       {
2674         for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2675           if (GET_CODE (XEXP (temp, 0)) == REG)
2676             REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2677
2678         distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2679       }
2680
2681     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
2682        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
2683        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
2684        in that case, it might delete I2.  Similarly for I2 and I1.
2685        Show an additional death due to the REG_DEAD note we make here.  If
2686        we discard it in distribute_notes, we will decrement it again.  */
2687
2688     if (i3dest_killed)
2689       {
2690         if (GET_CODE (i3dest_killed) == REG)
2691           REG_N_DEATHS (REGNO (i3dest_killed))++;
2692
2693         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2694           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2695                                                NULL_RTX),
2696                             NULL_RTX, i2, NULL_RTX);
2697         else
2698           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2699                                                NULL_RTX),
2700                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2701       }
2702
2703     if (i2dest_in_i2src)
2704       {
2705         if (GET_CODE (i2dest) == REG)
2706           REG_N_DEATHS (REGNO (i2dest))++;
2707
2708         if (newi2pat && reg_set_p (i2dest, newi2pat))
2709           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2710                             NULL_RTX, i2, NULL_RTX);
2711         else
2712           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2713                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2714       }
2715
2716     if (i1dest_in_i1src)
2717       {
2718         if (GET_CODE (i1dest) == REG)
2719           REG_N_DEATHS (REGNO (i1dest))++;
2720
2721         if (newi2pat && reg_set_p (i1dest, newi2pat))
2722           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2723                             NULL_RTX, i2, NULL_RTX);
2724         else
2725           distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2726                             NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2727       }
2728
2729     distribute_links (i3links);
2730     distribute_links (i2links);
2731     distribute_links (i1links);
2732
2733     if (GET_CODE (i2dest) == REG)
2734       {
2735         rtx link;
2736         rtx i2_insn = 0, i2_val = 0, set;
2737
2738         /* The insn that used to set this register doesn't exist, and
2739            this life of the register may not exist either.  See if one of
2740            I3's links points to an insn that sets I2DEST.  If it does,
2741            that is now the last known value for I2DEST. If we don't update
2742            this and I2 set the register to a value that depended on its old
2743            contents, we will get confused.  If this insn is used, thing
2744            will be set correctly in combine_instructions.  */
2745
2746         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2747           if ((set = single_set (XEXP (link, 0))) != 0
2748               && rtx_equal_p (i2dest, SET_DEST (set)))
2749             i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2750
2751         record_value_for_reg (i2dest, i2_insn, i2_val);
2752
2753         /* If the reg formerly set in I2 died only once and that was in I3,
2754            zero its use count so it won't make `reload' do any work.  */
2755         if (! added_sets_2
2756             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2757             && ! i2dest_in_i2src)
2758           {
2759             regno = REGNO (i2dest);
2760             REG_N_SETS (regno)--;
2761           }
2762       }
2763
2764     if (i1 && GET_CODE (i1dest) == REG)
2765       {
2766         rtx link;
2767         rtx i1_insn = 0, i1_val = 0, set;
2768
2769         for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2770           if ((set = single_set (XEXP (link, 0))) != 0
2771               && rtx_equal_p (i1dest, SET_DEST (set)))
2772             i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2773
2774         record_value_for_reg (i1dest, i1_insn, i1_val);
2775
2776         regno = REGNO (i1dest);
2777         if (! added_sets_1 && ! i1dest_in_i1src)
2778           REG_N_SETS (regno)--;
2779       }
2780
2781     /* Update reg_nonzero_bits et al for any changes that may have been made
2782        to this insn.  The order of set_nonzero_bits_and_sign_copies() is
2783        important.  Because newi2pat can affect nonzero_bits of newpat */
2784     if (newi2pat)
2785       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2786     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2787
2788     /* Set new_direct_jump_p if a new return or simple jump instruction
2789        has been created.
2790
2791        If I3 is now an unconditional jump, ensure that it has a
2792        BARRIER following it since it may have initially been a
2793        conditional jump.  It may also be the last nonnote insn.  */
2794
2795     if (returnjump_p (i3) || any_uncondjump_p (i3))
2796       {
2797         *new_direct_jump_p = 1;
2798         mark_jump_label (PATTERN (i3), i3, 0);
2799
2800         if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2801             || GET_CODE (temp) != BARRIER)
2802           emit_barrier_after (i3);
2803       }
2804
2805     if (undobuf.other_insn != NULL_RTX
2806         && (returnjump_p (undobuf.other_insn)
2807             || any_uncondjump_p (undobuf.other_insn)))
2808       {
2809         *new_direct_jump_p = 1;
2810
2811         if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
2812             || GET_CODE (temp) != BARRIER)
2813           emit_barrier_after (undobuf.other_insn);
2814       }
2815
2816     /* An NOOP jump does not need barrier, but it does need cleaning up
2817        of CFG.  */
2818     if (GET_CODE (newpat) == SET
2819         && SET_SRC (newpat) == pc_rtx
2820         && SET_DEST (newpat) == pc_rtx)
2821       *new_direct_jump_p = 1;
2822   }
2823
2824   combine_successes++;
2825   undo_commit ();
2826
2827   if (added_links_insn
2828       && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2829       && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2830     return added_links_insn;
2831   else
2832     return newi2pat ? i2 : i3;
2833 }
2834 \f
2835 /* Undo all the modifications recorded in undobuf.  */
2836
2837 static void
2838 undo_all (void)
2839 {
2840   struct undo *undo, *next;
2841
2842   for (undo = undobuf.undos; undo; undo = next)
2843     {
2844       next = undo->next;
2845       if (undo->is_int)
2846         *undo->where.i = undo->old_contents.i;
2847       else
2848         *undo->where.r = undo->old_contents.r;
2849
2850       undo->next = undobuf.frees;
2851       undobuf.frees = undo;
2852     }
2853
2854   undobuf.undos = 0;
2855 }
2856
2857 /* We've committed to accepting the changes we made.  Move all
2858    of the undos to the free list.  */
2859
2860 static void
2861 undo_commit (void)
2862 {
2863   struct undo *undo, *next;
2864
2865   for (undo = undobuf.undos; undo; undo = next)
2866     {
2867       next = undo->next;
2868       undo->next = undobuf.frees;
2869       undobuf.frees = undo;
2870     }
2871   undobuf.undos = 0;
2872 }
2873
2874 \f
2875 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2876    where we have an arithmetic expression and return that point.  LOC will
2877    be inside INSN.
2878
2879    try_combine will call this function to see if an insn can be split into
2880    two insns.  */
2881
2882 static rtx *
2883 find_split_point (rtx *loc, rtx insn)
2884 {
2885   rtx x = *loc;
2886   enum rtx_code code = GET_CODE (x);
2887   rtx *split;
2888   unsigned HOST_WIDE_INT len = 0;
2889   HOST_WIDE_INT pos = 0;
2890   int unsignedp = 0;
2891   rtx inner = NULL_RTX;
2892
2893   /* First special-case some codes.  */
2894   switch (code)
2895     {
2896     case SUBREG:
2897 #ifdef INSN_SCHEDULING
2898       /* If we are making a paradoxical SUBREG invalid, it becomes a split
2899          point.  */
2900       if (GET_CODE (SUBREG_REG (x)) == MEM)
2901         return loc;
2902 #endif
2903       return find_split_point (&SUBREG_REG (x), insn);
2904
2905     case MEM:
2906 #ifdef HAVE_lo_sum
2907       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2908          using LO_SUM and HIGH.  */
2909       if (GET_CODE (XEXP (x, 0)) == CONST
2910           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2911         {
2912           SUBST (XEXP (x, 0),
2913                  gen_rtx_LO_SUM (Pmode,
2914                                  gen_rtx_HIGH (Pmode, XEXP (x, 0)),
2915                                  XEXP (x, 0)));
2916           return &XEXP (XEXP (x, 0), 0);
2917         }
2918 #endif
2919
2920       /* If we have a PLUS whose second operand is a constant and the
2921          address is not valid, perhaps will can split it up using
2922          the machine-specific way to split large constants.  We use
2923          the first pseudo-reg (one of the virtual regs) as a placeholder;
2924          it will not remain in the result.  */
2925       if (GET_CODE (XEXP (x, 0)) == PLUS
2926           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2927           && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2928         {
2929           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2930           rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2931                                  subst_insn);
2932
2933           /* This should have produced two insns, each of which sets our
2934              placeholder.  If the source of the second is a valid address,
2935              we can make put both sources together and make a split point
2936              in the middle.  */
2937
2938           if (seq
2939               && NEXT_INSN (seq) != NULL_RTX
2940               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
2941               && GET_CODE (seq) == INSN
2942               && GET_CODE (PATTERN (seq)) == SET
2943               && SET_DEST (PATTERN (seq)) == reg
2944               && ! reg_mentioned_p (reg,
2945                                     SET_SRC (PATTERN (seq)))
2946               && GET_CODE (NEXT_INSN (seq)) == INSN
2947               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
2948               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
2949               && memory_address_p (GET_MODE (x),
2950                                    SET_SRC (PATTERN (NEXT_INSN (seq)))))
2951             {
2952               rtx src1 = SET_SRC (PATTERN (seq));
2953               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
2954
2955               /* Replace the placeholder in SRC2 with SRC1.  If we can
2956                  find where in SRC2 it was placed, that can become our
2957                  split point and we can replace this address with SRC2.
2958                  Just try two obvious places.  */
2959
2960               src2 = replace_rtx (src2, reg, src1);
2961               split = 0;
2962               if (XEXP (src2, 0) == src1)
2963                 split = &XEXP (src2, 0);
2964               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2965                        && XEXP (XEXP (src2, 0), 0) == src1)
2966                 split = &XEXP (XEXP (src2, 0), 0);
2967
2968               if (split)
2969                 {
2970                   SUBST (XEXP (x, 0), src2);
2971                   return split;
2972                 }
2973             }
2974
2975           /* If that didn't work, perhaps the first operand is complex and
2976              needs to be computed separately, so make a split point there.
2977              This will occur on machines that just support REG + CONST
2978              and have a constant moved through some previous computation.  */
2979
2980           else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2981                    && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2982                          && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2983                              == 'o')))
2984             return &XEXP (XEXP (x, 0), 0);
2985         }
2986       break;
2987
2988     case SET:
2989 #ifdef HAVE_cc0
2990       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2991          ZERO_EXTRACT, the most likely reason why this doesn't match is that
2992          we need to put the operand into a register.  So split at that
2993          point.  */
2994
2995       if (SET_DEST (x) == cc0_rtx
2996           && GET_CODE (SET_SRC (x)) != COMPARE
2997           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2998           && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2999           && ! (GET_CODE (SET_SRC (x)) == SUBREG
3000                 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
3001         return &SET_SRC (x);
3002 #endif
3003
3004       /* See if we can split SET_SRC as it stands.  */
3005       split = find_split_point (&SET_SRC (x), insn);
3006       if (split && split != &SET_SRC (x))
3007         return split;
3008
3009       /* See if we can split SET_DEST as it stands.  */
3010       split = find_split_point (&SET_DEST (x), insn);
3011       if (split && split != &SET_DEST (x))
3012         return split;
3013
3014       /* See if this is a bitfield assignment with everything constant.  If
3015          so, this is an IOR of an AND, so split it into that.  */
3016       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3017           && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3018               <= HOST_BITS_PER_WIDE_INT)
3019           && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3020           && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3021           && GET_CODE (SET_SRC (x)) == CONST_INT
3022           && ((INTVAL (XEXP (SET_DEST (x), 1))
3023                + INTVAL (XEXP (SET_DEST (x), 2)))
3024               <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3025           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3026         {
3027           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3028           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3029           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3030           rtx dest = XEXP (SET_DEST (x), 0);
3031           enum machine_mode mode = GET_MODE (dest);
3032           unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3033
3034           if (BITS_BIG_ENDIAN)
3035             pos = GET_MODE_BITSIZE (mode) - len - pos;
3036
3037           if (src == mask)
3038             SUBST (SET_SRC (x),
3039                    gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3040           else
3041             SUBST (SET_SRC (x),
3042                    gen_binary (IOR, mode,
3043                                gen_binary (AND, mode, dest,
3044                                            gen_int_mode (~(mask << pos),
3045                                                          mode)),
3046                                GEN_INT (src << pos)));
3047
3048           SUBST (SET_DEST (x), dest);
3049
3050           split = find_split_point (&SET_SRC (x), insn);
3051           if (split && split != &SET_SRC (x))
3052             return split;
3053         }
3054
3055       /* Otherwise, see if this is an operation that we can split into two.
3056          If so, try to split that.  */
3057       code = GET_CODE (SET_SRC (x));
3058
3059       switch (code)
3060         {
3061         case AND:
3062           /* If we are AND'ing with a large constant that is only a single
3063              bit and the result is only being used in a context where we
3064              need to know if it is zero or nonzero, replace it with a bit
3065              extraction.  This will avoid the large constant, which might
3066              have taken more than one insn to make.  If the constant were
3067              not a valid argument to the AND but took only one insn to make,
3068              this is no worse, but if it took more than one insn, it will
3069              be better.  */
3070
3071           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3072               && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
3073               && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3074               && GET_CODE (SET_DEST (x)) == REG
3075               && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3076               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3077               && XEXP (*split, 0) == SET_DEST (x)
3078               && XEXP (*split, 1) == const0_rtx)
3079             {
3080               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3081                                                 XEXP (SET_SRC (x), 0),
3082                                                 pos, NULL_RTX, 1, 1, 0, 0);
3083               if (extraction != 0)
3084                 {
3085                   SUBST (SET_SRC (x), extraction);
3086                   return find_split_point (loc, insn);
3087                 }
3088             }
3089           break;
3090
3091         case NE:
3092           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3093              is known to be on, this can be converted into a NEG of a shift.  */
3094           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3095               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3096               && 1 <= (pos = exact_log2
3097                        (nonzero_bits (XEXP (SET_SRC (x), 0),
3098                                       GET_MODE (XEXP (SET_SRC (x), 0))))))
3099             {
3100               enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3101
3102               SUBST (SET_SRC (x),
3103                      gen_rtx_NEG (mode,
3104                                   gen_rtx_LSHIFTRT (mode,
3105                                                     XEXP (SET_SRC (x), 0),
3106                                                     GEN_INT (pos))));
3107
3108               split = find_split_point (&SET_SRC (x), insn);
3109               if (split && split != &SET_SRC (x))
3110                 return split;
3111             }
3112           break;
3113
3114         case SIGN_EXTEND:
3115           inner = XEXP (SET_SRC (x), 0);
3116
3117           /* We can't optimize if either mode is a partial integer
3118              mode as we don't know how many bits are significant
3119              in those modes.  */
3120           if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3121               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3122             break;
3123
3124           pos = 0;
3125           len = GET_MODE_BITSIZE (GET_MODE (inner));
3126           unsignedp = 0;
3127           break;
3128
3129         case SIGN_EXTRACT:
3130         case ZERO_EXTRACT:
3131           if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3132               && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3133             {
3134               inner = XEXP (SET_SRC (x), 0);
3135               len = INTVAL (XEXP (SET_SRC (x), 1));
3136               pos = INTVAL (XEXP (SET_SRC (x), 2));
3137
3138               if (BITS_BIG_ENDIAN)
3139                 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3140               unsignedp = (code == ZERO_EXTRACT);
3141             }
3142           break;
3143
3144         default:
3145           break;
3146         }
3147
3148       if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3149         {
3150           enum machine_mode mode = GET_MODE (SET_SRC (x));
3151
3152           /* For unsigned, we have a choice of a shift followed by an
3153              AND or two shifts.  Use two shifts for field sizes where the
3154              constant might be too large.  We assume here that we can
3155              always at least get 8-bit constants in an AND insn, which is
3156              true for every current RISC.  */
3157
3158           if (unsignedp && len <= 8)
3159             {
3160               SUBST (SET_SRC (x),
3161                      gen_rtx_AND (mode,
3162                                   gen_rtx_LSHIFTRT
3163                                   (mode, gen_lowpart_for_combine (mode, inner),
3164                                    GEN_INT (pos)),
3165                                   GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3166
3167               split = find_split_point (&SET_SRC (x), insn);
3168               if (split && split != &SET_SRC (x))
3169                 return split;
3170             }
3171           else
3172             {
3173               SUBST (SET_SRC (x),
3174                      gen_rtx_fmt_ee
3175                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3176                       gen_rtx_ASHIFT (mode,
3177                                       gen_lowpart_for_combine (mode, inner),
3178                                       GEN_INT (GET_MODE_BITSIZE (mode)
3179                                                - len - pos)),
3180                       GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3181
3182               split = find_split_point (&SET_SRC (x), insn);
3183               if (split && split != &SET_SRC (x))
3184                 return split;
3185             }
3186         }
3187
3188       /* See if this is a simple operation with a constant as the second
3189          operand.  It might be that this constant is out of range and hence
3190          could be used as a split point.  */
3191       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3192            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3193            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
3194           && CONSTANT_P (XEXP (SET_SRC (x), 1))
3195           && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
3196               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3197                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
3198                       == 'o'))))
3199         return &XEXP (SET_SRC (x), 1);
3200
3201       /* Finally, see if this is a simple operation with its first operand
3202          not in a register.  The operation might require this operand in a
3203          register, so return it as a split point.  We can always do this
3204          because if the first operand were another operation, we would have
3205          already found it as a split point.  */
3206       if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3207            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3208            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
3209            || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
3210           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3211         return &XEXP (SET_SRC (x), 0);
3212
3213       return 0;
3214
3215     case AND:
3216     case IOR:
3217       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3218          it is better to write this as (not (ior A B)) so we can split it.
3219          Similarly for IOR.  */
3220       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3221         {
3222           SUBST (*loc,
3223                  gen_rtx_NOT (GET_MODE (x),
3224                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3225                                               GET_MODE (x),
3226                                               XEXP (XEXP (x, 0), 0),
3227                                               XEXP (XEXP (x, 1), 0))));
3228           return find_split_point (loc, insn);
3229         }
3230
3231       /* Many RISC machines have a large set of logical insns.  If the
3232          second operand is a NOT, put it first so we will try to split the
3233          other operand first.  */
3234       if (GET_CODE (XEXP (x, 1)) == NOT)
3235         {
3236           rtx tem = XEXP (x, 0);
3237           SUBST (XEXP (x, 0), XEXP (x, 1));
3238           SUBST (XEXP (x, 1), tem);
3239         }
3240       break;
3241
3242     default:
3243       break;
3244     }
3245
3246   /* Otherwise, select our actions depending on our rtx class.  */
3247   switch (GET_RTX_CLASS (code))
3248     {
3249     case 'b':                   /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
3250     case '3':
3251       split = find_split_point (&XEXP (x, 2), insn);
3252       if (split)
3253         return split;
3254       /* ... fall through ...  */
3255     case '2':
3256     case 'c':
3257     case '<':
3258       split = find_split_point (&XEXP (x, 1), insn);
3259       if (split)
3260         return split;
3261       /* ... fall through ...  */
3262     case '1':
3263       /* Some machines have (and (shift ...) ...) insns.  If X is not
3264          an AND, but XEXP (X, 0) is, use it as our split point.  */
3265       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3266         return &XEXP (x, 0);
3267
3268       split = find_split_point (&XEXP (x, 0), insn);
3269       if (split)
3270         return split;
3271       return loc;
3272     }
3273
3274   /* Otherwise, we don't have a split point.  */
3275   return 0;
3276 }
3277 \f
3278 /* Throughout X, replace FROM with TO, and return the result.
3279    The result is TO if X is FROM;
3280    otherwise the result is X, but its contents may have been modified.
3281    If they were modified, a record was made in undobuf so that
3282    undo_all will (among other things) return X to its original state.
3283
3284    If the number of changes necessary is too much to record to undo,
3285    the excess changes are not made, so the result is invalid.
3286    The changes already made can still be undone.
3287    undobuf.num_undo is incremented for such changes, so by testing that
3288    the caller can tell whether the result is valid.
3289
3290    `n_occurrences' is incremented each time FROM is replaced.
3291
3292    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3293
3294    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
3295    by copying if `n_occurrences' is nonzero.  */
3296
3297 static rtx
3298 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3299 {
3300   enum rtx_code code = GET_CODE (x);
3301   enum machine_mode op0_mode = VOIDmode;
3302   const char *fmt;
3303   int len, i;
3304   rtx new;
3305
3306 /* Two expressions are equal if they are identical copies of a shared
3307    RTX or if they are both registers with the same register number
3308    and mode.  */
3309
3310 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
3311   ((X) == (Y)                                           \
3312    || (GET_CODE (X) == REG && GET_CODE (Y) == REG       \
3313        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3314
3315   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3316     {
3317       n_occurrences++;
3318       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3319     }
3320
3321   /* If X and FROM are the same register but different modes, they will
3322      not have been seen as equal above.  However, flow.c will make a
3323      LOG_LINKS entry for that case.  If we do nothing, we will try to
3324      rerecognize our original insn and, when it succeeds, we will
3325      delete the feeding insn, which is incorrect.
3326
3327      So force this insn not to match in this (rare) case.  */
3328   if (! in_dest && code == REG && GET_CODE (from) == REG
3329       && REGNO (x) == REGNO (from))
3330     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3331
3332   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3333      of which may contain things that can be combined.  */
3334   if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3335     return x;
3336
3337   /* It is possible to have a subexpression appear twice in the insn.
3338      Suppose that FROM is a register that appears within TO.
3339      Then, after that subexpression has been scanned once by `subst',
3340      the second time it is scanned, TO may be found.  If we were
3341      to scan TO here, we would find FROM within it and create a
3342      self-referent rtl structure which is completely wrong.  */
3343   if (COMBINE_RTX_EQUAL_P (x, to))
3344     return to;
3345
3346   /* Parallel asm_operands need special attention because all of the
3347      inputs are shared across the arms.  Furthermore, unsharing the
3348      rtl results in recognition failures.  Failure to handle this case
3349      specially can result in circular rtl.
3350
3351      Solve this by doing a normal pass across the first entry of the
3352      parallel, and only processing the SET_DESTs of the subsequent
3353      entries.  Ug.  */
3354
3355   if (code == PARALLEL
3356       && GET_CODE (XVECEXP (x, 0, 0)) == SET
3357       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3358     {
3359       new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3360
3361       /* If this substitution failed, this whole thing fails.  */
3362       if (GET_CODE (new) == CLOBBER
3363           && XEXP (new, 0) == const0_rtx)
3364         return new;
3365
3366       SUBST (XVECEXP (x, 0, 0), new);
3367
3368       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3369         {
3370           rtx dest = SET_DEST (XVECEXP (x, 0, i));
3371
3372           if (GET_CODE (dest) != REG
3373               && GET_CODE (dest) != CC0
3374               && GET_CODE (dest) != PC)
3375             {
3376               new = subst (dest, from, to, 0, unique_copy);
3377
3378               /* If this substitution failed, this whole thing fails.  */
3379               if (GET_CODE (new) == CLOBBER
3380                   && XEXP (new, 0) == const0_rtx)
3381                 return new;
3382
3383               SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3384             }
3385         }
3386     }
3387   else
3388     {
3389       len = GET_RTX_LENGTH (code);
3390       fmt = GET_RTX_FORMAT (code);
3391
3392       /* We don't need to process a SET_DEST that is a register, CC0,
3393          or PC, so set up to skip this common case.  All other cases
3394          where we want to suppress replacing something inside a
3395          SET_SRC are handled via the IN_DEST operand.  */
3396       if (code == SET
3397           && (GET_CODE (SET_DEST (x)) == REG
3398               || GET_CODE (SET_DEST (x)) == CC0
3399               || GET_CODE (SET_DEST (x)) == PC))
3400         fmt = "ie";
3401
3402       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3403          constant.  */
3404       if (fmt[0] == 'e')
3405         op0_mode = GET_MODE (XEXP (x, 0));
3406
3407       for (i = 0; i < len; i++)
3408         {
3409           if (fmt[i] == 'E')
3410             {
3411               int j;
3412               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3413                 {
3414                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3415                     {
3416                       new = (unique_copy && n_occurrences
3417                              ? copy_rtx (to) : to);
3418                       n_occurrences++;
3419                     }
3420                   else
3421                     {
3422                       new = subst (XVECEXP (x, i, j), from, to, 0,
3423                                    unique_copy);
3424
3425                       /* If this substitution failed, this whole thing
3426                          fails.  */
3427                       if (GET_CODE (new) == CLOBBER
3428                           && XEXP (new, 0) == const0_rtx)
3429                         return new;
3430                     }
3431
3432                   SUBST (XVECEXP (x, i, j), new);
3433                 }
3434             }
3435           else if (fmt[i] == 'e')
3436             {
3437               /* If this is a register being set, ignore it.  */
3438               new = XEXP (x, i);
3439               if (in_dest
3440                   && (code == SUBREG || code == STRICT_LOW_PART
3441                       || code == ZERO_EXTRACT)
3442                   && i == 0
3443                   && GET_CODE (new) == REG)
3444                 ;
3445
3446               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3447                 {
3448                   /* In general, don't install a subreg involving two
3449                      modes not tieable.  It can worsen register
3450                      allocation, and can even make invalid reload
3451                      insns, since the reg inside may need to be copied
3452                      from in the outside mode, and that may be invalid
3453                      if it is an fp reg copied in integer mode.
3454
3455                      We allow two exceptions to this: It is valid if
3456                      it is inside another SUBREG and the mode of that
3457                      SUBREG and the mode of the inside of TO is
3458                      tieable and it is valid if X is a SET that copies
3459                      FROM to CC0.  */
3460
3461                   if (GET_CODE (to) == SUBREG
3462                       && ! MODES_TIEABLE_P (GET_MODE (to),
3463                                             GET_MODE (SUBREG_REG (to)))
3464                       && ! (code == SUBREG
3465                             && MODES_TIEABLE_P (GET_MODE (x),
3466                                                 GET_MODE (SUBREG_REG (to))))
3467 #ifdef HAVE_cc0
3468                       && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3469 #endif
3470                       )
3471                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3472
3473 #ifdef CANNOT_CHANGE_MODE_CLASS
3474                   if (code == SUBREG
3475                       && GET_CODE (to) == REG
3476                       && REGNO (to) < FIRST_PSEUDO_REGISTER
3477                       && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3478                                                    GET_MODE (to),
3479                                                    GET_MODE (x)))
3480                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3481 #endif
3482
3483                   new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3484                   n_occurrences++;
3485                 }
3486               else
3487                 /* If we are in a SET_DEST, suppress most cases unless we
3488                    have gone inside a MEM, in which case we want to
3489                    simplify the address.  We assume here that things that
3490                    are actually part of the destination have their inner
3491                    parts in the first expression.  This is true for SUBREG,
3492                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3493                    things aside from REG and MEM that should appear in a
3494                    SET_DEST.  */
3495                 new = subst (XEXP (x, i), from, to,
3496                              (((in_dest
3497                                 && (code == SUBREG || code == STRICT_LOW_PART
3498                                     || code == ZERO_EXTRACT))
3499                                || code == SET)
3500                               && i == 0), unique_copy);
3501
3502               /* If we found that we will have to reject this combination,
3503                  indicate that by returning the CLOBBER ourselves, rather than
3504                  an expression containing it.  This will speed things up as
3505                  well as prevent accidents where two CLOBBERs are considered
3506                  to be equal, thus producing an incorrect simplification.  */
3507
3508               if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3509                 return new;
3510
3511               if (GET_CODE (x) == SUBREG
3512                   && (GET_CODE (new) == CONST_INT
3513                       || GET_CODE (new) == CONST_DOUBLE))
3514                 {
3515                   enum machine_mode mode = GET_MODE (x);
3516
3517                   x = simplify_subreg (GET_MODE (x), new,
3518                                        GET_MODE (SUBREG_REG (x)),
3519                                        SUBREG_BYTE (x));
3520                   if (! x)
3521                     x = gen_rtx_CLOBBER (mode, const0_rtx);
3522                 }
3523               else if (GET_CODE (new) == CONST_INT
3524                        && GET_CODE (x) == ZERO_EXTEND)
3525                 {
3526                   x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3527                                                 new, GET_MODE (XEXP (x, 0)));
3528                   if (! x)
3529                     abort ();
3530                 }
3531               else
3532                 SUBST (XEXP (x, i), new);
3533             }
3534         }
3535     }
3536
3537   /* Try to simplify X.  If the simplification changed the code, it is likely
3538      that further simplification will help, so loop, but limit the number
3539      of repetitions that will be performed.  */
3540
3541   for (i = 0; i < 4; i++)
3542     {
3543       /* If X is sufficiently simple, don't bother trying to do anything
3544          with it.  */
3545       if (code != CONST_INT && code != REG && code != CLOBBER)
3546         x = combine_simplify_rtx (x, op0_mode, i == 3, in_dest);
3547
3548       if (GET_CODE (x) == code)
3549         break;
3550
3551       code = GET_CODE (x);
3552
3553       /* We no longer know the original mode of operand 0 since we
3554          have changed the form of X)  */
3555       op0_mode = VOIDmode;
3556     }
3557
3558   return x;
3559 }
3560 \f
3561 /* Simplify X, a piece of RTL.  We just operate on the expression at the
3562    outer level; call `subst' to simplify recursively.  Return the new
3563    expression.
3564
3565    OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3566    will be the iteration even if an expression with a code different from
3567    X is returned; IN_DEST is nonzero if we are inside a SET_DEST.  */
3568
3569 static rtx
3570 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int last,
3571                       int in_dest)
3572 {
3573   enum rtx_code code = GET_CODE (x);
3574   enum machine_mode mode = GET_MODE (x);
3575   rtx temp;
3576   rtx reversed;
3577   int i;
3578
3579   /* If this is a commutative operation, put a constant last and a complex
3580      expression first.  We don't need to do this for comparisons here.  */
3581   if (GET_RTX_CLASS (code) == 'c'
3582       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3583     {
3584       temp = XEXP (x, 0);
3585       SUBST (XEXP (x, 0), XEXP (x, 1));
3586       SUBST (XEXP (x, 1), temp);
3587     }
3588
3589   /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3590      sign extension of a PLUS with a constant, reverse the order of the sign
3591      extension and the addition. Note that this not the same as the original
3592      code, but overflow is undefined for signed values.  Also note that the
3593      PLUS will have been partially moved "inside" the sign-extension, so that
3594      the first operand of X will really look like:
3595          (ashiftrt (plus (ashift A C4) C5) C4).
3596      We convert this to
3597          (plus (ashiftrt (ashift A C4) C2) C4)
3598      and replace the first operand of X with that expression.  Later parts
3599      of this function may simplify the expression further.
3600
3601      For example, if we start with (mult (sign_extend (plus A C1)) C2),
3602      we swap the SIGN_EXTEND and PLUS.  Later code will apply the
3603      distributive law to produce (plus (mult (sign_extend X) C1) C3).
3604
3605      We do this to simplify address expressions.  */
3606
3607   if ((code == PLUS || code == MINUS || code == MULT)
3608       && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3609       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3610       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3611       && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3612       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3613       && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3614       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3615       && (temp = simplify_binary_operation (ASHIFTRT, mode,
3616                                             XEXP (XEXP (XEXP (x, 0), 0), 1),
3617                                             XEXP (XEXP (x, 0), 1))) != 0)
3618     {
3619       rtx new
3620         = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3621                                 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3622                                 INTVAL (XEXP (XEXP (x, 0), 1)));
3623
3624       new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3625                                   INTVAL (XEXP (XEXP (x, 0), 1)));
3626
3627       SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3628     }
3629
3630   /* If this is a simple operation applied to an IF_THEN_ELSE, try
3631      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
3632      things.  Check for cases where both arms are testing the same
3633      condition.
3634
3635      Don't do anything if all operands are very simple.  */
3636
3637   if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3638         || GET_RTX_CLASS (code) == '<')
3639        && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3640             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3641                   && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3642                       == 'o')))
3643            || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3644                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3645                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3646                          == 'o')))))
3647       || (GET_RTX_CLASS (code) == '1'
3648           && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3649                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3650                      && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3651                          == 'o'))))))
3652     {
3653       rtx cond, true_rtx, false_rtx;
3654
3655       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3656       if (cond != 0
3657           /* If everything is a comparison, what we have is highly unlikely
3658              to be simpler, so don't use it.  */
3659           && ! (GET_RTX_CLASS (code) == '<'
3660                 && (GET_RTX_CLASS (GET_CODE (true_rtx)) == '<'
3661                     || GET_RTX_CLASS (GET_CODE (false_rtx)) == '<')))
3662         {
3663           rtx cop1 = const0_rtx;
3664           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3665
3666           if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3667             return x;
3668
3669           /* Simplify the alternative arms; this may collapse the true and
3670              false arms to store-flag values.  Be careful to use copy_rtx
3671              here since true_rtx or false_rtx might share RTL with x as a
3672              result of the if_then_else_cond call above.  */
3673           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
3674           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
3675
3676           /* If true_rtx and false_rtx are not general_operands, an if_then_else
3677              is unlikely to be simpler.  */
3678           if (general_operand (true_rtx, VOIDmode)
3679               && general_operand (false_rtx, VOIDmode))
3680             {
3681               enum rtx_code reversed;
3682
3683               /* Restarting if we generate a store-flag expression will cause
3684                  us to loop.  Just drop through in this case.  */
3685
3686               /* If the result values are STORE_FLAG_VALUE and zero, we can
3687                  just make the comparison operation.  */
3688               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3689                 x = gen_binary (cond_code, mode, cond, cop1);
3690               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3691                        && ((reversed = reversed_comparison_code_parts
3692                                         (cond_code, cond, cop1, NULL))
3693                            != UNKNOWN))
3694                 x = gen_binary (reversed, mode, cond, cop1);
3695
3696               /* Likewise, we can make the negate of a comparison operation
3697                  if the result values are - STORE_FLAG_VALUE and zero.  */
3698               else if (GET_CODE (true_rtx) == CONST_INT
3699                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3700                        && false_rtx == const0_rtx)
3701                 x = simplify_gen_unary (NEG, mode,
3702                                         gen_binary (cond_code, mode, cond,
3703                                                     cop1),
3704                                         mode);
3705               else if (GET_CODE (false_rtx) == CONST_INT
3706                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3707                        && true_rtx == const0_rtx
3708                        && ((reversed = reversed_comparison_code_parts
3709                                         (cond_code, cond, cop1, NULL))
3710                            != UNKNOWN))
3711                 x = simplify_gen_unary (NEG, mode,
3712                                         gen_binary (reversed, mode,
3713                                                     cond, cop1),
3714                                         mode);
3715               else
3716                 return gen_rtx_IF_THEN_ELSE (mode,
3717                                              gen_binary (cond_code, VOIDmode,
3718                                                          cond, cop1),
3719                                              true_rtx, false_rtx);
3720
3721               code = GET_CODE (x);
3722               op0_mode = VOIDmode;
3723             }
3724         }
3725     }
3726
3727   /* Try to fold this expression in case we have constants that weren't
3728      present before.  */
3729   temp = 0;
3730   switch (GET_RTX_CLASS (code))
3731     {
3732     case '1':
3733       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3734       break;
3735     case '<':
3736       {
3737         enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3738         if (cmp_mode == VOIDmode)
3739           {
3740             cmp_mode = GET_MODE (XEXP (x, 1));
3741             if (cmp_mode == VOIDmode)
3742               cmp_mode = op0_mode;
3743           }
3744         temp = simplify_relational_operation (code, cmp_mode,
3745                                               XEXP (x, 0), XEXP (x, 1));
3746       }
3747 #ifdef FLOAT_STORE_FLAG_VALUE
3748       if (temp != 0 && GET_MODE_CLASS (mode) == MODE_FLOAT)
3749         {
3750           if (temp == const0_rtx)
3751             temp = CONST0_RTX (mode);
3752           else
3753             temp = CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE (mode),
3754                                                  mode);
3755         }
3756 #endif
3757       break;
3758     case 'c':
3759     case '2':
3760       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3761       break;
3762     case 'b':
3763     case '3':
3764       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3765                                          XEXP (x, 1), XEXP (x, 2));
3766       break;
3767     }
3768
3769   if (temp)
3770     {
3771       x = temp;
3772       code = GET_CODE (temp);
3773       op0_mode = VOIDmode;
3774       mode = GET_MODE (temp);
3775     }
3776
3777   /* First see if we can apply the inverse distributive law.  */
3778   if (code == PLUS || code == MINUS
3779       || code == AND || code == IOR || code == XOR)
3780     {
3781       x = apply_distributive_law (x);
3782       code = GET_CODE (x);
3783       op0_mode = VOIDmode;
3784     }
3785
3786   /* If CODE is an associative operation not otherwise handled, see if we
3787      can associate some operands.  This can win if they are constants or
3788      if they are logically related (i.e. (a & b) & a).  */
3789   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3790        || code == AND || code == IOR || code == XOR
3791        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3792       && ((INTEGRAL_MODE_P (mode) && code != DIV)
3793           || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3794     {
3795       if (GET_CODE (XEXP (x, 0)) == code)
3796         {
3797           rtx other = XEXP (XEXP (x, 0), 0);
3798           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3799           rtx inner_op1 = XEXP (x, 1);
3800           rtx inner;
3801
3802           /* Make sure we pass the constant operand if any as the second
3803              one if this is a commutative operation.  */
3804           if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3805             {
3806               rtx tem = inner_op0;
3807               inner_op0 = inner_op1;
3808               inner_op1 = tem;
3809             }
3810           inner = simplify_binary_operation (code == MINUS ? PLUS
3811                                              : code == DIV ? MULT
3812                                              : code,
3813                                              mode, inner_op0, inner_op1);
3814
3815           /* For commutative operations, try the other pair if that one
3816              didn't simplify.  */
3817           if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3818             {
3819               other = XEXP (XEXP (x, 0), 1);
3820               inner = simplify_binary_operation (code, mode,
3821                                                  XEXP (XEXP (x, 0), 0),
3822                                                  XEXP (x, 1));
3823             }
3824
3825           if (inner)
3826             return gen_binary (code, mode, other, inner);
3827         }
3828     }
3829
3830   /* A little bit of algebraic simplification here.  */
3831   switch (code)
3832     {
3833     case MEM:
3834       /* Ensure that our address has any ASHIFTs converted to MULT in case
3835          address-recognizing predicates are called later.  */
3836       temp = make_compound_operation (XEXP (x, 0), MEM);
3837       SUBST (XEXP (x, 0), temp);
3838       break;
3839
3840     case SUBREG:
3841       if (op0_mode == VOIDmode)
3842         op0_mode = GET_MODE (SUBREG_REG (x));
3843
3844       /* simplify_subreg can't use gen_lowpart_for_combine.  */
3845       if (CONSTANT_P (SUBREG_REG (x))
3846           && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
3847              /* Don't call gen_lowpart_for_combine if the inner mode
3848                 is VOIDmode and we cannot simplify it, as SUBREG without
3849                 inner mode is invalid.  */
3850           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
3851               || gen_lowpart_common (mode, SUBREG_REG (x))))
3852         return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3853
3854       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
3855         break;
3856       {
3857         rtx temp;
3858         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
3859                                 SUBREG_BYTE (x));
3860         if (temp)
3861           return temp;
3862       }
3863
3864       /* Don't change the mode of the MEM if that would change the meaning
3865          of the address.  */
3866       if (GET_CODE (SUBREG_REG (x)) == MEM
3867           && (MEM_VOLATILE_P (SUBREG_REG (x))
3868               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
3869         return gen_rtx_CLOBBER (mode, const0_rtx);
3870
3871       /* Note that we cannot do any narrowing for non-constants since
3872          we might have been counting on using the fact that some bits were
3873          zero.  We now do this in the SET.  */
3874
3875       break;
3876
3877     case NOT:
3878       if (GET_CODE (XEXP (x, 0)) == SUBREG
3879           && subreg_lowpart_p (XEXP (x, 0))
3880           && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3881               < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3882           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3883           && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3884         {
3885           enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3886
3887           x = gen_rtx_ROTATE (inner_mode,
3888                               simplify_gen_unary (NOT, inner_mode, const1_rtx,
3889                                                   inner_mode),
3890                               XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3891           return gen_lowpart_for_combine (mode, x);
3892         }
3893
3894       /* Apply De Morgan's laws to reduce number of patterns for machines
3895          with negating logical insns (and-not, nand, etc.).  If result has
3896          only one NOT, put it first, since that is how the patterns are
3897          coded.  */
3898
3899       if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3900         {
3901           rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3902           enum machine_mode op_mode;
3903
3904           op_mode = GET_MODE (in1);
3905           in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
3906
3907           op_mode = GET_MODE (in2);
3908           if (op_mode == VOIDmode)
3909             op_mode = mode;
3910           in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
3911
3912           if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
3913             {
3914               rtx tem = in2;
3915               in2 = in1; in1 = tem;
3916             }
3917
3918           return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3919                                  mode, in1, in2);
3920         }
3921       break;
3922
3923     case NEG:
3924       /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1.  */
3925       if (GET_CODE (XEXP (x, 0)) == XOR
3926           && XEXP (XEXP (x, 0), 1) == const1_rtx
3927           && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3928         return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3929
3930       temp = expand_compound_operation (XEXP (x, 0));
3931
3932       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3933          replaced by (lshiftrt X C).  This will convert
3934          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
3935
3936       if (GET_CODE (temp) == ASHIFTRT
3937           && GET_CODE (XEXP (temp, 1)) == CONST_INT
3938           && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3939         return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3940                                      INTVAL (XEXP (temp, 1)));
3941
3942       /* If X has only a single bit that might be nonzero, say, bit I, convert
3943          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3944          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
3945          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
3946          or a SUBREG of one since we'd be making the expression more
3947          complex if it was just a register.  */
3948
3949       if (GET_CODE (temp) != REG
3950           && ! (GET_CODE (temp) == SUBREG
3951                 && GET_CODE (SUBREG_REG (temp)) == REG)
3952           && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3953         {
3954           rtx temp1 = simplify_shift_const
3955             (NULL_RTX, ASHIFTRT, mode,
3956              simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3957                                    GET_MODE_BITSIZE (mode) - 1 - i),
3958              GET_MODE_BITSIZE (mode) - 1 - i);
3959
3960           /* If all we did was surround TEMP with the two shifts, we
3961              haven't improved anything, so don't use it.  Otherwise,
3962              we are better off with TEMP1.  */
3963           if (GET_CODE (temp1) != ASHIFTRT
3964               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3965               || XEXP (XEXP (temp1, 0), 0) != temp)
3966             return temp1;
3967         }
3968       break;
3969
3970     case TRUNCATE:
3971       /* We can't handle truncation to a partial integer mode here
3972          because we don't know the real bitsize of the partial
3973          integer mode.  */
3974       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3975         break;
3976
3977       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3978           && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3979                                     GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
3980         SUBST (XEXP (x, 0),
3981                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3982                               GET_MODE_MASK (mode), NULL_RTX, 0));
3983
3984       /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI.  */
3985       if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3986            || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3987           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3988         return XEXP (XEXP (x, 0), 0);
3989
3990       /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3991          (OP:SI foo:SI) if OP is NEG or ABS.  */
3992       if ((GET_CODE (XEXP (x, 0)) == ABS
3993            || GET_CODE (XEXP (x, 0)) == NEG)
3994           && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3995               || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3996           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3997         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
3998                                    XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
3999
4000       /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
4001          (truncate:SI x).  */
4002       if (GET_CODE (XEXP (x, 0)) == SUBREG
4003           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
4004           && subreg_lowpart_p (XEXP (x, 0)))
4005         return SUBREG_REG (XEXP (x, 0));
4006
4007       /* If we know that the value is already truncated, we can
4008          replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
4009          is nonzero for the corresponding modes.  But don't do this
4010          for an (LSHIFTRT (MULT ...)) since this will cause problems
4011          with the umulXi3_highpart patterns.  */
4012       if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4013                                  GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4014           && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4015              >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
4016           && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4017                 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4018         return gen_lowpart_for_combine (mode, XEXP (x, 0));
4019
4020       /* A truncate of a comparison can be replaced with a subreg if
4021          STORE_FLAG_VALUE permits.  This is like the previous test,
4022          but it works even if the comparison is done in a mode larger
4023          than HOST_BITS_PER_WIDE_INT.  */
4024       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4025           && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4026           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4027         return gen_lowpart_for_combine (mode, XEXP (x, 0));
4028
4029       /* Similarly, a truncate of a register whose value is a
4030          comparison can be replaced with a subreg if STORE_FLAG_VALUE
4031          permits.  */
4032       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4033           && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4034           && (temp = get_last_value (XEXP (x, 0)))
4035           && GET_RTX_CLASS (GET_CODE (temp)) == '<')
4036         return gen_lowpart_for_combine (mode, XEXP (x, 0));
4037
4038       break;
4039
4040     case FLOAT_TRUNCATE:
4041       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
4042       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4043           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4044         return XEXP (XEXP (x, 0), 0);
4045
4046       /* (float_truncate:SF (float_truncate:DF foo:XF))
4047          = (float_truncate:SF foo:XF).
4048          This may eliminate double rounding, so it is unsafe.
4049
4050          (float_truncate:SF (float_extend:XF foo:DF))
4051          = (float_truncate:SF foo:DF).
4052
4053          (float_truncate:DF (float_extend:XF foo:SF))
4054          = (float_extend:SF foo:DF).  */
4055       if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4056            && flag_unsafe_math_optimizations)
4057           || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4058         return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4059                                                             0)))
4060                                    > GET_MODE_SIZE (mode)
4061                                    ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4062                                    mode,
4063                                    XEXP (XEXP (x, 0), 0), mode);
4064
4065       /*  (float_truncate (float x)) is (float x)  */
4066       if (GET_CODE (XEXP (x, 0)) == FLOAT
4067           && (flag_unsafe_math_optimizations
4068               || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4069                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4070                       - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4071                                              GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4072         return simplify_gen_unary (FLOAT, mode,
4073                                    XEXP (XEXP (x, 0), 0),
4074                                    GET_MODE (XEXP (XEXP (x, 0), 0)));
4075
4076       /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4077          (OP:SF foo:SF) if OP is NEG or ABS.  */
4078       if ((GET_CODE (XEXP (x, 0)) == ABS
4079            || GET_CODE (XEXP (x, 0)) == NEG)
4080           && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4081           && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4082         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4083                                    XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4084
4085       /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4086          is (float_truncate:SF x).  */
4087       if (GET_CODE (XEXP (x, 0)) == SUBREG
4088           && subreg_lowpart_p (XEXP (x, 0))
4089           && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4090         return SUBREG_REG (XEXP (x, 0));
4091       break;
4092     case FLOAT_EXTEND:
4093       /*  (float_extend (float_extend x)) is (float_extend x)
4094
4095           (float_extend (float x)) is (float x) assuming that double
4096           rounding can't happen.
4097           */
4098       if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4099           || (GET_CODE (XEXP (x, 0)) == FLOAT
4100               && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4101                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4102                       - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4103                                              GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4104         return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4105                                    XEXP (XEXP (x, 0), 0),
4106                                    GET_MODE (XEXP (XEXP (x, 0), 0)));
4107
4108       break;
4109 #ifdef HAVE_cc0
4110     case COMPARE:
4111       /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4112          using cc0, in which case we want to leave it as a COMPARE
4113          so we can distinguish it from a register-register-copy.  */
4114       if (XEXP (x, 1) == const0_rtx)
4115         return XEXP (x, 0);
4116
4117       /* x - 0 is the same as x unless x's mode has signed zeros and
4118          allows rounding towards -infinity.  Under those conditions,
4119          0 - 0 is -0.  */
4120       if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4121             && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4122           && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4123         return XEXP (x, 0);
4124       break;
4125 #endif
4126
4127     case CONST:
4128       /* (const (const X)) can become (const X).  Do it this way rather than
4129          returning the inner CONST since CONST can be shared with a
4130          REG_EQUAL note.  */
4131       if (GET_CODE (XEXP (x, 0)) == CONST)
4132         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4133       break;
4134
4135 #ifdef HAVE_lo_sum
4136     case LO_SUM:
4137       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
4138          can add in an offset.  find_split_point will split this address up
4139          again if it doesn't match.  */
4140       if (GET_CODE (XEXP (x, 0)) == HIGH
4141           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4142         return XEXP (x, 1);
4143       break;
4144 #endif
4145
4146     case PLUS:
4147       /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4148        */
4149       if (GET_CODE (XEXP (x, 0)) == MULT
4150           && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4151         {
4152           rtx in1, in2;
4153
4154           in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4155           in2 = XEXP (XEXP (x, 0), 1);
4156           return gen_binary (MINUS, mode, XEXP (x, 1),
4157                              gen_binary (MULT, mode, in1, in2));
4158         }
4159
4160       /* If we have (plus (plus (A const) B)), associate it so that CONST is
4161          outermost.  That's because that's the way indexed addresses are
4162          supposed to appear.  This code used to check many more cases, but
4163          they are now checked elsewhere.  */
4164       if (GET_CODE (XEXP (x, 0)) == PLUS
4165           && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4166         return gen_binary (PLUS, mode,
4167                            gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4168                                        XEXP (x, 1)),
4169                            XEXP (XEXP (x, 0), 1));
4170
4171       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4172          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4173          bit-field and can be replaced by either a sign_extend or a
4174          sign_extract.  The `and' may be a zero_extend and the two
4175          <c>, -<c> constants may be reversed.  */
4176       if (GET_CODE (XEXP (x, 0)) == XOR
4177           && GET_CODE (XEXP (x, 1)) == CONST_INT
4178           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4179           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4180           && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4181               || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4182           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4183           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4184                && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4185                && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4186                    == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4187               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4188                   && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4189                       == (unsigned int) i + 1))))
4190         return simplify_shift_const
4191           (NULL_RTX, ASHIFTRT, mode,
4192            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4193                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
4194                                  GET_MODE_BITSIZE (mode) - (i + 1)),
4195            GET_MODE_BITSIZE (mode) - (i + 1));
4196
4197       /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4198          C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4199          is 1.  This produces better code than the alternative immediately
4200          below.  */
4201       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4202           && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4203               || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4204           && (reversed = reversed_comparison (XEXP (x, 0), mode,
4205                                               XEXP (XEXP (x, 0), 0),
4206                                               XEXP (XEXP (x, 0), 1))))
4207         return
4208           simplify_gen_unary (NEG, mode, reversed, mode);
4209
4210       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4211          can become (ashiftrt (ashift (xor x 1) C) C) where C is
4212          the bitsize of the mode - 1.  This allows simplification of
4213          "a = (b & 8) == 0;"  */
4214       if (XEXP (x, 1) == constm1_rtx
4215           && GET_CODE (XEXP (x, 0)) != REG
4216           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4217                 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
4218           && nonzero_bits (XEXP (x, 0), mode) == 1)
4219         return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4220            simplify_shift_const (NULL_RTX, ASHIFT, mode,
4221                                  gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4222                                  GET_MODE_BITSIZE (mode) - 1),
4223            GET_MODE_BITSIZE (mode) - 1);
4224
4225       /* If we are adding two things that have no bits in common, convert
4226          the addition into an IOR.  This will often be further simplified,
4227          for example in cases like ((a & 1) + (a & 2)), which can
4228          become a & 3.  */
4229
4230       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4231           && (nonzero_bits (XEXP (x, 0), mode)
4232               & nonzero_bits (XEXP (x, 1), mode)) == 0)
4233         {
4234           /* Try to simplify the expression further.  */
4235           rtx tor = gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4236           temp = combine_simplify_rtx (tor, mode, last, in_dest);
4237
4238           /* If we could, great.  If not, do not go ahead with the IOR
4239              replacement, since PLUS appears in many special purpose
4240              address arithmetic instructions.  */
4241           if (GET_CODE (temp) != CLOBBER && temp != tor)
4242             return temp;
4243         }
4244       break;
4245
4246     case MINUS:
4247       /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4248          by reversing the comparison code if valid.  */
4249       if (STORE_FLAG_VALUE == 1
4250           && XEXP (x, 0) == const1_rtx
4251           && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
4252           && (reversed = reversed_comparison (XEXP (x, 1), mode,
4253                                               XEXP (XEXP (x, 1), 0),
4254                                               XEXP (XEXP (x, 1), 1))))
4255         return reversed;
4256
4257       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4258          (and <foo> (const_int pow2-1))  */
4259       if (GET_CODE (XEXP (x, 1)) == AND
4260           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4261           && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4262           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4263         return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4264                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4265
4266       /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4267        */
4268       if (GET_CODE (XEXP (x, 1)) == MULT
4269           && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4270         {
4271           rtx in1, in2;
4272
4273           in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4274           in2 = XEXP (XEXP (x, 1), 1);
4275           return gen_binary (PLUS, mode, gen_binary (MULT, mode, in1, in2),
4276                              XEXP (x, 0));
4277         }
4278
4279       /* Canonicalize (minus (neg A) (mult B C)) to
4280          (minus (mult (neg B) C) A).  */
4281       if (GET_CODE (XEXP (x, 1)) == MULT
4282           && GET_CODE (XEXP (x, 0)) == NEG)
4283         {
4284           rtx in1, in2;
4285
4286           in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4287           in2 = XEXP (XEXP (x, 1), 1);
4288           return gen_binary (MINUS, mode, gen_binary (MULT, mode, in1, in2),
4289                              XEXP (XEXP (x, 0), 0));
4290         }
4291
4292       /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4293          integers.  */
4294       if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4295         return gen_binary (MINUS, mode,
4296                            gen_binary (MINUS, mode, XEXP (x, 0),
4297                                        XEXP (XEXP (x, 1), 0)),
4298                            XEXP (XEXP (x, 1), 1));
4299       break;
4300
4301     case MULT:
4302       /* If we have (mult (plus A B) C), apply the distributive law and then
4303          the inverse distributive law to see if things simplify.  This
4304          occurs mostly in addresses, often when unrolling loops.  */
4305
4306       if (GET_CODE (XEXP (x, 0)) == PLUS)
4307         {
4308           x = apply_distributive_law
4309             (gen_binary (PLUS, mode,
4310                          gen_binary (MULT, mode,
4311                                      XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4312                          gen_binary (MULT, mode,
4313                                      XEXP (XEXP (x, 0), 1),
4314                                      copy_rtx (XEXP (x, 1)))));
4315
4316           if (GET_CODE (x) != MULT)
4317             return x;
4318         }
4319       /* Try simplify a*(b/c) as (a*b)/c.  */
4320       if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4321           && GET_CODE (XEXP (x, 0)) == DIV)
4322         {
4323           rtx tem = simplify_binary_operation (MULT, mode,
4324                                                XEXP (XEXP (x, 0), 0),
4325                                                XEXP (x, 1));
4326           if (tem)
4327             return gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4328         }
4329       break;
4330
4331     case UDIV:
4332       /* If this is a divide by a power of two, treat it as a shift if
4333          its first operand is a shift.  */
4334       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4335           && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4336           && (GET_CODE (XEXP (x, 0)) == ASHIFT
4337               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4338               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4339               || GET_CODE (XEXP (x, 0)) == ROTATE
4340               || GET_CODE (XEXP (x, 0)) == ROTATERT))
4341         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4342       break;
4343
4344     case EQ:  case NE:
4345     case GT:  case GTU:  case GE:  case GEU:
4346     case LT:  case LTU:  case LE:  case LEU:
4347     case UNEQ:  case LTGT:
4348     case UNGT:  case UNGE:
4349     case UNLT:  case UNLE:
4350     case UNORDERED: case ORDERED:
4351       /* If the first operand is a condition code, we can't do anything
4352          with it.  */
4353       if (GET_CODE (XEXP (x, 0)) == COMPARE
4354           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4355               && ! CC0_P (XEXP (x, 0))))
4356         {
4357           rtx op0 = XEXP (x, 0);
4358           rtx op1 = XEXP (x, 1);
4359           enum rtx_code new_code;
4360
4361           if (GET_CODE (op0) == COMPARE)
4362             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4363
4364           /* Simplify our comparison, if possible.  */
4365           new_code = simplify_comparison (code, &op0, &op1);
4366
4367           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4368              if only the low-order bit is possibly nonzero in X (such as when
4369              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
4370              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
4371              known to be either 0 or -1, NE becomes a NEG and EQ becomes
4372              (plus X 1).
4373
4374              Remove any ZERO_EXTRACT we made when thinking this was a
4375              comparison.  It may now be simpler to use, e.g., an AND.  If a
4376              ZERO_EXTRACT is indeed appropriate, it will be placed back by
4377              the call to make_compound_operation in the SET case.  */
4378
4379           if (STORE_FLAG_VALUE == 1
4380               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4381               && op1 == const0_rtx
4382               && mode == GET_MODE (op0)
4383               && nonzero_bits (op0, mode) == 1)
4384             return gen_lowpart_for_combine (mode,
4385                                             expand_compound_operation (op0));
4386
4387           else if (STORE_FLAG_VALUE == 1
4388                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4389                    && op1 == const0_rtx
4390                    && mode == GET_MODE (op0)
4391                    && (num_sign_bit_copies (op0, mode)
4392                        == GET_MODE_BITSIZE (mode)))
4393             {
4394               op0 = expand_compound_operation (op0);
4395               return simplify_gen_unary (NEG, mode,
4396                                          gen_lowpart_for_combine (mode, op0),
4397                                          mode);
4398             }
4399
4400           else if (STORE_FLAG_VALUE == 1
4401                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4402                    && op1 == const0_rtx
4403                    && mode == GET_MODE (op0)
4404                    && nonzero_bits (op0, mode) == 1)
4405             {
4406               op0 = expand_compound_operation (op0);
4407               return gen_binary (XOR, mode,
4408                                  gen_lowpart_for_combine (mode, op0),
4409                                  const1_rtx);
4410             }
4411
4412           else if (STORE_FLAG_VALUE == 1
4413                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4414                    && op1 == const0_rtx
4415                    && mode == GET_MODE (op0)
4416                    && (num_sign_bit_copies (op0, mode)
4417                        == GET_MODE_BITSIZE (mode)))
4418             {
4419               op0 = expand_compound_operation (op0);
4420               return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4421             }
4422
4423           /* If STORE_FLAG_VALUE is -1, we have cases similar to
4424              those above.  */
4425           if (STORE_FLAG_VALUE == -1
4426               && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4427               && op1 == const0_rtx
4428               && (num_sign_bit_copies (op0, mode)
4429                   == GET_MODE_BITSIZE (mode)))
4430             return gen_lowpart_for_combine (mode,
4431                                             expand_compound_operation (op0));
4432
4433           else if (STORE_FLAG_VALUE == -1
4434                    && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4435                    && op1 == const0_rtx
4436                    && mode == GET_MODE (op0)
4437                    && nonzero_bits (op0, mode) == 1)
4438             {
4439               op0 = expand_compound_operation (op0);
4440               return simplify_gen_unary (NEG, mode,
4441                                          gen_lowpart_for_combine (mode, op0),
4442                                          mode);
4443             }
4444
4445           else if (STORE_FLAG_VALUE == -1
4446                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4447                    && op1 == const0_rtx
4448                    && mode == GET_MODE (op0)
4449                    && (num_sign_bit_copies (op0, mode)
4450                        == GET_MODE_BITSIZE (mode)))
4451             {
4452               op0 = expand_compound_operation (op0);
4453               return simplify_gen_unary (NOT, mode,
4454                                          gen_lowpart_for_combine (mode, op0),
4455                                          mode);
4456             }
4457
4458           /* If X is 0/1, (eq X 0) is X-1.  */
4459           else if (STORE_FLAG_VALUE == -1
4460                    && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4461                    && op1 == const0_rtx
4462                    && mode == GET_MODE (op0)
4463                    && nonzero_bits (op0, mode) == 1)
4464             {
4465               op0 = expand_compound_operation (op0);
4466               return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4467             }
4468
4469           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4470              one bit that might be nonzero, we can convert (ne x 0) to
4471              (ashift x c) where C puts the bit in the sign bit.  Remove any
4472              AND with STORE_FLAG_VALUE when we are done, since we are only
4473              going to test the sign bit.  */
4474           if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4475               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4476               && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4477                   == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4478               && op1 == const0_rtx
4479               && mode == GET_MODE (op0)
4480               && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4481             {
4482               x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4483                                         expand_compound_operation (op0),
4484                                         GET_MODE_BITSIZE (mode) - 1 - i);
4485               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4486                 return XEXP (x, 0);
4487               else
4488                 return x;
4489             }
4490
4491           /* If the code changed, return a whole new comparison.  */
4492           if (new_code != code)
4493             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4494
4495           /* Otherwise, keep this operation, but maybe change its operands.
4496              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
4497           SUBST (XEXP (x, 0), op0);
4498           SUBST (XEXP (x, 1), op1);
4499         }
4500       break;
4501
4502     case IF_THEN_ELSE:
4503       return simplify_if_then_else (x);
4504
4505     case ZERO_EXTRACT:
4506     case SIGN_EXTRACT:
4507     case ZERO_EXTEND:
4508     case SIGN_EXTEND:
4509       /* If we are processing SET_DEST, we are done.  */
4510       if (in_dest)
4511         return x;
4512
4513       return expand_compound_operation (x);
4514
4515     case SET:
4516       return simplify_set (x);
4517
4518     case AND:
4519     case IOR:
4520     case XOR:
4521       return simplify_logical (x, last);
4522
4523     case ABS:
4524       /* (abs (neg <foo>)) -> (abs <foo>) */
4525       if (GET_CODE (XEXP (x, 0)) == NEG)
4526         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4527
4528       /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4529          do nothing.  */
4530       if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4531         break;
4532
4533       /* If operand is something known to be positive, ignore the ABS.  */
4534       if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4535           || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4536                <= HOST_BITS_PER_WIDE_INT)
4537               && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4538                    & ((HOST_WIDE_INT) 1
4539                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4540                   == 0)))
4541         return XEXP (x, 0);
4542
4543       /* If operand is known to be only -1 or 0, convert ABS to NEG.  */
4544       if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4545         return gen_rtx_NEG (mode, XEXP (x, 0));
4546
4547       break;
4548
4549     case FFS:
4550       /* (ffs (*_extend <X>)) = (ffs <X>) */
4551       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4552           || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4553         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4554       break;
4555
4556     case POPCOUNT:
4557     case PARITY:
4558       /* (pop* (zero_extend <X>)) = (pop* <X>) */
4559       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4560         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4561       break;
4562
4563     case FLOAT:
4564       /* (float (sign_extend <X>)) = (float <X>).  */
4565       if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4566         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4567       break;
4568
4569     case ASHIFT:
4570     case LSHIFTRT:
4571     case ASHIFTRT:
4572     case ROTATE:
4573     case ROTATERT:
4574       /* If this is a shift by a constant amount, simplify it.  */
4575       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4576         return simplify_shift_const (x, code, mode, XEXP (x, 0),
4577                                      INTVAL (XEXP (x, 1)));
4578
4579 #ifdef SHIFT_COUNT_TRUNCATED
4580       else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4581         SUBST (XEXP (x, 1),
4582                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4583                               ((HOST_WIDE_INT) 1
4584                                << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4585                               - 1,
4586                               NULL_RTX, 0));
4587 #endif
4588
4589       break;
4590
4591     case VEC_SELECT:
4592       {
4593         rtx op0 = XEXP (x, 0);
4594         rtx op1 = XEXP (x, 1);
4595         int len;
4596
4597         if (GET_CODE (op1) != PARALLEL)
4598           abort ();
4599         len = XVECLEN (op1, 0);
4600         if (len == 1
4601             && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4602             && GET_CODE (op0) == VEC_CONCAT)
4603           {
4604             int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4605
4606             /* Try to find the element in the VEC_CONCAT.  */
4607             for (;;)
4608               {
4609                 if (GET_MODE (op0) == GET_MODE (x))
4610                   return op0;
4611                 if (GET_CODE (op0) == VEC_CONCAT)
4612                   {
4613                     HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4614                     if (op0_size < offset)
4615                       op0 = XEXP (op0, 0);
4616                     else
4617                       {
4618                         offset -= op0_size;
4619                         op0 = XEXP (op0, 1);
4620                       }
4621                   }
4622                 else
4623                   break;
4624               }
4625           }
4626       }
4627
4628       break;
4629
4630     default:
4631       break;
4632     }
4633
4634   return x;
4635 }
4636 \f
4637 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
4638
4639 static rtx
4640 simplify_if_then_else (rtx x)
4641 {
4642   enum machine_mode mode = GET_MODE (x);
4643   rtx cond = XEXP (x, 0);
4644   rtx true_rtx = XEXP (x, 1);
4645   rtx false_rtx = XEXP (x, 2);
4646   enum rtx_code true_code = GET_CODE (cond);
4647   int comparison_p = GET_RTX_CLASS (true_code) == '<';
4648   rtx temp;
4649   int i;
4650   enum rtx_code false_code;
4651   rtx reversed;
4652
4653   /* Simplify storing of the truth value.  */
4654   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4655     return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4656
4657   /* Also when the truth value has to be reversed.  */
4658   if (comparison_p
4659       && true_rtx == const0_rtx && false_rtx == const_true_rtx
4660       && (reversed = reversed_comparison (cond, mode, XEXP (cond, 0),
4661                                           XEXP (cond, 1))))
4662     return reversed;
4663
4664   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4665      in it is being compared against certain values.  Get the true and false
4666      comparisons and see if that says anything about the value of each arm.  */
4667
4668   if (comparison_p
4669       && ((false_code = combine_reversed_comparison_code (cond))
4670           != UNKNOWN)
4671       && GET_CODE (XEXP (cond, 0)) == REG)
4672     {
4673       HOST_WIDE_INT nzb;
4674       rtx from = XEXP (cond, 0);
4675       rtx true_val = XEXP (cond, 1);
4676       rtx false_val = true_val;
4677       int swapped = 0;
4678
4679       /* If FALSE_CODE is EQ, swap the codes and arms.  */
4680
4681       if (false_code == EQ)
4682         {
4683           swapped = 1, true_code = EQ, false_code = NE;
4684           temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4685         }
4686
4687       /* If we are comparing against zero and the expression being tested has
4688          only a single bit that might be nonzero, that is its value when it is
4689          not equal to zero.  Similarly if it is known to be -1 or 0.  */
4690
4691       if (true_code == EQ && true_val == const0_rtx
4692           && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4693         false_code = EQ, false_val = GEN_INT (nzb);
4694       else if (true_code == EQ && true_val == const0_rtx
4695                && (num_sign_bit_copies (from, GET_MODE (from))
4696                    == GET_MODE_BITSIZE (GET_MODE (from))))
4697         false_code = EQ, false_val = constm1_rtx;
4698
4699       /* Now simplify an arm if we know the value of the register in the
4700          branch and it is used in the arm.  Be careful due to the potential
4701          of locally-shared RTL.  */
4702
4703       if (reg_mentioned_p (from, true_rtx))
4704         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4705                                       from, true_val),
4706                       pc_rtx, pc_rtx, 0, 0);
4707       if (reg_mentioned_p (from, false_rtx))
4708         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4709                                    from, false_val),
4710                        pc_rtx, pc_rtx, 0, 0);
4711
4712       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4713       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4714
4715       true_rtx = XEXP (x, 1);
4716       false_rtx = XEXP (x, 2);
4717       true_code = GET_CODE (cond);
4718     }
4719
4720   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4721      reversed, do so to avoid needing two sets of patterns for
4722      subtract-and-branch insns.  Similarly if we have a constant in the true
4723      arm, the false arm is the same as the first operand of the comparison, or
4724      the false arm is more complicated than the true arm.  */
4725
4726   if (comparison_p
4727       && combine_reversed_comparison_code (cond) != UNKNOWN
4728       && (true_rtx == pc_rtx
4729           || (CONSTANT_P (true_rtx)
4730               && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4731           || true_rtx == const0_rtx
4732           || (GET_RTX_CLASS (GET_CODE (true_rtx)) == 'o'
4733               && GET_RTX_CLASS (GET_CODE (false_rtx)) != 'o')
4734           || (GET_CODE (true_rtx) == SUBREG
4735               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true_rtx))) == 'o'
4736               && GET_RTX_CLASS (GET_CODE (false_rtx)) != 'o')
4737           || reg_mentioned_p (true_rtx, false_rtx)
4738           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4739     {
4740       true_code = reversed_comparison_code (cond, NULL);
4741       SUBST (XEXP (x, 0),
4742              reversed_comparison (cond, GET_MODE (cond), XEXP (cond, 0),
4743                                   XEXP (cond, 1)));
4744
4745       SUBST (XEXP (x, 1), false_rtx);
4746       SUBST (XEXP (x, 2), true_rtx);
4747
4748       temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4749       cond = XEXP (x, 0);
4750
4751       /* It is possible that the conditional has been simplified out.  */
4752       true_code = GET_CODE (cond);
4753       comparison_p = GET_RTX_CLASS (true_code) == '<';
4754     }
4755
4756   /* If the two arms are identical, we don't need the comparison.  */
4757
4758   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4759     return true_rtx;
4760
4761   /* Convert a == b ? b : a to "a".  */
4762   if (true_code == EQ && ! side_effects_p (cond)
4763       && !HONOR_NANS (mode)
4764       && rtx_equal_p (XEXP (cond, 0), false_rtx)
4765       && rtx_equal_p (XEXP (cond, 1), true_rtx))
4766     return false_rtx;
4767   else if (true_code == NE && ! side_effects_p (cond)
4768            && !HONOR_NANS (mode)
4769            && rtx_equal_p (XEXP (cond, 0), true_rtx)
4770            && rtx_equal_p (XEXP (cond, 1), false_rtx))
4771     return true_rtx;
4772
4773   /* Look for cases where we have (abs x) or (neg (abs X)).  */
4774
4775   if (GET_MODE_CLASS (mode) == MODE_INT
4776       && GET_CODE (false_rtx) == NEG
4777       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4778       && comparison_p
4779       && rtx_equal_p (true_rtx, XEXP (cond, 0))
4780       && ! side_effects_p (true_rtx))
4781     switch (true_code)
4782       {
4783       case GT:
4784       case GE:
4785         return simplify_gen_unary (ABS, mode, true_rtx, mode);
4786       case LT:
4787       case LE:
4788         return
4789           simplify_gen_unary (NEG, mode,
4790                               simplify_gen_unary (ABS, mode, true_rtx, mode),
4791                               mode);
4792       default:
4793         break;
4794       }
4795
4796   /* Look for MIN or MAX.  */
4797
4798   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4799       && comparison_p
4800       && rtx_equal_p (XEXP (cond, 0), true_rtx)
4801       && rtx_equal_p (XEXP (cond, 1), false_rtx)
4802       && ! side_effects_p (cond))
4803     switch (true_code)
4804       {
4805       case GE:
4806       case GT:
4807         return gen_binary (SMAX, mode, true_rtx, false_rtx);
4808       case LE:
4809       case LT:
4810         return gen_binary (SMIN, mode, true_rtx, false_rtx);
4811       case GEU:
4812       case GTU:
4813         return gen_binary (UMAX, mode, true_rtx, false_rtx);
4814       case LEU:
4815       case LTU:
4816         return gen_binary (UMIN, mode, true_rtx, false_rtx);
4817       default:
4818         break;
4819       }
4820
4821   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4822      second operand is zero, this can be done as (OP Z (mult COND C2)) where
4823      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4824      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4825      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4826      neither 1 or -1, but it isn't worth checking for.  */
4827
4828   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4829       && comparison_p
4830       && GET_MODE_CLASS (mode) == MODE_INT
4831       && ! side_effects_p (x))
4832     {
4833       rtx t = make_compound_operation (true_rtx, SET);
4834       rtx f = make_compound_operation (false_rtx, SET);
4835       rtx cond_op0 = XEXP (cond, 0);
4836       rtx cond_op1 = XEXP (cond, 1);
4837       enum rtx_code op = NIL, extend_op = NIL;
4838       enum machine_mode m = mode;
4839       rtx z = 0, c1 = NULL_RTX;
4840
4841       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4842            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4843            || GET_CODE (t) == ASHIFT
4844            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4845           && rtx_equal_p (XEXP (t, 0), f))
4846         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4847
4848       /* If an identity-zero op is commutative, check whether there
4849          would be a match if we swapped the operands.  */
4850       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4851                 || GET_CODE (t) == XOR)
4852                && rtx_equal_p (XEXP (t, 1), f))
4853         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4854       else if (GET_CODE (t) == SIGN_EXTEND
4855                && (GET_CODE (XEXP (t, 0)) == PLUS
4856                    || GET_CODE (XEXP (t, 0)) == MINUS
4857                    || GET_CODE (XEXP (t, 0)) == IOR
4858                    || GET_CODE (XEXP (t, 0)) == XOR
4859                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4860                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4861                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4862                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4863                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4864                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4865                && (num_sign_bit_copies (f, GET_MODE (f))
4866                    > (unsigned int)
4867                      (GET_MODE_BITSIZE (mode)
4868                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4869         {
4870           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4871           extend_op = SIGN_EXTEND;
4872           m = GET_MODE (XEXP (t, 0));
4873         }
4874       else if (GET_CODE (t) == SIGN_EXTEND
4875                && (GET_CODE (XEXP (t, 0)) == PLUS
4876                    || GET_CODE (XEXP (t, 0)) == IOR
4877                    || GET_CODE (XEXP (t, 0)) == XOR)
4878                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4879                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4880                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4881                && (num_sign_bit_copies (f, GET_MODE (f))
4882                    > (unsigned int)
4883                      (GET_MODE_BITSIZE (mode)
4884                       - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4885         {
4886           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4887           extend_op = SIGN_EXTEND;
4888           m = GET_MODE (XEXP (t, 0));
4889         }
4890       else if (GET_CODE (t) == ZERO_EXTEND
4891                && (GET_CODE (XEXP (t, 0)) == PLUS
4892                    || GET_CODE (XEXP (t, 0)) == MINUS
4893                    || GET_CODE (XEXP (t, 0)) == IOR
4894                    || GET_CODE (XEXP (t, 0)) == XOR
4895                    || GET_CODE (XEXP (t, 0)) == ASHIFT
4896                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4897                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4898                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4899                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4900                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4901                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4902                && ((nonzero_bits (f, GET_MODE (f))
4903                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4904                    == 0))
4905         {
4906           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4907           extend_op = ZERO_EXTEND;
4908           m = GET_MODE (XEXP (t, 0));
4909         }
4910       else if (GET_CODE (t) == ZERO_EXTEND
4911                && (GET_CODE (XEXP (t, 0)) == PLUS
4912                    || GET_CODE (XEXP (t, 0)) == IOR
4913                    || GET_CODE (XEXP (t, 0)) == XOR)
4914                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4915                && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4916                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4917                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4918                && ((nonzero_bits (f, GET_MODE (f))
4919                     & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4920                    == 0))
4921         {
4922           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4923           extend_op = ZERO_EXTEND;
4924           m = GET_MODE (XEXP (t, 0));
4925         }
4926
4927       if (z)
4928         {
4929           temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4930                         pc_rtx, pc_rtx, 0, 0);
4931           temp = gen_binary (MULT, m, temp,
4932                              gen_binary (MULT, m, c1, const_true_rtx));
4933           temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4934           temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4935
4936           if (extend_op != NIL)
4937             temp = simplify_gen_unary (extend_op, mode, temp, m);
4938
4939           return temp;
4940         }
4941     }
4942
4943   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4944      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4945      negation of a single bit, we can convert this operation to a shift.  We
4946      can actually do this more generally, but it doesn't seem worth it.  */
4947
4948   if (true_code == NE && XEXP (cond, 1) == const0_rtx
4949       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
4950       && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4951            && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
4952           || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4953                == GET_MODE_BITSIZE (mode))
4954               && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
4955     return
4956       simplify_shift_const (NULL_RTX, ASHIFT, mode,
4957                             gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4958
4959   /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8.  */
4960   if (true_code == NE && XEXP (cond, 1) == const0_rtx
4961       && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
4962       && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
4963           == nonzero_bits (XEXP (cond, 0), mode)
4964       && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
4965     return XEXP (cond, 0);
4966
4967   return x;
4968 }
4969 \f
4970 /* Simplify X, a SET expression.  Return the new expression.  */
4971
4972 static rtx
4973 simplify_set (rtx x)
4974 {
4975   rtx src = SET_SRC (x);
4976   rtx dest = SET_DEST (x);
4977   enum machine_mode mode
4978     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4979   rtx other_insn;
4980   rtx *cc_use;
4981
4982   /* (set (pc) (return)) gets written as (return).  */
4983   if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4984     return src;
4985
4986   /* Now that we know for sure which bits of SRC we are using, see if we can
4987      simplify the expression for the object knowing that we only need the
4988      low-order bits.  */
4989
4990   if (GET_MODE_CLASS (mode) == MODE_INT
4991       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
4992     {
4993       src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
4994       SUBST (SET_SRC (x), src);
4995     }
4996
4997   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4998      the comparison result and try to simplify it unless we already have used
4999      undobuf.other_insn.  */
5000   if ((GET_MODE_CLASS (mode) == MODE_CC
5001        || GET_CODE (src) == COMPARE
5002        || CC0_P (dest))
5003       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5004       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5005       && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
5006       && rtx_equal_p (XEXP (*cc_use, 0), dest))
5007     {
5008       enum rtx_code old_code = GET_CODE (*cc_use);
5009       enum rtx_code new_code;
5010       rtx op0, op1, tmp;
5011       int other_changed = 0;
5012       enum machine_mode compare_mode = GET_MODE (dest);
5013       enum machine_mode tmp_mode;
5014
5015       if (GET_CODE (src) == COMPARE)
5016         op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5017       else
5018         op0 = src, op1 = const0_rtx;
5019
5020       /* Check whether the comparison is known at compile time.  */
5021       if (GET_MODE (op0) != VOIDmode)
5022         tmp_mode = GET_MODE (op0);
5023       else if (GET_MODE (op1) != VOIDmode)
5024         tmp_mode = GET_MODE (op1);
5025       else
5026         tmp_mode = compare_mode;
5027       tmp = simplify_relational_operation (old_code, tmp_mode, op0, op1);
5028       if (tmp != NULL_RTX)
5029         {
5030           rtx pat = PATTERN (other_insn);
5031           undobuf.other_insn = other_insn;
5032           SUBST (*cc_use, tmp);
5033
5034           /* Attempt to simplify CC user.  */
5035           if (GET_CODE (pat) == SET)
5036             {
5037               rtx new = simplify_rtx (SET_SRC (pat));
5038               if (new != NULL_RTX)
5039                 SUBST (SET_SRC (pat), new);
5040             }
5041
5042           /* Convert X into a no-op move.  */
5043           SUBST (SET_DEST (x), pc_rtx);
5044           SUBST (SET_SRC (x), pc_rtx);
5045           return x;
5046         }
5047
5048       /* Simplify our comparison, if possible.  */
5049       new_code = simplify_comparison (old_code, &op0, &op1);
5050
5051 #ifdef EXTRA_CC_MODES
5052       /* If this machine has CC modes other than CCmode, check to see if we
5053          need to use a different CC mode here.  */
5054       compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5055 #endif /* EXTRA_CC_MODES */
5056
5057 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
5058       /* If the mode changed, we have to change SET_DEST, the mode in the
5059          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
5060          a hard register, just build new versions with the proper mode.  If it
5061          is a pseudo, we lose unless it is only time we set the pseudo, in
5062          which case we can safely change its mode.  */
5063       if (compare_mode != GET_MODE (dest))
5064         {
5065           unsigned int regno = REGNO (dest);
5066           rtx new_dest = gen_rtx_REG (compare_mode, regno);
5067
5068           if (regno < FIRST_PSEUDO_REGISTER
5069               || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5070             {
5071               if (regno >= FIRST_PSEUDO_REGISTER)
5072                 SUBST (regno_reg_rtx[regno], new_dest);
5073
5074               SUBST (SET_DEST (x), new_dest);
5075               SUBST (XEXP (*cc_use, 0), new_dest);
5076               other_changed = 1;
5077
5078               dest = new_dest;
5079             }
5080         }
5081 #endif
5082
5083       /* If the code changed, we have to build a new comparison in
5084          undobuf.other_insn.  */
5085       if (new_code != old_code)
5086         {
5087           unsigned HOST_WIDE_INT mask;
5088
5089           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5090                                           dest, const0_rtx));
5091
5092           /* If the only change we made was to change an EQ into an NE or
5093              vice versa, OP0 has only one bit that might be nonzero, and OP1
5094              is zero, check if changing the user of the condition code will
5095              produce a valid insn.  If it won't, we can keep the original code
5096              in that insn by surrounding our operation with an XOR.  */
5097
5098           if (((old_code == NE && new_code == EQ)
5099                || (old_code == EQ && new_code == NE))
5100               && ! other_changed && op1 == const0_rtx
5101               && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5102               && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5103             {
5104               rtx pat = PATTERN (other_insn), note = 0;
5105
5106               if ((recog_for_combine (&pat, other_insn, &note) < 0
5107                    && ! check_asm_operands (pat)))
5108                 {
5109                   PUT_CODE (*cc_use, old_code);
5110                   other_insn = 0;
5111
5112                   op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
5113                 }
5114             }
5115
5116           other_changed = 1;
5117         }
5118
5119       if (other_changed)
5120         undobuf.other_insn = other_insn;
5121
5122 #ifdef HAVE_cc0
5123       /* If we are now comparing against zero, change our source if
5124          needed.  If we do not use cc0, we always have a COMPARE.  */
5125       if (op1 == const0_rtx && dest == cc0_rtx)
5126         {
5127           SUBST (SET_SRC (x), op0);
5128           src = op0;
5129         }
5130       else
5131 #endif
5132
5133       /* Otherwise, if we didn't previously have a COMPARE in the
5134          correct mode, we need one.  */
5135       if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5136         {
5137           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5138           src = SET_SRC (x);
5139         }
5140       else
5141         {
5142           /* Otherwise, update the COMPARE if needed.  */
5143           SUBST (XEXP (src, 0), op0);
5144           SUBST (XEXP (src, 1), op1);
5145         }
5146     }
5147   else
5148     {
5149       /* Get SET_SRC in a form where we have placed back any
5150          compound expressions.  Then do the checks below.  */
5151       src = make_compound_operation (src, SET);
5152       SUBST (SET_SRC (x), src);
5153     }
5154
5155 #ifdef WORD_REGISTER_OPERATIONS
5156   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5157      and X being a REG or (subreg (reg)), we may be able to convert this to
5158      (set (subreg:m2 x) (op)).
5159
5160      On a machine where WORD_REGISTER_OPERATIONS is defined, this
5161      transformation is safe as long as M1 and M2 have the same number
5162      of words.
5163
5164      However, on a machine without WORD_REGISTER_OPERATIONS defined,
5165      we cannot apply this transformation because it would create a
5166      paradoxical subreg in SET_DEST.  */
5167
5168   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5169       && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
5170       && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5171            / UNITS_PER_WORD)
5172           == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5173                + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5174 #ifdef CANNOT_CHANGE_MODE_CLASS
5175       && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
5176             && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5177                                          GET_MODE (SUBREG_REG (src)),
5178                                          GET_MODE (src)))
5179 #endif
5180       && (GET_CODE (dest) == REG
5181           || (GET_CODE (dest) == SUBREG
5182               && GET_CODE (SUBREG_REG (dest)) == REG)))
5183     {
5184       SUBST (SET_DEST (x),
5185              gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
5186                                       dest));
5187       SUBST (SET_SRC (x), SUBREG_REG (src));
5188
5189       src = SET_SRC (x), dest = SET_DEST (x);
5190     }
5191 #endif
5192
5193 #ifdef HAVE_cc0
5194   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5195      in SRC.  */
5196   if (dest == cc0_rtx
5197       && GET_CODE (src) == SUBREG
5198       && subreg_lowpart_p (src)
5199       && (GET_MODE_BITSIZE (GET_MODE (src))
5200           < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5201     {
5202       rtx inner = SUBREG_REG (src);
5203       enum machine_mode inner_mode = GET_MODE (inner);
5204
5205       /* Here we make sure that we don't have a sign bit on.  */
5206       if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5207           && (nonzero_bits (inner, inner_mode)
5208               < ((unsigned HOST_WIDE_INT) 1
5209                  << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5210         {
5211           SUBST (SET_SRC (x), inner);
5212           src = SET_SRC (x);
5213         }
5214     }
5215 #endif
5216
5217 #ifdef LOAD_EXTEND_OP
5218   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5219      would require a paradoxical subreg.  Replace the subreg with a
5220      zero_extend to avoid the reload that would otherwise be required.  */
5221
5222   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5223       && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
5224       && SUBREG_BYTE (src) == 0
5225       && (GET_MODE_SIZE (GET_MODE (src))
5226           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5227       && GET_CODE (SUBREG_REG (src)) == MEM)
5228     {
5229       SUBST (SET_SRC (x),
5230              gen_rtx (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5231                       GET_MODE (src), SUBREG_REG (src)));
5232
5233       src = SET_SRC (x);
5234     }
5235 #endif
5236
5237   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5238      are comparing an item known to be 0 or -1 against 0, use a logical
5239      operation instead. Check for one of the arms being an IOR of the other
5240      arm with some value.  We compute three terms to be IOR'ed together.  In
5241      practice, at most two will be nonzero.  Then we do the IOR's.  */
5242
5243   if (GET_CODE (dest) != PC
5244       && GET_CODE (src) == IF_THEN_ELSE
5245       && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5246       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5247       && XEXP (XEXP (src, 0), 1) == const0_rtx
5248       && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5249 #ifdef HAVE_conditional_move
5250       && ! can_conditionally_move_p (GET_MODE (src))
5251 #endif
5252       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5253                                GET_MODE (XEXP (XEXP (src, 0), 0)))
5254           == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5255       && ! side_effects_p (src))
5256     {
5257       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5258                       ? XEXP (src, 1) : XEXP (src, 2));
5259       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5260                    ? XEXP (src, 2) : XEXP (src, 1));
5261       rtx term1 = const0_rtx, term2, term3;
5262
5263       if (GET_CODE (true_rtx) == IOR
5264           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5265         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5266       else if (GET_CODE (true_rtx) == IOR
5267                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5268         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5269       else if (GET_CODE (false_rtx) == IOR
5270                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5271         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5272       else if (GET_CODE (false_rtx) == IOR
5273                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5274         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5275
5276       term2 = gen_binary (AND, GET_MODE (src),
5277                           XEXP (XEXP (src, 0), 0), true_rtx);
5278       term3 = gen_binary (AND, GET_MODE (src),
5279                           simplify_gen_unary (NOT, GET_MODE (src),
5280                                               XEXP (XEXP (src, 0), 0),
5281                                               GET_MODE (src)),
5282                           false_rtx);
5283
5284       SUBST (SET_SRC (x),
5285              gen_binary (IOR, GET_MODE (src),
5286                          gen_binary (IOR, GET_MODE (src), term1, term2),
5287                          term3));
5288
5289       src = SET_SRC (x);
5290     }
5291
5292   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5293      whole thing fail.  */
5294   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5295     return src;
5296   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5297     return dest;
5298   else
5299     /* Convert this into a field assignment operation, if possible.  */
5300     return make_field_assignment (x);
5301 }
5302 \f
5303 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5304    result.  LAST is nonzero if this is the last retry.  */
5305
5306 static rtx
5307 simplify_logical (rtx x, int last)
5308 {
5309   enum machine_mode mode = GET_MODE (x);
5310   rtx op0 = XEXP (x, 0);
5311   rtx op1 = XEXP (x, 1);
5312   rtx reversed;
5313
5314   switch (GET_CODE (x))
5315     {
5316     case AND:
5317       /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5318          insn (and may simplify more).  */
5319       if (GET_CODE (op0) == XOR
5320           && rtx_equal_p (XEXP (op0, 0), op1)
5321           && ! side_effects_p (op1))
5322         x = gen_binary (AND, mode,
5323                         simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5324                         op1);
5325
5326       if (GET_CODE (op0) == XOR
5327           && rtx_equal_p (XEXP (op0, 1), op1)
5328           && ! side_effects_p (op1))
5329         x = gen_binary (AND, mode,
5330                         simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5331                         op1);
5332
5333       /* Similarly for (~(A ^ B)) & A.  */
5334       if (GET_CODE (op0) == NOT
5335           && GET_CODE (XEXP (op0, 0)) == XOR
5336           && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5337           && ! side_effects_p (op1))
5338         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5339
5340       if (GET_CODE (op0) == NOT
5341           && GET_CODE (XEXP (op0, 0)) == XOR
5342           && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5343           && ! side_effects_p (op1))
5344         x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5345
5346       /* We can call simplify_and_const_int only if we don't lose
5347          any (sign) bits when converting INTVAL (op1) to
5348          "unsigned HOST_WIDE_INT".  */
5349       if (GET_CODE (op1) == CONST_INT
5350           && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5351               || INTVAL (op1) > 0))
5352         {
5353           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5354
5355           /* If we have (ior (and (X C1) C2)) and the next restart would be
5356              the last, simplify this by making C1 as small as possible
5357              and then exit.  */
5358           if (last
5359               && GET_CODE (x) == IOR && GET_CODE (op0) == AND
5360               && GET_CODE (XEXP (op0, 1)) == CONST_INT
5361               && GET_CODE (op1) == CONST_INT)
5362             return gen_binary (IOR, mode,
5363                                gen_binary (AND, mode, XEXP (op0, 0),
5364                                            GEN_INT (INTVAL (XEXP (op0, 1))
5365                                                     & ~INTVAL (op1))), op1);
5366
5367           if (GET_CODE (x) != AND)
5368             return x;
5369
5370           if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
5371               || GET_RTX_CLASS (GET_CODE (x)) == '2')
5372             op0 = XEXP (x, 0), op1 = XEXP (x, 1);
5373         }
5374
5375       /* Convert (A | B) & A to A.  */
5376       if (GET_CODE (op0) == IOR
5377           && (rtx_equal_p (XEXP (op0, 0), op1)
5378               || rtx_equal_p (XEXP (op0, 1), op1))
5379           && ! side_effects_p (XEXP (op0, 0))
5380           && ! side_effects_p (XEXP (op0, 1)))
5381         return op1;
5382
5383       /* In the following group of tests (and those in case IOR below),
5384          we start with some combination of logical operations and apply
5385          the distributive law followed by the inverse distributive law.
5386          Most of the time, this results in no change.  However, if some of
5387          the operands are the same or inverses of each other, simplifications
5388          will result.
5389
5390          For example, (and (ior A B) (not B)) can occur as the result of
5391          expanding a bit field assignment.  When we apply the distributive
5392          law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5393          which then simplifies to (and (A (not B))).
5394
5395          If we have (and (ior A B) C), apply the distributive law and then
5396          the inverse distributive law to see if things simplify.  */
5397
5398       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5399         {
5400           x = apply_distributive_law
5401             (gen_binary (GET_CODE (op0), mode,
5402                          gen_binary (AND, mode, XEXP (op0, 0), op1),
5403                          gen_binary (AND, mode, XEXP (op0, 1),
5404                                      copy_rtx (op1))));
5405           if (GET_CODE (x) != AND)
5406             return x;
5407         }
5408
5409       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5410         return apply_distributive_law
5411           (gen_binary (GET_CODE (op1), mode,
5412                        gen_binary (AND, mode, XEXP (op1, 0), op0),
5413                        gen_binary (AND, mode, XEXP (op1, 1),
5414                                    copy_rtx (op0))));
5415
5416       /* Similarly, taking advantage of the fact that
5417          (and (not A) (xor B C)) == (xor (ior A B) (ior A C))  */
5418
5419       if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5420         return apply_distributive_law
5421           (gen_binary (XOR, mode,
5422                        gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5423                        gen_binary (IOR, mode, copy_rtx (XEXP (op0, 0)),
5424                                    XEXP (op1, 1))));
5425
5426       else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5427         return apply_distributive_law
5428           (gen_binary (XOR, mode,
5429                        gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5430                        gen_binary (IOR, mode, copy_rtx (XEXP (op1, 0)), XEXP (op0, 1))));
5431       break;
5432
5433     case IOR:
5434       /* (ior A C) is C if all bits of A that might be nonzero are on in C.  */
5435       if (GET_CODE (op1) == CONST_INT
5436           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5437           && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5438         return op1;
5439
5440       /* Convert (A & B) | A to A.  */
5441       if (GET_CODE (op0) == AND
5442           && (rtx_equal_p (XEXP (op0, 0), op1)
5443               || rtx_equal_p (XEXP (op0, 1), op1))
5444           && ! side_effects_p (XEXP (op0, 0))
5445           && ! side_effects_p (XEXP (op0, 1)))
5446         return op1;
5447
5448       /* If we have (ior (and A B) C), apply the distributive law and then
5449          the inverse distributive law to see if things simplify.  */
5450
5451       if (GET_CODE (op0) == AND)
5452         {
5453           x = apply_distributive_law
5454             (gen_binary (AND, mode,
5455                          gen_binary (IOR, mode, XEXP (op0, 0), op1),
5456                          gen_binary (IOR, mode, XEXP (op0, 1),
5457                                      copy_rtx (op1))));
5458
5459           if (GET_CODE (x) != IOR)
5460             return x;
5461         }
5462
5463       if (GET_CODE (op1) == AND)
5464         {
5465           x = apply_distributive_law
5466             (gen_binary (AND, mode,
5467                          gen_binary (IOR, mode, XEXP (op1, 0), op0),
5468                          gen_binary (IOR, mode, XEXP (op1, 1),
5469                                      copy_rtx (op0))));
5470
5471           if (GET_CODE (x) != IOR)
5472             return x;
5473         }
5474
5475       /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5476          mode size to (rotate A CX).  */
5477
5478       if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5479            || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5480           && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5481           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5482           && GET_CODE (XEXP (op1, 1)) == CONST_INT
5483           && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5484               == GET_MODE_BITSIZE (mode)))
5485         return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5486                                (GET_CODE (op0) == ASHIFT
5487                                 ? XEXP (op0, 1) : XEXP (op1, 1)));
5488
5489       /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5490          a (sign_extend (plus ...)).  If so, OP1 is a CONST_INT, and the PLUS
5491          does not affect any of the bits in OP1, it can really be done
5492          as a PLUS and we can associate.  We do this by seeing if OP1
5493          can be safely shifted left C bits.  */
5494       if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5495           && GET_CODE (XEXP (op0, 0)) == PLUS
5496           && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5497           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5498           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5499         {
5500           int count = INTVAL (XEXP (op0, 1));
5501           HOST_WIDE_INT mask = INTVAL (op1) << count;
5502
5503           if (mask >> count == INTVAL (op1)
5504               && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5505             {
5506               SUBST (XEXP (XEXP (op0, 0), 1),
5507                      GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5508               return op0;
5509             }
5510         }
5511       break;
5512
5513     case XOR:
5514       /* If we are XORing two things that have no bits in common,
5515          convert them into an IOR.  This helps to detect rotation encoded
5516          using those methods and possibly other simplifications.  */
5517
5518       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5519           && (nonzero_bits (op0, mode)
5520               & nonzero_bits (op1, mode)) == 0)
5521         return (gen_binary (IOR, mode, op0, op1));
5522
5523       /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5524          Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5525          (NOT y).  */
5526       {
5527         int num_negated = 0;
5528
5529         if (GET_CODE (op0) == NOT)
5530           num_negated++, op0 = XEXP (op0, 0);
5531         if (GET_CODE (op1) == NOT)
5532           num_negated++, op1 = XEXP (op1, 0);
5533
5534         if (num_negated == 2)
5535           {
5536             SUBST (XEXP (x, 0), op0);
5537             SUBST (XEXP (x, 1), op1);
5538           }
5539         else if (num_negated == 1)
5540           return
5541             simplify_gen_unary (NOT, mode, gen_binary (XOR, mode, op0, op1),
5542                                 mode);
5543       }
5544
5545       /* Convert (xor (and A B) B) to (and (not A) B).  The latter may
5546          correspond to a machine insn or result in further simplifications
5547          if B is a constant.  */
5548
5549       if (GET_CODE (op0) == AND
5550           && rtx_equal_p (XEXP (op0, 1), op1)
5551           && ! side_effects_p (op1))
5552         return gen_binary (AND, mode,
5553                            simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5554                            op1);
5555
5556       else if (GET_CODE (op0) == AND
5557                && rtx_equal_p (XEXP (op0, 0), op1)
5558                && ! side_effects_p (op1))
5559         return gen_binary (AND, mode,
5560                            simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5561                            op1);
5562
5563       /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5564          comparison if STORE_FLAG_VALUE is 1.  */
5565       if (STORE_FLAG_VALUE == 1
5566           && op1 == const1_rtx
5567           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5568           && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5569                                               XEXP (op0, 1))))
5570         return reversed;
5571
5572       /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5573          is (lt foo (const_int 0)), so we can perform the above
5574          simplification if STORE_FLAG_VALUE is 1.  */
5575
5576       if (STORE_FLAG_VALUE == 1
5577           && op1 == const1_rtx
5578           && GET_CODE (op0) == LSHIFTRT
5579           && GET_CODE (XEXP (op0, 1)) == CONST_INT
5580           && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5581         return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5582
5583       /* (xor (comparison foo bar) (const_int sign-bit))
5584          when STORE_FLAG_VALUE is the sign bit.  */
5585       if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5586           && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5587               == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5588           && op1 == const_true_rtx
5589           && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5590           && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5591                                               XEXP (op0, 1))))
5592         return reversed;
5593
5594       break;
5595
5596     default:
5597       abort ();
5598     }
5599
5600   return x;
5601 }
5602 \f
5603 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5604    operations" because they can be replaced with two more basic operations.
5605    ZERO_EXTEND is also considered "compound" because it can be replaced with
5606    an AND operation, which is simpler, though only one operation.
5607
5608    The function expand_compound_operation is called with an rtx expression
5609    and will convert it to the appropriate shifts and AND operations,
5610    simplifying at each stage.
5611
5612    The function make_compound_operation is called to convert an expression
5613    consisting of shifts and ANDs into the equivalent compound expression.
5614    It is the inverse of this function, loosely speaking.  */
5615
5616 static rtx
5617 expand_compound_operation (rtx x)
5618 {
5619   unsigned HOST_WIDE_INT pos = 0, len;
5620   int unsignedp = 0;
5621   unsigned int modewidth;
5622   rtx tem;
5623
5624   switch (GET_CODE (x))
5625     {
5626     case ZERO_EXTEND:
5627       unsignedp = 1;
5628     case SIGN_EXTEND:
5629       /* We can't necessarily use a const_int for a multiword mode;
5630          it depends on implicitly extending the value.
5631          Since we don't know the right way to extend it,
5632          we can't tell whether the implicit way is right.
5633
5634          Even for a mode that is no wider than a const_int,
5635          we can't win, because we need to sign extend one of its bits through
5636          the rest of it, and we don't know which bit.  */
5637       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5638         return x;
5639
5640       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5641          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
5642          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5643          reloaded. If not for that, MEM's would very rarely be safe.
5644
5645          Reject MODEs bigger than a word, because we might not be able
5646          to reference a two-register group starting with an arbitrary register
5647          (and currently gen_lowpart might crash for a SUBREG).  */
5648
5649       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5650         return x;
5651
5652       /* Reject MODEs that aren't scalar integers because turning vector
5653          or complex modes into shifts causes problems.  */
5654
5655       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5656         return x;
5657
5658       len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5659       /* If the inner object has VOIDmode (the only way this can happen
5660          is if it is an ASM_OPERANDS), we can't do anything since we don't
5661          know how much masking to do.  */
5662       if (len == 0)
5663         return x;
5664
5665       break;
5666
5667     case ZERO_EXTRACT:
5668       unsignedp = 1;
5669     case SIGN_EXTRACT:
5670       /* If the operand is a CLOBBER, just return it.  */
5671       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5672         return XEXP (x, 0);
5673
5674       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5675           || GET_CODE (XEXP (x, 2)) != CONST_INT
5676           || GET_MODE (XEXP (x, 0)) == VOIDmode)
5677         return x;
5678
5679       /* Reject MODEs that aren't scalar integers because turning vector
5680          or complex modes into shifts causes problems.  */
5681
5682       if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5683         return x;
5684
5685       len = INTVAL (XEXP (x, 1));
5686       pos = INTVAL (XEXP (x, 2));
5687
5688       /* If this goes outside the object being extracted, replace the object
5689          with a (use (mem ...)) construct that only combine understands
5690          and is used only for this purpose.  */
5691       if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5692         SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5693
5694       if (BITS_BIG_ENDIAN)
5695         pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5696
5697       break;
5698
5699     default:
5700       return x;
5701     }
5702   /* Convert sign extension to zero extension, if we know that the high
5703      bit is not set, as this is easier to optimize.  It will be converted
5704      back to cheaper alternative in make_extraction.  */
5705   if (GET_CODE (x) == SIGN_EXTEND
5706       && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5707           && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5708                 & ~(((unsigned HOST_WIDE_INT)
5709                       GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5710                      >> 1))
5711                == 0)))
5712     {
5713       rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5714       rtx temp2 = expand_compound_operation (temp);
5715
5716       /* Make sure this is a profitable operation.  */
5717       if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5718        return temp2;
5719       else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5720        return temp;
5721       else
5722        return x;
5723     }
5724
5725   /* We can optimize some special cases of ZERO_EXTEND.  */
5726   if (GET_CODE (x) == ZERO_EXTEND)
5727     {
5728       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5729          know that the last value didn't have any inappropriate bits
5730          set.  */
5731       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5732           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5733           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5734           && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5735               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5736         return XEXP (XEXP (x, 0), 0);
5737
5738       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5739       if (GET_CODE (XEXP (x, 0)) == SUBREG
5740           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5741           && subreg_lowpart_p (XEXP (x, 0))
5742           && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5743           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5744               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5745         return SUBREG_REG (XEXP (x, 0));
5746
5747       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5748          is a comparison and STORE_FLAG_VALUE permits.  This is like
5749          the first case, but it works even when GET_MODE (x) is larger
5750          than HOST_WIDE_INT.  */
5751       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5752           && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5753           && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5754           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5755               <= HOST_BITS_PER_WIDE_INT)
5756           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5757               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5758         return XEXP (XEXP (x, 0), 0);
5759
5760       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
5761       if (GET_CODE (XEXP (x, 0)) == SUBREG
5762           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5763           && subreg_lowpart_p (XEXP (x, 0))
5764           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5765           && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5766               <= HOST_BITS_PER_WIDE_INT)
5767           && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5768               & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5769         return SUBREG_REG (XEXP (x, 0));
5770
5771     }
5772
5773   /* If we reach here, we want to return a pair of shifts.  The inner
5774      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
5775      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
5776      logical depending on the value of UNSIGNEDP.
5777
5778      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5779      converted into an AND of a shift.
5780
5781      We must check for the case where the left shift would have a negative
5782      count.  This can happen in a case like (x >> 31) & 255 on machines
5783      that can't shift by a constant.  On those machines, we would first
5784      combine the shift with the AND to produce a variable-position
5785      extraction.  Then the constant of 31 would be substituted in to produce
5786      a such a position.  */
5787
5788   modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5789   if (modewidth + len >= pos)
5790     tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5791                                 GET_MODE (x),
5792                                 simplify_shift_const (NULL_RTX, ASHIFT,
5793                                                       GET_MODE (x),
5794                                                       XEXP (x, 0),
5795                                                       modewidth - pos - len),
5796                                 modewidth - len);
5797
5798   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5799     tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5800                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
5801                                                         GET_MODE (x),
5802                                                         XEXP (x, 0), pos),
5803                                   ((HOST_WIDE_INT) 1 << len) - 1);
5804   else
5805     /* Any other cases we can't handle.  */
5806     return x;
5807
5808   /* If we couldn't do this for some reason, return the original
5809      expression.  */
5810   if (GET_CODE (tem) == CLOBBER)
5811     return x;
5812
5813   return tem;
5814 }
5815 \f
5816 /* X is a SET which contains an assignment of one object into
5817    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5818    or certain SUBREGS). If possible, convert it into a series of
5819    logical operations.
5820
5821    We half-heartedly support variable positions, but do not at all
5822    support variable lengths.  */
5823
5824 static rtx
5825 expand_field_assignment (rtx x)
5826 {
5827   rtx inner;
5828   rtx pos;                      /* Always counts from low bit.  */
5829   int len;
5830   rtx mask;
5831   enum machine_mode compute_mode;
5832
5833   /* Loop until we find something we can't simplify.  */
5834   while (1)
5835     {
5836       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5837           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5838         {
5839           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5840           len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5841           pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5842         }
5843       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5844                && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5845         {
5846           inner = XEXP (SET_DEST (x), 0);
5847           len = INTVAL (XEXP (SET_DEST (x), 1));
5848           pos = XEXP (SET_DEST (x), 2);
5849
5850           /* If the position is constant and spans the width of INNER,
5851              surround INNER  with a USE to indicate this.  */
5852           if (GET_CODE (pos) == CONST_INT
5853               && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5854             inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5855
5856           if (BITS_BIG_ENDIAN)
5857             {
5858               if (GET_CODE (pos) == CONST_INT)
5859                 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5860                                - INTVAL (pos));
5861               else if (GET_CODE (pos) == MINUS
5862                        && GET_CODE (XEXP (pos, 1)) == CONST_INT
5863                        && (INTVAL (XEXP (pos, 1))
5864                            == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5865                 /* If position is ADJUST - X, new position is X.  */
5866                 pos = XEXP (pos, 0);
5867               else
5868                 pos = gen_binary (MINUS, GET_MODE (pos),
5869                                   GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5870                                            - len),
5871                                   pos);
5872             }
5873         }
5874
5875       /* A SUBREG between two modes that occupy the same numbers of words
5876          can be done by moving the SUBREG to the source.  */
5877       else if (GET_CODE (SET_DEST (x)) == SUBREG
5878                /* We need SUBREGs to compute nonzero_bits properly.  */
5879                && nonzero_sign_valid
5880                && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5881                      + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5882                    == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5883                         + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5884         {
5885           x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5886                            gen_lowpart_for_combine
5887                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
5888                             SET_SRC (x)));
5889           continue;
5890         }
5891       else
5892         break;
5893
5894       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5895         inner = SUBREG_REG (inner);
5896
5897       compute_mode = GET_MODE (inner);
5898
5899       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
5900       if (! SCALAR_INT_MODE_P (compute_mode))
5901         {
5902           enum machine_mode imode;
5903
5904           /* Don't do anything for vector or complex integral types.  */
5905           if (! FLOAT_MODE_P (compute_mode))
5906             break;
5907
5908           /* Try to find an integral mode to pun with.  */
5909           imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5910           if (imode == BLKmode)
5911             break;
5912
5913           compute_mode = imode;
5914           inner = gen_lowpart_for_combine (imode, inner);
5915         }
5916
5917       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
5918       if (len < HOST_BITS_PER_WIDE_INT)
5919         mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5920       else
5921         break;
5922
5923       /* Now compute the equivalent expression.  Make a copy of INNER
5924          for the SET_DEST in case it is a MEM into which we will substitute;
5925          we don't want shared RTL in that case.  */
5926       x = gen_rtx_SET
5927         (VOIDmode, copy_rtx (inner),
5928          gen_binary (IOR, compute_mode,
5929                      gen_binary (AND, compute_mode,
5930                                  simplify_gen_unary (NOT, compute_mode,
5931                                                      gen_binary (ASHIFT,
5932                                                                  compute_mode,
5933                                                                  mask, pos),
5934                                                      compute_mode),
5935                                  inner),
5936                      gen_binary (ASHIFT, compute_mode,
5937                                  gen_binary (AND, compute_mode,
5938                                              gen_lowpart_for_combine
5939                                              (compute_mode, SET_SRC (x)),
5940                                              mask),
5941                                  pos)));
5942     }
5943
5944   return x;
5945 }
5946 \f
5947 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
5948    it is an RTX that represents a variable starting position; otherwise,
5949    POS is the (constant) starting bit position (counted from the LSB).
5950
5951    INNER may be a USE.  This will occur when we started with a bitfield
5952    that went outside the boundary of the object in memory, which is
5953    allowed on most machines.  To isolate this case, we produce a USE
5954    whose mode is wide enough and surround the MEM with it.  The only
5955    code that understands the USE is this routine.  If it is not removed,
5956    it will cause the resulting insn not to match.
5957
5958    UNSIGNEDP is nonzero for an unsigned reference and zero for a
5959    signed reference.
5960
5961    IN_DEST is nonzero if this is a reference in the destination of a
5962    SET.  This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
5963    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5964    be used.
5965
5966    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
5967    ZERO_EXTRACT should be built even for bits starting at bit 0.
5968
5969    MODE is the desired mode of the result (if IN_DEST == 0).
5970
5971    The result is an RTX for the extraction or NULL_RTX if the target
5972    can't handle it.  */
5973
5974 static rtx
5975 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
5976                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
5977                  int in_dest, int in_compare)
5978 {
5979   /* This mode describes the size of the storage area
5980      to fetch the overall value from.  Within that, we
5981      ignore the POS lowest bits, etc.  */
5982   enum machine_mode is_mode = GET_MODE (inner);
5983   enum machine_mode inner_mode;
5984   enum machine_mode wanted_inner_mode = byte_mode;
5985   enum machine_mode wanted_inner_reg_mode = word_mode;
5986   enum machine_mode pos_mode = word_mode;
5987   enum machine_mode extraction_mode = word_mode;
5988   enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5989   int spans_byte = 0;
5990   rtx new = 0;
5991   rtx orig_pos_rtx = pos_rtx;
5992   HOST_WIDE_INT orig_pos;
5993
5994   /* Get some information about INNER and get the innermost object.  */
5995   if (GET_CODE (inner) == USE)
5996     /* (use:SI (mem:QI foo)) stands for (mem:SI foo).  */
5997     /* We don't need to adjust the position because we set up the USE
5998        to pretend that it was a full-word object.  */
5999     spans_byte = 1, inner = XEXP (inner, 0);
6000   else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6001     {
6002       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6003          consider just the QI as the memory to extract from.
6004          The subreg adds or removes high bits; its mode is
6005          irrelevant to the meaning of this extraction,
6006          since POS and LEN count from the lsb.  */
6007       if (GET_CODE (SUBREG_REG (inner)) == MEM)
6008         is_mode = GET_MODE (SUBREG_REG (inner));
6009       inner = SUBREG_REG (inner);
6010     }
6011   else if (GET_CODE (inner) == ASHIFT
6012            && GET_CODE (XEXP (inner, 1)) == CONST_INT
6013            && pos_rtx == 0 && pos == 0
6014            && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6015     {
6016       /* We're extracting the least significant bits of an rtx
6017          (ashift X (const_int C)), where LEN > C.  Extract the
6018          least significant (LEN - C) bits of X, giving an rtx
6019          whose mode is MODE, then shift it left C times.  */
6020       new = make_extraction (mode, XEXP (inner, 0),
6021                              0, 0, len - INTVAL (XEXP (inner, 1)),
6022                              unsignedp, in_dest, in_compare);
6023       if (new != 0)
6024         return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6025     }
6026
6027   inner_mode = GET_MODE (inner);
6028
6029   if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6030     pos = INTVAL (pos_rtx), pos_rtx = 0;
6031
6032   /* See if this can be done without an extraction.  We never can if the
6033      width of the field is not the same as that of some integer mode. For
6034      registers, we can only avoid the extraction if the position is at the
6035      low-order bit and this is either not in the destination or we have the
6036      appropriate STRICT_LOW_PART operation available.
6037
6038      For MEM, we can avoid an extract if the field starts on an appropriate
6039      boundary and we can change the mode of the memory reference.  However,
6040      we cannot directly access the MEM if we have a USE and the underlying
6041      MEM is not TMODE.  This combination means that MEM was being used in a
6042      context where bits outside its mode were being referenced; that is only
6043      valid in bit-field insns.  */
6044
6045   if (tmode != BLKmode
6046       && ! (spans_byte && inner_mode != tmode)
6047       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6048            && GET_CODE (inner) != MEM
6049            && (! in_dest
6050                || (GET_CODE (inner) == REG
6051                    && have_insn_for (STRICT_LOW_PART, tmode))))
6052           || (GET_CODE (inner) == MEM && pos_rtx == 0
6053               && (pos
6054                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6055                      : BITS_PER_UNIT)) == 0
6056               /* We can't do this if we are widening INNER_MODE (it
6057                  may not be aligned, for one thing).  */
6058               && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6059               && (inner_mode == tmode
6060                   || (! mode_dependent_address_p (XEXP (inner, 0))
6061                       && ! MEM_VOLATILE_P (inner))))))
6062     {
6063       /* If INNER is a MEM, make a new MEM that encompasses just the desired
6064          field.  If the original and current mode are the same, we need not
6065          adjust the offset.  Otherwise, we do if bytes big endian.
6066
6067          If INNER is not a MEM, get a piece consisting of just the field
6068          of interest (in this case POS % BITS_PER_WORD must be 0).  */
6069
6070       if (GET_CODE (inner) == MEM)
6071         {
6072           HOST_WIDE_INT offset;
6073
6074           /* POS counts from lsb, but make OFFSET count in memory order.  */
6075           if (BYTES_BIG_ENDIAN)
6076             offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6077           else
6078             offset = pos / BITS_PER_UNIT;
6079
6080           new = adjust_address_nv (inner, tmode, offset);
6081         }
6082       else if (GET_CODE (inner) == REG)
6083         {
6084           if (tmode != inner_mode)
6085             {
6086               if (in_dest)
6087                 {
6088                   /* We can't call gen_lowpart_for_combine here since we always want
6089                      a SUBREG and it would sometimes return a new hard register.  */
6090                   HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6091
6092                   if (WORDS_BIG_ENDIAN
6093                       && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6094                     final_word = ((GET_MODE_SIZE (inner_mode)
6095                                    - GET_MODE_SIZE (tmode))
6096                                   / UNITS_PER_WORD) - final_word;
6097
6098                   final_word *= UNITS_PER_WORD;
6099                   if (BYTES_BIG_ENDIAN &&
6100                       GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6101                     final_word += (GET_MODE_SIZE (inner_mode)
6102                                    - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6103
6104                   /* Avoid creating invalid subregs, for example when
6105                      simplifying (x>>32)&255.  */
6106                   if (final_word >= GET_MODE_SIZE (inner_mode))
6107                     return NULL_RTX;
6108
6109                   new = gen_rtx_SUBREG (tmode, inner, final_word);
6110                 }
6111               else
6112                 new = gen_lowpart_for_combine (tmode, inner);
6113             }
6114           else
6115             new = inner;
6116         }
6117       else
6118         new = force_to_mode (inner, tmode,
6119                              len >= HOST_BITS_PER_WIDE_INT
6120                              ? ~(unsigned HOST_WIDE_INT) 0
6121                              : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6122                              NULL_RTX, 0);
6123
6124       /* If this extraction is going into the destination of a SET,
6125          make a STRICT_LOW_PART unless we made a MEM.  */
6126
6127       if (in_dest)
6128         return (GET_CODE (new) == MEM ? new
6129                 : (GET_CODE (new) != SUBREG
6130                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
6131                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6132
6133       if (mode == tmode)
6134         return new;
6135
6136       if (GET_CODE (new) == CONST_INT)
6137         return gen_int_mode (INTVAL (new), mode);
6138
6139       /* If we know that no extraneous bits are set, and that the high
6140          bit is not set, convert the extraction to the cheaper of
6141          sign and zero extension, that are equivalent in these cases.  */
6142       if (flag_expensive_optimizations
6143           && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6144               && ((nonzero_bits (new, tmode)
6145                    & ~(((unsigned HOST_WIDE_INT)
6146                         GET_MODE_MASK (tmode))
6147                        >> 1))
6148                   == 0)))
6149         {
6150           rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6151           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6152
6153           /* Prefer ZERO_EXTENSION, since it gives more information to
6154              backends.  */
6155           if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6156             return temp;
6157           return temp1;
6158         }
6159
6160       /* Otherwise, sign- or zero-extend unless we already are in the
6161          proper mode.  */
6162
6163       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6164                              mode, new));
6165     }
6166
6167   /* Unless this is a COMPARE or we have a funny memory reference,
6168      don't do anything with zero-extending field extracts starting at
6169      the low-order bit since they are simple AND operations.  */
6170   if (pos_rtx == 0 && pos == 0 && ! in_dest
6171       && ! in_compare && ! spans_byte && unsignedp)
6172     return 0;
6173
6174   /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6175      we would be spanning bytes or if the position is not a constant and the
6176      length is not 1.  In all other cases, we would only be going outside
6177      our object in cases when an original shift would have been
6178      undefined.  */
6179   if (! spans_byte && GET_CODE (inner) == MEM
6180       && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6181           || (pos_rtx != 0 && len != 1)))
6182     return 0;
6183
6184   /* Get the mode to use should INNER not be a MEM, the mode for the position,
6185      and the mode for the result.  */
6186   if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6187     {
6188       wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6189       pos_mode = mode_for_extraction (EP_insv, 2);
6190       extraction_mode = mode_for_extraction (EP_insv, 3);
6191     }
6192
6193   if (! in_dest && unsignedp
6194       && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6195     {
6196       wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6197       pos_mode = mode_for_extraction (EP_extzv, 3);
6198       extraction_mode = mode_for_extraction (EP_extzv, 0);
6199     }
6200
6201   if (! in_dest && ! unsignedp
6202       && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6203     {
6204       wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6205       pos_mode = mode_for_extraction (EP_extv, 3);
6206       extraction_mode = mode_for_extraction (EP_extv, 0);
6207     }
6208
6209   /* Never narrow an object, since that might not be safe.  */
6210
6211   if (mode != VOIDmode
6212       && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6213     extraction_mode = mode;
6214
6215   if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6216       && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6217     pos_mode = GET_MODE (pos_rtx);
6218
6219   /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6220      if we have to change the mode of memory and cannot, the desired mode is
6221      EXTRACTION_MODE.  */
6222   if (GET_CODE (inner) != MEM)
6223     wanted_inner_mode = wanted_inner_reg_mode;
6224   else if (inner_mode != wanted_inner_mode
6225            && (mode_dependent_address_p (XEXP (inner, 0))
6226                || MEM_VOLATILE_P (inner)))
6227     wanted_inner_mode = extraction_mode;
6228
6229   orig_pos = pos;
6230
6231   if (BITS_BIG_ENDIAN)
6232     {
6233       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6234          BITS_BIG_ENDIAN style.  If position is constant, compute new
6235          position.  Otherwise, build subtraction.
6236          Note that POS is relative to the mode of the original argument.
6237          If it's a MEM we need to recompute POS relative to that.
6238          However, if we're extracting from (or inserting into) a register,
6239          we want to recompute POS relative to wanted_inner_mode.  */
6240       int width = (GET_CODE (inner) == MEM
6241                    ? GET_MODE_BITSIZE (is_mode)
6242                    : GET_MODE_BITSIZE (wanted_inner_mode));
6243
6244       if (pos_rtx == 0)
6245         pos = width - len - pos;
6246       else
6247         pos_rtx
6248           = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6249       /* POS may be less than 0 now, but we check for that below.
6250          Note that it can only be less than 0 if GET_CODE (inner) != MEM.  */
6251     }
6252
6253   /* If INNER has a wider mode, make it smaller.  If this is a constant
6254      extract, try to adjust the byte to point to the byte containing
6255      the value.  */
6256   if (wanted_inner_mode != VOIDmode
6257       && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6258       && ((GET_CODE (inner) == MEM
6259            && (inner_mode == wanted_inner_mode
6260                || (! mode_dependent_address_p (XEXP (inner, 0))
6261                    && ! MEM_VOLATILE_P (inner))))))
6262     {
6263       int offset = 0;
6264
6265       /* The computations below will be correct if the machine is big
6266          endian in both bits and bytes or little endian in bits and bytes.
6267          If it is mixed, we must adjust.  */
6268
6269       /* If bytes are big endian and we had a paradoxical SUBREG, we must
6270          adjust OFFSET to compensate.  */
6271       if (BYTES_BIG_ENDIAN
6272           && ! spans_byte
6273           && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6274         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6275
6276       /* If this is a constant position, we can move to the desired byte.  */
6277       if (pos_rtx == 0)
6278         {
6279           offset += pos / BITS_PER_UNIT;
6280           pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6281         }
6282
6283       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6284           && ! spans_byte
6285           && is_mode != wanted_inner_mode)
6286         offset = (GET_MODE_SIZE (is_mode)
6287                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
6288
6289       if (offset != 0 || inner_mode != wanted_inner_mode)
6290         inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6291     }
6292
6293   /* If INNER is not memory, we can always get it into the proper mode.  If we
6294      are changing its mode, POS must be a constant and smaller than the size
6295      of the new mode.  */
6296   else if (GET_CODE (inner) != MEM)
6297     {
6298       if (GET_MODE (inner) != wanted_inner_mode
6299           && (pos_rtx != 0
6300               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6301         return 0;
6302
6303       inner = force_to_mode (inner, wanted_inner_mode,
6304                              pos_rtx
6305                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6306                              ? ~(unsigned HOST_WIDE_INT) 0
6307                              : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6308                                 << orig_pos),
6309                              NULL_RTX, 0);
6310     }
6311
6312   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
6313      have to zero extend.  Otherwise, we can just use a SUBREG.  */
6314   if (pos_rtx != 0
6315       && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6316     {
6317       rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6318
6319       /* If we know that no extraneous bits are set, and that the high
6320          bit is not set, convert extraction to cheaper one - either
6321          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6322          cases.  */
6323       if (flag_expensive_optimizations
6324           && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6325               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6326                    & ~(((unsigned HOST_WIDE_INT)
6327                         GET_MODE_MASK (GET_MODE (pos_rtx)))
6328                        >> 1))
6329                   == 0)))
6330         {
6331           rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6332
6333           /* Prefer ZERO_EXTENSION, since it gives more information to
6334              backends.  */
6335           if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6336             temp = temp1;
6337         }
6338       pos_rtx = temp;
6339     }
6340   else if (pos_rtx != 0
6341            && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6342     pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
6343
6344   /* Make POS_RTX unless we already have it and it is correct.  If we don't
6345      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6346      be a CONST_INT.  */
6347   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6348     pos_rtx = orig_pos_rtx;
6349
6350   else if (pos_rtx == 0)
6351     pos_rtx = GEN_INT (pos);
6352
6353   /* Make the required operation.  See if we can use existing rtx.  */
6354   new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6355                          extraction_mode, inner, GEN_INT (len), pos_rtx);
6356   if (! in_dest)
6357     new = gen_lowpart_for_combine (mode, new);
6358
6359   return new;
6360 }
6361 \f
6362 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6363    with any other operations in X.  Return X without that shift if so.  */
6364
6365 static rtx
6366 extract_left_shift (rtx x, int count)
6367 {
6368   enum rtx_code code = GET_CODE (x);
6369   enum machine_mode mode = GET_MODE (x);
6370   rtx tem;
6371
6372   switch (code)
6373     {
6374     case ASHIFT:
6375       /* This is the shift itself.  If it is wide enough, we will return
6376          either the value being shifted if the shift count is equal to
6377          COUNT or a shift for the difference.  */
6378       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6379           && INTVAL (XEXP (x, 1)) >= count)
6380         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6381                                      INTVAL (XEXP (x, 1)) - count);
6382       break;
6383
6384     case NEG:  case NOT:
6385       if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6386         return simplify_gen_unary (code, mode, tem, mode);
6387
6388       break;
6389
6390     case PLUS:  case IOR:  case XOR:  case AND:
6391       /* If we can safely shift this constant and we find the inner shift,
6392          make a new operation.  */
6393       if (GET_CODE (XEXP (x, 1)) == CONST_INT
6394           && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6395           && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6396         return gen_binary (code, mode, tem,
6397                            GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6398
6399       break;
6400
6401     default:
6402       break;
6403     }
6404
6405   return 0;
6406 }
6407 \f
6408 /* Look at the expression rooted at X.  Look for expressions
6409    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6410    Form these expressions.
6411
6412    Return the new rtx, usually just X.
6413
6414    Also, for machines like the VAX that don't have logical shift insns,
6415    try to convert logical to arithmetic shift operations in cases where
6416    they are equivalent.  This undoes the canonicalizations to logical
6417    shifts done elsewhere.
6418
6419    We try, as much as possible, to re-use rtl expressions to save memory.
6420
6421    IN_CODE says what kind of expression we are processing.  Normally, it is
6422    SET.  In a memory address (inside a MEM, PLUS or minus, the latter two
6423    being kludges), it is MEM.  When processing the arguments of a comparison
6424    or a COMPARE against zero, it is COMPARE.  */
6425
6426 static rtx
6427 make_compound_operation (rtx x, enum rtx_code in_code)
6428 {
6429   enum rtx_code code = GET_CODE (x);
6430   enum machine_mode mode = GET_MODE (x);
6431   int mode_width = GET_MODE_BITSIZE (mode);
6432   rtx rhs, lhs;
6433   enum rtx_code next_code;
6434   int i;
6435   rtx new = 0;
6436   rtx tem;
6437   const char *fmt;
6438
6439   /* Select the code to be used in recursive calls.  Once we are inside an
6440      address, we stay there.  If we have a comparison, set to COMPARE,
6441      but once inside, go back to our default of SET.  */
6442
6443   next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6444                : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6445                   && XEXP (x, 1) == const0_rtx) ? COMPARE
6446                : in_code == COMPARE ? SET : in_code);
6447
6448   /* Process depending on the code of this operation.  If NEW is set
6449      nonzero, it will be returned.  */
6450
6451   switch (code)
6452     {
6453     case ASHIFT:
6454       /* Convert shifts by constants into multiplications if inside
6455          an address.  */
6456       if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6457           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6458           && INTVAL (XEXP (x, 1)) >= 0)
6459         {
6460           new = make_compound_operation (XEXP (x, 0), next_code);
6461           new = gen_rtx_MULT (mode, new,
6462                               GEN_INT ((HOST_WIDE_INT) 1
6463                                        << INTVAL (XEXP (x, 1))));
6464         }
6465       break;
6466
6467     case AND:
6468       /* If the second operand is not a constant, we can't do anything
6469          with it.  */
6470       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6471         break;
6472
6473       /* If the constant is a power of two minus one and the first operand
6474          is a logical right shift, make an extraction.  */
6475       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6476           && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6477         {
6478           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6479           new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6480                                  0, in_code == COMPARE);
6481         }
6482
6483       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
6484       else if (GET_CODE (XEXP (x, 0)) == SUBREG
6485                && subreg_lowpart_p (XEXP (x, 0))
6486                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6487                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6488         {
6489           new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6490                                          next_code);
6491           new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6492                                  XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6493                                  0, in_code == COMPARE);
6494         }
6495       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
6496       else if ((GET_CODE (XEXP (x, 0)) == XOR
6497                 || GET_CODE (XEXP (x, 0)) == IOR)
6498                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6499                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6500                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6501         {
6502           /* Apply the distributive law, and then try to make extractions.  */
6503           new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6504                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6505                                              XEXP (x, 1)),
6506                                 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6507                                              XEXP (x, 1)));
6508           new = make_compound_operation (new, in_code);
6509         }
6510
6511       /* If we are have (and (rotate X C) M) and C is larger than the number
6512          of bits in M, this is an extraction.  */
6513
6514       else if (GET_CODE (XEXP (x, 0)) == ROTATE
6515                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6516                && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6517                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6518         {
6519           new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6520           new = make_extraction (mode, new,
6521                                  (GET_MODE_BITSIZE (mode)
6522                                   - INTVAL (XEXP (XEXP (x, 0), 1))),
6523                                  NULL_RTX, i, 1, 0, in_code == COMPARE);
6524         }
6525
6526       /* On machines without logical shifts, if the operand of the AND is
6527          a logical shift and our mask turns off all the propagated sign
6528          bits, we can replace the logical shift with an arithmetic shift.  */
6529       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6530                && !have_insn_for (LSHIFTRT, mode)
6531                && have_insn_for (ASHIFTRT, mode)
6532                && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6533                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6534                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6535                && mode_width <= HOST_BITS_PER_WIDE_INT)
6536         {
6537           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6538
6539           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6540           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6541             SUBST (XEXP (x, 0),
6542                    gen_rtx_ASHIFTRT (mode,
6543                                      make_compound_operation
6544                                      (XEXP (XEXP (x, 0), 0), next_code),
6545                                      XEXP (XEXP (x, 0), 1)));
6546         }
6547
6548       /* If the constant is one less than a power of two, this might be
6549          representable by an extraction even if no shift is present.
6550          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6551          we are in a COMPARE.  */
6552       else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6553         new = make_extraction (mode,
6554                                make_compound_operation (XEXP (x, 0),
6555                                                         next_code),
6556                                0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6557
6558       /* If we are in a comparison and this is an AND with a power of two,
6559          convert this into the appropriate bit extract.  */
6560       else if (in_code == COMPARE
6561                && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6562         new = make_extraction (mode,
6563                                make_compound_operation (XEXP (x, 0),
6564                                                         next_code),
6565                                i, NULL_RTX, 1, 1, 0, 1);
6566
6567       break;
6568
6569     case LSHIFTRT:
6570       /* If the sign bit is known to be zero, replace this with an
6571          arithmetic shift.  */
6572       if (have_insn_for (ASHIFTRT, mode)
6573           && ! have_insn_for (LSHIFTRT, mode)
6574           && mode_width <= HOST_BITS_PER_WIDE_INT
6575           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6576         {
6577           new = gen_rtx_ASHIFTRT (mode,
6578                                   make_compound_operation (XEXP (x, 0),
6579                                                            next_code),
6580                                   XEXP (x, 1));
6581           break;
6582         }
6583
6584       /* ... fall through ...  */
6585
6586     case ASHIFTRT:
6587       lhs = XEXP (x, 0);
6588       rhs = XEXP (x, 1);
6589
6590       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6591          this is a SIGN_EXTRACT.  */
6592       if (GET_CODE (rhs) == CONST_INT
6593           && GET_CODE (lhs) == ASHIFT
6594           && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6595           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6596         {
6597           new = make_compound_operation (XEXP (lhs, 0), next_code);
6598           new = make_extraction (mode, new,
6599                                  INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6600                                  NULL_RTX, mode_width - INTVAL (rhs),
6601                                  code == LSHIFTRT, 0, in_code == COMPARE);
6602           break;
6603         }
6604
6605       /* See if we have operations between an ASHIFTRT and an ASHIFT.
6606          If so, try to merge the shifts into a SIGN_EXTEND.  We could
6607          also do this for some cases of SIGN_EXTRACT, but it doesn't
6608          seem worth the effort; the case checked for occurs on Alpha.  */
6609
6610       if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6611           && ! (GET_CODE (lhs) == SUBREG
6612                 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6613           && GET_CODE (rhs) == CONST_INT
6614           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6615           && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6616         new = make_extraction (mode, make_compound_operation (new, next_code),
6617                                0, NULL_RTX, mode_width - INTVAL (rhs),
6618                                code == LSHIFTRT, 0, in_code == COMPARE);
6619
6620       break;
6621
6622     case SUBREG:
6623       /* Call ourselves recursively on the inner expression.  If we are
6624          narrowing the object and it has a different RTL code from
6625          what it originally did, do this SUBREG as a force_to_mode.  */
6626
6627       tem = make_compound_operation (SUBREG_REG (x), in_code);
6628       if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6629           && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6630           && subreg_lowpart_p (x))
6631         {
6632           rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6633                                      NULL_RTX, 0);
6634
6635           /* If we have something other than a SUBREG, we might have
6636              done an expansion, so rerun ourselves.  */
6637           if (GET_CODE (newer) != SUBREG)
6638             newer = make_compound_operation (newer, in_code);
6639
6640           return newer;
6641         }
6642
6643       /* If this is a paradoxical subreg, and the new code is a sign or
6644          zero extension, omit the subreg and widen the extension.  If it
6645          is a regular subreg, we can still get rid of the subreg by not
6646          widening so much, or in fact removing the extension entirely.  */
6647       if ((GET_CODE (tem) == SIGN_EXTEND
6648            || GET_CODE (tem) == ZERO_EXTEND)
6649           && subreg_lowpart_p (x))
6650         {
6651           if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6652               || (GET_MODE_SIZE (mode) >
6653                   GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6654             {
6655               if (! SCALAR_INT_MODE_P (mode))
6656                 break;
6657               tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6658             }
6659           else
6660             tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6661           return tem;
6662         }
6663       break;
6664
6665     default:
6666       break;
6667     }
6668
6669   if (new)
6670     {
6671       x = gen_lowpart_for_combine (mode, new);
6672       code = GET_CODE (x);
6673     }
6674
6675   /* Now recursively process each operand of this operation.  */
6676   fmt = GET_RTX_FORMAT (code);
6677   for (i = 0; i < GET_RTX_LENGTH (code); i++)
6678     if (fmt[i] == 'e')
6679       {
6680         new = make_compound_operation (XEXP (x, i), next_code);
6681         SUBST (XEXP (x, i), new);
6682       }
6683
6684   return x;
6685 }
6686 \f
6687 /* Given M see if it is a value that would select a field of bits
6688    within an item, but not the entire word.  Return -1 if not.
6689    Otherwise, return the starting position of the field, where 0 is the
6690    low-order bit.
6691
6692    *PLEN is set to the length of the field.  */
6693
6694 static int
6695 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6696 {
6697   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
6698   int pos = exact_log2 (m & -m);
6699   int len;
6700
6701   if (pos < 0)
6702     return -1;
6703
6704   /* Now shift off the low-order zero bits and see if we have a power of
6705      two minus 1.  */
6706   len = exact_log2 ((m >> pos) + 1);
6707
6708   if (len <= 0)
6709     return -1;
6710
6711   *plen = len;
6712   return pos;
6713 }
6714 \f
6715 /* See if X can be simplified knowing that we will only refer to it in
6716    MODE and will only refer to those bits that are nonzero in MASK.
6717    If other bits are being computed or if masking operations are done
6718    that select a superset of the bits in MASK, they can sometimes be
6719    ignored.
6720
6721    Return a possibly simplified expression, but always convert X to
6722    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
6723
6724    Also, if REG is nonzero and X is a register equal in value to REG,
6725    replace X with REG.
6726
6727    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6728    are all off in X.  This is used when X will be complemented, by either
6729    NOT, NEG, or XOR.  */
6730
6731 static rtx
6732 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6733                rtx reg, int just_select)
6734 {
6735   enum rtx_code code = GET_CODE (x);
6736   int next_select = just_select || code == XOR || code == NOT || code == NEG;
6737   enum machine_mode op_mode;
6738   unsigned HOST_WIDE_INT fuller_mask, nonzero;
6739   rtx op0, op1, temp;
6740
6741   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
6742      code below will do the wrong thing since the mode of such an
6743      expression is VOIDmode.
6744
6745      Also do nothing if X is a CLOBBER; this can happen if X was
6746      the return value from a call to gen_lowpart_for_combine.  */
6747   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6748     return x;
6749
6750   /* We want to perform the operation is its present mode unless we know
6751      that the operation is valid in MODE, in which case we do the operation
6752      in MODE.  */
6753   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6754               && have_insn_for (code, mode))
6755              ? mode : GET_MODE (x));
6756
6757   /* It is not valid to do a right-shift in a narrower mode
6758      than the one it came in with.  */
6759   if ((code == LSHIFTRT || code == ASHIFTRT)
6760       && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6761     op_mode = GET_MODE (x);
6762
6763   /* Truncate MASK to fit OP_MODE.  */
6764   if (op_mode)
6765     mask &= GET_MODE_MASK (op_mode);
6766
6767   /* When we have an arithmetic operation, or a shift whose count we
6768      do not know, we need to assume that all bits up to the highest-order
6769      bit in MASK will be needed.  This is how we form such a mask.  */
6770   if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6771     fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6772   else
6773     fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6774                    - 1);
6775
6776   /* Determine what bits of X are guaranteed to be (non)zero.  */
6777   nonzero = nonzero_bits (x, mode);
6778
6779   /* If none of the bits in X are needed, return a zero.  */
6780   if (! just_select && (nonzero & mask) == 0)
6781     x = const0_rtx;
6782
6783   /* If X is a CONST_INT, return a new one.  Do this here since the
6784      test below will fail.  */
6785   if (GET_CODE (x) == CONST_INT)
6786     {
6787       if (SCALAR_INT_MODE_P (mode))
6788         return gen_int_mode (INTVAL (x) & mask, mode);
6789       else
6790         {
6791           x = GEN_INT (INTVAL (x) & mask);
6792           return gen_lowpart_common (mode, x);
6793         }
6794     }
6795
6796   /* If X is narrower than MODE and we want all the bits in X's mode, just
6797      get X in the proper mode.  */
6798   if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6799       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6800     return gen_lowpart_for_combine (mode, x);
6801
6802   /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6803      MASK are already known to be zero in X, we need not do anything.  */
6804   if (GET_MODE (x) == mode && code != SUBREG && (~mask & nonzero) == 0)
6805     return x;
6806
6807   switch (code)
6808     {
6809     case CLOBBER:
6810       /* If X is a (clobber (const_int)), return it since we know we are
6811          generating something that won't match.  */
6812       return x;
6813
6814     case USE:
6815       /* X is a (use (mem ..)) that was made from a bit-field extraction that
6816          spanned the boundary of the MEM.  If we are now masking so it is
6817          within that boundary, we don't need the USE any more.  */
6818       if (! BITS_BIG_ENDIAN
6819           && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6820         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6821       break;
6822
6823     case SIGN_EXTEND:
6824     case ZERO_EXTEND:
6825     case ZERO_EXTRACT:
6826     case SIGN_EXTRACT:
6827       x = expand_compound_operation (x);
6828       if (GET_CODE (x) != code)
6829         return force_to_mode (x, mode, mask, reg, next_select);
6830       break;
6831
6832     case REG:
6833       if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6834                        || rtx_equal_p (reg, get_last_value (x))))
6835         x = reg;
6836       break;
6837
6838     case SUBREG:
6839       if (subreg_lowpart_p (x)
6840           /* We can ignore the effect of this SUBREG if it narrows the mode or
6841              if the constant masks to zero all the bits the mode doesn't
6842              have.  */
6843           && ((GET_MODE_SIZE (GET_MODE (x))
6844                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6845               || (0 == (mask
6846                         & GET_MODE_MASK (GET_MODE (x))
6847                         & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6848         return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6849       break;
6850
6851     case AND:
6852       /* If this is an AND with a constant, convert it into an AND
6853          whose constant is the AND of that constant with MASK.  If it
6854          remains an AND of MASK, delete it since it is redundant.  */
6855
6856       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6857         {
6858           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6859                                       mask & INTVAL (XEXP (x, 1)));
6860
6861           /* If X is still an AND, see if it is an AND with a mask that
6862              is just some low-order bits.  If so, and it is MASK, we don't
6863              need it.  */
6864
6865           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6866               && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6867                   == mask))
6868             x = XEXP (x, 0);
6869
6870           /* If it remains an AND, try making another AND with the bits
6871              in the mode mask that aren't in MASK turned on.  If the
6872              constant in the AND is wide enough, this might make a
6873              cheaper constant.  */
6874
6875           if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6876               && GET_MODE_MASK (GET_MODE (x)) != mask
6877               && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6878             {
6879               HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6880                                     | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6881               int width = GET_MODE_BITSIZE (GET_MODE (x));
6882               rtx y;
6883
6884               /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6885                  number, sign extend it.  */
6886               if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6887                   && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6888                 cval |= (HOST_WIDE_INT) -1 << width;
6889
6890               y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6891               if (rtx_cost (y, SET) < rtx_cost (x, SET))
6892                 x = y;
6893             }
6894
6895           break;
6896         }
6897
6898       goto binop;
6899
6900     case PLUS:
6901       /* In (and (plus FOO C1) M), if M is a mask that just turns off
6902          low-order bits (as in an alignment operation) and FOO is already
6903          aligned to that boundary, mask C1 to that boundary as well.
6904          This may eliminate that PLUS and, later, the AND.  */
6905
6906       {
6907         unsigned int width = GET_MODE_BITSIZE (mode);
6908         unsigned HOST_WIDE_INT smask = mask;
6909
6910         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6911            number, sign extend it.  */
6912
6913         if (width < HOST_BITS_PER_WIDE_INT
6914             && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6915           smask |= (HOST_WIDE_INT) -1 << width;
6916
6917         if (GET_CODE (XEXP (x, 1)) == CONST_INT
6918             && exact_log2 (- smask) >= 0
6919             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
6920             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
6921           return force_to_mode (plus_constant (XEXP (x, 0),
6922                                                (INTVAL (XEXP (x, 1)) & smask)),
6923                                 mode, smask, reg, next_select);
6924       }
6925
6926       /* ... fall through ...  */
6927
6928     case MULT:
6929       /* For PLUS, MINUS and MULT, we need any bits less significant than the
6930          most significant bit in MASK since carries from those bits will
6931          affect the bits we are interested in.  */
6932       mask = fuller_mask;
6933       goto binop;
6934
6935     case MINUS:
6936       /* If X is (minus C Y) where C's least set bit is larger than any bit
6937          in the mask, then we may replace with (neg Y).  */
6938       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6939           && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
6940                                         & -INTVAL (XEXP (x, 0))))
6941               > mask))
6942         {
6943           x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
6944                                   GET_MODE (x));
6945           return force_to_mode (x, mode, mask, reg, next_select);
6946         }
6947
6948       /* Similarly, if C contains every bit in the fuller_mask, then we may
6949          replace with (not Y).  */
6950       if (GET_CODE (XEXP (x, 0)) == CONST_INT
6951           && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
6952               == INTVAL (XEXP (x, 0))))
6953         {
6954           x = simplify_gen_unary (NOT, GET_MODE (x),
6955                                   XEXP (x, 1), GET_MODE (x));
6956           return force_to_mode (x, mode, mask, reg, next_select);
6957         }
6958
6959       mask = fuller_mask;
6960       goto binop;
6961
6962     case IOR:
6963     case XOR:
6964       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6965          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6966          operation which may be a bitfield extraction.  Ensure that the
6967          constant we form is not wider than the mode of X.  */
6968
6969       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6970           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6971           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6972           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6973           && GET_CODE (XEXP (x, 1)) == CONST_INT
6974           && ((INTVAL (XEXP (XEXP (x, 0), 1))
6975                + floor_log2 (INTVAL (XEXP (x, 1))))
6976               < GET_MODE_BITSIZE (GET_MODE (x)))
6977           && (INTVAL (XEXP (x, 1))
6978               & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6979         {
6980           temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6981                           << INTVAL (XEXP (XEXP (x, 0), 1)));
6982           temp = gen_binary (GET_CODE (x), GET_MODE (x),
6983                              XEXP (XEXP (x, 0), 0), temp);
6984           x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6985                           XEXP (XEXP (x, 0), 1));
6986           return force_to_mode (x, mode, mask, reg, next_select);
6987         }
6988
6989     binop:
6990       /* For most binary operations, just propagate into the operation and
6991          change the mode if we have an operation of that mode.  */
6992
6993       op0 = gen_lowpart_for_combine (op_mode,
6994                                      force_to_mode (XEXP (x, 0), mode, mask,
6995                                                     reg, next_select));
6996       op1 = gen_lowpart_for_combine (op_mode,
6997                                      force_to_mode (XEXP (x, 1), mode, mask,
6998                                                     reg, next_select));
6999
7000       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7001         x = gen_binary (code, op_mode, op0, op1);
7002       break;
7003
7004     case ASHIFT:
7005       /* For left shifts, do the same, but just for the first operand.
7006          However, we cannot do anything with shifts where we cannot
7007          guarantee that the counts are smaller than the size of the mode
7008          because such a count will have a different meaning in a
7009          wider mode.  */
7010
7011       if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7012              && INTVAL (XEXP (x, 1)) >= 0
7013              && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7014           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7015                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7016                     < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7017         break;
7018
7019       /* If the shift count is a constant and we can do arithmetic in
7020          the mode of the shift, refine which bits we need.  Otherwise, use the
7021          conservative form of the mask.  */
7022       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7023           && INTVAL (XEXP (x, 1)) >= 0
7024           && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7025           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7026         mask >>= INTVAL (XEXP (x, 1));
7027       else
7028         mask = fuller_mask;
7029
7030       op0 = gen_lowpart_for_combine (op_mode,
7031                                      force_to_mode (XEXP (x, 0), op_mode,
7032                                                     mask, reg, next_select));
7033
7034       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7035         x = gen_binary (code, op_mode, op0, XEXP (x, 1));
7036       break;
7037
7038     case LSHIFTRT:
7039       /* Here we can only do something if the shift count is a constant,
7040          this shift constant is valid for the host, and we can do arithmetic
7041          in OP_MODE.  */
7042
7043       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7044           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7045           && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7046         {
7047           rtx inner = XEXP (x, 0);
7048           unsigned HOST_WIDE_INT inner_mask;
7049
7050           /* Select the mask of the bits we need for the shift operand.  */
7051           inner_mask = mask << INTVAL (XEXP (x, 1));
7052
7053           /* We can only change the mode of the shift if we can do arithmetic
7054              in the mode of the shift and INNER_MASK is no wider than the
7055              width of OP_MODE.  */
7056           if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
7057               || (inner_mask & ~GET_MODE_MASK (op_mode)) != 0)
7058             op_mode = GET_MODE (x);
7059
7060           inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7061
7062           if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7063             x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7064         }
7065
7066       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7067          shift and AND produces only copies of the sign bit (C2 is one less
7068          than a power of two), we can do this with just a shift.  */
7069
7070       if (GET_CODE (x) == LSHIFTRT
7071           && GET_CODE (XEXP (x, 1)) == CONST_INT
7072           /* The shift puts one of the sign bit copies in the least significant
7073              bit.  */
7074           && ((INTVAL (XEXP (x, 1))
7075                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7076               >= GET_MODE_BITSIZE (GET_MODE (x)))
7077           && exact_log2 (mask + 1) >= 0
7078           /* Number of bits left after the shift must be more than the mask
7079              needs.  */
7080           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7081               <= GET_MODE_BITSIZE (GET_MODE (x)))
7082           /* Must be more sign bit copies than the mask needs.  */
7083           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7084               >= exact_log2 (mask + 1)))
7085         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7086                         GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7087                                  - exact_log2 (mask + 1)));
7088
7089       goto shiftrt;
7090
7091     case ASHIFTRT:
7092       /* If we are just looking for the sign bit, we don't need this shift at
7093          all, even if it has a variable count.  */
7094       if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7095           && (mask == ((unsigned HOST_WIDE_INT) 1
7096                        << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7097         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7098
7099       /* If this is a shift by a constant, get a mask that contains those bits
7100          that are not copies of the sign bit.  We then have two cases:  If
7101          MASK only includes those bits, this can be a logical shift, which may
7102          allow simplifications.  If MASK is a single-bit field not within
7103          those bits, we are requesting a copy of the sign bit and hence can
7104          shift the sign bit to the appropriate location.  */
7105
7106       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7107           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7108         {
7109           int i = -1;
7110
7111           /* If the considered data is wider than HOST_WIDE_INT, we can't
7112              represent a mask for all its bits in a single scalar.
7113              But we only care about the lower bits, so calculate these.  */
7114
7115           if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7116             {
7117               nonzero = ~(HOST_WIDE_INT) 0;
7118
7119               /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7120                  is the number of bits a full-width mask would have set.
7121                  We need only shift if these are fewer than nonzero can
7122                  hold.  If not, we must keep all bits set in nonzero.  */
7123
7124               if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7125                   < HOST_BITS_PER_WIDE_INT)
7126                 nonzero >>= INTVAL (XEXP (x, 1))
7127                             + HOST_BITS_PER_WIDE_INT
7128                             - GET_MODE_BITSIZE (GET_MODE (x)) ;
7129             }
7130           else
7131             {
7132               nonzero = GET_MODE_MASK (GET_MODE (x));
7133               nonzero >>= INTVAL (XEXP (x, 1));
7134             }
7135
7136           if ((mask & ~nonzero) == 0
7137               || (i = exact_log2 (mask)) >= 0)
7138             {
7139               x = simplify_shift_const
7140                 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7141                  i < 0 ? INTVAL (XEXP (x, 1))
7142                  : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7143
7144               if (GET_CODE (x) != ASHIFTRT)
7145                 return force_to_mode (x, mode, mask, reg, next_select);
7146             }
7147         }
7148
7149       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
7150          even if the shift count isn't a constant.  */
7151       if (mask == 1)
7152         x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
7153
7154     shiftrt:
7155
7156       /* If this is a zero- or sign-extension operation that just affects bits
7157          we don't care about, remove it.  Be sure the call above returned
7158          something that is still a shift.  */
7159
7160       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7161           && GET_CODE (XEXP (x, 1)) == CONST_INT
7162           && INTVAL (XEXP (x, 1)) >= 0
7163           && (INTVAL (XEXP (x, 1))
7164               <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7165           && GET_CODE (XEXP (x, 0)) == ASHIFT
7166           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7167         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7168                               reg, next_select);
7169
7170       break;
7171
7172     case ROTATE:
7173     case ROTATERT:
7174       /* If the shift count is constant and we can do computations
7175          in the mode of X, compute where the bits we care about are.
7176          Otherwise, we can't do anything.  Don't change the mode of
7177          the shift or propagate MODE into the shift, though.  */
7178       if (GET_CODE (XEXP (x, 1)) == CONST_INT
7179           && INTVAL (XEXP (x, 1)) >= 0)
7180         {
7181           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7182                                             GET_MODE (x), GEN_INT (mask),
7183                                             XEXP (x, 1));
7184           if (temp && GET_CODE (temp) == CONST_INT)
7185             SUBST (XEXP (x, 0),
7186                    force_to_mode (XEXP (x, 0), GET_MODE (x),
7187                                   INTVAL (temp), reg, next_select));
7188         }
7189       break;
7190
7191     case NEG:
7192       /* If we just want the low-order bit, the NEG isn't needed since it
7193          won't change the low-order bit.  */
7194       if (mask == 1)
7195         return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7196
7197       /* We need any bits less significant than the most significant bit in
7198          MASK since carries from those bits will affect the bits we are
7199          interested in.  */
7200       mask = fuller_mask;
7201       goto unop;
7202
7203     case NOT:
7204       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7205          same as the XOR case above.  Ensure that the constant we form is not
7206          wider than the mode of X.  */
7207
7208       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7209           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7210           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7211           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7212               < GET_MODE_BITSIZE (GET_MODE (x)))
7213           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7214         {
7215           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7216                                GET_MODE (x));
7217           temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
7218           x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
7219
7220           return force_to_mode (x, mode, mask, reg, next_select);
7221         }
7222
7223       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7224          use the full mask inside the NOT.  */
7225       mask = fuller_mask;
7226
7227     unop:
7228       op0 = gen_lowpart_for_combine (op_mode,
7229                                      force_to_mode (XEXP (x, 0), mode, mask,
7230                                                     reg, next_select));
7231       if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7232         x = simplify_gen_unary (code, op_mode, op0, op_mode);
7233       break;
7234
7235     case NE:
7236       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7237          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7238          which is equal to STORE_FLAG_VALUE.  */
7239       if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7240           && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7241           && (nonzero_bits (XEXP (x, 0), mode)
7242               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7243         return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7244
7245       break;
7246
7247     case IF_THEN_ELSE:
7248       /* We have no way of knowing if the IF_THEN_ELSE can itself be
7249          written in a narrower mode.  We play it safe and do not do so.  */
7250
7251       SUBST (XEXP (x, 1),
7252              gen_lowpart_for_combine (GET_MODE (x),
7253                                       force_to_mode (XEXP (x, 1), mode,
7254                                                      mask, reg, next_select)));
7255       SUBST (XEXP (x, 2),
7256              gen_lowpart_for_combine (GET_MODE (x),
7257                                       force_to_mode (XEXP (x, 2), mode,
7258                                                      mask, reg, next_select)));
7259       break;
7260
7261     default:
7262       break;
7263     }
7264
7265   /* Ensure we return a value of the proper mode.  */
7266   return gen_lowpart_for_combine (mode, x);
7267 }
7268 \f
7269 /* Return nonzero if X is an expression that has one of two values depending on
7270    whether some other value is zero or nonzero.  In that case, we return the
7271    value that is being tested, *PTRUE is set to the value if the rtx being
7272    returned has a nonzero value, and *PFALSE is set to the other alternative.
7273
7274    If we return zero, we set *PTRUE and *PFALSE to X.  */
7275
7276 static rtx
7277 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7278 {
7279   enum machine_mode mode = GET_MODE (x);
7280   enum rtx_code code = GET_CODE (x);
7281   rtx cond0, cond1, true0, true1, false0, false1;
7282   unsigned HOST_WIDE_INT nz;
7283
7284   /* If we are comparing a value against zero, we are done.  */
7285   if ((code == NE || code == EQ)
7286       && XEXP (x, 1) == const0_rtx)
7287     {
7288       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7289       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7290       return XEXP (x, 0);
7291     }
7292
7293   /* If this is a unary operation whose operand has one of two values, apply
7294      our opcode to compute those values.  */
7295   else if (GET_RTX_CLASS (code) == '1'
7296            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7297     {
7298       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7299       *pfalse = simplify_gen_unary (code, mode, false0,
7300                                     GET_MODE (XEXP (x, 0)));
7301       return cond0;
7302     }
7303
7304   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7305      make can't possibly match and would suppress other optimizations.  */
7306   else if (code == COMPARE)
7307     ;
7308
7309   /* If this is a binary operation, see if either side has only one of two
7310      values.  If either one does or if both do and they are conditional on
7311      the same value, compute the new true and false values.  */
7312   else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
7313            || GET_RTX_CLASS (code) == '<')
7314     {
7315       cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7316       cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7317
7318       if ((cond0 != 0 || cond1 != 0)
7319           && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7320         {
7321           /* If if_then_else_cond returned zero, then true/false are the
7322              same rtl.  We must copy one of them to prevent invalid rtl
7323              sharing.  */
7324           if (cond0 == 0)
7325             true0 = copy_rtx (true0);
7326           else if (cond1 == 0)
7327             true1 = copy_rtx (true1);
7328
7329           *ptrue = gen_binary (code, mode, true0, true1);
7330           *pfalse = gen_binary (code, mode, false0, false1);
7331           return cond0 ? cond0 : cond1;
7332         }
7333
7334       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7335          operands is zero when the other is nonzero, and vice-versa,
7336          and STORE_FLAG_VALUE is 1 or -1.  */
7337
7338       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7339           && (code == PLUS || code == IOR || code == XOR || code == MINUS
7340               || code == UMAX)
7341           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7342         {
7343           rtx op0 = XEXP (XEXP (x, 0), 1);
7344           rtx op1 = XEXP (XEXP (x, 1), 1);
7345
7346           cond0 = XEXP (XEXP (x, 0), 0);
7347           cond1 = XEXP (XEXP (x, 1), 0);
7348
7349           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7350               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7351               && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7352                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7353                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7354                   || ((swap_condition (GET_CODE (cond0))
7355                        == combine_reversed_comparison_code (cond1))
7356                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7357                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7358               && ! side_effects_p (x))
7359             {
7360               *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
7361               *pfalse = gen_binary (MULT, mode,
7362                                     (code == MINUS
7363                                      ? simplify_gen_unary (NEG, mode, op1,
7364                                                            mode)
7365                                      : op1),
7366                                     const_true_rtx);
7367               return cond0;
7368             }
7369         }
7370
7371       /* Similarly for MULT, AND and UMIN, except that for these the result
7372          is always zero.  */
7373       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7374           && (code == MULT || code == AND || code == UMIN)
7375           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7376         {
7377           cond0 = XEXP (XEXP (x, 0), 0);
7378           cond1 = XEXP (XEXP (x, 1), 0);
7379
7380           if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7381               && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7382               && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7383                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7384                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7385                   || ((swap_condition (GET_CODE (cond0))
7386                        == combine_reversed_comparison_code (cond1))
7387                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7388                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7389               && ! side_effects_p (x))
7390             {
7391               *ptrue = *pfalse = const0_rtx;
7392               return cond0;
7393             }
7394         }
7395     }
7396
7397   else if (code == IF_THEN_ELSE)
7398     {
7399       /* If we have IF_THEN_ELSE already, extract the condition and
7400          canonicalize it if it is NE or EQ.  */
7401       cond0 = XEXP (x, 0);
7402       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7403       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7404         return XEXP (cond0, 0);
7405       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7406         {
7407           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7408           return XEXP (cond0, 0);
7409         }
7410       else
7411         return cond0;
7412     }
7413
7414   /* If X is a SUBREG, we can narrow both the true and false values
7415      if the inner expression, if there is a condition.  */
7416   else if (code == SUBREG
7417            && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7418                                                &true0, &false0)))
7419     {
7420       *ptrue = simplify_gen_subreg (mode, true0,
7421                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7422       *pfalse = simplify_gen_subreg (mode, false0,
7423                                      GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7424
7425       return cond0;
7426     }
7427
7428   /* If X is a constant, this isn't special and will cause confusions
7429      if we treat it as such.  Likewise if it is equivalent to a constant.  */
7430   else if (CONSTANT_P (x)
7431            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7432     ;
7433
7434   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7435      will be least confusing to the rest of the compiler.  */
7436   else if (mode == BImode)
7437     {
7438       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7439       return x;
7440     }
7441
7442   /* If X is known to be either 0 or -1, those are the true and
7443      false values when testing X.  */
7444   else if (x == constm1_rtx || x == const0_rtx
7445            || (mode != VOIDmode
7446                && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7447     {
7448       *ptrue = constm1_rtx, *pfalse = const0_rtx;
7449       return x;
7450     }
7451
7452   /* Likewise for 0 or a single bit.  */
7453   else if (SCALAR_INT_MODE_P (mode)
7454            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7455            && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7456     {
7457       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7458       return x;
7459     }
7460
7461   /* Otherwise fail; show no condition with true and false values the same.  */
7462   *ptrue = *pfalse = x;
7463   return 0;
7464 }
7465 \f
7466 /* Return the value of expression X given the fact that condition COND
7467    is known to be true when applied to REG as its first operand and VAL
7468    as its second.  X is known to not be shared and so can be modified in
7469    place.
7470
7471    We only handle the simplest cases, and specifically those cases that
7472    arise with IF_THEN_ELSE expressions.  */
7473
7474 static rtx
7475 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7476 {
7477   enum rtx_code code = GET_CODE (x);
7478   rtx temp;
7479   const char *fmt;
7480   int i, j;
7481
7482   if (side_effects_p (x))
7483     return x;
7484
7485   /* If either operand of the condition is a floating point value,
7486      then we have to avoid collapsing an EQ comparison.  */
7487   if (cond == EQ
7488       && rtx_equal_p (x, reg)
7489       && ! FLOAT_MODE_P (GET_MODE (x))
7490       && ! FLOAT_MODE_P (GET_MODE (val)))
7491     return val;
7492
7493   if (cond == UNEQ && rtx_equal_p (x, reg))
7494     return val;
7495
7496   /* If X is (abs REG) and we know something about REG's relationship
7497      with zero, we may be able to simplify this.  */
7498
7499   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7500     switch (cond)
7501       {
7502       case GE:  case GT:  case EQ:
7503         return XEXP (x, 0);
7504       case LT:  case LE:
7505         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7506                                    XEXP (x, 0),
7507                                    GET_MODE (XEXP (x, 0)));
7508       default:
7509         break;
7510       }
7511
7512   /* The only other cases we handle are MIN, MAX, and comparisons if the
7513      operands are the same as REG and VAL.  */
7514
7515   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7516     {
7517       if (rtx_equal_p (XEXP (x, 0), val))
7518         cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7519
7520       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7521         {
7522           if (GET_RTX_CLASS (code) == '<')
7523             {
7524               if (comparison_dominates_p (cond, code))
7525                 return const_true_rtx;
7526
7527               code = combine_reversed_comparison_code (x);
7528               if (code != UNKNOWN
7529                   && comparison_dominates_p (cond, code))
7530                 return const0_rtx;
7531               else
7532                 return x;
7533             }
7534           else if (code == SMAX || code == SMIN
7535                    || code == UMIN || code == UMAX)
7536             {
7537               int unsignedp = (code == UMIN || code == UMAX);
7538
7539               /* Do not reverse the condition when it is NE or EQ.
7540                  This is because we cannot conclude anything about
7541                  the value of 'SMAX (x, y)' when x is not equal to y,
7542                  but we can when x equals y.  */
7543               if ((code == SMAX || code == UMAX)
7544                   && ! (cond == EQ || cond == NE))
7545                 cond = reverse_condition (cond);
7546
7547               switch (cond)
7548                 {
7549                 case GE:   case GT:
7550                   return unsignedp ? x : XEXP (x, 1);
7551                 case LE:   case LT:
7552                   return unsignedp ? x : XEXP (x, 0);
7553                 case GEU:  case GTU:
7554                   return unsignedp ? XEXP (x, 1) : x;
7555                 case LEU:  case LTU:
7556                   return unsignedp ? XEXP (x, 0) : x;
7557                 default:
7558                   break;
7559                 }
7560             }
7561         }
7562     }
7563   else if (code == SUBREG)
7564     {
7565       enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7566       rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7567
7568       if (SUBREG_REG (x) != r)
7569         {
7570           /* We must simplify subreg here, before we lose track of the
7571              original inner_mode.  */
7572           new = simplify_subreg (GET_MODE (x), r,
7573                                  inner_mode, SUBREG_BYTE (x));
7574           if (new)
7575             return new;
7576           else
7577             SUBST (SUBREG_REG (x), r);
7578         }
7579
7580       return x;
7581     }
7582   /* We don't have to handle SIGN_EXTEND here, because even in the
7583      case of replacing something with a modeless CONST_INT, a
7584      CONST_INT is already (supposed to be) a valid sign extension for
7585      its narrower mode, which implies it's already properly
7586      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
7587      story is different.  */
7588   else if (code == ZERO_EXTEND)
7589     {
7590       enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7591       rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7592
7593       if (XEXP (x, 0) != r)
7594         {
7595           /* We must simplify the zero_extend here, before we lose
7596              track of the original inner_mode.  */
7597           new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7598                                           r, inner_mode);
7599           if (new)
7600             return new;
7601           else
7602             SUBST (XEXP (x, 0), r);
7603         }
7604
7605       return x;
7606     }
7607
7608   fmt = GET_RTX_FORMAT (code);
7609   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7610     {
7611       if (fmt[i] == 'e')
7612         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7613       else if (fmt[i] == 'E')
7614         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7615           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7616                                                 cond, reg, val));
7617     }
7618
7619   return x;
7620 }
7621 \f
7622 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7623    assignment as a field assignment.  */
7624
7625 static int
7626 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7627 {
7628   if (x == y || rtx_equal_p (x, y))
7629     return 1;
7630
7631   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7632     return 0;
7633
7634   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7635      Note that all SUBREGs of MEM are paradoxical; otherwise they
7636      would have been rewritten.  */
7637   if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7638       && GET_CODE (SUBREG_REG (y)) == MEM
7639       && rtx_equal_p (SUBREG_REG (y),
7640                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7641     return 1;
7642
7643   if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7644       && GET_CODE (SUBREG_REG (x)) == MEM
7645       && rtx_equal_p (SUBREG_REG (x),
7646                       gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7647     return 1;
7648
7649   /* We used to see if get_last_value of X and Y were the same but that's
7650      not correct.  In one direction, we'll cause the assignment to have
7651      the wrong destination and in the case, we'll import a register into this
7652      insn that might have already have been dead.   So fail if none of the
7653      above cases are true.  */
7654   return 0;
7655 }
7656 \f
7657 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7658    Return that assignment if so.
7659
7660    We only handle the most common cases.  */
7661
7662 static rtx
7663 make_field_assignment (rtx x)
7664 {
7665   rtx dest = SET_DEST (x);
7666   rtx src = SET_SRC (x);
7667   rtx assign;
7668   rtx rhs, lhs;
7669   HOST_WIDE_INT c1;
7670   HOST_WIDE_INT pos;
7671   unsigned HOST_WIDE_INT len;
7672   rtx other;
7673   enum machine_mode mode;
7674
7675   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7676      a clear of a one-bit field.  We will have changed it to
7677      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
7678      for a SUBREG.  */
7679
7680   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7681       && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7682       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7683       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7684     {
7685       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7686                                 1, 1, 1, 0);
7687       if (assign != 0)
7688         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7689       return x;
7690     }
7691
7692   else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7693            && subreg_lowpart_p (XEXP (src, 0))
7694            && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7695                < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7696            && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7697            && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7698            && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7699            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7700     {
7701       assign = make_extraction (VOIDmode, dest, 0,
7702                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7703                                 1, 1, 1, 0);
7704       if (assign != 0)
7705         return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7706       return x;
7707     }
7708
7709   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7710      one-bit field.  */
7711   else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7712            && XEXP (XEXP (src, 0), 0) == const1_rtx
7713            && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7714     {
7715       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7716                                 1, 1, 1, 0);
7717       if (assign != 0)
7718         return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7719       return x;
7720     }
7721
7722   /* The other case we handle is assignments into a constant-position
7723      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
7724      a mask that has all one bits except for a group of zero bits and
7725      OTHER is known to have zeros where C1 has ones, this is such an
7726      assignment.  Compute the position and length from C1.  Shift OTHER
7727      to the appropriate position, force it to the required mode, and
7728      make the extraction.  Check for the AND in both operands.  */
7729
7730   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7731     return x;
7732
7733   rhs = expand_compound_operation (XEXP (src, 0));
7734   lhs = expand_compound_operation (XEXP (src, 1));
7735
7736   if (GET_CODE (rhs) == AND
7737       && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7738       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7739     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7740   else if (GET_CODE (lhs) == AND
7741            && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7742            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7743     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7744   else
7745     return x;
7746
7747   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7748   if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7749       || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7750       || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7751     return x;
7752
7753   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7754   if (assign == 0)
7755     return x;
7756
7757   /* The mode to use for the source is the mode of the assignment, or of
7758      what is inside a possible STRICT_LOW_PART.  */
7759   mode = (GET_CODE (assign) == STRICT_LOW_PART
7760           ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7761
7762   /* Shift OTHER right POS places and make it the source, restricting it
7763      to the proper length and mode.  */
7764
7765   src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7766                                              GET_MODE (src), other, pos),
7767                        mode,
7768                        GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7769                        ? ~(unsigned HOST_WIDE_INT) 0
7770                        : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7771                        dest, 0);
7772
7773   /* If SRC is masked by an AND that does not make a difference in
7774      the value being stored, strip it.  */
7775   if (GET_CODE (assign) == ZERO_EXTRACT
7776       && GET_CODE (XEXP (assign, 1)) == CONST_INT
7777       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7778       && GET_CODE (src) == AND
7779       && GET_CODE (XEXP (src, 1)) == CONST_INT
7780       && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7781           == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7782     src = XEXP (src, 0);
7783
7784   return gen_rtx_SET (VOIDmode, assign, src);
7785 }
7786 \f
7787 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7788    if so.  */
7789
7790 static rtx
7791 apply_distributive_law (rtx x)
7792 {
7793   enum rtx_code code = GET_CODE (x);
7794   rtx lhs, rhs, other;
7795   rtx tem;
7796   enum rtx_code inner_code;
7797
7798   /* Distributivity is not true for floating point.
7799      It can change the value.  So don't do it.
7800      -- rms and moshier@world.std.com.  */
7801   if (FLOAT_MODE_P (GET_MODE (x)))
7802     return x;
7803
7804   /* The outer operation can only be one of the following:  */
7805   if (code != IOR && code != AND && code != XOR
7806       && code != PLUS && code != MINUS)
7807     return x;
7808
7809   lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7810
7811   /* If either operand is a primitive we can't do anything, so get out
7812      fast.  */
7813   if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7814       || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7815     return x;
7816
7817   lhs = expand_compound_operation (lhs);
7818   rhs = expand_compound_operation (rhs);
7819   inner_code = GET_CODE (lhs);
7820   if (inner_code != GET_CODE (rhs))
7821     return x;
7822
7823   /* See if the inner and outer operations distribute.  */
7824   switch (inner_code)
7825     {
7826     case LSHIFTRT:
7827     case ASHIFTRT:
7828     case AND:
7829     case IOR:
7830       /* These all distribute except over PLUS.  */
7831       if (code == PLUS || code == MINUS)
7832         return x;
7833       break;
7834
7835     case MULT:
7836       if (code != PLUS && code != MINUS)
7837         return x;
7838       break;
7839
7840     case ASHIFT:
7841       /* This is also a multiply, so it distributes over everything.  */
7842       break;
7843
7844     case SUBREG:
7845       /* Non-paradoxical SUBREGs distributes over all operations, provided
7846          the inner modes and byte offsets are the same, this is an extraction
7847          of a low-order part, we don't convert an fp operation to int or
7848          vice versa, and we would not be converting a single-word
7849          operation into a multi-word operation.  The latter test is not
7850          required, but it prevents generating unneeded multi-word operations.
7851          Some of the previous tests are redundant given the latter test, but
7852          are retained because they are required for correctness.
7853
7854          We produce the result slightly differently in this case.  */
7855
7856       if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7857           || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
7858           || ! subreg_lowpart_p (lhs)
7859           || (GET_MODE_CLASS (GET_MODE (lhs))
7860               != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7861           || (GET_MODE_SIZE (GET_MODE (lhs))
7862               > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7863           || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7864         return x;
7865
7866       tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7867                         SUBREG_REG (lhs), SUBREG_REG (rhs));
7868       return gen_lowpart_for_combine (GET_MODE (x), tem);
7869
7870     default:
7871       return x;
7872     }
7873
7874   /* Set LHS and RHS to the inner operands (A and B in the example
7875      above) and set OTHER to the common operand (C in the example).
7876      These is only one way to do this unless the inner operation is
7877      commutative.  */
7878   if (GET_RTX_CLASS (inner_code) == 'c'
7879       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7880     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7881   else if (GET_RTX_CLASS (inner_code) == 'c'
7882            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7883     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7884   else if (GET_RTX_CLASS (inner_code) == 'c'
7885            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7886     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7887   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7888     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7889   else
7890     return x;
7891
7892   /* Form the new inner operation, seeing if it simplifies first.  */
7893   tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7894
7895   /* There is one exception to the general way of distributing:
7896      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
7897   if (code == XOR && inner_code == IOR)
7898     {
7899       inner_code = AND;
7900       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
7901     }
7902
7903   /* We may be able to continuing distributing the result, so call
7904      ourselves recursively on the inner operation before forming the
7905      outer operation, which we return.  */
7906   return gen_binary (inner_code, GET_MODE (x),
7907                      apply_distributive_law (tem), other);
7908 }
7909 \f
7910 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7911    in MODE.
7912
7913    Return an equivalent form, if different from X.  Otherwise, return X.  If
7914    X is zero, we are to always construct the equivalent form.  */
7915
7916 static rtx
7917 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
7918                         unsigned HOST_WIDE_INT constop)
7919 {
7920   unsigned HOST_WIDE_INT nonzero;
7921   int i;
7922
7923   /* Simplify VAROP knowing that we will be only looking at some of the
7924      bits in it.
7925
7926      Note by passing in CONSTOP, we guarantee that the bits not set in
7927      CONSTOP are not significant and will never be examined.  We must
7928      ensure that is the case by explicitly masking out those bits
7929      before returning.  */
7930   varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7931
7932   /* If VAROP is a CLOBBER, we will fail so return it.  */
7933   if (GET_CODE (varop) == CLOBBER)
7934     return varop;
7935
7936   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
7937      to VAROP and return the new constant.  */
7938   if (GET_CODE (varop) == CONST_INT)
7939     return GEN_INT (trunc_int_for_mode (INTVAL (varop) & constop, mode));
7940
7941   /* See what bits may be nonzero in VAROP.  Unlike the general case of
7942      a call to nonzero_bits, here we don't care about bits outside
7943      MODE.  */
7944
7945   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7946
7947   /* Turn off all bits in the constant that are known to already be zero.
7948      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7949      which is tested below.  */
7950
7951   constop &= nonzero;
7952
7953   /* If we don't have any bits left, return zero.  */
7954   if (constop == 0)
7955     return const0_rtx;
7956
7957   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7958      a power of two, we can replace this with an ASHIFT.  */
7959   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7960       && (i = exact_log2 (constop)) >= 0)
7961     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7962
7963   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7964      or XOR, then try to apply the distributive law.  This may eliminate
7965      operations if either branch can be simplified because of the AND.
7966      It may also make some cases more complex, but those cases probably
7967      won't match a pattern either with or without this.  */
7968
7969   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7970     return
7971       gen_lowpart_for_combine
7972         (mode,
7973          apply_distributive_law
7974          (gen_binary (GET_CODE (varop), GET_MODE (varop),
7975                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7976                                               XEXP (varop, 0), constop),
7977                       simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7978                                               XEXP (varop, 1), constop))));
7979
7980   /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
7981      the AND and see if one of the operands simplifies to zero.  If so, we
7982      may eliminate it.  */
7983
7984   if (GET_CODE (varop) == PLUS
7985       && exact_log2 (constop + 1) >= 0)
7986     {
7987       rtx o0, o1;
7988
7989       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
7990       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
7991       if (o0 == const0_rtx)
7992         return o1;
7993       if (o1 == const0_rtx)
7994         return o0;
7995     }
7996
7997   /* Get VAROP in MODE.  Try to get a SUBREG if not.  Don't make a new SUBREG
7998      if we already had one (just check for the simplest cases).  */
7999   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8000       && GET_MODE (XEXP (x, 0)) == mode
8001       && SUBREG_REG (XEXP (x, 0)) == varop)
8002     varop = XEXP (x, 0);
8003   else
8004     varop = gen_lowpart_for_combine (mode, varop);
8005
8006   /* If we can't make the SUBREG, try to return what we were given.  */
8007   if (GET_CODE (varop) == CLOBBER)
8008     return x ? x : varop;
8009
8010   /* If we are only masking insignificant bits, return VAROP.  */
8011   if (constop == nonzero)
8012     x = varop;
8013   else
8014     {
8015       /* Otherwise, return an AND.  */
8016       constop = trunc_int_for_mode (constop, mode);
8017       /* See how much, if any, of X we can use.  */
8018       if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8019         x = gen_binary (AND, mode, varop, GEN_INT (constop));
8020
8021       else
8022         {
8023           if (GET_CODE (XEXP (x, 1)) != CONST_INT
8024               || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8025             SUBST (XEXP (x, 1), GEN_INT (constop));
8026
8027           SUBST (XEXP (x, 0), varop);
8028         }
8029     }
8030
8031   return x;
8032 }
8033 \f
8034 #define nonzero_bits_with_known(X, MODE) \
8035   cached_nonzero_bits (X, MODE, known_x, known_mode, known_ret)
8036
8037 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
8038    It avoids exponential behavior in nonzero_bits1 when X has
8039    identical subexpressions on the first or the second level.  */
8040
8041 static unsigned HOST_WIDE_INT
8042 cached_nonzero_bits (rtx x, enum machine_mode mode, rtx known_x,
8043                      enum machine_mode known_mode,
8044                      unsigned HOST_WIDE_INT known_ret)
8045 {
8046   if (x == known_x && mode == known_mode)
8047     return known_ret;
8048
8049   /* Try to find identical subexpressions.  If found call
8050      nonzero_bits1 on X with the subexpressions as KNOWN_X and the
8051      precomputed value for the subexpression as KNOWN_RET.  */
8052
8053   if (GET_RTX_CLASS (GET_CODE (x)) == '2'
8054       || GET_RTX_CLASS (GET_CODE (x)) == 'c')
8055     {
8056       rtx x0 = XEXP (x, 0);
8057       rtx x1 = XEXP (x, 1);
8058
8059       /* Check the first level.  */
8060       if (x0 == x1)
8061         return nonzero_bits1 (x, mode, x0, mode,
8062                               nonzero_bits_with_known (x0, mode));
8063
8064       /* Check the second level.  */
8065       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
8066            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
8067           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8068         return nonzero_bits1 (x, mode, x1, mode,
8069                               nonzero_bits_with_known (x1, mode));
8070
8071       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
8072            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
8073           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8074         return nonzero_bits1 (x, mode, x0, mode,
8075                          nonzero_bits_with_known (x0, mode));
8076     }
8077
8078   return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
8079 }
8080
8081 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
8082    We don't let nonzero_bits recur into num_sign_bit_copies, because that
8083    is less useful.  We can't allow both, because that results in exponential
8084    run time recursion.  There is a nullstone testcase that triggered
8085    this.  This macro avoids accidental uses of num_sign_bit_copies.  */
8086 #define cached_num_sign_bit_copies()
8087
8088 /* Given an expression, X, compute which bits in X can be nonzero.
8089    We don't care about bits outside of those defined in MODE.
8090
8091    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8092    a shift, AND, or zero_extract, we can do better.  */
8093
8094 static unsigned HOST_WIDE_INT
8095 nonzero_bits1 (rtx x, enum machine_mode mode, rtx known_x,
8096                enum machine_mode known_mode,
8097                unsigned HOST_WIDE_INT known_ret)
8098 {
8099   unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
8100   unsigned HOST_WIDE_INT inner_nz;
8101   enum rtx_code code;
8102   unsigned int mode_width = GET_MODE_BITSIZE (mode);
8103   rtx tem;
8104
8105   /* For floating-point values, assume all bits are needed.  */
8106   if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
8107     return nonzero;
8108
8109   /* If X is wider than MODE, use its mode instead.  */
8110   if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
8111     {
8112       mode = GET_MODE (x);
8113       nonzero = GET_MODE_MASK (mode);
8114       mode_width = GET_MODE_BITSIZE (mode);
8115     }
8116
8117   if (mode_width > HOST_BITS_PER_WIDE_INT)
8118     /* Our only callers in this case look for single bit values.  So
8119        just return the mode mask.  Those tests will then be false.  */
8120     return nonzero;
8121
8122 #ifndef WORD_REGISTER_OPERATIONS
8123   /* If MODE is wider than X, but both are a single word for both the host
8124      and target machines, we can compute this from which bits of the
8125      object might be nonzero in its own mode, taking into account the fact
8126      that on many CISC machines, accessing an object in a wider mode
8127      causes the high-order bits to become undefined.  So they are
8128      not known to be zero.  */
8129
8130   if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
8131       && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
8132       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8133       && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
8134     {
8135       nonzero &= nonzero_bits_with_known (x, GET_MODE (x));
8136       nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
8137       return nonzero;
8138     }
8139 #endif
8140
8141   code = GET_CODE (x);
8142   switch (code)
8143     {
8144     case REG:
8145 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8146       /* If pointers extend unsigned and this is a pointer in Pmode, say that
8147          all the bits above ptr_mode are known to be zero.  */
8148       if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8149           && REG_POINTER (x))
8150         nonzero &= GET_MODE_MASK (ptr_mode);
8151 #endif
8152
8153       /* Include declared information about alignment of pointers.  */
8154       /* ??? We don't properly preserve REG_POINTER changes across
8155          pointer-to-integer casts, so we can't trust it except for
8156          things that we know must be pointers.  See execute/960116-1.c.  */
8157       if ((x == stack_pointer_rtx
8158            || x == frame_pointer_rtx
8159            || x == arg_pointer_rtx)
8160           && REGNO_POINTER_ALIGN (REGNO (x)))
8161         {
8162           unsigned HOST_WIDE_INT alignment
8163             = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
8164
8165 #ifdef PUSH_ROUNDING
8166           /* If PUSH_ROUNDING is defined, it is possible for the
8167              stack to be momentarily aligned only to that amount,
8168              so we pick the least alignment.  */
8169           if (x == stack_pointer_rtx && PUSH_ARGS)
8170             alignment = MIN ((unsigned HOST_WIDE_INT) PUSH_ROUNDING (1),
8171                              alignment);
8172 #endif
8173
8174           nonzero &= ~(alignment - 1);
8175         }
8176
8177       /* If X is a register whose nonzero bits value is current, use it.
8178          Otherwise, if X is a register whose value we can find, use that
8179          value.  Otherwise, use the previously-computed global nonzero bits
8180          for this register.  */
8181
8182       if (reg_last_set_value[REGNO (x)] != 0
8183           && (reg_last_set_mode[REGNO (x)] == mode
8184               || (GET_MODE_CLASS (reg_last_set_mode[REGNO (x)]) == MODE_INT
8185                   && GET_MODE_CLASS (mode) == MODE_INT))
8186           && (reg_last_set_label[REGNO (x)] == label_tick
8187               || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8188                   && REG_N_SETS (REGNO (x)) == 1
8189                   && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8190                                         REGNO (x))))
8191           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8192         return reg_last_set_nonzero_bits[REGNO (x)] & nonzero;
8193
8194       tem = get_last_value (x);
8195
8196       if (tem)
8197         {
8198 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8199           /* If X is narrower than MODE and TEM is a non-negative
8200              constant that would appear negative in the mode of X,
8201              sign-extend it for use in reg_nonzero_bits because some
8202              machines (maybe most) will actually do the sign-extension
8203              and this is the conservative approach.
8204
8205              ??? For 2.5, try to tighten up the MD files in this regard
8206              instead of this kludge.  */
8207
8208           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
8209               && GET_CODE (tem) == CONST_INT
8210               && INTVAL (tem) > 0
8211               && 0 != (INTVAL (tem)
8212                        & ((HOST_WIDE_INT) 1
8213                           << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8214             tem = GEN_INT (INTVAL (tem)
8215                            | ((HOST_WIDE_INT) (-1)
8216                               << GET_MODE_BITSIZE (GET_MODE (x))));
8217 #endif
8218           return nonzero_bits_with_known (tem, mode) & nonzero;
8219         }
8220       else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
8221         {
8222           unsigned HOST_WIDE_INT mask = reg_nonzero_bits[REGNO (x)];
8223
8224           if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width)
8225             /* We don't know anything about the upper bits.  */
8226             mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8227           return nonzero & mask;
8228         }
8229       else
8230         return nonzero;
8231
8232     case CONST_INT:
8233 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8234       /* If X is negative in MODE, sign-extend the value.  */
8235       if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
8236           && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
8237         return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
8238 #endif
8239
8240       return INTVAL (x);
8241
8242     case MEM:
8243 #ifdef LOAD_EXTEND_OP
8244       /* In many, if not most, RISC machines, reading a byte from memory
8245          zeros the rest of the register.  Noticing that fact saves a lot
8246          of extra zero-extends.  */
8247       if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
8248         nonzero &= GET_MODE_MASK (GET_MODE (x));
8249 #endif
8250       break;
8251
8252     case EQ:  case NE:
8253     case UNEQ:  case LTGT:
8254     case GT:  case GTU:  case UNGT:
8255     case LT:  case LTU:  case UNLT:
8256     case GE:  case GEU:  case UNGE:
8257     case LE:  case LEU:  case UNLE:
8258     case UNORDERED: case ORDERED:
8259
8260       /* If this produces an integer result, we know which bits are set.
8261          Code here used to clear bits outside the mode of X, but that is
8262          now done above.  */
8263
8264       if (GET_MODE_CLASS (mode) == MODE_INT
8265           && mode_width <= HOST_BITS_PER_WIDE_INT)
8266         nonzero = STORE_FLAG_VALUE;
8267       break;
8268
8269     case NEG:
8270 #if 0
8271       /* Disabled to avoid exponential mutual recursion between nonzero_bits
8272          and num_sign_bit_copies.  */
8273       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8274           == GET_MODE_BITSIZE (GET_MODE (x)))
8275         nonzero = 1;
8276 #endif
8277
8278       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
8279         nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
8280       break;
8281
8282     case ABS:
8283 #if 0
8284       /* Disabled to avoid exponential mutual recursion between nonzero_bits
8285          and num_sign_bit_copies.  */
8286       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8287           == GET_MODE_BITSIZE (GET_MODE (x)))
8288         nonzero = 1;
8289 #endif
8290       break;
8291
8292     case TRUNCATE:
8293       nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8294                   & GET_MODE_MASK (mode));
8295       break;
8296
8297     case ZERO_EXTEND:
8298       nonzero &= nonzero_bits_with_known (XEXP (x, 0), mode);
8299       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8300         nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8301       break;
8302
8303     case SIGN_EXTEND:
8304       /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
8305          Otherwise, show all the bits in the outer mode but not the inner
8306          may be nonzero.  */
8307       inner_nz = nonzero_bits_with_known (XEXP (x, 0), mode);
8308       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8309         {
8310           inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8311           if (inner_nz
8312               & (((HOST_WIDE_INT) 1
8313                   << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
8314             inner_nz |= (GET_MODE_MASK (mode)
8315                          & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
8316         }
8317
8318       nonzero &= inner_nz;
8319       break;
8320
8321     case AND:
8322       nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8323                   & nonzero_bits_with_known (XEXP (x, 1), mode));
8324       break;
8325
8326     case XOR:   case IOR:
8327     case UMIN:  case UMAX:  case SMIN:  case SMAX:
8328       {
8329         unsigned HOST_WIDE_INT nonzero0 =
8330           nonzero_bits_with_known (XEXP (x, 0), mode);
8331
8332         /* Don't call nonzero_bits for the second time if it cannot change
8333            anything.  */
8334         if ((nonzero & nonzero0) != nonzero)
8335           nonzero &= (nonzero0
8336                       | nonzero_bits_with_known (XEXP (x, 1), mode));
8337       }
8338       break;
8339
8340     case PLUS:  case MINUS:
8341     case MULT:
8342     case DIV:   case UDIV:
8343     case MOD:   case UMOD:
8344       /* We can apply the rules of arithmetic to compute the number of
8345          high- and low-order zero bits of these operations.  We start by
8346          computing the width (position of the highest-order nonzero bit)
8347          and the number of low-order zero bits for each value.  */
8348       {
8349         unsigned HOST_WIDE_INT nz0 =
8350           nonzero_bits_with_known (XEXP (x, 0), mode);
8351         unsigned HOST_WIDE_INT nz1 =
8352           nonzero_bits_with_known (XEXP (x, 1), mode);
8353         int sign_index = GET_MODE_BITSIZE (GET_MODE (x)) - 1;
8354         int width0 = floor_log2 (nz0) + 1;
8355         int width1 = floor_log2 (nz1) + 1;
8356         int low0 = floor_log2 (nz0 & -nz0);
8357         int low1 = floor_log2 (nz1 & -nz1);
8358         HOST_WIDE_INT op0_maybe_minusp
8359           = (nz0 & ((HOST_WIDE_INT) 1 << sign_index));
8360         HOST_WIDE_INT op1_maybe_minusp
8361           = (nz1 & ((HOST_WIDE_INT) 1 << sign_index));
8362         unsigned int result_width = mode_width;
8363         int result_low = 0;
8364
8365         switch (code)
8366           {
8367           case PLUS:
8368             result_width = MAX (width0, width1) + 1;
8369             result_low = MIN (low0, low1);
8370             break;
8371           case MINUS:
8372             result_low = MIN (low0, low1);
8373             break;
8374           case MULT:
8375             result_width = width0 + width1;
8376             result_low = low0 + low1;
8377             break;
8378           case DIV:
8379             if (width1 == 0)
8380               break;
8381             if (! op0_maybe_minusp && ! op1_maybe_minusp)
8382               result_width = width0;
8383             break;
8384           case UDIV:
8385             if (width1 == 0)
8386               break;
8387             result_width = width0;
8388             break;
8389           case MOD:
8390             if (width1 == 0)
8391               break;
8392             if (! op0_maybe_minusp && ! op1_maybe_minusp)
8393               result_width = MIN (width0, width1);
8394             result_low = MIN (low0, low1);
8395             break;
8396           case UMOD:
8397             if (width1 == 0)
8398               break;
8399             result_width = MIN (width0, width1);
8400             result_low = MIN (low0, low1);
8401             break;
8402           default:
8403             abort ();
8404           }
8405
8406         if (result_width < mode_width)
8407           nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
8408
8409         if (result_low > 0)
8410           nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
8411
8412 #ifdef POINTERS_EXTEND_UNSIGNED
8413         /* If pointers extend unsigned and this is an addition or subtraction
8414            to a pointer in Pmode, all the bits above ptr_mode are known to be
8415            zero.  */
8416         if (POINTERS_EXTEND_UNSIGNED > 0 && GET_MODE (x) == Pmode
8417             && (code == PLUS || code == MINUS)
8418             && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8419           nonzero &= GET_MODE_MASK (ptr_mode);
8420 #endif
8421       }
8422       break;
8423
8424     case ZERO_EXTRACT:
8425       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8426           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8427         nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
8428       break;
8429
8430     case SUBREG:
8431       /* If this is a SUBREG formed for a promoted variable that has
8432          been zero-extended, we know that at least the high-order bits
8433          are zero, though others might be too.  */
8434
8435       if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x) > 0)
8436         nonzero = (GET_MODE_MASK (GET_MODE (x))
8437                    & nonzero_bits_with_known (SUBREG_REG (x), GET_MODE (x)));
8438
8439       /* If the inner mode is a single word for both the host and target
8440          machines, we can compute this from which bits of the inner
8441          object might be nonzero.  */
8442       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
8443           && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8444               <= HOST_BITS_PER_WIDE_INT))
8445         {
8446           nonzero &= nonzero_bits_with_known (SUBREG_REG (x), mode);
8447
8448 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
8449           /* If this is a typical RISC machine, we only have to worry
8450              about the way loads are extended.  */
8451           if ((LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8452                ? (((nonzero
8453                     & (((unsigned HOST_WIDE_INT) 1
8454                         << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
8455                    != 0))
8456                : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
8457               || GET_CODE (SUBREG_REG (x)) != MEM)
8458 #endif
8459             {
8460               /* On many CISC machines, accessing an object in a wider mode
8461                  causes the high-order bits to become undefined.  So they are
8462                  not known to be zero.  */
8463               if (GET_MODE_SIZE (GET_MODE (x))
8464                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8465                 nonzero |= (GET_MODE_MASK (GET_MODE (x))
8466                             & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
8467             }
8468         }
8469       break;
8470
8471     case ASHIFTRT:
8472     case LSHIFTRT:
8473     case ASHIFT:
8474     case ROTATE:
8475       /* The nonzero bits are in two classes: any bits within MODE
8476          that aren't in GET_MODE (x) are always significant.  The rest of the
8477          nonzero bits are those that are significant in the operand of
8478          the shift when shifted the appropriate number of bits.  This
8479          shows that high-order bits are cleared by the right shift and
8480          low-order bits by left shifts.  */
8481       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8482           && INTVAL (XEXP (x, 1)) >= 0
8483           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8484         {
8485           enum machine_mode inner_mode = GET_MODE (x);
8486           unsigned int width = GET_MODE_BITSIZE (inner_mode);
8487           int count = INTVAL (XEXP (x, 1));
8488           unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
8489           unsigned HOST_WIDE_INT op_nonzero =
8490             nonzero_bits_with_known (XEXP (x, 0), mode);
8491           unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
8492           unsigned HOST_WIDE_INT outer = 0;
8493
8494           if (mode_width > width)
8495             outer = (op_nonzero & nonzero & ~mode_mask);
8496
8497           if (code == LSHIFTRT)
8498             inner >>= count;
8499           else if (code == ASHIFTRT)
8500             {
8501               inner >>= count;
8502
8503               /* If the sign bit may have been nonzero before the shift, we
8504                  need to mark all the places it could have been copied to
8505                  by the shift as possibly nonzero.  */
8506               if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
8507                 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
8508             }
8509           else if (code == ASHIFT)
8510             inner <<= count;
8511           else
8512             inner = ((inner << (count % width)
8513                       | (inner >> (width - (count % width)))) & mode_mask);
8514
8515           nonzero &= (outer | inner);
8516         }
8517       break;
8518
8519     case FFS:
8520     case POPCOUNT:
8521       /* This is at most the number of bits in the mode.  */
8522       nonzero = ((HOST_WIDE_INT) 2 << (floor_log2 (mode_width))) - 1;
8523       break;
8524
8525     case CLZ:
8526       /* If CLZ has a known value at zero, then the nonzero bits are
8527          that value, plus the number of bits in the mode minus one.  */
8528       if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8529         nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8530       else
8531         nonzero = -1;
8532       break;
8533
8534     case CTZ:
8535       /* If CTZ has a known value at zero, then the nonzero bits are
8536          that value, plus the number of bits in the mode minus one.  */
8537       if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8538         nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8539       else
8540         nonzero = -1;
8541       break;
8542
8543     case PARITY:
8544       nonzero = 1;
8545       break;
8546
8547     case IF_THEN_ELSE:
8548       nonzero &= (nonzero_bits_with_known (XEXP (x, 1), mode)
8549                   | nonzero_bits_with_known (XEXP (x, 2), mode));
8550       break;
8551
8552     default:
8553       break;
8554     }
8555
8556   return nonzero;
8557 }
8558
8559 /* See the macro definition above.  */
8560 #undef cached_num_sign_bit_copies
8561 \f
8562 #define num_sign_bit_copies_with_known(X, M) \
8563   cached_num_sign_bit_copies (X, M, known_x, known_mode, known_ret)
8564
8565 /* The function cached_num_sign_bit_copies is a wrapper around
8566    num_sign_bit_copies1.  It avoids exponential behavior in
8567    num_sign_bit_copies1 when X has identical subexpressions on the
8568    first or the second level.  */
8569
8570 static unsigned int
8571 cached_num_sign_bit_copies (rtx x, enum machine_mode mode, rtx known_x,
8572                             enum machine_mode known_mode,
8573                             unsigned int known_ret)
8574 {
8575   if (x == known_x && mode == known_mode)
8576     return known_ret;
8577
8578   /* Try to find identical subexpressions.  If found call
8579      num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
8580      the precomputed value for the subexpression as KNOWN_RET.  */
8581
8582   if (GET_RTX_CLASS (GET_CODE (x)) == '2'
8583       || GET_RTX_CLASS (GET_CODE (x)) == 'c')
8584     {
8585       rtx x0 = XEXP (x, 0);
8586       rtx x1 = XEXP (x, 1);
8587
8588       /* Check the first level.  */
8589       if (x0 == x1)
8590         return
8591           num_sign_bit_copies1 (x, mode, x0, mode,
8592                                 num_sign_bit_copies_with_known (x0, mode));
8593
8594       /* Check the second level.  */
8595       if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
8596            || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
8597           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8598         return
8599           num_sign_bit_copies1 (x, mode, x1, mode,
8600                                 num_sign_bit_copies_with_known (x1, mode));
8601
8602       if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
8603            || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
8604           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8605         return
8606           num_sign_bit_copies1 (x, mode, x0, mode,
8607                                 num_sign_bit_copies_with_known (x0, mode));
8608     }
8609
8610   return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
8611 }
8612
8613 /* Return the number of bits at the high-order end of X that are known to
8614    be equal to the sign bit.  X will be used in mode MODE; if MODE is
8615    VOIDmode, X will be used in its own mode.  The returned value  will always
8616    be between 1 and the number of bits in MODE.  */
8617
8618 static unsigned int
8619 num_sign_bit_copies1 (rtx x, enum machine_mode mode, rtx known_x,
8620                       enum machine_mode known_mode,
8621                       unsigned int known_ret)
8622 {
8623   enum rtx_code code = GET_CODE (x);
8624   unsigned int bitwidth;
8625   int num0, num1, result;
8626   unsigned HOST_WIDE_INT nonzero;
8627   rtx tem;
8628
8629   /* If we weren't given a mode, use the mode of X.  If the mode is still
8630      VOIDmode, we don't know anything.  Likewise if one of the modes is
8631      floating-point.  */
8632
8633   if (mode == VOIDmode)
8634     mode = GET_MODE (x);
8635
8636   if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
8637     return 1;
8638
8639   bitwidth = GET_MODE_BITSIZE (mode);
8640
8641   /* For a smaller object, just ignore the high bits.  */
8642   if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
8643     {
8644       num0 = num_sign_bit_copies_with_known (x, GET_MODE (x));
8645       return MAX (1,
8646                   num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
8647     }
8648
8649   if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
8650     {
8651 #ifndef WORD_REGISTER_OPERATIONS
8652   /* If this machine does not do all register operations on the entire
8653      register and MODE is wider than the mode of X, we can say nothing
8654      at all about the high-order bits.  */
8655       return 1;
8656 #else
8657       /* Likewise on machines that do, if the mode of the object is smaller
8658          than a word and loads of that size don't sign extend, we can say
8659          nothing about the high order bits.  */
8660       if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
8661 #ifdef LOAD_EXTEND_OP
8662           && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
8663 #endif
8664           )
8665         return 1;
8666 #endif
8667     }
8668
8669   switch (code)
8670     {
8671     case REG:
8672
8673 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8674       /* If pointers extend signed and this is a pointer in Pmode, say that
8675          all the bits above ptr_mode are known to be sign bit copies.  */
8676       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8677           && REG_POINTER (x))
8678         return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8679 #endif
8680
8681       if (reg_last_set_value[REGNO (x)] != 0
8682           && reg_last_set_mode[REGNO (x)] == mode
8683           && (reg_last_set_label[REGNO (x)] == label_tick
8684               || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8685                   && REG_N_SETS (REGNO (x)) == 1
8686                   && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8687                                         REGNO (x))))
8688           && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8689         return reg_last_set_sign_bit_copies[REGNO (x)];
8690
8691       tem = get_last_value (x);
8692       if (tem != 0)
8693         return num_sign_bit_copies_with_known (tem, mode);
8694
8695       if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0
8696           && GET_MODE_BITSIZE (GET_MODE (x)) == bitwidth)
8697         return reg_sign_bit_copies[REGNO (x)];
8698       break;
8699
8700     case MEM:
8701 #ifdef LOAD_EXTEND_OP
8702       /* Some RISC machines sign-extend all loads of smaller than a word.  */
8703       if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8704         return MAX (1, ((int) bitwidth
8705                         - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
8706 #endif
8707       break;
8708
8709     case CONST_INT:
8710       /* If the constant is negative, take its 1's complement and remask.
8711          Then see how many zero bits we have.  */
8712       nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8713       if (bitwidth <= HOST_BITS_PER_WIDE_INT
8714           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8715         nonzero = (~nonzero) & GET_MODE_MASK (mode);
8716
8717       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8718
8719     case SUBREG:
8720       /* If this is a SUBREG for a promoted object that is sign-extended
8721          and we are looking at it in a wider mode, we know that at least the
8722          high-order bits are known to be sign bit copies.  */
8723
8724       if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8725         {
8726           num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8727           return MAX ((int) bitwidth
8728                       - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8729                       num0);
8730         }
8731
8732       /* For a smaller object, just ignore the high bits.  */
8733       if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8734         {
8735           num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), VOIDmode);
8736           return MAX (1, (num0
8737                           - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8738                                    - bitwidth)));
8739         }
8740
8741 #ifdef WORD_REGISTER_OPERATIONS
8742 #ifdef LOAD_EXTEND_OP
8743       /* For paradoxical SUBREGs on machines where all register operations
8744          affect the entire register, just look inside.  Note that we are
8745          passing MODE to the recursive call, so the number of sign bit copies
8746          will remain relative to that mode, not the inner mode.  */
8747
8748       /* This works only if loads sign extend.  Otherwise, if we get a
8749          reload for the inner part, it may be loaded from the stack, and
8750          then we lose all sign bit copies that existed before the store
8751          to the stack.  */
8752
8753       if ((GET_MODE_SIZE (GET_MODE (x))
8754            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8755           && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8756           && GET_CODE (SUBREG_REG (x)) == MEM)
8757         return num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8758 #endif
8759 #endif
8760       break;
8761
8762     case SIGN_EXTRACT:
8763       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8764         return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
8765       break;
8766
8767     case SIGN_EXTEND:
8768       return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8769               + num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode));
8770
8771     case TRUNCATE:
8772       /* For a smaller object, just ignore the high bits.  */
8773       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode);
8774       return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8775                                     - bitwidth)));
8776
8777     case NOT:
8778       return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8779
8780     case ROTATE:       case ROTATERT:
8781       /* If we are rotating left by a number of bits less than the number
8782          of sign bit copies, we can just subtract that amount from the
8783          number.  */
8784       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8785           && INTVAL (XEXP (x, 1)) >= 0
8786           && INTVAL (XEXP (x, 1)) < (int) bitwidth)
8787         {
8788           num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8789           return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8790                                  : (int) bitwidth - INTVAL (XEXP (x, 1))));
8791         }
8792       break;
8793
8794     case NEG:
8795       /* In general, this subtracts one sign bit copy.  But if the value
8796          is known to be positive, the number of sign bit copies is the
8797          same as that of the input.  Finally, if the input has just one bit
8798          that might be nonzero, all the bits are copies of the sign bit.  */
8799       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8800       if (bitwidth > HOST_BITS_PER_WIDE_INT)
8801         return num0 > 1 ? num0 - 1 : 1;
8802
8803       nonzero = nonzero_bits (XEXP (x, 0), mode);
8804       if (nonzero == 1)
8805         return bitwidth;
8806
8807       if (num0 > 1
8808           && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8809         num0--;
8810
8811       return num0;
8812
8813     case IOR:   case AND:   case XOR:
8814     case SMIN:  case SMAX:  case UMIN:  case UMAX:
8815       /* Logical operations will preserve the number of sign-bit copies.
8816          MIN and MAX operations always return one of the operands.  */
8817       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8818       num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8819       return MIN (num0, num1);
8820
8821     case PLUS:  case MINUS:
8822       /* For addition and subtraction, we can have a 1-bit carry.  However,
8823          if we are subtracting 1 from a positive number, there will not
8824          be such a carry.  Furthermore, if the positive number is known to
8825          be 0 or 1, we know the result is either -1 or 0.  */
8826
8827       if (code == PLUS && XEXP (x, 1) == constm1_rtx
8828           && bitwidth <= HOST_BITS_PER_WIDE_INT)
8829         {
8830           nonzero = nonzero_bits (XEXP (x, 0), mode);
8831           if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8832             return (nonzero == 1 || nonzero == 0 ? bitwidth
8833                     : bitwidth - floor_log2 (nonzero) - 1);
8834         }
8835
8836       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8837       num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8838       result = MAX (1, MIN (num0, num1) - 1);
8839
8840 #ifdef POINTERS_EXTEND_UNSIGNED
8841       /* If pointers extend signed and this is an addition or subtraction
8842          to a pointer in Pmode, all the bits above ptr_mode are known to be
8843          sign bit copies.  */
8844       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8845           && (code == PLUS || code == MINUS)
8846           && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8847         result = MAX ((int) (GET_MODE_BITSIZE (Pmode)
8848                              - GET_MODE_BITSIZE (ptr_mode) + 1),
8849                       result);
8850 #endif
8851       return result;
8852
8853     case MULT:
8854       /* The number of bits of the product is the sum of the number of
8855          bits of both terms.  However, unless one of the terms if known
8856          to be positive, we must allow for an additional bit since negating
8857          a negative number can remove one sign bit copy.  */
8858
8859       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8860       num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8861
8862       result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8863       if (result > 0
8864           && (bitwidth > HOST_BITS_PER_WIDE_INT
8865               || (((nonzero_bits (XEXP (x, 0), mode)
8866                     & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8867                   && ((nonzero_bits (XEXP (x, 1), mode)
8868                        & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8869         result--;
8870
8871       return MAX (1, result);
8872
8873     case UDIV:
8874       /* The result must be <= the first operand.  If the first operand
8875          has the high bit set, we know nothing about the number of sign
8876          bit copies.  */
8877       if (bitwidth > HOST_BITS_PER_WIDE_INT)
8878         return 1;
8879       else if ((nonzero_bits (XEXP (x, 0), mode)
8880                 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8881         return 1;
8882       else
8883         return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8884
8885     case UMOD:
8886       /* The result must be <= the second operand.  */
8887       return num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8888
8889     case DIV:
8890       /* Similar to unsigned division, except that we have to worry about
8891          the case where the divisor is negative, in which case we have
8892          to add 1.  */
8893       result = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8894       if (result > 1
8895           && (bitwidth > HOST_BITS_PER_WIDE_INT
8896               || (nonzero_bits (XEXP (x, 1), mode)
8897                   & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8898         result--;
8899
8900       return result;
8901
8902     case MOD:
8903       result = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8904       if (result > 1
8905           && (bitwidth > HOST_BITS_PER_WIDE_INT
8906               || (nonzero_bits (XEXP (x, 1), mode)
8907                   & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8908         result--;
8909
8910       return result;
8911
8912     case ASHIFTRT:
8913       /* Shifts by a constant add to the number of bits equal to the
8914          sign bit.  */
8915       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8916       if (GET_CODE (XEXP (x, 1)) == CONST_INT
8917           && INTVAL (XEXP (x, 1)) > 0)
8918         num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
8919
8920       return num0;
8921
8922     case ASHIFT:
8923       /* Left shifts destroy copies.  */
8924       if (GET_CODE (XEXP (x, 1)) != CONST_INT
8925           || INTVAL (XEXP (x, 1)) < 0
8926           || INTVAL (XEXP (x, 1)) >= (int) bitwidth)
8927         return 1;
8928
8929       num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8930       return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8931
8932     case IF_THEN_ELSE:
8933       num0 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8934       num1 = num_sign_bit_copies_with_known (XEXP (x, 2), mode);
8935       return MIN (num0, num1);
8936
8937     case EQ:  case NE:  case GE:  case GT:  case LE:  case LT:
8938     case UNEQ:  case LTGT:  case UNGE:  case UNGT:  case UNLE:  case UNLT:
8939     case GEU: case GTU: case LEU: case LTU:
8940     case UNORDERED: case ORDERED:
8941       /* If the constant is negative, take its 1's complement and remask.
8942          Then see how many zero bits we have.  */
8943       nonzero = STORE_FLAG_VALUE;
8944       if (bitwidth <= HOST_BITS_PER_WIDE_INT
8945           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8946         nonzero = (~nonzero) & GET_MODE_MASK (mode);
8947
8948       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8949       break;
8950
8951     default:
8952       break;
8953     }
8954
8955   /* If we haven't been able to figure it out by one of the above rules,
8956      see if some of the high-order bits are known to be zero.  If so,
8957      count those bits and return one less than that amount.  If we can't
8958      safely compute the mask for this mode, always return BITWIDTH.  */
8959
8960   if (bitwidth > HOST_BITS_PER_WIDE_INT)
8961     return 1;
8962
8963   nonzero = nonzero_bits (x, mode);
8964   return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8965           ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8966 }
8967 \f
8968 /* Return the number of "extended" bits there are in X, when interpreted
8969    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
8970    unsigned quantities, this is the number of high-order zero bits.
8971    For signed quantities, this is the number of copies of the sign bit
8972    minus 1.  In both case, this function returns the number of "spare"
8973    bits.  For example, if two quantities for which this function returns
8974    at least 1 are added, the addition is known not to overflow.
8975
8976    This function will always return 0 unless called during combine, which
8977    implies that it must be called from a define_split.  */
8978
8979 unsigned int
8980 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8981 {
8982   if (nonzero_sign_valid == 0)
8983     return 0;
8984
8985   return (unsignedp
8986           ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8987              ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8988                                - floor_log2 (nonzero_bits (x, mode)))
8989              : 0)
8990           : num_sign_bit_copies (x, mode) - 1);
8991 }
8992 \f
8993 /* This function is called from `simplify_shift_const' to merge two
8994    outer operations.  Specifically, we have already found that we need
8995    to perform operation *POP0 with constant *PCONST0 at the outermost
8996    position.  We would now like to also perform OP1 with constant CONST1
8997    (with *POP0 being done last).
8998
8999    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9000    the resulting operation.  *PCOMP_P is set to 1 if we would need to
9001    complement the innermost operand, otherwise it is unchanged.
9002
9003    MODE is the mode in which the operation will be done.  No bits outside
9004    the width of this mode matter.  It is assumed that the width of this mode
9005    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9006
9007    If *POP0 or OP1 are NIL, it means no operation is required.  Only NEG, PLUS,
9008    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
9009    result is simply *PCONST0.
9010
9011    If the resulting operation cannot be expressed as one operation, we
9012    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
9013
9014 static int
9015 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)
9016 {
9017   enum rtx_code op0 = *pop0;
9018   HOST_WIDE_INT const0 = *pconst0;
9019
9020   const0 &= GET_MODE_MASK (mode);
9021   const1 &= GET_MODE_MASK (mode);
9022
9023   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
9024   if (op0 == AND)
9025     const1 &= const0;
9026
9027   /* If OP0 or OP1 is NIL, this is easy.  Similarly if they are the same or
9028      if OP0 is SET.  */
9029
9030   if (op1 == NIL || op0 == SET)
9031     return 1;
9032
9033   else if (op0 == NIL)
9034     op0 = op1, const0 = const1;
9035
9036   else if (op0 == op1)
9037     {
9038       switch (op0)
9039         {
9040         case AND:
9041           const0 &= const1;
9042           break;
9043         case IOR:
9044           const0 |= const1;
9045           break;
9046         case XOR:
9047           const0 ^= const1;
9048           break;
9049         case PLUS:
9050           const0 += const1;
9051           break;
9052         case NEG:
9053           op0 = NIL;
9054           break;
9055         default:
9056           break;
9057         }
9058     }
9059
9060   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
9061   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9062     return 0;
9063
9064   /* If the two constants aren't the same, we can't do anything.  The
9065      remaining six cases can all be done.  */
9066   else if (const0 != const1)
9067     return 0;
9068
9069   else
9070     switch (op0)
9071       {
9072       case IOR:
9073         if (op1 == AND)
9074           /* (a & b) | b == b */
9075           op0 = SET;
9076         else /* op1 == XOR */
9077           /* (a ^ b) | b == a | b */
9078           {;}
9079         break;
9080
9081       case XOR:
9082         if (op1 == AND)
9083           /* (a & b) ^ b == (~a) & b */
9084           op0 = AND, *pcomp_p = 1;
9085         else /* op1 == IOR */
9086           /* (a | b) ^ b == a & ~b */
9087           op0 = AND, const0 = ~const0;
9088         break;
9089
9090       case AND:
9091         if (op1 == IOR)
9092           /* (a | b) & b == b */
9093         op0 = SET;
9094         else /* op1 == XOR */
9095           /* (a ^ b) & b) == (~a) & b */
9096           *pcomp_p = 1;
9097         break;
9098       default:
9099         break;
9100       }
9101
9102   /* Check for NO-OP cases.  */
9103   const0 &= GET_MODE_MASK (mode);
9104   if (const0 == 0
9105       && (op0 == IOR || op0 == XOR || op0 == PLUS))
9106     op0 = NIL;
9107   else if (const0 == 0 && op0 == AND)
9108     op0 = SET;
9109   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9110            && op0 == AND)
9111     op0 = NIL;
9112
9113   /* ??? Slightly redundant with the above mask, but not entirely.
9114      Moving this above means we'd have to sign-extend the mode mask
9115      for the final test.  */
9116   const0 = trunc_int_for_mode (const0, mode);
9117
9118   *pop0 = op0;
9119   *pconst0 = const0;
9120
9121   return 1;
9122 }
9123 \f
9124 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
9125    The result of the shift is RESULT_MODE.  X, if nonzero, is an expression
9126    that we started with.
9127
9128    The shift is normally computed in the widest mode we find in VAROP, as
9129    long as it isn't a different number of words than RESULT_MODE.  Exceptions
9130    are ASHIFTRT and ROTATE, which are always done in their original mode,  */
9131
9132 static rtx
9133 simplify_shift_const (rtx x, enum rtx_code code,
9134                       enum machine_mode result_mode, rtx varop,
9135                       int orig_count)
9136 {
9137   enum rtx_code orig_code = code;
9138   unsigned int count;
9139   int signed_count;
9140   enum machine_mode mode = result_mode;
9141   enum machine_mode shift_mode, tmode;
9142   unsigned int mode_words
9143     = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9144   /* We form (outer_op (code varop count) (outer_const)).  */
9145   enum rtx_code outer_op = NIL;
9146   HOST_WIDE_INT outer_const = 0;
9147   rtx const_rtx;
9148   int complement_p = 0;
9149   rtx new;
9150
9151   /* Make sure and truncate the "natural" shift on the way in.  We don't
9152      want to do this inside the loop as it makes it more difficult to
9153      combine shifts.  */
9154 #ifdef SHIFT_COUNT_TRUNCATED
9155   if (SHIFT_COUNT_TRUNCATED)
9156     orig_count &= GET_MODE_BITSIZE (mode) - 1;
9157 #endif
9158
9159   /* If we were given an invalid count, don't do anything except exactly
9160      what was requested.  */
9161
9162   if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9163     {
9164       if (x)
9165         return x;
9166
9167       return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
9168     }
9169
9170   count = orig_count;
9171
9172   /* Unless one of the branches of the `if' in this loop does a `continue',
9173      we will `break' the loop after the `if'.  */
9174
9175   while (count != 0)
9176     {
9177       /* If we have an operand of (clobber (const_int 0)), just return that
9178          value.  */
9179       if (GET_CODE (varop) == CLOBBER)
9180         return varop;
9181
9182       /* If we discovered we had to complement VAROP, leave.  Making a NOT
9183          here would cause an infinite loop.  */
9184       if (complement_p)
9185         break;
9186
9187       /* Convert ROTATERT to ROTATE.  */
9188       if (code == ROTATERT)
9189         {
9190           unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9191           code = ROTATE;
9192           if (VECTOR_MODE_P (result_mode))
9193             count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9194           else
9195             count = bitsize - count;
9196         }
9197
9198       /* We need to determine what mode we will do the shift in.  If the
9199          shift is a right shift or a ROTATE, we must always do it in the mode
9200          it was originally done in.  Otherwise, we can do it in MODE, the
9201          widest mode encountered.  */
9202       shift_mode
9203         = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9204            ? result_mode : mode);
9205
9206       /* Handle cases where the count is greater than the size of the mode
9207          minus 1.  For ASHIFT, use the size minus one as the count (this can
9208          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
9209          take the count modulo the size.  For other shifts, the result is
9210          zero.
9211
9212          Since these shifts are being produced by the compiler by combining
9213          multiple operations, each of which are defined, we know what the
9214          result is supposed to be.  */
9215
9216       if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
9217         {
9218           if (code == ASHIFTRT)
9219             count = GET_MODE_BITSIZE (shift_mode) - 1;
9220           else if (code == ROTATE || code == ROTATERT)
9221             count %= GET_MODE_BITSIZE (shift_mode);
9222           else
9223             {
9224               /* We can't simply return zero because there may be an
9225                  outer op.  */
9226               varop = const0_rtx;
9227               count = 0;
9228               break;
9229             }
9230         }
9231
9232       /* An arithmetic right shift of a quantity known to be -1 or 0
9233          is a no-op.  */
9234       if (code == ASHIFTRT
9235           && (num_sign_bit_copies (varop, shift_mode)
9236               == GET_MODE_BITSIZE (shift_mode)))
9237         {
9238           count = 0;
9239           break;
9240         }
9241
9242       /* If we are doing an arithmetic right shift and discarding all but
9243          the sign bit copies, this is equivalent to doing a shift by the
9244          bitsize minus one.  Convert it into that shift because it will often
9245          allow other simplifications.  */
9246
9247       if (code == ASHIFTRT
9248           && (count + num_sign_bit_copies (varop, shift_mode)
9249               >= GET_MODE_BITSIZE (shift_mode)))
9250         count = GET_MODE_BITSIZE (shift_mode) - 1;
9251
9252       /* We simplify the tests below and elsewhere by converting
9253          ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9254          `make_compound_operation' will convert it to an ASHIFTRT for
9255          those machines (such as VAX) that don't have an LSHIFTRT.  */
9256       if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9257           && code == ASHIFTRT
9258           && ((nonzero_bits (varop, shift_mode)
9259                & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9260               == 0))
9261         code = LSHIFTRT;
9262
9263       if (code == LSHIFTRT
9264           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9265           && !(nonzero_bits (varop, shift_mode) >> count))
9266         varop = const0_rtx;
9267       if (code == ASHIFT
9268           && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9269           && !((nonzero_bits (varop, shift_mode) << count)
9270                & GET_MODE_MASK (shift_mode)))
9271         varop = const0_rtx;
9272
9273       switch (GET_CODE (varop))
9274         {
9275         case SIGN_EXTEND:
9276         case ZERO_EXTEND:
9277         case SIGN_EXTRACT:
9278         case ZERO_EXTRACT:
9279           new = expand_compound_operation (varop);
9280           if (new != varop)
9281             {
9282               varop = new;
9283               continue;
9284             }
9285           break;
9286
9287         case MEM:
9288           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9289              minus the width of a smaller mode, we can do this with a
9290              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
9291           if ((code == ASHIFTRT || code == LSHIFTRT)
9292               && ! mode_dependent_address_p (XEXP (varop, 0))
9293               && ! MEM_VOLATILE_P (varop)
9294               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9295                                          MODE_INT, 1)) != BLKmode)
9296             {
9297               new = adjust_address_nv (varop, tmode,
9298                                        BYTES_BIG_ENDIAN ? 0
9299                                        : count / BITS_PER_UNIT);
9300
9301               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9302                                      : ZERO_EXTEND, mode, new);
9303               count = 0;
9304               continue;
9305             }
9306           break;
9307
9308         case USE:
9309           /* Similar to the case above, except that we can only do this if
9310              the resulting mode is the same as that of the underlying
9311              MEM and adjust the address depending on the *bits* endianness
9312              because of the way that bit-field extract insns are defined.  */
9313           if ((code == ASHIFTRT || code == LSHIFTRT)
9314               && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9315                                          MODE_INT, 1)) != BLKmode
9316               && tmode == GET_MODE (XEXP (varop, 0)))
9317             {
9318               if (BITS_BIG_ENDIAN)
9319                 new = XEXP (varop, 0);
9320               else
9321                 {
9322                   new = copy_rtx (XEXP (varop, 0));
9323                   SUBST (XEXP (new, 0),
9324                          plus_constant (XEXP (new, 0),
9325                                         count / BITS_PER_UNIT));
9326                 }
9327
9328               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9329                                      : ZERO_EXTEND, mode, new);
9330               count = 0;
9331               continue;
9332             }
9333           break;
9334
9335         case SUBREG:
9336           /* If VAROP is a SUBREG, strip it as long as the inner operand has
9337              the same number of words as what we've seen so far.  Then store
9338              the widest mode in MODE.  */
9339           if (subreg_lowpart_p (varop)
9340               && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9341                   > GET_MODE_SIZE (GET_MODE (varop)))
9342               && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9343                                   + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9344                  == mode_words)
9345             {
9346               varop = SUBREG_REG (varop);
9347               if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9348                 mode = GET_MODE (varop);
9349               continue;
9350             }
9351           break;
9352
9353         case MULT:
9354           /* Some machines use MULT instead of ASHIFT because MULT
9355              is cheaper.  But it is still better on those machines to
9356              merge two shifts into one.  */
9357           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9358               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9359             {
9360               varop
9361                 = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
9362                               GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9363               continue;
9364             }
9365           break;
9366
9367         case UDIV:
9368           /* Similar, for when divides are cheaper.  */
9369           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9370               && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9371             {
9372               varop
9373                 = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
9374                               GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9375               continue;
9376             }
9377           break;
9378
9379         case ASHIFTRT:
9380           /* If we are extracting just the sign bit of an arithmetic
9381              right shift, that shift is not needed.  However, the sign
9382              bit of a wider mode may be different from what would be
9383              interpreted as the sign bit in a narrower mode, so, if
9384              the result is narrower, don't discard the shift.  */
9385           if (code == LSHIFTRT
9386               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9387               && (GET_MODE_BITSIZE (result_mode)
9388                   >= GET_MODE_BITSIZE (GET_MODE (varop))))
9389             {
9390               varop = XEXP (varop, 0);
9391               continue;
9392             }
9393
9394           /* ... fall through ...  */
9395
9396         case LSHIFTRT:
9397         case ASHIFT:
9398         case ROTATE:
9399           /* Here we have two nested shifts.  The result is usually the
9400              AND of a new shift with a mask.  We compute the result below.  */
9401           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9402               && INTVAL (XEXP (varop, 1)) >= 0
9403               && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9404               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9405               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9406             {
9407               enum rtx_code first_code = GET_CODE (varop);
9408               unsigned int first_count = INTVAL (XEXP (varop, 1));
9409               unsigned HOST_WIDE_INT mask;
9410               rtx mask_rtx;
9411
9412               /* We have one common special case.  We can't do any merging if
9413                  the inner code is an ASHIFTRT of a smaller mode.  However, if
9414                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9415                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9416                  we can convert it to
9417                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9418                  This simplifies certain SIGN_EXTEND operations.  */
9419               if (code == ASHIFT && first_code == ASHIFTRT
9420                   && count == (unsigned int)
9421                               (GET_MODE_BITSIZE (result_mode)
9422                                - GET_MODE_BITSIZE (GET_MODE (varop))))
9423                 {
9424                   /* C3 has the low-order C1 bits zero.  */
9425
9426                   mask = (GET_MODE_MASK (mode)
9427                           & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9428
9429                   varop = simplify_and_const_int (NULL_RTX, result_mode,
9430                                                   XEXP (varop, 0), mask);
9431                   varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9432                                                 varop, count);
9433                   count = first_count;
9434                   code = ASHIFTRT;
9435                   continue;
9436                 }
9437
9438               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9439                  than C1 high-order bits equal to the sign bit, we can convert
9440                  this to either an ASHIFT or an ASHIFTRT depending on the
9441                  two counts.
9442
9443                  We cannot do this if VAROP's mode is not SHIFT_MODE.  */
9444
9445               if (code == ASHIFTRT && first_code == ASHIFT
9446                   && GET_MODE (varop) == shift_mode
9447                   && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9448                       > first_count))
9449                 {
9450                   varop = XEXP (varop, 0);
9451
9452                   signed_count = count - first_count;
9453                   if (signed_count < 0)
9454                     count = -signed_count, code = ASHIFT;
9455                   else
9456                     count = signed_count;
9457
9458                   continue;
9459                 }
9460
9461               /* There are some cases we can't do.  If CODE is ASHIFTRT,
9462                  we can only do this if FIRST_CODE is also ASHIFTRT.
9463
9464                  We can't do the case when CODE is ROTATE and FIRST_CODE is
9465                  ASHIFTRT.
9466
9467                  If the mode of this shift is not the mode of the outer shift,
9468                  we can't do this if either shift is a right shift or ROTATE.
9469
9470                  Finally, we can't do any of these if the mode is too wide
9471                  unless the codes are the same.
9472
9473                  Handle the case where the shift codes are the same
9474                  first.  */
9475
9476               if (code == first_code)
9477                 {
9478                   if (GET_MODE (varop) != result_mode
9479                       && (code == ASHIFTRT || code == LSHIFTRT
9480                           || code == ROTATE))
9481                     break;
9482
9483                   count += first_count;
9484                   varop = XEXP (varop, 0);
9485                   continue;
9486                 }
9487
9488               if (code == ASHIFTRT
9489                   || (code == ROTATE && first_code == ASHIFTRT)
9490                   || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9491                   || (GET_MODE (varop) != result_mode
9492                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
9493                           || first_code == ROTATE
9494                           || code == ROTATE)))
9495                 break;
9496
9497               /* To compute the mask to apply after the shift, shift the
9498                  nonzero bits of the inner shift the same way the
9499                  outer shift will.  */
9500
9501               mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9502
9503               mask_rtx
9504                 = simplify_binary_operation (code, result_mode, mask_rtx,
9505                                              GEN_INT (count));
9506
9507               /* Give up if we can't compute an outer operation to use.  */
9508               if (mask_rtx == 0
9509                   || GET_CODE (mask_rtx) != CONST_INT
9510                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
9511                                         INTVAL (mask_rtx),
9512                                         result_mode, &complement_p))
9513                 break;
9514
9515               /* If the shifts are in the same direction, we add the
9516                  counts.  Otherwise, we subtract them.  */
9517               signed_count = count;
9518               if ((code == ASHIFTRT || code == LSHIFTRT)
9519                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9520                 signed_count += first_count;
9521               else
9522                 signed_count -= first_count;
9523
9524               /* If COUNT is positive, the new shift is usually CODE,
9525                  except for the two exceptions below, in which case it is
9526                  FIRST_CODE.  If the count is negative, FIRST_CODE should
9527                  always be used  */
9528               if (signed_count > 0
9529                   && ((first_code == ROTATE && code == ASHIFT)
9530                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
9531                 code = first_code, count = signed_count;
9532               else if (signed_count < 0)
9533                 code = first_code, count = -signed_count;
9534               else
9535                 count = signed_count;
9536
9537               varop = XEXP (varop, 0);
9538               continue;
9539             }
9540
9541           /* If we have (A << B << C) for any shift, we can convert this to
9542              (A << C << B).  This wins if A is a constant.  Only try this if
9543              B is not a constant.  */
9544
9545           else if (GET_CODE (varop) == code
9546                    && GET_CODE (XEXP (varop, 1)) != CONST_INT
9547                    && 0 != (new
9548                             = simplify_binary_operation (code, mode,
9549                                                          XEXP (varop, 0),
9550                                                          GEN_INT (count))))
9551             {
9552               varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9553               count = 0;
9554               continue;
9555             }
9556           break;
9557
9558         case NOT:
9559           /* Make this fit the case below.  */
9560           varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9561                                GEN_INT (GET_MODE_MASK (mode)));
9562           continue;
9563
9564         case IOR:
9565         case AND:
9566         case XOR:
9567           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9568              with C the size of VAROP - 1 and the shift is logical if
9569              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9570              we have an (le X 0) operation.   If we have an arithmetic shift
9571              and STORE_FLAG_VALUE is 1 or we have a logical shift with
9572              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
9573
9574           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9575               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9576               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9577               && (code == LSHIFTRT || code == ASHIFTRT)
9578               && count == (unsigned int)
9579                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9580               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9581             {
9582               count = 0;
9583               varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9584                                   const0_rtx);
9585
9586               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9587                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9588
9589               continue;
9590             }
9591
9592           /* If we have (shift (logical)), move the logical to the outside
9593              to allow it to possibly combine with another logical and the
9594              shift to combine with another shift.  This also canonicalizes to
9595              what a ZERO_EXTRACT looks like.  Also, some machines have
9596              (and (shift)) insns.  */
9597
9598           if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9599               && (new = simplify_binary_operation (code, result_mode,
9600                                                    XEXP (varop, 1),
9601                                                    GEN_INT (count))) != 0
9602               && GET_CODE (new) == CONST_INT
9603               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9604                                   INTVAL (new), result_mode, &complement_p))
9605             {
9606               varop = XEXP (varop, 0);
9607               continue;
9608             }
9609
9610           /* If we can't do that, try to simplify the shift in each arm of the
9611              logical expression, make a new logical expression, and apply
9612              the inverse distributive law.  */
9613           {
9614             rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9615                                             XEXP (varop, 0), count);
9616             rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9617                                             XEXP (varop, 1), count);
9618
9619             varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
9620             varop = apply_distributive_law (varop);
9621
9622             count = 0;
9623           }
9624           break;
9625
9626         case EQ:
9627           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9628              says that the sign bit can be tested, FOO has mode MODE, C is
9629              GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9630              that may be nonzero.  */
9631           if (code == LSHIFTRT
9632               && XEXP (varop, 1) == const0_rtx
9633               && GET_MODE (XEXP (varop, 0)) == result_mode
9634               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9635               && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9636               && ((STORE_FLAG_VALUE
9637                    & ((HOST_WIDE_INT) 1
9638                       < (GET_MODE_BITSIZE (result_mode) - 1))))
9639               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9640               && merge_outer_ops (&outer_op, &outer_const, XOR,
9641                                   (HOST_WIDE_INT) 1, result_mode,
9642                                   &complement_p))
9643             {
9644               varop = XEXP (varop, 0);
9645               count = 0;
9646               continue;
9647             }
9648           break;
9649
9650         case NEG:
9651           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9652              than the number of bits in the mode is equivalent to A.  */
9653           if (code == LSHIFTRT
9654               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9655               && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9656             {
9657               varop = XEXP (varop, 0);
9658               count = 0;
9659               continue;
9660             }
9661
9662           /* NEG commutes with ASHIFT since it is multiplication.  Move the
9663              NEG outside to allow shifts to combine.  */
9664           if (code == ASHIFT
9665               && merge_outer_ops (&outer_op, &outer_const, NEG,
9666                                   (HOST_WIDE_INT) 0, result_mode,
9667                                   &complement_p))
9668             {
9669               varop = XEXP (varop, 0);
9670               continue;
9671             }
9672           break;
9673
9674         case PLUS:
9675           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9676              is one less than the number of bits in the mode is
9677              equivalent to (xor A 1).  */
9678           if (code == LSHIFTRT
9679               && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9680               && XEXP (varop, 1) == constm1_rtx
9681               && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9682               && merge_outer_ops (&outer_op, &outer_const, XOR,
9683                                   (HOST_WIDE_INT) 1, result_mode,
9684                                   &complement_p))
9685             {
9686               count = 0;
9687               varop = XEXP (varop, 0);
9688               continue;
9689             }
9690
9691           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9692              that might be nonzero in BAR are those being shifted out and those
9693              bits are known zero in FOO, we can replace the PLUS with FOO.
9694              Similarly in the other operand order.  This code occurs when
9695              we are computing the size of a variable-size array.  */
9696
9697           if ((code == ASHIFTRT || code == LSHIFTRT)
9698               && count < HOST_BITS_PER_WIDE_INT
9699               && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9700               && (nonzero_bits (XEXP (varop, 1), result_mode)
9701                   & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9702             {
9703               varop = XEXP (varop, 0);
9704               continue;
9705             }
9706           else if ((code == ASHIFTRT || code == LSHIFTRT)
9707                    && count < HOST_BITS_PER_WIDE_INT
9708                    && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9709                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9710                             >> count)
9711                    && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9712                             & nonzero_bits (XEXP (varop, 1),
9713                                                  result_mode)))
9714             {
9715               varop = XEXP (varop, 1);
9716               continue;
9717             }
9718
9719           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
9720           if (code == ASHIFT
9721               && GET_CODE (XEXP (varop, 1)) == CONST_INT
9722               && (new = simplify_binary_operation (ASHIFT, result_mode,
9723                                                    XEXP (varop, 1),
9724                                                    GEN_INT (count))) != 0
9725               && GET_CODE (new) == CONST_INT
9726               && merge_outer_ops (&outer_op, &outer_const, PLUS,
9727                                   INTVAL (new), result_mode, &complement_p))
9728             {
9729               varop = XEXP (varop, 0);
9730               continue;
9731             }
9732           break;
9733
9734         case MINUS:
9735           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9736              with C the size of VAROP - 1 and the shift is logical if
9737              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9738              we have a (gt X 0) operation.  If the shift is arithmetic with
9739              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9740              we have a (neg (gt X 0)) operation.  */
9741
9742           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9743               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9744               && count == (unsigned int)
9745                           (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9746               && (code == LSHIFTRT || code == ASHIFTRT)
9747               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9748               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9749                  == count
9750               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9751             {
9752               count = 0;
9753               varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9754                                   const0_rtx);
9755
9756               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9757                 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9758
9759               continue;
9760             }
9761           break;
9762
9763         case TRUNCATE:
9764           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9765              if the truncate does not affect the value.  */
9766           if (code == LSHIFTRT
9767               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9768               && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9769               && (INTVAL (XEXP (XEXP (varop, 0), 1))
9770                   >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9771                       - GET_MODE_BITSIZE (GET_MODE (varop)))))
9772             {
9773               rtx varop_inner = XEXP (varop, 0);
9774
9775               varop_inner
9776                 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9777                                     XEXP (varop_inner, 0),
9778                                     GEN_INT
9779                                     (count + INTVAL (XEXP (varop_inner, 1))));
9780               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9781               count = 0;
9782               continue;
9783             }
9784           break;
9785
9786         default:
9787           break;
9788         }
9789
9790       break;
9791     }
9792
9793   /* We need to determine what mode to do the shift in.  If the shift is
9794      a right shift or ROTATE, we must always do it in the mode it was
9795      originally done in.  Otherwise, we can do it in MODE, the widest mode
9796      encountered.  The code we care about is that of the shift that will
9797      actually be done, not the shift that was originally requested.  */
9798   shift_mode
9799     = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9800        ? result_mode : mode);
9801
9802   /* We have now finished analyzing the shift.  The result should be
9803      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
9804      OUTER_OP is non-NIL, it is an operation that needs to be applied
9805      to the result of the shift.  OUTER_CONST is the relevant constant,
9806      but we must turn off all bits turned off in the shift.
9807
9808      If we were passed a value for X, see if we can use any pieces of
9809      it.  If not, make new rtx.  */
9810
9811   if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9812       && GET_CODE (XEXP (x, 1)) == CONST_INT
9813       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9814     const_rtx = XEXP (x, 1);
9815   else
9816     const_rtx = GEN_INT (count);
9817
9818   if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9819       && GET_MODE (XEXP (x, 0)) == shift_mode
9820       && SUBREG_REG (XEXP (x, 0)) == varop)
9821     varop = XEXP (x, 0);
9822   else if (GET_MODE (varop) != shift_mode)
9823     varop = gen_lowpart_for_combine (shift_mode, varop);
9824
9825   /* If we can't make the SUBREG, try to return what we were given.  */
9826   if (GET_CODE (varop) == CLOBBER)
9827     return x ? x : varop;
9828
9829   new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9830   if (new != 0)
9831     x = new;
9832   else
9833     x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9834
9835   /* If we have an outer operation and we just made a shift, it is
9836      possible that we could have simplified the shift were it not
9837      for the outer operation.  So try to do the simplification
9838      recursively.  */
9839
9840   if (outer_op != NIL && GET_CODE (x) == code
9841       && GET_CODE (XEXP (x, 1)) == CONST_INT)
9842     x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9843                               INTVAL (XEXP (x, 1)));
9844
9845   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9846      turn off all the bits that the shift would have turned off.  */
9847   if (orig_code == LSHIFTRT && result_mode != shift_mode)
9848     x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9849                                 GET_MODE_MASK (result_mode) >> orig_count);
9850
9851   /* Do the remainder of the processing in RESULT_MODE.  */
9852   x = gen_lowpart_for_combine (result_mode, x);
9853
9854   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9855      operation.  */
9856   if (complement_p)
9857     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9858
9859   if (outer_op != NIL)
9860     {
9861       if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9862         outer_const = trunc_int_for_mode (outer_const, result_mode);
9863
9864       if (outer_op == AND)
9865         x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9866       else if (outer_op == SET)
9867         /* This means that we have determined that the result is
9868            equivalent to a constant.  This should be rare.  */
9869         x = GEN_INT (outer_const);
9870       else if (GET_RTX_CLASS (outer_op) == '1')
9871         x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9872       else
9873         x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9874     }
9875
9876   return x;
9877 }
9878 \f
9879 /* Like recog, but we receive the address of a pointer to a new pattern.
9880    We try to match the rtx that the pointer points to.
9881    If that fails, we may try to modify or replace the pattern,
9882    storing the replacement into the same pointer object.
9883
9884    Modifications include deletion or addition of CLOBBERs.
9885
9886    PNOTES is a pointer to a location where any REG_UNUSED notes added for
9887    the CLOBBERs are placed.
9888
9889    The value is the final insn code from the pattern ultimately matched,
9890    or -1.  */
9891
9892 static int
9893 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9894 {
9895   rtx pat = *pnewpat;
9896   int insn_code_number;
9897   int num_clobbers_to_add = 0;
9898   int i;
9899   rtx notes = 0;
9900   rtx dummy_insn;
9901
9902   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9903      we use to indicate that something didn't match.  If we find such a
9904      thing, force rejection.  */
9905   if (GET_CODE (pat) == PARALLEL)
9906     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9907       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9908           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9909         return -1;
9910
9911   /* *pnewpat does not have to be actual PATTERN (insn), so make a dummy
9912      instruction for pattern recognition.  */
9913   dummy_insn = shallow_copy_rtx (insn);
9914   PATTERN (dummy_insn) = pat;
9915   REG_NOTES (dummy_insn) = 0;
9916
9917   insn_code_number = recog (pat, dummy_insn, &num_clobbers_to_add);
9918
9919   /* If it isn't, there is the possibility that we previously had an insn
9920      that clobbered some register as a side effect, but the combined
9921      insn doesn't need to do that.  So try once more without the clobbers
9922      unless this represents an ASM insn.  */
9923
9924   if (insn_code_number < 0 && ! check_asm_operands (pat)
9925       && GET_CODE (pat) == PARALLEL)
9926     {
9927       int pos;
9928
9929       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9930         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9931           {
9932             if (i != pos)
9933               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9934             pos++;
9935           }
9936
9937       SUBST_INT (XVECLEN (pat, 0), pos);
9938
9939       if (pos == 1)
9940         pat = XVECEXP (pat, 0, 0);
9941
9942       PATTERN (dummy_insn) = pat;
9943       insn_code_number = recog (pat, dummy_insn, &num_clobbers_to_add);
9944     }
9945
9946   /* Recognize all noop sets, these will be killed by followup pass.  */
9947   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9948     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9949
9950   /* If we had any clobbers to add, make a new pattern than contains
9951      them.  Then check to make sure that all of them are dead.  */
9952   if (num_clobbers_to_add)
9953     {
9954       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9955                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
9956                                                   ? (XVECLEN (pat, 0)
9957                                                      + num_clobbers_to_add)
9958                                                   : num_clobbers_to_add + 1));
9959
9960       if (GET_CODE (pat) == PARALLEL)
9961         for (i = 0; i < XVECLEN (pat, 0); i++)
9962           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9963       else
9964         XVECEXP (newpat, 0, 0) = pat;
9965
9966       add_clobbers (newpat, insn_code_number);
9967
9968       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9969            i < XVECLEN (newpat, 0); i++)
9970         {
9971           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9972               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9973             return -1;
9974           notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9975                                      XEXP (XVECEXP (newpat, 0, i), 0), notes);
9976         }
9977       pat = newpat;
9978     }
9979
9980   *pnewpat = pat;
9981   *pnotes = notes;
9982
9983   return insn_code_number;
9984 }
9985 \f
9986 /* Like gen_lowpart but for use by combine.  In combine it is not possible
9987    to create any new pseudoregs.  However, it is safe to create
9988    invalid memory addresses, because combine will try to recognize
9989    them and all they will do is make the combine attempt fail.
9990
9991    If for some reason this cannot do its job, an rtx
9992    (clobber (const_int 0)) is returned.
9993    An insn containing that will not be recognized.  */
9994
9995 #undef gen_lowpart
9996
9997 static rtx
9998 gen_lowpart_for_combine (enum machine_mode mode, rtx x)
9999 {
10000   rtx result;
10001
10002   if (GET_MODE (x) == mode)
10003     return x;
10004
10005   /* Return identity if this is a CONST or symbolic
10006      reference.  */
10007   if (mode == Pmode
10008       && (GET_CODE (x) == CONST
10009           || GET_CODE (x) == SYMBOL_REF
10010           || GET_CODE (x) == LABEL_REF))
10011     return x;
10012
10013   /* We can only support MODE being wider than a word if X is a
10014      constant integer or has a mode the same size.  */
10015
10016   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
10017       && ! ((GET_MODE (x) == VOIDmode
10018              && (GET_CODE (x) == CONST_INT
10019                  || GET_CODE (x) == CONST_DOUBLE))
10020             || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
10021     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10022
10023   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
10024      won't know what to do.  So we will strip off the SUBREG here and
10025      process normally.  */
10026   if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
10027     {
10028       x = SUBREG_REG (x);
10029       if (GET_MODE (x) == mode)
10030         return x;
10031     }
10032
10033   result = gen_lowpart_common (mode, x);
10034 #ifdef CANNOT_CHANGE_MODE_CLASS
10035   if (result != 0
10036       && GET_CODE (result) == SUBREG
10037       && GET_CODE (SUBREG_REG (result)) == REG
10038       && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER)
10039     bitmap_set_bit (&subregs_of_mode, REGNO (SUBREG_REG (result))
10040                                       * MAX_MACHINE_MODE
10041                                       + GET_MODE (result));
10042 #endif
10043
10044   if (result)
10045     return result;
10046
10047   if (GET_CODE (x) == MEM)
10048     {
10049       int offset = 0;
10050
10051       /* Refuse to work on a volatile memory ref or one with a mode-dependent
10052          address.  */
10053       if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10054         return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10055
10056       /* If we want to refer to something bigger than the original memref,
10057          generate a perverse subreg instead.  That will force a reload
10058          of the original memref X.  */
10059       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
10060         return gen_rtx_SUBREG (mode, x, 0);
10061
10062       if (WORDS_BIG_ENDIAN)
10063         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
10064                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
10065
10066       if (BYTES_BIG_ENDIAN)
10067         {
10068           /* Adjust the address so that the address-after-the-data is
10069              unchanged.  */
10070           offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
10071                      - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
10072         }
10073
10074       return adjust_address_nv (x, mode, offset);
10075     }
10076
10077   /* If X is a comparison operator, rewrite it in a new mode.  This
10078      probably won't match, but may allow further simplifications.  */
10079   else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
10080     return gen_rtx_fmt_ee (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
10081
10082   /* If we couldn't simplify X any other way, just enclose it in a
10083      SUBREG.  Normally, this SUBREG won't match, but some patterns may
10084      include an explicit SUBREG or we may simplify it further in combine.  */
10085   else
10086     {
10087       int offset = 0;
10088       rtx res;
10089       enum machine_mode sub_mode = GET_MODE (x);
10090
10091       offset = subreg_lowpart_offset (mode, sub_mode);
10092       if (sub_mode == VOIDmode)
10093         {
10094           sub_mode = int_mode_for_mode (mode);
10095           x = gen_lowpart_common (sub_mode, x);
10096           if (x == 0)
10097             return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
10098         }
10099       res = simplify_gen_subreg (mode, x, sub_mode, offset);
10100       if (res)
10101         return res;
10102       return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10103     }
10104 }
10105 \f
10106 /* These routines make binary and unary operations by first seeing if they
10107    fold; if not, a new expression is allocated.  */
10108
10109 static rtx
10110 gen_binary (enum rtx_code code, enum machine_mode mode, rtx op0, rtx op1)
10111 {
10112   rtx result;
10113   rtx tem;
10114
10115   if (GET_CODE (op0) == CLOBBER)
10116     return op0;
10117   else if (GET_CODE (op1) == CLOBBER)
10118     return op1;
10119   
10120   if (GET_RTX_CLASS (code) == 'c'
10121       && swap_commutative_operands_p (op0, op1))
10122     tem = op0, op0 = op1, op1 = tem;
10123
10124   if (GET_RTX_CLASS (code) == '<')
10125     {
10126       enum machine_mode op_mode = GET_MODE (op0);
10127
10128       /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
10129          just (REL_OP X Y).  */
10130       if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
10131         {
10132           op1 = XEXP (op0, 1);
10133           op0 = XEXP (op0, 0);
10134           op_mode = GET_MODE (op0);
10135         }
10136
10137       if (op_mode == VOIDmode)
10138         op_mode = GET_MODE (op1);
10139       result = simplify_relational_operation (code, op_mode, op0, op1);
10140     }
10141   else
10142     result = simplify_binary_operation (code, mode, op0, op1);
10143
10144   if (result)
10145     return result;
10146
10147   /* Put complex operands first and constants second.  */
10148   if (GET_RTX_CLASS (code) == 'c'
10149       && swap_commutative_operands_p (op0, op1))
10150     return gen_rtx_fmt_ee (code, mode, op1, op0);
10151
10152   /* If we are turning off bits already known off in OP0, we need not do
10153      an AND.  */
10154   else if (code == AND && GET_CODE (op1) == CONST_INT
10155            && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10156            && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
10157     return op0;
10158
10159   return gen_rtx_fmt_ee (code, mode, op0, op1);
10160 }
10161 \f
10162 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10163    comparison code that will be tested.
10164
10165    The result is a possibly different comparison code to use.  *POP0 and
10166    *POP1 may be updated.
10167
10168    It is possible that we might detect that a comparison is either always
10169    true or always false.  However, we do not perform general constant
10170    folding in combine, so this knowledge isn't useful.  Such tautologies
10171    should have been detected earlier.  Hence we ignore all such cases.  */
10172
10173 static enum rtx_code
10174 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10175 {
10176   rtx op0 = *pop0;
10177   rtx op1 = *pop1;
10178   rtx tem, tem1;
10179   int i;
10180   enum machine_mode mode, tmode;
10181
10182   /* Try a few ways of applying the same transformation to both operands.  */
10183   while (1)
10184     {
10185 #ifndef WORD_REGISTER_OPERATIONS
10186       /* The test below this one won't handle SIGN_EXTENDs on these machines,
10187          so check specially.  */
10188       if (code != GTU && code != GEU && code != LTU && code != LEU
10189           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10190           && GET_CODE (XEXP (op0, 0)) == ASHIFT
10191           && GET_CODE (XEXP (op1, 0)) == ASHIFT
10192           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10193           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10194           && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10195               == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10196           && GET_CODE (XEXP (op0, 1)) == CONST_INT
10197           && XEXP (op0, 1) == XEXP (op1, 1)
10198           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10199           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10200           && (INTVAL (XEXP (op0, 1))
10201               == (GET_MODE_BITSIZE (GET_MODE (op0))
10202                   - (GET_MODE_BITSIZE
10203                      (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10204         {
10205           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10206           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10207         }
10208 #endif
10209
10210       /* If both operands are the same constant shift, see if we can ignore the
10211          shift.  We can if the shift is a rotate or if the bits shifted out of
10212          this shift are known to be zero for both inputs and if the type of
10213          comparison is compatible with the shift.  */
10214       if (GET_CODE (op0) == GET_CODE (op1)
10215           && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10216           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10217               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10218                   && (code != GT && code != LT && code != GE && code != LE))
10219               || (GET_CODE (op0) == ASHIFTRT
10220                   && (code != GTU && code != LTU
10221                       && code != GEU && code != LEU)))
10222           && GET_CODE (XEXP (op0, 1)) == CONST_INT
10223           && INTVAL (XEXP (op0, 1)) >= 0
10224           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10225           && XEXP (op0, 1) == XEXP (op1, 1))
10226         {
10227           enum machine_mode mode = GET_MODE (op0);
10228           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10229           int shift_count = INTVAL (XEXP (op0, 1));
10230
10231           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10232             mask &= (mask >> shift_count) << shift_count;
10233           else if (GET_CODE (op0) == ASHIFT)
10234             mask = (mask & (mask << shift_count)) >> shift_count;
10235
10236           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10237               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10238             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10239           else
10240             break;
10241         }
10242
10243       /* If both operands are AND's of a paradoxical SUBREG by constant, the
10244          SUBREGs are of the same mode, and, in both cases, the AND would
10245          be redundant if the comparison was done in the narrower mode,
10246          do the comparison in the narrower mode (e.g., we are AND'ing with 1
10247          and the operand's possibly nonzero bits are 0xffffff01; in that case
10248          if we only care about QImode, we don't need the AND).  This case
10249          occurs if the output mode of an scc insn is not SImode and
10250          STORE_FLAG_VALUE == 1 (e.g., the 386).
10251
10252          Similarly, check for a case where the AND's are ZERO_EXTEND
10253          operations from some narrower mode even though a SUBREG is not
10254          present.  */
10255
10256       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10257                && GET_CODE (XEXP (op0, 1)) == CONST_INT
10258                && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10259         {
10260           rtx inner_op0 = XEXP (op0, 0);
10261           rtx inner_op1 = XEXP (op1, 0);
10262           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10263           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10264           int changed = 0;
10265
10266           if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10267               && (GET_MODE_SIZE (GET_MODE (inner_op0))
10268                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10269               && (GET_MODE (SUBREG_REG (inner_op0))
10270                   == GET_MODE (SUBREG_REG (inner_op1)))
10271               && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10272                   <= HOST_BITS_PER_WIDE_INT)
10273               && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10274                                              GET_MODE (SUBREG_REG (inner_op0)))))
10275               && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10276                                              GET_MODE (SUBREG_REG (inner_op1))))))
10277             {
10278               op0 = SUBREG_REG (inner_op0);
10279               op1 = SUBREG_REG (inner_op1);
10280
10281               /* The resulting comparison is always unsigned since we masked
10282                  off the original sign bit.  */
10283               code = unsigned_condition (code);
10284
10285               changed = 1;
10286             }
10287
10288           else if (c0 == c1)
10289             for (tmode = GET_CLASS_NARROWEST_MODE
10290                  (GET_MODE_CLASS (GET_MODE (op0)));
10291                  tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10292               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10293                 {
10294                   op0 = gen_lowpart_for_combine (tmode, inner_op0);
10295                   op1 = gen_lowpart_for_combine (tmode, inner_op1);
10296                   code = unsigned_condition (code);
10297                   changed = 1;
10298                   break;
10299                 }
10300
10301           if (! changed)
10302             break;
10303         }
10304
10305       /* If both operands are NOT, we can strip off the outer operation
10306          and adjust the comparison code for swapped operands; similarly for
10307          NEG, except that this must be an equality comparison.  */
10308       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10309                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10310                    && (code == EQ || code == NE)))
10311         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10312
10313       else
10314         break;
10315     }
10316
10317   /* If the first operand is a constant, swap the operands and adjust the
10318      comparison code appropriately, but don't do this if the second operand
10319      is already a constant integer.  */
10320   if (swap_commutative_operands_p (op0, op1))
10321     {
10322       tem = op0, op0 = op1, op1 = tem;
10323       code = swap_condition (code);
10324     }
10325
10326   /* We now enter a loop during which we will try to simplify the comparison.
10327      For the most part, we only are concerned with comparisons with zero,
10328      but some things may really be comparisons with zero but not start
10329      out looking that way.  */
10330
10331   while (GET_CODE (op1) == CONST_INT)
10332     {
10333       enum machine_mode mode = GET_MODE (op0);
10334       unsigned int mode_width = GET_MODE_BITSIZE (mode);
10335       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10336       int equality_comparison_p;
10337       int sign_bit_comparison_p;
10338       int unsigned_comparison_p;
10339       HOST_WIDE_INT const_op;
10340
10341       /* We only want to handle integral modes.  This catches VOIDmode,
10342          CCmode, and the floating-point modes.  An exception is that we
10343          can handle VOIDmode if OP0 is a COMPARE or a comparison
10344          operation.  */
10345
10346       if (GET_MODE_CLASS (mode) != MODE_INT
10347           && ! (mode == VOIDmode
10348                 && (GET_CODE (op0) == COMPARE
10349                     || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
10350         break;
10351
10352       /* Get the constant we are comparing against and turn off all bits
10353          not on in our mode.  */
10354       const_op = INTVAL (op1);
10355       if (mode != VOIDmode)
10356         const_op = trunc_int_for_mode (const_op, mode);
10357       op1 = GEN_INT (const_op);
10358
10359       /* If we are comparing against a constant power of two and the value
10360          being compared can only have that single bit nonzero (e.g., it was
10361          `and'ed with that bit), we can replace this with a comparison
10362          with zero.  */
10363       if (const_op
10364           && (code == EQ || code == NE || code == GE || code == GEU
10365               || code == LT || code == LTU)
10366           && mode_width <= HOST_BITS_PER_WIDE_INT
10367           && exact_log2 (const_op) >= 0
10368           && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10369         {
10370           code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10371           op1 = const0_rtx, const_op = 0;
10372         }
10373
10374       /* Similarly, if we are comparing a value known to be either -1 or
10375          0 with -1, change it to the opposite comparison against zero.  */
10376
10377       if (const_op == -1
10378           && (code == EQ || code == NE || code == GT || code == LE
10379               || code == GEU || code == LTU)
10380           && num_sign_bit_copies (op0, mode) == mode_width)
10381         {
10382           code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10383           op1 = const0_rtx, const_op = 0;
10384         }
10385
10386       /* Do some canonicalizations based on the comparison code.  We prefer
10387          comparisons against zero and then prefer equality comparisons.
10388          If we can reduce the size of a constant, we will do that too.  */
10389
10390       switch (code)
10391         {
10392         case LT:
10393           /* < C is equivalent to <= (C - 1) */
10394           if (const_op > 0)
10395             {
10396               const_op -= 1;
10397               op1 = GEN_INT (const_op);
10398               code = LE;
10399               /* ... fall through to LE case below.  */
10400             }
10401           else
10402             break;
10403
10404         case LE:
10405           /* <= C is equivalent to < (C + 1); we do this for C < 0  */
10406           if (const_op < 0)
10407             {
10408               const_op += 1;
10409               op1 = GEN_INT (const_op);
10410               code = LT;
10411             }
10412
10413           /* If we are doing a <= 0 comparison on a value known to have
10414              a zero sign bit, we can replace this with == 0.  */
10415           else if (const_op == 0
10416                    && mode_width <= HOST_BITS_PER_WIDE_INT
10417                    && (nonzero_bits (op0, mode)
10418                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10419             code = EQ;
10420           break;
10421
10422         case GE:
10423           /* >= C is equivalent to > (C - 1).  */
10424           if (const_op > 0)
10425             {
10426               const_op -= 1;
10427               op1 = GEN_INT (const_op);
10428               code = GT;
10429               /* ... fall through to GT below.  */
10430             }
10431           else
10432             break;
10433
10434         case GT:
10435           /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
10436           if (const_op < 0)
10437             {
10438               const_op += 1;
10439               op1 = GEN_INT (const_op);
10440               code = GE;
10441             }
10442
10443           /* If we are doing a > 0 comparison on a value known to have
10444              a zero sign bit, we can replace this with != 0.  */
10445           else if (const_op == 0
10446                    && mode_width <= HOST_BITS_PER_WIDE_INT
10447                    && (nonzero_bits (op0, mode)
10448                        & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10449             code = NE;
10450           break;
10451
10452         case LTU:
10453           /* < C is equivalent to <= (C - 1).  */
10454           if (const_op > 0)
10455             {
10456               const_op -= 1;
10457               op1 = GEN_INT (const_op);
10458               code = LEU;
10459               /* ... fall through ...  */
10460             }
10461
10462           /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
10463           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10464                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10465             {
10466               const_op = 0, op1 = const0_rtx;
10467               code = GE;
10468               break;
10469             }
10470           else
10471             break;
10472
10473         case LEU:
10474           /* unsigned <= 0 is equivalent to == 0 */
10475           if (const_op == 0)
10476             code = EQ;
10477
10478           /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
10479           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10480                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10481             {
10482               const_op = 0, op1 = const0_rtx;
10483               code = GE;
10484             }
10485           break;
10486
10487         case GEU:
10488           /* >= C is equivalent to < (C - 1).  */
10489           if (const_op > 1)
10490             {
10491               const_op -= 1;
10492               op1 = GEN_INT (const_op);
10493               code = GTU;
10494               /* ... fall through ...  */
10495             }
10496
10497           /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
10498           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10499                    && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10500             {
10501               const_op = 0, op1 = const0_rtx;
10502               code = LT;
10503               break;
10504             }
10505           else
10506             break;
10507
10508         case GTU:
10509           /* unsigned > 0 is equivalent to != 0 */
10510           if (const_op == 0)
10511             code = NE;
10512
10513           /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
10514           else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10515                    && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10516             {
10517               const_op = 0, op1 = const0_rtx;
10518               code = LT;
10519             }
10520           break;
10521
10522         default:
10523           break;
10524         }
10525
10526       /* Compute some predicates to simplify code below.  */
10527
10528       equality_comparison_p = (code == EQ || code == NE);
10529       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10530       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10531                                || code == GEU);
10532
10533       /* If this is a sign bit comparison and we can do arithmetic in
10534          MODE, say that we will only be needing the sign bit of OP0.  */
10535       if (sign_bit_comparison_p
10536           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10537         op0 = force_to_mode (op0, mode,
10538                              ((HOST_WIDE_INT) 1
10539                               << (GET_MODE_BITSIZE (mode) - 1)),
10540                              NULL_RTX, 0);
10541
10542       /* Now try cases based on the opcode of OP0.  If none of the cases
10543          does a "continue", we exit this loop immediately after the
10544          switch.  */
10545
10546       switch (GET_CODE (op0))
10547         {
10548         case ZERO_EXTRACT:
10549           /* If we are extracting a single bit from a variable position in
10550              a constant that has only a single bit set and are comparing it
10551              with zero, we can convert this into an equality comparison
10552              between the position and the location of the single bit.  */
10553
10554           if (GET_CODE (XEXP (op0, 0)) == CONST_INT
10555               && XEXP (op0, 1) == const1_rtx
10556               && equality_comparison_p && const_op == 0
10557               && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10558             {
10559               if (BITS_BIG_ENDIAN)
10560                 {
10561                   enum machine_mode new_mode
10562                     = mode_for_extraction (EP_extzv, 1);
10563                   if (new_mode == MAX_MACHINE_MODE)
10564                     i = BITS_PER_WORD - 1 - i;
10565                   else
10566                     {
10567                       mode = new_mode;
10568                       i = (GET_MODE_BITSIZE (mode) - 1 - i);
10569                     }
10570                 }
10571
10572               op0 = XEXP (op0, 2);
10573               op1 = GEN_INT (i);
10574               const_op = i;
10575
10576               /* Result is nonzero iff shift count is equal to I.  */
10577               code = reverse_condition (code);
10578               continue;
10579             }
10580
10581           /* ... fall through ...  */
10582
10583         case SIGN_EXTRACT:
10584           tem = expand_compound_operation (op0);
10585           if (tem != op0)
10586             {
10587               op0 = tem;
10588               continue;
10589             }
10590           break;
10591
10592         case NOT:
10593           /* If testing for equality, we can take the NOT of the constant.  */
10594           if (equality_comparison_p
10595               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10596             {
10597               op0 = XEXP (op0, 0);
10598               op1 = tem;
10599               continue;
10600             }
10601
10602           /* If just looking at the sign bit, reverse the sense of the
10603              comparison.  */
10604           if (sign_bit_comparison_p)
10605             {
10606               op0 = XEXP (op0, 0);
10607               code = (code == GE ? LT : GE);
10608               continue;
10609             }
10610           break;
10611
10612         case NEG:
10613           /* If testing for equality, we can take the NEG of the constant.  */
10614           if (equality_comparison_p
10615               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10616             {
10617               op0 = XEXP (op0, 0);
10618               op1 = tem;
10619               continue;
10620             }
10621
10622           /* The remaining cases only apply to comparisons with zero.  */
10623           if (const_op != 0)
10624             break;
10625
10626           /* When X is ABS or is known positive,
10627              (neg X) is < 0 if and only if X != 0.  */
10628
10629           if (sign_bit_comparison_p
10630               && (GET_CODE (XEXP (op0, 0)) == ABS
10631                   || (mode_width <= HOST_BITS_PER_WIDE_INT
10632                       && (nonzero_bits (XEXP (op0, 0), mode)
10633                           & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10634             {
10635               op0 = XEXP (op0, 0);
10636               code = (code == LT ? NE : EQ);
10637               continue;
10638             }
10639
10640           /* If we have NEG of something whose two high-order bits are the
10641              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
10642           if (num_sign_bit_copies (op0, mode) >= 2)
10643             {
10644               op0 = XEXP (op0, 0);
10645               code = swap_condition (code);
10646               continue;
10647             }
10648           break;
10649
10650         case ROTATE:
10651           /* If we are testing equality and our count is a constant, we
10652              can perform the inverse operation on our RHS.  */
10653           if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10654               && (tem = simplify_binary_operation (ROTATERT, mode,
10655                                                    op1, XEXP (op0, 1))) != 0)
10656             {
10657               op0 = XEXP (op0, 0);
10658               op1 = tem;
10659               continue;
10660             }
10661
10662           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10663              a particular bit.  Convert it to an AND of a constant of that
10664              bit.  This will be converted into a ZERO_EXTRACT.  */
10665           if (const_op == 0 && sign_bit_comparison_p
10666               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10667               && mode_width <= HOST_BITS_PER_WIDE_INT)
10668             {
10669               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10670                                             ((HOST_WIDE_INT) 1
10671                                              << (mode_width - 1
10672                                                  - INTVAL (XEXP (op0, 1)))));
10673               code = (code == LT ? NE : EQ);
10674               continue;
10675             }
10676
10677           /* Fall through.  */
10678
10679         case ABS:
10680           /* ABS is ignorable inside an equality comparison with zero.  */
10681           if (const_op == 0 && equality_comparison_p)
10682             {
10683               op0 = XEXP (op0, 0);
10684               continue;
10685             }
10686           break;
10687
10688         case SIGN_EXTEND:
10689           /* Can simplify (compare (zero/sign_extend FOO) CONST)
10690              to (compare FOO CONST) if CONST fits in FOO's mode and we
10691              are either testing inequality or have an unsigned comparison
10692              with ZERO_EXTEND or a signed comparison with SIGN_EXTEND.  */
10693           if (! unsigned_comparison_p
10694               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10695                   <= HOST_BITS_PER_WIDE_INT)
10696               && ((unsigned HOST_WIDE_INT) const_op
10697                   < (((unsigned HOST_WIDE_INT) 1
10698                       << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10699             {
10700               op0 = XEXP (op0, 0);
10701               continue;
10702             }
10703           break;
10704
10705         case SUBREG:
10706           /* Check for the case where we are comparing A - C1 with C2,
10707              both constants are smaller than 1/2 the maximum positive
10708              value in MODE, and the comparison is equality or unsigned.
10709              In that case, if A is either zero-extended to MODE or has
10710              sufficient sign bits so that the high-order bit in MODE
10711              is a copy of the sign in the inner mode, we can prove that it is
10712              safe to do the operation in the wider mode.  This simplifies
10713              many range checks.  */
10714
10715           if (mode_width <= HOST_BITS_PER_WIDE_INT
10716               && subreg_lowpart_p (op0)
10717               && GET_CODE (SUBREG_REG (op0)) == PLUS
10718               && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10719               && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10720               && (-INTVAL (XEXP (SUBREG_REG (op0), 1))
10721                   < (HOST_WIDE_INT) (GET_MODE_MASK (mode) / 2))
10722               && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10723               && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10724                                       GET_MODE (SUBREG_REG (op0)))
10725                         & ~GET_MODE_MASK (mode))
10726                   || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10727                                            GET_MODE (SUBREG_REG (op0)))
10728                       > (unsigned int)
10729                         (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10730                          - GET_MODE_BITSIZE (mode)))))
10731             {
10732               op0 = SUBREG_REG (op0);
10733               continue;
10734             }
10735
10736           /* If the inner mode is narrower and we are extracting the low part,
10737              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
10738           if (subreg_lowpart_p (op0)
10739               && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10740             /* Fall through */ ;
10741           else
10742             break;
10743
10744           /* ... fall through ...  */
10745
10746         case ZERO_EXTEND:
10747           if ((unsigned_comparison_p || equality_comparison_p)
10748               && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10749                   <= HOST_BITS_PER_WIDE_INT)
10750               && ((unsigned HOST_WIDE_INT) const_op
10751                   < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10752             {
10753               op0 = XEXP (op0, 0);
10754               continue;
10755             }
10756           break;
10757
10758         case PLUS:
10759           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
10760              this for equality comparisons due to pathological cases involving
10761              overflows.  */
10762           if (equality_comparison_p
10763               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10764                                                         op1, XEXP (op0, 1))))
10765             {
10766               op0 = XEXP (op0, 0);
10767               op1 = tem;
10768               continue;
10769             }
10770
10771           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
10772           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10773               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10774             {
10775               op0 = XEXP (XEXP (op0, 0), 0);
10776               code = (code == LT ? EQ : NE);
10777               continue;
10778             }
10779           break;
10780
10781         case MINUS:
10782           /* We used to optimize signed comparisons against zero, but that
10783              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
10784              arrive here as equality comparisons, or (GEU, LTU) are
10785              optimized away.  No need to special-case them.  */
10786
10787           /* (eq (minus A B) C) -> (eq A (plus B C)) or
10788              (eq B (minus A C)), whichever simplifies.  We can only do
10789              this for equality comparisons due to pathological cases involving
10790              overflows.  */
10791           if (equality_comparison_p
10792               && 0 != (tem = simplify_binary_operation (PLUS, mode,
10793                                                         XEXP (op0, 1), op1)))
10794             {
10795               op0 = XEXP (op0, 0);
10796               op1 = tem;
10797               continue;
10798             }
10799
10800           if (equality_comparison_p
10801               && 0 != (tem = simplify_binary_operation (MINUS, mode,
10802                                                         XEXP (op0, 0), op1)))
10803             {
10804               op0 = XEXP (op0, 1);
10805               op1 = tem;
10806               continue;
10807             }
10808
10809           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10810              of bits in X minus 1, is one iff X > 0.  */
10811           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10812               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10813               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10814                  == mode_width - 1
10815               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10816             {
10817               op0 = XEXP (op0, 1);
10818               code = (code == GE ? LE : GT);
10819               continue;
10820             }
10821           break;
10822
10823         case XOR:
10824           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
10825              if C is zero or B is a constant.  */
10826           if (equality_comparison_p
10827               && 0 != (tem = simplify_binary_operation (XOR, mode,
10828                                                         XEXP (op0, 1), op1)))
10829             {
10830               op0 = XEXP (op0, 0);
10831               op1 = tem;
10832               continue;
10833             }
10834           break;
10835
10836         case EQ:  case NE:
10837         case UNEQ:  case LTGT:
10838         case LT:  case LTU:  case UNLT:  case LE:  case LEU:  case UNLE:
10839         case GT:  case GTU:  case UNGT:  case GE:  case GEU:  case UNGE:
10840         case UNORDERED: case ORDERED:
10841           /* We can't do anything if OP0 is a condition code value, rather
10842              than an actual data value.  */
10843           if (const_op != 0
10844               || CC0_P (XEXP (op0, 0))
10845               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10846             break;
10847
10848           /* Get the two operands being compared.  */
10849           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10850             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10851           else
10852             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10853
10854           /* Check for the cases where we simply want the result of the
10855              earlier test or the opposite of that result.  */
10856           if (code == NE || code == EQ
10857               || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10858                   && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10859                   && (STORE_FLAG_VALUE
10860                       & (((HOST_WIDE_INT) 1
10861                           << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10862                   && (code == LT || code == GE)))
10863             {
10864               enum rtx_code new_code;
10865               if (code == LT || code == NE)
10866                 new_code = GET_CODE (op0);
10867               else
10868                 new_code = combine_reversed_comparison_code (op0);
10869
10870               if (new_code != UNKNOWN)
10871                 {
10872                   code = new_code;
10873                   op0 = tem;
10874                   op1 = tem1;
10875                   continue;
10876                 }
10877             }
10878           break;
10879
10880         case IOR:
10881           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10882              iff X <= 0.  */
10883           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10884               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10885               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10886             {
10887               op0 = XEXP (op0, 1);
10888               code = (code == GE ? GT : LE);
10889               continue;
10890             }
10891           break;
10892
10893         case AND:
10894           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
10895              will be converted to a ZERO_EXTRACT later.  */
10896           if (const_op == 0 && equality_comparison_p
10897               && GET_CODE (XEXP (op0, 0)) == ASHIFT
10898               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10899             {
10900               op0 = simplify_and_const_int
10901                 (op0, mode, gen_rtx_LSHIFTRT (mode,
10902                                               XEXP (op0, 1),
10903                                               XEXP (XEXP (op0, 0), 1)),
10904                  (HOST_WIDE_INT) 1);
10905               continue;
10906             }
10907
10908           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10909              zero and X is a comparison and C1 and C2 describe only bits set
10910              in STORE_FLAG_VALUE, we can compare with X.  */
10911           if (const_op == 0 && equality_comparison_p
10912               && mode_width <= HOST_BITS_PER_WIDE_INT
10913               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10914               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10915               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10916               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10917               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10918             {
10919               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10920                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
10921               if ((~STORE_FLAG_VALUE & mask) == 0
10922                   && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10923                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10924                           && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10925                 {
10926                   op0 = XEXP (XEXP (op0, 0), 0);
10927                   continue;
10928                 }
10929             }
10930
10931           /* If we are doing an equality comparison of an AND of a bit equal
10932              to the sign bit, replace this with a LT or GE comparison of
10933              the underlying value.  */
10934           if (equality_comparison_p
10935               && const_op == 0
10936               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10937               && mode_width <= HOST_BITS_PER_WIDE_INT
10938               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10939                   == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10940             {
10941               op0 = XEXP (op0, 0);
10942               code = (code == EQ ? GE : LT);
10943               continue;
10944             }
10945
10946           /* If this AND operation is really a ZERO_EXTEND from a narrower
10947              mode, the constant fits within that mode, and this is either an
10948              equality or unsigned comparison, try to do this comparison in
10949              the narrower mode.  */
10950           if ((equality_comparison_p || unsigned_comparison_p)
10951               && GET_CODE (XEXP (op0, 1)) == CONST_INT
10952               && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10953                                    & GET_MODE_MASK (mode))
10954                                   + 1)) >= 0
10955               && const_op >> i == 0
10956               && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10957             {
10958               op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
10959               continue;
10960             }
10961
10962           /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10963              fits in both M1 and M2 and the SUBREG is either paradoxical
10964              or represents the low part, permute the SUBREG and the AND
10965              and try again.  */
10966           if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10967             {
10968               unsigned HOST_WIDE_INT c1;
10969               tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10970               /* Require an integral mode, to avoid creating something like
10971                  (AND:SF ...).  */
10972               if (SCALAR_INT_MODE_P (tmode)
10973                   /* It is unsafe to commute the AND into the SUBREG if the
10974                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10975                      not defined.  As originally written the upper bits
10976                      have a defined value due to the AND operation.
10977                      However, if we commute the AND inside the SUBREG then
10978                      they no longer have defined values and the meaning of
10979                      the code has been changed.  */
10980                   && (0
10981 #ifdef WORD_REGISTER_OPERATIONS
10982                       || (mode_width > GET_MODE_BITSIZE (tmode)
10983                           && mode_width <= BITS_PER_WORD)
10984 #endif
10985                       || (mode_width <= GET_MODE_BITSIZE (tmode)
10986                           && subreg_lowpart_p (XEXP (op0, 0))))
10987                   && GET_CODE (XEXP (op0, 1)) == CONST_INT
10988                   && mode_width <= HOST_BITS_PER_WIDE_INT
10989                   && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10990                   && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10991                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
10992                   && c1 != mask
10993                   && c1 != GET_MODE_MASK (tmode))
10994                 {
10995                   op0 = gen_binary (AND, tmode,
10996                                     SUBREG_REG (XEXP (op0, 0)),
10997                                     gen_int_mode (c1, tmode));
10998                   op0 = gen_lowpart_for_combine (mode, op0);
10999                   continue;
11000                 }
11001             }
11002
11003           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
11004           if (const_op == 0 && equality_comparison_p
11005               && XEXP (op0, 1) == const1_rtx
11006               && GET_CODE (XEXP (op0, 0)) == NOT)
11007             {
11008               op0 = simplify_and_const_int
11009                 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
11010               code = (code == NE ? EQ : NE);
11011               continue;
11012             }
11013
11014           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11015              (eq (and (lshiftrt X) 1) 0).
11016              Also handle the case where (not X) is expressed using xor.  */
11017           if (const_op == 0 && equality_comparison_p
11018               && XEXP (op0, 1) == const1_rtx
11019               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11020             {
11021               rtx shift_op = XEXP (XEXP (op0, 0), 0);
11022               rtx shift_count = XEXP (XEXP (op0, 0), 1);
11023
11024               if (GET_CODE (shift_op) == NOT
11025                   || (GET_CODE (shift_op) == XOR
11026                       && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
11027                       && GET_CODE (shift_count) == CONST_INT
11028                       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11029                       && (INTVAL (XEXP (shift_op, 1))
11030                           == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
11031                 {
11032                   op0 = simplify_and_const_int
11033                     (NULL_RTX, mode,
11034                      gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
11035                      (HOST_WIDE_INT) 1);
11036                   code = (code == NE ? EQ : NE);
11037                   continue;
11038                 }
11039             }
11040           break;
11041
11042         case ASHIFT:
11043           /* If we have (compare (ashift FOO N) (const_int C)) and
11044              the high order N bits of FOO (N+1 if an inequality comparison)
11045              are known to be zero, we can do this by comparing FOO with C
11046              shifted right N bits so long as the low-order N bits of C are
11047              zero.  */
11048           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11049               && INTVAL (XEXP (op0, 1)) >= 0
11050               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11051                   < HOST_BITS_PER_WIDE_INT)
11052               && ((const_op
11053                    & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
11054               && mode_width <= HOST_BITS_PER_WIDE_INT
11055               && (nonzero_bits (XEXP (op0, 0), mode)
11056                   & ~(mask >> (INTVAL (XEXP (op0, 1))
11057                                + ! equality_comparison_p))) == 0)
11058             {
11059               /* We must perform a logical shift, not an arithmetic one,
11060                  as we want the top N bits of C to be zero.  */
11061               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11062
11063               temp >>= INTVAL (XEXP (op0, 1));
11064               op1 = gen_int_mode (temp, mode);
11065               op0 = XEXP (op0, 0);
11066               continue;
11067             }
11068
11069           /* If we are doing a sign bit comparison, it means we are testing
11070              a particular bit.  Convert it to the appropriate AND.  */
11071           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
11072               && mode_width <= HOST_BITS_PER_WIDE_INT)
11073             {
11074               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11075                                             ((HOST_WIDE_INT) 1
11076                                              << (mode_width - 1
11077                                                  - INTVAL (XEXP (op0, 1)))));
11078               code = (code == LT ? NE : EQ);
11079               continue;
11080             }
11081
11082           /* If this an equality comparison with zero and we are shifting
11083              the low bit to the sign bit, we can convert this to an AND of the
11084              low-order bit.  */
11085           if (const_op == 0 && equality_comparison_p
11086               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11087               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11088                  == mode_width - 1)
11089             {
11090               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11091                                             (HOST_WIDE_INT) 1);
11092               continue;
11093             }
11094           break;
11095
11096         case ASHIFTRT:
11097           /* If this is an equality comparison with zero, we can do this
11098              as a logical shift, which might be much simpler.  */
11099           if (equality_comparison_p && const_op == 0
11100               && GET_CODE (XEXP (op0, 1)) == CONST_INT)
11101             {
11102               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11103                                           XEXP (op0, 0),
11104                                           INTVAL (XEXP (op0, 1)));
11105               continue;
11106             }
11107
11108           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11109              do the comparison in a narrower mode.  */
11110           if (! unsigned_comparison_p
11111               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11112               && GET_CODE (XEXP (op0, 0)) == ASHIFT
11113               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11114               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11115                                          MODE_INT, 1)) != BLKmode
11116               && (((unsigned HOST_WIDE_INT) const_op
11117                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11118                   <= GET_MODE_MASK (tmode)))
11119             {
11120               op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
11121               continue;
11122             }
11123
11124           /* Likewise if OP0 is a PLUS of a sign extension with a
11125              constant, which is usually represented with the PLUS
11126              between the shifts.  */
11127           if (! unsigned_comparison_p
11128               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11129               && GET_CODE (XEXP (op0, 0)) == PLUS
11130               && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
11131               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11132               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11133               && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11134                                          MODE_INT, 1)) != BLKmode
11135               && (((unsigned HOST_WIDE_INT) const_op
11136                    + (GET_MODE_MASK (tmode) >> 1) + 1)
11137                   <= GET_MODE_MASK (tmode)))
11138             {
11139               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11140               rtx add_const = XEXP (XEXP (op0, 0), 1);
11141               rtx new_const = gen_binary (ASHIFTRT, GET_MODE (op0), add_const,
11142                                           XEXP (op0, 1));
11143
11144               op0 = gen_binary (PLUS, tmode,
11145                                 gen_lowpart_for_combine (tmode, inner),
11146                                 new_const);
11147               continue;
11148             }
11149
11150           /* ... fall through ...  */
11151         case LSHIFTRT:
11152           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11153              the low order N bits of FOO are known to be zero, we can do this
11154              by comparing FOO with C shifted left N bits so long as no
11155              overflow occurs.  */
11156           if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11157               && INTVAL (XEXP (op0, 1)) >= 0
11158               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11159               && mode_width <= HOST_BITS_PER_WIDE_INT
11160               && (nonzero_bits (XEXP (op0, 0), mode)
11161                   & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11162               && (((unsigned HOST_WIDE_INT) const_op
11163                    + (GET_CODE (op0) != LSHIFTRT
11164                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11165                          + 1)
11166                       : 0))
11167                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11168             {
11169               /* If the shift was logical, then we must make the condition
11170                  unsigned.  */
11171               if (GET_CODE (op0) == LSHIFTRT)
11172                 code = unsigned_condition (code);
11173
11174               const_op <<= INTVAL (XEXP (op0, 1));
11175               op1 = GEN_INT (const_op);
11176               op0 = XEXP (op0, 0);
11177               continue;
11178             }
11179
11180           /* If we are using this shift to extract just the sign bit, we
11181              can replace this with an LT or GE comparison.  */
11182           if (const_op == 0
11183               && (equality_comparison_p || sign_bit_comparison_p)
11184               && GET_CODE (XEXP (op0, 1)) == CONST_INT
11185               && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11186                  == mode_width - 1)
11187             {
11188               op0 = XEXP (op0, 0);
11189               code = (code == NE || code == GT ? LT : GE);
11190               continue;
11191             }
11192           break;
11193
11194         default:
11195           break;
11196         }
11197
11198       break;
11199     }
11200
11201   /* Now make any compound operations involved in this comparison.  Then,
11202      check for an outmost SUBREG on OP0 that is not doing anything or is
11203      paradoxical.  The latter transformation must only be performed when
11204      it is known that the "extra" bits will be the same in op0 and op1 or
11205      that they don't matter.  There are three cases to consider:
11206
11207      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
11208      care bits and we can assume they have any convenient value.  So
11209      making the transformation is safe.
11210
11211      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11212      In this case the upper bits of op0 are undefined.  We should not make
11213      the simplification in that case as we do not know the contents of
11214      those bits.
11215
11216      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11217      NIL.  In that case we know those bits are zeros or ones.  We must
11218      also be sure that they are the same as the upper bits of op1.
11219
11220      We can never remove a SUBREG for a non-equality comparison because
11221      the sign bit is in a different place in the underlying object.  */
11222
11223   op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11224   op1 = make_compound_operation (op1, SET);
11225
11226   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11227       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11228       && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11229       && (code == NE || code == EQ))
11230     {
11231       if (GET_MODE_SIZE (GET_MODE (op0))
11232           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11233         {
11234           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
11235              implemented.  */
11236           if (GET_CODE (SUBREG_REG (op0)) == REG)
11237             {
11238               op0 = SUBREG_REG (op0);
11239               op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
11240             }
11241         }
11242       else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11243                 <= HOST_BITS_PER_WIDE_INT)
11244                && (nonzero_bits (SUBREG_REG (op0),
11245                                  GET_MODE (SUBREG_REG (op0)))
11246                    & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11247         {
11248           tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)), op1);
11249
11250           if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11251                & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11252             op0 = SUBREG_REG (op0), op1 = tem;
11253         }
11254     }
11255
11256   /* We now do the opposite procedure: Some machines don't have compare
11257      insns in all modes.  If OP0's mode is an integer mode smaller than a
11258      word and we can't do a compare in that mode, see if there is a larger
11259      mode for which we can do the compare.  There are a number of cases in
11260      which we can use the wider mode.  */
11261
11262   mode = GET_MODE (op0);
11263   if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11264       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11265       && ! have_insn_for (COMPARE, mode))
11266     for (tmode = GET_MODE_WIDER_MODE (mode);
11267          (tmode != VOIDmode
11268           && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11269          tmode = GET_MODE_WIDER_MODE (tmode))
11270       if (have_insn_for (COMPARE, tmode))
11271         {
11272           int zero_extended;
11273
11274           /* If the only nonzero bits in OP0 and OP1 are those in the
11275              narrower mode and this is an equality or unsigned comparison,
11276              we can use the wider mode.  Similarly for sign-extended
11277              values, in which case it is true for all comparisons.  */
11278           zero_extended = ((code == EQ || code == NE
11279                             || code == GEU || code == GTU
11280                             || code == LEU || code == LTU)
11281                            && (nonzero_bits (op0, tmode)
11282                                & ~GET_MODE_MASK (mode)) == 0
11283                            && ((GET_CODE (op1) == CONST_INT
11284                                 || (nonzero_bits (op1, tmode)
11285                                     & ~GET_MODE_MASK (mode)) == 0)));
11286
11287           if (zero_extended
11288               || ((num_sign_bit_copies (op0, tmode)
11289                    > (unsigned int) (GET_MODE_BITSIZE (tmode)
11290                                      - GET_MODE_BITSIZE (mode)))
11291                   && (num_sign_bit_copies (op1, tmode)
11292                       > (unsigned int) (GET_MODE_BITSIZE (tmode)
11293                                         - GET_MODE_BITSIZE (mode)))))
11294             {
11295               /* If OP0 is an AND and we don't have an AND in MODE either,
11296                  make a new AND in the proper mode.  */
11297               if (GET_CODE (op0) == AND
11298                   && !have_insn_for (AND, mode))
11299                 op0 = gen_binary (AND, tmode,
11300                                   gen_lowpart_for_combine (tmode,
11301                                                            XEXP (op0, 0)),
11302                                   gen_lowpart_for_combine (tmode,
11303                                                            XEXP (op0, 1)));
11304
11305               op0 = gen_lowpart_for_combine (tmode, op0);
11306               if (zero_extended && GET_CODE (op1) == CONST_INT)
11307                 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11308               op1 = gen_lowpart_for_combine (tmode, op1);
11309               break;
11310             }
11311
11312           /* If this is a test for negative, we can make an explicit
11313              test of the sign bit.  */
11314
11315           if (op1 == const0_rtx && (code == LT || code == GE)
11316               && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11317             {
11318               op0 = gen_binary (AND, tmode,
11319                                 gen_lowpart_for_combine (tmode, op0),
11320                                 GEN_INT ((HOST_WIDE_INT) 1
11321                                          << (GET_MODE_BITSIZE (mode) - 1)));
11322               code = (code == LT) ? NE : EQ;
11323               break;
11324             }
11325         }
11326
11327 #ifdef CANONICALIZE_COMPARISON
11328   /* If this machine only supports a subset of valid comparisons, see if we
11329      can convert an unsupported one into a supported one.  */
11330   CANONICALIZE_COMPARISON (code, op0, op1);
11331 #endif
11332
11333   *pop0 = op0;
11334   *pop1 = op1;
11335
11336   return code;
11337 }
11338 \f
11339 /* Like jump.c' reversed_comparison_code, but use combine infrastructure for
11340    searching backward.  */
11341 static enum rtx_code
11342 combine_reversed_comparison_code (rtx exp)
11343 {
11344   enum rtx_code code1 = reversed_comparison_code (exp, NULL);
11345   rtx x;
11346
11347   if (code1 != UNKNOWN
11348       || GET_MODE_CLASS (GET_MODE (XEXP (exp, 0))) != MODE_CC)
11349     return code1;
11350   /* Otherwise try and find where the condition codes were last set and
11351      use that.  */
11352   x = get_last_value (XEXP (exp, 0));
11353   if (!x || GET_CODE (x) != COMPARE)
11354     return UNKNOWN;
11355   return reversed_comparison_code_parts (GET_CODE (exp),
11356                                          XEXP (x, 0), XEXP (x, 1), NULL);
11357 }
11358
11359 /* Return comparison with reversed code of EXP and operands OP0 and OP1.
11360    Return NULL_RTX in case we fail to do the reversal.  */
11361 static rtx
11362 reversed_comparison (rtx exp, enum machine_mode mode, rtx op0, rtx op1)
11363 {
11364   enum rtx_code reversed_code = combine_reversed_comparison_code (exp);
11365   if (reversed_code == UNKNOWN)
11366     return NULL_RTX;
11367   else
11368     return gen_binary (reversed_code, mode, op0, op1);
11369 }
11370 \f
11371 /* Utility function for following routine.  Called when X is part of a value
11372    being stored into reg_last_set_value.  Sets reg_last_set_table_tick
11373    for each register mentioned.  Similar to mention_regs in cse.c  */
11374
11375 static void
11376 update_table_tick (rtx x)
11377 {
11378   enum rtx_code code = GET_CODE (x);
11379   const char *fmt = GET_RTX_FORMAT (code);
11380   int i;
11381
11382   if (code == REG)
11383     {
11384       unsigned int regno = REGNO (x);
11385       unsigned int endregno
11386         = regno + (regno < FIRST_PSEUDO_REGISTER
11387                    ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11388       unsigned int r;
11389
11390       for (r = regno; r < endregno; r++)
11391         reg_last_set_table_tick[r] = label_tick;
11392
11393       return;
11394     }
11395
11396   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11397     /* Note that we can't have an "E" in values stored; see
11398        get_last_value_validate.  */
11399     if (fmt[i] == 'e')
11400       {
11401         /* Check for identical subexpressions.  If x contains
11402            identical subexpression we only have to traverse one of
11403            them.  */
11404         if (i == 0
11405             && (GET_RTX_CLASS (code) == '2'
11406                 || GET_RTX_CLASS (code) == 'c'))
11407           {
11408             /* Note that at this point x1 has already been
11409                processed.  */
11410             rtx x0 = XEXP (x, 0);
11411             rtx x1 = XEXP (x, 1);
11412
11413             /* If x0 and x1 are identical then there is no need to
11414                process x0.  */
11415             if (x0 == x1)
11416               break;
11417
11418             /* If x0 is identical to a subexpression of x1 then while
11419                processing x1, x0 has already been processed.  Thus we
11420                are done with x.  */
11421             if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11422                  || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11423                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11424               break;
11425
11426             /* If x1 is identical to a subexpression of x0 then we
11427                still have to process the rest of x0.  */
11428             if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11429                  || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11430                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11431               {
11432                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11433                 break;
11434               }
11435           }
11436
11437         update_table_tick (XEXP (x, i));
11438       }
11439 }
11440
11441 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
11442    are saying that the register is clobbered and we no longer know its
11443    value.  If INSN is zero, don't update reg_last_set; this is only permitted
11444    with VALUE also zero and is used to invalidate the register.  */
11445
11446 static void
11447 record_value_for_reg (rtx reg, rtx insn, rtx value)
11448 {
11449   unsigned int regno = REGNO (reg);
11450   unsigned int endregno
11451     = regno + (regno < FIRST_PSEUDO_REGISTER
11452                ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
11453   unsigned int i;
11454
11455   /* If VALUE contains REG and we have a previous value for REG, substitute
11456      the previous value.  */
11457   if (value && insn && reg_overlap_mentioned_p (reg, value))
11458     {
11459       rtx tem;
11460
11461       /* Set things up so get_last_value is allowed to see anything set up to
11462          our insn.  */
11463       subst_low_cuid = INSN_CUID (insn);
11464       tem = get_last_value (reg);
11465
11466       /* If TEM is simply a binary operation with two CLOBBERs as operands,
11467          it isn't going to be useful and will take a lot of time to process,
11468          so just use the CLOBBER.  */
11469
11470       if (tem)
11471         {
11472           if ((GET_RTX_CLASS (GET_CODE (tem)) == '2'
11473                || GET_RTX_CLASS (GET_CODE (tem)) == 'c')
11474               && GET_CODE (XEXP (tem, 0)) == CLOBBER
11475               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11476             tem = XEXP (tem, 0);
11477
11478           value = replace_rtx (copy_rtx (value), reg, tem);
11479         }
11480     }
11481
11482   /* For each register modified, show we don't know its value, that
11483      we don't know about its bitwise content, that its value has been
11484      updated, and that we don't know the location of the death of the
11485      register.  */
11486   for (i = regno; i < endregno; i++)
11487     {
11488       if (insn)
11489         reg_last_set[i] = insn;
11490
11491       reg_last_set_value[i] = 0;
11492       reg_last_set_mode[i] = 0;
11493       reg_last_set_nonzero_bits[i] = 0;
11494       reg_last_set_sign_bit_copies[i] = 0;
11495       reg_last_death[i] = 0;
11496     }
11497
11498   /* Mark registers that are being referenced in this value.  */
11499   if (value)
11500     update_table_tick (value);
11501
11502   /* Now update the status of each register being set.
11503      If someone is using this register in this block, set this register
11504      to invalid since we will get confused between the two lives in this
11505      basic block.  This makes using this register always invalid.  In cse, we
11506      scan the table to invalidate all entries using this register, but this
11507      is too much work for us.  */
11508
11509   for (i = regno; i < endregno; i++)
11510     {
11511       reg_last_set_label[i] = label_tick;
11512       if (value && reg_last_set_table_tick[i] == label_tick)
11513         reg_last_set_invalid[i] = 1;
11514       else
11515         reg_last_set_invalid[i] = 0;
11516     }
11517
11518   /* The value being assigned might refer to X (like in "x++;").  In that
11519      case, we must replace it with (clobber (const_int 0)) to prevent
11520      infinite loops.  */
11521   if (value && ! get_last_value_validate (&value, insn,
11522                                           reg_last_set_label[regno], 0))
11523     {
11524       value = copy_rtx (value);
11525       if (! get_last_value_validate (&value, insn,
11526                                      reg_last_set_label[regno], 1))
11527         value = 0;
11528     }
11529
11530   /* For the main register being modified, update the value, the mode, the
11531      nonzero bits, and the number of sign bit copies.  */
11532
11533   reg_last_set_value[regno] = value;
11534
11535   if (value)
11536     {
11537       enum machine_mode mode = GET_MODE (reg);
11538       subst_low_cuid = INSN_CUID (insn);
11539       reg_last_set_mode[regno] = mode;
11540       if (GET_MODE_CLASS (mode) == MODE_INT
11541           && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11542         mode = nonzero_bits_mode;
11543       reg_last_set_nonzero_bits[regno] = nonzero_bits (value, mode);
11544       reg_last_set_sign_bit_copies[regno]
11545         = num_sign_bit_copies (value, GET_MODE (reg));
11546     }
11547 }
11548
11549 /* Called via note_stores from record_dead_and_set_regs to handle one
11550    SET or CLOBBER in an insn.  DATA is the instruction in which the
11551    set is occurring.  */
11552
11553 static void
11554 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
11555 {
11556   rtx record_dead_insn = (rtx) data;
11557
11558   if (GET_CODE (dest) == SUBREG)
11559     dest = SUBREG_REG (dest);
11560
11561   if (GET_CODE (dest) == REG)
11562     {
11563       /* If we are setting the whole register, we know its value.  Otherwise
11564          show that we don't know the value.  We can handle SUBREG in
11565          some cases.  */
11566       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11567         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11568       else if (GET_CODE (setter) == SET
11569                && GET_CODE (SET_DEST (setter)) == SUBREG
11570                && SUBREG_REG (SET_DEST (setter)) == dest
11571                && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11572                && subreg_lowpart_p (SET_DEST (setter)))
11573         record_value_for_reg (dest, record_dead_insn,
11574                               gen_lowpart_for_combine (GET_MODE (dest),
11575                                                        SET_SRC (setter)));
11576       else
11577         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11578     }
11579   else if (GET_CODE (dest) == MEM
11580            /* Ignore pushes, they clobber nothing.  */
11581            && ! push_operand (dest, GET_MODE (dest)))
11582     mem_last_set = INSN_CUID (record_dead_insn);
11583 }
11584
11585 /* Update the records of when each REG was most recently set or killed
11586    for the things done by INSN.  This is the last thing done in processing
11587    INSN in the combiner loop.
11588
11589    We update reg_last_set, reg_last_set_value, reg_last_set_mode,
11590    reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
11591    and also the similar information mem_last_set (which insn most recently
11592    modified memory) and last_call_cuid (which insn was the most recent
11593    subroutine call).  */
11594
11595 static void
11596 record_dead_and_set_regs (rtx insn)
11597 {
11598   rtx link;
11599   unsigned int i;
11600
11601   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11602     {
11603       if (REG_NOTE_KIND (link) == REG_DEAD
11604           && GET_CODE (XEXP (link, 0)) == REG)
11605         {
11606           unsigned int regno = REGNO (XEXP (link, 0));
11607           unsigned int endregno
11608             = regno + (regno < FIRST_PSEUDO_REGISTER
11609                        ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
11610                        : 1);
11611
11612           for (i = regno; i < endregno; i++)
11613             reg_last_death[i] = insn;
11614         }
11615       else if (REG_NOTE_KIND (link) == REG_INC)
11616         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11617     }
11618
11619   if (GET_CODE (insn) == CALL_INSN)
11620     {
11621       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11622         if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11623           {
11624             reg_last_set_value[i] = 0;
11625             reg_last_set_mode[i] = 0;
11626             reg_last_set_nonzero_bits[i] = 0;
11627             reg_last_set_sign_bit_copies[i] = 0;
11628             reg_last_death[i] = 0;
11629           }
11630
11631       last_call_cuid = mem_last_set = INSN_CUID (insn);
11632
11633       /* Don't bother recording what this insn does.  It might set the
11634          return value register, but we can't combine into a call
11635          pattern anyway, so there's no point trying (and it may cause
11636          a crash, if e.g. we wind up asking for last_set_value of a
11637          SUBREG of the return value register).  */
11638       return;
11639     }
11640
11641   note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11642 }
11643
11644 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11645    register present in the SUBREG, so for each such SUBREG go back and
11646    adjust nonzero and sign bit information of the registers that are
11647    known to have some zero/sign bits set.
11648
11649    This is needed because when combine blows the SUBREGs away, the
11650    information on zero/sign bits is lost and further combines can be
11651    missed because of that.  */
11652
11653 static void
11654 record_promoted_value (rtx insn, rtx subreg)
11655 {
11656   rtx links, set;
11657   unsigned int regno = REGNO (SUBREG_REG (subreg));
11658   enum machine_mode mode = GET_MODE (subreg);
11659
11660   if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11661     return;
11662
11663   for (links = LOG_LINKS (insn); links;)
11664     {
11665       insn = XEXP (links, 0);
11666       set = single_set (insn);
11667
11668       if (! set || GET_CODE (SET_DEST (set)) != REG
11669           || REGNO (SET_DEST (set)) != regno
11670           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11671         {
11672           links = XEXP (links, 1);
11673           continue;
11674         }
11675
11676       if (reg_last_set[regno] == insn)
11677         {
11678           if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11679             reg_last_set_nonzero_bits[regno] &= GET_MODE_MASK (mode);
11680         }
11681
11682       if (GET_CODE (SET_SRC (set)) == REG)
11683         {
11684           regno = REGNO (SET_SRC (set));
11685           links = LOG_LINKS (insn);
11686         }
11687       else
11688         break;
11689     }
11690 }
11691
11692 /* Scan X for promoted SUBREGs.  For each one found,
11693    note what it implies to the registers used in it.  */
11694
11695 static void
11696 check_promoted_subreg (rtx insn, rtx x)
11697 {
11698   if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11699       && GET_CODE (SUBREG_REG (x)) == REG)
11700     record_promoted_value (insn, x);
11701   else
11702     {
11703       const char *format = GET_RTX_FORMAT (GET_CODE (x));
11704       int i, j;
11705
11706       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11707         switch (format[i])
11708           {
11709           case 'e':
11710             check_promoted_subreg (insn, XEXP (x, i));
11711             break;
11712           case 'V':
11713           case 'E':
11714             if (XVEC (x, i) != 0)
11715               for (j = 0; j < XVECLEN (x, i); j++)
11716                 check_promoted_subreg (insn, XVECEXP (x, i, j));
11717             break;
11718           }
11719     }
11720 }
11721 \f
11722 /* Utility routine for the following function.  Verify that all the registers
11723    mentioned in *LOC are valid when *LOC was part of a value set when
11724    label_tick == TICK.  Return 0 if some are not.
11725
11726    If REPLACE is nonzero, replace the invalid reference with
11727    (clobber (const_int 0)) and return 1.  This replacement is useful because
11728    we often can get useful information about the form of a value (e.g., if
11729    it was produced by a shift that always produces -1 or 0) even though
11730    we don't know exactly what registers it was produced from.  */
11731
11732 static int
11733 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11734 {
11735   rtx x = *loc;
11736   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11737   int len = GET_RTX_LENGTH (GET_CODE (x));
11738   int i;
11739
11740   if (GET_CODE (x) == REG)
11741     {
11742       unsigned int regno = REGNO (x);
11743       unsigned int endregno
11744         = regno + (regno < FIRST_PSEUDO_REGISTER
11745                    ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11746       unsigned int j;
11747
11748       for (j = regno; j < endregno; j++)
11749         if (reg_last_set_invalid[j]
11750             /* If this is a pseudo-register that was only set once and not
11751                live at the beginning of the function, it is always valid.  */
11752             || (! (regno >= FIRST_PSEUDO_REGISTER
11753                    && REG_N_SETS (regno) == 1
11754                    && (! REGNO_REG_SET_P
11755                        (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
11756                 && reg_last_set_label[j] > tick))
11757           {
11758             if (replace)
11759               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11760             return replace;
11761           }
11762
11763       return 1;
11764     }
11765   /* If this is a memory reference, make sure that there were
11766      no stores after it that might have clobbered the value.  We don't
11767      have alias info, so we assume any store invalidates it.  */
11768   else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
11769            && INSN_CUID (insn) <= mem_last_set)
11770     {
11771       if (replace)
11772         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11773       return replace;
11774     }
11775
11776   for (i = 0; i < len; i++)
11777     {
11778       if (fmt[i] == 'e')
11779         {
11780           /* Check for identical subexpressions.  If x contains
11781              identical subexpression we only have to traverse one of
11782              them.  */
11783           if (i == 1
11784               && (GET_RTX_CLASS (GET_CODE (x)) == '2'
11785                   || GET_RTX_CLASS (GET_CODE (x)) == 'c'))
11786             {
11787               /* Note that at this point x0 has already been checked
11788                  and found valid.  */
11789               rtx x0 = XEXP (x, 0);
11790               rtx x1 = XEXP (x, 1);
11791
11792               /* If x0 and x1 are identical then x is also valid.  */
11793               if (x0 == x1)
11794                 return 1;
11795
11796               /* If x1 is identical to a subexpression of x0 then
11797                  while checking x0, x1 has already been checked.  Thus
11798                  it is valid and so as x.  */
11799               if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11800                    || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11801                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11802                 return 1;
11803
11804               /* If x0 is identical to a subexpression of x1 then x is
11805                  valid iff the rest of x1 is valid.  */
11806               if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11807                    || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11808                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11809                 return
11810                   get_last_value_validate (&XEXP (x1,
11811                                                   x0 == XEXP (x1, 0) ? 1 : 0),
11812                                            insn, tick, replace);
11813             }
11814
11815           if (get_last_value_validate (&XEXP (x, i), insn, tick,
11816                                        replace) == 0)
11817             return 0;
11818         }
11819       /* Don't bother with these.  They shouldn't occur anyway.  */
11820       else if (fmt[i] == 'E')
11821         return 0;
11822     }
11823
11824   /* If we haven't found a reason for it to be invalid, it is valid.  */
11825   return 1;
11826 }
11827
11828 /* Get the last value assigned to X, if known.  Some registers
11829    in the value may be replaced with (clobber (const_int 0)) if their value
11830    is known longer known reliably.  */
11831
11832 static rtx
11833 get_last_value (rtx x)
11834 {
11835   unsigned int regno;
11836   rtx value;
11837
11838   /* If this is a non-paradoxical SUBREG, get the value of its operand and
11839      then convert it to the desired mode.  If this is a paradoxical SUBREG,
11840      we cannot predict what values the "extra" bits might have.  */
11841   if (GET_CODE (x) == SUBREG
11842       && subreg_lowpart_p (x)
11843       && (GET_MODE_SIZE (GET_MODE (x))
11844           <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11845       && (value = get_last_value (SUBREG_REG (x))) != 0)
11846     return gen_lowpart_for_combine (GET_MODE (x), value);
11847
11848   if (GET_CODE (x) != REG)
11849     return 0;
11850
11851   regno = REGNO (x);
11852   value = reg_last_set_value[regno];
11853
11854   /* If we don't have a value, or if it isn't for this basic block and
11855      it's either a hard register, set more than once, or it's a live
11856      at the beginning of the function, return 0.
11857
11858      Because if it's not live at the beginning of the function then the reg
11859      is always set before being used (is never used without being set).
11860      And, if it's set only once, and it's always set before use, then all
11861      uses must have the same last value, even if it's not from this basic
11862      block.  */
11863
11864   if (value == 0
11865       || (reg_last_set_label[regno] != label_tick
11866           && (regno < FIRST_PSEUDO_REGISTER
11867               || REG_N_SETS (regno) != 1
11868               || (REGNO_REG_SET_P
11869                   (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
11870     return 0;
11871
11872   /* If the value was set in a later insn than the ones we are processing,
11873      we can't use it even if the register was only set once.  */
11874   if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
11875     return 0;
11876
11877   /* If the value has all its registers valid, return it.  */
11878   if (get_last_value_validate (&value, reg_last_set[regno],
11879                                reg_last_set_label[regno], 0))
11880     return value;
11881
11882   /* Otherwise, make a copy and replace any invalid register with
11883      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
11884
11885   value = copy_rtx (value);
11886   if (get_last_value_validate (&value, reg_last_set[regno],
11887                                reg_last_set_label[regno], 1))
11888     return value;
11889
11890   return 0;
11891 }
11892 \f
11893 /* Return nonzero if expression X refers to a REG or to memory
11894    that is set in an instruction more recent than FROM_CUID.  */
11895
11896 static int
11897 use_crosses_set_p (rtx x, int from_cuid)
11898 {
11899   const char *fmt;
11900   int i;
11901   enum rtx_code code = GET_CODE (x);
11902
11903   if (code == REG)
11904     {
11905       unsigned int regno = REGNO (x);
11906       unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11907                                  ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11908
11909 #ifdef PUSH_ROUNDING
11910       /* Don't allow uses of the stack pointer to be moved,
11911          because we don't know whether the move crosses a push insn.  */
11912       if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11913         return 1;
11914 #endif
11915       for (; regno < endreg; regno++)
11916         if (reg_last_set[regno]
11917             && INSN_CUID (reg_last_set[regno]) > from_cuid)
11918           return 1;
11919       return 0;
11920     }
11921
11922   if (code == MEM && mem_last_set > from_cuid)
11923     return 1;
11924
11925   fmt = GET_RTX_FORMAT (code);
11926
11927   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11928     {
11929       if (fmt[i] == 'E')
11930         {
11931           int j;
11932           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11933             if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11934               return 1;
11935         }
11936       else if (fmt[i] == 'e'
11937                && use_crosses_set_p (XEXP (x, i), from_cuid))
11938         return 1;
11939     }
11940   return 0;
11941 }
11942 \f
11943 /* Define three variables used for communication between the following
11944    routines.  */
11945
11946 static unsigned int reg_dead_regno, reg_dead_endregno;
11947 static int reg_dead_flag;
11948
11949 /* Function called via note_stores from reg_dead_at_p.
11950
11951    If DEST is within [reg_dead_regno, reg_dead_endregno), set
11952    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
11953
11954 static void
11955 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11956 {
11957   unsigned int regno, endregno;
11958
11959   if (GET_CODE (dest) != REG)
11960     return;
11961
11962   regno = REGNO (dest);
11963   endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11964                       ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
11965
11966   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11967     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11968 }
11969
11970 /* Return nonzero if REG is known to be dead at INSN.
11971
11972    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
11973    referencing REG, it is dead.  If we hit a SET referencing REG, it is
11974    live.  Otherwise, see if it is live or dead at the start of the basic
11975    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
11976    must be assumed to be always live.  */
11977
11978 static int
11979 reg_dead_at_p (rtx reg, rtx insn)
11980 {
11981   basic_block block;
11982   unsigned int i;
11983
11984   /* Set variables for reg_dead_at_p_1.  */
11985   reg_dead_regno = REGNO (reg);
11986   reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11987                                         ? HARD_REGNO_NREGS (reg_dead_regno,
11988                                                             GET_MODE (reg))
11989                                         : 1);
11990
11991   reg_dead_flag = 0;
11992
11993   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  */
11994   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11995     {
11996       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11997         if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11998           return 0;
11999     }
12000
12001   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
12002      beginning of function.  */
12003   for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
12004        insn = prev_nonnote_insn (insn))
12005     {
12006       note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12007       if (reg_dead_flag)
12008         return reg_dead_flag == 1 ? 1 : 0;
12009
12010       if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12011         return 1;
12012     }
12013
12014   /* Get the basic block that we were in.  */
12015   if (insn == 0)
12016     block = ENTRY_BLOCK_PTR->next_bb;
12017   else
12018     {
12019       FOR_EACH_BB (block)
12020         if (insn == block->head)
12021           break;
12022
12023       if (block == EXIT_BLOCK_PTR)
12024         return 0;
12025     }
12026
12027   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12028     if (REGNO_REG_SET_P (block->global_live_at_start, i))
12029       return 0;
12030
12031   return 1;
12032 }
12033 \f
12034 /* Note hard registers in X that are used.  This code is similar to
12035    that in flow.c, but much simpler since we don't care about pseudos.  */
12036
12037 static void
12038 mark_used_regs_combine (rtx x)
12039 {
12040   RTX_CODE code = GET_CODE (x);
12041   unsigned int regno;
12042   int i;
12043
12044   switch (code)
12045     {
12046     case LABEL_REF:
12047     case SYMBOL_REF:
12048     case CONST_INT:
12049     case CONST:
12050     case CONST_DOUBLE:
12051     case CONST_VECTOR:
12052     case PC:
12053     case ADDR_VEC:
12054     case ADDR_DIFF_VEC:
12055     case ASM_INPUT:
12056 #ifdef HAVE_cc0
12057     /* CC0 must die in the insn after it is set, so we don't need to take
12058        special note of it here.  */
12059     case CC0:
12060 #endif
12061       return;
12062
12063     case CLOBBER:
12064       /* If we are clobbering a MEM, mark any hard registers inside the
12065          address as used.  */
12066       if (GET_CODE (XEXP (x, 0)) == MEM)
12067         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12068       return;
12069
12070     case REG:
12071       regno = REGNO (x);
12072       /* A hard reg in a wide mode may really be multiple registers.
12073          If so, mark all of them just like the first.  */
12074       if (regno < FIRST_PSEUDO_REGISTER)
12075         {
12076           unsigned int endregno, r;
12077
12078           /* None of this applies to the stack, frame or arg pointers.  */
12079           if (regno == STACK_POINTER_REGNUM
12080 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12081               || regno == HARD_FRAME_POINTER_REGNUM
12082 #endif
12083 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12084               || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12085 #endif
12086               || regno == FRAME_POINTER_REGNUM)
12087             return;
12088
12089           endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12090           for (r = regno; r < endregno; r++)
12091             SET_HARD_REG_BIT (newpat_used_regs, r);
12092         }
12093       return;
12094
12095     case SET:
12096       {
12097         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12098            the address.  */
12099         rtx testreg = SET_DEST (x);
12100
12101         while (GET_CODE (testreg) == SUBREG
12102                || GET_CODE (testreg) == ZERO_EXTRACT
12103                || GET_CODE (testreg) == SIGN_EXTRACT
12104                || GET_CODE (testreg) == STRICT_LOW_PART)
12105           testreg = XEXP (testreg, 0);
12106
12107         if (GET_CODE (testreg) == MEM)
12108           mark_used_regs_combine (XEXP (testreg, 0));
12109
12110         mark_used_regs_combine (SET_SRC (x));
12111       }
12112       return;
12113
12114     default:
12115       break;
12116     }
12117
12118   /* Recursively scan the operands of this expression.  */
12119
12120   {
12121     const char *fmt = GET_RTX_FORMAT (code);
12122
12123     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12124       {
12125         if (fmt[i] == 'e')
12126           mark_used_regs_combine (XEXP (x, i));
12127         else if (fmt[i] == 'E')
12128           {
12129             int j;
12130
12131             for (j = 0; j < XVECLEN (x, i); j++)
12132               mark_used_regs_combine (XVECEXP (x, i, j));
12133           }
12134       }
12135   }
12136 }
12137 \f
12138 /* Remove register number REGNO from the dead registers list of INSN.
12139
12140    Return the note used to record the death, if there was one.  */
12141
12142 rtx
12143 remove_death (unsigned int regno, rtx insn)
12144 {
12145   rtx note = find_regno_note (insn, REG_DEAD, regno);
12146
12147   if (note)
12148     {
12149       REG_N_DEATHS (regno)--;
12150       remove_note (insn, note);
12151     }
12152
12153   return note;
12154 }
12155
12156 /* For each register (hardware or pseudo) used within expression X, if its
12157    death is in an instruction with cuid between FROM_CUID (inclusive) and
12158    TO_INSN (exclusive), put a REG_DEAD note for that register in the
12159    list headed by PNOTES.
12160
12161    That said, don't move registers killed by maybe_kill_insn.
12162
12163    This is done when X is being merged by combination into TO_INSN.  These
12164    notes will then be distributed as needed.  */
12165
12166 static void
12167 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
12168              rtx *pnotes)
12169 {
12170   const char *fmt;
12171   int len, i;
12172   enum rtx_code code = GET_CODE (x);
12173
12174   if (code == REG)
12175     {
12176       unsigned int regno = REGNO (x);
12177       rtx where_dead = reg_last_death[regno];
12178       rtx before_dead, after_dead;
12179
12180       /* Don't move the register if it gets killed in between from and to.  */
12181       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12182           && ! reg_referenced_p (x, maybe_kill_insn))
12183         return;
12184
12185       /* WHERE_DEAD could be a USE insn made by combine, so first we
12186          make sure that we have insns with valid INSN_CUID values.  */
12187       before_dead = where_dead;
12188       while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
12189         before_dead = PREV_INSN (before_dead);
12190
12191       after_dead = where_dead;
12192       while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
12193         after_dead = NEXT_INSN (after_dead);
12194
12195       if (before_dead && after_dead
12196           && INSN_CUID (before_dead) >= from_cuid
12197           && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
12198               || (where_dead != after_dead
12199                   && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
12200         {
12201           rtx note = remove_death (regno, where_dead);
12202
12203           /* It is possible for the call above to return 0.  This can occur
12204              when reg_last_death points to I2 or I1 that we combined with.
12205              In that case make a new note.
12206
12207              We must also check for the case where X is a hard register
12208              and NOTE is a death note for a range of hard registers
12209              including X.  In that case, we must put REG_DEAD notes for
12210              the remaining registers in place of NOTE.  */
12211
12212           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12213               && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12214                   > GET_MODE_SIZE (GET_MODE (x))))
12215             {
12216               unsigned int deadregno = REGNO (XEXP (note, 0));
12217               unsigned int deadend
12218                 = (deadregno + HARD_REGNO_NREGS (deadregno,
12219                                                  GET_MODE (XEXP (note, 0))));
12220               unsigned int ourend
12221                 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12222               unsigned int i;
12223
12224               for (i = deadregno; i < deadend; i++)
12225                 if (i < regno || i >= ourend)
12226                   REG_NOTES (where_dead)
12227                     = gen_rtx_EXPR_LIST (REG_DEAD,
12228                                          regno_reg_rtx[i],
12229                                          REG_NOTES (where_dead));
12230             }
12231
12232           /* If we didn't find any note, or if we found a REG_DEAD note that
12233              covers only part of the given reg, and we have a multi-reg hard
12234              register, then to be safe we must check for REG_DEAD notes
12235              for each register other than the first.  They could have
12236              their own REG_DEAD notes lying around.  */
12237           else if ((note == 0
12238                     || (note != 0
12239                         && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12240                             < GET_MODE_SIZE (GET_MODE (x)))))
12241                    && regno < FIRST_PSEUDO_REGISTER
12242                    && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
12243             {
12244               unsigned int ourend
12245                 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12246               unsigned int i, offset;
12247               rtx oldnotes = 0;
12248
12249               if (note)
12250                 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
12251               else
12252                 offset = 1;
12253
12254               for (i = regno + offset; i < ourend; i++)
12255                 move_deaths (regno_reg_rtx[i],
12256                              maybe_kill_insn, from_cuid, to_insn, &oldnotes);
12257             }
12258
12259           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12260             {
12261               XEXP (note, 1) = *pnotes;
12262               *pnotes = note;
12263             }
12264           else
12265             *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12266
12267           REG_N_DEATHS (regno)++;
12268         }
12269
12270       return;
12271     }
12272
12273   else if (GET_CODE (x) == SET)
12274     {
12275       rtx dest = SET_DEST (x);
12276
12277       move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
12278
12279       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12280          that accesses one word of a multi-word item, some
12281          piece of everything register in the expression is used by
12282          this insn, so remove any old death.  */
12283       /* ??? So why do we test for equality of the sizes?  */
12284
12285       if (GET_CODE (dest) == ZERO_EXTRACT
12286           || GET_CODE (dest) == STRICT_LOW_PART
12287           || (GET_CODE (dest) == SUBREG
12288               && (((GET_MODE_SIZE (GET_MODE (dest))
12289                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12290                   == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12291                        + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12292         {
12293           move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
12294           return;
12295         }
12296
12297       /* If this is some other SUBREG, we know it replaces the entire
12298          value, so use that as the destination.  */
12299       if (GET_CODE (dest) == SUBREG)
12300         dest = SUBREG_REG (dest);
12301
12302       /* If this is a MEM, adjust deaths of anything used in the address.
12303          For a REG (the only other possibility), the entire value is
12304          being replaced so the old value is not used in this insn.  */
12305
12306       if (GET_CODE (dest) == MEM)
12307         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
12308                      to_insn, pnotes);
12309       return;
12310     }
12311
12312   else if (GET_CODE (x) == CLOBBER)
12313     return;
12314
12315   len = GET_RTX_LENGTH (code);
12316   fmt = GET_RTX_FORMAT (code);
12317
12318   for (i = 0; i < len; i++)
12319     {
12320       if (fmt[i] == 'E')
12321         {
12322           int j;
12323           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12324             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
12325                          to_insn, pnotes);
12326         }
12327       else if (fmt[i] == 'e')
12328         move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
12329     }
12330 }
12331 \f
12332 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12333    pattern of an insn.  X must be a REG.  */
12334
12335 static int
12336 reg_bitfield_target_p (rtx x, rtx body)
12337 {
12338   int i;
12339
12340   if (GET_CODE (body) == SET)
12341     {
12342       rtx dest = SET_DEST (body);
12343       rtx target;
12344       unsigned int regno, tregno, endregno, endtregno;
12345
12346       if (GET_CODE (dest) == ZERO_EXTRACT)
12347         target = XEXP (dest, 0);
12348       else if (GET_CODE (dest) == STRICT_LOW_PART)
12349         target = SUBREG_REG (XEXP (dest, 0));
12350       else
12351         return 0;
12352
12353       if (GET_CODE (target) == SUBREG)
12354         target = SUBREG_REG (target);
12355
12356       if (GET_CODE (target) != REG)
12357         return 0;
12358
12359       tregno = REGNO (target), regno = REGNO (x);
12360       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12361         return target == x;
12362
12363       endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
12364       endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12365
12366       return endregno > tregno && regno < endtregno;
12367     }
12368
12369   else if (GET_CODE (body) == PARALLEL)
12370     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12371       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12372         return 1;
12373
12374   return 0;
12375 }
12376 \f
12377 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12378    as appropriate.  I3 and I2 are the insns resulting from the combination
12379    insns including FROM (I2 may be zero).
12380
12381    Each note in the list is either ignored or placed on some insns, depending
12382    on the type of note.  */
12383
12384 static void
12385 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
12386 {
12387   rtx note, next_note;
12388   rtx tem;
12389
12390   for (note = notes; note; note = next_note)
12391     {
12392       rtx place = 0, place2 = 0;
12393
12394       /* If this NOTE references a pseudo register, ensure it references
12395          the latest copy of that register.  */
12396       if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
12397           && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
12398         XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
12399
12400       next_note = XEXP (note, 1);
12401       switch (REG_NOTE_KIND (note))
12402         {
12403         case REG_BR_PROB:
12404         case REG_BR_PRED:
12405           /* Doesn't matter much where we put this, as long as it's somewhere.
12406              It is preferable to keep these notes on branches, which is most
12407              likely to be i3.  */
12408           place = i3;
12409           break;
12410
12411         case REG_VALUE_PROFILE:
12412           /* Just get rid of this note, as it is unused later anyway.  */
12413           break;
12414
12415         case REG_VTABLE_REF:
12416           /* ??? Should remain with *a particular* memory load.  Given the
12417              nature of vtable data, the last insn seems relatively safe.  */
12418           place = i3;
12419           break;
12420
12421         case REG_NON_LOCAL_GOTO:
12422           if (GET_CODE (i3) == JUMP_INSN)
12423             place = i3;
12424           else if (i2 && GET_CODE (i2) == JUMP_INSN)
12425             place = i2;
12426           else
12427             abort ();
12428           break;
12429
12430         case REG_EH_REGION:
12431           /* These notes must remain with the call or trapping instruction.  */
12432           if (GET_CODE (i3) == CALL_INSN)
12433             place = i3;
12434           else if (i2 && GET_CODE (i2) == CALL_INSN)
12435             place = i2;
12436           else if (flag_non_call_exceptions)
12437             {
12438               if (may_trap_p (i3))
12439                 place = i3;
12440               else if (i2 && may_trap_p (i2))
12441                 place = i2;
12442               /* ??? Otherwise assume we've combined things such that we
12443                  can now prove that the instructions can't trap.  Drop the
12444                  note in this case.  */
12445             }
12446           else
12447             abort ();
12448           break;
12449
12450         case REG_ALWAYS_RETURN:
12451         case REG_NORETURN:
12452         case REG_SETJMP:
12453           /* These notes must remain with the call.  It should not be
12454              possible for both I2 and I3 to be a call.  */
12455           if (GET_CODE (i3) == CALL_INSN)
12456             place = i3;
12457           else if (i2 && GET_CODE (i2) == CALL_INSN)
12458             place = i2;
12459           else
12460             abort ();
12461           break;
12462
12463         case REG_UNUSED:
12464           /* Any clobbers for i3 may still exist, and so we must process
12465              REG_UNUSED notes from that insn.
12466
12467              Any clobbers from i2 or i1 can only exist if they were added by
12468              recog_for_combine.  In that case, recog_for_combine created the
12469              necessary REG_UNUSED notes.  Trying to keep any original
12470              REG_UNUSED notes from these insns can cause incorrect output
12471              if it is for the same register as the original i3 dest.
12472              In that case, we will notice that the register is set in i3,
12473              and then add a REG_UNUSED note for the destination of i3, which
12474              is wrong.  However, it is possible to have REG_UNUSED notes from
12475              i2 or i1 for register which were both used and clobbered, so
12476              we keep notes from i2 or i1 if they will turn into REG_DEAD
12477              notes.  */
12478
12479           /* If this register is set or clobbered in I3, put the note there
12480              unless there is one already.  */
12481           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12482             {
12483               if (from_insn != i3)
12484                 break;
12485
12486               if (! (GET_CODE (XEXP (note, 0)) == REG
12487                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12488                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12489                 place = i3;
12490             }
12491           /* Otherwise, if this register is used by I3, then this register
12492              now dies here, so we must put a REG_DEAD note here unless there
12493              is one already.  */
12494           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12495                    && ! (GET_CODE (XEXP (note, 0)) == REG
12496                          ? find_regno_note (i3, REG_DEAD,
12497                                             REGNO (XEXP (note, 0)))
12498                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12499             {
12500               PUT_REG_NOTE_KIND (note, REG_DEAD);
12501               place = i3;
12502             }
12503           break;
12504
12505         case REG_EQUAL:
12506         case REG_EQUIV:
12507         case REG_NOALIAS:
12508           /* These notes say something about results of an insn.  We can
12509              only support them if they used to be on I3 in which case they
12510              remain on I3.  Otherwise they are ignored.
12511
12512              If the note refers to an expression that is not a constant, we
12513              must also ignore the note since we cannot tell whether the
12514              equivalence is still true.  It might be possible to do
12515              slightly better than this (we only have a problem if I2DEST
12516              or I1DEST is present in the expression), but it doesn't
12517              seem worth the trouble.  */
12518
12519           if (from_insn == i3
12520               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12521             place = i3;
12522           break;
12523
12524         case REG_INC:
12525         case REG_NO_CONFLICT:
12526           /* These notes say something about how a register is used.  They must
12527              be present on any use of the register in I2 or I3.  */
12528           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12529             place = i3;
12530
12531           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12532             {
12533               if (place)
12534                 place2 = i2;
12535               else
12536                 place = i2;
12537             }
12538           break;
12539
12540         case REG_LABEL:
12541           /* This can show up in several ways -- either directly in the
12542              pattern, or hidden off in the constant pool with (or without?)
12543              a REG_EQUAL note.  */
12544           /* ??? Ignore the without-reg_equal-note problem for now.  */
12545           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12546               || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12547                   && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12548                   && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12549             place = i3;
12550
12551           if (i2
12552               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12553                   || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12554                       && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12555                       && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12556             {
12557               if (place)
12558                 place2 = i2;
12559               else
12560                 place = i2;
12561             }
12562
12563           /* Don't attach REG_LABEL note to a JUMP_INSN which has
12564              JUMP_LABEL already.  Instead, decrement LABEL_NUSES.  */
12565           if (place && GET_CODE (place) == JUMP_INSN && JUMP_LABEL (place))
12566             {
12567               if (JUMP_LABEL (place) != XEXP (note, 0))
12568                 abort ();
12569               if (GET_CODE (JUMP_LABEL (place)) == CODE_LABEL)
12570                 LABEL_NUSES (JUMP_LABEL (place))--;
12571               place = 0;
12572             }
12573           if (place2 && GET_CODE (place2) == JUMP_INSN && JUMP_LABEL (place2))
12574             {
12575               if (JUMP_LABEL (place2) != XEXP (note, 0))
12576                 abort ();
12577               if (GET_CODE (JUMP_LABEL (place2)) == CODE_LABEL)
12578                 LABEL_NUSES (JUMP_LABEL (place2))--;
12579               place2 = 0;
12580             }
12581           break;
12582
12583         case REG_NONNEG:
12584           /* This note says something about the value of a register prior
12585              to the execution of an insn.  It is too much trouble to see
12586              if the note is still correct in all situations.  It is better
12587              to simply delete it.  */
12588           break;
12589
12590         case REG_RETVAL:
12591           /* If the insn previously containing this note still exists,
12592              put it back where it was.  Otherwise move it to the previous
12593              insn.  Adjust the corresponding REG_LIBCALL note.  */
12594           if (GET_CODE (from_insn) != NOTE)
12595             place = from_insn;
12596           else
12597             {
12598               tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12599               place = prev_real_insn (from_insn);
12600               if (tem && place)
12601                 XEXP (tem, 0) = place;
12602               /* If we're deleting the last remaining instruction of a
12603                  libcall sequence, don't add the notes.  */
12604               else if (XEXP (note, 0) == from_insn)
12605                 tem = place = 0;
12606             }
12607           break;
12608
12609         case REG_LIBCALL:
12610           /* This is handled similarly to REG_RETVAL.  */
12611           if (GET_CODE (from_insn) != NOTE)
12612             place = from_insn;
12613           else
12614             {
12615               tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12616               place = next_real_insn (from_insn);
12617               if (tem && place)
12618                 XEXP (tem, 0) = place;
12619               /* If we're deleting the last remaining instruction of a
12620                  libcall sequence, don't add the notes.  */
12621               else if (XEXP (note, 0) == from_insn)
12622                 tem = place = 0;
12623             }
12624           break;
12625
12626         case REG_DEAD:
12627           /* If the register is used as an input in I3, it dies there.
12628              Similarly for I2, if it is nonzero and adjacent to I3.
12629
12630              If the register is not used as an input in either I3 or I2
12631              and it is not one of the registers we were supposed to eliminate,
12632              there are two possibilities.  We might have a non-adjacent I2
12633              or we might have somehow eliminated an additional register
12634              from a computation.  For example, we might have had A & B where
12635              we discover that B will always be zero.  In this case we will
12636              eliminate the reference to A.
12637
12638              In both cases, we must search to see if we can find a previous
12639              use of A and put the death note there.  */
12640
12641           if (from_insn
12642               && GET_CODE (from_insn) == CALL_INSN
12643               && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12644             place = from_insn;
12645           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12646             place = i3;
12647           else if (i2 != 0 && next_nonnote_insn (i2) == i3
12648                    && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12649             place = i2;
12650
12651           if (place == 0)
12652             {
12653               basic_block bb = this_basic_block;
12654
12655               for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12656                 {
12657                   if (! INSN_P (tem))
12658                     {
12659                       if (tem == bb->head)
12660                         break;
12661                       continue;
12662                     }
12663
12664                   /* If the register is being set at TEM, see if that is all
12665                      TEM is doing.  If so, delete TEM.  Otherwise, make this
12666                      into a REG_UNUSED note instead.  */
12667                   if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
12668                     {
12669                       rtx set = single_set (tem);
12670                       rtx inner_dest = 0;
12671 #ifdef HAVE_cc0
12672                       rtx cc0_setter = NULL_RTX;
12673 #endif
12674
12675                       if (set != 0)
12676                         for (inner_dest = SET_DEST (set);
12677                              (GET_CODE (inner_dest) == STRICT_LOW_PART
12678                               || GET_CODE (inner_dest) == SUBREG
12679                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
12680                              inner_dest = XEXP (inner_dest, 0))
12681                           ;
12682
12683                       /* Verify that it was the set, and not a clobber that
12684                          modified the register.
12685
12686                          CC0 targets must be careful to maintain setter/user
12687                          pairs.  If we cannot delete the setter due to side
12688                          effects, mark the user with an UNUSED note instead
12689                          of deleting it.  */
12690
12691                       if (set != 0 && ! side_effects_p (SET_SRC (set))
12692                           && rtx_equal_p (XEXP (note, 0), inner_dest)
12693 #ifdef HAVE_cc0
12694                           && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12695                               || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12696                                   && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12697 #endif
12698                           )
12699                         {
12700                           /* Move the notes and links of TEM elsewhere.
12701                              This might delete other dead insns recursively.
12702                              First set the pattern to something that won't use
12703                              any register.  */
12704
12705                           PATTERN (tem) = pc_rtx;
12706
12707                           distribute_notes (REG_NOTES (tem), tem, tem,
12708                                             NULL_RTX);
12709                           distribute_links (LOG_LINKS (tem));
12710
12711                           PUT_CODE (tem, NOTE);
12712                           NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
12713                           NOTE_SOURCE_FILE (tem) = 0;
12714
12715 #ifdef HAVE_cc0
12716                           /* Delete the setter too.  */
12717                           if (cc0_setter)
12718                             {
12719                               PATTERN (cc0_setter) = pc_rtx;
12720
12721                               distribute_notes (REG_NOTES (cc0_setter),
12722                                                 cc0_setter, cc0_setter,
12723                                                 NULL_RTX);
12724                               distribute_links (LOG_LINKS (cc0_setter));
12725
12726                               PUT_CODE (cc0_setter, NOTE);
12727                               NOTE_LINE_NUMBER (cc0_setter)
12728                                 = NOTE_INSN_DELETED;
12729                               NOTE_SOURCE_FILE (cc0_setter) = 0;
12730                             }
12731 #endif
12732                         }
12733                       /* If the register is both set and used here, put the
12734                          REG_DEAD note here, but place a REG_UNUSED note
12735                          here too unless there already is one.  */
12736                       else if (reg_referenced_p (XEXP (note, 0),
12737                                                  PATTERN (tem)))
12738                         {
12739                           place = tem;
12740
12741                           if (! find_regno_note (tem, REG_UNUSED,
12742                                                  REGNO (XEXP (note, 0))))
12743                             REG_NOTES (tem)
12744                               = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
12745                                                    REG_NOTES (tem));
12746                         }
12747                       else
12748                         {
12749                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
12750
12751                           /*  If there isn't already a REG_UNUSED note, put one
12752                               here.  */
12753                           if (! find_regno_note (tem, REG_UNUSED,
12754                                                  REGNO (XEXP (note, 0))))
12755                             place = tem;
12756                           break;
12757                         }
12758                     }
12759                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12760                            || (GET_CODE (tem) == CALL_INSN
12761                                && find_reg_fusage (tem, USE, XEXP (note, 0))))
12762                     {
12763                       place = tem;
12764
12765                       /* If we are doing a 3->2 combination, and we have a
12766                          register which formerly died in i3 and was not used
12767                          by i2, which now no longer dies in i3 and is used in
12768                          i2 but does not die in i2, and place is between i2
12769                          and i3, then we may need to move a link from place to
12770                          i2.  */
12771                       if (i2 && INSN_UID (place) <= max_uid_cuid
12772                           && INSN_CUID (place) > INSN_CUID (i2)
12773                           && from_insn
12774                           && INSN_CUID (from_insn) > INSN_CUID (i2)
12775                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12776                         {
12777                           rtx links = LOG_LINKS (place);
12778                           LOG_LINKS (place) = 0;
12779                           distribute_links (links);
12780                         }
12781                       break;
12782                     }
12783
12784                   if (tem == bb->head)
12785                     break;
12786                 }
12787
12788               /* We haven't found an insn for the death note and it
12789                  is still a REG_DEAD note, but we have hit the beginning
12790                  of the block.  If the existing life info says the reg
12791                  was dead, there's nothing left to do.  Otherwise, we'll
12792                  need to do a global life update after combine.  */
12793               if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12794                   && REGNO_REG_SET_P (bb->global_live_at_start,
12795                                       REGNO (XEXP (note, 0))))
12796                 SET_BIT (refresh_blocks, this_basic_block->index);
12797             }
12798
12799           /* If the register is set or already dead at PLACE, we needn't do
12800              anything with this note if it is still a REG_DEAD note.
12801              We can here if it is set at all, not if is it totally replace,
12802              which is what `dead_or_set_p' checks, so also check for it being
12803              set partially.  */
12804
12805           if (place && REG_NOTE_KIND (note) == REG_DEAD)
12806             {
12807               unsigned int regno = REGNO (XEXP (note, 0));
12808
12809               /* Similarly, if the instruction on which we want to place
12810                  the note is a noop, we'll need do a global live update
12811                  after we remove them in delete_noop_moves.  */
12812               if (noop_move_p (place))
12813                 SET_BIT (refresh_blocks, this_basic_block->index);
12814
12815               if (dead_or_set_p (place, XEXP (note, 0))
12816                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12817                 {
12818                   /* Unless the register previously died in PLACE, clear
12819                      reg_last_death.  [I no longer understand why this is
12820                      being done.] */
12821                   if (reg_last_death[regno] != place)
12822                     reg_last_death[regno] = 0;
12823                   place = 0;
12824                 }
12825               else
12826                 reg_last_death[regno] = place;
12827
12828               /* If this is a death note for a hard reg that is occupying
12829                  multiple registers, ensure that we are still using all
12830                  parts of the object.  If we find a piece of the object
12831                  that is unused, we must arrange for an appropriate REG_DEAD
12832                  note to be added for it.  However, we can't just emit a USE
12833                  and tag the note to it, since the register might actually
12834                  be dead; so we recourse, and the recursive call then finds
12835                  the previous insn that used this register.  */
12836
12837               if (place && regno < FIRST_PSEUDO_REGISTER
12838                   && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
12839                 {
12840                   unsigned int endregno
12841                     = regno + HARD_REGNO_NREGS (regno,
12842                                                 GET_MODE (XEXP (note, 0)));
12843                   int all_used = 1;
12844                   unsigned int i;
12845
12846                   for (i = regno; i < endregno; i++)
12847                     if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12848                          && ! find_regno_fusage (place, USE, i))
12849                         || dead_or_set_regno_p (place, i))
12850                       all_used = 0;
12851
12852                   if (! all_used)
12853                     {
12854                       /* Put only REG_DEAD notes for pieces that are
12855                          not already dead or set.  */
12856
12857                       for (i = regno; i < endregno;
12858                            i += HARD_REGNO_NREGS (i, reg_raw_mode[i]))
12859                         {
12860                           rtx piece = regno_reg_rtx[i];
12861                           basic_block bb = this_basic_block;
12862
12863                           if (! dead_or_set_p (place, piece)
12864                               && ! reg_bitfield_target_p (piece,
12865                                                           PATTERN (place)))
12866                             {
12867                               rtx new_note
12868                                 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12869
12870                               distribute_notes (new_note, place, place,
12871                                                 NULL_RTX);
12872                             }
12873                           else if (! refers_to_regno_p (i, i + 1,
12874                                                         PATTERN (place), 0)
12875                                    && ! find_regno_fusage (place, USE, i))
12876                             for (tem = PREV_INSN (place); ;
12877                                  tem = PREV_INSN (tem))
12878                               {
12879                                 if (! INSN_P (tem))
12880                                   {
12881                                     if (tem == bb->head)
12882                                       {
12883                                         SET_BIT (refresh_blocks,
12884                                                  this_basic_block->index);
12885                                         break;
12886                                       }
12887                                     continue;
12888                                   }
12889                                 if (dead_or_set_p (tem, piece)
12890                                     || reg_bitfield_target_p (piece,
12891                                                               PATTERN (tem)))
12892                                   {
12893                                     REG_NOTES (tem)
12894                                       = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12895                                                            REG_NOTES (tem));
12896                                     break;
12897                                   }
12898                               }
12899
12900                         }
12901
12902                       place = 0;
12903                     }
12904                 }
12905             }
12906           break;
12907
12908         default:
12909           /* Any other notes should not be present at this point in the
12910              compilation.  */
12911           abort ();
12912         }
12913
12914       if (place)
12915         {
12916           XEXP (note, 1) = REG_NOTES (place);
12917           REG_NOTES (place) = note;
12918         }
12919       else if ((REG_NOTE_KIND (note) == REG_DEAD
12920                 || REG_NOTE_KIND (note) == REG_UNUSED)
12921                && GET_CODE (XEXP (note, 0)) == REG)
12922         REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12923
12924       if (place2)
12925         {
12926           if ((REG_NOTE_KIND (note) == REG_DEAD
12927                || REG_NOTE_KIND (note) == REG_UNUSED)
12928               && GET_CODE (XEXP (note, 0)) == REG)
12929             REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12930
12931           REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12932                                                REG_NOTE_KIND (note),
12933                                                XEXP (note, 0),
12934                                                REG_NOTES (place2));
12935         }
12936     }
12937 }
12938 \f
12939 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12940    I3, I2, and I1 to new locations.  This is also called to add a link
12941    pointing at I3 when I3's destination is changed.  */
12942
12943 static void
12944 distribute_links (rtx links)
12945 {
12946   rtx link, next_link;
12947
12948   for (link = links; link; link = next_link)
12949     {
12950       rtx place = 0;
12951       rtx insn;
12952       rtx set, reg;
12953
12954       next_link = XEXP (link, 1);
12955
12956       /* If the insn that this link points to is a NOTE or isn't a single
12957          set, ignore it.  In the latter case, it isn't clear what we
12958          can do other than ignore the link, since we can't tell which
12959          register it was for.  Such links wouldn't be used by combine
12960          anyway.
12961
12962          It is not possible for the destination of the target of the link to
12963          have been changed by combine.  The only potential of this is if we
12964          replace I3, I2, and I1 by I3 and I2.  But in that case the
12965          destination of I2 also remains unchanged.  */
12966
12967       if (GET_CODE (XEXP (link, 0)) == NOTE
12968           || (set = single_set (XEXP (link, 0))) == 0)
12969         continue;
12970
12971       reg = SET_DEST (set);
12972       while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12973              || GET_CODE (reg) == SIGN_EXTRACT
12974              || GET_CODE (reg) == STRICT_LOW_PART)
12975         reg = XEXP (reg, 0);
12976
12977       /* A LOG_LINK is defined as being placed on the first insn that uses
12978          a register and points to the insn that sets the register.  Start
12979          searching at the next insn after the target of the link and stop
12980          when we reach a set of the register or the end of the basic block.
12981
12982          Note that this correctly handles the link that used to point from
12983          I3 to I2.  Also note that not much searching is typically done here
12984          since most links don't point very far away.  */
12985
12986       for (insn = NEXT_INSN (XEXP (link, 0));
12987            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12988                      || this_basic_block->next_bb->head != insn));
12989            insn = NEXT_INSN (insn))
12990         if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12991           {
12992             if (reg_referenced_p (reg, PATTERN (insn)))
12993               place = insn;
12994             break;
12995           }
12996         else if (GET_CODE (insn) == CALL_INSN
12997                  && find_reg_fusage (insn, USE, reg))
12998           {
12999             place = insn;
13000             break;
13001           }
13002
13003       /* If we found a place to put the link, place it there unless there
13004          is already a link to the same insn as LINK at that point.  */
13005
13006       if (place)
13007         {
13008           rtx link2;
13009
13010           for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
13011             if (XEXP (link2, 0) == XEXP (link, 0))
13012               break;
13013
13014           if (link2 == 0)
13015             {
13016               XEXP (link, 1) = LOG_LINKS (place);
13017               LOG_LINKS (place) = link;
13018
13019               /* Set added_links_insn to the earliest insn we added a
13020                  link to.  */
13021               if (added_links_insn == 0
13022                   || INSN_CUID (added_links_insn) > INSN_CUID (place))
13023                 added_links_insn = place;
13024             }
13025         }
13026     }
13027 }
13028 \f
13029 /* Compute INSN_CUID for INSN, which is an insn made by combine.  */
13030
13031 static int
13032 insn_cuid (rtx insn)
13033 {
13034   while (insn != 0 && INSN_UID (insn) > max_uid_cuid
13035          && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
13036     insn = NEXT_INSN (insn);
13037
13038   if (INSN_UID (insn) > max_uid_cuid)
13039     abort ();
13040
13041   return INSN_CUID (insn);
13042 }
13043 \f
13044 void
13045 dump_combine_stats (FILE *file)
13046 {
13047   fnotice
13048     (file,
13049      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13050      combine_attempts, combine_merges, combine_extras, combine_successes);
13051 }
13052
13053 void
13054 dump_combine_total_stats (FILE *file)
13055 {
13056   fnotice
13057     (file,
13058      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13059      total_attempts, total_merges, total_extras, total_successes);
13060 }